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