blob: ad92d41209e9aa8f4f3a1c019b6503bc9c111acf [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55:11 +00001//===-- MipsInstrInfo.cpp - Mips Instruction Information ------------------===//
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"
Akira Hatanaka794bf172011-07-07 23:56:50 +000015#include "InstPrinter/MipsInstPrinter.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "MipsAnalyzeImmediate.h"
17#include "MipsMachineFunction.h"
18#include "MipsTargetMachine.h"
19#include "llvm/ADT/STLExtras.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman99114052009-06-03 20:30:14 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000023#include "llvm/Support/TargetRegistry.h"
Evan Cheng22fee2d2011-06-28 20:07:07 +000024
Evan Cheng4db3cff2011-07-01 17:57:27 +000025#define GET_INSTRINFO_CTOR
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000026#include "MipsGenInstrInfo.inc"
27
28using namespace llvm;
29
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000030MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm, unsigned UncondBr)
Evan Cheng4db3cff2011-07-01 17:57:27 +000031 : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
Akira Hatanaka85890102012-07-31 23:41:32 +000032 TM(tm), UncondBrOpc(UncondBr) {}
Akira Hatanaka794bf172011-07-07 23:56:50 +000033
Akira Hatanakaaf266262012-08-02 18:21:47 +000034const MipsInstrInfo *MipsInstrInfo::create(MipsTargetMachine &TM) {
35 if (TM.getSubtargetImpl()->inMips16Mode())
36 return llvm::createMips16InstrInfo(TM);
37
38 return llvm::createMipsSEInstrInfo(TM);
39}
40
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000041bool MipsInstrInfo::isZeroImm(const MachineOperand &op) const {
Dan Gohmand735b802008-10-03 15:45:36 +000042 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000043}
44
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000045/// insertNoop - If data hazard condition is found insert the target nop
46/// instruction.
47void MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000048insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000049{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000050 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +000051 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000052}
53
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000054MachineMemOperand *MipsInstrInfo::GetMemOperand(MachineBasicBlock &MBB, int FI,
55 unsigned Flag) const {
Akira Hatanakafd1d9252011-12-24 03:11:18 +000056 MachineFunction &MF = *MBB.getParent();
57 MachineFrameInfo &MFI = *MF.getFrameInfo();
58 unsigned Align = MFI.getObjectAlignment(FI);
Jia Liubb481f82012-02-28 07:46:26 +000059
Akira Hatanakafd1d9252011-12-24 03:11:18 +000060 return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), Flag,
61 MFI.getObjectSize(FI), Align);
62}
63
Akira Hatanakac4f24eb2011-07-01 01:04:43 +000064MachineInstr*
65MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
66 uint64_t Offset, const MDNode *MDPtr,
67 DebugLoc DL) const {
68 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE))
69 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
70 return &*MIB;
71}
72
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000073//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000074// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000075//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000076
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000077void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
78 MachineBasicBlock *&BB,
79 SmallVectorImpl<MachineOperand> &Cond) const {
Akira Hatanaka20ada982011-04-01 17:39:08 +000080 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
81 int NumOp = Inst->getNumExplicitOperands();
Jia Liubb481f82012-02-28 07:46:26 +000082
Akira Hatanaka20ada982011-04-01 17:39:08 +000083 // for both int and fp branches, the last explicit operand is the
84 // MBB.
85 BB = Inst->getOperand(NumOp-1).getMBB();
86 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +000087
Akira Hatanaka20ada982011-04-01 17:39:08 +000088 for (int i=0; i<NumOp-1; i++)
89 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000090}
91
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000092bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000093 MachineBasicBlock *&TBB,
94 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +000095 SmallVectorImpl<MachineOperand> &Cond,
Akira Hatanakad0a4b602013-03-01 01:10:17 +000096 bool AllowModify) const {
97 SmallVector<MachineInstr*, 2> BranchInstrs;
98 BranchType BT = AnalyzeBranch(MBB, TBB, FBB, Cond, AllowModify, BranchInstrs);
Akira Hatanakae6ac7d62012-09-13 17:12:37 +000099
Akira Hatanakad0a4b602013-03-01 01:10:17 +0000100 return (BT == BT_None) || (BT == BT_Indirect);
Jia Liubb481f82012-02-28 07:46:26 +0000101}
102
Akira Hatanaka20ada982011-04-01 17:39:08 +0000103void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
104 MachineBasicBlock *TBB, DebugLoc DL,
105 const SmallVectorImpl<MachineOperand>& Cond)
106 const {
107 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +0000108 const MCInstrDesc &MCID = get(Opc);
109 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000110
Akira Hatanakae6ac7d62012-09-13 17:12:37 +0000111 for (unsigned i = 1; i < Cond.size(); ++i) {
112 if (Cond[i].isReg())
113 MIB.addReg(Cond[i].getReg());
114 else if (Cond[i].isImm())
115 MIB.addImm(Cond[i].getImm());
116 else
117 assert(true && "Cannot copy operand");
118 }
Akira Hatanaka20ada982011-04-01 17:39:08 +0000119 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000120}
121
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000122unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000123InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000124 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000125 const SmallVectorImpl<MachineOperand> &Cond,
126 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000127 // Shouldn't be a fall through.
128 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000129
Akira Hatanaka20ada982011-04-01 17:39:08 +0000130 // # of condition operands:
131 // Unconditional branches: 0
132 // Floating point branches: 1 (opc)
133 // Int BranchZero: 2 (opc, reg)
134 // Int Branch: 3 (opc, reg0, reg1)
135 assert((Cond.size() <= 3) &&
136 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000137
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000138 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000139 if (FBB) {
140 BuildCondBr(MBB, TBB, DL, Cond);
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000141 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000142 return 2;
143 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000144
Akira Hatanaka20ada982011-04-01 17:39:08 +0000145 // One way branch.
146 // Unconditional branch.
147 if (Cond.empty())
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000148 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000149 else // Conditional branch.
150 BuildCondBr(MBB, TBB, DL, Cond);
151 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000152}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000153
154unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000155RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000156{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000157 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
158 MachineBasicBlock::reverse_iterator FirstBr;
159 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000160
Akira Hatanaka20ada982011-04-01 17:39:08 +0000161 // Skip all the debug instructions.
162 while (I != REnd && I->isDebugValue())
163 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000164
Akira Hatanaka20ada982011-04-01 17:39:08 +0000165 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000166
Akira Hatanaka20ada982011-04-01 17:39:08 +0000167 // Up to 2 branches are removed.
168 // Note that indirect branches are not removed.
169 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
170 if (!GetAnalyzableBrOpc(I->getOpcode()))
171 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000172
Akira Hatanaka20ada982011-04-01 17:39:08 +0000173 MBB.erase(I.base(), FirstBr.base());
174
175 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000176}
177
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000178/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000179/// specified Branch instruction.
180bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000181ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000182{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000183 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000184 "Invalid Mips branch condition!");
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +0000185 Cond[0].setImm(GetOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000186 return false;
187}
Dan Gohman99114052009-06-03 20:30:14 +0000188
Akira Hatanakad0a4b602013-03-01 01:10:17 +0000189MipsInstrInfo::BranchType MipsInstrInfo::
190AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
191 MachineBasicBlock *&FBB, SmallVectorImpl<MachineOperand> &Cond,
192 bool AllowModify,
193 SmallVectorImpl<MachineInstr*> &BranchInstrs) const {
194
195 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
196
197 // Skip all the debug instructions.
198 while (I != REnd && I->isDebugValue())
199 ++I;
200
201 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
202 // This block ends with no branches (it just falls through to its succ).
203 // Leave TBB/FBB null.
204 TBB = FBB = NULL;
205 return BT_NoBranch;
206 }
207
208 MachineInstr *LastInst = &*I;
209 unsigned LastOpc = LastInst->getOpcode();
210 BranchInstrs.push_back(LastInst);
211
212 // Not an analyzable branch (e.g., indirect jump).
213 if (!GetAnalyzableBrOpc(LastOpc))
214 return LastInst->isIndirectBranch() ? BT_Indirect : BT_None;
215
216 // Get the second to last instruction in the block.
217 unsigned SecondLastOpc = 0;
218 MachineInstr *SecondLastInst = NULL;
219
220 if (++I != REnd) {
221 SecondLastInst = &*I;
222 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
223
224 // Not an analyzable branch (must be an indirect jump).
225 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
226 return BT_None;
227 }
228
Akira Hatanakad0a4b602013-03-01 01:10:17 +0000229 // If there is only one terminator instruction, process it.
230 if (!SecondLastOpc) {
231 // Unconditional branch
232 if (LastOpc == UncondBrOpc) {
233 TBB = LastInst->getOperand(0).getMBB();
234 return BT_Uncond;
235 }
236
237 // Conditional branch
238 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
239 return BT_Cond;
240 }
241
242 // If we reached here, there are two branches.
243 // If there are three terminators, we don't know what sort of block this is.
244 if (++I != REnd && isUnpredicatedTerminator(&*I))
245 return BT_None;
246
Akira Hatanaka888e8fe2013-03-01 01:22:26 +0000247 BranchInstrs.insert(BranchInstrs.begin(), SecondLastInst);
248
Akira Hatanakad0a4b602013-03-01 01:10:17 +0000249 // If second to last instruction is an unconditional branch,
250 // analyze it and remove the last instruction.
251 if (SecondLastOpc == UncondBrOpc) {
252 // Return if the last instruction cannot be removed.
253 if (!AllowModify)
254 return BT_None;
255
256 TBB = SecondLastInst->getOperand(0).getMBB();
257 LastInst->eraseFromParent();
258 BranchInstrs.pop_back();
259 return BT_Uncond;
260 }
261
262 // Conditional branch followed by an unconditional branch.
263 // The last one must be unconditional.
264 if (LastOpc != UncondBrOpc)
265 return BT_None;
266
267 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
268 FBB = LastInst->getOperand(0).getMBB();
269
270 return BT_CondUncond;
271}
272
Akira Hatanakad4b48b22012-06-14 01:16:45 +0000273/// Return the number of bytes of code the specified instruction may be.
274unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
275 switch (MI->getOpcode()) {
276 default:
277 return MI->getDesc().getSize();
278 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
279 const MachineFunction *MF = MI->getParent()->getParent();
280 const char *AsmStr = MI->getOperand(0).getSymbolName();
281 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
282 }
283 }
284}