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