blob: 7ede8e7ebbe469148e4b8ae5012e23aa1aae5fb9 [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
Chris Lattner158e1f52006-02-05 05:50:24 +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 Gohman0b273252008-11-18 19:49:32 +000036unsigned SparcInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner158e1f52006-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 Gohman0d1e9a82008-10-03 15:45:36 +000041 if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
Chris Lattner5c463782007-12-30 20:49:49 +000042 MI->getOperand(2).getImm() == 0) {
Chris Lattnera5bb3702007-12-30 23:10:15 +000043 FrameIndex = MI->getOperand(1).getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +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 Gohman0b273252008-11-18 19:49:32 +000055unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner158e1f52006-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 Gohman0d1e9a82008-10-03 15:45:36 +000060 if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
Chris Lattner5c463782007-12-30 20:49:49 +000061 MI->getOperand(1).getImm() == 0) {
Chris Lattnera5bb3702007-12-30 23:10:15 +000062 FrameIndex = MI->getOperand(0).getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +000063 return MI->getOperand(2).getReg();
64 }
65 }
66 return 0;
67}
Chris Lattnerb7267bd2006-10-24 16:39:19 +000068
Evan Chenge20dd922007-05-18 00:18:17 +000069unsigned
70SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
71 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +000072 const SmallVectorImpl<MachineOperand> &Cond,
73 DebugLoc DL)const{
Chris Lattnerb7267bd2006-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 Hastings0125b642010-06-17 22:43:56 +000076 BuildMI(&MBB, DL, get(SP::BA)).addMBB(TBB);
Evan Chenge20dd922007-05-18 00:18:17 +000077 return 1;
Rafael Espindolaed328832006-10-24 17:07:11 +000078}
Owen Anderson7a73ae92007-12-31 06:32:00 +000079
Jakob Stoklund Olesen976b7b62010-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 Anderson7a73ae92007-12-31 06:32:00 +000093 else
Jakob Stoklund Olesen976b7b62010-07-11 07:56:09 +000094 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +000095}
Owen Andersoneee14602008-01-01 21:11:32 +000096
97void SparcInstrInfo::
98storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
99 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000100 const TargetRegisterClass *RC,
101 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000102 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000103 if (I != MBB.end()) DL = I->getDebugLoc();
104
Owen Andersoneee14602008-01-01 21:11:32 +0000105 // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
106 if (RC == SP::IntRegsRegisterClass)
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000107 BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000108 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersoneee14602008-01-01 21:11:32 +0000109 else if (RC == SP::FPRegsRegisterClass)
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000110 BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000111 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersoneee14602008-01-01 21:11:32 +0000112 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000113 BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000114 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersoneee14602008-01-01 21:11:32 +0000115 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000116 llvm_unreachable("Can't store this register to stack slot");
Owen Andersoneee14602008-01-01 21:11:32 +0000117}
118
Owen Andersoneee14602008-01-01 21:11:32 +0000119void SparcInstrInfo::
120loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
121 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000122 const TargetRegisterClass *RC,
123 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000124 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000125 if (I != MBB.end()) DL = I->getDebugLoc();
126
Owen Andersoneee14602008-01-01 21:11:32 +0000127 if (RC == SP::IntRegsRegisterClass)
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000128 BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersoneee14602008-01-01 21:11:32 +0000129 else if (RC == SP::FPRegsRegisterClass)
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000130 BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersoneee14602008-01-01 21:11:32 +0000131 else if (RC == SP::DFPRegsRegisterClass)
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000132 BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
Owen Andersoneee14602008-01-01 21:11:32 +0000133 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000134 llvm_unreachable("Can't load this register from stack slot");
Owen Andersoneee14602008-01-01 21:11:32 +0000135}
136
Chris Lattner840c7002009-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 Lattner6f306d72010-04-02 20:16:16 +0000152 DebugLoc dl;
Chris Lattner840c7002009-09-15 17:46:24 +0000153
154 BuildMI(FirstMBB, MBBI, dl, get(SP::GETPCX), GlobalBaseReg);
155 SparcFI->setGlobalBaseReg(GlobalBaseReg);
156 return GlobalBaseReg;
157}