[LLVMdev] LLVM Instruction Operands

John McCall rjmccall at apple.com
Thu Jun 2 10:37:47 PDT 2011


On Jun 2, 2011, at 9:46 AM, Griffin Wright wrote:

> 
> Hello all:
> 
> I apologize for what I imagine is a rather silly question, but I'm still
> somewhat new to LLVM and am stuck.  I am reworking some code that was
> originally in the backend involving MachineInstructions and
> MachineOperands, and I now need for it to function as an LLVM IR pass,
> using just Instructions, etc, and nothing related to 'Machine'.
> 
> However, I am not sure how to get the information I need at the IR level. 
> I need to iterate over all operands of an instruction, analyzing each
> operand to get a register number (if available), see if the operand is a
> register or an immediate, get the operand def, and some other stuff.  I
> also need to be able to do implicitDef/Use determination on instructions. 
> I don't see a way to do this at present.

If your pass really needs to analyze the use of physical registers and
whether individual machine instructions are using immediate operands
or not, then it needs to stay at the machine level.  If you only really care
about higher-level things like (1) what operation is being performed,
(2) whether particular operands are constant or not, and (3) if not, how
and when they were produced, then it's far better to work on LLVM IR.

On IR, you test for specific kinds of values using isa or dyn_cast.
For example, I can check whether a value is a 'mul' instruction:
  if (BinaryOperator *op = dyn_cast<BinaryOperator>(val)) {
    if (op->getOpcode() == Instruction::Mul) {
and now I can check whether the LHS is a call:
      if (isa<CallInst>(op->getOperand(0))) {
etc.

John.



More information about the llvm-dev mailing list