LLVM  3.7.0
AliasDebugger.cpp
Go to the documentation of this file.
1 //===- AliasDebugger.cpp - Simple Alias Analysis Use Checker --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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"
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"
26 #include <set>
27 using namespace llvm;
28 
29 namespace {
30 
31  class AliasDebugger : public ModulePass, public AliasAnalysis {
32 
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:
41  static char ID; // Class identification, replacement for typeinfo
42  AliasDebugger() : ModulePass(ID) {
44  }
45 
46  bool runOnModule(Module &M) override {
47  InitializeAliasAnalysis(this, &M.getDataLayout()); // set up super class
48 
50  E = M.global_end(); I != E; ++I) {
51  Vals.insert(&*I);
52  for (User::const_op_iterator OI = I->op_begin(),
53  OE = I->op_end(); OI != OE; ++OI)
54  Vals.insert(*OI);
55  }
56 
57  for(Module::iterator I = M.begin(),
58  E = M.end(); I != E; ++I){
59  Vals.insert(&*I);
60  if(!I->isDeclaration()) {
61  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();
67  BI != BE; ++BI) {
68  Vals.insert(&*BI);
69  for (User::const_op_iterator OI = BI->op_begin(),
70  OE = BI->op_end(); OI != OE; ++OI)
71  Vals.insert(*OI);
72  }
73  }
74 
75  }
76  return false;
77  }
78 
79  void getAnalysisUsage(AnalysisUsage &AU) const override {
81  AU.setPreservesAll(); // Does not transform code
82  }
83 
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;
91  return this;
92  }
93 
94  //------------------------------------------------
95  // Implement the AliasAnalysis API
96  //
97  AliasResult alias(const MemoryLocation &LocA,
98  const MemoryLocation &LocB) override {
99  assert(Vals.find(LocA.Ptr) != Vals.end() &&
100  "Never seen value in AA before");
101  assert(Vals.find(LocB.Ptr) != Vals.end() &&
102  "Never seen value in AA before");
103  return AliasAnalysis::alias(LocA, LocB);
104  }
105 
106  ModRefResult getModRefInfo(ImmutableCallSite CS,
107  const MemoryLocation &Loc) override {
108  assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
109  return AliasAnalysis::getModRefInfo(CS, Loc);
110  }
111 
112  ModRefResult getModRefInfo(ImmutableCallSite CS1,
113  ImmutableCallSite CS2) override {
114  return AliasAnalysis::getModRefInfo(CS1,CS2);
115  }
116 
117  bool pointsToConstantMemory(const MemoryLocation &Loc,
118  bool OrLocal) override {
119  assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
120  return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
121  }
122 
123  void deleteValue(Value *V) override {
124  assert(Vals.find(V) != Vals.end() && "Never seen value in AA before");
126  }
127 
128  };
129 }
130 
131 char AliasDebugger::ID = 0;
132 INITIALIZE_AG_PASS(AliasDebugger, AliasAnalysis, "debug-aa",
133  "AA use debugger", false, true, false)
134 
135 Pass *llvm::createAliasDebugger() { return new AliasDebugger(); }
136 
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:82
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
virtual bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
pointsToConstantMemory - If the specified memory location is known to be constant, return true.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
INITIALIZE_AG_PASS(AliasDebugger, AliasAnalysis,"debug-aa","AA use debugger", false, true, false) Pass *llvm
global_iterator global_begin()
Definition: Module.h:552
void initializeAliasDebuggerPass(PassRegistry &)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:72
Represent the analysis usage information of a pass.
virtual void deleteValue(Value *V)
Methods that clients should call when they transform the program to allow alias analyses to update th...
global_iterator global_end()
Definition: Module.h:554
const Value * Ptr
The address of the start of the location.
Representation for a specific memory location.
virtual AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
Alias Queries...
Module.h This file contains the declarations for the Module class.
ModRefResult getModRefInfo(const Instruction *I)
getModRefInfo - Return information about whether or not an instruction may read or write memory (with...
void setPreservesAll()
Set by analyses that do not transform their input at all.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
iterator end()
Definition: Module.h:571
ImmutableCallSite - establish a view to a call site for examination.
Definition: CallSite.h:418
#define I(x, y, z)
Definition: MD5.cpp:54
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
iterator begin()
Definition: Module.h:569
const void * AnalysisID
Definition: Pass.h:47
Pass * createAliasDebugger()
LLVM Value Representation.
Definition: Value.h:69
virtual void getAnalysisUsage(AnalysisUsage &AU) const
getAnalysisUsage - All alias analysis implementations should invoke this directly (using AliasAnalysi...