blob: 6f2ff6dcdd1679e49f13fc3a95d574fd99d5f3fc [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"
Chris Lattner49102de2009-09-15 17:46:24 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Edwin Török675d5622009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "SparcGenInstrInfo.inc"
Chris Lattner49102de2009-09-15 17:46:24 +000023#include "SparcMachineFunctionInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024using namespace llvm;
25
26SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST)
Chris Lattnerd2fd6db2008-01-01 01:03:04 +000027 : TargetInstrInfoImpl(SparcInsts, array_lengthof(SparcInsts)),
Owen Anderson8f2c8932007-12-31 06:32:00 +000028 RI(ST, *this), Subtarget(ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029}
30
31static bool isZeroImm(const MachineOperand &op) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000032 return op.isImm() && op.getImm() == 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033}
34
35/// Return true if the instruction is a register to register move and
36/// leave the source and dest operands in the passed parameters.
37///
38bool SparcInstrInfo::isMoveInstr(const MachineInstr &MI,
Evan Chengf97496a2009-01-20 19:12:24 +000039 unsigned &SrcReg, unsigned &DstReg,
40 unsigned &SrcSR, unsigned &DstSR) const {
41 SrcSR = DstSR = 0; // No sub-registers.
42
Dan Gohmanf17a25c2007-07-18 16:29:46 +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).
47 if (MI.getOpcode() == SP::ORrr || MI.getOpcode() == SP::ADDrr) {
48 if (MI.getOperand(1).getReg() == SP::G0) {
49 DstReg = MI.getOperand(0).getReg();
50 SrcReg = MI.getOperand(2).getReg();
51 return true;
52 } else if (MI.getOperand(2).getReg() == SP::G0) {
53 DstReg = MI.getOperand(0).getReg();
54 SrcReg = MI.getOperand(1).getReg();
55 return true;
56 }
57 } else if ((MI.getOpcode() == SP::ORri || MI.getOpcode() == SP::ADDri) &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000058 isZeroImm(MI.getOperand(2)) && MI.getOperand(1).isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 DstReg = MI.getOperand(0).getReg();
60 SrcReg = MI.getOperand(1).getReg();
61 return true;
62 } else if (MI.getOpcode() == SP::FMOVS || MI.getOpcode() == SP::FpMOVD ||
63 MI.getOpcode() == SP::FMOVD) {
64 SrcReg = MI.getOperand(1).getReg();
65 DstReg = MI.getOperand(0).getReg();
66 return true;
67 }
68 return false;
69}
70
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 Gohman90feee22008-11-18 19:49:32 +000076unsigned SparcInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 int &FrameIndex) const {
78 if (MI->getOpcode() == SP::LDri ||
79 MI->getOpcode() == SP::LDFri ||
80 MI->getOpcode() == SP::LDDFri) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000081 if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
Chris Lattnera96056a2007-12-30 20:49:49 +000082 MI->getOperand(2).getImm() == 0) {
Chris Lattner6017d482007-12-30 23:10:15 +000083 FrameIndex = MI->getOperand(1).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Gohman90feee22008-11-18 19:49:32 +000095unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 int &FrameIndex) const {
97 if (MI->getOpcode() == SP::STri ||
98 MI->getOpcode() == SP::STFri ||
99 MI->getOpcode() == SP::STDFri) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000100 if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
Chris Lattnera96056a2007-12-30 20:49:49 +0000101 MI->getOperand(1).getImm() == 0) {
Chris Lattner6017d482007-12-30 23:10:15 +0000102 FrameIndex = MI->getOperand(0).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 return MI->getOperand(2).getReg();
104 }
105 }
106 return 0;
107}
108
109unsigned
110SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
111 MachineBasicBlock *FBB,
Stuart Hastings9fa5e332010-06-17 22:43:56 +0000112 const SmallVectorImpl<MachineOperand> &Cond,
113 DebugLoc DL)const{
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 // Can only insert uncond branches so far.
115 assert(Cond.empty() && !FBB && TBB && "Can only handle uncond branches!");
Stuart Hastings9fa5e332010-06-17 22:43:56 +0000116 BuildMI(&MBB, DL, get(SP::BA)).addMBB(TBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 return 1;
118}
Owen Anderson8f2c8932007-12-31 06:32:00 +0000119
Jakob Stoklund Olesen8473ef22010-07-11 07:56:09 +0000120void SparcInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
121 MachineBasicBlock::iterator I, DebugLoc DL,
122 unsigned DestReg, unsigned SrcReg,
123 bool KillSrc) const {
124 if (SP::IntRegsRegClass.contains(DestReg, SrcReg))
125 BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0)
126 .addReg(SrcReg, getKillRegState(KillSrc));
127 else if (SP::FPRegsRegClass.contains(DestReg, SrcReg))
128 BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg)
129 .addReg(SrcReg, getKillRegState(KillSrc));
130 else if (SP::DFPRegsRegClass.contains(DestReg, SrcReg))
131 BuildMI(MBB, I, DL, get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD), DestReg)
132 .addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson8f2c8932007-12-31 06:32:00 +0000133 else
Jakob Stoklund Olesen8473ef22010-07-11 07:56:09 +0000134 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson8f2c8932007-12-31 06:32:00 +0000135}
Owen Anderson81875432008-01-01 21:11:32 +0000136
137void SparcInstrInfo::
138storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
139 unsigned SrcReg, bool isKill, int FI,
Evan Cheng1f8534d2010-05-06 19:06:44 +0000140 const TargetRegisterClass *RC,
141 const TargetRegisterInfo *TRI) const {
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000142 DebugLoc DL;
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000143 if (I != MBB.end()) DL = I->getDebugLoc();
144
Owen Anderson81875432008-01-01 21:11:32 +0000145 // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
146 if (RC == SP::IntRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000147 BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
Bill Wendling2b739762009-05-13 21:33:08 +0000148 .addReg(SrcReg, getKillRegState(isKill));
Owen Anderson81875432008-01-01 21:11:32 +0000149 else if (RC == SP::FPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000150 BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
Bill Wendling2b739762009-05-13 21:33:08 +0000151 .addReg(SrcReg, getKillRegState(isKill));
Owen Anderson81875432008-01-01 21:11:32 +0000152 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000153 BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
Bill Wendling2b739762009-05-13 21:33:08 +0000154 .addReg(SrcReg, getKillRegState(isKill));
Owen Anderson81875432008-01-01 21:11:32 +0000155 else
Edwin Törökbd448e32009-07-14 16:55:14 +0000156 llvm_unreachable("Can't store this register to stack slot");
Owen Anderson81875432008-01-01 21:11:32 +0000157}
158
Owen Anderson81875432008-01-01 21:11:32 +0000159void SparcInstrInfo::
160loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
161 unsigned DestReg, int FI,
Evan Cheng1f8534d2010-05-06 19:06:44 +0000162 const TargetRegisterClass *RC,
163 const TargetRegisterInfo *TRI) const {
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000164 DebugLoc DL;
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000165 if (I != MBB.end()) DL = I->getDebugLoc();
166
Owen Anderson81875432008-01-01 21:11:32 +0000167 if (RC == SP::IntRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000168 BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
Owen Anderson81875432008-01-01 21:11:32 +0000169 else if (RC == SP::FPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000170 BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Anderson81875432008-01-01 21:11:32 +0000171 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000172 BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Anderson81875432008-01-01 21:11:32 +0000173 else
Edwin Törökbd448e32009-07-14 16:55:14 +0000174 llvm_unreachable("Can't load this register from stack slot");
Owen Anderson81875432008-01-01 21:11:32 +0000175}
176
Dan Gohmanedc83d62008-12-03 18:43:12 +0000177MachineInstr *SparcInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
178 MachineInstr* MI,
Dan Gohman46b948e2008-10-16 01:49:15 +0000179 const SmallVectorImpl<unsigned> &Ops,
Dan Gohmanedc83d62008-12-03 18:43:12 +0000180 int FI) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000181 if (Ops.size() != 1) return NULL;
182
183 unsigned OpNum = Ops[0];
184 bool isFloat = false;
185 MachineInstr *NewMI = NULL;
186 switch (MI->getOpcode()) {
187 case SP::ORrr:
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000188 if (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == SP::G0&&
189 MI->getOperand(0).isReg() && MI->getOperand(2).isReg()) {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000190 if (OpNum == 0) // COPY -> STORE
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000191 NewMI = BuildMI(MF, MI->getDebugLoc(), get(SP::STri))
192 .addFrameIndex(FI)
193 .addImm(0)
194 .addReg(MI->getOperand(2).getReg());
Owen Anderson9a184ef2008-01-07 01:35:02 +0000195 else // COPY -> LOAD
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000196 NewMI = BuildMI(MF, MI->getDebugLoc(), get(SP::LDri),
197 MI->getOperand(0).getReg())
198 .addFrameIndex(FI)
199 .addImm(0);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000200 }
201 break;
202 case SP::FMOVS:
203 isFloat = true;
204 // FALLTHROUGH
205 case SP::FMOVD:
Evan Chenge52c1912008-07-03 09:09:37 +0000206 if (OpNum == 0) { // COPY -> STORE
207 unsigned SrcReg = MI->getOperand(1).getReg();
208 bool isKill = MI->getOperand(1).isKill();
Evan Cheng65219822009-07-01 01:59:31 +0000209 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000210 NewMI = BuildMI(MF, MI->getDebugLoc(),
211 get(isFloat ? SP::STFri : SP::STDFri))
212 .addFrameIndex(FI)
213 .addImm(0)
Evan Cheng65219822009-07-01 01:59:31 +0000214 .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef));
Evan Chenge52c1912008-07-03 09:09:37 +0000215 } else { // COPY -> LOAD
216 unsigned DstReg = MI->getOperand(0).getReg();
217 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000218 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000219 NewMI = BuildMI(MF, MI->getDebugLoc(),
220 get(isFloat ? SP::LDFri : SP::LDDFri))
Evan Cheng65219822009-07-01 01:59:31 +0000221 .addReg(DstReg, RegState::Define |
222 getDeadRegState(isDead) | getUndefRegState(isUndef))
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000223 .addFrameIndex(FI)
224 .addImm(0);
Evan Chenge52c1912008-07-03 09:09:37 +0000225 }
Owen Anderson9a184ef2008-01-07 01:35:02 +0000226 break;
227 }
228
Owen Anderson9a184ef2008-01-07 01:35:02 +0000229 return NewMI;
Duncan Sands8ae88772008-01-07 19:13:36 +0000230}
Chris Lattner49102de2009-09-15 17:46:24 +0000231
232unsigned SparcInstrInfo::getGlobalBaseReg(MachineFunction *MF) const
233{
234 SparcMachineFunctionInfo *SparcFI = MF->getInfo<SparcMachineFunctionInfo>();
235 unsigned GlobalBaseReg = SparcFI->getGlobalBaseReg();
236 if (GlobalBaseReg != 0)
237 return GlobalBaseReg;
238
239 // Insert the set of GlobalBaseReg into the first MBB of the function
240 MachineBasicBlock &FirstMBB = MF->front();
241 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
242 MachineRegisterInfo &RegInfo = MF->getRegInfo();
243
244 GlobalBaseReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
245
246
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000247 DebugLoc dl;
Chris Lattner49102de2009-09-15 17:46:24 +0000248
249 BuildMI(FirstMBB, MBBI, dl, get(SP::GETPCX), GlobalBaseReg);
250 SparcFI->setGlobalBaseReg(GlobalBaseReg);
251 return GlobalBaseReg;
252}