1 //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
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,
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
25 //===----------------------------------------------------------------------===//
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/BasicBlock.h"
29 #include "llvm/iMemory.h"
30 #include "llvm/Target/TargetData.h"
33 // Register the AliasAnalysis interface, providing a nice name to refer to.
35 RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
38 //===----------------------------------------------------------------------===//
39 // Default chaining methods
40 //===----------------------------------------------------------------------===//
42 AliasAnalysis::AliasResult
43 AliasAnalysis::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);
49 void 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);
54 bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
55 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
56 return AA->pointsToConstantMemory(P);
59 bool AliasAnalysis::doesNotAccessMemory(Function *F) {
60 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
61 return AA->doesNotAccessMemory(F);
64 bool AliasAnalysis::onlyReadsMemory(Function *F) {
65 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
66 return doesNotAccessMemory(F) || AA->onlyReadsMemory(F);
69 bool AliasAnalysis::hasNoModRefInfoForCalls() const {
70 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
71 return AA->hasNoModRefInfoForCalls();
74 void AliasAnalysis::deleteValue(Value *V) {
75 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
79 void AliasAnalysis::copyValue(Value *From, Value *To) {
80 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
81 AA->copyValue(From, To);
84 AliasAnalysis::ModRefResult
85 AliasAnalysis::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);
92 //===----------------------------------------------------------------------===//
93 // AliasAnalysis non-virtual helper method implementation
94 //===----------------------------------------------------------------------===//
96 AliasAnalysis::ModRefResult
97 AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
98 return alias(L->getOperand(0), TD->getTypeSize(L->getType()),
99 P, Size) ? Ref : NoModRef;
102 AliasAnalysis::ModRefResult
103 AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
104 // 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()),
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;
115 AliasAnalysis::ModRefResult
116 AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
117 ModRefResult Mask = ModRef;
118 if (Function *F = CS.getCalledFunction())
119 if (onlyReadsMemory(F)) {
120 if (doesNotAccessMemory(F)) return NoModRef;
124 if (!AA) return Mask;
126 // If P points to a constant memory location, the call definitely could not
127 // modify the memory location.
128 if ((Mask & Mod) && AA->pointsToConstantMemory(P))
131 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
134 // 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!
139 AliasAnalysis::~AliasAnalysis() {}
141 /// setTargetData - Subclasses must call this method to initialize the
142 /// AliasAnalysis interface before any other methods are called.
144 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
145 TD = &P->getAnalysis<TargetData>();
146 AA = &P->getAnalysis<AliasAnalysis>();
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.
152 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
153 AU.addRequired<TargetData>(); // All AA's need TargetData.
154 AU.addRequired<AliasAnalysis>(); // All AA's chain
157 /// canBasicBlockModify - Return true if it is possible for execution of the
158 /// specified basic block to modify the value pointed to by Ptr.
160 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
161 const Value *Ptr, unsigned Size) {
162 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
165 /// 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.
170 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
171 const Instruction &I2,
172 const Value *Ptr, unsigned Size) {
173 assert(I1.getParent() == I2.getParent() &&
174 "Instructions not in same basic block!");
175 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
176 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
177 ++E; // Convert from inclusive to exclusive range.
179 for (; I != E; ++I) // Check every instruction in range
180 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
185 // 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.
190 extern void llvm::BasicAAStub();
191 static IncludeFile INCLUDE_BASICAA_CPP((void*)&BasicAAStub);