blob: f9331e76d0d07cf8b6ca57ac68798223d124761e [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"
16#include "llvm/Constants.h"
17#include "llvm/Instructions.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000018#include "llvm/GlobalVariable.h"
Dan Gohman307a7c42009-09-15 16:14:44 +000019#include "llvm/GlobalAlias.h"
Chris Lattner173234a2008-06-02 01:18:21 +000020#include "llvm/IntrinsicInst.h"
Owen Anderson76f600b2009-07-06 22:37:39 +000021#include "llvm/LLVMContext.h"
Dan Gohmanca178902009-07-17 20:47:02 +000022#include "llvm/Operator.h"
Bill Wendling0582ae92009-03-13 04:39:26 +000023#include "llvm/Target/TargetData.h"
Chris Lattner173234a2008-06-02 01:18:21 +000024#include "llvm/Support/GetElementPtrTypeIterator.h"
25#include "llvm/Support/MathExtras.h"
Chris Lattner32a9e7a2008-06-04 04:46:14 +000026#include <cstring>
Chris Lattner173234a2008-06-02 01:18:21 +000027using namespace llvm;
28
Chris Lattner173234a2008-06-02 01:18:21 +000029/// ComputeMaskedBits - Determine which of the bits specified in Mask are
30/// known to be either zero or one and return them in the KnownZero/KnownOne
31/// bit sets. This code only analyzes bits in Mask, in order to short-circuit
32/// processing.
33/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
34/// we cannot optimize based on the assumption that it is zero without changing
35/// it to be an explicit zero. If we don't change it to zero, other code could
36/// optimized based on the contradictory assumption that it is non-zero.
37/// Because instcombine aggressively folds operations with undef args anyway,
38/// this won't lose us code quality.
Chris Lattnercf5128e2009-09-08 00:06:16 +000039///
40/// This function is defined on values with integer type, values with pointer
41/// type (but only if TD is non-null), and vectors of integers. In the case
42/// where V is a vector, the mask, known zero, and known one values are the
43/// same width as the vector element, and the bit is set only if it is true
44/// for all of the elements in the vector.
Chris Lattner173234a2008-06-02 01:18:21 +000045void llvm::ComputeMaskedBits(Value *V, const APInt &Mask,
46 APInt &KnownZero, APInt &KnownOne,
Dan Gohman846a2f22009-08-27 17:51:25 +000047 const TargetData *TD, unsigned Depth) {
Dan Gohman9004c8a2009-05-21 02:28:33 +000048 const unsigned MaxDepth = 6;
Chris Lattner173234a2008-06-02 01:18:21 +000049 assert(V && "No Value?");
Dan Gohman9004c8a2009-05-21 02:28:33 +000050 assert(Depth <= MaxDepth && "Limit Search Depth");
Chris Lattner79abedb2009-01-20 18:22:57 +000051 unsigned BitWidth = Mask.getBitWidth();
Dan Gohman6de29f82009-06-15 22:12:54 +000052 assert((V->getType()->isIntOrIntVector() || isa<PointerType>(V->getType())) &&
Chris Lattner173234a2008-06-02 01:18:21 +000053 "Not integer or pointer type!");
Dan Gohman6de29f82009-06-15 22:12:54 +000054 assert((!TD ||
55 TD->getTypeSizeInBits(V->getType()->getScalarType()) == BitWidth) &&
56 (!V->getType()->isIntOrIntVector() ||
57 V->getType()->getScalarSizeInBits() == BitWidth) &&
Chris Lattner173234a2008-06-02 01:18:21 +000058 KnownZero.getBitWidth() == BitWidth &&
59 KnownOne.getBitWidth() == BitWidth &&
60 "V, Mask, KnownOne and KnownZero should have same BitWidth");
61
62 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
63 // We know all of the bits for a constant!
64 KnownOne = CI->getValue() & Mask;
65 KnownZero = ~KnownOne & Mask;
66 return;
67 }
Dan Gohman6de29f82009-06-15 22:12:54 +000068 // Null and aggregate-zero are all-zeros.
69 if (isa<ConstantPointerNull>(V) ||
70 isa<ConstantAggregateZero>(V)) {
Chris Lattner173234a2008-06-02 01:18:21 +000071 KnownOne.clear();
72 KnownZero = Mask;
73 return;
74 }
Dan Gohman6de29f82009-06-15 22:12:54 +000075 // Handle a constant vector by taking the intersection of the known bits of
76 // each element.
77 if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
78 KnownZero.set(); KnownOne.set();
79 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
80 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
81 ComputeMaskedBits(CV->getOperand(i), Mask, KnownZero2, KnownOne2,
82 TD, Depth);
83 KnownZero &= KnownZero2;
84 KnownOne &= KnownOne2;
85 }
86 return;
87 }
Chris Lattner173234a2008-06-02 01:18:21 +000088 // The address of an aligned GlobalValue has trailing zeros.
89 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
90 unsigned Align = GV->getAlignment();
Dan Gohman00407252009-08-11 15:50:03 +000091 if (Align == 0 && TD && GV->getType()->getElementType()->isSized()) {
92 const Type *ObjectType = GV->getType()->getElementType();
93 // If the object is defined in the current Module, we'll be giving
94 // it the preferred alignment. Otherwise, we have to assume that it
95 // may only have the minimum ABI alignment.
96 if (!GV->isDeclaration() && !GV->mayBeOverridden())
97 Align = TD->getPrefTypeAlignment(ObjectType);
98 else
99 Align = TD->getABITypeAlignment(ObjectType);
100 }
Chris Lattner173234a2008-06-02 01:18:21 +0000101 if (Align > 0)
102 KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
103 CountTrailingZeros_32(Align));
104 else
105 KnownZero.clear();
106 KnownOne.clear();
107 return;
108 }
Dan Gohman307a7c42009-09-15 16:14:44 +0000109 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
110 // the bits of its aliasee.
111 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
112 if (GA->mayBeOverridden()) {
113 KnownZero.clear(); KnownOne.clear();
114 } else {
115 ComputeMaskedBits(GA->getAliasee(), Mask, KnownZero, KnownOne,
116 TD, Depth+1);
117 }
118 return;
119 }
Chris Lattner173234a2008-06-02 01:18:21 +0000120
121 KnownZero.clear(); KnownOne.clear(); // Start out not knowing anything.
122
Dan Gohman9004c8a2009-05-21 02:28:33 +0000123 if (Depth == MaxDepth || Mask == 0)
Chris Lattner173234a2008-06-02 01:18:21 +0000124 return; // Limit search depth.
125
Dan Gohmanca178902009-07-17 20:47:02 +0000126 Operator *I = dyn_cast<Operator>(V);
Chris Lattner173234a2008-06-02 01:18:21 +0000127 if (!I) return;
128
129 APInt KnownZero2(KnownZero), KnownOne2(KnownOne);
Dan Gohmanca178902009-07-17 20:47:02 +0000130 switch (I->getOpcode()) {
Chris Lattner173234a2008-06-02 01:18:21 +0000131 default: break;
132 case Instruction::And: {
133 // If either the LHS or the RHS are Zero, the result is zero.
134 ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1);
135 APInt Mask2(Mask & ~KnownZero);
136 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
137 Depth+1);
138 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
139 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
140
141 // Output known-1 bits are only known if set in both the LHS & RHS.
142 KnownOne &= KnownOne2;
143 // Output known-0 are known to be clear if zero in either the LHS | RHS.
144 KnownZero |= KnownZero2;
145 return;
146 }
147 case Instruction::Or: {
148 ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1);
149 APInt Mask2(Mask & ~KnownOne);
150 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
151 Depth+1);
152 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
153 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
154
155 // Output known-0 bits are only known if clear in both the LHS & RHS.
156 KnownZero &= KnownZero2;
157 // Output known-1 are known to be set if set in either the LHS | RHS.
158 KnownOne |= KnownOne2;
159 return;
160 }
161 case Instruction::Xor: {
162 ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1);
163 ComputeMaskedBits(I->getOperand(0), Mask, 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-0 bits are known if clear or set in both the LHS & RHS.
169 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
170 // Output known-1 are known to be set if set in only one of the LHS, RHS.
171 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
172 KnownZero = KnownZeroOut;
173 return;
174 }
175 case Instruction::Mul: {
176 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
177 ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero, KnownOne, TD,Depth+1);
178 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
179 Depth+1);
180 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
181 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
182
183 // If low bits are zero in either operand, output low known-0 bits.
184 // Also compute a conserative estimate for high known-0 bits.
185 // More trickiness is possible, but this is sufficient for the
186 // interesting case of alignment computation.
187 KnownOne.clear();
188 unsigned TrailZ = KnownZero.countTrailingOnes() +
189 KnownZero2.countTrailingOnes();
190 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
191 KnownZero2.countLeadingOnes(),
192 BitWidth) - BitWidth;
193
194 TrailZ = std::min(TrailZ, BitWidth);
195 LeadZ = std::min(LeadZ, BitWidth);
196 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
197 APInt::getHighBitsSet(BitWidth, LeadZ);
198 KnownZero &= Mask;
199 return;
200 }
201 case Instruction::UDiv: {
202 // For the purposes of computing leading zeros we can conservatively
203 // treat a udiv as a logical right shift by the power of 2 known to
204 // be less than the denominator.
205 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
206 ComputeMaskedBits(I->getOperand(0),
207 AllOnes, KnownZero2, KnownOne2, TD, Depth+1);
208 unsigned LeadZ = KnownZero2.countLeadingOnes();
209
210 KnownOne2.clear();
211 KnownZero2.clear();
212 ComputeMaskedBits(I->getOperand(1),
213 AllOnes, KnownZero2, KnownOne2, TD, Depth+1);
214 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
215 if (RHSUnknownLeadingOnes != BitWidth)
216 LeadZ = std::min(BitWidth,
217 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
218
219 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
220 return;
221 }
222 case Instruction::Select:
223 ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, TD, Depth+1);
224 ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, TD,
225 Depth+1);
226 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
227 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
228
229 // Only known if known in both the LHS and RHS.
230 KnownOne &= KnownOne2;
231 KnownZero &= KnownZero2;
232 return;
233 case Instruction::FPTrunc:
234 case Instruction::FPExt:
235 case Instruction::FPToUI:
236 case Instruction::FPToSI:
237 case Instruction::SIToFP:
238 case Instruction::UIToFP:
239 return; // Can't work with floating point.
240 case Instruction::PtrToInt:
241 case Instruction::IntToPtr:
242 // We can't handle these if we don't know the pointer size.
243 if (!TD) return;
244 // FALL THROUGH and handle them the same as zext/trunc.
245 case Instruction::ZExt:
246 case Instruction::Trunc: {
Chris Lattnerb9a4ddb2009-09-08 00:13:52 +0000247 const Type *SrcTy = I->getOperand(0)->getType();
248
249 unsigned SrcBitWidth;
Chris Lattner173234a2008-06-02 01:18:21 +0000250 // Note that we handle pointer operands here because of inttoptr/ptrtoint
251 // which fall through here.
Chris Lattnerb9a4ddb2009-09-08 00:13:52 +0000252 if (isa<PointerType>(SrcTy))
253 SrcBitWidth = TD->getTypeSizeInBits(SrcTy);
254 else
255 SrcBitWidth = SrcTy->getScalarSizeInBits();
256
Chris Lattner173234a2008-06-02 01:18:21 +0000257 APInt MaskIn(Mask);
258 MaskIn.zextOrTrunc(SrcBitWidth);
259 KnownZero.zextOrTrunc(SrcBitWidth);
260 KnownOne.zextOrTrunc(SrcBitWidth);
261 ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, TD,
262 Depth+1);
263 KnownZero.zextOrTrunc(BitWidth);
264 KnownOne.zextOrTrunc(BitWidth);
265 // Any top bits are known to be zero.
266 if (BitWidth > SrcBitWidth)
267 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
268 return;
269 }
270 case Instruction::BitCast: {
271 const Type *SrcTy = I->getOperand(0)->getType();
Chris Lattner0dabb0b2009-07-02 16:04:08 +0000272 if ((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
273 // TODO: For now, not handling conversions like:
274 // (bitcast i64 %x to <2 x i32>)
275 !isa<VectorType>(I->getType())) {
Chris Lattner173234a2008-06-02 01:18:21 +0000276 ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, TD,
277 Depth+1);
278 return;
279 }
280 break;
281 }
282 case Instruction::SExt: {
283 // Compute the bits in the result that are not present in the input.
Chris Lattnerb9a4ddb2009-09-08 00:13:52 +0000284 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
Chris Lattner173234a2008-06-02 01:18:21 +0000285
286 APInt MaskIn(Mask);
287 MaskIn.trunc(SrcBitWidth);
288 KnownZero.trunc(SrcBitWidth);
289 KnownOne.trunc(SrcBitWidth);
290 ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, TD,
291 Depth+1);
292 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
293 KnownZero.zext(BitWidth);
294 KnownOne.zext(BitWidth);
295
296 // If the sign bit of the input is known set or clear, then we know the
297 // top bits of the result.
298 if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero
299 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
300 else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set
301 KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
302 return;
303 }
304 case Instruction::Shl:
305 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
306 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
307 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
308 APInt Mask2(Mask.lshr(ShiftAmt));
309 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD,
310 Depth+1);
311 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
312 KnownZero <<= ShiftAmt;
313 KnownOne <<= ShiftAmt;
314 KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0
315 return;
316 }
317 break;
318 case Instruction::LShr:
319 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
320 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
321 // Compute the new bits that are at the top now.
322 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
323
324 // Unsigned shift right.
325 APInt Mask2(Mask.shl(ShiftAmt));
326 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne, TD,
327 Depth+1);
Nick Lewyckyae3d8022009-11-23 03:29:18 +0000328 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattner173234a2008-06-02 01:18:21 +0000329 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
330 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
331 // high bits known zero.
332 KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
333 return;
334 }
335 break;
336 case Instruction::AShr:
337 // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
338 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
339 // Compute the new bits that are at the top now.
340 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
341
342 // Signed shift right.
343 APInt Mask2(Mask.shl(ShiftAmt));
344 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD,
345 Depth+1);
Nick Lewyckyae3d8022009-11-23 03:29:18 +0000346 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattner173234a2008-06-02 01:18:21 +0000347 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
348 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
349
350 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
351 if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero.
352 KnownZero |= HighBits;
353 else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one.
354 KnownOne |= HighBits;
355 return;
356 }
357 break;
358 case Instruction::Sub: {
359 if (ConstantInt *CLHS = dyn_cast<ConstantInt>(I->getOperand(0))) {
360 // We know that the top bits of C-X are clear if X contains less bits
361 // than C (i.e. no wrap-around can happen). For example, 20-X is
362 // positive if we can prove that X is >= 0 and < 16.
363 if (!CLHS->getValue().isNegative()) {
364 unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros();
365 // NLZ can't be BitWidth with no sign bit
366 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
367 ComputeMaskedBits(I->getOperand(1), MaskV, KnownZero2, KnownOne2,
368 TD, Depth+1);
369
370 // If all of the MaskV bits are known to be zero, then we know the
371 // output top bits are zero, because we now know that the output is
372 // from [0-C].
373 if ((KnownZero2 & MaskV) == MaskV) {
374 unsigned NLZ2 = CLHS->getValue().countLeadingZeros();
375 // Top bits known zero.
376 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
377 }
378 }
379 }
380 }
381 // fall through
382 case Instruction::Add: {
Nick Lewyckyae3d8022009-11-23 03:29:18 +0000383 // If one of the operands has trailing zeros, then the bits that the
Dan Gohman39250432009-05-24 18:02:35 +0000384 // other operand has in those bit positions will be preserved in the
385 // result. For an add, this works with either operand. For a subtract,
386 // this only works if the known zeros are in the right operand.
387 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
388 APInt Mask2 = APInt::getLowBitsSet(BitWidth,
389 BitWidth - Mask.countLeadingZeros());
390 ComputeMaskedBits(I->getOperand(0), Mask2, LHSKnownZero, LHSKnownOne, TD,
Chris Lattner173234a2008-06-02 01:18:21 +0000391 Depth+1);
Dan Gohman39250432009-05-24 18:02:35 +0000392 assert((LHSKnownZero & LHSKnownOne) == 0 &&
393 "Bits known to be one AND zero?");
394 unsigned LHSKnownZeroOut = LHSKnownZero.countTrailingOnes();
Chris Lattner173234a2008-06-02 01:18:21 +0000395
396 ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero2, KnownOne2, TD,
397 Depth+1);
398 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman39250432009-05-24 18:02:35 +0000399 unsigned RHSKnownZeroOut = KnownZero2.countTrailingOnes();
Chris Lattner173234a2008-06-02 01:18:21 +0000400
Dan Gohman39250432009-05-24 18:02:35 +0000401 // Determine which operand has more trailing zeros, and use that
402 // many bits from the other operand.
403 if (LHSKnownZeroOut > RHSKnownZeroOut) {
Dan Gohmanca178902009-07-17 20:47:02 +0000404 if (I->getOpcode() == Instruction::Add) {
Dan Gohman39250432009-05-24 18:02:35 +0000405 APInt Mask = APInt::getLowBitsSet(BitWidth, LHSKnownZeroOut);
406 KnownZero |= KnownZero2 & Mask;
407 KnownOne |= KnownOne2 & Mask;
408 } else {
409 // If the known zeros are in the left operand for a subtract,
410 // fall back to the minimum known zeros in both operands.
411 KnownZero |= APInt::getLowBitsSet(BitWidth,
412 std::min(LHSKnownZeroOut,
413 RHSKnownZeroOut));
414 }
415 } else if (RHSKnownZeroOut >= LHSKnownZeroOut) {
416 APInt Mask = APInt::getLowBitsSet(BitWidth, RHSKnownZeroOut);
417 KnownZero |= LHSKnownZero & Mask;
418 KnownOne |= LHSKnownOne & Mask;
419 }
Chris Lattner173234a2008-06-02 01:18:21 +0000420 return;
421 }
422 case Instruction::SRem:
423 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Duncan Sandscfd54182010-01-29 06:18:37 +0000424 APInt RA = Rem->getValue().abs();
425 if (RA.isPowerOf2()) {
426 APInt LowBits = RA - 1;
Chris Lattner173234a2008-06-02 01:18:21 +0000427 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
428 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
429 Depth+1);
430
Duncan Sandscfd54182010-01-29 06:18:37 +0000431 // The low bits of the first operand are unchanged by the srem.
432 KnownZero = KnownZero2 & LowBits;
433 KnownOne = KnownOne2 & LowBits;
Chris Lattner173234a2008-06-02 01:18:21 +0000434
Duncan Sandscfd54182010-01-29 06:18:37 +0000435 // If the first operand is non-negative or has all low bits zero, then
436 // the upper bits are all zero.
437 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
438 KnownZero |= ~LowBits;
439
440 // If the first operand is negative and not all low bits are zero, then
441 // the upper bits are all one.
442 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
443 KnownOne |= ~LowBits;
444
445 KnownZero &= Mask;
446 KnownOne &= Mask;
Chris Lattner173234a2008-06-02 01:18:21 +0000447
Nick Lewyckyae3d8022009-11-23 03:29:18 +0000448 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattner173234a2008-06-02 01:18:21 +0000449 }
450 }
451 break;
452 case Instruction::URem: {
453 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
454 APInt RA = Rem->getValue();
455 if (RA.isPowerOf2()) {
456 APInt LowBits = (RA - 1);
457 APInt Mask2 = LowBits & Mask;
458 KnownZero |= ~LowBits & Mask;
459 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD,
460 Depth+1);
Nick Lewyckyae3d8022009-11-23 03:29:18 +0000461 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Chris Lattner173234a2008-06-02 01:18:21 +0000462 break;
463 }
464 }
465
466 // Since the result is less than or equal to either operand, any leading
467 // zero bits in either operand must also exist in the result.
468 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
469 ComputeMaskedBits(I->getOperand(0), AllOnes, KnownZero, KnownOne,
470 TD, Depth+1);
471 ComputeMaskedBits(I->getOperand(1), AllOnes, KnownZero2, KnownOne2,
472 TD, Depth+1);
473
Chris Lattner79abedb2009-01-20 18:22:57 +0000474 unsigned Leaders = std::max(KnownZero.countLeadingOnes(),
Chris Lattner173234a2008-06-02 01:18:21 +0000475 KnownZero2.countLeadingOnes());
476 KnownOne.clear();
477 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
478 break;
479 }
480
Victor Hernandeza276c602009-10-17 01:18:07 +0000481 case Instruction::Alloca: {
Victor Hernandez7b929da2009-10-23 21:09:37 +0000482 AllocaInst *AI = cast<AllocaInst>(V);
Chris Lattner173234a2008-06-02 01:18:21 +0000483 unsigned Align = AI->getAlignment();
Victor Hernandeza276c602009-10-17 01:18:07 +0000484 if (Align == 0 && TD)
485 Align = TD->getABITypeAlignment(AI->getType()->getElementType());
Chris Lattner173234a2008-06-02 01:18:21 +0000486
487 if (Align > 0)
488 KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
489 CountTrailingZeros_32(Align));
490 break;
491 }
492 case Instruction::GetElementPtr: {
493 // Analyze all of the subscripts of this getelementptr instruction
494 // to determine if we can prove known low zero bits.
495 APInt LocalMask = APInt::getAllOnesValue(BitWidth);
496 APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0);
497 ComputeMaskedBits(I->getOperand(0), LocalMask,
498 LocalKnownZero, LocalKnownOne, TD, Depth+1);
499 unsigned TrailZ = LocalKnownZero.countTrailingOnes();
500
501 gep_type_iterator GTI = gep_type_begin(I);
502 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
503 Value *Index = I->getOperand(i);
504 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
505 // Handle struct member offset arithmetic.
506 if (!TD) return;
507 const StructLayout *SL = TD->getStructLayout(STy);
508 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
509 uint64_t Offset = SL->getElementOffset(Idx);
510 TrailZ = std::min(TrailZ,
511 CountTrailingZeros_64(Offset));
512 } else {
513 // Handle array index arithmetic.
514 const Type *IndexedTy = GTI.getIndexedType();
515 if (!IndexedTy->isSized()) return;
Dan Gohman6de29f82009-06-15 22:12:54 +0000516 unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits();
Duncan Sands777d2302009-05-09 07:06:46 +0000517 uint64_t TypeSize = TD ? TD->getTypeAllocSize(IndexedTy) : 1;
Chris Lattner173234a2008-06-02 01:18:21 +0000518 LocalMask = APInt::getAllOnesValue(GEPOpiBits);
519 LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0);
520 ComputeMaskedBits(Index, LocalMask,
521 LocalKnownZero, LocalKnownOne, TD, Depth+1);
522 TrailZ = std::min(TrailZ,
Chris Lattner79abedb2009-01-20 18:22:57 +0000523 unsigned(CountTrailingZeros_64(TypeSize) +
524 LocalKnownZero.countTrailingOnes()));
Chris Lattner173234a2008-06-02 01:18:21 +0000525 }
526 }
527
528 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) & Mask;
529 break;
530 }
531 case Instruction::PHI: {
532 PHINode *P = cast<PHINode>(I);
533 // Handle the case of a simple two-predecessor recurrence PHI.
534 // There's a lot more that could theoretically be done here, but
535 // this is sufficient to catch some interesting cases.
536 if (P->getNumIncomingValues() == 2) {
537 for (unsigned i = 0; i != 2; ++i) {
538 Value *L = P->getIncomingValue(i);
539 Value *R = P->getIncomingValue(!i);
Dan Gohmanca178902009-07-17 20:47:02 +0000540 Operator *LU = dyn_cast<Operator>(L);
Chris Lattner173234a2008-06-02 01:18:21 +0000541 if (!LU)
542 continue;
Dan Gohmanca178902009-07-17 20:47:02 +0000543 unsigned Opcode = LU->getOpcode();
Chris Lattner173234a2008-06-02 01:18:21 +0000544 // Check for operations that have the property that if
545 // both their operands have low zero bits, the result
546 // will have low zero bits.
547 if (Opcode == Instruction::Add ||
548 Opcode == Instruction::Sub ||
549 Opcode == Instruction::And ||
550 Opcode == Instruction::Or ||
551 Opcode == Instruction::Mul) {
552 Value *LL = LU->getOperand(0);
553 Value *LR = LU->getOperand(1);
554 // Find a recurrence.
555 if (LL == I)
556 L = LR;
557 else if (LR == I)
558 L = LL;
559 else
560 break;
561 // Ok, we have a PHI of the form L op= R. Check for low
562 // zero bits.
563 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
564 ComputeMaskedBits(R, Mask2, KnownZero2, KnownOne2, TD, Depth+1);
565 Mask2 = APInt::getLowBitsSet(BitWidth,
566 KnownZero2.countTrailingOnes());
David Greenec714f132008-10-27 23:24:03 +0000567
568 // We need to take the minimum number of known bits
569 APInt KnownZero3(KnownZero), KnownOne3(KnownOne);
570 ComputeMaskedBits(L, Mask2, KnownZero3, KnownOne3, TD, Depth+1);
571
Chris Lattner173234a2008-06-02 01:18:21 +0000572 KnownZero = Mask &
573 APInt::getLowBitsSet(BitWidth,
David Greenec714f132008-10-27 23:24:03 +0000574 std::min(KnownZero2.countTrailingOnes(),
575 KnownZero3.countTrailingOnes()));
Chris Lattner173234a2008-06-02 01:18:21 +0000576 break;
577 }
578 }
579 }
Dan Gohman9004c8a2009-05-21 02:28:33 +0000580
581 // Otherwise take the unions of the known bit sets of the operands,
582 // taking conservative care to avoid excessive recursion.
583 if (Depth < MaxDepth - 1 && !KnownZero && !KnownOne) {
584 KnownZero = APInt::getAllOnesValue(BitWidth);
585 KnownOne = APInt::getAllOnesValue(BitWidth);
586 for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) {
587 // Skip direct self references.
588 if (P->getIncomingValue(i) == P) continue;
589
590 KnownZero2 = APInt(BitWidth, 0);
591 KnownOne2 = APInt(BitWidth, 0);
592 // Recurse, but cap the recursion to one level, because we don't
593 // want to waste time spinning around in loops.
594 ComputeMaskedBits(P->getIncomingValue(i), KnownZero | KnownOne,
595 KnownZero2, KnownOne2, TD, MaxDepth-1);
596 KnownZero &= KnownZero2;
597 KnownOne &= KnownOne2;
598 // If all bits have been ruled out, there's no need to check
599 // more operands.
600 if (!KnownZero && !KnownOne)
601 break;
602 }
603 }
Chris Lattner173234a2008-06-02 01:18:21 +0000604 break;
605 }
606 case Instruction::Call:
607 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
608 switch (II->getIntrinsicID()) {
609 default: break;
610 case Intrinsic::ctpop:
611 case Intrinsic::ctlz:
612 case Intrinsic::cttz: {
613 unsigned LowBits = Log2_32(BitWidth)+1;
614 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
615 break;
616 }
617 }
618 }
619 break;
620 }
621}
622
623/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
624/// this predicate to simplify operations downstream. Mask is known to be zero
625/// for bits that V cannot have.
Chris Lattnercf5128e2009-09-08 00:06:16 +0000626///
627/// This function is defined on values with integer type, values with pointer
628/// type (but only if TD is non-null), and vectors of integers. In the case
629/// where V is a vector, the mask, known zero, and known one values are the
630/// same width as the vector element, and the bit is set only if it is true
631/// for all of the elements in the vector.
Chris Lattner173234a2008-06-02 01:18:21 +0000632bool llvm::MaskedValueIsZero(Value *V, const APInt &Mask,
Dan Gohman846a2f22009-08-27 17:51:25 +0000633 const TargetData *TD, unsigned Depth) {
Chris Lattner173234a2008-06-02 01:18:21 +0000634 APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0);
635 ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth);
636 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
637 return (KnownZero & Mask) == Mask;
638}
639
640
641
642/// ComputeNumSignBits - Return the number of times the sign bit of the
643/// register is replicated into the other bits. We know that at least 1 bit
644/// is always equal to the sign bit (itself), but other cases can give us
645/// information. For example, immediately after an "ashr X, 2", we know that
646/// the top 3 bits are all equal to each other, so we return 3.
647///
648/// 'Op' must have a scalar integer type.
649///
Dan Gohman846a2f22009-08-27 17:51:25 +0000650unsigned llvm::ComputeNumSignBits(Value *V, const TargetData *TD,
651 unsigned Depth) {
Dan Gohmanbd5ce522009-06-22 22:02:32 +0000652 assert((TD || V->getType()->isIntOrIntVector()) &&
653 "ComputeNumSignBits requires a TargetData object to operate "
654 "on non-integer values!");
Dan Gohman6de29f82009-06-15 22:12:54 +0000655 const Type *Ty = V->getType();
Dan Gohmanbd5ce522009-06-22 22:02:32 +0000656 unsigned TyBits = TD ? TD->getTypeSizeInBits(V->getType()->getScalarType()) :
657 Ty->getScalarSizeInBits();
Chris Lattner173234a2008-06-02 01:18:21 +0000658 unsigned Tmp, Tmp2;
659 unsigned FirstAnswer = 1;
660
Chris Lattnerd82e5112008-06-02 18:39:07 +0000661 // Note that ConstantInt is handled by the general ComputeMaskedBits case
662 // below.
663
Chris Lattner173234a2008-06-02 01:18:21 +0000664 if (Depth == 6)
665 return 1; // Limit search depth.
666
Dan Gohmanca178902009-07-17 20:47:02 +0000667 Operator *U = dyn_cast<Operator>(V);
668 switch (Operator::getOpcode(V)) {
Chris Lattner173234a2008-06-02 01:18:21 +0000669 default: break;
670 case Instruction::SExt:
Mon P Wang69a00802009-12-02 04:59:58 +0000671 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
Chris Lattner173234a2008-06-02 01:18:21 +0000672 return ComputeNumSignBits(U->getOperand(0), TD, Depth+1) + Tmp;
673
674 case Instruction::AShr:
675 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
676 // ashr X, C -> adds C sign bits.
677 if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) {
678 Tmp += C->getZExtValue();
679 if (Tmp > TyBits) Tmp = TyBits;
680 }
681 return Tmp;
682 case Instruction::Shl:
683 if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) {
684 // shl destroys sign bits.
685 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
686 if (C->getZExtValue() >= TyBits || // Bad shift.
687 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
688 return Tmp - C->getZExtValue();
689 }
690 break;
691 case Instruction::And:
692 case Instruction::Or:
693 case Instruction::Xor: // NOT is handled here.
694 // Logical binary ops preserve the number of sign bits at the worst.
695 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
696 if (Tmp != 1) {
697 Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
698 FirstAnswer = std::min(Tmp, Tmp2);
699 // We computed what we know about the sign bits as our first
700 // answer. Now proceed to the generic code that uses
701 // ComputeMaskedBits, and pick whichever answer is better.
702 }
703 break;
704
705 case Instruction::Select:
706 Tmp = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
707 if (Tmp == 1) return 1; // Early out.
708 Tmp2 = ComputeNumSignBits(U->getOperand(2), TD, Depth+1);
709 return std::min(Tmp, Tmp2);
710
711 case Instruction::Add:
712 // Add can have at most one carry bit. Thus we know that the output
713 // is, at worst, one more bit than the inputs.
714 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
715 if (Tmp == 1) return 1; // Early out.
716
717 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +0000718 if (ConstantInt *CRHS = dyn_cast<ConstantInt>(U->getOperand(1)))
Chris Lattner173234a2008-06-02 01:18:21 +0000719 if (CRHS->isAllOnesValue()) {
720 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
721 APInt Mask = APInt::getAllOnesValue(TyBits);
722 ComputeMaskedBits(U->getOperand(0), Mask, KnownZero, KnownOne, TD,
723 Depth+1);
724
725 // If the input is known to be 0 or 1, the output is 0/-1, which is all
726 // sign bits set.
727 if ((KnownZero | APInt(TyBits, 1)) == Mask)
728 return TyBits;
729
730 // If we are subtracting one from a positive number, there is no carry
731 // out of the result.
732 if (KnownZero.isNegative())
733 return Tmp;
734 }
735
736 Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
737 if (Tmp2 == 1) return 1;
Chris Lattner8d10f9d2010-01-07 23:44:37 +0000738 return std::min(Tmp, Tmp2)-1;
Chris Lattner173234a2008-06-02 01:18:21 +0000739
740 case Instruction::Sub:
741 Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
742 if (Tmp2 == 1) return 1;
743
744 // Handle NEG.
745 if (ConstantInt *CLHS = dyn_cast<ConstantInt>(U->getOperand(0)))
746 if (CLHS->isNullValue()) {
747 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
748 APInt Mask = APInt::getAllOnesValue(TyBits);
749 ComputeMaskedBits(U->getOperand(1), Mask, KnownZero, KnownOne,
750 TD, Depth+1);
751 // If the input is known to be 0 or 1, the output is 0/-1, which is all
752 // sign bits set.
753 if ((KnownZero | APInt(TyBits, 1)) == Mask)
754 return TyBits;
755
756 // If the input is known to be positive (the sign bit is known clear),
757 // the output of the NEG has the same number of sign bits as the input.
758 if (KnownZero.isNegative())
759 return Tmp2;
760
761 // Otherwise, we treat this like a SUB.
762 }
763
764 // Sub can have at most one carry bit. Thus we know that the output
765 // is, at worst, one more bit than the inputs.
766 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
767 if (Tmp == 1) return 1; // Early out.
Chris Lattner8d10f9d2010-01-07 23:44:37 +0000768 return std::min(Tmp, Tmp2)-1;
769
770 case Instruction::PHI: {
771 PHINode *PN = cast<PHINode>(U);
772 // Don't analyze large in-degree PHIs.
773 if (PN->getNumIncomingValues() > 4) break;
774
775 // Take the minimum of all incoming values. This can't infinitely loop
776 // because of our depth threshold.
777 Tmp = ComputeNumSignBits(PN->getIncomingValue(0), TD, Depth+1);
778 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
779 if (Tmp == 1) return Tmp;
780 Tmp = std::min(Tmp,
781 ComputeNumSignBits(PN->getIncomingValue(1), TD, Depth+1));
782 }
783 return Tmp;
784 }
785
Chris Lattner173234a2008-06-02 01:18:21 +0000786 case Instruction::Trunc:
787 // FIXME: it's tricky to do anything useful for this, but it is an important
788 // case for targets like X86.
789 break;
790 }
791
792 // Finally, if we can prove that the top bits of the result are 0's or 1's,
793 // use this information.
794 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
795 APInt Mask = APInt::getAllOnesValue(TyBits);
796 ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth);
797
798 if (KnownZero.isNegative()) { // sign bit is 0
799 Mask = KnownZero;
800 } else if (KnownOne.isNegative()) { // sign bit is 1;
801 Mask = KnownOne;
802 } else {
803 // Nothing known.
804 return FirstAnswer;
805 }
806
807 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
808 // the number of identical bits in the top of the input value.
809 Mask = ~Mask;
810 Mask <<= Mask.getBitWidth()-TyBits;
811 // Return # leading zeros. We use 'min' here in case Val was zero before
812 // shifting. We don't want to return '64' as for an i32 "0".
813 return std::max(FirstAnswer, std::min(TyBits, Mask.countLeadingZeros()));
814}
Chris Lattner833f25d2008-06-02 01:29:46 +0000815
Victor Hernandez2b6705f2009-11-10 08:28:35 +0000816/// ComputeMultiple - This function computes the integer multiple of Base that
817/// equals V. If successful, it returns true and returns the multiple in
Dan Gohman3dbb9e62009-11-18 00:58:27 +0000818/// Multiple. If unsuccessful, it returns false. It looks
Victor Hernandez2b6705f2009-11-10 08:28:35 +0000819/// through SExt instructions only if LookThroughSExt is true.
820bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
Dan Gohman3dbb9e62009-11-18 00:58:27 +0000821 bool LookThroughSExt, unsigned Depth) {
Victor Hernandez2b6705f2009-11-10 08:28:35 +0000822 const unsigned MaxDepth = 6;
823
Dan Gohman3dbb9e62009-11-18 00:58:27 +0000824 assert(V && "No Value?");
Victor Hernandez2b6705f2009-11-10 08:28:35 +0000825 assert(Depth <= MaxDepth && "Limit Search Depth");
826 assert(V->getType()->isInteger() && "Not integer or pointer type!");
827
828 const Type *T = V->getType();
Victor Hernandez2b6705f2009-11-10 08:28:35 +0000829
Dan Gohman3dbb9e62009-11-18 00:58:27 +0000830 ConstantInt *CI = dyn_cast<ConstantInt>(V);
Victor Hernandez2b6705f2009-11-10 08:28:35 +0000831
832 if (Base == 0)
833 return false;
834
835 if (Base == 1) {
836 Multiple = V;
837 return true;
838 }
839
840 ConstantExpr *CO = dyn_cast<ConstantExpr>(V);
841 Constant *BaseVal = ConstantInt::get(T, Base);
842 if (CO && CO == BaseVal) {
843 // Multiple is 1.
844 Multiple = ConstantInt::get(T, 1);
845 return true;
846 }
847
848 if (CI && CI->getZExtValue() % Base == 0) {
849 Multiple = ConstantInt::get(T, CI->getZExtValue() / Base);
850 return true;
851 }
852
853 if (Depth == MaxDepth) return false; // Limit search depth.
854
855 Operator *I = dyn_cast<Operator>(V);
856 if (!I) return false;
857
858 switch (I->getOpcode()) {
859 default: break;
Chris Lattner11fe7262009-11-26 01:50:12 +0000860 case Instruction::SExt:
Victor Hernandez2b6705f2009-11-10 08:28:35 +0000861 if (!LookThroughSExt) return false;
862 // otherwise fall through to ZExt
Chris Lattner11fe7262009-11-26 01:50:12 +0000863 case Instruction::ZExt:
Dan Gohman3dbb9e62009-11-18 00:58:27 +0000864 return ComputeMultiple(I->getOperand(0), Base, Multiple,
865 LookThroughSExt, Depth+1);
Victor Hernandez2b6705f2009-11-10 08:28:35 +0000866 case Instruction::Shl:
867 case Instruction::Mul: {
868 Value *Op0 = I->getOperand(0);
869 Value *Op1 = I->getOperand(1);
870
871 if (I->getOpcode() == Instruction::Shl) {
872 ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1);
873 if (!Op1CI) return false;
874 // Turn Op0 << Op1 into Op0 * 2^Op1
875 APInt Op1Int = Op1CI->getValue();
876 uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
877 Op1 = ConstantInt::get(V->getContext(),
878 APInt(Op1Int.getBitWidth(), 0).set(BitToSet));
879 }
880
881 Value *Mul0 = NULL;
882 Value *Mul1 = NULL;
Dan Gohman3dbb9e62009-11-18 00:58:27 +0000883 bool M0 = ComputeMultiple(Op0, Base, Mul0,
884 LookThroughSExt, Depth+1);
885 bool M1 = ComputeMultiple(Op1, Base, Mul1,
886 LookThroughSExt, Depth+1);
Victor Hernandez2b6705f2009-11-10 08:28:35 +0000887
888 if (M0) {
889 if (isa<Constant>(Op1) && isa<Constant>(Mul0)) {
890 // V == Base * (Mul0 * Op1), so return (Mul0 * Op1)
891 Multiple = ConstantExpr::getMul(cast<Constant>(Mul0),
Victor Hernandez2b6705f2009-11-10 08:28:35 +0000892 cast<Constant>(Op1));
893 return true;
894 }
895
896 if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0))
897 if (Mul0CI->getValue() == 1) {
898 // V == Base * Op1, so return Op1
899 Multiple = Op1;
900 return true;
901 }
902 }
903
904 if (M1) {
905 if (isa<Constant>(Op0) && isa<Constant>(Mul1)) {
906 // V == Base * (Mul1 * Op0), so return (Mul1 * Op0)
907 Multiple = ConstantExpr::getMul(cast<Constant>(Mul1),
Victor Hernandez2b6705f2009-11-10 08:28:35 +0000908 cast<Constant>(Op0));
909 return true;
910 }
911
912 if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1))
913 if (Mul1CI->getValue() == 1) {
914 // V == Base * Op0, so return Op0
915 Multiple = Op0;
916 return true;
917 }
918 }
Victor Hernandez2b6705f2009-11-10 08:28:35 +0000919 }
920 }
921
922 // We could not determine if V is a multiple of Base.
923 return false;
924}
925
Chris Lattner833f25d2008-06-02 01:29:46 +0000926/// CannotBeNegativeZero - Return true if we can prove that the specified FP
927/// value is never equal to -0.0.
928///
929/// NOTE: this function will need to be revisited when we support non-default
930/// rounding modes!
931///
932bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) {
933 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
934 return !CFP->getValueAPF().isNegZero();
935
936 if (Depth == 6)
937 return 1; // Limit search depth.
938
Dan Gohmanca178902009-07-17 20:47:02 +0000939 const Operator *I = dyn_cast<Operator>(V);
Chris Lattner833f25d2008-06-02 01:29:46 +0000940 if (I == 0) return false;
941
942 // (add x, 0.0) is guaranteed to return +0.0, not -0.0.
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000943 if (I->getOpcode() == Instruction::FAdd &&
Chris Lattner833f25d2008-06-02 01:29:46 +0000944 isa<ConstantFP>(I->getOperand(1)) &&
945 cast<ConstantFP>(I->getOperand(1))->isNullValue())
946 return true;
947
948 // sitofp and uitofp turn into +0.0 for zero.
949 if (isa<SIToFPInst>(I) || isa<UIToFPInst>(I))
950 return true;
951
952 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
953 // sqrt(-0.0) = -0.0, no other negative results are possible.
954 if (II->getIntrinsicID() == Intrinsic::sqrt)
955 return CannotBeNegativeZero(II->getOperand(1), Depth+1);
956
957 if (const CallInst *CI = dyn_cast<CallInst>(I))
958 if (const Function *F = CI->getCalledFunction()) {
959 if (F->isDeclaration()) {
Daniel Dunbarf0443c12009-07-26 08:34:35 +0000960 // abs(x) != -0.0
961 if (F->getName() == "abs") return true;
Dale Johannesen9d061752009-09-25 20:54:50 +0000962 // fabs[lf](x) != -0.0
963 if (F->getName() == "fabs") return true;
964 if (F->getName() == "fabsf") return true;
965 if (F->getName() == "fabsl") return true;
966 if (F->getName() == "sqrt" || F->getName() == "sqrtf" ||
967 F->getName() == "sqrtl")
968 return CannotBeNegativeZero(CI->getOperand(1), Depth+1);
Chris Lattner833f25d2008-06-02 01:29:46 +0000969 }
970 }
971
972 return false;
973}
974
Chris Lattnere405c642009-11-26 17:12:50 +0000975
976/// GetLinearExpression - Analyze the specified value as a linear expression:
Chris Lattner1ce0eaa2009-11-26 18:53:33 +0000977/// "A*V + B", where A and B are constant integers. Return the scale and offset
978/// values as APInts and return V as a Value*. The incoming Value is known to
979/// have IntegerType. Note that this looks through extends, so the high bits
980/// may not be represented in the result.
Chris Lattnere405c642009-11-26 17:12:50 +0000981static Value *GetLinearExpression(Value *V, APInt &Scale, APInt &Offset,
Chris Lattnera650f772009-11-27 08:32:52 +0000982 const TargetData *TD, unsigned Depth) {
Chris Lattnere405c642009-11-26 17:12:50 +0000983 assert(isa<IntegerType>(V->getType()) && "Not an integer value");
Chris Lattnera650f772009-11-27 08:32:52 +0000984
985 // Limit our recursion depth.
986 if (Depth == 6) {
987 Scale = 1;
988 Offset = 0;
989 return V;
990 }
Chris Lattnere405c642009-11-26 17:12:50 +0000991
992 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(V)) {
993 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
994 switch (BOp->getOpcode()) {
995 default: break;
996 case Instruction::Or:
997 // X|C == X+C if all the bits in C are unset in X. Otherwise we can't
998 // analyze it.
999 if (!MaskedValueIsZero(BOp->getOperand(0), RHSC->getValue(), TD))
1000 break;
1001 // FALL THROUGH.
1002 case Instruction::Add:
Chris Lattnera650f772009-11-27 08:32:52 +00001003 V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, TD, Depth+1);
Chris Lattnere405c642009-11-26 17:12:50 +00001004 Offset += RHSC->getValue();
1005 return V;
1006 case Instruction::Mul:
Chris Lattnera650f772009-11-27 08:32:52 +00001007 V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, TD, Depth+1);
Chris Lattnere405c642009-11-26 17:12:50 +00001008 Offset *= RHSC->getValue();
1009 Scale *= RHSC->getValue();
1010 return V;
1011 case Instruction::Shl:
Chris Lattnera650f772009-11-27 08:32:52 +00001012 V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, TD, Depth+1);
Chris Lattnere405c642009-11-26 17:12:50 +00001013 Offset <<= RHSC->getValue().getLimitedValue();
1014 Scale <<= RHSC->getValue().getLimitedValue();
1015 return V;
1016 }
1017 }
1018 }
1019
Chris Lattner1ce0eaa2009-11-26 18:53:33 +00001020 // Since clients don't care about the high bits of the value, just scales and
1021 // offsets, we can look through extensions.
1022 if (isa<SExtInst>(V) || isa<ZExtInst>(V)) {
1023 Value *CastOp = cast<CastInst>(V)->getOperand(0);
1024 unsigned OldWidth = Scale.getBitWidth();
1025 unsigned SmallWidth = CastOp->getType()->getPrimitiveSizeInBits();
1026 Scale.trunc(SmallWidth);
1027 Offset.trunc(SmallWidth);
Chris Lattnera650f772009-11-27 08:32:52 +00001028 Value *Result = GetLinearExpression(CastOp, Scale, Offset, TD, Depth+1);
Chris Lattner1ce0eaa2009-11-26 18:53:33 +00001029 Scale.zext(OldWidth);
1030 Offset.zext(OldWidth);
1031 return Result;
1032 }
1033
Chris Lattnere405c642009-11-26 17:12:50 +00001034 Scale = 1;
1035 Offset = 0;
1036 return V;
1037}
1038
1039/// DecomposeGEPExpression - If V is a symbolic pointer expression, decompose it
1040/// into a base pointer with a constant offset and a number of scaled symbolic
1041/// offsets.
1042///
Chris Lattner1ce0eaa2009-11-26 18:53:33 +00001043/// The scaled symbolic offsets (represented by pairs of a Value* and a scale in
1044/// the VarIndices vector) are Value*'s that are known to be scaled by the
1045/// specified amount, but which may have other unrepresented high bits. As such,
1046/// the gep cannot necessarily be reconstructed from its decomposed form.
1047///
Chris Lattnere405c642009-11-26 17:12:50 +00001048/// When TargetData is around, this function is capable of analyzing everything
1049/// that Value::getUnderlyingObject() can look through. When not, it just looks
1050/// through pointer casts.
1051///
1052const Value *llvm::DecomposeGEPExpression(const Value *V, int64_t &BaseOffs,
1053 SmallVectorImpl<std::pair<const Value*, int64_t> > &VarIndices,
1054 const TargetData *TD) {
Chris Lattnerab9530e2009-11-28 15:12:41 +00001055 // Limit recursion depth to limit compile time in crazy cases.
1056 unsigned MaxLookup = 6;
1057
Chris Lattnere405c642009-11-26 17:12:50 +00001058 BaseOffs = 0;
Chris Lattnerab9530e2009-11-28 15:12:41 +00001059 do {
Chris Lattnere405c642009-11-26 17:12:50 +00001060 // See if this is a bitcast or GEP.
1061 const Operator *Op = dyn_cast<Operator>(V);
1062 if (Op == 0) {
1063 // The only non-operator case we can handle are GlobalAliases.
1064 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
1065 if (!GA->mayBeOverridden()) {
1066 V = GA->getAliasee();
1067 continue;
1068 }
1069 }
1070 return V;
1071 }
1072
1073 if (Op->getOpcode() == Instruction::BitCast) {
1074 V = Op->getOperand(0);
1075 continue;
1076 }
1077
1078 const GEPOperator *GEPOp = dyn_cast<GEPOperator>(Op);
1079 if (GEPOp == 0)
1080 return V;
1081
1082 // Don't attempt to analyze GEPs over unsized objects.
1083 if (!cast<PointerType>(GEPOp->getOperand(0)->getType())
1084 ->getElementType()->isSized())
1085 return V;
1086
1087 // If we are lacking TargetData information, we can't compute the offets of
1088 // elements computed by GEPs. However, we can handle bitcast equivalent
1089 // GEPs.
1090 if (!TD) {
1091 if (!GEPOp->hasAllZeroIndices())
1092 return V;
1093 V = GEPOp->getOperand(0);
1094 continue;
1095 }
1096
1097 // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices.
1098 gep_type_iterator GTI = gep_type_begin(GEPOp);
1099 for (User::const_op_iterator I = GEPOp->op_begin()+1,
1100 E = GEPOp->op_end(); I != E; ++I) {
1101 Value *Index = *I;
1102 // Compute the (potentially symbolic) offset in bytes for this index.
1103 if (const StructType *STy = dyn_cast<StructType>(*GTI++)) {
1104 // For a struct, add the member offset.
1105 unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
1106 if (FieldNo == 0) continue;
1107
1108 BaseOffs += TD->getStructLayout(STy)->getElementOffset(FieldNo);
1109 continue;
1110 }
1111
1112 // For an array/pointer, add the element offset, explicitly scaled.
1113 if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) {
1114 if (CIdx->isZero()) continue;
1115 BaseOffs += TD->getTypeAllocSize(*GTI)*CIdx->getSExtValue();
1116 continue;
1117 }
1118
Chris Lattnere405c642009-11-26 17:12:50 +00001119 uint64_t Scale = TD->getTypeAllocSize(*GTI);
1120
Chris Lattnerb18004c2009-11-26 18:35:46 +00001121 // Use GetLinearExpression to decompose the index into a C1*V+C2 form.
Chris Lattnere405c642009-11-26 17:12:50 +00001122 unsigned Width = cast<IntegerType>(Index->getType())->getBitWidth();
1123 APInt IndexScale(Width, 0), IndexOffset(Width, 0);
Chris Lattnera650f772009-11-27 08:32:52 +00001124 Index = GetLinearExpression(Index, IndexScale, IndexOffset, TD, 0);
Chris Lattnere405c642009-11-26 17:12:50 +00001125
Chris Lattnerb18004c2009-11-26 18:35:46 +00001126 // The GEP index scale ("Scale") scales C1*V+C2, yielding (C1*V+C2)*Scale.
1127 // This gives us an aggregate computation of (C1*Scale)*V + C2*Scale.
Chris Lattnere405c642009-11-26 17:12:50 +00001128 BaseOffs += IndexOffset.getZExtValue()*Scale;
Chris Lattnerb18004c2009-11-26 18:35:46 +00001129 Scale *= IndexScale.getZExtValue();
Chris Lattnere405c642009-11-26 17:12:50 +00001130
1131
1132 // If we already had an occurrance of this index variable, merge this
1133 // scale into it. For example, we want to handle:
1134 // A[x][x] -> x*16 + x*4 -> x*20
1135 // This also ensures that 'x' only appears in the index list once.
1136 for (unsigned i = 0, e = VarIndices.size(); i != e; ++i) {
1137 if (VarIndices[i].first == Index) {
1138 Scale += VarIndices[i].second;
1139 VarIndices.erase(VarIndices.begin()+i);
1140 break;
1141 }
1142 }
1143
1144 // Make sure that we have a scale that makes sense for this target's
1145 // pointer size.
1146 if (unsigned ShiftBits = 64-TD->getPointerSizeInBits()) {
1147 Scale <<= ShiftBits;
1148 Scale >>= ShiftBits;
1149 }
1150
1151 if (Scale)
1152 VarIndices.push_back(std::make_pair(Index, Scale));
1153 }
1154
1155 // Analyze the base pointer next.
1156 V = GEPOp->getOperand(0);
Chris Lattnerab9530e2009-11-28 15:12:41 +00001157 } while (--MaxLookup);
1158
1159 // If the chain of expressions is too deep, just return early.
1160 return V;
Chris Lattnere405c642009-11-26 17:12:50 +00001161}
1162
1163
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001164// This is the recursive version of BuildSubAggregate. It takes a few different
1165// arguments. Idxs is the index within the nested struct From that we are
1166// looking at now (which is of type IndexedType). IdxSkip is the number of
1167// indices from Idxs that should be left out when inserting into the resulting
1168// struct. To is the result struct built so far, new insertvalue instructions
1169// build on that.
Dan Gohman7db949d2009-08-07 01:32:21 +00001170static Value *BuildSubAggregate(Value *From, Value* To, const Type *IndexedType,
1171 SmallVector<unsigned, 10> &Idxs,
1172 unsigned IdxSkip,
Dan Gohman7db949d2009-08-07 01:32:21 +00001173 Instruction *InsertBefore) {
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001174 const llvm::StructType *STy = llvm::dyn_cast<llvm::StructType>(IndexedType);
1175 if (STy) {
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001176 // Save the original To argument so we can modify it
1177 Value *OrigTo = To;
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001178 // General case, the type indexed by Idxs is a struct
1179 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1180 // Process each struct element recursively
1181 Idxs.push_back(i);
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001182 Value *PrevTo = To;
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001183 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
Nick Lewyckyae3d8022009-11-23 03:29:18 +00001184 InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001185 Idxs.pop_back();
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001186 if (!To) {
1187 // Couldn't find any inserted value for this index? Cleanup
1188 while (PrevTo != OrigTo) {
1189 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
1190 PrevTo = Del->getAggregateOperand();
1191 Del->eraseFromParent();
1192 }
1193 // Stop processing elements
1194 break;
1195 }
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001196 }
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001197 // If we succesfully found a value for each of our subaggregates
1198 if (To)
1199 return To;
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001200 }
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001201 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
1202 // the struct's elements had a value that was inserted directly. In the latter
1203 // case, perhaps we can't determine each of the subelements individually, but
1204 // we might be able to find the complete struct somewhere.
1205
1206 // Find the value that is at that particular spot
Nick Lewyckyae3d8022009-11-23 03:29:18 +00001207 Value *V = FindInsertedValue(From, Idxs.begin(), Idxs.end());
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001208
1209 if (!V)
1210 return NULL;
1211
1212 // Insert the value in the new (sub) aggregrate
1213 return llvm::InsertValueInst::Create(To, V, Idxs.begin() + IdxSkip,
1214 Idxs.end(), "tmp", InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001215}
1216
1217// This helper takes a nested struct and extracts a part of it (which is again a
1218// struct) into a new value. For example, given the struct:
1219// { a, { b, { c, d }, e } }
1220// and the indices "1, 1" this returns
1221// { c, d }.
1222//
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001223// It does this by inserting an insertvalue for each element in the resulting
1224// struct, as opposed to just inserting a single struct. This will only work if
1225// each of the elements of the substruct are known (ie, inserted into From by an
1226// insertvalue instruction somewhere).
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001227//
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001228// All inserted insertvalue instructions are inserted before InsertBefore
Dan Gohman7db949d2009-08-07 01:32:21 +00001229static Value *BuildSubAggregate(Value *From, const unsigned *idx_begin,
Nick Lewyckyae3d8022009-11-23 03:29:18 +00001230 const unsigned *idx_end,
Dan Gohman7db949d2009-08-07 01:32:21 +00001231 Instruction *InsertBefore) {
Matthijs Kooijman97728912008-06-16 13:28:31 +00001232 assert(InsertBefore && "Must have someplace to insert!");
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001233 const Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
1234 idx_begin,
1235 idx_end);
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001236 Value *To = UndefValue::get(IndexedType);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001237 SmallVector<unsigned, 10> Idxs(idx_begin, idx_end);
1238 unsigned IdxSkip = Idxs.size();
1239
Nick Lewyckyae3d8022009-11-23 03:29:18 +00001240 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001241}
1242
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001243/// FindInsertedValue - Given an aggregrate and an sequence of indices, see if
1244/// the scalar value indexed is already around as a register, for example if it
1245/// were inserted directly into the aggregrate.
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001246///
1247/// If InsertBefore is not null, this function will duplicate (modified)
1248/// insertvalues when a part of a nested struct is extracted.
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001249Value *llvm::FindInsertedValue(Value *V, const unsigned *idx_begin,
Nick Lewyckyae3d8022009-11-23 03:29:18 +00001250 const unsigned *idx_end, Instruction *InsertBefore) {
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001251 // Nothing to index? Just return V then (this is useful at the end of our
1252 // recursion)
1253 if (idx_begin == idx_end)
1254 return V;
1255 // We have indices, so V should have an indexable type
1256 assert((isa<StructType>(V->getType()) || isa<ArrayType>(V->getType()))
1257 && "Not looking at a struct or array?");
1258 assert(ExtractValueInst::getIndexedType(V->getType(), idx_begin, idx_end)
1259 && "Invalid indices for type?");
1260 const CompositeType *PTy = cast<CompositeType>(V->getType());
Owen Anderson76f600b2009-07-06 22:37:39 +00001261
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001262 if (isa<UndefValue>(V))
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001263 return UndefValue::get(ExtractValueInst::getIndexedType(PTy,
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001264 idx_begin,
1265 idx_end));
1266 else if (isa<ConstantAggregateZero>(V))
Owen Andersona7235ea2009-07-31 20:28:14 +00001267 return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy,
Owen Anderson76f600b2009-07-06 22:37:39 +00001268 idx_begin,
1269 idx_end));
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001270 else if (Constant *C = dyn_cast<Constant>(V)) {
1271 if (isa<ConstantArray>(C) || isa<ConstantStruct>(C))
1272 // Recursively process this constant
Owen Anderson76f600b2009-07-06 22:37:39 +00001273 return FindInsertedValue(C->getOperand(*idx_begin), idx_begin + 1,
Nick Lewyckyae3d8022009-11-23 03:29:18 +00001274 idx_end, InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001275 } else if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
1276 // Loop the indices for the insertvalue instruction in parallel with the
1277 // requested indices
1278 const unsigned *req_idx = idx_begin;
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001279 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
1280 i != e; ++i, ++req_idx) {
Duncan Sands9954c762008-06-19 08:47:31 +00001281 if (req_idx == idx_end) {
Matthijs Kooijman97728912008-06-16 13:28:31 +00001282 if (InsertBefore)
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +00001283 // The requested index identifies a part of a nested aggregate. Handle
1284 // this specially. For example,
1285 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
1286 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
1287 // %C = extractvalue {i32, { i32, i32 } } %B, 1
1288 // This can be changed into
1289 // %A = insertvalue {i32, i32 } undef, i32 10, 0
1290 // %C = insertvalue {i32, i32 } %A, i32 11, 1
1291 // which allows the unused 0,0 element from the nested struct to be
1292 // removed.
Nick Lewyckyae3d8022009-11-23 03:29:18 +00001293 return BuildSubAggregate(V, idx_begin, req_idx, InsertBefore);
Matthijs Kooijman97728912008-06-16 13:28:31 +00001294 else
1295 // We can't handle this without inserting insertvalues
1296 return 0;
Duncan Sands9954c762008-06-19 08:47:31 +00001297 }
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001298
1299 // This insert value inserts something else than what we are looking for.
1300 // See if the (aggregrate) value inserted into has the value we are
1301 // looking for, then.
1302 if (*req_idx != *i)
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001303 return FindInsertedValue(I->getAggregateOperand(), idx_begin, idx_end,
Nick Lewyckyae3d8022009-11-23 03:29:18 +00001304 InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001305 }
1306 // If we end up here, the indices of the insertvalue match with those
1307 // requested (though possibly only partially). Now we recursively look at
1308 // the inserted value, passing any remaining indices.
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001309 return FindInsertedValue(I->getInsertedValueOperand(), req_idx, idx_end,
Nick Lewyckyae3d8022009-11-23 03:29:18 +00001310 InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001311 } else if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
1312 // If we're extracting a value from an aggregrate that was extracted from
1313 // something else, we can extract from that something else directly instead.
1314 // However, we will need to chain I's indices with the requested indices.
1315
1316 // Calculate the number of indices required
1317 unsigned size = I->getNumIndices() + (idx_end - idx_begin);
1318 // Allocate some space to put the new indices in
Matthijs Kooijman3faf9df2008-06-17 08:24:37 +00001319 SmallVector<unsigned, 5> Idxs;
1320 Idxs.reserve(size);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001321 // Add indices from the extract value instruction
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001322 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
Matthijs Kooijman3faf9df2008-06-17 08:24:37 +00001323 i != e; ++i)
1324 Idxs.push_back(*i);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001325
1326 // Add requested indices
Matthijs Kooijman3faf9df2008-06-17 08:24:37 +00001327 for (const unsigned *i = idx_begin, *e = idx_end; i != e; ++i)
1328 Idxs.push_back(*i);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001329
Matthijs Kooijman3faf9df2008-06-17 08:24:37 +00001330 assert(Idxs.size() == size
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001331 && "Number of indices added not correct?");
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001332
Matthijs Kooijman3faf9df2008-06-17 08:24:37 +00001333 return FindInsertedValue(I->getAggregateOperand(), Idxs.begin(), Idxs.end(),
Nick Lewyckyae3d8022009-11-23 03:29:18 +00001334 InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001335 }
1336 // Otherwise, we don't know (such as, extracting from a function return value
1337 // or load instruction)
1338 return 0;
1339}
Evan Cheng0ff39b32008-06-30 07:31:25 +00001340
1341/// GetConstantStringInfo - This function computes the length of a
1342/// null-terminated C string pointed to by V. If successful, it returns true
1343/// and returns the string in Str. If unsuccessful, it returns false.
Bill Wendling0582ae92009-03-13 04:39:26 +00001344bool llvm::GetConstantStringInfo(Value *V, std::string &Str, uint64_t Offset,
1345 bool StopAtNul) {
1346 // If V is NULL then return false;
1347 if (V == NULL) return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001348
1349 // Look through bitcast instructions.
1350 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Bill Wendling0582ae92009-03-13 04:39:26 +00001351 return GetConstantStringInfo(BCI->getOperand(0), Str, Offset, StopAtNul);
1352
Evan Cheng0ff39b32008-06-30 07:31:25 +00001353 // If the value is not a GEP instruction nor a constant expression with a
1354 // GEP instruction, then return false because ConstantArray can't occur
1355 // any other way
1356 User *GEP = 0;
1357 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
1358 GEP = GEPI;
1359 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1360 if (CE->getOpcode() == Instruction::BitCast)
Bill Wendling0582ae92009-03-13 04:39:26 +00001361 return GetConstantStringInfo(CE->getOperand(0), Str, Offset, StopAtNul);
1362 if (CE->getOpcode() != Instruction::GetElementPtr)
1363 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001364 GEP = CE;
1365 }
1366
1367 if (GEP) {
1368 // Make sure the GEP has exactly three arguments.
Bill Wendling0582ae92009-03-13 04:39:26 +00001369 if (GEP->getNumOperands() != 3)
1370 return false;
1371
Evan Cheng0ff39b32008-06-30 07:31:25 +00001372 // Make sure the index-ee is a pointer to array of i8.
1373 const PointerType *PT = cast<PointerType>(GEP->getOperand(0)->getType());
1374 const ArrayType *AT = dyn_cast<ArrayType>(PT->getElementType());
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +00001375 if (AT == 0 || !AT->getElementType()->isInteger(8))
Bill Wendling0582ae92009-03-13 04:39:26 +00001376 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001377
1378 // Check to make sure that the first operand of the GEP is an integer and
1379 // has value 0 so that we are sure we're indexing into the initializer.
1380 ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
Bill Wendling0582ae92009-03-13 04:39:26 +00001381 if (FirstIdx == 0 || !FirstIdx->isZero())
1382 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001383
1384 // If the second index isn't a ConstantInt, then this is a variable index
1385 // into the array. If this occurs, we can't say anything meaningful about
1386 // the string.
1387 uint64_t StartIdx = 0;
Bill Wendling0582ae92009-03-13 04:39:26 +00001388 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
Evan Cheng0ff39b32008-06-30 07:31:25 +00001389 StartIdx = CI->getZExtValue();
Bill Wendling0582ae92009-03-13 04:39:26 +00001390 else
1391 return false;
1392 return GetConstantStringInfo(GEP->getOperand(0), Str, StartIdx+Offset,
Evan Cheng0ff39b32008-06-30 07:31:25 +00001393 StopAtNul);
1394 }
1395
1396 // The GEP instruction, constant or instruction, must reference a global
1397 // variable that is a constant and is initialized. The referenced constant
1398 // initializer is the array that we'll use for optimization.
1399 GlobalVariable* GV = dyn_cast<GlobalVariable>(V);
Dan Gohman82555732009-08-19 18:20:44 +00001400 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
Bill Wendling0582ae92009-03-13 04:39:26 +00001401 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001402 Constant *GlobalInit = GV->getInitializer();
1403
1404 // Handle the ConstantAggregateZero case
Bill Wendling0582ae92009-03-13 04:39:26 +00001405 if (isa<ConstantAggregateZero>(GlobalInit)) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00001406 // This is a degenerate case. The initializer is constant zero so the
1407 // length of the string must be zero.
Bill Wendling0582ae92009-03-13 04:39:26 +00001408 Str.clear();
1409 return true;
1410 }
Evan Cheng0ff39b32008-06-30 07:31:25 +00001411
1412 // Must be a Constant Array
1413 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +00001414 if (Array == 0 || !Array->getType()->getElementType()->isInteger(8))
Bill Wendling0582ae92009-03-13 04:39:26 +00001415 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001416
1417 // Get the number of elements in the array
1418 uint64_t NumElts = Array->getType()->getNumElements();
1419
Bill Wendling0582ae92009-03-13 04:39:26 +00001420 if (Offset > NumElts)
1421 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001422
1423 // Traverse the constant array from 'Offset' which is the place the GEP refers
1424 // to in the array.
Bill Wendling0582ae92009-03-13 04:39:26 +00001425 Str.reserve(NumElts-Offset);
Evan Cheng0ff39b32008-06-30 07:31:25 +00001426 for (unsigned i = Offset; i != NumElts; ++i) {
1427 Constant *Elt = Array->getOperand(i);
1428 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
Bill Wendling0582ae92009-03-13 04:39:26 +00001429 if (!CI) // This array isn't suitable, non-int initializer.
1430 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001431 if (StopAtNul && CI->isZero())
Bill Wendling0582ae92009-03-13 04:39:26 +00001432 return true; // we found end of string, success!
1433 Str += (char)CI->getZExtValue();
Evan Cheng0ff39b32008-06-30 07:31:25 +00001434 }
Bill Wendling0582ae92009-03-13 04:39:26 +00001435
Evan Cheng0ff39b32008-06-30 07:31:25 +00001436 // The array isn't null terminated, but maybe this is a memcpy, not a strcpy.
Bill Wendling0582ae92009-03-13 04:39:26 +00001437 return true;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001438}