blob: 6f2ff6dcdd1679e49f13fc3a95d574fd99d5f3fc [file] [log] [blame]
Chris Lattner158e1f52006-02-05 05:50:24 +00001//===- SparcInstrInfo.cpp - Sparc Instruction Information -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner158e1f52006-02-05 05:50:24 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Sparc implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SparcInstrInfo.h"
Owen Anderson7a73ae92007-12-31 06:32:00 +000015#include "SparcSubtarget.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000016#include "Sparc.h"
Owen Andersone2f23a32007-09-07 04:06:50 +000017#include "llvm/ADT/STLExtras.h"
Dan Gohman906152a2009-01-05 17:59:02 +000018#include "llvm/ADT/SmallVector.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner840c7002009-09-15 17:46:24 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwin56d06592009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000022#include "SparcGenInstrInfo.inc"
Chris Lattner840c7002009-09-15 17:46:24 +000023#include "SparcMachineFunctionInfo.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000024using namespace llvm;
25
26SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST)
Chris Lattner25568e42008-01-01 01:03:04 +000027 : TargetInstrInfoImpl(SparcInsts, array_lengthof(SparcInsts)),
Owen Anderson7a73ae92007-12-31 06:32:00 +000028 RI(ST, *this), Subtarget(ST) {
Chris Lattner158e1f52006-02-05 05:50:24 +000029}
30
31static bool isZeroImm(const MachineOperand &op) {
Dan Gohman0d1e9a82008-10-03 15:45:36 +000032 return op.isImm() && op.getImm() == 0;
Chris Lattner158e1f52006-02-05 05:50:24 +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 Chengc544cb02009-01-20 19:12:24 +000039 unsigned &SrcReg, unsigned &DstReg,
40 unsigned &SrcSR, unsigned &DstSR) const {
41 SrcSR = DstSR = 0; // No sub-registers.
42
Chris Lattner158e1f52006-02-05 05:50:24 +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 Gohman0d1e9a82008-10-03 15:45:36 +000058 isZeroImm(MI.getOperand(2)) && MI.getOperand(1).isReg()) {
Chris Lattner158e1f52006-02-05 05:50:24 +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 Gohman0b273252008-11-18 19:49:32 +000076unsigned SparcInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner158e1f52006-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 Gohman0d1e9a82008-10-03 15:45:36 +000081 if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
Chris Lattner5c463782007-12-30 20:49:49 +000082 MI->getOperand(2).getImm() == 0) {
Chris Lattnera5bb3702007-12-30 23:10:15 +000083 FrameIndex = MI->getOperand(1).getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +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 Gohman0b273252008-11-18 19:49:32 +000095unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner158e1f52006-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 Gohman0d1e9a82008-10-03 15:45:36 +0000100 if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
Chris Lattner5c463782007-12-30 20:49:49 +0000101 MI->getOperand(1).getImm() == 0) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000102 FrameIndex = MI->getOperand(0).getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +0000103 return MI->getOperand(2).getReg();
104 }
105 }
106 return 0;
107}
Chris Lattnerb7267bd2006-10-24 16:39:19 +0000108
Evan Chenge20dd922007-05-18 00:18:17 +0000109unsigned
110SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
111 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000112 const SmallVectorImpl<MachineOperand> &Cond,
113 DebugLoc DL)const{
Chris Lattnerb7267bd2006-10-24 16:39:19 +0000114 // Can only insert uncond branches so far.
115 assert(Cond.empty() && !FBB && TBB && "Can only handle uncond branches!");
Stuart Hastings0125b642010-06-17 22:43:56 +0000116 BuildMI(&MBB, DL, get(SP::BA)).addMBB(TBB);
Evan Chenge20dd922007-05-18 00:18:17 +0000117 return 1;
Rafael Espindolaed328832006-10-24 17:07:11 +0000118}
Owen Anderson7a73ae92007-12-31 06:32:00 +0000119
Jakob Stoklund Olesen976b7b62010-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 Anderson7a73ae92007-12-31 06:32:00 +0000133 else
Jakob Stoklund Olesen976b7b62010-07-11 07:56:09 +0000134 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000135}
Owen Andersoneee14602008-01-01 21:11:32 +0000136
137void SparcInstrInfo::
138storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
139 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000140 const TargetRegisterClass *RC,
141 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000142 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000143 if (I != MBB.end()) DL = I->getDebugLoc();
144
Owen Andersoneee14602008-01-01 21:11:32 +0000145 // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
146 if (RC == SP::IntRegsRegisterClass)
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000147 BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000148 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersoneee14602008-01-01 21:11:32 +0000149 else if (RC == SP::FPRegsRegisterClass)
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000150 BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000151 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersoneee14602008-01-01 21:11:32 +0000152 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000153 BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000154 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersoneee14602008-01-01 21:11:32 +0000155 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000156 llvm_unreachable("Can't store this register to stack slot");
Owen Andersoneee14602008-01-01 21:11:32 +0000157}
158
Owen Andersoneee14602008-01-01 21:11:32 +0000159void SparcInstrInfo::
160loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
161 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000162 const TargetRegisterClass *RC,
163 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000164 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000165 if (I != MBB.end()) DL = I->getDebugLoc();
166
Owen Andersoneee14602008-01-01 21:11:32 +0000167 if (RC == SP::IntRegsRegisterClass)
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000168 BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersoneee14602008-01-01 21:11:32 +0000169 else if (RC == SP::FPRegsRegisterClass)
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000170 BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersoneee14602008-01-01 21:11:32 +0000171 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000172 BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersoneee14602008-01-01 21:11:32 +0000173 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000174 llvm_unreachable("Can't load this register from stack slot");
Owen Andersoneee14602008-01-01 21:11:32 +0000175}
176
Dan Gohman3f86b512008-12-03 18:43:12 +0000177MachineInstr *SparcInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
178 MachineInstr* MI,
Dan Gohman33332bc2008-10-16 01:49:15 +0000179 const SmallVectorImpl<unsigned> &Ops,
Dan Gohman3f86b512008-12-03 18:43:12 +0000180 int FI) const {
Owen Anderson2a3be7b2008-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 Gohman0d1e9a82008-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 Anderson2a3be7b2008-01-07 01:35:02 +0000190 if (OpNum == 0) // COPY -> STORE
Bill Wendlingf6d609a2009-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 Anderson2a3be7b2008-01-07 01:35:02 +0000195 else // COPY -> LOAD
Bill Wendlingf6d609a2009-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 Anderson2a3be7b2008-01-07 01:35:02 +0000200 }
201 break;
202 case SP::FMOVS:
203 isFloat = true;
204 // FALLTHROUGH
205 case SP::FMOVD:
Evan Cheng7d98a482008-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 Chengd379e892009-07-01 01:59:31 +0000209 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendlingf6d609a2009-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 Chengd379e892009-07-01 01:59:31 +0000214 .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef));
Evan Cheng7d98a482008-07-03 09:09:37 +0000215 } else { // COPY -> LOAD
216 unsigned DstReg = MI->getOperand(0).getReg();
217 bool isDead = MI->getOperand(0).isDead();
Evan Chengd379e892009-07-01 01:59:31 +0000218 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000219 NewMI = BuildMI(MF, MI->getDebugLoc(),
220 get(isFloat ? SP::LDFri : SP::LDDFri))
Evan Chengd379e892009-07-01 01:59:31 +0000221 .addReg(DstReg, RegState::Define |
222 getDeadRegState(isDead) | getUndefRegState(isUndef))
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000223 .addFrameIndex(FI)
224 .addImm(0);
Evan Cheng7d98a482008-07-03 09:09:37 +0000225 }
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000226 break;
227 }
228
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000229 return NewMI;
Duncan Sandsd19a6f42008-01-07 19:13:36 +0000230}
Chris Lattner840c7002009-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 Lattner6f306d72010-04-02 20:16:16 +0000247 DebugLoc dl;
Chris Lattner840c7002009-09-15 17:46:24 +0000248
249 BuildMI(FirstMBB, MBBI, dl, get(SP::GETPCX), GlobalBaseReg);
250 SparcFI->setGlobalBaseReg(GlobalBaseReg);
251 return GlobalBaseReg;
252}