blob: 3a0e114fdcc948be058f796b8e5cbe5c3e26ec25 [file] [log] [blame]
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001//===- MipsInstrInfo.cpp - Mips Instruction Information ---------*- C++ -*-===//
2//
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//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Mips implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000014//#include "Mips.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "MipsInstrInfo.h"
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000016#include "MipsTargetMachine.h"
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"
19#include "MipsGenInstrInfo.inc"
20
21using namespace llvm;
22
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000023MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
Chris Lattner64105522008-01-01 01:03:04 +000024 : TargetInstrInfoImpl(MipsInsts, array_lengthof(MipsInsts)),
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000025 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000026
27static bool isZeroImm(const MachineOperand &op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +000028 return op.isImmediate() && op.getImm() == 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000029}
30
31/// Return true if the instruction is a register to register move and
32/// leave the source and dest operands in the passed parameters.
33bool MipsInstrInfo::
34isMoveInstr(const MachineInstr &MI, unsigned &SrcReg, unsigned &DstReg) const
35{
36 // addu $dst, $src, $zero || addu $dst, $zero, $src
37 // or $dst, $src, $zero || or $dst, $zero, $src
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000038 if ((MI.getOpcode() == Mips::ADDu) || (MI.getOpcode() == Mips::OR)) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000039 if (MI.getOperand(1).getReg() == Mips::ZERO) {
40 DstReg = MI.getOperand(0).getReg();
41 SrcReg = MI.getOperand(2).getReg();
42 return true;
43 } else if (MI.getOperand(2).getReg() == Mips::ZERO) {
44 DstReg = MI.getOperand(0).getReg();
45 SrcReg = MI.getOperand(1).getReg();
46 return true;
47 }
48 }
49
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000050 // mov $fpDst, $fpSrc
51 // mfc $gpDst, $fpSrc
52 // mtc $fpDst, $gpSrc
53 if (MI.getOpcode() == Mips::FMOV_SO32 || MI.getOpcode() == Mips::FMOV_AS32 ||
54 MI.getOpcode() == Mips::FMOV_D32 || MI.getOpcode() == Mips::MFC1A ||
55 MI.getOpcode() == Mips::MFC1 || MI.getOpcode() == Mips::MTC1A ||
56 MI.getOpcode() == Mips::MTC1 ) {
57 DstReg = MI.getOperand(0).getReg();
58 SrcReg = MI.getOperand(1).getReg();
59 return true;
60 }
61
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000062 // addiu $dst, $src, 0
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000063 if (MI.getOpcode() == Mips::ADDiu) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000064 if ((MI.getOperand(1).isRegister()) && (isZeroImm(MI.getOperand(2)))) {
65 DstReg = MI.getOperand(0).getReg();
66 SrcReg = MI.getOperand(1).getReg();
67 return true;
68 }
69 }
70 return false;
71}
72
73/// isLoadFromStackSlot - If the specified machine instruction is a direct
74/// load from a stack slot, return the virtual or physical register number of
75/// the destination along with the FrameIndex of the loaded stack slot. If
76/// not, return 0. This predicate must return 0 if the instruction has
77/// any side effects other than loading from the stack slot.
78unsigned MipsInstrInfo::
79isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const
80{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000081 if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
82 (MI->getOpcode() == Mips::LWC1A) || (MI->getOpcode() == Mips::LDC1)) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000083 if ((MI->getOperand(2).isFrameIndex()) && // is a stack slot
84 (MI->getOperand(1).isImmediate()) && // the imm is zero
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000085 (isZeroImm(MI->getOperand(1)))) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000086 FrameIndex = MI->getOperand(2).getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000087 return MI->getOperand(0).getReg();
88 }
89 }
90
91 return 0;
92}
93
94/// isStoreToStackSlot - If the specified machine instruction is a direct
95/// store to a stack slot, return the virtual or physical register number of
96/// the source reg along with the FrameIndex of the loaded stack slot. If
97/// not, return 0. This predicate must return 0 if the instruction has
98/// any side effects other than storing to the stack slot.
99unsigned MipsInstrInfo::
100isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const
101{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000102 if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
103 (MI->getOpcode() == Mips::SWC1A) || (MI->getOpcode() == Mips::SDC1)) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000104 if ((MI->getOperand(0).isFrameIndex()) && // is a stack slot
105 (MI->getOperand(1).isImmediate()) && // the imm is zero
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000106 (isZeroImm(MI->getOperand(1)))) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000107 FrameIndex = MI->getOperand(0).getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000108 return MI->getOperand(2).getReg();
109 }
110 }
111 return 0;
112}
113
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000114/// insertNoop - If data hazard condition is found insert the target nop
115/// instruction.
116void MipsInstrInfo::
117insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
118{
119 BuildMI(MBB, MI, get(Mips::NOP));
120}
121
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000122void MipsInstrInfo::
123copyRegToReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
124 unsigned DestReg, unsigned SrcReg,
125 const TargetRegisterClass *DestRC,
126 const TargetRegisterClass *SrcRC) const {
127 if (DestRC != SrcRC) {
128 if ((DestRC == Mips::CPURegsRegisterClass) &&
129 (SrcRC == Mips::FGR32RegisterClass))
130 BuildMI(MBB, I, get(Mips::MFC1), DestReg).addReg(SrcReg);
131 else if ((DestRC == Mips::CPURegsRegisterClass) &&
132 (SrcRC == Mips::AFGR32RegisterClass))
133 BuildMI(MBB, I, get(Mips::MFC1A), DestReg).addReg(SrcReg);
134 else if ((DestRC == Mips::FGR32RegisterClass) &&
135 (SrcRC == Mips::CPURegsRegisterClass))
136 BuildMI(MBB, I, get(Mips::MTC1), DestReg).addReg(SrcReg);
137 else if ((DestRC == Mips::AFGR32RegisterClass) &&
138 (SrcRC == Mips::CPURegsRegisterClass))
139 BuildMI(MBB, I, get(Mips::MTC1A), DestReg).addReg(SrcReg);
140 else
141 assert (0 && "DestRC != SrcRC, Can't copy this register");
142 }
143
144 if (DestRC == Mips::CPURegsRegisterClass)
145 BuildMI(MBB, I, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
146 .addReg(SrcReg);
147 else if (DestRC == Mips::FGR32RegisterClass)
148 BuildMI(MBB, I, get(Mips::FMOV_SO32), DestReg).addReg(SrcReg);
149 else if (DestRC == Mips::AFGR32RegisterClass)
150 BuildMI(MBB, I, get(Mips::FMOV_AS32), DestReg).addReg(SrcReg);
151 else if (DestRC == Mips::AFGR64RegisterClass)
152 BuildMI(MBB, I, get(Mips::FMOV_D32), DestReg).addReg(SrcReg);
153 else
154 assert (0 && "Can't copy this register");
155}
156
157void MipsInstrInfo::
158storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
159 unsigned SrcReg, bool isKill, int FI,
160 const TargetRegisterClass *RC) const
161{
162 unsigned Opc;
163 if (RC == Mips::CPURegsRegisterClass)
164 Opc = Mips::SW;
165 else if (RC == Mips::FGR32RegisterClass)
166 Opc = Mips::SWC1;
167 else if (RC == Mips::AFGR32RegisterClass)
168 Opc = Mips::SWC1A;
169 else if (RC == Mips::AFGR64RegisterClass)
170 Opc = Mips::SDC1;
171 else
172 assert(0 && "Can't store this register to stack slot");
173
174 BuildMI(MBB, I, get(Opc)).addReg(SrcReg, false, false, isKill)
175 .addImm(0).addFrameIndex(FI);
176}
177
178void MipsInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
179 bool isKill, SmallVectorImpl<MachineOperand> &Addr,
180 const TargetRegisterClass *RC, SmallVectorImpl<MachineInstr*> &NewMIs) const
181{
182 unsigned Opc;
183 if (RC == Mips::CPURegsRegisterClass)
184 Opc = Mips::SW;
185 else if (RC == Mips::FGR32RegisterClass)
186 Opc = Mips::SWC1;
187 else if (RC == Mips::AFGR32RegisterClass)
188 Opc = Mips::SWC1A;
189 else if (RC == Mips::AFGR64RegisterClass)
190 Opc = Mips::SDC1;
191 else
192 assert(0 && "Can't store this register");
193
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000194 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000195 .addReg(SrcReg, false, false, isKill);
196 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
197 MachineOperand &MO = Addr[i];
198 if (MO.isRegister())
199 MIB.addReg(MO.getReg());
200 else if (MO.isImmediate())
201 MIB.addImm(MO.getImm());
202 else
203 MIB.addFrameIndex(MO.getIndex());
204 }
205 NewMIs.push_back(MIB);
206 return;
207}
208
209void MipsInstrInfo::
210loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
211 unsigned DestReg, int FI,
212 const TargetRegisterClass *RC) const
213{
214 unsigned Opc;
215 if (RC == Mips::CPURegsRegisterClass)
216 Opc = Mips::LW;
217 else if (RC == Mips::FGR32RegisterClass)
218 Opc = Mips::LWC1;
219 else if (RC == Mips::AFGR32RegisterClass)
220 Opc = Mips::LWC1A;
221 else if (RC == Mips::AFGR64RegisterClass)
222 Opc = Mips::LDC1;
223 else
224 assert(0 && "Can't load this register from stack slot");
225
226 BuildMI(MBB, I, get(Opc), DestReg).addImm(0).addFrameIndex(FI);
227}
228
229void MipsInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
230 SmallVectorImpl<MachineOperand> &Addr,
231 const TargetRegisterClass *RC,
232 SmallVectorImpl<MachineInstr*> &NewMIs) const {
233 unsigned Opc;
234 if (RC == Mips::CPURegsRegisterClass)
235 Opc = Mips::LW;
236 else if (RC == Mips::FGR32RegisterClass)
237 Opc = Mips::LWC1;
238 else if (RC == Mips::AFGR32RegisterClass)
239 Opc = Mips::LWC1A;
240 else if (RC == Mips::AFGR64RegisterClass)
241 Opc = Mips::LDC1;
242 else
243 assert(0 && "Can't load this register");
244
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000245 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000246 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
247 MachineOperand &MO = Addr[i];
248 if (MO.isRegister())
249 MIB.addReg(MO.getReg());
250 else if (MO.isImmediate())
251 MIB.addImm(MO.getImm());
252 else
253 MIB.addFrameIndex(MO.getIndex());
254 }
255 NewMIs.push_back(MIB);
256 return;
257}
258
259MachineInstr *MipsInstrInfo::
260foldMemoryOperand(MachineFunction &MF,
261 MachineInstr* MI,
262 SmallVectorImpl<unsigned> &Ops, int FI) const
263{
264 if (Ops.size() != 1) return NULL;
265
266 MachineInstr *NewMI = NULL;
267
268 switch (MI->getOpcode()) {
269 case Mips::ADDu:
270 if ((MI->getOperand(0).isRegister()) &&
271 (MI->getOperand(1).isRegister()) &&
272 (MI->getOperand(1).getReg() == Mips::ZERO) &&
273 (MI->getOperand(2).isRegister())) {
274 if (Ops[0] == 0) { // COPY -> STORE
275 unsigned SrcReg = MI->getOperand(2).getReg();
276 bool isKill = MI->getOperand(2).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000277 NewMI = BuildMI(MF, get(Mips::SW)).addFrameIndex(FI)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000278 .addImm(0).addReg(SrcReg, false, false, isKill);
279 } else { // COPY -> LOAD
280 unsigned DstReg = MI->getOperand(0).getReg();
281 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000282 NewMI = BuildMI(MF, get(Mips::LW))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000283 .addReg(DstReg, true, false, false, isDead)
284 .addImm(0).addFrameIndex(FI);
285 }
286 }
287 break;
288 case Mips::FMOV_SO32:
289 case Mips::FMOV_AS32:
290 case Mips::FMOV_D32:
291 if ((MI->getOperand(0).isRegister()) &&
292 (MI->getOperand(1).isRegister())) {
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +0000293 const TargetRegisterClass
294 *RC = RI.getRegClass(MI->getOperand(0).getReg());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000295 unsigned StoreOpc, LoadOpc;
296
297 if (RC == Mips::FGR32RegisterClass) {
298 LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
299 } else if (RC == Mips::AFGR32RegisterClass) {
300 LoadOpc = Mips::LWC1A; StoreOpc = Mips::SWC1A;
301 } else if (RC == Mips::AFGR64RegisterClass) {
302 LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
303 } else
304 assert(0 && "foldMemoryOperand register unknown");
305
306 if (Ops[0] == 0) { // COPY -> STORE
307 unsigned SrcReg = MI->getOperand(1).getReg();
308 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000309 NewMI = BuildMI(MF, get(StoreOpc)).addFrameIndex(FI)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000310 .addImm(0).addReg(SrcReg, false, false, isKill);
311 } else { // COPY -> LOAD
312 unsigned DstReg = MI->getOperand(0).getReg();
313 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000314 NewMI = BuildMI(MF, get(LoadOpc))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000315 .addReg(DstReg, true, false, false, isDead)
316 .addImm(0).addFrameIndex(FI);
317 }
318 }
319 break;
320 }
321
322 return NewMI;
323}
324
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000325//===----------------------------------------------------------------------===//
326// Branch Analysis
327//===----------------------------------------------------------------------===//
328
329/// GetCondFromBranchOpc - Return the Mips CC that matches
330/// the correspondent Branch instruction opcode.
331static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc)
332{
333 switch (BrOpc) {
334 default: return Mips::COND_INVALID;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000335 case Mips::BEQ : return Mips::COND_E;
336 case Mips::BNE : return Mips::COND_NE;
337 case Mips::BGTZ : return Mips::COND_GZ;
338 case Mips::BGEZ : return Mips::COND_GEZ;
339 case Mips::BLTZ : return Mips::COND_LZ;
340 case Mips::BLEZ : return Mips::COND_LEZ;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000341 }
342}
343
344/// GetCondBranchFromCond - Return the Branch instruction
345/// opcode that matches the cc.
346unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC)
347{
348 switch (CC) {
349 default: assert(0 && "Illegal condition code!");
350 case Mips::COND_E : return Mips::BEQ;
351 case Mips::COND_NE : return Mips::BNE;
352 case Mips::COND_GZ : return Mips::BGTZ;
353 case Mips::COND_GEZ : return Mips::BGEZ;
354 case Mips::COND_LZ : return Mips::BLTZ;
355 case Mips::COND_LEZ : return Mips::BLEZ;
356 }
357}
358
359/// GetOppositeBranchCondition - Return the inverse of the specified
360/// condition, e.g. turning COND_E to COND_NE.
361Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
362{
363 switch (CC) {
364 default: assert(0 && "Illegal condition code!");
365 case Mips::COND_E : return Mips::COND_NE;
366 case Mips::COND_NE : return Mips::COND_E;
367 case Mips::COND_GZ : return Mips::COND_LEZ;
368 case Mips::COND_GEZ : return Mips::COND_LZ;
369 case Mips::COND_LZ : return Mips::COND_GEZ;
370 case Mips::COND_LEZ : return Mips::COND_GZ;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000371 case Mips::FCOND_F : return Mips::FCOND_T;
372 case Mips::FCOND_UN : return Mips::FCOND_OR;
373 case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
374 case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
375 case Mips::FCOND_OLT: return Mips::FCOND_UGE;
376 case Mips::FCOND_ULT: return Mips::FCOND_OGE;
377 case Mips::FCOND_OLE: return Mips::FCOND_UGT;
378 case Mips::FCOND_ULE: return Mips::FCOND_OGT;
379 case Mips::FCOND_SF: return Mips::FCOND_ST;
380 case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
381 case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
382 case Mips::FCOND_NGL: return Mips::FCOND_GL;
383 case Mips::FCOND_LT: return Mips::FCOND_NLT;
384 case Mips::FCOND_NGE: return Mips::FCOND_GE;
385 case Mips::FCOND_LE: return Mips::FCOND_NLE;
386 case Mips::FCOND_NGT: return Mips::FCOND_GT;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000387 }
388}
389
390bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
391 MachineBasicBlock *&TBB,
392 MachineBasicBlock *&FBB,
393 std::vector<MachineOperand> &Cond) const
394{
395 // If the block has no terminators, it just falls into the block after it.
396 MachineBasicBlock::iterator I = MBB.end();
397 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
398 return false;
399
400 // Get the last instruction in the block.
401 MachineInstr *LastInst = I;
402
403 // If there is only one terminator instruction, process it.
404 unsigned LastOpc = LastInst->getOpcode();
405 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000406 if (!LastInst->getDesc().isBranch())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000407 return true;
408
409 // Unconditional branch
410 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000411 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000412 return false;
413 }
414
415 Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
416 if (BranchCode == Mips::COND_INVALID)
417 return true; // Can't handle indirect branch.
418
419 // Conditional branch
420 // Block ends with fall-through condbranch.
421 if (LastOpc != Mips::COND_INVALID) {
422 int LastNumOp = LastInst->getNumOperands();
423
Chris Lattner8aa797a2007-12-30 23:10:15 +0000424 TBB = LastInst->getOperand(LastNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000425 Cond.push_back(MachineOperand::CreateImm(BranchCode));
426
427 for (int i=0; i<LastNumOp-1; i++) {
428 Cond.push_back(LastInst->getOperand(i));
429 }
430
431 return false;
432 }
433 }
434
435 // Get the instruction before it if it is a terminator.
436 MachineInstr *SecondLastInst = I;
437
438 // If there are three terminators, we don't know what sort of block this is.
439 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
440 return true;
441
442 // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
443 unsigned SecondLastOpc = SecondLastInst->getOpcode();
444 Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
445
446 if (SecondLastOpc != Mips::COND_INVALID && LastOpc == Mips::J) {
447 int SecondNumOp = SecondLastInst->getNumOperands();
448
Chris Lattner8aa797a2007-12-30 23:10:15 +0000449 TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000450 Cond.push_back(MachineOperand::CreateImm(BranchCode));
451
452 for (int i=0; i<SecondNumOp-1; i++) {
453 Cond.push_back(SecondLastInst->getOperand(i));
454 }
455
Chris Lattner8aa797a2007-12-30 23:10:15 +0000456 FBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000457 return false;
458 }
459
460 // If the block ends with two unconditional branches, handle it. The last
461 // one is not executed, so remove it.
462 if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000463 TBB = SecondLastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000464 I = LastInst;
465 I->eraseFromParent();
466 return false;
467 }
468
469 // Otherwise, can't handle this.
470 return true;
471}
472
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000473unsigned MipsInstrInfo::
474InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
475 MachineBasicBlock *FBB, const std::vector<MachineOperand> &Cond)
476 const
477{
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000478 // Shouldn't be a fall through.
479 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
480 assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
481 "Mips branch conditions can have two|three components!");
482
483 if (FBB == 0) { // One way branch.
484 if (Cond.empty()) {
485 // Unconditional branch?
486 BuildMI(&MBB, get(Mips::J)).addMBB(TBB);
487 } else {
488 // Conditional branch.
489 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000490 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000491
Chris Lattner349c4952008-01-07 03:13:06 +0000492 if (TID.getNumOperands() == 3)
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000493 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
494 .addReg(Cond[2].getReg())
495 .addMBB(TBB);
496 else
497 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
498 .addMBB(TBB);
499
500 }
501 return 1;
502 }
503
504 // Two-way Conditional branch.
505 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000506 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000507
Chris Lattner349c4952008-01-07 03:13:06 +0000508 if (TID.getNumOperands() == 3)
Chris Lattner749c6f62008-01-07 07:27:27 +0000509 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000510 .addMBB(TBB);
511 else
Chris Lattner749c6f62008-01-07 07:27:27 +0000512 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000513
514 BuildMI(&MBB, get(Mips::J)).addMBB(FBB);
515 return 2;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000516}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000517
518unsigned MipsInstrInfo::
519RemoveBranch(MachineBasicBlock &MBB) const
520{
521 MachineBasicBlock::iterator I = MBB.end();
522 if (I == MBB.begin()) return 0;
523 --I;
524 if (I->getOpcode() != Mips::J &&
525 GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
526 return 0;
527
528 // Remove the branch.
529 I->eraseFromParent();
530
531 I = MBB.end();
532
533 if (I == MBB.begin()) return 1;
534 --I;
535 if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
536 return 1;
537
538 // Remove the branch.
539 I->eraseFromParent();
540 return 2;
541}
542
543/// BlockHasNoFallThrough - Analyse if MachineBasicBlock does not
544/// fall-through into its successor block.
545bool MipsInstrInfo::
546BlockHasNoFallThrough(MachineBasicBlock &MBB) const
547{
548 if (MBB.empty()) return false;
549
550 switch (MBB.back().getOpcode()) {
551 case Mips::RET: // Return.
552 case Mips::JR: // Indirect branch.
553 case Mips::J: // Uncond branch.
554 return true;
555 default: return false;
556 }
557}
558
559/// ReverseBranchCondition - Return the inverse opcode of the
560/// specified Branch instruction.
561bool MipsInstrInfo::
562ReverseBranchCondition(std::vector<MachineOperand> &Cond) const
563{
564 assert( (Cond.size() == 3 || Cond.size() == 2) &&
565 "Invalid Mips branch condition!");
566 Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));
567 return false;
568}