Skip to content

Commit c44bd37

Browse files
[X86][NewPM] Port X86 FP Stackifier Pass to NewPM
Reviewers: arsenm, RKSimon, paperchalice, phoebewang Reviewed By: arsenm, RKSimon Pull Request: #167911
1 parent c40779a commit c44bd37

File tree

7 files changed

+93
-46
lines changed

7 files changed

+93
-46
lines changed

llvm/lib/Target/X86/X86.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ FunctionPass *createCleanupLocalDynamicTLSPass();
4444
/// This function returns a pass which converts floating-point register
4545
/// references and pseudo instructions into floating-point stack references and
4646
/// physical instructions.
47-
FunctionPass *createX86FloatingPointStackifierPass();
47+
class X86FPStackifierPass : public PassInfoMixin<X86FPStackifierPass> {
48+
public:
49+
PreservedAnalyses run(MachineFunction &MF,
50+
MachineFunctionAnalysisManager &MFAM);
51+
};
52+
53+
FunctionPass *createX86FPStackifierLegacyPass();
4854

4955
/// This pass inserts AVX vzeroupper instructions before each call to avoid
5056
/// transition penalty between functions encoded with AVX and SSE.
@@ -229,7 +235,6 @@ FunctionPass *createX86ArgumentStackSlotPass();
229235
FunctionPass *createX86SuppressAPXForRelocationPass();
230236

231237
void initializeCompressEVEXPassPass(PassRegistry &);
232-
void initializeFPSPass(PassRegistry &);
233238
void initializeFixupBWInstPassPass(PassRegistry &);
234239
void initializeFixupLEAPassPass(PassRegistry &);
235240
void initializeX86ArgumentStackSlotPassPass(PassRegistry &);
@@ -246,6 +251,7 @@ void initializeX86DomainReassignmentPass(PassRegistry &);
246251
void initializeX86DynAllocaExpanderLegacyPass(PassRegistry &);
247252
void initializeX86ExecutionDomainFixPass(PassRegistry &);
248253
void initializeX86ExpandPseudoPass(PassRegistry &);
254+
void initializeX86FPStackifierLegacyPass(PassRegistry &);
249255
void initializeX86FastPreTileConfigPass(PassRegistry &);
250256
void initializeX86FastTileConfigPass(PassRegistry &);
251257
void initializeX86FixupSetCCPassPass(PassRegistry &);

llvm/lib/Target/X86/X86FloatingPoint.cpp

Lines changed: 77 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@
3131
#include "llvm/ADT/Statistic.h"
3232
#include "llvm/CodeGen/EdgeBundles.h"
3333
#include "llvm/CodeGen/LiveRegUnits.h"
34+
#include "llvm/CodeGen/MachineFunction.h"
35+
#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
3436
#include "llvm/CodeGen/MachineFunctionPass.h"
3537
#include "llvm/CodeGen/MachineInstrBuilder.h"
3638
#include "llvm/CodeGen/MachineRegisterInfo.h"
3739
#include "llvm/CodeGen/Passes.h"
3840
#include "llvm/CodeGen/TargetInstrInfo.h"
3941
#include "llvm/CodeGen/TargetSubtargetInfo.h"
4042
#include "llvm/Config/llvm-config.h"
43+
#include "llvm/IR/Analysis.h"
4144
#include "llvm/IR/InlineAsm.h"
4245
#include "llvm/InitializePasses.h"
4346
#include "llvm/Support/Debug.h"
@@ -48,33 +51,18 @@
4851
#include <bitset>
4952
using namespace llvm;
5053

51-
#define DEBUG_TYPE "x86-codegen"
54+
#define DEBUG_TYPE "x86-fp-stackifier"
5255

5356
STATISTIC(NumFXCH, "Number of fxch instructions inserted");
5457
STATISTIC(NumFP, "Number of floating point instructions");
5558

