blob: 7ede8e7ebbe469148e4b8ae5012e23aa1aae5fb9 [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 Lattner5ccc7222006-02-03 06:44:54 +000031/// isLoadFromStackSlot - If the specified machine instruction is a direct
32/// load from a stack slot, return the virtual or physical register number of
33/// the destination along with the FrameIndex of the loaded stack slot. If
34/// not, return 0. This predicate must return 0 if the instruction has
35/// any side effects other than loading from the stack slot.
Dan Gohmancbad42c2008-11-18 19:49:32 +000036unsigned SparcInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner7c90f732006-02-05 05:50:24 +000037 int &FrameIndex) const {
38 if (MI->getOpcode() == SP::LDri ||
39 MI->getOpcode() == SP::LDFri ||
40 MI->getOpcode() == SP::LDDFri) {
Dan Gohmand735b802008-10-03 15:45:36 +000041 if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000042 MI->getOperand(2).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000043 FrameIndex = MI->getOperand(1).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +000044 return MI->getOperand(0).getReg();
45 }
46 }
47 return 0;
48}
49
50/// isStoreToStackSlot - If the specified machine instruction is a direct
51/// store to a stack slot, return the virtual or physical register number of
52/// the source reg along with the FrameIndex of the loaded stack slot. If
53/// not, return 0. This predicate must return 0 if the instruction has
54/// any side effects other than storing to the stack slot.
Dan Gohmancbad42c2008-11-18 19:49:32 +000055unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner7c90f732006-02-05 05:50:24 +000056 int &FrameIndex) const {
57 if (MI->getOpcode() == SP::STri ||
58 MI->getOpcode() == SP::STFri ||
59 MI->getOpcode() == SP::STDFri) {
Dan Gohmand735b802008-10-03 15:45:36 +000060 if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000061 MI->getOperand(1).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000062 FrameIndex = MI->getOperand(0).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +000063 return MI->getOperand(2).getReg();
64 }
65 }
66 return 0;
67}
Chris Lattnere87146a2006-10-24 16:39:19 +000068
Evan Cheng6ae36262007-05-18 00:18:17 +000069unsigned
70SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
71 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +000072 const SmallVectorImpl<MachineOperand> &Cond,
73 DebugLoc DL)const{
Chris Lattnere87146a2006-10-24 16:39:19 +000074 // Can only insert uncond branches so far.
75 assert(Cond.empty() && !FBB && TBB && "Can only handle uncond branches!");
Stuart Hastings3bf91252010-06-17 22:43:56 +000076 BuildMI(&MBB, DL, get(SP::BA)).addMBB(TBB);
Evan Cheng6ae36262007-05-18 00:18:17 +000077 return 1;
Rafael Espindola3d7d39a2006-10-24 17:07:11 +000078}
Owen Andersond10fd972007-12-31 06:32:00 +000079
Jakob Stoklund Olesen8e18a1a2010-07-11 07:56:09 +000080void SparcInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
81 MachineBasicBlock::iterator I, DebugLoc DL,
82 unsigned DestReg, unsigned SrcReg,
83 bool KillSrc) const {
84 if (SP::IntRegsRegClass.contains(DestReg, SrcReg))
85 BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0)
86 .addReg(SrcReg, getKillRegState(KillSrc));
87 else if (SP::FPRegsRegClass.contains(DestReg, SrcReg))
88 BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg)
89 .addReg(SrcReg, getKillRegState(KillSrc));
90 else if (SP::DFPRegsRegClass.contains(DestReg, SrcReg))
91 BuildMI(MBB, I, DL, get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD), DestReg)
92 .addReg(SrcReg, getKillRegState(KillSrc));
Owen Andersond10fd972007-12-31 06:32:00 +000093 else
Jakob Stoklund Olesen8e18a1a2010-07-11 07:56:09 +000094 llvm_unreachable("Impossible reg-to-reg copy");
Owen Andersond10fd972007-12-31 06:32:00 +000095}
Owen Andersonf6372aa2008-01-01 21:11:32 +000096
97void SparcInstrInfo::
98storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
99 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000100 const TargetRegisterClass *RC,
101 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000102 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000103 if (I != MBB.end()) DL = I->getDebugLoc();
104
Owen Andersonf6372aa2008-01-01 21:11:32 +0000105 // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
106 if (RC == SP::IntRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000107 BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000108 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000109 else if (RC == SP::FPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000110 BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000111 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000112 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000113 BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
Bill Wendling587daed2009-05-13 21:33:08 +0000114 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000115 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000116 llvm_unreachable("Can't store this register to stack slot");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000117}
118
Owen Andersonf6372aa2008-01-01 21:11:32 +0000119void SparcInstrInfo::
120loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
121 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000122 const TargetRegisterClass *RC,
123 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000124 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000125 if (I != MBB.end()) DL = I->getDebugLoc();
126
Owen Andersonf6372aa2008-01-01 21:11:32 +0000127 if (RC == SP::IntRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000128 BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000129 else if (RC == SP::FPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000130 BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000131 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000132 BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000133 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000134 llvm_unreachable("Can't load this register from stack slot");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000135}
136
Chris Lattnerdb486a62009-09-15 17:46:24 +0000137unsigned SparcInstrInfo::getGlobalBaseReg(MachineFunction *MF) const
138{
139 SparcMachineFunctionInfo *SparcFI = MF->getInfo<SparcMachineFunctionInfo>();
140 unsigned GlobalBaseReg = SparcFI->getGlobalBaseReg();
141 if (GlobalBaseReg != 0)
142 return GlobalBaseReg;
143
144 // Insert the set of GlobalBaseReg into the first MBB of the function
145 MachineBasicBlock &FirstMBB = MF->front();
146 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
147 MachineRegisterInfo &RegInfo = MF->getRegInfo();
148
149 GlobalBaseReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
150
151
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000152 DebugLoc dl;
Chris Lattnerdb486a62009-09-15 17:46:24 +0000153
154 BuildMI(FirstMBB, MBBI, dl, get(SP::GETPCX), GlobalBaseReg);
155 SparcFI->setGlobalBaseReg(GlobalBaseReg);
156 return GlobalBaseReg;
157}