45#define DEBUG_TYPE "riscv-opt-w-instrs"
46#define RISCV_OPT_W_INSTRS_NAME "RISC-V Optimize W Instructions"
48STATISTIC(NumRemovedSExtW,
"Number of removed sign-extensions");
50 "Number of instructions transformed to W-ops");
52 "Number of instructions transformed to non-W-ops");
55 cl::desc(
"Disable removal of sext.w"),
86char RISCVOptWInstrs::ID = 0;
91 return new RISCVOptWInstrs();
112 auto NumDemandedBits =
114 return NumDemandedBits && Bits >= *NumDemandedBits;
129 while (!Worklist.
empty()) {
132 unsigned Bits =
P.second;
138 if (
MI->getNumExplicitDefs() != 1)
141 Register DestReg =
MI->getOperand(0).getReg();
145 for (
auto &UserOp :
MRI.use_nodbg_operands(DestReg)) {
176 case RISCV::FCVT_H_W:
177 case RISCV::FCVT_H_W_INX:
178 case RISCV::FCVT_H_WU:
179 case RISCV::FCVT_H_WU_INX:
180 case RISCV::FCVT_S_W:
181 case RISCV::FCVT_S_W_INX:
182 case RISCV::FCVT_S_WU:
183 case RISCV::FCVT_S_WU_INX:
184 case RISCV::FCVT_D_W:
185 case RISCV::FCVT_D_W_INX:
186 case RISCV::FCVT_D_WU:
187 case RISCV::FCVT_D_WU_INX:
199 case RISCV::ZEXT_H_RV32:
200 case RISCV::ZEXT_H_RV64:
207 if (Bits >= (ST.getXLen() / 2))
227 if (Bits >= (ST.getXLen() - ShAmt))
234 if (Bits >= 32 - ShAmt)
249 if (Bits >= (
unsigned)llvm::bit_width<uint64_t>(~Imm))
261 if (Bits >=
Log2_32(ST.getXLen()))
278 case RISCV::SH1ADD_UW:
279 case RISCV::SH2ADD_UW:
280 case RISCV::SH3ADD_UW:
282 if (
OpIdx == 1 && Bits >= 32)
294 if (
OpIdx == 0 && Bits >= 8)
299 if (
OpIdx == 0 && Bits >= 16)
304 if (
OpIdx == 0 && Bits >= 32)
341 case RISCV::PseudoCCMOVGPR:
342 case RISCV::PseudoCCMOVGPRNoX0:
351 case RISCV::CZERO_EQZ:
352 case RISCV::CZERO_NEZ:
353 case RISCV::VT_MASKC:
354 case RISCV::VT_MASKCN:
381 switch (
MI.getOpcode()) {
384 return MI.getOperand(2).getImm() >= 32;
386 return MI.getOperand(2).getImm() > 32;
389 return MI.getOperand(1).isReg() &&
MI.getOperand(1).getReg() == RISCV::X0;
392 return isUInt<11>(
MI.getOperand(2).getImm());
395 return !isUInt<11>(
MI.getOperand(2).getImm());
398 return MI.getOperand(2).getImm() < 31 &&
399 MI.getOperand(1).getReg() == RISCV::X0;
402 return MI.getOperand(1).getReg() == RISCV::X0;
404 case RISCV::PseudoAtomicLoadNand32:
406 case RISCV::PseudoVMV_X_S: {
408 int64_t Log2SEW =
MI.getOperand(2).getImm();
409 assert(Log2SEW >= 3 && Log2SEW <= 6 &&
"Unexpected Log2SEW");
423 auto AddRegToWorkList = [&](
Register SrcReg) {
430 if (!AddRegToWorkList(SrcReg))
433 while (!Worklist.
empty()) {
437 if (!Visited.
insert(Reg).second)
444 int OpNo =
MI->findRegisterDefOperandIdx(Reg,
nullptr);
445 assert(OpNo != -1 &&
"Couldn't find register");
452 switch (
MI->getOpcode()) {
463 if (
MI->getParent() == &MF->
front()) {
469 Register CopySrcReg =
MI->getOperand(1).getReg();
470 if (CopySrcReg == RISCV::X10) {
480 auto II =
MI->getIterator();
494 auto *IntTy = dyn_cast<IntegerType>(CalleeFn->getReturnType());
498 const AttributeSet &Attrs = CalleeFn->getAttributes().getRetAttrs();
499 unsigned BitWidth = IntTy->getBitWidth();
500 if ((
BitWidth <= 32 && Attrs.hasAttribute(Attribute::SExt)) ||
501 (
BitWidth < 32 && Attrs.hasAttribute(Attribute::ZExt)))
505 if (!AddRegToWorkList(CopySrcReg))
515 if (
MI->getOperand(2).getImm() >= 31)
525 if (!AddRegToWorkList(
MI->getOperand(1).getReg()))
529 case RISCV::PseudoCCADDW:
530 case RISCV::PseudoCCADDIW:
531 case RISCV::PseudoCCSUBW:
532 case RISCV::PseudoCCSLLW:
533 case RISCV::PseudoCCSRLW:
534 case RISCV::PseudoCCSRAW:
535 case RISCV::PseudoCCSLLIW:
536 case RISCV::PseudoCCSRLIW:
537 case RISCV::PseudoCCSRAIW:
540 if (!AddRegToWorkList(
MI->getOperand(4).getReg()))
554 case RISCV::PseudoCCMOVGPR:
555 case RISCV::PseudoCCMOVGPRNoX0:
556 case RISCV::PseudoCCAND:
557 case RISCV::PseudoCCOR:
558 case RISCV::PseudoCCXOR:
567 unsigned B = 1, E = 3,
D = 1;
568 switch (
MI->getOpcode()) {
570 E =
MI->getNumOperands();
573 case RISCV::PseudoCCMOVGPR:
574 case RISCV::PseudoCCMOVGPRNoX0:
578 case RISCV::PseudoCCAND:
579 case RISCV::PseudoCCOR:
580 case RISCV::PseudoCCXOR:
586 for (
unsigned I =
B;
I != E;
I +=
D) {
587 if (!
MI->getOperand(
I).isReg())
590 if (!AddRegToWorkList(
MI->getOperand(
I).getReg()))
597 case RISCV::CZERO_EQZ:
598 case RISCV::CZERO_NEZ:
599 case RISCV::VT_MASKC:
600 case RISCV::VT_MASKCN:
603 if (!AddRegToWorkList(
MI->getOperand(1).getReg()))
608 if (
MI->getOperand(1).isReg() &&
MI->getOperand(1).getReg().isVirtual()) {
610 if (SrcMI->getOpcode() == RISCV::LUI &&
611 SrcMI->getOperand(1).isImm()) {
612 uint64_t Imm = SrcMI->getOperand(1).getImm();
613 Imm = SignExtend64<32>(Imm << 12);
632 if (
MI->getOperand(2).getImm() >= 32)
680 bool MadeChange =
false;
684 if (!RISCVInstrInfo::isSEXT_W(
MI))
699 if (!
MRI.constrainRegClass(SrcReg,
MRI.getRegClass(DstReg)))
705 Fixable->setDesc(
TII.get(
getWOp(Fixable->getOpcode())));
706 Fixable->clearFlag(MachineInstr::MIFlag::NoSWrap);
707 Fixable->clearFlag(MachineInstr::MIFlag::NoUWrap);
708 Fixable->clearFlag(MachineInstr::MIFlag::IsExact);
710 ++NumTransformedToWInstrs;
714 MRI.replaceRegWith(DstReg, SrcReg);
715 MRI.clearKillFlags(SrcReg);
716 MI.eraseFromParent();
732 bool ShouldPreferW =
ST.preferWInst();
733 bool MadeChange =
false;
737 std::optional<unsigned> WOpc;
738 std::optional<unsigned> NonWOpc;
739 unsigned OrigOpc =
MI.getOpcode();
744 NonWOpc = RISCV::ADD;
747 NonWOpc = RISCV::ADDI;
750 NonWOpc = RISCV::MUL;
753 NonWOpc = RISCV::SLLI;
756 NonWOpc = RISCV::SUB;
772 if (
MI.getOperand(2).getImm() >= 32)
784 MI.setDesc(
TII.get(NonWOpc.value()));
786 ++NumTransformedToNonWInstrs;
792 if ((ShouldPreferW || OrigOpc == RISCV::LWU) && WOpc.has_value() &&
795 MI.setDesc(
TII.get(WOpc.value()));
796 MI.clearFlag(MachineInstr::MIFlag::NoSWrap);
797 MI.clearFlag(MachineInstr::MIFlag::NoUWrap);
798 MI.clearFlag(MachineInstr::MIFlag::IsExact);
800 ++NumTransformedToWInstrs;
820 bool MadeChange =
false;
821 MadeChange |= removeSExtWInstrs(MF,
TII, ST,
MRI);
822 MadeChange |= canonicalizeWSuffixes(MF,
TII, ST,
MRI);
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
const HexagonInstrInfo * TII
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static bool isSignExtendedW(Register SrcReg, const RISCVSubtarget &ST, const MachineRegisterInfo &MRI, SmallPtrSetImpl< MachineInstr * > &FixableDef)
static bool hasAllWUsers(const MachineInstr &OrigMI, const RISCVSubtarget &ST, const MachineRegisterInfo &MRI)
static bool isSignExtendingOpW(const MachineInstr &MI, unsigned OpNo)
static cl::opt< bool > DisableStripWSuffix("riscv-disable-strip-w-suffix", cl::desc("Disable strip W suffix"), cl::init(false), cl::Hidden)
static bool hasAllNBitUsers(const MachineInstr &OrigMI, const RISCVSubtarget &ST, const MachineRegisterInfo &MRI, unsigned OrigBits)
#define RISCV_OPT_W_INSTRS_NAME
static bool vectorPseudoHasAllNBitUsers(const MachineOperand &UserOp, unsigned Bits)
static cl::opt< bool > DisableSExtWRemoval("riscv-disable-sextw-removal", cl::desc("Disable removal of sext.w"), cl::init(false), cl::Hidden)
static unsigned getWOp(unsigned Opcode)
This file defines the SmallSet class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
FunctionPass class - This class is used to implement most global optimizations.
Describe properties that are true of each instruction in the target description file.
instr_iterator instr_begin()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
bool isCall(QueryType Type=AnyInBundle) const
unsigned getOperandNo(const_mop_iterator I) const
Returns the number of the operand iterator I points to.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
LLVM_ABI unsigned getOperandNo() const
Returns the index of this operand in the instruction that it belongs to.
const GlobalValue * getGlobal() const
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI bool isLiveIn(Register Reg) const
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
RISCVMachineFunctionInfo - This class is derived from MachineFunctionInfo and contains private RISCV-...
bool isSExt32Register(Register Reg) const
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
static unsigned getVLOpNum(const MCInstrDesc &Desc)
static bool hasVLOp(uint64_t TSFlags)
static unsigned getSEWOpNum(const MCInstrDesc &Desc)
static bool hasSEWOp(uint64_t TSFlags)
unsigned getRVVMCOpcode(unsigned RVVPseudoOpcode)
std::optional< unsigned > getVectorLowDemandedScalarBits(unsigned Opcode, unsigned Log2SEW)
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createRISCVOptWInstrsPass()
constexpr unsigned BitWidth