LLVM 22.0.0git
CorrelatedValuePropagation.cpp
Go to the documentation of this file.
1//===- CorrelatedValuePropagation.cpp - Propagate CFG-derived info --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Correlated Value Propagation pass.
10//
11//===----------------------------------------------------------------------===//
12
16#include "llvm/ADT/Statistic.h"
22#include "llvm/IR/Attributes.h"
23#include "llvm/IR/BasicBlock.h"
24#include "llvm/IR/CFG.h"
25#include "llvm/IR/Constant.h"
27#include "llvm/IR/Constants.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/IRBuilder.h"
31#include "llvm/IR/InstrTypes.h"
32#include "llvm/IR/Instruction.h"
35#include "llvm/IR/Operator.h"
36#include "llvm/IR/PassManager.h"
38#include "llvm/IR/Type.h"
39#include "llvm/IR/Value.h"
42#include <cassert>
43#include <optional>
44#include <utility>
45
46using namespace llvm;
47
48#define DEBUG_TYPE "correlated-value-propagation"
49
50STATISTIC(NumPhis, "Number of phis propagated");
51STATISTIC(NumPhiCommon, "Number of phis deleted via common incoming value");
52STATISTIC(NumSelects, "Number of selects propagated");
53STATISTIC(NumCmps, "Number of comparisons propagated");
54STATISTIC(NumReturns, "Number of return values propagated");
55STATISTIC(NumDeadCases, "Number of switch cases removed");
56STATISTIC(NumSDivSRemsNarrowed,
57 "Number of sdivs/srems whose width was decreased");
58STATISTIC(NumSDivs, "Number of sdiv converted to udiv");
59STATISTIC(NumUDivURemsNarrowed,
60 "Number of udivs/urems whose width was decreased");
61STATISTIC(NumAShrsConverted, "Number of ashr converted to lshr");
62STATISTIC(NumAShrsRemoved, "Number of ashr removed");
63STATISTIC(NumSRems, "Number of srem converted to urem");
64STATISTIC(NumSExt, "Number of sext converted to zext");
65STATISTIC(NumSIToFP, "Number of sitofp converted to uitofp");
66STATISTIC(NumSICmps, "Number of signed icmp preds simplified to unsigned");
67STATISTIC(NumAnd, "Number of ands removed");
68STATISTIC(NumNW, "Number of no-wrap deductions");
69STATISTIC(NumNSW, "Number of no-signed-wrap deductions");
70STATISTIC(NumNUW, "Number of no-unsigned-wrap deductions");
71STATISTIC(NumAddNW, "Number of no-wrap deductions for add");
72STATISTIC(NumAddNSW, "Number of no-signed-wrap deductions for add");
73STATISTIC(NumAddNUW, "Number of no-unsigned-wrap deductions for add");
74STATISTIC(NumSubNW, "Number of no-wrap deductions for sub");
75STATISTIC(NumSubNSW, "Number of no-signed-wrap deductions for sub");
76STATISTIC(NumSubNUW, "Number of no-unsigned-wrap deductions for sub");
77STATISTIC(NumMulNW, "Number of no-wrap deductions for mul");
78STATISTIC(NumMulNSW, "Number of no-signed-wrap deductions for mul");
79STATISTIC(NumMulNUW, "Number of no-unsigned-wrap deductions for mul");
80STATISTIC(NumShlNW, "Number of no-wrap deductions for shl");
81STATISTIC(NumShlNSW, "Number of no-signed-wrap deductions for shl");
82STATISTIC(NumShlNUW, "Number of no-unsigned-wrap deductions for shl");
83STATISTIC(NumAbs, "Number of llvm.abs intrinsics removed");
84STATISTIC(NumOverflows, "Number of overflow checks removed");
85STATISTIC(NumSaturating,
86 "Number of saturating arithmetics converted to normal arithmetics");
87STATISTIC(NumNonNull, "Number of function pointer arguments marked non-null");
88STATISTIC(NumCmpIntr, "Number of llvm.[us]cmp intrinsics removed");
89STATISTIC(NumMinMax, "Number of llvm.[us]{min,max} intrinsics removed");
90STATISTIC(NumSMinMax,
91 "Number of llvm.s{min,max} intrinsics simplified to unsigned");
92STATISTIC(NumUDivURemsNarrowedExpanded,
93 "Number of bound udiv's/urem's expanded");
94STATISTIC(NumNNeg, "Number of zext/uitofp non-negative deductions");
95
97 if (Constant *C = LVI->getConstant(V, At))
98 return C;
99
100 // TODO: The following really should be sunk inside LVI's core algorithm, or
101 // at least the outer shims around such.
102 auto *C = dyn_cast<CmpInst>(V);
103 if (!C)
104 return nullptr;
105
106 Value *Op0 = C->getOperand(0);
107 Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
108 if (!Op1)
109 return nullptr;
110
111 return LVI->getPredicateAt(C->getPredicate(), Op0, Op1, At,
112 /*UseBlockValue=*/false);
113}
114
116 if (S->getType()->isVectorTy() || isa<Constant>(S->getCondition()))
117 return false;
118
119 bool Changed = false;
120 for (Use &U : make_early_inc_range(S->uses())) {
121 auto *I = cast<Instruction>(U.getUser());
122 Constant *C;
123 if (auto *PN = dyn_cast<PHINode>(I))
124 C = LVI->getConstantOnEdge(S->getCondition(), PN->getIncomingBlock(U),
125 I->getParent(), I);
126 else
127 C = getConstantAt(S->getCondition(), I, LVI);
128
129 auto *CI = dyn_cast_or_null<ConstantInt>(C);
130 if (!CI)
131 continue;
132
133 U.set(CI->isOne() ? S->getTrueValue() : S->getFalseValue());
134 Changed = true;
135 ++NumSelects;
136 }
137
138 if (Changed && S->use_empty())
139 S->eraseFromParent();
140
141 return Changed;
142}
143
144/// Try to simplify a phi with constant incoming values that match the edge
145/// values of a non-constant value on all other edges:
146/// bb0:
147/// %isnull = icmp eq i8* %x, null
148/// br i1 %isnull, label %bb2, label %bb1
149/// bb1:
150/// br label %bb2
151/// bb2:
152/// %r = phi i8* [ %x, %bb1 ], [ null, %bb0 ]
153/// -->
154/// %r = %x
156 DominatorTree *DT) {
157 // Collect incoming constants and initialize possible common value.
159 Value *CommonValue = nullptr;
160 for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) {
161 Value *Incoming = P->getIncomingValue(i);
162 if (auto *IncomingConstant = dyn_cast<Constant>(Incoming)) {
163 IncomingConstants.push_back(std::make_pair(IncomingConstant, i));
164 } else if (!CommonValue) {
165 // The potential common value is initialized to the first non-constant.
166 CommonValue = Incoming;
167 } else if (Incoming != CommonValue) {
168 // There can be only one non-constant common value.
169 return false;
170 }
171 }
172
173 if (!CommonValue || IncomingConstants.empty())
174 return false;
175
176 // The common value must be valid in all incoming blocks.
177 BasicBlock *ToBB = P->getParent();
178 if (auto *CommonInst = dyn_cast<Instruction>(CommonValue))
179 if (!DT->dominates(CommonInst, ToBB))
180 return false;
181
182 // We have a phi with exactly 1 variable incoming value and 1 or more constant
183 // incoming values. See if all constant incoming values can be mapped back to
184 // the same incoming variable value.
185 for (auto &IncomingConstant : IncomingConstants) {
186 Constant *C = IncomingConstant.first;
187 BasicBlock *IncomingBB = P->getIncomingBlock(IncomingConstant.second);
188 if (C != LVI->getConstantOnEdge(CommonValue, IncomingBB, ToBB, P))
189 return false;
190 }
191
192 // LVI only guarantees that the value matches a certain constant if the value
193 // is not poison. Make sure we don't replace a well-defined value with poison.
194 // This is usually satisfied due to a prior branch on the value.
195 if (!isGuaranteedNotToBePoison(CommonValue, nullptr, P, DT))
196 return false;
197
198 // All constant incoming values map to the same variable along the incoming
199 // edges of the phi. The phi is unnecessary.
200 P->replaceAllUsesWith(CommonValue);
201 P->eraseFromParent();
202 ++NumPhiCommon;
203 return true;
204}
205
208 Instruction *CxtI) {
209 if (Constant *C = LVI->getConstantOnEdge(Incoming, From, To, CxtI))
210 return C;
211
212 // Look if the incoming value is a select with a scalar condition for which
213 // LVI can tells us the value. In that case replace the incoming value with
214 // the appropriate value of the select. This often allows us to remove the
215 // select later.
216 auto *SI = dyn_cast<SelectInst>(Incoming);
217 if (!SI)
218 return nullptr;
219
220 // Once LVI learns to handle vector types, we could also add support
221 // for vector type constants that are not all zeroes or all ones.
222 Value *Condition = SI->getCondition();
223 if (!Condition->getType()->isVectorTy()) {
224 if (Constant *C = LVI->getConstantOnEdge(Condition, From, To, CxtI)) {
225 if (C->isOneValue())
226 return SI->getTrueValue();
227 if (C->isZeroValue())
228 return SI->getFalseValue();
229 }
230 }
231
232 // Look if the select has a constant but LVI tells us that the incoming
233 // value can never be that constant. In that case replace the incoming
234 // value with the other value of the select. This often allows us to
235 // remove the select later.
236
237 // The "false" case
238 if (auto *C = dyn_cast<Constant>(SI->getFalseValue()))
239 if (auto *Res = dyn_cast_or_null<ConstantInt>(
240 LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C, From, To, CxtI));
241 Res && Res->isZero())
242 return SI->getTrueValue();
243
244 // The "true" case,
245 // similar to the select "false" case, but try the select "true" value
246 if (auto *C = dyn_cast<Constant>(SI->getTrueValue()))
247 if (auto *Res = dyn_cast_or_null<ConstantInt>(
248 LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C, From, To, CxtI));
249 Res && Res->isZero())
250 return SI->getFalseValue();
251
252 return nullptr;
253}
254
256 const SimplifyQuery &SQ) {
257 bool Changed = false;
258
259 BasicBlock *BB = P->getParent();
260 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
261 Value *Incoming = P->getIncomingValue(i);
262 if (isa<Constant>(Incoming)) continue;
263
264 Value *V = getValueOnEdge(LVI, Incoming, P->getIncomingBlock(i), BB, P);
265 if (V) {
266 P->setIncomingValue(i, V);
267 Changed = true;
268 }
269 }
270
271 if (Value *V = simplifyInstruction(P, SQ)) {
272 P->replaceAllUsesWith(V);
273 P->eraseFromParent();
274 Changed = true;
275 }
276
277 if (!Changed)
278 Changed = simplifyCommonValuePhi(P, LVI, DT);
279
280 if (Changed)
281 ++NumPhis;
282
283 return Changed;
284}
285
286static bool processICmp(ICmpInst *Cmp, LazyValueInfo *LVI) {
287 // Only for signed relational comparisons of integers.
288 if (!Cmp->getOperand(0)->getType()->isIntOrIntVectorTy())
289 return false;
290
291 if (!Cmp->isSigned() && (!Cmp->isUnsigned() || Cmp->hasSameSign()))
292 return false;
293
294 bool Changed = false;
295
296 ConstantRange CR1 = LVI->getConstantRangeAtUse(Cmp->getOperandUse(0),
297 /*UndefAllowed=*/false),
298 CR2 = LVI->getConstantRangeAtUse(Cmp->getOperandUse(1),
299 /*UndefAllowed=*/false);
300
301 if (Cmp->isSigned()) {
302 ICmpInst::Predicate UnsignedPred =
304 Cmp->getPredicate(), CR1, CR2);
305
306 if (UnsignedPred == ICmpInst::Predicate::BAD_ICMP_PREDICATE)
307 return false;
308
309 ++NumSICmps;
310 Cmp->setPredicate(UnsignedPred);
311 Changed = true;
312 }
313
315 Cmp->setSameSign();
316 Changed = true;
317 }
318
319 return Changed;
320}
321
322/// See if LazyValueInfo's ability to exploit edge conditions or range
323/// information is sufficient to prove this comparison. Even for local
324/// conditions, this can sometimes prove conditions instcombine can't by
325/// exploiting range information.
326static bool constantFoldCmp(CmpInst *Cmp, LazyValueInfo *LVI) {
327 Value *Op0 = Cmp->getOperand(0);
328 Value *Op1 = Cmp->getOperand(1);
329 Constant *Res = LVI->getPredicateAt(Cmp->getPredicate(), Op0, Op1, Cmp,
330 /*UseBlockValue=*/true);
331 if (!Res)
332 return false;
333
334 ++NumCmps;
335 Cmp->replaceAllUsesWith(Res);
336 Cmp->eraseFromParent();
337 return true;
338}
339
340static bool processCmp(CmpInst *Cmp, LazyValueInfo *LVI) {
341 if (constantFoldCmp(Cmp, LVI))
342 return true;
343
344 if (auto *ICmp = dyn_cast<ICmpInst>(Cmp))
345 if (processICmp(ICmp, LVI))
346 return true;
347
348 return false;
349}
350
351/// Simplify a switch instruction by removing cases which can never fire. If the
352/// uselessness of a case could be determined locally then constant propagation
353/// would already have figured it out. Instead, walk the predecessors and
354/// statically evaluate cases based on information available on that edge. Cases
355/// that cannot fire no matter what the incoming edge can safely be removed. If
356/// a case fires on every incoming edge then the entire switch can be removed
357/// and replaced with a branch to the case destination.
359 DominatorTree *DT) {
360 DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy);
361 Value *Cond = I->getCondition();
362 BasicBlock *BB = I->getParent();
363
364 // Analyse each switch case in turn.
365 bool Changed = false;
366 DenseMap<BasicBlock*, int> SuccessorsCount;
367 for (auto *Succ : successors(BB))
368 SuccessorsCount[Succ]++;
369
370 { // Scope for SwitchInstProfUpdateWrapper. It must not live during
371 // ConstantFoldTerminator() as the underlying SwitchInst can be changed.
373 ConstantRange CR =
374 LVI->getConstantRangeAtUse(I->getOperandUse(0), /*UndefAllowed=*/false);
375 unsigned ReachableCaseCount = 0;
376
377 for (auto CI = SI->case_begin(), CE = SI->case_end(); CI != CE;) {
378 ConstantInt *Case = CI->getCaseValue();
379 std::optional<bool> Predicate = std::nullopt;
380 if (!CR.contains(Case->getValue()))
381 Predicate = false;
382 else if (CR.isSingleElement() &&
383 *CR.getSingleElement() == Case->getValue())
384 Predicate = true;
385 if (!Predicate) {
386 // Handle missing cases, e.g., the range has a hole.
387 auto *Res = dyn_cast_or_null<ConstantInt>(
389 /* UseBlockValue=*/true));
390 if (Res && Res->isZero())
391 Predicate = false;
392 else if (Res && Res->isOne())
393 Predicate = true;
394 }
395
396 if (Predicate && !*Predicate) {
397 // This case never fires - remove it.
398 BasicBlock *Succ = CI->getCaseSuccessor();
399 Succ->removePredecessor(BB);
400 CI = SI.removeCase(CI);
401 CE = SI->case_end();
402
403 // The condition can be modified by removePredecessor's PHI simplification
404 // logic.
405 Cond = SI->getCondition();
406
407 ++NumDeadCases;
408 Changed = true;
409 if (--SuccessorsCount[Succ] == 0)
410 DTU.applyUpdatesPermissive({{DominatorTree::Delete, BB, Succ}});
411 continue;
412 }
413 if (Predicate && *Predicate) {
414 // This case always fires. Arrange for the switch to be turned into an
415 // unconditional branch by replacing the switch condition with the case
416 // value.
417 SI->setCondition(Case);
418 NumDeadCases += SI->getNumCases();
419 Changed = true;
420 break;
421 }
422
423 // Increment the case iterator since we didn't delete it.
424 ++CI;
425 ++ReachableCaseCount;
426 }
427
428 // The default dest is unreachable if all cases are covered.
429 if (!SI->defaultDestUnreachable() &&
430 !CR.isSizeLargerThan(ReachableCaseCount)) {
431 BasicBlock *DefaultDest = SI->getDefaultDest();
432 BasicBlock *NewUnreachableBB =
433 BasicBlock::Create(BB->getContext(), "default.unreachable",
434 BB->getParent(), DefaultDest);
435 auto *UI = new UnreachableInst(BB->getContext(), NewUnreachableBB);
436 UI->setDebugLoc(DebugLoc::getTemporary());
437
438 DefaultDest->removePredecessor(BB);
439 SI->setDefaultDest(NewUnreachableBB);
440
441 if (SuccessorsCount[DefaultDest] == 1)
442 DTU.applyUpdates({{DominatorTree::Delete, BB, DefaultDest}});
443 DTU.applyUpdates({{DominatorTree::Insert, BB, NewUnreachableBB}});
444
445 ++NumDeadCases;
446 Changed = true;
447 }
448 }
449
450 if (Changed)
451 // If the switch has been simplified to the point where it can be replaced
452 // by a branch then do so now.
453 ConstantFoldTerminator(BB, /*DeleteDeadConditions = */ false,
454 /*TLI = */ nullptr, &DTU);
455 return Changed;
456}
457
458// See if we can prove that the given binary op intrinsic will not overflow.
460 ConstantRange LRange =
461 LVI->getConstantRangeAtUse(BO->getOperandUse(0), /*UndefAllowed*/ false);
462 ConstantRange RRange =
463 LVI->getConstantRangeAtUse(BO->getOperandUse(1), /*UndefAllowed*/ false);
465 BO->getBinaryOp(), RRange, BO->getNoWrapKind());
466 return NWRegion.contains(LRange);
467}
468
470 bool NewNSW, bool NewNUW) {
471 Statistic *OpcNW, *OpcNSW, *OpcNUW;
472 switch (Opcode) {
473 case Instruction::Add:
474 OpcNW = &NumAddNW;
475 OpcNSW = &NumAddNSW;
476 OpcNUW = &NumAddNUW;
477 break;
478 case Instruction::Sub:
479 OpcNW = &NumSubNW;
480 OpcNSW = &NumSubNSW;
481 OpcNUW = &NumSubNUW;
482 break;
483 case Instruction::Mul:
484 OpcNW = &NumMulNW;
485 OpcNSW = &NumMulNSW;
486 OpcNUW = &NumMulNUW;
487 break;
488 case Instruction::Shl:
489 OpcNW = &NumShlNW;
490 OpcNSW = &NumShlNSW;
491 OpcNUW = &NumShlNUW;
492 break;
493 default:
494 llvm_unreachable("Will not be called with other binops");
495 }
496
497 auto *Inst = dyn_cast<Instruction>(V);
498 if (NewNSW) {
499 ++NumNW;
500 ++*OpcNW;
501 ++NumNSW;
502 ++*OpcNSW;
503 if (Inst)
504 Inst->setHasNoSignedWrap();
505 }
506 if (NewNUW) {
507 ++NumNW;
508 ++*OpcNW;
509 ++NumNUW;
510 ++*OpcNUW;
511 if (Inst)
512 Inst->setHasNoUnsignedWrap();
513 }
514}
515
516static bool processBinOp(BinaryOperator *BinOp, LazyValueInfo *LVI);
517
518// See if @llvm.abs argument is alays positive/negative, and simplify.
519// Notably, INT_MIN can belong to either range, regardless of the NSW,
520// because it is negation-invariant.
522 Value *X = II->getArgOperand(0);
523 bool IsIntMinPoison = cast<ConstantInt>(II->getArgOperand(1))->isOne();
524 APInt IntMin = APInt::getSignedMinValue(X->getType()->getScalarSizeInBits());
526 II->getOperandUse(0), /*UndefAllowed*/ IsIntMinPoison);
527
528 // Is X in [0, IntMin]? NOTE: INT_MIN is fine!
529 if (Range.icmp(CmpInst::ICMP_ULE, IntMin)) {
530 ++NumAbs;
531 II->replaceAllUsesWith(X);
532 II->eraseFromParent();
533 return true;
534 }
535
536 // Is X in [IntMin, 0]? NOTE: INT_MIN is fine!
539 Value *NegX = B.CreateNeg(X, II->getName(),
540 /*HasNSW=*/IsIntMinPoison);
541 ++NumAbs;
542 II->replaceAllUsesWith(NegX);
543 II->eraseFromParent();
544
545 // See if we can infer some no-wrap flags.
546 if (auto *BO = dyn_cast<BinaryOperator>(NegX))
547 processBinOp(BO, LVI);
548
549 return true;
550 }
551
552 // Argument's range crosses zero.
553 // Can we at least tell that the argument is never INT_MIN?
554 if (!IsIntMinPoison && !Range.contains(IntMin)) {
555 ++NumNSW;
556 ++NumSubNSW;
557 II->setArgOperand(1, ConstantInt::getTrue(II->getContext()));
558 return true;
559 }
560 return false;
561}
562
564 ConstantRange LHS_CR =
565 LVI->getConstantRangeAtUse(CI->getOperandUse(0), /*UndefAllowed*/ false);
566 ConstantRange RHS_CR =
567 LVI->getConstantRangeAtUse(CI->getOperandUse(1), /*UndefAllowed*/ false);
568
569 if (LHS_CR.icmp(CI->getGTPredicate(), RHS_CR)) {
570 ++NumCmpIntr;
571 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
572 CI->eraseFromParent();
573 return true;
574 }
575 if (LHS_CR.icmp(CI->getLTPredicate(), RHS_CR)) {
576 ++NumCmpIntr;
578 CI->eraseFromParent();
579 return true;
580 }
581 if (LHS_CR.icmp(ICmpInst::ICMP_EQ, RHS_CR)) {
582 ++NumCmpIntr;
583 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 0));
584 CI->eraseFromParent();
585 return true;
586 }
587
588 return false;
589}
590
591// See if this min/max intrinsic always picks it's one specific operand.
592// If not, check whether we can canonicalize signed minmax into unsigned version
596 /*UndefAllowed*/ false);
598 /*UndefAllowed*/ false);
599 if (LHS_CR.icmp(Pred, RHS_CR)) {
600 ++NumMinMax;
601 MM->replaceAllUsesWith(MM->getLHS());
602 MM->eraseFromParent();
603 return true;
604 }
605 if (RHS_CR.icmp(Pred, LHS_CR)) {
606 ++NumMinMax;
607 MM->replaceAllUsesWith(MM->getRHS());
608 MM->eraseFromParent();
609 return true;
610 }
611
612 if (MM->isSigned() &&
614 RHS_CR)) {
615 ++NumSMinMax;
616 IRBuilder<> B(MM);
617 MM->replaceAllUsesWith(B.CreateBinaryIntrinsic(
618 MM->getIntrinsicID() == Intrinsic::smin ? Intrinsic::umin
619 : Intrinsic::umax,
620 MM->getLHS(), MM->getRHS()));
621 MM->eraseFromParent();
622 return true;
623 }
624
625 return false;
626}
627
628// Rewrite this with.overflow intrinsic as non-overflowing.
630 IRBuilder<> B(WO);
631 Instruction::BinaryOps Opcode = WO->getBinaryOp();
632 bool NSW = WO->isSigned();
633 bool NUW = !WO->isSigned();
634
635 Value *NewOp =
636 B.CreateBinOp(Opcode, WO->getLHS(), WO->getRHS(), WO->getName());
637 setDeducedOverflowingFlags(NewOp, Opcode, NSW, NUW);
638
639 StructType *ST = cast<StructType>(WO->getType());
641 { PoisonValue::get(ST->getElementType(0)),
642 ConstantInt::getFalse(ST->getElementType(1)) });
643 Value *NewI = B.CreateInsertValue(Struct, NewOp, 0);
644 WO->replaceAllUsesWith(NewI);
645 WO->eraseFromParent();
646 ++NumOverflows;
647
648 // See if we can infer the other no-wrap too.
649 if (auto *BO = dyn_cast<BinaryOperator>(NewOp))
650 processBinOp(BO, LVI);
651
652 return true;
653}
654
656 Instruction::BinaryOps Opcode = SI->getBinaryOp();
657 bool NSW = SI->isSigned();
658 bool NUW = !SI->isSigned();
660 Opcode, SI->getLHS(), SI->getRHS(), SI->getName(), SI->getIterator());
661 BinOp->setDebugLoc(SI->getDebugLoc());
662 setDeducedOverflowingFlags(BinOp, Opcode, NSW, NUW);
663
664 SI->replaceAllUsesWith(BinOp);
665 SI->eraseFromParent();
666 ++NumSaturating;
667
668 // See if we can infer the other no-wrap too.
669 if (auto *BO = dyn_cast<BinaryOperator>(BinOp))
670 processBinOp(BO, LVI);
671
672 return true;
673}
674
675/// Infer nonnull attributes for the arguments at the specified callsite.
676static bool processCallSite(CallBase &CB, LazyValueInfo *LVI) {
677
678 if (CB.getIntrinsicID() == Intrinsic::abs) {
679 return processAbsIntrinsic(&cast<IntrinsicInst>(CB), LVI);
680 }
681
682 if (auto *CI = dyn_cast<CmpIntrinsic>(&CB)) {
683 return processCmpIntrinsic(CI, LVI);
684 }
685
686 if (auto *MM = dyn_cast<MinMaxIntrinsic>(&CB)) {
687 return processMinMaxIntrinsic(MM, LVI);
688 }
689
690 if (auto *WO = dyn_cast<WithOverflowInst>(&CB)) {
691 if (willNotOverflow(WO, LVI))
692 return processOverflowIntrinsic(WO, LVI);
693 }
694
695 if (auto *SI = dyn_cast<SaturatingInst>(&CB)) {
696 if (willNotOverflow(SI, LVI))
697 return processSaturatingInst(SI, LVI);
698 }
699
700 bool Changed = false;
701
702 // Deopt bundle operands are intended to capture state with minimal
703 // perturbance of the code otherwise. If we can find a constant value for
704 // any such operand and remove a use of the original value, that's
705 // desireable since it may allow further optimization of that value (e.g. via
706 // single use rules in instcombine). Since deopt uses tend to,
707 // idiomatically, appear along rare conditional paths, it's reasonable likely
708 // we may have a conditional fact with which LVI can fold.
709 if (auto DeoptBundle = CB.getOperandBundle(LLVMContext::OB_deopt)) {
710 for (const Use &ConstU : DeoptBundle->Inputs) {
711 Use &U = const_cast<Use&>(ConstU);
712 Value *V = U.get();
713 if (V->getType()->isVectorTy()) continue;
714 if (isa<Constant>(V)) continue;
715
716 Constant *C = LVI->getConstant(V, &CB);
717 if (!C) continue;
718 U.set(C);
719 Changed = true;
720 }
721 }
722
724 unsigned ArgNo = 0;
725
726 for (Value *V : CB.args()) {
727 PointerType *Type = dyn_cast<PointerType>(V->getType());
728 // Try to mark pointer typed parameters as non-null. We skip the
729 // relatively expensive analysis for constants which are obviously either
730 // null or non-null to start with.
731 if (Type && !CB.paramHasAttr(ArgNo, Attribute::NonNull) &&
732 !isa<Constant>(V))
733 if (auto *Res = dyn_cast_or_null<ConstantInt>(LVI->getPredicateAt(
734 ICmpInst::ICMP_EQ, V, ConstantPointerNull::get(Type), &CB,
735 /*UseBlockValue=*/false));
736 Res && Res->isZero())
737 ArgNos.push_back(ArgNo);
738 ArgNo++;
739 }
740
741 assert(ArgNo == CB.arg_size() && "Call arguments not processed correctly.");
742
743 if (ArgNos.empty())
744 return Changed;
745
746 NumNonNull += ArgNos.size();
748 LLVMContext &Ctx = CB.getContext();
749 AS = AS.addParamAttribute(Ctx, ArgNos,
750 Attribute::get(Ctx, Attribute::NonNull));
751 CB.setAttributes(AS);
752
753 return true;
754}
755
757
758static Domain getDomain(const ConstantRange &CR) {
759 if (CR.isAllNonNegative())
760 return Domain::NonNegative;
761 if (CR.icmp(ICmpInst::ICMP_SLE, APInt::getZero(CR.getBitWidth())))
762 return Domain::NonPositive;
763 return Domain::Unknown;
764}
765
766/// Try to shrink a sdiv/srem's width down to the smallest power of two that's
767/// sufficient to contain its operands.
768static bool narrowSDivOrSRem(BinaryOperator *Instr, const ConstantRange &LCR,
769 const ConstantRange &RCR) {
770 assert(Instr->getOpcode() == Instruction::SDiv ||
771 Instr->getOpcode() == Instruction::SRem);
772
773 // Find the smallest power of two bitwidth that's sufficient to hold Instr's
774 // operands.
775 unsigned OrigWidth = Instr->getType()->getScalarSizeInBits();
776
777 // What is the smallest bit width that can accommodate the entire value ranges
778 // of both of the operands?
779 unsigned MinSignedBits =
780 std::max(LCR.getMinSignedBits(), RCR.getMinSignedBits());
781
782 // sdiv/srem is UB if divisor is -1 and divident is INT_MIN, so unless we can
783 // prove that such a combination is impossible, we need to bump the bitwidth.
784 if (RCR.contains(APInt::getAllOnes(OrigWidth)) &&
785 LCR.contains(APInt::getSignedMinValue(MinSignedBits).sext(OrigWidth)))
786 ++MinSignedBits;
787
788 // Don't shrink below 8 bits wide.
789 unsigned NewWidth = std::max<unsigned>(PowerOf2Ceil(MinSignedBits), 8);
790
791 // NewWidth might be greater than OrigWidth if OrigWidth is not a power of
792 // two.
793 if (NewWidth >= OrigWidth)
794 return false;
795
796 ++NumSDivSRemsNarrowed;
797 IRBuilder<> B{Instr};
798 auto *TruncTy = Instr->getType()->getWithNewBitWidth(NewWidth);
799 auto *LHS = B.CreateTruncOrBitCast(Instr->getOperand(0), TruncTy,
800 Instr->getName() + ".lhs.trunc");
801 auto *RHS = B.CreateTruncOrBitCast(Instr->getOperand(1), TruncTy,
802 Instr->getName() + ".rhs.trunc");
803 auto *BO = B.CreateBinOp(Instr->getOpcode(), LHS, RHS, Instr->getName());
804 auto *Sext = B.CreateSExt(BO, Instr->getType(), Instr->getName() + ".sext");
805 if (auto *BinOp = dyn_cast<BinaryOperator>(BO))
806 if (BinOp->getOpcode() == Instruction::SDiv)
807 BinOp->setIsExact(Instr->isExact());
808
809 Instr->replaceAllUsesWith(Sext);
810 Instr->eraseFromParent();
811 return true;
812}
813
814static bool expandUDivOrURem(BinaryOperator *Instr, const ConstantRange &XCR,
815 const ConstantRange &YCR) {
816 Type *Ty = Instr->getType();
817 assert(Instr->getOpcode() == Instruction::UDiv ||
818 Instr->getOpcode() == Instruction::URem);
819 bool IsRem = Instr->getOpcode() == Instruction::URem;
820
821 Value *X = Instr->getOperand(0);
822 Value *Y = Instr->getOperand(1);
823
824 // X u/ Y -> 0 iff X u< Y
825 // X u% Y -> X iff X u< Y
826 if (XCR.icmp(ICmpInst::ICMP_ULT, YCR)) {
827 Instr->replaceAllUsesWith(IsRem ? X : Constant::getNullValue(Ty));
828 Instr->eraseFromParent();
829 ++NumUDivURemsNarrowedExpanded;
830 return true;
831 }
832
833 // Given
834 // R = X u% Y
835 // We can represent the modulo operation as a loop/self-recursion:
836 // urem_rec(X, Y):
837 // Z = X - Y
838 // if X u< Y
839 // ret X
840 // else
841 // ret urem_rec(Z, Y)
842 // which isn't better, but if we only need a single iteration
843 // to compute the answer, this becomes quite good:
844 // R = X < Y ? X : X - Y iff X u< 2*Y (w/ unsigned saturation)
845 // Now, we do not care about all full multiples of Y in X, they do not change
846 // the answer, thus we could rewrite the expression as:
847 // X* = X - (Y * |_ X / Y _|)
848 // R = X* % Y
849 // so we don't need the *first* iteration to return, we just need to
850 // know *which* iteration will always return, so we could also rewrite it as:
851 // X* = X - (Y * |_ X / Y _|)
852 // R = X* % Y iff X* u< 2*Y (w/ unsigned saturation)
853 // but that does not seem profitable here.
854
855 // Even if we don't know X's range, the divisor may be so large, X can't ever
856 // be 2x larger than that. I.e. if divisor is always negative.
857 if (!XCR.icmp(ICmpInst::ICMP_ULT, YCR.uadd_sat(YCR)) && !YCR.isAllNegative())
858 return false;
859
860 IRBuilder<> B(Instr);
861 Value *ExpandedOp;
862 if (XCR.icmp(ICmpInst::ICMP_UGE, YCR)) {
863 // If X is between Y and 2*Y the result is known.
864 if (IsRem)
865 ExpandedOp = B.CreateNUWSub(X, Y);
866 else
867 ExpandedOp = ConstantInt::get(Instr->getType(), 1);
868 } else if (IsRem) {
869 // NOTE: this transformation introduces two uses of X,
870 // but it may be undef so we must freeze it first.
871 Value *FrozenX = X;
873 FrozenX = B.CreateFreeze(X, X->getName() + ".frozen");
874 Value *FrozenY = Y;
876 FrozenY = B.CreateFreeze(Y, Y->getName() + ".frozen");
877 auto *AdjX = B.CreateNUWSub(FrozenX, FrozenY, Instr->getName() + ".urem");
878 auto *Cmp = B.CreateICmp(ICmpInst::ICMP_ULT, FrozenX, FrozenY,
879 Instr->getName() + ".cmp");
880 ExpandedOp = B.CreateSelect(Cmp, FrozenX, AdjX);
881 } else {
882 auto *Cmp =
883 B.CreateICmp(ICmpInst::ICMP_UGE, X, Y, Instr->getName() + ".cmp");
884 ExpandedOp = B.CreateZExt(Cmp, Ty, Instr->getName() + ".udiv");
885 }
886 ExpandedOp->takeName(Instr);
887 Instr->replaceAllUsesWith(ExpandedOp);
888 Instr->eraseFromParent();
889 ++NumUDivURemsNarrowedExpanded;
890 return true;
891}
892
893/// Try to shrink a udiv/urem's width down to the smallest power of two that's
894/// sufficient to contain its operands.
895static bool narrowUDivOrURem(BinaryOperator *Instr, const ConstantRange &XCR,
896 const ConstantRange &YCR) {
897 assert(Instr->getOpcode() == Instruction::UDiv ||
898 Instr->getOpcode() == Instruction::URem);
899
900 // Find the smallest power of two bitwidth that's sufficient to hold Instr's
901 // operands.
902
903 // What is the smallest bit width that can accommodate the entire value ranges
904 // of both of the operands?
905 unsigned MaxActiveBits = std::max(XCR.getActiveBits(), YCR.getActiveBits());
906 // Don't shrink below 8 bits wide.
907 unsigned NewWidth = std::max<unsigned>(PowerOf2Ceil(MaxActiveBits), 8);
908
909 // NewWidth might be greater than OrigWidth if OrigWidth is not a power of
910 // two.
911 if (NewWidth >= Instr->getType()->getScalarSizeInBits())
912 return false;
913
914 ++NumUDivURemsNarrowed;
915 IRBuilder<> B{Instr};
916 auto *TruncTy = Instr->getType()->getWithNewBitWidth(NewWidth);
917 auto *LHS = B.CreateTruncOrBitCast(Instr->getOperand(0), TruncTy,
918 Instr->getName() + ".lhs.trunc");
919 auto *RHS = B.CreateTruncOrBitCast(Instr->getOperand(1), TruncTy,
920 Instr->getName() + ".rhs.trunc");
921 auto *BO = B.CreateBinOp(Instr->getOpcode(), LHS, RHS, Instr->getName());
922 auto *Zext = B.CreateZExt(BO, Instr->getType(), Instr->getName() + ".zext");
923 if (auto *BinOp = dyn_cast<BinaryOperator>(BO))
924 if (BinOp->getOpcode() == Instruction::UDiv)
925 BinOp->setIsExact(Instr->isExact());
926
927 Instr->replaceAllUsesWith(Zext);
928 Instr->eraseFromParent();
929 return true;
930}
931
933 assert(Instr->getOpcode() == Instruction::UDiv ||
934 Instr->getOpcode() == Instruction::URem);
935 ConstantRange XCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(0),
936 /*UndefAllowed*/ false);
937 // Allow undef for RHS, as we can assume it is division by zero UB.
938 ConstantRange YCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(1),
939 /*UndefAllowed*/ true);
940 if (expandUDivOrURem(Instr, XCR, YCR))
941 return true;
942
943 return narrowUDivOrURem(Instr, XCR, YCR);
944}
945
946static bool processSRem(BinaryOperator *SDI, const ConstantRange &LCR,
947 const ConstantRange &RCR, LazyValueInfo *LVI) {
948 assert(SDI->getOpcode() == Instruction::SRem);
949
950 if (LCR.abs().icmp(CmpInst::ICMP_ULT, RCR.abs())) {
951 SDI->replaceAllUsesWith(SDI->getOperand(0));
952 SDI->eraseFromParent();
953 return true;
954 }
955
956 struct Operand {
957 Value *V;
958 Domain D;
959 };
960 std::array<Operand, 2> Ops = {{{SDI->getOperand(0), getDomain(LCR)},
961 {SDI->getOperand(1), getDomain(RCR)}}};
962 if (Ops[0].D == Domain::Unknown || Ops[1].D == Domain::Unknown)
963 return false;
964
965 // We know domains of both of the operands!
966 ++NumSRems;
967
968 // We need operands to be non-negative, so negate each one that isn't.
969 for (Operand &Op : Ops) {
970 if (Op.D == Domain::NonNegative)
971 continue;
972 auto *BO = BinaryOperator::CreateNeg(Op.V, Op.V->getName() + ".nonneg",
973 SDI->getIterator());
974 BO->setDebugLoc(SDI->getDebugLoc());
975 Op.V = BO;
976 }
977
978 auto *URem = BinaryOperator::CreateURem(Ops[0].V, Ops[1].V, SDI->getName(),
979 SDI->getIterator());
980 URem->setDebugLoc(SDI->getDebugLoc());
981
982 auto *Res = URem;
983
984 // If the divident was non-positive, we need to negate the result.
985 if (Ops[0].D == Domain::NonPositive) {
986 Res = BinaryOperator::CreateNeg(Res, Res->getName() + ".neg",
987 SDI->getIterator());
988 Res->setDebugLoc(SDI->getDebugLoc());
989 }
990
991 SDI->replaceAllUsesWith(Res);
992 SDI->eraseFromParent();
993
994 // Try to simplify our new urem.
995 processUDivOrURem(URem, LVI);
996
997 return true;
998}
999
1000/// See if LazyValueInfo's ability to exploit edge conditions or range
1001/// information is sufficient to prove the signs of both operands of this SDiv.
1002/// If this is the case, replace the SDiv with a UDiv. Even for local
1003/// conditions, this can sometimes prove conditions instcombine can't by
1004/// exploiting range information.
1005static bool processSDiv(BinaryOperator *SDI, const ConstantRange &LCR,
1006 const ConstantRange &RCR, LazyValueInfo *LVI) {
1007 assert(SDI->getOpcode() == Instruction::SDiv);
1008
1009 // Check whether the division folds to a constant.
1010 ConstantRange DivCR = LCR.sdiv(RCR);
1011 if (const APInt *Elem = DivCR.getSingleElement()) {
1012 SDI->replaceAllUsesWith(ConstantInt::get(SDI->getType(), *Elem));
1013 SDI->eraseFromParent();
1014 return true;
1015 }
1016
1017 struct Operand {
1018 Value *V;
1019 Domain D;
1020 };
1021 std::array<Operand, 2> Ops = {{{SDI->getOperand(0), getDomain(LCR)},
1022 {SDI->getOperand(1), getDomain(RCR)}}};
1023 if (Ops[0].D == Domain::Unknown || Ops[1].D == Domain::Unknown)
1024 return false;
1025
1026 // We know domains of both of the operands!
1027 ++NumSDivs;
1028
1029 // We need operands to be non-negative, so negate each one that isn't.
1030 for (Operand &Op : Ops) {
1031 if (Op.D == Domain::NonNegative)
1032 continue;
1033 auto *BO = BinaryOperator::CreateNeg(Op.V, Op.V->getName() + ".nonneg",
1034 SDI->getIterator());
1035 BO->setDebugLoc(SDI->getDebugLoc());
1036 Op.V = BO;
1037 }
1038
1039 auto *UDiv = BinaryOperator::CreateUDiv(Ops[0].V, Ops[1].V, SDI->getName(),
1040 SDI->getIterator());
1041 UDiv->setDebugLoc(SDI->getDebugLoc());
1042 UDiv->setIsExact(SDI->isExact());
1043
1044 auto *Res = UDiv;
1045
1046 // If the operands had two different domains, we need to negate the result.
1047 if (Ops[0].D != Ops[1].D) {
1048 Res = BinaryOperator::CreateNeg(Res, Res->getName() + ".neg",
1049 SDI->getIterator());
1050 Res->setDebugLoc(SDI->getDebugLoc());
1051 }
1052
1053 SDI->replaceAllUsesWith(Res);
1054 SDI->eraseFromParent();
1055
1056 // Try to simplify our new udiv.
1057 processUDivOrURem(UDiv, LVI);
1058
1059 return true;
1060}
1061
1063 assert(Instr->getOpcode() == Instruction::SDiv ||
1064 Instr->getOpcode() == Instruction::SRem);
1065 ConstantRange LCR =
1066 LVI->getConstantRangeAtUse(Instr->getOperandUse(0), /*AllowUndef*/ false);
1067 // Allow undef for RHS, as we can assume it is division by zero UB.
1068 ConstantRange RCR =
1069 LVI->getConstantRangeAtUse(Instr->getOperandUse(1), /*AlloweUndef*/ true);
1070 if (Instr->getOpcode() == Instruction::SDiv)
1071 if (processSDiv(Instr, LCR, RCR, LVI))
1072 return true;
1073
1074 if (Instr->getOpcode() == Instruction::SRem) {
1075 if (processSRem(Instr, LCR, RCR, LVI))
1076 return true;
1077 }
1078
1079 return narrowSDivOrSRem(Instr, LCR, RCR);
1080}
1081
1083 ConstantRange LRange =
1084 LVI->getConstantRangeAtUse(SDI->getOperandUse(0), /*UndefAllowed*/ false);
1085 unsigned OrigWidth = SDI->getType()->getScalarSizeInBits();
1086 ConstantRange NegOneOrZero =
1087 ConstantRange(APInt(OrigWidth, (uint64_t)-1, true), APInt(OrigWidth, 1));
1088 if (NegOneOrZero.contains(LRange)) {
1089 // ashr of -1 or 0 never changes the value, so drop the whole instruction
1090 ++NumAShrsRemoved;
1091 SDI->replaceAllUsesWith(SDI->getOperand(0));
1092 SDI->eraseFromParent();
1093 return true;
1094 }
1095
1096 if (!LRange.isAllNonNegative())
1097 return false;
1098
1099 ++NumAShrsConverted;
1100 auto *BO = BinaryOperator::CreateLShr(SDI->getOperand(0), SDI->getOperand(1),
1101 "", SDI->getIterator());
1102 BO->takeName(SDI);
1103 BO->setDebugLoc(SDI->getDebugLoc());
1104 BO->setIsExact(SDI->isExact());
1105 SDI->replaceAllUsesWith(BO);
1106 SDI->eraseFromParent();
1107
1108 return true;
1109}
1110
1111static bool processSExt(SExtInst *SDI, LazyValueInfo *LVI) {
1112 const Use &Base = SDI->getOperandUse(0);
1113 if (!LVI->getConstantRangeAtUse(Base, /*UndefAllowed*/ false)
1115 return false;
1116
1117 ++NumSExt;
1118 auto *ZExt = CastInst::CreateZExtOrBitCast(Base, SDI->getType(), "",
1119 SDI->getIterator());
1120 ZExt->takeName(SDI);
1121 ZExt->setDebugLoc(SDI->getDebugLoc());
1122 ZExt->setNonNeg();
1123 SDI->replaceAllUsesWith(ZExt);
1124 SDI->eraseFromParent();
1125
1126 return true;
1127}
1128
1130 if (I->hasNonNeg())
1131 return false;
1132
1133 const Use &Base = I->getOperandUse(0);
1134 if (!LVI->getConstantRangeAtUse(Base, /*UndefAllowed*/ false)
1136 return false;
1137
1138 ++NumNNeg;
1139 I->setNonNeg();
1140
1141 return true;
1142}
1143
1144static bool processZExt(ZExtInst *ZExt, LazyValueInfo *LVI) {
1145 return processPossibleNonNeg(cast<PossiblyNonNegInst>(ZExt), LVI);
1146}
1147
1148static bool processUIToFP(UIToFPInst *UIToFP, LazyValueInfo *LVI) {
1149 return processPossibleNonNeg(cast<PossiblyNonNegInst>(UIToFP), LVI);
1150}
1151
1152static bool processSIToFP(SIToFPInst *SIToFP, LazyValueInfo *LVI) {
1153 const Use &Base = SIToFP->getOperandUse(0);
1154 if (!LVI->getConstantRangeAtUse(Base, /*UndefAllowed*/ false)
1156 return false;
1157
1158 ++NumSIToFP;
1159 auto *UIToFP = CastInst::Create(Instruction::UIToFP, Base, SIToFP->getType(),
1160 "", SIToFP->getIterator());
1161 UIToFP->takeName(SIToFP);
1162 UIToFP->setDebugLoc(SIToFP->getDebugLoc());
1163 UIToFP->setNonNeg();
1164 SIToFP->replaceAllUsesWith(UIToFP);
1165 SIToFP->eraseFromParent();
1166
1167 return true;
1168}
1169
1170static bool processBinOp(BinaryOperator *BinOp, LazyValueInfo *LVI) {
1171 using OBO = OverflowingBinaryOperator;
1172
1173 bool NSW = BinOp->hasNoSignedWrap();
1174 bool NUW = BinOp->hasNoUnsignedWrap();
1175 if (NSW && NUW)
1176 return false;
1177
1178 Instruction::BinaryOps Opcode = BinOp->getOpcode();
1179 ConstantRange LRange = LVI->getConstantRangeAtUse(BinOp->getOperandUse(0),
1180 /*UndefAllowed=*/false);
1181 ConstantRange RRange = LVI->getConstantRangeAtUse(BinOp->getOperandUse(1),
1182 /*UndefAllowed=*/false);
1183
1184 bool Changed = false;
1185 bool NewNUW = false, NewNSW = false;
1186 if (!NUW) {
1188 Opcode, RRange, OBO::NoUnsignedWrap);
1189 NewNUW = NUWRange.contains(LRange);
1190 Changed |= NewNUW;
1191 }
1192 if (!NSW) {
1194 Opcode, RRange, OBO::NoSignedWrap);
1195 NewNSW = NSWRange.contains(LRange);
1196 Changed |= NewNSW;
1197 }
1198
1199 setDeducedOverflowingFlags(BinOp, Opcode, NewNSW, NewNUW);
1200
1201 return Changed;
1202}
1203
1204static bool processAnd(BinaryOperator *BinOp, LazyValueInfo *LVI) {
1205 using namespace llvm::PatternMatch;
1206
1207 // Pattern match (and lhs, C) where C includes a superset of bits which might
1208 // be set in lhs. This is a common truncation idiom created by instcombine.
1209 const Use &LHS = BinOp->getOperandUse(0);
1210 const APInt *RHS;
1211 if (!match(BinOp->getOperand(1), m_LowBitMask(RHS)))
1212 return false;
1213
1214 // We can only replace the AND with LHS based on range info if the range does
1215 // not include undef.
1216 ConstantRange LRange =
1217 LVI->getConstantRangeAtUse(LHS, /*UndefAllowed=*/false);
1218 if (!LRange.getUnsignedMax().ule(*RHS))
1219 return false;
1220
1221 BinOp->replaceAllUsesWith(LHS);
1222 BinOp->eraseFromParent();
1223 NumAnd++;
1224 return true;
1225}
1226
1227static bool processTrunc(TruncInst *TI, LazyValueInfo *LVI) {
1228 if (TI->hasNoSignedWrap() && TI->hasNoUnsignedWrap())
1229 return false;
1230
1232 LVI->getConstantRangeAtUse(TI->getOperandUse(0), /*UndefAllowed=*/false);
1233 uint64_t DestWidth = TI->getDestTy()->getScalarSizeInBits();
1234 bool Changed = false;
1235
1236 if (!TI->hasNoUnsignedWrap()) {
1237 if (Range.getActiveBits() <= DestWidth) {
1238 TI->setHasNoUnsignedWrap(true);
1239 ++NumNUW;
1240 Changed = true;
1241 }
1242 }
1243
1244 if (!TI->hasNoSignedWrap()) {
1245 if (Range.getMinSignedBits() <= DestWidth) {
1246 TI->setHasNoSignedWrap(true);
1247 ++NumNSW;
1248 Changed = true;
1249 }
1250 }
1251
1252 return Changed;
1253}
1254
1256 const SimplifyQuery &SQ) {
1257 bool FnChanged = false;
1258 std::optional<ConstantRange> RetRange;
1259 if (F.hasExactDefinition() && F.getReturnType()->isIntOrIntVectorTy())
1260 RetRange =
1261 ConstantRange::getEmpty(F.getReturnType()->getScalarSizeInBits());
1262
1263 // Visiting in a pre-order depth-first traversal causes us to simplify early
1264 // blocks before querying later blocks (which require us to analyze early
1265 // blocks). Eagerly simplifying shallow blocks means there is strictly less
1266 // work to do for deep blocks. This also means we don't visit unreachable
1267 // blocks.
1268 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
1269 bool BBChanged = false;
1271 switch (II.getOpcode()) {
1272 case Instruction::Select:
1273 BBChanged |= processSelect(cast<SelectInst>(&II), LVI);
1274 break;
1275 case Instruction::PHI:
1276 BBChanged |= processPHI(cast<PHINode>(&II), LVI, DT, SQ);
1277 break;
1278 case Instruction::ICmp:
1279 case Instruction::FCmp:
1280 BBChanged |= processCmp(cast<CmpInst>(&II), LVI);
1281 break;
1282 case Instruction::Call:
1283 case Instruction::Invoke:
1284 BBChanged |= processCallSite(cast<CallBase>(II), LVI);
1285 break;
1286 case Instruction::SRem:
1287 case Instruction::SDiv:
1288 BBChanged |= processSDivOrSRem(cast<BinaryOperator>(&II), LVI);
1289 break;
1290 case Instruction::UDiv:
1291 case Instruction::URem:
1292 BBChanged |= processUDivOrURem(cast<BinaryOperator>(&II), LVI);
1293 break;
1294 case Instruction::AShr:
1295 BBChanged |= processAShr(cast<BinaryOperator>(&II), LVI);
1296 break;
1297 case Instruction::SExt:
1298 BBChanged |= processSExt(cast<SExtInst>(&II), LVI);
1299 break;
1300 case Instruction::ZExt:
1301 BBChanged |= processZExt(cast<ZExtInst>(&II), LVI);
1302 break;
1303 case Instruction::UIToFP:
1304 BBChanged |= processUIToFP(cast<UIToFPInst>(&II), LVI);
1305 break;
1306 case Instruction::SIToFP:
1307 BBChanged |= processSIToFP(cast<SIToFPInst>(&II), LVI);
1308 break;
1309 case Instruction::Add:
1310 case Instruction::Sub:
1311 case Instruction::Mul:
1312 case Instruction::Shl:
1313 BBChanged |= processBinOp(cast<BinaryOperator>(&II), LVI);
1314 break;
1315 case Instruction::And:
1316 BBChanged |= processAnd(cast<BinaryOperator>(&II), LVI);
1317 break;
1318 case Instruction::Trunc:
1319 BBChanged |= processTrunc(cast<TruncInst>(&II), LVI);
1320 break;
1321 }
1322 }
1323
1324 Instruction *Term = BB->getTerminator();
1325 switch (Term->getOpcode()) {
1326 case Instruction::Switch:
1327 BBChanged |= processSwitch(cast<SwitchInst>(Term), LVI, DT);
1328 break;
1329 case Instruction::Ret: {
1330 auto *RI = cast<ReturnInst>(Term);
1331 // Try to determine the return value if we can. This is mainly here to
1332 // simplify the writing of unit tests, but also helps to enable IPO by
1333 // constant folding the return values of callees.
1334 auto *RetVal = RI->getReturnValue();
1335 if (!RetVal) break; // handle "ret void"
1336 if (RetRange && !RetRange->isFullSet())
1337 RetRange =
1338 RetRange->unionWith(LVI->getConstantRange(RetVal, RI,
1339 /*UndefAllowed=*/false));
1340
1341 if (isa<Constant>(RetVal)) break; // nothing to do
1342 if (auto *C = getConstantAt(RetVal, RI, LVI)) {
1343 ++NumReturns;
1344 RI->replaceUsesOfWith(RetVal, C);
1345 BBChanged = true;
1346 }
1347 }
1348 }
1349
1350 FnChanged |= BBChanged;
1351 }
1352
1353 // Infer range attribute on return value.
1354 if (RetRange && !RetRange->isFullSet()) {
1355 Attribute RangeAttr = F.getRetAttribute(Attribute::Range);
1356 if (RangeAttr.isValid())
1357 RetRange = RetRange->intersectWith(RangeAttr.getRange());
1358 // Don't add attribute for constant integer returns to reduce noise. These
1359 // are propagated across functions by IPSCCP.
1360 if (!RetRange->isEmptySet() && !RetRange->isSingleElement()) {
1361 F.addRangeRetAttr(*RetRange);
1362 FnChanged = true;
1363 }
1364 }
1365 return FnChanged;
1366}
1367
1372
1373 bool Changed = runImpl(F, LVI, DT, getBestSimplifyQuery(AM, F));
1374
1376 if (!Changed) {
1378 } else {
1379#if defined(EXPENSIVE_CHECKS)
1380 assert(DT->verify(DominatorTree::VerificationLevel::Full));
1381#else
1382 assert(DT->verify(DominatorTree::VerificationLevel::Fast));
1383#endif // EXPENSIVE_CHECKS
1384
1387 }
1388
1389 // Keeping LVI alive is expensive, both because it uses a lot of memory, and
1390 // because invalidating values in LVI is expensive. While CVP does preserve
1391 // LVI, we know that passes after JumpThreading+CVP will not need the result
1392 // of this analysis, so we forcefully discard it early.
1394 return PA;
1395}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the simple types necessary to represent the attributes associated with functions a...
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool processICmp(ICmpInst *Cmp, LazyValueInfo *LVI)
static bool processAnd(BinaryOperator *BinOp, LazyValueInfo *LVI)
static bool processOverflowIntrinsic(WithOverflowInst *WO, LazyValueInfo *LVI)
static bool processSDivOrSRem(BinaryOperator *Instr, LazyValueInfo *LVI)
static bool expandUDivOrURem(BinaryOperator *Instr, const ConstantRange &XCR, const ConstantRange &YCR)
static bool constantFoldCmp(CmpInst *Cmp, LazyValueInfo *LVI)
See if LazyValueInfo's ability to exploit edge conditions or range information is sufficient to prove...
static bool processSaturatingInst(SaturatingInst *SI, LazyValueInfo *LVI)
static Value * getValueOnEdge(LazyValueInfo *LVI, Value *Incoming, BasicBlock *From, BasicBlock *To, Instruction *CxtI)
static bool narrowUDivOrURem(BinaryOperator *Instr, const ConstantRange &XCR, const ConstantRange &YCR)
Try to shrink a udiv/urem's width down to the smallest power of two that's sufficient to contain its ...
static bool willNotOverflow(BinaryOpIntrinsic *BO, LazyValueInfo *LVI)
static bool processSelect(SelectInst *S, LazyValueInfo *LVI)
static bool runImpl(Function &F, LazyValueInfo *LVI, DominatorTree *DT, const SimplifyQuery &SQ)
static void setDeducedOverflowingFlags(Value *V, Instruction::BinaryOps Opcode, bool NewNSW, bool NewNUW)
static bool processMinMaxIntrinsic(MinMaxIntrinsic *MM, LazyValueInfo *LVI)
static bool processBinOp(BinaryOperator *BinOp, LazyValueInfo *LVI)
static bool simplifyCommonValuePhi(PHINode *P, LazyValueInfo *LVI, DominatorTree *DT)
Try to simplify a phi with constant incoming values that match the edge values of a non-constant valu...
static bool processSRem(BinaryOperator *SDI, const ConstantRange &LCR, const ConstantRange &RCR, LazyValueInfo *LVI)
static Domain getDomain(const ConstantRange &CR)
static bool processPHI(PHINode *P, LazyValueInfo *LVI, DominatorTree *DT, const SimplifyQuery &SQ)
static bool processTrunc(TruncInst *TI, LazyValueInfo *LVI)
static bool processUDivOrURem(BinaryOperator *Instr, LazyValueInfo *LVI)
static bool processCmpIntrinsic(CmpIntrinsic *CI, LazyValueInfo *LVI)
@ NonNegative
@ NonPositive
static bool processSDiv(BinaryOperator *SDI, const ConstantRange &LCR, const ConstantRange &RCR, LazyValueInfo *LVI)
See if LazyValueInfo's ability to exploit edge conditions or range information is sufficient to prove...
static bool processAShr(BinaryOperator *SDI, LazyValueInfo *LVI)
static bool processZExt(ZExtInst *ZExt, LazyValueInfo *LVI)
static bool narrowSDivOrSRem(BinaryOperator *Instr, const ConstantRange &LCR, const ConstantRange &RCR)
Try to shrink a sdiv/srem's width down to the smallest power of two that's sufficient to contain its ...
static bool processSIToFP(SIToFPInst *SIToFP, LazyValueInfo *LVI)
static bool processSExt(SExtInst *SDI, LazyValueInfo *LVI)
static bool processPossibleNonNeg(PossiblyNonNegInst *I, LazyValueInfo *LVI)
static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI, DominatorTree *DT)
Simplify a switch instruction by removing cases which can never fire.
static bool processUIToFP(UIToFPInst *UIToFP, LazyValueInfo *LVI)
static Constant * getConstantAt(Value *V, Instruction *At, LazyValueInfo *LVI)
static bool processCmp(CmpInst *Cmp, LazyValueInfo *LVI)
static bool processAbsIntrinsic(IntrinsicInst *II, LazyValueInfo *LVI)
static bool processCallSite(CallBase &CB, LazyValueInfo *LVI)
Infer nonnull attributes for the arguments at the specified callsite.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static bool runImpl(Function &F, const TargetLowering &TLI)
Definition: ExpandFp.cpp:597
This is the interface for a simple mod/ref and alias analysis over globals.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
@ Struct
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition: APInt.h:78
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:234
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:219
bool isNonPositive() const
Determine if this APInt Value is non-positive (<= 0).
Definition: APInt.h:361
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1150
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:985
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:200
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:412
AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const
Add an argument attribute to the list.
Definition: Attributes.h:644
LLVM_ABI const ConstantRange & getRange() const
Returns the value of the range attribute.
Definition: Attributes.cpp:510
static LLVM_ABI Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Definition: Attributes.cpp:95
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:223
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:206
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:213
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:131
LLVM_ABI void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
Definition: BasicBlock.cpp:494
This class represents an intrinsic that is based on a binary operation.
Value * getRHS() const
LLVM_ABI unsigned getNoWrapKind() const
Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
LLVM_ABI bool isSigned() const
Whether the intrinsic is signed or unsigned.
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
Value * getLHS() const
static LLVM_ABI BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Helper functions to construct and inspect unary operations (NEG and NOT) via binary operators SUB and...
BinaryOps getOpcode() const
Definition: InstrTypes.h:374
static LLVM_ABI BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1116
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Definition: InstrTypes.h:2083
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
void setAttributes(AttributeList A)
Set the attributes for this call.
Definition: InstrTypes.h:1427
LLVM_ABI Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
Definition: InstrTypes.h:1283
unsigned arg_size() const
Definition: InstrTypes.h:1290
AttributeList getAttributes() const
Return the attributes for this call.
Definition: InstrTypes.h:1424
static LLVM_ABI CastInst * CreateZExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a ZExt or BitCast cast instruction.
static LLVM_ABI CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
Type * getDestTy() const
Return the destination type, as a convenience.
Definition: InstrTypes.h:619
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:666
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:678
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:703
@ ICMP_EQ
equal
Definition: InstrTypes.h:699
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:704
Predicate getNonStrictPredicate() const
For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
Definition: InstrTypes.h:873
This class represents a ucmp/scmp intrinsic.
static CmpInst::Predicate getGTPredicate(Intrinsic::ID ID)
static CmpInst::Predicate getLTPredicate(Intrinsic::ID ID)
This is the shared class of boolean and integer constants.
Definition: Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:868
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Definition: Constants.h:131
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:875
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:154
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1833
This class represents a range of values.
Definition: ConstantRange.h:47
LLVM_ABI unsigned getActiveBits() const
Compute the maximal number of active bits needed to represent every value in this range.
static LLVM_ABI CmpInst::Predicate getEquivalentPredWithFlippedSignedness(CmpInst::Predicate Pred, const ConstantRange &CR1, const ConstantRange &CR2)
If the comparison between constant ranges this and Other is insensitive to the signedness of the comp...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
LLVM_ABI bool isAllNegative() const
Return true if all values in this range are negative.
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other? NOTE: false does not mean that inverse pr...
LLVM_ABI bool isSizeLargerThan(uint64_t MaxSize) const
Compare set size of this range with Value.
LLVM_ABI ConstantRange abs(bool IntMinIsPoison=false) const
Calculate absolute value range.
LLVM_ABI ConstantRange uadd_sat(const ConstantRange &Other) const
Perform an unsigned saturating addition of two constant ranges.
bool isSingleElement() const
Return true if this set contains exactly one member.
LLVM_ABI bool isAllNonNegative() const
Return true if all values in this range are non-negative.
LLVM_ABI ConstantRange sdiv(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a signed division of a value in th...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
static LLVM_ABI bool areInsensitiveToSignednessOfICmpPredicate(const ConstantRange &CR1, const ConstantRange &CR2)
Return true iff CR1 ult CR2 is equivalent to CR1 slt CR2.
static LLVM_ABI ConstantRange makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp, const ConstantRange &Other, unsigned NoWrapKind)
Produce the largest range containing all X such that "X BinOp Y" is guaranteed not to wrap (overflow)...
LLVM_ABI unsigned getMinSignedBits() const
Compute the maximal number of bits needed to represent every value in this signed range.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1380
This is an important base class in LLVM.
Definition: Constant.h:43
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
This class represents an Operation in the Expression.
static DebugLoc getTemporary()
Definition: DebugLoc.h:161
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:284
bool verify(VerificationLevel VL=VerificationLevel::Full) const
verify - checks if the tree is correct.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:165
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:135
void applyUpdatesPermissive(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
This instruction compares its operands according to the predicate given to the constructor.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2780
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:513
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:510
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:49
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:56
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
Analysis to compute lazy value information.
This pass computes, caches, and vends lazy value constraint information.
Definition: LazyValueInfo.h:32
ConstantRange getConstantRangeAtUse(const Use &U, bool UndefAllowed)
Return the ConstantRange constraint that is known to hold for the value at a specific use-site.
ConstantRange getConstantRange(Value *V, Instruction *CxtI, bool UndefAllowed)
Return the ConstantRange constraint that is known to hold for the specified value at the specified in...
Constant * getPredicateOnEdge(CmpInst::Predicate Pred, Value *V, Constant *C, BasicBlock *FromBB, BasicBlock *ToBB, Instruction *CxtI=nullptr)
Determine whether the specified value comparison with a constant is known to be true or false on the ...
Constant * getConstantOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB, Instruction *CxtI=nullptr)
Determine whether the specified value is known to be a constant on the specified edge.
Constant * getConstant(Value *V, Instruction *CxtI)
Determine whether the specified value is known to be a constant at the specified instruction.
Constant * getPredicateAt(CmpInst::Predicate Pred, Value *V, Constant *C, Instruction *CxtI, bool UseBlockValue)
Determine whether the specified value comparison with a constant is known to be true or false at the ...
This class represents min/max intrinsics.
Value * getLHS() const
Value * getRHS() const
static ICmpInst::Predicate getPredicate(Intrinsic::ID ID)
Returns the comparison predicate underlying the intrinsic.
static bool isSigned(Intrinsic::ID ID)
Whether the intrinsic is signed or unsigned.
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition: Operator.h:78
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1885
Instruction that can have a nneg flag (zext/uitofp).
Definition: InstrTypes.h:641
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:118
PreservedAnalyses & abandon()
Mark an analysis as abandoned.
Definition: Analysis.h:171
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition: Analysis.h:132
This class represents a sign extension of integer types.
This class represents a cast from signed integer to floating point.
Represents a saturating add/sub intrinsic.
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
Class to represent struct types.
Definition: DerivedTypes.h:218
A wrapper class to simplify modification of SwitchInst cases along with their prof branch_weights met...
Multiway switch.
This class represents a truncation of integer types.
void setHasNoSignedWrap(bool B)
void setHasNoUnsignedWrap(bool B)
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property.
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:273
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
This class represents a cast unsigned integer to floating point.
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
const Use & getOperandUse(unsigned i) const
Definition: User.h:245
Value * getOperand(unsigned i) const
Definition: User.h:232
LLVM Value Representation.
Definition: Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:546
bool use_empty() const
Definition: Value.h:346
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1098
iterator_range< use_iterator > uses()
Definition: Value.h:380
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:322
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:396
Represents an op.with.overflow intrinsic.
This class represents zero extension of integer types.
self_iterator getIterator()
Definition: ilist_node.h:134
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:673
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr, DomTreeUpdater *DTU=nullptr)
If a terminator instruction is predicated on a constant value, convert it into an unconditional branc...
Definition: Local.cpp:134
auto successors(const MachineBasicBlock *BB)
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:663
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition: MathExtras.h:390
LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
iterator_range< df_iterator< T > > depth_first(const T &G)
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI const SimplifyQuery getBestSimplifyQuery(Pass &, Function &)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...