blob: 451c458ac0ab03f80c7e43f0dcd718bea4f05b87 [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"
Torok Edwinc25e7582009-07-11 20:10:48 +000020#include "llvm/Support/ErrorHandling.h"
Chris Lattner7c90f732006-02-05 05:50:24 +000021#include "SparcGenInstrInfo.inc"
Chris Lattner1ddf4752004-02-29 05:59:33 +000022using namespace llvm;
Brian Gaekee785e532004-02-25 19:28:19 +000023
Chris Lattner7c90f732006-02-05 05:50:24 +000024SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST)
Chris Lattner64105522008-01-01 01:03:04 +000025 : TargetInstrInfoImpl(SparcInsts, array_lengthof(SparcInsts)),
Owen Andersond10fd972007-12-31 06:32:00 +000026 RI(ST, *this), Subtarget(ST) {
Brian Gaekee785e532004-02-25 19:28:19 +000027}
28
Chris Lattner69d39092006-02-04 06:58:46 +000029static bool isZeroImm(const MachineOperand &op) {
Dan Gohmand735b802008-10-03 15:45:36 +000030 return op.isImm() && op.getImm() == 0;
Brian Gaeke4658ba12004-12-11 05:19:03 +000031}
32
Chris Lattner1d6dc972004-07-25 06:19:04 +000033/// Return true if the instruction is a register to register move and
34/// leave the source and dest operands in the passed parameters.
35///
Chris Lattner7c90f732006-02-05 05:50:24 +000036bool SparcInstrInfo::isMoveInstr(const MachineInstr &MI,
Evan Cheng04ee5a12009-01-20 19:12:24 +000037 unsigned &SrcReg, unsigned &DstReg,
38 unsigned &SrcSR, unsigned &DstSR) const {
39 SrcSR = DstSR = 0; // No sub-registers.
40
Brian Gaeke4658ba12004-12-11 05:19:03 +000041 // We look for 3 kinds of patterns here:
42 // or with G0 or 0
43 // add with G0 or 0
44 // fmovs or FpMOVD (pseudo double move).
Chris Lattner7c90f732006-02-05 05:50:24 +000045 if (MI.getOpcode() == SP::ORrr || MI.getOpcode() == SP::ADDrr) {
46 if (MI.getOperand(1).getReg() == SP::G0) {
Chris Lattner1d6dc972004-07-25 06:19:04 +000047 DstReg = MI.getOperand(0).getReg();
48 SrcReg = MI.getOperand(2).getReg();
Brian Gaeke9b8ed0e2004-09-29 03:28:15 +000049 return true;
Chris Lattner7c90f732006-02-05 05:50:24 +000050 } else if (MI.getOperand(2).getReg() == SP::G0) {
Brian Gaeke4658ba12004-12-11 05:19:03 +000051 DstReg = MI.getOperand(0).getReg();
52 SrcReg = MI.getOperand(1).getReg();
53 return true;
54 }
Chris Lattner7c90f732006-02-05 05:50:24 +000055 } else if ((MI.getOpcode() == SP::ORri || MI.getOpcode() == SP::ADDri) &&
Dan Gohmand735b802008-10-03 15:45:36 +000056 isZeroImm(MI.getOperand(2)) && MI.getOperand(1).isReg()) {
Chris Lattner69d39092006-02-04 06:58:46 +000057 DstReg = MI.getOperand(0).getReg();
58 SrcReg = MI.getOperand(1).getReg();
59 return true;
Chris Lattner7c90f732006-02-05 05:50:24 +000060 } else if (MI.getOpcode() == SP::FMOVS || MI.getOpcode() == SP::FpMOVD ||
61 MI.getOpcode() == SP::FMOVD) {
Chris Lattner1d6dc972004-07-25 06:19:04 +000062 SrcReg = MI.getOperand(1).getReg();
63 DstReg = MI.getOperand(0).getReg();
64 return true;
65 }
66 return false;
67}
Chris Lattner5ccc7222006-02-03 06:44:54 +000068
69/// isLoadFromStackSlot - If the specified machine instruction is a direct
70/// load from a stack slot, return the virtual or physical register number of
71/// the destination along with the FrameIndex of the loaded stack slot. If
72/// not, return 0. This predicate must return 0 if the instruction has
73/// any side effects other than loading from the stack slot.
Dan Gohmancbad42c2008-11-18 19:49:32 +000074unsigned SparcInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner7c90f732006-02-05 05:50:24 +000075 int &FrameIndex) const {
76 if (MI->getOpcode() == SP::LDri ||
77 MI->getOpcode() == SP::LDFri ||
78 MI->getOpcode() == SP::LDDFri) {
Dan Gohmand735b802008-10-03 15:45:36 +000079 if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000080 MI->getOperand(2).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000081 FrameIndex = MI->getOperand(1).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +000082 return MI->getOperand(0).getReg();
83 }
84 }
85 return 0;
86}
87
88/// isStoreToStackSlot - If the specified machine instruction is a direct
89/// store to a stack slot, return the virtual or physical register number of
90/// the source reg along with the FrameIndex of the loaded stack slot. If
91/// not, return 0. This predicate must return 0 if the instruction has
92/// any side effects other than storing to the stack slot.
Dan Gohmancbad42c2008-11-18 19:49:32 +000093unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner7c90f732006-02-05 05:50:24 +000094 int &FrameIndex) const {
95 if (MI->getOpcode() == SP::STri ||
96 MI->getOpcode() == SP::STFri ||
97 MI->getOpcode() == SP::STDFri) {
Dan Gohmand735b802008-10-03 15:45:36 +000098 if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000099 MI->getOperand(1).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000100 FrameIndex = MI->getOperand(0).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +0000101 return MI->getOperand(2).getReg();
102 }
103 }
104 return 0;
105}
Chris Lattnere87146a2006-10-24 16:39:19 +0000106
Evan Cheng6ae36262007-05-18 00:18:17 +0000107unsigned
108SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
109 MachineBasicBlock *FBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000110 const SmallVectorImpl<MachineOperand> &Cond)const{
Dale Johannesend552eee2009-02-13 02:31:35 +0000111 // FIXME this should probably take a DebugLoc argument
112 DebugLoc dl = DebugLoc::getUnknownLoc();
Chris Lattnere87146a2006-10-24 16:39:19 +0000113 // Can only insert uncond branches so far.
114 assert(Cond.empty() && !FBB && TBB && "Can only handle uncond branches!");
Dale Johannesend552eee2009-02-13 02:31:35 +0000115 BuildMI(&MBB, dl, get(SP::BA)).addMBB(TBB);
Evan Cheng6ae36262007-05-18 00:18:17 +0000116 return 1;
Rafael Espindola3d7d39a2006-10-24 17:07:11 +0000117}
Owen Andersond10fd972007-12-31 06:32:00 +0000118
Owen Anderson940f83e2008-08-26 18:03:31 +0000119bool SparcInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000120 MachineBasicBlock::iterator I,
121 unsigned DestReg, unsigned SrcReg,
122 const TargetRegisterClass *DestRC,
123 const TargetRegisterClass *SrcRC) const {
Owen Andersond10fd972007-12-31 06:32:00 +0000124 if (DestRC != SrcRC) {
Owen Anderson940f83e2008-08-26 18:03:31 +0000125 // Not yet supported!
126 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000127 }
128
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000129 DebugLoc DL = DebugLoc::getUnknownLoc();
130 if (I != MBB.end()) DL = I->getDebugLoc();
131
Owen Andersond10fd972007-12-31 06:32:00 +0000132 if (DestRC == SP::IntRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000133 BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000134 else if (DestRC == SP::FPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000135 BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000136 else if (DestRC == SP::DFPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000137 BuildMI(MBB, I, DL, get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD),DestReg)
Owen Andersond10fd972007-12-31 06:32:00 +0000138 .addReg(SrcReg);
139 else
Owen Anderson940f83e2008-08-26 18:03:31 +0000140 // Can't copy this register
141 return false;
142
143 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000144}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000145
146void SparcInstrInfo::
147storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
148 unsigned SrcReg, bool isKill, int FI,
149 const TargetRegisterClass *RC) const {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000150 DebugLoc DL = DebugLoc::getUnknownLoc();
151 if (I != MBB.end()) DL = I->getDebugLoc();
152
Owen Andersonf6372aa2008-01-01 21:11:32 +0000153 // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
154 if (RC == SP::IntRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000155 BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000156 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000157 else if (RC == SP::FPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000158 BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000159 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000160 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000161 BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000162 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000163 else
Torok Edwinc25e7582009-07-11 20:10:48 +0000164 LLVM_UNREACHABLE("Can't store this register to stack slot");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000165}
166
167void SparcInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000168 bool isKill,
169 SmallVectorImpl<MachineOperand> &Addr,
170 const TargetRegisterClass *RC,
Owen Andersonf6372aa2008-01-01 21:11:32 +0000171 SmallVectorImpl<MachineInstr*> &NewMIs) const {
172 unsigned Opc = 0;
Dale Johannesen21b55412009-02-12 23:08:38 +0000173 DebugLoc DL = DebugLoc::getUnknownLoc();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000174 if (RC == SP::IntRegsRegisterClass)
175 Opc = SP::STri;
176 else if (RC == SP::FPRegsRegisterClass)
177 Opc = SP::STFri;
178 else if (RC == SP::DFPRegsRegisterClass)
179 Opc = SP::STDFri;
180 else
Torok Edwinc25e7582009-07-11 20:10:48 +0000181 LLVM_UNREACHABLE("Can't load this register");
Dale Johannesen21b55412009-02-12 23:08:38 +0000182 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc));
Dan Gohman97357612009-02-18 05:45:50 +0000183 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
184 MIB.addOperand(Addr[i]);
Bill Wendling587daed2009-05-13 21:33:08 +0000185 MIB.addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000186 NewMIs.push_back(MIB);
187 return;
188}
189
190void SparcInstrInfo::
191loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
192 unsigned DestReg, int FI,
193 const TargetRegisterClass *RC) const {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000194 DebugLoc DL = DebugLoc::getUnknownLoc();
195 if (I != MBB.end()) DL = I->getDebugLoc();
196
Owen Andersonf6372aa2008-01-01 21:11:32 +0000197 if (RC == SP::IntRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000198 BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000199 else if (RC == SP::FPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000200 BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000201 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000202 BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000203 else
Torok Edwinc25e7582009-07-11 20:10:48 +0000204 LLVM_UNREACHABLE("Can't load this register from stack slot");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000205}
206
207void SparcInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000208 SmallVectorImpl<MachineOperand> &Addr,
209 const TargetRegisterClass *RC,
Owen Andersonf6372aa2008-01-01 21:11:32 +0000210 SmallVectorImpl<MachineInstr*> &NewMIs) const {
211 unsigned Opc = 0;
212 if (RC == SP::IntRegsRegisterClass)
213 Opc = SP::LDri;
214 else if (RC == SP::FPRegsRegisterClass)
215 Opc = SP::LDFri;
216 else if (RC == SP::DFPRegsRegisterClass)
217 Opc = SP::LDDFri;
218 else
Torok Edwinc25e7582009-07-11 20:10:48 +0000219 LLVM_UNREACHABLE("Can't load this register");
Dale Johannesen21b55412009-02-12 23:08:38 +0000220 DebugLoc DL = DebugLoc::getUnknownLoc();
221 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
Dan Gohman97357612009-02-18 05:45:50 +0000222 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
223 MIB.addOperand(Addr[i]);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000224 NewMIs.push_back(MIB);
225 return;
226}
Owen Anderson43dbe052008-01-07 01:35:02 +0000227
Dan Gohmanc54baa22008-12-03 18:43:12 +0000228MachineInstr *SparcInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
229 MachineInstr* MI,
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000230 const SmallVectorImpl<unsigned> &Ops,
Dan Gohmanc54baa22008-12-03 18:43:12 +0000231 int FI) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000232 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:
Dan Gohmand735b802008-10-03 15:45:36 +0000239 if (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == SP::G0&&
240 MI->getOperand(0).isReg() && MI->getOperand(2).isReg()) {
Owen Anderson43dbe052008-01-07 01:35:02 +0000241 if (OpNum == 0) // COPY -> STORE
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000242 NewMI = BuildMI(MF, MI->getDebugLoc(), get(SP::STri))
243 .addFrameIndex(FI)
244 .addImm(0)
245 .addReg(MI->getOperand(2).getReg());
Owen Anderson43dbe052008-01-07 01:35:02 +0000246 else // COPY -> LOAD
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000247 NewMI = BuildMI(MF, MI->getDebugLoc(), get(SP::LDri),
248 MI->getOperand(0).getReg())
249 .addFrameIndex(FI)
250 .addImm(0);
Owen Anderson43dbe052008-01-07 01:35:02 +0000251 }
252 break;
253 case SP::FMOVS:
254 isFloat = true;
255 // FALLTHROUGH
256 case SP::FMOVD:
Evan Cheng9f1c8312008-07-03 09:09:37 +0000257 if (OpNum == 0) { // COPY -> STORE
258 unsigned SrcReg = MI->getOperand(1).getReg();
259 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000260 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000261 NewMI = BuildMI(MF, MI->getDebugLoc(),
262 get(isFloat ? SP::STFri : SP::STDFri))
263 .addFrameIndex(FI)
264 .addImm(0)
Evan Cheng2578ba22009-07-01 01:59:31 +0000265 .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef));
Evan Cheng9f1c8312008-07-03 09:09:37 +0000266 } else { // COPY -> LOAD
267 unsigned DstReg = MI->getOperand(0).getReg();
268 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000269 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000270 NewMI = BuildMI(MF, MI->getDebugLoc(),
271 get(isFloat ? SP::LDFri : SP::LDDFri))
Evan Cheng2578ba22009-07-01 01:59:31 +0000272 .addReg(DstReg, RegState::Define |
273 getDeadRegState(isDead) | getUndefRegState(isUndef))
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000274 .addFrameIndex(FI)
275 .addImm(0);
Evan Cheng9f1c8312008-07-03 09:09:37 +0000276 }
Owen Anderson43dbe052008-01-07 01:35:02 +0000277 break;
278 }
279
Owen Anderson43dbe052008-01-07 01:35:02 +0000280 return NewMI;
Duncan Sands9c5525f2008-01-07 19:13:36 +0000281}