1 //===- MipsInstrInfo.cpp - Mips Instruction Information -------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains the Mips implementation of the TargetInstrInfo class.
11 //===----------------------------------------------------------------------===//
13 #include "MipsInstrInfo.h"
14 #include "MCTargetDesc/MipsBaseInfo.h"
15 #include "MCTargetDesc/MipsMCTargetDesc.h"
16 #include "MipsSubtarget.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstr.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineOperand.h"
24 #include "llvm/CodeGen/TargetOpcodes.h"
25 #include "llvm/CodeGen/TargetSubtargetInfo.h"
26 #include "llvm/IR/DebugInfoMetadata.h"
27 #include "llvm/IR/DebugLoc.h"
28 #include "llvm/MC/MCInstrDesc.h"
29 #include "llvm/Target/TargetMachine.h"
34 #define GET_INSTRINFO_CTOR_DTOR
35 #include "MipsGenInstrInfo.inc"
37 // Pin the vtable to this file.
38 void MipsInstrInfo::anchor() {}
40 MipsInstrInfo::MipsInstrInfo(const MipsSubtarget
&STI
, unsigned UncondBr
)
41 : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN
, Mips::ADJCALLSTACKUP
),
42 Subtarget(STI
), UncondBrOpc(UncondBr
) {}
44 const MipsInstrInfo
*MipsInstrInfo::create(MipsSubtarget
&STI
) {
45 if (STI
.inMips16Mode())
46 return createMips16InstrInfo(STI
);
48 return createMipsSEInstrInfo(STI
);
51 bool MipsInstrInfo::isZeroImm(const MachineOperand
&op
) const {
52 return op
.isImm() && op
.getImm() == 0;
55 /// insertNoop - If data hazard condition is found insert the target nop
58 insertNoop(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
) const
61 BuildMI(MBB
, MI
, DL
, get(Mips::NOP
));
64 MachineInstrBuilder
MipsInstrInfo::insertNop(MachineBasicBlock
&MBB
,
65 MachineBasicBlock::iterator MI
,
67 assert(!Subtarget
.inMips16Mode() &&
68 "insertNop does not support MIPS16e mode at this time");
69 const unsigned MMOpc
=
70 Subtarget
.hasMips32r6() ? Mips::SLL_MMR6
: Mips::SLL_MM
;
72 Subtarget
.inMicroMipsMode() ? MMOpc
: (unsigned)Mips::SLL
;
73 return BuildMI(MBB
, MI
, DL
, get(Opc
), Mips::ZERO
)
79 MipsInstrInfo::GetMemOperand(MachineBasicBlock
&MBB
, int FI
,
80 MachineMemOperand::Flags Flags
) const {
81 MachineFunction
&MF
= *MBB
.getParent();
82 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
84 return MF
.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF
, FI
),
85 Flags
, MFI
.getObjectSize(FI
),
86 MFI
.getObjectAlign(FI
));
89 //===----------------------------------------------------------------------===//
91 //===----------------------------------------------------------------------===//
93 void MipsInstrInfo::AnalyzeCondBr(const MachineInstr
*Inst
, unsigned Opc
,
94 MachineBasicBlock
*&BB
,
95 SmallVectorImpl
<MachineOperand
> &Cond
) const {
96 assert(getAnalyzableBrOpc(Opc
) && "Not an analyzable branch");
97 int NumOp
= Inst
->getNumExplicitOperands();
99 // for both int and fp branches, the last explicit operand is the
101 BB
= Inst
->getOperand(NumOp
-1).getMBB();
102 Cond
.push_back(MachineOperand::CreateImm(Opc
));
104 for (int i
= 0; i
< NumOp
-1; i
++)
105 Cond
.push_back(Inst
->getOperand(i
));
108 bool MipsInstrInfo::analyzeBranch(MachineBasicBlock
&MBB
,
109 MachineBasicBlock
*&TBB
,
110 MachineBasicBlock
*&FBB
,
111 SmallVectorImpl
<MachineOperand
> &Cond
,
112 bool AllowModify
) const {
113 SmallVector
<MachineInstr
*, 2> BranchInstrs
;
114 BranchType BT
= analyzeBranch(MBB
, TBB
, FBB
, Cond
, AllowModify
, BranchInstrs
);
116 return (BT
== BT_None
) || (BT
== BT_Indirect
);
119 void MipsInstrInfo::BuildCondBr(MachineBasicBlock
&MBB
, MachineBasicBlock
*TBB
,
121 ArrayRef
<MachineOperand
> Cond
) const {
122 unsigned Opc
= Cond
[0].getImm();
123 const MCInstrDesc
&MCID
= get(Opc
);
124 MachineInstrBuilder MIB
= BuildMI(&MBB
, DL
, MCID
);
126 for (unsigned i
= 1; i
< Cond
.size(); ++i
) {
127 assert((Cond
[i
].isImm() || Cond
[i
].isReg()) &&
128 "Cannot copy operand for conditional branch!");
134 unsigned MipsInstrInfo::insertBranch(MachineBasicBlock
&MBB
,
135 MachineBasicBlock
*TBB
,
136 MachineBasicBlock
*FBB
,
137 ArrayRef
<MachineOperand
> Cond
,
139 int *BytesAdded
) const {
140 // Shouldn't be a fall through.
141 assert(TBB
&& "insertBranch must not be told to insert a fallthrough");
142 assert(!BytesAdded
&& "code size not handled");
144 // # of condition operands:
145 // Unconditional branches: 0
146 // Floating point branches: 1 (opc)
147 // Int BranchZero: 2 (opc, reg)
148 // Int Branch: 3 (opc, reg0, reg1)
149 assert((Cond
.size() <= 3) &&
150 "# of Mips branch conditions must be <= 3!");
152 // Two-way Conditional branch.
154 BuildCondBr(MBB
, TBB
, DL
, Cond
);
155 BuildMI(&MBB
, DL
, get(UncondBrOpc
)).addMBB(FBB
);
160 // Unconditional branch.
162 BuildMI(&MBB
, DL
, get(UncondBrOpc
)).addMBB(TBB
);
163 else // Conditional branch.
164 BuildCondBr(MBB
, TBB
, DL
, Cond
);
168 unsigned MipsInstrInfo::removeBranch(MachineBasicBlock
&MBB
,
169 int *BytesRemoved
) const {
170 assert(!BytesRemoved
&& "code size not handled");
172 MachineBasicBlock::reverse_iterator I
= MBB
.rbegin(), REnd
= MBB
.rend();
173 unsigned removed
= 0;
175 // Up to 2 branches are removed.
176 // Note that indirect branches are not removed.
177 while (I
!= REnd
&& removed
< 2) {
178 // Skip past debug instructions.
179 if (I
->isDebugInstr()) {
183 if (!getAnalyzableBrOpc(I
->getOpcode()))
185 // Remove the branch.
186 I
->eraseFromParent();
194 /// reverseBranchCondition - Return the inverse opcode of the
195 /// specified Branch instruction.
196 bool MipsInstrInfo::reverseBranchCondition(
197 SmallVectorImpl
<MachineOperand
> &Cond
) const {
198 assert( (Cond
.size() && Cond
.size() <= 3) &&
199 "Invalid Mips branch condition!");
200 Cond
[0].setImm(getOppositeBranchOpc(Cond
[0].getImm()));
204 MipsInstrInfo::BranchType
MipsInstrInfo::analyzeBranch(
205 MachineBasicBlock
&MBB
, MachineBasicBlock
*&TBB
, MachineBasicBlock
*&FBB
,
206 SmallVectorImpl
<MachineOperand
> &Cond
, bool AllowModify
,
207 SmallVectorImpl
<MachineInstr
*> &BranchInstrs
) const {
208 MachineBasicBlock::reverse_iterator I
= MBB
.rbegin(), REnd
= MBB
.rend();
210 // Skip all the debug instructions.
211 while (I
!= REnd
&& I
->isDebugInstr())
214 if (I
== REnd
|| !isUnpredicatedTerminator(*I
)) {
215 // This block ends with no branches (it just falls through to its succ).
216 // Leave TBB/FBB null.
221 MachineInstr
*LastInst
= &*I
;
222 unsigned LastOpc
= LastInst
->getOpcode();
223 BranchInstrs
.push_back(LastInst
);
225 // Not an analyzable branch (e.g., indirect jump).
226 if (!getAnalyzableBrOpc(LastOpc
))
227 return LastInst
->isIndirectBranch() ? BT_Indirect
: BT_None
;
229 // Get the second to last instruction in the block.
230 unsigned SecondLastOpc
= 0;
231 MachineInstr
*SecondLastInst
= nullptr;
233 // Skip past any debug instruction to see if the second last actual
236 while (I
!= REnd
&& I
->isDebugInstr())
240 SecondLastInst
= &*I
;
241 SecondLastOpc
= getAnalyzableBrOpc(SecondLastInst
->getOpcode());
243 // Not an analyzable branch (must be an indirect jump).
244 if (isUnpredicatedTerminator(*SecondLastInst
) && !SecondLastOpc
)
248 // If there is only one terminator instruction, process it.
249 if (!SecondLastOpc
) {
250 // Unconditional branch.
251 if (LastInst
->isUnconditionalBranch()) {
252 TBB
= LastInst
->getOperand(0).getMBB();
256 // Conditional branch
257 AnalyzeCondBr(LastInst
, LastOpc
, TBB
, Cond
);
261 // If we reached here, there are two branches.
262 // If there are three terminators, we don't know what sort of block this is.
263 if (++I
!= REnd
&& isUnpredicatedTerminator(*I
))
266 BranchInstrs
.insert(BranchInstrs
.begin(), SecondLastInst
);
268 // If second to last instruction is an unconditional branch,
269 // analyze it and remove the last instruction.
270 if (SecondLastInst
->isUnconditionalBranch()) {
271 // Return if the last instruction cannot be removed.
275 TBB
= SecondLastInst
->getOperand(0).getMBB();
276 LastInst
->eraseFromParent();
277 BranchInstrs
.pop_back();
281 // Conditional branch followed by an unconditional branch.
282 // The last one must be unconditional.
283 if (!LastInst
->isUnconditionalBranch())
286 AnalyzeCondBr(SecondLastInst
, SecondLastOpc
, TBB
, Cond
);
287 FBB
= LastInst
->getOperand(0).getMBB();
289 return BT_CondUncond
;
292 bool MipsInstrInfo::isBranchOffsetInRange(unsigned BranchOpc
,
293 int64_t BrOffset
) const {
298 case Mips::BAL_BR_MM
:
303 case Mips::BEQ
: case Mips::BEQ64
:
305 case Mips::BGEZ
: case Mips::BGEZ64
:
309 case Mips::BGTZ
: case Mips::BGTZ64
:
311 case Mips::BLEZ
: case Mips::BLEZ64
:
313 case Mips::BLTZ
: case Mips::BLTZ64
:
317 case Mips::BNE
: case Mips::BNE64
:
319 return isInt
<18>(BrOffset
);
321 // microMIPSr3 branches
327 case Mips::BGEZAL_MM
:
331 case Mips::BLTZAL_MM
:
335 return isInt
<17>(BrOffset
);
337 // microMIPSR3 short branches.
339 return isInt
<11>(BrOffset
);
341 case Mips::BEQZ16_MM
:
342 case Mips::BNEZ16_MM
:
343 return isInt
<8>(BrOffset
);
348 return isInt
<28>(BrOffset
);
354 case Mips::BEQC
: case Mips::BEQC64
:
355 case Mips::BNEC
: case Mips::BNEC64
:
356 case Mips::BGEC
: case Mips::BGEC64
:
357 case Mips::BGEUC
: case Mips::BGEUC64
:
358 case Mips::BGEZC
: case Mips::BGEZC64
:
359 case Mips::BGTZC
: case Mips::BGTZC64
:
360 case Mips::BLEZC
: case Mips::BLEZC64
:
361 case Mips::BLTC
: case Mips::BLTC64
:
362 case Mips::BLTUC
: case Mips::BLTUC64
:
363 case Mips::BLTZC
: case Mips::BLTZC64
:
372 return isInt
<18>(BrOffset
);
374 case Mips::BEQZC
: case Mips::BEQZC64
:
375 case Mips::BNEZC
: case Mips::BNEZC64
:
376 return isInt
<23>(BrOffset
);
378 // microMIPSR6 branches
379 case Mips::BC16_MMR6
:
380 return isInt
<11>(BrOffset
);
382 case Mips::BEQZC16_MMR6
:
383 case Mips::BNEZC16_MMR6
:
384 return isInt
<8>(BrOffset
);
386 case Mips::BALC_MMR6
:
388 return isInt
<27>(BrOffset
);
390 case Mips::BC1EQZC_MMR6
:
391 case Mips::BC1NEZC_MMR6
:
392 case Mips::BC2EQZC_MMR6
:
393 case Mips::BC2NEZC_MMR6
:
394 case Mips::BGEZALC_MMR6
:
395 case Mips::BEQZALC_MMR6
:
396 case Mips::BGTZALC_MMR6
:
397 case Mips::BLEZALC_MMR6
:
398 case Mips::BLTZALC_MMR6
:
399 case Mips::BNEZALC_MMR6
:
400 case Mips::BNVC_MMR6
:
401 case Mips::BOVC_MMR6
:
402 return isInt
<17>(BrOffset
);
404 case Mips::BEQC_MMR6
:
405 case Mips::BNEC_MMR6
:
406 case Mips::BGEC_MMR6
:
407 case Mips::BGEUC_MMR6
:
408 case Mips::BGEZC_MMR6
:
409 case Mips::BGTZC_MMR6
:
410 case Mips::BLEZC_MMR6
:
411 case Mips::BLTC_MMR6
:
412 case Mips::BLTUC_MMR6
:
413 case Mips::BLTZC_MMR6
:
414 return isInt
<18>(BrOffset
);
416 case Mips::BEQZC_MMR6
:
417 case Mips::BNEZC_MMR6
:
418 return isInt
<23>(BrOffset
);
422 return isInt
<18>(BrOffset
);
423 case Mips::BPOSGE32_MM
:
424 case Mips::BPOSGE32C_MMR3
:
425 return isInt
<17>(BrOffset
);
432 return isInt
<18>(BrOffset
);
445 return isInt
<18>(BrOffset
);
448 llvm_unreachable("Unknown branch instruction!");
451 /// Return the corresponding compact (no delay slot) form of a branch.
452 unsigned MipsInstrInfo::getEquivalentCompactForm(
453 const MachineBasicBlock::iterator I
) const {
454 unsigned Opcode
= I
->getOpcode();
455 bool canUseShortMicroMipsCTI
= false;
457 if (Subtarget
.inMicroMipsMode()) {
463 // microMIPS has NE,EQ branches that do not have delay slots provided one
464 // of the operands is zero.
465 if (I
->getOperand(1).getReg() == Subtarget
.getABI().GetZeroReg())
466 canUseShortMicroMipsCTI
= true;
468 // For microMIPS the PseudoReturn and PseudoIndirectBranch are always
469 // expanded to JR_MM, so they can be replaced with JRC16_MM.
471 case Mips::PseudoReturn
:
472 case Mips::PseudoIndirectBranch
:
473 canUseShortMicroMipsCTI
= true;
478 // MIPSR6 forbids both operands being the zero register.
479 if (Subtarget
.hasMips32r6() && (I
->getNumOperands() > 1) &&
480 (I
->getOperand(0).isReg() &&
481 (I
->getOperand(0).getReg() == Mips::ZERO
||
482 I
->getOperand(0).getReg() == Mips::ZERO_64
)) &&
483 (I
->getOperand(1).isReg() &&
484 (I
->getOperand(1).getReg() == Mips::ZERO
||
485 I
->getOperand(1).getReg() == Mips::ZERO_64
)))
488 if (Subtarget
.hasMips32r6() || canUseShortMicroMipsCTI
) {
496 if (canUseShortMicroMipsCTI
)
497 return Mips::BEQZC_MM
;
498 else if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
503 if (canUseShortMicroMipsCTI
)
504 return Mips::BNEZC_MM
;
505 else if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
509 if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
513 if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
523 if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
527 if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
533 if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
537 if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
541 return Mips::BGTZC64
;
543 return Mips::BGEZC64
;
545 return Mips::BLTZC64
;
547 return Mips::BLEZC64
;
548 // For MIPSR6, the instruction 'jic' can be used for these cases. Some
549 // tools will accept 'jrc reg' as an alias for 'jic 0, $reg'.
551 case Mips::PseudoIndirectBranchR6
:
552 case Mips::PseudoReturn
:
553 case Mips::TAILCALLR6REG
:
554 if (canUseShortMicroMipsCTI
)
555 return Mips::JRC16_MM
;
557 case Mips::JALRPseudo
:
560 case Mips::PseudoIndirectBranch64R6
:
561 case Mips::PseudoReturn64
:
562 case Mips::TAILCALL64R6REG
:
564 case Mips::JALR64Pseudo
:
565 return Mips::JIALC64
;
574 /// Predicate for distingushing between control transfer instructions and all
575 /// other instructions for handling forbidden slots. Consider inline assembly
576 /// as unsafe as well.
577 bool MipsInstrInfo::SafeInForbiddenSlot(const MachineInstr
&MI
) const {
578 if (MI
.isInlineAsm())
581 return (MI
.getDesc().TSFlags
& MipsII::IsCTI
) == 0;
584 bool MipsInstrInfo::SafeInFPUDelaySlot(const MachineInstr
&MIInSlot
,
585 const MachineInstr
&FPUMI
) const {
586 if (MIInSlot
.isInlineAsm())
589 if (HasFPUDelaySlot(MIInSlot
))
592 switch (MIInSlot
.getOpcode()) {
600 for (const MachineOperand
&Op
: FPUMI
.defs()) {
605 std::tie(Reads
, Writes
) = MIInSlot
.readsWritesVirtualRegister(Op
.getReg());
614 /// Predicate for distinguishing instructions that are hazardous in a load delay
615 /// slot. Consider inline assembly as unsafe as well.
616 bool MipsInstrInfo::SafeInLoadDelaySlot(const MachineInstr
&MIInSlot
,
617 const MachineInstr
&LoadMI
) const {
618 if (MIInSlot
.isInlineAsm())
621 return !llvm::any_of(LoadMI
.defs(), [&](const MachineOperand
&Op
) {
622 return Op
.isReg() && MIInSlot
.readsRegister(Op
.getReg(), /*TRI=*/nullptr);
626 /// Predicate for distingushing instructions that have forbidden slots.
627 bool MipsInstrInfo::HasForbiddenSlot(const MachineInstr
&MI
) const {
628 return (MI
.getDesc().TSFlags
& MipsII::HasForbiddenSlot
) != 0;
631 /// Predicate for distingushing instructions that have FPU delay slots.
632 bool MipsInstrInfo::HasFPUDelaySlot(const MachineInstr
&MI
) const {
633 switch (MI
.getOpcode()) {
650 /// Predicate for distingushing instructions that have load delay slots.
651 bool MipsInstrInfo::HasLoadDelaySlot(const MachineInstr
&MI
) const {
652 switch (MI
.getOpcode()) {
666 /// Return the number of bytes of code the specified instruction may be.
667 unsigned MipsInstrInfo::getInstSizeInBytes(const MachineInstr
&MI
) const {
668 switch (MI
.getOpcode()) {
670 return MI
.getDesc().getSize();
671 case TargetOpcode::INLINEASM
:
672 case TargetOpcode::INLINEASM_BR
: { // Inline Asm: Variable size.
673 const MachineFunction
*MF
= MI
.getParent()->getParent();
674 const char *AsmStr
= MI
.getOperand(0).getSymbolName();
675 return getInlineAsmLength(AsmStr
, *MF
->getTarget().getMCAsmInfo());
677 case Mips::CONSTPOOL_ENTRY
:
678 // If this machine instr is a constant pool entry, its size is recorded as
680 return MI
.getOperand(2).getImm();
685 MipsInstrInfo::genInstrWithNewOpc(unsigned NewOpc
,
686 MachineBasicBlock::iterator I
) const {
687 MachineInstrBuilder MIB
;
689 // Certain branches have two forms: e.g beq $1, $zero, dest vs beqz $1, dest
690 // Pick the zero form of the branch for readable assembly and for greater
691 // branch distance in non-microMIPS mode.
692 // Additional MIPSR6 does not permit the use of register $zero for compact
694 // FIXME: Certain atomic sequences on mips64 generate 32bit references to
695 // Mips::ZERO, which is incorrect. This test should be updated to use
696 // Subtarget.getABI().GetZeroReg() when those atomic sequences and others
698 int ZeroOperandPosition
= -1;
699 bool BranchWithZeroOperand
= false;
700 if (I
->isBranch() && !I
->isPseudo()) {
701 auto TRI
= I
->getParent()->getParent()->getSubtarget().getRegisterInfo();
702 ZeroOperandPosition
= I
->findRegisterUseOperandIdx(Mips::ZERO
, TRI
, false);
703 BranchWithZeroOperand
= ZeroOperandPosition
!= -1;
706 if (BranchWithZeroOperand
) {
709 NewOpc
= Mips::BEQZC
;
712 NewOpc
= Mips::BNEZC
;
715 NewOpc
= Mips::BGEZC
;
718 NewOpc
= Mips::BLTZC
;
721 NewOpc
= Mips::BEQZC64
;
724 NewOpc
= Mips::BNEZC64
;
729 MIB
= BuildMI(*I
->getParent(), I
, I
->getDebugLoc(), get(NewOpc
));
731 // For MIPSR6 JI*C requires an immediate 0 as an operand, JIALC(64) an
732 // immediate 0 as an operand and requires the removal of it's implicit-def %ra
733 // implicit operand as copying the implicit operations of the instructio we're
734 // looking at will give us the correct flags.
735 if (NewOpc
== Mips::JIC
|| NewOpc
== Mips::JIALC
|| NewOpc
== Mips::JIC64
||
736 NewOpc
== Mips::JIALC64
) {
738 if (NewOpc
== Mips::JIALC
|| NewOpc
== Mips::JIALC64
)
739 MIB
->removeOperand(0);
741 for (unsigned J
= 0, E
= I
->getDesc().getNumOperands(); J
< E
; ++J
) {
742 MIB
.add(I
->getOperand(J
));
747 // If I has an MCSymbol operand (used by asm printer, to emit R_MIPS_JALR),
748 // add it to the new instruction.
749 for (unsigned J
= I
->getDesc().getNumOperands(), E
= I
->getNumOperands();
751 const MachineOperand
&MO
= I
->getOperand(J
);
752 if (MO
.isMCSymbol() && (MO
.getTargetFlags() & MipsII::MO_JALR
))
753 MIB
.addSym(MO
.getMCSymbol(), MipsII::MO_JALR
);
758 for (unsigned J
= 0, E
= I
->getDesc().getNumOperands(); J
< E
; ++J
) {
759 if (BranchWithZeroOperand
&& (unsigned)ZeroOperandPosition
== J
)
762 MIB
.add(I
->getOperand(J
));
766 MIB
.copyImplicitOps(*I
);
767 MIB
.cloneMemRefs(*I
);
771 bool MipsInstrInfo::findCommutedOpIndices(const MachineInstr
&MI
,
773 unsigned &SrcOpIdx2
) const {
774 assert(!MI
.isBundle() &&
775 "TargetInstrInfo::findCommutedOpIndices() can't handle bundles");
777 const MCInstrDesc
&MCID
= MI
.getDesc();
778 if (!MCID
.isCommutable())
781 switch (MI
.getOpcode()) {
782 case Mips::DPADD_U_H
:
783 case Mips::DPADD_U_W
:
784 case Mips::DPADD_U_D
:
785 case Mips::DPADD_S_H
:
786 case Mips::DPADD_S_W
:
787 case Mips::DPADD_S_D
:
788 // The first operand is both input and output, so it should not commute
789 if (!fixCommutedOpIndices(SrcOpIdx1
, SrcOpIdx2
, 2, 3))
792 if (!MI
.getOperand(SrcOpIdx1
).isReg() || !MI
.getOperand(SrcOpIdx2
).isReg())
796 return TargetInstrInfo::findCommutedOpIndices(MI
, SrcOpIdx1
, SrcOpIdx2
);
799 // ins, ext, dext*, dins have the following constraints:
804 // dinsm and dinsu have the following constraints:
809 // The callee of verifyInsExtInstruction however gives the bounds of
810 // dins[um] like the other (d)ins (d)ext(um) instructions, so that this
811 // function doesn't have to vary it's behaviour based on the instruction
813 static bool verifyInsExtInstruction(const MachineInstr
&MI
, StringRef
&ErrInfo
,
814 const int64_t PosLow
, const int64_t PosHigh
,
815 const int64_t SizeLow
,
816 const int64_t SizeHigh
,
817 const int64_t BothLow
,
818 const int64_t BothHigh
) {
819 MachineOperand MOPos
= MI
.getOperand(2);
820 if (!MOPos
.isImm()) {
821 ErrInfo
= "Position is not an immediate!";
824 int64_t Pos
= MOPos
.getImm();
825 if (!((PosLow
<= Pos
) && (Pos
< PosHigh
))) {
826 ErrInfo
= "Position operand is out of range!";
830 MachineOperand MOSize
= MI
.getOperand(3);
831 if (!MOSize
.isImm()) {
832 ErrInfo
= "Size operand is not an immediate!";
835 int64_t Size
= MOSize
.getImm();
836 if (!((SizeLow
< Size
) && (Size
<= SizeHigh
))) {
837 ErrInfo
= "Size operand is out of range!";
841 if (!((BothLow
< (Pos
+ Size
)) && ((Pos
+ Size
) <= BothHigh
))) {
842 ErrInfo
= "Position + Size is out of range!";
849 // Perform target specific instruction verification.
850 bool MipsInstrInfo::verifyInstruction(const MachineInstr
&MI
,
851 StringRef
&ErrInfo
) const {
852 // Verify that ins and ext instructions are well formed.
853 switch (MI
.getOpcode()) {
859 return verifyInsExtInstruction(MI
, ErrInfo
, 0, 32, 0, 32, 0, 32);
861 // The ISA spec has a subtle difference between dinsm and dextm
863 // 2 <= size <= 64 for 'dinsm' but 'dextm' has 32 < size <= 64.
864 // To make the bounds checks similar, the range 1 < size <= 64 is checked
866 return verifyInsExtInstruction(MI
, ErrInfo
, 0, 32, 1, 64, 32, 64);
868 // The ISA spec has a subtle difference between dinsu and dextu in that
869 // the size range of dinsu is specified as 1 <= size <= 32 whereas size
870 // for dextu is 0 < size <= 32. The range checked for dinsu here is
871 // 0 < size <= 32, which is equivalent and similar to dextu.
872 return verifyInsExtInstruction(MI
, ErrInfo
, 32, 64, 0, 32, 32, 64);
874 return verifyInsExtInstruction(MI
, ErrInfo
, 0, 32, 0, 32, 0, 63);
876 return verifyInsExtInstruction(MI
, ErrInfo
, 0, 32, 32, 64, 32, 64);
878 return verifyInsExtInstruction(MI
, ErrInfo
, 32, 64, 0, 32, 32, 64);
879 case Mips::TAILCALLREG
:
880 case Mips::PseudoIndirectBranch
:
885 case Mips::JALRPseudo
:
886 if (!Subtarget
.useIndirectJumpsHazard())
889 ErrInfo
= "invalid instruction when using jump guards!";
898 std::pair
<unsigned, unsigned>
899 MipsInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF
) const {
900 return std::make_pair(TF
, 0u);
903 ArrayRef
<std::pair
<unsigned, const char*>>
904 MipsInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
905 using namespace MipsII
;
907 static const std::pair
<unsigned, const char*> Flags
[] = {
908 {MO_GOT
, "mips-got"},
909 {MO_GOT_CALL
, "mips-got-call"},
910 {MO_GPREL
, "mips-gprel"},
911 {MO_ABS_HI
, "mips-abs-hi"},
912 {MO_ABS_LO
, "mips-abs-lo"},
913 {MO_TLSGD
, "mips-tlsgd"},
914 {MO_TLSLDM
, "mips-tlsldm"},
915 {MO_DTPREL_HI
, "mips-dtprel-hi"},
916 {MO_DTPREL_LO
, "mips-dtprel-lo"},
917 {MO_GOTTPREL
, "mips-gottprel"},
918 {MO_TPREL_HI
, "mips-tprel-hi"},
919 {MO_TPREL_LO
, "mips-tprel-lo"},
920 {MO_GPOFF_HI
, "mips-gpoff-hi"},
921 {MO_GPOFF_LO
, "mips-gpoff-lo"},
922 {MO_GOT_DISP
, "mips-got-disp"},
923 {MO_GOT_PAGE
, "mips-got-page"},
924 {MO_GOT_OFST
, "mips-got-ofst"},
925 {MO_HIGHER
, "mips-higher"},
926 {MO_HIGHEST
, "mips-highest"},
927 {MO_GOT_HI16
, "mips-got-hi16"},
928 {MO_GOT_LO16
, "mips-got-lo16"},
929 {MO_CALL_HI16
, "mips-call-hi16"},
930 {MO_CALL_LO16
, "mips-call-lo16"},
931 {MO_JALR
, "mips-jalr"}
933 return ArrayRef(Flags
);
936 std::optional
<ParamLoadedValue
>
937 MipsInstrInfo::describeLoadedValue(const MachineInstr
&MI
, Register Reg
) const {
939 DIExpression::get(MI
.getMF()->getFunction().getContext(), {});
941 // TODO: Special MIPS instructions that need to be described separately.
942 if (auto RegImm
= isAddImmediate(MI
, Reg
)) {
943 Register SrcReg
= RegImm
->Reg
;
944 int64_t Offset
= RegImm
->Imm
;
945 // When SrcReg is $zero, treat loaded value as immediate only.
946 // Ex. $a2 = ADDiu $zero, 10
947 if (SrcReg
== Mips::ZERO
|| SrcReg
== Mips::ZERO_64
) {
948 return ParamLoadedValue(MI
.getOperand(2), Expr
);
950 Expr
= DIExpression::prepend(Expr
, DIExpression::ApplyOffset
, Offset
);
951 return ParamLoadedValue(MachineOperand::CreateReg(SrcReg
, false), Expr
);
952 } else if (auto DestSrc
= isCopyInstr(MI
)) {
953 const MachineFunction
*MF
= MI
.getMF();
954 const TargetRegisterInfo
*TRI
= MF
->getSubtarget().getRegisterInfo();
955 Register DestReg
= DestSrc
->Destination
->getReg();
956 // TODO: Handle cases where the Reg is sub- or super-register of the
958 if (TRI
->isSuperRegister(Reg
, DestReg
) || TRI
->isSubRegister(Reg
, DestReg
))
962 return TargetInstrInfo::describeLoadedValue(MI
, Reg
);
965 std::optional
<RegImmPair
> MipsInstrInfo::isAddImmediate(const MachineInstr
&MI
,
966 Register Reg
) const {
967 // TODO: Handle cases where Reg is a super- or sub-register of the
968 // destination register.
969 const MachineOperand
&Op0
= MI
.getOperand(0);
970 if (!Op0
.isReg() || Reg
!= Op0
.getReg())
973 switch (MI
.getOpcode()) {
976 const MachineOperand
&Dop
= MI
.getOperand(0);
977 const MachineOperand
&Sop1
= MI
.getOperand(1);
978 const MachineOperand
&Sop2
= MI
.getOperand(2);
979 // Value is sum of register and immediate. Immediate value could be
980 // global string address which is not supported.
981 if (Dop
.isReg() && Sop1
.isReg() && Sop2
.isImm())
982 return RegImmPair
{Sop1
.getReg(), Sop2
.getImm()};
983 // TODO: Handle case where Sop1 is a frame-index.