blob: 193be84b0dbf1fd09ed71e668b3369c92fbf2ce3 [file] [log] [blame]
Chris Lattner53ad0ed2002-08-22 18:25:32 +00001//===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
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"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000028#include "llvm/BasicBlock.h"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000029#include "llvm/iMemory.h"
Chris Lattner14ac8772003-02-26 19:26:51 +000030#include "llvm/Target/TargetData.h"
Chris Lattner992860c2004-03-15 04:07:29 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner53ad0ed2002-08-22 18:25:32 +000033// Register the AliasAnalysis interface, providing a nice name to refer to.
Chris Lattnerdc1ad192003-02-03 21:16:17 +000034namespace {
35 RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
Chris Lattnerdc1ad192003-02-03 21:16:17 +000036}
Chris Lattner53ad0ed2002-08-22 18:25:32 +000037
Chris Lattner5a24d702004-05-23 21:15:48 +000038//===----------------------------------------------------------------------===//
39// Default chaining methods
40//===----------------------------------------------------------------------===//
41
42AliasAnalysis::AliasResult
43AliasAnalysis::alias(const Value *V1, unsigned V1Size,
44 const Value *V2, unsigned V2Size) {
45 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
46 return AA->alias(V1, V1Size, V2, V2Size);
47}
48
49void AliasAnalysis::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
50 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
51 return AA->getMustAliases(P, RetVals);
52}
53
54bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
55 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
56 return AA->pointsToConstantMemory(P);
57}
58
59bool AliasAnalysis::doesNotAccessMemory(Function *F) {
60 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
61 return AA->doesNotAccessMemory(F);
62}
63
64bool AliasAnalysis::onlyReadsMemory(Function *F) {
65 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
66 return doesNotAccessMemory(F) || AA->onlyReadsMemory(F);
67}
68
69bool AliasAnalysis::hasNoModRefInfoForCalls() const {
70 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
71 return AA->hasNoModRefInfoForCalls();
72}
73
74void AliasAnalysis::deleteValue(Value *V) {
75 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
76 AA->deleteValue(V);
77}
78
79void AliasAnalysis::copyValue(Value *From, Value *To) {
80 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
81 AA->copyValue(From, To);
82}
83
84AliasAnalysis::ModRefResult
85AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
86 // FIXME: we can do better.
87 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
88 return AA->getModRefInfo(CS1, CS2);
89}
90
91
92//===----------------------------------------------------------------------===//
93// AliasAnalysis non-virtual helper method implementation
94//===----------------------------------------------------------------------===//
95
Chris Lattner14ac8772003-02-26 19:26:51 +000096AliasAnalysis::ModRefResult
97AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
98 return alias(L->getOperand(0), TD->getTypeSize(L->getType()),
99 P, Size) ? Ref : NoModRef;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000100}
101
Chris Lattner14ac8772003-02-26 19:26:51 +0000102AliasAnalysis::ModRefResult
103AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000104 // If the stored address cannot alias the pointer in question, then the
105 // pointer cannot be modified by the store.
106 if (!alias(S->getOperand(1), TD->getTypeSize(S->getOperand(0)->getType()),
107 P, Size))
108 return NoModRef;
109
110 // If the pointer is a pointer to constant memory, then it could not have been
111 // modified by this store.
112 return pointsToConstantMemory(P) ? NoModRef : Mod;
Chris Lattner14ac8772003-02-26 19:26:51 +0000113}
114
Chris Lattner992860c2004-03-15 04:07:29 +0000115AliasAnalysis::ModRefResult
116AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Chris Lattner5a24d702004-05-23 21:15:48 +0000117 ModRefResult Mask = ModRef;
Chris Lattner992860c2004-03-15 04:07:29 +0000118 if (Function *F = CS.getCalledFunction())
119 if (onlyReadsMemory(F)) {
120 if (doesNotAccessMemory(F)) return NoModRef;
Chris Lattner5a24d702004-05-23 21:15:48 +0000121 Mask = Ref;
Chris Lattner992860c2004-03-15 04:07:29 +0000122 }
123
Chris Lattner5a24d702004-05-23 21:15:48 +0000124 if (!AA) return Mask;
125
Chris Lattner992860c2004-03-15 04:07:29 +0000126 // If P points to a constant memory location, the call definitely could not
127 // modify the memory location.
Chris Lattner5a24d702004-05-23 21:15:48 +0000128 if ((Mask & Mod) && AA->pointsToConstantMemory(P))
129 Mask = Ref;
Chris Lattner992860c2004-03-15 04:07:29 +0000130
Chris Lattner5a24d702004-05-23 21:15:48 +0000131 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
Chris Lattner992860c2004-03-15 04:07:29 +0000132}
133
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000134// AliasAnalysis destructor: DO NOT move this to the header file for
135// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
136// the AliasAnalysis.o file in the current .a file, causing alias analysis
137// support to not be included in the tool correctly!
138//
139AliasAnalysis::~AliasAnalysis() {}
140
Chris Lattner14ac8772003-02-26 19:26:51 +0000141/// setTargetData - Subclasses must call this method to initialize the
142/// AliasAnalysis interface before any other methods are called.
143///
144void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
145 TD = &P->getAnalysis<TargetData>();
Chris Lattner5a24d702004-05-23 21:15:48 +0000146 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner14ac8772003-02-26 19:26:51 +0000147}
148
149// getAnalysisUsage - All alias analysis implementations should invoke this
150// directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
151// TargetData is required by the pass.
152void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
153 AU.addRequired<TargetData>(); // All AA's need TargetData.
Chris Lattner5a24d702004-05-23 21:15:48 +0000154 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner14ac8772003-02-26 19:26:51 +0000155}
156
Chris Lattnerf9355f62002-08-22 22:46:39 +0000157/// canBasicBlockModify - Return true if it is possible for execution of the
158/// specified basic block to modify the value pointed to by Ptr.
159///
Chris Lattner14ac8772003-02-26 19:26:51 +0000160bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
161 const Value *Ptr, unsigned Size) {
162 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000163}
164
Chris Lattnerf9355f62002-08-22 22:46:39 +0000165/// canInstructionRangeModify - Return true if it is possible for the execution
166/// of the specified instructions to modify the value pointed to by Ptr. The
167/// instructions to consider are all of the instructions in the range of [I1,I2]
168/// INCLUSIVE. I1 and I2 must be in the same basic block.
169///
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000170bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
171 const Instruction &I2,
Chris Lattner14ac8772003-02-26 19:26:51 +0000172 const Value *Ptr, unsigned Size) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000173 assert(I1.getParent() == I2.getParent() &&
174 "Instructions not in same basic block!");
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000175 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
176 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
177 ++E; // Convert from inclusive to exclusive range.
178
Chris Lattner14ac8772003-02-26 19:26:51 +0000179 for (; I != E; ++I) // Check every instruction in range
180 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000181 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000182 return false;
183}
184
Chris Lattnerd501c132003-02-26 19:41:54 +0000185// Because of the way .a files work, we must force the BasicAA implementation to
186// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
187// the risk of AliasAnalysis being used, but the default implementation not
188// being linked into the tool that uses it.
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000189//
Chris Lattner992860c2004-03-15 04:07:29 +0000190extern void llvm::BasicAAStub();
Chris Lattnerd501c132003-02-26 19:41:54 +0000191static IncludeFile INCLUDE_BASICAA_CPP((void*)&BasicAAStub);