blob: 1c26683cb055f6a82a70d1eb5ecac6012d7aba0c [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()),
32 RI(*TM.getSubtargetImpl(), *this) {}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033
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{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000051 if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000052 (MI->getOpcode() == Mips::LDC1)) {
Akira Hatanakad3ac47f2011-07-07 18:57:00 +000053 if ((MI->getOperand(1).isFI()) && // is a stack slot
54 (MI->getOperand(2).isImm()) && // the imm is zero
55 (isZeroImm(MI->getOperand(2)))) {
56 FrameIndex = MI->getOperand(1).getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000057 return MI->getOperand(0).getReg();
58 }
59 }
60
61 return 0;
62}
63
64/// isStoreToStackSlot - If the specified machine instruction is a direct
65/// store to a stack slot, return the virtual or physical register number of
66/// the source reg along with the FrameIndex of the loaded stack slot. If
67/// not, return 0. This predicate must return 0 if the instruction has
68/// any side effects other than storing to the stack slot.
69unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000070isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000071{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000072 if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000073 (MI->getOpcode() == Mips::SDC1)) {
Akira Hatanakad3ac47f2011-07-07 18:57:00 +000074 if ((MI->getOperand(1).isFI()) && // is a stack slot
75 (MI->getOperand(2).isImm()) && // the imm is zero
76 (isZeroImm(MI->getOperand(2)))) {
77 FrameIndex = MI->getOperand(1).getIndex();
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +000078 return MI->getOperand(0).getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000079 }
80 }
81 return 0;
82}
83
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000084/// insertNoop - If data hazard condition is found insert the target nop
85/// instruction.
86void MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000087insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000088{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000089 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +000090 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000091}
92
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +000093void MipsInstrInfo::
94copyPhysReg(MachineBasicBlock &MBB,
95 MachineBasicBlock::iterator I, DebugLoc DL,
96 unsigned DestReg, unsigned SrcReg,
97 bool KillSrc) const {
Akira Hatanaka2ad76682011-10-03 20:38:08 +000098 unsigned Opc = 0, ZeroReg = 0;
Bill Wendlingd1c321a2009-02-12 00:02:55 +000099
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000100 if (Mips::CPURegsRegClass.contains(DestReg)) { // Copy to CPU Reg.
101 if (Mips::CPURegsRegClass.contains(SrcReg))
102 Opc = Mips::ADDu, ZeroReg = Mips::ZERO;
103 else if (Mips::CCRRegClass.contains(SrcReg))
104 Opc = Mips::CFC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000105 else if (Mips::FGR32RegClass.contains(SrcReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000106 Opc = Mips::MFC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000107 else if (SrcReg == Mips::HI)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000108 Opc = Mips::MFHI, SrcReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000109 else if (SrcReg == Mips::LO)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000110 Opc = Mips::MFLO, SrcReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000111 }
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000112 else if (Mips::CPURegsRegClass.contains(SrcReg)) { // Copy from CPU Reg.
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000113 if (Mips::CCRRegClass.contains(DestReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000114 Opc = Mips::CTC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000115 else if (Mips::FGR32RegClass.contains(DestReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000116 Opc = Mips::MTC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000117 else if (DestReg == Mips::HI)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000118 Opc = Mips::MTHI, DestReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000119 else if (DestReg == Mips::LO)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000120 Opc = Mips::MTLO, DestReg = 0;
121 }
122 else if (Mips::FGR32RegClass.contains(DestReg, SrcReg))
Akira Hatanaka4391bb72011-10-08 03:50:18 +0000123 Opc = Mips::FMOV_S;
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000124 else if (Mips::AFGR64RegClass.contains(DestReg, SrcReg))
125 Opc = Mips::FMOV_D32;
126 else if (Mips::CCRRegClass.contains(DestReg, SrcReg))
127 Opc = Mips::MOVCCRToCCR;
128 else if (Mips::CPU64RegsRegClass.contains(DestReg)) { // Copy to CPU64 Reg.
129 if (Mips::CPU64RegsRegClass.contains(SrcReg))
130 Opc = Mips::DADDu, ZeroReg = Mips::ZERO_64;
131 else if (SrcReg == Mips::HI64)
132 Opc = Mips::MFHI64, SrcReg = 0;
133 else if (SrcReg == Mips::LO64)
134 Opc = Mips::MFLO64, SrcReg = 0;
135 }
136 else if (Mips::CPU64RegsRegClass.contains(SrcReg)) { // Copy from CPU64 Reg.
137 if (DestReg == Mips::HI64)
138 Opc = Mips::MTHI64, DestReg = 0;
139 else if (DestReg == Mips::LO64)
140 Opc = Mips::MTLO64, DestReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000141 }
142
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000143 assert(Opc && "Cannot copy registers");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000144
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000145 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
146
147 if (DestReg)
148 MIB.addReg(DestReg, RegState::Define);
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000149
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000150 if (ZeroReg)
151 MIB.addReg(ZeroReg);
152
153 if (SrcReg)
154 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000155}
156
157void MipsInstrInfo::
158storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000159 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000160 const TargetRegisterClass *RC,
161 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000162 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000163 if (I != MBB.end()) DL = I->getDebugLoc();
Akira Hatanaka43aed322011-10-11 00:37:28 +0000164 unsigned Opc = 0;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000165
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000166 if (RC == Mips::CPURegsRegisterClass)
Akira Hatanaka43aed322011-10-11 00:37:28 +0000167 Opc = IsN64 ? Mips::SW_P8 : Mips::SW;
168 else if (RC == Mips::CPU64RegsRegisterClass)
169 Opc = IsN64 ? Mips::SD_P8 : Mips::SD;
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000170 else if (RC == Mips::FGR32RegisterClass)
Akira Hatanaka43aed322011-10-11 00:37:28 +0000171 Opc = Mips::SWC1;
172 else if (RC == Mips::AFGR64RegisterClass)
173 Opc = Mips::SDC1;
174
175 assert(Opc && "Register class not handled!");
176 BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
177 .addFrameIndex(FI).addImm(0);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000178}
179
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000180void MipsInstrInfo::
181loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
182 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000183 const TargetRegisterClass *RC,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000184 const TargetRegisterInfo *TRI) const
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000185{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000186 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000187 if (I != MBB.end()) DL = I->getDebugLoc();
Akira Hatanaka43aed322011-10-11 00:37:28 +0000188 unsigned Opc = 0;
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000189
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000190 if (RC == Mips::CPURegsRegisterClass)
Akira Hatanaka43aed322011-10-11 00:37:28 +0000191 Opc = IsN64 ? Mips::LW_P8 : Mips::LW;
192 else if (RC == Mips::CPU64RegsRegisterClass)
193 Opc = IsN64 ? Mips::LD_P8 : Mips::LD;
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000194 else if (RC == Mips::FGR32RegisterClass)
Akira Hatanaka43aed322011-10-11 00:37:28 +0000195 Opc = Mips::LWC1;
196 else if (RC == Mips::AFGR64RegisterClass)
197 Opc = Mips::LDC1;
198
199 assert(Opc && "Register class not handled!");
200 BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(0);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000201}
202
Akira Hatanakac4f24eb2011-07-01 01:04:43 +0000203MachineInstr*
204MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
205 uint64_t Offset, const MDNode *MDPtr,
206 DebugLoc DL) const {
207 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE))
208 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
209 return &*MIB;
210}
211
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000212//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000213// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000214//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000215
Akira Hatanaka20ada982011-04-01 17:39:08 +0000216static unsigned GetAnalyzableBrOpc(unsigned Opc) {
217 return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ ||
218 Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ ||
219 Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::J) ? Opc : 0;
220}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000221
Akira Hatanaka20ada982011-04-01 17:39:08 +0000222/// GetOppositeBranchOpc - Return the inverse of the specified
223/// opcode, e.g. turning BEQ to BNE.
224unsigned Mips::GetOppositeBranchOpc(unsigned Opc)
225{
226 switch (Opc) {
227 default: llvm_unreachable("Illegal opcode!");
228 case Mips::BEQ : return Mips::BNE;
229 case Mips::BNE : return Mips::BEQ;
230 case Mips::BGTZ : return Mips::BLEZ;
231 case Mips::BGEZ : return Mips::BLTZ;
232 case Mips::BLTZ : return Mips::BGEZ;
233 case Mips::BLEZ : return Mips::BGTZ;
234 case Mips::BC1T : return Mips::BC1F;
235 case Mips::BC1F : return Mips::BC1T;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000236 }
237}
238
Akira Hatanaka20ada982011-04-01 17:39:08 +0000239static void AnalyzeCondBr(const MachineInstr* Inst, unsigned Opc,
240 MachineBasicBlock *&BB,
241 SmallVectorImpl<MachineOperand>& Cond) {
242 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
243 int NumOp = Inst->getNumExplicitOperands();
244
245 // for both int and fp branches, the last explicit operand is the
246 // MBB.
247 BB = Inst->getOperand(NumOp-1).getMBB();
248 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000249
Akira Hatanaka20ada982011-04-01 17:39:08 +0000250 for (int i=0; i<NumOp-1; i++)
251 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000252}
253
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000254bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000255 MachineBasicBlock *&TBB,
256 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000257 SmallVectorImpl<MachineOperand> &Cond,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000258 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000259{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000260 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000261
Akira Hatanaka20ada982011-04-01 17:39:08 +0000262 // Skip all the debug instructions.
263 while (I != REnd && I->isDebugValue())
264 ++I;
265
266 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
267 // If this block ends with no branches (it just falls through to its succ)
268 // just return false, leaving TBB/FBB null.
269 TBB = FBB = NULL;
270 return false;
271 }
272
273 MachineInstr *LastInst = &*I;
274 unsigned LastOpc = LastInst->getOpcode();
275
276 // Not an analyzable branch (must be an indirect jump).
277 if (!GetAnalyzableBrOpc(LastOpc))
278 return true;
279
280 // Get the second to last instruction in the block.
281 unsigned SecondLastOpc = 0;
282 MachineInstr *SecondLastInst = NULL;
283
284 if (++I != REnd) {
285 SecondLastInst = &*I;
286 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
287
288 // Not an analyzable branch (must be an indirect jump).
289 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
290 return true;
291 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000292
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000293 // If there is only one terminator instruction, process it.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000294 if (!SecondLastOpc) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000295 // Unconditional branch
296 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000297 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000298 return false;
299 }
300
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000301 // Conditional branch
Akira Hatanaka20ada982011-04-01 17:39:08 +0000302 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
303 return false;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000304 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000305
Akira Hatanaka20ada982011-04-01 17:39:08 +0000306 // If we reached here, there are two branches.
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000307 // If there are three terminators, we don't know what sort of block this is.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000308 if (++I != REnd && isUnpredicatedTerminator(&*I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000309 return true;
310
Akira Hatanaka20ada982011-04-01 17:39:08 +0000311 // If second to last instruction is an unconditional branch,
312 // analyze it and remove the last instruction.
313 if (SecondLastOpc == Mips::J) {
314 // Return if the last instruction cannot be removed.
315 if (!AllowModify)
316 return true;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000317
Chris Lattner8aa797a2007-12-30 23:10:15 +0000318 TBB = SecondLastInst->getOperand(0).getMBB();
Akira Hatanaka20ada982011-04-01 17:39:08 +0000319 LastInst->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000320 return false;
321 }
322
Akira Hatanaka20ada982011-04-01 17:39:08 +0000323 // Conditional branch followed by an unconditional branch.
324 // The last one must be unconditional.
325 if (LastOpc != Mips::J)
326 return true;
327
328 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
329 FBB = LastInst->getOperand(0).getMBB();
330
331 return false;
332}
333
334void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
335 MachineBasicBlock *TBB, DebugLoc DL,
336 const SmallVectorImpl<MachineOperand>& Cond)
337 const {
338 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000339 const MCInstrDesc &MCID = get(Opc);
340 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000341
342 for (unsigned i = 1; i < Cond.size(); ++i)
343 MIB.addReg(Cond[i].getReg());
344
345 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000346}
347
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000348unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000349InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000350 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000351 const SmallVectorImpl<MachineOperand> &Cond,
352 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000353 // Shouldn't be a fall through.
354 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000355
Akira Hatanaka20ada982011-04-01 17:39:08 +0000356 // # of condition operands:
357 // Unconditional branches: 0
358 // Floating point branches: 1 (opc)
359 // Int BranchZero: 2 (opc, reg)
360 // Int Branch: 3 (opc, reg0, reg1)
361 assert((Cond.size() <= 3) &&
362 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000363
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000364 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000365 if (FBB) {
366 BuildCondBr(MBB, TBB, DL, Cond);
367 BuildMI(&MBB, DL, get(Mips::J)).addMBB(FBB);
368 return 2;
369 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000370
Akira Hatanaka20ada982011-04-01 17:39:08 +0000371 // One way branch.
372 // Unconditional branch.
373 if (Cond.empty())
374 BuildMI(&MBB, DL, get(Mips::J)).addMBB(TBB);
375 else // Conditional branch.
376 BuildCondBr(MBB, TBB, DL, Cond);
377 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000378}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000379
380unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000381RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000382{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000383 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
384 MachineBasicBlock::reverse_iterator FirstBr;
385 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000386
Akira Hatanaka20ada982011-04-01 17:39:08 +0000387 // Skip all the debug instructions.
388 while (I != REnd && I->isDebugValue())
389 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000390
Akira Hatanaka20ada982011-04-01 17:39:08 +0000391 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000392
Akira Hatanaka20ada982011-04-01 17:39:08 +0000393 // Up to 2 branches are removed.
394 // Note that indirect branches are not removed.
395 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
396 if (!GetAnalyzableBrOpc(I->getOpcode()))
397 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000398
Akira Hatanaka20ada982011-04-01 17:39:08 +0000399 MBB.erase(I.base(), FirstBr.base());
400
401 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000402}
403
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000404/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000405/// specified Branch instruction.
406bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000407ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000408{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000409 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000410 "Invalid Mips branch condition!");
Akira Hatanaka20ada982011-04-01 17:39:08 +0000411 Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000412 return false;
413}
Dan Gohman99114052009-06-03 20:30:14 +0000414
415/// getGlobalBaseReg - Return a virtual register initialized with the
416/// the global base register value. Output instructions required to
417/// initialize the register in the function entry block, if necessary.
418///
419unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
420 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
421 unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
422 if (GlobalBaseReg != 0)
423 return GlobalBaseReg;
424
425 // Insert the set of GlobalBaseReg into the first MBB of the function
426 MachineBasicBlock &FirstMBB = MF->front();
427 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
428 MachineRegisterInfo &RegInfo = MF->getRegInfo();
429 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
430
431 GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
Jakob Stoklund Olesen3ecf1f02010-07-10 22:43:03 +0000432 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
433 GlobalBaseReg).addReg(Mips::GP);
Dan Gohman99114052009-06-03 20:30:14 +0000434 RegInfo.addLiveIn(Mips::GP);
435
436 MipsFI->setGlobalBaseReg(GlobalBaseReg);
437 return GlobalBaseReg;
438}