Skip to content

Commit 3cb5604

Browse files
authored
MachineOutliner: Use PM to query MachineModuleInfo (#99688)
Avoid getting this from the MachineFunction
1 parent 7fad04e commit 3cb5604

11 files changed

+64
-44
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,7 @@ class TargetInstrInfo : public MCInstrInfo {
20892089
/// information for a set of outlining candidates. Returns std::nullopt if the
20902090
/// candidates are not suitable for outlining.
20912091
virtual std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
2092+
const MachineModuleInfo &MMI,
20922093
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
20932094
llvm_unreachable(
20942095
"Target didn't implement TargetInstrInfo::getOutliningCandidateInfo!");
@@ -2103,16 +2104,18 @@ class TargetInstrInfo : public MCInstrInfo {
21032104
protected:
21042105
/// Target-dependent implementation for getOutliningTypeImpl.
21052106
virtual outliner::InstrType
2106-
getOutliningTypeImpl(MachineBasicBlock::iterator &MIT, unsigned Flags) const {
2107+
getOutliningTypeImpl(const MachineModuleInfo &MMI,
2108+
MachineBasicBlock::iterator &MIT, unsigned Flags) const {
21072109
llvm_unreachable(
21082110
"Target didn't implement TargetInstrInfo::getOutliningTypeImpl!");
21092111
}
21102112

21112113
public:
21122114
/// Returns how or if \p MIT should be outlined. \p Flags is the
21132115
/// target-specific information returned by isMBBSafeToOutlineFrom.
2114-
outliner::InstrType
2115-
getOutliningType(MachineBasicBlock::iterator &MIT, unsigned Flags) const;
2116+
outliner::InstrType getOutliningType(const MachineModuleInfo &MMI,
2117+
MachineBasicBlock::iterator &MIT,
2118+
unsigned Flags) const;
21162119

21172120
/// Optional target hook that returns true if \p MBB is safe to outline from,
21182121
/// and returns any target-specific information in \p Flags.

llvm/lib/CodeGen/MachineOutliner.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ namespace {
132132

133133
/// Maps \p MachineInstrs to unsigned integers and stores the mappings.
134134
struct InstructionMapper {
135+
const MachineModuleInfo &MMI;
135136

136137
/// The next available integer to assign to a \p MachineInstr that
137138
/// cannot be outlined.
@@ -333,7 +334,7 @@ struct InstructionMapper {
333334
// which may be outlinable. Check if each instruction is known to be safe.
334335
for (; It != OutlinableRangeEnd; ++It) {
335336
// Keep track of where this instruction is in the module.
336-
switch (TII.getOutliningType(It, Flags)) {
337+
switch (TII.getOutliningType(MMI, It, Flags)) {
337338
case InstrType::Illegal:
338339
mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB,
339340
InstrListForMBB);
@@ -382,7 +383,7 @@ struct InstructionMapper {
382383
}
383384
}
384385

385-
InstructionMapper() {
386+
InstructionMapper(const MachineModuleInfo &MMI_) : MMI(MMI_) {
386387
// Make sure that the implementation of DenseMapInfo<unsigned> hasn't
387388
// changed.
388389
assert(DenseMapInfo<unsigned>::getEmptyKey() == (unsigned)-1 &&
@@ -405,6 +406,8 @@ struct MachineOutliner : public ModulePass {
405406

406407
static char ID;
407408

409+
MachineModuleInfo *MMI = nullptr;
410+
408411
/// Set to true if the outliner should consider functions with
409412
/// linkonceodr linkage.
410413
bool OutlineFromLinkOnceODRs = false;
@@ -489,20 +492,19 @@ struct MachineOutliner : public ModulePass {
489492

490493
/// Populate and \p InstructionMapper with instruction-to-integer mappings.
491494
/// These are used to construct a suffix tree.
492-
void populateMapper(InstructionMapper &Mapper, Module &M,
493-
MachineModuleInfo &MMI);
495+
void populateMapper(InstructionMapper &Mapper, Module &M);
494496

495497
/// Initialize information necessary to output a size remark.
496498
/// FIXME: This should be handled by the pass manager, not the outliner.
497499
/// FIXME: This is nearly identical to the initSizeRemarkInfo in the legacy
498500
/// pass manager.
499-
void initSizeRemarkInfo(const Module &M, const MachineModuleInfo &MMI,
501+
void initSizeRemarkInfo(const Module &M,
500502
StringMap<unsigned> &FunctionToInstrCount);
501503

502504
/// Emit the remark.
503505
// FIXME: This should be handled by the pass manager, not the outliner.
504506
void
505-
emitInstrCountChangedRemark(const Module &M, const MachineModuleInfo &MMI,
507+
emitInstrCountChangedRemark(const Module &M,
506508
const StringMap<unsigned> &FunctionToInstrCount);
507509
};
508510
} // Anonymous namespace.
@@ -669,7 +671,7 @@ void MachineOutliner::findCandidates(
669671
CandidatesForRepeatedSeq[0].getMF()->getSubtarget().getInstrInfo();
670672

671673
std::optional<OutlinedFunction> OF =
672-
TII->getOutliningCandidateInfo(CandidatesForRepeatedSeq);
674+
TII->getOutliningCandidateInfo(*MMI, CandidatesForRepeatedSeq);
673675

674676
// If we deleted too many candidates, then there's nothing worth outlining.
675677
// FIXME: This should take target-specified instruction sizes into account.
@@ -988,8 +990,7 @@ bool MachineOutliner::outline(Module &M,
988990
return OutlinedSomething;
989991
}
990992

991-
void MachineOutliner::populateMapper(InstructionMapper &Mapper, Module &M,
992-
MachineModuleInfo &MMI) {
993+
void MachineOutliner::populateMapper(InstructionMapper &Mapper, Module &M) {
993994
// Build instruction mappings for each function in the module. Start by
994995
// iterating over each Function in M.
995996
LLVM_DEBUG(dbgs() << "*** Populating mapper ***\n");
@@ -1003,7 +1004,7 @@ void MachineOutliner::populateMapper(InstructionMapper &Mapper, Module &M,
10031004

10041005
// There's something in F. Check if it has a MachineFunction associated with
10051006
// it.
1006-
MachineFunction *MF = MMI.getMachineFunction(F);
1007+
MachineFunction *MF = MMI->getMachineFunction(F);
10071008

10081009
// If it doesn't, then there's nothing to outline from. Move to the next
10091010
// Function.
@@ -1062,12 +1063,11 @@ void MachineOutliner::populateMapper(InstructionMapper &Mapper, Module &M,
10621063
}
10631064

10641065
void MachineOutliner::initSizeRemarkInfo(
1065-
const Module &M, const MachineModuleInfo &MMI,
1066-
StringMap<unsigned> &FunctionToInstrCount) {
1066+
const Module &M, StringMap<unsigned> &FunctionToInstrCount) {
10671067
// Collect instruction counts for every function. We'll use this to emit
10681068
// per-function size remarks later.
10691069
for (const Function &F : M) {
1070-
MachineFunction *MF = MMI.getMachineFunction(F);
1070+
MachineFunction *MF = MMI->getMachineFunction(F);
10711071

10721072
// We only care about MI counts here. If there's no MachineFunction at this
10731073
// point, then there won't be after the outliner runs, so let's move on.
@@ -1078,13 +1078,12 @@ void MachineOutliner::initSizeRemarkInfo(
10781078
}
10791079

10801080
void MachineOutliner::emitInstrCountChangedRemark(
1081-
const Module &M, const MachineModuleInfo &MMI,
1082-
const StringMap<unsigned> &FunctionToInstrCount) {
1081+
const Module &M, const StringMap<unsigned> &FunctionToInstrCount) {
10831082
// Iterate over each function in the module and emit remarks.
10841083
// Note that we won't miss anything by doing this, because the outliner never
10851084
// deletes functions.
10861085
for (const Function &F : M) {
1087-
MachineFunction *MF = MMI.getMachineFunction(F);
1086+
MachineFunction *MF = MMI->getMachineFunction(F);
10881087

10891088
// The outliner never deletes functions. If we don't have a MF here, then we
10901089
// didn't have one prior to outlining either.
@@ -1135,6 +1134,8 @@ bool MachineOutliner::runOnModule(Module &M) {
11351134
if (M.empty())
11361135
return false;
11371136

1137+
MMI = &getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
1138+
11381139
// Number to append to the current outlined function.
11391140
unsigned OutlinedFunctionNum = 0;
11401141

@@ -1158,8 +1159,6 @@ bool MachineOutliner::runOnModule(Module &M) {
11581159
}
11591160

11601161
bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
1161-
MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
1162-
11631162
// If the user passed -enable-machine-outliner=always or
11641163
// -enable-machine-outliner, the pass will run on all functions in the module.
11651164
// Otherwise, if the target supports default outlining, it will run on all
@@ -1177,10 +1176,10 @@ bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
11771176
// If the user specifies that they want to outline from linkonceodrs, set
11781177
// it here.
11791178
OutlineFromLinkOnceODRs = EnableLinkOnceODROutlining;
1180-
InstructionMapper Mapper;
1179+
InstructionMapper Mapper(*MMI);
11811180

11821181
// Prepare instruction mappings for the suffix tree.
1183-
populateMapper(Mapper, M, MMI);
1182+
populateMapper(Mapper, M);
11841183
std::vector<OutlinedFunction> FunctionList;
11851184

11861185
// Find all of the outlining candidates.
@@ -1198,7 +1197,7 @@ bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
11981197
bool ShouldEmitSizeRemarks = M.shouldEmitInstrCountChangedRemark();
11991198
StringMap<unsigned> FunctionToInstrCount;
12001199
if (ShouldEmitSizeRemarks)
1201-
initSizeRemarkInfo(M, MMI, FunctionToInstrCount);
1200+
initSizeRemarkInfo(M, FunctionToInstrCount);
12021201

12031202
// Outline each of the candidates and return true if something was outlined.
12041203
bool OutlinedSomething =
@@ -1208,7 +1207,7 @@ bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
12081207
// module. If we've asked for size remarks, then output them.
12091208
// FIXME: This should be in the pass manager.
12101209
if (ShouldEmitSizeRemarks && OutlinedSomething)
1211-
emitInstrCountChangedRemark(M, MMI, FunctionToInstrCount);
1210+
emitInstrCountChangedRemark(M, FunctionToInstrCount);
12121211

12131212
LLVM_DEBUG({
12141213
if (!OutlinedSomething)

llvm/lib/CodeGen/TargetInstrInfo.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,15 +1809,17 @@ void TargetInstrInfo::mergeOutliningCandidateAttributes(
18091809
F.addFnAttr(Attribute::NoUnwind);
18101810
}
18111811

1812-
outliner::InstrType TargetInstrInfo::getOutliningType(
1813-
MachineBasicBlock::iterator &MIT, unsigned Flags) const {
1812+
outliner::InstrType
1813+
TargetInstrInfo::getOutliningType(const MachineModuleInfo &MMI,
1814+
MachineBasicBlock::iterator &MIT,
1815+
unsigned Flags) const {
18141816
MachineInstr &MI = *MIT;
18151817

18161818
// NOTE: MI.isMetaInstruction() will match CFI_INSTRUCTION, but some targets
18171819
// have support for outlining those. Special-case that here.
18181820
if (MI.isCFIInstruction())
18191821
// Just go right to the target implementation.
1820-
return getOutliningTypeImpl(MIT, Flags);
1822+
return getOutliningTypeImpl(MMI, MIT, Flags);
18211823

18221824
// Be conservative about inline assembly.
18231825
if (MI.isInlineAsm())
@@ -1883,7 +1885,7 @@ outliner::InstrType TargetInstrInfo::getOutliningType(
18831885
}
18841886

18851887
// If we don't know, delegate to the target-specific hook.
1886-
return getOutliningTypeImpl(MIT, Flags);
1888+
return getOutliningTypeImpl(MMI, MIT, Flags);
18871889
}
18881890

18891891
bool TargetInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,

llvm/lib/Target/AArch64/AArch64InstrInfo.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8286,6 +8286,7 @@ static bool outliningCandidatesV8_3OpsConsensus(const outliner::Candidate &a,
82868286

82878287
std::optional<outliner::OutlinedFunction>
82888288
AArch64InstrInfo::getOutliningCandidateInfo(
8289+
const MachineModuleInfo &MMI,
82898290
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
82908291
unsigned SequenceSize = 0;
82918292
for (auto &MI : RepeatedSequenceLocs[0])
@@ -8861,8 +8862,9 @@ AArch64InstrInfo::getOutlinableRanges(MachineBasicBlock &MBB,
88618862
}
88628863

88638864
outliner::InstrType
8864-
AArch64InstrInfo::getOutliningTypeImpl(MachineBasicBlock::iterator &MIT,
8865-
unsigned Flags) const {
8865+
AArch64InstrInfo::getOutliningTypeImpl(const MachineModuleInfo &MMI,
8866+
MachineBasicBlock::iterator &MIT,
8867+
unsigned Flags) const {
88668868
MachineInstr &MI = *MIT;
88678869
MachineBasicBlock *MBB = MI.getParent();
88688870
MachineFunction *MF = MBB->getParent();
@@ -8974,7 +8976,7 @@ AArch64InstrInfo::getOutliningTypeImpl(MachineBasicBlock::iterator &MIT,
89748976

89758977
// We have a function we have information about. Check it if it's something
89768978
// can safely outline.
8977-
MachineFunction *CalleeMF = MF->getMMI().getMachineFunction(*Callee);
8979+
MachineFunction *CalleeMF = MMI.getMachineFunction(*Callee);
89788980

89798981
// We don't know what's going on with the callee at all. Don't touch it.
89808982
if (!CalleeMF)

llvm/lib/Target/AArch64/AArch64InstrInfo.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,13 @@ class AArch64InstrInfo final : public AArch64GenInstrInfo {
466466
bool isFunctionSafeToOutlineFrom(MachineFunction &MF,
467467
bool OutlineFromLinkOnceODRs) const override;
468468
std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
469+
const MachineModuleInfo &MMI,
469470
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;
470471
void mergeOutliningCandidateAttributes(
471472
Function &F, std::vector<outliner::Candidate> &Candidates) const override;
472-
outliner::InstrType
473-
getOutliningTypeImpl(MachineBasicBlock::iterator &MIT, unsigned Flags) const override;
473+
outliner::InstrType getOutliningTypeImpl(const MachineModuleInfo &MMI,
474+
MachineBasicBlock::iterator &MIT,
475+
unsigned Flags) const override;
474476
SmallVector<
475477
std::pair<MachineBasicBlock::iterator, MachineBasicBlock::iterator>>
476478
getOutlinableRanges(MachineBasicBlock &MBB, unsigned &Flags) const override;

llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5873,6 +5873,7 @@ static bool isLRAvailable(const TargetRegisterInfo &TRI,
58735873

58745874
std::optional<outliner::OutlinedFunction>
58755875
ARMBaseInstrInfo::getOutliningCandidateInfo(
5876+
const MachineModuleInfo &MMI,
58765877
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
58775878
unsigned SequenceSize = 0;
58785879
for (auto &MI : RepeatedSequenceLocs[0])
@@ -6278,8 +6279,9 @@ bool ARMBaseInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
62786279
}
62796280

62806281
outliner::InstrType
6281-
ARMBaseInstrInfo::getOutliningTypeImpl(MachineBasicBlock::iterator &MIT,
6282-
unsigned Flags) const {
6282+
ARMBaseInstrInfo::getOutliningTypeImpl(const MachineModuleInfo &MMI,
6283+
MachineBasicBlock::iterator &MIT,
6284+
unsigned Flags) const {
62836285
MachineInstr &MI = *MIT;
62846286
const TargetRegisterInfo *TRI = &getRegisterInfo();
62856287

llvm/lib/Target/ARM/ARMBaseInstrInfo.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,13 @@ class ARMBaseInstrInfo : public ARMGenInstrInfo {
356356
bool isFunctionSafeToOutlineFrom(MachineFunction &MF,
357357
bool OutlineFromLinkOnceODRs) const override;
358358
std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
359+
const MachineModuleInfo &MMI,
359360
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;
360361
void mergeOutliningCandidateAttributes(
361362
Function &F, std::vector<outliner::Candidate> &Candidates) const override;
362-
outliner::InstrType getOutliningTypeImpl(MachineBasicBlock::iterator &MIT,
363-
unsigned Flags) const override;
363+
outliner::InstrType getOutliningTypeImpl(const MachineModuleInfo &MMI,
364+
MachineBasicBlock::iterator &MIT,
365+
unsigned Flags) const override;
364366
bool isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
365367
unsigned &Flags) const override;
366368
void buildOutlinedFrame(MachineBasicBlock &MBB, MachineFunction &MF,

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,6 +2830,7 @@ bool RISCVInstrInfo::shouldOutlineFromFunctionByDefault(
28302830

28312831
std::optional<outliner::OutlinedFunction>
28322832
RISCVInstrInfo::getOutliningCandidateInfo(
2833+
const MachineModuleInfo &MMI,
28332834
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
28342835

28352836
// First we need to filter out candidates where the X5 register (IE t0) can't
@@ -2868,8 +2869,9 @@ RISCVInstrInfo::getOutliningCandidateInfo(
28682869
}
28692870

28702871
outliner::InstrType
2871-
RISCVInstrInfo::getOutliningTypeImpl(MachineBasicBlock::iterator &MBBI,
2872-
unsigned Flags) const {
2872+
RISCVInstrInfo::getOutliningTypeImpl(const MachineModuleInfo &MMI,
2873+
MachineBasicBlock::iterator &MBBI,
2874+
unsigned Flags) const {
28732875
MachineInstr &MI = *MBBI;
28742876
MachineBasicBlock *MBB = MI.getParent();
28752877
const TargetRegisterInfo *TRI =

llvm/lib/Target/RISCV/RISCVInstrInfo.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,13 @@ class RISCVInstrInfo : public RISCVGenInstrInfo {
206206

207207
// Calculate target-specific information for a set of outlining candidates.
208208
std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
209+
const MachineModuleInfo &MMI,
209210
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;
210211

211212
// Return if/how a given MachineInstr should be outlined.
212213
virtual outliner::InstrType
213-
getOutliningTypeImpl(MachineBasicBlock::iterator &MBBI,
214+
getOutliningTypeImpl(const MachineModuleInfo &MMI,
215+
MachineBasicBlock::iterator &MBBI,
214216
unsigned Flags) const override;
215217

216218
// Insert a custom frame for outlined functions.

llvm/lib/Target/X86/X86InstrInfo.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10468,6 +10468,7 @@ enum MachineOutlinerClass { MachineOutlinerDefault, MachineOutlinerTailCall };
1046810468

1046910469
std::optional<outliner::OutlinedFunction>
1047010470
X86InstrInfo::getOutliningCandidateInfo(
10471+
const MachineModuleInfo &MMI,
1047110472
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
1047210473
unsigned SequenceSize = 0;
1047310474
for (auto &MI : RepeatedSequenceLocs[0]) {
@@ -10544,7 +10545,8 @@ bool X86InstrInfo::isFunctionSafeToOutlineFrom(
1054410545
}
1054510546

1054610547
outliner::InstrType
10547-
X86InstrInfo::getOutliningTypeImpl(MachineBasicBlock::iterator &MIT,
10548+
X86InstrInfo::getOutliningTypeImpl(const MachineModuleInfo &MMI,
10549+
MachineBasicBlock::iterator &MIT,
1054810550
unsigned Flags) const {
1054910551
MachineInstr &MI = *MIT;
1055010552

llvm/lib/Target/X86/X86InstrInfo.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,13 +585,15 @@ class X86InstrInfo final : public X86GenInstrInfo {
585585
getSerializableDirectMachineOperandTargetFlags() const override;
586586

587587
std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
588+
const MachineModuleInfo &MMI,
588589
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;
589590

590591
bool isFunctionSafeToOutlineFrom(MachineFunction &MF,
591592
bool OutlineFromLinkOnceODRs) const override;
592593

593-
outliner::InstrType
594-
getOutliningTypeImpl(MachineBasicBlock::iterator &MIT, unsigned Flags) const override;
594+
outliner::InstrType getOutliningTypeImpl(const MachineModuleInfo &MMI,
595+
MachineBasicBlock::iterator &MIT,
596+
unsigned Flags) const override;
595597

596598
void buildOutlinedFrame(MachineBasicBlock &MBB, MachineFunction &MF,
597599
const outliner::OutlinedFunction &OF) const override;

0 commit comments

Comments
 (0)