AliasDebugger.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "llvm/Analysis/Passes.h"
  19. #include "llvm/Analysis/AliasAnalysis.h"
  20. #include "llvm/IR/Constants.h"
  21. #include "llvm/IR/DerivedTypes.h"
  22. #include "llvm/IR/Instructions.h"
  23. #include "llvm/IR/Module.h"
  24. #include "llvm/Pass.h"
  25. #include <set>
  26. using namespace llvm;
  27. namespace {
  28. class AliasDebugger : public ModulePass, public AliasAnalysis {
  29. //What we do is simple. Keep track of every value the AA could
  30. //know about, and verify that queries are one of those.
  31. //A query to a value that didn't exist when the AA was created
  32. //means someone forgot to update the AA when creating new values
  33. std::set<const Value*> Vals;
  34. public:
  35. static char ID; // Class identification, replacement for typeinfo
  36. AliasDebugger() : ModulePass(ID) {
  37. initializeAliasDebuggerPass(*PassRegistry::getPassRegistry());
  38. }
  39. bool runOnModule(Module &M) override {
  40. InitializeAliasAnalysis(this, &M.getDataLayout()); // set up super class
  41. for(Module::global_iterator I = M.global_begin(),
  42. E = M.global_end(); I != E; ++I) {
  43. Vals.insert(&*I);
  44. for (User::const_op_iterator OI = I->op_begin(),
  45. OE = I->op_end(); OI != OE; ++OI)
  46. Vals.insert(*OI);
  47. }
  48. for(Module::iterator I = M.begin(),
  49. E = M.end(); I != E; ++I){
  50. Vals.insert(&*I);
  51. if(!I->isDeclaration()) {
  52. for (Function::arg_iterator AI = I->arg_begin(), AE = I->arg_end();
  53. AI != AE; ++AI)
  54. Vals.insert(&*AI);
  55. for (Function::const_iterator FI = I->begin(), FE = I->end();
  56. FI != FE; ++FI)
  57. for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end();
  58. BI != BE; ++BI) {
  59. Vals.insert(&*BI);
  60. for (User::const_op_iterator OI = BI->op_begin(),
  61. OE = BI->op_end(); OI != OE; ++OI)
  62. Vals.insert(*OI);
  63. }
  64. }
  65. }
  66. return false;
  67. }
  68. void getAnalysisUsage(AnalysisUsage &AU) const override {
  69. AliasAnalysis::getAnalysisUsage(AU);
  70. AU.setPreservesAll(); // Does not transform code
  71. }
  72. /// getAdjustedAnalysisPointer - This method is used when a pass implements
  73. /// an analysis interface through multiple inheritance. If needed, it
  74. /// should override this to adjust the this pointer as needed for the
  75. /// specified pass info.
  76. void *getAdjustedAnalysisPointer(AnalysisID PI) override {
  77. if (PI == &AliasAnalysis::ID)
  78. return (AliasAnalysis*)this;
  79. return this;
  80. }
  81. //------------------------------------------------
  82. // Implement the AliasAnalysis API
  83. //
  84. AliasResult alias(const MemoryLocation &LocA,
  85. const MemoryLocation &LocB) override {
  86. assert(Vals.find(LocA.Ptr) != Vals.end() &&
  87. "Never seen value in AA before");
  88. assert(Vals.find(LocB.Ptr) != Vals.end() &&
  89. "Never seen value in AA before");
  90. return AliasAnalysis::alias(LocA, LocB);
  91. }
  92. ModRefResult getModRefInfo(ImmutableCallSite CS,
  93. const MemoryLocation &Loc) override {
  94. assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
  95. return AliasAnalysis::getModRefInfo(CS, Loc);
  96. }
  97. ModRefResult getModRefInfo(ImmutableCallSite CS1,
  98. ImmutableCallSite CS2) override {
  99. return AliasAnalysis::getModRefInfo(CS1,CS2);
  100. }
  101. bool pointsToConstantMemory(const MemoryLocation &Loc,
  102. bool OrLocal) override {
  103. assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
  104. return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
  105. }
  106. void deleteValue(Value *V) override {
  107. assert(Vals.find(V) != Vals.end() && "Never seen value in AA before");
  108. AliasAnalysis::deleteValue(V);
  109. }
  110. };
  111. }
  112. char AliasDebugger::ID = 0;
  113. INITIALIZE_AG_PASS(AliasDebugger, AliasAnalysis, "debug-aa",
  114. "AA use debugger", false, true, false)
  115. Pass *llvm::createAliasDebugger() { return new AliasDebugger(); }