blob: 5c6e6ebfddec041c301587ceb97b6980aef99ccd [file] [log] [blame]
Chris Lattnera960d952003-01-13 01:01:59 +00001//===-- FloatingPoint.cpp - Floating point Reg -> Stack converter ---------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnera960d952003-01-13 01:01:59 +00009//
10// This file defines the pass which converts floating point instructions from
11// virtual registers into register stack instructions.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattnercb533582003-08-03 21:14:38 +000015#define DEBUG_TYPE "fp"
Chris Lattnera960d952003-01-13 01:01:59 +000016#include "X86.h"
17#include "X86InstrInfo.h"
18#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/LiveVariables.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000021#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnera960d952003-01-13 01:01:59 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattnera11136b2003-08-01 22:21:34 +000023#include "Support/Debug.h"
Chris Lattnera960d952003-01-13 01:01:59 +000024#include "Support/Statistic.h"
25#include <algorithm>
26#include <iostream>
27
Brian Gaeked0fde302003-11-11 22:41:34 +000028namespace llvm {
29
Chris Lattnera960d952003-01-13 01:01:59 +000030namespace {
31 Statistic<> NumFXCH("x86-codegen", "Number of fxch instructions inserted");
32 Statistic<> NumFP ("x86-codegen", "Number of floating point instructions");
33
34 struct FPS : public MachineFunctionPass {
35 virtual bool runOnMachineFunction(MachineFunction &MF);
36
37 virtual const char *getPassName() const { return "X86 FP Stackifier"; }
38
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequired<LiveVariables>();
41 MachineFunctionPass::getAnalysisUsage(AU);
42 }
43 private:
44 LiveVariables *LV; // Live variable info for current function...
45 MachineBasicBlock *MBB; // Current basic block
46 unsigned Stack[8]; // FP<n> Registers in each stack slot...
47 unsigned RegMap[8]; // Track which stack slot contains each register
48 unsigned StackTop; // The current top of the FP stack.
49
50 void dumpStack() const {
51 std::cerr << "Stack contents:";
52 for (unsigned i = 0; i != StackTop; ++i) {
53 std::cerr << " FP" << Stack[i];
54 assert(RegMap[Stack[i]] == i && "Stack[] doesn't match RegMap[]!");
55 }
56 std::cerr << "\n";
57 }
58 private:
59 // getSlot - Return the stack slot number a particular register number is
60 // in...
61 unsigned getSlot(unsigned RegNo) const {
62 assert(RegNo < 8 && "Regno out of range!");
63 return RegMap[RegNo];
64 }
65
66 // getStackEntry - Return the X86::FP<n> register in register ST(i)
67 unsigned getStackEntry(unsigned STi) const {
68 assert(STi < StackTop && "Access past stack top!");
69 return Stack[StackTop-1-STi];
70 }
71
72 // getSTReg - Return the X86::ST(i) register which contains the specified
73 // FP<RegNo> register
74 unsigned getSTReg(unsigned RegNo) const {
Brian Gaeked0fde302003-11-11 22:41:34 +000075 return StackTop - 1 - getSlot(RegNo) + llvm::X86::ST0;
Chris Lattnera960d952003-01-13 01:01:59 +000076 }
77
78 // pushReg - Push the specifiex FP<n> register onto the stack
79 void pushReg(unsigned Reg) {
80 assert(Reg < 8 && "Register number out of range!");
81 assert(StackTop < 8 && "Stack overflow!");
82 Stack[StackTop] = Reg;
83 RegMap[Reg] = StackTop++;
84 }
85
86 bool isAtTop(unsigned RegNo) const { return getSlot(RegNo) == StackTop-1; }
87 void moveToTop(unsigned RegNo, MachineBasicBlock::iterator &I) {
88 if (!isAtTop(RegNo)) {
89 unsigned Slot = getSlot(RegNo);
90 unsigned STReg = getSTReg(RegNo);
91 unsigned RegOnTop = getStackEntry(0);
92
93 // Swap the slots the regs are in
94 std::swap(RegMap[RegNo], RegMap[RegOnTop]);
95
96 // Swap stack slot contents
97 assert(RegMap[RegOnTop] < StackTop);
98 std::swap(Stack[RegMap[RegOnTop]], Stack[StackTop-1]);
99
100 // Emit an fxch to update the runtime processors version of the state
101 MachineInstr *MI = BuildMI(X86::FXCH, 1).addReg(STReg);
102 I = 1+MBB->insert(I, MI);
103 NumFXCH++;
104 }
105 }
106
107 void duplicateToTop(unsigned RegNo, unsigned AsReg,
108 MachineBasicBlock::iterator &I) {
109 unsigned STReg = getSTReg(RegNo);
110 pushReg(AsReg); // New register on top of stack
111
112 MachineInstr *MI = BuildMI(X86::FLDrr, 1).addReg(STReg);
113 I = 1+MBB->insert(I, MI);
114 }
115
116 // popStackAfter - Pop the current value off of the top of the FP stack
117 // after the specified instruction.
118 void popStackAfter(MachineBasicBlock::iterator &I);
119
120 bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB);
121
122 void handleZeroArgFP(MachineBasicBlock::iterator &I);
123 void handleOneArgFP(MachineBasicBlock::iterator &I);
124 void handleTwoArgFP(MachineBasicBlock::iterator &I);
125 void handleSpecialFP(MachineBasicBlock::iterator &I);
126 };
127}
128
Brian Gaeke19df3872003-08-13 18:18:15 +0000129FunctionPass *createX86FloatingPointStackifierPass() { return new FPS(); }
Chris Lattnera960d952003-01-13 01:01:59 +0000130
131/// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
132/// register references into FP stack references.
133///
134bool FPS::runOnMachineFunction(MachineFunction &MF) {
135 LV = &getAnalysis<LiveVariables>();
136 StackTop = 0;
137
138 bool Changed = false;
139 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
140 Changed |= processBasicBlock(MF, *I);
141 return Changed;
142}
143
144/// processBasicBlock - Loop over all of the instructions in the basic block,
145/// transforming FP instructions into their stack form.
146///
147bool FPS::processBasicBlock(MachineFunction &MF, MachineBasicBlock &BB) {
148 const TargetInstrInfo &TII = MF.getTarget().getInstrInfo();
149 bool Changed = false;
150 MBB = &BB;
151
152 for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I) {
153 MachineInstr *MI = *I;
154 MachineInstr *PrevMI = I == BB.begin() ? 0 : *(I-1);
155 unsigned Flags = TII.get(MI->getOpcode()).TSFlags;
156
157 if ((Flags & X86II::FPTypeMask) == 0) continue; // Ignore non-fp insts!
158
159 ++NumFP; // Keep track of # of pseudo instrs
160 DEBUG(std::cerr << "\nFPInst:\t";
161 MI->print(std::cerr, MF.getTarget()));
162
163 // Get dead variables list now because the MI pointer may be deleted as part
164 // of processing!
165 LiveVariables::killed_iterator IB = LV->dead_begin(MI);
166 LiveVariables::killed_iterator IE = LV->dead_end(MI);
167
168 DEBUG(const MRegisterInfo *MRI = MF.getTarget().getRegisterInfo();
169 LiveVariables::killed_iterator I = LV->killed_begin(MI);
170 LiveVariables::killed_iterator E = LV->killed_end(MI);
171 if (I != E) {
172 std::cerr << "Killed Operands:";
173 for (; I != E; ++I)
174 std::cerr << " %" << MRI->getName(I->second);
175 std::cerr << "\n";
176 });
177
178 switch (Flags & X86II::FPTypeMask) {
179 case X86II::ZeroArgFP: handleZeroArgFP(I); break;
180 case X86II::OneArgFP: handleOneArgFP(I); break;
181
182 case X86II::OneArgFPRW: // ST(0) = fsqrt(ST(0))
183 assert(0 && "FP instr type not handled yet!");
184
185 case X86II::TwoArgFP: handleTwoArgFP(I); break;
186 case X86II::SpecialFP: handleSpecialFP(I); break;
187 default: assert(0 && "Unknown FP Type!");
188 }
189
190 // Check to see if any of the values defined by this instruction are dead
191 // after definition. If so, pop them.
192 for (; IB != IE; ++IB) {
193 unsigned Reg = IB->second;
194 if (Reg >= X86::FP0 && Reg <= X86::FP6) {
195 DEBUG(std::cerr << "Register FP#" << Reg-X86::FP0 << " is dead!\n");
196 ++I; // Insert fxch AFTER the instruction
Misha Brukman5560c9d2003-08-18 14:43:39 +0000197 moveToTop(Reg-X86::FP0, I); // Insert fxch if necessary
Chris Lattnera960d952003-01-13 01:01:59 +0000198 --I; // Move to fxch or old instruction
199 popStackAfter(I); // Pop the top of the stack, killing value
200 }
201 }
202
203 // Print out all of the instructions expanded to if -debug
204 DEBUG(if (*I == PrevMI) {
205 std::cerr<< "Just deleted pseudo instruction\n";
206 } else {
207 MachineBasicBlock::iterator Start = I;
208 // Rewind to first instruction newly inserted.
209 while (Start != BB.begin() && *(Start-1) != PrevMI) --Start;
Brian Gaeked7908f62003-06-27 00:00:48 +0000210 std::cerr << "Inserted instructions:\n\t";
211 (*Start)->print(std::cerr, MF.getTarget());
Chris Lattnera960d952003-01-13 01:01:59 +0000212 while (++Start != I+1);
213 }
214 dumpStack();
215 );
216
217 Changed = true;
218 }
219
220 assert(StackTop == 0 && "Stack not empty at end of basic block?");
221 return Changed;
222}
223
224//===----------------------------------------------------------------------===//
225// Efficient Lookup Table Support
226//===----------------------------------------------------------------------===//
227
228struct TableEntry {
229 unsigned from;
230 unsigned to;
231 bool operator<(const TableEntry &TE) const { return from < TE.from; }
232 bool operator<(unsigned V) const { return from < V; }
233};
234
235static bool TableIsSorted(const TableEntry *Table, unsigned NumEntries) {
236 for (unsigned i = 0; i != NumEntries-1; ++i)
237 if (!(Table[i] < Table[i+1])) return false;
238 return true;
239}
240
241static int Lookup(const TableEntry *Table, unsigned N, unsigned Opcode) {
242 const TableEntry *I = std::lower_bound(Table, Table+N, Opcode);
243 if (I != Table+N && I->from == Opcode)
244 return I->to;
245 return -1;
246}
247
248#define ARRAY_SIZE(TABLE) \
249 (sizeof(TABLE)/sizeof(TABLE[0]))
250
251#ifdef NDEBUG
252#define ASSERT_SORTED(TABLE)
253#else
254#define ASSERT_SORTED(TABLE) \
255 { static bool TABLE##Checked = false; \
256 if (!TABLE##Checked) \
257 assert(TableIsSorted(TABLE, ARRAY_SIZE(TABLE)) && \
258 "All lookup tables must be sorted for efficient access!"); \
259 }
260#endif
261
262
263//===----------------------------------------------------------------------===//
264// Helper Methods
265//===----------------------------------------------------------------------===//
266
267// PopTable - Sorted map of instructions to their popping version. The first
268// element is an instruction, the second is the version which pops.
269//
270static const TableEntry PopTable[] = {
Chris Lattner113455b2003-08-03 21:56:36 +0000271 { X86::FADDrST0 , X86::FADDPrST0 },
272
273 { X86::FDIVRrST0, X86::FDIVRPrST0 },
274 { X86::FDIVrST0 , X86::FDIVPrST0 },
275
Chris Lattnera960d952003-01-13 01:01:59 +0000276 { X86::FISTr16 , X86::FISTPr16 },
277 { X86::FISTr32 , X86::FISTPr32 },
278
Chris Lattnera960d952003-01-13 01:01:59 +0000279 { X86::FMULrST0 , X86::FMULPrST0 },
Chris Lattnera960d952003-01-13 01:01:59 +0000280
Chris Lattner113455b2003-08-03 21:56:36 +0000281 { X86::FSTr32 , X86::FSTPr32 },
282 { X86::FSTr64 , X86::FSTPr64 },
283 { X86::FSTrr , X86::FSTPrr },
284
285 { X86::FSUBRrST0, X86::FSUBRPrST0 },
286 { X86::FSUBrST0 , X86::FSUBPrST0 },
287
Chris Lattnera960d952003-01-13 01:01:59 +0000288 { X86::FUCOMPr , X86::FUCOMPPr },
Chris Lattner113455b2003-08-03 21:56:36 +0000289 { X86::FUCOMr , X86::FUCOMPr },
Chris Lattnera960d952003-01-13 01:01:59 +0000290};
291
292/// popStackAfter - Pop the current value off of the top of the FP stack after
293/// the specified instruction. This attempts to be sneaky and combine the pop
294/// into the instruction itself if possible. The iterator is left pointing to
295/// the last instruction, be it a new pop instruction inserted, or the old
296/// instruction if it was modified in place.
297///
298void FPS::popStackAfter(MachineBasicBlock::iterator &I) {
299 ASSERT_SORTED(PopTable);
300 assert(StackTop > 0 && "Cannot pop empty stack!");
301 RegMap[Stack[--StackTop]] = ~0; // Update state
302
303 // Check to see if there is a popping version of this instruction...
304 int Opcode = Lookup(PopTable, ARRAY_SIZE(PopTable), (*I)->getOpcode());
305 if (Opcode != -1) {
306 (*I)->setOpcode(Opcode);
307 if (Opcode == X86::FUCOMPPr)
308 (*I)->RemoveOperand(0);
309
310 } else { // Insert an explicit pop
311 MachineInstr *MI = BuildMI(X86::FSTPrr, 1).addReg(X86::ST0);
312 I = MBB->insert(I+1, MI);
313 }
314}
315
316static unsigned getFPReg(const MachineOperand &MO) {
317 assert(MO.isPhysicalRegister() && "Expected an FP register!");
318 unsigned Reg = MO.getReg();
319 assert(Reg >= X86::FP0 && Reg <= X86::FP6 && "Expected FP register!");
320 return Reg - X86::FP0;
321}
322
323
324//===----------------------------------------------------------------------===//
325// Instruction transformation implementation
326//===----------------------------------------------------------------------===//
327
328/// handleZeroArgFP - ST(0) = fld0 ST(0) = flds <mem>
329//
330void FPS::handleZeroArgFP(MachineBasicBlock::iterator &I) {
331 MachineInstr *MI = *I;
332 unsigned DestReg = getFPReg(MI->getOperand(0));
333 MI->RemoveOperand(0); // Remove the explicit ST(0) operand
334
335 // Result gets pushed on the stack...
336 pushReg(DestReg);
337}
338
339/// handleOneArgFP - fst ST(0), <mem>
340//
341void FPS::handleOneArgFP(MachineBasicBlock::iterator &I) {
342 MachineInstr *MI = *I;
343 assert(MI->getNumOperands() == 5 && "Can only handle fst* instructions!");
344
345 unsigned Reg = getFPReg(MI->getOperand(4));
346 bool KillsSrc = false;
347 for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
348 E = LV->killed_end(MI); KI != E; ++KI)
349 KillsSrc |= KI->second == X86::FP0+Reg;
350
351 // FSTPr80 and FISTPr64 are strange because there are no non-popping versions.
352 // If we have one _and_ we don't want to pop the operand, duplicate the value
353 // on the stack instead of moving it. This ensure that popping the value is
354 // always ok.
355 //
356 if ((MI->getOpcode() == X86::FSTPr80 ||
357 MI->getOpcode() == X86::FISTPr64) && !KillsSrc) {
358 duplicateToTop(Reg, 7 /*temp register*/, I);
359 } else {
360 moveToTop(Reg, I); // Move to the top of the stack...
361 }
362 MI->RemoveOperand(4); // Remove explicit ST(0) operand
363
364 if (MI->getOpcode() == X86::FSTPr80 || MI->getOpcode() == X86::FISTPr64) {
365 assert(StackTop > 0 && "Stack empty??");
366 --StackTop;
367 } else if (KillsSrc) { // Last use of operand?
368 popStackAfter(I);
369 }
370}
371
372//===----------------------------------------------------------------------===//
373// Define tables of various ways to map pseudo instructions
374//
375
376// ForwardST0Table - Map: A = B op C into: ST(0) = ST(0) op ST(i)
377static const TableEntry ForwardST0Table[] = {
378 { X86::FpADD, X86::FADDST0r },
Chris Lattnera960d952003-01-13 01:01:59 +0000379 { X86::FpDIV, X86::FDIVST0r },
Chris Lattner113455b2003-08-03 21:56:36 +0000380 { X86::FpMUL, X86::FMULST0r },
381 { X86::FpSUB, X86::FSUBST0r },
Chris Lattnera960d952003-01-13 01:01:59 +0000382 { X86::FpUCOM, X86::FUCOMr },
383};
384
385// ReverseST0Table - Map: A = B op C into: ST(0) = ST(i) op ST(0)
386static const TableEntry ReverseST0Table[] = {
387 { X86::FpADD, X86::FADDST0r }, // commutative
Chris Lattnera960d952003-01-13 01:01:59 +0000388 { X86::FpDIV, X86::FDIVRST0r },
Chris Lattner113455b2003-08-03 21:56:36 +0000389 { X86::FpMUL, X86::FMULST0r }, // commutative
390 { X86::FpSUB, X86::FSUBRST0r },
Chris Lattnera960d952003-01-13 01:01:59 +0000391 { X86::FpUCOM, ~0 },
392};
393
394// ForwardSTiTable - Map: A = B op C into: ST(i) = ST(0) op ST(i)
395static const TableEntry ForwardSTiTable[] = {
396 { X86::FpADD, X86::FADDrST0 }, // commutative
Chris Lattnera960d952003-01-13 01:01:59 +0000397 { X86::FpDIV, X86::FDIVRrST0 },
Chris Lattner113455b2003-08-03 21:56:36 +0000398 { X86::FpMUL, X86::FMULrST0 }, // commutative
399 { X86::FpSUB, X86::FSUBRrST0 },
Chris Lattnera960d952003-01-13 01:01:59 +0000400 { X86::FpUCOM, X86::FUCOMr },
401};
402
403// ReverseSTiTable - Map: A = B op C into: ST(i) = ST(i) op ST(0)
404static const TableEntry ReverseSTiTable[] = {
405 { X86::FpADD, X86::FADDrST0 },
Chris Lattnera960d952003-01-13 01:01:59 +0000406 { X86::FpDIV, X86::FDIVrST0 },
Chris Lattner113455b2003-08-03 21:56:36 +0000407 { X86::FpMUL, X86::FMULrST0 },
408 { X86::FpSUB, X86::FSUBrST0 },
Chris Lattnera960d952003-01-13 01:01:59 +0000409 { X86::FpUCOM, ~0 },
410};
411
412
413/// handleTwoArgFP - Handle instructions like FADD and friends which are virtual
414/// instructions which need to be simplified and possibly transformed.
415///
416/// Result: ST(0) = fsub ST(0), ST(i)
417/// ST(i) = fsub ST(0), ST(i)
418/// ST(0) = fsubr ST(0), ST(i)
419/// ST(i) = fsubr ST(0), ST(i)
420///
421/// In addition to three address instructions, this also handles the FpUCOM
422/// instruction which only has two operands, but no destination. This
423/// instruction is also annoying because there is no "reverse" form of it
424/// available.
425///
426void FPS::handleTwoArgFP(MachineBasicBlock::iterator &I) {
427 ASSERT_SORTED(ForwardST0Table); ASSERT_SORTED(ReverseST0Table);
428 ASSERT_SORTED(ForwardSTiTable); ASSERT_SORTED(ReverseSTiTable);
429 MachineInstr *MI = *I;
430
431 unsigned NumOperands = MI->getNumOperands();
432 assert(NumOperands == 3 ||
433 (NumOperands == 2 && MI->getOpcode() == X86::FpUCOM) &&
434 "Illegal TwoArgFP instruction!");
435 unsigned Dest = getFPReg(MI->getOperand(0));
436 unsigned Op0 = getFPReg(MI->getOperand(NumOperands-2));
437 unsigned Op1 = getFPReg(MI->getOperand(NumOperands-1));
438 bool KillsOp0 = false, KillsOp1 = false;
439
440 for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
441 E = LV->killed_end(MI); KI != E; ++KI) {
442 KillsOp0 |= (KI->second == X86::FP0+Op0);
443 KillsOp1 |= (KI->second == X86::FP0+Op1);
444 }
445
446 // If this is an FpUCOM instruction, we must make sure the first operand is on
447 // the top of stack, the other one can be anywhere...
448 if (MI->getOpcode() == X86::FpUCOM)
449 moveToTop(Op0, I);
450
451 unsigned TOS = getStackEntry(0);
452
453 // One of our operands must be on the top of the stack. If neither is yet, we
454 // need to move one.
455 if (Op0 != TOS && Op1 != TOS) { // No operand at TOS?
456 // We can choose to move either operand to the top of the stack. If one of
457 // the operands is killed by this instruction, we want that one so that we
458 // can update right on top of the old version.
459 if (KillsOp0) {
460 moveToTop(Op0, I); // Move dead operand to TOS.
461 TOS = Op0;
462 } else if (KillsOp1) {
463 moveToTop(Op1, I);
464 TOS = Op1;
465 } else {
466 // All of the operands are live after this instruction executes, so we
467 // cannot update on top of any operand. Because of this, we must
468 // duplicate one of the stack elements to the top. It doesn't matter
469 // which one we pick.
470 //
471 duplicateToTop(Op0, Dest, I);
472 Op0 = TOS = Dest;
473 KillsOp0 = true;
474 }
475 } else if (!KillsOp0 && !KillsOp1 && MI->getOpcode() != X86::FpUCOM) {
476 // If we DO have one of our operands at the top of the stack, but we don't
477 // have a dead operand, we must duplicate one of the operands to a new slot
478 // on the stack.
479 duplicateToTop(Op0, Dest, I);
480 Op0 = TOS = Dest;
481 KillsOp0 = true;
482 }
483
484 // Now we know that one of our operands is on the top of the stack, and at
485 // least one of our operands is killed by this instruction.
486 assert((TOS == Op0 || TOS == Op1) &&
487 (KillsOp0 || KillsOp1 || MI->getOpcode() == X86::FpUCOM) &&
488 "Stack conditions not set up right!");
489
490 // We decide which form to use based on what is on the top of the stack, and
491 // which operand is killed by this instruction.
492 const TableEntry *InstTable;
493 bool isForward = TOS == Op0;
494 bool updateST0 = (TOS == Op0 && !KillsOp1) || (TOS == Op1 && !KillsOp0);
495 if (updateST0) {
496 if (isForward)
497 InstTable = ForwardST0Table;
498 else
499 InstTable = ReverseST0Table;
500 } else {
501 if (isForward)
502 InstTable = ForwardSTiTable;
503 else
504 InstTable = ReverseSTiTable;
505 }
506
507 int Opcode = Lookup(InstTable, ARRAY_SIZE(ForwardST0Table), MI->getOpcode());
508 assert(Opcode != -1 && "Unknown TwoArgFP pseudo instruction!");
509
510 // NotTOS - The register which is not on the top of stack...
511 unsigned NotTOS = (TOS == Op0) ? Op1 : Op0;
512
513 // Replace the old instruction with a new instruction
514 *I = BuildMI(Opcode, 1).addReg(getSTReg(NotTOS));
515
516 // If both operands are killed, pop one off of the stack in addition to
517 // overwriting the other one.
518 if (KillsOp0 && KillsOp1 && Op0 != Op1) {
519 assert(!updateST0 && "Should have updated other operand!");
520 popStackAfter(I); // Pop the top of stack
521 }
522
523 // Insert an explicit pop of the "updated" operand for FUCOM
524 if (MI->getOpcode() == X86::FpUCOM) {
525 if (KillsOp0 && !KillsOp1)
526 popStackAfter(I); // If we kill the first operand, pop it!
527 else if (KillsOp1 && Op0 != Op1) {
528 if (getStackEntry(0) == Op1) {
529 popStackAfter(I); // If it's right at the top of stack, just pop it
530 } else {
531 // Otherwise, move the top of stack into the dead slot, killing the
532 // operand without having to add in an explicit xchg then pop.
533 //
534 unsigned STReg = getSTReg(Op1);
535 unsigned OldSlot = getSlot(Op1);
536 unsigned TopReg = Stack[StackTop-1];
537 Stack[OldSlot] = TopReg;
538 RegMap[TopReg] = OldSlot;
539 RegMap[Op1] = ~0;
540 Stack[--StackTop] = ~0;
541
542 MachineInstr *MI = BuildMI(X86::FSTPrr, 1).addReg(STReg);
543 I = MBB->insert(I+1, MI);
544 }
545 }
546 }
547
548 // Update stack information so that we know the destination register is now on
549 // the stack.
550 if (MI->getOpcode() != X86::FpUCOM) {
551 unsigned UpdatedSlot = getSlot(updateST0 ? TOS : NotTOS);
552 assert(UpdatedSlot < StackTop && Dest < 7);
553 Stack[UpdatedSlot] = Dest;
554 RegMap[Dest] = UpdatedSlot;
555 }
556 delete MI; // Remove the old instruction
557}
558
559
560/// handleSpecialFP - Handle special instructions which behave unlike other
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000561/// floating point instructions. This is primarily intended for use by pseudo
Chris Lattnera960d952003-01-13 01:01:59 +0000562/// instructions.
563///
564void FPS::handleSpecialFP(MachineBasicBlock::iterator &I) {
565 MachineInstr *MI = *I;
566 switch (MI->getOpcode()) {
567 default: assert(0 && "Unknown SpecialFP instruction!");
568 case X86::FpGETRESULT: // Appears immediately after a call returning FP type!
569 assert(StackTop == 0 && "Stack should be empty after a call!");
570 pushReg(getFPReg(MI->getOperand(0)));
571 break;
572 case X86::FpSETRESULT:
573 assert(StackTop == 1 && "Stack should have one element on it to return!");
574 --StackTop; // "Forget" we have something on the top of stack!
575 break;
576 case X86::FpMOV: {
577 unsigned SrcReg = getFPReg(MI->getOperand(1));
578 unsigned DestReg = getFPReg(MI->getOperand(0));
579 bool KillsSrc = false;
580 for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
581 E = LV->killed_end(MI); KI != E; ++KI)
582 KillsSrc |= KI->second == X86::FP0+SrcReg;
583
584 if (KillsSrc) {
585 // If the input operand is killed, we can just change the owner of the
586 // incoming stack slot into the result.
587 unsigned Slot = getSlot(SrcReg);
588 assert(Slot < 7 && DestReg < 7 && "FpMOV operands invalid!");
589 Stack[Slot] = DestReg;
590 RegMap[DestReg] = Slot;
591
592 } else {
593 // For FMOV we just duplicate the specified value to a new stack slot.
594 // This could be made better, but would require substantial changes.
595 duplicateToTop(SrcReg, DestReg, I);
596 }
597 break;
598 }
599 }
600
601 I = MBB->erase(I)-1; // Remove the pseudo instruction
602}
Brian Gaeked0fde302003-11-11 22:41:34 +0000603
604} // End llvm namespace