Need to clarify LLVM C methods

I am writing code using C and LLVM. I know that LLVMGetOperand returns LLVMValueRef , but I was wondering what exactly is happening because it seems that LLVMValueRef can be a lot of different things. Does LLVMGetOperand instruction that creates an operand?

What I ultimately need to do is get the operands of the instructions and get the instruction that 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 these values ​​get the instructions %3 = ADD %1 %2 and %5 = ADD ... I know that I need to use LLVMGetOperand() , but I don’t know what it returns, and how to use it to get the instructions I need. It would be nice if he just returned the instructions.

I also looked at LLVMGetFirstUse() . Does LLVMGetFirstUse() statement and then return the first use of the value that the command creates?

I know about llvm.org and used it, I just need more clarification than what is given on this website.

+4
source share
1 answer

Since C bundles mirror the structure of C ++ code, it is usually a good idea to get to know how everything is done in C ++. First you need to start the LLVM Programmer Manual .

ValueRef You specify only Value* in the C code. Here, as described in the manual :

The Value class is the most important class in the LLVM source database. It represents a typed value that can be used (among other things) as an operand to an instruction. There are many different types of Values , for example Constants , Arguments . Even Instructions and Functions Values .

Now, the IRBuilder class is usually used to build base blocks. In C code, this corresponds to the LLVMBuild* family of functions. For example, here is the function signature for creating the sub statement:

 LLVMValueRef LLVMBuildSub(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS, const char *Name); 

The first parameter is a reference to the IRBuilder object, the second is the first operand, the third is the second operand, and the last is an optional name for the resulting value. Thus, your example will look something like this (not verified):

 /* I'm assuming that you have already created a basic block bb. */ LLVMBuilderRef builder = LLVMCreateBuilder(); LLVMPositionBuilderAtEnd(builder, bb); LLVMValueRef lhs = LLVMBuildAdd(builder, LLVMConstInt(LLVMInt32Type(), 1, 0), LLVMConstInt(LLVMInt32Type(), 2, 0), NULL); LLVMValueRef rhs = LLVMBuildAdd(builder, LLVMConstInt(LLVMInt32Type(), 3, 0), LLVMConstInt(LLVMInt32Type(), 4, 0), NULL); LLVMBuildSub(build, lhs, rhs, NULL); 

Regarding LLVMGetFirstUse : given Value , you can LLVMGetFirstUse over all the places where it was used. LLVMGetFirstUse gives you an iterator ( LLVMUseRef ) pointing to the use list header, which you can increase ( LLVMGetNextUse ) and dereference ( LLVMGetUser ). See llvm/Use.h .

0
source

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


All Articles