1 //===- 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"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/LLVMContext.h"
21 #include "llvm/Operator.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Support/GetElementPtrTypeIterator.h"
24 #include "llvm/Support/MathExtras.h"
25 #include <cstring>
26 using namespace llvm;
27 
28 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
29 /// known to be either zero or one and return them in the KnownZero/KnownOne
30 /// bit sets.  This code only analyzes bits in Mask, in order to short-circuit
31 /// processing.
32 /// NOTE: we cannot consider 'undef' to be "IsZero" here.  The problem is that
33 /// we cannot optimize based on the assumption that it is zero without changing
34 /// it to be an explicit zero.  If we don't change it to zero, other code could
35 /// optimized based on the contradictory assumption that it is non-zero.
36 /// Because instcombine aggressively folds operations with undef args anyway,
37 /// this won't lose us code quality.
38 ///
39 /// This function is defined on values with integer type, values with pointer
40 /// type (but only if TD is non-null), and vectors of integers.  In the case
41 /// where V is a vector, the mask, known zero, and known one values are the
42 /// same width as the vector element, and the bit is set only if it is true
43 /// for all of the elements in the vector.
44 void llvm::ComputeMaskedBits(Value *V, const APInt &Mask,
45                              APInt &KnownZero, APInt &KnownOne,
46                              const TargetData *TD, unsigned Depth) {
47   const unsigned MaxDepth = 6;
48   assert(V && "No Value?");
49   assert(Depth <= MaxDepth && "Limit Search Depth");
50   unsigned BitWidth = Mask.getBitWidth();
51   assert((V->getType()->isIntOrIntVector() || isa<PointerType>(V->getType())) &&
52          "Not integer or pointer type!");
53   assert((!TD ||
54           TD->getTypeSizeInBits(V->getType()->getScalarType()) == BitWidth) &&
55          (!V->getType()->isIntOrIntVector() ||
56           V->getType()->getScalarSizeInBits() == BitWidth) &&
57          KnownZero.getBitWidth() == BitWidth &&
58          KnownOne.getBitWidth() == BitWidth &&
59          "V, Mask, KnownOne and KnownZero should have same BitWidth");
60 
61   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
62     // We know all of the bits for a constant!
63     KnownOne = CI->getValue() & Mask;
64     KnownZero = ~KnownOne & Mask;
65     return;
66   }
67   // Null and aggregate-zero are all-zeros.
68   if (isa<ConstantPointerNull>(V) ||
69       isa<ConstantAggregateZero>(V)) {
70     KnownOne.clear();
71     KnownZero = Mask;
72     return;
73   }
74   // Handle a constant vector by taking the intersection of the known bits of
75   // each element.
76   if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
77     KnownZero.set(); KnownOne.set();
78     for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
79       APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
80       ComputeMaskedBits(CV->getOperand(i), Mask, KnownZero2, KnownOne2,
81                         TD, Depth);
82       KnownZero &= KnownZero2;
83       KnownOne &= KnownOne2;
84     }
85     return;
86   }
87   // The address of an aligned GlobalValue has trailing zeros.
88   if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
89     unsigned Align = GV->getAlignment();
90     if (Align == 0 && TD && GV->getType()->getElementType()->isSized()) {
91       const Type *ObjectType = GV->getType()->getElementType();
92       // If the object is defined in the current Module, we'll be giving
93       // it the preferred alignment. Otherwise, we have to assume that it
94       // may only have the minimum ABI alignment.
95       if (!GV->isDeclaration() && !GV->mayBeOverridden())
96         Align = TD->getPrefTypeAlignment(ObjectType);
97       else
98         Align = TD->getABITypeAlignment(ObjectType);
99     }
100     if (Align > 0)
101       KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
102                                               CountTrailingZeros_32(Align));
103     else
104       KnownZero.clear();
105     KnownOne.clear();
106     return;
107   }
108 
109   KnownZero.clear(); KnownOne.clear();   // Start out not knowing anything.
110 
111   if (Depth == MaxDepth || Mask == 0)
112     return;  // Limit search depth.
113 
114   Operator *I = dyn_cast<Operator>(V);
115   if (!I) return;
116 
117   APInt KnownZero2(KnownZero), KnownOne2(KnownOne);
118   switch (I->getOpcode()) {
119   default: break;
120   case Instruction::And: {
121     // If either the LHS or the RHS are Zero, the result is zero.
122     ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1);
123     APInt Mask2(Mask & ~KnownZero);
124     ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
125                       Depth+1);
126     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
127     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
128 
129     // Output known-1 bits are only known if set in both the LHS & RHS.
130     KnownOne &= KnownOne2;
131     // Output known-0 are known to be clear if zero in either the LHS | RHS.
132     KnownZero |= KnownZero2;
133     return;
134   }
135   case Instruction::Or: {
136     ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1);
137     APInt Mask2(Mask & ~KnownOne);
138     ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
139                       Depth+1);
140     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
141     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
142 
143     // Output known-0 bits are only known if clear in both the LHS & RHS.
144     KnownZero &= KnownZero2;
145     // Output known-1 are known to be set if set in either the LHS | RHS.
146     KnownOne |= KnownOne2;
147     return;
148   }
149   case Instruction::Xor: {
150     ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1);
151     ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, TD,
152                       Depth+1);
153     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
154     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
155 
156     // Output known-0 bits are known if clear or set in both the LHS & RHS.
157     APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
158     // Output known-1 are known to be set if set in only one of the LHS, RHS.
159     KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
160     KnownZero = KnownZeroOut;
161     return;
162   }
163   case Instruction::Mul: {
164     APInt Mask2 = APInt::getAllOnesValue(BitWidth);
165     ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero, KnownOne, TD,Depth+1);
166     ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
167                       Depth+1);
168     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
169     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
170 
171     // If low bits are zero in either operand, output low known-0 bits.
172     // Also compute a conserative estimate for high known-0 bits.
173     // More trickiness is possible, but this is sufficient for the
174     // interesting case of alignment computation.
175     KnownOne.clear();
176     unsigned TrailZ = KnownZero.countTrailingOnes() +
177                       KnownZero2.countTrailingOnes();
178     unsigned LeadZ =  std::max(KnownZero.countLeadingOnes() +
179                                KnownZero2.countLeadingOnes(),
180                                BitWidth) - BitWidth;
181 
182     TrailZ = std::min(TrailZ, BitWidth);
183     LeadZ = std::min(LeadZ, BitWidth);
184     KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
185                 APInt::getHighBitsSet(BitWidth, LeadZ);
186     KnownZero &= Mask;
187     return;
188   }
189   case Instruction::UDiv: {
190     // For the purposes of computing leading zeros we can conservatively
191     // treat a udiv as a logical right shift by the power of 2 known to
192     // be less than the denominator.
193     APInt AllOnes = APInt::getAllOnesValue(BitWidth);
194     ComputeMaskedBits(I->getOperand(0),
195                       AllOnes, KnownZero2, KnownOne2, TD, Depth+1);
196     unsigned LeadZ = KnownZero2.countLeadingOnes();
197 
198     KnownOne2.clear();
199     KnownZero2.clear();
200     ComputeMaskedBits(I->getOperand(1),
201                       AllOnes, KnownZero2, KnownOne2, TD, Depth+1);
202     unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
203     if (RHSUnknownLeadingOnes != BitWidth)
204       LeadZ = std::min(BitWidth,
205                        LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
206 
207     KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
208     return;
209   }
210   case Instruction::Select:
211     ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, TD, Depth+1);
212     ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, TD,
213                       Depth+1);
214     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
215     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
216 
217     // Only known if known in both the LHS and RHS.
218     KnownOne &= KnownOne2;
219     KnownZero &= KnownZero2;
220     return;
221   case Instruction::FPTrunc:
222   case Instruction::FPExt:
223   case Instruction::FPToUI:
224   case Instruction::FPToSI:
225   case Instruction::SIToFP:
226   case Instruction::UIToFP:
227     return; // Can't work with floating point.
228   case Instruction::PtrToInt:
229   case Instruction::IntToPtr:
230     // We can't handle these if we don't know the pointer size.
231     if (!TD) return;
232     // FALL THROUGH and handle them the same as zext/trunc.
233   case Instruction::ZExt:
234   case Instruction::Trunc: {
235     const Type *SrcTy = I->getOperand(0)->getType();
236 
237     unsigned SrcBitWidth;
238     // Note that we handle pointer operands here because of inttoptr/ptrtoint
239     // which fall through here.
240     if (isa<PointerType>(SrcTy))
241       SrcBitWidth = TD->getTypeSizeInBits(SrcTy);
242     else
243       SrcBitWidth = SrcTy->getScalarSizeInBits();
244 
245     APInt MaskIn(Mask);
246     MaskIn.zextOrTrunc(SrcBitWidth);
247     KnownZero.zextOrTrunc(SrcBitWidth);
248     KnownOne.zextOrTrunc(SrcBitWidth);
249     ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, TD,
250                       Depth+1);
251     KnownZero.zextOrTrunc(BitWidth);
252     KnownOne.zextOrTrunc(BitWidth);
253     // Any top bits are known to be zero.
254     if (BitWidth > SrcBitWidth)
255       KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
256     return;
257   }
258   case Instruction::BitCast: {
259     const Type *SrcTy = I->getOperand(0)->getType();
260     if ((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
261         // TODO: For now, not handling conversions like:
262         // (bitcast i64 %x to <2 x i32>)
263         !isa<VectorType>(I->getType())) {
264       ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, TD,
265                         Depth+1);
266       return;
267     }
268     break;
269   }
270   case Instruction::SExt: {
271     // Compute the bits in the result that are not present in the input.
272     unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
273 
274     APInt MaskIn(Mask);
275     MaskIn.trunc(SrcBitWidth);
276     KnownZero.trunc(SrcBitWidth);
277     KnownOne.trunc(SrcBitWidth);
278     ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, TD,
279                       Depth+1);
280     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
281     KnownZero.zext(BitWidth);
282     KnownOne.zext(BitWidth);
283 
284     // If the sign bit of the input is known set or clear, then we know the
285     // top bits of the result.
286     if (KnownZero[SrcBitWidth-1])             // Input sign bit known zero
287       KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
288     else if (KnownOne[SrcBitWidth-1])           // Input sign bit known set
289       KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
290     return;
291   }
292   case Instruction::Shl:
293     // (shl X, C1) & C2 == 0   iff   (X & C2 >>u C1) == 0
294     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
295       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
296       APInt Mask2(Mask.lshr(ShiftAmt));
297       ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD,
298                         Depth+1);
299       assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
300       KnownZero <<= ShiftAmt;
301       KnownOne  <<= ShiftAmt;
302       KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0
303       return;
304     }
305     break;
306   case Instruction::LShr:
307     // (ushr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0
308     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
309       // Compute the new bits that are at the top now.
310       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
311 
312       // Unsigned shift right.
313       APInt Mask2(Mask.shl(ShiftAmt));
314       ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne, TD,
315                         Depth+1);
316       assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
317       KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
318       KnownOne  = APIntOps::lshr(KnownOne, ShiftAmt);
319       // high bits known zero.
320       KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
321       return;
322     }
323     break;
324   case Instruction::AShr:
325     // (ashr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0
326     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
327       // Compute the new bits that are at the top now.
328       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
329 
330       // Signed shift right.
331       APInt Mask2(Mask.shl(ShiftAmt));
332       ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD,
333                         Depth+1);
334       assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
335       KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
336       KnownOne  = APIntOps::lshr(KnownOne, ShiftAmt);
337 
338       APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
339       if (KnownZero[BitWidth-ShiftAmt-1])    // New bits are known zero.
340         KnownZero |= HighBits;
341       else if (KnownOne[BitWidth-ShiftAmt-1])  // New bits are known one.
342         KnownOne |= HighBits;
343       return;
344     }
345     break;
346   case Instruction::Sub: {
347     if (ConstantInt *CLHS = dyn_cast<ConstantInt>(I->getOperand(0))) {
348       // We know that the top bits of C-X are clear if X contains less bits
349       // than C (i.e. no wrap-around can happen).  For example, 20-X is
350       // positive if we can prove that X is >= 0 and < 16.
351       if (!CLHS->getValue().isNegative()) {
352         unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros();
353         // NLZ can't be BitWidth with no sign bit
354         APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
355         ComputeMaskedBits(I->getOperand(1), MaskV, KnownZero2, KnownOne2,
356                           TD, Depth+1);
357 
358         // If all of the MaskV bits are known to be zero, then we know the
359         // output top bits are zero, because we now know that the output is
360         // from [0-C].
361         if ((KnownZero2 & MaskV) == MaskV) {
362           unsigned NLZ2 = CLHS->getValue().countLeadingZeros();
363           // Top bits known zero.
364           KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
365         }
366       }
367     }
368   }
369   // fall through
370   case Instruction::Add: {
371     // If one of the operands has trailing zeros, than the bits that the
372     // other operand has in those bit positions will be preserved in the
373     // result. For an add, this works with either operand. For a subtract,
374     // this only works if the known zeros are in the right operand.
375     APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
376     APInt Mask2 = APInt::getLowBitsSet(BitWidth,
377                                        BitWidth - Mask.countLeadingZeros());
378     ComputeMaskedBits(I->getOperand(0), Mask2, LHSKnownZero, LHSKnownOne, TD,
379                       Depth+1);
380     assert((LHSKnownZero & LHSKnownOne) == 0 &&
381            "Bits known to be one AND zero?");
382     unsigned LHSKnownZeroOut = LHSKnownZero.countTrailingOnes();
383 
384     ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero2, KnownOne2, TD,
385                       Depth+1);
386     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
387     unsigned RHSKnownZeroOut = KnownZero2.countTrailingOnes();
388 
389     // Determine which operand has more trailing zeros, and use that
390     // many bits from the other operand.
391     if (LHSKnownZeroOut > RHSKnownZeroOut) {
392       if (I->getOpcode() == Instruction::Add) {
393         APInt Mask = APInt::getLowBitsSet(BitWidth, LHSKnownZeroOut);
394         KnownZero |= KnownZero2 & Mask;
395         KnownOne  |= KnownOne2 & Mask;
396       } else {
397         // If the known zeros are in the left operand for a subtract,
398         // fall back to the minimum known zeros in both operands.
399         KnownZero |= APInt::getLowBitsSet(BitWidth,
400                                           std::min(LHSKnownZeroOut,
401                                                    RHSKnownZeroOut));
402       }
403     } else if (RHSKnownZeroOut >= LHSKnownZeroOut) {
404       APInt Mask = APInt::getLowBitsSet(BitWidth, RHSKnownZeroOut);
405       KnownZero |= LHSKnownZero & Mask;
406       KnownOne  |= LHSKnownOne & Mask;
407     }
408     return;
409   }
410   case Instruction::SRem:
411     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
412       APInt RA = Rem->getValue();
413       if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
414         APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
415         APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
416         ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD,
417                           Depth+1);
418 
419         // If the sign bit of the first operand is zero, the sign bit of
420         // the result is zero. If the first operand has no one bits below
421         // the second operand's single 1 bit, its sign will be zero.
422         if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
423           KnownZero2 |= ~LowBits;
424 
425         KnownZero |= KnownZero2 & Mask;
426 
427         assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
428       }
429     }
430     break;
431   case Instruction::URem: {
432     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
433       APInt RA = Rem->getValue();
434       if (RA.isPowerOf2()) {
435         APInt LowBits = (RA - 1);
436         APInt Mask2 = LowBits & Mask;
437         KnownZero |= ~LowBits & Mask;
438         ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD,
439                           Depth+1);
440         assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
441         break;
442       }
443     }
444 
445     // Since the result is less than or equal to either operand, any leading
446     // zero bits in either operand must also exist in the result.
447     APInt AllOnes = APInt::getAllOnesValue(BitWidth);
448     ComputeMaskedBits(I->getOperand(0), AllOnes, KnownZero, KnownOne,
449                       TD, Depth+1);
450     ComputeMaskedBits(I->getOperand(1), AllOnes, KnownZero2, KnownOne2,
451                       TD, Depth+1);
452 
453     unsigned Leaders = std::max(KnownZero.countLeadingOnes(),
454                                 KnownZero2.countLeadingOnes());
455     KnownOne.clear();
456     KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
457     break;
458   }
459 
460   case Instruction::Alloca:
461   case Instruction::Malloc: {
462     AllocationInst *AI = cast<AllocationInst>(V);
463     unsigned Align = AI->getAlignment();
464     if (Align == 0 && TD) {
465       if (isa<AllocaInst>(AI))
466         Align = TD->getABITypeAlignment(AI->getType()->getElementType());
467       else if (isa<MallocInst>(AI)) {
468         // Malloc returns maximally aligned memory.
469         Align = TD->getABITypeAlignment(AI->getType()->getElementType());
470         Align =
471           std::max(Align,
472                    (unsigned)TD->getABITypeAlignment(
473                      Type::getDoubleTy(V->getContext())));
474         Align =
475           std::max(Align,
476                    (unsigned)TD->getABITypeAlignment(
477                       Type::getInt64Ty(V->getContext())));
478       }
479     }
480 
481     if (Align > 0)
482       KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
483                                               CountTrailingZeros_32(Align));
484     break;
485   }
486   case Instruction::GetElementPtr: {
487     // Analyze all of the subscripts of this getelementptr instruction
488     // to determine if we can prove known low zero bits.
489     APInt LocalMask = APInt::getAllOnesValue(BitWidth);
490     APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0);
491     ComputeMaskedBits(I->getOperand(0), LocalMask,
492                       LocalKnownZero, LocalKnownOne, TD, Depth+1);
493     unsigned TrailZ = LocalKnownZero.countTrailingOnes();
494 
495     gep_type_iterator GTI = gep_type_begin(I);
496     for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
497       Value *Index = I->getOperand(i);
498       if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
499         // Handle struct member offset arithmetic.
500         if (!TD) return;
501         const StructLayout *SL = TD->getStructLayout(STy);
502         unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
503         uint64_t Offset = SL->getElementOffset(Idx);
504         TrailZ = std::min(TrailZ,
505                           CountTrailingZeros_64(Offset));
506       } else {
507         // Handle array index arithmetic.
508         const Type *IndexedTy = GTI.getIndexedType();
509         if (!IndexedTy->isSized()) return;
510         unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits();
511         uint64_t TypeSize = TD ? TD->getTypeAllocSize(IndexedTy) : 1;
512         LocalMask = APInt::getAllOnesValue(GEPOpiBits);
513         LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0);
514         ComputeMaskedBits(Index, LocalMask,
515                           LocalKnownZero, LocalKnownOne, TD, Depth+1);
516         TrailZ = std::min(TrailZ,
517                           unsigned(CountTrailingZeros_64(TypeSize) +
518                                    LocalKnownZero.countTrailingOnes()));
519       }
520     }
521 
522     KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) & Mask;
523     break;
524   }
525   case Instruction::PHI: {
526     PHINode *P = cast<PHINode>(I);
527     // Handle the case of a simple two-predecessor recurrence PHI.
528     // There's a lot more that could theoretically be done here, but
529     // this is sufficient to catch some interesting cases.
530     if (P->getNumIncomingValues() == 2) {
531       for (unsigned i = 0; i != 2; ++i) {
532         Value *L = P->getIncomingValue(i);
533         Value *R = P->getIncomingValue(!i);
534         Operator *LU = dyn_cast<Operator>(L);
535         if (!LU)
536           continue;
537         unsigned Opcode = LU->getOpcode();
538         // Check for operations that have the property that if
539         // both their operands have low zero bits, the result
540         // will have low zero bits.
541         if (Opcode == Instruction::Add ||
542             Opcode == Instruction::Sub ||
543             Opcode == Instruction::And ||
544             Opcode == Instruction::Or ||
545             Opcode == Instruction::Mul) {
546           Value *LL = LU->getOperand(0);
547           Value *LR = LU->getOperand(1);
548           // Find a recurrence.
549           if (LL == I)
550             L = LR;
551           else if (LR == I)
552             L = LL;
553           else
554             break;
555           // Ok, we have a PHI of the form L op= R. Check for low
556           // zero bits.
557           APInt Mask2 = APInt::getAllOnesValue(BitWidth);
558           ComputeMaskedBits(R, Mask2, KnownZero2, KnownOne2, TD, Depth+1);
559           Mask2 = APInt::getLowBitsSet(BitWidth,
560                                        KnownZero2.countTrailingOnes());
561 
562           // We need to take the minimum number of known bits
563           APInt KnownZero3(KnownZero), KnownOne3(KnownOne);
564           ComputeMaskedBits(L, Mask2, KnownZero3, KnownOne3, TD, Depth+1);
565 
566           KnownZero = Mask &
567                       APInt::getLowBitsSet(BitWidth,
568                                            std::min(KnownZero2.countTrailingOnes(),
569                                                     KnownZero3.countTrailingOnes()));
570           break;
571         }
572       }
573     }
574 
575     // Otherwise take the unions of the known bit sets of the operands,
576     // taking conservative care to avoid excessive recursion.
577     if (Depth < MaxDepth - 1 && !KnownZero && !KnownOne) {
578       KnownZero = APInt::getAllOnesValue(BitWidth);
579       KnownOne = APInt::getAllOnesValue(BitWidth);
580       for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) {
581         // Skip direct self references.
582         if (P->getIncomingValue(i) == P) continue;
583 
584         KnownZero2 = APInt(BitWidth, 0);
585         KnownOne2 = APInt(BitWidth, 0);
586         // Recurse, but cap the recursion to one level, because we don't
587         // want to waste time spinning around in loops.
588         ComputeMaskedBits(P->getIncomingValue(i), KnownZero | KnownOne,
589                           KnownZero2, KnownOne2, TD, MaxDepth-1);
590         KnownZero &= KnownZero2;
591         KnownOne &= KnownOne2;
592         // If all bits have been ruled out, there's no need to check
593         // more operands.
594         if (!KnownZero && !KnownOne)
595           break;
596       }
597     }
598     break;
599   }
600   case Instruction::Call:
601     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
602       switch (II->getIntrinsicID()) {
603       default: break;
604       case Intrinsic::ctpop:
605       case Intrinsic::ctlz:
606       case Intrinsic::cttz: {
607         unsigned LowBits = Log2_32(BitWidth)+1;
608         KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
609         break;
610       }
611       }
612     }
613     break;
614   }
615 }
616 
617 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.  We use
618 /// this predicate to simplify operations downstream.  Mask is known to be zero
619 /// for bits that V cannot have.
620 ///
621 /// This function is defined on values with integer type, values with pointer
622 /// type (but only if TD is non-null), and vectors of integers.  In the case
623 /// where V is a vector, the mask, known zero, and known one values are the
624 /// same width as the vector element, and the bit is set only if it is true
625 /// for all of the elements in the vector.
626 bool llvm::MaskedValueIsZero(Value *V, const APInt &Mask,
627                              const TargetData *TD, unsigned Depth) {
628   APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0);
629   ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth);
630   assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
631   return (KnownZero & Mask) == Mask;
632 }
633 
634 
635 
636 /// ComputeNumSignBits - Return the number of times the sign bit of the
637 /// register is replicated into the other bits.  We know that at least 1 bit
638 /// is always equal to the sign bit (itself), but other cases can give us
639 /// information.  For example, immediately after an "ashr X, 2", we know that
640 /// the top 3 bits are all equal to each other, so we return 3.
641 ///
642 /// 'Op' must have a scalar integer type.
643 ///
644 unsigned llvm::ComputeNumSignBits(Value *V, const TargetData *TD,
645                                   unsigned Depth) {
646   assert((TD || V->getType()->isIntOrIntVector()) &&
647          "ComputeNumSignBits requires a TargetData object to operate "
648          "on non-integer values!");
649   const Type *Ty = V->getType();
650   unsigned TyBits = TD ? TD->getTypeSizeInBits(V->getType()->getScalarType()) :
651                          Ty->getScalarSizeInBits();
652   unsigned Tmp, Tmp2;
653   unsigned FirstAnswer = 1;
654 
655   // Note that ConstantInt is handled by the general ComputeMaskedBits case
656   // below.
657 
658   if (Depth == 6)
659     return 1;  // Limit search depth.
660 
661   Operator *U = dyn_cast<Operator>(V);
662   switch (Operator::getOpcode(V)) {
663   default: break;
664   case Instruction::SExt:
665     Tmp = TyBits-cast<IntegerType>(U->getOperand(0)->getType())->getBitWidth();
666     return ComputeNumSignBits(U->getOperand(0), TD, Depth+1) + Tmp;
667 
668   case Instruction::AShr:
669     Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
670     // ashr X, C   -> adds C sign bits.
671     if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) {
672       Tmp += C->getZExtValue();
673       if (Tmp > TyBits) Tmp = TyBits;
674     }
675     return Tmp;
676   case Instruction::Shl:
677     if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) {
678       // shl destroys sign bits.
679       Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
680       if (C->getZExtValue() >= TyBits ||      // Bad shift.
681           C->getZExtValue() >= Tmp) break;    // Shifted all sign bits out.
682       return Tmp - C->getZExtValue();
683     }
684     break;
685   case Instruction::And:
686   case Instruction::Or:
687   case Instruction::Xor:    // NOT is handled here.
688     // Logical binary ops preserve the number of sign bits at the worst.
689     Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
690     if (Tmp != 1) {
691       Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
692       FirstAnswer = std::min(Tmp, Tmp2);
693       // We computed what we know about the sign bits as our first
694       // answer. Now proceed to the generic code that uses
695       // ComputeMaskedBits, and pick whichever answer is better.
696     }
697     break;
698 
699   case Instruction::Select:
700     Tmp = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
701     if (Tmp == 1) return 1;  // Early out.
702     Tmp2 = ComputeNumSignBits(U->getOperand(2), TD, Depth+1);
703     return std::min(Tmp, Tmp2);
704 
705   case Instruction::Add:
706     // Add can have at most one carry bit.  Thus we know that the output
707     // is, at worst, one more bit than the inputs.
708     Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
709     if (Tmp == 1) return 1;  // Early out.
710 
711     // Special case decrementing a value (ADD X, -1):
712     if (ConstantInt *CRHS = dyn_cast<ConstantInt>(U->getOperand(1)))
713       if (CRHS->isAllOnesValue()) {
714         APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
715         APInt Mask = APInt::getAllOnesValue(TyBits);
716         ComputeMaskedBits(U->getOperand(0), Mask, KnownZero, KnownOne, TD,
717                           Depth+1);
718 
719         // If the input is known to be 0 or 1, the output is 0/-1, which is all
720         // sign bits set.
721         if ((KnownZero | APInt(TyBits, 1)) == Mask)
722           return TyBits;
723 
724         // If we are subtracting one from a positive number, there is no carry
725         // out of the result.
726         if (KnownZero.isNegative())
727           return Tmp;
728       }
729 
730     Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
731     if (Tmp2 == 1) return 1;
732       return std::min(Tmp, Tmp2)-1;
733     break;
734 
735   case Instruction::Sub:
736     Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1);
737     if (Tmp2 == 1) return 1;
738 
739     // Handle NEG.
740     if (ConstantInt *CLHS = dyn_cast<ConstantInt>(U->getOperand(0)))
741       if (CLHS->isNullValue()) {
742         APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
743         APInt Mask = APInt::getAllOnesValue(TyBits);
744         ComputeMaskedBits(U->getOperand(1), Mask, KnownZero, KnownOne,
745                           TD, Depth+1);
746         // If the input is known to be 0 or 1, the output is 0/-1, which is all
747         // sign bits set.
748         if ((KnownZero | APInt(TyBits, 1)) == Mask)
749           return TyBits;
750 
751         // If the input is known to be positive (the sign bit is known clear),
752         // the output of the NEG has the same number of sign bits as the input.
753         if (KnownZero.isNegative())
754           return Tmp2;
755 
756         // Otherwise, we treat this like a SUB.
757       }
758 
759     // Sub can have at most one carry bit.  Thus we know that the output
760     // is, at worst, one more bit than the inputs.
761     Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1);
762     if (Tmp == 1) return 1;  // Early out.
763       return std::min(Tmp, Tmp2)-1;
764     break;
765   case Instruction::Trunc:
766     // FIXME: it's tricky to do anything useful for this, but it is an important
767     // case for targets like X86.
768     break;
769   }
770 
771   // Finally, if we can prove that the top bits of the result are 0's or 1's,
772   // use this information.
773   APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
774   APInt Mask = APInt::getAllOnesValue(TyBits);
775   ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth);
776 
777   if (KnownZero.isNegative()) {        // sign bit is 0
778     Mask = KnownZero;
779   } else if (KnownOne.isNegative()) {  // sign bit is 1;
780     Mask = KnownOne;
781   } else {
782     // Nothing known.
783     return FirstAnswer;
784   }
785 
786   // Okay, we know that the sign bit in Mask is set.  Use CLZ to determine
787   // the number of identical bits in the top of the input value.
788   Mask = ~Mask;
789   Mask <<= Mask.getBitWidth()-TyBits;
790   // Return # leading zeros.  We use 'min' here in case Val was zero before
791   // shifting.  We don't want to return '64' as for an i32 "0".
792   return std::max(FirstAnswer, std::min(TyBits, Mask.countLeadingZeros()));
793 }
794 
795 /// CannotBeNegativeZero - Return true if we can prove that the specified FP
796 /// value is never equal to -0.0.
797 ///
798 /// NOTE: this function will need to be revisited when we support non-default
799 /// rounding modes!
800 ///
801 bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) {
802   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
803     return !CFP->getValueAPF().isNegZero();
804 
805   if (Depth == 6)
806     return 1;  // Limit search depth.
807 
808   const Operator *I = dyn_cast<Operator>(V);
809   if (I == 0) return false;
810 
811   // (add x, 0.0) is guaranteed to return +0.0, not -0.0.
812   if (I->getOpcode() == Instruction::FAdd &&
813       isa<ConstantFP>(I->getOperand(1)) &&
814       cast<ConstantFP>(I->getOperand(1))->isNullValue())
815     return true;
816 
817   // sitofp and uitofp turn into +0.0 for zero.
818   if (isa<SIToFPInst>(I) || isa<UIToFPInst>(I))
819     return true;
820 
821   if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
822     // sqrt(-0.0) = -0.0, no other negative results are possible.
823     if (II->getIntrinsicID() == Intrinsic::sqrt)
824       return CannotBeNegativeZero(II->getOperand(1), Depth+1);
825 
826   if (const CallInst *CI = dyn_cast<CallInst>(I))
827     if (const Function *F = CI->getCalledFunction()) {
828       if (F->isDeclaration()) {
829         // abs(x) != -0.0
830         if (F->getName() == "abs") return true;
831         // abs[lf](x) != -0.0
832         if (F->getName() == "absf") return true;
833         if (F->getName() == "absl") return true;
834       }
835     }
836 
837   return false;
838 }
839 
840 // This is the recursive version of BuildSubAggregate. It takes a few different
841 // arguments. Idxs is the index within the nested struct From that we are
842 // looking at now (which is of type IndexedType). IdxSkip is the number of
843 // indices from Idxs that should be left out when inserting into the resulting
844 // struct. To is the result struct built so far, new insertvalue instructions
845 // build on that.
846 static Value *BuildSubAggregate(Value *From, Value* To, const Type *IndexedType,
847                                 SmallVector<unsigned, 10> &Idxs,
848                                 unsigned IdxSkip,
849                                 LLVMContext &Context,
850                                 Instruction *InsertBefore) {
851   const llvm::StructType *STy = llvm::dyn_cast<llvm::StructType>(IndexedType);
852   if (STy) {
853     // Save the original To argument so we can modify it
854     Value *OrigTo = To;
855     // General case, the type indexed by Idxs is a struct
856     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
857       // Process each struct element recursively
858       Idxs.push_back(i);
859       Value *PrevTo = To;
860       To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
861                              Context, InsertBefore);
862       Idxs.pop_back();
863       if (!To) {
864         // Couldn't find any inserted value for this index? Cleanup
865         while (PrevTo != OrigTo) {
866           InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
867           PrevTo = Del->getAggregateOperand();
868           Del->eraseFromParent();
869         }
870         // Stop processing elements
871         break;
872       }
873     }
874     // If we succesfully found a value for each of our subaggregates
875     if (To)
876       return To;
877   }
878   // Base case, the type indexed by SourceIdxs is not a struct, or not all of
879   // the struct's elements had a value that was inserted directly. In the latter
880   // case, perhaps we can't determine each of the subelements individually, but
881   // we might be able to find the complete struct somewhere.
882 
883   // Find the value that is at that particular spot
884   Value *V = FindInsertedValue(From, Idxs.begin(), Idxs.end(), Context);
885 
886   if (!V)
887     return NULL;
888 
889   // Insert the value in the new (sub) aggregrate
890   return llvm::InsertValueInst::Create(To, V, Idxs.begin() + IdxSkip,
891                                        Idxs.end(), "tmp", InsertBefore);
892 }
893 
894 // This helper takes a nested struct and extracts a part of it (which is again a
895 // struct) into a new value. For example, given the struct:
896 // { a, { b, { c, d }, e } }
897 // and the indices "1, 1" this returns
898 // { c, d }.
899 //
900 // It does this by inserting an insertvalue for each element in the resulting
901 // struct, as opposed to just inserting a single struct. This will only work if
902 // each of the elements of the substruct are known (ie, inserted into From by an
903 // insertvalue instruction somewhere).
904 //
905 // All inserted insertvalue instructions are inserted before InsertBefore
906 static Value *BuildSubAggregate(Value *From, const unsigned *idx_begin,
907                                 const unsigned *idx_end, LLVMContext &Context,
908                                 Instruction *InsertBefore) {
909   assert(InsertBefore && "Must have someplace to insert!");
910   const Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
911                                                              idx_begin,
912                                                              idx_end);
913   Value *To = UndefValue::get(IndexedType);
914   SmallVector<unsigned, 10> Idxs(idx_begin, idx_end);
915   unsigned IdxSkip = Idxs.size();
916 
917   return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip,
918                            Context, InsertBefore);
919 }
920 
921 /// FindInsertedValue - Given an aggregrate and an sequence of indices, see if
922 /// the scalar value indexed is already around as a register, for example if it
923 /// were inserted directly into the aggregrate.
924 ///
925 /// If InsertBefore is not null, this function will duplicate (modified)
926 /// insertvalues when a part of a nested struct is extracted.
927 Value *llvm::FindInsertedValue(Value *V, const unsigned *idx_begin,
928                          const unsigned *idx_end, LLVMContext &Context,
929                          Instruction *InsertBefore) {
930   // Nothing to index? Just return V then (this is useful at the end of our
931   // recursion)
932   if (idx_begin == idx_end)
933     return V;
934   // We have indices, so V should have an indexable type
935   assert((isa<StructType>(V->getType()) || isa<ArrayType>(V->getType()))
936          && "Not looking at a struct or array?");
937   assert(ExtractValueInst::getIndexedType(V->getType(), idx_begin, idx_end)
938          && "Invalid indices for type?");
939   const CompositeType *PTy = cast<CompositeType>(V->getType());
940 
941   if (isa<UndefValue>(V))
942     return UndefValue::get(ExtractValueInst::getIndexedType(PTy,
943                                                               idx_begin,
944                                                               idx_end));
945   else if (isa<ConstantAggregateZero>(V))
946     return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy,
947                                                                   idx_begin,
948                                                                   idx_end));
949   else if (Constant *C = dyn_cast<Constant>(V)) {
950     if (isa<ConstantArray>(C) || isa<ConstantStruct>(C))
951       // Recursively process this constant
952       return FindInsertedValue(C->getOperand(*idx_begin), idx_begin + 1,
953                                idx_end, Context, InsertBefore);
954   } else if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
955     // Loop the indices for the insertvalue instruction in parallel with the
956     // requested indices
957     const unsigned *req_idx = idx_begin;
958     for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
959          i != e; ++i, ++req_idx) {
960       if (req_idx == idx_end) {
961         if (InsertBefore)
962           // The requested index identifies a part of a nested aggregate. Handle
963           // this specially. For example,
964           // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
965           // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
966           // %C = extractvalue {i32, { i32, i32 } } %B, 1
967           // This can be changed into
968           // %A = insertvalue {i32, i32 } undef, i32 10, 0
969           // %C = insertvalue {i32, i32 } %A, i32 11, 1
970           // which allows the unused 0,0 element from the nested struct to be
971           // removed.
972           return BuildSubAggregate(V, idx_begin, req_idx,
973                                    Context, InsertBefore);
974         else
975           // We can't handle this without inserting insertvalues
976           return 0;
977       }
978 
979       // This insert value inserts something else than what we are looking for.
980       // See if the (aggregrate) value inserted into has the value we are
981       // looking for, then.
982       if (*req_idx != *i)
983         return FindInsertedValue(I->getAggregateOperand(), idx_begin, idx_end,
984                                  Context, InsertBefore);
985     }
986     // If we end up here, the indices of the insertvalue match with those
987     // requested (though possibly only partially). Now we recursively look at
988     // the inserted value, passing any remaining indices.
989     return FindInsertedValue(I->getInsertedValueOperand(), req_idx, idx_end,
990                              Context, InsertBefore);
991   } else if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
992     // If we're extracting a value from an aggregrate that was extracted from
993     // something else, we can extract from that something else directly instead.
994     // However, we will need to chain I's indices with the requested indices.
995 
996     // Calculate the number of indices required
997     unsigned size = I->getNumIndices() + (idx_end - idx_begin);
998     // Allocate some space to put the new indices in
999     SmallVector<unsigned, 5> Idxs;
1000     Idxs.reserve(size);
1001     // Add indices from the extract value instruction
1002     for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
1003          i != e; ++i)
1004       Idxs.push_back(*i);
1005 
1006     // Add requested indices
1007     for (const unsigned *i = idx_begin, *e = idx_end; i != e; ++i)
1008       Idxs.push_back(*i);
1009 
1010     assert(Idxs.size() == size
1011            && "Number of indices added not correct?");
1012 
1013     return FindInsertedValue(I->getAggregateOperand(), Idxs.begin(), Idxs.end(),
1014                              Context, InsertBefore);
1015   }
1016   // Otherwise, we don't know (such as, extracting from a function return value
1017   // or load instruction)
1018   return 0;
1019 }
1020 
1021 /// GetConstantStringInfo - This function computes the length of a
1022 /// null-terminated C string pointed to by V.  If successful, it returns true
1023 /// and returns the string in Str.  If unsuccessful, it returns false.
1024 bool llvm::GetConstantStringInfo(Value *V, std::string &Str, uint64_t Offset,
1025                                  bool StopAtNul) {
1026   // If V is NULL then return false;
1027   if (V == NULL) return false;
1028 
1029   // Look through bitcast instructions.
1030   if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
1031     return GetConstantStringInfo(BCI->getOperand(0), Str, Offset, StopAtNul);
1032 
1033   // If the value is not a GEP instruction nor a constant expression with a
1034   // GEP instruction, then return false because ConstantArray can't occur
1035   // any other way
1036   User *GEP = 0;
1037   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
1038     GEP = GEPI;
1039   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1040     if (CE->getOpcode() == Instruction::BitCast)
1041       return GetConstantStringInfo(CE->getOperand(0), Str, Offset, StopAtNul);
1042     if (CE->getOpcode() != Instruction::GetElementPtr)
1043       return false;
1044     GEP = CE;
1045   }
1046 
1047   if (GEP) {
1048     // Make sure the GEP has exactly three arguments.
1049     if (GEP->getNumOperands() != 3)
1050       return false;
1051 
1052     // Make sure the index-ee is a pointer to array of i8.
1053     const PointerType *PT = cast<PointerType>(GEP->getOperand(0)->getType());
1054     const ArrayType *AT = dyn_cast<ArrayType>(PT->getElementType());
1055     if (AT == 0 || AT->getElementType() != Type::getInt8Ty(V->getContext()))
1056       return false;
1057 
1058     // Check to make sure that the first operand of the GEP is an integer and
1059     // has value 0 so that we are sure we're indexing into the initializer.
1060     ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
1061     if (FirstIdx == 0 || !FirstIdx->isZero())
1062       return false;
1063 
1064     // If the second index isn't a ConstantInt, then this is a variable index
1065     // into the array.  If this occurs, we can't say anything meaningful about
1066     // the string.
1067     uint64_t StartIdx = 0;
1068     if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
1069       StartIdx = CI->getZExtValue();
1070     else
1071       return false;
1072     return GetConstantStringInfo(GEP->getOperand(0), Str, StartIdx+Offset,
1073                                  StopAtNul);
1074   }
1075 
1076   if (MDString *MDStr = dyn_cast<MDString>(V)) {
1077     Str = MDStr->getString();
1078     return true;
1079   }
1080 
1081   // The GEP instruction, constant or instruction, must reference a global
1082   // variable that is a constant and is initialized. The referenced constant
1083   // initializer is the array that we'll use for optimization.
1084   GlobalVariable* GV = dyn_cast<GlobalVariable>(V);
1085   if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
1086     return false;
1087   Constant *GlobalInit = GV->getInitializer();
1088 
1089   // Handle the ConstantAggregateZero case
1090   if (isa<ConstantAggregateZero>(GlobalInit)) {
1091     // This is a degenerate case. The initializer is constant zero so the
1092     // length of the string must be zero.
1093     Str.clear();
1094     return true;
1095   }
1096 
1097   // Must be a Constant Array
1098   ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
1099   if (Array == 0 ||
1100       Array->getType()->getElementType() != Type::getInt8Ty(V->getContext()))
1101     return false;
1102 
1103   // Get the number of elements in the array
1104   uint64_t NumElts = Array->getType()->getNumElements();
1105 
1106   if (Offset > NumElts)
1107     return false;
1108 
1109   // Traverse the constant array from 'Offset' which is the place the GEP refers
1110   // to in the array.
1111   Str.reserve(NumElts-Offset);
1112   for (unsigned i = Offset; i != NumElts; ++i) {
1113     Constant *Elt = Array->getOperand(i);
1114     ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1115     if (!CI) // This array isn't suitable, non-int initializer.
1116       return false;
1117     if (StopAtNul && CI->isZero())
1118       return true; // we found end of string, success!
1119     Str += (char)CI->getZExtValue();
1120   }
1121 
1122   // The array isn't null terminated, but maybe this is a memcpy, not a strcpy.
1123   return true;
1124 }
1125