If we're about to emit something like:
[oota-llvm.git] / lib / Target / Sparc / FPMover.cpp
1 //===-- FPMover.cpp - SparcV8 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 // Turns FpMOVD instructions into FMOVS pairs after regalloc.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "SparcV8.h"
15 #include "llvm/CodeGen/MachineFunctionPass.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/Support/Debug.h"
19
20 using namespace llvm;
21
22 namespace {
23   Statistic<> NumFpMOVDs ("fpmover", "# FpMOVD instructions translated");
24   Statistic<> SkippedFpMOVDs ("fpmover", "# FpMOVD instructions skipped");
25
26   struct FPMover : public MachineFunctionPass {
27     /// Target machine description which we query for reg. names, data
28     /// layout, etc.
29     ///
30     TargetMachine &TM;
31
32     FPMover (TargetMachine &tm) : TM (tm) { }
33
34     virtual const char *getPassName () const {
35       return "SparcV8 Double-FP Move Fixer";
36     }
37
38     bool runOnMachineBasicBlock (MachineBasicBlock &MBB);
39     bool runOnMachineFunction (MachineFunction &F) {
40       bool Changed = false;
41       for (MachineFunction::iterator FI = F.begin (), FE = F.end ();
42            FI != FE; ++FI)
43         Changed |= runOnMachineBasicBlock (*FI);
44       return Changed;
45     }
46
47   };
48 } // end of anonymous namespace
49
50 /// createSparcV8FPMoverPass - Returns a pass that turns FpMOVD
51 /// instructions into FMOVS instructions
52 ///
53 FunctionPass *llvm::createSparcV8FPMoverPass (TargetMachine &tm) {
54   return new FPMover (tm);
55 }
56
57 static void doubleToSingleRegPair(unsigned doubleReg, unsigned &singleReg1, unsigned &singleReg2) {
58   const unsigned EvenHalvesOfPairs[] = { V8::F0, V8::F2, V8::F4, V8::F6, V8::F8, V8::F10, V8::F12, V8::F14, V8::F16, V8::F18, V8::F20, V8::F22, V8::F24, V8::F26, V8::F28, V8::F30 };
59   const unsigned OddHalvesOfPairs[] = { V8::F1, V8::F3, V8::F5, V8::F7, V8::F9, V8::F11, V8::F13, V8::F15, V8::F17, V8::F19, V8::F21, V8::F23, V8::F25, V8::F27, V8::F29, V8::F31 };
60   const unsigned DoubleRegsInOrder[] = { V8::D0, V8::D1, V8::D2, V8::D3, V8::D4, V8::D5, V8::D6, V8::D7, V8::D8, V8::D9, V8::D10, V8::D11, V8::D12, V8::D13, V8::D14, V8::D15 };
61   for (unsigned i = 0; i < sizeof(DoubleRegsInOrder)/sizeof(unsigned); ++i)
62     if (DoubleRegsInOrder[i] == doubleReg) {
63       singleReg1 = EvenHalvesOfPairs[i];
64       singleReg2 = OddHalvesOfPairs[i];
65       return;
66     }
67   assert (0 && "Can't find reg");
68 }
69
70 /// runOnMachineBasicBlock - Fixup FpMOVD instructions in this MBB.
71 ///
72 bool FPMover::runOnMachineBasicBlock (MachineBasicBlock &MBB) {
73   bool Changed = false;
74   for (MachineBasicBlock::iterator I = MBB.begin (); I != MBB.end (); ++I)
75     if (V8::FpMOVD == I->getOpcode ()) {
76       unsigned NewSrcReg0, NewSrcReg1, NewDestReg0, NewDestReg1;
77       doubleToSingleRegPair (I->getOperand (0).getReg (), NewDestReg0,
78                              NewDestReg1);
79       doubleToSingleRegPair (I->getOperand (1).getReg (), NewSrcReg0,
80                              NewSrcReg1);
81       MachineBasicBlock::iterator J = I;
82       ++J;
83       if (!(NewDestReg0 == NewSrcReg0 && NewDestReg1 == NewSrcReg1)) {
84         I->setOpcode (V8::FMOVS);
85         I->SetMachineOperandReg (0, NewDestReg0);
86         I->SetMachineOperandReg (1, NewSrcReg0);
87         DEBUG (std::cerr << "FPMover: new dest reg. is: " << NewDestReg0
88                          << "; modified instr is: " << *I);
89         // Insert copy for the other half of the double:
90         MachineInstr *MI2 =
91           BuildMI (MBB, J, V8::FMOVS, 1, NewDestReg1).addReg (NewSrcReg1);
92         DEBUG (std::cerr << "FPMover: new dest reg. is " << NewDestReg1
93                          << "; inserted instr is: " << *MI2);
94         ++NumFpMOVDs;
95       } else {
96         MBB.erase (I);
97         ++SkippedFpMOVDs;
98       }
99       I = J;
100       Changed = true;
101     }
102   return Changed;
103 }