LLVM 17.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"
37#include "llvm/IR/Type.h"
38#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
51 "canonicalize-icmp-predicates-to-unsigned", cl::init(true), cl::Hidden,
52 cl::desc("Enables canonicalization of signed relational predicates to "
53 "unsigned (e.g. sgt => ugt)"));
54
55STATISTIC(NumPhis, "Number of phis propagated");
56STATISTIC(NumPhiCommon, "Number of phis deleted via common incoming value");
57STATISTIC(NumSelects, "Number of selects propagated");
58STATISTIC(NumMemAccess, "Number of memory access targets propagated");
59STATISTIC(NumCmps, "Number of comparisons propagated");
60STATISTIC(NumReturns, "Number of return values propagated");
61STATISTIC(NumDeadCases, "Number of switch cases removed");
62STATISTIC(NumSDivSRemsNarrowed,
63 "Number of sdivs/srems whose width was decreased");
64STATISTIC(NumSDivs, "Number of sdiv converted to udiv");
65STATISTIC(NumUDivURemsNarrowed,
66 "Number of udivs/urems whose width was decreased");
67STATISTIC(NumAShrsConverted, "Number of ashr converted to lshr");
68STATISTIC(NumAShrsRemoved, "Number of ashr removed");
69STATISTIC(NumSRems, "Number of srem converted to urem");
70STATISTIC(NumSExt, "Number of sext converted to zext");
71STATISTIC(NumSICmps, "Number of signed icmp preds simplified to unsigned");
72STATISTIC(NumAnd, "Number of ands removed");
73STATISTIC(NumNW, "Number of no-wrap deductions");
74STATISTIC(NumNSW, "Number of no-signed-wrap deductions");
75STATISTIC(NumNUW, "Number of no-unsigned-wrap deductions");
76STATISTIC(NumAddNW, "Number of no-wrap deductions for add");
77STATISTIC(NumAddNSW, "Number of no-signed-wrap deductions for add");
78STATISTIC(NumAddNUW, "Number of no-unsigned-wrap deductions for add");
79STATISTIC(NumSubNW, "Number of no-wrap deductions for sub");
80STATISTIC(NumSubNSW, "Number of no-signed-wrap deductions for sub");
81STATISTIC(NumSubNUW, "Number of no-unsigned-wrap deductions for sub");
82STATISTIC(NumMulNW, "Number of no-wrap deductions for mul");
83STATISTIC(NumMulNSW, "Number of no-signed-wrap deductions for mul");
84STATISTIC(NumMulNUW, "Number of no-unsigned-wrap deductions for mul");
85STATISTIC(NumShlNW, "Number of no-wrap deductions for shl");
86STATISTIC(NumShlNSW, "Number of no-signed-wrap deductions for shl");
87STATISTIC(NumShlNUW, "Number of no-unsigned-wrap deductions for shl");
88STATISTIC(NumAbs, "Number of llvm.abs intrinsics removed");
89STATISTIC(NumOverflows, "Number of overflow checks removed");
90STATISTIC(NumSaturating,
91 "Number of saturating arithmetics converted to normal arithmetics");
92STATISTIC(NumNonNull, "Number of function pointer arguments marked non-null");
93STATISTIC(NumMinMax, "Number of llvm.[us]{min,max} intrinsics removed");
94STATISTIC(NumUDivURemsNarrowedExpanded,
95 "Number of bound udiv's/urem's expanded");
96
97static bool processSelect(SelectInst *S, LazyValueInfo *LVI) {
98 if (S->getType()->isVectorTy()) return false;
99 if (isa<Constant>(S->getCondition())) return false;
100
101 Constant *C = LVI->getConstant(S->getCondition(), S);
102 if (!C) return false;
103
104 ConstantInt *CI = dyn_cast<ConstantInt>(C);
105 if (!CI) return false;
106
107 Value *ReplaceWith = CI->isOne() ? S->getTrueValue() : S->getFalseValue();
108 S->replaceAllUsesWith(ReplaceWith);
109 S->eraseFromParent();
110
111 ++NumSelects;
112
113 return true;
114}
115
116/// Try to simplify a phi with constant incoming values that match the edge
117/// values of a non-constant value on all other edges:
118/// bb0:
119/// %isnull = icmp eq i8* %x, null
120/// br i1 %isnull, label %bb2, label %bb1
121/// bb1:
122/// br label %bb2
123/// bb2:
124/// %r = phi i8* [ %x, %bb1 ], [ null, %bb0 ]
125/// -->
126/// %r = %x
128 DominatorTree *DT) {
129 // Collect incoming constants and initialize possible common value.
131 Value *CommonValue = nullptr;
132 for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) {
133 Value *Incoming = P->getIncomingValue(i);
134 if (auto *IncomingConstant = dyn_cast<Constant>(Incoming)) {
135 IncomingConstants.push_back(std::make_pair(IncomingConstant, i));
136 } else if (!CommonValue) {
137 // The potential common value is initialized to the first non-constant.
138 CommonValue = Incoming;
139 } else if (Incoming != CommonValue) {
140 // There can be only one non-constant common value.
141 return false;
142 }
143 }
144
145 if (!CommonValue || IncomingConstants.empty())
146 return false;
147
148 // The common value must be valid in all incoming blocks.
149 BasicBlock *ToBB = P->getParent();
150 if (auto *CommonInst = dyn_cast<Instruction>(CommonValue))
151 if (!DT->dominates(CommonInst, ToBB))
152 return false;
153
154 // We have a phi with exactly 1 variable incoming value and 1 or more constant
155 // incoming values. See if all constant incoming values can be mapped back to
156 // the same incoming variable value.
157 for (auto &IncomingConstant : IncomingConstants) {
158 Constant *C = IncomingConstant.first;
159 BasicBlock *IncomingBB = P->getIncomingBlock(IncomingConstant.second);
160 if (C != LVI->getConstantOnEdge(CommonValue, IncomingBB, ToBB, P))
161 return false;
162 }
163
164 // LVI only guarantees that the value matches a certain constant if the value
165 // is not poison. Make sure we don't replace a well-defined value with poison.
166 // This is usually satisfied due to a prior branch on the value.
167 if (!isGuaranteedNotToBePoison(CommonValue, nullptr, P, DT))
168 return false;
169
170 // All constant incoming values map to the same variable along the incoming
171 // edges of the phi. The phi is unnecessary.
172 P->replaceAllUsesWith(CommonValue);
173 P->eraseFromParent();
174 ++NumPhiCommon;
175 return true;
176}
177
178static Value *getValueOnEdge(LazyValueInfo *LVI, Value *Incoming,
180 Instruction *CxtI) {
181 if (Constant *C = LVI->getConstantOnEdge(Incoming, From, To, CxtI))
182 return C;
183
184 // Look if the incoming value is a select with a scalar condition for which
185 // LVI can tells us the value. In that case replace the incoming value with
186 // the appropriate value of the select. This often allows us to remove the
187 // select later.
188 auto *SI = dyn_cast<SelectInst>(Incoming);
189 if (!SI)
190 return nullptr;
191
192 // Once LVI learns to handle vector types, we could also add support
193 // for vector type constants that are not all zeroes or all ones.
194 Value *Condition = SI->getCondition();
195 if (!Condition->getType()->isVectorTy()) {
196 if (Constant *C = LVI->getConstantOnEdge(Condition, From, To, CxtI)) {
197 if (C->isOneValue())
198 return SI->getTrueValue();
199 if (C->isZeroValue())
200 return SI->getFalseValue();
201 }
202 }
203
204 // Look if the select has a constant but LVI tells us that the incoming
205 // value can never be that constant. In that case replace the incoming
206 // value with the other value of the select. This often allows us to
207 // remove the select later.
208
209 // The "false" case
210 if (auto *C = dyn_cast<Constant>(SI->getFalseValue()))
211 if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C, From, To, CxtI) ==
213 return SI->getTrueValue();
214
215 // The "true" case,
216 // similar to the select "false" case, but try the select "true" value
217 if (auto *C = dyn_cast<Constant>(SI->getTrueValue()))
218 if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C, From, To, CxtI) ==
220 return SI->getFalseValue();
221
222 return nullptr;
223}
224
226 const SimplifyQuery &SQ) {
227 bool Changed = false;
228
229 BasicBlock *BB = P->getParent();
230 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
231 Value *Incoming = P->getIncomingValue(i);
232 if (isa<Constant>(Incoming)) continue;
233
234 Value *V = getValueOnEdge(LVI, Incoming, P->getIncomingBlock(i), BB, P);
235 if (V) {
236 P->setIncomingValue(i, V);
237 Changed = true;
238 }
239 }
240
241 if (Value *V = simplifyInstruction(P, SQ)) {
242 P->replaceAllUsesWith(V);
243 P->eraseFromParent();
244 Changed = true;
245 }
246
247 if (!Changed)
248 Changed = simplifyCommonValuePhi(P, LVI, DT);
249
250 if (Changed)
251 ++NumPhis;
252
253 return Changed;
254}
255
257 Value *Pointer = nullptr;
258 if (LoadInst *L = dyn_cast<LoadInst>(I))
259 Pointer = L->getPointerOperand();
260 else
261 Pointer = cast<StoreInst>(I)->getPointerOperand();
262
263 if (isa<Constant>(Pointer)) return false;
264
265 Constant *C = LVI->getConstant(Pointer, I);
266 if (!C) return false;
267
268 ++NumMemAccess;
269 I->replaceUsesOfWith(Pointer, C);
270 return true;
271}
272
273static bool processICmp(ICmpInst *Cmp, LazyValueInfo *LVI) {
275 return false;
276
277 // Only for signed relational comparisons of scalar integers.
278 if (Cmp->getType()->isVectorTy() ||
279 !Cmp->getOperand(0)->getType()->isIntegerTy())
280 return false;
281
282 if (!Cmp->isSigned())
283 return false;
284
285 ICmpInst::Predicate UnsignedPred =
287 Cmp->getPredicate(), LVI->getConstantRange(Cmp->getOperand(0), Cmp),
288 LVI->getConstantRange(Cmp->getOperand(1), Cmp));
289
290 if (UnsignedPred == ICmpInst::Predicate::BAD_ICMP_PREDICATE)
291 return false;
292
293 ++NumSICmps;
294 Cmp->setPredicate(UnsignedPred);
295
296 return true;
297}
298
299/// See if LazyValueInfo's ability to exploit edge conditions or range
300/// information is sufficient to prove this comparison. Even for local
301/// conditions, this can sometimes prove conditions instcombine can't by
302/// exploiting range information.
303static bool constantFoldCmp(CmpInst *Cmp, LazyValueInfo *LVI) {
304 Value *Op0 = Cmp->getOperand(0);
305 Value *Op1 = Cmp->getOperand(1);
307 LVI->getPredicateAt(Cmp->getPredicate(), Op0, Op1, Cmp,
308 /*UseBlockValue=*/true);
309 if (Result == LazyValueInfo::Unknown)
310 return false;
311
312 ++NumCmps;
313 Constant *TorF =
315 Cmp->replaceAllUsesWith(TorF);
316 Cmp->eraseFromParent();
317 return true;
318}
319
320static bool processCmp(CmpInst *Cmp, LazyValueInfo *LVI) {
321 if (constantFoldCmp(Cmp, LVI))
322 return true;
323
324 if (auto *ICmp = dyn_cast<ICmpInst>(Cmp))
325 if (processICmp(ICmp, LVI))
326 return true;
327
328 return false;
329}
330
331/// Simplify a switch instruction by removing cases which can never fire. If the
332/// uselessness of a case could be determined locally then constant propagation
333/// would already have figured it out. Instead, walk the predecessors and
334/// statically evaluate cases based on information available on that edge. Cases
335/// that cannot fire no matter what the incoming edge can safely be removed. If
336/// a case fires on every incoming edge then the entire switch can be removed
337/// and replaced with a branch to the case destination.
339 DominatorTree *DT) {
340 DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy);
341 Value *Cond = I->getCondition();
342 BasicBlock *BB = I->getParent();
343
344 // Analyse each switch case in turn.
345 bool Changed = false;
346 DenseMap<BasicBlock*, int> SuccessorsCount;
347 for (auto *Succ : successors(BB))
348 SuccessorsCount[Succ]++;
349
350 { // Scope for SwitchInstProfUpdateWrapper. It must not live during
351 // ConstantFoldTerminator() as the underlying SwitchInst can be changed.
353
354 for (auto CI = SI->case_begin(), CE = SI->case_end(); CI != CE;) {
355 ConstantInt *Case = CI->getCaseValue();
358 /* UseBlockValue */ true);
359
360 if (State == LazyValueInfo::False) {
361 // This case never fires - remove it.
362 BasicBlock *Succ = CI->getCaseSuccessor();
363 Succ->removePredecessor(BB);
364 CI = SI.removeCase(CI);
365 CE = SI->case_end();
366
367 // The condition can be modified by removePredecessor's PHI simplification
368 // logic.
369 Cond = SI->getCondition();
370
371 ++NumDeadCases;
372 Changed = true;
373 if (--SuccessorsCount[Succ] == 0)
374 DTU.applyUpdatesPermissive({{DominatorTree::Delete, BB, Succ}});
375 continue;
376 }
377 if (State == LazyValueInfo::True) {
378 // This case always fires. Arrange for the switch to be turned into an
379 // unconditional branch by replacing the switch condition with the case
380 // value.
381 SI->setCondition(Case);
382 NumDeadCases += SI->getNumCases();
383 Changed = true;
384 break;
385 }
386
387 // Increment the case iterator since we didn't delete it.
388 ++CI;
389 }
390 }
391
392 if (Changed)
393 // If the switch has been simplified to the point where it can be replaced
394 // by a branch then do so now.
395 ConstantFoldTerminator(BB, /*DeleteDeadConditions = */ false,
396 /*TLI = */ nullptr, &DTU);
397 return Changed;
398}
399
400// See if we can prove that the given binary op intrinsic will not overflow.
405 BO->getBinaryOp(), RRange, BO->getNoWrapKind());
406 return NWRegion.contains(LRange);
407}
408
410 bool NewNSW, bool NewNUW) {
411 Statistic *OpcNW, *OpcNSW, *OpcNUW;
412 switch (Opcode) {
413 case Instruction::Add:
414 OpcNW = &NumAddNW;
415 OpcNSW = &NumAddNSW;
416 OpcNUW = &NumAddNUW;
417 break;
418 case Instruction::Sub:
419 OpcNW = &NumSubNW;
420 OpcNSW = &NumSubNSW;
421 OpcNUW = &NumSubNUW;
422 break;
423 case Instruction::Mul:
424 OpcNW = &NumMulNW;
425 OpcNSW = &NumMulNSW;
426 OpcNUW = &NumMulNUW;
427 break;
428 case Instruction::Shl:
429 OpcNW = &NumShlNW;
430 OpcNSW = &NumShlNSW;
431 OpcNUW = &NumShlNUW;
432 break;
433 default:
434 llvm_unreachable("Will not be called with other binops");
435 }
436
437 auto *Inst = dyn_cast<Instruction>(V);
438 if (NewNSW) {
439 ++NumNW;
440 ++*OpcNW;
441 ++NumNSW;
442 ++*OpcNSW;
443 if (Inst)
444 Inst->setHasNoSignedWrap();
445 }
446 if (NewNUW) {
447 ++NumNW;
448 ++*OpcNW;
449 ++NumNUW;
450 ++*OpcNUW;
451 if (Inst)
452 Inst->setHasNoUnsignedWrap();
453 }
454}
455
456static bool processBinOp(BinaryOperator *BinOp, LazyValueInfo *LVI);
457
458// See if @llvm.abs argument is alays positive/negative, and simplify.
459// Notably, INT_MIN can belong to either range, regardless of the NSW,
460// because it is negation-invariant.
462 Value *X = II->getArgOperand(0);
463 bool IsIntMinPoison = cast<ConstantInt>(II->getArgOperand(1))->isOne();
464
465 Type *Ty = X->getType();
466 Constant *IntMin =
469
470 // Is X in [0, IntMin]? NOTE: INT_MIN is fine!
471 Result = LVI->getPredicateAt(CmpInst::Predicate::ICMP_ULE, X, IntMin, II,
472 /*UseBlockValue=*/true);
473 if (Result == LazyValueInfo::True) {
474 ++NumAbs;
476 II->eraseFromParent();
477 return true;
478 }
479
480 // Is X in [IntMin, 0]? NOTE: INT_MIN is fine!
481 Constant *Zero = ConstantInt::getNullValue(Ty);
482 Result = LVI->getPredicateAt(CmpInst::Predicate::ICMP_SLE, X, Zero, II,
483 /*UseBlockValue=*/true);
484 assert(Result != LazyValueInfo::False && "Should have been handled already.");
485
486 if (Result == LazyValueInfo::Unknown) {
487 // Argument's range crosses zero.
488 bool Changed = false;
489 if (!IsIntMinPoison) {
490 // Can we at least tell that the argument is never INT_MIN?
491 Result = LVI->getPredicateAt(CmpInst::Predicate::ICMP_NE, X, IntMin, II,
492 /*UseBlockValue=*/true);
493 if (Result == LazyValueInfo::True) {
494 ++NumNSW;
495 ++NumSubNSW;
497 Changed = true;
498 }
499 }
500 return Changed;
501 }
502
503 IRBuilder<> B(II);
504 Value *NegX = B.CreateNeg(X, II->getName(), /*HasNUW=*/false,
505 /*HasNSW=*/IsIntMinPoison);
506 ++NumAbs;
507 II->replaceAllUsesWith(NegX);
508 II->eraseFromParent();
509
510 // See if we can infer some no-wrap flags.
511 if (auto *BO = dyn_cast<BinaryOperator>(NegX))
512 processBinOp(BO, LVI);
513
514 return true;
515}
516
517// See if this min/max intrinsic always picks it's one specific operand.
521 Pred, MM->getLHS(), MM->getRHS(), MM, /*UseBlockValue=*/true);
522 if (Result == LazyValueInfo::Unknown)
523 return false;
524
525 ++NumMinMax;
526 MM->replaceAllUsesWith(MM->getOperand(!Result));
527 MM->eraseFromParent();
528 return true;
529}
530
531// Rewrite this with.overflow intrinsic as non-overflowing.
533 IRBuilder<> B(WO);
534 Instruction::BinaryOps Opcode = WO->getBinaryOp();
535 bool NSW = WO->isSigned();
536 bool NUW = !WO->isSigned();
537
538 Value *NewOp =
539 B.CreateBinOp(Opcode, WO->getLHS(), WO->getRHS(), WO->getName());
540 setDeducedOverflowingFlags(NewOp, Opcode, NSW, NUW);
541
542 StructType *ST = cast<StructType>(WO->getType());
544 { PoisonValue::get(ST->getElementType(0)),
545 ConstantInt::getFalse(ST->getElementType(1)) });
546 Value *NewI = B.CreateInsertValue(Struct, NewOp, 0);
547 WO->replaceAllUsesWith(NewI);
548 WO->eraseFromParent();
549 ++NumOverflows;
550
551 // See if we can infer the other no-wrap too.
552 if (auto *BO = dyn_cast<BinaryOperator>(NewOp))
553 processBinOp(BO, LVI);
554
555 return true;
556}
557
559 Instruction::BinaryOps Opcode = SI->getBinaryOp();
560 bool NSW = SI->isSigned();
561 bool NUW = !SI->isSigned();
563 Opcode, SI->getLHS(), SI->getRHS(), SI->getName(), SI);
564 BinOp->setDebugLoc(SI->getDebugLoc());
565 setDeducedOverflowingFlags(BinOp, Opcode, NSW, NUW);
566
567 SI->replaceAllUsesWith(BinOp);
568 SI->eraseFromParent();
569 ++NumSaturating;
570
571 // See if we can infer the other no-wrap too.
572 if (auto *BO = dyn_cast<BinaryOperator>(BinOp))
573 processBinOp(BO, LVI);
574
575 return true;
576}
577
578/// Infer nonnull attributes for the arguments at the specified callsite.
579static bool processCallSite(CallBase &CB, LazyValueInfo *LVI) {
580
581 if (CB.getIntrinsicID() == Intrinsic::abs) {
582 return processAbsIntrinsic(&cast<IntrinsicInst>(CB), LVI);
583 }
584
585 if (auto *MM = dyn_cast<MinMaxIntrinsic>(&CB)) {
586 return processMinMaxIntrinsic(MM, LVI);
587 }
588
589 if (auto *WO = dyn_cast<WithOverflowInst>(&CB)) {
590 if (WO->getLHS()->getType()->isIntegerTy() && willNotOverflow(WO, LVI)) {
591 return processOverflowIntrinsic(WO, LVI);
592 }
593 }
594
595 if (auto *SI = dyn_cast<SaturatingInst>(&CB)) {
596 if (SI->getType()->isIntegerTy() && willNotOverflow(SI, LVI)) {
597 return processSaturatingInst(SI, LVI);
598 }
599 }
600
601 bool Changed = false;
602
603 // Deopt bundle operands are intended to capture state with minimal
604 // perturbance of the code otherwise. If we can find a constant value for
605 // any such operand and remove a use of the original value, that's
606 // desireable since it may allow further optimization of that value (e.g. via
607 // single use rules in instcombine). Since deopt uses tend to,
608 // idiomatically, appear along rare conditional paths, it's reasonable likely
609 // we may have a conditional fact with which LVI can fold.
610 if (auto DeoptBundle = CB.getOperandBundle(LLVMContext::OB_deopt)) {
611 for (const Use &ConstU : DeoptBundle->Inputs) {
612 Use &U = const_cast<Use&>(ConstU);
613 Value *V = U.get();
614 if (V->getType()->isVectorTy()) continue;
615 if (isa<Constant>(V)) continue;
616
617 Constant *C = LVI->getConstant(V, &CB);
618 if (!C) continue;
619 U.set(C);
620 Changed = true;
621 }
622 }
623
625 unsigned ArgNo = 0;
626
627 for (Value *V : CB.args()) {
628 PointerType *Type = dyn_cast<PointerType>(V->getType());
629 // Try to mark pointer typed parameters as non-null. We skip the
630 // relatively expensive analysis for constants which are obviously either
631 // null or non-null to start with.
632 if (Type && !CB.paramHasAttr(ArgNo, Attribute::NonNull) &&
633 !isa<Constant>(V) &&
634 LVI->getPredicateAt(ICmpInst::ICMP_EQ, V,
636 /*UseBlockValue=*/false) == LazyValueInfo::False)
637 ArgNos.push_back(ArgNo);
638 ArgNo++;
639 }
640
641 assert(ArgNo == CB.arg_size() && "Call arguments not processed correctly.");
642
643 if (ArgNos.empty())
644 return Changed;
645
646 NumNonNull += ArgNos.size();
648 LLVMContext &Ctx = CB.getContext();
649 AS = AS.addParamAttribute(Ctx, ArgNos,
650 Attribute::get(Ctx, Attribute::NonNull));
651 CB.setAttributes(AS);
652
653 return true;
654}
655
657
658static Domain getDomain(const ConstantRange &CR) {
659 if (CR.isAllNonNegative())
660 return Domain::NonNegative;
661 if (CR.icmp(ICmpInst::ICMP_SLE, APInt::getZero(CR.getBitWidth())))
662 return Domain::NonPositive;
663 return Domain::Unknown;
664}
665
666/// Try to shrink a sdiv/srem's width down to the smallest power of two that's
667/// sufficient to contain its operands.
668static bool narrowSDivOrSRem(BinaryOperator *Instr, const ConstantRange &LCR,
669 const ConstantRange &RCR) {
670 assert(Instr->getOpcode() == Instruction::SDiv ||
671 Instr->getOpcode() == Instruction::SRem);
672 assert(!Instr->getType()->isVectorTy());
673
674 // Find the smallest power of two bitwidth that's sufficient to hold Instr's
675 // operands.
676 unsigned OrigWidth = Instr->getType()->getIntegerBitWidth();
677
678 // What is the smallest bit width that can accommodate the entire value ranges
679 // of both of the operands?
680 unsigned MinSignedBits =
681 std::max(LCR.getMinSignedBits(), RCR.getMinSignedBits());
682
683 // sdiv/srem is UB if divisor is -1 and divident is INT_MIN, so unless we can
684 // prove that such a combination is impossible, we need to bump the bitwidth.
685 if (RCR.contains(APInt::getAllOnes(OrigWidth)) &&
686 LCR.contains(APInt::getSignedMinValue(MinSignedBits).sext(OrigWidth)))
687 ++MinSignedBits;
688
689 // Don't shrink below 8 bits wide.
690 unsigned NewWidth = std::max<unsigned>(PowerOf2Ceil(MinSignedBits), 8);
691
692 // NewWidth might be greater than OrigWidth if OrigWidth is not a power of
693 // two.
694 if (NewWidth >= OrigWidth)
695 return false;
696
697 ++NumSDivSRemsNarrowed;
698 IRBuilder<> B{Instr};
699 auto *TruncTy = Type::getIntNTy(Instr->getContext(), NewWidth);
700 auto *LHS = B.CreateTruncOrBitCast(Instr->getOperand(0), TruncTy,
701 Instr->getName() + ".lhs.trunc");
702 auto *RHS = B.CreateTruncOrBitCast(Instr->getOperand(1), TruncTy,
703 Instr->getName() + ".rhs.trunc");
704 auto *BO = B.CreateBinOp(Instr->getOpcode(), LHS, RHS, Instr->getName());
705 auto *Sext = B.CreateSExt(BO, Instr->getType(), Instr->getName() + ".sext");
706 if (auto *BinOp = dyn_cast<BinaryOperator>(BO))
707 if (BinOp->getOpcode() == Instruction::SDiv)
708 BinOp->setIsExact(Instr->isExact());
709
710 Instr->replaceAllUsesWith(Sext);
711 Instr->eraseFromParent();
712 return true;
713}
714
715static bool expandUDivOrURem(BinaryOperator *Instr, const ConstantRange &XCR,
716 const ConstantRange &YCR) {
717 Type *Ty = Instr->getType();
718 assert(Instr->getOpcode() == Instruction::UDiv ||
719 Instr->getOpcode() == Instruction::URem);
720 assert(!Ty->isVectorTy());
721 bool IsRem = Instr->getOpcode() == Instruction::URem;
722
723 Value *X = Instr->getOperand(0);
724 Value *Y = Instr->getOperand(1);
725
726 // X u/ Y -> 0 iff X u< Y
727 // X u% Y -> X iff X u< Y
728 if (XCR.icmp(ICmpInst::ICMP_ULT, YCR)) {
729 Instr->replaceAllUsesWith(IsRem ? X : Constant::getNullValue(Ty));
730 Instr->eraseFromParent();
731 ++NumUDivURemsNarrowedExpanded;
732 return true;
733 }
734
735 // Given
736 // R = X u% Y
737 // We can represent the modulo operation as a loop/self-recursion:
738 // urem_rec(X, Y):
739 // Z = X - Y
740 // if X u< Y
741 // ret X
742 // else
743 // ret urem_rec(Z, Y)
744 // which isn't better, but if we only need a single iteration
745 // to compute the answer, this becomes quite good:
746 // R = X < Y ? X : X - Y iff X u< 2*Y (w/ unsigned saturation)
747 // Now, we do not care about all full multiples of Y in X, they do not change
748 // the answer, thus we could rewrite the expression as:
749 // X* = X - (Y * |_ X / Y _|)
750 // R = X* % Y
751 // so we don't need the *first* iteration to return, we just need to
752 // know *which* iteration will always return, so we could also rewrite it as:
753 // X* = X - (Y * |_ X / Y _|)
754 // R = X* % Y iff X* u< 2*Y (w/ unsigned saturation)
755 // but that does not seem profitable here.
756
757 // Even if we don't know X's range, the divisor may be so large, X can't ever
758 // be 2x larger than that. I.e. if divisor is always negative.
759 if (!XCR.icmp(ICmpInst::ICMP_ULT,
760 YCR.umul_sat(APInt(YCR.getBitWidth(), 2))) &&
761 !YCR.isAllNegative())
762 return false;
763
764 IRBuilder<> B(Instr);
765 Value *ExpandedOp;
766 if (IsRem) {
767 // NOTE: this transformation introduces two uses of X,
768 // but it may be undef so we must freeze it first.
769 Value *FrozenX = B.CreateFreeze(X, X->getName() + ".frozen");
770 auto *AdjX = B.CreateNUWSub(FrozenX, Y, Instr->getName() + ".urem");
771 auto *Cmp =
772 B.CreateICmp(ICmpInst::ICMP_ULT, FrozenX, Y, Instr->getName() + ".cmp");
773 ExpandedOp = B.CreateSelect(Cmp, FrozenX, AdjX);
774 } else {
775 auto *Cmp =
776 B.CreateICmp(ICmpInst::ICMP_UGE, X, Y, Instr->getName() + ".cmp");
777 ExpandedOp = B.CreateZExt(Cmp, Ty, Instr->getName() + ".udiv");
778 }
779 ExpandedOp->takeName(Instr);
780 Instr->replaceAllUsesWith(ExpandedOp);
781 Instr->eraseFromParent();
782 ++NumUDivURemsNarrowedExpanded;
783 return true;
784}
785
786/// Try to shrink a udiv/urem's width down to the smallest power of two that's
787/// sufficient to contain its operands.
788static bool narrowUDivOrURem(BinaryOperator *Instr, const ConstantRange &XCR,
789 const ConstantRange &YCR) {
790 assert(Instr->getOpcode() == Instruction::UDiv ||
791 Instr->getOpcode() == Instruction::URem);
792 assert(!Instr->getType()->isVectorTy());
793
794 // Find the smallest power of two bitwidth that's sufficient to hold Instr's
795 // operands.
796
797 // What is the smallest bit width that can accommodate the entire value ranges
798 // of both of the operands?
799 unsigned MaxActiveBits = std::max(XCR.getActiveBits(), YCR.getActiveBits());
800 // Don't shrink below 8 bits wide.
801 unsigned NewWidth = std::max<unsigned>(PowerOf2Ceil(MaxActiveBits), 8);
802
803 // NewWidth might be greater than OrigWidth if OrigWidth is not a power of
804 // two.
805 if (NewWidth >= Instr->getType()->getIntegerBitWidth())
806 return false;
807
808 ++NumUDivURemsNarrowed;
809 IRBuilder<> B{Instr};
810 auto *TruncTy = Type::getIntNTy(Instr->getContext(), NewWidth);
811 auto *LHS = B.CreateTruncOrBitCast(Instr->getOperand(0), TruncTy,
812 Instr->getName() + ".lhs.trunc");
813 auto *RHS = B.CreateTruncOrBitCast(Instr->getOperand(1), TruncTy,
814 Instr->getName() + ".rhs.trunc");
815 auto *BO = B.CreateBinOp(Instr->getOpcode(), LHS, RHS, Instr->getName());
816 auto *Zext = B.CreateZExt(BO, Instr->getType(), Instr->getName() + ".zext");
817 if (auto *BinOp = dyn_cast<BinaryOperator>(BO))
818 if (BinOp->getOpcode() == Instruction::UDiv)
819 BinOp->setIsExact(Instr->isExact());
820
821 Instr->replaceAllUsesWith(Zext);
822 Instr->eraseFromParent();
823 return true;
824}
825
827 assert(Instr->getOpcode() == Instruction::UDiv ||
828 Instr->getOpcode() == Instruction::URem);
829 if (Instr->getType()->isVectorTy())
830 return false;
831
834 if (expandUDivOrURem(Instr, XCR, YCR))
835 return true;
836
837 return narrowUDivOrURem(Instr, XCR, YCR);
838}
839
840static bool processSRem(BinaryOperator *SDI, const ConstantRange &LCR,
841 const ConstantRange &RCR, LazyValueInfo *LVI) {
842 assert(SDI->getOpcode() == Instruction::SRem);
843 assert(!SDI->getType()->isVectorTy());
844
845 if (LCR.abs().icmp(CmpInst::ICMP_ULT, RCR.abs())) {
846 SDI->replaceAllUsesWith(SDI->getOperand(0));
847 SDI->eraseFromParent();
848 return true;
849 }
850
851 struct Operand {
852 Value *V;
853 Domain D;
854 };
855 std::array<Operand, 2> Ops = {{{SDI->getOperand(0), getDomain(LCR)},
856 {SDI->getOperand(1), getDomain(RCR)}}};
857 if (Ops[0].D == Domain::Unknown || Ops[1].D == Domain::Unknown)
858 return false;
859
860 // We know domains of both of the operands!
861 ++NumSRems;
862
863 // We need operands to be non-negative, so negate each one that isn't.
864 for (Operand &Op : Ops) {
865 if (Op.D == Domain::NonNegative)
866 continue;
867 auto *BO =
868 BinaryOperator::CreateNeg(Op.V, Op.V->getName() + ".nonneg", SDI);
869 BO->setDebugLoc(SDI->getDebugLoc());
870 Op.V = BO;
871 }
872
873 auto *URem =
874 BinaryOperator::CreateURem(Ops[0].V, Ops[1].V, SDI->getName(), SDI);
875 URem->setDebugLoc(SDI->getDebugLoc());
876
877 auto *Res = URem;
878
879 // If the divident was non-positive, we need to negate the result.
880 if (Ops[0].D == Domain::NonPositive) {
881 Res = BinaryOperator::CreateNeg(Res, Res->getName() + ".neg", SDI);
882 Res->setDebugLoc(SDI->getDebugLoc());
883 }
884
885 SDI->replaceAllUsesWith(Res);
886 SDI->eraseFromParent();
887
888 // Try to simplify our new urem.
889 processUDivOrURem(URem, LVI);
890
891 return true;
892}
893
894/// See if LazyValueInfo's ability to exploit edge conditions or range
895/// information is sufficient to prove the signs of both operands of this SDiv.
896/// If this is the case, replace the SDiv with a UDiv. Even for local
897/// conditions, this can sometimes prove conditions instcombine can't by
898/// exploiting range information.
899static bool processSDiv(BinaryOperator *SDI, const ConstantRange &LCR,
900 const ConstantRange &RCR, LazyValueInfo *LVI) {
901 assert(SDI->getOpcode() == Instruction::SDiv);
902 assert(!SDI->getType()->isVectorTy());
903
904 struct Operand {
905 Value *V;
906 Domain D;
907 };
908 std::array<Operand, 2> Ops = {{{SDI->getOperand(0), getDomain(LCR)},
909 {SDI->getOperand(1), getDomain(RCR)}}};
910 if (Ops[0].D == Domain::Unknown || Ops[1].D == Domain::Unknown)
911 return false;
912
913 // We know domains of both of the operands!
914 ++NumSDivs;
915
916 // We need operands to be non-negative, so negate each one that isn't.
917 for (Operand &Op : Ops) {
918 if (Op.D == Domain::NonNegative)
919 continue;
920 auto *BO =
921 BinaryOperator::CreateNeg(Op.V, Op.V->getName() + ".nonneg", SDI);
922 BO->setDebugLoc(SDI->getDebugLoc());
923 Op.V = BO;
924 }
925
926 auto *UDiv =
927 BinaryOperator::CreateUDiv(Ops[0].V, Ops[1].V, SDI->getName(), SDI);
928 UDiv->setDebugLoc(SDI->getDebugLoc());
929 UDiv->setIsExact(SDI->isExact());
930
931 Value *Res = UDiv;
932
933 // If the operands had two different domains, we need to negate the result.
934 if (Ops[0].D != Ops[1].D)
935 Res = BinaryOperator::CreateNeg(Res, Res->getName() + ".neg", SDI);
936
937 SDI->replaceAllUsesWith(Res);
938 SDI->eraseFromParent();
939
940 // Try to simplify our new udiv.
941 processUDivOrURem(UDiv, LVI);
942
943 return true;
944}
945
947 assert(Instr->getOpcode() == Instruction::SDiv ||
948 Instr->getOpcode() == Instruction::SRem);
949 if (Instr->getType()->isVectorTy())
950 return false;
951
954 if (Instr->getOpcode() == Instruction::SDiv)
955 if (processSDiv(Instr, LCR, RCR, LVI))
956 return true;
957
958 if (Instr->getOpcode() == Instruction::SRem) {
959 if (processSRem(Instr, LCR, RCR, LVI))
960 return true;
961 }
962
963 return narrowSDivOrSRem(Instr, LCR, RCR);
964}
965
966static bool processAShr(BinaryOperator *SDI, LazyValueInfo *LVI) {
967 if (SDI->getType()->isVectorTy())
968 return false;
969
971 unsigned OrigWidth = SDI->getType()->getIntegerBitWidth();
972 ConstantRange NegOneOrZero =
973 ConstantRange(APInt(OrigWidth, (uint64_t)-1, true), APInt(OrigWidth, 1));
974 if (NegOneOrZero.contains(LRange)) {
975 // ashr of -1 or 0 never changes the value, so drop the whole instruction
976 ++NumAShrsRemoved;
977 SDI->replaceAllUsesWith(SDI->getOperand(0));
978 SDI->eraseFromParent();
979 return true;
980 }
981
982 if (!LRange.isAllNonNegative())
983 return false;
984
985 ++NumAShrsConverted;
986 auto *BO = BinaryOperator::CreateLShr(SDI->getOperand(0), SDI->getOperand(1),
987 "", SDI);
988 BO->takeName(SDI);
989 BO->setDebugLoc(SDI->getDebugLoc());
990 BO->setIsExact(SDI->isExact());
991 SDI->replaceAllUsesWith(BO);
992 SDI->eraseFromParent();
993
994 return true;
995}
996
997static bool processSExt(SExtInst *SDI, LazyValueInfo *LVI) {
998 if (SDI->getType()->isVectorTy())
999 return false;
1000
1001 const Use &Base = SDI->getOperandUse(0);
1003 return false;
1004
1005 ++NumSExt;
1006 auto *ZExt = CastInst::CreateZExtOrBitCast(Base, SDI->getType(), "", SDI);
1007 ZExt->takeName(SDI);
1008 ZExt->setDebugLoc(SDI->getDebugLoc());
1009 SDI->replaceAllUsesWith(ZExt);
1010 SDI->eraseFromParent();
1011
1012 return true;
1013}
1014
1015static bool processBinOp(BinaryOperator *BinOp, LazyValueInfo *LVI) {
1016 using OBO = OverflowingBinaryOperator;
1017
1018 if (BinOp->getType()->isVectorTy())
1019 return false;
1020
1021 bool NSW = BinOp->hasNoSignedWrap();
1022 bool NUW = BinOp->hasNoUnsignedWrap();
1023 if (NSW && NUW)
1024 return false;
1025
1026 Instruction::BinaryOps Opcode = BinOp->getOpcode();
1027 Value *LHS = BinOp->getOperand(0);
1028 Value *RHS = BinOp->getOperand(1);
1029
1030 ConstantRange LRange = LVI->getConstantRange(LHS, BinOp);
1031 ConstantRange RRange = LVI->getConstantRange(RHS, BinOp);
1032
1033 bool Changed = false;
1034 bool NewNUW = false, NewNSW = false;
1035 if (!NUW) {
1037 Opcode, RRange, OBO::NoUnsignedWrap);
1038 NewNUW = NUWRange.contains(LRange);
1039 Changed |= NewNUW;
1040 }
1041 if (!NSW) {
1043 Opcode, RRange, OBO::NoSignedWrap);
1044 NewNSW = NSWRange.contains(LRange);
1045 Changed |= NewNSW;
1046 }
1047
1048 setDeducedOverflowingFlags(BinOp, Opcode, NewNSW, NewNUW);
1049
1050 return Changed;
1051}
1052
1053static bool processAnd(BinaryOperator *BinOp, LazyValueInfo *LVI) {
1054 if (BinOp->getType()->isVectorTy())
1055 return false;
1056
1057 // Pattern match (and lhs, C) where C includes a superset of bits which might
1058 // be set in lhs. This is a common truncation idiom created by instcombine.
1059 const Use &LHS = BinOp->getOperandUse(0);
1060 ConstantInt *RHS = dyn_cast<ConstantInt>(BinOp->getOperand(1));
1061 if (!RHS || !RHS->getValue().isMask())
1062 return false;
1063
1064 // We can only replace the AND with LHS based on range info if the range does
1065 // not include undef.
1066 ConstantRange LRange =
1067 LVI->getConstantRangeAtUse(LHS, /*UndefAllowed=*/false);
1068 if (!LRange.getUnsignedMax().ule(RHS->getValue()))
1069 return false;
1070
1071 BinOp->replaceAllUsesWith(LHS);
1072 BinOp->eraseFromParent();
1073 NumAnd++;
1074 return true;
1075}
1076
1077
1079 if (Constant *C = LVI->getConstant(V, At))
1080 return C;
1081
1082 // TODO: The following really should be sunk inside LVI's core algorithm, or
1083 // at least the outer shims around such.
1084 auto *C = dyn_cast<CmpInst>(V);
1085 if (!C) return nullptr;
1086
1087 Value *Op0 = C->getOperand(0);
1088 Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
1089 if (!Op1) return nullptr;
1090
1092 C->getPredicate(), Op0, Op1, At, /*UseBlockValue=*/false);
1093 if (Result == LazyValueInfo::Unknown)
1094 return nullptr;
1095
1096 return (Result == LazyValueInfo::True) ?
1097 ConstantInt::getTrue(C->getContext()) :
1098 ConstantInt::getFalse(C->getContext());
1099}
1100
1102 const SimplifyQuery &SQ) {
1103 bool FnChanged = false;
1104 // Visiting in a pre-order depth-first traversal causes us to simplify early
1105 // blocks before querying later blocks (which require us to analyze early
1106 // blocks). Eagerly simplifying shallow blocks means there is strictly less
1107 // work to do for deep blocks. This also means we don't visit unreachable
1108 // blocks.
1109 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
1110 bool BBChanged = false;
1111 for (Instruction &II : llvm::make_early_inc_range(*BB)) {
1112 switch (II.getOpcode()) {
1113 case Instruction::Select:
1114 BBChanged |= processSelect(cast<SelectInst>(&II), LVI);
1115 break;
1116 case Instruction::PHI:
1117 BBChanged |= processPHI(cast<PHINode>(&II), LVI, DT, SQ);
1118 break;
1119 case Instruction::ICmp:
1120 case Instruction::FCmp:
1121 BBChanged |= processCmp(cast<CmpInst>(&II), LVI);
1122 break;
1123 case Instruction::Load:
1124 case Instruction::Store:
1125 BBChanged |= processMemAccess(&II, LVI);
1126 break;
1127 case Instruction::Call:
1128 case Instruction::Invoke:
1129 BBChanged |= processCallSite(cast<CallBase>(II), LVI);
1130 break;
1131 case Instruction::SRem:
1132 case Instruction::SDiv:
1133 BBChanged |= processSDivOrSRem(cast<BinaryOperator>(&II), LVI);
1134 break;
1135 case Instruction::UDiv:
1136 case Instruction::URem:
1137 BBChanged |= processUDivOrURem(cast<BinaryOperator>(&II), LVI);
1138 break;
1139 case Instruction::AShr:
1140 BBChanged |= processAShr(cast<BinaryOperator>(&II), LVI);
1141 break;
1142 case Instruction::SExt:
1143 BBChanged |= processSExt(cast<SExtInst>(&II), LVI);
1144 break;
1145 case Instruction::Add:
1146 case Instruction::Sub:
1147 case Instruction::Mul:
1148 case Instruction::Shl:
1149 BBChanged |= processBinOp(cast<BinaryOperator>(&II), LVI);
1150 break;
1151 case Instruction::And:
1152 BBChanged |= processAnd(cast<BinaryOperator>(&II), LVI);
1153 break;
1154 }
1155 }
1156
1157 Instruction *Term = BB->getTerminator();
1158 switch (Term->getOpcode()) {
1159 case Instruction::Switch:
1160 BBChanged |= processSwitch(cast<SwitchInst>(Term), LVI, DT);
1161 break;
1162 case Instruction::Ret: {
1163 auto *RI = cast<ReturnInst>(Term);
1164 // Try to determine the return value if we can. This is mainly here to
1165 // simplify the writing of unit tests, but also helps to enable IPO by
1166 // constant folding the return values of callees.
1167 auto *RetVal = RI->getReturnValue();
1168 if (!RetVal) break; // handle "ret void"
1169 if (isa<Constant>(RetVal)) break; // nothing to do
1170 if (auto *C = getConstantAt(RetVal, RI, LVI)) {
1171 ++NumReturns;
1172 RI->replaceUsesOfWith(RetVal, C);
1173 BBChanged = true;
1174 }
1175 }
1176 }
1177
1178 FnChanged |= BBChanged;
1179 }
1180
1181 return FnChanged;
1182}
1183
1188
1189 bool Changed = runImpl(F, LVI, DT, getBestSimplifyQuery(AM, F));
1190
1192 if (!Changed) {
1194 } else {
1197 }
1198
1199 // Keeping LVI alive is expensive, both because it uses a lot of memory, and
1200 // because invalidating values in LVI is expensive. While CVP does preserve
1201 // LVI, we know that passes after JumpThreading+CVP will not need the result
1202 // of this analysis, so we forcefully discard it early.
1204 return PA;
1205}
This file contains the simple types necessary to represent the attributes associated with functions a...
SmallVector< MachineOperand, 4 > Cond
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 processMemAccess(Instruction *I, 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 processUDivOrURem(BinaryOperator *Instr, 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 cl::opt< bool > CanonicalizeICmpPredicatesToUnsigned("canonicalize-icmp-predicates-to-unsigned", cl::init(true), cl::Hidden, cl::desc("Enables canonicalization of signed relational predicates to " "unsigned (e.g. sgt => ugt)"))
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 processSExt(SExtInst *SDI, LazyValueInfo *LVI)
static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI, DominatorTree *DT)
Simplify a switch instruction by removing cases which can never fire.
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)
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...
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
This header defines various interfaces for pass management in LLVM.
@ SI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:75
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:214
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:199
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1132
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:945
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:177
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:774
AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const
Add an argument attribute to the list.
Definition: Attributes.h:576
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Definition: Attributes.cpp:91
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
Definition: BasicBlock.cpp:349
This class represents an intrinsic that is based on a binary operation.
Value * getRHS() const
unsigned getNoWrapKind() const
Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
bool isSigned() const
Whether the intrinsic is signed or unsigned.
Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
Value * getLHS() const
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
static BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Helper functions to construct and inspect unary operations (NEG and NOT) via binary operators SUB and...
BinaryOps getOpcode() const
Definition: InstrTypes.h:391
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1190
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Definition: InstrTypes.h:2055
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
Definition: InstrTypes.h:1494
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1357
void setArgOperand(unsigned i, Value *v)
Definition: InstrTypes.h:1362
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:1348
unsigned arg_size() const
Definition: InstrTypes.h:1355
AttributeList getAttributes() const
Return the parameter attributes for this call.
Definition: InstrTypes.h:1490
static CastInst * CreateZExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt or BitCast cast instruction.
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:701
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:1058
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:711
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:736
@ ICMP_EQ
equal
Definition: InstrTypes.h:732
Predicate getNonStrictPredicate() const
For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
Definition: InstrTypes.h:907
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Definition: Constants.h:203
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:833
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:888
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:840
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1691
This class represents a range of values.
Definition: ConstantRange.h:47
unsigned getActiveBits() const
Compute the maximal number of active bits needed to represent every value in this range.
ConstantRange umul_sat(const ConstantRange &Other) const
Perform an unsigned saturating multiplication of two constant ranges.
static 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...
bool isAllNegative() const
Return true if all values in this range are negative.
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...
ConstantRange abs(bool IntMinIsPoison=false) const
Calculate absolute value range.
bool isAllNonNegative() const
Return true if all values in this range are non-negative.
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
static 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)...
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 Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1300
This is an important base class in LLVM.
Definition: Constant.h:41
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:356
void applyUpdatesPermissive(ArrayRef< DominatorTree::UpdateType > Updates)
Submit updates to all available trees.
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:166
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:122
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:2570
bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
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:365
bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:82
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:362
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:47
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Analysis to compute lazy value information.
This pass computes, caches, and vends lazy value constraint information.
Definition: LazyValueInfo.h:31
ConstantRange getConstantRangeAtUse(const Use &U, bool UndefAllowed=true)
Return the ConstantRange constraint that is known to hold for the value at a specific use-site.
Tristate
This is used to return true/false/dunno results.
Definition: LazyValueInfo.h:60
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.
Tristate getPredicateOnEdge(unsigned 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 ...
Tristate getPredicateAt(unsigned 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 ...
Constant * getConstant(Value *V, Instruction *CxtI)
Determine whether the specified value is known to be a constant at the specified instruction.
ConstantRange getConstantRange(Value *V, Instruction *CxtI, bool UndefAllowed=true)
Return the ConstantRange constraint that is known to hold for the specified value at the specified in...
An instruction for reading from memory.
Definition: Instructions.h:177
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.
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition: Operator.h:75
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1743
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
void abandon()
Mark an analysis as abandoned.
Definition: PassManager.h:206
void preserve()
Mark an analysis as preserved.
Definition: PassManager.h:173
This class represents a sign extension of integer types.
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:94
size_t size() const
Definition: SmallVector.h:91
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
Class to represent struct types.
Definition: DerivedTypes.h:213
A wrapper class to simplify modification of SwitchInst cases along with their prof branch_weights met...
Multiway switch.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:265
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
const Use & getOperandUse(unsigned i) const
Definition: User.h:182
Value * getOperand(unsigned i) const
Definition: User.h:169
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:535
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1069
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:384
Represents an op.with.overflow intrinsic.
#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
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:126
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:748
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition: MathExtras.h:430
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)
bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
const SimplifyQuery getBestSimplifyQuery(Pass &, Function &)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)