blob: 559943a8dbaeee6c37e03b7cb7b26e99aaf890d4 [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"
Torok Edwinc25e7582009-07-11 20:10:48 +000020#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000021#include "llvm/Support/TargetRegistry.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),
Akira Hatanaka43aed322011-10-11 00:37:28 +000031 TM(tm), IsN64(TM.getSubtarget<MipsSubtarget>().isABI_N64()),
32 RI(*TM.getSubtargetImpl(), *this) {}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033
Akira Hatanaka794bf172011-07-07 23:56:50 +000034
35const MipsRegisterInfo &MipsInstrInfo::getRegisterInfo() const {
36 return RI;
37}
38
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000039static bool isZeroImm(const MachineOperand &op) {
Dan Gohmand735b802008-10-03 15:45:36 +000040 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000041}
42
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000043/// isLoadFromStackSlot - If the specified machine instruction is a direct
44/// load from a stack slot, return the virtual or physical register number of
45/// the destination along with the FrameIndex of the loaded stack slot. If
46/// not, return 0. This predicate must return 0 if the instruction has
47/// any side effects other than loading from the stack slot.
48unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000049isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000050{
Akira Hatanaka1acb7df2011-10-11 01:12:52 +000051 unsigned Opc = MI->getOpcode();
52
53 if ((Opc == Mips::LW) || (Opc == Mips::LW_P8) || (Opc == Mips::LD) ||
54 (Opc == Mips::LD_P8) || (Opc == Mips::LWC1) || (Opc == Mips::LWC1_P8) ||
55 (Opc == Mips::LDC1) || (Opc == Mips::LDC164) ||
56 (Opc == Mips::LDC164_P8)) {
Akira Hatanakad3ac47f2011-07-07 18:57:00 +000057 if ((MI->getOperand(1).isFI()) && // is a stack slot
58 (MI->getOperand(2).isImm()) && // the imm is zero
59 (isZeroImm(MI->getOperand(2)))) {
60 FrameIndex = MI->getOperand(1).getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000061 return MI->getOperand(0).getReg();
62 }
63 }
64
65 return 0;
66}
67
68/// isStoreToStackSlot - If the specified machine instruction is a direct
69/// store to a stack slot, return the virtual or physical register number of
70/// the source reg along with the FrameIndex of the loaded stack slot. If
71/// not, return 0. This predicate must return 0 if the instruction has
72/// any side effects other than storing to the stack slot.
73unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000074isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000075{
Akira Hatanaka1acb7df2011-10-11 01:12:52 +000076 unsigned Opc = MI->getOpcode();
77
78 if ((Opc == Mips::SW) || (Opc == Mips::SW_P8) || (Opc == Mips::SD) ||
79 (Opc == Mips::SD_P8) || (Opc == Mips::SWC1) || (Opc == Mips::SWC1_P8) ||
80 (Opc == Mips::SDC1) || (Opc == Mips::SDC164) ||
81 (Opc == Mips::SDC164_P8)) {
Akira Hatanakad3ac47f2011-07-07 18:57:00 +000082 if ((MI->getOperand(1).isFI()) && // is a stack slot
83 (MI->getOperand(2).isImm()) && // the imm is zero
84 (isZeroImm(MI->getOperand(2)))) {
85 FrameIndex = MI->getOperand(1).getIndex();
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +000086 return MI->getOperand(0).getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000087 }
88 }
89 return 0;
90}
91
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000092/// insertNoop - If data hazard condition is found insert the target nop
93/// instruction.
94void MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000095insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000096{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000097 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +000098 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000099}
100
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000101void MipsInstrInfo::
102copyPhysReg(MachineBasicBlock &MBB,
103 MachineBasicBlock::iterator I, DebugLoc DL,
104 unsigned DestReg, unsigned SrcReg,
105 bool KillSrc) const {
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000106 unsigned Opc = 0, ZeroReg = 0;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000107
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000108 if (Mips::CPURegsRegClass.contains(DestReg)) { // Copy to CPU Reg.
109 if (Mips::CPURegsRegClass.contains(SrcReg))
110 Opc = Mips::ADDu, ZeroReg = Mips::ZERO;
111 else if (Mips::CCRRegClass.contains(SrcReg))
112 Opc = Mips::CFC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000113 else if (Mips::FGR32RegClass.contains(SrcReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000114 Opc = Mips::MFC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000115 else if (SrcReg == Mips::HI)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000116 Opc = Mips::MFHI, SrcReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000117 else if (SrcReg == Mips::LO)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000118 Opc = Mips::MFLO, SrcReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000119 }
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000120 else if (Mips::CPURegsRegClass.contains(SrcReg)) { // Copy from CPU Reg.
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000121 if (Mips::CCRRegClass.contains(DestReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000122 Opc = Mips::CTC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000123 else if (Mips::FGR32RegClass.contains(DestReg))
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000124 Opc = Mips::MTC1;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000125 else if (DestReg == Mips::HI)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000126 Opc = Mips::MTHI, DestReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000127 else if (DestReg == Mips::LO)
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000128 Opc = Mips::MTLO, DestReg = 0;
129 }
130 else if (Mips::FGR32RegClass.contains(DestReg, SrcReg))
Akira Hatanaka4391bb72011-10-08 03:50:18 +0000131 Opc = Mips::FMOV_S;
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000132 else if (Mips::AFGR64RegClass.contains(DestReg, SrcReg))
133 Opc = Mips::FMOV_D32;
134 else if (Mips::CCRRegClass.contains(DestReg, SrcReg))
135 Opc = Mips::MOVCCRToCCR;
136 else if (Mips::CPU64RegsRegClass.contains(DestReg)) { // Copy to CPU64 Reg.
137 if (Mips::CPU64RegsRegClass.contains(SrcReg))
138 Opc = Mips::DADDu, ZeroReg = Mips::ZERO_64;
139 else if (SrcReg == Mips::HI64)
140 Opc = Mips::MFHI64, SrcReg = 0;
141 else if (SrcReg == Mips::LO64)
142 Opc = Mips::MFLO64, SrcReg = 0;
143 }
144 else if (Mips::CPU64RegsRegClass.contains(SrcReg)) { // Copy from CPU64 Reg.
145 if (DestReg == Mips::HI64)
146 Opc = Mips::MTHI64, DestReg = 0;
147 else if (DestReg == Mips::LO64)
148 Opc = Mips::MTLO64, DestReg = 0;
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000149 }
150
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000151 assert(Opc && "Cannot copy registers");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000152
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000153 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
154
155 if (DestReg)
156 MIB.addReg(DestReg, RegState::Define);
Jakob Stoklund Olesen273c14f2010-07-11 01:08:31 +0000157
Akira Hatanaka2ad76682011-10-03 20:38:08 +0000158 if (ZeroReg)
159 MIB.addReg(ZeroReg);
160
161 if (SrcReg)
162 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000163}
164
165void MipsInstrInfo::
166storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000167 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000168 const TargetRegisterClass *RC,
169 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000170 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000171 if (I != MBB.end()) DL = I->getDebugLoc();
Akira Hatanaka43aed322011-10-11 00:37:28 +0000172 unsigned Opc = 0;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000173
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000174 if (RC == Mips::CPURegsRegisterClass)
Akira Hatanaka43aed322011-10-11 00:37:28 +0000175 Opc = IsN64 ? Mips::SW_P8 : Mips::SW;
176 else if (RC == Mips::CPU64RegsRegisterClass)
177 Opc = IsN64 ? Mips::SD_P8 : Mips::SD;
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000178 else if (RC == Mips::FGR32RegisterClass)
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000179 Opc = IsN64 ? Mips::SWC1_P8 : Mips::SWC1;
Akira Hatanaka43aed322011-10-11 00:37:28 +0000180 else if (RC == Mips::AFGR64RegisterClass)
181 Opc = Mips::SDC1;
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000182 else if (RC == Mips::FGR64RegisterClass)
183 Opc = IsN64 ? Mips::SDC164_P8 : Mips::SDC164;
Akira Hatanaka43aed322011-10-11 00:37:28 +0000184
185 assert(Opc && "Register class not handled!");
186 BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
187 .addFrameIndex(FI).addImm(0);
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();
Akira Hatanaka43aed322011-10-11 00:37:28 +0000198 unsigned Opc = 0;
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000199
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000200 if (RC == Mips::CPURegsRegisterClass)
Akira Hatanaka43aed322011-10-11 00:37:28 +0000201 Opc = IsN64 ? Mips::LW_P8 : Mips::LW;
202 else if (RC == Mips::CPU64RegsRegisterClass)
203 Opc = IsN64 ? Mips::LD_P8 : Mips::LD;
Bruno Cardoso Lopes302525b2009-11-25 00:36:00 +0000204 else if (RC == Mips::FGR32RegisterClass)
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000205 Opc = IsN64 ? Mips::LWC1_P8 : Mips::LWC1;
Akira Hatanaka43aed322011-10-11 00:37:28 +0000206 else if (RC == Mips::AFGR64RegisterClass)
207 Opc = Mips::LDC1;
Akira Hatanaka1acb7df2011-10-11 01:12:52 +0000208 else if (RC == Mips::FGR64RegisterClass)
209 Opc = IsN64 ? Mips::LDC164_P8 : Mips::LDC164;
Akira Hatanaka43aed322011-10-11 00:37:28 +0000210
211 assert(Opc && "Register class not handled!");
212 BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(0);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000213}
214
Akira Hatanakac4f24eb2011-07-01 01:04:43 +0000215MachineInstr*
216MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
217 uint64_t Offset, const MDNode *MDPtr,
218 DebugLoc DL) const {
219 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE))
220 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
221 return &*MIB;
222}
223
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000224//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000225// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000226//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000227
Akira Hatanaka20ada982011-04-01 17:39:08 +0000228static unsigned GetAnalyzableBrOpc(unsigned Opc) {
Akira Hatanaka3e3427a2011-10-11 18:49:17 +0000229 return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ ||
230 Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ ||
231 Opc == Mips::BEQ64 || Opc == Mips::BNE64 || Opc == Mips::BGTZ64 ||
232 Opc == Mips::BGEZ64 || Opc == Mips::BLTZ64 || Opc == Mips::BLEZ64 ||
233 Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::J) ?
234 Opc : 0;
Akira Hatanaka20ada982011-04-01 17:39:08 +0000235}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000236
Akira Hatanaka20ada982011-04-01 17:39:08 +0000237/// GetOppositeBranchOpc - Return the inverse of the specified
238/// opcode, e.g. turning BEQ to BNE.
239unsigned Mips::GetOppositeBranchOpc(unsigned Opc)
240{
241 switch (Opc) {
242 default: llvm_unreachable("Illegal opcode!");
Akira Hatanaka3e3427a2011-10-11 18:49:17 +0000243 case Mips::BEQ : return Mips::BNE;
244 case Mips::BNE : return Mips::BEQ;
245 case Mips::BGTZ : return Mips::BLEZ;
246 case Mips::BGEZ : return Mips::BLTZ;
247 case Mips::BLTZ : return Mips::BGEZ;
248 case Mips::BLEZ : return Mips::BGTZ;
249 case Mips::BEQ64 : return Mips::BNE64;
250 case Mips::BNE64 : return Mips::BEQ64;
251 case Mips::BGTZ64 : return Mips::BLEZ64;
252 case Mips::BGEZ64 : return Mips::BLTZ64;
253 case Mips::BLTZ64 : return Mips::BGEZ64;
254 case Mips::BLEZ64 : return Mips::BGTZ64;
255 case Mips::BC1T : return Mips::BC1F;
256 case Mips::BC1F : return Mips::BC1T;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000257 }
258}
259
Akira Hatanaka20ada982011-04-01 17:39:08 +0000260static void AnalyzeCondBr(const MachineInstr* Inst, unsigned Opc,
261 MachineBasicBlock *&BB,
262 SmallVectorImpl<MachineOperand>& Cond) {
263 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
264 int NumOp = Inst->getNumExplicitOperands();
265
266 // for both int and fp branches, the last explicit operand is the
267 // MBB.
268 BB = Inst->getOperand(NumOp-1).getMBB();
269 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000270
Akira Hatanaka20ada982011-04-01 17:39:08 +0000271 for (int i=0; i<NumOp-1; i++)
272 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000273}
274
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000275bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000276 MachineBasicBlock *&TBB,
277 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000278 SmallVectorImpl<MachineOperand> &Cond,
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000279 bool AllowModify) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000280{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000281 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000282
Akira Hatanaka20ada982011-04-01 17:39:08 +0000283 // Skip all the debug instructions.
284 while (I != REnd && I->isDebugValue())
285 ++I;
286
287 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
288 // If this block ends with no branches (it just falls through to its succ)
289 // just return false, leaving TBB/FBB null.
290 TBB = FBB = NULL;
291 return false;
292 }
293
294 MachineInstr *LastInst = &*I;
295 unsigned LastOpc = LastInst->getOpcode();
296
297 // Not an analyzable branch (must be an indirect jump).
298 if (!GetAnalyzableBrOpc(LastOpc))
299 return true;
300
301 // Get the second to last instruction in the block.
302 unsigned SecondLastOpc = 0;
303 MachineInstr *SecondLastInst = NULL;
304
305 if (++I != REnd) {
306 SecondLastInst = &*I;
307 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
308
309 // Not an analyzable branch (must be an indirect jump).
310 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
311 return true;
312 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000313
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000314 // If there is only one terminator instruction, process it.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000315 if (!SecondLastOpc) {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000316 // Unconditional branch
317 if (LastOpc == Mips::J) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000318 TBB = LastInst->getOperand(0).getMBB();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000319 return false;
320 }
321
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000322 // Conditional branch
Akira Hatanaka20ada982011-04-01 17:39:08 +0000323 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
324 return false;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000325 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000326
Akira Hatanaka20ada982011-04-01 17:39:08 +0000327 // If we reached here, there are two branches.
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000328 // If there are three terminators, we don't know what sort of block this is.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000329 if (++I != REnd && isUnpredicatedTerminator(&*I))
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000330 return true;
331
Akira Hatanaka20ada982011-04-01 17:39:08 +0000332 // If second to last instruction is an unconditional branch,
333 // analyze it and remove the last instruction.
334 if (SecondLastOpc == Mips::J) {
335 // Return if the last instruction cannot be removed.
336 if (!AllowModify)
337 return true;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000338
Chris Lattner8aa797a2007-12-30 23:10:15 +0000339 TBB = SecondLastInst->getOperand(0).getMBB();
Akira Hatanaka20ada982011-04-01 17:39:08 +0000340 LastInst->eraseFromParent();
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000341 return false;
342 }
343
Akira Hatanaka20ada982011-04-01 17:39:08 +0000344 // Conditional branch followed by an unconditional branch.
345 // The last one must be unconditional.
346 if (LastOpc != Mips::J)
347 return true;
348
349 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
350 FBB = LastInst->getOperand(0).getMBB();
351
352 return false;
353}
354
355void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
356 MachineBasicBlock *TBB, DebugLoc DL,
357 const SmallVectorImpl<MachineOperand>& Cond)
358 const {
359 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000360 const MCInstrDesc &MCID = get(Opc);
361 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000362
363 for (unsigned i = 1; i < Cond.size(); ++i)
364 MIB.addReg(Cond[i].getReg());
365
366 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000367}
368
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000369unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000370InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000371 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000372 const SmallVectorImpl<MachineOperand> &Cond,
373 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000374 // Shouldn't be a fall through.
375 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000376
Akira Hatanaka20ada982011-04-01 17:39:08 +0000377 // # of condition operands:
378 // Unconditional branches: 0
379 // Floating point branches: 1 (opc)
380 // Int BranchZero: 2 (opc, reg)
381 // Int Branch: 3 (opc, reg0, reg1)
382 assert((Cond.size() <= 3) &&
383 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000384
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000385 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000386 if (FBB) {
387 BuildCondBr(MBB, TBB, DL, Cond);
388 BuildMI(&MBB, DL, get(Mips::J)).addMBB(FBB);
389 return 2;
390 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000391
Akira Hatanaka20ada982011-04-01 17:39:08 +0000392 // One way branch.
393 // Unconditional branch.
394 if (Cond.empty())
395 BuildMI(&MBB, DL, get(Mips::J)).addMBB(TBB);
396 else // Conditional branch.
397 BuildCondBr(MBB, TBB, DL, Cond);
398 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000399}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000400
401unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000402RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000403{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000404 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
405 MachineBasicBlock::reverse_iterator FirstBr;
406 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000407
Akira Hatanaka20ada982011-04-01 17:39:08 +0000408 // Skip all the debug instructions.
409 while (I != REnd && I->isDebugValue())
410 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000411
Akira Hatanaka20ada982011-04-01 17:39:08 +0000412 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000413
Akira Hatanaka20ada982011-04-01 17:39:08 +0000414 // Up to 2 branches are removed.
415 // Note that indirect branches are not removed.
416 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
417 if (!GetAnalyzableBrOpc(I->getOpcode()))
418 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000419
Akira Hatanaka20ada982011-04-01 17:39:08 +0000420 MBB.erase(I.base(), FirstBr.base());
421
422 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000423}
424
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000425/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000426/// specified Branch instruction.
427bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000428ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000429{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000430 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000431 "Invalid Mips branch condition!");
Akira Hatanaka20ada982011-04-01 17:39:08 +0000432 Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000433 return false;
434}
Dan Gohman99114052009-06-03 20:30:14 +0000435
436/// getGlobalBaseReg - Return a virtual register initialized with the
437/// the global base register value. Output instructions required to
438/// initialize the register in the function entry block, if necessary.
439///
440unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
441 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
442 unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
443 if (GlobalBaseReg != 0)
444 return GlobalBaseReg;
445
446 // Insert the set of GlobalBaseReg into the first MBB of the function
447 MachineBasicBlock &FirstMBB = MF->front();
448 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
449 MachineRegisterInfo &RegInfo = MF->getRegInfo();
450 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
451
452 GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
Jakob Stoklund Olesen3ecf1f02010-07-10 22:43:03 +0000453 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
454 GlobalBaseReg).addReg(Mips::GP);
Dan Gohman99114052009-06-03 20:30:14 +0000455 RegInfo.addLiveIn(Mips::GP);
456
457 MipsFI->setGlobalBaseReg(GlobalBaseReg);
458 return GlobalBaseReg;
459}