[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
[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/IR/Module.h"
18 #include "llvm/Support/raw_ostream.h"
19
20 using namespace llvm;
21 using namespace llvm::objcarc;
22
23 namespace {
24 class PAEval : public FunctionPass {
25
26 public:
27   static char ID;
28   PAEval();
29   void getAnalysisUsage(AnalysisUsage &AU) const override;
30   bool runOnFunction(Function &F) override;
31 };
32 }
33
34 char PAEval::ID = 0;
35 PAEval::PAEval() : FunctionPass(ID) {}
36
37 void PAEval::getAnalysisUsage(AnalysisUsage &AU) const {
38   AU.addRequired<AAResultsWrapperPass>();
39 }
40
41 static StringRef getName(Value *V) {
42   StringRef Name = V->getName();
43   if (Name.startswith("\1"))
44     return Name.substr(1);
45   return Name;
46 }
47
48 static void insertIfNamed(SetVector<Value *> &Values, Value *V) {
49   if (!V->hasName())
50     return;
51   Values.insert(V);
52 }
53
54 bool PAEval::runOnFunction(Function &F) {
55   SetVector<Value *> Values;
56
57   for (auto &Arg : F.args())
58     insertIfNamed(Values, &Arg);
59
60   for (auto I = inst_begin(F), E = inst_end(F); I != E; ++I) {
61     insertIfNamed(Values, &*I);
62
63     for (auto &Op : I->operands())
64     insertIfNamed(Values, Op);
65   }
66
67   ProvenanceAnalysis PA;
68   PA.setAA(&getAnalysis<AAResultsWrapperPass>().getAAResults());
69   const DataLayout &DL = F.getParent()->getDataLayout();
70
71   for (Value *V1 : Values) {
72     StringRef NameV1 = getName(V1);
73     for (Value *V2 : Values) {
74       StringRef NameV2 = getName(V2);
75       if (NameV1 >= NameV2)
76         continue;
77       errs() << NameV1 << " and " << NameV2;
78       if (PA.related(V1, V2, DL))
79         errs() << " are related.\n";
80       else
81         errs() << " are not related.\n";
82     }
83   }
84
85   return false;
86 }
87
88 FunctionPass *llvm::createPAEvalPass() { return new PAEval(); }
89
90 INITIALIZE_PASS_BEGIN(PAEval, "pa-eval",
91                       "Evaluate ProvenanceAnalysis on all pairs", false, true)
92 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
93 INITIALIZE_PASS_END(PAEval, "pa-eval",
94                     "Evaluate ProvenanceAnalysis on all pairs", false, true)