1 //===-- FloatingPoint.cpp - Floating point Reg -> Stack converter ---------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines the pass which converts floating point instructions from
11 // virtual registers into register stack instructions.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "fp"
17 #include "X86InstrInfo.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/LiveVariables.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "Support/Debug.h"
25 #include "Support/Statistic.h"
31 Statistic<> NumFXCH("x86-codegen", "Number of fxch instructions inserted");
32 Statistic<> NumFP ("x86-codegen", "Number of floating point instructions");
34 struct FPS : public MachineFunctionPass {
35 virtual bool runOnMachineFunction(MachineFunction &MF);
37 virtual const char *getPassName() const { return "X86 FP Stackifier"; }
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequired<LiveVariables>();
41 MachineFunctionPass::getAnalysisUsage(AU);
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.
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[]!");
59 // getSlot - Return the stack slot number a particular register number is
61 unsigned getSlot(unsigned RegNo) const {
62 assert(RegNo < 8 && "Regno out of range!");
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];
72 // getSTReg - Return the X86::ST(i) register which contains the specified
74 unsigned getSTReg(unsigned RegNo) const {
75 return StackTop - 1 - getSlot(RegNo) + llvm::X86::ST0;
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++;
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);
93 // Swap the slots the regs are in
94 std::swap(RegMap[RegNo], RegMap[RegOnTop]);
96 // Swap stack slot contents
97 assert(RegMap[RegOnTop] < StackTop);
98 std::swap(Stack[RegMap[RegOnTop]], Stack[StackTop-1]);
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);
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
112 MachineInstr *MI = BuildMI(X86::FLDrr, 1).addReg(STReg);
113 I = 1+MBB->insert(I, MI);
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);
120 bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB);
122 void handleZeroArgFP(MachineBasicBlock::iterator &I);
123 void handleOneArgFP(MachineBasicBlock::iterator &I);
124 void handleTwoArgFP(MachineBasicBlock::iterator &I);
125 void handleSpecialFP(MachineBasicBlock::iterator &I);
129 FunctionPass *llvm::createX86FloatingPointStackifierPass() { return new FPS(); }
131 /// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
132 /// register references into FP stack references.
134 bool FPS::runOnMachineFunction(MachineFunction &MF) {
135 LV = &getAnalysis<LiveVariables>();
138 bool Changed = false;
139 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
140 Changed |= processBasicBlock(MF, *I);
144 /// processBasicBlock - Loop over all of the instructions in the basic block,
145 /// transforming FP instructions into their stack form.
147 bool FPS::processBasicBlock(MachineFunction &MF, MachineBasicBlock &BB) {
148 const TargetInstrInfo &TII = MF.getTarget().getInstrInfo();
149 bool Changed = false;
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;
157 if ((Flags & X86II::FPTypeMask) == 0) continue; // Ignore non-fp insts!
159 ++NumFP; // Keep track of # of pseudo instrs
160 DEBUG(std::cerr << "\nFPInst:\t";
161 MI->print(std::cerr, MF.getTarget()));
163 // Get dead variables list now because the MI pointer may be deleted as part
165 LiveVariables::killed_iterator IB = LV->dead_begin(MI);
166 LiveVariables::killed_iterator IE = LV->dead_end(MI);
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);
172 std::cerr << "Killed Operands:";
174 std::cerr << " %" << MRI->getName(I->second);
178 switch (Flags & X86II::FPTypeMask) {
179 case X86II::ZeroArgFP: handleZeroArgFP(I); break;
180 case X86II::OneArgFP: handleOneArgFP(I); break;
182 case X86II::OneArgFPRW: // ST(0) = fsqrt(ST(0))
183 assert(0 && "FP instr type not handled yet!");
185 case X86II::TwoArgFP: handleTwoArgFP(I); break;
186 case X86II::SpecialFP: handleSpecialFP(I); break;
187 default: assert(0 && "Unknown FP Type!");
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
197 moveToTop(Reg-X86::FP0, I); // Insert fxch if necessary
198 --I; // Move to fxch or old instruction
199 popStackAfter(I); // Pop the top of the stack, killing value
203 // Print out all of the instructions expanded to if -debug
204 DEBUG(if (*I == PrevMI) {
205 std::cerr<< "Just deleted pseudo instruction\n";
207 MachineBasicBlock::iterator Start = I;
208 // Rewind to first instruction newly inserted.
209 while (Start != BB.begin() && *(Start-1) != PrevMI) --Start;
210 std::cerr << "Inserted instructions:\n\t";
211 (*Start)->print(std::cerr, MF.getTarget());
212 while (++Start != I+1);
220 assert(StackTop == 0 && "Stack not empty at end of basic block?");
224 //===----------------------------------------------------------------------===//
225 // Efficient Lookup Table Support
226 //===----------------------------------------------------------------------===//
232 bool operator<(const TableEntry &TE) const { return from < TE.from; }
233 bool operator<(unsigned V) const { return from < V; }
237 static 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;
243 static 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)
250 #define ARRAY_SIZE(TABLE) \
251 (sizeof(TABLE)/sizeof(TABLE[0]))
254 #define ASSERT_SORTED(TABLE)
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!"); \
265 //===----------------------------------------------------------------------===//
267 //===----------------------------------------------------------------------===//
269 // PopTable - Sorted map of instructions to their popping version. The first
270 // element is an instruction, the second is the version which pops.
272 static const TableEntry PopTable[] = {
273 { X86::FADDrST0 , X86::FADDPrST0 },
275 { X86::FDIVRrST0, X86::FDIVRPrST0 },
276 { X86::FDIVrST0 , X86::FDIVPrST0 },
278 { X86::FISTr16 , X86::FISTPr16 },
279 { X86::FISTr32 , X86::FISTPr32 },
281 { X86::FMULrST0 , X86::FMULPrST0 },
283 { X86::FSTr32 , X86::FSTPr32 },
284 { X86::FSTr64 , X86::FSTPr64 },
285 { X86::FSTrr , X86::FSTPrr },
287 { X86::FSUBRrST0, X86::FSUBRPrST0 },
288 { X86::FSUBrST0 , X86::FSUBPrST0 },
290 { X86::FUCOMPr , X86::FUCOMPPr },
291 { X86::FUCOMr , X86::FUCOMPr },
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.
300 void FPS::popStackAfter(MachineBasicBlock::iterator &I) {
301 ASSERT_SORTED(PopTable);
302 assert(StackTop > 0 && "Cannot pop empty stack!");
303 RegMap[Stack[--StackTop]] = ~0; // Update state
305 // Check to see if there is a popping version of this instruction...
306 int Opcode = Lookup(PopTable, ARRAY_SIZE(PopTable), (*I)->getOpcode());
308 (*I)->setOpcode(Opcode);
309 if (Opcode == X86::FUCOMPPr)
310 (*I)->RemoveOperand(0);
312 } else { // Insert an explicit pop
313 MachineInstr *MI = BuildMI(X86::FSTPrr, 1).addReg(X86::ST0);
314 I = MBB->insert(I+1, MI);
318 static 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;
326 //===----------------------------------------------------------------------===//
327 // Instruction transformation implementation
328 //===----------------------------------------------------------------------===//
330 /// handleZeroArgFP - ST(0) = fld0 ST(0) = flds <mem>
332 void 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
337 // Result gets pushed on the stack...
341 /// handleOneArgFP - fst ST(0), <mem>
343 void FPS::handleOneArgFP(MachineBasicBlock::iterator &I) {
344 MachineInstr *MI = *I;
345 assert(MI->getNumOperands() == 5 && "Can only handle fst* instructions!");
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;
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
358 if ((MI->getOpcode() == X86::FSTPr80 ||
359 MI->getOpcode() == X86::FISTPr64) && !KillsSrc) {
360 duplicateToTop(Reg, 7 /*temp register*/, I);
362 moveToTop(Reg, I); // Move to the top of the stack...
364 MI->RemoveOperand(4); // Remove explicit ST(0) operand
366 if (MI->getOpcode() == X86::FSTPr80 || MI->getOpcode() == X86::FISTPr64) {
367 assert(StackTop > 0 && "Stack empty??");
369 } else if (KillsSrc) { // Last use of operand?
374 //===----------------------------------------------------------------------===//
375 // Define tables of various ways to map pseudo instructions
378 // ForwardST0Table - Map: A = B op C into: ST(0) = ST(0) op ST(i)
379 static const TableEntry ForwardST0Table[] = {
380 { X86::FpADD, X86::FADDST0r },
381 { X86::FpDIV, X86::FDIVST0r },
382 { X86::FpMUL, X86::FMULST0r },
383 { X86::FpSUB, X86::FSUBST0r },
384 { X86::FpUCOM, X86::FUCOMr },
387 // ReverseST0Table - Map: A = B op C into: ST(0) = ST(i) op ST(0)
388 static const TableEntry ReverseST0Table[] = {
389 { X86::FpADD, X86::FADDST0r }, // commutative
390 { X86::FpDIV, X86::FDIVRST0r },
391 { X86::FpMUL, X86::FMULST0r }, // commutative
392 { X86::FpSUB, X86::FSUBRST0r },
396 // ForwardSTiTable - Map: A = B op C into: ST(i) = ST(0) op ST(i)
397 static const TableEntry ForwardSTiTable[] = {
398 { X86::FpADD, X86::FADDrST0 }, // commutative
399 { X86::FpDIV, X86::FDIVRrST0 },
400 { X86::FpMUL, X86::FMULrST0 }, // commutative
401 { X86::FpSUB, X86::FSUBRrST0 },
402 { X86::FpUCOM, X86::FUCOMr },
405 // ReverseSTiTable - Map: A = B op C into: ST(i) = ST(i) op ST(0)
406 static const TableEntry ReverseSTiTable[] = {
407 { X86::FpADD, X86::FADDrST0 },
408 { X86::FpDIV, X86::FDIVrST0 },
409 { X86::FpMUL, X86::FMULrST0 },
410 { X86::FpSUB, X86::FSUBrST0 },
415 /// handleTwoArgFP - Handle instructions like FADD and friends which are virtual
416 /// instructions which need to be simplified and possibly transformed.
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)
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
428 void FPS::handleTwoArgFP(MachineBasicBlock::iterator &I) {
429 ASSERT_SORTED(ForwardST0Table); ASSERT_SORTED(ReverseST0Table);
430 ASSERT_SORTED(ForwardSTiTable); ASSERT_SORTED(ReverseSTiTable);
431 MachineInstr *MI = *I;
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;
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);
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)
453 unsigned TOS = getStackEntry(0);
455 // One of our operands must be on the top of the stack. If neither is yet, we
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.
462 moveToTop(Op0, I); // Move dead operand to TOS.
464 } else if (KillsOp1) {
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.
473 duplicateToTop(Op0, Dest, I);
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
481 duplicateToTop(Op0, Dest, I);
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!");
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);
499 InstTable = ForwardST0Table;
501 InstTable = ReverseST0Table;
504 InstTable = ForwardSTiTable;
506 InstTable = ReverseSTiTable;
509 int Opcode = Lookup(InstTable, ARRAY_SIZE(ForwardST0Table), MI->getOpcode());
510 assert(Opcode != -1 && "Unknown TwoArgFP pseudo instruction!");
512 // NotTOS - The register which is not on the top of stack...
513 unsigned NotTOS = (TOS == Op0) ? Op1 : Op0;
515 // Replace the old instruction with a new instruction
516 *I = BuildMI(Opcode, 1).addReg(getSTReg(NotTOS));
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
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
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.
536 unsigned STReg = getSTReg(Op1);
537 unsigned OldSlot = getSlot(Op1);
538 unsigned TopReg = Stack[StackTop-1];
539 Stack[OldSlot] = TopReg;
540 RegMap[TopReg] = OldSlot;
542 Stack[--StackTop] = ~0;
544 MachineInstr *MI = BuildMI(X86::FSTPrr, 1).addReg(STReg);
545 I = MBB->insert(I+1, MI);
550 // Update stack information so that we know the destination register is now on
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;
558 delete MI; // Remove the old instruction
562 /// handleSpecialFP - Handle special instructions which behave unlike other
563 /// floating point instructions. This is primarily intended for use by pseudo
566 void 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)));
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!
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;
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;
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);
603 I = MBB->erase(I)-1; // Remove the pseudo instruction