Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1 | //===- ValueTracking.cpp - Walk computations to compute properties --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains routines that help analyze properties that chains of |
| 11 | // computations have. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Analysis/ValueTracking.h" |
Dan Gohman | 2437127 | 2010-12-15 20:10:26 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/InstructionSimplify.h" |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/Instructions.h" |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 19 | #include "llvm/GlobalVariable.h" |
Dan Gohman | 307a7c4 | 2009-09-15 16:14:44 +0000 | [diff] [blame] | 20 | #include "llvm/GlobalAlias.h" |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 21 | #include "llvm/IntrinsicInst.h" |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 22 | #include "llvm/LLVMContext.h" |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 23 | #include "llvm/Operator.h" |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 25 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 26 | #include "llvm/Support/MathExtras.h" |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 27 | #include "llvm/Support/PatternMatch.h" |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 32a9e7a | 2008-06-04 04:46:14 +0000 | [diff] [blame] | 29 | #include <cstring> |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Duncan Sands | d70d1a5 | 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 | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 173234a | 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 | cf5128e | 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 | 173234a | 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 | 846a2f2 | 2009-08-27 17:51:25 +0000 | [diff] [blame] | 62 | const TargetData *TD, unsigned Depth) { |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 63 | assert(V && "No Value?"); |
Dan Gohman | 9004c8a | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 64 | assert(Depth <= MaxDepth && "Limit Search Depth"); |
Chris Lattner | 79abedb | 2009-01-20 18:22:57 +0000 | [diff] [blame] | 65 | unsigned BitWidth = Mask.getBitWidth(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 66 | assert((V->getType()->isIntOrIntVectorTy() || V->getType()->isPointerTy()) |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 67 | && "Not integer or pointer type!"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 68 | assert((!TD || |
| 69 | TD->getTypeSizeInBits(V->getType()->getScalarType()) == BitWidth) && |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 70 | (!V->getType()->isIntOrIntVectorTy() || |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 71 | V->getType()->getScalarSizeInBits() == BitWidth) && |
Chris Lattner | 173234a | 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 | 6de29f8 | 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 | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 85 | KnownOne.clearAllBits(); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 86 | KnownZero = Mask; |
| 87 | return; |
| 88 | } |
Dan Gohman | 6de29f8 | 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 | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 92 | KnownZero.setAllBits(); KnownOne.setAllBits(); |
Dan Gohman | 6de29f8 | 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 | 173234a | 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 | 0040725 | 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 | 173234a | 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 | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 119 | KnownZero.clearAllBits(); |
| 120 | KnownOne.clearAllBits(); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 121 | return; |
| 122 | } |
Dan Gohman | 307a7c4 | 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 | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 127 | KnownZero.clearAllBits(); KnownOne.clearAllBits(); |
Dan Gohman | 307a7c4 | 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 | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 134 | |
Jay Foad | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 135 | KnownZero.clearAllBits(); KnownOne.clearAllBits(); // Start out not knowing anything. |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 136 | |
Dan Gohman | 9004c8a | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 137 | if (Depth == MaxDepth || Mask == 0) |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 138 | return; // Limit search depth. |
| 139 | |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 140 | Operator *I = dyn_cast<Operator>(V); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 141 | if (!I) return; |
| 142 | |
| 143 | APInt KnownZero2(KnownZero), KnownOne2(KnownOne); |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 144 | switch (I->getOpcode()) { |
Chris Lattner | 173234a | 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 | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 201 | KnownOne.clearAllBits(); |
Chris Lattner | 173234a | 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 | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 224 | KnownOne2.clearAllBits(); |
| 225 | KnownZero2.clearAllBits(); |
Chris Lattner | 173234a | 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 | b9a4ddb | 2009-09-08 00:13:52 +0000 | [diff] [blame] | 261 | const Type *SrcTy = I->getOperand(0)->getType(); |
| 262 | |
| 263 | unsigned SrcBitWidth; |
Chris Lattner | 173234a | 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 | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 266 | if (SrcTy->isPointerTy()) |
Chris Lattner | b9a4ddb | 2009-09-08 00:13:52 +0000 | [diff] [blame] | 267 | SrcBitWidth = TD->getTypeSizeInBits(SrcTy); |
| 268 | else |
| 269 | SrcBitWidth = SrcTy->getScalarSizeInBits(); |
| 270 | |
Jay Foad | 40f8f62 | 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 | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 274 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, TD, |
| 275 | Depth+1); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 276 | KnownZero = KnownZero.zextOrTrunc(BitWidth); |
| 277 | KnownOne = KnownOne.zextOrTrunc(BitWidth); |
Chris Lattner | 173234a | 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 | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 285 | if ((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
Chris Lattner | 0dabb0b | 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 | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 288 | !I->getType()->isVectorTy()) { |
Chris Lattner | 173234a | 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 | b9a4ddb | 2009-09-08 00:13:52 +0000 | [diff] [blame] | 297 | unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits(); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 298 | |
Jay Foad | 40f8f62 | 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 | 173234a | 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 | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 305 | KnownZero = KnownZero.zext(BitWidth); |
| 306 | KnownOne = KnownOne.zext(BitWidth); |
Chris Lattner | 173234a | 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 | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 340 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 173234a | 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 | 43b40a4 | 2011-01-04 18:19:15 +0000 | [diff] [blame] | 352 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1); |
Chris Lattner | 173234a | 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 | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 358 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 173234a | 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 | ae3d802 | 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 | 3925043 | 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 | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 403 | Depth+1); |
Dan Gohman | 3925043 | 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 | 173234a | 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 | 3925043 | 2009-05-24 18:02:35 +0000 | [diff] [blame] | 411 | unsigned RHSKnownZeroOut = KnownZero2.countTrailingOnes(); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 412 | |
Dan Gohman | 3925043 | 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 | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 416 | if (I->getOpcode() == Instruction::Add) { |
Dan Gohman | 3925043 | 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 | } |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | case Instruction::SRem: |
| 435 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Duncan Sands | cfd5418 | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 436 | APInt RA = Rem->getValue().abs(); |
| 437 | if (RA.isPowerOf2()) { |
| 438 | APInt LowBits = RA - 1; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 439 | APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); |
| 440 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, TD, |
| 441 | Depth+1); |
| 442 | |
Duncan Sands | cfd5418 | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 443 | // The low bits of the first operand are unchanged by the srem. |
| 444 | KnownZero = KnownZero2 & LowBits; |
| 445 | KnownOne = KnownOne2 & LowBits; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 446 | |
Duncan Sands | cfd5418 | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 447 | // If the first operand is non-negative or has all low bits zero, then |
| 448 | // the upper bits are all zero. |
| 449 | if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits)) |
| 450 | KnownZero |= ~LowBits; |
| 451 | |
| 452 | // If the first operand is negative and not all low bits are zero, then |
| 453 | // the upper bits are all one. |
| 454 | if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0)) |
| 455 | KnownOne |= ~LowBits; |
| 456 | |
| 457 | KnownZero &= Mask; |
| 458 | KnownOne &= Mask; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 459 | |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 460 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | break; |
| 464 | case Instruction::URem: { |
| 465 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 466 | APInt RA = Rem->getValue(); |
| 467 | if (RA.isPowerOf2()) { |
| 468 | APInt LowBits = (RA - 1); |
| 469 | APInt Mask2 = LowBits & Mask; |
| 470 | KnownZero |= ~LowBits & Mask; |
| 471 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, TD, |
| 472 | Depth+1); |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 473 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 474 | break; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | // Since the result is less than or equal to either operand, any leading |
| 479 | // zero bits in either operand must also exist in the result. |
| 480 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| 481 | ComputeMaskedBits(I->getOperand(0), AllOnes, KnownZero, KnownOne, |
| 482 | TD, Depth+1); |
| 483 | ComputeMaskedBits(I->getOperand(1), AllOnes, KnownZero2, KnownOne2, |
| 484 | TD, Depth+1); |
| 485 | |
Chris Lattner | 79abedb | 2009-01-20 18:22:57 +0000 | [diff] [blame] | 486 | unsigned Leaders = std::max(KnownZero.countLeadingOnes(), |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 487 | KnownZero2.countLeadingOnes()); |
Jay Foad | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 488 | KnownOne.clearAllBits(); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 489 | KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask; |
| 490 | break; |
| 491 | } |
| 492 | |
Victor Hernandez | a276c60 | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 493 | case Instruction::Alloca: { |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 494 | AllocaInst *AI = cast<AllocaInst>(V); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 495 | unsigned Align = AI->getAlignment(); |
Victor Hernandez | a276c60 | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 496 | if (Align == 0 && TD) |
| 497 | Align = TD->getABITypeAlignment(AI->getType()->getElementType()); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 498 | |
| 499 | if (Align > 0) |
| 500 | KnownZero = Mask & APInt::getLowBitsSet(BitWidth, |
| 501 | CountTrailingZeros_32(Align)); |
| 502 | break; |
| 503 | } |
| 504 | case Instruction::GetElementPtr: { |
| 505 | // Analyze all of the subscripts of this getelementptr instruction |
| 506 | // to determine if we can prove known low zero bits. |
| 507 | APInt LocalMask = APInt::getAllOnesValue(BitWidth); |
| 508 | APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0); |
| 509 | ComputeMaskedBits(I->getOperand(0), LocalMask, |
| 510 | LocalKnownZero, LocalKnownOne, TD, Depth+1); |
| 511 | unsigned TrailZ = LocalKnownZero.countTrailingOnes(); |
| 512 | |
| 513 | gep_type_iterator GTI = gep_type_begin(I); |
| 514 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) { |
| 515 | Value *Index = I->getOperand(i); |
| 516 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 517 | // Handle struct member offset arithmetic. |
| 518 | if (!TD) return; |
| 519 | const StructLayout *SL = TD->getStructLayout(STy); |
| 520 | unsigned Idx = cast<ConstantInt>(Index)->getZExtValue(); |
| 521 | uint64_t Offset = SL->getElementOffset(Idx); |
| 522 | TrailZ = std::min(TrailZ, |
| 523 | CountTrailingZeros_64(Offset)); |
| 524 | } else { |
| 525 | // Handle array index arithmetic. |
| 526 | const Type *IndexedTy = GTI.getIndexedType(); |
| 527 | if (!IndexedTy->isSized()) return; |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 528 | unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 529 | uint64_t TypeSize = TD ? TD->getTypeAllocSize(IndexedTy) : 1; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 530 | LocalMask = APInt::getAllOnesValue(GEPOpiBits); |
| 531 | LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0); |
| 532 | ComputeMaskedBits(Index, LocalMask, |
| 533 | LocalKnownZero, LocalKnownOne, TD, Depth+1); |
| 534 | TrailZ = std::min(TrailZ, |
Chris Lattner | 79abedb | 2009-01-20 18:22:57 +0000 | [diff] [blame] | 535 | unsigned(CountTrailingZeros_64(TypeSize) + |
| 536 | LocalKnownZero.countTrailingOnes())); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) & Mask; |
| 541 | break; |
| 542 | } |
| 543 | case Instruction::PHI: { |
| 544 | PHINode *P = cast<PHINode>(I); |
| 545 | // Handle the case of a simple two-predecessor recurrence PHI. |
| 546 | // There's a lot more that could theoretically be done here, but |
| 547 | // this is sufficient to catch some interesting cases. |
| 548 | if (P->getNumIncomingValues() == 2) { |
| 549 | for (unsigned i = 0; i != 2; ++i) { |
| 550 | Value *L = P->getIncomingValue(i); |
| 551 | Value *R = P->getIncomingValue(!i); |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 552 | Operator *LU = dyn_cast<Operator>(L); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 553 | if (!LU) |
| 554 | continue; |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 555 | unsigned Opcode = LU->getOpcode(); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 556 | // Check for operations that have the property that if |
| 557 | // both their operands have low zero bits, the result |
| 558 | // will have low zero bits. |
| 559 | if (Opcode == Instruction::Add || |
| 560 | Opcode == Instruction::Sub || |
| 561 | Opcode == Instruction::And || |
| 562 | Opcode == Instruction::Or || |
| 563 | Opcode == Instruction::Mul) { |
| 564 | Value *LL = LU->getOperand(0); |
| 565 | Value *LR = LU->getOperand(1); |
| 566 | // Find a recurrence. |
| 567 | if (LL == I) |
| 568 | L = LR; |
| 569 | else if (LR == I) |
| 570 | L = LL; |
| 571 | else |
| 572 | break; |
| 573 | // Ok, we have a PHI of the form L op= R. Check for low |
| 574 | // zero bits. |
| 575 | APInt Mask2 = APInt::getAllOnesValue(BitWidth); |
| 576 | ComputeMaskedBits(R, Mask2, KnownZero2, KnownOne2, TD, Depth+1); |
| 577 | Mask2 = APInt::getLowBitsSet(BitWidth, |
| 578 | KnownZero2.countTrailingOnes()); |
David Greene | c714f13 | 2008-10-27 23:24:03 +0000 | [diff] [blame] | 579 | |
| 580 | // We need to take the minimum number of known bits |
| 581 | APInt KnownZero3(KnownZero), KnownOne3(KnownOne); |
| 582 | ComputeMaskedBits(L, Mask2, KnownZero3, KnownOne3, TD, Depth+1); |
| 583 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 584 | KnownZero = Mask & |
| 585 | APInt::getLowBitsSet(BitWidth, |
David Greene | c714f13 | 2008-10-27 23:24:03 +0000 | [diff] [blame] | 586 | std::min(KnownZero2.countTrailingOnes(), |
| 587 | KnownZero3.countTrailingOnes())); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 588 | break; |
| 589 | } |
| 590 | } |
| 591 | } |
Dan Gohman | 9004c8a | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 592 | |
Nick Lewycky | 3b739d2 | 2011-02-10 23:54:10 +0000 | [diff] [blame^] | 593 | // Unreachable blocks may have zero-operand PHI nodes. |
| 594 | if (P->getNumIncomingValues() == 0) |
| 595 | return; |
| 596 | |
Dan Gohman | 9004c8a | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 597 | // Otherwise take the unions of the known bit sets of the operands, |
| 598 | // taking conservative care to avoid excessive recursion. |
| 599 | if (Depth < MaxDepth - 1 && !KnownZero && !KnownOne) { |
| 600 | KnownZero = APInt::getAllOnesValue(BitWidth); |
| 601 | KnownOne = APInt::getAllOnesValue(BitWidth); |
| 602 | for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) { |
| 603 | // Skip direct self references. |
| 604 | if (P->getIncomingValue(i) == P) continue; |
| 605 | |
| 606 | KnownZero2 = APInt(BitWidth, 0); |
| 607 | KnownOne2 = APInt(BitWidth, 0); |
| 608 | // Recurse, but cap the recursion to one level, because we don't |
| 609 | // want to waste time spinning around in loops. |
| 610 | ComputeMaskedBits(P->getIncomingValue(i), KnownZero | KnownOne, |
| 611 | KnownZero2, KnownOne2, TD, MaxDepth-1); |
| 612 | KnownZero &= KnownZero2; |
| 613 | KnownOne &= KnownOne2; |
| 614 | // If all bits have been ruled out, there's no need to check |
| 615 | // more operands. |
| 616 | if (!KnownZero && !KnownOne) |
| 617 | break; |
| 618 | } |
| 619 | } |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 620 | break; |
| 621 | } |
| 622 | case Instruction::Call: |
| 623 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 624 | switch (II->getIntrinsicID()) { |
| 625 | default: break; |
| 626 | case Intrinsic::ctpop: |
| 627 | case Intrinsic::ctlz: |
| 628 | case Intrinsic::cttz: { |
| 629 | unsigned LowBits = Log2_32(BitWidth)+1; |
| 630 | KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits); |
| 631 | break; |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 639 | /// ComputeSignBit - Determine whether the sign bit is known to be zero or |
| 640 | /// one. Convenience wrapper around ComputeMaskedBits. |
| 641 | void llvm::ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, |
| 642 | const TargetData *TD, unsigned Depth) { |
| 643 | unsigned BitWidth = getBitWidth(V->getType(), TD); |
| 644 | if (!BitWidth) { |
| 645 | KnownZero = false; |
| 646 | KnownOne = false; |
| 647 | return; |
| 648 | } |
| 649 | APInt ZeroBits(BitWidth, 0); |
| 650 | APInt OneBits(BitWidth, 0); |
| 651 | ComputeMaskedBits(V, APInt::getSignBit(BitWidth), ZeroBits, OneBits, TD, |
| 652 | Depth); |
| 653 | KnownOne = OneBits[BitWidth - 1]; |
| 654 | KnownZero = ZeroBits[BitWidth - 1]; |
| 655 | } |
| 656 | |
| 657 | /// isPowerOfTwo - Return true if the given value is known to have exactly one |
| 658 | /// bit set when defined. For vectors return true if every element is known to |
| 659 | /// be a power of two when defined. Supports values with integer or pointer |
| 660 | /// types and vectors of integers. |
| 661 | bool llvm::isPowerOfTwo(Value *V, const TargetData *TD, unsigned Depth) { |
| 662 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
Duncan Sands | 464a4f3 | 2011-01-26 08:44:16 +0000 | [diff] [blame] | 663 | return CI->getValue().isPowerOf2(); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 664 | // TODO: Handle vector constants. |
| 665 | |
| 666 | // 1 << X is clearly a power of two if the one is not shifted off the end. If |
| 667 | // it is shifted off the end then the result is undefined. |
| 668 | if (match(V, m_Shl(m_One(), m_Value()))) |
| 669 | return true; |
| 670 | |
| 671 | // (signbit) >>l X is clearly a power of two if the one is not shifted off the |
| 672 | // bottom. If it is shifted off the bottom then the result is undefined. |
Duncan Sands | 93c7802 | 2011-02-01 08:50:33 +0000 | [diff] [blame] | 673 | if (match(V, m_LShr(m_SignBit(), m_Value()))) |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 674 | return true; |
| 675 | |
| 676 | // The remaining tests are all recursive, so bail out if we hit the limit. |
| 677 | if (Depth++ == MaxDepth) |
| 678 | return false; |
| 679 | |
| 680 | if (ZExtInst *ZI = dyn_cast<ZExtInst>(V)) |
| 681 | return isPowerOfTwo(ZI->getOperand(0), TD, Depth); |
| 682 | |
| 683 | if (SelectInst *SI = dyn_cast<SelectInst>(V)) |
| 684 | return isPowerOfTwo(SI->getTrueValue(), TD, Depth) && |
| 685 | isPowerOfTwo(SI->getFalseValue(), TD, Depth); |
| 686 | |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | /// isKnownNonZero - Return true if the given value is known to be non-zero |
| 691 | /// when defined. For vectors return true if every element is known to be |
| 692 | /// non-zero when defined. Supports values with integer or pointer type and |
| 693 | /// vectors of integers. |
| 694 | bool llvm::isKnownNonZero(Value *V, const TargetData *TD, unsigned Depth) { |
| 695 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 696 | if (C->isNullValue()) |
| 697 | return false; |
| 698 | if (isa<ConstantInt>(C)) |
| 699 | // Must be non-zero due to null test above. |
| 700 | return true; |
| 701 | // TODO: Handle vectors |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | // The remaining tests are all recursive, so bail out if we hit the limit. |
| 706 | if (Depth++ == MaxDepth) |
| 707 | return false; |
| 708 | |
| 709 | unsigned BitWidth = getBitWidth(V->getType(), TD); |
| 710 | |
| 711 | // X | Y != 0 if X != 0 or Y != 0. |
| 712 | Value *X = 0, *Y = 0; |
| 713 | if (match(V, m_Or(m_Value(X), m_Value(Y)))) |
| 714 | return isKnownNonZero(X, TD, Depth) || isKnownNonZero(Y, TD, Depth); |
| 715 | |
| 716 | // ext X != 0 if X != 0. |
| 717 | if (isa<SExtInst>(V) || isa<ZExtInst>(V)) |
| 718 | return isKnownNonZero(cast<Instruction>(V)->getOperand(0), TD, Depth); |
| 719 | |
Duncan Sands | 9136782 | 2011-01-29 13:27:00 +0000 | [diff] [blame] | 720 | // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 721 | // if the lowest bit is shifted off the end. |
| 722 | if (BitWidth && match(V, m_Shl(m_Value(X), m_Value(Y)))) { |
| 723 | APInt KnownZero(BitWidth, 0); |
| 724 | APInt KnownOne(BitWidth, 0); |
Duncan Sands | 9136782 | 2011-01-29 13:27:00 +0000 | [diff] [blame] | 725 | ComputeMaskedBits(X, APInt(BitWidth, 1), KnownZero, KnownOne, TD, Depth); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 726 | if (KnownOne[0]) |
| 727 | return true; |
| 728 | } |
Duncan Sands | 9136782 | 2011-01-29 13:27:00 +0000 | [diff] [blame] | 729 | // shr X, Y != 0 if X is negative. Note that the value of the shift is not |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 730 | // defined if the sign bit is shifted off the end. |
| 731 | else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) { |
| 732 | bool XKnownNonNegative, XKnownNegative; |
| 733 | ComputeSignBit(X, XKnownNonNegative, XKnownNegative, TD, Depth); |
| 734 | if (XKnownNegative) |
| 735 | return true; |
| 736 | } |
| 737 | // X + Y. |
| 738 | else if (match(V, m_Add(m_Value(X), m_Value(Y)))) { |
| 739 | bool XKnownNonNegative, XKnownNegative; |
| 740 | bool YKnownNonNegative, YKnownNegative; |
| 741 | ComputeSignBit(X, XKnownNonNegative, XKnownNegative, TD, Depth); |
| 742 | ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, TD, Depth); |
| 743 | |
| 744 | // If X and Y are both non-negative (as signed values) then their sum is not |
Duncan Sands | 227fba1 | 2011-01-25 15:14:15 +0000 | [diff] [blame] | 745 | // zero unless both X and Y are zero. |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 746 | if (XKnownNonNegative && YKnownNonNegative) |
Duncan Sands | 227fba1 | 2011-01-25 15:14:15 +0000 | [diff] [blame] | 747 | if (isKnownNonZero(X, TD, Depth) || isKnownNonZero(Y, TD, Depth)) |
| 748 | return true; |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 749 | |
| 750 | // If X and Y are both negative (as signed values) then their sum is not |
| 751 | // zero unless both X and Y equal INT_MIN. |
| 752 | if (BitWidth && XKnownNegative && YKnownNegative) { |
| 753 | APInt KnownZero(BitWidth, 0); |
| 754 | APInt KnownOne(BitWidth, 0); |
| 755 | APInt Mask = APInt::getSignedMaxValue(BitWidth); |
| 756 | // The sign bit of X is set. If some other bit is set then X is not equal |
| 757 | // to INT_MIN. |
| 758 | ComputeMaskedBits(X, Mask, KnownZero, KnownOne, TD, Depth); |
| 759 | if ((KnownOne & Mask) != 0) |
| 760 | return true; |
| 761 | // The sign bit of Y is set. If some other bit is set then Y is not equal |
| 762 | // to INT_MIN. |
| 763 | ComputeMaskedBits(Y, Mask, KnownZero, KnownOne, TD, Depth); |
| 764 | if ((KnownOne & Mask) != 0) |
| 765 | return true; |
| 766 | } |
| 767 | |
| 768 | // The sum of a non-negative number and a power of two is not zero. |
| 769 | if (XKnownNonNegative && isPowerOfTwo(Y, TD, Depth)) |
| 770 | return true; |
| 771 | if (YKnownNonNegative && isPowerOfTwo(X, TD, Depth)) |
| 772 | return true; |
| 773 | } |
| 774 | // (C ? X : Y) != 0 if X != 0 and Y != 0. |
| 775 | else if (SelectInst *SI = dyn_cast<SelectInst>(V)) { |
| 776 | if (isKnownNonZero(SI->getTrueValue(), TD, Depth) && |
| 777 | isKnownNonZero(SI->getFalseValue(), TD, Depth)) |
| 778 | return true; |
| 779 | } |
| 780 | |
| 781 | if (!BitWidth) return false; |
| 782 | APInt KnownZero(BitWidth, 0); |
| 783 | APInt KnownOne(BitWidth, 0); |
| 784 | ComputeMaskedBits(V, APInt::getAllOnesValue(BitWidth), KnownZero, KnownOne, |
| 785 | TD, Depth); |
| 786 | return KnownOne != 0; |
| 787 | } |
| 788 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 789 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 790 | /// this predicate to simplify operations downstream. Mask is known to be zero |
| 791 | /// for bits that V cannot have. |
Chris Lattner | cf5128e | 2009-09-08 00:06:16 +0000 | [diff] [blame] | 792 | /// |
| 793 | /// This function is defined on values with integer type, values with pointer |
| 794 | /// type (but only if TD is non-null), and vectors of integers. In the case |
| 795 | /// where V is a vector, the mask, known zero, and known one values are the |
| 796 | /// same width as the vector element, and the bit is set only if it is true |
| 797 | /// for all of the elements in the vector. |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 798 | bool llvm::MaskedValueIsZero(Value *V, const APInt &Mask, |
Dan Gohman | 846a2f2 | 2009-08-27 17:51:25 +0000 | [diff] [blame] | 799 | const TargetData *TD, unsigned Depth) { |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 800 | APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0); |
| 801 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth); |
| 802 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 803 | return (KnownZero & Mask) == Mask; |
| 804 | } |
| 805 | |
| 806 | |
| 807 | |
| 808 | /// ComputeNumSignBits - Return the number of times the sign bit of the |
| 809 | /// register is replicated into the other bits. We know that at least 1 bit |
| 810 | /// is always equal to the sign bit (itself), but other cases can give us |
| 811 | /// information. For example, immediately after an "ashr X, 2", we know that |
| 812 | /// the top 3 bits are all equal to each other, so we return 3. |
| 813 | /// |
| 814 | /// 'Op' must have a scalar integer type. |
| 815 | /// |
Dan Gohman | 846a2f2 | 2009-08-27 17:51:25 +0000 | [diff] [blame] | 816 | unsigned llvm::ComputeNumSignBits(Value *V, const TargetData *TD, |
| 817 | unsigned Depth) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 818 | assert((TD || V->getType()->isIntOrIntVectorTy()) && |
Dan Gohman | bd5ce52 | 2009-06-22 22:02:32 +0000 | [diff] [blame] | 819 | "ComputeNumSignBits requires a TargetData object to operate " |
| 820 | "on non-integer values!"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 821 | const Type *Ty = V->getType(); |
Dan Gohman | bd5ce52 | 2009-06-22 22:02:32 +0000 | [diff] [blame] | 822 | unsigned TyBits = TD ? TD->getTypeSizeInBits(V->getType()->getScalarType()) : |
| 823 | Ty->getScalarSizeInBits(); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 824 | unsigned Tmp, Tmp2; |
| 825 | unsigned FirstAnswer = 1; |
| 826 | |
Chris Lattner | d82e511 | 2008-06-02 18:39:07 +0000 | [diff] [blame] | 827 | // Note that ConstantInt is handled by the general ComputeMaskedBits case |
| 828 | // below. |
| 829 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 830 | if (Depth == 6) |
| 831 | return 1; // Limit search depth. |
| 832 | |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 833 | Operator *U = dyn_cast<Operator>(V); |
| 834 | switch (Operator::getOpcode(V)) { |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 835 | default: break; |
| 836 | case Instruction::SExt: |
Mon P Wang | 69a0080 | 2009-12-02 04:59:58 +0000 | [diff] [blame] | 837 | Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits(); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 838 | return ComputeNumSignBits(U->getOperand(0), TD, Depth+1) + Tmp; |
| 839 | |
| 840 | case Instruction::AShr: |
| 841 | Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1); |
| 842 | // ashr X, C -> adds C sign bits. |
| 843 | if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 844 | Tmp += C->getZExtValue(); |
| 845 | if (Tmp > TyBits) Tmp = TyBits; |
| 846 | } |
Nate Begeman | 9a3dc55 | 2010-12-17 23:12:19 +0000 | [diff] [blame] | 847 | // vector ashr X, <C, C, C, C> -> adds C sign bits |
| 848 | if (ConstantVector *C = dyn_cast<ConstantVector>(U->getOperand(1))) { |
| 849 | if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) { |
| 850 | Tmp += CI->getZExtValue(); |
| 851 | if (Tmp > TyBits) Tmp = TyBits; |
| 852 | } |
| 853 | } |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 854 | return Tmp; |
| 855 | case Instruction::Shl: |
| 856 | if (ConstantInt *C = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 857 | // shl destroys sign bits. |
| 858 | Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1); |
| 859 | if (C->getZExtValue() >= TyBits || // Bad shift. |
| 860 | C->getZExtValue() >= Tmp) break; // Shifted all sign bits out. |
| 861 | return Tmp - C->getZExtValue(); |
| 862 | } |
| 863 | break; |
| 864 | case Instruction::And: |
| 865 | case Instruction::Or: |
| 866 | case Instruction::Xor: // NOT is handled here. |
| 867 | // Logical binary ops preserve the number of sign bits at the worst. |
| 868 | Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1); |
| 869 | if (Tmp != 1) { |
| 870 | Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1); |
| 871 | FirstAnswer = std::min(Tmp, Tmp2); |
| 872 | // We computed what we know about the sign bits as our first |
| 873 | // answer. Now proceed to the generic code that uses |
| 874 | // ComputeMaskedBits, and pick whichever answer is better. |
| 875 | } |
| 876 | break; |
| 877 | |
| 878 | case Instruction::Select: |
| 879 | Tmp = ComputeNumSignBits(U->getOperand(1), TD, Depth+1); |
| 880 | if (Tmp == 1) return 1; // Early out. |
| 881 | Tmp2 = ComputeNumSignBits(U->getOperand(2), TD, Depth+1); |
| 882 | return std::min(Tmp, Tmp2); |
| 883 | |
| 884 | case Instruction::Add: |
| 885 | // Add can have at most one carry bit. Thus we know that the output |
| 886 | // is, at worst, one more bit than the inputs. |
| 887 | Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1); |
| 888 | if (Tmp == 1) return 1; // Early out. |
| 889 | |
| 890 | // Special case decrementing a value (ADD X, -1): |
Dan Gohman | 0001e56 | 2009-02-24 02:00:40 +0000 | [diff] [blame] | 891 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(U->getOperand(1))) |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 892 | if (CRHS->isAllOnesValue()) { |
| 893 | APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0); |
| 894 | APInt Mask = APInt::getAllOnesValue(TyBits); |
| 895 | ComputeMaskedBits(U->getOperand(0), Mask, KnownZero, KnownOne, TD, |
| 896 | Depth+1); |
| 897 | |
| 898 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 899 | // sign bits set. |
| 900 | if ((KnownZero | APInt(TyBits, 1)) == Mask) |
| 901 | return TyBits; |
| 902 | |
| 903 | // If we are subtracting one from a positive number, there is no carry |
| 904 | // out of the result. |
| 905 | if (KnownZero.isNegative()) |
| 906 | return Tmp; |
| 907 | } |
| 908 | |
| 909 | Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1); |
| 910 | if (Tmp2 == 1) return 1; |
Chris Lattner | 8d10f9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 911 | return std::min(Tmp, Tmp2)-1; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 912 | |
| 913 | case Instruction::Sub: |
| 914 | Tmp2 = ComputeNumSignBits(U->getOperand(1), TD, Depth+1); |
| 915 | if (Tmp2 == 1) return 1; |
| 916 | |
| 917 | // Handle NEG. |
| 918 | if (ConstantInt *CLHS = dyn_cast<ConstantInt>(U->getOperand(0))) |
| 919 | if (CLHS->isNullValue()) { |
| 920 | APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0); |
| 921 | APInt Mask = APInt::getAllOnesValue(TyBits); |
| 922 | ComputeMaskedBits(U->getOperand(1), Mask, KnownZero, KnownOne, |
| 923 | TD, Depth+1); |
| 924 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 925 | // sign bits set. |
| 926 | if ((KnownZero | APInt(TyBits, 1)) == Mask) |
| 927 | return TyBits; |
| 928 | |
| 929 | // If the input is known to be positive (the sign bit is known clear), |
| 930 | // the output of the NEG has the same number of sign bits as the input. |
| 931 | if (KnownZero.isNegative()) |
| 932 | return Tmp2; |
| 933 | |
| 934 | // Otherwise, we treat this like a SUB. |
| 935 | } |
| 936 | |
| 937 | // Sub can have at most one carry bit. Thus we know that the output |
| 938 | // is, at worst, one more bit than the inputs. |
| 939 | Tmp = ComputeNumSignBits(U->getOperand(0), TD, Depth+1); |
| 940 | if (Tmp == 1) return 1; // Early out. |
Chris Lattner | 8d10f9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 941 | return std::min(Tmp, Tmp2)-1; |
| 942 | |
| 943 | case Instruction::PHI: { |
| 944 | PHINode *PN = cast<PHINode>(U); |
| 945 | // Don't analyze large in-degree PHIs. |
| 946 | if (PN->getNumIncomingValues() > 4) break; |
| 947 | |
| 948 | // Take the minimum of all incoming values. This can't infinitely loop |
| 949 | // because of our depth threshold. |
| 950 | Tmp = ComputeNumSignBits(PN->getIncomingValue(0), TD, Depth+1); |
| 951 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 952 | if (Tmp == 1) return Tmp; |
| 953 | Tmp = std::min(Tmp, |
Evan Cheng | 0af20d8 | 2010-03-13 02:20:29 +0000 | [diff] [blame] | 954 | ComputeNumSignBits(PN->getIncomingValue(i), TD, Depth+1)); |
Chris Lattner | 8d10f9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 955 | } |
| 956 | return Tmp; |
| 957 | } |
| 958 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 959 | case Instruction::Trunc: |
| 960 | // FIXME: it's tricky to do anything useful for this, but it is an important |
| 961 | // case for targets like X86. |
| 962 | break; |
| 963 | } |
| 964 | |
| 965 | // Finally, if we can prove that the top bits of the result are 0's or 1's, |
| 966 | // use this information. |
| 967 | APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0); |
| 968 | APInt Mask = APInt::getAllOnesValue(TyBits); |
| 969 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth); |
| 970 | |
| 971 | if (KnownZero.isNegative()) { // sign bit is 0 |
| 972 | Mask = KnownZero; |
| 973 | } else if (KnownOne.isNegative()) { // sign bit is 1; |
| 974 | Mask = KnownOne; |
| 975 | } else { |
| 976 | // Nothing known. |
| 977 | return FirstAnswer; |
| 978 | } |
| 979 | |
| 980 | // Okay, we know that the sign bit in Mask is set. Use CLZ to determine |
| 981 | // the number of identical bits in the top of the input value. |
| 982 | Mask = ~Mask; |
| 983 | Mask <<= Mask.getBitWidth()-TyBits; |
| 984 | // Return # leading zeros. We use 'min' here in case Val was zero before |
| 985 | // shifting. We don't want to return '64' as for an i32 "0". |
| 986 | return std::max(FirstAnswer, std::min(TyBits, Mask.countLeadingZeros())); |
| 987 | } |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 988 | |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 989 | /// ComputeMultiple - This function computes the integer multiple of Base that |
| 990 | /// equals V. If successful, it returns true and returns the multiple in |
Dan Gohman | 3dbb9e6 | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 991 | /// Multiple. If unsuccessful, it returns false. It looks |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 992 | /// through SExt instructions only if LookThroughSExt is true. |
| 993 | bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple, |
Dan Gohman | 3dbb9e6 | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 994 | bool LookThroughSExt, unsigned Depth) { |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 995 | const unsigned MaxDepth = 6; |
| 996 | |
Dan Gohman | 3dbb9e6 | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 997 | assert(V && "No Value?"); |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 998 | assert(Depth <= MaxDepth && "Limit Search Depth"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 999 | assert(V->getType()->isIntegerTy() && "Not integer or pointer type!"); |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1000 | |
| 1001 | const Type *T = V->getType(); |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1002 | |
Dan Gohman | 3dbb9e6 | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 1003 | ConstantInt *CI = dyn_cast<ConstantInt>(V); |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1004 | |
| 1005 | if (Base == 0) |
| 1006 | return false; |
| 1007 | |
| 1008 | if (Base == 1) { |
| 1009 | Multiple = V; |
| 1010 | return true; |
| 1011 | } |
| 1012 | |
| 1013 | ConstantExpr *CO = dyn_cast<ConstantExpr>(V); |
| 1014 | Constant *BaseVal = ConstantInt::get(T, Base); |
| 1015 | if (CO && CO == BaseVal) { |
| 1016 | // Multiple is 1. |
| 1017 | Multiple = ConstantInt::get(T, 1); |
| 1018 | return true; |
| 1019 | } |
| 1020 | |
| 1021 | if (CI && CI->getZExtValue() % Base == 0) { |
| 1022 | Multiple = ConstantInt::get(T, CI->getZExtValue() / Base); |
| 1023 | return true; |
| 1024 | } |
| 1025 | |
| 1026 | if (Depth == MaxDepth) return false; // Limit search depth. |
| 1027 | |
| 1028 | Operator *I = dyn_cast<Operator>(V); |
| 1029 | if (!I) return false; |
| 1030 | |
| 1031 | switch (I->getOpcode()) { |
| 1032 | default: break; |
Chris Lattner | 11fe726 | 2009-11-26 01:50:12 +0000 | [diff] [blame] | 1033 | case Instruction::SExt: |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1034 | if (!LookThroughSExt) return false; |
| 1035 | // otherwise fall through to ZExt |
Chris Lattner | 11fe726 | 2009-11-26 01:50:12 +0000 | [diff] [blame] | 1036 | case Instruction::ZExt: |
Dan Gohman | 3dbb9e6 | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 1037 | return ComputeMultiple(I->getOperand(0), Base, Multiple, |
| 1038 | LookThroughSExt, Depth+1); |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1039 | case Instruction::Shl: |
| 1040 | case Instruction::Mul: { |
| 1041 | Value *Op0 = I->getOperand(0); |
| 1042 | Value *Op1 = I->getOperand(1); |
| 1043 | |
| 1044 | if (I->getOpcode() == Instruction::Shl) { |
| 1045 | ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1); |
| 1046 | if (!Op1CI) return false; |
| 1047 | // Turn Op0 << Op1 into Op0 * 2^Op1 |
| 1048 | APInt Op1Int = Op1CI->getValue(); |
| 1049 | uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1); |
Jay Foad | a99793c | 2010-11-30 09:02:01 +0000 | [diff] [blame] | 1050 | APInt API(Op1Int.getBitWidth(), 0); |
Jay Foad | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 1051 | API.setBit(BitToSet); |
Jay Foad | a99793c | 2010-11-30 09:02:01 +0000 | [diff] [blame] | 1052 | Op1 = ConstantInt::get(V->getContext(), API); |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | Value *Mul0 = NULL; |
Chris Lattner | e971131 | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 1056 | if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) { |
| 1057 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) |
| 1058 | if (Constant *MulC = dyn_cast<Constant>(Mul0)) { |
| 1059 | if (Op1C->getType()->getPrimitiveSizeInBits() < |
| 1060 | MulC->getType()->getPrimitiveSizeInBits()) |
| 1061 | Op1C = ConstantExpr::getZExt(Op1C, MulC->getType()); |
| 1062 | if (Op1C->getType()->getPrimitiveSizeInBits() > |
| 1063 | MulC->getType()->getPrimitiveSizeInBits()) |
| 1064 | MulC = ConstantExpr::getZExt(MulC, Op1C->getType()); |
| 1065 | |
| 1066 | // V == Base * (Mul0 * Op1), so return (Mul0 * Op1) |
| 1067 | Multiple = ConstantExpr::getMul(MulC, Op1C); |
| 1068 | return true; |
| 1069 | } |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1070 | |
| 1071 | if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0)) |
| 1072 | if (Mul0CI->getValue() == 1) { |
| 1073 | // V == Base * Op1, so return Op1 |
| 1074 | Multiple = Op1; |
| 1075 | return true; |
| 1076 | } |
| 1077 | } |
| 1078 | |
Chris Lattner | e971131 | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 1079 | Value *Mul1 = NULL; |
| 1080 | if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) { |
| 1081 | if (Constant *Op0C = dyn_cast<Constant>(Op0)) |
| 1082 | if (Constant *MulC = dyn_cast<Constant>(Mul1)) { |
| 1083 | if (Op0C->getType()->getPrimitiveSizeInBits() < |
| 1084 | MulC->getType()->getPrimitiveSizeInBits()) |
| 1085 | Op0C = ConstantExpr::getZExt(Op0C, MulC->getType()); |
| 1086 | if (Op0C->getType()->getPrimitiveSizeInBits() > |
| 1087 | MulC->getType()->getPrimitiveSizeInBits()) |
| 1088 | MulC = ConstantExpr::getZExt(MulC, Op0C->getType()); |
| 1089 | |
| 1090 | // V == Base * (Mul1 * Op0), so return (Mul1 * Op0) |
| 1091 | Multiple = ConstantExpr::getMul(MulC, Op0C); |
| 1092 | return true; |
| 1093 | } |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1094 | |
| 1095 | if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1)) |
| 1096 | if (Mul1CI->getValue() == 1) { |
| 1097 | // V == Base * Op0, so return Op0 |
| 1098 | Multiple = Op0; |
| 1099 | return true; |
| 1100 | } |
| 1101 | } |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | // We could not determine if V is a multiple of Base. |
| 1106 | return false; |
| 1107 | } |
| 1108 | |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 1109 | /// CannotBeNegativeZero - Return true if we can prove that the specified FP |
| 1110 | /// value is never equal to -0.0. |
| 1111 | /// |
| 1112 | /// NOTE: this function will need to be revisited when we support non-default |
| 1113 | /// rounding modes! |
| 1114 | /// |
| 1115 | bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) { |
| 1116 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) |
| 1117 | return !CFP->getValueAPF().isNegZero(); |
| 1118 | |
| 1119 | if (Depth == 6) |
| 1120 | return 1; // Limit search depth. |
| 1121 | |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 1122 | const Operator *I = dyn_cast<Operator>(V); |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 1123 | if (I == 0) return false; |
| 1124 | |
| 1125 | // (add x, 0.0) is guaranteed to return +0.0, not -0.0. |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1126 | if (I->getOpcode() == Instruction::FAdd && |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 1127 | isa<ConstantFP>(I->getOperand(1)) && |
| 1128 | cast<ConstantFP>(I->getOperand(1))->isNullValue()) |
| 1129 | return true; |
| 1130 | |
| 1131 | // sitofp and uitofp turn into +0.0 for zero. |
| 1132 | if (isa<SIToFPInst>(I) || isa<UIToFPInst>(I)) |
| 1133 | return true; |
| 1134 | |
| 1135 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 1136 | // sqrt(-0.0) = -0.0, no other negative results are possible. |
| 1137 | if (II->getIntrinsicID() == Intrinsic::sqrt) |
Gabor Greif | 71339c9 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 1138 | return CannotBeNegativeZero(II->getArgOperand(0), Depth+1); |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 1139 | |
| 1140 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 1141 | if (const Function *F = CI->getCalledFunction()) { |
| 1142 | if (F->isDeclaration()) { |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1143 | // abs(x) != -0.0 |
| 1144 | if (F->getName() == "abs") return true; |
Dale Johannesen | 9d06175 | 2009-09-25 20:54:50 +0000 | [diff] [blame] | 1145 | // fabs[lf](x) != -0.0 |
| 1146 | if (F->getName() == "fabs") return true; |
| 1147 | if (F->getName() == "fabsf") return true; |
| 1148 | if (F->getName() == "fabsl") return true; |
| 1149 | if (F->getName() == "sqrt" || F->getName() == "sqrtf" || |
| 1150 | F->getName() == "sqrtl") |
Gabor Greif | 71339c9 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 1151 | return CannotBeNegativeZero(CI->getArgOperand(0), Depth+1); |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | return false; |
| 1156 | } |
| 1157 | |
Chris Lattner | bb89710 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 1158 | /// isBytewiseValue - If the specified value can be set by repeating the same |
| 1159 | /// byte in memory, return the i8 value that it is represented with. This is |
| 1160 | /// true for all i8 values obviously, but is also true for i32 0, i32 -1, |
| 1161 | /// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated |
| 1162 | /// byte store (e.g. i16 0x1234), return null. |
| 1163 | Value *llvm::isBytewiseValue(Value *V) { |
| 1164 | // All byte-wide stores are splatable, even of arbitrary variables. |
| 1165 | if (V->getType()->isIntegerTy(8)) return V; |
| 1166 | |
| 1167 | // Constant float and double values can be handled as integer values if the |
| 1168 | // corresponding integer value is "byteable". An important case is 0.0. |
| 1169 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 1170 | if (CFP->getType()->isFloatTy()) |
| 1171 | V = ConstantExpr::getBitCast(CFP, Type::getInt32Ty(V->getContext())); |
| 1172 | if (CFP->getType()->isDoubleTy()) |
| 1173 | V = ConstantExpr::getBitCast(CFP, Type::getInt64Ty(V->getContext())); |
| 1174 | // Don't handle long double formats, which have strange constraints. |
| 1175 | } |
| 1176 | |
| 1177 | // We can handle constant integers that are power of two in size and a |
| 1178 | // multiple of 8 bits. |
| 1179 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 1180 | unsigned Width = CI->getBitWidth(); |
| 1181 | if (isPowerOf2_32(Width) && Width > 8) { |
| 1182 | // We can handle this value if the recursive binary decomposition is the |
| 1183 | // same at all levels. |
| 1184 | APInt Val = CI->getValue(); |
| 1185 | APInt Val2; |
| 1186 | while (Val.getBitWidth() != 8) { |
| 1187 | unsigned NextWidth = Val.getBitWidth()/2; |
| 1188 | Val2 = Val.lshr(NextWidth); |
| 1189 | Val2 = Val2.trunc(Val.getBitWidth()/2); |
| 1190 | Val = Val.trunc(Val.getBitWidth()/2); |
| 1191 | |
| 1192 | // If the top/bottom halves aren't the same, reject it. |
| 1193 | if (Val != Val2) |
| 1194 | return 0; |
| 1195 | } |
| 1196 | return ConstantInt::get(V->getContext(), Val); |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | // A ConstantArray is splatable if all its members are equal and also |
| 1201 | // splatable. |
| 1202 | if (ConstantArray *CA = dyn_cast<ConstantArray>(V)) { |
| 1203 | if (CA->getNumOperands() == 0) |
| 1204 | return 0; |
| 1205 | |
| 1206 | Value *Val = isBytewiseValue(CA->getOperand(0)); |
| 1207 | if (!Val) |
| 1208 | return 0; |
| 1209 | |
| 1210 | for (unsigned I = 1, E = CA->getNumOperands(); I != E; ++I) |
| 1211 | if (CA->getOperand(I-1) != CA->getOperand(I)) |
| 1212 | return 0; |
| 1213 | |
| 1214 | return Val; |
| 1215 | } |
| 1216 | |
| 1217 | // Conceptually, we could handle things like: |
| 1218 | // %a = zext i8 %X to i16 |
| 1219 | // %b = shl i16 %a, 8 |
| 1220 | // %c = or i16 %a, %b |
| 1221 | // but until there is an example that actually needs this, it doesn't seem |
| 1222 | // worth worrying about. |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
| 1226 | |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1227 | // This is the recursive version of BuildSubAggregate. It takes a few different |
| 1228 | // arguments. Idxs is the index within the nested struct From that we are |
| 1229 | // looking at now (which is of type IndexedType). IdxSkip is the number of |
| 1230 | // indices from Idxs that should be left out when inserting into the resulting |
| 1231 | // struct. To is the result struct built so far, new insertvalue instructions |
| 1232 | // build on that. |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 1233 | static Value *BuildSubAggregate(Value *From, Value* To, const Type *IndexedType, |
| 1234 | SmallVector<unsigned, 10> &Idxs, |
| 1235 | unsigned IdxSkip, |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 1236 | Instruction *InsertBefore) { |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1237 | const llvm::StructType *STy = llvm::dyn_cast<llvm::StructType>(IndexedType); |
| 1238 | if (STy) { |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1239 | // Save the original To argument so we can modify it |
| 1240 | Value *OrigTo = To; |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1241 | // General case, the type indexed by Idxs is a struct |
| 1242 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 1243 | // Process each struct element recursively |
| 1244 | Idxs.push_back(i); |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1245 | Value *PrevTo = To; |
Matthijs Kooijman | 710eb23 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1246 | To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip, |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1247 | InsertBefore); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1248 | Idxs.pop_back(); |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1249 | if (!To) { |
| 1250 | // Couldn't find any inserted value for this index? Cleanup |
| 1251 | while (PrevTo != OrigTo) { |
| 1252 | InsertValueInst* Del = cast<InsertValueInst>(PrevTo); |
| 1253 | PrevTo = Del->getAggregateOperand(); |
| 1254 | Del->eraseFromParent(); |
| 1255 | } |
| 1256 | // Stop processing elements |
| 1257 | break; |
| 1258 | } |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1259 | } |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1260 | // If we succesfully found a value for each of our subaggregates |
| 1261 | if (To) |
| 1262 | return To; |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1263 | } |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1264 | // Base case, the type indexed by SourceIdxs is not a struct, or not all of |
| 1265 | // the struct's elements had a value that was inserted directly. In the latter |
| 1266 | // case, perhaps we can't determine each of the subelements individually, but |
| 1267 | // we might be able to find the complete struct somewhere. |
| 1268 | |
| 1269 | // Find the value that is at that particular spot |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1270 | Value *V = FindInsertedValue(From, Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1271 | |
| 1272 | if (!V) |
| 1273 | return NULL; |
| 1274 | |
| 1275 | // Insert the value in the new (sub) aggregrate |
| 1276 | return llvm::InsertValueInst::Create(To, V, Idxs.begin() + IdxSkip, |
| 1277 | Idxs.end(), "tmp", InsertBefore); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | // This helper takes a nested struct and extracts a part of it (which is again a |
| 1281 | // struct) into a new value. For example, given the struct: |
| 1282 | // { a, { b, { c, d }, e } } |
| 1283 | // and the indices "1, 1" this returns |
| 1284 | // { c, d }. |
| 1285 | // |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1286 | // It does this by inserting an insertvalue for each element in the resulting |
| 1287 | // struct, as opposed to just inserting a single struct. This will only work if |
| 1288 | // each of the elements of the substruct are known (ie, inserted into From by an |
| 1289 | // insertvalue instruction somewhere). |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1290 | // |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1291 | // All inserted insertvalue instructions are inserted before InsertBefore |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 1292 | static Value *BuildSubAggregate(Value *From, const unsigned *idx_begin, |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1293 | const unsigned *idx_end, |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 1294 | Instruction *InsertBefore) { |
Matthijs Kooijman | 9772891 | 2008-06-16 13:28:31 +0000 | [diff] [blame] | 1295 | assert(InsertBefore && "Must have someplace to insert!"); |
Matthijs Kooijman | 710eb23 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1296 | const Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(), |
| 1297 | idx_begin, |
| 1298 | idx_end); |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1299 | Value *To = UndefValue::get(IndexedType); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1300 | SmallVector<unsigned, 10> Idxs(idx_begin, idx_end); |
| 1301 | unsigned IdxSkip = Idxs.size(); |
| 1302 | |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1303 | return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Matthijs Kooijman | 710eb23 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1306 | /// FindInsertedValue - Given an aggregrate and an sequence of indices, see if |
| 1307 | /// the scalar value indexed is already around as a register, for example if it |
| 1308 | /// were inserted directly into the aggregrate. |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1309 | /// |
| 1310 | /// If InsertBefore is not null, this function will duplicate (modified) |
| 1311 | /// insertvalues when a part of a nested struct is extracted. |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1312 | Value *llvm::FindInsertedValue(Value *V, const unsigned *idx_begin, |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1313 | const unsigned *idx_end, Instruction *InsertBefore) { |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1314 | // Nothing to index? Just return V then (this is useful at the end of our |
| 1315 | // recursion) |
| 1316 | if (idx_begin == idx_end) |
| 1317 | return V; |
| 1318 | // We have indices, so V should have an indexable type |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1319 | assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1320 | && "Not looking at a struct or array?"); |
| 1321 | assert(ExtractValueInst::getIndexedType(V->getType(), idx_begin, idx_end) |
| 1322 | && "Invalid indices for type?"); |
| 1323 | const CompositeType *PTy = cast<CompositeType>(V->getType()); |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 1324 | |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1325 | if (isa<UndefValue>(V)) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1326 | return UndefValue::get(ExtractValueInst::getIndexedType(PTy, |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1327 | idx_begin, |
| 1328 | idx_end)); |
| 1329 | else if (isa<ConstantAggregateZero>(V)) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1330 | return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy, |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 1331 | idx_begin, |
| 1332 | idx_end)); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1333 | else if (Constant *C = dyn_cast<Constant>(V)) { |
| 1334 | if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) |
| 1335 | // Recursively process this constant |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 1336 | return FindInsertedValue(C->getOperand(*idx_begin), idx_begin + 1, |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1337 | idx_end, InsertBefore); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1338 | } else if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) { |
| 1339 | // Loop the indices for the insertvalue instruction in parallel with the |
| 1340 | // requested indices |
| 1341 | const unsigned *req_idx = idx_begin; |
Matthijs Kooijman | 710eb23 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1342 | for (const unsigned *i = I->idx_begin(), *e = I->idx_end(); |
| 1343 | i != e; ++i, ++req_idx) { |
Duncan Sands | 9954c76 | 2008-06-19 08:47:31 +0000 | [diff] [blame] | 1344 | if (req_idx == idx_end) { |
Matthijs Kooijman | 9772891 | 2008-06-16 13:28:31 +0000 | [diff] [blame] | 1345 | if (InsertBefore) |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 1346 | // The requested index identifies a part of a nested aggregate. Handle |
| 1347 | // this specially. For example, |
| 1348 | // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0 |
| 1349 | // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1 |
| 1350 | // %C = extractvalue {i32, { i32, i32 } } %B, 1 |
| 1351 | // This can be changed into |
| 1352 | // %A = insertvalue {i32, i32 } undef, i32 10, 0 |
| 1353 | // %C = insertvalue {i32, i32 } %A, i32 11, 1 |
| 1354 | // which allows the unused 0,0 element from the nested struct to be |
| 1355 | // removed. |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1356 | return BuildSubAggregate(V, idx_begin, req_idx, InsertBefore); |
Matthijs Kooijman | 9772891 | 2008-06-16 13:28:31 +0000 | [diff] [blame] | 1357 | else |
| 1358 | // We can't handle this without inserting insertvalues |
| 1359 | return 0; |
Duncan Sands | 9954c76 | 2008-06-19 08:47:31 +0000 | [diff] [blame] | 1360 | } |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1361 | |
| 1362 | // This insert value inserts something else than what we are looking for. |
| 1363 | // See if the (aggregrate) value inserted into has the value we are |
| 1364 | // looking for, then. |
| 1365 | if (*req_idx != *i) |
Matthijs Kooijman | 710eb23 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1366 | return FindInsertedValue(I->getAggregateOperand(), idx_begin, idx_end, |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1367 | InsertBefore); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1368 | } |
| 1369 | // If we end up here, the indices of the insertvalue match with those |
| 1370 | // requested (though possibly only partially). Now we recursively look at |
| 1371 | // the inserted value, passing any remaining indices. |
Matthijs Kooijman | 710eb23 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1372 | return FindInsertedValue(I->getInsertedValueOperand(), req_idx, idx_end, |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1373 | InsertBefore); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1374 | } else if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) { |
| 1375 | // If we're extracting a value from an aggregrate that was extracted from |
| 1376 | // something else, we can extract from that something else directly instead. |
| 1377 | // However, we will need to chain I's indices with the requested indices. |
| 1378 | |
| 1379 | // Calculate the number of indices required |
| 1380 | unsigned size = I->getNumIndices() + (idx_end - idx_begin); |
| 1381 | // Allocate some space to put the new indices in |
Matthijs Kooijman | 3faf9df | 2008-06-17 08:24:37 +0000 | [diff] [blame] | 1382 | SmallVector<unsigned, 5> Idxs; |
| 1383 | Idxs.reserve(size); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1384 | // Add indices from the extract value instruction |
Matthijs Kooijman | 710eb23 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1385 | for (const unsigned *i = I->idx_begin(), *e = I->idx_end(); |
Matthijs Kooijman | 3faf9df | 2008-06-17 08:24:37 +0000 | [diff] [blame] | 1386 | i != e; ++i) |
| 1387 | Idxs.push_back(*i); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1388 | |
| 1389 | // Add requested indices |
Matthijs Kooijman | 3faf9df | 2008-06-17 08:24:37 +0000 | [diff] [blame] | 1390 | for (const unsigned *i = idx_begin, *e = idx_end; i != e; ++i) |
| 1391 | Idxs.push_back(*i); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1392 | |
Matthijs Kooijman | 3faf9df | 2008-06-17 08:24:37 +0000 | [diff] [blame] | 1393 | assert(Idxs.size() == size |
Matthijs Kooijman | 710eb23 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 1394 | && "Number of indices added not correct?"); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1395 | |
Matthijs Kooijman | 3faf9df | 2008-06-17 08:24:37 +0000 | [diff] [blame] | 1396 | return FindInsertedValue(I->getAggregateOperand(), Idxs.begin(), Idxs.end(), |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 1397 | InsertBefore); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 1398 | } |
| 1399 | // Otherwise, we don't know (such as, extracting from a function return value |
| 1400 | // or load instruction) |
| 1401 | return 0; |
| 1402 | } |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1403 | |
Chris Lattner | ed58a6f | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 1404 | /// GetPointerBaseWithConstantOffset - Analyze the specified pointer to see if |
| 1405 | /// it can be expressed as a base pointer plus a constant offset. Return the |
| 1406 | /// base and offset to the caller. |
| 1407 | Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, |
| 1408 | const TargetData &TD) { |
| 1409 | Operator *PtrOp = dyn_cast<Operator>(Ptr); |
| 1410 | if (PtrOp == 0) return Ptr; |
| 1411 | |
| 1412 | // Just look through bitcasts. |
| 1413 | if (PtrOp->getOpcode() == Instruction::BitCast) |
| 1414 | return GetPointerBaseWithConstantOffset(PtrOp->getOperand(0), Offset, TD); |
| 1415 | |
| 1416 | // If this is a GEP with constant indices, we can look through it. |
| 1417 | GEPOperator *GEP = dyn_cast<GEPOperator>(PtrOp); |
| 1418 | if (GEP == 0 || !GEP->hasAllConstantIndices()) return Ptr; |
| 1419 | |
| 1420 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 1421 | for (User::op_iterator I = GEP->idx_begin(), E = GEP->idx_end(); I != E; |
| 1422 | ++I, ++GTI) { |
| 1423 | ConstantInt *OpC = cast<ConstantInt>(*I); |
| 1424 | if (OpC->isZero()) continue; |
| 1425 | |
| 1426 | // Handle a struct and array indices which add their offset to the pointer. |
| 1427 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 1428 | Offset += TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
| 1429 | } else { |
| 1430 | uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()); |
| 1431 | Offset += OpC->getSExtValue()*Size; |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | // Re-sign extend from the pointer size if needed to get overflow edge cases |
| 1436 | // right. |
| 1437 | unsigned PtrSize = TD.getPointerSizeInBits(); |
| 1438 | if (PtrSize < 64) |
| 1439 | Offset = (Offset << (64-PtrSize)) >> (64-PtrSize); |
| 1440 | |
| 1441 | return GetPointerBaseWithConstantOffset(GEP->getPointerOperand(), Offset, TD); |
| 1442 | } |
| 1443 | |
| 1444 | |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1445 | /// GetConstantStringInfo - This function computes the length of a |
| 1446 | /// null-terminated C string pointed to by V. If successful, it returns true |
| 1447 | /// and returns the string in Str. If unsuccessful, it returns false. |
Dan Gohman | 0a60fa3 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1448 | bool llvm::GetConstantStringInfo(const Value *V, std::string &Str, |
| 1449 | uint64_t Offset, |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1450 | bool StopAtNul) { |
| 1451 | // If V is NULL then return false; |
| 1452 | if (V == NULL) return false; |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1453 | |
| 1454 | // Look through bitcast instructions. |
Dan Gohman | 0a60fa3 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1455 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V)) |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1456 | return GetConstantStringInfo(BCI->getOperand(0), Str, Offset, StopAtNul); |
| 1457 | |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1458 | // If the value is not a GEP instruction nor a constant expression with a |
| 1459 | // GEP instruction, then return false because ConstantArray can't occur |
| 1460 | // any other way |
Dan Gohman | 0a60fa3 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1461 | const User *GEP = 0; |
| 1462 | if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) { |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1463 | GEP = GEPI; |
Dan Gohman | 0a60fa3 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1464 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1465 | if (CE->getOpcode() == Instruction::BitCast) |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1466 | return GetConstantStringInfo(CE->getOperand(0), Str, Offset, StopAtNul); |
| 1467 | if (CE->getOpcode() != Instruction::GetElementPtr) |
| 1468 | return false; |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1469 | GEP = CE; |
| 1470 | } |
| 1471 | |
| 1472 | if (GEP) { |
| 1473 | // Make sure the GEP has exactly three arguments. |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1474 | if (GEP->getNumOperands() != 3) |
| 1475 | return false; |
| 1476 | |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1477 | // Make sure the index-ee is a pointer to array of i8. |
| 1478 | const PointerType *PT = cast<PointerType>(GEP->getOperand(0)->getType()); |
| 1479 | const ArrayType *AT = dyn_cast<ArrayType>(PT->getElementType()); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1480 | if (AT == 0 || !AT->getElementType()->isIntegerTy(8)) |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1481 | return false; |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1482 | |
| 1483 | // Check to make sure that the first operand of the GEP is an integer and |
| 1484 | // has value 0 so that we are sure we're indexing into the initializer. |
Dan Gohman | 0a60fa3 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1485 | const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1)); |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1486 | if (FirstIdx == 0 || !FirstIdx->isZero()) |
| 1487 | return false; |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1488 | |
| 1489 | // If the second index isn't a ConstantInt, then this is a variable index |
| 1490 | // into the array. If this occurs, we can't say anything meaningful about |
| 1491 | // the string. |
| 1492 | uint64_t StartIdx = 0; |
Dan Gohman | 0a60fa3 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1493 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1494 | StartIdx = CI->getZExtValue(); |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1495 | else |
| 1496 | return false; |
| 1497 | return GetConstantStringInfo(GEP->getOperand(0), Str, StartIdx+Offset, |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1498 | StopAtNul); |
| 1499 | } |
| 1500 | |
| 1501 | // The GEP instruction, constant or instruction, must reference a global |
| 1502 | // variable that is a constant and is initialized. The referenced constant |
| 1503 | // initializer is the array that we'll use for optimization. |
Dan Gohman | 0a60fa3 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1504 | const GlobalVariable* GV = dyn_cast<GlobalVariable>(V); |
Dan Gohman | 8255573 | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 1505 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer()) |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1506 | return false; |
Dan Gohman | 0a60fa3 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1507 | const Constant *GlobalInit = GV->getInitializer(); |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1508 | |
| 1509 | // Handle the ConstantAggregateZero case |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1510 | if (isa<ConstantAggregateZero>(GlobalInit)) { |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1511 | // This is a degenerate case. The initializer is constant zero so the |
| 1512 | // length of the string must be zero. |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1513 | Str.clear(); |
| 1514 | return true; |
| 1515 | } |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1516 | |
| 1517 | // Must be a Constant Array |
Dan Gohman | 0a60fa3 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1518 | const ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1519 | if (Array == 0 || !Array->getType()->getElementType()->isIntegerTy(8)) |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1520 | return false; |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1521 | |
| 1522 | // Get the number of elements in the array |
| 1523 | uint64_t NumElts = Array->getType()->getNumElements(); |
| 1524 | |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1525 | if (Offset > NumElts) |
| 1526 | return false; |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1527 | |
| 1528 | // Traverse the constant array from 'Offset' which is the place the GEP refers |
| 1529 | // to in the array. |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1530 | Str.reserve(NumElts-Offset); |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1531 | for (unsigned i = Offset; i != NumElts; ++i) { |
Dan Gohman | 0a60fa3 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 1532 | const Constant *Elt = Array->getOperand(i); |
| 1533 | const ConstantInt *CI = dyn_cast<ConstantInt>(Elt); |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1534 | if (!CI) // This array isn't suitable, non-int initializer. |
| 1535 | return false; |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1536 | if (StopAtNul && CI->isZero()) |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1537 | return true; // we found end of string, success! |
| 1538 | Str += (char)CI->getZExtValue(); |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1539 | } |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1540 | |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1541 | // The array isn't null terminated, but maybe this is a memcpy, not a strcpy. |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1542 | return true; |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 1543 | } |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 1544 | |
| 1545 | // These next two are very similar to the above, but also look through PHI |
| 1546 | // nodes. |
| 1547 | // TODO: See if we can integrate these two together. |
| 1548 | |
| 1549 | /// GetStringLengthH - If we can compute the length of the string pointed to by |
| 1550 | /// the specified pointer, return 'len+1'. If we can't, return 0. |
| 1551 | static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) { |
| 1552 | // Look through noop bitcast instructions. |
| 1553 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) |
| 1554 | return GetStringLengthH(BCI->getOperand(0), PHIs); |
| 1555 | |
| 1556 | // If this is a PHI node, there are two cases: either we have already seen it |
| 1557 | // or we haven't. |
| 1558 | if (PHINode *PN = dyn_cast<PHINode>(V)) { |
| 1559 | if (!PHIs.insert(PN)) |
| 1560 | return ~0ULL; // already in the set. |
| 1561 | |
| 1562 | // If it was new, see if all the input strings are the same length. |
| 1563 | uint64_t LenSoFar = ~0ULL; |
| 1564 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1565 | uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs); |
| 1566 | if (Len == 0) return 0; // Unknown length -> unknown. |
| 1567 | |
| 1568 | if (Len == ~0ULL) continue; |
| 1569 | |
| 1570 | if (Len != LenSoFar && LenSoFar != ~0ULL) |
| 1571 | return 0; // Disagree -> unknown. |
| 1572 | LenSoFar = Len; |
| 1573 | } |
| 1574 | |
| 1575 | // Success, all agree. |
| 1576 | return LenSoFar; |
| 1577 | } |
| 1578 | |
| 1579 | // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y) |
| 1580 | if (SelectInst *SI = dyn_cast<SelectInst>(V)) { |
| 1581 | uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs); |
| 1582 | if (Len1 == 0) return 0; |
| 1583 | uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs); |
| 1584 | if (Len2 == 0) return 0; |
| 1585 | if (Len1 == ~0ULL) return Len2; |
| 1586 | if (Len2 == ~0ULL) return Len1; |
| 1587 | if (Len1 != Len2) return 0; |
| 1588 | return Len1; |
| 1589 | } |
| 1590 | |
| 1591 | // If the value is not a GEP instruction nor a constant expression with a |
| 1592 | // GEP instruction, then return unknown. |
| 1593 | User *GEP = 0; |
| 1594 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) { |
| 1595 | GEP = GEPI; |
| 1596 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 1597 | if (CE->getOpcode() != Instruction::GetElementPtr) |
| 1598 | return 0; |
| 1599 | GEP = CE; |
| 1600 | } else { |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
| 1604 | // Make sure the GEP has exactly three arguments. |
| 1605 | if (GEP->getNumOperands() != 3) |
| 1606 | return 0; |
| 1607 | |
| 1608 | // Check to make sure that the first operand of the GEP is an integer and |
| 1609 | // has value 0 so that we are sure we're indexing into the initializer. |
| 1610 | if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) { |
| 1611 | if (!Idx->isZero()) |
| 1612 | return 0; |
| 1613 | } else |
| 1614 | return 0; |
| 1615 | |
| 1616 | // If the second index isn't a ConstantInt, then this is a variable index |
| 1617 | // into the array. If this occurs, we can't say anything meaningful about |
| 1618 | // the string. |
| 1619 | uint64_t StartIdx = 0; |
| 1620 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) |
| 1621 | StartIdx = CI->getZExtValue(); |
| 1622 | else |
| 1623 | return 0; |
| 1624 | |
| 1625 | // The GEP instruction, constant or instruction, must reference a global |
| 1626 | // variable that is a constant and is initialized. The referenced constant |
| 1627 | // initializer is the array that we'll use for optimization. |
| 1628 | GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
| 1629 | if (!GV || !GV->isConstant() || !GV->hasInitializer() || |
| 1630 | GV->mayBeOverridden()) |
| 1631 | return 0; |
| 1632 | Constant *GlobalInit = GV->getInitializer(); |
| 1633 | |
| 1634 | // Handle the ConstantAggregateZero case, which is a degenerate case. The |
| 1635 | // initializer is constant zero so the length of the string must be zero. |
| 1636 | if (isa<ConstantAggregateZero>(GlobalInit)) |
| 1637 | return 1; // Len = 0 offset by 1. |
| 1638 | |
| 1639 | // Must be a Constant Array |
| 1640 | ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit); |
| 1641 | if (!Array || !Array->getType()->getElementType()->isIntegerTy(8)) |
| 1642 | return false; |
| 1643 | |
| 1644 | // Get the number of elements in the array |
| 1645 | uint64_t NumElts = Array->getType()->getNumElements(); |
| 1646 | |
| 1647 | // Traverse the constant array from StartIdx (derived above) which is |
| 1648 | // the place the GEP refers to in the array. |
| 1649 | for (unsigned i = StartIdx; i != NumElts; ++i) { |
| 1650 | Constant *Elt = Array->getOperand(i); |
| 1651 | ConstantInt *CI = dyn_cast<ConstantInt>(Elt); |
| 1652 | if (!CI) // This array isn't suitable, non-int initializer. |
| 1653 | return 0; |
| 1654 | if (CI->isZero()) |
| 1655 | return i-StartIdx+1; // We found end of string, success! |
| 1656 | } |
| 1657 | |
| 1658 | return 0; // The array isn't null terminated, conservatively return 'unknown'. |
| 1659 | } |
| 1660 | |
| 1661 | /// GetStringLength - If we can compute the length of the string pointed to by |
| 1662 | /// the specified pointer, return 'len+1'. If we can't, return 0. |
| 1663 | uint64_t llvm::GetStringLength(Value *V) { |
| 1664 | if (!V->getType()->isPointerTy()) return 0; |
| 1665 | |
| 1666 | SmallPtrSet<PHINode*, 32> PHIs; |
| 1667 | uint64_t Len = GetStringLengthH(V, PHIs); |
| 1668 | // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return |
| 1669 | // an empty string as a length. |
| 1670 | return Len == ~0ULL ? 1 : Len; |
| 1671 | } |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 1672 | |
Dan Gohman | bd1801b | 2011-01-24 18:53:32 +0000 | [diff] [blame] | 1673 | Value * |
| 1674 | llvm::GetUnderlyingObject(Value *V, const TargetData *TD, unsigned MaxLookup) { |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 1675 | if (!V->getType()->isPointerTy()) |
| 1676 | return V; |
| 1677 | for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) { |
| 1678 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
| 1679 | V = GEP->getPointerOperand(); |
| 1680 | } else if (Operator::getOpcode(V) == Instruction::BitCast) { |
| 1681 | V = cast<Operator>(V)->getOperand(0); |
| 1682 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
| 1683 | if (GA->mayBeOverridden()) |
| 1684 | return V; |
| 1685 | V = GA->getAliasee(); |
| 1686 | } else { |
Dan Gohman | c01895c | 2010-12-15 20:49:55 +0000 | [diff] [blame] | 1687 | // See if InstructionSimplify knows any relevant tricks. |
| 1688 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | bd1801b | 2011-01-24 18:53:32 +0000 | [diff] [blame] | 1689 | // TODO: Aquire a DominatorTree and use it. |
| 1690 | if (Value *Simplified = SimplifyInstruction(I, TD, 0)) { |
Dan Gohman | c01895c | 2010-12-15 20:49:55 +0000 | [diff] [blame] | 1691 | V = Simplified; |
| 1692 | continue; |
| 1693 | } |
| 1694 | |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 1695 | return V; |
| 1696 | } |
| 1697 | assert(V->getType()->isPointerTy() && "Unexpected operand type!"); |
| 1698 | } |
| 1699 | return V; |
| 1700 | } |