Index: llvm/include/llvm/CodeGen/AsmPrinter.h =================================================================== --- llvm/include/llvm/CodeGen/AsmPrinter.h +++ llvm/include/llvm/CodeGen/AsmPrinter.h @@ -50,6 +50,7 @@ class GlobalVariable; class MachineBasicBlock; class MachineConstantPoolValue; +class MachineDominatorTree; class MachineFunction; class MachineInstr; class MachineJumpTableInfo; @@ -92,11 +93,17 @@ std::unique_ptr OutStreamer; /// The current machine function. - const MachineFunction *MF = nullptr; + MachineFunction *MF = nullptr; /// This is a pointer to the current MachineModuleInfo. MachineModuleInfo *MMI = nullptr; + /// This is a pointer to the current MachineLoopInfo. + MachineDominatorTree *MDT = nullptr; + + /// This is a pointer to the current MachineLoopInfo. + MachineLoopInfo *MLI = nullptr; + /// Optimization remark emitter. MachineOptimizationRemarkEmitter *ORE; @@ -130,9 +137,6 @@ static char ID; - /// If VerboseAsm is set, a pointer to the loop info for this function. - MachineLoopInfo *LI = nullptr; - struct HandlerInfo { AsmPrinterHandler *Handler; const char *TimerName; @@ -161,6 +165,12 @@ }; private: + /// If generated on the fly this own the instance. + std::unique_ptr OwnedMDT; + + /// If generated on the fly this own the instance. + std::unique_ptr OwnedMLI; + /// Structure for generating diagnostics for inline assembly. Only initialised /// when necessary. mutable std::unique_ptr DiagInfo; Index: llvm/include/llvm/MC/MCCodePadder.h =================================================================== --- llvm/include/llvm/MC/MCCodePadder.h +++ llvm/include/llvm/MC/MCCodePadder.h @@ -28,7 +28,6 @@ struct MCCodePaddingContext { bool IsPaddingActive; - bool IsBasicBlockInsideInnermostLoop; bool IsBasicBlockReachableViaFallthrough; bool IsBasicBlockReachableViaBranch; }; Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -39,6 +39,7 @@ #include "llvm/CodeGen/GCStrategy.h" #include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/CodeGen/MachineConstantPool.h" +#include "llvm/CodeGen/MachineDominators.h" #include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineFunction.h" #include "llvm/CodeGen/MachineFunctionPass.h" @@ -238,7 +239,6 @@ AU.addRequired(); AU.addRequired(); AU.addRequired(); - AU.addRequired(); } bool AsmPrinter::doInitialization(Module &M) { @@ -1009,6 +1009,24 @@ bool ShouldPrintDebugScopes = MMI->hasDebugInfo(); + if (isVerbose()) { + // Get MachineDominatorTree or compute it on the fly if it's unavailable + MDT = getAnalysisIfAvailable(); + if (!MDT) { + OwnedMDT = make_unique(); + OwnedMDT->getBase().recalculate(*MF); + MDT = OwnedMDT.get(); + } + + // Get MachineLoopInfo or compute it on the fly if it's unavailable + MLI = getAnalysisIfAvailable(); + if (!MLI) { + OwnedMLI = make_unique(); + OwnedMLI->getBase().analyze(MDT->getBase()); + MLI = OwnedMLI.get(); + } + } + // Print out code for the function. bool HasAnyRealCode = false; int NumInstsInFunction = 0; @@ -1489,6 +1507,8 @@ OutStreamer->Finish(); OutStreamer->reset(); + OwnedMLI.reset(); + OwnedMDT.reset(); return false; } @@ -1515,7 +1535,6 @@ } ORE = &getAnalysis().getORE(); - LI = &getAnalysis(); const TargetSubtargetInfo &STI = MF.getSubtarget(); EnablePrintSchedInfo = PrintSchedule.getNumOccurrences() @@ -2697,13 +2716,9 @@ void AsmPrinter::setupCodePaddingContext(const MachineBasicBlock &MBB, MCCodePaddingContext &Context) const { assert(MF != nullptr && "Machine function must be valid"); - assert(LI != nullptr && "Loop info must be valid"); Context.IsPaddingActive = !MF->hasInlineAsm() && !MF->getFunction().optForSize() && TM.getOptLevel() != CodeGenOpt::None; - const MachineLoop *CurrentLoop = LI->getLoopFor(&MBB); - Context.IsBasicBlockInsideInnermostLoop = - CurrentLoop != nullptr && CurrentLoop->getSubLoops().empty(); Context.IsBasicBlockReachableViaFallthrough = std::find(MBB.pred_begin(), MBB.pred_end(), MBB.getPrevNode()) != MBB.pred_end(); @@ -2755,7 +2770,9 @@ OutStreamer->GetCommentOS() << '\n'; } } - emitBasicBlockLoopComments(MBB, LI, *this); + + assert(MLI != nullptr && "MachineLoopInfo should has been computed"); + emitBasicBlockLoopComments(MBB, MLI, *this); } // Print the main label for the block. Index: llvm/test/CodeGen/AArch64/O0-pipeline.ll =================================================================== --- llvm/test/CodeGen/AArch64/O0-pipeline.ll +++ llvm/test/CodeGen/AArch64/O0-pipeline.ll @@ -59,8 +59,6 @@ ; CHECK-NEXT: Implement the 'patchable-function' attribute ; CHECK-NEXT: Lazy Machine Block Frequency Analysis ; CHECK-NEXT: Machine Optimization Remark Emitter -; CHECK-NEXT: MachineDominator Tree Construction -; CHECK-NEXT: Machine Natural Loop Construction ; CHECK-NEXT: AArch64 Assembly Printer ; CHECK-NEXT: Free MachineFunction Index: llvm/test/CodeGen/AArch64/O3-pipeline.ll =================================================================== --- llvm/test/CodeGen/AArch64/O3-pipeline.ll +++ llvm/test/CodeGen/AArch64/O3-pipeline.ll @@ -156,8 +156,6 @@ ; CHECK-NEXT: Implement the 'patchable-function' attribute ; CHECK-NEXT: Lazy Machine Block Frequency Analysis ; CHECK-NEXT: Machine Optimization Remark Emitter -; CHECK-NEXT: MachineDominator Tree Construction -; CHECK-NEXT: Machine Natural Loop Construction ; CHECK-NEXT: AArch64 Assembly Printer ; CHECK-NEXT: Free MachineFunction ; CHECK-NEXT: Pass Arguments: -domtree Index: llvm/test/CodeGen/AArch64/arm64-opt-remarks-lazy-bfi.ll =================================================================== --- llvm/test/CodeGen/AArch64/arm64-opt-remarks-lazy-bfi.ll +++ llvm/test/CodeGen/AArch64/arm64-opt-remarks-lazy-bfi.ll @@ -36,9 +36,7 @@ ; HOTNESS-NOT: Executing Pass ; HOTNESS: block-frequency: empty_func ; HOTNESS-NOT: Executing Pass -; HOTNESS: Executing Pass 'MachineDominator Tree Construction' -; HOTNESS-NEXT: Executing Pass 'Machine Natural Loop Construction' -; HOTNESS-NEXT: Executing Pass 'AArch64 Assembly Printer' +; HOTNESS: Executing Pass 'AArch64 Assembly Printer' ; HOTNESS: arm64-summary-remarks.ll:5:0: 1 instructions in function (hotness: 33) @@ -47,8 +45,6 @@ ; NO_HOTNESS-NEXT: Freeing Pass 'Implement the 'patchable-function' attribute' ; NO_HOTNESS-NEXT: Executing Pass 'Lazy Machine Block Frequency Analysis' ; NO_HOTNESS-NEXT: Executing Pass 'Machine Optimization Remark Emitter' -; NO_HOTNESS-NEXT: Executing Pass 'MachineDominator Tree Construction' -; NO_HOTNESS-NEXT: Executing Pass 'Machine Natural Loop Construction' ; NO_HOTNESS-NEXT: Executing Pass 'AArch64 Assembly Printer' ; NO_HOTNESS: arm64-summary-remarks.ll:5:0: 1 instructions in function{{$}} Index: llvm/test/CodeGen/X86/O0-pipeline.ll =================================================================== --- llvm/test/CodeGen/X86/O0-pipeline.ll +++ llvm/test/CodeGen/X86/O0-pipeline.ll @@ -61,8 +61,6 @@ ; CHECK-NEXT: X86 Retpoline Thunks ; CHECK-NEXT: Lazy Machine Block Frequency Analysis ; CHECK-NEXT: Machine Optimization Remark Emitter -; CHECK-NEXT: MachineDominator Tree Construction -; CHECK-NEXT: Machine Natural Loop Construction ; CHECK-NEXT: X86 Assembly Printer ; CHECK-NEXT: Free MachineFunction Index: llvm/test/CodeGen/X86/O3-pipeline.ll =================================================================== --- llvm/test/CodeGen/X86/O3-pipeline.ll +++ llvm/test/CodeGen/X86/O3-pipeline.ll @@ -160,8 +160,6 @@ ; CHECK-NEXT: X86 Retpoline Thunks ; CHECK-NEXT: Lazy Machine Block Frequency Analysis ; CHECK-NEXT: Machine Optimization Remark Emitter -; CHECK-NEXT: MachineDominator Tree Construction -; CHECK-NEXT: Machine Natural Loop Construction ; CHECK-NEXT: X86 Assembly Printer ; CHECK-NEXT: Free MachineFunction