blob: c2845c79c55accdaaae2dedbe7d21a1eeef3db84 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- SparcInstrInfo.cpp - Sparc Instruction Information -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Sparc implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SparcInstrInfo.h"
Owen Anderson8f2c8932007-12-31 06:32:00 +000015#include "SparcSubtarget.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016#include "Sparc.h"
Owen Anderson1636de92007-09-07 04:06:50 +000017#include "llvm/ADT/STLExtras.h"
Dan Gohmanc24a3f82009-01-05 17:59:02 +000018#include "llvm/ADT/SmallVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "SparcGenInstrInfo.inc"
21using namespace llvm;
22
23SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST)
Chris Lattnerd2fd6db2008-01-01 01:03:04 +000024 : TargetInstrInfoImpl(SparcInsts, array_lengthof(SparcInsts)),
Owen Anderson8f2c8932007-12-31 06:32:00 +000025 RI(ST, *this), Subtarget(ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026}
27
28static bool isZeroImm(const MachineOperand &op) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000029 return op.isImm() && op.getImm() == 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030}
31
32/// Return true if the instruction is a register to register move and
33/// leave the source and dest operands in the passed parameters.
34///
35bool SparcInstrInfo::isMoveInstr(const MachineInstr &MI,
Evan Chengf97496a2009-01-20 19:12:24 +000036 unsigned &SrcReg, unsigned &DstReg,
37 unsigned &SrcSR, unsigned &DstSR) const {
38 SrcSR = DstSR = 0; // No sub-registers.
39
Dan Gohmanf17a25c2007-07-18 16:29:46 +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).
44 if (MI.getOpcode() == SP::ORrr || MI.getOpcode() == SP::ADDrr) {
45 if (MI.getOperand(1).getReg() == SP::G0) {
46 DstReg = MI.getOperand(0).getReg();
47 SrcReg = MI.getOperand(2).getReg();
48 return true;
49 } else if (MI.getOperand(2).getReg() == SP::G0) {
50 DstReg = MI.getOperand(0).getReg();
51 SrcReg = MI.getOperand(1).getReg();
52 return true;
53 }
54 } else if ((MI.getOpcode() == SP::ORri || MI.getOpcode() == SP::ADDri) &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000055 isZeroImm(MI.getOperand(2)) && MI.getOperand(1).isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 DstReg = MI.getOperand(0).getReg();
57 SrcReg = MI.getOperand(1).getReg();
58 return true;
59 } else if (MI.getOpcode() == SP::FMOVS || MI.getOpcode() == SP::FpMOVD ||
60 MI.getOpcode() == SP::FMOVD) {
61 SrcReg = MI.getOperand(1).getReg();
62 DstReg = MI.getOperand(0).getReg();
63 return true;
64 }
65 return false;
66}
67
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 Gohman90feee22008-11-18 19:49:32 +000073unsigned SparcInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 int &FrameIndex) const {
75 if (MI->getOpcode() == SP::LDri ||
76 MI->getOpcode() == SP::LDFri ||
77 MI->getOpcode() == SP::LDDFri) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000078 if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
Chris Lattnera96056a2007-12-30 20:49:49 +000079 MI->getOperand(2).getImm() == 0) {
Chris Lattner6017d482007-12-30 23:10:15 +000080 FrameIndex = MI->getOperand(1).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Gohman90feee22008-11-18 19:49:32 +000092unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 int &FrameIndex) const {
94 if (MI->getOpcode() == SP::STri ||
95 MI->getOpcode() == SP::STFri ||
96 MI->getOpcode() == SP::STDFri) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000097 if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
Chris Lattnera96056a2007-12-30 20:49:49 +000098 MI->getOperand(1).getImm() == 0) {
Chris Lattner6017d482007-12-30 23:10:15 +000099 FrameIndex = MI->getOperand(0).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 return MI->getOperand(2).getReg();
101 }
102 }
103 return 0;
104}
105
106unsigned
107SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
108 MachineBasicBlock *FBB,
Owen Andersond131b5b2008-08-14 22:49:33 +0000109 const SmallVectorImpl<MachineOperand> &Cond)const{
Dale Johannesenb73e4bb2009-02-13 02:31:35 +0000110 // FIXME this should probably take a DebugLoc argument
111 DebugLoc dl = DebugLoc::getUnknownLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 // Can only insert uncond branches so far.
113 assert(Cond.empty() && !FBB && TBB && "Can only handle uncond branches!");
Dale Johannesenb73e4bb2009-02-13 02:31:35 +0000114 BuildMI(&MBB, dl, get(SP::BA)).addMBB(TBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 return 1;
116}
Owen Anderson8f2c8932007-12-31 06:32:00 +0000117
Owen Anderson9fa72d92008-08-26 18:03:31 +0000118bool SparcInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000119 MachineBasicBlock::iterator I,
120 unsigned DestReg, unsigned SrcReg,
121 const TargetRegisterClass *DestRC,
122 const TargetRegisterClass *SrcRC) const {
Owen Anderson8f2c8932007-12-31 06:32:00 +0000123 if (DestRC != SrcRC) {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000124 // Not yet supported!
125 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000126 }
127
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000128 DebugLoc DL = DebugLoc::getUnknownLoc();
129 if (I != MBB.end()) DL = I->getDebugLoc();
130
Owen Anderson8f2c8932007-12-31 06:32:00 +0000131 if (DestRC == SP::IntRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000132 BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000133 else if (DestRC == SP::FPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000134 BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000135 else if (DestRC == SP::DFPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000136 BuildMI(MBB, I, DL, get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD),DestReg)
Owen Anderson8f2c8932007-12-31 06:32:00 +0000137 .addReg(SrcReg);
138 else
Owen Anderson9fa72d92008-08-26 18:03:31 +0000139 // Can't copy this register
140 return false;
141
142 return true;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000143}
Owen Anderson81875432008-01-01 21:11:32 +0000144
145void SparcInstrInfo::
146storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
147 unsigned SrcReg, bool isKill, int FI,
148 const TargetRegisterClass *RC) const {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000149 DebugLoc DL = DebugLoc::getUnknownLoc();
150 if (I != MBB.end()) DL = I->getDebugLoc();
151
Owen Anderson81875432008-01-01 21:11:32 +0000152 // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
153 if (RC == SP::IntRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000154 BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
Owen Anderson81875432008-01-01 21:11:32 +0000155 .addReg(SrcReg, false, false, isKill);
156 else if (RC == SP::FPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000157 BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
Owen Anderson81875432008-01-01 21:11:32 +0000158 .addReg(SrcReg, false, false, isKill);
159 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000160 BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
Owen Anderson81875432008-01-01 21:11:32 +0000161 .addReg(SrcReg, false, false, isKill);
162 else
163 assert(0 && "Can't store this register to stack slot");
164}
165
166void SparcInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000167 bool isKill,
168 SmallVectorImpl<MachineOperand> &Addr,
169 const TargetRegisterClass *RC,
Owen Anderson81875432008-01-01 21:11:32 +0000170 SmallVectorImpl<MachineInstr*> &NewMIs) const {
171 unsigned Opc = 0;
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000172 DebugLoc DL = DebugLoc::getUnknownLoc();
Owen Anderson81875432008-01-01 21:11:32 +0000173 if (RC == SP::IntRegsRegisterClass)
174 Opc = SP::STri;
175 else if (RC == SP::FPRegsRegisterClass)
176 Opc = SP::STFri;
177 else if (RC == SP::DFPRegsRegisterClass)
178 Opc = SP::STDFri;
179 else
180 assert(0 && "Can't load this register");
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000181 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc));
Owen Anderson81875432008-01-01 21:11:32 +0000182 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
183 MachineOperand &MO = Addr[i];
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000184 if (MO.isReg())
Owen Anderson81875432008-01-01 21:11:32 +0000185 MIB.addReg(MO.getReg());
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000186 else if (MO.isImm())
Owen Anderson81875432008-01-01 21:11:32 +0000187 MIB.addImm(MO.getImm());
188 else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000189 assert(MO.isFI());
Owen Anderson81875432008-01-01 21:11:32 +0000190 MIB.addFrameIndex(MO.getIndex());
191 }
192 }
193 MIB.addReg(SrcReg, false, false, isKill);
194 NewMIs.push_back(MIB);
195 return;
196}
197
198void SparcInstrInfo::
199loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
200 unsigned DestReg, int FI,
201 const TargetRegisterClass *RC) const {
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000202 DebugLoc DL = DebugLoc::getUnknownLoc();
203 if (I != MBB.end()) DL = I->getDebugLoc();
204
Owen Anderson81875432008-01-01 21:11:32 +0000205 if (RC == SP::IntRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000206 BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
Owen Anderson81875432008-01-01 21:11:32 +0000207 else if (RC == SP::FPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000208 BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Anderson81875432008-01-01 21:11:32 +0000209 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000210 BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Anderson81875432008-01-01 21:11:32 +0000211 else
212 assert(0 && "Can't load this register from stack slot");
213}
214
215void SparcInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000216 SmallVectorImpl<MachineOperand> &Addr,
217 const TargetRegisterClass *RC,
Owen Anderson81875432008-01-01 21:11:32 +0000218 SmallVectorImpl<MachineInstr*> &NewMIs) const {
219 unsigned Opc = 0;
220 if (RC == SP::IntRegsRegisterClass)
221 Opc = SP::LDri;
222 else if (RC == SP::FPRegsRegisterClass)
223 Opc = SP::LDFri;
224 else if (RC == SP::DFPRegsRegisterClass)
225 Opc = SP::LDDFri;
226 else
227 assert(0 && "Can't load this register");
Dale Johannesen77cce4d2009-02-12 23:08:38 +0000228 DebugLoc DL = DebugLoc::getUnknownLoc();
229 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
Owen Anderson81875432008-01-01 21:11:32 +0000230 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
231 MachineOperand &MO = Addr[i];
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000232 if (MO.isReg())
Owen Anderson81875432008-01-01 21:11:32 +0000233 MIB.addReg(MO.getReg());
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000234 else if (MO.isImm())
Owen Anderson81875432008-01-01 21:11:32 +0000235 MIB.addImm(MO.getImm());
236 else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000237 assert(MO.isFI());
Owen Anderson81875432008-01-01 21:11:32 +0000238 MIB.addFrameIndex(MO.getIndex());
239 }
240 }
241 NewMIs.push_back(MIB);
242 return;
243}
Owen Anderson9a184ef2008-01-07 01:35:02 +0000244
Dan Gohmanedc83d62008-12-03 18:43:12 +0000245MachineInstr *SparcInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
246 MachineInstr* MI,
Dan Gohman46b948e2008-10-16 01:49:15 +0000247 const SmallVectorImpl<unsigned> &Ops,
Dan Gohmanedc83d62008-12-03 18:43:12 +0000248 int FI) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000249 if (Ops.size() != 1) return NULL;
250
251 unsigned OpNum = Ops[0];
252 bool isFloat = false;
253 MachineInstr *NewMI = NULL;
254 switch (MI->getOpcode()) {
255 case SP::ORrr:
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000256 if (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == SP::G0&&
257 MI->getOperand(0).isReg() && MI->getOperand(2).isReg()) {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000258 if (OpNum == 0) // COPY -> STORE
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000259 NewMI = BuildMI(MF, MI->getDebugLoc(), get(SP::STri))
260 .addFrameIndex(FI)
261 .addImm(0)
262 .addReg(MI->getOperand(2).getReg());
Owen Anderson9a184ef2008-01-07 01:35:02 +0000263 else // COPY -> LOAD
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000264 NewMI = BuildMI(MF, MI->getDebugLoc(), get(SP::LDri),
265 MI->getOperand(0).getReg())
266 .addFrameIndex(FI)
267 .addImm(0);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000268 }
269 break;
270 case SP::FMOVS:
271 isFloat = true;
272 // FALLTHROUGH
273 case SP::FMOVD:
Evan Chenge52c1912008-07-03 09:09:37 +0000274 if (OpNum == 0) { // COPY -> STORE
275 unsigned SrcReg = MI->getOperand(1).getReg();
276 bool isKill = MI->getOperand(1).isKill();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000277 NewMI = BuildMI(MF, MI->getDebugLoc(),
278 get(isFloat ? SP::STFri : SP::STDFri))
279 .addFrameIndex(FI)
280 .addImm(0)
281 .addReg(SrcReg, false, false, isKill);
Evan Chenge52c1912008-07-03 09:09:37 +0000282 } else { // COPY -> LOAD
283 unsigned DstReg = MI->getOperand(0).getReg();
284 bool isDead = MI->getOperand(0).isDead();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000285 NewMI = BuildMI(MF, MI->getDebugLoc(),
286 get(isFloat ? SP::LDFri : SP::LDDFri))
287 .addReg(DstReg, true, false, false, isDead)
288 .addFrameIndex(FI)
289 .addImm(0);
Evan Chenge52c1912008-07-03 09:09:37 +0000290 }
Owen Anderson9a184ef2008-01-07 01:35:02 +0000291 break;
292 }
293
Owen Anderson9a184ef2008-01-07 01:35:02 +0000294 return NewMI;
Duncan Sands8ae88772008-01-07 19:13:36 +0000295}