Skip to content

Commit d51a220

Browse files
boomanaiden154valadaptive
authored andcommitted
[NewPM][X86] Port X86ExpandPseudo to NPM (llvm#173463)
Porting this over and adding it to the pipeline means we only need to port AsmPrinter to fully lower very simple functions on X86.
1 parent 3f9635c commit d51a220

File tree

8 files changed

+92
-43
lines changed

8 files changed

+92
-43
lines changed

llvm/lib/Target/X86/X86.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@ FunctionPass *createX86WinEHStatePass();
141141
/// instructions into a sequence of actual instructions. This pass
142142
/// must run after prologue/epilogue insertion and before lowering
143143
/// the MachineInstr to MC.
144-
FunctionPass *createX86ExpandPseudoPass();
144+
class X86ExpandPseudoPass : public PassInfoMixin<X86ExpandPseudoPass> {
145+
public:
146+
PreservedAnalyses run(MachineFunction &MF,
147+
MachineFunctionAnalysisManager &MFAM);
148+
};
149+
150+
FunctionPass *createX86ExpandPseudoLegacyPass();
145151

146152
/// This pass converts X86 cmov instructions into branch when profitable.
147153
FunctionPass *createX86CmovConverterPass();
@@ -243,7 +249,7 @@ void initializeX86DAGToDAGISelLegacyPass(PassRegistry &);
243249
void initializeX86DomainReassignmentPass(PassRegistry &);
244250
void initializeX86DynAllocaExpanderLegacyPass(PassRegistry &);
245251
void initializeX86ExecutionDomainFixPass(PassRegistry &);
246-
void initializeX86ExpandPseudoPass(PassRegistry &);
252+
void initializeX86ExpandPseudoLegacyPass(PassRegistry &);
247253
void initializeX86FPStackifierLegacyPass(PassRegistry &);
248254
void initializeX86FastPreTileConfigPass(PassRegistry &);
249255
void initializeX86FastTileConfigPass(PassRegistry &);

llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class X86CodeGenPassBuilder
3232
void addPreISel(PassManagerWrapper &PMW) const;
3333
void addAsmPrinter(PassManagerWrapper &PMW, CreateMCStreamer) const;
3434
Error addInstSelector(PassManagerWrapper &PMW) const;
35+
void addPreSched2(PassManagerWrapper &PMW) const;
3536
};
3637

3738
void X86CodeGenPassBuilder::addPreISel(PassManagerWrapper &PMW) const {
@@ -49,6 +50,10 @@ Error X86CodeGenPassBuilder::addInstSelector(PassManagerWrapper &PMW) const {
4950
return Error::success();
5051
}
5152

53+
void X86CodeGenPassBuilder::addPreSched2(PassManagerWrapper &PMW) const {
54+
addMachineFunctionPass(X86ExpandPseudoPass(), PMW);
55+
}
56+
5257
} // namespace
5358

