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?
source share