blob: 8d3c5a0055e76e54f3f335cc27fe29d044b664f9 [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) {
Dan Gohmand735b802008-10-03 15:45:36 +000027 return op.isImm() && 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) {
Dan Gohmand735b802008-10-03 15:45:36 +000063 if ((MI.getOperand(1).isReg()) && (isZeroImm(MI.getOperand(2)))) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000064 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::
Dan Gohmancbad42c2008-11-18 19:49:32 +000078isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000079{
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)) {
Dan Gohmand735b802008-10-03 15:45:36 +000082 if ((MI->getOperand(2).isFI()) && // is a stack slot
83 (MI->getOperand(1).isImm()) && // 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::
Dan Gohmancbad42c2008-11-18 19:49:32 +000099isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000100{
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)) {
Dan Gohmand735b802008-10-03 15:45:36 +0000103 if ((MI->getOperand(2).isFI()) && // is a stack slot
104 (MI->getOperand(1).isImm()) && // 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
Owen Anderson940f83e2008-08-26 18:03:31 +0000121bool MipsInstrInfo::
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000122copyRegToReg(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))
Owen Anderson940f83e2008-08-26 18:03:31 +0000144 return true; // This register is used implicitly, no copy needed.
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000145 else if ((DestRC == Mips::CCRRegisterClass) &&
146 (DestReg == Mips::FCR31))
Owen Anderson940f83e2008-08-26 18:03:31 +0000147 return true; // 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
Owen Anderson940f83e2008-08-26 18:03:31 +0000157 // DestRC != SrcRC, Can't copy this register
158 return false;
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000159
Owen Anderson940f83e2008-08-26 18:03:31 +0000160 return true;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000161 }
162
163 if (DestRC == Mips::CPURegsRegisterClass)
164 BuildMI(MBB, I, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
165 .addReg(SrcReg);
166 else if (DestRC == Mips::FGR32RegisterClass)
167 BuildMI(MBB, I, get(Mips::FMOV_SO32), DestReg).addReg(SrcReg);
168 else if (DestRC == Mips::AFGR32RegisterClass)
169 BuildMI(MBB, I, get(Mips::FMOV_AS32), DestReg).addReg(SrcReg);
170 else if (DestRC == Mips::AFGR64RegisterClass)
171 BuildMI(MBB, I, get(Mips::FMOV_D32), DestReg).addReg(SrcReg);
172 else
Owen Anderson940f83e2008-08-26 18:03:31 +0000173 // Can't copy this register
174 return false;
175
176 return true;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000177}
178
179void MipsInstrInfo::
180storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
181 unsigned SrcReg, bool isKill, int FI,
182 const TargetRegisterClass *RC) const
183{
184 unsigned Opc;
185 if (RC == Mips::CPURegsRegisterClass)
186 Opc = Mips::SW;
187 else if (RC == Mips::FGR32RegisterClass)
188 Opc = Mips::SWC1;
189 else if (RC == Mips::AFGR32RegisterClass)
190 Opc = Mips::SWC1A;
191 else if (RC == Mips::AFGR64RegisterClass)
192 Opc = Mips::SDC1;
193 else
194 assert(0 && "Can't store this register to stack slot");
195
196 BuildMI(MBB, I, get(Opc)).addReg(SrcReg, false, false, isKill)
197 .addImm(0).addFrameIndex(FI);
198}
199
200void MipsInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
201 bool isKill, SmallVectorImpl<MachineOperand> &Addr,
202 const TargetRegisterClass *RC, SmallVectorImpl<MachineInstr*> &NewMIs) const
203{
204 unsigned Opc;
205 if (RC == Mips::CPURegsRegisterClass)
206 Opc = Mips::SW;
207 else if (RC == Mips::FGR32RegisterClass)
208 Opc = Mips::SWC1;
209 else if (RC == Mips::AFGR32RegisterClass)
210 Opc = Mips::SWC1A;
211 else if (RC == Mips::AFGR64RegisterClass)
212 Opc = Mips::SDC1;
213 else
214 assert(0 && "Can't store this register");
215
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000216 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000217 .addReg(SrcReg, false, false, isKill);
218 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
219 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000220 if (MO.isReg())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000221 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000222 else if (MO.isImm())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000223 MIB.addImm(MO.getImm());
224 else
225 MIB.addFrameIndex(MO.getIndex());
226 }
227 NewMIs.push_back(MIB);
228 return;
229}
230
231void MipsInstrInfo::
232loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
233 unsigned DestReg, int FI,
234 const TargetRegisterClass *RC) const
235{
236 unsigned Opc;
237 if (RC == Mips::CPURegsRegisterClass)
238 Opc = Mips::LW;
239 else if (RC == Mips::FGR32RegisterClass)
240 Opc = Mips::LWC1;
241 else if (RC == Mips::AFGR32RegisterClass)
242 Opc = Mips::LWC1A;
243 else if (RC == Mips::AFGR64RegisterClass)
244 Opc = Mips::LDC1;
245 else
246 assert(0 && "Can't load this register from stack slot");
247
248 BuildMI(MBB, I, get(Opc), DestReg).addImm(0).addFrameIndex(FI);
249}
250
251void MipsInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
252 SmallVectorImpl<MachineOperand> &Addr,
253 const TargetRegisterClass *RC,
254 SmallVectorImpl<MachineInstr*> &NewMIs) const {
255 unsigned Opc;
256 if (RC == Mips::CPURegsRegisterClass)
257 Opc = Mips::LW;
258 else if (RC == Mips::FGR32RegisterClass)
259 Opc = Mips::LWC1;
260 else if (RC == Mips::AFGR32RegisterClass)
261 Opc = Mips::LWC1A;
262 else if (RC == Mips::AFGR64RegisterClass)
263 Opc = Mips::LDC1;
264 else
265 assert(0 && "Can't load this register");
266
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000267 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000268 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
269 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000270 if (MO.isReg())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000271 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000272 else if (MO.isImm())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000273 MIB.addImm(MO.getImm());
274 else
275 MIB.addFrameIndex(MO.getIndex());
276 }
277 NewMIs.push_back(MIB);
278 return;
279}
280
281MachineInstr *MipsInstrInfo::
282foldMemoryOperand(MachineFunction &MF,
283 MachineInstr* MI,
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000284 const SmallVectorImpl<unsigned> &Ops, int FI) const
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000285{
286 if (Ops.size() != 1) return NULL;
287
288 MachineInstr *NewMI = NULL;
289
290 switch (MI->getOpcode()) {
291 case Mips::ADDu:
Dan Gohmand735b802008-10-03 15:45:36 +0000292 if ((MI->getOperand(0).isReg()) &&
293 (MI->getOperand(1).isReg()) &&
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000294 (MI->getOperand(1).getReg() == Mips::ZERO) &&
Dan Gohmand735b802008-10-03 15:45:36 +0000295 (MI->getOperand(2).isReg())) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000296 if (Ops[0] == 0) { // COPY -> STORE
297 unsigned SrcReg = MI->getOperand(2).getReg();
298 bool isKill = MI->getOperand(2).isKill();
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000299 NewMI = BuildMI(MF, get(Mips::SW)).addReg(SrcReg, false, false, isKill)
300 .addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000301 } else { // COPY -> LOAD
302 unsigned DstReg = MI->getOperand(0).getReg();
303 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000304 NewMI = BuildMI(MF, get(Mips::LW))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000305 .addReg(DstReg, true, false, false, isDead)
306 .addImm(0).addFrameIndex(FI);
307 }
308 }
309 break;
310 case Mips::FMOV_SO32:
311 case Mips::FMOV_AS32:
312 case Mips::FMOV_D32:
Dan Gohmand735b802008-10-03 15:45:36 +0000313 if ((MI->getOperand(0).isReg()) &&
314 (MI->getOperand(1).isReg())) {
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +0000315 const TargetRegisterClass
316 *RC = RI.getRegClass(MI->getOperand(0).getReg());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000317 unsigned StoreOpc, LoadOpc;
318
319 if (RC == Mips::FGR32RegisterClass) {
320 LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
321 } else if (RC == Mips::AFGR32RegisterClass) {
322 LoadOpc = Mips::LWC1A; StoreOpc = Mips::SWC1A;
323 } else if (RC == Mips::AFGR64RegisterClass) {
324 LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
325 } else
326 assert(0 && "foldMemoryOperand register unknown");
327
328 if (Ops[0] == 0) { // COPY -> STORE
329 unsigned SrcReg = MI->getOperand(1).getReg();
330 bool isKill = MI->getOperand(1).isKill();
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000331 NewMI = BuildMI(MF, get(StoreOpc)).addReg(SrcReg, false, false, isKill)
332 .addImm(0).addFrameIndex(FI) ;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000333 } else { // COPY -> LOAD
334 unsigned DstReg = MI->getOperand(0).getReg();
335 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000336 NewMI = BuildMI(MF, get(LoadOpc))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000337 .addReg(DstReg, true, false, false, isDead)
338 .addImm(0).addFrameIndex(FI);
339 }
340 }
341 break;
342 }
343
344 return NewMI;
345}
346
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000347//===----------------------------------------------------------------------===//
348// Branch Analysis
349//===----------------------------------------------------------------------===//
350
351/// GetCondFromBranchOpc - Return the Mips CC that matches
352/// the correspondent Branch instruction opcode.
353static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc)
354{
355 switch (BrOpc) {
356 default: return Mips::COND_INVALID;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000357 case Mips::BEQ : return Mips::COND_E;
358 case Mips::BNE : return Mips::COND_NE;
359 case Mips::BGTZ : return Mips::COND_GZ;
360 case Mips::BGEZ : return Mips::COND_GEZ;
361 case Mips::BLTZ : return Mips::COND_LZ;
362 case Mips::BLEZ : return Mips::COND_LEZ;
363
364 // We dont do fp branch analysis yet!
365 case Mips::BC1T :
366 case Mips::BC1F : return Mips::COND_INVALID;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000367 }
368}
369
370/// GetCondBranchFromCond - Return the Branch instruction
371/// opcode that matches the cc.
372unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC)
373{
374 switch (CC) {
375 default: assert(0 && "Illegal condition code!");
376 case Mips::COND_E : return Mips::BEQ;
377 case Mips::COND_NE : return Mips::BNE;
378 case Mips::COND_GZ : return Mips::BGTZ;
379 case Mips::COND_GEZ : return Mips::BGEZ;
380 case Mips::COND_LZ : return Mips::BLTZ;
381 case Mips::COND_LEZ : return Mips::BLEZ;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000382
383 case Mips::FCOND_F:
384 case Mips::FCOND_UN:
385 case Mips::FCOND_EQ:
386 case Mips::FCOND_UEQ:
387 case Mips::FCOND_OLT:
388 case Mips::FCOND_ULT:
389 case Mips::FCOND_OLE:
390 case Mips::FCOND_ULE:
391 case Mips::FCOND_SF:
392 case Mips::FCOND_NGLE:
393 case Mips::FCOND_SEQ:
394 case Mips::FCOND_NGL:
395 case Mips::FCOND_LT:
396 case Mips::FCOND_NGE:
397 case Mips::FCOND_LE:
398 case Mips::FCOND_NGT: return Mips::BC1T;
399
400 case Mips::FCOND_T:
401 case Mips::FCOND_OR:
402 case Mips::FCOND_NEQ:
403 case Mips::FCOND_OGL:
404 case Mips::FCOND_UGE:
405 case Mips::FCOND_OGE:
406 case Mips::FCOND_UGT:
407 case Mips::FCOND_OGT:
408 case Mips::FCOND_ST:
409 case Mips::FCOND_GLE:
410 case Mips::FCOND_SNE:
411 case Mips::FCOND_GL:
412 case Mips::FCOND_NLT:
413 case Mips::FCOND_GE:
414 case Mips::FCOND_NLE:
415 case Mips::FCOND_GT: return Mips::BC1F;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000416 }
417}
418
419/// GetOppositeBranchCondition - Return the inverse of the specified
420/// condition, e.g. turning COND_E to COND_NE.
421Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
422{
423 switch (CC) {
424 default: assert(0 && "Illegal condition code!");
425 case Mips::COND_E : return Mips::COND_NE;
426 case Mips::COND_NE : return Mips::COND_E;
427 case Mips::COND_GZ : return Mips::COND_LEZ;
428 case Mips::COND_GEZ : return Mips::COND_LZ;
429 case Mips::COND_LZ : return Mips::COND_GEZ;
430 case Mips::COND_LEZ : return Mips::COND_GZ;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000431 case Mips::FCOND_F : return Mips::FCOND_T;
432 case Mips::FCOND_UN : return Mips::FCOND_OR;
433 case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
434 case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
435 case Mips::FCOND_OLT: return Mips::FCOND_UGE;
436 case Mips::FCOND_ULT: return Mips::FCOND_OGE;
437 case Mips::FCOND_OLE: return Mips::FCOND_UGT;
438 case Mips::FCOND_ULE: return Mips::FCOND_OGT;
439 case Mips::FCOND_SF: return Mips::FCOND_ST;
440 case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
441 case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
442 case Mips::FCOND_NGL: return Mips::FCOND_GL;
443 case Mips::FCOND_LT: return Mips::FCOND_NLT;
444 case Mips::FCOND_NGE: return Mips::FCOND_GE;
445 case Mips::FCOND_LE: return Mips::FCOND_NLE;
446 case Mips::FCOND_NGT: return Mips::FCOND_GT;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000447 }
448}
449
450bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
451 MachineBasicBlock *&TBB,
452 MachineBasicBlock *&FBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000453 SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000454{
455 // If the block has no terminators, it just falls into the block after it.
456 MachineBasicBlock::iterator I = MBB.end();
457 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
458 return false;
459
460 // Get the last instruction in the block.
461 MachineInstr *LastInst = I;
462
463 // If there is only one terminator instruction, process it.
464 unsigned LastOpc = LastInst->getOpcode();
465 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000466 if (!LastInst->getDesc().isBranch())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000467 return true;
468
469 // Unconditional branch
470 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000471 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000472 return false;
473 }
474
475 Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
476 if (BranchCode == Mips::COND_INVALID)
477 return true; // Can't handle indirect branch.
478
479 // Conditional branch
480 // Block ends with fall-through condbranch.
481 if (LastOpc != Mips::COND_INVALID) {
482 int LastNumOp = LastInst->getNumOperands();
483
Chris Lattner8aa797a2007-12-30 23:10:15 +0000484 TBB = LastInst->getOperand(LastNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000485 Cond.push_back(MachineOperand::CreateImm(BranchCode));
486
487 for (int i=0; i<LastNumOp-1; i++) {
488 Cond.push_back(LastInst->getOperand(i));
489 }
490
491 return false;
492 }
493 }
494
495 // Get the instruction before it if it is a terminator.
496 MachineInstr *SecondLastInst = I;
497
498 // If there are three terminators, we don't know what sort of block this is.
499 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
500 return true;
501
502 // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
503 unsigned SecondLastOpc = SecondLastInst->getOpcode();
504 Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
505
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000506 if (BranchCode != Mips::COND_INVALID && LastOpc == Mips::J) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000507 int SecondNumOp = SecondLastInst->getNumOperands();
508
Chris Lattner8aa797a2007-12-30 23:10:15 +0000509 TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000510 Cond.push_back(MachineOperand::CreateImm(BranchCode));
511
512 for (int i=0; i<SecondNumOp-1; i++) {
513 Cond.push_back(SecondLastInst->getOperand(i));
514 }
515
Chris Lattner8aa797a2007-12-30 23:10:15 +0000516 FBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000517 return false;
518 }
519
520 // If the block ends with two unconditional branches, handle it. The last
521 // one is not executed, so remove it.
522 if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000523 TBB = SecondLastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000524 I = LastInst;
525 I->eraseFromParent();
526 return false;
527 }
528
529 // Otherwise, can't handle this.
530 return true;
531}
532
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000533unsigned MipsInstrInfo::
534InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000535 MachineBasicBlock *FBB,
536 const SmallVectorImpl<MachineOperand> &Cond) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000537 // Shouldn't be a fall through.
538 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
539 assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
540 "Mips branch conditions can have two|three components!");
541
542 if (FBB == 0) { // One way branch.
543 if (Cond.empty()) {
544 // Unconditional branch?
545 BuildMI(&MBB, get(Mips::J)).addMBB(TBB);
546 } else {
547 // Conditional branch.
548 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000549 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000550
Chris Lattner349c4952008-01-07 03:13:06 +0000551 if (TID.getNumOperands() == 3)
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000552 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
553 .addReg(Cond[2].getReg())
554 .addMBB(TBB);
555 else
556 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
557 .addMBB(TBB);
558
559 }
560 return 1;
561 }
562
563 // Two-way Conditional branch.
564 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000565 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000566
Chris Lattner349c4952008-01-07 03:13:06 +0000567 if (TID.getNumOperands() == 3)
Chris Lattner749c6f62008-01-07 07:27:27 +0000568 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000569 .addMBB(TBB);
570 else
Chris Lattner749c6f62008-01-07 07:27:27 +0000571 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000572
573 BuildMI(&MBB, get(Mips::J)).addMBB(FBB);
574 return 2;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000575}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000576
577unsigned MipsInstrInfo::
578RemoveBranch(MachineBasicBlock &MBB) const
579{
580 MachineBasicBlock::iterator I = MBB.end();
581 if (I == MBB.begin()) return 0;
582 --I;
583 if (I->getOpcode() != Mips::J &&
584 GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
585 return 0;
586
587 // Remove the branch.
588 I->eraseFromParent();
589
590 I = MBB.end();
591
592 if (I == MBB.begin()) return 1;
593 --I;
594 if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
595 return 1;
596
597 // Remove the branch.
598 I->eraseFromParent();
599 return 2;
600}
601
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000602/// BlockHasNoFallThrough - Analyze if MachineBasicBlock does not
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000603/// fall-through into its successor block.
604bool MipsInstrInfo::
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000605BlockHasNoFallThrough(const MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000606{
607 if (MBB.empty()) return false;
608
609 switch (MBB.back().getOpcode()) {
610 case Mips::RET: // Return.
611 case Mips::JR: // Indirect branch.
612 case Mips::J: // Uncond branch.
613 return true;
614 default: return false;
615 }
616}
617
618/// ReverseBranchCondition - Return the inverse opcode of the
619/// specified Branch instruction.
620bool MipsInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000621ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000622{
623 assert( (Cond.size() == 3 || Cond.size() == 2) &&
624 "Invalid Mips branch condition!");
625 Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));
626 return false;
627}