Misha Brukman | 91b5ca8 | 2004-07-26 18:45:48 +0000 | [diff] [blame] | 1 | //===-- X86FloatingPoint.cpp - Floating point Reg -> Stack converter ------===// |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines the pass which converts floating point instructions from |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 11 | // pseudo registers into register stack instructions. This pass uses live |
Chris Lattner | 847df25 | 2004-01-30 22:25:18 +0000 | [diff] [blame] | 12 | // variable information to indicate where the FPn registers are used and their |
| 13 | // lifetimes. |
| 14 | // |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 15 | // 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 Lattner | 847df25 | 2004-01-30 22:25:18 +0000 | [diff] [blame] | 20 | // |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 21 | // This approach avoids splitting critical edges at the potential cost of more |
| 22 | // live register shuffling instructions when critical edges are present. |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 23 | // |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Chris Lattner | 95b2c7d | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 26 | #define DEBUG_TYPE "x86-codegen" |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 27 | #include "X86.h" |
| 28 | #include "X86InstrInfo.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DepthFirstIterator.h" |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/DenseMap.h" |
Owen Anderson | eaa009d | 2008-08-14 21:01:00 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallPtrSet.h" |
Evan Cheng | ddd2a45 | 2006-11-15 20:56:39 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/Statistic.h" |
| 34 | #include "llvm/ADT/STLExtras.h" |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame^] | 35 | #include "llvm/CodeGen/EdgeBundles.h" |
Bill Wendling | 0ea8bf3 | 2009-08-03 00:11:34 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 37 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 38 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 39 | #include "llvm/CodeGen/Passes.h" |
Bill Wendling | 0ea8bf3 | 2009-08-03 00:11:34 +0000 | [diff] [blame] | 40 | #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 Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 45 | #include <algorithm> |
Chris Lattner | f2e49d4 | 2003-12-20 09:58:55 +0000 | [diff] [blame] | 46 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 95b2c7d | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 48 | STATISTIC(NumFXCH, "Number of fxch instructions inserted"); |
| 49 | STATISTIC(NumFP , "Number of floating point instructions"); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 95b2c7d | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 51 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 52 | struct FPS : public MachineFunctionPass { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 53 | static char ID; |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 54 | FPS() : MachineFunctionPass(ID) { |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame^] | 55 | initializeEdgeBundlesPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | b47bb13 | 2010-07-16 22:00:33 +0000 | [diff] [blame] | 56 | // 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 Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 61 | |
Evan Cheng | bbeeb2a | 2008-09-22 20:58:04 +0000 | [diff] [blame] | 62 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | df09055 | 2009-08-01 00:26:16 +0000 | [diff] [blame] | 63 | AU.setPreservesCFG(); |
Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame^] | 64 | AU.addRequired<EdgeBundles>(); |
Evan Cheng | 8b56a90 | 2008-09-22 22:21:38 +0000 | [diff] [blame] | 65 | AU.addPreservedID(MachineLoopInfoID); |
| 66 | AU.addPreservedID(MachineDominatorsID); |
Evan Cheng | bbeeb2a | 2008-09-22 20:58:04 +0000 | [diff] [blame] | 67 | MachineFunctionPass::getAnalysisUsage(AU); |
| 68 | } |
| 69 | |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 70 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 71 | |
| 72 | virtual const char *getPassName() const { return "X86 FP Stackifier"; } |
| 73 | |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 74 | private: |
Evan Cheng | 32644ac | 2006-12-01 10:11:51 +0000 | [diff] [blame] | 75 | const TargetInstrInfo *TII; // Machine instruction info. |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 76 | |
| 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 Cheng | 32644ac | 2006-12-01 10:11:51 +0000 | [diff] [blame] | 130 | 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 Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 134 | |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 135 | // 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 Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 141 | void dumpStack() const { |
David Greene | f5c95a6 | 2010-01-05 01:29:34 +0000 | [diff] [blame] | 142 | dbgs() << "Stack contents:"; |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 143 | for (unsigned i = 0; i != StackTop; ++i) { |
David Greene | f5c95a6 | 2010-01-05 01:29:34 +0000 | [diff] [blame] | 144 | dbgs() << " FP" << Stack[i]; |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 145 | assert(RegMap[Stack[i]] == i && "Stack[] doesn't match RegMap[]!"); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 146 | } |
David Greene | f5c95a6 | 2010-01-05 01:29:34 +0000 | [diff] [blame] | 147 | dbgs() << "\n"; |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 148 | } |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 149 | |
Chris Lattner | a40ce7e | 2010-07-17 17:40:51 +0000 | [diff] [blame] | 150 | /// getSlot - Return the stack slot number a particular register number is |
| 151 | /// in. |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 152 | unsigned getSlot(unsigned RegNo) const { |
| 153 | assert(RegNo < 8 && "Regno out of range!"); |
| 154 | return RegMap[RegNo]; |
| 155 | } |
| 156 | |
Chris Lattner | a40ce7e | 2010-07-17 17:40:51 +0000 | [diff] [blame] | 157 | /// isLive - Is RegNo currently live in the stack? |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 158 | bool isLive(unsigned RegNo) const { |
| 159 | unsigned Slot = getSlot(RegNo); |
| 160 | return Slot < StackTop && Stack[Slot] == RegNo; |
| 161 | } |
| 162 | |
Chris Lattner | a40ce7e | 2010-07-17 17:40:51 +0000 | [diff] [blame] | 163 | /// getScratchReg - Return an FP register that is not currently in use. |
Jakob Stoklund Olesen | e098e7a | 2010-07-16 17:41:40 +0000 | [diff] [blame] | 164 | 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 Lattner | a40ce7e | 2010-07-17 17:40:51 +0000 | [diff] [blame] | 171 | /// getStackEntry - Return the X86::FP<n> register in register ST(i). |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 172 | unsigned getStackEntry(unsigned STi) const { |
Evan Cheng | 3f490f3 | 2010-10-12 23:19:28 +0000 | [diff] [blame] | 173 | if (STi >= StackTop) |
| 174 | report_fatal_error("Access past stack top!"); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 175 | return Stack[StackTop-1-STi]; |
| 176 | } |
| 177 | |
Chris Lattner | a40ce7e | 2010-07-17 17:40:51 +0000 | [diff] [blame] | 178 | /// getSTReg - Return the X86::ST(i) register which contains the specified |
| 179 | /// FP<RegNo> register. |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 180 | unsigned getSTReg(unsigned RegNo) const { |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 181 | return StackTop - 1 - getSlot(RegNo) + llvm::X86::ST0; |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 184 | // pushReg - Push the specified FP<n> register onto the stack. |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 185 | void pushReg(unsigned Reg) { |
| 186 | assert(Reg < 8 && "Register number out of range!"); |
Evan Cheng | 3f490f3 | 2010-10-12 23:19:28 +0000 | [diff] [blame] | 187 | if (StackTop >= 8) |
| 188 | report_fatal_error("Stack overflow!"); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 189 | Stack[StackTop] = Reg; |
| 190 | RegMap[Reg] = StackTop++; |
| 191 | } |
| 192 | |
| 193 | bool isAtTop(unsigned RegNo) const { return getSlot(RegNo) == StackTop-1; } |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 194 | void moveToTop(unsigned RegNo, MachineBasicBlock::iterator I) { |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 195 | DebugLoc dl = I == MBB->end() ? DebugLoc() : I->getDebugLoc(); |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 196 | if (isAtTop(RegNo)) return; |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 198 | unsigned STReg = getSTReg(RegNo); |
| 199 | unsigned RegOnTop = getStackEntry(0); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 200 | |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 201 | // Swap the slots the regs are in. |
| 202 | std::swap(RegMap[RegNo], RegMap[RegOnTop]); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 204 | // Swap stack slot contents. |
Evan Cheng | 3f490f3 | 2010-10-12 23:19:28 +0000 | [diff] [blame] | 205 | if (RegMap[RegOnTop] >= StackTop) |
| 206 | report_fatal_error("Access past stack top!"); |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 207 | std::swap(Stack[RegMap[RegOnTop]], Stack[StackTop-1]); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 209 | // Emit an fxch to update the runtime processors version of the state. |
Dale Johannesen | 8d13f8f | 2009-02-13 02:33:27 +0000 | [diff] [blame] | 210 | BuildMI(*MBB, I, dl, TII->get(X86::XCH_F)).addReg(STReg); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 211 | ++NumFXCH; |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Chris Lattner | 0526f01 | 2004-04-01 04:06:09 +0000 | [diff] [blame] | 214 | void duplicateToTop(unsigned RegNo, unsigned AsReg, MachineInstr *I) { |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 215 | DebugLoc dl = I == MBB->end() ? DebugLoc() : I->getDebugLoc(); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 216 | unsigned STReg = getSTReg(RegNo); |
| 217 | pushReg(AsReg); // New register on top of stack |
| 218 | |
Dale Johannesen | 8d13f8f | 2009-02-13 02:33:27 +0000 | [diff] [blame] | 219 | BuildMI(*MBB, I, dl, TII->get(X86::LD_Frr)).addReg(STReg); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Chris Lattner | a40ce7e | 2010-07-17 17:40:51 +0000 | [diff] [blame] | 222 | /// popStackAfter - Pop the current value off of the top of the FP stack |
| 223 | /// after the specified instruction. |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 224 | void popStackAfter(MachineBasicBlock::iterator &I); |
| 225 | |
Chris Lattner | a40ce7e | 2010-07-17 17:40:51 +0000 | [diff] [blame] | 226 | /// 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 Lattner | 0526f01 | 2004-04-01 04:06:09 +0000 | [diff] [blame] | 231 | void freeStackSlotAfter(MachineBasicBlock::iterator &I, unsigned Reg); |
| 232 | |
Chris Lattner | a40ce7e | 2010-07-17 17:40:51 +0000 | [diff] [blame] | 233 | /// freeStackSlotBefore - Just the pop, no folding. Return the inserted |
| 234 | /// instruction. |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 235 | MachineBasicBlock::iterator |
| 236 | freeStackSlotBefore(MachineBasicBlock::iterator I, unsigned FPRegNo); |
| 237 | |
Chris Lattner | a40ce7e | 2010-07-17 17:40:51 +0000 | [diff] [blame] | 238 | /// Adjust the live registers to be the set in Mask. |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 239 | void adjustLiveRegs(unsigned Mask, MachineBasicBlock::iterator I); |
| 240 | |
Chris Lattner | a40ce7e | 2010-07-17 17:40:51 +0000 | [diff] [blame] | 241 | /// 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 Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 243 | void shuffleStackTop(const unsigned char *FixStack, unsigned FixCount, |
| 244 | MachineBasicBlock::iterator I); |
| 245 | |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 246 | bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB); |
| 247 | |
| 248 | void handleZeroArgFP(MachineBasicBlock::iterator &I); |
| 249 | void handleOneArgFP(MachineBasicBlock::iterator &I); |
Chris Lattner | 4a06f35 | 2004-02-02 19:23:15 +0000 | [diff] [blame] | 250 | void handleOneArgFPRW(MachineBasicBlock::iterator &I); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 251 | void handleTwoArgFP(MachineBasicBlock::iterator &I); |
Chris Lattner | d62d5d7 | 2004-06-11 04:25:06 +0000 | [diff] [blame] | 252 | void handleCompareFP(MachineBasicBlock::iterator &I); |
Chris Lattner | c1bab32 | 2004-03-31 22:02:36 +0000 | [diff] [blame] | 253 | void handleCondMovFP(MachineBasicBlock::iterator &I); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 254 | void handleSpecialFP(MachineBasicBlock::iterator &I); |
Jakob Stoklund Olesen | 7db1e7a | 2010-07-08 19:46:30 +0000 | [diff] [blame] | 255 | |
| 256 | bool translateCopy(MachineInstr*); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 257 | }; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 258 | char FPS::ID = 0; |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Chris Lattner | f2e49d4 | 2003-12-20 09:58:55 +0000 | [diff] [blame] | 261 | FunctionPass *llvm::createX86FloatingPointStackifierPass() { return new FPS(); } |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 3cc8384 | 2008-01-14 06:41:29 +0000 | [diff] [blame] | 263 | /// getFPReg - Return the X86::FPx register number for the specified operand. |
| 264 | /// For example, this returns 3 for X86::FP3. |
| 265 | static unsigned getFPReg(const MachineOperand &MO) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 266 | assert(MO.isReg() && "Expected an FP register!"); |
Chris Lattner | 3cc8384 | 2008-01-14 06:41:29 +0000 | [diff] [blame] | 267 | unsigned Reg = MO.getReg(); |
| 268 | assert(Reg >= X86::FP0 && Reg <= X86::FP6 && "Expected FP register!"); |
| 269 | return Reg - X86::FP0; |
| 270 | } |
| 271 | |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 272 | /// runOnMachineFunction - Loop over all of the basic blocks, transforming FP |
| 273 | /// register references into FP stack references. |
| 274 | /// |
| 275 | bool FPS::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | 42e25b3 | 2005-01-23 23:13:59 +0000 | [diff] [blame] | 276 | // 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 Lattner | 42e25b3 | 2005-01-23 23:13:59 +0000 | [diff] [blame] | 278 | 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 Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 282 | if (MF.getRegInfo().isPhysRegUsed(X86::FP0+i)) { |
Chris Lattner | 42e25b3 | 2005-01-23 23:13:59 +0000 | [diff] [blame] | 283 | FPIsUsed = true; |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | // Early exit. |
| 288 | if (!FPIsUsed) return false; |
| 289 | |
Evan Cheng | 32644ac | 2006-12-01 10:11:51 +0000 | [diff] [blame] | 290 | TII = MF.getTarget().getInstrInfo(); |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 291 | |
| 292 | // Prepare cross-MBB liveness. |
| 293 | bundleCFG(MF); |
| 294 | |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 295 | StackTop = 0; |
| 296 | |
Chris Lattner | 847df25 | 2004-01-30 22:25:18 +0000 | [diff] [blame] | 297 | // 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 Anderson | eaa009d | 2008-08-14 21:01:00 +0000 | [diff] [blame] | 299 | SmallPtrSet<MachineBasicBlock*, 8> Processed; |
Chris Lattner | 2268684 | 2004-05-01 21:27:53 +0000 | [diff] [blame] | 300 | MachineBasicBlock *Entry = MF.begin(); |
Chris Lattner | 847df25 | 2004-01-30 22:25:18 +0000 | [diff] [blame] | 301 | |
| 302 | bool Changed = false; |
Owen Anderson | eaa009d | 2008-08-14 21:01:00 +0000 | [diff] [blame] | 303 | for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 8> > |
Chris Lattner | 847df25 | 2004-01-30 22:25:18 +0000 | [diff] [blame] | 304 | I = df_ext_begin(Entry, Processed), E = df_ext_end(Entry, Processed); |
| 305 | I != E; ++I) |
Chris Lattner | 2268684 | 2004-05-01 21:27:53 +0000 | [diff] [blame] | 306 | Changed |= processBasicBlock(MF, **I); |
Chris Lattner | 847df25 | 2004-01-30 22:25:18 +0000 | [diff] [blame] | 307 | |
Chris Lattner | ba3598c | 2009-09-08 04:55:44 +0000 | [diff] [blame] | 308 | // Process any unreachable blocks in arbitrary order now. |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 309 | 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 Lattner | ba3598c | 2009-09-08 04:55:44 +0000 | [diff] [blame] | 313 | |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 314 | BlockBundle.clear(); |
| 315 | LiveBundles.clear(); |
| 316 | |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 317 | return Changed; |
| 318 | } |
| 319 | |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 320 | /// 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. |
| 325 | void 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 Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 414 | /// processBasicBlock - Loop over all of the instructions in the basic block, |
| 415 | /// transforming FP instructions into their stack form. |
| 416 | /// |
| 417 | bool FPS::processBasicBlock(MachineFunction &MF, MachineBasicBlock &BB) { |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 418 | bool Changed = false; |
| 419 | MBB = &BB; |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 420 | |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 421 | setupBlockStack(); |
| 422 | |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 423 | for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I) { |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 424 | MachineInstr *MI = I; |
Bruno Cardoso Lopes | 99405df | 2010-06-08 22:51:23 +0000 | [diff] [blame] | 425 | uint64_t Flags = MI->getDesc().TSFlags; |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 426 | |
Chris Lattner | e12ecf2 | 2008-03-11 19:50:13 +0000 | [diff] [blame] | 427 | unsigned FPInstClass = Flags & X86II::FPTypeMask; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 428 | if (MI->isInlineAsm()) |
Chris Lattner | e12ecf2 | 2008-03-11 19:50:13 +0000 | [diff] [blame] | 429 | FPInstClass = X86II::SpecialFP; |
Jakob Stoklund Olesen | 7db1e7a | 2010-07-08 19:46:30 +0000 | [diff] [blame] | 430 | |
| 431 | if (MI->isCopy() && translateCopy(MI)) |
| 432 | FPInstClass = X86II::SpecialFP; |
| 433 | |
Chris Lattner | e12ecf2 | 2008-03-11 19:50:13 +0000 | [diff] [blame] | 434 | if (FPInstClass == X86II::NotFP) |
Chris Lattner | 847df25 | 2004-01-30 22:25:18 +0000 | [diff] [blame] | 435 | continue; // Efficiently ignore non-fp insts! |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 436 | |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 437 | MachineInstr *PrevMI = 0; |
Alkis Evlogimenos | f81af21 | 2004-02-14 01:18:34 +0000 | [diff] [blame] | 438 | if (I != BB.begin()) |
Chris Lattner | 6fa2f9c | 2008-03-09 07:05:32 +0000 | [diff] [blame] | 439 | PrevMI = prior(I); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 440 | |
| 441 | ++NumFP; // Keep track of # of pseudo instrs |
David Greene | f5c95a6 | 2010-01-05 01:29:34 +0000 | [diff] [blame] | 442 | DEBUG(dbgs() << "\nFPInst:\t" << *MI); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 443 | |
| 444 | // Get dead variables list now because the MI pointer may be deleted as part |
| 445 | // of processing! |
Evan Cheng | ddd2a45 | 2006-11-15 20:56:39 +0000 | [diff] [blame] | 446 | SmallVector<unsigned, 8> DeadRegs; |
| 447 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 448 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 449 | if (MO.isReg() && MO.isDead()) |
Evan Cheng | ddd2a45 | 2006-11-15 20:56:39 +0000 | [diff] [blame] | 450 | DeadRegs.push_back(MO.getReg()); |
| 451 | } |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 452 | |
Chris Lattner | e12ecf2 | 2008-03-11 19:50:13 +0000 | [diff] [blame] | 453 | switch (FPInstClass) { |
Chris Lattner | 4a06f35 | 2004-02-02 19:23:15 +0000 | [diff] [blame] | 454 | case X86II::ZeroArgFP: handleZeroArgFP(I); break; |
Chris Lattner | c1bab32 | 2004-03-31 22:02:36 +0000 | [diff] [blame] | 455 | case X86II::OneArgFP: handleOneArgFP(I); break; // fstp ST(0) |
Chris Lattner | 4a06f35 | 2004-02-02 19:23:15 +0000 | [diff] [blame] | 456 | case X86II::OneArgFPRW: handleOneArgFPRW(I); break; // ST(0) = fsqrt(ST(0)) |
Evan Cheng | 5cd3e9f | 2006-11-11 10:21:44 +0000 | [diff] [blame] | 457 | case X86II::TwoArgFP: handleTwoArgFP(I); break; |
Chris Lattner | ab8decc | 2004-06-11 04:41:24 +0000 | [diff] [blame] | 458 | case X86II::CompareFP: handleCompareFP(I); break; |
Chris Lattner | c1bab32 | 2004-03-31 22:02:36 +0000 | [diff] [blame] | 459 | case X86II::CondMovFP: handleCondMovFP(I); break; |
Chris Lattner | 4a06f35 | 2004-02-02 19:23:15 +0000 | [diff] [blame] | 460 | case X86II::SpecialFP: handleSpecialFP(I); break; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 461 | default: llvm_unreachable("Unknown FP Type!"); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | // Check to see if any of the values defined by this instruction are dead |
| 465 | // after definition. If so, pop them. |
Evan Cheng | ddd2a45 | 2006-11-15 20:56:39 +0000 | [diff] [blame] | 466 | for (unsigned i = 0, e = DeadRegs.size(); i != e; ++i) { |
| 467 | unsigned Reg = DeadRegs[i]; |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 468 | if (Reg >= X86::FP0 && Reg <= X86::FP6) { |
David Greene | f5c95a6 | 2010-01-05 01:29:34 +0000 | [diff] [blame] | 469 | DEBUG(dbgs() << "Register FP#" << Reg-X86::FP0 << " is dead!\n"); |
Chris Lattner | d62d5d7 | 2004-06-11 04:25:06 +0000 | [diff] [blame] | 470 | freeStackSlotAfter(I, Reg-X86::FP0); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 473 | |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 474 | // Print out all of the instructions expanded to if -debug |
Alkis Evlogimenos | b929bca | 2004-02-15 00:46:41 +0000 | [diff] [blame] | 475 | DEBUG( |
| 476 | MachineBasicBlock::iterator PrevI(PrevMI); |
| 477 | if (I == PrevI) { |
David Greene | f5c95a6 | 2010-01-05 01:29:34 +0000 | [diff] [blame] | 478 | dbgs() << "Just deleted pseudo instruction\n"; |
Alkis Evlogimenos | b929bca | 2004-02-15 00:46:41 +0000 | [diff] [blame] | 479 | } else { |
| 480 | MachineBasicBlock::iterator Start = I; |
| 481 | // Rewind to first instruction newly inserted. |
| 482 | while (Start != BB.begin() && prior(Start) != PrevI) --Start; |
David Greene | f5c95a6 | 2010-01-05 01:29:34 +0000 | [diff] [blame] | 483 | dbgs() << "Inserted instructions:\n\t"; |
| 484 | Start->print(dbgs(), &MF.getTarget()); |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 485 | while (++Start != llvm::next(I)) {} |
Alkis Evlogimenos | b929bca | 2004-02-15 00:46:41 +0000 | [diff] [blame] | 486 | } |
| 487 | dumpStack(); |
| 488 | ); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 489 | |
| 490 | Changed = true; |
| 491 | } |
| 492 | |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 493 | finishBlockStack(); |
| 494 | |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 495 | return Changed; |
| 496 | } |
| 497 | |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 498 | /// setupBlockStack - Use the BlockBundle map to set up our model of the stack |
| 499 | /// to match predecessors' live out stack. |
| 500 | void 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. |
| 533 | void 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 Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 569 | //===----------------------------------------------------------------------===// |
| 570 | // Efficient Lookup Table Support |
| 571 | //===----------------------------------------------------------------------===// |
| 572 | |
Chris Lattner | f2e49d4 | 2003-12-20 09:58:55 +0000 | [diff] [blame] | 573 | namespace { |
| 574 | struct TableEntry { |
| 575 | unsigned from; |
| 576 | unsigned to; |
| 577 | bool operator<(const TableEntry &TE) const { return from < TE.from; } |
Jeff Cohen | 9471c8a | 2006-01-26 20:41:32 +0000 | [diff] [blame] | 578 | friend bool operator<(const TableEntry &TE, unsigned V) { |
| 579 | return TE.from < V; |
| 580 | } |
Chandler Carruth | 100c267 | 2010-10-23 08:10:43 +0000 | [diff] [blame] | 581 | friend bool LLVM_ATTRIBUTE_USED operator<(unsigned V, |
| 582 | const TableEntry &TE) { |
Jakob Stoklund Olesen | de78f05 | 2010-08-16 18:24:54 +0000 | [diff] [blame] | 583 | return V < TE.from; |
| 584 | } |
Chris Lattner | f2e49d4 | 2003-12-20 09:58:55 +0000 | [diff] [blame] | 585 | }; |
| 586 | } |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 587 | |
Evan Cheng | a022bdf | 2008-07-21 20:02:45 +0000 | [diff] [blame] | 588 | #ifndef NDEBUG |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 589 | static 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 Cheng | a022bdf | 2008-07-21 20:02:45 +0000 | [diff] [blame] | 594 | #endif |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 595 | |
| 596 | static 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 Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 603 | #ifdef NDEBUG |
| 604 | #define ASSERT_SORTED(TABLE) |
| 605 | #else |
| 606 | #define ASSERT_SORTED(TABLE) \ |
| 607 | { static bool TABLE##Checked = false; \ |
Jim Laskey | c06fe8a | 2006-07-19 19:33:08 +0000 | [diff] [blame] | 608 | if (!TABLE##Checked) { \ |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 609 | assert(TableIsSorted(TABLE, array_lengthof(TABLE)) && \ |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 610 | "All lookup tables must be sorted for efficient access!"); \ |
Jim Laskey | c06fe8a | 2006-07-19 19:33:08 +0000 | [diff] [blame] | 611 | TABLE##Checked = true; \ |
| 612 | } \ |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 613 | } |
| 614 | #endif |
| 615 | |
Chris Lattner | 58fe459 | 2005-12-21 07:47:04 +0000 | [diff] [blame] | 616 | //===----------------------------------------------------------------------===// |
| 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 | // |
| 624 | static const TableEntry OpcodeTable[] = { |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 625 | { X86::ABS_Fp32 , X86::ABS_F }, |
| 626 | { X86::ABS_Fp64 , X86::ABS_F }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 627 | { X86::ABS_Fp80 , X86::ABS_F }, |
Dale Johannesen | afdc7fd | 2007-07-10 21:53:30 +0000 | [diff] [blame] | 628 | { X86::ADD_Fp32m , X86::ADD_F32m }, |
| 629 | { X86::ADD_Fp64m , X86::ADD_F64m }, |
| 630 | { X86::ADD_Fp64m32 , X86::ADD_F32m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 631 | { X86::ADD_Fp80m32 , X86::ADD_F32m }, |
| 632 | { X86::ADD_Fp80m64 , X86::ADD_F64m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 633 | { X86::ADD_FpI16m32 , X86::ADD_FI16m }, |
| 634 | { X86::ADD_FpI16m64 , X86::ADD_FI16m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 635 | { X86::ADD_FpI16m80 , X86::ADD_FI16m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 636 | { X86::ADD_FpI32m32 , X86::ADD_FI32m }, |
| 637 | { X86::ADD_FpI32m64 , X86::ADD_FI32m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 638 | { X86::ADD_FpI32m80 , X86::ADD_FI32m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 639 | { X86::CHS_Fp32 , X86::CHS_F }, |
| 640 | { X86::CHS_Fp64 , X86::CHS_F }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 641 | { X86::CHS_Fp80 , X86::CHS_F }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 642 | { X86::CMOVBE_Fp32 , X86::CMOVBE_F }, |
| 643 | { X86::CMOVBE_Fp64 , X86::CMOVBE_F }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 644 | { X86::CMOVBE_Fp80 , X86::CMOVBE_F }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 645 | { X86::CMOVB_Fp32 , X86::CMOVB_F }, |
| 646 | { X86::CMOVB_Fp64 , X86::CMOVB_F }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 647 | { X86::CMOVB_Fp80 , X86::CMOVB_F }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 648 | { X86::CMOVE_Fp32 , X86::CMOVE_F }, |
| 649 | { X86::CMOVE_Fp64 , X86::CMOVE_F }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 650 | { X86::CMOVE_Fp80 , X86::CMOVE_F }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 651 | { X86::CMOVNBE_Fp32 , X86::CMOVNBE_F }, |
| 652 | { X86::CMOVNBE_Fp64 , X86::CMOVNBE_F }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 653 | { X86::CMOVNBE_Fp80 , X86::CMOVNBE_F }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 654 | { X86::CMOVNB_Fp32 , X86::CMOVNB_F }, |
| 655 | { X86::CMOVNB_Fp64 , X86::CMOVNB_F }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 656 | { X86::CMOVNB_Fp80 , X86::CMOVNB_F }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 657 | { X86::CMOVNE_Fp32 , X86::CMOVNE_F }, |
| 658 | { X86::CMOVNE_Fp64 , X86::CMOVNE_F }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 659 | { X86::CMOVNE_Fp80 , X86::CMOVNE_F }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 660 | { X86::CMOVNP_Fp32 , X86::CMOVNP_F }, |
| 661 | { X86::CMOVNP_Fp64 , X86::CMOVNP_F }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 662 | { X86::CMOVNP_Fp80 , X86::CMOVNP_F }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 663 | { X86::CMOVP_Fp32 , X86::CMOVP_F }, |
| 664 | { X86::CMOVP_Fp64 , X86::CMOVP_F }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 665 | { X86::CMOVP_Fp80 , X86::CMOVP_F }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 666 | { X86::COS_Fp32 , X86::COS_F }, |
| 667 | { X86::COS_Fp64 , X86::COS_F }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 668 | { X86::COS_Fp80 , X86::COS_F }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 669 | { X86::DIVR_Fp32m , X86::DIVR_F32m }, |
| 670 | { X86::DIVR_Fp64m , X86::DIVR_F64m }, |
Dale Johannesen | afdc7fd | 2007-07-10 21:53:30 +0000 | [diff] [blame] | 671 | { X86::DIVR_Fp64m32 , X86::DIVR_F32m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 672 | { X86::DIVR_Fp80m32 , X86::DIVR_F32m }, |
| 673 | { X86::DIVR_Fp80m64 , X86::DIVR_F64m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 674 | { X86::DIVR_FpI16m32, X86::DIVR_FI16m}, |
| 675 | { X86::DIVR_FpI16m64, X86::DIVR_FI16m}, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 676 | { X86::DIVR_FpI16m80, X86::DIVR_FI16m}, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 677 | { X86::DIVR_FpI32m32, X86::DIVR_FI32m}, |
| 678 | { X86::DIVR_FpI32m64, X86::DIVR_FI32m}, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 679 | { X86::DIVR_FpI32m80, X86::DIVR_FI32m}, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 680 | { X86::DIV_Fp32m , X86::DIV_F32m }, |
| 681 | { X86::DIV_Fp64m , X86::DIV_F64m }, |
Dale Johannesen | afdc7fd | 2007-07-10 21:53:30 +0000 | [diff] [blame] | 682 | { X86::DIV_Fp64m32 , X86::DIV_F32m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 683 | { X86::DIV_Fp80m32 , X86::DIV_F32m }, |
| 684 | { X86::DIV_Fp80m64 , X86::DIV_F64m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 685 | { X86::DIV_FpI16m32 , X86::DIV_FI16m }, |
| 686 | { X86::DIV_FpI16m64 , X86::DIV_FI16m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 687 | { X86::DIV_FpI16m80 , X86::DIV_FI16m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 688 | { X86::DIV_FpI32m32 , X86::DIV_FI32m }, |
| 689 | { X86::DIV_FpI32m64 , X86::DIV_FI32m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 690 | { X86::DIV_FpI32m80 , X86::DIV_FI32m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 691 | { X86::ILD_Fp16m32 , X86::ILD_F16m }, |
| 692 | { X86::ILD_Fp16m64 , X86::ILD_F16m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 693 | { X86::ILD_Fp16m80 , X86::ILD_F16m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 694 | { X86::ILD_Fp32m32 , X86::ILD_F32m }, |
| 695 | { X86::ILD_Fp32m64 , X86::ILD_F32m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 696 | { X86::ILD_Fp32m80 , X86::ILD_F32m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 697 | { X86::ILD_Fp64m32 , X86::ILD_F64m }, |
| 698 | { X86::ILD_Fp64m64 , X86::ILD_F64m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 699 | { X86::ILD_Fp64m80 , X86::ILD_F64m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 700 | { X86::ISTT_Fp16m32 , X86::ISTT_FP16m}, |
| 701 | { X86::ISTT_Fp16m64 , X86::ISTT_FP16m}, |
Dale Johannesen | a996d52 | 2007-08-07 01:17:37 +0000 | [diff] [blame] | 702 | { X86::ISTT_Fp16m80 , X86::ISTT_FP16m}, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 703 | { X86::ISTT_Fp32m32 , X86::ISTT_FP32m}, |
| 704 | { X86::ISTT_Fp32m64 , X86::ISTT_FP32m}, |
Dale Johannesen | a996d52 | 2007-08-07 01:17:37 +0000 | [diff] [blame] | 705 | { X86::ISTT_Fp32m80 , X86::ISTT_FP32m}, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 706 | { X86::ISTT_Fp64m32 , X86::ISTT_FP64m}, |
| 707 | { X86::ISTT_Fp64m64 , X86::ISTT_FP64m}, |
Dale Johannesen | a996d52 | 2007-08-07 01:17:37 +0000 | [diff] [blame] | 708 | { X86::ISTT_Fp64m80 , X86::ISTT_FP64m}, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 709 | { X86::IST_Fp16m32 , X86::IST_F16m }, |
| 710 | { X86::IST_Fp16m64 , X86::IST_F16m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 711 | { X86::IST_Fp16m80 , X86::IST_F16m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 712 | { X86::IST_Fp32m32 , X86::IST_F32m }, |
| 713 | { X86::IST_Fp32m64 , X86::IST_F32m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 714 | { X86::IST_Fp32m80 , X86::IST_F32m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 715 | { X86::IST_Fp64m32 , X86::IST_FP64m }, |
| 716 | { X86::IST_Fp64m64 , X86::IST_FP64m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 717 | { X86::IST_Fp64m80 , X86::IST_FP64m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 718 | { X86::LD_Fp032 , X86::LD_F0 }, |
| 719 | { X86::LD_Fp064 , X86::LD_F0 }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 720 | { X86::LD_Fp080 , X86::LD_F0 }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 721 | { X86::LD_Fp132 , X86::LD_F1 }, |
| 722 | { X86::LD_Fp164 , X86::LD_F1 }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 723 | { X86::LD_Fp180 , X86::LD_F1 }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 724 | { X86::LD_Fp32m , X86::LD_F32m }, |
Dale Johannesen | cdbe4d3 | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 725 | { X86::LD_Fp32m64 , X86::LD_F32m }, |
| 726 | { X86::LD_Fp32m80 , X86::LD_F32m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 727 | { X86::LD_Fp64m , X86::LD_F64m }, |
Dale Johannesen | cdbe4d3 | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 728 | { X86::LD_Fp64m80 , X86::LD_F64m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 729 | { X86::LD_Fp80m , X86::LD_F80m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 730 | { X86::MUL_Fp32m , X86::MUL_F32m }, |
| 731 | { X86::MUL_Fp64m , X86::MUL_F64m }, |
Dale Johannesen | afdc7fd | 2007-07-10 21:53:30 +0000 | [diff] [blame] | 732 | { X86::MUL_Fp64m32 , X86::MUL_F32m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 733 | { X86::MUL_Fp80m32 , X86::MUL_F32m }, |
| 734 | { X86::MUL_Fp80m64 , X86::MUL_F64m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 735 | { X86::MUL_FpI16m32 , X86::MUL_FI16m }, |
| 736 | { X86::MUL_FpI16m64 , X86::MUL_FI16m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 737 | { X86::MUL_FpI16m80 , X86::MUL_FI16m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 738 | { X86::MUL_FpI32m32 , X86::MUL_FI32m }, |
| 739 | { X86::MUL_FpI32m64 , X86::MUL_FI32m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 740 | { X86::MUL_FpI32m80 , X86::MUL_FI32m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 741 | { X86::SIN_Fp32 , X86::SIN_F }, |
| 742 | { X86::SIN_Fp64 , X86::SIN_F }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 743 | { X86::SIN_Fp80 , X86::SIN_F }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 744 | { X86::SQRT_Fp32 , X86::SQRT_F }, |
| 745 | { X86::SQRT_Fp64 , X86::SQRT_F }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 746 | { X86::SQRT_Fp80 , X86::SQRT_F }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 747 | { X86::ST_Fp32m , X86::ST_F32m }, |
| 748 | { X86::ST_Fp64m , X86::ST_F64m }, |
| 749 | { X86::ST_Fp64m32 , X86::ST_F32m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 750 | { X86::ST_Fp80m32 , X86::ST_F32m }, |
| 751 | { X86::ST_Fp80m64 , X86::ST_F64m }, |
| 752 | { X86::ST_FpP80m , X86::ST_FP80m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 753 | { X86::SUBR_Fp32m , X86::SUBR_F32m }, |
| 754 | { X86::SUBR_Fp64m , X86::SUBR_F64m }, |
Dale Johannesen | afdc7fd | 2007-07-10 21:53:30 +0000 | [diff] [blame] | 755 | { X86::SUBR_Fp64m32 , X86::SUBR_F32m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 756 | { X86::SUBR_Fp80m32 , X86::SUBR_F32m }, |
| 757 | { X86::SUBR_Fp80m64 , X86::SUBR_F64m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 758 | { X86::SUBR_FpI16m32, X86::SUBR_FI16m}, |
| 759 | { X86::SUBR_FpI16m64, X86::SUBR_FI16m}, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 760 | { X86::SUBR_FpI16m80, X86::SUBR_FI16m}, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 761 | { X86::SUBR_FpI32m32, X86::SUBR_FI32m}, |
| 762 | { X86::SUBR_FpI32m64, X86::SUBR_FI32m}, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 763 | { X86::SUBR_FpI32m80, X86::SUBR_FI32m}, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 764 | { X86::SUB_Fp32m , X86::SUB_F32m }, |
| 765 | { X86::SUB_Fp64m , X86::SUB_F64m }, |
Dale Johannesen | afdc7fd | 2007-07-10 21:53:30 +0000 | [diff] [blame] | 766 | { X86::SUB_Fp64m32 , X86::SUB_F32m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 767 | { X86::SUB_Fp80m32 , X86::SUB_F32m }, |
| 768 | { X86::SUB_Fp80m64 , X86::SUB_F64m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 769 | { X86::SUB_FpI16m32 , X86::SUB_FI16m }, |
| 770 | { X86::SUB_FpI16m64 , X86::SUB_FI16m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 771 | { X86::SUB_FpI16m80 , X86::SUB_FI16m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 772 | { X86::SUB_FpI32m32 , X86::SUB_FI32m }, |
| 773 | { X86::SUB_FpI32m64 , X86::SUB_FI32m }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 774 | { X86::SUB_FpI32m80 , X86::SUB_FI32m }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 775 | { X86::TST_Fp32 , X86::TST_F }, |
| 776 | { X86::TST_Fp64 , X86::TST_F }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 777 | { X86::TST_Fp80 , X86::TST_F }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 778 | { X86::UCOM_FpIr32 , X86::UCOM_FIr }, |
| 779 | { X86::UCOM_FpIr64 , X86::UCOM_FIr }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 780 | { X86::UCOM_FpIr80 , X86::UCOM_FIr }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 781 | { X86::UCOM_Fpr32 , X86::UCOM_Fr }, |
| 782 | { X86::UCOM_Fpr64 , X86::UCOM_Fr }, |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 783 | { X86::UCOM_Fpr80 , X86::UCOM_Fr }, |
Chris Lattner | 58fe459 | 2005-12-21 07:47:04 +0000 | [diff] [blame] | 784 | }; |
| 785 | |
| 786 | static unsigned getConcreteOpcode(unsigned Opcode) { |
| 787 | ASSERT_SORTED(OpcodeTable); |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 788 | int Opc = Lookup(OpcodeTable, array_lengthof(OpcodeTable), Opcode); |
Chris Lattner | 58fe459 | 2005-12-21 07:47:04 +0000 | [diff] [blame] | 789 | assert(Opc != -1 && "FP Stack instruction not in OpcodeTable!"); |
| 790 | return Opc; |
| 791 | } |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 792 | |
| 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 | // |
| 800 | static const TableEntry PopTable[] = { |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 801 | { X86::ADD_FrST0 , X86::ADD_FPrST0 }, |
Chris Lattner | 113455b | 2003-08-03 21:56:36 +0000 | [diff] [blame] | 802 | |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 803 | { X86::DIVR_FrST0, X86::DIVR_FPrST0 }, |
| 804 | { X86::DIV_FrST0 , X86::DIV_FPrST0 }, |
Chris Lattner | 113455b | 2003-08-03 21:56:36 +0000 | [diff] [blame] | 805 | |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 806 | { X86::IST_F16m , X86::IST_FP16m }, |
| 807 | { X86::IST_F32m , X86::IST_FP32m }, |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 808 | |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 809 | { X86::MUL_FrST0 , X86::MUL_FPrST0 }, |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 810 | |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 811 | { X86::ST_F32m , X86::ST_FP32m }, |
| 812 | { X86::ST_F64m , X86::ST_FP64m }, |
| 813 | { X86::ST_Frr , X86::ST_FPrr }, |
Chris Lattner | 113455b | 2003-08-03 21:56:36 +0000 | [diff] [blame] | 814 | |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 815 | { X86::SUBR_FrST0, X86::SUBR_FPrST0 }, |
| 816 | { X86::SUB_FrST0 , X86::SUB_FPrST0 }, |
Chris Lattner | 113455b | 2003-08-03 21:56:36 +0000 | [diff] [blame] | 817 | |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 818 | { X86::UCOM_FIr , X86::UCOM_FIPr }, |
Chris Lattner | c040bca | 2004-04-12 01:39:15 +0000 | [diff] [blame] | 819 | |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 820 | { X86::UCOM_FPr , X86::UCOM_FPPr }, |
| 821 | { X86::UCOM_Fr , X86::UCOM_FPr }, |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 822 | }; |
| 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 | /// |
| 830 | void FPS::popStackAfter(MachineBasicBlock::iterator &I) { |
Dale Johannesen | 8d13f8f | 2009-02-13 02:33:27 +0000 | [diff] [blame] | 831 | MachineInstr* MI = I; |
| 832 | DebugLoc dl = MI->getDebugLoc(); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 833 | ASSERT_SORTED(PopTable); |
Evan Cheng | 3f490f3 | 2010-10-12 23:19:28 +0000 | [diff] [blame] | 834 | if (StackTop == 0) |
| 835 | report_fatal_error("Cannot pop empty stack!"); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 836 | RegMap[Stack[--StackTop]] = ~0; // Update state |
| 837 | |
| 838 | // Check to see if there is a popping version of this instruction... |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 839 | int Opcode = Lookup(PopTable, array_lengthof(PopTable), I->getOpcode()); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 840 | if (Opcode != -1) { |
Chris Lattner | 5080f4d | 2008-01-11 18:10:50 +0000 | [diff] [blame] | 841 | I->setDesc(TII->get(Opcode)); |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 842 | if (Opcode == X86::UCOM_FPPr) |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 843 | I->RemoveOperand(0); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 844 | } else { // Insert an explicit pop |
Dale Johannesen | 8d13f8f | 2009-02-13 02:33:27 +0000 | [diff] [blame] | 845 | I = BuildMI(*MBB, ++I, dl, TII->get(X86::ST_FPrr)).addReg(X86::ST0); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
Chris Lattner | 0526f01 | 2004-04-01 04:06:09 +0000 | [diff] [blame] | 849 | /// 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. |
| 853 | void 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 Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 862 | I = freeStackSlotBefore(++I, FPRegNo); |
| 863 | } |
| 864 | |
| 865 | /// freeStackSlotBefore - Free the specified register without trying any |
| 866 | /// folding. |
| 867 | MachineBasicBlock::iterator |
| 868 | FPS::freeStackSlotBefore(MachineBasicBlock::iterator I, unsigned FPRegNo) { |
Chris Lattner | 0526f01 | 2004-04-01 04:06:09 +0000 | [diff] [blame] | 869 | 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 Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 876 | 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. |
| 881 | void 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? |
| 944 | void 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 Lattner | 0526f01 | 2004-04-01 04:06:09 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 963 | //===----------------------------------------------------------------------===// |
| 964 | // Instruction transformation implementation |
| 965 | //===----------------------------------------------------------------------===// |
| 966 | |
| 967 | /// handleZeroArgFP - ST(0) = fld0 ST(0) = flds <mem> |
Chris Lattner | 4a06f35 | 2004-02-02 19:23:15 +0000 | [diff] [blame] | 968 | /// |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 969 | void FPS::handleZeroArgFP(MachineBasicBlock::iterator &I) { |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 970 | MachineInstr *MI = I; |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 971 | unsigned DestReg = getFPReg(MI->getOperand(0)); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 972 | |
Chris Lattner | 58fe459 | 2005-12-21 07:47:04 +0000 | [diff] [blame] | 973 | // Change from the pseudo instruction to the concrete instruction. |
| 974 | MI->RemoveOperand(0); // Remove the explicit ST(0) operand |
Chris Lattner | 5080f4d | 2008-01-11 18:10:50 +0000 | [diff] [blame] | 975 | MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode()))); |
Chris Lattner | 58fe459 | 2005-12-21 07:47:04 +0000 | [diff] [blame] | 976 | |
| 977 | // Result gets pushed on the stack. |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 978 | pushReg(DestReg); |
| 979 | } |
| 980 | |
Chris Lattner | 4a06f35 | 2004-02-02 19:23:15 +0000 | [diff] [blame] | 981 | /// handleOneArgFP - fst <mem>, ST(0) |
| 982 | /// |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 983 | void FPS::handleOneArgFP(MachineBasicBlock::iterator &I) { |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 984 | MachineInstr *MI = I; |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 985 | unsigned NumOps = MI->getDesc().getNumOperands(); |
Chris Lattner | ac0ed5d | 2010-07-08 22:41:28 +0000 | [diff] [blame] | 986 | assert((NumOps == X86::AddrNumOperands + 1 || NumOps == 1) && |
Chris Lattner | b97046a | 2004-02-03 07:27:34 +0000 | [diff] [blame] | 987 | "Can only handle fst* & ftst instructions!"); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 988 | |
Chris Lattner | 4a06f35 | 2004-02-02 19:23:15 +0000 | [diff] [blame] | 989 | // Is this the last use of the source register? |
Evan Cheng | 171d09e | 2006-11-10 01:28:43 +0000 | [diff] [blame] | 990 | unsigned Reg = getFPReg(MI->getOperand(NumOps-1)); |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 991 | bool KillsSrc = MI->killsRegister(X86::FP0+Reg); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 992 | |
Evan Cheng | 2b15271 | 2006-02-18 02:36:28 +0000 | [diff] [blame] | 993 | // FISTP64m is strange because there isn't a non-popping versions. |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 994 | // 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 Johannesen | ca8035e | 2007-09-17 20:15:38 +0000 | [diff] [blame] | 997 | // Ditto FISTTP16m, FISTTP32m, FISTTP64m, ST_FpP80m. |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 998 | // |
Evan Cheng | 2b15271 | 2006-02-18 02:36:28 +0000 | [diff] [blame] | 999 | if (!KillsSrc && |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1000 | (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 Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 1007 | MI->getOpcode() == X86::ISTT_Fp64m64 || |
Dale Johannesen | 41de436 | 2007-09-20 01:27:54 +0000 | [diff] [blame] | 1008 | MI->getOpcode() == X86::IST_Fp64m80 || |
Dale Johannesen | a996d52 | 2007-08-07 01:17:37 +0000 | [diff] [blame] | 1009 | MI->getOpcode() == X86::ISTT_Fp16m80 || |
| 1010 | MI->getOpcode() == X86::ISTT_Fp32m80 || |
| 1011 | MI->getOpcode() == X86::ISTT_Fp64m80 || |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 1012 | MI->getOpcode() == X86::ST_FpP80m)) { |
Jakob Stoklund Olesen | e098e7a | 2010-07-16 17:41:40 +0000 | [diff] [blame] | 1013 | duplicateToTop(Reg, getScratchReg(), I); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1014 | } else { |
| 1015 | moveToTop(Reg, I); // Move to the top of the stack... |
| 1016 | } |
Chris Lattner | 58fe459 | 2005-12-21 07:47:04 +0000 | [diff] [blame] | 1017 | |
| 1018 | // Convert from the pseudo instruction to the concrete instruction. |
Evan Cheng | 171d09e | 2006-11-10 01:28:43 +0000 | [diff] [blame] | 1019 | MI->RemoveOperand(NumOps-1); // Remove explicit ST(0) operand |
Chris Lattner | 5080f4d | 2008-01-11 18:10:50 +0000 | [diff] [blame] | 1020 | MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode()))); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1021 | |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1022 | if (MI->getOpcode() == X86::IST_FP64m || |
| 1023 | MI->getOpcode() == X86::ISTT_FP16m || |
| 1024 | MI->getOpcode() == X86::ISTT_FP32m || |
Dale Johannesen | 8883573 | 2007-08-06 19:50:32 +0000 | [diff] [blame] | 1025 | MI->getOpcode() == X86::ISTT_FP64m || |
| 1026 | MI->getOpcode() == X86::ST_FP80m) { |
Evan Cheng | 3f490f3 | 2010-10-12 23:19:28 +0000 | [diff] [blame] | 1027 | if (StackTop == 0) |
| 1028 | report_fatal_error("Stack empty??"); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1029 | --StackTop; |
| 1030 | } else if (KillsSrc) { // Last use of operand? |
| 1031 | popStackAfter(I); |
| 1032 | } |
| 1033 | } |
| 1034 | |
Chris Lattner | 4a06f35 | 2004-02-02 19:23:15 +0000 | [diff] [blame] | 1035 | |
Chris Lattner | 4cf15e7 | 2004-04-11 20:21:06 +0000 | [diff] [blame] | 1036 | /// 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 Lattner | 4a06f35 | 2004-02-02 19:23:15 +0000 | [diff] [blame] | 1043 | /// |
| 1044 | void FPS::handleOneArgFPRW(MachineBasicBlock::iterator &I) { |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 1045 | MachineInstr *MI = I; |
Evan Cheng | a022bdf | 2008-07-21 20:02:45 +0000 | [diff] [blame] | 1046 | #ifndef NDEBUG |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1047 | unsigned NumOps = MI->getDesc().getNumOperands(); |
Evan Cheng | 171d09e | 2006-11-10 01:28:43 +0000 | [diff] [blame] | 1048 | assert(NumOps >= 2 && "FPRW instructions must have 2 ops!!"); |
Evan Cheng | a022bdf | 2008-07-21 20:02:45 +0000 | [diff] [blame] | 1049 | #endif |
Chris Lattner | 4a06f35 | 2004-02-02 19:23:15 +0000 | [diff] [blame] | 1050 | |
| 1051 | // Is this the last use of the source register? |
| 1052 | unsigned Reg = getFPReg(MI->getOperand(1)); |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 1053 | bool KillsSrc = MI->killsRegister(X86::FP0+Reg); |
Chris Lattner | 4a06f35 | 2004-02-02 19:23:15 +0000 | [diff] [blame] | 1054 | |
| 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 Cheng | 3f490f3 | 2010-10-12 23:19:28 +0000 | [diff] [blame] | 1059 | if (StackTop == 0) |
| 1060 | report_fatal_error("Stack cannot be empty!"); |
Chris Lattner | 4a06f35 | 2004-02-02 19:23:15 +0000 | [diff] [blame] | 1061 | --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 Lattner | 58fe459 | 2005-12-21 07:47:04 +0000 | [diff] [blame] | 1069 | // Change from the pseudo instruction to the concrete instruction. |
Chris Lattner | 4a06f35 | 2004-02-02 19:23:15 +0000 | [diff] [blame] | 1070 | MI->RemoveOperand(1); // Drop the source operand. |
| 1071 | MI->RemoveOperand(0); // Drop the destination operand. |
Chris Lattner | 5080f4d | 2008-01-11 18:10:50 +0000 | [diff] [blame] | 1072 | MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode()))); |
Chris Lattner | 4a06f35 | 2004-02-02 19:23:15 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1076 | //===----------------------------------------------------------------------===// |
| 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) |
| 1081 | static const TableEntry ForwardST0Table[] = { |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1082 | { X86::ADD_Fp32 , X86::ADD_FST0r }, |
| 1083 | { X86::ADD_Fp64 , X86::ADD_FST0r }, |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1084 | { X86::ADD_Fp80 , X86::ADD_FST0r }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1085 | { X86::DIV_Fp32 , X86::DIV_FST0r }, |
| 1086 | { X86::DIV_Fp64 , X86::DIV_FST0r }, |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1087 | { X86::DIV_Fp80 , X86::DIV_FST0r }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1088 | { X86::MUL_Fp32 , X86::MUL_FST0r }, |
| 1089 | { X86::MUL_Fp64 , X86::MUL_FST0r }, |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1090 | { X86::MUL_Fp80 , X86::MUL_FST0r }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1091 | { X86::SUB_Fp32 , X86::SUB_FST0r }, |
| 1092 | { X86::SUB_Fp64 , X86::SUB_FST0r }, |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1093 | { X86::SUB_Fp80 , X86::SUB_FST0r }, |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1094 | }; |
| 1095 | |
| 1096 | // ReverseST0Table - Map: A = B op C into: ST(0) = ST(i) op ST(0) |
| 1097 | static const TableEntry ReverseST0Table[] = { |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1098 | { X86::ADD_Fp32 , X86::ADD_FST0r }, // commutative |
| 1099 | { X86::ADD_Fp64 , X86::ADD_FST0r }, // commutative |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1100 | { X86::ADD_Fp80 , X86::ADD_FST0r }, // commutative |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1101 | { X86::DIV_Fp32 , X86::DIVR_FST0r }, |
| 1102 | { X86::DIV_Fp64 , X86::DIVR_FST0r }, |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1103 | { X86::DIV_Fp80 , X86::DIVR_FST0r }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1104 | { X86::MUL_Fp32 , X86::MUL_FST0r }, // commutative |
| 1105 | { X86::MUL_Fp64 , X86::MUL_FST0r }, // commutative |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1106 | { X86::MUL_Fp80 , X86::MUL_FST0r }, // commutative |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1107 | { X86::SUB_Fp32 , X86::SUBR_FST0r }, |
| 1108 | { X86::SUB_Fp64 , X86::SUBR_FST0r }, |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1109 | { X86::SUB_Fp80 , X86::SUBR_FST0r }, |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1110 | }; |
| 1111 | |
| 1112 | // ForwardSTiTable - Map: A = B op C into: ST(i) = ST(0) op ST(i) |
| 1113 | static const TableEntry ForwardSTiTable[] = { |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1114 | { X86::ADD_Fp32 , X86::ADD_FrST0 }, // commutative |
| 1115 | { X86::ADD_Fp64 , X86::ADD_FrST0 }, // commutative |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1116 | { X86::ADD_Fp80 , X86::ADD_FrST0 }, // commutative |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1117 | { X86::DIV_Fp32 , X86::DIVR_FrST0 }, |
| 1118 | { X86::DIV_Fp64 , X86::DIVR_FrST0 }, |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1119 | { X86::DIV_Fp80 , X86::DIVR_FrST0 }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1120 | { X86::MUL_Fp32 , X86::MUL_FrST0 }, // commutative |
| 1121 | { X86::MUL_Fp64 , X86::MUL_FrST0 }, // commutative |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1122 | { X86::MUL_Fp80 , X86::MUL_FrST0 }, // commutative |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1123 | { X86::SUB_Fp32 , X86::SUBR_FrST0 }, |
| 1124 | { X86::SUB_Fp64 , X86::SUBR_FrST0 }, |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1125 | { X86::SUB_Fp80 , X86::SUBR_FrST0 }, |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1126 | }; |
| 1127 | |
| 1128 | // ReverseSTiTable - Map: A = B op C into: ST(i) = ST(i) op ST(0) |
| 1129 | static const TableEntry ReverseSTiTable[] = { |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1130 | { X86::ADD_Fp32 , X86::ADD_FrST0 }, |
| 1131 | { X86::ADD_Fp64 , X86::ADD_FrST0 }, |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1132 | { X86::ADD_Fp80 , X86::ADD_FrST0 }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1133 | { X86::DIV_Fp32 , X86::DIV_FrST0 }, |
| 1134 | { X86::DIV_Fp64 , X86::DIV_FrST0 }, |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1135 | { X86::DIV_Fp80 , X86::DIV_FrST0 }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1136 | { X86::MUL_Fp32 , X86::MUL_FrST0 }, |
| 1137 | { X86::MUL_Fp64 , X86::MUL_FrST0 }, |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1138 | { X86::MUL_Fp80 , X86::MUL_FrST0 }, |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1139 | { X86::SUB_Fp32 , X86::SUB_FrST0 }, |
| 1140 | { X86::SUB_Fp64 , X86::SUB_FrST0 }, |
Dale Johannesen | 6a30811 | 2007-08-06 21:31:06 +0000 | [diff] [blame] | 1141 | { X86::SUB_Fp80 , X86::SUB_FrST0 }, |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1142 | }; |
| 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 Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1152 | /// |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1153 | void FPS::handleTwoArgFP(MachineBasicBlock::iterator &I) { |
| 1154 | ASSERT_SORTED(ForwardST0Table); ASSERT_SORTED(ReverseST0Table); |
| 1155 | ASSERT_SORTED(ForwardSTiTable); ASSERT_SORTED(ReverseSTiTable); |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 1156 | MachineInstr *MI = I; |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1157 | |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1158 | unsigned NumOperands = MI->getDesc().getNumOperands(); |
Chris Lattner | d62d5d7 | 2004-06-11 04:25:06 +0000 | [diff] [blame] | 1159 | assert(NumOperands == 3 && "Illegal TwoArgFP instruction!"); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1160 | unsigned Dest = getFPReg(MI->getOperand(0)); |
| 1161 | unsigned Op0 = getFPReg(MI->getOperand(NumOperands-2)); |
| 1162 | unsigned Op1 = getFPReg(MI->getOperand(NumOperands-1)); |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 1163 | bool KillsOp0 = MI->killsRegister(X86::FP0+Op0); |
| 1164 | bool KillsOp1 = MI->killsRegister(X86::FP0+Op1); |
Dale Johannesen | 8d13f8f | 2009-02-13 02:33:27 +0000 | [diff] [blame] | 1165 | DebugLoc dl = MI->getDebugLoc(); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1166 | |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1167 | 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 Lattner | d62d5d7 | 2004-06-11 04:25:06 +0000 | [diff] [blame] | 1191 | } else if (!KillsOp0 && !KillsOp1) { |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1192 | // 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 Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1202 | assert((TOS == Op0 || TOS == Op1) && (KillsOp0 || KillsOp1) && |
| 1203 | "Stack conditions not set up right!"); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1204 | |
| 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 Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1221 | |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 1222 | int Opcode = Lookup(InstTable, array_lengthof(ForwardST0Table), |
| 1223 | MI->getOpcode()); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1224 | 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 Lattner | c1bab32 | 2004-03-31 22:02:36 +0000 | [diff] [blame] | 1230 | MBB->remove(I++); |
Dale Johannesen | 8d13f8f | 2009-02-13 02:33:27 +0000 | [diff] [blame] | 1231 | I = BuildMI(*MBB, I, dl, TII->get(Opcode)).addReg(getSTReg(NotTOS)); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1232 | |
| 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 Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1240 | // Update stack information so that we know the destination register is now on |
| 1241 | // the stack. |
Chris Lattner | d62d5d7 | 2004-06-11 04:25:06 +0000 | [diff] [blame] | 1242 | unsigned UpdatedSlot = getSlot(updateST0 ? TOS : NotTOS); |
| 1243 | assert(UpdatedSlot < StackTop && Dest < 7); |
| 1244 | Stack[UpdatedSlot] = Dest; |
| 1245 | RegMap[Dest] = UpdatedSlot; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1246 | MBB->getParent()->DeleteMachineInstr(MI); // Remove the old instruction |
Chris Lattner | d62d5d7 | 2004-06-11 04:25:06 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Chris Lattner | 0ca2c8e | 2004-06-11 04:49:02 +0000 | [diff] [blame] | 1249 | /// handleCompareFP - Handle FUCOM and FUCOMI instructions, which have two FP |
Chris Lattner | d62d5d7 | 2004-06-11 04:25:06 +0000 | [diff] [blame] | 1250 | /// register arguments and no explicit destinations. |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1251 | /// |
Chris Lattner | d62d5d7 | 2004-06-11 04:25:06 +0000 | [diff] [blame] | 1252 | void 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 Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1257 | unsigned NumOperands = MI->getDesc().getNumOperands(); |
Chris Lattner | 0ca2c8e | 2004-06-11 04:49:02 +0000 | [diff] [blame] | 1258 | assert(NumOperands == 2 && "Illegal FUCOM* instruction!"); |
Chris Lattner | d62d5d7 | 2004-06-11 04:25:06 +0000 | [diff] [blame] | 1259 | unsigned Op0 = getFPReg(MI->getOperand(NumOperands-2)); |
| 1260 | unsigned Op1 = getFPReg(MI->getOperand(NumOperands-1)); |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 1261 | bool KillsOp0 = MI->killsRegister(X86::FP0+Op0); |
| 1262 | bool KillsOp1 = MI->killsRegister(X86::FP0+Op1); |
Chris Lattner | d62d5d7 | 2004-06-11 04:25:06 +0000 | [diff] [blame] | 1263 | |
| 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 Lattner | 58fe459 | 2005-12-21 07:47:04 +0000 | [diff] [blame] | 1268 | // Change from the pseudo instruction to the concrete instruction. |
Chris Lattner | 5779042 | 2004-06-11 05:22:44 +0000 | [diff] [blame] | 1269 | MI->getOperand(0).setReg(getSTReg(Op1)); |
| 1270 | MI->RemoveOperand(1); |
Chris Lattner | 5080f4d | 2008-01-11 18:10:50 +0000 | [diff] [blame] | 1271 | MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode()))); |
Chris Lattner | 5779042 | 2004-06-11 05:22:44 +0000 | [diff] [blame] | 1272 | |
Chris Lattner | d62d5d7 | 2004-06-11 04:25:06 +0000 | [diff] [blame] | 1273 | // 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 Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Chris Lattner | c1bab32 | 2004-03-31 22:02:36 +0000 | [diff] [blame] | 1278 | /// 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. |
| 1282 | void FPS::handleCondMovFP(MachineBasicBlock::iterator &I) { |
| 1283 | MachineInstr *MI = I; |
| 1284 | |
| 1285 | unsigned Op0 = getFPReg(MI->getOperand(0)); |
Chris Lattner | 6cdb1ea | 2006-09-05 20:27:32 +0000 | [diff] [blame] | 1286 | unsigned Op1 = getFPReg(MI->getOperand(2)); |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 1287 | bool KillsOp1 = MI->killsRegister(X86::FP0+Op1); |
Chris Lattner | c1bab32 | 2004-03-31 22:02:36 +0000 | [diff] [blame] | 1288 | |
| 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 Lattner | 58fe459 | 2005-12-21 07:47:04 +0000 | [diff] [blame] | 1293 | // Change from the pseudo instruction to the concrete instruction. |
Chris Lattner | c1bab32 | 2004-03-31 22:02:36 +0000 | [diff] [blame] | 1294 | MI->RemoveOperand(0); |
Chris Lattner | 6cdb1ea | 2006-09-05 20:27:32 +0000 | [diff] [blame] | 1295 | MI->RemoveOperand(1); |
Chris Lattner | c1bab32 | 2004-03-31 22:02:36 +0000 | [diff] [blame] | 1296 | MI->getOperand(0).setReg(getSTReg(Op1)); |
Chris Lattner | 5080f4d | 2008-01-11 18:10:50 +0000 | [diff] [blame] | 1297 | MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode()))); |
Chris Lattner | 58fe459 | 2005-12-21 07:47:04 +0000 | [diff] [blame] | 1298 | |
Chris Lattner | c1bab32 | 2004-03-31 22:02:36 +0000 | [diff] [blame] | 1299 | // If we kill the second operand, make sure to pop it from the stack. |
Evan Cheng | ddd2a45 | 2006-11-15 20:56:39 +0000 | [diff] [blame] | 1300 | if (Op0 != Op1 && KillsOp1) { |
Chris Lattner | 76eb08b | 2005-08-23 22:49:55 +0000 | [diff] [blame] | 1301 | // Get this value off of the register stack. |
| 1302 | freeStackSlotAfter(I, Op1); |
| 1303 | } |
Chris Lattner | c1bab32 | 2004-03-31 22:02:36 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1306 | |
| 1307 | /// handleSpecialFP - Handle special instructions which behave unlike other |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 1308 | /// floating point instructions. This is primarily intended for use by pseudo |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1309 | /// instructions. |
| 1310 | /// |
| 1311 | void FPS::handleSpecialFP(MachineBasicBlock::iterator &I) { |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 1312 | MachineInstr *MI = I; |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1313 | switch (MI->getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1314 | default: llvm_unreachable("Unknown SpecialFP instruction!"); |
Chris Lattner | 6fa2f9c | 2008-03-09 07:05:32 +0000 | [diff] [blame] | 1315 | 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 Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1318 | assert(StackTop == 0 && "Stack should be empty after a call!"); |
| 1319 | pushReg(getFPReg(MI->getOperand(0))); |
| 1320 | break; |
Chris Lattner | 24e0a54 | 2008-03-21 06:38:26 +0000 | [diff] [blame] | 1321 | 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 Cheng | 3f490f3 | 2010-10-12 23:19:28 +0000 | [diff] [blame] | 1353 | if (RegMap[RegOnTop] >= StackTop) |
| 1354 | report_fatal_error("Access past stack top!"); |
Chris Lattner | 24e0a54 | 2008-03-21 06:38:26 +0000 | [diff] [blame] | 1355 | std::swap(Stack[RegMap[RegOnTop]], Stack[StackTop-1]); |
| 1356 | break; |
| 1357 | } |
Chris Lattner | afb23f4 | 2008-03-09 07:08:44 +0000 | [diff] [blame] | 1358 | case X86::FpSET_ST0_32: |
| 1359 | case X86::FpSET_ST0_64: |
Rafael Espindola | f55715c | 2009-06-30 12:18:16 +0000 | [diff] [blame] | 1360 | case X86::FpSET_ST0_80: { |
Jakob Stoklund Olesen | 2b336bc | 2010-07-10 17:42:34 +0000 | [diff] [blame] | 1361 | // 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 Espindola | af5f6ba | 2009-06-30 16:40:03 +0000 | [diff] [blame] | 1364 | unsigned Op0 = getFPReg(MI->getOperand(0)); |
| 1365 | |
Rafael Espindola | af5f6ba | 2009-06-30 16:40:03 +0000 | [diff] [blame] | 1366 | if (!MI->killsRegister(X86::FP0 + Op0)) { |
Jakob Stoklund Olesen | 2b336bc | 2010-07-10 17:42:34 +0000 | [diff] [blame] | 1367 | // Duplicate Op0 into a temporary on the stack top. |
Jakob Stoklund Olesen | e098e7a | 2010-07-16 17:41:40 +0000 | [diff] [blame] | 1368 | duplicateToTop(Op0, getScratchReg(), I); |
Rafael Espindola | af5f6ba | 2009-06-30 16:40:03 +0000 | [diff] [blame] | 1369 | } else { |
Jakob Stoklund Olesen | 2b336bc | 2010-07-10 17:42:34 +0000 | [diff] [blame] | 1370 | // Op0 is killed, so just swap it into position. |
Rafael Espindola | af5f6ba | 2009-06-30 16:40:03 +0000 | [diff] [blame] | 1371 | moveToTop(Op0, I); |
Rafael Espindola | 1c3329f | 2009-06-21 12:02:51 +0000 | [diff] [blame] | 1372 | } |
Evan Cheng | a0eedac | 2009-02-09 23:32:07 +0000 | [diff] [blame] | 1373 | --StackTop; // "Forget" we have something on the top of stack! |
| 1374 | break; |
Rafael Espindola | f55715c | 2009-06-30 12:18:16 +0000 | [diff] [blame] | 1375 | } |
Evan Cheng | a0eedac | 2009-02-09 23:32:07 +0000 | [diff] [blame] | 1376 | case X86::FpSET_ST1_32: |
| 1377 | case X86::FpSET_ST1_64: |
Jakob Stoklund Olesen | 2b336bc | 2010-07-10 17:42:34 +0000 | [diff] [blame] | 1378 | 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 Olesen | e098e7a | 2010-07-16 17:41:40 +0000 | [diff] [blame] | 1388 | duplicateToTop(Op0, getScratchReg(), I); |
Jakob Stoklund Olesen | 2b336bc | 2010-07-10 17:42:34 +0000 | [diff] [blame] | 1389 | 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 Cheng | a0eedac | 2009-02-09 23:32:07 +0000 | [diff] [blame] | 1395 | } |
Jakob Stoklund Olesen | 2b336bc | 2010-07-10 17:42:34 +0000 | [diff] [blame] | 1396 | assert(StackTop >= 2 && "Too few live registers"); |
| 1397 | StackTop -= 2; // "Forget" both st(0) and st(1). |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1398 | break; |
Jakob Stoklund Olesen | 2b336bc | 2010-07-10 17:42:34 +0000 | [diff] [blame] | 1399 | } |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 1400 | case X86::MOV_Fp3232: |
| 1401 | case X86::MOV_Fp3264: |
| 1402 | case X86::MOV_Fp6432: |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 1403 | 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 Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 1409 | const MachineOperand &MO1 = MI->getOperand(1); |
| 1410 | unsigned SrcReg = getFPReg(MO1); |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1411 | |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 1412 | const MachineOperand &MO0 = MI->getOperand(0); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 1413 | unsigned DestReg = getFPReg(MO0); |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 1414 | if (MI->killsRegister(X86::FP0+SrcReg)) { |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1415 | // 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 Lewycky | 3c78697 | 2008-03-11 05:56:09 +0000 | [diff] [blame] | 1427 | } |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1428 | break; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 1429 | case TargetOpcode::INLINEASM: { |
Chris Lattner | e12ecf2 | 2008-03-11 19:50:13 +0000 | [diff] [blame] | 1430 | // 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 Olesen | 7261fb2 | 2010-04-28 18:28:37 +0000 | [diff] [blame] | 1434 | unsigned Kills = 0; |
Chris Lattner | e12ecf2 | 2008-03-11 19:50:13 +0000 | [diff] [blame] | 1435 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1436 | MachineOperand &Op = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1437 | if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6) |
Chris Lattner | e12ecf2 | 2008-03-11 19:50:13 +0000 | [diff] [blame] | 1438 | 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 Olesen | 7261fb2 | 2010-04-28 18:28:37 +0000 | [diff] [blame] | 1448 | Kills |= 1U << FPReg; |
Chris Lattner | e12ecf2 | 2008-03-11 19:50:13 +0000 | [diff] [blame] | 1449 | } |
| 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 Olesen | 7261fb2 | 2010-04-28 18:28:37 +0000 | [diff] [blame] | 1459 | while (Kills) { |
| 1460 | unsigned FPReg = CountTrailingZeros_32(Kills); |
| 1461 | freeStackSlotAfter(InsertPt, FPReg); |
| 1462 | Kills &= ~(1U << FPReg); |
| 1463 | } |
Chris Lattner | e12ecf2 | 2008-03-11 19:50:13 +0000 | [diff] [blame] | 1464 | // Don't delete the inline asm! |
| 1465 | return; |
| 1466 | } |
| 1467 | |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1468 | 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 Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 1472 | |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1473 | // Find the register operands. |
| 1474 | unsigned FirstFPRegOp = ~0U, SecondFPRegOp = ~0U; |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 1475 | unsigned LiveMask = 0; |
| 1476 | |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1477 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1478 | MachineOperand &Op = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1479 | if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6) |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1480 | continue; |
Chris Lattner | 35831d0 | 2008-03-21 20:41:27 +0000 | [diff] [blame] | 1481 | // 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 Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1488 | |
| 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 Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 1495 | LiveMask |= (1 << getFPReg(Op)); |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1496 | |
| 1497 | // Remove the operand so that later passes don't see it. |
| 1498 | MI->RemoveOperand(i); |
| 1499 | --i, --e; |
| 1500 | } |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 1501 | |
| 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 Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1507 | // 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 Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1523 | // 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 Olesen | e098e7a | 2010-07-16 17:41:40 +0000 | [diff] [blame] | 1532 | unsigned NewReg = getScratchReg(); |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1533 | 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 Lattner | 0353526 | 2008-03-21 05:57:20 +0000 | [diff] [blame] | 1550 | assert(getStackEntry(1) == SecondFPRegOp && "Unknown regs live"); |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1551 | StackTop = 0; |
| 1552 | return; |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1553 | } |
Chris Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1554 | |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 1555 | I = MBB->erase(I); // Remove the pseudo instruction |
Jakob Stoklund Olesen | e928ec9 | 2010-07-16 16:38:12 +0000 | [diff] [blame] | 1556 | |
| 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 Lattner | a960d95 | 2003-01-13 01:01:59 +0000 | [diff] [blame] | 1564 | } |
Jakob Stoklund Olesen | 7db1e7a | 2010-07-08 19:46:30 +0000 | [diff] [blame] | 1565 | |
| 1566 | // Translate a COPY instruction to a pseudo-op that handleSpecialFP understands. |
| 1567 | bool 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 | } |