[llvm] [X86] Add MI-layer routine for getting the index of the first address operand. NFC (PR #78019)
Shengchen Kan via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 15 18:06:56 PST 2024
================
@@ -3463,6 +3463,50 @@ bool X86::isX87Instruction(MachineInstr &MI) {
return false;
}
+int X86::getFirstAddrOperandIdx(const MachineInstr &MI) {
+ const auto isMemOp = [](const MCOperandInfo &OpInfo) -> bool {
+ return OpInfo.OperandType == MCOI::OPERAND_MEMORY;
+ };
+
+ const MCInstrDesc &Desc = MI.getDesc();
+
+ // Directly invoke the MC-layer routine for real (i.e., non-pseudo)
+ // instructions (fast case).
+ if (!X86II::isPseudo(Desc.TSFlags)) {
+ int MemRefIdx = X86II::getMemoryOperandNo(Desc.TSFlags);
+ if (MemRefIdx >= 0)
+ return MemRefIdx + X86II::getOperandBias(Desc);
+ assert(none_of(Desc.operands(), isMemOp) &&
+ "Got false negative from X86II::getMemoryOperandNo()!");
+ return -1;
+ }
+
+ // Otherwise, handle pseudo instructions by examining the type of their
+ // operands (slow case). An instruction cannot have a memory reference if it
+ // has fewer than AddrNumOperands (= 5) explicit operands.
+ if (Desc.getNumOperands() < X86::AddrNumOperands) {
+ assert(none_of(Desc.operands(), isMemOp) &&
+ "Expected no operands to have OPERAND_MEMORY type!");
+ return -1;
+ }
+
+ // The first operand with type OPERAND_MEMORY indicates the start of a memory
+ // reference. We expect the following AddrNumOperand-1 operands to also have
+ // OPERAND_MEMORY type.
+ for (unsigned i = 0; i <= Desc.getNumOperands() - X86::AddrNumOperands; ++i) {
+ if (Desc.operands()[i].OperandType == MCOI::OPERAND_MEMORY) {
+ assert(std::all_of(Desc.operands().begin() + i,
+ Desc.operands().begin() + i + X86::AddrNumOperands,
+ isMemOp) &&
+ "Expected all five operands in the memory reference to have "
+ "OPERAND_MEMORY type!");
----------------
KanRobert wrote:
Wrap these in
#ifdef EXPENSIVE_CHECKS
#endif
https://github.com/llvm/llvm-project/pull/78019
More information about the llvm-commits
mailing list