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