blob: 503fbbdab8d67d73e5694f4a9b1ac64d2bedc461 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the generic AliasAnalysis interface which is used as the
11// common interface used by all clients and implementations of alias analysis.
12//
13// This file also implements the default version of the AliasAnalysis interface
14// that is to be used when no other implementation is specified. This does some
15// simple tests that detect obvious cases: two different global pointers cannot
16// alias, a global cannot alias a malloc, two different mallocs cannot alias,
17// etc.
18//
19// This alias analysis implementation really isn't very good for anything, but
20// it is very fast, and makes a nice clean default implementation. Because it
21// handles lots of little corner cases, other, more complex, alias analysis
22// implementations may choose to rely on this pass to resolve these simple and
23// easy cases.
24//
25//===----------------------------------------------------------------------===//
26
27#include "llvm/Analysis/AliasAnalysis.h"
28#include "llvm/Pass.h"
29#include "llvm/BasicBlock.h"
Duncan Sands00b24b52007-12-01 07:51:45 +000030#include "llvm/Function.h"
Owen Andersonfbb585b2009-02-03 06:27:22 +000031#include "llvm/IntrinsicInst.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Instructions.h"
33#include "llvm/Type.h"
34#include "llvm/Target/TargetData.h"
35using namespace llvm;
36
37// Register the AliasAnalysis interface, providing a nice name to refer to.
Dan Gohman089efff2008-05-13 00:00:25 +000038static RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039char AliasAnalysis::ID = 0;
40
41//===----------------------------------------------------------------------===//
42// Default chaining methods
43//===----------------------------------------------------------------------===//
44
45AliasAnalysis::AliasResult
46AliasAnalysis::alias(const Value *V1, unsigned V1Size,
47 const Value *V2, unsigned V2Size) {
48 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
49 return AA->alias(V1, V1Size, V2, V2Size);
50}
51
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
53 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
54 return AA->pointsToConstantMemory(P);
55}
56
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057void AliasAnalysis::deleteValue(Value *V) {
58 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
59 AA->deleteValue(V);
60}
61
62void AliasAnalysis::copyValue(Value *From, Value *To) {
63 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
64 AA->copyValue(From, To);
65}
66
67AliasAnalysis::ModRefResult
68AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
69 // FIXME: we can do better.
70 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
71 return AA->getModRefInfo(CS1, CS2);
72}
73
74
75//===----------------------------------------------------------------------===//
76// AliasAnalysis non-virtual helper method implementation
77//===----------------------------------------------------------------------===//
78
79AliasAnalysis::ModRefResult
80AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
Dan Gohman624f9612009-07-25 00:48:42 +000081 return alias(L->getOperand(0), getTypeStoreSize(L->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 P, Size) ? Ref : NoModRef;
83}
84
85AliasAnalysis::ModRefResult
86AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
87 // If the stored address cannot alias the pointer in question, then the
88 // pointer cannot be modified by the store.
Duncan Sandsf99fdc62007-11-01 20:53:16 +000089 if (!alias(S->getOperand(1),
Dan Gohman624f9612009-07-25 00:48:42 +000090 getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 return NoModRef;
92
93 // If the pointer is a pointer to constant memory, then it could not have been
94 // modified by this store.
95 return pointsToConstantMemory(P) ? NoModRef : Mod;
96}
97
Duncan Sands00b24b52007-12-01 07:51:45 +000098AliasAnalysis::ModRefBehavior
99AliasAnalysis::getModRefBehavior(CallSite CS,
100 std::vector<PointerAccessInfo> *Info) {
Duncan Sands6c48de42007-12-12 16:01:40 +0000101 if (CS.doesNotAccessMemory())
Duncan Sands00b24b52007-12-01 07:51:45 +0000102 // Can't do better than this.
103 return DoesNotAccessMemory;
Owen Andersonab465642009-02-05 23:36:27 +0000104 ModRefBehavior MRB = getModRefBehavior(CS.getCalledFunction(), Info);
Duncan Sands6c48de42007-12-12 16:01:40 +0000105 if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory())
Duncan Sands00b24b52007-12-01 07:51:45 +0000106 return OnlyReadsMemory;
107 return MRB;
108}
109
110AliasAnalysis::ModRefBehavior
111AliasAnalysis::getModRefBehavior(Function *F,
112 std::vector<PointerAccessInfo> *Info) {
Duncan Sands038b4e72009-02-13 17:32:26 +0000113 if (F) {
114 if (F->doesNotAccessMemory())
115 // Can't do better than this.
116 return DoesNotAccessMemory;
Duncan Sands555ddcf2009-02-14 10:56:35 +0000117 if (F->onlyReadsMemory())
Duncan Sands038b4e72009-02-13 17:32:26 +0000118 return OnlyReadsMemory;
Duncan Sandsf68032c2010-01-06 08:45:52 +0000119 if (unsigned id = F->getIntrinsicID())
120 return getModRefBehavior(id);
121 }
122 return UnknownModRefBehavior;
123}
124
125AliasAnalysis::ModRefBehavior AliasAnalysis::getModRefBehavior(unsigned iid) {
Duncan Sands555ddcf2009-02-14 10:56:35 +0000126#define GET_INTRINSIC_MODREF_BEHAVIOR
127#include "llvm/Intrinsics.gen"
128#undef GET_INTRINSIC_MODREF_BEHAVIOR
Duncan Sands00b24b52007-12-01 07:51:45 +0000129}
130
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131AliasAnalysis::ModRefResult
132AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Duncan Sands00b24b52007-12-01 07:51:45 +0000133 ModRefBehavior MRB = getModRefBehavior(CS);
Duncan Sands038b4e72009-02-13 17:32:26 +0000134 if (MRB == DoesNotAccessMemory)
Duncan Sands00b24b52007-12-01 07:51:45 +0000135 return NoModRef;
Chris Lattner913c8ad2009-11-23 16:46:41 +0000136
137 ModRefResult Mask = ModRef;
138 if (MRB == OnlyReadsMemory)
Duncan Sands038b4e72009-02-13 17:32:26 +0000139 Mask = Ref;
Owen Andersonab465642009-02-05 23:36:27 +0000140 else if (MRB == AliasAnalysis::AccessesArguments) {
141 bool doesAlias = false;
142 for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
143 AI != AE; ++AI)
Chris Lattner913c8ad2009-11-23 16:46:41 +0000144 if (!isNoAlias(*AI, ~0U, P, Size)) {
Owen Andersonab465642009-02-05 23:36:27 +0000145 doesAlias = true;
146 break;
147 }
Duncan Sands038b4e72009-02-13 17:32:26 +0000148
Owen Andersonab465642009-02-05 23:36:27 +0000149 if (!doesAlias)
150 return NoModRef;
151 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152
153 if (!AA) return Mask;
154
155 // If P points to a constant memory location, the call definitely could not
156 // modify the memory location.
157 if ((Mask & Mod) && AA->pointsToConstantMemory(P))
158 Mask = ModRefResult(Mask & ~Mod);
159
160 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
161}
162
163// AliasAnalysis destructor: DO NOT move this to the header file for
164// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
165// the AliasAnalysis.o file in the current .a file, causing alias analysis
166// support to not be included in the tool correctly!
167//
168AliasAnalysis::~AliasAnalysis() {}
169
Dan Gohman5284bb22008-05-30 00:02:02 +0000170/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171/// AliasAnalysis interface before any other methods are called.
172///
173void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
Dan Gohman624f9612009-07-25 00:48:42 +0000174 TD = P->getAnalysisIfAvailable<TargetData>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 AA = &P->getAnalysis<AliasAnalysis>();
176}
177
178// getAnalysisUsage - All alias analysis implementations should invoke this
Dan Gohman624f9612009-07-25 00:48:42 +0000179// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 AU.addRequired<AliasAnalysis>(); // All AA's chain
182}
183
Dan Gohman624f9612009-07-25 00:48:42 +0000184/// getTypeStoreSize - Return the TargetData store size for the given type,
185/// if known, or a conservative value otherwise.
186///
187unsigned AliasAnalysis::getTypeStoreSize(const Type *Ty) {
188 return TD ? TD->getTypeStoreSize(Ty) : ~0u;
189}
190
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191/// canBasicBlockModify - Return true if it is possible for execution of the
192/// specified basic block to modify the value pointed to by Ptr.
193///
194bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
195 const Value *Ptr, unsigned Size) {
196 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
197}
198
199/// canInstructionRangeModify - Return true if it is possible for the execution
200/// of the specified instructions to modify the value pointed to by Ptr. The
201/// instructions to consider are all of the instructions in the range of [I1,I2]
202/// INCLUSIVE. I1 and I2 must be in the same basic block.
203///
204bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
205 const Instruction &I2,
206 const Value *Ptr, unsigned Size) {
207 assert(I1.getParent() == I2.getParent() &&
208 "Instructions not in same basic block!");
209 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
210 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
211 ++E; // Convert from inclusive to exclusive range.
212
213 for (; I != E; ++I) // Check every instruction in range
214 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
215 return true;
216 return false;
217}
218
Dan Gohmand4b0e322009-02-03 01:28:32 +0000219/// isNoAliasCall - Return true if this pointer is returned by a noalias
220/// function.
221bool llvm::isNoAliasCall(const Value *V) {
222 if (isa<CallInst>(V) || isa<InvokeInst>(V))
223 return CallSite(const_cast<Instruction*>(cast<Instruction>(V)))
224 .paramHasAttr(0, Attribute::NoAlias);
225 return false;
226}
227
228/// isIdentifiedObject - Return true if this pointer refers to a distinct and
229/// identifiable object. This returns true for:
Dan Gohman3c845512009-08-27 17:52:56 +0000230/// Global Variables and Functions (but not Global Aliases)
Dan Gohmand4b0e322009-02-03 01:28:32 +0000231/// Allocas and Mallocs
Dan Gohman83a01ad22010-07-07 14:27:09 +0000232/// ByVal and NoAlias Arguments
233/// NoAlias returns
Dan Gohmand4b0e322009-02-03 01:28:32 +0000234///
Dan Gohman83a01ad22010-07-07 14:27:09 +0000235bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohmanf9e8abc2010-06-29 00:50:39 +0000236 if (isa<AllocaInst>(V))
Dan Gohman3c845512009-08-27 17:52:56 +0000237 return true;
238 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmand4b0e322009-02-03 01:28:32 +0000239 return true;
Dan Gohman83a01ad22010-07-07 14:27:09 +0000240 if (isNoAliasCall(V))
241 return true;
242 if (const Argument *A = dyn_cast<Argument>(V))
243 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmand4b0e322009-02-03 01:28:32 +0000244 return false;
245}
246
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247// Because of the way .a files work, we must force the BasicAA implementation to
248// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
249// the risk of AliasAnalysis being used, but the default implementation not
250// being linked into the tool that uses it.
251DEFINING_FILE_FOR(AliasAnalysis)