Added LLVM project notice to the top of every C++ source file.
[oota-llvm.git] / lib / Analysis / AliasAnalysis.cpp
1 //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
2 // 
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 //===----------------------------------------------------------------------===//
9 //
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
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/BasicBlock.h"
29 #include "llvm/iMemory.h"
30 #include "llvm/Target/TargetData.h"
31
32 // Register the AliasAnalysis interface, providing a nice name to refer to.
33 namespace {
34   RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
35 }
36
37 AliasAnalysis::ModRefResult
38 AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
39   return alias(L->getOperand(0), TD->getTypeSize(L->getType()),
40                P, Size) ? Ref : NoModRef;
41 }
42
43 AliasAnalysis::ModRefResult
44 AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
45   return alias(S->getOperand(1), TD->getTypeSize(S->getOperand(0)->getType()),
46                P, Size) ? Mod : NoModRef;
47 }
48
49
50 // AliasAnalysis destructor: DO NOT move this to the header file for
51 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
52 // the AliasAnalysis.o file in the current .a file, causing alias analysis
53 // support to not be included in the tool correctly!
54 //
55 AliasAnalysis::~AliasAnalysis() {}
56
57 /// setTargetData - Subclasses must call this method to initialize the
58 /// AliasAnalysis interface before any other methods are called.
59 ///
60 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
61   TD = &P->getAnalysis<TargetData>();
62 }
63
64 // getAnalysisUsage - All alias analysis implementations should invoke this
65 // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
66 // TargetData is required by the pass.
67 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
68   AU.addRequired<TargetData>();            // All AA's need TargetData.
69 }
70
71 /// canBasicBlockModify - Return true if it is possible for execution of the
72 /// specified basic block to modify the value pointed to by Ptr.
73 ///
74 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
75                                         const Value *Ptr, unsigned Size) {
76   return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
77 }
78
79 /// canInstructionRangeModify - Return true if it is possible for the execution
80 /// of the specified instructions to modify the value pointed to by Ptr.  The
81 /// instructions to consider are all of the instructions in the range of [I1,I2]
82 /// INCLUSIVE.  I1 and I2 must be in the same basic block.
83 ///
84 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
85                                               const Instruction &I2,
86                                               const Value *Ptr, unsigned Size) {
87   assert(I1.getParent() == I2.getParent() &&
88          "Instructions not in same basic block!");
89   BasicBlock::iterator I = const_cast<Instruction*>(&I1);
90   BasicBlock::iterator E = const_cast<Instruction*>(&I2);
91   ++E;  // Convert from inclusive to exclusive range.
92
93   for (; I != E; ++I) // Check every instruction in range
94     if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
95       return true;
96   return false;
97 }
98
99 // Because of the way .a files work, we must force the BasicAA implementation to
100 // be pulled in if the AliasAnalysis classes are pulled in.  Otherwise we run
101 // the risk of AliasAnalysis being used, but the default implementation not
102 // being linked into the tool that uses it.
103 //
104 extern void BasicAAStub();
105 static IncludeFile INCLUDE_BASICAA_CPP((void*)&BasicAAStub);
106
107
108 namespace {
109   struct NoAA : public ImmutablePass, public AliasAnalysis {
110     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
111       AliasAnalysis::getAnalysisUsage(AU);
112     }
113     
114     virtual void initializePass() {
115       InitializeAliasAnalysis(this);
116     }
117   };
118  
119   // Register this pass...
120   RegisterOpt<NoAA>
121   X("no-aa", "No Alias Analysis (always returns 'may' alias)");
122
123   // Declare that we implement the AliasAnalysis interface
124   RegisterAnalysisGroup<AliasAnalysis, NoAA> Y;
125 }  // End of anonymous namespace