blob: 0ebad0584757297d00860047c0cee782d3c41221 [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsInstrInfo.cpp - Mips Instruction Information ------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lopes35e43c42007-06-06 07:42:06 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00009//
10// This file contains the Mips implementation of the TargetInstrInfo class.
11//
Akira Hatanakae2489122011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000013
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000014#include "MipsInstrInfo.h"
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000015#include "InstPrinter/MipsInstPrinter.h"
Chandler Carruthed0881b2012-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 Lopes35e43c42007-06-06 07:42:06 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohmand5ca70642009-06-03 20:30:14 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwin56d06592009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000023#include "llvm/Support/TargetRegistry.h"
Evan Cheng1e210d02011-06-28 20:07:07 +000024
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000025#define GET_INSTRINFO_CTOR_DTOR
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000026#include "MipsGenInstrInfo.inc"
27
28using namespace llvm;
29
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000030// Pin the vtable to this file.
31void MipsInstrInfo::anchor() {}
32
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000033MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm, unsigned UncondBr)
Evan Cheng703a0fb2011-07-01 17:57:27 +000034 : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
Akira Hatanakacb37e132012-07-31 23:41:32 +000035 TM(tm), UncondBrOpc(UncondBr) {}
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000036
Akira Hatanakafab89292012-08-02 18:21:47 +000037const MipsInstrInfo *MipsInstrInfo::create(MipsTargetMachine &TM) {
38 if (TM.getSubtargetImpl()->inMips16Mode())
39 return llvm::createMips16InstrInfo(TM);
40
41 return llvm::createMipsSEInstrInfo(TM);
42}
43
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000044bool MipsInstrInfo::isZeroImm(const MachineOperand &op) const {
Dan Gohman0d1e9a82008-10-03 15:45:36 +000045 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000046}
47
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000048/// insertNoop - If data hazard condition is found insert the target nop
49/// instruction.
50void MipsInstrInfo::
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000051insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000052{
Chris Lattner6f306d72010-04-02 20:16:16 +000053 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +000054 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000055}
56
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000057MachineMemOperand *MipsInstrInfo::GetMemOperand(MachineBasicBlock &MBB, int FI,
58 unsigned Flag) const {
Akira Hatanaka1cf75762011-12-24 03:11:18 +000059 MachineFunction &MF = *MBB.getParent();
60 MachineFrameInfo &MFI = *MF.getFrameInfo();
61 unsigned Align = MFI.getObjectAlignment(FI);
Jia Liuf54f60f2012-02-28 07:46:26 +000062
Akira Hatanaka1cf75762011-12-24 03:11:18 +000063 return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), Flag,
64 MFI.getObjectSize(FI), Align);
65}
66
Akira Hatanakae2489122011-04-15 21:51:11 +000067//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000068// Branch Analysis
Akira Hatanakae2489122011-04-15 21:51:11 +000069//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000070
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000071void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
72 MachineBasicBlock *&BB,
73 SmallVectorImpl<MachineOperand> &Cond) const {
Akira Hatanaka067d8152013-05-13 17:43:19 +000074 assert(getAnalyzableBrOpc(Opc) && "Not an analyzable branch");
Akira Hatanaka93f898f2011-04-01 17:39:08 +000075 int NumOp = Inst->getNumExplicitOperands();
Jia Liuf54f60f2012-02-28 07:46:26 +000076
Akira Hatanaka93f898f2011-04-01 17:39:08 +000077 // for both int and fp branches, the last explicit operand is the
78 // MBB.
79 BB = Inst->getOperand(NumOp-1).getMBB();
80 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +000081
Akira Hatanaka93f898f2011-04-01 17:39:08 +000082 for (int i=0; i<NumOp-1; i++)
83 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000084}
85
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000086bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000087 MachineBasicBlock *&TBB,
88 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +000089 SmallVectorImpl<MachineOperand> &Cond,
Akira Hatanaka7320b232013-03-01 01:10:17 +000090 bool AllowModify) const {
91 SmallVector<MachineInstr*, 2> BranchInstrs;
92 BranchType BT = AnalyzeBranch(MBB, TBB, FBB, Cond, AllowModify, BranchInstrs);
Akira Hatanakafcdd9b12012-09-13 17:12:37 +000093
Akira Hatanaka7320b232013-03-01 01:10:17 +000094 return (BT == BT_None) || (BT == BT_Indirect);
Jia Liuf54f60f2012-02-28 07:46:26 +000095}
96
Akira Hatanaka93f898f2011-04-01 17:39:08 +000097void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
98 MachineBasicBlock *TBB, DebugLoc DL,
99 const SmallVectorImpl<MachineOperand>& Cond)
100 const {
101 unsigned Opc = Cond[0].getImm();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000102 const MCInstrDesc &MCID = get(Opc);
103 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000104
Akira Hatanakafcdd9b12012-09-13 17:12:37 +0000105 for (unsigned i = 1; i < Cond.size(); ++i) {
106 if (Cond[i].isReg())
107 MIB.addReg(Cond[i].getReg());
108 else if (Cond[i].isImm())
109 MIB.addImm(Cond[i].getImm());
110 else
111 assert(true && "Cannot copy operand");
112 }
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000113 MIB.addMBB(TBB);
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000114}
115
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000116unsigned MipsInstrInfo::
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000117InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Owen Anderson4f6bf042008-08-14 22:49:33 +0000118 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000119 const SmallVectorImpl<MachineOperand> &Cond,
120 DebugLoc DL) const {
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000121 // Shouldn't be a fall through.
122 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000123
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000124 // # of condition operands:
125 // Unconditional branches: 0
126 // Floating point branches: 1 (opc)
127 // Int BranchZero: 2 (opc, reg)
128 // Int Branch: 3 (opc, reg0, reg1)
129 assert((Cond.size() <= 3) &&
130 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000131
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000132 // Two-way Conditional branch.
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000133 if (FBB) {
134 BuildCondBr(MBB, TBB, DL, Cond);
Akira Hatanaka5d5e0d82011-12-12 22:39:35 +0000135 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000136 return 2;
137 }
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000138
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000139 // One way branch.
140 // Unconditional branch.
141 if (Cond.empty())
Akira Hatanaka5d5e0d82011-12-12 22:39:35 +0000142 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000143 else // Conditional branch.
144 BuildCondBr(MBB, TBB, DL, Cond);
145 return 1;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000146}
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000147
148unsigned MipsInstrInfo::
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000149RemoveBranch(MachineBasicBlock &MBB) const
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000150{
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000151 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
152 MachineBasicBlock::reverse_iterator FirstBr;
153 unsigned removed;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000154
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000155 // Skip all the debug instructions.
156 while (I != REnd && I->isDebugValue())
157 ++I;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000158
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000159 FirstBr = I;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000160
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000161 // Up to 2 branches are removed.
162 // Note that indirect branches are not removed.
163 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
Akira Hatanaka067d8152013-05-13 17:43:19 +0000164 if (!getAnalyzableBrOpc(I->getOpcode()))
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000165 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000166
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000167 MBB.erase(I.base(), FirstBr.base());
168
169 return removed;
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000170}
171
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000172/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000173/// specified Branch instruction.
174bool MipsInstrInfo::
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000175ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000176{
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000177 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000178 "Invalid Mips branch condition!");
Akira Hatanaka067d8152013-05-13 17:43:19 +0000179 Cond[0].setImm(getOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000180 return false;
181}
Dan Gohmand5ca70642009-06-03 20:30:14 +0000182
Akira Hatanaka7320b232013-03-01 01:10:17 +0000183MipsInstrInfo::BranchType MipsInstrInfo::
184AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
185 MachineBasicBlock *&FBB, SmallVectorImpl<MachineOperand> &Cond,
186 bool AllowModify,
187 SmallVectorImpl<MachineInstr*> &BranchInstrs) const {
188
189 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
190
191 // Skip all the debug instructions.
192 while (I != REnd && I->isDebugValue())
193 ++I;
194
195 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
196 // This block ends with no branches (it just falls through to its succ).
197 // Leave TBB/FBB null.
198 TBB = FBB = NULL;
199 return BT_NoBranch;
200 }
201
202 MachineInstr *LastInst = &*I;
203 unsigned LastOpc = LastInst->getOpcode();
204 BranchInstrs.push_back(LastInst);
205
206 // Not an analyzable branch (e.g., indirect jump).
Akira Hatanaka067d8152013-05-13 17:43:19 +0000207 if (!getAnalyzableBrOpc(LastOpc))
Akira Hatanaka7320b232013-03-01 01:10:17 +0000208 return LastInst->isIndirectBranch() ? BT_Indirect : BT_None;
209
210 // Get the second to last instruction in the block.
211 unsigned SecondLastOpc = 0;
212 MachineInstr *SecondLastInst = NULL;
213
214 if (++I != REnd) {
215 SecondLastInst = &*I;
Akira Hatanaka067d8152013-05-13 17:43:19 +0000216 SecondLastOpc = getAnalyzableBrOpc(SecondLastInst->getOpcode());
Akira Hatanaka7320b232013-03-01 01:10:17 +0000217
218 // Not an analyzable branch (must be an indirect jump).
219 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
220 return BT_None;
221 }
222
Akira Hatanaka7320b232013-03-01 01:10:17 +0000223 // If there is only one terminator instruction, process it.
224 if (!SecondLastOpc) {
Matheus Almeida6de62d32013-10-01 12:53:00 +0000225 // Unconditional branch.
Akira Hatanaka7320b232013-03-01 01:10:17 +0000226 if (LastOpc == UncondBrOpc) {
227 TBB = LastInst->getOperand(0).getMBB();
228 return BT_Uncond;
229 }
230
231 // Conditional branch
232 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
233 return BT_Cond;
234 }
235
236 // If we reached here, there are two branches.
237 // If there are three terminators, we don't know what sort of block this is.
238 if (++I != REnd && isUnpredicatedTerminator(&*I))
239 return BT_None;
240
Akira Hatanaka28dc83c2013-03-01 01:22:26 +0000241 BranchInstrs.insert(BranchInstrs.begin(), SecondLastInst);
242
Akira Hatanaka7320b232013-03-01 01:10:17 +0000243 // If second to last instruction is an unconditional branch,
244 // analyze it and remove the last instruction.
245 if (SecondLastOpc == UncondBrOpc) {
246 // Return if the last instruction cannot be removed.
247 if (!AllowModify)
248 return BT_None;
249
250 TBB = SecondLastInst->getOperand(0).getMBB();
251 LastInst->eraseFromParent();
252 BranchInstrs.pop_back();
253 return BT_Uncond;
254 }
255
256 // Conditional branch followed by an unconditional branch.
257 // The last one must be unconditional.
258 if (LastOpc != UncondBrOpc)
259 return BT_None;
260
261 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
262 FBB = LastInst->getOperand(0).getMBB();
263
264 return BT_CondUncond;
265}
266
Akira Hatanakaacd1a7d2012-06-14 01:16:45 +0000267/// Return the number of bytes of code the specified instruction may be.
268unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
269 switch (MI->getOpcode()) {
270 default:
271 return MI->getDesc().getSize();
272 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
273 const MachineFunction *MF = MI->getParent()->getParent();
274 const char *AsmStr = MI->getOperand(0).getSymbolName();
275 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
276 }
Reed Kotler91ae9822013-10-27 21:57:36 +0000277 case Mips::CONSTPOOL_ENTRY:
278 // If this machine instr is a constant pool entry, its size is recorded as
279 // operand #2.
280 return MI->getOperand(2).getImm();
Akira Hatanakaacd1a7d2012-06-14 01:16:45 +0000281 }
282}
Akira Hatanaka310e26a2013-05-13 17:57:42 +0000283
284MachineInstrBuilder
285MipsInstrInfo::genInstrWithNewOpc(unsigned NewOpc,
286 MachineBasicBlock::iterator I) const {
287 MachineInstrBuilder MIB;
288 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), get(NewOpc));
289
290 for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J)
291 MIB.addOperand(I->getOperand(J));
292
293 MIB.setMemRefs(I->memoperands_begin(), I->memoperands_end());
294 return MIB;
295}