Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion llvm/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class RISCVInstructionSelector : public InstructionSelector {
bool selectIntrinsicWithSideEffects(MachineInstr &I,
MachineIRBuilder &MIB) const;
bool selectExtractSubvector(MachineInstr &MI, MachineIRBuilder &MIB) const;

bool selectInsertSubVector(MachineInstr &I, MachineIRBuilder &MIB) const;
ComplexRendererFns selectShiftMask(MachineOperand &Root,
unsigned ShiftWidth) const;
ComplexRendererFns selectShiftMaskXLen(MachineOperand &Root) const {
Expand Down Expand Up @@ -1007,6 +1007,66 @@ bool RISCVInstructionSelector::selectExtractSubvector(
return true;
}

bool RISCVInstructionSelector::selectInsertSubVector(
MachineInstr &MI, MachineIRBuilder &MIB) const {
assert(MI.getOpcode() == TargetOpcode::G_INSERT_SUBVECTOR);

Register DstReg = MI.getOperand(0).getReg();
Register VecReg = MI.getOperand(1).getReg();
Register SubVecReg = MI.getOperand(2).getReg();

LLT VecTy = MRI->getType(VecReg);
LLT SubVecTy = MRI->getType(SubVecReg);

MVT VecMVT = getMVTForLLT(VecTy);
MVT SubVecMVT = getMVTForLLT(SubVecTy);

unsigned Idx = static_cast<unsigned>(MI.getOperand(3).getImm());

unsigned SubRegIdx;
std::tie(SubRegIdx, Idx) =
RISCVTargetLowering::decomposeSubvectorInsertExtractToSubRegs(
VecMVT, SubVecMVT, Idx, &TRI);

// If the Idx hasn't been completely eliminated then this is a subvector
// insert which doesn't naturally align to a vector register. These must
// be handled using instructions to manipulate the vector registers.
if (Idx != 0)
return false;

RISCVVType::VLMUL SubVecLMUL = RISCVTargetLowering::getLMUL(SubVecMVT);
[[maybe_unused]] bool IsSubVecPartReg =
SubVecLMUL == RISCVVType::VLMUL::LMUL_F2 ||
SubVecLMUL == RISCVVType::VLMUL::LMUL_F4 ||
SubVecLMUL == RISCVVType::VLMUL::LMUL_F8;

// Constrain dst
unsigned DstRegClassID = RISCVTargetLowering::getRegClassIDForVecVT(VecMVT);
const TargetRegisterClass *DstRC = TRI.getRegClass(DstRegClassID);
if (!RBI.constrainGenericRegister(DstReg, *DstRC, *MRI))
return false;

// If we haven't set a SubRegIdx, then we must be going between
// equally-sized LMUL groups (e.g. VR -> VR). This can be done as a copy.
if (SubRegIdx == RISCV::NoSubRegister) {
assert(RISCVTargetLowering::getRegClassIDForVecVT(SubVecMVT) ==
DstRegClassID &&
"Unexpected subvector insert");
MIB.buildInstr(TargetOpcode::COPY, {DstReg}, {}).addReg(SubVecReg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Selector should not be using MachineInstrBuilder, and use direct BuildMI calls

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that we only use MachineInstrBuilder in RISCVInstructionSelector.cpp, I think maybe refactor them all in a new pr.

MI.eraseFromParent();
return true;
}

// Use INSERT_SUBREG to insert the subvector into the vector at the
// appropriate subregister index.
auto Ins =
MIB.buildInstr(TargetOpcode::INSERT_SUBREG, {DstReg}, {VecReg, SubVecReg})
.addImm(SubRegIdx);

MI.eraseFromParent();
return constrainSelectedInstRegOperands(*Ins, TII, TRI, RBI);
}

bool RISCVInstructionSelector::select(MachineInstr &MI) {
MachineIRBuilder MIB(MI);

Expand Down Expand Up @@ -1281,6 +1341,8 @@ bool RISCVInstructionSelector::select(MachineInstr &MI) {
return selectIntrinsicWithSideEffects(MI, MIB);
case TargetOpcode::G_EXTRACT_SUBVECTOR:
return selectExtractSubvector(MI, MIB);
case TargetOpcode::G_INSERT_SUBVECTOR:
return selectInsertSubVector(MI, MIB);
default:
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25587,7 +25587,8 @@ bool RISCVTargetLowering::fallBackToDAGISel(const Instruction &Inst) const {

return false;
}
if (II->getIntrinsicID() == Intrinsic::vector_extract)
if (II->getIntrinsicID() == Intrinsic::vector_extract ||
II->getIntrinsicID() == Intrinsic::vector_insert)
return false;
}

Expand Down
Loading