5659
namespace {
5760
const unsigned ScratchFPReg = 7;
5861

59-
struct FPS : public MachineFunctionPass {
60-
static char ID;
61-
FPS() : MachineFunctionPass(ID) {}
62-
63-
void getAnalysisUsage(AnalysisUsage &AU) const override {
64-
AU.setPreservesCFG();
65-
AU.addRequired<EdgeBundlesWrapperLegacy>();
66-
AU.addPreservedID(MachineLoopInfoID);
67-
AU.addPreservedID(MachineDominatorsID);
68-
MachineFunctionPass::getAnalysisUsage(AU);
69-
}
70-
71-
bool runOnMachineFunction(MachineFunction &MF) override;
72-
73-
MachineFunctionProperties getRequiredProperties() const override {
74-
return MachineFunctionProperties().setNoVRegs();
75-
}
76-
77-
StringRef getPassName() const override { return "X86 FP Stackifier"; }
62+
class FPS {
63+
public:
64+
bool shouldRun(MachineFunction &MF);
65+
bool run(MachineFunction &MF, EdgeBundles *EdgeBundles);
7866

7967
private:
8068
const TargetInstrInfo *TII = nullptr; // Machine instruction info.
@@ -292,15 +280,43 @@ struct FPS : public MachineFunctionPass {
292280

293281
void setKillFlags(MachineBasicBlock &MBB) const;
294282
};
283+
284+
class X86FPStackifierLegacy : public MachineFunctionPass {
285+
public:
286+
X86FPStackifierLegacy() : MachineFunctionPass(ID) {}
287+
288+
static char ID;
289+
290+
private:
291+
void getAnalysisUsage(AnalysisUsage &AU) const override {
292+
AU.setPreservesCFG();
293+
AU.addRequired<EdgeBundlesWrapperLegacy>();
294+
AU.addPreservedID(MachineLoopInfoID);
295+
AU.addPreservedID(MachineDominatorsID);
296+
MachineFunctionPass::getAnalysisUsage(AU);
297+
}
298+
299+
bool runOnMachineFunction(MachineFunction &MF) override;
300+
301+
MachineFunctionProperties getRequiredProperties() const override {
302+
return MachineFunctionProperties().setNoVRegs();
303+
}
304+
305+
StringRef getPassName() const override { return "X86 FP Stackifier"; }
306+
};
295307
} // namespace
296308

297-
char FPS::ID = 0;
309+
char X86FPStackifierLegacy::ID = 0;
298310

299-
INITIALIZE_PASS_BEGIN(FPS, DEBUG_TYPE, "X86 FP Stackifier", false, false)
311+
INITIALIZE_PASS_BEGIN(X86FPStackifierLegacy, DEBUG_TYPE, "X86 FP Stackifier",
312+
false, false)
300313
INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy)
301-
INITIALIZE_PASS_END(FPS, DEBUG_TYPE, "X86 FP Stackifier", false, false)
314+
INITIALIZE_PASS_END(X86FPStackifierLegacy, DEBUG_TYPE, "X86 FP Stackifier",
315+
false, false)
302316

303-
FunctionPass *llvm::createX86FloatingPointStackifierPass() { return new FPS(); }
317+
FunctionPass *llvm::createX86FPStackifierLegacyPass() {
318+
return new X86FPStackifierLegacy();
319+
}
304320

305321
/// getFPReg - Return the X86::FPx register number for the specified operand.
306322
/// For example, this returns 3 for X86::FP3.
@@ -311,28 +327,25 @@ static unsigned getFPReg(const MachineOperand &MO) {
311327
return Reg - X86::FP0;
312328
}
313329

314-
/// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
315-
/// register references into FP stack references.
316-
///
317-
bool FPS::runOnMachineFunction(MachineFunction &MF) {
330+
bool FPS::shouldRun(MachineFunction &MF) {
318331
// We only need to run this pass if there are any FP registers used in this
319332
// function. If it is all integer, there is nothing for us to do!
320-
bool FPIsUsed = false;
321-
322333
static_assert(X86::FP6 == X86::FP0 + 6,
323334
"Register enums aren't sorted right!");
324335
const MachineRegisterInfo &MRI = MF.getRegInfo();
325-
for (unsigned i = 0; i <= 6; ++i)
326-
if (!MRI.reg_nodbg_empty(X86::FP0 + i)) {
327-
FPIsUsed = true;
328-
break;
336+
for (unsigned I = 0; I <= 6; ++I)
337+
if (!MRI.reg_nodbg_empty(X86::FP0 + I)) {
338+
return true;
329339
}
330340

331-
// Early exit.
332-
if (!FPIsUsed)
333-
return false;
341+
return false;
342+
}
334343

335-
Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
344+
/// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
345+
/// register references into FP stack references.
346+
///
347+
bool FPS::run(MachineFunction &MF, EdgeBundles *FunctionBundles) {
348+
Bundles = FunctionBundles;
336349
TII = MF.getSubtarget().getInstrInfo();
337350

338351
// Prepare cross-MBB liveness.
@@ -1812,3 +1825,29 @@ void FPS::setKillFlags(MachineBasicBlock &MBB) const {
18121825
LPR.stepBackward(MI);
18131826
}
18141827
}
1828+
1829+
bool X86FPStackifierLegacy::runOnMachineFunction(MachineFunction &MF) {
1830+
FPS Impl;
1831+
if (!Impl.shouldRun(MF))
1832+
return false;
1833+
1834+
EdgeBundles *Bundles =
1835+
&getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
1836+
return FPS().run(MF, Bundles);
1837+
}
1838+
1839+
PreservedAnalyses
1840+
X86FPStackifierPass::run(MachineFunction &MF,
1841+
MachineFunctionAnalysisManager &MFAM) {
1842+
FPS Impl;
1843+
if (!Impl.shouldRun(MF))
1844+
return PreservedAnalyses::all();
1845+
1846+
EdgeBundles *Bundles = &MFAM.getResult<EdgeBundlesAnalysis>(MF);
1847+
bool Changed = Impl.run(MF, Bundles);
1848+
if (!Changed)
1849+
return PreservedAnalyses::all();
1850+
PreservedAnalyses PA = PreservedAnalyses::none();
1851+
PA.preserveSet<CFGAnalyses>();
1852+
return PA;
1853+
}

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-fp-stackifier", X86FPStackifierPass())
3435
MACHINE_FUNCTION_PASS("x86-isel", X86ISelDAGToDAGPass(*this))
3536
#undef MACHINE_FUNCTION_PASS
3637

@@ -40,7 +41,6 @@ MACHINE_FUNCTION_PASS("x86-isel", X86ISelDAGToDAGPass(*this))
4041
DUMMY_MACHINE_FUNCTION_PASS("x86-avoid-SFB", X86AvoidSFBPass())
4142
DUMMY_MACHINE_FUNCTION_PASS("x86-cf-opt", X86CallFrameOptimization())
4243
DUMMY_MACHINE_FUNCTION_PASS("x86-cmov-conversion", X86CmovConverterPass())
43-
DUMMY_MACHINE_FUNCTION_PASS("x86-codege", FPS())
4444
DUMMY_MACHINE_FUNCTION_PASS("x86-compress-evex", CompressEVEXPass())
4545
DUMMY_MACHINE_FUNCTION_PASS("x86-domain-reassignment", X86DomainReassignment())
4646
DUMMY_MACHINE_FUNCTION_PASS("x86-execution-domain-fix", X86ExecutionDomainFix())

llvm/lib/Target/X86/X86TargetMachine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ extern "C" LLVM_C_ABI void LLVMInitializeX86Target() {
7676
initializeFixupBWInstPassPass(PR);
7777
initializeCompressEVEXPassPass(PR);
7878
initializeFixupLEAPassPass(PR);
79-
initializeFPSPass(PR);
79+
initializeX86FPStackifierLegacyPass(PR);
8080
initializeX86FixupSetCCPassPass(PR);
8181
initializeX86CallFrameOptimizationPass(PR);
8282
initializeX86CmovConverterPassPass(PR);
@@ -531,7 +531,7 @@ void X86PassConfig::addMachineSSAOptimization() {
531531

532532
void X86PassConfig::addPostRegAlloc() {
533533
addPass(createX86LowerTileCopyPass());
534-
addPass(createX86FloatingPointStackifierPass());
534+
addPass(createX86FPStackifierLegacyPass());
535535
// When -O0 is enabled, the Load Value Injection Hardening pass will fall back
536536
// to using the Speculative Execution Side Effect Suppression pass for
537537
// mitigation. This is to prevent slow downs due to

llvm/test/CodeGen/X86/x87-stack-pop.mir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2-
# RUN: llc -mtriple=i686-- -run-pass x86-codegen -O2 -o - %s | FileCheck %s
2+
# RUN: llc -mtriple=i686-- -run-pass=x86-fp-stackifier -O2 -o - %s | FileCheck %s
3+
# RUN: llc -mtriple=i686-- -passes=x86-fp-stackifier -O2 -o - %s | FileCheck %s
34

45
---
56
name: func_fxam

llvm/test/DebugInfo/MIR/InstrRef/x86-fp-stackifier-drop-locations.mir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# RUN: llc %s -run-pass=x86-codegen -o - -experimental-debug-variable-locations | FileCheck %s --implicit-check-not=debug-instr-number
1+
# RUN: llc %s -run-pass=x86-fp-stackifier -o - -experimental-debug-variable-locations | FileCheck %s --implicit-check-not=debug-instr-number
2+
# RUN: llc %s -passes=x86-fp-stackifier -o - -experimental-debug-variable-locations | FileCheck %s --implicit-check-not=debug-instr-number
23
#
34
# The x87 FP instructions below have debug instr numbers attached -- but the
45
# operands get rewritten when it's converted to stack-form. Rather than trying

llvm/tools/llvm-exegesis/lib/X86/Target.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ constexpr MCPhysReg kDefaultLoopCounterReg = X86::R8;
869869

870870
void ExegesisX86Target::addTargetSpecificPasses(PassManagerBase &PM) const {
871871
// Lowers FP pseudo-instructions, e.g. ABS_Fp32 -> ABS_F.
872-
PM.add(createX86FloatingPointStackifierPass());
872+
PM.add(createX86FPStackifierLegacyPass());
873873
}
874874

875875
MCRegister ExegesisX86Target::getScratchMemoryRegister(const Triple &TT) const {

0 commit comments

Comments
 (0)