blob: baa347a6638f90f1f38c3cdd8c1b7084aa8e2a5d [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);
328 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
329 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);
346 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
347 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: {
Dan Gohman39250432009-05-24 18:02:35 +0000383 // If one of the operands has trailing zeros, than the bits that the
384 // 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))) {
424 APInt RA = Rem->getValue();
425 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
426 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
427 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
428 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
429 Depth+1);
430
Dan Gohmana60832b2008-08-13 23:12:35 +0000431 // If the sign bit of the first operand is zero, the sign bit of
432 // the result is zero. If the first operand has no one bits below
433 // the second operand's single 1 bit, its sign will be zero.
Chris Lattner173234a2008-06-02 01:18:21 +0000434 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
435 KnownZero2 |= ~LowBits;
Chris Lattner173234a2008-06-02 01:18:21 +0000436
437 KnownZero |= KnownZero2 & Mask;
Chris Lattner173234a2008-06-02 01:18:21 +0000438
439 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
440 }
441 }
442 break;
443 case Instruction::URem: {
444 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
445 APInt RA = Rem->getValue();
446 if (RA.isPowerOf2()) {
447 APInt LowBits = (RA - 1);
448 APInt Mask2 = LowBits & Mask;
449 KnownZero |= ~LowBits & Mask;
450 ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD,
451 Depth+1);
452 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
453 break;
454 }
455 }
456
457 // Since the result is less than or equal to either operand, any leading
458 // zero bits in either operand must also exist in the result.
459 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
460 ComputeMaskedBits(I->getOperand(0), AllOnes, KnownZero, KnownOne,
461 TD, Depth+1);
462 ComputeMaskedBits(I->getOperand(1), AllOnes, KnownZero2, KnownOne2,
463 TD, Depth+1);
464
Chris Lattner79abedb2009-01-20 18:22:57 +0000465 unsigned Leaders = std::max(KnownZero.countLeadingOnes(),
Chris Lattner173234a2008-06-02 01:18:21 +0000466 KnownZero2.countLeadingOnes());
467 KnownOne.clear();
468 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
469 break;
470 }
471
472 case Instruction::Alloca:
473 case Instruction::Malloc: {
474 AllocationInst *AI = cast<AllocationInst>(V);
475 unsigned Align = AI->getAlignment();
476 if (Align == 0 && TD) {
477 if (isa<AllocaInst>(AI))
Chris Lattner0f2831c2009-01-08 19:28:38 +0000478 Align = TD->getABITypeAlignment(AI->getType()->getElementType());
Chris Lattner173234a2008-06-02 01:18:21 +0000479 else if (isa<MallocInst>(AI)) {
480 // Malloc returns maximally aligned memory.
481 Align = TD->getABITypeAlignment(AI->getType()->getElementType());
482 Align =
483 std::max(Align,
Owen Anderson1d0be152009-08-13 21:58:54 +0000484 (unsigned)TD->getABITypeAlignment(
485 Type::getDoubleTy(V->getContext())));
Chris Lattner173234a2008-06-02 01:18:21 +0000486 Align =
487 std::max(Align,
Owen Anderson1d0be152009-08-13 21:58:54 +0000488 (unsigned)TD->getABITypeAlignment(
489 Type::getInt64Ty(V->getContext())));
Chris Lattner173234a2008-06-02 01:18:21 +0000490 }
491 }
492
493 if (Align > 0)
494 KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
495 CountTrailingZeros_32(Align));
496 break;
497 }
498 case Instruction::GetElementPtr: {
499 // Analyze all of the subscripts of this getelementptr instruction
500 // to determine if we can prove known low zero bits.
501 APInt LocalMask = APInt::getAllOnesValue(BitWidth);
502 APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0);
503 ComputeMaskedBits(I->getOperand(0), LocalMask,
504 LocalKnownZero, LocalKnownOne, TD, Depth+1);
505 unsigned TrailZ = LocalKnownZero.countTrailingOnes();
506
507 gep_type_iterator GTI = gep_type_begin(I);
508 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
509 Value *Index = I->getOperand(i);
510 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
511 // Handle struct member offset arithmetic.
512 if (!TD) return;
513 const StructLayout *SL = TD->getStructLayout(STy);
514 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
515 uint64_t Offset = SL->getElementOffset(Idx);
516 TrailZ = std::min(TrailZ,
517 CountTrailingZeros_64(Offset));
518 } else {
519 // Handle array index arithmetic.
520 const Type *IndexedTy = GTI.getIndexedType();
521 if (!IndexedTy->isSized()) return;
Dan Gohman6de29f82009-06-15 22:12:54 +0000522 unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits();
Duncan Sands777d2302009-05-09 07:06:46 +0000523 uint64_t TypeSize = TD ? TD->getTypeAllocSize(IndexedTy) : 1;
Chris Lattner173234a2008-06-02 01:18:21 +0000524 LocalMask = APInt::getAllOnesValue(GEPOpiBits);
525 LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0);
526 ComputeMaskedBits(Index, LocalMask,
527 LocalKnownZero, LocalKnownOne, TD, Depth+1);
528 TrailZ = std::min(TrailZ,
Chris Lattner79abedb2009-01-20 18:22:57 +0000529 unsigned(CountTrailingZeros_64(TypeSize) +
530 LocalKnownZero.countTrailingOnes()));
Chris Lattner173234a2008-06-02 01:18:21 +0000531 }
532 }
533
534 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) & Mask;
535 break;
536 }
537 case Instruction::PHI: {
538 PHINode *P = cast<PHINode>(I);
539 // Handle the case of a simple two-predecessor recurrence PHI.
540 // There's a lot more that could theoretically be done here, but
541 // this is sufficient to catch some interesting cases.
542 if (P->getNumIncomingValues() == 2) {
543 for (unsigned i = 0; i != 2; ++i) {
544 Value *L = P->getIncomingValue(i);
545 Value *R = P->getIncomingValue(!i);
Dan Gohmanca178902009-07-17 20:47:02 +0000546 Operator *LU = dyn_cast<Operator>(L);
Chris Lattner173234a2008-06-02 01:18:21 +0000547 if (!LU)
548 continue;
Dan Gohmanca178902009-07-17 20:47:02 +0000549 unsigned Opcode = LU->getOpcode();
Chris Lattner173234a2008-06-02 01:18:21 +0000550 // Check for operations that have the property that if
551 // both their operands have low zero bits, the result
552 // will have low zero bits.
553 if (Opcode == Instruction::Add ||
554 Opcode == Instruction::Sub ||
555 Opcode == Instruction::And ||
556 Opcode == Instruction::Or ||
557 Opcode == Instruction::Mul) {
558 Value *LL = LU->getOperand(0);
559 Value *LR = LU->getOperand(1);
560 // Find a recurrence.
561 if (LL == I)
562 L = LR;
563 else if (LR == I)
564 L = LL;
565 else
566 break;
567 // Ok, we have a PHI of the form L op= R. Check for low
568 // zero bits.
569 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
570 ComputeMaskedBits(R, Mask2, KnownZero2, KnownOne2, TD, Depth+1);
571 Mask2 = APInt::getLowBitsSet(BitWidth,
572 KnownZero2.countTrailingOnes());
David Greenec714f132008-10-27 23:24:03 +0000573
574 // We need to take the minimum number of known bits
575 APInt KnownZero3(KnownZero), KnownOne3(KnownOne);
576 ComputeMaskedBits(L, Mask2, KnownZero3, KnownOne3, TD, Depth+1);
577
Chris Lattner173234a2008-06-02 01:18:21 +0000578 KnownZero = Mask &
579 APInt::getLowBitsSet(BitWidth,
David Greenec714f132008-10-27 23:24:03 +0000580 std::min(KnownZero2.countTrailingOnes(),
581 KnownZero3.countTrailingOnes()));
Chris Lattner173234a2008-06-02 01:18:21 +0000582 break;
583 }
584 }
585 }
Dan Gohman9004c8a2009-05-21 02:28:33 +0000586
587 // Otherwise take the unions of the known bit sets of the operands,
588 // taking conservative care to avoid excessive recursion.
589 if (Depth < MaxDepth - 1 && !KnownZero && !KnownOne) {
590 KnownZero = APInt::getAllOnesValue(BitWidth);
591 KnownOne = APInt::getAllOnesValue(BitWidth);
592 for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) {
593 // Skip direct self references.
594 if (P->getIncomingValue(i) == P) continue;
595
596 KnownZero2 = APInt(BitWidth, 0);
597 KnownOne2 = APInt(BitWidth, 0);
598 // Recurse, but cap the recursion to one level, because we don't
599 // want to waste time spinning around in loops.
600 ComputeMaskedBits(P->getIncomingValue(i), KnownZero | KnownOne,
601 KnownZero2, KnownOne2, TD, MaxDepth-1);
602 KnownZero &= KnownZero2;
603 KnownOne &= KnownOne2;
604 // If all bits have been ruled out, there's no need to check
605 // more operands.
606 if (!KnownZero && !KnownOne)
607 break;
608 }
609 }
Chris Lattner173234a2008-06-02 01:18:21 +0000610 break;
611 }
612 case Instruction::Call:
613 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
614 switch (II->getIntrinsicID()) {
615 default: break;
616 case Intrinsic::ctpop:
617 case Intrinsic::ctlz:
618 case Intrinsic::cttz: {
619 unsigned LowBits = Log2_32(BitWidth)+1;
620 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
621 break;
622 }
623 }
624 }
625 break;
626 }
627}
628
629/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
630/// this predicate to simplify operations downstream. Mask is known to be zero
631/// for bits that V cannot have.
Chris Lattnercf5128e2009-09-08 00:06:16 +0000632///
633/// This function is defined on values with integer type, values with pointer
634/// type (but only if TD is non-null), and vectors of integers. In the case
635/// where V is a vector, the mask, known zero, and known one values are the
636/// same width as the vector element, and the bit is set only if it is true
637/// for all of the elements in the vector.
Chris Lattner173234a2008-06-02 01:18:21 +0000638bool llvm::MaskedValueIsZero(Value *V, const APInt &Mask,
Dan Gohman846a2f22009-08-27 17:51:25 +0000639 const TargetData *TD, unsigned Depth) {
Chris Lattner173234a2008-06-02 01:18:21 +0000640 APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0);
641 ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth);
642 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
643 return (KnownZero & Mask) == Mask;
644}
645
646
647
648/// ComputeNumSignBits - Return the number of times the sign bit of the
649/// register is replicated into the other bits. We know that at least 1 bit
650/// is always equal to the sign bit (itself), but other cases can give us
651/// information. For example, immediately after an "ashr X, 2", we know that
652/// the top 3 bits are all equal to each other, so we return 3.
653///
654/// 'Op' must have a scalar integer type.
655///
Dan Gohman846a2f22009-08-27 17:51:25 +0000656unsigned llvm::ComputeNumSignBits(Value *V, const TargetData *TD,
657 unsigned Depth) {
Dan Gohmanbd5ce522009-06-22 22:02:32 +0000658 assert((TD || V->getType()->isIntOrIntVector()) &&
659 "ComputeNumSignBits requires a TargetData object to operate "
660 "on non-integer values!");
Dan Gohman6de29f82009-06-15 22:12:54 +0000661 const Type *Ty = V->getType();
Dan Gohmanbd5ce522009-06-22 22:02:32 +0000662 unsigned TyBits = TD ? TD->getTypeSizeInBits(V->getType()->getScalarType()) :
663 Ty->getScalarSizeInBits();
Chris Lattner173234a2008-06-02 01:18:21 +0000664 unsigned Tmp, Tmp2;
665 unsigned FirstAnswer = 1;
666
Chris Lattnerd82e5112008-06-02 18:39:07 +0000667 // Note that ConstantInt is handled by the general ComputeMaskedBits case
668 // below.
669
Chris Lattner173234a2008-06-02 01:18:21 +0000670 if (Depth == 6)
671 return 1; // Limit search depth.
672
Dan Gohmanca178902009-07-17 20:47:02 +0000673 Operator *U = dyn_cast<Operator>(V);
674 switch (Operator::getOpcode(V)) {
Chris Lattner173234a2008-06-02 01:18:21 +0000675 default: break;
676 case Instruction::SExt:
677 Tmp = TyBits-cast<IntegerType>(U->getOperand(0)->getType())->getBitWidth();
678 return ComputeNumSignBits(U->getOperand(0), TD, Depth+1) + Tmp;
679
680 case Instruction::AShr:
681 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
682 // ashr X, C -> adds C sign bits.
683 if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) {
684 Tmp += C->getZExtValue();
685 if (Tmp > TyBits) Tmp = TyBits;
686 }
687 return Tmp;
688 case Instruction::Shl:
689 if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) {
690 // shl destroys sign bits.
691 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
692 if (C->getZExtValue() >= TyBits || // Bad shift.
693 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
694 return Tmp - C->getZExtValue();
695 }
696 break;
697 case Instruction::And:
698 case Instruction::Or:
699 case Instruction::Xor: // NOT is handled here.
700 // Logical binary ops preserve the number of sign bits at the worst.
701 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
702 if (Tmp != 1) {
703 Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
704 FirstAnswer = std::min(Tmp, Tmp2);
705 // We computed what we know about the sign bits as our first
706 // answer. Now proceed to the generic code that uses
707 // ComputeMaskedBits, and pick whichever answer is better.
708 }
709 break;
710
711 case Instruction::Select:
712 Tmp = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
713 if (Tmp == 1) return 1; // Early out.
714 Tmp2 = ComputeNumSignBits(U->getOperand(2), TD, Depth+1);
715 return std::min(Tmp, Tmp2);
716
717 case Instruction::Add:
718 // Add can have at most one carry bit. Thus we know that the output
719 // is, at worst, one more bit than the inputs.
720 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
721 if (Tmp == 1) return 1; // Early out.
722
723 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +0000724 if (ConstantInt *CRHS = dyn_cast<ConstantInt>(U->getOperand(1)))
Chris Lattner173234a2008-06-02 01:18:21 +0000725 if (CRHS->isAllOnesValue()) {
726 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
727 APInt Mask = APInt::getAllOnesValue(TyBits);
728 ComputeMaskedBits(U->getOperand(0), Mask, KnownZero, KnownOne, TD,
729 Depth+1);
730
731 // If the input is known to be 0 or 1, the output is 0/-1, which is all
732 // sign bits set.
733 if ((KnownZero | APInt(TyBits, 1)) == Mask)
734 return TyBits;
735
736 // If we are subtracting one from a positive number, there is no carry
737 // out of the result.
738 if (KnownZero.isNegative())
739 return Tmp;
740 }
741
742 Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
743 if (Tmp2 == 1) return 1;
744 return std::min(Tmp, Tmp2)-1;
745 break;
746
747 case Instruction::Sub:
748 Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
749 if (Tmp2 == 1) return 1;
750
751 // Handle NEG.
752 if (ConstantInt *CLHS = dyn_cast<ConstantInt>(U->getOperand(0)))
753 if (CLHS->isNullValue()) {
754 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
755 APInt Mask = APInt::getAllOnesValue(TyBits);
756 ComputeMaskedBits(U->getOperand(1), Mask, KnownZero, KnownOne,
757 TD, Depth+1);
758 // If the input is known to be 0 or 1, the output is 0/-1, which is all
759 // sign bits set.
760 if ((KnownZero | APInt(TyBits, 1)) == Mask)
761 return TyBits;
762
763 // If the input is known to be positive (the sign bit is known clear),
764 // the output of the NEG has the same number of sign bits as the input.
765 if (KnownZero.isNegative())
766 return Tmp2;
767
768 // Otherwise, we treat this like a SUB.
769 }
770
771 // Sub can have at most one carry bit. Thus we know that the output
772 // is, at worst, one more bit than the inputs.
773 Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
774 if (Tmp == 1) return 1; // Early out.
775 return std::min(Tmp, Tmp2)-1;
776 break;
777 case Instruction::Trunc:
778 // FIXME: it's tricky to do anything useful for this, but it is an important
779 // case for targets like X86.
780 break;
781 }
782
783 // Finally, if we can prove that the top bits of the result are 0's or 1's,
784 // use this information.
785 APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
786 APInt Mask = APInt::getAllOnesValue(TyBits);
787 ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth);
788
789 if (KnownZero.isNegative()) { // sign bit is 0
790 Mask = KnownZero;
791 } else if (KnownOne.isNegative()) { // sign bit is 1;
792 Mask = KnownOne;
793 } else {
794 // Nothing known.
795 return FirstAnswer;
796 }
797
798 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
799 // the number of identical bits in the top of the input value.
800 Mask = ~Mask;
801 Mask <<= Mask.getBitWidth()-TyBits;
802 // Return # leading zeros. We use 'min' here in case Val was zero before
803 // shifting. We don't want to return '64' as for an i32 "0".
804 return std::max(FirstAnswer, std::min(TyBits, Mask.countLeadingZeros()));
805}
Chris Lattner833f25d2008-06-02 01:29:46 +0000806
807/// CannotBeNegativeZero - Return true if we can prove that the specified FP
808/// value is never equal to -0.0.
809///
810/// NOTE: this function will need to be revisited when we support non-default
811/// rounding modes!
812///
813bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) {
814 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
815 return !CFP->getValueAPF().isNegZero();
816
817 if (Depth == 6)
818 return 1; // Limit search depth.
819
Dan Gohmanca178902009-07-17 20:47:02 +0000820 const Operator *I = dyn_cast<Operator>(V);
Chris Lattner833f25d2008-06-02 01:29:46 +0000821 if (I == 0) return false;
822
823 // (add x, 0.0) is guaranteed to return +0.0, not -0.0.
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000824 if (I->getOpcode() == Instruction::FAdd &&
Chris Lattner833f25d2008-06-02 01:29:46 +0000825 isa<ConstantFP>(I->getOperand(1)) &&
826 cast<ConstantFP>(I->getOperand(1))->isNullValue())
827 return true;
828
829 // sitofp and uitofp turn into +0.0 for zero.
830 if (isa<SIToFPInst>(I) || isa<UIToFPInst>(I))
831 return true;
832
833 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
834 // sqrt(-0.0) = -0.0, no other negative results are possible.
835 if (II->getIntrinsicID() == Intrinsic::sqrt)
836 return CannotBeNegativeZero(II->getOperand(1), Depth+1);
837
838 if (const CallInst *CI = dyn_cast<CallInst>(I))
839 if (const Function *F = CI->getCalledFunction()) {
840 if (F->isDeclaration()) {
Daniel Dunbarf0443c12009-07-26 08:34:35 +0000841 // abs(x) != -0.0
842 if (F->getName() == "abs") return true;
Dale Johannesen9d061752009-09-25 20:54:50 +0000843 // fabs[lf](x) != -0.0
844 if (F->getName() == "fabs") return true;
845 if (F->getName() == "fabsf") return true;
846 if (F->getName() == "fabsl") return true;
847 if (F->getName() == "sqrt" || F->getName() == "sqrtf" ||
848 F->getName() == "sqrtl")
849 return CannotBeNegativeZero(CI->getOperand(1), Depth+1);
Chris Lattner833f25d2008-06-02 01:29:46 +0000850 }
851 }
852
853 return false;
854}
855
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000856// This is the recursive version of BuildSubAggregate. It takes a few different
857// arguments. Idxs is the index within the nested struct From that we are
858// looking at now (which is of type IndexedType). IdxSkip is the number of
859// indices from Idxs that should be left out when inserting into the resulting
860// struct. To is the result struct built so far, new insertvalue instructions
861// build on that.
Dan Gohman7db949d2009-08-07 01:32:21 +0000862static Value *BuildSubAggregate(Value *From, Value* To, const Type *IndexedType,
863 SmallVector<unsigned, 10> &Idxs,
864 unsigned IdxSkip,
865 LLVMContext &Context,
866 Instruction *InsertBefore) {
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000867 const llvm::StructType *STy = llvm::dyn_cast<llvm::StructType>(IndexedType);
868 if (STy) {
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +0000869 // Save the original To argument so we can modify it
870 Value *OrigTo = To;
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000871 // General case, the type indexed by Idxs is a struct
872 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
873 // Process each struct element recursively
874 Idxs.push_back(i);
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +0000875 Value *PrevTo = To;
Matthijs Kooijman710eb232008-06-16 12:57:37 +0000876 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
Owen Anderson76f600b2009-07-06 22:37:39 +0000877 Context, InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000878 Idxs.pop_back();
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +0000879 if (!To) {
880 // Couldn't find any inserted value for this index? Cleanup
881 while (PrevTo != OrigTo) {
882 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
883 PrevTo = Del->getAggregateOperand();
884 Del->eraseFromParent();
885 }
886 // Stop processing elements
887 break;
888 }
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000889 }
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +0000890 // If we succesfully found a value for each of our subaggregates
891 if (To)
892 return To;
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000893 }
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +0000894 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
895 // the struct's elements had a value that was inserted directly. In the latter
896 // case, perhaps we can't determine each of the subelements individually, but
897 // we might be able to find the complete struct somewhere.
898
899 // Find the value that is at that particular spot
Owen Anderson76f600b2009-07-06 22:37:39 +0000900 Value *V = FindInsertedValue(From, Idxs.begin(), Idxs.end(), Context);
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +0000901
902 if (!V)
903 return NULL;
904
905 // Insert the value in the new (sub) aggregrate
906 return llvm::InsertValueInst::Create(To, V, Idxs.begin() + IdxSkip,
907 Idxs.end(), "tmp", InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000908}
909
910// This helper takes a nested struct and extracts a part of it (which is again a
911// struct) into a new value. For example, given the struct:
912// { a, { b, { c, d }, e } }
913// and the indices "1, 1" this returns
914// { c, d }.
915//
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +0000916// It does this by inserting an insertvalue for each element in the resulting
917// struct, as opposed to just inserting a single struct. This will only work if
918// each of the elements of the substruct are known (ie, inserted into From by an
919// insertvalue instruction somewhere).
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000920//
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +0000921// All inserted insertvalue instructions are inserted before InsertBefore
Dan Gohman7db949d2009-08-07 01:32:21 +0000922static Value *BuildSubAggregate(Value *From, const unsigned *idx_begin,
923 const unsigned *idx_end, LLVMContext &Context,
924 Instruction *InsertBefore) {
Matthijs Kooijman97728912008-06-16 13:28:31 +0000925 assert(InsertBefore && "Must have someplace to insert!");
Matthijs Kooijman710eb232008-06-16 12:57:37 +0000926 const Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
927 idx_begin,
928 idx_end);
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000929 Value *To = UndefValue::get(IndexedType);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000930 SmallVector<unsigned, 10> Idxs(idx_begin, idx_end);
931 unsigned IdxSkip = Idxs.size();
932
Owen Anderson76f600b2009-07-06 22:37:39 +0000933 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip,
934 Context, InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000935}
936
Matthijs Kooijman710eb232008-06-16 12:57:37 +0000937/// FindInsertedValue - Given an aggregrate and an sequence of indices, see if
938/// the scalar value indexed is already around as a register, for example if it
939/// were inserted directly into the aggregrate.
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +0000940///
941/// If InsertBefore is not null, this function will duplicate (modified)
942/// insertvalues when a part of a nested struct is extracted.
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000943Value *llvm::FindInsertedValue(Value *V, const unsigned *idx_begin,
Owen Andersone922c022009-07-22 00:24:57 +0000944 const unsigned *idx_end, LLVMContext &Context,
Owen Anderson76f600b2009-07-06 22:37:39 +0000945 Instruction *InsertBefore) {
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000946 // Nothing to index? Just return V then (this is useful at the end of our
947 // recursion)
948 if (idx_begin == idx_end)
949 return V;
950 // We have indices, so V should have an indexable type
951 assert((isa<StructType>(V->getType()) || isa<ArrayType>(V->getType()))
952 && "Not looking at a struct or array?");
953 assert(ExtractValueInst::getIndexedType(V->getType(), idx_begin, idx_end)
954 && "Invalid indices for type?");
955 const CompositeType *PTy = cast<CompositeType>(V->getType());
Owen Anderson76f600b2009-07-06 22:37:39 +0000956
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000957 if (isa<UndefValue>(V))
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000958 return UndefValue::get(ExtractValueInst::getIndexedType(PTy,
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000959 idx_begin,
960 idx_end));
961 else if (isa<ConstantAggregateZero>(V))
Owen Andersona7235ea2009-07-31 20:28:14 +0000962 return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy,
Owen Anderson76f600b2009-07-06 22:37:39 +0000963 idx_begin,
964 idx_end));
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000965 else if (Constant *C = dyn_cast<Constant>(V)) {
966 if (isa<ConstantArray>(C) || isa<ConstantStruct>(C))
967 // Recursively process this constant
Owen Anderson76f600b2009-07-06 22:37:39 +0000968 return FindInsertedValue(C->getOperand(*idx_begin), idx_begin + 1,
969 idx_end, Context, InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000970 } else if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
971 // Loop the indices for the insertvalue instruction in parallel with the
972 // requested indices
973 const unsigned *req_idx = idx_begin;
Matthijs Kooijman710eb232008-06-16 12:57:37 +0000974 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
975 i != e; ++i, ++req_idx) {
Duncan Sands9954c762008-06-19 08:47:31 +0000976 if (req_idx == idx_end) {
Matthijs Kooijman97728912008-06-16 13:28:31 +0000977 if (InsertBefore)
Matthijs Kooijman0a9aaf42008-06-16 14:13:46 +0000978 // The requested index identifies a part of a nested aggregate. Handle
979 // this specially. For example,
980 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
981 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
982 // %C = extractvalue {i32, { i32, i32 } } %B, 1
983 // This can be changed into
984 // %A = insertvalue {i32, i32 } undef, i32 10, 0
985 // %C = insertvalue {i32, i32 } %A, i32 11, 1
986 // which allows the unused 0,0 element from the nested struct to be
987 // removed.
Owen Anderson76f600b2009-07-06 22:37:39 +0000988 return BuildSubAggregate(V, idx_begin, req_idx,
989 Context, InsertBefore);
Matthijs Kooijman97728912008-06-16 13:28:31 +0000990 else
991 // We can't handle this without inserting insertvalues
992 return 0;
Duncan Sands9954c762008-06-19 08:47:31 +0000993 }
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +0000994
995 // This insert value inserts something else than what we are looking for.
996 // See if the (aggregrate) value inserted into has the value we are
997 // looking for, then.
998 if (*req_idx != *i)
Matthijs Kooijman710eb232008-06-16 12:57:37 +0000999 return FindInsertedValue(I->getAggregateOperand(), idx_begin, idx_end,
Owen Anderson76f600b2009-07-06 22:37:39 +00001000 Context, InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001001 }
1002 // If we end up here, the indices of the insertvalue match with those
1003 // requested (though possibly only partially). Now we recursively look at
1004 // the inserted value, passing any remaining indices.
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001005 return FindInsertedValue(I->getInsertedValueOperand(), req_idx, idx_end,
Owen Anderson76f600b2009-07-06 22:37:39 +00001006 Context, InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001007 } else if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
1008 // If we're extracting a value from an aggregrate that was extracted from
1009 // something else, we can extract from that something else directly instead.
1010 // However, we will need to chain I's indices with the requested indices.
1011
1012 // Calculate the number of indices required
1013 unsigned size = I->getNumIndices() + (idx_end - idx_begin);
1014 // Allocate some space to put the new indices in
Matthijs Kooijman3faf9df2008-06-17 08:24:37 +00001015 SmallVector<unsigned, 5> Idxs;
1016 Idxs.reserve(size);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001017 // Add indices from the extract value instruction
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001018 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
Matthijs Kooijman3faf9df2008-06-17 08:24:37 +00001019 i != e; ++i)
1020 Idxs.push_back(*i);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001021
1022 // Add requested indices
Matthijs Kooijman3faf9df2008-06-17 08:24:37 +00001023 for (const unsigned *i = idx_begin, *e = idx_end; i != e; ++i)
1024 Idxs.push_back(*i);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001025
Matthijs Kooijman3faf9df2008-06-17 08:24:37 +00001026 assert(Idxs.size() == size
Matthijs Kooijman710eb232008-06-16 12:57:37 +00001027 && "Number of indices added not correct?");
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001028
Matthijs Kooijman3faf9df2008-06-17 08:24:37 +00001029 return FindInsertedValue(I->getAggregateOperand(), Idxs.begin(), Idxs.end(),
Owen Anderson76f600b2009-07-06 22:37:39 +00001030 Context, InsertBefore);
Matthijs Kooijmanb23d5ad2008-06-16 12:48:21 +00001031 }
1032 // Otherwise, we don't know (such as, extracting from a function return value
1033 // or load instruction)
1034 return 0;
1035}
Evan Cheng0ff39b32008-06-30 07:31:25 +00001036
1037/// GetConstantStringInfo - This function computes the length of a
1038/// null-terminated C string pointed to by V. If successful, it returns true
1039/// and returns the string in Str. If unsuccessful, it returns false.
Bill Wendling0582ae92009-03-13 04:39:26 +00001040bool llvm::GetConstantStringInfo(Value *V, std::string &Str, uint64_t Offset,
1041 bool StopAtNul) {
1042 // If V is NULL then return false;
1043 if (V == NULL) return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001044
1045 // Look through bitcast instructions.
1046 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Bill Wendling0582ae92009-03-13 04:39:26 +00001047 return GetConstantStringInfo(BCI->getOperand(0), Str, Offset, StopAtNul);
1048
Evan Cheng0ff39b32008-06-30 07:31:25 +00001049 // If the value is not a GEP instruction nor a constant expression with a
1050 // GEP instruction, then return false because ConstantArray can't occur
1051 // any other way
1052 User *GEP = 0;
1053 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
1054 GEP = GEPI;
1055 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1056 if (CE->getOpcode() == Instruction::BitCast)
Bill Wendling0582ae92009-03-13 04:39:26 +00001057 return GetConstantStringInfo(CE->getOperand(0), Str, Offset, StopAtNul);
1058 if (CE->getOpcode() != Instruction::GetElementPtr)
1059 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001060 GEP = CE;
1061 }
1062
1063 if (GEP) {
1064 // Make sure the GEP has exactly three arguments.
Bill Wendling0582ae92009-03-13 04:39:26 +00001065 if (GEP->getNumOperands() != 3)
1066 return false;
1067
Evan Cheng0ff39b32008-06-30 07:31:25 +00001068 // Make sure the index-ee is a pointer to array of i8.
1069 const PointerType *PT = cast<PointerType>(GEP->getOperand(0)->getType());
1070 const ArrayType *AT = dyn_cast<ArrayType>(PT->getElementType());
Owen Anderson1d0be152009-08-13 21:58:54 +00001071 if (AT == 0 || AT->getElementType() != Type::getInt8Ty(V->getContext()))
Bill Wendling0582ae92009-03-13 04:39:26 +00001072 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001073
1074 // Check to make sure that the first operand of the GEP is an integer and
1075 // has value 0 so that we are sure we're indexing into the initializer.
1076 ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
Bill Wendling0582ae92009-03-13 04:39:26 +00001077 if (FirstIdx == 0 || !FirstIdx->isZero())
1078 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001079
1080 // If the second index isn't a ConstantInt, then this is a variable index
1081 // into the array. If this occurs, we can't say anything meaningful about
1082 // the string.
1083 uint64_t StartIdx = 0;
Bill Wendling0582ae92009-03-13 04:39:26 +00001084 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
Evan Cheng0ff39b32008-06-30 07:31:25 +00001085 StartIdx = CI->getZExtValue();
Bill Wendling0582ae92009-03-13 04:39:26 +00001086 else
1087 return false;
1088 return GetConstantStringInfo(GEP->getOperand(0), Str, StartIdx+Offset,
Evan Cheng0ff39b32008-06-30 07:31:25 +00001089 StopAtNul);
1090 }
1091
Torok Edwin148843b2009-09-02 11:13:56 +00001092 if (MDString *MDStr = dyn_cast<MDString>(V)) {
1093 Str = MDStr->getString();
1094 return true;
1095 }
1096
Evan Cheng0ff39b32008-06-30 07:31:25 +00001097 // The GEP instruction, constant or instruction, must reference a global
1098 // variable that is a constant and is initialized. The referenced constant
1099 // initializer is the array that we'll use for optimization.
1100 GlobalVariable* GV = dyn_cast<GlobalVariable>(V);
Dan Gohman82555732009-08-19 18:20:44 +00001101 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
Bill Wendling0582ae92009-03-13 04:39:26 +00001102 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001103 Constant *GlobalInit = GV->getInitializer();
1104
1105 // Handle the ConstantAggregateZero case
Bill Wendling0582ae92009-03-13 04:39:26 +00001106 if (isa<ConstantAggregateZero>(GlobalInit)) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00001107 // This is a degenerate case. The initializer is constant zero so the
1108 // length of the string must be zero.
Bill Wendling0582ae92009-03-13 04:39:26 +00001109 Str.clear();
1110 return true;
1111 }
Evan Cheng0ff39b32008-06-30 07:31:25 +00001112
1113 // Must be a Constant Array
1114 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
Owen Anderson1d0be152009-08-13 21:58:54 +00001115 if (Array == 0 ||
1116 Array->getType()->getElementType() != Type::getInt8Ty(V->getContext()))
Bill Wendling0582ae92009-03-13 04:39:26 +00001117 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001118
1119 // Get the number of elements in the array
1120 uint64_t NumElts = Array->getType()->getNumElements();
1121
Bill Wendling0582ae92009-03-13 04:39:26 +00001122 if (Offset > NumElts)
1123 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001124
1125 // Traverse the constant array from 'Offset' which is the place the GEP refers
1126 // to in the array.
Bill Wendling0582ae92009-03-13 04:39:26 +00001127 Str.reserve(NumElts-Offset);
Evan Cheng0ff39b32008-06-30 07:31:25 +00001128 for (unsigned i = Offset; i != NumElts; ++i) {
1129 Constant *Elt = Array->getOperand(i);
1130 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
Bill Wendling0582ae92009-03-13 04:39:26 +00001131 if (!CI) // This array isn't suitable, non-int initializer.
1132 return false;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001133 if (StopAtNul && CI->isZero())
Bill Wendling0582ae92009-03-13 04:39:26 +00001134 return true; // we found end of string, success!
1135 Str += (char)CI->getZExtValue();
Evan Cheng0ff39b32008-06-30 07:31:25 +00001136 }
Bill Wendling0582ae92009-03-13 04:39:26 +00001137
Evan Cheng0ff39b32008-06-30 07:31:25 +00001138 // The array isn't null terminated, but maybe this is a memcpy, not a strcpy.
Bill Wendling0582ae92009-03-13 04:39:26 +00001139 return true;
Evan Cheng0ff39b32008-06-30 07:31:25 +00001140}