LLVM  6.0.0svn
OptimizationRemarkEmitter.cpp
Go to the documentation of this file.
1 //===- OptimizationRemarkEmitter.cpp - Optimization Diagnostic --*- C++ -*-===//
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 // Optimization diagnostic interfaces. It's packaged as an analysis pass so
11 // that by using this service passes become dependent on BFI as well. BFI is
12 // used to compute the "hotness" of the diagnostic message.
13 //===----------------------------------------------------------------------===//
14 
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/IR/DebugInfo.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/Dominators.h"
22 #include "llvm/IR/LLVMContext.h"
23 
24 using namespace llvm;
25 
27  : F(F), BFI(nullptr) {
29  return;
30 
31  // First create a dominator tree.
32  DominatorTree DT;
33  DT.recalculate(*const_cast<Function *>(F));
34 
35  // Generate LoopInfo from it.
36  LoopInfo LI;
37  LI.analyze(DT);
38 
39  // Then compute BranchProbabilityInfo.
41  BPI.calculate(*F, LI);
42 
43  // Finally compute BFI.
44  OwnedBFI = llvm::make_unique<BlockFrequencyInfo>(*F, BPI, LI);
45  BFI = OwnedBFI.get();
46 }
47 
49  Function &F, const PreservedAnalyses &PA,
51  // This analysis has no state and so can be trivially preserved but it needs
52  // a fresh view of BFI if it was constructed with one.
53  if (BFI && Inv.invalidate<BlockFrequencyAnalysis>(F, PA))
54  return true;
55 
56  // Otherwise this analysis result remains valid.
57  return false;
58 }
59 
60 Optional<uint64_t> OptimizationRemarkEmitter::computeHotness(const Value *V) {
61  if (!BFI)
62  return None;
63 
64  return BFI->getBlockProfileCount(cast<BasicBlock>(V));
65 }
66 
67 void OptimizationRemarkEmitter::computeHotness(
69  const Value *V = OptDiag.getCodeRegion();
70  if (V)
71  OptDiag.setHotness(computeHotness(V));
72 }
73 
75  DiagnosticInfoOptimizationBase &OptDiagBase) {
76  auto &OptDiag = cast<DiagnosticInfoIROptimization>(OptDiagBase);
77  computeHotness(OptDiag);
78  // If a diagnostic has a hotness value, then only emit it if its hotness
79  // meets the threshold.
80  if (OptDiag.getHotness() &&
81  *OptDiag.getHotness() <
83  return;
84  }
85 
86  F->getContext().diagnose(OptDiag);
87 }
88 
90  : FunctionPass(ID) {
93 }
94 
97 
99  BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
100  else
101  BFI = nullptr;
102 
103  ORE = llvm::make_unique<OptimizationRemarkEmitter>(&Fn, BFI);
104  return false;
105 }
106 
108  AnalysisUsage &AU) const {
110  AU.setPreservesAll();
111 }
112 
113 AnalysisKey OptimizationRemarkEmitterAnalysis::Key;
114 
119 
121  BFI = &AM.getResult<BlockFrequencyAnalysis>(F);
122  else
123  BFI = nullptr;
124 
125  return OptimizationRemarkEmitter(&F, BFI);
126 }
127 
129 static const char ore_name[] = "Optimization Remark Emitter";
130 #define ORE_NAME "opt-remark-emitter"
131 
133  false, true)
134 INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)
136  false, true)
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
Definition: PassManager.h:577
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
INITIALIZE_PASS_BEGIN(OptimizationRemarkEmitterWrapperPass, ORE_NAME, ore_name, false, true) INITIALIZE_PASS_END(OptimizationRemarkEmitterWrapperPass
Result run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BFI.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:687
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void calculate(const Function &F, const LoopInfo &LI, const TargetLibraryInfo *TLI=nullptr)
void initializeOptimizationRemarkEmitterWrapperPassPass(PassRegistry &)
void recalculate(ParentType &Func)
recalculate - compute a dominator tree for the given function
static const char ore_name[]
F(f)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
#define ORE_NAME
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:140
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
void analyze(const DominatorTreeBase< BlockT, false > &DomTree)
Create the loop forest using a stable algorithm.
Definition: LoopInfoImpl.h:483
uint64_t getDiagnosticsHotnessThreshold() const
Return the minimum hotness value a diagnostic would need in order to be included in optimization diag...
bool getDiagnosticsHotnessRequested() const
Return if a code hotness metric should be included in optimization diagnostics.
const Value * getCodeRegion() const
Represent the analysis usage information of a pass.
Common features for diagnostics dealing with optimization remarks that are used by IR passes...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:194
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
Analysis pass which computes BlockFrequencyInfo.
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file. ...
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI)
void setPreservesAll()
Set by analyses that do not transform their input at all.
static void getLazyBFIAnalysisUsage(AnalysisUsage &AU)
Helper for client passes to set up the analysis usage on behalf of this pass.
Basic Alias true
Analysis providing branch probability information.
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:559
Optional< uint64_t > getHotness() const
LLVM Value Representation.
Definition: Value.h:73
OptimizationRemarkEmitter legacy analysis pass.
void setHotness(Optional< uint64_t > H)
A container for analyses that lazily runs them and caches their results.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
The optimization diagnostic interface.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:70
Optional< uint64_t > getBlockProfileCount(const BasicBlock *BB) const
Returns the estimated profile count of BB.