blob: e494d7ddf9f8ed6020a1b2d6deb9fcada6be1c55 [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,
Owen Andersond131b5b2008-08-14 22:49:33 +0000112 const SmallVectorImpl<MachineOperand> &Cond)const{
Dale Johannesenb73e4bb2009-02-13 02:31:35 +0000113 // FIXME this should probably take a DebugLoc argument
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000114 DebugLoc dl;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 // Can only insert uncond branches so far.
116 assert(Cond.empty() && !FBB && TBB && "Can only handle uncond branches!");
Dale Johannesenb73e4bb2009-02-13 02:31:35 +0000117 BuildMI(&MBB, dl, get(SP::BA)).addMBB(TBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 return 1;
119}
Owen Anderson8f2c8932007-12-31 06:32:00 +0000120
Owen Anderson9fa72d92008-08-26 18:03:31 +0000121bool SparcInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000122 MachineBasicBlock::iterator I,
123 unsigned DestReg, unsigned SrcReg,
124 const TargetRegisterClass *DestRC,
125 const TargetRegisterClass *SrcRC) const {
Owen Anderson8f2c8932007-12-31 06:32:00 +0000126 if (DestRC != SrcRC) {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000127 // Not yet supported!
128 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000129 }
130
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000131 DebugLoc DL;
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000132 if (I != MBB.end()) DL = I->getDebugLoc();
133
Owen Anderson8f2c8932007-12-31 06:32:00 +0000134 if (DestRC == SP::IntRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000135 BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000136 else if (DestRC == SP::FPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000137 BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg).addReg(SrcReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000138 else if (DestRC == SP::DFPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000139 BuildMI(MBB, I, DL, get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD),DestReg)
Owen Anderson8f2c8932007-12-31 06:32:00 +0000140 .addReg(SrcReg);
141 else
Owen Anderson9fa72d92008-08-26 18:03:31 +0000142 // Can't copy this register
143 return false;
144
145 return true;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000146}
Owen Anderson81875432008-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 Lattnerd2c680b2010-04-02 20:16:16 +0000152 DebugLoc DL;
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000153 if (I != MBB.end()) DL = I->getDebugLoc();
154
Owen Anderson81875432008-01-01 21:11:32 +0000155 // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
156 if (RC == SP::IntRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000157 BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
Bill Wendling2b739762009-05-13 21:33:08 +0000158 .addReg(SrcReg, getKillRegState(isKill));
Owen Anderson81875432008-01-01 21:11:32 +0000159 else if (RC == SP::FPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000160 BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
Bill Wendling2b739762009-05-13 21:33:08 +0000161 .addReg(SrcReg, getKillRegState(isKill));
Owen Anderson81875432008-01-01 21:11:32 +0000162 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000163 BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
Bill Wendling2b739762009-05-13 21:33:08 +0000164 .addReg(SrcReg, getKillRegState(isKill));
Owen Anderson81875432008-01-01 21:11:32 +0000165 else
Edwin Törökbd448e32009-07-14 16:55:14 +0000166 llvm_unreachable("Can't store this register to stack slot");
Owen Anderson81875432008-01-01 21:11:32 +0000167}
168
Owen Anderson81875432008-01-01 21:11:32 +0000169void SparcInstrInfo::
170loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
171 unsigned DestReg, int FI,
172 const TargetRegisterClass *RC) const {
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000173 DebugLoc DL;
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000174 if (I != MBB.end()) DL = I->getDebugLoc();
175
Owen Anderson81875432008-01-01 21:11:32 +0000176 if (RC == SP::IntRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000177 BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
Owen Anderson81875432008-01-01 21:11:32 +0000178 else if (RC == SP::FPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000179 BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Anderson81875432008-01-01 21:11:32 +0000180 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000181 BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Anderson81875432008-01-01 21:11:32 +0000182 else
Edwin Törökbd448e32009-07-14 16:55:14 +0000183 llvm_unreachable("Can't load this register from stack slot");
Owen Anderson81875432008-01-01 21:11:32 +0000184}
185
Dan Gohmanedc83d62008-12-03 18:43:12 +0000186MachineInstr *SparcInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
187 MachineInstr* MI,
Dan Gohman46b948e2008-10-16 01:49:15 +0000188 const SmallVectorImpl<unsigned> &Ops,
Dan Gohmanedc83d62008-12-03 18:43:12 +0000189 int FI) const {
Owen Anderson9a184ef2008-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 Gohmanb9f4fa72008-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 Anderson9a184ef2008-01-07 01:35:02 +0000199 if (OpNum == 0) // COPY -> STORE
Bill Wendling5b8a97b2009-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 Anderson9a184ef2008-01-07 01:35:02 +0000204 else // COPY -> LOAD
Bill Wendling5b8a97b2009-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 Anderson9a184ef2008-01-07 01:35:02 +0000209 }
210 break;
211 case SP::FMOVS:
212 isFloat = true;
213 // FALLTHROUGH
214 case SP::FMOVD:
Evan Chenge52c1912008-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 Cheng65219822009-07-01 01:59:31 +0000218 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendling5b8a97b2009-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 Cheng65219822009-07-01 01:59:31 +0000223 .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef));
Evan Chenge52c1912008-07-03 09:09:37 +0000224 } else { // COPY -> LOAD
225 unsigned DstReg = MI->getOperand(0).getReg();
226 bool isDead = MI->getOperand(0).isDead();
Evan Cheng65219822009-07-01 01:59:31 +0000227 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000228 NewMI = BuildMI(MF, MI->getDebugLoc(),
229 get(isFloat ? SP::LDFri : SP::LDDFri))
Evan Cheng65219822009-07-01 01:59:31 +0000230 .addReg(DstReg, RegState::Define |
231 getDeadRegState(isDead) | getUndefRegState(isUndef))
Bill Wendling5b8a97b2009-02-12 00:02:55 +0000232 .addFrameIndex(FI)
233 .addImm(0);
Evan Chenge52c1912008-07-03 09:09:37 +0000234 }
Owen Anderson9a184ef2008-01-07 01:35:02 +0000235 break;
236 }
237
Owen Anderson9a184ef2008-01-07 01:35:02 +0000238 return NewMI;
Duncan Sands8ae88772008-01-07 19:13:36 +0000239}
Chris Lattner49102de2009-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 Lattnerd2c680b2010-04-02 20:16:16 +0000256 DebugLoc dl;
Chris Lattner49102de2009-09-15 17:46:24 +0000257
258 BuildMI(FirstMBB, MBBI, dl, get(SP::GETPCX), GlobalBaseReg);
259 SparcFI->setGlobalBaseReg(GlobalBaseReg);
260 return GlobalBaseReg;
261}