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