LLVM  10.0.0svn
Go to the documentation of this file.
1 //===- OptimizationRemarkEmitter.cpp - Optimization Diagnostic --*- C++ -*-===//
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 // Optimization diagnostic interfaces. It's packaged as an analysis pass so
10 // that by using this service passes become dependent on BFI as well. BFI is
11 // used to compute the "hotness" of the diagnostic message.
12 //===----------------------------------------------------------------------===//
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/IR/DiagnosticInfo.h"
19 #include "llvm/IR/Dominators.h"
20 #include "llvm/IR/LLVMContext.h"
22 using namespace llvm;
25  : F(F), BFI(nullptr) {
27  return;
29  // First create a dominator tree.
30  DominatorTree DT;
31  DT.recalculate(*const_cast<Function *>(F));
33  // Generate LoopInfo from it.
34  LoopInfo LI;
35  LI.analyze(DT);
37  // Then compute BranchProbabilityInfo.
39  BPI.calculate(*F, LI);
41  // Finally compute BFI.
42  OwnedBFI = std::make_unique<BlockFrequencyInfo>(*F, BPI, LI);
43  BFI = OwnedBFI.get();
44 }
47  Function &F, const PreservedAnalyses &PA,
49  // This analysis has no state and so can be trivially preserved but it needs
50  // a fresh view of BFI if it was constructed with one.
51  if (BFI && Inv.invalidate<BlockFrequencyAnalysis>(F, PA))
52  return true;
54  // Otherwise this analysis result remains valid.
55  return false;
56 }
58 Optional<uint64_t> OptimizationRemarkEmitter::computeHotness(const Value *V) {
59  if (!BFI)
60  return None;
62  return BFI->getBlockProfileCount(cast<BasicBlock>(V));
63 }
65 void OptimizationRemarkEmitter::computeHotness(
67  const Value *V = OptDiag.getCodeRegion();
68  if (V)
69  OptDiag.setHotness(computeHotness(V));
70 }
73  DiagnosticInfoOptimizationBase &OptDiagBase) {
74  auto &OptDiag = cast<DiagnosticInfoIROptimization>(OptDiagBase);
75  computeHotness(OptDiag);
77  // Only emit it if its hotness meets the threshold.
78  if (OptDiag.getHotness().getValueOr(0) <
80  return;
81  }
83  F->getContext().diagnose(OptDiag);
84 }
87  : FunctionPass(ID) {
90 }
96  BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
97  else
98  BFI = nullptr;
100  ORE = std::make_unique<OptimizationRemarkEmitter>(&Fn, BFI);
101  return false;
102 }
105  AnalysisUsage &AU) const {
107  AU.setPreservesAll();
108 }
110 AnalysisKey OptimizationRemarkEmitterAnalysis::Key;
118  BFI = &AM.getResult<BlockFrequencyAnalysis>(F);
119  else
120  BFI = nullptr;
122  return OptimizationRemarkEmitter(&F, BFI);
123 }
126 static const char ore_name[] = "Optimization Remark Emitter";
127 #define ORE_NAME "opt-remark-emitter"
130  false, true)
133  false, true)
Optional< uint64_t > getBlockProfileCount(const BasicBlock *BB, bool AllowSynthetic=false) const
Returns the estimated profile count of BB.
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:667
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:777
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:266
block Block Frequency true
Definition: PassSupport.h:50
#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:144
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:538
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:284
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:205
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:649
Optional< uint64_t > getHotness() const
LLVM Value Representation.
Definition: Value.h:74
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