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