blob: 9a3ff12bf6a7bc41d56cca2344b844a2141e88f2 [file] [log] [blame]
Chris Lattner7c90f732006-02-05 05:50:24 +00001//===- SparcInstrInfo.cpp - Sparc Instruction Information -------*- C++ -*-===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Brian Gaekee785e532004-02-25 19:28:19 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Brian Gaekee785e532004-02-25 19:28:19 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattner7c90f732006-02-05 05:50:24 +000010// This file contains the Sparc implementation of the TargetInstrInfo class.
Brian Gaekee785e532004-02-25 19:28:19 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner7c90f732006-02-05 05:50:24 +000014#include "SparcInstrInfo.h"
Owen Andersond10fd972007-12-31 06:32:00 +000015#include "SparcSubtarget.h"
Chris Lattner7c90f732006-02-05 05:50:24 +000016#include "Sparc.h"
Owen Anderson718cb662007-09-07 04:06:50 +000017#include "llvm/ADT/STLExtras.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000018#include "llvm/ADT/SmallVector.h"
Brian Gaekee785e532004-02-25 19:28:19 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner7c90f732006-02-05 05:50:24 +000020#include "SparcGenInstrInfo.inc"
Chris Lattner1ddf4752004-02-29 05:59:33 +000021using namespace llvm;
Brian Gaekee785e532004-02-25 19:28:19 +000022
Chris Lattner7c90f732006-02-05 05:50:24 +000023SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST)
Chris Lattner64105522008-01-01 01:03:04 +000024 : TargetInstrInfoImpl(SparcInsts, array_lengthof(SparcInsts)),
Owen Andersond10fd972007-12-31 06:32:00 +000025 RI(ST, *this), Subtarget(ST) {
Brian Gaekee785e532004-02-25 19:28:19 +000026}
27
Chris Lattner69d39092006-02-04 06:58:46 +000028static bool isZeroImm(const MachineOperand &op) {
Dan Gohmand735b802008-10-03 15:45:36 +000029 return op.isImm() && op.getImm() == 0;
Brian Gaeke4658ba12004-12-11 05:19:03 +000030}
31
Chris Lattner1d6dc972004-07-25 06:19:04 +000032/// Return true if the instruction is a register to register move and
33/// leave the source and dest operands in the passed parameters.
34///
Chris Lattner7c90f732006-02-05 05:50:24 +000035bool SparcInstrInfo::isMoveInstr(const MachineInstr &MI,
Evan Cheng04ee5a12009-01-20 19:12:24 +000036 unsigned &SrcReg, unsigned &DstReg,
37 unsigned &SrcSR, unsigned &DstSR) const {
38 SrcSR = DstSR = 0; // No sub-registers.
39
Brian Gaeke4658ba12004-12-11 05:19:03 +000040 // We look for 3 kinds of patterns here:
41 // or with G0 or 0
42 // add with G0 or 0
43 // fmovs or FpMOVD (pseudo double move).
Chris Lattner7c90f732006-02-05 05:50:24 +000044 if (MI.getOpcode() == SP::ORrr || MI.getOpcode() == SP::ADDrr) {
45 if (MI.getOperand(1).getReg() == SP::G0) {
Chris Lattner1d6dc972004-07-25 06:19:04 +000046 DstReg = MI.getOperand(0).getReg();
47 SrcReg = MI.getOperand(2).getReg();
Brian Gaeke9b8ed0e2004-09-29 03:28:15 +000048 return true;
Chris Lattner7c90f732006-02-05 05:50:24 +000049 } else if (MI.getOperand(2).getReg() == SP::G0) {
Brian Gaeke4658ba12004-12-11 05:19:03 +000050 DstReg = MI.getOperand(0).getReg();
51 SrcReg = MI.getOperand(1).getReg();
52 return true;
53 }
Chris Lattner7c90f732006-02-05 05:50:24 +000054 } else if ((MI.getOpcode() == SP::ORri || MI.getOpcode() == SP::ADDri) &&
Dan Gohmand735b802008-10-03 15:45:36 +000055 isZeroImm(MI.getOperand(2)) && MI.getOperand(1).isReg()) {
Chris Lattner69d39092006-02-04 06:58:46 +000056 DstReg = MI.getOperand(0).getReg();
57 SrcReg = MI.getOperand(1).getReg();
58 return true;
Chris Lattner7c90f732006-02-05 05:50:24 +000059 } else if (MI.getOpcode() == SP::FMOVS || MI.getOpcode() == SP::FpMOVD ||
60 MI.getOpcode() == SP::FMOVD) {
Chris Lattner1d6dc972004-07-25 06:19:04 +000061 SrcReg = MI.getOperand(1).getReg();
62 DstReg = MI.getOperand(0).getReg();
63 return true;
64 }
65 return false;
66}
Chris Lattner5ccc7222006-02-03 06:44:54 +000067
68/// isLoadFromStackSlot - If the specified machine instruction is a direct
69/// load from a stack slot, return the virtual or physical register number of
70/// the destination along with the FrameIndex of the loaded stack slot. If
71/// not, return 0. This predicate must return 0 if the instruction has
72/// any side effects other than loading from the stack slot.
Dan Gohmancbad42c2008-11-18 19:49:32 +000073unsigned SparcInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner7c90f732006-02-05 05:50:24 +000074 int &FrameIndex) const {
75 if (MI->getOpcode() == SP::LDri ||
76 MI->getOpcode() == SP::LDFri ||
77 MI->getOpcode() == SP::LDDFri) {
Dan Gohmand735b802008-10-03 15:45:36 +000078 if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000079 MI->getOperand(2).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000080 FrameIndex = MI->getOperand(1).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +000081 return MI->getOperand(0).getReg();
82 }
83 }
84 return 0;
85}
86
87/// isStoreToStackSlot - If the specified machine instruction is a direct
88/// store to a stack slot, return the virtual or physical register number of
89/// the source reg along with the FrameIndex of the loaded stack slot. If
90/// not, return 0. This predicate must return 0 if the instruction has
91/// any side effects other than storing to the stack slot.
Dan Gohmancbad42c2008-11-18 19:49:32 +000092unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner7c90f732006-02-05 05:50:24 +000093 int &FrameIndex) const {
94 if (MI->getOpcode() == SP::STri ||
95 MI->getOpcode() == SP::STFri ||
96 MI->getOpcode() == SP::STDFri) {
Dan Gohmand735b802008-10-03 15:45:36 +000097 if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000098 MI->getOperand(1).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000099 FrameIndex = MI->getOperand(0).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +0000100 return MI->getOperand(2).getReg();
101 }
102 }
103 return 0;
104}
Chris Lattnere87146a2006-10-24 16:39:19 +0000105
Evan Cheng6ae36262007-05-18 00:18:17 +0000106unsigned
107SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
108 MachineBasicBlock *FBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000109 const SmallVectorImpl<MachineOperand> &Cond)const{
Chris Lattnere87146a2006-10-24 16:39:19 +0000110 // Can only insert uncond branches so far.
111 assert(Cond.empty() && !FBB && TBB && "Can only handle uncond branches!");
Evan Chengc0f64ff2006-11-27 23:37:22 +0000112 BuildMI(&MBB, get(SP::BA)).addMBB(TBB);
Evan Cheng6ae36262007-05-18 00:18:17 +0000113 return 1;
Rafael Espindola3d7d39a2006-10-24 17:07:11 +0000114}
Owen Andersond10fd972007-12-31 06:32:00 +0000115
Owen Anderson940f83e2008-08-26 18:03:31 +0000116bool SparcInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000117 MachineBasicBlock::iterator I,
118 unsigned DestReg, unsigned SrcReg,
119 const TargetRegisterClass *DestRC,
120 const TargetRegisterClass *SrcRC) const {
Owen Andersond10fd972007-12-31 06:32:00 +0000121 if (DestRC != SrcRC) {
Owen Anderson940f83e2008-08-26 18:03:31 +0000122 // Not yet supported!
123 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000124 }
125
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000126 DebugLoc DL = DebugLoc::getUnknownLoc();
127 if (I != MBB.end()) DL = I->getDebugLoc();
128
Owen Andersond10fd972007-12-31 06:32:00 +0000129 if (DestRC == SP::IntRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000130 BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000131 else if (DestRC == SP::FPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000132 BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000133 else if (DestRC == SP::DFPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000134 BuildMI(MBB, I, DL, get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD),DestReg)
Owen Andersond10fd972007-12-31 06:32:00 +0000135 .addReg(SrcReg);
136 else
Owen Anderson940f83e2008-08-26 18:03:31 +0000137 // Can't copy this register
138 return false;
139
140 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000141}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000142
143void SparcInstrInfo::
144storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
145 unsigned SrcReg, bool isKill, int FI,
146 const TargetRegisterClass *RC) const {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000147 DebugLoc DL = DebugLoc::getUnknownLoc();
148 if (I != MBB.end()) DL = I->getDebugLoc();
149
Owen Andersonf6372aa2008-01-01 21:11:32 +0000150 // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
151 if (RC == SP::IntRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000152 BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000153 .addReg(SrcReg, false, false, isKill);
154 else if (RC == SP::FPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000155 BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000156 .addReg(SrcReg, false, false, isKill);
157 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000158 BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000159 .addReg(SrcReg, false, false, isKill);
160 else
161 assert(0 && "Can't store this register to stack slot");
162}
163
164void SparcInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000165 bool isKill,
166 SmallVectorImpl<MachineOperand> &Addr,
167 const TargetRegisterClass *RC,
Owen Andersonf6372aa2008-01-01 21:11:32 +0000168 SmallVectorImpl<MachineInstr*> &NewMIs) const {
169 unsigned Opc = 0;
Dale Johannesen21b55412009-02-12 23:08:38 +0000170 DebugLoc DL = DebugLoc::getUnknownLoc();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000171 if (RC == SP::IntRegsRegisterClass)
172 Opc = SP::STri;
173 else if (RC == SP::FPRegsRegisterClass)
174 Opc = SP::STFri;
175 else if (RC == SP::DFPRegsRegisterClass)
176 Opc = SP::STDFri;
177 else
178 assert(0 && "Can't load this register");
Dale Johannesen21b55412009-02-12 23:08:38 +0000179 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000180 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
181 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000182 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000183 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000184 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000185 MIB.addImm(MO.getImm());
186 else {
Dan Gohmand735b802008-10-03 15:45:36 +0000187 assert(MO.isFI());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000188 MIB.addFrameIndex(MO.getIndex());
189 }
190 }
191 MIB.addReg(SrcReg, false, false, isKill);
192 NewMIs.push_back(MIB);
193 return;
194}
195
196void SparcInstrInfo::
197loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
198 unsigned DestReg, int FI,
199 const TargetRegisterClass *RC) const {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000200 DebugLoc DL = DebugLoc::getUnknownLoc();
201 if (I != MBB.end()) DL = I->getDebugLoc();
202
Owen Andersonf6372aa2008-01-01 21:11:32 +0000203 if (RC == SP::IntRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000204 BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000205 else if (RC == SP::FPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000206 BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000207 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000208 BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000209 else
210 assert(0 && "Can't load this register from stack slot");
211}
212
213void SparcInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000214 SmallVectorImpl<MachineOperand> &Addr,
215 const TargetRegisterClass *RC,
Owen Andersonf6372aa2008-01-01 21:11:32 +0000216 SmallVectorImpl<MachineInstr*> &NewMIs) const {
217 unsigned Opc = 0;
218 if (RC == SP::IntRegsRegisterClass)
219 Opc = SP::LDri;
220 else if (RC == SP::FPRegsRegisterClass)
221 Opc = SP::LDFri;
222 else if (RC == SP::DFPRegsRegisterClass)
223 Opc = SP::LDDFri;
224 else
225 assert(0 && "Can't load this register");
Dale Johannesen21b55412009-02-12 23:08:38 +0000226 DebugLoc DL = DebugLoc::getUnknownLoc();
227 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000228 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
229 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000230 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000231 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000232 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000233 MIB.addImm(MO.getImm());
234 else {
Dan Gohmand735b802008-10-03 15:45:36 +0000235 assert(MO.isFI());
Owen Andersonf6372aa2008-01-01 21:11:32 +0000236 MIB.addFrameIndex(MO.getIndex());
237 }
238 }
239 NewMIs.push_back(MIB);
240 return;
241}
Owen Anderson43dbe052008-01-07 01:35:02 +0000242
Dan Gohmanc54baa22008-12-03 18:43:12 +0000243MachineInstr *SparcInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
244 MachineInstr* MI,
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000245 const SmallVectorImpl<unsigned> &Ops,
Dan Gohmanc54baa22008-12-03 18:43:12 +0000246 int FI) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000247 if (Ops.size() != 1) return NULL;
248
249 unsigned OpNum = Ops[0];
250 bool isFloat = false;
251 MachineInstr *NewMI = NULL;
252 switch (MI->getOpcode()) {
253 case SP::ORrr:
Dan Gohmand735b802008-10-03 15:45:36 +0000254 if (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == SP::G0&&
255 MI->getOperand(0).isReg() && MI->getOperand(2).isReg()) {
Owen Anderson43dbe052008-01-07 01:35:02 +0000256 if (OpNum == 0) // COPY -> STORE
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000257 NewMI = BuildMI(MF, MI->getDebugLoc(), get(SP::STri))
258 .addFrameIndex(FI)
259 .addImm(0)
260 .addReg(MI->getOperand(2).getReg());
Owen Anderson43dbe052008-01-07 01:35:02 +0000261 else // COPY -> LOAD
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000262 NewMI = BuildMI(MF, MI->getDebugLoc(), get(SP::LDri),
263 MI->getOperand(0).getReg())
264 .addFrameIndex(FI)
265 .addImm(0);
Owen Anderson43dbe052008-01-07 01:35:02 +0000266 }
267 break;
268 case SP::FMOVS:
269 isFloat = true;
270 // FALLTHROUGH
271 case SP::FMOVD:
Evan Cheng9f1c8312008-07-03 09:09:37 +0000272 if (OpNum == 0) { // COPY -> STORE
273 unsigned SrcReg = MI->getOperand(1).getReg();
274 bool isKill = MI->getOperand(1).isKill();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000275 NewMI = BuildMI(MF, MI->getDebugLoc(),
276 get(isFloat ? SP::STFri : SP::STDFri))
277 .addFrameIndex(FI)
278 .addImm(0)
279 .addReg(SrcReg, false, false, isKill);
Evan Cheng9f1c8312008-07-03 09:09:37 +0000280 } else { // COPY -> LOAD
281 unsigned DstReg = MI->getOperand(0).getReg();
282 bool isDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000283 NewMI = BuildMI(MF, MI->getDebugLoc(),
284 get(isFloat ? SP::LDFri : SP::LDDFri))
285 .addReg(DstReg, true, false, false, isDead)
286 .addFrameIndex(FI)
287 .addImm(0);
Evan Cheng9f1c8312008-07-03 09:09:37 +0000288 }
Owen Anderson43dbe052008-01-07 01:35:02 +0000289 break;
290 }
291
Owen Anderson43dbe052008-01-07 01:35:02 +0000292 return NewMI;
Duncan Sands9c5525f2008-01-07 19:13:36 +0000293}