1 //===- AliasDebugger.cpp - Simple Alias Analysis Use Checker --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 // Yes keeping track of every value in the program is expensive, but this is
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Analysis/Passes.h"
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Pass.h"
31 class AliasDebugger : public ModulePass, public AliasAnalysis {
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
38 std::set<const Value*> Vals;
41 static char ID; // Class identification, replacement for typeinfo
42 AliasDebugger() : ModulePass(ID) {
43 initializeAliasDebuggerPass(*PassRegistry::getPassRegistry());
46 bool runOnModule(Module &M) override {
47 InitializeAliasAnalysis(this); // set up super class
49 for(Module::global_iterator I = M.global_begin(),
50 E = M.global_end(); I != E; ++I) {
52 for (User::const_op_iterator OI = I->op_begin(),
53 OE = I->op_end(); OI != OE; ++OI)
57 for(Module::iterator I = M.begin(),
58 E = M.end(); I != E; ++I){
60 if(!I->isDeclaration()) {
61 for (Function::arg_iterator AI = I->arg_begin(), AE = I->arg_end();
64 for (Function::const_iterator FI = I->begin(), FE = I->end();
66 for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end();
69 for (User::const_op_iterator OI = BI->op_begin(),
70 OE = BI->op_end(); OI != OE; ++OI)
79 void getAnalysisUsage(AnalysisUsage &AU) const override {
80 AliasAnalysis::getAnalysisUsage(AU);
81 AU.setPreservesAll(); // Does not transform code
84 /// 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.
88 void *getAdjustedAnalysisPointer(AnalysisID PI) override {
89 if (PI == &AliasAnalysis::ID)
90 return (AliasAnalysis*)this;
94 //------------------------------------------------
95 // Implement the AliasAnalysis API
97 AliasResult alias(const Location &LocA, const Location &LocB) override {
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);
105 ModRefResult getModRefInfo(ImmutableCallSite CS,
106 const Location &Loc) override {
107 assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
108 return AliasAnalysis::getModRefInfo(CS, Loc);
111 ModRefResult getModRefInfo(ImmutableCallSite CS1,
112 ImmutableCallSite CS2) override {
113 return AliasAnalysis::getModRefInfo(CS1,CS2);
116 bool pointsToConstantMemory(const Location &Loc, bool OrLocal) override {
117 assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
118 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
121 void deleteValue(Value *V) override {
122 assert(Vals.find(V) != Vals.end() && "Never seen value in AA before");
123 AliasAnalysis::deleteValue(V);
125 void copyValue(Value *From, Value *To) override {
127 AliasAnalysis::copyValue(From, To);
133 char AliasDebugger::ID = 0;
134 INITIALIZE_AG_PASS(AliasDebugger, AliasAnalysis, "debug-aa",
135 "AA use debugger", false, true, false)
137 Pass *llvm::createAliasDebugger() { return new AliasDebugger(); }