1 //===-- RISCVInstrInfo.cpp - RISCV Instruction Information ------*- C++ -*-===//
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 RISCV implementation of the TargetInstrInfo class.
11 //===----------------------------------------------------------------------===//
13 #include "RISCVInstrInfo.h"
15 #include "RISCVSubtarget.h"
16 #include "RISCVTargetMachine.h"
17 #include "Utils/RISCVMatInt.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/RegisterScavenging.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/TargetRegistry.h"
27 #define GET_INSTRINFO_CTOR_DTOR
28 #include "RISCVGenInstrInfo.inc"
32 RISCVInstrInfo::RISCVInstrInfo()
33 : RISCVGenInstrInfo(RISCV::ADJCALLSTACKDOWN
, RISCV::ADJCALLSTACKUP
) {}
35 unsigned RISCVInstrInfo::isLoadFromStackSlot(const MachineInstr
&MI
,
36 int &FrameIndex
) const {
37 switch (MI
.getOpcode()) {
52 if (MI
.getOperand(1).isFI() && MI
.getOperand(2).isImm() &&
53 MI
.getOperand(2).getImm() == 0) {
54 FrameIndex
= MI
.getOperand(1).getIndex();
55 return MI
.getOperand(0).getReg();
61 unsigned RISCVInstrInfo::isStoreToStackSlot(const MachineInstr
&MI
,
62 int &FrameIndex
) const {
63 switch (MI
.getOpcode()) {
75 if (MI
.getOperand(0).isFI() && MI
.getOperand(1).isImm() &&
76 MI
.getOperand(1).getImm() == 0) {
77 FrameIndex
= MI
.getOperand(0).getIndex();
78 return MI
.getOperand(2).getReg();
84 void RISCVInstrInfo::copyPhysReg(MachineBasicBlock
&MBB
,
85 MachineBasicBlock::iterator MBBI
,
86 const DebugLoc
&DL
, unsigned DstReg
,
87 unsigned SrcReg
, bool KillSrc
) const {
88 if (RISCV::GPRRegClass
.contains(DstReg
, SrcReg
)) {
89 BuildMI(MBB
, MBBI
, DL
, get(RISCV::ADDI
), DstReg
)
90 .addReg(SrcReg
, getKillRegState(KillSrc
))
97 if (RISCV::FPR32RegClass
.contains(DstReg
, SrcReg
))
99 else if (RISCV::FPR64RegClass
.contains(DstReg
, SrcReg
))
100 Opc
= RISCV::FSGNJ_D
;
102 llvm_unreachable("Impossible reg-to-reg copy");
104 BuildMI(MBB
, MBBI
, DL
, get(Opc
), DstReg
)
105 .addReg(SrcReg
, getKillRegState(KillSrc
))
106 .addReg(SrcReg
, getKillRegState(KillSrc
));
109 void RISCVInstrInfo::storeRegToStackSlot(MachineBasicBlock
&MBB
,
110 MachineBasicBlock::iterator I
,
111 unsigned SrcReg
, bool IsKill
, int FI
,
112 const TargetRegisterClass
*RC
,
113 const TargetRegisterInfo
*TRI
) const {
116 DL
= I
->getDebugLoc();
120 if (RISCV::GPRRegClass
.hasSubClassEq(RC
))
121 Opcode
= TRI
->getRegSizeInBits(RISCV::GPRRegClass
) == 32 ?
122 RISCV::SW
: RISCV::SD
;
123 else if (RISCV::FPR32RegClass
.hasSubClassEq(RC
))
125 else if (RISCV::FPR64RegClass
.hasSubClassEq(RC
))
128 llvm_unreachable("Can't store this register to stack slot");
130 BuildMI(MBB
, I
, DL
, get(Opcode
))
131 .addReg(SrcReg
, getKillRegState(IsKill
))
136 void RISCVInstrInfo::loadRegFromStackSlot(MachineBasicBlock
&MBB
,
137 MachineBasicBlock::iterator I
,
138 unsigned DstReg
, int FI
,
139 const TargetRegisterClass
*RC
,
140 const TargetRegisterInfo
*TRI
) const {
143 DL
= I
->getDebugLoc();
147 if (RISCV::GPRRegClass
.hasSubClassEq(RC
))
148 Opcode
= TRI
->getRegSizeInBits(RISCV::GPRRegClass
) == 32 ?
149 RISCV::LW
: RISCV::LD
;
150 else if (RISCV::FPR32RegClass
.hasSubClassEq(RC
))
152 else if (RISCV::FPR64RegClass
.hasSubClassEq(RC
))
155 llvm_unreachable("Can't load this register from stack slot");
157 BuildMI(MBB
, I
, DL
, get(Opcode
), DstReg
).addFrameIndex(FI
).addImm(0);
160 void RISCVInstrInfo::movImm(MachineBasicBlock
&MBB
,
161 MachineBasicBlock::iterator MBBI
,
162 const DebugLoc
&DL
, Register DstReg
, uint64_t Val
,
163 MachineInstr::MIFlag Flag
) const {
164 MachineFunction
*MF
= MBB
.getParent();
165 MachineRegisterInfo
&MRI
= MF
->getRegInfo();
166 bool IsRV64
= MF
->getSubtarget
<RISCVSubtarget
>().is64Bit();
167 Register SrcReg
= RISCV::X0
;
168 Register Result
= MRI
.createVirtualRegister(&RISCV::GPRRegClass
);
171 if (!IsRV64
&& !isInt
<32>(Val
))
172 report_fatal_error("Should only materialize 32-bit constants for RV32");
174 RISCVMatInt::InstSeq Seq
;
175 RISCVMatInt::generateInstSeq(Val
, IsRV64
, Seq
);
176 assert(Seq
.size() > 0);
178 for (RISCVMatInt::Inst
&Inst
: Seq
) {
179 // Write the final result to DstReg if it's the last instruction in the Seq.
180 // Otherwise, write the result to the temp register.
181 if (++Num
== Seq
.size())
184 if (Inst
.Opc
== RISCV::LUI
) {
185 BuildMI(MBB
, MBBI
, DL
, get(RISCV::LUI
), Result
)
189 BuildMI(MBB
, MBBI
, DL
, get(Inst
.Opc
), Result
)
190 .addReg(SrcReg
, RegState::Kill
)
194 // Only the first instruction has X0 as its source.
199 // The contents of values added to Cond are not examined outside of
200 // RISCVInstrInfo, giving us flexibility in what to push to it. For RISCV, we
201 // push BranchOpcode, Reg1, Reg2.
202 static void parseCondBranch(MachineInstr
&LastInst
, MachineBasicBlock
*&Target
,
203 SmallVectorImpl
<MachineOperand
> &Cond
) {
204 // Block ends with fall-through condbranch.
205 assert(LastInst
.getDesc().isConditionalBranch() &&
206 "Unknown conditional branch");
207 Target
= LastInst
.getOperand(2).getMBB();
208 Cond
.push_back(MachineOperand::CreateImm(LastInst
.getOpcode()));
209 Cond
.push_back(LastInst
.getOperand(0));
210 Cond
.push_back(LastInst
.getOperand(1));
213 static unsigned getOppositeBranchOpcode(int Opc
) {
216 llvm_unreachable("Unrecognized conditional branch");
232 bool RISCVInstrInfo::analyzeBranch(MachineBasicBlock
&MBB
,
233 MachineBasicBlock
*&TBB
,
234 MachineBasicBlock
*&FBB
,
235 SmallVectorImpl
<MachineOperand
> &Cond
,
236 bool AllowModify
) const {
240 // If the block has no terminators, it just falls into the block after it.
241 MachineBasicBlock::iterator I
= MBB
.getLastNonDebugInstr();
242 if (I
== MBB
.end() || !isUnpredicatedTerminator(*I
))
245 // Count the number of terminators and find the first unconditional or
247 MachineBasicBlock::iterator FirstUncondOrIndirectBr
= MBB
.end();
248 int NumTerminators
= 0;
249 for (auto J
= I
.getReverse(); J
!= MBB
.rend() && isUnpredicatedTerminator(*J
);
252 if (J
->getDesc().isUnconditionalBranch() ||
253 J
->getDesc().isIndirectBranch()) {
254 FirstUncondOrIndirectBr
= J
.getReverse();
258 // If AllowModify is true, we can erase any terminators after
259 // FirstUncondOrIndirectBR.
260 if (AllowModify
&& FirstUncondOrIndirectBr
!= MBB
.end()) {
261 while (std::next(FirstUncondOrIndirectBr
) != MBB
.end()) {
262 std::next(FirstUncondOrIndirectBr
)->eraseFromParent();
265 I
= FirstUncondOrIndirectBr
;
268 // We can't handle blocks that end in an indirect branch.
269 if (I
->getDesc().isIndirectBranch())
272 // We can't handle blocks with more than 2 terminators.
273 if (NumTerminators
> 2)
276 // Handle a single unconditional branch.
277 if (NumTerminators
== 1 && I
->getDesc().isUnconditionalBranch()) {
278 TBB
= I
->getOperand(0).getMBB();
282 // Handle a single conditional branch.
283 if (NumTerminators
== 1 && I
->getDesc().isConditionalBranch()) {
284 parseCondBranch(*I
, TBB
, Cond
);
288 // Handle a conditional branch followed by an unconditional branch.
289 if (NumTerminators
== 2 && std::prev(I
)->getDesc().isConditionalBranch() &&
290 I
->getDesc().isUnconditionalBranch()) {
291 parseCondBranch(*std::prev(I
), TBB
, Cond
);
292 FBB
= I
->getOperand(0).getMBB();
296 // Otherwise, we can't handle this.
300 unsigned RISCVInstrInfo::removeBranch(MachineBasicBlock
&MBB
,
301 int *BytesRemoved
) const {
304 MachineBasicBlock::iterator I
= MBB
.getLastNonDebugInstr();
308 if (!I
->getDesc().isUnconditionalBranch() &&
309 !I
->getDesc().isConditionalBranch())
312 // Remove the branch.
314 *BytesRemoved
+= getInstSizeInBytes(*I
);
315 I
->eraseFromParent();
319 if (I
== MBB
.begin())
322 if (!I
->getDesc().isConditionalBranch())
325 // Remove the branch.
327 *BytesRemoved
+= getInstSizeInBytes(*I
);
328 I
->eraseFromParent();
332 // Inserts a branch into the end of the specific MachineBasicBlock, returning
333 // the number of instructions inserted.
334 unsigned RISCVInstrInfo::insertBranch(
335 MachineBasicBlock
&MBB
, MachineBasicBlock
*TBB
, MachineBasicBlock
*FBB
,
336 ArrayRef
<MachineOperand
> Cond
, const DebugLoc
&DL
, int *BytesAdded
) const {
340 // Shouldn't be a fall through.
341 assert(TBB
&& "InsertBranch must not be told to insert a fallthrough");
342 assert((Cond
.size() == 3 || Cond
.size() == 0) &&
343 "RISCV branch conditions have two components!");
345 // Unconditional branch.
347 MachineInstr
&MI
= *BuildMI(&MBB
, DL
, get(RISCV::PseudoBR
)).addMBB(TBB
);
349 *BytesAdded
+= getInstSizeInBytes(MI
);
353 // Either a one or two-way conditional branch.
354 unsigned Opc
= Cond
[0].getImm();
355 MachineInstr
&CondMI
=
356 *BuildMI(&MBB
, DL
, get(Opc
)).add(Cond
[1]).add(Cond
[2]).addMBB(TBB
);
358 *BytesAdded
+= getInstSizeInBytes(CondMI
);
360 // One-way conditional branch.
364 // Two-way conditional branch.
365 MachineInstr
&MI
= *BuildMI(&MBB
, DL
, get(RISCV::PseudoBR
)).addMBB(FBB
);
367 *BytesAdded
+= getInstSizeInBytes(MI
);
371 unsigned RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock
&MBB
,
372 MachineBasicBlock
&DestBB
,
375 RegScavenger
*RS
) const {
376 assert(RS
&& "RegScavenger required for long branching");
377 assert(MBB
.empty() &&
378 "new block should be inserted for expanding unconditional branch");
379 assert(MBB
.pred_size() == 1);
381 MachineFunction
*MF
= MBB
.getParent();
382 MachineRegisterInfo
&MRI
= MF
->getRegInfo();
383 const auto &TM
= static_cast<const RISCVTargetMachine
&>(MF
->getTarget());
385 if (TM
.isPositionIndependent())
386 report_fatal_error("Unable to insert indirect branch");
388 if (!isInt
<32>(BrOffset
))
390 "Branch offsets outside of the signed 32-bit range not supported");
392 // FIXME: A virtual register must be used initially, as the register
393 // scavenger won't work with empty blocks (SIInstrInfo::insertIndirectBranch
394 // uses the same workaround).
395 Register ScratchReg
= MRI
.createVirtualRegister(&RISCV::GPRRegClass
);
398 MachineInstr
&LuiMI
= *BuildMI(MBB
, II
, DL
, get(RISCV::LUI
), ScratchReg
)
399 .addMBB(&DestBB
, RISCVII::MO_HI
);
400 BuildMI(MBB
, II
, DL
, get(RISCV::PseudoBRIND
))
401 .addReg(ScratchReg
, RegState::Kill
)
402 .addMBB(&DestBB
, RISCVII::MO_LO
);
404 RS
->enterBasicBlockEnd(MBB
);
405 unsigned Scav
= RS
->scavengeRegisterBackwards(RISCV::GPRRegClass
,
406 LuiMI
.getIterator(), false, 0);
407 MRI
.replaceRegWith(ScratchReg
, Scav
);
409 RS
->setRegUsed(Scav
);
413 bool RISCVInstrInfo::reverseBranchCondition(
414 SmallVectorImpl
<MachineOperand
> &Cond
) const {
415 assert((Cond
.size() == 3) && "Invalid branch condition!");
416 Cond
[0].setImm(getOppositeBranchOpcode(Cond
[0].getImm()));
421 RISCVInstrInfo::getBranchDestBlock(const MachineInstr
&MI
) const {
422 assert(MI
.getDesc().isBranch() && "Unexpected opcode!");
423 // The branch target is always the last operand.
424 int NumOp
= MI
.getNumExplicitOperands();
425 return MI
.getOperand(NumOp
- 1).getMBB();
428 bool RISCVInstrInfo::isBranchOffsetInRange(unsigned BranchOp
,
429 int64_t BrOffset
) const {
430 // Ideally we could determine the supported branch offset from the
431 // RISCVII::FormMask, but this can't be used for Pseudo instructions like
435 llvm_unreachable("Unexpected opcode!");
442 return isIntN(13, BrOffset
);
444 case RISCV::PseudoBR
:
445 return isIntN(21, BrOffset
);
449 unsigned RISCVInstrInfo::getInstSizeInBytes(const MachineInstr
&MI
) const {
450 unsigned Opcode
= MI
.getOpcode();
453 default: { return get(Opcode
).getSize(); }
454 case TargetOpcode::EH_LABEL
:
455 case TargetOpcode::IMPLICIT_DEF
:
456 case TargetOpcode::KILL
:
457 case TargetOpcode::DBG_VALUE
:
459 case RISCV::PseudoCALLReg
:
460 case RISCV::PseudoCALL
:
461 case RISCV::PseudoTAIL
:
462 case RISCV::PseudoLLA
:
463 case RISCV::PseudoLA
:
464 case RISCV::PseudoLA_TLS_IE
:
465 case RISCV::PseudoLA_TLS_GD
:
467 case TargetOpcode::INLINEASM
:
468 case TargetOpcode::INLINEASM_BR
: {
469 const MachineFunction
&MF
= *MI
.getParent()->getParent();
470 const auto &TM
= static_cast<const RISCVTargetMachine
&>(MF
.getTarget());
471 return getInlineAsmLength(MI
.getOperand(0).getSymbolName(),
477 bool RISCVInstrInfo::isAsCheapAsAMove(const MachineInstr
&MI
) const {
478 const unsigned Opcode
= MI
.getOpcode();
485 return (MI
.getOperand(1).isReg() && MI
.getOperand(1).getReg() == RISCV::X0
);
487 return MI
.isAsCheapAsAMove();