获取指向以前为CreateLoad函数分配的llvm :: Value的指针 [英] Get pointer to llvm::Value previously allocated for CreateLoad function

查看:478
本文介绍了获取指向以前为CreateLoad函数分配的llvm :: Value的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是llvm的新手,我正在编写一个小型的llvm IR Builder. 我使用IRBuilder和所有这些Create *函数来生成我的IR. 我想做的是创建一条 load 指令,该指令创建一个新的SSA局部变量,其值先前已分配的 llvm :: Value .

I'm new to llvm and I'm writing a small llvm IR Builder. I use the IRBuilder and all these Create* functions to generate my IR. What I'm trying to do is to create a load instruction which create a new SSA local variable with value of a previously allocated llvm::Value.

我希望拥有的东西:

%2 = load i32* %1

具有%2 个加载指令的结果和%1 我先前分配的值(CreateAlloca)

With %2 results of load instruction and %1 my previously allocated Value (CreateAlloca)

这是我尝试过的:

// Get Ptr from Val
Value* ptr = ConstantExpr::getIntToPtr((Constant*)loc[n],PointerType::getUnqual(builder->getInt32Ty()));

// Générate load instruction with the new Ptr
builder->CreateLoad(ptr);

这就是我所拥有的:

%2 = load i32* null

loc 是一个数组,其中包含我所有的llvm :: Value *

loc is an array which contains all my llvm::Value*

您能告诉我我做错了什么吗?还是如果我的方法不好? 谢谢.

Can you please tell me what I'm doing wrong ? Or maybe if I'm on a bad way ? Thanks.

推荐答案

ConstantExpr::getIntToPtr()创建一个恒定表达.因此,实际上,您要生成的内容等效于此IR:

ConstantExpr::getIntToPtr() creates a constant expression. So in effect, what you're trying to generate is equivalent to this IR:

%2 = load i32* inttoptr (i32 %1 to i32*)

但这是非法的,因为如其名称所暗示的常量表达式仅支持常量,并且%1不是常数. ConstantExpr::getIntToPtr()需要一个Constant作为验证它的第一个参数,但是您为它传递了一个非常数值,该值被强制转换为常数.

But this is illegal since a constant expression, as hinted by its name, only supports constants, and %1 isn't a constant. ConstantExpr::getIntToPtr() requires a Constant as a first argument to verify it, but you passed it a non-constant value which was forcefully cast to a constant.

将非恒定整数转换为指针的正确方法是使用IRBuilder::createIntToPtr.但是,由于您说以前的值(loc[n])是通过alloca创建的,因此它已经是一个指针,并且您无需执行任何转换:只需执行builder->CreateLoad(loc[n]).

The correct way to convert a non-constant integer to a pointer is with IRBuilder::createIntToPtr. However, since you say the previous value (loc[n]) was created via an alloca then it's already a pointer, and you don't need to perform any conversion: just do builder->CreateLoad(loc[n]).

顺便说一句,在LLVM中将值强制转换为常量的正确方法不是通过c样式强制转换,而是通过

By the way, the proper way to cast a Value to a Constant in LLVM is not via a c-style cast but via cast<>, like so: cast<Constant>(loc[n]).

这篇关于获取指向以前为CreateLoad函数分配的llvm :: Value的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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