blob: df5a089588f16ca1d45465ea3693a6a7d96c9948 [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===- MipsInstrInfo.cpp - Mips Instruction Information ---------*- C++ -*-===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
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//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file contains the Mips implementation of the TargetInstrInfo class.
11//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000013
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))
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000164 .addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000165 else if (RC == Mips::FGR32RegisterClass)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000166 BuildMI(MBB, I, DL, get(Mips::SWC1)).addReg(SrcReg, getKillRegState(isKill))
167 .addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000168 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()) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000203 BuildMI(MBB, I, DL, get(Mips::LDC1), DestReg).addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000204 } 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
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000217//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000218// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000219//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000220
Akira Hatanaka20ada982011-04-01 17:39:08 +0000221static unsigned GetAnalyzableBrOpc(unsigned Opc) {
222 return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ ||
223 Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ ||
224 Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::J) ? Opc : 0;
225}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000226
Akira Hatanaka20ada982011-04-01 17:39:08 +0000227/// GetOppositeBranchOpc - Return the inverse of the specified
228/// opcode, e.g. turning BEQ to BNE.
229unsigned Mips::GetOppositeBranchOpc(unsigned Opc)
230{
231 switch (Opc) {
232 default: llvm_unreachable("Illegal opcode!");
233 case Mips::BEQ : return Mips::BNE;
234 case Mips::BNE : return Mips::BEQ;
235 case Mips::BGTZ : return Mips::BLEZ;
236 case Mips::BGEZ : return Mips::BLTZ;
237 case Mips::BLTZ : return Mips::BGEZ;
238 case Mips::BLEZ : return Mips::BGTZ;
239 case Mips::BC1T : return Mips::BC1F;
240 case Mips::BC1F : return Mips::BC1T;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000241 }
242}
243
Akira Hatanaka20ada982011-04-01 17:39:08 +0000244static void AnalyzeCondBr(const MachineInstr* Inst, unsigned Opc,
245 MachineBasicBlock *&BB,
246 SmallVectorImpl<MachineOperand>& Cond) {
247 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
248 int NumOp = Inst->getNumExplicitOperands();
249
250 // for both int and fp branches, the last explicit operand is the
251 // MBB.
252 BB = Inst->getOperand(NumOp-1).getMBB();
253 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000254
Akira Hatanaka20ada982011-04-01 17:39:08 +0000255 for (int i=0; i<NumOp-1; i++)
256 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000257}
258
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000259bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000260 MachineBasicBlock *&TBB,
261 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000262 SmallVectorImpl<MachineOperand> &Cond,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000263 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000264{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000265 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000266
Akira Hatanaka20ada982011-04-01 17:39:08 +0000267 // Skip all the debug instructions.
268 while (I != REnd && I->isDebugValue())
269 ++I;
270
271 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
272 // If this block ends with no branches (it just falls through to its succ)
273 // just return false, leaving TBB/FBB null.
274 TBB = FBB = NULL;
275 return false;
276 }
277
278 MachineInstr *LastInst = &*I;
279 unsigned LastOpc = LastInst->getOpcode();
280
281 // Not an analyzable branch (must be an indirect jump).
282 if (!GetAnalyzableBrOpc(LastOpc))
283 return true;
284
285 // Get the second to last instruction in the block.
286 unsigned SecondLastOpc = 0;
287 MachineInstr *SecondLastInst = NULL;
288
289 if (++I != REnd) {
290 SecondLastInst = &*I;
291 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
292
293 // Not an analyzable branch (must be an indirect jump).
294 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
295 return true;
296 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000297
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000298 // If there is only one terminator instruction, process it.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000299 if (!SecondLastOpc) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000300 // Unconditional branch
301 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000302 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000303 return false;
304 }
305
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000306 // Conditional branch
Akira Hatanaka20ada982011-04-01 17:39:08 +0000307 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
308 return false;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000309 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000310
Akira Hatanaka20ada982011-04-01 17:39:08 +0000311 // If we reached here, there are two branches.
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000312 // If there are three terminators, we don't know what sort of block this is.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000313 if (++I != REnd && isUnpredicatedTerminator(&*I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000314 return true;
315
Akira Hatanaka20ada982011-04-01 17:39:08 +0000316 // If second to last instruction is an unconditional branch,
317 // analyze it and remove the last instruction.
318 if (SecondLastOpc == Mips::J) {
319 // Return if the last instruction cannot be removed.
320 if (!AllowModify)
321 return true;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000322
Chris Lattner8aa797a2007-12-30 23:10:15 +0000323 TBB = SecondLastInst->getOperand(0).getMBB();
Akira Hatanaka20ada982011-04-01 17:39:08 +0000324 LastInst->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000325 return false;
326 }
327
Akira Hatanaka20ada982011-04-01 17:39:08 +0000328 // Conditional branch followed by an unconditional branch.
329 // The last one must be unconditional.
330 if (LastOpc != Mips::J)
331 return true;
332
333 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
334 FBB = LastInst->getOperand(0).getMBB();
335
336 return false;
337}
338
339void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
340 MachineBasicBlock *TBB, DebugLoc DL,
341 const SmallVectorImpl<MachineOperand>& Cond)
342 const {
343 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000344 const MCInstrDesc &MCID = get(Opc);
345 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000346
347 for (unsigned i = 1; i < Cond.size(); ++i)
348 MIB.addReg(Cond[i].getReg());
349
350 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000351}
352
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000353unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000354InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000355 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000356 const SmallVectorImpl<MachineOperand> &Cond,
357 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000358 // Shouldn't be a fall through.
359 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000360
Akira Hatanaka20ada982011-04-01 17:39:08 +0000361 // # of condition operands:
362 // Unconditional branches: 0
363 // Floating point branches: 1 (opc)
364 // Int BranchZero: 2 (opc, reg)
365 // Int Branch: 3 (opc, reg0, reg1)
366 assert((Cond.size() <= 3) &&
367 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000368
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000369 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000370 if (FBB) {
371 BuildCondBr(MBB, TBB, DL, Cond);
372 BuildMI(&MBB, DL, get(Mips::J)).addMBB(FBB);
373 return 2;
374 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000375
Akira Hatanaka20ada982011-04-01 17:39:08 +0000376 // One way branch.
377 // Unconditional branch.
378 if (Cond.empty())
379 BuildMI(&MBB, DL, get(Mips::J)).addMBB(TBB);
380 else // Conditional branch.
381 BuildCondBr(MBB, TBB, DL, Cond);
382 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000383}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000384
385unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000386RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000387{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000388 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
389 MachineBasicBlock::reverse_iterator FirstBr;
390 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000391
Akira Hatanaka20ada982011-04-01 17:39:08 +0000392 // Skip all the debug instructions.
393 while (I != REnd && I->isDebugValue())
394 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000395
Akira Hatanaka20ada982011-04-01 17:39:08 +0000396 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000397
Akira Hatanaka20ada982011-04-01 17:39:08 +0000398 // Up to 2 branches are removed.
399 // Note that indirect branches are not removed.
400 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
401 if (!GetAnalyzableBrOpc(I->getOpcode()))
402 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000403
Akira Hatanaka20ada982011-04-01 17:39:08 +0000404 MBB.erase(I.base(), FirstBr.base());
405
406 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000407}
408
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000409/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000410/// specified Branch instruction.
411bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000412ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000413{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000414 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000415 "Invalid Mips branch condition!");
Akira Hatanaka20ada982011-04-01 17:39:08 +0000416 Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000417 return false;
418}
Dan Gohman99114052009-06-03 20:30:14 +0000419
420/// getGlobalBaseReg - Return a virtual register initialized with the
421/// the global base register value. Output instructions required to
422/// initialize the register in the function entry block, if necessary.
423///
424unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
425 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
426 unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
427 if (GlobalBaseReg != 0)
428 return GlobalBaseReg;
429
430 // Insert the set of GlobalBaseReg into the first MBB of the function
431 MachineBasicBlock &FirstMBB = MF->front();
432 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
433 MachineRegisterInfo &RegInfo = MF->getRegInfo();
434 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
435
436 GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
Jakob Stoklund Olesen3ecf1f02010-07-10 22:43:03 +0000437 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
438 GlobalBaseReg).addReg(Mips::GP);
Dan Gohman99114052009-06-03 20:30:14 +0000439 RegInfo.addLiveIn(Mips::GP);
440
441 MipsFI->setGlobalBaseReg(GlobalBaseReg);
442 return GlobalBaseReg;
443}