1f4a2713aSLionel Sambuc //===-- X86FloatingPoint.cpp - Floating point Reg -> Stack converter ------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file defines the pass which converts floating point instructions from
11f4a2713aSLionel Sambuc // pseudo registers into register stack instructions. This pass uses live
12f4a2713aSLionel Sambuc // variable information to indicate where the FPn registers are used and their
13f4a2713aSLionel Sambuc // lifetimes.
14f4a2713aSLionel Sambuc //
15f4a2713aSLionel Sambuc // The x87 hardware tracks liveness of the stack registers, so it is necessary
16f4a2713aSLionel Sambuc // to implement exact liveness tracking between basic blocks. The CFG edges are
17f4a2713aSLionel Sambuc // partitioned into bundles where the same FP registers must be live in
18f4a2713aSLionel Sambuc // identical stack positions. Instructions are inserted at the end of each basic
19f4a2713aSLionel Sambuc // block to rearrange the live registers to match the outgoing bundle.
20f4a2713aSLionel Sambuc //
21f4a2713aSLionel Sambuc // This approach avoids splitting critical edges at the potential cost of more
22f4a2713aSLionel Sambuc // live register shuffling instructions when critical edges are present.
23f4a2713aSLionel Sambuc //
24f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
25f4a2713aSLionel Sambuc
26f4a2713aSLionel Sambuc #include "X86.h"
27f4a2713aSLionel Sambuc #include "X86InstrInfo.h"
28f4a2713aSLionel Sambuc #include "llvm/ADT/DepthFirstIterator.h"
29f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
30f4a2713aSLionel Sambuc #include "llvm/ADT/SmallPtrSet.h"
31*0a6a1f1dSLionel Sambuc #include "llvm/ADT/SmallSet.h"
32f4a2713aSLionel Sambuc #include "llvm/ADT/SmallVector.h"
33f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h"
34f4a2713aSLionel Sambuc #include "llvm/CodeGen/EdgeBundles.h"
35f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFunctionPass.h"
36f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineInstrBuilder.h"
37f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineRegisterInfo.h"
38*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/LivePhysRegs.h"
39f4a2713aSLionel Sambuc #include "llvm/CodeGen/Passes.h"
40f4a2713aSLionel Sambuc #include "llvm/IR/InlineAsm.h"
41f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
42f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
43f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
44f4a2713aSLionel Sambuc #include "llvm/Target/TargetInstrInfo.h"
45f4a2713aSLionel Sambuc #include "llvm/Target/TargetMachine.h"
46*0a6a1f1dSLionel Sambuc #include "llvm/Target/TargetSubtargetInfo.h"
47f4a2713aSLionel Sambuc #include <algorithm>
48*0a6a1f1dSLionel Sambuc #include <bitset>
49f4a2713aSLionel Sambuc using namespace llvm;
50f4a2713aSLionel Sambuc
51*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "x86-codegen"
52*0a6a1f1dSLionel Sambuc
53f4a2713aSLionel Sambuc STATISTIC(NumFXCH, "Number of fxch instructions inserted");
54f4a2713aSLionel Sambuc STATISTIC(NumFP , "Number of floating point instructions");
55f4a2713aSLionel Sambuc
56f4a2713aSLionel Sambuc namespace {
57*0a6a1f1dSLionel Sambuc const unsigned ScratchFPReg = 7;
58*0a6a1f1dSLionel Sambuc
59f4a2713aSLionel Sambuc struct FPS : public MachineFunctionPass {
60f4a2713aSLionel Sambuc static char ID;
FPS__anon58d6a2b20111::FPS61f4a2713aSLionel Sambuc FPS() : MachineFunctionPass(ID) {
62f4a2713aSLionel Sambuc initializeEdgeBundlesPass(*PassRegistry::getPassRegistry());
63f4a2713aSLionel Sambuc // This is really only to keep valgrind quiet.
64f4a2713aSLionel Sambuc // The logic in isLive() is too much for it.
65f4a2713aSLionel Sambuc memset(Stack, 0, sizeof(Stack));
66f4a2713aSLionel Sambuc memset(RegMap, 0, sizeof(RegMap));
67f4a2713aSLionel Sambuc }
68f4a2713aSLionel Sambuc
getAnalysisUsage__anon58d6a2b20111::FPS69*0a6a1f1dSLionel Sambuc void getAnalysisUsage(AnalysisUsage &AU) const override {
70f4a2713aSLionel Sambuc AU.setPreservesCFG();
71f4a2713aSLionel Sambuc AU.addRequired<EdgeBundles>();
72f4a2713aSLionel Sambuc AU.addPreservedID(MachineLoopInfoID);
73f4a2713aSLionel Sambuc AU.addPreservedID(MachineDominatorsID);
74f4a2713aSLionel Sambuc MachineFunctionPass::getAnalysisUsage(AU);
75f4a2713aSLionel Sambuc }
76f4a2713aSLionel Sambuc
77*0a6a1f1dSLionel Sambuc bool runOnMachineFunction(MachineFunction &MF) override;
78f4a2713aSLionel Sambuc
getPassName__anon58d6a2b20111::FPS79*0a6a1f1dSLionel Sambuc const char *getPassName() const override { return "X86 FP Stackifier"; }
80f4a2713aSLionel Sambuc
81f4a2713aSLionel Sambuc private:
82f4a2713aSLionel Sambuc const TargetInstrInfo *TII; // Machine instruction info.
83f4a2713aSLionel Sambuc
84f4a2713aSLionel Sambuc // Two CFG edges are related if they leave the same block, or enter the same
85f4a2713aSLionel Sambuc // block. The transitive closure of an edge under this relation is a
86f4a2713aSLionel Sambuc // LiveBundle. It represents a set of CFG edges where the live FP stack
87f4a2713aSLionel Sambuc // registers must be allocated identically in the x87 stack.
88f4a2713aSLionel Sambuc //
89f4a2713aSLionel Sambuc // A LiveBundle is usually all the edges leaving a block, or all the edges
90f4a2713aSLionel Sambuc // entering a block, but it can contain more edges if critical edges are
91f4a2713aSLionel Sambuc // present.
92f4a2713aSLionel Sambuc //
93f4a2713aSLionel Sambuc // The set of live FP registers in a LiveBundle is calculated by bundleCFG,
94f4a2713aSLionel Sambuc // but the exact mapping of FP registers to stack slots is fixed later.
95f4a2713aSLionel Sambuc struct LiveBundle {
96f4a2713aSLionel Sambuc // Bit mask of live FP registers. Bit 0 = FP0, bit 1 = FP1, &c.
97f4a2713aSLionel Sambuc unsigned Mask;
98f4a2713aSLionel Sambuc
99f4a2713aSLionel Sambuc // Number of pre-assigned live registers in FixStack. This is 0 when the
100f4a2713aSLionel Sambuc // stack order has not yet been fixed.
101f4a2713aSLionel Sambuc unsigned FixCount;
102f4a2713aSLionel Sambuc
103f4a2713aSLionel Sambuc // Assigned stack order for live-in registers.
104f4a2713aSLionel Sambuc // FixStack[i] == getStackEntry(i) for all i < FixCount.
105f4a2713aSLionel Sambuc unsigned char FixStack[8];
106f4a2713aSLionel Sambuc
LiveBundle__anon58d6a2b20111::FPS::LiveBundle107f4a2713aSLionel Sambuc LiveBundle() : Mask(0), FixCount(0) {}
108f4a2713aSLionel Sambuc
109f4a2713aSLionel Sambuc // Have the live registers been assigned a stack order yet?
isFixed__anon58d6a2b20111::FPS::LiveBundle110f4a2713aSLionel Sambuc bool isFixed() const { return !Mask || FixCount; }
111f4a2713aSLionel Sambuc };
112f4a2713aSLionel Sambuc
113f4a2713aSLionel Sambuc // Numbered LiveBundle structs. LiveBundles[0] is used for all CFG edges
114f4a2713aSLionel Sambuc // with no live FP registers.
115f4a2713aSLionel Sambuc SmallVector<LiveBundle, 8> LiveBundles;
116f4a2713aSLionel Sambuc
117f4a2713aSLionel Sambuc // The edge bundle analysis provides indices into the LiveBundles vector.
118f4a2713aSLionel Sambuc EdgeBundles *Bundles;
119f4a2713aSLionel Sambuc
120f4a2713aSLionel Sambuc // Return a bitmask of FP registers in block's live-in list.
calcLiveInMask__anon58d6a2b20111::FPS121f4a2713aSLionel Sambuc static unsigned calcLiveInMask(MachineBasicBlock *MBB) {
122f4a2713aSLionel Sambuc unsigned Mask = 0;
123f4a2713aSLionel Sambuc for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
124f4a2713aSLionel Sambuc E = MBB->livein_end(); I != E; ++I) {
125f4a2713aSLionel Sambuc unsigned Reg = *I;
126f4a2713aSLionel Sambuc if (Reg < X86::FP0 || Reg > X86::FP6)
127f4a2713aSLionel Sambuc continue;
128f4a2713aSLionel Sambuc Mask |= 1 << (Reg - X86::FP0);
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc return Mask;
131f4a2713aSLionel Sambuc }
132f4a2713aSLionel Sambuc
133f4a2713aSLionel Sambuc // Partition all the CFG edges into LiveBundles.
134f4a2713aSLionel Sambuc void bundleCFG(MachineFunction &MF);
135f4a2713aSLionel Sambuc
136f4a2713aSLionel Sambuc MachineBasicBlock *MBB; // Current basic block
137f4a2713aSLionel Sambuc
138f4a2713aSLionel Sambuc // The hardware keeps track of how many FP registers are live, so we have
139f4a2713aSLionel Sambuc // to model that exactly. Usually, each live register corresponds to an
140f4a2713aSLionel Sambuc // FP<n> register, but when dealing with calls, returns, and inline
141f4a2713aSLionel Sambuc // assembly, it is sometimes necessary to have live scratch registers.
142f4a2713aSLionel Sambuc unsigned Stack[8]; // FP<n> Registers in each stack slot...
143f4a2713aSLionel Sambuc unsigned StackTop; // The current top of the FP stack.
144f4a2713aSLionel Sambuc
145f4a2713aSLionel Sambuc enum {
146*0a6a1f1dSLionel Sambuc NumFPRegs = 8 // Including scratch pseudo-registers.
147f4a2713aSLionel Sambuc };
148f4a2713aSLionel Sambuc
149f4a2713aSLionel Sambuc // For each live FP<n> register, point to its Stack[] entry.
150f4a2713aSLionel Sambuc // The first entries correspond to FP0-FP6, the rest are scratch registers
151f4a2713aSLionel Sambuc // used when we need slightly different live registers than what the
152f4a2713aSLionel Sambuc // register allocator thinks.
153f4a2713aSLionel Sambuc unsigned RegMap[NumFPRegs];
154f4a2713aSLionel Sambuc
155f4a2713aSLionel Sambuc // Set up our stack model to match the incoming registers to MBB.
156f4a2713aSLionel Sambuc void setupBlockStack();
157f4a2713aSLionel Sambuc
158f4a2713aSLionel Sambuc // Shuffle live registers to match the expectations of successor blocks.
159f4a2713aSLionel Sambuc void finishBlockStack();
160f4a2713aSLionel Sambuc
161f4a2713aSLionel Sambuc #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dumpStack__anon58d6a2b20111::FPS162f4a2713aSLionel Sambuc void dumpStack() const {
163f4a2713aSLionel Sambuc dbgs() << "Stack contents:";
164f4a2713aSLionel Sambuc for (unsigned i = 0; i != StackTop; ++i) {
165f4a2713aSLionel Sambuc dbgs() << " FP" << Stack[i];
166f4a2713aSLionel Sambuc assert(RegMap[Stack[i]] == i && "Stack[] doesn't match RegMap[]!");
167f4a2713aSLionel Sambuc }
168f4a2713aSLionel Sambuc }
169f4a2713aSLionel Sambuc #endif
170f4a2713aSLionel Sambuc
171f4a2713aSLionel Sambuc /// getSlot - Return the stack slot number a particular register number is
172f4a2713aSLionel Sambuc /// in.
getSlot__anon58d6a2b20111::FPS173f4a2713aSLionel Sambuc unsigned getSlot(unsigned RegNo) const {
174f4a2713aSLionel Sambuc assert(RegNo < NumFPRegs && "Regno out of range!");
175f4a2713aSLionel Sambuc return RegMap[RegNo];
176f4a2713aSLionel Sambuc }
177f4a2713aSLionel Sambuc
178f4a2713aSLionel Sambuc /// isLive - Is RegNo currently live in the stack?
isLive__anon58d6a2b20111::FPS179f4a2713aSLionel Sambuc bool isLive(unsigned RegNo) const {
180f4a2713aSLionel Sambuc unsigned Slot = getSlot(RegNo);
181f4a2713aSLionel Sambuc return Slot < StackTop && Stack[Slot] == RegNo;
182f4a2713aSLionel Sambuc }
183f4a2713aSLionel Sambuc
184f4a2713aSLionel Sambuc /// getStackEntry - Return the X86::FP<n> register in register ST(i).
getStackEntry__anon58d6a2b20111::FPS185f4a2713aSLionel Sambuc unsigned getStackEntry(unsigned STi) const {
186f4a2713aSLionel Sambuc if (STi >= StackTop)
187f4a2713aSLionel Sambuc report_fatal_error("Access past stack top!");
188f4a2713aSLionel Sambuc return Stack[StackTop-1-STi];
189f4a2713aSLionel Sambuc }
190f4a2713aSLionel Sambuc
191f4a2713aSLionel Sambuc /// getSTReg - Return the X86::ST(i) register which contains the specified
192f4a2713aSLionel Sambuc /// FP<RegNo> register.
getSTReg__anon58d6a2b20111::FPS193f4a2713aSLionel Sambuc unsigned getSTReg(unsigned RegNo) const {
194f4a2713aSLionel Sambuc return StackTop - 1 - getSlot(RegNo) + X86::ST0;
195f4a2713aSLionel Sambuc }
196f4a2713aSLionel Sambuc
197f4a2713aSLionel Sambuc // pushReg - Push the specified FP<n> register onto the stack.
pushReg__anon58d6a2b20111::FPS198f4a2713aSLionel Sambuc void pushReg(unsigned Reg) {
199f4a2713aSLionel Sambuc assert(Reg < NumFPRegs && "Register number out of range!");
200f4a2713aSLionel Sambuc if (StackTop >= 8)
201f4a2713aSLionel Sambuc report_fatal_error("Stack overflow!");
202f4a2713aSLionel Sambuc Stack[StackTop] = Reg;
203f4a2713aSLionel Sambuc RegMap[Reg] = StackTop++;
204f4a2713aSLionel Sambuc }
205f4a2713aSLionel Sambuc
isAtTop__anon58d6a2b20111::FPS206f4a2713aSLionel Sambuc bool isAtTop(unsigned RegNo) const { return getSlot(RegNo) == StackTop-1; }
moveToTop__anon58d6a2b20111::FPS207f4a2713aSLionel Sambuc void moveToTop(unsigned RegNo, MachineBasicBlock::iterator I) {
208f4a2713aSLionel Sambuc DebugLoc dl = I == MBB->end() ? DebugLoc() : I->getDebugLoc();
209f4a2713aSLionel Sambuc if (isAtTop(RegNo)) return;
210f4a2713aSLionel Sambuc
211f4a2713aSLionel Sambuc unsigned STReg = getSTReg(RegNo);
212f4a2713aSLionel Sambuc unsigned RegOnTop = getStackEntry(0);
213f4a2713aSLionel Sambuc
214f4a2713aSLionel Sambuc // Swap the slots the regs are in.
215f4a2713aSLionel Sambuc std::swap(RegMap[RegNo], RegMap[RegOnTop]);
216f4a2713aSLionel Sambuc
217f4a2713aSLionel Sambuc // Swap stack slot contents.
218f4a2713aSLionel Sambuc if (RegMap[RegOnTop] >= StackTop)
219f4a2713aSLionel Sambuc report_fatal_error("Access past stack top!");
220f4a2713aSLionel Sambuc std::swap(Stack[RegMap[RegOnTop]], Stack[StackTop-1]);
221f4a2713aSLionel Sambuc
222f4a2713aSLionel Sambuc // Emit an fxch to update the runtime processors version of the state.
223f4a2713aSLionel Sambuc BuildMI(*MBB, I, dl, TII->get(X86::XCH_F)).addReg(STReg);
224f4a2713aSLionel Sambuc ++NumFXCH;
225f4a2713aSLionel Sambuc }
226f4a2713aSLionel Sambuc
duplicateToTop__anon58d6a2b20111::FPS227f4a2713aSLionel Sambuc void duplicateToTop(unsigned RegNo, unsigned AsReg, MachineInstr *I) {
228f4a2713aSLionel Sambuc DebugLoc dl = I == MBB->end() ? DebugLoc() : I->getDebugLoc();
229f4a2713aSLionel Sambuc unsigned STReg = getSTReg(RegNo);
230f4a2713aSLionel Sambuc pushReg(AsReg); // New register on top of stack
231f4a2713aSLionel Sambuc
232f4a2713aSLionel Sambuc BuildMI(*MBB, I, dl, TII->get(X86::LD_Frr)).addReg(STReg);
233f4a2713aSLionel Sambuc }
234f4a2713aSLionel Sambuc
235f4a2713aSLionel Sambuc /// popStackAfter - Pop the current value off of the top of the FP stack
236f4a2713aSLionel Sambuc /// after the specified instruction.
237f4a2713aSLionel Sambuc void popStackAfter(MachineBasicBlock::iterator &I);
238f4a2713aSLionel Sambuc
239f4a2713aSLionel Sambuc /// freeStackSlotAfter - Free the specified register from the register
240f4a2713aSLionel Sambuc /// stack, so that it is no longer in a register. If the register is
241f4a2713aSLionel Sambuc /// currently at the top of the stack, we just pop the current instruction,
242f4a2713aSLionel Sambuc /// otherwise we store the current top-of-stack into the specified slot,
243f4a2713aSLionel Sambuc /// then pop the top of stack.
244f4a2713aSLionel Sambuc void freeStackSlotAfter(MachineBasicBlock::iterator &I, unsigned Reg);
245f4a2713aSLionel Sambuc
246f4a2713aSLionel Sambuc /// freeStackSlotBefore - Just the pop, no folding. Return the inserted
247f4a2713aSLionel Sambuc /// instruction.
248f4a2713aSLionel Sambuc MachineBasicBlock::iterator
249f4a2713aSLionel Sambuc freeStackSlotBefore(MachineBasicBlock::iterator I, unsigned FPRegNo);
250f4a2713aSLionel Sambuc
251f4a2713aSLionel Sambuc /// Adjust the live registers to be the set in Mask.
252f4a2713aSLionel Sambuc void adjustLiveRegs(unsigned Mask, MachineBasicBlock::iterator I);
253f4a2713aSLionel Sambuc
254f4a2713aSLionel Sambuc /// Shuffle the top FixCount stack entries such that FP reg FixStack[0] is
255f4a2713aSLionel Sambuc /// st(0), FP reg FixStack[1] is st(1) etc.
256f4a2713aSLionel Sambuc void shuffleStackTop(const unsigned char *FixStack, unsigned FixCount,
257f4a2713aSLionel Sambuc MachineBasicBlock::iterator I);
258f4a2713aSLionel Sambuc
259f4a2713aSLionel Sambuc bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB);
260f4a2713aSLionel Sambuc
261*0a6a1f1dSLionel Sambuc void handleCall(MachineBasicBlock::iterator &I);
262f4a2713aSLionel Sambuc void handleZeroArgFP(MachineBasicBlock::iterator &I);
263f4a2713aSLionel Sambuc void handleOneArgFP(MachineBasicBlock::iterator &I);
264f4a2713aSLionel Sambuc void handleOneArgFPRW(MachineBasicBlock::iterator &I);
265f4a2713aSLionel Sambuc void handleTwoArgFP(MachineBasicBlock::iterator &I);
266f4a2713aSLionel Sambuc void handleCompareFP(MachineBasicBlock::iterator &I);
267f4a2713aSLionel Sambuc void handleCondMovFP(MachineBasicBlock::iterator &I);
268f4a2713aSLionel Sambuc void handleSpecialFP(MachineBasicBlock::iterator &I);
269f4a2713aSLionel Sambuc
270f4a2713aSLionel Sambuc // Check if a COPY instruction is using FP registers.
isFPCopy__anon58d6a2b20111::FPS271f4a2713aSLionel Sambuc static bool isFPCopy(MachineInstr *MI) {
272f4a2713aSLionel Sambuc unsigned DstReg = MI->getOperand(0).getReg();
273f4a2713aSLionel Sambuc unsigned SrcReg = MI->getOperand(1).getReg();
274f4a2713aSLionel Sambuc
275f4a2713aSLionel Sambuc return X86::RFP80RegClass.contains(DstReg) ||
276f4a2713aSLionel Sambuc X86::RFP80RegClass.contains(SrcReg);
277f4a2713aSLionel Sambuc }
278*0a6a1f1dSLionel Sambuc
279*0a6a1f1dSLionel Sambuc void setKillFlags(MachineBasicBlock &MBB) const;
280f4a2713aSLionel Sambuc };
281f4a2713aSLionel Sambuc char FPS::ID = 0;
282f4a2713aSLionel Sambuc }
283f4a2713aSLionel Sambuc
createX86FloatingPointStackifierPass()284f4a2713aSLionel Sambuc FunctionPass *llvm::createX86FloatingPointStackifierPass() { return new FPS(); }
285f4a2713aSLionel Sambuc
286f4a2713aSLionel Sambuc /// getFPReg - Return the X86::FPx register number for the specified operand.
287f4a2713aSLionel Sambuc /// For example, this returns 3 for X86::FP3.
getFPReg(const MachineOperand & MO)288f4a2713aSLionel Sambuc static unsigned getFPReg(const MachineOperand &MO) {
289f4a2713aSLionel Sambuc assert(MO.isReg() && "Expected an FP register!");
290f4a2713aSLionel Sambuc unsigned Reg = MO.getReg();
291f4a2713aSLionel Sambuc assert(Reg >= X86::FP0 && Reg <= X86::FP6 && "Expected FP register!");
292f4a2713aSLionel Sambuc return Reg - X86::FP0;
293f4a2713aSLionel Sambuc }
294f4a2713aSLionel Sambuc
295f4a2713aSLionel Sambuc /// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
296f4a2713aSLionel Sambuc /// register references into FP stack references.
297f4a2713aSLionel Sambuc ///
runOnMachineFunction(MachineFunction & MF)298f4a2713aSLionel Sambuc bool FPS::runOnMachineFunction(MachineFunction &MF) {
299f4a2713aSLionel Sambuc // We only need to run this pass if there are any FP registers used in this
300f4a2713aSLionel Sambuc // function. If it is all integer, there is nothing for us to do!
301f4a2713aSLionel Sambuc bool FPIsUsed = false;
302f4a2713aSLionel Sambuc
303f4a2713aSLionel Sambuc assert(X86::FP6 == X86::FP0+6 && "Register enums aren't sorted right!");
304f4a2713aSLionel Sambuc for (unsigned i = 0; i <= 6; ++i)
305f4a2713aSLionel Sambuc if (MF.getRegInfo().isPhysRegUsed(X86::FP0+i)) {
306f4a2713aSLionel Sambuc FPIsUsed = true;
307f4a2713aSLionel Sambuc break;
308f4a2713aSLionel Sambuc }
309f4a2713aSLionel Sambuc
310f4a2713aSLionel Sambuc // Early exit.
311f4a2713aSLionel Sambuc if (!FPIsUsed) return false;
312f4a2713aSLionel Sambuc
313f4a2713aSLionel Sambuc Bundles = &getAnalysis<EdgeBundles>();
314*0a6a1f1dSLionel Sambuc TII = MF.getSubtarget().getInstrInfo();
315f4a2713aSLionel Sambuc
316f4a2713aSLionel Sambuc // Prepare cross-MBB liveness.
317f4a2713aSLionel Sambuc bundleCFG(MF);
318f4a2713aSLionel Sambuc
319f4a2713aSLionel Sambuc StackTop = 0;
320f4a2713aSLionel Sambuc
321f4a2713aSLionel Sambuc // Process the function in depth first order so that we process at least one
322f4a2713aSLionel Sambuc // of the predecessors for every reachable block in the function.
323f4a2713aSLionel Sambuc SmallPtrSet<MachineBasicBlock*, 8> Processed;
324f4a2713aSLionel Sambuc MachineBasicBlock *Entry = MF.begin();
325f4a2713aSLionel Sambuc
326f4a2713aSLionel Sambuc bool Changed = false;
327*0a6a1f1dSLionel Sambuc for (MachineBasicBlock *BB : depth_first_ext(Entry, Processed))
328*0a6a1f1dSLionel Sambuc Changed |= processBasicBlock(MF, *BB);
329f4a2713aSLionel Sambuc
330f4a2713aSLionel Sambuc // Process any unreachable blocks in arbitrary order now.
331f4a2713aSLionel Sambuc if (MF.size() != Processed.size())
332f4a2713aSLionel Sambuc for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
333*0a6a1f1dSLionel Sambuc if (Processed.insert(BB).second)
334f4a2713aSLionel Sambuc Changed |= processBasicBlock(MF, *BB);
335f4a2713aSLionel Sambuc
336f4a2713aSLionel Sambuc LiveBundles.clear();
337f4a2713aSLionel Sambuc
338f4a2713aSLionel Sambuc return Changed;
339f4a2713aSLionel Sambuc }
340f4a2713aSLionel Sambuc
341f4a2713aSLionel Sambuc /// bundleCFG - Scan all the basic blocks to determine consistent live-in and
342f4a2713aSLionel Sambuc /// live-out sets for the FP registers. Consistent means that the set of
343f4a2713aSLionel Sambuc /// registers live-out from a block is identical to the live-in set of all
344f4a2713aSLionel Sambuc /// successors. This is not enforced by the normal live-in lists since
345f4a2713aSLionel Sambuc /// registers may be implicitly defined, or not used by all successors.
bundleCFG(MachineFunction & MF)346f4a2713aSLionel Sambuc void FPS::bundleCFG(MachineFunction &MF) {
347f4a2713aSLionel Sambuc assert(LiveBundles.empty() && "Stale data in LiveBundles");
348f4a2713aSLionel Sambuc LiveBundles.resize(Bundles->getNumBundles());
349f4a2713aSLionel Sambuc
350f4a2713aSLionel Sambuc // Gather the actual live-in masks for all MBBs.
351f4a2713aSLionel Sambuc for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
352f4a2713aSLionel Sambuc MachineBasicBlock *MBB = I;
353f4a2713aSLionel Sambuc const unsigned Mask = calcLiveInMask(MBB);
354f4a2713aSLionel Sambuc if (!Mask)
355f4a2713aSLionel Sambuc continue;
356f4a2713aSLionel Sambuc // Update MBB ingoing bundle mask.
357f4a2713aSLionel Sambuc LiveBundles[Bundles->getBundle(MBB->getNumber(), false)].Mask |= Mask;
358f4a2713aSLionel Sambuc }
359f4a2713aSLionel Sambuc }
360f4a2713aSLionel Sambuc
361f4a2713aSLionel Sambuc /// processBasicBlock - Loop over all of the instructions in the basic block,
362f4a2713aSLionel Sambuc /// transforming FP instructions into their stack form.
363f4a2713aSLionel Sambuc ///
processBasicBlock(MachineFunction & MF,MachineBasicBlock & BB)364f4a2713aSLionel Sambuc bool FPS::processBasicBlock(MachineFunction &MF, MachineBasicBlock &BB) {
365f4a2713aSLionel Sambuc bool Changed = false;
366f4a2713aSLionel Sambuc MBB = &BB;
367f4a2713aSLionel Sambuc
368*0a6a1f1dSLionel Sambuc setKillFlags(BB);
369f4a2713aSLionel Sambuc setupBlockStack();
370f4a2713aSLionel Sambuc
371f4a2713aSLionel Sambuc for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I) {
372f4a2713aSLionel Sambuc MachineInstr *MI = I;
373f4a2713aSLionel Sambuc uint64_t Flags = MI->getDesc().TSFlags;
374f4a2713aSLionel Sambuc
375f4a2713aSLionel Sambuc unsigned FPInstClass = Flags & X86II::FPTypeMask;
376f4a2713aSLionel Sambuc if (MI->isInlineAsm())
377f4a2713aSLionel Sambuc FPInstClass = X86II::SpecialFP;
378f4a2713aSLionel Sambuc
379f4a2713aSLionel Sambuc if (MI->isCopy() && isFPCopy(MI))
380f4a2713aSLionel Sambuc FPInstClass = X86II::SpecialFP;
381f4a2713aSLionel Sambuc
382f4a2713aSLionel Sambuc if (MI->isImplicitDef() &&
383f4a2713aSLionel Sambuc X86::RFP80RegClass.contains(MI->getOperand(0).getReg()))
384f4a2713aSLionel Sambuc FPInstClass = X86II::SpecialFP;
385f4a2713aSLionel Sambuc
386*0a6a1f1dSLionel Sambuc if (MI->isCall())
387*0a6a1f1dSLionel Sambuc FPInstClass = X86II::SpecialFP;
388*0a6a1f1dSLionel Sambuc
389f4a2713aSLionel Sambuc if (FPInstClass == X86II::NotFP)
390f4a2713aSLionel Sambuc continue; // Efficiently ignore non-fp insts!
391f4a2713aSLionel Sambuc
392*0a6a1f1dSLionel Sambuc MachineInstr *PrevMI = nullptr;
393f4a2713aSLionel Sambuc if (I != BB.begin())
394*0a6a1f1dSLionel Sambuc PrevMI = std::prev(I);
395f4a2713aSLionel Sambuc
396f4a2713aSLionel Sambuc ++NumFP; // Keep track of # of pseudo instrs
397f4a2713aSLionel Sambuc DEBUG(dbgs() << "\nFPInst:\t" << *MI);
398f4a2713aSLionel Sambuc
399f4a2713aSLionel Sambuc // Get dead variables list now because the MI pointer may be deleted as part
400f4a2713aSLionel Sambuc // of processing!
401f4a2713aSLionel Sambuc SmallVector<unsigned, 8> DeadRegs;
402f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
403f4a2713aSLionel Sambuc const MachineOperand &MO = MI->getOperand(i);
404f4a2713aSLionel Sambuc if (MO.isReg() && MO.isDead())
405f4a2713aSLionel Sambuc DeadRegs.push_back(MO.getReg());
406f4a2713aSLionel Sambuc }
407f4a2713aSLionel Sambuc
408f4a2713aSLionel Sambuc switch (FPInstClass) {
409f4a2713aSLionel Sambuc case X86II::ZeroArgFP: handleZeroArgFP(I); break;
410f4a2713aSLionel Sambuc case X86II::OneArgFP: handleOneArgFP(I); break; // fstp ST(0)
411f4a2713aSLionel Sambuc case X86II::OneArgFPRW: handleOneArgFPRW(I); break; // ST(0) = fsqrt(ST(0))
412f4a2713aSLionel Sambuc case X86II::TwoArgFP: handleTwoArgFP(I); break;
413f4a2713aSLionel Sambuc case X86II::CompareFP: handleCompareFP(I); break;
414f4a2713aSLionel Sambuc case X86II::CondMovFP: handleCondMovFP(I); break;
415f4a2713aSLionel Sambuc case X86II::SpecialFP: handleSpecialFP(I); break;
416f4a2713aSLionel Sambuc default: llvm_unreachable("Unknown FP Type!");
417f4a2713aSLionel Sambuc }
418f4a2713aSLionel Sambuc
419f4a2713aSLionel Sambuc // Check to see if any of the values defined by this instruction are dead
420f4a2713aSLionel Sambuc // after definition. If so, pop them.
421f4a2713aSLionel Sambuc for (unsigned i = 0, e = DeadRegs.size(); i != e; ++i) {
422f4a2713aSLionel Sambuc unsigned Reg = DeadRegs[i];
423*0a6a1f1dSLionel Sambuc // Check if Reg is live on the stack. An inline-asm register operand that
424*0a6a1f1dSLionel Sambuc // is in the clobber list and marked dead might not be live on the stack.
425*0a6a1f1dSLionel Sambuc if (Reg >= X86::FP0 && Reg <= X86::FP6 && isLive(Reg-X86::FP0)) {
426f4a2713aSLionel Sambuc DEBUG(dbgs() << "Register FP#" << Reg-X86::FP0 << " is dead!\n");
427f4a2713aSLionel Sambuc freeStackSlotAfter(I, Reg-X86::FP0);
428f4a2713aSLionel Sambuc }
429f4a2713aSLionel Sambuc }
430f4a2713aSLionel Sambuc
431f4a2713aSLionel Sambuc // Print out all of the instructions expanded to if -debug
432f4a2713aSLionel Sambuc DEBUG(
433f4a2713aSLionel Sambuc MachineBasicBlock::iterator PrevI(PrevMI);
434f4a2713aSLionel Sambuc if (I == PrevI) {
435f4a2713aSLionel Sambuc dbgs() << "Just deleted pseudo instruction\n";
436f4a2713aSLionel Sambuc } else {
437f4a2713aSLionel Sambuc MachineBasicBlock::iterator Start = I;
438f4a2713aSLionel Sambuc // Rewind to first instruction newly inserted.
439*0a6a1f1dSLionel Sambuc while (Start != BB.begin() && std::prev(Start) != PrevI) --Start;
440f4a2713aSLionel Sambuc dbgs() << "Inserted instructions:\n\t";
441f4a2713aSLionel Sambuc Start->print(dbgs(), &MF.getTarget());
442*0a6a1f1dSLionel Sambuc while (++Start != std::next(I)) {}
443f4a2713aSLionel Sambuc }
444f4a2713aSLionel Sambuc dumpStack();
445f4a2713aSLionel Sambuc );
446f4a2713aSLionel Sambuc (void)PrevMI;
447f4a2713aSLionel Sambuc
448f4a2713aSLionel Sambuc Changed = true;
449f4a2713aSLionel Sambuc }
450f4a2713aSLionel Sambuc
451f4a2713aSLionel Sambuc finishBlockStack();
452f4a2713aSLionel Sambuc
453f4a2713aSLionel Sambuc return Changed;
454f4a2713aSLionel Sambuc }
455f4a2713aSLionel Sambuc
456f4a2713aSLionel Sambuc /// setupBlockStack - Use the live bundles to set up our model of the stack
457f4a2713aSLionel Sambuc /// to match predecessors' live out stack.
setupBlockStack()458f4a2713aSLionel Sambuc void FPS::setupBlockStack() {
459f4a2713aSLionel Sambuc DEBUG(dbgs() << "\nSetting up live-ins for BB#" << MBB->getNumber()
460f4a2713aSLionel Sambuc << " derived from " << MBB->getName() << ".\n");
461f4a2713aSLionel Sambuc StackTop = 0;
462f4a2713aSLionel Sambuc // Get the live-in bundle for MBB.
463f4a2713aSLionel Sambuc const LiveBundle &Bundle =
464f4a2713aSLionel Sambuc LiveBundles[Bundles->getBundle(MBB->getNumber(), false)];
465f4a2713aSLionel Sambuc
466f4a2713aSLionel Sambuc if (!Bundle.Mask) {
467f4a2713aSLionel Sambuc DEBUG(dbgs() << "Block has no FP live-ins.\n");
468f4a2713aSLionel Sambuc return;
469f4a2713aSLionel Sambuc }
470f4a2713aSLionel Sambuc
471f4a2713aSLionel Sambuc // Depth-first iteration should ensure that we always have an assigned stack.
472f4a2713aSLionel Sambuc assert(Bundle.isFixed() && "Reached block before any predecessors");
473f4a2713aSLionel Sambuc
474f4a2713aSLionel Sambuc // Push the fixed live-in registers.
475f4a2713aSLionel Sambuc for (unsigned i = Bundle.FixCount; i > 0; --i) {
476f4a2713aSLionel Sambuc MBB->addLiveIn(X86::ST0+i-1);
477f4a2713aSLionel Sambuc DEBUG(dbgs() << "Live-in st(" << (i-1) << "): %FP"
478f4a2713aSLionel Sambuc << unsigned(Bundle.FixStack[i-1]) << '\n');
479f4a2713aSLionel Sambuc pushReg(Bundle.FixStack[i-1]);
480f4a2713aSLionel Sambuc }
481f4a2713aSLionel Sambuc
482f4a2713aSLionel Sambuc // Kill off unwanted live-ins. This can happen with a critical edge.
483f4a2713aSLionel Sambuc // FIXME: We could keep these live registers around as zombies. They may need
484f4a2713aSLionel Sambuc // to be revived at the end of a short block. It might save a few instrs.
485f4a2713aSLionel Sambuc adjustLiveRegs(calcLiveInMask(MBB), MBB->begin());
486f4a2713aSLionel Sambuc DEBUG(MBB->dump());
487f4a2713aSLionel Sambuc }
488f4a2713aSLionel Sambuc
489f4a2713aSLionel Sambuc /// finishBlockStack - Revive live-outs that are implicitly defined out of
490f4a2713aSLionel Sambuc /// MBB. Shuffle live registers to match the expected fixed stack of any
491f4a2713aSLionel Sambuc /// predecessors, and ensure that all predecessors are expecting the same
492f4a2713aSLionel Sambuc /// stack.
finishBlockStack()493f4a2713aSLionel Sambuc void FPS::finishBlockStack() {
494f4a2713aSLionel Sambuc // The RET handling below takes care of return blocks for us.
495f4a2713aSLionel Sambuc if (MBB->succ_empty())
496f4a2713aSLionel Sambuc return;
497f4a2713aSLionel Sambuc
498f4a2713aSLionel Sambuc DEBUG(dbgs() << "Setting up live-outs for BB#" << MBB->getNumber()
499f4a2713aSLionel Sambuc << " derived from " << MBB->getName() << ".\n");
500f4a2713aSLionel Sambuc
501f4a2713aSLionel Sambuc // Get MBB's live-out bundle.
502f4a2713aSLionel Sambuc unsigned BundleIdx = Bundles->getBundle(MBB->getNumber(), true);
503f4a2713aSLionel Sambuc LiveBundle &Bundle = LiveBundles[BundleIdx];
504f4a2713aSLionel Sambuc
505f4a2713aSLionel Sambuc // We may need to kill and define some registers to match successors.
506f4a2713aSLionel Sambuc // FIXME: This can probably be combined with the shuffle below.
507f4a2713aSLionel Sambuc MachineBasicBlock::iterator Term = MBB->getFirstTerminator();
508f4a2713aSLionel Sambuc adjustLiveRegs(Bundle.Mask, Term);
509f4a2713aSLionel Sambuc
510f4a2713aSLionel Sambuc if (!Bundle.Mask) {
511f4a2713aSLionel Sambuc DEBUG(dbgs() << "No live-outs.\n");
512f4a2713aSLionel Sambuc return;
513f4a2713aSLionel Sambuc }
514f4a2713aSLionel Sambuc
515f4a2713aSLionel Sambuc // Has the stack order been fixed yet?
516f4a2713aSLionel Sambuc DEBUG(dbgs() << "LB#" << BundleIdx << ": ");
517f4a2713aSLionel Sambuc if (Bundle.isFixed()) {
518f4a2713aSLionel Sambuc DEBUG(dbgs() << "Shuffling stack to match.\n");
519f4a2713aSLionel Sambuc shuffleStackTop(Bundle.FixStack, Bundle.FixCount, Term);
520f4a2713aSLionel Sambuc } else {
521f4a2713aSLionel Sambuc // Not fixed yet, we get to choose.
522f4a2713aSLionel Sambuc DEBUG(dbgs() << "Fixing stack order now.\n");
523f4a2713aSLionel Sambuc Bundle.FixCount = StackTop;
524f4a2713aSLionel Sambuc for (unsigned i = 0; i < StackTop; ++i)
525f4a2713aSLionel Sambuc Bundle.FixStack[i] = getStackEntry(i);
526f4a2713aSLionel Sambuc }
527f4a2713aSLionel Sambuc }
528f4a2713aSLionel Sambuc
529f4a2713aSLionel Sambuc
530f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
531f4a2713aSLionel Sambuc // Efficient Lookup Table Support
532f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
533f4a2713aSLionel Sambuc
534f4a2713aSLionel Sambuc namespace {
535f4a2713aSLionel Sambuc struct TableEntry {
536f4a2713aSLionel Sambuc uint16_t from;
537f4a2713aSLionel Sambuc uint16_t to;
operator <__anon58d6a2b20311::TableEntry538f4a2713aSLionel Sambuc bool operator<(const TableEntry &TE) const { return from < TE.from; }
operator <(const TableEntry & TE,unsigned V)539f4a2713aSLionel Sambuc friend bool operator<(const TableEntry &TE, unsigned V) {
540f4a2713aSLionel Sambuc return TE.from < V;
541f4a2713aSLionel Sambuc }
operator <(unsigned V,const TableEntry & TE)542f4a2713aSLionel Sambuc friend bool LLVM_ATTRIBUTE_UNUSED operator<(unsigned V,
543f4a2713aSLionel Sambuc const TableEntry &TE) {
544f4a2713aSLionel Sambuc return V < TE.from;
545f4a2713aSLionel Sambuc }
546f4a2713aSLionel Sambuc };
547f4a2713aSLionel Sambuc }
548f4a2713aSLionel Sambuc
549f4a2713aSLionel Sambuc #ifndef NDEBUG
TableIsSorted(const TableEntry * Table,unsigned NumEntries)550f4a2713aSLionel Sambuc static bool TableIsSorted(const TableEntry *Table, unsigned NumEntries) {
551f4a2713aSLionel Sambuc for (unsigned i = 0; i != NumEntries-1; ++i)
552f4a2713aSLionel Sambuc if (!(Table[i] < Table[i+1])) return false;
553f4a2713aSLionel Sambuc return true;
554f4a2713aSLionel Sambuc }
555f4a2713aSLionel Sambuc #endif
556f4a2713aSLionel Sambuc
Lookup(const TableEntry * Table,unsigned N,unsigned Opcode)557f4a2713aSLionel Sambuc static int Lookup(const TableEntry *Table, unsigned N, unsigned Opcode) {
558f4a2713aSLionel Sambuc const TableEntry *I = std::lower_bound(Table, Table+N, Opcode);
559f4a2713aSLionel Sambuc if (I != Table+N && I->from == Opcode)
560f4a2713aSLionel Sambuc return I->to;
561f4a2713aSLionel Sambuc return -1;
562f4a2713aSLionel Sambuc }
563f4a2713aSLionel Sambuc
564f4a2713aSLionel Sambuc #ifdef NDEBUG
565f4a2713aSLionel Sambuc #define ASSERT_SORTED(TABLE)
566f4a2713aSLionel Sambuc #else
567f4a2713aSLionel Sambuc #define ASSERT_SORTED(TABLE) \
568f4a2713aSLionel Sambuc { static bool TABLE##Checked = false; \
569f4a2713aSLionel Sambuc if (!TABLE##Checked) { \
570f4a2713aSLionel Sambuc assert(TableIsSorted(TABLE, array_lengthof(TABLE)) && \
571f4a2713aSLionel Sambuc "All lookup tables must be sorted for efficient access!"); \
572f4a2713aSLionel Sambuc TABLE##Checked = true; \
573f4a2713aSLionel Sambuc } \
574f4a2713aSLionel Sambuc }
575f4a2713aSLionel Sambuc #endif
576f4a2713aSLionel Sambuc
577f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
578f4a2713aSLionel Sambuc // Register File -> Register Stack Mapping Methods
579f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
580f4a2713aSLionel Sambuc
581f4a2713aSLionel Sambuc // OpcodeTable - Sorted map of register instructions to their stack version.
582f4a2713aSLionel Sambuc // The first element is an register file pseudo instruction, the second is the
583f4a2713aSLionel Sambuc // concrete X86 instruction which uses the register stack.
584f4a2713aSLionel Sambuc //
585f4a2713aSLionel Sambuc static const TableEntry OpcodeTable[] = {
586f4a2713aSLionel Sambuc { X86::ABS_Fp32 , X86::ABS_F },
587f4a2713aSLionel Sambuc { X86::ABS_Fp64 , X86::ABS_F },
588f4a2713aSLionel Sambuc { X86::ABS_Fp80 , X86::ABS_F },
589f4a2713aSLionel Sambuc { X86::ADD_Fp32m , X86::ADD_F32m },
590f4a2713aSLionel Sambuc { X86::ADD_Fp64m , X86::ADD_F64m },
591f4a2713aSLionel Sambuc { X86::ADD_Fp64m32 , X86::ADD_F32m },
592f4a2713aSLionel Sambuc { X86::ADD_Fp80m32 , X86::ADD_F32m },
593f4a2713aSLionel Sambuc { X86::ADD_Fp80m64 , X86::ADD_F64m },
594f4a2713aSLionel Sambuc { X86::ADD_FpI16m32 , X86::ADD_FI16m },
595f4a2713aSLionel Sambuc { X86::ADD_FpI16m64 , X86::ADD_FI16m },
596f4a2713aSLionel Sambuc { X86::ADD_FpI16m80 , X86::ADD_FI16m },
597f4a2713aSLionel Sambuc { X86::ADD_FpI32m32 , X86::ADD_FI32m },
598f4a2713aSLionel Sambuc { X86::ADD_FpI32m64 , X86::ADD_FI32m },
599f4a2713aSLionel Sambuc { X86::ADD_FpI32m80 , X86::ADD_FI32m },
600f4a2713aSLionel Sambuc { X86::CHS_Fp32 , X86::CHS_F },
601f4a2713aSLionel Sambuc { X86::CHS_Fp64 , X86::CHS_F },
602f4a2713aSLionel Sambuc { X86::CHS_Fp80 , X86::CHS_F },
603f4a2713aSLionel Sambuc { X86::CMOVBE_Fp32 , X86::CMOVBE_F },
604f4a2713aSLionel Sambuc { X86::CMOVBE_Fp64 , X86::CMOVBE_F },
605f4a2713aSLionel Sambuc { X86::CMOVBE_Fp80 , X86::CMOVBE_F },
606f4a2713aSLionel Sambuc { X86::CMOVB_Fp32 , X86::CMOVB_F },
607f4a2713aSLionel Sambuc { X86::CMOVB_Fp64 , X86::CMOVB_F },
608f4a2713aSLionel Sambuc { X86::CMOVB_Fp80 , X86::CMOVB_F },
609f4a2713aSLionel Sambuc { X86::CMOVE_Fp32 , X86::CMOVE_F },
610f4a2713aSLionel Sambuc { X86::CMOVE_Fp64 , X86::CMOVE_F },
611f4a2713aSLionel Sambuc { X86::CMOVE_Fp80 , X86::CMOVE_F },
612f4a2713aSLionel Sambuc { X86::CMOVNBE_Fp32 , X86::CMOVNBE_F },
613f4a2713aSLionel Sambuc { X86::CMOVNBE_Fp64 , X86::CMOVNBE_F },
614f4a2713aSLionel Sambuc { X86::CMOVNBE_Fp80 , X86::CMOVNBE_F },
615f4a2713aSLionel Sambuc { X86::CMOVNB_Fp32 , X86::CMOVNB_F },
616f4a2713aSLionel Sambuc { X86::CMOVNB_Fp64 , X86::CMOVNB_F },
617f4a2713aSLionel Sambuc { X86::CMOVNB_Fp80 , X86::CMOVNB_F },
618f4a2713aSLionel Sambuc { X86::CMOVNE_Fp32 , X86::CMOVNE_F },
619f4a2713aSLionel Sambuc { X86::CMOVNE_Fp64 , X86::CMOVNE_F },
620f4a2713aSLionel Sambuc { X86::CMOVNE_Fp80 , X86::CMOVNE_F },
621f4a2713aSLionel Sambuc { X86::CMOVNP_Fp32 , X86::CMOVNP_F },
622f4a2713aSLionel Sambuc { X86::CMOVNP_Fp64 , X86::CMOVNP_F },
623f4a2713aSLionel Sambuc { X86::CMOVNP_Fp80 , X86::CMOVNP_F },
624f4a2713aSLionel Sambuc { X86::CMOVP_Fp32 , X86::CMOVP_F },
625f4a2713aSLionel Sambuc { X86::CMOVP_Fp64 , X86::CMOVP_F },
626f4a2713aSLionel Sambuc { X86::CMOVP_Fp80 , X86::CMOVP_F },
627f4a2713aSLionel Sambuc { X86::COS_Fp32 , X86::COS_F },
628f4a2713aSLionel Sambuc { X86::COS_Fp64 , X86::COS_F },
629f4a2713aSLionel Sambuc { X86::COS_Fp80 , X86::COS_F },
630f4a2713aSLionel Sambuc { X86::DIVR_Fp32m , X86::DIVR_F32m },
631f4a2713aSLionel Sambuc { X86::DIVR_Fp64m , X86::DIVR_F64m },
632f4a2713aSLionel Sambuc { X86::DIVR_Fp64m32 , X86::DIVR_F32m },
633f4a2713aSLionel Sambuc { X86::DIVR_Fp80m32 , X86::DIVR_F32m },
634f4a2713aSLionel Sambuc { X86::DIVR_Fp80m64 , X86::DIVR_F64m },
635f4a2713aSLionel Sambuc { X86::DIVR_FpI16m32, X86::DIVR_FI16m},
636f4a2713aSLionel Sambuc { X86::DIVR_FpI16m64, X86::DIVR_FI16m},
637f4a2713aSLionel Sambuc { X86::DIVR_FpI16m80, X86::DIVR_FI16m},
638f4a2713aSLionel Sambuc { X86::DIVR_FpI32m32, X86::DIVR_FI32m},
639f4a2713aSLionel Sambuc { X86::DIVR_FpI32m64, X86::DIVR_FI32m},
640f4a2713aSLionel Sambuc { X86::DIVR_FpI32m80, X86::DIVR_FI32m},
641f4a2713aSLionel Sambuc { X86::DIV_Fp32m , X86::DIV_F32m },
642f4a2713aSLionel Sambuc { X86::DIV_Fp64m , X86::DIV_F64m },
643f4a2713aSLionel Sambuc { X86::DIV_Fp64m32 , X86::DIV_F32m },
644f4a2713aSLionel Sambuc { X86::DIV_Fp80m32 , X86::DIV_F32m },
645f4a2713aSLionel Sambuc { X86::DIV_Fp80m64 , X86::DIV_F64m },
646f4a2713aSLionel Sambuc { X86::DIV_FpI16m32 , X86::DIV_FI16m },
647f4a2713aSLionel Sambuc { X86::DIV_FpI16m64 , X86::DIV_FI16m },
648f4a2713aSLionel Sambuc { X86::DIV_FpI16m80 , X86::DIV_FI16m },
649f4a2713aSLionel Sambuc { X86::DIV_FpI32m32 , X86::DIV_FI32m },
650f4a2713aSLionel Sambuc { X86::DIV_FpI32m64 , X86::DIV_FI32m },
651f4a2713aSLionel Sambuc { X86::DIV_FpI32m80 , X86::DIV_FI32m },
652f4a2713aSLionel Sambuc { X86::ILD_Fp16m32 , X86::ILD_F16m },
653f4a2713aSLionel Sambuc { X86::ILD_Fp16m64 , X86::ILD_F16m },
654f4a2713aSLionel Sambuc { X86::ILD_Fp16m80 , X86::ILD_F16m },
655f4a2713aSLionel Sambuc { X86::ILD_Fp32m32 , X86::ILD_F32m },
656f4a2713aSLionel Sambuc { X86::ILD_Fp32m64 , X86::ILD_F32m },
657f4a2713aSLionel Sambuc { X86::ILD_Fp32m80 , X86::ILD_F32m },
658f4a2713aSLionel Sambuc { X86::ILD_Fp64m32 , X86::ILD_F64m },
659f4a2713aSLionel Sambuc { X86::ILD_Fp64m64 , X86::ILD_F64m },
660f4a2713aSLionel Sambuc { X86::ILD_Fp64m80 , X86::ILD_F64m },
661f4a2713aSLionel Sambuc { X86::ISTT_Fp16m32 , X86::ISTT_FP16m},
662f4a2713aSLionel Sambuc { X86::ISTT_Fp16m64 , X86::ISTT_FP16m},
663f4a2713aSLionel Sambuc { X86::ISTT_Fp16m80 , X86::ISTT_FP16m},
664f4a2713aSLionel Sambuc { X86::ISTT_Fp32m32 , X86::ISTT_FP32m},
665f4a2713aSLionel Sambuc { X86::ISTT_Fp32m64 , X86::ISTT_FP32m},
666f4a2713aSLionel Sambuc { X86::ISTT_Fp32m80 , X86::ISTT_FP32m},
667f4a2713aSLionel Sambuc { X86::ISTT_Fp64m32 , X86::ISTT_FP64m},
668f4a2713aSLionel Sambuc { X86::ISTT_Fp64m64 , X86::ISTT_FP64m},
669f4a2713aSLionel Sambuc { X86::ISTT_Fp64m80 , X86::ISTT_FP64m},
670f4a2713aSLionel Sambuc { X86::IST_Fp16m32 , X86::IST_F16m },
671f4a2713aSLionel Sambuc { X86::IST_Fp16m64 , X86::IST_F16m },
672f4a2713aSLionel Sambuc { X86::IST_Fp16m80 , X86::IST_F16m },
673f4a2713aSLionel Sambuc { X86::IST_Fp32m32 , X86::IST_F32m },
674f4a2713aSLionel Sambuc { X86::IST_Fp32m64 , X86::IST_F32m },
675f4a2713aSLionel Sambuc { X86::IST_Fp32m80 , X86::IST_F32m },
676f4a2713aSLionel Sambuc { X86::IST_Fp64m32 , X86::IST_FP64m },
677f4a2713aSLionel Sambuc { X86::IST_Fp64m64 , X86::IST_FP64m },
678f4a2713aSLionel Sambuc { X86::IST_Fp64m80 , X86::IST_FP64m },
679f4a2713aSLionel Sambuc { X86::LD_Fp032 , X86::LD_F0 },
680f4a2713aSLionel Sambuc { X86::LD_Fp064 , X86::LD_F0 },
681f4a2713aSLionel Sambuc { X86::LD_Fp080 , X86::LD_F0 },
682f4a2713aSLionel Sambuc { X86::LD_Fp132 , X86::LD_F1 },
683f4a2713aSLionel Sambuc { X86::LD_Fp164 , X86::LD_F1 },
684f4a2713aSLionel Sambuc { X86::LD_Fp180 , X86::LD_F1 },
685f4a2713aSLionel Sambuc { X86::LD_Fp32m , X86::LD_F32m },
686f4a2713aSLionel Sambuc { X86::LD_Fp32m64 , X86::LD_F32m },
687f4a2713aSLionel Sambuc { X86::LD_Fp32m80 , X86::LD_F32m },
688f4a2713aSLionel Sambuc { X86::LD_Fp64m , X86::LD_F64m },
689f4a2713aSLionel Sambuc { X86::LD_Fp64m80 , X86::LD_F64m },
690f4a2713aSLionel Sambuc { X86::LD_Fp80m , X86::LD_F80m },
691f4a2713aSLionel Sambuc { X86::MUL_Fp32m , X86::MUL_F32m },
692f4a2713aSLionel Sambuc { X86::MUL_Fp64m , X86::MUL_F64m },
693f4a2713aSLionel Sambuc { X86::MUL_Fp64m32 , X86::MUL_F32m },
694f4a2713aSLionel Sambuc { X86::MUL_Fp80m32 , X86::MUL_F32m },
695f4a2713aSLionel Sambuc { X86::MUL_Fp80m64 , X86::MUL_F64m },
696f4a2713aSLionel Sambuc { X86::MUL_FpI16m32 , X86::MUL_FI16m },
697f4a2713aSLionel Sambuc { X86::MUL_FpI16m64 , X86::MUL_FI16m },
698f4a2713aSLionel Sambuc { X86::MUL_FpI16m80 , X86::MUL_FI16m },
699f4a2713aSLionel Sambuc { X86::MUL_FpI32m32 , X86::MUL_FI32m },
700f4a2713aSLionel Sambuc { X86::MUL_FpI32m64 , X86::MUL_FI32m },
701f4a2713aSLionel Sambuc { X86::MUL_FpI32m80 , X86::MUL_FI32m },
702f4a2713aSLionel Sambuc { X86::SIN_Fp32 , X86::SIN_F },
703f4a2713aSLionel Sambuc { X86::SIN_Fp64 , X86::SIN_F },
704f4a2713aSLionel Sambuc { X86::SIN_Fp80 , X86::SIN_F },
705f4a2713aSLionel Sambuc { X86::SQRT_Fp32 , X86::SQRT_F },
706f4a2713aSLionel Sambuc { X86::SQRT_Fp64 , X86::SQRT_F },
707f4a2713aSLionel Sambuc { X86::SQRT_Fp80 , X86::SQRT_F },
708f4a2713aSLionel Sambuc { X86::ST_Fp32m , X86::ST_F32m },
709f4a2713aSLionel Sambuc { X86::ST_Fp64m , X86::ST_F64m },
710f4a2713aSLionel Sambuc { X86::ST_Fp64m32 , X86::ST_F32m },
711f4a2713aSLionel Sambuc { X86::ST_Fp80m32 , X86::ST_F32m },
712f4a2713aSLionel Sambuc { X86::ST_Fp80m64 , X86::ST_F64m },
713f4a2713aSLionel Sambuc { X86::ST_FpP80m , X86::ST_FP80m },
714f4a2713aSLionel Sambuc { X86::SUBR_Fp32m , X86::SUBR_F32m },
715f4a2713aSLionel Sambuc { X86::SUBR_Fp64m , X86::SUBR_F64m },
716f4a2713aSLionel Sambuc { X86::SUBR_Fp64m32 , X86::SUBR_F32m },
717f4a2713aSLionel Sambuc { X86::SUBR_Fp80m32 , X86::SUBR_F32m },
718f4a2713aSLionel Sambuc { X86::SUBR_Fp80m64 , X86::SUBR_F64m },
719f4a2713aSLionel Sambuc { X86::SUBR_FpI16m32, X86::SUBR_FI16m},
720f4a2713aSLionel Sambuc { X86::SUBR_FpI16m64, X86::SUBR_FI16m},
721f4a2713aSLionel Sambuc { X86::SUBR_FpI16m80, X86::SUBR_FI16m},
722f4a2713aSLionel Sambuc { X86::SUBR_FpI32m32, X86::SUBR_FI32m},
723f4a2713aSLionel Sambuc { X86::SUBR_FpI32m64, X86::SUBR_FI32m},
724f4a2713aSLionel Sambuc { X86::SUBR_FpI32m80, X86::SUBR_FI32m},
725f4a2713aSLionel Sambuc { X86::SUB_Fp32m , X86::SUB_F32m },
726f4a2713aSLionel Sambuc { X86::SUB_Fp64m , X86::SUB_F64m },
727f4a2713aSLionel Sambuc { X86::SUB_Fp64m32 , X86::SUB_F32m },
728f4a2713aSLionel Sambuc { X86::SUB_Fp80m32 , X86::SUB_F32m },
729f4a2713aSLionel Sambuc { X86::SUB_Fp80m64 , X86::SUB_F64m },
730f4a2713aSLionel Sambuc { X86::SUB_FpI16m32 , X86::SUB_FI16m },
731f4a2713aSLionel Sambuc { X86::SUB_FpI16m64 , X86::SUB_FI16m },
732f4a2713aSLionel Sambuc { X86::SUB_FpI16m80 , X86::SUB_FI16m },
733f4a2713aSLionel Sambuc { X86::SUB_FpI32m32 , X86::SUB_FI32m },
734f4a2713aSLionel Sambuc { X86::SUB_FpI32m64 , X86::SUB_FI32m },
735f4a2713aSLionel Sambuc { X86::SUB_FpI32m80 , X86::SUB_FI32m },
736f4a2713aSLionel Sambuc { X86::TST_Fp32 , X86::TST_F },
737f4a2713aSLionel Sambuc { X86::TST_Fp64 , X86::TST_F },
738f4a2713aSLionel Sambuc { X86::TST_Fp80 , X86::TST_F },
739f4a2713aSLionel Sambuc { X86::UCOM_FpIr32 , X86::UCOM_FIr },
740f4a2713aSLionel Sambuc { X86::UCOM_FpIr64 , X86::UCOM_FIr },
741f4a2713aSLionel Sambuc { X86::UCOM_FpIr80 , X86::UCOM_FIr },
742f4a2713aSLionel Sambuc { X86::UCOM_Fpr32 , X86::UCOM_Fr },
743f4a2713aSLionel Sambuc { X86::UCOM_Fpr64 , X86::UCOM_Fr },
744f4a2713aSLionel Sambuc { X86::UCOM_Fpr80 , X86::UCOM_Fr },
745f4a2713aSLionel Sambuc };
746f4a2713aSLionel Sambuc
getConcreteOpcode(unsigned Opcode)747f4a2713aSLionel Sambuc static unsigned getConcreteOpcode(unsigned Opcode) {
748f4a2713aSLionel Sambuc ASSERT_SORTED(OpcodeTable);
749f4a2713aSLionel Sambuc int Opc = Lookup(OpcodeTable, array_lengthof(OpcodeTable), Opcode);
750f4a2713aSLionel Sambuc assert(Opc != -1 && "FP Stack instruction not in OpcodeTable!");
751f4a2713aSLionel Sambuc return Opc;
752f4a2713aSLionel Sambuc }
753f4a2713aSLionel Sambuc
754f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
755f4a2713aSLionel Sambuc // Helper Methods
756f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
757f4a2713aSLionel Sambuc
758f4a2713aSLionel Sambuc // PopTable - Sorted map of instructions to their popping version. The first
759f4a2713aSLionel Sambuc // element is an instruction, the second is the version which pops.
760f4a2713aSLionel Sambuc //
761f4a2713aSLionel Sambuc static const TableEntry PopTable[] = {
762f4a2713aSLionel Sambuc { X86::ADD_FrST0 , X86::ADD_FPrST0 },
763f4a2713aSLionel Sambuc
764f4a2713aSLionel Sambuc { X86::DIVR_FrST0, X86::DIVR_FPrST0 },
765f4a2713aSLionel Sambuc { X86::DIV_FrST0 , X86::DIV_FPrST0 },
766f4a2713aSLionel Sambuc
767f4a2713aSLionel Sambuc { X86::IST_F16m , X86::IST_FP16m },
768f4a2713aSLionel Sambuc { X86::IST_F32m , X86::IST_FP32m },
769f4a2713aSLionel Sambuc
770f4a2713aSLionel Sambuc { X86::MUL_FrST0 , X86::MUL_FPrST0 },
771f4a2713aSLionel Sambuc
772f4a2713aSLionel Sambuc { X86::ST_F32m , X86::ST_FP32m },
773f4a2713aSLionel Sambuc { X86::ST_F64m , X86::ST_FP64m },
774f4a2713aSLionel Sambuc { X86::ST_Frr , X86::ST_FPrr },
775f4a2713aSLionel Sambuc
776f4a2713aSLionel Sambuc { X86::SUBR_FrST0, X86::SUBR_FPrST0 },
777f4a2713aSLionel Sambuc { X86::SUB_FrST0 , X86::SUB_FPrST0 },
778f4a2713aSLionel Sambuc
779f4a2713aSLionel Sambuc { X86::UCOM_FIr , X86::UCOM_FIPr },
780f4a2713aSLionel Sambuc
781f4a2713aSLionel Sambuc { X86::UCOM_FPr , X86::UCOM_FPPr },
782f4a2713aSLionel Sambuc { X86::UCOM_Fr , X86::UCOM_FPr },
783f4a2713aSLionel Sambuc };
784f4a2713aSLionel Sambuc
785f4a2713aSLionel Sambuc /// popStackAfter - Pop the current value off of the top of the FP stack after
786f4a2713aSLionel Sambuc /// the specified instruction. This attempts to be sneaky and combine the pop
787f4a2713aSLionel Sambuc /// into the instruction itself if possible. The iterator is left pointing to
788f4a2713aSLionel Sambuc /// the last instruction, be it a new pop instruction inserted, or the old
789f4a2713aSLionel Sambuc /// instruction if it was modified in place.
790f4a2713aSLionel Sambuc ///
popStackAfter(MachineBasicBlock::iterator & I)791f4a2713aSLionel Sambuc void FPS::popStackAfter(MachineBasicBlock::iterator &I) {
792f4a2713aSLionel Sambuc MachineInstr* MI = I;
793f4a2713aSLionel Sambuc DebugLoc dl = MI->getDebugLoc();
794f4a2713aSLionel Sambuc ASSERT_SORTED(PopTable);
795f4a2713aSLionel Sambuc if (StackTop == 0)
796f4a2713aSLionel Sambuc report_fatal_error("Cannot pop empty stack!");
797f4a2713aSLionel Sambuc RegMap[Stack[--StackTop]] = ~0; // Update state
798f4a2713aSLionel Sambuc
799f4a2713aSLionel Sambuc // Check to see if there is a popping version of this instruction...
800f4a2713aSLionel Sambuc int Opcode = Lookup(PopTable, array_lengthof(PopTable), I->getOpcode());
801f4a2713aSLionel Sambuc if (Opcode != -1) {
802f4a2713aSLionel Sambuc I->setDesc(TII->get(Opcode));
803f4a2713aSLionel Sambuc if (Opcode == X86::UCOM_FPPr)
804f4a2713aSLionel Sambuc I->RemoveOperand(0);
805f4a2713aSLionel Sambuc } else { // Insert an explicit pop
806f4a2713aSLionel Sambuc I = BuildMI(*MBB, ++I, dl, TII->get(X86::ST_FPrr)).addReg(X86::ST0);
807f4a2713aSLionel Sambuc }
808f4a2713aSLionel Sambuc }
809f4a2713aSLionel Sambuc
810f4a2713aSLionel Sambuc /// freeStackSlotAfter - Free the specified register from the register stack, so
811f4a2713aSLionel Sambuc /// that it is no longer in a register. If the register is currently at the top
812f4a2713aSLionel Sambuc /// of the stack, we just pop the current instruction, otherwise we store the
813f4a2713aSLionel Sambuc /// current top-of-stack into the specified slot, then pop the top of stack.
freeStackSlotAfter(MachineBasicBlock::iterator & I,unsigned FPRegNo)814f4a2713aSLionel Sambuc void FPS::freeStackSlotAfter(MachineBasicBlock::iterator &I, unsigned FPRegNo) {
815f4a2713aSLionel Sambuc if (getStackEntry(0) == FPRegNo) { // already at the top of stack? easy.
816f4a2713aSLionel Sambuc popStackAfter(I);
817f4a2713aSLionel Sambuc return;
818f4a2713aSLionel Sambuc }
819f4a2713aSLionel Sambuc
820f4a2713aSLionel Sambuc // Otherwise, store the top of stack into the dead slot, killing the operand
821f4a2713aSLionel Sambuc // without having to add in an explicit xchg then pop.
822f4a2713aSLionel Sambuc //
823f4a2713aSLionel Sambuc I = freeStackSlotBefore(++I, FPRegNo);
824f4a2713aSLionel Sambuc }
825f4a2713aSLionel Sambuc
826f4a2713aSLionel Sambuc /// freeStackSlotBefore - Free the specified register without trying any
827f4a2713aSLionel Sambuc /// folding.
828f4a2713aSLionel Sambuc MachineBasicBlock::iterator
freeStackSlotBefore(MachineBasicBlock::iterator I,unsigned FPRegNo)829f4a2713aSLionel Sambuc FPS::freeStackSlotBefore(MachineBasicBlock::iterator I, unsigned FPRegNo) {
830f4a2713aSLionel Sambuc unsigned STReg = getSTReg(FPRegNo);
831f4a2713aSLionel Sambuc unsigned OldSlot = getSlot(FPRegNo);
832f4a2713aSLionel Sambuc unsigned TopReg = Stack[StackTop-1];
833f4a2713aSLionel Sambuc Stack[OldSlot] = TopReg;
834f4a2713aSLionel Sambuc RegMap[TopReg] = OldSlot;
835f4a2713aSLionel Sambuc RegMap[FPRegNo] = ~0;
836f4a2713aSLionel Sambuc Stack[--StackTop] = ~0;
837*0a6a1f1dSLionel Sambuc return BuildMI(*MBB, I, DebugLoc(), TII->get(X86::ST_FPrr))
838*0a6a1f1dSLionel Sambuc .addReg(STReg)
839*0a6a1f1dSLionel Sambuc .getInstr();
840f4a2713aSLionel Sambuc }
841f4a2713aSLionel Sambuc
842f4a2713aSLionel Sambuc /// adjustLiveRegs - Kill and revive registers such that exactly the FP
843f4a2713aSLionel Sambuc /// registers with a bit in Mask are live.
adjustLiveRegs(unsigned Mask,MachineBasicBlock::iterator I)844f4a2713aSLionel Sambuc void FPS::adjustLiveRegs(unsigned Mask, MachineBasicBlock::iterator I) {
845f4a2713aSLionel Sambuc unsigned Defs = Mask;
846f4a2713aSLionel Sambuc unsigned Kills = 0;
847f4a2713aSLionel Sambuc for (unsigned i = 0; i < StackTop; ++i) {
848f4a2713aSLionel Sambuc unsigned RegNo = Stack[i];
849f4a2713aSLionel Sambuc if (!(Defs & (1 << RegNo)))
850f4a2713aSLionel Sambuc // This register is live, but we don't want it.
851f4a2713aSLionel Sambuc Kills |= (1 << RegNo);
852f4a2713aSLionel Sambuc else
853f4a2713aSLionel Sambuc // We don't need to imp-def this live register.
854f4a2713aSLionel Sambuc Defs &= ~(1 << RegNo);
855f4a2713aSLionel Sambuc }
856f4a2713aSLionel Sambuc assert((Kills & Defs) == 0 && "Register needs killing and def'ing?");
857f4a2713aSLionel Sambuc
858f4a2713aSLionel Sambuc // Produce implicit-defs for free by using killed registers.
859f4a2713aSLionel Sambuc while (Kills && Defs) {
860f4a2713aSLionel Sambuc unsigned KReg = countTrailingZeros(Kills);
861f4a2713aSLionel Sambuc unsigned DReg = countTrailingZeros(Defs);
862f4a2713aSLionel Sambuc DEBUG(dbgs() << "Renaming %FP" << KReg << " as imp %FP" << DReg << "\n");
863f4a2713aSLionel Sambuc std::swap(Stack[getSlot(KReg)], Stack[getSlot(DReg)]);
864f4a2713aSLionel Sambuc std::swap(RegMap[KReg], RegMap[DReg]);
865f4a2713aSLionel Sambuc Kills &= ~(1 << KReg);
866f4a2713aSLionel Sambuc Defs &= ~(1 << DReg);
867f4a2713aSLionel Sambuc }
868f4a2713aSLionel Sambuc
869f4a2713aSLionel Sambuc // Kill registers by popping.
870f4a2713aSLionel Sambuc if (Kills && I != MBB->begin()) {
871*0a6a1f1dSLionel Sambuc MachineBasicBlock::iterator I2 = std::prev(I);
872f4a2713aSLionel Sambuc while (StackTop) {
873f4a2713aSLionel Sambuc unsigned KReg = getStackEntry(0);
874f4a2713aSLionel Sambuc if (!(Kills & (1 << KReg)))
875f4a2713aSLionel Sambuc break;
876f4a2713aSLionel Sambuc DEBUG(dbgs() << "Popping %FP" << KReg << "\n");
877f4a2713aSLionel Sambuc popStackAfter(I2);
878f4a2713aSLionel Sambuc Kills &= ~(1 << KReg);
879f4a2713aSLionel Sambuc }
880f4a2713aSLionel Sambuc }
881f4a2713aSLionel Sambuc
882f4a2713aSLionel Sambuc // Manually kill the rest.
883f4a2713aSLionel Sambuc while (Kills) {
884f4a2713aSLionel Sambuc unsigned KReg = countTrailingZeros(Kills);
885f4a2713aSLionel Sambuc DEBUG(dbgs() << "Killing %FP" << KReg << "\n");
886f4a2713aSLionel Sambuc freeStackSlotBefore(I, KReg);
887f4a2713aSLionel Sambuc Kills &= ~(1 << KReg);
888f4a2713aSLionel Sambuc }
889f4a2713aSLionel Sambuc
890f4a2713aSLionel Sambuc // Load zeros for all the imp-defs.
891f4a2713aSLionel Sambuc while(Defs) {
892f4a2713aSLionel Sambuc unsigned DReg = countTrailingZeros(Defs);
893f4a2713aSLionel Sambuc DEBUG(dbgs() << "Defining %FP" << DReg << " as 0\n");
894f4a2713aSLionel Sambuc BuildMI(*MBB, I, DebugLoc(), TII->get(X86::LD_F0));
895f4a2713aSLionel Sambuc pushReg(DReg);
896f4a2713aSLionel Sambuc Defs &= ~(1 << DReg);
897f4a2713aSLionel Sambuc }
898f4a2713aSLionel Sambuc
899f4a2713aSLionel Sambuc // Now we should have the correct registers live.
900f4a2713aSLionel Sambuc DEBUG(dumpStack());
901f4a2713aSLionel Sambuc assert(StackTop == CountPopulation_32(Mask) && "Live count mismatch");
902f4a2713aSLionel Sambuc }
903f4a2713aSLionel Sambuc
904f4a2713aSLionel Sambuc /// shuffleStackTop - emit fxch instructions before I to shuffle the top
905f4a2713aSLionel Sambuc /// FixCount entries into the order given by FixStack.
906f4a2713aSLionel Sambuc /// FIXME: Is there a better algorithm than insertion sort?
shuffleStackTop(const unsigned char * FixStack,unsigned FixCount,MachineBasicBlock::iterator I)907f4a2713aSLionel Sambuc void FPS::shuffleStackTop(const unsigned char *FixStack,
908f4a2713aSLionel Sambuc unsigned FixCount,
909f4a2713aSLionel Sambuc MachineBasicBlock::iterator I) {
910f4a2713aSLionel Sambuc // Move items into place, starting from the desired stack bottom.
911f4a2713aSLionel Sambuc while (FixCount--) {
912f4a2713aSLionel Sambuc // Old register at position FixCount.
913f4a2713aSLionel Sambuc unsigned OldReg = getStackEntry(FixCount);
914f4a2713aSLionel Sambuc // Desired register at position FixCount.
915f4a2713aSLionel Sambuc unsigned Reg = FixStack[FixCount];
916f4a2713aSLionel Sambuc if (Reg == OldReg)
917f4a2713aSLionel Sambuc continue;
918f4a2713aSLionel Sambuc // (Reg st0) (OldReg st0) = (Reg OldReg st0)
919f4a2713aSLionel Sambuc moveToTop(Reg, I);
920f4a2713aSLionel Sambuc if (FixCount > 0)
921f4a2713aSLionel Sambuc moveToTop(OldReg, I);
922f4a2713aSLionel Sambuc }
923f4a2713aSLionel Sambuc DEBUG(dumpStack());
924f4a2713aSLionel Sambuc }
925f4a2713aSLionel Sambuc
926f4a2713aSLionel Sambuc
927f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
928f4a2713aSLionel Sambuc // Instruction transformation implementation
929f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
930f4a2713aSLionel Sambuc
handleCall(MachineBasicBlock::iterator & I)931*0a6a1f1dSLionel Sambuc void FPS::handleCall(MachineBasicBlock::iterator &I) {
932*0a6a1f1dSLionel Sambuc unsigned STReturns = 0;
933*0a6a1f1dSLionel Sambuc
934*0a6a1f1dSLionel Sambuc for (const auto &MO : I->operands()) {
935*0a6a1f1dSLionel Sambuc if (!MO.isReg())
936*0a6a1f1dSLionel Sambuc continue;
937*0a6a1f1dSLionel Sambuc
938*0a6a1f1dSLionel Sambuc unsigned R = MO.getReg() - X86::FP0;
939*0a6a1f1dSLionel Sambuc
940*0a6a1f1dSLionel Sambuc if (R < 8) {
941*0a6a1f1dSLionel Sambuc assert(MO.isDef() && MO.isImplicit());
942*0a6a1f1dSLionel Sambuc STReturns |= 1 << R;
943*0a6a1f1dSLionel Sambuc }
944*0a6a1f1dSLionel Sambuc }
945*0a6a1f1dSLionel Sambuc
946*0a6a1f1dSLionel Sambuc unsigned N = CountTrailingOnes_32(STReturns);
947*0a6a1f1dSLionel Sambuc
948*0a6a1f1dSLionel Sambuc // FP registers used for function return must be consecutive starting at
949*0a6a1f1dSLionel Sambuc // FP0.
950*0a6a1f1dSLionel Sambuc assert(STReturns == 0 || (isMask_32(STReturns) && N <= 2));
951*0a6a1f1dSLionel Sambuc
952*0a6a1f1dSLionel Sambuc for (unsigned I = 0; I < N; ++I)
953*0a6a1f1dSLionel Sambuc pushReg(N - I - 1);
954*0a6a1f1dSLionel Sambuc }
955*0a6a1f1dSLionel Sambuc
956f4a2713aSLionel Sambuc /// handleZeroArgFP - ST(0) = fld0 ST(0) = flds <mem>
957f4a2713aSLionel Sambuc ///
handleZeroArgFP(MachineBasicBlock::iterator & I)958f4a2713aSLionel Sambuc void FPS::handleZeroArgFP(MachineBasicBlock::iterator &I) {
959f4a2713aSLionel Sambuc MachineInstr *MI = I;
960f4a2713aSLionel Sambuc unsigned DestReg = getFPReg(MI->getOperand(0));
961f4a2713aSLionel Sambuc
962f4a2713aSLionel Sambuc // Change from the pseudo instruction to the concrete instruction.
963f4a2713aSLionel Sambuc MI->RemoveOperand(0); // Remove the explicit ST(0) operand
964f4a2713aSLionel Sambuc MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
965f4a2713aSLionel Sambuc
966f4a2713aSLionel Sambuc // Result gets pushed on the stack.
967f4a2713aSLionel Sambuc pushReg(DestReg);
968f4a2713aSLionel Sambuc }
969f4a2713aSLionel Sambuc
970f4a2713aSLionel Sambuc /// handleOneArgFP - fst <mem>, ST(0)
971f4a2713aSLionel Sambuc ///
handleOneArgFP(MachineBasicBlock::iterator & I)972f4a2713aSLionel Sambuc void FPS::handleOneArgFP(MachineBasicBlock::iterator &I) {
973f4a2713aSLionel Sambuc MachineInstr *MI = I;
974f4a2713aSLionel Sambuc unsigned NumOps = MI->getDesc().getNumOperands();
975f4a2713aSLionel Sambuc assert((NumOps == X86::AddrNumOperands + 1 || NumOps == 1) &&
976f4a2713aSLionel Sambuc "Can only handle fst* & ftst instructions!");
977f4a2713aSLionel Sambuc
978f4a2713aSLionel Sambuc // Is this the last use of the source register?
979f4a2713aSLionel Sambuc unsigned Reg = getFPReg(MI->getOperand(NumOps-1));
980f4a2713aSLionel Sambuc bool KillsSrc = MI->killsRegister(X86::FP0+Reg);
981f4a2713aSLionel Sambuc
982f4a2713aSLionel Sambuc // FISTP64m is strange because there isn't a non-popping versions.
983f4a2713aSLionel Sambuc // If we have one _and_ we don't want to pop the operand, duplicate the value
984f4a2713aSLionel Sambuc // on the stack instead of moving it. This ensure that popping the value is
985f4a2713aSLionel Sambuc // always ok.
986f4a2713aSLionel Sambuc // Ditto FISTTP16m, FISTTP32m, FISTTP64m, ST_FpP80m.
987f4a2713aSLionel Sambuc //
988f4a2713aSLionel Sambuc if (!KillsSrc &&
989f4a2713aSLionel Sambuc (MI->getOpcode() == X86::IST_Fp64m32 ||
990f4a2713aSLionel Sambuc MI->getOpcode() == X86::ISTT_Fp16m32 ||
991f4a2713aSLionel Sambuc MI->getOpcode() == X86::ISTT_Fp32m32 ||
992f4a2713aSLionel Sambuc MI->getOpcode() == X86::ISTT_Fp64m32 ||
993f4a2713aSLionel Sambuc MI->getOpcode() == X86::IST_Fp64m64 ||
994f4a2713aSLionel Sambuc MI->getOpcode() == X86::ISTT_Fp16m64 ||
995f4a2713aSLionel Sambuc MI->getOpcode() == X86::ISTT_Fp32m64 ||
996f4a2713aSLionel Sambuc MI->getOpcode() == X86::ISTT_Fp64m64 ||
997f4a2713aSLionel Sambuc MI->getOpcode() == X86::IST_Fp64m80 ||
998f4a2713aSLionel Sambuc MI->getOpcode() == X86::ISTT_Fp16m80 ||
999f4a2713aSLionel Sambuc MI->getOpcode() == X86::ISTT_Fp32m80 ||
1000f4a2713aSLionel Sambuc MI->getOpcode() == X86::ISTT_Fp64m80 ||
1001f4a2713aSLionel Sambuc MI->getOpcode() == X86::ST_FpP80m)) {
1002*0a6a1f1dSLionel Sambuc duplicateToTop(Reg, ScratchFPReg, I);
1003f4a2713aSLionel Sambuc } else {
1004f4a2713aSLionel Sambuc moveToTop(Reg, I); // Move to the top of the stack...
1005f4a2713aSLionel Sambuc }
1006f4a2713aSLionel Sambuc
1007f4a2713aSLionel Sambuc // Convert from the pseudo instruction to the concrete instruction.
1008f4a2713aSLionel Sambuc MI->RemoveOperand(NumOps-1); // Remove explicit ST(0) operand
1009f4a2713aSLionel Sambuc MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
1010f4a2713aSLionel Sambuc
1011f4a2713aSLionel Sambuc if (MI->getOpcode() == X86::IST_FP64m ||
1012f4a2713aSLionel Sambuc MI->getOpcode() == X86::ISTT_FP16m ||
1013f4a2713aSLionel Sambuc MI->getOpcode() == X86::ISTT_FP32m ||
1014f4a2713aSLionel Sambuc MI->getOpcode() == X86::ISTT_FP64m ||
1015f4a2713aSLionel Sambuc MI->getOpcode() == X86::ST_FP80m) {
1016f4a2713aSLionel Sambuc if (StackTop == 0)
1017f4a2713aSLionel Sambuc report_fatal_error("Stack empty??");
1018f4a2713aSLionel Sambuc --StackTop;
1019f4a2713aSLionel Sambuc } else if (KillsSrc) { // Last use of operand?
1020f4a2713aSLionel Sambuc popStackAfter(I);
1021f4a2713aSLionel Sambuc }
1022f4a2713aSLionel Sambuc }
1023f4a2713aSLionel Sambuc
1024f4a2713aSLionel Sambuc
1025f4a2713aSLionel Sambuc /// handleOneArgFPRW: Handle instructions that read from the top of stack and
1026f4a2713aSLionel Sambuc /// replace the value with a newly computed value. These instructions may have
1027f4a2713aSLionel Sambuc /// non-fp operands after their FP operands.
1028f4a2713aSLionel Sambuc ///
1029f4a2713aSLionel Sambuc /// Examples:
1030f4a2713aSLionel Sambuc /// R1 = fchs R2
1031f4a2713aSLionel Sambuc /// R1 = fadd R2, [mem]
1032f4a2713aSLionel Sambuc ///
handleOneArgFPRW(MachineBasicBlock::iterator & I)1033f4a2713aSLionel Sambuc void FPS::handleOneArgFPRW(MachineBasicBlock::iterator &I) {
1034f4a2713aSLionel Sambuc MachineInstr *MI = I;
1035f4a2713aSLionel Sambuc #ifndef NDEBUG
1036f4a2713aSLionel Sambuc unsigned NumOps = MI->getDesc().getNumOperands();
1037f4a2713aSLionel Sambuc assert(NumOps >= 2 && "FPRW instructions must have 2 ops!!");
1038f4a2713aSLionel Sambuc #endif
1039f4a2713aSLionel Sambuc
1040f4a2713aSLionel Sambuc // Is this the last use of the source register?
1041f4a2713aSLionel Sambuc unsigned Reg = getFPReg(MI->getOperand(1));
1042f4a2713aSLionel Sambuc bool KillsSrc = MI->killsRegister(X86::FP0+Reg);
1043f4a2713aSLionel Sambuc
1044f4a2713aSLionel Sambuc if (KillsSrc) {
1045f4a2713aSLionel Sambuc // If this is the last use of the source register, just make sure it's on
1046f4a2713aSLionel Sambuc // the top of the stack.
1047f4a2713aSLionel Sambuc moveToTop(Reg, I);
1048f4a2713aSLionel Sambuc if (StackTop == 0)
1049f4a2713aSLionel Sambuc report_fatal_error("Stack cannot be empty!");
1050f4a2713aSLionel Sambuc --StackTop;
1051f4a2713aSLionel Sambuc pushReg(getFPReg(MI->getOperand(0)));
1052f4a2713aSLionel Sambuc } else {
1053f4a2713aSLionel Sambuc // If this is not the last use of the source register, _copy_ it to the top
1054f4a2713aSLionel Sambuc // of the stack.
1055f4a2713aSLionel Sambuc duplicateToTop(Reg, getFPReg(MI->getOperand(0)), I);
1056f4a2713aSLionel Sambuc }
1057f4a2713aSLionel Sambuc
1058f4a2713aSLionel Sambuc // Change from the pseudo instruction to the concrete instruction.
1059f4a2713aSLionel Sambuc MI->RemoveOperand(1); // Drop the source operand.
1060f4a2713aSLionel Sambuc MI->RemoveOperand(0); // Drop the destination operand.
1061f4a2713aSLionel Sambuc MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
1062f4a2713aSLionel Sambuc }
1063f4a2713aSLionel Sambuc
1064f4a2713aSLionel Sambuc
1065f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1066f4a2713aSLionel Sambuc // Define tables of various ways to map pseudo instructions
1067f4a2713aSLionel Sambuc //
1068f4a2713aSLionel Sambuc
1069f4a2713aSLionel Sambuc // ForwardST0Table - Map: A = B op C into: ST(0) = ST(0) op ST(i)
1070f4a2713aSLionel Sambuc static const TableEntry ForwardST0Table[] = {
1071f4a2713aSLionel Sambuc { X86::ADD_Fp32 , X86::ADD_FST0r },
1072f4a2713aSLionel Sambuc { X86::ADD_Fp64 , X86::ADD_FST0r },
1073f4a2713aSLionel Sambuc { X86::ADD_Fp80 , X86::ADD_FST0r },
1074f4a2713aSLionel Sambuc { X86::DIV_Fp32 , X86::DIV_FST0r },
1075f4a2713aSLionel Sambuc { X86::DIV_Fp64 , X86::DIV_FST0r },
1076f4a2713aSLionel Sambuc { X86::DIV_Fp80 , X86::DIV_FST0r },
1077f4a2713aSLionel Sambuc { X86::MUL_Fp32 , X86::MUL_FST0r },
1078f4a2713aSLionel Sambuc { X86::MUL_Fp64 , X86::MUL_FST0r },
1079f4a2713aSLionel Sambuc { X86::MUL_Fp80 , X86::MUL_FST0r },
1080f4a2713aSLionel Sambuc { X86::SUB_Fp32 , X86::SUB_FST0r },
1081f4a2713aSLionel Sambuc { X86::SUB_Fp64 , X86::SUB_FST0r },
1082f4a2713aSLionel Sambuc { X86::SUB_Fp80 , X86::SUB_FST0r },
1083f4a2713aSLionel Sambuc };
1084f4a2713aSLionel Sambuc
1085f4a2713aSLionel Sambuc // ReverseST0Table - Map: A = B op C into: ST(0) = ST(i) op ST(0)
1086f4a2713aSLionel Sambuc static const TableEntry ReverseST0Table[] = {
1087f4a2713aSLionel Sambuc { X86::ADD_Fp32 , X86::ADD_FST0r }, // commutative
1088f4a2713aSLionel Sambuc { X86::ADD_Fp64 , X86::ADD_FST0r }, // commutative
1089f4a2713aSLionel Sambuc { X86::ADD_Fp80 , X86::ADD_FST0r }, // commutative
1090f4a2713aSLionel Sambuc { X86::DIV_Fp32 , X86::DIVR_FST0r },
1091f4a2713aSLionel Sambuc { X86::DIV_Fp64 , X86::DIVR_FST0r },
1092f4a2713aSLionel Sambuc { X86::DIV_Fp80 , X86::DIVR_FST0r },
1093f4a2713aSLionel Sambuc { X86::MUL_Fp32 , X86::MUL_FST0r }, // commutative
1094f4a2713aSLionel Sambuc { X86::MUL_Fp64 , X86::MUL_FST0r }, // commutative
1095f4a2713aSLionel Sambuc { X86::MUL_Fp80 , X86::MUL_FST0r }, // commutative
1096f4a2713aSLionel Sambuc { X86::SUB_Fp32 , X86::SUBR_FST0r },
1097f4a2713aSLionel Sambuc { X86::SUB_Fp64 , X86::SUBR_FST0r },
1098f4a2713aSLionel Sambuc { X86::SUB_Fp80 , X86::SUBR_FST0r },
1099f4a2713aSLionel Sambuc };
1100f4a2713aSLionel Sambuc
1101f4a2713aSLionel Sambuc // ForwardSTiTable - Map: A = B op C into: ST(i) = ST(0) op ST(i)
1102f4a2713aSLionel Sambuc static const TableEntry ForwardSTiTable[] = {
1103f4a2713aSLionel Sambuc { X86::ADD_Fp32 , X86::ADD_FrST0 }, // commutative
1104f4a2713aSLionel Sambuc { X86::ADD_Fp64 , X86::ADD_FrST0 }, // commutative
1105f4a2713aSLionel Sambuc { X86::ADD_Fp80 , X86::ADD_FrST0 }, // commutative
1106f4a2713aSLionel Sambuc { X86::DIV_Fp32 , X86::DIVR_FrST0 },
1107f4a2713aSLionel Sambuc { X86::DIV_Fp64 , X86::DIVR_FrST0 },
1108f4a2713aSLionel Sambuc { X86::DIV_Fp80 , X86::DIVR_FrST0 },
1109f4a2713aSLionel Sambuc { X86::MUL_Fp32 , X86::MUL_FrST0 }, // commutative
1110f4a2713aSLionel Sambuc { X86::MUL_Fp64 , X86::MUL_FrST0 }, // commutative
1111f4a2713aSLionel Sambuc { X86::MUL_Fp80 , X86::MUL_FrST0 }, // commutative
1112f4a2713aSLionel Sambuc { X86::SUB_Fp32 , X86::SUBR_FrST0 },
1113f4a2713aSLionel Sambuc { X86::SUB_Fp64 , X86::SUBR_FrST0 },
1114f4a2713aSLionel Sambuc { X86::SUB_Fp80 , X86::SUBR_FrST0 },
1115f4a2713aSLionel Sambuc };
1116f4a2713aSLionel Sambuc
1117f4a2713aSLionel Sambuc // ReverseSTiTable - Map: A = B op C into: ST(i) = ST(i) op ST(0)
1118f4a2713aSLionel Sambuc static const TableEntry ReverseSTiTable[] = {
1119f4a2713aSLionel Sambuc { X86::ADD_Fp32 , X86::ADD_FrST0 },
1120f4a2713aSLionel Sambuc { X86::ADD_Fp64 , X86::ADD_FrST0 },
1121f4a2713aSLionel Sambuc { X86::ADD_Fp80 , X86::ADD_FrST0 },
1122f4a2713aSLionel Sambuc { X86::DIV_Fp32 , X86::DIV_FrST0 },
1123f4a2713aSLionel Sambuc { X86::DIV_Fp64 , X86::DIV_FrST0 },
1124f4a2713aSLionel Sambuc { X86::DIV_Fp80 , X86::DIV_FrST0 },
1125f4a2713aSLionel Sambuc { X86::MUL_Fp32 , X86::MUL_FrST0 },
1126f4a2713aSLionel Sambuc { X86::MUL_Fp64 , X86::MUL_FrST0 },
1127f4a2713aSLionel Sambuc { X86::MUL_Fp80 , X86::MUL_FrST0 },
1128f4a2713aSLionel Sambuc { X86::SUB_Fp32 , X86::SUB_FrST0 },
1129f4a2713aSLionel Sambuc { X86::SUB_Fp64 , X86::SUB_FrST0 },
1130f4a2713aSLionel Sambuc { X86::SUB_Fp80 , X86::SUB_FrST0 },
1131f4a2713aSLionel Sambuc };
1132f4a2713aSLionel Sambuc
1133f4a2713aSLionel Sambuc
1134f4a2713aSLionel Sambuc /// handleTwoArgFP - Handle instructions like FADD and friends which are virtual
1135f4a2713aSLionel Sambuc /// instructions which need to be simplified and possibly transformed.
1136f4a2713aSLionel Sambuc ///
1137f4a2713aSLionel Sambuc /// Result: ST(0) = fsub ST(0), ST(i)
1138f4a2713aSLionel Sambuc /// ST(i) = fsub ST(0), ST(i)
1139f4a2713aSLionel Sambuc /// ST(0) = fsubr ST(0), ST(i)
1140f4a2713aSLionel Sambuc /// ST(i) = fsubr ST(0), ST(i)
1141f4a2713aSLionel Sambuc ///
handleTwoArgFP(MachineBasicBlock::iterator & I)1142f4a2713aSLionel Sambuc void FPS::handleTwoArgFP(MachineBasicBlock::iterator &I) {
1143f4a2713aSLionel Sambuc ASSERT_SORTED(ForwardST0Table); ASSERT_SORTED(ReverseST0Table);
1144f4a2713aSLionel Sambuc ASSERT_SORTED(ForwardSTiTable); ASSERT_SORTED(ReverseSTiTable);
1145f4a2713aSLionel Sambuc MachineInstr *MI = I;
1146f4a2713aSLionel Sambuc
1147f4a2713aSLionel Sambuc unsigned NumOperands = MI->getDesc().getNumOperands();
1148f4a2713aSLionel Sambuc assert(NumOperands == 3 && "Illegal TwoArgFP instruction!");
1149f4a2713aSLionel Sambuc unsigned Dest = getFPReg(MI->getOperand(0));
1150f4a2713aSLionel Sambuc unsigned Op0 = getFPReg(MI->getOperand(NumOperands-2));
1151f4a2713aSLionel Sambuc unsigned Op1 = getFPReg(MI->getOperand(NumOperands-1));
1152f4a2713aSLionel Sambuc bool KillsOp0 = MI->killsRegister(X86::FP0+Op0);
1153f4a2713aSLionel Sambuc bool KillsOp1 = MI->killsRegister(X86::FP0+Op1);
1154f4a2713aSLionel Sambuc DebugLoc dl = MI->getDebugLoc();
1155f4a2713aSLionel Sambuc
1156f4a2713aSLionel Sambuc unsigned TOS = getStackEntry(0);
1157f4a2713aSLionel Sambuc
1158f4a2713aSLionel Sambuc // One of our operands must be on the top of the stack. If neither is yet, we
1159f4a2713aSLionel Sambuc // need to move one.
1160f4a2713aSLionel Sambuc if (Op0 != TOS && Op1 != TOS) { // No operand at TOS?
1161f4a2713aSLionel Sambuc // We can choose to move either operand to the top of the stack. If one of
1162f4a2713aSLionel Sambuc // the operands is killed by this instruction, we want that one so that we
1163f4a2713aSLionel Sambuc // can update right on top of the old version.
1164f4a2713aSLionel Sambuc if (KillsOp0) {
1165f4a2713aSLionel Sambuc moveToTop(Op0, I); // Move dead operand to TOS.
1166f4a2713aSLionel Sambuc TOS = Op0;
1167f4a2713aSLionel Sambuc } else if (KillsOp1) {
1168f4a2713aSLionel Sambuc moveToTop(Op1, I);
1169f4a2713aSLionel Sambuc TOS = Op1;
1170f4a2713aSLionel Sambuc } else {
1171f4a2713aSLionel Sambuc // All of the operands are live after this instruction executes, so we
1172f4a2713aSLionel Sambuc // cannot update on top of any operand. Because of this, we must
1173f4a2713aSLionel Sambuc // duplicate one of the stack elements to the top. It doesn't matter
1174f4a2713aSLionel Sambuc // which one we pick.
1175f4a2713aSLionel Sambuc //
1176f4a2713aSLionel Sambuc duplicateToTop(Op0, Dest, I);
1177f4a2713aSLionel Sambuc Op0 = TOS = Dest;
1178f4a2713aSLionel Sambuc KillsOp0 = true;
1179f4a2713aSLionel Sambuc }
1180f4a2713aSLionel Sambuc } else if (!KillsOp0 && !KillsOp1) {
1181f4a2713aSLionel Sambuc // If we DO have one of our operands at the top of the stack, but we don't
1182f4a2713aSLionel Sambuc // have a dead operand, we must duplicate one of the operands to a new slot
1183f4a2713aSLionel Sambuc // on the stack.
1184f4a2713aSLionel Sambuc duplicateToTop(Op0, Dest, I);
1185f4a2713aSLionel Sambuc Op0 = TOS = Dest;
1186f4a2713aSLionel Sambuc KillsOp0 = true;
1187f4a2713aSLionel Sambuc }
1188f4a2713aSLionel Sambuc
1189f4a2713aSLionel Sambuc // Now we know that one of our operands is on the top of the stack, and at
1190f4a2713aSLionel Sambuc // least one of our operands is killed by this instruction.
1191f4a2713aSLionel Sambuc assert((TOS == Op0 || TOS == Op1) && (KillsOp0 || KillsOp1) &&
1192f4a2713aSLionel Sambuc "Stack conditions not set up right!");
1193f4a2713aSLionel Sambuc
1194f4a2713aSLionel Sambuc // We decide which form to use based on what is on the top of the stack, and
1195f4a2713aSLionel Sambuc // which operand is killed by this instruction.
1196f4a2713aSLionel Sambuc const TableEntry *InstTable;
1197f4a2713aSLionel Sambuc bool isForward = TOS == Op0;
1198f4a2713aSLionel Sambuc bool updateST0 = (TOS == Op0 && !KillsOp1) || (TOS == Op1 && !KillsOp0);
1199f4a2713aSLionel Sambuc if (updateST0) {
1200f4a2713aSLionel Sambuc if (isForward)
1201f4a2713aSLionel Sambuc InstTable = ForwardST0Table;
1202f4a2713aSLionel Sambuc else
1203f4a2713aSLionel Sambuc InstTable = ReverseST0Table;
1204f4a2713aSLionel Sambuc } else {
1205f4a2713aSLionel Sambuc if (isForward)
1206f4a2713aSLionel Sambuc InstTable = ForwardSTiTable;
1207f4a2713aSLionel Sambuc else
1208f4a2713aSLionel Sambuc InstTable = ReverseSTiTable;
1209f4a2713aSLionel Sambuc }
1210f4a2713aSLionel Sambuc
1211f4a2713aSLionel Sambuc int Opcode = Lookup(InstTable, array_lengthof(ForwardST0Table),
1212f4a2713aSLionel Sambuc MI->getOpcode());
1213f4a2713aSLionel Sambuc assert(Opcode != -1 && "Unknown TwoArgFP pseudo instruction!");
1214f4a2713aSLionel Sambuc
1215f4a2713aSLionel Sambuc // NotTOS - The register which is not on the top of stack...
1216f4a2713aSLionel Sambuc unsigned NotTOS = (TOS == Op0) ? Op1 : Op0;
1217f4a2713aSLionel Sambuc
1218f4a2713aSLionel Sambuc // Replace the old instruction with a new instruction
1219f4a2713aSLionel Sambuc MBB->remove(I++);
1220f4a2713aSLionel Sambuc I = BuildMI(*MBB, I, dl, TII->get(Opcode)).addReg(getSTReg(NotTOS));
1221f4a2713aSLionel Sambuc
1222f4a2713aSLionel Sambuc // If both operands are killed, pop one off of the stack in addition to
1223f4a2713aSLionel Sambuc // overwriting the other one.
1224f4a2713aSLionel Sambuc if (KillsOp0 && KillsOp1 && Op0 != Op1) {
1225f4a2713aSLionel Sambuc assert(!updateST0 && "Should have updated other operand!");
1226f4a2713aSLionel Sambuc popStackAfter(I); // Pop the top of stack
1227f4a2713aSLionel Sambuc }
1228f4a2713aSLionel Sambuc
1229f4a2713aSLionel Sambuc // Update stack information so that we know the destination register is now on
1230f4a2713aSLionel Sambuc // the stack.
1231f4a2713aSLionel Sambuc unsigned UpdatedSlot = getSlot(updateST0 ? TOS : NotTOS);
1232f4a2713aSLionel Sambuc assert(UpdatedSlot < StackTop && Dest < 7);
1233f4a2713aSLionel Sambuc Stack[UpdatedSlot] = Dest;
1234f4a2713aSLionel Sambuc RegMap[Dest] = UpdatedSlot;
1235f4a2713aSLionel Sambuc MBB->getParent()->DeleteMachineInstr(MI); // Remove the old instruction
1236f4a2713aSLionel Sambuc }
1237f4a2713aSLionel Sambuc
1238f4a2713aSLionel Sambuc /// handleCompareFP - Handle FUCOM and FUCOMI instructions, which have two FP
1239f4a2713aSLionel Sambuc /// register arguments and no explicit destinations.
1240f4a2713aSLionel Sambuc ///
handleCompareFP(MachineBasicBlock::iterator & I)1241f4a2713aSLionel Sambuc void FPS::handleCompareFP(MachineBasicBlock::iterator &I) {
1242f4a2713aSLionel Sambuc ASSERT_SORTED(ForwardST0Table); ASSERT_SORTED(ReverseST0Table);
1243f4a2713aSLionel Sambuc ASSERT_SORTED(ForwardSTiTable); ASSERT_SORTED(ReverseSTiTable);
1244f4a2713aSLionel Sambuc MachineInstr *MI = I;
1245f4a2713aSLionel Sambuc
1246f4a2713aSLionel Sambuc unsigned NumOperands = MI->getDesc().getNumOperands();
1247f4a2713aSLionel Sambuc assert(NumOperands == 2 && "Illegal FUCOM* instruction!");
1248f4a2713aSLionel Sambuc unsigned Op0 = getFPReg(MI->getOperand(NumOperands-2));
1249f4a2713aSLionel Sambuc unsigned Op1 = getFPReg(MI->getOperand(NumOperands-1));
1250f4a2713aSLionel Sambuc bool KillsOp0 = MI->killsRegister(X86::FP0+Op0);
1251f4a2713aSLionel Sambuc bool KillsOp1 = MI->killsRegister(X86::FP0+Op1);
1252f4a2713aSLionel Sambuc
1253f4a2713aSLionel Sambuc // Make sure the first operand is on the top of stack, the other one can be
1254f4a2713aSLionel Sambuc // anywhere.
1255f4a2713aSLionel Sambuc moveToTop(Op0, I);
1256f4a2713aSLionel Sambuc
1257f4a2713aSLionel Sambuc // Change from the pseudo instruction to the concrete instruction.
1258f4a2713aSLionel Sambuc MI->getOperand(0).setReg(getSTReg(Op1));
1259f4a2713aSLionel Sambuc MI->RemoveOperand(1);
1260f4a2713aSLionel Sambuc MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
1261f4a2713aSLionel Sambuc
1262f4a2713aSLionel Sambuc // If any of the operands are killed by this instruction, free them.
1263f4a2713aSLionel Sambuc if (KillsOp0) freeStackSlotAfter(I, Op0);
1264f4a2713aSLionel Sambuc if (KillsOp1 && Op0 != Op1) freeStackSlotAfter(I, Op1);
1265f4a2713aSLionel Sambuc }
1266f4a2713aSLionel Sambuc
1267f4a2713aSLionel Sambuc /// handleCondMovFP - Handle two address conditional move instructions. These
1268f4a2713aSLionel Sambuc /// instructions move a st(i) register to st(0) iff a condition is true. These
1269f4a2713aSLionel Sambuc /// instructions require that the first operand is at the top of the stack, but
1270f4a2713aSLionel Sambuc /// otherwise don't modify the stack at all.
handleCondMovFP(MachineBasicBlock::iterator & I)1271f4a2713aSLionel Sambuc void FPS::handleCondMovFP(MachineBasicBlock::iterator &I) {
1272f4a2713aSLionel Sambuc MachineInstr *MI = I;
1273f4a2713aSLionel Sambuc
1274f4a2713aSLionel Sambuc unsigned Op0 = getFPReg(MI->getOperand(0));
1275f4a2713aSLionel Sambuc unsigned Op1 = getFPReg(MI->getOperand(2));
1276f4a2713aSLionel Sambuc bool KillsOp1 = MI->killsRegister(X86::FP0+Op1);
1277f4a2713aSLionel Sambuc
1278f4a2713aSLionel Sambuc // The first operand *must* be on the top of the stack.
1279f4a2713aSLionel Sambuc moveToTop(Op0, I);
1280f4a2713aSLionel Sambuc
1281f4a2713aSLionel Sambuc // Change the second operand to the stack register that the operand is in.
1282f4a2713aSLionel Sambuc // Change from the pseudo instruction to the concrete instruction.
1283f4a2713aSLionel Sambuc MI->RemoveOperand(0);
1284f4a2713aSLionel Sambuc MI->RemoveOperand(1);
1285f4a2713aSLionel Sambuc MI->getOperand(0).setReg(getSTReg(Op1));
1286f4a2713aSLionel Sambuc MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
1287f4a2713aSLionel Sambuc
1288f4a2713aSLionel Sambuc // If we kill the second operand, make sure to pop it from the stack.
1289f4a2713aSLionel Sambuc if (Op0 != Op1 && KillsOp1) {
1290f4a2713aSLionel Sambuc // Get this value off of the register stack.
1291f4a2713aSLionel Sambuc freeStackSlotAfter(I, Op1);
1292f4a2713aSLionel Sambuc }
1293f4a2713aSLionel Sambuc }
1294f4a2713aSLionel Sambuc
1295f4a2713aSLionel Sambuc
1296f4a2713aSLionel Sambuc /// handleSpecialFP - Handle special instructions which behave unlike other
1297f4a2713aSLionel Sambuc /// floating point instructions. This is primarily intended for use by pseudo
1298f4a2713aSLionel Sambuc /// instructions.
1299f4a2713aSLionel Sambuc ///
handleSpecialFP(MachineBasicBlock::iterator & Inst)1300*0a6a1f1dSLionel Sambuc void FPS::handleSpecialFP(MachineBasicBlock::iterator &Inst) {
1301*0a6a1f1dSLionel Sambuc MachineInstr *MI = Inst;
1302*0a6a1f1dSLionel Sambuc
1303*0a6a1f1dSLionel Sambuc if (MI->isCall()) {
1304*0a6a1f1dSLionel Sambuc handleCall(Inst);
1305*0a6a1f1dSLionel Sambuc return;
1306*0a6a1f1dSLionel Sambuc }
1307*0a6a1f1dSLionel Sambuc
1308f4a2713aSLionel Sambuc switch (MI->getOpcode()) {
1309f4a2713aSLionel Sambuc default: llvm_unreachable("Unknown SpecialFP instruction!");
1310f4a2713aSLionel Sambuc case TargetOpcode::COPY: {
1311f4a2713aSLionel Sambuc // We handle three kinds of copies: FP <- FP, FP <- ST, and ST <- FP.
1312f4a2713aSLionel Sambuc const MachineOperand &MO1 = MI->getOperand(1);
1313f4a2713aSLionel Sambuc const MachineOperand &MO0 = MI->getOperand(0);
1314f4a2713aSLionel Sambuc bool KillsSrc = MI->killsRegister(MO1.getReg());
1315f4a2713aSLionel Sambuc
1316f4a2713aSLionel Sambuc // FP <- FP copy.
1317f4a2713aSLionel Sambuc unsigned DstFP = getFPReg(MO0);
1318f4a2713aSLionel Sambuc unsigned SrcFP = getFPReg(MO1);
1319f4a2713aSLionel Sambuc assert(isLive(SrcFP) && "Cannot copy dead register");
1320f4a2713aSLionel Sambuc if (KillsSrc) {
1321f4a2713aSLionel Sambuc // If the input operand is killed, we can just change the owner of the
1322f4a2713aSLionel Sambuc // incoming stack slot into the result.
1323f4a2713aSLionel Sambuc unsigned Slot = getSlot(SrcFP);
1324f4a2713aSLionel Sambuc Stack[Slot] = DstFP;
1325f4a2713aSLionel Sambuc RegMap[DstFP] = Slot;
1326f4a2713aSLionel Sambuc } else {
1327f4a2713aSLionel Sambuc // For COPY we just duplicate the specified value to a new stack slot.
1328f4a2713aSLionel Sambuc // This could be made better, but would require substantial changes.
1329*0a6a1f1dSLionel Sambuc duplicateToTop(SrcFP, DstFP, Inst);
1330f4a2713aSLionel Sambuc }
1331f4a2713aSLionel Sambuc break;
1332f4a2713aSLionel Sambuc }
1333f4a2713aSLionel Sambuc
1334f4a2713aSLionel Sambuc case TargetOpcode::IMPLICIT_DEF: {
1335f4a2713aSLionel Sambuc // All FP registers must be explicitly defined, so load a 0 instead.
1336f4a2713aSLionel Sambuc unsigned Reg = MI->getOperand(0).getReg() - X86::FP0;
1337f4a2713aSLionel Sambuc DEBUG(dbgs() << "Emitting LD_F0 for implicit FP" << Reg << '\n');
1338*0a6a1f1dSLionel Sambuc BuildMI(*MBB, Inst, MI->getDebugLoc(), TII->get(X86::LD_F0));
1339f4a2713aSLionel Sambuc pushReg(Reg);
1340f4a2713aSLionel Sambuc break;
1341f4a2713aSLionel Sambuc }
1342f4a2713aSLionel Sambuc
1343f4a2713aSLionel Sambuc case TargetOpcode::INLINEASM: {
1344f4a2713aSLionel Sambuc // The inline asm MachineInstr currently only *uses* FP registers for the
1345f4a2713aSLionel Sambuc // 'f' constraint. These should be turned into the current ST(x) register
1346f4a2713aSLionel Sambuc // in the machine instr.
1347f4a2713aSLionel Sambuc //
1348f4a2713aSLionel Sambuc // There are special rules for x87 inline assembly. The compiler must know
1349f4a2713aSLionel Sambuc // exactly how many registers are popped and pushed implicitly by the asm.
1350f4a2713aSLionel Sambuc // Otherwise it is not possible to restore the stack state after the inline
1351f4a2713aSLionel Sambuc // asm.
1352f4a2713aSLionel Sambuc //
1353f4a2713aSLionel Sambuc // There are 3 kinds of input operands:
1354f4a2713aSLionel Sambuc //
1355f4a2713aSLionel Sambuc // 1. Popped inputs. These must appear at the stack top in ST0-STn. A
1356f4a2713aSLionel Sambuc // popped input operand must be in a fixed stack slot, and it is either
1357f4a2713aSLionel Sambuc // tied to an output operand, or in the clobber list. The MI has ST use
1358f4a2713aSLionel Sambuc // and def operands for these inputs.
1359f4a2713aSLionel Sambuc //
1360f4a2713aSLionel Sambuc // 2. Fixed inputs. These inputs appear in fixed stack slots, but are
1361f4a2713aSLionel Sambuc // preserved by the inline asm. The fixed stack slots must be STn-STm
1362f4a2713aSLionel Sambuc // following the popped inputs. A fixed input operand cannot be tied to
1363f4a2713aSLionel Sambuc // an output or appear in the clobber list. The MI has ST use operands
1364f4a2713aSLionel Sambuc // and no defs for these inputs.
1365f4a2713aSLionel Sambuc //
1366f4a2713aSLionel Sambuc // 3. Preserved inputs. These inputs use the "f" constraint which is
1367f4a2713aSLionel Sambuc // represented as an FP register. The inline asm won't change these
1368f4a2713aSLionel Sambuc // stack slots.
1369f4a2713aSLionel Sambuc //
1370f4a2713aSLionel Sambuc // Outputs must be in ST registers, FP outputs are not allowed. Clobbered
1371f4a2713aSLionel Sambuc // registers do not count as output operands. The inline asm changes the
1372f4a2713aSLionel Sambuc // stack as if it popped all the popped inputs and then pushed all the
1373f4a2713aSLionel Sambuc // output operands.
1374f4a2713aSLionel Sambuc
1375f4a2713aSLionel Sambuc // Scan the assembly for ST registers used, defined and clobbered. We can
1376f4a2713aSLionel Sambuc // only tell clobbers from defs by looking at the asm descriptor.
1377f4a2713aSLionel Sambuc unsigned STUses = 0, STDefs = 0, STClobbers = 0, STDeadDefs = 0;
1378f4a2713aSLionel Sambuc unsigned NumOps = 0;
1379*0a6a1f1dSLionel Sambuc SmallSet<unsigned, 1> FRegIdx;
1380*0a6a1f1dSLionel Sambuc unsigned RCID;
1381*0a6a1f1dSLionel Sambuc
1382f4a2713aSLionel Sambuc for (unsigned i = InlineAsm::MIOp_FirstOperand, e = MI->getNumOperands();
1383f4a2713aSLionel Sambuc i != e && MI->getOperand(i).isImm(); i += 1 + NumOps) {
1384f4a2713aSLionel Sambuc unsigned Flags = MI->getOperand(i).getImm();
1385*0a6a1f1dSLionel Sambuc
1386f4a2713aSLionel Sambuc NumOps = InlineAsm::getNumOperandRegisters(Flags);
1387f4a2713aSLionel Sambuc if (NumOps != 1)
1388f4a2713aSLionel Sambuc continue;
1389f4a2713aSLionel Sambuc const MachineOperand &MO = MI->getOperand(i + 1);
1390f4a2713aSLionel Sambuc if (!MO.isReg())
1391f4a2713aSLionel Sambuc continue;
1392*0a6a1f1dSLionel Sambuc unsigned STReg = MO.getReg() - X86::FP0;
1393f4a2713aSLionel Sambuc if (STReg >= 8)
1394f4a2713aSLionel Sambuc continue;
1395f4a2713aSLionel Sambuc
1396*0a6a1f1dSLionel Sambuc // If the flag has a register class constraint, this must be an operand
1397*0a6a1f1dSLionel Sambuc // with constraint "f". Record its index and continue.
1398*0a6a1f1dSLionel Sambuc if (InlineAsm::hasRegClassConstraint(Flags, RCID)) {
1399*0a6a1f1dSLionel Sambuc FRegIdx.insert(i + 1);
1400*0a6a1f1dSLionel Sambuc continue;
1401*0a6a1f1dSLionel Sambuc }
1402*0a6a1f1dSLionel Sambuc
1403f4a2713aSLionel Sambuc switch (InlineAsm::getKind(Flags)) {
1404f4a2713aSLionel Sambuc case InlineAsm::Kind_RegUse:
1405f4a2713aSLionel Sambuc STUses |= (1u << STReg);
1406f4a2713aSLionel Sambuc break;
1407f4a2713aSLionel Sambuc case InlineAsm::Kind_RegDef:
1408f4a2713aSLionel Sambuc case InlineAsm::Kind_RegDefEarlyClobber:
1409f4a2713aSLionel Sambuc STDefs |= (1u << STReg);
1410f4a2713aSLionel Sambuc if (MO.isDead())
1411f4a2713aSLionel Sambuc STDeadDefs |= (1u << STReg);
1412f4a2713aSLionel Sambuc break;
1413f4a2713aSLionel Sambuc case InlineAsm::Kind_Clobber:
1414f4a2713aSLionel Sambuc STClobbers |= (1u << STReg);
1415f4a2713aSLionel Sambuc break;
1416f4a2713aSLionel Sambuc default:
1417f4a2713aSLionel Sambuc break;
1418f4a2713aSLionel Sambuc }
1419f4a2713aSLionel Sambuc }
1420f4a2713aSLionel Sambuc
1421f4a2713aSLionel Sambuc if (STUses && !isMask_32(STUses))
1422f4a2713aSLionel Sambuc MI->emitError("fixed input regs must be last on the x87 stack");
1423f4a2713aSLionel Sambuc unsigned NumSTUses = CountTrailingOnes_32(STUses);
1424f4a2713aSLionel Sambuc
1425f4a2713aSLionel Sambuc // Defs must be contiguous from the stack top. ST0-STn.
1426f4a2713aSLionel Sambuc if (STDefs && !isMask_32(STDefs)) {
1427f4a2713aSLionel Sambuc MI->emitError("output regs must be last on the x87 stack");
1428f4a2713aSLionel Sambuc STDefs = NextPowerOf2(STDefs) - 1;
1429f4a2713aSLionel Sambuc }
1430f4a2713aSLionel Sambuc unsigned NumSTDefs = CountTrailingOnes_32(STDefs);
1431f4a2713aSLionel Sambuc
1432f4a2713aSLionel Sambuc // So must the clobbered stack slots. ST0-STm, m >= n.
1433f4a2713aSLionel Sambuc if (STClobbers && !isMask_32(STDefs | STClobbers))
1434f4a2713aSLionel Sambuc MI->emitError("clobbers must be last on the x87 stack");
1435f4a2713aSLionel Sambuc
1436f4a2713aSLionel Sambuc // Popped inputs are the ones that are also clobbered or defined.
1437f4a2713aSLionel Sambuc unsigned STPopped = STUses & (STDefs | STClobbers);
1438f4a2713aSLionel Sambuc if (STPopped && !isMask_32(STPopped))
1439f4a2713aSLionel Sambuc MI->emitError("implicitly popped regs must be last on the x87 stack");
1440f4a2713aSLionel Sambuc unsigned NumSTPopped = CountTrailingOnes_32(STPopped);
1441f4a2713aSLionel Sambuc
1442f4a2713aSLionel Sambuc DEBUG(dbgs() << "Asm uses " << NumSTUses << " fixed regs, pops "
1443f4a2713aSLionel Sambuc << NumSTPopped << ", and defines " << NumSTDefs << " regs.\n");
1444f4a2713aSLionel Sambuc
1445*0a6a1f1dSLionel Sambuc #ifndef NDEBUG
1446*0a6a1f1dSLionel Sambuc // If any input operand uses constraint "f", all output register
1447*0a6a1f1dSLionel Sambuc // constraints must be early-clobber defs.
1448*0a6a1f1dSLionel Sambuc for (unsigned I = 0, E = MI->getNumOperands(); I < E; ++I)
1449*0a6a1f1dSLionel Sambuc if (FRegIdx.count(I)) {
1450*0a6a1f1dSLionel Sambuc assert((1 << getFPReg(MI->getOperand(I)) & STDefs) == 0 &&
1451*0a6a1f1dSLionel Sambuc "Operands with constraint \"f\" cannot overlap with defs");
1452*0a6a1f1dSLionel Sambuc }
1453*0a6a1f1dSLionel Sambuc #endif
1454*0a6a1f1dSLionel Sambuc
1455*0a6a1f1dSLionel Sambuc // Collect all FP registers (register operands with constraints "t", "u",
1456*0a6a1f1dSLionel Sambuc // and "f") to kill afer the instruction.
1457f4a2713aSLionel Sambuc unsigned FPKills = ((1u << NumFPRegs) - 1) & ~0xff;
1458f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1459f4a2713aSLionel Sambuc MachineOperand &Op = MI->getOperand(i);
1460f4a2713aSLionel Sambuc if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6)
1461f4a2713aSLionel Sambuc continue;
1462f4a2713aSLionel Sambuc unsigned FPReg = getFPReg(Op);
1463f4a2713aSLionel Sambuc
1464f4a2713aSLionel Sambuc // If we kill this operand, make sure to pop it from the stack after the
1465f4a2713aSLionel Sambuc // asm. We just remember it for now, and pop them all off at the end in
1466f4a2713aSLionel Sambuc // a batch.
1467*0a6a1f1dSLionel Sambuc if (Op.isUse() && Op.isKill())
1468f4a2713aSLionel Sambuc FPKills |= 1U << FPReg;
1469f4a2713aSLionel Sambuc }
1470f4a2713aSLionel Sambuc
1471*0a6a1f1dSLionel Sambuc // Do not include registers that are implicitly popped by defs/clobbers.
1472*0a6a1f1dSLionel Sambuc FPKills &= ~(STDefs | STClobbers);
1473f4a2713aSLionel Sambuc
1474f4a2713aSLionel Sambuc // Now we can rearrange the live registers to match what was requested.
1475*0a6a1f1dSLionel Sambuc unsigned char STUsesArray[8];
1476*0a6a1f1dSLionel Sambuc
1477*0a6a1f1dSLionel Sambuc for (unsigned I = 0; I < NumSTUses; ++I)
1478*0a6a1f1dSLionel Sambuc STUsesArray[I] = I;
1479*0a6a1f1dSLionel Sambuc
1480*0a6a1f1dSLionel Sambuc shuffleStackTop(STUsesArray, NumSTUses, Inst);
1481f4a2713aSLionel Sambuc DEBUG({dbgs() << "Before asm: "; dumpStack();});
1482f4a2713aSLionel Sambuc
1483f4a2713aSLionel Sambuc // With the stack layout fixed, rewrite the FP registers.
1484f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1485f4a2713aSLionel Sambuc MachineOperand &Op = MI->getOperand(i);
1486f4a2713aSLionel Sambuc if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6)
1487f4a2713aSLionel Sambuc continue;
1488*0a6a1f1dSLionel Sambuc
1489f4a2713aSLionel Sambuc unsigned FPReg = getFPReg(Op);
1490*0a6a1f1dSLionel Sambuc
1491*0a6a1f1dSLionel Sambuc if (FRegIdx.count(i))
1492*0a6a1f1dSLionel Sambuc // Operand with constraint "f".
1493f4a2713aSLionel Sambuc Op.setReg(getSTReg(FPReg));
1494*0a6a1f1dSLionel Sambuc else
1495*0a6a1f1dSLionel Sambuc // Operand with a single register class constraint ("t" or "u").
1496*0a6a1f1dSLionel Sambuc Op.setReg(X86::ST0 + FPReg);
1497f4a2713aSLionel Sambuc }
1498f4a2713aSLionel Sambuc
1499f4a2713aSLionel Sambuc // Simulate the inline asm popping its inputs and pushing its outputs.
1500f4a2713aSLionel Sambuc StackTop -= NumSTPopped;
1501f4a2713aSLionel Sambuc
1502f4a2713aSLionel Sambuc for (unsigned i = 0; i < NumSTDefs; ++i)
1503*0a6a1f1dSLionel Sambuc pushReg(NumSTDefs - i - 1);
1504f4a2713aSLionel Sambuc
1505f4a2713aSLionel Sambuc // If this asm kills any FP registers (is the last use of them) we must
1506f4a2713aSLionel Sambuc // explicitly emit pop instructions for them. Do this now after the asm has
1507f4a2713aSLionel Sambuc // executed so that the ST(x) numbers are not off (which would happen if we
1508f4a2713aSLionel Sambuc // did this inline with operand rewriting).
1509f4a2713aSLionel Sambuc //
1510f4a2713aSLionel Sambuc // Note: this might be a non-optimal pop sequence. We might be able to do
1511f4a2713aSLionel Sambuc // better by trying to pop in stack order or something.
1512f4a2713aSLionel Sambuc while (FPKills) {
1513f4a2713aSLionel Sambuc unsigned FPReg = countTrailingZeros(FPKills);
1514f4a2713aSLionel Sambuc if (isLive(FPReg))
1515*0a6a1f1dSLionel Sambuc freeStackSlotAfter(Inst, FPReg);
1516f4a2713aSLionel Sambuc FPKills &= ~(1U << FPReg);
1517f4a2713aSLionel Sambuc }
1518*0a6a1f1dSLionel Sambuc
1519f4a2713aSLionel Sambuc // Don't delete the inline asm!
1520f4a2713aSLionel Sambuc return;
1521f4a2713aSLionel Sambuc }
1522f4a2713aSLionel Sambuc
1523f4a2713aSLionel Sambuc case X86::WIN_FTOL_32:
1524f4a2713aSLionel Sambuc case X86::WIN_FTOL_64: {
1525f4a2713aSLionel Sambuc // Push the operand into ST0.
1526f4a2713aSLionel Sambuc MachineOperand &Op = MI->getOperand(0);
1527f4a2713aSLionel Sambuc assert(Op.isUse() && Op.isReg() &&
1528f4a2713aSLionel Sambuc Op.getReg() >= X86::FP0 && Op.getReg() <= X86::FP6);
1529f4a2713aSLionel Sambuc unsigned FPReg = getFPReg(Op);
1530f4a2713aSLionel Sambuc if (Op.isKill())
1531*0a6a1f1dSLionel Sambuc moveToTop(FPReg, Inst);
1532f4a2713aSLionel Sambuc else
1533*0a6a1f1dSLionel Sambuc duplicateToTop(FPReg, FPReg, Inst);
1534f4a2713aSLionel Sambuc
1535f4a2713aSLionel Sambuc // Emit the call. This will pop the operand.
1536*0a6a1f1dSLionel Sambuc BuildMI(*MBB, Inst, MI->getDebugLoc(), TII->get(X86::CALLpcrel32))
1537f4a2713aSLionel Sambuc .addExternalSymbol("_ftol2")
1538f4a2713aSLionel Sambuc .addReg(X86::ST0, RegState::ImplicitKill)
1539f4a2713aSLionel Sambuc .addReg(X86::ECX, RegState::ImplicitDefine)
1540f4a2713aSLionel Sambuc .addReg(X86::EAX, RegState::Define | RegState::Implicit)
1541f4a2713aSLionel Sambuc .addReg(X86::EDX, RegState::Define | RegState::Implicit)
1542f4a2713aSLionel Sambuc .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
1543f4a2713aSLionel Sambuc --StackTop;
1544f4a2713aSLionel Sambuc
1545f4a2713aSLionel Sambuc break;
1546f4a2713aSLionel Sambuc }
1547f4a2713aSLionel Sambuc
1548*0a6a1f1dSLionel Sambuc case X86::RETQ:
1549*0a6a1f1dSLionel Sambuc case X86::RETL:
1550*0a6a1f1dSLionel Sambuc case X86::RETIL:
1551*0a6a1f1dSLionel Sambuc case X86::RETIQ:
1552f4a2713aSLionel Sambuc // If RET has an FP register use operand, pass the first one in ST(0) and
1553f4a2713aSLionel Sambuc // the second one in ST(1).
1554f4a2713aSLionel Sambuc
1555f4a2713aSLionel Sambuc // Find the register operands.
1556f4a2713aSLionel Sambuc unsigned FirstFPRegOp = ~0U, SecondFPRegOp = ~0U;
1557f4a2713aSLionel Sambuc unsigned LiveMask = 0;
1558f4a2713aSLionel Sambuc
1559f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1560f4a2713aSLionel Sambuc MachineOperand &Op = MI->getOperand(i);
1561f4a2713aSLionel Sambuc if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6)
1562f4a2713aSLionel Sambuc continue;
1563f4a2713aSLionel Sambuc // FP Register uses must be kills unless there are two uses of the same
1564f4a2713aSLionel Sambuc // register, in which case only one will be a kill.
1565f4a2713aSLionel Sambuc assert(Op.isUse() &&
1566f4a2713aSLionel Sambuc (Op.isKill() || // Marked kill.
1567f4a2713aSLionel Sambuc getFPReg(Op) == FirstFPRegOp || // Second instance.
1568f4a2713aSLionel Sambuc MI->killsRegister(Op.getReg())) && // Later use is marked kill.
1569f4a2713aSLionel Sambuc "Ret only defs operands, and values aren't live beyond it");
1570f4a2713aSLionel Sambuc
1571f4a2713aSLionel Sambuc if (FirstFPRegOp == ~0U)
1572f4a2713aSLionel Sambuc FirstFPRegOp = getFPReg(Op);
1573f4a2713aSLionel Sambuc else {
1574f4a2713aSLionel Sambuc assert(SecondFPRegOp == ~0U && "More than two fp operands!");
1575f4a2713aSLionel Sambuc SecondFPRegOp = getFPReg(Op);
1576f4a2713aSLionel Sambuc }
1577f4a2713aSLionel Sambuc LiveMask |= (1 << getFPReg(Op));
1578f4a2713aSLionel Sambuc
1579f4a2713aSLionel Sambuc // Remove the operand so that later passes don't see it.
1580f4a2713aSLionel Sambuc MI->RemoveOperand(i);
1581f4a2713aSLionel Sambuc --i, --e;
1582f4a2713aSLionel Sambuc }
1583f4a2713aSLionel Sambuc
1584f4a2713aSLionel Sambuc // We may have been carrying spurious live-ins, so make sure only the returned
1585f4a2713aSLionel Sambuc // registers are left live.
1586f4a2713aSLionel Sambuc adjustLiveRegs(LiveMask, MI);
1587f4a2713aSLionel Sambuc if (!LiveMask) return; // Quick check to see if any are possible.
1588f4a2713aSLionel Sambuc
1589f4a2713aSLionel Sambuc // There are only four possibilities here:
1590f4a2713aSLionel Sambuc // 1) we are returning a single FP value. In this case, it has to be in
1591f4a2713aSLionel Sambuc // ST(0) already, so just declare success by removing the value from the
1592f4a2713aSLionel Sambuc // FP Stack.
1593f4a2713aSLionel Sambuc if (SecondFPRegOp == ~0U) {
1594f4a2713aSLionel Sambuc // Assert that the top of stack contains the right FP register.
1595f4a2713aSLionel Sambuc assert(StackTop == 1 && FirstFPRegOp == getStackEntry(0) &&
1596f4a2713aSLionel Sambuc "Top of stack not the right register for RET!");
1597f4a2713aSLionel Sambuc
1598f4a2713aSLionel Sambuc // Ok, everything is good, mark the value as not being on the stack
1599f4a2713aSLionel Sambuc // anymore so that our assertion about the stack being empty at end of
1600f4a2713aSLionel Sambuc // block doesn't fire.
1601f4a2713aSLionel Sambuc StackTop = 0;
1602f4a2713aSLionel Sambuc return;
1603f4a2713aSLionel Sambuc }
1604f4a2713aSLionel Sambuc
1605f4a2713aSLionel Sambuc // Otherwise, we are returning two values:
1606f4a2713aSLionel Sambuc // 2) If returning the same value for both, we only have one thing in the FP
1607f4a2713aSLionel Sambuc // stack. Consider: RET FP1, FP1
1608f4a2713aSLionel Sambuc if (StackTop == 1) {
1609f4a2713aSLionel Sambuc assert(FirstFPRegOp == SecondFPRegOp && FirstFPRegOp == getStackEntry(0)&&
1610f4a2713aSLionel Sambuc "Stack misconfiguration for RET!");
1611f4a2713aSLionel Sambuc
1612f4a2713aSLionel Sambuc // Duplicate the TOS so that we return it twice. Just pick some other FPx
1613f4a2713aSLionel Sambuc // register to hold it.
1614*0a6a1f1dSLionel Sambuc unsigned NewReg = ScratchFPReg;
1615f4a2713aSLionel Sambuc duplicateToTop(FirstFPRegOp, NewReg, MI);
1616f4a2713aSLionel Sambuc FirstFPRegOp = NewReg;
1617f4a2713aSLionel Sambuc }
1618f4a2713aSLionel Sambuc
1619f4a2713aSLionel Sambuc /// Okay we know we have two different FPx operands now:
1620f4a2713aSLionel Sambuc assert(StackTop == 2 && "Must have two values live!");
1621f4a2713aSLionel Sambuc
1622f4a2713aSLionel Sambuc /// 3) If SecondFPRegOp is currently in ST(0) and FirstFPRegOp is currently
1623f4a2713aSLionel Sambuc /// in ST(1). In this case, emit an fxch.
1624f4a2713aSLionel Sambuc if (getStackEntry(0) == SecondFPRegOp) {
1625f4a2713aSLionel Sambuc assert(getStackEntry(1) == FirstFPRegOp && "Unknown regs live");
1626f4a2713aSLionel Sambuc moveToTop(FirstFPRegOp, MI);
1627f4a2713aSLionel Sambuc }
1628f4a2713aSLionel Sambuc
1629f4a2713aSLionel Sambuc /// 4) Finally, FirstFPRegOp must be in ST(0) and SecondFPRegOp must be in
1630f4a2713aSLionel Sambuc /// ST(1). Just remove both from our understanding of the stack and return.
1631f4a2713aSLionel Sambuc assert(getStackEntry(0) == FirstFPRegOp && "Unknown regs live");
1632f4a2713aSLionel Sambuc assert(getStackEntry(1) == SecondFPRegOp && "Unknown regs live");
1633f4a2713aSLionel Sambuc StackTop = 0;
1634f4a2713aSLionel Sambuc return;
1635f4a2713aSLionel Sambuc }
1636f4a2713aSLionel Sambuc
1637*0a6a1f1dSLionel Sambuc Inst = MBB->erase(Inst); // Remove the pseudo instruction
1638f4a2713aSLionel Sambuc
1639f4a2713aSLionel Sambuc // We want to leave I pointing to the previous instruction, but what if we
1640f4a2713aSLionel Sambuc // just erased the first instruction?
1641*0a6a1f1dSLionel Sambuc if (Inst == MBB->begin()) {
1642f4a2713aSLionel Sambuc DEBUG(dbgs() << "Inserting dummy KILL\n");
1643*0a6a1f1dSLionel Sambuc Inst = BuildMI(*MBB, Inst, DebugLoc(), TII->get(TargetOpcode::KILL));
1644f4a2713aSLionel Sambuc } else
1645*0a6a1f1dSLionel Sambuc --Inst;
1646*0a6a1f1dSLionel Sambuc }
1647*0a6a1f1dSLionel Sambuc
setKillFlags(MachineBasicBlock & MBB) const1648*0a6a1f1dSLionel Sambuc void FPS::setKillFlags(MachineBasicBlock &MBB) const {
1649*0a6a1f1dSLionel Sambuc const TargetRegisterInfo *TRI =
1650*0a6a1f1dSLionel Sambuc MBB.getParent()->getSubtarget().getRegisterInfo();
1651*0a6a1f1dSLionel Sambuc LivePhysRegs LPR(TRI);
1652*0a6a1f1dSLionel Sambuc
1653*0a6a1f1dSLionel Sambuc LPR.addLiveOuts(&MBB);
1654*0a6a1f1dSLionel Sambuc
1655*0a6a1f1dSLionel Sambuc for (MachineBasicBlock::reverse_iterator I = MBB.rbegin(), E = MBB.rend();
1656*0a6a1f1dSLionel Sambuc I != E; ++I) {
1657*0a6a1f1dSLionel Sambuc if (I->isDebugValue())
1658*0a6a1f1dSLionel Sambuc continue;
1659*0a6a1f1dSLionel Sambuc
1660*0a6a1f1dSLionel Sambuc std::bitset<8> Defs;
1661*0a6a1f1dSLionel Sambuc SmallVector<MachineOperand *, 2> Uses;
1662*0a6a1f1dSLionel Sambuc MachineInstr &MI = *I;
1663*0a6a1f1dSLionel Sambuc
1664*0a6a1f1dSLionel Sambuc for (auto &MO : I->operands()) {
1665*0a6a1f1dSLionel Sambuc if (!MO.isReg())
1666*0a6a1f1dSLionel Sambuc continue;
1667*0a6a1f1dSLionel Sambuc
1668*0a6a1f1dSLionel Sambuc unsigned Reg = MO.getReg() - X86::FP0;
1669*0a6a1f1dSLionel Sambuc
1670*0a6a1f1dSLionel Sambuc if (Reg >= 8)
1671*0a6a1f1dSLionel Sambuc continue;
1672*0a6a1f1dSLionel Sambuc
1673*0a6a1f1dSLionel Sambuc if (MO.isDef()) {
1674*0a6a1f1dSLionel Sambuc Defs.set(Reg);
1675*0a6a1f1dSLionel Sambuc if (!LPR.contains(MO.getReg()))
1676*0a6a1f1dSLionel Sambuc MO.setIsDead();
1677*0a6a1f1dSLionel Sambuc } else
1678*0a6a1f1dSLionel Sambuc Uses.push_back(&MO);
1679*0a6a1f1dSLionel Sambuc }
1680*0a6a1f1dSLionel Sambuc
1681*0a6a1f1dSLionel Sambuc for (auto *MO : Uses)
1682*0a6a1f1dSLionel Sambuc if (Defs.test(getFPReg(*MO)) || !LPR.contains(MO->getReg()))
1683*0a6a1f1dSLionel Sambuc MO->setIsKill();
1684*0a6a1f1dSLionel Sambuc
1685*0a6a1f1dSLionel Sambuc LPR.stepBackward(MI);
1686*0a6a1f1dSLionel Sambuc }
1687f4a2713aSLionel Sambuc }
1688