blob: ccd846efed62aefe32342b80913501dc69b3a957 [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);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000140 else if ((SrcRC == Mips::CCRRegisterClass) &&
141 (SrcReg == Mips::FCR31))
142 return; // This register is used implicitly, no copy needed.
143 else if ((DestRC == Mips::CCRRegisterClass) &&
144 (DestReg == Mips::FCR31))
145 return; // This register is used implicitly, no copy needed.
146 else
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000147 assert (0 && "DestRC != SrcRC, Can't copy this register");
148 }
149
150 if (DestRC == Mips::CPURegsRegisterClass)
151 BuildMI(MBB, I, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
152 .addReg(SrcReg);
153 else if (DestRC == Mips::FGR32RegisterClass)
154 BuildMI(MBB, I, get(Mips::FMOV_SO32), DestReg).addReg(SrcReg);
155 else if (DestRC == Mips::AFGR32RegisterClass)
156 BuildMI(MBB, I, get(Mips::FMOV_AS32), DestReg).addReg(SrcReg);
157 else if (DestRC == Mips::AFGR64RegisterClass)
158 BuildMI(MBB, I, get(Mips::FMOV_D32), DestReg).addReg(SrcReg);
159 else
160 assert (0 && "Can't copy this register");
161}
162
163void MipsInstrInfo::
164storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
165 unsigned SrcReg, bool isKill, int FI,
166 const TargetRegisterClass *RC) const
167{
168 unsigned Opc;
169 if (RC == Mips::CPURegsRegisterClass)
170 Opc = Mips::SW;
171 else if (RC == Mips::FGR32RegisterClass)
172 Opc = Mips::SWC1;
173 else if (RC == Mips::AFGR32RegisterClass)
174 Opc = Mips::SWC1A;
175 else if (RC == Mips::AFGR64RegisterClass)
176 Opc = Mips::SDC1;
177 else
178 assert(0 && "Can't store this register to stack slot");
179
180 BuildMI(MBB, I, get(Opc)).addReg(SrcReg, false, false, isKill)
181 .addImm(0).addFrameIndex(FI);
182}
183
184void MipsInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
185 bool isKill, SmallVectorImpl<MachineOperand> &Addr,
186 const TargetRegisterClass *RC, SmallVectorImpl<MachineInstr*> &NewMIs) const
187{
188 unsigned Opc;
189 if (RC == Mips::CPURegsRegisterClass)
190 Opc = Mips::SW;
191 else if (RC == Mips::FGR32RegisterClass)
192 Opc = Mips::SWC1;
193 else if (RC == Mips::AFGR32RegisterClass)
194 Opc = Mips::SWC1A;
195 else if (RC == Mips::AFGR64RegisterClass)
196 Opc = Mips::SDC1;
197 else
198 assert(0 && "Can't store this register");
199
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000200 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000201 .addReg(SrcReg, false, false, isKill);
202 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
203 MachineOperand &MO = Addr[i];
204 if (MO.isRegister())
205 MIB.addReg(MO.getReg());
206 else if (MO.isImmediate())
207 MIB.addImm(MO.getImm());
208 else
209 MIB.addFrameIndex(MO.getIndex());
210 }
211 NewMIs.push_back(MIB);
212 return;
213}
214
215void MipsInstrInfo::
216loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
217 unsigned DestReg, int FI,
218 const TargetRegisterClass *RC) const
219{
220 unsigned Opc;
221 if (RC == Mips::CPURegsRegisterClass)
222 Opc = Mips::LW;
223 else if (RC == Mips::FGR32RegisterClass)
224 Opc = Mips::LWC1;
225 else if (RC == Mips::AFGR32RegisterClass)
226 Opc = Mips::LWC1A;
227 else if (RC == Mips::AFGR64RegisterClass)
228 Opc = Mips::LDC1;
229 else
230 assert(0 && "Can't load this register from stack slot");
231
232 BuildMI(MBB, I, get(Opc), DestReg).addImm(0).addFrameIndex(FI);
233}
234
235void MipsInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
236 SmallVectorImpl<MachineOperand> &Addr,
237 const TargetRegisterClass *RC,
238 SmallVectorImpl<MachineInstr*> &NewMIs) const {
239 unsigned Opc;
240 if (RC == Mips::CPURegsRegisterClass)
241 Opc = Mips::LW;
242 else if (RC == Mips::FGR32RegisterClass)
243 Opc = Mips::LWC1;
244 else if (RC == Mips::AFGR32RegisterClass)
245 Opc = Mips::LWC1A;
246 else if (RC == Mips::AFGR64RegisterClass)
247 Opc = Mips::LDC1;
248 else
249 assert(0 && "Can't load this register");
250
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000251 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000252 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
253 MachineOperand &MO = Addr[i];
254 if (MO.isRegister())
255 MIB.addReg(MO.getReg());
256 else if (MO.isImmediate())
257 MIB.addImm(MO.getImm());
258 else
259 MIB.addFrameIndex(MO.getIndex());
260 }
261 NewMIs.push_back(MIB);
262 return;
263}
264
265MachineInstr *MipsInstrInfo::
266foldMemoryOperand(MachineFunction &MF,
267 MachineInstr* MI,
268 SmallVectorImpl<unsigned> &Ops, int FI) const
269{
270 if (Ops.size() != 1) return NULL;
271
272 MachineInstr *NewMI = NULL;
273
274 switch (MI->getOpcode()) {
275 case Mips::ADDu:
276 if ((MI->getOperand(0).isRegister()) &&
277 (MI->getOperand(1).isRegister()) &&
278 (MI->getOperand(1).getReg() == Mips::ZERO) &&
279 (MI->getOperand(2).isRegister())) {
280 if (Ops[0] == 0) { // COPY -> STORE
281 unsigned SrcReg = MI->getOperand(2).getReg();
282 bool isKill = MI->getOperand(2).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000283 NewMI = BuildMI(MF, get(Mips::SW)).addFrameIndex(FI)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000284 .addImm(0).addReg(SrcReg, false, false, isKill);
285 } else { // COPY -> LOAD
286 unsigned DstReg = MI->getOperand(0).getReg();
287 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000288 NewMI = BuildMI(MF, get(Mips::LW))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000289 .addReg(DstReg, true, false, false, isDead)
290 .addImm(0).addFrameIndex(FI);
291 }
292 }
293 break;
294 case Mips::FMOV_SO32:
295 case Mips::FMOV_AS32:
296 case Mips::FMOV_D32:
297 if ((MI->getOperand(0).isRegister()) &&
298 (MI->getOperand(1).isRegister())) {
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +0000299 const TargetRegisterClass
300 *RC = RI.getRegClass(MI->getOperand(0).getReg());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000301 unsigned StoreOpc, LoadOpc;
302
303 if (RC == Mips::FGR32RegisterClass) {
304 LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
305 } else if (RC == Mips::AFGR32RegisterClass) {
306 LoadOpc = Mips::LWC1A; StoreOpc = Mips::SWC1A;
307 } else if (RC == Mips::AFGR64RegisterClass) {
308 LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
309 } else
310 assert(0 && "foldMemoryOperand register unknown");
311
312 if (Ops[0] == 0) { // COPY -> STORE
313 unsigned SrcReg = MI->getOperand(1).getReg();
314 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000315 NewMI = BuildMI(MF, get(StoreOpc)).addFrameIndex(FI)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000316 .addImm(0).addReg(SrcReg, false, false, isKill);
317 } else { // COPY -> LOAD
318 unsigned DstReg = MI->getOperand(0).getReg();
319 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000320 NewMI = BuildMI(MF, get(LoadOpc))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000321 .addReg(DstReg, true, false, false, isDead)
322 .addImm(0).addFrameIndex(FI);
323 }
324 }
325 break;
326 }
327
328 return NewMI;
329}
330
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000331//===----------------------------------------------------------------------===//
332// Branch Analysis
333//===----------------------------------------------------------------------===//
334
335/// GetCondFromBranchOpc - Return the Mips CC that matches
336/// the correspondent Branch instruction opcode.
337static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc)
338{
339 switch (BrOpc) {
340 default: return Mips::COND_INVALID;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000341 case Mips::BEQ : return Mips::COND_E;
342 case Mips::BNE : return Mips::COND_NE;
343 case Mips::BGTZ : return Mips::COND_GZ;
344 case Mips::BGEZ : return Mips::COND_GEZ;
345 case Mips::BLTZ : return Mips::COND_LZ;
346 case Mips::BLEZ : return Mips::COND_LEZ;
347
348 // We dont do fp branch analysis yet!
349 case Mips::BC1T :
350 case Mips::BC1F : return Mips::COND_INVALID;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000351 }
352}
353
354/// GetCondBranchFromCond - Return the Branch instruction
355/// opcode that matches the cc.
356unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC)
357{
358 switch (CC) {
359 default: assert(0 && "Illegal condition code!");
360 case Mips::COND_E : return Mips::BEQ;
361 case Mips::COND_NE : return Mips::BNE;
362 case Mips::COND_GZ : return Mips::BGTZ;
363 case Mips::COND_GEZ : return Mips::BGEZ;
364 case Mips::COND_LZ : return Mips::BLTZ;
365 case Mips::COND_LEZ : return Mips::BLEZ;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000366
367 case Mips::FCOND_F:
368 case Mips::FCOND_UN:
369 case Mips::FCOND_EQ:
370 case Mips::FCOND_UEQ:
371 case Mips::FCOND_OLT:
372 case Mips::FCOND_ULT:
373 case Mips::FCOND_OLE:
374 case Mips::FCOND_ULE:
375 case Mips::FCOND_SF:
376 case Mips::FCOND_NGLE:
377 case Mips::FCOND_SEQ:
378 case Mips::FCOND_NGL:
379 case Mips::FCOND_LT:
380 case Mips::FCOND_NGE:
381 case Mips::FCOND_LE:
382 case Mips::FCOND_NGT: return Mips::BC1T;
383
384 case Mips::FCOND_T:
385 case Mips::FCOND_OR:
386 case Mips::FCOND_NEQ:
387 case Mips::FCOND_OGL:
388 case Mips::FCOND_UGE:
389 case Mips::FCOND_OGE:
390 case Mips::FCOND_UGT:
391 case Mips::FCOND_OGT:
392 case Mips::FCOND_ST:
393 case Mips::FCOND_GLE:
394 case Mips::FCOND_SNE:
395 case Mips::FCOND_GL:
396 case Mips::FCOND_NLT:
397 case Mips::FCOND_GE:
398 case Mips::FCOND_NLE:
399 case Mips::FCOND_GT: return Mips::BC1F;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000400 }
401}
402
403/// GetOppositeBranchCondition - Return the inverse of the specified
404/// condition, e.g. turning COND_E to COND_NE.
405Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
406{
407 switch (CC) {
408 default: assert(0 && "Illegal condition code!");
409 case Mips::COND_E : return Mips::COND_NE;
410 case Mips::COND_NE : return Mips::COND_E;
411 case Mips::COND_GZ : return Mips::COND_LEZ;
412 case Mips::COND_GEZ : return Mips::COND_LZ;
413 case Mips::COND_LZ : return Mips::COND_GEZ;
414 case Mips::COND_LEZ : return Mips::COND_GZ;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000415 case Mips::FCOND_F : return Mips::FCOND_T;
416 case Mips::FCOND_UN : return Mips::FCOND_OR;
417 case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
418 case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
419 case Mips::FCOND_OLT: return Mips::FCOND_UGE;
420 case Mips::FCOND_ULT: return Mips::FCOND_OGE;
421 case Mips::FCOND_OLE: return Mips::FCOND_UGT;
422 case Mips::FCOND_ULE: return Mips::FCOND_OGT;
423 case Mips::FCOND_SF: return Mips::FCOND_ST;
424 case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
425 case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
426 case Mips::FCOND_NGL: return Mips::FCOND_GL;
427 case Mips::FCOND_LT: return Mips::FCOND_NLT;
428 case Mips::FCOND_NGE: return Mips::FCOND_GE;
429 case Mips::FCOND_LE: return Mips::FCOND_NLE;
430 case Mips::FCOND_NGT: return Mips::FCOND_GT;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000431 }
432}
433
434bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
435 MachineBasicBlock *&TBB,
436 MachineBasicBlock *&FBB,
437 std::vector<MachineOperand> &Cond) const
438{
439 // If the block has no terminators, it just falls into the block after it.
440 MachineBasicBlock::iterator I = MBB.end();
441 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
442 return false;
443
444 // Get the last instruction in the block.
445 MachineInstr *LastInst = I;
446
447 // If there is only one terminator instruction, process it.
448 unsigned LastOpc = LastInst->getOpcode();
449 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000450 if (!LastInst->getDesc().isBranch())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000451 return true;
452
453 // Unconditional branch
454 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000455 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000456 return false;
457 }
458
459 Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
460 if (BranchCode == Mips::COND_INVALID)
461 return true; // Can't handle indirect branch.
462
463 // Conditional branch
464 // Block ends with fall-through condbranch.
465 if (LastOpc != Mips::COND_INVALID) {
466 int LastNumOp = LastInst->getNumOperands();
467
Chris Lattner8aa797a2007-12-30 23:10:15 +0000468 TBB = LastInst->getOperand(LastNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000469 Cond.push_back(MachineOperand::CreateImm(BranchCode));
470
471 for (int i=0; i<LastNumOp-1; i++) {
472 Cond.push_back(LastInst->getOperand(i));
473 }
474
475 return false;
476 }
477 }
478
479 // Get the instruction before it if it is a terminator.
480 MachineInstr *SecondLastInst = I;
481
482 // If there are three terminators, we don't know what sort of block this is.
483 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
484 return true;
485
486 // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
487 unsigned SecondLastOpc = SecondLastInst->getOpcode();
488 Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
489
490 if (SecondLastOpc != Mips::COND_INVALID && LastOpc == Mips::J) {
491 int SecondNumOp = SecondLastInst->getNumOperands();
492
Chris Lattner8aa797a2007-12-30 23:10:15 +0000493 TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000494 Cond.push_back(MachineOperand::CreateImm(BranchCode));
495
496 for (int i=0; i<SecondNumOp-1; i++) {
497 Cond.push_back(SecondLastInst->getOperand(i));
498 }
499
Chris Lattner8aa797a2007-12-30 23:10:15 +0000500 FBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000501 return false;
502 }
503
504 // If the block ends with two unconditional branches, handle it. The last
505 // one is not executed, so remove it.
506 if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000507 TBB = SecondLastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000508 I = LastInst;
509 I->eraseFromParent();
510 return false;
511 }
512
513 // Otherwise, can't handle this.
514 return true;
515}
516
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000517unsigned MipsInstrInfo::
518InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
519 MachineBasicBlock *FBB, const std::vector<MachineOperand> &Cond)
520 const
521{
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000522 // Shouldn't be a fall through.
523 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
524 assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
525 "Mips branch conditions can have two|three components!");
526
527 if (FBB == 0) { // One way branch.
528 if (Cond.empty()) {
529 // Unconditional branch?
530 BuildMI(&MBB, get(Mips::J)).addMBB(TBB);
531 } else {
532 // Conditional branch.
533 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000534 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000535
Chris Lattner349c4952008-01-07 03:13:06 +0000536 if (TID.getNumOperands() == 3)
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000537 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
538 .addReg(Cond[2].getReg())
539 .addMBB(TBB);
540 else
541 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
542 .addMBB(TBB);
543
544 }
545 return 1;
546 }
547
548 // Two-way Conditional branch.
549 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000550 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000551
Chris Lattner349c4952008-01-07 03:13:06 +0000552 if (TID.getNumOperands() == 3)
Chris Lattner749c6f62008-01-07 07:27:27 +0000553 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000554 .addMBB(TBB);
555 else
Chris Lattner749c6f62008-01-07 07:27:27 +0000556 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000557
558 BuildMI(&MBB, get(Mips::J)).addMBB(FBB);
559 return 2;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000560}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000561
562unsigned MipsInstrInfo::
563RemoveBranch(MachineBasicBlock &MBB) const
564{
565 MachineBasicBlock::iterator I = MBB.end();
566 if (I == MBB.begin()) return 0;
567 --I;
568 if (I->getOpcode() != Mips::J &&
569 GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
570 return 0;
571
572 // Remove the branch.
573 I->eraseFromParent();
574
575 I = MBB.end();
576
577 if (I == MBB.begin()) return 1;
578 --I;
579 if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
580 return 1;
581
582 // Remove the branch.
583 I->eraseFromParent();
584 return 2;
585}
586
587/// BlockHasNoFallThrough - Analyse if MachineBasicBlock does not
588/// fall-through into its successor block.
589bool MipsInstrInfo::
590BlockHasNoFallThrough(MachineBasicBlock &MBB) const
591{
592 if (MBB.empty()) return false;
593
594 switch (MBB.back().getOpcode()) {
595 case Mips::RET: // Return.
596 case Mips::JR: // Indirect branch.
597 case Mips::J: // Uncond branch.
598 return true;
599 default: return false;
600 }
601}
602
603/// ReverseBranchCondition - Return the inverse opcode of the
604/// specified Branch instruction.
605bool MipsInstrInfo::
606ReverseBranchCondition(std::vector<MachineOperand> &Cond) const
607{
608 assert( (Cond.size() == 3 || Cond.size() == 2) &&
609 "Invalid Mips branch condition!");
610 Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));
611 return false;
612}