blob: 04ad7de7bd3b9fb4d967ab32279938267fa223d2 [file] [log] [blame]
Chris Lattner53ad0ed2002-08-22 18:25:32 +00001//===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner53ad0ed2002-08-22 18:25:32 +00009//
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
Chris Lattnerd501c132003-02-26 19:41:54 +000027#include "llvm/Analysis/AliasAnalysis.h"
Reid Spencer6df60a92006-06-07 20:00:19 +000028#include "llvm/Pass.h"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000029#include "llvm/BasicBlock.h"
Duncan Sandsdff67102007-12-01 07:51:45 +000030#include "llvm/Function.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000031#include "llvm/Instructions.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000032#include "llvm/Type.h"
Chris Lattner14ac8772003-02-26 19:26:51 +000033#include "llvm/Target/TargetData.h"
Chris Lattner992860c2004-03-15 04:07:29 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattner53ad0ed2002-08-22 18:25:32 +000036// Register the AliasAnalysis interface, providing a nice name to refer to.
Chris Lattnerdc1ad192003-02-03 21:16:17 +000037namespace {
38 RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
Chris Lattnerdc1ad192003-02-03 21:16:17 +000039}
Devang Patel19974732007-05-03 01:11:54 +000040char AliasAnalysis::ID = 0;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000041
Chris Lattner5a24d702004-05-23 21:15:48 +000042//===----------------------------------------------------------------------===//
43// Default chaining methods
44//===----------------------------------------------------------------------===//
45
46AliasAnalysis::AliasResult
47AliasAnalysis::alias(const Value *V1, unsigned V1Size,
48 const Value *V2, unsigned V2Size) {
49 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
50 return AA->alias(V1, V1Size, V2, V2Size);
51}
52
53void AliasAnalysis::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
54 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
55 return AA->getMustAliases(P, RetVals);
56}
57
58bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
59 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
60 return AA->pointsToConstantMemory(P);
61}
62
Chris Lattner0af024c2004-12-15 07:22:13 +000063AliasAnalysis::ModRefBehavior
64AliasAnalysis::getModRefBehavior(Function *F, CallSite CS,
65 std::vector<PointerAccessInfo> *Info) {
Chris Lattner5a24d702004-05-23 21:15:48 +000066 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Chris Lattner0af024c2004-12-15 07:22:13 +000067 return AA->getModRefBehavior(F, CS, Info);
Chris Lattner5a24d702004-05-23 21:15:48 +000068}
69
70bool AliasAnalysis::hasNoModRefInfoForCalls() const {
71 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
72 return AA->hasNoModRefInfoForCalls();
73}
74
75void AliasAnalysis::deleteValue(Value *V) {
76 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
77 AA->deleteValue(V);
78}
79
80void AliasAnalysis::copyValue(Value *From, Value *To) {
81 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
82 AA->copyValue(From, To);
83}
84
85AliasAnalysis::ModRefResult
86AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
87 // FIXME: we can do better.
88 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
89 return AA->getModRefInfo(CS1, CS2);
90}
91
92
93//===----------------------------------------------------------------------===//
94// AliasAnalysis non-virtual helper method implementation
95//===----------------------------------------------------------------------===//
96
Chris Lattner14ac8772003-02-26 19:26:51 +000097AliasAnalysis::ModRefResult
98AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
Duncan Sands514ab342007-11-01 20:53:16 +000099 return alias(L->getOperand(0), TD->getTypeStoreSize(L->getType()),
Chris Lattner14ac8772003-02-26 19:26:51 +0000100 P, Size) ? Ref : NoModRef;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000101}
102
Chris Lattner14ac8772003-02-26 19:26:51 +0000103AliasAnalysis::ModRefResult
104AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000105 // If the stored address cannot alias the pointer in question, then the
106 // pointer cannot be modified by the store.
Duncan Sands514ab342007-11-01 20:53:16 +0000107 if (!alias(S->getOperand(1),
108 TD->getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000109 return NoModRef;
110
111 // If the pointer is a pointer to constant memory, then it could not have been
112 // modified by this store.
113 return pointsToConstantMemory(P) ? NoModRef : Mod;
Chris Lattner14ac8772003-02-26 19:26:51 +0000114}
115
Duncan Sandsdff67102007-12-01 07:51:45 +0000116AliasAnalysis::ModRefBehavior
117AliasAnalysis::getModRefBehavior(CallSite CS,
118 std::vector<PointerAccessInfo> *Info) {
Duncan Sands7915cbe2007-12-12 16:01:40 +0000119 if (CS.doesNotAccessMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000120 // Can't do better than this.
121 return DoesNotAccessMemory;
122 ModRefBehavior MRB = UnknownModRefBehavior;
123 if (Function *F = CS.getCalledFunction())
124 MRB = getModRefBehavior(F, CS, Info);
Duncan Sands7915cbe2007-12-12 16:01:40 +0000125 if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000126 return OnlyReadsMemory;
127 return MRB;
128}
129
130AliasAnalysis::ModRefBehavior
131AliasAnalysis::getModRefBehavior(Function *F,
132 std::vector<PointerAccessInfo> *Info) {
Duncan Sands7915cbe2007-12-12 16:01:40 +0000133 if (F->doesNotAccessMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000134 // Can't do better than this.
135 return DoesNotAccessMemory;
136 ModRefBehavior MRB = getModRefBehavior(F, CallSite(), Info);
Duncan Sands7915cbe2007-12-12 16:01:40 +0000137 if (MRB != DoesNotAccessMemory && F->onlyReadsMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000138 return OnlyReadsMemory;
139 return MRB;
140}
141
Chris Lattner992860c2004-03-15 04:07:29 +0000142AliasAnalysis::ModRefResult
143AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Chris Lattner5a24d702004-05-23 21:15:48 +0000144 ModRefResult Mask = ModRef;
Duncan Sandsdff67102007-12-01 07:51:45 +0000145 ModRefBehavior MRB = getModRefBehavior(CS);
146 if (MRB == OnlyReadsMemory)
147 Mask = Ref;
148 else if (MRB == DoesNotAccessMemory)
149 return NoModRef;
Chris Lattner992860c2004-03-15 04:07:29 +0000150
Chris Lattner5a24d702004-05-23 21:15:48 +0000151 if (!AA) return Mask;
152
Chris Lattner992860c2004-03-15 04:07:29 +0000153 // If P points to a constant memory location, the call definitely could not
154 // modify the memory location.
Chris Lattner5a24d702004-05-23 21:15:48 +0000155 if ((Mask & Mod) && AA->pointsToConstantMemory(P))
Chris Lattnerd433bde2005-03-23 22:06:41 +0000156 Mask = ModRefResult(Mask & ~Mod);
Chris Lattner992860c2004-03-15 04:07:29 +0000157
Chris Lattner5a24d702004-05-23 21:15:48 +0000158 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
Chris Lattner992860c2004-03-15 04:07:29 +0000159}
160
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000161// AliasAnalysis destructor: DO NOT move this to the header file for
162// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
163// the AliasAnalysis.o file in the current .a file, causing alias analysis
164// support to not be included in the tool correctly!
165//
166AliasAnalysis::~AliasAnalysis() {}
167
Chris Lattner14ac8772003-02-26 19:26:51 +0000168/// setTargetData - Subclasses must call this method to initialize the
169/// AliasAnalysis interface before any other methods are called.
170///
171void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
172 TD = &P->getAnalysis<TargetData>();
Chris Lattner5a24d702004-05-23 21:15:48 +0000173 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner14ac8772003-02-26 19:26:51 +0000174}
175
176// getAnalysisUsage - All alias analysis implementations should invoke this
177// directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
178// TargetData is required by the pass.
179void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
180 AU.addRequired<TargetData>(); // All AA's need TargetData.
Chris Lattner5a24d702004-05-23 21:15:48 +0000181 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner14ac8772003-02-26 19:26:51 +0000182}
183
Chris Lattnerf9355f62002-08-22 22:46:39 +0000184/// canBasicBlockModify - Return true if it is possible for execution of the
185/// specified basic block to modify the value pointed to by Ptr.
186///
Chris Lattner14ac8772003-02-26 19:26:51 +0000187bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
188 const Value *Ptr, unsigned Size) {
189 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000190}
191
Chris Lattnerf9355f62002-08-22 22:46:39 +0000192/// canInstructionRangeModify - Return true if it is possible for the execution
193/// of the specified instructions to modify the value pointed to by Ptr. The
194/// instructions to consider are all of the instructions in the range of [I1,I2]
195/// INCLUSIVE. I1 and I2 must be in the same basic block.
196///
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000197bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
198 const Instruction &I2,
Chris Lattner14ac8772003-02-26 19:26:51 +0000199 const Value *Ptr, unsigned Size) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000200 assert(I1.getParent() == I2.getParent() &&
201 "Instructions not in same basic block!");
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000202 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
203 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
204 ++E; // Convert from inclusive to exclusive range.
205
Chris Lattner14ac8772003-02-26 19:26:51 +0000206 for (; I != E; ++I) // Check every instruction in range
207 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000208 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000209 return false;
210}
211
Chris Lattnerd501c132003-02-26 19:41:54 +0000212// Because of the way .a files work, we must force the BasicAA implementation to
213// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
214// the risk of AliasAnalysis being used, but the default implementation not
215// being linked into the tool that uses it.
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000216DEFINING_FILE_FOR(AliasAnalysis)