blob: c8a3ab3082ad895bcd534ec69da122cfe13e969f [file] [log] [blame]
Misha Brukmancf7d3af2004-07-26 18:45:48 +00001//===-- X86FloatingPoint.cpp - Floating point Reg -> Stack converter ------===//
Misha Brukmanc88330a2005-04-21 23:38:14 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanc88330a2005-04-21 23:38:14 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnercf53bcf2003-01-13 01:01:59 +00009//
10// This file defines the pass which converts floating point instructions from
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +000011// pseudo registers into register stack instructions. This pass uses live
Chris Lattner11290272004-01-30 22:25:18 +000012// variable information to indicate where the FPn registers are used and their
13// lifetimes.
14//
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +000015// The x87 hardware tracks liveness of the stack registers, so it is necessary
16// to implement exact liveness tracking between basic blocks. The CFG edges are
17// partitioned into bundles where the same FP registers must be live in
18// identical stack positions. Instructions are inserted at the end of each basic
19// block to rearrange the live registers to match the outgoing bundle.
Chris Lattner11290272004-01-30 22:25:18 +000020//
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +000021// This approach avoids splitting critical edges at the potential cost of more
22// live register shuffling instructions when critical edges are present.
Chris Lattnercf53bcf2003-01-13 01:01:59 +000023//
24//===----------------------------------------------------------------------===//
25
26#include "X86.h"
27#include "X86InstrInfo.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000028#include "llvm/ADT/DepthFirstIterator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/ADT/STLExtras.h"
Owen Anderson1b351d42008-08-14 21:01:00 +000030#include "llvm/ADT/SmallPtrSet.h"
Evan Chengbbbcac32006-11-15 20:56:39 +000031#include "llvm/ADT/SmallVector.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000032#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000033#include "llvm/CodeGen/EdgeBundles.h"
Bill Wendling6eecd562009-08-03 00:11:34 +000034#include "llvm/CodeGen/MachineFunctionPass.h"
35#include "llvm/CodeGen/MachineInstrBuilder.h"
36#include "llvm/CodeGen/MachineRegisterInfo.h"
37#include "llvm/CodeGen/Passes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000038#include "llvm/IR/InlineAsm.h"
Bill Wendling6eecd562009-08-03 00:11:34 +000039#include "llvm/Support/Debug.h"
40#include "llvm/Support/ErrorHandling.h"
41#include "llvm/Support/raw_ostream.h"
42#include "llvm/Target/TargetInstrInfo.h"
43#include "llvm/Target/TargetMachine.h"
Chris Lattnercf53bcf2003-01-13 01:01:59 +000044#include <algorithm>
Chris Lattnerd46cd682003-12-20 09:58:55 +000045using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000046
Chandler Carruth84e68b22014-04-22 02:41:26 +000047#define DEBUG_TYPE "x86-codegen"
48
Chris Lattner1ef9cd42006-12-19 22:59:26 +000049STATISTIC(NumFXCH, "Number of fxch instructions inserted");
50STATISTIC(NumFP , "Number of floating point instructions");
Chris Lattnercf53bcf2003-01-13 01:01:59 +000051
Chris Lattner1ef9cd42006-12-19 22:59:26 +000052namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000053 struct FPS : public MachineFunctionPass {
Devang Patel8c78a0b2007-05-03 01:11:54 +000054 static char ID;
Owen Andersona7aed182010-08-06 18:33:48 +000055 FPS() : MachineFunctionPass(ID) {
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000056 initializeEdgeBundlesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen8d511492010-07-16 22:00:33 +000057 // This is really only to keep valgrind quiet.
58 // The logic in isLive() is too much for it.
59 memset(Stack, 0, sizeof(Stack));
60 memset(RegMap, 0, sizeof(RegMap));
61 }
Devang Patel09f162c2007-05-01 21:15:47 +000062
Craig Topper2d9361e2014-03-09 07:44:38 +000063 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman6735e102009-08-01 00:26:16 +000064 AU.setPreservesCFG();
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +000065 AU.addRequired<EdgeBundles>();
Evan Cheng962c2cf2008-09-22 22:21:38 +000066 AU.addPreservedID(MachineLoopInfoID);
67 AU.addPreservedID(MachineDominatorsID);
Evan Cheng168f8f32008-09-22 20:58:04 +000068 MachineFunctionPass::getAnalysisUsage(AU);
69 }
70
Craig Topper2d9361e2014-03-09 07:44:38 +000071 bool runOnMachineFunction(MachineFunction &MF) override;
Chris Lattnercf53bcf2003-01-13 01:01:59 +000072
Craig Topper2d9361e2014-03-09 07:44:38 +000073 const char *getPassName() const override { return "X86 FP Stackifier"; }
Chris Lattnercf53bcf2003-01-13 01:01:59 +000074
Chris Lattnercf53bcf2003-01-13 01:01:59 +000075 private:
Evan Cheng845bd6e2006-12-01 10:11:51 +000076 const TargetInstrInfo *TII; // Machine instruction info.
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +000077
78 // Two CFG edges are related if they leave the same block, or enter the same
79 // block. The transitive closure of an edge under this relation is a
80 // LiveBundle. It represents a set of CFG edges where the live FP stack
81 // registers must be allocated identically in the x87 stack.
82 //
83 // A LiveBundle is usually all the edges leaving a block, or all the edges
84 // entering a block, but it can contain more edges if critical edges are
85 // present.
86 //
87 // The set of live FP registers in a LiveBundle is calculated by bundleCFG,
88 // but the exact mapping of FP registers to stack slots is fixed later.
89 struct LiveBundle {
90 // Bit mask of live FP registers. Bit 0 = FP0, bit 1 = FP1, &c.
91 unsigned Mask;
92
93 // Number of pre-assigned live registers in FixStack. This is 0 when the
94 // stack order has not yet been fixed.
95 unsigned FixCount;
96
97 // Assigned stack order for live-in registers.
98 // FixStack[i] == getStackEntry(i) for all i < FixCount.
99 unsigned char FixStack[8];
100
Jakob Stoklund Olesen01d4d862011-01-04 21:10:11 +0000101 LiveBundle() : Mask(0), FixCount(0) {}
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000102
103 // Have the live registers been assigned a stack order yet?
104 bool isFixed() const { return !Mask || FixCount; }
105 };
106
107 // Numbered LiveBundle structs. LiveBundles[0] is used for all CFG edges
108 // with no live FP registers.
109 SmallVector<LiveBundle, 8> LiveBundles;
110
Jakob Stoklund Olesen01d4d862011-01-04 21:10:11 +0000111 // The edge bundle analysis provides indices into the LiveBundles vector.
112 EdgeBundles *Bundles;
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000113
114 // Return a bitmask of FP registers in block's live-in list.
Jakub Staszak59deec02012-11-21 00:59:34 +0000115 static unsigned calcLiveInMask(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000116 unsigned Mask = 0;
117 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
118 E = MBB->livein_end(); I != E; ++I) {
Chad Rosieree740c42013-06-28 18:57:01 +0000119 unsigned Reg = *I;
120 if (Reg < X86::FP0 || Reg > X86::FP6)
121 continue;
122 Mask |= 1 << (Reg - X86::FP0);
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000123 }
124 return Mask;
125 }
126
127 // Partition all the CFG edges into LiveBundles.
128 void bundleCFG(MachineFunction &MF);
129
Evan Cheng845bd6e2006-12-01 10:11:51 +0000130 MachineBasicBlock *MBB; // Current basic block
Jakob Stoklund Olesenff653a22011-06-27 04:08:36 +0000131
132 // The hardware keeps track of how many FP registers are live, so we have
133 // to model that exactly. Usually, each live register corresponds to an
134 // FP<n> register, but when dealing with calls, returns, and inline
Benjamin Kramerbde91762012-06-02 10:20:22 +0000135 // assembly, it is sometimes necessary to have live scratch registers.
Evan Cheng845bd6e2006-12-01 10:11:51 +0000136 unsigned Stack[8]; // FP<n> Registers in each stack slot...
Evan Cheng845bd6e2006-12-01 10:11:51 +0000137 unsigned StackTop; // The current top of the FP stack.
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000138
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +0000139 enum {
140 NumFPRegs = 16 // Including scratch pseudo-registers.
141 };
142
Jakob Stoklund Olesenff653a22011-06-27 04:08:36 +0000143 // For each live FP<n> register, point to its Stack[] entry.
144 // The first entries correspond to FP0-FP6, the rest are scratch registers
145 // used when we need slightly different live registers than what the
146 // register allocator thinks.
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +0000147 unsigned RegMap[NumFPRegs];
148
149 // Pending fixed registers - Inline assembly needs FP registers to appear
150 // in fixed stack slot positions. This is handled by copying FP registers
151 // to ST registers before the instruction, and copying back after the
152 // instruction.
153 //
154 // This is modeled with pending ST registers. NumPendingSTs is the number
155 // of ST registers (ST0-STn) we are tracking. PendingST[n] points to an FP
156 // register that holds the ST value. The ST registers are not moved into
157 // place until immediately before the instruction that needs them.
158 //
159 // It can happen that we need an ST register to be live when no FP register
160 // holds the value:
161 //
162 // %ST0 = COPY %FP4<kill>
163 //
164 // When that happens, we allocate a scratch FP register to hold the ST
165 // value. That means every register in PendingST must be live.
166
167 unsigned NumPendingSTs;
168 unsigned char PendingST[8];
Jakob Stoklund Olesenff653a22011-06-27 04:08:36 +0000169
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000170 // Set up our stack model to match the incoming registers to MBB.
171 void setupBlockStack();
172
173 // Shuffle live registers to match the expectations of successor blocks.
174 void finishBlockStack();
175
Manman Ren19f49ac2012-09-11 22:23:19 +0000176#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000177 void dumpStack() const {
David Greened85fd002010-01-05 01:29:34 +0000178 dbgs() << "Stack contents:";
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000179 for (unsigned i = 0; i != StackTop; ++i) {
David Greened85fd002010-01-05 01:29:34 +0000180 dbgs() << " FP" << Stack[i];
Misha Brukmanc88330a2005-04-21 23:38:14 +0000181 assert(RegMap[Stack[i]] == i && "Stack[] doesn't match RegMap[]!");
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000182 }
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +0000183 for (unsigned i = 0; i != NumPendingSTs; ++i)
184 dbgs() << ", ST" << i << " in FP" << unsigned(PendingST[i]);
David Greened85fd002010-01-05 01:29:34 +0000185 dbgs() << "\n";
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000186 }
Manman Ren742534c2012-09-06 19:06:06 +0000187#endif
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000188
Chris Lattner8f440bb2010-07-17 17:40:51 +0000189 /// getSlot - Return the stack slot number a particular register number is
190 /// in.
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000191 unsigned getSlot(unsigned RegNo) const {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +0000192 assert(RegNo < NumFPRegs && "Regno out of range!");
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000193 return RegMap[RegNo];
194 }
195
Chris Lattner8f440bb2010-07-17 17:40:51 +0000196 /// isLive - Is RegNo currently live in the stack?
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000197 bool isLive(unsigned RegNo) const {
198 unsigned Slot = getSlot(RegNo);
199 return Slot < StackTop && Stack[Slot] == RegNo;
200 }
201
Chris Lattner8f440bb2010-07-17 17:40:51 +0000202 /// getScratchReg - Return an FP register that is not currently in use.
Jakub Staszake2edeac2012-11-20 23:32:32 +0000203 unsigned getScratchReg() const {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +0000204 for (int i = NumFPRegs - 1; i >= 8; --i)
Jakob Stoklund Olesenf0af2362010-07-16 17:41:40 +0000205 if (!isLive(i))
206 return i;
207 llvm_unreachable("Ran out of scratch FP registers");
208 }
209
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +0000210 /// isScratchReg - Returns trus if RegNo is a scratch FP register.
Jakub Staszak6f58ce12012-11-21 00:50:57 +0000211 static bool isScratchReg(unsigned RegNo) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +0000212 return RegNo > 8 && RegNo < NumFPRegs;
213 }
214
Chris Lattner8f440bb2010-07-17 17:40:51 +0000215 /// getStackEntry - Return the X86::FP<n> register in register ST(i).
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000216 unsigned getStackEntry(unsigned STi) const {
Evan Chengd565b442010-10-12 23:19:28 +0000217 if (STi >= StackTop)
218 report_fatal_error("Access past stack top!");
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000219 return Stack[StackTop-1-STi];
220 }
221
Chris Lattner8f440bb2010-07-17 17:40:51 +0000222 /// getSTReg - Return the X86::ST(i) register which contains the specified
223 /// FP<RegNo> register.
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000224 unsigned getSTReg(unsigned RegNo) const {
Craig Topperf6e7e122012-03-27 07:21:54 +0000225 return StackTop - 1 - getSlot(RegNo) + X86::ST0;
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000226 }
227
Chris Lattner1bd44362008-03-11 03:23:40 +0000228 // pushReg - Push the specified FP<n> register onto the stack.
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000229 void pushReg(unsigned Reg) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +0000230 assert(Reg < NumFPRegs && "Register number out of range!");
Evan Chengd565b442010-10-12 23:19:28 +0000231 if (StackTop >= 8)
232 report_fatal_error("Stack overflow!");
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000233 Stack[StackTop] = Reg;
234 RegMap[Reg] = StackTop++;
235 }
236
237 bool isAtTop(unsigned RegNo) const { return getSlot(RegNo) == StackTop-1; }
Chris Lattner1bd44362008-03-11 03:23:40 +0000238 void moveToTop(unsigned RegNo, MachineBasicBlock::iterator I) {
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000239 DebugLoc dl = I == MBB->end() ? DebugLoc() : I->getDebugLoc();
Chris Lattner1bd44362008-03-11 03:23:40 +0000240 if (isAtTop(RegNo)) return;
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000241
Chris Lattner1bd44362008-03-11 03:23:40 +0000242 unsigned STReg = getSTReg(RegNo);
243 unsigned RegOnTop = getStackEntry(0);
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000244
Chris Lattner1bd44362008-03-11 03:23:40 +0000245 // Swap the slots the regs are in.
246 std::swap(RegMap[RegNo], RegMap[RegOnTop]);
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000247
Chris Lattner1bd44362008-03-11 03:23:40 +0000248 // Swap stack slot contents.
Evan Chengd565b442010-10-12 23:19:28 +0000249 if (RegMap[RegOnTop] >= StackTop)
250 report_fatal_error("Access past stack top!");
Chris Lattner1bd44362008-03-11 03:23:40 +0000251 std::swap(Stack[RegMap[RegOnTop]], Stack[StackTop-1]);
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000252
Chris Lattner1bd44362008-03-11 03:23:40 +0000253 // Emit an fxch to update the runtime processors version of the state.
Dale Johannesen9bba9022009-02-13 02:33:27 +0000254 BuildMI(*MBB, I, dl, TII->get(X86::XCH_F)).addReg(STReg);
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000255 ++NumFXCH;
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000256 }
257
Chris Lattnerbc7e35b2004-04-01 04:06:09 +0000258 void duplicateToTop(unsigned RegNo, unsigned AsReg, MachineInstr *I) {
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000259 DebugLoc dl = I == MBB->end() ? DebugLoc() : I->getDebugLoc();
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000260 unsigned STReg = getSTReg(RegNo);
261 pushReg(AsReg); // New register on top of stack
262
Dale Johannesen9bba9022009-02-13 02:33:27 +0000263 BuildMI(*MBB, I, dl, TII->get(X86::LD_Frr)).addReg(STReg);
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000264 }
265
Jakob Stoklund Olesen4f0ace52011-08-08 17:15:43 +0000266 /// duplicatePendingSTBeforeKill - The instruction at I is about to kill
267 /// RegNo. If any PendingST registers still need the RegNo value, duplicate
268 /// them to new scratch registers.
269 void duplicatePendingSTBeforeKill(unsigned RegNo, MachineInstr *I) {
270 for (unsigned i = 0; i != NumPendingSTs; ++i) {
271 if (PendingST[i] != RegNo)
272 continue;
273 unsigned SR = getScratchReg();
274 DEBUG(dbgs() << "Duplicating pending ST" << i
275 << " in FP" << RegNo << " to FP" << SR << '\n');
276 duplicateToTop(RegNo, SR, I);
277 PendingST[i] = SR;
278 }
279 }
280
Chris Lattner8f440bb2010-07-17 17:40:51 +0000281 /// popStackAfter - Pop the current value off of the top of the FP stack
282 /// after the specified instruction.
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000283 void popStackAfter(MachineBasicBlock::iterator &I);
284
Chris Lattner8f440bb2010-07-17 17:40:51 +0000285 /// freeStackSlotAfter - Free the specified register from the register
286 /// stack, so that it is no longer in a register. If the register is
287 /// currently at the top of the stack, we just pop the current instruction,
288 /// otherwise we store the current top-of-stack into the specified slot,
289 /// then pop the top of stack.
Chris Lattnerbc7e35b2004-04-01 04:06:09 +0000290 void freeStackSlotAfter(MachineBasicBlock::iterator &I, unsigned Reg);
291
Chris Lattner8f440bb2010-07-17 17:40:51 +0000292 /// freeStackSlotBefore - Just the pop, no folding. Return the inserted
293 /// instruction.
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000294 MachineBasicBlock::iterator
295 freeStackSlotBefore(MachineBasicBlock::iterator I, unsigned FPRegNo);
296
Chris Lattner8f440bb2010-07-17 17:40:51 +0000297 /// Adjust the live registers to be the set in Mask.
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000298 void adjustLiveRegs(unsigned Mask, MachineBasicBlock::iterator I);
299
Jakob Stoklund Olesenff653a22011-06-27 04:08:36 +0000300 /// Shuffle the top FixCount stack entries such that FP reg FixStack[0] is
Chris Lattner8f440bb2010-07-17 17:40:51 +0000301 /// st(0), FP reg FixStack[1] is st(1) etc.
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000302 void shuffleStackTop(const unsigned char *FixStack, unsigned FixCount,
303 MachineBasicBlock::iterator I);
304
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000305 bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB);
306
307 void handleZeroArgFP(MachineBasicBlock::iterator &I);
308 void handleOneArgFP(MachineBasicBlock::iterator &I);
Chris Lattner7af8ad62004-02-02 19:23:15 +0000309 void handleOneArgFPRW(MachineBasicBlock::iterator &I);
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000310 void handleTwoArgFP(MachineBasicBlock::iterator &I);
Chris Lattner94ff2c32004-06-11 04:25:06 +0000311 void handleCompareFP(MachineBasicBlock::iterator &I);
Chris Lattnerc07c9582004-03-31 22:02:36 +0000312 void handleCondMovFP(MachineBasicBlock::iterator &I);
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000313 void handleSpecialFP(MachineBasicBlock::iterator &I);
Jakob Stoklund Olesen63a622b2010-07-08 19:46:30 +0000314
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +0000315 // Check if a COPY instruction is using FP registers.
Jakub Staszak6f58ce12012-11-21 00:50:57 +0000316 static bool isFPCopy(MachineInstr *MI) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +0000317 unsigned DstReg = MI->getOperand(0).getReg();
318 unsigned SrcReg = MI->getOperand(1).getReg();
319
320 return X86::RFP80RegClass.contains(DstReg) ||
321 X86::RFP80RegClass.contains(SrcReg);
322 }
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000323 };
Devang Patel8c78a0b2007-05-03 01:11:54 +0000324 char FPS::ID = 0;
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000325}
326
Chris Lattnerd46cd682003-12-20 09:58:55 +0000327FunctionPass *llvm::createX86FloatingPointStackifierPass() { return new FPS(); }
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000328
Chris Lattner3c43efc2008-01-14 06:41:29 +0000329/// getFPReg - Return the X86::FPx register number for the specified operand.
330/// For example, this returns 3 for X86::FP3.
331static unsigned getFPReg(const MachineOperand &MO) {
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000332 assert(MO.isReg() && "Expected an FP register!");
Chris Lattner3c43efc2008-01-14 06:41:29 +0000333 unsigned Reg = MO.getReg();
334 assert(Reg >= X86::FP0 && Reg <= X86::FP6 && "Expected FP register!");
335 return Reg - X86::FP0;
336}
337
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000338/// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
339/// register references into FP stack references.
340///
341bool FPS::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnerdebae1e2005-01-23 23:13:59 +0000342 // We only need to run this pass if there are any FP registers used in this
343 // function. If it is all integer, there is nothing for us to do!
Chris Lattnerdebae1e2005-01-23 23:13:59 +0000344 bool FPIsUsed = false;
345
346 assert(X86::FP6 == X86::FP0+6 && "Register enums aren't sorted right!");
347 for (unsigned i = 0; i <= 6; ++i)
Chris Lattnera10fff52007-12-31 04:13:23 +0000348 if (MF.getRegInfo().isPhysRegUsed(X86::FP0+i)) {
Chris Lattnerdebae1e2005-01-23 23:13:59 +0000349 FPIsUsed = true;
350 break;
351 }
352
353 // Early exit.
354 if (!FPIsUsed) return false;
355
Jakob Stoklund Olesen01d4d862011-01-04 21:10:11 +0000356 Bundles = &getAnalysis<EdgeBundles>();
Evan Cheng845bd6e2006-12-01 10:11:51 +0000357 TII = MF.getTarget().getInstrInfo();
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000358
359 // Prepare cross-MBB liveness.
360 bundleCFG(MF);
361
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000362 StackTop = 0;
363
Chris Lattner11290272004-01-30 22:25:18 +0000364 // Process the function in depth first order so that we process at least one
365 // of the predecessors for every reachable block in the function.
Owen Anderson1b351d42008-08-14 21:01:00 +0000366 SmallPtrSet<MachineBasicBlock*, 8> Processed;
Chris Lattneracbf0c82004-05-01 21:27:53 +0000367 MachineBasicBlock *Entry = MF.begin();
Chris Lattner11290272004-01-30 22:25:18 +0000368
369 bool Changed = false;
Owen Anderson1b351d42008-08-14 21:01:00 +0000370 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 8> >
Chris Lattner11290272004-01-30 22:25:18 +0000371 I = df_ext_begin(Entry, Processed), E = df_ext_end(Entry, Processed);
372 I != E; ++I)
Chris Lattneracbf0c82004-05-01 21:27:53 +0000373 Changed |= processBasicBlock(MF, **I);
Chris Lattner11290272004-01-30 22:25:18 +0000374
Chris Lattnerb2fcd072009-09-08 04:55:44 +0000375 // Process any unreachable blocks in arbitrary order now.
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000376 if (MF.size() != Processed.size())
377 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
378 if (Processed.insert(BB))
379 Changed |= processBasicBlock(MF, *BB);
Chris Lattnerb2fcd072009-09-08 04:55:44 +0000380
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000381 LiveBundles.clear();
382
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000383 return Changed;
384}
385
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000386/// bundleCFG - Scan all the basic blocks to determine consistent live-in and
387/// live-out sets for the FP registers. Consistent means that the set of
388/// registers live-out from a block is identical to the live-in set of all
389/// successors. This is not enforced by the normal live-in lists since
390/// registers may be implicitly defined, or not used by all successors.
391void FPS::bundleCFG(MachineFunction &MF) {
392 assert(LiveBundles.empty() && "Stale data in LiveBundles");
Jakob Stoklund Olesen01d4d862011-01-04 21:10:11 +0000393 LiveBundles.resize(Bundles->getNumBundles());
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000394
Jakob Stoklund Olesen01d4d862011-01-04 21:10:11 +0000395 // Gather the actual live-in masks for all MBBs.
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000396 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
397 MachineBasicBlock *MBB = I;
398 const unsigned Mask = calcLiveInMask(MBB);
399 if (!Mask)
400 continue;
Jakob Stoklund Olesen01d4d862011-01-04 21:10:11 +0000401 // Update MBB ingoing bundle mask.
402 LiveBundles[Bundles->getBundle(MBB->getNumber(), false)].Mask |= Mask;
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000403 }
404}
405
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000406/// processBasicBlock - Loop over all of the instructions in the basic block,
407/// transforming FP instructions into their stack form.
408///
409bool FPS::processBasicBlock(MachineFunction &MF, MachineBasicBlock &BB) {
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000410 bool Changed = false;
411 MBB = &BB;
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +0000412 NumPendingSTs = 0;
Misha Brukmanc88330a2005-04-21 23:38:14 +0000413
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000414 setupBlockStack();
415
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000416 for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I) {
Alkis Evlogimenos80da8652004-02-12 02:27:10 +0000417 MachineInstr *MI = I;
Bruno Cardoso Lopesc2f87b72010-06-08 22:51:23 +0000418 uint64_t Flags = MI->getDesc().TSFlags;
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000419
Chris Lattner8abed802008-03-11 19:50:13 +0000420 unsigned FPInstClass = Flags & X86II::FPTypeMask;
Chris Lattnerb06015a2010-02-09 19:54:29 +0000421 if (MI->isInlineAsm())
Chris Lattner8abed802008-03-11 19:50:13 +0000422 FPInstClass = X86II::SpecialFP;
Jakob Stoklund Olesen63a622b2010-07-08 19:46:30 +0000423
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +0000424 if (MI->isCopy() && isFPCopy(MI))
Jakob Stoklund Olesen63a622b2010-07-08 19:46:30 +0000425 FPInstClass = X86II::SpecialFP;
426
Jakob Stoklund Olesenda618422011-08-03 16:33:19 +0000427 if (MI->isImplicitDef() &&
428 X86::RFP80RegClass.contains(MI->getOperand(0).getReg()))
429 FPInstClass = X86II::SpecialFP;
430
Chris Lattner8abed802008-03-11 19:50:13 +0000431 if (FPInstClass == X86II::NotFP)
Chris Lattner11290272004-01-30 22:25:18 +0000432 continue; // Efficiently ignore non-fp insts!
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000433
Craig Topper062a2ba2014-04-25 05:30:21 +0000434 MachineInstr *PrevMI = nullptr;
Alkis Evlogimenos5a922402004-02-14 01:18:34 +0000435 if (I != BB.begin())
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000436 PrevMI = std::prev(I);
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000437
438 ++NumFP; // Keep track of # of pseudo instrs
David Greened85fd002010-01-05 01:29:34 +0000439 DEBUG(dbgs() << "\nFPInst:\t" << *MI);
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000440
441 // Get dead variables list now because the MI pointer may be deleted as part
442 // of processing!
Evan Chengbbbcac32006-11-15 20:56:39 +0000443 SmallVector<unsigned, 8> DeadRegs;
444 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
445 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000446 if (MO.isReg() && MO.isDead())
Evan Chengbbbcac32006-11-15 20:56:39 +0000447 DeadRegs.push_back(MO.getReg());
448 }
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000449
Chris Lattner8abed802008-03-11 19:50:13 +0000450 switch (FPInstClass) {
Chris Lattner7af8ad62004-02-02 19:23:15 +0000451 case X86II::ZeroArgFP: handleZeroArgFP(I); break;
Chris Lattnerc07c9582004-03-31 22:02:36 +0000452 case X86II::OneArgFP: handleOneArgFP(I); break; // fstp ST(0)
Chris Lattner7af8ad62004-02-02 19:23:15 +0000453 case X86II::OneArgFPRW: handleOneArgFPRW(I); break; // ST(0) = fsqrt(ST(0))
Evan Chengdb04c952006-11-11 10:21:44 +0000454 case X86II::TwoArgFP: handleTwoArgFP(I); break;
Chris Lattner0876edf2004-06-11 04:41:24 +0000455 case X86II::CompareFP: handleCompareFP(I); break;
Chris Lattnerc07c9582004-03-31 22:02:36 +0000456 case X86II::CondMovFP: handleCondMovFP(I); break;
Chris Lattner7af8ad62004-02-02 19:23:15 +0000457 case X86II::SpecialFP: handleSpecialFP(I); break;
Torok Edwinfbcc6632009-07-14 16:55:14 +0000458 default: llvm_unreachable("Unknown FP Type!");
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000459 }
460
461 // Check to see if any of the values defined by this instruction are dead
462 // after definition. If so, pop them.
Evan Chengbbbcac32006-11-15 20:56:39 +0000463 for (unsigned i = 0, e = DeadRegs.size(); i != e; ++i) {
464 unsigned Reg = DeadRegs[i];
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000465 if (Reg >= X86::FP0 && Reg <= X86::FP6) {
David Greened85fd002010-01-05 01:29:34 +0000466 DEBUG(dbgs() << "Register FP#" << Reg-X86::FP0 << " is dead!\n");
Chris Lattner94ff2c32004-06-11 04:25:06 +0000467 freeStackSlotAfter(I, Reg-X86::FP0);
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000468 }
469 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000470
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000471 // Print out all of the instructions expanded to if -debug
Alkis Evlogimenos636e19d2004-02-15 00:46:41 +0000472 DEBUG(
473 MachineBasicBlock::iterator PrevI(PrevMI);
474 if (I == PrevI) {
David Greened85fd002010-01-05 01:29:34 +0000475 dbgs() << "Just deleted pseudo instruction\n";
Alkis Evlogimenos636e19d2004-02-15 00:46:41 +0000476 } else {
477 MachineBasicBlock::iterator Start = I;
478 // Rewind to first instruction newly inserted.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000479 while (Start != BB.begin() && std::prev(Start) != PrevI) --Start;
David Greened85fd002010-01-05 01:29:34 +0000480 dbgs() << "Inserted instructions:\n\t";
481 Start->print(dbgs(), &MF.getTarget());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000482 while (++Start != std::next(I)) {}
Alkis Evlogimenos636e19d2004-02-15 00:46:41 +0000483 }
484 dumpStack();
485 );
Duncan Sandsa41634e2011-08-12 14:54:45 +0000486 (void)PrevMI;
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000487
488 Changed = true;
489 }
490
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000491 finishBlockStack();
492
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000493 return Changed;
494}
495
Jakob Stoklund Olesen01d4d862011-01-04 21:10:11 +0000496/// setupBlockStack - Use the live bundles to set up our model of the stack
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000497/// to match predecessors' live out stack.
498void FPS::setupBlockStack() {
499 DEBUG(dbgs() << "\nSetting up live-ins for BB#" << MBB->getNumber()
500 << " derived from " << MBB->getName() << ".\n");
501 StackTop = 0;
Jakob Stoklund Olesen01d4d862011-01-04 21:10:11 +0000502 // Get the live-in bundle for MBB.
503 const LiveBundle &Bundle =
504 LiveBundles[Bundles->getBundle(MBB->getNumber(), false)];
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000505
506 if (!Bundle.Mask) {
507 DEBUG(dbgs() << "Block has no FP live-ins.\n");
508 return;
509 }
510
511 // Depth-first iteration should ensure that we always have an assigned stack.
512 assert(Bundle.isFixed() && "Reached block before any predecessors");
513
514 // Push the fixed live-in registers.
515 for (unsigned i = Bundle.FixCount; i > 0; --i) {
516 MBB->addLiveIn(X86::ST0+i-1);
517 DEBUG(dbgs() << "Live-in st(" << (i-1) << "): %FP"
518 << unsigned(Bundle.FixStack[i-1]) << '\n');
519 pushReg(Bundle.FixStack[i-1]);
520 }
521
522 // Kill off unwanted live-ins. This can happen with a critical edge.
523 // FIXME: We could keep these live registers around as zombies. They may need
524 // to be revived at the end of a short block. It might save a few instrs.
525 adjustLiveRegs(calcLiveInMask(MBB), MBB->begin());
526 DEBUG(MBB->dump());
527}
528
529/// finishBlockStack - Revive live-outs that are implicitly defined out of
530/// MBB. Shuffle live registers to match the expected fixed stack of any
531/// predecessors, and ensure that all predecessors are expecting the same
532/// stack.
533void FPS::finishBlockStack() {
534 // The RET handling below takes care of return blocks for us.
535 if (MBB->succ_empty())
536 return;
537
538 DEBUG(dbgs() << "Setting up live-outs for BB#" << MBB->getNumber()
539 << " derived from " << MBB->getName() << ".\n");
540
Jakob Stoklund Olesen01d4d862011-01-04 21:10:11 +0000541 // Get MBB's live-out bundle.
542 unsigned BundleIdx = Bundles->getBundle(MBB->getNumber(), true);
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000543 LiveBundle &Bundle = LiveBundles[BundleIdx];
544
545 // We may need to kill and define some registers to match successors.
546 // FIXME: This can probably be combined with the shuffle below.
547 MachineBasicBlock::iterator Term = MBB->getFirstTerminator();
548 adjustLiveRegs(Bundle.Mask, Term);
549
550 if (!Bundle.Mask) {
551 DEBUG(dbgs() << "No live-outs.\n");
552 return;
553 }
554
555 // Has the stack order been fixed yet?
556 DEBUG(dbgs() << "LB#" << BundleIdx << ": ");
557 if (Bundle.isFixed()) {
558 DEBUG(dbgs() << "Shuffling stack to match.\n");
559 shuffleStackTop(Bundle.FixStack, Bundle.FixCount, Term);
560 } else {
561 // Not fixed yet, we get to choose.
562 DEBUG(dbgs() << "Fixing stack order now.\n");
563 Bundle.FixCount = StackTop;
564 for (unsigned i = 0; i < StackTop; ++i)
565 Bundle.FixStack[i] = getStackEntry(i);
566 }
567}
568
569
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000570//===----------------------------------------------------------------------===//
571// Efficient Lookup Table Support
572//===----------------------------------------------------------------------===//
573
Chris Lattnerd46cd682003-12-20 09:58:55 +0000574namespace {
575 struct TableEntry {
Craig Topper2dac9622012-03-09 07:45:21 +0000576 uint16_t from;
577 uint16_t to;
Chris Lattnerd46cd682003-12-20 09:58:55 +0000578 bool operator<(const TableEntry &TE) const { return from < TE.from; }
Jeff Cohen15a8c152006-01-26 20:41:32 +0000579 friend bool operator<(const TableEntry &TE, unsigned V) {
580 return TE.from < V;
581 }
Benjamin Kramer0d874f72012-09-17 16:46:22 +0000582 friend bool LLVM_ATTRIBUTE_UNUSED operator<(unsigned V,
583 const TableEntry &TE) {
Jakob Stoklund Olesen2cd00732010-08-16 18:24:54 +0000584 return V < TE.from;
585 }
Chris Lattnerd46cd682003-12-20 09:58:55 +0000586 };
587}
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000588
Evan Chengfa374ca2008-07-21 20:02:45 +0000589#ifndef NDEBUG
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000590static bool TableIsSorted(const TableEntry *Table, unsigned NumEntries) {
591 for (unsigned i = 0; i != NumEntries-1; ++i)
592 if (!(Table[i] < Table[i+1])) return false;
593 return true;
594}
Evan Chengfa374ca2008-07-21 20:02:45 +0000595#endif
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000596
597static int Lookup(const TableEntry *Table, unsigned N, unsigned Opcode) {
598 const TableEntry *I = std::lower_bound(Table, Table+N, Opcode);
599 if (I != Table+N && I->from == Opcode)
600 return I->to;
601 return -1;
602}
603
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000604#ifdef NDEBUG
605#define ASSERT_SORTED(TABLE)
606#else
607#define ASSERT_SORTED(TABLE) \
608 { static bool TABLE##Checked = false; \
Jim Laskey181fb1c2006-07-19 19:33:08 +0000609 if (!TABLE##Checked) { \
Owen Andersone2f23a32007-09-07 04:06:50 +0000610 assert(TableIsSorted(TABLE, array_lengthof(TABLE)) && \
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000611 "All lookup tables must be sorted for efficient access!"); \
Jim Laskey181fb1c2006-07-19 19:33:08 +0000612 TABLE##Checked = true; \
613 } \
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000614 }
615#endif
616
Chris Lattnerf431ad42005-12-21 07:47:04 +0000617//===----------------------------------------------------------------------===//
618// Register File -> Register Stack Mapping Methods
619//===----------------------------------------------------------------------===//
620
621// OpcodeTable - Sorted map of register instructions to their stack version.
622// The first element is an register file pseudo instruction, the second is the
623// concrete X86 instruction which uses the register stack.
624//
625static const TableEntry OpcodeTable[] = {
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000626 { X86::ABS_Fp32 , X86::ABS_F },
627 { X86::ABS_Fp64 , X86::ABS_F },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000628 { X86::ABS_Fp80 , X86::ABS_F },
Dale Johannesen68471d22007-07-10 21:53:30 +0000629 { X86::ADD_Fp32m , X86::ADD_F32m },
630 { X86::ADD_Fp64m , X86::ADD_F64m },
631 { X86::ADD_Fp64m32 , X86::ADD_F32m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000632 { X86::ADD_Fp80m32 , X86::ADD_F32m },
633 { X86::ADD_Fp80m64 , X86::ADD_F64m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000634 { X86::ADD_FpI16m32 , X86::ADD_FI16m },
635 { X86::ADD_FpI16m64 , X86::ADD_FI16m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000636 { X86::ADD_FpI16m80 , X86::ADD_FI16m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000637 { X86::ADD_FpI32m32 , X86::ADD_FI32m },
638 { X86::ADD_FpI32m64 , X86::ADD_FI32m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000639 { X86::ADD_FpI32m80 , X86::ADD_FI32m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000640 { X86::CHS_Fp32 , X86::CHS_F },
641 { X86::CHS_Fp64 , X86::CHS_F },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000642 { X86::CHS_Fp80 , X86::CHS_F },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000643 { X86::CMOVBE_Fp32 , X86::CMOVBE_F },
644 { X86::CMOVBE_Fp64 , X86::CMOVBE_F },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000645 { X86::CMOVBE_Fp80 , X86::CMOVBE_F },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000646 { X86::CMOVB_Fp32 , X86::CMOVB_F },
647 { X86::CMOVB_Fp64 , X86::CMOVB_F },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000648 { X86::CMOVB_Fp80 , X86::CMOVB_F },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000649 { X86::CMOVE_Fp32 , X86::CMOVE_F },
650 { X86::CMOVE_Fp64 , X86::CMOVE_F },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000651 { X86::CMOVE_Fp80 , X86::CMOVE_F },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000652 { X86::CMOVNBE_Fp32 , X86::CMOVNBE_F },
653 { X86::CMOVNBE_Fp64 , X86::CMOVNBE_F },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000654 { X86::CMOVNBE_Fp80 , X86::CMOVNBE_F },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000655 { X86::CMOVNB_Fp32 , X86::CMOVNB_F },
656 { X86::CMOVNB_Fp64 , X86::CMOVNB_F },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000657 { X86::CMOVNB_Fp80 , X86::CMOVNB_F },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000658 { X86::CMOVNE_Fp32 , X86::CMOVNE_F },
659 { X86::CMOVNE_Fp64 , X86::CMOVNE_F },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000660 { X86::CMOVNE_Fp80 , X86::CMOVNE_F },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000661 { X86::CMOVNP_Fp32 , X86::CMOVNP_F },
662 { X86::CMOVNP_Fp64 , X86::CMOVNP_F },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000663 { X86::CMOVNP_Fp80 , X86::CMOVNP_F },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000664 { X86::CMOVP_Fp32 , X86::CMOVP_F },
665 { X86::CMOVP_Fp64 , X86::CMOVP_F },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000666 { X86::CMOVP_Fp80 , X86::CMOVP_F },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000667 { X86::COS_Fp32 , X86::COS_F },
668 { X86::COS_Fp64 , X86::COS_F },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000669 { X86::COS_Fp80 , X86::COS_F },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000670 { X86::DIVR_Fp32m , X86::DIVR_F32m },
671 { X86::DIVR_Fp64m , X86::DIVR_F64m },
Dale Johannesen68471d22007-07-10 21:53:30 +0000672 { X86::DIVR_Fp64m32 , X86::DIVR_F32m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000673 { X86::DIVR_Fp80m32 , X86::DIVR_F32m },
674 { X86::DIVR_Fp80m64 , X86::DIVR_F64m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000675 { X86::DIVR_FpI16m32, X86::DIVR_FI16m},
676 { X86::DIVR_FpI16m64, X86::DIVR_FI16m},
Dale Johannesenb1888e72007-08-05 18:49:15 +0000677 { X86::DIVR_FpI16m80, X86::DIVR_FI16m},
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000678 { X86::DIVR_FpI32m32, X86::DIVR_FI32m},
679 { X86::DIVR_FpI32m64, X86::DIVR_FI32m},
Dale Johannesenb1888e72007-08-05 18:49:15 +0000680 { X86::DIVR_FpI32m80, X86::DIVR_FI32m},
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000681 { X86::DIV_Fp32m , X86::DIV_F32m },
682 { X86::DIV_Fp64m , X86::DIV_F64m },
Dale Johannesen68471d22007-07-10 21:53:30 +0000683 { X86::DIV_Fp64m32 , X86::DIV_F32m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000684 { X86::DIV_Fp80m32 , X86::DIV_F32m },
685 { X86::DIV_Fp80m64 , X86::DIV_F64m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000686 { X86::DIV_FpI16m32 , X86::DIV_FI16m },
687 { X86::DIV_FpI16m64 , X86::DIV_FI16m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000688 { X86::DIV_FpI16m80 , X86::DIV_FI16m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000689 { X86::DIV_FpI32m32 , X86::DIV_FI32m },
690 { X86::DIV_FpI32m64 , X86::DIV_FI32m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000691 { X86::DIV_FpI32m80 , X86::DIV_FI32m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000692 { X86::ILD_Fp16m32 , X86::ILD_F16m },
693 { X86::ILD_Fp16m64 , X86::ILD_F16m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000694 { X86::ILD_Fp16m80 , X86::ILD_F16m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000695 { X86::ILD_Fp32m32 , X86::ILD_F32m },
696 { X86::ILD_Fp32m64 , X86::ILD_F32m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000697 { X86::ILD_Fp32m80 , X86::ILD_F32m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000698 { X86::ILD_Fp64m32 , X86::ILD_F64m },
699 { X86::ILD_Fp64m64 , X86::ILD_F64m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000700 { X86::ILD_Fp64m80 , X86::ILD_F64m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000701 { X86::ISTT_Fp16m32 , X86::ISTT_FP16m},
702 { X86::ISTT_Fp16m64 , X86::ISTT_FP16m},
Dale Johannesen57c6ac5f2007-08-07 01:17:37 +0000703 { X86::ISTT_Fp16m80 , X86::ISTT_FP16m},
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000704 { X86::ISTT_Fp32m32 , X86::ISTT_FP32m},
705 { X86::ISTT_Fp32m64 , X86::ISTT_FP32m},
Dale Johannesen57c6ac5f2007-08-07 01:17:37 +0000706 { X86::ISTT_Fp32m80 , X86::ISTT_FP32m},
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000707 { X86::ISTT_Fp64m32 , X86::ISTT_FP64m},
708 { X86::ISTT_Fp64m64 , X86::ISTT_FP64m},
Dale Johannesen57c6ac5f2007-08-07 01:17:37 +0000709 { X86::ISTT_Fp64m80 , X86::ISTT_FP64m},
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000710 { X86::IST_Fp16m32 , X86::IST_F16m },
711 { X86::IST_Fp16m64 , X86::IST_F16m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000712 { X86::IST_Fp16m80 , X86::IST_F16m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000713 { X86::IST_Fp32m32 , X86::IST_F32m },
714 { X86::IST_Fp32m64 , X86::IST_F32m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000715 { X86::IST_Fp32m80 , X86::IST_F32m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000716 { X86::IST_Fp64m32 , X86::IST_FP64m },
717 { X86::IST_Fp64m64 , X86::IST_FP64m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000718 { X86::IST_Fp64m80 , X86::IST_FP64m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000719 { X86::LD_Fp032 , X86::LD_F0 },
720 { X86::LD_Fp064 , X86::LD_F0 },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000721 { X86::LD_Fp080 , X86::LD_F0 },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000722 { X86::LD_Fp132 , X86::LD_F1 },
723 { X86::LD_Fp164 , X86::LD_F1 },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000724 { X86::LD_Fp180 , X86::LD_F1 },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000725 { X86::LD_Fp32m , X86::LD_F32m },
Dale Johannesena47f7d72007-08-07 20:29:26 +0000726 { X86::LD_Fp32m64 , X86::LD_F32m },
727 { X86::LD_Fp32m80 , X86::LD_F32m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000728 { X86::LD_Fp64m , X86::LD_F64m },
Dale Johannesena47f7d72007-08-07 20:29:26 +0000729 { X86::LD_Fp64m80 , X86::LD_F64m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000730 { X86::LD_Fp80m , X86::LD_F80m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000731 { X86::MUL_Fp32m , X86::MUL_F32m },
732 { X86::MUL_Fp64m , X86::MUL_F64m },
Dale Johannesen68471d22007-07-10 21:53:30 +0000733 { X86::MUL_Fp64m32 , X86::MUL_F32m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000734 { X86::MUL_Fp80m32 , X86::MUL_F32m },
735 { X86::MUL_Fp80m64 , X86::MUL_F64m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000736 { X86::MUL_FpI16m32 , X86::MUL_FI16m },
737 { X86::MUL_FpI16m64 , X86::MUL_FI16m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000738 { X86::MUL_FpI16m80 , X86::MUL_FI16m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000739 { X86::MUL_FpI32m32 , X86::MUL_FI32m },
740 { X86::MUL_FpI32m64 , X86::MUL_FI32m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000741 { X86::MUL_FpI32m80 , X86::MUL_FI32m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000742 { X86::SIN_Fp32 , X86::SIN_F },
743 { X86::SIN_Fp64 , X86::SIN_F },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000744 { X86::SIN_Fp80 , X86::SIN_F },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000745 { X86::SQRT_Fp32 , X86::SQRT_F },
746 { X86::SQRT_Fp64 , X86::SQRT_F },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000747 { X86::SQRT_Fp80 , X86::SQRT_F },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000748 { X86::ST_Fp32m , X86::ST_F32m },
749 { X86::ST_Fp64m , X86::ST_F64m },
750 { X86::ST_Fp64m32 , X86::ST_F32m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000751 { X86::ST_Fp80m32 , X86::ST_F32m },
752 { X86::ST_Fp80m64 , X86::ST_F64m },
753 { X86::ST_FpP80m , X86::ST_FP80m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000754 { X86::SUBR_Fp32m , X86::SUBR_F32m },
755 { X86::SUBR_Fp64m , X86::SUBR_F64m },
Dale Johannesen68471d22007-07-10 21:53:30 +0000756 { X86::SUBR_Fp64m32 , X86::SUBR_F32m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000757 { X86::SUBR_Fp80m32 , X86::SUBR_F32m },
758 { X86::SUBR_Fp80m64 , X86::SUBR_F64m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000759 { X86::SUBR_FpI16m32, X86::SUBR_FI16m},
760 { X86::SUBR_FpI16m64, X86::SUBR_FI16m},
Dale Johannesenb1888e72007-08-05 18:49:15 +0000761 { X86::SUBR_FpI16m80, X86::SUBR_FI16m},
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000762 { X86::SUBR_FpI32m32, X86::SUBR_FI32m},
763 { X86::SUBR_FpI32m64, X86::SUBR_FI32m},
Dale Johannesenb1888e72007-08-05 18:49:15 +0000764 { X86::SUBR_FpI32m80, X86::SUBR_FI32m},
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000765 { X86::SUB_Fp32m , X86::SUB_F32m },
766 { X86::SUB_Fp64m , X86::SUB_F64m },
Dale Johannesen68471d22007-07-10 21:53:30 +0000767 { X86::SUB_Fp64m32 , X86::SUB_F32m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000768 { X86::SUB_Fp80m32 , X86::SUB_F32m },
769 { X86::SUB_Fp80m64 , X86::SUB_F64m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000770 { X86::SUB_FpI16m32 , X86::SUB_FI16m },
771 { X86::SUB_FpI16m64 , X86::SUB_FI16m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000772 { X86::SUB_FpI16m80 , X86::SUB_FI16m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000773 { X86::SUB_FpI32m32 , X86::SUB_FI32m },
774 { X86::SUB_FpI32m64 , X86::SUB_FI32m },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000775 { X86::SUB_FpI32m80 , X86::SUB_FI32m },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000776 { X86::TST_Fp32 , X86::TST_F },
777 { X86::TST_Fp64 , X86::TST_F },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000778 { X86::TST_Fp80 , X86::TST_F },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000779 { X86::UCOM_FpIr32 , X86::UCOM_FIr },
780 { X86::UCOM_FpIr64 , X86::UCOM_FIr },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000781 { X86::UCOM_FpIr80 , X86::UCOM_FIr },
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000782 { X86::UCOM_Fpr32 , X86::UCOM_Fr },
783 { X86::UCOM_Fpr64 , X86::UCOM_Fr },
Dale Johannesenb1888e72007-08-05 18:49:15 +0000784 { X86::UCOM_Fpr80 , X86::UCOM_Fr },
Chris Lattnerf431ad42005-12-21 07:47:04 +0000785};
786
787static unsigned getConcreteOpcode(unsigned Opcode) {
788 ASSERT_SORTED(OpcodeTable);
Owen Andersone2f23a32007-09-07 04:06:50 +0000789 int Opc = Lookup(OpcodeTable, array_lengthof(OpcodeTable), Opcode);
Chris Lattnerf431ad42005-12-21 07:47:04 +0000790 assert(Opc != -1 && "FP Stack instruction not in OpcodeTable!");
791 return Opc;
792}
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000793
794//===----------------------------------------------------------------------===//
795// Helper Methods
796//===----------------------------------------------------------------------===//
797
798// PopTable - Sorted map of instructions to their popping version. The first
799// element is an instruction, the second is the version which pops.
800//
801static const TableEntry PopTable[] = {
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000802 { X86::ADD_FrST0 , X86::ADD_FPrST0 },
Chris Lattner637eebb2003-08-03 21:56:36 +0000803
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000804 { X86::DIVR_FrST0, X86::DIVR_FPrST0 },
805 { X86::DIV_FrST0 , X86::DIV_FPrST0 },
Chris Lattner637eebb2003-08-03 21:56:36 +0000806
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000807 { X86::IST_F16m , X86::IST_FP16m },
808 { X86::IST_F32m , X86::IST_FP32m },
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000809
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000810 { X86::MUL_FrST0 , X86::MUL_FPrST0 },
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000811
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000812 { X86::ST_F32m , X86::ST_FP32m },
813 { X86::ST_F64m , X86::ST_FP64m },
814 { X86::ST_Frr , X86::ST_FPrr },
Chris Lattner637eebb2003-08-03 21:56:36 +0000815
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000816 { X86::SUBR_FrST0, X86::SUBR_FPrST0 },
817 { X86::SUB_FrST0 , X86::SUB_FPrST0 },
Chris Lattner637eebb2003-08-03 21:56:36 +0000818
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000819 { X86::UCOM_FIr , X86::UCOM_FIPr },
Chris Lattnerd1c75452004-04-12 01:39:15 +0000820
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000821 { X86::UCOM_FPr , X86::UCOM_FPPr },
822 { X86::UCOM_Fr , X86::UCOM_FPr },
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000823};
824
825/// popStackAfter - Pop the current value off of the top of the FP stack after
826/// the specified instruction. This attempts to be sneaky and combine the pop
827/// into the instruction itself if possible. The iterator is left pointing to
828/// the last instruction, be it a new pop instruction inserted, or the old
829/// instruction if it was modified in place.
830///
831void FPS::popStackAfter(MachineBasicBlock::iterator &I) {
Dale Johannesen9bba9022009-02-13 02:33:27 +0000832 MachineInstr* MI = I;
833 DebugLoc dl = MI->getDebugLoc();
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000834 ASSERT_SORTED(PopTable);
Evan Chengd565b442010-10-12 23:19:28 +0000835 if (StackTop == 0)
836 report_fatal_error("Cannot pop empty stack!");
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000837 RegMap[Stack[--StackTop]] = ~0; // Update state
838
839 // Check to see if there is a popping version of this instruction...
Owen Andersone2f23a32007-09-07 04:06:50 +0000840 int Opcode = Lookup(PopTable, array_lengthof(PopTable), I->getOpcode());
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000841 if (Opcode != -1) {
Chris Lattner59687512008-01-11 18:10:50 +0000842 I->setDesc(TII->get(Opcode));
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000843 if (Opcode == X86::UCOM_FPPr)
Alkis Evlogimenos80da8652004-02-12 02:27:10 +0000844 I->RemoveOperand(0);
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000845 } else { // Insert an explicit pop
Dale Johannesen9bba9022009-02-13 02:33:27 +0000846 I = BuildMI(*MBB, ++I, dl, TII->get(X86::ST_FPrr)).addReg(X86::ST0);
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000847 }
848}
849
Chris Lattnerbc7e35b2004-04-01 04:06:09 +0000850/// freeStackSlotAfter - Free the specified register from the register stack, so
851/// that it is no longer in a register. If the register is currently at the top
852/// of the stack, we just pop the current instruction, otherwise we store the
853/// current top-of-stack into the specified slot, then pop the top of stack.
854void FPS::freeStackSlotAfter(MachineBasicBlock::iterator &I, unsigned FPRegNo) {
855 if (getStackEntry(0) == FPRegNo) { // already at the top of stack? easy.
856 popStackAfter(I);
857 return;
858 }
859
860 // Otherwise, store the top of stack into the dead slot, killing the operand
861 // without having to add in an explicit xchg then pop.
862 //
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000863 I = freeStackSlotBefore(++I, FPRegNo);
864}
865
866/// freeStackSlotBefore - Free the specified register without trying any
867/// folding.
868MachineBasicBlock::iterator
869FPS::freeStackSlotBefore(MachineBasicBlock::iterator I, unsigned FPRegNo) {
Chris Lattnerbc7e35b2004-04-01 04:06:09 +0000870 unsigned STReg = getSTReg(FPRegNo);
871 unsigned OldSlot = getSlot(FPRegNo);
872 unsigned TopReg = Stack[StackTop-1];
873 Stack[OldSlot] = TopReg;
874 RegMap[TopReg] = OldSlot;
875 RegMap[FPRegNo] = ~0;
876 Stack[--StackTop] = ~0;
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000877 return BuildMI(*MBB, I, DebugLoc(), TII->get(X86::ST_FPrr)).addReg(STReg);
878}
879
880/// adjustLiveRegs - Kill and revive registers such that exactly the FP
881/// registers with a bit in Mask are live.
882void FPS::adjustLiveRegs(unsigned Mask, MachineBasicBlock::iterator I) {
883 unsigned Defs = Mask;
884 unsigned Kills = 0;
885 for (unsigned i = 0; i < StackTop; ++i) {
886 unsigned RegNo = Stack[i];
887 if (!(Defs & (1 << RegNo)))
888 // This register is live, but we don't want it.
889 Kills |= (1 << RegNo);
890 else
891 // We don't need to imp-def this live register.
892 Defs &= ~(1 << RegNo);
893 }
894 assert((Kills & Defs) == 0 && "Register needs killing and def'ing?");
895
896 // Produce implicit-defs for free by using killed registers.
897 while (Kills && Defs) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000898 unsigned KReg = countTrailingZeros(Kills);
899 unsigned DReg = countTrailingZeros(Defs);
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000900 DEBUG(dbgs() << "Renaming %FP" << KReg << " as imp %FP" << DReg << "\n");
901 std::swap(Stack[getSlot(KReg)], Stack[getSlot(DReg)]);
902 std::swap(RegMap[KReg], RegMap[DReg]);
903 Kills &= ~(1 << KReg);
904 Defs &= ~(1 << DReg);
905 }
906
907 // Kill registers by popping.
908 if (Kills && I != MBB->begin()) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000909 MachineBasicBlock::iterator I2 = std::prev(I);
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +0000910 while (StackTop) {
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000911 unsigned KReg = getStackEntry(0);
912 if (!(Kills & (1 << KReg)))
913 break;
914 DEBUG(dbgs() << "Popping %FP" << KReg << "\n");
915 popStackAfter(I2);
916 Kills &= ~(1 << KReg);
917 }
918 }
919
920 // Manually kill the rest.
921 while (Kills) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000922 unsigned KReg = countTrailingZeros(Kills);
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000923 DEBUG(dbgs() << "Killing %FP" << KReg << "\n");
924 freeStackSlotBefore(I, KReg);
925 Kills &= ~(1 << KReg);
926 }
927
928 // Load zeros for all the imp-defs.
929 while(Defs) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000930 unsigned DReg = countTrailingZeros(Defs);
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000931 DEBUG(dbgs() << "Defining %FP" << DReg << " as 0\n");
932 BuildMI(*MBB, I, DebugLoc(), TII->get(X86::LD_F0));
933 pushReg(DReg);
934 Defs &= ~(1 << DReg);
935 }
936
937 // Now we should have the correct registers live.
938 DEBUG(dumpStack());
939 assert(StackTop == CountPopulation_32(Mask) && "Live count mismatch");
940}
941
942/// shuffleStackTop - emit fxch instructions before I to shuffle the top
943/// FixCount entries into the order given by FixStack.
944/// FIXME: Is there a better algorithm than insertion sort?
945void FPS::shuffleStackTop(const unsigned char *FixStack,
946 unsigned FixCount,
947 MachineBasicBlock::iterator I) {
948 // Move items into place, starting from the desired stack bottom.
949 while (FixCount--) {
950 // Old register at position FixCount.
951 unsigned OldReg = getStackEntry(FixCount);
952 // Desired register at position FixCount.
953 unsigned Reg = FixStack[FixCount];
954 if (Reg == OldReg)
955 continue;
956 // (Reg st0) (OldReg st0) = (Reg OldReg st0)
957 moveToTop(Reg, I);
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +0000958 if (FixCount > 0)
959 moveToTop(OldReg, I);
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +0000960 }
961 DEBUG(dumpStack());
Chris Lattnerbc7e35b2004-04-01 04:06:09 +0000962}
963
964
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000965//===----------------------------------------------------------------------===//
966// Instruction transformation implementation
967//===----------------------------------------------------------------------===//
968
969/// handleZeroArgFP - ST(0) = fld0 ST(0) = flds <mem>
Chris Lattner7af8ad62004-02-02 19:23:15 +0000970///
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000971void FPS::handleZeroArgFP(MachineBasicBlock::iterator &I) {
Alkis Evlogimenos80da8652004-02-12 02:27:10 +0000972 MachineInstr *MI = I;
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000973 unsigned DestReg = getFPReg(MI->getOperand(0));
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000974
Chris Lattnerf431ad42005-12-21 07:47:04 +0000975 // Change from the pseudo instruction to the concrete instruction.
976 MI->RemoveOperand(0); // Remove the explicit ST(0) operand
Chris Lattner59687512008-01-11 18:10:50 +0000977 MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
Chad Rosier24c19d22012-08-01 18:39:17 +0000978
Chris Lattnerf431ad42005-12-21 07:47:04 +0000979 // Result gets pushed on the stack.
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000980 pushReg(DestReg);
981}
982
Chris Lattner7af8ad62004-02-02 19:23:15 +0000983/// handleOneArgFP - fst <mem>, ST(0)
984///
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000985void FPS::handleOneArgFP(MachineBasicBlock::iterator &I) {
Alkis Evlogimenos80da8652004-02-12 02:27:10 +0000986 MachineInstr *MI = I;
Chris Lattner03ad8852008-01-07 07:27:27 +0000987 unsigned NumOps = MI->getDesc().getNumOperands();
Chris Lattnerec536272010-07-08 22:41:28 +0000988 assert((NumOps == X86::AddrNumOperands + 1 || NumOps == 1) &&
Chris Lattner81613062004-02-03 07:27:34 +0000989 "Can only handle fst* & ftst instructions!");
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000990
Chris Lattner7af8ad62004-02-02 19:23:15 +0000991 // Is this the last use of the source register?
Evan Cheng14140052006-11-10 01:28:43 +0000992 unsigned Reg = getFPReg(MI->getOperand(NumOps-1));
Evan Cheng63254462008-03-05 00:59:57 +0000993 bool KillsSrc = MI->killsRegister(X86::FP0+Reg);
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000994
Jakob Stoklund Olesen4f0ace52011-08-08 17:15:43 +0000995 if (KillsSrc)
996 duplicatePendingSTBeforeKill(Reg, I);
997
Evan Cheng70af6202006-02-18 02:36:28 +0000998 // FISTP64m is strange because there isn't a non-popping versions.
Chris Lattnercf53bcf2003-01-13 01:01:59 +0000999 // If we have one _and_ we don't want to pop the operand, duplicate the value
1000 // on the stack instead of moving it. This ensure that popping the value is
1001 // always ok.
Dale Johannesenff7e4432007-09-17 20:15:38 +00001002 // Ditto FISTTP16m, FISTTP32m, FISTTP64m, ST_FpP80m.
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001003 //
Evan Cheng70af6202006-02-18 02:36:28 +00001004 if (!KillsSrc &&
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001005 (MI->getOpcode() == X86::IST_Fp64m32 ||
1006 MI->getOpcode() == X86::ISTT_Fp16m32 ||
1007 MI->getOpcode() == X86::ISTT_Fp32m32 ||
1008 MI->getOpcode() == X86::ISTT_Fp64m32 ||
1009 MI->getOpcode() == X86::IST_Fp64m64 ||
1010 MI->getOpcode() == X86::ISTT_Fp16m64 ||
1011 MI->getOpcode() == X86::ISTT_Fp32m64 ||
Dale Johannesenb1888e72007-08-05 18:49:15 +00001012 MI->getOpcode() == X86::ISTT_Fp64m64 ||
Dale Johannesen95be0372007-09-20 01:27:54 +00001013 MI->getOpcode() == X86::IST_Fp64m80 ||
Dale Johannesen57c6ac5f2007-08-07 01:17:37 +00001014 MI->getOpcode() == X86::ISTT_Fp16m80 ||
1015 MI->getOpcode() == X86::ISTT_Fp32m80 ||
1016 MI->getOpcode() == X86::ISTT_Fp64m80 ||
Dale Johannesenb1888e72007-08-05 18:49:15 +00001017 MI->getOpcode() == X86::ST_FpP80m)) {
Jakob Stoklund Olesenf0af2362010-07-16 17:41:40 +00001018 duplicateToTop(Reg, getScratchReg(), I);
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001019 } else {
1020 moveToTop(Reg, I); // Move to the top of the stack...
1021 }
Chad Rosier24c19d22012-08-01 18:39:17 +00001022
Chris Lattnerf431ad42005-12-21 07:47:04 +00001023 // Convert from the pseudo instruction to the concrete instruction.
Evan Cheng14140052006-11-10 01:28:43 +00001024 MI->RemoveOperand(NumOps-1); // Remove explicit ST(0) operand
Chris Lattner59687512008-01-11 18:10:50 +00001025 MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
Misha Brukmanc88330a2005-04-21 23:38:14 +00001026
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001027 if (MI->getOpcode() == X86::IST_FP64m ||
1028 MI->getOpcode() == X86::ISTT_FP16m ||
1029 MI->getOpcode() == X86::ISTT_FP32m ||
Dale Johannesene279fd62007-08-06 19:50:32 +00001030 MI->getOpcode() == X86::ISTT_FP64m ||
1031 MI->getOpcode() == X86::ST_FP80m) {
Evan Chengd565b442010-10-12 23:19:28 +00001032 if (StackTop == 0)
1033 report_fatal_error("Stack empty??");
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001034 --StackTop;
1035 } else if (KillsSrc) { // Last use of operand?
1036 popStackAfter(I);
1037 }
1038}
1039
Chris Lattner7af8ad62004-02-02 19:23:15 +00001040
Chris Lattner5b444722004-04-11 20:21:06 +00001041/// handleOneArgFPRW: Handle instructions that read from the top of stack and
1042/// replace the value with a newly computed value. These instructions may have
1043/// non-fp operands after their FP operands.
1044///
1045/// Examples:
1046/// R1 = fchs R2
1047/// R1 = fadd R2, [mem]
Chris Lattner7af8ad62004-02-02 19:23:15 +00001048///
1049void FPS::handleOneArgFPRW(MachineBasicBlock::iterator &I) {
Alkis Evlogimenos80da8652004-02-12 02:27:10 +00001050 MachineInstr *MI = I;
Evan Chengfa374ca2008-07-21 20:02:45 +00001051#ifndef NDEBUG
Chris Lattner03ad8852008-01-07 07:27:27 +00001052 unsigned NumOps = MI->getDesc().getNumOperands();
Evan Cheng14140052006-11-10 01:28:43 +00001053 assert(NumOps >= 2 && "FPRW instructions must have 2 ops!!");
Evan Chengfa374ca2008-07-21 20:02:45 +00001054#endif
Chris Lattner7af8ad62004-02-02 19:23:15 +00001055
1056 // Is this the last use of the source register?
1057 unsigned Reg = getFPReg(MI->getOperand(1));
Evan Cheng63254462008-03-05 00:59:57 +00001058 bool KillsSrc = MI->killsRegister(X86::FP0+Reg);
Chris Lattner7af8ad62004-02-02 19:23:15 +00001059
1060 if (KillsSrc) {
Jakob Stoklund Olesen4f0ace52011-08-08 17:15:43 +00001061 duplicatePendingSTBeforeKill(Reg, I);
Chris Lattner7af8ad62004-02-02 19:23:15 +00001062 // If this is the last use of the source register, just make sure it's on
1063 // the top of the stack.
1064 moveToTop(Reg, I);
Evan Chengd565b442010-10-12 23:19:28 +00001065 if (StackTop == 0)
1066 report_fatal_error("Stack cannot be empty!");
Chris Lattner7af8ad62004-02-02 19:23:15 +00001067 --StackTop;
1068 pushReg(getFPReg(MI->getOperand(0)));
1069 } else {
1070 // If this is not the last use of the source register, _copy_ it to the top
1071 // of the stack.
1072 duplicateToTop(Reg, getFPReg(MI->getOperand(0)), I);
1073 }
1074
Chris Lattnerf431ad42005-12-21 07:47:04 +00001075 // Change from the pseudo instruction to the concrete instruction.
Chris Lattner7af8ad62004-02-02 19:23:15 +00001076 MI->RemoveOperand(1); // Drop the source operand.
1077 MI->RemoveOperand(0); // Drop the destination operand.
Chris Lattner59687512008-01-11 18:10:50 +00001078 MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
Chris Lattner7af8ad62004-02-02 19:23:15 +00001079}
1080
1081
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001082//===----------------------------------------------------------------------===//
1083// Define tables of various ways to map pseudo instructions
1084//
1085
1086// ForwardST0Table - Map: A = B op C into: ST(0) = ST(0) op ST(i)
1087static const TableEntry ForwardST0Table[] = {
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001088 { X86::ADD_Fp32 , X86::ADD_FST0r },
1089 { X86::ADD_Fp64 , X86::ADD_FST0r },
Dale Johannesen75169a82007-08-06 21:31:06 +00001090 { X86::ADD_Fp80 , X86::ADD_FST0r },
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001091 { X86::DIV_Fp32 , X86::DIV_FST0r },
1092 { X86::DIV_Fp64 , X86::DIV_FST0r },
Dale Johannesen75169a82007-08-06 21:31:06 +00001093 { X86::DIV_Fp80 , X86::DIV_FST0r },
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001094 { X86::MUL_Fp32 , X86::MUL_FST0r },
1095 { X86::MUL_Fp64 , X86::MUL_FST0r },
Dale Johannesen75169a82007-08-06 21:31:06 +00001096 { X86::MUL_Fp80 , X86::MUL_FST0r },
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001097 { X86::SUB_Fp32 , X86::SUB_FST0r },
1098 { X86::SUB_Fp64 , X86::SUB_FST0r },
Dale Johannesen75169a82007-08-06 21:31:06 +00001099 { X86::SUB_Fp80 , X86::SUB_FST0r },
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001100};
1101
1102// ReverseST0Table - Map: A = B op C into: ST(0) = ST(i) op ST(0)
1103static const TableEntry ReverseST0Table[] = {
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001104 { X86::ADD_Fp32 , X86::ADD_FST0r }, // commutative
1105 { X86::ADD_Fp64 , X86::ADD_FST0r }, // commutative
Dale Johannesen75169a82007-08-06 21:31:06 +00001106 { X86::ADD_Fp80 , X86::ADD_FST0r }, // commutative
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001107 { X86::DIV_Fp32 , X86::DIVR_FST0r },
1108 { X86::DIV_Fp64 , X86::DIVR_FST0r },
Dale Johannesen75169a82007-08-06 21:31:06 +00001109 { X86::DIV_Fp80 , X86::DIVR_FST0r },
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001110 { X86::MUL_Fp32 , X86::MUL_FST0r }, // commutative
1111 { X86::MUL_Fp64 , X86::MUL_FST0r }, // commutative
Dale Johannesen75169a82007-08-06 21:31:06 +00001112 { X86::MUL_Fp80 , X86::MUL_FST0r }, // commutative
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001113 { X86::SUB_Fp32 , X86::SUBR_FST0r },
1114 { X86::SUB_Fp64 , X86::SUBR_FST0r },
Dale Johannesen75169a82007-08-06 21:31:06 +00001115 { X86::SUB_Fp80 , X86::SUBR_FST0r },
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001116};
1117
1118// ForwardSTiTable - Map: A = B op C into: ST(i) = ST(0) op ST(i)
1119static const TableEntry ForwardSTiTable[] = {
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001120 { X86::ADD_Fp32 , X86::ADD_FrST0 }, // commutative
1121 { X86::ADD_Fp64 , X86::ADD_FrST0 }, // commutative
Dale Johannesen75169a82007-08-06 21:31:06 +00001122 { X86::ADD_Fp80 , X86::ADD_FrST0 }, // commutative
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001123 { X86::DIV_Fp32 , X86::DIVR_FrST0 },
1124 { X86::DIV_Fp64 , X86::DIVR_FrST0 },
Dale Johannesen75169a82007-08-06 21:31:06 +00001125 { X86::DIV_Fp80 , X86::DIVR_FrST0 },
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001126 { X86::MUL_Fp32 , X86::MUL_FrST0 }, // commutative
1127 { X86::MUL_Fp64 , X86::MUL_FrST0 }, // commutative
Dale Johannesen75169a82007-08-06 21:31:06 +00001128 { X86::MUL_Fp80 , X86::MUL_FrST0 }, // commutative
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001129 { X86::SUB_Fp32 , X86::SUBR_FrST0 },
1130 { X86::SUB_Fp64 , X86::SUBR_FrST0 },
Dale Johannesen75169a82007-08-06 21:31:06 +00001131 { X86::SUB_Fp80 , X86::SUBR_FrST0 },
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001132};
1133
1134// ReverseSTiTable - Map: A = B op C into: ST(i) = ST(i) op ST(0)
1135static const TableEntry ReverseSTiTable[] = {
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001136 { X86::ADD_Fp32 , X86::ADD_FrST0 },
1137 { X86::ADD_Fp64 , X86::ADD_FrST0 },
Dale Johannesen75169a82007-08-06 21:31:06 +00001138 { X86::ADD_Fp80 , X86::ADD_FrST0 },
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001139 { X86::DIV_Fp32 , X86::DIV_FrST0 },
1140 { X86::DIV_Fp64 , X86::DIV_FrST0 },
Dale Johannesen75169a82007-08-06 21:31:06 +00001141 { X86::DIV_Fp80 , X86::DIV_FrST0 },
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001142 { X86::MUL_Fp32 , X86::MUL_FrST0 },
1143 { X86::MUL_Fp64 , X86::MUL_FrST0 },
Dale Johannesen75169a82007-08-06 21:31:06 +00001144 { X86::MUL_Fp80 , X86::MUL_FrST0 },
Dale Johannesen3d7008c2007-07-04 21:07:47 +00001145 { X86::SUB_Fp32 , X86::SUB_FrST0 },
1146 { X86::SUB_Fp64 , X86::SUB_FrST0 },
Dale Johannesen75169a82007-08-06 21:31:06 +00001147 { X86::SUB_Fp80 , X86::SUB_FrST0 },
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001148};
1149
1150
1151/// handleTwoArgFP - Handle instructions like FADD and friends which are virtual
1152/// instructions which need to be simplified and possibly transformed.
1153///
1154/// Result: ST(0) = fsub ST(0), ST(i)
1155/// ST(i) = fsub ST(0), ST(i)
1156/// ST(0) = fsubr ST(0), ST(i)
1157/// ST(i) = fsubr ST(0), ST(i)
Misha Brukmanc88330a2005-04-21 23:38:14 +00001158///
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001159void FPS::handleTwoArgFP(MachineBasicBlock::iterator &I) {
1160 ASSERT_SORTED(ForwardST0Table); ASSERT_SORTED(ReverseST0Table);
1161 ASSERT_SORTED(ForwardSTiTable); ASSERT_SORTED(ReverseSTiTable);
Alkis Evlogimenos80da8652004-02-12 02:27:10 +00001162 MachineInstr *MI = I;
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001163
Chris Lattner03ad8852008-01-07 07:27:27 +00001164 unsigned NumOperands = MI->getDesc().getNumOperands();
Chris Lattner94ff2c32004-06-11 04:25:06 +00001165 assert(NumOperands == 3 && "Illegal TwoArgFP instruction!");
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001166 unsigned Dest = getFPReg(MI->getOperand(0));
1167 unsigned Op0 = getFPReg(MI->getOperand(NumOperands-2));
1168 unsigned Op1 = getFPReg(MI->getOperand(NumOperands-1));
Evan Cheng63254462008-03-05 00:59:57 +00001169 bool KillsOp0 = MI->killsRegister(X86::FP0+Op0);
1170 bool KillsOp1 = MI->killsRegister(X86::FP0+Op1);
Dale Johannesen9bba9022009-02-13 02:33:27 +00001171 DebugLoc dl = MI->getDebugLoc();
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001172
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001173 unsigned TOS = getStackEntry(0);
1174
1175 // One of our operands must be on the top of the stack. If neither is yet, we
1176 // need to move one.
1177 if (Op0 != TOS && Op1 != TOS) { // No operand at TOS?
1178 // We can choose to move either operand to the top of the stack. If one of
1179 // the operands is killed by this instruction, we want that one so that we
1180 // can update right on top of the old version.
1181 if (KillsOp0) {
1182 moveToTop(Op0, I); // Move dead operand to TOS.
1183 TOS = Op0;
1184 } else if (KillsOp1) {
1185 moveToTop(Op1, I);
1186 TOS = Op1;
1187 } else {
1188 // All of the operands are live after this instruction executes, so we
1189 // cannot update on top of any operand. Because of this, we must
1190 // duplicate one of the stack elements to the top. It doesn't matter
1191 // which one we pick.
1192 //
1193 duplicateToTop(Op0, Dest, I);
1194 Op0 = TOS = Dest;
1195 KillsOp0 = true;
1196 }
Chris Lattner94ff2c32004-06-11 04:25:06 +00001197 } else if (!KillsOp0 && !KillsOp1) {
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001198 // If we DO have one of our operands at the top of the stack, but we don't
1199 // have a dead operand, we must duplicate one of the operands to a new slot
1200 // on the stack.
1201 duplicateToTop(Op0, Dest, I);
1202 Op0 = TOS = Dest;
1203 KillsOp0 = true;
1204 }
1205
1206 // Now we know that one of our operands is on the top of the stack, and at
1207 // least one of our operands is killed by this instruction.
Misha Brukmanc88330a2005-04-21 23:38:14 +00001208 assert((TOS == Op0 || TOS == Op1) && (KillsOp0 || KillsOp1) &&
1209 "Stack conditions not set up right!");
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001210
1211 // We decide which form to use based on what is on the top of the stack, and
1212 // which operand is killed by this instruction.
1213 const TableEntry *InstTable;
1214 bool isForward = TOS == Op0;
1215 bool updateST0 = (TOS == Op0 && !KillsOp1) || (TOS == Op1 && !KillsOp0);
1216 if (updateST0) {
1217 if (isForward)
1218 InstTable = ForwardST0Table;
1219 else
1220 InstTable = ReverseST0Table;
1221 } else {
1222 if (isForward)
1223 InstTable = ForwardSTiTable;
1224 else
1225 InstTable = ReverseSTiTable;
1226 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001227
Owen Andersone2f23a32007-09-07 04:06:50 +00001228 int Opcode = Lookup(InstTable, array_lengthof(ForwardST0Table),
1229 MI->getOpcode());
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001230 assert(Opcode != -1 && "Unknown TwoArgFP pseudo instruction!");
1231
1232 // NotTOS - The register which is not on the top of stack...
1233 unsigned NotTOS = (TOS == Op0) ? Op1 : Op0;
1234
1235 // Replace the old instruction with a new instruction
Chris Lattnerc07c9582004-03-31 22:02:36 +00001236 MBB->remove(I++);
Dale Johannesen9bba9022009-02-13 02:33:27 +00001237 I = BuildMI(*MBB, I, dl, TII->get(Opcode)).addReg(getSTReg(NotTOS));
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001238
1239 // If both operands are killed, pop one off of the stack in addition to
1240 // overwriting the other one.
1241 if (KillsOp0 && KillsOp1 && Op0 != Op1) {
1242 assert(!updateST0 && "Should have updated other operand!");
1243 popStackAfter(I); // Pop the top of stack
1244 }
1245
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001246 // Update stack information so that we know the destination register is now on
1247 // the stack.
Chris Lattner94ff2c32004-06-11 04:25:06 +00001248 unsigned UpdatedSlot = getSlot(updateST0 ? TOS : NotTOS);
1249 assert(UpdatedSlot < StackTop && Dest < 7);
1250 Stack[UpdatedSlot] = Dest;
1251 RegMap[Dest] = UpdatedSlot;
Dan Gohman3b460302008-07-07 23:14:23 +00001252 MBB->getParent()->DeleteMachineInstr(MI); // Remove the old instruction
Chris Lattner94ff2c32004-06-11 04:25:06 +00001253}
1254
Chris Lattnerb35f4762004-06-11 04:49:02 +00001255/// handleCompareFP - Handle FUCOM and FUCOMI instructions, which have two FP
Chris Lattner94ff2c32004-06-11 04:25:06 +00001256/// register arguments and no explicit destinations.
Misha Brukmanc88330a2005-04-21 23:38:14 +00001257///
Chris Lattner94ff2c32004-06-11 04:25:06 +00001258void FPS::handleCompareFP(MachineBasicBlock::iterator &I) {
1259 ASSERT_SORTED(ForwardST0Table); ASSERT_SORTED(ReverseST0Table);
1260 ASSERT_SORTED(ForwardSTiTable); ASSERT_SORTED(ReverseSTiTable);
1261 MachineInstr *MI = I;
1262
Chris Lattner03ad8852008-01-07 07:27:27 +00001263 unsigned NumOperands = MI->getDesc().getNumOperands();
Chris Lattnerb35f4762004-06-11 04:49:02 +00001264 assert(NumOperands == 2 && "Illegal FUCOM* instruction!");
Chris Lattner94ff2c32004-06-11 04:25:06 +00001265 unsigned Op0 = getFPReg(MI->getOperand(NumOperands-2));
1266 unsigned Op1 = getFPReg(MI->getOperand(NumOperands-1));
Evan Cheng63254462008-03-05 00:59:57 +00001267 bool KillsOp0 = MI->killsRegister(X86::FP0+Op0);
1268 bool KillsOp1 = MI->killsRegister(X86::FP0+Op1);
Chris Lattner94ff2c32004-06-11 04:25:06 +00001269
1270 // Make sure the first operand is on the top of stack, the other one can be
1271 // anywhere.
1272 moveToTop(Op0, I);
1273
Chris Lattnerf431ad42005-12-21 07:47:04 +00001274 // Change from the pseudo instruction to the concrete instruction.
Chris Lattner71186e22004-06-11 05:22:44 +00001275 MI->getOperand(0).setReg(getSTReg(Op1));
1276 MI->RemoveOperand(1);
Chris Lattner59687512008-01-11 18:10:50 +00001277 MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
Chris Lattner71186e22004-06-11 05:22:44 +00001278
Chris Lattner94ff2c32004-06-11 04:25:06 +00001279 // If any of the operands are killed by this instruction, free them.
1280 if (KillsOp0) freeStackSlotAfter(I, Op0);
1281 if (KillsOp1 && Op0 != Op1) freeStackSlotAfter(I, Op1);
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001282}
1283
Chris Lattnerc07c9582004-03-31 22:02:36 +00001284/// handleCondMovFP - Handle two address conditional move instructions. These
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001285/// instructions move a st(i) register to st(0) iff a condition is true. These
Chris Lattnerc07c9582004-03-31 22:02:36 +00001286/// instructions require that the first operand is at the top of the stack, but
1287/// otherwise don't modify the stack at all.
1288void FPS::handleCondMovFP(MachineBasicBlock::iterator &I) {
1289 MachineInstr *MI = I;
1290
1291 unsigned Op0 = getFPReg(MI->getOperand(0));
Chris Lattner26569322006-09-05 20:27:32 +00001292 unsigned Op1 = getFPReg(MI->getOperand(2));
Evan Cheng63254462008-03-05 00:59:57 +00001293 bool KillsOp1 = MI->killsRegister(X86::FP0+Op1);
Chris Lattnerc07c9582004-03-31 22:02:36 +00001294
1295 // The first operand *must* be on the top of the stack.
1296 moveToTop(Op0, I);
1297
1298 // Change the second operand to the stack register that the operand is in.
Chris Lattnerf431ad42005-12-21 07:47:04 +00001299 // Change from the pseudo instruction to the concrete instruction.
Chris Lattnerc07c9582004-03-31 22:02:36 +00001300 MI->RemoveOperand(0);
Chris Lattner26569322006-09-05 20:27:32 +00001301 MI->RemoveOperand(1);
Chris Lattnerc07c9582004-03-31 22:02:36 +00001302 MI->getOperand(0).setReg(getSTReg(Op1));
Chris Lattner59687512008-01-11 18:10:50 +00001303 MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
Chad Rosier24c19d22012-08-01 18:39:17 +00001304
Chris Lattnerc07c9582004-03-31 22:02:36 +00001305 // If we kill the second operand, make sure to pop it from the stack.
Evan Chengbbbcac32006-11-15 20:56:39 +00001306 if (Op0 != Op1 && KillsOp1) {
Chris Lattner7c1c6e02005-08-23 22:49:55 +00001307 // Get this value off of the register stack.
1308 freeStackSlotAfter(I, Op1);
1309 }
Chris Lattnerc07c9582004-03-31 22:02:36 +00001310}
1311
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001312
1313/// handleSpecialFP - Handle special instructions which behave unlike other
Misha Brukman8b2bd4e2003-10-10 17:57:28 +00001314/// floating point instructions. This is primarily intended for use by pseudo
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001315/// instructions.
1316///
1317void FPS::handleSpecialFP(MachineBasicBlock::iterator &I) {
Alkis Evlogimenos80da8652004-02-12 02:27:10 +00001318 MachineInstr *MI = I;
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001319 switch (MI->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001320 default: llvm_unreachable("Unknown SpecialFP instruction!");
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001321 case TargetOpcode::COPY: {
1322 // We handle three kinds of copies: FP <- FP, FP <- ST, and ST <- FP.
Evan Cheng968c3b02009-03-23 08:01:15 +00001323 const MachineOperand &MO1 = MI->getOperand(1);
Evan Cheng968c3b02009-03-23 08:01:15 +00001324 const MachineOperand &MO0 = MI->getOperand(0);
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001325 unsigned DstST = MO0.getReg() - X86::ST0;
1326 unsigned SrcST = MO1.getReg() - X86::ST0;
1327 bool KillsSrc = MI->killsRegister(MO1.getReg());
1328
1329 // ST = COPY FP. Set up a pending ST register.
1330 if (DstST < 8) {
1331 unsigned SrcFP = getFPReg(MO1);
1332 assert(isLive(SrcFP) && "Cannot copy dead register");
1333 assert(!MO0.isDead() && "Cannot copy to dead ST register");
1334
1335 // Unallocated STs are marked as the nonexistent FP255.
1336 while (NumPendingSTs <= DstST)
1337 PendingST[NumPendingSTs++] = NumFPRegs;
1338
1339 // STi could still be live from a previous inline asm.
1340 if (isScratchReg(PendingST[DstST])) {
1341 DEBUG(dbgs() << "Clobbering old ST in FP" << unsigned(PendingST[DstST])
1342 << '\n');
1343 freeStackSlotBefore(MI, PendingST[DstST]);
1344 }
1345
1346 // When the source is killed, allocate a scratch FP register.
1347 if (KillsSrc) {
Jakob Stoklund Olesen4f0ace52011-08-08 17:15:43 +00001348 duplicatePendingSTBeforeKill(SrcFP, I);
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001349 unsigned Slot = getSlot(SrcFP);
1350 unsigned SR = getScratchReg();
1351 PendingST[DstST] = SR;
1352 Stack[Slot] = SR;
1353 RegMap[SR] = Slot;
1354 } else
1355 PendingST[DstST] = SrcFP;
1356 break;
1357 }
1358
1359 // FP = COPY ST. Extract fixed stack value.
1360 // Any instruction defining ST registers must have assigned them to a
1361 // scratch register.
1362 if (SrcST < 8) {
1363 unsigned DstFP = getFPReg(MO0);
1364 assert(!isLive(DstFP) && "Cannot copy ST to live FP register");
1365 assert(NumPendingSTs > SrcST && "Cannot copy from dead ST register");
1366 unsigned SrcFP = PendingST[SrcST];
1367 assert(isScratchReg(SrcFP) && "Expected ST in a scratch register");
1368 assert(isLive(SrcFP) && "Scratch holding ST is dead");
1369
1370 // DstFP steals the stack slot from SrcFP.
1371 unsigned Slot = getSlot(SrcFP);
1372 Stack[Slot] = DstFP;
1373 RegMap[DstFP] = Slot;
1374
1375 // Always treat the ST as killed.
1376 PendingST[SrcST] = NumFPRegs;
1377 while (NumPendingSTs && PendingST[NumPendingSTs - 1] == NumFPRegs)
1378 --NumPendingSTs;
1379 break;
1380 }
1381
1382 // FP <- FP copy.
1383 unsigned DstFP = getFPReg(MO0);
1384 unsigned SrcFP = getFPReg(MO1);
1385 assert(isLive(SrcFP) && "Cannot copy dead register");
1386 if (KillsSrc) {
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001387 // If the input operand is killed, we can just change the owner of the
1388 // incoming stack slot into the result.
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001389 unsigned Slot = getSlot(SrcFP);
1390 Stack[Slot] = DstFP;
1391 RegMap[DstFP] = Slot;
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001392 } else {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001393 // For COPY we just duplicate the specified value to a new stack slot.
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001394 // This could be made better, but would require substantial changes.
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001395 duplicateToTop(SrcFP, DstFP, I);
Nick Lewyckya3860a22008-03-11 05:56:09 +00001396 }
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001397 break;
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001398 }
1399
Jakob Stoklund Olesenda618422011-08-03 16:33:19 +00001400 case TargetOpcode::IMPLICIT_DEF: {
1401 // All FP registers must be explicitly defined, so load a 0 instead.
1402 unsigned Reg = MI->getOperand(0).getReg() - X86::FP0;
1403 DEBUG(dbgs() << "Emitting LD_F0 for implicit FP" << Reg << '\n');
1404 BuildMI(*MBB, I, MI->getDebugLoc(), TII->get(X86::LD_F0));
1405 pushReg(Reg);
1406 break;
1407 }
1408
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001409 case X86::FpPOP_RETVAL: {
1410 // The FpPOP_RETVAL instruction is used after calls that return a value on
1411 // the floating point stack. We cannot model this with ST defs since CALL
1412 // instructions have fixed clobber lists. This instruction is interpreted
1413 // to mean that there is one more live register on the stack than we
1414 // thought.
1415 //
1416 // This means that StackTop does not match the hardware stack between a
1417 // call and the FpPOP_RETVAL instructions. We do tolerate FP instructions
1418 // between CALL and FpPOP_RETVAL as long as they don't overflow the
1419 // hardware stack.
1420 unsigned DstFP = getFPReg(MI->getOperand(0));
1421
1422 // Move existing stack elements up to reflect reality.
1423 assert(StackTop < 8 && "Stack overflowed before FpPOP_RETVAL");
1424 if (StackTop) {
1425 std::copy_backward(Stack, Stack + StackTop, Stack + StackTop + 1);
1426 for (unsigned i = 0; i != NumFPRegs; ++i)
1427 ++RegMap[i];
1428 }
1429 ++StackTop;
1430
1431 // DstFP is the new bottom of the stack.
1432 Stack[0] = DstFP;
1433 RegMap[DstFP] = 0;
1434
1435 // DstFP will be killed by processBasicBlock if this was a dead def.
1436 break;
1437 }
1438
Chris Lattnerb06015a2010-02-09 19:54:29 +00001439 case TargetOpcode::INLINEASM: {
Chris Lattner8abed802008-03-11 19:50:13 +00001440 // The inline asm MachineInstr currently only *uses* FP registers for the
1441 // 'f' constraint. These should be turned into the current ST(x) register
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001442 // in the machine instr.
1443 //
1444 // There are special rules for x87 inline assembly. The compiler must know
1445 // exactly how many registers are popped and pushed implicitly by the asm.
1446 // Otherwise it is not possible to restore the stack state after the inline
1447 // asm.
1448 //
1449 // There are 3 kinds of input operands:
1450 //
1451 // 1. Popped inputs. These must appear at the stack top in ST0-STn. A
1452 // popped input operand must be in a fixed stack slot, and it is either
1453 // tied to an output operand, or in the clobber list. The MI has ST use
1454 // and def operands for these inputs.
1455 //
1456 // 2. Fixed inputs. These inputs appear in fixed stack slots, but are
1457 // preserved by the inline asm. The fixed stack slots must be STn-STm
1458 // following the popped inputs. A fixed input operand cannot be tied to
1459 // an output or appear in the clobber list. The MI has ST use operands
1460 // and no defs for these inputs.
1461 //
1462 // 3. Preserved inputs. These inputs use the "f" constraint which is
1463 // represented as an FP register. The inline asm won't change these
1464 // stack slots.
1465 //
1466 // Outputs must be in ST registers, FP outputs are not allowed. Clobbered
1467 // registers do not count as output operands. The inline asm changes the
1468 // stack as if it popped all the popped inputs and then pushed all the
1469 // output operands.
1470
1471 // Scan the assembly for ST registers used, defined and clobbered. We can
1472 // only tell clobbers from defs by looking at the asm descriptor.
1473 unsigned STUses = 0, STDefs = 0, STClobbers = 0, STDeadDefs = 0;
1474 unsigned NumOps = 0;
1475 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = MI->getNumOperands();
1476 i != e && MI->getOperand(i).isImm(); i += 1 + NumOps) {
1477 unsigned Flags = MI->getOperand(i).getImm();
1478 NumOps = InlineAsm::getNumOperandRegisters(Flags);
1479 if (NumOps != 1)
1480 continue;
1481 const MachineOperand &MO = MI->getOperand(i + 1);
1482 if (!MO.isReg())
1483 continue;
1484 unsigned STReg = MO.getReg() - X86::ST0;
1485 if (STReg >= 8)
1486 continue;
1487
1488 switch (InlineAsm::getKind(Flags)) {
1489 case InlineAsm::Kind_RegUse:
1490 STUses |= (1u << STReg);
1491 break;
1492 case InlineAsm::Kind_RegDef:
1493 case InlineAsm::Kind_RegDefEarlyClobber:
1494 STDefs |= (1u << STReg);
1495 if (MO.isDead())
1496 STDeadDefs |= (1u << STReg);
1497 break;
1498 case InlineAsm::Kind_Clobber:
1499 STClobbers |= (1u << STReg);
1500 break;
1501 default:
1502 break;
1503 }
1504 }
1505
1506 if (STUses && !isMask_32(STUses))
Jakob Stoklund Olesene925f222011-07-02 07:23:40 +00001507 MI->emitError("fixed input regs must be last on the x87 stack");
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001508 unsigned NumSTUses = CountTrailingOnes_32(STUses);
1509
1510 // Defs must be contiguous from the stack top. ST0-STn.
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00001511 if (STDefs && !isMask_32(STDefs)) {
Jakob Stoklund Olesene925f222011-07-02 07:23:40 +00001512 MI->emitError("output regs must be last on the x87 stack");
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00001513 STDefs = NextPowerOf2(STDefs) - 1;
1514 }
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001515 unsigned NumSTDefs = CountTrailingOnes_32(STDefs);
1516
1517 // So must the clobbered stack slots. ST0-STm, m >= n.
1518 if (STClobbers && !isMask_32(STDefs | STClobbers))
Jakob Stoklund Olesene925f222011-07-02 07:23:40 +00001519 MI->emitError("clobbers must be last on the x87 stack");
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001520
1521 // Popped inputs are the ones that are also clobbered or defined.
1522 unsigned STPopped = STUses & (STDefs | STClobbers);
1523 if (STPopped && !isMask_32(STPopped))
Jakob Stoklund Olesene925f222011-07-02 07:23:40 +00001524 MI->emitError("implicitly popped regs must be last on the x87 stack");
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001525 unsigned NumSTPopped = CountTrailingOnes_32(STPopped);
1526
1527 DEBUG(dbgs() << "Asm uses " << NumSTUses << " fixed regs, pops "
1528 << NumSTPopped << ", and defines " << NumSTDefs << " regs.\n");
1529
1530 // Scan the instruction for FP uses corresponding to "f" constraints.
1531 // Collect FP registers to kill afer the instruction.
1532 // Always kill all the scratch regs.
1533 unsigned FPKills = ((1u << NumFPRegs) - 1) & ~0xff;
1534 unsigned FPUsed = 0;
Chris Lattner8abed802008-03-11 19:50:13 +00001535 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1536 MachineOperand &Op = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001537 if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6)
Chris Lattner8abed802008-03-11 19:50:13 +00001538 continue;
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001539 if (!Op.isUse())
Jakob Stoklund Olesene925f222011-07-02 07:23:40 +00001540 MI->emitError("illegal \"f\" output constraint");
Chris Lattner8abed802008-03-11 19:50:13 +00001541 unsigned FPReg = getFPReg(Op);
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001542 FPUsed |= 1U << FPReg;
1543
Chris Lattner8abed802008-03-11 19:50:13 +00001544 // If we kill this operand, make sure to pop it from the stack after the
1545 // asm. We just remember it for now, and pop them all off at the end in
1546 // a batch.
1547 if (Op.isKill())
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001548 FPKills |= 1U << FPReg;
Chris Lattner8abed802008-03-11 19:50:13 +00001549 }
1550
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001551 // The popped inputs will be killed by the instruction, so duplicate them
1552 // if the FP register needs to be live after the instruction, or if it is
1553 // used in the instruction itself. We effectively treat the popped inputs
1554 // as early clobbers.
1555 for (unsigned i = 0; i < NumSTPopped; ++i) {
1556 if ((FPKills & ~FPUsed) & (1u << PendingST[i]))
1557 continue;
1558 unsigned SR = getScratchReg();
1559 duplicateToTop(PendingST[i], SR, I);
1560 DEBUG(dbgs() << "Duplicating ST" << i << " in FP"
1561 << unsigned(PendingST[i]) << " to avoid clobbering it.\n");
1562 PendingST[i] = SR;
1563 }
1564
1565 // Make sure we have a unique live register for every fixed use. Some of
1566 // them could be undef uses, and we need to emit LD_F0 instructions.
1567 for (unsigned i = 0; i < NumSTUses; ++i) {
1568 if (i < NumPendingSTs && PendingST[i] < NumFPRegs) {
1569 // Check for shared assignments.
1570 for (unsigned j = 0; j < i; ++j) {
1571 if (PendingST[j] != PendingST[i])
1572 continue;
1573 // STi and STj are inn the same register, create a copy.
1574 unsigned SR = getScratchReg();
1575 duplicateToTop(PendingST[i], SR, I);
1576 DEBUG(dbgs() << "Duplicating ST" << i << " in FP"
1577 << unsigned(PendingST[i])
1578 << " to avoid collision with ST" << j << '\n');
1579 PendingST[i] = SR;
1580 }
1581 continue;
1582 }
1583 unsigned SR = getScratchReg();
1584 DEBUG(dbgs() << "Emitting LD_F0 for ST" << i << " in FP" << SR << '\n');
1585 BuildMI(*MBB, I, MI->getDebugLoc(), TII->get(X86::LD_F0));
1586 pushReg(SR);
1587 PendingST[i] = SR;
1588 if (NumPendingSTs == i)
1589 ++NumPendingSTs;
1590 }
1591 assert(NumPendingSTs >= NumSTUses && "Fixed registers should be assigned");
1592
1593 // Now we can rearrange the live registers to match what was requested.
1594 shuffleStackTop(PendingST, NumPendingSTs, I);
1595 DEBUG({dbgs() << "Before asm: "; dumpStack();});
1596
1597 // With the stack layout fixed, rewrite the FP registers.
1598 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1599 MachineOperand &Op = MI->getOperand(i);
1600 if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6)
1601 continue;
1602 unsigned FPReg = getFPReg(Op);
1603 Op.setReg(getSTReg(FPReg));
1604 }
1605
1606 // Simulate the inline asm popping its inputs and pushing its outputs.
1607 StackTop -= NumSTPopped;
1608
1609 // Hold the fixed output registers in scratch FP registers. They will be
1610 // transferred to real FP registers by copies.
1611 NumPendingSTs = 0;
1612 for (unsigned i = 0; i < NumSTDefs; ++i) {
1613 unsigned SR = getScratchReg();
1614 pushReg(SR);
1615 FPKills &= ~(1u << SR);
1616 }
1617 for (unsigned i = 0; i < NumSTDefs; ++i)
1618 PendingST[NumPendingSTs++] = getStackEntry(i);
1619 DEBUG({dbgs() << "After asm: "; dumpStack();});
1620
1621 // If any of the ST defs were dead, pop them immediately. Our caller only
1622 // handles dead FP defs.
1623 MachineBasicBlock::iterator InsertPt = MI;
1624 for (unsigned i = 0; STDefs & (1u << i); ++i) {
1625 if (!(STDeadDefs & (1u << i)))
1626 continue;
1627 freeStackSlotAfter(InsertPt, PendingST[i]);
1628 PendingST[i] = NumFPRegs;
1629 }
1630 while (NumPendingSTs && PendingST[NumPendingSTs - 1] == NumFPRegs)
1631 --NumPendingSTs;
1632
Chris Lattner8abed802008-03-11 19:50:13 +00001633 // If this asm kills any FP registers (is the last use of them) we must
1634 // explicitly emit pop instructions for them. Do this now after the asm has
1635 // executed so that the ST(x) numbers are not off (which would happen if we
1636 // did this inline with operand rewriting).
1637 //
1638 // Note: this might be a non-optimal pop sequence. We might be able to do
1639 // better by trying to pop in stack order or something.
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001640 while (FPKills) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001641 unsigned FPReg = countTrailingZeros(FPKills);
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001642 if (isLive(FPReg))
1643 freeStackSlotAfter(InsertPt, FPReg);
1644 FPKills &= ~(1U << FPReg);
Jakob Stoklund Olesen96fad312010-04-28 18:28:37 +00001645 }
Chris Lattner8abed802008-03-11 19:50:13 +00001646 // Don't delete the inline asm!
1647 return;
1648 }
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00001649
Michael J. Spencer248d65e2012-02-24 19:01:22 +00001650 case X86::WIN_FTOL_32:
1651 case X86::WIN_FTOL_64: {
Michael J. Spencer248d65e2012-02-24 19:01:22 +00001652 // Push the operand into ST0.
1653 MachineOperand &Op = MI->getOperand(0);
1654 assert(Op.isUse() && Op.isReg() &&
1655 Op.getReg() >= X86::FP0 && Op.getReg() <= X86::FP6);
1656 unsigned FPReg = getFPReg(Op);
1657 if (Op.isKill())
1658 moveToTop(FPReg, I);
1659 else
1660 duplicateToTop(FPReg, FPReg, I);
1661
1662 // Emit the call. This will pop the operand.
1663 BuildMI(*MBB, I, MI->getDebugLoc(), TII->get(X86::CALLpcrel32))
1664 .addExternalSymbol("_ftol2")
1665 .addReg(X86::ST0, RegState::ImplicitKill)
Craig Topper8956fe02013-07-21 07:28:13 +00001666 .addReg(X86::ECX, RegState::ImplicitDefine)
Michael J. Spencer248d65e2012-02-24 19:01:22 +00001667 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
1668 .addReg(X86::EDX, RegState::Define | RegState::Implicit)
1669 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
1670 --StackTop;
1671
1672 break;
1673 }
1674
David Woodhouse79dd5052014-01-08 12:58:07 +00001675 case X86::RETQ:
1676 case X86::RETL:
David Woodhouse4e033b02014-01-13 14:05:59 +00001677 case X86::RETIL:
1678 case X86::RETIQ:
Chris Lattner1bd44362008-03-11 03:23:40 +00001679 // If RET has an FP register use operand, pass the first one in ST(0) and
1680 // the second one in ST(1).
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +00001681
Chris Lattner1bd44362008-03-11 03:23:40 +00001682 // Find the register operands.
1683 unsigned FirstFPRegOp = ~0U, SecondFPRegOp = ~0U;
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +00001684 unsigned LiveMask = 0;
1685
Chris Lattner1bd44362008-03-11 03:23:40 +00001686 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1687 MachineOperand &Op = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001688 if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6)
Chris Lattner1bd44362008-03-11 03:23:40 +00001689 continue;
Chris Lattnerc55b4442008-03-21 20:41:27 +00001690 // FP Register uses must be kills unless there are two uses of the same
1691 // register, in which case only one will be a kill.
1692 assert(Op.isUse() &&
1693 (Op.isKill() || // Marked kill.
1694 getFPReg(Op) == FirstFPRegOp || // Second instance.
1695 MI->killsRegister(Op.getReg())) && // Later use is marked kill.
1696 "Ret only defs operands, and values aren't live beyond it");
Chris Lattner1bd44362008-03-11 03:23:40 +00001697
1698 if (FirstFPRegOp == ~0U)
1699 FirstFPRegOp = getFPReg(Op);
1700 else {
1701 assert(SecondFPRegOp == ~0U && "More than two fp operands!");
1702 SecondFPRegOp = getFPReg(Op);
1703 }
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +00001704 LiveMask |= (1 << getFPReg(Op));
Chris Lattner1bd44362008-03-11 03:23:40 +00001705
1706 // Remove the operand so that later passes don't see it.
1707 MI->RemoveOperand(i);
1708 --i, --e;
1709 }
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +00001710
1711 // We may have been carrying spurious live-ins, so make sure only the returned
1712 // registers are left live.
1713 adjustLiveRegs(LiveMask, MI);
1714 if (!LiveMask) return; // Quick check to see if any are possible.
1715
Chris Lattner1bd44362008-03-11 03:23:40 +00001716 // There are only four possibilities here:
1717 // 1) we are returning a single FP value. In this case, it has to be in
1718 // ST(0) already, so just declare success by removing the value from the
1719 // FP Stack.
1720 if (SecondFPRegOp == ~0U) {
1721 // Assert that the top of stack contains the right FP register.
1722 assert(StackTop == 1 && FirstFPRegOp == getStackEntry(0) &&
1723 "Top of stack not the right register for RET!");
Chad Rosier24c19d22012-08-01 18:39:17 +00001724
Chris Lattner1bd44362008-03-11 03:23:40 +00001725 // Ok, everything is good, mark the value as not being on the stack
1726 // anymore so that our assertion about the stack being empty at end of
1727 // block doesn't fire.
1728 StackTop = 0;
1729 return;
1730 }
Chad Rosier24c19d22012-08-01 18:39:17 +00001731
Chris Lattner1bd44362008-03-11 03:23:40 +00001732 // Otherwise, we are returning two values:
1733 // 2) If returning the same value for both, we only have one thing in the FP
1734 // stack. Consider: RET FP1, FP1
1735 if (StackTop == 1) {
1736 assert(FirstFPRegOp == SecondFPRegOp && FirstFPRegOp == getStackEntry(0)&&
1737 "Stack misconfiguration for RET!");
Chad Rosier24c19d22012-08-01 18:39:17 +00001738
Chris Lattner1bd44362008-03-11 03:23:40 +00001739 // Duplicate the TOS so that we return it twice. Just pick some other FPx
1740 // register to hold it.
Jakob Stoklund Olesenf0af2362010-07-16 17:41:40 +00001741 unsigned NewReg = getScratchReg();
Chris Lattner1bd44362008-03-11 03:23:40 +00001742 duplicateToTop(FirstFPRegOp, NewReg, MI);
1743 FirstFPRegOp = NewReg;
1744 }
Chad Rosier24c19d22012-08-01 18:39:17 +00001745
Chris Lattner1bd44362008-03-11 03:23:40 +00001746 /// Okay we know we have two different FPx operands now:
1747 assert(StackTop == 2 && "Must have two values live!");
Chad Rosier24c19d22012-08-01 18:39:17 +00001748
Chris Lattner1bd44362008-03-11 03:23:40 +00001749 /// 3) If SecondFPRegOp is currently in ST(0) and FirstFPRegOp is currently
1750 /// in ST(1). In this case, emit an fxch.
1751 if (getStackEntry(0) == SecondFPRegOp) {
1752 assert(getStackEntry(1) == FirstFPRegOp && "Unknown regs live");
1753 moveToTop(FirstFPRegOp, MI);
1754 }
Chad Rosier24c19d22012-08-01 18:39:17 +00001755
Chris Lattner1bd44362008-03-11 03:23:40 +00001756 /// 4) Finally, FirstFPRegOp must be in ST(0) and SecondFPRegOp must be in
1757 /// ST(1). Just remove both from our understanding of the stack and return.
1758 assert(getStackEntry(0) == FirstFPRegOp && "Unknown regs live");
Chris Lattnerb6f04a32008-03-21 05:57:20 +00001759 assert(getStackEntry(1) == SecondFPRegOp && "Unknown regs live");
Chris Lattner1bd44362008-03-11 03:23:40 +00001760 StackTop = 0;
1761 return;
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001762 }
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001763
Alkis Evlogimenos80da8652004-02-12 02:27:10 +00001764 I = MBB->erase(I); // Remove the pseudo instruction
Jakob Stoklund Olesen0e5fb022010-07-16 16:38:12 +00001765
1766 // We want to leave I pointing to the previous instruction, but what if we
1767 // just erased the first instruction?
1768 if (I == MBB->begin()) {
1769 DEBUG(dbgs() << "Inserting dummy KILL\n");
1770 I = BuildMI(*MBB, I, DebugLoc(), TII->get(TargetOpcode::KILL));
1771 } else
1772 --I;
Chris Lattnercf53bcf2003-01-13 01:01:59 +00001773}