AliasDebugger.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "llvm/Support/Compiler.h"
  26. #include <set>
  27. using namespace llvm;
  28. namespace {
  29. class VISIBILITY_HIDDEN AliasDebugger
  30. : public ModulePass, public AliasAnalysis {
  31. //What we do is simple. Keep track of every value the AA could
  32. //know about, and verify that queries are one of those.
  33. //A query to a value that didn't exist when the AA was created
  34. //means someone forgot to update the AA when creating new values
  35. std::set<const Value*> Vals;
  36. public:
  37. static char ID; // Class identification, replacement for typeinfo
  38. AliasDebugger() : ModulePass(&ID) {}
  39. bool runOnModule(Module &M) {
  40. InitializeAliasAnalysis(this); // 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(Module::iterator I = M.begin(),
  45. E = M.end(); I != E; ++I){
  46. Vals.insert(&*I);
  47. if(!I->isDeclaration()) {
  48. for (Function::arg_iterator AI = I->arg_begin(), AE = I->arg_end();
  49. AI != AE; ++AI)
  50. Vals.insert(&*AI);
  51. for (Function::const_iterator FI = I->begin(), FE = I->end();
  52. FI != FE; ++FI)
  53. for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end();
  54. BI != BE; ++BI)
  55. Vals.insert(&*BI);
  56. }
  57. }
  58. return false;
  59. }
  60. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  61. AliasAnalysis::getAnalysisUsage(AU);
  62. AU.setPreservesAll(); // Does not transform code
  63. }
  64. //------------------------------------------------
  65. // Implement the AliasAnalysis API
  66. //
  67. AliasResult alias(const Value *V1, unsigned V1Size,
  68. const Value *V2, unsigned V2Size) {
  69. assert(Vals.find(V1) != Vals.end() && "Never seen value in AA before");
  70. assert(Vals.find(V2) != Vals.end() && "Never seen value in AA before");
  71. return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
  72. }
  73. ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
  74. assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
  75. return AliasAnalysis::getModRefInfo(CS, P, Size);
  76. }
  77. ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
  78. return AliasAnalysis::getModRefInfo(CS1,CS2);
  79. }
  80. void getMustAliases(Value *P, std::vector<Value*> &RetVals) {
  81. assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
  82. return AliasAnalysis::getMustAliases(P, RetVals);
  83. }
  84. bool pointsToConstantMemory(const Value *P) {
  85. assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
  86. return AliasAnalysis::pointsToConstantMemory(P);
  87. }
  88. /// getModRefBehavior - Return the behavior of the specified function if
  89. /// called from the specified call site. The call site may be null in which
  90. /// case the most generic behavior of this function should be returned.
  91. virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
  92. std::vector<PointerAccessInfo> *Info) {
  93. assert(Vals.find(F) != Vals.end() && "Never seen value in AA before");
  94. return AliasAnalysis::getModRefBehavior(F, CS, Info);
  95. }
  96. virtual void deleteValue(Value *V) {
  97. assert(Vals.find(V) != Vals.end() && "Never seen value in AA before");
  98. AliasAnalysis::deleteValue(V);
  99. }
  100. virtual void copyValue(Value *From, Value *To) {
  101. Vals.insert(To);
  102. AliasAnalysis::copyValue(From, To);
  103. }
  104. };
  105. }
  106. char AliasDebugger::ID = 0;
  107. static RegisterPass<AliasDebugger>
  108. X("debug-aa", "AA use debugger", false, true);
  109. static RegisterAnalysisGroup<AliasAnalysis> Y(X);
  110. Pass *llvm::createAliasDebugger() { return new AliasDebugger(); }