In llvm, can one trace back to the instruction that defines the value for a particular register? For example, if I have an instruction as:
%add14 = add i32 %add7, %add5
Is here a way for me to trace back to the instruction where add5 is defined?
First of all, there are no registers in LLVM IR: all those things with
%in their names are just names of values. You don’t store information inside those things, they are not variables or memory locations, they are just names. I recommend reading about SSA form, which helps explains this further.In any case, what you need to do is invoke the
getOperand(n)method on the instruction to get its nth operand – for example,getOperand(0)in your example will return the value named%add7. You can then check whether that value is indeed an instruction (as opposed to, say, a function argument) by checking its type (isa<Instruction>).To emphasize – calling the
getOperandmethod will give you the actual place in which the operand is defined, nothing else is required.