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