40#define DEBUG_TYPE "riscv-vector-peephole"
56 MachineFunctionProperties::Property::IsSSA);
60 return "RISC-V Vector Peephole Optimization";
79char RISCVVectorPeephole::ID = 0;
85std::optional<
unsigned>
91 if (!Def || Def->getOpcode() != RISCV::ADDI ||
92 Def->getOperand(1).getReg() != RISCV::X0)
94 return Def->getOperand(2).getImm();
105 unsigned LMULFixed =
LMUL.second ? (8 /
LMUL.first) : 8 *
LMUL.first;
108 unsigned SEW = Log2SEW ? 1 << Log2SEW : 8;
110 assert(8 * LMULFixed / SEW > 0);
115 VLen && AVL && (*VLen * LMULFixed) / SEW == *AVL * 8) {
131 if (
Def->getOpcode() == RISCV::SLLI) {
132 assert(
Def->getOperand(2).getImm() < 64);
133 ScaleFixed <<=
Def->getOperand(2).getImm();
134 Def =
MRI->getVRegDef(
Def->getOperand(1).getReg());
135 }
else if (
Def->getOpcode() == RISCV::SRLI) {
136 assert(
Def->getOperand(2).getImm() < 64);
137 ScaleFixed >>=
Def->getOperand(2).getImm();
138 Def =
MRI->getVRegDef(
Def->getOperand(1).getReg());
141 if (!Def ||
Def->getOpcode() != RISCV::PseudoReadVLENB)
151 if (ScaleFixed != 8 * LMULFixed / SEW)
159bool RISCVVectorPeephole::isAllOnesMask(
const MachineInstr *MaskDef)
const {
165 MaskDef =
MRI->getVRegDef(SrcReg);
173 case RISCV::PseudoVMSET_M_B1:
174 case RISCV::PseudoVMSET_M_B2:
175 case RISCV::PseudoVMSET_M_B4:
176 case RISCV::PseudoVMSET_M_B8:
177 case RISCV::PseudoVMSET_M_B16:
178 case RISCV::PseudoVMSET_M_B32:
179 case RISCV::PseudoVMSET_M_B64:
196bool RISCVVectorPeephole::convertToWholeRegister(
MachineInstr &
MI)
const {
197#define CASE_WHOLE_REGISTER_LMUL_SEW(lmul, sew) \
198 case RISCV::PseudoVLE##sew##_V_M##lmul: \
199 NewOpc = RISCV::VL##lmul##RE##sew##_V; \
201 case RISCV::PseudoVSE##sew##_V_M##lmul: \
202 NewOpc = RISCV::VS##lmul##R_V; \
204#define CASE_WHOLE_REGISTER_LMUL(lmul) \
205 CASE_WHOLE_REGISTER_LMUL_SEW(lmul, 8) \
206 CASE_WHOLE_REGISTER_LMUL_SEW(lmul, 16) \
207 CASE_WHOLE_REGISTER_LMUL_SEW(lmul, 32) \
208 CASE_WHOLE_REGISTER_LMUL_SEW(lmul, 64)
211 switch (
MI.getOpcode()) {
221 if (!VLOp.
isImm() || VLOp.getImm() != RISCV::VLMaxSentinel)
226 if (RISCVII::hasVecPolicyOp(
MI.getDesc().TSFlags))
227 MI.removeOperand(RISCVII::getVecPolicyOpNum(
MI.getDesc()));
228 MI.removeOperand(RISCVII::getSEWOpNum(
MI.getDesc()));
229 MI.removeOperand(RISCVII::getVLOpNum(
MI.getDesc()));
230 if (RISCVII::isFirstDefTiedToFirstUse(
MI.getDesc()))
240bool RISCVVectorPeephole::convertVMergeToVMv(
MachineInstr &
MI)
const {
241#define CASE_VMERGE_TO_VMV(lmul) \
242 case RISCV::PseudoVMERGE_VVM_##lmul: \
243 NewOpc = RISCV::PseudoVMV_V_V_##lmul; \
246 switch (
MI.getOpcode()) {
258 Register PassthruReg =
MI.getOperand(1).getReg();
259 Register FalseReg =
MI.getOperand(2).getReg();
261 if (PassthruReg != RISCV::NoRegister &&
262 TRI->lookThruCopyLike(PassthruReg,
MRI) !=
263 TRI->lookThruCopyLike(FalseReg,
MRI))
266 assert(
MI.getOperand(4).isReg() &&
MI.getOperand(4).getReg() == RISCV::V0);
267 if (!isAllOnesMask(V0Defs.lookup(&
MI)))
270 MI.setDesc(
TII->get(NewOpc));
272 MI.tieOperands(0, 1);
279 MRI->recomputeRegClass(
MI.getOperand(0).getReg());
280 MRI->recomputeRegClass(
MI.getOperand(1).getReg());
284bool RISCVVectorPeephole::convertToUnmasked(
MachineInstr &
MI)
const {
286 RISCV::getMaskedPseudoInfo(
MI.getOpcode());
290 if (!isAllOnesMask(V0Defs.lookup(&
MI)))
295 const unsigned Opc =
I->UnmaskedPseudo;
297 [[maybe_unused]]
const bool HasPolicyOp =
304 "Masked and unmasked pseudos are inconsistent");
305 assert(HasPolicyOp == HasPassthru &&
"Unexpected pseudo structure");
312 unsigned MaskOpIdx =
I->MaskOpIdx +
MI.getNumExplicitDefs();
313 MI.removeOperand(MaskOpIdx);
317 MRI->recomputeRegClass(
MI.getOperand(0).getReg());
318 unsigned PassthruOpIdx =
MI.getNumExplicitDefs();
320 if (
MI.getOperand(PassthruOpIdx).getReg() != RISCV::NoRegister)
321 MRI->recomputeRegClass(
MI.getOperand(PassthruOpIdx).getReg());
323 MI.removeOperand(PassthruOpIdx);
332 if (
LHS->isReg() &&
RHS->isReg() &&
LHS->getReg().isVirtual() &&
333 LHS->getReg() ==
RHS->getReg())
339 if (!
LHS->isImm() || !
RHS->isImm())
350 if (MO.getReg().isPhysical())
352 bool SawStore =
false;
355 if (
II->definesRegister(PhysReg,
nullptr))
357 if (
II->mayStore()) {
362 return From.isSafeToMove(SawStore);
386 if (!
MRI->hasOneUse(
MI.getOperand(2).getReg()))
390 if (!Src || Src->hasUnmodeledSideEffects() ||
391 Src->getParent() !=
MI.getParent() || Src->getNumDefs() != 1 ||
403 if (SrcPassthru.
getReg() != RISCV::NoRegister &&
428 if (VLChanged && (ActiveElementsAffectResult || Src->mayRaiseFPException()))
436 Src->moveBefore(&
MI);
442 if (Passthru.
getReg() != RISCV::NoRegister)
443 MRI->constrainRegClass(Passthru.
getReg(),
444 TII->getRegClass(Src->getDesc(), 1,
TRI,
445 *Src->getParent()->getParent()));
450 else if (MinVL->
isReg())
458 MRI->replaceRegWith(
MI.getOperand(0).getReg(), Src->getOperand(0).getReg());
459 MI.eraseFromParent();
471 if (!
ST->hasVInstructions())
474 TII =
ST->getInstrInfo();
476 TRI =
MRI->getTargetRegisterInfo();
478 bool Changed =
false;
490 if (
MI.readsRegister(RISCV::V0,
TRI))
491 V0Defs[&
MI] = CurrentV0Def;
493 if (
MI.definesRegister(RISCV::V0,
TRI))
500 Changed |= convertToVLMAX(
MI);
501 Changed |= convertToUnmasked(
MI);
502 Changed |= convertToWholeRegister(
MI);
503 Changed |= convertVMergeToVMv(
MI);
504 Changed |= foldVMV_V_V(
MI);
512 return new RISCVVectorPeephole();
unsigned const MachineRegisterInfo * MRI
static uint64_t getConstant(const Value *IndexValue)
BlockVerifier::State From
const HexagonInstrInfo * TII
unsigned const TargetRegisterInfo * TRI
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static unsigned getSEWLMULRatio(const MachineInstr &MI)
#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.
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.
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 MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
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.
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...
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 bool activeElementsAffectResult(uint64_t TSFlags)
static unsigned getSEWOpNum(const MCInstrDesc &Desc)
static bool hasSEWOp(uint64_t TSFlags)
static bool isFirstDefTiedToFirstUse(const MCInstrDesc &Desc)
std::pair< unsigned, bool > decodeVLMUL(RISCVII::VLMUL VLMUL)
unsigned getSEWLMULRatio(unsigned SEW, RISCVII::VLMUL VLMul)
static bool isValidSEW(unsigned SEW)
unsigned getRVVMCOpcode(unsigned RVVPseudoOpcode)
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()
Implement std::hash so that hash_code can be used in STL containers.