blob: 7d8488f0b853254219568ec513fec72389ae24f1 [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 Hatanaka6e55ff52011-12-12 22:39:35 +000033 RI(*TM.getSubtargetImpl(), *this),
34 UncondBrOpc(TM.getRelocationModel() == Reloc::PIC_ ? Mips::B : Mips::J) {}
Akira Hatanaka794bf172011-07-07 23:56:50 +000035
Jia Liubb481f82012-02-28 07:46:26 +000036const MipsRegisterInfo &MipsInstrInfo::getRegisterInfo() const {
Akira Hatanaka794bf172011-07-07 23:56:50 +000037 return RI;
38}
39
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000040static bool isZeroImm(const MachineOperand &op) {
Dan Gohmand735b802008-10-03 15:45:36 +000041 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000042}
43
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000044/// isLoadFromStackSlot - If the specified machine instruction is a direct
45/// load from a stack slot, return the virtual or physical register number of
46/// the destination along with the FrameIndex of the loaded stack slot. If
47/// not, return 0. This predicate must return 0 if the instruction has
48/// any side effects other than loading from the stack slot.
49unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000050isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000051{
Akira Hatanaka1acb7df2011-10-11 01:12:52 +000052 unsigned Opc = MI->getOpcode();
53
54 if ((Opc == Mips::LW) || (Opc == Mips::LW_P8) || (Opc == Mips::LD) ||
55 (Opc == Mips::LD_P8) || (Opc == Mips::LWC1) || (Opc == Mips::LWC1_P8) ||
56 (Opc == Mips::LDC1) || (Opc == Mips::LDC164) ||
57 (Opc == Mips::LDC164_P8)) {
Akira Hatanakad3ac47f2011-07-07 18:57:00 +000058 if ((MI->getOperand(1).isFI()) && // is a stack slot
59 (MI->getOperand(2).isImm()) && // the imm is zero
60 (isZeroImm(MI->getOperand(2)))) {
61 FrameIndex = MI->getOperand(1).getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000062 return MI->getOperand(0).getReg();
63 }
64 }
65
66 return 0;
67}
68
69/// isStoreToStackSlot - If the specified machine instruction is a direct
70/// store to a stack slot, return the virtual or physical register number of
71/// the source reg along with the FrameIndex of the loaded stack slot. If
72/// not, return 0. This predicate must return 0 if the instruction has
73/// any side effects other than storing to the stack slot.
74unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000075isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000076{
Akira Hatanaka1acb7df2011-10-11 01:12:52 +000077 unsigned Opc = MI->getOpcode();
78
79 if ((Opc == Mips::SW) || (Opc == Mips::SW_P8) || (Opc == Mips::SD) ||
80 (Opc == Mips::SD_P8) || (Opc == Mips::SWC1) || (Opc == Mips::SWC1_P8) ||
81 (Opc == Mips::SDC1) || (Opc == Mips::SDC164) ||
82 (Opc == Mips::SDC164_P8)) {
Akira Hatanakad3ac47f2011-07-07 18:57:00 +000083 if ((MI->getOperand(1).isFI()) && // is a stack slot
84 (MI->getOperand(2).isImm()) && // the imm is zero
85 (isZeroImm(MI->getOperand(2)))) {
86 FrameIndex = MI->getOperand(1).getIndex();
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +000087 return MI->getOperand(0).getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000088 }
89 }
90 return 0;
91}
92
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000093/// insertNoop - If data hazard condition is found insert the target nop
94/// instruction.
95void MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000096insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000097{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000098 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +000099 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000100}
101
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000102void MipsInstrInfo::
103copyPhysReg(MachineBasicBlock &MBB,
104 MachineBasicBlock::iterator I, DebugLoc DL,
105 unsigned DestReg, unsigned SrcReg,
106 bool KillSrc) const {
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000107 unsigned Opc = 0, ZeroReg = 0;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000108
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000109 if (Mips::CPURegsRegClass.contains(DestReg)) { // Copy to CPU Reg.
Akira Hatanakafef904d2012-07-21 02:20:33 +0000110 if (Mips::CPURegsRegClass.contains(SrcReg))
111 Opc = Mips::ADDu, ZeroReg = Mips::ZERO;
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000112 else if (Mips::CCRRegClass.contains(SrcReg))
113 Opc = Mips::CFC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000114 else if (Mips::FGR32RegClass.contains(SrcReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000115 Opc = Mips::MFC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000116 else if (SrcReg == Mips::HI)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000117 Opc = Mips::MFHI, SrcReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000118 else if (SrcReg == Mips::LO)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000119 Opc = Mips::MFLO, SrcReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000120 }
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000121 else if (Mips::CPURegsRegClass.contains(SrcReg)) { // Copy from CPU Reg.
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000122 if (Mips::CCRRegClass.contains(DestReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000123 Opc = Mips::CTC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000124 else if (Mips::FGR32RegClass.contains(DestReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000125 Opc = Mips::MTC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000126 else if (DestReg == Mips::HI)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000127 Opc = Mips::MTHI, DestReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000128 else if (DestReg == Mips::LO)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000129 Opc = Mips::MTLO, DestReg = 0;
130 }
131 else if (Mips::FGR32RegClass.contains(DestReg, SrcReg))
Akira Hatanaka4391bb72011-10-08 03:50:18 +0000132 Opc = Mips::FMOV_S;
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000133 else if (Mips::AFGR64RegClass.contains(DestReg, SrcReg))
134 Opc = Mips::FMOV_D32;
Akira Hatanaka29d525a2011-11-07 21:35:45 +0000135 else if (Mips::FGR64RegClass.contains(DestReg, SrcReg))
136 Opc = Mips::FMOV_D64;
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000137 else if (Mips::CCRRegClass.contains(DestReg, SrcReg))
138 Opc = Mips::MOVCCRToCCR;
139 else if (Mips::CPU64RegsRegClass.contains(DestReg)) { // Copy to CPU64 Reg.
140 if (Mips::CPU64RegsRegClass.contains(SrcReg))
141 Opc = Mips::DADDu, ZeroReg = Mips::ZERO_64;
142 else if (SrcReg == Mips::HI64)
143 Opc = Mips::MFHI64, SrcReg = 0;
144 else if (SrcReg == Mips::LO64)
145 Opc = Mips::MFLO64, SrcReg = 0;
Akira Hatanaka29d525a2011-11-07 21:35:45 +0000146 else if (Mips::FGR64RegClass.contains(SrcReg))
147 Opc = Mips::DMFC1;
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000148 }
149 else if (Mips::CPU64RegsRegClass.contains(SrcReg)) { // Copy from CPU64 Reg.
150 if (DestReg == Mips::HI64)
151 Opc = Mips::MTHI64, DestReg = 0;
152 else if (DestReg == Mips::LO64)
153 Opc = Mips::MTLO64, DestReg = 0;
Akira Hatanaka29d525a2011-11-07 21:35:45 +0000154 else if (Mips::FGR64RegClass.contains(DestReg))
155 Opc = Mips::DMTC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000156 }
157
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000158 assert(Opc && "Cannot copy registers");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000159
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000160 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
Jia Liubb481f82012-02-28 07:46:26 +0000161
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000162 if (DestReg)
163 MIB.addReg(DestReg, RegState::Define);
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000164
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000165 if (ZeroReg)
166 MIB.addReg(ZeroReg);
167
168 if (SrcReg)
169 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000170}
171
Akira Hatanakafd1d9252011-12-24 03:11:18 +0000172static MachineMemOperand* GetMemOperand(MachineBasicBlock &MBB, int FI,
173 unsigned Flag) {
174 MachineFunction &MF = *MBB.getParent();
175 MachineFrameInfo &MFI = *MF.getFrameInfo();
176 unsigned Align = MFI.getObjectAlignment(FI);
Jia Liubb481f82012-02-28 07:46:26 +0000177
Akira Hatanakafd1d9252011-12-24 03:11:18 +0000178 return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), Flag,
179 MFI.getObjectSize(FI), Align);
180}
181
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000182void MipsInstrInfo::
183storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000184 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000185 const TargetRegisterClass *RC,
186 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000187 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000188 if (I != MBB.end()) DL = I->getDebugLoc();
Akira Hatanakafd1d9252011-12-24 03:11:18 +0000189 MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
190
Akira Hatanaka43aed322011-10-11 00:37:28 +0000191 unsigned Opc = 0;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000192
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000193 if (Mips::CPURegsRegClass.hasSubClassEq(RC))
Akira Hatanaka43aed322011-10-11 00:37:28 +0000194 Opc = IsN64 ? Mips::SW_P8 : Mips::SW;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000195 else if (Mips::CPU64RegsRegClass.hasSubClassEq(RC))
Akira Hatanaka43aed322011-10-11 00:37:28 +0000196 Opc = IsN64 ? Mips::SD_P8 : Mips::SD;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000197 else if (Mips::FGR32RegClass.hasSubClassEq(RC))
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000198 Opc = IsN64 ? Mips::SWC1_P8 : Mips::SWC1;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000199 else if (Mips::AFGR64RegClass.hasSubClassEq(RC))
Akira Hatanaka43aed322011-10-11 00:37:28 +0000200 Opc = Mips::SDC1;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000201 else if (Mips::FGR64RegClass.hasSubClassEq(RC))
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000202 Opc = IsN64 ? Mips::SDC164_P8 : Mips::SDC164;
Akira Hatanaka43aed322011-10-11 00:37:28 +0000203
204 assert(Opc && "Register class not handled!");
205 BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
Akira Hatanakafd1d9252011-12-24 03:11:18 +0000206 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000207}
208
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000209void MipsInstrInfo::
210loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
211 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000212 const TargetRegisterClass *RC,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000213 const TargetRegisterInfo *TRI) const
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000214{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000215 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000216 if (I != MBB.end()) DL = I->getDebugLoc();
Akira Hatanakafd1d9252011-12-24 03:11:18 +0000217 MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
Akira Hatanaka43aed322011-10-11 00:37:28 +0000218 unsigned Opc = 0;
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000219
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000220 if (Mips::CPURegsRegClass.hasSubClassEq(RC))
Akira Hatanaka43aed322011-10-11 00:37:28 +0000221 Opc = IsN64 ? Mips::LW_P8 : Mips::LW;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000222 else if (Mips::CPU64RegsRegClass.hasSubClassEq(RC))
Akira Hatanaka43aed322011-10-11 00:37:28 +0000223 Opc = IsN64 ? Mips::LD_P8 : Mips::LD;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000224 else if (Mips::FGR32RegClass.hasSubClassEq(RC))
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000225 Opc = IsN64 ? Mips::LWC1_P8 : Mips::LWC1;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000226 else if (Mips::AFGR64RegClass.hasSubClassEq(RC))
Akira Hatanaka43aed322011-10-11 00:37:28 +0000227 Opc = Mips::LDC1;
Akira Hatanaka66e19c32012-05-16 22:19:56 +0000228 else if (Mips::FGR64RegClass.hasSubClassEq(RC))
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000229 Opc = IsN64 ? Mips::LDC164_P8 : Mips::LDC164;
Akira Hatanaka43aed322011-10-11 00:37:28 +0000230
231 assert(Opc && "Register class not handled!");
Akira Hatanakafd1d9252011-12-24 03:11:18 +0000232 BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(0)
233 .addMemOperand(MMO);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000234}
235
Akira Hatanaka182ef6f2012-07-10 00:19:06 +0000236void MipsInstrInfo::ExpandRetRA(MachineBasicBlock &MBB,
237 MachineBasicBlock::iterator I,
238 unsigned Opc) const {
239 BuildMI(MBB, I, I->getDebugLoc(), TM.getInstrInfo()->get(Opc))
240 .addReg(Mips::RA);
241}
242
Akira Hatanaka564f6902012-05-25 20:52:52 +0000243void MipsInstrInfo::ExpandExtractElementF64(MachineBasicBlock &MBB,
244 MachineBasicBlock::iterator I) const {
245 const TargetInstrInfo *TII = TM.getInstrInfo();
246 unsigned DstReg = I->getOperand(0).getReg();
247 unsigned SrcReg = I->getOperand(1).getReg();
248 unsigned N = I->getOperand(2).getImm();
249 const MCInstrDesc& Mfc1Tdd = TII->get(Mips::MFC1);
250 DebugLoc dl = I->getDebugLoc();
Akira Hatanaka564f6902012-05-25 20:52:52 +0000251
Jakob Stoklund Olesen6c823822012-05-30 18:40:49 +0000252 assert(N < 2 && "Invalid immediate");
253 unsigned SubIdx = N ? Mips::sub_fpodd : Mips::sub_fpeven;
254 unsigned SubReg = TM.getRegisterInfo()->getSubReg(SrcReg, SubIdx);
255
256 BuildMI(MBB, I, dl, Mfc1Tdd, DstReg).addReg(SubReg);
Akira Hatanaka564f6902012-05-25 20:52:52 +0000257}
258
259void MipsInstrInfo::ExpandBuildPairF64(MachineBasicBlock &MBB,
260 MachineBasicBlock::iterator I) const {
261 const TargetInstrInfo *TII = TM.getInstrInfo();
262 unsigned DstReg = I->getOperand(0).getReg();
263 unsigned LoReg = I->getOperand(1).getReg(), HiReg = I->getOperand(2).getReg();
264 const MCInstrDesc& Mtc1Tdd = TII->get(Mips::MTC1);
265 DebugLoc dl = I->getDebugLoc();
Jakob Stoklund Olesen6c823822012-05-30 18:40:49 +0000266 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
Akira Hatanaka564f6902012-05-25 20:52:52 +0000267
268 // mtc1 Lo, $fp
269 // mtc1 Hi, $fp + 1
Jakob Stoklund Olesen6c823822012-05-30 18:40:49 +0000270 BuildMI(MBB, I, dl, Mtc1Tdd, TRI->getSubReg(DstReg, Mips::sub_fpeven))
271 .addReg(LoReg);
272 BuildMI(MBB, I, dl, Mtc1Tdd, TRI->getSubReg(DstReg, Mips::sub_fpodd))
273 .addReg(HiReg);
Akira Hatanaka564f6902012-05-25 20:52:52 +0000274}
275
276bool MipsInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
277 MachineBasicBlock &MBB = *MI->getParent();
278
279 switch(MI->getDesc().getOpcode()) {
280 default:
281 return false;
Akira Hatanaka182ef6f2012-07-10 00:19:06 +0000282 case Mips::RetRA:
283 ExpandRetRA(MBB, MI, Mips::RET);
284 break;
285 case Mips::RetRA16:
Akira Hatanakafef904d2012-07-21 02:20:33 +0000286 ExpandRetRA(MBB, MI, Mips::RET16);
Akira Hatanaka182ef6f2012-07-10 00:19:06 +0000287 break;
Akira Hatanaka564f6902012-05-25 20:52:52 +0000288 case Mips::BuildPairF64:
289 ExpandBuildPairF64(MBB, MI);
290 break;
291 case Mips::ExtractElementF64:
292 ExpandExtractElementF64(MBB, MI);
293 break;
294 }
295
296 MBB.erase(MI);
297 return true;
298}
299
Akira Hatanakac4f24eb2011-07-01 01:04:43 +0000300MachineInstr*
301MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
302 uint64_t Offset, const MDNode *MDPtr,
303 DebugLoc DL) const {
304 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE))
305 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
306 return &*MIB;
307}
308
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000309//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000310// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000311//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000312
Akira Hatanaka20ada982011-04-01 17:39:08 +0000313static unsigned GetAnalyzableBrOpc(unsigned Opc) {
Akira Hatanaka3e3427a2011-10-11 18:49:17 +0000314 return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ ||
315 Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ ||
316 Opc == Mips::BEQ64 || Opc == Mips::BNE64 || Opc == Mips::BGTZ64 ||
317 Opc == Mips::BGEZ64 || Opc == Mips::BLTZ64 || Opc == Mips::BLEZ64 ||
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000318 Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::B ||
319 Opc == Mips::J) ?
Akira Hatanaka3e3427a2011-10-11 18:49:17 +0000320 Opc : 0;
Akira Hatanaka20ada982011-04-01 17:39:08 +0000321}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000322
Akira Hatanaka20ada982011-04-01 17:39:08 +0000323/// GetOppositeBranchOpc - Return the inverse of the specified
324/// opcode, e.g. turning BEQ to BNE.
325unsigned Mips::GetOppositeBranchOpc(unsigned Opc)
326{
327 switch (Opc) {
Akira Hatanaka82099682011-12-19 19:52:25 +0000328 default: llvm_unreachable("Illegal opcode!");
329 case Mips::BEQ: return Mips::BNE;
330 case Mips::BNE: return Mips::BEQ;
331 case Mips::BGTZ: return Mips::BLEZ;
332 case Mips::BGEZ: return Mips::BLTZ;
333 case Mips::BLTZ: return Mips::BGEZ;
334 case Mips::BLEZ: return Mips::BGTZ;
335 case Mips::BEQ64: return Mips::BNE64;
336 case Mips::BNE64: return Mips::BEQ64;
337 case Mips::BGTZ64: return Mips::BLEZ64;
338 case Mips::BGEZ64: return Mips::BLTZ64;
339 case Mips::BLTZ64: return Mips::BGEZ64;
340 case Mips::BLEZ64: return Mips::BGTZ64;
341 case Mips::BC1T: return Mips::BC1F;
342 case Mips::BC1F: return Mips::BC1T;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000343 }
344}
345
Akira Hatanaka864f6602012-06-14 21:10:56 +0000346static void AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
Akira Hatanaka20ada982011-04-01 17:39:08 +0000347 MachineBasicBlock *&BB,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000348 SmallVectorImpl<MachineOperand> &Cond) {
Akira Hatanaka20ada982011-04-01 17:39:08 +0000349 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
350 int NumOp = Inst->getNumExplicitOperands();
Jia Liubb481f82012-02-28 07:46:26 +0000351
Akira Hatanaka20ada982011-04-01 17:39:08 +0000352 // for both int and fp branches, the last explicit operand is the
353 // MBB.
354 BB = Inst->getOperand(NumOp-1).getMBB();
355 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000356
Akira Hatanaka20ada982011-04-01 17:39:08 +0000357 for (int i=0; i<NumOp-1; i++)
358 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000359}
360
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000361bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000362 MachineBasicBlock *&TBB,
363 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000364 SmallVectorImpl<MachineOperand> &Cond,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000365 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000366{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000367 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000368
Akira Hatanaka20ada982011-04-01 17:39:08 +0000369 // Skip all the debug instructions.
370 while (I != REnd && I->isDebugValue())
371 ++I;
372
373 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
374 // If this block ends with no branches (it just falls through to its succ)
375 // just return false, leaving TBB/FBB null.
376 TBB = FBB = NULL;
377 return false;
378 }
379
380 MachineInstr *LastInst = &*I;
381 unsigned LastOpc = LastInst->getOpcode();
382
383 // Not an analyzable branch (must be an indirect jump).
384 if (!GetAnalyzableBrOpc(LastOpc))
385 return true;
386
387 // Get the second to last instruction in the block.
388 unsigned SecondLastOpc = 0;
389 MachineInstr *SecondLastInst = NULL;
390
391 if (++I != REnd) {
392 SecondLastInst = &*I;
393 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
394
395 // Not an analyzable branch (must be an indirect jump).
396 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
397 return true;
398 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000399
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000400 // If there is only one terminator instruction, process it.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000401 if (!SecondLastOpc) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000402 // Unconditional branch
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000403 if (LastOpc == UncondBrOpc) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000404 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000405 return false;
406 }
407
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000408 // Conditional branch
Akira Hatanaka20ada982011-04-01 17:39:08 +0000409 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
410 return false;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000411 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000412
Akira Hatanaka20ada982011-04-01 17:39:08 +0000413 // If we reached here, there are two branches.
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000414 // If there are three terminators, we don't know what sort of block this is.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000415 if (++I != REnd && isUnpredicatedTerminator(&*I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000416 return true;
417
Akira Hatanaka20ada982011-04-01 17:39:08 +0000418 // If second to last instruction is an unconditional branch,
419 // analyze it and remove the last instruction.
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000420 if (SecondLastOpc == UncondBrOpc) {
Akira Hatanaka20ada982011-04-01 17:39:08 +0000421 // Return if the last instruction cannot be removed.
422 if (!AllowModify)
423 return true;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000424
Chris Lattner8aa797a2007-12-30 23:10:15 +0000425 TBB = SecondLastInst->getOperand(0).getMBB();
Akira Hatanaka20ada982011-04-01 17:39:08 +0000426 LastInst->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000427 return false;
428 }
429
Akira Hatanaka20ada982011-04-01 17:39:08 +0000430 // Conditional branch followed by an unconditional branch.
431 // The last one must be unconditional.
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000432 if (LastOpc != UncondBrOpc)
Akira Hatanaka20ada982011-04-01 17:39:08 +0000433 return true;
434
435 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
436 FBB = LastInst->getOperand(0).getMBB();
437
438 return false;
Jia Liubb481f82012-02-28 07:46:26 +0000439}
440
Akira Hatanaka20ada982011-04-01 17:39:08 +0000441void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
442 MachineBasicBlock *TBB, DebugLoc DL,
443 const SmallVectorImpl<MachineOperand>& Cond)
444 const {
445 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000446 const MCInstrDesc &MCID = get(Opc);
447 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000448
449 for (unsigned i = 1; i < Cond.size(); ++i)
450 MIB.addReg(Cond[i].getReg());
451
452 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000453}
454
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000455unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000456InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000457 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000458 const SmallVectorImpl<MachineOperand> &Cond,
459 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000460 // Shouldn't be a fall through.
461 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000462
Akira Hatanaka20ada982011-04-01 17:39:08 +0000463 // # of condition operands:
464 // Unconditional branches: 0
465 // Floating point branches: 1 (opc)
466 // Int BranchZero: 2 (opc, reg)
467 // Int Branch: 3 (opc, reg0, reg1)
468 assert((Cond.size() <= 3) &&
469 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000470
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000471 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000472 if (FBB) {
473 BuildCondBr(MBB, TBB, DL, Cond);
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000474 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000475 return 2;
476 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000477
Akira Hatanaka20ada982011-04-01 17:39:08 +0000478 // One way branch.
479 // Unconditional branch.
480 if (Cond.empty())
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000481 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000482 else // Conditional branch.
483 BuildCondBr(MBB, TBB, DL, Cond);
484 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000485}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000486
487unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000488RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000489{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000490 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
491 MachineBasicBlock::reverse_iterator FirstBr;
492 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000493
Akira Hatanaka20ada982011-04-01 17:39:08 +0000494 // Skip all the debug instructions.
495 while (I != REnd && I->isDebugValue())
496 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000497
Akira Hatanaka20ada982011-04-01 17:39:08 +0000498 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000499
Akira Hatanaka20ada982011-04-01 17:39:08 +0000500 // Up to 2 branches are removed.
501 // Note that indirect branches are not removed.
502 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
503 if (!GetAnalyzableBrOpc(I->getOpcode()))
504 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000505
Akira Hatanaka20ada982011-04-01 17:39:08 +0000506 MBB.erase(I.base(), FirstBr.base());
507
508 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000509}
510
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000511/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000512/// specified Branch instruction.
513bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000514ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000515{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000516 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000517 "Invalid Mips branch condition!");
Akira Hatanaka20ada982011-04-01 17:39:08 +0000518 Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000519 return false;
520}
Dan Gohman99114052009-06-03 20:30:14 +0000521
Akira Hatanakad4b48b22012-06-14 01:16:45 +0000522/// Return the number of bytes of code the specified instruction may be.
523unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
524 switch (MI->getOpcode()) {
525 default:
526 return MI->getDesc().getSize();
527 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
528 const MachineFunction *MF = MI->getParent()->getParent();
529 const char *AsmStr = MI->getOperand(0).getSymbolName();
530 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
531 }
532 }
533}
534
535unsigned
536llvm::Mips::loadImmediate(int64_t Imm, bool IsN64, const TargetInstrInfo &TII,
537 MachineBasicBlock& MBB,
538 MachineBasicBlock::iterator II, DebugLoc DL,
539 bool LastInstrIsADDiu,
540 MipsAnalyzeImmediate::Inst *LastInst) {
541 MipsAnalyzeImmediate AnalyzeImm;
542 unsigned Size = IsN64 ? 64 : 32;
543 unsigned LUi = IsN64 ? Mips::LUi64 : Mips::LUi;
544 unsigned ZEROReg = IsN64 ? Mips::ZERO_64 : Mips::ZERO;
545 unsigned ATReg = IsN64 ? Mips::AT_64 : Mips::AT;
546
547 const MipsAnalyzeImmediate::InstSeq &Seq =
548 AnalyzeImm.Analyze(Imm, Size, LastInstrIsADDiu);
549 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
550
551 if (LastInst && (Seq.size() == 1)) {
552 *LastInst = *Inst;
553 return 0;
554 }
555
556 // The first instruction can be a LUi, which is different from other
557 // instructions (ADDiu, ORI and SLL) in that it does not have a register
558 // operand.
559 if (Inst->Opc == LUi)
560 BuildMI(MBB, II, DL, TII.get(LUi), ATReg)
561 .addImm(SignExtend64<16>(Inst->ImmOpnd));
562 else
563 BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ZEROReg)
564 .addImm(SignExtend64<16>(Inst->ImmOpnd));
565
566 // Build the remaining instructions in Seq. Skip the last instruction if
567 // LastInst is not 0.
568 for (++Inst; Inst != Seq.end() - !!LastInst; ++Inst)
569 BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ATReg)
570 .addImm(SignExtend64<16>(Inst->ImmOpnd));
571
572 if (LastInst)
573 *LastInst = *Inst;
574
575 return Seq.size() - !!LastInst;
576}