Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- SparcInstrInfo.cpp - Sparc Instruction Information -------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the Sparc implementation of the TargetInstrInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "SparcInstrInfo.h" |
Owen Anderson | 8f2c893 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 15 | #include "SparcSubtarget.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 16 | #include "Sparc.h" |
Owen Anderson | 1636de9 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | c24a3f8 | 2009-01-05 17:59:02 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 49102de | 2009-09-15 17:46:24 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 22 | #include "SparcGenInstrInfo.inc" |
Chris Lattner | 49102de | 2009-09-15 17:46:24 +0000 | [diff] [blame] | 23 | #include "SparcMachineFunctionInfo.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST) |
Chris Lattner | d2fd6db | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 27 | : TargetInstrInfoImpl(SparcInsts, array_lengthof(SparcInsts)), |
Owen Anderson | 8f2c893 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 28 | RI(ST, *this), Subtarget(ST) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static bool isZeroImm(const MachineOperand &op) { |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 32 | return op.isImm() && op.getImm() == 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /// Return true if the instruction is a register to register move and |
| 36 | /// leave the source and dest operands in the passed parameters. |
| 37 | /// |
| 38 | bool SparcInstrInfo::isMoveInstr(const MachineInstr &MI, |
Evan Cheng | f97496a | 2009-01-20 19:12:24 +0000 | [diff] [blame] | 39 | unsigned &SrcReg, unsigned &DstReg, |
| 40 | unsigned &SrcSR, unsigned &DstSR) const { |
| 41 | SrcSR = DstSR = 0; // No sub-registers. |
| 42 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 43 | // We look for 3 kinds of patterns here: |
| 44 | // or with G0 or 0 |
| 45 | // add with G0 or 0 |
| 46 | // fmovs or FpMOVD (pseudo double move). |
| 47 | if (MI.getOpcode() == SP::ORrr || MI.getOpcode() == SP::ADDrr) { |
| 48 | if (MI.getOperand(1).getReg() == SP::G0) { |
| 49 | DstReg = MI.getOperand(0).getReg(); |
| 50 | SrcReg = MI.getOperand(2).getReg(); |
| 51 | return true; |
| 52 | } else if (MI.getOperand(2).getReg() == SP::G0) { |
| 53 | DstReg = MI.getOperand(0).getReg(); |
| 54 | SrcReg = MI.getOperand(1).getReg(); |
| 55 | return true; |
| 56 | } |
| 57 | } else if ((MI.getOpcode() == SP::ORri || MI.getOpcode() == SP::ADDri) && |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 58 | isZeroImm(MI.getOperand(2)) && MI.getOperand(1).isReg()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 59 | DstReg = MI.getOperand(0).getReg(); |
| 60 | SrcReg = MI.getOperand(1).getReg(); |
| 61 | return true; |
| 62 | } else if (MI.getOpcode() == SP::FMOVS || MI.getOpcode() == SP::FpMOVD || |
| 63 | MI.getOpcode() == SP::FMOVD) { |
| 64 | SrcReg = MI.getOperand(1).getReg(); |
| 65 | DstReg = MI.getOperand(0).getReg(); |
| 66 | return true; |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | /// isLoadFromStackSlot - If the specified machine instruction is a direct |
| 72 | /// load from a stack slot, return the virtual or physical register number of |
| 73 | /// the destination along with the FrameIndex of the loaded stack slot. If |
| 74 | /// not, return 0. This predicate must return 0 if the instruction has |
| 75 | /// any side effects other than loading from the stack slot. |
Dan Gohman | 90feee2 | 2008-11-18 19:49:32 +0000 | [diff] [blame] | 76 | unsigned SparcInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 77 | int &FrameIndex) const { |
| 78 | if (MI->getOpcode() == SP::LDri || |
| 79 | MI->getOpcode() == SP::LDFri || |
| 80 | MI->getOpcode() == SP::LDDFri) { |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 81 | if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() && |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 82 | MI->getOperand(2).getImm() == 0) { |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 83 | FrameIndex = MI->getOperand(1).getIndex(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 84 | return MI->getOperand(0).getReg(); |
| 85 | } |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | /// isStoreToStackSlot - If the specified machine instruction is a direct |
| 91 | /// store to a stack slot, return the virtual or physical register number of |
| 92 | /// the source reg along with the FrameIndex of the loaded stack slot. If |
| 93 | /// not, return 0. This predicate must return 0 if the instruction has |
| 94 | /// any side effects other than storing to the stack slot. |
Dan Gohman | 90feee2 | 2008-11-18 19:49:32 +0000 | [diff] [blame] | 95 | unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 96 | int &FrameIndex) const { |
| 97 | if (MI->getOpcode() == SP::STri || |
| 98 | MI->getOpcode() == SP::STFri || |
| 99 | MI->getOpcode() == SP::STDFri) { |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 100 | if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() && |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 101 | MI->getOperand(1).getImm() == 0) { |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 102 | FrameIndex = MI->getOperand(0).getIndex(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 103 | return MI->getOperand(2).getReg(); |
| 104 | } |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | unsigned |
| 110 | SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB, |
| 111 | MachineBasicBlock *FBB, |
Stuart Hastings | 9fa5e33 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 112 | const SmallVectorImpl<MachineOperand> &Cond, |
| 113 | DebugLoc DL)const{ |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 114 | // Can only insert uncond branches so far. |
| 115 | assert(Cond.empty() && !FBB && TBB && "Can only handle uncond branches!"); |
Stuart Hastings | 9fa5e33 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 116 | BuildMI(&MBB, DL, get(SP::BA)).addMBB(TBB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 117 | return 1; |
| 118 | } |
Owen Anderson | 8f2c893 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 119 | |
Jakob Stoklund Olesen | 8473ef2 | 2010-07-11 07:56:09 +0000 | [diff] [blame] | 120 | void SparcInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 121 | MachineBasicBlock::iterator I, DebugLoc DL, |
| 122 | unsigned DestReg, unsigned SrcReg, |
| 123 | bool KillSrc) const { |
| 124 | if (SP::IntRegsRegClass.contains(DestReg, SrcReg)) |
| 125 | BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0) |
| 126 | .addReg(SrcReg, getKillRegState(KillSrc)); |
| 127 | else if (SP::FPRegsRegClass.contains(DestReg, SrcReg)) |
| 128 | BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg) |
| 129 | .addReg(SrcReg, getKillRegState(KillSrc)); |
| 130 | else if (SP::DFPRegsRegClass.contains(DestReg, SrcReg)) |
| 131 | BuildMI(MBB, I, DL, get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD), DestReg) |
| 132 | .addReg(SrcReg, getKillRegState(KillSrc)); |
Owen Anderson | 8f2c893 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 133 | else |
Jakob Stoklund Olesen | 8473ef2 | 2010-07-11 07:56:09 +0000 | [diff] [blame] | 134 | llvm_unreachable("Impossible reg-to-reg copy"); |
Owen Anderson | 8f2c893 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 135 | } |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 136 | |
| 137 | void SparcInstrInfo:: |
| 138 | storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 139 | unsigned SrcReg, bool isKill, int FI, |
Evan Cheng | 1f8534d | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 140 | const TargetRegisterClass *RC, |
| 141 | const TargetRegisterInfo *TRI) const { |
Chris Lattner | d2c680b | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 142 | DebugLoc DL; |
Bill Wendling | 5b8a97b | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 143 | if (I != MBB.end()) DL = I->getDebugLoc(); |
| 144 | |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 145 | // On the order of operands here: think "[FrameIdx + 0] = SrcReg". |
| 146 | if (RC == SP::IntRegsRegisterClass) |
Bill Wendling | 5b8a97b | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 147 | BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0) |
Bill Wendling | 2b73976 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 148 | .addReg(SrcReg, getKillRegState(isKill)); |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 149 | else if (RC == SP::FPRegsRegisterClass) |
Bill Wendling | 5b8a97b | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 150 | BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0) |
Bill Wendling | 2b73976 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 151 | .addReg(SrcReg, getKillRegState(isKill)); |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 152 | else if (RC == SP::DFPRegsRegisterClass) |
Bill Wendling | 5b8a97b | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 153 | BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0) |
Bill Wendling | 2b73976 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 154 | .addReg(SrcReg, getKillRegState(isKill)); |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 155 | else |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 156 | llvm_unreachable("Can't store this register to stack slot"); |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 159 | void SparcInstrInfo:: |
| 160 | loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 161 | unsigned DestReg, int FI, |
Evan Cheng | 1f8534d | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 162 | const TargetRegisterClass *RC, |
| 163 | const TargetRegisterInfo *TRI) const { |
Chris Lattner | d2c680b | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 164 | DebugLoc DL; |
Bill Wendling | 5b8a97b | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 165 | if (I != MBB.end()) DL = I->getDebugLoc(); |
| 166 | |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 167 | if (RC == SP::IntRegsRegisterClass) |
Bill Wendling | 5b8a97b | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 168 | BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0); |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 169 | else if (RC == SP::FPRegsRegisterClass) |
Bill Wendling | 5b8a97b | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 170 | BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0); |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 171 | else if (RC == SP::DFPRegsRegisterClass) |
Bill Wendling | 5b8a97b | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 172 | BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0); |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 173 | else |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 174 | llvm_unreachable("Can't load this register from stack slot"); |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Chris Lattner | 49102de | 2009-09-15 17:46:24 +0000 | [diff] [blame] | 177 | unsigned SparcInstrInfo::getGlobalBaseReg(MachineFunction *MF) const |
| 178 | { |
| 179 | SparcMachineFunctionInfo *SparcFI = MF->getInfo<SparcMachineFunctionInfo>(); |
| 180 | unsigned GlobalBaseReg = SparcFI->getGlobalBaseReg(); |
| 181 | if (GlobalBaseReg != 0) |
| 182 | return GlobalBaseReg; |
| 183 | |
| 184 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 185 | MachineBasicBlock &FirstMBB = MF->front(); |
| 186 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 187 | MachineRegisterInfo &RegInfo = MF->getRegInfo(); |
| 188 | |
| 189 | GlobalBaseReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass); |
| 190 | |
| 191 | |
Chris Lattner | d2c680b | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 192 | DebugLoc dl; |
Chris Lattner | 49102de | 2009-09-15 17:46:24 +0000 | [diff] [blame] | 193 | |
| 194 | BuildMI(FirstMBB, MBBI, dl, get(SP::GETPCX), GlobalBaseReg); |
| 195 | SparcFI->setGlobalBaseReg(GlobalBaseReg); |
| 196 | return GlobalBaseReg; |
| 197 | } |