Using a ModulePass, my goal is to traverse a SSA graph upwards: going from one Statement with 0..2 operands (most opcodes fall under that), I want to find out two things:
- Is an operand a metadata / constant (easy: just try casting to Constant-Type) or a variable?
- If it is a variable, get me the Statement where it is defined (as LLVM IR is in SSA form, this is a well-formed query), so I can recursively continue this traversal.
As an example, assume 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 be starting with the return statement. Now I want to find out what I'm returning:
- I will get myself the operands of the return statement
- I detect that the operand is a variable called %tmp2
- I will get myself the operands of the statement where %tmp2 is defined
- I will first traverse the first operand, %tmp
- (...)
- %x is a function parameter and therefore probably the end of my traversal (or is it?)
- (... continue with the other branches in this DFS ...)
How would I use the C++ API to implement those steps?