blob: d04cc3b066f5639a71980e42ce78e6e2eb18ba1f [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"
Akira Hatanaka794bf172011-07-07 23:56:50 +000017#include "InstPrinter/MipsInstPrinter.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"
Evan Cheng59ee62d2011-07-11 03:57:24 +000020#include "llvm/Target/TargetRegistry.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000022#include "llvm/ADT/STLExtras.h"
Evan Cheng22fee2d2011-06-28 20:07:07 +000023
Evan Cheng4db3cff2011-07-01 17:57:27 +000024#define GET_INSTRINFO_CTOR
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000025#include "MipsGenInstrInfo.inc"
26
27using namespace llvm;
28
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000029MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
Evan Cheng4db3cff2011-07-01 17:57:27 +000030 : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000031 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032
Akira Hatanaka794bf172011-07-07 23:56:50 +000033
34const MipsRegisterInfo &MipsInstrInfo::getRegisterInfo() const {
35 return RI;
36}
37
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000038static bool isZeroImm(const MachineOperand &op) {
Dan Gohmand735b802008-10-03 15:45:36 +000039 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000040}
41
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000042/// isLoadFromStackSlot - If the specified machine instruction is a direct
43/// load from a stack slot, return the virtual or physical register number of
44/// the destination along with the FrameIndex of the loaded stack slot. If
45/// not, return 0. This predicate must return 0 if the instruction has
46/// any side effects other than loading from the stack slot.
47unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000048isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000049{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000050 if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000051 (MI->getOpcode() == Mips::LDC1)) {
Akira Hatanakad3ac47f2011-07-07 18:57:00 +000052 if ((MI->getOperand(1).isFI()) && // is a stack slot
53 (MI->getOperand(2).isImm()) && // the imm is zero
54 (isZeroImm(MI->getOperand(2)))) {
55 FrameIndex = MI->getOperand(1).getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000056 return MI->getOperand(0).getReg();
57 }
58 }
59
60 return 0;
61}
62
63/// isStoreToStackSlot - If the specified machine instruction is a direct
64/// store to a stack slot, return the virtual or physical register number of
65/// the source reg along with the FrameIndex of the loaded stack slot. If
66/// not, return 0. This predicate must return 0 if the instruction has
67/// any side effects other than storing to the stack slot.
68unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000069isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000070{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000071 if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000072 (MI->getOpcode() == Mips::SDC1)) {
Akira Hatanakad3ac47f2011-07-07 18:57:00 +000073 if ((MI->getOperand(1).isFI()) && // is a stack slot
74 (MI->getOperand(2).isImm()) && // the imm is zero
75 (isZeroImm(MI->getOperand(2)))) {
76 FrameIndex = MI->getOperand(1).getIndex();
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +000077 return MI->getOperand(0).getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000078 }
79 }
80 return 0;
81}
82
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000083/// insertNoop - If data hazard condition is found insert the target nop
84/// instruction.
85void MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000086insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000087{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000088 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +000089 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000090}
91
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +000092void MipsInstrInfo::
93copyPhysReg(MachineBasicBlock &MBB,
94 MachineBasicBlock::iterator I, DebugLoc DL,
95 unsigned DestReg, unsigned SrcReg,
96 bool KillSrc) const {
97 bool DestCPU = Mips::CPURegsRegClass.contains(DestReg);
98 bool SrcCPU = Mips::CPURegsRegClass.contains(SrcReg);
Bill Wendlingd1c321a2009-02-12 00:02:55 +000099
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000100 // CPU-CPU is the most common.
101 if (DestCPU && SrcCPU) {
102 BuildMI(MBB, I, DL, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
103 .addReg(SrcReg, getKillRegState(KillSrc));
104 return;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000105 }
106
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000107 // Copy to CPU from other registers.
108 if (DestCPU) {
109 if (Mips::CCRRegClass.contains(SrcReg))
110 BuildMI(MBB, I, DL, get(Mips::CFC1), DestReg)
111 .addReg(SrcReg, getKillRegState(KillSrc));
112 else if (Mips::FGR32RegClass.contains(SrcReg))
113 BuildMI(MBB, I, DL, get(Mips::MFC1), DestReg)
114 .addReg(SrcReg, getKillRegState(KillSrc));
115 else if (SrcReg == Mips::HI)
116 BuildMI(MBB, I, DL, get(Mips::MFHI), DestReg);
117 else if (SrcReg == Mips::LO)
118 BuildMI(MBB, I, DL, get(Mips::MFLO), DestReg);
119 else
120 llvm_unreachable("Copy to CPU from invalid register");
121 return;
122 }
123
124 // Copy to other registers from CPU.
125 if (SrcCPU) {
126 if (Mips::CCRRegClass.contains(DestReg))
127 BuildMI(MBB, I, DL, get(Mips::CTC1), DestReg)
128 .addReg(SrcReg, getKillRegState(KillSrc));
129 else if (Mips::FGR32RegClass.contains(DestReg))
130 BuildMI(MBB, I, DL, get(Mips::MTC1), DestReg)
131 .addReg(SrcReg, getKillRegState(KillSrc));
132 else if (DestReg == Mips::HI)
133 BuildMI(MBB, I, DL, get(Mips::MTHI))
134 .addReg(SrcReg, getKillRegState(KillSrc));
135 else if (DestReg == Mips::LO)
136 BuildMI(MBB, I, DL, get(Mips::MTLO))
137 .addReg(SrcReg, getKillRegState(KillSrc));
138 else
139 llvm_unreachable("Copy from CPU to invalid register");
140 return;
141 }
142
143 if (Mips::FGR32RegClass.contains(DestReg, SrcReg)) {
144 BuildMI(MBB, I, DL, get(Mips::FMOV_S32), DestReg)
145 .addReg(SrcReg, getKillRegState(KillSrc));
146 return;
147 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000148
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000149 if (Mips::AFGR64RegClass.contains(DestReg, SrcReg)) {
150 BuildMI(MBB, I, DL, get(Mips::FMOV_D32), DestReg)
151 .addReg(SrcReg, getKillRegState(KillSrc));
152 return;
153 }
154
155 if (Mips::CCRRegClass.contains(DestReg, SrcReg)) {
156 BuildMI(MBB, I, DL, get(Mips::MOVCCRToCCR), DestReg)
157 .addReg(SrcReg, getKillRegState(KillSrc));
158 return;
159 }
160 llvm_unreachable("Cannot copy registers");
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000161}
162
163void MipsInstrInfo::
164storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000165 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000166 const TargetRegisterClass *RC,
167 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000168 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000169 if (I != MBB.end()) DL = I->getDebugLoc();
170
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000171 if (RC == Mips::CPURegsRegisterClass)
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000172 BuildMI(MBB, I, DL, get(Mips::SW)).addReg(SrcReg, getKillRegState(isKill))
Akira Hatanakad3ac47f2011-07-07 18:57:00 +0000173 .addFrameIndex(FI).addImm(0);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000174 else if (RC == Mips::FGR32RegisterClass)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000175 BuildMI(MBB, I, DL, get(Mips::SWC1)).addReg(SrcReg, getKillRegState(isKill))
Akira Hatanakad3ac47f2011-07-07 18:57:00 +0000176 .addFrameIndex(FI).addImm(0);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000177 else if (RC == Mips::AFGR64RegisterClass) {
Akira Hatanaka614051a2011-08-16 03:51:51 +0000178 BuildMI(MBB, I, DL, get(Mips::SDC1))
179 .addReg(SrcReg, getKillRegState(isKill))
180 .addFrameIndex(FI).addImm(0);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000181 } else
182 llvm_unreachable("Register class not handled!");
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000183}
184
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000185void MipsInstrInfo::
186loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
187 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000188 const TargetRegisterClass *RC,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000189 const TargetRegisterInfo *TRI) const
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000190{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000191 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000192 if (I != MBB.end()) DL = I->getDebugLoc();
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000193
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000194 if (RC == Mips::CPURegsRegisterClass)
Akira Hatanakad3ac47f2011-07-07 18:57:00 +0000195 BuildMI(MBB, I, DL, get(Mips::LW), DestReg).addFrameIndex(FI).addImm(0);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000196 else if (RC == Mips::FGR32RegisterClass)
Akira Hatanakad3ac47f2011-07-07 18:57:00 +0000197 BuildMI(MBB, I, DL, get(Mips::LWC1), DestReg).addFrameIndex(FI).addImm(0);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000198 else if (RC == Mips::AFGR64RegisterClass) {
Akira Hatanaka614051a2011-08-16 03:51:51 +0000199 BuildMI(MBB, I, DL, get(Mips::LDC1), DestReg).addFrameIndex(FI).addImm(0);
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000200 } else
201 llvm_unreachable("Register class not handled!");
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000202}
203
Akira Hatanakac4f24eb2011-07-01 01:04:43 +0000204MachineInstr*
205MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
206 uint64_t Offset, const MDNode *MDPtr,
207 DebugLoc DL) const {
208 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE))
209 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
210 return &*MIB;
211}
212
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000213//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000214// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000215//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000216
Akira Hatanaka20ada982011-04-01 17:39:08 +0000217static unsigned GetAnalyzableBrOpc(unsigned Opc) {
218 return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ ||
219 Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ ||
220 Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::J) ? Opc : 0;
221}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000222
Akira Hatanaka20ada982011-04-01 17:39:08 +0000223/// GetOppositeBranchOpc - Return the inverse of the specified
224/// opcode, e.g. turning BEQ to BNE.
225unsigned Mips::GetOppositeBranchOpc(unsigned Opc)
226{
227 switch (Opc) {
228 default: llvm_unreachable("Illegal opcode!");
229 case Mips::BEQ : return Mips::BNE;
230 case Mips::BNE : return Mips::BEQ;
231 case Mips::BGTZ : return Mips::BLEZ;
232 case Mips::BGEZ : return Mips::BLTZ;
233 case Mips::BLTZ : return Mips::BGEZ;
234 case Mips::BLEZ : return Mips::BGTZ;
235 case Mips::BC1T : return Mips::BC1F;
236 case Mips::BC1F : return Mips::BC1T;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000237 }
238}
239
Akira Hatanaka20ada982011-04-01 17:39:08 +0000240static void AnalyzeCondBr(const MachineInstr* Inst, unsigned Opc,
241 MachineBasicBlock *&BB,
242 SmallVectorImpl<MachineOperand>& Cond) {
243 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
244 int NumOp = Inst->getNumExplicitOperands();
245
246 // for both int and fp branches, the last explicit operand is the
247 // MBB.
248 BB = Inst->getOperand(NumOp-1).getMBB();
249 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000250
Akira Hatanaka20ada982011-04-01 17:39:08 +0000251 for (int i=0; i<NumOp-1; i++)
252 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000253}
254
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000255bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000256 MachineBasicBlock *&TBB,
257 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000258 SmallVectorImpl<MachineOperand> &Cond,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000259 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000260{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000261 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000262
Akira Hatanaka20ada982011-04-01 17:39:08 +0000263 // Skip all the debug instructions.
264 while (I != REnd && I->isDebugValue())
265 ++I;
266
267 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
268 // If this block ends with no branches (it just falls through to its succ)
269 // just return false, leaving TBB/FBB null.
270 TBB = FBB = NULL;
271 return false;
272 }
273
274 MachineInstr *LastInst = &*I;
275 unsigned LastOpc = LastInst->getOpcode();
276
277 // Not an analyzable branch (must be an indirect jump).
278 if (!GetAnalyzableBrOpc(LastOpc))
279 return true;
280
281 // Get the second to last instruction in the block.
282 unsigned SecondLastOpc = 0;
283 MachineInstr *SecondLastInst = NULL;
284
285 if (++I != REnd) {
286 SecondLastInst = &*I;
287 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
288
289 // Not an analyzable branch (must be an indirect jump).
290 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
291 return true;
292 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000293
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000294 // If there is only one terminator instruction, process it.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000295 if (!SecondLastOpc) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000296 // Unconditional branch
297 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000298 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000299 return false;
300 }
301
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000302 // Conditional branch
Akira Hatanaka20ada982011-04-01 17:39:08 +0000303 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
304 return false;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000305 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000306
Akira Hatanaka20ada982011-04-01 17:39:08 +0000307 // If we reached here, there are two branches.
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000308 // If there are three terminators, we don't know what sort of block this is.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000309 if (++I != REnd && isUnpredicatedTerminator(&*I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000310 return true;
311
Akira Hatanaka20ada982011-04-01 17:39:08 +0000312 // If second to last instruction is an unconditional branch,
313 // analyze it and remove the last instruction.
314 if (SecondLastOpc == Mips::J) {
315 // Return if the last instruction cannot be removed.
316 if (!AllowModify)
317 return true;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000318
Chris Lattner8aa797a2007-12-30 23:10:15 +0000319 TBB = SecondLastInst->getOperand(0).getMBB();
Akira Hatanaka20ada982011-04-01 17:39:08 +0000320 LastInst->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000321 return false;
322 }
323
Akira Hatanaka20ada982011-04-01 17:39:08 +0000324 // Conditional branch followed by an unconditional branch.
325 // The last one must be unconditional.
326 if (LastOpc != Mips::J)
327 return true;
328
329 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
330 FBB = LastInst->getOperand(0).getMBB();
331
332 return false;
333}
334
335void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
336 MachineBasicBlock *TBB, DebugLoc DL,
337 const SmallVectorImpl<MachineOperand>& Cond)
338 const {
339 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000340 const MCInstrDesc &MCID = get(Opc);
341 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000342
343 for (unsigned i = 1; i < Cond.size(); ++i)
344 MIB.addReg(Cond[i].getReg());
345
346 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000347}
348
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000349unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000350InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000351 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000352 const SmallVectorImpl<MachineOperand> &Cond,
353 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000354 // Shouldn't be a fall through.
355 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000356
Akira Hatanaka20ada982011-04-01 17:39:08 +0000357 // # of condition operands:
358 // Unconditional branches: 0
359 // Floating point branches: 1 (opc)
360 // Int BranchZero: 2 (opc, reg)
361 // Int Branch: 3 (opc, reg0, reg1)
362 assert((Cond.size() <= 3) &&
363 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000364
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000365 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000366 if (FBB) {
367 BuildCondBr(MBB, TBB, DL, Cond);
368 BuildMI(&MBB, DL, get(Mips::J)).addMBB(FBB);
369 return 2;
370 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000371
Akira Hatanaka20ada982011-04-01 17:39:08 +0000372 // One way branch.
373 // Unconditional branch.
374 if (Cond.empty())
375 BuildMI(&MBB, DL, get(Mips::J)).addMBB(TBB);
376 else // Conditional branch.
377 BuildCondBr(MBB, TBB, DL, Cond);
378 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000379}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000380
381unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000382RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000383{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000384 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
385 MachineBasicBlock::reverse_iterator FirstBr;
386 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000387
Akira Hatanaka20ada982011-04-01 17:39:08 +0000388 // Skip all the debug instructions.
389 while (I != REnd && I->isDebugValue())
390 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000391
Akira Hatanaka20ada982011-04-01 17:39:08 +0000392 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000393
Akira Hatanaka20ada982011-04-01 17:39:08 +0000394 // Up to 2 branches are removed.
395 // Note that indirect branches are not removed.
396 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
397 if (!GetAnalyzableBrOpc(I->getOpcode()))
398 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000399
Akira Hatanaka20ada982011-04-01 17:39:08 +0000400 MBB.erase(I.base(), FirstBr.base());
401
402 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000403}
404
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000405/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000406/// specified Branch instruction.
407bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000408ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000409{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000410 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000411 "Invalid Mips branch condition!");
Akira Hatanaka20ada982011-04-01 17:39:08 +0000412 Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000413 return false;
414}
Dan Gohman99114052009-06-03 20:30:14 +0000415
416/// getGlobalBaseReg - Return a virtual register initialized with the
417/// the global base register value. Output instructions required to
418/// initialize the register in the function entry block, if necessary.
419///
420unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
421 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
422 unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
423 if (GlobalBaseReg != 0)
424 return GlobalBaseReg;
425
426 // Insert the set of GlobalBaseReg into the first MBB of the function
427 MachineBasicBlock &FirstMBB = MF->front();
428 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
429 MachineRegisterInfo &RegInfo = MF->getRegInfo();
430 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
431
432 GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
Jakob Stoklund Olesen3ecf1f02010-07-10 22:43:03 +0000433 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
434 GlobalBaseReg).addReg(Mips::GP);
Dan Gohman99114052009-06-03 20:30:14 +0000435 RegInfo.addLiveIn(Mips::GP);
436
437 MipsFI->setGlobalBaseReg(GlobalBaseReg);
438 return GlobalBaseReg;
439}