blob: f15c05153e1062ca68f7e77aa92cd5bd7ec2b464 [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"
26#include <set>
27using namespace llvm;
28
29namespace {
30
Nick Lewycky6726b6d2009-10-25 06:33:48 +000031 class AliasDebugger : public ModulePass, public AliasAnalysis {
Andrew Lenharth472c7912006-11-14 05:21:04 +000032
33 //What we do is simple. Keep track of every value the AA could
34 //know about, and verify that queries are one of those.
35 //A query to a value that didn't exist when the AA was created
36 //means someone forgot to update the AA when creating new values
37
38 std::set<const Value*> Vals;
39
40 public:
Devang Patel19974732007-05-03 01:11:54 +000041 static char ID; // Class identification, replacement for typeinfo
Owen Anderson081c34b2010-10-19 17:21:58 +000042 AliasDebugger() : ModulePass(ID) {
43 initializeAliasDebuggerPass(*PassRegistry::getPassRegistry());
44 }
Devang Patel1cee94f2008-03-18 00:39:19 +000045
Andrew Lenharth472c7912006-11-14 05:21:04 +000046 bool runOnModule(Module &M) {
47 InitializeAliasAnalysis(this); // set up super class
48
49 for(Module::global_iterator I = M.global_begin(),
Dan Gohmandd054662010-05-28 22:31:51 +000050 E = M.global_end(); I != E; ++I) {
Andrew Lenharth472c7912006-11-14 05:21:04 +000051 Vals.insert(&*I);
Dan Gohmandd054662010-05-28 22:31:51 +000052 for (User::const_op_iterator OI = I->op_begin(),
53 OE = I->op_end(); OI != OE; ++OI)
54 Vals.insert(*OI);
55 }
Andrew Lenharth472c7912006-11-14 05:21:04 +000056
57 for(Module::iterator I = M.begin(),
58 E = M.end(); I != E; ++I){
59 Vals.insert(&*I);
Reid Spencer5cbf9852007-01-30 20:08:39 +000060 if(!I->isDeclaration()) {
Andrew Lenharth472c7912006-11-14 05:21:04 +000061 for (Function::arg_iterator AI = I->arg_begin(), AE = I->arg_end();
62 AI != AE; ++AI)
63 Vals.insert(&*AI);
64 for (Function::const_iterator FI = I->begin(), FE = I->end();
65 FI != FE; ++FI)
66 for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end();
Dan Gohmandd054662010-05-28 22:31:51 +000067 BI != BE; ++BI) {
Andrew Lenharth472c7912006-11-14 05:21:04 +000068 Vals.insert(&*BI);
Dan Gohmandd054662010-05-28 22:31:51 +000069 for (User::const_op_iterator OI = BI->op_begin(),
70 OE = BI->op_end(); OI != OE; ++OI)
71 Vals.insert(*OI);
72 }
Andrew Lenharth472c7912006-11-14 05:21:04 +000073 }
74
75 }
76 return false;
77 }
78
79 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
80 AliasAnalysis::getAnalysisUsage(AU);
81 AU.setPreservesAll(); // Does not transform code
82 }
83
Chris Lattner1bc76d42010-01-20 20:09:02 +000084 /// getAdjustedAnalysisPointer - This method is used when a pass implements
85 /// an analysis interface through multiple inheritance. If needed, it
86 /// should override this to adjust the this pointer as needed for the
87 /// specified pass info.
Owen Anderson90c579d2010-08-06 18:33:48 +000088 virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
89 if (PI == &AliasAnalysis::ID)
Chris Lattner1bc76d42010-01-20 20:09:02 +000090 return (AliasAnalysis*)this;
91 return this;
92 }
93
Andrew Lenharth472c7912006-11-14 05:21:04 +000094 //------------------------------------------------
95 // Implement the AliasAnalysis API
96 //
Dan Gohmanb2143b62010-09-14 21:25:10 +000097 AliasResult alias(const Location &LocA, const Location &LocB) {
98 assert(Vals.find(LocA.Ptr) != Vals.end() &&
99 "Never seen value in AA before");
100 assert(Vals.find(LocB.Ptr) != Vals.end() &&
101 "Never seen value in AA before");
102 return AliasAnalysis::alias(LocA, LocB);
Andrew Lenharth472c7912006-11-14 05:21:04 +0000103 }
104
Dan Gohman79fca6f2010-08-03 21:48:53 +0000105 ModRefResult getModRefInfo(ImmutableCallSite CS,
Dan Gohmanb2143b62010-09-14 21:25:10 +0000106 const Location &Loc) {
107 assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
108 return AliasAnalysis::getModRefInfo(CS, Loc);
Andrew Lenharth472c7912006-11-14 05:21:04 +0000109 }
110
Dan Gohman79fca6f2010-08-03 21:48:53 +0000111 ModRefResult getModRefInfo(ImmutableCallSite CS1,
112 ImmutableCallSite CS2) {
Andrew Lenharth472c7912006-11-14 05:21:04 +0000113 return AliasAnalysis::getModRefInfo(CS1,CS2);
114 }
115
Dan Gohmana25e5db2010-11-08 16:45:26 +0000116 bool pointsToConstantMemory(const Location &Loc, bool OrLocal) {
Dan Gohmanb2143b62010-09-14 21:25:10 +0000117 assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
Dan Gohmana25e5db2010-11-08 16:45:26 +0000118 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
Andrew Lenharth472c7912006-11-14 05:21:04 +0000119 }
120
Andrew Lenharth472c7912006-11-14 05:21:04 +0000121 virtual void deleteValue(Value *V) {
122 assert(Vals.find(V) != Vals.end() && "Never seen value in AA before");
123 AliasAnalysis::deleteValue(V);
124 }
125 virtual void copyValue(Value *From, Value *To) {
126 Vals.insert(To);
127 AliasAnalysis::copyValue(From, To);
128 }
129
130 };
Andrew Lenharth472c7912006-11-14 05:21:04 +0000131}
132
Dan Gohman844731a2008-05-13 00:00:25 +0000133char AliasDebugger::ID = 0;
Owen Andersond8cc7be2010-07-21 23:07:00 +0000134INITIALIZE_AG_PASS(AliasDebugger, AliasAnalysis, "debug-aa",
Owen Andersonce665bd2010-10-07 22:25:06 +0000135 "AA use debugger", false, true, false)
Dan Gohman844731a2008-05-13 00:00:25 +0000136
Andrew Lenharth472c7912006-11-14 05:21:04 +0000137Pass *llvm::createAliasDebugger() { return new AliasDebugger(); }
138