1 //===- ValueTracking.cpp - Walk computations to compute properties --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains routines that help analyze properties that chains of
13 //===----------------------------------------------------------------------===//
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"
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
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.
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!");
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");
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
;
67 // Null and aggregate-zero are all-zeros.
68 if (isa
<ConstantPointerNull
>(V
) ||
69 isa
<ConstantAggregateZero
>(V
)) {
74 // Handle a constant vector by taking the intersection of the known bits of
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
,
82 KnownZero
&= KnownZero2
;
83 KnownOne
&= KnownOne2
;
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
);
98 Align
= TD
->getABITypeAlignment(ObjectType
);
101 KnownZero
= Mask
& APInt::getLowBitsSet(BitWidth
,
102 CountTrailingZeros_32(Align
));
109 KnownZero
.clear(); KnownOne
.clear(); // Start out not knowing anything.
111 if (Depth
== MaxDepth
|| Mask
== 0)
112 return; // Limit search depth.
114 Operator
*I
= dyn_cast
<Operator
>(V
);
117 APInt
KnownZero2(KnownZero
), KnownOne2(KnownOne
);
118 switch (I
->getOpcode()) {
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
,
126 assert((KnownZero
& KnownOne
) == 0 && "Bits known to be one AND zero?");
127 assert((KnownZero2
& KnownOne2
) == 0 && "Bits known to be one AND zero?");
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
;
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
,
140 assert((KnownZero
& KnownOne
) == 0 && "Bits known to be one AND zero?");
141 assert((KnownZero2
& KnownOne2
) == 0 && "Bits known to be one AND zero?");
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
;
149 case Instruction::Xor
: {
150 ComputeMaskedBits(I
->getOperand(1), Mask
, KnownZero
, KnownOne
, TD
, Depth
+1);
151 ComputeMaskedBits(I
->getOperand(0), Mask
, KnownZero2
, KnownOne2
, TD
,
153 assert((KnownZero
& KnownOne
) == 0 && "Bits known to be one AND zero?");
154 assert((KnownZero2
& KnownOne2
) == 0 && "Bits known to be one AND zero?");
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
;
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
,
168 assert((KnownZero
& KnownOne
) == 0 && "Bits known to be one AND zero?");
169 assert((KnownZero2
& KnownOne2
) == 0 && "Bits known to be one AND zero?");
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.
176 unsigned TrailZ
= KnownZero
.countTrailingOnes() +
177 KnownZero2
.countTrailingOnes();
178 unsigned LeadZ
= std::max(KnownZero
.countLeadingOnes() +
179 KnownZero2
.countLeadingOnes(),
180 BitWidth
) - BitWidth
;
182 TrailZ
= std::min(TrailZ
, BitWidth
);
183 LeadZ
= std::min(LeadZ
, BitWidth
);
184 KnownZero
= APInt::getLowBitsSet(BitWidth
, TrailZ
) |
185 APInt::getHighBitsSet(BitWidth
, LeadZ
);
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();
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);
207 KnownZero
= APInt::getHighBitsSet(BitWidth
, LeadZ
) & Mask
;
210 case Instruction::Select
:
211 ComputeMaskedBits(I
->getOperand(2), Mask
, KnownZero
, KnownOne
, TD
, Depth
+1);
212 ComputeMaskedBits(I
->getOperand(1), Mask
, KnownZero2
, KnownOne2
, TD
,
214 assert((KnownZero
& KnownOne
) == 0 && "Bits known to be one AND zero?");
215 assert((KnownZero2
& KnownOne2
) == 0 && "Bits known to be one AND zero?");
217 // Only known if known in both the LHS and RHS.
218 KnownOne
&= KnownOne2
;
219 KnownZero
&= KnownZero2
;
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.
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();
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
);
243 SrcBitWidth
= SrcTy
->getScalarSizeInBits();
246 MaskIn
.zextOrTrunc(SrcBitWidth
);
247 KnownZero
.zextOrTrunc(SrcBitWidth
);
248 KnownOne
.zextOrTrunc(SrcBitWidth
);
249 ComputeMaskedBits(I
->getOperand(0), MaskIn
, KnownZero
, KnownOne
, TD
,
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
);
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
,
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();
275 MaskIn
.trunc(SrcBitWidth
);
276 KnownZero
.trunc(SrcBitWidth
);
277 KnownOne
.trunc(SrcBitWidth
);
278 ComputeMaskedBits(I
->getOperand(0), MaskIn
, KnownZero
, KnownOne
, TD
,
280 assert((KnownZero
& KnownOne
) == 0 && "Bits known to be one AND zero?");
281 KnownZero
.zext(BitWidth
);
282 KnownOne
.zext(BitWidth
);
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
);
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
,
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
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
);
312 // Unsigned shift right.
313 APInt
Mask2(Mask
.shl(ShiftAmt
));
314 ComputeMaskedBits(I
->getOperand(0), Mask2
, KnownZero
,KnownOne
, TD
,
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
);
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
);
330 // Signed shift right.
331 APInt
Mask2(Mask
.shl(ShiftAmt
));
332 ComputeMaskedBits(I
->getOperand(0), Mask2
, KnownZero
, KnownOne
, TD
,
334 assert((KnownZero
& KnownOne
) == 0&&"Bits known to be one AND zero?");
335 KnownZero
= APIntOps::lshr(KnownZero
, ShiftAmt
);
336 KnownOne
= APIntOps::lshr(KnownOne
, ShiftAmt
);
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
;
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
,
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
361 if ((KnownZero2
& MaskV
) == MaskV
) {
362 unsigned NLZ2
= CLHS
->getValue().countLeadingZeros();
363 // Top bits known zero.
364 KnownZero
= APInt::getHighBitsSet(BitWidth
, NLZ2
) & Mask
;
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
,
380 assert((LHSKnownZero
& LHSKnownOne
) == 0 &&
381 "Bits known to be one AND zero?");
382 unsigned LHSKnownZeroOut
= LHSKnownZero
.countTrailingOnes();
384 ComputeMaskedBits(I
->getOperand(1), Mask2
, KnownZero2
, KnownOne2
, TD
,
386 assert((KnownZero2
& KnownOne2
) == 0 && "Bits known to be one AND zero?");
387 unsigned RHSKnownZeroOut
= KnownZero2
.countTrailingOnes();
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
;
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
,
403 } else if (RHSKnownZeroOut
>= LHSKnownZeroOut
) {
404 APInt Mask
= APInt::getLowBitsSet(BitWidth
, RHSKnownZeroOut
);
405 KnownZero
|= LHSKnownZero
& Mask
;
406 KnownOne
|= LHSKnownOne
& Mask
;
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
,
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
;
425 KnownZero
|= KnownZero2
& Mask
;
427 assert((KnownZero
& KnownOne
) == 0&&"Bits known to be one AND zero?");
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
,
440 assert((KnownZero
& KnownOne
) == 0&&"Bits known to be one AND zero?");
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
,
450 ComputeMaskedBits(I
->getOperand(1), AllOnes
, KnownZero2
, KnownOne2
,
453 unsigned Leaders
= std::max(KnownZero
.countLeadingOnes(),
454 KnownZero2
.countLeadingOnes());
456 KnownZero
= APInt::getHighBitsSet(BitWidth
, Leaders
) & Mask
;
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());
472 (unsigned)TD
->getABITypeAlignment(
473 Type::getDoubleTy(V
->getContext())));
476 (unsigned)TD
->getABITypeAlignment(
477 Type::getInt64Ty(V
->getContext())));
482 KnownZero
= Mask
& APInt::getLowBitsSet(BitWidth
,
483 CountTrailingZeros_32(Align
));
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();
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.
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
));
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()));
522 KnownZero
= APInt::getLowBitsSet(BitWidth
, TrailZ
) & Mask
;
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
);
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.
555 // Ok, we have a PHI of the form L op= R. Check for low
557 APInt Mask2
= APInt::getAllOnesValue(BitWidth
);
558 ComputeMaskedBits(R
, Mask2
, KnownZero2
, KnownOne2
, TD
, Depth
+1);
559 Mask2
= APInt::getLowBitsSet(BitWidth
,
560 KnownZero2
.countTrailingOnes());
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);
567 APInt::getLowBitsSet(BitWidth
,
568 std::min(KnownZero2
.countTrailingOnes(),
569 KnownZero3
.countTrailingOnes()));
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;
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
594 if (!KnownZero
&& !KnownOne
)
600 case Instruction::Call
:
601 if (IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(I
)) {
602 switch (II
->getIntrinsicID()) {
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
);
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.
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
;
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.
642 /// 'Op' must have a scalar integer type.
644 unsigned llvm::ComputeNumSignBits(Value
*V
, const TargetData
*TD
,
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();
653 unsigned FirstAnswer
= 1;
655 // Note that ConstantInt is handled by the general ComputeMaskedBits case
659 return 1; // Limit search depth.
661 Operator
*U
= dyn_cast
<Operator
>(V
);
662 switch (Operator::getOpcode(V
)) {
664 case Instruction::SExt
:
665 Tmp
= TyBits
-cast
<IntegerType
>(U
->getOperand(0)->getType())->getBitWidth();
666 return ComputeNumSignBits(U
->getOperand(0), TD
, Depth
+1) + Tmp
;
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
;
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();
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);
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.
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
);
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.
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
,
719 // If the input is known to be 0 or 1, the output is 0/-1, which is all
721 if ((KnownZero
| APInt(TyBits
, 1)) == Mask
)
724 // If we are subtracting one from a positive number, there is no carry
725 // out of the result.
726 if (KnownZero
.isNegative())
730 Tmp2
= ComputeNumSignBits(U
->getOperand(1), TD
, Depth
+1);
731 if (Tmp2
== 1) return 1;
732 return std::min(Tmp
, Tmp2
)-1;
735 case Instruction::Sub
:
736 Tmp2
= ComputeNumSignBits(U
->getOperand(1), TD
, Depth
+1);
737 if (Tmp2
== 1) return 1;
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
,
746 // If the input is known to be 0 or 1, the output is 0/-1, which is all
748 if ((KnownZero
| APInt(TyBits
, 1)) == Mask
)
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())
756 // Otherwise, we treat this like a SUB.
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;
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.
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
);
777 if (KnownZero
.isNegative()) { // sign bit is 0
779 } else if (KnownOne
.isNegative()) { // sign bit is 1;
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.
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()));
795 /// CannotBeNegativeZero - Return true if we can prove that the specified FP
796 /// value is never equal to -0.0.
798 /// NOTE: this function will need to be revisited when we support non-default
801 bool llvm::CannotBeNegativeZero(const Value
*V
, unsigned Depth
) {
802 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(V
))
803 return !CFP
->getValueAPF().isNegZero();
806 return 1; // Limit search depth.
808 const Operator
*I
= dyn_cast
<Operator
>(V
);
809 if (I
== 0) return false;
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())
817 // sitofp and uitofp turn into +0.0 for zero.
818 if (isa
<SIToFPInst
>(I
) || isa
<UIToFPInst
>(I
))
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);
826 if (const CallInst
*CI
= dyn_cast
<CallInst
>(I
))
827 if (const Function
*F
= CI
->getCalledFunction()) {
828 if (F
->isDeclaration()) {
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;
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
846 static Value
*BuildSubAggregate(Value
*From
, Value
* To
, const Type
*IndexedType
,
847 SmallVector
<unsigned, 10> &Idxs
,
849 LLVMContext
&Context
,
850 Instruction
*InsertBefore
) {
851 const llvm::StructType
*STy
= llvm::dyn_cast
<llvm::StructType
>(IndexedType
);
853 // Save the original To argument so we can modify it
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
860 To
= BuildSubAggregate(From
, To
, STy
->getElementType(i
), Idxs
, IdxSkip
,
861 Context
, InsertBefore
);
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();
870 // Stop processing elements
874 // If we succesfully found a value for each of our subaggregates
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.
883 // Find the value that is at that particular spot
884 Value
*V
= FindInsertedValue(From
, Idxs
.begin(), Idxs
.end(), Context
);
889 // Insert the value in the new (sub) aggregrate
890 return llvm::InsertValueInst::Create(To
, V
, Idxs
.begin() + IdxSkip
,
891 Idxs
.end(), "tmp", InsertBefore
);
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
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).
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(),
913 Value
*To
= UndefValue::get(IndexedType
);
914 SmallVector
<unsigned, 10> Idxs(idx_begin
, idx_end
);
915 unsigned IdxSkip
= Idxs
.size();
917 return BuildSubAggregate(From
, To
, IndexedType
, Idxs
, IdxSkip
,
918 Context
, InsertBefore
);
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.
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
932 if (idx_begin
== idx_end
)
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());
941 if (isa
<UndefValue
>(V
))
942 return UndefValue::get(ExtractValueInst::getIndexedType(PTy
,
945 else if (isa
<ConstantAggregateZero
>(V
))
946 return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy
,
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
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
) {
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
972 return BuildSubAggregate(V
, idx_begin
, req_idx
,
973 Context
, InsertBefore
);
975 // We can't handle this without inserting insertvalues
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.
983 return FindInsertedValue(I
->getAggregateOperand(), idx_begin
, idx_end
,
984 Context
, InsertBefore
);
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.
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
;
1001 // Add indices from the extract value instruction
1002 for (const unsigned *i
= I
->idx_begin(), *e
= I
->idx_end();
1006 // Add requested indices
1007 for (const unsigned *i
= idx_begin
, *e
= idx_end
; i
!= e
; ++i
)
1010 assert(Idxs
.size() == size
1011 && "Number of indices added not correct?");
1013 return FindInsertedValue(I
->getAggregateOperand(), Idxs
.begin(), Idxs
.end(),
1014 Context
, InsertBefore
);
1016 // Otherwise, we don't know (such as, extracting from a function return value
1017 // or load instruction)
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
,
1026 // If V is NULL then return false;
1027 if (V
== NULL
) return false;
1029 // Look through bitcast instructions.
1030 if (BitCastInst
*BCI
= dyn_cast
<BitCastInst
>(V
))
1031 return GetConstantStringInfo(BCI
->getOperand(0), Str
, Offset
, StopAtNul
);
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
1037 if (GetElementPtrInst
*GEPI
= dyn_cast
<GetElementPtrInst
>(V
)) {
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
)
1048 // Make sure the GEP has exactly three arguments.
1049 if (GEP
->getNumOperands() != 3)
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()))
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())
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
1067 uint64_t StartIdx
= 0;
1068 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(GEP
->getOperand(2)))
1069 StartIdx
= CI
->getZExtValue();
1072 return GetConstantStringInfo(GEP
->getOperand(0), Str
, StartIdx
+Offset
,
1076 if (MDString
*MDStr
= dyn_cast
<MDString
>(V
)) {
1077 Str
= MDStr
->getString();
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())
1087 Constant
*GlobalInit
= GV
->getInitializer();
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.
1097 // Must be a Constant Array
1098 ConstantArray
*Array
= dyn_cast
<ConstantArray
>(GlobalInit
);
1100 Array
->getType()->getElementType() != Type::getInt8Ty(V
->getContext()))
1103 // Get the number of elements in the array
1104 uint64_t NumElts
= Array
->getType()->getNumElements();
1106 if (Offset
> NumElts
)
1109 // Traverse the constant array from 'Offset' which is the place the GEP refers
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.
1117 if (StopAtNul
&& CI
->isZero())
1118 return true; // we found end of string, success!
1119 Str
+= (char)CI
->getZExtValue();
1122 // The array isn't null terminated, but maybe this is a memcpy, not a strcpy.