1 //===- RISCVOptWInstrs.cpp - MI W instruction optimizations ---------------===//
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 pass does some optimizations for *W instructions at the MI level.
11 // First it removes unneeded sext.w instructions. Either because the sign
12 // extended bits aren't consumed or because the input was already sign extended
13 // by an earlier instruction.
16 // 1. Unless explicit disabled or the target prefers instructions with W suffix,
17 // it removes the -w suffix from opw instructions whenever all users are
18 // dependent only on the lower word of the result of the instruction.
19 // The cases handled are:
20 // * addw because c.add has a larger register encoding than c.addw.
21 // * addiw because it helps reduce test differences between RV32 and RV64
22 // w/o being a pessimization.
23 // * mulw because c.mulw doesn't exist but c.mul does (w/ zcb)
24 // * slliw because c.slliw doesn't exist and c.slli does
26 // 2. Or if explicit enabled or the target prefers instructions with W suffix,
27 // it adds the W suffix to the instruction whenever all users are dependent
28 // only on the lower word of the result of the instruction.
29 // The cases handled are:
30 // * add/addi/sub/mul.
31 // * slli with imm < 32.
33 //===---------------------------------------------------------------------===//
36 #include "RISCVMachineFunctionInfo.h"
37 #include "RISCVSubtarget.h"
38 #include "llvm/ADT/SmallSet.h"
39 #include "llvm/ADT/Statistic.h"
40 #include "llvm/CodeGen/MachineFunctionPass.h"
41 #include "llvm/CodeGen/TargetInstrInfo.h"
45 #define DEBUG_TYPE "riscv-opt-w-instrs"
46 #define RISCV_OPT_W_INSTRS_NAME "RISC-V Optimize W Instructions"
48 STATISTIC(NumRemovedSExtW
, "Number of removed sign-extensions");
49 STATISTIC(NumTransformedToWInstrs
,
50 "Number of instructions transformed to W-ops");
52 static cl::opt
<bool> DisableSExtWRemoval("riscv-disable-sextw-removal",
53 cl::desc("Disable removal of sext.w"),
54 cl::init(false), cl::Hidden
);
55 static cl::opt
<bool> DisableStripWSuffix("riscv-disable-strip-w-suffix",
56 cl::desc("Disable strip W suffix"),
57 cl::init(false), cl::Hidden
);
61 class RISCVOptWInstrs
: public MachineFunctionPass
{
65 RISCVOptWInstrs() : MachineFunctionPass(ID
) {}
67 bool runOnMachineFunction(MachineFunction
&MF
) override
;
68 bool removeSExtWInstrs(MachineFunction
&MF
, const RISCVInstrInfo
&TII
,
69 const RISCVSubtarget
&ST
, MachineRegisterInfo
&MRI
);
70 bool stripWSuffixes(MachineFunction
&MF
, const RISCVInstrInfo
&TII
,
71 const RISCVSubtarget
&ST
, MachineRegisterInfo
&MRI
);
72 bool appendWSuffixes(MachineFunction
&MF
, const RISCVInstrInfo
&TII
,
73 const RISCVSubtarget
&ST
, MachineRegisterInfo
&MRI
);
75 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
77 MachineFunctionPass::getAnalysisUsage(AU
);
80 StringRef
getPassName() const override
{ return RISCV_OPT_W_INSTRS_NAME
; }
83 } // end anonymous namespace
85 char RISCVOptWInstrs::ID
= 0;
86 INITIALIZE_PASS(RISCVOptWInstrs
, DEBUG_TYPE
, RISCV_OPT_W_INSTRS_NAME
, false,
89 FunctionPass
*llvm::createRISCVOptWInstrsPass() {
90 return new RISCVOptWInstrs();
93 static bool vectorPseudoHasAllNBitUsers(const MachineOperand
&UserOp
,
95 const MachineInstr
&MI
= *UserOp
.getParent();
96 unsigned MCOpcode
= RISCV::getRVVMCOpcode(MI
.getOpcode());
101 const MCInstrDesc
&MCID
= MI
.getDesc();
102 const uint64_t TSFlags
= MCID
.TSFlags
;
103 if (!RISCVII::hasSEWOp(TSFlags
))
105 assert(RISCVII::hasVLOp(TSFlags
));
106 const unsigned Log2SEW
= MI
.getOperand(RISCVII::getSEWOpNum(MCID
)).getImm();
108 if (UserOp
.getOperandNo() == RISCVII::getVLOpNum(MCID
))
111 auto NumDemandedBits
=
112 RISCV::getVectorLowDemandedScalarBits(MCOpcode
, Log2SEW
);
113 return NumDemandedBits
&& Bits
>= *NumDemandedBits
;
116 // Checks if all users only demand the lower \p OrigBits of the original
117 // instruction's result.
118 // TODO: handle multiple interdependent transformations
119 static bool hasAllNBitUsers(const MachineInstr
&OrigMI
,
120 const RISCVSubtarget
&ST
,
121 const MachineRegisterInfo
&MRI
, unsigned OrigBits
) {
123 SmallSet
<std::pair
<const MachineInstr
*, unsigned>, 4> Visited
;
124 SmallVector
<std::pair
<const MachineInstr
*, unsigned>, 4> Worklist
;
126 Worklist
.push_back(std::make_pair(&OrigMI
, OrigBits
));
128 while (!Worklist
.empty()) {
129 auto P
= Worklist
.pop_back_val();
130 const MachineInstr
*MI
= P
.first
;
131 unsigned Bits
= P
.second
;
133 if (!Visited
.insert(P
).second
)
136 // Only handle instructions with one def.
137 if (MI
->getNumExplicitDefs() != 1)
140 Register DestReg
= MI
->getOperand(0).getReg();
141 if (!DestReg
.isVirtual())
144 for (auto &UserOp
: MRI
.use_nodbg_operands(DestReg
)) {
145 const MachineInstr
*UserMI
= UserOp
.getParent();
146 unsigned OpIdx
= UserOp
.getOperandNo();
148 switch (UserMI
->getOpcode()) {
150 if (vectorPseudoHasAllNBitUsers(UserOp
, Bits
))
176 case RISCV::FCVT_H_W
:
177 case RISCV::FCVT_H_WU
:
178 case RISCV::FCVT_S_W
:
179 case RISCV::FCVT_S_WU
:
180 case RISCV::FCVT_D_W
:
181 case RISCV::FCVT_D_WU
:
192 case RISCV::ZEXT_H_RV32
:
193 case RISCV::ZEXT_H_RV64
:
200 if (Bits
>= (ST
.getXLen() / 2))
205 // If we are shifting right by less than Bits, and users don't demand
206 // any bits that were shifted into [Bits-1:0], then we can consider this
208 unsigned ShAmt
= UserMI
->getOperand(2).getImm();
210 Worklist
.push_back(std::make_pair(UserMI
, Bits
- ShAmt
));
216 // these overwrite higher input bits, otherwise the lower word of output
217 // depends only on the lower word of input. So check their uses read W.
219 if (Bits
>= (ST
.getXLen() - UserMI
->getOperand(2).getImm()))
221 Worklist
.push_back(std::make_pair(UserMI
, Bits
));
224 uint64_t Imm
= UserMI
->getOperand(2).getImm();
225 if (Bits
>= (unsigned)llvm::bit_width(Imm
))
227 Worklist
.push_back(std::make_pair(UserMI
, Bits
));
231 uint64_t Imm
= UserMI
->getOperand(2).getImm();
232 if (Bits
>= (unsigned)llvm::bit_width
<uint64_t>(~Imm
))
234 Worklist
.push_back(std::make_pair(UserMI
, Bits
));
242 // Operand 2 is the shift amount which uses log2(xlen) bits.
244 if (Bits
>= Log2_32(ST
.getXLen()))
248 Worklist
.push_back(std::make_pair(UserMI
, Bits
));
255 // Operand 2 is the shift amount which uses 6 bits.
256 if (OpIdx
== 2 && Bits
>= Log2_32(ST
.getXLen()))
261 case RISCV::SH1ADD_UW
:
262 case RISCV::SH2ADD_UW
:
263 case RISCV::SH3ADD_UW
:
264 // Operand 1 is implicitly zero extended.
265 if (OpIdx
== 1 && Bits
>= 32)
267 Worklist
.push_back(std::make_pair(UserMI
, Bits
));
271 if (UserMI
->getOperand(2).getImm() >= Bits
)
276 // The first argument is the value to store.
277 if (OpIdx
== 0 && Bits
>= 8)
281 // The first argument is the value to store.
282 if (OpIdx
== 0 && Bits
>= 16)
286 // The first argument is the value to store.
287 if (OpIdx
== 0 && Bits
>= 32)
291 // For these, lower word of output in these operations, depends only on
292 // the lower word of input. So, we check all uses only read lower word.
317 Worklist
.push_back(std::make_pair(UserMI
, Bits
));
320 case RISCV::PseudoCCMOVGPR
:
321 // Either operand 4 or operand 5 is returned by this instruction. If
322 // only the lower word of the result is used, then only the lower word
323 // of operand 4 and 5 is used.
324 if (OpIdx
!= 4 && OpIdx
!= 5)
326 Worklist
.push_back(std::make_pair(UserMI
, Bits
));
329 case RISCV::CZERO_EQZ
:
330 case RISCV::CZERO_NEZ
:
331 case RISCV::VT_MASKC
:
332 case RISCV::VT_MASKCN
:
335 Worklist
.push_back(std::make_pair(UserMI
, Bits
));
344 static bool hasAllWUsers(const MachineInstr
&OrigMI
, const RISCVSubtarget
&ST
,
345 const MachineRegisterInfo
&MRI
) {
346 return hasAllNBitUsers(OrigMI
, ST
, MRI
, 32);
349 // This function returns true if the machine instruction always outputs a value
350 // where bits 63:32 match bit 31.
351 static bool isSignExtendingOpW(const MachineInstr
&MI
,
352 const MachineRegisterInfo
&MRI
, unsigned OpNo
) {
353 uint64_t TSFlags
= MI
.getDesc().TSFlags
;
355 // Instructions that can be determined from opcode are marked in tablegen.
356 if (TSFlags
& RISCVII::IsSignExtendingOpWMask
)
359 // Special cases that require checking operands.
360 switch (MI
.getOpcode()) {
361 // shifting right sufficiently makes the value 32-bit sign-extended
363 return MI
.getOperand(2).getImm() >= 32;
365 return MI
.getOperand(2).getImm() > 32;
366 // The LI pattern ADDI rd, X0, imm is sign extended.
368 return MI
.getOperand(1).isReg() && MI
.getOperand(1).getReg() == RISCV::X0
;
369 // An ANDI with an 11 bit immediate will zero bits 63:11.
371 return isUInt
<11>(MI
.getOperand(2).getImm());
372 // An ORI with an >11 bit immediate (negative 12-bit) will set bits 63:11.
374 return !isUInt
<11>(MI
.getOperand(2).getImm());
375 // A bseti with X0 is sign extended if the immediate is less than 31.
377 return MI
.getOperand(2).getImm() < 31 &&
378 MI
.getOperand(1).getReg() == RISCV::X0
;
379 // Copying from X0 produces zero.
381 return MI
.getOperand(1).getReg() == RISCV::X0
;
382 // Ignore the scratch register destination.
383 case RISCV::PseudoAtomicLoadNand32
:
385 case RISCV::PseudoVMV_X_S
: {
386 // vmv.x.s has at least 33 sign bits if log2(sew) <= 5.
387 int64_t Log2SEW
= MI
.getOperand(2).getImm();
388 assert(Log2SEW
>= 3 && Log2SEW
<= 6 && "Unexpected Log2SEW");
396 static bool isSignExtendedW(Register SrcReg
, const RISCVSubtarget
&ST
,
397 const MachineRegisterInfo
&MRI
,
398 SmallPtrSetImpl
<MachineInstr
*> &FixableDef
) {
399 SmallSet
<Register
, 4> Visited
;
400 SmallVector
<Register
, 4> Worklist
;
402 auto AddRegToWorkList
= [&](Register SrcReg
) {
403 if (!SrcReg
.isVirtual())
405 Worklist
.push_back(SrcReg
);
409 if (!AddRegToWorkList(SrcReg
))
412 while (!Worklist
.empty()) {
413 Register Reg
= Worklist
.pop_back_val();
415 // If we already visited this register, we don't need to check it again.
416 if (!Visited
.insert(Reg
).second
)
419 MachineInstr
*MI
= MRI
.getVRegDef(Reg
);
423 int OpNo
= MI
->findRegisterDefOperandIdx(Reg
, /*TRI=*/nullptr);
424 assert(OpNo
!= -1 && "Couldn't find register");
426 // If this is a sign extending operation we don't need to look any further.
427 if (isSignExtendingOpW(*MI
, MRI
, OpNo
))
430 // Is this an instruction that propagates sign extend?
431 switch (MI
->getOpcode()) {
433 // Unknown opcode, give up.
436 const MachineFunction
*MF
= MI
->getMF();
437 const RISCVMachineFunctionInfo
*RVFI
=
438 MF
->getInfo
<RISCVMachineFunctionInfo
>();
440 // If this is the entry block and the register is livein, see if we know
441 // it is sign extended.
442 if (MI
->getParent() == &MF
->front()) {
443 Register VReg
= MI
->getOperand(0).getReg();
444 if (MF
->getRegInfo().isLiveIn(VReg
) && RVFI
->isSExt32Register(VReg
))
448 Register CopySrcReg
= MI
->getOperand(1).getReg();
449 if (CopySrcReg
== RISCV::X10
) {
450 // For a method return value, we check the ZExt/SExt flags in attribute.
451 // We assume the following code sequence for method call.
452 // PseudoCALL @bar, ...
453 // ADJCALLSTACKUP 0, 0, implicit-def dead $x2, implicit $x2
454 // %0:gpr = COPY $x10
456 // We use the PseudoCall to look up the IR function being called to find
457 // its return attributes.
458 const MachineBasicBlock
*MBB
= MI
->getParent();
459 auto II
= MI
->getIterator();
460 if (II
== MBB
->instr_begin() ||
461 (--II
)->getOpcode() != RISCV::ADJCALLSTACKUP
)
464 const MachineInstr
&CallMI
= *(--II
);
465 if (!CallMI
.isCall() || !CallMI
.getOperand(0).isGlobal())
469 dyn_cast_if_present
<Function
>(CallMI
.getOperand(0).getGlobal());
473 auto *IntTy
= dyn_cast
<IntegerType
>(CalleeFn
->getReturnType());
477 const AttributeSet
&Attrs
= CalleeFn
->getAttributes().getRetAttrs();
478 unsigned BitWidth
= IntTy
->getBitWidth();
479 if ((BitWidth
<= 32 && Attrs
.hasAttribute(Attribute::SExt
)) ||
480 (BitWidth
< 32 && Attrs
.hasAttribute(Attribute::ZExt
)))
484 if (!AddRegToWorkList(CopySrcReg
))
490 // For these, we just need to check if the 1st operand is sign extended.
494 if (MI
->getOperand(2).getImm() >= 31)
501 // |Remainder| is always <= |Dividend|. If D is 32-bit, then so is R.
502 // DIV doesn't work because of the edge case 0xf..f 8000 0000 / (long)-1
503 // Logical operations use a sign extended 12-bit immediate.
504 if (!AddRegToWorkList(MI
->getOperand(1).getReg()))
508 case RISCV::PseudoCCADDW
:
509 case RISCV::PseudoCCADDIW
:
510 case RISCV::PseudoCCSUBW
:
511 case RISCV::PseudoCCSLLW
:
512 case RISCV::PseudoCCSRLW
:
513 case RISCV::PseudoCCSRAW
:
514 case RISCV::PseudoCCSLLIW
:
515 case RISCV::PseudoCCSRLIW
:
516 case RISCV::PseudoCCSRAIW
:
517 // Returns operand 4 or an ADDW/SUBW/etc. of operands 5 and 6. We only
518 // need to check if operand 4 is sign extended.
519 if (!AddRegToWorkList(MI
->getOperand(4).getReg()))
533 case RISCV::PseudoCCMOVGPR
:
534 case RISCV::PseudoCCAND
:
535 case RISCV::PseudoCCOR
:
536 case RISCV::PseudoCCXOR
:
538 // If all incoming values are sign-extended, the output of AND, OR, XOR,
539 // MIN, MAX, or PHI is also sign-extended.
541 // The input registers for PHI are operand 1, 3, ...
542 // The input registers for PseudoCCMOVGPR are 4 and 5.
543 // The input registers for PseudoCCAND/OR/XOR are 4, 5, and 6.
544 // The input registers for others are operand 1 and 2.
545 unsigned B
= 1, E
= 3, D
= 1;
546 switch (MI
->getOpcode()) {
548 E
= MI
->getNumOperands();
551 case RISCV::PseudoCCMOVGPR
:
555 case RISCV::PseudoCCAND
:
556 case RISCV::PseudoCCOR
:
557 case RISCV::PseudoCCXOR
:
563 for (unsigned I
= B
; I
!= E
; I
+= D
) {
564 if (!MI
->getOperand(I
).isReg())
567 if (!AddRegToWorkList(MI
->getOperand(I
).getReg()))
574 case RISCV::CZERO_EQZ
:
575 case RISCV::CZERO_NEZ
:
576 case RISCV::VT_MASKC
:
577 case RISCV::VT_MASKCN
:
578 // Instructions return zero or operand 1. Result is sign extended if
579 // operand 1 is sign extended.
580 if (!AddRegToWorkList(MI
->getOperand(1).getReg()))
584 // With these opcode, we can "fix" them with the W-version
585 // if we know all users of the result only rely on bits 31:0
587 // SLLIW reads the lowest 5 bits, while SLLI reads lowest 6 bits
588 if (MI
->getOperand(2).getImm() >= 32)
597 if (hasAllWUsers(*MI
, ST
, MRI
)) {
598 FixableDef
.insert(MI
);
605 // If we get here, then every node we visited produces a sign extended value
606 // or propagated sign extended values. So the result must be sign extended.
610 static unsigned getWOp(unsigned Opcode
) {
626 llvm_unreachable("Unexpected opcode for replacement with W variant");
630 bool RISCVOptWInstrs::removeSExtWInstrs(MachineFunction
&MF
,
631 const RISCVInstrInfo
&TII
,
632 const RISCVSubtarget
&ST
,
633 MachineRegisterInfo
&MRI
) {
634 if (DisableSExtWRemoval
)
637 bool MadeChange
= false;
638 for (MachineBasicBlock
&MBB
: MF
) {
639 for (MachineInstr
&MI
: llvm::make_early_inc_range(MBB
)) {
640 // We're looking for the sext.w pattern ADDIW rd, rs1, 0.
641 if (!RISCV::isSEXT_W(MI
))
644 Register SrcReg
= MI
.getOperand(1).getReg();
646 SmallPtrSet
<MachineInstr
*, 4> FixableDefs
;
648 // If all users only use the lower bits, this sext.w is redundant.
649 // Or if all definitions reaching MI sign-extend their output,
650 // then sext.w is redundant.
651 if (!hasAllWUsers(MI
, ST
, MRI
) &&
652 !isSignExtendedW(SrcReg
, ST
, MRI
, FixableDefs
))
655 Register DstReg
= MI
.getOperand(0).getReg();
656 if (!MRI
.constrainRegClass(SrcReg
, MRI
.getRegClass(DstReg
)))
659 // Convert Fixable instructions to their W versions.
660 for (MachineInstr
*Fixable
: FixableDefs
) {
661 LLVM_DEBUG(dbgs() << "Replacing " << *Fixable
);
662 Fixable
->setDesc(TII
.get(getWOp(Fixable
->getOpcode())));
663 Fixable
->clearFlag(MachineInstr::MIFlag::NoSWrap
);
664 Fixable
->clearFlag(MachineInstr::MIFlag::NoUWrap
);
665 Fixable
->clearFlag(MachineInstr::MIFlag::IsExact
);
666 LLVM_DEBUG(dbgs() << " with " << *Fixable
);
667 ++NumTransformedToWInstrs
;
670 LLVM_DEBUG(dbgs() << "Removing redundant sign-extension\n");
671 MRI
.replaceRegWith(DstReg
, SrcReg
);
672 MRI
.clearKillFlags(SrcReg
);
673 MI
.eraseFromParent();
682 bool RISCVOptWInstrs::stripWSuffixes(MachineFunction
&MF
,
683 const RISCVInstrInfo
&TII
,
684 const RISCVSubtarget
&ST
,
685 MachineRegisterInfo
&MRI
) {
686 bool MadeChange
= false;
687 for (MachineBasicBlock
&MBB
: MF
) {
688 for (MachineInstr
&MI
: MBB
) {
690 switch (MI
.getOpcode()) {
693 case RISCV::ADDW
: Opc
= RISCV::ADD
; break;
694 case RISCV::ADDIW
: Opc
= RISCV::ADDI
; break;
695 case RISCV::MULW
: Opc
= RISCV::MUL
; break;
696 case RISCV::SLLIW
: Opc
= RISCV::SLLI
; break;
699 if (hasAllWUsers(MI
, ST
, MRI
)) {
700 MI
.setDesc(TII
.get(Opc
));
709 bool RISCVOptWInstrs::appendWSuffixes(MachineFunction
&MF
,
710 const RISCVInstrInfo
&TII
,
711 const RISCVSubtarget
&ST
,
712 MachineRegisterInfo
&MRI
) {
713 bool MadeChange
= false;
714 for (MachineBasicBlock
&MBB
: MF
) {
715 for (MachineInstr
&MI
: MBB
) {
718 switch (MI
.getOpcode()) {
734 // SLLIW reads the lowest 5 bits, while SLLI reads lowest 6 bits
735 if (MI
.getOperand(2).getImm() >= 32)
745 if (hasAllWUsers(MI
, ST
, MRI
)) {
746 LLVM_DEBUG(dbgs() << "Replacing " << MI
);
747 MI
.setDesc(TII
.get(WOpc
));
748 MI
.clearFlag(MachineInstr::MIFlag::NoSWrap
);
749 MI
.clearFlag(MachineInstr::MIFlag::NoUWrap
);
750 MI
.clearFlag(MachineInstr::MIFlag::IsExact
);
751 LLVM_DEBUG(dbgs() << " with " << MI
);
752 ++NumTransformedToWInstrs
;
761 bool RISCVOptWInstrs::runOnMachineFunction(MachineFunction
&MF
) {
762 if (skipFunction(MF
.getFunction()))
765 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
766 const RISCVSubtarget
&ST
= MF
.getSubtarget
<RISCVSubtarget
>();
767 const RISCVInstrInfo
&TII
= *ST
.getInstrInfo();
772 bool MadeChange
= false;
773 MadeChange
|= removeSExtWInstrs(MF
, TII
, ST
, MRI
);
775 if (!(DisableStripWSuffix
|| ST
.preferWInst()))
776 MadeChange
|= stripWSuffixes(MF
, TII
, ST
, MRI
);
778 if (ST
.preferWInst())
779 MadeChange
|= appendWSuffixes(MF
, TII
, ST
, MRI
);