Pass which converts FpMOVD (double move pseudoinstructions) to pairs
[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
19 using namespace llvm;
20
21 namespace {
22   Statistic<> NumFpMOVDs ("fpmover", "# FpMOVD instructions translated");
23
24   struct FPMover : public MachineFunctionPass {
25     /// Target machine description which we query for reg. names, data
26     /// layout, etc.
27     ///
28     TargetMachine &TM;
29
30     FPMover (TargetMachine &tm) : TM (tm) { }
31
32     virtual const char *getPassName () const {
33       return "SparcV8 Double-FP Move Fixer";
34     }
35
36     bool runOnMachineBasicBlock (MachineBasicBlock &MBB);
37     bool runOnMachineFunction (MachineFunction &F) {
38       bool Changed = false;
39       for (MachineFunction::iterator FI = F.begin (), FE = F.end ();
40            FI != FE; ++FI)
41         Changed |= runOnMachineBasicBlock (*FI);
42       return Changed;
43     }
44
45   };
46 } // end of anonymous namespace
47
48 /// createSparcV8FPMoverPass - Returns a pass that turns FpMOVD
49 /// instructions into FMOVS instructions
50 ///
51 FunctionPass *llvm::createSparcV8FPMoverPass (TargetMachine &tm) {
52   return new FPMover (tm);
53 }
54
55 static void doubleToSingleRegPair(unsigned doubleReg, unsigned &singleReg1, unsigned &singleReg2) {
56   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 };
57   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 };
58   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 };
59   for (unsigned i = 0; i < sizeof(DoubleRegsInOrder)/sizeof(unsigned); ++i)
60     if (DoubleRegsInOrder[i] == doubleReg) {
61       singleReg1 = EvenHalvesOfPairs[i];
62       singleReg2 = OddHalvesOfPairs[i];
63       return;
64     }
65   assert (0 && "Can't find reg");
66 }
67
68 /// runOnMachineBasicBlock - Fixup FpMOVD instructions in this MBB.
69 ///
70 bool FPMover::runOnMachineBasicBlock (MachineBasicBlock &MBB) {
71   bool Changed = false;
72   for (MachineBasicBlock::iterator I = MBB.begin (); I != MBB.end (); ++I)
73     if (V8::FpMOVD == I->getOpcode()) {
74       I->setOpcode (V8::FMOVS);
75       unsigned DestReg = I->getOperand(0).getReg();
76       unsigned SrcReg = I->getOperand(1).getReg();
77       unsigned NewSrcReg0, NewSrcReg1;
78       unsigned NewDestReg0, NewDestReg1;
79       doubleToSingleRegPair (DestReg, NewDestReg0, NewDestReg1);
80       doubleToSingleRegPair (SrcReg, NewSrcReg0, NewSrcReg1);
81       I->SetMachineOperandReg (0, NewDestReg0);
82       I->SetMachineOperandReg (1, NewSrcReg0);
83       // Insert copy for the other half of the double:
84       MachineBasicBlock::iterator J = I;
85       ++J;
86       BuildMI (MBB, J, V8::FMOVS, 1, NewDestReg1).addReg(NewSrcReg1);
87       ++NumFpMOVDs;
88       Changed = true;
89     }
90   return Changed;
91 }