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