blob: e494d7ddf9f8ed6020a1b2d6deb9fcada6be1c55 [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 Lattnerdb486a62009-09-15 17:46:24 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner7c90f732006-02-05 05:50:24 +000022#include "SparcGenInstrInfo.inc"
Chris Lattnerdb486a62009-09-15 17:46:24 +000023#include "SparcMachineFunctionInfo.h"
Chris Lattner1ddf4752004-02-29 05:59:33 +000024using namespace llvm;
Brian Gaekee785e532004-02-25 19:28:19 +000025
Chris Lattner7c90f732006-02-05 05:50:24 +000026SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST)
Chris Lattner64105522008-01-01 01:03:04 +000027 : TargetInstrInfoImpl(SparcInsts, array_lengthof(SparcInsts)),
Owen Andersond10fd972007-12-31 06:32:00 +000028 RI(ST, *this), Subtarget(ST) {
Brian Gaekee785e532004-02-25 19:28:19 +000029}
30
Chris Lattner69d39092006-02-04 06:58:46 +000031static bool isZeroImm(const MachineOperand &op) {
Dan Gohmand735b802008-10-03 15:45:36 +000032 return op.isImm() && op.getImm() == 0;
Brian Gaeke4658ba12004-12-11 05:19:03 +000033}
34
Chris Lattner1d6dc972004-07-25 06:19:04 +000035/// Return true if the instruction is a register to register move and
36/// leave the source and dest operands in the passed parameters.
37///
Chris Lattner7c90f732006-02-05 05:50:24 +000038bool SparcInstrInfo::isMoveInstr(const MachineInstr &MI,
Evan Cheng04ee5a12009-01-20 19:12:24 +000039 unsigned &SrcReg, unsigned &DstReg,
40 unsigned &SrcSR, unsigned &DstSR) const {
41 SrcSR = DstSR = 0; // No sub-registers.
42
Brian Gaeke4658ba12004-12-11 05:19:03 +000043 // 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).
Chris Lattner7c90f732006-02-05 05:50:24 +000047 if (MI.getOpcode() == SP::ORrr || MI.getOpcode() == SP::ADDrr) {
48 if (MI.getOperand(1).getReg() == SP::G0) {
Chris Lattner1d6dc972004-07-25 06:19:04 +000049 DstReg = MI.getOperand(0).getReg();
50 SrcReg = MI.getOperand(2).getReg();
Brian Gaeke9b8ed0e2004-09-29 03:28:15 +000051 return true;
Chris Lattner7c90f732006-02-05 05:50:24 +000052 } else if (MI.getOperand(2).getReg() == SP::G0) {
Brian Gaeke4658ba12004-12-11 05:19:03 +000053 DstReg = MI.getOperand(0).getReg();
54 SrcReg = MI.getOperand(1).getReg();
55 return true;
56 }
Chris Lattner7c90f732006-02-05 05:50:24 +000057 } else if ((MI.getOpcode() == SP::ORri || MI.getOpcode() == SP::ADDri) &&
Dan Gohmand735b802008-10-03 15:45:36 +000058 isZeroImm(MI.getOperand(2)) && MI.getOperand(1).isReg()) {
Chris Lattner69d39092006-02-04 06:58:46 +000059 DstReg = MI.getOperand(0).getReg();
60 SrcReg = MI.getOperand(1).getReg();
61 return true;
Chris Lattner7c90f732006-02-05 05:50:24 +000062 } else if (MI.getOpcode() == SP::FMOVS || MI.getOpcode() == SP::FpMOVD ||
63 MI.getOpcode() == SP::FMOVD) {
Chris Lattner1d6dc972004-07-25 06:19:04 +000064 SrcReg = MI.getOperand(1).getReg();
65 DstReg = MI.getOperand(0).getReg();
66 return true;
67 }
68 return false;
69}
Chris Lattner5ccc7222006-02-03 06:44:54 +000070
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 Gohmancbad42c2008-11-18 19:49:32 +000076unsigned SparcInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner7c90f732006-02-05 05:50:24 +000077 int &FrameIndex) const {
78 if (MI->getOpcode() == SP::LDri ||
79 MI->getOpcode() == SP::LDFri ||
80 MI->getOpcode() == SP::LDDFri) {
Dan Gohmand735b802008-10-03 15:45:36 +000081 if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000082 MI->getOperand(2).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000083 FrameIndex = MI->getOperand(1).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +000084 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 Gohmancbad42c2008-11-18 19:49:32 +000095unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner7c90f732006-02-05 05:50:24 +000096 int &FrameIndex) const {
97 if (MI->getOpcode() == SP::STri ||
98 MI->getOpcode() == SP::STFri ||
99 MI->getOpcode() == SP::STDFri) {
Dan Gohmand735b802008-10-03 15:45:36 +0000100 if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000101 MI->getOperand(1).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000102 FrameIndex = MI->getOperand(0).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +0000103 return MI->getOperand(2).getReg();
104 }
105 }
106 return 0;
107}
Chris Lattnere87146a2006-10-24 16:39:19 +0000108
Evan Cheng6ae36262007-05-18 00:18:17 +0000109unsigned
110SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
111 MachineBasicBlock *FBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000112 const SmallVectorImpl<MachineOperand> &Cond)const{
Dale Johannesend552eee2009-02-13 02:31:35 +0000113 // FIXME this should probably take a DebugLoc argument
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000114 DebugLoc dl;
Chris Lattnere87146a2006-10-24 16:39:19 +0000115 // Can only insert uncond branches so far.
116 assert(Cond.empty() && !FBB && TBB && "Can only handle uncond branches!");
Dale Johannesend552eee2009-02-13 02:31:35 +0000117 BuildMI(&MBB, dl, get(SP::BA)).addMBB(TBB);
Evan Cheng6ae36262007-05-18 00:18:17 +0000118 return 1;
Rafael Espindola3d7d39a2006-10-24 17:07:11 +0000119}
Owen Andersond10fd972007-12-31 06:32:00 +0000120
Owen Anderson940f83e2008-08-26 18:03:31 +0000121bool SparcInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000122 MachineBasicBlock::iterator I,
123 unsigned DestReg, unsigned SrcReg,
124 const TargetRegisterClass *DestRC,
125 const TargetRegisterClass *SrcRC) const {
Owen Andersond10fd972007-12-31 06:32:00 +0000126 if (DestRC != SrcRC) {
Owen Anderson940f83e2008-08-26 18:03:31 +0000127 // Not yet supported!
128 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000129 }
130
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000131 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000132 if (I != MBB.end()) DL = I->getDebugLoc();
133
Owen Andersond10fd972007-12-31 06:32:00 +0000134 if (DestRC == SP::IntRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000135 BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000136 else if (DestRC == SP::FPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000137 BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000138 else if (DestRC == SP::DFPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000139 BuildMI(MBB, I, DL, get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD),DestReg)
Owen Andersond10fd972007-12-31 06:32:00 +0000140 .addReg(SrcReg);
141 else
Owen Anderson940f83e2008-08-26 18:03:31 +0000142 // Can't copy this register
143 return false;
144
145 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000146}
Owen Andersonf6372aa2008-01-01 21:11:32 +0000147
148void SparcInstrInfo::
149storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
150 unsigned SrcReg, bool isKill, int FI,
151 const TargetRegisterClass *RC) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000152 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000153 if (I != MBB.end()) DL = I->getDebugLoc();
154
Owen Andersonf6372aa2008-01-01 21:11:32 +0000155 // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
156 if (RC == SP::IntRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000157 BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000158 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000159 else if (RC == SP::FPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000160 BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000161 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000162 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000163 BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000164 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000165 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000166 llvm_unreachable("Can't store this register to stack slot");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000167}
168
Owen Andersonf6372aa2008-01-01 21:11:32 +0000169void SparcInstrInfo::
170loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
171 unsigned DestReg, int FI,
172 const TargetRegisterClass *RC) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000173 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000174 if (I != MBB.end()) DL = I->getDebugLoc();
175
Owen Andersonf6372aa2008-01-01 21:11:32 +0000176 if (RC == SP::IntRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000177 BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000178 else if (RC == SP::FPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000179 BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000180 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000181 BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000182 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000183 llvm_unreachable("Can't load this register from stack slot");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000184}
185
Dan Gohmanc54baa22008-12-03 18:43:12 +0000186MachineInstr *SparcInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
187 MachineInstr* MI,
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000188 const SmallVectorImpl<unsigned> &Ops,
Dan Gohmanc54baa22008-12-03 18:43:12 +0000189 int FI) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000190 if (Ops.size() != 1) return NULL;
191
192 unsigned OpNum = Ops[0];
193 bool isFloat = false;
194 MachineInstr *NewMI = NULL;
195 switch (MI->getOpcode()) {
196 case SP::ORrr:
Dan Gohmand735b802008-10-03 15:45:36 +0000197 if (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == SP::G0&&
198 MI->getOperand(0).isReg() && MI->getOperand(2).isReg()) {
Owen Anderson43dbe052008-01-07 01:35:02 +0000199 if (OpNum == 0) // COPY -> STORE
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000200 NewMI = BuildMI(MF, MI->getDebugLoc(), get(SP::STri))
201 .addFrameIndex(FI)
202 .addImm(0)
203 .addReg(MI->getOperand(2).getReg());
Owen Anderson43dbe052008-01-07 01:35:02 +0000204 else // COPY -> LOAD
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000205 NewMI = BuildMI(MF, MI->getDebugLoc(), get(SP::LDri),
206 MI->getOperand(0).getReg())
207 .addFrameIndex(FI)
208 .addImm(0);
Owen Anderson43dbe052008-01-07 01:35:02 +0000209 }
210 break;
211 case SP::FMOVS:
212 isFloat = true;
213 // FALLTHROUGH
214 case SP::FMOVD:
Evan Cheng9f1c8312008-07-03 09:09:37 +0000215 if (OpNum == 0) { // COPY -> STORE
216 unsigned SrcReg = MI->getOperand(1).getReg();
217 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000218 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000219 NewMI = BuildMI(MF, MI->getDebugLoc(),
220 get(isFloat ? SP::STFri : SP::STDFri))
221 .addFrameIndex(FI)
222 .addImm(0)
Evan Cheng2578ba22009-07-01 01:59:31 +0000223 .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef));
Evan Cheng9f1c8312008-07-03 09:09:37 +0000224 } else { // COPY -> LOAD
225 unsigned DstReg = MI->getOperand(0).getReg();
226 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000227 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000228 NewMI = BuildMI(MF, MI->getDebugLoc(),
229 get(isFloat ? SP::LDFri : SP::LDDFri))
Evan Cheng2578ba22009-07-01 01:59:31 +0000230 .addReg(DstReg, RegState::Define |
231 getDeadRegState(isDead) | getUndefRegState(isUndef))
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000232 .addFrameIndex(FI)
233 .addImm(0);
Evan Cheng9f1c8312008-07-03 09:09:37 +0000234 }
Owen Anderson43dbe052008-01-07 01:35:02 +0000235 break;
236 }
237
Owen Anderson43dbe052008-01-07 01:35:02 +0000238 return NewMI;
Duncan Sands9c5525f2008-01-07 19:13:36 +0000239}
Chris Lattnerdb486a62009-09-15 17:46:24 +0000240
241unsigned SparcInstrInfo::getGlobalBaseReg(MachineFunction *MF) const
242{
243 SparcMachineFunctionInfo *SparcFI = MF->getInfo<SparcMachineFunctionInfo>();
244 unsigned GlobalBaseReg = SparcFI->getGlobalBaseReg();
245 if (GlobalBaseReg != 0)
246 return GlobalBaseReg;
247
248 // Insert the set of GlobalBaseReg into the first MBB of the function
249 MachineBasicBlock &FirstMBB = MF->front();
250 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
251 MachineRegisterInfo &RegInfo = MF->getRegInfo();
252
253 GlobalBaseReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
254
255
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000256 DebugLoc dl;
Chris Lattnerdb486a62009-09-15 17:46:24 +0000257
258 BuildMI(FirstMBB, MBBI, dl, get(SP::GETPCX), GlobalBaseReg);
259 SparcFI->setGlobalBaseReg(GlobalBaseReg);
260 return GlobalBaseReg;
261}