blob: eae05a38af6189bf0dfde8dd48dced82e30483eb [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 Hatanaka4552c9a2011-04-15 21:51:11 +000064//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000065// Branch Analysis
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000066//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000067
Akira Hatanaka0bc1adb2012-07-31 21:49:49 +000068void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
69 MachineBasicBlock *&BB,
70 SmallVectorImpl<MachineOperand> &Cond) const {
Akira Hatanaka6daba282013-05-13 17:43:19 +000071 assert(getAnalyzableBrOpc(Opc) && "Not an analyzable branch");
Akira Hatanaka20ada982011-04-01 17:39:08 +000072 int NumOp = Inst->getNumExplicitOperands();
Jia Liubb481f82012-02-28 07:46:26 +000073
Akira Hatanaka20ada982011-04-01 17:39:08 +000074 // for both int and fp branches, the last explicit operand is the
75 // MBB.
76 BB = Inst->getOperand(NumOp-1).getMBB();
77 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +000078
Akira Hatanaka20ada982011-04-01 17:39:08 +000079 for (int i=0; i<NumOp-1; i++)
80 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000081}
82
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000083bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +000084 MachineBasicBlock *&TBB,
85 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +000086 SmallVectorImpl<MachineOperand> &Cond,
Akira Hatanakad0a4b602013-03-01 01:10:17 +000087 bool AllowModify) const {
88 SmallVector<MachineInstr*, 2> BranchInstrs;
89 BranchType BT = AnalyzeBranch(MBB, TBB, FBB, Cond, AllowModify, BranchInstrs);
Akira Hatanakae6ac7d62012-09-13 17:12:37 +000090
Akira Hatanakad0a4b602013-03-01 01:10:17 +000091 return (BT == BT_None) || (BT == BT_Indirect);
Jia Liubb481f82012-02-28 07:46:26 +000092}
93
Akira Hatanaka20ada982011-04-01 17:39:08 +000094void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
95 MachineBasicBlock *TBB, DebugLoc DL,
96 const SmallVectorImpl<MachineOperand>& Cond)
97 const {
98 unsigned Opc = Cond[0].getImm();
Evan Chenge837dea2011-06-28 19:10:37 +000099 const MCInstrDesc &MCID = get(Opc);
100 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000101
Akira Hatanakae6ac7d62012-09-13 17:12:37 +0000102 for (unsigned i = 1; i < Cond.size(); ++i) {
103 if (Cond[i].isReg())
104 MIB.addReg(Cond[i].getReg());
105 else if (Cond[i].isImm())
106 MIB.addImm(Cond[i].getImm());
107 else
108 assert(true && "Cannot copy operand");
109 }
Akira Hatanaka20ada982011-04-01 17:39:08 +0000110 MIB.addMBB(TBB);
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000111}
112
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000113unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000114InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000115 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000116 const SmallVectorImpl<MachineOperand> &Cond,
117 DebugLoc DL) const {
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000118 // Shouldn't be a fall through.
119 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000120
Akira Hatanaka20ada982011-04-01 17:39:08 +0000121 // # of condition operands:
122 // Unconditional branches: 0
123 // Floating point branches: 1 (opc)
124 // Int BranchZero: 2 (opc, reg)
125 // Int Branch: 3 (opc, reg0, reg1)
126 assert((Cond.size() <= 3) &&
127 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000128
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000129 // Two-way Conditional branch.
Akira Hatanaka20ada982011-04-01 17:39:08 +0000130 if (FBB) {
131 BuildCondBr(MBB, TBB, DL, Cond);
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000132 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000133 return 2;
134 }
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000135
Akira Hatanaka20ada982011-04-01 17:39:08 +0000136 // One way branch.
137 // Unconditional branch.
138 if (Cond.empty())
Akira Hatanaka6e55ff52011-12-12 22:39:35 +0000139 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
Akira Hatanaka20ada982011-04-01 17:39:08 +0000140 else // Conditional branch.
141 BuildCondBr(MBB, TBB, DL, Cond);
142 return 1;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000143}
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000144
145unsigned MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000146RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000147{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000148 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
149 MachineBasicBlock::reverse_iterator FirstBr;
150 unsigned removed;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000151
Akira Hatanaka20ada982011-04-01 17:39:08 +0000152 // Skip all the debug instructions.
153 while (I != REnd && I->isDebugValue())
154 ++I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000155
Akira Hatanaka20ada982011-04-01 17:39:08 +0000156 FirstBr = I;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000157
Akira Hatanaka20ada982011-04-01 17:39:08 +0000158 // Up to 2 branches are removed.
159 // Note that indirect branches are not removed.
160 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
Akira Hatanaka6daba282013-05-13 17:43:19 +0000161 if (!getAnalyzableBrOpc(I->getOpcode()))
Akira Hatanaka20ada982011-04-01 17:39:08 +0000162 break;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000163
Akira Hatanaka20ada982011-04-01 17:39:08 +0000164 MBB.erase(I.base(), FirstBr.base());
165
166 return removed;
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000167}
168
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000169/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000170/// specified Branch instruction.
171bool MipsInstrInfo::
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000172ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000173{
Akira Hatanaka20ada982011-04-01 17:39:08 +0000174 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000175 "Invalid Mips branch condition!");
Akira Hatanaka6daba282013-05-13 17:43:19 +0000176 Cond[0].setImm(getOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes35d2a472007-08-18 01:56:48 +0000177 return false;
178}
Dan Gohman99114052009-06-03 20:30:14 +0000179
Akira Hatanakad0a4b602013-03-01 01:10:17 +0000180MipsInstrInfo::BranchType MipsInstrInfo::
181AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
182 MachineBasicBlock *&FBB, SmallVectorImpl<MachineOperand> &Cond,
183 bool AllowModify,
184 SmallVectorImpl<MachineInstr*> &BranchInstrs) const {
185
186 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
187
188 // Skip all the debug instructions.
189 while (I != REnd && I->isDebugValue())
190 ++I;
191
192 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
193 // This block ends with no branches (it just falls through to its succ).
194 // Leave TBB/FBB null.
195 TBB = FBB = NULL;
196 return BT_NoBranch;
197 }
198
199 MachineInstr *LastInst = &*I;
200 unsigned LastOpc = LastInst->getOpcode();
201 BranchInstrs.push_back(LastInst);
202
203 // Not an analyzable branch (e.g., indirect jump).
Akira Hatanaka6daba282013-05-13 17:43:19 +0000204 if (!getAnalyzableBrOpc(LastOpc))
Akira Hatanakad0a4b602013-03-01 01:10:17 +0000205 return LastInst->isIndirectBranch() ? BT_Indirect : BT_None;
206
207 // Get the second to last instruction in the block.
208 unsigned SecondLastOpc = 0;
209 MachineInstr *SecondLastInst = NULL;
210
211 if (++I != REnd) {
212 SecondLastInst = &*I;
Akira Hatanaka6daba282013-05-13 17:43:19 +0000213 SecondLastOpc = getAnalyzableBrOpc(SecondLastInst->getOpcode());
Akira Hatanakad0a4b602013-03-01 01:10:17 +0000214
215 // Not an analyzable branch (must be an indirect jump).
216 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
217 return BT_None;
218 }
219
Akira Hatanakad0a4b602013-03-01 01:10:17 +0000220 // If there is only one terminator instruction, process it.
221 if (!SecondLastOpc) {
222 // Unconditional branch
223 if (LastOpc == UncondBrOpc) {
224 TBB = LastInst->getOperand(0).getMBB();
225 return BT_Uncond;
226 }
227
228 // Conditional branch
229 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
230 return BT_Cond;
231 }
232
233 // If we reached here, there are two branches.
234 // If there are three terminators, we don't know what sort of block this is.
235 if (++I != REnd && isUnpredicatedTerminator(&*I))
236 return BT_None;
237
Akira Hatanaka888e8fe2013-03-01 01:22:26 +0000238 BranchInstrs.insert(BranchInstrs.begin(), SecondLastInst);
239
Akira Hatanakad0a4b602013-03-01 01:10:17 +0000240 // If second to last instruction is an unconditional branch,
241 // analyze it and remove the last instruction.
242 if (SecondLastOpc == UncondBrOpc) {
243 // Return if the last instruction cannot be removed.
244 if (!AllowModify)
245 return BT_None;
246
247 TBB = SecondLastInst->getOperand(0).getMBB();
248 LastInst->eraseFromParent();
249 BranchInstrs.pop_back();
250 return BT_Uncond;
251 }
252
253 // Conditional branch followed by an unconditional branch.
254 // The last one must be unconditional.
255 if (LastOpc != UncondBrOpc)
256 return BT_None;
257
258 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
259 FBB = LastInst->getOperand(0).getMBB();
260
261 return BT_CondUncond;
262}
263
Akira Hatanakad4b48b22012-06-14 01:16:45 +0000264/// Return the number of bytes of code the specified instruction may be.
265unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
266 switch (MI->getOpcode()) {
267 default:
268 return MI->getDesc().getSize();
269 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
270 const MachineFunction *MF = MI->getParent()->getParent();
271 const char *AsmStr = MI->getOperand(0).getSymbolName();
272 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
273 }
274 }
275}
Akira Hatanaka151687c2013-05-13 17:57:42 +0000276
277MachineInstrBuilder
278MipsInstrInfo::genInstrWithNewOpc(unsigned NewOpc,
279 MachineBasicBlock::iterator I) const {
280 MachineInstrBuilder MIB;
281 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), get(NewOpc));
282
283 for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J)
284 MIB.addOperand(I->getOperand(J));
285
286 MIB.setMemRefs(I->memoperands_begin(), I->memoperands_end());
287 return MIB;
288}