blob: a56c68bdd844931d50ebe8dd6c36b67354ec0343 [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"
Evan Cheng22fee2d2011-06-28 20:07:07 +000021
22#define GET_INSTRINFO_MC_DESC
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000023#include "MipsGenInstrInfo.inc"
24
25using namespace llvm;
26
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000027MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
Chris Lattner64105522008-01-01 01:03:04 +000028 : TargetInstrInfoImpl(MipsInsts, array_lengthof(MipsInsts)),
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000029 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000030
31static bool isZeroImm(const MachineOperand &op) {
Dan Gohmand735b802008-10-03 15:45:36 +000032 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033}
34
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000035/// isLoadFromStackSlot - If the specified machine instruction is a direct
36/// load from a stack slot, return the virtual or physical register number of
37/// the destination along with the FrameIndex of the loaded stack slot. If
38/// not, return 0. This predicate must return 0 if the instruction has
39/// any side effects other than loading from the stack slot.
40unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000041isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000042{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000043 if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000044 (MI->getOpcode() == Mips::LDC1)) {
Dan Gohmand735b802008-10-03 15:45:36 +000045 if ((MI->getOperand(2).isFI()) && // is a stack slot
46 (MI->getOperand(1).isImm()) && // the imm is zero
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000047 (isZeroImm(MI->getOperand(1)))) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000048 FrameIndex = MI->getOperand(2).getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000049 return MI->getOperand(0).getReg();
50 }
51 }
52
53 return 0;
54}
55
56/// isStoreToStackSlot - If the specified machine instruction is a direct
57/// store to a stack slot, return the virtual or physical register number of
58/// the source reg along with the FrameIndex of the loaded stack slot. If
59/// not, return 0. This predicate must return 0 if the instruction has
60/// any side effects other than storing to the stack slot.
61unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000062isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000063{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000064 if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000065 (MI->getOpcode() == Mips::SDC1)) {
Dan Gohmand735b802008-10-03 15:45:36 +000066 if ((MI->getOperand(2).isFI()) && // is a stack slot
67 (MI->getOperand(1).isImm()) && // the imm is zero
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000068 (isZeroImm(MI->getOperand(1)))) {
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +000069 FrameIndex = MI->getOperand(2).getIndex();
70 return MI->getOperand(0).getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000071 }
72 }
73 return 0;
74}
75
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000076/// insertNoop - If data hazard condition is found insert the target nop
77/// instruction.
78void MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000079insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000080{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000081 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +000082 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000083}
84
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +000085void MipsInstrInfo::
86copyPhysReg(MachineBasicBlock &MBB,
87 MachineBasicBlock::iterator I, DebugLoc DL,
88 unsigned DestReg, unsigned SrcReg,
89 bool KillSrc) const {
90 bool DestCPU = Mips::CPURegsRegClass.contains(DestReg);
91 bool SrcCPU = Mips::CPURegsRegClass.contains(SrcReg);
Bill Wendlingd1c321a2009-02-12 00:02:55 +000092
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +000093 // CPU-CPU is the most common.
94 if (DestCPU && SrcCPU) {
95 BuildMI(MBB, I, DL, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
96 .addReg(SrcReg, getKillRegState(KillSrc));
97 return;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000098 }
99
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000100 // Copy to CPU from other registers.
101 if (DestCPU) {
102 if (Mips::CCRRegClass.contains(SrcReg))
103 BuildMI(MBB, I, DL, get(Mips::CFC1), DestReg)
104 .addReg(SrcReg, getKillRegState(KillSrc));
105 else if (Mips::FGR32RegClass.contains(SrcReg))
106 BuildMI(MBB, I, DL, get(Mips::MFC1), DestReg)
107 .addReg(SrcReg, getKillRegState(KillSrc));
108 else if (SrcReg == Mips::HI)
109 BuildMI(MBB, I, DL, get(Mips::MFHI), DestReg);
110 else if (SrcReg == Mips::LO)
111 BuildMI(MBB, I, DL, get(Mips::MFLO), DestReg);
112 else
113 llvm_unreachable("Copy to CPU from invalid register");
114 return;
115 }
116
117 // Copy to other registers from CPU.
118 if (SrcCPU) {
119 if (Mips::CCRRegClass.contains(DestReg))
120 BuildMI(MBB, I, DL, get(Mips::CTC1), DestReg)
121 .addReg(SrcReg, getKillRegState(KillSrc));
122 else if (Mips::FGR32RegClass.contains(DestReg))
123 BuildMI(MBB, I, DL, get(Mips::MTC1), DestReg)
124 .addReg(SrcReg, getKillRegState(KillSrc));
125 else if (DestReg == Mips::HI)
126 BuildMI(MBB, I, DL, get(Mips::MTHI))
127 .addReg(SrcReg, getKillRegState(KillSrc));
128 else if (DestReg == Mips::LO)
129 BuildMI(MBB, I, DL, get(Mips::MTLO))
130 .addReg(SrcReg, getKillRegState(KillSrc));
131 else
132 llvm_unreachable("Copy from CPU to invalid register");
133 return;
134 }
135
136 if (Mips::FGR32RegClass.contains(DestReg, SrcReg)) {
137 BuildMI(MBB, I, DL, get(Mips::FMOV_S32), DestReg)
138 .addReg(SrcReg, getKillRegState(KillSrc));
139 return;
140 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000141
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000142 if (Mips::AFGR64RegClass.contains(DestReg, SrcReg)) {
143 BuildMI(MBB, I, DL, get(Mips::FMOV_D32), DestReg)
144 .addReg(SrcReg, getKillRegState(KillSrc));
145 return;
146 }
147
148 if (Mips::CCRRegClass.contains(DestReg, SrcReg)) {
149 BuildMI(MBB, I, DL, get(Mips::MOVCCRToCCR), DestReg)
150 .addReg(SrcReg, getKillRegState(KillSrc));
151 return;
152 }
153 llvm_unreachable("Cannot copy registers");
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000154}
155
156void MipsInstrInfo::
157storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000158 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000159 const TargetRegisterClass *RC,
160 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000161 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000162 if (I != MBB.end()) DL = I->getDebugLoc();
163
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000164 if (RC == Mips::CPURegsRegisterClass)
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000165 BuildMI(MBB, I, DL, get(Mips::SW)).addReg(SrcReg, getKillRegState(isKill))
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000166 .addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000167 else if (RC == Mips::FGR32RegisterClass)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000168 BuildMI(MBB, I, DL, get(Mips::SWC1)).addReg(SrcReg, getKillRegState(isKill))
169 .addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000170 else if (RC == Mips::AFGR64RegisterClass) {
171 if (!TM.getSubtarget<MipsSubtarget>().isMips1()) {
172 BuildMI(MBB, I, DL, get(Mips::SDC1))
173 .addReg(SrcReg, getKillRegState(isKill))
174 .addImm(0).addFrameIndex(FI);
175 } else {
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000176 const TargetRegisterInfo *TRI =
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000177 MBB.getParent()->getTarget().getRegisterInfo();
178 const unsigned *SubSet = TRI->getSubRegisters(SrcReg);
179 BuildMI(MBB, I, DL, get(Mips::SWC1))
180 .addReg(SubSet[0], getKillRegState(isKill))
181 .addImm(0).addFrameIndex(FI);
182 BuildMI(MBB, I, DL, get(Mips::SWC1))
183 .addReg(SubSet[1], getKillRegState(isKill))
184 .addImm(4).addFrameIndex(FI);
185 }
186 } else
187 llvm_unreachable("Register class not handled!");
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000188}
189
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000190void MipsInstrInfo::
191loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
192 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000193 const TargetRegisterClass *RC,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000194 const TargetRegisterInfo *TRI) const
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000195{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000196 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000197 if (I != MBB.end()) DL = I->getDebugLoc();
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000198
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000199 if (RC == Mips::CPURegsRegisterClass)
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000200 BuildMI(MBB, I, DL, get(Mips::LW), DestReg).addImm(0).addFrameIndex(FI);
201 else if (RC == Mips::FGR32RegisterClass)
202 BuildMI(MBB, I, DL, get(Mips::LWC1), DestReg).addImm(0).addFrameIndex(FI);
203 else if (RC == Mips::AFGR64RegisterClass) {
204 if (!TM.getSubtarget<MipsSubtarget>().isMips1()) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000205 BuildMI(MBB, I, DL, get(Mips::LDC1), DestReg).addImm(0).addFrameIndex(FI);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000206 } else {
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000207 const TargetRegisterInfo *TRI =
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000208 MBB.getParent()->getTarget().getRegisterInfo();
209 const unsigned *SubSet = TRI->getSubRegisters(DestReg);
210 BuildMI(MBB, I, DL, get(Mips::LWC1), SubSet[0])
211 .addImm(0).addFrameIndex(FI);
212 BuildMI(MBB, I, DL, get(Mips::LWC1), SubSet[1])
213 .addImm(4).addFrameIndex(FI);
214 }
215 } else
216 llvm_unreachable("Register class not handled!");
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000217}
218
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000219//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000220// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000221//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000222
Akira Hatanaka20ada982011-04-01 17:39:08 +0000223static unsigned GetAnalyzableBrOpc(unsigned Opc) {
224 return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ ||
225 Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ ||
226 Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::J) ? Opc : 0;
227}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000228
Akira Hatanaka20ada982011-04-01 17:39:08 +0000229/// GetOppositeBranchOpc - Return the inverse of the specified
230/// opcode, e.g. turning BEQ to BNE.
231unsigned Mips::GetOppositeBranchOpc(unsigned Opc)
232{
233 switch (Opc) {
234 default: llvm_unreachable("Illegal opcode!");
235 case Mips::BEQ : return Mips::BNE;
236 case Mips::BNE : return Mips::BEQ;
237 case Mips::BGTZ : return Mips::BLEZ;
238 case Mips::BGEZ : return Mips::BLTZ;
239 case Mips::BLTZ : return Mips::BGEZ;
240 case Mips::BLEZ : return Mips::BGTZ;
241 case Mips::BC1T : return Mips::BC1F;
242 case Mips::BC1F : return Mips::BC1T;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000243 }
244}
245
Akira Hatanaka20ada982011-04-01 17:39:08 +0000246static void AnalyzeCondBr(const MachineInstr* Inst, unsigned Opc,
247 MachineBasicBlock *&BB,
248 SmallVectorImpl<MachineOperand>& Cond) {
249 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
250 int NumOp = Inst->getNumExplicitOperands();
251
252 // for both int and fp branches, the last explicit operand is the
253 // MBB.
254 BB = Inst->getOperand(NumOp-1).getMBB();
255 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000256
Akira Hatanaka20ada982011-04-01 17:39:08 +0000257 for (int i=0; i<NumOp-1; i++)
258 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000259}
260
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000261bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000262 MachineBasicBlock *&TBB,
263 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000264 SmallVectorImpl<MachineOperand> &Cond,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000265 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000266{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000267 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000268
Akira Hatanaka20ada982011-04-01 17:39:08 +0000269 // Skip all the debug instructions.
270 while (I != REnd && I->isDebugValue())
271 ++I;
272
273 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
274 // If this block ends with no branches (it just falls through to its succ)
275 // just return false, leaving TBB/FBB null.
276 TBB = FBB = NULL;
277 return false;
278 }
279
280 MachineInstr *LastInst = &*I;
281 unsigned LastOpc = LastInst->getOpcode();
282
283 // Not an analyzable branch (must be an indirect jump).
284 if (!GetAnalyzableBrOpc(LastOpc))
285 return true;
286
287 // Get the second to last instruction in the block.
288 unsigned SecondLastOpc = 0;
289 MachineInstr *SecondLastInst = NULL;
290
291 if (++I != REnd) {
292 SecondLastInst = &*I;
293 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
294
295 // Not an analyzable branch (must be an indirect jump).
296 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
297 return true;
298 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000299
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000300 // If there is only one terminator instruction, process it.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000301 if (!SecondLastOpc) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000302 // Unconditional branch
303 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000304 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000305 return false;
306 }
307
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000308 // Conditional branch
Akira Hatanaka20ada982011-04-01 17:39:08 +0000309 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
310 return false;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000311 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000312
Akira Hatanaka20ada982011-04-01 17:39:08 +0000313 // If we reached here, there are two branches.
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000314 // If there are three terminators, we don't know what sort of block this is.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000315 if (++I != REnd && isUnpredicatedTerminator(&*I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000316 return true;
317
Akira Hatanaka20ada982011-04-01 17:39:08 +0000318 // If second to last instruction is an unconditional branch,
319 // analyze it and remove the last instruction.
320 if (SecondLastOpc == Mips::J) {
321 // Return if the last instruction cannot be removed.
322 if (!AllowModify)
323 return true;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000324
Chris Lattner8aa797a2007-12-30 23:10:15 +0000325 TBB = SecondLastInst->getOperand(0).getMBB();
Akira Hatanaka20ada982011-04-01 17:39:08 +0000326 LastInst->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000327 return false;
328 }
329
Akira Hatanaka20ada982011-04-01 17:39:08 +0000330 // Conditional branch followed by an unconditional branch.
331 // The last one must be unconditional.
332 if (LastOpc != Mips::J)
333 return true;
334
335 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
336 FBB = LastInst->getOperand(0).getMBB();
337
338 return false;
339}
340
341void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
342 MachineBasicBlock *TBB, DebugLoc DL,
343 const SmallVectorImpl<MachineOperand>& Cond)
344 const {
345 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000346 const MCInstrDesc &MCID = get(Opc);
347 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000348
349 for (unsigned i = 1; i < Cond.size(); ++i)
350 MIB.addReg(Cond[i].getReg());
351
352 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000353}
354
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000355unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000356InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000357 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000358 const SmallVectorImpl<MachineOperand> &Cond,
359 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000360 // Shouldn't be a fall through.
361 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000362
Akira Hatanaka20ada982011-04-01 17:39:08 +0000363 // # of condition operands:
364 // Unconditional branches: 0
365 // Floating point branches: 1 (opc)
366 // Int BranchZero: 2 (opc, reg)
367 // Int Branch: 3 (opc, reg0, reg1)
368 assert((Cond.size() <= 3) &&
369 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000370
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000371 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000372 if (FBB) {
373 BuildCondBr(MBB, TBB, DL, Cond);
374 BuildMI(&MBB, DL, get(Mips::J)).addMBB(FBB);
375 return 2;
376 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000377
Akira Hatanaka20ada982011-04-01 17:39:08 +0000378 // One way branch.
379 // Unconditional branch.
380 if (Cond.empty())
381 BuildMI(&MBB, DL, get(Mips::J)).addMBB(TBB);
382 else // Conditional branch.
383 BuildCondBr(MBB, TBB, DL, Cond);
384 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000385}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000386
387unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000388RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000389{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000390 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
391 MachineBasicBlock::reverse_iterator FirstBr;
392 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000393
Akira Hatanaka20ada982011-04-01 17:39:08 +0000394 // Skip all the debug instructions.
395 while (I != REnd && I->isDebugValue())
396 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000397
Akira Hatanaka20ada982011-04-01 17:39:08 +0000398 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000399
Akira Hatanaka20ada982011-04-01 17:39:08 +0000400 // Up to 2 branches are removed.
401 // Note that indirect branches are not removed.
402 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
403 if (!GetAnalyzableBrOpc(I->getOpcode()))
404 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000405
Akira Hatanaka20ada982011-04-01 17:39:08 +0000406 MBB.erase(I.base(), FirstBr.base());
407
408 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000409}
410
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000411/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000412/// specified Branch instruction.
413bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000414ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000415{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000416 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000417 "Invalid Mips branch condition!");
Akira Hatanaka20ada982011-04-01 17:39:08 +0000418 Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000419 return false;
420}
Dan Gohman99114052009-06-03 20:30:14 +0000421
422/// getGlobalBaseReg - Return a virtual register initialized with the
423/// the global base register value. Output instructions required to
424/// initialize the register in the function entry block, if necessary.
425///
426unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
427 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
428 unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
429 if (GlobalBaseReg != 0)
430 return GlobalBaseReg;
431
432 // Insert the set of GlobalBaseReg into the first MBB of the function
433 MachineBasicBlock &FirstMBB = MF->front();
434 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
435 MachineRegisterInfo &RegInfo = MF->getRegInfo();
436 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
437
438 GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
Jakob Stoklund Olesen3ecf1f02010-07-10 22:43:03 +0000439 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
440 GlobalBaseReg).addReg(Mips::GP);
Dan Gohman99114052009-06-03 20:30:14 +0000441 RegInfo.addLiveIn(Mips::GP);
442
443 MipsFI->setGlobalBaseReg(GlobalBaseReg);
444 return GlobalBaseReg;
445}