Skip to content

Commit

Permalink
Remove MachineLoopInfo dependency from AsmPrinter.
Browse files Browse the repository at this point in the history
Summary:
Currently MachineLoopInfo is used in only two places:
1) for computing IsBasicBlockInsideInnermostLoop field of MCCodePaddingContext, and it is never used.
2) in emitBasicBlockLoopComments, which is called only if `isVerbose()` is true.
Despite that, we currently have a dependency on MachineLoopInfo, which makes
pass manager to compute it and MachineDominator Tree. This patch removes the
use (1) and makes the use (2) lazy, thus avoiding some redundant
recomputations.

Reviewers: opaparo, gadi.haber, rafael, craig.topper, zvi

Subscribers: rengolin, javed.absar, hiraditya, llvm-commits

Differential Revision: https://reviews.llvm.org/D44812

llvm-svn: 329542
  • Loading branch information
Michael Zolotukhin committed Apr 9, 2018
1 parent ba0543b commit 8d052a0
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 25 deletions.
18 changes: 14 additions & 4 deletions llvm/include/llvm/CodeGen/AsmPrinter.h
Expand Up @@ -50,6 +50,7 @@ class GlobalValue;
class GlobalVariable;
class MachineBasicBlock;
class MachineConstantPoolValue;
class MachineDominatorTree;
class MachineFunction;
class MachineInstr;
class MachineJumpTableInfo;
Expand Down Expand Up @@ -92,11 +93,17 @@ class AsmPrinter : public MachineFunctionPass {
std::unique_ptr<MCStreamer> 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;

Expand Down Expand Up @@ -130,9 +137,6 @@ class AsmPrinter : public MachineFunctionPass {

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;
Expand Down Expand Up @@ -161,6 +165,12 @@ class AsmPrinter : public MachineFunctionPass {
};

private:
/// If generated on the fly this own the instance.
std::unique_ptr<MachineDominatorTree> OwnedMDT;

/// If generated on the fly this own the instance.
std::unique_ptr<MachineLoopInfo> OwnedMLI;

/// Structure for generating diagnostics for inline assembly. Only initialised
/// when necessary.
mutable std::unique_ptr<SrcMgrDiagInfo> DiagInfo;
Expand Down
1 change: 0 additions & 1 deletion llvm/include/llvm/MC/MCCodePadder.h
Expand Up @@ -28,7 +28,6 @@ typedef SmallVector<const MCPaddingFragment *, 8> MCPFRange;

struct MCCodePaddingContext {
bool IsPaddingActive;
bool IsBasicBlockInsideInnermostLoop;
bool IsBasicBlockReachableViaFallthrough;
bool IsBasicBlockReachableViaBranch;
};
Expand Down
31 changes: 24 additions & 7 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -238,7 +239,6 @@ void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<MachineModuleInfo>();
AU.addRequired<MachineOptimizationRemarkEmitterPass>();
AU.addRequired<GCModuleInfo>();
AU.addRequired<MachineLoopInfo>();
}

bool AsmPrinter::doInitialization(Module &M) {
Expand Down Expand Up @@ -1009,6 +1009,24 @@ void AsmPrinter::EmitFunctionBody() {

bool ShouldPrintDebugScopes = MMI->hasDebugInfo();

if (isVerbose()) {
// Get MachineDominatorTree or compute it on the fly if it's unavailable
MDT = getAnalysisIfAvailable<MachineDominatorTree>();
if (!MDT) {
OwnedMDT = make_unique<MachineDominatorTree>();
OwnedMDT->getBase().recalculate(*MF);
MDT = OwnedMDT.get();
}

// Get MachineLoopInfo or compute it on the fly if it's unavailable
MLI = getAnalysisIfAvailable<MachineLoopInfo>();
if (!MLI) {
OwnedMLI = make_unique<MachineLoopInfo>();
OwnedMLI->getBase().analyze(MDT->getBase());
MLI = OwnedMLI.get();
}
}

// Print out code for the function.
bool HasAnyRealCode = false;
int NumInstsInFunction = 0;
Expand Down Expand Up @@ -1489,6 +1507,8 @@ bool AsmPrinter::doFinalization(Module &M) {

OutStreamer->Finish();
OutStreamer->reset();
OwnedMLI.reset();
OwnedMDT.reset();

return false;
}
Expand All @@ -1515,7 +1535,6 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
}

ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
LI = &getAnalysis<MachineLoopInfo>();

const TargetSubtargetInfo &STI = MF.getSubtarget();
EnablePrintSchedInfo = PrintSchedule.getNumOccurrences()
Expand Down Expand Up @@ -2697,13 +2716,9 @@ static void emitBasicBlockLoopComments(const MachineBasicBlock &MBB,
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();
Expand Down Expand Up @@ -2755,7 +2770,9 @@ void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock &MBB) const {
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.
Expand Down
2 changes: 0 additions & 2 deletions llvm/test/CodeGen/AArch64/O0-pipeline.ll
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions llvm/test/CodeGen/AArch64/O3-pipeline.ll
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions llvm/test/CodeGen/AArch64/arm64-opt-remarks-lazy-bfi.ll
Expand Up @@ -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)

Expand All @@ -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{{$}}
Expand Down
2 changes: 0 additions & 2 deletions llvm/test/CodeGen/X86/O0-pipeline.ll
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions llvm/test/CodeGen/X86/O3-pipeline.ll
Expand Up @@ -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

Expand Down

0 comments on commit 8d052a0

Please sign in to comment.