39#define DEBUG_TYPE "riscv-vector-peephole"
55 MachineFunctionProperties::Property::IsSSA);
59 return "RISC-V Vector Peephole Optimization";
83char RISCVVectorPeephole::ID = 0;
92 unsigned UserLog2SEW =
98 return SrcLog2EEW == UserLog2SEW;
130 case RISCV::VMERGE_VVM:
133 case RISCV::VREDSUM_VS:
134 case RISCV::VREDMAXU_VS:
135 case RISCV::VREDMAX_VS:
136 case RISCV::VREDMINU_VS:
137 case RISCV::VREDMIN_VS:
138 case RISCV::VREDAND_VS:
139 case RISCV::VREDOR_VS:
140 case RISCV::VREDXOR_VS:
141 case RISCV::VWREDSUM_VS:
142 case RISCV::VWREDSUMU_VS:
143 case RISCV::VFREDUSUM_VS:
144 case RISCV::VFREDOSUM_VS:
145 case RISCV::VFREDMAX_VS:
146 case RISCV::VFREDMIN_VS:
147 case RISCV::VFWREDUSUM_VS:
148 case RISCV::VFWREDOSUM_VS:
157 Register SrcReg =
MI.getOperand(SrcIdx).getReg();
159 if (!
MRI->hasOneUse(SrcReg))
163 if (!Src || Src->hasUnmodeledSideEffects() ||
164 Src->getParent() !=
MI.getParent() || Src->getNumDefs() != 1 ||
170 if (!hasSameEEW(
MI, *Src))
175 if (ElementsDependOnVL || Src->mayRaiseFPException())
182 if (!ensureDominates(VL, *Src))
196std::optional<unsigned>
202 if (!Def ||
Def->getOpcode() != RISCV::ADDI ||
203 Def->getOperand(1).getReg() != RISCV::X0)
205 return Def->getOperand(2).getImm();
216 unsigned LMULFixed =
LMUL.second ? (8 /
LMUL.first) : 8 *
LMUL.first;
219 unsigned SEW = Log2SEW ? 1 << Log2SEW : 8;
221 assert(8 * LMULFixed / SEW > 0);
226 VLen && AVL && (*VLen * LMULFixed) / SEW == *AVL * 8) {
242 if (
Def->getOpcode() == RISCV::SLLI) {
243 assert(
Def->getOperand(2).getImm() < 64);
244 ScaleFixed <<=
Def->getOperand(2).getImm();
245 Def =
MRI->getVRegDef(
Def->getOperand(1).getReg());
246 }
else if (
Def->getOpcode() == RISCV::SRLI) {
247 assert(
Def->getOperand(2).getImm() < 64);
248 ScaleFixed >>=
Def->getOperand(2).getImm();
249 Def =
MRI->getVRegDef(
Def->getOperand(1).getReg());
252 if (!Def ||
Def->getOpcode() != RISCV::PseudoReadVLENB)
262 if (ScaleFixed != 8 * LMULFixed / SEW)
270bool RISCVVectorPeephole::isAllOnesMask(
const MachineInstr *MaskDef)
const {
276 MaskDef =
MRI->getVRegDef(SrcReg);
284 case RISCV::PseudoVMSET_M_B1:
285 case RISCV::PseudoVMSET_M_B2:
286 case RISCV::PseudoVMSET_M_B4:
287 case RISCV::PseudoVMSET_M_B8:
288 case RISCV::PseudoVMSET_M_B16:
289 case RISCV::PseudoVMSET_M_B32:
290 case RISCV::PseudoVMSET_M_B64:
307bool RISCVVectorPeephole::convertToWholeRegister(
MachineInstr &
MI)
const {
308#define CASE_WHOLE_REGISTER_LMUL_SEW(lmul, sew) \
309 case RISCV::PseudoVLE##sew##_V_M##lmul: \
310 NewOpc = RISCV::VL##lmul##RE##sew##_V; \
312 case RISCV::PseudoVSE##sew##_V_M##lmul: \
313 NewOpc = RISCV::VS##lmul##R_V; \
315#define CASE_WHOLE_REGISTER_LMUL(lmul) \
316 CASE_WHOLE_REGISTER_LMUL_SEW(lmul, 8) \
317 CASE_WHOLE_REGISTER_LMUL_SEW(lmul, 16) \
318 CASE_WHOLE_REGISTER_LMUL_SEW(lmul, 32) \
319 CASE_WHOLE_REGISTER_LMUL_SEW(lmul, 64)
322 switch (
MI.getOpcode()) {
332 if (!VLOp.
isImm() || VLOp.getImm() != RISCV::VLMaxSentinel)
337 if (RISCVII::hasVecPolicyOp(
MI.getDesc().TSFlags))
338 MI.removeOperand(RISCVII::getVecPolicyOpNum(
MI.getDesc()));
339 MI.removeOperand(RISCVII::getSEWOpNum(
MI.getDesc()));
340 MI.removeOperand(RISCVII::getVLOpNum(
MI.getDesc()));
341 if (RISCVII::isFirstDefTiedToFirstUse(
MI.getDesc()))
350#define CASE_VMERGE_TO_VMV(lmul) \
351 case RISCV::PseudoVMERGE_VVM_##lmul: \
352 return RISCV::PseudoVMV_V_V_##lmul;
353 switch (
MI.getOpcode()) {
371bool RISCVVectorPeephole::convertAllOnesVMergeToVMv(
MachineInstr &
MI)
const {
372 unsigned NewOpc = getVMV_V_VOpcodeForVMERGE_VVM(
MI);
375 assert(
MI.getOperand(4).isReg() &&
MI.getOperand(4).getReg() == RISCV::V0);
376 if (!isAllOnesMask(V0Defs.lookup(&
MI)))
379 MI.setDesc(
TII->get(NewOpc));
387 MRI->recomputeRegClass(
MI.getOperand(0).getReg());
388 if (
MI.getOperand(1).getReg() != RISCV::NoRegister)
389 MRI->recomputeRegClass(
MI.getOperand(1).getReg());
402bool RISCVVectorPeephole::convertSameMaskVMergeToVMv(
MachineInstr &
MI) {
403 unsigned NewOpc = getVMV_V_VOpcodeForVMERGE_VVM(
MI);
407 if (!True || True->
getParent() !=
MI.getParent() ||
408 !RISCV::getMaskedPseudoInfo(True->
getOpcode()) || !hasSameEEW(
MI, *True))
419 Register FalseReg =
MI.getOperand(2).getReg();
420 if (TruePassthruReg != FalseReg) {
422 if (TruePassthruReg != RISCV::NoRegister ||
423 !
MRI->hasOneUse(
MI.getOperand(3).getReg()) ||
424 !ensureDominates(
MI.getOperand(2), *True))
433 MI.setDesc(
TII->get(NewOpc));
441 MRI->recomputeRegClass(
MI.getOperand(0).getReg());
442 if (
MI.getOperand(1).getReg() != RISCV::NoRegister)
443 MRI->recomputeRegClass(
MI.getOperand(1).getReg());
447bool RISCVVectorPeephole::convertToUnmasked(
MachineInstr &
MI)
const {
449 RISCV::getMaskedPseudoInfo(
MI.getOpcode());
453 if (!isAllOnesMask(V0Defs.lookup(&
MI)))
458 const unsigned Opc =
I->UnmaskedPseudo;
460 [[maybe_unused]]
const bool HasPolicyOp =
467 "Masked and unmasked pseudos are inconsistent");
468 assert(HasPolicyOp == HasPassthru &&
"Unexpected pseudo structure");
475 unsigned MaskOpIdx =
I->MaskOpIdx +
MI.getNumExplicitDefs();
476 MI.removeOperand(MaskOpIdx);
480 MRI->recomputeRegClass(
MI.getOperand(0).getReg());
481 unsigned PassthruOpIdx =
MI.getNumExplicitDefs();
483 if (
MI.getOperand(PassthruOpIdx).getReg() != RISCV::NoRegister)
484 MRI->recomputeRegClass(
MI.getOperand(PassthruOpIdx).getReg());
486 MI.removeOperand(PassthruOpIdx);
497 if (MO.getReg().isPhysical())
499 bool SawStore =
false;
502 if (
II->definesRegister(PhysReg,
nullptr))
504 if (
II->mayStore()) {
509 return From.isSafeToMove(SawStore);
515 assert(
A->getParent() ==
B->getParent());
522 for (; &*
I !=
A && &*
I !=
B; ++
I)
531bool RISCVVectorPeephole::ensureDominates(
const MachineOperand &MO,
534 if (!MO.
isReg() || MO.
getReg() == RISCV::NoRegister)
538 if (
Def->getParent() == Src.getParent() && !
dominates(Def, Src)) {
541 Src.moveBefore(
Def->getNextNode());
548bool RISCVVectorPeephole::foldUndefPassthruVMV_V_V(
MachineInstr &
MI) {
551 if (
MI.getOperand(1).getReg() != RISCV::NoRegister)
557 if (Src && !Src->hasUnmodeledSideEffects() &&
558 MRI->hasOneUse(
MI.getOperand(2).getReg()) &&
572 MRI->replaceRegWith(
MI.getOperand(0).getReg(),
MI.getOperand(2).getReg());
573 MI.eraseFromParent();
594 if (!
MRI->hasOneUse(
MI.getOperand(2).getReg()))
598 if (!Src || Src->hasUnmodeledSideEffects() ||
599 Src->getParent() !=
MI.getParent() || Src->getNumDefs() != 1 ||
606 if (!hasSameEEW(
MI, *Src))
611 if (SrcPassthru.
getReg() != RISCV::NoRegister &&
623 if (!ensureDominates(Passthru, *Src))
629 if (Passthru.
getReg() != RISCV::NoRegister)
630 MRI->constrainRegClass(Passthru.
getReg(),
631 TII->getRegClass(Src->getDesc(), 1,
TRI,
632 *Src->getParent()->getParent()));
642 MRI->replaceRegWith(
MI.getOperand(0).getReg(), Src->getOperand(0).getReg());
643 MI.eraseFromParent();
655 if (!
ST->hasVInstructions())
658 TII =
ST->getInstrInfo();
660 TRI =
MRI->getTargetRegisterInfo();
662 bool Changed =
false;
674 if (
MI.readsRegister(RISCV::V0,
TRI))
675 V0Defs[&
MI] = CurrentV0Def;
677 if (
MI.definesRegister(RISCV::V0,
TRI))
684 Changed |= convertToVLMAX(
MI);
685 Changed |= tryToReduceVL(
MI);
686 Changed |= convertToUnmasked(
MI);
687 Changed |= convertToWholeRegister(
MI);
688 Changed |= convertAllOnesVMergeToVMv(
MI);
689 Changed |= convertSameMaskVMergeToVMv(
MI);
690 if (foldUndefPassthruVMV_V_V(
MI)) {
694 Changed |= foldVMV_V_V(
MI);
702 return new RISCVVectorPeephole();
unsigned const MachineRegisterInfo * MRI
static uint64_t getConstant(const Value *IndexValue)
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
const HexagonInstrInfo * TII
unsigned const TargetRegisterInfo * TRI
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
#define CASE_WHOLE_REGISTER_LMUL(lmul)
#define CASE_VMERGE_TO_VMV(lmul)
static bool isSafeToMove(const MachineInstr &From, const MachineInstr &To)
Check if it's safe to move From down to To, checking that no physical registers are clobbered.
static bool dominates(MachineBasicBlock::const_iterator A, MachineBasicBlock::const_iterator B)
Given A and B are in the same MBB, returns true if A comes before B.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool isImm(const MachineOperand &MO, MachineRegisterInfo *MRI)
FunctionPass class - This class is used to implement most global optimizations.
Describe properties that are true of each instruction in the target description file.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
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.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
void setImm(int64_t immVal)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void setReg(Register Reg)
Change the register this operand corresponds to.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags=0)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value.
void ChangeToRegister(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static MachineOperand CreateImm(int64_t Val)
Register getReg() const
getReg - Returns the register number.
bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
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.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
A Use represents the edge between a Value definition and its users.
Value * getOperand(unsigned i) const
self_iterator getIterator()
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
static unsigned getVecPolicyOpNum(const MCInstrDesc &Desc)
@ TAIL_UNDISTURBED_MASK_UNDISTURBED
static unsigned getVLOpNum(const MCInstrDesc &Desc)
static VLMUL getLMul(uint64_t TSFlags)
static bool hasVLOp(uint64_t TSFlags)
static bool hasVecPolicyOp(uint64_t TSFlags)
static unsigned getSEWOpNum(const MCInstrDesc &Desc)
static bool elementsDependOnVL(uint64_t TSFlags)
static bool hasSEWOp(uint64_t TSFlags)
static bool isFirstDefTiedToFirstUse(const MCInstrDesc &Desc)
std::pair< unsigned, bool > decodeVLMUL(RISCVII::VLMUL VLMUL)
static bool isValidSEW(unsigned SEW)
bool isVLKnownLE(const MachineOperand &LHS, const MachineOperand &RHS)
Given two VL operands, do we know that LHS <= RHS?
unsigned getRVVMCOpcode(unsigned RVVPseudoOpcode)
unsigned getDestLog2EEW(const MCInstrDesc &Desc, unsigned Log2SEW)
static constexpr int64_t VLMaxSentinel
NodeAddr< DefNode * > Def
This is an optimization pass for GlobalISel generic memory operations.
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...
unsigned M1(unsigned Val)
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
FunctionPass * createRISCVVectorPeepholePass()