diff --git a/llvm/lib/CodeGen/SelectOptimize.cpp b/llvm/lib/CodeGen/SelectOptimize.cpp index ca46e8aa7d424f..b5a8a80193adbe 100644 --- a/llvm/lib/CodeGen/SelectOptimize.cpp +++ b/llvm/lib/CodeGen/SelectOptimize.cpp @@ -555,6 +555,12 @@ void SelectOptimize::findProfitableSIGroupsBase(SelectGroups &SIGroups, } } +static void EmitAndPrintRemark(OptimizationRemarkEmitter *ORE, + DiagnosticInfoOptimizationBase &Rem) { + LLVM_DEBUG(dbgs() << Rem.getMsg() << "\n"); + ORE->emit(Rem); +} + void SelectOptimize::findProfitableSIGroupsInnerLoops( const Loop *L, SelectGroups &SIGroups, SelectGroups &ProfSIGroups) { NumSelectOptAnalyzed += SIGroups.size(); @@ -589,7 +595,7 @@ void SelectOptimize::findProfitableSIGroupsInnerLoops( OR << "Profitable to convert to branch (loop analysis). BranchCost=" << BranchCost.toString() << ", SelectCost=" << SelectCost.toString() << ". "; - ORE->emit(OR); + EmitAndPrintRemark(ORE, OR); ++NumSelectConvertedLoop; ProfSIGroups.push_back(ASI); } else { @@ -597,7 +603,7 @@ void SelectOptimize::findProfitableSIGroupsInnerLoops( ORmiss << "Select is more profitable (loop analysis). BranchCost=" << BranchCost.toString() << ", SelectCost=" << SelectCost.toString() << ". "; - ORE->emit(ORmiss); + EmitAndPrintRemark(ORE, ORmiss); } } } @@ -605,6 +611,7 @@ void SelectOptimize::findProfitableSIGroupsInnerLoops( bool SelectOptimize::isConvertToBranchProfitableBase( const SmallVector &ASI) { SelectInst *SI = ASI.front(); + LLVM_DEBUG(dbgs() << "Analyzing select group containing " << *SI << "\n"); OptimizationRemark OR(DEBUG_TYPE, "SelectOpti", SI); OptimizationRemarkMissed ORmiss(DEBUG_TYPE, "SelectOpti", SI); @@ -612,7 +619,7 @@ bool SelectOptimize::isConvertToBranchProfitableBase( if (PSI->isColdBlock(SI->getParent(), BFI.get())) { ++NumSelectColdBB; ORmiss << "Not converted to branch because of cold basic block. "; - ORE->emit(ORmiss); + EmitAndPrintRemark(ORE, ORmiss); return false; } @@ -620,7 +627,7 @@ bool SelectOptimize::isConvertToBranchProfitableBase( if (SI->getMetadata(LLVMContext::MD_unpredictable)) { ++NumSelectUnPred; ORmiss << "Not converted to branch because of unpredictable branch. "; - ORE->emit(ORmiss); + EmitAndPrintRemark(ORE, ORmiss); return false; } @@ -629,7 +636,7 @@ bool SelectOptimize::isConvertToBranchProfitableBase( if (isSelectHighlyPredictable(SI) && TLI->isPredictableSelectExpensive()) { ++NumSelectConvertedHighPred; OR << "Converted to branch because of highly predictable branch. "; - ORE->emit(OR); + EmitAndPrintRemark(ORE, OR); return true; } @@ -638,12 +645,12 @@ bool SelectOptimize::isConvertToBranchProfitableBase( if (hasExpensiveColdOperand(ASI)) { ++NumSelectConvertedExpColdOperand; OR << "Converted to branch because of expensive cold operand."; - ORE->emit(OR); + EmitAndPrintRemark(ORE, OR); return true; } ORmiss << "Not profitable to convert to branch (base heuristic)."; - ORE->emit(ORmiss); + EmitAndPrintRemark(ORE, ORmiss); return false; } @@ -665,7 +672,7 @@ bool SelectOptimize::hasExpensiveColdOperand( OptimizationRemarkMissed ORmiss(DEBUG_TYPE, "SelectOpti", ASI.front()); ORmiss << "Profile data available but missing branch-weights metadata for " "select instruction. "; - ORE->emit(ORmiss); + EmitAndPrintRemark(ORE, ORmiss); } if (!ColdOperand) return false; @@ -801,7 +808,7 @@ bool SelectOptimize::checkLoopHeuristics(const Loop *L, LoopCost[1].NonPredCost >= LoopCost[1].PredCost) { ORmissL << "No select conversion in the loop due to no reduction of loop's " "critical path. "; - ORE->emit(ORmissL); + EmitAndPrintRemark(ORE, ORmissL); return false; } @@ -818,7 +825,7 @@ bool SelectOptimize::checkLoopHeuristics(const Loop *L, "loop's critical path. Gain=" << Gain[1].toString() << ", RelativeGain=" << RelativeGain.toString() << "%. "; - ORE->emit(ORmissL); + EmitAndPrintRemark(ORE, ORmissL); return false; } @@ -834,7 +841,7 @@ bool SelectOptimize::checkLoopHeuristics(const Loop *L, ORmissL << "No select conversion in the loop due to small gradient gain. " "GradientGain=" << GradientGain.toString() << "%. "; - ORE->emit(ORmissL); + EmitAndPrintRemark(ORE, ORmissL); return false; } } @@ -842,7 +849,7 @@ bool SelectOptimize::checkLoopHeuristics(const Loop *L, else if (Gain[1] < Gain[0]) { ORmissL << "No select conversion in the loop due to negative gradient gain. "; - ORE->emit(ORmissL); + EmitAndPrintRemark(ORE, ORmissL); return false; } @@ -858,6 +865,8 @@ bool SelectOptimize::checkLoopHeuristics(const Loop *L, bool SelectOptimize::computeLoopCosts( const Loop *L, const SelectGroups &SIGroups, DenseMap &InstCostMap, CostInfo *LoopCost) { + LLVM_DEBUG(dbgs() << "Calculating Latency / IPredCost / INonPredCost of loop " + << L->getHeader()->getName() << "\n"); const auto &SIset = getSIset(SIGroups); // Compute instruction and loop-critical-path costs across two iterations for // both predicated and non-predicated version. @@ -891,7 +900,7 @@ bool SelectOptimize::computeLoopCosts( ORmissL << "Invalid instruction cost preventing analysis and " "optimization of the inner-most loop containing this " "instruction. "; - ORE->emit(ORmissL); + EmitAndPrintRemark(ORE, ORmissL); return false; } IPredCost += Scaled64::get(ILatency.value()); @@ -925,12 +934,17 @@ bool SelectOptimize::computeLoopCosts( INonPredCost = PredictedPathCost + MispredictCost; } + LLVM_DEBUG(dbgs() << " " << ILatency << "/" << IPredCost << "/" + << INonPredCost << " for " << I << "\n"); InstCostMap[&I] = {IPredCost, INonPredCost}; MaxCost.PredCost = std::max(MaxCost.PredCost, IPredCost); MaxCost.NonPredCost = std::max(MaxCost.NonPredCost, INonPredCost); } } + LLVM_DEBUG(dbgs() << "Iteration " << Iter + 1 + << " MaxCost = " << MaxCost.PredCost << " " + << MaxCost.NonPredCost << "\n"); } return true; }