Chris Lattner | 7c90f73 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 1 | //===- SparcInstrInfo.cpp - Sparc Instruction Information -------*- C++ -*-===// |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
Brian Gaeke | e785e53 | 2004-02-25 19:28:19 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Brian Gaeke | e785e53 | 2004-02-25 19:28:19 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 7c90f73 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 10 | // This file contains the Sparc implementation of the TargetInstrInfo class. |
Brian Gaeke | e785e53 | 2004-02-25 19:28:19 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 7c90f73 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 14 | #include "SparcInstrInfo.h" |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 15 | #include "SparcSubtarget.h" |
Chris Lattner | 7c90f73 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 16 | #include "Sparc.h" |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Brian Gaeke | e785e53 | 2004-02-25 19:28:19 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 7c90f73 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 19 | #include "SparcGenInstrInfo.inc" |
Chris Lattner | 1ddf475 | 2004-02-29 05:59:33 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Brian Gaeke | e785e53 | 2004-02-25 19:28:19 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 7c90f73 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 22 | SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST) |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 23 | : TargetInstrInfoImpl(SparcInsts, array_lengthof(SparcInsts)), |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 24 | RI(ST, *this), Subtarget(ST) { |
Brian Gaeke | e785e53 | 2004-02-25 19:28:19 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Chris Lattner | 69d3909 | 2006-02-04 06:58:46 +0000 | [diff] [blame] | 27 | static bool isZeroImm(const MachineOperand &op) { |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 28 | return op.isImmediate() && op.getImm() == 0; |
Brian Gaeke | 4658ba1 | 2004-12-11 05:19:03 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Chris Lattner | 1d6dc97 | 2004-07-25 06:19:04 +0000 | [diff] [blame] | 31 | /// Return true if the instruction is a register to register move and |
| 32 | /// leave the source and dest operands in the passed parameters. |
| 33 | /// |
Chris Lattner | 7c90f73 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 34 | bool SparcInstrInfo::isMoveInstr(const MachineInstr &MI, |
| 35 | unsigned &SrcReg, unsigned &DstReg) const { |
Brian Gaeke | 4658ba1 | 2004-12-11 05:19:03 +0000 | [diff] [blame] | 36 | // We look for 3 kinds of patterns here: |
| 37 | // or with G0 or 0 |
| 38 | // add with G0 or 0 |
| 39 | // fmovs or FpMOVD (pseudo double move). |
Chris Lattner | 7c90f73 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 40 | if (MI.getOpcode() == SP::ORrr || MI.getOpcode() == SP::ADDrr) { |
| 41 | if (MI.getOperand(1).getReg() == SP::G0) { |
Chris Lattner | 1d6dc97 | 2004-07-25 06:19:04 +0000 | [diff] [blame] | 42 | DstReg = MI.getOperand(0).getReg(); |
| 43 | SrcReg = MI.getOperand(2).getReg(); |
Brian Gaeke | 9b8ed0e | 2004-09-29 03:28:15 +0000 | [diff] [blame] | 44 | return true; |
Chris Lattner | 7c90f73 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 45 | } else if (MI.getOperand(2).getReg() == SP::G0) { |
Brian Gaeke | 4658ba1 | 2004-12-11 05:19:03 +0000 | [diff] [blame] | 46 | DstReg = MI.getOperand(0).getReg(); |
| 47 | SrcReg = MI.getOperand(1).getReg(); |
| 48 | return true; |
| 49 | } |
Chris Lattner | 7c90f73 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 50 | } else if ((MI.getOpcode() == SP::ORri || MI.getOpcode() == SP::ADDri) && |
Chris Lattner | 69d3909 | 2006-02-04 06:58:46 +0000 | [diff] [blame] | 51 | isZeroImm(MI.getOperand(2)) && MI.getOperand(1).isRegister()) { |
| 52 | DstReg = MI.getOperand(0).getReg(); |
| 53 | SrcReg = MI.getOperand(1).getReg(); |
| 54 | return true; |
Chris Lattner | 7c90f73 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 55 | } else if (MI.getOpcode() == SP::FMOVS || MI.getOpcode() == SP::FpMOVD || |
| 56 | MI.getOpcode() == SP::FMOVD) { |
Chris Lattner | 1d6dc97 | 2004-07-25 06:19:04 +0000 | [diff] [blame] | 57 | SrcReg = MI.getOperand(1).getReg(); |
| 58 | DstReg = MI.getOperand(0).getReg(); |
| 59 | return true; |
| 60 | } |
| 61 | return false; |
| 62 | } |
Chris Lattner | 5ccc722 | 2006-02-03 06:44:54 +0000 | [diff] [blame] | 63 | |
| 64 | /// isLoadFromStackSlot - If the specified machine instruction is a direct |
| 65 | /// load from a stack slot, return the virtual or physical register number of |
| 66 | /// the destination along with the FrameIndex of the loaded stack slot. If |
| 67 | /// not, return 0. This predicate must return 0 if the instruction has |
| 68 | /// any side effects other than loading from the stack slot. |
Chris Lattner | 7c90f73 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 69 | unsigned SparcInstrInfo::isLoadFromStackSlot(MachineInstr *MI, |
| 70 | int &FrameIndex) const { |
| 71 | if (MI->getOpcode() == SP::LDri || |
| 72 | MI->getOpcode() == SP::LDFri || |
| 73 | MI->getOpcode() == SP::LDDFri) { |
Chris Lattner | 5ccc722 | 2006-02-03 06:44:54 +0000 | [diff] [blame] | 74 | if (MI->getOperand(1).isFrameIndex() && MI->getOperand(2).isImmediate() && |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 75 | MI->getOperand(2).getImm() == 0) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 76 | FrameIndex = MI->getOperand(1).getIndex(); |
Chris Lattner | 5ccc722 | 2006-02-03 06:44:54 +0000 | [diff] [blame] | 77 | return MI->getOperand(0).getReg(); |
| 78 | } |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | /// isStoreToStackSlot - If the specified machine instruction is a direct |
| 84 | /// store to a stack slot, return the virtual or physical register number of |
| 85 | /// the source reg along with the FrameIndex of the loaded stack slot. If |
| 86 | /// not, return 0. This predicate must return 0 if the instruction has |
| 87 | /// any side effects other than storing to the stack slot. |
Chris Lattner | 7c90f73 | 2006-02-05 05:50:24 +0000 | [diff] [blame] | 88 | unsigned SparcInstrInfo::isStoreToStackSlot(MachineInstr *MI, |
| 89 | int &FrameIndex) const { |
| 90 | if (MI->getOpcode() == SP::STri || |
| 91 | MI->getOpcode() == SP::STFri || |
| 92 | MI->getOpcode() == SP::STDFri) { |
Chris Lattner | 5ccc722 | 2006-02-03 06:44:54 +0000 | [diff] [blame] | 93 | if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() && |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 94 | MI->getOperand(1).getImm() == 0) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 95 | FrameIndex = MI->getOperand(0).getIndex(); |
Chris Lattner | 5ccc722 | 2006-02-03 06:44:54 +0000 | [diff] [blame] | 96 | return MI->getOperand(2).getReg(); |
| 97 | } |
| 98 | } |
| 99 | return 0; |
| 100 | } |
Chris Lattner | e87146a | 2006-10-24 16:39:19 +0000 | [diff] [blame] | 101 | |
Evan Cheng | 6ae3626 | 2007-05-18 00:18:17 +0000 | [diff] [blame] | 102 | unsigned |
| 103 | SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB, |
| 104 | MachineBasicBlock *FBB, |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 105 | const SmallVectorImpl<MachineOperand> &Cond)const{ |
Chris Lattner | e87146a | 2006-10-24 16:39:19 +0000 | [diff] [blame] | 106 | // Can only insert uncond branches so far. |
| 107 | assert(Cond.empty() && !FBB && TBB && "Can only handle uncond branches!"); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 108 | BuildMI(&MBB, get(SP::BA)).addMBB(TBB); |
Evan Cheng | 6ae3626 | 2007-05-18 00:18:17 +0000 | [diff] [blame] | 109 | return 1; |
Rafael Espindola | 3d7d39a | 2006-10-24 17:07:11 +0000 | [diff] [blame] | 110 | } |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 111 | |
Owen Anderson | 940f83e | 2008-08-26 18:03:31 +0000 | [diff] [blame^] | 112 | bool SparcInstrInfo::copyRegToReg(MachineBasicBlock &MBB, |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 113 | MachineBasicBlock::iterator I, |
| 114 | unsigned DestReg, unsigned SrcReg, |
| 115 | const TargetRegisterClass *DestRC, |
| 116 | const TargetRegisterClass *SrcRC) const { |
| 117 | if (DestRC != SrcRC) { |
Owen Anderson | 940f83e | 2008-08-26 18:03:31 +0000 | [diff] [blame^] | 118 | // Not yet supported! |
| 119 | return false; |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | if (DestRC == SP::IntRegsRegisterClass) |
| 123 | BuildMI(MBB, I, get(SP::ORrr), DestReg).addReg(SP::G0).addReg(SrcReg); |
| 124 | else if (DestRC == SP::FPRegsRegisterClass) |
| 125 | BuildMI(MBB, I, get(SP::FMOVS), DestReg).addReg(SrcReg); |
| 126 | else if (DestRC == SP::DFPRegsRegisterClass) |
| 127 | BuildMI(MBB, I, get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD),DestReg) |
| 128 | .addReg(SrcReg); |
| 129 | else |
Owen Anderson | 940f83e | 2008-08-26 18:03:31 +0000 | [diff] [blame^] | 130 | // Can't copy this register |
| 131 | return false; |
| 132 | |
| 133 | return true; |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 134 | } |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 135 | |
| 136 | void SparcInstrInfo:: |
| 137 | storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 138 | unsigned SrcReg, bool isKill, int FI, |
| 139 | const TargetRegisterClass *RC) const { |
| 140 | // On the order of operands here: think "[FrameIdx + 0] = SrcReg". |
| 141 | if (RC == SP::IntRegsRegisterClass) |
| 142 | BuildMI(MBB, I, get(SP::STri)).addFrameIndex(FI).addImm(0) |
| 143 | .addReg(SrcReg, false, false, isKill); |
| 144 | else if (RC == SP::FPRegsRegisterClass) |
| 145 | BuildMI(MBB, I, get(SP::STFri)).addFrameIndex(FI).addImm(0) |
| 146 | .addReg(SrcReg, false, false, isKill); |
| 147 | else if (RC == SP::DFPRegsRegisterClass) |
| 148 | BuildMI(MBB, I, get(SP::STDFri)).addFrameIndex(FI).addImm(0) |
| 149 | .addReg(SrcReg, false, false, isKill); |
| 150 | else |
| 151 | assert(0 && "Can't store this register to stack slot"); |
| 152 | } |
| 153 | |
| 154 | void SparcInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg, |
| 155 | bool isKill, |
| 156 | SmallVectorImpl<MachineOperand> &Addr, |
| 157 | const TargetRegisterClass *RC, |
| 158 | SmallVectorImpl<MachineInstr*> &NewMIs) const { |
| 159 | unsigned Opc = 0; |
| 160 | if (RC == SP::IntRegsRegisterClass) |
| 161 | Opc = SP::STri; |
| 162 | else if (RC == SP::FPRegsRegisterClass) |
| 163 | Opc = SP::STFri; |
| 164 | else if (RC == SP::DFPRegsRegisterClass) |
| 165 | Opc = SP::STDFri; |
| 166 | else |
| 167 | assert(0 && "Can't load this register"); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 168 | MachineInstrBuilder MIB = BuildMI(MF, get(Opc)); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 169 | for (unsigned i = 0, e = Addr.size(); i != e; ++i) { |
| 170 | MachineOperand &MO = Addr[i]; |
| 171 | if (MO.isRegister()) |
| 172 | MIB.addReg(MO.getReg()); |
| 173 | else if (MO.isImmediate()) |
| 174 | MIB.addImm(MO.getImm()); |
| 175 | else { |
| 176 | assert(MO.isFI()); |
| 177 | MIB.addFrameIndex(MO.getIndex()); |
| 178 | } |
| 179 | } |
| 180 | MIB.addReg(SrcReg, false, false, isKill); |
| 181 | NewMIs.push_back(MIB); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | void SparcInstrInfo:: |
| 186 | loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 187 | unsigned DestReg, int FI, |
| 188 | const TargetRegisterClass *RC) const { |
| 189 | if (RC == SP::IntRegsRegisterClass) |
| 190 | BuildMI(MBB, I, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0); |
| 191 | else if (RC == SP::FPRegsRegisterClass) |
| 192 | BuildMI(MBB, I, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0); |
| 193 | else if (RC == SP::DFPRegsRegisterClass) |
| 194 | BuildMI(MBB, I, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0); |
| 195 | else |
| 196 | assert(0 && "Can't load this register from stack slot"); |
| 197 | } |
| 198 | |
| 199 | void SparcInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg, |
| 200 | SmallVectorImpl<MachineOperand> &Addr, |
| 201 | const TargetRegisterClass *RC, |
| 202 | SmallVectorImpl<MachineInstr*> &NewMIs) const { |
| 203 | unsigned Opc = 0; |
| 204 | if (RC == SP::IntRegsRegisterClass) |
| 205 | Opc = SP::LDri; |
| 206 | else if (RC == SP::FPRegsRegisterClass) |
| 207 | Opc = SP::LDFri; |
| 208 | else if (RC == SP::DFPRegsRegisterClass) |
| 209 | Opc = SP::LDDFri; |
| 210 | else |
| 211 | assert(0 && "Can't load this register"); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 212 | MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 213 | for (unsigned i = 0, e = Addr.size(); i != e; ++i) { |
| 214 | MachineOperand &MO = Addr[i]; |
| 215 | if (MO.isReg()) |
| 216 | MIB.addReg(MO.getReg()); |
| 217 | else if (MO.isImm()) |
| 218 | MIB.addImm(MO.getImm()); |
| 219 | else { |
| 220 | assert(MO.isFI()); |
| 221 | MIB.addFrameIndex(MO.getIndex()); |
| 222 | } |
| 223 | } |
| 224 | NewMIs.push_back(MIB); |
| 225 | return; |
| 226 | } |
Owen Anderson | 43dbe05 | 2008-01-07 01:35:02 +0000 | [diff] [blame] | 227 | |
Evan Cheng | 5fd79d0 | 2008-02-08 21:20:40 +0000 | [diff] [blame] | 228 | MachineInstr *SparcInstrInfo::foldMemoryOperand(MachineFunction &MF, |
| 229 | MachineInstr* MI, |
| 230 | SmallVectorImpl<unsigned> &Ops, |
| 231 | int FI) const { |
Owen Anderson | 43dbe05 | 2008-01-07 01:35:02 +0000 | [diff] [blame] | 232 | if (Ops.size() != 1) return NULL; |
| 233 | |
| 234 | unsigned OpNum = Ops[0]; |
| 235 | bool isFloat = false; |
| 236 | MachineInstr *NewMI = NULL; |
| 237 | switch (MI->getOpcode()) { |
| 238 | case SP::ORrr: |
| 239 | if (MI->getOperand(1).isRegister() && MI->getOperand(1).getReg() == SP::G0&& |
| 240 | MI->getOperand(0).isRegister() && MI->getOperand(2).isRegister()) { |
| 241 | if (OpNum == 0) // COPY -> STORE |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 242 | NewMI = BuildMI(MF, get(SP::STri)).addFrameIndex(FI).addImm(0) |
Owen Anderson | 43dbe05 | 2008-01-07 01:35:02 +0000 | [diff] [blame] | 243 | .addReg(MI->getOperand(2).getReg()); |
| 244 | else // COPY -> LOAD |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 245 | NewMI = BuildMI(MF, get(SP::LDri), MI->getOperand(0).getReg()) |
Owen Anderson | 43dbe05 | 2008-01-07 01:35:02 +0000 | [diff] [blame] | 246 | .addFrameIndex(FI).addImm(0); |
| 247 | } |
| 248 | break; |
| 249 | case SP::FMOVS: |
| 250 | isFloat = true; |
| 251 | // FALLTHROUGH |
| 252 | case SP::FMOVD: |
Evan Cheng | 9f1c831 | 2008-07-03 09:09:37 +0000 | [diff] [blame] | 253 | if (OpNum == 0) { // COPY -> STORE |
| 254 | unsigned SrcReg = MI->getOperand(1).getReg(); |
| 255 | bool isKill = MI->getOperand(1).isKill(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 256 | NewMI = BuildMI(MF, get(isFloat ? SP::STFri : SP::STDFri)) |
Evan Cheng | 9f1c831 | 2008-07-03 09:09:37 +0000 | [diff] [blame] | 257 | .addFrameIndex(FI).addImm(0).addReg(SrcReg, false, false, isKill); |
| 258 | } else { // COPY -> LOAD |
| 259 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 260 | bool isDead = MI->getOperand(0).isDead(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 261 | NewMI = BuildMI(MF, get(isFloat ? SP::LDFri : SP::LDDFri)) |
Evan Cheng | 9f1c831 | 2008-07-03 09:09:37 +0000 | [diff] [blame] | 262 | .addReg(DstReg, true, false, false, isDead).addFrameIndex(FI).addImm(0); |
| 263 | } |
Owen Anderson | 43dbe05 | 2008-01-07 01:35:02 +0000 | [diff] [blame] | 264 | break; |
| 265 | } |
| 266 | |
Owen Anderson | 43dbe05 | 2008-01-07 01:35:02 +0000 | [diff] [blame] | 267 | return NewMI; |
Duncan Sands | 9c5525f | 2008-01-07 19:13:36 +0000 | [diff] [blame] | 268 | } |