blob: 873a4d9dc66ec082db387559d9bbf9356a206817 [file] [log] [blame]
Misha Brukman91b5ca82004-07-26 18:45:48 +00001//===-- X86FloatingPoint.cpp - Floating point Reg -> Stack converter ------===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnera960d952003-01-13 01:01:59 +00009//
10// This file defines the pass which converts floating point instructions from
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +000011// pseudo registers into register stack instructions. This pass uses live
Chris Lattner847df252004-01-30 22:25:18 +000012// variable information to indicate where the FPn registers are used and their
13// lifetimes.
14//
Jakob Stoklund Olesene928ec92010-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 Lattner847df252004-01-30 22:25:18 +000020//
Jakob Stoklund Olesene928ec92010-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 Lattnera960d952003-01-13 01:01:59 +000023//
24//===----------------------------------------------------------------------===//
25
Chris Lattner95b2c7d2006-12-19 22:59:26 +000026#define DEBUG_TYPE "x86-codegen"
Chris Lattnera960d952003-01-13 01:01:59 +000027#include "X86.h"
28#include "X86InstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/ADT/DepthFirstIterator.h"
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +000030#include "llvm/ADT/DenseMap.h"
Owen Andersoneaa009d2008-08-14 21:01:00 +000031#include "llvm/ADT/SmallPtrSet.h"
Evan Chengddd2a452006-11-15 20:56:39 +000032#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/STLExtras.h"
Jakob Stoklund Olesen8dd070e2011-01-04 21:10:05 +000035#include "llvm/CodeGen/EdgeBundles.h"
Bill Wendling0ea8bf32009-08-03 00:11:34 +000036#include "llvm/CodeGen/MachineFunctionPass.h"
37#include "llvm/CodeGen/MachineInstrBuilder.h"
38#include "llvm/CodeGen/MachineRegisterInfo.h"
39#include "llvm/CodeGen/Passes.h"
Bill Wendling0ea8bf32009-08-03 00:11:34 +000040#include "llvm/Support/Debug.h"
41#include "llvm/Support/ErrorHandling.h"
42#include "llvm/Support/raw_ostream.h"
43#include "llvm/Target/TargetInstrInfo.h"
44#include "llvm/Target/TargetMachine.h"
Chris Lattnera960d952003-01-13 01:01:59 +000045#include <algorithm>
Chris Lattnerf2e49d42003-12-20 09:58:55 +000046using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000047
Chris Lattner95b2c7d2006-12-19 22:59:26 +000048STATISTIC(NumFXCH, "Number of fxch instructions inserted");
49STATISTIC(NumFP , "Number of floating point instructions");
Chris Lattnera960d952003-01-13 01:01:59 +000050
Chris Lattner95b2c7d2006-12-19 22:59:26 +000051namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000052 struct FPS : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000053 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000054 FPS() : MachineFunctionPass(ID) {
Jakob Stoklund Olesen8dd070e2011-01-04 21:10:05 +000055 initializeEdgeBundlesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesenb47bb132010-07-16 22:00:33 +000056 // This is really only to keep valgrind quiet.
57 // The logic in isLive() is too much for it.
58 memset(Stack, 0, sizeof(Stack));
59 memset(RegMap, 0, sizeof(RegMap));
60 }
Devang Patel794fd752007-05-01 21:15:47 +000061
Evan Chengbbeeb2a2008-09-22 20:58:04 +000062 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmandf090552009-08-01 00:26:16 +000063 AU.setPreservesCFG();
Jakob Stoklund Olesen8dd070e2011-01-04 21:10:05 +000064 AU.addRequired<EdgeBundles>();
Evan Cheng8b56a902008-09-22 22:21:38 +000065 AU.addPreservedID(MachineLoopInfoID);
66 AU.addPreservedID(MachineDominatorsID);
Evan Chengbbeeb2a2008-09-22 20:58:04 +000067 MachineFunctionPass::getAnalysisUsage(AU);
68 }
69
Chris Lattnera960d952003-01-13 01:01:59 +000070 virtual bool runOnMachineFunction(MachineFunction &MF);
71
72 virtual const char *getPassName() const { return "X86 FP Stackifier"; }
73
Chris Lattnera960d952003-01-13 01:01:59 +000074 private:
Evan Cheng32644ac2006-12-01 10:11:51 +000075 const TargetInstrInfo *TII; // Machine instruction info.
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +000076
77 // Two CFG edges are related if they leave the same block, or enter the same
78 // block. The transitive closure of an edge under this relation is a
79 // LiveBundle. It represents a set of CFG edges where the live FP stack
80 // registers must be allocated identically in the x87 stack.
81 //
82 // A LiveBundle is usually all the edges leaving a block, or all the edges
83 // entering a block, but it can contain more edges if critical edges are
84 // present.
85 //
86 // The set of live FP registers in a LiveBundle is calculated by bundleCFG,
87 // but the exact mapping of FP registers to stack slots is fixed later.
88 struct LiveBundle {
89 // Bit mask of live FP registers. Bit 0 = FP0, bit 1 = FP1, &c.
90 unsigned Mask;
91
92 // Number of pre-assigned live registers in FixStack. This is 0 when the
93 // stack order has not yet been fixed.
94 unsigned FixCount;
95
96 // Assigned stack order for live-in registers.
97 // FixStack[i] == getStackEntry(i) for all i < FixCount.
98 unsigned char FixStack[8];
99
100 LiveBundle(unsigned m = 0) : Mask(m), FixCount(0) {}
101
102 // Have the live registers been assigned a stack order yet?
103 bool isFixed() const { return !Mask || FixCount; }
104 };
105
106 // Numbered LiveBundle structs. LiveBundles[0] is used for all CFG edges
107 // with no live FP registers.
108 SmallVector<LiveBundle, 8> LiveBundles;
109
110 // Map each MBB in the current function to an (ingoing, outgoing) index into
111 // LiveBundles. Blocks with no FP registers live in or out map to (0, 0)
112 // and are not actually stored in the map.
113 DenseMap<MachineBasicBlock*, std::pair<unsigned, unsigned> > BlockBundle;
114
115 // Return a bitmask of FP registers in block's live-in list.
116 unsigned calcLiveInMask(MachineBasicBlock *MBB) {
117 unsigned Mask = 0;
118 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
119 E = MBB->livein_end(); I != E; ++I) {
120 unsigned Reg = *I - X86::FP0;
121 if (Reg < 8)
122 Mask |= 1 << Reg;
123 }
124 return Mask;
125 }
126
127 // Partition all the CFG edges into LiveBundles.
128 void bundleCFG(MachineFunction &MF);
129
Evan Cheng32644ac2006-12-01 10:11:51 +0000130 MachineBasicBlock *MBB; // Current basic block
131 unsigned Stack[8]; // FP<n> Registers in each stack slot...
132 unsigned RegMap[8]; // Track which stack slot contains each register
133 unsigned StackTop; // The current top of the FP stack.
Chris Lattnera960d952003-01-13 01:01:59 +0000134
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000135 // Set up our stack model to match the incoming registers to MBB.
136 void setupBlockStack();
137
138 // Shuffle live registers to match the expectations of successor blocks.
139 void finishBlockStack();
140
Chris Lattnera960d952003-01-13 01:01:59 +0000141 void dumpStack() const {
David Greenef5c95a62010-01-05 01:29:34 +0000142 dbgs() << "Stack contents:";
Chris Lattnera960d952003-01-13 01:01:59 +0000143 for (unsigned i = 0; i != StackTop; ++i) {
David Greenef5c95a62010-01-05 01:29:34 +0000144 dbgs() << " FP" << Stack[i];
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000145 assert(RegMap[Stack[i]] == i && "Stack[] doesn't match RegMap[]!");
Chris Lattnera960d952003-01-13 01:01:59 +0000146 }
David Greenef5c95a62010-01-05 01:29:34 +0000147 dbgs() << "\n";
Chris Lattnera960d952003-01-13 01:01:59 +0000148 }
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000149
Chris Lattnera40ce7e2010-07-17 17:40:51 +0000150 /// getSlot - Return the stack slot number a particular register number is
151 /// in.
Chris Lattnera960d952003-01-13 01:01:59 +0000152 unsigned getSlot(unsigned RegNo) const {
153 assert(RegNo < 8 && "Regno out of range!");
154 return RegMap[RegNo];
155 }
156
Chris Lattnera40ce7e2010-07-17 17:40:51 +0000157 /// isLive - Is RegNo currently live in the stack?
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000158 bool isLive(unsigned RegNo) const {
159 unsigned Slot = getSlot(RegNo);
160 return Slot < StackTop && Stack[Slot] == RegNo;
161 }
162
Chris Lattnera40ce7e2010-07-17 17:40:51 +0000163 /// getScratchReg - Return an FP register that is not currently in use.
Jakob Stoklund Olesene098e7a2010-07-16 17:41:40 +0000164 unsigned getScratchReg() {
165 for (int i = 7; i >= 0; --i)
166 if (!isLive(i))
167 return i;
168 llvm_unreachable("Ran out of scratch FP registers");
169 }
170
Chris Lattnera40ce7e2010-07-17 17:40:51 +0000171 /// getStackEntry - Return the X86::FP<n> register in register ST(i).
Chris Lattnera960d952003-01-13 01:01:59 +0000172 unsigned getStackEntry(unsigned STi) const {
Evan Cheng3f490f32010-10-12 23:19:28 +0000173 if (STi >= StackTop)
174 report_fatal_error("Access past stack top!");
Chris Lattnera960d952003-01-13 01:01:59 +0000175 return Stack[StackTop-1-STi];
176 }
177
Chris Lattnera40ce7e2010-07-17 17:40:51 +0000178 /// getSTReg - Return the X86::ST(i) register which contains the specified
179 /// FP<RegNo> register.
Chris Lattnera960d952003-01-13 01:01:59 +0000180 unsigned getSTReg(unsigned RegNo) const {
Brian Gaeked0fde302003-11-11 22:41:34 +0000181 return StackTop - 1 - getSlot(RegNo) + llvm::X86::ST0;
Chris Lattnera960d952003-01-13 01:01:59 +0000182 }
183
Chris Lattner447ff682008-03-11 03:23:40 +0000184 // pushReg - Push the specified FP<n> register onto the stack.
Chris Lattnera960d952003-01-13 01:01:59 +0000185 void pushReg(unsigned Reg) {
186 assert(Reg < 8 && "Register number out of range!");
Evan Cheng3f490f32010-10-12 23:19:28 +0000187 if (StackTop >= 8)
188 report_fatal_error("Stack overflow!");
Chris Lattnera960d952003-01-13 01:01:59 +0000189 Stack[StackTop] = Reg;
190 RegMap[Reg] = StackTop++;
191 }
192
193 bool isAtTop(unsigned RegNo) const { return getSlot(RegNo) == StackTop-1; }
Chris Lattner447ff682008-03-11 03:23:40 +0000194 void moveToTop(unsigned RegNo, MachineBasicBlock::iterator I) {
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000195 DebugLoc dl = I == MBB->end() ? DebugLoc() : I->getDebugLoc();
Chris Lattner447ff682008-03-11 03:23:40 +0000196 if (isAtTop(RegNo)) return;
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000197
Chris Lattner447ff682008-03-11 03:23:40 +0000198 unsigned STReg = getSTReg(RegNo);
199 unsigned RegOnTop = getStackEntry(0);
Chris Lattnera960d952003-01-13 01:01:59 +0000200
Chris Lattner447ff682008-03-11 03:23:40 +0000201 // Swap the slots the regs are in.
202 std::swap(RegMap[RegNo], RegMap[RegOnTop]);
Chris Lattnera960d952003-01-13 01:01:59 +0000203
Chris Lattner447ff682008-03-11 03:23:40 +0000204 // Swap stack slot contents.
Evan Cheng3f490f32010-10-12 23:19:28 +0000205 if (RegMap[RegOnTop] >= StackTop)
206 report_fatal_error("Access past stack top!");
Chris Lattner447ff682008-03-11 03:23:40 +0000207 std::swap(Stack[RegMap[RegOnTop]], Stack[StackTop-1]);
Chris Lattnera960d952003-01-13 01:01:59 +0000208
Chris Lattner447ff682008-03-11 03:23:40 +0000209 // Emit an fxch to update the runtime processors version of the state.
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000210 BuildMI(*MBB, I, dl, TII->get(X86::XCH_F)).addReg(STReg);
Dan Gohmanfe601042010-06-22 15:08:57 +0000211 ++NumFXCH;
Chris Lattnera960d952003-01-13 01:01:59 +0000212 }
213
Chris Lattner0526f012004-04-01 04:06:09 +0000214 void duplicateToTop(unsigned RegNo, unsigned AsReg, MachineInstr *I) {
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000215 DebugLoc dl = I == MBB->end() ? DebugLoc() : I->getDebugLoc();
Chris Lattnera960d952003-01-13 01:01:59 +0000216 unsigned STReg = getSTReg(RegNo);
217 pushReg(AsReg); // New register on top of stack
218
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000219 BuildMI(*MBB, I, dl, TII->get(X86::LD_Frr)).addReg(STReg);
Chris Lattnera960d952003-01-13 01:01:59 +0000220 }
221
Chris Lattnera40ce7e2010-07-17 17:40:51 +0000222 /// popStackAfter - Pop the current value off of the top of the FP stack
223 /// after the specified instruction.
Chris Lattnera960d952003-01-13 01:01:59 +0000224 void popStackAfter(MachineBasicBlock::iterator &I);
225
Chris Lattnera40ce7e2010-07-17 17:40:51 +0000226 /// freeStackSlotAfter - Free the specified register from the register
227 /// stack, so that it is no longer in a register. If the register is
228 /// currently at the top of the stack, we just pop the current instruction,
229 /// otherwise we store the current top-of-stack into the specified slot,
230 /// then pop the top of stack.
Chris Lattner0526f012004-04-01 04:06:09 +0000231 void freeStackSlotAfter(MachineBasicBlock::iterator &I, unsigned Reg);
232
Chris Lattnera40ce7e2010-07-17 17:40:51 +0000233 /// freeStackSlotBefore - Just the pop, no folding. Return the inserted
234 /// instruction.
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000235 MachineBasicBlock::iterator
236 freeStackSlotBefore(MachineBasicBlock::iterator I, unsigned FPRegNo);
237
Chris Lattnera40ce7e2010-07-17 17:40:51 +0000238 /// Adjust the live registers to be the set in Mask.
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000239 void adjustLiveRegs(unsigned Mask, MachineBasicBlock::iterator I);
240
Chris Lattnera40ce7e2010-07-17 17:40:51 +0000241 /// Shuffle the top FixCount stack entries susch that FP reg FixStack[0] is
242 /// st(0), FP reg FixStack[1] is st(1) etc.
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000243 void shuffleStackTop(const unsigned char *FixStack, unsigned FixCount,
244 MachineBasicBlock::iterator I);
245
Chris Lattnera960d952003-01-13 01:01:59 +0000246 bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB);
247
248 void handleZeroArgFP(MachineBasicBlock::iterator &I);
249 void handleOneArgFP(MachineBasicBlock::iterator &I);
Chris Lattner4a06f352004-02-02 19:23:15 +0000250 void handleOneArgFPRW(MachineBasicBlock::iterator &I);
Chris Lattnera960d952003-01-13 01:01:59 +0000251 void handleTwoArgFP(MachineBasicBlock::iterator &I);
Chris Lattnerd62d5d72004-06-11 04:25:06 +0000252 void handleCompareFP(MachineBasicBlock::iterator &I);
Chris Lattnerc1bab322004-03-31 22:02:36 +0000253 void handleCondMovFP(MachineBasicBlock::iterator &I);
Chris Lattnera960d952003-01-13 01:01:59 +0000254 void handleSpecialFP(MachineBasicBlock::iterator &I);
Jakob Stoklund Olesen7db1e7a2010-07-08 19:46:30 +0000255
256 bool translateCopy(MachineInstr*);
Chris Lattnera960d952003-01-13 01:01:59 +0000257 };
Devang Patel19974732007-05-03 01:11:54 +0000258 char FPS::ID = 0;
Chris Lattnera960d952003-01-13 01:01:59 +0000259}
260
Chris Lattnerf2e49d42003-12-20 09:58:55 +0000261FunctionPass *llvm::createX86FloatingPointStackifierPass() { return new FPS(); }
Chris Lattnera960d952003-01-13 01:01:59 +0000262
Chris Lattner3cc83842008-01-14 06:41:29 +0000263/// getFPReg - Return the X86::FPx register number for the specified operand.
264/// For example, this returns 3 for X86::FP3.
265static unsigned getFPReg(const MachineOperand &MO) {
Dan Gohmand735b802008-10-03 15:45:36 +0000266 assert(MO.isReg() && "Expected an FP register!");
Chris Lattner3cc83842008-01-14 06:41:29 +0000267 unsigned Reg = MO.getReg();
268 assert(Reg >= X86::FP0 && Reg <= X86::FP6 && "Expected FP register!");
269 return Reg - X86::FP0;
270}
271
Chris Lattnera960d952003-01-13 01:01:59 +0000272/// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
273/// register references into FP stack references.
274///
275bool FPS::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner42e25b32005-01-23 23:13:59 +0000276 // We only need to run this pass if there are any FP registers used in this
277 // function. If it is all integer, there is nothing for us to do!
Chris Lattner42e25b32005-01-23 23:13:59 +0000278 bool FPIsUsed = false;
279
280 assert(X86::FP6 == X86::FP0+6 && "Register enums aren't sorted right!");
281 for (unsigned i = 0; i <= 6; ++i)
Chris Lattner84bc5422007-12-31 04:13:23 +0000282 if (MF.getRegInfo().isPhysRegUsed(X86::FP0+i)) {
Chris Lattner42e25b32005-01-23 23:13:59 +0000283 FPIsUsed = true;
284 break;
285 }
286
287 // Early exit.
288 if (!FPIsUsed) return false;
289
Evan Cheng32644ac2006-12-01 10:11:51 +0000290 TII = MF.getTarget().getInstrInfo();
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000291
292 // Prepare cross-MBB liveness.
293 bundleCFG(MF);
294
Chris Lattnera960d952003-01-13 01:01:59 +0000295 StackTop = 0;
296
Chris Lattner847df252004-01-30 22:25:18 +0000297 // Process the function in depth first order so that we process at least one
298 // of the predecessors for every reachable block in the function.
Owen Andersoneaa009d2008-08-14 21:01:00 +0000299 SmallPtrSet<MachineBasicBlock*, 8> Processed;
Chris Lattner22686842004-05-01 21:27:53 +0000300 MachineBasicBlock *Entry = MF.begin();
Chris Lattner847df252004-01-30 22:25:18 +0000301
302 bool Changed = false;
Owen Andersoneaa009d2008-08-14 21:01:00 +0000303 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 8> >
Chris Lattner847df252004-01-30 22:25:18 +0000304 I = df_ext_begin(Entry, Processed), E = df_ext_end(Entry, Processed);
305 I != E; ++I)
Chris Lattner22686842004-05-01 21:27:53 +0000306 Changed |= processBasicBlock(MF, **I);
Chris Lattner847df252004-01-30 22:25:18 +0000307
Chris Lattnerba3598c2009-09-08 04:55:44 +0000308 // Process any unreachable blocks in arbitrary order now.
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000309 if (MF.size() != Processed.size())
310 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
311 if (Processed.insert(BB))
312 Changed |= processBasicBlock(MF, *BB);
Chris Lattnerba3598c2009-09-08 04:55:44 +0000313
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000314 BlockBundle.clear();
315 LiveBundles.clear();
316
Chris Lattnera960d952003-01-13 01:01:59 +0000317 return Changed;
318}
319
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000320/// bundleCFG - Scan all the basic blocks to determine consistent live-in and
321/// live-out sets for the FP registers. Consistent means that the set of
322/// registers live-out from a block is identical to the live-in set of all
323/// successors. This is not enforced by the normal live-in lists since
324/// registers may be implicitly defined, or not used by all successors.
325void FPS::bundleCFG(MachineFunction &MF) {
326 assert(LiveBundles.empty() && "Stale data in LiveBundles");
327 assert(BlockBundle.empty() && "Stale data in BlockBundle");
328 SmallPtrSet<MachineBasicBlock*, 8> PropDown, PropUp;
329
330 // LiveBundle[0] is the empty live-in set.
331 LiveBundles.resize(1);
332
333 // First gather the actual live-in masks for all MBBs.
334 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
335 MachineBasicBlock *MBB = I;
336 const unsigned Mask = calcLiveInMask(MBB);
337 if (!Mask)
338 continue;
339 // Ingoing bundle index.
340 unsigned &Idx = BlockBundle[MBB].first;
341 // Already assigned an ingoing bundle?
342 if (Idx)
343 continue;
344 // Allocate a new LiveBundle struct for this block's live-ins.
345 const unsigned BundleIdx = Idx = LiveBundles.size();
346 DEBUG(dbgs() << "Creating LB#" << BundleIdx << ": in:BB#"
347 << MBB->getNumber());
348 LiveBundles.push_back(Mask);
349 LiveBundle &Bundle = LiveBundles.back();
350
351 // Make sure all predecessors have the same live-out set.
352 PropUp.insert(MBB);
353
354 // Keep pushing liveness up and down the CFG until convergence.
355 // Only critical edges cause iteration here, but when they do, multiple
356 // blocks can be assigned to the same LiveBundle index.
357 do {
358 // Assign BundleIdx as liveout from predecessors in PropUp.
359 for (SmallPtrSet<MachineBasicBlock*, 16>::iterator I = PropUp.begin(),
360 E = PropUp.end(); I != E; ++I) {
361 MachineBasicBlock *MBB = *I;
362 for (MachineBasicBlock::const_pred_iterator LinkI = MBB->pred_begin(),
363 LinkE = MBB->pred_end(); LinkI != LinkE; ++LinkI) {
364 MachineBasicBlock *PredMBB = *LinkI;
365 // PredMBB's liveout bundle should be set to LIIdx.
366 unsigned &Idx = BlockBundle[PredMBB].second;
367 if (Idx) {
368 assert(Idx == BundleIdx && "Inconsistent CFG");
369 continue;
370 }
371 Idx = BundleIdx;
372 DEBUG(dbgs() << " out:BB#" << PredMBB->getNumber());
373 // Propagate to siblings.
374 if (PredMBB->succ_size() > 1)
375 PropDown.insert(PredMBB);
376 }
377 }
378 PropUp.clear();
379
380 // Assign BundleIdx as livein to successors in PropDown.
381 for (SmallPtrSet<MachineBasicBlock*, 16>::iterator I = PropDown.begin(),
382 E = PropDown.end(); I != E; ++I) {
383 MachineBasicBlock *MBB = *I;
384 for (MachineBasicBlock::const_succ_iterator LinkI = MBB->succ_begin(),
385 LinkE = MBB->succ_end(); LinkI != LinkE; ++LinkI) {
386 MachineBasicBlock *SuccMBB = *LinkI;
387 // LinkMBB's livein bundle should be set to BundleIdx.
388 unsigned &Idx = BlockBundle[SuccMBB].first;
389 if (Idx) {
390 assert(Idx == BundleIdx && "Inconsistent CFG");
391 continue;
392 }
393 Idx = BundleIdx;
394 DEBUG(dbgs() << " in:BB#" << SuccMBB->getNumber());
395 // Propagate to siblings.
396 if (SuccMBB->pred_size() > 1)
397 PropUp.insert(SuccMBB);
398 // Also accumulate the bundle liveness mask from the liveins here.
399 Bundle.Mask |= calcLiveInMask(SuccMBB);
400 }
401 }
402 PropDown.clear();
403 } while (!PropUp.empty());
404 DEBUG({
405 dbgs() << " live:";
406 for (unsigned i = 0; i < 8; ++i)
407 if (Bundle.Mask & (1<<i))
408 dbgs() << " %FP" << i;
409 dbgs() << '\n';
410 });
411 }
412}
413
Chris Lattnera960d952003-01-13 01:01:59 +0000414/// processBasicBlock - Loop over all of the instructions in the basic block,
415/// transforming FP instructions into their stack form.
416///
417bool FPS::processBasicBlock(MachineFunction &MF, MachineBasicBlock &BB) {
Chris Lattnera960d952003-01-13 01:01:59 +0000418 bool Changed = false;
419 MBB = &BB;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000420
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000421 setupBlockStack();
422
Chris Lattnera960d952003-01-13 01:01:59 +0000423 for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000424 MachineInstr *MI = I;
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +0000425 uint64_t Flags = MI->getDesc().TSFlags;
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000426
Chris Lattnere12ecf22008-03-11 19:50:13 +0000427 unsigned FPInstClass = Flags & X86II::FPTypeMask;
Chris Lattner518bb532010-02-09 19:54:29 +0000428 if (MI->isInlineAsm())
Chris Lattnere12ecf22008-03-11 19:50:13 +0000429 FPInstClass = X86II::SpecialFP;
Jakob Stoklund Olesen7db1e7a2010-07-08 19:46:30 +0000430
431 if (MI->isCopy() && translateCopy(MI))
432 FPInstClass = X86II::SpecialFP;
433
Chris Lattnere12ecf22008-03-11 19:50:13 +0000434 if (FPInstClass == X86II::NotFP)
Chris Lattner847df252004-01-30 22:25:18 +0000435 continue; // Efficiently ignore non-fp insts!
Chris Lattnera960d952003-01-13 01:01:59 +0000436
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000437 MachineInstr *PrevMI = 0;
Alkis Evlogimenosf81af212004-02-14 01:18:34 +0000438 if (I != BB.begin())
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000439 PrevMI = prior(I);
Chris Lattnera960d952003-01-13 01:01:59 +0000440
441 ++NumFP; // Keep track of # of pseudo instrs
David Greenef5c95a62010-01-05 01:29:34 +0000442 DEBUG(dbgs() << "\nFPInst:\t" << *MI);
Chris Lattnera960d952003-01-13 01:01:59 +0000443
444 // Get dead variables list now because the MI pointer may be deleted as part
445 // of processing!
Evan Chengddd2a452006-11-15 20:56:39 +0000446 SmallVector<unsigned, 8> DeadRegs;
447 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
448 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000449 if (MO.isReg() && MO.isDead())
Evan Chengddd2a452006-11-15 20:56:39 +0000450 DeadRegs.push_back(MO.getReg());
451 }
Chris Lattnera960d952003-01-13 01:01:59 +0000452
Chris Lattnere12ecf22008-03-11 19:50:13 +0000453 switch (FPInstClass) {
Chris Lattner4a06f352004-02-02 19:23:15 +0000454 case X86II::ZeroArgFP: handleZeroArgFP(I); break;
Chris Lattnerc1bab322004-03-31 22:02:36 +0000455 case X86II::OneArgFP: handleOneArgFP(I); break; // fstp ST(0)
Chris Lattner4a06f352004-02-02 19:23:15 +0000456 case X86II::OneArgFPRW: handleOneArgFPRW(I); break; // ST(0) = fsqrt(ST(0))
Evan Cheng5cd3e9f2006-11-11 10:21:44 +0000457 case X86II::TwoArgFP: handleTwoArgFP(I); break;
Chris Lattnerab8decc2004-06-11 04:41:24 +0000458 case X86II::CompareFP: handleCompareFP(I); break;
Chris Lattnerc1bab322004-03-31 22:02:36 +0000459 case X86II::CondMovFP: handleCondMovFP(I); break;
Chris Lattner4a06f352004-02-02 19:23:15 +0000460 case X86II::SpecialFP: handleSpecialFP(I); break;
Torok Edwinc23197a2009-07-14 16:55:14 +0000461 default: llvm_unreachable("Unknown FP Type!");
Chris Lattnera960d952003-01-13 01:01:59 +0000462 }
463
464 // Check to see if any of the values defined by this instruction are dead
465 // after definition. If so, pop them.
Evan Chengddd2a452006-11-15 20:56:39 +0000466 for (unsigned i = 0, e = DeadRegs.size(); i != e; ++i) {
467 unsigned Reg = DeadRegs[i];
Chris Lattnera960d952003-01-13 01:01:59 +0000468 if (Reg >= X86::FP0 && Reg <= X86::FP6) {
David Greenef5c95a62010-01-05 01:29:34 +0000469 DEBUG(dbgs() << "Register FP#" << Reg-X86::FP0 << " is dead!\n");
Chris Lattnerd62d5d72004-06-11 04:25:06 +0000470 freeStackSlotAfter(I, Reg-X86::FP0);
Chris Lattnera960d952003-01-13 01:01:59 +0000471 }
472 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000473
Chris Lattnera960d952003-01-13 01:01:59 +0000474 // Print out all of the instructions expanded to if -debug
Alkis Evlogimenosb929bca2004-02-15 00:46:41 +0000475 DEBUG(
476 MachineBasicBlock::iterator PrevI(PrevMI);
477 if (I == PrevI) {
David Greenef5c95a62010-01-05 01:29:34 +0000478 dbgs() << "Just deleted pseudo instruction\n";
Alkis Evlogimenosb929bca2004-02-15 00:46:41 +0000479 } else {
480 MachineBasicBlock::iterator Start = I;
481 // Rewind to first instruction newly inserted.
482 while (Start != BB.begin() && prior(Start) != PrevI) --Start;
David Greenef5c95a62010-01-05 01:29:34 +0000483 dbgs() << "Inserted instructions:\n\t";
484 Start->print(dbgs(), &MF.getTarget());
Chris Lattner7896c9f2009-12-03 00:50:42 +0000485 while (++Start != llvm::next(I)) {}
Alkis Evlogimenosb929bca2004-02-15 00:46:41 +0000486 }
487 dumpStack();
488 );
Chris Lattnera960d952003-01-13 01:01:59 +0000489
490 Changed = true;
491 }
492
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000493 finishBlockStack();
494
Chris Lattnera960d952003-01-13 01:01:59 +0000495 return Changed;
496}
497
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000498/// setupBlockStack - Use the BlockBundle map to set up our model of the stack
499/// to match predecessors' live out stack.
500void FPS::setupBlockStack() {
501 DEBUG(dbgs() << "\nSetting up live-ins for BB#" << MBB->getNumber()
502 << " derived from " << MBB->getName() << ".\n");
503 StackTop = 0;
504 const LiveBundle &Bundle = LiveBundles[BlockBundle.lookup(MBB).first];
505
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
541 unsigned BundleIdx = BlockBundle.lookup(MBB).second;
542 LiveBundle &Bundle = LiveBundles[BundleIdx];
543
544 // We may need to kill and define some registers to match successors.
545 // FIXME: This can probably be combined with the shuffle below.
546 MachineBasicBlock::iterator Term = MBB->getFirstTerminator();
547 adjustLiveRegs(Bundle.Mask, Term);
548
549 if (!Bundle.Mask) {
550 DEBUG(dbgs() << "No live-outs.\n");
551 return;
552 }
553
554 // Has the stack order been fixed yet?
555 DEBUG(dbgs() << "LB#" << BundleIdx << ": ");
556 if (Bundle.isFixed()) {
557 DEBUG(dbgs() << "Shuffling stack to match.\n");
558 shuffleStackTop(Bundle.FixStack, Bundle.FixCount, Term);
559 } else {
560 // Not fixed yet, we get to choose.
561 DEBUG(dbgs() << "Fixing stack order now.\n");
562 Bundle.FixCount = StackTop;
563 for (unsigned i = 0; i < StackTop; ++i)
564 Bundle.FixStack[i] = getStackEntry(i);
565 }
566}
567
568
Chris Lattnera960d952003-01-13 01:01:59 +0000569//===----------------------------------------------------------------------===//
570// Efficient Lookup Table Support
571//===----------------------------------------------------------------------===//
572
Chris Lattnerf2e49d42003-12-20 09:58:55 +0000573namespace {
574 struct TableEntry {
575 unsigned from;
576 unsigned to;
577 bool operator<(const TableEntry &TE) const { return from < TE.from; }
Jeff Cohen9471c8a2006-01-26 20:41:32 +0000578 friend bool operator<(const TableEntry &TE, unsigned V) {
579 return TE.from < V;
580 }
Chandler Carruth100c2672010-10-23 08:10:43 +0000581 friend bool LLVM_ATTRIBUTE_USED operator<(unsigned V,
582 const TableEntry &TE) {
Jakob Stoklund Olesende78f052010-08-16 18:24:54 +0000583 return V < TE.from;
584 }
Chris Lattnerf2e49d42003-12-20 09:58:55 +0000585 };
586}
Chris Lattnera960d952003-01-13 01:01:59 +0000587
Evan Chenga022bdf2008-07-21 20:02:45 +0000588#ifndef NDEBUG
Chris Lattnera960d952003-01-13 01:01:59 +0000589static bool TableIsSorted(const TableEntry *Table, unsigned NumEntries) {
590 for (unsigned i = 0; i != NumEntries-1; ++i)
591 if (!(Table[i] < Table[i+1])) return false;
592 return true;
593}
Evan Chenga022bdf2008-07-21 20:02:45 +0000594#endif
Chris Lattnera960d952003-01-13 01:01:59 +0000595
596static int Lookup(const TableEntry *Table, unsigned N, unsigned Opcode) {
597 const TableEntry *I = std::lower_bound(Table, Table+N, Opcode);
598 if (I != Table+N && I->from == Opcode)
599 return I->to;
600 return -1;
601}
602
Chris Lattnera960d952003-01-13 01:01:59 +0000603#ifdef NDEBUG
604#define ASSERT_SORTED(TABLE)
605#else
606#define ASSERT_SORTED(TABLE) \
607 { static bool TABLE##Checked = false; \
Jim Laskeyc06fe8a2006-07-19 19:33:08 +0000608 if (!TABLE##Checked) { \
Owen Anderson718cb662007-09-07 04:06:50 +0000609 assert(TableIsSorted(TABLE, array_lengthof(TABLE)) && \
Chris Lattnera960d952003-01-13 01:01:59 +0000610 "All lookup tables must be sorted for efficient access!"); \
Jim Laskeyc06fe8a2006-07-19 19:33:08 +0000611 TABLE##Checked = true; \
612 } \
Chris Lattnera960d952003-01-13 01:01:59 +0000613 }
614#endif
615
Chris Lattner58fe4592005-12-21 07:47:04 +0000616//===----------------------------------------------------------------------===//
617// Register File -> Register Stack Mapping Methods
618//===----------------------------------------------------------------------===//
619
620// OpcodeTable - Sorted map of register instructions to their stack version.
621// The first element is an register file pseudo instruction, the second is the
622// concrete X86 instruction which uses the register stack.
623//
624static const TableEntry OpcodeTable[] = {
Dale Johannesene377d4d2007-07-04 21:07:47 +0000625 { X86::ABS_Fp32 , X86::ABS_F },
626 { X86::ABS_Fp64 , X86::ABS_F },
Dale Johannesen59a58732007-08-05 18:49:15 +0000627 { X86::ABS_Fp80 , X86::ABS_F },
Dale Johannesenafdc7fd2007-07-10 21:53:30 +0000628 { X86::ADD_Fp32m , X86::ADD_F32m },
629 { X86::ADD_Fp64m , X86::ADD_F64m },
630 { X86::ADD_Fp64m32 , X86::ADD_F32m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000631 { X86::ADD_Fp80m32 , X86::ADD_F32m },
632 { X86::ADD_Fp80m64 , X86::ADD_F64m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000633 { X86::ADD_FpI16m32 , X86::ADD_FI16m },
634 { X86::ADD_FpI16m64 , X86::ADD_FI16m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000635 { X86::ADD_FpI16m80 , X86::ADD_FI16m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000636 { X86::ADD_FpI32m32 , X86::ADD_FI32m },
637 { X86::ADD_FpI32m64 , X86::ADD_FI32m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000638 { X86::ADD_FpI32m80 , X86::ADD_FI32m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000639 { X86::CHS_Fp32 , X86::CHS_F },
640 { X86::CHS_Fp64 , X86::CHS_F },
Dale Johannesen59a58732007-08-05 18:49:15 +0000641 { X86::CHS_Fp80 , X86::CHS_F },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000642 { X86::CMOVBE_Fp32 , X86::CMOVBE_F },
643 { X86::CMOVBE_Fp64 , X86::CMOVBE_F },
Dale Johannesen59a58732007-08-05 18:49:15 +0000644 { X86::CMOVBE_Fp80 , X86::CMOVBE_F },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000645 { X86::CMOVB_Fp32 , X86::CMOVB_F },
646 { X86::CMOVB_Fp64 , X86::CMOVB_F },
Dale Johannesen59a58732007-08-05 18:49:15 +0000647 { X86::CMOVB_Fp80 , X86::CMOVB_F },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000648 { X86::CMOVE_Fp32 , X86::CMOVE_F },
649 { X86::CMOVE_Fp64 , X86::CMOVE_F },
Dale Johannesen59a58732007-08-05 18:49:15 +0000650 { X86::CMOVE_Fp80 , X86::CMOVE_F },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000651 { X86::CMOVNBE_Fp32 , X86::CMOVNBE_F },
652 { X86::CMOVNBE_Fp64 , X86::CMOVNBE_F },
Dale Johannesen59a58732007-08-05 18:49:15 +0000653 { X86::CMOVNBE_Fp80 , X86::CMOVNBE_F },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000654 { X86::CMOVNB_Fp32 , X86::CMOVNB_F },
655 { X86::CMOVNB_Fp64 , X86::CMOVNB_F },
Dale Johannesen59a58732007-08-05 18:49:15 +0000656 { X86::CMOVNB_Fp80 , X86::CMOVNB_F },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000657 { X86::CMOVNE_Fp32 , X86::CMOVNE_F },
658 { X86::CMOVNE_Fp64 , X86::CMOVNE_F },
Dale Johannesen59a58732007-08-05 18:49:15 +0000659 { X86::CMOVNE_Fp80 , X86::CMOVNE_F },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000660 { X86::CMOVNP_Fp32 , X86::CMOVNP_F },
661 { X86::CMOVNP_Fp64 , X86::CMOVNP_F },
Dale Johannesen59a58732007-08-05 18:49:15 +0000662 { X86::CMOVNP_Fp80 , X86::CMOVNP_F },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000663 { X86::CMOVP_Fp32 , X86::CMOVP_F },
664 { X86::CMOVP_Fp64 , X86::CMOVP_F },
Dale Johannesen59a58732007-08-05 18:49:15 +0000665 { X86::CMOVP_Fp80 , X86::CMOVP_F },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000666 { X86::COS_Fp32 , X86::COS_F },
667 { X86::COS_Fp64 , X86::COS_F },
Dale Johannesen59a58732007-08-05 18:49:15 +0000668 { X86::COS_Fp80 , X86::COS_F },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000669 { X86::DIVR_Fp32m , X86::DIVR_F32m },
670 { X86::DIVR_Fp64m , X86::DIVR_F64m },
Dale Johannesenafdc7fd2007-07-10 21:53:30 +0000671 { X86::DIVR_Fp64m32 , X86::DIVR_F32m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000672 { X86::DIVR_Fp80m32 , X86::DIVR_F32m },
673 { X86::DIVR_Fp80m64 , X86::DIVR_F64m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000674 { X86::DIVR_FpI16m32, X86::DIVR_FI16m},
675 { X86::DIVR_FpI16m64, X86::DIVR_FI16m},
Dale Johannesen59a58732007-08-05 18:49:15 +0000676 { X86::DIVR_FpI16m80, X86::DIVR_FI16m},
Dale Johannesene377d4d2007-07-04 21:07:47 +0000677 { X86::DIVR_FpI32m32, X86::DIVR_FI32m},
678 { X86::DIVR_FpI32m64, X86::DIVR_FI32m},
Dale Johannesen59a58732007-08-05 18:49:15 +0000679 { X86::DIVR_FpI32m80, X86::DIVR_FI32m},
Dale Johannesene377d4d2007-07-04 21:07:47 +0000680 { X86::DIV_Fp32m , X86::DIV_F32m },
681 { X86::DIV_Fp64m , X86::DIV_F64m },
Dale Johannesenafdc7fd2007-07-10 21:53:30 +0000682 { X86::DIV_Fp64m32 , X86::DIV_F32m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000683 { X86::DIV_Fp80m32 , X86::DIV_F32m },
684 { X86::DIV_Fp80m64 , X86::DIV_F64m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000685 { X86::DIV_FpI16m32 , X86::DIV_FI16m },
686 { X86::DIV_FpI16m64 , X86::DIV_FI16m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000687 { X86::DIV_FpI16m80 , X86::DIV_FI16m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000688 { X86::DIV_FpI32m32 , X86::DIV_FI32m },
689 { X86::DIV_FpI32m64 , X86::DIV_FI32m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000690 { X86::DIV_FpI32m80 , X86::DIV_FI32m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000691 { X86::ILD_Fp16m32 , X86::ILD_F16m },
692 { X86::ILD_Fp16m64 , X86::ILD_F16m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000693 { X86::ILD_Fp16m80 , X86::ILD_F16m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000694 { X86::ILD_Fp32m32 , X86::ILD_F32m },
695 { X86::ILD_Fp32m64 , X86::ILD_F32m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000696 { X86::ILD_Fp32m80 , X86::ILD_F32m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000697 { X86::ILD_Fp64m32 , X86::ILD_F64m },
698 { X86::ILD_Fp64m64 , X86::ILD_F64m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000699 { X86::ILD_Fp64m80 , X86::ILD_F64m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000700 { X86::ISTT_Fp16m32 , X86::ISTT_FP16m},
701 { X86::ISTT_Fp16m64 , X86::ISTT_FP16m},
Dale Johannesena996d522007-08-07 01:17:37 +0000702 { X86::ISTT_Fp16m80 , X86::ISTT_FP16m},
Dale Johannesene377d4d2007-07-04 21:07:47 +0000703 { X86::ISTT_Fp32m32 , X86::ISTT_FP32m},
704 { X86::ISTT_Fp32m64 , X86::ISTT_FP32m},
Dale Johannesena996d522007-08-07 01:17:37 +0000705 { X86::ISTT_Fp32m80 , X86::ISTT_FP32m},
Dale Johannesene377d4d2007-07-04 21:07:47 +0000706 { X86::ISTT_Fp64m32 , X86::ISTT_FP64m},
707 { X86::ISTT_Fp64m64 , X86::ISTT_FP64m},
Dale Johannesena996d522007-08-07 01:17:37 +0000708 { X86::ISTT_Fp64m80 , X86::ISTT_FP64m},
Dale Johannesene377d4d2007-07-04 21:07:47 +0000709 { X86::IST_Fp16m32 , X86::IST_F16m },
710 { X86::IST_Fp16m64 , X86::IST_F16m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000711 { X86::IST_Fp16m80 , X86::IST_F16m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000712 { X86::IST_Fp32m32 , X86::IST_F32m },
713 { X86::IST_Fp32m64 , X86::IST_F32m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000714 { X86::IST_Fp32m80 , X86::IST_F32m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000715 { X86::IST_Fp64m32 , X86::IST_FP64m },
716 { X86::IST_Fp64m64 , X86::IST_FP64m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000717 { X86::IST_Fp64m80 , X86::IST_FP64m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000718 { X86::LD_Fp032 , X86::LD_F0 },
719 { X86::LD_Fp064 , X86::LD_F0 },
Dale Johannesen59a58732007-08-05 18:49:15 +0000720 { X86::LD_Fp080 , X86::LD_F0 },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000721 { X86::LD_Fp132 , X86::LD_F1 },
722 { X86::LD_Fp164 , X86::LD_F1 },
Dale Johannesen59a58732007-08-05 18:49:15 +0000723 { X86::LD_Fp180 , X86::LD_F1 },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000724 { X86::LD_Fp32m , X86::LD_F32m },
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000725 { X86::LD_Fp32m64 , X86::LD_F32m },
726 { X86::LD_Fp32m80 , X86::LD_F32m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000727 { X86::LD_Fp64m , X86::LD_F64m },
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000728 { X86::LD_Fp64m80 , X86::LD_F64m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000729 { X86::LD_Fp80m , X86::LD_F80m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000730 { X86::MUL_Fp32m , X86::MUL_F32m },
731 { X86::MUL_Fp64m , X86::MUL_F64m },
Dale Johannesenafdc7fd2007-07-10 21:53:30 +0000732 { X86::MUL_Fp64m32 , X86::MUL_F32m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000733 { X86::MUL_Fp80m32 , X86::MUL_F32m },
734 { X86::MUL_Fp80m64 , X86::MUL_F64m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000735 { X86::MUL_FpI16m32 , X86::MUL_FI16m },
736 { X86::MUL_FpI16m64 , X86::MUL_FI16m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000737 { X86::MUL_FpI16m80 , X86::MUL_FI16m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000738 { X86::MUL_FpI32m32 , X86::MUL_FI32m },
739 { X86::MUL_FpI32m64 , X86::MUL_FI32m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000740 { X86::MUL_FpI32m80 , X86::MUL_FI32m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000741 { X86::SIN_Fp32 , X86::SIN_F },
742 { X86::SIN_Fp64 , X86::SIN_F },
Dale Johannesen59a58732007-08-05 18:49:15 +0000743 { X86::SIN_Fp80 , X86::SIN_F },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000744 { X86::SQRT_Fp32 , X86::SQRT_F },
745 { X86::SQRT_Fp64 , X86::SQRT_F },
Dale Johannesen59a58732007-08-05 18:49:15 +0000746 { X86::SQRT_Fp80 , X86::SQRT_F },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000747 { X86::ST_Fp32m , X86::ST_F32m },
748 { X86::ST_Fp64m , X86::ST_F64m },
749 { X86::ST_Fp64m32 , X86::ST_F32m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000750 { X86::ST_Fp80m32 , X86::ST_F32m },
751 { X86::ST_Fp80m64 , X86::ST_F64m },
752 { X86::ST_FpP80m , X86::ST_FP80m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000753 { X86::SUBR_Fp32m , X86::SUBR_F32m },
754 { X86::SUBR_Fp64m , X86::SUBR_F64m },
Dale Johannesenafdc7fd2007-07-10 21:53:30 +0000755 { X86::SUBR_Fp64m32 , X86::SUBR_F32m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000756 { X86::SUBR_Fp80m32 , X86::SUBR_F32m },
757 { X86::SUBR_Fp80m64 , X86::SUBR_F64m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000758 { X86::SUBR_FpI16m32, X86::SUBR_FI16m},
759 { X86::SUBR_FpI16m64, X86::SUBR_FI16m},
Dale Johannesen59a58732007-08-05 18:49:15 +0000760 { X86::SUBR_FpI16m80, X86::SUBR_FI16m},
Dale Johannesene377d4d2007-07-04 21:07:47 +0000761 { X86::SUBR_FpI32m32, X86::SUBR_FI32m},
762 { X86::SUBR_FpI32m64, X86::SUBR_FI32m},
Dale Johannesen59a58732007-08-05 18:49:15 +0000763 { X86::SUBR_FpI32m80, X86::SUBR_FI32m},
Dale Johannesene377d4d2007-07-04 21:07:47 +0000764 { X86::SUB_Fp32m , X86::SUB_F32m },
765 { X86::SUB_Fp64m , X86::SUB_F64m },
Dale Johannesenafdc7fd2007-07-10 21:53:30 +0000766 { X86::SUB_Fp64m32 , X86::SUB_F32m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000767 { X86::SUB_Fp80m32 , X86::SUB_F32m },
768 { X86::SUB_Fp80m64 , X86::SUB_F64m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000769 { X86::SUB_FpI16m32 , X86::SUB_FI16m },
770 { X86::SUB_FpI16m64 , X86::SUB_FI16m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000771 { X86::SUB_FpI16m80 , X86::SUB_FI16m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000772 { X86::SUB_FpI32m32 , X86::SUB_FI32m },
773 { X86::SUB_FpI32m64 , X86::SUB_FI32m },
Dale Johannesen59a58732007-08-05 18:49:15 +0000774 { X86::SUB_FpI32m80 , X86::SUB_FI32m },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000775 { X86::TST_Fp32 , X86::TST_F },
776 { X86::TST_Fp64 , X86::TST_F },
Dale Johannesen59a58732007-08-05 18:49:15 +0000777 { X86::TST_Fp80 , X86::TST_F },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000778 { X86::UCOM_FpIr32 , X86::UCOM_FIr },
779 { X86::UCOM_FpIr64 , X86::UCOM_FIr },
Dale Johannesen59a58732007-08-05 18:49:15 +0000780 { X86::UCOM_FpIr80 , X86::UCOM_FIr },
Dale Johannesene377d4d2007-07-04 21:07:47 +0000781 { X86::UCOM_Fpr32 , X86::UCOM_Fr },
782 { X86::UCOM_Fpr64 , X86::UCOM_Fr },
Dale Johannesen59a58732007-08-05 18:49:15 +0000783 { X86::UCOM_Fpr80 , X86::UCOM_Fr },
Chris Lattner58fe4592005-12-21 07:47:04 +0000784};
785
786static unsigned getConcreteOpcode(unsigned Opcode) {
787 ASSERT_SORTED(OpcodeTable);
Owen Anderson718cb662007-09-07 04:06:50 +0000788 int Opc = Lookup(OpcodeTable, array_lengthof(OpcodeTable), Opcode);
Chris Lattner58fe4592005-12-21 07:47:04 +0000789 assert(Opc != -1 && "FP Stack instruction not in OpcodeTable!");
790 return Opc;
791}
Chris Lattnera960d952003-01-13 01:01:59 +0000792
793//===----------------------------------------------------------------------===//
794// Helper Methods
795//===----------------------------------------------------------------------===//
796
797// PopTable - Sorted map of instructions to their popping version. The first
798// element is an instruction, the second is the version which pops.
799//
800static const TableEntry PopTable[] = {
Dale Johannesene377d4d2007-07-04 21:07:47 +0000801 { X86::ADD_FrST0 , X86::ADD_FPrST0 },
Chris Lattner113455b2003-08-03 21:56:36 +0000802
Dale Johannesene377d4d2007-07-04 21:07:47 +0000803 { X86::DIVR_FrST0, X86::DIVR_FPrST0 },
804 { X86::DIV_FrST0 , X86::DIV_FPrST0 },
Chris Lattner113455b2003-08-03 21:56:36 +0000805
Dale Johannesene377d4d2007-07-04 21:07:47 +0000806 { X86::IST_F16m , X86::IST_FP16m },
807 { X86::IST_F32m , X86::IST_FP32m },
Chris Lattnera960d952003-01-13 01:01:59 +0000808
Dale Johannesene377d4d2007-07-04 21:07:47 +0000809 { X86::MUL_FrST0 , X86::MUL_FPrST0 },
Chris Lattnera960d952003-01-13 01:01:59 +0000810
Dale Johannesene377d4d2007-07-04 21:07:47 +0000811 { X86::ST_F32m , X86::ST_FP32m },
812 { X86::ST_F64m , X86::ST_FP64m },
813 { X86::ST_Frr , X86::ST_FPrr },
Chris Lattner113455b2003-08-03 21:56:36 +0000814
Dale Johannesene377d4d2007-07-04 21:07:47 +0000815 { X86::SUBR_FrST0, X86::SUBR_FPrST0 },
816 { X86::SUB_FrST0 , X86::SUB_FPrST0 },
Chris Lattner113455b2003-08-03 21:56:36 +0000817
Dale Johannesene377d4d2007-07-04 21:07:47 +0000818 { X86::UCOM_FIr , X86::UCOM_FIPr },
Chris Lattnerc040bca2004-04-12 01:39:15 +0000819
Dale Johannesene377d4d2007-07-04 21:07:47 +0000820 { X86::UCOM_FPr , X86::UCOM_FPPr },
821 { X86::UCOM_Fr , X86::UCOM_FPr },
Chris Lattnera960d952003-01-13 01:01:59 +0000822};
823
824/// popStackAfter - Pop the current value off of the top of the FP stack after
825/// the specified instruction. This attempts to be sneaky and combine the pop
826/// into the instruction itself if possible. The iterator is left pointing to
827/// the last instruction, be it a new pop instruction inserted, or the old
828/// instruction if it was modified in place.
829///
830void FPS::popStackAfter(MachineBasicBlock::iterator &I) {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000831 MachineInstr* MI = I;
832 DebugLoc dl = MI->getDebugLoc();
Chris Lattnera960d952003-01-13 01:01:59 +0000833 ASSERT_SORTED(PopTable);
Evan Cheng3f490f32010-10-12 23:19:28 +0000834 if (StackTop == 0)
835 report_fatal_error("Cannot pop empty stack!");
Chris Lattnera960d952003-01-13 01:01:59 +0000836 RegMap[Stack[--StackTop]] = ~0; // Update state
837
838 // Check to see if there is a popping version of this instruction...
Owen Anderson718cb662007-09-07 04:06:50 +0000839 int Opcode = Lookup(PopTable, array_lengthof(PopTable), I->getOpcode());
Chris Lattnera960d952003-01-13 01:01:59 +0000840 if (Opcode != -1) {
Chris Lattner5080f4d2008-01-11 18:10:50 +0000841 I->setDesc(TII->get(Opcode));
Dale Johannesene377d4d2007-07-04 21:07:47 +0000842 if (Opcode == X86::UCOM_FPPr)
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000843 I->RemoveOperand(0);
Chris Lattnera960d952003-01-13 01:01:59 +0000844 } else { // Insert an explicit pop
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000845 I = BuildMI(*MBB, ++I, dl, TII->get(X86::ST_FPrr)).addReg(X86::ST0);
Chris Lattnera960d952003-01-13 01:01:59 +0000846 }
847}
848
Chris Lattner0526f012004-04-01 04:06:09 +0000849/// freeStackSlotAfter - Free the specified register from the register stack, so
850/// that it is no longer in a register. If the register is currently at the top
851/// of the stack, we just pop the current instruction, otherwise we store the
852/// current top-of-stack into the specified slot, then pop the top of stack.
853void FPS::freeStackSlotAfter(MachineBasicBlock::iterator &I, unsigned FPRegNo) {
854 if (getStackEntry(0) == FPRegNo) { // already at the top of stack? easy.
855 popStackAfter(I);
856 return;
857 }
858
859 // Otherwise, store the top of stack into the dead slot, killing the operand
860 // without having to add in an explicit xchg then pop.
861 //
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000862 I = freeStackSlotBefore(++I, FPRegNo);
863}
864
865/// freeStackSlotBefore - Free the specified register without trying any
866/// folding.
867MachineBasicBlock::iterator
868FPS::freeStackSlotBefore(MachineBasicBlock::iterator I, unsigned FPRegNo) {
Chris Lattner0526f012004-04-01 04:06:09 +0000869 unsigned STReg = getSTReg(FPRegNo);
870 unsigned OldSlot = getSlot(FPRegNo);
871 unsigned TopReg = Stack[StackTop-1];
872 Stack[OldSlot] = TopReg;
873 RegMap[TopReg] = OldSlot;
874 RegMap[FPRegNo] = ~0;
875 Stack[--StackTop] = ~0;
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +0000876 return BuildMI(*MBB, I, DebugLoc(), TII->get(X86::ST_FPrr)).addReg(STReg);
877}
878
879/// adjustLiveRegs - Kill and revive registers such that exactly the FP
880/// registers with a bit in Mask are live.
881void FPS::adjustLiveRegs(unsigned Mask, MachineBasicBlock::iterator I) {
882 unsigned Defs = Mask;
883 unsigned Kills = 0;
884 for (unsigned i = 0; i < StackTop; ++i) {
885 unsigned RegNo = Stack[i];
886 if (!(Defs & (1 << RegNo)))
887 // This register is live, but we don't want it.
888 Kills |= (1 << RegNo);
889 else
890 // We don't need to imp-def this live register.
891 Defs &= ~(1 << RegNo);
892 }
893 assert((Kills & Defs) == 0 && "Register needs killing and def'ing?");
894
895 // Produce implicit-defs for free by using killed registers.
896 while (Kills && Defs) {
897 unsigned KReg = CountTrailingZeros_32(Kills);
898 unsigned DReg = CountTrailingZeros_32(Defs);
899 DEBUG(dbgs() << "Renaming %FP" << KReg << " as imp %FP" << DReg << "\n");
900 std::swap(Stack[getSlot(KReg)], Stack[getSlot(DReg)]);
901 std::swap(RegMap[KReg], RegMap[DReg]);
902 Kills &= ~(1 << KReg);
903 Defs &= ~(1 << DReg);
904 }
905
906 // Kill registers by popping.
907 if (Kills && I != MBB->begin()) {
908 MachineBasicBlock::iterator I2 = llvm::prior(I);
909 for (;;) {
910 unsigned KReg = getStackEntry(0);
911 if (!(Kills & (1 << KReg)))
912 break;
913 DEBUG(dbgs() << "Popping %FP" << KReg << "\n");
914 popStackAfter(I2);
915 Kills &= ~(1 << KReg);
916 }
917 }
918
919 // Manually kill the rest.
920 while (Kills) {
921 unsigned KReg = CountTrailingZeros_32(Kills);
922 DEBUG(dbgs() << "Killing %FP" << KReg << "\n");
923 freeStackSlotBefore(I, KReg);
924 Kills &= ~(1 << KReg);
925 }
926
927 // Load zeros for all the imp-defs.
928 while(Defs) {
929 unsigned DReg = CountTrailingZeros_32(Defs);
930 DEBUG(dbgs() << "Defining %FP" << DReg << " as 0\n");
931 BuildMI(*MBB, I, DebugLoc(), TII->get(X86::LD_F0));
932 pushReg(DReg);
933 Defs &= ~(1 << DReg);
934 }
935
936 // Now we should have the correct registers live.
937 DEBUG(dumpStack());
938 assert(StackTop == CountPopulation_32(Mask) && "Live count mismatch");
939}
940
941/// shuffleStackTop - emit fxch instructions before I to shuffle the top
942/// FixCount entries into the order given by FixStack.
943/// FIXME: Is there a better algorithm than insertion sort?
944void FPS::shuffleStackTop(const unsigned char *FixStack,
945 unsigned FixCount,
946 MachineBasicBlock::iterator I) {
947 // Move items into place, starting from the desired stack bottom.
948 while (FixCount--) {
949 // Old register at position FixCount.
950 unsigned OldReg = getStackEntry(FixCount);
951 // Desired register at position FixCount.
952 unsigned Reg = FixStack[FixCount];
953 if (Reg == OldReg)
954 continue;
955 // (Reg st0) (OldReg st0) = (Reg OldReg st0)
956 moveToTop(Reg, I);
957 moveToTop(OldReg, I);
958 }
959 DEBUG(dumpStack());
Chris Lattner0526f012004-04-01 04:06:09 +0000960}
961
962
Chris Lattnera960d952003-01-13 01:01:59 +0000963//===----------------------------------------------------------------------===//
964// Instruction transformation implementation
965//===----------------------------------------------------------------------===//
966
967/// handleZeroArgFP - ST(0) = fld0 ST(0) = flds <mem>
Chris Lattner4a06f352004-02-02 19:23:15 +0000968///
Chris Lattnera960d952003-01-13 01:01:59 +0000969void FPS::handleZeroArgFP(MachineBasicBlock::iterator &I) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000970 MachineInstr *MI = I;
Chris Lattnera960d952003-01-13 01:01:59 +0000971 unsigned DestReg = getFPReg(MI->getOperand(0));
Chris Lattnera960d952003-01-13 01:01:59 +0000972
Chris Lattner58fe4592005-12-21 07:47:04 +0000973 // Change from the pseudo instruction to the concrete instruction.
974 MI->RemoveOperand(0); // Remove the explicit ST(0) operand
Chris Lattner5080f4d2008-01-11 18:10:50 +0000975 MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
Chris Lattner58fe4592005-12-21 07:47:04 +0000976
977 // Result gets pushed on the stack.
Chris Lattnera960d952003-01-13 01:01:59 +0000978 pushReg(DestReg);
979}
980
Chris Lattner4a06f352004-02-02 19:23:15 +0000981/// handleOneArgFP - fst <mem>, ST(0)
982///
Chris Lattnera960d952003-01-13 01:01:59 +0000983void FPS::handleOneArgFP(MachineBasicBlock::iterator &I) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000984 MachineInstr *MI = I;
Chris Lattner749c6f62008-01-07 07:27:27 +0000985 unsigned NumOps = MI->getDesc().getNumOperands();
Chris Lattnerac0ed5d2010-07-08 22:41:28 +0000986 assert((NumOps == X86::AddrNumOperands + 1 || NumOps == 1) &&
Chris Lattnerb97046a2004-02-03 07:27:34 +0000987 "Can only handle fst* & ftst instructions!");
Chris Lattnera960d952003-01-13 01:01:59 +0000988
Chris Lattner4a06f352004-02-02 19:23:15 +0000989 // Is this the last use of the source register?
Evan Cheng171d09e2006-11-10 01:28:43 +0000990 unsigned Reg = getFPReg(MI->getOperand(NumOps-1));
Evan Cheng6130f662008-03-05 00:59:57 +0000991 bool KillsSrc = MI->killsRegister(X86::FP0+Reg);
Chris Lattnera960d952003-01-13 01:01:59 +0000992
Evan Cheng2b152712006-02-18 02:36:28 +0000993 // FISTP64m is strange because there isn't a non-popping versions.
Chris Lattnera960d952003-01-13 01:01:59 +0000994 // If we have one _and_ we don't want to pop the operand, duplicate the value
995 // on the stack instead of moving it. This ensure that popping the value is
996 // always ok.
Dale Johannesenca8035e2007-09-17 20:15:38 +0000997 // Ditto FISTTP16m, FISTTP32m, FISTTP64m, ST_FpP80m.
Chris Lattnera960d952003-01-13 01:01:59 +0000998 //
Evan Cheng2b152712006-02-18 02:36:28 +0000999 if (!KillsSrc &&
Dale Johannesene377d4d2007-07-04 21:07:47 +00001000 (MI->getOpcode() == X86::IST_Fp64m32 ||
1001 MI->getOpcode() == X86::ISTT_Fp16m32 ||
1002 MI->getOpcode() == X86::ISTT_Fp32m32 ||
1003 MI->getOpcode() == X86::ISTT_Fp64m32 ||
1004 MI->getOpcode() == X86::IST_Fp64m64 ||
1005 MI->getOpcode() == X86::ISTT_Fp16m64 ||
1006 MI->getOpcode() == X86::ISTT_Fp32m64 ||
Dale Johannesen59a58732007-08-05 18:49:15 +00001007 MI->getOpcode() == X86::ISTT_Fp64m64 ||
Dale Johannesen41de4362007-09-20 01:27:54 +00001008 MI->getOpcode() == X86::IST_Fp64m80 ||
Dale Johannesena996d522007-08-07 01:17:37 +00001009 MI->getOpcode() == X86::ISTT_Fp16m80 ||
1010 MI->getOpcode() == X86::ISTT_Fp32m80 ||
1011 MI->getOpcode() == X86::ISTT_Fp64m80 ||
Dale Johannesen59a58732007-08-05 18:49:15 +00001012 MI->getOpcode() == X86::ST_FpP80m)) {
Jakob Stoklund Olesene098e7a2010-07-16 17:41:40 +00001013 duplicateToTop(Reg, getScratchReg(), I);
Chris Lattnera960d952003-01-13 01:01:59 +00001014 } else {
1015 moveToTop(Reg, I); // Move to the top of the stack...
1016 }
Chris Lattner58fe4592005-12-21 07:47:04 +00001017
1018 // Convert from the pseudo instruction to the concrete instruction.
Evan Cheng171d09e2006-11-10 01:28:43 +00001019 MI->RemoveOperand(NumOps-1); // Remove explicit ST(0) operand
Chris Lattner5080f4d2008-01-11 18:10:50 +00001020 MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001021
Dale Johannesene377d4d2007-07-04 21:07:47 +00001022 if (MI->getOpcode() == X86::IST_FP64m ||
1023 MI->getOpcode() == X86::ISTT_FP16m ||
1024 MI->getOpcode() == X86::ISTT_FP32m ||
Dale Johannesen88835732007-08-06 19:50:32 +00001025 MI->getOpcode() == X86::ISTT_FP64m ||
1026 MI->getOpcode() == X86::ST_FP80m) {
Evan Cheng3f490f32010-10-12 23:19:28 +00001027 if (StackTop == 0)
1028 report_fatal_error("Stack empty??");
Chris Lattnera960d952003-01-13 01:01:59 +00001029 --StackTop;
1030 } else if (KillsSrc) { // Last use of operand?
1031 popStackAfter(I);
1032 }
1033}
1034
Chris Lattner4a06f352004-02-02 19:23:15 +00001035
Chris Lattner4cf15e72004-04-11 20:21:06 +00001036/// handleOneArgFPRW: Handle instructions that read from the top of stack and
1037/// replace the value with a newly computed value. These instructions may have
1038/// non-fp operands after their FP operands.
1039///
1040/// Examples:
1041/// R1 = fchs R2
1042/// R1 = fadd R2, [mem]
Chris Lattner4a06f352004-02-02 19:23:15 +00001043///
1044void FPS::handleOneArgFPRW(MachineBasicBlock::iterator &I) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +00001045 MachineInstr *MI = I;
Evan Chenga022bdf2008-07-21 20:02:45 +00001046#ifndef NDEBUG
Chris Lattner749c6f62008-01-07 07:27:27 +00001047 unsigned NumOps = MI->getDesc().getNumOperands();
Evan Cheng171d09e2006-11-10 01:28:43 +00001048 assert(NumOps >= 2 && "FPRW instructions must have 2 ops!!");
Evan Chenga022bdf2008-07-21 20:02:45 +00001049#endif
Chris Lattner4a06f352004-02-02 19:23:15 +00001050
1051 // Is this the last use of the source register?
1052 unsigned Reg = getFPReg(MI->getOperand(1));
Evan Cheng6130f662008-03-05 00:59:57 +00001053 bool KillsSrc = MI->killsRegister(X86::FP0+Reg);
Chris Lattner4a06f352004-02-02 19:23:15 +00001054
1055 if (KillsSrc) {
1056 // If this is the last use of the source register, just make sure it's on
1057 // the top of the stack.
1058 moveToTop(Reg, I);
Evan Cheng3f490f32010-10-12 23:19:28 +00001059 if (StackTop == 0)
1060 report_fatal_error("Stack cannot be empty!");
Chris Lattner4a06f352004-02-02 19:23:15 +00001061 --StackTop;
1062 pushReg(getFPReg(MI->getOperand(0)));
1063 } else {
1064 // If this is not the last use of the source register, _copy_ it to the top
1065 // of the stack.
1066 duplicateToTop(Reg, getFPReg(MI->getOperand(0)), I);
1067 }
1068
Chris Lattner58fe4592005-12-21 07:47:04 +00001069 // Change from the pseudo instruction to the concrete instruction.
Chris Lattner4a06f352004-02-02 19:23:15 +00001070 MI->RemoveOperand(1); // Drop the source operand.
1071 MI->RemoveOperand(0); // Drop the destination operand.
Chris Lattner5080f4d2008-01-11 18:10:50 +00001072 MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
Chris Lattner4a06f352004-02-02 19:23:15 +00001073}
1074
1075
Chris Lattnera960d952003-01-13 01:01:59 +00001076//===----------------------------------------------------------------------===//
1077// Define tables of various ways to map pseudo instructions
1078//
1079
1080// ForwardST0Table - Map: A = B op C into: ST(0) = ST(0) op ST(i)
1081static const TableEntry ForwardST0Table[] = {
Dale Johannesene377d4d2007-07-04 21:07:47 +00001082 { X86::ADD_Fp32 , X86::ADD_FST0r },
1083 { X86::ADD_Fp64 , X86::ADD_FST0r },
Dale Johannesen6a308112007-08-06 21:31:06 +00001084 { X86::ADD_Fp80 , X86::ADD_FST0r },
Dale Johannesene377d4d2007-07-04 21:07:47 +00001085 { X86::DIV_Fp32 , X86::DIV_FST0r },
1086 { X86::DIV_Fp64 , X86::DIV_FST0r },
Dale Johannesen6a308112007-08-06 21:31:06 +00001087 { X86::DIV_Fp80 , X86::DIV_FST0r },
Dale Johannesene377d4d2007-07-04 21:07:47 +00001088 { X86::MUL_Fp32 , X86::MUL_FST0r },
1089 { X86::MUL_Fp64 , X86::MUL_FST0r },
Dale Johannesen6a308112007-08-06 21:31:06 +00001090 { X86::MUL_Fp80 , X86::MUL_FST0r },
Dale Johannesene377d4d2007-07-04 21:07:47 +00001091 { X86::SUB_Fp32 , X86::SUB_FST0r },
1092 { X86::SUB_Fp64 , X86::SUB_FST0r },
Dale Johannesen6a308112007-08-06 21:31:06 +00001093 { X86::SUB_Fp80 , X86::SUB_FST0r },
Chris Lattnera960d952003-01-13 01:01:59 +00001094};
1095
1096// ReverseST0Table - Map: A = B op C into: ST(0) = ST(i) op ST(0)
1097static const TableEntry ReverseST0Table[] = {
Dale Johannesene377d4d2007-07-04 21:07:47 +00001098 { X86::ADD_Fp32 , X86::ADD_FST0r }, // commutative
1099 { X86::ADD_Fp64 , X86::ADD_FST0r }, // commutative
Dale Johannesen6a308112007-08-06 21:31:06 +00001100 { X86::ADD_Fp80 , X86::ADD_FST0r }, // commutative
Dale Johannesene377d4d2007-07-04 21:07:47 +00001101 { X86::DIV_Fp32 , X86::DIVR_FST0r },
1102 { X86::DIV_Fp64 , X86::DIVR_FST0r },
Dale Johannesen6a308112007-08-06 21:31:06 +00001103 { X86::DIV_Fp80 , X86::DIVR_FST0r },
Dale Johannesene377d4d2007-07-04 21:07:47 +00001104 { X86::MUL_Fp32 , X86::MUL_FST0r }, // commutative
1105 { X86::MUL_Fp64 , X86::MUL_FST0r }, // commutative
Dale Johannesen6a308112007-08-06 21:31:06 +00001106 { X86::MUL_Fp80 , X86::MUL_FST0r }, // commutative
Dale Johannesene377d4d2007-07-04 21:07:47 +00001107 { X86::SUB_Fp32 , X86::SUBR_FST0r },
1108 { X86::SUB_Fp64 , X86::SUBR_FST0r },
Dale Johannesen6a308112007-08-06 21:31:06 +00001109 { X86::SUB_Fp80 , X86::SUBR_FST0r },
Chris Lattnera960d952003-01-13 01:01:59 +00001110};
1111
1112// ForwardSTiTable - Map: A = B op C into: ST(i) = ST(0) op ST(i)
1113static const TableEntry ForwardSTiTable[] = {
Dale Johannesene377d4d2007-07-04 21:07:47 +00001114 { X86::ADD_Fp32 , X86::ADD_FrST0 }, // commutative
1115 { X86::ADD_Fp64 , X86::ADD_FrST0 }, // commutative
Dale Johannesen6a308112007-08-06 21:31:06 +00001116 { X86::ADD_Fp80 , X86::ADD_FrST0 }, // commutative
Dale Johannesene377d4d2007-07-04 21:07:47 +00001117 { X86::DIV_Fp32 , X86::DIVR_FrST0 },
1118 { X86::DIV_Fp64 , X86::DIVR_FrST0 },
Dale Johannesen6a308112007-08-06 21:31:06 +00001119 { X86::DIV_Fp80 , X86::DIVR_FrST0 },
Dale Johannesene377d4d2007-07-04 21:07:47 +00001120 { X86::MUL_Fp32 , X86::MUL_FrST0 }, // commutative
1121 { X86::MUL_Fp64 , X86::MUL_FrST0 }, // commutative
Dale Johannesen6a308112007-08-06 21:31:06 +00001122 { X86::MUL_Fp80 , X86::MUL_FrST0 }, // commutative
Dale Johannesene377d4d2007-07-04 21:07:47 +00001123 { X86::SUB_Fp32 , X86::SUBR_FrST0 },
1124 { X86::SUB_Fp64 , X86::SUBR_FrST0 },
Dale Johannesen6a308112007-08-06 21:31:06 +00001125 { X86::SUB_Fp80 , X86::SUBR_FrST0 },
Chris Lattnera960d952003-01-13 01:01:59 +00001126};
1127
1128// ReverseSTiTable - Map: A = B op C into: ST(i) = ST(i) op ST(0)
1129static const TableEntry ReverseSTiTable[] = {
Dale Johannesene377d4d2007-07-04 21:07:47 +00001130 { X86::ADD_Fp32 , X86::ADD_FrST0 },
1131 { X86::ADD_Fp64 , X86::ADD_FrST0 },
Dale Johannesen6a308112007-08-06 21:31:06 +00001132 { X86::ADD_Fp80 , X86::ADD_FrST0 },
Dale Johannesene377d4d2007-07-04 21:07:47 +00001133 { X86::DIV_Fp32 , X86::DIV_FrST0 },
1134 { X86::DIV_Fp64 , X86::DIV_FrST0 },
Dale Johannesen6a308112007-08-06 21:31:06 +00001135 { X86::DIV_Fp80 , X86::DIV_FrST0 },
Dale Johannesene377d4d2007-07-04 21:07:47 +00001136 { X86::MUL_Fp32 , X86::MUL_FrST0 },
1137 { X86::MUL_Fp64 , X86::MUL_FrST0 },
Dale Johannesen6a308112007-08-06 21:31:06 +00001138 { X86::MUL_Fp80 , X86::MUL_FrST0 },
Dale Johannesene377d4d2007-07-04 21:07:47 +00001139 { X86::SUB_Fp32 , X86::SUB_FrST0 },
1140 { X86::SUB_Fp64 , X86::SUB_FrST0 },
Dale Johannesen6a308112007-08-06 21:31:06 +00001141 { X86::SUB_Fp80 , X86::SUB_FrST0 },
Chris Lattnera960d952003-01-13 01:01:59 +00001142};
1143
1144
1145/// handleTwoArgFP - Handle instructions like FADD and friends which are virtual
1146/// instructions which need to be simplified and possibly transformed.
1147///
1148/// Result: ST(0) = fsub ST(0), ST(i)
1149/// ST(i) = fsub ST(0), ST(i)
1150/// ST(0) = fsubr ST(0), ST(i)
1151/// ST(i) = fsubr ST(0), ST(i)
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001152///
Chris Lattnera960d952003-01-13 01:01:59 +00001153void FPS::handleTwoArgFP(MachineBasicBlock::iterator &I) {
1154 ASSERT_SORTED(ForwardST0Table); ASSERT_SORTED(ReverseST0Table);
1155 ASSERT_SORTED(ForwardSTiTable); ASSERT_SORTED(ReverseSTiTable);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +00001156 MachineInstr *MI = I;
Chris Lattnera960d952003-01-13 01:01:59 +00001157
Chris Lattner749c6f62008-01-07 07:27:27 +00001158 unsigned NumOperands = MI->getDesc().getNumOperands();
Chris Lattnerd62d5d72004-06-11 04:25:06 +00001159 assert(NumOperands == 3 && "Illegal TwoArgFP instruction!");
Chris Lattnera960d952003-01-13 01:01:59 +00001160 unsigned Dest = getFPReg(MI->getOperand(0));
1161 unsigned Op0 = getFPReg(MI->getOperand(NumOperands-2));
1162 unsigned Op1 = getFPReg(MI->getOperand(NumOperands-1));
Evan Cheng6130f662008-03-05 00:59:57 +00001163 bool KillsOp0 = MI->killsRegister(X86::FP0+Op0);
1164 bool KillsOp1 = MI->killsRegister(X86::FP0+Op1);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001165 DebugLoc dl = MI->getDebugLoc();
Chris Lattnera960d952003-01-13 01:01:59 +00001166
Chris Lattnera960d952003-01-13 01:01:59 +00001167 unsigned TOS = getStackEntry(0);
1168
1169 // One of our operands must be on the top of the stack. If neither is yet, we
1170 // need to move one.
1171 if (Op0 != TOS && Op1 != TOS) { // No operand at TOS?
1172 // We can choose to move either operand to the top of the stack. If one of
1173 // the operands is killed by this instruction, we want that one so that we
1174 // can update right on top of the old version.
1175 if (KillsOp0) {
1176 moveToTop(Op0, I); // Move dead operand to TOS.
1177 TOS = Op0;
1178 } else if (KillsOp1) {
1179 moveToTop(Op1, I);
1180 TOS = Op1;
1181 } else {
1182 // All of the operands are live after this instruction executes, so we
1183 // cannot update on top of any operand. Because of this, we must
1184 // duplicate one of the stack elements to the top. It doesn't matter
1185 // which one we pick.
1186 //
1187 duplicateToTop(Op0, Dest, I);
1188 Op0 = TOS = Dest;
1189 KillsOp0 = true;
1190 }
Chris Lattnerd62d5d72004-06-11 04:25:06 +00001191 } else if (!KillsOp0 && !KillsOp1) {
Chris Lattnera960d952003-01-13 01:01:59 +00001192 // If we DO have one of our operands at the top of the stack, but we don't
1193 // have a dead operand, we must duplicate one of the operands to a new slot
1194 // on the stack.
1195 duplicateToTop(Op0, Dest, I);
1196 Op0 = TOS = Dest;
1197 KillsOp0 = true;
1198 }
1199
1200 // Now we know that one of our operands is on the top of the stack, and at
1201 // least one of our operands is killed by this instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001202 assert((TOS == Op0 || TOS == Op1) && (KillsOp0 || KillsOp1) &&
1203 "Stack conditions not set up right!");
Chris Lattnera960d952003-01-13 01:01:59 +00001204
1205 // We decide which form to use based on what is on the top of the stack, and
1206 // which operand is killed by this instruction.
1207 const TableEntry *InstTable;
1208 bool isForward = TOS == Op0;
1209 bool updateST0 = (TOS == Op0 && !KillsOp1) || (TOS == Op1 && !KillsOp0);
1210 if (updateST0) {
1211 if (isForward)
1212 InstTable = ForwardST0Table;
1213 else
1214 InstTable = ReverseST0Table;
1215 } else {
1216 if (isForward)
1217 InstTable = ForwardSTiTable;
1218 else
1219 InstTable = ReverseSTiTable;
1220 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001221
Owen Anderson718cb662007-09-07 04:06:50 +00001222 int Opcode = Lookup(InstTable, array_lengthof(ForwardST0Table),
1223 MI->getOpcode());
Chris Lattnera960d952003-01-13 01:01:59 +00001224 assert(Opcode != -1 && "Unknown TwoArgFP pseudo instruction!");
1225
1226 // NotTOS - The register which is not on the top of stack...
1227 unsigned NotTOS = (TOS == Op0) ? Op1 : Op0;
1228
1229 // Replace the old instruction with a new instruction
Chris Lattnerc1bab322004-03-31 22:02:36 +00001230 MBB->remove(I++);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001231 I = BuildMI(*MBB, I, dl, TII->get(Opcode)).addReg(getSTReg(NotTOS));
Chris Lattnera960d952003-01-13 01:01:59 +00001232
1233 // If both operands are killed, pop one off of the stack in addition to
1234 // overwriting the other one.
1235 if (KillsOp0 && KillsOp1 && Op0 != Op1) {
1236 assert(!updateST0 && "Should have updated other operand!");
1237 popStackAfter(I); // Pop the top of stack
1238 }
1239
Chris Lattnera960d952003-01-13 01:01:59 +00001240 // Update stack information so that we know the destination register is now on
1241 // the stack.
Chris Lattnerd62d5d72004-06-11 04:25:06 +00001242 unsigned UpdatedSlot = getSlot(updateST0 ? TOS : NotTOS);
1243 assert(UpdatedSlot < StackTop && Dest < 7);
1244 Stack[UpdatedSlot] = Dest;
1245 RegMap[Dest] = UpdatedSlot;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001246 MBB->getParent()->DeleteMachineInstr(MI); // Remove the old instruction
Chris Lattnerd62d5d72004-06-11 04:25:06 +00001247}
1248
Chris Lattner0ca2c8e2004-06-11 04:49:02 +00001249/// handleCompareFP - Handle FUCOM and FUCOMI instructions, which have two FP
Chris Lattnerd62d5d72004-06-11 04:25:06 +00001250/// register arguments and no explicit destinations.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001251///
Chris Lattnerd62d5d72004-06-11 04:25:06 +00001252void FPS::handleCompareFP(MachineBasicBlock::iterator &I) {
1253 ASSERT_SORTED(ForwardST0Table); ASSERT_SORTED(ReverseST0Table);
1254 ASSERT_SORTED(ForwardSTiTable); ASSERT_SORTED(ReverseSTiTable);
1255 MachineInstr *MI = I;
1256
Chris Lattner749c6f62008-01-07 07:27:27 +00001257 unsigned NumOperands = MI->getDesc().getNumOperands();
Chris Lattner0ca2c8e2004-06-11 04:49:02 +00001258 assert(NumOperands == 2 && "Illegal FUCOM* instruction!");
Chris Lattnerd62d5d72004-06-11 04:25:06 +00001259 unsigned Op0 = getFPReg(MI->getOperand(NumOperands-2));
1260 unsigned Op1 = getFPReg(MI->getOperand(NumOperands-1));
Evan Cheng6130f662008-03-05 00:59:57 +00001261 bool KillsOp0 = MI->killsRegister(X86::FP0+Op0);
1262 bool KillsOp1 = MI->killsRegister(X86::FP0+Op1);
Chris Lattnerd62d5d72004-06-11 04:25:06 +00001263
1264 // Make sure the first operand is on the top of stack, the other one can be
1265 // anywhere.
1266 moveToTop(Op0, I);
1267
Chris Lattner58fe4592005-12-21 07:47:04 +00001268 // Change from the pseudo instruction to the concrete instruction.
Chris Lattner57790422004-06-11 05:22:44 +00001269 MI->getOperand(0).setReg(getSTReg(Op1));
1270 MI->RemoveOperand(1);
Chris Lattner5080f4d2008-01-11 18:10:50 +00001271 MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
Chris Lattner57790422004-06-11 05:22:44 +00001272
Chris Lattnerd62d5d72004-06-11 04:25:06 +00001273 // If any of the operands are killed by this instruction, free them.
1274 if (KillsOp0) freeStackSlotAfter(I, Op0);
1275 if (KillsOp1 && Op0 != Op1) freeStackSlotAfter(I, Op1);
Chris Lattnera960d952003-01-13 01:01:59 +00001276}
1277
Chris Lattnerc1bab322004-03-31 22:02:36 +00001278/// handleCondMovFP - Handle two address conditional move instructions. These
1279/// instructions move a st(i) register to st(0) iff a condition is true. These
1280/// instructions require that the first operand is at the top of the stack, but
1281/// otherwise don't modify the stack at all.
1282void FPS::handleCondMovFP(MachineBasicBlock::iterator &I) {
1283 MachineInstr *MI = I;
1284
1285 unsigned Op0 = getFPReg(MI->getOperand(0));
Chris Lattner6cdb1ea2006-09-05 20:27:32 +00001286 unsigned Op1 = getFPReg(MI->getOperand(2));
Evan Cheng6130f662008-03-05 00:59:57 +00001287 bool KillsOp1 = MI->killsRegister(X86::FP0+Op1);
Chris Lattnerc1bab322004-03-31 22:02:36 +00001288
1289 // The first operand *must* be on the top of the stack.
1290 moveToTop(Op0, I);
1291
1292 // Change the second operand to the stack register that the operand is in.
Chris Lattner58fe4592005-12-21 07:47:04 +00001293 // Change from the pseudo instruction to the concrete instruction.
Chris Lattnerc1bab322004-03-31 22:02:36 +00001294 MI->RemoveOperand(0);
Chris Lattner6cdb1ea2006-09-05 20:27:32 +00001295 MI->RemoveOperand(1);
Chris Lattnerc1bab322004-03-31 22:02:36 +00001296 MI->getOperand(0).setReg(getSTReg(Op1));
Chris Lattner5080f4d2008-01-11 18:10:50 +00001297 MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
Chris Lattner58fe4592005-12-21 07:47:04 +00001298
Chris Lattnerc1bab322004-03-31 22:02:36 +00001299 // If we kill the second operand, make sure to pop it from the stack.
Evan Chengddd2a452006-11-15 20:56:39 +00001300 if (Op0 != Op1 && KillsOp1) {
Chris Lattner76eb08b2005-08-23 22:49:55 +00001301 // Get this value off of the register stack.
1302 freeStackSlotAfter(I, Op1);
1303 }
Chris Lattnerc1bab322004-03-31 22:02:36 +00001304}
1305
Chris Lattnera960d952003-01-13 01:01:59 +00001306
1307/// handleSpecialFP - Handle special instructions which behave unlike other
Misha Brukmancf00c4a2003-10-10 17:57:28 +00001308/// floating point instructions. This is primarily intended for use by pseudo
Chris Lattnera960d952003-01-13 01:01:59 +00001309/// instructions.
1310///
1311void FPS::handleSpecialFP(MachineBasicBlock::iterator &I) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +00001312 MachineInstr *MI = I;
Chris Lattnera960d952003-01-13 01:01:59 +00001313 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001314 default: llvm_unreachable("Unknown SpecialFP instruction!");
Chris Lattner6fa2f9c2008-03-09 07:05:32 +00001315 case X86::FpGET_ST0_32:// Appears immediately after a call returning FP type!
1316 case X86::FpGET_ST0_64:// Appears immediately after a call returning FP type!
1317 case X86::FpGET_ST0_80:// Appears immediately after a call returning FP type!
Chris Lattnera960d952003-01-13 01:01:59 +00001318 assert(StackTop == 0 && "Stack should be empty after a call!");
1319 pushReg(getFPReg(MI->getOperand(0)));
1320 break;
Chris Lattner24e0a542008-03-21 06:38:26 +00001321 case X86::FpGET_ST1_32:// Appears immediately after a call returning FP type!
1322 case X86::FpGET_ST1_64:// Appears immediately after a call returning FP type!
1323 case X86::FpGET_ST1_80:{// Appears immediately after a call returning FP type!
1324 // FpGET_ST1 should occur right after a FpGET_ST0 for a call or inline asm.
1325 // The pattern we expect is:
1326 // CALL
1327 // FP1 = FpGET_ST0
1328 // FP4 = FpGET_ST1
1329 //
1330 // At this point, we've pushed FP1 on the top of stack, so it should be
1331 // present if it isn't dead. If it was dead, we already emitted a pop to
1332 // remove it from the stack and StackTop = 0.
1333
1334 // Push FP4 as top of stack next.
1335 pushReg(getFPReg(MI->getOperand(0)));
1336
1337 // If StackTop was 0 before we pushed our operand, then ST(0) must have been
1338 // dead. In this case, the ST(1) value is the only thing that is live, so
1339 // it should be on the TOS (after the pop that was emitted) and is. Just
1340 // continue in this case.
1341 if (StackTop == 1)
1342 break;
1343
1344 // Because pushReg just pushed ST(1) as TOS, we now have to swap the two top
1345 // elements so that our accounting is correct.
1346 unsigned RegOnTop = getStackEntry(0);
1347 unsigned RegNo = getStackEntry(1);
1348
1349 // Swap the slots the regs are in.
1350 std::swap(RegMap[RegNo], RegMap[RegOnTop]);
1351
1352 // Swap stack slot contents.
Evan Cheng3f490f32010-10-12 23:19:28 +00001353 if (RegMap[RegOnTop] >= StackTop)
1354 report_fatal_error("Access past stack top!");
Chris Lattner24e0a542008-03-21 06:38:26 +00001355 std::swap(Stack[RegMap[RegOnTop]], Stack[StackTop-1]);
1356 break;
1357 }
Chris Lattnerafb23f42008-03-09 07:08:44 +00001358 case X86::FpSET_ST0_32:
1359 case X86::FpSET_ST0_64:
Rafael Espindolaf55715c2009-06-30 12:18:16 +00001360 case X86::FpSET_ST0_80: {
Jakob Stoklund Olesen2b336bc2010-07-10 17:42:34 +00001361 // FpSET_ST0_80 is generated by copyRegToReg for setting up inline asm
1362 // arguments that use an st constraint. We expect a sequence of
1363 // instructions: Fp_SET_ST0 Fp_SET_ST1? INLINEASM
Rafael Espindolaaf5f6ba2009-06-30 16:40:03 +00001364 unsigned Op0 = getFPReg(MI->getOperand(0));
1365
Rafael Espindolaaf5f6ba2009-06-30 16:40:03 +00001366 if (!MI->killsRegister(X86::FP0 + Op0)) {
Jakob Stoklund Olesen2b336bc2010-07-10 17:42:34 +00001367 // Duplicate Op0 into a temporary on the stack top.
Jakob Stoklund Olesene098e7a2010-07-16 17:41:40 +00001368 duplicateToTop(Op0, getScratchReg(), I);
Rafael Espindolaaf5f6ba2009-06-30 16:40:03 +00001369 } else {
Jakob Stoklund Olesen2b336bc2010-07-10 17:42:34 +00001370 // Op0 is killed, so just swap it into position.
Rafael Espindolaaf5f6ba2009-06-30 16:40:03 +00001371 moveToTop(Op0, I);
Rafael Espindola1c3329f2009-06-21 12:02:51 +00001372 }
Evan Chenga0eedac2009-02-09 23:32:07 +00001373 --StackTop; // "Forget" we have something on the top of stack!
1374 break;
Rafael Espindolaf55715c2009-06-30 12:18:16 +00001375 }
Evan Chenga0eedac2009-02-09 23:32:07 +00001376 case X86::FpSET_ST1_32:
1377 case X86::FpSET_ST1_64:
Jakob Stoklund Olesen2b336bc2010-07-10 17:42:34 +00001378 case X86::FpSET_ST1_80: {
1379 // Set up st(1) for inline asm. We are assuming that st(0) has already been
1380 // set up by FpSET_ST0, and our StackTop is off by one because of it.
1381 unsigned Op0 = getFPReg(MI->getOperand(0));
1382 // Restore the actual StackTop from before Fp_SET_ST0.
1383 // Note we can't handle Fp_SET_ST1 without a preceeding Fp_SET_ST0, and we
1384 // are not enforcing the constraint.
1385 ++StackTop;
1386 unsigned RegOnTop = getStackEntry(0); // This reg must remain in st(0).
1387 if (!MI->killsRegister(X86::FP0 + Op0)) {
Jakob Stoklund Olesene098e7a2010-07-16 17:41:40 +00001388 duplicateToTop(Op0, getScratchReg(), I);
Jakob Stoklund Olesen2b336bc2010-07-10 17:42:34 +00001389 moveToTop(RegOnTop, I);
1390 } else if (getSTReg(Op0) != X86::ST1) {
1391 // We have the wrong value at st(1). Shuffle! Untested!
1392 moveToTop(getStackEntry(1), I);
1393 moveToTop(Op0, I);
1394 moveToTop(RegOnTop, I);
Evan Chenga0eedac2009-02-09 23:32:07 +00001395 }
Jakob Stoklund Olesen2b336bc2010-07-10 17:42:34 +00001396 assert(StackTop >= 2 && "Too few live registers");
1397 StackTop -= 2; // "Forget" both st(0) and st(1).
Chris Lattnera960d952003-01-13 01:01:59 +00001398 break;
Jakob Stoklund Olesen2b336bc2010-07-10 17:42:34 +00001399 }
Dale Johannesene377d4d2007-07-04 21:07:47 +00001400 case X86::MOV_Fp3232:
1401 case X86::MOV_Fp3264:
1402 case X86::MOV_Fp6432:
Dale Johannesen59a58732007-08-05 18:49:15 +00001403 case X86::MOV_Fp6464:
1404 case X86::MOV_Fp3280:
1405 case X86::MOV_Fp6480:
1406 case X86::MOV_Fp8032:
1407 case X86::MOV_Fp8064:
1408 case X86::MOV_Fp8080: {
Evan Chengfb112882009-03-23 08:01:15 +00001409 const MachineOperand &MO1 = MI->getOperand(1);
1410 unsigned SrcReg = getFPReg(MO1);
Chris Lattnera960d952003-01-13 01:01:59 +00001411
Evan Chengfb112882009-03-23 08:01:15 +00001412 const MachineOperand &MO0 = MI->getOperand(0);
Evan Chengfb112882009-03-23 08:01:15 +00001413 unsigned DestReg = getFPReg(MO0);
Evan Cheng6130f662008-03-05 00:59:57 +00001414 if (MI->killsRegister(X86::FP0+SrcReg)) {
Chris Lattnera960d952003-01-13 01:01:59 +00001415 // If the input operand is killed, we can just change the owner of the
1416 // incoming stack slot into the result.
1417 unsigned Slot = getSlot(SrcReg);
1418 assert(Slot < 7 && DestReg < 7 && "FpMOV operands invalid!");
1419 Stack[Slot] = DestReg;
1420 RegMap[DestReg] = Slot;
1421
1422 } else {
1423 // For FMOV we just duplicate the specified value to a new stack slot.
1424 // This could be made better, but would require substantial changes.
1425 duplicateToTop(SrcReg, DestReg, I);
1426 }
Nick Lewycky3c786972008-03-11 05:56:09 +00001427 }
Chris Lattnera960d952003-01-13 01:01:59 +00001428 break;
Chris Lattner518bb532010-02-09 19:54:29 +00001429 case TargetOpcode::INLINEASM: {
Chris Lattnere12ecf22008-03-11 19:50:13 +00001430 // The inline asm MachineInstr currently only *uses* FP registers for the
1431 // 'f' constraint. These should be turned into the current ST(x) register
1432 // in the machine instr. Also, any kills should be explicitly popped after
1433 // the inline asm.
Jakob Stoklund Olesen7261fb22010-04-28 18:28:37 +00001434 unsigned Kills = 0;
Chris Lattnere12ecf22008-03-11 19:50:13 +00001435 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1436 MachineOperand &Op = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001437 if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6)
Chris Lattnere12ecf22008-03-11 19:50:13 +00001438 continue;
1439 assert(Op.isUse() && "Only handle inline asm uses right now");
1440
1441 unsigned FPReg = getFPReg(Op);
1442 Op.setReg(getSTReg(FPReg));
1443
1444 // If we kill this operand, make sure to pop it from the stack after the
1445 // asm. We just remember it for now, and pop them all off at the end in
1446 // a batch.
1447 if (Op.isKill())
Jakob Stoklund Olesen7261fb22010-04-28 18:28:37 +00001448 Kills |= 1U << FPReg;
Chris Lattnere12ecf22008-03-11 19:50:13 +00001449 }
1450
1451 // If this asm kills any FP registers (is the last use of them) we must
1452 // explicitly emit pop instructions for them. Do this now after the asm has
1453 // executed so that the ST(x) numbers are not off (which would happen if we
1454 // did this inline with operand rewriting).
1455 //
1456 // Note: this might be a non-optimal pop sequence. We might be able to do
1457 // better by trying to pop in stack order or something.
1458 MachineBasicBlock::iterator InsertPt = MI;
Jakob Stoklund Olesen7261fb22010-04-28 18:28:37 +00001459 while (Kills) {
1460 unsigned FPReg = CountTrailingZeros_32(Kills);
1461 freeStackSlotAfter(InsertPt, FPReg);
1462 Kills &= ~(1U << FPReg);
1463 }
Chris Lattnere12ecf22008-03-11 19:50:13 +00001464 // Don't delete the inline asm!
1465 return;
1466 }
1467
Chris Lattner447ff682008-03-11 03:23:40 +00001468 case X86::RET:
1469 case X86::RETI:
1470 // If RET has an FP register use operand, pass the first one in ST(0) and
1471 // the second one in ST(1).
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +00001472
Chris Lattner447ff682008-03-11 03:23:40 +00001473 // Find the register operands.
1474 unsigned FirstFPRegOp = ~0U, SecondFPRegOp = ~0U;
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +00001475 unsigned LiveMask = 0;
1476
Chris Lattner447ff682008-03-11 03:23:40 +00001477 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1478 MachineOperand &Op = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001479 if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6)
Chris Lattner447ff682008-03-11 03:23:40 +00001480 continue;
Chris Lattner35831d02008-03-21 20:41:27 +00001481 // FP Register uses must be kills unless there are two uses of the same
1482 // register, in which case only one will be a kill.
1483 assert(Op.isUse() &&
1484 (Op.isKill() || // Marked kill.
1485 getFPReg(Op) == FirstFPRegOp || // Second instance.
1486 MI->killsRegister(Op.getReg())) && // Later use is marked kill.
1487 "Ret only defs operands, and values aren't live beyond it");
Chris Lattner447ff682008-03-11 03:23:40 +00001488
1489 if (FirstFPRegOp == ~0U)
1490 FirstFPRegOp = getFPReg(Op);
1491 else {
1492 assert(SecondFPRegOp == ~0U && "More than two fp operands!");
1493 SecondFPRegOp = getFPReg(Op);
1494 }
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +00001495 LiveMask |= (1 << getFPReg(Op));
Chris Lattner447ff682008-03-11 03:23:40 +00001496
1497 // Remove the operand so that later passes don't see it.
1498 MI->RemoveOperand(i);
1499 --i, --e;
1500 }
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +00001501
1502 // We may have been carrying spurious live-ins, so make sure only the returned
1503 // registers are left live.
1504 adjustLiveRegs(LiveMask, MI);
1505 if (!LiveMask) return; // Quick check to see if any are possible.
1506
Chris Lattner447ff682008-03-11 03:23:40 +00001507 // There are only four possibilities here:
1508 // 1) we are returning a single FP value. In this case, it has to be in
1509 // ST(0) already, so just declare success by removing the value from the
1510 // FP Stack.
1511 if (SecondFPRegOp == ~0U) {
1512 // Assert that the top of stack contains the right FP register.
1513 assert(StackTop == 1 && FirstFPRegOp == getStackEntry(0) &&
1514 "Top of stack not the right register for RET!");
1515
1516 // Ok, everything is good, mark the value as not being on the stack
1517 // anymore so that our assertion about the stack being empty at end of
1518 // block doesn't fire.
1519 StackTop = 0;
1520 return;
1521 }
1522
Chris Lattner447ff682008-03-11 03:23:40 +00001523 // Otherwise, we are returning two values:
1524 // 2) If returning the same value for both, we only have one thing in the FP
1525 // stack. Consider: RET FP1, FP1
1526 if (StackTop == 1) {
1527 assert(FirstFPRegOp == SecondFPRegOp && FirstFPRegOp == getStackEntry(0)&&
1528 "Stack misconfiguration for RET!");
1529
1530 // Duplicate the TOS so that we return it twice. Just pick some other FPx
1531 // register to hold it.
Jakob Stoklund Olesene098e7a2010-07-16 17:41:40 +00001532 unsigned NewReg = getScratchReg();
Chris Lattner447ff682008-03-11 03:23:40 +00001533 duplicateToTop(FirstFPRegOp, NewReg, MI);
1534 FirstFPRegOp = NewReg;
1535 }
1536
1537 /// Okay we know we have two different FPx operands now:
1538 assert(StackTop == 2 && "Must have two values live!");
1539
1540 /// 3) If SecondFPRegOp is currently in ST(0) and FirstFPRegOp is currently
1541 /// in ST(1). In this case, emit an fxch.
1542 if (getStackEntry(0) == SecondFPRegOp) {
1543 assert(getStackEntry(1) == FirstFPRegOp && "Unknown regs live");
1544 moveToTop(FirstFPRegOp, MI);
1545 }
1546
1547 /// 4) Finally, FirstFPRegOp must be in ST(0) and SecondFPRegOp must be in
1548 /// ST(1). Just remove both from our understanding of the stack and return.
1549 assert(getStackEntry(0) == FirstFPRegOp && "Unknown regs live");
Chris Lattner03535262008-03-21 05:57:20 +00001550 assert(getStackEntry(1) == SecondFPRegOp && "Unknown regs live");
Chris Lattner447ff682008-03-11 03:23:40 +00001551 StackTop = 0;
1552 return;
Chris Lattnera960d952003-01-13 01:01:59 +00001553 }
Chris Lattnera960d952003-01-13 01:01:59 +00001554
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +00001555 I = MBB->erase(I); // Remove the pseudo instruction
Jakob Stoklund Olesene928ec92010-07-16 16:38:12 +00001556
1557 // We want to leave I pointing to the previous instruction, but what if we
1558 // just erased the first instruction?
1559 if (I == MBB->begin()) {
1560 DEBUG(dbgs() << "Inserting dummy KILL\n");
1561 I = BuildMI(*MBB, I, DebugLoc(), TII->get(TargetOpcode::KILL));
1562 } else
1563 --I;
Chris Lattnera960d952003-01-13 01:01:59 +00001564}
Jakob Stoklund Olesen7db1e7a2010-07-08 19:46:30 +00001565
1566// Translate a COPY instruction to a pseudo-op that handleSpecialFP understands.
1567bool FPS::translateCopy(MachineInstr *MI) {
1568 unsigned DstReg = MI->getOperand(0).getReg();
1569 unsigned SrcReg = MI->getOperand(1).getReg();
1570
1571 if (DstReg == X86::ST0) {
1572 MI->setDesc(TII->get(X86::FpSET_ST0_80));
1573 MI->RemoveOperand(0);
1574 return true;
1575 }
1576 if (DstReg == X86::ST1) {
1577 MI->setDesc(TII->get(X86::FpSET_ST1_80));
1578 MI->RemoveOperand(0);
1579 return true;
1580 }
1581 if (SrcReg == X86::ST0) {
1582 MI->setDesc(TII->get(X86::FpGET_ST0_80));
1583 return true;
1584 }
1585 if (SrcReg == X86::ST1) {
1586 MI->setDesc(TII->get(X86::FpGET_ST1_80));
1587 return true;
1588 }
1589 if (X86::RFP80RegClass.contains(DstReg, SrcReg)) {
1590 MI->setDesc(TII->get(X86::MOV_Fp8080));
1591 return true;
1592 }
1593 return false;
1594}