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