| Chris Lattner | 965c769 | 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" | 
| Dan Gohman | 949ab78 | 2010-12-15 20:10:26 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/InstructionSimplify.h" | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" | 
|  | 18 | #include "llvm/Instructions.h" | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 19 | #include "llvm/GlobalVariable.h" | 
| Dan Gohman | 94262db | 2009-09-15 16:14:44 +0000 | [diff] [blame] | 20 | #include "llvm/GlobalAlias.h" | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 21 | #include "llvm/IntrinsicInst.h" | 
| Owen Anderson | f1f1743 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 22 | #include "llvm/LLVMContext.h" | 
| Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 23 | #include "llvm/Operator.h" | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetData.h" | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 25 | #include "llvm/Support/GetElementPtrTypeIterator.h" | 
|  | 26 | #include "llvm/Support/MathExtras.h" | 
| Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 27 | #include "llvm/Support/PatternMatch.h" | 
| Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallPtrSet.h" | 
| Chris Lattner | 6449690 | 2008-06-04 04:46:14 +0000 | [diff] [blame] | 29 | #include <cstring> | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 30 | using namespace llvm; | 
| Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 31 | using namespace llvm::PatternMatch; | 
|  | 32 |  | 
|  | 33 | const unsigned MaxDepth = 6; | 
|  | 34 |  | 
|  | 35 | /// getBitWidth - Returns the bitwidth of the given scalar or pointer type (if | 
|  | 36 | /// unknown returns 0).  For vector types, returns the element type's bitwidth. | 
|  | 37 | static unsigned getBitWidth(const Type *Ty, const TargetData *TD) { | 
|  | 38 | if (unsigned BitWidth = Ty->getScalarSizeInBits()) | 
|  | 39 | return BitWidth; | 
|  | 40 | assert(isa<PointerType>(Ty) && "Expected a pointer type!"); | 
|  | 41 | return TD ? TD->getPointerSizeInBits() : 0; | 
|  | 42 | } | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 43 |  | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 44 | /// ComputeMaskedBits - Determine which of the bits specified in Mask are | 
|  | 45 | /// known to be either zero or one and return them in the KnownZero/KnownOne | 
|  | 46 | /// bit sets.  This code only analyzes bits in Mask, in order to short-circuit | 
|  | 47 | /// processing. | 
|  | 48 | /// NOTE: we cannot consider 'undef' to be "IsZero" here.  The problem is that | 
|  | 49 | /// we cannot optimize based on the assumption that it is zero without changing | 
|  | 50 | /// it to be an explicit zero.  If we don't change it to zero, other code could | 
|  | 51 | /// optimized based on the contradictory assumption that it is non-zero. | 
|  | 52 | /// Because instcombine aggressively folds operations with undef args anyway, | 
|  | 53 | /// this won't lose us code quality. | 
| Chris Lattner | 4bc2825 | 2009-09-08 00:06:16 +0000 | [diff] [blame] | 54 | /// | 
|  | 55 | /// This function is defined on values with integer type, values with pointer | 
|  | 56 | /// type (but only if TD is non-null), and vectors of integers.  In the case | 
|  | 57 | /// where V is a vector, the mask, known zero, and known one values are the | 
|  | 58 | /// same width as the vector element, and the bit is set only if it is true | 
|  | 59 | /// for all of the elements in the vector. | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 60 | void llvm::ComputeMaskedBits(Value *V, const APInt &Mask, | 
|  | 61 | APInt &KnownZero, APInt &KnownOne, | 
| Dan Gohman | 05f1135 | 2009-08-27 17:51:25 +0000 | [diff] [blame] | 62 | const TargetData *TD, unsigned Depth) { | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 63 | assert(V && "No Value?"); | 
| Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 64 | assert(Depth <= MaxDepth && "Limit Search Depth"); | 
| Chris Lattner | 4612ae1 | 2009-01-20 18:22:57 +0000 | [diff] [blame] | 65 | unsigned BitWidth = Mask.getBitWidth(); | 
| Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 66 | assert((V->getType()->isIntOrIntVectorTy() || V->getType()->isPointerTy()) | 
| Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 67 | && "Not integer or pointer type!"); | 
| Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 68 | assert((!TD || | 
|  | 69 | TD->getTypeSizeInBits(V->getType()->getScalarType()) == BitWidth) && | 
| Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 70 | (!V->getType()->isIntOrIntVectorTy() || | 
| Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 71 | V->getType()->getScalarSizeInBits() == BitWidth) && | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 72 | KnownZero.getBitWidth() == BitWidth && | 
|  | 73 | KnownOne.getBitWidth() == BitWidth && | 
|  | 74 | "V, Mask, KnownOne and KnownZero should have same BitWidth"); | 
|  | 75 |  | 
|  | 76 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { | 
|  | 77 | // We know all of the bits for a constant! | 
|  | 78 | KnownOne = CI->getValue() & Mask; | 
|  | 79 | KnownZero = ~KnownOne & Mask; | 
|  | 80 | return; | 
|  | 81 | } | 
| Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 82 | // Null and aggregate-zero are all-zeros. | 
|  | 83 | if (isa<ConstantPointerNull>(V) || | 
|  | 84 | isa<ConstantAggregateZero>(V)) { | 
| Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 85 | KnownOne.clearAllBits(); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 86 | KnownZero = Mask; | 
|  | 87 | return; | 
|  | 88 | } | 
| Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 89 | // Handle a constant vector by taking the intersection of the known bits of | 
|  | 90 | // each element. | 
|  | 91 | if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) { | 
| Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 92 | KnownZero.setAllBits(); KnownOne.setAllBits(); | 
| Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 93 | for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) { | 
|  | 94 | APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0); | 
|  | 95 | ComputeMaskedBits(CV->getOperand(i), Mask, KnownZero2, KnownOne2, | 
|  | 96 | TD, Depth); | 
|  | 97 | KnownZero &= KnownZero2; | 
|  | 98 | KnownOne &= KnownOne2; | 
|  | 99 | } | 
|  | 100 | return; | 
|  | 101 | } | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 102 | // The address of an aligned GlobalValue has trailing zeros. | 
|  | 103 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { | 
|  | 104 | unsigned Align = GV->getAlignment(); | 
| Dan Gohman | a72f856 | 2009-08-11 15:50:03 +0000 | [diff] [blame] | 105 | if (Align == 0 && TD && GV->getType()->getElementType()->isSized()) { | 
|  | 106 | const Type *ObjectType = GV->getType()->getElementType(); | 
|  | 107 | // If the object is defined in the current Module, we'll be giving | 
|  | 108 | // it the preferred alignment. Otherwise, we have to assume that it | 
|  | 109 | // may only have the minimum ABI alignment. | 
|  | 110 | if (!GV->isDeclaration() && !GV->mayBeOverridden()) | 
|  | 111 | Align = TD->getPrefTypeAlignment(ObjectType); | 
|  | 112 | else | 
|  | 113 | Align = TD->getABITypeAlignment(ObjectType); | 
|  | 114 | } | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 115 | if (Align > 0) | 
|  | 116 | KnownZero = Mask & APInt::getLowBitsSet(BitWidth, | 
|  | 117 | CountTrailingZeros_32(Align)); | 
|  | 118 | else | 
| Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 119 | KnownZero.clearAllBits(); | 
|  | 120 | KnownOne.clearAllBits(); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 121 | return; | 
|  | 122 | } | 
| Dan Gohman | 94262db | 2009-09-15 16:14:44 +0000 | [diff] [blame] | 123 | // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has | 
|  | 124 | // the bits of its aliasee. | 
|  | 125 | if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { | 
|  | 126 | if (GA->mayBeOverridden()) { | 
| Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 127 | KnownZero.clearAllBits(); KnownOne.clearAllBits(); | 
| Dan Gohman | 94262db | 2009-09-15 16:14:44 +0000 | [diff] [blame] | 128 | } else { | 
|  | 129 | ComputeMaskedBits(GA->getAliasee(), Mask, KnownZero, KnownOne, | 
|  | 130 | TD, Depth+1); | 
|  | 131 | } | 
|  | 132 | return; | 
|  | 133 | } | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 134 |  | 
| Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 135 | KnownZero.clearAllBits(); KnownOne.clearAllBits();   // Start out not knowing anything. | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 136 |  | 
| Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 137 | if (Depth == MaxDepth || Mask == 0) | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 138 | return;  // Limit search depth. | 
|  | 139 |  | 
| Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 140 | Operator *I = dyn_cast<Operator>(V); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 141 | if (!I) return; | 
|  | 142 |  | 
|  | 143 | APInt KnownZero2(KnownZero), KnownOne2(KnownOne); | 
| Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 144 | switch (I->getOpcode()) { | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 145 | default: break; | 
|  | 146 | case Instruction::And: { | 
|  | 147 | // If either the LHS or the RHS are Zero, the result is zero. | 
|  | 148 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1); | 
|  | 149 | APInt Mask2(Mask & ~KnownZero); | 
|  | 150 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD, | 
|  | 151 | Depth+1); | 
|  | 152 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
|  | 153 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | 
|  | 154 |  | 
|  | 155 | // Output known-1 bits are only known if set in both the LHS & RHS. | 
|  | 156 | KnownOne &= KnownOne2; | 
|  | 157 | // Output known-0 are known to be clear if zero in either the LHS | RHS. | 
|  | 158 | KnownZero |= KnownZero2; | 
|  | 159 | return; | 
|  | 160 | } | 
|  | 161 | case Instruction::Or: { | 
|  | 162 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1); | 
|  | 163 | APInt Mask2(Mask & ~KnownOne); | 
|  | 164 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD, | 
|  | 165 | Depth+1); | 
|  | 166 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
|  | 167 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | 
|  | 168 |  | 
|  | 169 | // Output known-0 bits are only known if clear in both the LHS & RHS. | 
|  | 170 | KnownZero &= KnownZero2; | 
|  | 171 | // Output known-1 are known to be set if set in either the LHS | RHS. | 
|  | 172 | KnownOne |= KnownOne2; | 
|  | 173 | return; | 
|  | 174 | } | 
|  | 175 | case Instruction::Xor: { | 
|  | 176 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, TD, Depth+1); | 
|  | 177 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, TD, | 
|  | 178 | Depth+1); | 
|  | 179 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
|  | 180 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | 
|  | 181 |  | 
|  | 182 | // Output known-0 bits are known if clear or set in both the LHS & RHS. | 
|  | 183 | APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); | 
|  | 184 | // Output known-1 are known to be set if set in only one of the LHS, RHS. | 
|  | 185 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); | 
|  | 186 | KnownZero = KnownZeroOut; | 
|  | 187 | return; | 
|  | 188 | } | 
|  | 189 | case Instruction::Mul: { | 
|  | 190 | APInt Mask2 = APInt::getAllOnesValue(BitWidth); | 
|  | 191 | ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero, KnownOne, TD,Depth+1); | 
|  | 192 | ComputeMaskedBits(I->getOperand(0), Mask2, 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 | // If low bits are zero in either operand, output low known-0 bits. | 
|  | 198 | // Also compute a conserative estimate for high known-0 bits. | 
|  | 199 | // More trickiness is possible, but this is sufficient for the | 
|  | 200 | // interesting case of alignment computation. | 
| Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 201 | KnownOne.clearAllBits(); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 202 | unsigned TrailZ = KnownZero.countTrailingOnes() + | 
|  | 203 | KnownZero2.countTrailingOnes(); | 
|  | 204 | unsigned LeadZ =  std::max(KnownZero.countLeadingOnes() + | 
|  | 205 | KnownZero2.countLeadingOnes(), | 
|  | 206 | BitWidth) - BitWidth; | 
|  | 207 |  | 
|  | 208 | TrailZ = std::min(TrailZ, BitWidth); | 
|  | 209 | LeadZ = std::min(LeadZ, BitWidth); | 
|  | 210 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) | | 
|  | 211 | APInt::getHighBitsSet(BitWidth, LeadZ); | 
|  | 212 | KnownZero &= Mask; | 
|  | 213 | return; | 
|  | 214 | } | 
|  | 215 | case Instruction::UDiv: { | 
|  | 216 | // For the purposes of computing leading zeros we can conservatively | 
|  | 217 | // treat a udiv as a logical right shift by the power of 2 known to | 
|  | 218 | // be less than the denominator. | 
|  | 219 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); | 
|  | 220 | ComputeMaskedBits(I->getOperand(0), | 
|  | 221 | AllOnes, KnownZero2, KnownOne2, TD, Depth+1); | 
|  | 222 | unsigned LeadZ = KnownZero2.countLeadingOnes(); | 
|  | 223 |  | 
| Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 224 | KnownOne2.clearAllBits(); | 
|  | 225 | KnownZero2.clearAllBits(); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 226 | ComputeMaskedBits(I->getOperand(1), | 
|  | 227 | AllOnes, KnownZero2, KnownOne2, TD, Depth+1); | 
|  | 228 | unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros(); | 
|  | 229 | if (RHSUnknownLeadingOnes != BitWidth) | 
|  | 230 | LeadZ = std::min(BitWidth, | 
|  | 231 | LeadZ + BitWidth - RHSUnknownLeadingOnes - 1); | 
|  | 232 |  | 
|  | 233 | KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask; | 
|  | 234 | return; | 
|  | 235 | } | 
|  | 236 | case Instruction::Select: | 
|  | 237 | ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, TD, Depth+1); | 
|  | 238 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, TD, | 
|  | 239 | Depth+1); | 
|  | 240 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
|  | 241 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | 
|  | 242 |  | 
|  | 243 | // Only known if known in both the LHS and RHS. | 
|  | 244 | KnownOne &= KnownOne2; | 
|  | 245 | KnownZero &= KnownZero2; | 
|  | 246 | return; | 
|  | 247 | case Instruction::FPTrunc: | 
|  | 248 | case Instruction::FPExt: | 
|  | 249 | case Instruction::FPToUI: | 
|  | 250 | case Instruction::FPToSI: | 
|  | 251 | case Instruction::SIToFP: | 
|  | 252 | case Instruction::UIToFP: | 
|  | 253 | return; // Can't work with floating point. | 
|  | 254 | case Instruction::PtrToInt: | 
|  | 255 | case Instruction::IntToPtr: | 
|  | 256 | // We can't handle these if we don't know the pointer size. | 
|  | 257 | if (!TD) return; | 
|  | 258 | // FALL THROUGH and handle them the same as zext/trunc. | 
|  | 259 | case Instruction::ZExt: | 
|  | 260 | case Instruction::Trunc: { | 
| Chris Lattner | 0cdbc7a | 2009-09-08 00:13:52 +0000 | [diff] [blame] | 261 | const Type *SrcTy = I->getOperand(0)->getType(); | 
|  | 262 |  | 
|  | 263 | unsigned SrcBitWidth; | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 264 | // Note that we handle pointer operands here because of inttoptr/ptrtoint | 
|  | 265 | // which fall through here. | 
| Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 266 | if (SrcTy->isPointerTy()) | 
| Chris Lattner | 0cdbc7a | 2009-09-08 00:13:52 +0000 | [diff] [blame] | 267 | SrcBitWidth = TD->getTypeSizeInBits(SrcTy); | 
|  | 268 | else | 
|  | 269 | SrcBitWidth = SrcTy->getScalarSizeInBits(); | 
|  | 270 |  | 
| Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 271 | APInt MaskIn = Mask.zextOrTrunc(SrcBitWidth); | 
|  | 272 | KnownZero = KnownZero.zextOrTrunc(SrcBitWidth); | 
|  | 273 | KnownOne = KnownOne.zextOrTrunc(SrcBitWidth); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 274 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, TD, | 
|  | 275 | Depth+1); | 
| Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 276 | KnownZero = KnownZero.zextOrTrunc(BitWidth); | 
|  | 277 | KnownOne = KnownOne.zextOrTrunc(BitWidth); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 278 | // Any top bits are known to be zero. | 
|  | 279 | if (BitWidth > SrcBitWidth) | 
|  | 280 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); | 
|  | 281 | return; | 
|  | 282 | } | 
|  | 283 | case Instruction::BitCast: { | 
|  | 284 | const Type *SrcTy = I->getOperand(0)->getType(); | 
| Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 285 | if ((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && | 
| Chris Lattner | edb8407 | 2009-07-02 16:04:08 +0000 | [diff] [blame] | 286 | // TODO: For now, not handling conversions like: | 
|  | 287 | // (bitcast i64 %x to <2 x i32>) | 
| Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 288 | !I->getType()->isVectorTy()) { | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 289 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, TD, | 
|  | 290 | Depth+1); | 
|  | 291 | return; | 
|  | 292 | } | 
|  | 293 | break; | 
|  | 294 | } | 
|  | 295 | case Instruction::SExt: { | 
|  | 296 | // Compute the bits in the result that are not present in the input. | 
| Chris Lattner | 0cdbc7a | 2009-09-08 00:13:52 +0000 | [diff] [blame] | 297 | unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits(); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 298 |  | 
| Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 299 | APInt MaskIn = Mask.trunc(SrcBitWidth); | 
|  | 300 | KnownZero = KnownZero.trunc(SrcBitWidth); | 
|  | 301 | KnownOne = KnownOne.trunc(SrcBitWidth); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 302 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, TD, | 
|  | 303 | Depth+1); | 
|  | 304 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
| Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 305 | KnownZero = KnownZero.zext(BitWidth); | 
|  | 306 | KnownOne = KnownOne.zext(BitWidth); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 307 |  | 
|  | 308 | // If the sign bit of the input is known set or clear, then we know the | 
|  | 309 | // top bits of the result. | 
|  | 310 | if (KnownZero[SrcBitWidth-1])             // Input sign bit known zero | 
|  | 311 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); | 
|  | 312 | else if (KnownOne[SrcBitWidth-1])           // Input sign bit known set | 
|  | 313 | KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); | 
|  | 314 | return; | 
|  | 315 | } | 
|  | 316 | case Instruction::Shl: | 
|  | 317 | // (shl X, C1) & C2 == 0   iff   (X & C2 >>u C1) == 0 | 
|  | 318 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
|  | 319 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); | 
|  | 320 | APInt Mask2(Mask.lshr(ShiftAmt)); | 
|  | 321 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD, | 
|  | 322 | Depth+1); | 
|  | 323 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
|  | 324 | KnownZero <<= ShiftAmt; | 
|  | 325 | KnownOne  <<= ShiftAmt; | 
|  | 326 | KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0 | 
|  | 327 | return; | 
|  | 328 | } | 
|  | 329 | break; | 
|  | 330 | case Instruction::LShr: | 
|  | 331 | // (ushr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0 | 
|  | 332 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
|  | 333 | // Compute the new bits that are at the top now. | 
|  | 334 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); | 
|  | 335 |  | 
|  | 336 | // Unsigned shift right. | 
|  | 337 | APInt Mask2(Mask.shl(ShiftAmt)); | 
|  | 338 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne, TD, | 
|  | 339 | Depth+1); | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 340 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 341 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); | 
|  | 342 | KnownOne  = APIntOps::lshr(KnownOne, ShiftAmt); | 
|  | 343 | // high bits known zero. | 
|  | 344 | KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt); | 
|  | 345 | return; | 
|  | 346 | } | 
|  | 347 | break; | 
|  | 348 | case Instruction::AShr: | 
|  | 349 | // (ashr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0 | 
|  | 350 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
|  | 351 | // Compute the new bits that are at the top now. | 
| Chris Lattner | c86e67e | 2011-01-04 18:19:15 +0000 | [diff] [blame] | 352 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 353 |  | 
|  | 354 | // Signed shift right. | 
|  | 355 | APInt Mask2(Mask.shl(ShiftAmt)); | 
|  | 356 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD, | 
|  | 357 | Depth+1); | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 358 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 359 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); | 
|  | 360 | KnownOne  = APIntOps::lshr(KnownOne, ShiftAmt); | 
|  | 361 |  | 
|  | 362 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); | 
|  | 363 | if (KnownZero[BitWidth-ShiftAmt-1])    // New bits are known zero. | 
|  | 364 | KnownZero |= HighBits; | 
|  | 365 | else if (KnownOne[BitWidth-ShiftAmt-1])  // New bits are known one. | 
|  | 366 | KnownOne |= HighBits; | 
|  | 367 | return; | 
|  | 368 | } | 
|  | 369 | break; | 
|  | 370 | case Instruction::Sub: { | 
|  | 371 | if (ConstantInt *CLHS = dyn_cast<ConstantInt>(I->getOperand(0))) { | 
|  | 372 | // We know that the top bits of C-X are clear if X contains less bits | 
|  | 373 | // than C (i.e. no wrap-around can happen).  For example, 20-X is | 
|  | 374 | // positive if we can prove that X is >= 0 and < 16. | 
|  | 375 | if (!CLHS->getValue().isNegative()) { | 
|  | 376 | unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros(); | 
|  | 377 | // NLZ can't be BitWidth with no sign bit | 
|  | 378 | APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1); | 
|  | 379 | ComputeMaskedBits(I->getOperand(1), MaskV, KnownZero2, KnownOne2, | 
|  | 380 | TD, Depth+1); | 
|  | 381 |  | 
|  | 382 | // If all of the MaskV bits are known to be zero, then we know the | 
|  | 383 | // output top bits are zero, because we now know that the output is | 
|  | 384 | // from [0-C]. | 
|  | 385 | if ((KnownZero2 & MaskV) == MaskV) { | 
|  | 386 | unsigned NLZ2 = CLHS->getValue().countLeadingZeros(); | 
|  | 387 | // Top bits known zero. | 
|  | 388 | KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask; | 
|  | 389 | } | 
|  | 390 | } | 
|  | 391 | } | 
|  | 392 | } | 
|  | 393 | // fall through | 
|  | 394 | case Instruction::Add: { | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 395 | // If one of the operands has trailing zeros, then the bits that the | 
| Dan Gohman | 071156c | 2009-05-24 18:02:35 +0000 | [diff] [blame] | 396 | // other operand has in those bit positions will be preserved in the | 
|  | 397 | // result. For an add, this works with either operand. For a subtract, | 
|  | 398 | // this only works if the known zeros are in the right operand. | 
|  | 399 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); | 
|  | 400 | APInt Mask2 = APInt::getLowBitsSet(BitWidth, | 
|  | 401 | BitWidth - Mask.countLeadingZeros()); | 
|  | 402 | ComputeMaskedBits(I->getOperand(0), Mask2, LHSKnownZero, LHSKnownOne, TD, | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 403 | Depth+1); | 
| Dan Gohman | 071156c | 2009-05-24 18:02:35 +0000 | [diff] [blame] | 404 | assert((LHSKnownZero & LHSKnownOne) == 0 && | 
|  | 405 | "Bits known to be one AND zero?"); | 
|  | 406 | unsigned LHSKnownZeroOut = LHSKnownZero.countTrailingOnes(); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 407 |  | 
|  | 408 | ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero2, KnownOne2, TD, | 
|  | 409 | Depth+1); | 
|  | 410 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | 
| Dan Gohman | 071156c | 2009-05-24 18:02:35 +0000 | [diff] [blame] | 411 | unsigned RHSKnownZeroOut = KnownZero2.countTrailingOnes(); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 412 |  | 
| Dan Gohman | 071156c | 2009-05-24 18:02:35 +0000 | [diff] [blame] | 413 | // Determine which operand has more trailing zeros, and use that | 
|  | 414 | // many bits from the other operand. | 
|  | 415 | if (LHSKnownZeroOut > RHSKnownZeroOut) { | 
| Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 416 | if (I->getOpcode() == Instruction::Add) { | 
| Dan Gohman | 071156c | 2009-05-24 18:02:35 +0000 | [diff] [blame] | 417 | APInt Mask = APInt::getLowBitsSet(BitWidth, LHSKnownZeroOut); | 
|  | 418 | KnownZero |= KnownZero2 & Mask; | 
|  | 419 | KnownOne  |= KnownOne2 & Mask; | 
|  | 420 | } else { | 
|  | 421 | // If the known zeros are in the left operand for a subtract, | 
|  | 422 | // fall back to the minimum known zeros in both operands. | 
|  | 423 | KnownZero |= APInt::getLowBitsSet(BitWidth, | 
|  | 424 | std::min(LHSKnownZeroOut, | 
|  | 425 | RHSKnownZeroOut)); | 
|  | 426 | } | 
|  | 427 | } else if (RHSKnownZeroOut >= LHSKnownZeroOut) { | 
|  | 428 | APInt Mask = APInt::getLowBitsSet(BitWidth, RHSKnownZeroOut); | 
|  | 429 | KnownZero |= LHSKnownZero & Mask; | 
|  | 430 | KnownOne  |= LHSKnownOne & Mask; | 
|  | 431 | } | 
| Nick Lewycky | cc79973 | 2011-03-11 09:00:19 +0000 | [diff] [blame] | 432 |  | 
|  | 433 | // Are we still trying to solve for the sign bit? | 
| Benjamin Kramer | 5acc751 | 2011-03-12 17:18:11 +0000 | [diff] [blame] | 434 | if (Mask.isNegative() && !KnownZero.isNegative() && !KnownOne.isNegative()){ | 
| Nick Lewycky | cc79973 | 2011-03-11 09:00:19 +0000 | [diff] [blame] | 435 | OverflowingBinaryOperator *OBO = cast<OverflowingBinaryOperator>(I); | 
|  | 436 | if (OBO->hasNoSignedWrap()) { | 
| Benjamin Kramer | 5acc751 | 2011-03-12 17:18:11 +0000 | [diff] [blame] | 437 | if (I->getOpcode() == Instruction::Add) { | 
|  | 438 | // Adding two positive numbers can't wrap into negative | 
|  | 439 | if (LHSKnownZero.isNegative() && KnownZero2.isNegative()) | 
|  | 440 | KnownZero |= APInt::getSignBit(BitWidth); | 
|  | 441 | // and adding two negative numbers can't wrap into positive. | 
|  | 442 | else if (LHSKnownOne.isNegative() && KnownOne2.isNegative()) | 
|  | 443 | KnownOne |= APInt::getSignBit(BitWidth); | 
|  | 444 | } else { | 
|  | 445 | // Subtracting a negative number from a positive one can't wrap | 
|  | 446 | if (LHSKnownZero.isNegative() && KnownOne2.isNegative()) | 
|  | 447 | KnownZero |= APInt::getSignBit(BitWidth); | 
|  | 448 | // neither can subtracting a positive number from a negative one. | 
|  | 449 | else if (LHSKnownOne.isNegative() && KnownZero2.isNegative()) | 
|  | 450 | KnownOne |= APInt::getSignBit(BitWidth); | 
|  | 451 | } | 
| Nick Lewycky | cc79973 | 2011-03-11 09:00:19 +0000 | [diff] [blame] | 452 | } | 
|  | 453 | } | 
|  | 454 |  | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 455 | return; | 
|  | 456 | } | 
|  | 457 | case Instruction::SRem: | 
|  | 458 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
| Duncan Sands | 26cd6bd | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 459 | APInt RA = Rem->getValue().abs(); | 
|  | 460 | if (RA.isPowerOf2()) { | 
|  | 461 | APInt LowBits = RA - 1; | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 462 | APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); | 
|  | 463 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD, | 
|  | 464 | Depth+1); | 
|  | 465 |  | 
| Duncan Sands | 26cd6bd | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 466 | // The low bits of the first operand are unchanged by the srem. | 
|  | 467 | KnownZero = KnownZero2 & LowBits; | 
|  | 468 | KnownOne = KnownOne2 & LowBits; | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 469 |  | 
| Duncan Sands | 26cd6bd | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 470 | // If the first operand is non-negative or has all low bits zero, then | 
|  | 471 | // the upper bits are all zero. | 
|  | 472 | if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits)) | 
|  | 473 | KnownZero |= ~LowBits; | 
|  | 474 |  | 
|  | 475 | // If the first operand is negative and not all low bits are zero, then | 
|  | 476 | // the upper bits are all one. | 
|  | 477 | if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0)) | 
|  | 478 | KnownOne |= ~LowBits; | 
|  | 479 |  | 
|  | 480 | KnownZero &= Mask; | 
|  | 481 | KnownOne &= Mask; | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 482 |  | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 483 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 484 | } | 
|  | 485 | } | 
| Nick Lewycky | e467979 | 2011-03-07 01:50:10 +0000 | [diff] [blame] | 486 |  | 
|  | 487 | // The sign bit is the LHS's sign bit, except when the result of the | 
|  | 488 | // remainder is zero. | 
|  | 489 | if (Mask.isNegative() && KnownZero.isNonNegative()) { | 
|  | 490 | APInt Mask2 = APInt::getSignBit(BitWidth); | 
|  | 491 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); | 
|  | 492 | ComputeMaskedBits(I->getOperand(0), Mask2, LHSKnownZero, LHSKnownOne, TD, | 
|  | 493 | Depth+1); | 
|  | 494 | // If it's known zero, our sign bit is also zero. | 
|  | 495 | if (LHSKnownZero.isNegative()) | 
|  | 496 | KnownZero |= LHSKnownZero; | 
|  | 497 | } | 
|  | 498 |  | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 499 | break; | 
|  | 500 | case Instruction::URem: { | 
|  | 501 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
|  | 502 | APInt RA = Rem->getValue(); | 
|  | 503 | if (RA.isPowerOf2()) { | 
|  | 504 | APInt LowBits = (RA - 1); | 
|  | 505 | APInt Mask2 = LowBits & Mask; | 
|  | 506 | KnownZero |= ~LowBits & Mask; | 
|  | 507 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD, | 
|  | 508 | Depth+1); | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 509 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 510 | break; | 
|  | 511 | } | 
|  | 512 | } | 
|  | 513 |  | 
|  | 514 | // Since the result is less than or equal to either operand, any leading | 
|  | 515 | // zero bits in either operand must also exist in the result. | 
|  | 516 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); | 
|  | 517 | ComputeMaskedBits(I->getOperand(0), AllOnes, KnownZero, KnownOne, | 
|  | 518 | TD, Depth+1); | 
|  | 519 | ComputeMaskedBits(I->getOperand(1), AllOnes, KnownZero2, KnownOne2, | 
|  | 520 | TD, Depth+1); | 
|  | 521 |  | 
| Chris Lattner | 4612ae1 | 2009-01-20 18:22:57 +0000 | [diff] [blame] | 522 | unsigned Leaders = std::max(KnownZero.countLeadingOnes(), | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 523 | KnownZero2.countLeadingOnes()); | 
| Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 524 | KnownOne.clearAllBits(); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 525 | KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask; | 
|  | 526 | break; | 
|  | 527 | } | 
|  | 528 |  | 
| Victor Hernandez | a3aaf85 | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 529 | case Instruction::Alloca: { | 
| Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 530 | AllocaInst *AI = cast<AllocaInst>(V); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 531 | unsigned Align = AI->getAlignment(); | 
| Victor Hernandez | a3aaf85 | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 532 | if (Align == 0 && TD) | 
|  | 533 | Align = TD->getABITypeAlignment(AI->getType()->getElementType()); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 534 |  | 
|  | 535 | if (Align > 0) | 
|  | 536 | KnownZero = Mask & APInt::getLowBitsSet(BitWidth, | 
|  | 537 | CountTrailingZeros_32(Align)); | 
|  | 538 | break; | 
|  | 539 | } | 
|  | 540 | case Instruction::GetElementPtr: { | 
|  | 541 | // Analyze all of the subscripts of this getelementptr instruction | 
|  | 542 | // to determine if we can prove known low zero bits. | 
|  | 543 | APInt LocalMask = APInt::getAllOnesValue(BitWidth); | 
|  | 544 | APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0); | 
|  | 545 | ComputeMaskedBits(I->getOperand(0), LocalMask, | 
|  | 546 | LocalKnownZero, LocalKnownOne, TD, Depth+1); | 
|  | 547 | unsigned TrailZ = LocalKnownZero.countTrailingOnes(); | 
|  | 548 |  | 
|  | 549 | gep_type_iterator GTI = gep_type_begin(I); | 
|  | 550 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) { | 
|  | 551 | Value *Index = I->getOperand(i); | 
|  | 552 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { | 
|  | 553 | // Handle struct member offset arithmetic. | 
|  | 554 | if (!TD) return; | 
|  | 555 | const StructLayout *SL = TD->getStructLayout(STy); | 
|  | 556 | unsigned Idx = cast<ConstantInt>(Index)->getZExtValue(); | 
|  | 557 | uint64_t Offset = SL->getElementOffset(Idx); | 
|  | 558 | TrailZ = std::min(TrailZ, | 
|  | 559 | CountTrailingZeros_64(Offset)); | 
|  | 560 | } else { | 
|  | 561 | // Handle array index arithmetic. | 
|  | 562 | const Type *IndexedTy = GTI.getIndexedType(); | 
|  | 563 | if (!IndexedTy->isSized()) return; | 
| Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 564 | unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits(); | 
| Duncan Sands | af9eaa8 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 565 | uint64_t TypeSize = TD ? TD->getTypeAllocSize(IndexedTy) : 1; | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 566 | LocalMask = APInt::getAllOnesValue(GEPOpiBits); | 
|  | 567 | LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0); | 
|  | 568 | ComputeMaskedBits(Index, LocalMask, | 
|  | 569 | LocalKnownZero, LocalKnownOne, TD, Depth+1); | 
|  | 570 | TrailZ = std::min(TrailZ, | 
| Chris Lattner | 4612ae1 | 2009-01-20 18:22:57 +0000 | [diff] [blame] | 571 | unsigned(CountTrailingZeros_64(TypeSize) + | 
|  | 572 | LocalKnownZero.countTrailingOnes())); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 573 | } | 
|  | 574 | } | 
|  | 575 |  | 
|  | 576 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) & Mask; | 
|  | 577 | break; | 
|  | 578 | } | 
|  | 579 | case Instruction::PHI: { | 
|  | 580 | PHINode *P = cast<PHINode>(I); | 
|  | 581 | // Handle the case of a simple two-predecessor recurrence PHI. | 
|  | 582 | // There's a lot more that could theoretically be done here, but | 
|  | 583 | // this is sufficient to catch some interesting cases. | 
|  | 584 | if (P->getNumIncomingValues() == 2) { | 
|  | 585 | for (unsigned i = 0; i != 2; ++i) { | 
|  | 586 | Value *L = P->getIncomingValue(i); | 
|  | 587 | Value *R = P->getIncomingValue(!i); | 
| Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 588 | Operator *LU = dyn_cast<Operator>(L); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 589 | if (!LU) | 
|  | 590 | continue; | 
| Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 591 | unsigned Opcode = LU->getOpcode(); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 592 | // Check for operations that have the property that if | 
|  | 593 | // both their operands have low zero bits, the result | 
|  | 594 | // will have low zero bits. | 
|  | 595 | if (Opcode == Instruction::Add || | 
|  | 596 | Opcode == Instruction::Sub || | 
|  | 597 | Opcode == Instruction::And || | 
|  | 598 | Opcode == Instruction::Or || | 
|  | 599 | Opcode == Instruction::Mul) { | 
|  | 600 | Value *LL = LU->getOperand(0); | 
|  | 601 | Value *LR = LU->getOperand(1); | 
|  | 602 | // Find a recurrence. | 
|  | 603 | if (LL == I) | 
|  | 604 | L = LR; | 
|  | 605 | else if (LR == I) | 
|  | 606 | L = LL; | 
|  | 607 | else | 
|  | 608 | break; | 
|  | 609 | // Ok, we have a PHI of the form L op= R. Check for low | 
|  | 610 | // zero bits. | 
|  | 611 | APInt Mask2 = APInt::getAllOnesValue(BitWidth); | 
|  | 612 | ComputeMaskedBits(R, Mask2, KnownZero2, KnownOne2, TD, Depth+1); | 
|  | 613 | Mask2 = APInt::getLowBitsSet(BitWidth, | 
|  | 614 | KnownZero2.countTrailingOnes()); | 
| David Greene | aebd9e0 | 2008-10-27 23:24:03 +0000 | [diff] [blame] | 615 |  | 
|  | 616 | // We need to take the minimum number of known bits | 
|  | 617 | APInt KnownZero3(KnownZero), KnownOne3(KnownOne); | 
|  | 618 | ComputeMaskedBits(L, Mask2, KnownZero3, KnownOne3, TD, Depth+1); | 
|  | 619 |  | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 620 | KnownZero = Mask & | 
|  | 621 | APInt::getLowBitsSet(BitWidth, | 
| David Greene | aebd9e0 | 2008-10-27 23:24:03 +0000 | [diff] [blame] | 622 | std::min(KnownZero2.countTrailingOnes(), | 
|  | 623 | KnownZero3.countTrailingOnes())); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 624 | break; | 
|  | 625 | } | 
|  | 626 | } | 
|  | 627 | } | 
| Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 628 |  | 
| Nick Lewycky | ac0b62c | 2011-02-10 23:54:10 +0000 | [diff] [blame] | 629 | // Unreachable blocks may have zero-operand PHI nodes. | 
|  | 630 | if (P->getNumIncomingValues() == 0) | 
|  | 631 | return; | 
|  | 632 |  | 
| Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 633 | // Otherwise take the unions of the known bit sets of the operands, | 
|  | 634 | // taking conservative care to avoid excessive recursion. | 
|  | 635 | if (Depth < MaxDepth - 1 && !KnownZero && !KnownOne) { | 
| Duncan Sands | 7dc3d47 | 2011-03-08 12:39:03 +0000 | [diff] [blame] | 636 | // Skip if every incoming value references to ourself. | 
|  | 637 | if (P->hasConstantValue() == P) | 
|  | 638 | break; | 
|  | 639 |  | 
| Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 640 | KnownZero = APInt::getAllOnesValue(BitWidth); | 
|  | 641 | KnownOne = APInt::getAllOnesValue(BitWidth); | 
|  | 642 | for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) { | 
|  | 643 | // Skip direct self references. | 
|  | 644 | if (P->getIncomingValue(i) == P) continue; | 
|  | 645 |  | 
|  | 646 | KnownZero2 = APInt(BitWidth, 0); | 
|  | 647 | KnownOne2 = APInt(BitWidth, 0); | 
|  | 648 | // Recurse, but cap the recursion to one level, because we don't | 
|  | 649 | // want to waste time spinning around in loops. | 
|  | 650 | ComputeMaskedBits(P->getIncomingValue(i), KnownZero | KnownOne, | 
|  | 651 | KnownZero2, KnownOne2, TD, MaxDepth-1); | 
|  | 652 | KnownZero &= KnownZero2; | 
|  | 653 | KnownOne &= KnownOne2; | 
|  | 654 | // If all bits have been ruled out, there's no need to check | 
|  | 655 | // more operands. | 
|  | 656 | if (!KnownZero && !KnownOne) | 
|  | 657 | break; | 
|  | 658 | } | 
|  | 659 | } | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 660 | break; | 
|  | 661 | } | 
|  | 662 | case Instruction::Call: | 
|  | 663 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { | 
|  | 664 | switch (II->getIntrinsicID()) { | 
|  | 665 | default: break; | 
|  | 666 | case Intrinsic::ctpop: | 
|  | 667 | case Intrinsic::ctlz: | 
|  | 668 | case Intrinsic::cttz: { | 
|  | 669 | unsigned LowBits = Log2_32(BitWidth)+1; | 
|  | 670 | KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits); | 
|  | 671 | break; | 
|  | 672 | } | 
|  | 673 | } | 
|  | 674 | } | 
|  | 675 | break; | 
|  | 676 | } | 
|  | 677 | } | 
|  | 678 |  | 
| Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 679 | /// ComputeSignBit - Determine whether the sign bit is known to be zero or | 
|  | 680 | /// one.  Convenience wrapper around ComputeMaskedBits. | 
|  | 681 | void llvm::ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, | 
|  | 682 | const TargetData *TD, unsigned Depth) { | 
|  | 683 | unsigned BitWidth = getBitWidth(V->getType(), TD); | 
|  | 684 | if (!BitWidth) { | 
|  | 685 | KnownZero = false; | 
|  | 686 | KnownOne = false; | 
|  | 687 | return; | 
|  | 688 | } | 
|  | 689 | APInt ZeroBits(BitWidth, 0); | 
|  | 690 | APInt OneBits(BitWidth, 0); | 
|  | 691 | ComputeMaskedBits(V, APInt::getSignBit(BitWidth), ZeroBits, OneBits, TD, | 
|  | 692 | Depth); | 
|  | 693 | KnownOne = OneBits[BitWidth - 1]; | 
|  | 694 | KnownZero = ZeroBits[BitWidth - 1]; | 
|  | 695 | } | 
|  | 696 |  | 
|  | 697 | /// isPowerOfTwo - Return true if the given value is known to have exactly one | 
|  | 698 | /// bit set when defined. For vectors return true if every element is known to | 
|  | 699 | /// be a power of two when defined.  Supports values with integer or pointer | 
|  | 700 | /// types and vectors of integers. | 
|  | 701 | bool llvm::isPowerOfTwo(Value *V, const TargetData *TD, unsigned Depth) { | 
|  | 702 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) | 
| Duncan Sands | 8a33733 | 2011-01-26 08:44:16 +0000 | [diff] [blame] | 703 | return CI->getValue().isPowerOf2(); | 
| Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 704 | // TODO: Handle vector constants. | 
|  | 705 |  | 
|  | 706 | // 1 << X is clearly a power of two if the one is not shifted off the end.  If | 
|  | 707 | // it is shifted off the end then the result is undefined. | 
|  | 708 | if (match(V, m_Shl(m_One(), m_Value()))) | 
|  | 709 | return true; | 
|  | 710 |  | 
|  | 711 | // (signbit) >>l X is clearly a power of two if the one is not shifted off the | 
|  | 712 | // bottom.  If it is shifted off the bottom then the result is undefined. | 
| Duncan Sands | 4b397fc | 2011-02-01 08:50:33 +0000 | [diff] [blame] | 713 | if (match(V, m_LShr(m_SignBit(), m_Value()))) | 
| Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 714 | return true; | 
|  | 715 |  | 
|  | 716 | // The remaining tests are all recursive, so bail out if we hit the limit. | 
|  | 717 | if (Depth++ == MaxDepth) | 
|  | 718 | return false; | 
|  | 719 |  | 
|  | 720 | if (ZExtInst *ZI = dyn_cast<ZExtInst>(V)) | 
|  | 721 | return isPowerOfTwo(ZI->getOperand(0), TD, Depth); | 
|  | 722 |  | 
|  | 723 | if (SelectInst *SI = dyn_cast<SelectInst>(V)) | 
|  | 724 | return isPowerOfTwo(SI->getTrueValue(), TD, Depth) && | 
|  | 725 | isPowerOfTwo(SI->getFalseValue(), TD, Depth); | 
|  | 726 |  | 
| Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 727 | // An exact divide or right shift can only shift off zero bits, so the result | 
| Nick Lewycky | f0469af | 2011-03-21 21:40:32 +0000 | [diff] [blame] | 728 | // is a power of two only if the first operand is a power of two and not | 
|  | 729 | // copying a sign bit (sdiv int_min, 2). | 
|  | 730 | if (match(V, m_LShr(m_Value(), m_Value())) || | 
|  | 731 | match(V, m_UDiv(m_Value(), m_Value()))) { | 
| Eli Friedman | 8baa2c7 | 2011-04-02 22:11:56 +0000 | [diff] [blame] | 732 | PossiblyExactOperator *PEO = cast<PossiblyExactOperator>(V); | 
|  | 733 | if (PEO->isExact()) | 
|  | 734 | return isPowerOfTwo(PEO->getOperand(0), TD, Depth); | 
| Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 735 | } | 
|  | 736 |  | 
| Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 737 | return false; | 
|  | 738 | } | 
|  | 739 |  | 
|  | 740 | /// isKnownNonZero - Return true if the given value is known to be non-zero | 
|  | 741 | /// when defined.  For vectors return true if every element is known to be | 
|  | 742 | /// non-zero when defined.  Supports values with integer or pointer type and | 
|  | 743 | /// vectors of integers. | 
|  | 744 | bool llvm::isKnownNonZero(Value *V, const TargetData *TD, unsigned Depth) { | 
|  | 745 | if (Constant *C = dyn_cast<Constant>(V)) { | 
|  | 746 | if (C->isNullValue()) | 
|  | 747 | return false; | 
|  | 748 | if (isa<ConstantInt>(C)) | 
|  | 749 | // Must be non-zero due to null test above. | 
|  | 750 | return true; | 
|  | 751 | // TODO: Handle vectors | 
|  | 752 | return false; | 
|  | 753 | } | 
|  | 754 |  | 
|  | 755 | // The remaining tests are all recursive, so bail out if we hit the limit. | 
|  | 756 | if (Depth++ == MaxDepth) | 
|  | 757 | return false; | 
|  | 758 |  | 
|  | 759 | unsigned BitWidth = getBitWidth(V->getType(), TD); | 
|  | 760 |  | 
|  | 761 | // X | Y != 0 if X != 0 or Y != 0. | 
|  | 762 | Value *X = 0, *Y = 0; | 
|  | 763 | if (match(V, m_Or(m_Value(X), m_Value(Y)))) | 
|  | 764 | return isKnownNonZero(X, TD, Depth) || isKnownNonZero(Y, TD, Depth); | 
|  | 765 |  | 
|  | 766 | // ext X != 0 if X != 0. | 
|  | 767 | if (isa<SExtInst>(V) || isa<ZExtInst>(V)) | 
|  | 768 | return isKnownNonZero(cast<Instruction>(V)->getOperand(0), TD, Depth); | 
|  | 769 |  | 
| Duncan Sands | 2e9e4f1 | 2011-01-29 13:27:00 +0000 | [diff] [blame] | 770 | // shl X, Y != 0 if X is odd.  Note that the value of the shift is undefined | 
| Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 771 | // if the lowest bit is shifted off the end. | 
|  | 772 | if (BitWidth && match(V, m_Shl(m_Value(X), m_Value(Y)))) { | 
| Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 773 | // shl nuw can't remove any non-zero bits. | 
|  | 774 | BinaryOperator *BO = cast<BinaryOperator>(V); | 
|  | 775 | if (BO->hasNoUnsignedWrap()) | 
|  | 776 | return isKnownNonZero(X, TD, Depth); | 
|  | 777 |  | 
| Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 778 | APInt KnownZero(BitWidth, 0); | 
|  | 779 | APInt KnownOne(BitWidth, 0); | 
| Duncan Sands | 2e9e4f1 | 2011-01-29 13:27:00 +0000 | [diff] [blame] | 780 | ComputeMaskedBits(X, APInt(BitWidth, 1), KnownZero, KnownOne, TD, Depth); | 
| Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 781 | if (KnownOne[0]) | 
|  | 782 | return true; | 
|  | 783 | } | 
| Duncan Sands | 2e9e4f1 | 2011-01-29 13:27:00 +0000 | [diff] [blame] | 784 | // shr X, Y != 0 if X is negative.  Note that the value of the shift is not | 
| Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 785 | // defined if the sign bit is shifted off the end. | 
|  | 786 | else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) { | 
| Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 787 | // shr exact can only shift out zero bits. | 
|  | 788 | BinaryOperator *BO = cast<BinaryOperator>(V); | 
|  | 789 | if (BO->isExact()) | 
|  | 790 | return isKnownNonZero(X, TD, Depth); | 
|  | 791 |  | 
| Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 792 | bool XKnownNonNegative, XKnownNegative; | 
|  | 793 | ComputeSignBit(X, XKnownNonNegative, XKnownNegative, TD, Depth); | 
|  | 794 | if (XKnownNegative) | 
|  | 795 | return true; | 
|  | 796 | } | 
| Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 797 | // div exact can only produce a zero if the dividend is zero. | 
|  | 798 | else if (match(V, m_IDiv(m_Value(X), m_Value()))) { | 
|  | 799 | BinaryOperator *BO = cast<BinaryOperator>(V); | 
|  | 800 | if (BO->isExact()) | 
|  | 801 | return isKnownNonZero(X, TD, Depth); | 
|  | 802 | } | 
| Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 803 | // X + Y. | 
|  | 804 | else if (match(V, m_Add(m_Value(X), m_Value(Y)))) { | 
|  | 805 | bool XKnownNonNegative, XKnownNegative; | 
|  | 806 | bool YKnownNonNegative, YKnownNegative; | 
|  | 807 | ComputeSignBit(X, XKnownNonNegative, XKnownNegative, TD, Depth); | 
|  | 808 | ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, TD, Depth); | 
|  | 809 |  | 
|  | 810 | // If X and Y are both non-negative (as signed values) then their sum is not | 
| Duncan Sands | 9e9d5b2 | 2011-01-25 15:14:15 +0000 | [diff] [blame] | 811 | // zero unless both X and Y are zero. | 
| Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 812 | if (XKnownNonNegative && YKnownNonNegative) | 
| Duncan Sands | 9e9d5b2 | 2011-01-25 15:14:15 +0000 | [diff] [blame] | 813 | if (isKnownNonZero(X, TD, Depth) || isKnownNonZero(Y, TD, Depth)) | 
|  | 814 | return true; | 
| Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 815 |  | 
|  | 816 | // If X and Y are both negative (as signed values) then their sum is not | 
|  | 817 | // zero unless both X and Y equal INT_MIN. | 
|  | 818 | if (BitWidth && XKnownNegative && YKnownNegative) { | 
|  | 819 | APInt KnownZero(BitWidth, 0); | 
|  | 820 | APInt KnownOne(BitWidth, 0); | 
|  | 821 | APInt Mask = APInt::getSignedMaxValue(BitWidth); | 
|  | 822 | // The sign bit of X is set.  If some other bit is set then X is not equal | 
|  | 823 | // to INT_MIN. | 
|  | 824 | ComputeMaskedBits(X, Mask, KnownZero, KnownOne, TD, Depth); | 
|  | 825 | if ((KnownOne & Mask) != 0) | 
|  | 826 | return true; | 
|  | 827 | // The sign bit of Y is set.  If some other bit is set then Y is not equal | 
|  | 828 | // to INT_MIN. | 
|  | 829 | ComputeMaskedBits(Y, Mask, KnownZero, KnownOne, TD, Depth); | 
|  | 830 | if ((KnownOne & Mask) != 0) | 
|  | 831 | return true; | 
|  | 832 | } | 
|  | 833 |  | 
|  | 834 | // The sum of a non-negative number and a power of two is not zero. | 
|  | 835 | if (XKnownNonNegative && isPowerOfTwo(Y, TD, Depth)) | 
|  | 836 | return true; | 
|  | 837 | if (YKnownNonNegative && isPowerOfTwo(X, TD, Depth)) | 
|  | 838 | return true; | 
|  | 839 | } | 
|  | 840 | // (C ? X : Y) != 0 if X != 0 and Y != 0. | 
|  | 841 | else if (SelectInst *SI = dyn_cast<SelectInst>(V)) { | 
|  | 842 | if (isKnownNonZero(SI->getTrueValue(), TD, Depth) && | 
|  | 843 | isKnownNonZero(SI->getFalseValue(), TD, Depth)) | 
|  | 844 | return true; | 
|  | 845 | } | 
|  | 846 |  | 
|  | 847 | if (!BitWidth) return false; | 
|  | 848 | APInt KnownZero(BitWidth, 0); | 
|  | 849 | APInt KnownOne(BitWidth, 0); | 
|  | 850 | ComputeMaskedBits(V, APInt::getAllOnesValue(BitWidth), KnownZero, KnownOne, | 
|  | 851 | TD, Depth); | 
|  | 852 | return KnownOne != 0; | 
|  | 853 | } | 
|  | 854 |  | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 855 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.  We use | 
|  | 856 | /// this predicate to simplify operations downstream.  Mask is known to be zero | 
|  | 857 | /// for bits that V cannot have. | 
| Chris Lattner | 4bc2825 | 2009-09-08 00:06:16 +0000 | [diff] [blame] | 858 | /// | 
|  | 859 | /// This function is defined on values with integer type, values with pointer | 
|  | 860 | /// type (but only if TD is non-null), and vectors of integers.  In the case | 
|  | 861 | /// where V is a vector, the mask, known zero, and known one values are the | 
|  | 862 | /// same width as the vector element, and the bit is set only if it is true | 
|  | 863 | /// for all of the elements in the vector. | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 864 | bool llvm::MaskedValueIsZero(Value *V, const APInt &Mask, | 
| Dan Gohman | 05f1135 | 2009-08-27 17:51:25 +0000 | [diff] [blame] | 865 | const TargetData *TD, unsigned Depth) { | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 866 | APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0); | 
|  | 867 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth); | 
|  | 868 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
|  | 869 | return (KnownZero & Mask) == Mask; | 
|  | 870 | } | 
|  | 871 |  | 
|  | 872 |  | 
|  | 873 |  | 
|  | 874 | /// ComputeNumSignBits - Return the number of times the sign bit of the | 
|  | 875 | /// register is replicated into the other bits.  We know that at least 1 bit | 
|  | 876 | /// is always equal to the sign bit (itself), but other cases can give us | 
|  | 877 | /// information.  For example, immediately after an "ashr X, 2", we know that | 
|  | 878 | /// the top 3 bits are all equal to each other, so we return 3. | 
|  | 879 | /// | 
|  | 880 | /// 'Op' must have a scalar integer type. | 
|  | 881 | /// | 
| Dan Gohman | 05f1135 | 2009-08-27 17:51:25 +0000 | [diff] [blame] | 882 | unsigned llvm::ComputeNumSignBits(Value *V, const TargetData *TD, | 
|  | 883 | unsigned Depth) { | 
| Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 884 | assert((TD || V->getType()->isIntOrIntVectorTy()) && | 
| Dan Gohman | 2636693 | 2009-06-22 22:02:32 +0000 | [diff] [blame] | 885 | "ComputeNumSignBits requires a TargetData object to operate " | 
|  | 886 | "on non-integer values!"); | 
| Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 887 | const Type *Ty = V->getType(); | 
| Dan Gohman | 2636693 | 2009-06-22 22:02:32 +0000 | [diff] [blame] | 888 | unsigned TyBits = TD ? TD->getTypeSizeInBits(V->getType()->getScalarType()) : | 
|  | 889 | Ty->getScalarSizeInBits(); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 890 | unsigned Tmp, Tmp2; | 
|  | 891 | unsigned FirstAnswer = 1; | 
|  | 892 |  | 
| Chris Lattner | 2e01a69 | 2008-06-02 18:39:07 +0000 | [diff] [blame] | 893 | // Note that ConstantInt is handled by the general ComputeMaskedBits case | 
|  | 894 | // below. | 
|  | 895 |  | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 896 | if (Depth == 6) | 
|  | 897 | return 1;  // Limit search depth. | 
|  | 898 |  | 
| Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 899 | Operator *U = dyn_cast<Operator>(V); | 
|  | 900 | switch (Operator::getOpcode(V)) { | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 901 | default: break; | 
|  | 902 | case Instruction::SExt: | 
| Mon P Wang | bb3eac9 | 2009-12-02 04:59:58 +0000 | [diff] [blame] | 903 | Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits(); | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 904 | return ComputeNumSignBits(U->getOperand(0), TD, Depth+1) + Tmp; | 
|  | 905 |  | 
|  | 906 | case Instruction::AShr: | 
|  | 907 | Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1); | 
|  | 908 | // ashr X, C   -> adds C sign bits. | 
|  | 909 | if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) { | 
|  | 910 | Tmp += C->getZExtValue(); | 
|  | 911 | if (Tmp > TyBits) Tmp = TyBits; | 
|  | 912 | } | 
| Nate Begeman | 7aa18bf | 2010-12-17 23:12:19 +0000 | [diff] [blame] | 913 | // vector ashr X, <C, C, C, C>  -> adds C sign bits | 
|  | 914 | if (ConstantVector *C = dyn_cast<ConstantVector>(U->getOperand(1))) { | 
|  | 915 | if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) { | 
|  | 916 | Tmp += CI->getZExtValue(); | 
|  | 917 | if (Tmp > TyBits) Tmp = TyBits; | 
|  | 918 | } | 
|  | 919 | } | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 920 | return Tmp; | 
|  | 921 | case Instruction::Shl: | 
|  | 922 | if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) { | 
|  | 923 | // shl destroys sign bits. | 
|  | 924 | Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1); | 
|  | 925 | if (C->getZExtValue() >= TyBits ||      // Bad shift. | 
|  | 926 | C->getZExtValue() >= Tmp) break;    // Shifted all sign bits out. | 
|  | 927 | return Tmp - C->getZExtValue(); | 
|  | 928 | } | 
|  | 929 | break; | 
|  | 930 | case Instruction::And: | 
|  | 931 | case Instruction::Or: | 
|  | 932 | case Instruction::Xor:    // NOT is handled here. | 
|  | 933 | // Logical binary ops preserve the number of sign bits at the worst. | 
|  | 934 | Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1); | 
|  | 935 | if (Tmp != 1) { | 
|  | 936 | Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1); | 
|  | 937 | FirstAnswer = std::min(Tmp, Tmp2); | 
|  | 938 | // We computed what we know about the sign bits as our first | 
|  | 939 | // answer. Now proceed to the generic code that uses | 
|  | 940 | // ComputeMaskedBits, and pick whichever answer is better. | 
|  | 941 | } | 
|  | 942 | break; | 
|  | 943 |  | 
|  | 944 | case Instruction::Select: | 
|  | 945 | Tmp = ComputeNumSignBits(U->getOperand(1), TD, Depth+1); | 
|  | 946 | if (Tmp == 1) return 1;  // Early out. | 
|  | 947 | Tmp2 = ComputeNumSignBits(U->getOperand(2), TD, Depth+1); | 
|  | 948 | return std::min(Tmp, Tmp2); | 
|  | 949 |  | 
|  | 950 | case Instruction::Add: | 
|  | 951 | // Add can have at most one carry bit.  Thus we know that the output | 
|  | 952 | // is, at worst, one more bit than the inputs. | 
|  | 953 | Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1); | 
|  | 954 | if (Tmp == 1) return 1;  // Early out. | 
|  | 955 |  | 
|  | 956 | // Special case decrementing a value (ADD X, -1): | 
| Dan Gohman | 4f356bb | 2009-02-24 02:00:40 +0000 | [diff] [blame] | 957 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(U->getOperand(1))) | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 958 | if (CRHS->isAllOnesValue()) { | 
|  | 959 | APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0); | 
|  | 960 | APInt Mask = APInt::getAllOnesValue(TyBits); | 
|  | 961 | ComputeMaskedBits(U->getOperand(0), Mask, KnownZero, KnownOne, TD, | 
|  | 962 | Depth+1); | 
|  | 963 |  | 
|  | 964 | // If the input is known to be 0 or 1, the output is 0/-1, which is all | 
|  | 965 | // sign bits set. | 
|  | 966 | if ((KnownZero | APInt(TyBits, 1)) == Mask) | 
|  | 967 | return TyBits; | 
|  | 968 |  | 
|  | 969 | // If we are subtracting one from a positive number, there is no carry | 
|  | 970 | // out of the result. | 
|  | 971 | if (KnownZero.isNegative()) | 
|  | 972 | return Tmp; | 
|  | 973 | } | 
|  | 974 |  | 
|  | 975 | Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1); | 
|  | 976 | if (Tmp2 == 1) return 1; | 
| Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 977 | return std::min(Tmp, Tmp2)-1; | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 978 |  | 
|  | 979 | case Instruction::Sub: | 
|  | 980 | Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1); | 
|  | 981 | if (Tmp2 == 1) return 1; | 
|  | 982 |  | 
|  | 983 | // Handle NEG. | 
|  | 984 | if (ConstantInt *CLHS = dyn_cast<ConstantInt>(U->getOperand(0))) | 
|  | 985 | if (CLHS->isNullValue()) { | 
|  | 986 | APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0); | 
|  | 987 | APInt Mask = APInt::getAllOnesValue(TyBits); | 
|  | 988 | ComputeMaskedBits(U->getOperand(1), Mask, KnownZero, KnownOne, | 
|  | 989 | TD, Depth+1); | 
|  | 990 | // If the input is known to be 0 or 1, the output is 0/-1, which is all | 
|  | 991 | // sign bits set. | 
|  | 992 | if ((KnownZero | APInt(TyBits, 1)) == Mask) | 
|  | 993 | return TyBits; | 
|  | 994 |  | 
|  | 995 | // If the input is known to be positive (the sign bit is known clear), | 
|  | 996 | // the output of the NEG has the same number of sign bits as the input. | 
|  | 997 | if (KnownZero.isNegative()) | 
|  | 998 | return Tmp2; | 
|  | 999 |  | 
|  | 1000 | // Otherwise, we treat this like a SUB. | 
|  | 1001 | } | 
|  | 1002 |  | 
|  | 1003 | // Sub can have at most one carry bit.  Thus we know that the output | 
|  | 1004 | // is, at worst, one more bit than the inputs. | 
|  | 1005 | Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1); | 
|  | 1006 | if (Tmp == 1) return 1;  // Early out. | 
| Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 1007 | return std::min(Tmp, Tmp2)-1; | 
|  | 1008 |  | 
|  | 1009 | case Instruction::PHI: { | 
|  | 1010 | PHINode *PN = cast<PHINode>(U); | 
|  | 1011 | // Don't analyze large in-degree PHIs. | 
|  | 1012 | if (PN->getNumIncomingValues() > 4) break; | 
|  | 1013 |  | 
|  | 1014 | // Take the minimum of all incoming values.  This can't infinitely loop | 
|  | 1015 | // because of our depth threshold. | 
|  | 1016 | Tmp = ComputeNumSignBits(PN->getIncomingValue(0), TD, Depth+1); | 
|  | 1017 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) { | 
|  | 1018 | if (Tmp == 1) return Tmp; | 
|  | 1019 | Tmp = std::min(Tmp, | 
| Evan Cheng | 2a65429 | 2010-03-13 02:20:29 +0000 | [diff] [blame] | 1020 | ComputeNumSignBits(PN->getIncomingValue(i), TD, Depth+1)); | 
| Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 1021 | } | 
|  | 1022 | return Tmp; | 
|  | 1023 | } | 
|  | 1024 |  | 
| Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1025 | case Instruction::Trunc: | 
|  | 1026 | // FIXME: it's tricky to do anything useful for this, but it is an important | 
|  | 1027 | // case for targets like X86. | 
|  | 1028 | break; | 
|  | 1029 | } | 
|  | 1030 |  | 
|  | 1031 | // Finally, if we can prove that the top bits of the result are 0's or 1's, | 
|  | 1032 | // use this information. | 
|  | 1033 | APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0); | 
|  | 1034 | APInt Mask = APInt::getAllOnesValue(TyBits); | 
|  | 1035 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth); | 
|  | 1036 |  | 
|  | 1037 | if (KnownZero.isNegative()) {        // sign bit is 0 | 
|  | 1038 | Mask = KnownZero; | 
|  | 1039 | } else if (KnownOne.isNegative()) {  // sign bit is 1; | 
|  | 1040 | Mask = KnownOne; | 
|  | 1041 | } else { | 
|  | 1042 | // Nothing known. | 
|  | 1043 | return FirstAnswer; | 
|  | 1044 | } | 
|  | 1045 |  | 
|  | 1046 | // Okay, we know that the sign bit in Mask is set.  Use CLZ to determine | 
|  | 1047 | // the number of identical bits in the top of the input value. | 
|  | 1048 | Mask = ~Mask; | 
|  | 1049 | Mask <<= Mask.getBitWidth()-TyBits; | 
|  | 1050 | // Return # leading zeros.  We use 'min' here in case Val was zero before | 
|  | 1051 | // shifting.  We don't want to return '64' as for an i32 "0". | 
|  | 1052 | return std::max(FirstAnswer, std::min(TyBits, Mask.countLeadingZeros())); | 
|  | 1053 | } | 
| Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 1054 |  | 
| Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1055 | /// ComputeMultiple - This function computes the integer multiple of Base that | 
|  | 1056 | /// equals V.  If successful, it returns true and returns the multiple in | 
| Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 1057 | /// Multiple.  If unsuccessful, it returns false. It looks | 
| Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1058 | /// through SExt instructions only if LookThroughSExt is true. | 
|  | 1059 | bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple, | 
| Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 1060 | bool LookThroughSExt, unsigned Depth) { | 
| Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1061 | const unsigned MaxDepth = 6; | 
|  | 1062 |  | 
| Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 1063 | assert(V && "No Value?"); | 
| Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1064 | assert(Depth <= MaxDepth && "Limit Search Depth"); | 
| Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1065 | assert(V->getType()->isIntegerTy() && "Not integer or pointer type!"); | 
| Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1066 |  | 
|  | 1067 | const Type *T = V->getType(); | 
| Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1068 |  | 
| Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 1069 | ConstantInt *CI = dyn_cast<ConstantInt>(V); | 
| Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1070 |  | 
|  | 1071 | if (Base == 0) | 
|  | 1072 | return false; | 
|  | 1073 |  | 
|  | 1074 | if (Base == 1) { | 
|  | 1075 | Multiple = V; | 
|  | 1076 | return true; | 
|  | 1077 | } | 
|  | 1078 |  | 
|  | 1079 | ConstantExpr *CO = dyn_cast<ConstantExpr>(V); | 
|  | 1080 | Constant *BaseVal = ConstantInt::get(T, Base); | 
|  | 1081 | if (CO && CO == BaseVal) { | 
|  | 1082 | // Multiple is 1. | 
|  | 1083 | Multiple = ConstantInt::get(T, 1); | 
|  | 1084 | return true; | 
|  | 1085 | } | 
|  | 1086 |  | 
|  | 1087 | if (CI && CI->getZExtValue() % Base == 0) { | 
|  | 1088 | Multiple = ConstantInt::get(T, CI->getZExtValue() / Base); | 
|  | 1089 | return true; | 
|  | 1090 | } | 
|  | 1091 |  | 
|  | 1092 | if (Depth == MaxDepth) return false;  // Limit search depth. | 
|  | 1093 |  | 
|  | 1094 | Operator *I = dyn_cast<Operator>(V); | 
|  | 1095 | if (!I) return false; | 
|  | 1096 |  | 
|  | 1097 | switch (I->getOpcode()) { | 
|  | 1098 | default: break; | 
| Chris Lattner | 4f0b47d | 2009-11-26 01:50:12 +0000 | [diff] [blame] | 1099 | case Instruction::SExt: | 
| Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1100 | if (!LookThroughSExt) return false; | 
|  | 1101 | // otherwise fall through to ZExt | 
| Chris Lattner | 4f0b47d | 2009-11-26 01:50:12 +0000 | [diff] [blame] | 1102 | case Instruction::ZExt: | 
| Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 1103 | return ComputeMultiple(I->getOperand(0), Base, Multiple, | 
|  | 1104 | LookThroughSExt, Depth+1); | 
| Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1105 | case Instruction::Shl: | 
|  | 1106 | case Instruction::Mul: { | 
|  | 1107 | Value *Op0 = I->getOperand(0); | 
|  | 1108 | Value *Op1 = I->getOperand(1); | 
|  | 1109 |  | 
|  | 1110 | if (I->getOpcode() == Instruction::Shl) { | 
|  | 1111 | ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1); | 
|  | 1112 | if (!Op1CI) return false; | 
|  | 1113 | // Turn Op0 << Op1 into Op0 * 2^Op1 | 
|  | 1114 | APInt Op1Int = Op1CI->getValue(); | 
|  | 1115 | uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1); | 
| Jay Foad | 15084f0 | 2010-11-30 09:02:01 +0000 | [diff] [blame] | 1116 | APInt API(Op1Int.getBitWidth(), 0); | 
| Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 1117 | API.setBit(BitToSet); | 
| Jay Foad | 15084f0 | 2010-11-30 09:02:01 +0000 | [diff] [blame] | 1118 | Op1 = ConstantInt::get(V->getContext(), API); | 
| Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1119 | } | 
|  | 1120 |  | 
|  | 1121 | Value *Mul0 = NULL; | 
| Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 1122 | if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) { | 
|  | 1123 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) | 
|  | 1124 | if (Constant *MulC = dyn_cast<Constant>(Mul0)) { | 
|  | 1125 | if (Op1C->getType()->getPrimitiveSizeInBits() < | 
|  | 1126 | MulC->getType()->getPrimitiveSizeInBits()) | 
|  | 1127 | Op1C = ConstantExpr::getZExt(Op1C, MulC->getType()); | 
|  | 1128 | if (Op1C->getType()->getPrimitiveSizeInBits() > | 
|  | 1129 | MulC->getType()->getPrimitiveSizeInBits()) | 
|  | 1130 | MulC = ConstantExpr::getZExt(MulC, Op1C->getType()); | 
|  | 1131 |  | 
|  | 1132 | // V == Base * (Mul0 * Op1), so return (Mul0 * Op1) | 
|  | 1133 | Multiple = ConstantExpr::getMul(MulC, Op1C); | 
|  | 1134 | return true; | 
|  | 1135 | } | 
| Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1136 |  | 
|  | 1137 | if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0)) | 
|  | 1138 | if (Mul0CI->getValue() == 1) { | 
|  | 1139 | // V == Base * Op1, so return Op1 | 
|  | 1140 | Multiple = Op1; | 
|  | 1141 | return true; | 
|  | 1142 | } | 
|  | 1143 | } | 
|  | 1144 |  | 
| Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 1145 | Value *Mul1 = NULL; | 
|  | 1146 | if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) { | 
|  | 1147 | if (Constant *Op0C = dyn_cast<Constant>(Op0)) | 
|  | 1148 | if (Constant *MulC = dyn_cast<Constant>(Mul1)) { | 
|  | 1149 | if (Op0C->getType()->getPrimitiveSizeInBits() < | 
|  | 1150 | MulC->getType()->getPrimitiveSizeInBits()) | 
|  | 1151 | Op0C = ConstantExpr::getZExt(Op0C, MulC->getType()); | 
|  | 1152 | if (Op0C->getType()->getPrimitiveSizeInBits() > | 
|  | 1153 | MulC->getType()->getPrimitiveSizeInBits()) | 
|  | 1154 | MulC = ConstantExpr::getZExt(MulC, Op0C->getType()); | 
|  | 1155 |  | 
|  | 1156 | // V == Base * (Mul1 * Op0), so return (Mul1 * Op0) | 
|  | 1157 | Multiple = ConstantExpr::getMul(MulC, Op0C); | 
|  | 1158 | return true; | 
|  | 1159 | } | 
| Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1160 |  | 
|  | 1161 | if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1)) | 
|  | 1162 | if (Mul1CI->getValue() == 1) { | 
|  | 1163 | // V == Base * Op0, so return Op0 | 
|  | 1164 | Multiple = Op0; | 
|  | 1165 | return true; | 
|  | 1166 | } | 
|  | 1167 | } | 
| Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1168 | } | 
|  | 1169 | } | 
|  | 1170 |  | 
|  | 1171 | // We could not determine if V is a multiple of Base. | 
|  | 1172 | return false; | 
|  | 1173 | } | 
|  | 1174 |  | 
| Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 1175 | /// CannotBeNegativeZero - Return true if we can prove that the specified FP | 
|  | 1176 | /// value is never equal to -0.0. | 
|  | 1177 | /// | 
|  | 1178 | /// NOTE: this function will need to be revisited when we support non-default | 
|  | 1179 | /// rounding modes! | 
|  | 1180 | /// | 
|  | 1181 | bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) { | 
|  | 1182 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) | 
|  | 1183 | return !CFP->getValueAPF().isNegZero(); | 
|  | 1184 |  | 
|  | 1185 | if (Depth == 6) | 
|  | 1186 | return 1;  // Limit search depth. | 
|  | 1187 |  | 
| Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 1188 | const Operator *I = dyn_cast<Operator>(V); | 
| Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 1189 | if (I == 0) return false; | 
|  | 1190 |  | 
|  | 1191 | // (add x, 0.0) is guaranteed to return +0.0, not -0.0. | 
| Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1192 | if (I->getOpcode() == Instruction::FAdd && | 
| Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 1193 | isa<ConstantFP>(I->getOperand(1)) && | 
|  | 1194 | cast<ConstantFP>(I->getOperand(1))->isNullValue()) | 
|  | 1195 | return true; | 
|  | 1196 |  | 
|  | 1197 | // sitofp and uitofp turn into +0.0 for zero. | 
|  | 1198 | if (isa<SIToFPInst>(I) || isa<UIToFPInst>(I)) | 
|  | 1199 | return true; | 
|  | 1200 |  | 
|  | 1201 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) | 
|  | 1202 | // sqrt(-0.0) = -0.0, no other negative results are possible. | 
|  | 1203 | if (II->getIntrinsicID() == Intrinsic::sqrt) | 
| Gabor Greif | 1abbde3 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 1204 | return CannotBeNegativeZero(II->getArgOperand(0), Depth+1); | 
| Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 1205 |  | 
|  | 1206 | if (const CallInst *CI = dyn_cast<CallInst>(I)) | 
|  | 1207 | if (const Function *F = CI->getCalledFunction()) { | 
|  | 1208 | if (F->isDeclaration()) { | 
| Daniel Dunbar | ca414c7 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1209 | // abs(x) != -0.0 | 
|  | 1210 | if (F->getName() == "abs") return true; | 
| Dale Johannesen | f6a987b | 2009-09-25 20:54:50 +0000 | [diff] [blame] | 1211 | // fabs[lf](x) != -0.0 | 
|  | 1212 | if (F->getName() == "fabs") return true; | 
|  | 1213 | if (F->getName() == "fabsf") return true; | 
|  | 1214 | if (F->getName() == "fabsl") return true; | 
|  | 1215 | if (F->getName() == "sqrt" || F->getName() == "sqrtf" || | 
|  | 1216 | F->getName() == "sqrtl") | 
| Gabor Greif | 1abbde3 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 1217 | return CannotBeNegativeZero(CI->getArgOperand(0), Depth+1); | 
| Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 1218 | } | 
|  | 1219 | } | 
|  | 1220 |  | 
|  | 1221 | return false; | 
|  | 1222 | } | 
|  | 1223 |  | 
| Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 1224 | /// isBytewiseValue - If the specified value can be set by repeating the same | 
|  | 1225 | /// byte in memory, return the i8 value that it is represented with.  This is | 
|  | 1226 | /// true for all i8 values obviously, but is also true for i32 0, i32 -1, | 
|  | 1227 | /// i16 0xF0F0, double 0.0 etc.  If the value can't be handled with a repeated | 
|  | 1228 | /// byte store (e.g. i16 0x1234), return null. | 
|  | 1229 | Value *llvm::isBytewiseValue(Value *V) { | 
|  | 1230 | // All byte-wide stores are splatable, even of arbitrary variables. | 
|  | 1231 | if (V->getType()->isIntegerTy(8)) return V; | 
| Chris Lattner | acf6b07 | 2011-02-19 19:35:49 +0000 | [diff] [blame] | 1232 |  | 
|  | 1233 | // Handle 'null' ConstantArrayZero etc. | 
|  | 1234 | if (Constant *C = dyn_cast<Constant>(V)) | 
|  | 1235 | if (C->isNullValue()) | 
|  | 1236 | return Constant::getNullValue(Type::getInt8Ty(V->getContext())); | 
| Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 1237 |  | 
|  | 1238 | // Constant float and double values can be handled as integer values if the | 
|  | 1239 | // corresponding integer value is "byteable".  An important case is 0.0. | 
|  | 1240 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { | 
|  | 1241 | if (CFP->getType()->isFloatTy()) | 
|  | 1242 | V = ConstantExpr::getBitCast(CFP, Type::getInt32Ty(V->getContext())); | 
|  | 1243 | if (CFP->getType()->isDoubleTy()) | 
|  | 1244 | V = ConstantExpr::getBitCast(CFP, Type::getInt64Ty(V->getContext())); | 
|  | 1245 | // Don't handle long double formats, which have strange constraints. | 
|  | 1246 | } | 
|  | 1247 |  | 
|  | 1248 | // We can handle constant integers that are power of two in size and a | 
|  | 1249 | // multiple of 8 bits. | 
|  | 1250 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { | 
|  | 1251 | unsigned Width = CI->getBitWidth(); | 
|  | 1252 | if (isPowerOf2_32(Width) && Width > 8) { | 
|  | 1253 | // We can handle this value if the recursive binary decomposition is the | 
|  | 1254 | // same at all levels. | 
|  | 1255 | APInt Val = CI->getValue(); | 
|  | 1256 | APInt Val2; | 
|  | 1257 | while (Val.getBitWidth() != 8) { | 
|  | 1258 | unsigned NextWidth = Val.getBitWidth()/2; | 
|  | 1259 | Val2  = Val.lshr(NextWidth); | 
|  | 1260 | Val2 = Val2.trunc(Val.getBitWidth()/2); | 
|  | 1261 | Val = Val.trunc(Val.getBitWidth()/2); | 
|  | 1262 |  | 
|  | 1263 | // If the top/bottom halves aren't the same, reject it. | 
|  | 1264 | if (Val != Val2) | 
|  | 1265 | return 0; | 
|  | 1266 | } | 
|  | 1267 | return ConstantInt::get(V->getContext(), Val); | 
|  | 1268 | } | 
|  | 1269 | } | 
|  | 1270 |  | 
|  | 1271 | // A ConstantArray is splatable if all its members are equal and also | 
|  | 1272 | // splatable. | 
|  | 1273 | if (ConstantArray *CA = dyn_cast<ConstantArray>(V)) { | 
|  | 1274 | if (CA->getNumOperands() == 0) | 
|  | 1275 | return 0; | 
|  | 1276 |  | 
|  | 1277 | Value *Val = isBytewiseValue(CA->getOperand(0)); | 
|  | 1278 | if (!Val) | 
|  | 1279 | return 0; | 
|  | 1280 |  | 
|  | 1281 | for (unsigned I = 1, E = CA->getNumOperands(); I != E; ++I) | 
|  | 1282 | if (CA->getOperand(I-1) != CA->getOperand(I)) | 
|  | 1283 | return 0; | 
|  | 1284 |  | 
|  | 1285 | return Val; | 
|  | 1286 | } | 
|  | 1287 |  | 
|  | 1288 | // Conceptually, we could handle things like: | 
|  | 1289 | //   %a = zext i8 %X to i16 | 
|  | 1290 | //   %b = shl i16 %a, 8 | 
|  | 1291 | //   %c = or i16 %a, %b | 
|  | 1292 | // but until there is an example that actually needs this, it doesn't seem | 
|  | 1293 | // worth worrying about. | 
|  | 1294 | return 0; | 
|  | 1295 | } | 
|  | 1296 |  | 
|  | 1297 |  | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1298 | // This is the recursive version of BuildSubAggregate. It takes a few different | 
|  | 1299 | // arguments. Idxs is the index within the nested struct From that we are | 
|  | 1300 | // looking at now (which is of type IndexedType). IdxSkip is the number of | 
|  | 1301 | // indices from Idxs that should be left out when inserting into the resulting | 
|  | 1302 | // struct. To is the result struct built so far, new insertvalue instructions | 
|  | 1303 | // build on that. | 
| Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 1304 | static Value *BuildSubAggregate(Value *From, Value* To, const Type *IndexedType, | 
|  | 1305 | SmallVector<unsigned, 10> &Idxs, | 
|  | 1306 | unsigned IdxSkip, | 
| Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 1307 | Instruction *InsertBefore) { | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1308 | const llvm::StructType *STy = llvm::dyn_cast<llvm::StructType>(IndexedType); | 
|  | 1309 | if (STy) { | 
| Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1310 | // Save the original To argument so we can modify it | 
|  | 1311 | Value *OrigTo = To; | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1312 | // General case, the type indexed by Idxs is a struct | 
|  | 1313 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { | 
|  | 1314 | // Process each struct element recursively | 
|  | 1315 | Idxs.push_back(i); | 
| Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1316 | Value *PrevTo = To; | 
| Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1317 | To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip, | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1318 | InsertBefore); | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1319 | Idxs.pop_back(); | 
| Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1320 | if (!To) { | 
|  | 1321 | // Couldn't find any inserted value for this index? Cleanup | 
|  | 1322 | while (PrevTo != OrigTo) { | 
|  | 1323 | InsertValueInst* Del = cast<InsertValueInst>(PrevTo); | 
|  | 1324 | PrevTo = Del->getAggregateOperand(); | 
|  | 1325 | Del->eraseFromParent(); | 
|  | 1326 | } | 
|  | 1327 | // Stop processing elements | 
|  | 1328 | break; | 
|  | 1329 | } | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1330 | } | 
| Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 1331 | // If we successfully found a value for each of our subaggregates | 
| Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1332 | if (To) | 
|  | 1333 | return To; | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1334 | } | 
| Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1335 | // Base case, the type indexed by SourceIdxs is not a struct, or not all of | 
|  | 1336 | // the struct's elements had a value that was inserted directly. In the latter | 
|  | 1337 | // case, perhaps we can't determine each of the subelements individually, but | 
|  | 1338 | // we might be able to find the complete struct somewhere. | 
|  | 1339 |  | 
|  | 1340 | // Find the value that is at that particular spot | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1341 | Value *V = FindInsertedValue(From, Idxs.begin(), Idxs.end()); | 
| Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1342 |  | 
|  | 1343 | if (!V) | 
|  | 1344 | return NULL; | 
|  | 1345 |  | 
|  | 1346 | // Insert the value in the new (sub) aggregrate | 
|  | 1347 | return llvm::InsertValueInst::Create(To, V, Idxs.begin() + IdxSkip, | 
|  | 1348 | Idxs.end(), "tmp", InsertBefore); | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1349 | } | 
|  | 1350 |  | 
|  | 1351 | // This helper takes a nested struct and extracts a part of it (which is again a | 
|  | 1352 | // struct) into a new value. For example, given the struct: | 
|  | 1353 | // { a, { b, { c, d }, e } } | 
|  | 1354 | // and the indices "1, 1" this returns | 
|  | 1355 | // { c, d }. | 
|  | 1356 | // | 
| Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1357 | // It does this by inserting an insertvalue for each element in the resulting | 
|  | 1358 | // struct, as opposed to just inserting a single struct. This will only work if | 
|  | 1359 | // each of the elements of the substruct are known (ie, inserted into From by an | 
|  | 1360 | // insertvalue instruction somewhere). | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1361 | // | 
| Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1362 | // All inserted insertvalue instructions are inserted before InsertBefore | 
| Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 1363 | static Value *BuildSubAggregate(Value *From, const unsigned *idx_begin, | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1364 | const unsigned *idx_end, | 
| Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 1365 | Instruction *InsertBefore) { | 
| Matthijs Kooijman | 69801d4 | 2008-06-16 13:28:31 +0000 | [diff] [blame] | 1366 | assert(InsertBefore && "Must have someplace to insert!"); | 
| Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1367 | const Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(), | 
|  | 1368 | idx_begin, | 
|  | 1369 | idx_end); | 
| Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1370 | Value *To = UndefValue::get(IndexedType); | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1371 | SmallVector<unsigned, 10> Idxs(idx_begin, idx_end); | 
|  | 1372 | unsigned IdxSkip = Idxs.size(); | 
|  | 1373 |  | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1374 | return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore); | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1375 | } | 
|  | 1376 |  | 
| Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1377 | /// FindInsertedValue - Given an aggregrate and an sequence of indices, see if | 
|  | 1378 | /// the scalar value indexed is already around as a register, for example if it | 
|  | 1379 | /// were inserted directly into the aggregrate. | 
| Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1380 | /// | 
|  | 1381 | /// If InsertBefore is not null, this function will duplicate (modified) | 
|  | 1382 | /// insertvalues when a part of a nested struct is extracted. | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1383 | Value *llvm::FindInsertedValue(Value *V, const unsigned *idx_begin, | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1384 | const unsigned *idx_end, Instruction *InsertBefore) { | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1385 | // Nothing to index? Just return V then (this is useful at the end of our | 
|  | 1386 | // recursion) | 
|  | 1387 | if (idx_begin == idx_end) | 
|  | 1388 | return V; | 
|  | 1389 | // We have indices, so V should have an indexable type | 
| Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1390 | assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1391 | && "Not looking at a struct or array?"); | 
|  | 1392 | assert(ExtractValueInst::getIndexedType(V->getType(), idx_begin, idx_end) | 
|  | 1393 | && "Invalid indices for type?"); | 
|  | 1394 | const CompositeType *PTy = cast<CompositeType>(V->getType()); | 
| Owen Anderson | f1f1743 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 1395 |  | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1396 | if (isa<UndefValue>(V)) | 
| Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1397 | return UndefValue::get(ExtractValueInst::getIndexedType(PTy, | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1398 | idx_begin, | 
|  | 1399 | idx_end)); | 
|  | 1400 | else if (isa<ConstantAggregateZero>(V)) | 
| Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1401 | return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy, | 
| Owen Anderson | f1f1743 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 1402 | idx_begin, | 
|  | 1403 | idx_end)); | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1404 | else if (Constant *C = dyn_cast<Constant>(V)) { | 
|  | 1405 | if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) | 
|  | 1406 | // Recursively process this constant | 
| Owen Anderson | f1f1743 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 1407 | return FindInsertedValue(C->getOperand(*idx_begin), idx_begin + 1, | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1408 | idx_end, InsertBefore); | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1409 | } else if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) { | 
|  | 1410 | // Loop the indices for the insertvalue instruction in parallel with the | 
|  | 1411 | // requested indices | 
|  | 1412 | const unsigned *req_idx = idx_begin; | 
| Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1413 | for (const unsigned *i = I->idx_begin(), *e = I->idx_end(); | 
|  | 1414 | i != e; ++i, ++req_idx) { | 
| Duncan Sands | db356ee | 2008-06-19 08:47:31 +0000 | [diff] [blame] | 1415 | if (req_idx == idx_end) { | 
| Matthijs Kooijman | 69801d4 | 2008-06-16 13:28:31 +0000 | [diff] [blame] | 1416 | if (InsertBefore) | 
| Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1417 | // The requested index identifies a part of a nested aggregate. Handle | 
|  | 1418 | // this specially. For example, | 
|  | 1419 | // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0 | 
|  | 1420 | // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1 | 
|  | 1421 | // %C = extractvalue {i32, { i32, i32 } } %B, 1 | 
|  | 1422 | // This can be changed into | 
|  | 1423 | // %A = insertvalue {i32, i32 } undef, i32 10, 0 | 
|  | 1424 | // %C = insertvalue {i32, i32 } %A, i32 11, 1 | 
|  | 1425 | // which allows the unused 0,0 element from the nested struct to be | 
|  | 1426 | // removed. | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1427 | return BuildSubAggregate(V, idx_begin, req_idx, InsertBefore); | 
| Matthijs Kooijman | 69801d4 | 2008-06-16 13:28:31 +0000 | [diff] [blame] | 1428 | else | 
|  | 1429 | // We can't handle this without inserting insertvalues | 
|  | 1430 | return 0; | 
| Duncan Sands | db356ee | 2008-06-19 08:47:31 +0000 | [diff] [blame] | 1431 | } | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1432 |  | 
|  | 1433 | // This insert value inserts something else than what we are looking for. | 
|  | 1434 | // See if the (aggregrate) value inserted into has the value we are | 
|  | 1435 | // looking for, then. | 
|  | 1436 | if (*req_idx != *i) | 
| Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1437 | return FindInsertedValue(I->getAggregateOperand(), idx_begin, idx_end, | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1438 | InsertBefore); | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1439 | } | 
|  | 1440 | // If we end up here, the indices of the insertvalue match with those | 
|  | 1441 | // requested (though possibly only partially). Now we recursively look at | 
|  | 1442 | // the inserted value, passing any remaining indices. | 
| Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1443 | return FindInsertedValue(I->getInsertedValueOperand(), req_idx, idx_end, | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1444 | InsertBefore); | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1445 | } else if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) { | 
|  | 1446 | // If we're extracting a value from an aggregrate that was extracted from | 
|  | 1447 | // something else, we can extract from that something else directly instead. | 
|  | 1448 | // However, we will need to chain I's indices with the requested indices. | 
|  | 1449 |  | 
|  | 1450 | // Calculate the number of indices required | 
|  | 1451 | unsigned size = I->getNumIndices() + (idx_end - idx_begin); | 
|  | 1452 | // Allocate some space to put the new indices in | 
| Matthijs Kooijman | 8369c67 | 2008-06-17 08:24:37 +0000 | [diff] [blame] | 1453 | SmallVector<unsigned, 5> Idxs; | 
|  | 1454 | Idxs.reserve(size); | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1455 | // Add indices from the extract value instruction | 
| Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1456 | for (const unsigned *i = I->idx_begin(), *e = I->idx_end(); | 
| Matthijs Kooijman | 8369c67 | 2008-06-17 08:24:37 +0000 | [diff] [blame] | 1457 | i != e; ++i) | 
|  | 1458 | Idxs.push_back(*i); | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1459 |  | 
|  | 1460 | // Add requested indices | 
| Matthijs Kooijman | 8369c67 | 2008-06-17 08:24:37 +0000 | [diff] [blame] | 1461 | for (const unsigned *i = idx_begin, *e = idx_end; i != e; ++i) | 
|  | 1462 | Idxs.push_back(*i); | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1463 |  | 
| Matthijs Kooijman | 8369c67 | 2008-06-17 08:24:37 +0000 | [diff] [blame] | 1464 | assert(Idxs.size() == size | 
| Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1465 | && "Number of indices added not correct?"); | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1466 |  | 
| Matthijs Kooijman | 8369c67 | 2008-06-17 08:24:37 +0000 | [diff] [blame] | 1467 | return FindInsertedValue(I->getAggregateOperand(), Idxs.begin(), Idxs.end(), | 
| Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1468 | InsertBefore); | 
| Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1469 | } | 
|  | 1470 | // Otherwise, we don't know (such as, extracting from a function return value | 
|  | 1471 | // or load instruction) | 
|  | 1472 | return 0; | 
|  | 1473 | } | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1474 |  | 
| Chris Lattner | e28618d | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 1475 | /// GetPointerBaseWithConstantOffset - Analyze the specified pointer to see if | 
|  | 1476 | /// it can be expressed as a base pointer plus a constant offset.  Return the | 
|  | 1477 | /// base and offset to the caller. | 
|  | 1478 | Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, | 
|  | 1479 | const TargetData &TD) { | 
|  | 1480 | Operator *PtrOp = dyn_cast<Operator>(Ptr); | 
|  | 1481 | if (PtrOp == 0) return Ptr; | 
|  | 1482 |  | 
|  | 1483 | // Just look through bitcasts. | 
|  | 1484 | if (PtrOp->getOpcode() == Instruction::BitCast) | 
|  | 1485 | return GetPointerBaseWithConstantOffset(PtrOp->getOperand(0), Offset, TD); | 
|  | 1486 |  | 
|  | 1487 | // If this is a GEP with constant indices, we can look through it. | 
|  | 1488 | GEPOperator *GEP = dyn_cast<GEPOperator>(PtrOp); | 
|  | 1489 | if (GEP == 0 || !GEP->hasAllConstantIndices()) return Ptr; | 
|  | 1490 |  | 
|  | 1491 | gep_type_iterator GTI = gep_type_begin(GEP); | 
|  | 1492 | for (User::op_iterator I = GEP->idx_begin(), E = GEP->idx_end(); I != E; | 
|  | 1493 | ++I, ++GTI) { | 
|  | 1494 | ConstantInt *OpC = cast<ConstantInt>(*I); | 
|  | 1495 | if (OpC->isZero()) continue; | 
|  | 1496 |  | 
|  | 1497 | // Handle a struct and array indices which add their offset to the pointer. | 
|  | 1498 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { | 
|  | 1499 | Offset += TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); | 
|  | 1500 | } else { | 
|  | 1501 | uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()); | 
|  | 1502 | Offset += OpC->getSExtValue()*Size; | 
|  | 1503 | } | 
|  | 1504 | } | 
|  | 1505 |  | 
|  | 1506 | // Re-sign extend from the pointer size if needed to get overflow edge cases | 
|  | 1507 | // right. | 
|  | 1508 | unsigned PtrSize = TD.getPointerSizeInBits(); | 
|  | 1509 | if (PtrSize < 64) | 
|  | 1510 | Offset = (Offset << (64-PtrSize)) >> (64-PtrSize); | 
|  | 1511 |  | 
|  | 1512 | return GetPointerBaseWithConstantOffset(GEP->getPointerOperand(), Offset, TD); | 
|  | 1513 | } | 
|  | 1514 |  | 
|  | 1515 |  | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1516 | /// GetConstantStringInfo - This function computes the length of a | 
|  | 1517 | /// null-terminated C string pointed to by V.  If successful, it returns true | 
|  | 1518 | /// and returns the string in Str.  If unsuccessful, it returns false. | 
| Dan Gohman | 0b4df04 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1519 | bool llvm::GetConstantStringInfo(const Value *V, std::string &Str, | 
|  | 1520 | uint64_t Offset, | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1521 | bool StopAtNul) { | 
|  | 1522 | // If V is NULL then return false; | 
|  | 1523 | if (V == NULL) return false; | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1524 |  | 
|  | 1525 | // Look through bitcast instructions. | 
| Dan Gohman | 0b4df04 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1526 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V)) | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1527 | return GetConstantStringInfo(BCI->getOperand(0), Str, Offset, StopAtNul); | 
|  | 1528 |  | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1529 | // If the value is not a GEP instruction nor a constant expression with a | 
|  | 1530 | // GEP instruction, then return false because ConstantArray can't occur | 
|  | 1531 | // any other way | 
| Dan Gohman | 0b4df04 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1532 | const User *GEP = 0; | 
|  | 1533 | if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) { | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1534 | GEP = GEPI; | 
| Dan Gohman | 0b4df04 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1535 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1536 | if (CE->getOpcode() == Instruction::BitCast) | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1537 | return GetConstantStringInfo(CE->getOperand(0), Str, Offset, StopAtNul); | 
|  | 1538 | if (CE->getOpcode() != Instruction::GetElementPtr) | 
|  | 1539 | return false; | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1540 | GEP = CE; | 
|  | 1541 | } | 
|  | 1542 |  | 
|  | 1543 | if (GEP) { | 
|  | 1544 | // Make sure the GEP has exactly three arguments. | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1545 | if (GEP->getNumOperands() != 3) | 
|  | 1546 | return false; | 
|  | 1547 |  | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1548 | // Make sure the index-ee is a pointer to array of i8. | 
|  | 1549 | const PointerType *PT = cast<PointerType>(GEP->getOperand(0)->getType()); | 
|  | 1550 | const ArrayType *AT = dyn_cast<ArrayType>(PT->getElementType()); | 
| Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1551 | if (AT == 0 || !AT->getElementType()->isIntegerTy(8)) | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1552 | return false; | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1553 |  | 
|  | 1554 | // Check to make sure that the first operand of the GEP is an integer and | 
|  | 1555 | // has value 0 so that we are sure we're indexing into the initializer. | 
| Dan Gohman | 0b4df04 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1556 | const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1)); | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1557 | if (FirstIdx == 0 || !FirstIdx->isZero()) | 
|  | 1558 | return false; | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1559 |  | 
|  | 1560 | // If the second index isn't a ConstantInt, then this is a variable index | 
|  | 1561 | // into the array.  If this occurs, we can't say anything meaningful about | 
|  | 1562 | // the string. | 
|  | 1563 | uint64_t StartIdx = 0; | 
| Dan Gohman | 0b4df04 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1564 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1565 | StartIdx = CI->getZExtValue(); | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1566 | else | 
|  | 1567 | return false; | 
|  | 1568 | return GetConstantStringInfo(GEP->getOperand(0), Str, StartIdx+Offset, | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1569 | StopAtNul); | 
|  | 1570 | } | 
|  | 1571 |  | 
|  | 1572 | // The GEP instruction, constant or instruction, must reference a global | 
|  | 1573 | // variable that is a constant and is initialized. The referenced constant | 
|  | 1574 | // initializer is the array that we'll use for optimization. | 
| Dan Gohman | 0b4df04 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1575 | const GlobalVariable* GV = dyn_cast<GlobalVariable>(V); | 
| Dan Gohman | 5d5bc6d | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 1576 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer()) | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1577 | return false; | 
| Dan Gohman | 0b4df04 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1578 | const Constant *GlobalInit = GV->getInitializer(); | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1579 |  | 
|  | 1580 | // Handle the ConstantAggregateZero case | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1581 | if (isa<ConstantAggregateZero>(GlobalInit)) { | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1582 | // This is a degenerate case. The initializer is constant zero so the | 
|  | 1583 | // length of the string must be zero. | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1584 | Str.clear(); | 
|  | 1585 | return true; | 
|  | 1586 | } | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1587 |  | 
|  | 1588 | // Must be a Constant Array | 
| Dan Gohman | 0b4df04 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1589 | const ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit); | 
| Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1590 | if (Array == 0 || !Array->getType()->getElementType()->isIntegerTy(8)) | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1591 | return false; | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1592 |  | 
|  | 1593 | // Get the number of elements in the array | 
|  | 1594 | uint64_t NumElts = Array->getType()->getNumElements(); | 
|  | 1595 |  | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1596 | if (Offset > NumElts) | 
|  | 1597 | return false; | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1598 |  | 
|  | 1599 | // Traverse the constant array from 'Offset' which is the place the GEP refers | 
|  | 1600 | // to in the array. | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1601 | Str.reserve(NumElts-Offset); | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1602 | for (unsigned i = Offset; i != NumElts; ++i) { | 
| Dan Gohman | 0b4df04 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1603 | const Constant *Elt = Array->getOperand(i); | 
|  | 1604 | const ConstantInt *CI = dyn_cast<ConstantInt>(Elt); | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1605 | if (!CI) // This array isn't suitable, non-int initializer. | 
|  | 1606 | return false; | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1607 | if (StopAtNul && CI->isZero()) | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1608 | return true; // we found end of string, success! | 
|  | 1609 | Str += (char)CI->getZExtValue(); | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1610 | } | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1611 |  | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1612 | // The array isn't null terminated, but maybe this is a memcpy, not a strcpy. | 
| Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1613 | return true; | 
| Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1614 | } | 
| Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 1615 |  | 
|  | 1616 | // These next two are very similar to the above, but also look through PHI | 
|  | 1617 | // nodes. | 
|  | 1618 | // TODO: See if we can integrate these two together. | 
|  | 1619 |  | 
|  | 1620 | /// GetStringLengthH - If we can compute the length of the string pointed to by | 
|  | 1621 | /// the specified pointer, return 'len+1'.  If we can't, return 0. | 
|  | 1622 | static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) { | 
|  | 1623 | // Look through noop bitcast instructions. | 
|  | 1624 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) | 
|  | 1625 | return GetStringLengthH(BCI->getOperand(0), PHIs); | 
|  | 1626 |  | 
|  | 1627 | // If this is a PHI node, there are two cases: either we have already seen it | 
|  | 1628 | // or we haven't. | 
|  | 1629 | if (PHINode *PN = dyn_cast<PHINode>(V)) { | 
|  | 1630 | if (!PHIs.insert(PN)) | 
|  | 1631 | return ~0ULL;  // already in the set. | 
|  | 1632 |  | 
|  | 1633 | // If it was new, see if all the input strings are the same length. | 
|  | 1634 | uint64_t LenSoFar = ~0ULL; | 
|  | 1635 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { | 
|  | 1636 | uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs); | 
|  | 1637 | if (Len == 0) return 0; // Unknown length -> unknown. | 
|  | 1638 |  | 
|  | 1639 | if (Len == ~0ULL) continue; | 
|  | 1640 |  | 
|  | 1641 | if (Len != LenSoFar && LenSoFar != ~0ULL) | 
|  | 1642 | return 0;    // Disagree -> unknown. | 
|  | 1643 | LenSoFar = Len; | 
|  | 1644 | } | 
|  | 1645 |  | 
|  | 1646 | // Success, all agree. | 
|  | 1647 | return LenSoFar; | 
|  | 1648 | } | 
|  | 1649 |  | 
|  | 1650 | // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y) | 
|  | 1651 | if (SelectInst *SI = dyn_cast<SelectInst>(V)) { | 
|  | 1652 | uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs); | 
|  | 1653 | if (Len1 == 0) return 0; | 
|  | 1654 | uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs); | 
|  | 1655 | if (Len2 == 0) return 0; | 
|  | 1656 | if (Len1 == ~0ULL) return Len2; | 
|  | 1657 | if (Len2 == ~0ULL) return Len1; | 
|  | 1658 | if (Len1 != Len2) return 0; | 
|  | 1659 | return Len1; | 
|  | 1660 | } | 
|  | 1661 |  | 
|  | 1662 | // If the value is not a GEP instruction nor a constant expression with a | 
|  | 1663 | // GEP instruction, then return unknown. | 
|  | 1664 | User *GEP = 0; | 
|  | 1665 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) { | 
|  | 1666 | GEP = GEPI; | 
|  | 1667 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { | 
|  | 1668 | if (CE->getOpcode() != Instruction::GetElementPtr) | 
|  | 1669 | return 0; | 
|  | 1670 | GEP = CE; | 
|  | 1671 | } else { | 
|  | 1672 | return 0; | 
|  | 1673 | } | 
|  | 1674 |  | 
|  | 1675 | // Make sure the GEP has exactly three arguments. | 
|  | 1676 | if (GEP->getNumOperands() != 3) | 
|  | 1677 | return 0; | 
|  | 1678 |  | 
|  | 1679 | // Check to make sure that the first operand of the GEP is an integer and | 
|  | 1680 | // has value 0 so that we are sure we're indexing into the initializer. | 
|  | 1681 | if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) { | 
|  | 1682 | if (!Idx->isZero()) | 
|  | 1683 | return 0; | 
|  | 1684 | } else | 
|  | 1685 | return 0; | 
|  | 1686 |  | 
|  | 1687 | // If the second index isn't a ConstantInt, then this is a variable index | 
|  | 1688 | // into the array.  If this occurs, we can't say anything meaningful about | 
|  | 1689 | // the string. | 
|  | 1690 | uint64_t StartIdx = 0; | 
|  | 1691 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) | 
|  | 1692 | StartIdx = CI->getZExtValue(); | 
|  | 1693 | else | 
|  | 1694 | return 0; | 
|  | 1695 |  | 
|  | 1696 | // The GEP instruction, constant or instruction, must reference a global | 
|  | 1697 | // variable that is a constant and is initialized. The referenced constant | 
|  | 1698 | // initializer is the array that we'll use for optimization. | 
|  | 1699 | GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); | 
|  | 1700 | if (!GV || !GV->isConstant() || !GV->hasInitializer() || | 
|  | 1701 | GV->mayBeOverridden()) | 
|  | 1702 | return 0; | 
|  | 1703 | Constant *GlobalInit = GV->getInitializer(); | 
|  | 1704 |  | 
|  | 1705 | // Handle the ConstantAggregateZero case, which is a degenerate case. The | 
|  | 1706 | // initializer is constant zero so the length of the string must be zero. | 
|  | 1707 | if (isa<ConstantAggregateZero>(GlobalInit)) | 
|  | 1708 | return 1;  // Len = 0 offset by 1. | 
|  | 1709 |  | 
|  | 1710 | // Must be a Constant Array | 
|  | 1711 | ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit); | 
|  | 1712 | if (!Array || !Array->getType()->getElementType()->isIntegerTy(8)) | 
|  | 1713 | return false; | 
|  | 1714 |  | 
|  | 1715 | // Get the number of elements in the array | 
|  | 1716 | uint64_t NumElts = Array->getType()->getNumElements(); | 
|  | 1717 |  | 
|  | 1718 | // Traverse the constant array from StartIdx (derived above) which is | 
|  | 1719 | // the place the GEP refers to in the array. | 
|  | 1720 | for (unsigned i = StartIdx; i != NumElts; ++i) { | 
|  | 1721 | Constant *Elt = Array->getOperand(i); | 
|  | 1722 | ConstantInt *CI = dyn_cast<ConstantInt>(Elt); | 
|  | 1723 | if (!CI) // This array isn't suitable, non-int initializer. | 
|  | 1724 | return 0; | 
|  | 1725 | if (CI->isZero()) | 
|  | 1726 | return i-StartIdx+1; // We found end of string, success! | 
|  | 1727 | } | 
|  | 1728 |  | 
|  | 1729 | return 0; // The array isn't null terminated, conservatively return 'unknown'. | 
|  | 1730 | } | 
|  | 1731 |  | 
|  | 1732 | /// GetStringLength - If we can compute the length of the string pointed to by | 
|  | 1733 | /// the specified pointer, return 'len+1'.  If we can't, return 0. | 
|  | 1734 | uint64_t llvm::GetStringLength(Value *V) { | 
|  | 1735 | if (!V->getType()->isPointerTy()) return 0; | 
|  | 1736 |  | 
|  | 1737 | SmallPtrSet<PHINode*, 32> PHIs; | 
|  | 1738 | uint64_t Len = GetStringLengthH(V, PHIs); | 
|  | 1739 | // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return | 
|  | 1740 | // an empty string as a length. | 
|  | 1741 | return Len == ~0ULL ? 1 : Len; | 
|  | 1742 | } | 
| Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 1743 |  | 
| Dan Gohman | 0f124e1 | 2011-01-24 18:53:32 +0000 | [diff] [blame] | 1744 | Value * | 
|  | 1745 | llvm::GetUnderlyingObject(Value *V, const TargetData *TD, unsigned MaxLookup) { | 
| Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 1746 | if (!V->getType()->isPointerTy()) | 
|  | 1747 | return V; | 
|  | 1748 | for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) { | 
|  | 1749 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { | 
|  | 1750 | V = GEP->getPointerOperand(); | 
|  | 1751 | } else if (Operator::getOpcode(V) == Instruction::BitCast) { | 
|  | 1752 | V = cast<Operator>(V)->getOperand(0); | 
|  | 1753 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { | 
|  | 1754 | if (GA->mayBeOverridden()) | 
|  | 1755 | return V; | 
|  | 1756 | V = GA->getAliasee(); | 
|  | 1757 | } else { | 
| Dan Gohman | 05b18f1 | 2010-12-15 20:49:55 +0000 | [diff] [blame] | 1758 | // See if InstructionSimplify knows any relevant tricks. | 
|  | 1759 | if (Instruction *I = dyn_cast<Instruction>(V)) | 
| Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 1760 | // TODO: Acquire a DominatorTree and use it. | 
| Dan Gohman | 0f124e1 | 2011-01-24 18:53:32 +0000 | [diff] [blame] | 1761 | if (Value *Simplified = SimplifyInstruction(I, TD, 0)) { | 
| Dan Gohman | 05b18f1 | 2010-12-15 20:49:55 +0000 | [diff] [blame] | 1762 | V = Simplified; | 
|  | 1763 | continue; | 
|  | 1764 | } | 
|  | 1765 |  | 
| Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 1766 | return V; | 
|  | 1767 | } | 
|  | 1768 | assert(V->getType()->isPointerTy() && "Unexpected operand type!"); | 
|  | 1769 | } | 
|  | 1770 | return V; | 
|  | 1771 | } |