AliasDebugger.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/Module.h"
  20. #include "llvm/Pass.h"
  21. #include "llvm/Instructions.h"
  22. #include "llvm/Constants.h"
  23. #include "llvm/DerivedTypes.h"
  24. #include "llvm/Analysis/AliasAnalysis.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. bool runOnModule(Module &M) {
  38. InitializeAliasAnalysis(this); // set up super class
  39. for(Module::global_iterator I = M.global_begin(),
  40. E = M.global_end(); I != E; ++I)
  41. Vals.insert(&*I);
  42. for(Module::iterator I = M.begin(),
  43. E = M.end(); I != E; ++I){
  44. Vals.insert(&*I);
  45. if(!I->isDeclaration()) {
  46. for (Function::arg_iterator AI = I->arg_begin(), AE = I->arg_end();
  47. AI != AE; ++AI)
  48. Vals.insert(&*AI);
  49. for (Function::const_iterator FI = I->begin(), FE = I->end();
  50. FI != FE; ++FI)
  51. for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end();
  52. BI != BE; ++BI)
  53. Vals.insert(&*BI);
  54. }
  55. }
  56. return false;
  57. }
  58. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  59. AliasAnalysis::getAnalysisUsage(AU);
  60. AU.setPreservesAll(); // Does not transform code
  61. }
  62. //------------------------------------------------
  63. // Implement the AliasAnalysis API
  64. //
  65. AliasResult alias(const Value *V1, unsigned V1Size,
  66. const Value *V2, unsigned V2Size) {
  67. assert(Vals.find(V1) != Vals.end() && "Never seen value in AA before");
  68. assert(Vals.find(V2) != Vals.end() && "Never seen value in AA before");
  69. return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
  70. }
  71. ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
  72. assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
  73. return AliasAnalysis::getModRefInfo(CS, P, Size);
  74. }
  75. ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
  76. return AliasAnalysis::getModRefInfo(CS1,CS2);
  77. }
  78. void getMustAliases(Value *P, std::vector<Value*> &RetVals) {
  79. assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
  80. return AliasAnalysis::getMustAliases(P, RetVals);
  81. }
  82. bool pointsToConstantMemory(const Value *P) {
  83. assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
  84. return AliasAnalysis::pointsToConstantMemory(P);
  85. }
  86. virtual void deleteValue(Value *V) {
  87. assert(Vals.find(V) != Vals.end() && "Never seen value in AA before");
  88. AliasAnalysis::deleteValue(V);
  89. }
  90. virtual void copyValue(Value *From, Value *To) {
  91. Vals.insert(To);
  92. AliasAnalysis::copyValue(From, To);
  93. }
  94. };
  95. }
  96. char AliasDebugger::ID = 0;
  97. static RegisterPass<AliasDebugger>
  98. X("debug-aa", "AA use debugger", false, true);
  99. static RegisterAnalysisGroup<AliasAnalysis> Y(X);
  100. Pass *llvm::createAliasDebugger() { return new AliasDebugger(); }