blob: 2ccbe898fa30488f4f2ec55f569f3927ff85324b [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,
Chris Lattnere3a85832009-03-26 05:28:26 +0000185 const TargetRegisterClass *RC) const {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000186 unsigned Opc;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000187
188 DebugLoc DL = DebugLoc::getUnknownLoc();
189 if (I != MBB.end()) DL = I->getDebugLoc();
190
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000191 if (RC == Mips::CPURegsRegisterClass)
192 Opc = Mips::SW;
193 else if (RC == Mips::FGR32RegisterClass)
194 Opc = Mips::SWC1;
Chris Lattnere3a85832009-03-26 05:28:26 +0000195 else {
196 assert(RC == Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000197 Opc = Mips::SDC1;
Chris Lattnere3a85832009-03-26 05:28:26 +0000198 }
199
Bill Wendling587daed2009-05-13 21:33:08 +0000200 BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000201 .addImm(0).addFrameIndex(FI);
202}
203
204void MipsInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
205 bool isKill, SmallVectorImpl<MachineOperand> &Addr,
206 const TargetRegisterClass *RC, SmallVectorImpl<MachineInstr*> &NewMIs) const
207{
208 unsigned Opc;
209 if (RC == Mips::CPURegsRegisterClass)
210 Opc = Mips::SW;
211 else if (RC == Mips::FGR32RegisterClass)
212 Opc = Mips::SWC1;
Chris Lattnere3a85832009-03-26 05:28:26 +0000213 else {
214 assert(RC == Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000215 Opc = Mips::SDC1;
Chris Lattnere3a85832009-03-26 05:28:26 +0000216 }
217
Dale Johannesen21b55412009-02-12 23:08:38 +0000218 DebugLoc DL = DebugLoc::getUnknownLoc();
219 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc))
Bill Wendling587daed2009-05-13 21:33:08 +0000220 .addReg(SrcReg, getKillRegState(isKill));
Dan Gohman97357612009-02-18 05:45:50 +0000221 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
222 MIB.addOperand(Addr[i]);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000223 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;
Chris Lattnere3a85832009-03-26 05:28:26 +0000237 else {
238 assert(RC == Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000239 Opc = Mips::LDC1;
Chris Lattnere3a85832009-03-26 05:28:26 +0000240 }
241
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000242 DebugLoc DL = DebugLoc::getUnknownLoc();
243 if (I != MBB.end()) DL = I->getDebugLoc();
244 BuildMI(MBB, I, DL, get(Opc), DestReg).addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000245}
246
247void MipsInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000248 SmallVectorImpl<MachineOperand> &Addr,
249 const TargetRegisterClass *RC,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000250 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;
Chris Lattnere3a85832009-03-26 05:28:26 +0000256 else {
257 assert(RC == Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000258 Opc = Mips::LDC1;
Chris Lattnere3a85832009-03-26 05:28:26 +0000259 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000260
Dale Johannesen21b55412009-02-12 23:08:38 +0000261 DebugLoc DL = DebugLoc::getUnknownLoc();
262 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
Dan Gohman97357612009-02-18 05:45:50 +0000263 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
264 MIB.addOperand(Addr[i]);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000265 NewMIs.push_back(MIB);
266 return;
267}
268
269MachineInstr *MipsInstrInfo::
Dan Gohmanc54baa22008-12-03 18:43:12 +0000270foldMemoryOperandImpl(MachineFunction &MF,
271 MachineInstr* MI,
272 const SmallVectorImpl<unsigned> &Ops, int FI) const
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000273{
274 if (Ops.size() != 1) return NULL;
275
276 MachineInstr *NewMI = NULL;
277
278 switch (MI->getOpcode()) {
279 case Mips::ADDu:
Dan Gohmand735b802008-10-03 15:45:36 +0000280 if ((MI->getOperand(0).isReg()) &&
281 (MI->getOperand(1).isReg()) &&
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000282 (MI->getOperand(1).getReg() == Mips::ZERO) &&
Dan Gohmand735b802008-10-03 15:45:36 +0000283 (MI->getOperand(2).isReg())) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000284 if (Ops[0] == 0) { // COPY -> STORE
285 unsigned SrcReg = MI->getOperand(2).getReg();
286 bool isKill = MI->getOperand(2).isKill();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000287 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::SW))
Bill Wendling587daed2009-05-13 21:33:08 +0000288 .addReg(SrcReg, getKillRegState(isKill))
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000289 .addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000290 } else { // COPY -> LOAD
291 unsigned DstReg = MI->getOperand(0).getReg();
292 bool isDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000293 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::LW))
Bill Wendling587daed2009-05-13 21:33:08 +0000294 .addReg(DstReg, RegState::Define | getDeadRegState(isDead))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000295 .addImm(0).addFrameIndex(FI);
296 }
297 }
298 break;
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000299 case Mips::FMOV_S32:
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000300 case Mips::FMOV_D32:
Dan Gohmand735b802008-10-03 15:45:36 +0000301 if ((MI->getOperand(0).isReg()) &&
302 (MI->getOperand(1).isReg())) {
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +0000303 const TargetRegisterClass
304 *RC = RI.getRegClass(MI->getOperand(0).getReg());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000305 unsigned StoreOpc, LoadOpc;
306
307 if (RC == Mips::FGR32RegisterClass) {
308 LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
Chris Lattnere3a85832009-03-26 05:28:26 +0000309 } else {
310 assert(RC == Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000311 LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
Chris Lattnere3a85832009-03-26 05:28:26 +0000312 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000313
314 if (Ops[0] == 0) { // COPY -> STORE
315 unsigned SrcReg = MI->getOperand(1).getReg();
316 bool isKill = MI->getOperand(1).isKill();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000317 NewMI = BuildMI(MF, MI->getDebugLoc(), get(StoreOpc))
Bill Wendling587daed2009-05-13 21:33:08 +0000318 .addReg(SrcReg, getKillRegState(isKill))
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000319 .addImm(0).addFrameIndex(FI) ;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000320 } else { // COPY -> LOAD
321 unsigned DstReg = MI->getOperand(0).getReg();
322 bool isDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000323 NewMI = BuildMI(MF, MI->getDebugLoc(), get(LoadOpc))
Bill Wendling587daed2009-05-13 21:33:08 +0000324 .addReg(DstReg, RegState::Define | getDeadRegState(isDead))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000325 .addImm(0).addFrameIndex(FI);
326 }
327 }
328 break;
329 }
330
331 return NewMI;
332}
333
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000334//===----------------------------------------------------------------------===//
335// Branch Analysis
336//===----------------------------------------------------------------------===//
337
338/// GetCondFromBranchOpc - Return the Mips CC that matches
339/// the correspondent Branch instruction opcode.
340static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc)
341{
342 switch (BrOpc) {
343 default: return Mips::COND_INVALID;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000344 case Mips::BEQ : return Mips::COND_E;
345 case Mips::BNE : return Mips::COND_NE;
346 case Mips::BGTZ : return Mips::COND_GZ;
347 case Mips::BGEZ : return Mips::COND_GEZ;
348 case Mips::BLTZ : return Mips::COND_LZ;
349 case Mips::BLEZ : return Mips::COND_LEZ;
350
351 // We dont do fp branch analysis yet!
352 case Mips::BC1T :
353 case Mips::BC1F : return Mips::COND_INVALID;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000354 }
355}
356
357/// GetCondBranchFromCond - Return the Branch instruction
358/// opcode that matches the cc.
359unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC)
360{
361 switch (CC) {
362 default: assert(0 && "Illegal condition code!");
363 case Mips::COND_E : return Mips::BEQ;
364 case Mips::COND_NE : return Mips::BNE;
365 case Mips::COND_GZ : return Mips::BGTZ;
366 case Mips::COND_GEZ : return Mips::BGEZ;
367 case Mips::COND_LZ : return Mips::BLTZ;
368 case Mips::COND_LEZ : return Mips::BLEZ;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000369
370 case Mips::FCOND_F:
371 case Mips::FCOND_UN:
372 case Mips::FCOND_EQ:
373 case Mips::FCOND_UEQ:
374 case Mips::FCOND_OLT:
375 case Mips::FCOND_ULT:
376 case Mips::FCOND_OLE:
377 case Mips::FCOND_ULE:
378 case Mips::FCOND_SF:
379 case Mips::FCOND_NGLE:
380 case Mips::FCOND_SEQ:
381 case Mips::FCOND_NGL:
382 case Mips::FCOND_LT:
383 case Mips::FCOND_NGE:
384 case Mips::FCOND_LE:
385 case Mips::FCOND_NGT: return Mips::BC1T;
386
387 case Mips::FCOND_T:
388 case Mips::FCOND_OR:
389 case Mips::FCOND_NEQ:
390 case Mips::FCOND_OGL:
391 case Mips::FCOND_UGE:
392 case Mips::FCOND_OGE:
393 case Mips::FCOND_UGT:
394 case Mips::FCOND_OGT:
395 case Mips::FCOND_ST:
396 case Mips::FCOND_GLE:
397 case Mips::FCOND_SNE:
398 case Mips::FCOND_GL:
399 case Mips::FCOND_NLT:
400 case Mips::FCOND_GE:
401 case Mips::FCOND_NLE:
402 case Mips::FCOND_GT: return Mips::BC1F;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000403 }
404}
405
406/// GetOppositeBranchCondition - Return the inverse of the specified
407/// condition, e.g. turning COND_E to COND_NE.
408Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
409{
410 switch (CC) {
411 default: assert(0 && "Illegal condition code!");
412 case Mips::COND_E : return Mips::COND_NE;
413 case Mips::COND_NE : return Mips::COND_E;
414 case Mips::COND_GZ : return Mips::COND_LEZ;
415 case Mips::COND_GEZ : return Mips::COND_LZ;
416 case Mips::COND_LZ : return Mips::COND_GEZ;
417 case Mips::COND_LEZ : return Mips::COND_GZ;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000418 case Mips::FCOND_F : return Mips::FCOND_T;
419 case Mips::FCOND_UN : return Mips::FCOND_OR;
420 case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
421 case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
422 case Mips::FCOND_OLT: return Mips::FCOND_UGE;
423 case Mips::FCOND_ULT: return Mips::FCOND_OGE;
424 case Mips::FCOND_OLE: return Mips::FCOND_UGT;
425 case Mips::FCOND_ULE: return Mips::FCOND_OGT;
426 case Mips::FCOND_SF: return Mips::FCOND_ST;
427 case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
428 case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
429 case Mips::FCOND_NGL: return Mips::FCOND_GL;
430 case Mips::FCOND_LT: return Mips::FCOND_NLT;
431 case Mips::FCOND_NGE: return Mips::FCOND_GE;
432 case Mips::FCOND_LE: return Mips::FCOND_NLE;
433 case Mips::FCOND_NGT: return Mips::FCOND_GT;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000434 }
435}
436
437bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
438 MachineBasicBlock *&TBB,
439 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000440 SmallVectorImpl<MachineOperand> &Cond,
441 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000442{
443 // If the block has no terminators, it just falls into the block after it.
444 MachineBasicBlock::iterator I = MBB.end();
445 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
446 return false;
447
448 // Get the last instruction in the block.
449 MachineInstr *LastInst = I;
450
451 // If there is only one terminator instruction, process it.
452 unsigned LastOpc = LastInst->getOpcode();
453 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000454 if (!LastInst->getDesc().isBranch())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000455 return true;
456
457 // Unconditional branch
458 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000459 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000460 return false;
461 }
462
463 Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
464 if (BranchCode == Mips::COND_INVALID)
465 return true; // Can't handle indirect branch.
466
467 // Conditional branch
468 // Block ends with fall-through condbranch.
469 if (LastOpc != Mips::COND_INVALID) {
470 int LastNumOp = LastInst->getNumOperands();
471
Chris Lattner8aa797a2007-12-30 23:10:15 +0000472 TBB = LastInst->getOperand(LastNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000473 Cond.push_back(MachineOperand::CreateImm(BranchCode));
474
475 for (int i=0; i<LastNumOp-1; i++) {
476 Cond.push_back(LastInst->getOperand(i));
477 }
478
479 return false;
480 }
481 }
482
483 // Get the instruction before it if it is a terminator.
484 MachineInstr *SecondLastInst = I;
485
486 // If there are three terminators, we don't know what sort of block this is.
487 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
488 return true;
489
490 // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
491 unsigned SecondLastOpc = SecondLastInst->getOpcode();
492 Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
493
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000494 if (BranchCode != Mips::COND_INVALID && LastOpc == Mips::J) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000495 int SecondNumOp = SecondLastInst->getNumOperands();
496
Chris Lattner8aa797a2007-12-30 23:10:15 +0000497 TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000498 Cond.push_back(MachineOperand::CreateImm(BranchCode));
499
500 for (int i=0; i<SecondNumOp-1; i++) {
501 Cond.push_back(SecondLastInst->getOperand(i));
502 }
503
Chris Lattner8aa797a2007-12-30 23:10:15 +0000504 FBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000505 return false;
506 }
507
508 // If the block ends with two unconditional branches, handle it. The last
509 // one is not executed, so remove it.
510 if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000511 TBB = SecondLastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000512 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000513 if (AllowModify)
514 I->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000515 return false;
516 }
517
518 // Otherwise, can't handle this.
519 return true;
520}
521
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000522unsigned MipsInstrInfo::
523InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000524 MachineBasicBlock *FBB,
525 const SmallVectorImpl<MachineOperand> &Cond) const {
Dale Johannesen94817572009-02-13 02:34:39 +0000526 // FIXME this should probably have a DebugLoc argument
527 DebugLoc dl = DebugLoc::getUnknownLoc();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000528 // Shouldn't be a fall through.
529 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
530 assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
531 "Mips branch conditions can have two|three components!");
532
533 if (FBB == 0) { // One way branch.
534 if (Cond.empty()) {
535 // Unconditional branch?
Dale Johannesen94817572009-02-13 02:34:39 +0000536 BuildMI(&MBB, dl, get(Mips::J)).addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000537 } else {
538 // Conditional branch.
539 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000540 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000541
Chris Lattner349c4952008-01-07 03:13:06 +0000542 if (TID.getNumOperands() == 3)
Dale Johannesen94817572009-02-13 02:34:39 +0000543 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000544 .addReg(Cond[2].getReg())
545 .addMBB(TBB);
546 else
Dale Johannesen94817572009-02-13 02:34:39 +0000547 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000548 .addMBB(TBB);
549
550 }
551 return 1;
552 }
553
554 // Two-way Conditional branch.
555 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000556 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000557
Chris Lattner349c4952008-01-07 03:13:06 +0000558 if (TID.getNumOperands() == 3)
Dale Johannesen94817572009-02-13 02:34:39 +0000559 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000560 .addMBB(TBB);
561 else
Dale Johannesen94817572009-02-13 02:34:39 +0000562 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()).addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000563
Dale Johannesen94817572009-02-13 02:34:39 +0000564 BuildMI(&MBB, dl, get(Mips::J)).addMBB(FBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000565 return 2;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000566}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000567
568unsigned MipsInstrInfo::
569RemoveBranch(MachineBasicBlock &MBB) const
570{
571 MachineBasicBlock::iterator I = MBB.end();
572 if (I == MBB.begin()) return 0;
573 --I;
574 if (I->getOpcode() != Mips::J &&
575 GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
576 return 0;
577
578 // Remove the branch.
579 I->eraseFromParent();
580
581 I = MBB.end();
582
583 if (I == MBB.begin()) return 1;
584 --I;
585 if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
586 return 1;
587
588 // Remove the branch.
589 I->eraseFromParent();
590 return 2;
591}
592
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000593/// BlockHasNoFallThrough - Analyze if MachineBasicBlock does not
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000594/// fall-through into its successor block.
595bool MipsInstrInfo::
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000596BlockHasNoFallThrough(const MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000597{
598 if (MBB.empty()) return false;
599
600 switch (MBB.back().getOpcode()) {
601 case Mips::RET: // Return.
602 case Mips::JR: // Indirect branch.
603 case Mips::J: // Uncond branch.
604 return true;
605 default: return false;
606 }
607}
608
609/// ReverseBranchCondition - Return the inverse opcode of the
610/// specified Branch instruction.
611bool MipsInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000612ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000613{
614 assert( (Cond.size() == 3 || Cond.size() == 2) &&
615 "Invalid Mips branch condition!");
616 Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));
617 return false;
618}