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"
4851#include < bitset>
4952using namespace llvm ;
5053
51- #define DEBUG_TYPE " x86-codegen "
54+ #define DEBUG_TYPE " x86-fp-stackifier "
5255
5356STATISTIC (NumFXCH, " Number of fxch instructions inserted" );
5457STATISTIC (NumFP, " Number of floating point instructions" );
5558
5659namespace {
5760const 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
7967private:
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 )
300313INITIALIZE_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+ }
0 commit comments