1 //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
3 // This file implements the generic AliasAnalysis interface which is used as the
4 // common interface used by all clients and implementations of alias analysis.
6 // This file also implements the default version of the AliasAnalysis interface
7 // that is to be used when no other implementation is specified. This does some
8 // simple tests that detect obvious cases: two different global pointers cannot
9 // alias, a global cannot alias a malloc, two different mallocs cannot alias,
12 // This alias analysis implementation really isn't very good for anything, but
13 // it is very fast, and makes a nice clean default implementation. Because it
14 // handles lots of little corner cases, other, more complex, alias analysis
15 // implementations may choose to rely on this pass to resolve these simple and
18 //===----------------------------------------------------------------------===//
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/BasicBlock.h"
22 #include "llvm/iMemory.h"
23 #include "llvm/Target/TargetData.h"
25 // Register the AliasAnalysis interface, providing a nice name to refer to.
27 RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
30 AliasAnalysis::ModRefResult
31 AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
32 return alias(L->getOperand(0), TD->getTypeSize(L->getType()),
33 P, Size) ? Ref : NoModRef;
36 AliasAnalysis::ModRefResult
37 AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
38 return alias(S->getOperand(1), TD->getTypeSize(S->getOperand(0)->getType()),
39 P, Size) ? Mod : NoModRef;
43 // AliasAnalysis destructor: DO NOT move this to the header file for
44 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
45 // the AliasAnalysis.o file in the current .a file, causing alias analysis
46 // support to not be included in the tool correctly!
48 AliasAnalysis::~AliasAnalysis() {}
50 /// setTargetData - Subclasses must call this method to initialize the
51 /// AliasAnalysis interface before any other methods are called.
53 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
54 TD = &P->getAnalysis<TargetData>();
57 // getAnalysisUsage - All alias analysis implementations should invoke this
58 // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
59 // TargetData is required by the pass.
60 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
61 AU.addRequired<TargetData>(); // All AA's need TargetData.
64 /// canBasicBlockModify - Return true if it is possible for execution of the
65 /// specified basic block to modify the value pointed to by Ptr.
67 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
68 const Value *Ptr, unsigned Size) {
69 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
72 /// canInstructionRangeModify - Return true if it is possible for the execution
73 /// of the specified instructions to modify the value pointed to by Ptr. The
74 /// instructions to consider are all of the instructions in the range of [I1,I2]
75 /// INCLUSIVE. I1 and I2 must be in the same basic block.
77 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
78 const Instruction &I2,
79 const Value *Ptr, unsigned Size) {
80 assert(I1.getParent() == I2.getParent() &&
81 "Instructions not in same basic block!");
82 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
83 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
84 ++E; // Convert from inclusive to exclusive range.
86 for (; I != E; ++I) // Check every instruction in range
87 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
92 // Because of the way .a files work, we must force the BasicAA implementation to
93 // be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
94 // the risk of AliasAnalysis being used, but the default implementation not
95 // being linked into the tool that uses it.
97 extern void BasicAAStub();
98 static IncludeFile INCLUDE_BASICAA_CPP((void*)&BasicAAStub);
102 struct NoAA : public ImmutablePass, public AliasAnalysis {
103 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
104 AliasAnalysis::getAnalysisUsage(AU);
107 virtual void initializePass() {
108 InitializeAliasAnalysis(this);
112 // Register this pass...
114 X("no-aa", "No Alias Analysis (always returns 'may' alias)");
116 // Declare that we implement the AliasAnalysis interface
117 RegisterAnalysisGroup<AliasAnalysis, NoAA> Y;
118 } // End of anonymous namespace