blob: a6b975b7b0d89d9ac931f10409500269e798ae17 [file] [log] [blame]
Chris Lattner965c7692008-06-02 01:18:21 +00001//===- 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 Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/Analysis/AssumptionCache.h"
Dan Gohman949ab782010-12-15 20:10:26 +000018#include "llvm/Analysis/InstructionSimplify.h"
Benjamin Kramerfd4777c2013-09-24 16:37:51 +000019#include "llvm/Analysis/MemoryBuiltins.h"
Nick Lewyckyec373542014-05-20 05:13:21 +000020#include "llvm/IR/CallSite.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000021#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Constants.h"
23#include "llvm/IR/DataLayout.h"
Hal Finkel60db0582014-09-07 18:57:58 +000024#include "llvm/IR/Dominators.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000025#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/GlobalAlias.h"
27#include "llvm/IR/GlobalVariable.h"
28#include "llvm/IR/Instructions.h"
29#include "llvm/IR/IntrinsicInst.h"
30#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Metadata.h"
32#include "llvm/IR/Operator.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000033#include "llvm/IR/PatternMatch.h"
Matt Arsenaultf1a7e622014-07-15 01:55:03 +000034#include "llvm/Support/Debug.h"
Chris Lattner965c7692008-06-02 01:18:21 +000035#include "llvm/Support/MathExtras.h"
Chris Lattner64496902008-06-04 04:46:14 +000036#include <cstring>
Chris Lattner965c7692008-06-02 01:18:21 +000037using namespace llvm;
Duncan Sandsd3951082011-01-25 09:38:29 +000038using namespace llvm::PatternMatch;
39
40const unsigned MaxDepth = 6;
41
Philip Reames1c292272015-03-10 22:43:20 +000042/// Enable an experimental feature to leverage information about dominating
43/// conditions to compute known bits. The individual options below control how
44/// hard we search. The defaults are choosen to be fairly aggressive. If you
45/// run into compile time problems when testing, scale them back and report
46/// your findings.
47static cl::opt<bool> EnableDomConditions("value-tracking-dom-conditions",
48 cl::Hidden, cl::init(false));
49
50// This is expensive, so we only do it for the top level query value.
51// (TODO: evaluate cost vs profit, consider higher thresholds)
52static cl::opt<unsigned> DomConditionsMaxDepth("dom-conditions-max-depth",
53 cl::Hidden, cl::init(1));
54
55/// How many dominating blocks should be scanned looking for dominating
56/// conditions?
57static cl::opt<unsigned> DomConditionsMaxDomBlocks("dom-conditions-dom-blocks",
58 cl::Hidden,
59 cl::init(20000));
60
61// Controls the number of uses of the value searched for possible
62// dominating comparisons.
63static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
64 cl::Hidden, cl::init(2000));
65
66// If true, don't consider only compares whose only use is a branch.
67static cl::opt<bool> DomConditionsSingleCmpUse("dom-conditions-single-cmp-use",
68 cl::Hidden, cl::init(false));
69
Sanjay Patelaee84212014-11-04 16:27:42 +000070/// Returns the bitwidth of the given scalar or pointer type (if unknown returns
71/// 0). For vector types, returns the element type's bitwidth.
Mehdi Aminia28d91d2015-03-10 02:37:25 +000072static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
Duncan Sandsd3951082011-01-25 09:38:29 +000073 if (unsigned BitWidth = Ty->getScalarSizeInBits())
74 return BitWidth;
Matt Arsenaultf55e5e72013-08-10 17:34:08 +000075
Mehdi Aminia28d91d2015-03-10 02:37:25 +000076 return DL.getPointerTypeSizeInBits(Ty);
Duncan Sandsd3951082011-01-25 09:38:29 +000077}
Chris Lattner965c7692008-06-02 01:18:21 +000078
Hal Finkel60db0582014-09-07 18:57:58 +000079// Many of these functions have internal versions that take an assumption
80// exclusion set. This is because of the potential for mutual recursion to
81// cause computeKnownBits to repeatedly visit the same assume intrinsic. The
82// classic case of this is assume(x = y), which will attempt to determine
83// bits in x from bits in y, which will attempt to determine bits in y from
84// bits in x, etc. Regarding the mutual recursion, computeKnownBits can call
85// isKnownNonZero, which calls computeKnownBits and ComputeSignBit and
86// isKnownToBeAPowerOfTwo (all of which can call computeKnownBits), and so on.
87typedef SmallPtrSet<const Value *, 8> ExclInvsSet;
88
Benjamin Kramercfd8d902014-09-12 08:56:53 +000089namespace {
Hal Finkel60db0582014-09-07 18:57:58 +000090// Simplifying using an assume can only be done in a particular control-flow
91// context (the context instruction provides that context). If an assume and
92// the context instruction are not in the same block then the DT helps in
93// figuring out if we can use it.
94struct Query {
95 ExclInvsSet ExclInvs;
Chandler Carruth66b31302015-01-04 12:03:27 +000096 AssumptionCache *AC;
Hal Finkel60db0582014-09-07 18:57:58 +000097 const Instruction *CxtI;
98 const DominatorTree *DT;
99
Chandler Carruth66b31302015-01-04 12:03:27 +0000100 Query(AssumptionCache *AC = nullptr, const Instruction *CxtI = nullptr,
Hal Finkel60db0582014-09-07 18:57:58 +0000101 const DominatorTree *DT = nullptr)
Chandler Carruth66b31302015-01-04 12:03:27 +0000102 : AC(AC), CxtI(CxtI), DT(DT) {}
Hal Finkel60db0582014-09-07 18:57:58 +0000103
104 Query(const Query &Q, const Value *NewExcl)
Chandler Carruth66b31302015-01-04 12:03:27 +0000105 : ExclInvs(Q.ExclInvs), AC(Q.AC), CxtI(Q.CxtI), DT(Q.DT) {
Hal Finkel60db0582014-09-07 18:57:58 +0000106 ExclInvs.insert(NewExcl);
107 }
108};
Benjamin Kramercfd8d902014-09-12 08:56:53 +0000109} // end anonymous namespace
Hal Finkel60db0582014-09-07 18:57:58 +0000110
Sanjay Patel547e9752014-11-04 16:09:50 +0000111// Given the provided Value and, potentially, a context instruction, return
Hal Finkel60db0582014-09-07 18:57:58 +0000112// the preferred context instruction (if any).
113static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
114 // If we've been provided with a context instruction, then use that (provided
115 // it has been inserted).
116 if (CxtI && CxtI->getParent())
117 return CxtI;
118
119 // If the value is really an already-inserted instruction, then use that.
120 CxtI = dyn_cast<Instruction>(V);
121 if (CxtI && CxtI->getParent())
122 return CxtI;
123
124 return nullptr;
125}
126
127static void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000128 const DataLayout &DL, unsigned Depth,
129 const Query &Q);
Hal Finkel60db0582014-09-07 18:57:58 +0000130
131void llvm::computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000132 const DataLayout &DL, unsigned Depth,
Chandler Carruth66b31302015-01-04 12:03:27 +0000133 AssumptionCache *AC, const Instruction *CxtI,
Hal Finkel60db0582014-09-07 18:57:58 +0000134 const DominatorTree *DT) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000135 ::computeKnownBits(V, KnownZero, KnownOne, DL, Depth,
Chandler Carruth66b31302015-01-04 12:03:27 +0000136 Query(AC, safeCxtI(V, CxtI), DT));
Hal Finkel60db0582014-09-07 18:57:58 +0000137}
138
139static void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000140 const DataLayout &DL, unsigned Depth,
141 const Query &Q);
Hal Finkel60db0582014-09-07 18:57:58 +0000142
143void llvm::ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000144 const DataLayout &DL, unsigned Depth,
Chandler Carruth66b31302015-01-04 12:03:27 +0000145 AssumptionCache *AC, const Instruction *CxtI,
Hal Finkel60db0582014-09-07 18:57:58 +0000146 const DominatorTree *DT) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000147 ::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth,
Chandler Carruth66b31302015-01-04 12:03:27 +0000148 Query(AC, safeCxtI(V, CxtI), DT));
Hal Finkel60db0582014-09-07 18:57:58 +0000149}
150
151static bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero, unsigned Depth,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000152 const Query &Q, const DataLayout &DL);
Hal Finkel60db0582014-09-07 18:57:58 +0000153
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000154bool llvm::isKnownToBeAPowerOfTwo(Value *V, const DataLayout &DL, bool OrZero,
Chandler Carruth66b31302015-01-04 12:03:27 +0000155 unsigned Depth, AssumptionCache *AC,
Hal Finkel60db0582014-09-07 18:57:58 +0000156 const Instruction *CxtI,
157 const DominatorTree *DT) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000158 return ::isKnownToBeAPowerOfTwo(V, OrZero, Depth,
159 Query(AC, safeCxtI(V, CxtI), DT), DL);
160}
161
162static bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth,
163 const Query &Q);
164
165bool llvm::isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth,
166 AssumptionCache *AC, const Instruction *CxtI,
167 const DominatorTree *DT) {
168 return ::isKnownNonZero(V, DL, Depth, Query(AC, safeCxtI(V, CxtI), DT));
169}
170
171static bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
172 unsigned Depth, const Query &Q);
173
174bool llvm::MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
175 unsigned Depth, AssumptionCache *AC,
176 const Instruction *CxtI, const DominatorTree *DT) {
177 return ::MaskedValueIsZero(V, Mask, DL, Depth,
178 Query(AC, safeCxtI(V, CxtI), DT));
179}
180
181static unsigned ComputeNumSignBits(Value *V, const DataLayout &DL,
182 unsigned Depth, const Query &Q);
183
184unsigned llvm::ComputeNumSignBits(Value *V, const DataLayout &DL,
185 unsigned Depth, AssumptionCache *AC,
186 const Instruction *CxtI,
187 const DominatorTree *DT) {
188 return ::ComputeNumSignBits(V, DL, Depth, Query(AC, safeCxtI(V, CxtI), DT));
Hal Finkel60db0582014-09-07 18:57:58 +0000189}
190
Jay Foada0653a32014-05-14 21:14:37 +0000191static void computeKnownBitsAddSub(bool Add, Value *Op0, Value *Op1, bool NSW,
192 APInt &KnownZero, APInt &KnownOne,
193 APInt &KnownZero2, APInt &KnownOne2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000194 const DataLayout &DL, unsigned Depth,
Hal Finkel60db0582014-09-07 18:57:58 +0000195 const Query &Q) {
196 if (!Add) {
197 if (ConstantInt *CLHS = dyn_cast<ConstantInt>(Op0)) {
198 // We know that the top bits of C-X are clear if X contains less bits
199 // than C (i.e. no wrap-around can happen). For example, 20-X is
200 // positive if we can prove that X is >= 0 and < 16.
201 if (!CLHS->getValue().isNegative()) {
202 unsigned BitWidth = KnownZero.getBitWidth();
203 unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros();
204 // NLZ can't be BitWidth with no sign bit
205 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000206 computeKnownBits(Op1, KnownZero2, KnownOne2, DL, Depth + 1, Q);
Hal Finkel60db0582014-09-07 18:57:58 +0000207
208 // If all of the MaskV bits are known to be zero, then we know the
209 // output top bits are zero, because we now know that the output is
210 // from [0-C].
211 if ((KnownZero2 & MaskV) == MaskV) {
212 unsigned NLZ2 = CLHS->getValue().countLeadingZeros();
213 // Top bits known zero.
214 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
215 }
216 }
217 }
218 }
219
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000220 unsigned BitWidth = KnownZero.getBitWidth();
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000221
David Majnemer97ddca32014-08-22 00:40:43 +0000222 // If an initial sequence of bits in the result is not needed, the
223 // corresponding bits in the operands are not needed.
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000224 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000225 computeKnownBits(Op0, LHSKnownZero, LHSKnownOne, DL, Depth + 1, Q);
226 computeKnownBits(Op1, KnownZero2, KnownOne2, DL, Depth + 1, Q);
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000227
David Majnemer97ddca32014-08-22 00:40:43 +0000228 // Carry in a 1 for a subtract, rather than a 0.
229 APInt CarryIn(BitWidth, 0);
230 if (!Add) {
231 // Sum = LHS + ~RHS + 1
232 std::swap(KnownZero2, KnownOne2);
233 CarryIn.setBit(0);
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000234 }
235
David Majnemer97ddca32014-08-22 00:40:43 +0000236 APInt PossibleSumZero = ~LHSKnownZero + ~KnownZero2 + CarryIn;
237 APInt PossibleSumOne = LHSKnownOne + KnownOne2 + CarryIn;
238
239 // Compute known bits of the carry.
240 APInt CarryKnownZero = ~(PossibleSumZero ^ LHSKnownZero ^ KnownZero2);
241 APInt CarryKnownOne = PossibleSumOne ^ LHSKnownOne ^ KnownOne2;
242
243 // Compute set of known bits (where all three relevant bits are known).
244 APInt LHSKnown = LHSKnownZero | LHSKnownOne;
245 APInt RHSKnown = KnownZero2 | KnownOne2;
246 APInt CarryKnown = CarryKnownZero | CarryKnownOne;
247 APInt Known = LHSKnown & RHSKnown & CarryKnown;
248
249 assert((PossibleSumZero & Known) == (PossibleSumOne & Known) &&
250 "known bits of sum differ");
251
252 // Compute known bits of the result.
253 KnownZero = ~PossibleSumOne & Known;
254 KnownOne = PossibleSumOne & Known;
255
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000256 // Are we still trying to solve for the sign bit?
David Majnemer97ddca32014-08-22 00:40:43 +0000257 if (!Known.isNegative()) {
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000258 if (NSW) {
David Majnemer97ddca32014-08-22 00:40:43 +0000259 // Adding two non-negative numbers, or subtracting a negative number from
260 // a non-negative one, can't wrap into negative.
261 if (LHSKnownZero.isNegative() && KnownZero2.isNegative())
262 KnownZero |= APInt::getSignBit(BitWidth);
263 // Adding two negative numbers, or subtracting a non-negative number from
264 // a negative one, can't wrap into non-negative.
265 else if (LHSKnownOne.isNegative() && KnownOne2.isNegative())
266 KnownOne |= APInt::getSignBit(BitWidth);
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000267 }
268 }
269}
270
Jay Foada0653a32014-05-14 21:14:37 +0000271static void computeKnownBitsMul(Value *Op0, Value *Op1, bool NSW,
272 APInt &KnownZero, APInt &KnownOne,
273 APInt &KnownZero2, APInt &KnownOne2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000274 const DataLayout &DL, unsigned Depth,
Hal Finkel60db0582014-09-07 18:57:58 +0000275 const Query &Q) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000276 unsigned BitWidth = KnownZero.getBitWidth();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000277 computeKnownBits(Op1, KnownZero, KnownOne, DL, Depth + 1, Q);
278 computeKnownBits(Op0, KnownZero2, KnownOne2, DL, Depth + 1, Q);
Nick Lewyckyfa306072012-03-18 23:28:48 +0000279
280 bool isKnownNegative = false;
281 bool isKnownNonNegative = false;
282 // If the multiplication is known not to overflow, compute the sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000283 if (NSW) {
Nick Lewyckyfa306072012-03-18 23:28:48 +0000284 if (Op0 == Op1) {
285 // The product of a number with itself is non-negative.
286 isKnownNonNegative = true;
287 } else {
288 bool isKnownNonNegativeOp1 = KnownZero.isNegative();
289 bool isKnownNonNegativeOp0 = KnownZero2.isNegative();
290 bool isKnownNegativeOp1 = KnownOne.isNegative();
291 bool isKnownNegativeOp0 = KnownOne2.isNegative();
292 // The product of two numbers with the same sign is non-negative.
293 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
294 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
295 // The product of a negative number and a non-negative number is either
296 // negative or zero.
297 if (!isKnownNonNegative)
298 isKnownNegative = (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000299 isKnownNonZero(Op0, DL, Depth, Q)) ||
Nick Lewyckyfa306072012-03-18 23:28:48 +0000300 (isKnownNegativeOp0 && isKnownNonNegativeOp1 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000301 isKnownNonZero(Op1, DL, Depth, Q));
Nick Lewyckyfa306072012-03-18 23:28:48 +0000302 }
303 }
304
305 // If low bits are zero in either operand, output low known-0 bits.
306 // Also compute a conserative estimate for high known-0 bits.
307 // More trickiness is possible, but this is sufficient for the
308 // interesting case of alignment computation.
309 KnownOne.clearAllBits();
310 unsigned TrailZ = KnownZero.countTrailingOnes() +
311 KnownZero2.countTrailingOnes();
312 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
313 KnownZero2.countLeadingOnes(),
314 BitWidth) - BitWidth;
315
316 TrailZ = std::min(TrailZ, BitWidth);
317 LeadZ = std::min(LeadZ, BitWidth);
318 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
319 APInt::getHighBitsSet(BitWidth, LeadZ);
Nick Lewyckyfa306072012-03-18 23:28:48 +0000320
321 // Only make use of no-wrap flags if we failed to compute the sign bit
322 // directly. This matters if the multiplication always overflows, in
323 // which case we prefer to follow the result of the direct computation,
324 // though as the program is invoking undefined behaviour we can choose
325 // whatever we like here.
326 if (isKnownNonNegative && !KnownOne.isNegative())
327 KnownZero.setBit(BitWidth - 1);
328 else if (isKnownNegative && !KnownZero.isNegative())
329 KnownOne.setBit(BitWidth - 1);
330}
331
Jingyue Wu37fcb592014-06-19 16:50:16 +0000332void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
333 APInt &KnownZero) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000334 unsigned BitWidth = KnownZero.getBitWidth();
Rafael Espindola53190532012-03-30 15:52:11 +0000335 unsigned NumRanges = Ranges.getNumOperands() / 2;
336 assert(NumRanges >= 1);
337
338 // Use the high end of the ranges to find leading zeros.
339 unsigned MinLeadingZeros = BitWidth;
340 for (unsigned i = 0; i < NumRanges; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000341 ConstantInt *Lower =
342 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
343 ConstantInt *Upper =
344 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
Rafael Espindola53190532012-03-30 15:52:11 +0000345 ConstantRange Range(Lower->getValue(), Upper->getValue());
346 if (Range.isWrappedSet())
347 MinLeadingZeros = 0; // -1 has no zeros
348 unsigned LeadingZeros = (Upper->getValue() - 1).countLeadingZeros();
349 MinLeadingZeros = std::min(LeadingZeros, MinLeadingZeros);
350 }
351
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000352 KnownZero = APInt::getHighBitsSet(BitWidth, MinLeadingZeros);
Rafael Espindola53190532012-03-30 15:52:11 +0000353}
Jay Foad5a29c362014-05-15 12:12:55 +0000354
Hal Finkel60db0582014-09-07 18:57:58 +0000355static bool isEphemeralValueOf(Instruction *I, const Value *E) {
356 SmallVector<const Value *, 16> WorkSet(1, I);
357 SmallPtrSet<const Value *, 32> Visited;
358 SmallPtrSet<const Value *, 16> EphValues;
359
360 while (!WorkSet.empty()) {
361 const Value *V = WorkSet.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +0000362 if (!Visited.insert(V).second)
Hal Finkel60db0582014-09-07 18:57:58 +0000363 continue;
364
365 // If all uses of this value are ephemeral, then so is this value.
366 bool FoundNEUse = false;
367 for (const User *I : V->users())
368 if (!EphValues.count(I)) {
369 FoundNEUse = true;
370 break;
371 }
372
373 if (!FoundNEUse) {
374 if (V == E)
375 return true;
376
377 EphValues.insert(V);
378 if (const User *U = dyn_cast<User>(V))
379 for (User::const_op_iterator J = U->op_begin(), JE = U->op_end();
380 J != JE; ++J) {
381 if (isSafeToSpeculativelyExecute(*J))
382 WorkSet.push_back(*J);
383 }
384 }
385 }
386
387 return false;
388}
389
390// Is this an intrinsic that cannot be speculated but also cannot trap?
391static bool isAssumeLikeIntrinsic(const Instruction *I) {
392 if (const CallInst *CI = dyn_cast<CallInst>(I))
393 if (Function *F = CI->getCalledFunction())
394 switch (F->getIntrinsicID()) {
395 default: break;
396 // FIXME: This list is repeated from NoTTI::getIntrinsicCost.
397 case Intrinsic::assume:
398 case Intrinsic::dbg_declare:
399 case Intrinsic::dbg_value:
400 case Intrinsic::invariant_start:
401 case Intrinsic::invariant_end:
402 case Intrinsic::lifetime_start:
403 case Intrinsic::lifetime_end:
404 case Intrinsic::objectsize:
405 case Intrinsic::ptr_annotation:
406 case Intrinsic::var_annotation:
407 return true;
408 }
409
410 return false;
411}
412
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000413static bool isValidAssumeForContext(Value *V, const Query &Q) {
Hal Finkel60db0582014-09-07 18:57:58 +0000414 Instruction *Inv = cast<Instruction>(V);
415
416 // There are two restrictions on the use of an assume:
417 // 1. The assume must dominate the context (or the control flow must
418 // reach the assume whenever it reaches the context).
419 // 2. The context must not be in the assume's set of ephemeral values
420 // (otherwise we will use the assume to prove that the condition
421 // feeding the assume is trivially true, thus causing the removal of
422 // the assume).
423
424 if (Q.DT) {
425 if (Q.DT->dominates(Inv, Q.CxtI)) {
426 return true;
427 } else if (Inv->getParent() == Q.CxtI->getParent()) {
428 // The context comes first, but they're both in the same block. Make sure
429 // there is nothing in between that might interrupt the control flow.
430 for (BasicBlock::const_iterator I =
431 std::next(BasicBlock::const_iterator(Q.CxtI)),
432 IE(Inv); I != IE; ++I)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000433 if (!isSafeToSpeculativelyExecute(I) && !isAssumeLikeIntrinsic(I))
Hal Finkel60db0582014-09-07 18:57:58 +0000434 return false;
435
436 return !isEphemeralValueOf(Inv, Q.CxtI);
437 }
438
439 return false;
440 }
441
442 // When we don't have a DT, we do a limited search...
443 if (Inv->getParent() == Q.CxtI->getParent()->getSinglePredecessor()) {
444 return true;
445 } else if (Inv->getParent() == Q.CxtI->getParent()) {
446 // Search forward from the assume until we reach the context (or the end
447 // of the block); the common case is that the assume will come first.
448 for (BasicBlock::iterator I = std::next(BasicBlock::iterator(Inv)),
449 IE = Inv->getParent()->end(); I != IE; ++I)
450 if (I == Q.CxtI)
451 return true;
452
453 // The context must come first...
454 for (BasicBlock::const_iterator I =
455 std::next(BasicBlock::const_iterator(Q.CxtI)),
456 IE(Inv); I != IE; ++I)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000457 if (!isSafeToSpeculativelyExecute(I) && !isAssumeLikeIntrinsic(I))
Hal Finkel60db0582014-09-07 18:57:58 +0000458 return false;
459
460 return !isEphemeralValueOf(Inv, Q.CxtI);
461 }
462
463 return false;
464}
465
466bool llvm::isValidAssumeForContext(const Instruction *I,
467 const Instruction *CxtI,
Hal Finkel60db0582014-09-07 18:57:58 +0000468 const DominatorTree *DT) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000469 return ::isValidAssumeForContext(const_cast<Instruction *>(I),
470 Query(nullptr, CxtI, DT));
Hal Finkel60db0582014-09-07 18:57:58 +0000471}
472
473template<typename LHS, typename RHS>
474inline match_combine_or<CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>,
475 CmpClass_match<RHS, LHS, ICmpInst, ICmpInst::Predicate>>
476m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
477 return m_CombineOr(m_ICmp(Pred, L, R), m_ICmp(Pred, R, L));
478}
479
480template<typename LHS, typename RHS>
481inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::And>,
482 BinaryOp_match<RHS, LHS, Instruction::And>>
483m_c_And(const LHS &L, const RHS &R) {
484 return m_CombineOr(m_And(L, R), m_And(R, L));
485}
486
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000487template<typename LHS, typename RHS>
488inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::Or>,
489 BinaryOp_match<RHS, LHS, Instruction::Or>>
490m_c_Or(const LHS &L, const RHS &R) {
491 return m_CombineOr(m_Or(L, R), m_Or(R, L));
492}
493
494template<typename LHS, typename RHS>
495inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::Xor>,
496 BinaryOp_match<RHS, LHS, Instruction::Xor>>
497m_c_Xor(const LHS &L, const RHS &R) {
498 return m_CombineOr(m_Xor(L, R), m_Xor(R, L));
499}
500
Philip Reames1c292272015-03-10 22:43:20 +0000501/// Compute known bits in 'V' under the assumption that the condition 'Cmp' is
502/// true (at the context instruction.) This is mostly a utility function for
503/// the prototype dominating conditions reasoning below.
504static void computeKnownBitsFromTrueCondition(Value *V, ICmpInst *Cmp,
505 APInt &KnownZero,
506 APInt &KnownOne,
507 const DataLayout &DL,
508 unsigned Depth, const Query &Q) {
509 Value *LHS = Cmp->getOperand(0);
510 Value *RHS = Cmp->getOperand(1);
511 // TODO: We could potentially be more aggressive here. This would be worth
512 // evaluating. If we can, explore commoning this code with the assume
513 // handling logic.
514 if (LHS != V && RHS != V)
515 return;
516
517 const unsigned BitWidth = KnownZero.getBitWidth();
518
519 switch (Cmp->getPredicate()) {
520 default:
521 // We know nothing from this condition
522 break;
523 // TODO: implement unsigned bound from below (known one bits)
524 // TODO: common condition check implementations with assumes
525 // TODO: implement other patterns from assume (e.g. V & B == A)
526 case ICmpInst::ICMP_SGT:
527 if (LHS == V) {
528 APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0);
529 computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
530 if (KnownOneTemp.isAllOnesValue() || KnownZeroTemp.isNegative()) {
531 // We know that the sign bit is zero.
532 KnownZero |= APInt::getSignBit(BitWidth);
533 }
534 }
535 break;
536 case ICmpInst::ICMP_EQ:
537 if (LHS == V)
538 computeKnownBits(RHS, KnownZero, KnownOne, DL, Depth + 1, Q);
539 else if (RHS == V)
540 computeKnownBits(LHS, KnownZero, KnownOne, DL, Depth + 1, Q);
541 else
542 llvm_unreachable("missing use?");
543 break;
544 case ICmpInst::ICMP_ULE:
545 if (LHS == V) {
546 APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0);
547 computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
548 // The known zero bits carry over
549 unsigned SignBits = KnownZeroTemp.countLeadingOnes();
550 KnownZero |= APInt::getHighBitsSet(BitWidth, SignBits);
551 }
552 break;
553 case ICmpInst::ICMP_ULT:
554 if (LHS == V) {
555 APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0);
556 computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
557 // Whatever high bits in rhs are zero are known to be zero (if rhs is a
558 // power of 2, then one more).
559 unsigned SignBits = KnownZeroTemp.countLeadingOnes();
560 if (isKnownToBeAPowerOfTwo(RHS, false, Depth + 1, Query(Q, Cmp), DL))
561 SignBits++;
562 KnownZero |= APInt::getHighBitsSet(BitWidth, SignBits);
563 }
564 break;
565 };
566}
567
568/// Compute known bits in 'V' from conditions which are known to be true along
569/// all paths leading to the context instruction. In particular, look for
570/// cases where one branch of an interesting condition dominates the context
571/// instruction. This does not do general dataflow.
572/// NOTE: This code is EXPERIMENTAL and currently off by default.
573static void computeKnownBitsFromDominatingCondition(Value *V, APInt &KnownZero,
574 APInt &KnownOne,
575 const DataLayout &DL,
576 unsigned Depth,
577 const Query &Q) {
578 // Need both the dominator tree and the query location to do anything useful
579 if (!Q.DT || !Q.CxtI)
580 return;
581 Instruction *Cxt = const_cast<Instruction *>(Q.CxtI);
582
583 // Avoid useless work
584 if (auto VI = dyn_cast<Instruction>(V))
585 if (VI->getParent() == Cxt->getParent())
586 return;
587
588 // Note: We currently implement two options. It's not clear which of these
589 // will survive long term, we need data for that.
590 // Option 1 - Try walking the dominator tree looking for conditions which
591 // might apply. This works well for local conditions (loop guards, etc..),
592 // but not as well for things far from the context instruction (presuming a
593 // low max blocks explored). If we can set an high enough limit, this would
594 // be all we need.
595 // Option 2 - We restrict out search to those conditions which are uses of
596 // the value we're interested in. This is independent of dom structure,
597 // but is slightly less powerful without looking through lots of use chains.
598 // It does handle conditions far from the context instruction (e.g. early
599 // function exits on entry) really well though.
600
601 // Option 1 - Search the dom tree
602 unsigned NumBlocksExplored = 0;
603 BasicBlock *Current = Cxt->getParent();
604 while (true) {
605 // Stop searching if we've gone too far up the chain
606 if (NumBlocksExplored >= DomConditionsMaxDomBlocks)
607 break;
608 NumBlocksExplored++;
609
610 if (!Q.DT->getNode(Current)->getIDom())
611 break;
612 Current = Q.DT->getNode(Current)->getIDom()->getBlock();
613 if (!Current)
614 // found function entry
615 break;
616
617 BranchInst *BI = dyn_cast<BranchInst>(Current->getTerminator());
618 if (!BI || BI->isUnconditional())
619 continue;
620 ICmpInst *Cmp = dyn_cast<ICmpInst>(BI->getCondition());
621 if (!Cmp)
622 continue;
623
624 // We're looking for conditions that are guaranteed to hold at the context
625 // instruction. Finding a condition where one path dominates the context
626 // isn't enough because both the true and false cases could merge before
627 // the context instruction we're actually interested in. Instead, we need
628 // to ensure that the taken *edge* dominates the context instruction.
629 BasicBlock *BB0 = BI->getSuccessor(0);
630 BasicBlockEdge Edge(BI->getParent(), BB0);
631 if (!Edge.isSingleEdge() || !Q.DT->dominates(Edge, Q.CxtI->getParent()))
632 continue;
633
634 computeKnownBitsFromTrueCondition(V, Cmp, KnownZero, KnownOne, DL, Depth,
635 Q);
636 }
637
638 // Option 2 - Search the other uses of V
639 unsigned NumUsesExplored = 0;
640 for (auto U : V->users()) {
641 // Avoid massive lists
642 if (NumUsesExplored >= DomConditionsMaxUses)
643 break;
644 NumUsesExplored++;
645 // Consider only compare instructions uniquely controlling a branch
646 ICmpInst *Cmp = dyn_cast<ICmpInst>(U);
647 if (!Cmp)
648 continue;
649
650 if (DomConditionsSingleCmpUse && !Cmp->hasOneUse())
651 continue;
652
653 for (auto *CmpU : Cmp->users()) {
654 BranchInst *BI = dyn_cast<BranchInst>(CmpU);
655 if (!BI || BI->isUnconditional())
656 continue;
657 // We're looking for conditions that are guaranteed to hold at the
658 // context instruction. Finding a condition where one path dominates
659 // the context isn't enough because both the true and false cases could
660 // merge before the context instruction we're actually interested in.
661 // Instead, we need to ensure that the taken *edge* dominates the context
662 // instruction.
663 BasicBlock *BB0 = BI->getSuccessor(0);
664 BasicBlockEdge Edge(BI->getParent(), BB0);
665 if (!Edge.isSingleEdge() || !Q.DT->dominates(Edge, Q.CxtI->getParent()))
666 continue;
667
668 computeKnownBitsFromTrueCondition(V, Cmp, KnownZero, KnownOne, DL, Depth,
669 Q);
670 }
671 }
672}
673
Hal Finkel60db0582014-09-07 18:57:58 +0000674static void computeKnownBitsFromAssume(Value *V, APInt &KnownZero,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000675 APInt &KnownOne, const DataLayout &DL,
Hal Finkel60db0582014-09-07 18:57:58 +0000676 unsigned Depth, const Query &Q) {
677 // Use of assumptions is context-sensitive. If we don't have a context, we
678 // cannot use them!
Chandler Carruth66b31302015-01-04 12:03:27 +0000679 if (!Q.AC || !Q.CxtI)
Hal Finkel60db0582014-09-07 18:57:58 +0000680 return;
681
682 unsigned BitWidth = KnownZero.getBitWidth();
683
Chandler Carruth66b31302015-01-04 12:03:27 +0000684 for (auto &AssumeVH : Q.AC->assumptions()) {
685 if (!AssumeVH)
686 continue;
687 CallInst *I = cast<CallInst>(AssumeVH);
Chandler Carruth75c11b82015-01-04 23:13:57 +0000688 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
Chandler Carruth66b31302015-01-04 12:03:27 +0000689 "Got assumption for the wrong function!");
Hal Finkel60db0582014-09-07 18:57:58 +0000690 if (Q.ExclInvs.count(I))
691 continue;
692
Philip Reames00d3b272014-11-24 23:44:28 +0000693 // Warning: This loop can end up being somewhat performance sensetive.
694 // We're running this loop for once for each value queried resulting in a
695 // runtime of ~O(#assumes * #values).
696
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000697 assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
Philip Reames00d3b272014-11-24 23:44:28 +0000698 "must be an assume intrinsic");
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000699
Philip Reames00d3b272014-11-24 23:44:28 +0000700 Value *Arg = I->getArgOperand(0);
701
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000702 if (Arg == V && isValidAssumeForContext(I, Q)) {
Hal Finkel60db0582014-09-07 18:57:58 +0000703 assert(BitWidth == 1 && "assume operand is not i1?");
704 KnownZero.clearAllBits();
705 KnownOne.setAllBits();
706 return;
707 }
708
David Majnemer9b609752014-12-12 23:59:29 +0000709 // The remaining tests are all recursive, so bail out if we hit the limit.
710 if (Depth == MaxDepth)
711 continue;
712
Hal Finkel60db0582014-09-07 18:57:58 +0000713 Value *A, *B;
714 auto m_V = m_CombineOr(m_Specific(V),
715 m_CombineOr(m_PtrToInt(m_Specific(V)),
716 m_BitCast(m_Specific(V))));
717
718 CmpInst::Predicate Pred;
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000719 ConstantInt *C;
Hal Finkel60db0582014-09-07 18:57:58 +0000720 // assume(v = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000721 if (match(Arg, m_c_ICmp(Pred, m_V, m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000722 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel60db0582014-09-07 18:57:58 +0000723 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
724 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
725 KnownZero |= RHSKnownZero;
726 KnownOne |= RHSKnownOne;
727 // assume(v & b = a)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000728 } else if (match(Arg,
729 m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A))) &&
730 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel60db0582014-09-07 18:57:58 +0000731 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
732 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
733 APInt MaskKnownZero(BitWidth, 0), MaskKnownOne(BitWidth, 0);
734 computeKnownBits(B, MaskKnownZero, MaskKnownOne, DL, Depth+1, Query(Q, I));
735
736 // For those bits in the mask that are known to be one, we can propagate
737 // known bits from the RHS to V.
738 KnownZero |= RHSKnownZero & MaskKnownOne;
739 KnownOne |= RHSKnownOne & MaskKnownOne;
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000740 // assume(~(v & b) = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000741 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))),
742 m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000743 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000744 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
745 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
746 APInt MaskKnownZero(BitWidth, 0), MaskKnownOne(BitWidth, 0);
747 computeKnownBits(B, MaskKnownZero, MaskKnownOne, DL, Depth+1, Query(Q, I));
748
749 // For those bits in the mask that are known to be one, we can propagate
750 // inverted known bits from the RHS to V.
751 KnownZero |= RHSKnownOne & MaskKnownOne;
752 KnownOne |= RHSKnownZero & MaskKnownOne;
753 // assume(v | b = a)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000754 } else if (match(Arg,
755 m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A))) &&
756 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000757 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
758 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
759 APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
760 computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
761
762 // For those bits in B that are known to be zero, we can propagate known
763 // bits from the RHS to V.
764 KnownZero |= RHSKnownZero & BKnownZero;
765 KnownOne |= RHSKnownOne & BKnownZero;
766 // assume(~(v | b) = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000767 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))),
768 m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000769 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000770 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
771 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
772 APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
773 computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
774
775 // For those bits in B that are known to be zero, we can propagate
776 // inverted known bits from the RHS to V.
777 KnownZero |= RHSKnownOne & BKnownZero;
778 KnownOne |= RHSKnownZero & BKnownZero;
779 // assume(v ^ b = a)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000780 } else if (match(Arg,
781 m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A))) &&
782 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000783 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
784 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
785 APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
786 computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
787
788 // For those bits in B that are known to be zero, we can propagate known
789 // bits from the RHS to V. For those bits in B that are known to be one,
790 // we can propagate inverted known bits from the RHS to V.
791 KnownZero |= RHSKnownZero & BKnownZero;
792 KnownOne |= RHSKnownOne & BKnownZero;
793 KnownZero |= RHSKnownOne & BKnownOne;
794 KnownOne |= RHSKnownZero & BKnownOne;
795 // assume(~(v ^ b) = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000796 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))),
797 m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000798 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000799 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
800 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
801 APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
802 computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
803
804 // For those bits in B that are known to be zero, we can propagate
805 // inverted known bits from the RHS to V. For those bits in B that are
806 // known to be one, we can propagate known bits from the RHS to V.
807 KnownZero |= RHSKnownOne & BKnownZero;
808 KnownOne |= RHSKnownZero & BKnownZero;
809 KnownZero |= RHSKnownZero & BKnownOne;
810 KnownOne |= RHSKnownOne & BKnownOne;
811 // assume(v << c = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000812 } else if (match(Arg, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)),
813 m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000814 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000815 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
816 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
817 // For those bits in RHS that are known, we can propagate them to known
818 // bits in V shifted to the right by C.
819 KnownZero |= RHSKnownZero.lshr(C->getZExtValue());
820 KnownOne |= RHSKnownOne.lshr(C->getZExtValue());
821 // assume(~(v << c) = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000822 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))),
823 m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000824 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000825 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
826 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
827 // For those bits in RHS that are known, we can propagate them inverted
828 // to known bits in V shifted to the right by C.
829 KnownZero |= RHSKnownOne.lshr(C->getZExtValue());
830 KnownOne |= RHSKnownZero.lshr(C->getZExtValue());
831 // assume(v >> c = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000832 } else if (match(Arg,
833 m_c_ICmp(Pred, m_CombineOr(m_LShr(m_V, m_ConstantInt(C)),
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000834 m_AShr(m_V, m_ConstantInt(C))),
835 m_Value(A))) &&
836 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000837 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 << C->getZExtValue();
842 KnownOne |= RHSKnownOne << C->getZExtValue();
843 // assume(~(v >> c) = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000844 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_CombineOr(
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000845 m_LShr(m_V, m_ConstantInt(C)),
846 m_AShr(m_V, m_ConstantInt(C)))),
Philip Reames00d3b272014-11-24 23:44:28 +0000847 m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000848 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000849 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
850 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
851 // For those bits in RHS that are known, we can propagate them inverted
852 // to known bits in V shifted to the right by C.
853 KnownZero |= RHSKnownOne << C->getZExtValue();
854 KnownOne |= RHSKnownZero << C->getZExtValue();
855 // assume(v >=_s c) where c is non-negative
Philip Reames00d3b272014-11-24 23:44:28 +0000856 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000857 Pred == ICmpInst::ICMP_SGE && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000858 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
859 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
860
861 if (RHSKnownZero.isNegative()) {
862 // We know that the sign bit is zero.
863 KnownZero |= APInt::getSignBit(BitWidth);
864 }
865 // assume(v >_s c) where c is at least -1.
Philip Reames00d3b272014-11-24 23:44:28 +0000866 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000867 Pred == ICmpInst::ICMP_SGT && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000868 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
869 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
870
871 if (RHSKnownOne.isAllOnesValue() || RHSKnownZero.isNegative()) {
872 // We know that the sign bit is zero.
873 KnownZero |= APInt::getSignBit(BitWidth);
874 }
875 // assume(v <=_s c) where c is negative
Philip Reames00d3b272014-11-24 23:44:28 +0000876 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000877 Pred == ICmpInst::ICMP_SLE && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000878 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
879 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
880
881 if (RHSKnownOne.isNegative()) {
882 // We know that the sign bit is one.
883 KnownOne |= APInt::getSignBit(BitWidth);
884 }
885 // assume(v <_s c) where c is non-positive
Philip Reames00d3b272014-11-24 23:44:28 +0000886 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000887 Pred == ICmpInst::ICMP_SLT && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000888 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
889 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
890
891 if (RHSKnownZero.isAllOnesValue() || RHSKnownOne.isNegative()) {
892 // We know that the sign bit is one.
893 KnownOne |= APInt::getSignBit(BitWidth);
894 }
895 // assume(v <=_u c)
Philip Reames00d3b272014-11-24 23:44:28 +0000896 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000897 Pred == ICmpInst::ICMP_ULE && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000898 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
899 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
900
901 // Whatever high bits in c are zero are known to be zero.
902 KnownZero |=
903 APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes());
904 // assume(v <_u c)
Philip Reames00d3b272014-11-24 23:44:28 +0000905 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000906 Pred == ICmpInst::ICMP_ULT && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000907 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
908 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
909
910 // Whatever high bits in c are zero are known to be zero (if c is a power
911 // of 2, then one more).
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000912 if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, Query(Q, I), DL))
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000913 KnownZero |=
914 APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes()+1);
915 else
916 KnownZero |=
917 APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes());
Hal Finkel60db0582014-09-07 18:57:58 +0000918 }
919 }
920}
921
Jay Foada0653a32014-05-14 21:14:37 +0000922/// Determine which bits of V are known to be either zero or one and return
923/// them in the KnownZero/KnownOne bit sets.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000924///
Chris Lattner965c7692008-06-02 01:18:21 +0000925/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
926/// we cannot optimize based on the assumption that it is zero without changing
927/// it to be an explicit zero. If we don't change it to zero, other code could
928/// optimized based on the contradictory assumption that it is non-zero.
929/// Because instcombine aggressively folds operations with undef args anyway,
930/// this won't lose us code quality.
Chris Lattner4bc28252009-09-08 00:06:16 +0000931///
932/// This function is defined on values with integer type, values with pointer
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000933/// type, and vectors of integers. In the case
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000934/// where V is a vector, known zero, and known one values are the
Chris Lattner4bc28252009-09-08 00:06:16 +0000935/// same width as the vector element, and the bit is set only if it is true
936/// for all of the elements in the vector.
Hal Finkel60db0582014-09-07 18:57:58 +0000937void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000938 const DataLayout &DL, unsigned Depth, const Query &Q) {
Chris Lattner965c7692008-06-02 01:18:21 +0000939 assert(V && "No Value?");
Dan Gohmanbf0002e2009-05-21 02:28:33 +0000940 assert(Depth <= MaxDepth && "Limit Search Depth");
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000941 unsigned BitWidth = KnownZero.getBitWidth();
942
Nadav Rotem3924cb02011-12-05 06:29:09 +0000943 assert((V->getType()->isIntOrIntVectorTy() ||
944 V->getType()->getScalarType()->isPointerTy()) &&
945 "Not integer or pointer type!");
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000946 assert((DL.getTypeSizeInBits(V->getType()->getScalarType()) == BitWidth) &&
Duncan Sands9dff9be2010-02-15 16:12:20 +0000947 (!V->getType()->isIntOrIntVectorTy() ||
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000948 V->getType()->getScalarSizeInBits() == BitWidth) &&
Nadav Rotem3924cb02011-12-05 06:29:09 +0000949 KnownZero.getBitWidth() == BitWidth &&
Chris Lattner965c7692008-06-02 01:18:21 +0000950 KnownOne.getBitWidth() == BitWidth &&
Jay Foade48d9e82014-05-14 08:00:07 +0000951 "V, KnownOne and KnownZero should have same BitWidth");
Chris Lattner965c7692008-06-02 01:18:21 +0000952
953 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
954 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000955 KnownOne = CI->getValue();
956 KnownZero = ~KnownOne;
Chris Lattner965c7692008-06-02 01:18:21 +0000957 return;
958 }
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000959 // Null and aggregate-zero are all-zeros.
960 if (isa<ConstantPointerNull>(V) ||
961 isa<ConstantAggregateZero>(V)) {
Jay Foad25a5e4c2010-12-01 08:53:58 +0000962 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000963 KnownZero = APInt::getAllOnesValue(BitWidth);
Chris Lattner965c7692008-06-02 01:18:21 +0000964 return;
965 }
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000966 // Handle a constant vector by taking the intersection of the known bits of
Chris Lattner8213c8a2012-02-06 21:56:39 +0000967 // each element. There is no real need to handle ConstantVector here, because
968 // we don't handle undef in any particularly useful way.
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000969 if (ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) {
970 // We know that CDS must be a vector of integers. Take the intersection of
971 // each element.
972 KnownZero.setAllBits(); KnownOne.setAllBits();
973 APInt Elt(KnownZero.getBitWidth(), 0);
Chris Lattner9be59592012-01-25 01:27:20 +0000974 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000975 Elt = CDS->getElementAsInteger(i);
976 KnownZero &= ~Elt;
Craig Topper1bef2c82012-12-22 19:15:35 +0000977 KnownOne &= Elt;
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000978 }
979 return;
980 }
Craig Topper1bef2c82012-12-22 19:15:35 +0000981
Chris Lattner965c7692008-06-02 01:18:21 +0000982 // The address of an aligned GlobalValue has trailing zeros.
Michael Kupersteinbe8032c2014-12-23 11:33:41 +0000983 if (auto *GO = dyn_cast<GlobalObject>(V)) {
984 unsigned Align = GO->getAlignment();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000985 if (Align == 0) {
Michael Kupersteinbe8032c2014-12-23 11:33:41 +0000986 if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
Eli Friedmane7ab1a22011-11-28 22:48:22 +0000987 Type *ObjectType = GVar->getType()->getElementType();
Nick Lewycky1d57ee32012-03-07 02:27:53 +0000988 if (ObjectType->isSized()) {
989 // If the object is defined in the current Module, we'll be giving
990 // it the preferred alignment. Otherwise, we have to assume that it
991 // may only have the minimum ABI alignment.
992 if (!GVar->isDeclaration() && !GVar->isWeakForLinker())
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000993 Align = DL.getPreferredAlignment(GVar);
Nick Lewycky1d57ee32012-03-07 02:27:53 +0000994 else
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000995 Align = DL.getABITypeAlignment(ObjectType);
Nick Lewycky1d57ee32012-03-07 02:27:53 +0000996 }
Eli Friedmane7ab1a22011-11-28 22:48:22 +0000997 }
Dan Gohmana72f8562009-08-11 15:50:03 +0000998 }
Chris Lattner965c7692008-06-02 01:18:21 +0000999 if (Align > 0)
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001000 KnownZero = APInt::getLowBitsSet(BitWidth,
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001001 countTrailingZeros(Align));
Chris Lattner965c7692008-06-02 01:18:21 +00001002 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001003 KnownZero.clearAllBits();
1004 KnownOne.clearAllBits();
Chris Lattner965c7692008-06-02 01:18:21 +00001005 return;
1006 }
Craig Topper1bef2c82012-12-22 19:15:35 +00001007
Chris Lattner83791ce2011-05-23 00:03:39 +00001008 if (Argument *A = dyn_cast<Argument>(V)) {
Hal Finkelccc70902014-07-22 16:58:55 +00001009 unsigned Align = A->getType()->isPointerTy() ? A->getParamAlignment() : 0;
Duncan Sands271ea6c2012-10-04 13:36:31 +00001010
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001011 if (!Align && A->hasStructRetAttr()) {
Duncan Sands271ea6c2012-10-04 13:36:31 +00001012 // An sret parameter has at least the ABI alignment of the return type.
1013 Type *EltTy = cast<PointerType>(A->getType())->getElementType();
1014 if (EltTy->isSized())
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001015 Align = DL.getABITypeAlignment(EltTy);
Duncan Sands271ea6c2012-10-04 13:36:31 +00001016 }
1017
1018 if (Align)
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001019 KnownZero = APInt::getLowBitsSet(BitWidth, countTrailingZeros(Align));
David Majnemer8df46c92015-01-03 02:33:25 +00001020 else
1021 KnownZero.clearAllBits();
1022 KnownOne.clearAllBits();
Hal Finkel60db0582014-09-07 18:57:58 +00001023
1024 // Don't give up yet... there might be an assumption that provides more
1025 // information...
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001026 computeKnownBitsFromAssume(V, KnownZero, KnownOne, DL, Depth, Q);
Philip Reames1c292272015-03-10 22:43:20 +00001027
1028 // Or a dominating condition for that matter
1029 if (EnableDomConditions && Depth <= DomConditionsMaxDepth)
1030 computeKnownBitsFromDominatingCondition(V, KnownZero, KnownOne, DL,
1031 Depth, Q);
Chris Lattner83791ce2011-05-23 00:03:39 +00001032 return;
1033 }
Chris Lattner965c7692008-06-02 01:18:21 +00001034
Chris Lattner83791ce2011-05-23 00:03:39 +00001035 // Start out not knowing anything.
1036 KnownZero.clearAllBits(); KnownOne.clearAllBits();
Chris Lattner965c7692008-06-02 01:18:21 +00001037
Michael Kupersteinbe8032c2014-12-23 11:33:41 +00001038 // Limit search depth.
1039 // All recursive calls that increase depth must come after this.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001040 if (Depth == MaxDepth)
Michael Kupersteinbe8032c2014-12-23 11:33:41 +00001041 return;
1042
1043 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
1044 // the bits of its aliasee.
1045 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
1046 if (!GA->mayBeOverridden())
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001047 computeKnownBits(GA->getAliasee(), KnownZero, KnownOne, DL, Depth + 1, Q);
Michael Kupersteinbe8032c2014-12-23 11:33:41 +00001048 return;
1049 }
Chris Lattner965c7692008-06-02 01:18:21 +00001050
Hal Finkel60db0582014-09-07 18:57:58 +00001051 // Check whether a nearby assume intrinsic can determine some known bits.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001052 computeKnownBitsFromAssume(V, KnownZero, KnownOne, DL, Depth, Q);
Hal Finkel60db0582014-09-07 18:57:58 +00001053
Philip Reames1c292272015-03-10 22:43:20 +00001054 // Check whether there's a dominating condition which implies something about
1055 // this value at the given context.
1056 if (EnableDomConditions && Depth <= DomConditionsMaxDepth)
1057 computeKnownBitsFromDominatingCondition(V, KnownZero, KnownOne, DL, Depth,
1058 Q);
1059
Dan Gohman80ca01c2009-07-17 20:47:02 +00001060 Operator *I = dyn_cast<Operator>(V);
Chris Lattner965c7692008-06-02 01:18:21 +00001061 if (!I) return;
1062
1063 APInt KnownZero2(KnownZero), KnownOne2(KnownOne);
Dan Gohman80ca01c2009-07-17 20:47:02 +00001064 switch (I->getOpcode()) {
Chris Lattner965c7692008-06-02 01:18:21 +00001065 default: break;
Rafael Espindola53190532012-03-30 15:52:11 +00001066 case Instruction::Load:
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001067 if (MDNode *MD = cast<LoadInst>(I)->getMetadata(LLVMContext::MD_range))
Jingyue Wu37fcb592014-06-19 16:50:16 +00001068 computeKnownBitsFromRangeMetadata(*MD, KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +00001069 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001070 case Instruction::And: {
1071 // If either the LHS or the RHS are Zero, the result is zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001072 computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q);
1073 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
Craig Topper1bef2c82012-12-22 19:15:35 +00001074
Chris Lattner965c7692008-06-02 01:18:21 +00001075 // Output known-1 bits are only known if set in both the LHS & RHS.
1076 KnownOne &= KnownOne2;
1077 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1078 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001079 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001080 }
1081 case Instruction::Or: {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001082 computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q);
1083 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
Craig Topper1bef2c82012-12-22 19:15:35 +00001084
Chris Lattner965c7692008-06-02 01:18:21 +00001085 // Output known-0 bits are only known if clear in both the LHS & RHS.
1086 KnownZero &= KnownZero2;
1087 // Output known-1 are known to be set if set in either the LHS | RHS.
1088 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +00001089 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001090 }
1091 case Instruction::Xor: {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001092 computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q);
1093 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
Craig Topper1bef2c82012-12-22 19:15:35 +00001094
Chris Lattner965c7692008-06-02 01:18:21 +00001095 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1096 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1097 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1098 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1099 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +00001100 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001101 }
1102 case Instruction::Mul: {
Nick Lewyckyfa306072012-03-18 23:28:48 +00001103 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001104 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, KnownZero,
1105 KnownOne, KnownZero2, KnownOne2, DL, Depth, Q);
Nick Lewyckyfa306072012-03-18 23:28:48 +00001106 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001107 }
1108 case Instruction::UDiv: {
1109 // For the purposes of computing leading zeros we can conservatively
1110 // treat a udiv as a logical right shift by the power of 2 known to
1111 // be less than the denominator.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001112 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001113 unsigned LeadZ = KnownZero2.countLeadingOnes();
1114
Jay Foad25a5e4c2010-12-01 08:53:58 +00001115 KnownOne2.clearAllBits();
1116 KnownZero2.clearAllBits();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001117 computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001118 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1119 if (RHSUnknownLeadingOnes != BitWidth)
1120 LeadZ = std::min(BitWidth,
1121 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
1122
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001123 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00001124 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001125 }
1126 case Instruction::Select:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001127 computeKnownBits(I->getOperand(2), KnownZero, KnownOne, DL, Depth + 1, Q);
1128 computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001129
1130 // Only known if known in both the LHS and RHS.
1131 KnownOne &= KnownOne2;
1132 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001133 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001134 case Instruction::FPTrunc:
1135 case Instruction::FPExt:
1136 case Instruction::FPToUI:
1137 case Instruction::FPToSI:
1138 case Instruction::SIToFP:
1139 case Instruction::UIToFP:
Jay Foad5a29c362014-05-15 12:12:55 +00001140 break; // Can't work with floating point.
Chris Lattner965c7692008-06-02 01:18:21 +00001141 case Instruction::PtrToInt:
1142 case Instruction::IntToPtr:
Matt Arsenaultf1a7e622014-07-15 01:55:03 +00001143 case Instruction::AddrSpaceCast: // Pointers could be different sizes.
Chris Lattner965c7692008-06-02 01:18:21 +00001144 // FALL THROUGH and handle them the same as zext/trunc.
1145 case Instruction::ZExt:
1146 case Instruction::Trunc: {
Chris Lattner229907c2011-07-18 04:54:35 +00001147 Type *SrcTy = I->getOperand(0)->getType();
Nadav Rotem15198e92012-10-26 17:17:05 +00001148
Chris Lattner0cdbc7a2009-09-08 00:13:52 +00001149 unsigned SrcBitWidth;
Chris Lattner965c7692008-06-02 01:18:21 +00001150 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1151 // which fall through here.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001152 SrcBitWidth = DL.getTypeSizeInBits(SrcTy->getScalarType());
Nadav Rotem15198e92012-10-26 17:17:05 +00001153
1154 assert(SrcBitWidth && "SrcBitWidth can't be zero");
Jay Foad583abbc2010-12-07 08:25:19 +00001155 KnownZero = KnownZero.zextOrTrunc(SrcBitWidth);
1156 KnownOne = KnownOne.zextOrTrunc(SrcBitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001157 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
Jay Foad583abbc2010-12-07 08:25:19 +00001158 KnownZero = KnownZero.zextOrTrunc(BitWidth);
1159 KnownOne = KnownOne.zextOrTrunc(BitWidth);
Chris Lattner965c7692008-06-02 01:18:21 +00001160 // Any top bits are known to be zero.
1161 if (BitWidth > SrcBitWidth)
1162 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00001163 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001164 }
1165 case Instruction::BitCast: {
Chris Lattner229907c2011-07-18 04:54:35 +00001166 Type *SrcTy = I->getOperand(0)->getType();
Duncan Sands19d0b472010-02-16 11:11:14 +00001167 if ((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
Chris Lattneredb84072009-07-02 16:04:08 +00001168 // TODO: For now, not handling conversions like:
1169 // (bitcast i64 %x to <2 x i32>)
Duncan Sands19d0b472010-02-16 11:11:14 +00001170 !I->getType()->isVectorTy()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001171 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
Jay Foad5a29c362014-05-15 12:12:55 +00001172 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001173 }
1174 break;
1175 }
1176 case Instruction::SExt: {
1177 // Compute the bits in the result that are not present in the input.
Chris Lattner0cdbc7a2009-09-08 00:13:52 +00001178 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper1bef2c82012-12-22 19:15:35 +00001179
Jay Foad583abbc2010-12-07 08:25:19 +00001180 KnownZero = KnownZero.trunc(SrcBitWidth);
1181 KnownOne = KnownOne.trunc(SrcBitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001182 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
Jay Foad583abbc2010-12-07 08:25:19 +00001183 KnownZero = KnownZero.zext(BitWidth);
1184 KnownOne = KnownOne.zext(BitWidth);
Chris Lattner965c7692008-06-02 01:18:21 +00001185
1186 // If the sign bit of the input is known set or clear, then we know the
1187 // top bits of the result.
1188 if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero
1189 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
1190 else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set
1191 KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00001192 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001193 }
1194 case Instruction::Shl:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001195 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Chris Lattner965c7692008-06-02 01:18:21 +00001196 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1197 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001198 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001199 KnownZero <<= ShiftAmt;
1200 KnownOne <<= ShiftAmt;
1201 KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0
Chris Lattner965c7692008-06-02 01:18:21 +00001202 }
1203 break;
1204 case Instruction::LShr:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001205 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Chris Lattner965c7692008-06-02 01:18:21 +00001206 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1207 // Compute the new bits that are at the top now.
1208 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
Craig Topper1bef2c82012-12-22 19:15:35 +00001209
Chris Lattner965c7692008-06-02 01:18:21 +00001210 // Unsigned shift right.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001211 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001212 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
1213 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
1214 // high bits known zero.
1215 KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
Chris Lattner965c7692008-06-02 01:18:21 +00001216 }
1217 break;
1218 case Instruction::AShr:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001219 // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Chris Lattner965c7692008-06-02 01:18:21 +00001220 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1221 // Compute the new bits that are at the top now.
Chris Lattnerc86e67e2011-01-04 18:19:15 +00001222 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper1bef2c82012-12-22 19:15:35 +00001223
Chris Lattner965c7692008-06-02 01:18:21 +00001224 // Signed shift right.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001225 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001226 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
1227 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Craig Topper1bef2c82012-12-22 19:15:35 +00001228
Chris Lattner965c7692008-06-02 01:18:21 +00001229 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
1230 if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero.
1231 KnownZero |= HighBits;
1232 else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one.
1233 KnownOne |= HighBits;
Chris Lattner965c7692008-06-02 01:18:21 +00001234 }
1235 break;
1236 case Instruction::Sub: {
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001237 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
Jay Foada0653a32014-05-14 21:14:37 +00001238 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001239 KnownZero, KnownOne, KnownZero2, KnownOne2, DL,
1240 Depth, Q);
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001241 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001242 }
Chris Lattner965c7692008-06-02 01:18:21 +00001243 case Instruction::Add: {
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001244 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
Jay Foada0653a32014-05-14 21:14:37 +00001245 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001246 KnownZero, KnownOne, KnownZero2, KnownOne2, DL,
1247 Depth, Q);
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001248 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001249 }
1250 case Instruction::SRem:
1251 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Duncan Sands26cd6bd2010-01-29 06:18:37 +00001252 APInt RA = Rem->getValue().abs();
1253 if (RA.isPowerOf2()) {
1254 APInt LowBits = RA - 1;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001255 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1,
1256 Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001257
Duncan Sands26cd6bd2010-01-29 06:18:37 +00001258 // The low bits of the first operand are unchanged by the srem.
1259 KnownZero = KnownZero2 & LowBits;
1260 KnownOne = KnownOne2 & LowBits;
Chris Lattner965c7692008-06-02 01:18:21 +00001261
Duncan Sands26cd6bd2010-01-29 06:18:37 +00001262 // If the first operand is non-negative or has all low bits zero, then
1263 // the upper bits are all zero.
1264 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1265 KnownZero |= ~LowBits;
1266
1267 // If the first operand is negative and not all low bits are zero, then
1268 // the upper bits are all one.
1269 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
1270 KnownOne |= ~LowBits;
1271
Craig Topper1bef2c82012-12-22 19:15:35 +00001272 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattner965c7692008-06-02 01:18:21 +00001273 }
1274 }
Nick Lewyckye4679792011-03-07 01:50:10 +00001275
1276 // The sign bit is the LHS's sign bit, except when the result of the
1277 // remainder is zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001278 if (KnownZero.isNonNegative()) {
Nick Lewyckye4679792011-03-07 01:50:10 +00001279 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001280 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, DL,
1281 Depth + 1, Q);
Nick Lewyckye4679792011-03-07 01:50:10 +00001282 // If it's known zero, our sign bit is also zero.
1283 if (LHSKnownZero.isNegative())
Duncan Sands34c48692012-04-30 11:56:58 +00001284 KnownZero.setBit(BitWidth - 1);
Nick Lewyckye4679792011-03-07 01:50:10 +00001285 }
1286
Chris Lattner965c7692008-06-02 01:18:21 +00001287 break;
1288 case Instruction::URem: {
1289 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
1290 APInt RA = Rem->getValue();
1291 if (RA.isPowerOf2()) {
1292 APInt LowBits = (RA - 1);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001293 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1,
1294 Q);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001295 KnownZero |= ~LowBits;
1296 KnownOne &= LowBits;
Chris Lattner965c7692008-06-02 01:18:21 +00001297 break;
1298 }
1299 }
1300
1301 // Since the result is less than or equal to either operand, any leading
1302 // zero bits in either operand must also exist in the result.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001303 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1304 computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001305
Chris Lattner4612ae12009-01-20 18:22:57 +00001306 unsigned Leaders = std::max(KnownZero.countLeadingOnes(),
Chris Lattner965c7692008-06-02 01:18:21 +00001307 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00001308 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001309 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Chris Lattner965c7692008-06-02 01:18:21 +00001310 break;
1311 }
1312
Victor Hernandeza3aaf852009-10-17 01:18:07 +00001313 case Instruction::Alloca: {
Victor Hernandez8acf2952009-10-23 21:09:37 +00001314 AllocaInst *AI = cast<AllocaInst>(V);
Chris Lattner965c7692008-06-02 01:18:21 +00001315 unsigned Align = AI->getAlignment();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001316 if (Align == 0)
1317 Align = DL.getABITypeAlignment(AI->getType()->getElementType());
Craig Topper1bef2c82012-12-22 19:15:35 +00001318
Chris Lattner965c7692008-06-02 01:18:21 +00001319 if (Align > 0)
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001320 KnownZero = APInt::getLowBitsSet(BitWidth, countTrailingZeros(Align));
Chris Lattner965c7692008-06-02 01:18:21 +00001321 break;
1322 }
1323 case Instruction::GetElementPtr: {
1324 // Analyze all of the subscripts of this getelementptr instruction
1325 // to determine if we can prove known low zero bits.
Chris Lattner965c7692008-06-02 01:18:21 +00001326 APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001327 computeKnownBits(I->getOperand(0), LocalKnownZero, LocalKnownOne, DL,
1328 Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001329 unsigned TrailZ = LocalKnownZero.countTrailingOnes();
1330
1331 gep_type_iterator GTI = gep_type_begin(I);
1332 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1333 Value *Index = I->getOperand(i);
Chris Lattner229907c2011-07-18 04:54:35 +00001334 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Chris Lattner965c7692008-06-02 01:18:21 +00001335 // Handle struct member offset arithmetic.
Matt Arsenault74742a12013-08-19 21:43:16 +00001336
1337 // Handle case when index is vector zeroinitializer
1338 Constant *CIndex = cast<Constant>(Index);
1339 if (CIndex->isZeroValue())
1340 continue;
1341
1342 if (CIndex->getType()->isVectorTy())
1343 Index = CIndex->getSplatValue();
1344
Chris Lattner965c7692008-06-02 01:18:21 +00001345 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001346 const StructLayout *SL = DL.getStructLayout(STy);
Chris Lattner965c7692008-06-02 01:18:21 +00001347 uint64_t Offset = SL->getElementOffset(Idx);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001348 TrailZ = std::min<unsigned>(TrailZ,
1349 countTrailingZeros(Offset));
Chris Lattner965c7692008-06-02 01:18:21 +00001350 } else {
1351 // Handle array index arithmetic.
Chris Lattner229907c2011-07-18 04:54:35 +00001352 Type *IndexedTy = GTI.getIndexedType();
Jay Foad5a29c362014-05-15 12:12:55 +00001353 if (!IndexedTy->isSized()) {
1354 TrailZ = 0;
1355 break;
1356 }
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001357 unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001358 uint64_t TypeSize = DL.getTypeAllocSize(IndexedTy);
Chris Lattner965c7692008-06-02 01:18:21 +00001359 LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001360 computeKnownBits(Index, LocalKnownZero, LocalKnownOne, DL, Depth + 1,
1361 Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001362 TrailZ = std::min(TrailZ,
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001363 unsigned(countTrailingZeros(TypeSize) +
Chris Lattner4612ae12009-01-20 18:22:57 +00001364 LocalKnownZero.countTrailingOnes()));
Chris Lattner965c7692008-06-02 01:18:21 +00001365 }
1366 }
Craig Topper1bef2c82012-12-22 19:15:35 +00001367
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001368 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ);
Chris Lattner965c7692008-06-02 01:18:21 +00001369 break;
1370 }
1371 case Instruction::PHI: {
1372 PHINode *P = cast<PHINode>(I);
1373 // Handle the case of a simple two-predecessor recurrence PHI.
1374 // There's a lot more that could theoretically be done here, but
1375 // this is sufficient to catch some interesting cases.
1376 if (P->getNumIncomingValues() == 2) {
1377 for (unsigned i = 0; i != 2; ++i) {
1378 Value *L = P->getIncomingValue(i);
1379 Value *R = P->getIncomingValue(!i);
Dan Gohman80ca01c2009-07-17 20:47:02 +00001380 Operator *LU = dyn_cast<Operator>(L);
Chris Lattner965c7692008-06-02 01:18:21 +00001381 if (!LU)
1382 continue;
Dan Gohman80ca01c2009-07-17 20:47:02 +00001383 unsigned Opcode = LU->getOpcode();
Chris Lattner965c7692008-06-02 01:18:21 +00001384 // Check for operations that have the property that if
1385 // both their operands have low zero bits, the result
1386 // will have low zero bits.
1387 if (Opcode == Instruction::Add ||
1388 Opcode == Instruction::Sub ||
1389 Opcode == Instruction::And ||
1390 Opcode == Instruction::Or ||
1391 Opcode == Instruction::Mul) {
1392 Value *LL = LU->getOperand(0);
1393 Value *LR = LU->getOperand(1);
1394 // Find a recurrence.
1395 if (LL == I)
1396 L = LR;
1397 else if (LR == I)
1398 L = LL;
1399 else
1400 break;
1401 // Ok, we have a PHI of the form L op= R. Check for low
1402 // zero bits.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001403 computeKnownBits(R, KnownZero2, KnownOne2, DL, Depth + 1, Q);
David Greeneaebd9e02008-10-27 23:24:03 +00001404
1405 // We need to take the minimum number of known bits
1406 APInt KnownZero3(KnownZero), KnownOne3(KnownOne);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001407 computeKnownBits(L, KnownZero3, KnownOne3, DL, Depth + 1, Q);
David Greeneaebd9e02008-10-27 23:24:03 +00001408
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001409 KnownZero = APInt::getLowBitsSet(BitWidth,
David Greeneaebd9e02008-10-27 23:24:03 +00001410 std::min(KnownZero2.countTrailingOnes(),
1411 KnownZero3.countTrailingOnes()));
Chris Lattner965c7692008-06-02 01:18:21 +00001412 break;
1413 }
1414 }
1415 }
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001416
Nick Lewyckyac0b62c2011-02-10 23:54:10 +00001417 // Unreachable blocks may have zero-operand PHI nodes.
1418 if (P->getNumIncomingValues() == 0)
Jay Foad5a29c362014-05-15 12:12:55 +00001419 break;
Nick Lewyckyac0b62c2011-02-10 23:54:10 +00001420
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001421 // Otherwise take the unions of the known bit sets of the operands,
1422 // taking conservative care to avoid excessive recursion.
1423 if (Depth < MaxDepth - 1 && !KnownZero && !KnownOne) {
Duncan Sands7dc3d472011-03-08 12:39:03 +00001424 // Skip if every incoming value references to ourself.
Nuno Lopes0d44a502012-07-03 21:15:40 +00001425 if (dyn_cast_or_null<UndefValue>(P->hasConstantValue()))
Duncan Sands7dc3d472011-03-08 12:39:03 +00001426 break;
1427
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001428 KnownZero = APInt::getAllOnesValue(BitWidth);
1429 KnownOne = APInt::getAllOnesValue(BitWidth);
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001430 for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) {
1431 // Skip direct self references.
1432 if (P->getIncomingValue(i) == P) continue;
1433
1434 KnownZero2 = APInt(BitWidth, 0);
1435 KnownOne2 = APInt(BitWidth, 0);
1436 // Recurse, but cap the recursion to one level, because we don't
1437 // want to waste time spinning around in loops.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001438 computeKnownBits(P->getIncomingValue(i), KnownZero2, KnownOne2, DL,
1439 MaxDepth - 1, Q);
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001440 KnownZero &= KnownZero2;
1441 KnownOne &= KnownOne2;
1442 // If all bits have been ruled out, there's no need to check
1443 // more operands.
1444 if (!KnownZero && !KnownOne)
1445 break;
1446 }
1447 }
Chris Lattner965c7692008-06-02 01:18:21 +00001448 break;
1449 }
1450 case Instruction::Call:
Jingyue Wu37fcb592014-06-19 16:50:16 +00001451 case Instruction::Invoke:
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001452 if (MDNode *MD = cast<Instruction>(I)->getMetadata(LLVMContext::MD_range))
Jingyue Wu37fcb592014-06-19 16:50:16 +00001453 computeKnownBitsFromRangeMetadata(*MD, KnownZero);
1454 // If a range metadata is attached to this IntrinsicInst, intersect the
1455 // explicit range specified by the metadata and the implicit range of
1456 // the intrinsic.
Chris Lattner965c7692008-06-02 01:18:21 +00001457 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1458 switch (II->getIntrinsicID()) {
1459 default: break;
Chris Lattner965c7692008-06-02 01:18:21 +00001460 case Intrinsic::ctlz:
1461 case Intrinsic::cttz: {
1462 unsigned LowBits = Log2_32(BitWidth)+1;
Benjamin Kramer4ee57472011-12-24 17:31:46 +00001463 // If this call is undefined for 0, the result will be less than 2^n.
1464 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1465 LowBits -= 1;
Jingyue Wu37fcb592014-06-19 16:50:16 +00001466 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Benjamin Kramer4ee57472011-12-24 17:31:46 +00001467 break;
1468 }
1469 case Intrinsic::ctpop: {
1470 unsigned LowBits = Log2_32(BitWidth)+1;
Jingyue Wu37fcb592014-06-19 16:50:16 +00001471 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Chris Lattner965c7692008-06-02 01:18:21 +00001472 break;
1473 }
Chad Rosierb3628842011-05-26 23:13:19 +00001474 case Intrinsic::x86_sse42_crc32_64_64:
Jingyue Wu37fcb592014-06-19 16:50:16 +00001475 KnownZero |= APInt::getHighBitsSet(64, 32);
Evan Cheng2a746bf2011-05-22 18:25:30 +00001476 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001477 }
1478 }
1479 break;
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001480 case Instruction::ExtractValue:
1481 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
1482 ExtractValueInst *EVI = cast<ExtractValueInst>(I);
1483 if (EVI->getNumIndices() != 1) break;
1484 if (EVI->getIndices()[0] == 0) {
1485 switch (II->getIntrinsicID()) {
1486 default: break;
1487 case Intrinsic::uadd_with_overflow:
1488 case Intrinsic::sadd_with_overflow:
Jay Foada0653a32014-05-14 21:14:37 +00001489 computeKnownBitsAddSub(true, II->getArgOperand(0),
1490 II->getArgOperand(1), false, KnownZero,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001491 KnownOne, KnownZero2, KnownOne2, DL, Depth, Q);
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001492 break;
1493 case Intrinsic::usub_with_overflow:
1494 case Intrinsic::ssub_with_overflow:
Jay Foada0653a32014-05-14 21:14:37 +00001495 computeKnownBitsAddSub(false, II->getArgOperand(0),
1496 II->getArgOperand(1), false, KnownZero,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001497 KnownOne, KnownZero2, KnownOne2, DL, Depth, Q);
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001498 break;
Nick Lewyckyfa306072012-03-18 23:28:48 +00001499 case Intrinsic::umul_with_overflow:
1500 case Intrinsic::smul_with_overflow:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001501 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
1502 KnownZero, KnownOne, KnownZero2, KnownOne2, DL,
1503 Depth, Q);
Nick Lewyckyfa306072012-03-18 23:28:48 +00001504 break;
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001505 }
1506 }
1507 }
Chris Lattner965c7692008-06-02 01:18:21 +00001508 }
Jay Foad5a29c362014-05-15 12:12:55 +00001509
1510 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattner965c7692008-06-02 01:18:21 +00001511}
1512
Sanjay Patelaee84212014-11-04 16:27:42 +00001513/// Determine whether the sign bit is known to be zero or one.
1514/// Convenience wrapper around computeKnownBits.
Hal Finkel60db0582014-09-07 18:57:58 +00001515void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001516 const DataLayout &DL, unsigned Depth, const Query &Q) {
1517 unsigned BitWidth = getBitWidth(V->getType(), DL);
Duncan Sandsd3951082011-01-25 09:38:29 +00001518 if (!BitWidth) {
1519 KnownZero = false;
1520 KnownOne = false;
1521 return;
1522 }
1523 APInt ZeroBits(BitWidth, 0);
1524 APInt OneBits(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001525 computeKnownBits(V, ZeroBits, OneBits, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001526 KnownOne = OneBits[BitWidth - 1];
1527 KnownZero = ZeroBits[BitWidth - 1];
1528}
1529
Sanjay Patelaee84212014-11-04 16:27:42 +00001530/// Return true if the given value is known to have exactly one
Duncan Sandsd3951082011-01-25 09:38:29 +00001531/// bit set when defined. For vectors return true if every element is known to
Sanjay Patelaee84212014-11-04 16:27:42 +00001532/// be a power of two when defined. Supports values with integer or pointer
Duncan Sandsd3951082011-01-25 09:38:29 +00001533/// types and vectors of integers.
Hal Finkel60db0582014-09-07 18:57:58 +00001534bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero, unsigned Depth,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001535 const Query &Q, const DataLayout &DL) {
Duncan Sandsba286d72011-10-26 20:55:21 +00001536 if (Constant *C = dyn_cast<Constant>(V)) {
1537 if (C->isNullValue())
1538 return OrZero;
1539 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
1540 return CI->getValue().isPowerOf2();
1541 // TODO: Handle vector constants.
1542 }
Duncan Sandsd3951082011-01-25 09:38:29 +00001543
1544 // 1 << X is clearly a power of two if the one is not shifted off the end. If
1545 // it is shifted off the end then the result is undefined.
1546 if (match(V, m_Shl(m_One(), m_Value())))
1547 return true;
1548
1549 // (signbit) >>l X is clearly a power of two if the one is not shifted off the
1550 // bottom. If it is shifted off the bottom then the result is undefined.
Duncan Sands4b397fc2011-02-01 08:50:33 +00001551 if (match(V, m_LShr(m_SignBit(), m_Value())))
Duncan Sandsd3951082011-01-25 09:38:29 +00001552 return true;
1553
1554 // The remaining tests are all recursive, so bail out if we hit the limit.
1555 if (Depth++ == MaxDepth)
1556 return false;
1557
Craig Topper9f008862014-04-15 04:59:12 +00001558 Value *X = nullptr, *Y = nullptr;
Duncan Sands985ba632011-10-28 18:30:05 +00001559 // A shift of a power of two is a power of two or zero.
1560 if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) ||
1561 match(V, m_Shr(m_Value(X), m_Value()))))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001562 return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q, DL);
Duncan Sands985ba632011-10-28 18:30:05 +00001563
Duncan Sandsd3951082011-01-25 09:38:29 +00001564 if (ZExtInst *ZI = dyn_cast<ZExtInst>(V))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001565 return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q, DL);
Duncan Sandsd3951082011-01-25 09:38:29 +00001566
1567 if (SelectInst *SI = dyn_cast<SelectInst>(V))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001568 return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q, DL) &&
1569 isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q, DL);
Duncan Sandsba286d72011-10-26 20:55:21 +00001570
Duncan Sandsba286d72011-10-26 20:55:21 +00001571 if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) {
1572 // A power of two and'd with anything is a power of two or zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001573 if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q, DL) ||
1574 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q, DL))
Duncan Sandsba286d72011-10-26 20:55:21 +00001575 return true;
1576 // X & (-X) is always a power of two or zero.
1577 if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X))))
1578 return true;
1579 return false;
1580 }
Duncan Sandsd3951082011-01-25 09:38:29 +00001581
David Majnemerb7d54092013-07-30 21:01:36 +00001582 // Adding a power-of-two or zero to the same power-of-two or zero yields
1583 // either the original power-of-two, a larger power-of-two or zero.
1584 if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
1585 OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
1586 if (OrZero || VOBO->hasNoUnsignedWrap() || VOBO->hasNoSignedWrap()) {
1587 if (match(X, m_And(m_Specific(Y), m_Value())) ||
1588 match(X, m_And(m_Value(), m_Specific(Y))))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001589 if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q, DL))
David Majnemerb7d54092013-07-30 21:01:36 +00001590 return true;
1591 if (match(Y, m_And(m_Specific(X), m_Value())) ||
1592 match(Y, m_And(m_Value(), m_Specific(X))))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001593 if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q, DL))
David Majnemerb7d54092013-07-30 21:01:36 +00001594 return true;
1595
1596 unsigned BitWidth = V->getType()->getScalarSizeInBits();
1597 APInt LHSZeroBits(BitWidth, 0), LHSOneBits(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001598 computeKnownBits(X, LHSZeroBits, LHSOneBits, DL, Depth, Q);
David Majnemerb7d54092013-07-30 21:01:36 +00001599
1600 APInt RHSZeroBits(BitWidth, 0), RHSOneBits(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001601 computeKnownBits(Y, RHSZeroBits, RHSOneBits, DL, Depth, Q);
David Majnemerb7d54092013-07-30 21:01:36 +00001602 // If i8 V is a power of two or zero:
1603 // ZeroBits: 1 1 1 0 1 1 1 1
1604 // ~ZeroBits: 0 0 0 1 0 0 0 0
1605 if ((~(LHSZeroBits & RHSZeroBits)).isPowerOf2())
1606 // If OrZero isn't set, we cannot give back a zero result.
1607 // Make sure either the LHS or RHS has a bit set.
1608 if (OrZero || RHSOneBits.getBoolValue() || LHSOneBits.getBoolValue())
1609 return true;
1610 }
1611 }
David Majnemerbeab5672013-05-18 19:30:37 +00001612
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001613 // An exact divide or right shift can only shift off zero bits, so the result
Nick Lewyckyf0469af2011-03-21 21:40:32 +00001614 // is a power of two only if the first operand is a power of two and not
1615 // copying a sign bit (sdiv int_min, 2).
Benjamin Kramer9442cd02012-01-01 17:55:30 +00001616 if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) ||
1617 match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) {
Hal Finkel60db0582014-09-07 18:57:58 +00001618 return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001619 Depth, Q, DL);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001620 }
1621
Duncan Sandsd3951082011-01-25 09:38:29 +00001622 return false;
1623}
1624
Chandler Carruth80d3e562012-12-07 02:08:58 +00001625/// \brief Test whether a GEP's result is known to be non-null.
1626///
1627/// Uses properties inherent in a GEP to try to determine whether it is known
1628/// to be non-null.
1629///
1630/// Currently this routine does not support vector GEPs.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001631static bool isGEPKnownNonNull(GEPOperator *GEP, const DataLayout &DL,
Hal Finkel60db0582014-09-07 18:57:58 +00001632 unsigned Depth, const Query &Q) {
Chandler Carruth80d3e562012-12-07 02:08:58 +00001633 if (!GEP->isInBounds() || GEP->getPointerAddressSpace() != 0)
1634 return false;
1635
1636 // FIXME: Support vector-GEPs.
1637 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
1638
1639 // If the base pointer is non-null, we cannot walk to a null address with an
1640 // inbounds GEP in address space zero.
Hal Finkel60db0582014-09-07 18:57:58 +00001641 if (isKnownNonZero(GEP->getPointerOperand(), DL, Depth, Q))
Chandler Carruth80d3e562012-12-07 02:08:58 +00001642 return true;
1643
Chandler Carruth80d3e562012-12-07 02:08:58 +00001644 // Walk the GEP operands and see if any operand introduces a non-zero offset.
1645 // If so, then the GEP cannot produce a null pointer, as doing so would
1646 // inherently violate the inbounds contract within address space zero.
1647 for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
1648 GTI != GTE; ++GTI) {
1649 // Struct types are easy -- they must always be indexed by a constant.
1650 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
1651 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
1652 unsigned ElementIdx = OpC->getZExtValue();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001653 const StructLayout *SL = DL.getStructLayout(STy);
Chandler Carruth80d3e562012-12-07 02:08:58 +00001654 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
1655 if (ElementOffset > 0)
1656 return true;
1657 continue;
1658 }
1659
1660 // If we have a zero-sized type, the index doesn't matter. Keep looping.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001661 if (DL.getTypeAllocSize(GTI.getIndexedType()) == 0)
Chandler Carruth80d3e562012-12-07 02:08:58 +00001662 continue;
1663
1664 // Fast path the constant operand case both for efficiency and so we don't
1665 // increment Depth when just zipping down an all-constant GEP.
1666 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
1667 if (!OpC->isZero())
1668 return true;
1669 continue;
1670 }
1671
1672 // We post-increment Depth here because while isKnownNonZero increments it
1673 // as well, when we pop back up that increment won't persist. We don't want
1674 // to recurse 10k times just because we have 10k GEP operands. We don't
1675 // bail completely out because we want to handle constant GEPs regardless
1676 // of depth.
1677 if (Depth++ >= MaxDepth)
1678 continue;
1679
Hal Finkel60db0582014-09-07 18:57:58 +00001680 if (isKnownNonZero(GTI.getOperand(), DL, Depth, Q))
Chandler Carruth80d3e562012-12-07 02:08:58 +00001681 return true;
1682 }
1683
1684 return false;
1685}
1686
Philip Reames4cb4d3e2014-10-30 20:25:19 +00001687/// Does the 'Range' metadata (which must be a valid MD_range operand list)
1688/// ensure that the value it's attached to is never Value? 'RangeType' is
1689/// is the type of the value described by the range.
1690static bool rangeMetadataExcludesValue(MDNode* Ranges,
1691 const APInt& Value) {
1692 const unsigned NumRanges = Ranges->getNumOperands() / 2;
1693 assert(NumRanges >= 1);
1694 for (unsigned i = 0; i < NumRanges; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001695 ConstantInt *Lower =
1696 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
1697 ConstantInt *Upper =
1698 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
Philip Reames4cb4d3e2014-10-30 20:25:19 +00001699 ConstantRange Range(Lower->getValue(), Upper->getValue());
1700 if (Range.contains(Value))
1701 return false;
1702 }
1703 return true;
1704}
1705
Sanjay Patelaee84212014-11-04 16:27:42 +00001706/// Return true if the given value is known to be non-zero when defined.
1707/// For vectors return true if every element is known to be non-zero when
1708/// defined. Supports values with integer or pointer type and vectors of
1709/// integers.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001710bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth,
Hal Finkel60db0582014-09-07 18:57:58 +00001711 const Query &Q) {
Duncan Sandsd3951082011-01-25 09:38:29 +00001712 if (Constant *C = dyn_cast<Constant>(V)) {
1713 if (C->isNullValue())
1714 return false;
1715 if (isa<ConstantInt>(C))
1716 // Must be non-zero due to null test above.
1717 return true;
1718 // TODO: Handle vectors
1719 return false;
1720 }
1721
Philip Reames4cb4d3e2014-10-30 20:25:19 +00001722 if (Instruction* I = dyn_cast<Instruction>(V)) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001723 if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range)) {
Philip Reames4cb4d3e2014-10-30 20:25:19 +00001724 // If the possible ranges don't contain zero, then the value is
1725 // definitely non-zero.
1726 if (IntegerType* Ty = dyn_cast<IntegerType>(V->getType())) {
1727 const APInt ZeroValue(Ty->getBitWidth(), 0);
1728 if (rangeMetadataExcludesValue(Ranges, ZeroValue))
1729 return true;
1730 }
1731 }
1732 }
1733
Duncan Sandsd3951082011-01-25 09:38:29 +00001734 // The remaining tests are all recursive, so bail out if we hit the limit.
Duncan Sands7cb61e52011-10-27 19:16:21 +00001735 if (Depth++ >= MaxDepth)
Duncan Sandsd3951082011-01-25 09:38:29 +00001736 return false;
1737
Chandler Carruth80d3e562012-12-07 02:08:58 +00001738 // Check for pointer simplifications.
1739 if (V->getType()->isPointerTy()) {
Manman Ren12171122013-03-18 21:23:25 +00001740 if (isKnownNonNull(V))
1741 return true;
Chandler Carruth80d3e562012-12-07 02:08:58 +00001742 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001743 if (isGEPKnownNonNull(GEP, DL, Depth, Q))
Chandler Carruth80d3e562012-12-07 02:08:58 +00001744 return true;
1745 }
1746
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001747 unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), DL);
Duncan Sandsd3951082011-01-25 09:38:29 +00001748
1749 // X | Y != 0 if X != 0 or Y != 0.
Craig Topper9f008862014-04-15 04:59:12 +00001750 Value *X = nullptr, *Y = nullptr;
Duncan Sandsd3951082011-01-25 09:38:29 +00001751 if (match(V, m_Or(m_Value(X), m_Value(Y))))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001752 return isKnownNonZero(X, DL, Depth, Q) || isKnownNonZero(Y, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001753
1754 // ext X != 0 if X != 0.
1755 if (isa<SExtInst>(V) || isa<ZExtInst>(V))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001756 return isKnownNonZero(cast<Instruction>(V)->getOperand(0), DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001757
Duncan Sands2e9e4f12011-01-29 13:27:00 +00001758 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
Duncan Sandsd3951082011-01-25 09:38:29 +00001759 // if the lowest bit is shifted off the end.
1760 if (BitWidth && match(V, m_Shl(m_Value(X), m_Value(Y)))) {
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001761 // shl nuw can't remove any non-zero bits.
Duncan Sands7cb61e52011-10-27 19:16:21 +00001762 OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001763 if (BO->hasNoUnsignedWrap())
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001764 return isKnownNonZero(X, DL, Depth, Q);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001765
Duncan Sandsd3951082011-01-25 09:38:29 +00001766 APInt KnownZero(BitWidth, 0);
1767 APInt KnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001768 computeKnownBits(X, KnownZero, KnownOne, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001769 if (KnownOne[0])
1770 return true;
1771 }
Duncan Sands2e9e4f12011-01-29 13:27:00 +00001772 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
Duncan Sandsd3951082011-01-25 09:38:29 +00001773 // defined if the sign bit is shifted off the end.
1774 else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) {
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001775 // shr exact can only shift out zero bits.
Duncan Sands7cb61e52011-10-27 19:16:21 +00001776 PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001777 if (BO->isExact())
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001778 return isKnownNonZero(X, DL, Depth, Q);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001779
Duncan Sandsd3951082011-01-25 09:38:29 +00001780 bool XKnownNonNegative, XKnownNegative;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001781 ComputeSignBit(X, XKnownNonNegative, XKnownNegative, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001782 if (XKnownNegative)
1783 return true;
1784 }
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001785 // div exact can only produce a zero if the dividend is zero.
Benjamin Kramer9442cd02012-01-01 17:55:30 +00001786 else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001787 return isKnownNonZero(X, DL, Depth, Q);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001788 }
Duncan Sandsd3951082011-01-25 09:38:29 +00001789 // X + Y.
1790 else if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
1791 bool XKnownNonNegative, XKnownNegative;
1792 bool YKnownNonNegative, YKnownNegative;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001793 ComputeSignBit(X, XKnownNonNegative, XKnownNegative, DL, Depth, Q);
1794 ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001795
1796 // If X and Y are both non-negative (as signed values) then their sum is not
Duncan Sands9e9d5b22011-01-25 15:14:15 +00001797 // zero unless both X and Y are zero.
Duncan Sandsd3951082011-01-25 09:38:29 +00001798 if (XKnownNonNegative && YKnownNonNegative)
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001799 if (isKnownNonZero(X, DL, Depth, Q) || isKnownNonZero(Y, DL, Depth, Q))
Duncan Sands9e9d5b22011-01-25 15:14:15 +00001800 return true;
Duncan Sandsd3951082011-01-25 09:38:29 +00001801
1802 // If X and Y are both negative (as signed values) then their sum is not
1803 // zero unless both X and Y equal INT_MIN.
1804 if (BitWidth && XKnownNegative && YKnownNegative) {
1805 APInt KnownZero(BitWidth, 0);
1806 APInt KnownOne(BitWidth, 0);
1807 APInt Mask = APInt::getSignedMaxValue(BitWidth);
1808 // The sign bit of X is set. If some other bit is set then X is not equal
1809 // to INT_MIN.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001810 computeKnownBits(X, KnownZero, KnownOne, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001811 if ((KnownOne & Mask) != 0)
1812 return true;
1813 // The sign bit of Y is set. If some other bit is set then Y is not equal
1814 // to INT_MIN.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001815 computeKnownBits(Y, KnownZero, KnownOne, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001816 if ((KnownOne & Mask) != 0)
1817 return true;
1818 }
1819
1820 // The sum of a non-negative number and a power of two is not zero.
Hal Finkel60db0582014-09-07 18:57:58 +00001821 if (XKnownNonNegative &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001822 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q, DL))
Duncan Sandsd3951082011-01-25 09:38:29 +00001823 return true;
Hal Finkel60db0582014-09-07 18:57:58 +00001824 if (YKnownNonNegative &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001825 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q, DL))
Duncan Sandsd3951082011-01-25 09:38:29 +00001826 return true;
1827 }
Duncan Sands7cb61e52011-10-27 19:16:21 +00001828 // X * Y.
1829 else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) {
1830 OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
1831 // If X and Y are non-zero then so is X * Y as long as the multiplication
1832 // does not overflow.
1833 if ((BO->hasNoSignedWrap() || BO->hasNoUnsignedWrap()) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001834 isKnownNonZero(X, DL, Depth, Q) && isKnownNonZero(Y, DL, Depth, Q))
Duncan Sands7cb61e52011-10-27 19:16:21 +00001835 return true;
1836 }
Duncan Sandsd3951082011-01-25 09:38:29 +00001837 // (C ? X : Y) != 0 if X != 0 and Y != 0.
1838 else if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001839 if (isKnownNonZero(SI->getTrueValue(), DL, Depth, Q) &&
1840 isKnownNonZero(SI->getFalseValue(), DL, Depth, Q))
Duncan Sandsd3951082011-01-25 09:38:29 +00001841 return true;
1842 }
1843
1844 if (!BitWidth) return false;
1845 APInt KnownZero(BitWidth, 0);
1846 APInt KnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001847 computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001848 return KnownOne != 0;
1849}
1850
Sanjay Patelaee84212014-11-04 16:27:42 +00001851/// Return true if 'V & Mask' is known to be zero. We use this predicate to
1852/// simplify operations downstream. Mask is known to be zero for bits that V
1853/// cannot have.
Chris Lattner4bc28252009-09-08 00:06:16 +00001854///
1855/// This function is defined on values with integer type, values with pointer
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001856/// type, and vectors of integers. In the case
Chris Lattner4bc28252009-09-08 00:06:16 +00001857/// where V is a vector, the mask, known zero, and known one values are the
1858/// same width as the vector element, and the bit is set only if it is true
1859/// for all of the elements in the vector.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001860bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
1861 unsigned Depth, const Query &Q) {
Chris Lattner965c7692008-06-02 01:18:21 +00001862 APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001863 computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001864 return (KnownZero & Mask) == Mask;
1865}
1866
1867
1868
Sanjay Patelaee84212014-11-04 16:27:42 +00001869/// Return the number of times the sign bit of the register is replicated into
1870/// the other bits. We know that at least 1 bit is always equal to the sign bit
1871/// (itself), but other cases can give us information. For example, immediately
1872/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
1873/// other, so we return 3.
Chris Lattner965c7692008-06-02 01:18:21 +00001874///
1875/// 'Op' must have a scalar integer type.
1876///
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001877unsigned ComputeNumSignBits(Value *V, const DataLayout &DL, unsigned Depth,
1878 const Query &Q) {
1879 unsigned TyBits = DL.getTypeSizeInBits(V->getType()->getScalarType());
Chris Lattner965c7692008-06-02 01:18:21 +00001880 unsigned Tmp, Tmp2;
1881 unsigned FirstAnswer = 1;
1882
Jay Foada0653a32014-05-14 21:14:37 +00001883 // Note that ConstantInt is handled by the general computeKnownBits case
Chris Lattner2e01a692008-06-02 18:39:07 +00001884 // below.
1885
Chris Lattner965c7692008-06-02 01:18:21 +00001886 if (Depth == 6)
1887 return 1; // Limit search depth.
Craig Topper1bef2c82012-12-22 19:15:35 +00001888
Dan Gohman80ca01c2009-07-17 20:47:02 +00001889 Operator *U = dyn_cast<Operator>(V);
1890 switch (Operator::getOpcode(V)) {
Chris Lattner965c7692008-06-02 01:18:21 +00001891 default: break;
1892 case Instruction::SExt:
Mon P Wangbb3eac92009-12-02 04:59:58 +00001893 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001894 return ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q) + Tmp;
Craig Topper1bef2c82012-12-22 19:15:35 +00001895
Nadav Rotemc99a3872015-03-06 00:23:58 +00001896 case Instruction::SDiv: {
Nadav Rotem029c5c72015-03-03 21:39:02 +00001897 const APInt *Denominator;
1898 // sdiv X, C -> adds log(C) sign bits.
1899 if (match(U->getOperand(1), m_APInt(Denominator))) {
1900
1901 // Ignore non-positive denominator.
1902 if (!Denominator->isStrictlyPositive())
1903 break;
1904
1905 // Calculate the incoming numerator bits.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001906 unsigned NumBits = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
Nadav Rotem029c5c72015-03-03 21:39:02 +00001907
1908 // Add floor(log(C)) bits to the numerator bits.
1909 return std::min(TyBits, NumBits + Denominator->logBase2());
1910 }
1911 break;
Nadav Rotemc99a3872015-03-06 00:23:58 +00001912 }
1913
1914 case Instruction::SRem: {
1915 const APInt *Denominator;
Sanjoy Dase561fee2015-03-25 22:33:53 +00001916 // srem X, C -> we know that the result is within [-C+1,C) when C is a
1917 // positive constant. This let us put a lower bound on the number of sign
1918 // bits.
Nadav Rotemc99a3872015-03-06 00:23:58 +00001919 if (match(U->getOperand(1), m_APInt(Denominator))) {
1920
1921 // Ignore non-positive denominator.
1922 if (!Denominator->isStrictlyPositive())
1923 break;
1924
1925 // Calculate the incoming numerator bits. SRem by a positive constant
1926 // can't lower the number of sign bits.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001927 unsigned NumrBits =
1928 ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
Nadav Rotemc99a3872015-03-06 00:23:58 +00001929
1930 // Calculate the leading sign bit constraints by examining the
Sanjoy Dase561fee2015-03-25 22:33:53 +00001931 // denominator. Given that the denominator is positive, there are two
1932 // cases:
1933 //
1934 // 1. the numerator is positive. The result range is [0,C) and [0,C) u<
1935 // (1 << ceilLogBase2(C)).
1936 //
1937 // 2. the numerator is negative. Then the result range is (-C,0] and
1938 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
1939 //
1940 // Thus a lower bound on the number of sign bits is `TyBits -
1941 // ceilLogBase2(C)`.
Nadav Rotemc99a3872015-03-06 00:23:58 +00001942
Sanjoy Dase561fee2015-03-25 22:33:53 +00001943 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
Nadav Rotemc99a3872015-03-06 00:23:58 +00001944 return std::max(NumrBits, ResBits);
1945 }
1946 break;
1947 }
Nadav Rotem029c5c72015-03-03 21:39:02 +00001948
Chris Lattner61a1d6c2012-01-26 21:37:55 +00001949 case Instruction::AShr: {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001950 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
Chris Lattner61a1d6c2012-01-26 21:37:55 +00001951 // ashr X, C -> adds C sign bits. Vectors too.
1952 const APInt *ShAmt;
1953 if (match(U->getOperand(1), m_APInt(ShAmt))) {
1954 Tmp += ShAmt->getZExtValue();
Chris Lattner965c7692008-06-02 01:18:21 +00001955 if (Tmp > TyBits) Tmp = TyBits;
1956 }
1957 return Tmp;
Chris Lattner61a1d6c2012-01-26 21:37:55 +00001958 }
1959 case Instruction::Shl: {
1960 const APInt *ShAmt;
1961 if (match(U->getOperand(1), m_APInt(ShAmt))) {
Chris Lattner965c7692008-06-02 01:18:21 +00001962 // shl destroys sign bits.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001963 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
Chris Lattner61a1d6c2012-01-26 21:37:55 +00001964 Tmp2 = ShAmt->getZExtValue();
1965 if (Tmp2 >= TyBits || // Bad shift.
1966 Tmp2 >= Tmp) break; // Shifted all sign bits out.
1967 return Tmp - Tmp2;
Chris Lattner965c7692008-06-02 01:18:21 +00001968 }
1969 break;
Chris Lattner61a1d6c2012-01-26 21:37:55 +00001970 }
Chris Lattner965c7692008-06-02 01:18:21 +00001971 case Instruction::And:
1972 case Instruction::Or:
1973 case Instruction::Xor: // NOT is handled here.
1974 // Logical binary ops preserve the number of sign bits at the worst.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001975 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001976 if (Tmp != 1) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001977 Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001978 FirstAnswer = std::min(Tmp, Tmp2);
1979 // We computed what we know about the sign bits as our first
1980 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00001981 // computeKnownBits, and pick whichever answer is better.
Chris Lattner965c7692008-06-02 01:18:21 +00001982 }
1983 break;
1984
1985 case Instruction::Select:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001986 Tmp = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001987 if (Tmp == 1) return 1; // Early out.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001988 Tmp2 = ComputeNumSignBits(U->getOperand(2), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001989 return std::min(Tmp, Tmp2);
Craig Topper1bef2c82012-12-22 19:15:35 +00001990
Chris Lattner965c7692008-06-02 01:18:21 +00001991 case Instruction::Add:
1992 // Add can have at most one carry bit. Thus we know that the output
1993 // is, at worst, one more bit than the inputs.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001994 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001995 if (Tmp == 1) return 1; // Early out.
Craig Topper1bef2c82012-12-22 19:15:35 +00001996
Chris Lattner965c7692008-06-02 01:18:21 +00001997 // Special case decrementing a value (ADD X, -1):
David Majnemera55027f2014-12-26 09:20:17 +00001998 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
Chris Lattner965c7692008-06-02 01:18:21 +00001999 if (CRHS->isAllOnesValue()) {
2000 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002001 computeKnownBits(U->getOperand(0), KnownZero, KnownOne, DL, Depth + 1,
2002 Q);
Craig Topper1bef2c82012-12-22 19:15:35 +00002003
Chris Lattner965c7692008-06-02 01:18:21 +00002004 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2005 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002006 if ((KnownZero | APInt(TyBits, 1)).isAllOnesValue())
Chris Lattner965c7692008-06-02 01:18:21 +00002007 return TyBits;
Craig Topper1bef2c82012-12-22 19:15:35 +00002008
Chris Lattner965c7692008-06-02 01:18:21 +00002009 // If we are subtracting one from a positive number, there is no carry
2010 // out of the result.
2011 if (KnownZero.isNegative())
2012 return Tmp;
2013 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002014
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002015 Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002016 if (Tmp2 == 1) return 1;
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002017 return std::min(Tmp, Tmp2)-1;
Craig Topper1bef2c82012-12-22 19:15:35 +00002018
Chris Lattner965c7692008-06-02 01:18:21 +00002019 case Instruction::Sub:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002020 Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002021 if (Tmp2 == 1) return 1;
Craig Topper1bef2c82012-12-22 19:15:35 +00002022
Chris Lattner965c7692008-06-02 01:18:21 +00002023 // Handle NEG.
David Majnemera55027f2014-12-26 09:20:17 +00002024 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
Chris Lattner965c7692008-06-02 01:18:21 +00002025 if (CLHS->isNullValue()) {
2026 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002027 computeKnownBits(U->getOperand(1), KnownZero, KnownOne, DL, Depth + 1,
2028 Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002029 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2030 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002031 if ((KnownZero | APInt(TyBits, 1)).isAllOnesValue())
Chris Lattner965c7692008-06-02 01:18:21 +00002032 return TyBits;
Craig Topper1bef2c82012-12-22 19:15:35 +00002033
Chris Lattner965c7692008-06-02 01:18:21 +00002034 // If the input is known to be positive (the sign bit is known clear),
2035 // the output of the NEG has the same number of sign bits as the input.
2036 if (KnownZero.isNegative())
2037 return Tmp2;
Craig Topper1bef2c82012-12-22 19:15:35 +00002038
Chris Lattner965c7692008-06-02 01:18:21 +00002039 // Otherwise, we treat this like a SUB.
2040 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002041
Chris Lattner965c7692008-06-02 01:18:21 +00002042 // Sub can have at most one carry bit. Thus we know that the output
2043 // is, at worst, one more bit than the inputs.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002044 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002045 if (Tmp == 1) return 1; // Early out.
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002046 return std::min(Tmp, Tmp2)-1;
Craig Topper1bef2c82012-12-22 19:15:35 +00002047
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002048 case Instruction::PHI: {
2049 PHINode *PN = cast<PHINode>(U);
David Majnemer6ee8d172015-01-04 07:06:53 +00002050 unsigned NumIncomingValues = PN->getNumIncomingValues();
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002051 // Don't analyze large in-degree PHIs.
David Majnemer6ee8d172015-01-04 07:06:53 +00002052 if (NumIncomingValues > 4) break;
2053 // Unreachable blocks may have zero-operand PHI nodes.
2054 if (NumIncomingValues == 0) break;
Craig Topper1bef2c82012-12-22 19:15:35 +00002055
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002056 // Take the minimum of all incoming values. This can't infinitely loop
2057 // because of our depth threshold.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002058 Tmp = ComputeNumSignBits(PN->getIncomingValue(0), DL, Depth + 1, Q);
David Majnemer6ee8d172015-01-04 07:06:53 +00002059 for (unsigned i = 1, e = NumIncomingValues; i != e; ++i) {
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002060 if (Tmp == 1) return Tmp;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002061 Tmp = std::min(
2062 Tmp, ComputeNumSignBits(PN->getIncomingValue(i), DL, Depth + 1, Q));
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002063 }
2064 return Tmp;
2065 }
2066
Chris Lattner965c7692008-06-02 01:18:21 +00002067 case Instruction::Trunc:
2068 // FIXME: it's tricky to do anything useful for this, but it is an important
2069 // case for targets like X86.
2070 break;
2071 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002072
Chris Lattner965c7692008-06-02 01:18:21 +00002073 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2074 // use this information.
2075 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002076 APInt Mask;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002077 computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q);
Craig Topper1bef2c82012-12-22 19:15:35 +00002078
Chris Lattner965c7692008-06-02 01:18:21 +00002079 if (KnownZero.isNegative()) { // sign bit is 0
2080 Mask = KnownZero;
2081 } else if (KnownOne.isNegative()) { // sign bit is 1;
2082 Mask = KnownOne;
2083 } else {
2084 // Nothing known.
2085 return FirstAnswer;
2086 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002087
Chris Lattner965c7692008-06-02 01:18:21 +00002088 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2089 // the number of identical bits in the top of the input value.
2090 Mask = ~Mask;
2091 Mask <<= Mask.getBitWidth()-TyBits;
2092 // Return # leading zeros. We use 'min' here in case Val was zero before
2093 // shifting. We don't want to return '64' as for an i32 "0".
2094 return std::max(FirstAnswer, std::min(TyBits, Mask.countLeadingZeros()));
2095}
Chris Lattnera12a6de2008-06-02 01:29:46 +00002096
Sanjay Patelaee84212014-11-04 16:27:42 +00002097/// This function computes the integer multiple of Base that equals V.
2098/// If successful, it returns true and returns the multiple in
2099/// Multiple. If unsuccessful, it returns false. It looks
Victor Hernandez47444882009-11-10 08:28:35 +00002100/// through SExt instructions only if LookThroughSExt is true.
2101bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
Dan Gohman6a976bb2009-11-18 00:58:27 +00002102 bool LookThroughSExt, unsigned Depth) {
Victor Hernandez47444882009-11-10 08:28:35 +00002103 const unsigned MaxDepth = 6;
2104
Dan Gohman6a976bb2009-11-18 00:58:27 +00002105 assert(V && "No Value?");
Victor Hernandez47444882009-11-10 08:28:35 +00002106 assert(Depth <= MaxDepth && "Limit Search Depth");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002107 assert(V->getType()->isIntegerTy() && "Not integer or pointer type!");
Victor Hernandez47444882009-11-10 08:28:35 +00002108
Chris Lattner229907c2011-07-18 04:54:35 +00002109 Type *T = V->getType();
Victor Hernandez47444882009-11-10 08:28:35 +00002110
Dan Gohman6a976bb2009-11-18 00:58:27 +00002111 ConstantInt *CI = dyn_cast<ConstantInt>(V);
Victor Hernandez47444882009-11-10 08:28:35 +00002112
2113 if (Base == 0)
2114 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00002115
Victor Hernandez47444882009-11-10 08:28:35 +00002116 if (Base == 1) {
2117 Multiple = V;
2118 return true;
2119 }
2120
2121 ConstantExpr *CO = dyn_cast<ConstantExpr>(V);
2122 Constant *BaseVal = ConstantInt::get(T, Base);
2123 if (CO && CO == BaseVal) {
2124 // Multiple is 1.
2125 Multiple = ConstantInt::get(T, 1);
2126 return true;
2127 }
2128
2129 if (CI && CI->getZExtValue() % Base == 0) {
2130 Multiple = ConstantInt::get(T, CI->getZExtValue() / Base);
Craig Topper1bef2c82012-12-22 19:15:35 +00002131 return true;
Victor Hernandez47444882009-11-10 08:28:35 +00002132 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002133
Victor Hernandez47444882009-11-10 08:28:35 +00002134 if (Depth == MaxDepth) return false; // Limit search depth.
Craig Topper1bef2c82012-12-22 19:15:35 +00002135
Victor Hernandez47444882009-11-10 08:28:35 +00002136 Operator *I = dyn_cast<Operator>(V);
2137 if (!I) return false;
2138
2139 switch (I->getOpcode()) {
2140 default: break;
Chris Lattner4f0b47d2009-11-26 01:50:12 +00002141 case Instruction::SExt:
Victor Hernandez47444882009-11-10 08:28:35 +00002142 if (!LookThroughSExt) return false;
2143 // otherwise fall through to ZExt
Chris Lattner4f0b47d2009-11-26 01:50:12 +00002144 case Instruction::ZExt:
Dan Gohman6a976bb2009-11-18 00:58:27 +00002145 return ComputeMultiple(I->getOperand(0), Base, Multiple,
2146 LookThroughSExt, Depth+1);
Victor Hernandez47444882009-11-10 08:28:35 +00002147 case Instruction::Shl:
2148 case Instruction::Mul: {
2149 Value *Op0 = I->getOperand(0);
2150 Value *Op1 = I->getOperand(1);
2151
2152 if (I->getOpcode() == Instruction::Shl) {
2153 ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1);
2154 if (!Op1CI) return false;
2155 // Turn Op0 << Op1 into Op0 * 2^Op1
2156 APInt Op1Int = Op1CI->getValue();
2157 uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
Jay Foad15084f02010-11-30 09:02:01 +00002158 APInt API(Op1Int.getBitWidth(), 0);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002159 API.setBit(BitToSet);
Jay Foad15084f02010-11-30 09:02:01 +00002160 Op1 = ConstantInt::get(V->getContext(), API);
Victor Hernandez47444882009-11-10 08:28:35 +00002161 }
2162
Craig Topper9f008862014-04-15 04:59:12 +00002163 Value *Mul0 = nullptr;
Chris Lattner72d283c2010-09-05 17:20:46 +00002164 if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) {
2165 if (Constant *Op1C = dyn_cast<Constant>(Op1))
2166 if (Constant *MulC = dyn_cast<Constant>(Mul0)) {
Craig Topper1bef2c82012-12-22 19:15:35 +00002167 if (Op1C->getType()->getPrimitiveSizeInBits() <
Chris Lattner72d283c2010-09-05 17:20:46 +00002168 MulC->getType()->getPrimitiveSizeInBits())
2169 Op1C = ConstantExpr::getZExt(Op1C, MulC->getType());
Craig Topper1bef2c82012-12-22 19:15:35 +00002170 if (Op1C->getType()->getPrimitiveSizeInBits() >
Chris Lattner72d283c2010-09-05 17:20:46 +00002171 MulC->getType()->getPrimitiveSizeInBits())
2172 MulC = ConstantExpr::getZExt(MulC, Op1C->getType());
Craig Topper1bef2c82012-12-22 19:15:35 +00002173
Chris Lattner72d283c2010-09-05 17:20:46 +00002174 // V == Base * (Mul0 * Op1), so return (Mul0 * Op1)
2175 Multiple = ConstantExpr::getMul(MulC, Op1C);
2176 return true;
2177 }
Victor Hernandez47444882009-11-10 08:28:35 +00002178
2179 if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0))
2180 if (Mul0CI->getValue() == 1) {
2181 // V == Base * Op1, so return Op1
2182 Multiple = Op1;
2183 return true;
2184 }
2185 }
2186
Craig Topper9f008862014-04-15 04:59:12 +00002187 Value *Mul1 = nullptr;
Chris Lattner72d283c2010-09-05 17:20:46 +00002188 if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) {
2189 if (Constant *Op0C = dyn_cast<Constant>(Op0))
2190 if (Constant *MulC = dyn_cast<Constant>(Mul1)) {
Craig Topper1bef2c82012-12-22 19:15:35 +00002191 if (Op0C->getType()->getPrimitiveSizeInBits() <
Chris Lattner72d283c2010-09-05 17:20:46 +00002192 MulC->getType()->getPrimitiveSizeInBits())
2193 Op0C = ConstantExpr::getZExt(Op0C, MulC->getType());
Craig Topper1bef2c82012-12-22 19:15:35 +00002194 if (Op0C->getType()->getPrimitiveSizeInBits() >
Chris Lattner72d283c2010-09-05 17:20:46 +00002195 MulC->getType()->getPrimitiveSizeInBits())
2196 MulC = ConstantExpr::getZExt(MulC, Op0C->getType());
Craig Topper1bef2c82012-12-22 19:15:35 +00002197
Chris Lattner72d283c2010-09-05 17:20:46 +00002198 // V == Base * (Mul1 * Op0), so return (Mul1 * Op0)
2199 Multiple = ConstantExpr::getMul(MulC, Op0C);
2200 return true;
2201 }
Victor Hernandez47444882009-11-10 08:28:35 +00002202
2203 if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1))
2204 if (Mul1CI->getValue() == 1) {
2205 // V == Base * Op0, so return Op0
2206 Multiple = Op0;
2207 return true;
2208 }
2209 }
Victor Hernandez47444882009-11-10 08:28:35 +00002210 }
2211 }
2212
2213 // We could not determine if V is a multiple of Base.
2214 return false;
2215}
2216
Sanjay Patelaee84212014-11-04 16:27:42 +00002217/// Return true if we can prove that the specified FP value is never equal to
2218/// -0.0.
Chris Lattnera12a6de2008-06-02 01:29:46 +00002219///
2220/// NOTE: this function will need to be revisited when we support non-default
2221/// rounding modes!
2222///
2223bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) {
2224 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
2225 return !CFP->getValueAPF().isNegZero();
Craig Topper1bef2c82012-12-22 19:15:35 +00002226
Sanjay Patel40eaa8d2015-02-25 18:00:15 +00002227 // FIXME: Magic number! At the least, this should be given a name because it's
2228 // used similarly in CannotBeOrderedLessThanZero(). A better fix may be to
2229 // expose it as a parameter, so it can be used for testing / experimenting.
Chris Lattnera12a6de2008-06-02 01:29:46 +00002230 if (Depth == 6)
Sanjay Patel40eaa8d2015-02-25 18:00:15 +00002231 return false; // Limit search depth.
Chris Lattnera12a6de2008-06-02 01:29:46 +00002232
Dan Gohman80ca01c2009-07-17 20:47:02 +00002233 const Operator *I = dyn_cast<Operator>(V);
Craig Topper9f008862014-04-15 04:59:12 +00002234 if (!I) return false;
Michael Ilseman0f128372012-12-06 00:07:09 +00002235
2236 // Check if the nsz fast-math flag is set
2237 if (const FPMathOperator *FPO = dyn_cast<FPMathOperator>(I))
2238 if (FPO->hasNoSignedZeros())
2239 return true;
2240
Chris Lattnera12a6de2008-06-02 01:29:46 +00002241 // (add x, 0.0) is guaranteed to return +0.0, not -0.0.
Jakub Staszakb7129f22013-03-06 00:16:16 +00002242 if (I->getOpcode() == Instruction::FAdd)
2243 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I->getOperand(1)))
2244 if (CFP->isNullValue())
2245 return true;
Craig Topper1bef2c82012-12-22 19:15:35 +00002246
Chris Lattnera12a6de2008-06-02 01:29:46 +00002247 // sitofp and uitofp turn into +0.0 for zero.
2248 if (isa<SIToFPInst>(I) || isa<UIToFPInst>(I))
2249 return true;
Craig Topper1bef2c82012-12-22 19:15:35 +00002250
Chris Lattnera12a6de2008-06-02 01:29:46 +00002251 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2252 // sqrt(-0.0) = -0.0, no other negative results are possible.
2253 if (II->getIntrinsicID() == Intrinsic::sqrt)
Gabor Greif1abbde32010-06-23 23:38:07 +00002254 return CannotBeNegativeZero(II->getArgOperand(0), Depth+1);
Craig Topper1bef2c82012-12-22 19:15:35 +00002255
Chris Lattnera12a6de2008-06-02 01:29:46 +00002256 if (const CallInst *CI = dyn_cast<CallInst>(I))
2257 if (const Function *F = CI->getCalledFunction()) {
2258 if (F->isDeclaration()) {
Daniel Dunbarca414c72009-07-26 08:34:35 +00002259 // abs(x) != -0.0
2260 if (F->getName() == "abs") return true;
Dale Johannesenf6a987b2009-09-25 20:54:50 +00002261 // fabs[lf](x) != -0.0
2262 if (F->getName() == "fabs") return true;
2263 if (F->getName() == "fabsf") return true;
2264 if (F->getName() == "fabsl") return true;
2265 if (F->getName() == "sqrt" || F->getName() == "sqrtf" ||
2266 F->getName() == "sqrtl")
Gabor Greif1abbde32010-06-23 23:38:07 +00002267 return CannotBeNegativeZero(CI->getArgOperand(0), Depth+1);
Chris Lattnera12a6de2008-06-02 01:29:46 +00002268 }
2269 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002270
Chris Lattnera12a6de2008-06-02 01:29:46 +00002271 return false;
2272}
2273
Elena Demikhovsky45f04482015-01-28 08:03:58 +00002274bool llvm::CannotBeOrderedLessThanZero(const Value *V, unsigned Depth) {
2275 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
2276 return !CFP->getValueAPF().isNegative() || CFP->getValueAPF().isZero();
2277
Sanjay Patel40eaa8d2015-02-25 18:00:15 +00002278 // FIXME: Magic number! At the least, this should be given a name because it's
2279 // used similarly in CannotBeNegativeZero(). A better fix may be to
2280 // expose it as a parameter, so it can be used for testing / experimenting.
Elena Demikhovsky45f04482015-01-28 08:03:58 +00002281 if (Depth == 6)
2282 return false; // Limit search depth.
2283
2284 const Operator *I = dyn_cast<Operator>(V);
2285 if (!I) return false;
2286
2287 switch (I->getOpcode()) {
2288 default: break;
2289 case Instruction::FMul:
2290 // x*x is always non-negative or a NaN.
2291 if (I->getOperand(0) == I->getOperand(1))
2292 return true;
2293 // Fall through
2294 case Instruction::FAdd:
2295 case Instruction::FDiv:
2296 case Instruction::FRem:
2297 return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1) &&
2298 CannotBeOrderedLessThanZero(I->getOperand(1), Depth+1);
2299 case Instruction::FPExt:
2300 case Instruction::FPTrunc:
2301 // Widening/narrowing never change sign.
2302 return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1);
2303 case Instruction::Call:
2304 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2305 switch (II->getIntrinsicID()) {
2306 default: break;
2307 case Intrinsic::exp:
2308 case Intrinsic::exp2:
2309 case Intrinsic::fabs:
2310 case Intrinsic::sqrt:
2311 return true;
2312 case Intrinsic::powi:
2313 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
2314 // powi(x,n) is non-negative if n is even.
2315 if (CI->getBitWidth() <= 64 && CI->getSExtValue() % 2u == 0)
2316 return true;
2317 }
2318 return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1);
2319 case Intrinsic::fma:
2320 case Intrinsic::fmuladd:
2321 // x*x+y is non-negative if y is non-negative.
2322 return I->getOperand(0) == I->getOperand(1) &&
2323 CannotBeOrderedLessThanZero(I->getOperand(2), Depth+1);
2324 }
2325 break;
2326 }
2327 return false;
2328}
2329
Sanjay Patelaee84212014-11-04 16:27:42 +00002330/// If the specified value can be set by repeating the same byte in memory,
2331/// return the i8 value that it is represented with. This is
Chris Lattner9cb10352010-12-26 20:15:01 +00002332/// true for all i8 values obviously, but is also true for i32 0, i32 -1,
2333/// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated
2334/// byte store (e.g. i16 0x1234), return null.
2335Value *llvm::isBytewiseValue(Value *V) {
2336 // All byte-wide stores are splatable, even of arbitrary variables.
2337 if (V->getType()->isIntegerTy(8)) return V;
Chris Lattneracf6b072011-02-19 19:35:49 +00002338
2339 // Handle 'null' ConstantArrayZero etc.
2340 if (Constant *C = dyn_cast<Constant>(V))
2341 if (C->isNullValue())
2342 return Constant::getNullValue(Type::getInt8Ty(V->getContext()));
Craig Topper1bef2c82012-12-22 19:15:35 +00002343
Chris Lattner9cb10352010-12-26 20:15:01 +00002344 // Constant float and double values can be handled as integer values if the
Craig Topper1bef2c82012-12-22 19:15:35 +00002345 // corresponding integer value is "byteable". An important case is 0.0.
Chris Lattner9cb10352010-12-26 20:15:01 +00002346 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
2347 if (CFP->getType()->isFloatTy())
2348 V = ConstantExpr::getBitCast(CFP, Type::getInt32Ty(V->getContext()));
2349 if (CFP->getType()->isDoubleTy())
2350 V = ConstantExpr::getBitCast(CFP, Type::getInt64Ty(V->getContext()));
2351 // Don't handle long double formats, which have strange constraints.
2352 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002353
Benjamin Kramer17d90152015-02-07 19:29:02 +00002354 // We can handle constant integers that are multiple of 8 bits.
Chris Lattner9cb10352010-12-26 20:15:01 +00002355 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Benjamin Kramer17d90152015-02-07 19:29:02 +00002356 if (CI->getBitWidth() % 8 == 0) {
2357 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
Craig Topper1bef2c82012-12-22 19:15:35 +00002358
Benjamin Kramerb4b51502015-03-25 16:49:59 +00002359 if (!CI->getValue().isSplat(8))
Benjamin Kramer17d90152015-02-07 19:29:02 +00002360 return nullptr;
2361 return ConstantInt::get(V->getContext(), CI->getValue().trunc(8));
Chris Lattner9cb10352010-12-26 20:15:01 +00002362 }
2363 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002364
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002365 // A ConstantDataArray/Vector is splatable if all its members are equal and
2366 // also splatable.
2367 if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(V)) {
2368 Value *Elt = CA->getElementAsConstant(0);
2369 Value *Val = isBytewiseValue(Elt);
Chris Lattner9cb10352010-12-26 20:15:01 +00002370 if (!Val)
Craig Topper9f008862014-04-15 04:59:12 +00002371 return nullptr;
Craig Topper1bef2c82012-12-22 19:15:35 +00002372
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002373 for (unsigned I = 1, E = CA->getNumElements(); I != E; ++I)
2374 if (CA->getElementAsConstant(I) != Elt)
Craig Topper9f008862014-04-15 04:59:12 +00002375 return nullptr;
Craig Topper1bef2c82012-12-22 19:15:35 +00002376
Chris Lattner9cb10352010-12-26 20:15:01 +00002377 return Val;
2378 }
Chad Rosier8abf65a2011-12-06 00:19:08 +00002379
Chris Lattner9cb10352010-12-26 20:15:01 +00002380 // Conceptually, we could handle things like:
2381 // %a = zext i8 %X to i16
2382 // %b = shl i16 %a, 8
2383 // %c = or i16 %a, %b
2384 // but until there is an example that actually needs this, it doesn't seem
2385 // worth worrying about.
Craig Topper9f008862014-04-15 04:59:12 +00002386 return nullptr;
Chris Lattner9cb10352010-12-26 20:15:01 +00002387}
2388
2389
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002390// This is the recursive version of BuildSubAggregate. It takes a few different
2391// arguments. Idxs is the index within the nested struct From that we are
2392// looking at now (which is of type IndexedType). IdxSkip is the number of
2393// indices from Idxs that should be left out when inserting into the resulting
2394// struct. To is the result struct built so far, new insertvalue instructions
2395// build on that.
Chris Lattner229907c2011-07-18 04:54:35 +00002396static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType,
Craig Topper2cd5ff82013-07-11 16:22:38 +00002397 SmallVectorImpl<unsigned> &Idxs,
Dan Gohmana6d0afc2009-08-07 01:32:21 +00002398 unsigned IdxSkip,
Dan Gohmana6d0afc2009-08-07 01:32:21 +00002399 Instruction *InsertBefore) {
Dmitri Gribenko226fea52013-01-13 16:01:15 +00002400 llvm::StructType *STy = dyn_cast<llvm::StructType>(IndexedType);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002401 if (STy) {
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002402 // Save the original To argument so we can modify it
2403 Value *OrigTo = To;
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002404 // General case, the type indexed by Idxs is a struct
2405 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
2406 // Process each struct element recursively
2407 Idxs.push_back(i);
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002408 Value *PrevTo = To;
Matthijs Kooijman5cb38772008-06-16 12:57:37 +00002409 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
Nick Lewycky39dbfd32009-11-23 03:29:18 +00002410 InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002411 Idxs.pop_back();
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002412 if (!To) {
2413 // Couldn't find any inserted value for this index? Cleanup
2414 while (PrevTo != OrigTo) {
2415 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
2416 PrevTo = Del->getAggregateOperand();
2417 Del->eraseFromParent();
2418 }
2419 // Stop processing elements
2420 break;
2421 }
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002422 }
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00002423 // If we successfully found a value for each of our subaggregates
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002424 if (To)
2425 return To;
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002426 }
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002427 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
2428 // the struct's elements had a value that was inserted directly. In the latter
2429 // case, perhaps we can't determine each of the subelements individually, but
2430 // we might be able to find the complete struct somewhere.
Craig Topper1bef2c82012-12-22 19:15:35 +00002431
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002432 // Find the value that is at that particular spot
Jay Foad57aa6362011-07-13 10:26:04 +00002433 Value *V = FindInsertedValue(From, Idxs);
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002434
2435 if (!V)
Craig Topper9f008862014-04-15 04:59:12 +00002436 return nullptr;
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002437
2438 // Insert the value in the new (sub) aggregrate
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002439 return llvm::InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip),
Jay Foad57aa6362011-07-13 10:26:04 +00002440 "tmp", InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002441}
2442
2443// This helper takes a nested struct and extracts a part of it (which is again a
2444// struct) into a new value. For example, given the struct:
2445// { a, { b, { c, d }, e } }
2446// and the indices "1, 1" this returns
2447// { c, d }.
2448//
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002449// It does this by inserting an insertvalue for each element in the resulting
2450// struct, as opposed to just inserting a single struct. This will only work if
2451// each of the elements of the substruct are known (ie, inserted into From by an
2452// insertvalue instruction somewhere).
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002453//
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002454// All inserted insertvalue instructions are inserted before InsertBefore
Jay Foad57aa6362011-07-13 10:26:04 +00002455static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range,
Dan Gohmana6d0afc2009-08-07 01:32:21 +00002456 Instruction *InsertBefore) {
Matthijs Kooijman69801d42008-06-16 13:28:31 +00002457 assert(InsertBefore && "Must have someplace to insert!");
Chris Lattner229907c2011-07-18 04:54:35 +00002458 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
Jay Foad57aa6362011-07-13 10:26:04 +00002459 idx_range);
Owen Andersonb292b8c2009-07-30 23:03:37 +00002460 Value *To = UndefValue::get(IndexedType);
Jay Foad57aa6362011-07-13 10:26:04 +00002461 SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end());
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002462 unsigned IdxSkip = Idxs.size();
2463
Nick Lewycky39dbfd32009-11-23 03:29:18 +00002464 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002465}
2466
Sanjay Patelaee84212014-11-04 16:27:42 +00002467/// Given an aggregrate and an sequence of indices, see if
Matthijs Kooijman5cb38772008-06-16 12:57:37 +00002468/// the scalar value indexed is already around as a register, for example if it
2469/// were inserted directly into the aggregrate.
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002470///
2471/// If InsertBefore is not null, this function will duplicate (modified)
2472/// insertvalues when a part of a nested struct is extracted.
Jay Foad57aa6362011-07-13 10:26:04 +00002473Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range,
2474 Instruction *InsertBefore) {
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002475 // Nothing to index? Just return V then (this is useful at the end of our
Chris Lattnerf7eb5432012-01-24 07:54:10 +00002476 // recursion).
Jay Foad57aa6362011-07-13 10:26:04 +00002477 if (idx_range.empty())
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002478 return V;
Chris Lattnerf7eb5432012-01-24 07:54:10 +00002479 // We have indices, so V should have an indexable type.
2480 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
2481 "Not looking at a struct or array?");
2482 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
2483 "Invalid indices for type?");
Owen Andersonf1f17432009-07-06 22:37:39 +00002484
Chris Lattner67058832012-01-25 06:48:06 +00002485 if (Constant *C = dyn_cast<Constant>(V)) {
2486 C = C->getAggregateElement(idx_range[0]);
Craig Topper9f008862014-04-15 04:59:12 +00002487 if (!C) return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +00002488 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
2489 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002490
Chris Lattnerf7eb5432012-01-24 07:54:10 +00002491 if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002492 // Loop the indices for the insertvalue instruction in parallel with the
2493 // requested indices
Jay Foad57aa6362011-07-13 10:26:04 +00002494 const unsigned *req_idx = idx_range.begin();
Matthijs Kooijman5cb38772008-06-16 12:57:37 +00002495 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
2496 i != e; ++i, ++req_idx) {
Jay Foad57aa6362011-07-13 10:26:04 +00002497 if (req_idx == idx_range.end()) {
Chris Lattnerf7eb5432012-01-24 07:54:10 +00002498 // We can't handle this without inserting insertvalues
2499 if (!InsertBefore)
Craig Topper9f008862014-04-15 04:59:12 +00002500 return nullptr;
Chris Lattnerf7eb5432012-01-24 07:54:10 +00002501
2502 // The requested index identifies a part of a nested aggregate. Handle
2503 // this specially. For example,
2504 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
2505 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
2506 // %C = extractvalue {i32, { i32, i32 } } %B, 1
2507 // This can be changed into
2508 // %A = insertvalue {i32, i32 } undef, i32 10, 0
2509 // %C = insertvalue {i32, i32 } %A, i32 11, 1
2510 // which allows the unused 0,0 element from the nested struct to be
2511 // removed.
2512 return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx),
2513 InsertBefore);
Duncan Sandsdb356ee2008-06-19 08:47:31 +00002514 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002515
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002516 // This insert value inserts something else than what we are looking for.
2517 // See if the (aggregrate) value inserted into has the value we are
2518 // looking for, then.
2519 if (*req_idx != *i)
Jay Foad57aa6362011-07-13 10:26:04 +00002520 return FindInsertedValue(I->getAggregateOperand(), idx_range,
Nick Lewycky39dbfd32009-11-23 03:29:18 +00002521 InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002522 }
2523 // If we end up here, the indices of the insertvalue match with those
2524 // requested (though possibly only partially). Now we recursively look at
2525 // the inserted value, passing any remaining indices.
Jay Foad57aa6362011-07-13 10:26:04 +00002526 return FindInsertedValue(I->getInsertedValueOperand(),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002527 makeArrayRef(req_idx, idx_range.end()),
Nick Lewycky39dbfd32009-11-23 03:29:18 +00002528 InsertBefore);
Chris Lattnerf7eb5432012-01-24 07:54:10 +00002529 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002530
Chris Lattnerf7eb5432012-01-24 07:54:10 +00002531 if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002532 // If we're extracting a value from an aggregrate that was extracted from
2533 // something else, we can extract from that something else directly instead.
2534 // However, we will need to chain I's indices with the requested indices.
Craig Topper1bef2c82012-12-22 19:15:35 +00002535
2536 // Calculate the number of indices required
Jay Foad57aa6362011-07-13 10:26:04 +00002537 unsigned size = I->getNumIndices() + idx_range.size();
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002538 // Allocate some space to put the new indices in
Matthijs Kooijman8369c672008-06-17 08:24:37 +00002539 SmallVector<unsigned, 5> Idxs;
2540 Idxs.reserve(size);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002541 // Add indices from the extract value instruction
Jay Foad57aa6362011-07-13 10:26:04 +00002542 Idxs.append(I->idx_begin(), I->idx_end());
Craig Topper1bef2c82012-12-22 19:15:35 +00002543
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002544 // Add requested indices
Jay Foad57aa6362011-07-13 10:26:04 +00002545 Idxs.append(idx_range.begin(), idx_range.end());
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002546
Craig Topper1bef2c82012-12-22 19:15:35 +00002547 assert(Idxs.size() == size
Matthijs Kooijman5cb38772008-06-16 12:57:37 +00002548 && "Number of indices added not correct?");
Craig Topper1bef2c82012-12-22 19:15:35 +00002549
Jay Foad57aa6362011-07-13 10:26:04 +00002550 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002551 }
2552 // Otherwise, we don't know (such as, extracting from a function return value
2553 // or load instruction)
Craig Topper9f008862014-04-15 04:59:12 +00002554 return nullptr;
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002555}
Evan Chengda3db112008-06-30 07:31:25 +00002556
Sanjay Patelaee84212014-11-04 16:27:42 +00002557/// Analyze the specified pointer to see if it can be expressed as a base
2558/// pointer plus a constant offset. Return the base and offset to the caller.
Chris Lattnere28618d2010-11-30 22:25:26 +00002559Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002560 const DataLayout &DL) {
2561 unsigned BitWidth = DL.getPointerTypeSizeInBits(Ptr->getType());
Nuno Lopes368c4d02012-12-31 20:48:35 +00002562 APInt ByteOffset(BitWidth, 0);
2563 while (1) {
2564 if (Ptr->getType()->isVectorTy())
2565 break;
Craig Topper1bef2c82012-12-22 19:15:35 +00002566
Nuno Lopes368c4d02012-12-31 20:48:35 +00002567 if (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002568 APInt GEPOffset(BitWidth, 0);
2569 if (!GEP->accumulateConstantOffset(DL, GEPOffset))
2570 break;
Matt Arsenaultf55e5e72013-08-10 17:34:08 +00002571
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002572 ByteOffset += GEPOffset;
Matt Arsenaultf55e5e72013-08-10 17:34:08 +00002573
Nuno Lopes368c4d02012-12-31 20:48:35 +00002574 Ptr = GEP->getPointerOperand();
Matt Arsenaultfd78d0c2014-07-14 22:39:22 +00002575 } else if (Operator::getOpcode(Ptr) == Instruction::BitCast ||
2576 Operator::getOpcode(Ptr) == Instruction::AddrSpaceCast) {
Nuno Lopes368c4d02012-12-31 20:48:35 +00002577 Ptr = cast<Operator>(Ptr)->getOperand(0);
2578 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) {
2579 if (GA->mayBeOverridden())
2580 break;
2581 Ptr = GA->getAliasee();
Chris Lattnere28618d2010-11-30 22:25:26 +00002582 } else {
Nuno Lopes368c4d02012-12-31 20:48:35 +00002583 break;
Chris Lattnere28618d2010-11-30 22:25:26 +00002584 }
2585 }
Nuno Lopes368c4d02012-12-31 20:48:35 +00002586 Offset = ByteOffset.getSExtValue();
2587 return Ptr;
Chris Lattnere28618d2010-11-30 22:25:26 +00002588}
2589
2590
Sanjay Patelaee84212014-11-04 16:27:42 +00002591/// This function computes the length of a null-terminated C string pointed to
2592/// by V. If successful, it returns true and returns the string in Str.
2593/// If unsuccessful, it returns false.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002594bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
2595 uint64_t Offset, bool TrimAtNul) {
2596 assert(V);
Evan Chengda3db112008-06-30 07:31:25 +00002597
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002598 // Look through bitcast instructions and geps.
2599 V = V->stripPointerCasts();
Craig Topper1bef2c82012-12-22 19:15:35 +00002600
Benjamin Kramer0248a3e2015-03-21 15:36:06 +00002601 // If the value is a GEP instruction or constant expression, treat it as an
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002602 // offset.
2603 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Evan Chengda3db112008-06-30 07:31:25 +00002604 // Make sure the GEP has exactly three arguments.
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002605 if (GEP->getNumOperands() != 3)
2606 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00002607
Evan Chengda3db112008-06-30 07:31:25 +00002608 // Make sure the index-ee is a pointer to array of i8.
Chris Lattner229907c2011-07-18 04:54:35 +00002609 PointerType *PT = cast<PointerType>(GEP->getOperand(0)->getType());
2610 ArrayType *AT = dyn_cast<ArrayType>(PT->getElementType());
Craig Topper9f008862014-04-15 04:59:12 +00002611 if (!AT || !AT->getElementType()->isIntegerTy(8))
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002612 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00002613
Evan Chengda3db112008-06-30 07:31:25 +00002614 // Check to make sure that the first operand of the GEP is an integer and
2615 // has value 0 so that we are sure we're indexing into the initializer.
Dan Gohman0b4df042010-04-14 22:20:45 +00002616 const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
Craig Topper9f008862014-04-15 04:59:12 +00002617 if (!FirstIdx || !FirstIdx->isZero())
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002618 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00002619
Evan Chengda3db112008-06-30 07:31:25 +00002620 // If the second index isn't a ConstantInt, then this is a variable index
2621 // into the array. If this occurs, we can't say anything meaningful about
2622 // the string.
2623 uint64_t StartIdx = 0;
Dan Gohman0b4df042010-04-14 22:20:45 +00002624 if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
Evan Chengda3db112008-06-30 07:31:25 +00002625 StartIdx = CI->getZExtValue();
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002626 else
2627 return false;
Benjamin Kramer0248a3e2015-03-21 15:36:06 +00002628 return getConstantStringInfo(GEP->getOperand(0), Str, StartIdx + Offset,
2629 TrimAtNul);
Evan Chengda3db112008-06-30 07:31:25 +00002630 }
Nick Lewycky46209882011-10-20 00:34:35 +00002631
Evan Chengda3db112008-06-30 07:31:25 +00002632 // The GEP instruction, constant or instruction, must reference a global
2633 // variable that is a constant and is initialized. The referenced constant
2634 // initializer is the array that we'll use for optimization.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002635 const GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
Dan Gohman5d5bc6d2009-08-19 18:20:44 +00002636 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002637 return false;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002638
Nick Lewycky46209882011-10-20 00:34:35 +00002639 // Handle the all-zeros case
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002640 if (GV->getInitializer()->isNullValue()) {
Evan Chengda3db112008-06-30 07:31:25 +00002641 // This is a degenerate case. The initializer is constant zero so the
2642 // length of the string must be zero.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002643 Str = "";
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002644 return true;
2645 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002646
Evan Chengda3db112008-06-30 07:31:25 +00002647 // Must be a Constant Array
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002648 const ConstantDataArray *Array =
2649 dyn_cast<ConstantDataArray>(GV->getInitializer());
Craig Topper9f008862014-04-15 04:59:12 +00002650 if (!Array || !Array->isString())
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002651 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00002652
Evan Chengda3db112008-06-30 07:31:25 +00002653 // Get the number of elements in the array
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002654 uint64_t NumElts = Array->getType()->getArrayNumElements();
2655
2656 // Start out with the entire array in the StringRef.
2657 Str = Array->getAsString();
2658
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002659 if (Offset > NumElts)
2660 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00002661
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002662 // Skip over 'offset' bytes.
2663 Str = Str.substr(Offset);
Craig Topper1bef2c82012-12-22 19:15:35 +00002664
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002665 if (TrimAtNul) {
2666 // Trim off the \0 and anything after it. If the array is not nul
2667 // terminated, we just return the whole end of string. The client may know
2668 // some other way that the string is length-bound.
2669 Str = Str.substr(0, Str.find('\0'));
2670 }
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002671 return true;
Evan Chengda3db112008-06-30 07:31:25 +00002672}
Eric Christopher4899cbc2010-03-05 06:58:57 +00002673
2674// These next two are very similar to the above, but also look through PHI
2675// nodes.
2676// TODO: See if we can integrate these two together.
2677
Sanjay Patelaee84212014-11-04 16:27:42 +00002678/// If we can compute the length of the string pointed to by
Eric Christopher4899cbc2010-03-05 06:58:57 +00002679/// the specified pointer, return 'len+1'. If we can't, return 0.
Craig Topper71b7b682014-08-21 05:55:13 +00002680static uint64_t GetStringLengthH(Value *V, SmallPtrSetImpl<PHINode*> &PHIs) {
Eric Christopher4899cbc2010-03-05 06:58:57 +00002681 // Look through noop bitcast instructions.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002682 V = V->stripPointerCasts();
Eric Christopher4899cbc2010-03-05 06:58:57 +00002683
2684 // If this is a PHI node, there are two cases: either we have already seen it
2685 // or we haven't.
2686 if (PHINode *PN = dyn_cast<PHINode>(V)) {
David Blaikie70573dc2014-11-19 07:49:26 +00002687 if (!PHIs.insert(PN).second)
Eric Christopher4899cbc2010-03-05 06:58:57 +00002688 return ~0ULL; // already in the set.
2689
2690 // If it was new, see if all the input strings are the same length.
2691 uint64_t LenSoFar = ~0ULL;
2692 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2693 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
2694 if (Len == 0) return 0; // Unknown length -> unknown.
2695
2696 if (Len == ~0ULL) continue;
2697
2698 if (Len != LenSoFar && LenSoFar != ~0ULL)
2699 return 0; // Disagree -> unknown.
2700 LenSoFar = Len;
2701 }
2702
2703 // Success, all agree.
2704 return LenSoFar;
2705 }
2706
2707 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
2708 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
2709 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
2710 if (Len1 == 0) return 0;
2711 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
2712 if (Len2 == 0) return 0;
2713 if (Len1 == ~0ULL) return Len2;
2714 if (Len2 == ~0ULL) return Len1;
2715 if (Len1 != Len2) return 0;
2716 return Len1;
2717 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002718
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002719 // Otherwise, see if we can read the string.
2720 StringRef StrData;
2721 if (!getConstantStringInfo(V, StrData))
Eric Christopher4899cbc2010-03-05 06:58:57 +00002722 return 0;
2723
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002724 return StrData.size()+1;
Eric Christopher4899cbc2010-03-05 06:58:57 +00002725}
2726
Sanjay Patelaee84212014-11-04 16:27:42 +00002727/// If we can compute the length of the string pointed to by
Eric Christopher4899cbc2010-03-05 06:58:57 +00002728/// the specified pointer, return 'len+1'. If we can't, return 0.
2729uint64_t llvm::GetStringLength(Value *V) {
2730 if (!V->getType()->isPointerTy()) return 0;
2731
2732 SmallPtrSet<PHINode*, 32> PHIs;
2733 uint64_t Len = GetStringLengthH(V, PHIs);
2734 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
2735 // an empty string as a length.
2736 return Len == ~0ULL ? 1 : Len;
2737}
Dan Gohmana4fcd242010-12-15 20:02:24 +00002738
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002739Value *llvm::GetUnderlyingObject(Value *V, const DataLayout &DL,
2740 unsigned MaxLookup) {
Dan Gohmana4fcd242010-12-15 20:02:24 +00002741 if (!V->getType()->isPointerTy())
2742 return V;
2743 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
2744 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
2745 V = GEP->getPointerOperand();
Matt Arsenault70f4db882014-07-15 00:56:40 +00002746 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
2747 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
Dan Gohmana4fcd242010-12-15 20:02:24 +00002748 V = cast<Operator>(V)->getOperand(0);
2749 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2750 if (GA->mayBeOverridden())
2751 return V;
2752 V = GA->getAliasee();
2753 } else {
Dan Gohman05b18f12010-12-15 20:49:55 +00002754 // See if InstructionSimplify knows any relevant tricks.
2755 if (Instruction *I = dyn_cast<Instruction>(V))
Chandler Carruth66b31302015-01-04 12:03:27 +00002756 // TODO: Acquire a DominatorTree and AssumptionCache and use them.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002757 if (Value *Simplified = SimplifyInstruction(I, DL, nullptr)) {
Dan Gohman05b18f12010-12-15 20:49:55 +00002758 V = Simplified;
2759 continue;
2760 }
2761
Dan Gohmana4fcd242010-12-15 20:02:24 +00002762 return V;
2763 }
2764 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
2765 }
2766 return V;
2767}
Nick Lewycky3e334a42011-06-27 04:20:45 +00002768
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002769void llvm::GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects,
2770 const DataLayout &DL, unsigned MaxLookup) {
Dan Gohmaned7c24e22012-05-10 18:57:38 +00002771 SmallPtrSet<Value *, 4> Visited;
2772 SmallVector<Value *, 4> Worklist;
2773 Worklist.push_back(V);
2774 do {
2775 Value *P = Worklist.pop_back_val();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002776 P = GetUnderlyingObject(P, DL, MaxLookup);
Dan Gohmaned7c24e22012-05-10 18:57:38 +00002777
David Blaikie70573dc2014-11-19 07:49:26 +00002778 if (!Visited.insert(P).second)
Dan Gohmaned7c24e22012-05-10 18:57:38 +00002779 continue;
2780
2781 if (SelectInst *SI = dyn_cast<SelectInst>(P)) {
2782 Worklist.push_back(SI->getTrueValue());
2783 Worklist.push_back(SI->getFalseValue());
2784 continue;
2785 }
2786
2787 if (PHINode *PN = dyn_cast<PHINode>(P)) {
2788 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
2789 Worklist.push_back(PN->getIncomingValue(i));
2790 continue;
2791 }
2792
2793 Objects.push_back(P);
2794 } while (!Worklist.empty());
2795}
2796
Sanjay Patelaee84212014-11-04 16:27:42 +00002797/// Return true if the only users of this pointer are lifetime markers.
Nick Lewycky3e334a42011-06-27 04:20:45 +00002798bool llvm::onlyUsedByLifetimeMarkers(const Value *V) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00002799 for (const User *U : V->users()) {
2800 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
Nick Lewycky3e334a42011-06-27 04:20:45 +00002801 if (!II) return false;
2802
2803 if (II->getIntrinsicID() != Intrinsic::lifetime_start &&
2804 II->getIntrinsicID() != Intrinsic::lifetime_end)
2805 return false;
2806 }
2807 return true;
2808}
Dan Gohman75d7d5e2011-12-14 23:49:11 +00002809
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002810bool llvm::isSafeToSpeculativelyExecute(const Value *V) {
Dan Gohman7ac046a2012-01-04 23:01:09 +00002811 const Operator *Inst = dyn_cast<Operator>(V);
2812 if (!Inst)
2813 return false;
2814
Dan Gohman75d7d5e2011-12-14 23:49:11 +00002815 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
2816 if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i)))
2817 if (C->canTrap())
2818 return false;
2819
2820 switch (Inst->getOpcode()) {
2821 default:
2822 return true;
2823 case Instruction::UDiv:
David Majnemerf20d7c42014-11-04 23:49:08 +00002824 case Instruction::URem: {
2825 // x / y is undefined if y == 0.
2826 const APInt *V;
2827 if (match(Inst->getOperand(1), m_APInt(V)))
2828 return *V != 0;
2829 return false;
2830 }
Dan Gohman75d7d5e2011-12-14 23:49:11 +00002831 case Instruction::SDiv:
2832 case Instruction::SRem: {
David Majnemerf20d7c42014-11-04 23:49:08 +00002833 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
David Majnemer8a6578a2015-02-01 19:10:19 +00002834 const APInt *Numerator, *Denominator;
2835 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
2836 return false;
2837 // We cannot hoist this division if the denominator is 0.
2838 if (*Denominator == 0)
2839 return false;
2840 // It's safe to hoist if the denominator is not 0 or -1.
2841 if (*Denominator != -1)
2842 return true;
2843 // At this point we know that the denominator is -1. It is safe to hoist as
2844 // long we know that the numerator is not INT_MIN.
2845 if (match(Inst->getOperand(0), m_APInt(Numerator)))
2846 return !Numerator->isMinSignedValue();
2847 // The numerator *might* be MinSignedValue.
David Majnemerf20d7c42014-11-04 23:49:08 +00002848 return false;
Dan Gohman75d7d5e2011-12-14 23:49:11 +00002849 }
2850 case Instruction::Load: {
2851 const LoadInst *LI = cast<LoadInst>(Inst);
Kostya Serebryany0b458282013-11-21 07:29:28 +00002852 if (!LI->isUnordered() ||
2853 // Speculative load may create a race that did not exist in the source.
2854 LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeThread))
Dan Gohman75d7d5e2011-12-14 23:49:11 +00002855 return false;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002856 const DataLayout &DL = LI->getModule()->getDataLayout();
2857 return LI->getPointerOperand()->isDereferenceablePointer(DL);
Dan Gohman75d7d5e2011-12-14 23:49:11 +00002858 }
Nick Lewyckyb4039f62011-12-21 05:52:02 +00002859 case Instruction::Call: {
Michael Liao736bac62014-11-06 19:05:57 +00002860 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
2861 switch (II->getIntrinsicID()) {
2862 // These synthetic intrinsics have no side-effects and just mark
2863 // information about their operands.
2864 // FIXME: There are other no-op synthetic instructions that potentially
2865 // should be considered at least *safe* to speculate...
2866 case Intrinsic::dbg_declare:
2867 case Intrinsic::dbg_value:
2868 return true;
Chandler Carruth28192c92012-04-07 19:22:18 +00002869
Michael Liao736bac62014-11-06 19:05:57 +00002870 case Intrinsic::bswap:
2871 case Intrinsic::ctlz:
2872 case Intrinsic::ctpop:
2873 case Intrinsic::cttz:
2874 case Intrinsic::objectsize:
2875 case Intrinsic::sadd_with_overflow:
2876 case Intrinsic::smul_with_overflow:
2877 case Intrinsic::ssub_with_overflow:
2878 case Intrinsic::uadd_with_overflow:
2879 case Intrinsic::umul_with_overflow:
2880 case Intrinsic::usub_with_overflow:
2881 return true;
2882 // Sqrt should be OK, since the llvm sqrt intrinsic isn't defined to set
2883 // errno like libm sqrt would.
2884 case Intrinsic::sqrt:
2885 case Intrinsic::fma:
2886 case Intrinsic::fmuladd:
2887 case Intrinsic::fabs:
2888 case Intrinsic::minnum:
2889 case Intrinsic::maxnum:
2890 return true;
2891 // TODO: some fp intrinsics are marked as having the same error handling
2892 // as libm. They're safe to speculate when they won't error.
2893 // TODO: are convert_{from,to}_fp16 safe?
2894 // TODO: can we list target-specific intrinsics here?
2895 default: break;
2896 }
2897 }
Dan Gohman75d7d5e2011-12-14 23:49:11 +00002898 return false; // The called function could have undefined behavior or
Nick Lewyckyb4039f62011-12-21 05:52:02 +00002899 // side-effects, even if marked readnone nounwind.
2900 }
Dan Gohman75d7d5e2011-12-14 23:49:11 +00002901 case Instruction::VAArg:
2902 case Instruction::Alloca:
2903 case Instruction::Invoke:
2904 case Instruction::PHI:
2905 case Instruction::Store:
2906 case Instruction::Ret:
2907 case Instruction::Br:
2908 case Instruction::IndirectBr:
2909 case Instruction::Switch:
Dan Gohman75d7d5e2011-12-14 23:49:11 +00002910 case Instruction::Unreachable:
2911 case Instruction::Fence:
2912 case Instruction::LandingPad:
2913 case Instruction::AtomicRMW:
2914 case Instruction::AtomicCmpXchg:
2915 case Instruction::Resume:
2916 return false; // Misc instructions which have effects
2917 }
2918}
Dan Gohman1b0f79d2013-01-31 02:40:59 +00002919
Sanjay Patelaee84212014-11-04 16:27:42 +00002920/// Return true if we know that the specified value is never null.
Benjamin Kramerfd4777c2013-09-24 16:37:51 +00002921bool llvm::isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI) {
Dan Gohman1b0f79d2013-01-31 02:40:59 +00002922 // Alloca never returns null, malloc might.
2923 if (isa<AllocaInst>(V)) return true;
2924
Nick Lewyckyd52b1522014-05-20 01:23:40 +00002925 // A byval, inalloca, or nonnull argument is never null.
Dan Gohman1b0f79d2013-01-31 02:40:59 +00002926 if (const Argument *A = dyn_cast<Argument>(V))
Nick Lewyckyd52b1522014-05-20 01:23:40 +00002927 return A->hasByValOrInAllocaAttr() || A->hasNonNullAttr();
Dan Gohman1b0f79d2013-01-31 02:40:59 +00002928
2929 // Global values are not null unless extern weak.
2930 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
2931 return !GV->hasExternalWeakLinkage();
Benjamin Kramerfd4777c2013-09-24 16:37:51 +00002932
Philip Reamescdb72f32014-10-20 22:40:55 +00002933 // A Load tagged w/nonnull metadata is never null.
2934 if (const LoadInst *LI = dyn_cast<LoadInst>(V))
Philip Reames5a3f5f72014-10-21 00:13:20 +00002935 return LI->getMetadata(LLVMContext::MD_nonnull);
Philip Reamescdb72f32014-10-20 22:40:55 +00002936
Nick Lewyckyec373542014-05-20 05:13:21 +00002937 if (ImmutableCallSite CS = V)
Hal Finkelb0407ba2014-07-18 15:51:28 +00002938 if (CS.isReturnNonNull())
Nick Lewyckyec373542014-05-20 05:13:21 +00002939 return true;
2940
Benjamin Kramerfd4777c2013-09-24 16:37:51 +00002941 // operator new never returns null.
2942 if (isOperatorNewLikeFn(V, TLI, /*LookThroughBitCast=*/true))
2943 return true;
2944
Dan Gohman1b0f79d2013-01-31 02:40:59 +00002945 return false;
2946}
David Majnemer491331a2015-01-02 07:29:43 +00002947
2948OverflowResult llvm::computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002949 const DataLayout &DL,
Chandler Carruth66b31302015-01-04 12:03:27 +00002950 AssumptionCache *AC,
David Majnemer491331a2015-01-02 07:29:43 +00002951 const Instruction *CxtI,
2952 const DominatorTree *DT) {
2953 // Multiplying n * m significant bits yields a result of n + m significant
2954 // bits. If the total number of significant bits does not exceed the
2955 // result bit width (minus 1), there is no overflow.
2956 // This means if we have enough leading zero bits in the operands
2957 // we can guarantee that the result does not overflow.
2958 // Ref: "Hacker's Delight" by Henry Warren
2959 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
2960 APInt LHSKnownZero(BitWidth, 0);
David Majnemerc8a576b2015-01-02 07:29:47 +00002961 APInt LHSKnownOne(BitWidth, 0);
David Majnemer491331a2015-01-02 07:29:43 +00002962 APInt RHSKnownZero(BitWidth, 0);
David Majnemerc8a576b2015-01-02 07:29:47 +00002963 APInt RHSKnownOne(BitWidth, 0);
Chandler Carruth66b31302015-01-04 12:03:27 +00002964 computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, DL, /*Depth=*/0, AC, CxtI,
2965 DT);
2966 computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, DL, /*Depth=*/0, AC, CxtI,
2967 DT);
David Majnemer491331a2015-01-02 07:29:43 +00002968 // Note that underestimating the number of zero bits gives a more
2969 // conservative answer.
2970 unsigned ZeroBits = LHSKnownZero.countLeadingOnes() +
2971 RHSKnownZero.countLeadingOnes();
2972 // First handle the easy case: if we have enough zero bits there's
2973 // definitely no overflow.
2974 if (ZeroBits >= BitWidth)
2975 return OverflowResult::NeverOverflows;
2976
2977 // Get the largest possible values for each operand.
2978 APInt LHSMax = ~LHSKnownZero;
2979 APInt RHSMax = ~RHSKnownZero;
2980
2981 // We know the multiply operation doesn't overflow if the maximum values for
2982 // each operand will not overflow after we multiply them together.
David Majnemerc8a576b2015-01-02 07:29:47 +00002983 bool MaxOverflow;
2984 LHSMax.umul_ov(RHSMax, MaxOverflow);
2985 if (!MaxOverflow)
2986 return OverflowResult::NeverOverflows;
David Majnemer491331a2015-01-02 07:29:43 +00002987
David Majnemerc8a576b2015-01-02 07:29:47 +00002988 // We know it always overflows if multiplying the smallest possible values for
2989 // the operands also results in overflow.
2990 bool MinOverflow;
2991 LHSKnownOne.umul_ov(RHSKnownOne, MinOverflow);
2992 if (MinOverflow)
2993 return OverflowResult::AlwaysOverflows;
2994
2995 return OverflowResult::MayOverflow;
David Majnemer491331a2015-01-02 07:29:43 +00002996}
David Majnemer5310c1e2015-01-07 00:39:50 +00002997
2998OverflowResult llvm::computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002999 const DataLayout &DL,
David Majnemer5310c1e2015-01-07 00:39:50 +00003000 AssumptionCache *AC,
3001 const Instruction *CxtI,
3002 const DominatorTree *DT) {
3003 bool LHSKnownNonNegative, LHSKnownNegative;
3004 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, DL, /*Depth=*/0,
3005 AC, CxtI, DT);
3006 if (LHSKnownNonNegative || LHSKnownNegative) {
3007 bool RHSKnownNonNegative, RHSKnownNegative;
3008 ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, DL, /*Depth=*/0,
3009 AC, CxtI, DT);
3010
3011 if (LHSKnownNegative && RHSKnownNegative) {
3012 // The sign bit is set in both cases: this MUST overflow.
3013 // Create a simple add instruction, and insert it into the struct.
3014 return OverflowResult::AlwaysOverflows;
3015 }
3016
3017 if (LHSKnownNonNegative && RHSKnownNonNegative) {
3018 // The sign bit is clear in both cases: this CANNOT overflow.
3019 // Create a simple add instruction, and insert it into the struct.
3020 return OverflowResult::NeverOverflows;
3021 }
3022 }
3023
3024 return OverflowResult::MayOverflow;
3025}