blob: b9a48eaf5711ca763777dc28f8da2e98e710fc14 [file] [log] [blame]
Chris Lattner173234a2008-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"
Dan Gohman24371272010-12-15 20:10:26 +000016#include "llvm/Analysis/InstructionSimplify.h"
Chris Lattner173234a2008-06-02 01:18:21 +000017#include "llvm/Constants.h"
18#include "llvm/Instructions.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000019#include "llvm/GlobalVariable.h"
Dan Gohman307a7c42009-09-15 16:14:44 +000020#include "llvm/GlobalAlias.h"
Chris Lattner173234a2008-06-02 01:18:21 +000021#include "llvm/IntrinsicInst.h"
Owen Anderson76f600b2009-07-06 22:37:39 +000022#include "llvm/LLVMContext.h"
Dan Gohmanca178902009-07-17 20:47:02 +000023#include "llvm/Operator.h"
Bill Wendling0582ae92009-03-13 04:39:26 +000024#include "llvm/Target/TargetData.h"
Chris Lattner173234a2008-06-02 01:18:21 +000025#include "llvm/Support/GetElementPtrTypeIterator.h"
26#include "llvm/Support/MathExtras.h"
Duncan Sandsd70d1a52011-01-25 09:38:29 +000027#include "llvm/Support/PatternMatch.h"
Eric Christopher25ec4832010-03-05 06:58:57 +000028#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner32a9e7a2008-06-04 04:46:14 +000029#include <cstring>
Chris Lattner173234a2008-06-02 01:18:21 +000030using namespace llvm;
Duncan Sandsd70d1a52011-01-25 09:38:29 +000031using namespace llvm::PatternMatch;
32
33const unsigned MaxDepth = 6;
34
35/// getBitWidth - Returns the bitwidth of the given scalar or pointer type (if
36/// unknown returns 0). For vector types, returns the element type's bitwidth.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000037static unsigned getBitWidth(Type *Ty, const TargetData *TD) {
Duncan Sandsd70d1a52011-01-25 09:38:29 +000038 if (unsigned BitWidth = Ty->getScalarSizeInBits())
39 return BitWidth;
40 assert(isa<PointerType>(Ty) && "Expected a pointer type!");
41 return TD ? TD->getPointerSizeInBits() : 0;
42}
Chris Lattner173234a2008-06-02 01:18:21 +000043
Chris Lattner173234a2008-06-02 01:18:21 +000044/// ComputeMaskedBits - Determine which of the bits specified in Mask are
45/// known to be either zero or one and return them in the KnownZero/KnownOne
46/// bit sets. This code only analyzes bits in Mask, in order to short-circuit
47/// processing.
48/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
49/// we cannot optimize based on the assumption that it is zero without changing
50/// it to be an explicit zero. If we don't change it to zero, other code could
51/// optimized based on the contradictory assumption that it is non-zero.
52/// Because instcombine aggressively folds operations with undef args anyway,
53/// this won't lose us code quality.
Chris Lattnercf5128e2009-09-08 00:06:16 +000054///
55/// This function is defined on values with integer type, values with pointer
56/// type (but only if TD is non-null), and vectors of integers. In the case
57/// where V is a vector, the mask, known zero, and known one values are the
58/// same width as the vector element, and the bit is set only if it is true
59/// for all of the elements in the vector.
Chris Lattner173234a2008-06-02 01:18:21 +000060void llvm::ComputeMaskedBits(Value *V, const APInt &Mask,
61 APInt &KnownZero, APInt &KnownOne,
Dan Gohman846a2f22009-08-27 17:51:25 +000062 const TargetData *TD, unsigned Depth) {
Chris Lattner173234a2008-06-02 01:18:21 +000063 assert(V && "No Value?");
Dan Gohman9004c8a2009-05-21 02:28:33 +000064 assert(Depth <= MaxDepth && "Limit Search Depth");
Chris Lattner79abedb2009-01-20 18:22:57 +000065 unsigned BitWidth = Mask.getBitWidth();
Nadav Rotem16087692011-12-05 06:29:09 +000066 assert((V->getType()->isIntOrIntVectorTy() ||
67 V->getType()->getScalarType()->isPointerTy()) &&
68 "Not integer or pointer type!");
Dan Gohman6de29f82009-06-15 22:12:54 +000069 assert((!TD ||
70 TD->getTypeSizeInBits(V->getType()->getScalarType()) == BitWidth) &&
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000071 (!V->getType()->isIntOrIntVectorTy() ||
Dan Gohman6de29f82009-06-15 22:12:54 +000072 V->getType()->getScalarSizeInBits() == BitWidth) &&
Nadav Rotem16087692011-12-05 06:29:09 +000073 KnownZero.getBitWidth() == BitWidth &&
Chris Lattner173234a2008-06-02 01:18:21 +000074 KnownOne.getBitWidth() == BitWidth &&
75 "V, Mask, KnownOne and KnownZero should have same BitWidth");
76
77 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
78 // We know all of the bits for a constant!
79 KnownOne = CI->getValue() & Mask;
80 KnownZero = ~KnownOne & Mask;
81 return;
82 }
Dan Gohman6de29f82009-06-15 22:12:54 +000083 // Null and aggregate-zero are all-zeros.
84 if (isa<ConstantPointerNull>(V) ||
85 isa<ConstantAggregateZero>(V)) {
Jay Foad7a874dd2010-12-01 08:53:58 +000086 KnownOne.clearAllBits();
Chris Lattner173234a2008-06-02 01:18:21 +000087 KnownZero = Mask;
88 return;
89 }
Dan Gohman6de29f82009-06-15 22:12:54 +000090 // Handle a constant vector by taking the intersection of the known bits of
91 // each element.
92 if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
Jay Foad7a874dd2010-12-01 08:53:58 +000093 KnownZero.setAllBits(); KnownOne.setAllBits();
Dan Gohman6de29f82009-06-15 22:12:54 +000094 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
95 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
96 ComputeMaskedBits(CV->getOperand(i), Mask, KnownZero2, KnownOne2,
97 TD, Depth);
98 KnownZero &= KnownZero2;
99 KnownOne &= KnownOne2;
100 }
101 return;
102 }
Chris Lattner173234a2008-06-02 01:18:21 +0000103 // The address of an aligned GlobalValue has trailing zeros.
104 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
105 unsigned Align = GV->getAlignment();
Dan Gohman00407252009-08-11 15:50:03 +0000106 if (Align == 0 && TD && GV->getType()->getElementType()->isSized()) {
Eli Friedmanc4c2a022011-11-28 22:48:22 +0000107 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
108 Type *ObjectType = GVar->getType()->getElementType();
109 // If the object is defined in the current Module, we'll be giving
110 // it the preferred alignment. Otherwise, we have to assume that it
111 // may only have the minimum ABI alignment.
Duncan Sandsd3a38cc2011-11-29 18:26:38 +0000112 if (!GVar->isDeclaration() && !GVar->isWeakForLinker())
Eli Friedmanc4c2a022011-11-28 22:48:22 +0000113 Align = TD->getPreferredAlignment(GVar);
114 else
115 Align = TD->getABITypeAlignment(ObjectType);
116 }
Dan Gohman00407252009-08-11 15:50:03 +0000117 }
Chris Lattner173234a2008-06-02 01:18:21 +0000118 if (Align > 0)
119 KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
120 CountTrailingZeros_32(Align));
121 else
Jay Foad7a874dd2010-12-01 08:53:58 +0000122 KnownZero.clearAllBits();
123 KnownOne.clearAllBits();
Chris Lattner173234a2008-06-02 01:18:21 +0000124 return;
125 }
Dan Gohman307a7c42009-09-15 16:14:44 +0000126 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
127 // the bits of its aliasee.
128 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
129 if (GA->mayBeOverridden()) {
Jay Foad7a874dd2010-12-01 08:53:58 +0000130 KnownZero.clearAllBits(); KnownOne.clearAllBits();
Dan Gohman307a7c42009-09-15 16:14:44 +0000131 } else {
132 ComputeMaskedBits(GA->getAliasee(), Mask, KnownZero, KnownOne,
133 TD, Depth+1);
134 }
135 return;
136 }
Chris Lattnerb3f06732011-05-23 00:03:39 +0000137
138 if (Argument *A = dyn_cast<Argument>(V)) {
139 // Get alignment information off byval arguments if specified in the IR.
140 if (A->hasByValAttr())
141 if (unsigned Align = A->getParamAlignment())
142 KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
143 CountTrailingZeros_32(Align));
144 return;
145 }
Chris Lattner173234a2008-06-02 01:18:21 +0000146
Chris Lattnerb3f06732011-05-23 00:03:39 +0000147 // Start out not knowing anything.
148 KnownZero.clearAllBits(); KnownOne.clearAllBits();
Chris Lattner173234a2008-06-02 01:18:21 +0000149
Dan Gohman9004c8a2009-05-21 02:28:33 +0000150 if (Depth == MaxDepth || Mask == 0)
Chris Lattner173234a2008-06-02 01:18:21 +0000151 return; // Limit search depth.
152
Dan Gohmanca178902009-07-17 20:47:02 +0000153 Operator *I = dyn_cast<Operator>(V);
Chris Lattner173234a2008-06-02 01:18:21 +0000154 if (!I) return;
155
156 APInt KnownZero2(KnownZero), KnownOne2(KnownOne);
Dan Gohmanca178902009-07-17 20:47:02 +0000157 switch (I->getOpcode()) {
Chris Lattner173234a2008-06-02 01:18:21 +0000158 default: break;
159 case Instruction::And: {
160 // If either the LHS or the RHS are Zero, the result is zero.
161 ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1);
162 APInt Mask2(Mask & ~KnownZero);
163 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
164 Depth+1);
165 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
166 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
167
168 // Output known-1 bits are only known if set in both the LHS & RHS.
169 KnownOne &= KnownOne2;
170 // Output known-0 are known to be clear if zero in either the LHS | RHS.
171 KnownZero |= KnownZero2;
172 return;
173 }
174 case Instruction::Or: {
175 ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1);
176 APInt Mask2(Mask & ~KnownOne);
177 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
178 Depth+1);
179 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
180 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
181
182 // Output known-0 bits are only known if clear in both the LHS & RHS.
183 KnownZero &= KnownZero2;
184 // Output known-1 are known to be set if set in either the LHS | RHS.
185 KnownOne |= KnownOne2;
186 return;
187 }
188 case Instruction::Xor: {
189 ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1);
190 ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, TD,
191 Depth+1);
192 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
193 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
194
195 // Output known-0 bits are known if clear or set in both the LHS & RHS.
196 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
197 // Output known-1 are known to be set if set in only one of the LHS, RHS.
198 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
199 KnownZero = KnownZeroOut;
200 return;
201 }
202 case Instruction::Mul: {
203 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
204 ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero, KnownOne, TD,Depth+1);
205 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
206 Depth+1);
Duncan Sands32a43cc2011-10-27 19:16:21 +0000207 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
208 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
209
210 bool isKnownNegative = false;
211 bool isKnownNonNegative = false;
212 // If the multiplication is known not to overflow, compute the sign bit.
213 if (Mask.isNegative() &&
214 cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap()) {
215 Value *Op1 = I->getOperand(1), *Op2 = I->getOperand(0);
216 if (Op1 == Op2) {
217 // The product of a number with itself is non-negative.
218 isKnownNonNegative = true;
219 } else {
220 bool isKnownNonNegative1 = KnownZero.isNegative();
221 bool isKnownNonNegative2 = KnownZero2.isNegative();
222 bool isKnownNegative1 = KnownOne.isNegative();
223 bool isKnownNegative2 = KnownOne2.isNegative();
224 // The product of two numbers with the same sign is non-negative.
225 isKnownNonNegative = (isKnownNegative1 && isKnownNegative2) ||
226 (isKnownNonNegative1 && isKnownNonNegative2);
227 // The product of a negative number and a non-negative number is either
228 // negative or zero.
229 if (!isKnownNonNegative)
230 isKnownNegative = (isKnownNegative1 && isKnownNonNegative2 &&
231 isKnownNonZero(Op2, TD, Depth)) ||
232 (isKnownNegative2 && isKnownNonNegative1 &&
233 isKnownNonZero(Op1, TD, Depth));
234 }
235 }
236
Chris Lattner173234a2008-06-02 01:18:21 +0000237 // If low bits are zero in either operand, output low known-0 bits.
238 // Also compute a conserative estimate for high known-0 bits.
239 // More trickiness is possible, but this is sufficient for the
240 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +0000241 KnownOne.clearAllBits();
Chris Lattner173234a2008-06-02 01:18:21 +0000242 unsigned TrailZ = KnownZero.countTrailingOnes() +
243 KnownZero2.countTrailingOnes();
244 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
245 KnownZero2.countLeadingOnes(),
246 BitWidth) - BitWidth;
247
248 TrailZ = std::min(TrailZ, BitWidth);
249 LeadZ = std::min(LeadZ, BitWidth);
250 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
251 APInt::getHighBitsSet(BitWidth, LeadZ);
252 KnownZero &= Mask;
Duncan Sands32a43cc2011-10-27 19:16:21 +0000253
Duncan Sandsa8f5cd32011-11-23 16:26:47 +0000254 // Only make use of no-wrap flags if we failed to compute the sign bit
255 // directly. This matters if the multiplication always overflows, in
256 // which case we prefer to follow the result of the direct computation,
257 // though as the program is invoking undefined behaviour we can choose
258 // whatever we like here.
259 if (isKnownNonNegative && !KnownOne.isNegative())
Duncan Sands32a43cc2011-10-27 19:16:21 +0000260 KnownZero.setBit(BitWidth - 1);
Duncan Sandsa8f5cd32011-11-23 16:26:47 +0000261 else if (isKnownNegative && !KnownZero.isNegative())
Duncan Sands32a43cc2011-10-27 19:16:21 +0000262 KnownOne.setBit(BitWidth - 1);
263
Chris Lattner173234a2008-06-02 01:18:21 +0000264 return;
265 }
266 case Instruction::UDiv: {
267 // For the purposes of computing leading zeros we can conservatively
268 // treat a udiv as a logical right shift by the power of 2 known to
269 // be less than the denominator.
270 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
271 ComputeMaskedBits(I->getOperand(0),
272 AllOnes, KnownZero2, KnownOne2, TD, Depth+1);
273 unsigned LeadZ = KnownZero2.countLeadingOnes();
274
Jay Foad7a874dd2010-12-01 08:53:58 +0000275 KnownOne2.clearAllBits();
276 KnownZero2.clearAllBits();
Chris Lattner173234a2008-06-02 01:18:21 +0000277 ComputeMaskedBits(I->getOperand(1),
278 AllOnes, KnownZero2, KnownOne2, TD, Depth+1);
279 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
280 if (RHSUnknownLeadingOnes != BitWidth)
281 LeadZ = std::min(BitWidth,
282 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
283
284 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
285 return;
286 }
287 case Instruction::Select:
288 ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, TD, Depth+1);
289 ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, TD,
290 Depth+1);
291 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
292 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
293
294 // Only known if known in both the LHS and RHS.
295 KnownOne &= KnownOne2;
296 KnownZero &= KnownZero2;
297 return;
298 case Instruction::FPTrunc:
299 case Instruction::FPExt:
300 case Instruction::FPToUI:
301 case Instruction::FPToSI:
302 case Instruction::SIToFP:
303 case Instruction::UIToFP:
304 return; // Can't work with floating point.
305 case Instruction::PtrToInt:
306 case Instruction::IntToPtr:
307 // We can't handle these if we don't know the pointer size.
308 if (!TD) return;
309 // FALL THROUGH and handle them the same as zext/trunc.
310 case Instruction::ZExt:
311 case Instruction::Trunc: {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000312 Type *SrcTy = I->getOperand(0)->getType();
Chris Lattnerb9a4ddb2009-09-08 00:13:52 +0000313
314 unsigned SrcBitWidth;
Chris Lattner173234a2008-06-02 01:18:21 +0000315 // Note that we handle pointer operands here because of inttoptr/ptrtoint
316 // which fall through here.
Duncan Sands1df98592010-02-16 11:11:14 +0000317 if (SrcTy->isPointerTy())
Chris Lattnerb9a4ddb2009-09-08 00:13:52 +0000318 SrcBitWidth = TD->getTypeSizeInBits(SrcTy);
319 else
320 SrcBitWidth = SrcTy->getScalarSizeInBits();
321
Jay Foad40f8f622010-12-07 08:25:19 +0000322 APInt MaskIn = Mask.zextOrTrunc(SrcBitWidth);
323 KnownZero = KnownZero.zextOrTrunc(SrcBitWidth);
324 KnownOne = KnownOne.zextOrTrunc(SrcBitWidth);
Chris Lattner173234a2008-06-02 01:18:21 +0000325 ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, TD,
326 Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +0000327 KnownZero = KnownZero.zextOrTrunc(BitWidth);
328 KnownOne = KnownOne.zextOrTrunc(BitWidth);
Chris Lattner173234a2008-06-02 01:18:21 +0000329 // Any top bits are known to be zero.
330 if (BitWidth > SrcBitWidth)
331 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
332 return;
333 }
334 case Instruction::BitCast: {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000335 Type *SrcTy = I->getOperand(0)->getType();
Duncan Sands1df98592010-02-16 11:11:14 +0000336 if ((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
Chris Lattner0dabb0b2009-07-02 16:04:08 +0000337 // TODO: For now, not handling conversions like:
338 // (bitcast i64 %x to <2 x i32>)
Duncan Sands1df98592010-02-16 11:11:14 +0000339 !I->getType()->isVectorTy()) {
Chris Lattner173234a2008-06-02 01:18:21 +0000340 ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, TD,
341 Depth+1);
342 return;
343 }
344 break;
345 }
346 case Instruction::SExt: {
347 // Compute the bits in the result that are not present in the input.
Chris Lattnerb9a4ddb2009-09-08 00:13:52 +0000348 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
Chris Lattner173234a2008-06-02 01:18:21 +0000349
Jay Foad40f8f622010-12-07 08:25:19 +0000350 APInt MaskIn = Mask.trunc(SrcBitWidth);
351 KnownZero = KnownZero.trunc(SrcBitWidth);
352 KnownOne = KnownOne.trunc(SrcBitWidth);
Chris Lattner173234a2008-06-02 01:18:21 +0000353 ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, TD,
354 Depth+1);
355 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +0000356 KnownZero = KnownZero.zext(BitWidth);
357 KnownOne = KnownOne.zext(BitWidth);
Chris Lattner173234a2008-06-02 01:18:21 +0000358
359 // If the sign bit of the input is known set or clear, then we know the
360 // top bits of the result.
361 if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero
362 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
363 else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set
364 KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
365 return;
366 }
367 case Instruction::Shl:
368 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
369 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
370 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
371 APInt Mask2(Mask.lshr(ShiftAmt));
372 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD,
373 Depth+1);
374 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
375 KnownZero <<= ShiftAmt;
376 KnownOne <<= ShiftAmt;
377 KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0
378 return;
379 }
380 break;
381 case Instruction::LShr:
382 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
383 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
384 // Compute the new bits that are at the top now.
385 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
386
387 // Unsigned shift right.
388 APInt Mask2(Mask.shl(ShiftAmt));
389 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne, TD,
390 Depth+1);
Nick Lewyckyae3d8022009-11-23 03:29:18 +0000391 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattner173234a2008-06-02 01:18:21 +0000392 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
393 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
394 // high bits known zero.
395 KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
396 return;
397 }
398 break;
399 case Instruction::AShr:
400 // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
401 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
402 // Compute the new bits that are at the top now.
Chris Lattner43b40a42011-01-04 18:19:15 +0000403 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner173234a2008-06-02 01:18:21 +0000404
405 // Signed shift right.
406 APInt Mask2(Mask.shl(ShiftAmt));
407 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD,
408 Depth+1);
Nick Lewyckyae3d8022009-11-23 03:29:18 +0000409 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattner173234a2008-06-02 01:18:21 +0000410 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
411 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
412
413 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
414 if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero.
415 KnownZero |= HighBits;
416 else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one.
417 KnownOne |= HighBits;
418 return;
419 }
420 break;
421 case Instruction::Sub: {
422 if (ConstantInt *CLHS = dyn_cast<ConstantInt>(I->getOperand(0))) {
423 // We know that the top bits of C-X are clear if X contains less bits
424 // than C (i.e. no wrap-around can happen). For example, 20-X is
425 // positive if we can prove that X is >= 0 and < 16.
426 if (!CLHS->getValue().isNegative()) {
427 unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros();
428 // NLZ can't be BitWidth with no sign bit
429 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
430 ComputeMaskedBits(I->getOperand(1), MaskV, KnownZero2, KnownOne2,
431 TD, Depth+1);
432
433 // If all of the MaskV bits are known to be zero, then we know the
434 // output top bits are zero, because we now know that the output is
435 // from [0-C].
436 if ((KnownZero2 & MaskV) == MaskV) {
437 unsigned NLZ2 = CLHS->getValue().countLeadingZeros();
438 // Top bits known zero.
439 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
440 }
441 }
442 }
443 }
444 // fall through
445 case Instruction::Add: {
Nick Lewyckyae3d8022009-11-23 03:29:18 +0000446 // If one of the operands has trailing zeros, then the bits that the
Dan Gohman39250432009-05-24 18:02:35 +0000447 // other operand has in those bit positions will be preserved in the
448 // result. For an add, this works with either operand. For a subtract,
449 // this only works if the known zeros are in the right operand.
450 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
451 APInt Mask2 = APInt::getLowBitsSet(BitWidth,
452 BitWidth - Mask.countLeadingZeros());
453 ComputeMaskedBits(I->getOperand(0), Mask2, LHSKnownZero, LHSKnownOne, TD,
Chris Lattner173234a2008-06-02 01:18:21 +0000454 Depth+1);
Dan Gohman39250432009-05-24 18:02:35 +0000455 assert((LHSKnownZero & LHSKnownOne) == 0 &&
456 "Bits known to be one AND zero?");
457 unsigned LHSKnownZeroOut = LHSKnownZero.countTrailingOnes();
Chris Lattner173234a2008-06-02 01:18:21 +0000458
459 ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero2, KnownOne2, TD,
460 Depth+1);
461 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman39250432009-05-24 18:02:35 +0000462 unsigned RHSKnownZeroOut = KnownZero2.countTrailingOnes();
Chris Lattner173234a2008-06-02 01:18:21 +0000463
Dan Gohman39250432009-05-24 18:02:35 +0000464 // Determine which operand has more trailing zeros, and use that
465 // many bits from the other operand.
466 if (LHSKnownZeroOut > RHSKnownZeroOut) {
Dan Gohmanca178902009-07-17 20:47:02 +0000467 if (I->getOpcode() == Instruction::Add) {
Dan Gohman39250432009-05-24 18:02:35 +0000468 APInt Mask = APInt::getLowBitsSet(BitWidth, LHSKnownZeroOut);
469 KnownZero |= KnownZero2 & Mask;
470 KnownOne |= KnownOne2 & Mask;
471 } else {
472 // If the known zeros are in the left operand for a subtract,
473 // fall back to the minimum known zeros in both operands.
474 KnownZero |= APInt::getLowBitsSet(BitWidth,
475 std::min(LHSKnownZeroOut,
476 RHSKnownZeroOut));
477 }
478 } else if (RHSKnownZeroOut >= LHSKnownZeroOut) {
479 APInt Mask = APInt::getLowBitsSet(BitWidth, RHSKnownZeroOut);
480 KnownZero |= LHSKnownZero & Mask;
481 KnownOne |= LHSKnownOne & Mask;
482 }
Nick Lewyckyb69050a2011-03-11 09:00:19 +0000483
484 // Are we still trying to solve for the sign bit?
Benjamin Kramer14b2a592011-03-12 17:18:11 +0000485 if (Mask.isNegative() && !KnownZero.isNegative() && !KnownOne.isNegative()){
Nick Lewyckyb69050a2011-03-11 09:00:19 +0000486 OverflowingBinaryOperator *OBO = cast<OverflowingBinaryOperator>(I);
487 if (OBO->hasNoSignedWrap()) {
Benjamin Kramer14b2a592011-03-12 17:18:11 +0000488 if (I->getOpcode() == Instruction::Add) {
489 // Adding two positive numbers can't wrap into negative
490 if (LHSKnownZero.isNegative() && KnownZero2.isNegative())
491 KnownZero |= APInt::getSignBit(BitWidth);
492 // and adding two negative numbers can't wrap into positive.
493 else if (LHSKnownOne.isNegative() && KnownOne2.isNegative())
494 KnownOne |= APInt::getSignBit(BitWidth);
495 } else {
496 // Subtracting a negative number from a positive one can't wrap
497 if (LHSKnownZero.isNegative() && KnownOne2.isNegative())
498 KnownZero |= APInt::getSignBit(BitWidth);
499 // neither can subtracting a positive number from a negative one.
500 else if (LHSKnownOne.isNegative() && KnownZero2.isNegative())
501 KnownOne |= APInt::getSignBit(BitWidth);
502 }
Nick Lewyckyb69050a2011-03-11 09:00:19 +0000503 }
504 }
505
Chris Lattner173234a2008-06-02 01:18:21 +0000506 return;
507 }
508 case Instruction::SRem:
509 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Duncan Sandscfd54182010-01-29 06:18:37 +0000510 APInt RA = Rem->getValue().abs();
511 if (RA.isPowerOf2()) {
512 APInt LowBits = RA - 1;
Chris Lattner173234a2008-06-02 01:18:21 +0000513 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
514 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
515 Depth+1);
516
Duncan Sandscfd54182010-01-29 06:18:37 +0000517 // The low bits of the first operand are unchanged by the srem.
518 KnownZero = KnownZero2 & LowBits;
519 KnownOne = KnownOne2 & LowBits;
Chris Lattner173234a2008-06-02 01:18:21 +0000520
Duncan Sandscfd54182010-01-29 06:18:37 +0000521 // If the first operand is non-negative or has all low bits zero, then
522 // the upper bits are all zero.
523 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
524 KnownZero |= ~LowBits;
525
526 // If the first operand is negative and not all low bits are zero, then
527 // the upper bits are all one.
528 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
529 KnownOne |= ~LowBits;
530
531 KnownZero &= Mask;
532 KnownOne &= Mask;
Chris Lattner173234a2008-06-02 01:18:21 +0000533
Nick Lewyckyae3d8022009-11-23 03:29:18 +0000534 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattner173234a2008-06-02 01:18:21 +0000535 }
536 }
Nick Lewyckyc14bc772011-03-07 01:50:10 +0000537
538 // The sign bit is the LHS's sign bit, except when the result of the
539 // remainder is zero.
540 if (Mask.isNegative() && KnownZero.isNonNegative()) {
541 APInt Mask2 = APInt::getSignBit(BitWidth);
542 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
543 ComputeMaskedBits(I->getOperand(0), Mask2, LHSKnownZero, LHSKnownOne, TD,
544 Depth+1);
545 // If it's known zero, our sign bit is also zero.
546 if (LHSKnownZero.isNegative())
547 KnownZero |= LHSKnownZero;
548 }
549
Chris Lattner173234a2008-06-02 01:18:21 +0000550 break;
551 case Instruction::URem: {
552 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
553 APInt RA = Rem->getValue();
554 if (RA.isPowerOf2()) {
555 APInt LowBits = (RA - 1);
556 APInt Mask2 = LowBits & Mask;
557 KnownZero |= ~LowBits & Mask;
558 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD,
559 Depth+1);
Nick Lewyckyae3d8022009-11-23 03:29:18 +0000560 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattner173234a2008-06-02 01:18:21 +0000561 break;
562 }
563 }
564
565 // Since the result is less than or equal to either operand, any leading
566 // zero bits in either operand must also exist in the result.
567 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
568 ComputeMaskedBits(I->getOperand(0), AllOnes, KnownZero, KnownOne,
569 TD, Depth+1);
570 ComputeMaskedBits(I->getOperand(1), AllOnes, KnownZero2, KnownOne2,
571 TD, Depth+1);
572
Chris Lattner79abedb2009-01-20 18:22:57 +0000573 unsigned Leaders = std::max(KnownZero.countLeadingOnes(),
Chris Lattner173234a2008-06-02 01:18:21 +0000574 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +0000575 KnownOne.clearAllBits();
Chris Lattner173234a2008-06-02 01:18:21 +0000576 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
577 break;
578 }
579
Victor Hernandeza276c602009-10-17 01:18:07 +0000580 case Instruction::Alloca: {
Victor Hernandez7b929da2009-10-23 21:09:37 +0000581 AllocaInst *AI = cast<AllocaInst>(V);
Chris Lattner173234a2008-06-02 01:18:21 +0000582 unsigned Align = AI->getAlignment();
Victor Hernandeza276c602009-10-17 01:18:07 +0000583 if (Align == 0 && TD)
584 Align = TD->getABITypeAlignment(AI->getType()->getElementType());
Chris Lattner173234a2008-06-02 01:18:21 +0000585
586 if (Align > 0)
587 KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
588 CountTrailingZeros_32(Align));
589 break;
590 }
591 case Instruction::GetElementPtr: {
592 // Analyze all of the subscripts of this getelementptr instruction
593 // to determine if we can prove known low zero bits.
594 APInt LocalMask = APInt::getAllOnesValue(BitWidth);
595 APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0);
596 ComputeMaskedBits(I->getOperand(0), LocalMask,
597 LocalKnownZero, LocalKnownOne, TD, Depth+1);
598 unsigned TrailZ = LocalKnownZero.countTrailingOnes();
599
600 gep_type_iterator GTI = gep_type_begin(I);
601 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
602 Value *Index = I->getOperand(i);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000603 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Chris Lattner173234a2008-06-02 01:18:21 +0000604 // Handle struct member offset arithmetic.
605 if (!TD) return;
606 const StructLayout *SL = TD->getStructLayout(STy);
607 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
608 uint64_t Offset = SL->getElementOffset(Idx);
609 TrailZ = std::min(TrailZ,
610 CountTrailingZeros_64(Offset));
611 } else {
612 // Handle array index arithmetic.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000613 Type *IndexedTy = GTI.getIndexedType();
Chris Lattner173234a2008-06-02 01:18:21 +0000614 if (!IndexedTy->isSized()) return;
Dan Gohman6de29f82009-06-15 22:12:54 +0000615 unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits();
Duncan Sands777d2302009-05-09 07:06:46 +0000616 uint64_t TypeSize = TD ? TD->getTypeAllocSize(IndexedTy) : 1;
Chris Lattner173234a2008-06-02 01:18:21 +0000617 LocalMask = APInt::getAllOnesValue(GEPOpiBits);
618 LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0);
619 ComputeMaskedBits(Index, LocalMask,
620 LocalKnownZero, LocalKnownOne, TD, Depth+1);
621 TrailZ = std::min(TrailZ,
Chris Lattner79abedb2009-01-20 18:22:57 +0000622 unsigned(CountTrailingZeros_64(TypeSize) +
623 LocalKnownZero.countTrailingOnes()));
Chris Lattner173234a2008-06-02 01:18:21 +0000624 }
625 }
626
627 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) & Mask;
628 break;
629 }
630 case Instruction::PHI: {
631 PHINode *P = cast<PHINode>(I);
632 // Handle the case of a simple two-predecessor recurrence PHI.
633 // There's a lot more that could theoretically be done here, but
634 // this is sufficient to catch some interesting cases.
635 if (P->getNumIncomingValues() == 2) {
636 for (unsigned i = 0; i != 2; ++i) {
637 Value *L = P->getIncomingValue(i);
638 Value *R = P->getIncomingValue(!i);
Dan Gohmanca178902009-07-17 20:47:02 +0000639 Operator *LU = dyn_cast<Operator>(L);
Chris Lattner173234a2008-06-02 01:18:21 +0000640 if (!LU)
641 continue;
Dan Gohmanca178902009-07-17 20:47:02 +0000642 unsigned Opcode = LU->getOpcode();
Chris Lattner173234a2008-06-02 01:18:21 +0000643 // Check for operations that have the property that if
644 // both their operands have low zero bits, the result
645 // will have low zero bits.
646 if (Opcode == Instruction::Add ||
647 Opcode == Instruction::Sub ||
648 Opcode == Instruction::And ||
649 Opcode == Instruction::Or ||
650 Opcode == Instruction::Mul) {
651 Value *LL = LU->getOperand(0);
652 Value *LR = LU->getOperand(1);
653 // Find a recurrence.
654 if (LL == I)
655 L = LR;
656 else if (LR == I)
657 L = LL;
658 else
659 break;
660 // Ok, we have a PHI of the form L op= R. Check for low
661 // zero bits.
662 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
663 ComputeMaskedBits(R, Mask2, KnownZero2, KnownOne2, TD, Depth+1);
664 Mask2 = APInt::getLowBitsSet(BitWidth,
665 KnownZero2.countTrailingOnes());
David Greenec714f132008-10-27 23:24:03 +0000666
667 // We need to take the minimum number of known bits
668 APInt KnownZero3(KnownZero), KnownOne3(KnownOne);
669 ComputeMaskedBits(L, Mask2, KnownZero3, KnownOne3, TD, Depth+1);
670
Chris Lattner173234a2008-06-02 01:18:21 +0000671 KnownZero = Mask &
672 APInt::getLowBitsSet(BitWidth,
David Greenec714f132008-10-27 23:24:03 +0000673 std::min(KnownZero2.countTrailingOnes(),
674 KnownZero3.countTrailingOnes()));
Chris Lattner173234a2008-06-02 01:18:21 +0000675 break;
676 }
677 }
678 }
Dan Gohman9004c8a2009-05-21 02:28:33 +0000679
Nick Lewycky3b739d22011-02-10 23:54:10 +0000680 // Unreachable blocks may have zero-operand PHI nodes.
681 if (P->getNumIncomingValues() == 0)
682 return;
683
Dan Gohman9004c8a2009-05-21 02:28:33 +0000684 // Otherwise take the unions of the known bit sets of the operands,
685 // taking conservative care to avoid excessive recursion.
686 if (Depth < MaxDepth - 1 && !KnownZero && !KnownOne) {
Duncan Sands606199f2011-03-08 12:39:03 +0000687 // Skip if every incoming value references to ourself.
688 if (P->hasConstantValue() == P)
689 break;
690
Dan Gohman9004c8a2009-05-21 02:28:33 +0000691 KnownZero = APInt::getAllOnesValue(BitWidth);
692 KnownOne = APInt::getAllOnesValue(BitWidth);
693 for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) {
694 // Skip direct self references.
695 if (P->getIncomingValue(i) == P) continue;
696
697 KnownZero2 = APInt(BitWidth, 0);
698 KnownOne2 = APInt(BitWidth, 0);
699 // Recurse, but cap the recursion to one level, because we don't
700 // want to waste time spinning around in loops.
701 ComputeMaskedBits(P->getIncomingValue(i), KnownZero | KnownOne,
702 KnownZero2, KnownOne2, TD, MaxDepth-1);
703 KnownZero &= KnownZero2;
704 KnownOne &= KnownOne2;
705 // If all bits have been ruled out, there's no need to check
706 // more operands.
707 if (!KnownZero && !KnownOne)
708 break;
709 }
710 }
Chris Lattner173234a2008-06-02 01:18:21 +0000711 break;
712 }
713 case Instruction::Call:
714 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
715 switch (II->getIntrinsicID()) {
716 default: break;
Chris Lattner173234a2008-06-02 01:18:21 +0000717 case Intrinsic::ctlz:
718 case Intrinsic::cttz: {
719 unsigned LowBits = Log2_32(BitWidth)+1;
Benjamin Kramer009da052011-12-24 17:31:46 +0000720 // If this call is undefined for 0, the result will be less than 2^n.
721 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
722 LowBits -= 1;
723 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
724 break;
725 }
726 case Intrinsic::ctpop: {
727 unsigned LowBits = Log2_32(BitWidth)+1;
Chris Lattner173234a2008-06-02 01:18:21 +0000728 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
729 break;
730 }
Chad Rosier62660312011-05-26 23:13:19 +0000731 case Intrinsic::x86_sse42_crc32_64_8:
732 case Intrinsic::x86_sse42_crc32_64_64:
Evan Chengcb559c12011-05-22 18:25:30 +0000733 KnownZero = APInt::getHighBitsSet(64, 32);
734 break;
Chris Lattner173234a2008-06-02 01:18:21 +0000735 }
736 }
737 break;
738 }
739}
740
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000741/// ComputeSignBit - Determine whether the sign bit is known to be zero or
742/// one. Convenience wrapper around ComputeMaskedBits.
743void llvm::ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
744 const TargetData *TD, unsigned Depth) {
745 unsigned BitWidth = getBitWidth(V->getType(), TD);
746 if (!BitWidth) {
747 KnownZero = false;
748 KnownOne = false;
749 return;
750 }
751 APInt ZeroBits(BitWidth, 0);
752 APInt OneBits(BitWidth, 0);
753 ComputeMaskedBits(V, APInt::getSignBit(BitWidth), ZeroBits, OneBits, TD,
754 Depth);
755 KnownOne = OneBits[BitWidth - 1];
756 KnownZero = ZeroBits[BitWidth - 1];
757}
758
759/// isPowerOfTwo - Return true if the given value is known to have exactly one
760/// bit set when defined. For vectors return true if every element is known to
761/// be a power of two when defined. Supports values with integer or pointer
762/// types and vectors of integers.
Duncan Sandsdd3149d2011-10-26 20:55:21 +0000763bool llvm::isPowerOfTwo(Value *V, const TargetData *TD, bool OrZero,
764 unsigned Depth) {
765 if (Constant *C = dyn_cast<Constant>(V)) {
766 if (C->isNullValue())
767 return OrZero;
768 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
769 return CI->getValue().isPowerOf2();
770 // TODO: Handle vector constants.
771 }
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000772
773 // 1 << X is clearly a power of two if the one is not shifted off the end. If
774 // it is shifted off the end then the result is undefined.
775 if (match(V, m_Shl(m_One(), m_Value())))
776 return true;
777
778 // (signbit) >>l X is clearly a power of two if the one is not shifted off the
779 // bottom. If it is shifted off the bottom then the result is undefined.
Duncan Sands93c78022011-02-01 08:50:33 +0000780 if (match(V, m_LShr(m_SignBit(), m_Value())))
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000781 return true;
782
783 // The remaining tests are all recursive, so bail out if we hit the limit.
784 if (Depth++ == MaxDepth)
785 return false;
786
Duncan Sands4604fc72011-10-28 18:30:05 +0000787 Value *X = 0, *Y = 0;
788 // A shift of a power of two is a power of two or zero.
789 if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) ||
790 match(V, m_Shr(m_Value(X), m_Value()))))
791 return isPowerOfTwo(X, TD, /*OrZero*/true, Depth);
792
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000793 if (ZExtInst *ZI = dyn_cast<ZExtInst>(V))
Duncan Sandsdd3149d2011-10-26 20:55:21 +0000794 return isPowerOfTwo(ZI->getOperand(0), TD, OrZero, Depth);
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000795
796 if (SelectInst *SI = dyn_cast<SelectInst>(V))
Duncan Sandsdd3149d2011-10-26 20:55:21 +0000797 return isPowerOfTwo(SI->getTrueValue(), TD, OrZero, Depth) &&
798 isPowerOfTwo(SI->getFalseValue(), TD, OrZero, Depth);
799
Duncan Sandsdd3149d2011-10-26 20:55:21 +0000800 if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) {
801 // A power of two and'd with anything is a power of two or zero.
802 if (isPowerOfTwo(X, TD, /*OrZero*/true, Depth) ||
803 isPowerOfTwo(Y, TD, /*OrZero*/true, Depth))
804 return true;
805 // X & (-X) is always a power of two or zero.
806 if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X))))
807 return true;
808 return false;
809 }
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000810
Nick Lewycky3dfd9872011-02-28 08:02:21 +0000811 // An exact divide or right shift can only shift off zero bits, so the result
Nick Lewycky1f7bc702011-03-21 21:40:32 +0000812 // is a power of two only if the first operand is a power of two and not
813 // copying a sign bit (sdiv int_min, 2).
814 if (match(V, m_LShr(m_Value(), m_Value())) ||
815 match(V, m_UDiv(m_Value(), m_Value()))) {
Eli Friedman6bdd2612011-04-02 22:11:56 +0000816 PossiblyExactOperator *PEO = cast<PossiblyExactOperator>(V);
817 if (PEO->isExact())
Duncan Sandsdd3149d2011-10-26 20:55:21 +0000818 return isPowerOfTwo(PEO->getOperand(0), TD, OrZero, Depth);
Nick Lewycky3dfd9872011-02-28 08:02:21 +0000819 }
820
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000821 return false;
822}
823
824/// isKnownNonZero - Return true if the given value is known to be non-zero
825/// when defined. For vectors return true if every element is known to be
826/// non-zero when defined. Supports values with integer or pointer type and
827/// vectors of integers.
828bool llvm::isKnownNonZero(Value *V, const TargetData *TD, unsigned Depth) {
829 if (Constant *C = dyn_cast<Constant>(V)) {
830 if (C->isNullValue())
831 return false;
832 if (isa<ConstantInt>(C))
833 // Must be non-zero due to null test above.
834 return true;
835 // TODO: Handle vectors
836 return false;
837 }
838
839 // The remaining tests are all recursive, so bail out if we hit the limit.
Duncan Sands32a43cc2011-10-27 19:16:21 +0000840 if (Depth++ >= MaxDepth)
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000841 return false;
842
843 unsigned BitWidth = getBitWidth(V->getType(), TD);
844
845 // X | Y != 0 if X != 0 or Y != 0.
846 Value *X = 0, *Y = 0;
847 if (match(V, m_Or(m_Value(X), m_Value(Y))))
848 return isKnownNonZero(X, TD, Depth) || isKnownNonZero(Y, TD, Depth);
849
850 // ext X != 0 if X != 0.
851 if (isa<SExtInst>(V) || isa<ZExtInst>(V))
852 return isKnownNonZero(cast<Instruction>(V)->getOperand(0), TD, Depth);
853
Duncan Sands91367822011-01-29 13:27:00 +0000854 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000855 // if the lowest bit is shifted off the end.
856 if (BitWidth && match(V, m_Shl(m_Value(X), m_Value(Y)))) {
Nick Lewycky3dfd9872011-02-28 08:02:21 +0000857 // shl nuw can't remove any non-zero bits.
Duncan Sands32a43cc2011-10-27 19:16:21 +0000858 OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
Nick Lewycky3dfd9872011-02-28 08:02:21 +0000859 if (BO->hasNoUnsignedWrap())
860 return isKnownNonZero(X, TD, Depth);
861
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000862 APInt KnownZero(BitWidth, 0);
863 APInt KnownOne(BitWidth, 0);
Duncan Sands91367822011-01-29 13:27:00 +0000864 ComputeMaskedBits(X, APInt(BitWidth, 1), KnownZero, KnownOne, TD, Depth);
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000865 if (KnownOne[0])
866 return true;
867 }
Duncan Sands91367822011-01-29 13:27:00 +0000868 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000869 // defined if the sign bit is shifted off the end.
870 else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) {
Nick Lewycky3dfd9872011-02-28 08:02:21 +0000871 // shr exact can only shift out zero bits.
Duncan Sands32a43cc2011-10-27 19:16:21 +0000872 PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V);
Nick Lewycky3dfd9872011-02-28 08:02:21 +0000873 if (BO->isExact())
874 return isKnownNonZero(X, TD, Depth);
875
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000876 bool XKnownNonNegative, XKnownNegative;
877 ComputeSignBit(X, XKnownNonNegative, XKnownNegative, TD, Depth);
878 if (XKnownNegative)
879 return true;
880 }
Nick Lewycky3dfd9872011-02-28 08:02:21 +0000881 // div exact can only produce a zero if the dividend is zero.
882 else if (match(V, m_IDiv(m_Value(X), m_Value()))) {
Duncan Sands32a43cc2011-10-27 19:16:21 +0000883 PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V);
Nick Lewycky3dfd9872011-02-28 08:02:21 +0000884 if (BO->isExact())
885 return isKnownNonZero(X, TD, Depth);
886 }
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000887 // X + Y.
888 else if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
889 bool XKnownNonNegative, XKnownNegative;
890 bool YKnownNonNegative, YKnownNegative;
891 ComputeSignBit(X, XKnownNonNegative, XKnownNegative, TD, Depth);
892 ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, TD, Depth);
893
894 // If X and Y are both non-negative (as signed values) then their sum is not
Duncan Sands227fba12011-01-25 15:14:15 +0000895 // zero unless both X and Y are zero.
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000896 if (XKnownNonNegative && YKnownNonNegative)
Duncan Sands227fba12011-01-25 15:14:15 +0000897 if (isKnownNonZero(X, TD, Depth) || isKnownNonZero(Y, TD, Depth))
898 return true;
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000899
900 // If X and Y are both negative (as signed values) then their sum is not
901 // zero unless both X and Y equal INT_MIN.
902 if (BitWidth && XKnownNegative && YKnownNegative) {
903 APInt KnownZero(BitWidth, 0);
904 APInt KnownOne(BitWidth, 0);
905 APInt Mask = APInt::getSignedMaxValue(BitWidth);
906 // The sign bit of X is set. If some other bit is set then X is not equal
907 // to INT_MIN.
908 ComputeMaskedBits(X, Mask, KnownZero, KnownOne, TD, Depth);
909 if ((KnownOne & Mask) != 0)
910 return true;
911 // The sign bit of Y is set. If some other bit is set then Y is not equal
912 // to INT_MIN.
913 ComputeMaskedBits(Y, Mask, KnownZero, KnownOne, TD, Depth);
914 if ((KnownOne & Mask) != 0)
915 return true;
916 }
917
918 // The sum of a non-negative number and a power of two is not zero.
Duncan Sandsdd3149d2011-10-26 20:55:21 +0000919 if (XKnownNonNegative && isPowerOfTwo(Y, TD, /*OrZero*/false, Depth))
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000920 return true;
Duncan Sandsdd3149d2011-10-26 20:55:21 +0000921 if (YKnownNonNegative && isPowerOfTwo(X, TD, /*OrZero*/false, Depth))
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000922 return true;
923 }
Duncan Sands32a43cc2011-10-27 19:16:21 +0000924 // X * Y.
925 else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) {
926 OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
927 // If X and Y are non-zero then so is X * Y as long as the multiplication
928 // does not overflow.
929 if ((BO->hasNoSignedWrap() || BO->hasNoUnsignedWrap()) &&
930 isKnownNonZero(X, TD, Depth) && isKnownNonZero(Y, TD, Depth))
931 return true;
932 }
Duncan Sandsd70d1a52011-01-25 09:38:29 +0000933 // (C ? X : Y) != 0 if X != 0 and Y != 0.
934 else if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
935 if (isKnownNonZero(SI->getTrueValue(), TD, Depth) &&
936 isKnownNonZero(SI->getFalseValue(), TD, Depth))
937 return true;
938 }
939
940 if (!BitWidth) return false;
941 APInt KnownZero(BitWidth, 0);
942 APInt KnownOne(BitWidth, 0);
943 ComputeMaskedBits(V, APInt::getAllOnesValue(BitWidth), KnownZero, KnownOne,
944 TD, Depth);
945 return KnownOne != 0;
946}
947
Chris Lattner173234a2008-06-02 01:18:21 +0000948/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
949/// this predicate to simplify operations downstream. Mask is known to be zero
950/// for bits that V cannot have.
Chris Lattnercf5128e2009-09-08 00:06:16 +0000951///
952/// This function is defined on values with integer type, values with pointer
953/// type (but only if TD is non-null), and vectors of integers. In the case
954/// where V is a vector, the mask, known zero, and known one values are the
955/// same width as the vector element, and the bit is set only if it is true
956/// for all of the elements in the vector.
Chris Lattner173234a2008-06-02 01:18:21 +0000957bool llvm::MaskedValueIsZero(Value *V, const APInt &Mask,
Dan Gohman846a2f22009-08-27 17:51:25 +0000958 const TargetData *TD, unsigned Depth) {
Chris Lattner173234a2008-06-02 01:18:21 +0000959 APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0);
960 ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth);
961 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
962 return (KnownZero & Mask) == Mask;
963}
964
965
966
967/// ComputeNumSignBits - Return the number of times the sign bit of the
968/// register is replicated into the other bits. We know that at least 1 bit
969/// is always equal to the sign bit (itself), but other cases can give us
970/// information. For example, immediately after an "ashr X, 2", we know that
971/// the top 3 bits are all equal to each other, so we return 3.
972///
973/// 'Op' must have a scalar integer type.
974///
Dan Gohman846a2f22009-08-27 17:51:25 +0000975unsigned llvm::ComputeNumSignBits(Value *V, const TargetData *TD,
976 unsigned Depth) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000977 assert((TD || V->getType()->isIntOrIntVectorTy()) &&
Dan Gohmanbd5ce522009-06-22 22:02:32 +0000978 "ComputeNumSignBits requires a TargetData object to operate "
979 "on non-integer values!");
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000980 Type *Ty = V->getType();
Dan Gohmanbd5ce522009-06-22 22:02:32 +0000981 unsigned TyBits = TD ? TD->getTypeSizeInBits(V->getType()->getScalarType()) :
982 Ty->getScalarSizeInBits();
Chris Lattner173234a2008-06-02 01:18:21 +0000983 unsigned Tmp, Tmp2;
984 unsigned FirstAnswer = 1;
985
Chris Lattnerd82e5112008-06-02 18:39:07 +0000986 // Note that ConstantInt is handled by the general ComputeMaskedBits case
987 // below.
988
Chris Lattner173234a2008-06-02 01:18:21 +0000989 if (Depth == 6)
990 return 1; // Limit search depth.
991
Dan Gohmanca178902009-07-17 20:47:02 +0000992 Operator *U = dyn_cast<Operator>(V);
993 switch (Operator::getOpcode(V)) {
Chris Lattner173234a2008-06-02 01:18:21 +0000994 default: break;
995 case Instruction::SExt:
Mon P Wang69a00802009-12-02 04:59:58 +0000996 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
Chris Lattner173234a2008-06-02 01:18:21 +0000997 return ComputeNumSignBits(U->getOperand(0), TD, Depth+1) + Tmp;
998
999 case Instruction::AShr:
1000 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
1001 // ashr X, C -> adds C sign bits.
1002 if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) {
1003 Tmp += C->getZExtValue();
1004 if (Tmp > TyBits) Tmp = TyBits;
1005 }
Nate Begeman9a3dc552010-12-17 23:12:19 +00001006 // vector ashr X, <C, C, C, C> -> adds C sign bits
1007 if (ConstantVector *C = dyn_cast<ConstantVector>(U->getOperand(1))) {
1008 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
1009 Tmp += CI->getZExtValue();
1010 if (Tmp > TyBits) Tmp = TyBits;
1011 }
1012 }
Chris Lattner173234a2008-06-02 01:18:21 +00001013 return Tmp;
1014 case Instruction::Shl:
1015 if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) {
1016 // shl destroys sign bits.
1017 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
1018 if (C->getZExtValue() >= TyBits || // Bad shift.
1019 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
1020 return Tmp - C->getZExtValue();
1021 }
1022 break;
1023 case Instruction::And:
1024 case Instruction::Or:
1025 case Instruction::Xor: // NOT is handled here.
1026 // Logical binary ops preserve the number of sign bits at the worst.
1027 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
1028 if (Tmp != 1) {
1029 Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
1030 FirstAnswer = std::min(Tmp, Tmp2);
1031 // We computed what we know about the sign bits as our first
1032 // answer. Now proceed to the generic code that uses
1033 // ComputeMaskedBits, and pick whichever answer is better.
1034 }
1035 break;
1036
1037 case Instruction::Select:
1038 Tmp = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
1039 if (Tmp == 1) return 1; // Early out.
1040 Tmp2 = ComputeNumSignBits(U->getOperand(2), TD, Depth+1);
1041 return std::min(Tmp, Tmp2);
1042
1043 case Instruction::Add:
1044 // Add can have at most one carry bit. Thus we know that the output
1045 // is, at worst, one more bit than the inputs.
1046 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
1047 if (Tmp == 1) return 1; // Early out.
1048
1049 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00001050 if (ConstantInt *CRHS = dyn_cast<ConstantInt>(U->getOperand(1)))
Chris Lattner173234a2008-06-02 01:18:21 +00001051 if (CRHS->isAllOnesValue()) {
1052 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
1053 APInt Mask = APInt::getAllOnesValue(TyBits);
1054 ComputeMaskedBits(U->getOperand(0), Mask, KnownZero, KnownOne, TD,
1055 Depth+1);
1056
1057 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1058 // sign bits set.
1059 if ((KnownZero | APInt(TyBits, 1)) == Mask)
1060 return TyBits;
1061
1062 // If we are subtracting one from a positive number, there is no carry
1063 // out of the result.
1064 if (KnownZero.isNegative())
1065 return Tmp;
1066 }
1067
1068 Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
1069 if (Tmp2 == 1) return 1;
Chris Lattner8d10f9d2010-01-07 23:44:37 +00001070 return std::min(Tmp, Tmp2)-1;
Chris Lattner173234a2008-06-02 01:18:21 +00001071
1072 case Instruction::Sub:
1073 Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
1074 if (Tmp2 == 1) return 1;
1075
1076 // Handle NEG.
1077 if (ConstantInt *CLHS = dyn_cast<ConstantInt>(U->getOperand(0)))
1078 if (CLHS->isNullValue()) {
1079 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
1080 APInt Mask = APInt::getAllOnesValue(TyBits);
1081 ComputeMaskedBits(U->getOperand(1), Mask, KnownZero, KnownOne,
1082 TD, Depth+1);
1083 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1084 // sign bits set.
1085 if ((KnownZero | APInt(TyBits, 1)) == Mask)
1086 return TyBits;
1087
1088 // If the input is known to be positive (the sign bit is known clear),
1089 // the output of the NEG has the same number of sign bits as the input.
1090 if (KnownZero.isNegative())
1091 return Tmp2;
1092
1093 // Otherwise, we treat this like a SUB.
1094 }
1095
1096 // Sub can have at most one carry bit. Thus we know that the output
1097 // is, at worst, one more bit than the inputs.
1098 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
1099 if (Tmp == 1) return 1; // Early out.
Chris Lattner8d10f9d2010-01-07 23:44:37 +00001100 return std::min(Tmp, Tmp2)-1;
1101
1102 case Instruction::PHI: {
1103 PHINode *PN = cast<PHINode>(U);
1104 // Don't analyze large in-degree PHIs.
1105 if (PN->getNumIncomingValues() > 4) break;
1106
1107 // Take the minimum of all incoming values. This can't infinitely loop
1108 // because of our depth threshold.
1109 Tmp = ComputeNumSignBits(PN->getIncomingValue(0), TD, Depth+1);
1110 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
1111 if (Tmp == 1) return Tmp;
1112 Tmp = std::min(Tmp,
Evan Cheng0af20d82010-03-13 02:20:29 +00001113 ComputeNumSignBits(PN->getIncomingValue(i), TD, Depth+1));
Chris Lattner8d10f9d2010-01-07 23:44:37 +00001114 }
1115 return Tmp;
1116 }
1117
Chris Lattner173234a2008-06-02 01:18:21 +00001118 case Instruction::Trunc:
1119 // FIXME: it's tricky to do anything useful for this, but it is an important
1120 // case for targets like X86.
1121 break;
1122 }
1123
1124 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1125 // use this information.
1126 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
1127 APInt Mask = APInt::getAllOnesValue(TyBits);
1128 ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth);
1129
1130 if (KnownZero.isNegative()) { // sign bit is 0
1131 Mask = KnownZero;
1132 } else if (KnownOne.isNegative()) { // sign bit is 1;
1133 Mask = KnownOne;
1134 } else {
1135 // Nothing known.
1136 return FirstAnswer;
1137 }
1138
1139 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1140 // the number of identical bits in the top of the input value.
1141 Mask = ~Mask;
1142 Mask <<= Mask.getBitWidth()-TyBits;
1143 // Return # leading zeros. We use 'min' here in case Val was zero before
1144 // shifting. We don't want to return '64' as for an i32 "0".
1145 return std::max(FirstAnswer, std::min(TyBits, Mask.countLeadingZeros()));
1146}
Chris Lattner833f25d2008-06-02 01:29:46 +00001147
Victor Hernandez2b6705f2009-11-10 08:28:35 +00001148/// ComputeMultiple - This function computes the integer multiple of Base that
1149/// equals V. If successful, it returns true and returns the multiple in
Dan Gohman3dbb9e62009-11-18 00:58:27 +00001150/// Multiple. If unsuccessful, it returns false. It looks
Victor Hernandez2b6705f2009-11-10 08:28:35 +00001151/// through SExt instructions only if LookThroughSExt is true.
1152bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
Dan Gohman3dbb9e62009-11-18 00:58:27 +00001153 bool LookThroughSExt, unsigned Depth) {
Victor Hernandez2b6705f2009-11-10 08:28:35 +00001154 const unsigned MaxDepth = 6;
1155
Dan Gohman3dbb9e62009-11-18 00:58:27 +00001156 assert(V && "No Value?");
Victor Hernandez2b6705f2009-11-10 08:28:35 +00001157 assert(Depth <= MaxDepth && "Limit Search Depth");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001158 assert(V->getType()->isIntegerTy() && "Not integer or pointer type!");
Victor Hernandez2b6705f2009-11-10 08:28:35 +00001159
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001160 Type *T = V->getType();
Victor Hernandez2b6705f2009-11-10 08:28:35 +00001161
Dan Gohman3dbb9e62009-11-18 00:58:27 +00001162 ConstantInt *CI = dyn_cast<ConstantInt>(V);
Victor Hernandez2b6705f2009-11-10 08:28:35 +00001163
1164 if (Base == 0)
1165 return false;
1166
1167 if (Base == 1) {
1168 Multiple = V;
1169 return true;
1170 }
1171
1172 ConstantExpr *CO = dyn_cast<ConstantExpr>(V);
1173 Constant *BaseVal = ConstantInt::get(T, Base);
1174 if (CO && CO == BaseVal) {
1175 // Multiple is 1.
1176 Multiple = ConstantInt::get(T, 1);
1177 return true;
1178 }
1179
1180 if (CI && CI->getZExtValue() % Base == 0) {
1181 Multiple = ConstantInt::get(T, CI->getZExtValue() / Base);
1182 return true;
1183 }
1184
1185 if (Depth == MaxDepth) return false; // Limit search depth.
1186
1187 Operator *I = dyn_cast<Operator>(V);
1188 if (!I) return false;
1189
1190 switch (I->getOpcode()) {
1191 default: break;
Chris Lattner11fe7262009-11-26 01:50:12 +00001192 case Instruction::SExt:
Victor Hernandez2b6705f2009-11-10 08:28:35 +00001193 if (!LookThroughSExt) return false;
1194 // otherwise fall through to ZExt
Chris Lattner11fe7262009-11-26 01:50:12 +00001195 case Instruction::ZExt:
Dan Gohman3dbb9e62009-11-18 00:58:27 +00001196 return ComputeMultiple(I->getOperand(0), Base, Multiple,
1197 LookThroughSExt, Depth+1);
Victor Hernandez2b6705f2009-11-10 08:28:35 +00001198 case Instruction::Shl:
1199 case Instruction::Mul: {
1200 Value *Op0 = I->getOperand(0);
1201 Value *Op1 = I->getOperand(1);
1202
1203 if (I->getOpcode() == Instruction::Shl) {
1204 ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1);
1205 if (!Op1CI) return false;
1206 // Turn Op0 << Op1 into Op0 * 2^Op1
1207 APInt Op1Int = Op1CI->getValue();
1208 uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
Jay Foada99793c2010-11-30 09:02:01 +00001209 APInt API(Op1Int.getBitWidth(), 0);
Jay Foad7a874dd2010-12-01 08:53:58 +00001210 API.setBit(BitToSet);
Jay Foada99793c2010-11-30 09:02:01 +00001211 Op1 = ConstantInt::get(V->getContext(), API);
Victor Hernandez2b6705f2009-11-10 08:28:35 +00001212 }
1213
1214 Value *Mul0 = NULL;
Chris Lattnere9711312010-09-05 17:20:46 +00001215 if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) {
1216 if (Constant *Op1C = dyn_cast<Constant>(Op1))
1217 if (Constant *MulC = dyn_cast<Constant>(Mul0)) {
1218 if (Op1C->getType()->getPrimitiveSizeInBits() <
1219 MulC->getType()->getPrimitiveSizeInBits())
1220 Op1C = ConstantExpr::getZExt(Op1C, MulC->getType());
1221 if (Op1C->getType()->getPrimitiveSizeInBits() >
1222 MulC->getType()->getPrimitiveSizeInBits())
1223 MulC = ConstantExpr::getZExt(MulC, Op1C->getType());
1224
1225 // V == Base * (Mul0 * Op1), so return (Mul0 * Op1)
1226 Multiple = ConstantExpr::getMul(MulC, Op1C);
1227 return true;
1228 }
Victor Hernandez2b6705f2009-11-10 08:28:35 +00001229
1230 if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0))
1231 if (Mul0CI->getValue() == 1) {
1232 // V == Base * Op1, so return Op1
1233 Multiple = Op1;
1234 return true;
1235 }
1236 }
1237
Chris Lattnere9711312010-09-05 17:20:46 +00001238 Value *Mul1 = NULL;
1239 if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) {
1240 if (Constant *Op0C = dyn_cast<Constant>(Op0))
1241 if (Constant *MulC = dyn_cast<Constant>(Mul1)) {
1242 if (Op0C->getType()->getPrimitiveSizeInBits() <
1243 MulC->getType()->getPrimitiveSizeInBits())
1244 Op0C = ConstantExpr::getZExt(Op0C, MulC->getType());
1245 if (Op0C->getType()->getPrimitiveSizeInBits() >
1246 MulC->getType()->getPrimitiveSizeInBits())
1247 MulC = ConstantExpr::getZExt(MulC, Op0C->getType());
1248
1249 // V == Base * (Mul1 * Op0), so return (Mul1 * Op0)
1250 Multiple = ConstantExpr::getMul(MulC, Op0C);
1251 return true;
1252 }
Victor Hernandez2b6705f2009-11-10 08:28:35 +00001253
1254 if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1))
1255 if (Mul1CI->getValue() == 1) {
1256 // V == Base * Op0, so return Op0
1257 Multiple = Op0;
1258 return true;
1259 }
1260 }
Victor Hernandez2b6705f2009-11-10 08:28:35 +00001261 }
1262 }
1263
1264 // We could not determine if V is a multiple of Base.
1265 return false;
1266}
1267
Chris Lattner833f25d2008-06-02 01:29:46 +00001268/// CannotBeNegativeZero - Return true if we can prove that the specified FP
1269/// value is never equal to -0.0.
1270///
1271/// NOTE: this function will need to be revisited when we support non-default
1272/// rounding modes!
1273///
1274bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) {
1275 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
1276 return !CFP->getValueAPF().isNegZero();
1277
1278 if (Depth == 6)
1279 return 1; // Limit search depth.
1280
Dan Gohmanca178902009-07-17 20:47:02 +00001281 const Operator *I = dyn_cast<Operator>(V);
Chris Lattner833f25d2008-06-02 01:29:46 +00001282 if (I == 0) return false;
1283
1284 // (add x, 0.0) is guaranteed to return +0.0, not -0.0.
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001285 if (I->getOpcode() == Instruction::FAdd &&
Chris Lattner833f25d2008-06-02 01:29:46 +00001286 isa<ConstantFP>(I->getOperand(1)) &&
1287 cast<ConstantFP>(I->getOperand(1))->isNullValue())
1288 return true;
1289
1290 // sitofp and uitofp turn into +0.0 for zero.
1291 if (isa<SIToFPInst>(I) || isa<UIToFPInst>(I))
1292 return true;
1293
1294 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
1295 // sqrt(-0.0) = -0.0, no other negative results are possible.
1296 if (II->getIntrinsicID() == Intrinsic::sqrt)
Gabor Greif71339c92010-06-23 23:38:07 +00001297 return CannotBeNegativeZero(II->getArgOperand(0), Depth+1);
Chris Lattner833f25d2008-06-02 01:29:46 +00001298
1299 if (const CallInst *CI = dyn_cast<CallInst>(I))
1300 if (const Function *F = CI->getCalledFunction()) {
1301 if (F->isDeclaration()) {
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001302 // abs(x) != -0.0
1303 if (F->getName() == "abs") return true;
Dale Johannesen9d061752009-09-25 20:54:50 +00001304 // fabs[lf](x) != -0.0
1305 if (F->getName() == "fabs") return true;
1306 if (F->getName() == "fabsf") return true;
1307 if (F->getName() == "fabsl") return true;
1308 if (F->getName() == "sqrt" || F->getName() == "sqrtf" ||
1309 F->getName() == "sqrtl")
Gabor Greif71339c92010-06-23 23:38:07 +00001310 return CannotBeNegativeZero(CI->getArgOperand(0), Depth+1);
Chris Lattner833f25d2008-06-02 01:29:46 +00001311 }
1312 }
1313
1314 return false;
1315}
1316
Chris Lattnerbb897102010-12-26 20:15:01 +00001317/// isBytewiseValue - If the specified value can be set by repeating the same
1318/// byte in memory, return the i8 value that it is represented with. This is
1319/// true for all i8 values obviously, but is also true for i32 0, i32 -1,
1320/// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated
1321/// byte store (e.g. i16 0x1234), return null.
1322Value *llvm::isBytewiseValue(Value *V) {
1323 // All byte-wide stores are splatable, even of arbitrary variables.
1324 if (V->getType()->isIntegerTy(8)) return V;
Chris Lattner41bfbb02011-02-19 19:35:49 +00001325
1326 // Handle 'null' ConstantArrayZero etc.
1327 if (Constant *C = dyn_cast<Constant>(V))
1328 if (C->isNullValue())
1329 return Constant::getNullValue(Type::getInt8Ty(V->getContext()));
Chris Lattnerbb897102010-12-26 20:15:01 +00001330
1331 // Constant float and double values can be handled as integer values if the
1332 // corresponding integer value is "byteable". An important case is 0.0.
1333 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
1334 if (CFP->getType()->isFloatTy())
1335 V = ConstantExpr::getBitCast(CFP, Type::getInt32Ty(V->getContext()));
1336 if (CFP->getType()->isDoubleTy())
1337 V = ConstantExpr::getBitCast(CFP, Type::getInt64Ty(V->getContext()));
1338 // Don't handle long double formats, which have strange constraints.
1339 }
1340
1341 // We can handle constant integers that are power of two in size and a
1342 // multiple of 8 bits.
1343 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
1344 unsigned Width = CI->getBitWidth();
1345 if (isPowerOf2_32(Width) && Width > 8) {
1346 // We can handle this value if the recursive binary decomposition is the
1347 // same at all levels.
1348 APInt Val = CI->getValue();
1349 APInt Val2;
1350 while (Val.getBitWidth() != 8) {
1351 unsigned NextWidth = Val.getBitWidth()/2;
1352 Val2 = Val.lshr(NextWidth);
1353 Val2 = Val2.trunc(Val.getBitWidth()/2);
1354 Val = Val.trunc(Val.getBitWidth()/2);
1355
1356 // If the top/bottom halves aren't the same, reject it.
1357 if (Val != Val2)
1358 return 0;
1359 }
1360 return ConstantInt::get(V->getContext(), Val);
1361 }
1362 }
1363
1364 // A ConstantArray is splatable if all its members are equal and also
1365 // splatable.
1366 if (ConstantArray *CA = dyn_cast<ConstantArray>(V)) {
1367 if (CA->getNumOperands() == 0)
1368 return 0;
1369
1370 Value *Val = isBytewiseValue(CA->getOperand(0));
1371 if (!Val)
1372 return 0;
1373
1374 for (unsigned I = 1, E = CA->getNumOperands(); I != E; ++I)
1375 if (CA->getOperand(I-1) != CA->getOperand(I))
1376 return 0;
1377
1378 return Val;
1379 }
Chad Rosierdce42b72011-12-06 00:19:08 +00001380
1381 // FIXME: Vector types (e.g., <4 x i32> <i32 -1, i32 -1, i32 -1, i32 -1>).
Chris Lattnerbb897102010-12-26 20:15:01 +00001382
1383 // Conceptually, we could handle things like:
1384 // %a = zext i8 %X to i16
1385 // %b = shl i16 %a, 8
1386 // %c = or i16 %a, %b
1387 // but until there is an example that actually needs this, it doesn't seem
1388 // worth worrying about.
1389 return 0;
1390}
1391
1392
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001393// This is the recursive version of BuildSubAggregate. It takes a few different
1394// arguments. Idxs is the index within the nested struct From that we are
1395// looking at now (which is of type IndexedType). IdxSkip is the number of
1396// indices from Idxs that should be left out when inserting into the resulting
1397// struct. To is the result struct built so far, new insertvalue instructions
1398// build on that.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001399static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType,
Dan Gohman7db949d2009-08-07 01:32:21 +00001400 SmallVector<unsigned, 10> &Idxs,
1401 unsigned IdxSkip,
Dan Gohman7db949d2009-08-07 01:32:21 +00001402 Instruction *InsertBefore) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001403 llvm::StructType *STy = llvm::dyn_cast<llvm::StructType>(IndexedType);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001404 if (STy) {
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001405 // Save the original To argument so we can modify it
1406 Value *OrigTo = To;
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001407 // General case, the type indexed by Idxs is a struct
1408 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1409 // Process each struct element recursively
1410 Idxs.push_back(i);
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001411 Value *PrevTo = To;
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001412 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
Nick Lewyckyae3d8022009-11-23 03:29:18 +00001413 InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001414 Idxs.pop_back();
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001415 if (!To) {
1416 // Couldn't find any inserted value for this index? Cleanup
1417 while (PrevTo != OrigTo) {
1418 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
1419 PrevTo = Del->getAggregateOperand();
1420 Del->eraseFromParent();
1421 }
1422 // Stop processing elements
1423 break;
1424 }
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001425 }
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001426 // If we successfully found a value for each of our subaggregates
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001427 if (To)
1428 return To;
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001429 }
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001430 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
1431 // the struct's elements had a value that was inserted directly. In the latter
1432 // case, perhaps we can't determine each of the subelements individually, but
1433 // we might be able to find the complete struct somewhere.
1434
1435 // Find the value that is at that particular spot
Jay Foadfc6d3a42011-07-13 10:26:04 +00001436 Value *V = FindInsertedValue(From, Idxs);
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001437
1438 if (!V)
1439 return NULL;
1440
1441 // Insert the value in the new (sub) aggregrate
Frits van Bommel39b5abf2011-07-18 12:00:32 +00001442 return llvm::InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip),
Jay Foadfc6d3a42011-07-13 10:26:04 +00001443 "tmp", InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001444}
1445
1446// This helper takes a nested struct and extracts a part of it (which is again a
1447// struct) into a new value. For example, given the struct:
1448// { a, { b, { c, d }, e } }
1449// and the indices "1, 1" this returns
1450// { c, d }.
1451//
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001452// It does this by inserting an insertvalue for each element in the resulting
1453// struct, as opposed to just inserting a single struct. This will only work if
1454// each of the elements of the substruct are known (ie, inserted into From by an
1455// insertvalue instruction somewhere).
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001456//
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001457// All inserted insertvalue instructions are inserted before InsertBefore
Jay Foadfc6d3a42011-07-13 10:26:04 +00001458static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range,
Dan Gohman7db949d2009-08-07 01:32:21 +00001459 Instruction *InsertBefore) {
Matthijs Kooijman97728912008-06-16 13:28:31 +00001460 assert(InsertBefore && "Must have someplace to insert!");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001461 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
Jay Foadfc6d3a42011-07-13 10:26:04 +00001462 idx_range);
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001463 Value *To = UndefValue::get(IndexedType);
Jay Foadfc6d3a42011-07-13 10:26:04 +00001464 SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end());
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001465 unsigned IdxSkip = Idxs.size();
1466
Nick Lewyckyae3d8022009-11-23 03:29:18 +00001467 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001468}
1469
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001470/// FindInsertedValue - Given an aggregrate and an sequence of indices, see if
1471/// the scalar value indexed is already around as a register, for example if it
1472/// were inserted directly into the aggregrate.
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001473///
1474/// If InsertBefore is not null, this function will duplicate (modified)
1475/// insertvalues when a part of a nested struct is extracted.
Jay Foadfc6d3a42011-07-13 10:26:04 +00001476Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range,
1477 Instruction *InsertBefore) {
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001478 // Nothing to index? Just return V then (this is useful at the end of our
1479 // recursion)
Jay Foadfc6d3a42011-07-13 10:26:04 +00001480 if (idx_range.empty())
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001481 return V;
1482 // We have indices, so V should have an indexable type
Duncan Sands1df98592010-02-16 11:11:14 +00001483 assert((V->getType()->isStructTy() || V->getType()->isArrayTy())
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001484 && "Not looking at a struct or array?");
Jay Foadfc6d3a42011-07-13 10:26:04 +00001485 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range)
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001486 && "Invalid indices for type?");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001487 CompositeType *PTy = cast<CompositeType>(V->getType());
Owen Anderson76f600b2009-07-06 22:37:39 +00001488
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001489 if (isa<UndefValue>(V))
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001490 return UndefValue::get(ExtractValueInst::getIndexedType(PTy,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001491 idx_range));
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001492 else if (isa<ConstantAggregateZero>(V))
Owen Andersona7235ea2009-07-31 20:28:14 +00001493 return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy,
Jay Foadfc6d3a42011-07-13 10:26:04 +00001494 idx_range));
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001495 else if (Constant *C = dyn_cast<Constant>(V)) {
1496 if (isa<ConstantArray>(C) || isa<ConstantStruct>(C))
1497 // Recursively process this constant
Jay Foadfc6d3a42011-07-13 10:26:04 +00001498 return FindInsertedValue(C->getOperand(idx_range[0]), idx_range.slice(1),
1499 InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001500 } else if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
1501 // Loop the indices for the insertvalue instruction in parallel with the
1502 // requested indices
Jay Foadfc6d3a42011-07-13 10:26:04 +00001503 const unsigned *req_idx = idx_range.begin();
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001504 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
1505 i != e; ++i, ++req_idx) {
Jay Foadfc6d3a42011-07-13 10:26:04 +00001506 if (req_idx == idx_range.end()) {
Matthijs Kooijman97728912008-06-16 13:28:31 +00001507 if (InsertBefore)
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001508 // The requested index identifies a part of a nested aggregate. Handle
1509 // this specially. For example,
1510 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
1511 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
1512 // %C = extractvalue {i32, { i32, i32 } } %B, 1
1513 // This can be changed into
1514 // %A = insertvalue {i32, i32 } undef, i32 10, 0
1515 // %C = insertvalue {i32, i32 } %A, i32 11, 1
1516 // which allows the unused 0,0 element from the nested struct to be
1517 // removed.
Frits van Bommel39b5abf2011-07-18 12:00:32 +00001518 return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx),
Jay Foadfc6d3a42011-07-13 10:26:04 +00001519 InsertBefore);
Matthijs Kooijman97728912008-06-16 13:28:31 +00001520 else
1521 // We can't handle this without inserting insertvalues
1522 return 0;
Duncan Sands9954c762008-06-19 08:47:31 +00001523 }
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001524
1525 // This insert value inserts something else than what we are looking for.
1526 // See if the (aggregrate) value inserted into has the value we are
1527 // looking for, then.
1528 if (*req_idx != *i)
Jay Foadfc6d3a42011-07-13 10:26:04 +00001529 return FindInsertedValue(I->getAggregateOperand(), idx_range,
Nick Lewyckyae3d8022009-11-23 03:29:18 +00001530 InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001531 }
1532 // If we end up here, the indices of the insertvalue match with those
1533 // requested (though possibly only partially). Now we recursively look at
1534 // the inserted value, passing any remaining indices.
Jay Foadfc6d3a42011-07-13 10:26:04 +00001535 return FindInsertedValue(I->getInsertedValueOperand(),
Frits van Bommel39b5abf2011-07-18 12:00:32 +00001536 makeArrayRef(req_idx, idx_range.end()),
Nick Lewyckyae3d8022009-11-23 03:29:18 +00001537 InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001538 } else if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
1539 // If we're extracting a value from an aggregrate that was extracted from
1540 // something else, we can extract from that something else directly instead.
1541 // However, we will need to chain I's indices with the requested indices.
1542
1543 // Calculate the number of indices required
Jay Foadfc6d3a42011-07-13 10:26:04 +00001544 unsigned size = I->getNumIndices() + idx_range.size();
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001545 // Allocate some space to put the new indices in
Matthijs Kooijman3faf9df2008-06-17 08:24:37 +00001546 SmallVector<unsigned, 5> Idxs;
1547 Idxs.reserve(size);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001548 // Add indices from the extract value instruction
Jay Foadfc6d3a42011-07-13 10:26:04 +00001549 Idxs.append(I->idx_begin(), I->idx_end());
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001550
1551 // Add requested indices
Jay Foadfc6d3a42011-07-13 10:26:04 +00001552 Idxs.append(idx_range.begin(), idx_range.end());
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001553
Matthijs Kooijman3faf9df2008-06-17 08:24:37 +00001554 assert(Idxs.size() == size
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001555 && "Number of indices added not correct?");
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001556
Jay Foadfc6d3a42011-07-13 10:26:04 +00001557 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001558 }
1559 // Otherwise, we don't know (such as, extracting from a function return value
1560 // or load instruction)
1561 return 0;
1562}
Evan Cheng0ff39b32008-06-30 07:31:25 +00001563
Chris Lattnered58a6f2010-11-30 22:25:26 +00001564/// GetPointerBaseWithConstantOffset - Analyze the specified pointer to see if
1565/// it can be expressed as a base pointer plus a constant offset. Return the
1566/// base and offset to the caller.
1567Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
1568 const TargetData &TD) {
1569 Operator *PtrOp = dyn_cast<Operator>(Ptr);
Nadav Rotem16087692011-12-05 06:29:09 +00001570 if (PtrOp == 0 || Ptr->getType()->isVectorTy())
1571 return Ptr;
Chris Lattnered58a6f2010-11-30 22:25:26 +00001572
1573 // Just look through bitcasts.
1574 if (PtrOp->getOpcode() == Instruction::BitCast)
1575 return GetPointerBaseWithConstantOffset(PtrOp->getOperand(0), Offset, TD);
1576
1577 // If this is a GEP with constant indices, we can look through it.
1578 GEPOperator *GEP = dyn_cast<GEPOperator>(PtrOp);
1579 if (GEP == 0 || !GEP->hasAllConstantIndices()) return Ptr;
1580
1581 gep_type_iterator GTI = gep_type_begin(GEP);
1582 for (User::op_iterator I = GEP->idx_begin(), E = GEP->idx_end(); I != E;
1583 ++I, ++GTI) {
1584 ConstantInt *OpC = cast<ConstantInt>(*I);
1585 if (OpC->isZero()) continue;
1586
1587 // Handle a struct and array indices which add their offset to the pointer.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001588 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Chris Lattnered58a6f2010-11-30 22:25:26 +00001589 Offset += TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
1590 } else {
1591 uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
1592 Offset += OpC->getSExtValue()*Size;
1593 }
1594 }
1595
1596 // Re-sign extend from the pointer size if needed to get overflow edge cases
1597 // right.
1598 unsigned PtrSize = TD.getPointerSizeInBits();
1599 if (PtrSize < 64)
1600 Offset = (Offset << (64-PtrSize)) >> (64-PtrSize);
1601
1602 return GetPointerBaseWithConstantOffset(GEP->getPointerOperand(), Offset, TD);
1603}
1604
1605
Evan Cheng0ff39b32008-06-30 07:31:25 +00001606/// GetConstantStringInfo - This function computes the length of a
1607/// null-terminated C string pointed to by V. If successful, it returns true
1608/// and returns the string in Str. If unsuccessful, it returns false.
Dan Gohman0a60fa32010-04-14 22:20:45 +00001609bool llvm::GetConstantStringInfo(const Value *V, std::string &Str,
Nick Lewycky0cd0fee2011-10-20 00:34:35 +00001610 uint64_t Offset, bool StopAtNul) {
Bill Wendling0582ae92009-03-13 04:39:26 +00001611 // If V is NULL then return false;
1612 if (V == NULL) return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001613
1614 // Look through bitcast instructions.
Dan Gohman0a60fa32010-04-14 22:20:45 +00001615 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Bill Wendling0582ae92009-03-13 04:39:26 +00001616 return GetConstantStringInfo(BCI->getOperand(0), Str, Offset, StopAtNul);
1617
Evan Cheng0ff39b32008-06-30 07:31:25 +00001618 // If the value is not a GEP instruction nor a constant expression with a
1619 // GEP instruction, then return false because ConstantArray can't occur
Nick Lewycky0cd0fee2011-10-20 00:34:35 +00001620 // any other way.
Dan Gohman0a60fa32010-04-14 22:20:45 +00001621 const User *GEP = 0;
1622 if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00001623 GEP = GEPI;
Dan Gohman0a60fa32010-04-14 22:20:45 +00001624 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00001625 if (CE->getOpcode() == Instruction::BitCast)
Bill Wendling0582ae92009-03-13 04:39:26 +00001626 return GetConstantStringInfo(CE->getOperand(0), Str, Offset, StopAtNul);
1627 if (CE->getOpcode() != Instruction::GetElementPtr)
1628 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001629 GEP = CE;
1630 }
1631
1632 if (GEP) {
1633 // Make sure the GEP has exactly three arguments.
Bill Wendling0582ae92009-03-13 04:39:26 +00001634 if (GEP->getNumOperands() != 3)
1635 return false;
1636
Evan Cheng0ff39b32008-06-30 07:31:25 +00001637 // Make sure the index-ee is a pointer to array of i8.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001638 PointerType *PT = cast<PointerType>(GEP->getOperand(0)->getType());
1639 ArrayType *AT = dyn_cast<ArrayType>(PT->getElementType());
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001640 if (AT == 0 || !AT->getElementType()->isIntegerTy(8))
Bill Wendling0582ae92009-03-13 04:39:26 +00001641 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001642
1643 // Check to make sure that the first operand of the GEP is an integer and
1644 // has value 0 so that we are sure we're indexing into the initializer.
Dan Gohman0a60fa32010-04-14 22:20:45 +00001645 const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
Bill Wendling0582ae92009-03-13 04:39:26 +00001646 if (FirstIdx == 0 || !FirstIdx->isZero())
1647 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001648
1649 // If the second index isn't a ConstantInt, then this is a variable index
1650 // into the array. If this occurs, we can't say anything meaningful about
1651 // the string.
1652 uint64_t StartIdx = 0;
Dan Gohman0a60fa32010-04-14 22:20:45 +00001653 if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
Evan Cheng0ff39b32008-06-30 07:31:25 +00001654 StartIdx = CI->getZExtValue();
Bill Wendling0582ae92009-03-13 04:39:26 +00001655 else
1656 return false;
1657 return GetConstantStringInfo(GEP->getOperand(0), Str, StartIdx+Offset,
Evan Cheng0ff39b32008-06-30 07:31:25 +00001658 StopAtNul);
1659 }
Nick Lewycky0cd0fee2011-10-20 00:34:35 +00001660
Evan Cheng0ff39b32008-06-30 07:31:25 +00001661 // The GEP instruction, constant or instruction, must reference a global
1662 // variable that is a constant and is initialized. The referenced constant
1663 // initializer is the array that we'll use for optimization.
Dan Gohman0a60fa32010-04-14 22:20:45 +00001664 const GlobalVariable* GV = dyn_cast<GlobalVariable>(V);
Dan Gohman82555732009-08-19 18:20:44 +00001665 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
Bill Wendling0582ae92009-03-13 04:39:26 +00001666 return false;
Dan Gohman0a60fa32010-04-14 22:20:45 +00001667 const Constant *GlobalInit = GV->getInitializer();
Evan Cheng0ff39b32008-06-30 07:31:25 +00001668
Nick Lewycky0cd0fee2011-10-20 00:34:35 +00001669 // Handle the all-zeros case
1670 if (GlobalInit->isNullValue()) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00001671 // This is a degenerate case. The initializer is constant zero so the
1672 // length of the string must be zero.
Bill Wendling0582ae92009-03-13 04:39:26 +00001673 Str.clear();
1674 return true;
1675 }
Evan Cheng0ff39b32008-06-30 07:31:25 +00001676
1677 // Must be a Constant Array
Dan Gohman0a60fa32010-04-14 22:20:45 +00001678 const ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001679 if (Array == 0 || !Array->getType()->getElementType()->isIntegerTy(8))
Bill Wendling0582ae92009-03-13 04:39:26 +00001680 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001681
1682 // Get the number of elements in the array
1683 uint64_t NumElts = Array->getType()->getNumElements();
1684
Bill Wendling0582ae92009-03-13 04:39:26 +00001685 if (Offset > NumElts)
1686 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001687
1688 // Traverse the constant array from 'Offset' which is the place the GEP refers
1689 // to in the array.
Bill Wendling0582ae92009-03-13 04:39:26 +00001690 Str.reserve(NumElts-Offset);
Evan Cheng0ff39b32008-06-30 07:31:25 +00001691 for (unsigned i = Offset; i != NumElts; ++i) {
Dan Gohman0a60fa32010-04-14 22:20:45 +00001692 const Constant *Elt = Array->getOperand(i);
1693 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
Bill Wendling0582ae92009-03-13 04:39:26 +00001694 if (!CI) // This array isn't suitable, non-int initializer.
1695 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001696 if (StopAtNul && CI->isZero())
Bill Wendling0582ae92009-03-13 04:39:26 +00001697 return true; // we found end of string, success!
1698 Str += (char)CI->getZExtValue();
Evan Cheng0ff39b32008-06-30 07:31:25 +00001699 }
Bill Wendling0582ae92009-03-13 04:39:26 +00001700
Evan Cheng0ff39b32008-06-30 07:31:25 +00001701 // The array isn't null terminated, but maybe this is a memcpy, not a strcpy.
Bill Wendling0582ae92009-03-13 04:39:26 +00001702 return true;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001703}
Eric Christopher25ec4832010-03-05 06:58:57 +00001704
1705// These next two are very similar to the above, but also look through PHI
1706// nodes.
1707// TODO: See if we can integrate these two together.
1708
1709/// GetStringLengthH - If we can compute the length of the string pointed to by
1710/// the specified pointer, return 'len+1'. If we can't, return 0.
1711static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
1712 // Look through noop bitcast instructions.
1713 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
1714 return GetStringLengthH(BCI->getOperand(0), PHIs);
1715
1716 // If this is a PHI node, there are two cases: either we have already seen it
1717 // or we haven't.
1718 if (PHINode *PN = dyn_cast<PHINode>(V)) {
1719 if (!PHIs.insert(PN))
1720 return ~0ULL; // already in the set.
1721
1722 // If it was new, see if all the input strings are the same length.
1723 uint64_t LenSoFar = ~0ULL;
1724 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1725 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
1726 if (Len == 0) return 0; // Unknown length -> unknown.
1727
1728 if (Len == ~0ULL) continue;
1729
1730 if (Len != LenSoFar && LenSoFar != ~0ULL)
1731 return 0; // Disagree -> unknown.
1732 LenSoFar = Len;
1733 }
1734
1735 // Success, all agree.
1736 return LenSoFar;
1737 }
1738
1739 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
1740 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
1741 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
1742 if (Len1 == 0) return 0;
1743 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
1744 if (Len2 == 0) return 0;
1745 if (Len1 == ~0ULL) return Len2;
1746 if (Len2 == ~0ULL) return Len1;
1747 if (Len1 != Len2) return 0;
1748 return Len1;
1749 }
1750
Nick Lewycky0cd0fee2011-10-20 00:34:35 +00001751 // As a special-case, "@string = constant i8 0" is also a string with zero
1752 // length, not wrapped in a bitcast or GEP.
1753 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
1754 if (GV->isConstant() && GV->hasDefinitiveInitializer())
1755 if (GV->getInitializer()->isNullValue()) return 1;
1756 return 0;
1757 }
1758
Eric Christopher25ec4832010-03-05 06:58:57 +00001759 // If the value is not a GEP instruction nor a constant expression with a
1760 // GEP instruction, then return unknown.
1761 User *GEP = 0;
1762 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
1763 GEP = GEPI;
1764 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1765 if (CE->getOpcode() != Instruction::GetElementPtr)
1766 return 0;
1767 GEP = CE;
1768 } else {
1769 return 0;
1770 }
1771
1772 // Make sure the GEP has exactly three arguments.
1773 if (GEP->getNumOperands() != 3)
1774 return 0;
1775
1776 // Check to make sure that the first operand of the GEP is an integer and
1777 // has value 0 so that we are sure we're indexing into the initializer.
1778 if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
1779 if (!Idx->isZero())
1780 return 0;
1781 } else
1782 return 0;
1783
1784 // If the second index isn't a ConstantInt, then this is a variable index
1785 // into the array. If this occurs, we can't say anything meaningful about
1786 // the string.
1787 uint64_t StartIdx = 0;
1788 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
1789 StartIdx = CI->getZExtValue();
1790 else
1791 return 0;
1792
1793 // The GEP instruction, constant or instruction, must reference a global
1794 // variable that is a constant and is initialized. The referenced constant
1795 // initializer is the array that we'll use for optimization.
1796 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
1797 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
1798 GV->mayBeOverridden())
1799 return 0;
1800 Constant *GlobalInit = GV->getInitializer();
1801
1802 // Handle the ConstantAggregateZero case, which is a degenerate case. The
1803 // initializer is constant zero so the length of the string must be zero.
1804 if (isa<ConstantAggregateZero>(GlobalInit))
1805 return 1; // Len = 0 offset by 1.
1806
1807 // Must be a Constant Array
1808 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
1809 if (!Array || !Array->getType()->getElementType()->isIntegerTy(8))
1810 return false;
1811
1812 // Get the number of elements in the array
1813 uint64_t NumElts = Array->getType()->getNumElements();
1814
1815 // Traverse the constant array from StartIdx (derived above) which is
1816 // the place the GEP refers to in the array.
1817 for (unsigned i = StartIdx; i != NumElts; ++i) {
1818 Constant *Elt = Array->getOperand(i);
1819 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1820 if (!CI) // This array isn't suitable, non-int initializer.
1821 return 0;
1822 if (CI->isZero())
1823 return i-StartIdx+1; // We found end of string, success!
1824 }
1825
1826 return 0; // The array isn't null terminated, conservatively return 'unknown'.
1827}
1828
1829/// GetStringLength - If we can compute the length of the string pointed to by
1830/// the specified pointer, return 'len+1'. If we can't, return 0.
1831uint64_t llvm::GetStringLength(Value *V) {
1832 if (!V->getType()->isPointerTy()) return 0;
1833
1834 SmallPtrSet<PHINode*, 32> PHIs;
1835 uint64_t Len = GetStringLengthH(V, PHIs);
1836 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
1837 // an empty string as a length.
1838 return Len == ~0ULL ? 1 : Len;
1839}
Dan Gohman5034dd32010-12-15 20:02:24 +00001840
Dan Gohmanbd1801b2011-01-24 18:53:32 +00001841Value *
1842llvm::GetUnderlyingObject(Value *V, const TargetData *TD, unsigned MaxLookup) {
Dan Gohman5034dd32010-12-15 20:02:24 +00001843 if (!V->getType()->isPointerTy())
1844 return V;
1845 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
1846 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
1847 V = GEP->getPointerOperand();
1848 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
1849 V = cast<Operator>(V)->getOperand(0);
1850 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
1851 if (GA->mayBeOverridden())
1852 return V;
1853 V = GA->getAliasee();
1854 } else {
Dan Gohmanc01895c2010-12-15 20:49:55 +00001855 // See if InstructionSimplify knows any relevant tricks.
1856 if (Instruction *I = dyn_cast<Instruction>(V))
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001857 // TODO: Acquire a DominatorTree and use it.
Dan Gohmanbd1801b2011-01-24 18:53:32 +00001858 if (Value *Simplified = SimplifyInstruction(I, TD, 0)) {
Dan Gohmanc01895c2010-12-15 20:49:55 +00001859 V = Simplified;
1860 continue;
1861 }
1862
Dan Gohman5034dd32010-12-15 20:02:24 +00001863 return V;
1864 }
1865 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
1866 }
1867 return V;
1868}
Nick Lewycky99e0b2a2011-06-27 04:20:45 +00001869
1870/// onlyUsedByLifetimeMarkers - Return true if the only users of this pointer
1871/// are lifetime markers.
1872///
1873bool llvm::onlyUsedByLifetimeMarkers(const Value *V) {
1874 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
1875 UI != UE; ++UI) {
1876 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(*UI);
1877 if (!II) return false;
1878
1879 if (II->getIntrinsicID() != Intrinsic::lifetime_start &&
1880 II->getIntrinsicID() != Intrinsic::lifetime_end)
1881 return false;
1882 }
1883 return true;
1884}
Dan Gohmanf0426602011-12-14 23:49:11 +00001885
1886bool llvm::isSafeToSpeculativelyExecute(const Instruction *Inst,
1887 const TargetData *TD) {
1888 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
1889 if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i)))
1890 if (C->canTrap())
1891 return false;
1892
1893 switch (Inst->getOpcode()) {
1894 default:
1895 return true;
1896 case Instruction::UDiv:
1897 case Instruction::URem:
1898 // x / y is undefined if y == 0, but calcuations like x / 3 are safe.
1899 return isKnownNonZero(Inst->getOperand(1), TD);
1900 case Instruction::SDiv:
1901 case Instruction::SRem: {
1902 Value *Op = Inst->getOperand(1);
1903 // x / y is undefined if y == 0
1904 if (!isKnownNonZero(Op, TD))
1905 return false;
1906 // x / y might be undefined if y == -1
1907 unsigned BitWidth = getBitWidth(Op->getType(), TD);
1908 if (BitWidth == 0)
1909 return false;
1910 APInt KnownZero(BitWidth, 0);
1911 APInt KnownOne(BitWidth, 0);
1912 ComputeMaskedBits(Op, APInt::getAllOnesValue(BitWidth),
1913 KnownZero, KnownOne, TD);
1914 return !!KnownZero;
1915 }
1916 case Instruction::Load: {
1917 const LoadInst *LI = cast<LoadInst>(Inst);
1918 if (!LI->isUnordered())
1919 return false;
1920 return LI->getPointerOperand()->isDereferenceablePointer();
1921 }
Nick Lewycky83696872011-12-21 05:52:02 +00001922 case Instruction::Call: {
1923 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
1924 switch (II->getIntrinsicID()) {
1925 case Intrinsic::bswap:
1926 case Intrinsic::ctlz:
1927 case Intrinsic::ctpop:
1928 case Intrinsic::cttz:
1929 case Intrinsic::objectsize:
1930 case Intrinsic::sadd_with_overflow:
1931 case Intrinsic::smul_with_overflow:
1932 case Intrinsic::ssub_with_overflow:
1933 case Intrinsic::uadd_with_overflow:
1934 case Intrinsic::umul_with_overflow:
1935 case Intrinsic::usub_with_overflow:
1936 return true;
1937 // TODO: some fp intrinsics are marked as having the same error handling
1938 // as libm. They're safe to speculate when they won't error.
1939 // TODO: are convert_{from,to}_fp16 safe?
1940 // TODO: can we list target-specific intrinsics here?
1941 default: break;
1942 }
1943 }
Dan Gohmanf0426602011-12-14 23:49:11 +00001944 return false; // The called function could have undefined behavior or
Nick Lewycky83696872011-12-21 05:52:02 +00001945 // side-effects, even if marked readnone nounwind.
1946 }
Dan Gohmanf0426602011-12-14 23:49:11 +00001947 case Instruction::VAArg:
1948 case Instruction::Alloca:
1949 case Instruction::Invoke:
1950 case Instruction::PHI:
1951 case Instruction::Store:
1952 case Instruction::Ret:
1953 case Instruction::Br:
1954 case Instruction::IndirectBr:
1955 case Instruction::Switch:
1956 case Instruction::Unwind:
1957 case Instruction::Unreachable:
1958 case Instruction::Fence:
1959 case Instruction::LandingPad:
1960 case Instruction::AtomicRMW:
1961 case Instruction::AtomicCmpXchg:
1962 case Instruction::Resume:
1963 return false; // Misc instructions which have effects
1964 }
1965}