blob: 458e4f7d1fae893284e15c4b6053906dc8ef72ed [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55:11 +00001//===-- MipsInstrInfo.cpp - Mips Instruction Information ------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// 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.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file contains the Mips implementation of the TargetInstrInfo class.
11//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000013
Akira Hatanakad4b48b22012-06-14 01:16:45 +000014#include "MipsAnalyzeImmediate.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "MipsInstrInfo.h"
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000016#include "MipsTargetMachine.h"
Dan Gohman99114052009-06-03 20:30:14 +000017#include "MipsMachineFunction.h"
Akira Hatanaka794bf172011-07-07 23:56:50 +000018#include "InstPrinter/MipsInstPrinter.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman99114052009-06-03 20:30:14 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000022#include "llvm/Support/TargetRegistry.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000023#include "llvm/ADT/STLExtras.h"
Evan Cheng22fee2d2011-06-28 20:07:07 +000024
Evan Cheng4db3cff2011-07-01 17:57:27 +000025#define GET_INSTRINFO_CTOR
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000026#include "MipsGenInstrInfo.inc"
27
28using namespace llvm;
29
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000030MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
Evan Cheng4db3cff2011-07-01 17:57:27 +000031 : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
Akira Hatanaka43aed322011-10-11 00:37:28 +000032 TM(tm), IsN64(TM.getSubtarget<MipsSubtarget>().isABI_N64()),
Akira Hatanakab7dd9fc2012-07-21 02:15:19 +000033 InMips16Mode(TM.getSubtarget<MipsSubtarget>().inMips16Mode()),
Akira Hatanaka6e55ff52011-12-12 22:39:35 +000034 RI(*TM.getSubtargetImpl(), *this),
35 UncondBrOpc(TM.getRelocationModel() == Reloc::PIC_ ? Mips::B : Mips::J) {}
Akira Hatanaka794bf172011-07-07 23:56:50 +000036
Jia Liubb481f82012-02-28 07:46:26 +000037const MipsRegisterInfo &MipsInstrInfo::getRegisterInfo() const {
Akira Hatanaka794bf172011-07-07 23:56:50 +000038 return RI;
39}
40
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000041static bool isZeroImm(const MachineOperand &op) {
Dan Gohmand735b802008-10-03 15:45:36 +000042 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000043}
44
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000045/// isLoadFromStackSlot - If the specified machine instruction is a direct
46/// load from a stack slot, return the virtual or physical register number of
47/// the destination along with the FrameIndex of the loaded stack slot. If
48/// not, return 0. This predicate must return 0 if the instruction has
49/// any side effects other than loading from the stack slot.
50unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000051isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000052{
Akira Hatanaka1acb7df2011-10-11 01:12:52 +000053 unsigned Opc = MI->getOpcode();
54
55 if ((Opc == Mips::LW) || (Opc == Mips::LW_P8) || (Opc == Mips::LD) ||
56 (Opc == Mips::LD_P8) || (Opc == Mips::LWC1) || (Opc == Mips::LWC1_P8) ||
57 (Opc == Mips::LDC1) || (Opc == Mips::LDC164) ||
58 (Opc == Mips::LDC164_P8)) {
Akira Hatanakad3ac47f2011-07-07 18:57:00 +000059 if ((MI->getOperand(1).isFI()) && // is a stack slot
60 (MI->getOperand(2).isImm()) && // the imm is zero
61 (isZeroImm(MI->getOperand(2)))) {
62 FrameIndex = MI->getOperand(1).getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000063 return MI->getOperand(0).getReg();
64 }
65 }
66
67 return 0;
68}
69
70/// isStoreToStackSlot - If the specified machine instruction is a direct
71/// store to a stack slot, return the virtual or physical register number of
72/// the source reg along with the FrameIndex of the loaded stack slot. If
73/// not, return 0. This predicate must return 0 if the instruction has
74/// any side effects other than storing to the stack slot.
75unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000076isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000077{
Akira Hatanaka1acb7df2011-10-11 01:12:52 +000078 unsigned Opc = MI->getOpcode();
79
80 if ((Opc == Mips::SW) || (Opc == Mips::SW_P8) || (Opc == Mips::SD) ||
81 (Opc == Mips::SD_P8) || (Opc == Mips::SWC1) || (Opc == Mips::SWC1_P8) ||
82 (Opc == Mips::SDC1) || (Opc == Mips::SDC164) ||
83 (Opc == Mips::SDC164_P8)) {
Akira Hatanakad3ac47f2011-07-07 18:57:00 +000084 if ((MI->getOperand(1).isFI()) && // is a stack slot
85 (MI->getOperand(2).isImm()) && // the imm is zero
86 (isZeroImm(MI->getOperand(2)))) {
87 FrameIndex = MI->getOperand(1).getIndex();
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +000088 return MI->getOperand(0).getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000089 }
90 }
91 return 0;
92}
93
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000094/// insertNoop - If data hazard condition is found insert the target nop
95/// instruction.
96void MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000097insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000098{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000099 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000100 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000101}
102
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000103void MipsInstrInfo::
104copyPhysReg(MachineBasicBlock &MBB,
105 MachineBasicBlock::iterator I, DebugLoc DL,
106 unsigned DestReg, unsigned SrcReg,
107 bool KillSrc) const {
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000108 unsigned Opc = 0, ZeroReg = 0;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000109
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000110 if (Mips::CPURegsRegClass.contains(DestReg)) { // Copy to CPU Reg.
Akira Hatanakab7dd9fc2012-07-21 02:15:19 +0000111 if (Mips::CPURegsRegClass.contains(SrcReg)) {
112 if (InMips16Mode)
113 Opc=Mips::Mov32R16;
114 else {
115 Opc = Mips::ADDu, ZeroReg = Mips::ZERO;
116 }
117 }
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000118 else if (Mips::CCRRegClass.contains(SrcReg))
119 Opc = Mips::CFC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000120 else if (Mips::FGR32RegClass.contains(SrcReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000121 Opc = Mips::MFC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000122 else if (SrcReg == Mips::HI)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000123 Opc = Mips::MFHI, SrcReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000124 else if (SrcReg == Mips::LO)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000125 Opc = Mips::MFLO, SrcReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000126 }
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000127 else if (Mips::CPURegsRegClass.contains(SrcReg)) { // Copy from CPU Reg.
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000128 if (Mips::CCRRegClass.contains(DestReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000129 Opc = Mips::CTC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000130 else if (Mips::FGR32RegClass.contains(DestReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000131 Opc = Mips::MTC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000132 else if (DestReg == Mips::HI)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000133 Opc = Mips::MTHI, DestReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000134 else if (DestReg == Mips::LO)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000135 Opc = Mips::MTLO, DestReg = 0;
136 }
137 else if (Mips::FGR32RegClass.contains(DestReg, SrcReg))
Akira Hatanaka4391bb72011-10-08 03:50:18 +0000138 Opc = Mips::FMOV_S;
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000139 else if (Mips::AFGR64RegClass.contains(DestReg, SrcReg))
140 Opc = Mips::FMOV_D32;
Akira Hatanaka29d525a2011-11-07 21:35:45 +0000141 else if (Mips::FGR64RegClass.contains(DestReg, SrcReg))
142 Opc = Mips::FMOV_D64;
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000143 else if (Mips::CCRRegClass.contains(DestReg, SrcReg))
144 Opc = Mips::MOVCCRToCCR;
145 else if (Mips::CPU64RegsRegClass.contains(DestReg)) { // Copy to CPU64 Reg.
146 if (Mips::CPU64RegsRegClass.contains(SrcReg))
147 Opc = Mips::DADDu, ZeroReg = Mips::ZERO_64;
148 else if (SrcReg == Mips::HI64)
149 Opc = Mips::MFHI64, SrcReg = 0;
150 else if (SrcReg == Mips::LO64)
151 Opc = Mips::MFLO64, SrcReg = 0;
Akira Hatanaka29d525a2011-11-07 21:35:45 +0000152 else if (Mips::FGR64RegClass.contains(SrcReg))
153 Opc = Mips::DMFC1;
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000154 }
155 else if (Mips::CPU64RegsRegClass.contains(SrcReg)) { // Copy from CPU64 Reg.
156 if (DestReg == Mips::HI64)
157 Opc = Mips::MTHI64, DestReg = 0;
158 else if (DestReg == Mips::LO64)
159 Opc = Mips::MTLO64, DestReg = 0;
Akira Hatanaka29d525a2011-11-07 21:35:45 +0000160 else if (Mips::FGR64RegClass.contains(DestReg))
161 Opc = Mips::DMTC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000162 }
163
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000164 assert(Opc && "Cannot copy registers");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000165
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000166 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
Jia Liubb481f82012-02-28 07:46:26 +0000167
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000168 if (DestReg)
169 MIB.addReg(DestReg, RegState::Define);
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000170
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000171 if (ZeroReg)
172 MIB.addReg(ZeroReg);
173
174 if (SrcReg)
175 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000176}
177
Akira Hatanakafd1d9252011-12-24 03:11:18 +0000178static MachineMemOperand* GetMemOperand(MachineBasicBlock &MBB, int FI,
179 unsigned Flag) {
180 MachineFunction &MF = *MBB.getParent();
181 MachineFrameInfo &MFI = *MF.getFrameInfo();
182 unsigned Align = MFI.getObjectAlignment(FI);
Jia Liubb481f82012-02-28 07:46:26 +0000183
Akira Hatanakafd1d9252011-12-24 03:11:18 +0000184 return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), Flag,
185 MFI.getObjectSize(FI), Align);
186}
187
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000188void MipsInstrInfo::
189storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000190 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000191 const TargetRegisterClass *RC,
192 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000193 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000194 if (I != MBB.end()) DL = I->getDebugLoc();
Akira Hatanakafd1d9252011-12-24 03:11:18 +0000195 MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
196
Akira Hatanaka43aed322011-10-11 00:37:28 +0000197 unsigned Opc = 0;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000198
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000199 if (Mips::CPURegsRegClass.hasSubClassEq(RC))
Akira Hatanaka43aed322011-10-11 00:37:28 +0000200 Opc = IsN64 ? Mips::SW_P8 : Mips::SW;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000201 else if (Mips::CPU64RegsRegClass.hasSubClassEq(RC))
Akira Hatanaka43aed322011-10-11 00:37:28 +0000202 Opc = IsN64 ? Mips::SD_P8 : Mips::SD;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000203 else if (Mips::FGR32RegClass.hasSubClassEq(RC))
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000204 Opc = IsN64 ? Mips::SWC1_P8 : Mips::SWC1;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000205 else if (Mips::AFGR64RegClass.hasSubClassEq(RC))
Akira Hatanaka43aed322011-10-11 00:37:28 +0000206 Opc = Mips::SDC1;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000207 else if (Mips::FGR64RegClass.hasSubClassEq(RC))
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000208 Opc = IsN64 ? Mips::SDC164_P8 : Mips::SDC164;
Akira Hatanaka43aed322011-10-11 00:37:28 +0000209
210 assert(Opc && "Register class not handled!");
211 BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
Akira Hatanakafd1d9252011-12-24 03:11:18 +0000212 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000213}
214
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000215void MipsInstrInfo::
216loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
217 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000218 const TargetRegisterClass *RC,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000219 const TargetRegisterInfo *TRI) const
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000220{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000221 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000222 if (I != MBB.end()) DL = I->getDebugLoc();
Akira Hatanakafd1d9252011-12-24 03:11:18 +0000223 MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
Akira Hatanaka43aed322011-10-11 00:37:28 +0000224 unsigned Opc = 0;
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000225
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000226 if (Mips::CPURegsRegClass.hasSubClassEq(RC))
Akira Hatanaka43aed322011-10-11 00:37:28 +0000227 Opc = IsN64 ? Mips::LW_P8 : Mips::LW;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000228 else if (Mips::CPU64RegsRegClass.hasSubClassEq(RC))
Akira Hatanaka43aed322011-10-11 00:37:28 +0000229 Opc = IsN64 ? Mips::LD_P8 : Mips::LD;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000230 else if (Mips::FGR32RegClass.hasSubClassEq(RC))
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000231 Opc = IsN64 ? Mips::LWC1_P8 : Mips::LWC1;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000232 else if (Mips::AFGR64RegClass.hasSubClassEq(RC))
Akira Hatanaka43aed322011-10-11 00:37:28 +0000233 Opc = Mips::LDC1;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000234 else if (Mips::FGR64RegClass.hasSubClassEq(RC))
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000235 Opc = IsN64 ? Mips::LDC164_P8 : Mips::LDC164;
Akira Hatanaka43aed322011-10-11 00:37:28 +0000236
237 assert(Opc && "Register class not handled!");
Akira Hatanakafd1d9252011-12-24 03:11:18 +0000238 BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(0)
239 .addMemOperand(MMO);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000240}
241
Akira Hatanaka182ef6f2012-07-10 00:19:06 +0000242void MipsInstrInfo::ExpandRetRA(MachineBasicBlock &MBB,
243 MachineBasicBlock::iterator I,
244 unsigned Opc) const {
245 BuildMI(MBB, I, I->getDebugLoc(), TM.getInstrInfo()->get(Opc))
246 .addReg(Mips::RA);
247}
248
Akira Hatanakab7dd9fc2012-07-21 02:15:19 +0000249void MipsInstrInfo::ExpandRetRA16(MachineBasicBlock &MBB,
250 MachineBasicBlock::iterator I,
251 unsigned Opc) const {
252 BuildMI(MBB, I, I->getDebugLoc(), TM.getInstrInfo()->get(Opc));
253}
254
Akira Hatanaka564f6902012-05-25 20:52:52 +0000255void MipsInstrInfo::ExpandExtractElementF64(MachineBasicBlock &MBB,
256 MachineBasicBlock::iterator I) const {
257 const TargetInstrInfo *TII = TM.getInstrInfo();
258 unsigned DstReg = I->getOperand(0).getReg();
259 unsigned SrcReg = I->getOperand(1).getReg();
260 unsigned N = I->getOperand(2).getImm();
261 const MCInstrDesc& Mfc1Tdd = TII->get(Mips::MFC1);
262 DebugLoc dl = I->getDebugLoc();
Akira Hatanaka564f6902012-05-25 20:52:52 +0000263
Jakob Stoklund Olesen6c823822012-05-30 18:40:49 +0000264 assert(N < 2 && "Invalid immediate");
265 unsigned SubIdx = N ? Mips::sub_fpodd : Mips::sub_fpeven;
266 unsigned SubReg = TM.getRegisterInfo()->getSubReg(SrcReg, SubIdx);
267
268 BuildMI(MBB, I, dl, Mfc1Tdd, DstReg).addReg(SubReg);
Akira Hatanaka564f6902012-05-25 20:52:52 +0000269}
270
271void MipsInstrInfo::ExpandBuildPairF64(MachineBasicBlock &MBB,
272 MachineBasicBlock::iterator I) const {
273 const TargetInstrInfo *TII = TM.getInstrInfo();
274 unsigned DstReg = I->getOperand(0).getReg();
275 unsigned LoReg = I->getOperand(1).getReg(), HiReg = I->getOperand(2).getReg();
276 const MCInstrDesc& Mtc1Tdd = TII->get(Mips::MTC1);
277 DebugLoc dl = I->getDebugLoc();
Jakob Stoklund Olesen6c823822012-05-30 18:40:49 +0000278 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Akira Hatanaka564f6902012-05-25 20:52:52 +0000279
280 // mtc1 Lo, $fp
281 // mtc1 Hi, $fp + 1
Jakob Stoklund Olesen6c823822012-05-30 18:40:49 +0000282 BuildMI(MBB, I, dl, Mtc1Tdd, TRI->getSubReg(DstReg, Mips::sub_fpeven))
283 .addReg(LoReg);
284 BuildMI(MBB, I, dl, Mtc1Tdd, TRI->getSubReg(DstReg, Mips::sub_fpodd))
285 .addReg(HiReg);
Akira Hatanaka564f6902012-05-25 20:52:52 +0000286}
287
288bool MipsInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
289 MachineBasicBlock &MBB = *MI->getParent();
290
291 switch(MI->getDesc().getOpcode()) {
292 default:
293 return false;
Akira Hatanaka182ef6f2012-07-10 00:19:06 +0000294 case Mips::RetRA:
295 ExpandRetRA(MBB, MI, Mips::RET);
296 break;
297 case Mips::RetRA16:
Akira Hatanakab7dd9fc2012-07-21 02:15:19 +0000298 ExpandRetRA16(MBB, MI, Mips::JrRa16);
Akira Hatanaka182ef6f2012-07-10 00:19:06 +0000299 break;
Akira Hatanaka564f6902012-05-25 20:52:52 +0000300 case Mips::BuildPairF64:
301 ExpandBuildPairF64(MBB, MI);
302 break;
303 case Mips::ExtractElementF64:
304 ExpandExtractElementF64(MBB, MI);
305 break;
306 }
307
308 MBB.erase(MI);
309 return true;
310}
311
Akira Hatanakac4f24eb2011-07-01 01:04:43 +0000312MachineInstr*
313MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
314 uint64_t Offset, const MDNode *MDPtr,
315 DebugLoc DL) const {
316 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE))
317 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
318 return &*MIB;
319}
320
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000321//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000322// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000323//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000324
Akira Hatanaka20ada982011-04-01 17:39:08 +0000325static unsigned GetAnalyzableBrOpc(unsigned Opc) {
Akira Hatanaka3e3427a2011-10-11 18:49:17 +0000326 return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ ||
327 Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ ||
328 Opc == Mips::BEQ64 || Opc == Mips::BNE64 || Opc == Mips::BGTZ64 ||
329 Opc == Mips::BGEZ64 || Opc == Mips::BLTZ64 || Opc == Mips::BLEZ64 ||
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000330 Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::B ||
331 Opc == Mips::J) ?
Akira Hatanaka3e3427a2011-10-11 18:49:17 +0000332 Opc : 0;
Akira Hatanaka20ada982011-04-01 17:39:08 +0000333}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000334
Akira Hatanaka20ada982011-04-01 17:39:08 +0000335/// GetOppositeBranchOpc - Return the inverse of the specified
336/// opcode, e.g. turning BEQ to BNE.
337unsigned Mips::GetOppositeBranchOpc(unsigned Opc)
338{
339 switch (Opc) {
Akira Hatanaka82099682011-12-19 19:52:25 +0000340 default: llvm_unreachable("Illegal opcode!");
341 case Mips::BEQ: return Mips::BNE;
342 case Mips::BNE: return Mips::BEQ;
343 case Mips::BGTZ: return Mips::BLEZ;
344 case Mips::BGEZ: return Mips::BLTZ;
345 case Mips::BLTZ: return Mips::BGEZ;
346 case Mips::BLEZ: return Mips::BGTZ;
347 case Mips::BEQ64: return Mips::BNE64;
348 case Mips::BNE64: return Mips::BEQ64;
349 case Mips::BGTZ64: return Mips::BLEZ64;
350 case Mips::BGEZ64: return Mips::BLTZ64;
351 case Mips::BLTZ64: return Mips::BGEZ64;
352 case Mips::BLEZ64: return Mips::BGTZ64;
353 case Mips::BC1T: return Mips::BC1F;
354 case Mips::BC1F: return Mips::BC1T;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000355 }
356}
357
Akira Hatanaka864f6602012-06-14 21:10:56 +0000358static void AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
Akira Hatanaka20ada982011-04-01 17:39:08 +0000359 MachineBasicBlock *&BB,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000360 SmallVectorImpl<MachineOperand> &Cond) {
Akira Hatanaka20ada982011-04-01 17:39:08 +0000361 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
362 int NumOp = Inst->getNumExplicitOperands();
Jia Liubb481f82012-02-28 07:46:26 +0000363
Akira Hatanaka20ada982011-04-01 17:39:08 +0000364 // for both int and fp branches, the last explicit operand is the
365 // MBB.
366 BB = Inst->getOperand(NumOp-1).getMBB();
367 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000368
Akira Hatanaka20ada982011-04-01 17:39:08 +0000369 for (int i=0; i<NumOp-1; i++)
370 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000371}
372
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000373bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000374 MachineBasicBlock *&TBB,
375 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000376 SmallVectorImpl<MachineOperand> &Cond,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000377 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000378{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000379 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000380
Akira Hatanaka20ada982011-04-01 17:39:08 +0000381 // Skip all the debug instructions.
382 while (I != REnd && I->isDebugValue())
383 ++I;
384
385 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
386 // If this block ends with no branches (it just falls through to its succ)
387 // just return false, leaving TBB/FBB null.
388 TBB = FBB = NULL;
389 return false;
390 }
391
392 MachineInstr *LastInst = &*I;
393 unsigned LastOpc = LastInst->getOpcode();
394
395 // Not an analyzable branch (must be an indirect jump).
396 if (!GetAnalyzableBrOpc(LastOpc))
397 return true;
398
399 // Get the second to last instruction in the block.
400 unsigned SecondLastOpc = 0;
401 MachineInstr *SecondLastInst = NULL;
402
403 if (++I != REnd) {
404 SecondLastInst = &*I;
405 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
406
407 // Not an analyzable branch (must be an indirect jump).
408 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
409 return true;
410 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000411
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000412 // If there is only one terminator instruction, process it.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000413 if (!SecondLastOpc) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000414 // Unconditional branch
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000415 if (LastOpc == UncondBrOpc) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000416 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000417 return false;
418 }
419
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000420 // Conditional branch
Akira Hatanaka20ada982011-04-01 17:39:08 +0000421 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
422 return false;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000423 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000424
Akira Hatanaka20ada982011-04-01 17:39:08 +0000425 // If we reached here, there are two branches.
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000426 // If there are three terminators, we don't know what sort of block this is.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000427 if (++I != REnd && isUnpredicatedTerminator(&*I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000428 return true;
429
Akira Hatanaka20ada982011-04-01 17:39:08 +0000430 // If second to last instruction is an unconditional branch,
431 // analyze it and remove the last instruction.
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000432 if (SecondLastOpc == UncondBrOpc) {
Akira Hatanaka20ada982011-04-01 17:39:08 +0000433 // Return if the last instruction cannot be removed.
434 if (!AllowModify)
435 return true;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000436
Chris Lattner8aa797a2007-12-30 23:10:15 +0000437 TBB = SecondLastInst->getOperand(0).getMBB();
Akira Hatanaka20ada982011-04-01 17:39:08 +0000438 LastInst->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000439 return false;
440 }
441
Akira Hatanaka20ada982011-04-01 17:39:08 +0000442 // Conditional branch followed by an unconditional branch.
443 // The last one must be unconditional.
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000444 if (LastOpc != UncondBrOpc)
Akira Hatanaka20ada982011-04-01 17:39:08 +0000445 return true;
446
447 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
448 FBB = LastInst->getOperand(0).getMBB();
449
450 return false;
Jia Liubb481f82012-02-28 07:46:26 +0000451}
452
Akira Hatanaka20ada982011-04-01 17:39:08 +0000453void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
454 MachineBasicBlock *TBB, DebugLoc DL,
455 const SmallVectorImpl<MachineOperand>& Cond)
456 const {
457 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000458 const MCInstrDesc &MCID = get(Opc);
459 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000460
461 for (unsigned i = 1; i < Cond.size(); ++i)
462 MIB.addReg(Cond[i].getReg());
463
464 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000465}
466
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000467unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000468InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000469 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000470 const SmallVectorImpl<MachineOperand> &Cond,
471 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000472 // Shouldn't be a fall through.
473 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000474
Akira Hatanaka20ada982011-04-01 17:39:08 +0000475 // # of condition operands:
476 // Unconditional branches: 0
477 // Floating point branches: 1 (opc)
478 // Int BranchZero: 2 (opc, reg)
479 // Int Branch: 3 (opc, reg0, reg1)
480 assert((Cond.size() <= 3) &&
481 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000482
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000483 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000484 if (FBB) {
485 BuildCondBr(MBB, TBB, DL, Cond);
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000486 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000487 return 2;
488 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000489
Akira Hatanaka20ada982011-04-01 17:39:08 +0000490 // One way branch.
491 // Unconditional branch.
492 if (Cond.empty())
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000493 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000494 else // Conditional branch.
495 BuildCondBr(MBB, TBB, DL, Cond);
496 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000497}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000498
499unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000500RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000501{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000502 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
503 MachineBasicBlock::reverse_iterator FirstBr;
504 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000505
Akira Hatanaka20ada982011-04-01 17:39:08 +0000506 // Skip all the debug instructions.
507 while (I != REnd && I->isDebugValue())
508 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000509
Akira Hatanaka20ada982011-04-01 17:39:08 +0000510 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000511
Akira Hatanaka20ada982011-04-01 17:39:08 +0000512 // Up to 2 branches are removed.
513 // Note that indirect branches are not removed.
514 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
515 if (!GetAnalyzableBrOpc(I->getOpcode()))
516 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000517
Akira Hatanaka20ada982011-04-01 17:39:08 +0000518 MBB.erase(I.base(), FirstBr.base());
519
520 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000521}
522
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000523/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000524/// specified Branch instruction.
525bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000526ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000527{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000528 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000529 "Invalid Mips branch condition!");
Akira Hatanaka20ada982011-04-01 17:39:08 +0000530 Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000531 return false;
532}
Dan Gohman99114052009-06-03 20:30:14 +0000533
Akira Hatanakad4b48b22012-06-14 01:16:45 +0000534/// Return the number of bytes of code the specified instruction may be.
535unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
536 switch (MI->getOpcode()) {
537 default:
538 return MI->getDesc().getSize();
539 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
540 const MachineFunction *MF = MI->getParent()->getParent();
541 const char *AsmStr = MI->getOperand(0).getSymbolName();
542 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
543 }
544 }
545}
546
547unsigned
548llvm::Mips::loadImmediate(int64_t Imm, bool IsN64, const TargetInstrInfo &TII,
549 MachineBasicBlock& MBB,
550 MachineBasicBlock::iterator II, DebugLoc DL,
551 bool LastInstrIsADDiu,
552 MipsAnalyzeImmediate::Inst *LastInst) {
553 MipsAnalyzeImmediate AnalyzeImm;
554 unsigned Size = IsN64 ? 64 : 32;
555 unsigned LUi = IsN64 ? Mips::LUi64 : Mips::LUi;
556 unsigned ZEROReg = IsN64 ? Mips::ZERO_64 : Mips::ZERO;
557 unsigned ATReg = IsN64 ? Mips::AT_64 : Mips::AT;
558
559 const MipsAnalyzeImmediate::InstSeq &Seq =
560 AnalyzeImm.Analyze(Imm, Size, LastInstrIsADDiu);
561 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
562
563 if (LastInst && (Seq.size() == 1)) {
564 *LastInst = *Inst;
565 return 0;
566 }
567
568 // The first instruction can be a LUi, which is different from other
569 // instructions (ADDiu, ORI and SLL) in that it does not have a register
570 // operand.
571 if (Inst->Opc == LUi)
572 BuildMI(MBB, II, DL, TII.get(LUi), ATReg)
573 .addImm(SignExtend64<16>(Inst->ImmOpnd));
574 else
575 BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ZEROReg)
576 .addImm(SignExtend64<16>(Inst->ImmOpnd));
577
578 // Build the remaining instructions in Seq. Skip the last instruction if
579 // LastInst is not 0.
580 for (++Inst; Inst != Seq.end() - !!LastInst; ++Inst)
581 BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ATReg)
582 .addImm(SignExtend64<16>(Inst->ImmOpnd));
583
584 if (LastInst)
585 *LastInst = *Inst;
586
587 return Seq.size() - !!LastInst;
588}