blob: 687237f972481a2dbf5b4b73fdec54f324481cf9 [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
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000193 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000194 .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
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000244 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000245 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();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000276 NewMI = BuildMI(MF, get(Mips::SW)).addFrameIndex(FI)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000277 .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();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000281 NewMI = BuildMI(MF, get(Mips::LW))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000282 .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())) {
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +0000292 const TargetRegisterClass
293 *RC = RI.getRegClass(MI->getOperand(0).getReg());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000294 unsigned StoreOpc, LoadOpc;
295
296 if (RC == Mips::FGR32RegisterClass) {
297 LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
298 } else if (RC == Mips::AFGR32RegisterClass) {
299 LoadOpc = Mips::LWC1A; StoreOpc = Mips::SWC1A;
300 } else if (RC == Mips::AFGR64RegisterClass) {
301 LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
302 } else
303 assert(0 && "foldMemoryOperand register unknown");
304
305 if (Ops[0] == 0) { // COPY -> STORE
306 unsigned SrcReg = MI->getOperand(1).getReg();
307 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000308 NewMI = BuildMI(MF, get(StoreOpc)).addFrameIndex(FI)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000309 .addImm(0).addReg(SrcReg, false, false, isKill);
310 } else { // COPY -> LOAD
311 unsigned DstReg = MI->getOperand(0).getReg();
312 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000313 NewMI = BuildMI(MF, get(LoadOpc))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000314 .addReg(DstReg, true, false, false, isDead)
315 .addImm(0).addFrameIndex(FI);
316 }
317 }
318 break;
319 }
320
321 return NewMI;
322}
323
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000324//===----------------------------------------------------------------------===//
325// Branch Analysis
326//===----------------------------------------------------------------------===//
327
328/// GetCondFromBranchOpc - Return the Mips CC that matches
329/// the correspondent Branch instruction opcode.
330static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc)
331{
332 switch (BrOpc) {
333 default: return Mips::COND_INVALID;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000334 case Mips::BEQ : return Mips::COND_E;
335 case Mips::BNE : return Mips::COND_NE;
336 case Mips::BGTZ : return Mips::COND_GZ;
337 case Mips::BGEZ : return Mips::COND_GEZ;
338 case Mips::BLTZ : return Mips::COND_LZ;
339 case Mips::BLEZ : return Mips::COND_LEZ;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000340 }
341}
342
343/// GetCondBranchFromCond - Return the Branch instruction
344/// opcode that matches the cc.
345unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC)
346{
347 switch (CC) {
348 default: assert(0 && "Illegal condition code!");
349 case Mips::COND_E : return Mips::BEQ;
350 case Mips::COND_NE : return Mips::BNE;
351 case Mips::COND_GZ : return Mips::BGTZ;
352 case Mips::COND_GEZ : return Mips::BGEZ;
353 case Mips::COND_LZ : return Mips::BLTZ;
354 case Mips::COND_LEZ : return Mips::BLEZ;
355 }
356}
357
358/// GetOppositeBranchCondition - Return the inverse of the specified
359/// condition, e.g. turning COND_E to COND_NE.
360Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
361{
362 switch (CC) {
363 default: assert(0 && "Illegal condition code!");
364 case Mips::COND_E : return Mips::COND_NE;
365 case Mips::COND_NE : return Mips::COND_E;
366 case Mips::COND_GZ : return Mips::COND_LEZ;
367 case Mips::COND_GEZ : return Mips::COND_LZ;
368 case Mips::COND_LZ : return Mips::COND_GEZ;
369 case Mips::COND_LEZ : return Mips::COND_GZ;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000370 case Mips::FCOND_F : return Mips::FCOND_T;
371 case Mips::FCOND_UN : return Mips::FCOND_OR;
372 case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
373 case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
374 case Mips::FCOND_OLT: return Mips::FCOND_UGE;
375 case Mips::FCOND_ULT: return Mips::FCOND_OGE;
376 case Mips::FCOND_OLE: return Mips::FCOND_UGT;
377 case Mips::FCOND_ULE: return Mips::FCOND_OGT;
378 case Mips::FCOND_SF: return Mips::FCOND_ST;
379 case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
380 case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
381 case Mips::FCOND_NGL: return Mips::FCOND_GL;
382 case Mips::FCOND_LT: return Mips::FCOND_NLT;
383 case Mips::FCOND_NGE: return Mips::FCOND_GE;
384 case Mips::FCOND_LE: return Mips::FCOND_NLE;
385 case Mips::FCOND_NGT: return Mips::FCOND_GT;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000386 }
387}
388
389bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
390 MachineBasicBlock *&TBB,
391 MachineBasicBlock *&FBB,
392 std::vector<MachineOperand> &Cond) const
393{
394 // If the block has no terminators, it just falls into the block after it.
395 MachineBasicBlock::iterator I = MBB.end();
396 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
397 return false;
398
399 // Get the last instruction in the block.
400 MachineInstr *LastInst = I;
401
402 // If there is only one terminator instruction, process it.
403 unsigned LastOpc = LastInst->getOpcode();
404 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000405 if (!LastInst->getDesc().isBranch())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000406 return true;
407
408 // Unconditional branch
409 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000410 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000411 return false;
412 }
413
414 Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
415 if (BranchCode == Mips::COND_INVALID)
416 return true; // Can't handle indirect branch.
417
418 // Conditional branch
419 // Block ends with fall-through condbranch.
420 if (LastOpc != Mips::COND_INVALID) {
421 int LastNumOp = LastInst->getNumOperands();
422
Chris Lattner8aa797a2007-12-30 23:10:15 +0000423 TBB = LastInst->getOperand(LastNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000424 Cond.push_back(MachineOperand::CreateImm(BranchCode));
425
426 for (int i=0; i<LastNumOp-1; i++) {
427 Cond.push_back(LastInst->getOperand(i));
428 }
429
430 return false;
431 }
432 }
433
434 // Get the instruction before it if it is a terminator.
435 MachineInstr *SecondLastInst = I;
436
437 // If there are three terminators, we don't know what sort of block this is.
438 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
439 return true;
440
441 // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
442 unsigned SecondLastOpc = SecondLastInst->getOpcode();
443 Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
444
445 if (SecondLastOpc != Mips::COND_INVALID && LastOpc == Mips::J) {
446 int SecondNumOp = SecondLastInst->getNumOperands();
447
Chris Lattner8aa797a2007-12-30 23:10:15 +0000448 TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000449 Cond.push_back(MachineOperand::CreateImm(BranchCode));
450
451 for (int i=0; i<SecondNumOp-1; i++) {
452 Cond.push_back(SecondLastInst->getOperand(i));
453 }
454
Chris Lattner8aa797a2007-12-30 23:10:15 +0000455 FBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000456 return false;
457 }
458
459 // If the block ends with two unconditional branches, handle it. The last
460 // one is not executed, so remove it.
461 if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000462 TBB = SecondLastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000463 I = LastInst;
464 I->eraseFromParent();
465 return false;
466 }
467
468 // Otherwise, can't handle this.
469 return true;
470}
471
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000472unsigned MipsInstrInfo::
473InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
474 MachineBasicBlock *FBB, const std::vector<MachineOperand> &Cond)
475 const
476{
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000477 // Shouldn't be a fall through.
478 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
479 assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
480 "Mips branch conditions can have two|three components!");
481
482 if (FBB == 0) { // One way branch.
483 if (Cond.empty()) {
484 // Unconditional branch?
485 BuildMI(&MBB, get(Mips::J)).addMBB(TBB);
486 } else {
487 // Conditional branch.
488 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000489 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000490
Chris Lattner349c4952008-01-07 03:13:06 +0000491 if (TID.getNumOperands() == 3)
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000492 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
493 .addReg(Cond[2].getReg())
494 .addMBB(TBB);
495 else
496 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
497 .addMBB(TBB);
498
499 }
500 return 1;
501 }
502
503 // Two-way Conditional branch.
504 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000505 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000506
Chris Lattner349c4952008-01-07 03:13:06 +0000507 if (TID.getNumOperands() == 3)
Chris Lattner749c6f62008-01-07 07:27:27 +0000508 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000509 .addMBB(TBB);
510 else
Chris Lattner749c6f62008-01-07 07:27:27 +0000511 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000512
513 BuildMI(&MBB, get(Mips::J)).addMBB(FBB);
514 return 2;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000515}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000516
517unsigned MipsInstrInfo::
518RemoveBranch(MachineBasicBlock &MBB) const
519{
520 MachineBasicBlock::iterator I = MBB.end();
521 if (I == MBB.begin()) return 0;
522 --I;
523 if (I->getOpcode() != Mips::J &&
524 GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
525 return 0;
526
527 // Remove the branch.
528 I->eraseFromParent();
529
530 I = MBB.end();
531
532 if (I == MBB.begin()) return 1;
533 --I;
534 if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
535 return 1;
536
537 // Remove the branch.
538 I->eraseFromParent();
539 return 2;
540}
541
542/// BlockHasNoFallThrough - Analyse if MachineBasicBlock does not
543/// fall-through into its successor block.
544bool MipsInstrInfo::
545BlockHasNoFallThrough(MachineBasicBlock &MBB) const
546{
547 if (MBB.empty()) return false;
548
549 switch (MBB.back().getOpcode()) {
550 case Mips::RET: // Return.
551 case Mips::JR: // Indirect branch.
552 case Mips::J: // Uncond branch.
553 return true;
554 default: return false;
555 }
556}
557
558/// ReverseBranchCondition - Return the inverse opcode of the
559/// specified Branch instruction.
560bool MipsInstrInfo::
561ReverseBranchCondition(std::vector<MachineOperand> &Cond) const
562{
563 assert( (Cond.size() == 3 || Cond.size() == 2) &&
564 "Invalid Mips branch condition!");
565 Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));
566 return false;
567}