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