How to locate the start if an address mode in an X86 MachineInstr?

My team interested in doing some post-RA optimizations on X86 instructions, which would require identifying memory reference instructions.

In the X86 back end instructions, memory addresses consist of a set of five operands. The offset to the start of the five operands depends on the format of the instruction. For instance, the instructions ADC32rm, ADD32rm, AND32rm, ANDN32rm, CMOVA32rm, CMOVAE32rm, CMOVB32rm, et al. all have a 32 bit memory address set starting at operand 2.

There does not seem to be any way to obtain the offset to the start of a memory reference in an X86 MachineInstr.

Tablegen code is able to determine the offset when it generates the file at build time. That code obtains the offset indirectly from the OpInfo table entry for an opcodes, which involves extracting a bit field from the OpInfo Entry, then using the extracted code in a switch statement to print out the operands for a particular class of instruction.

Would you know if there is a way, given a MachineInstr, to obtain the address offset?

Or, if not, could you sketch out a way to do that?



I think getMemoryOperandNo in X86BaseInfo.h is what you want. That’s what the MC code emitter uses to encode the memory operand.