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