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
57 // FIXME: This appears to be dead code.
59 insertNoop(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
) const
62 BuildMI(MBB
, MI
, DL
, get(Mips::NOP
));
66 MipsInstrInfo::GetMemOperand(MachineBasicBlock
&MBB
, int FI
,
67 MachineMemOperand::Flags Flags
) const {
68 MachineFunction
&MF
= *MBB
.getParent();
69 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
71 return MF
.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF
, FI
),
72 Flags
, MFI
.getObjectSize(FI
),
73 MFI
.getObjectAlign(FI
));
76 //===----------------------------------------------------------------------===//
78 //===----------------------------------------------------------------------===//
80 void MipsInstrInfo::AnalyzeCondBr(const MachineInstr
*Inst
, unsigned Opc
,
81 MachineBasicBlock
*&BB
,
82 SmallVectorImpl
<MachineOperand
> &Cond
) const {
83 assert(getAnalyzableBrOpc(Opc
) && "Not an analyzable branch");
84 int NumOp
= Inst
->getNumExplicitOperands();
86 // for both int and fp branches, the last explicit operand is the
88 BB
= Inst
->getOperand(NumOp
-1).getMBB();
89 Cond
.push_back(MachineOperand::CreateImm(Opc
));
91 for (int i
= 0; i
< NumOp
-1; i
++)
92 Cond
.push_back(Inst
->getOperand(i
));
95 bool MipsInstrInfo::analyzeBranch(MachineBasicBlock
&MBB
,
96 MachineBasicBlock
*&TBB
,
97 MachineBasicBlock
*&FBB
,
98 SmallVectorImpl
<MachineOperand
> &Cond
,
99 bool AllowModify
) const {
100 SmallVector
<MachineInstr
*, 2> BranchInstrs
;
101 BranchType BT
= analyzeBranch(MBB
, TBB
, FBB
, Cond
, AllowModify
, BranchInstrs
);
103 return (BT
== BT_None
) || (BT
== BT_Indirect
);
106 void MipsInstrInfo::BuildCondBr(MachineBasicBlock
&MBB
, MachineBasicBlock
*TBB
,
108 ArrayRef
<MachineOperand
> Cond
) const {
109 unsigned Opc
= Cond
[0].getImm();
110 const MCInstrDesc
&MCID
= get(Opc
);
111 MachineInstrBuilder MIB
= BuildMI(&MBB
, DL
, MCID
);
113 for (unsigned i
= 1; i
< Cond
.size(); ++i
) {
114 assert((Cond
[i
].isImm() || Cond
[i
].isReg()) &&
115 "Cannot copy operand for conditional branch!");
121 unsigned MipsInstrInfo::insertBranch(MachineBasicBlock
&MBB
,
122 MachineBasicBlock
*TBB
,
123 MachineBasicBlock
*FBB
,
124 ArrayRef
<MachineOperand
> Cond
,
126 int *BytesAdded
) const {
127 // Shouldn't be a fall through.
128 assert(TBB
&& "insertBranch must not be told to insert a fallthrough");
129 assert(!BytesAdded
&& "code size not handled");
131 // # 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!");
139 // Two-way Conditional branch.
141 BuildCondBr(MBB
, TBB
, DL
, Cond
);
142 BuildMI(&MBB
, DL
, get(UncondBrOpc
)).addMBB(FBB
);
147 // Unconditional branch.
149 BuildMI(&MBB
, DL
, get(UncondBrOpc
)).addMBB(TBB
);
150 else // Conditional branch.
151 BuildCondBr(MBB
, TBB
, DL
, Cond
);
155 unsigned MipsInstrInfo::removeBranch(MachineBasicBlock
&MBB
,
156 int *BytesRemoved
) const {
157 assert(!BytesRemoved
&& "code size not handled");
159 MachineBasicBlock::reverse_iterator I
= MBB
.rbegin(), REnd
= MBB
.rend();
160 unsigned removed
= 0;
162 // Up to 2 branches are removed.
163 // Note that indirect branches are not removed.
164 while (I
!= REnd
&& removed
< 2) {
165 // Skip past debug instructions.
166 if (I
->isDebugInstr()) {
170 if (!getAnalyzableBrOpc(I
->getOpcode()))
172 // Remove the branch.
173 I
->eraseFromParent();
181 /// reverseBranchCondition - Return the inverse opcode of the
182 /// specified Branch instruction.
183 bool MipsInstrInfo::reverseBranchCondition(
184 SmallVectorImpl
<MachineOperand
> &Cond
) const {
185 assert( (Cond
.size() && Cond
.size() <= 3) &&
186 "Invalid Mips branch condition!");
187 Cond
[0].setImm(getOppositeBranchOpc(Cond
[0].getImm()));
191 MipsInstrInfo::BranchType
MipsInstrInfo::analyzeBranch(
192 MachineBasicBlock
&MBB
, MachineBasicBlock
*&TBB
, MachineBasicBlock
*&FBB
,
193 SmallVectorImpl
<MachineOperand
> &Cond
, bool AllowModify
,
194 SmallVectorImpl
<MachineInstr
*> &BranchInstrs
) const {
195 MachineBasicBlock::reverse_iterator I
= MBB
.rbegin(), REnd
= MBB
.rend();
197 // Skip all the debug instructions.
198 while (I
!= REnd
&& I
->isDebugInstr())
201 if (I
== REnd
|| !isUnpredicatedTerminator(*I
)) {
202 // This block ends with no branches (it just falls through to its succ).
203 // Leave TBB/FBB null.
208 MachineInstr
*LastInst
= &*I
;
209 unsigned LastOpc
= LastInst
->getOpcode();
210 BranchInstrs
.push_back(LastInst
);
212 // Not an analyzable branch (e.g., indirect jump).
213 if (!getAnalyzableBrOpc(LastOpc
))
214 return LastInst
->isIndirectBranch() ? BT_Indirect
: BT_None
;
216 // Get the second to last instruction in the block.
217 unsigned SecondLastOpc
= 0;
218 MachineInstr
*SecondLastInst
= nullptr;
220 // Skip past any debug instruction to see if the second last actual
223 while (I
!= REnd
&& I
->isDebugInstr())
227 SecondLastInst
= &*I
;
228 SecondLastOpc
= getAnalyzableBrOpc(SecondLastInst
->getOpcode());
230 // Not an analyzable branch (must be an indirect jump).
231 if (isUnpredicatedTerminator(*SecondLastInst
) && !SecondLastOpc
)
235 // If there is only one terminator instruction, process it.
236 if (!SecondLastOpc
) {
237 // Unconditional branch.
238 if (LastInst
->isUnconditionalBranch()) {
239 TBB
= LastInst
->getOperand(0).getMBB();
243 // Conditional branch
244 AnalyzeCondBr(LastInst
, LastOpc
, TBB
, Cond
);
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.
250 if (++I
!= REnd
&& isUnpredicatedTerminator(*I
))
253 BranchInstrs
.insert(BranchInstrs
.begin(), SecondLastInst
);
255 // If second to last instruction is an unconditional branch,
256 // analyze it and remove the last instruction.
257 if (SecondLastInst
->isUnconditionalBranch()) {
258 // Return if the last instruction cannot be removed.
262 TBB
= SecondLastInst
->getOperand(0).getMBB();
263 LastInst
->eraseFromParent();
264 BranchInstrs
.pop_back();
268 // Conditional branch followed by an unconditional branch.
269 // The last one must be unconditional.
270 if (!LastInst
->isUnconditionalBranch())
273 AnalyzeCondBr(SecondLastInst
, SecondLastOpc
, TBB
, Cond
);
274 FBB
= LastInst
->getOperand(0).getMBB();
276 return BT_CondUncond
;
279 bool MipsInstrInfo::isBranchOffsetInRange(unsigned BranchOpc
,
280 int64_t BrOffset
) const {
285 case Mips::BAL_BR_MM
:
290 case Mips::BEQ
: case Mips::BEQ64
:
292 case Mips::BGEZ
: case Mips::BGEZ64
:
296 case Mips::BGTZ
: case Mips::BGTZ64
:
298 case Mips::BLEZ
: case Mips::BLEZ64
:
300 case Mips::BLTZ
: case Mips::BLTZ64
:
304 case Mips::BNE
: case Mips::BNE64
:
306 return isInt
<18>(BrOffset
);
308 // microMIPSr3 branches
314 case Mips::BGEZAL_MM
:
318 case Mips::BLTZAL_MM
:
322 return isInt
<17>(BrOffset
);
324 // microMIPSR3 short branches.
326 return isInt
<11>(BrOffset
);
328 case Mips::BEQZ16_MM
:
329 case Mips::BNEZ16_MM
:
330 return isInt
<8>(BrOffset
);
335 return isInt
<28>(BrOffset
);
341 case Mips::BEQC
: case Mips::BEQC64
:
342 case Mips::BNEC
: case Mips::BNEC64
:
343 case Mips::BGEC
: case Mips::BGEC64
:
344 case Mips::BGEUC
: case Mips::BGEUC64
:
345 case Mips::BGEZC
: case Mips::BGEZC64
:
346 case Mips::BGTZC
: case Mips::BGTZC64
:
347 case Mips::BLEZC
: case Mips::BLEZC64
:
348 case Mips::BLTC
: case Mips::BLTC64
:
349 case Mips::BLTUC
: case Mips::BLTUC64
:
350 case Mips::BLTZC
: case Mips::BLTZC64
:
359 return isInt
<18>(BrOffset
);
361 case Mips::BEQZC
: case Mips::BEQZC64
:
362 case Mips::BNEZC
: case Mips::BNEZC64
:
363 return isInt
<23>(BrOffset
);
365 // microMIPSR6 branches
366 case Mips::BC16_MMR6
:
367 return isInt
<11>(BrOffset
);
369 case Mips::BEQZC16_MMR6
:
370 case Mips::BNEZC16_MMR6
:
371 return isInt
<8>(BrOffset
);
373 case Mips::BALC_MMR6
:
375 return isInt
<27>(BrOffset
);
377 case Mips::BC1EQZC_MMR6
:
378 case Mips::BC1NEZC_MMR6
:
379 case Mips::BC2EQZC_MMR6
:
380 case Mips::BC2NEZC_MMR6
:
381 case Mips::BGEZALC_MMR6
:
382 case Mips::BEQZALC_MMR6
:
383 case Mips::BGTZALC_MMR6
:
384 case Mips::BLEZALC_MMR6
:
385 case Mips::BLTZALC_MMR6
:
386 case Mips::BNEZALC_MMR6
:
387 case Mips::BNVC_MMR6
:
388 case Mips::BOVC_MMR6
:
389 return isInt
<17>(BrOffset
);
391 case Mips::BEQC_MMR6
:
392 case Mips::BNEC_MMR6
:
393 case Mips::BGEC_MMR6
:
394 case Mips::BGEUC_MMR6
:
395 case Mips::BGEZC_MMR6
:
396 case Mips::BGTZC_MMR6
:
397 case Mips::BLEZC_MMR6
:
398 case Mips::BLTC_MMR6
:
399 case Mips::BLTUC_MMR6
:
400 case Mips::BLTZC_MMR6
:
401 return isInt
<18>(BrOffset
);
403 case Mips::BEQZC_MMR6
:
404 case Mips::BNEZC_MMR6
:
405 return isInt
<23>(BrOffset
);
409 return isInt
<18>(BrOffset
);
410 case Mips::BPOSGE32_MM
:
411 case Mips::BPOSGE32C_MMR3
:
412 return isInt
<17>(BrOffset
);
419 return isInt
<18>(BrOffset
);
432 return isInt
<18>(BrOffset
);
435 llvm_unreachable("Unknown branch instruction!");
438 /// Return the corresponding compact (no delay slot) form of a branch.
439 unsigned MipsInstrInfo::getEquivalentCompactForm(
440 const MachineBasicBlock::iterator I
) const {
441 unsigned Opcode
= I
->getOpcode();
442 bool canUseShortMicroMipsCTI
= false;
444 if (Subtarget
.inMicroMipsMode()) {
450 // microMIPS has NE,EQ branches that do not have delay slots provided one
451 // of the operands is zero.
452 if (I
->getOperand(1).getReg() == Subtarget
.getABI().GetZeroReg())
453 canUseShortMicroMipsCTI
= true;
455 // For microMIPS the PseudoReturn and PseudoIndirectBranch are always
456 // expanded to JR_MM, so they can be replaced with JRC16_MM.
458 case Mips::PseudoReturn
:
459 case Mips::PseudoIndirectBranch
:
460 canUseShortMicroMipsCTI
= true;
465 // MIPSR6 forbids both operands being the zero register.
466 if (Subtarget
.hasMips32r6() && (I
->getNumOperands() > 1) &&
467 (I
->getOperand(0).isReg() &&
468 (I
->getOperand(0).getReg() == Mips::ZERO
||
469 I
->getOperand(0).getReg() == Mips::ZERO_64
)) &&
470 (I
->getOperand(1).isReg() &&
471 (I
->getOperand(1).getReg() == Mips::ZERO
||
472 I
->getOperand(1).getReg() == Mips::ZERO_64
)))
475 if (Subtarget
.hasMips32r6() || canUseShortMicroMipsCTI
) {
483 if (canUseShortMicroMipsCTI
)
484 return Mips::BEQZC_MM
;
485 else if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
490 if (canUseShortMicroMipsCTI
)
491 return Mips::BNEZC_MM
;
492 else if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
496 if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
500 if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
510 if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
514 if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
520 if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
524 if (I
->getOperand(0).getReg() == I
->getOperand(1).getReg())
528 return Mips::BGTZC64
;
530 return Mips::BGEZC64
;
532 return Mips::BLTZC64
;
534 return Mips::BLEZC64
;
535 // For MIPSR6, the instruction 'jic' can be used for these cases. Some
536 // tools will accept 'jrc reg' as an alias for 'jic 0, $reg'.
538 case Mips::PseudoIndirectBranchR6
:
539 case Mips::PseudoReturn
:
540 case Mips::TAILCALLR6REG
:
541 if (canUseShortMicroMipsCTI
)
542 return Mips::JRC16_MM
;
544 case Mips::JALRPseudo
:
547 case Mips::PseudoIndirectBranch64R6
:
548 case Mips::PseudoReturn64
:
549 case Mips::TAILCALL64R6REG
:
551 case Mips::JALR64Pseudo
:
552 return Mips::JIALC64
;
561 /// Predicate for distingushing between control transfer instructions and all
562 /// other instructions for handling forbidden slots. Consider inline assembly
563 /// as unsafe as well.
564 bool MipsInstrInfo::SafeInForbiddenSlot(const MachineInstr
&MI
) const {
565 if (MI
.isInlineAsm())
568 return (MI
.getDesc().TSFlags
& MipsII::IsCTI
) == 0;
571 /// Predicate for distingushing instructions that have forbidden slots.
572 bool MipsInstrInfo::HasForbiddenSlot(const MachineInstr
&MI
) const {
573 return (MI
.getDesc().TSFlags
& MipsII::HasForbiddenSlot
) != 0;
576 /// Return the number of bytes of code the specified instruction may be.
577 unsigned MipsInstrInfo::getInstSizeInBytes(const MachineInstr
&MI
) const {
578 switch (MI
.getOpcode()) {
580 return MI
.getDesc().getSize();
581 case TargetOpcode::INLINEASM
:
582 case TargetOpcode::INLINEASM_BR
: { // Inline Asm: Variable size.
583 const MachineFunction
*MF
= MI
.getParent()->getParent();
584 const char *AsmStr
= MI
.getOperand(0).getSymbolName();
585 return getInlineAsmLength(AsmStr
, *MF
->getTarget().getMCAsmInfo());
587 case Mips::CONSTPOOL_ENTRY
:
588 // If this machine instr is a constant pool entry, its size is recorded as
590 return MI
.getOperand(2).getImm();
595 MipsInstrInfo::genInstrWithNewOpc(unsigned NewOpc
,
596 MachineBasicBlock::iterator I
) const {
597 MachineInstrBuilder MIB
;
599 // Certain branches have two forms: e.g beq $1, $zero, dest vs beqz $1, dest
600 // Pick the zero form of the branch for readable assembly and for greater
601 // branch distance in non-microMIPS mode.
602 // Additional MIPSR6 does not permit the use of register $zero for compact
604 // FIXME: Certain atomic sequences on mips64 generate 32bit references to
605 // Mips::ZERO, which is incorrect. This test should be updated to use
606 // Subtarget.getABI().GetZeroReg() when those atomic sequences and others
608 int ZeroOperandPosition
= -1;
609 bool BranchWithZeroOperand
= false;
610 if (I
->isBranch() && !I
->isPseudo()) {
611 auto TRI
= I
->getParent()->getParent()->getSubtarget().getRegisterInfo();
612 ZeroOperandPosition
= I
->findRegisterUseOperandIdx(Mips::ZERO
, false, TRI
);
613 BranchWithZeroOperand
= ZeroOperandPosition
!= -1;
616 if (BranchWithZeroOperand
) {
619 NewOpc
= Mips::BEQZC
;
622 NewOpc
= Mips::BNEZC
;
625 NewOpc
= Mips::BGEZC
;
628 NewOpc
= Mips::BLTZC
;
631 NewOpc
= Mips::BEQZC64
;
634 NewOpc
= Mips::BNEZC64
;
639 MIB
= BuildMI(*I
->getParent(), I
, I
->getDebugLoc(), get(NewOpc
));
641 // For MIPSR6 JI*C requires an immediate 0 as an operand, JIALC(64) an
642 // immediate 0 as an operand and requires the removal of it's implicit-def %ra
643 // implicit operand as copying the implicit operations of the instructio we're
644 // looking at will give us the correct flags.
645 if (NewOpc
== Mips::JIC
|| NewOpc
== Mips::JIALC
|| NewOpc
== Mips::JIC64
||
646 NewOpc
== Mips::JIALC64
) {
648 if (NewOpc
== Mips::JIALC
|| NewOpc
== Mips::JIALC64
)
649 MIB
->RemoveOperand(0);
651 for (unsigned J
= 0, E
= I
->getDesc().getNumOperands(); J
< E
; ++J
) {
652 MIB
.add(I
->getOperand(J
));
657 // If I has an MCSymbol operand (used by asm printer, to emit R_MIPS_JALR),
658 // add it to the new instruction.
659 for (unsigned J
= I
->getDesc().getNumOperands(), E
= I
->getNumOperands();
661 const MachineOperand
&MO
= I
->getOperand(J
);
662 if (MO
.isMCSymbol() && (MO
.getTargetFlags() & MipsII::MO_JALR
))
663 MIB
.addSym(MO
.getMCSymbol(), MipsII::MO_JALR
);
668 for (unsigned J
= 0, E
= I
->getDesc().getNumOperands(); J
< E
; ++J
) {
669 if (BranchWithZeroOperand
&& (unsigned)ZeroOperandPosition
== J
)
672 MIB
.add(I
->getOperand(J
));
676 MIB
.copyImplicitOps(*I
);
677 MIB
.cloneMemRefs(*I
);
681 bool MipsInstrInfo::findCommutedOpIndices(const MachineInstr
&MI
,
683 unsigned &SrcOpIdx2
) const {
684 assert(!MI
.isBundle() &&
685 "TargetInstrInfo::findCommutedOpIndices() can't handle bundles");
687 const MCInstrDesc
&MCID
= MI
.getDesc();
688 if (!MCID
.isCommutable())
691 switch (MI
.getOpcode()) {
692 case Mips::DPADD_U_H
:
693 case Mips::DPADD_U_W
:
694 case Mips::DPADD_U_D
:
695 case Mips::DPADD_S_H
:
696 case Mips::DPADD_S_W
:
697 case Mips::DPADD_S_D
:
698 // The first operand is both input and output, so it should not commute
699 if (!fixCommutedOpIndices(SrcOpIdx1
, SrcOpIdx2
, 2, 3))
702 if (!MI
.getOperand(SrcOpIdx1
).isReg() || !MI
.getOperand(SrcOpIdx2
).isReg())
706 return TargetInstrInfo::findCommutedOpIndices(MI
, SrcOpIdx1
, SrcOpIdx2
);
709 // ins, ext, dext*, dins have the following constraints:
714 // dinsm and dinsu have the following constraints:
719 // The callee of verifyInsExtInstruction however gives the bounds of
720 // dins[um] like the other (d)ins (d)ext(um) instructions, so that this
721 // function doesn't have to vary it's behaviour based on the instruction
723 static bool verifyInsExtInstruction(const MachineInstr
&MI
, StringRef
&ErrInfo
,
724 const int64_t PosLow
, const int64_t PosHigh
,
725 const int64_t SizeLow
,
726 const int64_t SizeHigh
,
727 const int64_t BothLow
,
728 const int64_t BothHigh
) {
729 MachineOperand MOPos
= MI
.getOperand(2);
730 if (!MOPos
.isImm()) {
731 ErrInfo
= "Position is not an immediate!";
734 int64_t Pos
= MOPos
.getImm();
735 if (!((PosLow
<= Pos
) && (Pos
< PosHigh
))) {
736 ErrInfo
= "Position operand is out of range!";
740 MachineOperand MOSize
= MI
.getOperand(3);
741 if (!MOSize
.isImm()) {
742 ErrInfo
= "Size operand is not an immediate!";
745 int64_t Size
= MOSize
.getImm();
746 if (!((SizeLow
< Size
) && (Size
<= SizeHigh
))) {
747 ErrInfo
= "Size operand is out of range!";
751 if (!((BothLow
< (Pos
+ Size
)) && ((Pos
+ Size
) <= BothHigh
))) {
752 ErrInfo
= "Position + Size is out of range!";
759 // Perform target specific instruction verification.
760 bool MipsInstrInfo::verifyInstruction(const MachineInstr
&MI
,
761 StringRef
&ErrInfo
) const {
762 // Verify that ins and ext instructions are well formed.
763 switch (MI
.getOpcode()) {
769 return verifyInsExtInstruction(MI
, ErrInfo
, 0, 32, 0, 32, 0, 32);
771 // The ISA spec has a subtle difference between dinsm and dextm
773 // 2 <= size <= 64 for 'dinsm' but 'dextm' has 32 < size <= 64.
774 // To make the bounds checks similar, the range 1 < size <= 64 is checked
776 return verifyInsExtInstruction(MI
, ErrInfo
, 0, 32, 1, 64, 32, 64);
778 // The ISA spec has a subtle difference between dinsu and dextu in that
779 // the size range of dinsu is specified as 1 <= size <= 32 whereas size
780 // for dextu is 0 < size <= 32. The range checked for dinsu here is
781 // 0 < size <= 32, which is equivalent and similar to dextu.
782 return verifyInsExtInstruction(MI
, ErrInfo
, 32, 64, 0, 32, 32, 64);
784 return verifyInsExtInstruction(MI
, ErrInfo
, 0, 32, 0, 32, 0, 63);
786 return verifyInsExtInstruction(MI
, ErrInfo
, 0, 32, 32, 64, 32, 64);
788 return verifyInsExtInstruction(MI
, ErrInfo
, 32, 64, 0, 32, 32, 64);
789 case Mips::TAILCALLREG
:
790 case Mips::PseudoIndirectBranch
:
795 case Mips::JALRPseudo
:
796 if (!Subtarget
.useIndirectJumpsHazard())
799 ErrInfo
= "invalid instruction when using jump guards!";
808 std::pair
<unsigned, unsigned>
809 MipsInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF
) const {
810 return std::make_pair(TF
, 0u);
813 ArrayRef
<std::pair
<unsigned, const char*>>
814 MipsInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
815 using namespace MipsII
;
817 static const std::pair
<unsigned, const char*> Flags
[] = {
818 {MO_GOT
, "mips-got"},
819 {MO_GOT_CALL
, "mips-got-call"},
820 {MO_GPREL
, "mips-gprel"},
821 {MO_ABS_HI
, "mips-abs-hi"},
822 {MO_ABS_LO
, "mips-abs-lo"},
823 {MO_TLSGD
, "mips-tlsgd"},
824 {MO_TLSLDM
, "mips-tlsldm"},
825 {MO_DTPREL_HI
, "mips-dtprel-hi"},
826 {MO_DTPREL_LO
, "mips-dtprel-lo"},
827 {MO_GOTTPREL
, "mips-gottprel"},
828 {MO_TPREL_HI
, "mips-tprel-hi"},
829 {MO_TPREL_LO
, "mips-tprel-lo"},
830 {MO_GPOFF_HI
, "mips-gpoff-hi"},
831 {MO_GPOFF_LO
, "mips-gpoff-lo"},
832 {MO_GOT_DISP
, "mips-got-disp"},
833 {MO_GOT_PAGE
, "mips-got-page"},
834 {MO_GOT_OFST
, "mips-got-ofst"},
835 {MO_HIGHER
, "mips-higher"},
836 {MO_HIGHEST
, "mips-highest"},
837 {MO_GOT_HI16
, "mips-got-hi16"},
838 {MO_GOT_LO16
, "mips-got-lo16"},
839 {MO_CALL_HI16
, "mips-call-hi16"},
840 {MO_CALL_LO16
, "mips-call-lo16"},
841 {MO_JALR
, "mips-jalr"}
843 return makeArrayRef(Flags
);
846 Optional
<ParamLoadedValue
>
847 MipsInstrInfo::describeLoadedValue(const MachineInstr
&MI
, Register Reg
) const {
849 DIExpression::get(MI
.getMF()->getFunction().getContext(), {});
851 // TODO: Special MIPS instructions that need to be described separately.
852 if (auto RegImm
= isAddImmediate(MI
, Reg
)) {
853 Register SrcReg
= RegImm
->Reg
;
854 int64_t Offset
= RegImm
->Imm
;
855 // When SrcReg is $zero, treat loaded value as immediate only.
856 // Ex. $a2 = ADDiu $zero, 10
857 if (SrcReg
== Mips::ZERO
|| SrcReg
== Mips::ZERO_64
) {
858 return ParamLoadedValue(MI
.getOperand(2), Expr
);
860 Expr
= DIExpression::prepend(Expr
, DIExpression::ApplyOffset
, Offset
);
861 return ParamLoadedValue(MachineOperand::CreateReg(SrcReg
, false), Expr
);
862 } else if (auto DestSrc
= isCopyInstr(MI
)) {
863 const MachineFunction
*MF
= MI
.getMF();
864 const TargetRegisterInfo
*TRI
= MF
->getSubtarget().getRegisterInfo();
865 Register DestReg
= DestSrc
->Destination
->getReg();
866 // TODO: Handle cases where the Reg is sub- or super-register of the
868 if (TRI
->isSuperRegister(Reg
, DestReg
) || TRI
->isSubRegister(Reg
, DestReg
))
872 return TargetInstrInfo::describeLoadedValue(MI
, Reg
);
875 Optional
<RegImmPair
> MipsInstrInfo::isAddImmediate(const MachineInstr
&MI
,
876 Register Reg
) const {
877 // TODO: Handle cases where Reg is a super- or sub-register of the
878 // destination register.
879 const MachineOperand
&Op0
= MI
.getOperand(0);
880 if (!Op0
.isReg() || Reg
!= Op0
.getReg())
883 switch (MI
.getOpcode()) {
886 const MachineOperand
&Dop
= MI
.getOperand(0);
887 const MachineOperand
&Sop1
= MI
.getOperand(1);
888 const MachineOperand
&Sop2
= MI
.getOperand(2);
889 // Value is sum of register and immediate. Immediate value could be
890 // global string address which is not supported.
891 if (Dop
.isReg() && Sop1
.isReg() && Sop2
.isImm())
892 return RegImmPair
{Sop1
.getReg(), Sop2
.getImm()};
893 // TODO: Handle case where Sop1 is a frame-index.