blob: ea101f71a65de325aa236249ca3c3b7263279e04 [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===- MipsInstrInfo.cpp - Mips Instruction Information ---------*- C++ -*-===//
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
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014#include "MipsInstrInfo.h"
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000015#include "MipsTargetMachine.h"
Dan Gohman99114052009-06-03 20:30:14 +000016#include "MipsMachineFunction.h"
Akira Hatanaka794bf172011-07-07 23:56:50 +000017#include "InstPrinter/MipsInstPrinter.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman99114052009-06-03 20:30:14 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000020#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000021#include "llvm/Support/TargetRegistry.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000022#include "llvm/ADT/STLExtras.h"
Evan Cheng22fee2d2011-06-28 20:07:07 +000023
Evan Cheng4db3cff2011-07-01 17:57:27 +000024#define GET_INSTRINFO_CTOR
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000025#include "MipsGenInstrInfo.inc"
26
27using namespace llvm;
28
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000029MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
Evan Cheng4db3cff2011-07-01 17:57:27 +000030 : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
Akira Hatanaka43aed322011-10-11 00:37:28 +000031 TM(tm), IsN64(TM.getSubtarget<MipsSubtarget>().isABI_N64()),
Akira Hatanaka6e55ff52011-12-12 22:39:35 +000032 RI(*TM.getSubtargetImpl(), *this),
33 UncondBrOpc(TM.getRelocationModel() == Reloc::PIC_ ? Mips::B : Mips::J) {}
Akira Hatanaka794bf172011-07-07 23:56:50 +000034
35const MipsRegisterInfo &MipsInstrInfo::getRegisterInfo() const {
36 return RI;
37}
38
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000039static bool isZeroImm(const MachineOperand &op) {
Dan Gohmand735b802008-10-03 15:45:36 +000040 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000041}
42
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000043/// isLoadFromStackSlot - If the specified machine instruction is a direct
44/// load from a stack slot, return the virtual or physical register number of
45/// the destination along with the FrameIndex of the loaded stack slot. If
46/// not, return 0. This predicate must return 0 if the instruction has
47/// any side effects other than loading from the stack slot.
48unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000049isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000050{
Akira Hatanaka1acb7df2011-10-11 01:12:52 +000051 unsigned Opc = MI->getOpcode();
52
53 if ((Opc == Mips::LW) || (Opc == Mips::LW_P8) || (Opc == Mips::LD) ||
54 (Opc == Mips::LD_P8) || (Opc == Mips::LWC1) || (Opc == Mips::LWC1_P8) ||
55 (Opc == Mips::LDC1) || (Opc == Mips::LDC164) ||
56 (Opc == Mips::LDC164_P8)) {
Akira Hatanakad3ac47f2011-07-07 18:57:00 +000057 if ((MI->getOperand(1).isFI()) && // is a stack slot
58 (MI->getOperand(2).isImm()) && // the imm is zero
59 (isZeroImm(MI->getOperand(2)))) {
60 FrameIndex = MI->getOperand(1).getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000061 return MI->getOperand(0).getReg();
62 }
63 }
64
65 return 0;
66}
67
68/// isStoreToStackSlot - If the specified machine instruction is a direct
69/// store to a stack slot, return the virtual or physical register number of
70/// the source reg along with the FrameIndex of the loaded stack slot. If
71/// not, return 0. This predicate must return 0 if the instruction has
72/// any side effects other than storing to the stack slot.
73unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000074isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000075{
Akira Hatanaka1acb7df2011-10-11 01:12:52 +000076 unsigned Opc = MI->getOpcode();
77
78 if ((Opc == Mips::SW) || (Opc == Mips::SW_P8) || (Opc == Mips::SD) ||
79 (Opc == Mips::SD_P8) || (Opc == Mips::SWC1) || (Opc == Mips::SWC1_P8) ||
80 (Opc == Mips::SDC1) || (Opc == Mips::SDC164) ||
81 (Opc == Mips::SDC164_P8)) {
Akira Hatanakad3ac47f2011-07-07 18:57:00 +000082 if ((MI->getOperand(1).isFI()) && // is a stack slot
83 (MI->getOperand(2).isImm()) && // the imm is zero
84 (isZeroImm(MI->getOperand(2)))) {
85 FrameIndex = MI->getOperand(1).getIndex();
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +000086 return MI->getOperand(0).getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000087 }
88 }
89 return 0;
90}
91
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000092/// insertNoop - If data hazard condition is found insert the target nop
93/// instruction.
94void MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000095insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000096{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000097 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +000098 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000099}
100
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000101void MipsInstrInfo::
102copyPhysReg(MachineBasicBlock &MBB,
103 MachineBasicBlock::iterator I, DebugLoc DL,
104 unsigned DestReg, unsigned SrcReg,
105 bool KillSrc) const {
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000106 unsigned Opc = 0, ZeroReg = 0;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000107
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000108 if (Mips::CPURegsRegClass.contains(DestReg)) { // Copy to CPU Reg.
109 if (Mips::CPURegsRegClass.contains(SrcReg))
110 Opc = Mips::ADDu, ZeroReg = Mips::ZERO;
111 else if (Mips::CCRRegClass.contains(SrcReg))
112 Opc = Mips::CFC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000113 else if (Mips::FGR32RegClass.contains(SrcReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000114 Opc = Mips::MFC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000115 else if (SrcReg == Mips::HI)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000116 Opc = Mips::MFHI, SrcReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000117 else if (SrcReg == Mips::LO)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000118 Opc = Mips::MFLO, SrcReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000119 }
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000120 else if (Mips::CPURegsRegClass.contains(SrcReg)) { // Copy from CPU Reg.
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000121 if (Mips::CCRRegClass.contains(DestReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000122 Opc = Mips::CTC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000123 else if (Mips::FGR32RegClass.contains(DestReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000124 Opc = Mips::MTC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000125 else if (DestReg == Mips::HI)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000126 Opc = Mips::MTHI, DestReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000127 else if (DestReg == Mips::LO)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000128 Opc = Mips::MTLO, DestReg = 0;
129 }
130 else if (Mips::FGR32RegClass.contains(DestReg, SrcReg))
Akira Hatanaka4391bb72011-10-08 03:50:18 +0000131 Opc = Mips::FMOV_S;
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000132 else if (Mips::AFGR64RegClass.contains(DestReg, SrcReg))
133 Opc = Mips::FMOV_D32;
Akira Hatanaka29d525a2011-11-07 21:35:45 +0000134 else if (Mips::FGR64RegClass.contains(DestReg, SrcReg))
135 Opc = Mips::FMOV_D64;
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000136 else if (Mips::CCRRegClass.contains(DestReg, SrcReg))
137 Opc = Mips::MOVCCRToCCR;
138 else if (Mips::CPU64RegsRegClass.contains(DestReg)) { // Copy to CPU64 Reg.
139 if (Mips::CPU64RegsRegClass.contains(SrcReg))
140 Opc = Mips::DADDu, ZeroReg = Mips::ZERO_64;
141 else if (SrcReg == Mips::HI64)
142 Opc = Mips::MFHI64, SrcReg = 0;
143 else if (SrcReg == Mips::LO64)
144 Opc = Mips::MFLO64, SrcReg = 0;
Akira Hatanaka29d525a2011-11-07 21:35:45 +0000145 else if (Mips::FGR64RegClass.contains(SrcReg))
146 Opc = Mips::DMFC1;
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000147 }
148 else if (Mips::CPU64RegsRegClass.contains(SrcReg)) { // Copy from CPU64 Reg.
149 if (DestReg == Mips::HI64)
150 Opc = Mips::MTHI64, DestReg = 0;
151 else if (DestReg == Mips::LO64)
152 Opc = Mips::MTLO64, DestReg = 0;
Akira Hatanaka29d525a2011-11-07 21:35:45 +0000153 else if (Mips::FGR64RegClass.contains(DestReg))
154 Opc = Mips::DMTC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000155 }
156
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000157 assert(Opc && "Cannot copy registers");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000158
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000159 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
160
161 if (DestReg)
162 MIB.addReg(DestReg, RegState::Define);
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000163
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000164 if (ZeroReg)
165 MIB.addReg(ZeroReg);
166
167 if (SrcReg)
168 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000169}
170
171void MipsInstrInfo::
172storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000173 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000174 const TargetRegisterClass *RC,
175 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000176 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000177 if (I != MBB.end()) DL = I->getDebugLoc();
Akira Hatanaka43aed322011-10-11 00:37:28 +0000178 unsigned Opc = 0;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000179
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000180 if (RC == Mips::CPURegsRegisterClass)
Akira Hatanaka43aed322011-10-11 00:37:28 +0000181 Opc = IsN64 ? Mips::SW_P8 : Mips::SW;
182 else if (RC == Mips::CPU64RegsRegisterClass)
183 Opc = IsN64 ? Mips::SD_P8 : Mips::SD;
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000184 else if (RC == Mips::FGR32RegisterClass)
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000185 Opc = IsN64 ? Mips::SWC1_P8 : Mips::SWC1;
Akira Hatanaka43aed322011-10-11 00:37:28 +0000186 else if (RC == Mips::AFGR64RegisterClass)
187 Opc = Mips::SDC1;
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000188 else if (RC == Mips::FGR64RegisterClass)
189 Opc = IsN64 ? Mips::SDC164_P8 : Mips::SDC164;
Akira Hatanaka43aed322011-10-11 00:37:28 +0000190
191 assert(Opc && "Register class not handled!");
192 BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
193 .addFrameIndex(FI).addImm(0);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000194}
195
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000196void MipsInstrInfo::
197loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
198 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000199 const TargetRegisterClass *RC,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000200 const TargetRegisterInfo *TRI) const
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000201{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000202 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000203 if (I != MBB.end()) DL = I->getDebugLoc();
Akira Hatanaka43aed322011-10-11 00:37:28 +0000204 unsigned Opc = 0;
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000205
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000206 if (RC == Mips::CPURegsRegisterClass)
Akira Hatanaka43aed322011-10-11 00:37:28 +0000207 Opc = IsN64 ? Mips::LW_P8 : Mips::LW;
208 else if (RC == Mips::CPU64RegsRegisterClass)
209 Opc = IsN64 ? Mips::LD_P8 : Mips::LD;
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000210 else if (RC == Mips::FGR32RegisterClass)
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000211 Opc = IsN64 ? Mips::LWC1_P8 : Mips::LWC1;
Akira Hatanaka43aed322011-10-11 00:37:28 +0000212 else if (RC == Mips::AFGR64RegisterClass)
213 Opc = Mips::LDC1;
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000214 else if (RC == Mips::FGR64RegisterClass)
215 Opc = IsN64 ? Mips::LDC164_P8 : Mips::LDC164;
Akira Hatanaka43aed322011-10-11 00:37:28 +0000216
217 assert(Opc && "Register class not handled!");
218 BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(0);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000219}
220
Akira Hatanakac4f24eb2011-07-01 01:04:43 +0000221MachineInstr*
222MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
223 uint64_t Offset, const MDNode *MDPtr,
224 DebugLoc DL) const {
225 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE))
226 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
227 return &*MIB;
228}
229
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000230//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000231// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000232//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000233
Akira Hatanaka20ada982011-04-01 17:39:08 +0000234static unsigned GetAnalyzableBrOpc(unsigned Opc) {
Akira Hatanaka3e3427a2011-10-11 18:49:17 +0000235 return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ ||
236 Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ ||
237 Opc == Mips::BEQ64 || Opc == Mips::BNE64 || Opc == Mips::BGTZ64 ||
238 Opc == Mips::BGEZ64 || Opc == Mips::BLTZ64 || Opc == Mips::BLEZ64 ||
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000239 Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::B ||
240 Opc == Mips::J) ?
Akira Hatanaka3e3427a2011-10-11 18:49:17 +0000241 Opc : 0;
Akira Hatanaka20ada982011-04-01 17:39:08 +0000242}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000243
Akira Hatanaka20ada982011-04-01 17:39:08 +0000244/// GetOppositeBranchOpc - Return the inverse of the specified
245/// opcode, e.g. turning BEQ to BNE.
246unsigned Mips::GetOppositeBranchOpc(unsigned Opc)
247{
248 switch (Opc) {
249 default: llvm_unreachable("Illegal opcode!");
Akira Hatanaka3e3427a2011-10-11 18:49:17 +0000250 case Mips::BEQ : return Mips::BNE;
251 case Mips::BNE : return Mips::BEQ;
252 case Mips::BGTZ : return Mips::BLEZ;
253 case Mips::BGEZ : return Mips::BLTZ;
254 case Mips::BLTZ : return Mips::BGEZ;
255 case Mips::BLEZ : return Mips::BGTZ;
256 case Mips::BEQ64 : return Mips::BNE64;
257 case Mips::BNE64 : return Mips::BEQ64;
258 case Mips::BGTZ64 : return Mips::BLEZ64;
259 case Mips::BGEZ64 : return Mips::BLTZ64;
260 case Mips::BLTZ64 : return Mips::BGEZ64;
261 case Mips::BLEZ64 : return Mips::BGTZ64;
262 case Mips::BC1T : return Mips::BC1F;
263 case Mips::BC1F : return Mips::BC1T;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000264 }
265}
266
Akira Hatanaka20ada982011-04-01 17:39:08 +0000267static void AnalyzeCondBr(const MachineInstr* Inst, unsigned Opc,
268 MachineBasicBlock *&BB,
269 SmallVectorImpl<MachineOperand>& Cond) {
270 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
271 int NumOp = Inst->getNumExplicitOperands();
272
273 // for both int and fp branches, the last explicit operand is the
274 // MBB.
275 BB = Inst->getOperand(NumOp-1).getMBB();
276 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000277
Akira Hatanaka20ada982011-04-01 17:39:08 +0000278 for (int i=0; i<NumOp-1; i++)
279 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000280}
281
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000282bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000283 MachineBasicBlock *&TBB,
284 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000285 SmallVectorImpl<MachineOperand> &Cond,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000286 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000287{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000288 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000289
Akira Hatanaka20ada982011-04-01 17:39:08 +0000290 // Skip all the debug instructions.
291 while (I != REnd && I->isDebugValue())
292 ++I;
293
294 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
295 // If this block ends with no branches (it just falls through to its succ)
296 // just return false, leaving TBB/FBB null.
297 TBB = FBB = NULL;
298 return false;
299 }
300
301 MachineInstr *LastInst = &*I;
302 unsigned LastOpc = LastInst->getOpcode();
303
304 // Not an analyzable branch (must be an indirect jump).
305 if (!GetAnalyzableBrOpc(LastOpc))
306 return true;
307
308 // Get the second to last instruction in the block.
309 unsigned SecondLastOpc = 0;
310 MachineInstr *SecondLastInst = NULL;
311
312 if (++I != REnd) {
313 SecondLastInst = &*I;
314 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
315
316 // Not an analyzable branch (must be an indirect jump).
317 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
318 return true;
319 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000320
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000321 // If there is only one terminator instruction, process it.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000322 if (!SecondLastOpc) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000323 // Unconditional branch
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000324 if (LastOpc == UncondBrOpc) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000325 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000326 return false;
327 }
328
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000329 // Conditional branch
Akira Hatanaka20ada982011-04-01 17:39:08 +0000330 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
331 return false;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000332 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000333
Akira Hatanaka20ada982011-04-01 17:39:08 +0000334 // If we reached here, there are two branches.
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000335 // If there are three terminators, we don't know what sort of block this is.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000336 if (++I != REnd && isUnpredicatedTerminator(&*I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000337 return true;
338
Akira Hatanaka20ada982011-04-01 17:39:08 +0000339 // If second to last instruction is an unconditional branch,
340 // analyze it and remove the last instruction.
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000341 if (SecondLastOpc == UncondBrOpc) {
Akira Hatanaka20ada982011-04-01 17:39:08 +0000342 // Return if the last instruction cannot be removed.
343 if (!AllowModify)
344 return true;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000345
Chris Lattner8aa797a2007-12-30 23:10:15 +0000346 TBB = SecondLastInst->getOperand(0).getMBB();
Akira Hatanaka20ada982011-04-01 17:39:08 +0000347 LastInst->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000348 return false;
349 }
350
Akira Hatanaka20ada982011-04-01 17:39:08 +0000351 // Conditional branch followed by an unconditional branch.
352 // The last one must be unconditional.
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000353 if (LastOpc != UncondBrOpc)
Akira Hatanaka20ada982011-04-01 17:39:08 +0000354 return true;
355
356 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
357 FBB = LastInst->getOperand(0).getMBB();
358
359 return false;
360}
361
362void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
363 MachineBasicBlock *TBB, DebugLoc DL,
364 const SmallVectorImpl<MachineOperand>& Cond)
365 const {
366 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000367 const MCInstrDesc &MCID = get(Opc);
368 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000369
370 for (unsigned i = 1; i < Cond.size(); ++i)
371 MIB.addReg(Cond[i].getReg());
372
373 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000374}
375
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000376unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000377InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000378 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000379 const SmallVectorImpl<MachineOperand> &Cond,
380 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000381 // Shouldn't be a fall through.
382 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000383
Akira Hatanaka20ada982011-04-01 17:39:08 +0000384 // # of condition operands:
385 // Unconditional branches: 0
386 // Floating point branches: 1 (opc)
387 // Int BranchZero: 2 (opc, reg)
388 // Int Branch: 3 (opc, reg0, reg1)
389 assert((Cond.size() <= 3) &&
390 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000391
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000392 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000393 if (FBB) {
394 BuildCondBr(MBB, TBB, DL, Cond);
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000395 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000396 return 2;
397 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000398
Akira Hatanaka20ada982011-04-01 17:39:08 +0000399 // One way branch.
400 // Unconditional branch.
401 if (Cond.empty())
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000402 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000403 else // Conditional branch.
404 BuildCondBr(MBB, TBB, DL, Cond);
405 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000406}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000407
408unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000409RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000410{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000411 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
412 MachineBasicBlock::reverse_iterator FirstBr;
413 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000414
Akira Hatanaka20ada982011-04-01 17:39:08 +0000415 // Skip all the debug instructions.
416 while (I != REnd && I->isDebugValue())
417 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000418
Akira Hatanaka20ada982011-04-01 17:39:08 +0000419 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000420
Akira Hatanaka20ada982011-04-01 17:39:08 +0000421 // Up to 2 branches are removed.
422 // Note that indirect branches are not removed.
423 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
424 if (!GetAnalyzableBrOpc(I->getOpcode()))
425 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000426
Akira Hatanaka20ada982011-04-01 17:39:08 +0000427 MBB.erase(I.base(), FirstBr.base());
428
429 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000430}
431
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000432/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000433/// specified Branch instruction.
434bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000435ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000436{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000437 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000438 "Invalid Mips branch condition!");
Akira Hatanaka20ada982011-04-01 17:39:08 +0000439 Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000440 return false;
441}
Dan Gohman99114052009-06-03 20:30:14 +0000442
443/// getGlobalBaseReg - Return a virtual register initialized with the
444/// the global base register value. Output instructions required to
445/// initialize the register in the function entry block, if necessary.
446///
447unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
448 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
449 unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
450 if (GlobalBaseReg != 0)
451 return GlobalBaseReg;
452
453 // Insert the set of GlobalBaseReg into the first MBB of the function
454 MachineBasicBlock &FirstMBB = MF->front();
455 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
456 MachineRegisterInfo &RegInfo = MF->getRegInfo();
457 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
458
459 GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
Jakob Stoklund Olesen3ecf1f02010-07-10 22:43:03 +0000460 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
461 GlobalBaseReg).addReg(Mips::GP);
Dan Gohman99114052009-06-03 20:30:14 +0000462 RegInfo.addLiveIn(Mips::GP);
463
464 MipsFI->setGlobalBaseReg(GlobalBaseReg);
465 return GlobalBaseReg;
466}