I am writing some code using C and LLVM. I know LLVMGetOperand returns a LLVMValueRef but I was wondering what exactly it is because it seems like LLVMValueRef can be a number of different things. Does LLVMGetOperand return the instruction that creates the operand?
What I ultimately need to do is get an instructions operands and get the instruction who creates the value. As in
%3 = ADD %1 %2
...
%5 = ADD ...
...
%8 = SUB %3 %5
If I know the instruction %8 = SUB %3 %5, I want to get the operands %3 and %5 and then with those values get the instructions %3 = ADD %1 %2 and %5 = ADD .... I know I need to use LLVMGetOperand() but I dont know what it returns and how to use it to get the instruction I need. Would be nice if it just returned the instruction.
I also looked into LLVMGetFirstUse(). Does LLVMGetFirstUse() take in an instruction and then return the first use of the value the instruction produces?
I know about llvm.org and have been using it, I just need more clarification than what is given on that website.
Since the C bindings mirror the structure of the C++ code, it is in general a good idea to acquaint yourself with how the things are done in C++. One place to start is the LLVM Programmer’s Manual.
The
ValueRefyou mention is justValue*in the C code. Here’s how it is described in the manual:Now, for assembling basic blocks you usually use the
IRBuilderclass. In the C code this corresponds to theLLVMBuild*family of functions. For example, here’s the signature of the function for creating thesubinstruction:The first parameter is the reference to the
IRBuilderobject, the second is the first operand, the third is the second operand, and the last is the optional name for the resulting value. So your example would look something like this (not tested):With regards to
LLVMGetFirstUse: given aValue, you can iterate over all places where it’s used.LLVMGetFirstUsegives you an iterator (LLVMUseRef) pointing to the head of the use-list that you can increment (LLVMGetNextUse) and dereference (LLVMGetUser). Seellvm/Use.hfor more information.