blob: 6b534e94be17d7ae46695d031d2b5a5f4ada1598 [file] [log] [blame]
Andrew Lenharth472c7912006-11-14 05:21:04 +00001//===- AliasDebugger.cpp - Simple Alias Analysis Use Checker --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Andrew Lenharth472c7912006-11-14 05:21:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This simple pass checks alias analysis users to ensure that if they
11// create a new value, they do not query AA without informing it of the value.
12// It acts as a shim over any other AA pass you want.
13//
14// Yes keeping track of every value in the program is expensive, but this is
15// a debugging pass.
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Analysis/Passes.h"
20#include "llvm/Module.h"
21#include "llvm/Pass.h"
22#include "llvm/Instructions.h"
23#include "llvm/Constants.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Analysis/AliasAnalysis.h"
Reid Spencerd7d83db2007-02-05 23:42:17 +000026#include "llvm/Support/Compiler.h"
Andrew Lenharth472c7912006-11-14 05:21:04 +000027#include <set>
28using namespace llvm;
29
30namespace {
31
Reid Spencerd7d83db2007-02-05 23:42:17 +000032 class VISIBILITY_HIDDEN AliasDebugger
33 : public ModulePass, public AliasAnalysis {
Andrew Lenharth472c7912006-11-14 05:21:04 +000034
35 //What we do is simple. Keep track of every value the AA could
36 //know about, and verify that queries are one of those.
37 //A query to a value that didn't exist when the AA was created
38 //means someone forgot to update the AA when creating new values
39
40 std::set<const Value*> Vals;
41
42 public:
Devang Patel19974732007-05-03 01:11:54 +000043 static char ID; // Class identification, replacement for typeinfo
Devang Patel794fd752007-05-01 21:15:47 +000044 AliasDebugger() : ModulePass((intptr_t)&ID) {}
45
Devang Patel1cee94f2008-03-18 00:39:19 +000046 /// isAnalysis - Return true if this pass is implementing an analysis pass.
47 virtual bool isAnalysis() const { return true; }
48
Andrew Lenharth472c7912006-11-14 05:21:04 +000049 bool runOnModule(Module &M) {
50 InitializeAliasAnalysis(this); // set up super class
51
52 for(Module::global_iterator I = M.global_begin(),
53 E = M.global_end(); I != E; ++I)
54 Vals.insert(&*I);
55
56 for(Module::iterator I = M.begin(),
57 E = M.end(); I != E; ++I){
58 Vals.insert(&*I);
Reid Spencer5cbf9852007-01-30 20:08:39 +000059 if(!I->isDeclaration()) {
Andrew Lenharth472c7912006-11-14 05:21:04 +000060 for (Function::arg_iterator AI = I->arg_begin(), AE = I->arg_end();
61 AI != AE; ++AI)
62 Vals.insert(&*AI);
63 for (Function::const_iterator FI = I->begin(), FE = I->end();
64 FI != FE; ++FI)
65 for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end();
66 BI != BE; ++BI)
67 Vals.insert(&*BI);
68 }
69
70 }
71 return false;
72 }
73
74 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
75 AliasAnalysis::getAnalysisUsage(AU);
76 AU.setPreservesAll(); // Does not transform code
77 }
78
79 //------------------------------------------------
80 // Implement the AliasAnalysis API
81 //
82 AliasResult alias(const Value *V1, unsigned V1Size,
83 const Value *V2, unsigned V2Size) {
84 assert(Vals.find(V1) != Vals.end() && "Never seen value in AA before");
85 assert(Vals.find(V2) != Vals.end() && "Never seen value in AA before");
86 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
87 }
88
89 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
90 assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
91 return AliasAnalysis::getModRefInfo(CS, P, Size);
92 }
93
94 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
95 return AliasAnalysis::getModRefInfo(CS1,CS2);
96 }
97
98 void getMustAliases(Value *P, std::vector<Value*> &RetVals) {
99 assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
100 return AliasAnalysis::getMustAliases(P, RetVals);
101 }
102
103 bool pointsToConstantMemory(const Value *P) {
104 assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
105 return AliasAnalysis::pointsToConstantMemory(P);
106 }
107
108 /// getModRefBehavior - Return the behavior of the specified function if
109 /// called from the specified call site. The call site may be null in which
110 /// case the most generic behavior of this function should be returned.
111 virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
112 std::vector<PointerAccessInfo> *Info) {
113 assert(Vals.find(F) != Vals.end() && "Never seen value in AA before");
114 return AliasAnalysis::getModRefBehavior(F, CS, Info);
115 }
116
117 virtual void deleteValue(Value *V) {
118 assert(Vals.find(V) != Vals.end() && "Never seen value in AA before");
119 AliasAnalysis::deleteValue(V);
120 }
121 virtual void copyValue(Value *From, Value *To) {
122 Vals.insert(To);
123 AliasAnalysis::copyValue(From, To);
124 }
125
126 };
127
Devang Patel19974732007-05-03 01:11:54 +0000128 char AliasDebugger::ID = 0;
Andrew Lenharth472c7912006-11-14 05:21:04 +0000129 RegisterPass<AliasDebugger> X("debug-aa", "AA use debugger");
130 RegisterAnalysisGroup<AliasAnalysis> Y(X);
131}
132
133Pass *llvm::createAliasDebugger() { return new AliasDebugger(); }
134