blob: a672d3ef7e154f6aa09a5edcc850354171d41993 [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::
Evan Cheng04ee5a12009-01-20 19:12:24 +000033isMoveInstr(const MachineInstr &MI, unsigned &SrcReg, unsigned &DstReg,
34 unsigned &SrcSubIdx, unsigned &DstSubIdx) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000035{
Evan Cheng04ee5a12009-01-20 19:12:24 +000036 SrcSubIdx = DstSubIdx = 0; // No sub-registers.
37
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000038 // addu $dst, $src, $zero || addu $dst, $zero, $src
39 // or $dst, $src, $zero || or $dst, $zero, $src
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000040 if ((MI.getOpcode() == Mips::ADDu) || (MI.getOpcode() == Mips::OR)) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000041 if (MI.getOperand(1).getReg() == Mips::ZERO) {
42 DstReg = MI.getOperand(0).getReg();
43 SrcReg = MI.getOperand(2).getReg();
44 return true;
45 } else if (MI.getOperand(2).getReg() == Mips::ZERO) {
46 DstReg = MI.getOperand(0).getReg();
47 SrcReg = MI.getOperand(1).getReg();
48 return true;
49 }
50 }
51
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000052 // mov $fpDst, $fpSrc
53 // mfc $gpDst, $fpSrc
54 // mtc $fpDst, $gpSrc
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000055 if (MI.getOpcode() == Mips::FMOV_S32 ||
56 MI.getOpcode() == Mips::FMOV_D32 ||
57 MI.getOpcode() == Mips::MFC1 ||
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000058 MI.getOpcode() == Mips::MTC1 ) {
59 DstReg = MI.getOperand(0).getReg();
60 SrcReg = MI.getOperand(1).getReg();
61 return true;
62 }
63
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000064 // addiu $dst, $src, 0
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000065 if (MI.getOpcode() == Mips::ADDiu) {
Dan Gohmand735b802008-10-03 15:45:36 +000066 if ((MI.getOperand(1).isReg()) && (isZeroImm(MI.getOperand(2)))) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000067 DstReg = MI.getOperand(0).getReg();
68 SrcReg = MI.getOperand(1).getReg();
69 return true;
70 }
71 }
72 return false;
73}
74
75/// isLoadFromStackSlot - If the specified machine instruction is a direct
76/// load from a stack slot, return the virtual or physical register number of
77/// the destination along with the FrameIndex of the loaded stack slot. If
78/// not, return 0. This predicate must return 0 if the instruction has
79/// any side effects other than loading from the stack slot.
80unsigned MipsInstrInfo::
Dan Gohmancbad42c2008-11-18 19:49:32 +000081isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000082{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000083 if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000084 (MI->getOpcode() == Mips::LDC1)) {
Dan Gohmand735b802008-10-03 15:45:36 +000085 if ((MI->getOperand(2).isFI()) && // is a stack slot
86 (MI->getOperand(1).isImm()) && // the imm is zero
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000087 (isZeroImm(MI->getOperand(1)))) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000088 FrameIndex = MI->getOperand(2).getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000089 return MI->getOperand(0).getReg();
90 }
91 }
92
93 return 0;
94}
95
96/// isStoreToStackSlot - If the specified machine instruction is a direct
97/// store to a stack slot, return the virtual or physical register number of
98/// the source reg along with the FrameIndex of the loaded stack slot. If
99/// not, return 0. This predicate must return 0 if the instruction has
100/// any side effects other than storing to the stack slot.
101unsigned MipsInstrInfo::
Dan Gohmancbad42c2008-11-18 19:49:32 +0000102isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000103{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000104 if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000105 (MI->getOpcode() == Mips::SDC1)) {
Dan Gohmand735b802008-10-03 15:45:36 +0000106 if ((MI->getOperand(2).isFI()) && // is a stack slot
107 (MI->getOperand(1).isImm()) && // the imm is zero
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000108 (isZeroImm(MI->getOperand(1)))) {
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000109 FrameIndex = MI->getOperand(2).getIndex();
110 return MI->getOperand(0).getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000111 }
112 }
113 return 0;
114}
115
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000116/// insertNoop - If data hazard condition is found insert the target nop
117/// instruction.
118void MipsInstrInfo::
119insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
120{
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000121 DebugLoc DL = DebugLoc::getUnknownLoc();
122 if (MI != MBB.end()) DL = MI->getDebugLoc();
123 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000124}
125
Owen Anderson940f83e2008-08-26 18:03:31 +0000126bool MipsInstrInfo::
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000127copyRegToReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
128 unsigned DestReg, unsigned SrcReg,
129 const TargetRegisterClass *DestRC,
130 const TargetRegisterClass *SrcRC) const {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000131 DebugLoc DL = DebugLoc::getUnknownLoc();
132 if (I != MBB.end()) DL = I->getDebugLoc();
133
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000134 if (DestRC != SrcRC) {
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000135 // Moves between coprocessors and cpu
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000136 if ((DestRC == Mips::CPURegsRegisterClass) &&
137 (SrcRC == Mips::FGR32RegisterClass))
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000138 BuildMI(MBB, I, DL, get(Mips::MFC1), DestReg).addReg(SrcReg);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000139 else if ((DestRC == Mips::FGR32RegisterClass) &&
140 (SrcRC == Mips::CPURegsRegisterClass))
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000141 BuildMI(MBB, I, DL, get(Mips::MTC1), DestReg).addReg(SrcReg);
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000142
143 // Condition registers
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000144 else if ((SrcRC == Mips::CCRRegisterClass) &&
145 (SrcReg == Mips::FCR31))
Owen Anderson940f83e2008-08-26 18:03:31 +0000146 return true; // This register is used implicitly, no copy needed.
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000147 else if ((DestRC == Mips::CCRRegisterClass) &&
148 (DestReg == Mips::FCR31))
Owen Anderson940f83e2008-08-26 18:03:31 +0000149 return true; // This register is used implicitly, no copy needed.
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000150
151 // Move from/to Hi/Lo registers
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000152 else if ((DestRC == Mips::HILORegisterClass) &&
153 (SrcRC == Mips::CPURegsRegisterClass)) {
154 unsigned Opc = (DestReg == Mips::HI) ? Mips::MTHI : Mips::MTLO;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000155 BuildMI(MBB, I, DL, get(Opc), DestReg);
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000156 } else if ((SrcRC == Mips::HILORegisterClass) &&
157 (DestRC == Mips::CPURegsRegisterClass)) {
158 unsigned Opc = (SrcReg == Mips::HI) ? Mips::MFHI : Mips::MFLO;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000159 BuildMI(MBB, I, DL, get(Opc), DestReg);
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000160
161 // Can't copy this register
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000162 } else
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000163 return false;
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000164
Owen Anderson940f83e2008-08-26 18:03:31 +0000165 return true;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000166 }
167
168 if (DestRC == Mips::CPURegsRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000169 BuildMI(MBB, I, DL, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000170 .addReg(SrcReg);
171 else if (DestRC == Mips::FGR32RegisterClass)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000172 BuildMI(MBB, I, DL, get(Mips::FMOV_S32), DestReg).addReg(SrcReg);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000173 else if (DestRC == Mips::AFGR64RegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000174 BuildMI(MBB, I, DL, get(Mips::FMOV_D32), DestReg).addReg(SrcReg);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000175 else
Owen Anderson940f83e2008-08-26 18:03:31 +0000176 // Can't copy this register
177 return false;
178
179 return true;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000180}
181
182void MipsInstrInfo::
183storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000184 unsigned SrcReg, bool isKill, int FI,
185 const TargetRegisterClass *RC) const
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000186{
187 unsigned Opc;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000188
189 DebugLoc DL = DebugLoc::getUnknownLoc();
190 if (I != MBB.end()) DL = I->getDebugLoc();
191
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000192 if (RC == Mips::CPURegsRegisterClass)
193 Opc = Mips::SW;
194 else if (RC == Mips::FGR32RegisterClass)
195 Opc = Mips::SWC1;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000196 else if (RC == Mips::AFGR64RegisterClass)
197 Opc = Mips::SDC1;
198 else
199 assert(0 && "Can't store this register to stack slot");
200
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000201 BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, false, false, isKill)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000202 .addImm(0).addFrameIndex(FI);
203}
204
205void MipsInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
206 bool isKill, SmallVectorImpl<MachineOperand> &Addr,
207 const TargetRegisterClass *RC, SmallVectorImpl<MachineInstr*> &NewMIs) const
208{
209 unsigned Opc;
210 if (RC == Mips::CPURegsRegisterClass)
211 Opc = Mips::SW;
212 else if (RC == Mips::FGR32RegisterClass)
213 Opc = Mips::SWC1;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000214 else if (RC == Mips::AFGR64RegisterClass)
215 Opc = Mips::SDC1;
216 else
217 assert(0 && "Can't store this register");
218
Dale Johannesen21b55412009-02-12 23:08:38 +0000219 DebugLoc DL = DebugLoc::getUnknownLoc();
220 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000221 .addReg(SrcReg, false, false, isKill);
Dan Gohman97357612009-02-18 05:45:50 +0000222 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
223 MIB.addOperand(Addr[i]);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000224 NewMIs.push_back(MIB);
225 return;
226}
227
228void MipsInstrInfo::
229loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
230 unsigned DestReg, int FI,
231 const TargetRegisterClass *RC) const
232{
233 unsigned Opc;
234 if (RC == Mips::CPURegsRegisterClass)
235 Opc = Mips::LW;
236 else if (RC == Mips::FGR32RegisterClass)
237 Opc = Mips::LWC1;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000238 else if (RC == Mips::AFGR64RegisterClass)
239 Opc = Mips::LDC1;
240 else
241 assert(0 && "Can't load this register from stack slot");
242
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000243 DebugLoc DL = DebugLoc::getUnknownLoc();
244 if (I != MBB.end()) DL = I->getDebugLoc();
245 BuildMI(MBB, I, DL, get(Opc), DestReg).addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000246}
247
248void MipsInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000249 SmallVectorImpl<MachineOperand> &Addr,
250 const TargetRegisterClass *RC,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000251 SmallVectorImpl<MachineInstr*> &NewMIs) const {
252 unsigned Opc;
253 if (RC == Mips::CPURegsRegisterClass)
254 Opc = Mips::LW;
255 else if (RC == Mips::FGR32RegisterClass)
256 Opc = Mips::LWC1;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000257 else if (RC == Mips::AFGR64RegisterClass)
258 Opc = Mips::LDC1;
259 else
260 assert(0 && "Can't load this register");
261
Dale Johannesen21b55412009-02-12 23:08:38 +0000262 DebugLoc DL = DebugLoc::getUnknownLoc();
263 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
Dan Gohman97357612009-02-18 05:45:50 +0000264 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
265 MIB.addOperand(Addr[i]);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000266 NewMIs.push_back(MIB);
267 return;
268}
269
270MachineInstr *MipsInstrInfo::
Dan Gohmanc54baa22008-12-03 18:43:12 +0000271foldMemoryOperandImpl(MachineFunction &MF,
272 MachineInstr* MI,
273 const SmallVectorImpl<unsigned> &Ops, int FI) const
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000274{
275 if (Ops.size() != 1) return NULL;
276
277 MachineInstr *NewMI = NULL;
278
279 switch (MI->getOpcode()) {
280 case Mips::ADDu:
Dan Gohmand735b802008-10-03 15:45:36 +0000281 if ((MI->getOperand(0).isReg()) &&
282 (MI->getOperand(1).isReg()) &&
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000283 (MI->getOperand(1).getReg() == Mips::ZERO) &&
Dan Gohmand735b802008-10-03 15:45:36 +0000284 (MI->getOperand(2).isReg())) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000285 if (Ops[0] == 0) { // COPY -> STORE
286 unsigned SrcReg = MI->getOperand(2).getReg();
287 bool isKill = MI->getOperand(2).isKill();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000288 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::SW))
289 .addReg(SrcReg, false, false, isKill)
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000290 .addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000291 } else { // COPY -> LOAD
292 unsigned DstReg = MI->getOperand(0).getReg();
293 bool isDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000294 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::LW))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000295 .addReg(DstReg, true, false, false, isDead)
296 .addImm(0).addFrameIndex(FI);
297 }
298 }
299 break;
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000300 case Mips::FMOV_S32:
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000301 case Mips::FMOV_D32:
Dan Gohmand735b802008-10-03 15:45:36 +0000302 if ((MI->getOperand(0).isReg()) &&
303 (MI->getOperand(1).isReg())) {
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +0000304 const TargetRegisterClass
305 *RC = RI.getRegClass(MI->getOperand(0).getReg());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000306 unsigned StoreOpc, LoadOpc;
307
308 if (RC == Mips::FGR32RegisterClass) {
309 LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000310 } else if (RC == Mips::AFGR64RegisterClass) {
311 LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
312 } else
Dan Gohmanc54baa22008-12-03 18:43:12 +0000313 assert(0 && "foldMemoryOperandImpl register unknown");
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000314
315 if (Ops[0] == 0) { // COPY -> STORE
316 unsigned SrcReg = MI->getOperand(1).getReg();
317 bool isKill = MI->getOperand(1).isKill();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000318 NewMI = BuildMI(MF, MI->getDebugLoc(), get(StoreOpc))
319 .addReg(SrcReg, false, false, isKill)
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000320 .addImm(0).addFrameIndex(FI) ;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000321 } else { // COPY -> LOAD
322 unsigned DstReg = MI->getOperand(0).getReg();
323 bool isDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000324 NewMI = BuildMI(MF, MI->getDebugLoc(), get(LoadOpc))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000325 .addReg(DstReg, true, false, false, isDead)
326 .addImm(0).addFrameIndex(FI);
327 }
328 }
329 break;
330 }
331
332 return NewMI;
333}
334
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000335//===----------------------------------------------------------------------===//
336// Branch Analysis
337//===----------------------------------------------------------------------===//
338
339/// GetCondFromBranchOpc - Return the Mips CC that matches
340/// the correspondent Branch instruction opcode.
341static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc)
342{
343 switch (BrOpc) {
344 default: return Mips::COND_INVALID;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000345 case Mips::BEQ : return Mips::COND_E;
346 case Mips::BNE : return Mips::COND_NE;
347 case Mips::BGTZ : return Mips::COND_GZ;
348 case Mips::BGEZ : return Mips::COND_GEZ;
349 case Mips::BLTZ : return Mips::COND_LZ;
350 case Mips::BLEZ : return Mips::COND_LEZ;
351
352 // We dont do fp branch analysis yet!
353 case Mips::BC1T :
354 case Mips::BC1F : return Mips::COND_INVALID;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000355 }
356}
357
358/// GetCondBranchFromCond - Return the Branch instruction
359/// opcode that matches the cc.
360unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC)
361{
362 switch (CC) {
363 default: assert(0 && "Illegal condition code!");
364 case Mips::COND_E : return Mips::BEQ;
365 case Mips::COND_NE : return Mips::BNE;
366 case Mips::COND_GZ : return Mips::BGTZ;
367 case Mips::COND_GEZ : return Mips::BGEZ;
368 case Mips::COND_LZ : return Mips::BLTZ;
369 case Mips::COND_LEZ : return Mips::BLEZ;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000370
371 case Mips::FCOND_F:
372 case Mips::FCOND_UN:
373 case Mips::FCOND_EQ:
374 case Mips::FCOND_UEQ:
375 case Mips::FCOND_OLT:
376 case Mips::FCOND_ULT:
377 case Mips::FCOND_OLE:
378 case Mips::FCOND_ULE:
379 case Mips::FCOND_SF:
380 case Mips::FCOND_NGLE:
381 case Mips::FCOND_SEQ:
382 case Mips::FCOND_NGL:
383 case Mips::FCOND_LT:
384 case Mips::FCOND_NGE:
385 case Mips::FCOND_LE:
386 case Mips::FCOND_NGT: return Mips::BC1T;
387
388 case Mips::FCOND_T:
389 case Mips::FCOND_OR:
390 case Mips::FCOND_NEQ:
391 case Mips::FCOND_OGL:
392 case Mips::FCOND_UGE:
393 case Mips::FCOND_OGE:
394 case Mips::FCOND_UGT:
395 case Mips::FCOND_OGT:
396 case Mips::FCOND_ST:
397 case Mips::FCOND_GLE:
398 case Mips::FCOND_SNE:
399 case Mips::FCOND_GL:
400 case Mips::FCOND_NLT:
401 case Mips::FCOND_GE:
402 case Mips::FCOND_NLE:
403 case Mips::FCOND_GT: return Mips::BC1F;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000404 }
405}
406
407/// GetOppositeBranchCondition - Return the inverse of the specified
408/// condition, e.g. turning COND_E to COND_NE.
409Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
410{
411 switch (CC) {
412 default: assert(0 && "Illegal condition code!");
413 case Mips::COND_E : return Mips::COND_NE;
414 case Mips::COND_NE : return Mips::COND_E;
415 case Mips::COND_GZ : return Mips::COND_LEZ;
416 case Mips::COND_GEZ : return Mips::COND_LZ;
417 case Mips::COND_LZ : return Mips::COND_GEZ;
418 case Mips::COND_LEZ : return Mips::COND_GZ;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000419 case Mips::FCOND_F : return Mips::FCOND_T;
420 case Mips::FCOND_UN : return Mips::FCOND_OR;
421 case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
422 case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
423 case Mips::FCOND_OLT: return Mips::FCOND_UGE;
424 case Mips::FCOND_ULT: return Mips::FCOND_OGE;
425 case Mips::FCOND_OLE: return Mips::FCOND_UGT;
426 case Mips::FCOND_ULE: return Mips::FCOND_OGT;
427 case Mips::FCOND_SF: return Mips::FCOND_ST;
428 case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
429 case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
430 case Mips::FCOND_NGL: return Mips::FCOND_GL;
431 case Mips::FCOND_LT: return Mips::FCOND_NLT;
432 case Mips::FCOND_NGE: return Mips::FCOND_GE;
433 case Mips::FCOND_LE: return Mips::FCOND_NLE;
434 case Mips::FCOND_NGT: return Mips::FCOND_GT;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000435 }
436}
437
438bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
439 MachineBasicBlock *&TBB,
440 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000441 SmallVectorImpl<MachineOperand> &Cond,
442 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000443{
444 // If the block has no terminators, it just falls into the block after it.
445 MachineBasicBlock::iterator I = MBB.end();
446 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
447 return false;
448
449 // Get the last instruction in the block.
450 MachineInstr *LastInst = I;
451
452 // If there is only one terminator instruction, process it.
453 unsigned LastOpc = LastInst->getOpcode();
454 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000455 if (!LastInst->getDesc().isBranch())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000456 return true;
457
458 // Unconditional branch
459 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000460 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000461 return false;
462 }
463
464 Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
465 if (BranchCode == Mips::COND_INVALID)
466 return true; // Can't handle indirect branch.
467
468 // Conditional branch
469 // Block ends with fall-through condbranch.
470 if (LastOpc != Mips::COND_INVALID) {
471 int LastNumOp = LastInst->getNumOperands();
472
Chris Lattner8aa797a2007-12-30 23:10:15 +0000473 TBB = LastInst->getOperand(LastNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000474 Cond.push_back(MachineOperand::CreateImm(BranchCode));
475
476 for (int i=0; i<LastNumOp-1; i++) {
477 Cond.push_back(LastInst->getOperand(i));
478 }
479
480 return false;
481 }
482 }
483
484 // Get the instruction before it if it is a terminator.
485 MachineInstr *SecondLastInst = I;
486
487 // If there are three terminators, we don't know what sort of block this is.
488 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
489 return true;
490
491 // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
492 unsigned SecondLastOpc = SecondLastInst->getOpcode();
493 Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
494
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000495 if (BranchCode != Mips::COND_INVALID && LastOpc == Mips::J) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000496 int SecondNumOp = SecondLastInst->getNumOperands();
497
Chris Lattner8aa797a2007-12-30 23:10:15 +0000498 TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000499 Cond.push_back(MachineOperand::CreateImm(BranchCode));
500
501 for (int i=0; i<SecondNumOp-1; i++) {
502 Cond.push_back(SecondLastInst->getOperand(i));
503 }
504
Chris Lattner8aa797a2007-12-30 23:10:15 +0000505 FBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000506 return false;
507 }
508
509 // If the block ends with two unconditional branches, handle it. The last
510 // one is not executed, so remove it.
511 if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000512 TBB = SecondLastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000513 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000514 if (AllowModify)
515 I->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000516 return false;
517 }
518
519 // Otherwise, can't handle this.
520 return true;
521}
522
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000523unsigned MipsInstrInfo::
524InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000525 MachineBasicBlock *FBB,
526 const SmallVectorImpl<MachineOperand> &Cond) const {
Dale Johannesen94817572009-02-13 02:34:39 +0000527 // FIXME this should probably have a DebugLoc argument
528 DebugLoc dl = DebugLoc::getUnknownLoc();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000529 // Shouldn't be a fall through.
530 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
531 assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
532 "Mips branch conditions can have two|three components!");
533
534 if (FBB == 0) { // One way branch.
535 if (Cond.empty()) {
536 // Unconditional branch?
Dale Johannesen94817572009-02-13 02:34:39 +0000537 BuildMI(&MBB, dl, get(Mips::J)).addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000538 } else {
539 // Conditional branch.
540 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000541 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000542
Chris Lattner349c4952008-01-07 03:13:06 +0000543 if (TID.getNumOperands() == 3)
Dale Johannesen94817572009-02-13 02:34:39 +0000544 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000545 .addReg(Cond[2].getReg())
546 .addMBB(TBB);
547 else
Dale Johannesen94817572009-02-13 02:34:39 +0000548 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000549 .addMBB(TBB);
550
551 }
552 return 1;
553 }
554
555 // Two-way Conditional branch.
556 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000557 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000558
Chris Lattner349c4952008-01-07 03:13:06 +0000559 if (TID.getNumOperands() == 3)
Dale Johannesen94817572009-02-13 02:34:39 +0000560 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000561 .addMBB(TBB);
562 else
Dale Johannesen94817572009-02-13 02:34:39 +0000563 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()).addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000564
Dale Johannesen94817572009-02-13 02:34:39 +0000565 BuildMI(&MBB, dl, get(Mips::J)).addMBB(FBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000566 return 2;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000567}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000568
569unsigned MipsInstrInfo::
570RemoveBranch(MachineBasicBlock &MBB) const
571{
572 MachineBasicBlock::iterator I = MBB.end();
573 if (I == MBB.begin()) return 0;
574 --I;
575 if (I->getOpcode() != Mips::J &&
576 GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
577 return 0;
578
579 // Remove the branch.
580 I->eraseFromParent();
581
582 I = MBB.end();
583
584 if (I == MBB.begin()) return 1;
585 --I;
586 if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
587 return 1;
588
589 // Remove the branch.
590 I->eraseFromParent();
591 return 2;
592}
593
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000594/// BlockHasNoFallThrough - Analyze if MachineBasicBlock does not
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000595/// fall-through into its successor block.
596bool MipsInstrInfo::
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000597BlockHasNoFallThrough(const MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000598{
599 if (MBB.empty()) return false;
600
601 switch (MBB.back().getOpcode()) {
602 case Mips::RET: // Return.
603 case Mips::JR: // Indirect branch.
604 case Mips::J: // Uncond branch.
605 return true;
606 default: return false;
607 }
608}
609
610/// ReverseBranchCondition - Return the inverse opcode of the
611/// specified Branch instruction.
612bool MipsInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000613ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000614{
615 assert( (Cond.size() == 3 || Cond.size() == 2) &&
616 "Invalid Mips branch condition!");
617 Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));
618 return false;
619}