blob: c456990d8ae2ce58aa4635f2a179e690ba073709 [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
52void AliasAnalysis::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
53 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
54 return AA->getMustAliases(P, RetVals);
55}
56
57bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
58 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
59 return AA->pointsToConstantMemory(P);
60}
61
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062bool AliasAnalysis::hasNoModRefInfoForCalls() const {
63 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
64 return AA->hasNoModRefInfoForCalls();
65}
66
67void AliasAnalysis::deleteValue(Value *V) {
68 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
69 AA->deleteValue(V);
70}
71
72void AliasAnalysis::copyValue(Value *From, Value *To) {
73 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
74 AA->copyValue(From, To);
75}
76
77AliasAnalysis::ModRefResult
78AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
79 // FIXME: we can do better.
80 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
81 return AA->getModRefInfo(CS1, CS2);
82}
83
84
85//===----------------------------------------------------------------------===//
86// AliasAnalysis non-virtual helper method implementation
87//===----------------------------------------------------------------------===//
88
89AliasAnalysis::ModRefResult
90AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
Dan Gohman624f9612009-07-25 00:48:42 +000091 return alias(L->getOperand(0), getTypeStoreSize(L->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 P, Size) ? Ref : NoModRef;
93}
94
95AliasAnalysis::ModRefResult
96AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
97 // If the stored address cannot alias the pointer in question, then the
98 // pointer cannot be modified by the store.
Duncan Sandsf99fdc62007-11-01 20:53:16 +000099 if (!alias(S->getOperand(1),
Dan Gohman624f9612009-07-25 00:48:42 +0000100 getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 return NoModRef;
102
103 // If the pointer is a pointer to constant memory, then it could not have been
104 // modified by this store.
105 return pointsToConstantMemory(P) ? NoModRef : Mod;
106}
107
Duncan Sands00b24b52007-12-01 07:51:45 +0000108AliasAnalysis::ModRefBehavior
109AliasAnalysis::getModRefBehavior(CallSite CS,
110 std::vector<PointerAccessInfo> *Info) {
Duncan Sands6c48de42007-12-12 16:01:40 +0000111 if (CS.doesNotAccessMemory())
Duncan Sands00b24b52007-12-01 07:51:45 +0000112 // Can't do better than this.
113 return DoesNotAccessMemory;
Owen Andersonab465642009-02-05 23:36:27 +0000114 ModRefBehavior MRB = getModRefBehavior(CS.getCalledFunction(), Info);
Duncan Sands6c48de42007-12-12 16:01:40 +0000115 if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory())
Duncan Sands00b24b52007-12-01 07:51:45 +0000116 return OnlyReadsMemory;
117 return MRB;
118}
119
120AliasAnalysis::ModRefBehavior
121AliasAnalysis::getModRefBehavior(Function *F,
122 std::vector<PointerAccessInfo> *Info) {
Duncan Sands038b4e72009-02-13 17:32:26 +0000123 if (F) {
124 if (F->doesNotAccessMemory())
125 // Can't do better than this.
126 return DoesNotAccessMemory;
Duncan Sands555ddcf2009-02-14 10:56:35 +0000127 if (F->onlyReadsMemory())
Duncan Sands038b4e72009-02-13 17:32:26 +0000128 return OnlyReadsMemory;
Duncan Sands555ddcf2009-02-14 10:56:35 +0000129 if (unsigned id = F->getIntrinsicID()) {
130#define GET_INTRINSIC_MODREF_BEHAVIOR
131#include "llvm/Intrinsics.gen"
132#undef GET_INTRINSIC_MODREF_BEHAVIOR
133 }
Duncan Sands038b4e72009-02-13 17:32:26 +0000134 }
Owen Andersonab465642009-02-05 23:36:27 +0000135 return UnknownModRefBehavior;
Duncan Sands00b24b52007-12-01 07:51:45 +0000136}
137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138AliasAnalysis::ModRefResult
139AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
140 ModRefResult Mask = ModRef;
Duncan Sands00b24b52007-12-01 07:51:45 +0000141 ModRefBehavior MRB = getModRefBehavior(CS);
Duncan Sands038b4e72009-02-13 17:32:26 +0000142 if (MRB == DoesNotAccessMemory)
Duncan Sands00b24b52007-12-01 07:51:45 +0000143 return NoModRef;
Duncan Sands038b4e72009-02-13 17:32:26 +0000144 else if (MRB == OnlyReadsMemory)
145 Mask = Ref;
Owen Andersonab465642009-02-05 23:36:27 +0000146 else if (MRB == AliasAnalysis::AccessesArguments) {
147 bool doesAlias = false;
148 for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
149 AI != AE; ++AI)
150 if (alias(*AI, ~0U, P, Size) != NoAlias) {
151 doesAlias = true;
152 break;
153 }
Duncan Sands038b4e72009-02-13 17:32:26 +0000154
Owen Andersonab465642009-02-05 23:36:27 +0000155 if (!doesAlias)
156 return NoModRef;
157 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158
159 if (!AA) return Mask;
160
161 // If P points to a constant memory location, the call definitely could not
162 // modify the memory location.
163 if ((Mask & Mod) && AA->pointsToConstantMemory(P))
164 Mask = ModRefResult(Mask & ~Mod);
165
166 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
167}
168
169// AliasAnalysis destructor: DO NOT move this to the header file for
170// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
171// the AliasAnalysis.o file in the current .a file, causing alias analysis
172// support to not be included in the tool correctly!
173//
174AliasAnalysis::~AliasAnalysis() {}
175
Dan Gohman5284bb22008-05-30 00:02:02 +0000176/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177/// AliasAnalysis interface before any other methods are called.
178///
179void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
Dan Gohman624f9612009-07-25 00:48:42 +0000180 TD = P->getAnalysisIfAvailable<TargetData>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 AA = &P->getAnalysis<AliasAnalysis>();
182}
183
184// getAnalysisUsage - All alias analysis implementations should invoke this
Dan Gohman624f9612009-07-25 00:48:42 +0000185// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 AU.addRequired<AliasAnalysis>(); // All AA's chain
188}
189
Dan Gohman624f9612009-07-25 00:48:42 +0000190/// getTypeStoreSize - Return the TargetData store size for the given type,
191/// if known, or a conservative value otherwise.
192///
193unsigned AliasAnalysis::getTypeStoreSize(const Type *Ty) {
194 return TD ? TD->getTypeStoreSize(Ty) : ~0u;
195}
196
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197/// canBasicBlockModify - Return true if it is possible for execution of the
198/// specified basic block to modify the value pointed to by Ptr.
199///
200bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
201 const Value *Ptr, unsigned Size) {
202 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
203}
204
205/// canInstructionRangeModify - Return true if it is possible for the execution
206/// of the specified instructions to modify the value pointed to by Ptr. The
207/// instructions to consider are all of the instructions in the range of [I1,I2]
208/// INCLUSIVE. I1 and I2 must be in the same basic block.
209///
210bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
211 const Instruction &I2,
212 const Value *Ptr, unsigned Size) {
213 assert(I1.getParent() == I2.getParent() &&
214 "Instructions not in same basic block!");
215 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
216 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
217 ++E; // Convert from inclusive to exclusive range.
218
219 for (; I != E; ++I) // Check every instruction in range
220 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
221 return true;
222 return false;
223}
224
Dan Gohmand4b0e322009-02-03 01:28:32 +0000225/// isNoAliasCall - Return true if this pointer is returned by a noalias
226/// function.
227bool llvm::isNoAliasCall(const Value *V) {
228 if (isa<CallInst>(V) || isa<InvokeInst>(V))
229 return CallSite(const_cast<Instruction*>(cast<Instruction>(V)))
230 .paramHasAttr(0, Attribute::NoAlias);
231 return false;
232}
233
234/// isIdentifiedObject - Return true if this pointer refers to a distinct and
235/// identifiable object. This returns true for:
Dan Gohman3c845512009-08-27 17:52:56 +0000236/// Global Variables and Functions (but not Global Aliases)
Dan Gohmand4b0e322009-02-03 01:28:32 +0000237/// Allocas and Mallocs
238/// ByVal and NoAlias Arguments
239/// NoAlias returns
240///
241bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman3c845512009-08-27 17:52:56 +0000242 if (isa<AllocationInst>(V) || isNoAliasCall(V))
243 return true;
244 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmand4b0e322009-02-03 01:28:32 +0000245 return true;
246 if (const Argument *A = dyn_cast<Argument>(V))
247 return A->hasNoAliasAttr() || A->hasByValAttr();
248 return false;
249}
250
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251// Because of the way .a files work, we must force the BasicAA implementation to
252// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
253// the risk of AliasAnalysis being used, but the default implementation not
254// being linked into the tool that uses it.
255DEFINING_FILE_FOR(AliasAnalysis)