blob: a3c69c67448d50cb35ecb66684fe1d741a9c516c [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 "MipsMachineFunction.h"
Eric Christopherd8abc3a2015-01-08 18:18:54 +000017#include "MipsSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/STLExtras.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohmand5ca70642009-06-03 20:30:14 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwin56d06592009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000022#include "llvm/Support/TargetRegistry.h"
Evan Cheng1e210d02011-06-28 20:07:07 +000023
Chandler Carruthd174b722014-04-22 02:03:14 +000024using namespace llvm;
25
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000026#define GET_INSTRINFO_CTOR_DTOR
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000027#include "MipsGenInstrInfo.inc"
28
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000029// Pin the vtable to this file.
30void MipsInstrInfo::anchor() {}
31
Eric Christopher675cb4d2014-07-18 23:25:00 +000032MipsInstrInfo::MipsInstrInfo(const MipsSubtarget &STI, unsigned UncondBr)
33 : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
34 Subtarget(STI), UncondBrOpc(UncondBr) {}
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000035
Eric Christopher675cb4d2014-07-18 23:25:00 +000036const MipsInstrInfo *MipsInstrInfo::create(MipsSubtarget &STI) {
37 if (STI.inMips16Mode())
38 return llvm::createMips16InstrInfo(STI);
Akira Hatanakafab89292012-08-02 18:21:47 +000039
Eric Christopher675cb4d2014-07-18 23:25:00 +000040 return llvm::createMipsSEInstrInfo(STI);
Akira Hatanakafab89292012-08-02 18:21:47 +000041}
42
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000043bool MipsInstrInfo::isZeroImm(const MachineOperand &op) const {
Dan Gohman0d1e9a82008-10-03 15:45:36 +000044 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000045}
46
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000047/// insertNoop - If data hazard condition is found insert the target nop
48/// instruction.
49void MipsInstrInfo::
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000050insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000051{
Chris Lattner6f306d72010-04-02 20:16:16 +000052 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +000053 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000054}
55
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000056MachineMemOperand *MipsInstrInfo::GetMemOperand(MachineBasicBlock &MBB, int FI,
57 unsigned Flag) const {
Akira Hatanaka1cf75762011-12-24 03:11:18 +000058 MachineFunction &MF = *MBB.getParent();
59 MachineFrameInfo &MFI = *MF.getFrameInfo();
60 unsigned Align = MFI.getObjectAlignment(FI);
Jia Liuf54f60f2012-02-28 07:46:26 +000061
Alex Lorenze40c8a22015-08-11 23:09:45 +000062 return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
63 Flag, MFI.getObjectSize(FI), Align);
Akira Hatanaka1cf75762011-12-24 03:11:18 +000064}
65
Akira Hatanakae2489122011-04-15 21:51:11 +000066//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000067// Branch Analysis
Akira Hatanakae2489122011-04-15 21:51:11 +000068//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000069
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000070void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
71 MachineBasicBlock *&BB,
72 SmallVectorImpl<MachineOperand> &Cond) const {
Akira Hatanaka067d8152013-05-13 17:43:19 +000073 assert(getAnalyzableBrOpc(Opc) && "Not an analyzable branch");
Akira Hatanaka93f898f2011-04-01 17:39:08 +000074 int NumOp = Inst->getNumExplicitOperands();
Jia Liuf54f60f2012-02-28 07:46:26 +000075
Akira Hatanaka93f898f2011-04-01 17:39:08 +000076 // for both int and fp branches, the last explicit operand is the
77 // MBB.
78 BB = Inst->getOperand(NumOp-1).getMBB();
79 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +000080
Akira Hatanaka93f898f2011-04-01 17:39:08 +000081 for (int i=0; i<NumOp-1; i++)
82 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000083}
84
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000085bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000086 MachineBasicBlock *&TBB,
87 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +000088 SmallVectorImpl<MachineOperand> &Cond,
Akira Hatanaka7320b232013-03-01 01:10:17 +000089 bool AllowModify) const {
90 SmallVector<MachineInstr*, 2> BranchInstrs;
91 BranchType BT = AnalyzeBranch(MBB, TBB, FBB, Cond, AllowModify, BranchInstrs);
Akira Hatanakafcdd9b12012-09-13 17:12:37 +000092
Akira Hatanaka7320b232013-03-01 01:10:17 +000093 return (BT == BT_None) || (BT == BT_Indirect);
Jia Liuf54f60f2012-02-28 07:46:26 +000094}
95
Eric Christopher754d54f2014-07-18 20:35:49 +000096void
97MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +000098 DebugLoc DL, ArrayRef<MachineOperand> Cond) const {
Akira Hatanaka93f898f2011-04-01 17:39:08 +000099 unsigned Opc = Cond[0].getImm();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000100 const MCInstrDesc &MCID = get(Opc);
101 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000102
Akira Hatanakafcdd9b12012-09-13 17:12:37 +0000103 for (unsigned i = 1; i < Cond.size(); ++i) {
104 if (Cond[i].isReg())
105 MIB.addReg(Cond[i].getReg());
106 else if (Cond[i].isImm())
107 MIB.addImm(Cond[i].getImm());
108 else
109 assert(true && "Cannot copy operand");
110 }
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000111 MIB.addMBB(TBB);
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000112}
113
Eric Christopher754d54f2014-07-18 20:35:49 +0000114unsigned MipsInstrInfo::InsertBranch(
115 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000116 ArrayRef<MachineOperand> Cond, DebugLoc DL) const {
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000117 // Shouldn't be a fall through.
118 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000119
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000120 // # of condition operands:
121 // Unconditional branches: 0
122 // Floating point branches: 1 (opc)
123 // Int BranchZero: 2 (opc, reg)
124 // Int Branch: 3 (opc, reg0, reg1)
125 assert((Cond.size() <= 3) &&
126 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000127
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000128 // Two-way Conditional branch.
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000129 if (FBB) {
130 BuildCondBr(MBB, TBB, DL, Cond);
Akira Hatanaka5d5e0d82011-12-12 22:39:35 +0000131 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000132 return 2;
133 }
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000134
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000135 // One way branch.
136 // Unconditional branch.
137 if (Cond.empty())
Akira Hatanaka5d5e0d82011-12-12 22:39:35 +0000138 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000139 else // Conditional branch.
140 BuildCondBr(MBB, TBB, DL, Cond);
141 return 1;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000142}
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000143
Eric Christopher754d54f2014-07-18 20:35:49 +0000144unsigned MipsInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000145 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
146 MachineBasicBlock::reverse_iterator FirstBr;
147 unsigned removed;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000148
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000149 // Skip all the debug instructions.
150 while (I != REnd && I->isDebugValue())
151 ++I;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000152
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000153 FirstBr = I;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000154
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000155 // Up to 2 branches are removed.
156 // Note that indirect branches are not removed.
Eric Christopher675cb4d2014-07-18 23:25:00 +0000157 for (removed = 0; I != REnd && removed < 2; ++I, ++removed)
Akira Hatanaka067d8152013-05-13 17:43:19 +0000158 if (!getAnalyzableBrOpc(I->getOpcode()))
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000159 break;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000160
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000161 MBB.erase(I.base(), FirstBr.base());
162
163 return removed;
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000164}
165
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000166/// ReverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000167/// specified Branch instruction.
Eric Christopher754d54f2014-07-18 20:35:49 +0000168bool MipsInstrInfo::ReverseBranchCondition(
169 SmallVectorImpl<MachineOperand> &Cond) const {
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000170 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000171 "Invalid Mips branch condition!");
Akira Hatanaka067d8152013-05-13 17:43:19 +0000172 Cond[0].setImm(getOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000173 return false;
174}
Dan Gohmand5ca70642009-06-03 20:30:14 +0000175
Eric Christopher754d54f2014-07-18 20:35:49 +0000176MipsInstrInfo::BranchType MipsInstrInfo::AnalyzeBranch(
177 MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB,
178 SmallVectorImpl<MachineOperand> &Cond, bool AllowModify,
179 SmallVectorImpl<MachineInstr *> &BranchInstrs) const {
Akira Hatanaka7320b232013-03-01 01:10:17 +0000180
181 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
182
183 // Skip all the debug instructions.
184 while (I != REnd && I->isDebugValue())
185 ++I;
186
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000187 if (I == REnd || !isUnpredicatedTerminator(*I)) {
Akira Hatanaka7320b232013-03-01 01:10:17 +0000188 // This block ends with no branches (it just falls through to its succ).
189 // Leave TBB/FBB null.
Craig Topper062a2ba2014-04-25 05:30:21 +0000190 TBB = FBB = nullptr;
Akira Hatanaka7320b232013-03-01 01:10:17 +0000191 return BT_NoBranch;
192 }
193
194 MachineInstr *LastInst = &*I;
195 unsigned LastOpc = LastInst->getOpcode();
196 BranchInstrs.push_back(LastInst);
197
198 // Not an analyzable branch (e.g., indirect jump).
Akira Hatanaka067d8152013-05-13 17:43:19 +0000199 if (!getAnalyzableBrOpc(LastOpc))
Akira Hatanaka7320b232013-03-01 01:10:17 +0000200 return LastInst->isIndirectBranch() ? BT_Indirect : BT_None;
201
202 // Get the second to last instruction in the block.
203 unsigned SecondLastOpc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000204 MachineInstr *SecondLastInst = nullptr;
Akira Hatanaka7320b232013-03-01 01:10:17 +0000205
206 if (++I != REnd) {
207 SecondLastInst = &*I;
Akira Hatanaka067d8152013-05-13 17:43:19 +0000208 SecondLastOpc = getAnalyzableBrOpc(SecondLastInst->getOpcode());
Akira Hatanaka7320b232013-03-01 01:10:17 +0000209
210 // Not an analyzable branch (must be an indirect jump).
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000211 if (isUnpredicatedTerminator(*SecondLastInst) && !SecondLastOpc)
Akira Hatanaka7320b232013-03-01 01:10:17 +0000212 return BT_None;
213 }
214
Akira Hatanaka7320b232013-03-01 01:10:17 +0000215 // If there is only one terminator instruction, process it.
216 if (!SecondLastOpc) {
Matheus Almeida6de62d32013-10-01 12:53:00 +0000217 // Unconditional branch.
Akira Hatanaka7320b232013-03-01 01:10:17 +0000218 if (LastOpc == UncondBrOpc) {
219 TBB = LastInst->getOperand(0).getMBB();
220 return BT_Uncond;
221 }
222
223 // Conditional branch
224 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
225 return BT_Cond;
226 }
227
228 // If we reached here, there are two branches.
229 // If there are three terminators, we don't know what sort of block this is.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000230 if (++I != REnd && isUnpredicatedTerminator(*I))
Akira Hatanaka7320b232013-03-01 01:10:17 +0000231 return BT_None;
232
Akira Hatanaka28dc83c2013-03-01 01:22:26 +0000233 BranchInstrs.insert(BranchInstrs.begin(), SecondLastInst);
234
Akira Hatanaka7320b232013-03-01 01:10:17 +0000235 // If second to last instruction is an unconditional branch,
236 // analyze it and remove the last instruction.
237 if (SecondLastOpc == UncondBrOpc) {
238 // Return if the last instruction cannot be removed.
239 if (!AllowModify)
240 return BT_None;
241
242 TBB = SecondLastInst->getOperand(0).getMBB();
243 LastInst->eraseFromParent();
244 BranchInstrs.pop_back();
245 return BT_Uncond;
246 }
247
248 // Conditional branch followed by an unconditional branch.
249 // The last one must be unconditional.
250 if (LastOpc != UncondBrOpc)
251 return BT_None;
252
253 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
254 FBB = LastInst->getOperand(0).getMBB();
255
256 return BT_CondUncond;
257}
258
Akira Hatanakaacd1a7d2012-06-14 01:16:45 +0000259/// Return the number of bytes of code the specified instruction may be.
260unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
261 switch (MI->getOpcode()) {
262 default:
263 return MI->getDesc().getSize();
264 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
265 const MachineFunction *MF = MI->getParent()->getParent();
266 const char *AsmStr = MI->getOperand(0).getSymbolName();
267 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
268 }
Reed Kotler91ae9822013-10-27 21:57:36 +0000269 case Mips::CONSTPOOL_ENTRY:
270 // If this machine instr is a constant pool entry, its size is recorded as
271 // operand #2.
272 return MI->getOperand(2).getImm();
Akira Hatanakaacd1a7d2012-06-14 01:16:45 +0000273 }
274}
Akira Hatanaka310e26a2013-05-13 17:57:42 +0000275
276MachineInstrBuilder
277MipsInstrInfo::genInstrWithNewOpc(unsigned NewOpc,
278 MachineBasicBlock::iterator I) const {
279 MachineInstrBuilder MIB;
280 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), get(NewOpc));
281
282 for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J)
283 MIB.addOperand(I->getOperand(J));
284
285 MIB.setMemRefs(I->memoperands_begin(), I->memoperands_end());
286 return MIB;
287}