llvm.org GIT mirror llvm / 89a08aa
[PM/AA] Port alias analysis evaluator to the new pass manager, and use it to actually test the new pass manager AA wiring. This patch was extracted from the (somewhat too large) D12357 and rebosed on top of the slightly different design of the new pass manager AA wiring that I just landed. With this we can start testing the AA in a thorough way with the new pass manager. Some minor cleanups to the code in the pass was necessitated here, but otherwise it is a very minimal change. Differential Revision: http://reviews.llvm.org/D17372 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@261403 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 4 years ago
10 changed file(s) with 146 addition(s) and 86 deletion(s). Raw diff Collapse all Expand all
0 //===- AliasAnalysisEvaluator.h - Alias Analysis Accuracy Evaluator -------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a simple N^2 alias analysis accuracy evaluator.
10 // Basically, for each function in the program, it simply queries to see how the
11 // alias analysis implementation answers alias queries between each pair of
12 // pointers in the function.
13 //
14 // This is inspired and adapted from code by: Naveen Neelakantam, Francesco
15 // Spadini, and Wojciech Stryjewski.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_ANALYSIS_ALIASANALYSISEVALUATOR_H
20 #define LLVM_ANALYSIS_ALIASANALYSISEVALUATOR_H
21
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/PassManager.h"
24
25 namespace llvm {
26 class AAResults;
27
28 class AAEvaluator {
29 int64_t FunctionCount;
30 int64_t NoAliasCount, MayAliasCount, PartialAliasCount, MustAliasCount;
31 int64_t NoModRefCount, ModCount, RefCount, ModRefCount;
32
33 public:
34 AAEvaluator()
35 : FunctionCount(), NoAliasCount(), MayAliasCount(), PartialAliasCount(),
36 MustAliasCount(), NoModRefCount(), ModCount(), RefCount(),
37 ModRefCount() {}
38 AAEvaluator(AAEvaluator &&Arg)
39 : FunctionCount(Arg.FunctionCount), NoAliasCount(Arg.NoAliasCount),
40 MayAliasCount(Arg.MayAliasCount),
41 PartialAliasCount(Arg.PartialAliasCount),
42 MustAliasCount(Arg.MustAliasCount), NoModRefCount(Arg.NoModRefCount),
43 ModCount(Arg.ModCount), RefCount(Arg.RefCount),
44 ModRefCount(Arg.ModRefCount) {
45 Arg.FunctionCount = 0;
46 }
47 ~AAEvaluator();
48
49 static StringRef name() { return "AAEvaluator"; }
50
51 /// \brief Run the pass over the function.
52 PreservedAnalyses run(Function &F, AnalysisManager *AM);
53
54 private:
55 // Allow the legacy pass to run this using an internal API.
56 friend class AAEvalLegacyPass;
57
58 void runInternal(Function &F, AAResults &AA);
59 };
60
61 /// Create a wrapper of the above for the legacy pass manager.
62 FunctionPass *createAAEvalPass();
63
64 }
65
66 #endif
2121 class ModulePass;
2222 class Pass;
2323 class PassInfo;
24
25 //===--------------------------------------------------------------------===//
26 //
27 // createAAEvalPass - This pass implements a simple N^2 alias analysis
28 // accuracy evaluator.
29 //
30 FunctionPass *createAAEvalPass();
3124
3225 //===--------------------------------------------------------------------===//
3326 //
5858 /// initializeCodeGen - Initialize all passes linked into the CodeGen library.
5959 void initializeTarget(PassRegistry&);
6060
61 void initializeAAEvalPass(PassRegistry&);
61 void initializeAAEvalLegacyPassPass(PassRegistry&);
6262 void initializeAddDiscriminatorsPass(PassRegistry&);
6363 void initializeADCELegacyPassPass(PassRegistry&);
6464 void initializeBDCEPass(PassRegistry&);
1616
1717 #include "llvm/ADT/Statistic.h"
1818 #include "llvm/Analysis/AliasSetTracker.h"
19 #include "llvm/Analysis/AliasAnalysisEvaluator.h"
1920 #include "llvm/Analysis/BasicAliasAnalysis.h"
2021 #include "llvm/Analysis/CFLAliasAnalysis.h"
2122 #include "llvm/Analysis/CallPrinter.h"
55 // License. See LICENSE.TXT for details.
66 //
77 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a simple N^2 alias analysis accuracy evaluator.
10 // Basically, for each function in the program, it simply queries to see how the
11 // alias analysis implementation answers alias queries between each pair of
12 // pointers in the function.
13 //
14 // This is inspired and adapted from code by: Naveen Neelakantam, Francesco
15 // Spadini, and Wojciech Stryjewski.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Analysis/Passes.h"
8
9 #include "llvm/Analysis/AliasAnalysisEvaluator.h"
2010 #include "llvm/ADT/SetVector.h"
2111 #include "llvm/Analysis/AliasAnalysis.h"
2212 #include "llvm/IR/Constants.h"
4636
4737 static cl::opt EvalAAMD("evaluate-aa-metadata", cl::ReallyHidden);
4838
49 namespace {
50 class AAEval : public FunctionPass {
51 unsigned NoAliasCount, MayAliasCount, PartialAliasCount, MustAliasCount;
52 unsigned NoModRefCount, ModCount, RefCount, ModRefCount;
53
54 public:
55 static char ID; // Pass identification, replacement for typeid
56 AAEval() : FunctionPass(ID) {
57 initializeAAEvalPass(*PassRegistry::getPassRegistry());
58 }
59
60 void getAnalysisUsage(AnalysisUsage &AU) const override {
61 AU.addRequired();
62 AU.setPreservesAll();
63 }
64
65 bool doInitialization(Module &M) override {
66 NoAliasCount = MayAliasCount = PartialAliasCount = MustAliasCount = 0;
67 NoModRefCount = ModCount = RefCount = ModRefCount = 0;
68
69 if (PrintAll) {
70 PrintNoAlias = PrintMayAlias = true;
71 PrintPartialAlias = PrintMustAlias = true;
72 PrintNoModRef = PrintMod = PrintRef = PrintModRef = true;
73 }
74 return false;
75 }
76
77 bool runOnFunction(Function &F) override;
78 bool doFinalization(Module &M) override;
79 };
80 }
81
82 char AAEval::ID = 0;
83 INITIALIZE_PASS_BEGIN(AAEval, "aa-eval",
84 "Exhaustive Alias Analysis Precision Evaluator", false, true)
85 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
86 INITIALIZE_PASS_END(AAEval, "aa-eval",
87 "Exhaustive Alias Analysis Precision Evaluator", false, true)
88
89 FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
90
9139 static void PrintResults(const char *Msg, bool P, const Value *V1,
9240 const Value *V2, const Module *M) {
93 if (P) {
41 if (PrintAll || P) {
9442 std::string o1, o2;
9543 {
9644 raw_string_ostream os1(o1), os2(o2);
10957 static inline void
11058 PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
11159 Module *M) {
112 if (P) {
60 if (PrintAll || P) {
11361 errs() << " " << Msg << ": Ptr: ";
11462 Ptr->printAsOperand(errs(), true, M);
11563 errs() << "\t<->" << *I << '\n';
11967 static inline void
12068 PrintModRefResults(const char *Msg, bool P, CallSite CSA, CallSite CSB,
12169 Module *M) {
122 if (P) {
70 if (PrintAll || P) {
12371 errs() << " " << Msg << ": " << *CSA.getInstruction()
12472 << " <-> " << *CSB.getInstruction() << '\n';
12573 }
12876 static inline void
12977 PrintLoadStoreResults(const char *Msg, bool P, const Value *V1,
13078 const Value *V2, const Module *M) {
131 if (P) {
79 if (PrintAll || P) {
13280 errs() << " " << Msg << ": " << *V1
13381 << " <-> " << *V2 << '\n';
13482 }
13987 && !isa(V);
14088 }
14189
142 bool AAEval::runOnFunction(Function &F) {
90 PreservedAnalyses AAEvaluator::run(Function &F, AnalysisManager *AM) {
91 runInternal(F, AM->getResult(F));
92 return PreservedAnalyses::all();
93 }
94
95 void AAEvaluator::runInternal(Function &F, AAResults &AA) {
14396 const DataLayout &DL = F.getParent()->getDataLayout();
144 AliasAnalysis &AA = getAnalysis().getAAResults();
97
98 ++FunctionCount;
14599
146100 SetVector Pointers;
147101 SmallSetVector CallSites;
179133 }
180134 }
181135
182 if (PrintNoAlias || PrintMayAlias || PrintPartialAlias || PrintMustAlias ||
183 PrintNoModRef || PrintMod || PrintRef || PrintModRef)
136 if (PrintAll || PrintNoAlias || PrintMayAlias || PrintPartialAlias ||
137 PrintMustAlias || PrintNoModRef || PrintMod || PrintRef || PrintModRef)
184138 errs() << "Function: " << F.getName() << ": " << Pointers.size()
185139 << " pointers, " << CallSites.size() << " call sites\n";
186140
337291 }
338292 }
339293 }
340
341 return false;
342 }
343
344 static void PrintPercent(unsigned Num, unsigned Sum) {
345 errs() << "(" << Num*100ULL/Sum << "."
346 << ((Num*1000ULL/Sum) % 10) << "%)\n";
347 }
348
349 bool AAEval::doFinalization(Module &M) {
350 unsigned AliasSum =
294 }
295
296 static void PrintPercent(int64_t Num, int64_t Sum) {
297 errs() << "(" << Num * 100LL / Sum << "." << ((Num * 1000LL / Sum) % 10)
298 << "%)\n";
299 }
300
301 AAEvaluator::~AAEvaluator() {
302 if (FunctionCount == 0)
303 return;
304
305 int64_t AliasSum =
351306 NoAliasCount + MayAliasCount + PartialAliasCount + MustAliasCount;
352307 errs() << "===== Alias Analysis Evaluator Report =====\n";
353308 if (AliasSum == 0) {
370325 }
371326
372327 // Display the summary for mod/ref analysis
373 unsigned ModRefSum = NoModRefCount + ModCount + RefCount + ModRefCount;
328 int64_t ModRefSum = NoModRefCount + ModCount + RefCount + ModRefCount;
374329 if (ModRefSum == 0) {
375330 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no "
376331 "mod/ref!\n";
389344 << ModCount * 100 / ModRefSum << "%/" << RefCount * 100 / ModRefSum
390345 << "%/" << ModRefCount * 100 / ModRefSum << "%\n";
391346 }
392
393 return false;
394 }
347 }
348
349 namespace llvm {
350 class AAEvalLegacyPass : public FunctionPass {
351 std::unique_ptr P;
352
353 public:
354 static char ID; // Pass identification, replacement for typeid
355 AAEvalLegacyPass() : FunctionPass(ID) {
356 initializeAAEvalLegacyPassPass(*PassRegistry::getPassRegistry());
357 }
358
359 void getAnalysisUsage(AnalysisUsage &AU) const override {
360 AU.addRequired();
361 AU.setPreservesAll();
362 }
363
364 bool doInitialization(Module &M) override {
365 P.reset(new AAEvaluator());
366 return false;
367 }
368
369 bool runOnFunction(Function &F) override {
370 P->runInternal(F, getAnalysis().getAAResults());
371 return false;
372 }
373 bool doFinalization(Module &M) override {
374 P.reset();
375 return false;
376 }
377 };
378 }
379
380 char AAEvalLegacyPass::ID = 0;
381 INITIALIZE_PASS_BEGIN(AAEvalLegacyPass, "aa-eval",
382 "Exhaustive Alias Analysis Precision Evaluator", false,
383 true)
384 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
385 INITIALIZE_PASS_END(AAEvalLegacyPass, "aa-eval",
386 "Exhaustive Alias Analysis Precision Evaluator", false,
387 true)
388
389 FunctionPass *llvm::createAAEvalPass() { return new AAEvalLegacyPass(); }
1919
2020 /// initializeAnalysis - Initialize all passes linked into the Analysis library.
2121 void llvm::initializeAnalysis(PassRegistry &Registry) {
22 initializeAAEvalPass(Registry);
22 initializeAAEvalLegacyPassPass(Registry);
2323 initializeAliasSetPrinterPass(Registry);
2424 initializeBasicAAWrapperPassPass(Registry);
2525 initializeBlockFrequencyInfoWrapperPassPass(Registry);
1616
1717 #include "llvm/Passes/PassBuilder.h"
1818 #include "llvm/Analysis/AliasAnalysis.h"
19 #include "llvm/Analysis/AliasAnalysisEvaluator.h"
1920 #include "llvm/Analysis/AssumptionCache.h"
2021 #include "llvm/Analysis/BasicAliasAnalysis.h"
2122 #include "llvm/Analysis/CGSCCPassManager.h"
7474 #ifndef FUNCTION_PASS
7575 #define FUNCTION_PASS(NAME, CREATE_PASS)
7676 #endif
77 FUNCTION_PASS("aa-eval", AAEvaluator())
7778 FUNCTION_PASS("adce", ADCEPass())
7879 FUNCTION_PASS("early-cse", EarlyCSEPass())
7980 FUNCTION_PASS("instcombine", InstCombinePass())
0 ; RUN: opt < %s -basicaa -aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
1 ; RUN: opt < %s -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
12 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
23 target triple = "x86_64-unknown-linux-gnu"
34
77 //===----------------------------------------------------------------------===//
88
99 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
10 #include "llvm/Analysis/AliasAnalysisEvaluator.h"
1011 #include "llvm/Analysis/Passes.h"
1112 #include "llvm/IR/Constants.h"
1213 #include "llvm/IR/Instructions.h"