blob: 3a4c80ad076a46d11149dc189a6f8051e9e184eb [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
Chris Lattner49102de2009-09-15 17:46:24 +0000177unsigned SparcInstrInfo::getGlobalBaseReg(MachineFunction *MF) const
178{
179 SparcMachineFunctionInfo *SparcFI = MF->getInfo<SparcMachineFunctionInfo>();
180 unsigned GlobalBaseReg = SparcFI->getGlobalBaseReg();
181 if (GlobalBaseReg != 0)
182 return GlobalBaseReg;
183
184 // Insert the set of GlobalBaseReg into the first MBB of the function
185 MachineBasicBlock &FirstMBB = MF->front();
186 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
187 MachineRegisterInfo &RegInfo = MF->getRegInfo();
188
189 GlobalBaseReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
190
191
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000192 DebugLoc dl;
Chris Lattner49102de2009-09-15 17:46:24 +0000193
194 BuildMI(FirstMBB, MBBI, dl, get(SP::GETPCX), GlobalBaseReg);
195 SparcFI->setGlobalBaseReg(GlobalBaseReg);
196 return GlobalBaseReg;
197}