blob: 43c86a5dc9e96a2d82d4c5eed202d9b230190e47 [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"
Misha Brukman47b14a42004-07-29 17:30:56 +000029#include "llvm/Instructions.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000030#include "llvm/Type.h"
Chris Lattner14ac8772003-02-26 19:26:51 +000031#include "llvm/Target/TargetData.h"
Reid Spencer954da372004-07-04 12:19:56 +000032#include <iostream>
Chris Lattner992860c2004-03-15 04:07:29 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner53ad0ed2002-08-22 18:25:32 +000035// Register the AliasAnalysis interface, providing a nice name to refer to.
Chris Lattnerdc1ad192003-02-03 21:16:17 +000036namespace {
37 RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
Chris Lattnerdc1ad192003-02-03 21:16:17 +000038}
Chris Lattner53ad0ed2002-08-22 18:25:32 +000039
Chris Lattner5a24d702004-05-23 21:15:48 +000040//===----------------------------------------------------------------------===//
41// Default chaining methods
42//===----------------------------------------------------------------------===//
43
44AliasAnalysis::AliasResult
45AliasAnalysis::alias(const Value *V1, unsigned V1Size,
46 const Value *V2, unsigned V2Size) {
47 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
48 return AA->alias(V1, V1Size, V2, V2Size);
49}
50
51void AliasAnalysis::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
52 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
53 return AA->getMustAliases(P, RetVals);
54}
55
56bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
57 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
58 return AA->pointsToConstantMemory(P);
59}
60
Chris Lattner0af024c2004-12-15 07:22:13 +000061AliasAnalysis::ModRefBehavior
62AliasAnalysis::getModRefBehavior(Function *F, CallSite CS,
63 std::vector<PointerAccessInfo> *Info) {
Chris Lattner5a24d702004-05-23 21:15:48 +000064 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Chris Lattner0af024c2004-12-15 07:22:13 +000065 return AA->getModRefBehavior(F, CS, Info);
Chris Lattner5a24d702004-05-23 21:15:48 +000066}
67
68bool AliasAnalysis::hasNoModRefInfoForCalls() const {
69 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
70 return AA->hasNoModRefInfoForCalls();
71}
72
73void AliasAnalysis::deleteValue(Value *V) {
74 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
75 AA->deleteValue(V);
76}
77
78void AliasAnalysis::copyValue(Value *From, Value *To) {
79 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
80 AA->copyValue(From, To);
81}
82
83AliasAnalysis::ModRefResult
84AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
85 // FIXME: we can do better.
86 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
87 return AA->getModRefInfo(CS1, CS2);
88}
89
90
91//===----------------------------------------------------------------------===//
92// AliasAnalysis non-virtual helper method implementation
93//===----------------------------------------------------------------------===//
94
Chris Lattner14ac8772003-02-26 19:26:51 +000095AliasAnalysis::ModRefResult
96AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
97 return alias(L->getOperand(0), TD->getTypeSize(L->getType()),
98 P, Size) ? Ref : NoModRef;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000099}
100
Chris Lattner14ac8772003-02-26 19:26:51 +0000101AliasAnalysis::ModRefResult
102AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000103 // If the stored address cannot alias the pointer in question, then the
104 // pointer cannot be modified by the store.
105 if (!alias(S->getOperand(1), TD->getTypeSize(S->getOperand(0)->getType()),
106 P, Size))
107 return NoModRef;
108
109 // If the pointer is a pointer to constant memory, then it could not have been
110 // modified by this store.
111 return pointsToConstantMemory(P) ? NoModRef : Mod;
Chris Lattner14ac8772003-02-26 19:26:51 +0000112}
113
Chris Lattner992860c2004-03-15 04:07:29 +0000114AliasAnalysis::ModRefResult
115AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Chris Lattner5a24d702004-05-23 21:15:48 +0000116 ModRefResult Mask = ModRef;
Chris Lattner992860c2004-03-15 04:07:29 +0000117 if (Function *F = CS.getCalledFunction())
118 if (onlyReadsMemory(F)) {
119 if (doesNotAccessMemory(F)) return NoModRef;
Chris Lattner5a24d702004-05-23 21:15:48 +0000120 Mask = Ref;
Chris Lattner992860c2004-03-15 04:07:29 +0000121 }
122
Chris Lattner5a24d702004-05-23 21:15:48 +0000123 if (!AA) return Mask;
124
Chris Lattner992860c2004-03-15 04:07:29 +0000125 // If P points to a constant memory location, the call definitely could not
126 // modify the memory location.
Chris Lattner5a24d702004-05-23 21:15:48 +0000127 if ((Mask & Mod) && AA->pointsToConstantMemory(P))
128 Mask = Ref;
Chris Lattner992860c2004-03-15 04:07:29 +0000129
Chris Lattner5a24d702004-05-23 21:15:48 +0000130 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
Chris Lattner992860c2004-03-15 04:07:29 +0000131}
132
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000133// AliasAnalysis destructor: DO NOT move this to the header file for
134// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
135// the AliasAnalysis.o file in the current .a file, causing alias analysis
136// support to not be included in the tool correctly!
137//
138AliasAnalysis::~AliasAnalysis() {}
139
Chris Lattner14ac8772003-02-26 19:26:51 +0000140/// setTargetData - Subclasses must call this method to initialize the
141/// AliasAnalysis interface before any other methods are called.
142///
143void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
144 TD = &P->getAnalysis<TargetData>();
Chris Lattner5a24d702004-05-23 21:15:48 +0000145 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner14ac8772003-02-26 19:26:51 +0000146}
147
148// getAnalysisUsage - All alias analysis implementations should invoke this
149// directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
150// TargetData is required by the pass.
151void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
152 AU.addRequired<TargetData>(); // All AA's need TargetData.
Chris Lattner5a24d702004-05-23 21:15:48 +0000153 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner14ac8772003-02-26 19:26:51 +0000154}
155
Chris Lattnerf9355f62002-08-22 22:46:39 +0000156/// canBasicBlockModify - Return true if it is possible for execution of the
157/// specified basic block to modify the value pointed to by Ptr.
158///
Chris Lattner14ac8772003-02-26 19:26:51 +0000159bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
160 const Value *Ptr, unsigned Size) {
161 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000162}
163
Chris Lattnerf9355f62002-08-22 22:46:39 +0000164/// canInstructionRangeModify - Return true if it is possible for the execution
165/// of the specified instructions to modify the value pointed to by Ptr. The
166/// instructions to consider are all of the instructions in the range of [I1,I2]
167/// INCLUSIVE. I1 and I2 must be in the same basic block.
168///
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000169bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
170 const Instruction &I2,
Chris Lattner14ac8772003-02-26 19:26:51 +0000171 const Value *Ptr, unsigned Size) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000172 assert(I1.getParent() == I2.getParent() &&
173 "Instructions not in same basic block!");
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000174 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
175 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
176 ++E; // Convert from inclusive to exclusive range.
177
Chris Lattner14ac8772003-02-26 19:26:51 +0000178 for (; I != E; ++I) // Check every instruction in range
179 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000180 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000181 return false;
182}
183
Chris Lattnerd501c132003-02-26 19:41:54 +0000184// Because of the way .a files work, we must force the BasicAA implementation to
185// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
186// the risk of AliasAnalysis being used, but the default implementation not
187// being linked into the tool that uses it.
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000188//
Chris Lattner2c20ef52004-12-08 21:00:59 +0000189namespace llvm {
190 extern void BasicAAStub();
191}
Chris Lattnerd501c132003-02-26 19:41:54 +0000192static IncludeFile INCLUDE_BASICAA_CPP((void*)&BasicAAStub);