feeadc52a799f33520cd695150aa65074dee9e91
[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 // Expand FpMOVD/FpABSD/FpNEGD instructions into their single-precision pieces.
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 using namespace llvm;
20
21 namespace {
22   Statistic<> NumFpDs("fpmover", "Number of instructions translated");
23   Statistic<> NoopFpDs("fpmover", "Number of noop instructions removed");
24
25   struct FPMover : public MachineFunctionPass {
26     /// Target machine description which we query for reg. names, data
27     /// layout, etc.
28     ///
29     TargetMachine &TM;
30
31     FPMover(TargetMachine &tm) : TM(tm) { }
32
33     virtual const char *getPassName() const {
34       return "SparcV8 Double-FP Move Fixer";
35     }
36
37     bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
38     bool runOnMachineFunction(MachineFunction &F) {
39       bool Changed = false;
40       for (MachineFunction::iterator FI = F.begin(), FE = F.end();
41            FI != FE; ++FI)
42         Changed |= runOnMachineBasicBlock(*FI);
43       return Changed;
44     }
45
46   };
47 } // end of anonymous namespace
48
49 /// createSparcV8FPMoverPass - Returns a pass that turns FpMOVD
50 /// instructions into FMOVS instructions
51 ///
52 FunctionPass *llvm::createSparcV8FPMoverPass(TargetMachine &tm) {
53   return new FPMover(tm);
54 }
55
56 /// getDoubleRegPair - Given a DFP register, return the even and odd FP
57 /// registers that correspond to it.
58 static void getDoubleRegPair(unsigned DoubleReg, unsigned &EvenReg,
59                              unsigned &OddReg) {
60   static const unsigned EvenHalvesOfPairs[] = {
61     V8::F0, V8::F2, V8::F4, V8::F6, V8::F8, V8::F10, V8::F12, V8::F14,
62     V8::F16, V8::F18, V8::F20, V8::F22, V8::F24, V8::F26, V8::F28, V8::F30
63   };
64   static const unsigned OddHalvesOfPairs[] = {
65     V8::F1, V8::F3, V8::F5, V8::F7, V8::F9, V8::F11, V8::F13, V8::F15,
66     V8::F17, V8::F19, V8::F21, V8::F23, V8::F25, V8::F27, V8::F29, V8::F31
67   };
68   static const unsigned DoubleRegsInOrder[] = {
69     V8::D0, V8::D1, V8::D2, V8::D3, V8::D4, V8::D5, V8::D6, V8::D7, V8::D8,
70     V8::D9, V8::D10, V8::D11, V8::D12, V8::D13, V8::D14, V8::D15
71   };
72   for (unsigned i = 0; i < sizeof(DoubleRegsInOrder)/sizeof(unsigned); ++i)
73     if (DoubleRegsInOrder[i] == DoubleReg) {
74       EvenReg = EvenHalvesOfPairs[i];
75       OddReg = OddHalvesOfPairs[i];
76       return;
77     }
78   assert(0 && "Can't find reg");
79 }
80
81 /// runOnMachineBasicBlock - Fixup FpMOVD instructions in this MBB.
82 ///
83 bool FPMover::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
84   bool Changed = false;
85   for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {
86     MachineInstr *MI = I++;
87     if (MI->getOpcode() == V8::FpMOVD) {
88       unsigned DestDReg = MI->getOperand(0).getReg();
89       unsigned SrcDReg  = MI->getOperand(1).getReg();
90       if (DestDReg != SrcDReg || MI->getOpcode() != V8::FpMOVD) {
91         unsigned EvenSrcReg = 0, OddSrcReg = 0, EvenDestReg = 0, OddDestReg = 0;
92         getDoubleRegPair(DestDReg, EvenDestReg, OddDestReg);
93         getDoubleRegPair(SrcDReg, EvenSrcReg, OddSrcReg);
94
95         I->setOpcode(V8::FMOVS);
96         I->SetMachineOperandReg(0, EvenDestReg);
97         I->SetMachineOperandReg(1, EvenSrcReg);
98         DEBUG(std::cerr << "FPMover: the modified instr is: " << *I);
99         // Insert copy for the other half of the double:
100         MI = BuildMI(MBB, I, V8::FMOVS, 1, OddDestReg).addReg(OddSrcReg);
101         DEBUG(std::cerr << "FPMover: the inserted instr is: " << *MI);
102         ++NumFpDs;
103       } else {
104         MBB.erase(MI);
105         ++NoopFpDs;
106       }
107       Changed = true;
108     }
109   }
110   return Changed;
111 }