blob: 7bc1f728f89515cb9b0f19dd7b916f194a686aad [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"
Alkis Evlogimenos359b65f2003-12-13 05:36:22 +000021#include "llvm/CodeGen/Passes.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000022#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnera960d952003-01-13 01:01:59 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnera11136b2003-08-01 22:21:34 +000024#include "Support/Debug.h"
Chris Lattnera960d952003-01-13 01:01:59 +000025#include "Support/Statistic.h"
26#include <algorithm>
27#include <iostream>
Chris Lattnerf2e49d42003-12-20 09:58:55 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
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
Chris Lattnerf2e49d42003-12-20 09:58:55 +0000129FunctionPass *llvm::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
Chris Lattnerf2e49d42003-12-20 09:58:55 +0000228namespace {
229 struct TableEntry {
230 unsigned from;
231 unsigned to;
232 bool operator<(const TableEntry &TE) const { return from < TE.from; }
233 bool operator<(unsigned V) const { return from < V; }
234 };
235}
Chris Lattnera960d952003-01-13 01:01:59 +0000236
237static bool TableIsSorted(const TableEntry *Table, unsigned NumEntries) {
238 for (unsigned i = 0; i != NumEntries-1; ++i)
239 if (!(Table[i] < Table[i+1])) return false;
240 return true;
241}
242
243static int Lookup(const TableEntry *Table, unsigned N, unsigned Opcode) {
244 const TableEntry *I = std::lower_bound(Table, Table+N, Opcode);
245 if (I != Table+N && I->from == Opcode)
246 return I->to;
247 return -1;
248}
249
250#define ARRAY_SIZE(TABLE) \
251 (sizeof(TABLE)/sizeof(TABLE[0]))
252
253#ifdef NDEBUG
254#define ASSERT_SORTED(TABLE)
255#else
256#define ASSERT_SORTED(TABLE) \
257 { static bool TABLE##Checked = false; \
258 if (!TABLE##Checked) \
259 assert(TableIsSorted(TABLE, ARRAY_SIZE(TABLE)) && \
260 "All lookup tables must be sorted for efficient access!"); \
261 }
262#endif
263
264
265//===----------------------------------------------------------------------===//
266// Helper Methods
267//===----------------------------------------------------------------------===//
268
269// PopTable - Sorted map of instructions to their popping version. The first
270// element is an instruction, the second is the version which pops.
271//
272static const TableEntry PopTable[] = {
Chris Lattner113455b2003-08-03 21:56:36 +0000273 { X86::FADDrST0 , X86::FADDPrST0 },
274
275 { X86::FDIVRrST0, X86::FDIVRPrST0 },
276 { X86::FDIVrST0 , X86::FDIVPrST0 },
277
Chris Lattnera960d952003-01-13 01:01:59 +0000278 { X86::FISTr16 , X86::FISTPr16 },
279 { X86::FISTr32 , X86::FISTPr32 },
280
Chris Lattnera960d952003-01-13 01:01:59 +0000281 { X86::FMULrST0 , X86::FMULPrST0 },
Chris Lattnera960d952003-01-13 01:01:59 +0000282
Chris Lattner113455b2003-08-03 21:56:36 +0000283 { X86::FSTr32 , X86::FSTPr32 },
284 { X86::FSTr64 , X86::FSTPr64 },
285 { X86::FSTrr , X86::FSTPrr },
286
287 { X86::FSUBRrST0, X86::FSUBRPrST0 },
288 { X86::FSUBrST0 , X86::FSUBPrST0 },
289
Chris Lattnera960d952003-01-13 01:01:59 +0000290 { X86::FUCOMPr , X86::FUCOMPPr },
Chris Lattner113455b2003-08-03 21:56:36 +0000291 { X86::FUCOMr , X86::FUCOMPr },
Chris Lattnera960d952003-01-13 01:01:59 +0000292};
293
294/// popStackAfter - Pop the current value off of the top of the FP stack after
295/// the specified instruction. This attempts to be sneaky and combine the pop
296/// into the instruction itself if possible. The iterator is left pointing to
297/// the last instruction, be it a new pop instruction inserted, or the old
298/// instruction if it was modified in place.
299///
300void FPS::popStackAfter(MachineBasicBlock::iterator &I) {
301 ASSERT_SORTED(PopTable);
302 assert(StackTop > 0 && "Cannot pop empty stack!");
303 RegMap[Stack[--StackTop]] = ~0; // Update state
304
305 // Check to see if there is a popping version of this instruction...
306 int Opcode = Lookup(PopTable, ARRAY_SIZE(PopTable), (*I)->getOpcode());
307 if (Opcode != -1) {
308 (*I)->setOpcode(Opcode);
309 if (Opcode == X86::FUCOMPPr)
310 (*I)->RemoveOperand(0);
311
312 } else { // Insert an explicit pop
313 MachineInstr *MI = BuildMI(X86::FSTPrr, 1).addReg(X86::ST0);
314 I = MBB->insert(I+1, MI);
315 }
316}
317
318static unsigned getFPReg(const MachineOperand &MO) {
319 assert(MO.isPhysicalRegister() && "Expected an FP register!");
320 unsigned Reg = MO.getReg();
321 assert(Reg >= X86::FP0 && Reg <= X86::FP6 && "Expected FP register!");
322 return Reg - X86::FP0;
323}
324
325
326//===----------------------------------------------------------------------===//
327// Instruction transformation implementation
328//===----------------------------------------------------------------------===//
329
330/// handleZeroArgFP - ST(0) = fld0 ST(0) = flds <mem>
331//
332void FPS::handleZeroArgFP(MachineBasicBlock::iterator &I) {
333 MachineInstr *MI = *I;
334 unsigned DestReg = getFPReg(MI->getOperand(0));
335 MI->RemoveOperand(0); // Remove the explicit ST(0) operand
336
337 // Result gets pushed on the stack...
338 pushReg(DestReg);
339}
340
341/// handleOneArgFP - fst ST(0), <mem>
342//
343void FPS::handleOneArgFP(MachineBasicBlock::iterator &I) {
344 MachineInstr *MI = *I;
345 assert(MI->getNumOperands() == 5 && "Can only handle fst* instructions!");
346
347 unsigned Reg = getFPReg(MI->getOperand(4));
348 bool KillsSrc = false;
349 for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
350 E = LV->killed_end(MI); KI != E; ++KI)
351 KillsSrc |= KI->second == X86::FP0+Reg;
352
353 // FSTPr80 and FISTPr64 are strange because there are no non-popping versions.
354 // If we have one _and_ we don't want to pop the operand, duplicate the value
355 // on the stack instead of moving it. This ensure that popping the value is
356 // always ok.
357 //
358 if ((MI->getOpcode() == X86::FSTPr80 ||
359 MI->getOpcode() == X86::FISTPr64) && !KillsSrc) {
360 duplicateToTop(Reg, 7 /*temp register*/, I);
361 } else {
362 moveToTop(Reg, I); // Move to the top of the stack...
363 }
364 MI->RemoveOperand(4); // Remove explicit ST(0) operand
365
366 if (MI->getOpcode() == X86::FSTPr80 || MI->getOpcode() == X86::FISTPr64) {
367 assert(StackTop > 0 && "Stack empty??");
368 --StackTop;
369 } else if (KillsSrc) { // Last use of operand?
370 popStackAfter(I);
371 }
372}
373
374//===----------------------------------------------------------------------===//
375// Define tables of various ways to map pseudo instructions
376//
377
378// ForwardST0Table - Map: A = B op C into: ST(0) = ST(0) op ST(i)
379static const TableEntry ForwardST0Table[] = {
380 { X86::FpADD, X86::FADDST0r },
Chris Lattnera960d952003-01-13 01:01:59 +0000381 { X86::FpDIV, X86::FDIVST0r },
Chris Lattner113455b2003-08-03 21:56:36 +0000382 { X86::FpMUL, X86::FMULST0r },
383 { X86::FpSUB, X86::FSUBST0r },
Chris Lattnera960d952003-01-13 01:01:59 +0000384 { X86::FpUCOM, X86::FUCOMr },
385};
386
387// ReverseST0Table - Map: A = B op C into: ST(0) = ST(i) op ST(0)
388static const TableEntry ReverseST0Table[] = {
389 { X86::FpADD, X86::FADDST0r }, // commutative
Chris Lattnera960d952003-01-13 01:01:59 +0000390 { X86::FpDIV, X86::FDIVRST0r },
Chris Lattner113455b2003-08-03 21:56:36 +0000391 { X86::FpMUL, X86::FMULST0r }, // commutative
392 { X86::FpSUB, X86::FSUBRST0r },
Chris Lattnera960d952003-01-13 01:01:59 +0000393 { X86::FpUCOM, ~0 },
394};
395
396// ForwardSTiTable - Map: A = B op C into: ST(i) = ST(0) op ST(i)
397static const TableEntry ForwardSTiTable[] = {
398 { X86::FpADD, X86::FADDrST0 }, // commutative
Chris Lattnera960d952003-01-13 01:01:59 +0000399 { X86::FpDIV, X86::FDIVRrST0 },
Chris Lattner113455b2003-08-03 21:56:36 +0000400 { X86::FpMUL, X86::FMULrST0 }, // commutative
401 { X86::FpSUB, X86::FSUBRrST0 },
Chris Lattnera960d952003-01-13 01:01:59 +0000402 { X86::FpUCOM, X86::FUCOMr },
403};
404
405// ReverseSTiTable - Map: A = B op C into: ST(i) = ST(i) op ST(0)
406static const TableEntry ReverseSTiTable[] = {
407 { X86::FpADD, X86::FADDrST0 },
Chris Lattnera960d952003-01-13 01:01:59 +0000408 { X86::FpDIV, X86::FDIVrST0 },
Chris Lattner113455b2003-08-03 21:56:36 +0000409 { X86::FpMUL, X86::FMULrST0 },
410 { X86::FpSUB, X86::FSUBrST0 },
Chris Lattnera960d952003-01-13 01:01:59 +0000411 { X86::FpUCOM, ~0 },
412};
413
414
415/// handleTwoArgFP - Handle instructions like FADD and friends which are virtual
416/// instructions which need to be simplified and possibly transformed.
417///
418/// Result: ST(0) = fsub ST(0), ST(i)
419/// ST(i) = fsub ST(0), ST(i)
420/// ST(0) = fsubr ST(0), ST(i)
421/// ST(i) = fsubr ST(0), ST(i)
422///
423/// In addition to three address instructions, this also handles the FpUCOM
424/// instruction which only has two operands, but no destination. This
425/// instruction is also annoying because there is no "reverse" form of it
426/// available.
427///
428void FPS::handleTwoArgFP(MachineBasicBlock::iterator &I) {
429 ASSERT_SORTED(ForwardST0Table); ASSERT_SORTED(ReverseST0Table);
430 ASSERT_SORTED(ForwardSTiTable); ASSERT_SORTED(ReverseSTiTable);
431 MachineInstr *MI = *I;
432
433 unsigned NumOperands = MI->getNumOperands();
434 assert(NumOperands == 3 ||
435 (NumOperands == 2 && MI->getOpcode() == X86::FpUCOM) &&
436 "Illegal TwoArgFP instruction!");
437 unsigned Dest = getFPReg(MI->getOperand(0));
438 unsigned Op0 = getFPReg(MI->getOperand(NumOperands-2));
439 unsigned Op1 = getFPReg(MI->getOperand(NumOperands-1));
440 bool KillsOp0 = false, KillsOp1 = false;
441
442 for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
443 E = LV->killed_end(MI); KI != E; ++KI) {
444 KillsOp0 |= (KI->second == X86::FP0+Op0);
445 KillsOp1 |= (KI->second == X86::FP0+Op1);
446 }
447
448 // If this is an FpUCOM instruction, we must make sure the first operand is on
449 // the top of stack, the other one can be anywhere...
450 if (MI->getOpcode() == X86::FpUCOM)
451 moveToTop(Op0, I);
452
453 unsigned TOS = getStackEntry(0);
454
455 // One of our operands must be on the top of the stack. If neither is yet, we
456 // need to move one.
457 if (Op0 != TOS && Op1 != TOS) { // No operand at TOS?
458 // We can choose to move either operand to the top of the stack. If one of
459 // the operands is killed by this instruction, we want that one so that we
460 // can update right on top of the old version.
461 if (KillsOp0) {
462 moveToTop(Op0, I); // Move dead operand to TOS.
463 TOS = Op0;
464 } else if (KillsOp1) {
465 moveToTop(Op1, I);
466 TOS = Op1;
467 } else {
468 // All of the operands are live after this instruction executes, so we
469 // cannot update on top of any operand. Because of this, we must
470 // duplicate one of the stack elements to the top. It doesn't matter
471 // which one we pick.
472 //
473 duplicateToTop(Op0, Dest, I);
474 Op0 = TOS = Dest;
475 KillsOp0 = true;
476 }
477 } else if (!KillsOp0 && !KillsOp1 && MI->getOpcode() != X86::FpUCOM) {
478 // If we DO have one of our operands at the top of the stack, but we don't
479 // have a dead operand, we must duplicate one of the operands to a new slot
480 // on the stack.
481 duplicateToTop(Op0, Dest, I);
482 Op0 = TOS = Dest;
483 KillsOp0 = true;
484 }
485
486 // Now we know that one of our operands is on the top of the stack, and at
487 // least one of our operands is killed by this instruction.
488 assert((TOS == Op0 || TOS == Op1) &&
489 (KillsOp0 || KillsOp1 || MI->getOpcode() == X86::FpUCOM) &&
490 "Stack conditions not set up right!");
491
492 // We decide which form to use based on what is on the top of the stack, and
493 // which operand is killed by this instruction.
494 const TableEntry *InstTable;
495 bool isForward = TOS == Op0;
496 bool updateST0 = (TOS == Op0 && !KillsOp1) || (TOS == Op1 && !KillsOp0);
497 if (updateST0) {
498 if (isForward)
499 InstTable = ForwardST0Table;
500 else
501 InstTable = ReverseST0Table;
502 } else {
503 if (isForward)
504 InstTable = ForwardSTiTable;
505 else
506 InstTable = ReverseSTiTable;
507 }
508
509 int Opcode = Lookup(InstTable, ARRAY_SIZE(ForwardST0Table), MI->getOpcode());
510 assert(Opcode != -1 && "Unknown TwoArgFP pseudo instruction!");
511
512 // NotTOS - The register which is not on the top of stack...
513 unsigned NotTOS = (TOS == Op0) ? Op1 : Op0;
514
515 // Replace the old instruction with a new instruction
516 *I = BuildMI(Opcode, 1).addReg(getSTReg(NotTOS));
517
518 // If both operands are killed, pop one off of the stack in addition to
519 // overwriting the other one.
520 if (KillsOp0 && KillsOp1 && Op0 != Op1) {
521 assert(!updateST0 && "Should have updated other operand!");
522 popStackAfter(I); // Pop the top of stack
523 }
524
525 // Insert an explicit pop of the "updated" operand for FUCOM
526 if (MI->getOpcode() == X86::FpUCOM) {
527 if (KillsOp0 && !KillsOp1)
528 popStackAfter(I); // If we kill the first operand, pop it!
529 else if (KillsOp1 && Op0 != Op1) {
530 if (getStackEntry(0) == Op1) {
531 popStackAfter(I); // If it's right at the top of stack, just pop it
532 } else {
533 // Otherwise, move the top of stack into the dead slot, killing the
534 // operand without having to add in an explicit xchg then pop.
535 //
536 unsigned STReg = getSTReg(Op1);
537 unsigned OldSlot = getSlot(Op1);
538 unsigned TopReg = Stack[StackTop-1];
539 Stack[OldSlot] = TopReg;
540 RegMap[TopReg] = OldSlot;
541 RegMap[Op1] = ~0;
542 Stack[--StackTop] = ~0;
543
544 MachineInstr *MI = BuildMI(X86::FSTPrr, 1).addReg(STReg);
545 I = MBB->insert(I+1, MI);
546 }
547 }
548 }
549
550 // Update stack information so that we know the destination register is now on
551 // the stack.
552 if (MI->getOpcode() != X86::FpUCOM) {
553 unsigned UpdatedSlot = getSlot(updateST0 ? TOS : NotTOS);
554 assert(UpdatedSlot < StackTop && Dest < 7);
555 Stack[UpdatedSlot] = Dest;
556 RegMap[Dest] = UpdatedSlot;
557 }
558 delete MI; // Remove the old instruction
559}
560
561
562/// handleSpecialFP - Handle special instructions which behave unlike other
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000563/// floating point instructions. This is primarily intended for use by pseudo
Chris Lattnera960d952003-01-13 01:01:59 +0000564/// instructions.
565///
566void FPS::handleSpecialFP(MachineBasicBlock::iterator &I) {
567 MachineInstr *MI = *I;
568 switch (MI->getOpcode()) {
569 default: assert(0 && "Unknown SpecialFP instruction!");
570 case X86::FpGETRESULT: // Appears immediately after a call returning FP type!
571 assert(StackTop == 0 && "Stack should be empty after a call!");
572 pushReg(getFPReg(MI->getOperand(0)));
573 break;
574 case X86::FpSETRESULT:
575 assert(StackTop == 1 && "Stack should have one element on it to return!");
576 --StackTop; // "Forget" we have something on the top of stack!
577 break;
578 case X86::FpMOV: {
579 unsigned SrcReg = getFPReg(MI->getOperand(1));
580 unsigned DestReg = getFPReg(MI->getOperand(0));
581 bool KillsSrc = false;
582 for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
583 E = LV->killed_end(MI); KI != E; ++KI)
584 KillsSrc |= KI->second == X86::FP0+SrcReg;
585
586 if (KillsSrc) {
587 // If the input operand is killed, we can just change the owner of the
588 // incoming stack slot into the result.
589 unsigned Slot = getSlot(SrcReg);
590 assert(Slot < 7 && DestReg < 7 && "FpMOV operands invalid!");
591 Stack[Slot] = DestReg;
592 RegMap[DestReg] = Slot;
593
594 } else {
595 // For FMOV we just duplicate the specified value to a new stack slot.
596 // This could be made better, but would require substantial changes.
597 duplicateToTop(SrcReg, DestReg, I);
598 }
599 break;
600 }
601 }
602
603 I = MBB->erase(I)-1; // Remove the pseudo instruction
604}
Brian Gaeked0fde302003-11-11 22:41:34 +0000605
Alkis Evlogimenos359b65f2003-12-13 05:36:22 +0000606namespace {
Alkis Evlogimenos359b65f2003-12-13 05:36:22 +0000607 struct FPK : public MachineFunctionPass {
608 virtual const char *getPassName() const { return "X86 FP Killer"; }
609 virtual bool runOnMachineFunction(MachineFunction &MF);
610 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Alkis Evlogimenos97323a42003-12-14 10:14:23 +0000611 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos359b65f2003-12-13 05:36:22 +0000612 AU.addRequired<LiveVariables>();
Alkis Evlogimenos97323a42003-12-14 10:14:23 +0000613 AU.addPreservedID(PHIEliminationID);
Alkis Evlogimenos359b65f2003-12-13 05:36:22 +0000614 AU.addRequiredID(PHIEliminationID);
615 MachineFunctionPass::getAnalysisUsage(AU);
616 }
617 };
618}
619
Chris Lattnerf2e49d42003-12-20 09:58:55 +0000620FunctionPass *llvm::createX86FloatingPointKillerPass() { return new FPK(); }
Alkis Evlogimenos359b65f2003-12-13 05:36:22 +0000621
Chris Lattnerf2e49d42003-12-20 09:58:55 +0000622bool FPK::runOnMachineFunction(MachineFunction &MF) {
Alkis Evlogimenos359b65f2003-12-13 05:36:22 +0000623 const TargetInstrInfo& tii = MF.getTarget().getInstrInfo();;
Chris Lattnerf2e49d42003-12-20 09:58:55 +0000624 LiveVariables &LV = getAnalysis<LiveVariables>();
Alkis Evlogimenos359b65f2003-12-13 05:36:22 +0000625
626 for (MachineFunction::iterator
627 mbbi = MF.begin(), mbbe = MF.end(); mbbi != mbbe; ++mbbi) {
628 MachineBasicBlock& mbb = *mbbi;
629 MachineBasicBlock::reverse_iterator mii = mbb.rbegin();
630 // rewind to the last non terminating instruction
Chris Lattnerf2e49d42003-12-20 09:58:55 +0000631 while (mii != mbb.rend() && tii.isTerminatorInstr((*mii)->getOpcode()))
Alkis Evlogimenos359b65f2003-12-13 05:36:22 +0000632 ++mii;
Chris Lattnerf2e49d42003-12-20 09:58:55 +0000633
Alkis Evlogimenos359b65f2003-12-13 05:36:22 +0000634 // add implicit def for all virtual floating point registers so that
635 // they are spilled at the end of each basic block, since our
636 // register stackifier doesn't handle them otherwise.
637 MachineInstr* instr = BuildMI(X86::IMPLICIT_DEF, 7)
638 .addReg(X86::FP6, MOTy::Def)
639 .addReg(X86::FP5, MOTy::Def)
640 .addReg(X86::FP4, MOTy::Def)
641 .addReg(X86::FP3, MOTy::Def)
642 .addReg(X86::FP2, MOTy::Def)
643 .addReg(X86::FP1, MOTy::Def)
644 .addReg(X86::FP0, MOTy::Def);
645
646 mbb.insert(mii.base(), instr);
Chris Lattnerf2e49d42003-12-20 09:58:55 +0000647
Alkis Evlogimenos359b65f2003-12-13 05:36:22 +0000648 for (unsigned i = 0; i < instr->getNumOperands(); ++i) {
Chris Lattnerf2e49d42003-12-20 09:58:55 +0000649 LV.HandlePhysRegDef(instr->getOperand(i).getAllocatedRegNum(), instr);
650
651 // force live variables to compute that these registers are dead
652 LV.HandlePhysRegDef(instr->getOperand(i).getAllocatedRegNum(), 0);
Alkis Evlogimenos359b65f2003-12-13 05:36:22 +0000653 }
654 }
655 return true;
656}