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