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 |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 46 | /// hard we search. The defaults are chosen to be fairly aggressive. If you |
Philip Reames | 1c29227 | 2015-03-10 22:43:20 +0000 | [diff] [blame] | 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: |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 554 | { |
| 555 | APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0); |
| 556 | if (LHS == V) |
| 557 | computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q); |
| 558 | else if (RHS == V) |
| 559 | computeKnownBits(LHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q); |
| 560 | else |
| 561 | llvm_unreachable("missing use?"); |
| 562 | KnownZero |= KnownZeroTemp; |
| 563 | KnownOne |= KnownOneTemp; |
| 564 | } |
Philip Reames | 1c29227 | 2015-03-10 22:43:20 +0000 | [diff] [blame] | 565 | break; |
| 566 | case ICmpInst::ICMP_ULE: |
| 567 | if (LHS == V) { |
| 568 | APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0); |
| 569 | computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q); |
| 570 | // The known zero bits carry over |
| 571 | unsigned SignBits = KnownZeroTemp.countLeadingOnes(); |
| 572 | KnownZero |= APInt::getHighBitsSet(BitWidth, SignBits); |
| 573 | } |
| 574 | break; |
| 575 | case ICmpInst::ICMP_ULT: |
| 576 | if (LHS == V) { |
| 577 | APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0); |
| 578 | computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q); |
| 579 | // Whatever high bits in rhs are zero are known to be zero (if rhs is a |
| 580 | // power of 2, then one more). |
| 581 | unsigned SignBits = KnownZeroTemp.countLeadingOnes(); |
| 582 | if (isKnownToBeAPowerOfTwo(RHS, false, Depth + 1, Query(Q, Cmp), DL)) |
| 583 | SignBits++; |
| 584 | KnownZero |= APInt::getHighBitsSet(BitWidth, SignBits); |
| 585 | } |
| 586 | break; |
| 587 | }; |
| 588 | } |
| 589 | |
| 590 | /// Compute known bits in 'V' from conditions which are known to be true along |
| 591 | /// all paths leading to the context instruction. In particular, look for |
| 592 | /// cases where one branch of an interesting condition dominates the context |
| 593 | /// instruction. This does not do general dataflow. |
| 594 | /// NOTE: This code is EXPERIMENTAL and currently off by default. |
| 595 | static void computeKnownBitsFromDominatingCondition(Value *V, APInt &KnownZero, |
| 596 | APInt &KnownOne, |
| 597 | const DataLayout &DL, |
| 598 | unsigned Depth, |
| 599 | const Query &Q) { |
| 600 | // Need both the dominator tree and the query location to do anything useful |
| 601 | if (!Q.DT || !Q.CxtI) |
| 602 | return; |
| 603 | Instruction *Cxt = const_cast<Instruction *>(Q.CxtI); |
| 604 | |
| 605 | // Avoid useless work |
| 606 | if (auto VI = dyn_cast<Instruction>(V)) |
| 607 | if (VI->getParent() == Cxt->getParent()) |
| 608 | return; |
| 609 | |
| 610 | // Note: We currently implement two options. It's not clear which of these |
| 611 | // will survive long term, we need data for that. |
| 612 | // Option 1 - Try walking the dominator tree looking for conditions which |
| 613 | // might apply. This works well for local conditions (loop guards, etc..), |
| 614 | // but not as well for things far from the context instruction (presuming a |
| 615 | // low max blocks explored). If we can set an high enough limit, this would |
| 616 | // be all we need. |
| 617 | // Option 2 - We restrict out search to those conditions which are uses of |
| 618 | // the value we're interested in. This is independent of dom structure, |
| 619 | // but is slightly less powerful without looking through lots of use chains. |
| 620 | // It does handle conditions far from the context instruction (e.g. early |
| 621 | // function exits on entry) really well though. |
| 622 | |
| 623 | // Option 1 - Search the dom tree |
| 624 | unsigned NumBlocksExplored = 0; |
| 625 | BasicBlock *Current = Cxt->getParent(); |
| 626 | while (true) { |
| 627 | // Stop searching if we've gone too far up the chain |
| 628 | if (NumBlocksExplored >= DomConditionsMaxDomBlocks) |
| 629 | break; |
| 630 | NumBlocksExplored++; |
| 631 | |
| 632 | if (!Q.DT->getNode(Current)->getIDom()) |
| 633 | break; |
| 634 | Current = Q.DT->getNode(Current)->getIDom()->getBlock(); |
| 635 | if (!Current) |
| 636 | // found function entry |
| 637 | break; |
| 638 | |
| 639 | BranchInst *BI = dyn_cast<BranchInst>(Current->getTerminator()); |
| 640 | if (!BI || BI->isUnconditional()) |
| 641 | continue; |
| 642 | ICmpInst *Cmp = dyn_cast<ICmpInst>(BI->getCondition()); |
| 643 | if (!Cmp) |
| 644 | continue; |
| 645 | |
| 646 | // We're looking for conditions that are guaranteed to hold at the context |
| 647 | // instruction. Finding a condition where one path dominates the context |
| 648 | // isn't enough because both the true and false cases could merge before |
| 649 | // the context instruction we're actually interested in. Instead, we need |
| 650 | // to ensure that the taken *edge* dominates the context instruction. |
| 651 | BasicBlock *BB0 = BI->getSuccessor(0); |
| 652 | BasicBlockEdge Edge(BI->getParent(), BB0); |
| 653 | if (!Edge.isSingleEdge() || !Q.DT->dominates(Edge, Q.CxtI->getParent())) |
| 654 | continue; |
| 655 | |
| 656 | computeKnownBitsFromTrueCondition(V, Cmp, KnownZero, KnownOne, DL, Depth, |
| 657 | Q); |
| 658 | } |
| 659 | |
| 660 | // Option 2 - Search the other uses of V |
| 661 | unsigned NumUsesExplored = 0; |
| 662 | for (auto U : V->users()) { |
| 663 | // Avoid massive lists |
| 664 | if (NumUsesExplored >= DomConditionsMaxUses) |
| 665 | break; |
| 666 | NumUsesExplored++; |
| 667 | // Consider only compare instructions uniquely controlling a branch |
| 668 | ICmpInst *Cmp = dyn_cast<ICmpInst>(U); |
| 669 | if (!Cmp) |
| 670 | continue; |
| 671 | |
| 672 | if (DomConditionsSingleCmpUse && !Cmp->hasOneUse()) |
| 673 | continue; |
| 674 | |
| 675 | for (auto *CmpU : Cmp->users()) { |
| 676 | BranchInst *BI = dyn_cast<BranchInst>(CmpU); |
| 677 | if (!BI || BI->isUnconditional()) |
| 678 | continue; |
| 679 | // We're looking for conditions that are guaranteed to hold at the |
| 680 | // context instruction. Finding a condition where one path dominates |
| 681 | // the context isn't enough because both the true and false cases could |
| 682 | // merge before the context instruction we're actually interested in. |
| 683 | // Instead, we need to ensure that the taken *edge* dominates the context |
| 684 | // instruction. |
| 685 | BasicBlock *BB0 = BI->getSuccessor(0); |
| 686 | BasicBlockEdge Edge(BI->getParent(), BB0); |
| 687 | if (!Edge.isSingleEdge() || !Q.DT->dominates(Edge, Q.CxtI->getParent())) |
| 688 | continue; |
| 689 | |
| 690 | computeKnownBitsFromTrueCondition(V, Cmp, KnownZero, KnownOne, DL, Depth, |
| 691 | Q); |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 696 | static void computeKnownBitsFromAssume(Value *V, APInt &KnownZero, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 697 | APInt &KnownOne, const DataLayout &DL, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 698 | unsigned Depth, const Query &Q) { |
| 699 | // Use of assumptions is context-sensitive. If we don't have a context, we |
| 700 | // cannot use them! |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 701 | if (!Q.AC || !Q.CxtI) |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 702 | return; |
| 703 | |
| 704 | unsigned BitWidth = KnownZero.getBitWidth(); |
| 705 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 706 | for (auto &AssumeVH : Q.AC->assumptions()) { |
| 707 | if (!AssumeVH) |
| 708 | continue; |
| 709 | CallInst *I = cast<CallInst>(AssumeVH); |
Chandler Carruth | 75c11b8 | 2015-01-04 23:13:57 +0000 | [diff] [blame] | 710 | assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() && |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 711 | "Got assumption for the wrong function!"); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 712 | if (Q.ExclInvs.count(I)) |
| 713 | continue; |
| 714 | |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 715 | // Warning: This loop can end up being somewhat performance sensetive. |
| 716 | // We're running this loop for once for each value queried resulting in a |
| 717 | // runtime of ~O(#assumes * #values). |
| 718 | |
Benjamin Kramer | 619c4e5 | 2015-04-10 11:24:51 +0000 | [diff] [blame] | 719 | assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume && |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 720 | "must be an assume intrinsic"); |
Benjamin Kramer | 619c4e5 | 2015-04-10 11:24:51 +0000 | [diff] [blame] | 721 | |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 722 | Value *Arg = I->getArgOperand(0); |
| 723 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 724 | if (Arg == V && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 725 | assert(BitWidth == 1 && "assume operand is not i1?"); |
| 726 | KnownZero.clearAllBits(); |
| 727 | KnownOne.setAllBits(); |
| 728 | return; |
| 729 | } |
| 730 | |
David Majnemer | 9b60975 | 2014-12-12 23:59:29 +0000 | [diff] [blame] | 731 | // The remaining tests are all recursive, so bail out if we hit the limit. |
| 732 | if (Depth == MaxDepth) |
| 733 | continue; |
| 734 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 735 | Value *A, *B; |
| 736 | auto m_V = m_CombineOr(m_Specific(V), |
| 737 | m_CombineOr(m_PtrToInt(m_Specific(V)), |
| 738 | m_BitCast(m_Specific(V)))); |
| 739 | |
| 740 | CmpInst::Predicate Pred; |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 741 | ConstantInt *C; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 742 | // assume(v = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 743 | if (match(Arg, m_c_ICmp(Pred, m_V, m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 744 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 745 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 746 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 747 | KnownZero |= RHSKnownZero; |
| 748 | KnownOne |= RHSKnownOne; |
| 749 | // assume(v & b = a) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 750 | } else if (match(Arg, |
| 751 | m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A))) && |
| 752 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 753 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 754 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 755 | APInt MaskKnownZero(BitWidth, 0), MaskKnownOne(BitWidth, 0); |
| 756 | computeKnownBits(B, MaskKnownZero, MaskKnownOne, DL, Depth+1, Query(Q, I)); |
| 757 | |
| 758 | // For those bits in the mask that are known to be one, we can propagate |
| 759 | // known bits from the RHS to V. |
| 760 | KnownZero |= RHSKnownZero & MaskKnownOne; |
| 761 | KnownOne |= RHSKnownOne & MaskKnownOne; |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 762 | // assume(~(v & b) = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 763 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))), |
| 764 | m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 765 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 766 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 767 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 768 | APInt MaskKnownZero(BitWidth, 0), MaskKnownOne(BitWidth, 0); |
| 769 | computeKnownBits(B, MaskKnownZero, MaskKnownOne, DL, Depth+1, Query(Q, I)); |
| 770 | |
| 771 | // For those bits in the mask that are known to be one, we can propagate |
| 772 | // inverted known bits from the RHS to V. |
| 773 | KnownZero |= RHSKnownOne & MaskKnownOne; |
| 774 | KnownOne |= RHSKnownZero & MaskKnownOne; |
| 775 | // assume(v | b = a) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 776 | } else if (match(Arg, |
| 777 | m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A))) && |
| 778 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 779 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 780 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 781 | APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0); |
| 782 | computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I)); |
| 783 | |
| 784 | // For those bits in B that are known to be zero, we can propagate known |
| 785 | // bits from the RHS to V. |
| 786 | KnownZero |= RHSKnownZero & BKnownZero; |
| 787 | KnownOne |= RHSKnownOne & BKnownZero; |
| 788 | // assume(~(v | b) = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 789 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))), |
| 790 | m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 791 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 792 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 793 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 794 | APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0); |
| 795 | computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I)); |
| 796 | |
| 797 | // For those bits in B that are known to be zero, we can propagate |
| 798 | // inverted known bits from the RHS to V. |
| 799 | KnownZero |= RHSKnownOne & BKnownZero; |
| 800 | KnownOne |= RHSKnownZero & BKnownZero; |
| 801 | // assume(v ^ b = a) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 802 | } else if (match(Arg, |
| 803 | m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A))) && |
| 804 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 805 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 806 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 807 | APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0); |
| 808 | computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I)); |
| 809 | |
| 810 | // For those bits in B that are known to be zero, we can propagate known |
| 811 | // bits from the RHS to V. For those bits in B that are known to be one, |
| 812 | // we can propagate inverted known bits from the RHS to V. |
| 813 | KnownZero |= RHSKnownZero & BKnownZero; |
| 814 | KnownOne |= RHSKnownOne & BKnownZero; |
| 815 | KnownZero |= RHSKnownOne & BKnownOne; |
| 816 | KnownOne |= RHSKnownZero & BKnownOne; |
| 817 | // assume(~(v ^ b) = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 818 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))), |
| 819 | m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 820 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 821 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 822 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 823 | APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0); |
| 824 | computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I)); |
| 825 | |
| 826 | // For those bits in B that are known to be zero, we can propagate |
| 827 | // inverted known bits from the RHS to V. For those bits in B that are |
| 828 | // known to be one, we can propagate known bits from the RHS to V. |
| 829 | KnownZero |= RHSKnownOne & BKnownZero; |
| 830 | KnownOne |= RHSKnownZero & BKnownZero; |
| 831 | KnownZero |= RHSKnownZero & BKnownOne; |
| 832 | KnownOne |= RHSKnownOne & BKnownOne; |
| 833 | // assume(v << c = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 834 | } else if (match(Arg, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)), |
| 835 | m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 836 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 837 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 838 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 839 | // For those bits in RHS that are known, we can propagate them to known |
| 840 | // bits in V shifted to the right by C. |
| 841 | KnownZero |= RHSKnownZero.lshr(C->getZExtValue()); |
| 842 | KnownOne |= RHSKnownOne.lshr(C->getZExtValue()); |
| 843 | // assume(~(v << c) = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 844 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))), |
| 845 | m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 846 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 847 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 848 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 849 | // For those bits in RHS that are known, we can propagate them inverted |
| 850 | // to known bits in V shifted to the right by C. |
| 851 | KnownZero |= RHSKnownOne.lshr(C->getZExtValue()); |
| 852 | KnownOne |= RHSKnownZero.lshr(C->getZExtValue()); |
| 853 | // assume(v >> c = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 854 | } else if (match(Arg, |
| 855 | 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] | 856 | m_AShr(m_V, m_ConstantInt(C))), |
| 857 | m_Value(A))) && |
| 858 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 859 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 860 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 861 | // For those bits in RHS that are known, we can propagate them to known |
| 862 | // bits in V shifted to the right by C. |
| 863 | KnownZero |= RHSKnownZero << C->getZExtValue(); |
| 864 | KnownOne |= RHSKnownOne << C->getZExtValue(); |
| 865 | // assume(~(v >> c) = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 866 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_CombineOr( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 867 | m_LShr(m_V, m_ConstantInt(C)), |
| 868 | m_AShr(m_V, m_ConstantInt(C)))), |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 869 | m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 870 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 871 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 872 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 873 | // For those bits in RHS that are known, we can propagate them inverted |
| 874 | // to known bits in V shifted to the right by C. |
| 875 | KnownZero |= RHSKnownOne << C->getZExtValue(); |
| 876 | KnownOne |= RHSKnownZero << C->getZExtValue(); |
| 877 | // assume(v >=_s c) where c is non-negative |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 878 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 879 | Pred == ICmpInst::ICMP_SGE && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 880 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 881 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 882 | |
| 883 | if (RHSKnownZero.isNegative()) { |
| 884 | // We know that the sign bit is zero. |
| 885 | KnownZero |= APInt::getSignBit(BitWidth); |
| 886 | } |
| 887 | // assume(v >_s c) where c is at least -1. |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 888 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 889 | Pred == ICmpInst::ICMP_SGT && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 890 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 891 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 892 | |
| 893 | if (RHSKnownOne.isAllOnesValue() || RHSKnownZero.isNegative()) { |
| 894 | // We know that the sign bit is zero. |
| 895 | KnownZero |= APInt::getSignBit(BitWidth); |
| 896 | } |
| 897 | // assume(v <=_s c) where c is negative |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 898 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 899 | Pred == ICmpInst::ICMP_SLE && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 900 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 901 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 902 | |
| 903 | if (RHSKnownOne.isNegative()) { |
| 904 | // We know that the sign bit is one. |
| 905 | KnownOne |= APInt::getSignBit(BitWidth); |
| 906 | } |
| 907 | // assume(v <_s c) where c is non-positive |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 908 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 909 | Pred == ICmpInst::ICMP_SLT && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 910 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 911 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 912 | |
| 913 | if (RHSKnownZero.isAllOnesValue() || RHSKnownOne.isNegative()) { |
| 914 | // We know that the sign bit is one. |
| 915 | KnownOne |= APInt::getSignBit(BitWidth); |
| 916 | } |
| 917 | // assume(v <=_u c) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 918 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 919 | Pred == ICmpInst::ICMP_ULE && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 920 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 921 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 922 | |
| 923 | // Whatever high bits in c are zero are known to be zero. |
| 924 | KnownZero |= |
| 925 | APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes()); |
| 926 | // assume(v <_u c) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 927 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 928 | Pred == ICmpInst::ICMP_ULT && isValidAssumeForContext(I, Q)) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 929 | APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0); |
| 930 | computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I)); |
| 931 | |
| 932 | // Whatever high bits in c are zero are known to be zero (if c is a power |
| 933 | // of 2, then one more). |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 934 | if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, Query(Q, I), DL)) |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 935 | KnownZero |= |
| 936 | APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes()+1); |
| 937 | else |
| 938 | KnownZero |= |
| 939 | APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes()); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 944 | static void computeKnownBitsFromOperator(Operator *I, APInt &KnownZero, |
| 945 | APInt &KnownOne, const DataLayout &DL, |
| 946 | unsigned Depth, const Query &Q) { |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 947 | unsigned BitWidth = KnownZero.getBitWidth(); |
| 948 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 949 | APInt KnownZero2(KnownZero), KnownOne2(KnownOne); |
Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 950 | switch (I->getOpcode()) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 951 | default: break; |
Rafael Espindola | 5319053 | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 952 | case Instruction::Load: |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 953 | if (MDNode *MD = cast<LoadInst>(I)->getMetadata(LLVMContext::MD_range)) |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 954 | computeKnownBitsFromRangeMetadata(*MD, KnownZero); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 955 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 956 | case Instruction::And: { |
| 957 | // 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] | 958 | computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q); |
| 959 | computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 960 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 961 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 962 | KnownOne &= KnownOne2; |
| 963 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 964 | KnownZero |= KnownZero2; |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 965 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 966 | } |
| 967 | case Instruction::Or: { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 968 | computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q); |
| 969 | computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 970 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 971 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 972 | KnownZero &= KnownZero2; |
| 973 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 974 | KnownOne |= KnownOne2; |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 975 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 976 | } |
| 977 | case Instruction::Xor: { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 978 | computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q); |
| 979 | computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 980 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 981 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 982 | APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
| 983 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 984 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 985 | KnownZero = KnownZeroOut; |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 986 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 987 | } |
| 988 | case Instruction::Mul: { |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 989 | bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 990 | computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, KnownZero, |
| 991 | KnownOne, KnownZero2, KnownOne2, DL, Depth, Q); |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 992 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 993 | } |
| 994 | case Instruction::UDiv: { |
| 995 | // For the purposes of computing leading zeros we can conservatively |
| 996 | // treat a udiv as a logical right shift by the power of 2 known to |
| 997 | // be less than the denominator. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 998 | computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 999 | unsigned LeadZ = KnownZero2.countLeadingOnes(); |
| 1000 | |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 1001 | KnownOne2.clearAllBits(); |
| 1002 | KnownZero2.clearAllBits(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1003 | computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1004 | unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros(); |
| 1005 | if (RHSUnknownLeadingOnes != BitWidth) |
| 1006 | LeadZ = std::min(BitWidth, |
| 1007 | LeadZ + BitWidth - RHSUnknownLeadingOnes - 1); |
| 1008 | |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1009 | KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1010 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1011 | } |
| 1012 | case Instruction::Select: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1013 | computeKnownBits(I->getOperand(2), KnownZero, KnownOne, DL, Depth + 1, Q); |
| 1014 | computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1015 | |
| 1016 | // Only known if known in both the LHS and RHS. |
| 1017 | KnownOne &= KnownOne2; |
| 1018 | KnownZero &= KnownZero2; |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1019 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1020 | case Instruction::FPTrunc: |
| 1021 | case Instruction::FPExt: |
| 1022 | case Instruction::FPToUI: |
| 1023 | case Instruction::FPToSI: |
| 1024 | case Instruction::SIToFP: |
| 1025 | case Instruction::UIToFP: |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1026 | break; // Can't work with floating point. |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1027 | case Instruction::PtrToInt: |
| 1028 | case Instruction::IntToPtr: |
Matt Arsenault | f1a7e62 | 2014-07-15 01:55:03 +0000 | [diff] [blame] | 1029 | case Instruction::AddrSpaceCast: // Pointers could be different sizes. |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1030 | // FALL THROUGH and handle them the same as zext/trunc. |
| 1031 | case Instruction::ZExt: |
| 1032 | case Instruction::Trunc: { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1033 | Type *SrcTy = I->getOperand(0)->getType(); |
Nadav Rotem | 15198e9 | 2012-10-26 17:17:05 +0000 | [diff] [blame] | 1034 | |
Chris Lattner | 0cdbc7a | 2009-09-08 00:13:52 +0000 | [diff] [blame] | 1035 | unsigned SrcBitWidth; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1036 | // Note that we handle pointer operands here because of inttoptr/ptrtoint |
| 1037 | // which fall through here. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1038 | SrcBitWidth = DL.getTypeSizeInBits(SrcTy->getScalarType()); |
Nadav Rotem | 15198e9 | 2012-10-26 17:17:05 +0000 | [diff] [blame] | 1039 | |
| 1040 | assert(SrcBitWidth && "SrcBitWidth can't be zero"); |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1041 | KnownZero = KnownZero.zextOrTrunc(SrcBitWidth); |
| 1042 | KnownOne = KnownOne.zextOrTrunc(SrcBitWidth); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1043 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q); |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1044 | KnownZero = KnownZero.zextOrTrunc(BitWidth); |
| 1045 | KnownOne = KnownOne.zextOrTrunc(BitWidth); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1046 | // Any top bits are known to be zero. |
| 1047 | if (BitWidth > SrcBitWidth) |
| 1048 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1049 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1050 | } |
| 1051 | case Instruction::BitCast: { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1052 | Type *SrcTy = I->getOperand(0)->getType(); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1053 | if ((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
Chris Lattner | edb8407 | 2009-07-02 16:04:08 +0000 | [diff] [blame] | 1054 | // TODO: For now, not handling conversions like: |
| 1055 | // (bitcast i64 %x to <2 x i32>) |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1056 | !I->getType()->isVectorTy()) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1057 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1058 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1059 | } |
| 1060 | break; |
| 1061 | } |
| 1062 | case Instruction::SExt: { |
| 1063 | // 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] | 1064 | unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits(); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1065 | |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1066 | KnownZero = KnownZero.trunc(SrcBitWidth); |
| 1067 | KnownOne = KnownOne.trunc(SrcBitWidth); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1068 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q); |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1069 | KnownZero = KnownZero.zext(BitWidth); |
| 1070 | KnownOne = KnownOne.zext(BitWidth); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1071 | |
| 1072 | // If the sign bit of the input is known set or clear, then we know the |
| 1073 | // top bits of the result. |
| 1074 | if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero |
| 1075 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
| 1076 | else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set |
| 1077 | KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1078 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1079 | } |
| 1080 | case Instruction::Shl: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1081 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1082 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1083 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1084 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1085 | KnownZero <<= ShiftAmt; |
| 1086 | KnownOne <<= ShiftAmt; |
| 1087 | KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0 |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1088 | } |
| 1089 | break; |
| 1090 | case Instruction::LShr: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1091 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1092 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1093 | // Compute the new bits that are at the top now. |
| 1094 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1095 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1096 | // Unsigned shift right. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1097 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1098 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 1099 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
| 1100 | // high bits known zero. |
| 1101 | KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1102 | } |
| 1103 | break; |
| 1104 | case Instruction::AShr: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1105 | // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1106 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1107 | // Compute the new bits that are at the top now. |
Chris Lattner | c86e67e | 2011-01-04 18:19:15 +0000 | [diff] [blame] | 1108 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1109 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1110 | // Signed shift right. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1111 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1112 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); |
| 1113 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1114 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1115 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
| 1116 | if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero. |
| 1117 | KnownZero |= HighBits; |
| 1118 | else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one. |
| 1119 | KnownOne |= HighBits; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1120 | } |
| 1121 | break; |
| 1122 | case Instruction::Sub: { |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1123 | bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap(); |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1124 | computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1125 | KnownZero, KnownOne, KnownZero2, KnownOne2, DL, |
| 1126 | Depth, Q); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1127 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1128 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1129 | case Instruction::Add: { |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1130 | bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap(); |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1131 | computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1132 | KnownZero, KnownOne, KnownZero2, KnownOne2, DL, |
| 1133 | Depth, Q); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1134 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1135 | } |
| 1136 | case Instruction::SRem: |
| 1137 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Duncan Sands | 26cd6bd | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 1138 | APInt RA = Rem->getValue().abs(); |
| 1139 | if (RA.isPowerOf2()) { |
| 1140 | APInt LowBits = RA - 1; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1141 | computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, |
| 1142 | Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1143 | |
Duncan Sands | 26cd6bd | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 1144 | // The low bits of the first operand are unchanged by the srem. |
| 1145 | KnownZero = KnownZero2 & LowBits; |
| 1146 | KnownOne = KnownOne2 & LowBits; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1147 | |
Duncan Sands | 26cd6bd | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 1148 | // If the first operand is non-negative or has all low bits zero, then |
| 1149 | // the upper bits are all zero. |
| 1150 | if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits)) |
| 1151 | KnownZero |= ~LowBits; |
| 1152 | |
| 1153 | // If the first operand is negative and not all low bits are zero, then |
| 1154 | // the upper bits are all one. |
| 1155 | if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0)) |
| 1156 | KnownOne |= ~LowBits; |
| 1157 | |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1158 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1159 | } |
| 1160 | } |
Nick Lewycky | e467979 | 2011-03-07 01:50:10 +0000 | [diff] [blame] | 1161 | |
| 1162 | // The sign bit is the LHS's sign bit, except when the result of the |
| 1163 | // remainder is zero. |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1164 | if (KnownZero.isNonNegative()) { |
Nick Lewycky | e467979 | 2011-03-07 01:50:10 +0000 | [diff] [blame] | 1165 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1166 | computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, DL, |
| 1167 | Depth + 1, Q); |
Nick Lewycky | e467979 | 2011-03-07 01:50:10 +0000 | [diff] [blame] | 1168 | // If it's known zero, our sign bit is also zero. |
| 1169 | if (LHSKnownZero.isNegative()) |
Duncan Sands | 34c4869 | 2012-04-30 11:56:58 +0000 | [diff] [blame] | 1170 | KnownZero.setBit(BitWidth - 1); |
Nick Lewycky | e467979 | 2011-03-07 01:50:10 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1173 | break; |
| 1174 | case Instruction::URem: { |
| 1175 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1176 | APInt RA = Rem->getValue(); |
| 1177 | if (RA.isPowerOf2()) { |
| 1178 | APInt LowBits = (RA - 1); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1179 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, |
| 1180 | Q); |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1181 | KnownZero |= ~LowBits; |
| 1182 | KnownOne &= LowBits; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1183 | break; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | // Since the result is less than or equal to either operand, any leading |
| 1188 | // zero bits in either operand must also exist in the result. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1189 | computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q); |
| 1190 | computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1191 | |
Chris Lattner | 4612ae1 | 2009-01-20 18:22:57 +0000 | [diff] [blame] | 1192 | unsigned Leaders = std::max(KnownZero.countLeadingOnes(), |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1193 | KnownZero2.countLeadingOnes()); |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 1194 | KnownOne.clearAllBits(); |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1195 | KnownZero = APInt::getHighBitsSet(BitWidth, Leaders); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1196 | break; |
| 1197 | } |
| 1198 | |
Victor Hernandez | a3aaf85 | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 1199 | case Instruction::Alloca: { |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1200 | AllocaInst *AI = cast<AllocaInst>(I); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1201 | unsigned Align = AI->getAlignment(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1202 | if (Align == 0) |
| 1203 | Align = DL.getABITypeAlignment(AI->getType()->getElementType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1204 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1205 | if (Align > 0) |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1206 | KnownZero = APInt::getLowBitsSet(BitWidth, countTrailingZeros(Align)); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1207 | break; |
| 1208 | } |
| 1209 | case Instruction::GetElementPtr: { |
| 1210 | // Analyze all of the subscripts of this getelementptr instruction |
| 1211 | // to determine if we can prove known low zero bits. |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1212 | APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1213 | computeKnownBits(I->getOperand(0), LocalKnownZero, LocalKnownOne, DL, |
| 1214 | Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1215 | unsigned TrailZ = LocalKnownZero.countTrailingOnes(); |
| 1216 | |
| 1217 | gep_type_iterator GTI = gep_type_begin(I); |
| 1218 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) { |
| 1219 | Value *Index = I->getOperand(i); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1220 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1221 | // Handle struct member offset arithmetic. |
Matt Arsenault | 74742a1 | 2013-08-19 21:43:16 +0000 | [diff] [blame] | 1222 | |
| 1223 | // Handle case when index is vector zeroinitializer |
| 1224 | Constant *CIndex = cast<Constant>(Index); |
| 1225 | if (CIndex->isZeroValue()) |
| 1226 | continue; |
| 1227 | |
| 1228 | if (CIndex->getType()->isVectorTy()) |
| 1229 | Index = CIndex->getSplatValue(); |
| 1230 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1231 | unsigned Idx = cast<ConstantInt>(Index)->getZExtValue(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1232 | const StructLayout *SL = DL.getStructLayout(STy); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1233 | uint64_t Offset = SL->getElementOffset(Idx); |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1234 | TrailZ = std::min<unsigned>(TrailZ, |
| 1235 | countTrailingZeros(Offset)); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1236 | } else { |
| 1237 | // Handle array index arithmetic. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1238 | Type *IndexedTy = GTI.getIndexedType(); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1239 | if (!IndexedTy->isSized()) { |
| 1240 | TrailZ = 0; |
| 1241 | break; |
| 1242 | } |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1243 | unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1244 | uint64_t TypeSize = DL.getTypeAllocSize(IndexedTy); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1245 | LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1246 | computeKnownBits(Index, LocalKnownZero, LocalKnownOne, DL, Depth + 1, |
| 1247 | Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1248 | TrailZ = std::min(TrailZ, |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1249 | unsigned(countTrailingZeros(TypeSize) + |
Chris Lattner | 4612ae1 | 2009-01-20 18:22:57 +0000 | [diff] [blame] | 1250 | LocalKnownZero.countTrailingOnes())); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1251 | } |
| 1252 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1253 | |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1254 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1255 | break; |
| 1256 | } |
| 1257 | case Instruction::PHI: { |
| 1258 | PHINode *P = cast<PHINode>(I); |
| 1259 | // Handle the case of a simple two-predecessor recurrence PHI. |
| 1260 | // There's a lot more that could theoretically be done here, but |
| 1261 | // this is sufficient to catch some interesting cases. |
| 1262 | if (P->getNumIncomingValues() == 2) { |
| 1263 | for (unsigned i = 0; i != 2; ++i) { |
| 1264 | Value *L = P->getIncomingValue(i); |
| 1265 | Value *R = P->getIncomingValue(!i); |
Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 1266 | Operator *LU = dyn_cast<Operator>(L); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1267 | if (!LU) |
| 1268 | continue; |
Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 1269 | unsigned Opcode = LU->getOpcode(); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1270 | // Check for operations that have the property that if |
| 1271 | // both their operands have low zero bits, the result |
| 1272 | // will have low zero bits. |
| 1273 | if (Opcode == Instruction::Add || |
| 1274 | Opcode == Instruction::Sub || |
| 1275 | Opcode == Instruction::And || |
| 1276 | Opcode == Instruction::Or || |
| 1277 | Opcode == Instruction::Mul) { |
| 1278 | Value *LL = LU->getOperand(0); |
| 1279 | Value *LR = LU->getOperand(1); |
| 1280 | // Find a recurrence. |
| 1281 | if (LL == I) |
| 1282 | L = LR; |
| 1283 | else if (LR == I) |
| 1284 | L = LL; |
| 1285 | else |
| 1286 | break; |
| 1287 | // Ok, we have a PHI of the form L op= R. Check for low |
| 1288 | // zero bits. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1289 | computeKnownBits(R, KnownZero2, KnownOne2, DL, Depth + 1, Q); |
David Greene | aebd9e0 | 2008-10-27 23:24:03 +0000 | [diff] [blame] | 1290 | |
| 1291 | // We need to take the minimum number of known bits |
| 1292 | APInt KnownZero3(KnownZero), KnownOne3(KnownOne); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1293 | computeKnownBits(L, KnownZero3, KnownOne3, DL, Depth + 1, Q); |
David Greene | aebd9e0 | 2008-10-27 23:24:03 +0000 | [diff] [blame] | 1294 | |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1295 | KnownZero = APInt::getLowBitsSet(BitWidth, |
David Greene | aebd9e0 | 2008-10-27 23:24:03 +0000 | [diff] [blame] | 1296 | std::min(KnownZero2.countTrailingOnes(), |
| 1297 | KnownZero3.countTrailingOnes())); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1298 | break; |
| 1299 | } |
| 1300 | } |
| 1301 | } |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1302 | |
Nick Lewycky | ac0b62c | 2011-02-10 23:54:10 +0000 | [diff] [blame] | 1303 | // Unreachable blocks may have zero-operand PHI nodes. |
| 1304 | if (P->getNumIncomingValues() == 0) |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1305 | break; |
Nick Lewycky | ac0b62c | 2011-02-10 23:54:10 +0000 | [diff] [blame] | 1306 | |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1307 | // Otherwise take the unions of the known bit sets of the operands, |
| 1308 | // taking conservative care to avoid excessive recursion. |
| 1309 | if (Depth < MaxDepth - 1 && !KnownZero && !KnownOne) { |
Duncan Sands | 7dc3d47 | 2011-03-08 12:39:03 +0000 | [diff] [blame] | 1310 | // Skip if every incoming value references to ourself. |
Nuno Lopes | 0d44a50 | 2012-07-03 21:15:40 +0000 | [diff] [blame] | 1311 | if (dyn_cast_or_null<UndefValue>(P->hasConstantValue())) |
Duncan Sands | 7dc3d47 | 2011-03-08 12:39:03 +0000 | [diff] [blame] | 1312 | break; |
| 1313 | |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1314 | KnownZero = APInt::getAllOnesValue(BitWidth); |
| 1315 | KnownOne = APInt::getAllOnesValue(BitWidth); |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 1316 | for (Value *IncValue : P->incoming_values()) { |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1317 | // Skip direct self references. |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 1318 | if (IncValue == P) continue; |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1319 | |
| 1320 | KnownZero2 = APInt(BitWidth, 0); |
| 1321 | KnownOne2 = APInt(BitWidth, 0); |
| 1322 | // Recurse, but cap the recursion to one level, because we don't |
| 1323 | // want to waste time spinning around in loops. |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 1324 | computeKnownBits(IncValue, KnownZero2, KnownOne2, DL, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1325 | MaxDepth - 1, Q); |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1326 | KnownZero &= KnownZero2; |
| 1327 | KnownOne &= KnownOne2; |
| 1328 | // If all bits have been ruled out, there's no need to check |
| 1329 | // more operands. |
| 1330 | if (!KnownZero && !KnownOne) |
| 1331 | break; |
| 1332 | } |
| 1333 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1334 | break; |
| 1335 | } |
| 1336 | case Instruction::Call: |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 1337 | case Instruction::Invoke: |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1338 | if (MDNode *MD = cast<Instruction>(I)->getMetadata(LLVMContext::MD_range)) |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 1339 | computeKnownBitsFromRangeMetadata(*MD, KnownZero); |
| 1340 | // If a range metadata is attached to this IntrinsicInst, intersect the |
| 1341 | // explicit range specified by the metadata and the implicit range of |
| 1342 | // the intrinsic. |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1343 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 1344 | switch (II->getIntrinsicID()) { |
| 1345 | default: break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1346 | case Intrinsic::ctlz: |
| 1347 | case Intrinsic::cttz: { |
| 1348 | unsigned LowBits = Log2_32(BitWidth)+1; |
Benjamin Kramer | 4ee5747 | 2011-12-24 17:31:46 +0000 | [diff] [blame] | 1349 | // If this call is undefined for 0, the result will be less than 2^n. |
| 1350 | if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext())) |
| 1351 | LowBits -= 1; |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 1352 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - LowBits); |
Benjamin Kramer | 4ee5747 | 2011-12-24 17:31:46 +0000 | [diff] [blame] | 1353 | break; |
| 1354 | } |
| 1355 | case Intrinsic::ctpop: { |
| 1356 | unsigned LowBits = Log2_32(BitWidth)+1; |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 1357 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - LowBits); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1358 | break; |
| 1359 | } |
Chad Rosier | b362884 | 2011-05-26 23:13:19 +0000 | [diff] [blame] | 1360 | case Intrinsic::x86_sse42_crc32_64_64: |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 1361 | KnownZero |= APInt::getHighBitsSet(64, 32); |
Evan Cheng | 2a746bf | 2011-05-22 18:25:30 +0000 | [diff] [blame] | 1362 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | break; |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1366 | case Instruction::ExtractValue: |
| 1367 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) { |
| 1368 | ExtractValueInst *EVI = cast<ExtractValueInst>(I); |
| 1369 | if (EVI->getNumIndices() != 1) break; |
| 1370 | if (EVI->getIndices()[0] == 0) { |
| 1371 | switch (II->getIntrinsicID()) { |
| 1372 | default: break; |
| 1373 | case Intrinsic::uadd_with_overflow: |
| 1374 | case Intrinsic::sadd_with_overflow: |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1375 | computeKnownBitsAddSub(true, II->getArgOperand(0), |
| 1376 | II->getArgOperand(1), false, KnownZero, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1377 | KnownOne, KnownZero2, KnownOne2, DL, Depth, Q); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1378 | break; |
| 1379 | case Intrinsic::usub_with_overflow: |
| 1380 | case Intrinsic::ssub_with_overflow: |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1381 | computeKnownBitsAddSub(false, II->getArgOperand(0), |
| 1382 | II->getArgOperand(1), false, KnownZero, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1383 | KnownOne, KnownZero2, KnownOne2, DL, Depth, Q); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1384 | break; |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 1385 | case Intrinsic::umul_with_overflow: |
| 1386 | case Intrinsic::smul_with_overflow: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1387 | computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false, |
| 1388 | KnownZero, KnownOne, KnownZero2, KnownOne2, DL, |
| 1389 | Depth, Q); |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 1390 | break; |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1391 | } |
| 1392 | } |
| 1393 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1394 | } |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | /// Determine which bits of V are known to be either zero or one and return |
| 1398 | /// them in the KnownZero/KnownOne bit sets. |
| 1399 | /// |
| 1400 | /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 1401 | /// we cannot optimize based on the assumption that it is zero without changing |
| 1402 | /// it to be an explicit zero. If we don't change it to zero, other code could |
| 1403 | /// optimized based on the contradictory assumption that it is non-zero. |
| 1404 | /// Because instcombine aggressively folds operations with undef args anyway, |
| 1405 | /// this won't lose us code quality. |
| 1406 | /// |
| 1407 | /// This function is defined on values with integer type, values with pointer |
| 1408 | /// type, and vectors of integers. In the case |
| 1409 | /// where V is a vector, known zero, and known one values are the |
| 1410 | /// same width as the vector element, and the bit is set only if it is true |
| 1411 | /// for all of the elements in the vector. |
| 1412 | void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, |
| 1413 | const DataLayout &DL, unsigned Depth, const Query &Q) { |
| 1414 | assert(V && "No Value?"); |
| 1415 | assert(Depth <= MaxDepth && "Limit Search Depth"); |
| 1416 | unsigned BitWidth = KnownZero.getBitWidth(); |
| 1417 | |
| 1418 | assert((V->getType()->isIntOrIntVectorTy() || |
| 1419 | V->getType()->getScalarType()->isPointerTy()) && |
| 1420 | "Not integer or pointer type!"); |
| 1421 | assert((DL.getTypeSizeInBits(V->getType()->getScalarType()) == BitWidth) && |
| 1422 | (!V->getType()->isIntOrIntVectorTy() || |
| 1423 | V->getType()->getScalarSizeInBits() == BitWidth) && |
| 1424 | KnownZero.getBitWidth() == BitWidth && |
| 1425 | KnownOne.getBitWidth() == BitWidth && |
| 1426 | "V, KnownOne and KnownZero should have same BitWidth"); |
| 1427 | |
| 1428 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 1429 | // We know all of the bits for a constant! |
| 1430 | KnownOne = CI->getValue(); |
| 1431 | KnownZero = ~KnownOne; |
| 1432 | return; |
| 1433 | } |
| 1434 | // Null and aggregate-zero are all-zeros. |
| 1435 | if (isa<ConstantPointerNull>(V) || |
| 1436 | isa<ConstantAggregateZero>(V)) { |
| 1437 | KnownOne.clearAllBits(); |
| 1438 | KnownZero = APInt::getAllOnesValue(BitWidth); |
| 1439 | return; |
| 1440 | } |
| 1441 | // Handle a constant vector by taking the intersection of the known bits of |
| 1442 | // each element. There is no real need to handle ConstantVector here, because |
| 1443 | // we don't handle undef in any particularly useful way. |
| 1444 | if (ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) { |
| 1445 | // We know that CDS must be a vector of integers. Take the intersection of |
| 1446 | // each element. |
| 1447 | KnownZero.setAllBits(); KnownOne.setAllBits(); |
| 1448 | APInt Elt(KnownZero.getBitWidth(), 0); |
| 1449 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { |
| 1450 | Elt = CDS->getElementAsInteger(i); |
| 1451 | KnownZero &= ~Elt; |
| 1452 | KnownOne &= Elt; |
| 1453 | } |
| 1454 | return; |
| 1455 | } |
| 1456 | |
| 1457 | // The address of an aligned GlobalValue has trailing zeros. |
| 1458 | if (auto *GO = dyn_cast<GlobalObject>(V)) { |
| 1459 | unsigned Align = GO->getAlignment(); |
| 1460 | if (Align == 0) { |
| 1461 | if (auto *GVar = dyn_cast<GlobalVariable>(GO)) { |
| 1462 | Type *ObjectType = GVar->getType()->getElementType(); |
| 1463 | if (ObjectType->isSized()) { |
| 1464 | // If the object is defined in the current Module, we'll be giving |
| 1465 | // it the preferred alignment. Otherwise, we have to assume that it |
| 1466 | // may only have the minimum ABI alignment. |
Peter Collingbourne | 6a9d177 | 2015-07-05 20:52:35 +0000 | [diff] [blame] | 1467 | if (GVar->isStrongDefinitionForLinker()) |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1468 | Align = DL.getPreferredAlignment(GVar); |
| 1469 | else |
| 1470 | Align = DL.getABITypeAlignment(ObjectType); |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | if (Align > 0) |
| 1475 | KnownZero = APInt::getLowBitsSet(BitWidth, |
| 1476 | countTrailingZeros(Align)); |
| 1477 | else |
| 1478 | KnownZero.clearAllBits(); |
| 1479 | KnownOne.clearAllBits(); |
| 1480 | return; |
| 1481 | } |
| 1482 | |
| 1483 | if (Argument *A = dyn_cast<Argument>(V)) { |
| 1484 | unsigned Align = A->getType()->isPointerTy() ? A->getParamAlignment() : 0; |
| 1485 | |
| 1486 | if (!Align && A->hasStructRetAttr()) { |
| 1487 | // An sret parameter has at least the ABI alignment of the return type. |
| 1488 | Type *EltTy = cast<PointerType>(A->getType())->getElementType(); |
| 1489 | if (EltTy->isSized()) |
| 1490 | Align = DL.getABITypeAlignment(EltTy); |
| 1491 | } |
| 1492 | |
| 1493 | if (Align) |
| 1494 | KnownZero = APInt::getLowBitsSet(BitWidth, countTrailingZeros(Align)); |
| 1495 | else |
| 1496 | KnownZero.clearAllBits(); |
| 1497 | KnownOne.clearAllBits(); |
| 1498 | |
| 1499 | // Don't give up yet... there might be an assumption that provides more |
| 1500 | // information... |
| 1501 | computeKnownBitsFromAssume(V, KnownZero, KnownOne, DL, Depth, Q); |
| 1502 | |
| 1503 | // Or a dominating condition for that matter |
| 1504 | if (EnableDomConditions && Depth <= DomConditionsMaxDepth) |
| 1505 | computeKnownBitsFromDominatingCondition(V, KnownZero, KnownOne, DL, |
| 1506 | Depth, Q); |
| 1507 | return; |
| 1508 | } |
| 1509 | |
| 1510 | // Start out not knowing anything. |
| 1511 | KnownZero.clearAllBits(); KnownOne.clearAllBits(); |
| 1512 | |
| 1513 | // Limit search depth. |
| 1514 | // All recursive calls that increase depth must come after this. |
| 1515 | if (Depth == MaxDepth) |
| 1516 | return; |
| 1517 | |
| 1518 | // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has |
| 1519 | // the bits of its aliasee. |
| 1520 | if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
| 1521 | if (!GA->mayBeOverridden()) |
| 1522 | computeKnownBits(GA->getAliasee(), KnownZero, KnownOne, DL, Depth + 1, Q); |
| 1523 | return; |
| 1524 | } |
| 1525 | |
| 1526 | if (Operator *I = dyn_cast<Operator>(V)) |
| 1527 | computeKnownBitsFromOperator(I, KnownZero, KnownOne, DL, Depth, Q); |
| 1528 | // computeKnownBitsFromAssume and computeKnownBitsFromDominatingCondition |
| 1529 | // strictly refines KnownZero and KnownOne. Therefore, we run them after |
| 1530 | // computeKnownBitsFromOperator. |
| 1531 | |
| 1532 | // Check whether a nearby assume intrinsic can determine some known bits. |
| 1533 | computeKnownBitsFromAssume(V, KnownZero, KnownOne, DL, Depth, Q); |
| 1534 | |
| 1535 | // Check whether there's a dominating condition which implies something about |
| 1536 | // this value at the given context. |
| 1537 | if (EnableDomConditions && Depth <= DomConditionsMaxDepth) |
| 1538 | computeKnownBitsFromDominatingCondition(V, KnownZero, KnownOne, DL, Depth, |
| 1539 | Q); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1540 | |
| 1541 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1544 | /// Determine whether the sign bit is known to be zero or one. |
| 1545 | /// Convenience wrapper around computeKnownBits. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1546 | void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1547 | const DataLayout &DL, unsigned Depth, const Query &Q) { |
| 1548 | unsigned BitWidth = getBitWidth(V->getType(), DL); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1549 | if (!BitWidth) { |
| 1550 | KnownZero = false; |
| 1551 | KnownOne = false; |
| 1552 | return; |
| 1553 | } |
| 1554 | APInt ZeroBits(BitWidth, 0); |
| 1555 | APInt OneBits(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1556 | computeKnownBits(V, ZeroBits, OneBits, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1557 | KnownOne = OneBits[BitWidth - 1]; |
| 1558 | KnownZero = ZeroBits[BitWidth - 1]; |
| 1559 | } |
| 1560 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1561 | /// Return true if the given value is known to have exactly one |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1562 | /// 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] | 1563 | /// 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] | 1564 | /// types and vectors of integers. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1565 | bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero, unsigned Depth, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1566 | const Query &Q, const DataLayout &DL) { |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1567 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 1568 | if (C->isNullValue()) |
| 1569 | return OrZero; |
| 1570 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 1571 | return CI->getValue().isPowerOf2(); |
| 1572 | // TODO: Handle vector constants. |
| 1573 | } |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1574 | |
| 1575 | // 1 << X is clearly a power of two if the one is not shifted off the end. If |
| 1576 | // it is shifted off the end then the result is undefined. |
| 1577 | if (match(V, m_Shl(m_One(), m_Value()))) |
| 1578 | return true; |
| 1579 | |
| 1580 | // (signbit) >>l X is clearly a power of two if the one is not shifted off the |
| 1581 | // 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] | 1582 | if (match(V, m_LShr(m_SignBit(), m_Value()))) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1583 | return true; |
| 1584 | |
| 1585 | // The remaining tests are all recursive, so bail out if we hit the limit. |
| 1586 | if (Depth++ == MaxDepth) |
| 1587 | return false; |
| 1588 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1589 | Value *X = nullptr, *Y = nullptr; |
Duncan Sands | 985ba63 | 2011-10-28 18:30:05 +0000 | [diff] [blame] | 1590 | // A shift of a power of two is a power of two or zero. |
| 1591 | if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) || |
| 1592 | match(V, m_Shr(m_Value(X), m_Value())))) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1593 | return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q, DL); |
Duncan Sands | 985ba63 | 2011-10-28 18:30:05 +0000 | [diff] [blame] | 1594 | |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1595 | if (ZExtInst *ZI = dyn_cast<ZExtInst>(V)) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1596 | return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q, DL); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1597 | |
| 1598 | if (SelectInst *SI = dyn_cast<SelectInst>(V)) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1599 | return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q, DL) && |
| 1600 | isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q, DL); |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1601 | |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1602 | if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) { |
| 1603 | // 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] | 1604 | if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q, DL) || |
| 1605 | isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q, DL)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1606 | return true; |
| 1607 | // X & (-X) is always a power of two or zero. |
| 1608 | if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X)))) |
| 1609 | return true; |
| 1610 | return false; |
| 1611 | } |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1612 | |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1613 | // Adding a power-of-two or zero to the same power-of-two or zero yields |
| 1614 | // either the original power-of-two, a larger power-of-two or zero. |
| 1615 | if (match(V, m_Add(m_Value(X), m_Value(Y)))) { |
| 1616 | OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V); |
| 1617 | if (OrZero || VOBO->hasNoUnsignedWrap() || VOBO->hasNoSignedWrap()) { |
| 1618 | if (match(X, m_And(m_Specific(Y), m_Value())) || |
| 1619 | match(X, m_And(m_Value(), m_Specific(Y)))) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1620 | if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q, DL)) |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1621 | return true; |
| 1622 | if (match(Y, m_And(m_Specific(X), m_Value())) || |
| 1623 | match(Y, m_And(m_Value(), m_Specific(X)))) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1624 | if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q, DL)) |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1625 | return true; |
| 1626 | |
| 1627 | unsigned BitWidth = V->getType()->getScalarSizeInBits(); |
| 1628 | APInt LHSZeroBits(BitWidth, 0), LHSOneBits(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1629 | computeKnownBits(X, LHSZeroBits, LHSOneBits, DL, Depth, Q); |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1630 | |
| 1631 | APInt RHSZeroBits(BitWidth, 0), RHSOneBits(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1632 | computeKnownBits(Y, RHSZeroBits, RHSOneBits, DL, Depth, Q); |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1633 | // If i8 V is a power of two or zero: |
| 1634 | // ZeroBits: 1 1 1 0 1 1 1 1 |
| 1635 | // ~ZeroBits: 0 0 0 1 0 0 0 0 |
| 1636 | if ((~(LHSZeroBits & RHSZeroBits)).isPowerOf2()) |
| 1637 | // If OrZero isn't set, we cannot give back a zero result. |
| 1638 | // Make sure either the LHS or RHS has a bit set. |
| 1639 | if (OrZero || RHSOneBits.getBoolValue() || LHSOneBits.getBoolValue()) |
| 1640 | return true; |
| 1641 | } |
| 1642 | } |
David Majnemer | beab567 | 2013-05-18 19:30:37 +0000 | [diff] [blame] | 1643 | |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1644 | // 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] | 1645 | // is a power of two only if the first operand is a power of two and not |
| 1646 | // copying a sign bit (sdiv int_min, 2). |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1647 | if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) || |
| 1648 | match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1649 | return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1650 | Depth, Q, DL); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1653 | return false; |
| 1654 | } |
| 1655 | |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1656 | /// \brief Test whether a GEP's result is known to be non-null. |
| 1657 | /// |
| 1658 | /// Uses properties inherent in a GEP to try to determine whether it is known |
| 1659 | /// to be non-null. |
| 1660 | /// |
| 1661 | /// Currently this routine does not support vector GEPs. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1662 | static bool isGEPKnownNonNull(GEPOperator *GEP, const DataLayout &DL, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1663 | unsigned Depth, const Query &Q) { |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1664 | if (!GEP->isInBounds() || GEP->getPointerAddressSpace() != 0) |
| 1665 | return false; |
| 1666 | |
| 1667 | // FIXME: Support vector-GEPs. |
| 1668 | assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP"); |
| 1669 | |
| 1670 | // If the base pointer is non-null, we cannot walk to a null address with an |
| 1671 | // inbounds GEP in address space zero. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1672 | if (isKnownNonZero(GEP->getPointerOperand(), DL, Depth, Q)) |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1673 | return true; |
| 1674 | |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1675 | // Walk the GEP operands and see if any operand introduces a non-zero offset. |
| 1676 | // If so, then the GEP cannot produce a null pointer, as doing so would |
| 1677 | // inherently violate the inbounds contract within address space zero. |
| 1678 | for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP); |
| 1679 | GTI != GTE; ++GTI) { |
| 1680 | // Struct types are easy -- they must always be indexed by a constant. |
| 1681 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 1682 | ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand()); |
| 1683 | unsigned ElementIdx = OpC->getZExtValue(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1684 | const StructLayout *SL = DL.getStructLayout(STy); |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1685 | uint64_t ElementOffset = SL->getElementOffset(ElementIdx); |
| 1686 | if (ElementOffset > 0) |
| 1687 | return true; |
| 1688 | continue; |
| 1689 | } |
| 1690 | |
| 1691 | // 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] | 1692 | if (DL.getTypeAllocSize(GTI.getIndexedType()) == 0) |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1693 | continue; |
| 1694 | |
| 1695 | // Fast path the constant operand case both for efficiency and so we don't |
| 1696 | // increment Depth when just zipping down an all-constant GEP. |
| 1697 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) { |
| 1698 | if (!OpC->isZero()) |
| 1699 | return true; |
| 1700 | continue; |
| 1701 | } |
| 1702 | |
| 1703 | // We post-increment Depth here because while isKnownNonZero increments it |
| 1704 | // as well, when we pop back up that increment won't persist. We don't want |
| 1705 | // to recurse 10k times just because we have 10k GEP operands. We don't |
| 1706 | // bail completely out because we want to handle constant GEPs regardless |
| 1707 | // of depth. |
| 1708 | if (Depth++ >= MaxDepth) |
| 1709 | continue; |
| 1710 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1711 | if (isKnownNonZero(GTI.getOperand(), DL, Depth, Q)) |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1712 | return true; |
| 1713 | } |
| 1714 | |
| 1715 | return false; |
| 1716 | } |
| 1717 | |
Philip Reames | 4cb4d3e | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1718 | /// Does the 'Range' metadata (which must be a valid MD_range operand list) |
| 1719 | /// ensure that the value it's attached to is never Value? 'RangeType' is |
| 1720 | /// is the type of the value described by the range. |
| 1721 | static bool rangeMetadataExcludesValue(MDNode* Ranges, |
| 1722 | const APInt& Value) { |
| 1723 | const unsigned NumRanges = Ranges->getNumOperands() / 2; |
| 1724 | assert(NumRanges >= 1); |
| 1725 | for (unsigned i = 0; i < NumRanges; ++i) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1726 | ConstantInt *Lower = |
| 1727 | mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0)); |
| 1728 | ConstantInt *Upper = |
| 1729 | mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1)); |
Philip Reames | 4cb4d3e | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1730 | ConstantRange Range(Lower->getValue(), Upper->getValue()); |
| 1731 | if (Range.contains(Value)) |
| 1732 | return false; |
| 1733 | } |
| 1734 | return true; |
| 1735 | } |
| 1736 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1737 | /// Return true if the given value is known to be non-zero when defined. |
| 1738 | /// For vectors return true if every element is known to be non-zero when |
| 1739 | /// defined. Supports values with integer or pointer type and vectors of |
| 1740 | /// integers. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1741 | bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1742 | const Query &Q) { |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1743 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 1744 | if (C->isNullValue()) |
| 1745 | return false; |
| 1746 | if (isa<ConstantInt>(C)) |
| 1747 | // Must be non-zero due to null test above. |
| 1748 | return true; |
| 1749 | // TODO: Handle vectors |
| 1750 | return false; |
| 1751 | } |
| 1752 | |
Philip Reames | 4cb4d3e | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1753 | if (Instruction* I = dyn_cast<Instruction>(V)) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1754 | if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range)) { |
Philip Reames | 4cb4d3e | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1755 | // If the possible ranges don't contain zero, then the value is |
| 1756 | // definitely non-zero. |
| 1757 | if (IntegerType* Ty = dyn_cast<IntegerType>(V->getType())) { |
| 1758 | const APInt ZeroValue(Ty->getBitWidth(), 0); |
| 1759 | if (rangeMetadataExcludesValue(Ranges, ZeroValue)) |
| 1760 | return true; |
| 1761 | } |
| 1762 | } |
| 1763 | } |
| 1764 | |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1765 | // 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] | 1766 | if (Depth++ >= MaxDepth) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1767 | return false; |
| 1768 | |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1769 | // Check for pointer simplifications. |
| 1770 | if (V->getType()->isPointerTy()) { |
Manman Ren | 1217112 | 2013-03-18 21:23:25 +0000 | [diff] [blame] | 1771 | if (isKnownNonNull(V)) |
| 1772 | return true; |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1773 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1774 | if (isGEPKnownNonNull(GEP, DL, Depth, Q)) |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1775 | return true; |
| 1776 | } |
| 1777 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1778 | unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), DL); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1779 | |
| 1780 | // X | Y != 0 if X != 0 or Y != 0. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1781 | Value *X = nullptr, *Y = nullptr; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1782 | if (match(V, m_Or(m_Value(X), m_Value(Y)))) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1783 | return isKnownNonZero(X, DL, Depth, Q) || isKnownNonZero(Y, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1784 | |
| 1785 | // ext X != 0 if X != 0. |
| 1786 | if (isa<SExtInst>(V) || isa<ZExtInst>(V)) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1787 | return isKnownNonZero(cast<Instruction>(V)->getOperand(0), DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1788 | |
Duncan Sands | 2e9e4f1 | 2011-01-29 13:27:00 +0000 | [diff] [blame] | 1789 | // 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] | 1790 | // if the lowest bit is shifted off the end. |
| 1791 | if (BitWidth && match(V, m_Shl(m_Value(X), m_Value(Y)))) { |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1792 | // shl nuw can't remove any non-zero bits. |
Duncan Sands | 7cb61e5 | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 1793 | OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1794 | if (BO->hasNoUnsignedWrap()) |
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 | APInt KnownZero(BitWidth, 0); |
| 1798 | APInt KnownOne(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1799 | computeKnownBits(X, KnownZero, KnownOne, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1800 | if (KnownOne[0]) |
| 1801 | return true; |
| 1802 | } |
Duncan Sands | 2e9e4f1 | 2011-01-29 13:27:00 +0000 | [diff] [blame] | 1803 | // 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] | 1804 | // defined if the sign bit is shifted off the end. |
| 1805 | else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) { |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1806 | // shr exact can only shift out zero bits. |
Duncan Sands | 7cb61e5 | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 1807 | PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1808 | if (BO->isExact()) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1809 | return isKnownNonZero(X, DL, Depth, Q); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1810 | |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1811 | bool XKnownNonNegative, XKnownNegative; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1812 | ComputeSignBit(X, XKnownNonNegative, XKnownNegative, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1813 | if (XKnownNegative) |
| 1814 | return true; |
| 1815 | } |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1816 | // div exact can only produce a zero if the dividend is zero. |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1817 | 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] | 1818 | return isKnownNonZero(X, DL, Depth, Q); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1819 | } |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1820 | // X + Y. |
| 1821 | else if (match(V, m_Add(m_Value(X), m_Value(Y)))) { |
| 1822 | bool XKnownNonNegative, XKnownNegative; |
| 1823 | bool YKnownNonNegative, YKnownNegative; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1824 | ComputeSignBit(X, XKnownNonNegative, XKnownNegative, DL, Depth, Q); |
| 1825 | ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1826 | |
| 1827 | // 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] | 1828 | // zero unless both X and Y are zero. |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1829 | if (XKnownNonNegative && YKnownNonNegative) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1830 | if (isKnownNonZero(X, DL, Depth, Q) || isKnownNonZero(Y, DL, Depth, Q)) |
Duncan Sands | 9e9d5b2 | 2011-01-25 15:14:15 +0000 | [diff] [blame] | 1831 | return true; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1832 | |
| 1833 | // If X and Y are both negative (as signed values) then their sum is not |
| 1834 | // zero unless both X and Y equal INT_MIN. |
| 1835 | if (BitWidth && XKnownNegative && YKnownNegative) { |
| 1836 | APInt KnownZero(BitWidth, 0); |
| 1837 | APInt KnownOne(BitWidth, 0); |
| 1838 | APInt Mask = APInt::getSignedMaxValue(BitWidth); |
| 1839 | // The sign bit of X is set. If some other bit is set then X is not equal |
| 1840 | // to INT_MIN. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1841 | computeKnownBits(X, KnownZero, KnownOne, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1842 | if ((KnownOne & Mask) != 0) |
| 1843 | return true; |
| 1844 | // The sign bit of Y is set. If some other bit is set then Y is not equal |
| 1845 | // to INT_MIN. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1846 | computeKnownBits(Y, KnownZero, KnownOne, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1847 | if ((KnownOne & Mask) != 0) |
| 1848 | return true; |
| 1849 | } |
| 1850 | |
| 1851 | // 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] | 1852 | if (XKnownNonNegative && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1853 | isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q, DL)) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1854 | return true; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1855 | if (YKnownNonNegative && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1856 | isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q, DL)) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1857 | return true; |
| 1858 | } |
Duncan Sands | 7cb61e5 | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 1859 | // X * Y. |
| 1860 | else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) { |
| 1861 | OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V); |
| 1862 | // If X and Y are non-zero then so is X * Y as long as the multiplication |
| 1863 | // does not overflow. |
| 1864 | if ((BO->hasNoSignedWrap() || BO->hasNoUnsignedWrap()) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1865 | isKnownNonZero(X, DL, Depth, Q) && isKnownNonZero(Y, DL, Depth, Q)) |
Duncan Sands | 7cb61e5 | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 1866 | return true; |
| 1867 | } |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1868 | // (C ? X : Y) != 0 if X != 0 and Y != 0. |
| 1869 | else if (SelectInst *SI = dyn_cast<SelectInst>(V)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1870 | if (isKnownNonZero(SI->getTrueValue(), DL, Depth, Q) && |
| 1871 | isKnownNonZero(SI->getFalseValue(), DL, Depth, Q)) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1872 | return true; |
| 1873 | } |
| 1874 | |
| 1875 | if (!BitWidth) return false; |
| 1876 | APInt KnownZero(BitWidth, 0); |
| 1877 | APInt KnownOne(BitWidth, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1878 | computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1879 | return KnownOne != 0; |
| 1880 | } |
| 1881 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1882 | /// Return true if 'V & Mask' is known to be zero. We use this predicate to |
| 1883 | /// simplify operations downstream. Mask is known to be zero for bits that V |
| 1884 | /// cannot have. |
Chris Lattner | 4bc2825 | 2009-09-08 00:06:16 +0000 | [diff] [blame] | 1885 | /// |
| 1886 | /// This function is defined on values with integer type, values with pointer |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1887 | /// type, and vectors of integers. In the case |
Chris Lattner | 4bc2825 | 2009-09-08 00:06:16 +0000 | [diff] [blame] | 1888 | /// where V is a vector, the mask, known zero, and known one values are the |
| 1889 | /// same width as the vector element, and the bit is set only if it is true |
| 1890 | /// for all of the elements in the vector. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1891 | bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL, |
| 1892 | unsigned Depth, const Query &Q) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1893 | APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1894 | computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1895 | return (KnownZero & Mask) == Mask; |
| 1896 | } |
| 1897 | |
| 1898 | |
| 1899 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1900 | /// Return the number of times the sign bit of the register is replicated into |
| 1901 | /// the other bits. We know that at least 1 bit is always equal to the sign bit |
| 1902 | /// (itself), but other cases can give us information. For example, immediately |
| 1903 | /// after an "ashr X, 2", we know that the top 3 bits are all equal to each |
| 1904 | /// other, so we return 3. |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1905 | /// |
| 1906 | /// 'Op' must have a scalar integer type. |
| 1907 | /// |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1908 | unsigned ComputeNumSignBits(Value *V, const DataLayout &DL, unsigned Depth, |
| 1909 | const Query &Q) { |
| 1910 | unsigned TyBits = DL.getTypeSizeInBits(V->getType()->getScalarType()); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1911 | unsigned Tmp, Tmp2; |
| 1912 | unsigned FirstAnswer = 1; |
| 1913 | |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1914 | // Note that ConstantInt is handled by the general computeKnownBits case |
Chris Lattner | 2e01a69 | 2008-06-02 18:39:07 +0000 | [diff] [blame] | 1915 | // below. |
| 1916 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1917 | if (Depth == 6) |
| 1918 | return 1; // Limit search depth. |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1919 | |
Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 1920 | Operator *U = dyn_cast<Operator>(V); |
| 1921 | switch (Operator::getOpcode(V)) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1922 | default: break; |
| 1923 | case Instruction::SExt: |
Mon P Wang | bb3eac9 | 2009-12-02 04:59:58 +0000 | [diff] [blame] | 1924 | Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1925 | return ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q) + Tmp; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1926 | |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 1927 | case Instruction::SDiv: { |
Nadav Rotem | 029c5c7 | 2015-03-03 21:39:02 +0000 | [diff] [blame] | 1928 | const APInt *Denominator; |
| 1929 | // sdiv X, C -> adds log(C) sign bits. |
| 1930 | if (match(U->getOperand(1), m_APInt(Denominator))) { |
| 1931 | |
| 1932 | // Ignore non-positive denominator. |
| 1933 | if (!Denominator->isStrictlyPositive()) |
| 1934 | break; |
| 1935 | |
| 1936 | // Calculate the incoming numerator bits. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1937 | unsigned NumBits = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q); |
Nadav Rotem | 029c5c7 | 2015-03-03 21:39:02 +0000 | [diff] [blame] | 1938 | |
| 1939 | // Add floor(log(C)) bits to the numerator bits. |
| 1940 | return std::min(TyBits, NumBits + Denominator->logBase2()); |
| 1941 | } |
| 1942 | break; |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | case Instruction::SRem: { |
| 1946 | const APInt *Denominator; |
Sanjoy Das | e561fee | 2015-03-25 22:33:53 +0000 | [diff] [blame] | 1947 | // srem X, C -> we know that the result is within [-C+1,C) when C is a |
| 1948 | // positive constant. This let us put a lower bound on the number of sign |
| 1949 | // bits. |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 1950 | if (match(U->getOperand(1), m_APInt(Denominator))) { |
| 1951 | |
| 1952 | // Ignore non-positive denominator. |
| 1953 | if (!Denominator->isStrictlyPositive()) |
| 1954 | break; |
| 1955 | |
| 1956 | // Calculate the incoming numerator bits. SRem by a positive constant |
| 1957 | // can't lower the number of sign bits. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1958 | unsigned NumrBits = |
| 1959 | ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q); |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 1960 | |
| 1961 | // Calculate the leading sign bit constraints by examining the |
Sanjoy Das | e561fee | 2015-03-25 22:33:53 +0000 | [diff] [blame] | 1962 | // denominator. Given that the denominator is positive, there are two |
| 1963 | // cases: |
| 1964 | // |
| 1965 | // 1. the numerator is positive. The result range is [0,C) and [0,C) u< |
| 1966 | // (1 << ceilLogBase2(C)). |
| 1967 | // |
| 1968 | // 2. the numerator is negative. Then the result range is (-C,0] and |
| 1969 | // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)). |
| 1970 | // |
| 1971 | // Thus a lower bound on the number of sign bits is `TyBits - |
| 1972 | // ceilLogBase2(C)`. |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 1973 | |
Sanjoy Das | e561fee | 2015-03-25 22:33:53 +0000 | [diff] [blame] | 1974 | unsigned ResBits = TyBits - Denominator->ceilLogBase2(); |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 1975 | return std::max(NumrBits, ResBits); |
| 1976 | } |
| 1977 | break; |
| 1978 | } |
Nadav Rotem | 029c5c7 | 2015-03-03 21:39:02 +0000 | [diff] [blame] | 1979 | |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 1980 | case Instruction::AShr: { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1981 | Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q); |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 1982 | // ashr X, C -> adds C sign bits. Vectors too. |
| 1983 | const APInt *ShAmt; |
| 1984 | if (match(U->getOperand(1), m_APInt(ShAmt))) { |
| 1985 | Tmp += ShAmt->getZExtValue(); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1986 | if (Tmp > TyBits) Tmp = TyBits; |
| 1987 | } |
| 1988 | return Tmp; |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 1989 | } |
| 1990 | case Instruction::Shl: { |
| 1991 | const APInt *ShAmt; |
| 1992 | if (match(U->getOperand(1), m_APInt(ShAmt))) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1993 | // shl destroys sign bits. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1994 | Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q); |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 1995 | Tmp2 = ShAmt->getZExtValue(); |
| 1996 | if (Tmp2 >= TyBits || // Bad shift. |
| 1997 | Tmp2 >= Tmp) break; // Shifted all sign bits out. |
| 1998 | return Tmp - Tmp2; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1999 | } |
| 2000 | break; |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 2001 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2002 | case Instruction::And: |
| 2003 | case Instruction::Or: |
| 2004 | case Instruction::Xor: // NOT is handled here. |
| 2005 | // Logical binary ops preserve the number of sign bits at the worst. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2006 | Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2007 | if (Tmp != 1) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2008 | Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2009 | FirstAnswer = std::min(Tmp, Tmp2); |
| 2010 | // We computed what we know about the sign bits as our first |
| 2011 | // answer. Now proceed to the generic code that uses |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 2012 | // computeKnownBits, and pick whichever answer is better. |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2013 | } |
| 2014 | break; |
| 2015 | |
| 2016 | case Instruction::Select: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2017 | Tmp = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2018 | if (Tmp == 1) return 1; // Early out. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2019 | Tmp2 = ComputeNumSignBits(U->getOperand(2), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2020 | return std::min(Tmp, Tmp2); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2021 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2022 | case Instruction::Add: |
| 2023 | // Add can have at most one carry bit. Thus we know that the output |
| 2024 | // is, at worst, one more bit than the inputs. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2025 | Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2026 | if (Tmp == 1) return 1; // Early out. |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2027 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2028 | // Special case decrementing a value (ADD X, -1): |
David Majnemer | a55027f | 2014-12-26 09:20:17 +0000 | [diff] [blame] | 2029 | if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1))) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2030 | if (CRHS->isAllOnesValue()) { |
| 2031 | APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2032 | computeKnownBits(U->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, |
| 2033 | Q); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2034 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2035 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 2036 | // sign bits set. |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2037 | if ((KnownZero | APInt(TyBits, 1)).isAllOnesValue()) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2038 | return TyBits; |
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 | // If we are subtracting one from a positive number, there is no carry |
| 2041 | // out of the result. |
| 2042 | if (KnownZero.isNegative()) |
| 2043 | return Tmp; |
| 2044 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2045 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2046 | Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2047 | if (Tmp2 == 1) return 1; |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2048 | return std::min(Tmp, Tmp2)-1; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2049 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2050 | case Instruction::Sub: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2051 | Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2052 | if (Tmp2 == 1) return 1; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2053 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2054 | // Handle NEG. |
David Majnemer | a55027f | 2014-12-26 09:20:17 +0000 | [diff] [blame] | 2055 | if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0))) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2056 | if (CLHS->isNullValue()) { |
| 2057 | APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2058 | computeKnownBits(U->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, |
| 2059 | Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2060 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 2061 | // sign bits set. |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2062 | if ((KnownZero | APInt(TyBits, 1)).isAllOnesValue()) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2063 | return TyBits; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2064 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2065 | // If the input is known to be positive (the sign bit is known clear), |
| 2066 | // the output of the NEG has the same number of sign bits as the input. |
| 2067 | if (KnownZero.isNegative()) |
| 2068 | return Tmp2; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2069 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2070 | // Otherwise, we treat this like a SUB. |
| 2071 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2072 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2073 | // Sub can have at most one carry bit. Thus we know that the output |
| 2074 | // is, at worst, one more bit than the inputs. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2075 | Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2076 | if (Tmp == 1) return 1; // Early out. |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2077 | return std::min(Tmp, Tmp2)-1; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2078 | |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2079 | case Instruction::PHI: { |
| 2080 | PHINode *PN = cast<PHINode>(U); |
David Majnemer | 6ee8d17 | 2015-01-04 07:06:53 +0000 | [diff] [blame] | 2081 | unsigned NumIncomingValues = PN->getNumIncomingValues(); |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2082 | // Don't analyze large in-degree PHIs. |
David Majnemer | 6ee8d17 | 2015-01-04 07:06:53 +0000 | [diff] [blame] | 2083 | if (NumIncomingValues > 4) break; |
| 2084 | // Unreachable blocks may have zero-operand PHI nodes. |
| 2085 | if (NumIncomingValues == 0) break; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2086 | |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2087 | // Take the minimum of all incoming values. This can't infinitely loop |
| 2088 | // because of our depth threshold. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2089 | Tmp = ComputeNumSignBits(PN->getIncomingValue(0), DL, Depth + 1, Q); |
David Majnemer | 6ee8d17 | 2015-01-04 07:06:53 +0000 | [diff] [blame] | 2090 | for (unsigned i = 1, e = NumIncomingValues; i != e; ++i) { |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2091 | if (Tmp == 1) return Tmp; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2092 | Tmp = std::min( |
| 2093 | Tmp, ComputeNumSignBits(PN->getIncomingValue(i), DL, Depth + 1, Q)); |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2094 | } |
| 2095 | return Tmp; |
| 2096 | } |
| 2097 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2098 | case Instruction::Trunc: |
| 2099 | // FIXME: it's tricky to do anything useful for this, but it is an important |
| 2100 | // case for targets like X86. |
| 2101 | break; |
| 2102 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2103 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2104 | // Finally, if we can prove that the top bits of the result are 0's or 1's, |
| 2105 | // use this information. |
| 2106 | APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0); |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2107 | APInt Mask; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2108 | computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2109 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2110 | if (KnownZero.isNegative()) { // sign bit is 0 |
| 2111 | Mask = KnownZero; |
| 2112 | } else if (KnownOne.isNegative()) { // sign bit is 1; |
| 2113 | Mask = KnownOne; |
| 2114 | } else { |
| 2115 | // Nothing known. |
| 2116 | return FirstAnswer; |
| 2117 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2118 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2119 | // Okay, we know that the sign bit in Mask is set. Use CLZ to determine |
| 2120 | // the number of identical bits in the top of the input value. |
| 2121 | Mask = ~Mask; |
| 2122 | Mask <<= Mask.getBitWidth()-TyBits; |
| 2123 | // Return # leading zeros. We use 'min' here in case Val was zero before |
| 2124 | // shifting. We don't want to return '64' as for an i32 "0". |
| 2125 | return std::max(FirstAnswer, std::min(TyBits, Mask.countLeadingZeros())); |
| 2126 | } |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2127 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2128 | /// This function computes the integer multiple of Base that equals V. |
| 2129 | /// If successful, it returns true and returns the multiple in |
| 2130 | /// Multiple. If unsuccessful, it returns false. It looks |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2131 | /// through SExt instructions only if LookThroughSExt is true. |
| 2132 | bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple, |
Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2133 | bool LookThroughSExt, unsigned Depth) { |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2134 | const unsigned MaxDepth = 6; |
| 2135 | |
Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2136 | assert(V && "No Value?"); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2137 | assert(Depth <= MaxDepth && "Limit Search Depth"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2138 | assert(V->getType()->isIntegerTy() && "Not integer or pointer type!"); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2139 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2140 | Type *T = V->getType(); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2141 | |
Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2142 | ConstantInt *CI = dyn_cast<ConstantInt>(V); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2143 | |
| 2144 | if (Base == 0) |
| 2145 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2146 | |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2147 | if (Base == 1) { |
| 2148 | Multiple = V; |
| 2149 | return true; |
| 2150 | } |
| 2151 | |
| 2152 | ConstantExpr *CO = dyn_cast<ConstantExpr>(V); |
| 2153 | Constant *BaseVal = ConstantInt::get(T, Base); |
| 2154 | if (CO && CO == BaseVal) { |
| 2155 | // Multiple is 1. |
| 2156 | Multiple = ConstantInt::get(T, 1); |
| 2157 | return true; |
| 2158 | } |
| 2159 | |
| 2160 | if (CI && CI->getZExtValue() % Base == 0) { |
| 2161 | Multiple = ConstantInt::get(T, CI->getZExtValue() / Base); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2162 | return true; |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2163 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2164 | |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2165 | if (Depth == MaxDepth) return false; // Limit search depth. |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2166 | |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2167 | Operator *I = dyn_cast<Operator>(V); |
| 2168 | if (!I) return false; |
| 2169 | |
| 2170 | switch (I->getOpcode()) { |
| 2171 | default: break; |
Chris Lattner | 4f0b47d | 2009-11-26 01:50:12 +0000 | [diff] [blame] | 2172 | case Instruction::SExt: |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2173 | if (!LookThroughSExt) return false; |
| 2174 | // otherwise fall through to ZExt |
Chris Lattner | 4f0b47d | 2009-11-26 01:50:12 +0000 | [diff] [blame] | 2175 | case Instruction::ZExt: |
Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2176 | return ComputeMultiple(I->getOperand(0), Base, Multiple, |
| 2177 | LookThroughSExt, Depth+1); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2178 | case Instruction::Shl: |
| 2179 | case Instruction::Mul: { |
| 2180 | Value *Op0 = I->getOperand(0); |
| 2181 | Value *Op1 = I->getOperand(1); |
| 2182 | |
| 2183 | if (I->getOpcode() == Instruction::Shl) { |
| 2184 | ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1); |
| 2185 | if (!Op1CI) return false; |
| 2186 | // Turn Op0 << Op1 into Op0 * 2^Op1 |
| 2187 | APInt Op1Int = Op1CI->getValue(); |
| 2188 | uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1); |
Jay Foad | 15084f0 | 2010-11-30 09:02:01 +0000 | [diff] [blame] | 2189 | APInt API(Op1Int.getBitWidth(), 0); |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 2190 | API.setBit(BitToSet); |
Jay Foad | 15084f0 | 2010-11-30 09:02:01 +0000 | [diff] [blame] | 2191 | Op1 = ConstantInt::get(V->getContext(), API); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2194 | Value *Mul0 = nullptr; |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2195 | if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) { |
| 2196 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) |
| 2197 | if (Constant *MulC = dyn_cast<Constant>(Mul0)) { |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2198 | if (Op1C->getType()->getPrimitiveSizeInBits() < |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2199 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2200 | Op1C = ConstantExpr::getZExt(Op1C, MulC->getType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2201 | if (Op1C->getType()->getPrimitiveSizeInBits() > |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2202 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2203 | MulC = ConstantExpr::getZExt(MulC, Op1C->getType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2204 | |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2205 | // V == Base * (Mul0 * Op1), so return (Mul0 * Op1) |
| 2206 | Multiple = ConstantExpr::getMul(MulC, Op1C); |
| 2207 | return true; |
| 2208 | } |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2209 | |
| 2210 | if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0)) |
| 2211 | if (Mul0CI->getValue() == 1) { |
| 2212 | // V == Base * Op1, so return Op1 |
| 2213 | Multiple = Op1; |
| 2214 | return true; |
| 2215 | } |
| 2216 | } |
| 2217 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2218 | Value *Mul1 = nullptr; |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2219 | if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) { |
| 2220 | if (Constant *Op0C = dyn_cast<Constant>(Op0)) |
| 2221 | if (Constant *MulC = dyn_cast<Constant>(Mul1)) { |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2222 | if (Op0C->getType()->getPrimitiveSizeInBits() < |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2223 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2224 | Op0C = ConstantExpr::getZExt(Op0C, MulC->getType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2225 | if (Op0C->getType()->getPrimitiveSizeInBits() > |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2226 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2227 | MulC = ConstantExpr::getZExt(MulC, Op0C->getType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2228 | |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2229 | // V == Base * (Mul1 * Op0), so return (Mul1 * Op0) |
| 2230 | Multiple = ConstantExpr::getMul(MulC, Op0C); |
| 2231 | return true; |
| 2232 | } |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2233 | |
| 2234 | if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1)) |
| 2235 | if (Mul1CI->getValue() == 1) { |
| 2236 | // V == Base * Op0, so return Op0 |
| 2237 | Multiple = Op0; |
| 2238 | return true; |
| 2239 | } |
| 2240 | } |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | // We could not determine if V is a multiple of Base. |
| 2245 | return false; |
| 2246 | } |
| 2247 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2248 | /// Return true if we can prove that the specified FP value is never equal to |
| 2249 | /// -0.0. |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2250 | /// |
| 2251 | /// NOTE: this function will need to be revisited when we support non-default |
| 2252 | /// rounding modes! |
| 2253 | /// |
| 2254 | bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) { |
| 2255 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) |
| 2256 | return !CFP->getValueAPF().isNegZero(); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2257 | |
Sanjay Patel | 40eaa8d | 2015-02-25 18:00:15 +0000 | [diff] [blame] | 2258 | // FIXME: Magic number! At the least, this should be given a name because it's |
| 2259 | // used similarly in CannotBeOrderedLessThanZero(). A better fix may be to |
| 2260 | // 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] | 2261 | if (Depth == 6) |
Sanjay Patel | 40eaa8d | 2015-02-25 18:00:15 +0000 | [diff] [blame] | 2262 | return false; // Limit search depth. |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2263 | |
Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 2264 | const Operator *I = dyn_cast<Operator>(V); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2265 | if (!I) return false; |
Michael Ilseman | 0f12837 | 2012-12-06 00:07:09 +0000 | [diff] [blame] | 2266 | |
| 2267 | // Check if the nsz fast-math flag is set |
| 2268 | if (const FPMathOperator *FPO = dyn_cast<FPMathOperator>(I)) |
| 2269 | if (FPO->hasNoSignedZeros()) |
| 2270 | return true; |
| 2271 | |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2272 | // (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] | 2273 | if (I->getOpcode() == Instruction::FAdd) |
| 2274 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I->getOperand(1))) |
| 2275 | if (CFP->isNullValue()) |
| 2276 | return true; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2277 | |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2278 | // sitofp and uitofp turn into +0.0 for zero. |
| 2279 | if (isa<SIToFPInst>(I) || isa<UIToFPInst>(I)) |
| 2280 | return true; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2281 | |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2282 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 2283 | // sqrt(-0.0) = -0.0, no other negative results are possible. |
| 2284 | if (II->getIntrinsicID() == Intrinsic::sqrt) |
Gabor Greif | 1abbde3 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 2285 | return CannotBeNegativeZero(II->getArgOperand(0), Depth+1); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2286 | |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2287 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 2288 | if (const Function *F = CI->getCalledFunction()) { |
| 2289 | if (F->isDeclaration()) { |
Daniel Dunbar | ca414c7 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 2290 | // abs(x) != -0.0 |
| 2291 | if (F->getName() == "abs") return true; |
Dale Johannesen | f6a987b | 2009-09-25 20:54:50 +0000 | [diff] [blame] | 2292 | // fabs[lf](x) != -0.0 |
| 2293 | if (F->getName() == "fabs") return true; |
| 2294 | if (F->getName() == "fabsf") return true; |
| 2295 | if (F->getName() == "fabsl") return true; |
| 2296 | if (F->getName() == "sqrt" || F->getName() == "sqrtf" || |
| 2297 | F->getName() == "sqrtl") |
Gabor Greif | 1abbde3 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 2298 | return CannotBeNegativeZero(CI->getArgOperand(0), Depth+1); |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2299 | } |
| 2300 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2301 | |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2302 | return false; |
| 2303 | } |
| 2304 | |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2305 | bool llvm::CannotBeOrderedLessThanZero(const Value *V, unsigned Depth) { |
| 2306 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) |
| 2307 | return !CFP->getValueAPF().isNegative() || CFP->getValueAPF().isZero(); |
| 2308 | |
Sanjay Patel | 40eaa8d | 2015-02-25 18:00:15 +0000 | [diff] [blame] | 2309 | // FIXME: Magic number! At the least, this should be given a name because it's |
| 2310 | // used similarly in CannotBeNegativeZero(). A better fix may be to |
| 2311 | // 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] | 2312 | if (Depth == 6) |
| 2313 | return false; // Limit search depth. |
| 2314 | |
| 2315 | const Operator *I = dyn_cast<Operator>(V); |
| 2316 | if (!I) return false; |
| 2317 | |
| 2318 | switch (I->getOpcode()) { |
| 2319 | default: break; |
| 2320 | case Instruction::FMul: |
| 2321 | // x*x is always non-negative or a NaN. |
| 2322 | if (I->getOperand(0) == I->getOperand(1)) |
| 2323 | return true; |
| 2324 | // Fall through |
| 2325 | case Instruction::FAdd: |
| 2326 | case Instruction::FDiv: |
| 2327 | case Instruction::FRem: |
| 2328 | return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1) && |
| 2329 | CannotBeOrderedLessThanZero(I->getOperand(1), Depth+1); |
| 2330 | case Instruction::FPExt: |
| 2331 | case Instruction::FPTrunc: |
| 2332 | // Widening/narrowing never change sign. |
| 2333 | return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1); |
| 2334 | case Instruction::Call: |
| 2335 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 2336 | switch (II->getIntrinsicID()) { |
| 2337 | default: break; |
| 2338 | case Intrinsic::exp: |
| 2339 | case Intrinsic::exp2: |
| 2340 | case Intrinsic::fabs: |
| 2341 | case Intrinsic::sqrt: |
| 2342 | return true; |
| 2343 | case Intrinsic::powi: |
| 2344 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 2345 | // powi(x,n) is non-negative if n is even. |
| 2346 | if (CI->getBitWidth() <= 64 && CI->getSExtValue() % 2u == 0) |
| 2347 | return true; |
| 2348 | } |
| 2349 | return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1); |
| 2350 | case Intrinsic::fma: |
| 2351 | case Intrinsic::fmuladd: |
| 2352 | // x*x+y is non-negative if y is non-negative. |
| 2353 | return I->getOperand(0) == I->getOperand(1) && |
| 2354 | CannotBeOrderedLessThanZero(I->getOperand(2), Depth+1); |
| 2355 | } |
| 2356 | break; |
| 2357 | } |
| 2358 | return false; |
| 2359 | } |
| 2360 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2361 | /// If the specified value can be set by repeating the same byte in memory, |
| 2362 | /// return the i8 value that it is represented with. This is |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2363 | /// true for all i8 values obviously, but is also true for i32 0, i32 -1, |
| 2364 | /// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated |
| 2365 | /// byte store (e.g. i16 0x1234), return null. |
| 2366 | Value *llvm::isBytewiseValue(Value *V) { |
| 2367 | // All byte-wide stores are splatable, even of arbitrary variables. |
| 2368 | if (V->getType()->isIntegerTy(8)) return V; |
Chris Lattner | acf6b07 | 2011-02-19 19:35:49 +0000 | [diff] [blame] | 2369 | |
| 2370 | // Handle 'null' ConstantArrayZero etc. |
| 2371 | if (Constant *C = dyn_cast<Constant>(V)) |
| 2372 | if (C->isNullValue()) |
| 2373 | return Constant::getNullValue(Type::getInt8Ty(V->getContext())); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2374 | |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2375 | // 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] | 2376 | // corresponding integer value is "byteable". An important case is 0.0. |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2377 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 2378 | if (CFP->getType()->isFloatTy()) |
| 2379 | V = ConstantExpr::getBitCast(CFP, Type::getInt32Ty(V->getContext())); |
| 2380 | if (CFP->getType()->isDoubleTy()) |
| 2381 | V = ConstantExpr::getBitCast(CFP, Type::getInt64Ty(V->getContext())); |
| 2382 | // Don't handle long double formats, which have strange constraints. |
| 2383 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2384 | |
Benjamin Kramer | 17d9015 | 2015-02-07 19:29:02 +0000 | [diff] [blame] | 2385 | // We can handle constant integers that are multiple of 8 bits. |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2386 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Benjamin Kramer | 17d9015 | 2015-02-07 19:29:02 +0000 | [diff] [blame] | 2387 | if (CI->getBitWidth() % 8 == 0) { |
| 2388 | assert(CI->getBitWidth() > 8 && "8 bits should be handled above!"); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2389 | |
Benjamin Kramer | b4b5150 | 2015-03-25 16:49:59 +0000 | [diff] [blame] | 2390 | if (!CI->getValue().isSplat(8)) |
Benjamin Kramer | 17d9015 | 2015-02-07 19:29:02 +0000 | [diff] [blame] | 2391 | return nullptr; |
| 2392 | return ConstantInt::get(V->getContext(), CI->getValue().trunc(8)); |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2393 | } |
| 2394 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2395 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2396 | // A ConstantDataArray/Vector is splatable if all its members are equal and |
| 2397 | // also splatable. |
| 2398 | if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(V)) { |
| 2399 | Value *Elt = CA->getElementAsConstant(0); |
| 2400 | Value *Val = isBytewiseValue(Elt); |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2401 | if (!Val) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2402 | return nullptr; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2403 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2404 | for (unsigned I = 1, E = CA->getNumElements(); I != E; ++I) |
| 2405 | if (CA->getElementAsConstant(I) != Elt) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2406 | return nullptr; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2407 | |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2408 | return Val; |
| 2409 | } |
Chad Rosier | 8abf65a | 2011-12-06 00:19:08 +0000 | [diff] [blame] | 2410 | |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2411 | // Conceptually, we could handle things like: |
| 2412 | // %a = zext i8 %X to i16 |
| 2413 | // %b = shl i16 %a, 8 |
| 2414 | // %c = or i16 %a, %b |
| 2415 | // but until there is an example that actually needs this, it doesn't seem |
| 2416 | // worth worrying about. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2417 | return nullptr; |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2421 | // This is the recursive version of BuildSubAggregate. It takes a few different |
| 2422 | // arguments. Idxs is the index within the nested struct From that we are |
| 2423 | // looking at now (which is of type IndexedType). IdxSkip is the number of |
| 2424 | // indices from Idxs that should be left out when inserting into the resulting |
| 2425 | // struct. To is the result struct built so far, new insertvalue instructions |
| 2426 | // build on that. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2427 | static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType, |
Craig Topper | 2cd5ff8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 2428 | SmallVectorImpl<unsigned> &Idxs, |
Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 2429 | unsigned IdxSkip, |
Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 2430 | Instruction *InsertBefore) { |
Dmitri Gribenko | 226fea5 | 2013-01-13 16:01:15 +0000 | [diff] [blame] | 2431 | llvm::StructType *STy = dyn_cast<llvm::StructType>(IndexedType); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2432 | if (STy) { |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2433 | // Save the original To argument so we can modify it |
| 2434 | Value *OrigTo = To; |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2435 | // General case, the type indexed by Idxs is a struct |
| 2436 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 2437 | // Process each struct element recursively |
| 2438 | Idxs.push_back(i); |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2439 | Value *PrevTo = To; |
Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 2440 | To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip, |
Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 2441 | InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2442 | Idxs.pop_back(); |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2443 | if (!To) { |
| 2444 | // Couldn't find any inserted value for this index? Cleanup |
| 2445 | while (PrevTo != OrigTo) { |
| 2446 | InsertValueInst* Del = cast<InsertValueInst>(PrevTo); |
| 2447 | PrevTo = Del->getAggregateOperand(); |
| 2448 | Del->eraseFromParent(); |
| 2449 | } |
| 2450 | // Stop processing elements |
| 2451 | break; |
| 2452 | } |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2453 | } |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 2454 | // If we successfully found a value for each of our subaggregates |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2455 | if (To) |
| 2456 | return To; |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2457 | } |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2458 | // Base case, the type indexed by SourceIdxs is not a struct, or not all of |
| 2459 | // the struct's elements had a value that was inserted directly. In the latter |
| 2460 | // case, perhaps we can't determine each of the subelements individually, but |
| 2461 | // we might be able to find the complete struct somewhere. |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2462 | |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2463 | // Find the value that is at that particular spot |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2464 | Value *V = FindInsertedValue(From, Idxs); |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2465 | |
| 2466 | if (!V) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2467 | return nullptr; |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2468 | |
| 2469 | // Insert the value in the new (sub) aggregrate |
Frits van Bommel | 717d7ed | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 2470 | return llvm::InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip), |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2471 | "tmp", InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | // This helper takes a nested struct and extracts a part of it (which is again a |
| 2475 | // struct) into a new value. For example, given the struct: |
| 2476 | // { a, { b, { c, d }, e } } |
| 2477 | // and the indices "1, 1" this returns |
| 2478 | // { c, d }. |
| 2479 | // |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2480 | // It does this by inserting an insertvalue for each element in the resulting |
| 2481 | // struct, as opposed to just inserting a single struct. This will only work if |
| 2482 | // each of the elements of the substruct are known (ie, inserted into From by an |
| 2483 | // insertvalue instruction somewhere). |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2484 | // |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2485 | // All inserted insertvalue instructions are inserted before InsertBefore |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2486 | static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range, |
Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 2487 | Instruction *InsertBefore) { |
Matthijs Kooijman | 69801d4 | 2008-06-16 13:28:31 +0000 | [diff] [blame] | 2488 | assert(InsertBefore && "Must have someplace to insert!"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2489 | Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(), |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2490 | idx_range); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2491 | Value *To = UndefValue::get(IndexedType); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2492 | SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end()); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2493 | unsigned IdxSkip = Idxs.size(); |
| 2494 | |
Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 2495 | return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2498 | /// Given an aggregrate and an sequence of indices, see if |
Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 2499 | /// the scalar value indexed is already around as a register, for example if it |
| 2500 | /// were inserted directly into the aggregrate. |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 2501 | /// |
| 2502 | /// If InsertBefore is not null, this function will duplicate (modified) |
| 2503 | /// insertvalues when a part of a nested struct is extracted. |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2504 | Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range, |
| 2505 | Instruction *InsertBefore) { |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2506 | // 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] | 2507 | // recursion). |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2508 | if (idx_range.empty()) |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2509 | return V; |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 2510 | // We have indices, so V should have an indexable type. |
| 2511 | assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) && |
| 2512 | "Not looking at a struct or array?"); |
| 2513 | assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) && |
| 2514 | "Invalid indices for type?"); |
Owen Anderson | f1f1743 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 2515 | |
Chris Lattner | 6705883 | 2012-01-25 06:48:06 +0000 | [diff] [blame] | 2516 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 2517 | C = C->getAggregateElement(idx_range[0]); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2518 | if (!C) return nullptr; |
Chris Lattner | 6705883 | 2012-01-25 06:48:06 +0000 | [diff] [blame] | 2519 | return FindInsertedValue(C, idx_range.slice(1), InsertBefore); |
| 2520 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2521 | |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 2522 | if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) { |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2523 | // Loop the indices for the insertvalue instruction in parallel with the |
| 2524 | // requested indices |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2525 | const unsigned *req_idx = idx_range.begin(); |
Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 2526 | for (const unsigned *i = I->idx_begin(), *e = I->idx_end(); |
| 2527 | i != e; ++i, ++req_idx) { |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2528 | if (req_idx == idx_range.end()) { |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 2529 | // We can't handle this without inserting insertvalues |
| 2530 | if (!InsertBefore) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2531 | return nullptr; |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 2532 | |
| 2533 | // The requested index identifies a part of a nested aggregate. Handle |
| 2534 | // this specially. For example, |
| 2535 | // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0 |
| 2536 | // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1 |
| 2537 | // %C = extractvalue {i32, { i32, i32 } } %B, 1 |
| 2538 | // This can be changed into |
| 2539 | // %A = insertvalue {i32, i32 } undef, i32 10, 0 |
| 2540 | // %C = insertvalue {i32, i32 } %A, i32 11, 1 |
| 2541 | // which allows the unused 0,0 element from the nested struct to be |
| 2542 | // removed. |
| 2543 | return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx), |
| 2544 | InsertBefore); |
Duncan Sands | db356ee | 2008-06-19 08:47:31 +0000 | [diff] [blame] | 2545 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2546 | |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2547 | // This insert value inserts something else than what we are looking for. |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 2548 | // See if the (aggregate) value inserted into has the value we are |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2549 | // looking for, then. |
| 2550 | if (*req_idx != *i) |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2551 | return FindInsertedValue(I->getAggregateOperand(), idx_range, |
Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 2552 | InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2553 | } |
| 2554 | // If we end up here, the indices of the insertvalue match with those |
| 2555 | // requested (though possibly only partially). Now we recursively look at |
| 2556 | // the inserted value, passing any remaining indices. |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2557 | return FindInsertedValue(I->getInsertedValueOperand(), |
Frits van Bommel | 717d7ed | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 2558 | makeArrayRef(req_idx, idx_range.end()), |
Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 2559 | InsertBefore); |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 2560 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2561 | |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 2562 | if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) { |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 2563 | // If we're extracting a value from an aggregate that was extracted from |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2564 | // something else, we can extract from that something else directly instead. |
| 2565 | // 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] | 2566 | |
| 2567 | // Calculate the number of indices required |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2568 | unsigned size = I->getNumIndices() + idx_range.size(); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2569 | // Allocate some space to put the new indices in |
Matthijs Kooijman | 8369c67 | 2008-06-17 08:24:37 +0000 | [diff] [blame] | 2570 | SmallVector<unsigned, 5> Idxs; |
| 2571 | Idxs.reserve(size); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2572 | // Add indices from the extract value instruction |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2573 | Idxs.append(I->idx_begin(), I->idx_end()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2574 | |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2575 | // Add requested indices |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2576 | Idxs.append(idx_range.begin(), idx_range.end()); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2577 | |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2578 | assert(Idxs.size() == size |
Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 2579 | && "Number of indices added not correct?"); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2580 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2581 | return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2582 | } |
| 2583 | // Otherwise, we don't know (such as, extracting from a function return value |
| 2584 | // or load instruction) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2585 | return nullptr; |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 2586 | } |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2587 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2588 | /// Analyze the specified pointer to see if it can be expressed as a base |
| 2589 | /// 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] | 2590 | Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2591 | const DataLayout &DL) { |
| 2592 | unsigned BitWidth = DL.getPointerTypeSizeInBits(Ptr->getType()); |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 2593 | APInt ByteOffset(BitWidth, 0); |
| 2594 | while (1) { |
| 2595 | if (Ptr->getType()->isVectorTy()) |
| 2596 | break; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2597 | |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 2598 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2599 | APInt GEPOffset(BitWidth, 0); |
| 2600 | if (!GEP->accumulateConstantOffset(DL, GEPOffset)) |
| 2601 | break; |
Matt Arsenault | f55e5e7 | 2013-08-10 17:34:08 +0000 | [diff] [blame] | 2602 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2603 | ByteOffset += GEPOffset; |
Matt Arsenault | f55e5e7 | 2013-08-10 17:34:08 +0000 | [diff] [blame] | 2604 | |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 2605 | Ptr = GEP->getPointerOperand(); |
Matt Arsenault | fd78d0c | 2014-07-14 22:39:22 +0000 | [diff] [blame] | 2606 | } else if (Operator::getOpcode(Ptr) == Instruction::BitCast || |
| 2607 | Operator::getOpcode(Ptr) == Instruction::AddrSpaceCast) { |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 2608 | Ptr = cast<Operator>(Ptr)->getOperand(0); |
| 2609 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) { |
| 2610 | if (GA->mayBeOverridden()) |
| 2611 | break; |
| 2612 | Ptr = GA->getAliasee(); |
Chris Lattner | e28618d | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 2613 | } else { |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 2614 | break; |
Chris Lattner | e28618d | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 2615 | } |
| 2616 | } |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 2617 | Offset = ByteOffset.getSExtValue(); |
| 2618 | return Ptr; |
Chris Lattner | e28618d | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2622 | /// This function computes the length of a null-terminated C string pointed to |
| 2623 | /// by V. If successful, it returns true and returns the string in Str. |
| 2624 | /// If unsuccessful, it returns false. |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2625 | bool llvm::getConstantStringInfo(const Value *V, StringRef &Str, |
| 2626 | uint64_t Offset, bool TrimAtNul) { |
| 2627 | assert(V); |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2628 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2629 | // Look through bitcast instructions and geps. |
| 2630 | V = V->stripPointerCasts(); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2631 | |
Benjamin Kramer | 0248a3e | 2015-03-21 15:36:06 +0000 | [diff] [blame] | 2632 | // 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] | 2633 | // offset. |
| 2634 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2635 | // Make sure the GEP has exactly three arguments. |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2636 | if (GEP->getNumOperands() != 3) |
| 2637 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2638 | |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2639 | // Make sure the index-ee is a pointer to array of i8. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2640 | PointerType *PT = cast<PointerType>(GEP->getOperand(0)->getType()); |
| 2641 | ArrayType *AT = dyn_cast<ArrayType>(PT->getElementType()); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2642 | if (!AT || !AT->getElementType()->isIntegerTy(8)) |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2643 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2644 | |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2645 | // Check to make sure that the first operand of the GEP is an integer and |
| 2646 | // 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] | 2647 | const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1)); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2648 | if (!FirstIdx || !FirstIdx->isZero()) |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2649 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2650 | |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2651 | // If the second index isn't a ConstantInt, then this is a variable index |
| 2652 | // into the array. If this occurs, we can't say anything meaningful about |
| 2653 | // the string. |
| 2654 | uint64_t StartIdx = 0; |
Dan Gohman | 0b4df04 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 2655 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2656 | StartIdx = CI->getZExtValue(); |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2657 | else |
| 2658 | return false; |
Benjamin Kramer | 0248a3e | 2015-03-21 15:36:06 +0000 | [diff] [blame] | 2659 | return getConstantStringInfo(GEP->getOperand(0), Str, StartIdx + Offset, |
| 2660 | TrimAtNul); |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2661 | } |
Nick Lewycky | 4620988 | 2011-10-20 00:34:35 +0000 | [diff] [blame] | 2662 | |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2663 | // The GEP instruction, constant or instruction, must reference a global |
| 2664 | // variable that is a constant and is initialized. The referenced constant |
| 2665 | // initializer is the array that we'll use for optimization. |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2666 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(V); |
Dan Gohman | 5d5bc6d | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 2667 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer()) |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2668 | return false; |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2669 | |
Nick Lewycky | 4620988 | 2011-10-20 00:34:35 +0000 | [diff] [blame] | 2670 | // Handle the all-zeros case |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2671 | if (GV->getInitializer()->isNullValue()) { |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2672 | // This is a degenerate case. The initializer is constant zero so the |
| 2673 | // length of the string must be zero. |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2674 | Str = ""; |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2675 | return true; |
| 2676 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2677 | |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2678 | // Must be a Constant Array |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2679 | const ConstantDataArray *Array = |
| 2680 | dyn_cast<ConstantDataArray>(GV->getInitializer()); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2681 | if (!Array || !Array->isString()) |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2682 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2683 | |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2684 | // Get the number of elements in the array |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2685 | uint64_t NumElts = Array->getType()->getArrayNumElements(); |
| 2686 | |
| 2687 | // Start out with the entire array in the StringRef. |
| 2688 | Str = Array->getAsString(); |
| 2689 | |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2690 | if (Offset > NumElts) |
| 2691 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2692 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2693 | // Skip over 'offset' bytes. |
| 2694 | Str = Str.substr(Offset); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2695 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2696 | if (TrimAtNul) { |
| 2697 | // Trim off the \0 and anything after it. If the array is not nul |
| 2698 | // terminated, we just return the whole end of string. The client may know |
| 2699 | // some other way that the string is length-bound. |
| 2700 | Str = Str.substr(0, Str.find('\0')); |
| 2701 | } |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 2702 | return true; |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 2703 | } |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2704 | |
| 2705 | // These next two are very similar to the above, but also look through PHI |
| 2706 | // nodes. |
| 2707 | // TODO: See if we can integrate these two together. |
| 2708 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2709 | /// If we can compute the length of the string pointed to by |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2710 | /// the specified pointer, return 'len+1'. If we can't, return 0. |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 2711 | static uint64_t GetStringLengthH(Value *V, SmallPtrSetImpl<PHINode*> &PHIs) { |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2712 | // Look through noop bitcast instructions. |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2713 | V = V->stripPointerCasts(); |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2714 | |
| 2715 | // If this is a PHI node, there are two cases: either we have already seen it |
| 2716 | // or we haven't. |
| 2717 | if (PHINode *PN = dyn_cast<PHINode>(V)) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 2718 | if (!PHIs.insert(PN).second) |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2719 | return ~0ULL; // already in the set. |
| 2720 | |
| 2721 | // If it was new, see if all the input strings are the same length. |
| 2722 | uint64_t LenSoFar = ~0ULL; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 2723 | for (Value *IncValue : PN->incoming_values()) { |
| 2724 | uint64_t Len = GetStringLengthH(IncValue, PHIs); |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2725 | if (Len == 0) return 0; // Unknown length -> unknown. |
| 2726 | |
| 2727 | if (Len == ~0ULL) continue; |
| 2728 | |
| 2729 | if (Len != LenSoFar && LenSoFar != ~0ULL) |
| 2730 | return 0; // Disagree -> unknown. |
| 2731 | LenSoFar = Len; |
| 2732 | } |
| 2733 | |
| 2734 | // Success, all agree. |
| 2735 | return LenSoFar; |
| 2736 | } |
| 2737 | |
| 2738 | // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y) |
| 2739 | if (SelectInst *SI = dyn_cast<SelectInst>(V)) { |
| 2740 | uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs); |
| 2741 | if (Len1 == 0) return 0; |
| 2742 | uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs); |
| 2743 | if (Len2 == 0) return 0; |
| 2744 | if (Len1 == ~0ULL) return Len2; |
| 2745 | if (Len2 == ~0ULL) return Len1; |
| 2746 | if (Len1 != Len2) return 0; |
| 2747 | return Len1; |
| 2748 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2749 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2750 | // Otherwise, see if we can read the string. |
| 2751 | StringRef StrData; |
| 2752 | if (!getConstantStringInfo(V, StrData)) |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2753 | return 0; |
| 2754 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2755 | return StrData.size()+1; |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2758 | /// If we can compute the length of the string pointed to by |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 2759 | /// the specified pointer, return 'len+1'. If we can't, return 0. |
| 2760 | uint64_t llvm::GetStringLength(Value *V) { |
| 2761 | if (!V->getType()->isPointerTy()) return 0; |
| 2762 | |
| 2763 | SmallPtrSet<PHINode*, 32> PHIs; |
| 2764 | uint64_t Len = GetStringLengthH(V, PHIs); |
| 2765 | // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return |
| 2766 | // an empty string as a length. |
| 2767 | return Len == ~0ULL ? 1 : Len; |
| 2768 | } |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 2769 | |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 2770 | /// \brief \p PN defines a loop-variant pointer to an object. Check if the |
| 2771 | /// previous iteration of the loop was referring to the same object as \p PN. |
| 2772 | static bool isSameUnderlyingObjectInLoop(PHINode *PN, LoopInfo *LI) { |
| 2773 | // Find the loop-defined value. |
| 2774 | Loop *L = LI->getLoopFor(PN->getParent()); |
| 2775 | if (PN->getNumIncomingValues() != 2) |
| 2776 | return true; |
| 2777 | |
| 2778 | // Find the value from previous iteration. |
| 2779 | auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0)); |
| 2780 | if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) |
| 2781 | PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1)); |
| 2782 | if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) |
| 2783 | return true; |
| 2784 | |
| 2785 | // If a new pointer is loaded in the loop, the pointer references a different |
| 2786 | // object in every iteration. E.g.: |
| 2787 | // for (i) |
| 2788 | // int *p = a[i]; |
| 2789 | // ... |
| 2790 | if (auto *Load = dyn_cast<LoadInst>(PrevValue)) |
| 2791 | if (!L->isLoopInvariant(Load->getPointerOperand())) |
| 2792 | return false; |
| 2793 | return true; |
| 2794 | } |
| 2795 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2796 | Value *llvm::GetUnderlyingObject(Value *V, const DataLayout &DL, |
| 2797 | unsigned MaxLookup) { |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 2798 | if (!V->getType()->isPointerTy()) |
| 2799 | return V; |
| 2800 | for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) { |
| 2801 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
| 2802 | V = GEP->getPointerOperand(); |
Matt Arsenault | 70f4db88 | 2014-07-15 00:56:40 +0000 | [diff] [blame] | 2803 | } else if (Operator::getOpcode(V) == Instruction::BitCast || |
| 2804 | Operator::getOpcode(V) == Instruction::AddrSpaceCast) { |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 2805 | V = cast<Operator>(V)->getOperand(0); |
| 2806 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
| 2807 | if (GA->mayBeOverridden()) |
| 2808 | return V; |
| 2809 | V = GA->getAliasee(); |
| 2810 | } else { |
Dan Gohman | 05b18f1 | 2010-12-15 20:49:55 +0000 | [diff] [blame] | 2811 | // See if InstructionSimplify knows any relevant tricks. |
| 2812 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 2813 | // TODO: Acquire a DominatorTree and AssumptionCache and use them. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2814 | if (Value *Simplified = SimplifyInstruction(I, DL, nullptr)) { |
Dan Gohman | 05b18f1 | 2010-12-15 20:49:55 +0000 | [diff] [blame] | 2815 | V = Simplified; |
| 2816 | continue; |
| 2817 | } |
| 2818 | |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 2819 | return V; |
| 2820 | } |
| 2821 | assert(V->getType()->isPointerTy() && "Unexpected operand type!"); |
| 2822 | } |
| 2823 | return V; |
| 2824 | } |
Nick Lewycky | 3e334a4 | 2011-06-27 04:20:45 +0000 | [diff] [blame] | 2825 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2826 | void llvm::GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects, |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 2827 | const DataLayout &DL, LoopInfo *LI, |
| 2828 | unsigned MaxLookup) { |
Dan Gohman | ed7c24e2 | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 2829 | SmallPtrSet<Value *, 4> Visited; |
| 2830 | SmallVector<Value *, 4> Worklist; |
| 2831 | Worklist.push_back(V); |
| 2832 | do { |
| 2833 | Value *P = Worklist.pop_back_val(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2834 | P = GetUnderlyingObject(P, DL, MaxLookup); |
Dan Gohman | ed7c24e2 | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 2835 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 2836 | if (!Visited.insert(P).second) |
Dan Gohman | ed7c24e2 | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 2837 | continue; |
| 2838 | |
| 2839 | if (SelectInst *SI = dyn_cast<SelectInst>(P)) { |
| 2840 | Worklist.push_back(SI->getTrueValue()); |
| 2841 | Worklist.push_back(SI->getFalseValue()); |
| 2842 | continue; |
| 2843 | } |
| 2844 | |
| 2845 | if (PHINode *PN = dyn_cast<PHINode>(P)) { |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 2846 | // If this PHI changes the underlying object in every iteration of the |
| 2847 | // loop, don't look through it. Consider: |
| 2848 | // int **A; |
| 2849 | // for (i) { |
| 2850 | // Prev = Curr; // Prev = PHI (Prev_0, Curr) |
| 2851 | // Curr = A[i]; |
| 2852 | // *Prev, *Curr; |
| 2853 | // |
| 2854 | // Prev is tracking Curr one iteration behind so they refer to different |
| 2855 | // underlying objects. |
| 2856 | if (!LI || !LI->isLoopHeader(PN->getParent()) || |
| 2857 | isSameUnderlyingObjectInLoop(PN, LI)) |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 2858 | for (Value *IncValue : PN->incoming_values()) |
| 2859 | Worklist.push_back(IncValue); |
Dan Gohman | ed7c24e2 | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 2860 | continue; |
| 2861 | } |
| 2862 | |
| 2863 | Objects.push_back(P); |
| 2864 | } while (!Worklist.empty()); |
| 2865 | } |
| 2866 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2867 | /// Return true if the only users of this pointer are lifetime markers. |
Nick Lewycky | 3e334a4 | 2011-06-27 04:20:45 +0000 | [diff] [blame] | 2868 | bool llvm::onlyUsedByLifetimeMarkers(const Value *V) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2869 | for (const User *U : V->users()) { |
| 2870 | const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U); |
Nick Lewycky | 3e334a4 | 2011-06-27 04:20:45 +0000 | [diff] [blame] | 2871 | if (!II) return false; |
| 2872 | |
| 2873 | if (II->getIntrinsicID() != Intrinsic::lifetime_start && |
| 2874 | II->getIntrinsicID() != Intrinsic::lifetime_end) |
| 2875 | return false; |
| 2876 | } |
| 2877 | return true; |
| 2878 | } |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 2879 | |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2880 | static bool isDereferenceableFromAttribute(const Value *BV, APInt Offset, |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 2881 | Type *Ty, const DataLayout &DL, |
| 2882 | const Instruction *CtxI, |
| 2883 | const DominatorTree *DT, |
| 2884 | const TargetLibraryInfo *TLI) { |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2885 | assert(Offset.isNonNegative() && "offset can't be negative"); |
| 2886 | assert(Ty->isSized() && "must be sized"); |
| 2887 | |
| 2888 | APInt DerefBytes(Offset.getBitWidth(), 0); |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 2889 | bool CheckForNonNull = false; |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2890 | if (const Argument *A = dyn_cast<Argument>(BV)) { |
| 2891 | DerefBytes = A->getDereferenceableBytes(); |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 2892 | if (!DerefBytes.getBoolValue()) { |
| 2893 | DerefBytes = A->getDereferenceableOrNullBytes(); |
| 2894 | CheckForNonNull = true; |
| 2895 | } |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2896 | } else if (auto CS = ImmutableCallSite(BV)) { |
| 2897 | DerefBytes = CS.getDereferenceableBytes(0); |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 2898 | if (!DerefBytes.getBoolValue()) { |
| 2899 | DerefBytes = CS.getDereferenceableOrNullBytes(0); |
| 2900 | CheckForNonNull = true; |
| 2901 | } |
Sanjoy Das | f999547 | 2015-05-19 20:10:19 +0000 | [diff] [blame] | 2902 | } else if (const LoadInst *LI = dyn_cast<LoadInst>(BV)) { |
| 2903 | if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) { |
| 2904 | ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); |
| 2905 | DerefBytes = CI->getLimitedValue(); |
| 2906 | } |
| 2907 | if (!DerefBytes.getBoolValue()) { |
| 2908 | if (MDNode *MD = |
| 2909 | LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { |
| 2910 | ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); |
| 2911 | DerefBytes = CI->getLimitedValue(); |
| 2912 | } |
| 2913 | CheckForNonNull = true; |
| 2914 | } |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | if (DerefBytes.getBoolValue()) |
| 2918 | if (DerefBytes.uge(Offset + DL.getTypeStoreSize(Ty))) |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 2919 | if (!CheckForNonNull || isKnownNonNullAt(BV, CtxI, DT, TLI)) |
| 2920 | return true; |
| 2921 | |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2922 | return false; |
| 2923 | } |
| 2924 | |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 2925 | static bool isDereferenceableFromAttribute(const Value *V, const DataLayout &DL, |
| 2926 | const Instruction *CtxI, |
| 2927 | const DominatorTree *DT, |
| 2928 | const TargetLibraryInfo *TLI) { |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2929 | Type *VTy = V->getType(); |
| 2930 | Type *Ty = VTy->getPointerElementType(); |
| 2931 | if (!Ty->isSized()) |
| 2932 | return false; |
| 2933 | |
| 2934 | APInt Offset(DL.getTypeStoreSizeInBits(VTy), 0); |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 2935 | return isDereferenceableFromAttribute(V, Offset, Ty, DL, CtxI, DT, TLI); |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | /// Return true if Value is always a dereferenceable pointer. |
| 2939 | /// |
| 2940 | /// Test if V is always a pointer to allocated and suitably aligned memory for |
| 2941 | /// a simple load or store. |
| 2942 | static bool isDereferenceablePointer(const Value *V, const DataLayout &DL, |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 2943 | const Instruction *CtxI, |
| 2944 | const DominatorTree *DT, |
| 2945 | const TargetLibraryInfo *TLI, |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2946 | SmallPtrSetImpl<const Value *> &Visited) { |
| 2947 | // Note that it is not safe to speculate into a malloc'd region because |
| 2948 | // malloc may return null. |
| 2949 | |
| 2950 | // These are obviously ok. |
| 2951 | if (isa<AllocaInst>(V)) return true; |
| 2952 | |
| 2953 | // It's not always safe to follow a bitcast, for example: |
| 2954 | // bitcast i8* (alloca i8) to i32* |
| 2955 | // would result in a 4-byte load from a 1-byte alloca. However, |
| 2956 | // if we're casting from a pointer from a type of larger size |
| 2957 | // to a type of smaller size (or the same size), and the alignment |
| 2958 | // is at least as large as for the resulting pointer type, then |
| 2959 | // we can look through the bitcast. |
| 2960 | if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) { |
| 2961 | Type *STy = BC->getSrcTy()->getPointerElementType(), |
| 2962 | *DTy = BC->getDestTy()->getPointerElementType(); |
| 2963 | if (STy->isSized() && DTy->isSized() && |
| 2964 | (DL.getTypeStoreSize(STy) >= DL.getTypeStoreSize(DTy)) && |
| 2965 | (DL.getABITypeAlignment(STy) >= DL.getABITypeAlignment(DTy))) |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 2966 | return isDereferenceablePointer(BC->getOperand(0), DL, CtxI, |
| 2967 | DT, TLI, Visited); |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
| 2970 | // Global variables which can't collapse to null are ok. |
| 2971 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 2972 | return !GV->hasExternalWeakLinkage(); |
| 2973 | |
| 2974 | // byval arguments are okay. |
| 2975 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 2976 | if (A->hasByValAttr()) |
| 2977 | return true; |
| 2978 | |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 2979 | if (isDereferenceableFromAttribute(V, DL, CtxI, DT, TLI)) |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2980 | return true; |
| 2981 | |
| 2982 | // For GEPs, determine if the indexing lands within the allocated object. |
| 2983 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Artur Pilipenko | 7fad7e5 | 2015-06-08 11:58:13 +0000 | [diff] [blame] | 2984 | Type *VTy = GEP->getType(); |
| 2985 | Type *Ty = VTy->getPointerElementType(); |
| 2986 | const Value *Base = GEP->getPointerOperand(); |
| 2987 | |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2988 | // Conservatively require that the base pointer be fully dereferenceable. |
Artur Pilipenko | 7fad7e5 | 2015-06-08 11:58:13 +0000 | [diff] [blame] | 2989 | if (!Visited.insert(Base).second) |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2990 | return false; |
Artur Pilipenko | 7fad7e5 | 2015-06-08 11:58:13 +0000 | [diff] [blame] | 2991 | if (!isDereferenceablePointer(Base, DL, CtxI, |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 2992 | DT, TLI, Visited)) |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 2993 | return false; |
Artur Pilipenko | 7fad7e5 | 2015-06-08 11:58:13 +0000 | [diff] [blame] | 2994 | |
| 2995 | APInt Offset(DL.getPointerTypeSizeInBits(VTy), 0); |
| 2996 | if (!GEP->accumulateConstantOffset(DL, Offset)) |
| 2997 | return false; |
| 2998 | |
| 2999 | // Check if the load is within the bounds of the underlying object. |
| 3000 | uint64_t LoadSize = DL.getTypeStoreSize(Ty); |
| 3001 | Type *BaseType = Base->getType()->getPointerElementType(); |
| 3002 | return (Offset + LoadSize).ule(DL.getTypeAllocSize(BaseType)); |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | // For gc.relocate, look through relocations |
| 3006 | if (const IntrinsicInst *I = dyn_cast<IntrinsicInst>(V)) |
| 3007 | if (I->getIntrinsicID() == Intrinsic::experimental_gc_relocate) { |
| 3008 | GCRelocateOperands RelocateInst(I); |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 3009 | return isDereferenceablePointer(RelocateInst.getDerivedPtr(), DL, CtxI, |
| 3010 | DT, TLI, Visited); |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V)) |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 3014 | return isDereferenceablePointer(ASC->getOperand(0), DL, CtxI, |
| 3015 | DT, TLI, Visited); |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 3016 | |
| 3017 | // If we don't know, assume the worst. |
| 3018 | return false; |
| 3019 | } |
| 3020 | |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 3021 | bool llvm::isDereferenceablePointer(const Value *V, const DataLayout &DL, |
| 3022 | const Instruction *CtxI, |
| 3023 | const DominatorTree *DT, |
| 3024 | const TargetLibraryInfo *TLI) { |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 3025 | // When dereferenceability information is provided by a dereferenceable |
| 3026 | // attribute, we know exactly how many bytes are dereferenceable. If we can |
| 3027 | // determine the exact offset to the attributed variable, we can use that |
| 3028 | // information here. |
| 3029 | Type *VTy = V->getType(); |
| 3030 | Type *Ty = VTy->getPointerElementType(); |
| 3031 | if (Ty->isSized()) { |
| 3032 | APInt Offset(DL.getTypeStoreSizeInBits(VTy), 0); |
| 3033 | const Value *BV = V->stripAndAccumulateInBoundsConstantOffsets(DL, Offset); |
| 3034 | |
| 3035 | if (Offset.isNonNegative()) |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 3036 | if (isDereferenceableFromAttribute(BV, Offset, Ty, DL, |
| 3037 | CtxI, DT, TLI)) |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 3038 | return true; |
| 3039 | } |
| 3040 | |
| 3041 | SmallPtrSet<const Value *, 32> Visited; |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 3042 | return ::isDereferenceablePointer(V, DL, CtxI, DT, TLI, Visited); |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 3045 | bool llvm::isSafeToSpeculativelyExecute(const Value *V, |
| 3046 | const Instruction *CtxI, |
| 3047 | const DominatorTree *DT, |
| 3048 | const TargetLibraryInfo *TLI) { |
Dan Gohman | 7ac046a | 2012-01-04 23:01:09 +0000 | [diff] [blame] | 3049 | const Operator *Inst = dyn_cast<Operator>(V); |
| 3050 | if (!Inst) |
| 3051 | return false; |
| 3052 | |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3053 | for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) |
| 3054 | if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i))) |
| 3055 | if (C->canTrap()) |
| 3056 | return false; |
| 3057 | |
| 3058 | switch (Inst->getOpcode()) { |
| 3059 | default: |
| 3060 | return true; |
| 3061 | case Instruction::UDiv: |
David Majnemer | f20d7c4 | 2014-11-04 23:49:08 +0000 | [diff] [blame] | 3062 | case Instruction::URem: { |
| 3063 | // x / y is undefined if y == 0. |
| 3064 | const APInt *V; |
| 3065 | if (match(Inst->getOperand(1), m_APInt(V))) |
| 3066 | return *V != 0; |
| 3067 | return false; |
| 3068 | } |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3069 | case Instruction::SDiv: |
| 3070 | case Instruction::SRem: { |
David Majnemer | f20d7c4 | 2014-11-04 23:49:08 +0000 | [diff] [blame] | 3071 | // 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] | 3072 | const APInt *Numerator, *Denominator; |
| 3073 | if (!match(Inst->getOperand(1), m_APInt(Denominator))) |
| 3074 | return false; |
| 3075 | // We cannot hoist this division if the denominator is 0. |
| 3076 | if (*Denominator == 0) |
| 3077 | return false; |
| 3078 | // It's safe to hoist if the denominator is not 0 or -1. |
| 3079 | if (*Denominator != -1) |
| 3080 | return true; |
| 3081 | // At this point we know that the denominator is -1. It is safe to hoist as |
| 3082 | // long we know that the numerator is not INT_MIN. |
| 3083 | if (match(Inst->getOperand(0), m_APInt(Numerator))) |
| 3084 | return !Numerator->isMinSignedValue(); |
| 3085 | // The numerator *might* be MinSignedValue. |
David Majnemer | f20d7c4 | 2014-11-04 23:49:08 +0000 | [diff] [blame] | 3086 | return false; |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3087 | } |
| 3088 | case Instruction::Load: { |
| 3089 | const LoadInst *LI = cast<LoadInst>(Inst); |
Kostya Serebryany | 0b45828 | 2013-11-21 07:29:28 +0000 | [diff] [blame] | 3090 | if (!LI->isUnordered() || |
| 3091 | // Speculative load may create a race that did not exist in the source. |
| 3092 | LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeThread)) |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3093 | return false; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3094 | const DataLayout &DL = LI->getModule()->getDataLayout(); |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 3095 | return isDereferenceablePointer(LI->getPointerOperand(), DL, CtxI, DT, TLI); |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3096 | } |
Nick Lewycky | b4039f6 | 2011-12-21 05:52:02 +0000 | [diff] [blame] | 3097 | case Instruction::Call: { |
Michael Liao | 736bac6 | 2014-11-06 19:05:57 +0000 | [diff] [blame] | 3098 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
| 3099 | switch (II->getIntrinsicID()) { |
| 3100 | // These synthetic intrinsics have no side-effects and just mark |
| 3101 | // information about their operands. |
| 3102 | // FIXME: There are other no-op synthetic instructions that potentially |
| 3103 | // should be considered at least *safe* to speculate... |
| 3104 | case Intrinsic::dbg_declare: |
| 3105 | case Intrinsic::dbg_value: |
| 3106 | return true; |
Chandler Carruth | 28192c9 | 2012-04-07 19:22:18 +0000 | [diff] [blame] | 3107 | |
Michael Liao | 736bac6 | 2014-11-06 19:05:57 +0000 | [diff] [blame] | 3108 | case Intrinsic::bswap: |
| 3109 | case Intrinsic::ctlz: |
| 3110 | case Intrinsic::ctpop: |
| 3111 | case Intrinsic::cttz: |
| 3112 | case Intrinsic::objectsize: |
| 3113 | case Intrinsic::sadd_with_overflow: |
| 3114 | case Intrinsic::smul_with_overflow: |
| 3115 | case Intrinsic::ssub_with_overflow: |
| 3116 | case Intrinsic::uadd_with_overflow: |
| 3117 | case Intrinsic::umul_with_overflow: |
| 3118 | case Intrinsic::usub_with_overflow: |
| 3119 | return true; |
| 3120 | // Sqrt should be OK, since the llvm sqrt intrinsic isn't defined to set |
| 3121 | // errno like libm sqrt would. |
| 3122 | case Intrinsic::sqrt: |
| 3123 | case Intrinsic::fma: |
| 3124 | case Intrinsic::fmuladd: |
| 3125 | case Intrinsic::fabs: |
| 3126 | case Intrinsic::minnum: |
| 3127 | case Intrinsic::maxnum: |
| 3128 | return true; |
| 3129 | // TODO: some fp intrinsics are marked as having the same error handling |
| 3130 | // as libm. They're safe to speculate when they won't error. |
| 3131 | // TODO: are convert_{from,to}_fp16 safe? |
| 3132 | // TODO: can we list target-specific intrinsics here? |
| 3133 | default: break; |
| 3134 | } |
| 3135 | } |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3136 | return false; // The called function could have undefined behavior or |
Nick Lewycky | b4039f6 | 2011-12-21 05:52:02 +0000 | [diff] [blame] | 3137 | // side-effects, even if marked readnone nounwind. |
| 3138 | } |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3139 | case Instruction::VAArg: |
| 3140 | case Instruction::Alloca: |
| 3141 | case Instruction::Invoke: |
| 3142 | case Instruction::PHI: |
| 3143 | case Instruction::Store: |
| 3144 | case Instruction::Ret: |
| 3145 | case Instruction::Br: |
| 3146 | case Instruction::IndirectBr: |
| 3147 | case Instruction::Switch: |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3148 | case Instruction::Unreachable: |
| 3149 | case Instruction::Fence: |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3150 | case Instruction::AtomicRMW: |
| 3151 | case Instruction::AtomicCmpXchg: |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3152 | case Instruction::LandingPad: |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3153 | case Instruction::Resume: |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3154 | case Instruction::CatchPad: |
| 3155 | case Instruction::CatchEndPad: |
| 3156 | case Instruction::CatchRet: |
| 3157 | case Instruction::CleanupPad: |
| 3158 | case Instruction::CleanupRet: |
| 3159 | case Instruction::TerminatePad: |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3160 | return false; // Misc instructions which have effects |
| 3161 | } |
| 3162 | } |
Dan Gohman | 1b0f79d | 2013-01-31 02:40:59 +0000 | [diff] [blame] | 3163 | |
Quentin Colombet | 6443cce | 2015-08-06 18:44:34 +0000 | [diff] [blame] | 3164 | bool llvm::mayBeMemoryDependent(const Instruction &I) { |
| 3165 | return I.mayReadOrWriteMemory() || !isSafeToSpeculativelyExecute(&I); |
| 3166 | } |
| 3167 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 3168 | /// Return true if we know that the specified value is never null. |
Benjamin Kramer | fd4777c | 2013-09-24 16:37:51 +0000 | [diff] [blame] | 3169 | bool llvm::isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI) { |
Dan Gohman | 1b0f79d | 2013-01-31 02:40:59 +0000 | [diff] [blame] | 3170 | // Alloca never returns null, malloc might. |
| 3171 | if (isa<AllocaInst>(V)) return true; |
| 3172 | |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 3173 | // A byval, inalloca, or nonnull argument is never null. |
Dan Gohman | 1b0f79d | 2013-01-31 02:40:59 +0000 | [diff] [blame] | 3174 | if (const Argument *A = dyn_cast<Argument>(V)) |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 3175 | return A->hasByValOrInAllocaAttr() || A->hasNonNullAttr(); |
Dan Gohman | 1b0f79d | 2013-01-31 02:40:59 +0000 | [diff] [blame] | 3176 | |
| 3177 | // Global values are not null unless extern weak. |
| 3178 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 3179 | return !GV->hasExternalWeakLinkage(); |
Benjamin Kramer | fd4777c | 2013-09-24 16:37:51 +0000 | [diff] [blame] | 3180 | |
Philip Reames | cdb72f3 | 2014-10-20 22:40:55 +0000 | [diff] [blame] | 3181 | // A Load tagged w/nonnull metadata is never null. |
| 3182 | if (const LoadInst *LI = dyn_cast<LoadInst>(V)) |
Philip Reames | 5a3f5f7 | 2014-10-21 00:13:20 +0000 | [diff] [blame] | 3183 | return LI->getMetadata(LLVMContext::MD_nonnull); |
Philip Reames | cdb72f3 | 2014-10-20 22:40:55 +0000 | [diff] [blame] | 3184 | |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 3185 | if (auto CS = ImmutableCallSite(V)) |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 3186 | if (CS.isReturnNonNull()) |
Nick Lewycky | ec37354 | 2014-05-20 05:13:21 +0000 | [diff] [blame] | 3187 | return true; |
| 3188 | |
Benjamin Kramer | fd4777c | 2013-09-24 16:37:51 +0000 | [diff] [blame] | 3189 | // operator new never returns null. |
| 3190 | if (isOperatorNewLikeFn(V, TLI, /*LookThroughBitCast=*/true)) |
| 3191 | return true; |
| 3192 | |
Dan Gohman | 1b0f79d | 2013-01-31 02:40:59 +0000 | [diff] [blame] | 3193 | return false; |
| 3194 | } |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3195 | |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 3196 | static bool isKnownNonNullFromDominatingCondition(const Value *V, |
| 3197 | const Instruction *CtxI, |
| 3198 | const DominatorTree *DT) { |
| 3199 | unsigned NumUsesExplored = 0; |
| 3200 | for (auto U : V->users()) { |
| 3201 | // Avoid massive lists |
| 3202 | if (NumUsesExplored >= DomConditionsMaxUses) |
| 3203 | break; |
| 3204 | NumUsesExplored++; |
| 3205 | // Consider only compare instructions uniquely controlling a branch |
| 3206 | const ICmpInst *Cmp = dyn_cast<ICmpInst>(U); |
| 3207 | if (!Cmp) |
| 3208 | continue; |
| 3209 | |
| 3210 | if (DomConditionsSingleCmpUse && !Cmp->hasOneUse()) |
| 3211 | continue; |
| 3212 | |
| 3213 | for (auto *CmpU : Cmp->users()) { |
| 3214 | const BranchInst *BI = dyn_cast<BranchInst>(CmpU); |
| 3215 | if (!BI) |
| 3216 | continue; |
| 3217 | |
| 3218 | assert(BI->isConditional() && "uses a comparison!"); |
| 3219 | |
| 3220 | BasicBlock *NonNullSuccessor = nullptr; |
| 3221 | CmpInst::Predicate Pred; |
| 3222 | |
| 3223 | if (match(const_cast<ICmpInst*>(Cmp), |
| 3224 | m_c_ICmp(Pred, m_Specific(V), m_Zero()))) { |
| 3225 | if (Pred == ICmpInst::ICMP_EQ) |
| 3226 | NonNullSuccessor = BI->getSuccessor(1); |
| 3227 | else if (Pred == ICmpInst::ICMP_NE) |
| 3228 | NonNullSuccessor = BI->getSuccessor(0); |
| 3229 | } |
| 3230 | |
| 3231 | if (NonNullSuccessor) { |
| 3232 | BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor); |
| 3233 | if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent())) |
| 3234 | return true; |
| 3235 | } |
| 3236 | } |
| 3237 | } |
| 3238 | |
| 3239 | return false; |
| 3240 | } |
| 3241 | |
| 3242 | bool llvm::isKnownNonNullAt(const Value *V, const Instruction *CtxI, |
| 3243 | const DominatorTree *DT, const TargetLibraryInfo *TLI) { |
| 3244 | if (isKnownNonNull(V, TLI)) |
| 3245 | return true; |
| 3246 | |
| 3247 | return CtxI ? ::isKnownNonNullFromDominatingCondition(V, CtxI, DT) : false; |
| 3248 | } |
| 3249 | |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3250 | OverflowResult llvm::computeOverflowForUnsignedMul(Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3251 | const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3252 | AssumptionCache *AC, |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3253 | const Instruction *CxtI, |
| 3254 | const DominatorTree *DT) { |
| 3255 | // Multiplying n * m significant bits yields a result of n + m significant |
| 3256 | // bits. If the total number of significant bits does not exceed the |
| 3257 | // result bit width (minus 1), there is no overflow. |
| 3258 | // This means if we have enough leading zero bits in the operands |
| 3259 | // we can guarantee that the result does not overflow. |
| 3260 | // Ref: "Hacker's Delight" by Henry Warren |
| 3261 | unsigned BitWidth = LHS->getType()->getScalarSizeInBits(); |
| 3262 | APInt LHSKnownZero(BitWidth, 0); |
David Majnemer | c8a576b | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3263 | APInt LHSKnownOne(BitWidth, 0); |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3264 | APInt RHSKnownZero(BitWidth, 0); |
David Majnemer | c8a576b | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3265 | APInt RHSKnownOne(BitWidth, 0); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3266 | computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, DL, /*Depth=*/0, AC, CxtI, |
| 3267 | DT); |
| 3268 | computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, DL, /*Depth=*/0, AC, CxtI, |
| 3269 | DT); |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3270 | // Note that underestimating the number of zero bits gives a more |
| 3271 | // conservative answer. |
| 3272 | unsigned ZeroBits = LHSKnownZero.countLeadingOnes() + |
| 3273 | RHSKnownZero.countLeadingOnes(); |
| 3274 | // First handle the easy case: if we have enough zero bits there's |
| 3275 | // definitely no overflow. |
| 3276 | if (ZeroBits >= BitWidth) |
| 3277 | return OverflowResult::NeverOverflows; |
| 3278 | |
| 3279 | // Get the largest possible values for each operand. |
| 3280 | APInt LHSMax = ~LHSKnownZero; |
| 3281 | APInt RHSMax = ~RHSKnownZero; |
| 3282 | |
| 3283 | // We know the multiply operation doesn't overflow if the maximum values for |
| 3284 | // each operand will not overflow after we multiply them together. |
David Majnemer | c8a576b | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3285 | bool MaxOverflow; |
| 3286 | LHSMax.umul_ov(RHSMax, MaxOverflow); |
| 3287 | if (!MaxOverflow) |
| 3288 | return OverflowResult::NeverOverflows; |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3289 | |
David Majnemer | c8a576b | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3290 | // We know it always overflows if multiplying the smallest possible values for |
| 3291 | // the operands also results in overflow. |
| 3292 | bool MinOverflow; |
| 3293 | LHSKnownOne.umul_ov(RHSKnownOne, MinOverflow); |
| 3294 | if (MinOverflow) |
| 3295 | return OverflowResult::AlwaysOverflows; |
| 3296 | |
| 3297 | return OverflowResult::MayOverflow; |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3298 | } |
David Majnemer | 5310c1e | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 3299 | |
| 3300 | OverflowResult llvm::computeOverflowForUnsignedAdd(Value *LHS, Value *RHS, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3301 | const DataLayout &DL, |
David Majnemer | 5310c1e | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 3302 | AssumptionCache *AC, |
| 3303 | const Instruction *CxtI, |
| 3304 | const DominatorTree *DT) { |
| 3305 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 3306 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, DL, /*Depth=*/0, |
| 3307 | AC, CxtI, DT); |
| 3308 | if (LHSKnownNonNegative || LHSKnownNegative) { |
| 3309 | bool RHSKnownNonNegative, RHSKnownNegative; |
| 3310 | ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, DL, /*Depth=*/0, |
| 3311 | AC, CxtI, DT); |
| 3312 | |
| 3313 | if (LHSKnownNegative && RHSKnownNegative) { |
| 3314 | // The sign bit is set in both cases: this MUST overflow. |
| 3315 | // Create a simple add instruction, and insert it into the struct. |
| 3316 | return OverflowResult::AlwaysOverflows; |
| 3317 | } |
| 3318 | |
| 3319 | if (LHSKnownNonNegative && RHSKnownNonNegative) { |
| 3320 | // The sign bit is clear in both cases: this CANNOT overflow. |
| 3321 | // Create a simple add instruction, and insert it into the struct. |
| 3322 | return OverflowResult::NeverOverflows; |
| 3323 | } |
| 3324 | } |
| 3325 | |
| 3326 | return OverflowResult::MayOverflow; |
| 3327 | } |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3328 | |
Jingyue Wu | 42f1d67 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 3329 | bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) { |
| 3330 | // FIXME: This conservative implementation can be relaxed. E.g. most |
| 3331 | // atomic operations are guaranteed to terminate on most platforms |
| 3332 | // and most functions terminate. |
| 3333 | |
| 3334 | return !I->isAtomic() && // atomics may never succeed on some platforms |
| 3335 | !isa<CallInst>(I) && // could throw and might not terminate |
| 3336 | !isa<InvokeInst>(I) && // might not terminate and could throw to |
| 3337 | // non-successor (see bug 24185 for details). |
| 3338 | !isa<ResumeInst>(I) && // has no successors |
| 3339 | !isa<ReturnInst>(I); // has no successors |
| 3340 | } |
| 3341 | |
| 3342 | bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I, |
| 3343 | const Loop *L) { |
| 3344 | // The loop header is guaranteed to be executed for every iteration. |
| 3345 | // |
| 3346 | // FIXME: Relax this constraint to cover all basic blocks that are |
| 3347 | // guaranteed to be executed at every iteration. |
| 3348 | if (I->getParent() != L->getHeader()) return false; |
| 3349 | |
| 3350 | for (const Instruction &LI : *L->getHeader()) { |
| 3351 | if (&LI == I) return true; |
| 3352 | if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false; |
| 3353 | } |
| 3354 | llvm_unreachable("Instruction not contained in its own parent basic block."); |
| 3355 | } |
| 3356 | |
| 3357 | bool llvm::propagatesFullPoison(const Instruction *I) { |
| 3358 | switch (I->getOpcode()) { |
| 3359 | case Instruction::Add: |
| 3360 | case Instruction::Sub: |
| 3361 | case Instruction::Xor: |
| 3362 | case Instruction::Trunc: |
| 3363 | case Instruction::BitCast: |
| 3364 | case Instruction::AddrSpaceCast: |
| 3365 | // These operations all propagate poison unconditionally. Note that poison |
| 3366 | // is not any particular value, so xor or subtraction of poison with |
| 3367 | // itself still yields poison, not zero. |
| 3368 | return true; |
| 3369 | |
| 3370 | case Instruction::AShr: |
| 3371 | case Instruction::SExt: |
| 3372 | // For these operations, one bit of the input is replicated across |
| 3373 | // multiple output bits. A replicated poison bit is still poison. |
| 3374 | return true; |
| 3375 | |
| 3376 | case Instruction::Shl: { |
| 3377 | // Left shift *by* a poison value is poison. The number of |
| 3378 | // positions to shift is unsigned, so no negative values are |
| 3379 | // possible there. Left shift by zero places preserves poison. So |
| 3380 | // it only remains to consider left shift of poison by a positive |
| 3381 | // number of places. |
| 3382 | // |
| 3383 | // A left shift by a positive number of places leaves the lowest order bit |
| 3384 | // non-poisoned. However, if such a shift has a no-wrap flag, then we can |
| 3385 | // make the poison operand violate that flag, yielding a fresh full-poison |
| 3386 | // value. |
| 3387 | auto *OBO = cast<OverflowingBinaryOperator>(I); |
| 3388 | return OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap(); |
| 3389 | } |
| 3390 | |
| 3391 | case Instruction::Mul: { |
| 3392 | // A multiplication by zero yields a non-poison zero result, so we need to |
| 3393 | // rule out zero as an operand. Conservatively, multiplication by a |
| 3394 | // non-zero constant is not multiplication by zero. |
| 3395 | // |
| 3396 | // Multiplication by a non-zero constant can leave some bits |
| 3397 | // non-poisoned. For example, a multiplication by 2 leaves the lowest |
| 3398 | // order bit unpoisoned. So we need to consider that. |
| 3399 | // |
| 3400 | // Multiplication by 1 preserves poison. If the multiplication has a |
| 3401 | // no-wrap flag, then we can make the poison operand violate that flag |
| 3402 | // when multiplied by any integer other than 0 and 1. |
| 3403 | auto *OBO = cast<OverflowingBinaryOperator>(I); |
| 3404 | if (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) { |
| 3405 | for (Value *V : OBO->operands()) { |
| 3406 | if (auto *CI = dyn_cast<ConstantInt>(V)) { |
| 3407 | // A ConstantInt cannot yield poison, so we can assume that it is |
| 3408 | // the other operand that is poison. |
| 3409 | return !CI->isZero(); |
| 3410 | } |
| 3411 | } |
| 3412 | } |
| 3413 | return false; |
| 3414 | } |
| 3415 | |
| 3416 | case Instruction::GetElementPtr: |
| 3417 | // A GEP implicitly represents a sequence of additions, subtractions, |
| 3418 | // truncations, sign extensions and multiplications. The multiplications |
| 3419 | // are by the non-zero sizes of some set of types, so we do not have to be |
| 3420 | // concerned with multiplication by zero. If the GEP is in-bounds, then |
| 3421 | // these operations are implicitly no-signed-wrap so poison is propagated |
| 3422 | // by the arguments above for Add, Sub, Trunc, SExt and Mul. |
| 3423 | return cast<GEPOperator>(I)->isInBounds(); |
| 3424 | |
| 3425 | default: |
| 3426 | return false; |
| 3427 | } |
| 3428 | } |
| 3429 | |
| 3430 | const Value *llvm::getGuaranteedNonFullPoisonOp(const Instruction *I) { |
| 3431 | switch (I->getOpcode()) { |
| 3432 | case Instruction::Store: |
| 3433 | return cast<StoreInst>(I)->getPointerOperand(); |
| 3434 | |
| 3435 | case Instruction::Load: |
| 3436 | return cast<LoadInst>(I)->getPointerOperand(); |
| 3437 | |
| 3438 | case Instruction::AtomicCmpXchg: |
| 3439 | return cast<AtomicCmpXchgInst>(I)->getPointerOperand(); |
| 3440 | |
| 3441 | case Instruction::AtomicRMW: |
| 3442 | return cast<AtomicRMWInst>(I)->getPointerOperand(); |
| 3443 | |
| 3444 | case Instruction::UDiv: |
| 3445 | case Instruction::SDiv: |
| 3446 | case Instruction::URem: |
| 3447 | case Instruction::SRem: |
| 3448 | return I->getOperand(1); |
| 3449 | |
| 3450 | default: |
| 3451 | return nullptr; |
| 3452 | } |
| 3453 | } |
| 3454 | |
| 3455 | bool llvm::isKnownNotFullPoison(const Instruction *PoisonI) { |
| 3456 | // We currently only look for uses of poison values within the same basic |
| 3457 | // block, as that makes it easier to guarantee that the uses will be |
| 3458 | // executed given that PoisonI is executed. |
| 3459 | // |
| 3460 | // FIXME: Expand this to consider uses beyond the same basic block. To do |
| 3461 | // this, look out for the distinction between post-dominance and strong |
| 3462 | // post-dominance. |
| 3463 | const BasicBlock *BB = PoisonI->getParent(); |
| 3464 | |
| 3465 | // Set of instructions that we have proved will yield poison if PoisonI |
| 3466 | // does. |
| 3467 | SmallSet<const Value *, 16> YieldsPoison; |
| 3468 | YieldsPoison.insert(PoisonI); |
| 3469 | |
| 3470 | for (const Instruction *I = PoisonI, *E = BB->end(); I != E; |
| 3471 | I = I->getNextNode()) { |
| 3472 | if (I != PoisonI) { |
| 3473 | const Value *NotPoison = getGuaranteedNonFullPoisonOp(I); |
| 3474 | if (NotPoison != nullptr && YieldsPoison.count(NotPoison)) return true; |
| 3475 | if (!isGuaranteedToTransferExecutionToSuccessor(I)) return false; |
| 3476 | } |
| 3477 | |
| 3478 | // Mark poison that propagates from I through uses of I. |
| 3479 | if (YieldsPoison.count(I)) { |
| 3480 | for (const User *User : I->users()) { |
| 3481 | const Instruction *UserI = cast<Instruction>(User); |
| 3482 | if (UserI->getParent() == BB && propagatesFullPoison(UserI)) |
| 3483 | YieldsPoison.insert(User); |
| 3484 | } |
| 3485 | } |
| 3486 | } |
| 3487 | return false; |
| 3488 | } |
| 3489 | |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3490 | static bool isKnownNonNaN(Value *V, FastMathFlags FMF) { |
| 3491 | if (FMF.noNaNs()) |
| 3492 | return true; |
| 3493 | |
| 3494 | if (auto *C = dyn_cast<ConstantFP>(V)) |
| 3495 | return !C->isNaN(); |
| 3496 | return false; |
| 3497 | } |
| 3498 | |
| 3499 | static bool isKnownNonZero(Value *V) { |
| 3500 | if (auto *C = dyn_cast<ConstantFP>(V)) |
| 3501 | return !C->isZero(); |
| 3502 | return false; |
| 3503 | } |
| 3504 | |
| 3505 | static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, |
| 3506 | FastMathFlags FMF, |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 3507 | Value *CmpLHS, Value *CmpRHS, |
| 3508 | Value *TrueVal, Value *FalseVal, |
| 3509 | Value *&LHS, Value *&RHS) { |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3510 | LHS = CmpLHS; |
| 3511 | RHS = CmpRHS; |
| 3512 | |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3513 | // If the predicate is an "or-equal" (FP) predicate, then signed zeroes may |
| 3514 | // return inconsistent results between implementations. |
| 3515 | // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0 |
| 3516 | // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1) |
| 3517 | // Therefore we behave conservatively and only proceed if at least one of the |
| 3518 | // operands is known to not be zero, or if we don't care about signed zeroes. |
| 3519 | switch (Pred) { |
| 3520 | default: break; |
| 3521 | case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE: |
| 3522 | case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE: |
| 3523 | if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) && |
| 3524 | !isKnownNonZero(CmpRHS)) |
| 3525 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 3526 | } |
| 3527 | |
| 3528 | SelectPatternNaNBehavior NaNBehavior = SPNB_NA; |
| 3529 | bool Ordered = false; |
| 3530 | |
| 3531 | // When given one NaN and one non-NaN input: |
| 3532 | // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input. |
| 3533 | // - A simple C99 (a < b ? a : b) construction will return 'b' (as the |
| 3534 | // ordered comparison fails), which could be NaN or non-NaN. |
| 3535 | // so here we discover exactly what NaN behavior is required/accepted. |
| 3536 | if (CmpInst::isFPPredicate(Pred)) { |
| 3537 | bool LHSSafe = isKnownNonNaN(CmpLHS, FMF); |
| 3538 | bool RHSSafe = isKnownNonNaN(CmpRHS, FMF); |
| 3539 | |
| 3540 | if (LHSSafe && RHSSafe) { |
| 3541 | // Both operands are known non-NaN. |
| 3542 | NaNBehavior = SPNB_RETURNS_ANY; |
| 3543 | } else if (CmpInst::isOrdered(Pred)) { |
| 3544 | // An ordered comparison will return false when given a NaN, so it |
| 3545 | // returns the RHS. |
| 3546 | Ordered = true; |
| 3547 | if (LHSSafe) |
James Molloy | 8990b06 | 2015-08-12 15:11:43 +0000 | [diff] [blame^] | 3548 | // LHS is non-NaN, so if RHS is NaN then NaN will be returned. |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3549 | NaNBehavior = SPNB_RETURNS_NAN; |
| 3550 | else if (RHSSafe) |
| 3551 | NaNBehavior = SPNB_RETURNS_OTHER; |
| 3552 | else |
| 3553 | // Completely unsafe. |
| 3554 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 3555 | } else { |
| 3556 | Ordered = false; |
| 3557 | // An unordered comparison will return true when given a NaN, so it |
| 3558 | // returns the LHS. |
| 3559 | if (LHSSafe) |
James Molloy | 8990b06 | 2015-08-12 15:11:43 +0000 | [diff] [blame^] | 3560 | // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned. |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3561 | NaNBehavior = SPNB_RETURNS_OTHER; |
| 3562 | else if (RHSSafe) |
| 3563 | NaNBehavior = SPNB_RETURNS_NAN; |
| 3564 | else |
| 3565 | // Completely unsafe. |
| 3566 | return {SPF_UNKNOWN, SPNB_NA, false}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3567 | } |
| 3568 | } |
| 3569 | |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3570 | if (TrueVal == CmpRHS && FalseVal == CmpLHS) { |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3571 | std::swap(CmpLHS, CmpRHS); |
| 3572 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 3573 | if (NaNBehavior == SPNB_RETURNS_NAN) |
| 3574 | NaNBehavior = SPNB_RETURNS_OTHER; |
| 3575 | else if (NaNBehavior == SPNB_RETURNS_OTHER) |
| 3576 | NaNBehavior = SPNB_RETURNS_NAN; |
| 3577 | Ordered = !Ordered; |
| 3578 | } |
| 3579 | |
| 3580 | // ([if]cmp X, Y) ? X : Y |
| 3581 | if (TrueVal == CmpLHS && FalseVal == CmpRHS) { |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3582 | switch (Pred) { |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3583 | default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality. |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3584 | case ICmpInst::ICMP_UGT: |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3585 | case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3586 | case ICmpInst::ICMP_SGT: |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3587 | case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3588 | case ICmpInst::ICMP_ULT: |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3589 | case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3590 | case ICmpInst::ICMP_SLT: |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3591 | case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false}; |
| 3592 | case FCmpInst::FCMP_UGT: |
| 3593 | case FCmpInst::FCMP_UGE: |
| 3594 | case FCmpInst::FCMP_OGT: |
| 3595 | case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered}; |
| 3596 | case FCmpInst::FCMP_ULT: |
| 3597 | case FCmpInst::FCMP_ULE: |
| 3598 | case FCmpInst::FCMP_OLT: |
| 3599 | case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3600 | } |
| 3601 | } |
| 3602 | |
| 3603 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(CmpRHS)) { |
| 3604 | if ((CmpLHS == TrueVal && match(FalseVal, m_Neg(m_Specific(CmpLHS)))) || |
| 3605 | (CmpLHS == FalseVal && match(TrueVal, m_Neg(m_Specific(CmpLHS))))) { |
| 3606 | |
| 3607 | // ABS(X) ==> (X >s 0) ? X : -X and (X >s -1) ? X : -X |
| 3608 | // NABS(X) ==> (X >s 0) ? -X : X and (X >s -1) ? -X : X |
| 3609 | if (Pred == ICmpInst::ICMP_SGT && (C1->isZero() || C1->isMinusOne())) { |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3610 | return {(CmpLHS == TrueVal) ? SPF_ABS : SPF_NABS, SPNB_NA, false}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | // ABS(X) ==> (X <s 0) ? -X : X and (X <s 1) ? -X : X |
| 3614 | // NABS(X) ==> (X <s 0) ? X : -X and (X <s 1) ? X : -X |
| 3615 | if (Pred == ICmpInst::ICMP_SLT && (C1->isZero() || C1->isOne())) { |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3616 | return {(CmpLHS == FalseVal) ? SPF_ABS : SPF_NABS, SPNB_NA, false}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3617 | } |
| 3618 | } |
| 3619 | |
| 3620 | // Y >s C ? ~Y : ~C == ~Y <s ~C ? ~Y : ~C = SMIN(~Y, ~C) |
| 3621 | if (const auto *C2 = dyn_cast<ConstantInt>(FalseVal)) { |
| 3622 | if (C1->getType() == C2->getType() && ~C1->getValue() == C2->getValue() && |
| 3623 | (match(TrueVal, m_Not(m_Specific(CmpLHS))) || |
| 3624 | match(CmpLHS, m_Not(m_Specific(TrueVal))))) { |
| 3625 | LHS = TrueVal; |
| 3626 | RHS = FalseVal; |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3627 | return {SPF_SMIN, SPNB_NA, false}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3628 | } |
| 3629 | } |
| 3630 | } |
| 3631 | |
| 3632 | // TODO: (X > 4) ? X : 5 --> (X >= 5) ? X : 5 --> MAX(X, 5) |
| 3633 | |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3634 | return {SPF_UNKNOWN, SPNB_NA, false}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3635 | } |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 3636 | |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3637 | static Constant *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 3638 | Instruction::CastOps *CastOp) { |
| 3639 | CastInst *CI = dyn_cast<CastInst>(V1); |
| 3640 | Constant *C = dyn_cast<Constant>(V2); |
| 3641 | if (!CI || !C) |
| 3642 | return nullptr; |
| 3643 | *CastOp = CI->getOpcode(); |
| 3644 | |
James Molloy | 2b21a7c | 2015-05-20 18:41:25 +0000 | [diff] [blame] | 3645 | if (isa<SExtInst>(CI) && CmpI->isSigned()) { |
| 3646 | Constant *T = ConstantExpr::getTrunc(C, CI->getSrcTy()); |
| 3647 | // This is only valid if the truncated value can be sign-extended |
| 3648 | // back to the original value. |
| 3649 | if (ConstantExpr::getSExt(T, C->getType()) == C) |
| 3650 | return T; |
| 3651 | return nullptr; |
| 3652 | } |
| 3653 | if (isa<ZExtInst>(CI) && CmpI->isUnsigned()) |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 3654 | return ConstantExpr::getTrunc(C, CI->getSrcTy()); |
| 3655 | |
| 3656 | if (isa<TruncInst>(CI)) |
| 3657 | return ConstantExpr::getIntegerCast(C, CI->getSrcTy(), CmpI->isSigned()); |
| 3658 | |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3659 | if (isa<FPToUIInst>(CI)) |
| 3660 | return ConstantExpr::getUIToFP(C, CI->getSrcTy(), true); |
| 3661 | |
| 3662 | if (isa<FPToSIInst>(CI)) |
| 3663 | return ConstantExpr::getSIToFP(C, CI->getSrcTy(), true); |
| 3664 | |
| 3665 | if (isa<UIToFPInst>(CI)) |
| 3666 | return ConstantExpr::getFPToUI(C, CI->getSrcTy(), true); |
| 3667 | |
| 3668 | if (isa<SIToFPInst>(CI)) |
| 3669 | return ConstantExpr::getFPToSI(C, CI->getSrcTy(), true); |
| 3670 | |
| 3671 | if (isa<FPTruncInst>(CI)) |
| 3672 | return ConstantExpr::getFPExtend(C, CI->getSrcTy(), true); |
| 3673 | |
| 3674 | if (isa<FPExtInst>(CI)) |
| 3675 | return ConstantExpr::getFPTrunc(C, CI->getSrcTy(), true); |
| 3676 | |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 3677 | return nullptr; |
| 3678 | } |
| 3679 | |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3680 | SelectPatternResult llvm::matchSelectPattern(Value *V, |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 3681 | Value *&LHS, Value *&RHS, |
| 3682 | Instruction::CastOps *CastOp) { |
| 3683 | SelectInst *SI = dyn_cast<SelectInst>(V); |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3684 | if (!SI) return {SPF_UNKNOWN, SPNB_NA, false}; |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 3685 | |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3686 | CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition()); |
| 3687 | if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false}; |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 3688 | |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3689 | CmpInst::Predicate Pred = CmpI->getPredicate(); |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 3690 | Value *CmpLHS = CmpI->getOperand(0); |
| 3691 | Value *CmpRHS = CmpI->getOperand(1); |
| 3692 | Value *TrueVal = SI->getTrueValue(); |
| 3693 | Value *FalseVal = SI->getFalseValue(); |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3694 | FastMathFlags FMF; |
| 3695 | if (isa<FPMathOperator>(CmpI)) |
| 3696 | FMF = CmpI->getFastMathFlags(); |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 3697 | |
| 3698 | // Bail out early. |
| 3699 | if (CmpI->isEquality()) |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3700 | return {SPF_UNKNOWN, SPNB_NA, false}; |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 3701 | |
| 3702 | // Deal with type mismatches. |
| 3703 | if (CastOp && CmpLHS->getType() != TrueVal->getType()) { |
| 3704 | if (Constant *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3705 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 3706 | cast<CastInst>(TrueVal)->getOperand(0), C, |
| 3707 | LHS, RHS); |
| 3708 | if (Constant *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3709 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 3710 | C, cast<CastInst>(FalseVal)->getOperand(0), |
| 3711 | LHS, RHS); |
| 3712 | } |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 3713 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal, |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 3714 | LHS, RHS); |
| 3715 | } |