在以下位置获取LLVM getelementptr的操作数名称 [英] Get operand name of a LLVM getelementptr in

查看:653
本文介绍了在以下位置获取LLVM getelementptr的操作数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取getelementptr指令所引用的数组的名称.当像这样在实际的c代码中使用中间变量将数组编入索引时,这似乎起作用了

I am trying to get the name of an array that a getelementptr instruction is referencing. This seems to work when the array is indexed into using an intermediate variable in the actual c code like so

int a = 0;
i[a] = 3; 

在这种情况下,我得到以下位码

In this case, i get the following bitcode

%arrayidx = getelementptr inbounds [2 x i32], [2 x i32]* @i, i64 0, i64 %idxprom
store i32 3, i32* %arrayidx, align 4

在这种情况下,我可以遍历getelementptr指令的操作数 并通过第一个操作数上的getName()方法找到array(i)的名称.

In this case I can iterate over the operands of the getelementptr instruction and find the name of the array(i) through the getName() method on the first operand.

但是,如果在源代码中,则数组直接按原样索引,

However, if in the source, the array is index directly as so,

i[0] = 3;

然后,生成的位代码如下

Then, the bitcode generated is as follows

store i32 3, i32* getelementptr inbounds ([2 x i32], [2 x i32]* @i, i64 0, i64 0), align 4

在这里,我不确定如何从位代码获取数组名称(i). 存储指令的第二个操作数的类型设置为PointerTy.第二个操作数的包含类型为int.两者都符合预期,因为操作数是i32 *.但是,在这种情况下,我不确定如何获取getelementptr指令的句柄以迭代其操作数.

Here I am not sure of how to get the array name(i) from the bitcode. The type of the second operand of the store instruction is set to be PointerTy. And the contained type of the second operand is an int. Both of this is as expected since the operand is a i32*. But, I am not sure how to get a handle to the getelementptr instruction to iterate over its operands in this case.

我应该提到数组i是全局的

I should mention that the array i is a global

推荐答案

在第一个示例中,store指令的第二个操作数是getelementptr 指令,但是在第二个示例中,这是一个getelementptr 常量表达式,因为指针和索引都是常量.

In the first example, the second operand of the store instruction is a getelementptr instruction, but in the second example, it's a getelementptr constant expression, because both the pointer and the indices are constants.

因此,在第一种情况下,如果获得StoreInst的指针操作数,则将获得 GetElementPtrInst .在第二种情况下,您将得到一个 ConstantExpr ,其getOpcode()方法返回Instruction::GetElementPtr.

Thus, in the first case, if you get the pointer operand of the StoreInst, you'll get a GetElementPtrInst. In the second case, you'll get a ConstantExpr whose getOpcode() method returns Instruction::GetElementPtr.

您可以使用 GEPOperator 在代码中统一处理这两种情况.您可以使用dyn_cast<GEPOperator>(I),它将对指令和constantexprs做正确的事情.

You can handle both cases uniformly in your code by using GEPOperator. You can use dyn_cast<GEPOperator>(I) and it'll do the right thing for both instructions and constantexprs.

(注意-Operator不是LLVM IR概念,它只是一种C ++抽象,可以在这种情况下帮助您处理指令或常量表达式(可能在强制转换,GEP,或算术运算),但您不必担心区别.)

(Note -- Operator is not an LLVM IR concept -- it's just a C++ abstraction to help in cases like this where you might be dealing with an instruction or a constant expression (which might happen with casts, GEPs, or arithmetic operations) but you don't care about the distinction.)

这篇关于在以下位置获取LLVM getelementptr的操作数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