blob: 6bbf240370a2afe3cf8a7fb8ab6b0543b35e6c90 [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 Lopes972f5892007-06-06 07:42:06 +000014#include "MipsInstrInfo.h"
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000015#include "MipsTargetMachine.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 Lopes43d526d2008-07-14 14:42:54 +000024 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000025
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 Lopes91ef8492008-08-02 19:42:36 +0000103 if ((MI->getOperand(2).isFrameIndex()) && // is a stack slot
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000104 (MI->getOperand(1).isImmediate()) && // the imm is zero
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000105 (isZeroImm(MI->getOperand(1)))) {
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000106 FrameIndex = MI->getOperand(2).getIndex();
107 return MI->getOperand(0).getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000108 }
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);
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000139 else if ((DestRC == Mips::AFGR32RegisterClass) &&
140 (SrcRC == Mips::CPURegsRegisterClass))
141 BuildMI(MBB, I, get(Mips::MTC1A), DestReg).addReg(SrcReg);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000142 else if ((SrcRC == Mips::CCRRegisterClass) &&
143 (SrcReg == Mips::FCR31))
144 return; // This register is used implicitly, no copy needed.
145 else if ((DestRC == Mips::CCRRegisterClass) &&
146 (DestReg == Mips::FCR31))
147 return; // This register is used implicitly, no copy needed.
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000148 else if ((DestRC == Mips::HILORegisterClass) &&
149 (SrcRC == Mips::CPURegsRegisterClass)) {
150 unsigned Opc = (DestReg == Mips::HI) ? Mips::MTHI : Mips::MTLO;
151 BuildMI(MBB, I, get(Opc), DestReg);
152 } else if ((SrcRC == Mips::HILORegisterClass) &&
153 (DestRC == Mips::CPURegsRegisterClass)) {
154 unsigned Opc = (SrcReg == Mips::HI) ? Mips::MFHI : Mips::MFLO;
155 BuildMI(MBB, I, get(Opc), DestReg);
156 } else
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000157 assert (0 && "DestRC != SrcRC, Can't copy this register");
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000158
159 return;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000160 }
161
162 if (DestRC == Mips::CPURegsRegisterClass)
163 BuildMI(MBB, I, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
164 .addReg(SrcReg);
165 else if (DestRC == Mips::FGR32RegisterClass)
166 BuildMI(MBB, I, get(Mips::FMOV_SO32), DestReg).addReg(SrcReg);
167 else if (DestRC == Mips::AFGR32RegisterClass)
168 BuildMI(MBB, I, get(Mips::FMOV_AS32), DestReg).addReg(SrcReg);
169 else if (DestRC == Mips::AFGR64RegisterClass)
170 BuildMI(MBB, I, get(Mips::FMOV_D32), DestReg).addReg(SrcReg);
171 else
172 assert (0 && "Can't copy this register");
173}
174
175void MipsInstrInfo::
176storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
177 unsigned SrcReg, bool isKill, int FI,
178 const TargetRegisterClass *RC) const
179{
180 unsigned Opc;
181 if (RC == Mips::CPURegsRegisterClass)
182 Opc = Mips::SW;
183 else if (RC == Mips::FGR32RegisterClass)
184 Opc = Mips::SWC1;
185 else if (RC == Mips::AFGR32RegisterClass)
186 Opc = Mips::SWC1A;
187 else if (RC == Mips::AFGR64RegisterClass)
188 Opc = Mips::SDC1;
189 else
190 assert(0 && "Can't store this register to stack slot");
191
192 BuildMI(MBB, I, get(Opc)).addReg(SrcReg, false, false, isKill)
193 .addImm(0).addFrameIndex(FI);
194}
195
196void MipsInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
197 bool isKill, SmallVectorImpl<MachineOperand> &Addr,
198 const TargetRegisterClass *RC, SmallVectorImpl<MachineInstr*> &NewMIs) const
199{
200 unsigned Opc;
201 if (RC == Mips::CPURegsRegisterClass)
202 Opc = Mips::SW;
203 else if (RC == Mips::FGR32RegisterClass)
204 Opc = Mips::SWC1;
205 else if (RC == Mips::AFGR32RegisterClass)
206 Opc = Mips::SWC1A;
207 else if (RC == Mips::AFGR64RegisterClass)
208 Opc = Mips::SDC1;
209 else
210 assert(0 && "Can't store this register");
211
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000212 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000213 .addReg(SrcReg, false, false, isKill);
214 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
215 MachineOperand &MO = Addr[i];
216 if (MO.isRegister())
217 MIB.addReg(MO.getReg());
218 else if (MO.isImmediate())
219 MIB.addImm(MO.getImm());
220 else
221 MIB.addFrameIndex(MO.getIndex());
222 }
223 NewMIs.push_back(MIB);
224 return;
225}
226
227void MipsInstrInfo::
228loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
229 unsigned DestReg, int FI,
230 const TargetRegisterClass *RC) const
231{
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 from stack slot");
243
244 BuildMI(MBB, I, get(Opc), DestReg).addImm(0).addFrameIndex(FI);
245}
246
247void MipsInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
248 SmallVectorImpl<MachineOperand> &Addr,
249 const TargetRegisterClass *RC,
250 SmallVectorImpl<MachineInstr*> &NewMIs) const {
251 unsigned Opc;
252 if (RC == Mips::CPURegsRegisterClass)
253 Opc = Mips::LW;
254 else if (RC == Mips::FGR32RegisterClass)
255 Opc = Mips::LWC1;
256 else if (RC == Mips::AFGR32RegisterClass)
257 Opc = Mips::LWC1A;
258 else if (RC == Mips::AFGR64RegisterClass)
259 Opc = Mips::LDC1;
260 else
261 assert(0 && "Can't load this register");
262
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000263 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000264 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
265 MachineOperand &MO = Addr[i];
266 if (MO.isRegister())
267 MIB.addReg(MO.getReg());
268 else if (MO.isImmediate())
269 MIB.addImm(MO.getImm());
270 else
271 MIB.addFrameIndex(MO.getIndex());
272 }
273 NewMIs.push_back(MIB);
274 return;
275}
276
277MachineInstr *MipsInstrInfo::
278foldMemoryOperand(MachineFunction &MF,
279 MachineInstr* MI,
280 SmallVectorImpl<unsigned> &Ops, int FI) const
281{
282 if (Ops.size() != 1) return NULL;
283
284 MachineInstr *NewMI = NULL;
285
286 switch (MI->getOpcode()) {
287 case Mips::ADDu:
288 if ((MI->getOperand(0).isRegister()) &&
289 (MI->getOperand(1).isRegister()) &&
290 (MI->getOperand(1).getReg() == Mips::ZERO) &&
291 (MI->getOperand(2).isRegister())) {
292 if (Ops[0] == 0) { // COPY -> STORE
293 unsigned SrcReg = MI->getOperand(2).getReg();
294 bool isKill = MI->getOperand(2).isKill();
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000295 NewMI = BuildMI(MF, get(Mips::SW)).addReg(SrcReg, false, false, isKill)
296 .addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000297 } else { // COPY -> LOAD
298 unsigned DstReg = MI->getOperand(0).getReg();
299 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000300 NewMI = BuildMI(MF, get(Mips::LW))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000301 .addReg(DstReg, true, false, false, isDead)
302 .addImm(0).addFrameIndex(FI);
303 }
304 }
305 break;
306 case Mips::FMOV_SO32:
307 case Mips::FMOV_AS32:
308 case Mips::FMOV_D32:
309 if ((MI->getOperand(0).isRegister()) &&
310 (MI->getOperand(1).isRegister())) {
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +0000311 const TargetRegisterClass
312 *RC = RI.getRegClass(MI->getOperand(0).getReg());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000313 unsigned StoreOpc, LoadOpc;
314
315 if (RC == Mips::FGR32RegisterClass) {
316 LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
317 } else if (RC == Mips::AFGR32RegisterClass) {
318 LoadOpc = Mips::LWC1A; StoreOpc = Mips::SWC1A;
319 } else if (RC == Mips::AFGR64RegisterClass) {
320 LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
321 } else
322 assert(0 && "foldMemoryOperand register unknown");
323
324 if (Ops[0] == 0) { // COPY -> STORE
325 unsigned SrcReg = MI->getOperand(1).getReg();
326 bool isKill = MI->getOperand(1).isKill();
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000327 NewMI = BuildMI(MF, get(StoreOpc)).addReg(SrcReg, false, false, isKill)
328 .addImm(0).addFrameIndex(FI) ;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000329 } else { // COPY -> LOAD
330 unsigned DstReg = MI->getOperand(0).getReg();
331 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000332 NewMI = BuildMI(MF, get(LoadOpc))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000333 .addReg(DstReg, true, false, false, isDead)
334 .addImm(0).addFrameIndex(FI);
335 }
336 }
337 break;
338 }
339
340 return NewMI;
341}
342
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000343//===----------------------------------------------------------------------===//
344// Branch Analysis
345//===----------------------------------------------------------------------===//
346
347/// GetCondFromBranchOpc - Return the Mips CC that matches
348/// the correspondent Branch instruction opcode.
349static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc)
350{
351 switch (BrOpc) {
352 default: return Mips::COND_INVALID;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000353 case Mips::BEQ : return Mips::COND_E;
354 case Mips::BNE : return Mips::COND_NE;
355 case Mips::BGTZ : return Mips::COND_GZ;
356 case Mips::BGEZ : return Mips::COND_GEZ;
357 case Mips::BLTZ : return Mips::COND_LZ;
358 case Mips::BLEZ : return Mips::COND_LEZ;
359
360 // We dont do fp branch analysis yet!
361 case Mips::BC1T :
362 case Mips::BC1F : return Mips::COND_INVALID;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000363 }
364}
365
366/// GetCondBranchFromCond - Return the Branch instruction
367/// opcode that matches the cc.
368unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC)
369{
370 switch (CC) {
371 default: assert(0 && "Illegal condition code!");
372 case Mips::COND_E : return Mips::BEQ;
373 case Mips::COND_NE : return Mips::BNE;
374 case Mips::COND_GZ : return Mips::BGTZ;
375 case Mips::COND_GEZ : return Mips::BGEZ;
376 case Mips::COND_LZ : return Mips::BLTZ;
377 case Mips::COND_LEZ : return Mips::BLEZ;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000378
379 case Mips::FCOND_F:
380 case Mips::FCOND_UN:
381 case Mips::FCOND_EQ:
382 case Mips::FCOND_UEQ:
383 case Mips::FCOND_OLT:
384 case Mips::FCOND_ULT:
385 case Mips::FCOND_OLE:
386 case Mips::FCOND_ULE:
387 case Mips::FCOND_SF:
388 case Mips::FCOND_NGLE:
389 case Mips::FCOND_SEQ:
390 case Mips::FCOND_NGL:
391 case Mips::FCOND_LT:
392 case Mips::FCOND_NGE:
393 case Mips::FCOND_LE:
394 case Mips::FCOND_NGT: return Mips::BC1T;
395
396 case Mips::FCOND_T:
397 case Mips::FCOND_OR:
398 case Mips::FCOND_NEQ:
399 case Mips::FCOND_OGL:
400 case Mips::FCOND_UGE:
401 case Mips::FCOND_OGE:
402 case Mips::FCOND_UGT:
403 case Mips::FCOND_OGT:
404 case Mips::FCOND_ST:
405 case Mips::FCOND_GLE:
406 case Mips::FCOND_SNE:
407 case Mips::FCOND_GL:
408 case Mips::FCOND_NLT:
409 case Mips::FCOND_GE:
410 case Mips::FCOND_NLE:
411 case Mips::FCOND_GT: return Mips::BC1F;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000412 }
413}
414
415/// GetOppositeBranchCondition - Return the inverse of the specified
416/// condition, e.g. turning COND_E to COND_NE.
417Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
418{
419 switch (CC) {
420 default: assert(0 && "Illegal condition code!");
421 case Mips::COND_E : return Mips::COND_NE;
422 case Mips::COND_NE : return Mips::COND_E;
423 case Mips::COND_GZ : return Mips::COND_LEZ;
424 case Mips::COND_GEZ : return Mips::COND_LZ;
425 case Mips::COND_LZ : return Mips::COND_GEZ;
426 case Mips::COND_LEZ : return Mips::COND_GZ;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000427 case Mips::FCOND_F : return Mips::FCOND_T;
428 case Mips::FCOND_UN : return Mips::FCOND_OR;
429 case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
430 case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
431 case Mips::FCOND_OLT: return Mips::FCOND_UGE;
432 case Mips::FCOND_ULT: return Mips::FCOND_OGE;
433 case Mips::FCOND_OLE: return Mips::FCOND_UGT;
434 case Mips::FCOND_ULE: return Mips::FCOND_OGT;
435 case Mips::FCOND_SF: return Mips::FCOND_ST;
436 case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
437 case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
438 case Mips::FCOND_NGL: return Mips::FCOND_GL;
439 case Mips::FCOND_LT: return Mips::FCOND_NLT;
440 case Mips::FCOND_NGE: return Mips::FCOND_GE;
441 case Mips::FCOND_LE: return Mips::FCOND_NLE;
442 case Mips::FCOND_NGT: return Mips::FCOND_GT;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000443 }
444}
445
446bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
447 MachineBasicBlock *&TBB,
448 MachineBasicBlock *&FBB,
449 std::vector<MachineOperand> &Cond) const
450{
451 // If the block has no terminators, it just falls into the block after it.
452 MachineBasicBlock::iterator I = MBB.end();
453 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
454 return false;
455
456 // Get the last instruction in the block.
457 MachineInstr *LastInst = I;
458
459 // If there is only one terminator instruction, process it.
460 unsigned LastOpc = LastInst->getOpcode();
461 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000462 if (!LastInst->getDesc().isBranch())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000463 return true;
464
465 // Unconditional branch
466 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000467 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000468 return false;
469 }
470
471 Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
472 if (BranchCode == Mips::COND_INVALID)
473 return true; // Can't handle indirect branch.
474
475 // Conditional branch
476 // Block ends with fall-through condbranch.
477 if (LastOpc != Mips::COND_INVALID) {
478 int LastNumOp = LastInst->getNumOperands();
479
Chris Lattner8aa797a2007-12-30 23:10:15 +0000480 TBB = LastInst->getOperand(LastNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000481 Cond.push_back(MachineOperand::CreateImm(BranchCode));
482
483 for (int i=0; i<LastNumOp-1; i++) {
484 Cond.push_back(LastInst->getOperand(i));
485 }
486
487 return false;
488 }
489 }
490
491 // Get the instruction before it if it is a terminator.
492 MachineInstr *SecondLastInst = I;
493
494 // If there are three terminators, we don't know what sort of block this is.
495 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
496 return true;
497
498 // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
499 unsigned SecondLastOpc = SecondLastInst->getOpcode();
500 Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
501
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000502 if (BranchCode != Mips::COND_INVALID && LastOpc == Mips::J) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000503 int SecondNumOp = SecondLastInst->getNumOperands();
504
Chris Lattner8aa797a2007-12-30 23:10:15 +0000505 TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000506 Cond.push_back(MachineOperand::CreateImm(BranchCode));
507
508 for (int i=0; i<SecondNumOp-1; i++) {
509 Cond.push_back(SecondLastInst->getOperand(i));
510 }
511
Chris Lattner8aa797a2007-12-30 23:10:15 +0000512 FBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000513 return false;
514 }
515
516 // If the block ends with two unconditional branches, handle it. The last
517 // one is not executed, so remove it.
518 if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000519 TBB = SecondLastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000520 I = LastInst;
521 I->eraseFromParent();
522 return false;
523 }
524
525 // Otherwise, can't handle this.
526 return true;
527}
528
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000529unsigned MipsInstrInfo::
530InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
531 MachineBasicBlock *FBB, const std::vector<MachineOperand> &Cond)
532 const
533{
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000534 // Shouldn't be a fall through.
535 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
536 assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
537 "Mips branch conditions can have two|three components!");
538
539 if (FBB == 0) { // One way branch.
540 if (Cond.empty()) {
541 // Unconditional branch?
542 BuildMI(&MBB, get(Mips::J)).addMBB(TBB);
543 } else {
544 // Conditional branch.
545 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000546 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000547
Chris Lattner349c4952008-01-07 03:13:06 +0000548 if (TID.getNumOperands() == 3)
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000549 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
550 .addReg(Cond[2].getReg())
551 .addMBB(TBB);
552 else
553 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
554 .addMBB(TBB);
555
556 }
557 return 1;
558 }
559
560 // Two-way Conditional branch.
561 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000562 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000563
Chris Lattner349c4952008-01-07 03:13:06 +0000564 if (TID.getNumOperands() == 3)
Chris Lattner749c6f62008-01-07 07:27:27 +0000565 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000566 .addMBB(TBB);
567 else
Chris Lattner749c6f62008-01-07 07:27:27 +0000568 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000569
570 BuildMI(&MBB, get(Mips::J)).addMBB(FBB);
571 return 2;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000572}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000573
574unsigned MipsInstrInfo::
575RemoveBranch(MachineBasicBlock &MBB) const
576{
577 MachineBasicBlock::iterator I = MBB.end();
578 if (I == MBB.begin()) return 0;
579 --I;
580 if (I->getOpcode() != Mips::J &&
581 GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
582 return 0;
583
584 // Remove the branch.
585 I->eraseFromParent();
586
587 I = MBB.end();
588
589 if (I == MBB.begin()) return 1;
590 --I;
591 if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
592 return 1;
593
594 // Remove the branch.
595 I->eraseFromParent();
596 return 2;
597}
598
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000599/// BlockHasNoFallThrough - Analyze if MachineBasicBlock does not
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000600/// fall-through into its successor block.
601bool MipsInstrInfo::
602BlockHasNoFallThrough(MachineBasicBlock &MBB) const
603{
604 if (MBB.empty()) return false;
605
606 switch (MBB.back().getOpcode()) {
607 case Mips::RET: // Return.
608 case Mips::JR: // Indirect branch.
609 case Mips::J: // Uncond branch.
610 return true;
611 default: return false;
612 }
613}
614
615/// ReverseBranchCondition - Return the inverse opcode of the
616/// specified Branch instruction.
617bool MipsInstrInfo::
618ReverseBranchCondition(std::vector<MachineOperand> &Cond) const
619{
620 assert( (Cond.size() == 3 || Cond.size() == 2) &&
621 "Invalid Mips branch condition!");
622 Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));
623 return false;
624}