106f32e7eSjoerg //===-- X86FloatingPoint.cpp - Floating point Reg -> Stack converter ------===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This file defines the pass which converts floating point instructions from
1006f32e7eSjoerg // pseudo registers into register stack instructions.  This pass uses live
1106f32e7eSjoerg // variable information to indicate where the FPn registers are used and their
1206f32e7eSjoerg // lifetimes.
1306f32e7eSjoerg //
1406f32e7eSjoerg // The x87 hardware tracks liveness of the stack registers, so it is necessary
1506f32e7eSjoerg // to implement exact liveness tracking between basic blocks. The CFG edges are
1606f32e7eSjoerg // partitioned into bundles where the same FP registers must be live in
1706f32e7eSjoerg // identical stack positions. Instructions are inserted at the end of each basic
1806f32e7eSjoerg // block to rearrange the live registers to match the outgoing bundle.
1906f32e7eSjoerg //
2006f32e7eSjoerg // This approach avoids splitting critical edges at the potential cost of more
2106f32e7eSjoerg // live register shuffling instructions when critical edges are present.
2206f32e7eSjoerg //
2306f32e7eSjoerg //===----------------------------------------------------------------------===//
2406f32e7eSjoerg 
2506f32e7eSjoerg #include "X86.h"
2606f32e7eSjoerg #include "X86InstrInfo.h"
2706f32e7eSjoerg #include "llvm/ADT/DepthFirstIterator.h"
2806f32e7eSjoerg #include "llvm/ADT/STLExtras.h"
2906f32e7eSjoerg #include "llvm/ADT/SmallPtrSet.h"
3006f32e7eSjoerg #include "llvm/ADT/SmallSet.h"
3106f32e7eSjoerg #include "llvm/ADT/SmallVector.h"
3206f32e7eSjoerg #include "llvm/ADT/Statistic.h"
3306f32e7eSjoerg #include "llvm/CodeGen/EdgeBundles.h"
3406f32e7eSjoerg #include "llvm/CodeGen/LivePhysRegs.h"
3506f32e7eSjoerg #include "llvm/CodeGen/MachineFunctionPass.h"
3606f32e7eSjoerg #include "llvm/CodeGen/MachineInstrBuilder.h"
3706f32e7eSjoerg #include "llvm/CodeGen/MachineRegisterInfo.h"
3806f32e7eSjoerg #include "llvm/CodeGen/Passes.h"
3906f32e7eSjoerg #include "llvm/CodeGen/TargetInstrInfo.h"
4006f32e7eSjoerg #include "llvm/CodeGen/TargetSubtargetInfo.h"
4106f32e7eSjoerg #include "llvm/Config/llvm-config.h"
4206f32e7eSjoerg #include "llvm/IR/InlineAsm.h"
43*da58b97aSjoerg #include "llvm/InitializePasses.h"
4406f32e7eSjoerg #include "llvm/Support/Debug.h"
4506f32e7eSjoerg #include "llvm/Support/ErrorHandling.h"
4606f32e7eSjoerg #include "llvm/Support/raw_ostream.h"
4706f32e7eSjoerg #include "llvm/Target/TargetMachine.h"
4806f32e7eSjoerg #include <algorithm>
4906f32e7eSjoerg #include <bitset>
5006f32e7eSjoerg using namespace llvm;
5106f32e7eSjoerg 
5206f32e7eSjoerg #define DEBUG_TYPE "x86-codegen"
5306f32e7eSjoerg 
5406f32e7eSjoerg STATISTIC(NumFXCH, "Number of fxch instructions inserted");
5506f32e7eSjoerg STATISTIC(NumFP  , "Number of floating point instructions");
5606f32e7eSjoerg 
5706f32e7eSjoerg namespace {
5806f32e7eSjoerg   const unsigned ScratchFPReg = 7;
5906f32e7eSjoerg 
6006f32e7eSjoerg   struct FPS : public MachineFunctionPass {
6106f32e7eSjoerg     static char ID;
FPS__anond501d9200111::FPS6206f32e7eSjoerg     FPS() : MachineFunctionPass(ID) {
6306f32e7eSjoerg       // This is really only to keep valgrind quiet.
6406f32e7eSjoerg       // The logic in isLive() is too much for it.
6506f32e7eSjoerg       memset(Stack, 0, sizeof(Stack));
6606f32e7eSjoerg       memset(RegMap, 0, sizeof(RegMap));
6706f32e7eSjoerg     }
6806f32e7eSjoerg 
getAnalysisUsage__anond501d9200111::FPS6906f32e7eSjoerg     void getAnalysisUsage(AnalysisUsage &AU) const override {
7006f32e7eSjoerg       AU.setPreservesCFG();
7106f32e7eSjoerg       AU.addRequired<EdgeBundles>();
7206f32e7eSjoerg       AU.addPreservedID(MachineLoopInfoID);
7306f32e7eSjoerg       AU.addPreservedID(MachineDominatorsID);
7406f32e7eSjoerg       MachineFunctionPass::getAnalysisUsage(AU);
7506f32e7eSjoerg     }
7606f32e7eSjoerg 
7706f32e7eSjoerg     bool runOnMachineFunction(MachineFunction &MF) override;
7806f32e7eSjoerg 
getRequiredProperties__anond501d9200111::FPS7906f32e7eSjoerg     MachineFunctionProperties getRequiredProperties() const override {
8006f32e7eSjoerg       return MachineFunctionProperties().set(
8106f32e7eSjoerg           MachineFunctionProperties::Property::NoVRegs);
8206f32e7eSjoerg     }
8306f32e7eSjoerg 
getPassName__anond501d9200111::FPS8406f32e7eSjoerg     StringRef getPassName() const override { return "X86 FP Stackifier"; }
8506f32e7eSjoerg 
8606f32e7eSjoerg   private:
87*da58b97aSjoerg     const TargetInstrInfo *TII = nullptr; // Machine instruction info.
8806f32e7eSjoerg 
8906f32e7eSjoerg     // Two CFG edges are related if they leave the same block, or enter the same
9006f32e7eSjoerg     // block. The transitive closure of an edge under this relation is a
9106f32e7eSjoerg     // LiveBundle. It represents a set of CFG edges where the live FP stack
9206f32e7eSjoerg     // registers must be allocated identically in the x87 stack.
9306f32e7eSjoerg     //
9406f32e7eSjoerg     // A LiveBundle is usually all the edges leaving a block, or all the edges
9506f32e7eSjoerg     // entering a block, but it can contain more edges if critical edges are
9606f32e7eSjoerg     // present.
9706f32e7eSjoerg     //
9806f32e7eSjoerg     // The set of live FP registers in a LiveBundle is calculated by bundleCFG,
9906f32e7eSjoerg     // but the exact mapping of FP registers to stack slots is fixed later.
10006f32e7eSjoerg     struct LiveBundle {
10106f32e7eSjoerg       // Bit mask of live FP registers. Bit 0 = FP0, bit 1 = FP1, &c.
10206f32e7eSjoerg       unsigned Mask;
10306f32e7eSjoerg 
10406f32e7eSjoerg       // Number of pre-assigned live registers in FixStack. This is 0 when the
10506f32e7eSjoerg       // stack order has not yet been fixed.
10606f32e7eSjoerg       unsigned FixCount;
10706f32e7eSjoerg 
10806f32e7eSjoerg       // Assigned stack order for live-in registers.
10906f32e7eSjoerg       // FixStack[i] == getStackEntry(i) for all i < FixCount.
11006f32e7eSjoerg       unsigned char FixStack[8];
11106f32e7eSjoerg 
LiveBundle__anond501d9200111::FPS::LiveBundle11206f32e7eSjoerg       LiveBundle() : Mask(0), FixCount(0) {}
11306f32e7eSjoerg 
11406f32e7eSjoerg       // Have the live registers been assigned a stack order yet?
isFixed__anond501d9200111::FPS::LiveBundle11506f32e7eSjoerg       bool isFixed() const { return !Mask || FixCount; }
11606f32e7eSjoerg     };
11706f32e7eSjoerg 
11806f32e7eSjoerg     // Numbered LiveBundle structs. LiveBundles[0] is used for all CFG edges
11906f32e7eSjoerg     // with no live FP registers.
12006f32e7eSjoerg     SmallVector<LiveBundle, 8> LiveBundles;
12106f32e7eSjoerg 
12206f32e7eSjoerg     // The edge bundle analysis provides indices into the LiveBundles vector.
123*da58b97aSjoerg     EdgeBundles *Bundles = nullptr;
12406f32e7eSjoerg 
12506f32e7eSjoerg     // Return a bitmask of FP registers in block's live-in list.
calcLiveInMask__anond501d9200111::FPS12606f32e7eSjoerg     static unsigned calcLiveInMask(MachineBasicBlock *MBB, bool RemoveFPs) {
12706f32e7eSjoerg       unsigned Mask = 0;
12806f32e7eSjoerg       for (MachineBasicBlock::livein_iterator I = MBB->livein_begin();
12906f32e7eSjoerg            I != MBB->livein_end(); ) {
13006f32e7eSjoerg         MCPhysReg Reg = I->PhysReg;
13106f32e7eSjoerg         static_assert(X86::FP6 - X86::FP0 == 6, "sequential regnums");
13206f32e7eSjoerg         if (Reg >= X86::FP0 && Reg <= X86::FP6) {
13306f32e7eSjoerg           Mask |= 1 << (Reg - X86::FP0);
13406f32e7eSjoerg           if (RemoveFPs) {
13506f32e7eSjoerg             I = MBB->removeLiveIn(I);
13606f32e7eSjoerg             continue;
13706f32e7eSjoerg           }
13806f32e7eSjoerg         }
13906f32e7eSjoerg         ++I;
14006f32e7eSjoerg       }
14106f32e7eSjoerg       return Mask;
14206f32e7eSjoerg     }
14306f32e7eSjoerg 
14406f32e7eSjoerg     // Partition all the CFG edges into LiveBundles.
14506f32e7eSjoerg     void bundleCFGRecomputeKillFlags(MachineFunction &MF);
14606f32e7eSjoerg 
147*da58b97aSjoerg     MachineBasicBlock *MBB = nullptr;     // Current basic block
14806f32e7eSjoerg 
14906f32e7eSjoerg     // The hardware keeps track of how many FP registers are live, so we have
15006f32e7eSjoerg     // to model that exactly. Usually, each live register corresponds to an
15106f32e7eSjoerg     // FP<n> register, but when dealing with calls, returns, and inline
15206f32e7eSjoerg     // assembly, it is sometimes necessary to have live scratch registers.
15306f32e7eSjoerg     unsigned Stack[8];          // FP<n> Registers in each stack slot...
154*da58b97aSjoerg     unsigned StackTop = 0;      // The current top of the FP stack.
15506f32e7eSjoerg 
15606f32e7eSjoerg     enum {
15706f32e7eSjoerg       NumFPRegs = 8             // Including scratch pseudo-registers.
15806f32e7eSjoerg     };
15906f32e7eSjoerg 
16006f32e7eSjoerg     // For each live FP<n> register, point to its Stack[] entry.
16106f32e7eSjoerg     // The first entries correspond to FP0-FP6, the rest are scratch registers
16206f32e7eSjoerg     // used when we need slightly different live registers than what the
16306f32e7eSjoerg     // register allocator thinks.
16406f32e7eSjoerg     unsigned RegMap[NumFPRegs];
16506f32e7eSjoerg 
16606f32e7eSjoerg     // Set up our stack model to match the incoming registers to MBB.
16706f32e7eSjoerg     void setupBlockStack();
16806f32e7eSjoerg 
16906f32e7eSjoerg     // Shuffle live registers to match the expectations of successor blocks.
17006f32e7eSjoerg     void finishBlockStack();
17106f32e7eSjoerg 
17206f32e7eSjoerg #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dumpStack__anond501d9200111::FPS17306f32e7eSjoerg     void dumpStack() const {
17406f32e7eSjoerg       dbgs() << "Stack contents:";
17506f32e7eSjoerg       for (unsigned i = 0; i != StackTop; ++i) {
17606f32e7eSjoerg         dbgs() << " FP" << Stack[i];
17706f32e7eSjoerg         assert(RegMap[Stack[i]] == i && "Stack[] doesn't match RegMap[]!");
17806f32e7eSjoerg       }
17906f32e7eSjoerg     }
18006f32e7eSjoerg #endif
18106f32e7eSjoerg 
18206f32e7eSjoerg     /// getSlot - Return the stack slot number a particular register number is
18306f32e7eSjoerg     /// in.
getSlot__anond501d9200111::FPS18406f32e7eSjoerg     unsigned getSlot(unsigned RegNo) const {
18506f32e7eSjoerg       assert(RegNo < NumFPRegs && "Regno out of range!");
18606f32e7eSjoerg       return RegMap[RegNo];
18706f32e7eSjoerg     }
18806f32e7eSjoerg 
18906f32e7eSjoerg     /// isLive - Is RegNo currently live in the stack?
isLive__anond501d9200111::FPS19006f32e7eSjoerg     bool isLive(unsigned RegNo) const {
19106f32e7eSjoerg       unsigned Slot = getSlot(RegNo);
19206f32e7eSjoerg       return Slot < StackTop && Stack[Slot] == RegNo;
19306f32e7eSjoerg     }
19406f32e7eSjoerg 
19506f32e7eSjoerg     /// getStackEntry - Return the X86::FP<n> register in register ST(i).
getStackEntry__anond501d9200111::FPS19606f32e7eSjoerg     unsigned getStackEntry(unsigned STi) const {
19706f32e7eSjoerg       if (STi >= StackTop)
19806f32e7eSjoerg         report_fatal_error("Access past stack top!");
19906f32e7eSjoerg       return Stack[StackTop-1-STi];
20006f32e7eSjoerg     }
20106f32e7eSjoerg 
20206f32e7eSjoerg     /// getSTReg - Return the X86::ST(i) register which contains the specified
20306f32e7eSjoerg     /// FP<RegNo> register.
getSTReg__anond501d9200111::FPS20406f32e7eSjoerg     unsigned getSTReg(unsigned RegNo) const {
20506f32e7eSjoerg       return StackTop - 1 - getSlot(RegNo) + X86::ST0;
20606f32e7eSjoerg     }
20706f32e7eSjoerg 
20806f32e7eSjoerg     // pushReg - Push the specified FP<n> register onto the stack.
pushReg__anond501d9200111::FPS20906f32e7eSjoerg     void pushReg(unsigned Reg) {
21006f32e7eSjoerg       assert(Reg < NumFPRegs && "Register number out of range!");
21106f32e7eSjoerg       if (StackTop >= 8)
21206f32e7eSjoerg         report_fatal_error("Stack overflow!");
21306f32e7eSjoerg       Stack[StackTop] = Reg;
21406f32e7eSjoerg       RegMap[Reg] = StackTop++;
21506f32e7eSjoerg     }
21606f32e7eSjoerg 
21706f32e7eSjoerg     // popReg - Pop a register from the stack.
popReg__anond501d9200111::FPS21806f32e7eSjoerg     void popReg() {
21906f32e7eSjoerg       if (StackTop == 0)
22006f32e7eSjoerg         report_fatal_error("Cannot pop empty stack!");
22106f32e7eSjoerg       RegMap[Stack[--StackTop]] = ~0;     // Update state
22206f32e7eSjoerg     }
22306f32e7eSjoerg 
isAtTop__anond501d9200111::FPS22406f32e7eSjoerg     bool isAtTop(unsigned RegNo) const { return getSlot(RegNo) == StackTop-1; }
moveToTop__anond501d9200111::FPS22506f32e7eSjoerg     void moveToTop(unsigned RegNo, MachineBasicBlock::iterator I) {
22606f32e7eSjoerg       DebugLoc dl = I == MBB->end() ? DebugLoc() : I->getDebugLoc();
22706f32e7eSjoerg       if (isAtTop(RegNo)) return;
22806f32e7eSjoerg 
22906f32e7eSjoerg       unsigned STReg = getSTReg(RegNo);
23006f32e7eSjoerg       unsigned RegOnTop = getStackEntry(0);
23106f32e7eSjoerg 
23206f32e7eSjoerg       // Swap the slots the regs are in.
23306f32e7eSjoerg       std::swap(RegMap[RegNo], RegMap[RegOnTop]);
23406f32e7eSjoerg 
23506f32e7eSjoerg       // Swap stack slot contents.
23606f32e7eSjoerg       if (RegMap[RegOnTop] >= StackTop)
23706f32e7eSjoerg         report_fatal_error("Access past stack top!");
23806f32e7eSjoerg       std::swap(Stack[RegMap[RegOnTop]], Stack[StackTop-1]);
23906f32e7eSjoerg 
24006f32e7eSjoerg       // Emit an fxch to update the runtime processors version of the state.
24106f32e7eSjoerg       BuildMI(*MBB, I, dl, TII->get(X86::XCH_F)).addReg(STReg);
24206f32e7eSjoerg       ++NumFXCH;
24306f32e7eSjoerg     }
24406f32e7eSjoerg 
duplicateToTop__anond501d9200111::FPS24506f32e7eSjoerg     void duplicateToTop(unsigned RegNo, unsigned AsReg,
24606f32e7eSjoerg                         MachineBasicBlock::iterator I) {
24706f32e7eSjoerg       DebugLoc dl = I == MBB->end() ? DebugLoc() : I->getDebugLoc();
24806f32e7eSjoerg       unsigned STReg = getSTReg(RegNo);
24906f32e7eSjoerg       pushReg(AsReg);   // New register on top of stack
25006f32e7eSjoerg 
25106f32e7eSjoerg       BuildMI(*MBB, I, dl, TII->get(X86::LD_Frr)).addReg(STReg);
25206f32e7eSjoerg     }
25306f32e7eSjoerg 
25406f32e7eSjoerg     /// popStackAfter - Pop the current value off of the top of the FP stack
25506f32e7eSjoerg     /// after the specified instruction.
25606f32e7eSjoerg     void popStackAfter(MachineBasicBlock::iterator &I);
25706f32e7eSjoerg 
25806f32e7eSjoerg     /// freeStackSlotAfter - Free the specified register from the register
25906f32e7eSjoerg     /// stack, so that it is no longer in a register.  If the register is
26006f32e7eSjoerg     /// currently at the top of the stack, we just pop the current instruction,
26106f32e7eSjoerg     /// otherwise we store the current top-of-stack into the specified slot,
26206f32e7eSjoerg     /// then pop the top of stack.
26306f32e7eSjoerg     void freeStackSlotAfter(MachineBasicBlock::iterator &I, unsigned Reg);
26406f32e7eSjoerg 
26506f32e7eSjoerg     /// freeStackSlotBefore - Just the pop, no folding. Return the inserted
26606f32e7eSjoerg     /// instruction.
26706f32e7eSjoerg     MachineBasicBlock::iterator
26806f32e7eSjoerg     freeStackSlotBefore(MachineBasicBlock::iterator I, unsigned FPRegNo);
26906f32e7eSjoerg 
27006f32e7eSjoerg     /// Adjust the live registers to be the set in Mask.
27106f32e7eSjoerg     void adjustLiveRegs(unsigned Mask, MachineBasicBlock::iterator I);
27206f32e7eSjoerg 
27306f32e7eSjoerg     /// Shuffle the top FixCount stack entries such that FP reg FixStack[0] is
27406f32e7eSjoerg     /// st(0), FP reg FixStack[1] is st(1) etc.
27506f32e7eSjoerg     void shuffleStackTop(const unsigned char *FixStack, unsigned FixCount,
27606f32e7eSjoerg                          MachineBasicBlock::iterator I);
27706f32e7eSjoerg 
27806f32e7eSjoerg     bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB);
27906f32e7eSjoerg 
28006f32e7eSjoerg     void handleCall(MachineBasicBlock::iterator &I);
28106f32e7eSjoerg     void handleReturn(MachineBasicBlock::iterator &I);
28206f32e7eSjoerg     void handleZeroArgFP(MachineBasicBlock::iterator &I);
28306f32e7eSjoerg     void handleOneArgFP(MachineBasicBlock::iterator &I);
28406f32e7eSjoerg     void handleOneArgFPRW(MachineBasicBlock::iterator &I);
28506f32e7eSjoerg     void handleTwoArgFP(MachineBasicBlock::iterator &I);
28606f32e7eSjoerg     void handleCompareFP(MachineBasicBlock::iterator &I);
28706f32e7eSjoerg     void handleCondMovFP(MachineBasicBlock::iterator &I);
28806f32e7eSjoerg     void handleSpecialFP(MachineBasicBlock::iterator &I);
28906f32e7eSjoerg 
29006f32e7eSjoerg     // Check if a COPY instruction is using FP registers.
isFPCopy__anond501d9200111::FPS29106f32e7eSjoerg     static bool isFPCopy(MachineInstr &MI) {
29206f32e7eSjoerg       Register DstReg = MI.getOperand(0).getReg();
29306f32e7eSjoerg       Register SrcReg = MI.getOperand(1).getReg();
29406f32e7eSjoerg 
29506f32e7eSjoerg       return X86::RFP80RegClass.contains(DstReg) ||
29606f32e7eSjoerg         X86::RFP80RegClass.contains(SrcReg);
29706f32e7eSjoerg     }
29806f32e7eSjoerg 
29906f32e7eSjoerg     void setKillFlags(MachineBasicBlock &MBB) const;
30006f32e7eSjoerg   };
30106f32e7eSjoerg }
30206f32e7eSjoerg 
30306f32e7eSjoerg char FPS::ID = 0;
30406f32e7eSjoerg 
30506f32e7eSjoerg INITIALIZE_PASS_BEGIN(FPS, DEBUG_TYPE, "X86 FP Stackifier",
30606f32e7eSjoerg                       false, false)
INITIALIZE_PASS_DEPENDENCY(EdgeBundles)30706f32e7eSjoerg INITIALIZE_PASS_DEPENDENCY(EdgeBundles)
30806f32e7eSjoerg INITIALIZE_PASS_END(FPS, DEBUG_TYPE, "X86 FP Stackifier",
30906f32e7eSjoerg                     false, false)
31006f32e7eSjoerg 
31106f32e7eSjoerg FunctionPass *llvm::createX86FloatingPointStackifierPass() { return new FPS(); }
31206f32e7eSjoerg 
31306f32e7eSjoerg /// getFPReg - Return the X86::FPx register number for the specified operand.
31406f32e7eSjoerg /// For example, this returns 3 for X86::FP3.
getFPReg(const MachineOperand & MO)31506f32e7eSjoerg static unsigned getFPReg(const MachineOperand &MO) {
31606f32e7eSjoerg   assert(MO.isReg() && "Expected an FP register!");
31706f32e7eSjoerg   Register Reg = MO.getReg();
31806f32e7eSjoerg   assert(Reg >= X86::FP0 && Reg <= X86::FP6 && "Expected FP register!");
31906f32e7eSjoerg   return Reg - X86::FP0;
32006f32e7eSjoerg }
32106f32e7eSjoerg 
32206f32e7eSjoerg /// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
32306f32e7eSjoerg /// register references into FP stack references.
32406f32e7eSjoerg ///
runOnMachineFunction(MachineFunction & MF)32506f32e7eSjoerg bool FPS::runOnMachineFunction(MachineFunction &MF) {
32606f32e7eSjoerg   // We only need to run this pass if there are any FP registers used in this
32706f32e7eSjoerg   // function.  If it is all integer, there is nothing for us to do!
32806f32e7eSjoerg   bool FPIsUsed = false;
32906f32e7eSjoerg 
33006f32e7eSjoerg   static_assert(X86::FP6 == X86::FP0+6, "Register enums aren't sorted right!");
33106f32e7eSjoerg   const MachineRegisterInfo &MRI = MF.getRegInfo();
33206f32e7eSjoerg   for (unsigned i = 0; i <= 6; ++i)
33306f32e7eSjoerg     if (!MRI.reg_nodbg_empty(X86::FP0 + i)) {
33406f32e7eSjoerg       FPIsUsed = true;
33506f32e7eSjoerg       break;
33606f32e7eSjoerg     }
33706f32e7eSjoerg 
33806f32e7eSjoerg   // Early exit.
33906f32e7eSjoerg   if (!FPIsUsed) return false;
34006f32e7eSjoerg 
34106f32e7eSjoerg   Bundles = &getAnalysis<EdgeBundles>();
34206f32e7eSjoerg   TII = MF.getSubtarget().getInstrInfo();
34306f32e7eSjoerg 
34406f32e7eSjoerg   // Prepare cross-MBB liveness.
34506f32e7eSjoerg   bundleCFGRecomputeKillFlags(MF);
34606f32e7eSjoerg 
34706f32e7eSjoerg   StackTop = 0;
34806f32e7eSjoerg 
34906f32e7eSjoerg   // Process the function in depth first order so that we process at least one
35006f32e7eSjoerg   // of the predecessors for every reachable block in the function.
35106f32e7eSjoerg   df_iterator_default_set<MachineBasicBlock*> Processed;
35206f32e7eSjoerg   MachineBasicBlock *Entry = &MF.front();
35306f32e7eSjoerg 
35406f32e7eSjoerg   LiveBundle &Bundle =
35506f32e7eSjoerg     LiveBundles[Bundles->getBundle(Entry->getNumber(), false)];
35606f32e7eSjoerg 
35706f32e7eSjoerg   // In regcall convention, some FP registers may not be passed through
35806f32e7eSjoerg   // the stack, so they will need to be assigned to the stack first
35906f32e7eSjoerg   if ((Entry->getParent()->getFunction().getCallingConv() ==
36006f32e7eSjoerg     CallingConv::X86_RegCall) && (Bundle.Mask && !Bundle.FixCount)) {
36106f32e7eSjoerg     // In the register calling convention, up to one FP argument could be
36206f32e7eSjoerg     // saved in the first FP register.
36306f32e7eSjoerg     // If bundle.mask is non-zero and Bundle.FixCount is zero, it means
36406f32e7eSjoerg     // that the FP registers contain arguments.
36506f32e7eSjoerg     // The actual value is passed in FP0.
36606f32e7eSjoerg     // Here we fix the stack and mark FP0 as pre-assigned register.
36706f32e7eSjoerg     assert((Bundle.Mask & 0xFE) == 0 &&
36806f32e7eSjoerg       "Only FP0 could be passed as an argument");
36906f32e7eSjoerg     Bundle.FixCount = 1;
37006f32e7eSjoerg     Bundle.FixStack[0] = 0;
37106f32e7eSjoerg   }
37206f32e7eSjoerg 
37306f32e7eSjoerg   bool Changed = false;
37406f32e7eSjoerg   for (MachineBasicBlock *BB : depth_first_ext(Entry, Processed))
37506f32e7eSjoerg     Changed |= processBasicBlock(MF, *BB);
37606f32e7eSjoerg 
37706f32e7eSjoerg   // Process any unreachable blocks in arbitrary order now.
37806f32e7eSjoerg   if (MF.size() != Processed.size())
37906f32e7eSjoerg     for (MachineBasicBlock &BB : MF)
38006f32e7eSjoerg       if (Processed.insert(&BB).second)
38106f32e7eSjoerg         Changed |= processBasicBlock(MF, BB);
38206f32e7eSjoerg 
38306f32e7eSjoerg   LiveBundles.clear();
38406f32e7eSjoerg 
38506f32e7eSjoerg   return Changed;
38606f32e7eSjoerg }
38706f32e7eSjoerg 
38806f32e7eSjoerg /// bundleCFG - Scan all the basic blocks to determine consistent live-in and
38906f32e7eSjoerg /// live-out sets for the FP registers. Consistent means that the set of
39006f32e7eSjoerg /// registers live-out from a block is identical to the live-in set of all
39106f32e7eSjoerg /// successors. This is not enforced by the normal live-in lists since
39206f32e7eSjoerg /// registers may be implicitly defined, or not used by all successors.
bundleCFGRecomputeKillFlags(MachineFunction & MF)39306f32e7eSjoerg void FPS::bundleCFGRecomputeKillFlags(MachineFunction &MF) {
39406f32e7eSjoerg   assert(LiveBundles.empty() && "Stale data in LiveBundles");
39506f32e7eSjoerg   LiveBundles.resize(Bundles->getNumBundles());
39606f32e7eSjoerg 
39706f32e7eSjoerg   // Gather the actual live-in masks for all MBBs.
39806f32e7eSjoerg   for (MachineBasicBlock &MBB : MF) {
39906f32e7eSjoerg     setKillFlags(MBB);
40006f32e7eSjoerg 
40106f32e7eSjoerg     const unsigned Mask = calcLiveInMask(&MBB, false);
40206f32e7eSjoerg     if (!Mask)
40306f32e7eSjoerg       continue;
40406f32e7eSjoerg     // Update MBB ingoing bundle mask.
40506f32e7eSjoerg     LiveBundles[Bundles->getBundle(MBB.getNumber(), false)].Mask |= Mask;
40606f32e7eSjoerg   }
40706f32e7eSjoerg }
40806f32e7eSjoerg 
40906f32e7eSjoerg /// processBasicBlock - Loop over all of the instructions in the basic block,
41006f32e7eSjoerg /// transforming FP instructions into their stack form.
41106f32e7eSjoerg ///
processBasicBlock(MachineFunction & MF,MachineBasicBlock & BB)41206f32e7eSjoerg bool FPS::processBasicBlock(MachineFunction &MF, MachineBasicBlock &BB) {
41306f32e7eSjoerg   bool Changed = false;
41406f32e7eSjoerg   MBB = &BB;
41506f32e7eSjoerg 
41606f32e7eSjoerg   setupBlockStack();
41706f32e7eSjoerg 
41806f32e7eSjoerg   for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I) {
41906f32e7eSjoerg     MachineInstr &MI = *I;
42006f32e7eSjoerg     uint64_t Flags = MI.getDesc().TSFlags;
42106f32e7eSjoerg 
42206f32e7eSjoerg     unsigned FPInstClass = Flags & X86II::FPTypeMask;
42306f32e7eSjoerg     if (MI.isInlineAsm())
42406f32e7eSjoerg       FPInstClass = X86II::SpecialFP;
42506f32e7eSjoerg 
42606f32e7eSjoerg     if (MI.isCopy() && isFPCopy(MI))
42706f32e7eSjoerg       FPInstClass = X86II::SpecialFP;
42806f32e7eSjoerg 
42906f32e7eSjoerg     if (MI.isImplicitDef() &&
43006f32e7eSjoerg         X86::RFP80RegClass.contains(MI.getOperand(0).getReg()))
43106f32e7eSjoerg       FPInstClass = X86II::SpecialFP;
43206f32e7eSjoerg 
43306f32e7eSjoerg     if (MI.isCall())
43406f32e7eSjoerg       FPInstClass = X86II::SpecialFP;
43506f32e7eSjoerg 
43606f32e7eSjoerg     if (FPInstClass == X86II::NotFP)
43706f32e7eSjoerg       continue;  // Efficiently ignore non-fp insts!
43806f32e7eSjoerg 
43906f32e7eSjoerg     MachineInstr *PrevMI = nullptr;
44006f32e7eSjoerg     if (I != BB.begin())
44106f32e7eSjoerg       PrevMI = &*std::prev(I);
44206f32e7eSjoerg 
44306f32e7eSjoerg     ++NumFP;  // Keep track of # of pseudo instrs
44406f32e7eSjoerg     LLVM_DEBUG(dbgs() << "\nFPInst:\t" << MI);
44506f32e7eSjoerg 
44606f32e7eSjoerg     // Get dead variables list now because the MI pointer may be deleted as part
44706f32e7eSjoerg     // of processing!
44806f32e7eSjoerg     SmallVector<unsigned, 8> DeadRegs;
44906f32e7eSjoerg     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
45006f32e7eSjoerg       const MachineOperand &MO = MI.getOperand(i);
45106f32e7eSjoerg       if (MO.isReg() && MO.isDead())
45206f32e7eSjoerg         DeadRegs.push_back(MO.getReg());
45306f32e7eSjoerg     }
45406f32e7eSjoerg 
45506f32e7eSjoerg     switch (FPInstClass) {
45606f32e7eSjoerg     case X86II::ZeroArgFP:  handleZeroArgFP(I); break;
45706f32e7eSjoerg     case X86II::OneArgFP:   handleOneArgFP(I);  break;  // fstp ST(0)
45806f32e7eSjoerg     case X86II::OneArgFPRW: handleOneArgFPRW(I); break; // ST(0) = fsqrt(ST(0))
45906f32e7eSjoerg     case X86II::TwoArgFP:   handleTwoArgFP(I);  break;
46006f32e7eSjoerg     case X86II::CompareFP:  handleCompareFP(I); break;
46106f32e7eSjoerg     case X86II::CondMovFP:  handleCondMovFP(I); break;
46206f32e7eSjoerg     case X86II::SpecialFP:  handleSpecialFP(I); break;
46306f32e7eSjoerg     default: llvm_unreachable("Unknown FP Type!");
46406f32e7eSjoerg     }
46506f32e7eSjoerg 
46606f32e7eSjoerg     // Check to see if any of the values defined by this instruction are dead
46706f32e7eSjoerg     // after definition.  If so, pop them.
46806f32e7eSjoerg     for (unsigned i = 0, e = DeadRegs.size(); i != e; ++i) {
46906f32e7eSjoerg       unsigned Reg = DeadRegs[i];
47006f32e7eSjoerg       // Check if Reg is live on the stack. An inline-asm register operand that
47106f32e7eSjoerg       // is in the clobber list and marked dead might not be live on the stack.
47206f32e7eSjoerg       static_assert(X86::FP7 - X86::FP0 == 7, "sequential FP regnumbers");
47306f32e7eSjoerg       if (Reg >= X86::FP0 && Reg <= X86::FP6 && isLive(Reg-X86::FP0)) {
47406f32e7eSjoerg         LLVM_DEBUG(dbgs() << "Register FP#" << Reg - X86::FP0 << " is dead!\n");
47506f32e7eSjoerg         freeStackSlotAfter(I, Reg-X86::FP0);
47606f32e7eSjoerg       }
47706f32e7eSjoerg     }
47806f32e7eSjoerg 
47906f32e7eSjoerg     // Print out all of the instructions expanded to if -debug
48006f32e7eSjoerg     LLVM_DEBUG({
48106f32e7eSjoerg       MachineBasicBlock::iterator PrevI = PrevMI;
48206f32e7eSjoerg       if (I == PrevI) {
48306f32e7eSjoerg         dbgs() << "Just deleted pseudo instruction\n";
48406f32e7eSjoerg       } else {
48506f32e7eSjoerg         MachineBasicBlock::iterator Start = I;
48606f32e7eSjoerg         // Rewind to first instruction newly inserted.
48706f32e7eSjoerg         while (Start != BB.begin() && std::prev(Start) != PrevI)
48806f32e7eSjoerg           --Start;
48906f32e7eSjoerg         dbgs() << "Inserted instructions:\n\t";
49006f32e7eSjoerg         Start->print(dbgs());
49106f32e7eSjoerg         while (++Start != std::next(I)) {
49206f32e7eSjoerg         }
49306f32e7eSjoerg       }
49406f32e7eSjoerg       dumpStack();
49506f32e7eSjoerg     });
49606f32e7eSjoerg     (void)PrevMI;
49706f32e7eSjoerg 
49806f32e7eSjoerg     Changed = true;
49906f32e7eSjoerg   }
50006f32e7eSjoerg 
50106f32e7eSjoerg   finishBlockStack();
50206f32e7eSjoerg 
50306f32e7eSjoerg   return Changed;
50406f32e7eSjoerg }
50506f32e7eSjoerg 
50606f32e7eSjoerg /// setupBlockStack - Use the live bundles to set up our model of the stack
50706f32e7eSjoerg /// to match predecessors' live out stack.
setupBlockStack()50806f32e7eSjoerg void FPS::setupBlockStack() {
50906f32e7eSjoerg   LLVM_DEBUG(dbgs() << "\nSetting up live-ins for " << printMBBReference(*MBB)
51006f32e7eSjoerg                     << " derived from " << MBB->getName() << ".\n");
51106f32e7eSjoerg   StackTop = 0;
51206f32e7eSjoerg   // Get the live-in bundle for MBB.
51306f32e7eSjoerg   const LiveBundle &Bundle =
51406f32e7eSjoerg     LiveBundles[Bundles->getBundle(MBB->getNumber(), false)];
51506f32e7eSjoerg 
51606f32e7eSjoerg   if (!Bundle.Mask) {
51706f32e7eSjoerg     LLVM_DEBUG(dbgs() << "Block has no FP live-ins.\n");
51806f32e7eSjoerg     return;
51906f32e7eSjoerg   }
52006f32e7eSjoerg 
52106f32e7eSjoerg   // Depth-first iteration should ensure that we always have an assigned stack.
52206f32e7eSjoerg   assert(Bundle.isFixed() && "Reached block before any predecessors");
52306f32e7eSjoerg 
52406f32e7eSjoerg   // Push the fixed live-in registers.
52506f32e7eSjoerg   for (unsigned i = Bundle.FixCount; i > 0; --i) {
52606f32e7eSjoerg     LLVM_DEBUG(dbgs() << "Live-in st(" << (i - 1) << "): %fp"
52706f32e7eSjoerg                       << unsigned(Bundle.FixStack[i - 1]) << '\n');
52806f32e7eSjoerg     pushReg(Bundle.FixStack[i-1]);
52906f32e7eSjoerg   }
53006f32e7eSjoerg 
53106f32e7eSjoerg   // Kill off unwanted live-ins. This can happen with a critical edge.
53206f32e7eSjoerg   // FIXME: We could keep these live registers around as zombies. They may need
53306f32e7eSjoerg   // to be revived at the end of a short block. It might save a few instrs.
53406f32e7eSjoerg   unsigned Mask = calcLiveInMask(MBB, /*RemoveFPs=*/true);
53506f32e7eSjoerg   adjustLiveRegs(Mask, MBB->begin());
53606f32e7eSjoerg   LLVM_DEBUG(MBB->dump());
53706f32e7eSjoerg }
53806f32e7eSjoerg 
53906f32e7eSjoerg /// finishBlockStack - Revive live-outs that are implicitly defined out of
54006f32e7eSjoerg /// MBB. Shuffle live registers to match the expected fixed stack of any
54106f32e7eSjoerg /// predecessors, and ensure that all predecessors are expecting the same
54206f32e7eSjoerg /// stack.
finishBlockStack()54306f32e7eSjoerg void FPS::finishBlockStack() {
54406f32e7eSjoerg   // The RET handling below takes care of return blocks for us.
54506f32e7eSjoerg   if (MBB->succ_empty())
54606f32e7eSjoerg     return;
54706f32e7eSjoerg 
54806f32e7eSjoerg   LLVM_DEBUG(dbgs() << "Setting up live-outs for " << printMBBReference(*MBB)
54906f32e7eSjoerg                     << " derived from " << MBB->getName() << ".\n");
55006f32e7eSjoerg 
55106f32e7eSjoerg   // Get MBB's live-out bundle.
55206f32e7eSjoerg   unsigned BundleIdx = Bundles->getBundle(MBB->getNumber(), true);
55306f32e7eSjoerg   LiveBundle &Bundle = LiveBundles[BundleIdx];
55406f32e7eSjoerg 
55506f32e7eSjoerg   // We may need to kill and define some registers to match successors.
55606f32e7eSjoerg   // FIXME: This can probably be combined with the shuffle below.
55706f32e7eSjoerg   MachineBasicBlock::iterator Term = MBB->getFirstTerminator();
55806f32e7eSjoerg   adjustLiveRegs(Bundle.Mask, Term);
55906f32e7eSjoerg 
56006f32e7eSjoerg   if (!Bundle.Mask) {
56106f32e7eSjoerg     LLVM_DEBUG(dbgs() << "No live-outs.\n");
56206f32e7eSjoerg     return;
56306f32e7eSjoerg   }
56406f32e7eSjoerg 
56506f32e7eSjoerg   // Has the stack order been fixed yet?
56606f32e7eSjoerg   LLVM_DEBUG(dbgs() << "LB#" << BundleIdx << ": ");
56706f32e7eSjoerg   if (Bundle.isFixed()) {
56806f32e7eSjoerg     LLVM_DEBUG(dbgs() << "Shuffling stack to match.\n");
56906f32e7eSjoerg     shuffleStackTop(Bundle.FixStack, Bundle.FixCount, Term);
57006f32e7eSjoerg   } else {
57106f32e7eSjoerg     // Not fixed yet, we get to choose.
57206f32e7eSjoerg     LLVM_DEBUG(dbgs() << "Fixing stack order now.\n");
57306f32e7eSjoerg     Bundle.FixCount = StackTop;
57406f32e7eSjoerg     for (unsigned i = 0; i < StackTop; ++i)
57506f32e7eSjoerg       Bundle.FixStack[i] = getStackEntry(i);
57606f32e7eSjoerg   }
57706f32e7eSjoerg }
57806f32e7eSjoerg 
57906f32e7eSjoerg 
58006f32e7eSjoerg //===----------------------------------------------------------------------===//
58106f32e7eSjoerg // Efficient Lookup Table Support
58206f32e7eSjoerg //===----------------------------------------------------------------------===//
58306f32e7eSjoerg 
58406f32e7eSjoerg namespace {
58506f32e7eSjoerg   struct TableEntry {
58606f32e7eSjoerg     uint16_t from;
58706f32e7eSjoerg     uint16_t to;
operator <__anond501d9200311::TableEntry58806f32e7eSjoerg     bool operator<(const TableEntry &TE) const { return from < TE.from; }
operator <(const TableEntry & TE,unsigned V)58906f32e7eSjoerg     friend bool operator<(const TableEntry &TE, unsigned V) {
59006f32e7eSjoerg       return TE.from < V;
59106f32e7eSjoerg     }
operator <(unsigned V,const TableEntry & TE)59206f32e7eSjoerg     friend bool LLVM_ATTRIBUTE_UNUSED operator<(unsigned V,
59306f32e7eSjoerg                                                 const TableEntry &TE) {
59406f32e7eSjoerg       return V < TE.from;
59506f32e7eSjoerg     }
59606f32e7eSjoerg   };
59706f32e7eSjoerg }
59806f32e7eSjoerg 
Lookup(ArrayRef<TableEntry> Table,unsigned Opcode)59906f32e7eSjoerg static int Lookup(ArrayRef<TableEntry> Table, unsigned Opcode) {
60006f32e7eSjoerg   const TableEntry *I = llvm::lower_bound(Table, Opcode);
60106f32e7eSjoerg   if (I != Table.end() && I->from == Opcode)
60206f32e7eSjoerg     return I->to;
60306f32e7eSjoerg   return -1;
60406f32e7eSjoerg }
60506f32e7eSjoerg 
60606f32e7eSjoerg #ifdef NDEBUG
60706f32e7eSjoerg #define ASSERT_SORTED(TABLE)
60806f32e7eSjoerg #else
60906f32e7eSjoerg #define ASSERT_SORTED(TABLE)                                                   \
61006f32e7eSjoerg   {                                                                            \
61106f32e7eSjoerg     static std::atomic<bool> TABLE##Checked(false);                            \
61206f32e7eSjoerg     if (!TABLE##Checked.load(std::memory_order_relaxed)) {                     \
613*da58b97aSjoerg       assert(is_sorted(TABLE) &&                                               \
61406f32e7eSjoerg              "All lookup tables must be sorted for efficient access!");        \
61506f32e7eSjoerg       TABLE##Checked.store(true, std::memory_order_relaxed);                   \
61606f32e7eSjoerg     }                                                                          \
61706f32e7eSjoerg   }
61806f32e7eSjoerg #endif
61906f32e7eSjoerg 
62006f32e7eSjoerg //===----------------------------------------------------------------------===//
62106f32e7eSjoerg // Register File -> Register Stack Mapping Methods
62206f32e7eSjoerg //===----------------------------------------------------------------------===//
62306f32e7eSjoerg 
62406f32e7eSjoerg // OpcodeTable - Sorted map of register instructions to their stack version.
62506f32e7eSjoerg // The first element is an register file pseudo instruction, the second is the
62606f32e7eSjoerg // concrete X86 instruction which uses the register stack.
62706f32e7eSjoerg //
62806f32e7eSjoerg static const TableEntry OpcodeTable[] = {
62906f32e7eSjoerg   { X86::ABS_Fp32     , X86::ABS_F     },
63006f32e7eSjoerg   { X86::ABS_Fp64     , X86::ABS_F     },
63106f32e7eSjoerg   { X86::ABS_Fp80     , X86::ABS_F     },
63206f32e7eSjoerg   { X86::ADD_Fp32m    , X86::ADD_F32m  },
63306f32e7eSjoerg   { X86::ADD_Fp64m    , X86::ADD_F64m  },
63406f32e7eSjoerg   { X86::ADD_Fp64m32  , X86::ADD_F32m  },
63506f32e7eSjoerg   { X86::ADD_Fp80m32  , X86::ADD_F32m  },
63606f32e7eSjoerg   { X86::ADD_Fp80m64  , X86::ADD_F64m  },
63706f32e7eSjoerg   { X86::ADD_FpI16m32 , X86::ADD_FI16m },
63806f32e7eSjoerg   { X86::ADD_FpI16m64 , X86::ADD_FI16m },
63906f32e7eSjoerg   { X86::ADD_FpI16m80 , X86::ADD_FI16m },
64006f32e7eSjoerg   { X86::ADD_FpI32m32 , X86::ADD_FI32m },
64106f32e7eSjoerg   { X86::ADD_FpI32m64 , X86::ADD_FI32m },
64206f32e7eSjoerg   { X86::ADD_FpI32m80 , X86::ADD_FI32m },
64306f32e7eSjoerg   { X86::CHS_Fp32     , X86::CHS_F     },
64406f32e7eSjoerg   { X86::CHS_Fp64     , X86::CHS_F     },
64506f32e7eSjoerg   { X86::CHS_Fp80     , X86::CHS_F     },
64606f32e7eSjoerg   { X86::CMOVBE_Fp32  , X86::CMOVBE_F  },
64706f32e7eSjoerg   { X86::CMOVBE_Fp64  , X86::CMOVBE_F  },
64806f32e7eSjoerg   { X86::CMOVBE_Fp80  , X86::CMOVBE_F  },
64906f32e7eSjoerg   { X86::CMOVB_Fp32   , X86::CMOVB_F   },
65006f32e7eSjoerg   { X86::CMOVB_Fp64   , X86::CMOVB_F  },
65106f32e7eSjoerg   { X86::CMOVB_Fp80   , X86::CMOVB_F  },
65206f32e7eSjoerg   { X86::CMOVE_Fp32   , X86::CMOVE_F  },
65306f32e7eSjoerg   { X86::CMOVE_Fp64   , X86::CMOVE_F   },
65406f32e7eSjoerg   { X86::CMOVE_Fp80   , X86::CMOVE_F   },
65506f32e7eSjoerg   { X86::CMOVNBE_Fp32 , X86::CMOVNBE_F },
65606f32e7eSjoerg   { X86::CMOVNBE_Fp64 , X86::CMOVNBE_F },
65706f32e7eSjoerg   { X86::CMOVNBE_Fp80 , X86::CMOVNBE_F },
65806f32e7eSjoerg   { X86::CMOVNB_Fp32  , X86::CMOVNB_F  },
65906f32e7eSjoerg   { X86::CMOVNB_Fp64  , X86::CMOVNB_F  },
66006f32e7eSjoerg   { X86::CMOVNB_Fp80  , X86::CMOVNB_F  },
66106f32e7eSjoerg   { X86::CMOVNE_Fp32  , X86::CMOVNE_F  },
66206f32e7eSjoerg   { X86::CMOVNE_Fp64  , X86::CMOVNE_F  },
66306f32e7eSjoerg   { X86::CMOVNE_Fp80  , X86::CMOVNE_F  },
66406f32e7eSjoerg   { X86::CMOVNP_Fp32  , X86::CMOVNP_F  },
66506f32e7eSjoerg   { X86::CMOVNP_Fp64  , X86::CMOVNP_F  },
66606f32e7eSjoerg   { X86::CMOVNP_Fp80  , X86::CMOVNP_F  },
66706f32e7eSjoerg   { X86::CMOVP_Fp32   , X86::CMOVP_F   },
66806f32e7eSjoerg   { X86::CMOVP_Fp64   , X86::CMOVP_F   },
66906f32e7eSjoerg   { X86::CMOVP_Fp80   , X86::CMOVP_F   },
670*da58b97aSjoerg   { X86::COM_FpIr32   , X86::COM_FIr   },
671*da58b97aSjoerg   { X86::COM_FpIr64   , X86::COM_FIr   },
672*da58b97aSjoerg   { X86::COM_FpIr80   , X86::COM_FIr   },
673*da58b97aSjoerg   { X86::COM_Fpr32    , X86::COM_FST0r },
674*da58b97aSjoerg   { X86::COM_Fpr64    , X86::COM_FST0r },
675*da58b97aSjoerg   { X86::COM_Fpr80    , X86::COM_FST0r },
67606f32e7eSjoerg   { X86::DIVR_Fp32m   , X86::DIVR_F32m },
67706f32e7eSjoerg   { X86::DIVR_Fp64m   , X86::DIVR_F64m },
67806f32e7eSjoerg   { X86::DIVR_Fp64m32 , X86::DIVR_F32m },
67906f32e7eSjoerg   { X86::DIVR_Fp80m32 , X86::DIVR_F32m },
68006f32e7eSjoerg   { X86::DIVR_Fp80m64 , X86::DIVR_F64m },
68106f32e7eSjoerg   { X86::DIVR_FpI16m32, X86::DIVR_FI16m},
68206f32e7eSjoerg   { X86::DIVR_FpI16m64, X86::DIVR_FI16m},
68306f32e7eSjoerg   { X86::DIVR_FpI16m80, X86::DIVR_FI16m},
68406f32e7eSjoerg   { X86::DIVR_FpI32m32, X86::DIVR_FI32m},
68506f32e7eSjoerg   { X86::DIVR_FpI32m64, X86::DIVR_FI32m},
68606f32e7eSjoerg   { X86::DIVR_FpI32m80, X86::DIVR_FI32m},
68706f32e7eSjoerg   { X86::DIV_Fp32m    , X86::DIV_F32m  },
68806f32e7eSjoerg   { X86::DIV_Fp64m    , X86::DIV_F64m  },
68906f32e7eSjoerg   { X86::DIV_Fp64m32  , X86::DIV_F32m  },
69006f32e7eSjoerg   { X86::DIV_Fp80m32  , X86::DIV_F32m  },
69106f32e7eSjoerg   { X86::DIV_Fp80m64  , X86::DIV_F64m  },
69206f32e7eSjoerg   { X86::DIV_FpI16m32 , X86::DIV_FI16m },
69306f32e7eSjoerg   { X86::DIV_FpI16m64 , X86::DIV_FI16m },
69406f32e7eSjoerg   { X86::DIV_FpI16m80 , X86::DIV_FI16m },
69506f32e7eSjoerg   { X86::DIV_FpI32m32 , X86::DIV_FI32m },
69606f32e7eSjoerg   { X86::DIV_FpI32m64 , X86::DIV_FI32m },
69706f32e7eSjoerg   { X86::DIV_FpI32m80 , X86::DIV_FI32m },
69806f32e7eSjoerg   { X86::ILD_Fp16m32  , X86::ILD_F16m  },
69906f32e7eSjoerg   { X86::ILD_Fp16m64  , X86::ILD_F16m  },
70006f32e7eSjoerg   { X86::ILD_Fp16m80  , X86::ILD_F16m  },
70106f32e7eSjoerg   { X86::ILD_Fp32m32  , X86::ILD_F32m  },
70206f32e7eSjoerg   { X86::ILD_Fp32m64  , X86::ILD_F32m  },
70306f32e7eSjoerg   { X86::ILD_Fp32m80  , X86::ILD_F32m  },
70406f32e7eSjoerg   { X86::ILD_Fp64m32  , X86::ILD_F64m  },
70506f32e7eSjoerg   { X86::ILD_Fp64m64  , X86::ILD_F64m  },
70606f32e7eSjoerg   { X86::ILD_Fp64m80  , X86::ILD_F64m  },
70706f32e7eSjoerg   { X86::ISTT_Fp16m32 , X86::ISTT_FP16m},
70806f32e7eSjoerg   { X86::ISTT_Fp16m64 , X86::ISTT_FP16m},
70906f32e7eSjoerg   { X86::ISTT_Fp16m80 , X86::ISTT_FP16m},
71006f32e7eSjoerg   { X86::ISTT_Fp32m32 , X86::ISTT_FP32m},
71106f32e7eSjoerg   { X86::ISTT_Fp32m64 , X86::ISTT_FP32m},
71206f32e7eSjoerg   { X86::ISTT_Fp32m80 , X86::ISTT_FP32m},
71306f32e7eSjoerg   { X86::ISTT_Fp64m32 , X86::ISTT_FP64m},
71406f32e7eSjoerg   { X86::ISTT_Fp64m64 , X86::ISTT_FP64m},
71506f32e7eSjoerg   { X86::ISTT_Fp64m80 , X86::ISTT_FP64m},
71606f32e7eSjoerg   { X86::IST_Fp16m32  , X86::IST_F16m  },
71706f32e7eSjoerg   { X86::IST_Fp16m64  , X86::IST_F16m  },
71806f32e7eSjoerg   { X86::IST_Fp16m80  , X86::IST_F16m  },
71906f32e7eSjoerg   { X86::IST_Fp32m32  , X86::IST_F32m  },
72006f32e7eSjoerg   { X86::IST_Fp32m64  , X86::IST_F32m  },
72106f32e7eSjoerg   { X86::IST_Fp32m80  , X86::IST_F32m  },
72206f32e7eSjoerg   { X86::IST_Fp64m32  , X86::IST_FP64m },
72306f32e7eSjoerg   { X86::IST_Fp64m64  , X86::IST_FP64m },
72406f32e7eSjoerg   { X86::IST_Fp64m80  , X86::IST_FP64m },
72506f32e7eSjoerg   { X86::LD_Fp032     , X86::LD_F0     },
72606f32e7eSjoerg   { X86::LD_Fp064     , X86::LD_F0     },
72706f32e7eSjoerg   { X86::LD_Fp080     , X86::LD_F0     },
72806f32e7eSjoerg   { X86::LD_Fp132     , X86::LD_F1     },
72906f32e7eSjoerg   { X86::LD_Fp164     , X86::LD_F1     },
73006f32e7eSjoerg   { X86::LD_Fp180     , X86::LD_F1     },
73106f32e7eSjoerg   { X86::LD_Fp32m     , X86::LD_F32m   },
73206f32e7eSjoerg   { X86::LD_Fp32m64   , X86::LD_F32m   },
73306f32e7eSjoerg   { X86::LD_Fp32m80   , X86::LD_F32m   },
73406f32e7eSjoerg   { X86::LD_Fp64m     , X86::LD_F64m   },
73506f32e7eSjoerg   { X86::LD_Fp64m80   , X86::LD_F64m   },
73606f32e7eSjoerg   { X86::LD_Fp80m     , X86::LD_F80m   },
73706f32e7eSjoerg   { X86::MUL_Fp32m    , X86::MUL_F32m  },
73806f32e7eSjoerg   { X86::MUL_Fp64m    , X86::MUL_F64m  },
73906f32e7eSjoerg   { X86::MUL_Fp64m32  , X86::MUL_F32m  },
74006f32e7eSjoerg   { X86::MUL_Fp80m32  , X86::MUL_F32m  },
74106f32e7eSjoerg   { X86::MUL_Fp80m64  , X86::MUL_F64m  },
74206f32e7eSjoerg   { X86::MUL_FpI16m32 , X86::MUL_FI16m },
74306f32e7eSjoerg   { X86::MUL_FpI16m64 , X86::MUL_FI16m },
74406f32e7eSjoerg   { X86::MUL_FpI16m80 , X86::MUL_FI16m },
74506f32e7eSjoerg   { X86::MUL_FpI32m32 , X86::MUL_FI32m },
74606f32e7eSjoerg   { X86::MUL_FpI32m64 , X86::MUL_FI32m },
74706f32e7eSjoerg   { X86::MUL_FpI32m80 , X86::MUL_FI32m },
74806f32e7eSjoerg   { X86::SQRT_Fp32    , X86::SQRT_F    },
74906f32e7eSjoerg   { X86::SQRT_Fp64    , X86::SQRT_F    },
75006f32e7eSjoerg   { X86::SQRT_Fp80    , X86::SQRT_F    },
75106f32e7eSjoerg   { X86::ST_Fp32m     , X86::ST_F32m   },
75206f32e7eSjoerg   { X86::ST_Fp64m     , X86::ST_F64m   },
75306f32e7eSjoerg   { X86::ST_Fp64m32   , X86::ST_F32m   },
75406f32e7eSjoerg   { X86::ST_Fp80m32   , X86::ST_F32m   },
75506f32e7eSjoerg   { X86::ST_Fp80m64   , X86::ST_F64m   },
75606f32e7eSjoerg   { X86::ST_FpP80m    , X86::ST_FP80m  },
75706f32e7eSjoerg   { X86::SUBR_Fp32m   , X86::SUBR_F32m },
75806f32e7eSjoerg   { X86::SUBR_Fp64m   , X86::SUBR_F64m },
75906f32e7eSjoerg   { X86::SUBR_Fp64m32 , X86::SUBR_F32m },
76006f32e7eSjoerg   { X86::SUBR_Fp80m32 , X86::SUBR_F32m },
76106f32e7eSjoerg   { X86::SUBR_Fp80m64 , X86::SUBR_F64m },
76206f32e7eSjoerg   { X86::SUBR_FpI16m32, X86::SUBR_FI16m},
76306f32e7eSjoerg   { X86::SUBR_FpI16m64, X86::SUBR_FI16m},
76406f32e7eSjoerg   { X86::SUBR_FpI16m80, X86::SUBR_FI16m},
76506f32e7eSjoerg   { X86::SUBR_FpI32m32, X86::SUBR_FI32m},
76606f32e7eSjoerg   { X86::SUBR_FpI32m64, X86::SUBR_FI32m},
76706f32e7eSjoerg   { X86::SUBR_FpI32m80, X86::SUBR_FI32m},
76806f32e7eSjoerg   { X86::SUB_Fp32m    , X86::SUB_F32m  },
76906f32e7eSjoerg   { X86::SUB_Fp64m    , X86::SUB_F64m  },
77006f32e7eSjoerg   { X86::SUB_Fp64m32  , X86::SUB_F32m  },
77106f32e7eSjoerg   { X86::SUB_Fp80m32  , X86::SUB_F32m  },
77206f32e7eSjoerg   { X86::SUB_Fp80m64  , X86::SUB_F64m  },
77306f32e7eSjoerg   { X86::SUB_FpI16m32 , X86::SUB_FI16m },
77406f32e7eSjoerg   { X86::SUB_FpI16m64 , X86::SUB_FI16m },
77506f32e7eSjoerg   { X86::SUB_FpI16m80 , X86::SUB_FI16m },
77606f32e7eSjoerg   { X86::SUB_FpI32m32 , X86::SUB_FI32m },
77706f32e7eSjoerg   { X86::SUB_FpI32m64 , X86::SUB_FI32m },
77806f32e7eSjoerg   { X86::SUB_FpI32m80 , X86::SUB_FI32m },
77906f32e7eSjoerg   { X86::TST_Fp32     , X86::TST_F     },
78006f32e7eSjoerg   { X86::TST_Fp64     , X86::TST_F     },
78106f32e7eSjoerg   { X86::TST_Fp80     , X86::TST_F     },
78206f32e7eSjoerg   { X86::UCOM_FpIr32  , X86::UCOM_FIr  },
78306f32e7eSjoerg   { X86::UCOM_FpIr64  , X86::UCOM_FIr  },
78406f32e7eSjoerg   { X86::UCOM_FpIr80  , X86::UCOM_FIr  },
78506f32e7eSjoerg   { X86::UCOM_Fpr32   , X86::UCOM_Fr   },
78606f32e7eSjoerg   { X86::UCOM_Fpr64   , X86::UCOM_Fr   },
78706f32e7eSjoerg   { X86::UCOM_Fpr80   , X86::UCOM_Fr   },
78806f32e7eSjoerg };
78906f32e7eSjoerg 
getConcreteOpcode(unsigned Opcode)79006f32e7eSjoerg static unsigned getConcreteOpcode(unsigned Opcode) {
79106f32e7eSjoerg   ASSERT_SORTED(OpcodeTable);
79206f32e7eSjoerg   int Opc = Lookup(OpcodeTable, Opcode);
79306f32e7eSjoerg   assert(Opc != -1 && "FP Stack instruction not in OpcodeTable!");
79406f32e7eSjoerg   return Opc;
79506f32e7eSjoerg }
79606f32e7eSjoerg 
79706f32e7eSjoerg //===----------------------------------------------------------------------===//
79806f32e7eSjoerg // Helper Methods
79906f32e7eSjoerg //===----------------------------------------------------------------------===//
80006f32e7eSjoerg 
80106f32e7eSjoerg // PopTable - Sorted map of instructions to their popping version.  The first
80206f32e7eSjoerg // element is an instruction, the second is the version which pops.
80306f32e7eSjoerg //
80406f32e7eSjoerg static const TableEntry PopTable[] = {
80506f32e7eSjoerg   { X86::ADD_FrST0 , X86::ADD_FPrST0  },
80606f32e7eSjoerg 
807*da58b97aSjoerg   { X86::COMP_FST0r, X86::FCOMPP      },
808*da58b97aSjoerg   { X86::COM_FIr   , X86::COM_FIPr    },
809*da58b97aSjoerg   { X86::COM_FST0r , X86::COMP_FST0r  },
810*da58b97aSjoerg 
81106f32e7eSjoerg   { X86::DIVR_FrST0, X86::DIVR_FPrST0 },
81206f32e7eSjoerg   { X86::DIV_FrST0 , X86::DIV_FPrST0  },
81306f32e7eSjoerg 
81406f32e7eSjoerg   { X86::IST_F16m  , X86::IST_FP16m   },
81506f32e7eSjoerg   { X86::IST_F32m  , X86::IST_FP32m   },
81606f32e7eSjoerg 
81706f32e7eSjoerg   { X86::MUL_FrST0 , X86::MUL_FPrST0  },
81806f32e7eSjoerg 
81906f32e7eSjoerg   { X86::ST_F32m   , X86::ST_FP32m    },
82006f32e7eSjoerg   { X86::ST_F64m   , X86::ST_FP64m    },
82106f32e7eSjoerg   { X86::ST_Frr    , X86::ST_FPrr     },
82206f32e7eSjoerg 
82306f32e7eSjoerg   { X86::SUBR_FrST0, X86::SUBR_FPrST0 },
82406f32e7eSjoerg   { X86::SUB_FrST0 , X86::SUB_FPrST0  },
82506f32e7eSjoerg 
82606f32e7eSjoerg   { X86::UCOM_FIr  , X86::UCOM_FIPr   },
82706f32e7eSjoerg 
82806f32e7eSjoerg   { X86::UCOM_FPr  , X86::UCOM_FPPr   },
82906f32e7eSjoerg   { X86::UCOM_Fr   , X86::UCOM_FPr    },
83006f32e7eSjoerg };
83106f32e7eSjoerg 
83206f32e7eSjoerg /// popStackAfter - Pop the current value off of the top of the FP stack after
83306f32e7eSjoerg /// the specified instruction.  This attempts to be sneaky and combine the pop
83406f32e7eSjoerg /// into the instruction itself if possible.  The iterator is left pointing to
83506f32e7eSjoerg /// the last instruction, be it a new pop instruction inserted, or the old
83606f32e7eSjoerg /// instruction if it was modified in place.
83706f32e7eSjoerg ///
popStackAfter(MachineBasicBlock::iterator & I)83806f32e7eSjoerg void FPS::popStackAfter(MachineBasicBlock::iterator &I) {
83906f32e7eSjoerg   MachineInstr &MI = *I;
84006f32e7eSjoerg   const DebugLoc &dl = MI.getDebugLoc();
84106f32e7eSjoerg   ASSERT_SORTED(PopTable);
84206f32e7eSjoerg 
84306f32e7eSjoerg   popReg();
84406f32e7eSjoerg 
84506f32e7eSjoerg   // Check to see if there is a popping version of this instruction...
84606f32e7eSjoerg   int Opcode = Lookup(PopTable, I->getOpcode());
84706f32e7eSjoerg   if (Opcode != -1) {
84806f32e7eSjoerg     I->setDesc(TII->get(Opcode));
849*da58b97aSjoerg     if (Opcode == X86::FCOMPP || Opcode == X86::UCOM_FPPr)
85006f32e7eSjoerg       I->RemoveOperand(0);
85106f32e7eSjoerg   } else {    // Insert an explicit pop
85206f32e7eSjoerg     I = BuildMI(*MBB, ++I, dl, TII->get(X86::ST_FPrr)).addReg(X86::ST0);
85306f32e7eSjoerg   }
85406f32e7eSjoerg }
85506f32e7eSjoerg 
85606f32e7eSjoerg /// freeStackSlotAfter - Free the specified register from the register stack, so
85706f32e7eSjoerg /// that it is no longer in a register.  If the register is currently at the top
85806f32e7eSjoerg /// of the stack, we just pop the current instruction, otherwise we store the
85906f32e7eSjoerg /// current top-of-stack into the specified slot, then pop the top of stack.
freeStackSlotAfter(MachineBasicBlock::iterator & I,unsigned FPRegNo)86006f32e7eSjoerg void FPS::freeStackSlotAfter(MachineBasicBlock::iterator &I, unsigned FPRegNo) {
86106f32e7eSjoerg   if (getStackEntry(0) == FPRegNo) {  // already at the top of stack? easy.
86206f32e7eSjoerg     popStackAfter(I);
86306f32e7eSjoerg     return;
86406f32e7eSjoerg   }
86506f32e7eSjoerg 
86606f32e7eSjoerg   // Otherwise, store the top of stack into the dead slot, killing the operand
86706f32e7eSjoerg   // without having to add in an explicit xchg then pop.
86806f32e7eSjoerg   //
86906f32e7eSjoerg   I = freeStackSlotBefore(++I, FPRegNo);
87006f32e7eSjoerg }
87106f32e7eSjoerg 
87206f32e7eSjoerg /// freeStackSlotBefore - Free the specified register without trying any
87306f32e7eSjoerg /// folding.
87406f32e7eSjoerg MachineBasicBlock::iterator
freeStackSlotBefore(MachineBasicBlock::iterator I,unsigned FPRegNo)87506f32e7eSjoerg FPS::freeStackSlotBefore(MachineBasicBlock::iterator I, unsigned FPRegNo) {
87606f32e7eSjoerg   unsigned STReg    = getSTReg(FPRegNo);
87706f32e7eSjoerg   unsigned OldSlot  = getSlot(FPRegNo);
87806f32e7eSjoerg   unsigned TopReg   = Stack[StackTop-1];
87906f32e7eSjoerg   Stack[OldSlot]    = TopReg;
88006f32e7eSjoerg   RegMap[TopReg]    = OldSlot;
88106f32e7eSjoerg   RegMap[FPRegNo]   = ~0;
88206f32e7eSjoerg   Stack[--StackTop] = ~0;
88306f32e7eSjoerg   return BuildMI(*MBB, I, DebugLoc(), TII->get(X86::ST_FPrr))
88406f32e7eSjoerg       .addReg(STReg)
88506f32e7eSjoerg       .getInstr();
88606f32e7eSjoerg }
88706f32e7eSjoerg 
88806f32e7eSjoerg /// adjustLiveRegs - Kill and revive registers such that exactly the FP
88906f32e7eSjoerg /// registers with a bit in Mask are live.
adjustLiveRegs(unsigned Mask,MachineBasicBlock::iterator I)89006f32e7eSjoerg void FPS::adjustLiveRegs(unsigned Mask, MachineBasicBlock::iterator I) {
89106f32e7eSjoerg   unsigned Defs = Mask;
89206f32e7eSjoerg   unsigned Kills = 0;
89306f32e7eSjoerg   for (unsigned i = 0; i < StackTop; ++i) {
89406f32e7eSjoerg     unsigned RegNo = Stack[i];
89506f32e7eSjoerg     if (!(Defs & (1 << RegNo)))
89606f32e7eSjoerg       // This register is live, but we don't want it.
89706f32e7eSjoerg       Kills |= (1 << RegNo);
89806f32e7eSjoerg     else
89906f32e7eSjoerg       // We don't need to imp-def this live register.
90006f32e7eSjoerg       Defs &= ~(1 << RegNo);
90106f32e7eSjoerg   }
90206f32e7eSjoerg   assert((Kills & Defs) == 0 && "Register needs killing and def'ing?");
90306f32e7eSjoerg 
90406f32e7eSjoerg   // Produce implicit-defs for free by using killed registers.
90506f32e7eSjoerg   while (Kills && Defs) {
90606f32e7eSjoerg     unsigned KReg = countTrailingZeros(Kills);
90706f32e7eSjoerg     unsigned DReg = countTrailingZeros(Defs);
90806f32e7eSjoerg     LLVM_DEBUG(dbgs() << "Renaming %fp" << KReg << " as imp %fp" << DReg
90906f32e7eSjoerg                       << "\n");
91006f32e7eSjoerg     std::swap(Stack[getSlot(KReg)], Stack[getSlot(DReg)]);
91106f32e7eSjoerg     std::swap(RegMap[KReg], RegMap[DReg]);
91206f32e7eSjoerg     Kills &= ~(1 << KReg);
91306f32e7eSjoerg     Defs &= ~(1 << DReg);
91406f32e7eSjoerg   }
91506f32e7eSjoerg 
91606f32e7eSjoerg   // Kill registers by popping.
91706f32e7eSjoerg   if (Kills && I != MBB->begin()) {
91806f32e7eSjoerg     MachineBasicBlock::iterator I2 = std::prev(I);
91906f32e7eSjoerg     while (StackTop) {
92006f32e7eSjoerg       unsigned KReg = getStackEntry(0);
92106f32e7eSjoerg       if (!(Kills & (1 << KReg)))
92206f32e7eSjoerg         break;
92306f32e7eSjoerg       LLVM_DEBUG(dbgs() << "Popping %fp" << KReg << "\n");
92406f32e7eSjoerg       popStackAfter(I2);
92506f32e7eSjoerg       Kills &= ~(1 << KReg);
92606f32e7eSjoerg     }
92706f32e7eSjoerg   }
92806f32e7eSjoerg 
92906f32e7eSjoerg   // Manually kill the rest.
93006f32e7eSjoerg   while (Kills) {
93106f32e7eSjoerg     unsigned KReg = countTrailingZeros(Kills);
93206f32e7eSjoerg     LLVM_DEBUG(dbgs() << "Killing %fp" << KReg << "\n");
93306f32e7eSjoerg     freeStackSlotBefore(I, KReg);
93406f32e7eSjoerg     Kills &= ~(1 << KReg);
93506f32e7eSjoerg   }
93606f32e7eSjoerg 
93706f32e7eSjoerg   // Load zeros for all the imp-defs.
93806f32e7eSjoerg   while(Defs) {
93906f32e7eSjoerg     unsigned DReg = countTrailingZeros(Defs);
94006f32e7eSjoerg     LLVM_DEBUG(dbgs() << "Defining %fp" << DReg << " as 0\n");
94106f32e7eSjoerg     BuildMI(*MBB, I, DebugLoc(), TII->get(X86::LD_F0));
94206f32e7eSjoerg     pushReg(DReg);
94306f32e7eSjoerg     Defs &= ~(1 << DReg);
94406f32e7eSjoerg   }
94506f32e7eSjoerg 
94606f32e7eSjoerg   // Now we should have the correct registers live.
94706f32e7eSjoerg   LLVM_DEBUG(dumpStack());
94806f32e7eSjoerg   assert(StackTop == countPopulation(Mask) && "Live count mismatch");
94906f32e7eSjoerg }
95006f32e7eSjoerg 
95106f32e7eSjoerg /// shuffleStackTop - emit fxch instructions before I to shuffle the top
95206f32e7eSjoerg /// FixCount entries into the order given by FixStack.
95306f32e7eSjoerg /// FIXME: Is there a better algorithm than insertion sort?
shuffleStackTop(const unsigned char * FixStack,unsigned FixCount,MachineBasicBlock::iterator I)95406f32e7eSjoerg void FPS::shuffleStackTop(const unsigned char *FixStack,
95506f32e7eSjoerg                           unsigned FixCount,
95606f32e7eSjoerg                           MachineBasicBlock::iterator I) {
95706f32e7eSjoerg   // Move items into place, starting from the desired stack bottom.
95806f32e7eSjoerg   while (FixCount--) {
95906f32e7eSjoerg     // Old register at position FixCount.
96006f32e7eSjoerg     unsigned OldReg = getStackEntry(FixCount);
96106f32e7eSjoerg     // Desired register at position FixCount.
96206f32e7eSjoerg     unsigned Reg = FixStack[FixCount];
96306f32e7eSjoerg     if (Reg == OldReg)
96406f32e7eSjoerg       continue;
96506f32e7eSjoerg     // (Reg st0) (OldReg st0) = (Reg OldReg st0)
96606f32e7eSjoerg     moveToTop(Reg, I);
96706f32e7eSjoerg     if (FixCount > 0)
96806f32e7eSjoerg       moveToTop(OldReg, I);
96906f32e7eSjoerg   }
97006f32e7eSjoerg   LLVM_DEBUG(dumpStack());
97106f32e7eSjoerg }
97206f32e7eSjoerg 
97306f32e7eSjoerg 
97406f32e7eSjoerg //===----------------------------------------------------------------------===//
97506f32e7eSjoerg // Instruction transformation implementation
97606f32e7eSjoerg //===----------------------------------------------------------------------===//
97706f32e7eSjoerg 
handleCall(MachineBasicBlock::iterator & I)97806f32e7eSjoerg void FPS::handleCall(MachineBasicBlock::iterator &I) {
979*da58b97aSjoerg   MachineInstr &MI = *I;
98006f32e7eSjoerg   unsigned STReturns = 0;
98106f32e7eSjoerg 
982*da58b97aSjoerg   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
983*da58b97aSjoerg     MachineOperand &Op = MI.getOperand(i);
984*da58b97aSjoerg     if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6)
98506f32e7eSjoerg       continue;
98606f32e7eSjoerg 
987*da58b97aSjoerg     assert(Op.isImplicit() && "Expected implicit def/use");
98806f32e7eSjoerg 
989*da58b97aSjoerg     if (Op.isDef())
990*da58b97aSjoerg       STReturns |= 1 << getFPReg(Op);
99106f32e7eSjoerg 
992*da58b97aSjoerg     // Remove the operand so that later passes don't see it.
993*da58b97aSjoerg     MI.RemoveOperand(i);
994*da58b97aSjoerg     --i;
995*da58b97aSjoerg     --e;
99606f32e7eSjoerg   }
99706f32e7eSjoerg 
99806f32e7eSjoerg   unsigned N = countTrailingOnes(STReturns);
99906f32e7eSjoerg 
100006f32e7eSjoerg   // FP registers used for function return must be consecutive starting at
100106f32e7eSjoerg   // FP0
100206f32e7eSjoerg   assert(STReturns == 0 || (isMask_32(STReturns) && N <= 2));
100306f32e7eSjoerg 
100406f32e7eSjoerg   // Reset the FP Stack - It is required because of possible leftovers from
100506f32e7eSjoerg   // passed arguments. The caller should assume that the FP stack is
100606f32e7eSjoerg   // returned empty (unless the callee returns values on FP stack).
100706f32e7eSjoerg   while (StackTop > 0)
100806f32e7eSjoerg     popReg();
100906f32e7eSjoerg 
101006f32e7eSjoerg   for (unsigned I = 0; I < N; ++I)
101106f32e7eSjoerg     pushReg(N - I - 1);
101206f32e7eSjoerg }
101306f32e7eSjoerg 
101406f32e7eSjoerg /// If RET has an FP register use operand, pass the first one in ST(0) and
101506f32e7eSjoerg /// the second one in ST(1).
handleReturn(MachineBasicBlock::iterator & I)101606f32e7eSjoerg void FPS::handleReturn(MachineBasicBlock::iterator &I) {
101706f32e7eSjoerg   MachineInstr &MI = *I;
101806f32e7eSjoerg 
101906f32e7eSjoerg   // Find the register operands.
102006f32e7eSjoerg   unsigned FirstFPRegOp = ~0U, SecondFPRegOp = ~0U;
102106f32e7eSjoerg   unsigned LiveMask = 0;
102206f32e7eSjoerg 
102306f32e7eSjoerg   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
102406f32e7eSjoerg     MachineOperand &Op = MI.getOperand(i);
102506f32e7eSjoerg     if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6)
102606f32e7eSjoerg       continue;
102706f32e7eSjoerg     // FP Register uses must be kills unless there are two uses of the same
102806f32e7eSjoerg     // register, in which case only one will be a kill.
102906f32e7eSjoerg     assert(Op.isUse() &&
103006f32e7eSjoerg            (Op.isKill() ||                    // Marked kill.
103106f32e7eSjoerg             getFPReg(Op) == FirstFPRegOp ||   // Second instance.
103206f32e7eSjoerg             MI.killsRegister(Op.getReg())) && // Later use is marked kill.
103306f32e7eSjoerg            "Ret only defs operands, and values aren't live beyond it");
103406f32e7eSjoerg 
103506f32e7eSjoerg     if (FirstFPRegOp == ~0U)
103606f32e7eSjoerg       FirstFPRegOp = getFPReg(Op);
103706f32e7eSjoerg     else {
103806f32e7eSjoerg       assert(SecondFPRegOp == ~0U && "More than two fp operands!");
103906f32e7eSjoerg       SecondFPRegOp = getFPReg(Op);
104006f32e7eSjoerg     }
104106f32e7eSjoerg     LiveMask |= (1 << getFPReg(Op));
104206f32e7eSjoerg 
104306f32e7eSjoerg     // Remove the operand so that later passes don't see it.
104406f32e7eSjoerg     MI.RemoveOperand(i);
104506f32e7eSjoerg     --i;
104606f32e7eSjoerg     --e;
104706f32e7eSjoerg   }
104806f32e7eSjoerg 
104906f32e7eSjoerg   // We may have been carrying spurious live-ins, so make sure only the
105006f32e7eSjoerg   // returned registers are left live.
105106f32e7eSjoerg   adjustLiveRegs(LiveMask, MI);
105206f32e7eSjoerg   if (!LiveMask) return;  // Quick check to see if any are possible.
105306f32e7eSjoerg 
105406f32e7eSjoerg   // There are only four possibilities here:
105506f32e7eSjoerg   // 1) we are returning a single FP value.  In this case, it has to be in
105606f32e7eSjoerg   //    ST(0) already, so just declare success by removing the value from the
105706f32e7eSjoerg   //    FP Stack.
105806f32e7eSjoerg   if (SecondFPRegOp == ~0U) {
105906f32e7eSjoerg     // Assert that the top of stack contains the right FP register.
106006f32e7eSjoerg     assert(StackTop == 1 && FirstFPRegOp == getStackEntry(0) &&
106106f32e7eSjoerg            "Top of stack not the right register for RET!");
106206f32e7eSjoerg 
106306f32e7eSjoerg     // Ok, everything is good, mark the value as not being on the stack
106406f32e7eSjoerg     // anymore so that our assertion about the stack being empty at end of
106506f32e7eSjoerg     // block doesn't fire.
106606f32e7eSjoerg     StackTop = 0;
106706f32e7eSjoerg     return;
106806f32e7eSjoerg   }
106906f32e7eSjoerg 
107006f32e7eSjoerg   // Otherwise, we are returning two values:
107106f32e7eSjoerg   // 2) If returning the same value for both, we only have one thing in the FP
107206f32e7eSjoerg   //    stack.  Consider:  RET FP1, FP1
107306f32e7eSjoerg   if (StackTop == 1) {
107406f32e7eSjoerg     assert(FirstFPRegOp == SecondFPRegOp && FirstFPRegOp == getStackEntry(0)&&
107506f32e7eSjoerg            "Stack misconfiguration for RET!");
107606f32e7eSjoerg 
107706f32e7eSjoerg     // Duplicate the TOS so that we return it twice.  Just pick some other FPx
107806f32e7eSjoerg     // register to hold it.
107906f32e7eSjoerg     unsigned NewReg = ScratchFPReg;
108006f32e7eSjoerg     duplicateToTop(FirstFPRegOp, NewReg, MI);
108106f32e7eSjoerg     FirstFPRegOp = NewReg;
108206f32e7eSjoerg   }
108306f32e7eSjoerg 
108406f32e7eSjoerg   /// Okay we know we have two different FPx operands now:
108506f32e7eSjoerg   assert(StackTop == 2 && "Must have two values live!");
108606f32e7eSjoerg 
108706f32e7eSjoerg   /// 3) If SecondFPRegOp is currently in ST(0) and FirstFPRegOp is currently
108806f32e7eSjoerg   ///    in ST(1).  In this case, emit an fxch.
108906f32e7eSjoerg   if (getStackEntry(0) == SecondFPRegOp) {
109006f32e7eSjoerg     assert(getStackEntry(1) == FirstFPRegOp && "Unknown regs live");
109106f32e7eSjoerg     moveToTop(FirstFPRegOp, MI);
109206f32e7eSjoerg   }
109306f32e7eSjoerg 
109406f32e7eSjoerg   /// 4) Finally, FirstFPRegOp must be in ST(0) and SecondFPRegOp must be in
109506f32e7eSjoerg   /// ST(1).  Just remove both from our understanding of the stack and return.
109606f32e7eSjoerg   assert(getStackEntry(0) == FirstFPRegOp && "Unknown regs live");
109706f32e7eSjoerg   assert(getStackEntry(1) == SecondFPRegOp && "Unknown regs live");
109806f32e7eSjoerg   StackTop = 0;
109906f32e7eSjoerg }
110006f32e7eSjoerg 
110106f32e7eSjoerg /// handleZeroArgFP - ST(0) = fld0    ST(0) = flds <mem>
110206f32e7eSjoerg ///
handleZeroArgFP(MachineBasicBlock::iterator & I)110306f32e7eSjoerg void FPS::handleZeroArgFP(MachineBasicBlock::iterator &I) {
110406f32e7eSjoerg   MachineInstr &MI = *I;
110506f32e7eSjoerg   unsigned DestReg = getFPReg(MI.getOperand(0));
110606f32e7eSjoerg 
110706f32e7eSjoerg   // Change from the pseudo instruction to the concrete instruction.
110806f32e7eSjoerg   MI.RemoveOperand(0); // Remove the explicit ST(0) operand
110906f32e7eSjoerg   MI.setDesc(TII->get(getConcreteOpcode(MI.getOpcode())));
111006f32e7eSjoerg   MI.addOperand(
111106f32e7eSjoerg       MachineOperand::CreateReg(X86::ST0, /*isDef*/ true, /*isImp*/ true));
111206f32e7eSjoerg 
111306f32e7eSjoerg   // Result gets pushed on the stack.
111406f32e7eSjoerg   pushReg(DestReg);
111506f32e7eSjoerg }
111606f32e7eSjoerg 
111706f32e7eSjoerg /// handleOneArgFP - fst <mem>, ST(0)
111806f32e7eSjoerg ///
handleOneArgFP(MachineBasicBlock::iterator & I)111906f32e7eSjoerg void FPS::handleOneArgFP(MachineBasicBlock::iterator &I) {
112006f32e7eSjoerg   MachineInstr &MI = *I;
112106f32e7eSjoerg   unsigned NumOps = MI.getDesc().getNumOperands();
112206f32e7eSjoerg   assert((NumOps == X86::AddrNumOperands + 1 || NumOps == 1) &&
112306f32e7eSjoerg          "Can only handle fst* & ftst instructions!");
112406f32e7eSjoerg 
112506f32e7eSjoerg   // Is this the last use of the source register?
112606f32e7eSjoerg   unsigned Reg = getFPReg(MI.getOperand(NumOps - 1));
112706f32e7eSjoerg   bool KillsSrc = MI.killsRegister(X86::FP0 + Reg);
112806f32e7eSjoerg 
112906f32e7eSjoerg   // FISTP64m is strange because there isn't a non-popping versions.
113006f32e7eSjoerg   // If we have one _and_ we don't want to pop the operand, duplicate the value
113106f32e7eSjoerg   // on the stack instead of moving it.  This ensure that popping the value is
113206f32e7eSjoerg   // always ok.
113306f32e7eSjoerg   // Ditto FISTTP16m, FISTTP32m, FISTTP64m, ST_FpP80m.
113406f32e7eSjoerg   //
113506f32e7eSjoerg   if (!KillsSrc && (MI.getOpcode() == X86::IST_Fp64m32 ||
113606f32e7eSjoerg                     MI.getOpcode() == X86::ISTT_Fp16m32 ||
113706f32e7eSjoerg                     MI.getOpcode() == X86::ISTT_Fp32m32 ||
113806f32e7eSjoerg                     MI.getOpcode() == X86::ISTT_Fp64m32 ||
113906f32e7eSjoerg                     MI.getOpcode() == X86::IST_Fp64m64 ||
114006f32e7eSjoerg                     MI.getOpcode() == X86::ISTT_Fp16m64 ||
114106f32e7eSjoerg                     MI.getOpcode() == X86::ISTT_Fp32m64 ||
114206f32e7eSjoerg                     MI.getOpcode() == X86::ISTT_Fp64m64 ||
114306f32e7eSjoerg                     MI.getOpcode() == X86::IST_Fp64m80 ||
114406f32e7eSjoerg                     MI.getOpcode() == X86::ISTT_Fp16m80 ||
114506f32e7eSjoerg                     MI.getOpcode() == X86::ISTT_Fp32m80 ||
114606f32e7eSjoerg                     MI.getOpcode() == X86::ISTT_Fp64m80 ||
114706f32e7eSjoerg                     MI.getOpcode() == X86::ST_FpP80m)) {
114806f32e7eSjoerg     duplicateToTop(Reg, ScratchFPReg, I);
114906f32e7eSjoerg   } else {
115006f32e7eSjoerg     moveToTop(Reg, I);            // Move to the top of the stack...
115106f32e7eSjoerg   }
115206f32e7eSjoerg 
115306f32e7eSjoerg   // Convert from the pseudo instruction to the concrete instruction.
115406f32e7eSjoerg   MI.RemoveOperand(NumOps - 1); // Remove explicit ST(0) operand
115506f32e7eSjoerg   MI.setDesc(TII->get(getConcreteOpcode(MI.getOpcode())));
115606f32e7eSjoerg   MI.addOperand(
115706f32e7eSjoerg       MachineOperand::CreateReg(X86::ST0, /*isDef*/ false, /*isImp*/ true));
115806f32e7eSjoerg 
115906f32e7eSjoerg   if (MI.getOpcode() == X86::IST_FP64m || MI.getOpcode() == X86::ISTT_FP16m ||
116006f32e7eSjoerg       MI.getOpcode() == X86::ISTT_FP32m || MI.getOpcode() == X86::ISTT_FP64m ||
116106f32e7eSjoerg       MI.getOpcode() == X86::ST_FP80m) {
116206f32e7eSjoerg     if (StackTop == 0)
116306f32e7eSjoerg       report_fatal_error("Stack empty??");
116406f32e7eSjoerg     --StackTop;
116506f32e7eSjoerg   } else if (KillsSrc) { // Last use of operand?
116606f32e7eSjoerg     popStackAfter(I);
116706f32e7eSjoerg   }
116806f32e7eSjoerg }
116906f32e7eSjoerg 
117006f32e7eSjoerg 
117106f32e7eSjoerg /// handleOneArgFPRW: Handle instructions that read from the top of stack and
117206f32e7eSjoerg /// replace the value with a newly computed value.  These instructions may have
117306f32e7eSjoerg /// non-fp operands after their FP operands.
117406f32e7eSjoerg ///
117506f32e7eSjoerg ///  Examples:
117606f32e7eSjoerg ///     R1 = fchs R2
117706f32e7eSjoerg ///     R1 = fadd R2, [mem]
117806f32e7eSjoerg ///
handleOneArgFPRW(MachineBasicBlock::iterator & I)117906f32e7eSjoerg void FPS::handleOneArgFPRW(MachineBasicBlock::iterator &I) {
118006f32e7eSjoerg   MachineInstr &MI = *I;
118106f32e7eSjoerg #ifndef NDEBUG
118206f32e7eSjoerg   unsigned NumOps = MI.getDesc().getNumOperands();
118306f32e7eSjoerg   assert(NumOps >= 2 && "FPRW instructions must have 2 ops!!");
118406f32e7eSjoerg #endif
118506f32e7eSjoerg 
118606f32e7eSjoerg   // Is this the last use of the source register?
118706f32e7eSjoerg   unsigned Reg = getFPReg(MI.getOperand(1));
118806f32e7eSjoerg   bool KillsSrc = MI.killsRegister(X86::FP0 + Reg);
118906f32e7eSjoerg 
119006f32e7eSjoerg   if (KillsSrc) {
119106f32e7eSjoerg     // If this is the last use of the source register, just make sure it's on
119206f32e7eSjoerg     // the top of the stack.
119306f32e7eSjoerg     moveToTop(Reg, I);
119406f32e7eSjoerg     if (StackTop == 0)
119506f32e7eSjoerg       report_fatal_error("Stack cannot be empty!");
119606f32e7eSjoerg     --StackTop;
119706f32e7eSjoerg     pushReg(getFPReg(MI.getOperand(0)));
119806f32e7eSjoerg   } else {
119906f32e7eSjoerg     // If this is not the last use of the source register, _copy_ it to the top
120006f32e7eSjoerg     // of the stack.
120106f32e7eSjoerg     duplicateToTop(Reg, getFPReg(MI.getOperand(0)), I);
120206f32e7eSjoerg   }
120306f32e7eSjoerg 
120406f32e7eSjoerg   // Change from the pseudo instruction to the concrete instruction.
120506f32e7eSjoerg   MI.RemoveOperand(1); // Drop the source operand.
120606f32e7eSjoerg   MI.RemoveOperand(0); // Drop the destination operand.
120706f32e7eSjoerg   MI.setDesc(TII->get(getConcreteOpcode(MI.getOpcode())));
120806f32e7eSjoerg }
120906f32e7eSjoerg 
121006f32e7eSjoerg 
121106f32e7eSjoerg //===----------------------------------------------------------------------===//
121206f32e7eSjoerg // Define tables of various ways to map pseudo instructions
121306f32e7eSjoerg //
121406f32e7eSjoerg 
121506f32e7eSjoerg // ForwardST0Table - Map: A = B op C  into: ST(0) = ST(0) op ST(i)
121606f32e7eSjoerg static const TableEntry ForwardST0Table[] = {
121706f32e7eSjoerg   { X86::ADD_Fp32  , X86::ADD_FST0r },
121806f32e7eSjoerg   { X86::ADD_Fp64  , X86::ADD_FST0r },
121906f32e7eSjoerg   { X86::ADD_Fp80  , X86::ADD_FST0r },
122006f32e7eSjoerg   { X86::DIV_Fp32  , X86::DIV_FST0r },
122106f32e7eSjoerg   { X86::DIV_Fp64  , X86::DIV_FST0r },
122206f32e7eSjoerg   { X86::DIV_Fp80  , X86::DIV_FST0r },
122306f32e7eSjoerg   { X86::MUL_Fp32  , X86::MUL_FST0r },
122406f32e7eSjoerg   { X86::MUL_Fp64  , X86::MUL_FST0r },
122506f32e7eSjoerg   { X86::MUL_Fp80  , X86::MUL_FST0r },
122606f32e7eSjoerg   { X86::SUB_Fp32  , X86::SUB_FST0r },
122706f32e7eSjoerg   { X86::SUB_Fp64  , X86::SUB_FST0r },
122806f32e7eSjoerg   { X86::SUB_Fp80  , X86::SUB_FST0r },
122906f32e7eSjoerg };
123006f32e7eSjoerg 
123106f32e7eSjoerg // ReverseST0Table - Map: A = B op C  into: ST(0) = ST(i) op ST(0)
123206f32e7eSjoerg static const TableEntry ReverseST0Table[] = {
123306f32e7eSjoerg   { X86::ADD_Fp32  , X86::ADD_FST0r  },   // commutative
123406f32e7eSjoerg   { X86::ADD_Fp64  , X86::ADD_FST0r  },   // commutative
123506f32e7eSjoerg   { X86::ADD_Fp80  , X86::ADD_FST0r  },   // commutative
123606f32e7eSjoerg   { X86::DIV_Fp32  , X86::DIVR_FST0r },
123706f32e7eSjoerg   { X86::DIV_Fp64  , X86::DIVR_FST0r },
123806f32e7eSjoerg   { X86::DIV_Fp80  , X86::DIVR_FST0r },
123906f32e7eSjoerg   { X86::MUL_Fp32  , X86::MUL_FST0r  },   // commutative
124006f32e7eSjoerg   { X86::MUL_Fp64  , X86::MUL_FST0r  },   // commutative
124106f32e7eSjoerg   { X86::MUL_Fp80  , X86::MUL_FST0r  },   // commutative
124206f32e7eSjoerg   { X86::SUB_Fp32  , X86::SUBR_FST0r },
124306f32e7eSjoerg   { X86::SUB_Fp64  , X86::SUBR_FST0r },
124406f32e7eSjoerg   { X86::SUB_Fp80  , X86::SUBR_FST0r },
124506f32e7eSjoerg };
124606f32e7eSjoerg 
124706f32e7eSjoerg // ForwardSTiTable - Map: A = B op C  into: ST(i) = ST(0) op ST(i)
124806f32e7eSjoerg static const TableEntry ForwardSTiTable[] = {
124906f32e7eSjoerg   { X86::ADD_Fp32  , X86::ADD_FrST0  },   // commutative
125006f32e7eSjoerg   { X86::ADD_Fp64  , X86::ADD_FrST0  },   // commutative
125106f32e7eSjoerg   { X86::ADD_Fp80  , X86::ADD_FrST0  },   // commutative
125206f32e7eSjoerg   { X86::DIV_Fp32  , X86::DIVR_FrST0 },
125306f32e7eSjoerg   { X86::DIV_Fp64  , X86::DIVR_FrST0 },
125406f32e7eSjoerg   { X86::DIV_Fp80  , X86::DIVR_FrST0 },
125506f32e7eSjoerg   { X86::MUL_Fp32  , X86::MUL_FrST0  },   // commutative
125606f32e7eSjoerg   { X86::MUL_Fp64  , X86::MUL_FrST0  },   // commutative
125706f32e7eSjoerg   { X86::MUL_Fp80  , X86::MUL_FrST0  },   // commutative
125806f32e7eSjoerg   { X86::SUB_Fp32  , X86::SUBR_FrST0 },
125906f32e7eSjoerg   { X86::SUB_Fp64  , X86::SUBR_FrST0 },
126006f32e7eSjoerg   { X86::SUB_Fp80  , X86::SUBR_FrST0 },
126106f32e7eSjoerg };
126206f32e7eSjoerg 
126306f32e7eSjoerg // ReverseSTiTable - Map: A = B op C  into: ST(i) = ST(i) op ST(0)
126406f32e7eSjoerg static const TableEntry ReverseSTiTable[] = {
126506f32e7eSjoerg   { X86::ADD_Fp32  , X86::ADD_FrST0 },
126606f32e7eSjoerg   { X86::ADD_Fp64  , X86::ADD_FrST0 },
126706f32e7eSjoerg   { X86::ADD_Fp80  , X86::ADD_FrST0 },
126806f32e7eSjoerg   { X86::DIV_Fp32  , X86::DIV_FrST0 },
126906f32e7eSjoerg   { X86::DIV_Fp64  , X86::DIV_FrST0 },
127006f32e7eSjoerg   { X86::DIV_Fp80  , X86::DIV_FrST0 },
127106f32e7eSjoerg   { X86::MUL_Fp32  , X86::MUL_FrST0 },
127206f32e7eSjoerg   { X86::MUL_Fp64  , X86::MUL_FrST0 },
127306f32e7eSjoerg   { X86::MUL_Fp80  , X86::MUL_FrST0 },
127406f32e7eSjoerg   { X86::SUB_Fp32  , X86::SUB_FrST0 },
127506f32e7eSjoerg   { X86::SUB_Fp64  , X86::SUB_FrST0 },
127606f32e7eSjoerg   { X86::SUB_Fp80  , X86::SUB_FrST0 },
127706f32e7eSjoerg };
127806f32e7eSjoerg 
127906f32e7eSjoerg 
128006f32e7eSjoerg /// handleTwoArgFP - Handle instructions like FADD and friends which are virtual
128106f32e7eSjoerg /// instructions which need to be simplified and possibly transformed.
128206f32e7eSjoerg ///
128306f32e7eSjoerg /// Result: ST(0) = fsub  ST(0), ST(i)
128406f32e7eSjoerg ///         ST(i) = fsub  ST(0), ST(i)
128506f32e7eSjoerg ///         ST(0) = fsubr ST(0), ST(i)
128606f32e7eSjoerg ///         ST(i) = fsubr ST(0), ST(i)
128706f32e7eSjoerg ///
handleTwoArgFP(MachineBasicBlock::iterator & I)128806f32e7eSjoerg void FPS::handleTwoArgFP(MachineBasicBlock::iterator &I) {
128906f32e7eSjoerg   ASSERT_SORTED(ForwardST0Table); ASSERT_SORTED(ReverseST0Table);
129006f32e7eSjoerg   ASSERT_SORTED(ForwardSTiTable); ASSERT_SORTED(ReverseSTiTable);
129106f32e7eSjoerg   MachineInstr &MI = *I;
129206f32e7eSjoerg 
129306f32e7eSjoerg   unsigned NumOperands = MI.getDesc().getNumOperands();
129406f32e7eSjoerg   assert(NumOperands == 3 && "Illegal TwoArgFP instruction!");
129506f32e7eSjoerg   unsigned Dest = getFPReg(MI.getOperand(0));
129606f32e7eSjoerg   unsigned Op0 = getFPReg(MI.getOperand(NumOperands - 2));
129706f32e7eSjoerg   unsigned Op1 = getFPReg(MI.getOperand(NumOperands - 1));
129806f32e7eSjoerg   bool KillsOp0 = MI.killsRegister(X86::FP0 + Op0);
129906f32e7eSjoerg   bool KillsOp1 = MI.killsRegister(X86::FP0 + Op1);
1300*da58b97aSjoerg   const DebugLoc &dl = MI.getDebugLoc();
130106f32e7eSjoerg 
130206f32e7eSjoerg   unsigned TOS = getStackEntry(0);
130306f32e7eSjoerg 
130406f32e7eSjoerg   // One of our operands must be on the top of the stack.  If neither is yet, we
130506f32e7eSjoerg   // need to move one.
130606f32e7eSjoerg   if (Op0 != TOS && Op1 != TOS) {   // No operand at TOS?
130706f32e7eSjoerg     // We can choose to move either operand to the top of the stack.  If one of
130806f32e7eSjoerg     // the operands is killed by this instruction, we want that one so that we
130906f32e7eSjoerg     // can update right on top of the old version.
131006f32e7eSjoerg     if (KillsOp0) {
131106f32e7eSjoerg       moveToTop(Op0, I);         // Move dead operand to TOS.
131206f32e7eSjoerg       TOS = Op0;
131306f32e7eSjoerg     } else if (KillsOp1) {
131406f32e7eSjoerg       moveToTop(Op1, I);
131506f32e7eSjoerg       TOS = Op1;
131606f32e7eSjoerg     } else {
131706f32e7eSjoerg       // All of the operands are live after this instruction executes, so we
131806f32e7eSjoerg       // cannot update on top of any operand.  Because of this, we must
131906f32e7eSjoerg       // duplicate one of the stack elements to the top.  It doesn't matter
132006f32e7eSjoerg       // which one we pick.
132106f32e7eSjoerg       //
132206f32e7eSjoerg       duplicateToTop(Op0, Dest, I);
132306f32e7eSjoerg       Op0 = TOS = Dest;
132406f32e7eSjoerg       KillsOp0 = true;
132506f32e7eSjoerg     }
132606f32e7eSjoerg   } else if (!KillsOp0 && !KillsOp1) {
132706f32e7eSjoerg     // If we DO have one of our operands at the top of the stack, but we don't
132806f32e7eSjoerg     // have a dead operand, we must duplicate one of the operands to a new slot
132906f32e7eSjoerg     // on the stack.
133006f32e7eSjoerg     duplicateToTop(Op0, Dest, I);
133106f32e7eSjoerg     Op0 = TOS = Dest;
133206f32e7eSjoerg     KillsOp0 = true;
133306f32e7eSjoerg   }
133406f32e7eSjoerg 
133506f32e7eSjoerg   // Now we know that one of our operands is on the top of the stack, and at
133606f32e7eSjoerg   // least one of our operands is killed by this instruction.
133706f32e7eSjoerg   assert((TOS == Op0 || TOS == Op1) && (KillsOp0 || KillsOp1) &&
133806f32e7eSjoerg          "Stack conditions not set up right!");
133906f32e7eSjoerg 
134006f32e7eSjoerg   // We decide which form to use based on what is on the top of the stack, and
134106f32e7eSjoerg   // which operand is killed by this instruction.
134206f32e7eSjoerg   ArrayRef<TableEntry> InstTable;
134306f32e7eSjoerg   bool isForward = TOS == Op0;
134406f32e7eSjoerg   bool updateST0 = (TOS == Op0 && !KillsOp1) || (TOS == Op1 && !KillsOp0);
134506f32e7eSjoerg   if (updateST0) {
134606f32e7eSjoerg     if (isForward)
134706f32e7eSjoerg       InstTable = ForwardST0Table;
134806f32e7eSjoerg     else
134906f32e7eSjoerg       InstTable = ReverseST0Table;
135006f32e7eSjoerg   } else {
135106f32e7eSjoerg     if (isForward)
135206f32e7eSjoerg       InstTable = ForwardSTiTable;
135306f32e7eSjoerg     else
135406f32e7eSjoerg       InstTable = ReverseSTiTable;
135506f32e7eSjoerg   }
135606f32e7eSjoerg 
135706f32e7eSjoerg   int Opcode = Lookup(InstTable, MI.getOpcode());
135806f32e7eSjoerg   assert(Opcode != -1 && "Unknown TwoArgFP pseudo instruction!");
135906f32e7eSjoerg 
136006f32e7eSjoerg   // NotTOS - The register which is not on the top of stack...
136106f32e7eSjoerg   unsigned NotTOS = (TOS == Op0) ? Op1 : Op0;
136206f32e7eSjoerg 
136306f32e7eSjoerg   // Replace the old instruction with a new instruction
136406f32e7eSjoerg   MBB->remove(&*I++);
136506f32e7eSjoerg   I = BuildMI(*MBB, I, dl, TII->get(Opcode)).addReg(getSTReg(NotTOS));
136606f32e7eSjoerg 
1367*da58b97aSjoerg   if (!MI.mayRaiseFPException())
1368*da58b97aSjoerg     I->setFlag(MachineInstr::MIFlag::NoFPExcept);
1369*da58b97aSjoerg 
137006f32e7eSjoerg   // If both operands are killed, pop one off of the stack in addition to
137106f32e7eSjoerg   // overwriting the other one.
137206f32e7eSjoerg   if (KillsOp0 && KillsOp1 && Op0 != Op1) {
137306f32e7eSjoerg     assert(!updateST0 && "Should have updated other operand!");
137406f32e7eSjoerg     popStackAfter(I);   // Pop the top of stack
137506f32e7eSjoerg   }
137606f32e7eSjoerg 
137706f32e7eSjoerg   // Update stack information so that we know the destination register is now on
137806f32e7eSjoerg   // the stack.
137906f32e7eSjoerg   unsigned UpdatedSlot = getSlot(updateST0 ? TOS : NotTOS);
138006f32e7eSjoerg   assert(UpdatedSlot < StackTop && Dest < 7);
138106f32e7eSjoerg   Stack[UpdatedSlot]   = Dest;
138206f32e7eSjoerg   RegMap[Dest]         = UpdatedSlot;
138306f32e7eSjoerg   MBB->getParent()->DeleteMachineInstr(&MI); // Remove the old instruction
138406f32e7eSjoerg }
138506f32e7eSjoerg 
138606f32e7eSjoerg /// handleCompareFP - Handle FUCOM and FUCOMI instructions, which have two FP
138706f32e7eSjoerg /// register arguments and no explicit destinations.
138806f32e7eSjoerg ///
handleCompareFP(MachineBasicBlock::iterator & I)138906f32e7eSjoerg void FPS::handleCompareFP(MachineBasicBlock::iterator &I) {
139006f32e7eSjoerg   MachineInstr &MI = *I;
139106f32e7eSjoerg 
139206f32e7eSjoerg   unsigned NumOperands = MI.getDesc().getNumOperands();
139306f32e7eSjoerg   assert(NumOperands == 2 && "Illegal FUCOM* instruction!");
139406f32e7eSjoerg   unsigned Op0 = getFPReg(MI.getOperand(NumOperands - 2));
139506f32e7eSjoerg   unsigned Op1 = getFPReg(MI.getOperand(NumOperands - 1));
139606f32e7eSjoerg   bool KillsOp0 = MI.killsRegister(X86::FP0 + Op0);
139706f32e7eSjoerg   bool KillsOp1 = MI.killsRegister(X86::FP0 + Op1);
139806f32e7eSjoerg 
139906f32e7eSjoerg   // Make sure the first operand is on the top of stack, the other one can be
140006f32e7eSjoerg   // anywhere.
140106f32e7eSjoerg   moveToTop(Op0, I);
140206f32e7eSjoerg 
140306f32e7eSjoerg   // Change from the pseudo instruction to the concrete instruction.
140406f32e7eSjoerg   MI.getOperand(0).setReg(getSTReg(Op1));
140506f32e7eSjoerg   MI.RemoveOperand(1);
140606f32e7eSjoerg   MI.setDesc(TII->get(getConcreteOpcode(MI.getOpcode())));
140706f32e7eSjoerg 
140806f32e7eSjoerg   // If any of the operands are killed by this instruction, free them.
140906f32e7eSjoerg   if (KillsOp0) freeStackSlotAfter(I, Op0);
141006f32e7eSjoerg   if (KillsOp1 && Op0 != Op1) freeStackSlotAfter(I, Op1);
141106f32e7eSjoerg }
141206f32e7eSjoerg 
141306f32e7eSjoerg /// handleCondMovFP - Handle two address conditional move instructions.  These
141406f32e7eSjoerg /// instructions move a st(i) register to st(0) iff a condition is true.  These
141506f32e7eSjoerg /// instructions require that the first operand is at the top of the stack, but
141606f32e7eSjoerg /// otherwise don't modify the stack at all.
handleCondMovFP(MachineBasicBlock::iterator & I)141706f32e7eSjoerg void FPS::handleCondMovFP(MachineBasicBlock::iterator &I) {
141806f32e7eSjoerg   MachineInstr &MI = *I;
141906f32e7eSjoerg 
142006f32e7eSjoerg   unsigned Op0 = getFPReg(MI.getOperand(0));
142106f32e7eSjoerg   unsigned Op1 = getFPReg(MI.getOperand(2));
142206f32e7eSjoerg   bool KillsOp1 = MI.killsRegister(X86::FP0 + Op1);
142306f32e7eSjoerg 
142406f32e7eSjoerg   // The first operand *must* be on the top of the stack.
142506f32e7eSjoerg   moveToTop(Op0, I);
142606f32e7eSjoerg 
142706f32e7eSjoerg   // Change the second operand to the stack register that the operand is in.
142806f32e7eSjoerg   // Change from the pseudo instruction to the concrete instruction.
142906f32e7eSjoerg   MI.RemoveOperand(0);
143006f32e7eSjoerg   MI.RemoveOperand(1);
143106f32e7eSjoerg   MI.getOperand(0).setReg(getSTReg(Op1));
143206f32e7eSjoerg   MI.setDesc(TII->get(getConcreteOpcode(MI.getOpcode())));
143306f32e7eSjoerg 
143406f32e7eSjoerg   // If we kill the second operand, make sure to pop it from the stack.
143506f32e7eSjoerg   if (Op0 != Op1 && KillsOp1) {
143606f32e7eSjoerg     // Get this value off of the register stack.
143706f32e7eSjoerg     freeStackSlotAfter(I, Op1);
143806f32e7eSjoerg   }
143906f32e7eSjoerg }
144006f32e7eSjoerg 
144106f32e7eSjoerg 
144206f32e7eSjoerg /// handleSpecialFP - Handle special instructions which behave unlike other
144306f32e7eSjoerg /// floating point instructions.  This is primarily intended for use by pseudo
144406f32e7eSjoerg /// instructions.
144506f32e7eSjoerg ///
handleSpecialFP(MachineBasicBlock::iterator & Inst)144606f32e7eSjoerg void FPS::handleSpecialFP(MachineBasicBlock::iterator &Inst) {
144706f32e7eSjoerg   MachineInstr &MI = *Inst;
144806f32e7eSjoerg 
144906f32e7eSjoerg   if (MI.isCall()) {
145006f32e7eSjoerg     handleCall(Inst);
145106f32e7eSjoerg     return;
145206f32e7eSjoerg   }
145306f32e7eSjoerg 
145406f32e7eSjoerg   if (MI.isReturn()) {
145506f32e7eSjoerg     handleReturn(Inst);
145606f32e7eSjoerg     return;
145706f32e7eSjoerg   }
145806f32e7eSjoerg 
145906f32e7eSjoerg   switch (MI.getOpcode()) {
146006f32e7eSjoerg   default: llvm_unreachable("Unknown SpecialFP instruction!");
146106f32e7eSjoerg   case TargetOpcode::COPY: {
146206f32e7eSjoerg     // We handle three kinds of copies: FP <- FP, FP <- ST, and ST <- FP.
146306f32e7eSjoerg     const MachineOperand &MO1 = MI.getOperand(1);
146406f32e7eSjoerg     const MachineOperand &MO0 = MI.getOperand(0);
146506f32e7eSjoerg     bool KillsSrc = MI.killsRegister(MO1.getReg());
146606f32e7eSjoerg 
146706f32e7eSjoerg     // FP <- FP copy.
146806f32e7eSjoerg     unsigned DstFP = getFPReg(MO0);
146906f32e7eSjoerg     unsigned SrcFP = getFPReg(MO1);
147006f32e7eSjoerg     assert(isLive(SrcFP) && "Cannot copy dead register");
147106f32e7eSjoerg     if (KillsSrc) {
147206f32e7eSjoerg       // If the input operand is killed, we can just change the owner of the
147306f32e7eSjoerg       // incoming stack slot into the result.
147406f32e7eSjoerg       unsigned Slot = getSlot(SrcFP);
147506f32e7eSjoerg       Stack[Slot] = DstFP;
147606f32e7eSjoerg       RegMap[DstFP] = Slot;
147706f32e7eSjoerg     } else {
147806f32e7eSjoerg       // For COPY we just duplicate the specified value to a new stack slot.
147906f32e7eSjoerg       // This could be made better, but would require substantial changes.
148006f32e7eSjoerg       duplicateToTop(SrcFP, DstFP, Inst);
148106f32e7eSjoerg     }
148206f32e7eSjoerg     break;
148306f32e7eSjoerg   }
148406f32e7eSjoerg 
148506f32e7eSjoerg   case TargetOpcode::IMPLICIT_DEF: {
148606f32e7eSjoerg     // All FP registers must be explicitly defined, so load a 0 instead.
148706f32e7eSjoerg     unsigned Reg = MI.getOperand(0).getReg() - X86::FP0;
148806f32e7eSjoerg     LLVM_DEBUG(dbgs() << "Emitting LD_F0 for implicit FP" << Reg << '\n');
148906f32e7eSjoerg     BuildMI(*MBB, Inst, MI.getDebugLoc(), TII->get(X86::LD_F0));
149006f32e7eSjoerg     pushReg(Reg);
149106f32e7eSjoerg     break;
149206f32e7eSjoerg   }
149306f32e7eSjoerg 
149406f32e7eSjoerg   case TargetOpcode::INLINEASM:
149506f32e7eSjoerg   case TargetOpcode::INLINEASM_BR: {
149606f32e7eSjoerg     // The inline asm MachineInstr currently only *uses* FP registers for the
149706f32e7eSjoerg     // 'f' constraint.  These should be turned into the current ST(x) register
149806f32e7eSjoerg     // in the machine instr.
149906f32e7eSjoerg     //
150006f32e7eSjoerg     // There are special rules for x87 inline assembly. The compiler must know
150106f32e7eSjoerg     // exactly how many registers are popped and pushed implicitly by the asm.
150206f32e7eSjoerg     // Otherwise it is not possible to restore the stack state after the inline
150306f32e7eSjoerg     // asm.
150406f32e7eSjoerg     //
150506f32e7eSjoerg     // There are 3 kinds of input operands:
150606f32e7eSjoerg     //
150706f32e7eSjoerg     // 1. Popped inputs. These must appear at the stack top in ST0-STn. A
150806f32e7eSjoerg     //    popped input operand must be in a fixed stack slot, and it is either
150906f32e7eSjoerg     //    tied to an output operand, or in the clobber list. The MI has ST use
151006f32e7eSjoerg     //    and def operands for these inputs.
151106f32e7eSjoerg     //
151206f32e7eSjoerg     // 2. Fixed inputs. These inputs appear in fixed stack slots, but are
151306f32e7eSjoerg     //    preserved by the inline asm. The fixed stack slots must be STn-STm
151406f32e7eSjoerg     //    following the popped inputs. A fixed input operand cannot be tied to
151506f32e7eSjoerg     //    an output or appear in the clobber list. The MI has ST use operands
151606f32e7eSjoerg     //    and no defs for these inputs.
151706f32e7eSjoerg     //
151806f32e7eSjoerg     // 3. Preserved inputs. These inputs use the "f" constraint which is
151906f32e7eSjoerg     //    represented as an FP register. The inline asm won't change these
152006f32e7eSjoerg     //    stack slots.
152106f32e7eSjoerg     //
152206f32e7eSjoerg     // Outputs must be in ST registers, FP outputs are not allowed. Clobbered
152306f32e7eSjoerg     // registers do not count as output operands. The inline asm changes the
152406f32e7eSjoerg     // stack as if it popped all the popped inputs and then pushed all the
152506f32e7eSjoerg     // output operands.
152606f32e7eSjoerg 
152706f32e7eSjoerg     // Scan the assembly for ST registers used, defined and clobbered. We can
152806f32e7eSjoerg     // only tell clobbers from defs by looking at the asm descriptor.
152906f32e7eSjoerg     unsigned STUses = 0, STDefs = 0, STClobbers = 0, STDeadDefs = 0;
153006f32e7eSjoerg     unsigned NumOps = 0;
153106f32e7eSjoerg     SmallSet<unsigned, 1> FRegIdx;
153206f32e7eSjoerg     unsigned RCID;
153306f32e7eSjoerg 
153406f32e7eSjoerg     for (unsigned i = InlineAsm::MIOp_FirstOperand, e = MI.getNumOperands();
153506f32e7eSjoerg          i != e && MI.getOperand(i).isImm(); i += 1 + NumOps) {
153606f32e7eSjoerg       unsigned Flags = MI.getOperand(i).getImm();
153706f32e7eSjoerg 
153806f32e7eSjoerg       NumOps = InlineAsm::getNumOperandRegisters(Flags);
153906f32e7eSjoerg       if (NumOps != 1)
154006f32e7eSjoerg         continue;
154106f32e7eSjoerg       const MachineOperand &MO = MI.getOperand(i + 1);
154206f32e7eSjoerg       if (!MO.isReg())
154306f32e7eSjoerg         continue;
154406f32e7eSjoerg       unsigned STReg = MO.getReg() - X86::FP0;
154506f32e7eSjoerg       if (STReg >= 8)
154606f32e7eSjoerg         continue;
154706f32e7eSjoerg 
154806f32e7eSjoerg       // If the flag has a register class constraint, this must be an operand
154906f32e7eSjoerg       // with constraint "f". Record its index and continue.
155006f32e7eSjoerg       if (InlineAsm::hasRegClassConstraint(Flags, RCID)) {
155106f32e7eSjoerg         FRegIdx.insert(i + 1);
155206f32e7eSjoerg         continue;
155306f32e7eSjoerg       }
155406f32e7eSjoerg 
155506f32e7eSjoerg       switch (InlineAsm::getKind(Flags)) {
155606f32e7eSjoerg       case InlineAsm::Kind_RegUse:
155706f32e7eSjoerg         STUses |= (1u << STReg);
155806f32e7eSjoerg         break;
155906f32e7eSjoerg       case InlineAsm::Kind_RegDef:
156006f32e7eSjoerg       case InlineAsm::Kind_RegDefEarlyClobber:
156106f32e7eSjoerg         STDefs |= (1u << STReg);
156206f32e7eSjoerg         if (MO.isDead())
156306f32e7eSjoerg           STDeadDefs |= (1u << STReg);
156406f32e7eSjoerg         break;
156506f32e7eSjoerg       case InlineAsm::Kind_Clobber:
156606f32e7eSjoerg         STClobbers |= (1u << STReg);
156706f32e7eSjoerg         break;
156806f32e7eSjoerg       default:
156906f32e7eSjoerg         break;
157006f32e7eSjoerg       }
157106f32e7eSjoerg     }
157206f32e7eSjoerg 
157306f32e7eSjoerg     if (STUses && !isMask_32(STUses))
157406f32e7eSjoerg       MI.emitError("fixed input regs must be last on the x87 stack");
157506f32e7eSjoerg     unsigned NumSTUses = countTrailingOnes(STUses);
157606f32e7eSjoerg 
157706f32e7eSjoerg     // Defs must be contiguous from the stack top. ST0-STn.
157806f32e7eSjoerg     if (STDefs && !isMask_32(STDefs)) {
157906f32e7eSjoerg       MI.emitError("output regs must be last on the x87 stack");
158006f32e7eSjoerg       STDefs = NextPowerOf2(STDefs) - 1;
158106f32e7eSjoerg     }
158206f32e7eSjoerg     unsigned NumSTDefs = countTrailingOnes(STDefs);
158306f32e7eSjoerg 
158406f32e7eSjoerg     // So must the clobbered stack slots. ST0-STm, m >= n.
158506f32e7eSjoerg     if (STClobbers && !isMask_32(STDefs | STClobbers))
158606f32e7eSjoerg       MI.emitError("clobbers must be last on the x87 stack");
158706f32e7eSjoerg 
158806f32e7eSjoerg     // Popped inputs are the ones that are also clobbered or defined.
158906f32e7eSjoerg     unsigned STPopped = STUses & (STDefs | STClobbers);
159006f32e7eSjoerg     if (STPopped && !isMask_32(STPopped))
159106f32e7eSjoerg       MI.emitError("implicitly popped regs must be last on the x87 stack");
159206f32e7eSjoerg     unsigned NumSTPopped = countTrailingOnes(STPopped);
159306f32e7eSjoerg 
159406f32e7eSjoerg     LLVM_DEBUG(dbgs() << "Asm uses " << NumSTUses << " fixed regs, pops "
159506f32e7eSjoerg                       << NumSTPopped << ", and defines " << NumSTDefs
159606f32e7eSjoerg                       << " regs.\n");
159706f32e7eSjoerg 
159806f32e7eSjoerg #ifndef NDEBUG
159906f32e7eSjoerg     // If any input operand uses constraint "f", all output register
160006f32e7eSjoerg     // constraints must be early-clobber defs.
160106f32e7eSjoerg     for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I)
160206f32e7eSjoerg       if (FRegIdx.count(I)) {
160306f32e7eSjoerg         assert((1 << getFPReg(MI.getOperand(I)) & STDefs) == 0 &&
160406f32e7eSjoerg                "Operands with constraint \"f\" cannot overlap with defs");
160506f32e7eSjoerg       }
160606f32e7eSjoerg #endif
160706f32e7eSjoerg 
160806f32e7eSjoerg     // Collect all FP registers (register operands with constraints "t", "u",
160906f32e7eSjoerg     // and "f") to kill afer the instruction.
161006f32e7eSjoerg     unsigned FPKills = ((1u << NumFPRegs) - 1) & ~0xff;
161106f32e7eSjoerg     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
161206f32e7eSjoerg       MachineOperand &Op = MI.getOperand(i);
161306f32e7eSjoerg       if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6)
161406f32e7eSjoerg         continue;
161506f32e7eSjoerg       unsigned FPReg = getFPReg(Op);
161606f32e7eSjoerg 
161706f32e7eSjoerg       // If we kill this operand, make sure to pop it from the stack after the
161806f32e7eSjoerg       // asm.  We just remember it for now, and pop them all off at the end in
161906f32e7eSjoerg       // a batch.
162006f32e7eSjoerg       if (Op.isUse() && Op.isKill())
162106f32e7eSjoerg         FPKills |= 1U << FPReg;
162206f32e7eSjoerg     }
162306f32e7eSjoerg 
162406f32e7eSjoerg     // Do not include registers that are implicitly popped by defs/clobbers.
162506f32e7eSjoerg     FPKills &= ~(STDefs | STClobbers);
162606f32e7eSjoerg 
162706f32e7eSjoerg     // Now we can rearrange the live registers to match what was requested.
162806f32e7eSjoerg     unsigned char STUsesArray[8];
162906f32e7eSjoerg 
163006f32e7eSjoerg     for (unsigned I = 0; I < NumSTUses; ++I)
163106f32e7eSjoerg       STUsesArray[I] = I;
163206f32e7eSjoerg 
163306f32e7eSjoerg     shuffleStackTop(STUsesArray, NumSTUses, Inst);
163406f32e7eSjoerg     LLVM_DEBUG({
163506f32e7eSjoerg       dbgs() << "Before asm: ";
163606f32e7eSjoerg       dumpStack();
163706f32e7eSjoerg     });
163806f32e7eSjoerg 
163906f32e7eSjoerg     // With the stack layout fixed, rewrite the FP registers.
164006f32e7eSjoerg     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
164106f32e7eSjoerg       MachineOperand &Op = MI.getOperand(i);
164206f32e7eSjoerg       if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6)
164306f32e7eSjoerg         continue;
164406f32e7eSjoerg 
164506f32e7eSjoerg       unsigned FPReg = getFPReg(Op);
164606f32e7eSjoerg 
164706f32e7eSjoerg       if (FRegIdx.count(i))
164806f32e7eSjoerg         // Operand with constraint "f".
164906f32e7eSjoerg         Op.setReg(getSTReg(FPReg));
165006f32e7eSjoerg       else
165106f32e7eSjoerg         // Operand with a single register class constraint ("t" or "u").
165206f32e7eSjoerg         Op.setReg(X86::ST0 + FPReg);
165306f32e7eSjoerg     }
165406f32e7eSjoerg 
165506f32e7eSjoerg     // Simulate the inline asm popping its inputs and pushing its outputs.
165606f32e7eSjoerg     StackTop -= NumSTPopped;
165706f32e7eSjoerg 
165806f32e7eSjoerg     for (unsigned i = 0; i < NumSTDefs; ++i)
165906f32e7eSjoerg       pushReg(NumSTDefs - i - 1);
166006f32e7eSjoerg 
166106f32e7eSjoerg     // If this asm kills any FP registers (is the last use of them) we must
166206f32e7eSjoerg     // explicitly emit pop instructions for them.  Do this now after the asm has
166306f32e7eSjoerg     // executed so that the ST(x) numbers are not off (which would happen if we
166406f32e7eSjoerg     // did this inline with operand rewriting).
166506f32e7eSjoerg     //
166606f32e7eSjoerg     // Note: this might be a non-optimal pop sequence.  We might be able to do
166706f32e7eSjoerg     // better by trying to pop in stack order or something.
166806f32e7eSjoerg     while (FPKills) {
166906f32e7eSjoerg       unsigned FPReg = countTrailingZeros(FPKills);
167006f32e7eSjoerg       if (isLive(FPReg))
167106f32e7eSjoerg         freeStackSlotAfter(Inst, FPReg);
167206f32e7eSjoerg       FPKills &= ~(1U << FPReg);
167306f32e7eSjoerg     }
167406f32e7eSjoerg 
167506f32e7eSjoerg     // Don't delete the inline asm!
167606f32e7eSjoerg     return;
167706f32e7eSjoerg   }
167806f32e7eSjoerg   }
167906f32e7eSjoerg 
168006f32e7eSjoerg   Inst = MBB->erase(Inst);  // Remove the pseudo instruction
168106f32e7eSjoerg 
168206f32e7eSjoerg   // We want to leave I pointing to the previous instruction, but what if we
168306f32e7eSjoerg   // just erased the first instruction?
168406f32e7eSjoerg   if (Inst == MBB->begin()) {
168506f32e7eSjoerg     LLVM_DEBUG(dbgs() << "Inserting dummy KILL\n");
168606f32e7eSjoerg     Inst = BuildMI(*MBB, Inst, DebugLoc(), TII->get(TargetOpcode::KILL));
168706f32e7eSjoerg   } else
168806f32e7eSjoerg     --Inst;
168906f32e7eSjoerg }
169006f32e7eSjoerg 
setKillFlags(MachineBasicBlock & MBB) const169106f32e7eSjoerg void FPS::setKillFlags(MachineBasicBlock &MBB) const {
169206f32e7eSjoerg   const TargetRegisterInfo &TRI =
169306f32e7eSjoerg       *MBB.getParent()->getSubtarget().getRegisterInfo();
169406f32e7eSjoerg   LivePhysRegs LPR(TRI);
169506f32e7eSjoerg 
169606f32e7eSjoerg   LPR.addLiveOuts(MBB);
169706f32e7eSjoerg 
169806f32e7eSjoerg   for (MachineBasicBlock::reverse_iterator I = MBB.rbegin(), E = MBB.rend();
169906f32e7eSjoerg        I != E; ++I) {
170006f32e7eSjoerg     if (I->isDebugInstr())
170106f32e7eSjoerg       continue;
170206f32e7eSjoerg 
170306f32e7eSjoerg     std::bitset<8> Defs;
170406f32e7eSjoerg     SmallVector<MachineOperand *, 2> Uses;
170506f32e7eSjoerg     MachineInstr &MI = *I;
170606f32e7eSjoerg 
170706f32e7eSjoerg     for (auto &MO : I->operands()) {
170806f32e7eSjoerg       if (!MO.isReg())
170906f32e7eSjoerg         continue;
171006f32e7eSjoerg 
171106f32e7eSjoerg       unsigned Reg = MO.getReg() - X86::FP0;
171206f32e7eSjoerg 
171306f32e7eSjoerg       if (Reg >= 8)
171406f32e7eSjoerg         continue;
171506f32e7eSjoerg 
171606f32e7eSjoerg       if (MO.isDef()) {
171706f32e7eSjoerg         Defs.set(Reg);
171806f32e7eSjoerg         if (!LPR.contains(MO.getReg()))
171906f32e7eSjoerg           MO.setIsDead();
172006f32e7eSjoerg       } else
172106f32e7eSjoerg         Uses.push_back(&MO);
172206f32e7eSjoerg     }
172306f32e7eSjoerg 
172406f32e7eSjoerg     for (auto *MO : Uses)
172506f32e7eSjoerg       if (Defs.test(getFPReg(*MO)) || !LPR.contains(MO->getReg()))
172606f32e7eSjoerg         MO->setIsKill();
172706f32e7eSjoerg 
172806f32e7eSjoerg     LPR.stepBackward(MI);
172906f32e7eSjoerg   }
173006f32e7eSjoerg }
1731