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?
我正在使用C和LLVM编写一些代码。我知道vmllgetoperand返回的是一个LLVMValueRef,但我想知道它到底是什么,因为它看起来像是很多不同的东西。LLVMGetOperand是否返回创建操作数的指令?
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.
如果我知道指令% 8 =子% 3% 5,我想操作数% 3和% 5,然后与这些价值观得到指令% % 1% 2和% 5 = 3 =添加添加....我知道我需要使用LLVMGetOperand(),但是我不知道它返回什么,以及如何使用它来获得我需要的指令。如果它只是返回指令就好了。
I also looked into LLVMGetFirstUse()
. Does LLVMGetFirstUse()
take in an instruction and then return the first use of the value the instruction produces?
我还研究了LLVMGetFirstUse()。LLVMGetFirstUse()是否接受一条指令,然后返回该指令所产生的值的第一次使用?
I know about llvm.org and have been using it, I just need more clarification than what is given on that website.
我知道llvm.org并且一直在使用它,我只是需要更多的澄清,而不是在那个网站上。
1 个解决方案
#1
0
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.
由于C绑定反映了c++代码的结构,所以一般来说,熟悉c++中的操作是一个好主意。一个开始的地方是LLVM程序员手册。
The ValueRef
you mention is just Value*
in the C code. Here's how it is described in the manual:
您提到的ValueRef只是C代码中的Value*。以下是手册中的描述:
The
Value
class is the most important class in the LLVM Source base. It represents a typed value that may be used (among other things) as an operand to an instruction. There are many different types ofValues
, such asConstants
,Arguments
. EvenInstructions
andFunctions
areValues
.值类是LLVM源库中最重要的类。它表示一个类型化值,该值可以用作指令的操作数。有许多不同类型的值,例如常量、参数。甚至指令和函数都是值。
Now, for assembling basic blocks you usually use the IRBuilder
class. In the C code this corresponds to the LLVMBuild*
family of functions. For example, here's the signature of the function for creating the sub
instruction:
现在,对于组装基本块,通常使用IRBuilder类。在C代码中,它对应于LLVMBuild*系列函数。例如,下面是创建子指令的函数签名:
LLVMValueRef LLVMBuildSub(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
const char *Name);
The first parameter is the reference to the IRBuilder
object, 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):
第一个参数是对IRBuilder对象的引用,第二个参数是第一个操作数,第三个是第二个操作数,最后一个是结果值的可选名称。你的例子应该是这样的(没有经过测试)
/* 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);
With regards to LLVMGetFirstUse
: given a Value
, you can iterate over all places where it's used. LLVMGetFirstUse
gives you an iterator (LLVMUseRef
) pointing to the head of the use-list that you can increment (LLVMGetNextUse
) and dereference (LLVMGetUser
). See llvm/Use.h
for more information.
对于LLVMGetFirstUse:给定一个值,您可以遍历它使用的所有位置。LLVMGetFirstUse为您提供了一个迭代器(LLVMUseRef),指向您可以增加的use-list的头部(LLVMGetNextUse)和dereference (LLVMGetUser)。看到llvm /使用。h的更多信息。
#1
0
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.
由于C绑定反映了c++代码的结构,所以一般来说,熟悉c++中的操作是一个好主意。一个开始的地方是LLVM程序员手册。
The ValueRef
you mention is just Value*
in the C code. Here's how it is described in the manual:
您提到的ValueRef只是C代码中的Value*。以下是手册中的描述:
The
Value
class is the most important class in the LLVM Source base. It represents a typed value that may be used (among other things) as an operand to an instruction. There are many different types ofValues
, such asConstants
,Arguments
. EvenInstructions
andFunctions
areValues
.值类是LLVM源库中最重要的类。它表示一个类型化值,该值可以用作指令的操作数。有许多不同类型的值,例如常量、参数。甚至指令和函数都是值。
Now, for assembling basic blocks you usually use the IRBuilder
class. In the C code this corresponds to the LLVMBuild*
family of functions. For example, here's the signature of the function for creating the sub
instruction:
现在,对于组装基本块,通常使用IRBuilder类。在C代码中,它对应于LLVMBuild*系列函数。例如,下面是创建子指令的函数签名:
LLVMValueRef LLVMBuildSub(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
const char *Name);
The first parameter is the reference to the IRBuilder
object, 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):
第一个参数是对IRBuilder对象的引用,第二个参数是第一个操作数,第三个是第二个操作数,最后一个是结果值的可选名称。你的例子应该是这样的(没有经过测试)
/* 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);
With regards to LLVMGetFirstUse
: given a Value
, you can iterate over all places where it's used. LLVMGetFirstUse
gives you an iterator (LLVMUseRef
) pointing to the head of the use-list that you can increment (LLVMGetNextUse
) and dereference (LLVMGetUser
). See llvm/Use.h
for more information.
对于LLVMGetFirstUse:给定一个值,您可以遍历它使用的所有位置。LLVMGetFirstUse为您提供了一个迭代器(LLVMUseRef),指向您可以增加的use-list的头部(LLVMGetNextUse)和dereference (LLVMGetUser)。看到llvm /使用。h的更多信息。