ProvenanceAnalysisEvaluator.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===- ProvenanceAnalysisEvaluator.cpp - ObjC ARC Optimization ------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "ProvenanceAnalysis.h"
  9. #include "llvm/ADT/SetVector.h"
  10. #include "llvm/Analysis/AliasAnalysis.h"
  11. #include "llvm/Analysis/Passes.h"
  12. #include "llvm/IR/Function.h"
  13. #include "llvm/IR/InstIterator.h"
  14. #include "llvm/IR/InstrTypes.h"
  15. #include "llvm/IR/Module.h"
  16. #include "llvm/InitializePasses.h"
  17. #include "llvm/Pass.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace llvm;
  20. using namespace llvm::objcarc;
  21. namespace {
  22. class PAEval : public FunctionPass {
  23. public:
  24. static char ID;
  25. PAEval();
  26. void getAnalysisUsage(AnalysisUsage &AU) const override;
  27. bool runOnFunction(Function &F) override;
  28. };
  29. }
  30. char PAEval::ID = 0;
  31. PAEval::PAEval() : FunctionPass(ID) {}
  32. void PAEval::getAnalysisUsage(AnalysisUsage &AU) const {
  33. AU.addRequired<AAResultsWrapperPass>();
  34. }
  35. static StringRef getName(Value *V) {
  36. StringRef Name = V->getName();
  37. if (Name.startswith("\1"))
  38. return Name.substr(1);
  39. return Name;
  40. }
  41. static void insertIfNamed(SetVector<Value *> &Values, Value *V) {
  42. if (!V->hasName())
  43. return;
  44. Values.insert(V);
  45. }
  46. bool PAEval::runOnFunction(Function &F) {
  47. SetVector<Value *> Values;
  48. for (auto &Arg : F.args())
  49. insertIfNamed(Values, &Arg);
  50. for (Instruction &I : instructions(F)) {
  51. insertIfNamed(Values, &I);
  52. for (auto &Op : I.operands())
  53. insertIfNamed(Values, Op);
  54. }
  55. ProvenanceAnalysis PA;
  56. PA.setAA(&getAnalysis<AAResultsWrapperPass>().getAAResults());
  57. for (Value *V1 : Values) {
  58. StringRef NameV1 = getName(V1);
  59. for (Value *V2 : Values) {
  60. StringRef NameV2 = getName(V2);
  61. if (NameV1 >= NameV2)
  62. continue;
  63. errs() << NameV1 << " and " << NameV2;
  64. if (PA.related(V1, V2))
  65. errs() << " are related.\n";
  66. else
  67. errs() << " are not related.\n";
  68. }
  69. }
  70. return false;
  71. }
  72. FunctionPass *llvm::createPAEvalPass() { return new PAEval(); }
  73. INITIALIZE_PASS_BEGIN(PAEval, "pa-eval",
  74. "Evaluate ProvenanceAnalysis on all pairs", false, true)
  75. INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
  76. INITIALIZE_PASS_END(PAEval, "pa-eval",
  77. "Evaluate ProvenanceAnalysis on all pairs", false, true)