5459
void X86TargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {

llvm/lib/Target/X86/X86ExpandPseudo.cpp

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,32 @@
1818
#include "X86MachineFunctionInfo.h"
1919
#include "X86Subtarget.h"
2020
#include "llvm/CodeGen/LivePhysRegs.h"
21+
#include "llvm/CodeGen/MachineDominators.h"
22+
#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
2123
#include "llvm/CodeGen/MachineFunctionPass.h"
2224
#include "llvm/CodeGen/MachineInstrBuilder.h"
25+
#include "llvm/CodeGen/MachineLoopInfo.h"
26+
#include "llvm/CodeGen/MachinePassManager.h"
2327
#include "llvm/CodeGen/Passes.h" // For IDs of passes that are preserved.
28+
#include "llvm/IR/Analysis.h"
2429
#include "llvm/IR/EHPersonalities.h"
2530
#include "llvm/IR/GlobalValue.h"
2631
#include "llvm/Target/TargetMachine.h"
2732
using namespace llvm;
2833

29-
#define DEBUG_TYPE "x86-pseudo"
34+
#define DEBUG_TYPE "x86-expand-pseudo"
3035
#define X86_EXPAND_PSEUDO_NAME "X86 pseudo instruction expansion pass"
3136

3237
namespace {
33-
class X86ExpandPseudo : public MachineFunctionPass {
38+
class X86ExpandPseudoImpl {
3439
public:
35-
static char ID;
36-
X86ExpandPseudo() : MachineFunctionPass(ID) {}
37-
38-
void getAnalysisUsage(AnalysisUsage &AU) const override {
39-
AU.setPreservesCFG();
40-
AU.addPreservedID(MachineLoopInfoID);
41-
AU.addPreservedID(MachineDominatorsID);
42-
MachineFunctionPass::getAnalysisUsage(AU);
43-
}
44-
4540
const X86Subtarget *STI = nullptr;
4641
const X86InstrInfo *TII = nullptr;
4742
const X86RegisterInfo *TRI = nullptr;
4843
const X86MachineFunctionInfo *X86FI = nullptr;
4944
const X86FrameLowering *X86FL = nullptr;
5045

51-
bool runOnMachineFunction(MachineFunction &MF) override;
52-
53-
MachineFunctionProperties getRequiredProperties() const override {
54-
return MachineFunctionProperties().setNoVRegs();
55-
}
56-
57-
StringRef getPassName() const override {
58-
return "X86 pseudo instruction expansion pass";
59-
}
46+
bool runOnMachineFunction(MachineFunction &MF);
6047

6148
private:
6249
void expandICallBranchFunnel(MachineBasicBlock *MBB,
@@ -78,14 +65,42 @@ class X86ExpandPseudo : public MachineFunctionPass {
7865
MachineBasicBlock *EntryBlk,
7966
MachineBasicBlock::iterator VAStartPseudoInstr) const;
8067
};
81-
char X86ExpandPseudo::ID = 0;
8268

69+
class X86ExpandPseudoLegacy : public MachineFunctionPass {
70+
public:
71+
static char ID;
72+
X86ExpandPseudoLegacy() : MachineFunctionPass(ID) {}
73+
74+
void getAnalysisUsage(AnalysisUsage &AU) const override {
75+
AU.setPreservesCFG();
76+
AU.addPreservedID(MachineLoopInfoID);
77+
AU.addPreservedID(MachineDominatorsID);
78+
MachineFunctionPass::getAnalysisUsage(AU);
79+
}
80+
81+
const X86Subtarget *STI = nullptr;
82+
const X86InstrInfo *TII = nullptr;
83+
const X86RegisterInfo *TRI = nullptr;
84+
const X86MachineFunctionInfo *X86FI = nullptr;
85+
const X86FrameLowering *X86FL = nullptr;
86+
87+
bool runOnMachineFunction(MachineFunction &MF) override;
88+
89+
MachineFunctionProperties getRequiredProperties() const override {
90+
return MachineFunctionProperties().setNoVRegs();
91+
}
92+
93+
StringRef getPassName() const override {
94+
return "X86 pseudo instruction expansion pass";
95+
}
96+
};
97+
char X86ExpandPseudoLegacy::ID = 0;
8398
} // End anonymous namespace.
8499

85-
INITIALIZE_PASS(X86ExpandPseudo, DEBUG_TYPE, X86_EXPAND_PSEUDO_NAME, false,
86-
false)
100+
INITIALIZE_PASS(X86ExpandPseudoLegacy, DEBUG_TYPE, X86_EXPAND_PSEUDO_NAME,
101+
false, false)
87102

88-
void X86ExpandPseudo::expandICallBranchFunnel(
103+
void X86ExpandPseudoImpl::expandICallBranchFunnel(
89104
MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI) {
90105
MachineBasicBlock *JTMBB = MBB;
91106
MachineInstr *JTInst = &*MBBI;
@@ -186,8 +201,8 @@ void X86ExpandPseudo::expandICallBranchFunnel(
186201
JTMBB->erase(JTInst);
187202
}
188203

189-
void X86ExpandPseudo::expandCALL_RVMARKER(MachineBasicBlock &MBB,
190-
MachineBasicBlock::iterator MBBI) {
204+
void X86ExpandPseudoImpl::expandCALL_RVMARKER(
205+
MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) {
191206
// Expand CALL_RVMARKER pseudo to call instruction, followed by the special
192207
//"movq %rax, %rdi" marker.
193208
MachineInstr &MI = *MBBI;
@@ -257,8 +272,8 @@ void X86ExpandPseudo::expandCALL_RVMARKER(MachineBasicBlock &MBB,
257272
/// If \p MBBI is a pseudo instruction, this method expands
258273
/// it to the corresponding (sequence of) actual instruction(s).
259274
/// \returns true if \p MBBI has been expanded.
260-
bool X86ExpandPseudo::expandMI(MachineBasicBlock &MBB,
261-
MachineBasicBlock::iterator MBBI) {
275+
bool X86ExpandPseudoImpl::expandMI(MachineBasicBlock &MBB,
276+
MachineBasicBlock::iterator MBBI) {
262277
MachineInstr &MI = *MBBI;
263278
unsigned Opcode = MI.getOpcode();
264279
const DebugLoc &DL = MBBI->getDebugLoc();
@@ -813,7 +828,7 @@ bool X86ExpandPseudo::expandMI(MachineBasicBlock &MBB,
813828
// | |
814829
// | |
815830
//
816-
void X86ExpandPseudo::expandVastartSaveXmmRegs(
831+
void X86ExpandPseudoImpl::expandVastartSaveXmmRegs(
817832
MachineBasicBlock *EntryBlk,
818833
MachineBasicBlock::iterator VAStartPseudoInstr) const {
819834
assert(VAStartPseudoInstr->getOpcode() == X86::VASTART_SAVE_XMM_REGS);
@@ -898,7 +913,7 @@ void X86ExpandPseudo::expandVastartSaveXmmRegs(
898913

899914
/// Expand all pseudo instructions contained in \p MBB.
900915
/// \returns true if any expansion occurred for \p MBB.
901-
bool X86ExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
916+
bool X86ExpandPseudoImpl::expandMBB(MachineBasicBlock &MBB) {
902917
bool Modified = false;
903918

904919
// MBBI may be invalidated by the expansion.
@@ -912,7 +927,8 @@ bool X86ExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
912927
return Modified;
913928
}
914929

915-
bool X86ExpandPseudo::expandPseudosWhichAffectControlFlow(MachineFunction &MF) {
930+
bool X86ExpandPseudoImpl::expandPseudosWhichAffectControlFlow(
931+
MachineFunction &MF) {
916932
// Currently pseudo which affects control flow is only
917933
// X86::VASTART_SAVE_XMM_REGS which is located in Entry block.
918934
// So we do not need to evaluate other blocks.
@@ -926,7 +942,7 @@ bool X86ExpandPseudo::expandPseudosWhichAffectControlFlow(MachineFunction &MF) {
926942
return false;
927943
}
928944

929-
bool X86ExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
945+
bool X86ExpandPseudoImpl::runOnMachineFunction(MachineFunction &MF) {
930946
STI = &MF.getSubtarget<X86Subtarget>();
931947
TII = STI->getInstrInfo();
932948
TRI = STI->getRegisterInfo();
@@ -941,6 +957,24 @@ bool X86ExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
941957
}
942958

943959
/// Returns an instance of the pseudo instruction expansion pass.
944-
FunctionPass *llvm::createX86ExpandPseudoPass() {
945-
return new X86ExpandPseudo();
960+
FunctionPass *llvm::createX86ExpandPseudoLegacyPass() {
961+
return new X86ExpandPseudoLegacy();
962+
}
963+
964+
bool X86ExpandPseudoLegacy::runOnMachineFunction(MachineFunction &MF) {
965+
X86ExpandPseudoImpl Impl;
966+
return Impl.runOnMachineFunction(MF);
967+
}
968+
969+
PreservedAnalyses
970+
X86ExpandPseudoPass::run(MachineFunction &MF,
971+
MachineFunctionAnalysisManager &MFAM) {
972+
X86ExpandPseudoImpl Impl;
973+
bool Changed = Impl.runOnMachineFunction(MF);
974+
if (!Changed)
975+
return PreservedAnalyses::all();
976+
977+
PreservedAnalyses PA = getMachineFunctionPassPreservedAnalyses();
978+
PA.preserveSet<CFGAnalyses>();
979+
return PA;
946980
}

llvm/lib/Target/X86/X86PassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ DUMMY_FUNCTION_PASS("x86-winehstate", WinEHStatePass())
3131
#endif
3232
MACHINE_FUNCTION_PASS("x86-avoid-trailing-call", X86AvoidTrailingCallPass())
3333
MACHINE_FUNCTION_PASS("x86-dyn-alloca-expander", X86DynAllocaExpanderPass())
34+
MACHINE_FUNCTION_PASS("x86-expand-pseudo", X86ExpandPseudoPass())
3435
MACHINE_FUNCTION_PASS("x86-fp-stackifier", X86FPStackifierPass())
3536
MACHINE_FUNCTION_PASS("x86-isel", X86ISelDAGToDAGPass(*this))
3637
#undef MACHINE_FUNCTION_PASS
@@ -56,7 +57,6 @@ DUMMY_MACHINE_FUNCTION_PASS("x86-lower-tile-copy", X86LowerTileCopy())
5657
DUMMY_MACHINE_FUNCTION_PASS("x86-lvi-load", X86LoadValueInjectionLoadHardeningPass())
5758
DUMMY_MACHINE_FUNCTION_PASS("x86-lvi-ret", X86LoadValueInjectionRetHardeningPass())
5859
DUMMY_MACHINE_FUNCTION_PASS("x86-optimize-LEAs", X86OptimizeLEAPass())
59-
DUMMY_MACHINE_FUNCTION_PASS("x86-pseudo", X86ExpandPseudo())
6060
DUMMY_MACHINE_FUNCTION_PASS("x86-return-thunks", X86ReturnThunks())
6161
DUMMY_MACHINE_FUNCTION_PASS("x86-seses", X86SpeculativeExecutionSideEffectSuppression())
6262
DUMMY_MACHINE_FUNCTION_PASS("x86-slh", X86SpeculativeLoadHardeningPass())

llvm/lib/Target/X86/X86TargetMachine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ extern "C" LLVM_C_ABI void LLVMInitializeX86Target() {
8585
initializeX86FastTileConfigPass(PR);
8686
initializeKCFIPass(PR);
8787
initializeX86LowerTileCopyPass(PR);
88-
initializeX86ExpandPseudoPass(PR);
88+
initializeX86ExpandPseudoLegacyPass(PR);
8989
initializeX86ExecutionDomainFixPass(PR);
9090
initializeX86DomainReassignmentPass(PR);
9191
initializeX86AvoidSFBPassPass(PR);
@@ -541,7 +541,7 @@ void X86PassConfig::addPostRegAlloc() {
541541
}
542542

543543
void X86PassConfig::addPreSched2() {
544-
addPass(createX86ExpandPseudoPass());
544+
addPass(createX86ExpandPseudoLegacyPass());
545545
addPass(createKCFIPass());
546546
}
547547

llvm/test/CodeGen/X86/expand-call-rvmarker.mir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# RUN: llc -o - -run-pass=x86-pseudo -verify-machineinstrs %s | FileCheck %s
1+
# RUN: llc -o - -run-pass=x86-expand-pseudo -verify-machineinstrs %s | FileCheck %s
2+
# RUN: llc -o - -passes=x86-expand-pseudo -verify-machineinstrs %s | FileCheck %s
23

34
--- |
45
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

llvm/test/CodeGen/X86/tailcall-pseudo-64.mir

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#RUN: llc -verify-machineinstrs -mtriple=x86_64-apple-darwin -o - -run-pass=x86-pseudo %s | FileCheck %s
1+
#RUN: llc -verify-machineinstrs -mtriple=x86_64-apple-darwin -o - -run-pass=x86-expand-pseudo %s | FileCheck %s
2+
#RUN: llc -verify-machineinstrs -mtriple=x86_64-apple-darwin -o - -passes=x86-expand-pseudo %s | FileCheck %s
3+
24
---
35
name: tail_call_fail_64
46
tracksRegLiveness: true

llvm/test/CodeGen/X86/tailcall-pseudo.mir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#RUN: llc -verify-machineinstrs -mtriple=i386-apple-darwin -o - -run-pass=x86-pseudo %s | FileCheck %s
1+
#RUN: llc -verify-machineinstrs -mtriple=i386-apple-darwin -o - -run-pass=x86-expand-pseudo %s | FileCheck %s
2+
#RUN: llc -verify-machineinstrs -mtriple=i386-apple-darwin -o - -passes=x86-expand-pseudo %s | FileCheck %s
23
---
34
name: tail_call_fail
45
tracksRegLiveness: true

0 commit comments

Comments
 (0)