Chris Lattner | 9d7c9ea | 2003-11-25 20:11:47 +0000 | [diff] [blame] | 1 | //===- BasicAliasAnalysis.cpp - Local Alias Analysis Impl -----------------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines the default implementation of the Alias Analysis interface |
| 11 | // that simply implements a few identities (two different globals cannot alias, |
| 12 | // etc), but otherwise does no analysis. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/Analysis/AliasAnalysis.h" |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/Passes.h" |
Chris Lattner | 4244bb5 | 2004-03-15 03:36:49 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Function.h" |
| 21 | #include "llvm/GlobalVariable.h" |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 22 | #include "llvm/Instructions.h" |
Owen Anderson | 9b636cb | 2008-02-17 21:29:08 +0000 | [diff] [blame] | 23 | #include "llvm/IntrinsicInst.h" |
Dan Gohman | 3a7a68c | 2009-07-17 22:25:10 +0000 | [diff] [blame] | 24 | #include "llvm/Operator.h" |
Chris Lattner | 4244bb5 | 2004-03-15 03:36:49 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Chris Lattner | 5d5261c | 2009-11-26 16:26:43 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/CaptureTracking.h" |
| 27 | #include "llvm/Analysis/MemoryBuiltins.h" |
| 28 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetData.h" |
Chris Lattner | e405c64 | 2009-11-26 17:12:50 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | a77600e | 2007-02-10 22:15:31 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ca9f7ef | 2010-06-29 06:46:00 +0000 | [diff] [blame^] | 32 | #include "llvm/Support/Compiler.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 33 | #include "llvm/Support/ErrorHandling.h" |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 34 | #include <algorithm> |
Chris Lattner | ec4e808 | 2003-11-25 18:33:40 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
| 38 | // Useful predicates |
| 39 | //===----------------------------------------------------------------------===// |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 40 | |
Chris Lattner | a413960 | 2008-06-16 06:10:11 +0000 | [diff] [blame] | 41 | /// isKnownNonNull - Return true if we know that the specified value is never |
| 42 | /// null. |
| 43 | static bool isKnownNonNull(const Value *V) { |
| 44 | // Alloca never returns null, malloc might. |
| 45 | if (isa<AllocaInst>(V)) return true; |
| 46 | |
| 47 | // A byval argument is never null. |
| 48 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 49 | return A->hasByValAttr(); |
| 50 | |
| 51 | // Global values are not null unless extern weak. |
| 52 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 53 | return !GV->hasExternalWeakLinkage(); |
| 54 | return false; |
| 55 | } |
| 56 | |
Chris Lattner | 845f0d2 | 2008-06-16 06:19:11 +0000 | [diff] [blame] | 57 | /// isNonEscapingLocalObject - Return true if the pointer is to a function-local |
| 58 | /// object that never escapes from the function. |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 59 | static bool isNonEscapingLocalObject(const Value *V, bool Interprocedural) { |
Chris Lattner | e727579 | 2008-06-16 06:28:01 +0000 | [diff] [blame] | 60 | // If this is a local allocation, check to see if it escapes. |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 61 | if (isa<AllocaInst>(V) || |
| 62 | (!Interprocedural && isNoAliasCall(V))) |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 63 | // Set StoreCaptures to True so that we can assume in our callers that the |
| 64 | // pointer is not the result of a load instruction. Currently |
| 65 | // PointerMayBeCaptured doesn't have any special analysis for the |
| 66 | // StoreCaptures=false case; if it did, our callers could be refined to be |
| 67 | // more precise. |
| 68 | return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true); |
Duncan Sands | 91c9c310 | 2009-01-05 21:19:53 +0000 | [diff] [blame] | 69 | |
Chris Lattner | e727579 | 2008-06-16 06:28:01 +0000 | [diff] [blame] | 70 | // If this is an argument that corresponds to a byval or noalias argument, |
Duncan Sands | 91c9c310 | 2009-01-05 21:19:53 +0000 | [diff] [blame] | 71 | // then it has not escaped before entering the function. Check if it escapes |
| 72 | // inside the function. |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 73 | if (!Interprocedural) |
| 74 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 75 | if (A->hasByValAttr() || A->hasNoAliasAttr()) { |
| 76 | // Don't bother analyzing arguments already known not to escape. |
| 77 | if (A->hasNoCaptureAttr()) |
| 78 | return true; |
| 79 | return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true); |
| 80 | } |
Chris Lattner | 845f0d2 | 2008-06-16 06:19:11 +0000 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 84 | /// isEscapeSource - Return true if the pointer is one which would have |
| 85 | /// been considered an escape by isNonEscapingLocalObject. |
| 86 | static bool isEscapeSource(const Value *V, bool Interprocedural) { |
| 87 | if (!Interprocedural) |
| 88 | if (isa<CallInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V)) |
| 89 | return true; |
| 90 | |
| 91 | // The load case works because isNonEscapingLocalObject considers all |
| 92 | // stores to be escapes (it passes true for the StoreCaptures argument |
| 93 | // to PointerMayBeCaptured). |
| 94 | if (isa<LoadInst>(V)) |
| 95 | return true; |
| 96 | |
| 97 | return false; |
| 98 | } |
Chris Lattner | 845f0d2 | 2008-06-16 06:19:11 +0000 | [diff] [blame] | 99 | |
Chris Lattner | a413960 | 2008-06-16 06:10:11 +0000 | [diff] [blame] | 100 | /// isObjectSmallerThan - Return true if we can prove that the object specified |
| 101 | /// by V is smaller than Size. |
| 102 | static bool isObjectSmallerThan(const Value *V, unsigned Size, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 103 | const TargetData &TD) { |
Chris Lattner | 295d4e9 | 2008-12-08 06:28:54 +0000 | [diff] [blame] | 104 | const Type *AccessTy; |
| 105 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
Chris Lattner | a413960 | 2008-06-16 06:10:11 +0000 | [diff] [blame] | 106 | AccessTy = GV->getType()->getElementType(); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 107 | } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
Chris Lattner | a413960 | 2008-06-16 06:10:11 +0000 | [diff] [blame] | 108 | if (!AI->isArrayAllocation()) |
| 109 | AccessTy = AI->getType()->getElementType(); |
Chris Lattner | 295d4e9 | 2008-12-08 06:28:54 +0000 | [diff] [blame] | 110 | else |
| 111 | return false; |
Victor Hernandez | 46e8312 | 2009-09-18 21:34:51 +0000 | [diff] [blame] | 112 | } else if (const CallInst* CI = extractMallocCall(V)) { |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 113 | if (!isArrayMalloc(V, &TD)) |
Victor Hernandez | 46e8312 | 2009-09-18 21:34:51 +0000 | [diff] [blame] | 114 | // The size is the argument to the malloc call. |
Gabor Greif | 71339c9 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 115 | if (const ConstantInt* C = dyn_cast<ConstantInt>(CI->getArgOperand(0))) |
Victor Hernandez | 46e8312 | 2009-09-18 21:34:51 +0000 | [diff] [blame] | 116 | return (C->getZExtValue() < Size); |
| 117 | return false; |
Chris Lattner | 295d4e9 | 2008-12-08 06:28:54 +0000 | [diff] [blame] | 118 | } else if (const Argument *A = dyn_cast<Argument>(V)) { |
Chris Lattner | a413960 | 2008-06-16 06:10:11 +0000 | [diff] [blame] | 119 | if (A->hasByValAttr()) |
| 120 | AccessTy = cast<PointerType>(A->getType())->getElementType(); |
Chris Lattner | 295d4e9 | 2008-12-08 06:28:54 +0000 | [diff] [blame] | 121 | else |
| 122 | return false; |
| 123 | } else { |
| 124 | return false; |
| 125 | } |
Chris Lattner | a413960 | 2008-06-16 06:10:11 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 295d4e9 | 2008-12-08 06:28:54 +0000 | [diff] [blame] | 127 | if (AccessTy->isSized()) |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 128 | return TD.getTypeAllocSize(AccessTy) < Size; |
Chris Lattner | a413960 | 2008-06-16 06:10:11 +0000 | [diff] [blame] | 129 | return false; |
| 130 | } |
| 131 | |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 132 | //===----------------------------------------------------------------------===// |
| 133 | // NoAA Pass |
| 134 | //===----------------------------------------------------------------------===// |
| 135 | |
| 136 | namespace { |
| 137 | /// NoAA - This class implements the -no-aa pass, which always returns "I |
| 138 | /// don't know" for alias queries. NoAA is unlike other alias analysis |
| 139 | /// implementations, in that it does not chain to a previous analysis. As |
| 140 | /// such it doesn't follow many of the rules that other alias analyses must. |
| 141 | /// |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 142 | struct NoAA : public ImmutablePass, public AliasAnalysis { |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 143 | static char ID; // Class identification, replacement for typeinfo |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 144 | NoAA() : ImmutablePass(&ID) {} |
| 145 | explicit NoAA(void *PID) : ImmutablePass(PID) { } |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 146 | |
| 147 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | virtual void initializePass() { |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 151 | TD = getAnalysisIfAvailable<TargetData>(); |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | virtual AliasResult alias(const Value *V1, unsigned V1Size, |
| 155 | const Value *V2, unsigned V2Size) { |
| 156 | return MayAlias; |
| 157 | } |
| 158 | |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 159 | virtual void getArgumentAccesses(Function *F, CallSite CS, |
| 160 | std::vector<PointerAccessInfo> &Info) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 161 | llvm_unreachable("This method may not be called on this function!"); |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 164 | virtual bool pointsToConstantMemory(const Value *P) { return false; } |
| 165 | virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) { |
| 166 | return ModRef; |
| 167 | } |
| 168 | virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) { |
| 169 | return ModRef; |
| 170 | } |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 171 | |
| 172 | virtual void deleteValue(Value *V) {} |
| 173 | virtual void copyValue(Value *From, Value *To) {} |
Chris Lattner | 2033097 | 2010-01-20 19:26:14 +0000 | [diff] [blame] | 174 | |
| 175 | /// getAdjustedAnalysisPointer - This method is used when a pass implements |
| 176 | /// an analysis interface through multiple inheritance. If needed, it should |
| 177 | /// override this to adjust the this pointer as needed for the specified pass |
| 178 | /// info. |
| 179 | virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) { |
| 180 | if (PI->isPassID(&AliasAnalysis::ID)) |
| 181 | return (AliasAnalysis*)this; |
| 182 | return this; |
| 183 | } |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 184 | }; |
| 185 | } // End of anonymous namespace |
| 186 | |
| 187 | // Register this pass... |
| 188 | char NoAA::ID = 0; |
| 189 | static RegisterPass<NoAA> |
| 190 | U("no-aa", "No Alias Analysis (always returns 'may' alias)", true, true); |
| 191 | |
| 192 | // Declare that we implement the AliasAnalysis interface |
| 193 | static RegisterAnalysisGroup<AliasAnalysis> V(U); |
| 194 | |
| 195 | ImmutablePass *llvm::createNoAAPass() { return new NoAA(); } |
| 196 | |
| 197 | //===----------------------------------------------------------------------===// |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 198 | // BasicAliasAnalysis Pass |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 199 | //===----------------------------------------------------------------------===// |
| 200 | |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 201 | static const Function *getParent(const Value *V) { |
| 202 | if(const Instruction *inst = dyn_cast<Instruction>(V)) |
| 203 | return inst->getParent()->getParent(); |
| 204 | |
| 205 | if(const Argument *arg = dyn_cast<Argument>(V)) |
| 206 | return arg->getParent(); |
| 207 | |
| 208 | return NULL; |
| 209 | } |
| 210 | |
Chandler Carruth | ca9f7ef | 2010-06-29 06:46:00 +0000 | [diff] [blame^] | 211 | static bool sameParent(const Value *O1, const Value *O2) ATTRIBUTE_UNUSED; |
| 212 | |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 213 | static bool sameParent(const Value *O1, const Value *O2) { |
| 214 | |
| 215 | const Function *F1 = getParent(O1); |
| 216 | const Function *F2 = getParent(O2); |
| 217 | |
| 218 | return !F1 || !F2 || F1 == F2; |
| 219 | } |
| 220 | |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 221 | namespace { |
| 222 | /// BasicAliasAnalysis - This is the default alias analysis implementation. |
| 223 | /// Because it doesn't chain to a previous alias analysis (like -no-aa), it |
| 224 | /// derives from the NoAA class. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 225 | struct BasicAliasAnalysis : public NoAA { |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 226 | /// Interprocedural - Flag for "interprocedural" mode, where we must |
| 227 | /// support queries of values which live in different functions. |
| 228 | bool Interprocedural; |
| 229 | |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 230 | static char ID; // Class identification, replacement for typeinfo |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 231 | BasicAliasAnalysis() |
| 232 | : NoAA(&ID), Interprocedural(false) {} |
| 233 | BasicAliasAnalysis(void *PID, bool interprocedural) |
| 234 | : NoAA(PID), Interprocedural(interprocedural) {} |
| 235 | |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 236 | AliasResult alias(const Value *V1, unsigned V1Size, |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 237 | const Value *V2, unsigned V2Size) { |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 238 | assert(Visited.empty() && "Visited must be cleared after use!"); |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 239 | #ifdef XDEBUG |
| 240 | assert((Interprocedural || sameParent(V1, V2)) && |
| 241 | "BasicAliasAnalysis (-basicaa) doesn't support interprocedural " |
| 242 | "queries; use InterproceduralAliasAnalysis " |
| 243 | "(-interprocedural-basic-aa) instead."); |
| 244 | #endif |
Evan Cheng | f0429fd | 2009-10-14 06:46:26 +0000 | [diff] [blame] | 245 | AliasResult Alias = aliasCheck(V1, V1Size, V2, V2Size); |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 246 | Visited.clear(); |
Evan Cheng | f0429fd | 2009-10-14 06:46:26 +0000 | [diff] [blame] | 247 | return Alias; |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 248 | } |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 249 | |
| 250 | ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size); |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 251 | ModRefResult getModRefInfo(CallSite CS1, CallSite CS2); |
Owen Anderson | e794220 | 2009-02-05 23:36:27 +0000 | [diff] [blame] | 252 | |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 253 | /// pointsToConstantMemory - Chase pointers until we find a (constant |
| 254 | /// global) or not. |
| 255 | bool pointsToConstantMemory(const Value *P); |
| 256 | |
Chris Lattner | 2033097 | 2010-01-20 19:26:14 +0000 | [diff] [blame] | 257 | /// getAdjustedAnalysisPointer - This method is used when a pass implements |
| 258 | /// an analysis interface through multiple inheritance. If needed, it should |
| 259 | /// override this to adjust the this pointer as needed for the specified pass |
| 260 | /// info. |
| 261 | virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) { |
| 262 | if (PI->isPassID(&AliasAnalysis::ID)) |
| 263 | return (AliasAnalysis*)this; |
| 264 | return this; |
| 265 | } |
| 266 | |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 267 | private: |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 268 | // Visited - Track instructions visited by a aliasPHI, aliasSelect(), and aliasGEP(). |
| 269 | SmallPtrSet<const Value*, 16> Visited; |
Evan Cheng | 3dbe43b | 2009-10-14 05:05:02 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 5d56b2d | 2009-11-23 16:45:27 +0000 | [diff] [blame] | 271 | // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP |
| 272 | // instruction against another. |
Chris Lattner | 539c9b9 | 2009-11-26 02:11:08 +0000 | [diff] [blame] | 273 | AliasResult aliasGEP(const GEPOperator *V1, unsigned V1Size, |
Chris Lattner | 23e2a5b | 2009-11-26 02:14:59 +0000 | [diff] [blame] | 274 | const Value *V2, unsigned V2Size, |
| 275 | const Value *UnderlyingV1, const Value *UnderlyingV2); |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 5d56b2d | 2009-11-23 16:45:27 +0000 | [diff] [blame] | 277 | // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI |
| 278 | // instruction against another. |
Evan Cheng | d83c2ca | 2009-10-14 05:22:03 +0000 | [diff] [blame] | 279 | AliasResult aliasPHI(const PHINode *PN, unsigned PNSize, |
Evan Cheng | 3dbe43b | 2009-10-14 05:05:02 +0000 | [diff] [blame] | 280 | const Value *V2, unsigned V2Size); |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 281 | |
Dan Gohman | 6665b0e | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 282 | /// aliasSelect - Disambiguate a Select instruction against another value. |
| 283 | AliasResult aliasSelect(const SelectInst *SI, unsigned SISize, |
| 284 | const Value *V2, unsigned V2Size); |
| 285 | |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 286 | AliasResult aliasCheck(const Value *V1, unsigned V1Size, |
Evan Cheng | 3dbe43b | 2009-10-14 05:05:02 +0000 | [diff] [blame] | 287 | const Value *V2, unsigned V2Size); |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 288 | }; |
| 289 | } // End of anonymous namespace |
| 290 | |
| 291 | // Register this pass... |
| 292 | char BasicAliasAnalysis::ID = 0; |
| 293 | static RegisterPass<BasicAliasAnalysis> |
| 294 | X("basicaa", "Basic Alias Analysis (default AA impl)", false, true); |
| 295 | |
| 296 | // Declare that we implement the AliasAnalysis interface |
| 297 | static RegisterAnalysisGroup<AliasAnalysis, true> Y(X); |
| 298 | |
| 299 | ImmutablePass *llvm::createBasicAliasAnalysisPass() { |
| 300 | return new BasicAliasAnalysis(); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | /// pointsToConstantMemory - Chase pointers until we find a (constant |
| 305 | /// global) or not. |
| 306 | bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) { |
| 307 | if (const GlobalVariable *GV = |
Duncan Sands | 5d0392c | 2008-10-01 15:25:41 +0000 | [diff] [blame] | 308 | dyn_cast<GlobalVariable>(P->getUnderlyingObject())) |
Chris Lattner | b27db37 | 2009-11-23 17:07:35 +0000 | [diff] [blame] | 309 | // Note: this doesn't require GV to be "ODR" because it isn't legal for a |
| 310 | // global to be marked constant in some modules and non-constant in others. |
| 311 | // GV may even be a declaration, not a definition. |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 312 | return GV->isConstant(); |
| 313 | return false; |
| 314 | } |
| 315 | |
Owen Anderson | e794220 | 2009-02-05 23:36:27 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 5d56b2d | 2009-11-23 16:45:27 +0000 | [diff] [blame] | 317 | /// getModRefInfo - Check to see if the specified callsite can clobber the |
| 318 | /// specified memory object. Since we only look at local properties of this |
| 319 | /// function, we really can't say much about this query. We do, however, use |
| 320 | /// simple "address taken" analysis on local objects. |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 321 | AliasAnalysis::ModRefResult |
| 322 | BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) { |
Chris Lattner | 92e803c | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 323 | const Value *Object = P->getUnderlyingObject(); |
| 324 | |
| 325 | // If this is a tail call and P points to a stack location, we know that |
| 326 | // the tail call cannot access or modify the local stack. |
| 327 | // We cannot exclude byval arguments here; these belong to the caller of |
| 328 | // the current function not to the current function, and a tail callee |
| 329 | // may reference them. |
| 330 | if (isa<AllocaInst>(Object)) |
| 331 | if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) |
| 332 | if (CI->isTailCall()) |
| 333 | return NoModRef; |
| 334 | |
| 335 | // If the pointer is to a locally allocated object that does not escape, |
Chris Lattner | b34b82e | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 336 | // then the call can not mod/ref the pointer unless the call takes the pointer |
| 337 | // as an argument, and itself doesn't capture it. |
Chris Lattner | 403ac2e | 2009-11-23 16:46:41 +0000 | [diff] [blame] | 338 | if (!isa<Constant>(Object) && CS.getInstruction() != Object && |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 339 | isNonEscapingLocalObject(Object, Interprocedural)) { |
Chris Lattner | b34b82e | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 340 | bool PassedAsArg = false; |
| 341 | unsigned ArgNo = 0; |
Chris Lattner | 92e803c | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 342 | for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); |
Chris Lattner | b34b82e | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 343 | CI != CE; ++CI, ++ArgNo) { |
| 344 | // Only look at the no-capture pointer arguments. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 345 | if (!(*CI)->getType()->isPointerTy() || |
Chris Lattner | b34b82e | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 346 | !CS.paramHasAttr(ArgNo+1, Attribute::NoCapture)) |
| 347 | continue; |
| 348 | |
| 349 | // If this is a no-capture pointer argument, see if we can tell that it |
| 350 | // is impossible to alias the pointer we're checking. If not, we have to |
| 351 | // assume that the call could touch the pointer, even though it doesn't |
| 352 | // escape. |
Chris Lattner | 403ac2e | 2009-11-23 16:46:41 +0000 | [diff] [blame] | 353 | if (!isNoAlias(cast<Value>(CI), ~0U, P, ~0U)) { |
Chris Lattner | b34b82e | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 354 | PassedAsArg = true; |
| 355 | break; |
| 356 | } |
| 357 | } |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 358 | |
Chris Lattner | b34b82e | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 359 | if (!PassedAsArg) |
Chris Lattner | 92e803c | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 360 | return NoModRef; |
| 361 | } |
| 362 | |
| 363 | // Finally, handle specific knowledge of intrinsics. |
| 364 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction()); |
| 365 | if (II == 0) |
| 366 | return AliasAnalysis::getModRefInfo(CS, P, Size); |
| 367 | |
| 368 | switch (II->getIntrinsicID()) { |
| 369 | default: break; |
| 370 | case Intrinsic::memcpy: |
| 371 | case Intrinsic::memmove: { |
| 372 | unsigned Len = ~0U; |
Gabor Greif | 71339c9 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 373 | if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2))) |
Chris Lattner | 92e803c | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 374 | Len = LenCI->getZExtValue(); |
Gabor Greif | 71339c9 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 375 | Value *Dest = II->getArgOperand(0); |
| 376 | Value *Src = II->getArgOperand(1); |
Chris Lattner | 403ac2e | 2009-11-23 16:46:41 +0000 | [diff] [blame] | 377 | if (isNoAlias(Dest, Len, P, Size)) { |
| 378 | if (isNoAlias(Src, Len, P, Size)) |
Chris Lattner | 92e803c | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 379 | return NoModRef; |
| 380 | return Ref; |
| 381 | } |
| 382 | break; |
| 383 | } |
| 384 | case Intrinsic::memset: |
Chris Lattner | 403ac2e | 2009-11-23 16:46:41 +0000 | [diff] [blame] | 385 | // Since memset is 'accesses arguments' only, the AliasAnalysis base class |
| 386 | // will handle it for the variable length case. |
Gabor Greif | 71339c9 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 387 | if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2))) { |
Chris Lattner | 92e803c | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 388 | unsigned Len = LenCI->getZExtValue(); |
Gabor Greif | 71339c9 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 389 | Value *Dest = II->getArgOperand(0); |
Chris Lattner | 403ac2e | 2009-11-23 16:46:41 +0000 | [diff] [blame] | 390 | if (isNoAlias(Dest, Len, P, Size)) |
Chris Lattner | 25df20f | 2008-06-16 06:38:26 +0000 | [diff] [blame] | 391 | return NoModRef; |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 392 | } |
Chris Lattner | 92e803c | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 393 | break; |
| 394 | case Intrinsic::atomic_cmp_swap: |
| 395 | case Intrinsic::atomic_swap: |
| 396 | case Intrinsic::atomic_load_add: |
| 397 | case Intrinsic::atomic_load_sub: |
| 398 | case Intrinsic::atomic_load_and: |
| 399 | case Intrinsic::atomic_load_nand: |
| 400 | case Intrinsic::atomic_load_or: |
| 401 | case Intrinsic::atomic_load_xor: |
| 402 | case Intrinsic::atomic_load_max: |
| 403 | case Intrinsic::atomic_load_min: |
| 404 | case Intrinsic::atomic_load_umax: |
| 405 | case Intrinsic::atomic_load_umin: |
| 406 | if (TD) { |
Gabor Greif | 71339c9 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 407 | Value *Op1 = II->getArgOperand(0); |
Chris Lattner | 92e803c | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 408 | unsigned Op1Size = TD->getTypeStoreSize(Op1->getType()); |
Chris Lattner | 403ac2e | 2009-11-23 16:46:41 +0000 | [diff] [blame] | 409 | if (isNoAlias(Op1, Op1Size, P, Size)) |
Chris Lattner | 92e803c | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 410 | return NoModRef; |
Nick Lewycky | 5c9be67 | 2009-10-13 07:48:38 +0000 | [diff] [blame] | 411 | } |
Chris Lattner | 92e803c | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 412 | break; |
| 413 | case Intrinsic::lifetime_start: |
| 414 | case Intrinsic::lifetime_end: |
| 415 | case Intrinsic::invariant_start: { |
Gabor Greif | 71339c9 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 416 | unsigned PtrSize = cast<ConstantInt>(II->getArgOperand(0))->getZExtValue(); |
| 417 | if (isNoAlias(II->getArgOperand(1), PtrSize, P, Size)) |
Chris Lattner | 92e803c | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 418 | return NoModRef; |
| 419 | break; |
| 420 | } |
| 421 | case Intrinsic::invariant_end: { |
Gabor Greif | 71339c9 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 422 | unsigned PtrSize = cast<ConstantInt>(II->getArgOperand(1))->getZExtValue(); |
| 423 | if (isNoAlias(II->getArgOperand(2), PtrSize, P, Size)) |
Chris Lattner | 92e803c | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 424 | return NoModRef; |
| 425 | break; |
| 426 | } |
Chris Lattner | defa1c8 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | // The AliasAnalysis base class has some smarts, lets use them. |
| 430 | return AliasAnalysis::getModRefInfo(CS, P, Size); |
| 431 | } |
| 432 | |
| 433 | |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 434 | AliasAnalysis::ModRefResult |
| 435 | BasicAliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) { |
| 436 | // If CS1 or CS2 are readnone, they don't interact. |
| 437 | ModRefBehavior CS1B = AliasAnalysis::getModRefBehavior(CS1); |
| 438 | if (CS1B == DoesNotAccessMemory) return NoModRef; |
| 439 | |
| 440 | ModRefBehavior CS2B = AliasAnalysis::getModRefBehavior(CS2); |
| 441 | if (CS2B == DoesNotAccessMemory) return NoModRef; |
| 442 | |
| 443 | // If they both only read from memory, just return ref. |
| 444 | if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory) |
| 445 | return Ref; |
| 446 | |
| 447 | // Otherwise, fall back to NoAA (mod+ref). |
| 448 | return NoAA::getModRefInfo(CS1, CS2); |
| 449 | } |
| 450 | |
Chris Lattner | d84eb91 | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 451 | /// GetIndiceDifference - Dest and Src are the variable indices from two |
| 452 | /// decomposed GetElementPtr instructions GEP1 and GEP2 which have common base |
| 453 | /// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic |
| 454 | /// difference between the two pointers. |
| 455 | static void GetIndiceDifference( |
| 456 | SmallVectorImpl<std::pair<const Value*, int64_t> > &Dest, |
| 457 | const SmallVectorImpl<std::pair<const Value*, int64_t> > &Src) { |
| 458 | if (Src.empty()) return; |
| 459 | |
| 460 | for (unsigned i = 0, e = Src.size(); i != e; ++i) { |
| 461 | const Value *V = Src[i].first; |
| 462 | int64_t Scale = Src[i].second; |
| 463 | |
| 464 | // Find V in Dest. This is N^2, but pointer indices almost never have more |
| 465 | // than a few variable indexes. |
| 466 | for (unsigned j = 0, e = Dest.size(); j != e; ++j) { |
| 467 | if (Dest[j].first != V) continue; |
| 468 | |
| 469 | // If we found it, subtract off Scale V's from the entry in Dest. If it |
| 470 | // goes to zero, remove the entry. |
| 471 | if (Dest[j].second != Scale) |
| 472 | Dest[j].second -= Scale; |
| 473 | else |
| 474 | Dest.erase(Dest.begin()+j); |
| 475 | Scale = 0; |
| 476 | break; |
| 477 | } |
| 478 | |
| 479 | // If we didn't consume this entry, add it to the end of the Dest list. |
| 480 | if (Scale) |
| 481 | Dest.push_back(std::make_pair(V, -Scale)); |
| 482 | } |
| 483 | } |
Chris Lattner | 4e91ee7 | 2009-11-26 02:13:03 +0000 | [diff] [blame] | 484 | |
Chris Lattner | 539c9b9 | 2009-11-26 02:11:08 +0000 | [diff] [blame] | 485 | /// aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP instruction |
| 486 | /// against another pointer. We know that V1 is a GEP, but we don't know |
Chris Lattner | 23e2a5b | 2009-11-26 02:14:59 +0000 | [diff] [blame] | 487 | /// anything about V2. UnderlyingV1 is GEP1->getUnderlyingObject(), |
| 488 | /// UnderlyingV2 is the same for V2. |
Chris Lattner | 539c9b9 | 2009-11-26 02:11:08 +0000 | [diff] [blame] | 489 | /// |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 490 | AliasAnalysis::AliasResult |
Chris Lattner | 539c9b9 | 2009-11-26 02:11:08 +0000 | [diff] [blame] | 491 | BasicAliasAnalysis::aliasGEP(const GEPOperator *GEP1, unsigned V1Size, |
Chris Lattner | 23e2a5b | 2009-11-26 02:14:59 +0000 | [diff] [blame] | 492 | const Value *V2, unsigned V2Size, |
| 493 | const Value *UnderlyingV1, |
| 494 | const Value *UnderlyingV2) { |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 495 | // If this GEP has been visited before, we're on a use-def cycle. |
| 496 | // Such cycles are only valid when PHI nodes are involved or in unreachable |
| 497 | // code. The visitPHI function catches cycles containing PHIs, but there |
| 498 | // could still be a cycle without PHIs in unreachable code. |
| 499 | if (!Visited.insert(GEP1)) |
| 500 | return MayAlias; |
| 501 | |
Chris Lattner | d84eb91 | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 502 | int64_t GEP1BaseOffset; |
| 503 | SmallVector<std::pair<const Value*, int64_t>, 4> GEP1VariableIndices; |
| 504 | |
Chris Lattner | b307c88 | 2003-12-11 22:44:13 +0000 | [diff] [blame] | 505 | // If we have two gep instructions with must-alias'ing base pointers, figure |
| 506 | // out if the indexes to the GEP tell us anything about the derived pointer. |
Chris Lattner | 539c9b9 | 2009-11-26 02:11:08 +0000 | [diff] [blame] | 507 | if (const GEPOperator *GEP2 = dyn_cast<GEPOperator>(V2)) { |
Chris Lattner | b307c88 | 2003-12-11 22:44:13 +0000 | [diff] [blame] | 508 | // Do the base pointers alias? |
Chris Lattner | d84eb91 | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 509 | AliasResult BaseAlias = aliasCheck(UnderlyingV1, ~0U, UnderlyingV2, ~0U); |
| 510 | |
| 511 | // If we get a No or May, then return it immediately, no amount of analysis |
| 512 | // will improve this situation. |
| 513 | if (BaseAlias != MustAlias) return BaseAlias; |
| 514 | |
| 515 | // Otherwise, we have a MustAlias. Since the base pointers alias each other |
| 516 | // exactly, see if the computed offset from the common pointer tells us |
| 517 | // about the relation of the resulting pointer. |
| 518 | const Value *GEP1BasePtr = |
| 519 | DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD); |
| 520 | |
| 521 | int64_t GEP2BaseOffset; |
| 522 | SmallVector<std::pair<const Value*, int64_t>, 4> GEP2VariableIndices; |
| 523 | const Value *GEP2BasePtr = |
| 524 | DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices, TD); |
| 525 | |
| 526 | // If DecomposeGEPExpression isn't able to look all the way through the |
| 527 | // addressing operation, we must not have TD and this is too complex for us |
| 528 | // to handle without it. |
| 529 | if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) { |
| 530 | assert(TD == 0 && |
| 531 | "DecomposeGEPExpression and getUnderlyingObject disagree!"); |
| 532 | return MayAlias; |
| 533 | } |
| 534 | |
| 535 | // Subtract the GEP2 pointer from the GEP1 pointer to find out their |
| 536 | // symbolic difference. |
| 537 | GEP1BaseOffset -= GEP2BaseOffset; |
| 538 | GetIndiceDifference(GEP1VariableIndices, GEP2VariableIndices); |
| 539 | |
| 540 | } else { |
| 541 | // Check to see if these two pointers are related by the getelementptr |
| 542 | // instruction. If one pointer is a GEP with a non-zero index of the other |
| 543 | // pointer, we know they cannot alias. |
Chris Lattner | 5369250 | 2009-11-26 16:52:32 +0000 | [diff] [blame] | 544 | |
| 545 | // If both accesses are unknown size, we can't do anything useful here. |
| 546 | if (V1Size == ~0U && V2Size == ~0U) |
Chris Lattner | d84eb91 | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 547 | return MayAlias; |
Chris Lattner | b307c88 | 2003-12-11 22:44:13 +0000 | [diff] [blame] | 548 | |
Chris Lattner | d84eb91 | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 549 | AliasResult R = aliasCheck(UnderlyingV1, ~0U, V2, V2Size); |
| 550 | if (R != MustAlias) |
| 551 | // If V2 may alias GEP base pointer, conservatively returns MayAlias. |
| 552 | // If V2 is known not to alias GEP base pointer, then the two values |
| 553 | // cannot alias per GEP semantics: "A pointer value formed from a |
| 554 | // getelementptr instruction is associated with the addresses associated |
| 555 | // with the first operand of the getelementptr". |
| 556 | return R; |
Chris Lattner | b307c88 | 2003-12-11 22:44:13 +0000 | [diff] [blame] | 557 | |
Chris Lattner | d84eb91 | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 558 | const Value *GEP1BasePtr = |
| 559 | DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD); |
| 560 | |
| 561 | // If DecomposeGEPExpression isn't able to look all the way through the |
| 562 | // addressing operation, we must not have TD and this is too complex for us |
| 563 | // to handle without it. |
| 564 | if (GEP1BasePtr != UnderlyingV1) { |
| 565 | assert(TD == 0 && |
| 566 | "DecomposeGEPExpression and getUnderlyingObject disagree!"); |
| 567 | return MayAlias; |
Chris Lattner | b307c88 | 2003-12-11 22:44:13 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
Chris Lattner | d84eb91 | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 570 | |
| 571 | // In the two GEP Case, if there is no difference in the offsets of the |
| 572 | // computed pointers, the resultant pointers are a must alias. This |
| 573 | // hapens when we have two lexically identical GEP's (for example). |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 574 | // |
Chris Lattner | d84eb91 | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 575 | // In the other case, if we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2 |
| 576 | // must aliases the GEP, the end result is a must alias also. |
| 577 | if (GEP1BaseOffset == 0 && GEP1VariableIndices.empty()) |
Evan Cheng | 681a33e | 2009-10-14 06:41:49 +0000 | [diff] [blame] | 578 | return MustAlias; |
Evan Cheng | 094f04b | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 579 | |
Chris Lattner | 4e91ee7 | 2009-11-26 02:13:03 +0000 | [diff] [blame] | 580 | // If we have a known constant offset, see if this offset is larger than the |
| 581 | // access size being queried. If so, and if no variable indices can remove |
| 582 | // pieces of this constant, then we know we have a no-alias. For example, |
| 583 | // &A[100] != &A. |
| 584 | |
| 585 | // In order to handle cases like &A[100][i] where i is an out of range |
| 586 | // subscript, we have to ignore all constant offset pieces that are a multiple |
| 587 | // of a scaled index. Do this by removing constant offsets that are a |
| 588 | // multiple of any of our variable indices. This allows us to transform |
| 589 | // things like &A[i][1] because i has a stride of (e.g.) 8 bytes but the 1 |
| 590 | // provides an offset of 4 bytes (assuming a <= 4 byte access). |
Chris Lattner | d84eb91 | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 591 | for (unsigned i = 0, e = GEP1VariableIndices.size(); |
| 592 | i != e && GEP1BaseOffset;++i) |
| 593 | if (int64_t RemovedOffset = GEP1BaseOffset/GEP1VariableIndices[i].second) |
| 594 | GEP1BaseOffset -= RemovedOffset*GEP1VariableIndices[i].second; |
Chris Lattner | 4e91ee7 | 2009-11-26 02:13:03 +0000 | [diff] [blame] | 595 | |
| 596 | // If our known offset is bigger than the access size, we know we don't have |
| 597 | // an alias. |
| 598 | if (GEP1BaseOffset) { |
| 599 | if (GEP1BaseOffset >= (int64_t)V2Size || |
| 600 | GEP1BaseOffset <= -(int64_t)V1Size) |
Evan Cheng | 681a33e | 2009-10-14 06:41:49 +0000 | [diff] [blame] | 601 | return NoAlias; |
Evan Cheng | 094f04b | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 602 | } |
Chris Lattner | 4e91ee7 | 2009-11-26 02:13:03 +0000 | [diff] [blame] | 603 | |
Evan Cheng | 094f04b | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 604 | return MayAlias; |
| 605 | } |
| 606 | |
Chris Lattner | 5d56b2d | 2009-11-23 16:45:27 +0000 | [diff] [blame] | 607 | /// aliasSelect - Provide a bunch of ad-hoc rules to disambiguate a Select |
| 608 | /// instruction against another. |
Dan Gohman | 6665b0e | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 609 | AliasAnalysis::AliasResult |
| 610 | BasicAliasAnalysis::aliasSelect(const SelectInst *SI, unsigned SISize, |
| 611 | const Value *V2, unsigned V2Size) { |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 612 | // If this select has been visited before, we're on a use-def cycle. |
| 613 | // Such cycles are only valid when PHI nodes are involved or in unreachable |
| 614 | // code. The visitPHI function catches cycles containing PHIs, but there |
| 615 | // could still be a cycle without PHIs in unreachable code. |
| 616 | if (!Visited.insert(SI)) |
| 617 | return MayAlias; |
| 618 | |
Dan Gohman | 6665b0e | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 619 | // If the values are Selects with the same condition, we can do a more precise |
| 620 | // check: just check for aliases between the values on corresponding arms. |
| 621 | if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) |
| 622 | if (SI->getCondition() == SI2->getCondition()) { |
| 623 | AliasResult Alias = |
| 624 | aliasCheck(SI->getTrueValue(), SISize, |
| 625 | SI2->getTrueValue(), V2Size); |
| 626 | if (Alias == MayAlias) |
| 627 | return MayAlias; |
| 628 | AliasResult ThisAlias = |
| 629 | aliasCheck(SI->getFalseValue(), SISize, |
| 630 | SI2->getFalseValue(), V2Size); |
| 631 | if (ThisAlias != Alias) |
| 632 | return MayAlias; |
| 633 | return Alias; |
| 634 | } |
| 635 | |
| 636 | // If both arms of the Select node NoAlias or MustAlias V2, then returns |
| 637 | // NoAlias / MustAlias. Otherwise, returns MayAlias. |
| 638 | AliasResult Alias = |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 639 | aliasCheck(V2, V2Size, SI->getTrueValue(), SISize); |
Dan Gohman | 6665b0e | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 640 | if (Alias == MayAlias) |
| 641 | return MayAlias; |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 642 | |
| 643 | // If V2 is visited, the recursive case will have been caught in the |
| 644 | // above aliasCheck call, so these subsequent calls to aliasCheck |
| 645 | // don't need to assume that V2 is being visited recursively. |
| 646 | Visited.erase(V2); |
| 647 | |
Dan Gohman | 6665b0e | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 648 | AliasResult ThisAlias = |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 649 | aliasCheck(V2, V2Size, SI->getFalseValue(), SISize); |
Dan Gohman | 6665b0e | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 650 | if (ThisAlias != Alias) |
| 651 | return MayAlias; |
| 652 | return Alias; |
| 653 | } |
| 654 | |
Evan Cheng | d83c2ca | 2009-10-14 05:22:03 +0000 | [diff] [blame] | 655 | // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI instruction |
Evan Cheng | 3dbe43b | 2009-10-14 05:05:02 +0000 | [diff] [blame] | 656 | // against another. |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 657 | AliasAnalysis::AliasResult |
Evan Cheng | d83c2ca | 2009-10-14 05:22:03 +0000 | [diff] [blame] | 658 | BasicAliasAnalysis::aliasPHI(const PHINode *PN, unsigned PNSize, |
Evan Cheng | 3dbe43b | 2009-10-14 05:05:02 +0000 | [diff] [blame] | 659 | const Value *V2, unsigned V2Size) { |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 660 | // The PHI node has already been visited, avoid recursion any further. |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 661 | if (!Visited.insert(PN)) |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 662 | return MayAlias; |
| 663 | |
Dan Gohman | 6665b0e | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 664 | // If the values are PHIs in the same block, we can do a more precise |
| 665 | // as well as efficient check: just check for aliases between the values |
| 666 | // on corresponding edges. |
| 667 | if (const PHINode *PN2 = dyn_cast<PHINode>(V2)) |
| 668 | if (PN2->getParent() == PN->getParent()) { |
| 669 | AliasResult Alias = |
| 670 | aliasCheck(PN->getIncomingValue(0), PNSize, |
| 671 | PN2->getIncomingValueForBlock(PN->getIncomingBlock(0)), |
| 672 | V2Size); |
| 673 | if (Alias == MayAlias) |
| 674 | return MayAlias; |
| 675 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 676 | AliasResult ThisAlias = |
| 677 | aliasCheck(PN->getIncomingValue(i), PNSize, |
| 678 | PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)), |
| 679 | V2Size); |
| 680 | if (ThisAlias != Alias) |
| 681 | return MayAlias; |
| 682 | } |
| 683 | return Alias; |
| 684 | } |
| 685 | |
Evan Cheng | a846a8a | 2009-10-16 00:33:09 +0000 | [diff] [blame] | 686 | SmallPtrSet<Value*, 4> UniqueSrc; |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 687 | SmallVector<Value*, 4> V1Srcs; |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 688 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 689 | Value *PV1 = PN->getIncomingValue(i); |
| 690 | if (isa<PHINode>(PV1)) |
| 691 | // If any of the source itself is a PHI, return MayAlias conservatively |
Evan Cheng | 681a33e | 2009-10-14 06:41:49 +0000 | [diff] [blame] | 692 | // to avoid compile time explosion. The worst possible case is if both |
| 693 | // sides are PHI nodes. In which case, this is O(m x n) time where 'm' |
| 694 | // and 'n' are the number of PHI sources. |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 695 | return MayAlias; |
| 696 | if (UniqueSrc.insert(PV1)) |
| 697 | V1Srcs.push_back(PV1); |
| 698 | } |
| 699 | |
Dan Gohman | 6665b0e | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 700 | AliasResult Alias = aliasCheck(V2, V2Size, V1Srcs[0], PNSize); |
Evan Cheng | d83c2ca | 2009-10-14 05:22:03 +0000 | [diff] [blame] | 701 | // Early exit if the check of the first PHI source against V2 is MayAlias. |
| 702 | // Other results are not possible. |
| 703 | if (Alias == MayAlias) |
| 704 | return MayAlias; |
| 705 | |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 706 | // If all sources of the PHI node NoAlias or MustAlias V2, then returns |
| 707 | // NoAlias / MustAlias. Otherwise, returns MayAlias. |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 708 | for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) { |
| 709 | Value *V = V1Srcs[i]; |
Dan Gohman | 6665b0e | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 710 | |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 711 | // If V2 is visited, the recursive case will have been caught in the |
Dan Gohman | 6665b0e | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 712 | // above aliasCheck call, so these subsequent calls to aliasCheck |
| 713 | // don't need to assume that V2 is being visited recursively. |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 714 | Visited.erase(V2); |
Dan Gohman | 6665b0e | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 715 | |
Evan Cheng | a846a8a | 2009-10-16 00:33:09 +0000 | [diff] [blame] | 716 | AliasResult ThisAlias = aliasCheck(V2, V2Size, V, PNSize); |
Evan Cheng | d83c2ca | 2009-10-14 05:22:03 +0000 | [diff] [blame] | 717 | if (ThisAlias != Alias || ThisAlias == MayAlias) |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 718 | return MayAlias; |
| 719 | } |
| 720 | |
| 721 | return Alias; |
| 722 | } |
| 723 | |
| 724 | // aliasCheck - Provide a bunch of ad-hoc rules to disambiguate in common cases, |
| 725 | // such as array references. |
Evan Cheng | 094f04b | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 726 | // |
| 727 | AliasAnalysis::AliasResult |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 728 | BasicAliasAnalysis::aliasCheck(const Value *V1, unsigned V1Size, |
Evan Cheng | 3dbe43b | 2009-10-14 05:05:02 +0000 | [diff] [blame] | 729 | const Value *V2, unsigned V2Size) { |
Dan Gohman | b57b6f1 | 2010-04-08 18:11:50 +0000 | [diff] [blame] | 730 | // If either of the memory references is empty, it doesn't matter what the |
| 731 | // pointer values are. |
| 732 | if (V1Size == 0 || V2Size == 0) |
| 733 | return NoAlias; |
| 734 | |
Evan Cheng | 094f04b | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 735 | // Strip off any casts if they exist. |
| 736 | V1 = V1->stripPointerCasts(); |
| 737 | V2 = V2->stripPointerCasts(); |
| 738 | |
| 739 | // Are we checking for alias of the same value? |
| 740 | if (V1 == V2) return MustAlias; |
| 741 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 742 | if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy()) |
Evan Cheng | 094f04b | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 743 | return NoAlias; // Scalars cannot alias each other |
| 744 | |
| 745 | // Figure out what objects these things are pointing to if we can. |
| 746 | const Value *O1 = V1->getUnderlyingObject(); |
| 747 | const Value *O2 = V2->getUnderlyingObject(); |
| 748 | |
Dan Gohman | f75ef66 | 2009-11-09 19:29:11 +0000 | [diff] [blame] | 749 | // Null values in the default address space don't point to any object, so they |
| 750 | // don't alias any other pointer. |
| 751 | if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1)) |
| 752 | if (CPN->getType()->getAddressSpace() == 0) |
| 753 | return NoAlias; |
| 754 | if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2)) |
| 755 | if (CPN->getType()->getAddressSpace() == 0) |
| 756 | return NoAlias; |
| 757 | |
Evan Cheng | 094f04b | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 758 | if (O1 != O2) { |
| 759 | // If V1/V2 point to two different objects we know that we have no alias. |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 760 | if (isIdentifiedObject(O1, Interprocedural) && |
| 761 | isIdentifiedObject(O2, Interprocedural)) |
Evan Cheng | 094f04b | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 762 | return NoAlias; |
Nick Lewycky | 20162ac | 2009-11-14 06:15:14 +0000 | [diff] [blame] | 763 | |
| 764 | // Constant pointers can't alias with non-const isIdentifiedObject objects. |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 765 | if ((isa<Constant>(O1) && |
| 766 | isIdentifiedObject(O2, Interprocedural) && |
| 767 | !isa<Constant>(O2)) || |
| 768 | (isa<Constant>(O2) && |
| 769 | isIdentifiedObject(O1, Interprocedural) && |
| 770 | !isa<Constant>(O1))) |
Nick Lewycky | 20162ac | 2009-11-14 06:15:14 +0000 | [diff] [blame] | 771 | return NoAlias; |
| 772 | |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 773 | // Arguments can't alias with local allocations or noalias calls, unless |
| 774 | // we have to consider interprocedural aliasing. |
| 775 | if (!Interprocedural) |
| 776 | if ((isa<Argument>(O1) && (isa<AllocaInst>(O2) || isNoAliasCall(O2))) || |
| 777 | (isa<Argument>(O2) && (isa<AllocaInst>(O1) || isNoAliasCall(O1)))) |
| 778 | return NoAlias; |
Evan Cheng | 094f04b | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 779 | |
| 780 | // Most objects can't alias null. |
| 781 | if ((isa<ConstantPointerNull>(V2) && isKnownNonNull(O1)) || |
| 782 | (isa<ConstantPointerNull>(V1) && isKnownNonNull(O2))) |
| 783 | return NoAlias; |
| 784 | } |
| 785 | |
| 786 | // If the size of one access is larger than the entire object on the other |
| 787 | // side, then we know such behavior is undefined and can assume no alias. |
Evan Cheng | 094f04b | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 788 | if (TD) |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 789 | if ((V1Size != ~0U && isObjectSmallerThan(O2, V1Size, *TD)) || |
| 790 | (V2Size != ~0U && isObjectSmallerThan(O1, V2Size, *TD))) |
Evan Cheng | 094f04b | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 791 | return NoAlias; |
| 792 | |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 793 | // If one pointer is the result of a call/invoke or load and the other is a |
Evan Cheng | 094f04b | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 794 | // non-escaping local object, then we know the object couldn't escape to a |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 795 | // point where the call could return it. |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 796 | if (O1 != O2) { |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 797 | if (isEscapeSource(O1, Interprocedural) && |
| 798 | isNonEscapingLocalObject(O2, Interprocedural)) |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 799 | return NoAlias; |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 800 | if (isEscapeSource(O2, Interprocedural) && |
| 801 | isNonEscapingLocalObject(O1, Interprocedural)) |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 802 | return NoAlias; |
| 803 | } |
Evan Cheng | 094f04b | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 804 | |
Chris Lattner | 4e91ee7 | 2009-11-26 02:13:03 +0000 | [diff] [blame] | 805 | // FIXME: This isn't aggressively handling alias(GEP, PHI) for example: if the |
| 806 | // GEP can't simplify, we don't even look at the PHI cases. |
Chris Lattner | 391d23b | 2009-10-17 23:48:54 +0000 | [diff] [blame] | 807 | if (!isa<GEPOperator>(V1) && isa<GEPOperator>(V2)) { |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 808 | std::swap(V1, V2); |
| 809 | std::swap(V1Size, V2Size); |
Chris Lattner | 23e2a5b | 2009-11-26 02:14:59 +0000 | [diff] [blame] | 810 | std::swap(O1, O2); |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 811 | } |
Chris Lattner | 539c9b9 | 2009-11-26 02:11:08 +0000 | [diff] [blame] | 812 | if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1)) |
Chris Lattner | 23e2a5b | 2009-11-26 02:14:59 +0000 | [diff] [blame] | 813 | return aliasGEP(GV1, V1Size, V2, V2Size, O1, O2); |
Evan Cheng | 50a5914 | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 814 | |
| 815 | if (isa<PHINode>(V2) && !isa<PHINode>(V1)) { |
| 816 | std::swap(V1, V2); |
| 817 | std::swap(V1Size, V2Size); |
| 818 | } |
Evan Cheng | d83c2ca | 2009-10-14 05:22:03 +0000 | [diff] [blame] | 819 | if (const PHINode *PN = dyn_cast<PHINode>(V1)) |
| 820 | return aliasPHI(PN, V1Size, V2, V2Size); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 821 | |
Dan Gohman | 6665b0e | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 822 | if (isa<SelectInst>(V2) && !isa<SelectInst>(V1)) { |
| 823 | std::swap(V1, V2); |
| 824 | std::swap(V1Size, V2Size); |
| 825 | } |
| 826 | if (const SelectInst *S1 = dyn_cast<SelectInst>(V1)) |
| 827 | return aliasSelect(S1, V1Size, V2, V2Size); |
| 828 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 829 | return MayAlias; |
| 830 | } |
| 831 | |
Chris Lattner | 5d56b2d | 2009-11-23 16:45:27 +0000 | [diff] [blame] | 832 | // Make sure that anything that uses AliasAnalysis pulls in this file. |
Reid Spencer | 4f1bd9e | 2006-06-07 22:00:26 +0000 | [diff] [blame] | 833 | DEFINING_FILE_FOR(BasicAliasAnalysis) |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 834 | |
| 835 | //===----------------------------------------------------------------------===// |
| 836 | // InterproceduralBasicAliasAnalysis Pass |
| 837 | //===----------------------------------------------------------------------===// |
| 838 | |
| 839 | namespace { |
| 840 | /// InterproceduralBasicAliasAnalysis - This is similar to basicaa, except |
| 841 | /// that it properly supports queries to values which live in different |
| 842 | /// functions. |
| 843 | /// |
| 844 | /// Note that we don't currently take this to the extreme, analyzing all |
| 845 | /// call sites of a function to answer a query about an Argument. |
| 846 | /// |
| 847 | struct InterproceduralBasicAliasAnalysis : public BasicAliasAnalysis { |
| 848 | static char ID; // Class identification, replacement for typeinfo |
| 849 | InterproceduralBasicAliasAnalysis() : BasicAliasAnalysis(&ID, true) {} |
| 850 | }; |
| 851 | } |
| 852 | |
| 853 | // Register this pass... |
| 854 | char InterproceduralBasicAliasAnalysis::ID = 0; |
| 855 | static RegisterPass<InterproceduralBasicAliasAnalysis> |
| 856 | W("interprocedural-basic-aa", "Interprocedural Basic Alias Analysis", false, true); |
| 857 | |
| 858 | // Declare that we implement the AliasAnalysis interface |
| 859 | static RegisterAnalysisGroup<AliasAnalysis> Z(W); |
| 860 | |
| 861 | ImmutablePass *llvm::createInterproceduralBasicAliasAnalysisPass() { |
| 862 | return new InterproceduralBasicAliasAnalysis(); |
| 863 | } |