LLVM Operand Bypass

Using the ModulePass module, my goal is to cross the SSA graph up: moving from a single operator with 0..2 operands (most opcodes fall under this), I want to learn two things:

  • Is the operand metadata / constant (simple: just try listing in Constant-Type) or a variable?
  • If this is a variable, give me the instruction in which it is defined (since LLVM IR is in SSA form, this is a well-formed request), so I can continue this tour recursively.

As an example, suppose the following LLVM IR:

define i32 @mul_add(i32 %x, i32 %y, i32 %z) { entry: %tmp = mul i32 %x, %y %tmp2 = add i32 %tmp, %z ret i32 %tmp2 } 

I will start with the return statement. Now I want to know what I am returning:

  • I will get myself the operands of the return statement
  • I found that the operand is a variable called% tmp2
  • I will get myself the operands of the operator that defines% tmp2
  • First I will pass the first operand,% tmp
  • (...)
  • % x is a parameter of the function and therefore possibly the end of my traversal (or is it?)
  • (... continue with other branches in this DFS ...)

How can I use the C ++ API to complete these steps?

+5
source share
1 answer

The solution is simple, and I will describe it as general as possible.

Each Operand of llvm::Instruction in LLVM has a super type llvm::Value . The Value subtype is llvm::Instruction . This allows recursion using the following methods:

 bool runOnInstruction(llvm::Instruction *instruction) { bool flag = false; for (auto operand = instruction->operands().begin(); operand != instruction->operands().end(); ++operand) { printOperandStats(operand->get()); flag = runOnOperand(operand->get()) | flag; } return flag; } bool runOnOperand(llvm::Value *operand) { operand->printAsOperand(errs(), true); // ... do something else ... auto *instruction = dyn_cast<llvm::Instruction>(operand); if (nullptr != instruction) { return runOnInstruction(instruction); } else { return false; } } 

This is equal to DFS, as the question requires. Each Operand will be transferred to the Instruction and will be recursively analyzed. The boolean return value is used as usual for LLVM Passes: a value of true describes the modification of IR.

0
source

Source: https://habr.com/ru/post/1269556/


All Articles