Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 1 | //===-- FPMover.cpp - SparcV8 double-precision floating point move fixer --===// |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 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. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 10 | // Expand FpMOVD/FpABSD/FpNEGD instructions into their single-precision pieces. |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 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" |
Brian Gaeke | 9e0b902 | 2004-11-30 08:15:15 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
Chris Lattner | 2c2c6c6 | 2006-01-22 23:41:00 +0000 | [diff] [blame^] | 19 | #include <iostream> |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 23 | Statistic<> NumFpDs("fpmover", "Number of instructions translated"); |
| 24 | Statistic<> NoopFpDs("fpmover", "Number of noop instructions removed"); |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 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 | |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 32 | FPMover(TargetMachine &tm) : TM(tm) { } |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 33 | |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 34 | virtual const char *getPassName() const { |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 35 | return "SparcV8 Double-FP Move Fixer"; |
| 36 | } |
| 37 | |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 38 | bool runOnMachineBasicBlock(MachineBasicBlock &MBB); |
| 39 | bool runOnMachineFunction(MachineFunction &F) { |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 40 | bool Changed = false; |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 41 | for (MachineFunction::iterator FI = F.begin(), FE = F.end(); |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 42 | FI != FE; ++FI) |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 43 | Changed |= runOnMachineBasicBlock(*FI); |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 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 | /// |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 53 | FunctionPass *llvm::createSparcV8FPMoverPass(TargetMachine &tm) { |
| 54 | return new FPMover(tm); |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 57 | /// getDoubleRegPair - Given a DFP register, return the even and odd FP |
| 58 | /// registers that correspond to it. |
| 59 | static void getDoubleRegPair(unsigned DoubleReg, unsigned &EvenReg, |
| 60 | unsigned &OddReg) { |
| 61 | static const unsigned EvenHalvesOfPairs[] = { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 62 | V8::F0, V8::F2, V8::F4, V8::F6, V8::F8, V8::F10, V8::F12, V8::F14, |
| 63 | V8::F16, V8::F18, V8::F20, V8::F22, V8::F24, V8::F26, V8::F28, V8::F30 |
Misha Brukman | 29eeea5 | 2005-05-18 20:37:33 +0000 | [diff] [blame] | 64 | }; |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 65 | static const unsigned OddHalvesOfPairs[] = { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 66 | V8::F1, V8::F3, V8::F5, V8::F7, V8::F9, V8::F11, V8::F13, V8::F15, |
| 67 | V8::F17, V8::F19, V8::F21, V8::F23, V8::F25, V8::F27, V8::F29, V8::F31 |
Misha Brukman | 29eeea5 | 2005-05-18 20:37:33 +0000 | [diff] [blame] | 68 | }; |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 69 | static const unsigned DoubleRegsInOrder[] = { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 70 | V8::D0, V8::D1, V8::D2, V8::D3, V8::D4, V8::D5, V8::D6, V8::D7, V8::D8, |
| 71 | V8::D9, V8::D10, V8::D11, V8::D12, V8::D13, V8::D14, V8::D15 |
Misha Brukman | 29eeea5 | 2005-05-18 20:37:33 +0000 | [diff] [blame] | 72 | }; |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 73 | for (unsigned i = 0; i < sizeof(DoubleRegsInOrder)/sizeof(unsigned); ++i) |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 74 | if (DoubleRegsInOrder[i] == DoubleReg) { |
| 75 | EvenReg = EvenHalvesOfPairs[i]; |
| 76 | OddReg = OddHalvesOfPairs[i]; |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 77 | return; |
| 78 | } |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 79 | assert(0 && "Can't find reg"); |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /// runOnMachineBasicBlock - Fixup FpMOVD instructions in this MBB. |
| 83 | /// |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 84 | bool FPMover::runOnMachineBasicBlock(MachineBasicBlock &MBB) { |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 85 | bool Changed = false; |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 86 | for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) { |
| 87 | MachineInstr *MI = I++; |
Chris Lattner | beecfd2 | 2005-12-19 00:50:12 +0000 | [diff] [blame] | 88 | if (MI->getOpcode() == V8::FpMOVD || MI->getOpcode() == V8::FpABSD || |
| 89 | MI->getOpcode() == V8::FpNEGD) { |
| 90 | Changed = true; |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 91 | unsigned DestDReg = MI->getOperand(0).getReg(); |
| 92 | unsigned SrcDReg = MI->getOperand(1).getReg(); |
Chris Lattner | beecfd2 | 2005-12-19 00:50:12 +0000 | [diff] [blame] | 93 | if (DestDReg == SrcDReg && MI->getOpcode() == V8::FpMOVD) { |
| 94 | MBB.erase(MI); // Eliminate the noop copy. |
| 95 | ++NoopFpDs; |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | unsigned EvenSrcReg = 0, OddSrcReg = 0, EvenDestReg = 0, OddDestReg = 0; |
| 100 | getDoubleRegPair(DestDReg, EvenDestReg, OddDestReg); |
| 101 | getDoubleRegPair(SrcDReg, EvenSrcReg, OddSrcReg); |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 102 | |
Chris Lattner | beecfd2 | 2005-12-19 00:50:12 +0000 | [diff] [blame] | 103 | if (MI->getOpcode() == V8::FpMOVD) |
| 104 | MI->setOpcode(V8::FMOVS); |
| 105 | else if (MI->getOpcode() == V8::FpNEGD) |
| 106 | MI->setOpcode(V8::FNEGS); |
| 107 | else if (MI->getOpcode() == V8::FpABSD) |
| 108 | MI->setOpcode(V8::FABSS); |
| 109 | else |
| 110 | assert(0 && "Unknown opcode!"); |
| 111 | |
| 112 | MI->SetMachineOperandReg(0, EvenDestReg); |
| 113 | MI->SetMachineOperandReg(1, EvenSrcReg); |
| 114 | DEBUG(std::cerr << "FPMover: the modified instr is: " << *MI); |
| 115 | // Insert copy for the other half of the double. |
| 116 | if (DestDReg != SrcDReg) { |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 117 | MI = BuildMI(MBB, I, V8::FMOVS, 1, OddDestReg).addReg(OddSrcReg); |
| 118 | DEBUG(std::cerr << "FPMover: the inserted instr is: " << *MI); |
Brian Gaeke | 9e0b902 | 2004-11-30 08:15:15 +0000 | [diff] [blame] | 119 | } |
Chris Lattner | beecfd2 | 2005-12-19 00:50:12 +0000 | [diff] [blame] | 120 | ++NumFpDs; |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 121 | } |
Chris Lattner | da5a7fd | 2005-12-19 00:46:20 +0000 | [diff] [blame] | 122 | } |
Brian Gaeke | 15b2838 | 2004-09-29 03:24:34 +0000 | [diff] [blame] | 123 | return Changed; |
| 124 | } |