blob: deab5e5ea74236052d76a8297c477fdbfd91bc7e [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"
Owen Anderson718cb662007-09-07 04:06:50 +000017#include "llvm/ADT/STLExtras.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 Cheng22fee2d2011-06-28 20:07:07 +000021
22#define GET_INSTRINFO_MC_DESC
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000023#include "MipsGenInstrInfo.inc"
24
25using namespace llvm;
26
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000027MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
Evan Chengd5b03f22011-06-28 21:14:33 +000028 : TargetInstrInfoImpl(MipsInsts, array_lengthof(MipsInsts),
29 Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000030 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031
32static bool isZeroImm(const MachineOperand &op) {
Dan Gohmand735b802008-10-03 15:45:36 +000033 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034}
35
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036/// isLoadFromStackSlot - If the specified machine instruction is a direct
37/// load from a stack slot, return the virtual or physical register number of
38/// the destination along with the FrameIndex of the loaded stack slot. If
39/// not, return 0. This predicate must return 0 if the instruction has
40/// any side effects other than loading from the stack slot.
41unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000042isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000043{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000044 if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000045 (MI->getOpcode() == Mips::LDC1)) {
Dan Gohmand735b802008-10-03 15:45:36 +000046 if ((MI->getOperand(2).isFI()) && // is a stack slot
47 (MI->getOperand(1).isImm()) && // the imm is zero
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000048 (isZeroImm(MI->getOperand(1)))) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000049 FrameIndex = MI->getOperand(2).getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000050 return MI->getOperand(0).getReg();
51 }
52 }
53
54 return 0;
55}
56
57/// isStoreToStackSlot - If the specified machine instruction is a direct
58/// store to a stack slot, return the virtual or physical register number of
59/// the source reg along with the FrameIndex of the loaded stack slot. If
60/// not, return 0. This predicate must return 0 if the instruction has
61/// any side effects other than storing to the stack slot.
62unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000063isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000064{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000065 if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000066 (MI->getOpcode() == Mips::SDC1)) {
Dan Gohmand735b802008-10-03 15:45:36 +000067 if ((MI->getOperand(2).isFI()) && // is a stack slot
68 (MI->getOperand(1).isImm()) && // the imm is zero
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000069 (isZeroImm(MI->getOperand(1)))) {
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +000070 FrameIndex = MI->getOperand(2).getIndex();
71 return MI->getOperand(0).getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000072 }
73 }
74 return 0;
75}
76
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000077/// insertNoop - If data hazard condition is found insert the target nop
78/// instruction.
79void MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000080insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000081{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000082 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +000083 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000084}
85
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +000086void MipsInstrInfo::
87copyPhysReg(MachineBasicBlock &MBB,
88 MachineBasicBlock::iterator I, DebugLoc DL,
89 unsigned DestReg, unsigned SrcReg,
90 bool KillSrc) const {
91 bool DestCPU = Mips::CPURegsRegClass.contains(DestReg);
92 bool SrcCPU = Mips::CPURegsRegClass.contains(SrcReg);
Bill Wendlingd1c321a2009-02-12 00:02:55 +000093
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +000094 // CPU-CPU is the most common.
95 if (DestCPU && SrcCPU) {
96 BuildMI(MBB, I, DL, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
97 .addReg(SrcReg, getKillRegState(KillSrc));
98 return;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000099 }
100
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000101 // Copy to CPU from other registers.
102 if (DestCPU) {
103 if (Mips::CCRRegClass.contains(SrcReg))
104 BuildMI(MBB, I, DL, get(Mips::CFC1), DestReg)
105 .addReg(SrcReg, getKillRegState(KillSrc));
106 else if (Mips::FGR32RegClass.contains(SrcReg))
107 BuildMI(MBB, I, DL, get(Mips::MFC1), DestReg)
108 .addReg(SrcReg, getKillRegState(KillSrc));
109 else if (SrcReg == Mips::HI)
110 BuildMI(MBB, I, DL, get(Mips::MFHI), DestReg);
111 else if (SrcReg == Mips::LO)
112 BuildMI(MBB, I, DL, get(Mips::MFLO), DestReg);
113 else
114 llvm_unreachable("Copy to CPU from invalid register");
115 return;
116 }
117
118 // Copy to other registers from CPU.
119 if (SrcCPU) {
120 if (Mips::CCRRegClass.contains(DestReg))
121 BuildMI(MBB, I, DL, get(Mips::CTC1), DestReg)
122 .addReg(SrcReg, getKillRegState(KillSrc));
123 else if (Mips::FGR32RegClass.contains(DestReg))
124 BuildMI(MBB, I, DL, get(Mips::MTC1), DestReg)
125 .addReg(SrcReg, getKillRegState(KillSrc));
126 else if (DestReg == Mips::HI)
127 BuildMI(MBB, I, DL, get(Mips::MTHI))
128 .addReg(SrcReg, getKillRegState(KillSrc));
129 else if (DestReg == Mips::LO)
130 BuildMI(MBB, I, DL, get(Mips::MTLO))
131 .addReg(SrcReg, getKillRegState(KillSrc));
132 else
133 llvm_unreachable("Copy from CPU to invalid register");
134 return;
135 }
136
137 if (Mips::FGR32RegClass.contains(DestReg, SrcReg)) {
138 BuildMI(MBB, I, DL, get(Mips::FMOV_S32), DestReg)
139 .addReg(SrcReg, getKillRegState(KillSrc));
140 return;
141 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000142
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000143 if (Mips::AFGR64RegClass.contains(DestReg, SrcReg)) {
144 BuildMI(MBB, I, DL, get(Mips::FMOV_D32), DestReg)
145 .addReg(SrcReg, getKillRegState(KillSrc));
146 return;
147 }
148
149 if (Mips::CCRRegClass.contains(DestReg, SrcReg)) {
150 BuildMI(MBB, I, DL, get(Mips::MOVCCRToCCR), DestReg)
151 .addReg(SrcReg, getKillRegState(KillSrc));
152 return;
153 }
154 llvm_unreachable("Cannot copy registers");
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();
164
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000165 if (RC == Mips::CPURegsRegisterClass)
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000166 BuildMI(MBB, I, DL, get(Mips::SW)).addReg(SrcReg, getKillRegState(isKill))
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000167 .addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000168 else if (RC == Mips::FGR32RegisterClass)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000169 BuildMI(MBB, I, DL, get(Mips::SWC1)).addReg(SrcReg, getKillRegState(isKill))
170 .addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000171 else if (RC == Mips::AFGR64RegisterClass) {
172 if (!TM.getSubtarget<MipsSubtarget>().isMips1()) {
173 BuildMI(MBB, I, DL, get(Mips::SDC1))
174 .addReg(SrcReg, getKillRegState(isKill))
175 .addImm(0).addFrameIndex(FI);
176 } else {
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000177 const TargetRegisterInfo *TRI =
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000178 MBB.getParent()->getTarget().getRegisterInfo();
179 const unsigned *SubSet = TRI->getSubRegisters(SrcReg);
180 BuildMI(MBB, I, DL, get(Mips::SWC1))
181 .addReg(SubSet[0], getKillRegState(isKill))
182 .addImm(0).addFrameIndex(FI);
183 BuildMI(MBB, I, DL, get(Mips::SWC1))
184 .addReg(SubSet[1], getKillRegState(isKill))
185 .addImm(4).addFrameIndex(FI);
186 }
187 } else
188 llvm_unreachable("Register class not handled!");
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000189}
190
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000191void MipsInstrInfo::
192loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
193 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000194 const TargetRegisterClass *RC,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000195 const TargetRegisterInfo *TRI) const
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000196{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000197 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000198 if (I != MBB.end()) DL = I->getDebugLoc();
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000199
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000200 if (RC == Mips::CPURegsRegisterClass)
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000201 BuildMI(MBB, I, DL, get(Mips::LW), DestReg).addImm(0).addFrameIndex(FI);
202 else if (RC == Mips::FGR32RegisterClass)
203 BuildMI(MBB, I, DL, get(Mips::LWC1), DestReg).addImm(0).addFrameIndex(FI);
204 else if (RC == Mips::AFGR64RegisterClass) {
205 if (!TM.getSubtarget<MipsSubtarget>().isMips1()) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000206 BuildMI(MBB, I, DL, get(Mips::LDC1), DestReg).addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000207 } else {
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000208 const TargetRegisterInfo *TRI =
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000209 MBB.getParent()->getTarget().getRegisterInfo();
210 const unsigned *SubSet = TRI->getSubRegisters(DestReg);
211 BuildMI(MBB, I, DL, get(Mips::LWC1), SubSet[0])
212 .addImm(0).addFrameIndex(FI);
213 BuildMI(MBB, I, DL, get(Mips::LWC1), SubSet[1])
214 .addImm(4).addFrameIndex(FI);
215 }
216 } else
217 llvm_unreachable("Register class not handled!");
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000218}
219
Akira Hatanakac4f24eb2011-07-01 01:04:43 +0000220MachineInstr*
221MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
222 uint64_t Offset, const MDNode *MDPtr,
223 DebugLoc DL) const {
224 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE))
225 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
226 return &*MIB;
227}
228
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000229//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000230// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000231//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000232
Akira Hatanaka20ada982011-04-01 17:39:08 +0000233static unsigned GetAnalyzableBrOpc(unsigned Opc) {
234 return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ ||
235 Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ ||
236 Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::J) ? Opc : 0;
237}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000238
Akira Hatanaka20ada982011-04-01 17:39:08 +0000239/// GetOppositeBranchOpc - Return the inverse of the specified
240/// opcode, e.g. turning BEQ to BNE.
241unsigned Mips::GetOppositeBranchOpc(unsigned Opc)
242{
243 switch (Opc) {
244 default: llvm_unreachable("Illegal opcode!");
245 case Mips::BEQ : return Mips::BNE;
246 case Mips::BNE : return Mips::BEQ;
247 case Mips::BGTZ : return Mips::BLEZ;
248 case Mips::BGEZ : return Mips::BLTZ;
249 case Mips::BLTZ : return Mips::BGEZ;
250 case Mips::BLEZ : return Mips::BGTZ;
251 case Mips::BC1T : return Mips::BC1F;
252 case Mips::BC1F : return Mips::BC1T;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000253 }
254}
255
Akira Hatanaka20ada982011-04-01 17:39:08 +0000256static void AnalyzeCondBr(const MachineInstr* Inst, unsigned Opc,
257 MachineBasicBlock *&BB,
258 SmallVectorImpl<MachineOperand>& Cond) {
259 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
260 int NumOp = Inst->getNumExplicitOperands();
261
262 // for both int and fp branches, the last explicit operand is the
263 // MBB.
264 BB = Inst->getOperand(NumOp-1).getMBB();
265 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000266
Akira Hatanaka20ada982011-04-01 17:39:08 +0000267 for (int i=0; i<NumOp-1; i++)
268 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000269}
270
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000271bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000272 MachineBasicBlock *&TBB,
273 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000274 SmallVectorImpl<MachineOperand> &Cond,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000275 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000276{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000277 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000278
Akira Hatanaka20ada982011-04-01 17:39:08 +0000279 // Skip all the debug instructions.
280 while (I != REnd && I->isDebugValue())
281 ++I;
282
283 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
284 // If this block ends with no branches (it just falls through to its succ)
285 // just return false, leaving TBB/FBB null.
286 TBB = FBB = NULL;
287 return false;
288 }
289
290 MachineInstr *LastInst = &*I;
291 unsigned LastOpc = LastInst->getOpcode();
292
293 // Not an analyzable branch (must be an indirect jump).
294 if (!GetAnalyzableBrOpc(LastOpc))
295 return true;
296
297 // Get the second to last instruction in the block.
298 unsigned SecondLastOpc = 0;
299 MachineInstr *SecondLastInst = NULL;
300
301 if (++I != REnd) {
302 SecondLastInst = &*I;
303 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
304
305 // Not an analyzable branch (must be an indirect jump).
306 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
307 return true;
308 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000309
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000310 // If there is only one terminator instruction, process it.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000311 if (!SecondLastOpc) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000312 // Unconditional branch
313 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000314 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000315 return false;
316 }
317
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000318 // Conditional branch
Akira Hatanaka20ada982011-04-01 17:39:08 +0000319 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
320 return false;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000321 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000322
Akira Hatanaka20ada982011-04-01 17:39:08 +0000323 // If we reached here, there are two branches.
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000324 // If there are three terminators, we don't know what sort of block this is.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000325 if (++I != REnd && isUnpredicatedTerminator(&*I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000326 return true;
327
Akira Hatanaka20ada982011-04-01 17:39:08 +0000328 // If second to last instruction is an unconditional branch,
329 // analyze it and remove the last instruction.
330 if (SecondLastOpc == Mips::J) {
331 // Return if the last instruction cannot be removed.
332 if (!AllowModify)
333 return true;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000334
Chris Lattner8aa797a2007-12-30 23:10:15 +0000335 TBB = SecondLastInst->getOperand(0).getMBB();
Akira Hatanaka20ada982011-04-01 17:39:08 +0000336 LastInst->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000337 return false;
338 }
339
Akira Hatanaka20ada982011-04-01 17:39:08 +0000340 // Conditional branch followed by an unconditional branch.
341 // The last one must be unconditional.
342 if (LastOpc != Mips::J)
343 return true;
344
345 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
346 FBB = LastInst->getOperand(0).getMBB();
347
348 return false;
349}
350
351void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
352 MachineBasicBlock *TBB, DebugLoc DL,
353 const SmallVectorImpl<MachineOperand>& Cond)
354 const {
355 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000356 const MCInstrDesc &MCID = get(Opc);
357 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000358
359 for (unsigned i = 1; i < Cond.size(); ++i)
360 MIB.addReg(Cond[i].getReg());
361
362 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000363}
364
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000365unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000366InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000367 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000368 const SmallVectorImpl<MachineOperand> &Cond,
369 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000370 // Shouldn't be a fall through.
371 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000372
Akira Hatanaka20ada982011-04-01 17:39:08 +0000373 // # of condition operands:
374 // Unconditional branches: 0
375 // Floating point branches: 1 (opc)
376 // Int BranchZero: 2 (opc, reg)
377 // Int Branch: 3 (opc, reg0, reg1)
378 assert((Cond.size() <= 3) &&
379 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000380
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000381 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000382 if (FBB) {
383 BuildCondBr(MBB, TBB, DL, Cond);
384 BuildMI(&MBB, DL, get(Mips::J)).addMBB(FBB);
385 return 2;
386 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000387
Akira Hatanaka20ada982011-04-01 17:39:08 +0000388 // One way branch.
389 // Unconditional branch.
390 if (Cond.empty())
391 BuildMI(&MBB, DL, get(Mips::J)).addMBB(TBB);
392 else // Conditional branch.
393 BuildCondBr(MBB, TBB, DL, Cond);
394 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000395}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000396
397unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000398RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000399{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000400 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
401 MachineBasicBlock::reverse_iterator FirstBr;
402 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000403
Akira Hatanaka20ada982011-04-01 17:39:08 +0000404 // Skip all the debug instructions.
405 while (I != REnd && I->isDebugValue())
406 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000407
Akira Hatanaka20ada982011-04-01 17:39:08 +0000408 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000409
Akira Hatanaka20ada982011-04-01 17:39:08 +0000410 // Up to 2 branches are removed.
411 // Note that indirect branches are not removed.
412 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
413 if (!GetAnalyzableBrOpc(I->getOpcode()))
414 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000415
Akira Hatanaka20ada982011-04-01 17:39:08 +0000416 MBB.erase(I.base(), FirstBr.base());
417
418 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000419}
420
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000421/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000422/// specified Branch instruction.
423bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000424ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000425{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000426 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000427 "Invalid Mips branch condition!");
Akira Hatanaka20ada982011-04-01 17:39:08 +0000428 Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000429 return false;
430}
Dan Gohman99114052009-06-03 20:30:14 +0000431
432/// getGlobalBaseReg - Return a virtual register initialized with the
433/// the global base register value. Output instructions required to
434/// initialize the register in the function entry block, if necessary.
435///
436unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
437 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
438 unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
439 if (GlobalBaseReg != 0)
440 return GlobalBaseReg;
441
442 // Insert the set of GlobalBaseReg into the first MBB of the function
443 MachineBasicBlock &FirstMBB = MF->front();
444 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
445 MachineRegisterInfo &RegInfo = MF->getRegInfo();
446 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
447
448 GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
Jakob Stoklund Olesen3ecf1f02010-07-10 22:43:03 +0000449 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
450 GlobalBaseReg).addReg(Mips::GP);
Dan Gohman99114052009-06-03 20:30:14 +0000451 RegInfo.addLiveIn(Mips::GP);
452
453 MipsFI->setGlobalBaseReg(GlobalBaseReg);
454 return GlobalBaseReg;
455}