blob: dcc0e24e080bab1a05818c8aa6897cb5bf90d793 [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
Chandler Carruthd174b722014-04-22 02:03:14 +000025using namespace llvm;
26
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000027#define GET_INSTRINFO_CTOR_DTOR
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000028#include "MipsGenInstrInfo.inc"
29
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000030// Pin the vtable to this file.
31void MipsInstrInfo::anchor() {}
32
Eric Christopher675cb4d2014-07-18 23:25:00 +000033MipsInstrInfo::MipsInstrInfo(const MipsSubtarget &STI, unsigned UncondBr)
34 : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
35 Subtarget(STI), UncondBrOpc(UncondBr) {}
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000036
Eric Christopher675cb4d2014-07-18 23:25:00 +000037const MipsInstrInfo *MipsInstrInfo::create(MipsSubtarget &STI) {
38 if (STI.inMips16Mode())
39 return llvm::createMips16InstrInfo(STI);
Akira Hatanakafab89292012-08-02 18:21:47 +000040
Eric Christopher675cb4d2014-07-18 23:25:00 +000041 return llvm::createMipsSEInstrInfo(STI);
Akira Hatanakafab89292012-08-02 18:21:47 +000042}
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
Eric Christopher754d54f2014-07-18 20:35:49 +000097void
98MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
99 DebugLoc DL,
100 const SmallVectorImpl<MachineOperand> &Cond) const {
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000101 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
Eric Christopher754d54f2014-07-18 20:35:49 +0000116unsigned MipsInstrInfo::InsertBranch(
117 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
118 const SmallVectorImpl<MachineOperand> &Cond, DebugLoc DL) const {
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000119 // Shouldn't be a fall through.
120 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000121
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000122 // # of condition operands:
123 // Unconditional branches: 0
124 // Floating point branches: 1 (opc)
125 // Int BranchZero: 2 (opc, reg)
126 // Int Branch: 3 (opc, reg0, reg1)
127 assert((Cond.size() <= 3) &&
128 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000129
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000130 // Two-way Conditional branch.
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000131 if (FBB) {
132 BuildCondBr(MBB, TBB, DL, Cond);
Akira Hatanaka5d5e0d82011-12-12 22:39:35 +0000133 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000134 return 2;
135 }
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000136
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000137 // One way branch.
138 // Unconditional branch.
139 if (Cond.empty())
Akira Hatanaka5d5e0d82011-12-12 22:39:35 +0000140 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000141 else // Conditional branch.
142 BuildCondBr(MBB, TBB, DL, Cond);
143 return 1;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000144}
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000145
Eric Christopher754d54f2014-07-18 20:35:49 +0000146unsigned MipsInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000147 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
148 MachineBasicBlock::reverse_iterator FirstBr;
149 unsigned removed;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000150
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000151 // Skip all the debug instructions.
152 while (I != REnd && I->isDebugValue())
153 ++I;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000154
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000155 FirstBr = I;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000156
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000157 // Up to 2 branches are removed.
158 // Note that indirect branches are not removed.
Eric Christopher675cb4d2014-07-18 23:25:00 +0000159 for (removed = 0; I != REnd && removed < 2; ++I, ++removed)
Akira Hatanaka067d8152013-05-13 17:43:19 +0000160 if (!getAnalyzableBrOpc(I->getOpcode()))
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000161 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000162
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000163 MBB.erase(I.base(), FirstBr.base());
164
165 return removed;
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000166}
167
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000168/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000169/// specified Branch instruction.
Eric Christopher754d54f2014-07-18 20:35:49 +0000170bool MipsInstrInfo::ReverseBranchCondition(
171 SmallVectorImpl<MachineOperand> &Cond) const {
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000172 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000173 "Invalid Mips branch condition!");
Akira Hatanaka067d8152013-05-13 17:43:19 +0000174 Cond[0].setImm(getOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000175 return false;
176}
Dan Gohmand5ca70642009-06-03 20:30:14 +0000177
Eric Christopher754d54f2014-07-18 20:35:49 +0000178MipsInstrInfo::BranchType MipsInstrInfo::AnalyzeBranch(
179 MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB,
180 SmallVectorImpl<MachineOperand> &Cond, bool AllowModify,
181 SmallVectorImpl<MachineInstr *> &BranchInstrs) const {
Akira Hatanaka7320b232013-03-01 01:10:17 +0000182
183 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
184
185 // Skip all the debug instructions.
186 while (I != REnd && I->isDebugValue())
187 ++I;
188
189 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
190 // This block ends with no branches (it just falls through to its succ).
191 // Leave TBB/FBB null.
Craig Topper062a2ba2014-04-25 05:30:21 +0000192 TBB = FBB = nullptr;
Akira Hatanaka7320b232013-03-01 01:10:17 +0000193 return BT_NoBranch;
194 }
195
196 MachineInstr *LastInst = &*I;
197 unsigned LastOpc = LastInst->getOpcode();
198 BranchInstrs.push_back(LastInst);
199
200 // Not an analyzable branch (e.g., indirect jump).
Akira Hatanaka067d8152013-05-13 17:43:19 +0000201 if (!getAnalyzableBrOpc(LastOpc))
Akira Hatanaka7320b232013-03-01 01:10:17 +0000202 return LastInst->isIndirectBranch() ? BT_Indirect : BT_None;
203
204 // Get the second to last instruction in the block.
205 unsigned SecondLastOpc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000206 MachineInstr *SecondLastInst = nullptr;
Akira Hatanaka7320b232013-03-01 01:10:17 +0000207
208 if (++I != REnd) {
209 SecondLastInst = &*I;
Akira Hatanaka067d8152013-05-13 17:43:19 +0000210 SecondLastOpc = getAnalyzableBrOpc(SecondLastInst->getOpcode());
Akira Hatanaka7320b232013-03-01 01:10:17 +0000211
212 // Not an analyzable branch (must be an indirect jump).
213 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
214 return BT_None;
215 }
216
Akira Hatanaka7320b232013-03-01 01:10:17 +0000217 // If there is only one terminator instruction, process it.
218 if (!SecondLastOpc) {
Matheus Almeida6de62d32013-10-01 12:53:00 +0000219 // Unconditional branch.
Akira Hatanaka7320b232013-03-01 01:10:17 +0000220 if (LastOpc == UncondBrOpc) {
221 TBB = LastInst->getOperand(0).getMBB();
222 return BT_Uncond;
223 }
224
225 // Conditional branch
226 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
227 return BT_Cond;
228 }
229
230 // If we reached here, there are two branches.
231 // If there are three terminators, we don't know what sort of block this is.
232 if (++I != REnd && isUnpredicatedTerminator(&*I))
233 return BT_None;
234
Akira Hatanaka28dc83c2013-03-01 01:22:26 +0000235 BranchInstrs.insert(BranchInstrs.begin(), SecondLastInst);
236
Akira Hatanaka7320b232013-03-01 01:10:17 +0000237 // If second to last instruction is an unconditional branch,
238 // analyze it and remove the last instruction.
239 if (SecondLastOpc == UncondBrOpc) {
240 // Return if the last instruction cannot be removed.
241 if (!AllowModify)
242 return BT_None;
243
244 TBB = SecondLastInst->getOperand(0).getMBB();
245 LastInst->eraseFromParent();
246 BranchInstrs.pop_back();
247 return BT_Uncond;
248 }
249
250 // Conditional branch followed by an unconditional branch.
251 // The last one must be unconditional.
252 if (LastOpc != UncondBrOpc)
253 return BT_None;
254
255 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
256 FBB = LastInst->getOperand(0).getMBB();
257
258 return BT_CondUncond;
259}
260
Akira Hatanakaacd1a7d2012-06-14 01:16:45 +0000261/// Return the number of bytes of code the specified instruction may be.
262unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
263 switch (MI->getOpcode()) {
264 default:
265 return MI->getDesc().getSize();
266 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
267 const MachineFunction *MF = MI->getParent()->getParent();
268 const char *AsmStr = MI->getOperand(0).getSymbolName();
269 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
270 }
Reed Kotler91ae9822013-10-27 21:57:36 +0000271 case Mips::CONSTPOOL_ENTRY:
272 // If this machine instr is a constant pool entry, its size is recorded as
273 // operand #2.
274 return MI->getOperand(2).getImm();
Akira Hatanakaacd1a7d2012-06-14 01:16:45 +0000275 }
276}
Akira Hatanaka310e26a2013-05-13 17:57:42 +0000277
278MachineInstrBuilder
279MipsInstrInfo::genInstrWithNewOpc(unsigned NewOpc,
280 MachineBasicBlock::iterator I) const {
281 MachineInstrBuilder MIB;
282 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), get(NewOpc));
283
284 for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J)
285 MIB.addOperand(I->getOperand(J));
286
287 MIB.setMemRefs(I->memoperands_begin(), I->memoperands_end());
288 return MIB;
289}