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