blob: 8a4a8b3e0fa681bdd50d63caae178021d8580836 [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"
Dan Gohman99114052009-06-03 20:30:14 +000016#include "MipsMachineFunction.h"
Owen Anderson718cb662007-09-07 04:06:50 +000017#include "llvm/ADT/STLExtras.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman99114052009-06-03 20:30:14 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000020#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021#include "MipsGenInstrInfo.inc"
22
23using namespace llvm;
24
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000025MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
Chris Lattner64105522008-01-01 01:03:04 +000026 : TargetInstrInfoImpl(MipsInsts, array_lengthof(MipsInsts)),
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000027 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000028
29static bool isZeroImm(const MachineOperand &op) {
Dan Gohmand735b802008-10-03 15:45:36 +000030 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031}
32
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033/// isLoadFromStackSlot - If the specified machine instruction is a direct
34/// load from a stack slot, return the virtual or physical register number of
35/// the destination along with the FrameIndex of the loaded stack slot. If
36/// not, return 0. This predicate must return 0 if the instruction has
37/// any side effects other than loading from the stack slot.
38unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000039isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000040{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000041 if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000042 (MI->getOpcode() == Mips::LDC1)) {
Dan Gohmand735b802008-10-03 15:45:36 +000043 if ((MI->getOperand(2).isFI()) && // is a stack slot
44 (MI->getOperand(1).isImm()) && // the imm is zero
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000045 (isZeroImm(MI->getOperand(1)))) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000046 FrameIndex = MI->getOperand(2).getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000047 return MI->getOperand(0).getReg();
48 }
49 }
50
51 return 0;
52}
53
54/// isStoreToStackSlot - If the specified machine instruction is a direct
55/// store to a stack slot, return the virtual or physical register number of
56/// the source reg along with the FrameIndex of the loaded stack slot. If
57/// not, return 0. This predicate must return 0 if the instruction has
58/// any side effects other than storing to the stack slot.
59unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000060isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000061{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000062 if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000063 (MI->getOpcode() == Mips::SDC1)) {
Dan Gohmand735b802008-10-03 15:45:36 +000064 if ((MI->getOperand(2).isFI()) && // is a stack slot
65 (MI->getOperand(1).isImm()) && // the imm is zero
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000066 (isZeroImm(MI->getOperand(1)))) {
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +000067 FrameIndex = MI->getOperand(2).getIndex();
68 return MI->getOperand(0).getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000069 }
70 }
71 return 0;
72}
73
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000074/// insertNoop - If data hazard condition is found insert the target nop
75/// instruction.
76void MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000077insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000078{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000079 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +000080 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000081}
82
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +000083void MipsInstrInfo::
84copyPhysReg(MachineBasicBlock &MBB,
85 MachineBasicBlock::iterator I, DebugLoc DL,
86 unsigned DestReg, unsigned SrcReg,
87 bool KillSrc) const {
88 bool DestCPU = Mips::CPURegsRegClass.contains(DestReg);
89 bool SrcCPU = Mips::CPURegsRegClass.contains(SrcReg);
Bill Wendlingd1c321a2009-02-12 00:02:55 +000090
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +000091 // CPU-CPU is the most common.
92 if (DestCPU && SrcCPU) {
93 BuildMI(MBB, I, DL, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
94 .addReg(SrcReg, getKillRegState(KillSrc));
95 return;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000096 }
97
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +000098 // Copy to CPU from other registers.
99 if (DestCPU) {
100 if (Mips::CCRRegClass.contains(SrcReg))
101 BuildMI(MBB, I, DL, get(Mips::CFC1), DestReg)
102 .addReg(SrcReg, getKillRegState(KillSrc));
103 else if (Mips::FGR32RegClass.contains(SrcReg))
104 BuildMI(MBB, I, DL, get(Mips::MFC1), DestReg)
105 .addReg(SrcReg, getKillRegState(KillSrc));
106 else if (SrcReg == Mips::HI)
107 BuildMI(MBB, I, DL, get(Mips::MFHI), DestReg);
108 else if (SrcReg == Mips::LO)
109 BuildMI(MBB, I, DL, get(Mips::MFLO), DestReg);
110 else
111 llvm_unreachable("Copy to CPU from invalid register");
112 return;
113 }
114
115 // Copy to other registers from CPU.
116 if (SrcCPU) {
117 if (Mips::CCRRegClass.contains(DestReg))
118 BuildMI(MBB, I, DL, get(Mips::CTC1), DestReg)
119 .addReg(SrcReg, getKillRegState(KillSrc));
120 else if (Mips::FGR32RegClass.contains(DestReg))
121 BuildMI(MBB, I, DL, get(Mips::MTC1), DestReg)
122 .addReg(SrcReg, getKillRegState(KillSrc));
123 else if (DestReg == Mips::HI)
124 BuildMI(MBB, I, DL, get(Mips::MTHI))
125 .addReg(SrcReg, getKillRegState(KillSrc));
126 else if (DestReg == Mips::LO)
127 BuildMI(MBB, I, DL, get(Mips::MTLO))
128 .addReg(SrcReg, getKillRegState(KillSrc));
129 else
130 llvm_unreachable("Copy from CPU to invalid register");
131 return;
132 }
133
134 if (Mips::FGR32RegClass.contains(DestReg, SrcReg)) {
135 BuildMI(MBB, I, DL, get(Mips::FMOV_S32), DestReg)
136 .addReg(SrcReg, getKillRegState(KillSrc));
137 return;
138 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000139
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000140 if (Mips::AFGR64RegClass.contains(DestReg, SrcReg)) {
141 BuildMI(MBB, I, DL, get(Mips::FMOV_D32), DestReg)
142 .addReg(SrcReg, getKillRegState(KillSrc));
143 return;
144 }
145
146 if (Mips::CCRRegClass.contains(DestReg, SrcReg)) {
147 BuildMI(MBB, I, DL, get(Mips::MOVCCRToCCR), DestReg)
148 .addReg(SrcReg, getKillRegState(KillSrc));
149 return;
150 }
151 llvm_unreachable("Cannot copy registers");
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000152}
153
154void MipsInstrInfo::
155storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000156 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000157 const TargetRegisterClass *RC,
158 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000159 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000160 if (I != MBB.end()) DL = I->getDebugLoc();
161
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000162 if (RC == Mips::CPURegsRegisterClass)
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000163 BuildMI(MBB, I, DL, get(Mips::SW)).addReg(SrcReg, getKillRegState(isKill))
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000164 .addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000165 else if (RC == Mips::FGR32RegisterClass)
166 BuildMI(MBB, I, DL, get(Mips::SWC1)).addReg(SrcReg, getKillRegState(isKill))
167 .addImm(0).addFrameIndex(FI);
168 else if (RC == Mips::AFGR64RegisterClass) {
169 if (!TM.getSubtarget<MipsSubtarget>().isMips1()) {
170 BuildMI(MBB, I, DL, get(Mips::SDC1))
171 .addReg(SrcReg, getKillRegState(isKill))
172 .addImm(0).addFrameIndex(FI);
173 } else {
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000174 const TargetRegisterInfo *TRI =
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000175 MBB.getParent()->getTarget().getRegisterInfo();
176 const unsigned *SubSet = TRI->getSubRegisters(SrcReg);
177 BuildMI(MBB, I, DL, get(Mips::SWC1))
178 .addReg(SubSet[0], getKillRegState(isKill))
179 .addImm(0).addFrameIndex(FI);
180 BuildMI(MBB, I, DL, get(Mips::SWC1))
181 .addReg(SubSet[1], getKillRegState(isKill))
182 .addImm(4).addFrameIndex(FI);
183 }
184 } else
185 llvm_unreachable("Register class not handled!");
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000186}
187
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000188void MipsInstrInfo::
189loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
190 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000191 const TargetRegisterClass *RC,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000192 const TargetRegisterInfo *TRI) const
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000193{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000194 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000195 if (I != MBB.end()) DL = I->getDebugLoc();
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000196
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000197 if (RC == Mips::CPURegsRegisterClass)
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000198 BuildMI(MBB, I, DL, get(Mips::LW), DestReg).addImm(0).addFrameIndex(FI);
199 else if (RC == Mips::FGR32RegisterClass)
200 BuildMI(MBB, I, DL, get(Mips::LWC1), DestReg).addImm(0).addFrameIndex(FI);
201 else if (RC == Mips::AFGR64RegisterClass) {
202 if (!TM.getSubtarget<MipsSubtarget>().isMips1()) {
203 BuildMI(MBB, I, DL, get(Mips::LDC1), DestReg).addImm(0).addFrameIndex(FI);
204 } else {
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000205 const TargetRegisterInfo *TRI =
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000206 MBB.getParent()->getTarget().getRegisterInfo();
207 const unsigned *SubSet = TRI->getSubRegisters(DestReg);
208 BuildMI(MBB, I, DL, get(Mips::LWC1), SubSet[0])
209 .addImm(0).addFrameIndex(FI);
210 BuildMI(MBB, I, DL, get(Mips::LWC1), SubSet[1])
211 .addImm(4).addFrameIndex(FI);
212 }
213 } else
214 llvm_unreachable("Register class not handled!");
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000215}
216
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000217//===----------------------------------------------------------------------===//
218// Branch Analysis
219//===----------------------------------------------------------------------===//
220
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000221/// GetCondFromBranchOpc - Return the Mips CC that matches
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000222/// the correspondent Branch instruction opcode.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000223static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc)
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000224{
225 switch (BrOpc) {
226 default: return Mips::COND_INVALID;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000227 case Mips::BEQ : return Mips::COND_E;
228 case Mips::BNE : return Mips::COND_NE;
229 case Mips::BGTZ : return Mips::COND_GZ;
230 case Mips::BGEZ : return Mips::COND_GEZ;
231 case Mips::BLTZ : return Mips::COND_LZ;
232 case Mips::BLEZ : return Mips::COND_LEZ;
233
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000234 // We dont do fp branch analysis yet!
235 case Mips::BC1T :
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000236 case Mips::BC1F : return Mips::COND_INVALID;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000237 }
238}
239
240/// GetCondBranchFromCond - Return the Branch instruction
241/// opcode that matches the cc.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000242unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC)
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000243{
244 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000245 default: llvm_unreachable("Illegal condition code!");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000246 case Mips::COND_E : return Mips::BEQ;
247 case Mips::COND_NE : return Mips::BNE;
248 case Mips::COND_GZ : return Mips::BGTZ;
249 case Mips::COND_GEZ : return Mips::BGEZ;
250 case Mips::COND_LZ : return Mips::BLTZ;
251 case Mips::COND_LEZ : return Mips::BLEZ;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000252
253 case Mips::FCOND_F:
254 case Mips::FCOND_UN:
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000255 case Mips::FCOND_OEQ:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000256 case Mips::FCOND_UEQ:
257 case Mips::FCOND_OLT:
258 case Mips::FCOND_ULT:
259 case Mips::FCOND_OLE:
260 case Mips::FCOND_ULE:
261 case Mips::FCOND_SF:
262 case Mips::FCOND_NGLE:
263 case Mips::FCOND_SEQ:
264 case Mips::FCOND_NGL:
265 case Mips::FCOND_LT:
266 case Mips::FCOND_NGE:
267 case Mips::FCOND_LE:
268 case Mips::FCOND_NGT: return Mips::BC1T;
269
270 case Mips::FCOND_T:
271 case Mips::FCOND_OR:
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000272 case Mips::FCOND_UNE:
273 case Mips::FCOND_ONE:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000274 case Mips::FCOND_UGE:
275 case Mips::FCOND_OGE:
276 case Mips::FCOND_UGT:
277 case Mips::FCOND_OGT:
278 case Mips::FCOND_ST:
279 case Mips::FCOND_GLE:
280 case Mips::FCOND_SNE:
281 case Mips::FCOND_GL:
282 case Mips::FCOND_NLT:
283 case Mips::FCOND_GE:
284 case Mips::FCOND_NLE:
285 case Mips::FCOND_GT: return Mips::BC1F;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000286 }
287}
288
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000289/// GetOppositeBranchCondition - Return the inverse of the specified
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000290/// condition, e.g. turning COND_E to COND_NE.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000291Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000292{
293 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000294 default: llvm_unreachable("Illegal condition code!");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000295 case Mips::COND_E : return Mips::COND_NE;
296 case Mips::COND_NE : return Mips::COND_E;
297 case Mips::COND_GZ : return Mips::COND_LEZ;
298 case Mips::COND_GEZ : return Mips::COND_LZ;
299 case Mips::COND_LZ : return Mips::COND_GEZ;
300 case Mips::COND_LEZ : return Mips::COND_GZ;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000301 case Mips::FCOND_F : return Mips::FCOND_T;
302 case Mips::FCOND_UN : return Mips::FCOND_OR;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000303 case Mips::FCOND_OEQ: return Mips::FCOND_UNE;
304 case Mips::FCOND_UEQ: return Mips::FCOND_ONE;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000305 case Mips::FCOND_OLT: return Mips::FCOND_UGE;
306 case Mips::FCOND_ULT: return Mips::FCOND_OGE;
307 case Mips::FCOND_OLE: return Mips::FCOND_UGT;
308 case Mips::FCOND_ULE: return Mips::FCOND_OGT;
309 case Mips::FCOND_SF: return Mips::FCOND_ST;
310 case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
311 case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
312 case Mips::FCOND_NGL: return Mips::FCOND_GL;
313 case Mips::FCOND_LT: return Mips::FCOND_NLT;
314 case Mips::FCOND_NGE: return Mips::FCOND_GE;
315 case Mips::FCOND_LE: return Mips::FCOND_NLE;
316 case Mips::FCOND_NGT: return Mips::FCOND_GT;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000317 }
318}
319
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000320bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000321 MachineBasicBlock *&TBB,
322 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000323 SmallVectorImpl<MachineOperand> &Cond,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000324 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000325{
326 // If the block has no terminators, it just falls into the block after it.
327 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000328 if (I == MBB.begin())
329 return false;
330 --I;
331 while (I->isDebugValue()) {
332 if (I == MBB.begin())
333 return false;
334 --I;
335 }
336 if (!isUnpredicatedTerminator(I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000337 return false;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000338
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000339 // Get the last instruction in the block.
340 MachineInstr *LastInst = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000341
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000342 // If there is only one terminator instruction, process it.
343 unsigned LastOpc = LastInst->getOpcode();
344 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000345 if (!LastInst->getDesc().isBranch())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000346 return true;
347
348 // Unconditional branch
349 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000350 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000351 return false;
352 }
353
354 Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
355 if (BranchCode == Mips::COND_INVALID)
356 return true; // Can't handle indirect branch.
357
358 // Conditional branch
359 // Block ends with fall-through condbranch.
360 if (LastOpc != Mips::COND_INVALID) {
361 int LastNumOp = LastInst->getNumOperands();
362
Chris Lattner8aa797a2007-12-30 23:10:15 +0000363 TBB = LastInst->getOperand(LastNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000364 Cond.push_back(MachineOperand::CreateImm(BranchCode));
365
366 for (int i=0; i<LastNumOp-1; i++) {
367 Cond.push_back(LastInst->getOperand(i));
368 }
369
370 return false;
371 }
372 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000373
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000374 // Get the instruction before it if it is a terminator.
375 MachineInstr *SecondLastInst = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000376
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000377 // If there are three terminators, we don't know what sort of block this is.
378 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
379 return true;
380
381 // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
382 unsigned SecondLastOpc = SecondLastInst->getOpcode();
383 Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
384
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000385 if (BranchCode != Mips::COND_INVALID && LastOpc == Mips::J) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000386 int SecondNumOp = SecondLastInst->getNumOperands();
387
Chris Lattner8aa797a2007-12-30 23:10:15 +0000388 TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000389 Cond.push_back(MachineOperand::CreateImm(BranchCode));
390
391 for (int i=0; i<SecondNumOp-1; i++) {
392 Cond.push_back(SecondLastInst->getOperand(i));
393 }
394
Chris Lattner8aa797a2007-12-30 23:10:15 +0000395 FBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000396 return false;
397 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000398
399 // If the block ends with two unconditional branches, handle it. The last
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000400 // one is not executed, so remove it.
401 if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000402 TBB = SecondLastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000403 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000404 if (AllowModify)
405 I->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000406 return false;
407 }
408
409 // Otherwise, can't handle this.
410 return true;
411}
412
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000413unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000414InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000415 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000416 const SmallVectorImpl<MachineOperand> &Cond,
417 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000418 // Shouldn't be a fall through.
419 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
420 assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
421 "Mips branch conditions can have two|three components!");
422
423 if (FBB == 0) { // One way branch.
424 if (Cond.empty()) {
425 // Unconditional branch?
Stuart Hastings3bf91252010-06-17 22:43:56 +0000426 BuildMI(&MBB, DL, get(Mips::J)).addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000427 } else {
428 // Conditional branch.
429 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000430 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000431
Chris Lattner349c4952008-01-07 03:13:06 +0000432 if (TID.getNumOperands() == 3)
Stuart Hastings3bf91252010-06-17 22:43:56 +0000433 BuildMI(&MBB, DL, TID).addReg(Cond[1].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000434 .addReg(Cond[2].getReg())
435 .addMBB(TBB);
436 else
Stuart Hastings3bf91252010-06-17 22:43:56 +0000437 BuildMI(&MBB, DL, TID).addReg(Cond[1].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000438 .addMBB(TBB);
439
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000440 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000441 return 1;
442 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000443
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000444 // Two-way Conditional branch.
445 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
Chris Lattner749c6f62008-01-07 07:27:27 +0000446 const TargetInstrDesc &TID = get(Opc);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000447
Chris Lattner349c4952008-01-07 03:13:06 +0000448 if (TID.getNumOperands() == 3)
Stuart Hastings3bf91252010-06-17 22:43:56 +0000449 BuildMI(&MBB, DL, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000450 .addMBB(TBB);
451 else
Stuart Hastings3bf91252010-06-17 22:43:56 +0000452 BuildMI(&MBB, DL, TID).addReg(Cond[1].getReg()).addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000453
Stuart Hastings3bf91252010-06-17 22:43:56 +0000454 BuildMI(&MBB, DL, get(Mips::J)).addMBB(FBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000455 return 2;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000456}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000457
458unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000459RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000460{
461 MachineBasicBlock::iterator I = MBB.end();
462 if (I == MBB.begin()) return 0;
463 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000464 while (I->isDebugValue()) {
465 if (I == MBB.begin())
466 return 0;
467 --I;
468 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000469 if (I->getOpcode() != Mips::J &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000470 GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
471 return 0;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000472
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000473 // Remove the branch.
474 I->eraseFromParent();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000475
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000476 I = MBB.end();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000477
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000478 if (I == MBB.begin()) return 1;
479 --I;
480 if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
481 return 1;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000482
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000483 // Remove the branch.
484 I->eraseFromParent();
485 return 2;
486}
487
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000488/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000489/// specified Branch instruction.
490bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000491ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000492{
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000493 assert( (Cond.size() == 3 || Cond.size() == 2) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000494 "Invalid Mips branch condition!");
495 Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));
496 return false;
497}
Dan Gohman99114052009-06-03 20:30:14 +0000498
499/// getGlobalBaseReg - Return a virtual register initialized with the
500/// the global base register value. Output instructions required to
501/// initialize the register in the function entry block, if necessary.
502///
503unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
504 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
505 unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
506 if (GlobalBaseReg != 0)
507 return GlobalBaseReg;
508
509 // Insert the set of GlobalBaseReg into the first MBB of the function
510 MachineBasicBlock &FirstMBB = MF->front();
511 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
512 MachineRegisterInfo &RegInfo = MF->getRegInfo();
513 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
514
515 GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
Jakob Stoklund Olesen3ecf1f02010-07-10 22:43:03 +0000516 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
517 GlobalBaseReg).addReg(Mips::GP);
Dan Gohman99114052009-06-03 20:30:14 +0000518 RegInfo.addLiveIn(Mips::GP);
519
520 MipsFI->setGlobalBaseReg(GlobalBaseReg);
521 return GlobalBaseReg;
522}