1 //===-- FPMover.cpp - SparcV8 double-precision floating point move fixer --===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Expand FpMOVD/FpABSD/FpNEGD instructions into their single-precision pieces.
12 //===----------------------------------------------------------------------===//
15 #include "SparcV8Subtarget.h"
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/CodeGen/MachineInstrBuilder.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Support/Debug.h"
25 Statistic<> NumFpDs("fpmover", "Number of instructions translated");
26 Statistic<> NoopFpDs("fpmover", "Number of noop instructions removed");
28 struct FPMover : public MachineFunctionPass {
29 /// Target machine description which we query for reg. names, data
34 FPMover(TargetMachine &tm) : TM(tm) { }
36 virtual const char *getPassName() const {
37 return "SparcV8 Double-FP Move Fixer";
40 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
41 bool runOnMachineFunction(MachineFunction &F);
43 } // end of anonymous namespace
45 /// createSparcV8FPMoverPass - Returns a pass that turns FpMOVD
46 /// instructions into FMOVS instructions
48 FunctionPass *llvm::createSparcV8FPMoverPass(TargetMachine &tm) {
49 return new FPMover(tm);
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,
56 static const unsigned EvenHalvesOfPairs[] = {
57 V8::F0, V8::F2, V8::F4, V8::F6, V8::F8, V8::F10, V8::F12, V8::F14,
58 V8::F16, V8::F18, V8::F20, V8::F22, V8::F24, V8::F26, V8::F28, V8::F30
60 static const unsigned OddHalvesOfPairs[] = {
61 V8::F1, V8::F3, V8::F5, V8::F7, V8::F9, V8::F11, V8::F13, V8::F15,
62 V8::F17, V8::F19, V8::F21, V8::F23, V8::F25, V8::F27, V8::F29, V8::F31
64 static const unsigned DoubleRegsInOrder[] = {
65 V8::D0, V8::D1, V8::D2, V8::D3, V8::D4, V8::D5, V8::D6, V8::D7, V8::D8,
66 V8::D9, V8::D10, V8::D11, V8::D12, V8::D13, V8::D14, V8::D15
68 for (unsigned i = 0; i < sizeof(DoubleRegsInOrder)/sizeof(unsigned); ++i)
69 if (DoubleRegsInOrder[i] == DoubleReg) {
70 EvenReg = EvenHalvesOfPairs[i];
71 OddReg = OddHalvesOfPairs[i];
74 assert(0 && "Can't find reg");
77 /// runOnMachineBasicBlock - Fixup FpMOVD instructions in this MBB.
79 bool FPMover::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
81 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {
82 MachineInstr *MI = I++;
83 if (MI->getOpcode() == V8::FpMOVD || MI->getOpcode() == V8::FpABSD ||
84 MI->getOpcode() == V8::FpNEGD) {
86 unsigned DestDReg = MI->getOperand(0).getReg();
87 unsigned SrcDReg = MI->getOperand(1).getReg();
88 if (DestDReg == SrcDReg && MI->getOpcode() == V8::FpMOVD) {
89 MBB.erase(MI); // Eliminate the noop copy.
94 unsigned EvenSrcReg = 0, OddSrcReg = 0, EvenDestReg = 0, OddDestReg = 0;
95 getDoubleRegPair(DestDReg, EvenDestReg, OddDestReg);
96 getDoubleRegPair(SrcDReg, EvenSrcReg, OddSrcReg);
98 if (MI->getOpcode() == V8::FpMOVD)
99 MI->setOpcode(V8::FMOVS);
100 else if (MI->getOpcode() == V8::FpNEGD)
101 MI->setOpcode(V8::FNEGS);
102 else if (MI->getOpcode() == V8::FpABSD)
103 MI->setOpcode(V8::FABSS);
105 assert(0 && "Unknown opcode!");
107 MI->SetMachineOperandReg(0, EvenDestReg);
108 MI->SetMachineOperandReg(1, EvenSrcReg);
109 DEBUG(std::cerr << "FPMover: the modified instr is: " << *MI);
110 // Insert copy for the other half of the double.
111 if (DestDReg != SrcDReg) {
112 MI = BuildMI(MBB, I, V8::FMOVS, 1, OddDestReg).addReg(OddSrcReg);
113 DEBUG(std::cerr << "FPMover: the inserted instr is: " << *MI);
121 bool FPMover::runOnMachineFunction(MachineFunction &F) {
122 // If the target has V9 instructions, the fp-mover pseudos will never be
123 // emitted. Avoid a scan of the instructions to improve compile time.
124 if (TM.getSubtarget<SparcV8Subtarget>().isV9())
127 bool Changed = false;
128 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
130 Changed |= runOnMachineBasicBlock(*FI);