Add back r222061 with a fix.
[oota-llvm.git] / lib / Transforms / ObjCARC / ProvenanceAnalysisEvaluator.cpp
1 //===- ProvenanceAnalysisEvaluator.cpp - ObjC ARC Optimization ------------===//
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 #include "ProvenanceAnalysis.h"
11 #include "llvm/Pass.h"
12 #include "llvm/ADT/SetVector.h"
13 #include "llvm/Analysis/AliasAnalysis.h"
14 #include "llvm/Analysis/Passes.h"
15 #include "llvm/IR/InstIterator.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/Support/raw_ostream.h"
18
19 using namespace llvm;
20 using namespace llvm::objcarc;
21
22 namespace {
23 class PAEval : public FunctionPass {
24
25 public:
26   static char ID;
27   PAEval();
28   void getAnalysisUsage(AnalysisUsage &AU) const override;
29   bool runOnFunction(Function &F) override;
30 };
31 }
32
33 char PAEval::ID = 0;
34 PAEval::PAEval() : FunctionPass(ID) {}
35
36 void PAEval::getAnalysisUsage(AnalysisUsage &AU) const {
37   AU.addRequired<AliasAnalysis>();
38 }
39
40 static StringRef getName(Value *V) {
41   StringRef Name = V->getName();
42   if (Name.startswith("\1"))
43     return Name.substr(1);
44   return Name;
45 }
46
47 static void insertIfNamed(SetVector<Value *> &Values, Value *V) {
48   if (!V->hasName())
49     return;
50   Values.insert(V);
51 }
52
53 bool PAEval::runOnFunction(Function &F) {
54   SetVector<Value *> Values;
55
56   for (auto &Arg : F.args())
57     insertIfNamed(Values, &Arg);
58
59   for (auto I = inst_begin(F), E = inst_end(F); I != E; ++I) {
60     insertIfNamed(Values, &*I);
61
62     for (auto &Op : I->operands())
63     insertIfNamed(Values, Op);
64   }
65
66   ProvenanceAnalysis PA;
67   PA.setAA(&getAnalysis<AliasAnalysis>());
68
69   for (Value *V1 : Values) {
70     StringRef NameV1 = getName(V1);
71     for (Value *V2 : Values) {
72       StringRef NameV2 = getName(V2);
73       if (NameV1 >= NameV2)
74         continue;
75       errs() << NameV1 << " and " << NameV2;
76       if (PA.related(V1, V2))
77         errs() << " are related.\n";
78       else
79         errs() << " are not related.\n";
80     }
81   }
82
83   return false;
84 }
85
86 FunctionPass *llvm::createPAEvalPass() { return new PAEval(); }
87
88 INITIALIZE_PASS_BEGIN(PAEval, "pa-eval",
89                       "Evaluate ProvenanceAnalysis on all pairs", false, true)
90 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
91 INITIALIZE_PASS_END(PAEval, "pa-eval",
92                     "Evaluate ProvenanceAnalysis on all pairs", false, true)