Skip to content

Commit 20442f6

Browse files
committed
AMDGPU: Refactor isImmOperandLegal
The goal is to expose more variants that can operate without preconstructed MachineInstrs or MachineOperands.
1 parent ec7dbe2 commit 20442f6

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

llvm/lib/Target/AMDGPU/SIInstrInfo.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4572,33 +4572,43 @@ static bool compareMachineOp(const MachineOperand &Op0,
45724572
}
45734573
}
45744574

4575-
bool SIInstrInfo::isImmOperandLegal(const MCInstrDesc &InstDesc, unsigned OpNo,
4576-
const MachineOperand &MO) const {
4577-
const MCOperandInfo &OpInfo = InstDesc.operands()[OpNo];
4578-
4579-
assert(MO.isImm() || MO.isTargetIndex() || MO.isFI() || MO.isGlobal());
4580-
4575+
bool SIInstrInfo::isLiteralOperandLegal(const MCInstrDesc &InstDesc,
4576+
const MCOperandInfo &OpInfo) const {
45814577
if (OpInfo.OperandType == MCOI::OPERAND_IMMEDIATE)
45824578
return true;
45834579

4584-
if (OpInfo.RegClass < 0)
4580+
if (!RI.opCanUseLiteralConstant(OpInfo.OperandType))
45854581
return false;
45864582

4587-
if (MO.isImm() && isInlineConstant(MO, OpInfo)) {
4583+
if (!isVOP3(InstDesc) || !AMDGPU::isSISrcOperand(OpInfo))
4584+
return true;
4585+
4586+
return ST.hasVOP3Literal();
4587+
}
4588+
4589+
bool SIInstrInfo::isImmOperandLegal(const MCInstrDesc &InstDesc, unsigned OpNo,
4590+
int64_t ImmVal) const {
4591+
const MCOperandInfo &OpInfo = InstDesc.operands()[OpNo];
4592+
if (isInlineConstant(ImmVal, OpInfo.OperandType)) {
45884593
if (isMAI(InstDesc) && ST.hasMFMAInlineLiteralBug() &&
45894594
OpNo == (unsigned)AMDGPU::getNamedOperandIdx(InstDesc.getOpcode(),
45904595
AMDGPU::OpName::src2))
45914596
return false;
45924597
return RI.opCanUseInlineConstant(OpInfo.OperandType);
45934598
}
45944599

4595-
if (!RI.opCanUseLiteralConstant(OpInfo.OperandType))
4596-
return false;
4600+
return isLiteralOperandLegal(InstDesc, OpInfo);
4601+
}
45974602

4598-
if (!isVOP3(InstDesc) || !AMDGPU::isSISrcOperand(InstDesc, OpNo))
4599-
return true;
4603+
bool SIInstrInfo::isImmOperandLegal(const MCInstrDesc &InstDesc, unsigned OpNo,
4604+
const MachineOperand &MO) const {
4605+
if (MO.isImm())
4606+
return isImmOperandLegal(InstDesc, OpNo, MO.getImm());
46004607

4601-
return ST.hasVOP3Literal();
4608+
assert((MO.isTargetIndex() || MO.isFI() || MO.isGlobal()) &&
4609+
"unexpected imm-like operand kind");
4610+
const MCOperandInfo &OpInfo = InstDesc.operands()[OpNo];
4611+
return isLiteralOperandLegal(InstDesc, OpInfo);
46024612
}
46034613

46044614
bool SIInstrInfo::isLegalAV64PseudoImm(uint64_t Imm) const {
@@ -6268,7 +6278,7 @@ bool SIInstrInfo::isOperandLegal(const MachineInstr &MI, unsigned OpIdx,
62686278
return false;
62696279
}
62706280
}
6271-
} else if (AMDGPU::isSISrcOperand(InstDesc, i) &&
6281+
} else if (AMDGPU::isSISrcOperand(InstDesc.operands()[i]) &&
62726282
!isInlineConstant(Op, InstDesc.operands()[i])) {
62736283
// The same literal may be used multiple times.
62746284
if (!UsedLiteral)

llvm/lib/Target/AMDGPU/SIInstrInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,12 @@ class SIInstrInfo final : public AMDGPUGenInstrInfo {
11831183
bool isImmOperandLegal(const MCInstrDesc &InstDesc, unsigned OpNo,
11841184
const MachineOperand &MO) const;
11851185

1186+
bool isLiteralOperandLegal(const MCInstrDesc &InstDesc,
1187+
const MCOperandInfo &OpInfo) const;
1188+
1189+
bool isImmOperandLegal(const MCInstrDesc &InstDesc, unsigned OpNo,
1190+
int64_t ImmVal) const;
1191+
11861192
bool isImmOperandLegal(const MachineInstr &MI, unsigned OpNo,
11871193
const MachineOperand &MO) const {
11881194
return isImmOperandLegal(MI.getDesc(), OpNo, MO);

llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,13 +2720,6 @@ bool isInlineValue(unsigned Reg) {
27202720
#undef CASE_GFXPRE11_GFX11PLUS_TO
27212721
#undef MAP_REG2REG
27222722

2723-
bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) {
2724-
assert(OpNo < Desc.NumOperands);
2725-
unsigned OpType = Desc.operands()[OpNo].OperandType;
2726-
return OpType >= AMDGPU::OPERAND_SRC_FIRST &&
2727-
OpType <= AMDGPU::OPERAND_SRC_LAST;
2728-
}
2729-
27302723
bool isKImmOperand(const MCInstrDesc &Desc, unsigned OpNo) {
27312724
assert(OpNo < Desc.NumOperands);
27322725
unsigned OpType = Desc.operands()[OpNo].OperandType;

llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,14 @@ bool isInlineValue(unsigned Reg);
15901590

15911591
/// Is this an AMDGPU specific source operand? These include registers,
15921592
/// inline constants, literals and mandatory literals (KImm).
1593-
bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo);
1593+
constexpr bool isSISrcOperand(const MCOperandInfo &OpInfo) {
1594+
return OpInfo.OperandType >= AMDGPU::OPERAND_SRC_FIRST &&
1595+
OpInfo.OperandType <= AMDGPU::OPERAND_SRC_LAST;
1596+
}
1597+
1598+
constexpr bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) {
1599+
return isSISrcOperand(Desc.operands()[OpNo]);
1600+
}
15941601

15951602
/// Is this a KImm operand?
15961603
bool isKImmOperand(const MCInstrDesc &Desc, unsigned OpNo);

0 commit comments

Comments
 (0)