blob: 51ddc0d44c0022cb95750441148560bd0f981385 [file] [log] [blame]
Eugene Zelenko79220eae2017-08-03 22:12:30 +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"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000015#include "MCTargetDesc/MipsBaseInfo.h"
16#include "MCTargetDesc/MipsMCTargetDesc.h"
Eric Christopherd8abc3a2015-01-08 18:18:54 +000017#include "MipsSubtarget.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000018#include "llvm/ADT/SmallVector.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstr.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000024#include "llvm/CodeGen/MachineOperand.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000025#include "llvm/CodeGen/TargetOpcodes.h"
26#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000027#include "llvm/IR/DebugLoc.h"
28#include "llvm/MC/MCInstrDesc.h"
29#include "llvm/Target/TargetMachine.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000030#include <cassert>
Evan Cheng1e210d02011-06-28 20:07:07 +000031
Chandler Carruthd174b722014-04-22 02:03:14 +000032using namespace llvm;
33
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000034#define GET_INSTRINFO_CTOR_DTOR
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000035#include "MipsGenInstrInfo.inc"
36
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000037// Pin the vtable to this file.
38void MipsInstrInfo::anchor() {}
39
Eric Christopher675cb4d2014-07-18 23:25:00 +000040MipsInstrInfo::MipsInstrInfo(const MipsSubtarget &STI, unsigned UncondBr)
41 : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
42 Subtarget(STI), UncondBrOpc(UncondBr) {}
Akira Hatanaka9c6028f2011-07-07 23:56:50 +000043
Eric Christopher675cb4d2014-07-18 23:25:00 +000044const MipsInstrInfo *MipsInstrInfo::create(MipsSubtarget &STI) {
45 if (STI.inMips16Mode())
Eugene Zelenko79220eae2017-08-03 22:12:30 +000046 return createMips16InstrInfo(STI);
Akira Hatanakafab89292012-08-02 18:21:47 +000047
Eugene Zelenko79220eae2017-08-03 22:12:30 +000048 return createMipsSEInstrInfo(STI);
Akira Hatanakafab89292012-08-02 18:21:47 +000049}
50
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000051bool MipsInstrInfo::isZeroImm(const MachineOperand &op) const {
Dan Gohman0d1e9a82008-10-03 15:45:36 +000052 return op.isImm() && op.getImm() == 0;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000053}
54
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000055/// insertNoop - If data hazard condition is found insert the target nop
56/// instruction.
Simon Dardis9a3f32c2016-03-29 13:02:19 +000057// FIXME: This appears to be dead code.
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000058void MipsInstrInfo::
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +000059insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000060{
Chris Lattner6f306d72010-04-02 20:16:16 +000061 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +000062 BuildMI(MBB, MI, DL, get(Mips::NOP));
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000063}
64
Justin Lebar0af80cd2016-07-15 18:26:59 +000065MachineMemOperand *
66MipsInstrInfo::GetMemOperand(MachineBasicBlock &MBB, int FI,
67 MachineMemOperand::Flags Flags) const {
Akira Hatanaka1cf75762011-12-24 03:11:18 +000068 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +000069 MachineFrameInfo &MFI = MF.getFrameInfo();
Akira Hatanaka1cf75762011-12-24 03:11:18 +000070 unsigned Align = MFI.getObjectAlignment(FI);
Jia Liuf54f60f2012-02-28 07:46:26 +000071
Alex Lorenze40c8a22015-08-11 23:09:45 +000072 return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
Justin Lebar0af80cd2016-07-15 18:26:59 +000073 Flags, MFI.getObjectSize(FI), Align);
Akira Hatanaka1cf75762011-12-24 03:11:18 +000074}
75
Akira Hatanakae2489122011-04-15 21:51:11 +000076//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000077// Branch Analysis
Akira Hatanakae2489122011-04-15 21:51:11 +000078//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000079
Akira Hatanakab7fa3c92012-07-31 21:49:49 +000080void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
81 MachineBasicBlock *&BB,
82 SmallVectorImpl<MachineOperand> &Cond) const {
Akira Hatanaka067d8152013-05-13 17:43:19 +000083 assert(getAnalyzableBrOpc(Opc) && "Not an analyzable branch");
Akira Hatanaka93f898f2011-04-01 17:39:08 +000084 int NumOp = Inst->getNumExplicitOperands();
Jia Liuf54f60f2012-02-28 07:46:26 +000085
Akira Hatanaka93f898f2011-04-01 17:39:08 +000086 // for both int and fp branches, the last explicit operand is the
87 // MBB.
88 BB = Inst->getOperand(NumOp-1).getMBB();
89 Cond.push_back(MachineOperand::CreateImm(Opc));
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +000090
Eugene Zelenko79220eae2017-08-03 22:12:30 +000091 for (int i = 0; i < NumOp-1; i++)
Akira Hatanaka93f898f2011-04-01 17:39:08 +000092 Cond.push_back(Inst->getOperand(i));
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000093}
94
Jacques Pienaar71c30a12016-07-15 14:41:04 +000095bool MipsInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +000096 MachineBasicBlock *&TBB,
97 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +000098 SmallVectorImpl<MachineOperand> &Cond,
Akira Hatanaka7320b232013-03-01 01:10:17 +000099 bool AllowModify) const {
100 SmallVector<MachineInstr*, 2> BranchInstrs;
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000101 BranchType BT = analyzeBranch(MBB, TBB, FBB, Cond, AllowModify, BranchInstrs);
Akira Hatanakafcdd9b12012-09-13 17:12:37 +0000102
Akira Hatanaka7320b232013-03-01 01:10:17 +0000103 return (BT == BT_None) || (BT == BT_Indirect);
Jia Liuf54f60f2012-02-28 07:46:26 +0000104}
105
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000106void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
107 const DebugLoc &DL,
108 ArrayRef<MachineOperand> Cond) const {
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000109 unsigned Opc = Cond[0].getImm();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000110 const MCInstrDesc &MCID = get(Opc);
111 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000112
Akira Hatanakafcdd9b12012-09-13 17:12:37 +0000113 for (unsigned i = 1; i < Cond.size(); ++i) {
Simon Dardisc38d3912017-06-13 14:11:29 +0000114 assert((Cond[i].isImm() || Cond[i].isReg()) &&
115 "Cannot copy operand for conditional branch!");
116 MIB.add(Cond[i]);
Akira Hatanakafcdd9b12012-09-13 17:12:37 +0000117 }
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000118 MIB.addMBB(TBB);
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000119}
120
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000121unsigned MipsInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000122 MachineBasicBlock *TBB,
123 MachineBasicBlock *FBB,
124 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000125 const DebugLoc &DL,
126 int *BytesAdded) const {
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000127 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000128 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000129 assert(!BytesAdded && "code size not handled");
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000130
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000131 // # of condition operands:
132 // Unconditional branches: 0
133 // Floating point branches: 1 (opc)
134 // Int BranchZero: 2 (opc, reg)
135 // Int Branch: 3 (opc, reg0, reg1)
136 assert((Cond.size() <= 3) &&
137 "# of Mips branch conditions must be <= 3!");
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000138
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000139 // Two-way Conditional branch.
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000140 if (FBB) {
141 BuildCondBr(MBB, TBB, DL, Cond);
Akira Hatanaka5d5e0d82011-12-12 22:39:35 +0000142 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000143 return 2;
144 }
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000145
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000146 // One way branch.
147 // Unconditional branch.
148 if (Cond.empty())
Akira Hatanaka5d5e0d82011-12-12 22:39:35 +0000149 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000150 else // Conditional branch.
151 BuildCondBr(MBB, TBB, DL, Cond);
152 return 1;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000153}
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000154
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000155unsigned MipsInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000156 int *BytesRemoved) const {
157 assert(!BytesRemoved && "code size not handled");
158
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000159 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
Simon Dardis13452382017-10-26 10:58:36 +0000160 unsigned removed = 0;
Bruno Cardoso Lopesed874ef2011-03-04 17:51:39 +0000161
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000162 // Up to 2 branches are removed.
163 // Note that indirect branches are not removed.
Simon Dardis13452382017-10-26 10:58:36 +0000164 while (I != REnd && removed < 2) {
165 // Skip past debug instructions.
166 if (I->isDebugValue()) {
167 ++I;
168 continue;
169 }
Akira Hatanaka067d8152013-05-13 17:43:19 +0000170 if (!getAnalyzableBrOpc(I->getOpcode()))
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000171 break;
Simon Dardis13452382017-10-26 10:58:36 +0000172 // Remove the branch.
173 I->eraseFromParent();
174 I = MBB.rbegin();
175 ++removed;
176 }
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000177
178 return removed;
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000179}
180
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000181/// reverseBranchCondition - Return the inverse opcode of the
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000182/// specified Branch instruction.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000183bool MipsInstrInfo::reverseBranchCondition(
Eric Christopher754d54f2014-07-18 20:35:49 +0000184 SmallVectorImpl<MachineOperand> &Cond) const {
Akira Hatanaka93f898f2011-04-01 17:39:08 +0000185 assert( (Cond.size() && Cond.size() <= 3) &&
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000186 "Invalid Mips branch condition!");
Akira Hatanaka067d8152013-05-13 17:43:19 +0000187 Cond[0].setImm(getOppositeBranchOpc(Cond[0].getImm()));
Bruno Cardoso Lopes7b616f52007-08-18 01:56:48 +0000188 return false;
189}
Dan Gohmand5ca70642009-06-03 20:30:14 +0000190
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000191MipsInstrInfo::BranchType MipsInstrInfo::analyzeBranch(
Eric Christopher754d54f2014-07-18 20:35:49 +0000192 MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB,
193 SmallVectorImpl<MachineOperand> &Cond, bool AllowModify,
194 SmallVectorImpl<MachineInstr *> &BranchInstrs) const {
Akira Hatanaka7320b232013-03-01 01:10:17 +0000195 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
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000201 if (I == REnd || !isUnpredicatedTerminator(*I)) {
Akira Hatanaka7320b232013-03-01 01:10:17 +0000202 // This block ends with no branches (it just falls through to its succ).
203 // Leave TBB/FBB null.
Craig Topper062a2ba2014-04-25 05:30:21 +0000204 TBB = FBB = nullptr;
Akira Hatanaka7320b232013-03-01 01:10:17 +0000205 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).
Akira Hatanaka067d8152013-05-13 17:43:19 +0000213 if (!getAnalyzableBrOpc(LastOpc))
Akira Hatanaka7320b232013-03-01 01:10:17 +0000214 return LastInst->isIndirectBranch() ? BT_Indirect : BT_None;
215
216 // Get the second to last instruction in the block.
217 unsigned SecondLastOpc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000218 MachineInstr *SecondLastInst = nullptr;
Akira Hatanaka7320b232013-03-01 01:10:17 +0000219
Simon Dardis03c2c652017-10-18 14:35:29 +0000220 // Skip past any debug instruction to see if the second last actual
221 // is a branch.
222 ++I;
223 while (I != REnd && I->isDebugValue())
224 ++I;
225
226 if (I != REnd) {
Akira Hatanaka7320b232013-03-01 01:10:17 +0000227 SecondLastInst = &*I;
Akira Hatanaka067d8152013-05-13 17:43:19 +0000228 SecondLastOpc = getAnalyzableBrOpc(SecondLastInst->getOpcode());
Akira Hatanaka7320b232013-03-01 01:10:17 +0000229
230 // Not an analyzable branch (must be an indirect jump).
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000231 if (isUnpredicatedTerminator(*SecondLastInst) && !SecondLastOpc)
Akira Hatanaka7320b232013-03-01 01:10:17 +0000232 return BT_None;
233 }
234
Akira Hatanaka7320b232013-03-01 01:10:17 +0000235 // If there is only one terminator instruction, process it.
236 if (!SecondLastOpc) {
Matheus Almeida6de62d32013-10-01 12:53:00 +0000237 // Unconditional branch.
Daniel Sandersf9d8b8c2016-05-06 13:23:51 +0000238 if (LastInst->isUnconditionalBranch()) {
Akira Hatanaka7320b232013-03-01 01:10:17 +0000239 TBB = LastInst->getOperand(0).getMBB();
240 return BT_Uncond;
241 }
242
243 // Conditional branch
244 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
245 return BT_Cond;
246 }
247
248 // If we reached here, there are two branches.
249 // 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 +0000250 if (++I != REnd && isUnpredicatedTerminator(*I))
Akira Hatanaka7320b232013-03-01 01:10:17 +0000251 return BT_None;
252
Akira Hatanaka28dc83c2013-03-01 01:22:26 +0000253 BranchInstrs.insert(BranchInstrs.begin(), SecondLastInst);
254
Akira Hatanaka7320b232013-03-01 01:10:17 +0000255 // If second to last instruction is an unconditional branch,
256 // analyze it and remove the last instruction.
Daniel Sandersf9d8b8c2016-05-06 13:23:51 +0000257 if (SecondLastInst->isUnconditionalBranch()) {
Akira Hatanaka7320b232013-03-01 01:10:17 +0000258 // Return if the last instruction cannot be removed.
259 if (!AllowModify)
260 return BT_None;
261
262 TBB = SecondLastInst->getOperand(0).getMBB();
263 LastInst->eraseFromParent();
264 BranchInstrs.pop_back();
265 return BT_Uncond;
266 }
267
268 // Conditional branch followed by an unconditional branch.
269 // The last one must be unconditional.
Daniel Sandersf9d8b8c2016-05-06 13:23:51 +0000270 if (!LastInst->isUnconditionalBranch())
Akira Hatanaka7320b232013-03-01 01:10:17 +0000271 return BT_None;
272
273 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
274 FBB = LastInst->getOperand(0).getMBB();
275
276 return BT_CondUncond;
277}
278
Daniel Sanderse8efff32016-03-14 16:24:05 +0000279/// Return the corresponding compact (no delay slot) form of a branch.
280unsigned MipsInstrInfo::getEquivalentCompactForm(
281 const MachineBasicBlock::iterator I) const {
282 unsigned Opcode = I->getOpcode();
Simon Dardisd9d41f52016-04-05 12:50:29 +0000283 bool canUseShortMicroMipsCTI = false;
Daniel Sanderse8efff32016-03-14 16:24:05 +0000284
Simon Dardisd9d41f52016-04-05 12:50:29 +0000285 if (Subtarget.inMicroMipsMode()) {
286 switch (Opcode) {
287 case Mips::BNE:
Hrvoje Varga2db00ce2016-07-22 07:18:33 +0000288 case Mips::BNE_MM:
Simon Dardisd9d41f52016-04-05 12:50:29 +0000289 case Mips::BEQ:
Hrvoje Varga2db00ce2016-07-22 07:18:33 +0000290 case Mips::BEQ_MM:
Simon Dardisd9d41f52016-04-05 12:50:29 +0000291 // microMIPS has NE,EQ branches that do not have delay slots provided one
292 // of the operands is zero.
293 if (I->getOperand(1).getReg() == Subtarget.getABI().GetZeroReg())
294 canUseShortMicroMipsCTI = true;
295 break;
296 // For microMIPS the PseudoReturn and PseudoIndirectBranch are always
297 // expanded to JR_MM, so they can be replaced with JRC16_MM.
298 case Mips::JR:
299 case Mips::PseudoReturn:
300 case Mips::PseudoIndirectBranch:
Simon Dardisea343152016-08-18 13:22:43 +0000301 case Mips::TAILCALLREG:
Simon Dardisd9d41f52016-04-05 12:50:29 +0000302 canUseShortMicroMipsCTI = true;
303 break;
304 }
305 }
306
Simon Dardis669d8dd2016-05-18 10:38:01 +0000307 // MIPSR6 forbids both operands being the zero register.
308 if (Subtarget.hasMips32r6() && (I->getNumOperands() > 1) &&
309 (I->getOperand(0).isReg() &&
310 (I->getOperand(0).getReg() == Mips::ZERO ||
311 I->getOperand(0).getReg() == Mips::ZERO_64)) &&
312 (I->getOperand(1).isReg() &&
313 (I->getOperand(1).getReg() == Mips::ZERO ||
314 I->getOperand(1).getReg() == Mips::ZERO_64)))
315 return 0;
316
Simon Dardisd9d41f52016-04-05 12:50:29 +0000317 if (Subtarget.hasMips32r6() || canUseShortMicroMipsCTI) {
Daniel Sanderse8efff32016-03-14 16:24:05 +0000318 switch (Opcode) {
319 case Mips::B:
320 return Mips::BC;
321 case Mips::BAL:
322 return Mips::BALC;
323 case Mips::BEQ:
Hrvoje Varga2db00ce2016-07-22 07:18:33 +0000324 case Mips::BEQ_MM:
Simon Dardisd9d41f52016-04-05 12:50:29 +0000325 if (canUseShortMicroMipsCTI)
Daniel Sanderse8efff32016-03-14 16:24:05 +0000326 return Mips::BEQZC_MM;
Simon Dardis03676dc2016-05-31 09:54:55 +0000327 else if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
328 return 0;
329 return Mips::BEQC;
Daniel Sanderse8efff32016-03-14 16:24:05 +0000330 case Mips::BNE:
Hrvoje Varga2db00ce2016-07-22 07:18:33 +0000331 case Mips::BNE_MM:
Simon Dardisd9d41f52016-04-05 12:50:29 +0000332 if (canUseShortMicroMipsCTI)
Daniel Sanderse8efff32016-03-14 16:24:05 +0000333 return Mips::BNEZC_MM;
Simon Dardis03676dc2016-05-31 09:54:55 +0000334 else if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
335 return 0;
336 return Mips::BNEC;
Daniel Sanderse8efff32016-03-14 16:24:05 +0000337 case Mips::BGE:
Simon Dardis669d8dd2016-05-18 10:38:01 +0000338 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
339 return 0;
Daniel Sanderse8efff32016-03-14 16:24:05 +0000340 return Mips::BGEC;
341 case Mips::BGEU:
Simon Dardis669d8dd2016-05-18 10:38:01 +0000342 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
343 return 0;
Daniel Sanderse8efff32016-03-14 16:24:05 +0000344 return Mips::BGEUC;
345 case Mips::BGEZ:
346 return Mips::BGEZC;
347 case Mips::BGTZ:
348 return Mips::BGTZC;
349 case Mips::BLEZ:
350 return Mips::BLEZC;
351 case Mips::BLT:
Simon Dardis669d8dd2016-05-18 10:38:01 +0000352 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
353 return 0;
Daniel Sanderse8efff32016-03-14 16:24:05 +0000354 return Mips::BLTC;
355 case Mips::BLTU:
Simon Dardis669d8dd2016-05-18 10:38:01 +0000356 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
357 return 0;
Daniel Sanderse8efff32016-03-14 16:24:05 +0000358 return Mips::BLTUC;
359 case Mips::BLTZ:
360 return Mips::BLTZC;
Simon Dardis68a204d2016-07-26 10:25:07 +0000361 case Mips::BEQ64:
362 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
363 return 0;
364 return Mips::BEQC64;
365 case Mips::BNE64:
366 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
367 return 0;
368 return Mips::BNEC64;
369 case Mips::BGTZ64:
370 return Mips::BGTZC64;
371 case Mips::BGEZ64:
372 return Mips::BGEZC64;
373 case Mips::BLTZ64:
374 return Mips::BLTZC64;
375 case Mips::BLEZ64:
376 return Mips::BLEZC64;
Simon Dardisd9d41f52016-04-05 12:50:29 +0000377 // For MIPSR6, the instruction 'jic' can be used for these cases. Some
378 // tools will accept 'jrc reg' as an alias for 'jic 0, $reg'.
379 case Mips::JR:
380 case Mips::PseudoReturn:
381 case Mips::PseudoIndirectBranch:
Simon Dardisea343152016-08-18 13:22:43 +0000382 case Mips::TAILCALLREG:
Simon Dardisd9d41f52016-04-05 12:50:29 +0000383 if (canUseShortMicroMipsCTI)
384 return Mips::JRC16_MM;
385 return Mips::JIC;
386 case Mips::JALRPseudo:
387 return Mips::JIALC;
388 case Mips::JR64:
389 case Mips::PseudoReturn64:
390 case Mips::PseudoIndirectBranch64:
Simon Dardisea343152016-08-18 13:22:43 +0000391 case Mips::TAILCALLREG64:
Simon Dardisd9d41f52016-04-05 12:50:29 +0000392 return Mips::JIC64;
393 case Mips::JALR64Pseudo:
394 return Mips::JIALC64;
Simon Dardis669d8dd2016-05-18 10:38:01 +0000395 default:
Daniel Sanderse8efff32016-03-14 16:24:05 +0000396 return 0;
397 }
398 }
399
400 return 0;
401}
402
403/// Predicate for distingushing between control transfer instructions and all
404/// other instructions for handling forbidden slots. Consider inline assembly
405/// as unsafe as well.
406bool MipsInstrInfo::SafeInForbiddenSlot(const MachineInstr &MI) const {
407 if (MI.isInlineAsm())
408 return false;
409
410 return (MI.getDesc().TSFlags & MipsII::IsCTI) == 0;
Daniel Sanderse8efff32016-03-14 16:24:05 +0000411}
412
413/// Predicate for distingushing instructions that have forbidden slots.
414bool MipsInstrInfo::HasForbiddenSlot(const MachineInstr &MI) const {
415 return (MI.getDesc().TSFlags & MipsII::HasForbiddenSlot) != 0;
416}
417
Akira Hatanakaacd1a7d2012-06-14 01:16:45 +0000418/// Return the number of bytes of code the specified instruction may be.
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000419unsigned MipsInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000420 switch (MI.getOpcode()) {
Akira Hatanakaacd1a7d2012-06-14 01:16:45 +0000421 default:
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000422 return MI.getDesc().getSize();
Akira Hatanakaacd1a7d2012-06-14 01:16:45 +0000423 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000424 const MachineFunction *MF = MI.getParent()->getParent();
425 const char *AsmStr = MI.getOperand(0).getSymbolName();
Akira Hatanakaacd1a7d2012-06-14 01:16:45 +0000426 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
427 }
Reed Kotler91ae9822013-10-27 21:57:36 +0000428 case Mips::CONSTPOOL_ENTRY:
429 // If this machine instr is a constant pool entry, its size is recorded as
430 // operand #2.
Duncan P. N. Exon Smith670900b2016-07-15 23:09:47 +0000431 return MI.getOperand(2).getImm();
Akira Hatanakaacd1a7d2012-06-14 01:16:45 +0000432 }
433}
Akira Hatanaka310e26a2013-05-13 17:57:42 +0000434
435MachineInstrBuilder
436MipsInstrInfo::genInstrWithNewOpc(unsigned NewOpc,
437 MachineBasicBlock::iterator I) const {
438 MachineInstrBuilder MIB;
Daniel Sanderse8efff32016-03-14 16:24:05 +0000439
Simon Dardis68a204d2016-07-26 10:25:07 +0000440 // Certain branches have two forms: e.g beq $1, $zero, dest vs beqz $1, dest
Daniel Sanderse8efff32016-03-14 16:24:05 +0000441 // Pick the zero form of the branch for readable assembly and for greater
442 // branch distance in non-microMIPS mode.
Simon Dardis4893aff2016-08-16 17:16:11 +0000443 // Additional MIPSR6 does not permit the use of register $zero for compact
444 // branches.
Simon Dardisd9d41f52016-04-05 12:50:29 +0000445 // FIXME: Certain atomic sequences on mips64 generate 32bit references to
446 // Mips::ZERO, which is incorrect. This test should be updated to use
447 // Subtarget.getABI().GetZeroReg() when those atomic sequences and others
448 // are fixed.
Simon Dardis4893aff2016-08-16 17:16:11 +0000449 int ZeroOperandPosition = -1;
450 bool BranchWithZeroOperand = false;
451 if (I->isBranch() && !I->isPseudo()) {
452 auto TRI = I->getParent()->getParent()->getSubtarget().getRegisterInfo();
453 ZeroOperandPosition = I->findRegisterUseOperandIdx(Mips::ZERO, false, TRI);
454 BranchWithZeroOperand = ZeroOperandPosition != -1;
455 }
Simon Dardisd9d41f52016-04-05 12:50:29 +0000456
457 if (BranchWithZeroOperand) {
Daniel Sanderse8efff32016-03-14 16:24:05 +0000458 switch (NewOpc) {
459 case Mips::BEQC:
460 NewOpc = Mips::BEQZC;
461 break;
462 case Mips::BNEC:
463 NewOpc = Mips::BNEZC;
464 break;
465 case Mips::BGEC:
466 NewOpc = Mips::BGEZC;
467 break;
468 case Mips::BLTC:
469 NewOpc = Mips::BLTZC;
470 break;
Simon Dardis68a204d2016-07-26 10:25:07 +0000471 case Mips::BEQC64:
472 NewOpc = Mips::BEQZC64;
473 break;
474 case Mips::BNEC64:
475 NewOpc = Mips::BNEZC64;
476 break;
Daniel Sanderse8efff32016-03-14 16:24:05 +0000477 }
478 }
479
Akira Hatanaka310e26a2013-05-13 17:57:42 +0000480 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), get(NewOpc));
481
Simon Dardisd9d41f52016-04-05 12:50:29 +0000482 // For MIPSR6 JI*C requires an immediate 0 as an operand, JIALC(64) an
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000483 // immediate 0 as an operand and requires the removal of it's implicit-def %ra
Simon Dardisd9d41f52016-04-05 12:50:29 +0000484 // implicit operand as copying the implicit operations of the instructio we're
485 // looking at will give us the correct flags.
486 if (NewOpc == Mips::JIC || NewOpc == Mips::JIALC || NewOpc == Mips::JIC64 ||
487 NewOpc == Mips::JIALC64) {
488
489 if (NewOpc == Mips::JIALC || NewOpc == Mips::JIALC64)
490 MIB->RemoveOperand(0);
491
492 for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J) {
Diana Picus116bbab2017-01-13 09:58:52 +0000493 MIB.add(I->getOperand(J));
Simon Dardisd9d41f52016-04-05 12:50:29 +0000494 }
495
496 MIB.addImm(0);
497
Simon Dardisd9d41f52016-04-05 12:50:29 +0000498 } else {
Simon Dardisd9d41f52016-04-05 12:50:29 +0000499 for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J) {
Simon Dardis4893aff2016-08-16 17:16:11 +0000500 if (BranchWithZeroOperand && (unsigned)ZeroOperandPosition == J)
501 continue;
502
Diana Picus116bbab2017-01-13 09:58:52 +0000503 MIB.add(I->getOperand(J));
Simon Dardisd9d41f52016-04-05 12:50:29 +0000504 }
505 }
506
507 MIB.copyImplicitOps(*I);
Akira Hatanaka310e26a2013-05-13 17:57:42 +0000508
509 MIB.setMemRefs(I->memoperands_begin(), I->memoperands_end());
510 return MIB;
511}
Petar Jovanovic9bff3b72017-03-31 14:31:55 +0000512
513bool MipsInstrInfo::findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx1,
514 unsigned &SrcOpIdx2) const {
515 assert(!MI.isBundle() &&
516 "TargetInstrInfo::findCommutedOpIndices() can't handle bundles");
517
518 const MCInstrDesc &MCID = MI.getDesc();
519 if (!MCID.isCommutable())
520 return false;
521
522 switch (MI.getOpcode()) {
523 case Mips::DPADD_U_H:
524 case Mips::DPADD_U_W:
525 case Mips::DPADD_U_D:
526 case Mips::DPADD_S_H:
527 case Mips::DPADD_S_W:
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000528 case Mips::DPADD_S_D:
Petar Jovanovic9bff3b72017-03-31 14:31:55 +0000529 // The first operand is both input and output, so it should not commute
530 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3))
531 return false;
532
533 if (!MI.getOperand(SrcOpIdx1).isReg() || !MI.getOperand(SrcOpIdx2).isReg())
534 return false;
535 return true;
536 }
Petar Jovanovic9bff3b72017-03-31 14:31:55 +0000537 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
538}
Simon Dardis28365b32017-09-14 10:58:00 +0000539
540// ins, ext, dext*, dins have the following constraints:
Simon Dardisfd8c65e2017-12-18 15:56:40 +0000541// X <= pos < Y
542// X < size <= Y
543// X < pos+size <= Y
Simon Dardis28365b32017-09-14 10:58:00 +0000544//
Simon Dardisfd8c65e2017-12-18 15:56:40 +0000545// dinsm and dinsu have the following constraints:
546// X <= pos < Y
547// X <= size <= Y
548// X < pos+size <= Y
549//
550// The callee of verifyInsExtInstruction however gives the bounds of
551// dins[um] like the other (d)ins (d)ext(um) instructions, so that this
552// function doesn't have to vary it's behaviour based on the instruction
553// being checked.
Simon Dardis28365b32017-09-14 10:58:00 +0000554static bool verifyInsExtInstruction(const MachineInstr &MI, StringRef &ErrInfo,
555 const int64_t PosLow, const int64_t PosHigh,
556 const int64_t SizeLow,
557 const int64_t SizeHigh,
558 const int64_t BothLow,
559 const int64_t BothHigh) {
560 MachineOperand MOPos = MI.getOperand(2);
561 if (!MOPos.isImm()) {
562 ErrInfo = "Position is not an immediate!";
563 return false;
564 }
565 int64_t Pos = MOPos.getImm();
566 if (!((PosLow <= Pos) && (Pos < PosHigh))) {
567 ErrInfo = "Position operand is out of range!";
568 return false;
569 }
570
571 MachineOperand MOSize = MI.getOperand(3);
572 if (!MOSize.isImm()) {
573 ErrInfo = "Size operand is not an immediate!";
574 return false;
575 }
576 int64_t Size = MOSize.getImm();
577 if (!((SizeLow < Size) && (Size <= SizeHigh))) {
578 ErrInfo = "Size operand is out of range!";
579 return false;
580 }
581
582 if (!((BothLow < (Pos + Size)) && ((Pos + Size) <= BothHigh))) {
583 ErrInfo = "Position + Size is out of range!";
584 return false;
585 }
586
587 return true;
588}
589
590// Perform target specific instruction verification.
591bool MipsInstrInfo::verifyInstruction(const MachineInstr &MI,
592 StringRef &ErrInfo) const {
593 // Verify that ins and ext instructions are well formed.
594 switch (MI.getOpcode()) {
595 case Mips::EXT:
596 case Mips::EXT_MM:
597 case Mips::INS:
598 case Mips::INS_MM:
599 case Mips::DINS:
Simon Dardis28365b32017-09-14 10:58:00 +0000600 return verifyInsExtInstruction(MI, ErrInfo, 0, 32, 0, 32, 0, 32);
601 case Mips::DINSM:
Simon Dardisfd8c65e2017-12-18 15:56:40 +0000602 // The ISA spec has a subtle difference difference between dinsm and dextm
603 // in that it says:
604 // 2 <= size <= 64 for 'dinsm' but 'dextm' has 32 < size <= 64.
605 // To make the bounds checks similar, the range 1 < size <= 64 is checked
606 // for 'dinsm'.
Simon Dardis28365b32017-09-14 10:58:00 +0000607 return verifyInsExtInstruction(MI, ErrInfo, 0, 32, 1, 64, 32, 64);
608 case Mips::DINSU:
Simon Dardisfd8c65e2017-12-18 15:56:40 +0000609 // The ISA spec has a subtle difference between dinsu and dextu in that
610 // the size range of dinsu is specified as 1 <= size <= 32 whereas size
611 // for dextu is 0 < size <= 32. The range checked for dinsu here is
612 // 0 < size <= 32, which is equivalent and similar to dextu.
613 return verifyInsExtInstruction(MI, ErrInfo, 32, 64, 0, 32, 32, 64);
Simon Dardis28365b32017-09-14 10:58:00 +0000614 case Mips::DEXT:
Simon Dardis28365b32017-09-14 10:58:00 +0000615 return verifyInsExtInstruction(MI, ErrInfo, 0, 32, 0, 32, 0, 63);
616 case Mips::DEXTM:
Simon Dardis28365b32017-09-14 10:58:00 +0000617 return verifyInsExtInstruction(MI, ErrInfo, 0, 32, 32, 64, 32, 64);
618 case Mips::DEXTU:
Simon Dardis28365b32017-09-14 10:58:00 +0000619 return verifyInsExtInstruction(MI, ErrInfo, 32, 64, 0, 32, 32, 64);
620 default:
621 return true;
622 }
623
624 return true;
625}
626
Simon Dardis41851e32017-10-11 11:11:35 +0000627std::pair<unsigned, unsigned>
628MipsInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
629 return std::make_pair(TF, 0u);
630}
631
632ArrayRef<std::pair<unsigned, const char*>>
633MipsInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
634 using namespace MipsII;
635
636 static const std::pair<unsigned, const char*> Flags[] = {
637 {MO_GOT, "mips-got"},
638 {MO_GOT_CALL, "mips-got-call"},
639 {MO_GPREL, "mips-gprel"},
640 {MO_ABS_HI, "mips-abs-hi"},
641 {MO_ABS_LO, "mips-abs-lo"},
642 {MO_TLSGD, "mips-tlsgd"},
643 {MO_TLSLDM, "mips-tlsldm"},
644 {MO_DTPREL_HI, "mips-dtprel-hi"},
645 {MO_DTPREL_LO, "mips-dtprel-lo"},
646 {MO_GOTTPREL, "mips-gottprel"},
647 {MO_TPREL_HI, "mips-tprel-hi"},
648 {MO_TPREL_LO, "mips-tprel-lo"},
649 {MO_GPOFF_HI, "mips-gpoff-hi"},
650 {MO_GPOFF_LO, "mips-gpoff-lo"},
651 {MO_GOT_DISP, "mips-got-disp"},
652 {MO_GOT_PAGE, "mips-got-page"},
653 {MO_GOT_OFST, "mips-got-ofst"},
654 {MO_HIGHER, "mips-higher"},
655 {MO_HIGHEST, "mips-highest"},
656 {MO_GOT_HI16, "mips-got-hi16"},
657 {MO_GOT_LO16, "mips-got-lo16"},
658 {MO_CALL_HI16, "mips-call-hi16"},
659 {MO_CALL_LO16, "mips-call-lo16"}
660 };
661 return makeArrayRef(Flags);
662}