1 //===- ValueTracking.cpp - Walk computations to compute properties --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains routines that help analyze properties that chains of
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/ValueTracking.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/Analysis/AssumptionCache.h"
18 #include "llvm/Analysis/InstructionSimplify.h"
19 #include "llvm/Analysis/MemoryBuiltins.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/IR/CallSite.h"
22 #include "llvm/IR/ConstantRange.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/Dominators.h"
26 #include "llvm/IR/GetElementPtrTypeIterator.h"
27 #include "llvm/IR/GlobalAlias.h"
28 #include "llvm/IR/GlobalVariable.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/IntrinsicInst.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/Metadata.h"
33 #include "llvm/IR/Operator.h"
34 #include "llvm/IR/PatternMatch.h"
35 #include "llvm/IR/Statepoint.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/MathExtras.h"
40 using namespace llvm::PatternMatch;
42 const unsigned MaxDepth = 6;
44 /// Enable an experimental feature to leverage information about dominating
45 /// conditions to compute known bits. The individual options below control how
46 /// hard we search. The defaults are choosen to be fairly aggressive. If you
47 /// run into compile time problems when testing, scale them back and report
49 static cl::opt<bool> EnableDomConditions("value-tracking-dom-conditions",
50 cl::Hidden, cl::init(false));
52 // This is expensive, so we only do it for the top level query value.
53 // (TODO: evaluate cost vs profit, consider higher thresholds)
54 static cl::opt<unsigned> DomConditionsMaxDepth("dom-conditions-max-depth",
55 cl::Hidden, cl::init(1));
57 /// How many dominating blocks should be scanned looking for dominating
59 static cl::opt<unsigned> DomConditionsMaxDomBlocks("dom-conditions-dom-blocks",
63 // Controls the number of uses of the value searched for possible
64 // dominating comparisons.
65 static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
66 cl::Hidden, cl::init(2000));
68 // If true, don't consider only compares whose only use is a branch.
69 static cl::opt<bool> DomConditionsSingleCmpUse("dom-conditions-single-cmp-use",
70 cl::Hidden, cl::init(false));
72 /// Returns the bitwidth of the given scalar or pointer type (if unknown returns
73 /// 0). For vector types, returns the element type's bitwidth.
74 static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
75 if (unsigned BitWidth = Ty->getScalarSizeInBits())
78 return DL.getPointerTypeSizeInBits(Ty);
81 // Many of these functions have internal versions that take an assumption
82 // exclusion set. This is because of the potential for mutual recursion to
83 // cause computeKnownBits to repeatedly visit the same assume intrinsic. The
84 // classic case of this is assume(x = y), which will attempt to determine
85 // bits in x from bits in y, which will attempt to determine bits in y from
86 // bits in x, etc. Regarding the mutual recursion, computeKnownBits can call
87 // isKnownNonZero, which calls computeKnownBits and ComputeSignBit and
88 // isKnownToBeAPowerOfTwo (all of which can call computeKnownBits), and so on.
89 typedef SmallPtrSet<const Value *, 8> ExclInvsSet;
92 // Simplifying using an assume can only be done in a particular control-flow
93 // context (the context instruction provides that context). If an assume and
94 // the context instruction are not in the same block then the DT helps in
95 // figuring out if we can use it.
99 const Instruction *CxtI;
100 const DominatorTree *DT;
102 Query(AssumptionCache *AC = nullptr, const Instruction *CxtI = nullptr,
103 const DominatorTree *DT = nullptr)
104 : AC(AC), CxtI(CxtI), DT(DT) {}
106 Query(const Query &Q, const Value *NewExcl)
107 : ExclInvs(Q.ExclInvs), AC(Q.AC), CxtI(Q.CxtI), DT(Q.DT) {
108 ExclInvs.insert(NewExcl);
111 } // end anonymous namespace
113 // Given the provided Value and, potentially, a context instruction, return
114 // the preferred context instruction (if any).
115 static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
116 // If we've been provided with a context instruction, then use that (provided
117 // it has been inserted).
118 if (CxtI && CxtI->getParent())
121 // If the value is really an already-inserted instruction, then use that.
122 CxtI = dyn_cast<Instruction>(V);
123 if (CxtI && CxtI->getParent())
129 static void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
130 const DataLayout &DL, unsigned Depth,
133 void llvm::computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
134 const DataLayout &DL, unsigned Depth,
135 AssumptionCache *AC, const Instruction *CxtI,
136 const DominatorTree *DT) {
137 ::computeKnownBits(V, KnownZero, KnownOne, DL, Depth,
138 Query(AC, safeCxtI(V, CxtI), DT));
141 static void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
142 const DataLayout &DL, unsigned Depth,
145 void llvm::ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
146 const DataLayout &DL, unsigned Depth,
147 AssumptionCache *AC, const Instruction *CxtI,
148 const DominatorTree *DT) {
149 ::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth,
150 Query(AC, safeCxtI(V, CxtI), DT));
153 static bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero, unsigned Depth,
154 const Query &Q, const DataLayout &DL);
156 bool llvm::isKnownToBeAPowerOfTwo(Value *V, const DataLayout &DL, bool OrZero,
157 unsigned Depth, AssumptionCache *AC,
158 const Instruction *CxtI,
159 const DominatorTree *DT) {
160 return ::isKnownToBeAPowerOfTwo(V, OrZero, Depth,
161 Query(AC, safeCxtI(V, CxtI), DT), DL);
164 static bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth,
167 bool llvm::isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth,
168 AssumptionCache *AC, const Instruction *CxtI,
169 const DominatorTree *DT) {
170 return ::isKnownNonZero(V, DL, Depth, Query(AC, safeCxtI(V, CxtI), DT));
173 static bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
174 unsigned Depth, const Query &Q);
176 bool llvm::MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
177 unsigned Depth, AssumptionCache *AC,
178 const Instruction *CxtI, const DominatorTree *DT) {
179 return ::MaskedValueIsZero(V, Mask, DL, Depth,
180 Query(AC, safeCxtI(V, CxtI), DT));
183 static unsigned ComputeNumSignBits(Value *V, const DataLayout &DL,
184 unsigned Depth, const Query &Q);
186 unsigned llvm::ComputeNumSignBits(Value *V, const DataLayout &DL,
187 unsigned Depth, AssumptionCache *AC,
188 const Instruction *CxtI,
189 const DominatorTree *DT) {
190 return ::ComputeNumSignBits(V, DL, Depth, Query(AC, safeCxtI(V, CxtI), DT));
193 static void computeKnownBitsAddSub(bool Add, Value *Op0, Value *Op1, bool NSW,
194 APInt &KnownZero, APInt &KnownOne,
195 APInt &KnownZero2, APInt &KnownOne2,
196 const DataLayout &DL, unsigned Depth,
199 if (ConstantInt *CLHS = dyn_cast<ConstantInt>(Op0)) {
200 // We know that the top bits of C-X are clear if X contains less bits
201 // than C (i.e. no wrap-around can happen). For example, 20-X is
202 // positive if we can prove that X is >= 0 and < 16.
203 if (!CLHS->getValue().isNegative()) {
204 unsigned BitWidth = KnownZero.getBitWidth();
205 unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros();
206 // NLZ can't be BitWidth with no sign bit
207 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
208 computeKnownBits(Op1, KnownZero2, KnownOne2, DL, Depth + 1, Q);
210 // If all of the MaskV bits are known to be zero, then we know the
211 // output top bits are zero, because we now know that the output is
213 if ((KnownZero2 & MaskV) == MaskV) {
214 unsigned NLZ2 = CLHS->getValue().countLeadingZeros();
215 // Top bits known zero.
216 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
222 unsigned BitWidth = KnownZero.getBitWidth();
224 // If an initial sequence of bits in the result is not needed, the
225 // corresponding bits in the operands are not needed.
226 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
227 computeKnownBits(Op0, LHSKnownZero, LHSKnownOne, DL, Depth + 1, Q);
228 computeKnownBits(Op1, KnownZero2, KnownOne2, DL, Depth + 1, Q);
230 // Carry in a 1 for a subtract, rather than a 0.
231 APInt CarryIn(BitWidth, 0);
233 // Sum = LHS + ~RHS + 1
234 std::swap(KnownZero2, KnownOne2);
238 APInt PossibleSumZero = ~LHSKnownZero + ~KnownZero2 + CarryIn;
239 APInt PossibleSumOne = LHSKnownOne + KnownOne2 + CarryIn;
241 // Compute known bits of the carry.
242 APInt CarryKnownZero = ~(PossibleSumZero ^ LHSKnownZero ^ KnownZero2);
243 APInt CarryKnownOne = PossibleSumOne ^ LHSKnownOne ^ KnownOne2;
245 // Compute set of known bits (where all three relevant bits are known).
246 APInt LHSKnown = LHSKnownZero | LHSKnownOne;
247 APInt RHSKnown = KnownZero2 | KnownOne2;
248 APInt CarryKnown = CarryKnownZero | CarryKnownOne;
249 APInt Known = LHSKnown & RHSKnown & CarryKnown;
251 assert((PossibleSumZero & Known) == (PossibleSumOne & Known) &&
252 "known bits of sum differ");
254 // Compute known bits of the result.
255 KnownZero = ~PossibleSumOne & Known;
256 KnownOne = PossibleSumOne & Known;
258 // Are we still trying to solve for the sign bit?
259 if (!Known.isNegative()) {
261 // Adding two non-negative numbers, or subtracting a negative number from
262 // a non-negative one, can't wrap into negative.
263 if (LHSKnownZero.isNegative() && KnownZero2.isNegative())
264 KnownZero |= APInt::getSignBit(BitWidth);
265 // Adding two negative numbers, or subtracting a non-negative number from
266 // a negative one, can't wrap into non-negative.
267 else if (LHSKnownOne.isNegative() && KnownOne2.isNegative())
268 KnownOne |= APInt::getSignBit(BitWidth);
273 static void computeKnownBitsMul(Value *Op0, Value *Op1, bool NSW,
274 APInt &KnownZero, APInt &KnownOne,
275 APInt &KnownZero2, APInt &KnownOne2,
276 const DataLayout &DL, unsigned Depth,
278 unsigned BitWidth = KnownZero.getBitWidth();
279 computeKnownBits(Op1, KnownZero, KnownOne, DL, Depth + 1, Q);
280 computeKnownBits(Op0, KnownZero2, KnownOne2, DL, Depth + 1, Q);
282 bool isKnownNegative = false;
283 bool isKnownNonNegative = false;
284 // If the multiplication is known not to overflow, compute the sign bit.
287 // The product of a number with itself is non-negative.
288 isKnownNonNegative = true;
290 bool isKnownNonNegativeOp1 = KnownZero.isNegative();
291 bool isKnownNonNegativeOp0 = KnownZero2.isNegative();
292 bool isKnownNegativeOp1 = KnownOne.isNegative();
293 bool isKnownNegativeOp0 = KnownOne2.isNegative();
294 // The product of two numbers with the same sign is non-negative.
295 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
296 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
297 // The product of a negative number and a non-negative number is either
299 if (!isKnownNonNegative)
300 isKnownNegative = (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
301 isKnownNonZero(Op0, DL, Depth, Q)) ||
302 (isKnownNegativeOp0 && isKnownNonNegativeOp1 &&
303 isKnownNonZero(Op1, DL, Depth, Q));
307 // If low bits are zero in either operand, output low known-0 bits.
308 // Also compute a conserative estimate for high known-0 bits.
309 // More trickiness is possible, but this is sufficient for the
310 // interesting case of alignment computation.
311 KnownOne.clearAllBits();
312 unsigned TrailZ = KnownZero.countTrailingOnes() +
313 KnownZero2.countTrailingOnes();
314 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
315 KnownZero2.countLeadingOnes(),
316 BitWidth) - BitWidth;
318 TrailZ = std::min(TrailZ, BitWidth);
319 LeadZ = std::min(LeadZ, BitWidth);
320 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
321 APInt::getHighBitsSet(BitWidth, LeadZ);
323 // Only make use of no-wrap flags if we failed to compute the sign bit
324 // directly. This matters if the multiplication always overflows, in
325 // which case we prefer to follow the result of the direct computation,
326 // though as the program is invoking undefined behaviour we can choose
327 // whatever we like here.
328 if (isKnownNonNegative && !KnownOne.isNegative())
329 KnownZero.setBit(BitWidth - 1);
330 else if (isKnownNegative && !KnownZero.isNegative())
331 KnownOne.setBit(BitWidth - 1);
334 void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
336 unsigned BitWidth = KnownZero.getBitWidth();
337 unsigned NumRanges = Ranges.getNumOperands() / 2;
338 assert(NumRanges >= 1);
340 // Use the high end of the ranges to find leading zeros.
341 unsigned MinLeadingZeros = BitWidth;
342 for (unsigned i = 0; i < NumRanges; ++i) {
344 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
346 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
347 ConstantRange Range(Lower->getValue(), Upper->getValue());
348 if (Range.isWrappedSet())
349 MinLeadingZeros = 0; // -1 has no zeros
350 unsigned LeadingZeros = (Upper->getValue() - 1).countLeadingZeros();
351 MinLeadingZeros = std::min(LeadingZeros, MinLeadingZeros);
354 KnownZero = APInt::getHighBitsSet(BitWidth, MinLeadingZeros);
357 static bool isEphemeralValueOf(Instruction *I, const Value *E) {
358 SmallVector<const Value *, 16> WorkSet(1, I);
359 SmallPtrSet<const Value *, 32> Visited;
360 SmallPtrSet<const Value *, 16> EphValues;
362 while (!WorkSet.empty()) {
363 const Value *V = WorkSet.pop_back_val();
364 if (!Visited.insert(V).second)
367 // If all uses of this value are ephemeral, then so is this value.
368 bool FoundNEUse = false;
369 for (const User *I : V->users())
370 if (!EphValues.count(I)) {
380 if (const User *U = dyn_cast<User>(V))
381 for (User::const_op_iterator J = U->op_begin(), JE = U->op_end();
383 if (isSafeToSpeculativelyExecute(*J))
384 WorkSet.push_back(*J);
392 // Is this an intrinsic that cannot be speculated but also cannot trap?
393 static bool isAssumeLikeIntrinsic(const Instruction *I) {
394 if (const CallInst *CI = dyn_cast<CallInst>(I))
395 if (Function *F = CI->getCalledFunction())
396 switch (F->getIntrinsicID()) {
398 // FIXME: This list is repeated from NoTTI::getIntrinsicCost.
399 case Intrinsic::assume:
400 case Intrinsic::dbg_declare:
401 case Intrinsic::dbg_value:
402 case Intrinsic::invariant_start:
403 case Intrinsic::invariant_end:
404 case Intrinsic::lifetime_start:
405 case Intrinsic::lifetime_end:
406 case Intrinsic::objectsize:
407 case Intrinsic::ptr_annotation:
408 case Intrinsic::var_annotation:
415 static bool isValidAssumeForContext(Value *V, const Query &Q) {
416 Instruction *Inv = cast<Instruction>(V);
418 // There are two restrictions on the use of an assume:
419 // 1. The assume must dominate the context (or the control flow must
420 // reach the assume whenever it reaches the context).
421 // 2. The context must not be in the assume's set of ephemeral values
422 // (otherwise we will use the assume to prove that the condition
423 // feeding the assume is trivially true, thus causing the removal of
427 if (Q.DT->dominates(Inv, Q.CxtI)) {
429 } else if (Inv->getParent() == Q.CxtI->getParent()) {
430 // The context comes first, but they're both in the same block. Make sure
431 // there is nothing in between that might interrupt the control flow.
432 for (BasicBlock::const_iterator I =
433 std::next(BasicBlock::const_iterator(Q.CxtI)),
434 IE(Inv); I != IE; ++I)
435 if (!isSafeToSpeculativelyExecute(I) && !isAssumeLikeIntrinsic(I))
438 return !isEphemeralValueOf(Inv, Q.CxtI);
444 // When we don't have a DT, we do a limited search...
445 if (Inv->getParent() == Q.CxtI->getParent()->getSinglePredecessor()) {
447 } else if (Inv->getParent() == Q.CxtI->getParent()) {
448 // Search forward from the assume until we reach the context (or the end
449 // of the block); the common case is that the assume will come first.
450 for (BasicBlock::iterator I = std::next(BasicBlock::iterator(Inv)),
451 IE = Inv->getParent()->end(); I != IE; ++I)
455 // The context must come first...
456 for (BasicBlock::const_iterator I =
457 std::next(BasicBlock::const_iterator(Q.CxtI)),
458 IE(Inv); I != IE; ++I)
459 if (!isSafeToSpeculativelyExecute(I) && !isAssumeLikeIntrinsic(I))
462 return !isEphemeralValueOf(Inv, Q.CxtI);
468 bool llvm::isValidAssumeForContext(const Instruction *I,
469 const Instruction *CxtI,
470 const DominatorTree *DT) {
471 return ::isValidAssumeForContext(const_cast<Instruction *>(I),
472 Query(nullptr, CxtI, DT));
475 template<typename LHS, typename RHS>
476 inline match_combine_or<CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>,
477 CmpClass_match<RHS, LHS, ICmpInst, ICmpInst::Predicate>>
478 m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
479 return m_CombineOr(m_ICmp(Pred, L, R), m_ICmp(Pred, R, L));
482 template<typename LHS, typename RHS>
483 inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::And>,
484 BinaryOp_match<RHS, LHS, Instruction::And>>
485 m_c_And(const LHS &L, const RHS &R) {
486 return m_CombineOr(m_And(L, R), m_And(R, L));
489 template<typename LHS, typename RHS>
490 inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::Or>,
491 BinaryOp_match<RHS, LHS, Instruction::Or>>
492 m_c_Or(const LHS &L, const RHS &R) {
493 return m_CombineOr(m_Or(L, R), m_Or(R, L));
496 template<typename LHS, typename RHS>
497 inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::Xor>,
498 BinaryOp_match<RHS, LHS, Instruction::Xor>>
499 m_c_Xor(const LHS &L, const RHS &R) {
500 return m_CombineOr(m_Xor(L, R), m_Xor(R, L));
503 /// Compute known bits in 'V' under the assumption that the condition 'Cmp' is
504 /// true (at the context instruction.) This is mostly a utility function for
505 /// the prototype dominating conditions reasoning below.
506 static void computeKnownBitsFromTrueCondition(Value *V, ICmpInst *Cmp,
509 const DataLayout &DL,
510 unsigned Depth, const Query &Q) {
511 Value *LHS = Cmp->getOperand(0);
512 Value *RHS = Cmp->getOperand(1);
513 // TODO: We could potentially be more aggressive here. This would be worth
514 // evaluating. If we can, explore commoning this code with the assume
516 if (LHS != V && RHS != V)
519 const unsigned BitWidth = KnownZero.getBitWidth();
521 switch (Cmp->getPredicate()) {
523 // We know nothing from this condition
525 // TODO: implement unsigned bound from below (known one bits)
526 // TODO: common condition check implementations with assumes
527 // TODO: implement other patterns from assume (e.g. V & B == A)
528 case ICmpInst::ICMP_SGT:
530 APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0);
531 computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
532 if (KnownOneTemp.isAllOnesValue() || KnownZeroTemp.isNegative()) {
533 // We know that the sign bit is zero.
534 KnownZero |= APInt::getSignBit(BitWidth);
538 case ICmpInst::ICMP_EQ:
540 computeKnownBits(RHS, KnownZero, KnownOne, DL, Depth + 1, Q);
542 computeKnownBits(LHS, KnownZero, KnownOne, DL, Depth + 1, Q);
544 llvm_unreachable("missing use?");
546 case ICmpInst::ICMP_ULE:
548 APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0);
549 computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
550 // The known zero bits carry over
551 unsigned SignBits = KnownZeroTemp.countLeadingOnes();
552 KnownZero |= APInt::getHighBitsSet(BitWidth, SignBits);
555 case ICmpInst::ICMP_ULT:
557 APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0);
558 computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
559 // Whatever high bits in rhs are zero are known to be zero (if rhs is a
560 // power of 2, then one more).
561 unsigned SignBits = KnownZeroTemp.countLeadingOnes();
562 if (isKnownToBeAPowerOfTwo(RHS, false, Depth + 1, Query(Q, Cmp), DL))
564 KnownZero |= APInt::getHighBitsSet(BitWidth, SignBits);
570 /// Compute known bits in 'V' from conditions which are known to be true along
571 /// all paths leading to the context instruction. In particular, look for
572 /// cases where one branch of an interesting condition dominates the context
573 /// instruction. This does not do general dataflow.
574 /// NOTE: This code is EXPERIMENTAL and currently off by default.
575 static void computeKnownBitsFromDominatingCondition(Value *V, APInt &KnownZero,
577 const DataLayout &DL,
580 // Need both the dominator tree and the query location to do anything useful
581 if (!Q.DT || !Q.CxtI)
583 Instruction *Cxt = const_cast<Instruction *>(Q.CxtI);
585 // Avoid useless work
586 if (auto VI = dyn_cast<Instruction>(V))
587 if (VI->getParent() == Cxt->getParent())
590 // Note: We currently implement two options. It's not clear which of these
591 // will survive long term, we need data for that.
592 // Option 1 - Try walking the dominator tree looking for conditions which
593 // might apply. This works well for local conditions (loop guards, etc..),
594 // but not as well for things far from the context instruction (presuming a
595 // low max blocks explored). If we can set an high enough limit, this would
597 // Option 2 - We restrict out search to those conditions which are uses of
598 // the value we're interested in. This is independent of dom structure,
599 // but is slightly less powerful without looking through lots of use chains.
600 // It does handle conditions far from the context instruction (e.g. early
601 // function exits on entry) really well though.
603 // Option 1 - Search the dom tree
604 unsigned NumBlocksExplored = 0;
605 BasicBlock *Current = Cxt->getParent();
607 // Stop searching if we've gone too far up the chain
608 if (NumBlocksExplored >= DomConditionsMaxDomBlocks)
612 if (!Q.DT->getNode(Current)->getIDom())
614 Current = Q.DT->getNode(Current)->getIDom()->getBlock();
616 // found function entry
619 BranchInst *BI = dyn_cast<BranchInst>(Current->getTerminator());
620 if (!BI || BI->isUnconditional())
622 ICmpInst *Cmp = dyn_cast<ICmpInst>(BI->getCondition());
626 // We're looking for conditions that are guaranteed to hold at the context
627 // instruction. Finding a condition where one path dominates the context
628 // isn't enough because both the true and false cases could merge before
629 // the context instruction we're actually interested in. Instead, we need
630 // to ensure that the taken *edge* dominates the context instruction.
631 BasicBlock *BB0 = BI->getSuccessor(0);
632 BasicBlockEdge Edge(BI->getParent(), BB0);
633 if (!Edge.isSingleEdge() || !Q.DT->dominates(Edge, Q.CxtI->getParent()))
636 computeKnownBitsFromTrueCondition(V, Cmp, KnownZero, KnownOne, DL, Depth,
640 // Option 2 - Search the other uses of V
641 unsigned NumUsesExplored = 0;
642 for (auto U : V->users()) {
643 // Avoid massive lists
644 if (NumUsesExplored >= DomConditionsMaxUses)
647 // Consider only compare instructions uniquely controlling a branch
648 ICmpInst *Cmp = dyn_cast<ICmpInst>(U);
652 if (DomConditionsSingleCmpUse && !Cmp->hasOneUse())
655 for (auto *CmpU : Cmp->users()) {
656 BranchInst *BI = dyn_cast<BranchInst>(CmpU);
657 if (!BI || BI->isUnconditional())
659 // We're looking for conditions that are guaranteed to hold at the
660 // context instruction. Finding a condition where one path dominates
661 // the context isn't enough because both the true and false cases could
662 // merge before the context instruction we're actually interested in.
663 // Instead, we need to ensure that the taken *edge* dominates the context
665 BasicBlock *BB0 = BI->getSuccessor(0);
666 BasicBlockEdge Edge(BI->getParent(), BB0);
667 if (!Edge.isSingleEdge() || !Q.DT->dominates(Edge, Q.CxtI->getParent()))
670 computeKnownBitsFromTrueCondition(V, Cmp, KnownZero, KnownOne, DL, Depth,
676 static void computeKnownBitsFromAssume(Value *V, APInt &KnownZero,
677 APInt &KnownOne, const DataLayout &DL,
678 unsigned Depth, const Query &Q) {
679 // Use of assumptions is context-sensitive. If we don't have a context, we
681 if (!Q.AC || !Q.CxtI)
684 unsigned BitWidth = KnownZero.getBitWidth();
686 for (auto &AssumeVH : Q.AC->assumptions()) {
689 CallInst *I = cast<CallInst>(AssumeVH);
690 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
691 "Got assumption for the wrong function!");
692 if (Q.ExclInvs.count(I))
695 // Warning: This loop can end up being somewhat performance sensetive.
696 // We're running this loop for once for each value queried resulting in a
697 // runtime of ~O(#assumes * #values).
699 assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
700 "must be an assume intrinsic");
702 Value *Arg = I->getArgOperand(0);
704 if (Arg == V && isValidAssumeForContext(I, Q)) {
705 assert(BitWidth == 1 && "assume operand is not i1?");
706 KnownZero.clearAllBits();
707 KnownOne.setAllBits();
711 // The remaining tests are all recursive, so bail out if we hit the limit.
712 if (Depth == MaxDepth)
716 auto m_V = m_CombineOr(m_Specific(V),
717 m_CombineOr(m_PtrToInt(m_Specific(V)),
718 m_BitCast(m_Specific(V))));
720 CmpInst::Predicate Pred;
723 if (match(Arg, m_c_ICmp(Pred, m_V, m_Value(A))) &&
724 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
725 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
726 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
727 KnownZero |= RHSKnownZero;
728 KnownOne |= RHSKnownOne;
730 } else if (match(Arg,
731 m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A))) &&
732 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
733 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
734 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
735 APInt MaskKnownZero(BitWidth, 0), MaskKnownOne(BitWidth, 0);
736 computeKnownBits(B, MaskKnownZero, MaskKnownOne, DL, Depth+1, Query(Q, I));
738 // For those bits in the mask that are known to be one, we can propagate
739 // known bits from the RHS to V.
740 KnownZero |= RHSKnownZero & MaskKnownOne;
741 KnownOne |= RHSKnownOne & MaskKnownOne;
742 // assume(~(v & b) = a)
743 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))),
745 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
746 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
747 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
748 APInt MaskKnownZero(BitWidth, 0), MaskKnownOne(BitWidth, 0);
749 computeKnownBits(B, MaskKnownZero, MaskKnownOne, DL, Depth+1, Query(Q, I));
751 // For those bits in the mask that are known to be one, we can propagate
752 // inverted known bits from the RHS to V.
753 KnownZero |= RHSKnownOne & MaskKnownOne;
754 KnownOne |= RHSKnownZero & MaskKnownOne;
756 } else if (match(Arg,
757 m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A))) &&
758 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
759 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
760 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
761 APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
762 computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
764 // For those bits in B that are known to be zero, we can propagate known
765 // bits from the RHS to V.
766 KnownZero |= RHSKnownZero & BKnownZero;
767 KnownOne |= RHSKnownOne & BKnownZero;
768 // assume(~(v | b) = a)
769 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))),
771 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
772 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
773 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
774 APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
775 computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
777 // For those bits in B that are known to be zero, we can propagate
778 // inverted known bits from the RHS to V.
779 KnownZero |= RHSKnownOne & BKnownZero;
780 KnownOne |= RHSKnownZero & BKnownZero;
782 } else if (match(Arg,
783 m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A))) &&
784 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
785 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
786 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
787 APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
788 computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
790 // For those bits in B that are known to be zero, we can propagate known
791 // bits from the RHS to V. For those bits in B that are known to be one,
792 // we can propagate inverted known bits from the RHS to V.
793 KnownZero |= RHSKnownZero & BKnownZero;
794 KnownOne |= RHSKnownOne & BKnownZero;
795 KnownZero |= RHSKnownOne & BKnownOne;
796 KnownOne |= RHSKnownZero & BKnownOne;
797 // assume(~(v ^ b) = a)
798 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))),
800 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
801 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
802 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
803 APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
804 computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
806 // For those bits in B that are known to be zero, we can propagate
807 // inverted known bits from the RHS to V. For those bits in B that are
808 // known to be one, we can propagate known bits from the RHS to V.
809 KnownZero |= RHSKnownOne & BKnownZero;
810 KnownOne |= RHSKnownZero & BKnownZero;
811 KnownZero |= RHSKnownZero & BKnownOne;
812 KnownOne |= RHSKnownOne & BKnownOne;
813 // assume(v << c = a)
814 } else if (match(Arg, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)),
816 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
817 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
818 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
819 // For those bits in RHS that are known, we can propagate them to known
820 // bits in V shifted to the right by C.
821 KnownZero |= RHSKnownZero.lshr(C->getZExtValue());
822 KnownOne |= RHSKnownOne.lshr(C->getZExtValue());
823 // assume(~(v << c) = a)
824 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))),
826 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
827 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
828 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
829 // For those bits in RHS that are known, we can propagate them inverted
830 // to known bits in V shifted to the right by C.
831 KnownZero |= RHSKnownOne.lshr(C->getZExtValue());
832 KnownOne |= RHSKnownZero.lshr(C->getZExtValue());
833 // assume(v >> c = a)
834 } else if (match(Arg,
835 m_c_ICmp(Pred, m_CombineOr(m_LShr(m_V, m_ConstantInt(C)),
836 m_AShr(m_V, m_ConstantInt(C))),
838 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
839 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
840 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
841 // For those bits in RHS that are known, we can propagate them to known
842 // bits in V shifted to the right by C.
843 KnownZero |= RHSKnownZero << C->getZExtValue();
844 KnownOne |= RHSKnownOne << C->getZExtValue();
845 // assume(~(v >> c) = a)
846 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_CombineOr(
847 m_LShr(m_V, m_ConstantInt(C)),
848 m_AShr(m_V, m_ConstantInt(C)))),
850 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
851 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
852 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
853 // For those bits in RHS that are known, we can propagate them inverted
854 // to known bits in V shifted to the right by C.
855 KnownZero |= RHSKnownOne << C->getZExtValue();
856 KnownOne |= RHSKnownZero << C->getZExtValue();
857 // assume(v >=_s c) where c is non-negative
858 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
859 Pred == ICmpInst::ICMP_SGE && isValidAssumeForContext(I, Q)) {
860 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
861 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
863 if (RHSKnownZero.isNegative()) {
864 // We know that the sign bit is zero.
865 KnownZero |= APInt::getSignBit(BitWidth);
867 // assume(v >_s c) where c is at least -1.
868 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
869 Pred == ICmpInst::ICMP_SGT && isValidAssumeForContext(I, Q)) {
870 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
871 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
873 if (RHSKnownOne.isAllOnesValue() || RHSKnownZero.isNegative()) {
874 // We know that the sign bit is zero.
875 KnownZero |= APInt::getSignBit(BitWidth);
877 // assume(v <=_s c) where c is negative
878 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
879 Pred == ICmpInst::ICMP_SLE && isValidAssumeForContext(I, Q)) {
880 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
881 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
883 if (RHSKnownOne.isNegative()) {
884 // We know that the sign bit is one.
885 KnownOne |= APInt::getSignBit(BitWidth);
887 // assume(v <_s c) where c is non-positive
888 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
889 Pred == ICmpInst::ICMP_SLT && isValidAssumeForContext(I, Q)) {
890 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
891 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
893 if (RHSKnownZero.isAllOnesValue() || RHSKnownOne.isNegative()) {
894 // We know that the sign bit is one.
895 KnownOne |= APInt::getSignBit(BitWidth);
898 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
899 Pred == ICmpInst::ICMP_ULE && isValidAssumeForContext(I, Q)) {
900 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
901 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
903 // Whatever high bits in c are zero are known to be zero.
905 APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes());
907 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
908 Pred == ICmpInst::ICMP_ULT && isValidAssumeForContext(I, Q)) {
909 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
910 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
912 // Whatever high bits in c are zero are known to be zero (if c is a power
913 // of 2, then one more).
914 if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, Query(Q, I), DL))
916 APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes()+1);
919 APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes());
924 /// Determine which bits of V are known to be either zero or one and return
925 /// them in the KnownZero/KnownOne bit sets.
927 /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
928 /// we cannot optimize based on the assumption that it is zero without changing
929 /// it to be an explicit zero. If we don't change it to zero, other code could
930 /// optimized based on the contradictory assumption that it is non-zero.
931 /// Because instcombine aggressively folds operations with undef args anyway,
932 /// this won't lose us code quality.
934 /// This function is defined on values with integer type, values with pointer
935 /// type, and vectors of integers. In the case
936 /// where V is a vector, known zero, and known one values are the
937 /// same width as the vector element, and the bit is set only if it is true
938 /// for all of the elements in the vector.
939 void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
940 const DataLayout &DL, unsigned Depth, const Query &Q) {
941 assert(V && "No Value?");
942 assert(Depth <= MaxDepth && "Limit Search Depth");
943 unsigned BitWidth = KnownZero.getBitWidth();
945 assert((V->getType()->isIntOrIntVectorTy() ||
946 V->getType()->getScalarType()->isPointerTy()) &&
947 "Not integer or pointer type!");
948 assert((DL.getTypeSizeInBits(V->getType()->getScalarType()) == BitWidth) &&
949 (!V->getType()->isIntOrIntVectorTy() ||
950 V->getType()->getScalarSizeInBits() == BitWidth) &&
951 KnownZero.getBitWidth() == BitWidth &&
952 KnownOne.getBitWidth() == BitWidth &&
953 "V, KnownOne and KnownZero should have same BitWidth");
955 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
956 // We know all of the bits for a constant!
957 KnownOne = CI->getValue();
958 KnownZero = ~KnownOne;
961 // Null and aggregate-zero are all-zeros.
962 if (isa<ConstantPointerNull>(V) ||
963 isa<ConstantAggregateZero>(V)) {
964 KnownOne.clearAllBits();
965 KnownZero = APInt::getAllOnesValue(BitWidth);
968 // Handle a constant vector by taking the intersection of the known bits of
969 // each element. There is no real need to handle ConstantVector here, because
970 // we don't handle undef in any particularly useful way.
971 if (ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) {
972 // We know that CDS must be a vector of integers. Take the intersection of
974 KnownZero.setAllBits(); KnownOne.setAllBits();
975 APInt Elt(KnownZero.getBitWidth(), 0);
976 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
977 Elt = CDS->getElementAsInteger(i);
984 // The address of an aligned GlobalValue has trailing zeros.
985 if (auto *GO = dyn_cast<GlobalObject>(V)) {
986 unsigned Align = GO->getAlignment();
988 if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
989 Type *ObjectType = GVar->getType()->getElementType();
990 if (ObjectType->isSized()) {
991 // If the object is defined in the current Module, we'll be giving
992 // it the preferred alignment. Otherwise, we have to assume that it
993 // may only have the minimum ABI alignment.
994 if (!GVar->isDeclaration() && !GVar->isWeakForLinker())
995 Align = DL.getPreferredAlignment(GVar);
997 Align = DL.getABITypeAlignment(ObjectType);
1002 KnownZero = APInt::getLowBitsSet(BitWidth,
1003 countTrailingZeros(Align));
1005 KnownZero.clearAllBits();
1006 KnownOne.clearAllBits();
1010 if (Argument *A = dyn_cast<Argument>(V)) {
1011 unsigned Align = A->getType()->isPointerTy() ? A->getParamAlignment() : 0;
1013 if (!Align && A->hasStructRetAttr()) {
1014 // An sret parameter has at least the ABI alignment of the return type.
1015 Type *EltTy = cast<PointerType>(A->getType())->getElementType();
1016 if (EltTy->isSized())
1017 Align = DL.getABITypeAlignment(EltTy);
1021 KnownZero = APInt::getLowBitsSet(BitWidth, countTrailingZeros(Align));
1023 KnownZero.clearAllBits();
1024 KnownOne.clearAllBits();
1026 // Don't give up yet... there might be an assumption that provides more
1028 computeKnownBitsFromAssume(V, KnownZero, KnownOne, DL, Depth, Q);
1030 // Or a dominating condition for that matter
1031 if (EnableDomConditions && Depth <= DomConditionsMaxDepth)
1032 computeKnownBitsFromDominatingCondition(V, KnownZero, KnownOne, DL,
1037 // Start out not knowing anything.
1038 KnownZero.clearAllBits(); KnownOne.clearAllBits();
1040 // Limit search depth.
1041 // All recursive calls that increase depth must come after this.
1042 if (Depth == MaxDepth)
1045 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
1046 // the bits of its aliasee.
1047 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
1048 if (!GA->mayBeOverridden())
1049 computeKnownBits(GA->getAliasee(), KnownZero, KnownOne, DL, Depth + 1, Q);
1053 // Check whether a nearby assume intrinsic can determine some known bits.
1054 computeKnownBitsFromAssume(V, KnownZero, KnownOne, DL, Depth, Q);
1056 // Check whether there's a dominating condition which implies something about
1057 // this value at the given context.
1058 if (EnableDomConditions && Depth <= DomConditionsMaxDepth)
1059 computeKnownBitsFromDominatingCondition(V, KnownZero, KnownOne, DL, Depth,
1062 Operator *I = dyn_cast<Operator>(V);
1065 APInt KnownZero2(KnownZero), KnownOne2(KnownOne);
1066 switch (I->getOpcode()) {
1068 case Instruction::Load:
1069 if (MDNode *MD = cast<LoadInst>(I)->getMetadata(LLVMContext::MD_range))
1070 computeKnownBitsFromRangeMetadata(*MD, KnownZero);
1072 case Instruction::And: {
1073 // If either the LHS or the RHS are Zero, the result is zero.
1074 computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q);
1075 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
1077 // Output known-1 bits are only known if set in both the LHS & RHS.
1078 KnownOne &= KnownOne2;
1079 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1080 KnownZero |= KnownZero2;
1083 case Instruction::Or: {
1084 computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q);
1085 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
1087 // Output known-0 bits are only known if clear in both the LHS & RHS.
1088 KnownZero &= KnownZero2;
1089 // Output known-1 are known to be set if set in either the LHS | RHS.
1090 KnownOne |= KnownOne2;
1093 case Instruction::Xor: {
1094 computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q);
1095 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
1097 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1098 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1099 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1100 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1101 KnownZero = KnownZeroOut;
1104 case Instruction::Mul: {
1105 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
1106 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, KnownZero,
1107 KnownOne, KnownZero2, KnownOne2, DL, Depth, Q);
1110 case Instruction::UDiv: {
1111 // For the purposes of computing leading zeros we can conservatively
1112 // treat a udiv as a logical right shift by the power of 2 known to
1113 // be less than the denominator.
1114 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
1115 unsigned LeadZ = KnownZero2.countLeadingOnes();
1117 KnownOne2.clearAllBits();
1118 KnownZero2.clearAllBits();
1119 computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q);
1120 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1121 if (RHSUnknownLeadingOnes != BitWidth)
1122 LeadZ = std::min(BitWidth,
1123 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
1125 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
1128 case Instruction::Select:
1129 computeKnownBits(I->getOperand(2), KnownZero, KnownOne, DL, Depth + 1, Q);
1130 computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q);
1132 // Only known if known in both the LHS and RHS.
1133 KnownOne &= KnownOne2;
1134 KnownZero &= KnownZero2;
1136 case Instruction::FPTrunc:
1137 case Instruction::FPExt:
1138 case Instruction::FPToUI:
1139 case Instruction::FPToSI:
1140 case Instruction::SIToFP:
1141 case Instruction::UIToFP:
1142 break; // Can't work with floating point.
1143 case Instruction::PtrToInt:
1144 case Instruction::IntToPtr:
1145 case Instruction::AddrSpaceCast: // Pointers could be different sizes.
1146 // FALL THROUGH and handle them the same as zext/trunc.
1147 case Instruction::ZExt:
1148 case Instruction::Trunc: {
1149 Type *SrcTy = I->getOperand(0)->getType();
1151 unsigned SrcBitWidth;
1152 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1153 // which fall through here.
1154 SrcBitWidth = DL.getTypeSizeInBits(SrcTy->getScalarType());
1156 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1157 KnownZero = KnownZero.zextOrTrunc(SrcBitWidth);
1158 KnownOne = KnownOne.zextOrTrunc(SrcBitWidth);
1159 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1160 KnownZero = KnownZero.zextOrTrunc(BitWidth);
1161 KnownOne = KnownOne.zextOrTrunc(BitWidth);
1162 // Any top bits are known to be zero.
1163 if (BitWidth > SrcBitWidth)
1164 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
1167 case Instruction::BitCast: {
1168 Type *SrcTy = I->getOperand(0)->getType();
1169 if ((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
1170 // TODO: For now, not handling conversions like:
1171 // (bitcast i64 %x to <2 x i32>)
1172 !I->getType()->isVectorTy()) {
1173 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1178 case Instruction::SExt: {
1179 // Compute the bits in the result that are not present in the input.
1180 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1182 KnownZero = KnownZero.trunc(SrcBitWidth);
1183 KnownOne = KnownOne.trunc(SrcBitWidth);
1184 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1185 KnownZero = KnownZero.zext(BitWidth);
1186 KnownOne = KnownOne.zext(BitWidth);
1188 // If the sign bit of the input is known set or clear, then we know the
1189 // top bits of the result.
1190 if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero
1191 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
1192 else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set
1193 KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
1196 case Instruction::Shl:
1197 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1198 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1199 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
1200 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1201 KnownZero <<= ShiftAmt;
1202 KnownOne <<= ShiftAmt;
1203 KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0
1206 case Instruction::LShr:
1207 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1208 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1209 // Compute the new bits that are at the top now.
1210 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
1212 // Unsigned shift right.
1213 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1214 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
1215 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
1216 // high bits known zero.
1217 KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
1220 case Instruction::AShr:
1221 // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1222 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1223 // Compute the new bits that are at the top now.
1224 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
1226 // Signed shift right.
1227 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1228 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
1229 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
1231 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
1232 if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero.
1233 KnownZero |= HighBits;
1234 else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one.
1235 KnownOne |= HighBits;
1238 case Instruction::Sub: {
1239 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
1240 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW,
1241 KnownZero, KnownOne, KnownZero2, KnownOne2, DL,
1245 case Instruction::Add: {
1246 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
1247 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW,
1248 KnownZero, KnownOne, KnownZero2, KnownOne2, DL,
1252 case Instruction::SRem:
1253 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
1254 APInt RA = Rem->getValue().abs();
1255 if (RA.isPowerOf2()) {
1256 APInt LowBits = RA - 1;
1257 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1,
1260 // The low bits of the first operand are unchanged by the srem.
1261 KnownZero = KnownZero2 & LowBits;
1262 KnownOne = KnownOne2 & LowBits;
1264 // If the first operand is non-negative or has all low bits zero, then
1265 // the upper bits are all zero.
1266 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1267 KnownZero |= ~LowBits;
1269 // If the first operand is negative and not all low bits are zero, then
1270 // the upper bits are all one.
1271 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
1272 KnownOne |= ~LowBits;
1274 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1278 // The sign bit is the LHS's sign bit, except when the result of the
1279 // remainder is zero.
1280 if (KnownZero.isNonNegative()) {
1281 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
1282 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, DL,
1284 // If it's known zero, our sign bit is also zero.
1285 if (LHSKnownZero.isNegative())
1286 KnownZero.setBit(BitWidth - 1);
1290 case Instruction::URem: {
1291 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
1292 APInt RA = Rem->getValue();
1293 if (RA.isPowerOf2()) {
1294 APInt LowBits = (RA - 1);
1295 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1,
1297 KnownZero |= ~LowBits;
1298 KnownOne &= LowBits;
1303 // Since the result is less than or equal to either operand, any leading
1304 // zero bits in either operand must also exist in the result.
1305 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1306 computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q);
1308 unsigned Leaders = std::max(KnownZero.countLeadingOnes(),
1309 KnownZero2.countLeadingOnes());
1310 KnownOne.clearAllBits();
1311 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
1315 case Instruction::Alloca: {
1316 AllocaInst *AI = cast<AllocaInst>(V);
1317 unsigned Align = AI->getAlignment();
1319 Align = DL.getABITypeAlignment(AI->getType()->getElementType());
1322 KnownZero = APInt::getLowBitsSet(BitWidth, countTrailingZeros(Align));
1325 case Instruction::GetElementPtr: {
1326 // Analyze all of the subscripts of this getelementptr instruction
1327 // to determine if we can prove known low zero bits.
1328 APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0);
1329 computeKnownBits(I->getOperand(0), LocalKnownZero, LocalKnownOne, DL,
1331 unsigned TrailZ = LocalKnownZero.countTrailingOnes();
1333 gep_type_iterator GTI = gep_type_begin(I);
1334 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1335 Value *Index = I->getOperand(i);
1336 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
1337 // Handle struct member offset arithmetic.
1339 // Handle case when index is vector zeroinitializer
1340 Constant *CIndex = cast<Constant>(Index);
1341 if (CIndex->isZeroValue())
1344 if (CIndex->getType()->isVectorTy())
1345 Index = CIndex->getSplatValue();
1347 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1348 const StructLayout *SL = DL.getStructLayout(STy);
1349 uint64_t Offset = SL->getElementOffset(Idx);
1350 TrailZ = std::min<unsigned>(TrailZ,
1351 countTrailingZeros(Offset));
1353 // Handle array index arithmetic.
1354 Type *IndexedTy = GTI.getIndexedType();
1355 if (!IndexedTy->isSized()) {
1359 unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits();
1360 uint64_t TypeSize = DL.getTypeAllocSize(IndexedTy);
1361 LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0);
1362 computeKnownBits(Index, LocalKnownZero, LocalKnownOne, DL, Depth + 1,
1364 TrailZ = std::min(TrailZ,
1365 unsigned(countTrailingZeros(TypeSize) +
1366 LocalKnownZero.countTrailingOnes()));
1370 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ);
1373 case Instruction::PHI: {
1374 PHINode *P = cast<PHINode>(I);
1375 // Handle the case of a simple two-predecessor recurrence PHI.
1376 // There's a lot more that could theoretically be done here, but
1377 // this is sufficient to catch some interesting cases.
1378 if (P->getNumIncomingValues() == 2) {
1379 for (unsigned i = 0; i != 2; ++i) {
1380 Value *L = P->getIncomingValue(i);
1381 Value *R = P->getIncomingValue(!i);
1382 Operator *LU = dyn_cast<Operator>(L);
1385 unsigned Opcode = LU->getOpcode();
1386 // Check for operations that have the property that if
1387 // both their operands have low zero bits, the result
1388 // will have low zero bits.
1389 if (Opcode == Instruction::Add ||
1390 Opcode == Instruction::Sub ||
1391 Opcode == Instruction::And ||
1392 Opcode == Instruction::Or ||
1393 Opcode == Instruction::Mul) {
1394 Value *LL = LU->getOperand(0);
1395 Value *LR = LU->getOperand(1);
1396 // Find a recurrence.
1403 // Ok, we have a PHI of the form L op= R. Check for low
1405 computeKnownBits(R, KnownZero2, KnownOne2, DL, Depth + 1, Q);
1407 // We need to take the minimum number of known bits
1408 APInt KnownZero3(KnownZero), KnownOne3(KnownOne);
1409 computeKnownBits(L, KnownZero3, KnownOne3, DL, Depth + 1, Q);
1411 KnownZero = APInt::getLowBitsSet(BitWidth,
1412 std::min(KnownZero2.countTrailingOnes(),
1413 KnownZero3.countTrailingOnes()));
1419 // Unreachable blocks may have zero-operand PHI nodes.
1420 if (P->getNumIncomingValues() == 0)
1423 // Otherwise take the unions of the known bit sets of the operands,
1424 // taking conservative care to avoid excessive recursion.
1425 if (Depth < MaxDepth - 1 && !KnownZero && !KnownOne) {
1426 // Skip if every incoming value references to ourself.
1427 if (dyn_cast_or_null<UndefValue>(P->hasConstantValue()))
1430 KnownZero = APInt::getAllOnesValue(BitWidth);
1431 KnownOne = APInt::getAllOnesValue(BitWidth);
1432 for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) {
1433 // Skip direct self references.
1434 if (P->getIncomingValue(i) == P) continue;
1436 KnownZero2 = APInt(BitWidth, 0);
1437 KnownOne2 = APInt(BitWidth, 0);
1438 // Recurse, but cap the recursion to one level, because we don't
1439 // want to waste time spinning around in loops.
1440 computeKnownBits(P->getIncomingValue(i), KnownZero2, KnownOne2, DL,
1442 KnownZero &= KnownZero2;
1443 KnownOne &= KnownOne2;
1444 // If all bits have been ruled out, there's no need to check
1446 if (!KnownZero && !KnownOne)
1452 case Instruction::Call:
1453 case Instruction::Invoke:
1454 if (MDNode *MD = cast<Instruction>(I)->getMetadata(LLVMContext::MD_range))
1455 computeKnownBitsFromRangeMetadata(*MD, KnownZero);
1456 // If a range metadata is attached to this IntrinsicInst, intersect the
1457 // explicit range specified by the metadata and the implicit range of
1459 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1460 switch (II->getIntrinsicID()) {
1462 case Intrinsic::ctlz:
1463 case Intrinsic::cttz: {
1464 unsigned LowBits = Log2_32(BitWidth)+1;
1465 // If this call is undefined for 0, the result will be less than 2^n.
1466 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1468 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1471 case Intrinsic::ctpop: {
1472 unsigned LowBits = Log2_32(BitWidth)+1;
1473 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1476 case Intrinsic::x86_sse42_crc32_64_64:
1477 KnownZero |= APInt::getHighBitsSet(64, 32);
1482 case Instruction::ExtractValue:
1483 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
1484 ExtractValueInst *EVI = cast<ExtractValueInst>(I);
1485 if (EVI->getNumIndices() != 1) break;
1486 if (EVI->getIndices()[0] == 0) {
1487 switch (II->getIntrinsicID()) {
1489 case Intrinsic::uadd_with_overflow:
1490 case Intrinsic::sadd_with_overflow:
1491 computeKnownBitsAddSub(true, II->getArgOperand(0),
1492 II->getArgOperand(1), false, KnownZero,
1493 KnownOne, KnownZero2, KnownOne2, DL, Depth, Q);
1495 case Intrinsic::usub_with_overflow:
1496 case Intrinsic::ssub_with_overflow:
1497 computeKnownBitsAddSub(false, II->getArgOperand(0),
1498 II->getArgOperand(1), false, KnownZero,
1499 KnownOne, KnownZero2, KnownOne2, DL, Depth, Q);
1501 case Intrinsic::umul_with_overflow:
1502 case Intrinsic::smul_with_overflow:
1503 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
1504 KnownZero, KnownOne, KnownZero2, KnownOne2, DL,
1512 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1515 /// Determine whether the sign bit is known to be zero or one.
1516 /// Convenience wrapper around computeKnownBits.
1517 void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
1518 const DataLayout &DL, unsigned Depth, const Query &Q) {
1519 unsigned BitWidth = getBitWidth(V->getType(), DL);
1525 APInt ZeroBits(BitWidth, 0);
1526 APInt OneBits(BitWidth, 0);
1527 computeKnownBits(V, ZeroBits, OneBits, DL, Depth, Q);
1528 KnownOne = OneBits[BitWidth - 1];
1529 KnownZero = ZeroBits[BitWidth - 1];
1532 /// Return true if the given value is known to have exactly one
1533 /// bit set when defined. For vectors return true if every element is known to
1534 /// be a power of two when defined. Supports values with integer or pointer
1535 /// types and vectors of integers.
1536 bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero, unsigned Depth,
1537 const Query &Q, const DataLayout &DL) {
1538 if (Constant *C = dyn_cast<Constant>(V)) {
1539 if (C->isNullValue())
1541 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
1542 return CI->getValue().isPowerOf2();
1543 // TODO: Handle vector constants.
1546 // 1 << X is clearly a power of two if the one is not shifted off the end. If
1547 // it is shifted off the end then the result is undefined.
1548 if (match(V, m_Shl(m_One(), m_Value())))
1551 // (signbit) >>l X is clearly a power of two if the one is not shifted off the
1552 // bottom. If it is shifted off the bottom then the result is undefined.
1553 if (match(V, m_LShr(m_SignBit(), m_Value())))
1556 // The remaining tests are all recursive, so bail out if we hit the limit.
1557 if (Depth++ == MaxDepth)
1560 Value *X = nullptr, *Y = nullptr;
1561 // A shift of a power of two is a power of two or zero.
1562 if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) ||
1563 match(V, m_Shr(m_Value(X), m_Value()))))
1564 return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q, DL);
1566 if (ZExtInst *ZI = dyn_cast<ZExtInst>(V))
1567 return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q, DL);
1569 if (SelectInst *SI = dyn_cast<SelectInst>(V))
1570 return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q, DL) &&
1571 isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q, DL);
1573 if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) {
1574 // A power of two and'd with anything is a power of two or zero.
1575 if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q, DL) ||
1576 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q, DL))
1578 // X & (-X) is always a power of two or zero.
1579 if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X))))
1584 // Adding a power-of-two or zero to the same power-of-two or zero yields
1585 // either the original power-of-two, a larger power-of-two or zero.
1586 if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
1587 OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
1588 if (OrZero || VOBO->hasNoUnsignedWrap() || VOBO->hasNoSignedWrap()) {
1589 if (match(X, m_And(m_Specific(Y), m_Value())) ||
1590 match(X, m_And(m_Value(), m_Specific(Y))))
1591 if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q, DL))
1593 if (match(Y, m_And(m_Specific(X), m_Value())) ||
1594 match(Y, m_And(m_Value(), m_Specific(X))))
1595 if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q, DL))
1598 unsigned BitWidth = V->getType()->getScalarSizeInBits();
1599 APInt LHSZeroBits(BitWidth, 0), LHSOneBits(BitWidth, 0);
1600 computeKnownBits(X, LHSZeroBits, LHSOneBits, DL, Depth, Q);
1602 APInt RHSZeroBits(BitWidth, 0), RHSOneBits(BitWidth, 0);
1603 computeKnownBits(Y, RHSZeroBits, RHSOneBits, DL, Depth, Q);
1604 // If i8 V is a power of two or zero:
1605 // ZeroBits: 1 1 1 0 1 1 1 1
1606 // ~ZeroBits: 0 0 0 1 0 0 0 0
1607 if ((~(LHSZeroBits & RHSZeroBits)).isPowerOf2())
1608 // If OrZero isn't set, we cannot give back a zero result.
1609 // Make sure either the LHS or RHS has a bit set.
1610 if (OrZero || RHSOneBits.getBoolValue() || LHSOneBits.getBoolValue())
1615 // An exact divide or right shift can only shift off zero bits, so the result
1616 // is a power of two only if the first operand is a power of two and not
1617 // copying a sign bit (sdiv int_min, 2).
1618 if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) ||
1619 match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) {
1620 return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero,
1627 /// \brief Test whether a GEP's result is known to be non-null.
1629 /// Uses properties inherent in a GEP to try to determine whether it is known
1632 /// Currently this routine does not support vector GEPs.
1633 static bool isGEPKnownNonNull(GEPOperator *GEP, const DataLayout &DL,
1634 unsigned Depth, const Query &Q) {
1635 if (!GEP->isInBounds() || GEP->getPointerAddressSpace() != 0)
1638 // FIXME: Support vector-GEPs.
1639 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
1641 // If the base pointer is non-null, we cannot walk to a null address with an
1642 // inbounds GEP in address space zero.
1643 if (isKnownNonZero(GEP->getPointerOperand(), DL, Depth, Q))
1646 // Walk the GEP operands and see if any operand introduces a non-zero offset.
1647 // If so, then the GEP cannot produce a null pointer, as doing so would
1648 // inherently violate the inbounds contract within address space zero.
1649 for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
1650 GTI != GTE; ++GTI) {
1651 // Struct types are easy -- they must always be indexed by a constant.
1652 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
1653 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
1654 unsigned ElementIdx = OpC->getZExtValue();
1655 const StructLayout *SL = DL.getStructLayout(STy);
1656 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
1657 if (ElementOffset > 0)
1662 // If we have a zero-sized type, the index doesn't matter. Keep looping.
1663 if (DL.getTypeAllocSize(GTI.getIndexedType()) == 0)
1666 // Fast path the constant operand case both for efficiency and so we don't
1667 // increment Depth when just zipping down an all-constant GEP.
1668 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
1674 // We post-increment Depth here because while isKnownNonZero increments it
1675 // as well, when we pop back up that increment won't persist. We don't want
1676 // to recurse 10k times just because we have 10k GEP operands. We don't
1677 // bail completely out because we want to handle constant GEPs regardless
1679 if (Depth++ >= MaxDepth)
1682 if (isKnownNonZero(GTI.getOperand(), DL, Depth, Q))
1689 /// Does the 'Range' metadata (which must be a valid MD_range operand list)
1690 /// ensure that the value it's attached to is never Value? 'RangeType' is
1691 /// is the type of the value described by the range.
1692 static bool rangeMetadataExcludesValue(MDNode* Ranges,
1693 const APInt& Value) {
1694 const unsigned NumRanges = Ranges->getNumOperands() / 2;
1695 assert(NumRanges >= 1);
1696 for (unsigned i = 0; i < NumRanges; ++i) {
1697 ConstantInt *Lower =
1698 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
1699 ConstantInt *Upper =
1700 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
1701 ConstantRange Range(Lower->getValue(), Upper->getValue());
1702 if (Range.contains(Value))
1708 /// Return true if the given value is known to be non-zero when defined.
1709 /// For vectors return true if every element is known to be non-zero when
1710 /// defined. Supports values with integer or pointer type and vectors of
1712 bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth,
1714 if (Constant *C = dyn_cast<Constant>(V)) {
1715 if (C->isNullValue())
1717 if (isa<ConstantInt>(C))
1718 // Must be non-zero due to null test above.
1720 // TODO: Handle vectors
1724 if (Instruction* I = dyn_cast<Instruction>(V)) {
1725 if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range)) {
1726 // If the possible ranges don't contain zero, then the value is
1727 // definitely non-zero.
1728 if (IntegerType* Ty = dyn_cast<IntegerType>(V->getType())) {
1729 const APInt ZeroValue(Ty->getBitWidth(), 0);
1730 if (rangeMetadataExcludesValue(Ranges, ZeroValue))
1736 // The remaining tests are all recursive, so bail out if we hit the limit.
1737 if (Depth++ >= MaxDepth)
1740 // Check for pointer simplifications.
1741 if (V->getType()->isPointerTy()) {
1742 if (isKnownNonNull(V))
1744 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
1745 if (isGEPKnownNonNull(GEP, DL, Depth, Q))
1749 unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), DL);
1751 // X | Y != 0 if X != 0 or Y != 0.
1752 Value *X = nullptr, *Y = nullptr;
1753 if (match(V, m_Or(m_Value(X), m_Value(Y))))
1754 return isKnownNonZero(X, DL, Depth, Q) || isKnownNonZero(Y, DL, Depth, Q);
1756 // ext X != 0 if X != 0.
1757 if (isa<SExtInst>(V) || isa<ZExtInst>(V))
1758 return isKnownNonZero(cast<Instruction>(V)->getOperand(0), DL, Depth, Q);
1760 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
1761 // if the lowest bit is shifted off the end.
1762 if (BitWidth && match(V, m_Shl(m_Value(X), m_Value(Y)))) {
1763 // shl nuw can't remove any non-zero bits.
1764 OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
1765 if (BO->hasNoUnsignedWrap())
1766 return isKnownNonZero(X, DL, Depth, Q);
1768 APInt KnownZero(BitWidth, 0);
1769 APInt KnownOne(BitWidth, 0);
1770 computeKnownBits(X, KnownZero, KnownOne, DL, Depth, Q);
1774 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
1775 // defined if the sign bit is shifted off the end.
1776 else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) {
1777 // shr exact can only shift out zero bits.
1778 PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V);
1780 return isKnownNonZero(X, DL, Depth, Q);
1782 bool XKnownNonNegative, XKnownNegative;
1783 ComputeSignBit(X, XKnownNonNegative, XKnownNegative, DL, Depth, Q);
1787 // div exact can only produce a zero if the dividend is zero.
1788 else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) {
1789 return isKnownNonZero(X, DL, Depth, Q);
1792 else if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
1793 bool XKnownNonNegative, XKnownNegative;
1794 bool YKnownNonNegative, YKnownNegative;
1795 ComputeSignBit(X, XKnownNonNegative, XKnownNegative, DL, Depth, Q);
1796 ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, DL, Depth, Q);
1798 // If X and Y are both non-negative (as signed values) then their sum is not
1799 // zero unless both X and Y are zero.
1800 if (XKnownNonNegative && YKnownNonNegative)
1801 if (isKnownNonZero(X, DL, Depth, Q) || isKnownNonZero(Y, DL, Depth, Q))
1804 // If X and Y are both negative (as signed values) then their sum is not
1805 // zero unless both X and Y equal INT_MIN.
1806 if (BitWidth && XKnownNegative && YKnownNegative) {
1807 APInt KnownZero(BitWidth, 0);
1808 APInt KnownOne(BitWidth, 0);
1809 APInt Mask = APInt::getSignedMaxValue(BitWidth);
1810 // The sign bit of X is set. If some other bit is set then X is not equal
1812 computeKnownBits(X, KnownZero, KnownOne, DL, Depth, Q);
1813 if ((KnownOne & Mask) != 0)
1815 // The sign bit of Y is set. If some other bit is set then Y is not equal
1817 computeKnownBits(Y, KnownZero, KnownOne, DL, Depth, Q);
1818 if ((KnownOne & Mask) != 0)
1822 // The sum of a non-negative number and a power of two is not zero.
1823 if (XKnownNonNegative &&
1824 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q, DL))
1826 if (YKnownNonNegative &&
1827 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q, DL))
1831 else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) {
1832 OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
1833 // If X and Y are non-zero then so is X * Y as long as the multiplication
1834 // does not overflow.
1835 if ((BO->hasNoSignedWrap() || BO->hasNoUnsignedWrap()) &&
1836 isKnownNonZero(X, DL, Depth, Q) && isKnownNonZero(Y, DL, Depth, Q))
1839 // (C ? X : Y) != 0 if X != 0 and Y != 0.
1840 else if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
1841 if (isKnownNonZero(SI->getTrueValue(), DL, Depth, Q) &&
1842 isKnownNonZero(SI->getFalseValue(), DL, Depth, Q))
1846 if (!BitWidth) return false;
1847 APInt KnownZero(BitWidth, 0);
1848 APInt KnownOne(BitWidth, 0);
1849 computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q);
1850 return KnownOne != 0;
1853 /// Return true if 'V & Mask' is known to be zero. We use this predicate to
1854 /// simplify operations downstream. Mask is known to be zero for bits that V
1857 /// This function is defined on values with integer type, values with pointer
1858 /// type, and vectors of integers. In the case
1859 /// where V is a vector, the mask, known zero, and known one values are the
1860 /// same width as the vector element, and the bit is set only if it is true
1861 /// for all of the elements in the vector.
1862 bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
1863 unsigned Depth, const Query &Q) {
1864 APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0);
1865 computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q);
1866 return (KnownZero & Mask) == Mask;
1871 /// Return the number of times the sign bit of the register is replicated into
1872 /// the other bits. We know that at least 1 bit is always equal to the sign bit
1873 /// (itself), but other cases can give us information. For example, immediately
1874 /// after an "ashr X, 2", we know that the top 3 bits are all equal to each
1875 /// other, so we return 3.
1877 /// 'Op' must have a scalar integer type.
1879 unsigned ComputeNumSignBits(Value *V, const DataLayout &DL, unsigned Depth,
1881 unsigned TyBits = DL.getTypeSizeInBits(V->getType()->getScalarType());
1883 unsigned FirstAnswer = 1;
1885 // Note that ConstantInt is handled by the general computeKnownBits case
1889 return 1; // Limit search depth.
1891 Operator *U = dyn_cast<Operator>(V);
1892 switch (Operator::getOpcode(V)) {
1894 case Instruction::SExt:
1895 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
1896 return ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q) + Tmp;
1898 case Instruction::SDiv: {
1899 const APInt *Denominator;
1900 // sdiv X, C -> adds log(C) sign bits.
1901 if (match(U->getOperand(1), m_APInt(Denominator))) {
1903 // Ignore non-positive denominator.
1904 if (!Denominator->isStrictlyPositive())
1907 // Calculate the incoming numerator bits.
1908 unsigned NumBits = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
1910 // Add floor(log(C)) bits to the numerator bits.
1911 return std::min(TyBits, NumBits + Denominator->logBase2());
1916 case Instruction::SRem: {
1917 const APInt *Denominator;
1918 // srem X, C -> we know that the result is within [-C+1,C) when C is a
1919 // positive constant. This let us put a lower bound on the number of sign
1921 if (match(U->getOperand(1), m_APInt(Denominator))) {
1923 // Ignore non-positive denominator.
1924 if (!Denominator->isStrictlyPositive())
1927 // Calculate the incoming numerator bits. SRem by a positive constant
1928 // can't lower the number of sign bits.
1930 ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
1932 // Calculate the leading sign bit constraints by examining the
1933 // denominator. Given that the denominator is positive, there are two
1936 // 1. the numerator is positive. The result range is [0,C) and [0,C) u<
1937 // (1 << ceilLogBase2(C)).
1939 // 2. the numerator is negative. Then the result range is (-C,0] and
1940 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
1942 // Thus a lower bound on the number of sign bits is `TyBits -
1943 // ceilLogBase2(C)`.
1945 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
1946 return std::max(NumrBits, ResBits);
1951 case Instruction::AShr: {
1952 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
1953 // ashr X, C -> adds C sign bits. Vectors too.
1955 if (match(U->getOperand(1), m_APInt(ShAmt))) {
1956 Tmp += ShAmt->getZExtValue();
1957 if (Tmp > TyBits) Tmp = TyBits;
1961 case Instruction::Shl: {
1963 if (match(U->getOperand(1), m_APInt(ShAmt))) {
1964 // shl destroys sign bits.
1965 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
1966 Tmp2 = ShAmt->getZExtValue();
1967 if (Tmp2 >= TyBits || // Bad shift.
1968 Tmp2 >= Tmp) break; // Shifted all sign bits out.
1973 case Instruction::And:
1974 case Instruction::Or:
1975 case Instruction::Xor: // NOT is handled here.
1976 // Logical binary ops preserve the number of sign bits at the worst.
1977 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
1979 Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
1980 FirstAnswer = std::min(Tmp, Tmp2);
1981 // We computed what we know about the sign bits as our first
1982 // answer. Now proceed to the generic code that uses
1983 // computeKnownBits, and pick whichever answer is better.
1987 case Instruction::Select:
1988 Tmp = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
1989 if (Tmp == 1) return 1; // Early out.
1990 Tmp2 = ComputeNumSignBits(U->getOperand(2), DL, Depth + 1, Q);
1991 return std::min(Tmp, Tmp2);
1993 case Instruction::Add:
1994 // Add can have at most one carry bit. Thus we know that the output
1995 // is, at worst, one more bit than the inputs.
1996 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
1997 if (Tmp == 1) return 1; // Early out.
1999 // Special case decrementing a value (ADD X, -1):
2000 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
2001 if (CRHS->isAllOnesValue()) {
2002 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
2003 computeKnownBits(U->getOperand(0), KnownZero, KnownOne, DL, Depth + 1,
2006 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2008 if ((KnownZero | APInt(TyBits, 1)).isAllOnesValue())
2011 // If we are subtracting one from a positive number, there is no carry
2012 // out of the result.
2013 if (KnownZero.isNegative())
2017 Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
2018 if (Tmp2 == 1) return 1;
2019 return std::min(Tmp, Tmp2)-1;
2021 case Instruction::Sub:
2022 Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
2023 if (Tmp2 == 1) return 1;
2026 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
2027 if (CLHS->isNullValue()) {
2028 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
2029 computeKnownBits(U->getOperand(1), KnownZero, KnownOne, DL, Depth + 1,
2031 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2033 if ((KnownZero | APInt(TyBits, 1)).isAllOnesValue())
2036 // If the input is known to be positive (the sign bit is known clear),
2037 // the output of the NEG has the same number of sign bits as the input.
2038 if (KnownZero.isNegative())
2041 // Otherwise, we treat this like a SUB.
2044 // Sub can have at most one carry bit. Thus we know that the output
2045 // is, at worst, one more bit than the inputs.
2046 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
2047 if (Tmp == 1) return 1; // Early out.
2048 return std::min(Tmp, Tmp2)-1;
2050 case Instruction::PHI: {
2051 PHINode *PN = cast<PHINode>(U);
2052 unsigned NumIncomingValues = PN->getNumIncomingValues();
2053 // Don't analyze large in-degree PHIs.
2054 if (NumIncomingValues > 4) break;
2055 // Unreachable blocks may have zero-operand PHI nodes.
2056 if (NumIncomingValues == 0) break;
2058 // Take the minimum of all incoming values. This can't infinitely loop
2059 // because of our depth threshold.
2060 Tmp = ComputeNumSignBits(PN->getIncomingValue(0), DL, Depth + 1, Q);
2061 for (unsigned i = 1, e = NumIncomingValues; i != e; ++i) {
2062 if (Tmp == 1) return Tmp;
2064 Tmp, ComputeNumSignBits(PN->getIncomingValue(i), DL, Depth + 1, Q));
2069 case Instruction::Trunc:
2070 // FIXME: it's tricky to do anything useful for this, but it is an important
2071 // case for targets like X86.
2075 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2076 // use this information.
2077 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
2079 computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q);
2081 if (KnownZero.isNegative()) { // sign bit is 0
2083 } else if (KnownOne.isNegative()) { // sign bit is 1;
2090 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2091 // the number of identical bits in the top of the input value.
2093 Mask <<= Mask.getBitWidth()-TyBits;
2094 // Return # leading zeros. We use 'min' here in case Val was zero before
2095 // shifting. We don't want to return '64' as for an i32 "0".
2096 return std::max(FirstAnswer, std::min(TyBits, Mask.countLeadingZeros()));
2099 /// This function computes the integer multiple of Base that equals V.
2100 /// If successful, it returns true and returns the multiple in
2101 /// Multiple. If unsuccessful, it returns false. It looks
2102 /// through SExt instructions only if LookThroughSExt is true.
2103 bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
2104 bool LookThroughSExt, unsigned Depth) {
2105 const unsigned MaxDepth = 6;
2107 assert(V && "No Value?");
2108 assert(Depth <= MaxDepth && "Limit Search Depth");
2109 assert(V->getType()->isIntegerTy() && "Not integer or pointer type!");
2111 Type *T = V->getType();
2113 ConstantInt *CI = dyn_cast<ConstantInt>(V);
2123 ConstantExpr *CO = dyn_cast<ConstantExpr>(V);
2124 Constant *BaseVal = ConstantInt::get(T, Base);
2125 if (CO && CO == BaseVal) {
2127 Multiple = ConstantInt::get(T, 1);
2131 if (CI && CI->getZExtValue() % Base == 0) {
2132 Multiple = ConstantInt::get(T, CI->getZExtValue() / Base);
2136 if (Depth == MaxDepth) return false; // Limit search depth.
2138 Operator *I = dyn_cast<Operator>(V);
2139 if (!I) return false;
2141 switch (I->getOpcode()) {
2143 case Instruction::SExt:
2144 if (!LookThroughSExt) return false;
2145 // otherwise fall through to ZExt
2146 case Instruction::ZExt:
2147 return ComputeMultiple(I->getOperand(0), Base, Multiple,
2148 LookThroughSExt, Depth+1);
2149 case Instruction::Shl:
2150 case Instruction::Mul: {
2151 Value *Op0 = I->getOperand(0);
2152 Value *Op1 = I->getOperand(1);
2154 if (I->getOpcode() == Instruction::Shl) {
2155 ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1);
2156 if (!Op1CI) return false;
2157 // Turn Op0 << Op1 into Op0 * 2^Op1
2158 APInt Op1Int = Op1CI->getValue();
2159 uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
2160 APInt API(Op1Int.getBitWidth(), 0);
2161 API.setBit(BitToSet);
2162 Op1 = ConstantInt::get(V->getContext(), API);
2165 Value *Mul0 = nullptr;
2166 if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) {
2167 if (Constant *Op1C = dyn_cast<Constant>(Op1))
2168 if (Constant *MulC = dyn_cast<Constant>(Mul0)) {
2169 if (Op1C->getType()->getPrimitiveSizeInBits() <
2170 MulC->getType()->getPrimitiveSizeInBits())
2171 Op1C = ConstantExpr::getZExt(Op1C, MulC->getType());
2172 if (Op1C->getType()->getPrimitiveSizeInBits() >
2173 MulC->getType()->getPrimitiveSizeInBits())
2174 MulC = ConstantExpr::getZExt(MulC, Op1C->getType());
2176 // V == Base * (Mul0 * Op1), so return (Mul0 * Op1)
2177 Multiple = ConstantExpr::getMul(MulC, Op1C);
2181 if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0))
2182 if (Mul0CI->getValue() == 1) {
2183 // V == Base * Op1, so return Op1
2189 Value *Mul1 = nullptr;
2190 if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) {
2191 if (Constant *Op0C = dyn_cast<Constant>(Op0))
2192 if (Constant *MulC = dyn_cast<Constant>(Mul1)) {
2193 if (Op0C->getType()->getPrimitiveSizeInBits() <
2194 MulC->getType()->getPrimitiveSizeInBits())
2195 Op0C = ConstantExpr::getZExt(Op0C, MulC->getType());
2196 if (Op0C->getType()->getPrimitiveSizeInBits() >
2197 MulC->getType()->getPrimitiveSizeInBits())
2198 MulC = ConstantExpr::getZExt(MulC, Op0C->getType());
2200 // V == Base * (Mul1 * Op0), so return (Mul1 * Op0)
2201 Multiple = ConstantExpr::getMul(MulC, Op0C);
2205 if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1))
2206 if (Mul1CI->getValue() == 1) {
2207 // V == Base * Op0, so return Op0
2215 // We could not determine if V is a multiple of Base.
2219 /// Return true if we can prove that the specified FP value is never equal to
2222 /// NOTE: this function will need to be revisited when we support non-default
2225 bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) {
2226 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
2227 return !CFP->getValueAPF().isNegZero();
2229 // FIXME: Magic number! At the least, this should be given a name because it's
2230 // used similarly in CannotBeOrderedLessThanZero(). A better fix may be to
2231 // expose it as a parameter, so it can be used for testing / experimenting.
2233 return false; // Limit search depth.
2235 const Operator *I = dyn_cast<Operator>(V);
2236 if (!I) return false;
2238 // Check if the nsz fast-math flag is set
2239 if (const FPMathOperator *FPO = dyn_cast<FPMathOperator>(I))
2240 if (FPO->hasNoSignedZeros())
2243 // (add x, 0.0) is guaranteed to return +0.0, not -0.0.
2244 if (I->getOpcode() == Instruction::FAdd)
2245 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I->getOperand(1)))
2246 if (CFP->isNullValue())
2249 // sitofp and uitofp turn into +0.0 for zero.
2250 if (isa<SIToFPInst>(I) || isa<UIToFPInst>(I))
2253 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2254 // sqrt(-0.0) = -0.0, no other negative results are possible.
2255 if (II->getIntrinsicID() == Intrinsic::sqrt)
2256 return CannotBeNegativeZero(II->getArgOperand(0), Depth+1);
2258 if (const CallInst *CI = dyn_cast<CallInst>(I))
2259 if (const Function *F = CI->getCalledFunction()) {
2260 if (F->isDeclaration()) {
2262 if (F->getName() == "abs") return true;
2263 // fabs[lf](x) != -0.0
2264 if (F->getName() == "fabs") return true;
2265 if (F->getName() == "fabsf") return true;
2266 if (F->getName() == "fabsl") return true;
2267 if (F->getName() == "sqrt" || F->getName() == "sqrtf" ||
2268 F->getName() == "sqrtl")
2269 return CannotBeNegativeZero(CI->getArgOperand(0), Depth+1);
2276 bool llvm::CannotBeOrderedLessThanZero(const Value *V, unsigned Depth) {
2277 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
2278 return !CFP->getValueAPF().isNegative() || CFP->getValueAPF().isZero();
2280 // FIXME: Magic number! At the least, this should be given a name because it's
2281 // used similarly in CannotBeNegativeZero(). A better fix may be to
2282 // expose it as a parameter, so it can be used for testing / experimenting.
2284 return false; // Limit search depth.
2286 const Operator *I = dyn_cast<Operator>(V);
2287 if (!I) return false;
2289 switch (I->getOpcode()) {
2291 case Instruction::FMul:
2292 // x*x is always non-negative or a NaN.
2293 if (I->getOperand(0) == I->getOperand(1))
2296 case Instruction::FAdd:
2297 case Instruction::FDiv:
2298 case Instruction::FRem:
2299 return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1) &&
2300 CannotBeOrderedLessThanZero(I->getOperand(1), Depth+1);
2301 case Instruction::FPExt:
2302 case Instruction::FPTrunc:
2303 // Widening/narrowing never change sign.
2304 return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1);
2305 case Instruction::Call:
2306 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2307 switch (II->getIntrinsicID()) {
2309 case Intrinsic::exp:
2310 case Intrinsic::exp2:
2311 case Intrinsic::fabs:
2312 case Intrinsic::sqrt:
2314 case Intrinsic::powi:
2315 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
2316 // powi(x,n) is non-negative if n is even.
2317 if (CI->getBitWidth() <= 64 && CI->getSExtValue() % 2u == 0)
2320 return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1);
2321 case Intrinsic::fma:
2322 case Intrinsic::fmuladd:
2323 // x*x+y is non-negative if y is non-negative.
2324 return I->getOperand(0) == I->getOperand(1) &&
2325 CannotBeOrderedLessThanZero(I->getOperand(2), Depth+1);
2332 /// If the specified value can be set by repeating the same byte in memory,
2333 /// return the i8 value that it is represented with. This is
2334 /// true for all i8 values obviously, but is also true for i32 0, i32 -1,
2335 /// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated
2336 /// byte store (e.g. i16 0x1234), return null.
2337 Value *llvm::isBytewiseValue(Value *V) {
2338 // All byte-wide stores are splatable, even of arbitrary variables.
2339 if (V->getType()->isIntegerTy(8)) return V;
2341 // Handle 'null' ConstantArrayZero etc.
2342 if (Constant *C = dyn_cast<Constant>(V))
2343 if (C->isNullValue())
2344 return Constant::getNullValue(Type::getInt8Ty(V->getContext()));
2346 // Constant float and double values can be handled as integer values if the
2347 // corresponding integer value is "byteable". An important case is 0.0.
2348 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
2349 if (CFP->getType()->isFloatTy())
2350 V = ConstantExpr::getBitCast(CFP, Type::getInt32Ty(V->getContext()));
2351 if (CFP->getType()->isDoubleTy())
2352 V = ConstantExpr::getBitCast(CFP, Type::getInt64Ty(V->getContext()));
2353 // Don't handle long double formats, which have strange constraints.
2356 // We can handle constant integers that are multiple of 8 bits.
2357 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
2358 if (CI->getBitWidth() % 8 == 0) {
2359 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
2361 if (!CI->getValue().isSplat(8))
2363 return ConstantInt::get(V->getContext(), CI->getValue().trunc(8));
2367 // A ConstantDataArray/Vector is splatable if all its members are equal and
2369 if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(V)) {
2370 Value *Elt = CA->getElementAsConstant(0);
2371 Value *Val = isBytewiseValue(Elt);
2375 for (unsigned I = 1, E = CA->getNumElements(); I != E; ++I)
2376 if (CA->getElementAsConstant(I) != Elt)
2382 // Conceptually, we could handle things like:
2383 // %a = zext i8 %X to i16
2384 // %b = shl i16 %a, 8
2385 // %c = or i16 %a, %b
2386 // but until there is an example that actually needs this, it doesn't seem
2387 // worth worrying about.
2392 // This is the recursive version of BuildSubAggregate. It takes a few different
2393 // arguments. Idxs is the index within the nested struct From that we are
2394 // looking at now (which is of type IndexedType). IdxSkip is the number of
2395 // indices from Idxs that should be left out when inserting into the resulting
2396 // struct. To is the result struct built so far, new insertvalue instructions
2398 static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType,
2399 SmallVectorImpl<unsigned> &Idxs,
2401 Instruction *InsertBefore) {
2402 llvm::StructType *STy = dyn_cast<llvm::StructType>(IndexedType);
2404 // Save the original To argument so we can modify it
2406 // General case, the type indexed by Idxs is a struct
2407 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
2408 // Process each struct element recursively
2411 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
2415 // Couldn't find any inserted value for this index? Cleanup
2416 while (PrevTo != OrigTo) {
2417 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
2418 PrevTo = Del->getAggregateOperand();
2419 Del->eraseFromParent();
2421 // Stop processing elements
2425 // If we successfully found a value for each of our subaggregates
2429 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
2430 // the struct's elements had a value that was inserted directly. In the latter
2431 // case, perhaps we can't determine each of the subelements individually, but
2432 // we might be able to find the complete struct somewhere.
2434 // Find the value that is at that particular spot
2435 Value *V = FindInsertedValue(From, Idxs);
2440 // Insert the value in the new (sub) aggregrate
2441 return llvm::InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip),
2442 "tmp", InsertBefore);
2445 // This helper takes a nested struct and extracts a part of it (which is again a
2446 // struct) into a new value. For example, given the struct:
2447 // { a, { b, { c, d }, e } }
2448 // and the indices "1, 1" this returns
2451 // It does this by inserting an insertvalue for each element in the resulting
2452 // struct, as opposed to just inserting a single struct. This will only work if
2453 // each of the elements of the substruct are known (ie, inserted into From by an
2454 // insertvalue instruction somewhere).
2456 // All inserted insertvalue instructions are inserted before InsertBefore
2457 static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range,
2458 Instruction *InsertBefore) {
2459 assert(InsertBefore && "Must have someplace to insert!");
2460 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
2462 Value *To = UndefValue::get(IndexedType);
2463 SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end());
2464 unsigned IdxSkip = Idxs.size();
2466 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
2469 /// Given an aggregrate and an sequence of indices, see if
2470 /// the scalar value indexed is already around as a register, for example if it
2471 /// were inserted directly into the aggregrate.
2473 /// If InsertBefore is not null, this function will duplicate (modified)
2474 /// insertvalues when a part of a nested struct is extracted.
2475 Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range,
2476 Instruction *InsertBefore) {
2477 // Nothing to index? Just return V then (this is useful at the end of our
2479 if (idx_range.empty())
2481 // We have indices, so V should have an indexable type.
2482 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
2483 "Not looking at a struct or array?");
2484 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
2485 "Invalid indices for type?");
2487 if (Constant *C = dyn_cast<Constant>(V)) {
2488 C = C->getAggregateElement(idx_range[0]);
2489 if (!C) return nullptr;
2490 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
2493 if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
2494 // Loop the indices for the insertvalue instruction in parallel with the
2495 // requested indices
2496 const unsigned *req_idx = idx_range.begin();
2497 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
2498 i != e; ++i, ++req_idx) {
2499 if (req_idx == idx_range.end()) {
2500 // We can't handle this without inserting insertvalues
2504 // The requested index identifies a part of a nested aggregate. Handle
2505 // this specially. For example,
2506 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
2507 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
2508 // %C = extractvalue {i32, { i32, i32 } } %B, 1
2509 // This can be changed into
2510 // %A = insertvalue {i32, i32 } undef, i32 10, 0
2511 // %C = insertvalue {i32, i32 } %A, i32 11, 1
2512 // which allows the unused 0,0 element from the nested struct to be
2514 return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx),
2518 // This insert value inserts something else than what we are looking for.
2519 // See if the (aggregrate) value inserted into has the value we are
2520 // looking for, then.
2522 return FindInsertedValue(I->getAggregateOperand(), idx_range,
2525 // If we end up here, the indices of the insertvalue match with those
2526 // requested (though possibly only partially). Now we recursively look at
2527 // the inserted value, passing any remaining indices.
2528 return FindInsertedValue(I->getInsertedValueOperand(),
2529 makeArrayRef(req_idx, idx_range.end()),
2533 if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
2534 // If we're extracting a value from an aggregrate that was extracted from
2535 // something else, we can extract from that something else directly instead.
2536 // However, we will need to chain I's indices with the requested indices.
2538 // Calculate the number of indices required
2539 unsigned size = I->getNumIndices() + idx_range.size();
2540 // Allocate some space to put the new indices in
2541 SmallVector<unsigned, 5> Idxs;
2543 // Add indices from the extract value instruction
2544 Idxs.append(I->idx_begin(), I->idx_end());
2546 // Add requested indices
2547 Idxs.append(idx_range.begin(), idx_range.end());
2549 assert(Idxs.size() == size
2550 && "Number of indices added not correct?");
2552 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
2554 // Otherwise, we don't know (such as, extracting from a function return value
2555 // or load instruction)
2559 /// Analyze the specified pointer to see if it can be expressed as a base
2560 /// pointer plus a constant offset. Return the base and offset to the caller.
2561 Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
2562 const DataLayout &DL) {
2563 unsigned BitWidth = DL.getPointerTypeSizeInBits(Ptr->getType());
2564 APInt ByteOffset(BitWidth, 0);
2566 if (Ptr->getType()->isVectorTy())
2569 if (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
2570 APInt GEPOffset(BitWidth, 0);
2571 if (!GEP->accumulateConstantOffset(DL, GEPOffset))
2574 ByteOffset += GEPOffset;
2576 Ptr = GEP->getPointerOperand();
2577 } else if (Operator::getOpcode(Ptr) == Instruction::BitCast ||
2578 Operator::getOpcode(Ptr) == Instruction::AddrSpaceCast) {
2579 Ptr = cast<Operator>(Ptr)->getOperand(0);
2580 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) {
2581 if (GA->mayBeOverridden())
2583 Ptr = GA->getAliasee();
2588 Offset = ByteOffset.getSExtValue();
2593 /// This function computes the length of a null-terminated C string pointed to
2594 /// by V. If successful, it returns true and returns the string in Str.
2595 /// If unsuccessful, it returns false.
2596 bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
2597 uint64_t Offset, bool TrimAtNul) {
2600 // Look through bitcast instructions and geps.
2601 V = V->stripPointerCasts();
2603 // If the value is a GEP instruction or constant expression, treat it as an
2605 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
2606 // Make sure the GEP has exactly three arguments.
2607 if (GEP->getNumOperands() != 3)
2610 // Make sure the index-ee is a pointer to array of i8.
2611 PointerType *PT = cast<PointerType>(GEP->getOperand(0)->getType());
2612 ArrayType *AT = dyn_cast<ArrayType>(PT->getElementType());
2613 if (!AT || !AT->getElementType()->isIntegerTy(8))
2616 // Check to make sure that the first operand of the GEP is an integer and
2617 // has value 0 so that we are sure we're indexing into the initializer.
2618 const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
2619 if (!FirstIdx || !FirstIdx->isZero())
2622 // If the second index isn't a ConstantInt, then this is a variable index
2623 // into the array. If this occurs, we can't say anything meaningful about
2625 uint64_t StartIdx = 0;
2626 if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
2627 StartIdx = CI->getZExtValue();
2630 return getConstantStringInfo(GEP->getOperand(0), Str, StartIdx + Offset,
2634 // The GEP instruction, constant or instruction, must reference a global
2635 // variable that is a constant and is initialized. The referenced constant
2636 // initializer is the array that we'll use for optimization.
2637 const GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
2638 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
2641 // Handle the all-zeros case
2642 if (GV->getInitializer()->isNullValue()) {
2643 // This is a degenerate case. The initializer is constant zero so the
2644 // length of the string must be zero.
2649 // Must be a Constant Array
2650 const ConstantDataArray *Array =
2651 dyn_cast<ConstantDataArray>(GV->getInitializer());
2652 if (!Array || !Array->isString())
2655 // Get the number of elements in the array
2656 uint64_t NumElts = Array->getType()->getArrayNumElements();
2658 // Start out with the entire array in the StringRef.
2659 Str = Array->getAsString();
2661 if (Offset > NumElts)
2664 // Skip over 'offset' bytes.
2665 Str = Str.substr(Offset);
2668 // Trim off the \0 and anything after it. If the array is not nul
2669 // terminated, we just return the whole end of string. The client may know
2670 // some other way that the string is length-bound.
2671 Str = Str.substr(0, Str.find('\0'));
2676 // These next two are very similar to the above, but also look through PHI
2678 // TODO: See if we can integrate these two together.
2680 /// If we can compute the length of the string pointed to by
2681 /// the specified pointer, return 'len+1'. If we can't, return 0.
2682 static uint64_t GetStringLengthH(Value *V, SmallPtrSetImpl<PHINode*> &PHIs) {
2683 // Look through noop bitcast instructions.
2684 V = V->stripPointerCasts();
2686 // If this is a PHI node, there are two cases: either we have already seen it
2688 if (PHINode *PN = dyn_cast<PHINode>(V)) {
2689 if (!PHIs.insert(PN).second)
2690 return ~0ULL; // already in the set.
2692 // If it was new, see if all the input strings are the same length.
2693 uint64_t LenSoFar = ~0ULL;
2694 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2695 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
2696 if (Len == 0) return 0; // Unknown length -> unknown.
2698 if (Len == ~0ULL) continue;
2700 if (Len != LenSoFar && LenSoFar != ~0ULL)
2701 return 0; // Disagree -> unknown.
2705 // Success, all agree.
2709 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
2710 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
2711 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
2712 if (Len1 == 0) return 0;
2713 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
2714 if (Len2 == 0) return 0;
2715 if (Len1 == ~0ULL) return Len2;
2716 if (Len2 == ~0ULL) return Len1;
2717 if (Len1 != Len2) return 0;
2721 // Otherwise, see if we can read the string.
2723 if (!getConstantStringInfo(V, StrData))
2726 return StrData.size()+1;
2729 /// If we can compute the length of the string pointed to by
2730 /// the specified pointer, return 'len+1'. If we can't, return 0.
2731 uint64_t llvm::GetStringLength(Value *V) {
2732 if (!V->getType()->isPointerTy()) return 0;
2734 SmallPtrSet<PHINode*, 32> PHIs;
2735 uint64_t Len = GetStringLengthH(V, PHIs);
2736 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
2737 // an empty string as a length.
2738 return Len == ~0ULL ? 1 : Len;
2741 /// \brief \p PN defines a loop-variant pointer to an object. Check if the
2742 /// previous iteration of the loop was referring to the same object as \p PN.
2743 static bool isSameUnderlyingObjectInLoop(PHINode *PN, LoopInfo *LI) {
2744 // Find the loop-defined value.
2745 Loop *L = LI->getLoopFor(PN->getParent());
2746 if (PN->getNumIncomingValues() != 2)
2749 // Find the value from previous iteration.
2750 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
2751 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
2752 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
2753 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
2756 // If a new pointer is loaded in the loop, the pointer references a different
2757 // object in every iteration. E.g.:
2761 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
2762 if (!L->isLoopInvariant(Load->getPointerOperand()))
2767 Value *llvm::GetUnderlyingObject(Value *V, const DataLayout &DL,
2768 unsigned MaxLookup) {
2769 if (!V->getType()->isPointerTy())
2771 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
2772 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
2773 V = GEP->getPointerOperand();
2774 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
2775 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
2776 V = cast<Operator>(V)->getOperand(0);
2777 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2778 if (GA->mayBeOverridden())
2780 V = GA->getAliasee();
2782 // See if InstructionSimplify knows any relevant tricks.
2783 if (Instruction *I = dyn_cast<Instruction>(V))
2784 // TODO: Acquire a DominatorTree and AssumptionCache and use them.
2785 if (Value *Simplified = SimplifyInstruction(I, DL, nullptr)) {
2792 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
2797 void llvm::GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects,
2798 const DataLayout &DL, LoopInfo *LI,
2799 unsigned MaxLookup) {
2800 SmallPtrSet<Value *, 4> Visited;
2801 SmallVector<Value *, 4> Worklist;
2802 Worklist.push_back(V);
2804 Value *P = Worklist.pop_back_val();
2805 P = GetUnderlyingObject(P, DL, MaxLookup);
2807 if (!Visited.insert(P).second)
2810 if (SelectInst *SI = dyn_cast<SelectInst>(P)) {
2811 Worklist.push_back(SI->getTrueValue());
2812 Worklist.push_back(SI->getFalseValue());
2816 if (PHINode *PN = dyn_cast<PHINode>(P)) {
2817 // If this PHI changes the underlying object in every iteration of the
2818 // loop, don't look through it. Consider:
2821 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
2825 // Prev is tracking Curr one iteration behind so they refer to different
2826 // underlying objects.
2827 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
2828 isSameUnderlyingObjectInLoop(PN, LI))
2829 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
2830 Worklist.push_back(PN->getIncomingValue(i));
2834 Objects.push_back(P);
2835 } while (!Worklist.empty());
2838 /// Return true if the only users of this pointer are lifetime markers.
2839 bool llvm::onlyUsedByLifetimeMarkers(const Value *V) {
2840 for (const User *U : V->users()) {
2841 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
2842 if (!II) return false;
2844 if (II->getIntrinsicID() != Intrinsic::lifetime_start &&
2845 II->getIntrinsicID() != Intrinsic::lifetime_end)
2851 static bool isDereferenceableFromAttribute(const Value *BV, APInt Offset,
2852 Type *Ty, const DataLayout &DL) {
2853 assert(Offset.isNonNegative() && "offset can't be negative");
2854 assert(Ty->isSized() && "must be sized");
2856 APInt DerefBytes(Offset.getBitWidth(), 0);
2857 if (const Argument *A = dyn_cast<Argument>(BV)) {
2858 DerefBytes = A->getDereferenceableBytes();
2859 } else if (auto CS = ImmutableCallSite(BV)) {
2860 DerefBytes = CS.getDereferenceableBytes(0);
2863 if (DerefBytes.getBoolValue())
2864 if (DerefBytes.uge(Offset + DL.getTypeStoreSize(Ty)))
2870 static bool isDereferenceableFromAttribute(const Value *V,
2871 const DataLayout &DL) {
2872 Type *VTy = V->getType();
2873 Type *Ty = VTy->getPointerElementType();
2877 APInt Offset(DL.getTypeStoreSizeInBits(VTy), 0);
2878 return isDereferenceableFromAttribute(V, Offset, Ty, DL);
2881 /// Return true if Value is always a dereferenceable pointer.
2883 /// Test if V is always a pointer to allocated and suitably aligned memory for
2884 /// a simple load or store.
2885 static bool isDereferenceablePointer(const Value *V, const DataLayout &DL,
2886 SmallPtrSetImpl<const Value *> &Visited) {
2887 // Note that it is not safe to speculate into a malloc'd region because
2888 // malloc may return null.
2890 // These are obviously ok.
2891 if (isa<AllocaInst>(V)) return true;
2893 // It's not always safe to follow a bitcast, for example:
2894 // bitcast i8* (alloca i8) to i32*
2895 // would result in a 4-byte load from a 1-byte alloca. However,
2896 // if we're casting from a pointer from a type of larger size
2897 // to a type of smaller size (or the same size), and the alignment
2898 // is at least as large as for the resulting pointer type, then
2899 // we can look through the bitcast.
2900 if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) {
2901 Type *STy = BC->getSrcTy()->getPointerElementType(),
2902 *DTy = BC->getDestTy()->getPointerElementType();
2903 if (STy->isSized() && DTy->isSized() &&
2904 (DL.getTypeStoreSize(STy) >= DL.getTypeStoreSize(DTy)) &&
2905 (DL.getABITypeAlignment(STy) >= DL.getABITypeAlignment(DTy)))
2906 return isDereferenceablePointer(BC->getOperand(0), DL, Visited);
2909 // Global variables which can't collapse to null are ok.
2910 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
2911 return !GV->hasExternalWeakLinkage();
2913 // byval arguments are okay.
2914 if (const Argument *A = dyn_cast<Argument>(V))
2915 if (A->hasByValAttr())
2918 if (isDereferenceableFromAttribute(V, DL))
2921 // For GEPs, determine if the indexing lands within the allocated object.
2922 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
2923 // Conservatively require that the base pointer be fully dereferenceable.
2924 if (!Visited.insert(GEP->getOperand(0)).second)
2926 if (!isDereferenceablePointer(GEP->getOperand(0), DL, Visited))
2928 // Check the indices.
2929 gep_type_iterator GTI = gep_type_begin(GEP);
2930 for (User::const_op_iterator I = GEP->op_begin()+1,
2931 E = GEP->op_end(); I != E; ++I) {
2934 // Struct indices can't be out of bounds.
2935 if (isa<StructType>(Ty))
2937 ConstantInt *CI = dyn_cast<ConstantInt>(Index);
2940 // Zero is always ok.
2943 // Check to see that it's within the bounds of an array.
2944 ArrayType *ATy = dyn_cast<ArrayType>(Ty);
2947 if (CI->getValue().getActiveBits() > 64)
2949 if (CI->getZExtValue() >= ATy->getNumElements())
2952 // Indices check out; this is dereferenceable.
2956 // For gc.relocate, look through relocations
2957 if (const IntrinsicInst *I = dyn_cast<IntrinsicInst>(V))
2958 if (I->getIntrinsicID() == Intrinsic::experimental_gc_relocate) {
2959 GCRelocateOperands RelocateInst(I);
2960 return isDereferenceablePointer(RelocateInst.getDerivedPtr(), DL,
2964 if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V))
2965 return isDereferenceablePointer(ASC->getOperand(0), DL, Visited);
2967 // If we don't know, assume the worst.
2971 bool llvm::isDereferenceablePointer(const Value *V, const DataLayout &DL) {
2972 // When dereferenceability information is provided by a dereferenceable
2973 // attribute, we know exactly how many bytes are dereferenceable. If we can
2974 // determine the exact offset to the attributed variable, we can use that
2975 // information here.
2976 Type *VTy = V->getType();
2977 Type *Ty = VTy->getPointerElementType();
2978 if (Ty->isSized()) {
2979 APInt Offset(DL.getTypeStoreSizeInBits(VTy), 0);
2980 const Value *BV = V->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
2982 if (Offset.isNonNegative())
2983 if (isDereferenceableFromAttribute(BV, Offset, Ty, DL))
2987 SmallPtrSet<const Value *, 32> Visited;
2988 return ::isDereferenceablePointer(V, DL, Visited);
2991 bool llvm::isSafeToSpeculativelyExecute(const Value *V) {
2992 const Operator *Inst = dyn_cast<Operator>(V);
2996 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
2997 if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i)))
3001 switch (Inst->getOpcode()) {
3004 case Instruction::UDiv:
3005 case Instruction::URem: {
3006 // x / y is undefined if y == 0.
3008 if (match(Inst->getOperand(1), m_APInt(V)))
3012 case Instruction::SDiv:
3013 case Instruction::SRem: {
3014 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
3015 const APInt *Numerator, *Denominator;
3016 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
3018 // We cannot hoist this division if the denominator is 0.
3019 if (*Denominator == 0)
3021 // It's safe to hoist if the denominator is not 0 or -1.
3022 if (*Denominator != -1)
3024 // At this point we know that the denominator is -1. It is safe to hoist as
3025 // long we know that the numerator is not INT_MIN.
3026 if (match(Inst->getOperand(0), m_APInt(Numerator)))
3027 return !Numerator->isMinSignedValue();
3028 // The numerator *might* be MinSignedValue.
3031 case Instruction::Load: {
3032 const LoadInst *LI = cast<LoadInst>(Inst);
3033 if (!LI->isUnordered() ||
3034 // Speculative load may create a race that did not exist in the source.
3035 LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeThread))
3037 const DataLayout &DL = LI->getModule()->getDataLayout();
3038 return isDereferenceablePointer(LI->getPointerOperand(), DL);
3040 case Instruction::Call: {
3041 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
3042 switch (II->getIntrinsicID()) {
3043 // These synthetic intrinsics have no side-effects and just mark
3044 // information about their operands.
3045 // FIXME: There are other no-op synthetic instructions that potentially
3046 // should be considered at least *safe* to speculate...
3047 case Intrinsic::dbg_declare:
3048 case Intrinsic::dbg_value:
3051 case Intrinsic::bswap:
3052 case Intrinsic::ctlz:
3053 case Intrinsic::ctpop:
3054 case Intrinsic::cttz:
3055 case Intrinsic::objectsize:
3056 case Intrinsic::sadd_with_overflow:
3057 case Intrinsic::smul_with_overflow:
3058 case Intrinsic::ssub_with_overflow:
3059 case Intrinsic::uadd_with_overflow:
3060 case Intrinsic::umul_with_overflow:
3061 case Intrinsic::usub_with_overflow:
3063 // Sqrt should be OK, since the llvm sqrt intrinsic isn't defined to set
3064 // errno like libm sqrt would.
3065 case Intrinsic::sqrt:
3066 case Intrinsic::fma:
3067 case Intrinsic::fmuladd:
3068 case Intrinsic::fabs:
3069 case Intrinsic::minnum:
3070 case Intrinsic::maxnum:
3072 // TODO: some fp intrinsics are marked as having the same error handling
3073 // as libm. They're safe to speculate when they won't error.
3074 // TODO: are convert_{from,to}_fp16 safe?
3075 // TODO: can we list target-specific intrinsics here?
3079 return false; // The called function could have undefined behavior or
3080 // side-effects, even if marked readnone nounwind.
3082 case Instruction::VAArg:
3083 case Instruction::Alloca:
3084 case Instruction::Invoke:
3085 case Instruction::PHI:
3086 case Instruction::Store:
3087 case Instruction::Ret:
3088 case Instruction::Br:
3089 case Instruction::IndirectBr:
3090 case Instruction::Switch:
3091 case Instruction::Unreachable:
3092 case Instruction::Fence:
3093 case Instruction::LandingPad:
3094 case Instruction::AtomicRMW:
3095 case Instruction::AtomicCmpXchg:
3096 case Instruction::Resume:
3097 return false; // Misc instructions which have effects
3101 /// Return true if we know that the specified value is never null.
3102 bool llvm::isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI) {
3103 // Alloca never returns null, malloc might.
3104 if (isa<AllocaInst>(V)) return true;
3106 // A byval, inalloca, or nonnull argument is never null.
3107 if (const Argument *A = dyn_cast<Argument>(V))
3108 return A->hasByValOrInAllocaAttr() || A->hasNonNullAttr();
3110 // Global values are not null unless extern weak.
3111 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
3112 return !GV->hasExternalWeakLinkage();
3114 // A Load tagged w/nonnull metadata is never null.
3115 if (const LoadInst *LI = dyn_cast<LoadInst>(V))
3116 return LI->getMetadata(LLVMContext::MD_nonnull);
3118 if (auto CS = ImmutableCallSite(V))
3119 if (CS.isReturnNonNull())
3122 // operator new never returns null.
3123 if (isOperatorNewLikeFn(V, TLI, /*LookThroughBitCast=*/true))
3129 OverflowResult llvm::computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
3130 const DataLayout &DL,
3131 AssumptionCache *AC,
3132 const Instruction *CxtI,
3133 const DominatorTree *DT) {
3134 // Multiplying n * m significant bits yields a result of n + m significant
3135 // bits. If the total number of significant bits does not exceed the
3136 // result bit width (minus 1), there is no overflow.
3137 // This means if we have enough leading zero bits in the operands
3138 // we can guarantee that the result does not overflow.
3139 // Ref: "Hacker's Delight" by Henry Warren
3140 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
3141 APInt LHSKnownZero(BitWidth, 0);
3142 APInt LHSKnownOne(BitWidth, 0);
3143 APInt RHSKnownZero(BitWidth, 0);
3144 APInt RHSKnownOne(BitWidth, 0);
3145 computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, DL, /*Depth=*/0, AC, CxtI,
3147 computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, DL, /*Depth=*/0, AC, CxtI,
3149 // Note that underestimating the number of zero bits gives a more
3150 // conservative answer.
3151 unsigned ZeroBits = LHSKnownZero.countLeadingOnes() +
3152 RHSKnownZero.countLeadingOnes();
3153 // First handle the easy case: if we have enough zero bits there's
3154 // definitely no overflow.
3155 if (ZeroBits >= BitWidth)
3156 return OverflowResult::NeverOverflows;
3158 // Get the largest possible values for each operand.
3159 APInt LHSMax = ~LHSKnownZero;
3160 APInt RHSMax = ~RHSKnownZero;
3162 // We know the multiply operation doesn't overflow if the maximum values for
3163 // each operand will not overflow after we multiply them together.
3165 LHSMax.umul_ov(RHSMax, MaxOverflow);
3167 return OverflowResult::NeverOverflows;
3169 // We know it always overflows if multiplying the smallest possible values for
3170 // the operands also results in overflow.
3172 LHSKnownOne.umul_ov(RHSKnownOne, MinOverflow);
3174 return OverflowResult::AlwaysOverflows;
3176 return OverflowResult::MayOverflow;
3179 OverflowResult llvm::computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
3180 const DataLayout &DL,
3181 AssumptionCache *AC,
3182 const Instruction *CxtI,
3183 const DominatorTree *DT) {
3184 bool LHSKnownNonNegative, LHSKnownNegative;
3185 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, DL, /*Depth=*/0,
3187 if (LHSKnownNonNegative || LHSKnownNegative) {
3188 bool RHSKnownNonNegative, RHSKnownNegative;
3189 ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, DL, /*Depth=*/0,
3192 if (LHSKnownNegative && RHSKnownNegative) {
3193 // The sign bit is set in both cases: this MUST overflow.
3194 // Create a simple add instruction, and insert it into the struct.
3195 return OverflowResult::AlwaysOverflows;
3198 if (LHSKnownNonNegative && RHSKnownNonNegative) {
3199 // The sign bit is clear in both cases: this CANNOT overflow.
3200 // Create a simple add instruction, and insert it into the struct.
3201 return OverflowResult::NeverOverflows;
3205 return OverflowResult::MayOverflow;