blob: 508d1c2226f9b95263b062dd1b7ec347028c5186 [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 Hatanaka4552c9a2011-04-15 21:51:11 +0000220//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000221// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000222//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000223
Akira Hatanaka20ada982011-04-01 17:39:08 +0000224static unsigned GetAnalyzableBrOpc(unsigned Opc) {
225 return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ ||
226 Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ ||
227 Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::J) ? Opc : 0;
228}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000229
Akira Hatanaka20ada982011-04-01 17:39:08 +0000230/// GetOppositeBranchOpc - Return the inverse of the specified
231/// opcode, e.g. turning BEQ to BNE.
232unsigned Mips::GetOppositeBranchOpc(unsigned Opc)
233{
234 switch (Opc) {
235 default: llvm_unreachable("Illegal opcode!");
236 case Mips::BEQ : return Mips::BNE;
237 case Mips::BNE : return Mips::BEQ;
238 case Mips::BGTZ : return Mips::BLEZ;
239 case Mips::BGEZ : return Mips::BLTZ;
240 case Mips::BLTZ : return Mips::BGEZ;
241 case Mips::BLEZ : return Mips::BGTZ;
242 case Mips::BC1T : return Mips::BC1F;
243 case Mips::BC1F : return Mips::BC1T;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000244 }
245}
246
Akira Hatanaka20ada982011-04-01 17:39:08 +0000247static void AnalyzeCondBr(const MachineInstr* Inst, unsigned Opc,
248 MachineBasicBlock *&BB,
249 SmallVectorImpl<MachineOperand>& Cond) {
250 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
251 int NumOp = Inst->getNumExplicitOperands();
252
253 // for both int and fp branches, the last explicit operand is the
254 // MBB.
255 BB = Inst->getOperand(NumOp-1).getMBB();
256 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000257
Akira Hatanaka20ada982011-04-01 17:39:08 +0000258 for (int i=0; i<NumOp-1; i++)
259 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000260}
261
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000262bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000263 MachineBasicBlock *&TBB,
264 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000265 SmallVectorImpl<MachineOperand> &Cond,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000266 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000267{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000268 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000269
Akira Hatanaka20ada982011-04-01 17:39:08 +0000270 // Skip all the debug instructions.
271 while (I != REnd && I->isDebugValue())
272 ++I;
273
274 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
275 // If this block ends with no branches (it just falls through to its succ)
276 // just return false, leaving TBB/FBB null.
277 TBB = FBB = NULL;
278 return false;
279 }
280
281 MachineInstr *LastInst = &*I;
282 unsigned LastOpc = LastInst->getOpcode();
283
284 // Not an analyzable branch (must be an indirect jump).
285 if (!GetAnalyzableBrOpc(LastOpc))
286 return true;
287
288 // Get the second to last instruction in the block.
289 unsigned SecondLastOpc = 0;
290 MachineInstr *SecondLastInst = NULL;
291
292 if (++I != REnd) {
293 SecondLastInst = &*I;
294 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
295
296 // Not an analyzable branch (must be an indirect jump).
297 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
298 return true;
299 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000300
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000301 // If there is only one terminator instruction, process it.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000302 if (!SecondLastOpc) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000303 // Unconditional branch
304 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000305 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000306 return false;
307 }
308
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000309 // Conditional branch
Akira Hatanaka20ada982011-04-01 17:39:08 +0000310 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
311 return false;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000312 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000313
Akira Hatanaka20ada982011-04-01 17:39:08 +0000314 // If we reached here, there are two branches.
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000315 // If there are three terminators, we don't know what sort of block this is.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000316 if (++I != REnd && isUnpredicatedTerminator(&*I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000317 return true;
318
Akira Hatanaka20ada982011-04-01 17:39:08 +0000319 // If second to last instruction is an unconditional branch,
320 // analyze it and remove the last instruction.
321 if (SecondLastOpc == Mips::J) {
322 // Return if the last instruction cannot be removed.
323 if (!AllowModify)
324 return true;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000325
Chris Lattner8aa797a2007-12-30 23:10:15 +0000326 TBB = SecondLastInst->getOperand(0).getMBB();
Akira Hatanaka20ada982011-04-01 17:39:08 +0000327 LastInst->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000328 return false;
329 }
330
Akira Hatanaka20ada982011-04-01 17:39:08 +0000331 // Conditional branch followed by an unconditional branch.
332 // The last one must be unconditional.
333 if (LastOpc != Mips::J)
334 return true;
335
336 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
337 FBB = LastInst->getOperand(0).getMBB();
338
339 return false;
340}
341
342void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
343 MachineBasicBlock *TBB, DebugLoc DL,
344 const SmallVectorImpl<MachineOperand>& Cond)
345 const {
346 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000347 const MCInstrDesc &MCID = get(Opc);
348 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000349
350 for (unsigned i = 1; i < Cond.size(); ++i)
351 MIB.addReg(Cond[i].getReg());
352
353 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000354}
355
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000356unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000357InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000358 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000359 const SmallVectorImpl<MachineOperand> &Cond,
360 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000361 // Shouldn't be a fall through.
362 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000363
Akira Hatanaka20ada982011-04-01 17:39:08 +0000364 // # of condition operands:
365 // Unconditional branches: 0
366 // Floating point branches: 1 (opc)
367 // Int BranchZero: 2 (opc, reg)
368 // Int Branch: 3 (opc, reg0, reg1)
369 assert((Cond.size() <= 3) &&
370 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000371
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000372 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000373 if (FBB) {
374 BuildCondBr(MBB, TBB, DL, Cond);
375 BuildMI(&MBB, DL, get(Mips::J)).addMBB(FBB);
376 return 2;
377 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000378
Akira Hatanaka20ada982011-04-01 17:39:08 +0000379 // One way branch.
380 // Unconditional branch.
381 if (Cond.empty())
382 BuildMI(&MBB, DL, get(Mips::J)).addMBB(TBB);
383 else // Conditional branch.
384 BuildCondBr(MBB, TBB, DL, Cond);
385 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000386}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000387
388unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000389RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000390{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000391 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
392 MachineBasicBlock::reverse_iterator FirstBr;
393 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000394
Akira Hatanaka20ada982011-04-01 17:39:08 +0000395 // Skip all the debug instructions.
396 while (I != REnd && I->isDebugValue())
397 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000398
Akira Hatanaka20ada982011-04-01 17:39:08 +0000399 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000400
Akira Hatanaka20ada982011-04-01 17:39:08 +0000401 // Up to 2 branches are removed.
402 // Note that indirect branches are not removed.
403 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
404 if (!GetAnalyzableBrOpc(I->getOpcode()))
405 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000406
Akira Hatanaka20ada982011-04-01 17:39:08 +0000407 MBB.erase(I.base(), FirstBr.base());
408
409 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000410}
411
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000412/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000413/// specified Branch instruction.
414bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000415ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000416{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000417 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000418 "Invalid Mips branch condition!");
Akira Hatanaka20ada982011-04-01 17:39:08 +0000419 Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000420 return false;
421}
Dan Gohman99114052009-06-03 20:30:14 +0000422
423/// getGlobalBaseReg - Return a virtual register initialized with the
424/// the global base register value. Output instructions required to
425/// initialize the register in the function entry block, if necessary.
426///
427unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
428 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
429 unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
430 if (GlobalBaseReg != 0)
431 return GlobalBaseReg;
432
433 // Insert the set of GlobalBaseReg into the first MBB of the function
434 MachineBasicBlock &FirstMBB = MF->front();
435 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
436 MachineRegisterInfo &RegInfo = MF->getRegInfo();
437 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
438
439 GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
Jakob Stoklund Olesen3ecf1f02010-07-10 22:43:03 +0000440 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
441 GlobalBaseReg).addReg(Mips::GP);
Dan Gohman99114052009-06-03 20:30:14 +0000442 RegInfo.addLiveIn(Mips::GP);
443
444 MipsFI->setGlobalBaseReg(GlobalBaseReg);
445 return GlobalBaseReg;
446}