Implement constant folding if vector<->vector bitcasts where the number
[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/Pass.h"
29 #include "llvm/BasicBlock.h"
30 #include "llvm/Function.h"
31 #include "llvm/Instructions.h"
32 #include "llvm/Type.h"
33 #include "llvm/Target/TargetData.h"
34 using namespace llvm;
35
36 // Register the AliasAnalysis interface, providing a nice name to refer to.
37 namespace {
38   RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
39 }
40 char AliasAnalysis::ID = 0;
41
42 //===----------------------------------------------------------------------===//
43 // Default chaining methods
44 //===----------------------------------------------------------------------===//
45
46 AliasAnalysis::AliasResult
47 AliasAnalysis::alias(const Value *V1, unsigned V1Size,
48                      const Value *V2, unsigned V2Size) {
49   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
50   return AA->alias(V1, V1Size, V2, V2Size);
51 }
52
53 void AliasAnalysis::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
54   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
55   return AA->getMustAliases(P, RetVals);
56 }
57
58 bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
59   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
60   return AA->pointsToConstantMemory(P);
61 }
62
63 AliasAnalysis::ModRefBehavior
64 AliasAnalysis::getModRefBehavior(Function *F, CallSite CS,
65                                  std::vector<PointerAccessInfo> *Info) {
66   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
67   return AA->getModRefBehavior(F, CS, Info);
68 }
69
70 bool AliasAnalysis::hasNoModRefInfoForCalls() const {
71   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
72   return AA->hasNoModRefInfoForCalls();
73 }
74
75 void AliasAnalysis::deleteValue(Value *V) {
76   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
77   AA->deleteValue(V);
78 }
79
80 void AliasAnalysis::copyValue(Value *From, Value *To) {
81   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
82   AA->copyValue(From, To);
83 }
84
85 AliasAnalysis::ModRefResult
86 AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
87   // FIXME: we can do better.
88   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
89   return AA->getModRefInfo(CS1, CS2);
90 }
91
92
93 //===----------------------------------------------------------------------===//
94 // AliasAnalysis non-virtual helper method implementation
95 //===----------------------------------------------------------------------===//
96
97 AliasAnalysis::ModRefResult
98 AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
99   return alias(L->getOperand(0), TD->getTypeStoreSize(L->getType()),
100                P, Size) ? Ref : NoModRef;
101 }
102
103 AliasAnalysis::ModRefResult
104 AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
105   // If the stored address cannot alias the pointer in question, then the
106   // pointer cannot be modified by the store.
107   if (!alias(S->getOperand(1),
108              TD->getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
109     return NoModRef;
110
111   // If the pointer is a pointer to constant memory, then it could not have been
112   // modified by this store.
113   return pointsToConstantMemory(P) ? NoModRef : Mod;
114 }
115
116 AliasAnalysis::ModRefBehavior
117 AliasAnalysis::getModRefBehavior(CallSite CS,
118                                  std::vector<PointerAccessInfo> *Info) {
119   if (CS.doesNotAccessMemory() &&
120       // FIXME: workaround gcc bootstrap breakage
121       CS.getCalledFunction() && CS.getCalledFunction()->isDeclaration())
122     // Can't do better than this.
123     return DoesNotAccessMemory;
124   ModRefBehavior MRB = UnknownModRefBehavior;
125   if (Function *F = CS.getCalledFunction())
126     MRB = getModRefBehavior(F, CS, Info);
127   if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory() &&
128       // FIXME: workaround gcc bootstrap breakage
129       CS.getCalledFunction() && CS.getCalledFunction()->isDeclaration())
130     return OnlyReadsMemory;
131   return MRB;
132 }
133
134 AliasAnalysis::ModRefBehavior
135 AliasAnalysis::getModRefBehavior(Function *F,
136                                  std::vector<PointerAccessInfo> *Info) {
137   if (F->doesNotAccessMemory() &&
138       // FIXME: workaround gcc bootstrap breakage
139       F->isDeclaration())
140     // Can't do better than this.
141     return DoesNotAccessMemory;
142   ModRefBehavior MRB = getModRefBehavior(F, CallSite(), Info);
143   if (MRB != DoesNotAccessMemory && F->onlyReadsMemory() &&
144       // FIXME: workaround gcc bootstrap breakage
145       F->isDeclaration())
146     return OnlyReadsMemory;
147   return MRB;
148 }
149
150 AliasAnalysis::ModRefResult
151 AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
152   ModRefResult Mask = ModRef;
153   ModRefBehavior MRB = getModRefBehavior(CS);
154   if (MRB == OnlyReadsMemory)
155     Mask = Ref;
156   else if (MRB == DoesNotAccessMemory)
157     return NoModRef;
158
159   if (!AA) return Mask;
160
161   // If P points to a constant memory location, the call definitely could not
162   // modify the memory location.
163   if ((Mask & Mod) && AA->pointsToConstantMemory(P))
164     Mask = ModRefResult(Mask & ~Mod);
165
166   return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
167 }
168
169 // AliasAnalysis destructor: DO NOT move this to the header file for
170 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
171 // the AliasAnalysis.o file in the current .a file, causing alias analysis
172 // support to not be included in the tool correctly!
173 //
174 AliasAnalysis::~AliasAnalysis() {}
175
176 /// setTargetData - Subclasses must call this method to initialize the
177 /// AliasAnalysis interface before any other methods are called.
178 ///
179 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
180   TD = &P->getAnalysis<TargetData>();
181   AA = &P->getAnalysis<AliasAnalysis>();
182 }
183
184 // getAnalysisUsage - All alias analysis implementations should invoke this
185 // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
186 // TargetData is required by the pass.
187 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
188   AU.addRequired<TargetData>();            // All AA's need TargetData.
189   AU.addRequired<AliasAnalysis>();         // All AA's chain
190 }
191
192 /// canBasicBlockModify - Return true if it is possible for execution of the
193 /// specified basic block to modify the value pointed to by Ptr.
194 ///
195 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
196                                         const Value *Ptr, unsigned Size) {
197   return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
198 }
199
200 /// canInstructionRangeModify - Return true if it is possible for the execution
201 /// of the specified instructions to modify the value pointed to by Ptr.  The
202 /// instructions to consider are all of the instructions in the range of [I1,I2]
203 /// INCLUSIVE.  I1 and I2 must be in the same basic block.
204 ///
205 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
206                                               const Instruction &I2,
207                                               const Value *Ptr, unsigned Size) {
208   assert(I1.getParent() == I2.getParent() &&
209          "Instructions not in same basic block!");
210   BasicBlock::iterator I = const_cast<Instruction*>(&I1);
211   BasicBlock::iterator E = const_cast<Instruction*>(&I2);
212   ++E;  // Convert from inclusive to exclusive range.
213
214   for (; I != E; ++I) // Check every instruction in range
215     if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
216       return true;
217   return false;
218 }
219
220 // Because of the way .a files work, we must force the BasicAA implementation to
221 // be pulled in if the AliasAnalysis classes are pulled in.  Otherwise we run
222 // the risk of AliasAnalysis being used, but the default implementation not
223 // being linked into the tool that uses it.
224 DEFINING_FILE_FOR(AliasAnalysis)