Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 1 | //===- llvm/Analysis/BasicAliasAnalysis.h - Alias Analysis Impl -*- C++ -*-===// |
| 2 | // |
| 3 | // This file defines the default implementation of the Alias Analysis interface |
| 4 | // that simply implements a few identities (two different globals cannot alias, |
| 5 | // etc), but otherwise does no analysis. |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Analysis/AliasAnalysis.h" |
| 10 | #include "llvm/Pass.h" |
| 11 | #include "llvm/iMemory.h" |
| 12 | #include "llvm/iOther.h" |
| 13 | #include "llvm/ConstantHandling.h" |
| 14 | #include "llvm/GlobalValue.h" |
| 15 | #include "llvm/DerivedTypes.h" |
| 16 | #include "llvm/Target/TargetData.h" |
| 17 | |
| 18 | // Make sure that anything that uses AliasAnalysis pulls in this file... |
| 19 | void BasicAAStub() {} |
| 20 | |
Chris Lattner | 3e76157 | 2003-03-06 16:37:29 +0000 | [diff] [blame] | 21 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | struct BasicAliasAnalysis : public ImmutablePass, public AliasAnalysis { |
| 24 | |
| 25 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 26 | AliasAnalysis::getAnalysisUsage(AU); |
| 27 | } |
| 28 | |
| 29 | virtual void initializePass(); |
| 30 | |
| 31 | // alias - This is the only method here that does anything interesting... |
| 32 | // |
| 33 | AliasResult alias(const Value *V1, unsigned V1Size, |
| 34 | const Value *V2, unsigned V2Size); |
| 35 | private: |
| 36 | // CheckGEPInstructions - Check two GEP instructions of compatible types and |
| 37 | // equal number of arguments. This checks to see if the index expressions |
| 38 | // preclude the pointers from aliasing... |
| 39 | AliasResult CheckGEPInstructions(GetElementPtrInst *GEP1, unsigned G1Size, |
| 40 | GetElementPtrInst *GEP2, unsigned G2Size); |
| 41 | }; |
| 42 | |
| 43 | // Register this pass... |
| 44 | RegisterOpt<BasicAliasAnalysis> |
| 45 | X("basicaa", "Basic Alias Analysis (default AA impl)"); |
| 46 | |
| 47 | // Declare that we implement the AliasAnalysis interface |
| 48 | RegisterAnalysisGroup<AliasAnalysis, BasicAliasAnalysis, true> Y; |
| 49 | } // End of anonymous namespace |
| 50 | |
| 51 | void BasicAliasAnalysis::initializePass() { |
| 52 | InitializeAliasAnalysis(this); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | // hasUniqueAddress - Return true if the |
| 58 | static inline bool hasUniqueAddress(const Value *V) { |
| 59 | return isa<GlobalValue>(V) || isa<MallocInst>(V) || isa<AllocaInst>(V); |
| 60 | } |
| 61 | |
| 62 | static const Value *getUnderlyingObject(const Value *V) { |
| 63 | if (!isa<PointerType>(V->getType())) return 0; |
| 64 | |
| 65 | // If we are at some type of object... return it. |
| 66 | if (hasUniqueAddress(V)) return V; |
| 67 | |
| 68 | // Traverse through different addressing mechanisms... |
| 69 | if (const Instruction *I = dyn_cast<Instruction>(V)) { |
| 70 | if (isa<CastInst>(I) || isa<GetElementPtrInst>(I)) |
| 71 | return getUnderlyingObject(I->getOperand(0)); |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | // alias - Provide a bunch of ad-hoc rules to disambiguate in common cases, such |
| 78 | // as array references. Note that this function is heavily tail recursive. |
| 79 | // Hopefully we have a smart C++ compiler. :) |
| 80 | // |
| 81 | AliasAnalysis::AliasResult |
| 82 | BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size, |
| 83 | const Value *V2, unsigned V2Size) { |
| 84 | // Strip off constant pointer refs if they exist |
| 85 | if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V1)) |
| 86 | V1 = CPR->getValue(); |
| 87 | if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V2)) |
| 88 | V2 = CPR->getValue(); |
| 89 | |
| 90 | // Are we checking for alias of the same value? |
| 91 | if (V1 == V2) return MustAlias; |
| 92 | |
| 93 | if ((!isa<PointerType>(V1->getType()) || !isa<PointerType>(V2->getType())) && |
| 94 | V1->getType() != Type::LongTy && V2->getType() != Type::LongTy) |
| 95 | return NoAlias; // Scalars cannot alias each other |
| 96 | |
| 97 | // Strip off cast instructions... |
| 98 | if (const Instruction *I = dyn_cast<CastInst>(V1)) |
| 99 | return alias(I->getOperand(0), V1Size, V2, V2Size); |
| 100 | if (const Instruction *I = dyn_cast<CastInst>(V2)) |
| 101 | return alias(V1, V1Size, I->getOperand(0), V2Size); |
| 102 | |
| 103 | // Figure out what objects these things are pointing to if we can... |
| 104 | const Value *O1 = getUnderlyingObject(V1); |
| 105 | const Value *O2 = getUnderlyingObject(V2); |
| 106 | |
| 107 | // Pointing at a discernable object? |
| 108 | if (O1 && O2) { |
| 109 | // If they are two different objects, we know that we have no alias... |
| 110 | if (O1 != O2) return NoAlias; |
| 111 | |
| 112 | // If they are the same object, they we can look at the indexes. If they |
| 113 | // index off of the object is the same for both pointers, they must alias. |
| 114 | // If they are provably different, they must not alias. Otherwise, we can't |
| 115 | // tell anything. |
| 116 | } else if (O1 && isa<ConstantPointerNull>(V2)) { |
| 117 | return NoAlias; // Unique values don't alias null |
| 118 | } else if (O2 && isa<ConstantPointerNull>(V1)) { |
| 119 | return NoAlias; // Unique values don't alias null |
| 120 | } |
| 121 | |
| 122 | // If we have two gep instructions with identical indices, return an alias |
| 123 | // result equal to the alias result of the original pointer... |
| 124 | // |
| 125 | if (const GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(V1)) |
| 126 | if (const GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(V2)) |
| 127 | if (GEP1->getNumOperands() == GEP2->getNumOperands() && |
| 128 | GEP1->getOperand(0)->getType() == GEP2->getOperand(0)->getType()) { |
| 129 | AliasResult GAlias = |
| 130 | CheckGEPInstructions((GetElementPtrInst*)GEP1, V1Size, |
| 131 | (GetElementPtrInst*)GEP2, V2Size); |
| 132 | if (GAlias != MayAlias) |
| 133 | return GAlias; |
| 134 | } |
| 135 | |
| 136 | // Check to see if these two pointers are related by a getelementptr |
| 137 | // instruction. If one pointer is a GEP with a non-zero index of the other |
| 138 | // pointer, we know they cannot alias. |
| 139 | // |
| 140 | if (isa<GetElementPtrInst>(V2)) { |
| 141 | std::swap(V1, V2); |
| 142 | std::swap(V1Size, V2Size); |
| 143 | } |
| 144 | |
Chris Lattner | c330ee6 | 2003-02-26 21:57:23 +0000 | [diff] [blame] | 145 | if (V1Size != ~0U && V2Size != ~0U) |
| 146 | if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V1)) { |
| 147 | AliasResult R = alias(GEP->getOperand(0), V1Size, V2, V2Size); |
Chris Lattner | c330ee6 | 2003-02-26 21:57:23 +0000 | [diff] [blame] | 148 | if (R == MustAlias) { |
| 149 | // If there is at least one non-zero constant index, we know they cannot |
| 150 | // alias. |
| 151 | bool ConstantFound = false; |
| 152 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 153 | if (const Constant *C = dyn_cast<Constant>(GEP->getOperand(i))) |
| 154 | if (!C->isNullValue()) { |
| 155 | ConstantFound = true; |
| 156 | break; |
| 157 | } |
| 158 | if (ConstantFound) { |
| 159 | if (V2Size <= 1 && V1Size <= 1) // Just pointer check? |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 160 | return NoAlias; |
Chris Lattner | c330ee6 | 2003-02-26 21:57:23 +0000 | [diff] [blame] | 161 | |
| 162 | // Otherwise we have to check to see that the distance is more than |
| 163 | // the size of the argument... build an index vector that is equal to |
| 164 | // the arguments provided, except substitute 0's for any variable |
| 165 | // indexes we find... |
| 166 | |
| 167 | std::vector<Value*> Indices; |
| 168 | Indices.reserve(GEP->getNumOperands()-1); |
| 169 | for (unsigned i = 1; i != GEP->getNumOperands(); ++i) |
| 170 | if (const Constant *C = dyn_cast<Constant>(GEP->getOperand(i))) |
| 171 | Indices.push_back((Value*)C); |
| 172 | else |
| 173 | Indices.push_back(Constant::getNullValue(Type::LongTy)); |
| 174 | const Type *Ty = GEP->getOperand(0)->getType(); |
| 175 | int Offset = getTargetData().getIndexedOffset(Ty, Indices); |
| 176 | if (Offset >= (int)V2Size || Offset <= -(int)V1Size) |
| 177 | return NoAlias; |
| 178 | } |
| 179 | } |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 180 | } |
Chris Lattner | c330ee6 | 2003-02-26 21:57:23 +0000 | [diff] [blame] | 181 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 182 | return MayAlias; |
| 183 | } |
| 184 | |
Chris Lattner | 920bd79 | 2003-06-02 05:42:39 +0000 | [diff] [blame^] | 185 | static Value *CheckArrayIndicesForOverflow(const Type *PtrTy, |
| 186 | const std::vector<Value*> &Indices, |
| 187 | const ConstantInt *Idx) { |
| 188 | if (const ConstantSInt *IdxS = dyn_cast<ConstantSInt>(Idx)) { |
| 189 | if (IdxS->getValue() < 0) // Underflow on the array subscript? |
| 190 | return Constant::getNullValue(Type::LongTy); |
| 191 | else { // Check for overflow |
| 192 | const ArrayType *ATy = |
| 193 | cast<ArrayType>(GetElementPtrInst::getIndexedType(PtrTy, Indices,true)); |
| 194 | if (IdxS->getValue() >= (int64_t)ATy->getNumElements()) |
| 195 | return ConstantSInt::get(Type::LongTy, ATy->getNumElements()-1); |
| 196 | } |
| 197 | } |
| 198 | return (Value*)Idx; // Everything is acceptable. |
| 199 | } |
| 200 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 201 | // CheckGEPInstructions - Check two GEP instructions of compatible types and |
| 202 | // equal number of arguments. This checks to see if the index expressions |
| 203 | // preclude the pointers from aliasing... |
| 204 | // |
| 205 | AliasAnalysis::AliasResult |
| 206 | BasicAliasAnalysis::CheckGEPInstructions(GetElementPtrInst *GEP1, unsigned G1S, |
| 207 | GetElementPtrInst *GEP2, unsigned G2S){ |
| 208 | // Do the base pointers alias? |
| 209 | AliasResult BaseAlias = alias(GEP1->getOperand(0), G1S, |
| 210 | GEP2->getOperand(0), G2S); |
| 211 | if (BaseAlias != MustAlias) // No or May alias: We cannot add anything... |
| 212 | return BaseAlias; |
| 213 | |
| 214 | // Find the (possibly empty) initial sequence of equal values... |
| 215 | unsigned NumGEPOperands = GEP1->getNumOperands(); |
| 216 | unsigned UnequalOper = 1; |
| 217 | while (UnequalOper != NumGEPOperands && |
| 218 | GEP1->getOperand(UnequalOper) == GEP2->getOperand(UnequalOper)) |
| 219 | ++UnequalOper; |
| 220 | |
| 221 | // If all operands equal each other, then the derived pointers must |
| 222 | // alias each other... |
| 223 | if (UnequalOper == NumGEPOperands) return MustAlias; |
| 224 | |
| 225 | // So now we know that the indexes derived from the base pointers, |
| 226 | // which are known to alias, are different. We can still determine a |
| 227 | // no-alias result if there are differing constant pairs in the index |
| 228 | // chain. For example: |
| 229 | // A[i][0] != A[j][1] iff (&A[0][1]-&A[0][0] >= std::max(G1S, G2S)) |
| 230 | // |
| 231 | unsigned SizeMax = std::max(G1S, G2S); |
| 232 | if (SizeMax == ~0U) return MayAlias; // Avoid frivolous work... |
Chris Lattner | 920bd79 | 2003-06-02 05:42:39 +0000 | [diff] [blame^] | 233 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 234 | // Scan for the first operand that is constant and unequal in the |
| 235 | // two getelemenptrs... |
| 236 | unsigned FirstConstantOper = UnequalOper; |
| 237 | for (; FirstConstantOper != NumGEPOperands; ++FirstConstantOper) { |
| 238 | const Value *G1Oper = GEP1->getOperand(FirstConstantOper); |
| 239 | const Value *G2Oper = GEP2->getOperand(FirstConstantOper); |
| 240 | if (G1Oper != G2Oper && // Found non-equal constant indexes... |
| 241 | isa<Constant>(G1Oper) && isa<Constant>(G2Oper)) { |
| 242 | // Make sure they are comparable... and make sure the GEP with |
| 243 | // the smaller leading constant is GEP1. |
| 244 | ConstantBool *Compare = |
| 245 | *cast<Constant>(GEP1->getOperand(FirstConstantOper)) > |
| 246 | *cast<Constant>(GEP2->getOperand(FirstConstantOper)); |
| 247 | if (Compare) { // If they are comparable... |
| 248 | if (Compare->getValue()) |
| 249 | std::swap(GEP1, GEP2); // Make GEP1 < GEP2 |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // No constant operands, we cannot tell anything... |
| 256 | if (FirstConstantOper == NumGEPOperands) return MayAlias; |
| 257 | |
| 258 | // If there are non-equal constants arguments, then we can figure |
| 259 | // out a minimum known delta between the two index expressions... at |
| 260 | // this point we know that the first constant index of GEP1 is less |
| 261 | // than the first constant index of GEP2. |
| 262 | // |
| 263 | std::vector<Value*> Indices1; |
| 264 | Indices1.reserve(NumGEPOperands-1); |
| 265 | for (unsigned i = 1; i != FirstConstantOper; ++i) |
Chris Lattner | a36635a | 2003-02-26 21:28:49 +0000 | [diff] [blame] | 266 | if (GEP1->getOperand(i)->getType() == Type::UByteTy) |
| 267 | Indices1.push_back(GEP1->getOperand(i)); |
| 268 | else |
| 269 | Indices1.push_back(Constant::getNullValue(Type::LongTy)); |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 270 | std::vector<Value*> Indices2; |
| 271 | Indices2.reserve(NumGEPOperands-1); |
| 272 | Indices2 = Indices1; // Copy the zeros prefix... |
| 273 | |
| 274 | // Add the two known constant operands... |
| 275 | Indices1.push_back((Value*)GEP1->getOperand(FirstConstantOper)); |
| 276 | Indices2.push_back((Value*)GEP2->getOperand(FirstConstantOper)); |
| 277 | |
| 278 | const Type *GEPPointerTy = GEP1->getOperand(0)->getType(); |
| 279 | |
| 280 | // Loop over the rest of the operands... |
Chris Lattner | 920bd79 | 2003-06-02 05:42:39 +0000 | [diff] [blame^] | 281 | for (unsigned i = FirstConstantOper+1; i != NumGEPOperands; ++i) { |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 282 | const Value *Op1 = GEP1->getOperand(i); |
Chris Lattner | a36635a | 2003-02-26 21:28:49 +0000 | [diff] [blame] | 283 | const Value *Op2 = GEP2->getOperand(i); |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 284 | if (Op1 == Op2) { // If they are equal, use a zero index... |
| 285 | Indices1.push_back(Constant::getNullValue(Op1->getType())); |
| 286 | Indices2.push_back(Indices1.back()); |
| 287 | } else { |
Chris Lattner | 920bd79 | 2003-06-02 05:42:39 +0000 | [diff] [blame^] | 288 | if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
| 289 | // If this is an array index, make sure the array element is in range... |
| 290 | if (i != 1) // The pointer index can be "out of range" |
| 291 | Op1 = CheckArrayIndicesForOverflow(GEPPointerTy, Indices1, Op1C); |
| 292 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 293 | Indices1.push_back((Value*)Op1); |
Chris Lattner | 920bd79 | 2003-06-02 05:42:39 +0000 | [diff] [blame^] | 294 | } else { |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 295 | // GEP1 is known to produce a value less than GEP2. To be |
Chris Lattner | 506b4e4 | 2003-03-04 16:40:17 +0000 | [diff] [blame] | 296 | // conservatively correct, we must assume the largest possible constant |
| 297 | // is used in this position. This cannot be the initial index to the |
| 298 | // GEP instructions (because we know we have at least one element before |
| 299 | // this one with the different constant arguments), so we know that the |
| 300 | // current index must be into either a struct or array. Because we know |
| 301 | // it's not constant, this cannot be a structure index. Because of |
| 302 | // this, we can calculate the maximum value possible. |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 303 | // |
Chris Lattner | 506b4e4 | 2003-03-04 16:40:17 +0000 | [diff] [blame] | 304 | const ArrayType *ElTy = |
| 305 | cast<ArrayType>(GEP1->getIndexedType(GEPPointerTy, Indices1, true)); |
| 306 | Indices1.push_back(ConstantSInt::get(Type::LongTy, |
| 307 | ElTy->getNumElements()-1)); |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Chris Lattner | 920bd79 | 2003-06-02 05:42:39 +0000 | [diff] [blame^] | 310 | if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op2)) { |
| 311 | // If this is an array index, make sure the array element is in range... |
| 312 | if (i != 1) // The pointer index can be "out of range" |
| 313 | Op1 = CheckArrayIndicesForOverflow(GEPPointerTy, Indices2, Op1C); |
| 314 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 315 | Indices2.push_back((Value*)Op2); |
Chris Lattner | 920bd79 | 2003-06-02 05:42:39 +0000 | [diff] [blame^] | 316 | } |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 317 | else // Conservatively assume the minimum value for this index |
Chris Lattner | 9098f64 | 2003-05-21 20:23:26 +0000 | [diff] [blame] | 318 | Indices2.push_back(Constant::getNullValue(Op2->getType())); |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Chris Lattner | 807b705 | 2003-04-25 18:03:06 +0000 | [diff] [blame] | 322 | int64_t Offset1 = getTargetData().getIndexedOffset(GEPPointerTy, Indices1); |
| 323 | int64_t Offset2 = getTargetData().getIndexedOffset(GEPPointerTy, Indices2); |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 324 | assert(Offset1 < Offset2 &&"There is at least one different constant here!"); |
| 325 | |
Chris Lattner | 807b705 | 2003-04-25 18:03:06 +0000 | [diff] [blame] | 326 | if ((uint64_t)(Offset2-Offset1) >= SizeMax) { |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 327 | //std::cerr << "Determined that these two GEP's don't alias [" |
| 328 | // << SizeMax << " bytes]: \n" << *GEP1 << *GEP2; |
| 329 | return NoAlias; |
| 330 | } |
| 331 | return MayAlias; |
| 332 | } |
| 333 | |