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 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 15 #include "llvm/Analysis/BranchProbabilityInfo.h" 16 #include "llvm/Analysis/LazyBlockFrequencyInfo.h" 17 #include "llvm/Analysis/LoopInfo.h" 18 #include "llvm/IR/DiagnosticInfo.h" 19 #include "llvm/IR/Dominators.h" 20 #include "llvm/IR/LLVMContext.h" 21 #include "llvm/InitializePasses.h" 22 23 using namespace llvm; 24 25 OptimizationRemarkEmitter::OptimizationRemarkEmitter(const Function *F) 26 : F(F), BFI(nullptr) { 27 if (!F->getContext().getDiagnosticsHotnessRequested()) 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. 39 BranchProbabilityInfo BPI(*F, LI); 40 41 // Finally compute BFI. 42 OwnedBFI = std::make_unique<BlockFrequencyInfo>(*F, BPI, LI); 43 BFI = OwnedBFI.get(); 44 } 45 46 bool OptimizationRemarkEmitter::invalidate( 47 Function &F, const PreservedAnalyses &PA, 48 FunctionAnalysisManager::Invalidator &Inv) { 49 if (OwnedBFI.get()) { 50 OwnedBFI.reset(); 51 BFI = nullptr; 52 } 53 // This analysis has no state and so can be trivially preserved but it needs 54 // a fresh view of BFI if it was constructed with one. 55 if (BFI && Inv.invalidate<BlockFrequencyAnalysis>(F, PA)) 56 return true; 57 58 // Otherwise this analysis result remains valid. 59 return false; 60 } 61 62 Optional<uint64_t> OptimizationRemarkEmitter::computeHotness(const Value *V) { 63 if (!BFI) 64 return None; 65 66 return BFI->getBlockProfileCount(cast<BasicBlock>(V)); 67 } 68 69 void OptimizationRemarkEmitter::computeHotness( 70 DiagnosticInfoIROptimization &OptDiag) { 71 const Value *V = OptDiag.getCodeRegion(); 72 if (V) 73 OptDiag.setHotness(computeHotness(V)); 74 } 75 76 void OptimizationRemarkEmitter::emit( 77 DiagnosticInfoOptimizationBase &OptDiagBase) { 78 auto &OptDiag = cast<DiagnosticInfoIROptimization>(OptDiagBase); 79 computeHotness(OptDiag); 80 81 // Only emit it if its hotness meets the threshold. 82 if (OptDiag.getHotness().getValueOr(0) < 83 F->getContext().getDiagnosticsHotnessThreshold()) { 84 return; 85 } 86 87 F->getContext().diagnose(OptDiag); 88 } 89 90 OptimizationRemarkEmitterWrapperPass::OptimizationRemarkEmitterWrapperPass() 91 : FunctionPass(ID) { 92 initializeOptimizationRemarkEmitterWrapperPassPass( 93 *PassRegistry::getPassRegistry()); 94 } 95 96 bool OptimizationRemarkEmitterWrapperPass::runOnFunction(Function &Fn) { 97 BlockFrequencyInfo *BFI; 98 99 if (Fn.getContext().getDiagnosticsHotnessRequested()) 100 BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI(); 101 else 102 BFI = nullptr; 103 104 ORE = std::make_unique<OptimizationRemarkEmitter>(&Fn, BFI); 105 return false; 106 } 107 108 void OptimizationRemarkEmitterWrapperPass::getAnalysisUsage( 109 AnalysisUsage &AU) const { 110 LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU); 111 AU.setPreservesAll(); 112 } 113 114 AnalysisKey OptimizationRemarkEmitterAnalysis::Key; 115 116 OptimizationRemarkEmitter 117 OptimizationRemarkEmitterAnalysis::run(Function &F, 118 FunctionAnalysisManager &AM) { 119 BlockFrequencyInfo *BFI; 120 121 if (F.getContext().getDiagnosticsHotnessRequested()) 122 BFI = &AM.getResult<BlockFrequencyAnalysis>(F); 123 else 124 BFI = nullptr; 125 126 return OptimizationRemarkEmitter(&F, BFI); 127 } 128 129 char OptimizationRemarkEmitterWrapperPass::ID = 0; 130 static const char ore_name[] = "Optimization Remark Emitter"; 131 #define ORE_NAME "opt-remark-emitter" 132 133 INITIALIZE_PASS_BEGIN(OptimizationRemarkEmitterWrapperPass, ORE_NAME, ore_name, 134 false, true) 135 INITIALIZE_PASS_DEPENDENCY(LazyBFIPass) 136 INITIALIZE_PASS_END(OptimizationRemarkEmitterWrapperPass, ORE_NAME, ore_name, 137 false, true) 138