blob: 87ab3bb7fda6b08c29d9e7683f3d86b77a8197f2 [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"
Adam Nemete2b885c2015-04-23 20:09:20 +000020#include "llvm/Analysis/LoopInfo.h"
Nick Lewyckyec373542014-05-20 05:13:21 +000021#include "llvm/IR/CallSite.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000022#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Constants.h"
24#include "llvm/IR/DataLayout.h"
Hal Finkel60db0582014-09-07 18:57:58 +000025#include "llvm/IR/Dominators.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000026#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/GlobalAlias.h"
28#include "llvm/IR/GlobalVariable.h"
29#include "llvm/IR/Instructions.h"
30#include "llvm/IR/IntrinsicInst.h"
31#include "llvm/IR/LLVMContext.h"
32#include "llvm/IR/Metadata.h"
33#include "llvm/IR/Operator.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000034#include "llvm/IR/PatternMatch.h"
Philip Reames5461d452015-04-23 17:36:48 +000035#include "llvm/IR/Statepoint.h"
Matt Arsenaultf1a7e622014-07-15 01:55:03 +000036#include "llvm/Support/Debug.h"
Chris Lattner965c7692008-06-02 01:18:21 +000037#include "llvm/Support/MathExtras.h"
Chris Lattner64496902008-06-04 04:46:14 +000038#include <cstring>
Chris Lattner965c7692008-06-02 01:18:21 +000039using namespace llvm;
Duncan Sandsd3951082011-01-25 09:38:29 +000040using namespace llvm::PatternMatch;
41
42const unsigned MaxDepth = 6;
43
Philip Reames1c292272015-03-10 22:43:20 +000044/// Enable an experimental feature to leverage information about dominating
45/// conditions to compute known bits. The individual options below control how
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000046/// hard we search. The defaults are chosen to be fairly aggressive. If you
Philip Reames1c292272015-03-10 22:43:20 +000047/// run into compile time problems when testing, scale them back and report
48/// your findings.
49static cl::opt<bool> EnableDomConditions("value-tracking-dom-conditions",
50 cl::Hidden, cl::init(false));
51
52// This is expensive, so we only do it for the top level query value.
53// (TODO: evaluate cost vs profit, consider higher thresholds)
54static cl::opt<unsigned> DomConditionsMaxDepth("dom-conditions-max-depth",
55 cl::Hidden, cl::init(1));
56
57/// How many dominating blocks should be scanned looking for dominating
58/// conditions?
59static cl::opt<unsigned> DomConditionsMaxDomBlocks("dom-conditions-dom-blocks",
60 cl::Hidden,
61 cl::init(20000));
62
63// Controls the number of uses of the value searched for possible
64// dominating comparisons.
65static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
66 cl::Hidden, cl::init(2000));
67
68// If true, don't consider only compares whose only use is a branch.
69static cl::opt<bool> DomConditionsSingleCmpUse("dom-conditions-single-cmp-use",
70 cl::Hidden, cl::init(false));
71
Sanjay Patelaee84212014-11-04 16:27:42 +000072/// Returns the bitwidth of the given scalar or pointer type (if unknown returns
73/// 0). For vector types, returns the element type's bitwidth.
Mehdi Aminia28d91d2015-03-10 02:37:25 +000074static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
Duncan Sandsd3951082011-01-25 09:38:29 +000075 if (unsigned BitWidth = Ty->getScalarSizeInBits())
76 return BitWidth;
Matt Arsenaultf55e5e72013-08-10 17:34:08 +000077
Mehdi Aminia28d91d2015-03-10 02:37:25 +000078 return DL.getPointerTypeSizeInBits(Ty);
Duncan Sandsd3951082011-01-25 09:38:29 +000079}
Chris Lattner965c7692008-06-02 01:18:21 +000080
Hal Finkel60db0582014-09-07 18:57:58 +000081// Many of these functions have internal versions that take an assumption
82// exclusion set. This is because of the potential for mutual recursion to
83// cause computeKnownBits to repeatedly visit the same assume intrinsic. The
84// classic case of this is assume(x = y), which will attempt to determine
85// bits in x from bits in y, which will attempt to determine bits in y from
86// bits in x, etc. Regarding the mutual recursion, computeKnownBits can call
87// isKnownNonZero, which calls computeKnownBits and ComputeSignBit and
88// isKnownToBeAPowerOfTwo (all of which can call computeKnownBits), and so on.
89typedef SmallPtrSet<const Value *, 8> ExclInvsSet;
90
Benjamin Kramercfd8d902014-09-12 08:56:53 +000091namespace {
Hal Finkel60db0582014-09-07 18:57:58 +000092// Simplifying using an assume can only be done in a particular control-flow
93// context (the context instruction provides that context). If an assume and
94// the context instruction are not in the same block then the DT helps in
95// figuring out if we can use it.
96struct Query {
97 ExclInvsSet ExclInvs;
Chandler Carruth66b31302015-01-04 12:03:27 +000098 AssumptionCache *AC;
Hal Finkel60db0582014-09-07 18:57:58 +000099 const Instruction *CxtI;
100 const DominatorTree *DT;
101
Chandler Carruth66b31302015-01-04 12:03:27 +0000102 Query(AssumptionCache *AC = nullptr, const Instruction *CxtI = nullptr,
Hal Finkel60db0582014-09-07 18:57:58 +0000103 const DominatorTree *DT = nullptr)
Chandler Carruth66b31302015-01-04 12:03:27 +0000104 : AC(AC), CxtI(CxtI), DT(DT) {}
Hal Finkel60db0582014-09-07 18:57:58 +0000105
106 Query(const Query &Q, const Value *NewExcl)
Chandler Carruth66b31302015-01-04 12:03:27 +0000107 : ExclInvs(Q.ExclInvs), AC(Q.AC), CxtI(Q.CxtI), DT(Q.DT) {
Hal Finkel60db0582014-09-07 18:57:58 +0000108 ExclInvs.insert(NewExcl);
109 }
110};
Benjamin Kramercfd8d902014-09-12 08:56:53 +0000111} // end anonymous namespace
Hal Finkel60db0582014-09-07 18:57:58 +0000112
Sanjay Patel547e9752014-11-04 16:09:50 +0000113// Given the provided Value and, potentially, a context instruction, return
Hal Finkel60db0582014-09-07 18:57:58 +0000114// the preferred context instruction (if any).
115static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
116 // If we've been provided with a context instruction, then use that (provided
117 // it has been inserted).
118 if (CxtI && CxtI->getParent())
119 return CxtI;
120
121 // If the value is really an already-inserted instruction, then use that.
122 CxtI = dyn_cast<Instruction>(V);
123 if (CxtI && CxtI->getParent())
124 return CxtI;
125
126 return nullptr;
127}
128
129static void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000130 const DataLayout &DL, unsigned Depth,
131 const Query &Q);
Hal Finkel60db0582014-09-07 18:57:58 +0000132
133void llvm::computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000134 const DataLayout &DL, unsigned Depth,
Chandler Carruth66b31302015-01-04 12:03:27 +0000135 AssumptionCache *AC, const Instruction *CxtI,
Hal Finkel60db0582014-09-07 18:57:58 +0000136 const DominatorTree *DT) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000137 ::computeKnownBits(V, KnownZero, KnownOne, DL, Depth,
Chandler Carruth66b31302015-01-04 12:03:27 +0000138 Query(AC, safeCxtI(V, CxtI), DT));
Hal Finkel60db0582014-09-07 18:57:58 +0000139}
140
Jingyue Wuca321902015-05-14 23:53:19 +0000141bool llvm::haveNoCommonBitsSet(Value *LHS, Value *RHS, const DataLayout &DL,
142 AssumptionCache *AC, const Instruction *CxtI,
143 const DominatorTree *DT) {
144 assert(LHS->getType() == RHS->getType() &&
145 "LHS and RHS should have the same type");
146 assert(LHS->getType()->isIntOrIntVectorTy() &&
147 "LHS and RHS should be integers");
148 IntegerType *IT = cast<IntegerType>(LHS->getType()->getScalarType());
149 APInt LHSKnownZero(IT->getBitWidth(), 0), LHSKnownOne(IT->getBitWidth(), 0);
150 APInt RHSKnownZero(IT->getBitWidth(), 0), RHSKnownOne(IT->getBitWidth(), 0);
151 computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, DL, 0, AC, CxtI, DT);
152 computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, DL, 0, AC, CxtI, DT);
153 return (LHSKnownZero | RHSKnownZero).isAllOnesValue();
154}
155
Hal Finkel60db0582014-09-07 18:57:58 +0000156static void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000157 const DataLayout &DL, unsigned Depth,
158 const Query &Q);
Hal Finkel60db0582014-09-07 18:57:58 +0000159
160void llvm::ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000161 const DataLayout &DL, unsigned Depth,
Chandler Carruth66b31302015-01-04 12:03:27 +0000162 AssumptionCache *AC, const Instruction *CxtI,
Hal Finkel60db0582014-09-07 18:57:58 +0000163 const DominatorTree *DT) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000164 ::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth,
Chandler Carruth66b31302015-01-04 12:03:27 +0000165 Query(AC, safeCxtI(V, CxtI), DT));
Hal Finkel60db0582014-09-07 18:57:58 +0000166}
167
168static bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero, unsigned Depth,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000169 const Query &Q, const DataLayout &DL);
Hal Finkel60db0582014-09-07 18:57:58 +0000170
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000171bool llvm::isKnownToBeAPowerOfTwo(Value *V, const DataLayout &DL, bool OrZero,
Chandler Carruth66b31302015-01-04 12:03:27 +0000172 unsigned Depth, AssumptionCache *AC,
Hal Finkel60db0582014-09-07 18:57:58 +0000173 const Instruction *CxtI,
174 const DominatorTree *DT) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000175 return ::isKnownToBeAPowerOfTwo(V, OrZero, Depth,
176 Query(AC, safeCxtI(V, CxtI), DT), DL);
177}
178
179static bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth,
180 const Query &Q);
181
182bool llvm::isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth,
183 AssumptionCache *AC, const Instruction *CxtI,
184 const DominatorTree *DT) {
185 return ::isKnownNonZero(V, DL, Depth, Query(AC, safeCxtI(V, CxtI), DT));
186}
187
188static bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
189 unsigned Depth, const Query &Q);
190
191bool llvm::MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
192 unsigned Depth, AssumptionCache *AC,
193 const Instruction *CxtI, const DominatorTree *DT) {
194 return ::MaskedValueIsZero(V, Mask, DL, Depth,
195 Query(AC, safeCxtI(V, CxtI), DT));
196}
197
198static unsigned ComputeNumSignBits(Value *V, const DataLayout &DL,
199 unsigned Depth, const Query &Q);
200
201unsigned llvm::ComputeNumSignBits(Value *V, const DataLayout &DL,
202 unsigned Depth, AssumptionCache *AC,
203 const Instruction *CxtI,
204 const DominatorTree *DT) {
205 return ::ComputeNumSignBits(V, DL, Depth, Query(AC, safeCxtI(V, CxtI), DT));
Hal Finkel60db0582014-09-07 18:57:58 +0000206}
207
Jay Foada0653a32014-05-14 21:14:37 +0000208static void computeKnownBitsAddSub(bool Add, Value *Op0, Value *Op1, bool NSW,
209 APInt &KnownZero, APInt &KnownOne,
210 APInt &KnownZero2, APInt &KnownOne2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000211 const DataLayout &DL, unsigned Depth,
Hal Finkel60db0582014-09-07 18:57:58 +0000212 const Query &Q) {
213 if (!Add) {
214 if (ConstantInt *CLHS = dyn_cast<ConstantInt>(Op0)) {
215 // We know that the top bits of C-X are clear if X contains less bits
216 // than C (i.e. no wrap-around can happen). For example, 20-X is
217 // positive if we can prove that X is >= 0 and < 16.
218 if (!CLHS->getValue().isNegative()) {
219 unsigned BitWidth = KnownZero.getBitWidth();
220 unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros();
221 // NLZ can't be BitWidth with no sign bit
222 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000223 computeKnownBits(Op1, KnownZero2, KnownOne2, DL, Depth + 1, Q);
Hal Finkel60db0582014-09-07 18:57:58 +0000224
225 // If all of the MaskV bits are known to be zero, then we know the
226 // output top bits are zero, because we now know that the output is
227 // from [0-C].
228 if ((KnownZero2 & MaskV) == MaskV) {
229 unsigned NLZ2 = CLHS->getValue().countLeadingZeros();
230 // Top bits known zero.
231 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
232 }
233 }
234 }
235 }
236
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000237 unsigned BitWidth = KnownZero.getBitWidth();
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000238
David Majnemer97ddca32014-08-22 00:40:43 +0000239 // If an initial sequence of bits in the result is not needed, the
240 // corresponding bits in the operands are not needed.
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000241 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000242 computeKnownBits(Op0, LHSKnownZero, LHSKnownOne, DL, Depth + 1, Q);
243 computeKnownBits(Op1, KnownZero2, KnownOne2, DL, Depth + 1, Q);
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000244
David Majnemer97ddca32014-08-22 00:40:43 +0000245 // Carry in a 1 for a subtract, rather than a 0.
246 APInt CarryIn(BitWidth, 0);
247 if (!Add) {
248 // Sum = LHS + ~RHS + 1
249 std::swap(KnownZero2, KnownOne2);
250 CarryIn.setBit(0);
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000251 }
252
David Majnemer97ddca32014-08-22 00:40:43 +0000253 APInt PossibleSumZero = ~LHSKnownZero + ~KnownZero2 + CarryIn;
254 APInt PossibleSumOne = LHSKnownOne + KnownOne2 + CarryIn;
255
256 // Compute known bits of the carry.
257 APInt CarryKnownZero = ~(PossibleSumZero ^ LHSKnownZero ^ KnownZero2);
258 APInt CarryKnownOne = PossibleSumOne ^ LHSKnownOne ^ KnownOne2;
259
260 // Compute set of known bits (where all three relevant bits are known).
261 APInt LHSKnown = LHSKnownZero | LHSKnownOne;
262 APInt RHSKnown = KnownZero2 | KnownOne2;
263 APInt CarryKnown = CarryKnownZero | CarryKnownOne;
264 APInt Known = LHSKnown & RHSKnown & CarryKnown;
265
266 assert((PossibleSumZero & Known) == (PossibleSumOne & Known) &&
267 "known bits of sum differ");
268
269 // Compute known bits of the result.
270 KnownZero = ~PossibleSumOne & Known;
271 KnownOne = PossibleSumOne & Known;
272
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000273 // Are we still trying to solve for the sign bit?
David Majnemer97ddca32014-08-22 00:40:43 +0000274 if (!Known.isNegative()) {
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000275 if (NSW) {
David Majnemer97ddca32014-08-22 00:40:43 +0000276 // Adding two non-negative numbers, or subtracting a negative number from
277 // a non-negative one, can't wrap into negative.
278 if (LHSKnownZero.isNegative() && KnownZero2.isNegative())
279 KnownZero |= APInt::getSignBit(BitWidth);
280 // Adding two negative numbers, or subtracting a non-negative number from
281 // a negative one, can't wrap into non-negative.
282 else if (LHSKnownOne.isNegative() && KnownOne2.isNegative())
283 KnownOne |= APInt::getSignBit(BitWidth);
Nick Lewyckyfea3e002012-03-09 09:23:50 +0000284 }
285 }
286}
287
Jay Foada0653a32014-05-14 21:14:37 +0000288static void computeKnownBitsMul(Value *Op0, Value *Op1, bool NSW,
289 APInt &KnownZero, APInt &KnownOne,
290 APInt &KnownZero2, APInt &KnownOne2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000291 const DataLayout &DL, unsigned Depth,
Hal Finkel60db0582014-09-07 18:57:58 +0000292 const Query &Q) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000293 unsigned BitWidth = KnownZero.getBitWidth();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000294 computeKnownBits(Op1, KnownZero, KnownOne, DL, Depth + 1, Q);
295 computeKnownBits(Op0, KnownZero2, KnownOne2, DL, Depth + 1, Q);
Nick Lewyckyfa306072012-03-18 23:28:48 +0000296
297 bool isKnownNegative = false;
298 bool isKnownNonNegative = false;
299 // If the multiplication is known not to overflow, compute the sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000300 if (NSW) {
Nick Lewyckyfa306072012-03-18 23:28:48 +0000301 if (Op0 == Op1) {
302 // The product of a number with itself is non-negative.
303 isKnownNonNegative = true;
304 } else {
305 bool isKnownNonNegativeOp1 = KnownZero.isNegative();
306 bool isKnownNonNegativeOp0 = KnownZero2.isNegative();
307 bool isKnownNegativeOp1 = KnownOne.isNegative();
308 bool isKnownNegativeOp0 = KnownOne2.isNegative();
309 // The product of two numbers with the same sign is non-negative.
310 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
311 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
312 // The product of a negative number and a non-negative number is either
313 // negative or zero.
314 if (!isKnownNonNegative)
315 isKnownNegative = (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000316 isKnownNonZero(Op0, DL, Depth, Q)) ||
Nick Lewyckyfa306072012-03-18 23:28:48 +0000317 (isKnownNegativeOp0 && isKnownNonNegativeOp1 &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000318 isKnownNonZero(Op1, DL, Depth, Q));
Nick Lewyckyfa306072012-03-18 23:28:48 +0000319 }
320 }
321
322 // If low bits are zero in either operand, output low known-0 bits.
323 // Also compute a conserative estimate for high known-0 bits.
324 // More trickiness is possible, but this is sufficient for the
325 // interesting case of alignment computation.
326 KnownOne.clearAllBits();
327 unsigned TrailZ = KnownZero.countTrailingOnes() +
328 KnownZero2.countTrailingOnes();
329 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
330 KnownZero2.countLeadingOnes(),
331 BitWidth) - BitWidth;
332
333 TrailZ = std::min(TrailZ, BitWidth);
334 LeadZ = std::min(LeadZ, BitWidth);
335 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
336 APInt::getHighBitsSet(BitWidth, LeadZ);
Nick Lewyckyfa306072012-03-18 23:28:48 +0000337
338 // Only make use of no-wrap flags if we failed to compute the sign bit
339 // directly. This matters if the multiplication always overflows, in
340 // which case we prefer to follow the result of the direct computation,
341 // though as the program is invoking undefined behaviour we can choose
342 // whatever we like here.
343 if (isKnownNonNegative && !KnownOne.isNegative())
344 KnownZero.setBit(BitWidth - 1);
345 else if (isKnownNegative && !KnownZero.isNegative())
346 KnownOne.setBit(BitWidth - 1);
347}
348
Jingyue Wu37fcb592014-06-19 16:50:16 +0000349void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
350 APInt &KnownZero) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000351 unsigned BitWidth = KnownZero.getBitWidth();
Rafael Espindola53190532012-03-30 15:52:11 +0000352 unsigned NumRanges = Ranges.getNumOperands() / 2;
353 assert(NumRanges >= 1);
354
355 // Use the high end of the ranges to find leading zeros.
356 unsigned MinLeadingZeros = BitWidth;
357 for (unsigned i = 0; i < NumRanges; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000358 ConstantInt *Lower =
359 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
360 ConstantInt *Upper =
361 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
Rafael Espindola53190532012-03-30 15:52:11 +0000362 ConstantRange Range(Lower->getValue(), Upper->getValue());
363 if (Range.isWrappedSet())
364 MinLeadingZeros = 0; // -1 has no zeros
365 unsigned LeadingZeros = (Upper->getValue() - 1).countLeadingZeros();
366 MinLeadingZeros = std::min(LeadingZeros, MinLeadingZeros);
367 }
368
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000369 KnownZero = APInt::getHighBitsSet(BitWidth, MinLeadingZeros);
Rafael Espindola53190532012-03-30 15:52:11 +0000370}
Jay Foad5a29c362014-05-15 12:12:55 +0000371
Hal Finkel60db0582014-09-07 18:57:58 +0000372static bool isEphemeralValueOf(Instruction *I, const Value *E) {
373 SmallVector<const Value *, 16> WorkSet(1, I);
374 SmallPtrSet<const Value *, 32> Visited;
375 SmallPtrSet<const Value *, 16> EphValues;
376
377 while (!WorkSet.empty()) {
378 const Value *V = WorkSet.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +0000379 if (!Visited.insert(V).second)
Hal Finkel60db0582014-09-07 18:57:58 +0000380 continue;
381
382 // If all uses of this value are ephemeral, then so is this value.
383 bool FoundNEUse = false;
384 for (const User *I : V->users())
385 if (!EphValues.count(I)) {
386 FoundNEUse = true;
387 break;
388 }
389
390 if (!FoundNEUse) {
391 if (V == E)
392 return true;
393
394 EphValues.insert(V);
395 if (const User *U = dyn_cast<User>(V))
396 for (User::const_op_iterator J = U->op_begin(), JE = U->op_end();
397 J != JE; ++J) {
398 if (isSafeToSpeculativelyExecute(*J))
399 WorkSet.push_back(*J);
400 }
401 }
402 }
403
404 return false;
405}
406
407// Is this an intrinsic that cannot be speculated but also cannot trap?
408static bool isAssumeLikeIntrinsic(const Instruction *I) {
409 if (const CallInst *CI = dyn_cast<CallInst>(I))
410 if (Function *F = CI->getCalledFunction())
411 switch (F->getIntrinsicID()) {
412 default: break;
413 // FIXME: This list is repeated from NoTTI::getIntrinsicCost.
414 case Intrinsic::assume:
415 case Intrinsic::dbg_declare:
416 case Intrinsic::dbg_value:
417 case Intrinsic::invariant_start:
418 case Intrinsic::invariant_end:
419 case Intrinsic::lifetime_start:
420 case Intrinsic::lifetime_end:
421 case Intrinsic::objectsize:
422 case Intrinsic::ptr_annotation:
423 case Intrinsic::var_annotation:
424 return true;
425 }
426
427 return false;
428}
429
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000430static bool isValidAssumeForContext(Value *V, const Query &Q) {
Hal Finkel60db0582014-09-07 18:57:58 +0000431 Instruction *Inv = cast<Instruction>(V);
432
433 // There are two restrictions on the use of an assume:
434 // 1. The assume must dominate the context (or the control flow must
435 // reach the assume whenever it reaches the context).
436 // 2. The context must not be in the assume's set of ephemeral values
437 // (otherwise we will use the assume to prove that the condition
438 // feeding the assume is trivially true, thus causing the removal of
439 // the assume).
440
441 if (Q.DT) {
442 if (Q.DT->dominates(Inv, Q.CxtI)) {
443 return true;
444 } else if (Inv->getParent() == Q.CxtI->getParent()) {
445 // The context comes first, but they're both in the same block. Make sure
446 // there is nothing in between that might interrupt the control flow.
447 for (BasicBlock::const_iterator I =
448 std::next(BasicBlock::const_iterator(Q.CxtI)),
449 IE(Inv); I != IE; ++I)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000450 if (!isSafeToSpeculativelyExecute(I) && !isAssumeLikeIntrinsic(I))
Hal Finkel60db0582014-09-07 18:57:58 +0000451 return false;
452
453 return !isEphemeralValueOf(Inv, Q.CxtI);
454 }
455
456 return false;
457 }
458
459 // When we don't have a DT, we do a limited search...
460 if (Inv->getParent() == Q.CxtI->getParent()->getSinglePredecessor()) {
461 return true;
462 } else if (Inv->getParent() == Q.CxtI->getParent()) {
463 // Search forward from the assume until we reach the context (or the end
464 // of the block); the common case is that the assume will come first.
465 for (BasicBlock::iterator I = std::next(BasicBlock::iterator(Inv)),
466 IE = Inv->getParent()->end(); I != IE; ++I)
467 if (I == Q.CxtI)
468 return true;
469
470 // The context must come first...
471 for (BasicBlock::const_iterator I =
472 std::next(BasicBlock::const_iterator(Q.CxtI)),
473 IE(Inv); I != IE; ++I)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000474 if (!isSafeToSpeculativelyExecute(I) && !isAssumeLikeIntrinsic(I))
Hal Finkel60db0582014-09-07 18:57:58 +0000475 return false;
476
477 return !isEphemeralValueOf(Inv, Q.CxtI);
478 }
479
480 return false;
481}
482
483bool llvm::isValidAssumeForContext(const Instruction *I,
484 const Instruction *CxtI,
Hal Finkel60db0582014-09-07 18:57:58 +0000485 const DominatorTree *DT) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000486 return ::isValidAssumeForContext(const_cast<Instruction *>(I),
487 Query(nullptr, CxtI, DT));
Hal Finkel60db0582014-09-07 18:57:58 +0000488}
489
490template<typename LHS, typename RHS>
491inline match_combine_or<CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>,
492 CmpClass_match<RHS, LHS, ICmpInst, ICmpInst::Predicate>>
493m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
494 return m_CombineOr(m_ICmp(Pred, L, R), m_ICmp(Pred, R, L));
495}
496
497template<typename LHS, typename RHS>
498inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::And>,
499 BinaryOp_match<RHS, LHS, Instruction::And>>
500m_c_And(const LHS &L, const RHS &R) {
501 return m_CombineOr(m_And(L, R), m_And(R, L));
502}
503
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000504template<typename LHS, typename RHS>
505inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::Or>,
506 BinaryOp_match<RHS, LHS, Instruction::Or>>
507m_c_Or(const LHS &L, const RHS &R) {
508 return m_CombineOr(m_Or(L, R), m_Or(R, L));
509}
510
511template<typename LHS, typename RHS>
512inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::Xor>,
513 BinaryOp_match<RHS, LHS, Instruction::Xor>>
514m_c_Xor(const LHS &L, const RHS &R) {
515 return m_CombineOr(m_Xor(L, R), m_Xor(R, L));
516}
517
Philip Reames1c292272015-03-10 22:43:20 +0000518/// Compute known bits in 'V' under the assumption that the condition 'Cmp' is
519/// true (at the context instruction.) This is mostly a utility function for
520/// the prototype dominating conditions reasoning below.
521static void computeKnownBitsFromTrueCondition(Value *V, ICmpInst *Cmp,
522 APInt &KnownZero,
523 APInt &KnownOne,
524 const DataLayout &DL,
525 unsigned Depth, const Query &Q) {
526 Value *LHS = Cmp->getOperand(0);
527 Value *RHS = Cmp->getOperand(1);
528 // TODO: We could potentially be more aggressive here. This would be worth
529 // evaluating. If we can, explore commoning this code with the assume
530 // handling logic.
531 if (LHS != V && RHS != V)
532 return;
533
534 const unsigned BitWidth = KnownZero.getBitWidth();
535
536 switch (Cmp->getPredicate()) {
537 default:
538 // We know nothing from this condition
539 break;
540 // TODO: implement unsigned bound from below (known one bits)
541 // TODO: common condition check implementations with assumes
542 // TODO: implement other patterns from assume (e.g. V & B == A)
543 case ICmpInst::ICMP_SGT:
544 if (LHS == V) {
545 APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0);
546 computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
547 if (KnownOneTemp.isAllOnesValue() || KnownZeroTemp.isNegative()) {
548 // We know that the sign bit is zero.
549 KnownZero |= APInt::getSignBit(BitWidth);
550 }
551 }
552 break;
553 case ICmpInst::ICMP_EQ:
Jingyue Wu12b0c282015-06-15 05:46:29 +0000554 {
555 APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0);
556 if (LHS == V)
557 computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
558 else if (RHS == V)
559 computeKnownBits(LHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
560 else
561 llvm_unreachable("missing use?");
562 KnownZero |= KnownZeroTemp;
563 KnownOne |= KnownOneTemp;
564 }
Philip Reames1c292272015-03-10 22:43:20 +0000565 break;
566 case ICmpInst::ICMP_ULE:
567 if (LHS == V) {
568 APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0);
569 computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
570 // The known zero bits carry over
571 unsigned SignBits = KnownZeroTemp.countLeadingOnes();
572 KnownZero |= APInt::getHighBitsSet(BitWidth, SignBits);
573 }
574 break;
575 case ICmpInst::ICMP_ULT:
576 if (LHS == V) {
577 APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0);
578 computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
579 // Whatever high bits in rhs are zero are known to be zero (if rhs is a
580 // power of 2, then one more).
581 unsigned SignBits = KnownZeroTemp.countLeadingOnes();
582 if (isKnownToBeAPowerOfTwo(RHS, false, Depth + 1, Query(Q, Cmp), DL))
583 SignBits++;
584 KnownZero |= APInt::getHighBitsSet(BitWidth, SignBits);
585 }
586 break;
587 };
588}
589
590/// Compute known bits in 'V' from conditions which are known to be true along
591/// all paths leading to the context instruction. In particular, look for
592/// cases where one branch of an interesting condition dominates the context
593/// instruction. This does not do general dataflow.
594/// NOTE: This code is EXPERIMENTAL and currently off by default.
595static void computeKnownBitsFromDominatingCondition(Value *V, APInt &KnownZero,
596 APInt &KnownOne,
597 const DataLayout &DL,
598 unsigned Depth,
599 const Query &Q) {
600 // Need both the dominator tree and the query location to do anything useful
601 if (!Q.DT || !Q.CxtI)
602 return;
603 Instruction *Cxt = const_cast<Instruction *>(Q.CxtI);
604
605 // Avoid useless work
606 if (auto VI = dyn_cast<Instruction>(V))
607 if (VI->getParent() == Cxt->getParent())
608 return;
609
610 // Note: We currently implement two options. It's not clear which of these
611 // will survive long term, we need data for that.
612 // Option 1 - Try walking the dominator tree looking for conditions which
613 // might apply. This works well for local conditions (loop guards, etc..),
614 // but not as well for things far from the context instruction (presuming a
615 // low max blocks explored). If we can set an high enough limit, this would
616 // be all we need.
617 // Option 2 - We restrict out search to those conditions which are uses of
618 // the value we're interested in. This is independent of dom structure,
619 // but is slightly less powerful without looking through lots of use chains.
620 // It does handle conditions far from the context instruction (e.g. early
621 // function exits on entry) really well though.
622
623 // Option 1 - Search the dom tree
624 unsigned NumBlocksExplored = 0;
625 BasicBlock *Current = Cxt->getParent();
626 while (true) {
627 // Stop searching if we've gone too far up the chain
628 if (NumBlocksExplored >= DomConditionsMaxDomBlocks)
629 break;
630 NumBlocksExplored++;
631
632 if (!Q.DT->getNode(Current)->getIDom())
633 break;
634 Current = Q.DT->getNode(Current)->getIDom()->getBlock();
635 if (!Current)
636 // found function entry
637 break;
638
639 BranchInst *BI = dyn_cast<BranchInst>(Current->getTerminator());
640 if (!BI || BI->isUnconditional())
641 continue;
642 ICmpInst *Cmp = dyn_cast<ICmpInst>(BI->getCondition());
643 if (!Cmp)
644 continue;
645
646 // We're looking for conditions that are guaranteed to hold at the context
647 // instruction. Finding a condition where one path dominates the context
648 // isn't enough because both the true and false cases could merge before
649 // the context instruction we're actually interested in. Instead, we need
650 // to ensure that the taken *edge* dominates the context instruction.
651 BasicBlock *BB0 = BI->getSuccessor(0);
652 BasicBlockEdge Edge(BI->getParent(), BB0);
653 if (!Edge.isSingleEdge() || !Q.DT->dominates(Edge, Q.CxtI->getParent()))
654 continue;
655
656 computeKnownBitsFromTrueCondition(V, Cmp, KnownZero, KnownOne, DL, Depth,
657 Q);
658 }
659
660 // Option 2 - Search the other uses of V
661 unsigned NumUsesExplored = 0;
662 for (auto U : V->users()) {
663 // Avoid massive lists
664 if (NumUsesExplored >= DomConditionsMaxUses)
665 break;
666 NumUsesExplored++;
667 // Consider only compare instructions uniquely controlling a branch
668 ICmpInst *Cmp = dyn_cast<ICmpInst>(U);
669 if (!Cmp)
670 continue;
671
672 if (DomConditionsSingleCmpUse && !Cmp->hasOneUse())
673 continue;
674
675 for (auto *CmpU : Cmp->users()) {
676 BranchInst *BI = dyn_cast<BranchInst>(CmpU);
677 if (!BI || BI->isUnconditional())
678 continue;
679 // We're looking for conditions that are guaranteed to hold at the
680 // context instruction. Finding a condition where one path dominates
681 // the context isn't enough because both the true and false cases could
682 // merge before the context instruction we're actually interested in.
683 // Instead, we need to ensure that the taken *edge* dominates the context
684 // instruction.
685 BasicBlock *BB0 = BI->getSuccessor(0);
686 BasicBlockEdge Edge(BI->getParent(), BB0);
687 if (!Edge.isSingleEdge() || !Q.DT->dominates(Edge, Q.CxtI->getParent()))
688 continue;
689
690 computeKnownBitsFromTrueCondition(V, Cmp, KnownZero, KnownOne, DL, Depth,
691 Q);
692 }
693 }
694}
695
Hal Finkel60db0582014-09-07 18:57:58 +0000696static void computeKnownBitsFromAssume(Value *V, APInt &KnownZero,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000697 APInt &KnownOne, const DataLayout &DL,
Hal Finkel60db0582014-09-07 18:57:58 +0000698 unsigned Depth, const Query &Q) {
699 // Use of assumptions is context-sensitive. If we don't have a context, we
700 // cannot use them!
Chandler Carruth66b31302015-01-04 12:03:27 +0000701 if (!Q.AC || !Q.CxtI)
Hal Finkel60db0582014-09-07 18:57:58 +0000702 return;
703
704 unsigned BitWidth = KnownZero.getBitWidth();
705
Chandler Carruth66b31302015-01-04 12:03:27 +0000706 for (auto &AssumeVH : Q.AC->assumptions()) {
707 if (!AssumeVH)
708 continue;
709 CallInst *I = cast<CallInst>(AssumeVH);
Chandler Carruth75c11b82015-01-04 23:13:57 +0000710 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
Chandler Carruth66b31302015-01-04 12:03:27 +0000711 "Got assumption for the wrong function!");
Hal Finkel60db0582014-09-07 18:57:58 +0000712 if (Q.ExclInvs.count(I))
713 continue;
714
Philip Reames00d3b272014-11-24 23:44:28 +0000715 // Warning: This loop can end up being somewhat performance sensetive.
716 // We're running this loop for once for each value queried resulting in a
717 // runtime of ~O(#assumes * #values).
718
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000719 assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
Philip Reames00d3b272014-11-24 23:44:28 +0000720 "must be an assume intrinsic");
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000721
Philip Reames00d3b272014-11-24 23:44:28 +0000722 Value *Arg = I->getArgOperand(0);
723
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000724 if (Arg == V && isValidAssumeForContext(I, Q)) {
Hal Finkel60db0582014-09-07 18:57:58 +0000725 assert(BitWidth == 1 && "assume operand is not i1?");
726 KnownZero.clearAllBits();
727 KnownOne.setAllBits();
728 return;
729 }
730
David Majnemer9b609752014-12-12 23:59:29 +0000731 // The remaining tests are all recursive, so bail out if we hit the limit.
732 if (Depth == MaxDepth)
733 continue;
734
Hal Finkel60db0582014-09-07 18:57:58 +0000735 Value *A, *B;
736 auto m_V = m_CombineOr(m_Specific(V),
737 m_CombineOr(m_PtrToInt(m_Specific(V)),
738 m_BitCast(m_Specific(V))));
739
740 CmpInst::Predicate Pred;
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000741 ConstantInt *C;
Hal Finkel60db0582014-09-07 18:57:58 +0000742 // assume(v = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000743 if (match(Arg, m_c_ICmp(Pred, m_V, m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000744 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel60db0582014-09-07 18:57:58 +0000745 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
746 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
747 KnownZero |= RHSKnownZero;
748 KnownOne |= RHSKnownOne;
749 // assume(v & b = a)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000750 } else if (match(Arg,
751 m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A))) &&
752 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel60db0582014-09-07 18:57:58 +0000753 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
754 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
755 APInt MaskKnownZero(BitWidth, 0), MaskKnownOne(BitWidth, 0);
756 computeKnownBits(B, MaskKnownZero, MaskKnownOne, DL, Depth+1, Query(Q, I));
757
758 // For those bits in the mask that are known to be one, we can propagate
759 // known bits from the RHS to V.
760 KnownZero |= RHSKnownZero & MaskKnownOne;
761 KnownOne |= RHSKnownOne & MaskKnownOne;
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000762 // assume(~(v & b) = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000763 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))),
764 m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000765 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000766 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
767 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
768 APInt MaskKnownZero(BitWidth, 0), MaskKnownOne(BitWidth, 0);
769 computeKnownBits(B, MaskKnownZero, MaskKnownOne, DL, Depth+1, Query(Q, I));
770
771 // For those bits in the mask that are known to be one, we can propagate
772 // inverted known bits from the RHS to V.
773 KnownZero |= RHSKnownOne & MaskKnownOne;
774 KnownOne |= RHSKnownZero & MaskKnownOne;
775 // assume(v | b = a)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000776 } else if (match(Arg,
777 m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A))) &&
778 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000779 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
780 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
781 APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
782 computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
783
784 // For those bits in B that are known to be zero, we can propagate known
785 // bits from the RHS to V.
786 KnownZero |= RHSKnownZero & BKnownZero;
787 KnownOne |= RHSKnownOne & BKnownZero;
788 // assume(~(v | b) = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000789 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))),
790 m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000791 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000792 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
793 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
794 APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
795 computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
796
797 // For those bits in B that are known to be zero, we can propagate
798 // inverted known bits from the RHS to V.
799 KnownZero |= RHSKnownOne & BKnownZero;
800 KnownOne |= RHSKnownZero & BKnownZero;
801 // assume(v ^ b = a)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000802 } else if (match(Arg,
803 m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A))) &&
804 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000805 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
806 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
807 APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
808 computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
809
810 // For those bits in B that are known to be zero, we can propagate known
811 // bits from the RHS to V. For those bits in B that are known to be one,
812 // we can propagate inverted known bits from the RHS to V.
813 KnownZero |= RHSKnownZero & BKnownZero;
814 KnownOne |= RHSKnownOne & BKnownZero;
815 KnownZero |= RHSKnownOne & BKnownOne;
816 KnownOne |= RHSKnownZero & BKnownOne;
817 // assume(~(v ^ b) = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000818 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))),
819 m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000820 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000821 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
822 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
823 APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
824 computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
825
826 // For those bits in B that are known to be zero, we can propagate
827 // inverted known bits from the RHS to V. For those bits in B that are
828 // known to be one, we can propagate known bits from the RHS to V.
829 KnownZero |= RHSKnownOne & BKnownZero;
830 KnownOne |= RHSKnownZero & BKnownZero;
831 KnownZero |= RHSKnownZero & BKnownOne;
832 KnownOne |= RHSKnownOne & BKnownOne;
833 // assume(v << c = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000834 } else if (match(Arg, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)),
835 m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000836 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.lshr(C->getZExtValue());
842 KnownOne |= RHSKnownOne.lshr(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_Shl(m_V, m_ConstantInt(C))),
845 m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000846 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000847 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
848 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
849 // For those bits in RHS that are known, we can propagate them inverted
850 // to known bits in V shifted to the right by C.
851 KnownZero |= RHSKnownOne.lshr(C->getZExtValue());
852 KnownOne |= RHSKnownZero.lshr(C->getZExtValue());
853 // assume(v >> c = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000854 } else if (match(Arg,
855 m_c_ICmp(Pred, m_CombineOr(m_LShr(m_V, m_ConstantInt(C)),
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000856 m_AShr(m_V, m_ConstantInt(C))),
857 m_Value(A))) &&
858 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000859 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
860 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
861 // For those bits in RHS that are known, we can propagate them to known
862 // bits in V shifted to the right by C.
863 KnownZero |= RHSKnownZero << C->getZExtValue();
864 KnownOne |= RHSKnownOne << C->getZExtValue();
865 // assume(~(v >> c) = a)
Philip Reames00d3b272014-11-24 23:44:28 +0000866 } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_CombineOr(
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000867 m_LShr(m_V, m_ConstantInt(C)),
868 m_AShr(m_V, m_ConstantInt(C)))),
Philip Reames00d3b272014-11-24 23:44:28 +0000869 m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000870 Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000871 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
872 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
873 // For those bits in RHS that are known, we can propagate them inverted
874 // to known bits in V shifted to the right by C.
875 KnownZero |= RHSKnownOne << C->getZExtValue();
876 KnownOne |= RHSKnownZero << C->getZExtValue();
877 // assume(v >=_s c) where c is non-negative
Philip Reames00d3b272014-11-24 23:44:28 +0000878 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000879 Pred == ICmpInst::ICMP_SGE && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000880 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
881 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
882
883 if (RHSKnownZero.isNegative()) {
884 // We know that the sign bit is zero.
885 KnownZero |= APInt::getSignBit(BitWidth);
886 }
887 // assume(v >_s c) where c is at least -1.
Philip Reames00d3b272014-11-24 23:44:28 +0000888 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000889 Pred == ICmpInst::ICMP_SGT && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000890 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
891 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
892
893 if (RHSKnownOne.isAllOnesValue() || RHSKnownZero.isNegative()) {
894 // We know that the sign bit is zero.
895 KnownZero |= APInt::getSignBit(BitWidth);
896 }
897 // assume(v <=_s c) where c is negative
Philip Reames00d3b272014-11-24 23:44:28 +0000898 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000899 Pred == ICmpInst::ICMP_SLE && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000900 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
901 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
902
903 if (RHSKnownOne.isNegative()) {
904 // We know that the sign bit is one.
905 KnownOne |= APInt::getSignBit(BitWidth);
906 }
907 // assume(v <_s c) where c is non-positive
Philip Reames00d3b272014-11-24 23:44:28 +0000908 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000909 Pred == ICmpInst::ICMP_SLT && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000910 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
911 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
912
913 if (RHSKnownZero.isAllOnesValue() || RHSKnownOne.isNegative()) {
914 // We know that the sign bit is one.
915 KnownOne |= APInt::getSignBit(BitWidth);
916 }
917 // assume(v <=_u c)
Philip Reames00d3b272014-11-24 23:44:28 +0000918 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000919 Pred == ICmpInst::ICMP_ULE && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000920 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
921 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
922
923 // Whatever high bits in c are zero are known to be zero.
924 KnownZero |=
925 APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes());
926 // assume(v <_u c)
Philip Reames00d3b272014-11-24 23:44:28 +0000927 } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000928 Pred == ICmpInst::ICMP_ULT && isValidAssumeForContext(I, Q)) {
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000929 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
930 computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
931
932 // Whatever high bits in c are zero are known to be zero (if c is a power
933 // of 2, then one more).
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000934 if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, Query(Q, I), DL))
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000935 KnownZero |=
936 APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes()+1);
937 else
938 KnownZero |=
939 APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes());
Hal Finkel60db0582014-09-07 18:57:58 +0000940 }
941 }
942}
943
Jingyue Wu12b0c282015-06-15 05:46:29 +0000944static void computeKnownBitsFromOperator(Operator *I, APInt &KnownZero,
945 APInt &KnownOne, const DataLayout &DL,
946 unsigned Depth, const Query &Q) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000947 unsigned BitWidth = KnownZero.getBitWidth();
948
Chris Lattner965c7692008-06-02 01:18:21 +0000949 APInt KnownZero2(KnownZero), KnownOne2(KnownOne);
Dan Gohman80ca01c2009-07-17 20:47:02 +0000950 switch (I->getOpcode()) {
Chris Lattner965c7692008-06-02 01:18:21 +0000951 default: break;
Rafael Espindola53190532012-03-30 15:52:11 +0000952 case Instruction::Load:
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000953 if (MDNode *MD = cast<LoadInst>(I)->getMetadata(LLVMContext::MD_range))
Jingyue Wu37fcb592014-06-19 16:50:16 +0000954 computeKnownBitsFromRangeMetadata(*MD, KnownZero);
Jay Foad5a29c362014-05-15 12:12:55 +0000955 break;
Chris Lattner965c7692008-06-02 01:18:21 +0000956 case Instruction::And: {
957 // If either the LHS or the RHS are Zero, the result is zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000958 computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q);
959 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
Craig Topper1bef2c82012-12-22 19:15:35 +0000960
Chris Lattner965c7692008-06-02 01:18:21 +0000961 // Output known-1 bits are only known if set in both the LHS & RHS.
962 KnownOne &= KnownOne2;
963 // Output known-0 are known to be clear if zero in either the LHS | RHS.
964 KnownZero |= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +0000965 break;
Chris Lattner965c7692008-06-02 01:18:21 +0000966 }
967 case Instruction::Or: {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000968 computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q);
969 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
Craig Topper1bef2c82012-12-22 19:15:35 +0000970
Chris Lattner965c7692008-06-02 01:18:21 +0000971 // Output known-0 bits are only known if clear in both the LHS & RHS.
972 KnownZero &= KnownZero2;
973 // Output known-1 are known to be set if set in either the LHS | RHS.
974 KnownOne |= KnownOne2;
Jay Foad5a29c362014-05-15 12:12:55 +0000975 break;
Chris Lattner965c7692008-06-02 01:18:21 +0000976 }
977 case Instruction::Xor: {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000978 computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q);
979 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
Craig Topper1bef2c82012-12-22 19:15:35 +0000980
Chris Lattner965c7692008-06-02 01:18:21 +0000981 // Output known-0 bits are known if clear or set in both the LHS & RHS.
982 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
983 // Output known-1 are known to be set if set in only one of the LHS, RHS.
984 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
985 KnownZero = KnownZeroOut;
Jay Foad5a29c362014-05-15 12:12:55 +0000986 break;
Chris Lattner965c7692008-06-02 01:18:21 +0000987 }
988 case Instruction::Mul: {
Nick Lewyckyfa306072012-03-18 23:28:48 +0000989 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000990 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, KnownZero,
991 KnownOne, KnownZero2, KnownOne2, DL, Depth, Q);
Nick Lewyckyfa306072012-03-18 23:28:48 +0000992 break;
Chris Lattner965c7692008-06-02 01:18:21 +0000993 }
994 case Instruction::UDiv: {
995 // For the purposes of computing leading zeros we can conservatively
996 // treat a udiv as a logical right shift by the power of 2 known to
997 // be less than the denominator.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000998 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +0000999 unsigned LeadZ = KnownZero2.countLeadingOnes();
1000
Jay Foad25a5e4c2010-12-01 08:53:58 +00001001 KnownOne2.clearAllBits();
1002 KnownZero2.clearAllBits();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001003 computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001004 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1005 if (RHSUnknownLeadingOnes != BitWidth)
1006 LeadZ = std::min(BitWidth,
1007 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
1008
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001009 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Jay Foad5a29c362014-05-15 12:12:55 +00001010 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001011 }
1012 case Instruction::Select:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001013 computeKnownBits(I->getOperand(2), KnownZero, KnownOne, DL, Depth + 1, Q);
1014 computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001015
1016 // Only known if known in both the LHS and RHS.
1017 KnownOne &= KnownOne2;
1018 KnownZero &= KnownZero2;
Jay Foad5a29c362014-05-15 12:12:55 +00001019 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001020 case Instruction::FPTrunc:
1021 case Instruction::FPExt:
1022 case Instruction::FPToUI:
1023 case Instruction::FPToSI:
1024 case Instruction::SIToFP:
1025 case Instruction::UIToFP:
Jay Foad5a29c362014-05-15 12:12:55 +00001026 break; // Can't work with floating point.
Chris Lattner965c7692008-06-02 01:18:21 +00001027 case Instruction::PtrToInt:
1028 case Instruction::IntToPtr:
Matt Arsenaultf1a7e622014-07-15 01:55:03 +00001029 case Instruction::AddrSpaceCast: // Pointers could be different sizes.
Chris Lattner965c7692008-06-02 01:18:21 +00001030 // FALL THROUGH and handle them the same as zext/trunc.
1031 case Instruction::ZExt:
1032 case Instruction::Trunc: {
Chris Lattner229907c2011-07-18 04:54:35 +00001033 Type *SrcTy = I->getOperand(0)->getType();
Nadav Rotem15198e92012-10-26 17:17:05 +00001034
Chris Lattner0cdbc7a2009-09-08 00:13:52 +00001035 unsigned SrcBitWidth;
Chris Lattner965c7692008-06-02 01:18:21 +00001036 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1037 // which fall through here.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001038 SrcBitWidth = DL.getTypeSizeInBits(SrcTy->getScalarType());
Nadav Rotem15198e92012-10-26 17:17:05 +00001039
1040 assert(SrcBitWidth && "SrcBitWidth can't be zero");
Jay Foad583abbc2010-12-07 08:25:19 +00001041 KnownZero = KnownZero.zextOrTrunc(SrcBitWidth);
1042 KnownOne = KnownOne.zextOrTrunc(SrcBitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001043 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
Jay Foad583abbc2010-12-07 08:25:19 +00001044 KnownZero = KnownZero.zextOrTrunc(BitWidth);
1045 KnownOne = KnownOne.zextOrTrunc(BitWidth);
Chris Lattner965c7692008-06-02 01:18:21 +00001046 // Any top bits are known to be zero.
1047 if (BitWidth > SrcBitWidth)
1048 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00001049 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001050 }
1051 case Instruction::BitCast: {
Chris Lattner229907c2011-07-18 04:54:35 +00001052 Type *SrcTy = I->getOperand(0)->getType();
Duncan Sands19d0b472010-02-16 11:11:14 +00001053 if ((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
Chris Lattneredb84072009-07-02 16:04:08 +00001054 // TODO: For now, not handling conversions like:
1055 // (bitcast i64 %x to <2 x i32>)
Duncan Sands19d0b472010-02-16 11:11:14 +00001056 !I->getType()->isVectorTy()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001057 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
Jay Foad5a29c362014-05-15 12:12:55 +00001058 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001059 }
1060 break;
1061 }
1062 case Instruction::SExt: {
1063 // Compute the bits in the result that are not present in the input.
Chris Lattner0cdbc7a2009-09-08 00:13:52 +00001064 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper1bef2c82012-12-22 19:15:35 +00001065
Jay Foad583abbc2010-12-07 08:25:19 +00001066 KnownZero = KnownZero.trunc(SrcBitWidth);
1067 KnownOne = KnownOne.trunc(SrcBitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001068 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
Jay Foad583abbc2010-12-07 08:25:19 +00001069 KnownZero = KnownZero.zext(BitWidth);
1070 KnownOne = KnownOne.zext(BitWidth);
Chris Lattner965c7692008-06-02 01:18:21 +00001071
1072 // If the sign bit of the input is known set or clear, then we know the
1073 // top bits of the result.
1074 if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero
1075 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
1076 else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set
1077 KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Jay Foad5a29c362014-05-15 12:12:55 +00001078 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001079 }
1080 case Instruction::Shl:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001081 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Chris Lattner965c7692008-06-02 01:18:21 +00001082 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1083 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001084 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001085 KnownZero <<= ShiftAmt;
1086 KnownOne <<= ShiftAmt;
1087 KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0
Chris Lattner965c7692008-06-02 01:18:21 +00001088 }
1089 break;
1090 case Instruction::LShr:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001091 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Chris Lattner965c7692008-06-02 01:18:21 +00001092 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1093 // Compute the new bits that are at the top now.
1094 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
Craig Topper1bef2c82012-12-22 19:15:35 +00001095
Chris Lattner965c7692008-06-02 01:18:21 +00001096 // Unsigned shift right.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001097 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001098 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
1099 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
1100 // high bits known zero.
1101 KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
Chris Lattner965c7692008-06-02 01:18:21 +00001102 }
1103 break;
1104 case Instruction::AShr:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001105 // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Chris Lattner965c7692008-06-02 01:18:21 +00001106 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1107 // Compute the new bits that are at the top now.
Chris Lattnerc86e67e2011-01-04 18:19:15 +00001108 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper1bef2c82012-12-22 19:15:35 +00001109
Chris Lattner965c7692008-06-02 01:18:21 +00001110 // Signed shift right.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001111 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001112 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
1113 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Craig Topper1bef2c82012-12-22 19:15:35 +00001114
Chris Lattner965c7692008-06-02 01:18:21 +00001115 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
1116 if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero.
1117 KnownZero |= HighBits;
1118 else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one.
1119 KnownOne |= HighBits;
Chris Lattner965c7692008-06-02 01:18:21 +00001120 }
1121 break;
1122 case Instruction::Sub: {
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001123 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
Jay Foada0653a32014-05-14 21:14:37 +00001124 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001125 KnownZero, KnownOne, KnownZero2, KnownOne2, DL,
1126 Depth, Q);
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001127 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001128 }
Chris Lattner965c7692008-06-02 01:18:21 +00001129 case Instruction::Add: {
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001130 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
Jay Foada0653a32014-05-14 21:14:37 +00001131 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001132 KnownZero, KnownOne, KnownZero2, KnownOne2, DL,
1133 Depth, Q);
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001134 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001135 }
1136 case Instruction::SRem:
1137 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Duncan Sands26cd6bd2010-01-29 06:18:37 +00001138 APInt RA = Rem->getValue().abs();
1139 if (RA.isPowerOf2()) {
1140 APInt LowBits = RA - 1;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001141 computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1,
1142 Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001143
Duncan Sands26cd6bd2010-01-29 06:18:37 +00001144 // The low bits of the first operand are unchanged by the srem.
1145 KnownZero = KnownZero2 & LowBits;
1146 KnownOne = KnownOne2 & LowBits;
Chris Lattner965c7692008-06-02 01:18:21 +00001147
Duncan Sands26cd6bd2010-01-29 06:18:37 +00001148 // If the first operand is non-negative or has all low bits zero, then
1149 // the upper bits are all zero.
1150 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1151 KnownZero |= ~LowBits;
1152
1153 // If the first operand is negative and not all low bits are zero, then
1154 // the upper bits are all one.
1155 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
1156 KnownOne |= ~LowBits;
1157
Craig Topper1bef2c82012-12-22 19:15:35 +00001158 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattner965c7692008-06-02 01:18:21 +00001159 }
1160 }
Nick Lewyckye4679792011-03-07 01:50:10 +00001161
1162 // The sign bit is the LHS's sign bit, except when the result of the
1163 // remainder is zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001164 if (KnownZero.isNonNegative()) {
Nick Lewyckye4679792011-03-07 01:50:10 +00001165 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001166 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, DL,
1167 Depth + 1, Q);
Nick Lewyckye4679792011-03-07 01:50:10 +00001168 // If it's known zero, our sign bit is also zero.
1169 if (LHSKnownZero.isNegative())
Duncan Sands34c48692012-04-30 11:56:58 +00001170 KnownZero.setBit(BitWidth - 1);
Nick Lewyckye4679792011-03-07 01:50:10 +00001171 }
1172
Chris Lattner965c7692008-06-02 01:18:21 +00001173 break;
1174 case Instruction::URem: {
1175 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
1176 APInt RA = Rem->getValue();
1177 if (RA.isPowerOf2()) {
1178 APInt LowBits = (RA - 1);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001179 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1,
1180 Q);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001181 KnownZero |= ~LowBits;
1182 KnownOne &= LowBits;
Chris Lattner965c7692008-06-02 01:18:21 +00001183 break;
1184 }
1185 }
1186
1187 // Since the result is less than or equal to either operand, any leading
1188 // zero bits in either operand must also exist in the result.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001189 computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1190 computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001191
Chris Lattner4612ae12009-01-20 18:22:57 +00001192 unsigned Leaders = std::max(KnownZero.countLeadingOnes(),
Chris Lattner965c7692008-06-02 01:18:21 +00001193 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00001194 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001195 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Chris Lattner965c7692008-06-02 01:18:21 +00001196 break;
1197 }
1198
Victor Hernandeza3aaf852009-10-17 01:18:07 +00001199 case Instruction::Alloca: {
Jingyue Wu12b0c282015-06-15 05:46:29 +00001200 AllocaInst *AI = cast<AllocaInst>(I);
Chris Lattner965c7692008-06-02 01:18:21 +00001201 unsigned Align = AI->getAlignment();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001202 if (Align == 0)
1203 Align = DL.getABITypeAlignment(AI->getType()->getElementType());
Craig Topper1bef2c82012-12-22 19:15:35 +00001204
Chris Lattner965c7692008-06-02 01:18:21 +00001205 if (Align > 0)
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001206 KnownZero = APInt::getLowBitsSet(BitWidth, countTrailingZeros(Align));
Chris Lattner965c7692008-06-02 01:18:21 +00001207 break;
1208 }
1209 case Instruction::GetElementPtr: {
1210 // Analyze all of the subscripts of this getelementptr instruction
1211 // to determine if we can prove known low zero bits.
Chris Lattner965c7692008-06-02 01:18:21 +00001212 APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001213 computeKnownBits(I->getOperand(0), LocalKnownZero, LocalKnownOne, DL,
1214 Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001215 unsigned TrailZ = LocalKnownZero.countTrailingOnes();
1216
1217 gep_type_iterator GTI = gep_type_begin(I);
1218 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1219 Value *Index = I->getOperand(i);
Chris Lattner229907c2011-07-18 04:54:35 +00001220 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Chris Lattner965c7692008-06-02 01:18:21 +00001221 // Handle struct member offset arithmetic.
Matt Arsenault74742a12013-08-19 21:43:16 +00001222
1223 // Handle case when index is vector zeroinitializer
1224 Constant *CIndex = cast<Constant>(Index);
1225 if (CIndex->isZeroValue())
1226 continue;
1227
1228 if (CIndex->getType()->isVectorTy())
1229 Index = CIndex->getSplatValue();
1230
Chris Lattner965c7692008-06-02 01:18:21 +00001231 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001232 const StructLayout *SL = DL.getStructLayout(STy);
Chris Lattner965c7692008-06-02 01:18:21 +00001233 uint64_t Offset = SL->getElementOffset(Idx);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001234 TrailZ = std::min<unsigned>(TrailZ,
1235 countTrailingZeros(Offset));
Chris Lattner965c7692008-06-02 01:18:21 +00001236 } else {
1237 // Handle array index arithmetic.
Chris Lattner229907c2011-07-18 04:54:35 +00001238 Type *IndexedTy = GTI.getIndexedType();
Jay Foad5a29c362014-05-15 12:12:55 +00001239 if (!IndexedTy->isSized()) {
1240 TrailZ = 0;
1241 break;
1242 }
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001243 unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001244 uint64_t TypeSize = DL.getTypeAllocSize(IndexedTy);
Chris Lattner965c7692008-06-02 01:18:21 +00001245 LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001246 computeKnownBits(Index, LocalKnownZero, LocalKnownOne, DL, Depth + 1,
1247 Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001248 TrailZ = std::min(TrailZ,
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001249 unsigned(countTrailingZeros(TypeSize) +
Chris Lattner4612ae12009-01-20 18:22:57 +00001250 LocalKnownZero.countTrailingOnes()));
Chris Lattner965c7692008-06-02 01:18:21 +00001251 }
1252 }
Craig Topper1bef2c82012-12-22 19:15:35 +00001253
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001254 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ);
Chris Lattner965c7692008-06-02 01:18:21 +00001255 break;
1256 }
1257 case Instruction::PHI: {
1258 PHINode *P = cast<PHINode>(I);
1259 // Handle the case of a simple two-predecessor recurrence PHI.
1260 // There's a lot more that could theoretically be done here, but
1261 // this is sufficient to catch some interesting cases.
1262 if (P->getNumIncomingValues() == 2) {
1263 for (unsigned i = 0; i != 2; ++i) {
1264 Value *L = P->getIncomingValue(i);
1265 Value *R = P->getIncomingValue(!i);
Dan Gohman80ca01c2009-07-17 20:47:02 +00001266 Operator *LU = dyn_cast<Operator>(L);
Chris Lattner965c7692008-06-02 01:18:21 +00001267 if (!LU)
1268 continue;
Dan Gohman80ca01c2009-07-17 20:47:02 +00001269 unsigned Opcode = LU->getOpcode();
Chris Lattner965c7692008-06-02 01:18:21 +00001270 // Check for operations that have the property that if
1271 // both their operands have low zero bits, the result
1272 // will have low zero bits.
1273 if (Opcode == Instruction::Add ||
1274 Opcode == Instruction::Sub ||
1275 Opcode == Instruction::And ||
1276 Opcode == Instruction::Or ||
1277 Opcode == Instruction::Mul) {
1278 Value *LL = LU->getOperand(0);
1279 Value *LR = LU->getOperand(1);
1280 // Find a recurrence.
1281 if (LL == I)
1282 L = LR;
1283 else if (LR == I)
1284 L = LL;
1285 else
1286 break;
1287 // Ok, we have a PHI of the form L op= R. Check for low
1288 // zero bits.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001289 computeKnownBits(R, KnownZero2, KnownOne2, DL, Depth + 1, Q);
David Greeneaebd9e02008-10-27 23:24:03 +00001290
1291 // We need to take the minimum number of known bits
1292 APInt KnownZero3(KnownZero), KnownOne3(KnownOne);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001293 computeKnownBits(L, KnownZero3, KnownOne3, DL, Depth + 1, Q);
David Greeneaebd9e02008-10-27 23:24:03 +00001294
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001295 KnownZero = APInt::getLowBitsSet(BitWidth,
David Greeneaebd9e02008-10-27 23:24:03 +00001296 std::min(KnownZero2.countTrailingOnes(),
1297 KnownZero3.countTrailingOnes()));
Chris Lattner965c7692008-06-02 01:18:21 +00001298 break;
1299 }
1300 }
1301 }
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001302
Nick Lewyckyac0b62c2011-02-10 23:54:10 +00001303 // Unreachable blocks may have zero-operand PHI nodes.
1304 if (P->getNumIncomingValues() == 0)
Jay Foad5a29c362014-05-15 12:12:55 +00001305 break;
Nick Lewyckyac0b62c2011-02-10 23:54:10 +00001306
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001307 // Otherwise take the unions of the known bit sets of the operands,
1308 // taking conservative care to avoid excessive recursion.
1309 if (Depth < MaxDepth - 1 && !KnownZero && !KnownOne) {
Duncan Sands7dc3d472011-03-08 12:39:03 +00001310 // Skip if every incoming value references to ourself.
Nuno Lopes0d44a502012-07-03 21:15:40 +00001311 if (dyn_cast_or_null<UndefValue>(P->hasConstantValue()))
Duncan Sands7dc3d472011-03-08 12:39:03 +00001312 break;
1313
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001314 KnownZero = APInt::getAllOnesValue(BitWidth);
1315 KnownOne = APInt::getAllOnesValue(BitWidth);
Pete Cooper833f34d2015-05-12 20:05:31 +00001316 for (Value *IncValue : P->incoming_values()) {
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001317 // Skip direct self references.
Pete Cooper833f34d2015-05-12 20:05:31 +00001318 if (IncValue == P) continue;
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001319
1320 KnownZero2 = APInt(BitWidth, 0);
1321 KnownOne2 = APInt(BitWidth, 0);
1322 // Recurse, but cap the recursion to one level, because we don't
1323 // want to waste time spinning around in loops.
Pete Cooper833f34d2015-05-12 20:05:31 +00001324 computeKnownBits(IncValue, KnownZero2, KnownOne2, DL,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001325 MaxDepth - 1, Q);
Dan Gohmanbf0002e2009-05-21 02:28:33 +00001326 KnownZero &= KnownZero2;
1327 KnownOne &= KnownOne2;
1328 // If all bits have been ruled out, there's no need to check
1329 // more operands.
1330 if (!KnownZero && !KnownOne)
1331 break;
1332 }
1333 }
Chris Lattner965c7692008-06-02 01:18:21 +00001334 break;
1335 }
1336 case Instruction::Call:
Jingyue Wu37fcb592014-06-19 16:50:16 +00001337 case Instruction::Invoke:
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001338 if (MDNode *MD = cast<Instruction>(I)->getMetadata(LLVMContext::MD_range))
Jingyue Wu37fcb592014-06-19 16:50:16 +00001339 computeKnownBitsFromRangeMetadata(*MD, KnownZero);
1340 // If a range metadata is attached to this IntrinsicInst, intersect the
1341 // explicit range specified by the metadata and the implicit range of
1342 // the intrinsic.
Chris Lattner965c7692008-06-02 01:18:21 +00001343 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1344 switch (II->getIntrinsicID()) {
1345 default: break;
Chris Lattner965c7692008-06-02 01:18:21 +00001346 case Intrinsic::ctlz:
1347 case Intrinsic::cttz: {
1348 unsigned LowBits = Log2_32(BitWidth)+1;
Benjamin Kramer4ee57472011-12-24 17:31:46 +00001349 // If this call is undefined for 0, the result will be less than 2^n.
1350 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1351 LowBits -= 1;
Jingyue Wu37fcb592014-06-19 16:50:16 +00001352 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Benjamin Kramer4ee57472011-12-24 17:31:46 +00001353 break;
1354 }
1355 case Intrinsic::ctpop: {
1356 unsigned LowBits = Log2_32(BitWidth)+1;
Jingyue Wu37fcb592014-06-19 16:50:16 +00001357 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Chris Lattner965c7692008-06-02 01:18:21 +00001358 break;
1359 }
Chad Rosierb3628842011-05-26 23:13:19 +00001360 case Intrinsic::x86_sse42_crc32_64_64:
Jingyue Wu37fcb592014-06-19 16:50:16 +00001361 KnownZero |= APInt::getHighBitsSet(64, 32);
Evan Cheng2a746bf2011-05-22 18:25:30 +00001362 break;
Chris Lattner965c7692008-06-02 01:18:21 +00001363 }
1364 }
1365 break;
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001366 case Instruction::ExtractValue:
1367 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
1368 ExtractValueInst *EVI = cast<ExtractValueInst>(I);
1369 if (EVI->getNumIndices() != 1) break;
1370 if (EVI->getIndices()[0] == 0) {
1371 switch (II->getIntrinsicID()) {
1372 default: break;
1373 case Intrinsic::uadd_with_overflow:
1374 case Intrinsic::sadd_with_overflow:
Jay Foada0653a32014-05-14 21:14:37 +00001375 computeKnownBitsAddSub(true, II->getArgOperand(0),
1376 II->getArgOperand(1), false, KnownZero,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001377 KnownOne, KnownZero2, KnownOne2, DL, Depth, Q);
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001378 break;
1379 case Intrinsic::usub_with_overflow:
1380 case Intrinsic::ssub_with_overflow:
Jay Foada0653a32014-05-14 21:14:37 +00001381 computeKnownBitsAddSub(false, II->getArgOperand(0),
1382 II->getArgOperand(1), false, KnownZero,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001383 KnownOne, KnownZero2, KnownOne2, DL, Depth, Q);
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001384 break;
Nick Lewyckyfa306072012-03-18 23:28:48 +00001385 case Intrinsic::umul_with_overflow:
1386 case Intrinsic::smul_with_overflow:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001387 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
1388 KnownZero, KnownOne, KnownZero2, KnownOne2, DL,
1389 Depth, Q);
Nick Lewyckyfa306072012-03-18 23:28:48 +00001390 break;
Nick Lewyckyfea3e002012-03-09 09:23:50 +00001391 }
1392 }
1393 }
Chris Lattner965c7692008-06-02 01:18:21 +00001394 }
Jingyue Wu12b0c282015-06-15 05:46:29 +00001395}
1396
1397/// Determine which bits of V are known to be either zero or one and return
1398/// them in the KnownZero/KnownOne bit sets.
1399///
1400/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
1401/// we cannot optimize based on the assumption that it is zero without changing
1402/// it to be an explicit zero. If we don't change it to zero, other code could
1403/// optimized based on the contradictory assumption that it is non-zero.
1404/// Because instcombine aggressively folds operations with undef args anyway,
1405/// this won't lose us code quality.
1406///
1407/// This function is defined on values with integer type, values with pointer
1408/// type, and vectors of integers. In the case
1409/// where V is a vector, known zero, and known one values are the
1410/// same width as the vector element, and the bit is set only if it is true
1411/// for all of the elements in the vector.
1412void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
1413 const DataLayout &DL, unsigned Depth, const Query &Q) {
1414 assert(V && "No Value?");
1415 assert(Depth <= MaxDepth && "Limit Search Depth");
1416 unsigned BitWidth = KnownZero.getBitWidth();
1417
1418 assert((V->getType()->isIntOrIntVectorTy() ||
1419 V->getType()->getScalarType()->isPointerTy()) &&
1420 "Not integer or pointer type!");
1421 assert((DL.getTypeSizeInBits(V->getType()->getScalarType()) == BitWidth) &&
1422 (!V->getType()->isIntOrIntVectorTy() ||
1423 V->getType()->getScalarSizeInBits() == BitWidth) &&
1424 KnownZero.getBitWidth() == BitWidth &&
1425 KnownOne.getBitWidth() == BitWidth &&
1426 "V, KnownOne and KnownZero should have same BitWidth");
1427
1428 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
1429 // We know all of the bits for a constant!
1430 KnownOne = CI->getValue();
1431 KnownZero = ~KnownOne;
1432 return;
1433 }
1434 // Null and aggregate-zero are all-zeros.
1435 if (isa<ConstantPointerNull>(V) ||
1436 isa<ConstantAggregateZero>(V)) {
1437 KnownOne.clearAllBits();
1438 KnownZero = APInt::getAllOnesValue(BitWidth);
1439 return;
1440 }
1441 // Handle a constant vector by taking the intersection of the known bits of
1442 // each element. There is no real need to handle ConstantVector here, because
1443 // we don't handle undef in any particularly useful way.
1444 if (ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) {
1445 // We know that CDS must be a vector of integers. Take the intersection of
1446 // each element.
1447 KnownZero.setAllBits(); KnownOne.setAllBits();
1448 APInt Elt(KnownZero.getBitWidth(), 0);
1449 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1450 Elt = CDS->getElementAsInteger(i);
1451 KnownZero &= ~Elt;
1452 KnownOne &= Elt;
1453 }
1454 return;
1455 }
1456
1457 // The address of an aligned GlobalValue has trailing zeros.
1458 if (auto *GO = dyn_cast<GlobalObject>(V)) {
1459 unsigned Align = GO->getAlignment();
1460 if (Align == 0) {
1461 if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
1462 Type *ObjectType = GVar->getType()->getElementType();
1463 if (ObjectType->isSized()) {
1464 // If the object is defined in the current Module, we'll be giving
1465 // it the preferred alignment. Otherwise, we have to assume that it
1466 // may only have the minimum ABI alignment.
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001467 if (GVar->isStrongDefinitionForLinker())
Jingyue Wu12b0c282015-06-15 05:46:29 +00001468 Align = DL.getPreferredAlignment(GVar);
1469 else
1470 Align = DL.getABITypeAlignment(ObjectType);
1471 }
1472 }
1473 }
1474 if (Align > 0)
1475 KnownZero = APInt::getLowBitsSet(BitWidth,
1476 countTrailingZeros(Align));
1477 else
1478 KnownZero.clearAllBits();
1479 KnownOne.clearAllBits();
1480 return;
1481 }
1482
1483 if (Argument *A = dyn_cast<Argument>(V)) {
1484 unsigned Align = A->getType()->isPointerTy() ? A->getParamAlignment() : 0;
1485
1486 if (!Align && A->hasStructRetAttr()) {
1487 // An sret parameter has at least the ABI alignment of the return type.
1488 Type *EltTy = cast<PointerType>(A->getType())->getElementType();
1489 if (EltTy->isSized())
1490 Align = DL.getABITypeAlignment(EltTy);
1491 }
1492
1493 if (Align)
1494 KnownZero = APInt::getLowBitsSet(BitWidth, countTrailingZeros(Align));
1495 else
1496 KnownZero.clearAllBits();
1497 KnownOne.clearAllBits();
1498
1499 // Don't give up yet... there might be an assumption that provides more
1500 // information...
1501 computeKnownBitsFromAssume(V, KnownZero, KnownOne, DL, Depth, Q);
1502
1503 // Or a dominating condition for that matter
1504 if (EnableDomConditions && Depth <= DomConditionsMaxDepth)
1505 computeKnownBitsFromDominatingCondition(V, KnownZero, KnownOne, DL,
1506 Depth, Q);
1507 return;
1508 }
1509
1510 // Start out not knowing anything.
1511 KnownZero.clearAllBits(); KnownOne.clearAllBits();
1512
1513 // Limit search depth.
1514 // All recursive calls that increase depth must come after this.
1515 if (Depth == MaxDepth)
1516 return;
1517
1518 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
1519 // the bits of its aliasee.
1520 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
1521 if (!GA->mayBeOverridden())
1522 computeKnownBits(GA->getAliasee(), KnownZero, KnownOne, DL, Depth + 1, Q);
1523 return;
1524 }
1525
1526 if (Operator *I = dyn_cast<Operator>(V))
1527 computeKnownBitsFromOperator(I, KnownZero, KnownOne, DL, Depth, Q);
1528 // computeKnownBitsFromAssume and computeKnownBitsFromDominatingCondition
1529 // strictly refines KnownZero and KnownOne. Therefore, we run them after
1530 // computeKnownBitsFromOperator.
1531
1532 // Check whether a nearby assume intrinsic can determine some known bits.
1533 computeKnownBitsFromAssume(V, KnownZero, KnownOne, DL, Depth, Q);
1534
1535 // Check whether there's a dominating condition which implies something about
1536 // this value at the given context.
1537 if (EnableDomConditions && Depth <= DomConditionsMaxDepth)
1538 computeKnownBitsFromDominatingCondition(V, KnownZero, KnownOne, DL, Depth,
1539 Q);
Jay Foad5a29c362014-05-15 12:12:55 +00001540
1541 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattner965c7692008-06-02 01:18:21 +00001542}
1543
Sanjay Patelaee84212014-11-04 16:27:42 +00001544/// Determine whether the sign bit is known to be zero or one.
1545/// Convenience wrapper around computeKnownBits.
Hal Finkel60db0582014-09-07 18:57:58 +00001546void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001547 const DataLayout &DL, unsigned Depth, const Query &Q) {
1548 unsigned BitWidth = getBitWidth(V->getType(), DL);
Duncan Sandsd3951082011-01-25 09:38:29 +00001549 if (!BitWidth) {
1550 KnownZero = false;
1551 KnownOne = false;
1552 return;
1553 }
1554 APInt ZeroBits(BitWidth, 0);
1555 APInt OneBits(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001556 computeKnownBits(V, ZeroBits, OneBits, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001557 KnownOne = OneBits[BitWidth - 1];
1558 KnownZero = ZeroBits[BitWidth - 1];
1559}
1560
Sanjay Patelaee84212014-11-04 16:27:42 +00001561/// Return true if the given value is known to have exactly one
Duncan Sandsd3951082011-01-25 09:38:29 +00001562/// bit set when defined. For vectors return true if every element is known to
Sanjay Patelaee84212014-11-04 16:27:42 +00001563/// be a power of two when defined. Supports values with integer or pointer
Duncan Sandsd3951082011-01-25 09:38:29 +00001564/// types and vectors of integers.
Hal Finkel60db0582014-09-07 18:57:58 +00001565bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero, unsigned Depth,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001566 const Query &Q, const DataLayout &DL) {
Duncan Sandsba286d72011-10-26 20:55:21 +00001567 if (Constant *C = dyn_cast<Constant>(V)) {
1568 if (C->isNullValue())
1569 return OrZero;
1570 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
1571 return CI->getValue().isPowerOf2();
1572 // TODO: Handle vector constants.
1573 }
Duncan Sandsd3951082011-01-25 09:38:29 +00001574
1575 // 1 << X is clearly a power of two if the one is not shifted off the end. If
1576 // it is shifted off the end then the result is undefined.
1577 if (match(V, m_Shl(m_One(), m_Value())))
1578 return true;
1579
1580 // (signbit) >>l X is clearly a power of two if the one is not shifted off the
1581 // bottom. If it is shifted off the bottom then the result is undefined.
Duncan Sands4b397fc2011-02-01 08:50:33 +00001582 if (match(V, m_LShr(m_SignBit(), m_Value())))
Duncan Sandsd3951082011-01-25 09:38:29 +00001583 return true;
1584
1585 // The remaining tests are all recursive, so bail out if we hit the limit.
1586 if (Depth++ == MaxDepth)
1587 return false;
1588
Craig Topper9f008862014-04-15 04:59:12 +00001589 Value *X = nullptr, *Y = nullptr;
Duncan Sands985ba632011-10-28 18:30:05 +00001590 // A shift of a power of two is a power of two or zero.
1591 if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) ||
1592 match(V, m_Shr(m_Value(X), m_Value()))))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001593 return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q, DL);
Duncan Sands985ba632011-10-28 18:30:05 +00001594
Duncan Sandsd3951082011-01-25 09:38:29 +00001595 if (ZExtInst *ZI = dyn_cast<ZExtInst>(V))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001596 return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q, DL);
Duncan Sandsd3951082011-01-25 09:38:29 +00001597
1598 if (SelectInst *SI = dyn_cast<SelectInst>(V))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001599 return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q, DL) &&
1600 isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q, DL);
Duncan Sandsba286d72011-10-26 20:55:21 +00001601
Duncan Sandsba286d72011-10-26 20:55:21 +00001602 if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) {
1603 // A power of two and'd with anything is a power of two or zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001604 if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q, DL) ||
1605 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q, DL))
Duncan Sandsba286d72011-10-26 20:55:21 +00001606 return true;
1607 // X & (-X) is always a power of two or zero.
1608 if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X))))
1609 return true;
1610 return false;
1611 }
Duncan Sandsd3951082011-01-25 09:38:29 +00001612
David Majnemerb7d54092013-07-30 21:01:36 +00001613 // Adding a power-of-two or zero to the same power-of-two or zero yields
1614 // either the original power-of-two, a larger power-of-two or zero.
1615 if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
1616 OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
1617 if (OrZero || VOBO->hasNoUnsignedWrap() || VOBO->hasNoSignedWrap()) {
1618 if (match(X, m_And(m_Specific(Y), m_Value())) ||
1619 match(X, m_And(m_Value(), m_Specific(Y))))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001620 if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q, DL))
David Majnemerb7d54092013-07-30 21:01:36 +00001621 return true;
1622 if (match(Y, m_And(m_Specific(X), m_Value())) ||
1623 match(Y, m_And(m_Value(), m_Specific(X))))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001624 if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q, DL))
David Majnemerb7d54092013-07-30 21:01:36 +00001625 return true;
1626
1627 unsigned BitWidth = V->getType()->getScalarSizeInBits();
1628 APInt LHSZeroBits(BitWidth, 0), LHSOneBits(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001629 computeKnownBits(X, LHSZeroBits, LHSOneBits, DL, Depth, Q);
David Majnemerb7d54092013-07-30 21:01:36 +00001630
1631 APInt RHSZeroBits(BitWidth, 0), RHSOneBits(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001632 computeKnownBits(Y, RHSZeroBits, RHSOneBits, DL, Depth, Q);
David Majnemerb7d54092013-07-30 21:01:36 +00001633 // If i8 V is a power of two or zero:
1634 // ZeroBits: 1 1 1 0 1 1 1 1
1635 // ~ZeroBits: 0 0 0 1 0 0 0 0
1636 if ((~(LHSZeroBits & RHSZeroBits)).isPowerOf2())
1637 // If OrZero isn't set, we cannot give back a zero result.
1638 // Make sure either the LHS or RHS has a bit set.
1639 if (OrZero || RHSOneBits.getBoolValue() || LHSOneBits.getBoolValue())
1640 return true;
1641 }
1642 }
David Majnemerbeab5672013-05-18 19:30:37 +00001643
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001644 // An exact divide or right shift can only shift off zero bits, so the result
Nick Lewyckyf0469af2011-03-21 21:40:32 +00001645 // is a power of two only if the first operand is a power of two and not
1646 // copying a sign bit (sdiv int_min, 2).
Benjamin Kramer9442cd02012-01-01 17:55:30 +00001647 if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) ||
1648 match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) {
Hal Finkel60db0582014-09-07 18:57:58 +00001649 return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001650 Depth, Q, DL);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001651 }
1652
Duncan Sandsd3951082011-01-25 09:38:29 +00001653 return false;
1654}
1655
Chandler Carruth80d3e562012-12-07 02:08:58 +00001656/// \brief Test whether a GEP's result is known to be non-null.
1657///
1658/// Uses properties inherent in a GEP to try to determine whether it is known
1659/// to be non-null.
1660///
1661/// Currently this routine does not support vector GEPs.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001662static bool isGEPKnownNonNull(GEPOperator *GEP, const DataLayout &DL,
Hal Finkel60db0582014-09-07 18:57:58 +00001663 unsigned Depth, const Query &Q) {
Chandler Carruth80d3e562012-12-07 02:08:58 +00001664 if (!GEP->isInBounds() || GEP->getPointerAddressSpace() != 0)
1665 return false;
1666
1667 // FIXME: Support vector-GEPs.
1668 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
1669
1670 // If the base pointer is non-null, we cannot walk to a null address with an
1671 // inbounds GEP in address space zero.
Hal Finkel60db0582014-09-07 18:57:58 +00001672 if (isKnownNonZero(GEP->getPointerOperand(), DL, Depth, Q))
Chandler Carruth80d3e562012-12-07 02:08:58 +00001673 return true;
1674
Chandler Carruth80d3e562012-12-07 02:08:58 +00001675 // Walk the GEP operands and see if any operand introduces a non-zero offset.
1676 // If so, then the GEP cannot produce a null pointer, as doing so would
1677 // inherently violate the inbounds contract within address space zero.
1678 for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
1679 GTI != GTE; ++GTI) {
1680 // Struct types are easy -- they must always be indexed by a constant.
1681 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
1682 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
1683 unsigned ElementIdx = OpC->getZExtValue();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001684 const StructLayout *SL = DL.getStructLayout(STy);
Chandler Carruth80d3e562012-12-07 02:08:58 +00001685 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
1686 if (ElementOffset > 0)
1687 return true;
1688 continue;
1689 }
1690
1691 // If we have a zero-sized type, the index doesn't matter. Keep looping.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001692 if (DL.getTypeAllocSize(GTI.getIndexedType()) == 0)
Chandler Carruth80d3e562012-12-07 02:08:58 +00001693 continue;
1694
1695 // Fast path the constant operand case both for efficiency and so we don't
1696 // increment Depth when just zipping down an all-constant GEP.
1697 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
1698 if (!OpC->isZero())
1699 return true;
1700 continue;
1701 }
1702
1703 // We post-increment Depth here because while isKnownNonZero increments it
1704 // as well, when we pop back up that increment won't persist. We don't want
1705 // to recurse 10k times just because we have 10k GEP operands. We don't
1706 // bail completely out because we want to handle constant GEPs regardless
1707 // of depth.
1708 if (Depth++ >= MaxDepth)
1709 continue;
1710
Hal Finkel60db0582014-09-07 18:57:58 +00001711 if (isKnownNonZero(GTI.getOperand(), DL, Depth, Q))
Chandler Carruth80d3e562012-12-07 02:08:58 +00001712 return true;
1713 }
1714
1715 return false;
1716}
1717
Philip Reames4cb4d3e2014-10-30 20:25:19 +00001718/// Does the 'Range' metadata (which must be a valid MD_range operand list)
1719/// ensure that the value it's attached to is never Value? 'RangeType' is
1720/// is the type of the value described by the range.
1721static bool rangeMetadataExcludesValue(MDNode* Ranges,
1722 const APInt& Value) {
1723 const unsigned NumRanges = Ranges->getNumOperands() / 2;
1724 assert(NumRanges >= 1);
1725 for (unsigned i = 0; i < NumRanges; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001726 ConstantInt *Lower =
1727 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
1728 ConstantInt *Upper =
1729 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
Philip Reames4cb4d3e2014-10-30 20:25:19 +00001730 ConstantRange Range(Lower->getValue(), Upper->getValue());
1731 if (Range.contains(Value))
1732 return false;
1733 }
1734 return true;
1735}
1736
Sanjay Patelaee84212014-11-04 16:27:42 +00001737/// Return true if the given value is known to be non-zero when defined.
1738/// For vectors return true if every element is known to be non-zero when
1739/// defined. Supports values with integer or pointer type and vectors of
1740/// integers.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001741bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth,
Hal Finkel60db0582014-09-07 18:57:58 +00001742 const Query &Q) {
Duncan Sandsd3951082011-01-25 09:38:29 +00001743 if (Constant *C = dyn_cast<Constant>(V)) {
1744 if (C->isNullValue())
1745 return false;
1746 if (isa<ConstantInt>(C))
1747 // Must be non-zero due to null test above.
1748 return true;
1749 // TODO: Handle vectors
1750 return false;
1751 }
1752
Philip Reames4cb4d3e2014-10-30 20:25:19 +00001753 if (Instruction* I = dyn_cast<Instruction>(V)) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001754 if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range)) {
Philip Reames4cb4d3e2014-10-30 20:25:19 +00001755 // If the possible ranges don't contain zero, then the value is
1756 // definitely non-zero.
1757 if (IntegerType* Ty = dyn_cast<IntegerType>(V->getType())) {
1758 const APInt ZeroValue(Ty->getBitWidth(), 0);
1759 if (rangeMetadataExcludesValue(Ranges, ZeroValue))
1760 return true;
1761 }
1762 }
1763 }
1764
Duncan Sandsd3951082011-01-25 09:38:29 +00001765 // The remaining tests are all recursive, so bail out if we hit the limit.
Duncan Sands7cb61e52011-10-27 19:16:21 +00001766 if (Depth++ >= MaxDepth)
Duncan Sandsd3951082011-01-25 09:38:29 +00001767 return false;
1768
Chandler Carruth80d3e562012-12-07 02:08:58 +00001769 // Check for pointer simplifications.
1770 if (V->getType()->isPointerTy()) {
Manman Ren12171122013-03-18 21:23:25 +00001771 if (isKnownNonNull(V))
1772 return true;
Chandler Carruth80d3e562012-12-07 02:08:58 +00001773 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001774 if (isGEPKnownNonNull(GEP, DL, Depth, Q))
Chandler Carruth80d3e562012-12-07 02:08:58 +00001775 return true;
1776 }
1777
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001778 unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), DL);
Duncan Sandsd3951082011-01-25 09:38:29 +00001779
1780 // X | Y != 0 if X != 0 or Y != 0.
Craig Topper9f008862014-04-15 04:59:12 +00001781 Value *X = nullptr, *Y = nullptr;
Duncan Sandsd3951082011-01-25 09:38:29 +00001782 if (match(V, m_Or(m_Value(X), m_Value(Y))))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001783 return isKnownNonZero(X, DL, Depth, Q) || isKnownNonZero(Y, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001784
1785 // ext X != 0 if X != 0.
1786 if (isa<SExtInst>(V) || isa<ZExtInst>(V))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001787 return isKnownNonZero(cast<Instruction>(V)->getOperand(0), DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001788
Duncan Sands2e9e4f12011-01-29 13:27:00 +00001789 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
Duncan Sandsd3951082011-01-25 09:38:29 +00001790 // if the lowest bit is shifted off the end.
1791 if (BitWidth && match(V, m_Shl(m_Value(X), m_Value(Y)))) {
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001792 // shl nuw can't remove any non-zero bits.
Duncan Sands7cb61e52011-10-27 19:16:21 +00001793 OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001794 if (BO->hasNoUnsignedWrap())
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001795 return isKnownNonZero(X, DL, Depth, Q);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001796
Duncan Sandsd3951082011-01-25 09:38:29 +00001797 APInt KnownZero(BitWidth, 0);
1798 APInt KnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001799 computeKnownBits(X, KnownZero, KnownOne, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001800 if (KnownOne[0])
1801 return true;
1802 }
Duncan Sands2e9e4f12011-01-29 13:27:00 +00001803 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
Duncan Sandsd3951082011-01-25 09:38:29 +00001804 // defined if the sign bit is shifted off the end.
1805 else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) {
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001806 // shr exact can only shift out zero bits.
Duncan Sands7cb61e52011-10-27 19:16:21 +00001807 PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001808 if (BO->isExact())
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001809 return isKnownNonZero(X, DL, Depth, Q);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001810
Duncan Sandsd3951082011-01-25 09:38:29 +00001811 bool XKnownNonNegative, XKnownNegative;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001812 ComputeSignBit(X, XKnownNonNegative, XKnownNegative, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001813 if (XKnownNegative)
1814 return true;
1815 }
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001816 // div exact can only produce a zero if the dividend is zero.
Benjamin Kramer9442cd02012-01-01 17:55:30 +00001817 else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001818 return isKnownNonZero(X, DL, Depth, Q);
Nick Lewyckyc9aab852011-02-28 08:02:21 +00001819 }
Duncan Sandsd3951082011-01-25 09:38:29 +00001820 // X + Y.
1821 else if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
1822 bool XKnownNonNegative, XKnownNegative;
1823 bool YKnownNonNegative, YKnownNegative;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001824 ComputeSignBit(X, XKnownNonNegative, XKnownNegative, DL, Depth, Q);
1825 ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001826
1827 // If X and Y are both non-negative (as signed values) then their sum is not
Duncan Sands9e9d5b22011-01-25 15:14:15 +00001828 // zero unless both X and Y are zero.
Duncan Sandsd3951082011-01-25 09:38:29 +00001829 if (XKnownNonNegative && YKnownNonNegative)
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001830 if (isKnownNonZero(X, DL, Depth, Q) || isKnownNonZero(Y, DL, Depth, Q))
Duncan Sands9e9d5b22011-01-25 15:14:15 +00001831 return true;
Duncan Sandsd3951082011-01-25 09:38:29 +00001832
1833 // If X and Y are both negative (as signed values) then their sum is not
1834 // zero unless both X and Y equal INT_MIN.
1835 if (BitWidth && XKnownNegative && YKnownNegative) {
1836 APInt KnownZero(BitWidth, 0);
1837 APInt KnownOne(BitWidth, 0);
1838 APInt Mask = APInt::getSignedMaxValue(BitWidth);
1839 // The sign bit of X is set. If some other bit is set then X is not equal
1840 // to INT_MIN.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001841 computeKnownBits(X, KnownZero, KnownOne, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001842 if ((KnownOne & Mask) != 0)
1843 return true;
1844 // The sign bit of Y is set. If some other bit is set then Y is not equal
1845 // to INT_MIN.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001846 computeKnownBits(Y, KnownZero, KnownOne, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001847 if ((KnownOne & Mask) != 0)
1848 return true;
1849 }
1850
1851 // The sum of a non-negative number and a power of two is not zero.
Hal Finkel60db0582014-09-07 18:57:58 +00001852 if (XKnownNonNegative &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001853 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q, DL))
Duncan Sandsd3951082011-01-25 09:38:29 +00001854 return true;
Hal Finkel60db0582014-09-07 18:57:58 +00001855 if (YKnownNonNegative &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001856 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q, DL))
Duncan Sandsd3951082011-01-25 09:38:29 +00001857 return true;
1858 }
Duncan Sands7cb61e52011-10-27 19:16:21 +00001859 // X * Y.
1860 else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) {
1861 OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
1862 // If X and Y are non-zero then so is X * Y as long as the multiplication
1863 // does not overflow.
1864 if ((BO->hasNoSignedWrap() || BO->hasNoUnsignedWrap()) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001865 isKnownNonZero(X, DL, Depth, Q) && isKnownNonZero(Y, DL, Depth, Q))
Duncan Sands7cb61e52011-10-27 19:16:21 +00001866 return true;
1867 }
Duncan Sandsd3951082011-01-25 09:38:29 +00001868 // (C ? X : Y) != 0 if X != 0 and Y != 0.
1869 else if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001870 if (isKnownNonZero(SI->getTrueValue(), DL, Depth, Q) &&
1871 isKnownNonZero(SI->getFalseValue(), DL, Depth, Q))
Duncan Sandsd3951082011-01-25 09:38:29 +00001872 return true;
1873 }
1874
1875 if (!BitWidth) return false;
1876 APInt KnownZero(BitWidth, 0);
1877 APInt KnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001878 computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q);
Duncan Sandsd3951082011-01-25 09:38:29 +00001879 return KnownOne != 0;
1880}
1881
Sanjay Patelaee84212014-11-04 16:27:42 +00001882/// Return true if 'V & Mask' is known to be zero. We use this predicate to
1883/// simplify operations downstream. Mask is known to be zero for bits that V
1884/// cannot have.
Chris Lattner4bc28252009-09-08 00:06:16 +00001885///
1886/// This function is defined on values with integer type, values with pointer
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001887/// type, and vectors of integers. In the case
Chris Lattner4bc28252009-09-08 00:06:16 +00001888/// where V is a vector, the mask, known zero, and known one values are the
1889/// same width as the vector element, and the bit is set only if it is true
1890/// for all of the elements in the vector.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001891bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
1892 unsigned Depth, const Query &Q) {
Chris Lattner965c7692008-06-02 01:18:21 +00001893 APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001894 computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00001895 return (KnownZero & Mask) == Mask;
1896}
1897
1898
1899
Sanjay Patelaee84212014-11-04 16:27:42 +00001900/// Return the number of times the sign bit of the register is replicated into
1901/// the other bits. We know that at least 1 bit is always equal to the sign bit
1902/// (itself), but other cases can give us information. For example, immediately
1903/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
1904/// other, so we return 3.
Chris Lattner965c7692008-06-02 01:18:21 +00001905///
1906/// 'Op' must have a scalar integer type.
1907///
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001908unsigned ComputeNumSignBits(Value *V, const DataLayout &DL, unsigned Depth,
1909 const Query &Q) {
1910 unsigned TyBits = DL.getTypeSizeInBits(V->getType()->getScalarType());
Chris Lattner965c7692008-06-02 01:18:21 +00001911 unsigned Tmp, Tmp2;
1912 unsigned FirstAnswer = 1;
1913
Jay Foada0653a32014-05-14 21:14:37 +00001914 // Note that ConstantInt is handled by the general computeKnownBits case
Chris Lattner2e01a692008-06-02 18:39:07 +00001915 // below.
1916
Chris Lattner965c7692008-06-02 01:18:21 +00001917 if (Depth == 6)
1918 return 1; // Limit search depth.
Craig Topper1bef2c82012-12-22 19:15:35 +00001919
Dan Gohman80ca01c2009-07-17 20:47:02 +00001920 Operator *U = dyn_cast<Operator>(V);
1921 switch (Operator::getOpcode(V)) {
Chris Lattner965c7692008-06-02 01:18:21 +00001922 default: break;
1923 case Instruction::SExt:
Mon P Wangbb3eac92009-12-02 04:59:58 +00001924 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001925 return ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q) + Tmp;
Craig Topper1bef2c82012-12-22 19:15:35 +00001926
Nadav Rotemc99a3872015-03-06 00:23:58 +00001927 case Instruction::SDiv: {
Nadav Rotem029c5c72015-03-03 21:39:02 +00001928 const APInt *Denominator;
1929 // sdiv X, C -> adds log(C) sign bits.
1930 if (match(U->getOperand(1), m_APInt(Denominator))) {
1931
1932 // Ignore non-positive denominator.
1933 if (!Denominator->isStrictlyPositive())
1934 break;
1935
1936 // Calculate the incoming numerator bits.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001937 unsigned NumBits = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
Nadav Rotem029c5c72015-03-03 21:39:02 +00001938
1939 // Add floor(log(C)) bits to the numerator bits.
1940 return std::min(TyBits, NumBits + Denominator->logBase2());
1941 }
1942 break;
Nadav Rotemc99a3872015-03-06 00:23:58 +00001943 }
1944
1945 case Instruction::SRem: {
1946 const APInt *Denominator;
Sanjoy Dase561fee2015-03-25 22:33:53 +00001947 // srem X, C -> we know that the result is within [-C+1,C) when C is a
1948 // positive constant. This let us put a lower bound on the number of sign
1949 // bits.
Nadav Rotemc99a3872015-03-06 00:23:58 +00001950 if (match(U->getOperand(1), m_APInt(Denominator))) {
1951
1952 // Ignore non-positive denominator.
1953 if (!Denominator->isStrictlyPositive())
1954 break;
1955
1956 // Calculate the incoming numerator bits. SRem by a positive constant
1957 // can't lower the number of sign bits.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001958 unsigned NumrBits =
1959 ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
Nadav Rotemc99a3872015-03-06 00:23:58 +00001960
1961 // Calculate the leading sign bit constraints by examining the
Sanjoy Dase561fee2015-03-25 22:33:53 +00001962 // denominator. Given that the denominator is positive, there are two
1963 // cases:
1964 //
1965 // 1. the numerator is positive. The result range is [0,C) and [0,C) u<
1966 // (1 << ceilLogBase2(C)).
1967 //
1968 // 2. the numerator is negative. Then the result range is (-C,0] and
1969 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
1970 //
1971 // Thus a lower bound on the number of sign bits is `TyBits -
1972 // ceilLogBase2(C)`.
Nadav Rotemc99a3872015-03-06 00:23:58 +00001973
Sanjoy Dase561fee2015-03-25 22:33:53 +00001974 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
Nadav Rotemc99a3872015-03-06 00:23:58 +00001975 return std::max(NumrBits, ResBits);
1976 }
1977 break;
1978 }
Nadav Rotem029c5c72015-03-03 21:39:02 +00001979
Chris Lattner61a1d6c2012-01-26 21:37:55 +00001980 case Instruction::AShr: {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001981 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
Chris Lattner61a1d6c2012-01-26 21:37:55 +00001982 // ashr X, C -> adds C sign bits. Vectors too.
1983 const APInt *ShAmt;
1984 if (match(U->getOperand(1), m_APInt(ShAmt))) {
1985 Tmp += ShAmt->getZExtValue();
Chris Lattner965c7692008-06-02 01:18:21 +00001986 if (Tmp > TyBits) Tmp = TyBits;
1987 }
1988 return Tmp;
Chris Lattner61a1d6c2012-01-26 21:37:55 +00001989 }
1990 case Instruction::Shl: {
1991 const APInt *ShAmt;
1992 if (match(U->getOperand(1), m_APInt(ShAmt))) {
Chris Lattner965c7692008-06-02 01:18:21 +00001993 // shl destroys sign bits.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001994 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
Chris Lattner61a1d6c2012-01-26 21:37:55 +00001995 Tmp2 = ShAmt->getZExtValue();
1996 if (Tmp2 >= TyBits || // Bad shift.
1997 Tmp2 >= Tmp) break; // Shifted all sign bits out.
1998 return Tmp - Tmp2;
Chris Lattner965c7692008-06-02 01:18:21 +00001999 }
2000 break;
Chris Lattner61a1d6c2012-01-26 21:37:55 +00002001 }
Chris Lattner965c7692008-06-02 01:18:21 +00002002 case Instruction::And:
2003 case Instruction::Or:
2004 case Instruction::Xor: // NOT is handled here.
2005 // Logical binary ops preserve the number of sign bits at the worst.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002006 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002007 if (Tmp != 1) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002008 Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002009 FirstAnswer = std::min(Tmp, Tmp2);
2010 // We computed what we know about the sign bits as our first
2011 // answer. Now proceed to the generic code that uses
Jay Foada0653a32014-05-14 21:14:37 +00002012 // computeKnownBits, and pick whichever answer is better.
Chris Lattner965c7692008-06-02 01:18:21 +00002013 }
2014 break;
2015
2016 case Instruction::Select:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002017 Tmp = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002018 if (Tmp == 1) return 1; // Early out.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002019 Tmp2 = ComputeNumSignBits(U->getOperand(2), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002020 return std::min(Tmp, Tmp2);
Craig Topper1bef2c82012-12-22 19:15:35 +00002021
Chris Lattner965c7692008-06-02 01:18:21 +00002022 case Instruction::Add:
2023 // Add can have at most one carry bit. Thus we know that the output
2024 // is, at worst, one more bit than the inputs.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002025 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002026 if (Tmp == 1) return 1; // Early out.
Craig Topper1bef2c82012-12-22 19:15:35 +00002027
Chris Lattner965c7692008-06-02 01:18:21 +00002028 // Special case decrementing a value (ADD X, -1):
David Majnemera55027f2014-12-26 09:20:17 +00002029 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
Chris Lattner965c7692008-06-02 01:18:21 +00002030 if (CRHS->isAllOnesValue()) {
2031 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002032 computeKnownBits(U->getOperand(0), KnownZero, KnownOne, DL, Depth + 1,
2033 Q);
Craig Topper1bef2c82012-12-22 19:15:35 +00002034
Chris Lattner965c7692008-06-02 01:18:21 +00002035 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2036 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002037 if ((KnownZero | APInt(TyBits, 1)).isAllOnesValue())
Chris Lattner965c7692008-06-02 01:18:21 +00002038 return TyBits;
Craig Topper1bef2c82012-12-22 19:15:35 +00002039
Chris Lattner965c7692008-06-02 01:18:21 +00002040 // If we are subtracting one from a positive number, there is no carry
2041 // out of the result.
2042 if (KnownZero.isNegative())
2043 return Tmp;
2044 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002045
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002046 Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002047 if (Tmp2 == 1) return 1;
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002048 return std::min(Tmp, Tmp2)-1;
Craig Topper1bef2c82012-12-22 19:15:35 +00002049
Chris Lattner965c7692008-06-02 01:18:21 +00002050 case Instruction::Sub:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002051 Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002052 if (Tmp2 == 1) return 1;
Craig Topper1bef2c82012-12-22 19:15:35 +00002053
Chris Lattner965c7692008-06-02 01:18:21 +00002054 // Handle NEG.
David Majnemera55027f2014-12-26 09:20:17 +00002055 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
Chris Lattner965c7692008-06-02 01:18:21 +00002056 if (CLHS->isNullValue()) {
2057 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002058 computeKnownBits(U->getOperand(1), KnownZero, KnownOne, DL, Depth + 1,
2059 Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002060 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2061 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002062 if ((KnownZero | APInt(TyBits, 1)).isAllOnesValue())
Chris Lattner965c7692008-06-02 01:18:21 +00002063 return TyBits;
Craig Topper1bef2c82012-12-22 19:15:35 +00002064
Chris Lattner965c7692008-06-02 01:18:21 +00002065 // If the input is known to be positive (the sign bit is known clear),
2066 // the output of the NEG has the same number of sign bits as the input.
2067 if (KnownZero.isNegative())
2068 return Tmp2;
Craig Topper1bef2c82012-12-22 19:15:35 +00002069
Chris Lattner965c7692008-06-02 01:18:21 +00002070 // Otherwise, we treat this like a SUB.
2071 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002072
Chris Lattner965c7692008-06-02 01:18:21 +00002073 // Sub can have at most one carry bit. Thus we know that the output
2074 // is, at worst, one more bit than the inputs.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002075 Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
Chris Lattner965c7692008-06-02 01:18:21 +00002076 if (Tmp == 1) return 1; // Early out.
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002077 return std::min(Tmp, Tmp2)-1;
Craig Topper1bef2c82012-12-22 19:15:35 +00002078
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002079 case Instruction::PHI: {
2080 PHINode *PN = cast<PHINode>(U);
David Majnemer6ee8d172015-01-04 07:06:53 +00002081 unsigned NumIncomingValues = PN->getNumIncomingValues();
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002082 // Don't analyze large in-degree PHIs.
David Majnemer6ee8d172015-01-04 07:06:53 +00002083 if (NumIncomingValues > 4) break;
2084 // Unreachable blocks may have zero-operand PHI nodes.
2085 if (NumIncomingValues == 0) break;
Craig Topper1bef2c82012-12-22 19:15:35 +00002086
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002087 // Take the minimum of all incoming values. This can't infinitely loop
2088 // because of our depth threshold.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002089 Tmp = ComputeNumSignBits(PN->getIncomingValue(0), DL, Depth + 1, Q);
David Majnemer6ee8d172015-01-04 07:06:53 +00002090 for (unsigned i = 1, e = NumIncomingValues; i != e; ++i) {
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002091 if (Tmp == 1) return Tmp;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002092 Tmp = std::min(
2093 Tmp, ComputeNumSignBits(PN->getIncomingValue(i), DL, Depth + 1, Q));
Chris Lattner35d3b9d2010-01-07 23:44:37 +00002094 }
2095 return Tmp;
2096 }
2097
Chris Lattner965c7692008-06-02 01:18:21 +00002098 case Instruction::Trunc:
2099 // FIXME: it's tricky to do anything useful for this, but it is an important
2100 // case for targets like X86.
2101 break;
2102 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002103
Chris Lattner965c7692008-06-02 01:18:21 +00002104 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2105 // use this information.
2106 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002107 APInt Mask;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002108 computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q);
Craig Topper1bef2c82012-12-22 19:15:35 +00002109
Chris Lattner965c7692008-06-02 01:18:21 +00002110 if (KnownZero.isNegative()) { // sign bit is 0
2111 Mask = KnownZero;
2112 } else if (KnownOne.isNegative()) { // sign bit is 1;
2113 Mask = KnownOne;
2114 } else {
2115 // Nothing known.
2116 return FirstAnswer;
2117 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002118
Chris Lattner965c7692008-06-02 01:18:21 +00002119 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2120 // the number of identical bits in the top of the input value.
2121 Mask = ~Mask;
2122 Mask <<= Mask.getBitWidth()-TyBits;
2123 // Return # leading zeros. We use 'min' here in case Val was zero before
2124 // shifting. We don't want to return '64' as for an i32 "0".
2125 return std::max(FirstAnswer, std::min(TyBits, Mask.countLeadingZeros()));
2126}
Chris Lattnera12a6de2008-06-02 01:29:46 +00002127
Sanjay Patelaee84212014-11-04 16:27:42 +00002128/// This function computes the integer multiple of Base that equals V.
2129/// If successful, it returns true and returns the multiple in
2130/// Multiple. If unsuccessful, it returns false. It looks
Victor Hernandez47444882009-11-10 08:28:35 +00002131/// through SExt instructions only if LookThroughSExt is true.
2132bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
Dan Gohman6a976bb2009-11-18 00:58:27 +00002133 bool LookThroughSExt, unsigned Depth) {
Victor Hernandez47444882009-11-10 08:28:35 +00002134 const unsigned MaxDepth = 6;
2135
Dan Gohman6a976bb2009-11-18 00:58:27 +00002136 assert(V && "No Value?");
Victor Hernandez47444882009-11-10 08:28:35 +00002137 assert(Depth <= MaxDepth && "Limit Search Depth");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002138 assert(V->getType()->isIntegerTy() && "Not integer or pointer type!");
Victor Hernandez47444882009-11-10 08:28:35 +00002139
Chris Lattner229907c2011-07-18 04:54:35 +00002140 Type *T = V->getType();
Victor Hernandez47444882009-11-10 08:28:35 +00002141
Dan Gohman6a976bb2009-11-18 00:58:27 +00002142 ConstantInt *CI = dyn_cast<ConstantInt>(V);
Victor Hernandez47444882009-11-10 08:28:35 +00002143
2144 if (Base == 0)
2145 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00002146
Victor Hernandez47444882009-11-10 08:28:35 +00002147 if (Base == 1) {
2148 Multiple = V;
2149 return true;
2150 }
2151
2152 ConstantExpr *CO = dyn_cast<ConstantExpr>(V);
2153 Constant *BaseVal = ConstantInt::get(T, Base);
2154 if (CO && CO == BaseVal) {
2155 // Multiple is 1.
2156 Multiple = ConstantInt::get(T, 1);
2157 return true;
2158 }
2159
2160 if (CI && CI->getZExtValue() % Base == 0) {
2161 Multiple = ConstantInt::get(T, CI->getZExtValue() / Base);
Craig Topper1bef2c82012-12-22 19:15:35 +00002162 return true;
Victor Hernandez47444882009-11-10 08:28:35 +00002163 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002164
Victor Hernandez47444882009-11-10 08:28:35 +00002165 if (Depth == MaxDepth) return false; // Limit search depth.
Craig Topper1bef2c82012-12-22 19:15:35 +00002166
Victor Hernandez47444882009-11-10 08:28:35 +00002167 Operator *I = dyn_cast<Operator>(V);
2168 if (!I) return false;
2169
2170 switch (I->getOpcode()) {
2171 default: break;
Chris Lattner4f0b47d2009-11-26 01:50:12 +00002172 case Instruction::SExt:
Victor Hernandez47444882009-11-10 08:28:35 +00002173 if (!LookThroughSExt) return false;
2174 // otherwise fall through to ZExt
Chris Lattner4f0b47d2009-11-26 01:50:12 +00002175 case Instruction::ZExt:
Dan Gohman6a976bb2009-11-18 00:58:27 +00002176 return ComputeMultiple(I->getOperand(0), Base, Multiple,
2177 LookThroughSExt, Depth+1);
Victor Hernandez47444882009-11-10 08:28:35 +00002178 case Instruction::Shl:
2179 case Instruction::Mul: {
2180 Value *Op0 = I->getOperand(0);
2181 Value *Op1 = I->getOperand(1);
2182
2183 if (I->getOpcode() == Instruction::Shl) {
2184 ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1);
2185 if (!Op1CI) return false;
2186 // Turn Op0 << Op1 into Op0 * 2^Op1
2187 APInt Op1Int = Op1CI->getValue();
2188 uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
Jay Foad15084f02010-11-30 09:02:01 +00002189 APInt API(Op1Int.getBitWidth(), 0);
Jay Foad25a5e4c2010-12-01 08:53:58 +00002190 API.setBit(BitToSet);
Jay Foad15084f02010-11-30 09:02:01 +00002191 Op1 = ConstantInt::get(V->getContext(), API);
Victor Hernandez47444882009-11-10 08:28:35 +00002192 }
2193
Craig Topper9f008862014-04-15 04:59:12 +00002194 Value *Mul0 = nullptr;
Chris Lattner72d283c2010-09-05 17:20:46 +00002195 if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) {
2196 if (Constant *Op1C = dyn_cast<Constant>(Op1))
2197 if (Constant *MulC = dyn_cast<Constant>(Mul0)) {
Craig Topper1bef2c82012-12-22 19:15:35 +00002198 if (Op1C->getType()->getPrimitiveSizeInBits() <
Chris Lattner72d283c2010-09-05 17:20:46 +00002199 MulC->getType()->getPrimitiveSizeInBits())
2200 Op1C = ConstantExpr::getZExt(Op1C, MulC->getType());
Craig Topper1bef2c82012-12-22 19:15:35 +00002201 if (Op1C->getType()->getPrimitiveSizeInBits() >
Chris Lattner72d283c2010-09-05 17:20:46 +00002202 MulC->getType()->getPrimitiveSizeInBits())
2203 MulC = ConstantExpr::getZExt(MulC, Op1C->getType());
Craig Topper1bef2c82012-12-22 19:15:35 +00002204
Chris Lattner72d283c2010-09-05 17:20:46 +00002205 // V == Base * (Mul0 * Op1), so return (Mul0 * Op1)
2206 Multiple = ConstantExpr::getMul(MulC, Op1C);
2207 return true;
2208 }
Victor Hernandez47444882009-11-10 08:28:35 +00002209
2210 if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0))
2211 if (Mul0CI->getValue() == 1) {
2212 // V == Base * Op1, so return Op1
2213 Multiple = Op1;
2214 return true;
2215 }
2216 }
2217
Craig Topper9f008862014-04-15 04:59:12 +00002218 Value *Mul1 = nullptr;
Chris Lattner72d283c2010-09-05 17:20:46 +00002219 if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) {
2220 if (Constant *Op0C = dyn_cast<Constant>(Op0))
2221 if (Constant *MulC = dyn_cast<Constant>(Mul1)) {
Craig Topper1bef2c82012-12-22 19:15:35 +00002222 if (Op0C->getType()->getPrimitiveSizeInBits() <
Chris Lattner72d283c2010-09-05 17:20:46 +00002223 MulC->getType()->getPrimitiveSizeInBits())
2224 Op0C = ConstantExpr::getZExt(Op0C, MulC->getType());
Craig Topper1bef2c82012-12-22 19:15:35 +00002225 if (Op0C->getType()->getPrimitiveSizeInBits() >
Chris Lattner72d283c2010-09-05 17:20:46 +00002226 MulC->getType()->getPrimitiveSizeInBits())
2227 MulC = ConstantExpr::getZExt(MulC, Op0C->getType());
Craig Topper1bef2c82012-12-22 19:15:35 +00002228
Chris Lattner72d283c2010-09-05 17:20:46 +00002229 // V == Base * (Mul1 * Op0), so return (Mul1 * Op0)
2230 Multiple = ConstantExpr::getMul(MulC, Op0C);
2231 return true;
2232 }
Victor Hernandez47444882009-11-10 08:28:35 +00002233
2234 if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1))
2235 if (Mul1CI->getValue() == 1) {
2236 // V == Base * Op0, so return Op0
2237 Multiple = Op0;
2238 return true;
2239 }
2240 }
Victor Hernandez47444882009-11-10 08:28:35 +00002241 }
2242 }
2243
2244 // We could not determine if V is a multiple of Base.
2245 return false;
2246}
2247
Sanjay Patelaee84212014-11-04 16:27:42 +00002248/// Return true if we can prove that the specified FP value is never equal to
2249/// -0.0.
Chris Lattnera12a6de2008-06-02 01:29:46 +00002250///
2251/// NOTE: this function will need to be revisited when we support non-default
2252/// rounding modes!
2253///
2254bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) {
2255 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
2256 return !CFP->getValueAPF().isNegZero();
Craig Topper1bef2c82012-12-22 19:15:35 +00002257
Sanjay Patel40eaa8d2015-02-25 18:00:15 +00002258 // FIXME: Magic number! At the least, this should be given a name because it's
2259 // used similarly in CannotBeOrderedLessThanZero(). A better fix may be to
2260 // expose it as a parameter, so it can be used for testing / experimenting.
Chris Lattnera12a6de2008-06-02 01:29:46 +00002261 if (Depth == 6)
Sanjay Patel40eaa8d2015-02-25 18:00:15 +00002262 return false; // Limit search depth.
Chris Lattnera12a6de2008-06-02 01:29:46 +00002263
Dan Gohman80ca01c2009-07-17 20:47:02 +00002264 const Operator *I = dyn_cast<Operator>(V);
Craig Topper9f008862014-04-15 04:59:12 +00002265 if (!I) return false;
Michael Ilseman0f128372012-12-06 00:07:09 +00002266
2267 // Check if the nsz fast-math flag is set
2268 if (const FPMathOperator *FPO = dyn_cast<FPMathOperator>(I))
2269 if (FPO->hasNoSignedZeros())
2270 return true;
2271
Chris Lattnera12a6de2008-06-02 01:29:46 +00002272 // (add x, 0.0) is guaranteed to return +0.0, not -0.0.
Jakub Staszakb7129f22013-03-06 00:16:16 +00002273 if (I->getOpcode() == Instruction::FAdd)
2274 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I->getOperand(1)))
2275 if (CFP->isNullValue())
2276 return true;
Craig Topper1bef2c82012-12-22 19:15:35 +00002277
Chris Lattnera12a6de2008-06-02 01:29:46 +00002278 // sitofp and uitofp turn into +0.0 for zero.
2279 if (isa<SIToFPInst>(I) || isa<UIToFPInst>(I))
2280 return true;
Craig Topper1bef2c82012-12-22 19:15:35 +00002281
Chris Lattnera12a6de2008-06-02 01:29:46 +00002282 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2283 // sqrt(-0.0) = -0.0, no other negative results are possible.
2284 if (II->getIntrinsicID() == Intrinsic::sqrt)
Gabor Greif1abbde32010-06-23 23:38:07 +00002285 return CannotBeNegativeZero(II->getArgOperand(0), Depth+1);
Craig Topper1bef2c82012-12-22 19:15:35 +00002286
Chris Lattnera12a6de2008-06-02 01:29:46 +00002287 if (const CallInst *CI = dyn_cast<CallInst>(I))
2288 if (const Function *F = CI->getCalledFunction()) {
2289 if (F->isDeclaration()) {
Daniel Dunbarca414c72009-07-26 08:34:35 +00002290 // abs(x) != -0.0
2291 if (F->getName() == "abs") return true;
Dale Johannesenf6a987b2009-09-25 20:54:50 +00002292 // fabs[lf](x) != -0.0
2293 if (F->getName() == "fabs") return true;
2294 if (F->getName() == "fabsf") return true;
2295 if (F->getName() == "fabsl") return true;
2296 if (F->getName() == "sqrt" || F->getName() == "sqrtf" ||
2297 F->getName() == "sqrtl")
Gabor Greif1abbde32010-06-23 23:38:07 +00002298 return CannotBeNegativeZero(CI->getArgOperand(0), Depth+1);
Chris Lattnera12a6de2008-06-02 01:29:46 +00002299 }
2300 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002301
Chris Lattnera12a6de2008-06-02 01:29:46 +00002302 return false;
2303}
2304
Elena Demikhovsky45f04482015-01-28 08:03:58 +00002305bool llvm::CannotBeOrderedLessThanZero(const Value *V, unsigned Depth) {
2306 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
2307 return !CFP->getValueAPF().isNegative() || CFP->getValueAPF().isZero();
2308
Sanjay Patel40eaa8d2015-02-25 18:00:15 +00002309 // FIXME: Magic number! At the least, this should be given a name because it's
2310 // used similarly in CannotBeNegativeZero(). A better fix may be to
2311 // expose it as a parameter, so it can be used for testing / experimenting.
Elena Demikhovsky45f04482015-01-28 08:03:58 +00002312 if (Depth == 6)
2313 return false; // Limit search depth.
2314
2315 const Operator *I = dyn_cast<Operator>(V);
2316 if (!I) return false;
2317
2318 switch (I->getOpcode()) {
2319 default: break;
2320 case Instruction::FMul:
2321 // x*x is always non-negative or a NaN.
2322 if (I->getOperand(0) == I->getOperand(1))
2323 return true;
2324 // Fall through
2325 case Instruction::FAdd:
2326 case Instruction::FDiv:
2327 case Instruction::FRem:
2328 return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1) &&
2329 CannotBeOrderedLessThanZero(I->getOperand(1), Depth+1);
2330 case Instruction::FPExt:
2331 case Instruction::FPTrunc:
2332 // Widening/narrowing never change sign.
2333 return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1);
2334 case Instruction::Call:
2335 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2336 switch (II->getIntrinsicID()) {
2337 default: break;
2338 case Intrinsic::exp:
2339 case Intrinsic::exp2:
2340 case Intrinsic::fabs:
2341 case Intrinsic::sqrt:
2342 return true;
2343 case Intrinsic::powi:
2344 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
2345 // powi(x,n) is non-negative if n is even.
2346 if (CI->getBitWidth() <= 64 && CI->getSExtValue() % 2u == 0)
2347 return true;
2348 }
2349 return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1);
2350 case Intrinsic::fma:
2351 case Intrinsic::fmuladd:
2352 // x*x+y is non-negative if y is non-negative.
2353 return I->getOperand(0) == I->getOperand(1) &&
2354 CannotBeOrderedLessThanZero(I->getOperand(2), Depth+1);
2355 }
2356 break;
2357 }
2358 return false;
2359}
2360
Sanjay Patelaee84212014-11-04 16:27:42 +00002361/// If the specified value can be set by repeating the same byte in memory,
2362/// return the i8 value that it is represented with. This is
Chris Lattner9cb10352010-12-26 20:15:01 +00002363/// true for all i8 values obviously, but is also true for i32 0, i32 -1,
2364/// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated
2365/// byte store (e.g. i16 0x1234), return null.
2366Value *llvm::isBytewiseValue(Value *V) {
2367 // All byte-wide stores are splatable, even of arbitrary variables.
2368 if (V->getType()->isIntegerTy(8)) return V;
Chris Lattneracf6b072011-02-19 19:35:49 +00002369
2370 // Handle 'null' ConstantArrayZero etc.
2371 if (Constant *C = dyn_cast<Constant>(V))
2372 if (C->isNullValue())
2373 return Constant::getNullValue(Type::getInt8Ty(V->getContext()));
Craig Topper1bef2c82012-12-22 19:15:35 +00002374
Chris Lattner9cb10352010-12-26 20:15:01 +00002375 // Constant float and double values can be handled as integer values if the
Craig Topper1bef2c82012-12-22 19:15:35 +00002376 // corresponding integer value is "byteable". An important case is 0.0.
Chris Lattner9cb10352010-12-26 20:15:01 +00002377 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
2378 if (CFP->getType()->isFloatTy())
2379 V = ConstantExpr::getBitCast(CFP, Type::getInt32Ty(V->getContext()));
2380 if (CFP->getType()->isDoubleTy())
2381 V = ConstantExpr::getBitCast(CFP, Type::getInt64Ty(V->getContext()));
2382 // Don't handle long double formats, which have strange constraints.
2383 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002384
Benjamin Kramer17d90152015-02-07 19:29:02 +00002385 // We can handle constant integers that are multiple of 8 bits.
Chris Lattner9cb10352010-12-26 20:15:01 +00002386 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Benjamin Kramer17d90152015-02-07 19:29:02 +00002387 if (CI->getBitWidth() % 8 == 0) {
2388 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
Craig Topper1bef2c82012-12-22 19:15:35 +00002389
Benjamin Kramerb4b51502015-03-25 16:49:59 +00002390 if (!CI->getValue().isSplat(8))
Benjamin Kramer17d90152015-02-07 19:29:02 +00002391 return nullptr;
2392 return ConstantInt::get(V->getContext(), CI->getValue().trunc(8));
Chris Lattner9cb10352010-12-26 20:15:01 +00002393 }
2394 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002395
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002396 // A ConstantDataArray/Vector is splatable if all its members are equal and
2397 // also splatable.
2398 if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(V)) {
2399 Value *Elt = CA->getElementAsConstant(0);
2400 Value *Val = isBytewiseValue(Elt);
Chris Lattner9cb10352010-12-26 20:15:01 +00002401 if (!Val)
Craig Topper9f008862014-04-15 04:59:12 +00002402 return nullptr;
Craig Topper1bef2c82012-12-22 19:15:35 +00002403
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002404 for (unsigned I = 1, E = CA->getNumElements(); I != E; ++I)
2405 if (CA->getElementAsConstant(I) != Elt)
Craig Topper9f008862014-04-15 04:59:12 +00002406 return nullptr;
Craig Topper1bef2c82012-12-22 19:15:35 +00002407
Chris Lattner9cb10352010-12-26 20:15:01 +00002408 return Val;
2409 }
Chad Rosier8abf65a2011-12-06 00:19:08 +00002410
Chris Lattner9cb10352010-12-26 20:15:01 +00002411 // Conceptually, we could handle things like:
2412 // %a = zext i8 %X to i16
2413 // %b = shl i16 %a, 8
2414 // %c = or i16 %a, %b
2415 // but until there is an example that actually needs this, it doesn't seem
2416 // worth worrying about.
Craig Topper9f008862014-04-15 04:59:12 +00002417 return nullptr;
Chris Lattner9cb10352010-12-26 20:15:01 +00002418}
2419
2420
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002421// This is the recursive version of BuildSubAggregate. It takes a few different
2422// arguments. Idxs is the index within the nested struct From that we are
2423// looking at now (which is of type IndexedType). IdxSkip is the number of
2424// indices from Idxs that should be left out when inserting into the resulting
2425// struct. To is the result struct built so far, new insertvalue instructions
2426// build on that.
Chris Lattner229907c2011-07-18 04:54:35 +00002427static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType,
Craig Topper2cd5ff82013-07-11 16:22:38 +00002428 SmallVectorImpl<unsigned> &Idxs,
Dan Gohmana6d0afc2009-08-07 01:32:21 +00002429 unsigned IdxSkip,
Dan Gohmana6d0afc2009-08-07 01:32:21 +00002430 Instruction *InsertBefore) {
Dmitri Gribenko226fea52013-01-13 16:01:15 +00002431 llvm::StructType *STy = dyn_cast<llvm::StructType>(IndexedType);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002432 if (STy) {
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002433 // Save the original To argument so we can modify it
2434 Value *OrigTo = To;
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002435 // General case, the type indexed by Idxs is a struct
2436 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
2437 // Process each struct element recursively
2438 Idxs.push_back(i);
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002439 Value *PrevTo = To;
Matthijs Kooijman5cb38772008-06-16 12:57:37 +00002440 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
Nick Lewycky39dbfd32009-11-23 03:29:18 +00002441 InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002442 Idxs.pop_back();
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002443 if (!To) {
2444 // Couldn't find any inserted value for this index? Cleanup
2445 while (PrevTo != OrigTo) {
2446 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
2447 PrevTo = Del->getAggregateOperand();
2448 Del->eraseFromParent();
2449 }
2450 // Stop processing elements
2451 break;
2452 }
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002453 }
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00002454 // If we successfully found a value for each of our subaggregates
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002455 if (To)
2456 return To;
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002457 }
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002458 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
2459 // the struct's elements had a value that was inserted directly. In the latter
2460 // case, perhaps we can't determine each of the subelements individually, but
2461 // we might be able to find the complete struct somewhere.
Craig Topper1bef2c82012-12-22 19:15:35 +00002462
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002463 // Find the value that is at that particular spot
Jay Foad57aa6362011-07-13 10:26:04 +00002464 Value *V = FindInsertedValue(From, Idxs);
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002465
2466 if (!V)
Craig Topper9f008862014-04-15 04:59:12 +00002467 return nullptr;
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002468
2469 // Insert the value in the new (sub) aggregrate
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002470 return llvm::InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip),
Jay Foad57aa6362011-07-13 10:26:04 +00002471 "tmp", InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002472}
2473
2474// This helper takes a nested struct and extracts a part of it (which is again a
2475// struct) into a new value. For example, given the struct:
2476// { a, { b, { c, d }, e } }
2477// and the indices "1, 1" this returns
2478// { c, d }.
2479//
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002480// It does this by inserting an insertvalue for each element in the resulting
2481// struct, as opposed to just inserting a single struct. This will only work if
2482// each of the elements of the substruct are known (ie, inserted into From by an
2483// insertvalue instruction somewhere).
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002484//
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002485// All inserted insertvalue instructions are inserted before InsertBefore
Jay Foad57aa6362011-07-13 10:26:04 +00002486static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range,
Dan Gohmana6d0afc2009-08-07 01:32:21 +00002487 Instruction *InsertBefore) {
Matthijs Kooijman69801d42008-06-16 13:28:31 +00002488 assert(InsertBefore && "Must have someplace to insert!");
Chris Lattner229907c2011-07-18 04:54:35 +00002489 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
Jay Foad57aa6362011-07-13 10:26:04 +00002490 idx_range);
Owen Andersonb292b8c2009-07-30 23:03:37 +00002491 Value *To = UndefValue::get(IndexedType);
Jay Foad57aa6362011-07-13 10:26:04 +00002492 SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end());
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002493 unsigned IdxSkip = Idxs.size();
2494
Nick Lewycky39dbfd32009-11-23 03:29:18 +00002495 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002496}
2497
Sanjay Patelaee84212014-11-04 16:27:42 +00002498/// Given an aggregrate and an sequence of indices, see if
Matthijs Kooijman5cb38772008-06-16 12:57:37 +00002499/// the scalar value indexed is already around as a register, for example if it
2500/// were inserted directly into the aggregrate.
Matthijs Kooijmanfa4d0b82008-06-16 14:13:46 +00002501///
2502/// If InsertBefore is not null, this function will duplicate (modified)
2503/// insertvalues when a part of a nested struct is extracted.
Jay Foad57aa6362011-07-13 10:26:04 +00002504Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range,
2505 Instruction *InsertBefore) {
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002506 // Nothing to index? Just return V then (this is useful at the end of our
Chris Lattnerf7eb5432012-01-24 07:54:10 +00002507 // recursion).
Jay Foad57aa6362011-07-13 10:26:04 +00002508 if (idx_range.empty())
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002509 return V;
Chris Lattnerf7eb5432012-01-24 07:54:10 +00002510 // We have indices, so V should have an indexable type.
2511 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
2512 "Not looking at a struct or array?");
2513 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
2514 "Invalid indices for type?");
Owen Andersonf1f17432009-07-06 22:37:39 +00002515
Chris Lattner67058832012-01-25 06:48:06 +00002516 if (Constant *C = dyn_cast<Constant>(V)) {
2517 C = C->getAggregateElement(idx_range[0]);
Craig Topper9f008862014-04-15 04:59:12 +00002518 if (!C) return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +00002519 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
2520 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002521
Chris Lattnerf7eb5432012-01-24 07:54:10 +00002522 if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002523 // Loop the indices for the insertvalue instruction in parallel with the
2524 // requested indices
Jay Foad57aa6362011-07-13 10:26:04 +00002525 const unsigned *req_idx = idx_range.begin();
Matthijs Kooijman5cb38772008-06-16 12:57:37 +00002526 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
2527 i != e; ++i, ++req_idx) {
Jay Foad57aa6362011-07-13 10:26:04 +00002528 if (req_idx == idx_range.end()) {
Chris Lattnerf7eb5432012-01-24 07:54:10 +00002529 // We can't handle this without inserting insertvalues
2530 if (!InsertBefore)
Craig Topper9f008862014-04-15 04:59:12 +00002531 return nullptr;
Chris Lattnerf7eb5432012-01-24 07:54:10 +00002532
2533 // The requested index identifies a part of a nested aggregate. Handle
2534 // this specially. For example,
2535 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
2536 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
2537 // %C = extractvalue {i32, { i32, i32 } } %B, 1
2538 // This can be changed into
2539 // %A = insertvalue {i32, i32 } undef, i32 10, 0
2540 // %C = insertvalue {i32, i32 } %A, i32 11, 1
2541 // which allows the unused 0,0 element from the nested struct to be
2542 // removed.
2543 return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx),
2544 InsertBefore);
Duncan Sandsdb356ee2008-06-19 08:47:31 +00002545 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002546
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002547 // This insert value inserts something else than what we are looking for.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002548 // See if the (aggregate) value inserted into has the value we are
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002549 // looking for, then.
2550 if (*req_idx != *i)
Jay Foad57aa6362011-07-13 10:26:04 +00002551 return FindInsertedValue(I->getAggregateOperand(), idx_range,
Nick Lewycky39dbfd32009-11-23 03:29:18 +00002552 InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002553 }
2554 // If we end up here, the indices of the insertvalue match with those
2555 // requested (though possibly only partially). Now we recursively look at
2556 // the inserted value, passing any remaining indices.
Jay Foad57aa6362011-07-13 10:26:04 +00002557 return FindInsertedValue(I->getInsertedValueOperand(),
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002558 makeArrayRef(req_idx, idx_range.end()),
Nick Lewycky39dbfd32009-11-23 03:29:18 +00002559 InsertBefore);
Chris Lattnerf7eb5432012-01-24 07:54:10 +00002560 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002561
Chris Lattnerf7eb5432012-01-24 07:54:10 +00002562 if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002563 // If we're extracting a value from an aggregate that was extracted from
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002564 // something else, we can extract from that something else directly instead.
2565 // However, we will need to chain I's indices with the requested indices.
Craig Topper1bef2c82012-12-22 19:15:35 +00002566
2567 // Calculate the number of indices required
Jay Foad57aa6362011-07-13 10:26:04 +00002568 unsigned size = I->getNumIndices() + idx_range.size();
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002569 // Allocate some space to put the new indices in
Matthijs Kooijman8369c672008-06-17 08:24:37 +00002570 SmallVector<unsigned, 5> Idxs;
2571 Idxs.reserve(size);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002572 // Add indices from the extract value instruction
Jay Foad57aa6362011-07-13 10:26:04 +00002573 Idxs.append(I->idx_begin(), I->idx_end());
Craig Topper1bef2c82012-12-22 19:15:35 +00002574
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002575 // Add requested indices
Jay Foad57aa6362011-07-13 10:26:04 +00002576 Idxs.append(idx_range.begin(), idx_range.end());
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002577
Craig Topper1bef2c82012-12-22 19:15:35 +00002578 assert(Idxs.size() == size
Matthijs Kooijman5cb38772008-06-16 12:57:37 +00002579 && "Number of indices added not correct?");
Craig Topper1bef2c82012-12-22 19:15:35 +00002580
Jay Foad57aa6362011-07-13 10:26:04 +00002581 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002582 }
2583 // Otherwise, we don't know (such as, extracting from a function return value
2584 // or load instruction)
Craig Topper9f008862014-04-15 04:59:12 +00002585 return nullptr;
Matthijs Kooijmane92e18b2008-06-16 12:48:21 +00002586}
Evan Chengda3db112008-06-30 07:31:25 +00002587
Sanjay Patelaee84212014-11-04 16:27:42 +00002588/// Analyze the specified pointer to see if it can be expressed as a base
2589/// pointer plus a constant offset. Return the base and offset to the caller.
Chris Lattnere28618d2010-11-30 22:25:26 +00002590Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002591 const DataLayout &DL) {
2592 unsigned BitWidth = DL.getPointerTypeSizeInBits(Ptr->getType());
Nuno Lopes368c4d02012-12-31 20:48:35 +00002593 APInt ByteOffset(BitWidth, 0);
2594 while (1) {
2595 if (Ptr->getType()->isVectorTy())
2596 break;
Craig Topper1bef2c82012-12-22 19:15:35 +00002597
Nuno Lopes368c4d02012-12-31 20:48:35 +00002598 if (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002599 APInt GEPOffset(BitWidth, 0);
2600 if (!GEP->accumulateConstantOffset(DL, GEPOffset))
2601 break;
Matt Arsenaultf55e5e72013-08-10 17:34:08 +00002602
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002603 ByteOffset += GEPOffset;
Matt Arsenaultf55e5e72013-08-10 17:34:08 +00002604
Nuno Lopes368c4d02012-12-31 20:48:35 +00002605 Ptr = GEP->getPointerOperand();
Matt Arsenaultfd78d0c2014-07-14 22:39:22 +00002606 } else if (Operator::getOpcode(Ptr) == Instruction::BitCast ||
2607 Operator::getOpcode(Ptr) == Instruction::AddrSpaceCast) {
Nuno Lopes368c4d02012-12-31 20:48:35 +00002608 Ptr = cast<Operator>(Ptr)->getOperand(0);
2609 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) {
2610 if (GA->mayBeOverridden())
2611 break;
2612 Ptr = GA->getAliasee();
Chris Lattnere28618d2010-11-30 22:25:26 +00002613 } else {
Nuno Lopes368c4d02012-12-31 20:48:35 +00002614 break;
Chris Lattnere28618d2010-11-30 22:25:26 +00002615 }
2616 }
Nuno Lopes368c4d02012-12-31 20:48:35 +00002617 Offset = ByteOffset.getSExtValue();
2618 return Ptr;
Chris Lattnere28618d2010-11-30 22:25:26 +00002619}
2620
2621
Sanjay Patelaee84212014-11-04 16:27:42 +00002622/// This function computes the length of a null-terminated C string pointed to
2623/// by V. If successful, it returns true and returns the string in Str.
2624/// If unsuccessful, it returns false.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002625bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
2626 uint64_t Offset, bool TrimAtNul) {
2627 assert(V);
Evan Chengda3db112008-06-30 07:31:25 +00002628
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002629 // Look through bitcast instructions and geps.
2630 V = V->stripPointerCasts();
Craig Topper1bef2c82012-12-22 19:15:35 +00002631
Benjamin Kramer0248a3e2015-03-21 15:36:06 +00002632 // If the value is a GEP instruction or constant expression, treat it as an
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002633 // offset.
2634 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Evan Chengda3db112008-06-30 07:31:25 +00002635 // Make sure the GEP has exactly three arguments.
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002636 if (GEP->getNumOperands() != 3)
2637 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00002638
Evan Chengda3db112008-06-30 07:31:25 +00002639 // Make sure the index-ee is a pointer to array of i8.
Chris Lattner229907c2011-07-18 04:54:35 +00002640 PointerType *PT = cast<PointerType>(GEP->getOperand(0)->getType());
2641 ArrayType *AT = dyn_cast<ArrayType>(PT->getElementType());
Craig Topper9f008862014-04-15 04:59:12 +00002642 if (!AT || !AT->getElementType()->isIntegerTy(8))
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002643 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00002644
Evan Chengda3db112008-06-30 07:31:25 +00002645 // Check to make sure that the first operand of the GEP is an integer and
2646 // has value 0 so that we are sure we're indexing into the initializer.
Dan Gohman0b4df042010-04-14 22:20:45 +00002647 const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
Craig Topper9f008862014-04-15 04:59:12 +00002648 if (!FirstIdx || !FirstIdx->isZero())
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002649 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00002650
Evan Chengda3db112008-06-30 07:31:25 +00002651 // If the second index isn't a ConstantInt, then this is a variable index
2652 // into the array. If this occurs, we can't say anything meaningful about
2653 // the string.
2654 uint64_t StartIdx = 0;
Dan Gohman0b4df042010-04-14 22:20:45 +00002655 if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
Evan Chengda3db112008-06-30 07:31:25 +00002656 StartIdx = CI->getZExtValue();
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002657 else
2658 return false;
Benjamin Kramer0248a3e2015-03-21 15:36:06 +00002659 return getConstantStringInfo(GEP->getOperand(0), Str, StartIdx + Offset,
2660 TrimAtNul);
Evan Chengda3db112008-06-30 07:31:25 +00002661 }
Nick Lewycky46209882011-10-20 00:34:35 +00002662
Evan Chengda3db112008-06-30 07:31:25 +00002663 // The GEP instruction, constant or instruction, must reference a global
2664 // variable that is a constant and is initialized. The referenced constant
2665 // initializer is the array that we'll use for optimization.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002666 const GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
Dan Gohman5d5bc6d2009-08-19 18:20:44 +00002667 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002668 return false;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002669
Nick Lewycky46209882011-10-20 00:34:35 +00002670 // Handle the all-zeros case
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002671 if (GV->getInitializer()->isNullValue()) {
Evan Chengda3db112008-06-30 07:31:25 +00002672 // This is a degenerate case. The initializer is constant zero so the
2673 // length of the string must be zero.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002674 Str = "";
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002675 return true;
2676 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002677
Evan Chengda3db112008-06-30 07:31:25 +00002678 // Must be a Constant Array
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002679 const ConstantDataArray *Array =
2680 dyn_cast<ConstantDataArray>(GV->getInitializer());
Craig Topper9f008862014-04-15 04:59:12 +00002681 if (!Array || !Array->isString())
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002682 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00002683
Evan Chengda3db112008-06-30 07:31:25 +00002684 // Get the number of elements in the array
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002685 uint64_t NumElts = Array->getType()->getArrayNumElements();
2686
2687 // Start out with the entire array in the StringRef.
2688 Str = Array->getAsString();
2689
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002690 if (Offset > NumElts)
2691 return false;
Craig Topper1bef2c82012-12-22 19:15:35 +00002692
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002693 // Skip over 'offset' bytes.
2694 Str = Str.substr(Offset);
Craig Topper1bef2c82012-12-22 19:15:35 +00002695
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002696 if (TrimAtNul) {
2697 // Trim off the \0 and anything after it. If the array is not nul
2698 // terminated, we just return the whole end of string. The client may know
2699 // some other way that the string is length-bound.
2700 Str = Str.substr(0, Str.find('\0'));
2701 }
Bill Wendlingfa54bc22009-03-13 04:39:26 +00002702 return true;
Evan Chengda3db112008-06-30 07:31:25 +00002703}
Eric Christopher4899cbc2010-03-05 06:58:57 +00002704
2705// These next two are very similar to the above, but also look through PHI
2706// nodes.
2707// TODO: See if we can integrate these two together.
2708
Sanjay Patelaee84212014-11-04 16:27:42 +00002709/// If we can compute the length of the string pointed to by
Eric Christopher4899cbc2010-03-05 06:58:57 +00002710/// the specified pointer, return 'len+1'. If we can't, return 0.
Craig Topper71b7b682014-08-21 05:55:13 +00002711static uint64_t GetStringLengthH(Value *V, SmallPtrSetImpl<PHINode*> &PHIs) {
Eric Christopher4899cbc2010-03-05 06:58:57 +00002712 // Look through noop bitcast instructions.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002713 V = V->stripPointerCasts();
Eric Christopher4899cbc2010-03-05 06:58:57 +00002714
2715 // If this is a PHI node, there are two cases: either we have already seen it
2716 // or we haven't.
2717 if (PHINode *PN = dyn_cast<PHINode>(V)) {
David Blaikie70573dc2014-11-19 07:49:26 +00002718 if (!PHIs.insert(PN).second)
Eric Christopher4899cbc2010-03-05 06:58:57 +00002719 return ~0ULL; // already in the set.
2720
2721 // If it was new, see if all the input strings are the same length.
2722 uint64_t LenSoFar = ~0ULL;
Pete Cooper833f34d2015-05-12 20:05:31 +00002723 for (Value *IncValue : PN->incoming_values()) {
2724 uint64_t Len = GetStringLengthH(IncValue, PHIs);
Eric Christopher4899cbc2010-03-05 06:58:57 +00002725 if (Len == 0) return 0; // Unknown length -> unknown.
2726
2727 if (Len == ~0ULL) continue;
2728
2729 if (Len != LenSoFar && LenSoFar != ~0ULL)
2730 return 0; // Disagree -> unknown.
2731 LenSoFar = Len;
2732 }
2733
2734 // Success, all agree.
2735 return LenSoFar;
2736 }
2737
2738 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
2739 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
2740 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
2741 if (Len1 == 0) return 0;
2742 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
2743 if (Len2 == 0) return 0;
2744 if (Len1 == ~0ULL) return Len2;
2745 if (Len2 == ~0ULL) return Len1;
2746 if (Len1 != Len2) return 0;
2747 return Len1;
2748 }
Craig Topper1bef2c82012-12-22 19:15:35 +00002749
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002750 // Otherwise, see if we can read the string.
2751 StringRef StrData;
2752 if (!getConstantStringInfo(V, StrData))
Eric Christopher4899cbc2010-03-05 06:58:57 +00002753 return 0;
2754
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002755 return StrData.size()+1;
Eric Christopher4899cbc2010-03-05 06:58:57 +00002756}
2757
Sanjay Patelaee84212014-11-04 16:27:42 +00002758/// If we can compute the length of the string pointed to by
Eric Christopher4899cbc2010-03-05 06:58:57 +00002759/// the specified pointer, return 'len+1'. If we can't, return 0.
2760uint64_t llvm::GetStringLength(Value *V) {
2761 if (!V->getType()->isPointerTy()) return 0;
2762
2763 SmallPtrSet<PHINode*, 32> PHIs;
2764 uint64_t Len = GetStringLengthH(V, PHIs);
2765 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
2766 // an empty string as a length.
2767 return Len == ~0ULL ? 1 : Len;
2768}
Dan Gohmana4fcd242010-12-15 20:02:24 +00002769
Adam Nemete2b885c2015-04-23 20:09:20 +00002770/// \brief \p PN defines a loop-variant pointer to an object. Check if the
2771/// previous iteration of the loop was referring to the same object as \p PN.
2772static bool isSameUnderlyingObjectInLoop(PHINode *PN, LoopInfo *LI) {
2773 // Find the loop-defined value.
2774 Loop *L = LI->getLoopFor(PN->getParent());
2775 if (PN->getNumIncomingValues() != 2)
2776 return true;
2777
2778 // Find the value from previous iteration.
2779 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
2780 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
2781 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
2782 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
2783 return true;
2784
2785 // If a new pointer is loaded in the loop, the pointer references a different
2786 // object in every iteration. E.g.:
2787 // for (i)
2788 // int *p = a[i];
2789 // ...
2790 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
2791 if (!L->isLoopInvariant(Load->getPointerOperand()))
2792 return false;
2793 return true;
2794}
2795
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002796Value *llvm::GetUnderlyingObject(Value *V, const DataLayout &DL,
2797 unsigned MaxLookup) {
Dan Gohmana4fcd242010-12-15 20:02:24 +00002798 if (!V->getType()->isPointerTy())
2799 return V;
2800 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
2801 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
2802 V = GEP->getPointerOperand();
Matt Arsenault70f4db882014-07-15 00:56:40 +00002803 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
2804 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
Dan Gohmana4fcd242010-12-15 20:02:24 +00002805 V = cast<Operator>(V)->getOperand(0);
2806 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2807 if (GA->mayBeOverridden())
2808 return V;
2809 V = GA->getAliasee();
2810 } else {
Dan Gohman05b18f12010-12-15 20:49:55 +00002811 // See if InstructionSimplify knows any relevant tricks.
2812 if (Instruction *I = dyn_cast<Instruction>(V))
Chandler Carruth66b31302015-01-04 12:03:27 +00002813 // TODO: Acquire a DominatorTree and AssumptionCache and use them.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002814 if (Value *Simplified = SimplifyInstruction(I, DL, nullptr)) {
Dan Gohman05b18f12010-12-15 20:49:55 +00002815 V = Simplified;
2816 continue;
2817 }
2818
Dan Gohmana4fcd242010-12-15 20:02:24 +00002819 return V;
2820 }
2821 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
2822 }
2823 return V;
2824}
Nick Lewycky3e334a42011-06-27 04:20:45 +00002825
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002826void llvm::GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects,
Adam Nemete2b885c2015-04-23 20:09:20 +00002827 const DataLayout &DL, LoopInfo *LI,
2828 unsigned MaxLookup) {
Dan Gohmaned7c24e22012-05-10 18:57:38 +00002829 SmallPtrSet<Value *, 4> Visited;
2830 SmallVector<Value *, 4> Worklist;
2831 Worklist.push_back(V);
2832 do {
2833 Value *P = Worklist.pop_back_val();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002834 P = GetUnderlyingObject(P, DL, MaxLookup);
Dan Gohmaned7c24e22012-05-10 18:57:38 +00002835
David Blaikie70573dc2014-11-19 07:49:26 +00002836 if (!Visited.insert(P).second)
Dan Gohmaned7c24e22012-05-10 18:57:38 +00002837 continue;
2838
2839 if (SelectInst *SI = dyn_cast<SelectInst>(P)) {
2840 Worklist.push_back(SI->getTrueValue());
2841 Worklist.push_back(SI->getFalseValue());
2842 continue;
2843 }
2844
2845 if (PHINode *PN = dyn_cast<PHINode>(P)) {
Adam Nemete2b885c2015-04-23 20:09:20 +00002846 // If this PHI changes the underlying object in every iteration of the
2847 // loop, don't look through it. Consider:
2848 // int **A;
2849 // for (i) {
2850 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
2851 // Curr = A[i];
2852 // *Prev, *Curr;
2853 //
2854 // Prev is tracking Curr one iteration behind so they refer to different
2855 // underlying objects.
2856 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
2857 isSameUnderlyingObjectInLoop(PN, LI))
Pete Cooper833f34d2015-05-12 20:05:31 +00002858 for (Value *IncValue : PN->incoming_values())
2859 Worklist.push_back(IncValue);
Dan Gohmaned7c24e22012-05-10 18:57:38 +00002860 continue;
2861 }
2862
2863 Objects.push_back(P);
2864 } while (!Worklist.empty());
2865}
2866
Sanjay Patelaee84212014-11-04 16:27:42 +00002867/// Return true if the only users of this pointer are lifetime markers.
Nick Lewycky3e334a42011-06-27 04:20:45 +00002868bool llvm::onlyUsedByLifetimeMarkers(const Value *V) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00002869 for (const User *U : V->users()) {
2870 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
Nick Lewycky3e334a42011-06-27 04:20:45 +00002871 if (!II) return false;
2872
2873 if (II->getIntrinsicID() != Intrinsic::lifetime_start &&
2874 II->getIntrinsicID() != Intrinsic::lifetime_end)
2875 return false;
2876 }
2877 return true;
2878}
Dan Gohman75d7d5e2011-12-14 23:49:11 +00002879
Philip Reames5461d452015-04-23 17:36:48 +00002880static bool isDereferenceableFromAttribute(const Value *BV, APInt Offset,
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00002881 Type *Ty, const DataLayout &DL,
2882 const Instruction *CtxI,
2883 const DominatorTree *DT,
2884 const TargetLibraryInfo *TLI) {
Philip Reames5461d452015-04-23 17:36:48 +00002885 assert(Offset.isNonNegative() && "offset can't be negative");
2886 assert(Ty->isSized() && "must be sized");
2887
2888 APInt DerefBytes(Offset.getBitWidth(), 0);
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00002889 bool CheckForNonNull = false;
Philip Reames5461d452015-04-23 17:36:48 +00002890 if (const Argument *A = dyn_cast<Argument>(BV)) {
2891 DerefBytes = A->getDereferenceableBytes();
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00002892 if (!DerefBytes.getBoolValue()) {
2893 DerefBytes = A->getDereferenceableOrNullBytes();
2894 CheckForNonNull = true;
2895 }
Philip Reames5461d452015-04-23 17:36:48 +00002896 } else if (auto CS = ImmutableCallSite(BV)) {
2897 DerefBytes = CS.getDereferenceableBytes(0);
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00002898 if (!DerefBytes.getBoolValue()) {
2899 DerefBytes = CS.getDereferenceableOrNullBytes(0);
2900 CheckForNonNull = true;
2901 }
Sanjoy Dasf9995472015-05-19 20:10:19 +00002902 } else if (const LoadInst *LI = dyn_cast<LoadInst>(BV)) {
2903 if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {
2904 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
2905 DerefBytes = CI->getLimitedValue();
2906 }
2907 if (!DerefBytes.getBoolValue()) {
2908 if (MDNode *MD =
2909 LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
2910 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
2911 DerefBytes = CI->getLimitedValue();
2912 }
2913 CheckForNonNull = true;
2914 }
Philip Reames5461d452015-04-23 17:36:48 +00002915 }
2916
2917 if (DerefBytes.getBoolValue())
2918 if (DerefBytes.uge(Offset + DL.getTypeStoreSize(Ty)))
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00002919 if (!CheckForNonNull || isKnownNonNullAt(BV, CtxI, DT, TLI))
2920 return true;
2921
Philip Reames5461d452015-04-23 17:36:48 +00002922 return false;
2923}
2924
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00002925static bool isDereferenceableFromAttribute(const Value *V, const DataLayout &DL,
2926 const Instruction *CtxI,
2927 const DominatorTree *DT,
2928 const TargetLibraryInfo *TLI) {
Philip Reames5461d452015-04-23 17:36:48 +00002929 Type *VTy = V->getType();
2930 Type *Ty = VTy->getPointerElementType();
2931 if (!Ty->isSized())
2932 return false;
2933
2934 APInt Offset(DL.getTypeStoreSizeInBits(VTy), 0);
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00002935 return isDereferenceableFromAttribute(V, Offset, Ty, DL, CtxI, DT, TLI);
Philip Reames5461d452015-04-23 17:36:48 +00002936}
2937
2938/// Return true if Value is always a dereferenceable pointer.
2939///
2940/// Test if V is always a pointer to allocated and suitably aligned memory for
2941/// a simple load or store.
2942static bool isDereferenceablePointer(const Value *V, const DataLayout &DL,
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00002943 const Instruction *CtxI,
2944 const DominatorTree *DT,
2945 const TargetLibraryInfo *TLI,
Philip Reames5461d452015-04-23 17:36:48 +00002946 SmallPtrSetImpl<const Value *> &Visited) {
2947 // Note that it is not safe to speculate into a malloc'd region because
2948 // malloc may return null.
2949
2950 // These are obviously ok.
2951 if (isa<AllocaInst>(V)) return true;
2952
2953 // It's not always safe to follow a bitcast, for example:
2954 // bitcast i8* (alloca i8) to i32*
2955 // would result in a 4-byte load from a 1-byte alloca. However,
2956 // if we're casting from a pointer from a type of larger size
2957 // to a type of smaller size (or the same size), and the alignment
2958 // is at least as large as for the resulting pointer type, then
2959 // we can look through the bitcast.
2960 if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) {
2961 Type *STy = BC->getSrcTy()->getPointerElementType(),
2962 *DTy = BC->getDestTy()->getPointerElementType();
2963 if (STy->isSized() && DTy->isSized() &&
2964 (DL.getTypeStoreSize(STy) >= DL.getTypeStoreSize(DTy)) &&
2965 (DL.getABITypeAlignment(STy) >= DL.getABITypeAlignment(DTy)))
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00002966 return isDereferenceablePointer(BC->getOperand(0), DL, CtxI,
2967 DT, TLI, Visited);
Philip Reames5461d452015-04-23 17:36:48 +00002968 }
2969
2970 // Global variables which can't collapse to null are ok.
2971 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
2972 return !GV->hasExternalWeakLinkage();
2973
2974 // byval arguments are okay.
2975 if (const Argument *A = dyn_cast<Argument>(V))
2976 if (A->hasByValAttr())
2977 return true;
2978
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00002979 if (isDereferenceableFromAttribute(V, DL, CtxI, DT, TLI))
Philip Reames5461d452015-04-23 17:36:48 +00002980 return true;
2981
2982 // For GEPs, determine if the indexing lands within the allocated object.
2983 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Artur Pilipenko7fad7e52015-06-08 11:58:13 +00002984 Type *VTy = GEP->getType();
2985 Type *Ty = VTy->getPointerElementType();
2986 const Value *Base = GEP->getPointerOperand();
2987
Philip Reames5461d452015-04-23 17:36:48 +00002988 // Conservatively require that the base pointer be fully dereferenceable.
Artur Pilipenko7fad7e52015-06-08 11:58:13 +00002989 if (!Visited.insert(Base).second)
Philip Reames5461d452015-04-23 17:36:48 +00002990 return false;
Artur Pilipenko7fad7e52015-06-08 11:58:13 +00002991 if (!isDereferenceablePointer(Base, DL, CtxI,
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00002992 DT, TLI, Visited))
Philip Reames5461d452015-04-23 17:36:48 +00002993 return false;
Artur Pilipenko7fad7e52015-06-08 11:58:13 +00002994
2995 APInt Offset(DL.getPointerTypeSizeInBits(VTy), 0);
2996 if (!GEP->accumulateConstantOffset(DL, Offset))
2997 return false;
2998
2999 // Check if the load is within the bounds of the underlying object.
3000 uint64_t LoadSize = DL.getTypeStoreSize(Ty);
3001 Type *BaseType = Base->getType()->getPointerElementType();
3002 return (Offset + LoadSize).ule(DL.getTypeAllocSize(BaseType));
Philip Reames5461d452015-04-23 17:36:48 +00003003 }
3004
3005 // For gc.relocate, look through relocations
3006 if (const IntrinsicInst *I = dyn_cast<IntrinsicInst>(V))
3007 if (I->getIntrinsicID() == Intrinsic::experimental_gc_relocate) {
3008 GCRelocateOperands RelocateInst(I);
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00003009 return isDereferenceablePointer(RelocateInst.getDerivedPtr(), DL, CtxI,
3010 DT, TLI, Visited);
Philip Reames5461d452015-04-23 17:36:48 +00003011 }
3012
3013 if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V))
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00003014 return isDereferenceablePointer(ASC->getOperand(0), DL, CtxI,
3015 DT, TLI, Visited);
Philip Reames5461d452015-04-23 17:36:48 +00003016
3017 // If we don't know, assume the worst.
3018 return false;
3019}
3020
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00003021bool llvm::isDereferenceablePointer(const Value *V, const DataLayout &DL,
3022 const Instruction *CtxI,
3023 const DominatorTree *DT,
3024 const TargetLibraryInfo *TLI) {
Philip Reames5461d452015-04-23 17:36:48 +00003025 // When dereferenceability information is provided by a dereferenceable
3026 // attribute, we know exactly how many bytes are dereferenceable. If we can
3027 // determine the exact offset to the attributed variable, we can use that
3028 // information here.
3029 Type *VTy = V->getType();
3030 Type *Ty = VTy->getPointerElementType();
3031 if (Ty->isSized()) {
3032 APInt Offset(DL.getTypeStoreSizeInBits(VTy), 0);
3033 const Value *BV = V->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
3034
3035 if (Offset.isNonNegative())
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00003036 if (isDereferenceableFromAttribute(BV, Offset, Ty, DL,
3037 CtxI, DT, TLI))
Philip Reames5461d452015-04-23 17:36:48 +00003038 return true;
3039 }
3040
3041 SmallPtrSet<const Value *, 32> Visited;
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00003042 return ::isDereferenceablePointer(V, DL, CtxI, DT, TLI, Visited);
Philip Reames5461d452015-04-23 17:36:48 +00003043}
3044
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00003045bool llvm::isSafeToSpeculativelyExecute(const Value *V,
3046 const Instruction *CtxI,
3047 const DominatorTree *DT,
3048 const TargetLibraryInfo *TLI) {
Dan Gohman7ac046a2012-01-04 23:01:09 +00003049 const Operator *Inst = dyn_cast<Operator>(V);
3050 if (!Inst)
3051 return false;
3052
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003053 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
3054 if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i)))
3055 if (C->canTrap())
3056 return false;
3057
3058 switch (Inst->getOpcode()) {
3059 default:
3060 return true;
3061 case Instruction::UDiv:
David Majnemerf20d7c42014-11-04 23:49:08 +00003062 case Instruction::URem: {
3063 // x / y is undefined if y == 0.
3064 const APInt *V;
3065 if (match(Inst->getOperand(1), m_APInt(V)))
3066 return *V != 0;
3067 return false;
3068 }
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003069 case Instruction::SDiv:
3070 case Instruction::SRem: {
David Majnemerf20d7c42014-11-04 23:49:08 +00003071 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
David Majnemer8a6578a2015-02-01 19:10:19 +00003072 const APInt *Numerator, *Denominator;
3073 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
3074 return false;
3075 // We cannot hoist this division if the denominator is 0.
3076 if (*Denominator == 0)
3077 return false;
3078 // It's safe to hoist if the denominator is not 0 or -1.
3079 if (*Denominator != -1)
3080 return true;
3081 // At this point we know that the denominator is -1. It is safe to hoist as
3082 // long we know that the numerator is not INT_MIN.
3083 if (match(Inst->getOperand(0), m_APInt(Numerator)))
3084 return !Numerator->isMinSignedValue();
3085 // The numerator *might* be MinSignedValue.
David Majnemerf20d7c42014-11-04 23:49:08 +00003086 return false;
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003087 }
3088 case Instruction::Load: {
3089 const LoadInst *LI = cast<LoadInst>(Inst);
Kostya Serebryany0b458282013-11-21 07:29:28 +00003090 if (!LI->isUnordered() ||
3091 // Speculative load may create a race that did not exist in the source.
3092 LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeThread))
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003093 return false;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003094 const DataLayout &DL = LI->getModule()->getDataLayout();
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00003095 return isDereferenceablePointer(LI->getPointerOperand(), DL, CtxI, DT, TLI);
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003096 }
Nick Lewyckyb4039f62011-12-21 05:52:02 +00003097 case Instruction::Call: {
Michael Liao736bac62014-11-06 19:05:57 +00003098 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
3099 switch (II->getIntrinsicID()) {
3100 // These synthetic intrinsics have no side-effects and just mark
3101 // information about their operands.
3102 // FIXME: There are other no-op synthetic instructions that potentially
3103 // should be considered at least *safe* to speculate...
3104 case Intrinsic::dbg_declare:
3105 case Intrinsic::dbg_value:
3106 return true;
Chandler Carruth28192c92012-04-07 19:22:18 +00003107
Michael Liao736bac62014-11-06 19:05:57 +00003108 case Intrinsic::bswap:
3109 case Intrinsic::ctlz:
3110 case Intrinsic::ctpop:
3111 case Intrinsic::cttz:
3112 case Intrinsic::objectsize:
3113 case Intrinsic::sadd_with_overflow:
3114 case Intrinsic::smul_with_overflow:
3115 case Intrinsic::ssub_with_overflow:
3116 case Intrinsic::uadd_with_overflow:
3117 case Intrinsic::umul_with_overflow:
3118 case Intrinsic::usub_with_overflow:
3119 return true;
3120 // Sqrt should be OK, since the llvm sqrt intrinsic isn't defined to set
3121 // errno like libm sqrt would.
3122 case Intrinsic::sqrt:
3123 case Intrinsic::fma:
3124 case Intrinsic::fmuladd:
3125 case Intrinsic::fabs:
3126 case Intrinsic::minnum:
3127 case Intrinsic::maxnum:
3128 return true;
3129 // TODO: some fp intrinsics are marked as having the same error handling
3130 // as libm. They're safe to speculate when they won't error.
3131 // TODO: are convert_{from,to}_fp16 safe?
3132 // TODO: can we list target-specific intrinsics here?
3133 default: break;
3134 }
3135 }
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003136 return false; // The called function could have undefined behavior or
Nick Lewyckyb4039f62011-12-21 05:52:02 +00003137 // side-effects, even if marked readnone nounwind.
3138 }
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003139 case Instruction::VAArg:
3140 case Instruction::Alloca:
3141 case Instruction::Invoke:
3142 case Instruction::PHI:
3143 case Instruction::Store:
3144 case Instruction::Ret:
3145 case Instruction::Br:
3146 case Instruction::IndirectBr:
3147 case Instruction::Switch:
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003148 case Instruction::Unreachable:
3149 case Instruction::Fence:
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003150 case Instruction::AtomicRMW:
3151 case Instruction::AtomicCmpXchg:
David Majnemer654e1302015-07-31 17:58:14 +00003152 case Instruction::LandingPad:
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003153 case Instruction::Resume:
David Majnemer654e1302015-07-31 17:58:14 +00003154 case Instruction::CatchPad:
3155 case Instruction::CatchEndPad:
3156 case Instruction::CatchRet:
3157 case Instruction::CleanupPad:
3158 case Instruction::CleanupRet:
3159 case Instruction::TerminatePad:
Dan Gohman75d7d5e2011-12-14 23:49:11 +00003160 return false; // Misc instructions which have effects
3161 }
3162}
Dan Gohman1b0f79d2013-01-31 02:40:59 +00003163
Quentin Colombet6443cce2015-08-06 18:44:34 +00003164bool llvm::mayBeMemoryDependent(const Instruction &I) {
3165 return I.mayReadOrWriteMemory() || !isSafeToSpeculativelyExecute(&I);
3166}
3167
Sanjay Patelaee84212014-11-04 16:27:42 +00003168/// Return true if we know that the specified value is never null.
Benjamin Kramerfd4777c2013-09-24 16:37:51 +00003169bool llvm::isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI) {
Dan Gohman1b0f79d2013-01-31 02:40:59 +00003170 // Alloca never returns null, malloc might.
3171 if (isa<AllocaInst>(V)) return true;
3172
Nick Lewyckyd52b1522014-05-20 01:23:40 +00003173 // A byval, inalloca, or nonnull argument is never null.
Dan Gohman1b0f79d2013-01-31 02:40:59 +00003174 if (const Argument *A = dyn_cast<Argument>(V))
Nick Lewyckyd52b1522014-05-20 01:23:40 +00003175 return A->hasByValOrInAllocaAttr() || A->hasNonNullAttr();
Dan Gohman1b0f79d2013-01-31 02:40:59 +00003176
3177 // Global values are not null unless extern weak.
3178 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
3179 return !GV->hasExternalWeakLinkage();
Benjamin Kramerfd4777c2013-09-24 16:37:51 +00003180
Philip Reamescdb72f32014-10-20 22:40:55 +00003181 // A Load tagged w/nonnull metadata is never null.
3182 if (const LoadInst *LI = dyn_cast<LoadInst>(V))
Philip Reames5a3f5f72014-10-21 00:13:20 +00003183 return LI->getMetadata(LLVMContext::MD_nonnull);
Philip Reamescdb72f32014-10-20 22:40:55 +00003184
Benjamin Kramer3a09ef62015-04-10 14:50:08 +00003185 if (auto CS = ImmutableCallSite(V))
Hal Finkelb0407ba2014-07-18 15:51:28 +00003186 if (CS.isReturnNonNull())
Nick Lewyckyec373542014-05-20 05:13:21 +00003187 return true;
3188
Benjamin Kramerfd4777c2013-09-24 16:37:51 +00003189 // operator new never returns null.
3190 if (isOperatorNewLikeFn(V, TLI, /*LookThroughBitCast=*/true))
3191 return true;
3192
Dan Gohman1b0f79d2013-01-31 02:40:59 +00003193 return false;
3194}
David Majnemer491331a2015-01-02 07:29:43 +00003195
Sanjoy Dasf8a0db52015-05-18 18:07:00 +00003196static bool isKnownNonNullFromDominatingCondition(const Value *V,
3197 const Instruction *CtxI,
3198 const DominatorTree *DT) {
3199 unsigned NumUsesExplored = 0;
3200 for (auto U : V->users()) {
3201 // Avoid massive lists
3202 if (NumUsesExplored >= DomConditionsMaxUses)
3203 break;
3204 NumUsesExplored++;
3205 // Consider only compare instructions uniquely controlling a branch
3206 const ICmpInst *Cmp = dyn_cast<ICmpInst>(U);
3207 if (!Cmp)
3208 continue;
3209
3210 if (DomConditionsSingleCmpUse && !Cmp->hasOneUse())
3211 continue;
3212
3213 for (auto *CmpU : Cmp->users()) {
3214 const BranchInst *BI = dyn_cast<BranchInst>(CmpU);
3215 if (!BI)
3216 continue;
3217
3218 assert(BI->isConditional() && "uses a comparison!");
3219
3220 BasicBlock *NonNullSuccessor = nullptr;
3221 CmpInst::Predicate Pred;
3222
3223 if (match(const_cast<ICmpInst*>(Cmp),
3224 m_c_ICmp(Pred, m_Specific(V), m_Zero()))) {
3225 if (Pred == ICmpInst::ICMP_EQ)
3226 NonNullSuccessor = BI->getSuccessor(1);
3227 else if (Pred == ICmpInst::ICMP_NE)
3228 NonNullSuccessor = BI->getSuccessor(0);
3229 }
3230
3231 if (NonNullSuccessor) {
3232 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
3233 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
3234 return true;
3235 }
3236 }
3237 }
3238
3239 return false;
3240}
3241
3242bool llvm::isKnownNonNullAt(const Value *V, const Instruction *CtxI,
3243 const DominatorTree *DT, const TargetLibraryInfo *TLI) {
3244 if (isKnownNonNull(V, TLI))
3245 return true;
3246
3247 return CtxI ? ::isKnownNonNullFromDominatingCondition(V, CtxI, DT) : false;
3248}
3249
David Majnemer491331a2015-01-02 07:29:43 +00003250OverflowResult llvm::computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003251 const DataLayout &DL,
Chandler Carruth66b31302015-01-04 12:03:27 +00003252 AssumptionCache *AC,
David Majnemer491331a2015-01-02 07:29:43 +00003253 const Instruction *CxtI,
3254 const DominatorTree *DT) {
3255 // Multiplying n * m significant bits yields a result of n + m significant
3256 // bits. If the total number of significant bits does not exceed the
3257 // result bit width (minus 1), there is no overflow.
3258 // This means if we have enough leading zero bits in the operands
3259 // we can guarantee that the result does not overflow.
3260 // Ref: "Hacker's Delight" by Henry Warren
3261 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
3262 APInt LHSKnownZero(BitWidth, 0);
David Majnemerc8a576b2015-01-02 07:29:47 +00003263 APInt LHSKnownOne(BitWidth, 0);
David Majnemer491331a2015-01-02 07:29:43 +00003264 APInt RHSKnownZero(BitWidth, 0);
David Majnemerc8a576b2015-01-02 07:29:47 +00003265 APInt RHSKnownOne(BitWidth, 0);
Chandler Carruth66b31302015-01-04 12:03:27 +00003266 computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, DL, /*Depth=*/0, AC, CxtI,
3267 DT);
3268 computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, DL, /*Depth=*/0, AC, CxtI,
3269 DT);
David Majnemer491331a2015-01-02 07:29:43 +00003270 // Note that underestimating the number of zero bits gives a more
3271 // conservative answer.
3272 unsigned ZeroBits = LHSKnownZero.countLeadingOnes() +
3273 RHSKnownZero.countLeadingOnes();
3274 // First handle the easy case: if we have enough zero bits there's
3275 // definitely no overflow.
3276 if (ZeroBits >= BitWidth)
3277 return OverflowResult::NeverOverflows;
3278
3279 // Get the largest possible values for each operand.
3280 APInt LHSMax = ~LHSKnownZero;
3281 APInt RHSMax = ~RHSKnownZero;
3282
3283 // We know the multiply operation doesn't overflow if the maximum values for
3284 // each operand will not overflow after we multiply them together.
David Majnemerc8a576b2015-01-02 07:29:47 +00003285 bool MaxOverflow;
3286 LHSMax.umul_ov(RHSMax, MaxOverflow);
3287 if (!MaxOverflow)
3288 return OverflowResult::NeverOverflows;
David Majnemer491331a2015-01-02 07:29:43 +00003289
David Majnemerc8a576b2015-01-02 07:29:47 +00003290 // We know it always overflows if multiplying the smallest possible values for
3291 // the operands also results in overflow.
3292 bool MinOverflow;
3293 LHSKnownOne.umul_ov(RHSKnownOne, MinOverflow);
3294 if (MinOverflow)
3295 return OverflowResult::AlwaysOverflows;
3296
3297 return OverflowResult::MayOverflow;
David Majnemer491331a2015-01-02 07:29:43 +00003298}
David Majnemer5310c1e2015-01-07 00:39:50 +00003299
3300OverflowResult llvm::computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003301 const DataLayout &DL,
David Majnemer5310c1e2015-01-07 00:39:50 +00003302 AssumptionCache *AC,
3303 const Instruction *CxtI,
3304 const DominatorTree *DT) {
3305 bool LHSKnownNonNegative, LHSKnownNegative;
3306 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, DL, /*Depth=*/0,
3307 AC, CxtI, DT);
3308 if (LHSKnownNonNegative || LHSKnownNegative) {
3309 bool RHSKnownNonNegative, RHSKnownNegative;
3310 ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, DL, /*Depth=*/0,
3311 AC, CxtI, DT);
3312
3313 if (LHSKnownNegative && RHSKnownNegative) {
3314 // The sign bit is set in both cases: this MUST overflow.
3315 // Create a simple add instruction, and insert it into the struct.
3316 return OverflowResult::AlwaysOverflows;
3317 }
3318
3319 if (LHSKnownNonNegative && RHSKnownNonNegative) {
3320 // The sign bit is clear in both cases: this CANNOT overflow.
3321 // Create a simple add instruction, and insert it into the struct.
3322 return OverflowResult::NeverOverflows;
3323 }
3324 }
3325
3326 return OverflowResult::MayOverflow;
3327}
James Molloy71b91c22015-05-11 14:42:20 +00003328
Jingyue Wu42f1d672015-07-28 18:22:40 +00003329bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) {
3330 // FIXME: This conservative implementation can be relaxed. E.g. most
3331 // atomic operations are guaranteed to terminate on most platforms
3332 // and most functions terminate.
3333
3334 return !I->isAtomic() && // atomics may never succeed on some platforms
3335 !isa<CallInst>(I) && // could throw and might not terminate
3336 !isa<InvokeInst>(I) && // might not terminate and could throw to
3337 // non-successor (see bug 24185 for details).
3338 !isa<ResumeInst>(I) && // has no successors
3339 !isa<ReturnInst>(I); // has no successors
3340}
3341
3342bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I,
3343 const Loop *L) {
3344 // The loop header is guaranteed to be executed for every iteration.
3345 //
3346 // FIXME: Relax this constraint to cover all basic blocks that are
3347 // guaranteed to be executed at every iteration.
3348 if (I->getParent() != L->getHeader()) return false;
3349
3350 for (const Instruction &LI : *L->getHeader()) {
3351 if (&LI == I) return true;
3352 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
3353 }
3354 llvm_unreachable("Instruction not contained in its own parent basic block.");
3355}
3356
3357bool llvm::propagatesFullPoison(const Instruction *I) {
3358 switch (I->getOpcode()) {
3359 case Instruction::Add:
3360 case Instruction::Sub:
3361 case Instruction::Xor:
3362 case Instruction::Trunc:
3363 case Instruction::BitCast:
3364 case Instruction::AddrSpaceCast:
3365 // These operations all propagate poison unconditionally. Note that poison
3366 // is not any particular value, so xor or subtraction of poison with
3367 // itself still yields poison, not zero.
3368 return true;
3369
3370 case Instruction::AShr:
3371 case Instruction::SExt:
3372 // For these operations, one bit of the input is replicated across
3373 // multiple output bits. A replicated poison bit is still poison.
3374 return true;
3375
3376 case Instruction::Shl: {
3377 // Left shift *by* a poison value is poison. The number of
3378 // positions to shift is unsigned, so no negative values are
3379 // possible there. Left shift by zero places preserves poison. So
3380 // it only remains to consider left shift of poison by a positive
3381 // number of places.
3382 //
3383 // A left shift by a positive number of places leaves the lowest order bit
3384 // non-poisoned. However, if such a shift has a no-wrap flag, then we can
3385 // make the poison operand violate that flag, yielding a fresh full-poison
3386 // value.
3387 auto *OBO = cast<OverflowingBinaryOperator>(I);
3388 return OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap();
3389 }
3390
3391 case Instruction::Mul: {
3392 // A multiplication by zero yields a non-poison zero result, so we need to
3393 // rule out zero as an operand. Conservatively, multiplication by a
3394 // non-zero constant is not multiplication by zero.
3395 //
3396 // Multiplication by a non-zero constant can leave some bits
3397 // non-poisoned. For example, a multiplication by 2 leaves the lowest
3398 // order bit unpoisoned. So we need to consider that.
3399 //
3400 // Multiplication by 1 preserves poison. If the multiplication has a
3401 // no-wrap flag, then we can make the poison operand violate that flag
3402 // when multiplied by any integer other than 0 and 1.
3403 auto *OBO = cast<OverflowingBinaryOperator>(I);
3404 if (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) {
3405 for (Value *V : OBO->operands()) {
3406 if (auto *CI = dyn_cast<ConstantInt>(V)) {
3407 // A ConstantInt cannot yield poison, so we can assume that it is
3408 // the other operand that is poison.
3409 return !CI->isZero();
3410 }
3411 }
3412 }
3413 return false;
3414 }
3415
3416 case Instruction::GetElementPtr:
3417 // A GEP implicitly represents a sequence of additions, subtractions,
3418 // truncations, sign extensions and multiplications. The multiplications
3419 // are by the non-zero sizes of some set of types, so we do not have to be
3420 // concerned with multiplication by zero. If the GEP is in-bounds, then
3421 // these operations are implicitly no-signed-wrap so poison is propagated
3422 // by the arguments above for Add, Sub, Trunc, SExt and Mul.
3423 return cast<GEPOperator>(I)->isInBounds();
3424
3425 default:
3426 return false;
3427 }
3428}
3429
3430const Value *llvm::getGuaranteedNonFullPoisonOp(const Instruction *I) {
3431 switch (I->getOpcode()) {
3432 case Instruction::Store:
3433 return cast<StoreInst>(I)->getPointerOperand();
3434
3435 case Instruction::Load:
3436 return cast<LoadInst>(I)->getPointerOperand();
3437
3438 case Instruction::AtomicCmpXchg:
3439 return cast<AtomicCmpXchgInst>(I)->getPointerOperand();
3440
3441 case Instruction::AtomicRMW:
3442 return cast<AtomicRMWInst>(I)->getPointerOperand();
3443
3444 case Instruction::UDiv:
3445 case Instruction::SDiv:
3446 case Instruction::URem:
3447 case Instruction::SRem:
3448 return I->getOperand(1);
3449
3450 default:
3451 return nullptr;
3452 }
3453}
3454
3455bool llvm::isKnownNotFullPoison(const Instruction *PoisonI) {
3456 // We currently only look for uses of poison values within the same basic
3457 // block, as that makes it easier to guarantee that the uses will be
3458 // executed given that PoisonI is executed.
3459 //
3460 // FIXME: Expand this to consider uses beyond the same basic block. To do
3461 // this, look out for the distinction between post-dominance and strong
3462 // post-dominance.
3463 const BasicBlock *BB = PoisonI->getParent();
3464
3465 // Set of instructions that we have proved will yield poison if PoisonI
3466 // does.
3467 SmallSet<const Value *, 16> YieldsPoison;
3468 YieldsPoison.insert(PoisonI);
3469
3470 for (const Instruction *I = PoisonI, *E = BB->end(); I != E;
3471 I = I->getNextNode()) {
3472 if (I != PoisonI) {
3473 const Value *NotPoison = getGuaranteedNonFullPoisonOp(I);
3474 if (NotPoison != nullptr && YieldsPoison.count(NotPoison)) return true;
3475 if (!isGuaranteedToTransferExecutionToSuccessor(I)) return false;
3476 }
3477
3478 // Mark poison that propagates from I through uses of I.
3479 if (YieldsPoison.count(I)) {
3480 for (const User *User : I->users()) {
3481 const Instruction *UserI = cast<Instruction>(User);
3482 if (UserI->getParent() == BB && propagatesFullPoison(UserI))
3483 YieldsPoison.insert(User);
3484 }
3485 }
3486 }
3487 return false;
3488}
3489
James Molloy134bec22015-08-11 09:12:57 +00003490static bool isKnownNonNaN(Value *V, FastMathFlags FMF) {
3491 if (FMF.noNaNs())
3492 return true;
3493
3494 if (auto *C = dyn_cast<ConstantFP>(V))
3495 return !C->isNaN();
3496 return false;
3497}
3498
3499static bool isKnownNonZero(Value *V) {
3500 if (auto *C = dyn_cast<ConstantFP>(V))
3501 return !C->isZero();
3502 return false;
3503}
3504
3505static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred,
3506 FastMathFlags FMF,
James Molloy270ef8c2015-05-15 16:04:50 +00003507 Value *CmpLHS, Value *CmpRHS,
3508 Value *TrueVal, Value *FalseVal,
3509 Value *&LHS, Value *&RHS) {
James Molloy71b91c22015-05-11 14:42:20 +00003510 LHS = CmpLHS;
3511 RHS = CmpRHS;
3512
James Molloy134bec22015-08-11 09:12:57 +00003513 // If the predicate is an "or-equal" (FP) predicate, then signed zeroes may
3514 // return inconsistent results between implementations.
3515 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
3516 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
3517 // Therefore we behave conservatively and only proceed if at least one of the
3518 // operands is known to not be zero, or if we don't care about signed zeroes.
3519 switch (Pred) {
3520 default: break;
3521 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE:
3522 case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE:
3523 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
3524 !isKnownNonZero(CmpRHS))
3525 return {SPF_UNKNOWN, SPNB_NA, false};
3526 }
3527
3528 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
3529 bool Ordered = false;
3530
3531 // When given one NaN and one non-NaN input:
3532 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
3533 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
3534 // ordered comparison fails), which could be NaN or non-NaN.
3535 // so here we discover exactly what NaN behavior is required/accepted.
3536 if (CmpInst::isFPPredicate(Pred)) {
3537 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
3538 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
3539
3540 if (LHSSafe && RHSSafe) {
3541 // Both operands are known non-NaN.
3542 NaNBehavior = SPNB_RETURNS_ANY;
3543 } else if (CmpInst::isOrdered(Pred)) {
3544 // An ordered comparison will return false when given a NaN, so it
3545 // returns the RHS.
3546 Ordered = true;
3547 if (LHSSafe)
James Molloy8990b062015-08-12 15:11:43 +00003548 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
James Molloy134bec22015-08-11 09:12:57 +00003549 NaNBehavior = SPNB_RETURNS_NAN;
3550 else if (RHSSafe)
3551 NaNBehavior = SPNB_RETURNS_OTHER;
3552 else
3553 // Completely unsafe.
3554 return {SPF_UNKNOWN, SPNB_NA, false};
3555 } else {
3556 Ordered = false;
3557 // An unordered comparison will return true when given a NaN, so it
3558 // returns the LHS.
3559 if (LHSSafe)
James Molloy8990b062015-08-12 15:11:43 +00003560 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
James Molloy134bec22015-08-11 09:12:57 +00003561 NaNBehavior = SPNB_RETURNS_OTHER;
3562 else if (RHSSafe)
3563 NaNBehavior = SPNB_RETURNS_NAN;
3564 else
3565 // Completely unsafe.
3566 return {SPF_UNKNOWN, SPNB_NA, false};
James Molloy71b91c22015-05-11 14:42:20 +00003567 }
3568 }
3569
James Molloy71b91c22015-05-11 14:42:20 +00003570 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
James Molloy134bec22015-08-11 09:12:57 +00003571 std::swap(CmpLHS, CmpRHS);
3572 Pred = CmpInst::getSwappedPredicate(Pred);
3573 if (NaNBehavior == SPNB_RETURNS_NAN)
3574 NaNBehavior = SPNB_RETURNS_OTHER;
3575 else if (NaNBehavior == SPNB_RETURNS_OTHER)
3576 NaNBehavior = SPNB_RETURNS_NAN;
3577 Ordered = !Ordered;
3578 }
3579
3580 // ([if]cmp X, Y) ? X : Y
3581 if (TrueVal == CmpLHS && FalseVal == CmpRHS) {
James Molloy71b91c22015-05-11 14:42:20 +00003582 switch (Pred) {
James Molloy134bec22015-08-11 09:12:57 +00003583 default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
James Molloy71b91c22015-05-11 14:42:20 +00003584 case ICmpInst::ICMP_UGT:
James Molloy134bec22015-08-11 09:12:57 +00003585 case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false};
James Molloy71b91c22015-05-11 14:42:20 +00003586 case ICmpInst::ICMP_SGT:
James Molloy134bec22015-08-11 09:12:57 +00003587 case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false};
James Molloy71b91c22015-05-11 14:42:20 +00003588 case ICmpInst::ICMP_ULT:
James Molloy134bec22015-08-11 09:12:57 +00003589 case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false};
James Molloy71b91c22015-05-11 14:42:20 +00003590 case ICmpInst::ICMP_SLT:
James Molloy134bec22015-08-11 09:12:57 +00003591 case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false};
3592 case FCmpInst::FCMP_UGT:
3593 case FCmpInst::FCMP_UGE:
3594 case FCmpInst::FCMP_OGT:
3595 case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered};
3596 case FCmpInst::FCMP_ULT:
3597 case FCmpInst::FCMP_ULE:
3598 case FCmpInst::FCMP_OLT:
3599 case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered};
James Molloy71b91c22015-05-11 14:42:20 +00003600 }
3601 }
3602
3603 if (ConstantInt *C1 = dyn_cast<ConstantInt>(CmpRHS)) {
3604 if ((CmpLHS == TrueVal && match(FalseVal, m_Neg(m_Specific(CmpLHS)))) ||
3605 (CmpLHS == FalseVal && match(TrueVal, m_Neg(m_Specific(CmpLHS))))) {
3606
3607 // ABS(X) ==> (X >s 0) ? X : -X and (X >s -1) ? X : -X
3608 // NABS(X) ==> (X >s 0) ? -X : X and (X >s -1) ? -X : X
3609 if (Pred == ICmpInst::ICMP_SGT && (C1->isZero() || C1->isMinusOne())) {
James Molloy134bec22015-08-11 09:12:57 +00003610 return {(CmpLHS == TrueVal) ? SPF_ABS : SPF_NABS, SPNB_NA, false};
James Molloy71b91c22015-05-11 14:42:20 +00003611 }
3612
3613 // ABS(X) ==> (X <s 0) ? -X : X and (X <s 1) ? -X : X
3614 // NABS(X) ==> (X <s 0) ? X : -X and (X <s 1) ? X : -X
3615 if (Pred == ICmpInst::ICMP_SLT && (C1->isZero() || C1->isOne())) {
James Molloy134bec22015-08-11 09:12:57 +00003616 return {(CmpLHS == FalseVal) ? SPF_ABS : SPF_NABS, SPNB_NA, false};
James Molloy71b91c22015-05-11 14:42:20 +00003617 }
3618 }
3619
3620 // Y >s C ? ~Y : ~C == ~Y <s ~C ? ~Y : ~C = SMIN(~Y, ~C)
3621 if (const auto *C2 = dyn_cast<ConstantInt>(FalseVal)) {
3622 if (C1->getType() == C2->getType() && ~C1->getValue() == C2->getValue() &&
3623 (match(TrueVal, m_Not(m_Specific(CmpLHS))) ||
3624 match(CmpLHS, m_Not(m_Specific(TrueVal))))) {
3625 LHS = TrueVal;
3626 RHS = FalseVal;
James Molloy134bec22015-08-11 09:12:57 +00003627 return {SPF_SMIN, SPNB_NA, false};
James Molloy71b91c22015-05-11 14:42:20 +00003628 }
3629 }
3630 }
3631
3632 // TODO: (X > 4) ? X : 5 --> (X >= 5) ? X : 5 --> MAX(X, 5)
3633
James Molloy134bec22015-08-11 09:12:57 +00003634 return {SPF_UNKNOWN, SPNB_NA, false};
James Molloy71b91c22015-05-11 14:42:20 +00003635}
James Molloy270ef8c2015-05-15 16:04:50 +00003636
James Molloy134bec22015-08-11 09:12:57 +00003637static Constant *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
James Molloy270ef8c2015-05-15 16:04:50 +00003638 Instruction::CastOps *CastOp) {
3639 CastInst *CI = dyn_cast<CastInst>(V1);
3640 Constant *C = dyn_cast<Constant>(V2);
3641 if (!CI || !C)
3642 return nullptr;
3643 *CastOp = CI->getOpcode();
3644
James Molloy2b21a7c2015-05-20 18:41:25 +00003645 if (isa<SExtInst>(CI) && CmpI->isSigned()) {
3646 Constant *T = ConstantExpr::getTrunc(C, CI->getSrcTy());
3647 // This is only valid if the truncated value can be sign-extended
3648 // back to the original value.
3649 if (ConstantExpr::getSExt(T, C->getType()) == C)
3650 return T;
3651 return nullptr;
3652 }
3653 if (isa<ZExtInst>(CI) && CmpI->isUnsigned())
James Molloy270ef8c2015-05-15 16:04:50 +00003654 return ConstantExpr::getTrunc(C, CI->getSrcTy());
3655
3656 if (isa<TruncInst>(CI))
3657 return ConstantExpr::getIntegerCast(C, CI->getSrcTy(), CmpI->isSigned());
3658
James Molloy134bec22015-08-11 09:12:57 +00003659 if (isa<FPToUIInst>(CI))
3660 return ConstantExpr::getUIToFP(C, CI->getSrcTy(), true);
3661
3662 if (isa<FPToSIInst>(CI))
3663 return ConstantExpr::getSIToFP(C, CI->getSrcTy(), true);
3664
3665 if (isa<UIToFPInst>(CI))
3666 return ConstantExpr::getFPToUI(C, CI->getSrcTy(), true);
3667
3668 if (isa<SIToFPInst>(CI))
3669 return ConstantExpr::getFPToSI(C, CI->getSrcTy(), true);
3670
3671 if (isa<FPTruncInst>(CI))
3672 return ConstantExpr::getFPExtend(C, CI->getSrcTy(), true);
3673
3674 if (isa<FPExtInst>(CI))
3675 return ConstantExpr::getFPTrunc(C, CI->getSrcTy(), true);
3676
James Molloy270ef8c2015-05-15 16:04:50 +00003677 return nullptr;
3678}
3679
James Molloy134bec22015-08-11 09:12:57 +00003680SelectPatternResult llvm::matchSelectPattern(Value *V,
James Molloy270ef8c2015-05-15 16:04:50 +00003681 Value *&LHS, Value *&RHS,
3682 Instruction::CastOps *CastOp) {
3683 SelectInst *SI = dyn_cast<SelectInst>(V);
James Molloy134bec22015-08-11 09:12:57 +00003684 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
James Molloy270ef8c2015-05-15 16:04:50 +00003685
James Molloy134bec22015-08-11 09:12:57 +00003686 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
3687 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
James Molloy270ef8c2015-05-15 16:04:50 +00003688
James Molloy134bec22015-08-11 09:12:57 +00003689 CmpInst::Predicate Pred = CmpI->getPredicate();
James Molloy270ef8c2015-05-15 16:04:50 +00003690 Value *CmpLHS = CmpI->getOperand(0);
3691 Value *CmpRHS = CmpI->getOperand(1);
3692 Value *TrueVal = SI->getTrueValue();
3693 Value *FalseVal = SI->getFalseValue();
James Molloy134bec22015-08-11 09:12:57 +00003694 FastMathFlags FMF;
3695 if (isa<FPMathOperator>(CmpI))
3696 FMF = CmpI->getFastMathFlags();
James Molloy270ef8c2015-05-15 16:04:50 +00003697
3698 // Bail out early.
3699 if (CmpI->isEquality())
James Molloy134bec22015-08-11 09:12:57 +00003700 return {SPF_UNKNOWN, SPNB_NA, false};
James Molloy270ef8c2015-05-15 16:04:50 +00003701
3702 // Deal with type mismatches.
3703 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
3704 if (Constant *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp))
James Molloy134bec22015-08-11 09:12:57 +00003705 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
James Molloy270ef8c2015-05-15 16:04:50 +00003706 cast<CastInst>(TrueVal)->getOperand(0), C,
3707 LHS, RHS);
3708 if (Constant *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp))
James Molloy134bec22015-08-11 09:12:57 +00003709 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
James Molloy270ef8c2015-05-15 16:04:50 +00003710 C, cast<CastInst>(FalseVal)->getOperand(0),
3711 LHS, RHS);
3712 }
James Molloy134bec22015-08-11 09:12:57 +00003713 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
James Molloy270ef8c2015-05-15 16:04:50 +00003714 LHS, RHS);
3715}