What should be the last unnecessary <iostream>s in the library.
[oota-llvm.git] / lib / Target / Sparc / FPMover.cpp
1 //===-- FPMover.cpp - Sparc double-precision floating point move fixer ----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Expand FpMOVD/FpABSD/FpNEGD instructions into their single-precision pieces.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Sparc.h"
15 #include "SparcSubtarget.h"
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/CodeGen/MachineInstrBuilder.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Support/Debug.h"
22 using namespace llvm;
23
24 namespace {
25   Statistic NumFpDs("fpmover", "Number of instructions translated");
26   Statistic NoopFpDs("fpmover", "Number of noop instructions removed");
27
28   struct FPMover : public MachineFunctionPass {
29     /// Target machine description which we query for reg. names, data
30     /// layout, etc.
31     ///
32     TargetMachine &TM;
33
34     FPMover(TargetMachine &tm) : TM(tm) { }
35
36     virtual const char *getPassName() const {
37       return "Sparc Double-FP Move Fixer";
38     }
39
40     bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
41     bool runOnMachineFunction(MachineFunction &F);
42   };
43 } // end of anonymous namespace
44
45 /// createSparcFPMoverPass - Returns a pass that turns FpMOVD
46 /// instructions into FMOVS instructions
47 ///
48 FunctionPass *llvm::createSparcFPMoverPass(TargetMachine &tm) {
49   return new FPMover(tm);
50 }
51
52 /// getDoubleRegPair - Given a DFP register, return the even and odd FP
53 /// registers that correspond to it.
54 static void getDoubleRegPair(unsigned DoubleReg, unsigned &EvenReg,
55                              unsigned &OddReg) {
56   static const unsigned EvenHalvesOfPairs[] = {
57     SP::F0, SP::F2, SP::F4, SP::F6, SP::F8, SP::F10, SP::F12, SP::F14,
58     SP::F16, SP::F18, SP::F20, SP::F22, SP::F24, SP::F26, SP::F28, SP::F30
59   };
60   static const unsigned OddHalvesOfPairs[] = {
61     SP::F1, SP::F3, SP::F5, SP::F7, SP::F9, SP::F11, SP::F13, SP::F15,
62     SP::F17, SP::F19, SP::F21, SP::F23, SP::F25, SP::F27, SP::F29, SP::F31
63   };
64   static const unsigned DoubleRegsInOrder[] = {
65     SP::D0, SP::D1, SP::D2, SP::D3, SP::D4, SP::D5, SP::D6, SP::D7, SP::D8,
66     SP::D9, SP::D10, SP::D11, SP::D12, SP::D13, SP::D14, SP::D15
67   };
68   for (unsigned i = 0; i < sizeof(DoubleRegsInOrder)/sizeof(unsigned); ++i)
69     if (DoubleRegsInOrder[i] == DoubleReg) {
70       EvenReg = EvenHalvesOfPairs[i];
71       OddReg = OddHalvesOfPairs[i];
72       return;
73     }
74   assert(0 && "Can't find reg");
75 }
76
77 /// runOnMachineBasicBlock - Fixup FpMOVD instructions in this MBB.
78 ///
79 bool FPMover::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
80   bool Changed = false;
81   for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {
82     MachineInstr *MI = I++;
83     if (MI->getOpcode() == SP::FpMOVD || MI->getOpcode() == SP::FpABSD ||
84         MI->getOpcode() == SP::FpNEGD) {
85       Changed = true;
86       unsigned DestDReg = MI->getOperand(0).getReg();
87       unsigned SrcDReg  = MI->getOperand(1).getReg();
88       if (DestDReg == SrcDReg && MI->getOpcode() == SP::FpMOVD) {
89         MBB.erase(MI);   // Eliminate the noop copy.
90         ++NoopFpDs;
91         continue;
92       }
93       
94       unsigned EvenSrcReg = 0, OddSrcReg = 0, EvenDestReg = 0, OddDestReg = 0;
95       getDoubleRegPair(DestDReg, EvenDestReg, OddDestReg);
96       getDoubleRegPair(SrcDReg, EvenSrcReg, OddSrcReg);
97
98       const TargetInstrInfo *TII = TM.getInstrInfo();
99       if (MI->getOpcode() == SP::FpMOVD)
100         MI->setInstrDescriptor(TII->get(SP::FMOVS));
101       else if (MI->getOpcode() == SP::FpNEGD)
102         MI->setInstrDescriptor(TII->get(SP::FNEGS));
103       else if (MI->getOpcode() == SP::FpABSD)
104         MI->setInstrDescriptor(TII->get(SP::FABSS));
105       else
106         assert(0 && "Unknown opcode!");
107         
108       MI->getOperand(0).setReg(EvenDestReg);
109       MI->getOperand(1).setReg(EvenSrcReg);
110       DOUT << "FPMover: the modified instr is: " << *MI;
111       // Insert copy for the other half of the double.
112       if (DestDReg != SrcDReg) {
113         MI = BuildMI(MBB, I, TM.getInstrInfo()->get(SP::FMOVS), OddDestReg)
114           .addReg(OddSrcReg);
115         DOUT << "FPMover: the inserted instr is: " << *MI;
116       }
117       ++NumFpDs;
118     }
119   }
120   return Changed;
121 }
122
123 bool FPMover::runOnMachineFunction(MachineFunction &F) {
124   // If the target has V9 instructions, the fp-mover pseudos will never be
125   // emitted.  Avoid a scan of the instructions to improve compile time.
126   if (TM.getSubtarget<SparcSubtarget>().isV9())
127     return false;
128   
129   bool Changed = false;
130   for (MachineFunction::iterator FI = F.begin(), FE = F.end();
131        FI != FE; ++FI)
132     Changed |= runOnMachineBasicBlock(*FI);
133   return Changed;
134 }