LLVM API Documentation

X86FloatingPoint.cpp

Go to the documentation of this file.
00001 //===-- X86FloatingPoint.cpp - Floating point Reg -> Stack converter ------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the pass which converts floating point instructions from
00011 // virtual registers into register stack instructions.  This pass uses live
00012 // variable information to indicate where the FPn registers are used and their
00013 // lifetimes.
00014 //
00015 // This pass is hampered by the lack of decent CFG manipulation routines for
00016 // machine code.  In particular, this wants to be able to split critical edges
00017 // as necessary, traverse the machine basic block CFG in depth-first order, and
00018 // allow there to be multiple machine basic blocks for each LLVM basicblock
00019 // (needed for critical edge splitting).
00020 //
00021 // In particular, this pass currently barfs on critical edges.  Because of this,
00022 // it requires the instruction selector to insert FP_REG_KILL instructions on
00023 // the exits of any basic block that has critical edges going from it, or which
00024 // branch to a critical basic block.
00025 //
00026 // FIXME: this is not implemented yet.  The stackifier pass only works on local
00027 // basic blocks.
00028 //
00029 //===----------------------------------------------------------------------===//
00030 
00031 #define DEBUG_TYPE "x86-codegen"
00032 #include "X86.h"
00033 #include "X86InstrInfo.h"
00034 #include "llvm/ADT/DepthFirstIterator.h"
00035 #include "llvm/ADT/SmallPtrSet.h"
00036 #include "llvm/ADT/SmallVector.h"
00037 #include "llvm/ADT/Statistic.h"
00038 #include "llvm/ADT/STLExtras.h"
00039 #include "llvm/CodeGen/MachineFunctionPass.h"
00040 #include "llvm/CodeGen/MachineInstrBuilder.h"
00041 #include "llvm/CodeGen/MachineRegisterInfo.h"
00042 #include "llvm/CodeGen/Passes.h"
00043 #include "llvm/Support/Compiler.h"
00044 #include "llvm/Support/Debug.h"
00045 #include "llvm/Support/ErrorHandling.h"
00046 #include "llvm/Support/raw_ostream.h"
00047 #include "llvm/Target/TargetInstrInfo.h"
00048 #include "llvm/Target/TargetMachine.h"
00049 #include <algorithm>
00050 using namespace llvm;
00051 
00052 STATISTIC(NumFXCH, "Number of fxch instructions inserted");
00053 STATISTIC(NumFP  , "Number of floating point instructions");
00054 
00055 namespace {
00056   struct VISIBILITY_HIDDEN FPS : public MachineFunctionPass {
00057     static char ID;
00058     FPS() : MachineFunctionPass(&ID) {}
00059 
00060     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00061       AU.setPreservesCFG();
00062       AU.addPreservedID(MachineLoopInfoID);
00063       AU.addPreservedID(MachineDominatorsID);
00064       MachineFunctionPass::getAnalysisUsage(AU);
00065     }
00066 
00067     virtual bool runOnMachineFunction(MachineFunction &MF);
00068 
00069     virtual const char *getPassName() const { return "X86 FP Stackifier"; }
00070 
00071   private:
00072     const TargetInstrInfo *TII; // Machine instruction info.
00073     MachineBasicBlock *MBB;     // Current basic block
00074     unsigned Stack[8];          // FP<n> Registers in each stack slot...
00075     unsigned RegMap[8];         // Track which stack slot contains each register
00076     unsigned StackTop;          // The current top of the FP stack.
00077 
00078     void dumpStack() const {
00079       cerr << "Stack contents:";
00080       for (unsigned i = 0; i != StackTop; ++i) {
00081         cerr << " FP" << Stack[i];
00082         assert(RegMap[Stack[i]] == i && "Stack[] doesn't match RegMap[]!");
00083       }
00084       cerr << "\n";
00085     }
00086   private:
00087     /// isStackEmpty - Return true if the FP stack is empty.
00088     bool isStackEmpty() const {
00089       return StackTop == 0;
00090     }
00091     
00092     // getSlot - Return the stack slot number a particular register number is
00093     // in.
00094     unsigned getSlot(unsigned RegNo) const {
00095       assert(RegNo < 8 && "Regno out of range!");
00096       return RegMap[RegNo];
00097     }
00098 
00099     // getStackEntry - Return the X86::FP<n> register in register ST(i).
00100     unsigned getStackEntry(unsigned STi) const {
00101       assert(STi < StackTop && "Access past stack top!");
00102       return Stack[StackTop-1-STi];
00103     }
00104 
00105     // getSTReg - Return the X86::ST(i) register which contains the specified
00106     // FP<RegNo> register.
00107     unsigned getSTReg(unsigned RegNo) const {
00108       return StackTop - 1 - getSlot(RegNo) + llvm::X86::ST0;
00109     }
00110 
00111     // pushReg - Push the specified FP<n> register onto the stack.
00112     void pushReg(unsigned Reg) {
00113       assert(Reg < 8 && "Register number out of range!");
00114       assert(StackTop < 8 && "Stack overflow!");
00115       Stack[StackTop] = Reg;
00116       RegMap[Reg] = StackTop++;
00117     }
00118 
00119     bool isAtTop(unsigned RegNo) const { return getSlot(RegNo) == StackTop-1; }
00120     void moveToTop(unsigned RegNo, MachineBasicBlock::iterator I) {
00121       MachineInstr *MI = I;
00122       DebugLoc dl = MI->getDebugLoc();
00123       if (isAtTop(RegNo)) return;
00124       
00125       unsigned STReg = getSTReg(RegNo);
00126       unsigned RegOnTop = getStackEntry(0);
00127 
00128       // Swap the slots the regs are in.
00129       std::swap(RegMap[RegNo], RegMap[RegOnTop]);
00130 
00131       // Swap stack slot contents.
00132       assert(RegMap[RegOnTop] < StackTop);
00133       std::swap(Stack[RegMap[RegOnTop]], Stack[StackTop-1]);
00134 
00135       // Emit an fxch to update the runtime processors version of the state.
00136       BuildMI(*MBB, I, dl, TII->get(X86::XCH_F)).addReg(STReg);
00137       NumFXCH++;
00138     }
00139 
00140     void duplicateToTop(unsigned RegNo, unsigned AsReg, MachineInstr *I) {
00141       DebugLoc dl = I->getDebugLoc();
00142       unsigned STReg = getSTReg(RegNo);
00143       pushReg(AsReg);   // New register on top of stack
00144 
00145       BuildMI(*MBB, I, dl, TII->get(X86::LD_Frr)).addReg(STReg);
00146     }
00147 
00148     // popStackAfter - Pop the current value off of the top of the FP stack
00149     // after the specified instruction.
00150     void popStackAfter(MachineBasicBlock::iterator &I);
00151 
00152     // freeStackSlotAfter - Free the specified register from the register stack,
00153     // so that it is no longer in a register.  If the register is currently at
00154     // the top of the stack, we just pop the current instruction, otherwise we
00155     // store the current top-of-stack into the specified slot, then pop the top
00156     // of stack.
00157     void freeStackSlotAfter(MachineBasicBlock::iterator &I, unsigned Reg);
00158 
00159     bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB);
00160 
00161     void handleZeroArgFP(MachineBasicBlock::iterator &I);
00162     void handleOneArgFP(MachineBasicBlock::iterator &I);
00163     void handleOneArgFPRW(MachineBasicBlock::iterator &I);
00164     void handleTwoArgFP(MachineBasicBlock::iterator &I);
00165     void handleCompareFP(MachineBasicBlock::iterator &I);
00166     void handleCondMovFP(MachineBasicBlock::iterator &I);
00167     void handleSpecialFP(MachineBasicBlock::iterator &I);
00168   };
00169   char FPS::ID = 0;
00170 }
00171 
00172 FunctionPass *llvm::createX86FloatingPointStackifierPass() { return new FPS(); }
00173 
00174 /// getFPReg - Return the X86::FPx register number for the specified operand.
00175 /// For example, this returns 3 for X86::FP3.
00176 static unsigned getFPReg(const MachineOperand &MO) {
00177   assert(MO.isReg() && "Expected an FP register!");
00178   unsigned Reg = MO.getReg();
00179   assert(Reg >= X86::FP0 && Reg <= X86::FP6 && "Expected FP register!");
00180   return Reg - X86::FP0;
00181 }
00182 
00183 
00184 /// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
00185 /// register references into FP stack references.
00186 ///
00187 bool FPS::runOnMachineFunction(MachineFunction &MF) {
00188   // We only need to run this pass if there are any FP registers used in this
00189   // function.  If it is all integer, there is nothing for us to do!
00190   bool FPIsUsed = false;
00191 
00192   assert(X86::FP6 == X86::FP0+6 && "Register enums aren't sorted right!");
00193   for (unsigned i = 0; i <= 6; ++i)
00194     if (MF.getRegInfo().isPhysRegUsed(X86::FP0+i)) {
00195       FPIsUsed = true;
00196       break;
00197     }
00198 
00199   // Early exit.
00200   if (!FPIsUsed) return false;
00201 
00202   TII = MF.getTarget().getInstrInfo();
00203   StackTop = 0;
00204 
00205   // Process the function in depth first order so that we process at least one
00206   // of the predecessors for every reachable block in the function.
00207   SmallPtrSet<MachineBasicBlock*, 8> Processed;
00208   MachineBasicBlock *Entry = MF.begin();
00209 
00210   bool Changed = false;
00211   for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 8> >
00212          I = df_ext_begin(Entry, Processed), E = df_ext_end(Entry, Processed);
00213        I != E; ++I)
00214     Changed |= processBasicBlock(MF, **I);
00215 
00216   // Process any unreachable blocks in arbitrary order now.
00217   if (MF.size() == Processed.size())
00218     return Changed;
00219 
00220   for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
00221     if (Processed.insert(BB))
00222       Changed |= processBasicBlock(MF, *BB);
00223   
00224   return Changed;
00225 }
00226 
00227 /// processBasicBlock - Loop over all of the instructions in the basic block,
00228 /// transforming FP instructions into their stack form.
00229 ///
00230 bool FPS::processBasicBlock(MachineFunction &MF, MachineBasicBlock &BB) {
00231   bool Changed = false;
00232   MBB = &BB;
00233 
00234   for (MachineBasicBlock::iterator I = BB.begin(); I != BB.end(); ++I) {
00235     MachineInstr *MI = I;
00236     unsigned Flags = MI->getDesc().TSFlags;
00237     
00238     unsigned FPInstClass = Flags & X86II::FPTypeMask;
00239     if (MI->getOpcode() == TargetInstrInfo::INLINEASM)
00240       FPInstClass = X86II::SpecialFP;
00241     
00242     if (FPInstClass == X86II::NotFP)
00243       continue;  // Efficiently ignore non-fp insts!
00244 
00245     MachineInstr *PrevMI = 0;
00246     if (I != BB.begin())
00247       PrevMI = prior(I);
00248 
00249     ++NumFP;  // Keep track of # of pseudo instrs
00250     DEBUG(errs() << "\nFPInst:\t" << *MI);
00251 
00252     // Get dead variables list now because the MI pointer may be deleted as part
00253     // of processing!
00254     SmallVector<unsigned, 8> DeadRegs;
00255     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00256       const MachineOperand &MO = MI->getOperand(i);
00257       if (MO.isReg() && MO.isDead())
00258         DeadRegs.push_back(MO.getReg());
00259     }
00260 
00261     switch (FPInstClass) {
00262     case X86II::ZeroArgFP:  handleZeroArgFP(I); break;
00263     case X86II::OneArgFP:   handleOneArgFP(I);  break;  // fstp ST(0)
00264     case X86II::OneArgFPRW: handleOneArgFPRW(I); break; // ST(0) = fsqrt(ST(0))
00265     case X86II::TwoArgFP:   handleTwoArgFP(I);  break;
00266     case X86II::CompareFP:  handleCompareFP(I); break;
00267     case X86II::CondMovFP:  handleCondMovFP(I); break;
00268     case X86II::SpecialFP:  handleSpecialFP(I); break;
00269     default: llvm_unreachable("Unknown FP Type!");
00270     }
00271 
00272     // Check to see if any of the values defined by this instruction are dead
00273     // after definition.  If so, pop them.
00274     for (unsigned i = 0, e = DeadRegs.size(); i != e; ++i) {
00275       unsigned Reg = DeadRegs[i];
00276       if (Reg >= X86::FP0 && Reg <= X86::FP6) {
00277         DEBUG(errs() << "Register FP#" << Reg-X86::FP0 << " is dead!\n");
00278         freeStackSlotAfter(I, Reg-X86::FP0);
00279       }
00280     }
00281 
00282     // Print out all of the instructions expanded to if -debug
00283     DEBUG(
00284       MachineBasicBlock::iterator PrevI(PrevMI);
00285       if (I == PrevI) {
00286         cerr << "Just deleted pseudo instruction\n";
00287       } else {
00288         MachineBasicBlock::iterator Start = I;
00289         // Rewind to first instruction newly inserted.
00290         while (Start != BB.begin() && prior(Start) != PrevI) --Start;
00291         cerr << "Inserted instructions:\n\t";
00292         Start->print(*cerr.stream(), &MF.getTarget());
00293         while (++Start != next(I)) {}
00294       }
00295       dumpStack();
00296     );
00297 
00298     Changed = true;
00299   }
00300 
00301   assert(isStackEmpty() && "Stack not empty at end of basic block?");
00302   return Changed;
00303 }
00304 
00305 //===----------------------------------------------------------------------===//
00306 // Efficient Lookup Table Support
00307 //===----------------------------------------------------------------------===//
00308 
00309 namespace {
00310   struct TableEntry {
00311     unsigned from;
00312     unsigned to;
00313     bool operator<(const TableEntry &TE) const { return from < TE.from; }
00314     friend bool operator<(const TableEntry &TE, unsigned V) {
00315       return TE.from < V;
00316     }
00317     friend bool operator<(unsigned V, const TableEntry &TE) {
00318       return V < TE.from;
00319     }
00320   };
00321 }
00322 
00323 #ifndef NDEBUG
00324 static bool TableIsSorted(const TableEntry *Table, unsigned NumEntries) {
00325   for (unsigned i = 0; i != NumEntries-1; ++i)
00326     if (!(Table[i] < Table[i+1])) return false;
00327   return true;
00328 }
00329 #endif
00330 
00331 static int Lookup(const TableEntry *Table, unsigned N, unsigned Opcode) {
00332   const TableEntry *I = std::lower_bound(Table, Table+N, Opcode);
00333   if (I != Table+N && I->from == Opcode)
00334     return I->to;
00335   return -1;
00336 }
00337 
00338 #ifdef NDEBUG
00339 #define ASSERT_SORTED(TABLE)
00340 #else
00341 #define ASSERT_SORTED(TABLE)                                              \
00342   { static bool TABLE##Checked = false;                                   \
00343     if (!TABLE##Checked) {                                                \
00344        assert(TableIsSorted(TABLE, array_lengthof(TABLE)) &&              \
00345               "All lookup tables must be sorted for efficient access!");  \
00346        TABLE##Checked = true;                                             \
00347     }                                                                     \
00348   }
00349 #endif
00350 
00351 //===----------------------------------------------------------------------===//
00352 // Register File -> Register Stack Mapping Methods
00353 //===----------------------------------------------------------------------===//
00354 
00355 // OpcodeTable - Sorted map of register instructions to their stack version.
00356 // The first element is an register file pseudo instruction, the second is the
00357 // concrete X86 instruction which uses the register stack.
00358 //
00359 static const TableEntry OpcodeTable[] = {
00360   { X86::ABS_Fp32     , X86::ABS_F     },
00361   { X86::ABS_Fp64     , X86::ABS_F     },
00362   { X86::ABS_Fp80     , X86::ABS_F     },
00363   { X86::ADD_Fp32m    , X86::ADD_F32m  },
00364   { X86::ADD_Fp64m    , X86::ADD_F64m  },
00365   { X86::ADD_Fp64m32  , X86::ADD_F32m  },
00366   { X86::ADD_Fp80m32  , X86::ADD_F32m  },
00367   { X86::ADD_Fp80m64  , X86::ADD_F64m  },
00368   { X86::ADD_FpI16m32 , X86::ADD_FI16m },
00369   { X86::ADD_FpI16m64 , X86::ADD_FI16m },
00370   { X86::ADD_FpI16m80 , X86::ADD_FI16m },
00371   { X86::ADD_FpI32m32 , X86::ADD_FI32m },
00372   { X86::ADD_FpI32m64 , X86::ADD_FI32m },
00373   { X86::ADD_FpI32m80 , X86::ADD_FI32m },
00374   { X86::CHS_Fp32     , X86::CHS_F     },
00375   { X86::CHS_Fp64     , X86::CHS_F     },
00376   { X86::CHS_Fp80     , X86::CHS_F     },
00377   { X86::CMOVBE_Fp32  , X86::CMOVBE_F  },
00378   { X86::CMOVBE_Fp64  , X86::CMOVBE_F  },
00379   { X86::CMOVBE_Fp80  , X86::CMOVBE_F  },
00380   { X86::CMOVB_Fp32   , X86::CMOVB_F   },
00381   { X86::CMOVB_Fp64   , X86::CMOVB_F  },
00382   { X86::CMOVB_Fp80   , X86::CMOVB_F  },
00383   { X86::CMOVE_Fp32   , X86::CMOVE_F  },
00384   { X86::CMOVE_Fp64   , X86::CMOVE_F   },
00385   { X86::CMOVE_Fp80   , X86::CMOVE_F   },
00386   { X86::CMOVNBE_Fp32 , X86::CMOVNBE_F },
00387   { X86::CMOVNBE_Fp64 , X86::CMOVNBE_F },
00388   { X86::CMOVNBE_Fp80 , X86::CMOVNBE_F },
00389   { X86::CMOVNB_Fp32  , X86::CMOVNB_F  },
00390   { X86::CMOVNB_Fp64  , X86::CMOVNB_F  },
00391   { X86::CMOVNB_Fp80  , X86::CMOVNB_F  },
00392   { X86::CMOVNE_Fp32  , X86::CMOVNE_F  },
00393   { X86::CMOVNE_Fp64  , X86::CMOVNE_F  },
00394   { X86::CMOVNE_Fp80  , X86::CMOVNE_F  },
00395   { X86::CMOVNP_Fp32  , X86::CMOVNP_F  },
00396   { X86::CMOVNP_Fp64  , X86::CMOVNP_F  },
00397   { X86::CMOVNP_Fp80  , X86::CMOVNP_F  },
00398   { X86::CMOVP_Fp32   , X86::CMOVP_F   },
00399   { X86::CMOVP_Fp64   , X86::CMOVP_F   },
00400   { X86::CMOVP_Fp80   , X86::CMOVP_F   },
00401   { X86::COS_Fp32     , X86::COS_F     },
00402   { X86::COS_Fp64     , X86::COS_F     },
00403   { X86::COS_Fp80     , X86::COS_F     },
00404   { X86::DIVR_Fp32m   , X86::DIVR_F32m },
00405   { X86::DIVR_Fp64m   , X86::DIVR_F64m },
00406   { X86::DIVR_Fp64m32 , X86::DIVR_F32m },
00407   { X86::DIVR_Fp80m32 , X86::DIVR_F32m },
00408   { X86::DIVR_Fp80m64 , X86::DIVR_F64m },
00409   { X86::DIVR_FpI16m32, X86::DIVR_FI16m},
00410   { X86::DIVR_FpI16m64, X86::DIVR_FI16m},
00411   { X86::DIVR_FpI16m80, X86::DIVR_FI16m},
00412   { X86::DIVR_FpI32m32, X86::DIVR_FI32m},
00413   { X86::DIVR_FpI32m64, X86::DIVR_FI32m},
00414   { X86::DIVR_FpI32m80, X86::DIVR_FI32m},
00415   { X86::DIV_Fp32m    , X86::DIV_F32m  },
00416   { X86::DIV_Fp64m    , X86::DIV_F64m  },
00417   { X86::DIV_Fp64m32  , X86::DIV_F32m  },
00418   { X86::DIV_Fp80m32  , X86::DIV_F32m  },
00419   { X86::DIV_Fp80m64  , X86::DIV_F64m  },
00420   { X86::DIV_FpI16m32 , X86::DIV_FI16m },
00421   { X86::DIV_FpI16m64 , X86::DIV_FI16m },
00422   { X86::DIV_FpI16m80 , X86::DIV_FI16m },
00423   { X86::DIV_FpI32m32 , X86::DIV_FI32m },
00424   { X86::DIV_FpI32m64 , X86::DIV_FI32m },
00425   { X86::DIV_FpI32m80 , X86::DIV_FI32m },
00426   { X86::ILD_Fp16m32  , X86::ILD_F16m  },
00427   { X86::ILD_Fp16m64  , X86::ILD_F16m  },
00428   { X86::ILD_Fp16m80  , X86::ILD_F16m  },
00429   { X86::ILD_Fp32m32  , X86::ILD_F32m  },
00430   { X86::ILD_Fp32m64  , X86::ILD_F32m  },
00431   { X86::ILD_Fp32m80  , X86::ILD_F32m  },
00432   { X86::ILD_Fp64m32  , X86::ILD_F64m  },
00433   { X86::ILD_Fp64m64  , X86::ILD_F64m  },
00434   { X86::ILD_Fp64m80  , X86::ILD_F64m  },
00435   { X86::ISTT_Fp16m32 , X86::ISTT_FP16m},
00436   { X86::ISTT_Fp16m64 , X86::ISTT_FP16m},
00437   { X86::ISTT_Fp16m80 , X86::ISTT_FP16m},
00438   { X86::ISTT_Fp32m32 , X86::ISTT_FP32m},
00439   { X86::ISTT_Fp32m64 , X86::ISTT_FP32m},
00440   { X86::ISTT_Fp32m80 , X86::ISTT_FP32m},
00441   { X86::ISTT_Fp64m32 , X86::ISTT_FP64m},
00442   { X86::ISTT_Fp64m64 , X86::ISTT_FP64m},
00443   { X86::ISTT_Fp64m80 , X86::ISTT_FP64m},
00444   { X86::IST_Fp16m32  , X86::IST_F16m  },
00445   { X86::IST_Fp16m64  , X86::IST_F16m  },
00446   { X86::IST_Fp16m80  , X86::IST_F16m  },
00447   { X86::IST_Fp32m32  , X86::IST_F32m  },
00448   { X86::IST_Fp32m64  , X86::IST_F32m  },
00449   { X86::IST_Fp32m80  , X86::IST_F32m  },
00450   { X86::IST_Fp64m32  , X86::IST_FP64m },
00451   { X86::IST_Fp64m64  , X86::IST_FP64m },
00452   { X86::IST_Fp64m80  , X86::IST_FP64m },
00453   { X86::LD_Fp032     , X86::LD_F0     },
00454   { X86::LD_Fp064     , X86::LD_F0     },
00455   { X86::LD_Fp080     , X86::LD_F0     },
00456   { X86::LD_Fp132     , X86::LD_F1     },
00457   { X86::LD_Fp164     , X86::LD_F1     },
00458   { X86::LD_Fp180     , X86::LD_F1     },
00459   { X86::LD_Fp32m     , X86::LD_F32m   },
00460   { X86::LD_Fp32m64   , X86::LD_F32m   },
00461   { X86::LD_Fp32m80   , X86::LD_F32m   },
00462   { X86::LD_Fp64m     , X86::LD_F64m   },
00463   { X86::LD_Fp64m80   , X86::LD_F64m   },
00464   { X86::LD_Fp80m     , X86::LD_F80m   },
00465   { X86::MUL_Fp32m    , X86::MUL_F32m  },
00466   { X86::MUL_Fp64m    , X86::MUL_F64m  },
00467   { X86::MUL_Fp64m32  , X86::MUL_F32m  },
00468   { X86::MUL_Fp80m32  , X86::MUL_F32m  },
00469   { X86::MUL_Fp80m64  , X86::MUL_F64m  },
00470   { X86::MUL_FpI16m32 , X86::MUL_FI16m },
00471   { X86::MUL_FpI16m64 , X86::MUL_FI16m },
00472   { X86::MUL_FpI16m80 , X86::MUL_FI16m },
00473   { X86::MUL_FpI32m32 , X86::MUL_FI32m },
00474   { X86::MUL_FpI32m64 , X86::MUL_FI32m },
00475   { X86::MUL_FpI32m80 , X86::MUL_FI32m },
00476   { X86::SIN_Fp32     , X86::SIN_F     },
00477   { X86::SIN_Fp64     , X86::SIN_F     },
00478   { X86::SIN_Fp80     , X86::SIN_F     },
00479   { X86::SQRT_Fp32    , X86::SQRT_F    },
00480   { X86::SQRT_Fp64    , X86::SQRT_F    },
00481   { X86::SQRT_Fp80    , X86::SQRT_F    },
00482   { X86::ST_Fp32m     , X86::ST_F32m   },
00483   { X86::ST_Fp64m     , X86::ST_F64m   },
00484   { X86::ST_Fp64m32   , X86::ST_F32m   },
00485   { X86::ST_Fp80m32   , X86::ST_F32m   },
00486   { X86::ST_Fp80m64   , X86::ST_F64m   },
00487   { X86::ST_FpP80m    , X86::ST_FP80m  },
00488   { X86::SUBR_Fp32m   , X86::SUBR_F32m },
00489   { X86::SUBR_Fp64m   , X86::SUBR_F64m },
00490   { X86::SUBR_Fp64m32 , X86::SUBR_F32m },
00491   { X86::SUBR_Fp80m32 , X86::SUBR_F32m },
00492   { X86::SUBR_Fp80m64 , X86::SUBR_F64m },
00493   { X86::SUBR_FpI16m32, X86::SUBR_FI16m},
00494   { X86::SUBR_FpI16m64, X86::SUBR_FI16m},
00495   { X86::SUBR_FpI16m80, X86::SUBR_FI16m},
00496   { X86::SUBR_FpI32m32, X86::SUBR_FI32m},
00497   { X86::SUBR_FpI32m64, X86::SUBR_FI32m},
00498   { X86::SUBR_FpI32m80, X86::SUBR_FI32m},
00499   { X86::SUB_Fp32m    , X86::SUB_F32m  },
00500   { X86::SUB_Fp64m    , X86::SUB_F64m  },
00501   { X86::SUB_Fp64m32  , X86::SUB_F32m  },
00502   { X86::SUB_Fp80m32  , X86::SUB_F32m  },
00503   { X86::SUB_Fp80m64  , X86::SUB_F64m  },
00504   { X86::SUB_FpI16m32 , X86::SUB_FI16m },
00505   { X86::SUB_FpI16m64 , X86::SUB_FI16m },
00506   { X86::SUB_FpI16m80 , X86::SUB_FI16m },
00507   { X86::SUB_FpI32m32 , X86::SUB_FI32m },
00508   { X86::SUB_FpI32m64 , X86::SUB_FI32m },
00509   { X86::SUB_FpI32m80 , X86::SUB_FI32m },
00510   { X86::TST_Fp32     , X86::TST_F     },
00511   { X86::TST_Fp64     , X86::TST_F     },
00512   { X86::TST_Fp80     , X86::TST_F     },
00513   { X86::UCOM_FpIr32  , X86::UCOM_FIr  },
00514   { X86::UCOM_FpIr64  , X86::UCOM_FIr  },
00515   { X86::UCOM_FpIr80  , X86::UCOM_FIr  },
00516   { X86::UCOM_Fpr32   , X86::UCOM_Fr   },
00517   { X86::UCOM_Fpr64   , X86::UCOM_Fr   },
00518   { X86::UCOM_Fpr80   , X86::UCOM_Fr   },
00519 };
00520 
00521 static unsigned getConcreteOpcode(unsigned Opcode) {
00522   ASSERT_SORTED(OpcodeTable);
00523   int Opc = Lookup(OpcodeTable, array_lengthof(OpcodeTable), Opcode);
00524   assert(Opc != -1 && "FP Stack instruction not in OpcodeTable!");
00525   return Opc;
00526 }
00527 
00528 //===----------------------------------------------------------------------===//
00529 // Helper Methods
00530 //===----------------------------------------------------------------------===//
00531 
00532 // PopTable - Sorted map of instructions to their popping version.  The first
00533 // element is an instruction, the second is the version which pops.
00534 //
00535 static const TableEntry PopTable[] = {
00536   { X86::ADD_FrST0 , X86::ADD_FPrST0  },
00537 
00538   { X86::DIVR_FrST0, X86::DIVR_FPrST0 },
00539   { X86::DIV_FrST0 , X86::DIV_FPrST0  },
00540 
00541   { X86::IST_F16m  , X86::IST_FP16m   },
00542   { X86::IST_F32m  , X86::IST_FP32m   },
00543 
00544   { X86::MUL_FrST0 , X86::MUL_FPrST0  },
00545 
00546   { X86::ST_F32m   , X86::ST_FP32m    },
00547   { X86::ST_F64m   , X86::ST_FP64m    },
00548   { X86::ST_Frr    , X86::ST_FPrr     },
00549 
00550   { X86::SUBR_FrST0, X86::SUBR_FPrST0 },
00551   { X86::SUB_FrST0 , X86::SUB_FPrST0  },
00552 
00553   { X86::UCOM_FIr  , X86::UCOM_FIPr   },
00554 
00555   { X86::UCOM_FPr  , X86::UCOM_FPPr   },
00556   { X86::UCOM_Fr   , X86::UCOM_FPr    },
00557 };
00558 
00559 /// popStackAfter - Pop the current value off of the top of the FP stack after
00560 /// the specified instruction.  This attempts to be sneaky and combine the pop
00561 /// into the instruction itself if possible.  The iterator is left pointing to
00562 /// the last instruction, be it a new pop instruction inserted, or the old
00563 /// instruction if it was modified in place.
00564 ///
00565 void FPS::popStackAfter(MachineBasicBlock::iterator &I) {
00566   MachineInstr* MI = I;
00567   DebugLoc dl = MI->getDebugLoc();
00568   ASSERT_SORTED(PopTable);
00569   assert(StackTop > 0 && "Cannot pop empty stack!");
00570   RegMap[Stack[--StackTop]] = ~0;     // Update state
00571 
00572   // Check to see if there is a popping version of this instruction...
00573   int Opcode = Lookup(PopTable, array_lengthof(PopTable), I->getOpcode());
00574   if (Opcode != -1) {
00575     I->setDesc(TII->get(Opcode));
00576     if (Opcode == X86::UCOM_FPPr)
00577       I->RemoveOperand(0);
00578   } else {    // Insert an explicit pop
00579     I = BuildMI(*MBB, ++I, dl, TII->get(X86::ST_FPrr)).addReg(X86::ST0);
00580   }
00581 }
00582 
00583 /// freeStackSlotAfter - Free the specified register from the register stack, so
00584 /// that it is no longer in a register.  If the register is currently at the top
00585 /// of the stack, we just pop the current instruction, otherwise we store the
00586 /// current top-of-stack into the specified slot, then pop the top of stack.
00587 void FPS::freeStackSlotAfter(MachineBasicBlock::iterator &I, unsigned FPRegNo) {
00588   if (getStackEntry(0) == FPRegNo) {  // already at the top of stack? easy.
00589     popStackAfter(I);
00590     return;
00591   }
00592 
00593   // Otherwise, store the top of stack into the dead slot, killing the operand
00594   // without having to add in an explicit xchg then pop.
00595   //
00596   unsigned STReg    = getSTReg(FPRegNo);
00597   unsigned OldSlot  = getSlot(FPRegNo);
00598   unsigned TopReg   = Stack[StackTop-1];
00599   Stack[OldSlot]    = TopReg;
00600   RegMap[TopReg]    = OldSlot;
00601   RegMap[FPRegNo]   = ~0;
00602   Stack[--StackTop] = ~0;
00603   MachineInstr *MI  = I;
00604   DebugLoc dl = MI->getDebugLoc();
00605   I = BuildMI(*MBB, ++I, dl, TII->get(X86::ST_FPrr)).addReg(STReg);
00606 }
00607 
00608 
00609 //===----------------------------------------------------------------------===//
00610 // Instruction transformation implementation
00611 //===----------------------------------------------------------------------===//
00612 
00613 /// handleZeroArgFP - ST(0) = fld0    ST(0) = flds <mem>
00614 ///
00615 void FPS::handleZeroArgFP(MachineBasicBlock::iterator &I) {
00616   MachineInstr *MI = I;
00617   unsigned DestReg = getFPReg(MI->getOperand(0));
00618 
00619   // Change from the pseudo instruction to the concrete instruction.
00620   MI->RemoveOperand(0);   // Remove the explicit ST(0) operand
00621   MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
00622   
00623   // Result gets pushed on the stack.
00624   pushReg(DestReg);
00625 }
00626 
00627 /// handleOneArgFP - fst <mem>, ST(0)
00628 ///
00629 void FPS::handleOneArgFP(MachineBasicBlock::iterator &I) {
00630   MachineInstr *MI = I;
00631   unsigned NumOps = MI->getDesc().getNumOperands();
00632   assert((NumOps == X86AddrNumOperands + 1 || NumOps == 1) &&
00633          "Can only handle fst* & ftst instructions!");
00634 
00635   // Is this the last use of the source register?
00636   unsigned Reg = getFPReg(MI->getOperand(NumOps-1));
00637   bool KillsSrc = MI->killsRegister(X86::FP0+Reg);
00638 
00639   // FISTP64m is strange because there isn't a non-popping versions.
00640   // If we have one _and_ we don't want to pop the operand, duplicate the value
00641   // on the stack instead of moving it.  This ensure that popping the value is
00642   // always ok.
00643   // Ditto FISTTP16m, FISTTP32m, FISTTP64m, ST_FpP80m.
00644   //
00645   if (!KillsSrc &&
00646       (MI->getOpcode() == X86::IST_Fp64m32 ||
00647        MI->getOpcode() == X86::ISTT_Fp16m32 ||
00648        MI->getOpcode() == X86::ISTT_Fp32m32 ||
00649        MI->getOpcode() == X86::ISTT_Fp64m32 ||
00650        MI->getOpcode() == X86::IST_Fp64m64 ||
00651        MI->getOpcode() == X86::ISTT_Fp16m64 ||
00652        MI->getOpcode() == X86::ISTT_Fp32m64 ||
00653        MI->getOpcode() == X86::ISTT_Fp64m64 ||
00654        MI->getOpcode() == X86::IST_Fp64m80 ||
00655        MI->getOpcode() == X86::ISTT_Fp16m80 ||
00656        MI->getOpcode() == X86::ISTT_Fp32m80 ||
00657        MI->getOpcode() == X86::ISTT_Fp64m80 ||
00658        MI->getOpcode() == X86::ST_FpP80m)) {
00659     duplicateToTop(Reg, 7 /*temp register*/, I);
00660   } else {
00661     moveToTop(Reg, I);            // Move to the top of the stack...
00662   }
00663   
00664   // Convert from the pseudo instruction to the concrete instruction.
00665   MI->RemoveOperand(NumOps-1);    // Remove explicit ST(0) operand
00666   MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
00667 
00668   if (MI->getOpcode() == X86::IST_FP64m ||
00669       MI->getOpcode() == X86::ISTT_FP16m ||
00670       MI->getOpcode() == X86::ISTT_FP32m ||
00671       MI->getOpcode() == X86::ISTT_FP64m ||
00672       MI->getOpcode() == X86::ST_FP80m) {
00673     assert(StackTop > 0 && "Stack empty??");
00674     --StackTop;
00675   } else if (KillsSrc) { // Last use of operand?
00676     popStackAfter(I);
00677   }
00678 }
00679 
00680 
00681 /// handleOneArgFPRW: Handle instructions that read from the top of stack and
00682 /// replace the value with a newly computed value.  These instructions may have
00683 /// non-fp operands after their FP operands.
00684 ///
00685 ///  Examples:
00686 ///     R1 = fchs R2
00687 ///     R1 = fadd R2, [mem]
00688 ///
00689 void FPS::handleOneArgFPRW(MachineBasicBlock::iterator &I) {
00690   MachineInstr *MI = I;
00691 #ifndef NDEBUG
00692   unsigned NumOps = MI->getDesc().getNumOperands();
00693   assert(NumOps >= 2 && "FPRW instructions must have 2 ops!!");
00694 #endif
00695 
00696   // Is this the last use of the source register?
00697   unsigned Reg = getFPReg(MI->getOperand(1));
00698   bool KillsSrc = MI->killsRegister(X86::FP0+Reg);
00699 
00700   if (KillsSrc) {
00701     // If this is the last use of the source register, just make sure it's on
00702     // the top of the stack.
00703     moveToTop(Reg, I);
00704     assert(StackTop > 0 && "Stack cannot be empty!");
00705     --StackTop;
00706     pushReg(getFPReg(MI->getOperand(0)));
00707   } else {
00708     // If this is not the last use of the source register, _copy_ it to the top
00709     // of the stack.
00710     duplicateToTop(Reg, getFPReg(MI->getOperand(0)), I);
00711   }
00712 
00713   // Change from the pseudo instruction to the concrete instruction.
00714   MI->RemoveOperand(1);   // Drop the source operand.
00715   MI->RemoveOperand(0);   // Drop the destination operand.
00716   MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
00717 }
00718 
00719 
00720 //===----------------------------------------------------------------------===//
00721 // Define tables of various ways to map pseudo instructions
00722 //
00723 
00724 // ForwardST0Table - Map: A = B op C  into: ST(0) = ST(0) op ST(i)
00725 static const TableEntry ForwardST0Table[] = {
00726   { X86::ADD_Fp32  , X86::ADD_FST0r },
00727   { X86::ADD_Fp64  , X86::ADD_FST0r },
00728   { X86::ADD_Fp80  , X86::ADD_FST0r },
00729   { X86::DIV_Fp32  , X86::DIV_FST0r },
00730   { X86::DIV_Fp64  , X86::DIV_FST0r },
00731   { X86::DIV_Fp80  , X86::DIV_FST0r },
00732   { X86::MUL_Fp32  , X86::MUL_FST0r },
00733   { X86::MUL_Fp64  , X86::MUL_FST0r },
00734   { X86::MUL_Fp80  , X86::MUL_FST0r },
00735   { X86::SUB_Fp32  , X86::SUB_FST0r },
00736   { X86::SUB_Fp64  , X86::SUB_FST0r },
00737   { X86::SUB_Fp80  , X86::SUB_FST0r },
00738 };
00739 
00740 // ReverseST0Table - Map: A = B op C  into: ST(0) = ST(i) op ST(0)
00741 static const TableEntry ReverseST0Table[] = {
00742   { X86::ADD_Fp32  , X86::ADD_FST0r  },   // commutative
00743   { X86::ADD_Fp64  , X86::ADD_FST0r  },   // commutative
00744   { X86::ADD_Fp80  , X86::ADD_FST0r  },   // commutative
00745   { X86::DIV_Fp32  , X86::DIVR_FST0r },
00746   { X86::DIV_Fp64  , X86::DIVR_FST0r },
00747   { X86::DIV_Fp80  , X86::DIVR_FST0r },
00748   { X86::MUL_Fp32  , X86::MUL_FST0r  },   // commutative
00749   { X86::MUL_Fp64  , X86::MUL_FST0r  },   // commutative
00750   { X86::MUL_Fp80  , X86::MUL_FST0r  },   // commutative
00751   { X86::SUB_Fp32  , X86::SUBR_FST0r },
00752   { X86::SUB_Fp64  , X86::SUBR_FST0r },
00753   { X86::SUB_Fp80  , X86::SUBR_FST0r },
00754 };
00755 
00756 // ForwardSTiTable - Map: A = B op C  into: ST(i) = ST(0) op ST(i)
00757 static const TableEntry ForwardSTiTable[] = {
00758   { X86::ADD_Fp32  , X86::ADD_FrST0  },   // commutative
00759   { X86::ADD_Fp64  , X86::ADD_FrST0  },   // commutative
00760   { X86::ADD_Fp80  , X86::ADD_FrST0  },   // commutative
00761   { X86::DIV_Fp32  , X86::DIVR_FrST0 },
00762   { X86::DIV_Fp64  , X86::DIVR_FrST0 },
00763   { X86::DIV_Fp80  , X86::DIVR_FrST0 },
00764   { X86::MUL_Fp32  , X86::MUL_FrST0  },   // commutative
00765   { X86::MUL_Fp64  , X86::MUL_FrST0  },   // commutative
00766   { X86::MUL_Fp80  , X86::MUL_FrST0  },   // commutative
00767   { X86::SUB_Fp32  , X86::SUBR_FrST0 },
00768   { X86::SUB_Fp64  , X86::SUBR_FrST0 },
00769   { X86::SUB_Fp80  , X86::SUBR_FrST0 },
00770 };
00771 
00772 // ReverseSTiTable - Map: A = B op C  into: ST(i) = ST(i) op ST(0)
00773 static const TableEntry ReverseSTiTable[] = {
00774   { X86::ADD_Fp32  , X86::ADD_FrST0 },
00775   { X86::ADD_Fp64  , X86::ADD_FrST0 },
00776   { X86::ADD_Fp80  , X86::ADD_FrST0 },
00777   { X86::DIV_Fp32  , X86::DIV_FrST0 },
00778   { X86::DIV_Fp64  , X86::DIV_FrST0 },
00779   { X86::DIV_Fp80  , X86::DIV_FrST0 },
00780   { X86::MUL_Fp32  , X86::MUL_FrST0 },
00781   { X86::MUL_Fp64  , X86::MUL_FrST0 },
00782   { X86::MUL_Fp80  , X86::MUL_FrST0 },
00783   { X86::SUB_Fp32  , X86::SUB_FrST0 },
00784   { X86::SUB_Fp64  , X86::SUB_FrST0 },
00785   { X86::SUB_Fp80  , X86::SUB_FrST0 },
00786 };
00787 
00788 
00789 /// handleTwoArgFP - Handle instructions like FADD and friends which are virtual
00790 /// instructions which need to be simplified and possibly transformed.
00791 ///
00792 /// Result: ST(0) = fsub  ST(0), ST(i)
00793 ///         ST(i) = fsub  ST(0), ST(i)
00794 ///         ST(0) = fsubr ST(0), ST(i)
00795 ///         ST(i) = fsubr ST(0), ST(i)
00796 ///
00797 void FPS::handleTwoArgFP(MachineBasicBlock::iterator &I) {
00798   ASSERT_SORTED(ForwardST0Table); ASSERT_SORTED(ReverseST0Table);
00799   ASSERT_SORTED(ForwardSTiTable); ASSERT_SORTED(ReverseSTiTable);
00800   MachineInstr *MI = I;
00801 
00802   unsigned NumOperands = MI->getDesc().getNumOperands();
00803   assert(NumOperands == 3 && "Illegal TwoArgFP instruction!");
00804   unsigned Dest = getFPReg(MI->getOperand(0));
00805   unsigned Op0 = getFPReg(MI->getOperand(NumOperands-2));
00806   unsigned Op1 = getFPReg(MI->getOperand(NumOperands-1));
00807   bool KillsOp0 = MI->killsRegister(X86::FP0+Op0);
00808   bool KillsOp1 = MI->killsRegister(X86::FP0+Op1);
00809   DebugLoc dl = MI->getDebugLoc();
00810 
00811   unsigned TOS = getStackEntry(0);
00812 
00813   // One of our operands must be on the top of the stack.  If neither is yet, we
00814   // need to move one.
00815   if (Op0 != TOS && Op1 != TOS) {   // No operand at TOS?
00816     // We can choose to move either operand to the top of the stack.  If one of
00817     // the operands is killed by this instruction, we want that one so that we
00818     // can update right on top of the old version.
00819     if (KillsOp0) {
00820       moveToTop(Op0, I);         // Move dead operand to TOS.
00821       TOS = Op0;
00822     } else if (KillsOp1) {
00823       moveToTop(Op1, I);
00824       TOS = Op1;
00825     } else {
00826       // All of the operands are live after this instruction executes, so we
00827       // cannot update on top of any operand.  Because of this, we must
00828       // duplicate one of the stack elements to the top.  It doesn't matter
00829       // which one we pick.
00830       //
00831       duplicateToTop(Op0, Dest, I);
00832       Op0 = TOS = Dest;
00833       KillsOp0 = true;
00834     }
00835   } else if (!KillsOp0 && !KillsOp1) {
00836     // If we DO have one of our operands at the top of the stack, but we don't
00837     // have a dead operand, we must duplicate one of the operands to a new slot
00838     // on the stack.
00839     duplicateToTop(Op0, Dest, I);
00840     Op0 = TOS = Dest;
00841     KillsOp0 = true;
00842   }
00843 
00844   // Now we know that one of our operands is on the top of the stack, and at
00845   // least one of our operands is killed by this instruction.
00846   assert((TOS == Op0 || TOS == Op1) && (KillsOp0 || KillsOp1) &&
00847          "Stack conditions not set up right!");
00848 
00849   // We decide which form to use based on what is on the top of the stack, and
00850   // which operand is killed by this instruction.
00851   const TableEntry *InstTable;
00852   bool isForward = TOS == Op0;
00853   bool updateST0 = (TOS == Op0 && !KillsOp1) || (TOS == Op1 && !KillsOp0);
00854   if (updateST0) {
00855     if (isForward)
00856       InstTable = ForwardST0Table;
00857     else
00858       InstTable = ReverseST0Table;
00859   } else {
00860     if (isForward)
00861       InstTable = ForwardSTiTable;
00862     else
00863       InstTable = ReverseSTiTable;
00864   }
00865 
00866   int Opcode = Lookup(InstTable, array_lengthof(ForwardST0Table),
00867                       MI->getOpcode());
00868   assert(Opcode != -1 && "Unknown TwoArgFP pseudo instruction!");
00869 
00870   // NotTOS - The register which is not on the top of stack...
00871   unsigned NotTOS = (TOS == Op0) ? Op1 : Op0;
00872 
00873   // Replace the old instruction with a new instruction
00874   MBB->remove(I++);
00875   I = BuildMI(*MBB, I, dl, TII->get(Opcode)).addReg(getSTReg(NotTOS));
00876 
00877   // If both operands are killed, pop one off of the stack in addition to
00878   // overwriting the other one.
00879   if (KillsOp0 && KillsOp1 && Op0 != Op1) {
00880     assert(!updateST0 && "Should have updated other operand!");
00881     popStackAfter(I);   // Pop the top of stack
00882   }
00883 
00884   // Update stack information so that we know the destination register is now on
00885   // the stack.
00886   unsigned UpdatedSlot = getSlot(updateST0 ? TOS : NotTOS);
00887   assert(UpdatedSlot < StackTop && Dest < 7);
00888   Stack[UpdatedSlot]   = Dest;
00889   RegMap[Dest]         = UpdatedSlot;
00890   MBB->getParent()->DeleteMachineInstr(MI); // Remove the old instruction
00891 }
00892 
00893 /// handleCompareFP - Handle FUCOM and FUCOMI instructions, which have two FP
00894 /// register arguments and no explicit destinations.
00895 ///
00896 void FPS::handleCompareFP(MachineBasicBlock::iterator &I) {
00897   ASSERT_SORTED(ForwardST0Table); ASSERT_SORTED(ReverseST0Table);
00898   ASSERT_SORTED(ForwardSTiTable); ASSERT_SORTED(ReverseSTiTable);
00899   MachineInstr *MI = I;
00900 
00901   unsigned NumOperands = MI->getDesc().getNumOperands();
00902   assert(NumOperands == 2 && "Illegal FUCOM* instruction!");
00903   unsigned Op0 = getFPReg(MI->getOperand(NumOperands-2));
00904   unsigned Op1 = getFPReg(MI->getOperand(NumOperands-1));
00905   bool KillsOp0 = MI->killsRegister(X86::FP0+Op0);
00906   bool KillsOp1 = MI->killsRegister(X86::FP0+Op1);
00907 
00908   // Make sure the first operand is on the top of stack, the other one can be
00909   // anywhere.
00910   moveToTop(Op0, I);
00911 
00912   // Change from the pseudo instruction to the concrete instruction.
00913   MI->getOperand(0).setReg(getSTReg(Op1));
00914   MI->RemoveOperand(1);
00915   MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
00916 
00917   // If any of the operands are killed by this instruction, free them.
00918   if (KillsOp0) freeStackSlotAfter(I, Op0);
00919   if (KillsOp1 && Op0 != Op1) freeStackSlotAfter(I, Op1);
00920 }
00921 
00922 /// handleCondMovFP - Handle two address conditional move instructions.  These
00923 /// instructions move a st(i) register to st(0) iff a condition is true.  These
00924 /// instructions require that the first operand is at the top of the stack, but
00925 /// otherwise don't modify the stack at all.
00926 void FPS::handleCondMovFP(MachineBasicBlock::iterator &I) {
00927   MachineInstr *MI = I;
00928 
00929   unsigned Op0 = getFPReg(MI->getOperand(0));
00930   unsigned Op1 = getFPReg(MI->getOperand(2));
00931   bool KillsOp1 = MI->killsRegister(X86::FP0+Op1);
00932 
00933   // The first operand *must* be on the top of the stack.
00934   moveToTop(Op0, I);
00935 
00936   // Change the second operand to the stack register that the operand is in.
00937   // Change from the pseudo instruction to the concrete instruction.
00938   MI->RemoveOperand(0);
00939   MI->RemoveOperand(1);
00940   MI->getOperand(0).setReg(getSTReg(Op1));
00941   MI->setDesc(TII->get(getConcreteOpcode(MI->getOpcode())));
00942   
00943   // If we kill the second operand, make sure to pop it from the stack.
00944   if (Op0 != Op1 && KillsOp1) {
00945     // Get this value off of the register stack.
00946     freeStackSlotAfter(I, Op1);
00947   }
00948 }
00949 
00950 
00951 /// handleSpecialFP - Handle special instructions which behave unlike other
00952 /// floating point instructions.  This is primarily intended for use by pseudo
00953 /// instructions.
00954 ///
00955 void FPS::handleSpecialFP(MachineBasicBlock::iterator &I) {
00956   MachineInstr *MI = I;
00957   DebugLoc dl = MI->getDebugLoc();
00958   switch (MI->getOpcode()) {
00959   default: llvm_unreachable("Unknown SpecialFP instruction!");
00960   case X86::FpGET_ST0_32:// Appears immediately after a call returning FP type!
00961   case X86::FpGET_ST0_64:// Appears immediately after a call returning FP type!
00962   case X86::FpGET_ST0_80:// Appears immediately after a call returning FP type!
00963     assert(StackTop == 0 && "Stack should be empty after a call!");
00964     pushReg(getFPReg(MI->getOperand(0)));
00965     break;
00966   case X86::FpGET_ST1_32:// Appears immediately after a call returning FP type!
00967   case X86::FpGET_ST1_64:// Appears immediately after a call returning FP type!
00968   case X86::FpGET_ST1_80:{// Appears immediately after a call returning FP type!
00969     // FpGET_ST1 should occur right after a FpGET_ST0 for a call or inline asm.
00970     // The pattern we expect is:
00971     //  CALL
00972     //  FP1 = FpGET_ST0
00973     //  FP4 = FpGET_ST1
00974     //
00975     // At this point, we've pushed FP1 on the top of stack, so it should be
00976     // present if it isn't dead.  If it was dead, we already emitted a pop to
00977     // remove it from the stack and StackTop = 0.
00978     
00979     // Push FP4 as top of stack next.
00980     pushReg(getFPReg(MI->getOperand(0)));
00981 
00982     // If StackTop was 0 before we pushed our operand, then ST(0) must have been
00983     // dead.  In this case, the ST(1) value is the only thing that is live, so
00984     // it should be on the TOS (after the pop that was emitted) and is.  Just
00985     // continue in this case.
00986     if (StackTop == 1)
00987       break;
00988     
00989     // Because pushReg just pushed ST(1) as TOS, we now have to swap the two top
00990     // elements so that our accounting is correct.
00991     unsigned RegOnTop = getStackEntry(0);
00992     unsigned RegNo = getStackEntry(1);
00993     
00994     // Swap the slots the regs are in.
00995     std::swap(RegMap[RegNo], RegMap[RegOnTop]);
00996     
00997     // Swap stack slot contents.
00998     assert(RegMap[RegOnTop] < StackTop);
00999     std::swap(Stack[RegMap[RegOnTop]], Stack[StackTop-1]);
01000     break;
01001   }
01002   case X86::FpSET_ST0_32:
01003   case X86::FpSET_ST0_64:
01004   case X86::FpSET_ST0_80: {
01005     unsigned Op0 = getFPReg(MI->getOperand(0));
01006 
01007     // FpSET_ST0_80 is generated by copyRegToReg for both function return
01008     // and inline assembly with the "st" constrain. In the latter case,
01009     // it is possible for ST(0) to be alive after this instruction.
01010     if (!MI->killsRegister(X86::FP0 + Op0)) {
01011       // Duplicate Op0
01012       duplicateToTop(0, 7 /*temp register*/, I);
01013     } else {
01014       moveToTop(Op0, I);
01015     }
01016     --StackTop;   // "Forget" we have something on the top of stack!
01017     break;
01018   }
01019   case X86::FpSET_ST1_32:
01020   case X86::FpSET_ST1_64:
01021   case X86::FpSET_ST1_80:
01022     // StackTop can be 1 if a FpSET_ST0_* was before this. Exchange them.
01023     if (StackTop == 1) {
01024       BuildMI(*MBB, I, dl, TII->get(X86::XCH_F)).addReg(X86::ST1);
01025       NumFXCH++;
01026       StackTop = 0;
01027       break;
01028     }
01029     assert(StackTop == 2 && "Stack should have two element on it to return!");
01030     --StackTop;   // "Forget" we have something on the top of stack!
01031     break;
01032   case X86::MOV_Fp3232:
01033   case X86::MOV_Fp3264:
01034   case X86::MOV_Fp6432:
01035   case X86::MOV_Fp6464: 
01036   case X86::MOV_Fp3280:
01037   case X86::MOV_Fp6480:
01038   case X86::MOV_Fp8032:
01039   case X86::MOV_Fp8064: 
01040   case X86::MOV_Fp8080: {
01041     const MachineOperand &MO1 = MI->getOperand(1);
01042     unsigned SrcReg = getFPReg(MO1);
01043 
01044     const MachineOperand &MO0 = MI->getOperand(0);
01045     // These can be created due to inline asm. Two address pass can introduce
01046     // copies from RFP registers to virtual registers.
01047     if (MO0.getReg() == X86::ST0 && SrcReg == 0) {
01048       assert(MO1.isKill());
01049       // Treat %ST0<def> = MOV_Fp8080 %FP0<kill>
01050       // like  FpSET_ST0_80 %FP0<kill>, %ST0<imp-def>
01051       assert((StackTop == 1 || StackTop == 2)
01052              && "Stack should have one or two element on it to return!");
01053       --StackTop;   // "Forget" we have something on the top of stack!
01054       break;
01055     } else if (MO0.getReg() == X86::ST1 && SrcReg == 1) {
01056       assert(MO1.isKill());
01057       // Treat %ST1<def> = MOV_Fp8080 %FP1<kill>
01058       // like  FpSET_ST1_80 %FP0<kill>, %ST1<imp-def>
01059       // StackTop can be 1 if a FpSET_ST0_* was before this. Exchange them.
01060       if (StackTop == 1) {
01061         BuildMI(*MBB, I, dl, TII->get(X86::XCH_F)).addReg(X86::ST1);
01062         NumFXCH++;
01063         StackTop = 0;
01064         break;
01065       }
01066       assert(StackTop == 2 && "Stack should have two element on it to return!");
01067       --StackTop;   // "Forget" we have something on the top of stack!
01068       break;
01069     }
01070 
01071     unsigned DestReg = getFPReg(MO0);
01072     if (MI->killsRegister(X86::FP0+SrcReg)) {
01073       // If the input operand is killed, we can just change the owner of the
01074       // incoming stack slot into the result.
01075       unsigned Slot = getSlot(SrcReg);
01076       assert(Slot < 7 && DestReg < 7 && "FpMOV operands invalid!");
01077       Stack[Slot] = DestReg;
01078       RegMap[DestReg] = Slot;
01079 
01080     } else {
01081       // For FMOV we just duplicate the specified value to a new stack slot.
01082       // This could be made better, but would require substantial changes.
01083       duplicateToTop(SrcReg, DestReg, I);
01084     }
01085     }
01086     break;
01087   case TargetInstrInfo::INLINEASM: {
01088     // The inline asm MachineInstr currently only *uses* FP registers for the
01089     // 'f' constraint.  These should be turned into the current ST(x) register
01090     // in the machine instr.  Also, any kills should be explicitly popped after
01091     // the inline asm.
01092     unsigned Kills[7];
01093     unsigned NumKills = 0;
01094     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
01095       MachineOperand &Op = MI->getOperand(i);
01096       if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6)
01097         continue;
01098       assert(Op.isUse() && "Only handle inline asm uses right now");
01099       
01100       unsigned FPReg = getFPReg(Op);
01101       Op.setReg(getSTReg(FPReg));
01102       
01103       // If we kill this operand, make sure to pop it from the stack after the
01104       // asm.  We just remember it for now, and pop them all off at the end in
01105       // a batch.
01106       if (Op.isKill())
01107         Kills[NumKills++] = FPReg;
01108     }
01109 
01110     // If this asm kills any FP registers (is the last use of them) we must
01111     // explicitly emit pop instructions for them.  Do this now after the asm has
01112     // executed so that the ST(x) numbers are not off (which would happen if we
01113     // did this inline with operand rewriting).
01114     //
01115     // Note: this might be a non-optimal pop sequence.  We might be able to do
01116     // better by trying to pop in stack order or something.
01117     MachineBasicBlock::iterator InsertPt = MI;
01118     while (NumKills)
01119       freeStackSlotAfter(InsertPt, Kills[--NumKills]);
01120 
01121     // Don't delete the inline asm!
01122     return;
01123   }
01124       
01125   case X86::RET:
01126   case X86::RETI:
01127     // If RET has an FP register use operand, pass the first one in ST(0) and
01128     // the second one in ST(1).
01129     if (isStackEmpty()) return;  // Quick check to see if any are possible.
01130     
01131     // Find the register operands.
01132     unsigned FirstFPRegOp = ~0U, SecondFPRegOp = ~0U;
01133     
01134     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
01135       MachineOperand &Op = MI->getOperand(i);
01136       if (!Op.isReg() || Op.getReg() < X86::FP0 || Op.getReg() > X86::FP6)
01137         continue;
01138       // FP Register uses must be kills unless there are two uses of the same
01139       // register, in which case only one will be a kill.
01140       assert(Op.isUse() &&
01141              (Op.isKill() ||                        // Marked kill.
01142               getFPReg(Op) == FirstFPRegOp ||       // Second instance.
01143               MI->killsRegister(Op.getReg())) &&    // Later use is marked kill.
01144              "Ret only defs operands, and values aren't live beyond it");
01145 
01146       if (FirstFPRegOp == ~0U)
01147         FirstFPRegOp = getFPReg(Op);
01148       else {
01149         assert(SecondFPRegOp == ~0U && "More than two fp operands!");
01150         SecondFPRegOp = getFPReg(Op);
01151       }
01152 
01153       // Remove the operand so that later passes don't see it.
01154       MI->RemoveOperand(i);
01155       --i, --e;
01156     }
01157     
01158     // There are only four possibilities here:
01159     // 1) we are returning a single FP value.  In this case, it has to be in
01160     //    ST(0) already, so just declare success by removing the value from the
01161     //    FP Stack.
01162     if (SecondFPRegOp == ~0U) {
01163       // Assert that the top of stack contains the right FP register.
01164       assert(StackTop == 1 && FirstFPRegOp == getStackEntry(0) &&
01165              "Top of stack not the right register for RET!");
01166       
01167       // Ok, everything is good, mark the value as not being on the stack
01168       // anymore so that our assertion about the stack being empty at end of
01169       // block doesn't fire.
01170       StackTop = 0;
01171       return;
01172     }
01173     
01174     // Otherwise, we are returning two values:
01175     // 2) If returning the same value for both, we only have one thing in the FP
01176     //    stack.  Consider:  RET FP1, FP1
01177     if (StackTop == 1) {
01178       assert(FirstFPRegOp == SecondFPRegOp && FirstFPRegOp == getStackEntry(0)&&
01179              "Stack misconfiguration for RET!");
01180       
01181       // Duplicate the TOS so that we return it twice.  Just pick some other FPx
01182       // register to hold it.
01183       unsigned NewReg = (FirstFPRegOp+1)%7;
01184       duplicateToTop(FirstFPRegOp, NewReg, MI);
01185       FirstFPRegOp = NewReg;
01186     }
01187     
01188     /// Okay we know we have two different FPx operands now:
01189     assert(StackTop == 2 && "Must have two values live!");
01190     
01191     /// 3) If SecondFPRegOp is currently in ST(0) and FirstFPRegOp is currently
01192     ///    in ST(1).  In this case, emit an fxch.
01193     if (getStackEntry(0) == SecondFPRegOp) {
01194       assert(getStackEntry(1) == FirstFPRegOp && "Unknown regs live");
01195       moveToTop(FirstFPRegOp, MI);
01196     }
01197     
01198     /// 4) Finally, FirstFPRegOp must be in ST(0) and SecondFPRegOp must be in
01199     /// ST(1).  Just remove both from our understanding of the stack and return.
01200     assert(getStackEntry(0) == FirstFPRegOp && "Unknown regs live");
01201     assert(getStackEntry(1) == SecondFPRegOp && "Unknown regs live");
01202     StackTop = 0;
01203     return;
01204   }
01205 
01206   I = MBB->erase(I);  // Remove the pseudo instruction
01207   --I;
01208 }