如何使用 AllocaInst 创建 LLVM 数组类型
How to create LLVM Array type using AllocaInst?
我想在堆栈上创建LLVM ArrayType,所以我想使用AllocaInst (Type *Ty, Value *ArraySize=nullptr, const Twine &Name="", Instruction *InsertBefore=nullptr)
。问题是我不明白这个界面。我猜Ty
会像ArrayType::get(I.getType(), 4)
一样,但我应该为ArraySize
付出什么.此外,它需要Value*
,所以它让我很困惑。
我误解了 llvm 分配,要么我需要提供一个 llvm 常量作为数组大小的值。如果我必须给出常量,是不是有点多余,因为ArrayType
包含 numElement 作为信息。
作为示例代码行,我尝试的方式:
AllocaInst* arr_alloc = new AllocaInst(ArrayType::get(I.getType(), num)
/*, What is this parameter for?*/,
"",
funcEntry.getFirstInsertionPt());
I 是数组元素的类型,例如:
Type* I = IntegerType::getInt32Ty(module->getContext());
然后,您可以创建num
元素的数组类型:
ArrayType* arrayType = ArrayType::get(I, num);
此类型可以在 AllocInstr 中使用,如下所示:
AllocaInst* arr_alloc = new AllocaInst(
arrayType, "myarray" , funcEntry
// ~~~~~~~~~
// -> custom variable name in the LLVM IR which can be omitted,
// LLVM will create a random name then such as %2.
);
此示例将生成以下 LLVM IR 指令:
%myarray = alloca [10 x i32]
编辑此外,似乎允许您将可变数组大小传递给 AllocInstr,如下所示:
Type* I = IntegerType::getInt32Ty(module->getContext());
auto num = 10;
auto funcEntry = label_entry;
ArrayType* arrayType = ArrayType::get(I, num);
AllocaInst* variable = new AllocaInst(
I, "array_size", funcEntry
);
new StoreInst(ConstantInt::get(I, APInt(32, 10)), variable, funcEntry);
auto load = new LoadInst(variable, "loader", funcEntry);
AllocaInst* arr_alloc = new AllocaInst(
I, load, "my_array", funcEntry
);
相关文章:
- 声明C++具有动态大小的数组类型在 Linux 中工作正常,但不能在 Windows 中工作
- 数组类型 int[n][n] 不可赋值,因为表达式必须具有常量值
- 取消引用结束指针到数组类型的一个
- 结构化绑定初始值设定项表单 { 赋值表达式 } 对于 clang 上的数组类型失败
- 如何在数组类型的模板代码中计算std::size_t
- LLVM 无法将数组类型强制转换为常量数组
- 推荐的方法在不初始化值的情况下使数组类型为 std::unique_ptr?
- 特征:从数组类型中获取标量类型是否记录?
- T D[N] 总是声明数组类型的对象吗?
- 如何在模板中推断数组类型?
- 尝试使用 Array 类的模板构造整数数组时,错误:为什么数组类型"int [5]"不可分配,
- 如何将二维数组类型字符(字符串)作为函数参数传递?
- 如何为所有非数组类型专门化模板?
- 数组类型 (C++) 的初始化
- 我预测char数组类型值只能投射指针类型
- 将数组类型更改为 Int16 会节省存储空间吗?
- 采用通用数组类型的函数
- 数组类型 'int [8]'不可分配
- 数组类型 'float [size]'不可分配
- C++。候选模板被忽略 - 无法将数组类型与自身匹配