Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1 | //===- ValueTracking.cpp - Walk computations to compute properties --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains routines that help analyze properties that chains of |
| 11 | // computations have. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/AssumptionCache.h" |
Dan Gohman | 949ab78 | 2010-12-15 20:10:26 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/InstructionSimplify.h" |
Benjamin Kramer | fd4777c | 2013-09-24 16:37:51 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/MemoryBuiltins.h" |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopInfo.h" |
Nick Lewycky | ec37354 | 2014-05-20 05:13:21 +0000 | [diff] [blame] | 21 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 22 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Constants.h" |
| 24 | #include "llvm/IR/DataLayout.h" |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 26 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 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" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 34 | #include "llvm/IR/PatternMatch.h" |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Statepoint.h" |
Matt Arsenault | f1a7e62 | 2014-07-15 01:55:03 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Debug.h" |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 6449690 | 2008-06-04 04:46:14 +0000 | [diff] [blame] | 38 | #include <cstring> |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 40 | using namespace llvm::PatternMatch; |
| 41 | |
| 42 | const unsigned MaxDepth = 6; |
| 43 | |
Philip Reames | 1c29227 | 2015-03-10 22:43:20 +0000 | [diff] [blame] | 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 |
| 48 | /// your findings. |
| 49 | static cl::opt<bool> EnableDomConditions("value-tracking-dom-conditions", |
| 50 | cl::Hidden, cl::init(false)); |
| 51 | |
| 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)); |
| 56 | |
| 57 | /// How many dominating blocks should be scanned looking for dominating |
| 58 | /// conditions? |
| 59 | static cl::opt<unsigned> DomConditionsMaxDomBlocks("dom-conditions-dom-blocks", |
| 60 | cl::Hidden, |
| 61 | cl::init(20000)); |
| 62 | |
| 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)); |
| 67 | |
| 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)); |
| 71 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 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. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 74 | static unsigned getBitWidth(Type *Ty, const DataLayout &DL) { |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 75 | if (unsigned BitWidth = Ty->getScalarSizeInBits()) |
| 76 | return BitWidth; |
Matt Arsenault | f55e5e7 | 2013-08-10 17:34:08 +0000 | [diff] [blame] | 77 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 78 | return DL.getPointerTypeSizeInBits(Ty); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 79 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 80 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 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; |
| 90 | |
Benjamin Kramer | cfd8d90 | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 91 | namespace { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 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. |
| 96 | struct Query { |
| 97 | ExclInvsSet ExclInvs; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 98 | AssumptionCache *AC; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 99 | const Instruction *CxtI; |
| 100 | const DominatorTree *DT; |
| 101 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 102 | Query(AssumptionCache *AC = nullptr, const Instruction *CxtI = nullptr, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 103 | const DominatorTree *DT = nullptr) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 104 | : AC(AC), CxtI(CxtI), DT(DT) {} |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 105 | |
| 106 | Query(const Query &Q, const Value *NewExcl) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 107 | : ExclInvs(Q.ExclInvs), AC(Q.AC), CxtI(Q.CxtI), DT(Q.DT) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 108 | ExclInvs.insert(NewExcl); |
| 109 | } |
| 110 | }; |
Benjamin Kramer | cfd8d90 | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 111 | } // end anonymous namespace |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 112 | |
Sanjay Patel | 547e975 | 2014-11-04 16:09:50 +0000 | [diff] [blame] | 113 | // Given the provided Value and, potentially, a context instruction, return |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 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()) |
| 119 | return CxtI; |
| 120 | |
| 121 | // If the value is really an already-inserted instruction, then use that. |
| 122 | CxtI = dyn_cast<Instruction>(V); |
| 123 | if (CxtI && CxtI->getParent()) |
| 124 | return CxtI; |
| 125 | |
| 126 | return nullptr; |
| 127 | } |
| 128 | |
| 129 | static void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 130 | const DataLayout &DL, unsigned Depth, |
| 131 | const Query &Q); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 132 | |
| 133 | void llvm::computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 134 | const DataLayout &DL, unsigned Depth, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 135 | AssumptionCache *AC, const Instruction *CxtI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 136 | const DominatorTree *DT) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 137 | ::computeKnownBits(V, KnownZero, KnownOne, DL, Depth, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 138 | Query(AC, safeCxtI(V, CxtI), DT)); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Jingyue Wu | ca32190 | 2015-05-14 23:53:19 +0000 | [diff] [blame] | 141 | bool llvm::haveNoCommonBitsSet(Value *LHS, Value *RHS, const DataLayout &DL, |
| 142 | AssumptionCache *AC, const Instruction *CxtI, |
| 143 | const DominatorTree *DT) { |
| 144 | assert(LHS->getType() == RHS->getType() && |
| 145 | "LHS and RHS should have the same type"); |
| 146 | assert(LHS->getType()->isIntOrIntVectorTy() && |
| 147 | "LHS and RHS should be integers"); |
| 148 | IntegerType *IT = cast<IntegerType>(LHS->getType()->getScalarType()); |
| 149 | APInt LHSKnownZero(IT->getBitWidth(), 0), LHSKnownOne(IT->getBitWidth(), 0); |
| 150 | APInt RHSKnownZero(IT->getBitWidth(), 0), RHSKnownOne(IT->getBitWidth(), 0); |
| 151 | computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, DL, 0, AC, CxtI, DT); |
| 152 | computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, DL, 0, AC, CxtI, DT); |
| 153 | return (LHSKnownZero | RHSKnownZero).isAllOnesValue(); |
| 154 | } |
| 155 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 156 | static void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 157 | const DataLayout &DL, unsigned Depth, |
| 158 | const Query &Q); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 159 | |
| 160 | void llvm::ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 161 | const DataLayout &DL, unsigned Depth, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 162 | AssumptionCache *AC, const Instruction *CxtI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 163 | const DominatorTree *DT) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 164 | ::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 165 | Query(AC, safeCxtI(V, CxtI), DT)); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero, unsigned Depth, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 169 | const Query &Q, const DataLayout &DL); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 170 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 171 | bool llvm::isKnownToBeAPowerOfTwo(Value *V, const DataLayout &DL, bool OrZero, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 172 | unsigned Depth, AssumptionCache *AC, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 173 | const Instruction *CxtI, |
| 174 | const DominatorTree *DT) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 175 | return ::isKnownToBeAPowerOfTwo(V, OrZero, Depth, |
| 176 | Query(AC, safeCxtI(V, CxtI), DT), DL); |
| 177 | } |
| 178 | |
| 179 | static bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth, |
| 180 | const Query &Q); |
| 181 | |
| 182 | bool llvm::isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth, |
| 183 | AssumptionCache *AC, const Instruction *CxtI, |
| 184 | const DominatorTree *DT) { |
| 185 | return ::isKnownNonZero(V, DL, Depth, Query(AC, safeCxtI(V, CxtI), DT)); |
| 186 | } |
| 187 | |
| 188 | static bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL, |
| 189 | unsigned Depth, const Query &Q); |
| 190 | |
| 191 | bool llvm::MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL, |
| 192 | unsigned Depth, AssumptionCache *AC, |
| 193 | const Instruction *CxtI, const DominatorTree *DT) { |
| 194 | return ::MaskedValueIsZero(V, Mask, DL, Depth, |
| 195 | Query(AC, safeCxtI(V, CxtI), DT)); |
| 196 | } |
| 197 | |
| 198 | static unsigned ComputeNumSignBits(Value *V, const DataLayout &DL, |
| 199 | unsigned Depth, const Query &Q); |
| 200 | |
| 201 | unsigned llvm::ComputeNumSignBits(Value *V, const DataLayout &DL, |
| 202 | unsigned Depth, AssumptionCache *AC, |
| 203 | const Instruction *CxtI, |
| 204 | const DominatorTree *DT) { |
| 205 | return ::ComputeNumSignBits(V, DL, Depth, Query(AC, safeCxtI(V, CxtI), DT)); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 208 | static void computeKnownBitsAddSub(bool Add, Value *Op0, Value *Op1, bool NSW, |
| 209 | APInt &KnownZero, APInt &KnownOne, |
| 210 | APInt &KnownZero2, APInt &KnownOne2, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 211 | const DataLayout &DL, unsigned Depth, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 212 | const Query &Q) { |
| 213 | if (!Add) { |
| 214 | if (ConstantInt *CLHS = dyn_cast<ConstantInt>(Op0)) { |
| 215 | // We know that the top bits of C-X are clear if X contains less bits |
| 216 | // than C (i.e. no wrap-around can happen). For example, 20-X is |
| 217 | // positive if we can prove that X is >= 0 and < 16. |
| 218 | if (!CLHS->getValue().isNegative()) { |
| 219 | unsigned BitWidth = KnownZero.getBitWidth(); |
| 220 | unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros(); |
| 221 | // NLZ can't be BitWidth with no sign bit |
| 222 | APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 223 | computeKnownBits(Op1, KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 224 | |
| 225 | // If all of the MaskV bits are known to be zero, then we know the |
| 226 | // output top bits are zero, because we now know that the output is |
| 227 | // from [0-C]. |
| 228 | if ((KnownZero2 & MaskV) == MaskV) { |
| 229 | unsigned NLZ2 = CLHS->getValue().countLeadingZeros(); |
| 230 | // Top bits known zero. |
| 231 | KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 237 | unsigned BitWidth = KnownZero.getBitWidth(); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 238 | |
David Majnemer | 97ddca3 | 2014-08-22 00:40:43 +0000 | [diff] [blame] | 239 | // If an initial sequence of bits in the result is not needed, the |
| 240 | // corresponding bits in the operands are not needed. |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 241 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 242 | computeKnownBits(Op0, LHSKnownZero, LHSKnownOne, DL, Depth + 1, Q); |
| 243 | computeKnownBits(Op1, KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 244 | |
David Majnemer | 97ddca3 | 2014-08-22 00:40:43 +0000 | [diff] [blame] | 245 | // Carry in a 1 for a subtract, rather than a 0. |
| 246 | APInt CarryIn(BitWidth, 0); |
| 247 | if (!Add) { |
| 248 | // Sum = LHS + ~RHS + 1 |
| 249 | std::swap(KnownZero2, KnownOne2); |
| 250 | CarryIn.setBit(0); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 251 | } |
| 252 | |
David Majnemer | 97ddca3 | 2014-08-22 00:40:43 +0000 | [diff] [blame] | 253 | APInt PossibleSumZero = ~LHSKnownZero + ~KnownZero2 + CarryIn; |
| 254 | APInt PossibleSumOne = LHSKnownOne + KnownOne2 + CarryIn; |
| 255 | |
| 256 | // Compute known bits of the carry. |
| 257 | APInt CarryKnownZero = ~(PossibleSumZero ^ LHSKnownZero ^ KnownZero2); |
| 258 | APInt CarryKnownOne = PossibleSumOne ^ LHSKnownOne ^ KnownOne2; |
| 259 | |
| 260 | // Compute set of known bits (where all three relevant bits are known). |
| 261 | APInt LHSKnown = LHSKnownZero | LHSKnownOne; |
| 262 | APInt RHSKnown = KnownZero2 | KnownOne2; |
| 263 | APInt CarryKnown = CarryKnownZero | CarryKnownOne; |
| 264 | APInt Known = LHSKnown & RHSKnown & CarryKnown; |
| 265 | |
| 266 | assert((PossibleSumZero & Known) == (PossibleSumOne & Known) && |
| 267 | "known bits of sum differ"); |
| 268 | |
| 269 | // Compute known bits of the result. |
| 270 | KnownZero = ~PossibleSumOne & Known; |
| 271 | KnownOne = PossibleSumOne & Known; |
| 272 | |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 273 | // Are we still trying to solve for the sign bit? |
David Majnemer | 97ddca3 | 2014-08-22 00:40:43 +0000 | [diff] [blame] | 274 | if (!Known.isNegative()) { |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 275 | if (NSW) { |
David Majnemer | 97ddca3 | 2014-08-22 00:40:43 +0000 | [diff] [blame] | 276 | // Adding two non-negative numbers, or subtracting a negative number from |
| 277 | // a non-negative one, can't wrap into negative. |
| 278 | if (LHSKnownZero.isNegative() && KnownZero2.isNegative()) |
| 279 | KnownZero |= APInt::getSignBit(BitWidth); |
| 280 | // Adding two negative numbers, or subtracting a non-negative number from |
| 281 | // a negative one, can't wrap into non-negative. |
| 282 | else if (LHSKnownOne.isNegative() && KnownOne2.isNegative()) |
| 283 | KnownOne |= APInt::getSignBit(BitWidth); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 288 | static void computeKnownBitsMul(Value *Op0, Value *Op1, bool NSW, |
| 289 | APInt &KnownZero, APInt &KnownOne, |
| 290 | APInt &KnownZero2, APInt &KnownOne2, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 291 | const DataLayout &DL, unsigned Depth, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 292 | const Query &Q) { |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 293 | unsigned BitWidth = KnownZero.getBitWidth(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 294 | computeKnownBits(Op1, KnownZero, KnownOne, DL, Depth + 1, Q); |
| 295 | computeKnownBits(Op0, KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 296 | |
| 297 | bool isKnownNegative = false; |
| 298 | bool isKnownNonNegative = false; |
| 299 | // If the multiplication is known not to overflow, compute the sign bit. |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 300 | if (NSW) { |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 301 | if (Op0 == Op1) { |
| 302 | // The product of a number with itself is non-negative. |
| 303 | isKnownNonNegative = true; |
| 304 | } else { |
| 305 | bool isKnownNonNegativeOp1 = KnownZero.isNegative(); |
| 306 | bool isKnownNonNegativeOp0 = KnownZero2.isNegative(); |
| 307 | bool isKnownNegativeOp1 = KnownOne.isNegative(); |
| 308 | bool isKnownNegativeOp0 = KnownOne2.isNegative(); |
| 309 | // The product of two numbers with the same sign is non-negative. |
| 310 | isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) || |
| 311 | (isKnownNonNegativeOp1 && isKnownNonNegativeOp0); |
| 312 | // The product of a negative number and a non-negative number is either |
| 313 | // negative or zero. |
| 314 | if (!isKnownNonNegative) |
| 315 | isKnownNegative = (isKnownNegativeOp1 && isKnownNonNegativeOp0 && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 316 | isKnownNonZero(Op0, DL, Depth, Q)) || |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 317 | (isKnownNegativeOp0 && isKnownNonNegativeOp1 && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 318 | isKnownNonZero(Op1, DL, Depth, Q)); |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | // If low bits are zero in either operand, output low known-0 bits. |
| 323 | // Also compute a conserative estimate for high known-0 bits. |
| 324 | // More trickiness is possible, but this is sufficient for the |
| 325 | // interesting case of alignment computation. |
| 326 | KnownOne.clearAllBits(); |
| 327 | unsigned TrailZ = KnownZero.countTrailingOnes() + |
| 328 | KnownZero2.countTrailingOnes(); |
| 329 | unsigned LeadZ = std::max(KnownZero.countLeadingOnes() + |
| 330 | KnownZero2.countLeadingOnes(), |
| 331 | BitWidth) - BitWidth; |
| 332 | |
| 333 | TrailZ = std::min(TrailZ, BitWidth); |
| 334 | LeadZ = std::min(LeadZ, BitWidth); |
| 335 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) | |
| 336 | APInt::getHighBitsSet(BitWidth, LeadZ); |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 337 | |
| 338 | // Only make use of no-wrap flags if we failed to compute the sign bit |
| 339 | // directly. This matters if the multiplication always overflows, in |
| 340 | // which case we prefer to follow the result of the direct computation, |
| 341 | // though as the program is invoking undefined behaviour we can choose |
| 342 | // whatever we like here. |
| 343 | if (isKnownNonNegative && !KnownOne.isNegative()) |
| 344 | KnownZero.setBit(BitWidth - 1); |
| 345 | else if (isKnownNegative && !KnownZero.isNegative()) |
| 346 | KnownOne.setBit(BitWidth - 1); |
| 347 | } |
| 348 | |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 349 | void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges, |
| 350 | APInt &KnownZero) { |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 351 | unsigned BitWidth = KnownZero.getBitWidth(); |
Rafael Espindola | 5319053 | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 352 | unsigned NumRanges = Ranges.getNumOperands() / 2; |
| 353 | assert(NumRanges >= 1); |
| 354 | |
| 355 | // Use the high end of the ranges to find leading zeros. |
| 356 | unsigned MinLeadingZeros = BitWidth; |
| 357 | for (unsigned i = 0; i < NumRanges; ++i) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 358 | ConstantInt *Lower = |
| 359 | mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0)); |
| 360 | ConstantInt *Upper = |
| 361 | mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1)); |
Rafael Espindola | 5319053 | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 362 | ConstantRange Range(Lower->getValue(), Upper->getValue()); |
| 363 | if (Range.isWrappedSet()) |
| 364 | MinLeadingZeros = 0; // -1 has no zeros |
| 365 | unsigned LeadingZeros = (Upper->getValue() - 1).countLeadingZeros(); |
| 366 | MinLeadingZeros = std::min(LeadingZeros, MinLeadingZeros); |
| 367 | } |
| 368 | |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 369 | KnownZero = APInt::getHighBitsSet(BitWidth, MinLeadingZeros); |
Rafael Espindola | 5319053 | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 370 | } |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 371 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 372 | static bool isEphemeralValueOf(Instruction *I, const Value *E) { |
| 373 | SmallVector<const Value *, 16> WorkSet(1, I); |
| 374 | SmallPtrSet<const Value *, 32> Visited; |
| 375 | SmallPtrSet<const Value *, 16> EphValues; |
| 376 | |
| 377 | while (!WorkSet.empty()) { |
| 378 | const Value *V = WorkSet.pop_back_val(); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 379 | if (!Visited.insert(V).second) |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 380 | continue; |
| 381 | |
| 382 | // If all uses of this value are ephemeral, then so is this value. |
| 383 | bool FoundNEUse = false; |
| 384 | for (const User *I : V->users()) |
| 385 | if (!EphValues.count(I)) { |
| 386 | FoundNEUse = true; |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | if (!FoundNEUse) { |
| 391 | if (V == E) |
| 392 | return true; |
| 393 | |
| 394 | EphValues.insert(V); |
| 395 | if (const User *U = dyn_cast<User>(V)) |
| 396 | for (User::const_op_iterator J = U->op_begin(), JE = U->op_end(); |
| 397 | J != JE; ++J) { |
| 398 | if (isSafeToSpeculativelyExecute(*J)) |
| 399 | WorkSet.push_back(*J); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | // Is this an intrinsic that cannot be speculated but also cannot trap? |
| 408 | static bool isAssumeLikeIntrinsic(const Instruction *I) { |
| 409 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 410 | if (Function *F = CI->getCalledFunction()) |
| 411 | switch (F->getIntrinsicID()) { |
| 412 | default: break; |
| 413 | // FIXME: This list is repeated from NoTTI::getIntrinsicCost. |
| 414 | case Intrinsic::assume: |
| 415 | case Intrinsic::dbg_declare: |
| 416 | case Intrinsic::dbg_value: |
| 417 | case Intrinsic::invariant_start: |
| 418 | case Intrinsic::invariant_end: |
| 419 | case Intrinsic::lifetime_start: |
| 420 | case Intrinsic::lifetime_end: |
| 421 | case Intrinsic::objectsize: |
| 422 | case Intrinsic::ptr_annotation: |
| 423 | case Intrinsic::var_annotation: |
| 424 | return true; |
| 425 | } |
| 426 | |
| 427 | return false; |
| 428 | } |
| 429 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 430 | static bool isValidAssumeForContext(Value *V, const Query &Q) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 431 | Instruction *Inv = cast<Instruction>(V); |
| 432 | |
| 433 | // There are two restrictions on the use of an assume: |
| 434 | // 1. The assume must dominate the context (or the control flow must |
| 435 | // reach the assume whenever it reaches the context). |
| 436 | // 2. The context must not be in the assume's set of ephemeral values |
| 437 | // (otherwise we will use the assume to prove that the condition |
| 438 | // feeding the assume is trivially true, thus causing the removal of |
| 439 | // the assume). |
| 440 | |
| 441 | if (Q.DT) { |
| 442 | if (Q.DT->dominates(Inv, Q.CxtI)) { |
| 443 | return true; |
| 444 | } else if (Inv->getParent() == Q.CxtI->getParent()) { |
| 445 | // The context comes first, but they're both in the same block. Make sure |
| 446 | // there is nothing in between that might interrupt the control flow. |
| 447 | for (BasicBlock::const_iterator I = |
| 448 | std::next(BasicBlock::const_iterator(Q.CxtI)), |
| 449 | IE(Inv); I != IE; ++I) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 450 | if (!isSafeToSpeculativelyExecute(I) && !isAssumeLikeIntrinsic(I)) |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 451 | return false; |
| 452 | |
| 453 | return !isEphemeralValueOf(Inv, Q.CxtI); |
| 454 | } |
| 455 | |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | // When we don't have a DT, we do a limited search... |
| 460 | if (Inv->getParent() == Q.CxtI->getParent()->getSinglePredecessor()) { |
| 461 | return true; |
| 462 | } else if (Inv->getParent() == Q.CxtI->getParent()) { |
| 463 | // Search forward from the assume until we reach the context (or the end |
| 464 | // of the block); the common case is that the assume will come first. |
| 465 | for (BasicBlock::iterator I = std::next(BasicBlock::iterator(Inv)), |
| 466 | IE = Inv->getParent()->end(); I != IE; ++I) |
| 467 | if (I == Q.CxtI) |
| 468 | return true; |
| 469 | |
| 470 | // The context must come first... |
| 471 | for (BasicBlock::const_iterator I = |
| 472 | std::next(BasicBlock::const_iterator(Q.CxtI)), |
| 473 | IE(Inv); I != IE; ++I) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 474 | if (!isSafeToSpeculativelyExecute(I) && !isAssumeLikeIntrinsic(I)) |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 475 | return false; |
| 476 | |
| 477 | return !isEphemeralValueOf(Inv, Q.CxtI); |
| 478 | } |
| 479 | |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | bool llvm::isValidAssumeForContext(const Instruction *I, |
| 484 | const Instruction *CxtI, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 485 | const DominatorTree *DT) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 486 | return ::isValidAssumeForContext(const_cast<Instruction *>(I), |
| 487 | Query(nullptr, CxtI, DT)); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | template<typename LHS, typename RHS> |
| 491 | inline match_combine_or<CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>, |
| 492 | CmpClass_match<RHS, LHS, ICmpInst, ICmpInst::Predicate>> |
| 493 | m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) { |
| 494 | return m_CombineOr(m_ICmp(Pred, L, R), m_ICmp(Pred, R, L)); |
| 495 | } |
| 496 | |
| 497 | template<typename LHS, typename RHS> |
| 498 | inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::And>, |
| 499 | BinaryOp_match<RHS, LHS, Instruction::And>> |
| 500 | m_c_And(const LHS &L, const RHS &R) { |
| 501 | return m_CombineOr(m_And(L, R), m_And(R, L)); |
| 502 | } |
| 503 | |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 504 | template<typename LHS, typename RHS> |
| 505 | inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::Or>, |
| 506 | BinaryOp_match<RHS, LHS, Instruction::Or>> |
| 507 | m_c_Or(const LHS &L, const RHS &R) { |
| 508 | return m_CombineOr(m_Or(L, R), m_Or(R, L)); |
| 509 | } |
| 510 | |
| 511 | template<typename LHS, typename RHS> |
| 512 | inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::Xor>, |
| 513 | BinaryOp_match<RHS, LHS, Instruction::Xor>> |
| 514 | m_c_Xor(const LHS &L, const RHS &R) { |
| 515 | return m_CombineOr(m_Xor(L, R), m_Xor(R, L)); |
| 516 | } |
| 517 | |
Philip Reames | 1c29227 | 2015-03-10 22:43:20 +0000 | [diff] [blame] | 518 | /// Compute known bits in 'V' under the assumption that the condition 'Cmp' is |
| 519 | /// true (at the context instruction.) This is mostly a utility function for |
| 520 | /// the prototype dominating conditions reasoning below. |
| 521 | static void computeKnownBitsFromTrueCondition(Value *V, ICmpInst *Cmp, |
| 522 | APInt &KnownZero, |
| 523 | APInt &KnownOne, |
| 524 | const DataLayout &DL, |
| 525 | unsigned Depth, const Query &Q) { |
| 526 | Value *LHS = Cmp->getOperand(0); |
| 527 | Value *RHS = Cmp->getOperand(1); |
| 528 | // TODO: We could potentially be more aggressive here. This would be worth |
| 529 | // evaluating. If we can, explore commoning this code with the assume |
| 530 | // handling logic. |
| 531 | if (LHS != V && RHS != V) |
| 532 | return; |
| 533 | |
| 534 | const unsigned BitWidth = KnownZero.getBitWidth(); |
| 535 | |
| 536 | switch (Cmp->getPredicate()) { |
| 537 | default: |
| 538 | // We know nothing from this condition |
| 539 | break; |
| 540 | // TODO: implement unsigned bound from below (known one bits) |
| 541 | // TODO: common condition check implementations with assumes |
| 542 | // TODO: implement other patterns from assume (e.g. V & B == A) |
| 543 | case ICmpInst::ICMP_SGT: |
| 544 | if (LHS == V) { |
| 545 | APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0); |
| 546 | computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q); |
| 547 | if (KnownOneTemp.isAllOnesValue() || KnownZeroTemp.isNegative()) { |
| 548 | // We know that the sign bit is zero. |
| 549 | KnownZero |= APInt::getSignBit(BitWidth); |
| 550 | } |
| 551 | } |
| 552 | break; |
| 553 | case ICmpInst::ICMP_EQ: |
| 554 | if (LHS == V) |
| 555 | computeKnownBits(RHS, KnownZero, KnownOne, DL, Depth + 1, Q); |
| 556 | else if (RHS == V) |
| 557 | computeKnownBits(LHS, KnownZero, KnownOne, DL, Depth + 1, Q); |
| 558 | else |
| 559 | llvm_unreachable("missing use?"); |
| 560 | break; |
| 561 | case ICmpInst::ICMP_ULE: |
| 562 | if (LHS == V) { |
| 563 | APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0); |
| 564 | computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q); |
| 565 | // The known zero bits carry over |
| 566 | unsigned SignBits = KnownZeroTemp.countLeadingOnes(); |
| 567 | KnownZero |= APInt::getHighBitsSet(BitWidth, SignBits); |
| 568 | } |
| 569 | break; |
| 570 | case ICmpInst::ICMP_ULT: |
| 571 | if (LHS == V) { |
| 572 | APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0); |
| 573 | computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q); |
| 574 | // Whatever high bits in rhs are zero are known to be zero (if rhs is a |
| 575 | // power of 2, then one more). |
| 576 | unsigned SignBits = KnownZeroTemp.countLeadingOnes(); |
| 577 | if (isKnownToBeAPowerOfTwo(RHS, false, Depth + 1, Query(Q, Cmp), DL)) |
| 578 | SignBits++; |
| 579 | KnownZero |= APInt::getHighBitsSet(BitWidth, SignBits); |
| 580 | } |
| 581 | break; |
| 582 | }; |
| 583 | } |
| 584 | |
| 585 | /// Compute known bits in 'V' from conditions which are known to be true along |
| 586 | /// all paths leading to the context instruction. In particular, look for |
| 587 | /// cases where one branch of an interesting condition dominates the context |
| 588 | /// instruction. This does not do general dataflow. |
| 589 | /// NOTE: This code is EXPERIMENTAL and currently off by default. |
| 590 | static void computeKnownBitsFromDominatingCondition(Value *V, APInt &KnownZero, |
| 591 | APInt &KnownOne, |
| 592 | const DataLayout &DL, |
| 593 | unsigned Depth, |
| 594 | const Query &Q) { |
| 595 | // Need both the dominator tree and the query location to do anything useful |
| 596 | if (!Q.DT || !Q.CxtI) |
| 597 | return; |
| 598 | Instruction *Cxt = const_cast<Instruction *>(Q.CxtI); |
| 599 | |
| 600 | // Avoid useless work |
| 601 | if (auto VI = dyn_cast<Instruction>(V)) |
| 602 | if (VI->getParent() == Cxt->getParent()) |
| 603 | return; |
| 604 | |
| 605 | // Note: We currently implement two options. It's not clear which of these |
| 606 | // will survive long term, we need data for that. |
| 607 | // Option 1 - Try walking the dominator tree looking for conditions which |
| 608 | // might apply. This works well for local conditions (loop guards, etc..), |
| 609 | // but not as well for things far from the context instruction (presuming a |
| 610 | // low max blocks explored). If we can set an high enough limit, this would |
| 611 | // be all we need. |
| 612 | // Option 2 - We restrict out search to those conditions which are uses of |
| 613 | // the value we're interested in. This is independent of dom structure, |
| 614 | // but is slightly less powerful without looking through lots of use chains. |
| 615 | // It does handle conditions far from the context instruction (e.g. early |
| 616 | // function exits on entry) really well though. |
| 617 | |
| 618 | // Option 1 - Search the dom tree |
| 619 | unsigned NumBlocksExplored = 0; |
| 620 | BasicBlock *Current = Cxt->getParent(); |
| 621 | while (true) { |
| 622 | // Stop searching if we've gone too far up the chain |
| 623 | if (NumBlocksExplored >= DomConditionsMaxDomBlocks) |
| 624 | break; |
| 625 | NumBlocksExplored++; |
| 626 | |
| 627 | if (!Q.DT->getNode(Current)->getIDom()) |
| 628 | break; |
| 629 | Current = Q.DT->getNode(Current)->getIDom()->getBlock(); |
| 630 | if (!Current) |
| 631 | // found function entry |
| 632 | break; |
| 633 | |
| 634 | BranchInst *BI = dyn_cast<BranchInst>(Current->getTerminator()); |
| 635 | if (!BI || BI->isUnconditional()) |
| 636 | continue; |
| 637 | ICmpInst *Cmp = dyn_cast<ICmpInst>(BI->getCondition()); |
| 638 | if (!Cmp) |
| 639 | continue; |
| 640 | |
| 641 | // We're looking for conditions that are guaranteed to hold at the context |
| 642 | // instruction. Finding a condition where one path dominates the context |
| 643 | // isn't enough because both the true and false cases could merge before |
| 644 | // the context instruction we're actually interested in. Instead, we need |
| 645 | // to ensure that the taken *edge* dominates the context instruction. |
| 646 | BasicBlock *BB0 = BI->getSuccessor(0); |
| 647 | BasicBlockEdge Edge(BI->getParent(), BB0); |
| 648 | if (!Edge.isSingleEdge() || !Q.DT->dominates(Edge, Q.CxtI->getParent())) |
| 649 | continue; |
| 650 | |
| 651 | computeKnownBitsFromTrueCondition(V, Cmp, KnownZero, KnownOne, DL, Depth, |
| 652 | Q); |
| 653 | } |
| 654 | |
| 655 | // Option 2 - Search the other uses of V |
| 656 | unsigned NumUsesExplored = 0; |
| 657 | for (auto U : V->users()) { |
| 658 | // Avoid massive lists |
| 659 | if (NumUsesExplored >= DomConditionsMaxUses) |
| 660 | break; |
| 661 | NumUsesExplored++; |
| 662 | // Consider only compare instructions uniquely controlling a branch |
| 663 | ICmpInst *Cmp = dyn_cast<ICmpInst>(U); |
| 664 | if (!Cmp) |
| 665 | continue; |
| 666 | |
| 667 | if (DomConditionsSingleCmpUse && !Cmp->hasOneUse()) |
| 668 | continue; |
| 669 | |
| 670 | for (auto *CmpU : Cmp->users()) { |
| 671 | BranchInst *BI = dyn_cast<BranchInst>(CmpU); |
| 672 | if (!BI || BI->isUnconditional()) |
| 673 | continue; |
| 674 | // We're looking for conditions that are guaranteed to hold at the |
| 675 | // context instruction. Finding a condition where one path dominates |
| 676 | // the context isn't enough because both the true and false cases could |
| 677 | // merge before the context instruction we're actually interested in. |
| 678 | // Instead, we need to ensure that the taken *edge* dominates the context |
| 679 | // instruction. |
| 680 | BasicBlock *BB0 = BI->getSuccessor(0); |
| 681 | BasicBlockEdge Edge(BI->getParent(), BB0); |
| 682 | if (!Edge.isSingleEdge() || !Q.DT->dominates(Edge, Q.CxtI->getParent())) |
| 683 | continue; |
| 684 | |
| 685 | computeKnownBitsFromTrueCondition(V, Cmp, KnownZero, KnownOne, DL, Depth, |
| 686 | Q); |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 691 | static void computeKnownBitsFromAssume(Value *V, APInt &KnownZero, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 692 | APInt &KnownOne, const DataLayout &DL, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 693 | unsigned Depth, const Query &Q) { |
| 694 | // Use of assumptions is context-sensitive. If we don't have a context, we |
| 695 | // cannot use them! |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 696 | if (!Q.AC || !Q.CxtI) |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 697 | return; |
| 698 | |
| 699 | unsigned BitWidth = KnownZero.getBitWidth(); |
| 700 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 701 | for (auto &AssumeVH : Q.AC->assumptions()) { |
| 702 | if (!AssumeVH) |
| 703 | continue; |
| 704 | CallInst *I = cast<CallInst>(AssumeVH); |
Chandler Carruth | 75c11b8 | 2015-01-04 23:13:57 +0000 | [diff] [blame] | 705 | assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() && |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 706 | "Got assumption for the wrong function!"); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 707 | if (Q.ExclInvs.count(I)) |
| 708 | continue; |
| 709 | |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 710 | // Warning: This loop can end up being somewhat performance sensetive. |
| 711 | // We're running this loop for once for each value queried resulting in a |
| 712 | // runtime of ~O(#assumes * #values). |
| 713 | |
Benjamin Kramer | 619c4e5 | 2015-04-10 11:24:51 +0000 | [diff] [blame] | 714 | assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume && |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 715 | "must be an assume intrinsic"); |
Benjamin Kramer | 619c4e5 | 2015-04-10 11:24:51 +0000 | [diff] [blame] | 716 | |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 717 | Value *Arg = I->getArgOperand(0); |
| 718 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 719 | if (Arg == V && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 720 | assert(BitWidth == 1 && "assume operand is not i1?"); |
| 721 | KnownZero.clearAllBits(); |
| 722 | KnownOne.setAllBits(); |
| 723 | return; |
| 724 | } |
| 725 | |
David Majnemer | 9b60975 | 2014-12-12 23:59:29 +0000 | [diff] [blame] | 726 | // The remaining tests are all recursive, so bail out if we hit the limit. |
| 727 | if (Depth == MaxDepth) |
| 728 | continue; |
| 729 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 730 | Value *A, *B; |
| 731 | auto m_V = m_CombineOr(m_Specific(V), |
| 732 | m_CombineOr(m_PtrToInt(m_Specific(V)), |
| 733 | m_BitCast(m_Specific(V)))); |
| 734 | |
| 735 | CmpInst::Predicate Pred; |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 736 | ConstantInt *C; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 737 | // assume(v = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 738 | if (match(Arg, m_c_ICmp(Pred, m_V, m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 739 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 740 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 741 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 742 | KnownZero |= RHSKnownZero; |
| 743 | KnownOne |= RHSKnownOne; |
| 744 | // assume(v & b = a) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 745 | } else if (match(Arg, |
| 746 | m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A))) && |
| 747 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 748 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 749 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 750 | APInt MaskKnownZero(BitWidth, 0), MaskKnownOne(BitWidth, 0); |
| 751 | computeKnownBits(B, MaskKnownZero, MaskKnownOne, DL, Depth+1, Query(Q, I)); |
| 752 | |
| 753 | // For those bits in the mask that are known to be one, we can propagate |
| 754 | // known bits from the RHS to V. |
| 755 | KnownZero |= RHSKnownZero & MaskKnownOne; |
| 756 | KnownOne |= RHSKnownOne & MaskKnownOne; |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 757 | // assume(~(v & b) = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 758 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))), |
| 759 | m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 760 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 761 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 762 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 763 | APInt MaskKnownZero(BitWidth, 0), MaskKnownOne(BitWidth, 0); |
| 764 | computeKnownBits(B, MaskKnownZero, MaskKnownOne, DL, Depth+1, Query(Q, I)); |
| 765 | |
| 766 | // For those bits in the mask that are known to be one, we can propagate |
| 767 | // inverted known bits from the RHS to V. |
| 768 | KnownZero |= RHSKnownOne & MaskKnownOne; |
| 769 | KnownOne |= RHSKnownZero & MaskKnownOne; |
| 770 | // assume(v | b = a) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 771 | } else if (match(Arg, |
| 772 | m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A))) && |
| 773 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 774 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 775 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 776 | APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0); |
| 777 | computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I)); |
| 778 | |
| 779 | // For those bits in B that are known to be zero, we can propagate known |
| 780 | // bits from the RHS to V. |
| 781 | KnownZero |= RHSKnownZero & BKnownZero; |
| 782 | KnownOne |= RHSKnownOne & BKnownZero; |
| 783 | // assume(~(v | b) = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 784 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))), |
| 785 | m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 786 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 787 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 788 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 789 | APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0); |
| 790 | computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I)); |
| 791 | |
| 792 | // For those bits in B that are known to be zero, we can propagate |
| 793 | // inverted known bits from the RHS to V. |
| 794 | KnownZero |= RHSKnownOne & BKnownZero; |
| 795 | KnownOne |= RHSKnownZero & BKnownZero; |
| 796 | // assume(v ^ b = a) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 797 | } else if (match(Arg, |
| 798 | m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A))) && |
| 799 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 800 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 801 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 802 | APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0); |
| 803 | computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I)); |
| 804 | |
| 805 | // For those bits in B that are known to be zero, we can propagate known |
| 806 | // bits from the RHS to V. For those bits in B that are known to be one, |
| 807 | // we can propagate inverted known bits from the RHS to V. |
| 808 | KnownZero |= RHSKnownZero & BKnownZero; |
| 809 | KnownOne |= RHSKnownOne & BKnownZero; |
| 810 | KnownZero |= RHSKnownOne & BKnownOne; |
| 811 | KnownOne |= RHSKnownZero & BKnownOne; |
| 812 | // assume(~(v ^ b) = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 813 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))), |
| 814 | m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 815 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 816 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 817 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 818 | APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0); |
| 819 | computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I)); |
| 820 | |
| 821 | // For those bits in B that are known to be zero, we can propagate |
| 822 | // inverted known bits from the RHS to V. For those bits in B that are |
| 823 | // known to be one, we can propagate known bits from the RHS to V. |
| 824 | KnownZero |= RHSKnownOne & BKnownZero; |
| 825 | KnownOne |= RHSKnownZero & BKnownZero; |
| 826 | KnownZero |= RHSKnownZero & BKnownOne; |
| 827 | KnownOne |= RHSKnownOne & BKnownOne; |
| 828 | // assume(v << c = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 829 | } else if (match(Arg, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)), |
| 830 | m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 831 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 832 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 833 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 834 | // For those bits in RHS that are known, we can propagate them to known |
| 835 | // bits in V shifted to the right by C. |
| 836 | KnownZero |= RHSKnownZero.lshr(C->getZExtValue()); |
| 837 | KnownOne |= RHSKnownOne.lshr(C->getZExtValue()); |
| 838 | // assume(~(v << c) = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 839 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))), |
| 840 | m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 841 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 842 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 843 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 844 | // For those bits in RHS that are known, we can propagate them inverted |
| 845 | // to known bits in V shifted to the right by C. |
| 846 | KnownZero |= RHSKnownOne.lshr(C->getZExtValue()); |
| 847 | KnownOne |= RHSKnownZero.lshr(C->getZExtValue()); |
| 848 | // assume(v >> c = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 849 | } else if (match(Arg, |
| 850 | m_c_ICmp(Pred, m_CombineOr(m_LShr(m_V, m_ConstantInt(C)), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 851 | m_AShr(m_V, m_ConstantInt(C))), |
| 852 | m_Value(A))) && |
| 853 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 854 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 855 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 856 | // For those bits in RHS that are known, we can propagate them to known |
| 857 | // bits in V shifted to the right by C. |
| 858 | KnownZero |= RHSKnownZero << C->getZExtValue(); |
| 859 | KnownOne |= RHSKnownOne << C->getZExtValue(); |
| 860 | // assume(~(v >> c) = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 861 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_CombineOr( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 862 | m_LShr(m_V, m_ConstantInt(C)), |
| 863 | m_AShr(m_V, m_ConstantInt(C)))), |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 864 | m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 865 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 866 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 867 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 868 | // For those bits in RHS that are known, we can propagate them inverted |
| 869 | // to known bits in V shifted to the right by C. |
| 870 | KnownZero |= RHSKnownOne << C->getZExtValue(); |
| 871 | KnownOne |= RHSKnownZero << C->getZExtValue(); |
| 872 | // assume(v >=_s c) where c is non-negative |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 873 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 874 | Pred == ICmpInst::ICMP_SGE && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 875 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 876 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 877 | |
| 878 | if (RHSKnownZero.isNegative()) { |
| 879 | // We know that the sign bit is zero. |
| 880 | KnownZero |= APInt::getSignBit(BitWidth); |
| 881 | } |
| 882 | // assume(v >_s c) where c is at least -1. |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 883 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 884 | Pred == ICmpInst::ICMP_SGT && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 885 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 886 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 887 | |
| 888 | if (RHSKnownOne.isAllOnesValue() || RHSKnownZero.isNegative()) { |
| 889 | // We know that the sign bit is zero. |
| 890 | KnownZero |= APInt::getSignBit(BitWidth); |
| 891 | } |
| 892 | // assume(v <=_s c) where c is negative |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 893 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 894 | Pred == ICmpInst::ICMP_SLE && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 895 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 896 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 897 | |
| 898 | if (RHSKnownOne.isNegative()) { |
| 899 | // We know that the sign bit is one. |
| 900 | KnownOne |= APInt::getSignBit(BitWidth); |
| 901 | } |
| 902 | // assume(v <_s c) where c is non-positive |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 903 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 904 | Pred == ICmpInst::ICMP_SLT && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 905 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 906 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 907 | |
| 908 | if (RHSKnownZero.isAllOnesValue() || RHSKnownOne.isNegative()) { |
| 909 | // We know that the sign bit is one. |
| 910 | KnownOne |= APInt::getSignBit(BitWidth); |
| 911 | } |
| 912 | // assume(v <=_u c) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 913 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 914 | Pred == ICmpInst::ICMP_ULE && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 915 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 916 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 917 | |
| 918 | // Whatever high bits in c are zero are known to be zero. |
| 919 | KnownZero |= |
| 920 | APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes()); |
| 921 | // assume(v <_u c) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 922 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 923 | Pred == ICmpInst::ICMP_ULT && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 924 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 925 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 926 | |
| 927 | // Whatever high bits in c are zero are known to be zero (if c is a power |
| 928 | // of 2, then one more). |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 929 | if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, Query(Q, I), DL)) |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 930 | KnownZero |= |
| 931 | APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes()+1); |
| 932 | else |
| 933 | KnownZero |= |
| 934 | APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes()); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 939 | /// Determine which bits of V are known to be either zero or one and return |
| 940 | /// them in the KnownZero/KnownOne bit sets. |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 941 | /// |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 942 | /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 943 | /// we cannot optimize based on the assumption that it is zero without changing |
| 944 | /// it to be an explicit zero. If we don't change it to zero, other code could |
| 945 | /// optimized based on the contradictory assumption that it is non-zero. |
| 946 | /// Because instcombine aggressively folds operations with undef args anyway, |
| 947 | /// this won't lose us code quality. |
Chris Lattner | 4bc2825 | 2009-09-08 00:06:16 +0000 | [diff] [blame] | 948 | /// |
| 949 | /// This function is defined on values with integer type, values with pointer |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 950 | /// type, and vectors of integers. In the case |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 951 | /// where V is a vector, known zero, and known one values are the |
Chris Lattner | 4bc2825 | 2009-09-08 00:06:16 +0000 | [diff] [blame] | 952 | /// same width as the vector element, and the bit is set only if it is true |
| 953 | /// for all of the elements in the vector. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 954 | void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 955 | const DataLayout &DL, unsigned Depth, const Query &Q) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 956 | assert(V && "No Value?"); |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 957 | assert(Depth <= MaxDepth && "Limit Search Depth"); |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 958 | unsigned BitWidth = KnownZero.getBitWidth(); |
| 959 | |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 960 | assert((V->getType()->isIntOrIntVectorTy() || |
| 961 | V->getType()->getScalarType()->isPointerTy()) && |
| 962 | "Not integer or pointer type!"); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 963 | assert((DL.getTypeSizeInBits(V->getType()->getScalarType()) == BitWidth) && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 964 | (!V->getType()->isIntOrIntVectorTy() || |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 965 | V->getType()->getScalarSizeInBits() == BitWidth) && |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 966 | KnownZero.getBitWidth() == BitWidth && |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 967 | KnownOne.getBitWidth() == BitWidth && |
Jay Foad | e48d9e8 | 2014-05-14 08:00:07 +0000 | [diff] [blame] | 968 | "V, KnownOne and KnownZero should have same BitWidth"); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 969 | |
| 970 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 971 | // We know all of the bits for a constant! |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 972 | KnownOne = CI->getValue(); |
| 973 | KnownZero = ~KnownOne; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 974 | return; |
| 975 | } |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 976 | // Null and aggregate-zero are all-zeros. |
| 977 | if (isa<ConstantPointerNull>(V) || |
| 978 | isa<ConstantAggregateZero>(V)) { |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 979 | KnownOne.clearAllBits(); |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 980 | KnownZero = APInt::getAllOnesValue(BitWidth); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 981 | return; |
| 982 | } |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 983 | // Handle a constant vector by taking the intersection of the known bits of |
Chris Lattner | 8213c8a | 2012-02-06 21:56:39 +0000 | [diff] [blame] | 984 | // each element. There is no real need to handle ConstantVector here, because |
| 985 | // we don't handle undef in any particularly useful way. |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 986 | if (ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) { |
| 987 | // We know that CDS must be a vector of integers. Take the intersection of |
| 988 | // each element. |
| 989 | KnownZero.setAllBits(); KnownOne.setAllBits(); |
| 990 | APInt Elt(KnownZero.getBitWidth(), 0); |
Chris Lattner | 9be5959 | 2012-01-25 01:27:20 +0000 | [diff] [blame] | 991 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 992 | Elt = CDS->getElementAsInteger(i); |
| 993 | KnownZero &= ~Elt; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 994 | KnownOne &= Elt; |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 995 | } |
| 996 | return; |
| 997 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 998 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 999 | // The address of an aligned GlobalValue has trailing zeros. |
Michael Kuperstein | be8032c | 2014-12-23 11:33:41 +0000 | [diff] [blame] | 1000 | if (auto *GO = dyn_cast<GlobalObject>(V)) { |
| 1001 | unsigned Align = GO->getAlignment(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1002 | if (Align == 0) { |
Michael Kuperstein | be8032c | 2014-12-23 11:33:41 +0000 | [diff] [blame] | 1003 | if (auto *GVar = dyn_cast<GlobalVariable>(GO)) { |
Eli Friedman | e7ab1a2 | 2011-11-28 22:48:22 +0000 | [diff] [blame] | 1004 | Type *ObjectType = GVar->getType()->getElementType(); |
Nick Lewycky | 1d57ee3 | 2012-03-07 02:27:53 +0000 | [diff] [blame] | 1005 | if (ObjectType->isSized()) { |
| 1006 | // If the object is defined in the current Module, we'll be giving |
| 1007 | // it the preferred alignment. Otherwise, we have to assume that it |
| 1008 | // may only have the minimum ABI alignment. |
| 1009 | if (!GVar->isDeclaration() && !GVar->isWeakForLinker()) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1010 | Align = DL.getPreferredAlignment(GVar); |
Nick Lewycky | 1d57ee3 | 2012-03-07 02:27:53 +0000 | [diff] [blame] | 1011 | else |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1012 | Align = DL.getABITypeAlignment(ObjectType); |
Nick Lewycky | 1d57ee3 | 2012-03-07 02:27:53 +0000 | [diff] [blame] | 1013 | } |
Eli Friedman | e7ab1a2 | 2011-11-28 22:48:22 +0000 | [diff] [blame] | 1014 | } |
Dan Gohman | a72f856 | 2009-08-11 15:50:03 +0000 | [diff] [blame] | 1015 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1016 | if (Align > 0) |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1017 | KnownZero = APInt::getLowBitsSet(BitWidth, |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1018 | countTrailingZeros(Align)); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1019 | else |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 1020 | KnownZero.clearAllBits(); |
| 1021 | KnownOne.clearAllBits(); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1022 | return; |
| 1023 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1024 | |
Chris Lattner | 83791ce | 2011-05-23 00:03:39 +0000 | [diff] [blame] | 1025 | if (Argument *A = dyn_cast<Argument>(V)) { |
Hal Finkel | ccc7090 | 2014-07-22 16:58:55 +0000 | [diff] [blame] | 1026 | unsigned Align = A->getType()->isPointerTy() ? A->getParamAlignment() : 0; |
Duncan Sands | 271ea6c | 2012-10-04 13:36:31 +0000 | [diff] [blame] | 1027 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1028 | if (!Align && A->hasStructRetAttr()) { |
Duncan Sands | 271ea6c | 2012-10-04 13:36:31 +0000 | [diff] [blame] | 1029 | // An sret parameter has at least the ABI alignment of the return type. |
| 1030 | Type *EltTy = cast<PointerType>(A->getType())->getElementType(); |
| 1031 | if (EltTy->isSized()) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1032 | Align = DL.getABITypeAlignment(EltTy); |
Duncan Sands | 271ea6c | 2012-10-04 13:36:31 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | if (Align) |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1036 | KnownZero = APInt::getLowBitsSet(BitWidth, countTrailingZeros(Align)); |
David Majnemer | 8df46c9 | 2015-01-03 02:33:25 +0000 | [diff] [blame] | 1037 | else |
| 1038 | KnownZero.clearAllBits(); |
| 1039 | KnownOne.clearAllBits(); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1040 | |
| 1041 | // Don't give up yet... there might be an assumption that provides more |
| 1042 | // information... |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1043 | computeKnownBitsFromAssume(V, KnownZero, KnownOne, DL, Depth, Q); |
Philip Reames | 1c29227 | 2015-03-10 22:43:20 +0000 | [diff] [blame] | 1044 | |
| 1045 | // Or a dominating condition for that matter |
| 1046 | if (EnableDomConditions && Depth <= DomConditionsMaxDepth) |
| 1047 | computeKnownBitsFromDominatingCondition(V, KnownZero, KnownOne, DL, |
| 1048 | Depth, Q); |
Chris Lattner | 83791ce | 2011-05-23 00:03:39 +0000 | [diff] [blame] | 1049 | return; |
| 1050 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1051 | |
Chris Lattner | 83791ce | 2011-05-23 00:03:39 +0000 | [diff] [blame] | 1052 | // Start out not knowing anything. |
| 1053 | KnownZero.clearAllBits(); KnownOne.clearAllBits(); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1054 | |
Michael Kuperstein | be8032c | 2014-12-23 11:33:41 +0000 | [diff] [blame] | 1055 | // Limit search depth. |
| 1056 | // All recursive calls that increase depth must come after this. |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1057 | if (Depth == MaxDepth) |
Michael Kuperstein | be8032c | 2014-12-23 11:33:41 +0000 | [diff] [blame] | 1058 | return; |
| 1059 | |
| 1060 | // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has |
| 1061 | // the bits of its aliasee. |
| 1062 | if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
| 1063 | if (!GA->mayBeOverridden()) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1064 | computeKnownBits(GA->getAliasee(), KnownZero, KnownOne, DL, Depth + 1, Q); |
Michael Kuperstein | be8032c | 2014-12-23 11:33:41 +0000 | [diff] [blame] | 1065 | return; |
| 1066 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1067 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1068 | // Check whether a nearby assume intrinsic can determine some known bits. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1069 | computeKnownBitsFromAssume(V, KnownZero, KnownOne, DL, Depth, Q); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1070 | |
Philip Reames | 1c29227 | 2015-03-10 22:43:20 +0000 | [diff] [blame] | 1071 | // Check whether there's a dominating condition which implies something about |
| 1072 | // this value at the given context. |
| 1073 | if (EnableDomConditions && Depth <= DomConditionsMaxDepth) |
| 1074 | computeKnownBitsFromDominatingCondition(V, KnownZero, KnownOne, DL, Depth, |
| 1075 | Q); |
| 1076 | |
Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 1077 | Operator *I = dyn_cast<Operator>(V); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1078 | if (!I) return; |
| 1079 | |
| 1080 | APInt KnownZero2(KnownZero), KnownOne2(KnownOne); |
Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 1081 | switch (I->getOpcode()) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1082 | default: break; |
Rafael Espindola | 5319053 | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 1083 | case Instruction::Load: |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1084 | if (MDNode *MD = cast<LoadInst>(I)->getMetadata(LLVMContext::MD_range)) |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 1085 | computeKnownBitsFromRangeMetadata(*MD, KnownZero); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1086 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1087 | case Instruction::And: { |
| 1088 | // If either the LHS or the RHS are Zero, the result is zero. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1089 | computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q); |
| 1090 | computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1091 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1092 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 1093 | KnownOne &= KnownOne2; |
| 1094 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 1095 | KnownZero |= KnownZero2; |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1096 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1097 | } |
| 1098 | case Instruction::Or: { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1099 | computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q); |
| 1100 | computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1102 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 1103 | KnownZero &= KnownZero2; |
| 1104 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 1105 | KnownOne |= KnownOne2; |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1106 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1107 | } |
| 1108 | case Instruction::Xor: { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1109 | computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q); |
| 1110 | computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1112 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 1113 | APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 1114 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 1115 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 1116 | KnownZero = KnownZeroOut; |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1117 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1118 | } |
| 1119 | case Instruction::Mul: { |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 1120 | bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1121 | computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, KnownZero, |
| 1122 | KnownOne, KnownZero2, KnownOne2, DL, Depth, Q); |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 1123 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1124 | } |
| 1125 | case Instruction::UDiv: { |
| 1126 | // For the purposes of computing leading zeros we can conservatively |
| 1127 | // treat a udiv as a logical right shift by the power of 2 known to |
| 1128 | // be less than the denominator. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1129 | computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1130 | unsigned LeadZ = KnownZero2.countLeadingOnes(); |
| 1131 | |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 1132 | KnownOne2.clearAllBits(); |
| 1133 | KnownZero2.clearAllBits(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1134 | computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1135 | unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros(); |
| 1136 | if (RHSUnknownLeadingOnes != BitWidth) |
| 1137 | LeadZ = std::min(BitWidth, |
| 1138 | LeadZ + BitWidth - RHSUnknownLeadingOnes - 1); |
| 1139 | |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1140 | KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1141 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1142 | } |
| 1143 | case Instruction::Select: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1144 | computeKnownBits(I->getOperand(2), KnownZero, KnownOne, DL, Depth + 1, Q); |
| 1145 | computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1146 | |
| 1147 | // Only known if known in both the LHS and RHS. |
| 1148 | KnownOne &= KnownOne2; |
| 1149 | KnownZero &= KnownZero2; |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1150 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1151 | case Instruction::FPTrunc: |
| 1152 | case Instruction::FPExt: |
| 1153 | case Instruction::FPToUI: |
| 1154 | case Instruction::FPToSI: |
| 1155 | case Instruction::SIToFP: |
| 1156 | case Instruction::UIToFP: |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1157 | break; // Can't work with floating point. |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1158 | case Instruction::PtrToInt: |
| 1159 | case Instruction::IntToPtr: |
Matt Arsenault | f1a7e62 | 2014-07-15 01:55:03 +0000 | [diff] [blame] | 1160 | case Instruction::AddrSpaceCast: // Pointers could be different sizes. |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1161 | // FALL THROUGH and handle them the same as zext/trunc. |
| 1162 | case Instruction::ZExt: |
| 1163 | case Instruction::Trunc: { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1164 | Type *SrcTy = I->getOperand(0)->getType(); |
Nadav Rotem | 15198e9 | 2012-10-26 17:17:05 +0000 | [diff] [blame] | 1165 | |
Chris Lattner | 0cdbc7a | 2009-09-08 00:13:52 +0000 | [diff] [blame] | 1166 | unsigned SrcBitWidth; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1167 | // Note that we handle pointer operands here because of inttoptr/ptrtoint |
| 1168 | // which fall through here. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1169 | SrcBitWidth = DL.getTypeSizeInBits(SrcTy->getScalarType()); |
Nadav Rotem | 15198e9 | 2012-10-26 17:17:05 +0000 | [diff] [blame] | 1170 | |
| 1171 | assert(SrcBitWidth && "SrcBitWidth can't be zero"); |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1172 | KnownZero = KnownZero.zextOrTrunc(SrcBitWidth); |
| 1173 | KnownOne = KnownOne.zextOrTrunc(SrcBitWidth); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1174 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q); |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1175 | KnownZero = KnownZero.zextOrTrunc(BitWidth); |
| 1176 | KnownOne = KnownOne.zextOrTrunc(BitWidth); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1177 | // Any top bits are known to be zero. |
| 1178 | if (BitWidth > SrcBitWidth) |
| 1179 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1180 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1181 | } |
| 1182 | case Instruction::BitCast: { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1183 | Type *SrcTy = I->getOperand(0)->getType(); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1184 | if ((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
Chris Lattner | edb8407 | 2009-07-02 16:04:08 +0000 | [diff] [blame] | 1185 | // TODO: For now, not handling conversions like: |
| 1186 | // (bitcast i64 %x to <2 x i32>) |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1187 | !I->getType()->isVectorTy()) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1188 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1189 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1190 | } |
| 1191 | break; |
| 1192 | } |
| 1193 | case Instruction::SExt: { |
| 1194 | // Compute the bits in the result that are not present in the input. |
Chris Lattner | 0cdbc7a | 2009-09-08 00:13:52 +0000 | [diff] [blame] | 1195 | unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits(); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1196 | |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1197 | KnownZero = KnownZero.trunc(SrcBitWidth); |
| 1198 | KnownOne = KnownOne.trunc(SrcBitWidth); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1199 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q); |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1200 | KnownZero = KnownZero.zext(BitWidth); |
| 1201 | KnownOne = KnownOne.zext(BitWidth); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1202 | |
| 1203 | // If the sign bit of the input is known set or clear, then we know the |
| 1204 | // top bits of the result. |
| 1205 | if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero |
| 1206 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
| 1207 | else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set |
| 1208 | KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1209 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1210 | } |
| 1211 | case Instruction::Shl: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1212 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1213 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1214 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1215 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1216 | KnownZero <<= ShiftAmt; |
| 1217 | KnownOne <<= ShiftAmt; |
| 1218 | KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0 |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1219 | } |
| 1220 | break; |
| 1221 | case Instruction::LShr: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1222 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1223 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1224 | // Compute the new bits that are at the top now. |
| 1225 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1226 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1227 | // Unsigned shift right. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1228 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1229 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 1230 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
| 1231 | // high bits known zero. |
| 1232 | KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1233 | } |
| 1234 | break; |
| 1235 | case Instruction::AShr: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1236 | // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1237 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1238 | // Compute the new bits that are at the top now. |
Chris Lattner | c86e67e | 2011-01-04 18:19:15 +0000 | [diff] [blame] | 1239 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1240 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1241 | // Signed shift right. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1242 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1243 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 1244 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1245 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1246 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
| 1247 | if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero. |
| 1248 | KnownZero |= HighBits; |
| 1249 | else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one. |
| 1250 | KnownOne |= HighBits; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1251 | } |
| 1252 | break; |
| 1253 | case Instruction::Sub: { |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1254 | bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap(); |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1255 | computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1256 | KnownZero, KnownOne, KnownZero2, KnownOne2, DL, |
| 1257 | Depth, Q); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1258 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1259 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1260 | case Instruction::Add: { |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1261 | bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap(); |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1262 | computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1263 | KnownZero, KnownOne, KnownZero2, KnownOne2, DL, |
| 1264 | Depth, Q); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1265 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1266 | } |
| 1267 | case Instruction::SRem: |
| 1268 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Duncan Sands | 26cd6bd | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 1269 | APInt RA = Rem->getValue().abs(); |
| 1270 | if (RA.isPowerOf2()) { |
| 1271 | APInt LowBits = RA - 1; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1272 | computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, |
| 1273 | Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1274 | |
Duncan Sands | 26cd6bd | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 1275 | // The low bits of the first operand are unchanged by the srem. |
| 1276 | KnownZero = KnownZero2 & LowBits; |
| 1277 | KnownOne = KnownOne2 & LowBits; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1278 | |
Duncan Sands | 26cd6bd | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 1279 | // If the first operand is non-negative or has all low bits zero, then |
| 1280 | // the upper bits are all zero. |
| 1281 | if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits)) |
| 1282 | KnownZero |= ~LowBits; |
| 1283 | |
| 1284 | // If the first operand is negative and not all low bits are zero, then |
| 1285 | // the upper bits are all one. |
| 1286 | if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0)) |
| 1287 | KnownOne |= ~LowBits; |
| 1288 | |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1289 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1290 | } |
| 1291 | } |
Nick Lewycky | e467979 | 2011-03-07 01:50:10 +0000 | [diff] [blame] | 1292 | |
| 1293 | // The sign bit is the LHS's sign bit, except when the result of the |
| 1294 | // remainder is zero. |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1295 | if (KnownZero.isNonNegative()) { |
Nick Lewycky | e467979 | 2011-03-07 01:50:10 +0000 | [diff] [blame] | 1296 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1297 | computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, DL, |
| 1298 | Depth + 1, Q); |
Nick Lewycky | e467979 | 2011-03-07 01:50:10 +0000 | [diff] [blame] | 1299 | // If it's known zero, our sign bit is also zero. |
| 1300 | if (LHSKnownZero.isNegative()) |
Duncan Sands | 34c4869 | 2012-04-30 11:56:58 +0000 | [diff] [blame] | 1301 | KnownZero.setBit(BitWidth - 1); |
Nick Lewycky | e467979 | 2011-03-07 01:50:10 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1304 | break; |
| 1305 | case Instruction::URem: { |
| 1306 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1307 | APInt RA = Rem->getValue(); |
| 1308 | if (RA.isPowerOf2()) { |
| 1309 | APInt LowBits = (RA - 1); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1310 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, |
| 1311 | Q); |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1312 | KnownZero |= ~LowBits; |
| 1313 | KnownOne &= LowBits; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1314 | break; |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | // Since the result is less than or equal to either operand, any leading |
| 1319 | // zero bits in either operand must also exist in the result. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1320 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q); |
| 1321 | computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1322 | |
Chris Lattner | 4612ae1 | 2009-01-20 18:22:57 +0000 | [diff] [blame] | 1323 | unsigned Leaders = std::max(KnownZero.countLeadingOnes(), |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1324 | KnownZero2.countLeadingOnes()); |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 1325 | KnownOne.clearAllBits(); |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1326 | KnownZero = APInt::getHighBitsSet(BitWidth, Leaders); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1327 | break; |
| 1328 | } |
| 1329 | |
Victor Hernandez | a3aaf85 | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 1330 | case Instruction::Alloca: { |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1331 | AllocaInst *AI = cast<AllocaInst>(V); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1332 | unsigned Align = AI->getAlignment(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1333 | if (Align == 0) |
| 1334 | Align = DL.getABITypeAlignment(AI->getType()->getElementType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1335 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1336 | if (Align > 0) |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1337 | KnownZero = APInt::getLowBitsSet(BitWidth, countTrailingZeros(Align)); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1338 | break; |
| 1339 | } |
| 1340 | case Instruction::GetElementPtr: { |
| 1341 | // Analyze all of the subscripts of this getelementptr instruction |
| 1342 | // to determine if we can prove known low zero bits. |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1343 | APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1344 | computeKnownBits(I->getOperand(0), LocalKnownZero, LocalKnownOne, DL, |
| 1345 | Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1346 | unsigned TrailZ = LocalKnownZero.countTrailingOnes(); |
| 1347 | |
| 1348 | gep_type_iterator GTI = gep_type_begin(I); |
| 1349 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) { |
| 1350 | Value *Index = I->getOperand(i); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1351 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1352 | // Handle struct member offset arithmetic. |
Matt Arsenault | 74742a1 | 2013-08-19 21:43:16 +0000 | [diff] [blame] | 1353 | |
| 1354 | // Handle case when index is vector zeroinitializer |
| 1355 | Constant *CIndex = cast<Constant>(Index); |
| 1356 | if (CIndex->isZeroValue()) |
| 1357 | continue; |
| 1358 | |
| 1359 | if (CIndex->getType()->isVectorTy()) |
| 1360 | Index = CIndex->getSplatValue(); |
| 1361 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1362 | unsigned Idx = cast<ConstantInt>(Index)->getZExtValue(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1363 | const StructLayout *SL = DL.getStructLayout(STy); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1364 | uint64_t Offset = SL->getElementOffset(Idx); |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1365 | TrailZ = std::min<unsigned>(TrailZ, |
| 1366 | countTrailingZeros(Offset)); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1367 | } else { |
| 1368 | // Handle array index arithmetic. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1369 | Type *IndexedTy = GTI.getIndexedType(); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1370 | if (!IndexedTy->isSized()) { |
| 1371 | TrailZ = 0; |
| 1372 | break; |
| 1373 | } |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1374 | unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1375 | uint64_t TypeSize = DL.getTypeAllocSize(IndexedTy); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1376 | LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1377 | computeKnownBits(Index, LocalKnownZero, LocalKnownOne, DL, Depth + 1, |
| 1378 | Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1379 | TrailZ = std::min(TrailZ, |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1380 | unsigned(countTrailingZeros(TypeSize) + |
Chris Lattner | 4612ae1 | 2009-01-20 18:22:57 +0000 | [diff] [blame] | 1381 | LocalKnownZero.countTrailingOnes())); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1384 | |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1385 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1386 | break; |
| 1387 | } |
| 1388 | case Instruction::PHI: { |
| 1389 | PHINode *P = cast<PHINode>(I); |
| 1390 | // Handle the case of a simple two-predecessor recurrence PHI. |
| 1391 | // There's a lot more that could theoretically be done here, but |
| 1392 | // this is sufficient to catch some interesting cases. |
| 1393 | if (P->getNumIncomingValues() == 2) { |
| 1394 | for (unsigned i = 0; i != 2; ++i) { |
| 1395 | Value *L = P->getIncomingValue(i); |
| 1396 | Value *R = P->getIncomingValue(!i); |
Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 1397 | Operator *LU = dyn_cast<Operator>(L); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1398 | if (!LU) |
| 1399 | continue; |
Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 1400 | unsigned Opcode = LU->getOpcode(); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1401 | // Check for operations that have the property that if |
| 1402 | // both their operands have low zero bits, the result |
| 1403 | // will have low zero bits. |
| 1404 | if (Opcode == Instruction::Add || |
| 1405 | Opcode == Instruction::Sub || |
| 1406 | Opcode == Instruction::And || |
| 1407 | Opcode == Instruction::Or || |
| 1408 | Opcode == Instruction::Mul) { |
| 1409 | Value *LL = LU->getOperand(0); |
| 1410 | Value *LR = LU->getOperand(1); |
| 1411 | // Find a recurrence. |
| 1412 | if (LL == I) |
| 1413 | L = LR; |
| 1414 | else if (LR == I) |
| 1415 | L = LL; |
| 1416 | else |
| 1417 | break; |
| 1418 | // Ok, we have a PHI of the form L op= R. Check for low |
| 1419 | // zero bits. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1420 | computeKnownBits(R, KnownZero2, KnownOne2, DL, Depth + 1, Q); |
David Greene | aebd9e0 | 2008-10-27 23:24:03 +0000 | [diff] [blame] | 1421 | |
| 1422 | // We need to take the minimum number of known bits |
| 1423 | APInt KnownZero3(KnownZero), KnownOne3(KnownOne); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1424 | computeKnownBits(L, KnownZero3, KnownOne3, DL, Depth + 1, Q); |
David Greene | aebd9e0 | 2008-10-27 23:24:03 +0000 | [diff] [blame] | 1425 | |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1426 | KnownZero = APInt::getLowBitsSet(BitWidth, |
David Greene | aebd9e0 | 2008-10-27 23:24:03 +0000 | [diff] [blame] | 1427 | std::min(KnownZero2.countTrailingOnes(), |
| 1428 | KnownZero3.countTrailingOnes())); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1429 | break; |
| 1430 | } |
| 1431 | } |
| 1432 | } |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1433 | |
Nick Lewycky | ac0b62c | 2011-02-10 23:54:10 +0000 | [diff] [blame] | 1434 | // Unreachable blocks may have zero-operand PHI nodes. |
| 1435 | if (P->getNumIncomingValues() == 0) |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1436 | break; |
Nick Lewycky | ac0b62c | 2011-02-10 23:54:10 +0000 | [diff] [blame] | 1437 | |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1438 | // Otherwise take the unions of the known bit sets of the operands, |
| 1439 | // taking conservative care to avoid excessive recursion. |
| 1440 | if (Depth < MaxDepth - 1 && !KnownZero && !KnownOne) { |
Duncan Sands | 7dc3d47 | 2011-03-08 12:39:03 +0000 | [diff] [blame] | 1441 | // Skip if every incoming value references to ourself. |
Nuno Lopes | 0d44a50 | 2012-07-03 21:15:40 +0000 | [diff] [blame] | 1442 | if (dyn_cast_or_null<UndefValue>(P->hasConstantValue())) |
Duncan Sands | 7dc3d47 | 2011-03-08 12:39:03 +0000 | [diff] [blame] | 1443 | break; |
| 1444 | |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1445 | KnownZero = APInt::getAllOnesValue(BitWidth); |
| 1446 | KnownOne = APInt::getAllOnesValue(BitWidth); |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 1447 | for (Value *IncValue : P->incoming_values()) { |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1448 | // Skip direct self references. |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 1449 | if (IncValue == P) continue; |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1450 | |
| 1451 | KnownZero2 = APInt(BitWidth, 0); |
| 1452 | KnownOne2 = APInt(BitWidth, 0); |
| 1453 | // Recurse, but cap the recursion to one level, because we don't |
| 1454 | // want to waste time spinning around in loops. |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 1455 | computeKnownBits(IncValue, KnownZero2, KnownOne2, DL, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1456 | MaxDepth - 1, Q); |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1457 | KnownZero &= KnownZero2; |
| 1458 | KnownOne &= KnownOne2; |
| 1459 | // If all bits have been ruled out, there's no need to check |
| 1460 | // more operands. |
| 1461 | if (!KnownZero && !KnownOne) |
| 1462 | break; |
| 1463 | } |
| 1464 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1465 | break; |
| 1466 | } |
| 1467 | case Instruction::Call: |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 1468 | case Instruction::Invoke: |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1469 | if (MDNode *MD = cast<Instruction>(I)->getMetadata(LLVMContext::MD_range)) |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 1470 | computeKnownBitsFromRangeMetadata(*MD, KnownZero); |
| 1471 | // If a range metadata is attached to this IntrinsicInst, intersect the |
| 1472 | // explicit range specified by the metadata and the implicit range of |
| 1473 | // the intrinsic. |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1474 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 1475 | switch (II->getIntrinsicID()) { |
| 1476 | default: break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1477 | case Intrinsic::ctlz: |
| 1478 | case Intrinsic::cttz: { |
| 1479 | unsigned LowBits = Log2_32(BitWidth)+1; |
Benjamin Kramer | 4ee5747 | 2011-12-24 17:31:46 +0000 | [diff] [blame] | 1480 | // If this call is undefined for 0, the result will be less than 2^n. |
| 1481 | if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext())) |
| 1482 | LowBits -= 1; |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 1483 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - LowBits); |
Benjamin Kramer | 4ee5747 | 2011-12-24 17:31:46 +0000 | [diff] [blame] | 1484 | break; |
| 1485 | } |
| 1486 | case Intrinsic::ctpop: { |
| 1487 | unsigned LowBits = Log2_32(BitWidth)+1; |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 1488 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - LowBits); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1489 | break; |
| 1490 | } |
Chad Rosier | b362884 | 2011-05-26 23:13:19 +0000 | [diff] [blame] | 1491 | case Intrinsic::x86_sse42_crc32_64_64: |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 1492 | KnownZero |= APInt::getHighBitsSet(64, 32); |
Evan Cheng | 2a746bf | 2011-05-22 18:25:30 +0000 | [diff] [blame] | 1493 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1494 | } |
| 1495 | } |
| 1496 | break; |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1497 | case Instruction::ExtractValue: |
| 1498 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) { |
| 1499 | ExtractValueInst *EVI = cast<ExtractValueInst>(I); |
| 1500 | if (EVI->getNumIndices() != 1) break; |
| 1501 | if (EVI->getIndices()[0] == 0) { |
| 1502 | switch (II->getIntrinsicID()) { |
| 1503 | default: break; |
| 1504 | case Intrinsic::uadd_with_overflow: |
| 1505 | case Intrinsic::sadd_with_overflow: |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1506 | computeKnownBitsAddSub(true, II->getArgOperand(0), |
| 1507 | II->getArgOperand(1), false, KnownZero, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1508 | KnownOne, KnownZero2, KnownOne2, DL, Depth, Q); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1509 | break; |
| 1510 | case Intrinsic::usub_with_overflow: |
| 1511 | case Intrinsic::ssub_with_overflow: |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1512 | computeKnownBitsAddSub(false, II->getArgOperand(0), |
| 1513 | II->getArgOperand(1), false, KnownZero, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1514 | KnownOne, KnownZero2, KnownOne2, DL, Depth, Q); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1515 | break; |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 1516 | case Intrinsic::umul_with_overflow: |
| 1517 | case Intrinsic::smul_with_overflow: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1518 | computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false, |
| 1519 | KnownZero, KnownOne, KnownZero2, KnownOne2, DL, |
| 1520 | Depth, Q); |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 1521 | break; |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1522 | } |
| 1523 | } |
| 1524 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1525 | } |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1526 | |
| 1527 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1530 | /// Determine whether the sign bit is known to be zero or one. |
| 1531 | /// Convenience wrapper around computeKnownBits. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1532 | void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1533 | const DataLayout &DL, unsigned Depth, const Query &Q) { |
| 1534 | unsigned BitWidth = getBitWidth(V->getType(), DL); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1535 | if (!BitWidth) { |
| 1536 | KnownZero = false; |
| 1537 | KnownOne = false; |
| 1538 | return; |
| 1539 | } |
| 1540 | APInt ZeroBits(BitWidth, 0); |
| 1541 | APInt OneBits(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1542 | computeKnownBits(V, ZeroBits, OneBits, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1543 | KnownOne = OneBits[BitWidth - 1]; |
| 1544 | KnownZero = ZeroBits[BitWidth - 1]; |
| 1545 | } |
| 1546 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1547 | /// Return true if the given value is known to have exactly one |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1548 | /// bit set when defined. For vectors return true if every element is known to |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1549 | /// be a power of two when defined. Supports values with integer or pointer |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1550 | /// types and vectors of integers. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1551 | bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero, unsigned Depth, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1552 | const Query &Q, const DataLayout &DL) { |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1553 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 1554 | if (C->isNullValue()) |
| 1555 | return OrZero; |
| 1556 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 1557 | return CI->getValue().isPowerOf2(); |
| 1558 | // TODO: Handle vector constants. |
| 1559 | } |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1560 | |
| 1561 | // 1 << X is clearly a power of two if the one is not shifted off the end. If |
| 1562 | // it is shifted off the end then the result is undefined. |
| 1563 | if (match(V, m_Shl(m_One(), m_Value()))) |
| 1564 | return true; |
| 1565 | |
| 1566 | // (signbit) >>l X is clearly a power of two if the one is not shifted off the |
| 1567 | // bottom. If it is shifted off the bottom then the result is undefined. |
Duncan Sands | 4b397fc | 2011-02-01 08:50:33 +0000 | [diff] [blame] | 1568 | if (match(V, m_LShr(m_SignBit(), m_Value()))) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1569 | return true; |
| 1570 | |
| 1571 | // The remaining tests are all recursive, so bail out if we hit the limit. |
| 1572 | if (Depth++ == MaxDepth) |
| 1573 | return false; |
| 1574 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1575 | Value *X = nullptr, *Y = nullptr; |
Duncan Sands | 985ba63 | 2011-10-28 18:30:05 +0000 | [diff] [blame] | 1576 | // A shift of a power of two is a power of two or zero. |
| 1577 | if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) || |
| 1578 | match(V, m_Shr(m_Value(X), m_Value())))) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1579 | return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q, DL); |
Duncan Sands | 985ba63 | 2011-10-28 18:30:05 +0000 | [diff] [blame] | 1580 | |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1581 | if (ZExtInst *ZI = dyn_cast<ZExtInst>(V)) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1582 | return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q, DL); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1583 | |
| 1584 | if (SelectInst *SI = dyn_cast<SelectInst>(V)) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1585 | return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q, DL) && |
| 1586 | isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q, DL); |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1587 | |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1588 | if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) { |
| 1589 | // A power of two and'd with anything is a power of two or zero. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1590 | if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q, DL) || |
| 1591 | isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q, DL)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1592 | return true; |
| 1593 | // X & (-X) is always a power of two or zero. |
| 1594 | if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X)))) |
| 1595 | return true; |
| 1596 | return false; |
| 1597 | } |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1598 | |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1599 | // Adding a power-of-two or zero to the same power-of-two or zero yields |
| 1600 | // either the original power-of-two, a larger power-of-two or zero. |
| 1601 | if (match(V, m_Add(m_Value(X), m_Value(Y)))) { |
| 1602 | OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V); |
| 1603 | if (OrZero || VOBO->hasNoUnsignedWrap() || VOBO->hasNoSignedWrap()) { |
| 1604 | if (match(X, m_And(m_Specific(Y), m_Value())) || |
| 1605 | match(X, m_And(m_Value(), m_Specific(Y)))) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1606 | if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q, DL)) |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1607 | return true; |
| 1608 | if (match(Y, m_And(m_Specific(X), m_Value())) || |
| 1609 | match(Y, m_And(m_Value(), m_Specific(X)))) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1610 | if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q, DL)) |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1611 | return true; |
| 1612 | |
| 1613 | unsigned BitWidth = V->getType()->getScalarSizeInBits(); |
| 1614 | APInt LHSZeroBits(BitWidth, 0), LHSOneBits(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1615 | computeKnownBits(X, LHSZeroBits, LHSOneBits, DL, Depth, Q); |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1616 | |
| 1617 | APInt RHSZeroBits(BitWidth, 0), RHSOneBits(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1618 | computeKnownBits(Y, RHSZeroBits, RHSOneBits, DL, Depth, Q); |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1619 | // If i8 V is a power of two or zero: |
| 1620 | // ZeroBits: 1 1 1 0 1 1 1 1 |
| 1621 | // ~ZeroBits: 0 0 0 1 0 0 0 0 |
| 1622 | if ((~(LHSZeroBits & RHSZeroBits)).isPowerOf2()) |
| 1623 | // If OrZero isn't set, we cannot give back a zero result. |
| 1624 | // Make sure either the LHS or RHS has a bit set. |
| 1625 | if (OrZero || RHSOneBits.getBoolValue() || LHSOneBits.getBoolValue()) |
| 1626 | return true; |
| 1627 | } |
| 1628 | } |
David Majnemer | beab567 | 2013-05-18 19:30:37 +0000 | [diff] [blame] | 1629 | |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1630 | // An exact divide or right shift can only shift off zero bits, so the result |
Nick Lewycky | f0469af | 2011-03-21 21:40:32 +0000 | [diff] [blame] | 1631 | // is a power of two only if the first operand is a power of two and not |
| 1632 | // copying a sign bit (sdiv int_min, 2). |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1633 | if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) || |
| 1634 | match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1635 | return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1636 | Depth, Q, DL); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1639 | return false; |
| 1640 | } |
| 1641 | |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1642 | /// \brief Test whether a GEP's result is known to be non-null. |
| 1643 | /// |
| 1644 | /// Uses properties inherent in a GEP to try to determine whether it is known |
| 1645 | /// to be non-null. |
| 1646 | /// |
| 1647 | /// Currently this routine does not support vector GEPs. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1648 | static bool isGEPKnownNonNull(GEPOperator *GEP, const DataLayout &DL, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1649 | unsigned Depth, const Query &Q) { |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1650 | if (!GEP->isInBounds() || GEP->getPointerAddressSpace() != 0) |
| 1651 | return false; |
| 1652 | |
| 1653 | // FIXME: Support vector-GEPs. |
| 1654 | assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP"); |
| 1655 | |
| 1656 | // If the base pointer is non-null, we cannot walk to a null address with an |
| 1657 | // inbounds GEP in address space zero. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1658 | if (isKnownNonZero(GEP->getPointerOperand(), DL, Depth, Q)) |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1659 | return true; |
| 1660 | |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1661 | // Walk the GEP operands and see if any operand introduces a non-zero offset. |
| 1662 | // If so, then the GEP cannot produce a null pointer, as doing so would |
| 1663 | // inherently violate the inbounds contract within address space zero. |
| 1664 | for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP); |
| 1665 | GTI != GTE; ++GTI) { |
| 1666 | // Struct types are easy -- they must always be indexed by a constant. |
| 1667 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 1668 | ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand()); |
| 1669 | unsigned ElementIdx = OpC->getZExtValue(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1670 | const StructLayout *SL = DL.getStructLayout(STy); |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1671 | uint64_t ElementOffset = SL->getElementOffset(ElementIdx); |
| 1672 | if (ElementOffset > 0) |
| 1673 | return true; |
| 1674 | continue; |
| 1675 | } |
| 1676 | |
| 1677 | // If we have a zero-sized type, the index doesn't matter. Keep looping. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1678 | if (DL.getTypeAllocSize(GTI.getIndexedType()) == 0) |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1679 | continue; |
| 1680 | |
| 1681 | // Fast path the constant operand case both for efficiency and so we don't |
| 1682 | // increment Depth when just zipping down an all-constant GEP. |
| 1683 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) { |
| 1684 | if (!OpC->isZero()) |
| 1685 | return true; |
| 1686 | continue; |
| 1687 | } |
| 1688 | |
| 1689 | // We post-increment Depth here because while isKnownNonZero increments it |
| 1690 | // as well, when we pop back up that increment won't persist. We don't want |
| 1691 | // to recurse 10k times just because we have 10k GEP operands. We don't |
| 1692 | // bail completely out because we want to handle constant GEPs regardless |
| 1693 | // of depth. |
| 1694 | if (Depth++ >= MaxDepth) |
| 1695 | continue; |
| 1696 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1697 | if (isKnownNonZero(GTI.getOperand(), DL, Depth, Q)) |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1698 | return true; |
| 1699 | } |
| 1700 | |
| 1701 | return false; |
| 1702 | } |
| 1703 | |
Philip Reames | 4cb4d3e | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1704 | /// Does the 'Range' metadata (which must be a valid MD_range operand list) |
| 1705 | /// ensure that the value it's attached to is never Value? 'RangeType' is |
| 1706 | /// is the type of the value described by the range. |
| 1707 | static bool rangeMetadataExcludesValue(MDNode* Ranges, |
| 1708 | const APInt& Value) { |
| 1709 | const unsigned NumRanges = Ranges->getNumOperands() / 2; |
| 1710 | assert(NumRanges >= 1); |
| 1711 | for (unsigned i = 0; i < NumRanges; ++i) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1712 | ConstantInt *Lower = |
| 1713 | mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0)); |
| 1714 | ConstantInt *Upper = |
| 1715 | mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1)); |
Philip Reames | 4cb4d3e | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1716 | ConstantRange Range(Lower->getValue(), Upper->getValue()); |
| 1717 | if (Range.contains(Value)) |
| 1718 | return false; |
| 1719 | } |
| 1720 | return true; |
| 1721 | } |
| 1722 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1723 | /// Return true if the given value is known to be non-zero when defined. |
| 1724 | /// For vectors return true if every element is known to be non-zero when |
| 1725 | /// defined. Supports values with integer or pointer type and vectors of |
| 1726 | /// integers. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1727 | bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1728 | const Query &Q) { |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1729 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 1730 | if (C->isNullValue()) |
| 1731 | return false; |
| 1732 | if (isa<ConstantInt>(C)) |
| 1733 | // Must be non-zero due to null test above. |
| 1734 | return true; |
| 1735 | // TODO: Handle vectors |
| 1736 | return false; |
| 1737 | } |
| 1738 | |
Philip Reames | 4cb4d3e | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1739 | if (Instruction* I = dyn_cast<Instruction>(V)) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1740 | if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range)) { |
Philip Reames | 4cb4d3e | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1741 | // If the possible ranges don't contain zero, then the value is |
| 1742 | // definitely non-zero. |
| 1743 | if (IntegerType* Ty = dyn_cast<IntegerType>(V->getType())) { |
| 1744 | const APInt ZeroValue(Ty->getBitWidth(), 0); |
| 1745 | if (rangeMetadataExcludesValue(Ranges, ZeroValue)) |
| 1746 | return true; |
| 1747 | } |
| 1748 | } |
| 1749 | } |
| 1750 | |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1751 | // The remaining tests are all recursive, so bail out if we hit the limit. |
Duncan Sands | 7cb61e5 | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 1752 | if (Depth++ >= MaxDepth) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1753 | return false; |
| 1754 | |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1755 | // Check for pointer simplifications. |
| 1756 | if (V->getType()->isPointerTy()) { |
Manman Ren | 1217112 | 2013-03-18 21:23:25 +0000 | [diff] [blame] | 1757 | if (isKnownNonNull(V)) |
| 1758 | return true; |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1759 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1760 | if (isGEPKnownNonNull(GEP, DL, Depth, Q)) |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1761 | return true; |
| 1762 | } |
| 1763 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1764 | unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), DL); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1765 | |
| 1766 | // X | Y != 0 if X != 0 or Y != 0. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1767 | Value *X = nullptr, *Y = nullptr; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1768 | if (match(V, m_Or(m_Value(X), m_Value(Y)))) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1769 | return isKnownNonZero(X, DL, Depth, Q) || isKnownNonZero(Y, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1770 | |
| 1771 | // ext X != 0 if X != 0. |
| 1772 | if (isa<SExtInst>(V) || isa<ZExtInst>(V)) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1773 | return isKnownNonZero(cast<Instruction>(V)->getOperand(0), DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1774 | |
Duncan Sands | 2e9e4f1 | 2011-01-29 13:27:00 +0000 | [diff] [blame] | 1775 | // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1776 | // if the lowest bit is shifted off the end. |
| 1777 | if (BitWidth && match(V, m_Shl(m_Value(X), m_Value(Y)))) { |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1778 | // shl nuw can't remove any non-zero bits. |
Duncan Sands | 7cb61e5 | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 1779 | OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1780 | if (BO->hasNoUnsignedWrap()) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1781 | return isKnownNonZero(X, DL, Depth, Q); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1782 | |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1783 | APInt KnownZero(BitWidth, 0); |
| 1784 | APInt KnownOne(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1785 | computeKnownBits(X, KnownZero, KnownOne, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1786 | if (KnownOne[0]) |
| 1787 | return true; |
| 1788 | } |
Duncan Sands | 2e9e4f1 | 2011-01-29 13:27:00 +0000 | [diff] [blame] | 1789 | // shr X, Y != 0 if X is negative. Note that the value of the shift is not |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1790 | // defined if the sign bit is shifted off the end. |
| 1791 | else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) { |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1792 | // shr exact can only shift out zero bits. |
Duncan Sands | 7cb61e5 | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 1793 | PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1794 | if (BO->isExact()) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1795 | return isKnownNonZero(X, DL, Depth, Q); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1796 | |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1797 | bool XKnownNonNegative, XKnownNegative; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1798 | ComputeSignBit(X, XKnownNonNegative, XKnownNegative, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1799 | if (XKnownNegative) |
| 1800 | return true; |
| 1801 | } |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1802 | // div exact can only produce a zero if the dividend is zero. |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1803 | else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1804 | return isKnownNonZero(X, DL, Depth, Q); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1805 | } |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1806 | // X + Y. |
| 1807 | else if (match(V, m_Add(m_Value(X), m_Value(Y)))) { |
| 1808 | bool XKnownNonNegative, XKnownNegative; |
| 1809 | bool YKnownNonNegative, YKnownNegative; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1810 | ComputeSignBit(X, XKnownNonNegative, XKnownNegative, DL, Depth, Q); |
| 1811 | ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1812 | |
| 1813 | // If X and Y are both non-negative (as signed values) then their sum is not |
Duncan Sands | 9e9d5b2 | 2011-01-25 15:14:15 +0000 | [diff] [blame] | 1814 | // zero unless both X and Y are zero. |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1815 | if (XKnownNonNegative && YKnownNonNegative) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1816 | if (isKnownNonZero(X, DL, Depth, Q) || isKnownNonZero(Y, DL, Depth, Q)) |
Duncan Sands | 9e9d5b2 | 2011-01-25 15:14:15 +0000 | [diff] [blame] | 1817 | return true; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1818 | |
| 1819 | // If X and Y are both negative (as signed values) then their sum is not |
| 1820 | // zero unless both X and Y equal INT_MIN. |
| 1821 | if (BitWidth && XKnownNegative && YKnownNegative) { |
| 1822 | APInt KnownZero(BitWidth, 0); |
| 1823 | APInt KnownOne(BitWidth, 0); |
| 1824 | APInt Mask = APInt::getSignedMaxValue(BitWidth); |
| 1825 | // The sign bit of X is set. If some other bit is set then X is not equal |
| 1826 | // to INT_MIN. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1827 | computeKnownBits(X, KnownZero, KnownOne, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1828 | if ((KnownOne & Mask) != 0) |
| 1829 | return true; |
| 1830 | // The sign bit of Y is set. If some other bit is set then Y is not equal |
| 1831 | // to INT_MIN. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1832 | computeKnownBits(Y, KnownZero, KnownOne, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1833 | if ((KnownOne & Mask) != 0) |
| 1834 | return true; |
| 1835 | } |
| 1836 | |
| 1837 | // The sum of a non-negative number and a power of two is not zero. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1838 | if (XKnownNonNegative && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1839 | isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q, DL)) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1840 | return true; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1841 | if (YKnownNonNegative && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1842 | isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q, DL)) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1843 | return true; |
| 1844 | } |
Duncan Sands | 7cb61e5 | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 1845 | // X * Y. |
| 1846 | else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) { |
| 1847 | OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V); |
| 1848 | // If X and Y are non-zero then so is X * Y as long as the multiplication |
| 1849 | // does not overflow. |
| 1850 | if ((BO->hasNoSignedWrap() || BO->hasNoUnsignedWrap()) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1851 | isKnownNonZero(X, DL, Depth, Q) && isKnownNonZero(Y, DL, Depth, Q)) |
Duncan Sands | 7cb61e5 | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 1852 | return true; |
| 1853 | } |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1854 | // (C ? X : Y) != 0 if X != 0 and Y != 0. |
| 1855 | else if (SelectInst *SI = dyn_cast<SelectInst>(V)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1856 | if (isKnownNonZero(SI->getTrueValue(), DL, Depth, Q) && |
| 1857 | isKnownNonZero(SI->getFalseValue(), DL, Depth, Q)) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1858 | return true; |
| 1859 | } |
| 1860 | |
| 1861 | if (!BitWidth) return false; |
| 1862 | APInt KnownZero(BitWidth, 0); |
| 1863 | APInt KnownOne(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1864 | computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1865 | return KnownOne != 0; |
| 1866 | } |
| 1867 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1868 | /// Return true if 'V & Mask' is known to be zero. We use this predicate to |
| 1869 | /// simplify operations downstream. Mask is known to be zero for bits that V |
| 1870 | /// cannot have. |
Chris Lattner | 4bc2825 | 2009-09-08 00:06:16 +0000 | [diff] [blame] | 1871 | /// |
| 1872 | /// This function is defined on values with integer type, values with pointer |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1873 | /// type, and vectors of integers. In the case |
Chris Lattner | 4bc2825 | 2009-09-08 00:06:16 +0000 | [diff] [blame] | 1874 | /// where V is a vector, the mask, known zero, and known one values are the |
| 1875 | /// same width as the vector element, and the bit is set only if it is true |
| 1876 | /// for all of the elements in the vector. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1877 | bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL, |
| 1878 | unsigned Depth, const Query &Q) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1879 | APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1880 | computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1881 | return (KnownZero & Mask) == Mask; |
| 1882 | } |
| 1883 | |
| 1884 | |
| 1885 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1886 | /// Return the number of times the sign bit of the register is replicated into |
| 1887 | /// the other bits. We know that at least 1 bit is always equal to the sign bit |
| 1888 | /// (itself), but other cases can give us information. For example, immediately |
| 1889 | /// after an "ashr X, 2", we know that the top 3 bits are all equal to each |
| 1890 | /// other, so we return 3. |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1891 | /// |
| 1892 | /// 'Op' must have a scalar integer type. |
| 1893 | /// |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1894 | unsigned ComputeNumSignBits(Value *V, const DataLayout &DL, unsigned Depth, |
| 1895 | const Query &Q) { |
| 1896 | unsigned TyBits = DL.getTypeSizeInBits(V->getType()->getScalarType()); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1897 | unsigned Tmp, Tmp2; |
| 1898 | unsigned FirstAnswer = 1; |
| 1899 | |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1900 | // Note that ConstantInt is handled by the general computeKnownBits case |
Chris Lattner | 2e01a69 | 2008-06-02 18:39:07 +0000 | [diff] [blame] | 1901 | // below. |
| 1902 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1903 | if (Depth == 6) |
| 1904 | return 1; // Limit search depth. |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1905 | |
Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 1906 | Operator *U = dyn_cast<Operator>(V); |
| 1907 | switch (Operator::getOpcode(V)) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1908 | default: break; |
| 1909 | case Instruction::SExt: |
Mon P Wang | bb3eac9 | 2009-12-02 04:59:58 +0000 | [diff] [blame] | 1910 | Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1911 | return ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q) + Tmp; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1912 | |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 1913 | case Instruction::SDiv: { |
Nadav Rotem | 029c5c7 | 2015-03-03 21:39:02 +0000 | [diff] [blame] | 1914 | const APInt *Denominator; |
| 1915 | // sdiv X, C -> adds log(C) sign bits. |
| 1916 | if (match(U->getOperand(1), m_APInt(Denominator))) { |
| 1917 | |
| 1918 | // Ignore non-positive denominator. |
| 1919 | if (!Denominator->isStrictlyPositive()) |
| 1920 | break; |
| 1921 | |
| 1922 | // Calculate the incoming numerator bits. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1923 | unsigned NumBits = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q); |
Nadav Rotem | 029c5c7 | 2015-03-03 21:39:02 +0000 | [diff] [blame] | 1924 | |
| 1925 | // Add floor(log(C)) bits to the numerator bits. |
| 1926 | return std::min(TyBits, NumBits + Denominator->logBase2()); |
| 1927 | } |
| 1928 | break; |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | case Instruction::SRem: { |
| 1932 | const APInt *Denominator; |
Sanjoy Das | e561fee | 2015-03-25 22:33:53 +0000 | [diff] [blame] | 1933 | // srem X, C -> we know that the result is within [-C+1,C) when C is a |
| 1934 | // positive constant. This let us put a lower bound on the number of sign |
| 1935 | // bits. |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 1936 | if (match(U->getOperand(1), m_APInt(Denominator))) { |
| 1937 | |
| 1938 | // Ignore non-positive denominator. |
| 1939 | if (!Denominator->isStrictlyPositive()) |
| 1940 | break; |
| 1941 | |
| 1942 | // Calculate the incoming numerator bits. SRem by a positive constant |
| 1943 | // can't lower the number of sign bits. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1944 | unsigned NumrBits = |
| 1945 | ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q); |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 1946 | |
| 1947 | // Calculate the leading sign bit constraints by examining the |
Sanjoy Das | e561fee | 2015-03-25 22:33:53 +0000 | [diff] [blame] | 1948 | // denominator. Given that the denominator is positive, there are two |
| 1949 | // cases: |
| 1950 | // |
| 1951 | // 1. the numerator is positive. The result range is [0,C) and [0,C) u< |
| 1952 | // (1 << ceilLogBase2(C)). |
| 1953 | // |
| 1954 | // 2. the numerator is negative. Then the result range is (-C,0] and |
| 1955 | // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)). |
| 1956 | // |
| 1957 | // Thus a lower bound on the number of sign bits is `TyBits - |
| 1958 | // ceilLogBase2(C)`. |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 1959 | |
Sanjoy Das | e561fee | 2015-03-25 22:33:53 +0000 | [diff] [blame] | 1960 | unsigned ResBits = TyBits - Denominator->ceilLogBase2(); |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 1961 | return std::max(NumrBits, ResBits); |
| 1962 | } |
| 1963 | break; |
| 1964 | } |
Nadav Rotem | 029c5c7 | 2015-03-03 21:39:02 +0000 | [diff] [blame] | 1965 | |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 1966 | case Instruction::AShr: { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1967 | Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q); |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 1968 | // ashr X, C -> adds C sign bits. Vectors too. |
| 1969 | const APInt *ShAmt; |
| 1970 | if (match(U->getOperand(1), m_APInt(ShAmt))) { |
| 1971 | Tmp += ShAmt->getZExtValue(); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1972 | if (Tmp > TyBits) Tmp = TyBits; |
| 1973 | } |
| 1974 | return Tmp; |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 1975 | } |
| 1976 | case Instruction::Shl: { |
| 1977 | const APInt *ShAmt; |
| 1978 | if (match(U->getOperand(1), m_APInt(ShAmt))) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1979 | // shl destroys sign bits. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1980 | Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q); |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 1981 | Tmp2 = ShAmt->getZExtValue(); |
| 1982 | if (Tmp2 >= TyBits || // Bad shift. |
| 1983 | Tmp2 >= Tmp) break; // Shifted all sign bits out. |
| 1984 | return Tmp - Tmp2; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1985 | } |
| 1986 | break; |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 1987 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1988 | case Instruction::And: |
| 1989 | case Instruction::Or: |
| 1990 | case Instruction::Xor: // NOT is handled here. |
| 1991 | // Logical binary ops preserve the number of sign bits at the worst. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1992 | Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1993 | if (Tmp != 1) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1994 | Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1995 | FirstAnswer = std::min(Tmp, Tmp2); |
| 1996 | // We computed what we know about the sign bits as our first |
| 1997 | // answer. Now proceed to the generic code that uses |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1998 | // computeKnownBits, and pick whichever answer is better. |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1999 | } |
| 2000 | break; |
| 2001 | |
| 2002 | case Instruction::Select: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2003 | Tmp = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2004 | if (Tmp == 1) return 1; // Early out. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2005 | Tmp2 = ComputeNumSignBits(U->getOperand(2), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2006 | return std::min(Tmp, Tmp2); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2007 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2008 | case Instruction::Add: |
| 2009 | // Add can have at most one carry bit. Thus we know that the output |
| 2010 | // is, at worst, one more bit than the inputs. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2011 | Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2012 | if (Tmp == 1) return 1; // Early out. |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2013 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2014 | // Special case decrementing a value (ADD X, -1): |
David Majnemer | a55027f | 2014-12-26 09:20:17 +0000 | [diff] [blame] | 2015 | if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1))) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2016 | if (CRHS->isAllOnesValue()) { |
| 2017 | APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2018 | computeKnownBits(U->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, |
| 2019 | Q); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2020 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2021 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 2022 | // sign bits set. |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2023 | if ((KnownZero | APInt(TyBits, 1)).isAllOnesValue()) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2024 | return TyBits; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2025 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2026 | // If we are subtracting one from a positive number, there is no carry |
| 2027 | // out of the result. |
| 2028 | if (KnownZero.isNegative()) |
| 2029 | return Tmp; |
| 2030 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2031 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2032 | Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2033 | if (Tmp2 == 1) return 1; |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2034 | return std::min(Tmp, Tmp2)-1; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2035 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2036 | case Instruction::Sub: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2037 | Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2038 | if (Tmp2 == 1) return 1; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2039 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2040 | // Handle NEG. |
David Majnemer | a55027f | 2014-12-26 09:20:17 +0000 | [diff] [blame] | 2041 | if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0))) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2042 | if (CLHS->isNullValue()) { |
| 2043 | APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2044 | computeKnownBits(U->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, |
| 2045 | Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2046 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 2047 | // sign bits set. |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2048 | if ((KnownZero | APInt(TyBits, 1)).isAllOnesValue()) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2049 | return TyBits; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2050 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2051 | // If the input is known to be positive (the sign bit is known clear), |
| 2052 | // the output of the NEG has the same number of sign bits as the input. |
| 2053 | if (KnownZero.isNegative()) |
| 2054 | return Tmp2; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2055 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2056 | // Otherwise, we treat this like a SUB. |
| 2057 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2058 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2059 | // Sub can have at most one carry bit. Thus we know that the output |
| 2060 | // is, at worst, one more bit than the inputs. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2061 | Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2062 | if (Tmp == 1) return 1; // Early out. |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2063 | return std::min(Tmp, Tmp2)-1; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2064 | |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2065 | case Instruction::PHI: { |
| 2066 | PHINode *PN = cast<PHINode>(U); |
David Majnemer | 6ee8d17 | 2015-01-04 07:06:53 +0000 | [diff] [blame] | 2067 | unsigned NumIncomingValues = PN->getNumIncomingValues(); |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2068 | // Don't analyze large in-degree PHIs. |
David Majnemer | 6ee8d17 | 2015-01-04 07:06:53 +0000 | [diff] [blame] | 2069 | if (NumIncomingValues > 4) break; |
| 2070 | // Unreachable blocks may have zero-operand PHI nodes. |
| 2071 | if (NumIncomingValues == 0) break; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2072 | |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2073 | // Take the minimum of all incoming values. This can't infinitely loop |
| 2074 | // because of our depth threshold. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2075 | Tmp = ComputeNumSignBits(PN->getIncomingValue(0), DL, Depth + 1, Q); |
David Majnemer | 6ee8d17 | 2015-01-04 07:06:53 +0000 | [diff] [blame] | 2076 | for (unsigned i = 1, e = NumIncomingValues; i != e; ++i) { |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2077 | if (Tmp == 1) return Tmp; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2078 | Tmp = std::min( |
| 2079 | Tmp, ComputeNumSignBits(PN->getIncomingValue(i), DL, Depth + 1, Q)); |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2080 | } |
| 2081 | return Tmp; |
| 2082 | } |
| 2083 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2084 | case Instruction::Trunc: |
| 2085 | // FIXME: it's tricky to do anything useful for this, but it is an important |
| 2086 | // case for targets like X86. |
| 2087 | break; |
| 2088 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2089 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2090 | // Finally, if we can prove that the top bits of the result are 0's or 1's, |
| 2091 | // use this information. |
| 2092 | APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0); |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2093 | APInt Mask; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2094 | computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2095 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2096 | if (KnownZero.isNegative()) { // sign bit is 0 |
| 2097 | Mask = KnownZero; |
| 2098 | } else if (KnownOne.isNegative()) { // sign bit is 1; |
| 2099 | Mask = KnownOne; |
| 2100 | } else { |
| 2101 | // Nothing known. |
| 2102 | return FirstAnswer; |
| 2103 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2104 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2105 | // Okay, we know that the sign bit in Mask is set. Use CLZ to determine |
| 2106 | // the number of identical bits in the top of the input value. |
| 2107 | Mask = ~Mask; |
| 2108 | Mask <<= Mask.getBitWidth()-TyBits; |
| 2109 | // Return # leading zeros. We use 'min' here in case Val was zero before |
| 2110 | // shifting. We don't want to return '64' as for an i32 "0". |
| 2111 | return std::max(FirstAnswer, std::min(TyBits, Mask.countLeadingZeros())); |
| 2112 | } |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2113 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2114 | /// This function computes the integer multiple of Base that equals V. |
| 2115 | /// If successful, it returns true and returns the multiple in |
| 2116 | /// Multiple. If unsuccessful, it returns false. It looks |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2117 | /// through SExt instructions only if LookThroughSExt is true. |
| 2118 | bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple, |
Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2119 | bool LookThroughSExt, unsigned Depth) { |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2120 | const unsigned MaxDepth = 6; |
| 2121 | |
Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2122 | assert(V && "No Value?"); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2123 | assert(Depth <= MaxDepth && "Limit Search Depth"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2124 | assert(V->getType()->isIntegerTy() && "Not integer or pointer type!"); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2125 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2126 | Type *T = V->getType(); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2127 | |
Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2128 | ConstantInt *CI = dyn_cast<ConstantInt>(V); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2129 | |
| 2130 | if (Base == 0) |
| 2131 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2132 | |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2133 | if (Base == 1) { |
| 2134 | Multiple = V; |
| 2135 | return true; |
| 2136 | } |
| 2137 | |
| 2138 | ConstantExpr *CO = dyn_cast<ConstantExpr>(V); |
| 2139 | Constant *BaseVal = ConstantInt::get(T, Base); |
| 2140 | if (CO && CO == BaseVal) { |
| 2141 | // Multiple is 1. |
| 2142 | Multiple = ConstantInt::get(T, 1); |
| 2143 | return true; |
| 2144 | } |
| 2145 | |
| 2146 | if (CI && CI->getZExtValue() % Base == 0) { |
| 2147 | Multiple = ConstantInt::get(T, CI->getZExtValue() / Base); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2148 | return true; |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2149 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2150 | |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2151 | if (Depth == MaxDepth) return false; // Limit search depth. |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2152 | |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2153 | Operator *I = dyn_cast<Operator>(V); |
| 2154 | if (!I) return false; |
| 2155 | |
| 2156 | switch (I->getOpcode()) { |
| 2157 | default: break; |
Chris Lattner | 4f0b47d | 2009-11-26 01:50:12 +0000 | [diff] [blame] | 2158 | case Instruction::SExt: |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2159 | if (!LookThroughSExt) return false; |
| 2160 | // otherwise fall through to ZExt |
Chris Lattner | 4f0b47d | 2009-11-26 01:50:12 +0000 | [diff] [blame] | 2161 | case Instruction::ZExt: |
Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2162 | return ComputeMultiple(I->getOperand(0), Base, Multiple, |
| 2163 | LookThroughSExt, Depth+1); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2164 | case Instruction::Shl: |
| 2165 | case Instruction::Mul: { |
| 2166 | Value *Op0 = I->getOperand(0); |
| 2167 | Value *Op1 = I->getOperand(1); |
| 2168 | |
| 2169 | if (I->getOpcode() == Instruction::Shl) { |
| 2170 | ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1); |
| 2171 | if (!Op1CI) return false; |
| 2172 | // Turn Op0 << Op1 into Op0 * 2^Op1 |
| 2173 | APInt Op1Int = Op1CI->getValue(); |
| 2174 | uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1); |
Jay Foad | 15084f0 | 2010-11-30 09:02:01 +0000 | [diff] [blame] | 2175 | APInt API(Op1Int.getBitWidth(), 0); |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 2176 | API.setBit(BitToSet); |
Jay Foad | 15084f0 | 2010-11-30 09:02:01 +0000 | [diff] [blame] | 2177 | Op1 = ConstantInt::get(V->getContext(), API); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2180 | Value *Mul0 = nullptr; |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2181 | if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) { |
| 2182 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) |
| 2183 | if (Constant *MulC = dyn_cast<Constant>(Mul0)) { |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2184 | if (Op1C->getType()->getPrimitiveSizeInBits() < |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2185 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2186 | Op1C = ConstantExpr::getZExt(Op1C, MulC->getType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2187 | if (Op1C->getType()->getPrimitiveSizeInBits() > |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2188 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2189 | MulC = ConstantExpr::getZExt(MulC, Op1C->getType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2190 | |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2191 | // V == Base * (Mul0 * Op1), so return (Mul0 * Op1) |
| 2192 | Multiple = ConstantExpr::getMul(MulC, Op1C); |
| 2193 | return true; |
| 2194 | } |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2195 | |
| 2196 | if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0)) |
| 2197 | if (Mul0CI->getValue() == 1) { |
| 2198 | // V == Base * Op1, so return Op1 |
| 2199 | Multiple = Op1; |
| 2200 | return true; |
| 2201 | } |
| 2202 | } |
| 2203 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2204 | Value *Mul1 = nullptr; |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2205 | if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) { |
| 2206 | if (Constant *Op0C = dyn_cast<Constant>(Op0)) |
| 2207 | if (Constant *MulC = dyn_cast<Constant>(Mul1)) { |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2208 | if (Op0C->getType()->getPrimitiveSizeInBits() < |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2209 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2210 | Op0C = ConstantExpr::getZExt(Op0C, MulC->getType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2211 | if (Op0C->getType()->getPrimitiveSizeInBits() > |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2212 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2213 | MulC = ConstantExpr::getZExt(MulC, Op0C->getType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2214 | |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2215 | // V == Base * (Mul1 * Op0), so return (Mul1 * Op0) |
| 2216 | Multiple = ConstantExpr::getMul(MulC, Op0C); |
| 2217 | return true; |
| 2218 | } |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2219 | |
| 2220 | if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1)) |
| 2221 | if (Mul1CI->getValue() == 1) { |
| 2222 | // V == Base * Op0, so return Op0 |
| 2223 | Multiple = Op0; |
| 2224 | return true; |
| 2225 | } |
| 2226 | } |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | // We could not determine if V is a multiple of Base. |
| 2231 | return false; |
| 2232 | } |
| 2233 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2234 | /// Return true if we can prove that the specified FP value is never equal to |
| 2235 | /// -0.0. |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2236 | /// |
| 2237 | /// NOTE: this function will need to be revisited when we support non-default |
| 2238 | /// rounding modes! |
| 2239 | /// |
| 2240 | bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) { |
| 2241 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) |
| 2242 | return !CFP->getValueAPF().isNegZero(); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2243 | |
Sanjay Patel | 40eaa8d | 2015-02-25 18:00:15 +0000 | [diff] [blame] | 2244 | // FIXME: Magic number! At the least, this should be given a name because it's |
| 2245 | // used similarly in CannotBeOrderedLessThanZero(). A better fix may be to |
| 2246 | // expose it as a parameter, so it can be used for testing / experimenting. |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2247 | if (Depth == 6) |
Sanjay Patel | 40eaa8d | 2015-02-25 18:00:15 +0000 | [diff] [blame] | 2248 | return false; // Limit search depth. |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2249 | |
Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 2250 | const Operator *I = dyn_cast<Operator>(V); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2251 | if (!I) return false; |
Michael Ilseman | 0f12837 | 2012-12-06 00:07:09 +0000 | [diff] [blame] | 2252 | |
| 2253 | // Check if the nsz fast-math flag is set |
| 2254 | if (const FPMathOperator *FPO = dyn_cast<FPMathOperator>(I)) |
| 2255 | if (FPO->hasNoSignedZeros()) |
| 2256 | return true; |
| 2257 | |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2258 | // (add x, 0.0) is guaranteed to return +0.0, not -0.0. |
Jakub Staszak | b7129f2 | 2013-03-06 00:16:16 +0000 | [diff] [blame] | 2259 | if (I->getOpcode() == Instruction::FAdd) |
| 2260 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I->getOperand(1))) |
| 2261 | if (CFP->isNullValue()) |
| 2262 | return true; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2263 | |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2264 | // sitofp and uitofp turn into +0.0 for zero. |
| 2265 | if (isa<SIToFPInst>(I) || isa<UIToFPInst>(I)) |
| 2266 | return true; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2267 | |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2268 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 2269 | // sqrt(-0.0) = -0.0, no other negative results are possible. |
| 2270 | if (II->getIntrinsicID() == Intrinsic::sqrt) |
Gabor Greif | 1abbde3 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 2271 | return CannotBeNegativeZero(II->getArgOperand(0), Depth+1); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2272 | |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2273 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 2274 | if (const Function *F = CI->getCalledFunction()) { |
| 2275 | if (F->isDeclaration()) { |
Daniel Dunbar | ca414c7 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 2276 | // abs(x) != -0.0 |
| 2277 | if (F->getName() == "abs") return true; |
Dale Johannesen | f6a987b | 2009-09-25 20:54:50 +0000 | [diff] [blame] | 2278 | // fabs[lf](x) != -0.0 |
| 2279 | if (F->getName() == "fabs") return true; |
| 2280 | if (F->getName() == "fabsf") return true; |
| 2281 | if (F->getName() == "fabsl") return true; |
| 2282 | if (F->getName() == "sqrt" || F->getName() == "sqrtf" || |
| 2283 | F->getName() == "sqrtl") |
Gabor Greif | 1abbde3 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 2284 | return CannotBeNegativeZero(CI->getArgOperand(0), Depth+1); |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2285 | } |
| 2286 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2287 | |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2288 | return false; |
| 2289 | } |
| 2290 | |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2291 | bool llvm::CannotBeOrderedLessThanZero(const Value *V, unsigned Depth) { |
| 2292 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) |
| 2293 | return !CFP->getValueAPF().isNegative() || CFP->getValueAPF().isZero(); |
| 2294 | |
Sanjay Patel | 40eaa8d | 2015-02-25 18:00:15 +0000 | [diff] [blame] | 2295 | // FIXME: Magic number! At the least, this should be given a name because it's |
| 2296 | // used similarly in CannotBeNegativeZero(). A better fix may be to |
| 2297 | // expose it as a parameter, so it can be used for testing / experimenting. |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2298 | if (Depth == 6) |
| 2299 | return false; // Limit search depth. |
| 2300 | |
| 2301 | const Operator *I = dyn_cast<Operator>(V); |
| 2302 | if (!I) return false; |
| 2303 | |
| 2304 | switch (I->getOpcode()) { |
| 2305 | default: break; |
| 2306 | case Instruction::FMul: |
| 2307 | // x*x is always non-negative or a NaN. |
| 2308 | if (I->getOperand(0) == I->getOperand(1)) |
| 2309 | return true; |
| 2310 | // Fall through |
| 2311 | case Instruction::FAdd: |
| 2312 | case Instruction::FDiv: |
| 2313 | case Instruction::FRem: |
| 2314 | return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1) && |
| 2315 | CannotBeOrderedLessThanZero(I->getOperand(1), Depth+1); |
| 2316 | case Instruction::FPExt: |
| 2317 | case Instruction::FPTrunc: |
| 2318 | // Widening/narrowing never change sign. |
| 2319 | return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1); |
| 2320 | case Instruction::Call: |
| 2321 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 2322 | switch (II->getIntrinsicID()) { |
| 2323 | default: break; |
| 2324 | case Intrinsic::exp: |
| 2325 | case Intrinsic::exp2: |
| 2326 | case Intrinsic::fabs: |
| 2327 | case Intrinsic::sqrt: |
| 2328 | return true; |
| 2329 | case Intrinsic::powi: |
| 2330 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 2331 | // powi(x,n) is non-negative if n is even. |
| 2332 | if (CI->getBitWidth() <= 64 && CI->getSExtValue() % 2u == 0) |
| 2333 | return true; |
| 2334 | } |
| 2335 | return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1); |
| 2336 | case Intrinsic::fma: |
| 2337 | case Intrinsic::fmuladd: |
| 2338 | // x*x+y is non-negative if y is non-negative. |
| 2339 | return I->getOperand(0) == I->getOperand(1) && |
| 2340 | CannotBeOrderedLessThanZero(I->getOperand(2), Depth+1); |
| 2341 | } |
| 2342 | break; |
| 2343 | } |
| 2344 | return false; |
| 2345 | } |
| 2346 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2347 | /// If the specified value can be set by repeating the same byte in memory, |
| 2348 | /// return the i8 value that it is represented with. This is |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2349 | /// true for all i8 values obviously, but is also true for i32 0, i32 -1, |
| 2350 | /// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated |
| 2351 | /// byte store (e.g. i16 0x1234), return null. |
| 2352 | Value *llvm::isBytewiseValue(Value *V) { |
| 2353 | // All byte-wide stores are splatable, even of arbitrary variables. |
| 2354 | if (V->getType()->isIntegerTy(8)) return V; |
Chris Lattner | acf6b07 | 2011-02-19 19:35:49 +0000 | [diff] [blame] | 2355 | |
| 2356 | // Handle 'null' ConstantArrayZero etc. |
| 2357 | if (Constant *C = dyn_cast<Constant>(V)) |
| 2358 | if (C->isNullValue()) |
| 2359 | return Constant::getNullValue(Type::getInt8Ty(V->getContext())); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2360 | |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2361 | // Constant float and double values can be handled as integer values if the |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2362 | // corresponding integer value is "byteable". An important case is 0.0. |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2363 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 2364 | if (CFP->getType()->isFloatTy()) |
| 2365 | V = ConstantExpr::getBitCast(CFP, Type::getInt32Ty(V->getContext())); |
| 2366 | if (CFP->getType()->isDoubleTy()) |
| 2367 | V = ConstantExpr::getBitCast(CFP, Type::getInt64Ty(V->getContext())); |
| 2368 | // Don't handle long double formats, which have strange constraints. |
| 2369 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2370 | |
Benjamin Kramer | 17d9015 | 2015-02-07 19:29:02 +0000 | [diff] [blame] | 2371 | // We can handle constant integers that are multiple of 8 bits. |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2372 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Benjamin Kramer | 17d9015 | 2015-02-07 19:29:02 +0000 | [diff] [blame] | 2373 | if (CI->getBitWidth() % 8 == 0) { |
| 2374 | assert(CI->getBitWidth() > 8 && "8 bits should be handled above!"); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2375 | |
Benjamin Kramer | b4b5150 | 2015-03-25 16:49:59 +0000 | [diff] [blame] | 2376 | if (!CI->getValue().isSplat(8)) |
Benjamin Kramer | 17d9015 | 2015-02-07 19:29:02 +0000 | [diff] [blame] | 2377 | return nullptr; |
| 2378 | return ConstantInt::get(V->getContext(), CI->getValue().trunc(8)); |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2379 | } |
| 2380 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2381 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2382 | // A ConstantDataArray/Vector is splatable if all its members are equal and |
| 2383 | // also splatable. |
| 2384 | if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(V)) { |
| 2385 | Value *Elt = CA->getElementAsConstant(0); |
| 2386 | Value *Val = isBytewiseValue(Elt); |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2387 | if (!Val) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2388 | return nullptr; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2389 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2390 | for (unsigned I = 1, E = CA->getNumElements(); I != E; ++I) |
| 2391 | if (CA->getElementAsConstant(I) != Elt) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2392 | return nullptr; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2393 | |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2394 | return Val; |
| 2395 | } |
Chad Rosier | 8abf65a | 2011-12-06 00:19:08 +0000 | [diff] [blame] | 2396 | |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2397 | // Conceptually, we could handle things like: |
| 2398 | // %a = zext i8 %X to i16 |
| 2399 | // %b = shl i16 %a, 8 |
| 2400 | // %c = or i16 %a, %b |
| 2401 | // but until there is an example that actually needs this, it doesn't seem |
| 2402 | // worth worrying about. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2403 | return nullptr; |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2407 | // This is the recursive version of BuildSubAggregate. It takes a few different |
| 2408 | // arguments. Idxs is the index within the nested struct From that we are |
| 2409 | // looking at now (which is of type IndexedType). IdxSkip is the number of |
| 2410 | // indices from Idxs that should be left out when inserting into the resulting |
| 2411 | // struct. To is the result struct built so far, new insertvalue instructions |
| 2412 | // build on that. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2413 | static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType, |
Craig Topper | 2cd5ff8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 2414 | SmallVectorImpl<unsigned> &Idxs, |
Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 2415 | unsigned IdxSkip, |
Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 2416 | Instruction *InsertBefore) { |
Dmitri Gribenko | 226fea5 | 2013-01-13 16:01:15 +0000 | [diff] [blame] | 2417 | llvm::StructType *STy = dyn_cast<llvm::StructType>(IndexedType); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2418 | if (STy) { |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2419 | // Save the original To argument so we can modify it |
| 2420 | Value *OrigTo = To; |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2421 | // General case, the type indexed by Idxs is a struct |
| 2422 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 2423 | // Process each struct element recursively |
| 2424 | Idxs.push_back(i); |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2425 | Value *PrevTo = To; |
Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 2426 | To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip, |
Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 2427 | InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2428 | Idxs.pop_back(); |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2429 | if (!To) { |
| 2430 | // Couldn't find any inserted value for this index? Cleanup |
| 2431 | while (PrevTo != OrigTo) { |
| 2432 | InsertValueInst* Del = cast<InsertValueInst>(PrevTo); |
| 2433 | PrevTo = Del->getAggregateOperand(); |
| 2434 | Del->eraseFromParent(); |
| 2435 | } |
| 2436 | // Stop processing elements |
| 2437 | break; |
| 2438 | } |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2439 | } |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 2440 | // If we successfully found a value for each of our subaggregates |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2441 | if (To) |
| 2442 | return To; |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2443 | } |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2444 | // Base case, the type indexed by SourceIdxs is not a struct, or not all of |
| 2445 | // the struct's elements had a value that was inserted directly. In the latter |
| 2446 | // case, perhaps we can't determine each of the subelements individually, but |
| 2447 | // we might be able to find the complete struct somewhere. |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2448 | |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2449 | // Find the value that is at that particular spot |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2450 | Value *V = FindInsertedValue(From, Idxs); |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2451 | |
| 2452 | if (!V) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2453 | return nullptr; |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2454 | |
| 2455 | // Insert the value in the new (sub) aggregrate |
Frits van Bommel | 717d7ed | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 2456 | return llvm::InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip), |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2457 | "tmp", InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | // This helper takes a nested struct and extracts a part of it (which is again a |
| 2461 | // struct) into a new value. For example, given the struct: |
| 2462 | // { a, { b, { c, d }, e } } |
| 2463 | // and the indices "1, 1" this returns |
| 2464 | // { c, d }. |
| 2465 | // |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2466 | // It does this by inserting an insertvalue for each element in the resulting |
| 2467 | // struct, as opposed to just inserting a single struct. This will only work if |
| 2468 | // each of the elements of the substruct are known (ie, inserted into From by an |
| 2469 | // insertvalue instruction somewhere). |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2470 | // |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2471 | // All inserted insertvalue instructions are inserted before InsertBefore |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2472 | static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range, |
Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 2473 | Instruction *InsertBefore) { |
Matthijs Kooijman | 69801d4 | 2008-06-16 13:28:31 +0000 | [diff] [blame] | 2474 | assert(InsertBefore && "Must have someplace to insert!"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2475 | Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(), |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2476 | idx_range); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2477 | Value *To = UndefValue::get(IndexedType); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2478 | SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end()); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2479 | unsigned IdxSkip = Idxs.size(); |
| 2480 | |
Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 2481 | return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2484 | /// Given an aggregrate and an sequence of indices, see if |
Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 2485 | /// the scalar value indexed is already around as a register, for example if it |
| 2486 | /// were inserted directly into the aggregrate. |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2487 | /// |
| 2488 | /// If InsertBefore is not null, this function will duplicate (modified) |
| 2489 | /// insertvalues when a part of a nested struct is extracted. |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2490 | Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range, |
| 2491 | Instruction *InsertBefore) { |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2492 | // Nothing to index? Just return V then (this is useful at the end of our |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 2493 | // recursion). |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2494 | if (idx_range.empty()) |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2495 | return V; |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 2496 | // We have indices, so V should have an indexable type. |
| 2497 | assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) && |
| 2498 | "Not looking at a struct or array?"); |
| 2499 | assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) && |
| 2500 | "Invalid indices for type?"); |
Owen Anderson | f1f1743 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 2501 | |
Chris Lattner | 6705883 | 2012-01-25 06:48:06 +0000 | [diff] [blame] | 2502 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 2503 | C = C->getAggregateElement(idx_range[0]); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2504 | if (!C) return nullptr; |
Chris Lattner | 6705883 | 2012-01-25 06:48:06 +0000 | [diff] [blame] | 2505 | return FindInsertedValue(C, idx_range.slice(1), InsertBefore); |
| 2506 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2507 | |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 2508 | if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) { |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2509 | // Loop the indices for the insertvalue instruction in parallel with the |
| 2510 | // requested indices |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2511 | const unsigned *req_idx = idx_range.begin(); |
Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 2512 | for (const unsigned *i = I->idx_begin(), *e = I->idx_end(); |
| 2513 | i != e; ++i, ++req_idx) { |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2514 | if (req_idx == idx_range.end()) { |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 2515 | // We can't handle this without inserting insertvalues |
| 2516 | if (!InsertBefore) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2517 | return nullptr; |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 2518 | |
| 2519 | // The requested index identifies a part of a nested aggregate. Handle |
| 2520 | // this specially. For example, |
| 2521 | // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0 |
| 2522 | // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1 |
| 2523 | // %C = extractvalue {i32, { i32, i32 } } %B, 1 |
| 2524 | // This can be changed into |
| 2525 | // %A = insertvalue {i32, i32 } undef, i32 10, 0 |
| 2526 | // %C = insertvalue {i32, i32 } %A, i32 11, 1 |
| 2527 | // which allows the unused 0,0 element from the nested struct to be |
| 2528 | // removed. |
| 2529 | return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx), |
| 2530 | InsertBefore); |
Duncan Sands | db356ee | 2008-06-19 08:47:31 +0000 | [diff] [blame] | 2531 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2532 | |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2533 | // This insert value inserts something else than what we are looking for. |
| 2534 | // See if the (aggregrate) value inserted into has the value we are |
| 2535 | // looking for, then. |
| 2536 | if (*req_idx != *i) |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2537 | return FindInsertedValue(I->getAggregateOperand(), idx_range, |
Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 2538 | InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2539 | } |
| 2540 | // If we end up here, the indices of the insertvalue match with those |
| 2541 | // requested (though possibly only partially). Now we recursively look at |
| 2542 | // the inserted value, passing any remaining indices. |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2543 | return FindInsertedValue(I->getInsertedValueOperand(), |
Frits van Bommel | 717d7ed | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 2544 | makeArrayRef(req_idx, idx_range.end()), |
Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 2545 | InsertBefore); |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 2546 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2547 | |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 2548 | if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) { |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2549 | // If we're extracting a value from an aggregrate that was extracted from |
| 2550 | // something else, we can extract from that something else directly instead. |
| 2551 | // However, we will need to chain I's indices with the requested indices. |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2552 | |
| 2553 | // Calculate the number of indices required |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2554 | unsigned size = I->getNumIndices() + idx_range.size(); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2555 | // Allocate some space to put the new indices in |
Matthijs Kooijman | 8369c67 | 2008-06-17 08:24:37 +0000 | [diff] [blame] | 2556 | SmallVector<unsigned, 5> Idxs; |
| 2557 | Idxs.reserve(size); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2558 | // Add indices from the extract value instruction |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2559 | Idxs.append(I->idx_begin(), I->idx_end()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2560 | |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2561 | // Add requested indices |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2562 | Idxs.append(idx_range.begin(), idx_range.end()); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2563 | |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2564 | assert(Idxs.size() == size |
Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 2565 | && "Number of indices added not correct?"); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2566 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2567 | return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2568 | } |
| 2569 | // Otherwise, we don't know (such as, extracting from a function return value |
| 2570 | // or load instruction) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2571 | return nullptr; |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2572 | } |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2573 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2574 | /// Analyze the specified pointer to see if it can be expressed as a base |
| 2575 | /// pointer plus a constant offset. Return the base and offset to the caller. |
Chris Lattner | e28618d | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 2576 | Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2577 | const DataLayout &DL) { |
| 2578 | unsigned BitWidth = DL.getPointerTypeSizeInBits(Ptr->getType()); |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 2579 | APInt ByteOffset(BitWidth, 0); |
| 2580 | while (1) { |
| 2581 | if (Ptr->getType()->isVectorTy()) |
| 2582 | break; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2583 | |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 2584 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2585 | APInt GEPOffset(BitWidth, 0); |
| 2586 | if (!GEP->accumulateConstantOffset(DL, GEPOffset)) |
| 2587 | break; |
Matt Arsenault | f55e5e7 | 2013-08-10 17:34:08 +0000 | [diff] [blame] | 2588 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2589 | ByteOffset += GEPOffset; |
Matt Arsenault | f55e5e7 | 2013-08-10 17:34:08 +0000 | [diff] [blame] | 2590 | |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 2591 | Ptr = GEP->getPointerOperand(); |
Matt Arsenault | fd78d0c | 2014-07-14 22:39:22 +0000 | [diff] [blame] | 2592 | } else if (Operator::getOpcode(Ptr) == Instruction::BitCast || |
| 2593 | Operator::getOpcode(Ptr) == Instruction::AddrSpaceCast) { |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 2594 | Ptr = cast<Operator>(Ptr)->getOperand(0); |
| 2595 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) { |
| 2596 | if (GA->mayBeOverridden()) |
| 2597 | break; |
| 2598 | Ptr = GA->getAliasee(); |
Chris Lattner | e28618d | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 2599 | } else { |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 2600 | break; |
Chris Lattner | e28618d | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 2601 | } |
| 2602 | } |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 2603 | Offset = ByteOffset.getSExtValue(); |
| 2604 | return Ptr; |
Chris Lattner | e28618d | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
| 2607 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2608 | /// This function computes the length of a null-terminated C string pointed to |
| 2609 | /// by V. If successful, it returns true and returns the string in Str. |
| 2610 | /// If unsuccessful, it returns false. |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2611 | bool llvm::getConstantStringInfo(const Value *V, StringRef &Str, |
| 2612 | uint64_t Offset, bool TrimAtNul) { |
| 2613 | assert(V); |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2614 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2615 | // Look through bitcast instructions and geps. |
| 2616 | V = V->stripPointerCasts(); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2617 | |
Benjamin Kramer | 0248a3e | 2015-03-21 15:36:06 +0000 | [diff] [blame] | 2618 | // If the value is a GEP instruction or constant expression, treat it as an |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2619 | // offset. |
| 2620 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2621 | // Make sure the GEP has exactly three arguments. |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2622 | if (GEP->getNumOperands() != 3) |
| 2623 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2624 | |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2625 | // Make sure the index-ee is a pointer to array of i8. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2626 | PointerType *PT = cast<PointerType>(GEP->getOperand(0)->getType()); |
| 2627 | ArrayType *AT = dyn_cast<ArrayType>(PT->getElementType()); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2628 | if (!AT || !AT->getElementType()->isIntegerTy(8)) |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2629 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2630 | |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2631 | // Check to make sure that the first operand of the GEP is an integer and |
| 2632 | // has value 0 so that we are sure we're indexing into the initializer. |
Dan Gohman | 0b4df04 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 2633 | const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1)); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2634 | if (!FirstIdx || !FirstIdx->isZero()) |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2635 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2636 | |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2637 | // If the second index isn't a ConstantInt, then this is a variable index |
| 2638 | // into the array. If this occurs, we can't say anything meaningful about |
| 2639 | // the string. |
| 2640 | uint64_t StartIdx = 0; |
Dan Gohman | 0b4df04 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 2641 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2642 | StartIdx = CI->getZExtValue(); |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2643 | else |
| 2644 | return false; |
Benjamin Kramer | 0248a3e | 2015-03-21 15:36:06 +0000 | [diff] [blame] | 2645 | return getConstantStringInfo(GEP->getOperand(0), Str, StartIdx + Offset, |
| 2646 | TrimAtNul); |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2647 | } |
Nick Lewycky | 4620988 | 2011-10-20 00:34:35 +0000 | [diff] [blame] | 2648 | |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2649 | // The GEP instruction, constant or instruction, must reference a global |
| 2650 | // variable that is a constant and is initialized. The referenced constant |
| 2651 | // initializer is the array that we'll use for optimization. |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2652 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(V); |
Dan Gohman | 5d5bc6d | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 2653 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer()) |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2654 | return false; |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2655 | |
Nick Lewycky | 4620988 | 2011-10-20 00:34:35 +0000 | [diff] [blame] | 2656 | // Handle the all-zeros case |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2657 | if (GV->getInitializer()->isNullValue()) { |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2658 | // This is a degenerate case. The initializer is constant zero so the |
| 2659 | // length of the string must be zero. |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2660 | Str = ""; |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2661 | return true; |
| 2662 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2663 | |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2664 | // Must be a Constant Array |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2665 | const ConstantDataArray *Array = |
| 2666 | dyn_cast<ConstantDataArray>(GV->getInitializer()); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2667 | if (!Array || !Array->isString()) |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2668 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2669 | |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2670 | // Get the number of elements in the array |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2671 | uint64_t NumElts = Array->getType()->getArrayNumElements(); |
| 2672 | |
| 2673 | // Start out with the entire array in the StringRef. |
| 2674 | Str = Array->getAsString(); |
| 2675 | |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2676 | if (Offset > NumElts) |
| 2677 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2678 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2679 | // Skip over 'offset' bytes. |
| 2680 | Str = Str.substr(Offset); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2681 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2682 | if (TrimAtNul) { |
| 2683 | // Trim off the \0 and anything after it. If the array is not nul |
| 2684 | // terminated, we just return the whole end of string. The client may know |
| 2685 | // some other way that the string is length-bound. |
| 2686 | Str = Str.substr(0, Str.find('\0')); |
| 2687 | } |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2688 | return true; |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2689 | } |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2690 | |
| 2691 | // These next two are very similar to the above, but also look through PHI |
| 2692 | // nodes. |
| 2693 | // TODO: See if we can integrate these two together. |
| 2694 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2695 | /// If we can compute the length of the string pointed to by |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2696 | /// the specified pointer, return 'len+1'. If we can't, return 0. |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 2697 | static uint64_t GetStringLengthH(Value *V, SmallPtrSetImpl<PHINode*> &PHIs) { |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2698 | // Look through noop bitcast instructions. |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2699 | V = V->stripPointerCasts(); |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2700 | |
| 2701 | // If this is a PHI node, there are two cases: either we have already seen it |
| 2702 | // or we haven't. |
| 2703 | if (PHINode *PN = dyn_cast<PHINode>(V)) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 2704 | if (!PHIs.insert(PN).second) |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2705 | return ~0ULL; // already in the set. |
| 2706 | |
| 2707 | // If it was new, see if all the input strings are the same length. |
| 2708 | uint64_t LenSoFar = ~0ULL; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 2709 | for (Value *IncValue : PN->incoming_values()) { |
| 2710 | uint64_t Len = GetStringLengthH(IncValue, PHIs); |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2711 | if (Len == 0) return 0; // Unknown length -> unknown. |
| 2712 | |
| 2713 | if (Len == ~0ULL) continue; |
| 2714 | |
| 2715 | if (Len != LenSoFar && LenSoFar != ~0ULL) |
| 2716 | return 0; // Disagree -> unknown. |
| 2717 | LenSoFar = Len; |
| 2718 | } |
| 2719 | |
| 2720 | // Success, all agree. |
| 2721 | return LenSoFar; |
| 2722 | } |
| 2723 | |
| 2724 | // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y) |
| 2725 | if (SelectInst *SI = dyn_cast<SelectInst>(V)) { |
| 2726 | uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs); |
| 2727 | if (Len1 == 0) return 0; |
| 2728 | uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs); |
| 2729 | if (Len2 == 0) return 0; |
| 2730 | if (Len1 == ~0ULL) return Len2; |
| 2731 | if (Len2 == ~0ULL) return Len1; |
| 2732 | if (Len1 != Len2) return 0; |
| 2733 | return Len1; |
| 2734 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2735 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2736 | // Otherwise, see if we can read the string. |
| 2737 | StringRef StrData; |
| 2738 | if (!getConstantStringInfo(V, StrData)) |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2739 | return 0; |
| 2740 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2741 | return StrData.size()+1; |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2744 | /// If we can compute the length of the string pointed to by |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2745 | /// the specified pointer, return 'len+1'. If we can't, return 0. |
| 2746 | uint64_t llvm::GetStringLength(Value *V) { |
| 2747 | if (!V->getType()->isPointerTy()) return 0; |
| 2748 | |
| 2749 | SmallPtrSet<PHINode*, 32> PHIs; |
| 2750 | uint64_t Len = GetStringLengthH(V, PHIs); |
| 2751 | // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return |
| 2752 | // an empty string as a length. |
| 2753 | return Len == ~0ULL ? 1 : Len; |
| 2754 | } |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 2755 | |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 2756 | /// \brief \p PN defines a loop-variant pointer to an object. Check if the |
| 2757 | /// previous iteration of the loop was referring to the same object as \p PN. |
| 2758 | static bool isSameUnderlyingObjectInLoop(PHINode *PN, LoopInfo *LI) { |
| 2759 | // Find the loop-defined value. |
| 2760 | Loop *L = LI->getLoopFor(PN->getParent()); |
| 2761 | if (PN->getNumIncomingValues() != 2) |
| 2762 | return true; |
| 2763 | |
| 2764 | // Find the value from previous iteration. |
| 2765 | auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0)); |
| 2766 | if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) |
| 2767 | PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1)); |
| 2768 | if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) |
| 2769 | return true; |
| 2770 | |
| 2771 | // If a new pointer is loaded in the loop, the pointer references a different |
| 2772 | // object in every iteration. E.g.: |
| 2773 | // for (i) |
| 2774 | // int *p = a[i]; |
| 2775 | // ... |
| 2776 | if (auto *Load = dyn_cast<LoadInst>(PrevValue)) |
| 2777 | if (!L->isLoopInvariant(Load->getPointerOperand())) |
| 2778 | return false; |
| 2779 | return true; |
| 2780 | } |
| 2781 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2782 | Value *llvm::GetUnderlyingObject(Value *V, const DataLayout &DL, |
| 2783 | unsigned MaxLookup) { |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 2784 | if (!V->getType()->isPointerTy()) |
| 2785 | return V; |
| 2786 | for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) { |
| 2787 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
| 2788 | V = GEP->getPointerOperand(); |
Matt Arsenault | 70f4db88 | 2014-07-15 00:56:40 +0000 | [diff] [blame] | 2789 | } else if (Operator::getOpcode(V) == Instruction::BitCast || |
| 2790 | Operator::getOpcode(V) == Instruction::AddrSpaceCast) { |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 2791 | V = cast<Operator>(V)->getOperand(0); |
| 2792 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
| 2793 | if (GA->mayBeOverridden()) |
| 2794 | return V; |
| 2795 | V = GA->getAliasee(); |
| 2796 | } else { |
Dan Gohman | 05b18f1 | 2010-12-15 20:49:55 +0000 | [diff] [blame] | 2797 | // See if InstructionSimplify knows any relevant tricks. |
| 2798 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 2799 | // TODO: Acquire a DominatorTree and AssumptionCache and use them. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2800 | if (Value *Simplified = SimplifyInstruction(I, DL, nullptr)) { |
Dan Gohman | 05b18f1 | 2010-12-15 20:49:55 +0000 | [diff] [blame] | 2801 | V = Simplified; |
| 2802 | continue; |
| 2803 | } |
| 2804 | |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 2805 | return V; |
| 2806 | } |
| 2807 | assert(V->getType()->isPointerTy() && "Unexpected operand type!"); |
| 2808 | } |
| 2809 | return V; |
| 2810 | } |
Nick Lewycky | 3e334a4 | 2011-06-27 04:20:45 +0000 | [diff] [blame] | 2811 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2812 | void llvm::GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects, |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 2813 | const DataLayout &DL, LoopInfo *LI, |
| 2814 | unsigned MaxLookup) { |
Dan Gohman | ed7c24e2 | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 2815 | SmallPtrSet<Value *, 4> Visited; |
| 2816 | SmallVector<Value *, 4> Worklist; |
| 2817 | Worklist.push_back(V); |
| 2818 | do { |
| 2819 | Value *P = Worklist.pop_back_val(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2820 | P = GetUnderlyingObject(P, DL, MaxLookup); |
Dan Gohman | ed7c24e2 | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 2821 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 2822 | if (!Visited.insert(P).second) |
Dan Gohman | ed7c24e2 | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 2823 | continue; |
| 2824 | |
| 2825 | if (SelectInst *SI = dyn_cast<SelectInst>(P)) { |
| 2826 | Worklist.push_back(SI->getTrueValue()); |
| 2827 | Worklist.push_back(SI->getFalseValue()); |
| 2828 | continue; |
| 2829 | } |
| 2830 | |
| 2831 | if (PHINode *PN = dyn_cast<PHINode>(P)) { |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 2832 | // If this PHI changes the underlying object in every iteration of the |
| 2833 | // loop, don't look through it. Consider: |
| 2834 | // int **A; |
| 2835 | // for (i) { |
| 2836 | // Prev = Curr; // Prev = PHI (Prev_0, Curr) |
| 2837 | // Curr = A[i]; |
| 2838 | // *Prev, *Curr; |
| 2839 | // |
| 2840 | // Prev is tracking Curr one iteration behind so they refer to different |
| 2841 | // underlying objects. |
| 2842 | if (!LI || !LI->isLoopHeader(PN->getParent()) || |
| 2843 | isSameUnderlyingObjectInLoop(PN, LI)) |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 2844 | for (Value *IncValue : PN->incoming_values()) |
| 2845 | Worklist.push_back(IncValue); |
Dan Gohman | ed7c24e2 | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 2846 | continue; |
| 2847 | } |
| 2848 | |
| 2849 | Objects.push_back(P); |
| 2850 | } while (!Worklist.empty()); |
| 2851 | } |
| 2852 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2853 | /// Return true if the only users of this pointer are lifetime markers. |
Nick Lewycky | 3e334a4 | 2011-06-27 04:20:45 +0000 | [diff] [blame] | 2854 | bool llvm::onlyUsedByLifetimeMarkers(const Value *V) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2855 | for (const User *U : V->users()) { |
| 2856 | const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U); |
Nick Lewycky | 3e334a4 | 2011-06-27 04:20:45 +0000 | [diff] [blame] | 2857 | if (!II) return false; |
| 2858 | |
| 2859 | if (II->getIntrinsicID() != Intrinsic::lifetime_start && |
| 2860 | II->getIntrinsicID() != Intrinsic::lifetime_end) |
| 2861 | return false; |
| 2862 | } |
| 2863 | return true; |
| 2864 | } |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 2865 | |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2866 | static bool isDereferenceableFromAttribute(const Value *BV, APInt Offset, |
| 2867 | Type *Ty, const DataLayout &DL) { |
| 2868 | assert(Offset.isNonNegative() && "offset can't be negative"); |
| 2869 | assert(Ty->isSized() && "must be sized"); |
| 2870 | |
| 2871 | APInt DerefBytes(Offset.getBitWidth(), 0); |
| 2872 | if (const Argument *A = dyn_cast<Argument>(BV)) { |
| 2873 | DerefBytes = A->getDereferenceableBytes(); |
| 2874 | } else if (auto CS = ImmutableCallSite(BV)) { |
| 2875 | DerefBytes = CS.getDereferenceableBytes(0); |
| 2876 | } |
| 2877 | |
| 2878 | if (DerefBytes.getBoolValue()) |
| 2879 | if (DerefBytes.uge(Offset + DL.getTypeStoreSize(Ty))) |
| 2880 | return true; |
| 2881 | |
| 2882 | return false; |
| 2883 | } |
| 2884 | |
| 2885 | static bool isDereferenceableFromAttribute(const Value *V, |
| 2886 | const DataLayout &DL) { |
| 2887 | Type *VTy = V->getType(); |
| 2888 | Type *Ty = VTy->getPointerElementType(); |
| 2889 | if (!Ty->isSized()) |
| 2890 | return false; |
| 2891 | |
| 2892 | APInt Offset(DL.getTypeStoreSizeInBits(VTy), 0); |
| 2893 | return isDereferenceableFromAttribute(V, Offset, Ty, DL); |
| 2894 | } |
| 2895 | |
| 2896 | /// Return true if Value is always a dereferenceable pointer. |
| 2897 | /// |
| 2898 | /// Test if V is always a pointer to allocated and suitably aligned memory for |
| 2899 | /// a simple load or store. |
| 2900 | static bool isDereferenceablePointer(const Value *V, const DataLayout &DL, |
| 2901 | SmallPtrSetImpl<const Value *> &Visited) { |
| 2902 | // Note that it is not safe to speculate into a malloc'd region because |
| 2903 | // malloc may return null. |
| 2904 | |
| 2905 | // These are obviously ok. |
| 2906 | if (isa<AllocaInst>(V)) return true; |
| 2907 | |
| 2908 | // It's not always safe to follow a bitcast, for example: |
| 2909 | // bitcast i8* (alloca i8) to i32* |
| 2910 | // would result in a 4-byte load from a 1-byte alloca. However, |
| 2911 | // if we're casting from a pointer from a type of larger size |
| 2912 | // to a type of smaller size (or the same size), and the alignment |
| 2913 | // is at least as large as for the resulting pointer type, then |
| 2914 | // we can look through the bitcast. |
| 2915 | if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) { |
| 2916 | Type *STy = BC->getSrcTy()->getPointerElementType(), |
| 2917 | *DTy = BC->getDestTy()->getPointerElementType(); |
| 2918 | if (STy->isSized() && DTy->isSized() && |
| 2919 | (DL.getTypeStoreSize(STy) >= DL.getTypeStoreSize(DTy)) && |
| 2920 | (DL.getABITypeAlignment(STy) >= DL.getABITypeAlignment(DTy))) |
| 2921 | return isDereferenceablePointer(BC->getOperand(0), DL, Visited); |
| 2922 | } |
| 2923 | |
| 2924 | // Global variables which can't collapse to null are ok. |
| 2925 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 2926 | return !GV->hasExternalWeakLinkage(); |
| 2927 | |
| 2928 | // byval arguments are okay. |
| 2929 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 2930 | if (A->hasByValAttr()) |
| 2931 | return true; |
| 2932 | |
| 2933 | if (isDereferenceableFromAttribute(V, DL)) |
| 2934 | return true; |
| 2935 | |
| 2936 | // For GEPs, determine if the indexing lands within the allocated object. |
| 2937 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
| 2938 | // Conservatively require that the base pointer be fully dereferenceable. |
| 2939 | if (!Visited.insert(GEP->getOperand(0)).second) |
| 2940 | return false; |
| 2941 | if (!isDereferenceablePointer(GEP->getOperand(0), DL, Visited)) |
| 2942 | return false; |
| 2943 | // Check the indices. |
| 2944 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 2945 | for (User::const_op_iterator I = GEP->op_begin()+1, |
| 2946 | E = GEP->op_end(); I != E; ++I) { |
| 2947 | Value *Index = *I; |
| 2948 | Type *Ty = *GTI++; |
| 2949 | // Struct indices can't be out of bounds. |
| 2950 | if (isa<StructType>(Ty)) |
| 2951 | continue; |
| 2952 | ConstantInt *CI = dyn_cast<ConstantInt>(Index); |
| 2953 | if (!CI) |
| 2954 | return false; |
| 2955 | // Zero is always ok. |
| 2956 | if (CI->isZero()) |
| 2957 | continue; |
| 2958 | // Check to see that it's within the bounds of an array. |
| 2959 | ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
| 2960 | if (!ATy) |
| 2961 | return false; |
| 2962 | if (CI->getValue().getActiveBits() > 64) |
| 2963 | return false; |
| 2964 | if (CI->getZExtValue() >= ATy->getNumElements()) |
| 2965 | return false; |
| 2966 | } |
| 2967 | // Indices check out; this is dereferenceable. |
| 2968 | return true; |
| 2969 | } |
| 2970 | |
| 2971 | // For gc.relocate, look through relocations |
| 2972 | if (const IntrinsicInst *I = dyn_cast<IntrinsicInst>(V)) |
| 2973 | if (I->getIntrinsicID() == Intrinsic::experimental_gc_relocate) { |
| 2974 | GCRelocateOperands RelocateInst(I); |
Sanjoy Das | 499d703 | 2015-05-06 02:36:26 +0000 | [diff] [blame] | 2975 | return isDereferenceablePointer(RelocateInst.getDerivedPtr(), DL, |
| 2976 | Visited); |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V)) |
| 2980 | return isDereferenceablePointer(ASC->getOperand(0), DL, Visited); |
| 2981 | |
| 2982 | // If we don't know, assume the worst. |
| 2983 | return false; |
| 2984 | } |
| 2985 | |
| 2986 | bool llvm::isDereferenceablePointer(const Value *V, const DataLayout &DL) { |
| 2987 | // When dereferenceability information is provided by a dereferenceable |
| 2988 | // attribute, we know exactly how many bytes are dereferenceable. If we can |
| 2989 | // determine the exact offset to the attributed variable, we can use that |
| 2990 | // information here. |
| 2991 | Type *VTy = V->getType(); |
| 2992 | Type *Ty = VTy->getPointerElementType(); |
| 2993 | if (Ty->isSized()) { |
| 2994 | APInt Offset(DL.getTypeStoreSizeInBits(VTy), 0); |
| 2995 | const Value *BV = V->stripAndAccumulateInBoundsConstantOffsets(DL, Offset); |
| 2996 | |
| 2997 | if (Offset.isNonNegative()) |
| 2998 | if (isDereferenceableFromAttribute(BV, Offset, Ty, DL)) |
| 2999 | return true; |
| 3000 | } |
| 3001 | |
| 3002 | SmallPtrSet<const Value *, 32> Visited; |
| 3003 | return ::isDereferenceablePointer(V, DL, Visited); |
| 3004 | } |
| 3005 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3006 | bool llvm::isSafeToSpeculativelyExecute(const Value *V) { |
Dan Gohman | 7ac046a | 2012-01-04 23:01:09 +0000 | [diff] [blame] | 3007 | const Operator *Inst = dyn_cast<Operator>(V); |
| 3008 | if (!Inst) |
| 3009 | return false; |
| 3010 | |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3011 | for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) |
| 3012 | if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i))) |
| 3013 | if (C->canTrap()) |
| 3014 | return false; |
| 3015 | |
| 3016 | switch (Inst->getOpcode()) { |
| 3017 | default: |
| 3018 | return true; |
| 3019 | case Instruction::UDiv: |
David Majnemer | f20d7c4 | 2014-11-04 23:49:08 +0000 | [diff] [blame] | 3020 | case Instruction::URem: { |
| 3021 | // x / y is undefined if y == 0. |
| 3022 | const APInt *V; |
| 3023 | if (match(Inst->getOperand(1), m_APInt(V))) |
| 3024 | return *V != 0; |
| 3025 | return false; |
| 3026 | } |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3027 | case Instruction::SDiv: |
| 3028 | case Instruction::SRem: { |
David Majnemer | f20d7c4 | 2014-11-04 23:49:08 +0000 | [diff] [blame] | 3029 | // x / y is undefined if y == 0 or x == INT_MIN and y == -1 |
David Majnemer | 8a6578a | 2015-02-01 19:10:19 +0000 | [diff] [blame] | 3030 | const APInt *Numerator, *Denominator; |
| 3031 | if (!match(Inst->getOperand(1), m_APInt(Denominator))) |
| 3032 | return false; |
| 3033 | // We cannot hoist this division if the denominator is 0. |
| 3034 | if (*Denominator == 0) |
| 3035 | return false; |
| 3036 | // It's safe to hoist if the denominator is not 0 or -1. |
| 3037 | if (*Denominator != -1) |
| 3038 | return true; |
| 3039 | // At this point we know that the denominator is -1. It is safe to hoist as |
| 3040 | // long we know that the numerator is not INT_MIN. |
| 3041 | if (match(Inst->getOperand(0), m_APInt(Numerator))) |
| 3042 | return !Numerator->isMinSignedValue(); |
| 3043 | // The numerator *might* be MinSignedValue. |
David Majnemer | f20d7c4 | 2014-11-04 23:49:08 +0000 | [diff] [blame] | 3044 | return false; |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3045 | } |
| 3046 | case Instruction::Load: { |
| 3047 | const LoadInst *LI = cast<LoadInst>(Inst); |
Kostya Serebryany | 0b45828 | 2013-11-21 07:29:28 +0000 | [diff] [blame] | 3048 | if (!LI->isUnordered() || |
| 3049 | // Speculative load may create a race that did not exist in the source. |
| 3050 | LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeThread)) |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3051 | return false; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3052 | const DataLayout &DL = LI->getModule()->getDataLayout(); |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 3053 | return isDereferenceablePointer(LI->getPointerOperand(), DL); |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3054 | } |
Nick Lewycky | b4039f6 | 2011-12-21 05:52:02 +0000 | [diff] [blame] | 3055 | case Instruction::Call: { |
Michael Liao | 736bac6 | 2014-11-06 19:05:57 +0000 | [diff] [blame] | 3056 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
| 3057 | switch (II->getIntrinsicID()) { |
| 3058 | // These synthetic intrinsics have no side-effects and just mark |
| 3059 | // information about their operands. |
| 3060 | // FIXME: There are other no-op synthetic instructions that potentially |
| 3061 | // should be considered at least *safe* to speculate... |
| 3062 | case Intrinsic::dbg_declare: |
| 3063 | case Intrinsic::dbg_value: |
| 3064 | return true; |
Chandler Carruth | 28192c9 | 2012-04-07 19:22:18 +0000 | [diff] [blame] | 3065 | |
Michael Liao | 736bac6 | 2014-11-06 19:05:57 +0000 | [diff] [blame] | 3066 | case Intrinsic::bswap: |
| 3067 | case Intrinsic::ctlz: |
| 3068 | case Intrinsic::ctpop: |
| 3069 | case Intrinsic::cttz: |
| 3070 | case Intrinsic::objectsize: |
| 3071 | case Intrinsic::sadd_with_overflow: |
| 3072 | case Intrinsic::smul_with_overflow: |
| 3073 | case Intrinsic::ssub_with_overflow: |
| 3074 | case Intrinsic::uadd_with_overflow: |
| 3075 | case Intrinsic::umul_with_overflow: |
| 3076 | case Intrinsic::usub_with_overflow: |
| 3077 | return true; |
| 3078 | // Sqrt should be OK, since the llvm sqrt intrinsic isn't defined to set |
| 3079 | // errno like libm sqrt would. |
| 3080 | case Intrinsic::sqrt: |
| 3081 | case Intrinsic::fma: |
| 3082 | case Intrinsic::fmuladd: |
| 3083 | case Intrinsic::fabs: |
| 3084 | case Intrinsic::minnum: |
| 3085 | case Intrinsic::maxnum: |
| 3086 | return true; |
| 3087 | // TODO: some fp intrinsics are marked as having the same error handling |
| 3088 | // as libm. They're safe to speculate when they won't error. |
| 3089 | // TODO: are convert_{from,to}_fp16 safe? |
| 3090 | // TODO: can we list target-specific intrinsics here? |
| 3091 | default: break; |
| 3092 | } |
| 3093 | } |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3094 | return false; // The called function could have undefined behavior or |
Nick Lewycky | b4039f6 | 2011-12-21 05:52:02 +0000 | [diff] [blame] | 3095 | // side-effects, even if marked readnone nounwind. |
| 3096 | } |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3097 | case Instruction::VAArg: |
| 3098 | case Instruction::Alloca: |
| 3099 | case Instruction::Invoke: |
| 3100 | case Instruction::PHI: |
| 3101 | case Instruction::Store: |
| 3102 | case Instruction::Ret: |
| 3103 | case Instruction::Br: |
| 3104 | case Instruction::IndirectBr: |
| 3105 | case Instruction::Switch: |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3106 | case Instruction::Unreachable: |
| 3107 | case Instruction::Fence: |
| 3108 | case Instruction::LandingPad: |
| 3109 | case Instruction::AtomicRMW: |
| 3110 | case Instruction::AtomicCmpXchg: |
| 3111 | case Instruction::Resume: |
| 3112 | return false; // Misc instructions which have effects |
| 3113 | } |
| 3114 | } |
Dan Gohman | 1b0f79d | 2013-01-31 02:40:59 +0000 | [diff] [blame] | 3115 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 3116 | /// Return true if we know that the specified value is never null. |
Benjamin Kramer | fd4777c | 2013-09-24 16:37:51 +0000 | [diff] [blame] | 3117 | bool llvm::isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI) { |
Dan Gohman | 1b0f79d | 2013-01-31 02:40:59 +0000 | [diff] [blame] | 3118 | // Alloca never returns null, malloc might. |
| 3119 | if (isa<AllocaInst>(V)) return true; |
| 3120 | |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 3121 | // A byval, inalloca, or nonnull argument is never null. |
Dan Gohman | 1b0f79d | 2013-01-31 02:40:59 +0000 | [diff] [blame] | 3122 | if (const Argument *A = dyn_cast<Argument>(V)) |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 3123 | return A->hasByValOrInAllocaAttr() || A->hasNonNullAttr(); |
Dan Gohman | 1b0f79d | 2013-01-31 02:40:59 +0000 | [diff] [blame] | 3124 | |
| 3125 | // Global values are not null unless extern weak. |
| 3126 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 3127 | return !GV->hasExternalWeakLinkage(); |
Benjamin Kramer | fd4777c | 2013-09-24 16:37:51 +0000 | [diff] [blame] | 3128 | |
Philip Reames | cdb72f3 | 2014-10-20 22:40:55 +0000 | [diff] [blame] | 3129 | // A Load tagged w/nonnull metadata is never null. |
| 3130 | if (const LoadInst *LI = dyn_cast<LoadInst>(V)) |
Philip Reames | 5a3f5f7 | 2014-10-21 00:13:20 +0000 | [diff] [blame] | 3131 | return LI->getMetadata(LLVMContext::MD_nonnull); |
Philip Reames | cdb72f3 | 2014-10-20 22:40:55 +0000 | [diff] [blame] | 3132 | |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 3133 | if (auto CS = ImmutableCallSite(V)) |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 3134 | if (CS.isReturnNonNull()) |
Nick Lewycky | ec37354 | 2014-05-20 05:13:21 +0000 | [diff] [blame] | 3135 | return true; |
| 3136 | |
Benjamin Kramer | fd4777c | 2013-09-24 16:37:51 +0000 | [diff] [blame] | 3137 | // operator new never returns null. |
| 3138 | if (isOperatorNewLikeFn(V, TLI, /*LookThroughBitCast=*/true)) |
| 3139 | return true; |
| 3140 | |
Dan Gohman | 1b0f79d | 2013-01-31 02:40:59 +0000 | [diff] [blame] | 3141 | return false; |
| 3142 | } |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3143 | |
| 3144 | OverflowResult llvm::computeOverflowForUnsignedMul(Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3145 | const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3146 | AssumptionCache *AC, |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3147 | const Instruction *CxtI, |
| 3148 | const DominatorTree *DT) { |
| 3149 | // Multiplying n * m significant bits yields a result of n + m significant |
| 3150 | // bits. If the total number of significant bits does not exceed the |
| 3151 | // result bit width (minus 1), there is no overflow. |
| 3152 | // This means if we have enough leading zero bits in the operands |
| 3153 | // we can guarantee that the result does not overflow. |
| 3154 | // Ref: "Hacker's Delight" by Henry Warren |
| 3155 | unsigned BitWidth = LHS->getType()->getScalarSizeInBits(); |
| 3156 | APInt LHSKnownZero(BitWidth, 0); |
David Majnemer | c8a576b | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3157 | APInt LHSKnownOne(BitWidth, 0); |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3158 | APInt RHSKnownZero(BitWidth, 0); |
David Majnemer | c8a576b | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3159 | APInt RHSKnownOne(BitWidth, 0); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3160 | computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, DL, /*Depth=*/0, AC, CxtI, |
| 3161 | DT); |
| 3162 | computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, DL, /*Depth=*/0, AC, CxtI, |
| 3163 | DT); |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3164 | // Note that underestimating the number of zero bits gives a more |
| 3165 | // conservative answer. |
| 3166 | unsigned ZeroBits = LHSKnownZero.countLeadingOnes() + |
| 3167 | RHSKnownZero.countLeadingOnes(); |
| 3168 | // First handle the easy case: if we have enough zero bits there's |
| 3169 | // definitely no overflow. |
| 3170 | if (ZeroBits >= BitWidth) |
| 3171 | return OverflowResult::NeverOverflows; |
| 3172 | |
| 3173 | // Get the largest possible values for each operand. |
| 3174 | APInt LHSMax = ~LHSKnownZero; |
| 3175 | APInt RHSMax = ~RHSKnownZero; |
| 3176 | |
| 3177 | // We know the multiply operation doesn't overflow if the maximum values for |
| 3178 | // each operand will not overflow after we multiply them together. |
David Majnemer | c8a576b | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3179 | bool MaxOverflow; |
| 3180 | LHSMax.umul_ov(RHSMax, MaxOverflow); |
| 3181 | if (!MaxOverflow) |
| 3182 | return OverflowResult::NeverOverflows; |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3183 | |
David Majnemer | c8a576b | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3184 | // We know it always overflows if multiplying the smallest possible values for |
| 3185 | // the operands also results in overflow. |
| 3186 | bool MinOverflow; |
| 3187 | LHSKnownOne.umul_ov(RHSKnownOne, MinOverflow); |
| 3188 | if (MinOverflow) |
| 3189 | return OverflowResult::AlwaysOverflows; |
| 3190 | |
| 3191 | return OverflowResult::MayOverflow; |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3192 | } |
David Majnemer | 5310c1e | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 3193 | |
| 3194 | OverflowResult llvm::computeOverflowForUnsignedAdd(Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3195 | const DataLayout &DL, |
David Majnemer | 5310c1e | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 3196 | AssumptionCache *AC, |
| 3197 | const Instruction *CxtI, |
| 3198 | const DominatorTree *DT) { |
| 3199 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 3200 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, DL, /*Depth=*/0, |
| 3201 | AC, CxtI, DT); |
| 3202 | if (LHSKnownNonNegative || LHSKnownNegative) { |
| 3203 | bool RHSKnownNonNegative, RHSKnownNegative; |
| 3204 | ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, DL, /*Depth=*/0, |
| 3205 | AC, CxtI, DT); |
| 3206 | |
| 3207 | if (LHSKnownNegative && RHSKnownNegative) { |
| 3208 | // The sign bit is set in both cases: this MUST overflow. |
| 3209 | // Create a simple add instruction, and insert it into the struct. |
| 3210 | return OverflowResult::AlwaysOverflows; |
| 3211 | } |
| 3212 | |
| 3213 | if (LHSKnownNonNegative && RHSKnownNonNegative) { |
| 3214 | // The sign bit is clear in both cases: this CANNOT overflow. |
| 3215 | // Create a simple add instruction, and insert it into the struct. |
| 3216 | return OverflowResult::NeverOverflows; |
| 3217 | } |
| 3218 | } |
| 3219 | |
| 3220 | return OverflowResult::MayOverflow; |
| 3221 | } |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3222 | |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame^] | 3223 | static SelectPatternFlavor matchSelectPattern(ICmpInst::Predicate Pred, |
| 3224 | Value *CmpLHS, Value *CmpRHS, |
| 3225 | Value *TrueVal, Value *FalseVal, |
| 3226 | Value *&LHS, Value *&RHS) { |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3227 | LHS = CmpLHS; |
| 3228 | RHS = CmpRHS; |
| 3229 | |
| 3230 | // (icmp X, Y) ? X : Y |
| 3231 | if (TrueVal == CmpLHS && FalseVal == CmpRHS) { |
| 3232 | switch (Pred) { |
| 3233 | default: return SPF_UNKNOWN; // Equality. |
| 3234 | case ICmpInst::ICMP_UGT: |
| 3235 | case ICmpInst::ICMP_UGE: return SPF_UMAX; |
| 3236 | case ICmpInst::ICMP_SGT: |
| 3237 | case ICmpInst::ICMP_SGE: return SPF_SMAX; |
| 3238 | case ICmpInst::ICMP_ULT: |
| 3239 | case ICmpInst::ICMP_ULE: return SPF_UMIN; |
| 3240 | case ICmpInst::ICMP_SLT: |
| 3241 | case ICmpInst::ICMP_SLE: return SPF_SMIN; |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | // (icmp X, Y) ? Y : X |
| 3246 | if (TrueVal == CmpRHS && FalseVal == CmpLHS) { |
| 3247 | switch (Pred) { |
| 3248 | default: return SPF_UNKNOWN; // Equality. |
| 3249 | case ICmpInst::ICMP_UGT: |
| 3250 | case ICmpInst::ICMP_UGE: return SPF_UMIN; |
| 3251 | case ICmpInst::ICMP_SGT: |
| 3252 | case ICmpInst::ICMP_SGE: return SPF_SMIN; |
| 3253 | case ICmpInst::ICMP_ULT: |
| 3254 | case ICmpInst::ICMP_ULE: return SPF_UMAX; |
| 3255 | case ICmpInst::ICMP_SLT: |
| 3256 | case ICmpInst::ICMP_SLE: return SPF_SMAX; |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(CmpRHS)) { |
| 3261 | if ((CmpLHS == TrueVal && match(FalseVal, m_Neg(m_Specific(CmpLHS)))) || |
| 3262 | (CmpLHS == FalseVal && match(TrueVal, m_Neg(m_Specific(CmpLHS))))) { |
| 3263 | |
| 3264 | // ABS(X) ==> (X >s 0) ? X : -X and (X >s -1) ? X : -X |
| 3265 | // NABS(X) ==> (X >s 0) ? -X : X and (X >s -1) ? -X : X |
| 3266 | if (Pred == ICmpInst::ICMP_SGT && (C1->isZero() || C1->isMinusOne())) { |
| 3267 | return (CmpLHS == TrueVal) ? SPF_ABS : SPF_NABS; |
| 3268 | } |
| 3269 | |
| 3270 | // ABS(X) ==> (X <s 0) ? -X : X and (X <s 1) ? -X : X |
| 3271 | // NABS(X) ==> (X <s 0) ? X : -X and (X <s 1) ? X : -X |
| 3272 | if (Pred == ICmpInst::ICMP_SLT && (C1->isZero() || C1->isOne())) { |
| 3273 | return (CmpLHS == FalseVal) ? SPF_ABS : SPF_NABS; |
| 3274 | } |
| 3275 | } |
| 3276 | |
| 3277 | // Y >s C ? ~Y : ~C == ~Y <s ~C ? ~Y : ~C = SMIN(~Y, ~C) |
| 3278 | if (const auto *C2 = dyn_cast<ConstantInt>(FalseVal)) { |
| 3279 | if (C1->getType() == C2->getType() && ~C1->getValue() == C2->getValue() && |
| 3280 | (match(TrueVal, m_Not(m_Specific(CmpLHS))) || |
| 3281 | match(CmpLHS, m_Not(m_Specific(TrueVal))))) { |
| 3282 | LHS = TrueVal; |
| 3283 | RHS = FalseVal; |
| 3284 | return SPF_SMIN; |
| 3285 | } |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | // TODO: (X > 4) ? X : 5 --> (X >= 5) ? X : 5 --> MAX(X, 5) |
| 3290 | |
| 3291 | return SPF_UNKNOWN; |
| 3292 | } |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame^] | 3293 | |
| 3294 | static Constant *lookThroughCast(ICmpInst *CmpI, Value *V1, Value *V2, |
| 3295 | Instruction::CastOps *CastOp) { |
| 3296 | CastInst *CI = dyn_cast<CastInst>(V1); |
| 3297 | Constant *C = dyn_cast<Constant>(V2); |
| 3298 | if (!CI || !C) |
| 3299 | return nullptr; |
| 3300 | *CastOp = CI->getOpcode(); |
| 3301 | |
| 3302 | if ((isa<SExtInst>(CI) && CmpI->isSigned()) || |
| 3303 | (isa<ZExtInst>(CI) && CmpI->isUnsigned())) |
| 3304 | return ConstantExpr::getTrunc(C, CI->getSrcTy()); |
| 3305 | |
| 3306 | if (isa<TruncInst>(CI)) |
| 3307 | return ConstantExpr::getIntegerCast(C, CI->getSrcTy(), CmpI->isSigned()); |
| 3308 | |
| 3309 | return nullptr; |
| 3310 | } |
| 3311 | |
| 3312 | SelectPatternFlavor llvm::matchSelectPattern(Value *V, |
| 3313 | Value *&LHS, Value *&RHS, |
| 3314 | Instruction::CastOps *CastOp) { |
| 3315 | SelectInst *SI = dyn_cast<SelectInst>(V); |
| 3316 | if (!SI) return SPF_UNKNOWN; |
| 3317 | |
| 3318 | ICmpInst *CmpI = dyn_cast<ICmpInst>(SI->getCondition()); |
| 3319 | if (!CmpI) return SPF_UNKNOWN; |
| 3320 | |
| 3321 | ICmpInst::Predicate Pred = CmpI->getPredicate(); |
| 3322 | Value *CmpLHS = CmpI->getOperand(0); |
| 3323 | Value *CmpRHS = CmpI->getOperand(1); |
| 3324 | Value *TrueVal = SI->getTrueValue(); |
| 3325 | Value *FalseVal = SI->getFalseValue(); |
| 3326 | |
| 3327 | // Bail out early. |
| 3328 | if (CmpI->isEquality()) |
| 3329 | return SPF_UNKNOWN; |
| 3330 | |
| 3331 | // Deal with type mismatches. |
| 3332 | if (CastOp && CmpLHS->getType() != TrueVal->getType()) { |
| 3333 | if (Constant *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) |
| 3334 | return ::matchSelectPattern(Pred, CmpLHS, CmpRHS, |
| 3335 | cast<CastInst>(TrueVal)->getOperand(0), C, |
| 3336 | LHS, RHS); |
| 3337 | if (Constant *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) |
| 3338 | return ::matchSelectPattern(Pred, CmpLHS, CmpRHS, |
| 3339 | C, cast<CastInst>(FalseVal)->getOperand(0), |
| 3340 | LHS, RHS); |
| 3341 | } |
| 3342 | return ::matchSelectPattern(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, |
| 3343 | LHS, RHS); |
| 3344 | } |