Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions llvm/lib/Target/X86/X86.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ FunctionPass *createCleanupLocalDynamicTLSPass();
/// This function returns a pass which converts floating-point register
/// references and pseudo instructions into floating-point stack references and
/// physical instructions.
FunctionPass *createX86FloatingPointStackifierPass();
class X86FPStackifierPass : public PassInfoMixin<X86FPStackifierPass> {
public:
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);
};

FunctionPass *createX86FPStackifierLegacyPass();

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

void initializeCompressEVEXPassPass(PassRegistry &);
void initializeFPSPass(PassRegistry &);
void initializeFixupBWInstPassPass(PassRegistry &);
void initializeFixupLEAPassPass(PassRegistry &);
void initializeX86ArgumentStackSlotPassPass(PassRegistry &);
Expand All @@ -246,6 +251,7 @@ void initializeX86DomainReassignmentPass(PassRegistry &);
void initializeX86DynAllocaExpanderLegacyPass(PassRegistry &);
void initializeX86ExecutionDomainFixPass(PassRegistry &);
void initializeX86ExpandPseudoPass(PassRegistry &);
void initializeX86FPStackifierLegacyPass(PassRegistry &);
void initializeX86FastPreTileConfigPass(PassRegistry &);
void initializeX86FastTileConfigPass(PassRegistry &);
void initializeX86FixupSetCCPassPass(PassRegistry &);
Expand Down
115 changes: 77 additions & 38 deletions llvm/lib/Target/X86/X86FloatingPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/EdgeBundles.h"
#include "llvm/CodeGen/LiveRegUnits.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/Analysis.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/InitializePasses.h"
#include "llvm/Support/Debug.h"
Expand All @@ -48,33 +51,18 @@
#include <bitset>
using namespace llvm;

#define DEBUG_TYPE "x86-codegen"
#define DEBUG_TYPE "x86-fp-stackifier"

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

namespace {
const unsigned ScratchFPReg = 7;

struct FPS : public MachineFunctionPass {
static char ID;
FPS() : MachineFunctionPass(ID) {}

void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
AU.addRequired<EdgeBundlesWrapperLegacy>();
AU.addPreservedID(MachineLoopInfoID);
AU.addPreservedID(MachineDominatorsID);
MachineFunctionPass::getAnalysisUsage(AU);
}

bool runOnMachineFunction(MachineFunction &MF) override;

MachineFunctionProperties getRequiredProperties() const override {
return MachineFunctionProperties().setNoVRegs();
}

StringRef getPassName() const override { return "X86 FP Stackifier"; }
class FPS {
public:
bool shouldRun(MachineFunction &MF);
bool run(MachineFunction &MF, EdgeBundles *EdgeBundles);

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

void setKillFlags(MachineBasicBlock &MBB) const;
};

class X86FPStackifierLegacy : public MachineFunctionPass {
public:
X86FPStackifierLegacy() : MachineFunctionPass(ID) {}

static char ID;

private:
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
AU.addRequired<EdgeBundlesWrapperLegacy>();
AU.addPreservedID(MachineLoopInfoID);
AU.addPreservedID(MachineDominatorsID);
MachineFunctionPass::getAnalysisUsage(AU);
}

bool runOnMachineFunction(MachineFunction &MF) override;

MachineFunctionProperties getRequiredProperties() const override {
return MachineFunctionProperties().setNoVRegs();
}

StringRef getPassName() const override { return "X86 FP Stackifier"; }
};
} // namespace

char FPS::ID = 0;
char X86FPStackifierLegacy::ID = 0;

INITIALIZE_PASS_BEGIN(FPS, DEBUG_TYPE, "X86 FP Stackifier", false, false)
INITIALIZE_PASS_BEGIN(X86FPStackifierLegacy, DEBUG_TYPE, "X86 FP Stackifier",
false, false)
INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy)
INITIALIZE_PASS_END(FPS, DEBUG_TYPE, "X86 FP Stackifier", false, false)
INITIALIZE_PASS_END(X86FPStackifierLegacy, DEBUG_TYPE, "X86 FP Stackifier",
false, false)

FunctionPass *llvm::createX86FloatingPointStackifierPass() { return new FPS(); }
FunctionPass *llvm::createX86FPStackifierLegacyPass() {
return new X86FPStackifierLegacy();
}

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

/// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
/// register references into FP stack references.
///
bool FPS::runOnMachineFunction(MachineFunction &MF) {
bool FPS::shouldRun(MachineFunction &MF) {
// We only need to run this pass if there are any FP registers used in this
// function. If it is all integer, there is nothing for us to do!
bool FPIsUsed = false;

static_assert(X86::FP6 == X86::FP0 + 6,
"Register enums aren't sorted right!");
const MachineRegisterInfo &MRI = MF.getRegInfo();
for (unsigned i = 0; i <= 6; ++i)
if (!MRI.reg_nodbg_empty(X86::FP0 + i)) {
FPIsUsed = true;
break;
for (unsigned I = 0; I <= 6; ++I)
if (!MRI.reg_nodbg_empty(X86::FP0 + I)) {
return true;
}

// Early exit.
if (!FPIsUsed)
return false;
return false;
}

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

// Prepare cross-MBB liveness.
Expand Down Expand Up @@ -1812,3 +1825,29 @@ void FPS::setKillFlags(MachineBasicBlock &MBB) const {
LPR.stepBackward(MI);
}
}

bool X86FPStackifierLegacy::runOnMachineFunction(MachineFunction &MF) {
FPS Impl;
if (!Impl.shouldRun(MF))
return false;

EdgeBundles *Bundles =
&getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
return FPS().run(MF, Bundles);
}

PreservedAnalyses
X86FPStackifierPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
FPS Impl;
if (!Impl.shouldRun(MF))
return PreservedAnalyses::all();

EdgeBundles *Bundles = &MFAM.getResult<EdgeBundlesAnalysis>(MF);
bool Changed = Impl.run(MF, Bundles);
if (!Changed)
return PreservedAnalyses::all();
PreservedAnalyses PA = PreservedAnalyses::none();
PA.preserveSet<CFGAnalyses>();
return PA;
}
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/X86PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ DUMMY_FUNCTION_PASS("x86-winehstate", WinEHStatePass())
#endif
MACHINE_FUNCTION_PASS("x86-avoid-trailing-call", X86AvoidTrailingCallPass())
MACHINE_FUNCTION_PASS("x86-dyn-alloca-expander", X86DynAllocaExpanderPass())
MACHINE_FUNCTION_PASS("x86-fp-stackifier", X86FPStackifierPass())
MACHINE_FUNCTION_PASS("x86-isel", X86ISelDAGToDAGPass(*this))
#undef MACHINE_FUNCTION_PASS

Expand All @@ -40,7 +41,6 @@ MACHINE_FUNCTION_PASS("x86-isel", X86ISelDAGToDAGPass(*this))
DUMMY_MACHINE_FUNCTION_PASS("x86-avoid-SFB", X86AvoidSFBPass())
DUMMY_MACHINE_FUNCTION_PASS("x86-cf-opt", X86CallFrameOptimization())
DUMMY_MACHINE_FUNCTION_PASS("x86-cmov-conversion", X86CmovConverterPass())
DUMMY_MACHINE_FUNCTION_PASS("x86-codege", FPS())
DUMMY_MACHINE_FUNCTION_PASS("x86-compress-evex", CompressEVEXPass())
DUMMY_MACHINE_FUNCTION_PASS("x86-domain-reassignment", X86DomainReassignment())
DUMMY_MACHINE_FUNCTION_PASS("x86-execution-domain-fix", X86ExecutionDomainFix())
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/X86/X86TargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ extern "C" LLVM_C_ABI void LLVMInitializeX86Target() {
initializeFixupBWInstPassPass(PR);
initializeCompressEVEXPassPass(PR);
initializeFixupLEAPassPass(PR);
initializeFPSPass(PR);
initializeX86FPStackifierLegacyPass(PR);
initializeX86FixupSetCCPassPass(PR);
initializeX86CallFrameOptimizationPass(PR);
initializeX86CmovConverterPassPass(PR);
Expand Down Expand Up @@ -531,7 +531,7 @@ void X86PassConfig::addMachineSSAOptimization() {

void X86PassConfig::addPostRegAlloc() {
addPass(createX86LowerTileCopyPass());
addPass(createX86FloatingPointStackifierPass());
addPass(createX86FPStackifierLegacyPass());
// When -O0 is enabled, the Load Value Injection Hardening pass will fall back
// to using the Speculative Execution Side Effect Suppression pass for
// mitigation. This is to prevent slow downs due to
Expand Down
3 changes: 2 additions & 1 deletion llvm/test/CodeGen/X86/x87-stack-pop.mir
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
# RUN: llc -mtriple=i686-- -run-pass x86-codegen -O2 -o - %s | FileCheck %s
# RUN: llc -mtriple=i686-- -run-pass=x86-fp-stackifier -O2 -o - %s | FileCheck %s
# RUN: llc -mtriple=i686-- -passes=x86-fp-stackifier -O2 -o - %s | FileCheck %s

---
name: func_fxam
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# RUN: llc %s -run-pass=x86-codegen -o - -experimental-debug-variable-locations | FileCheck %s --implicit-check-not=debug-instr-number
# RUN: llc %s -run-pass=x86-fp-stackifier -o - -experimental-debug-variable-locations | FileCheck %s --implicit-check-not=debug-instr-number
# RUN: llc %s -passes=x86-fp-stackifier -o - -experimental-debug-variable-locations | FileCheck %s --implicit-check-not=debug-instr-number
#
# The x87 FP instructions below have debug instr numbers attached -- but the
# operands get rewritten when it's converted to stack-form. Rather than trying
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-exegesis/lib/X86/Target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ constexpr MCPhysReg kDefaultLoopCounterReg = X86::R8;

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

MCRegister ExegesisX86Target::getScratchMemoryRegister(const Triple &TT) const {
Expand Down
Loading