LLVM  8.0.1
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/DiagnosticInfo.h"
20 #include "llvm/IR/Dominators.h"
21 #include "llvm/IR/LLVMContext.h"
22 
23 using namespace llvm;
24 
26  : F(F), BFI(nullptr) {
28  return;
29 
30  // First create a dominator tree.
31  DominatorTree DT;
32  DT.recalculate(*const_cast<Function *>(F));
33 
34  // Generate LoopInfo from it.
35  LoopInfo LI;
36  LI.analyze(DT);
37 
38  // Then compute BranchProbabilityInfo.
40  BPI.calculate(*F, LI);
41 
42  // Finally compute BFI.
43  OwnedBFI = llvm::make_unique<BlockFrequencyInfo>(*F, BPI, LI);
44  BFI = OwnedBFI.get();
45 }
46 
48  Function &F, const PreservedAnalyses &PA,
50  // This analysis has no state and so can be trivially preserved but it needs
51  // a fresh view of BFI if it was constructed with one.
52  if (BFI && Inv.invalidate<BlockFrequencyAnalysis>(F, PA))
53  return true;
54 
55  // Otherwise this analysis result remains valid.
56  return false;
57 }
58 
59 Optional<uint64_t> OptimizationRemarkEmitter::computeHotness(const Value *V) {
60  if (!BFI)
61  return None;
62 
63  return BFI->getBlockProfileCount(cast<BasicBlock>(V));
64 }
65 
66 void OptimizationRemarkEmitter::computeHotness(
68  const Value *V = OptDiag.getCodeRegion();
69  if (V)
70  OptDiag.setHotness(computeHotness(V));
71 }
72 
74  DiagnosticInfoOptimizationBase &OptDiagBase) {
75  auto &OptDiag = cast<DiagnosticInfoIROptimization>(OptDiagBase);
76  computeHotness(OptDiag);
77 
78  // Only emit it if its hotness meets the threshold.
79  if (OptDiag.getHotness().getValueOr(0) <
81  return;
82  }
83 
84  F->getContext().diagnose(OptDiag);
85 }
86 
88  : FunctionPass(ID) {
91 }
92 
95 
97  BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
98  else
99  BFI = nullptr;
100 
101  ORE = llvm::make_unique<OptimizationRemarkEmitter>(&Fn, BFI);
102  return false;
103 }
104 
106  AnalysisUsage &AU) const {
108  AU.setPreservesAll();
109 }
110 
111 AnalysisKey OptimizationRemarkEmitterAnalysis::Key;
112 
117 
119  BFI = &AM.getResult<BlockFrequencyAnalysis>(F);
120  else
121  BFI = nullptr;
122 
123  return OptimizationRemarkEmitter(&F, BFI);
124 }
125 
127 static const char ore_name[] = "Optimization Remark Emitter";
128 #define ORE_NAME "opt-remark-emitter"
129 
131  false, true)
132 INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)
134  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:660
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:770
This class represents lattice values for constants.
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[]
constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION
Definition: Optional.h:172
F(f)
block Block Frequency true
#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:145
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:154
void analyze(const DominatorTreeBase< BlockT, false > &DomTree)
Create the loop forest using a stable algorithm.
Definition: LoopInfoImpl.h:554
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:193
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.
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:642
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:71
Optional< uint64_t > getBlockProfileCount(const BasicBlock *BB) const
Returns the estimated profile count of BB.