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 9 #include "ProvenanceAnalysis.h" 10 #include "llvm/ADT/SetVector.h" 11 #include "llvm/Analysis/AliasAnalysis.h" 12 #include "llvm/Analysis/Passes.h" 13 #include "llvm/IR/Function.h" 14 #include "llvm/IR/InstIterator.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 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) 95