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