c++ - LLVM 从 Value* 返回常量整数

标签 c++ llvm

我从这样的整数常量创建一个 llvm::Value*:

llvm::Value* constValue = llvm::ConstantInt::get( llvmContext , llvm::APInt( node->someInt() ));

现在我想找回编译时常量值;

int constIntValue = constValue->???

LLVM Programmer manual 中显示的示例似乎暗示 cast<> 在使用类型(而不是类型加指针)模板参数时将接受一个指针,但是我很确定从 2.8 开始就失败了:

llvm::Value* foo = 0;
llvm::ConstantInt* intValue = & llvm::cast< llvm::ConstantInt , llvm::Value >(foo );

//build error:
//error: no matching function for call to ‘cast(llvm::Value*&)’

这里的正确方法是什么?

最佳答案

Eli 的答案很棒,但它缺少最后一部分,即取回整数。完整的图片应该是这样的:

if (ConstantInt* CI = dyn_cast<ConstantInt>(Val)) {
  if (CI->getBitWidth() <= 32) {
    constIntValue = CI->getSExtValue();
  }
}

当然你也可以改成<= 64如果 constIntValue是 64 位整数等。

正如 Eli 所写,如果您确信该值确实是 ConstInt 类型, 你可以使用 cast<ConstantInt>而不是 dyn_cast .

关于c++ - LLVM 从 Value* 返回常量整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5315176/

相关文章:

c++ - 在 std::map 的元素上设置数据断点

compilation - 如何为堆栈机器编写 LLVM 后端?

cmake - 使用 CMake 使用 LLVM LLD 发出单个 IR 位码文件

C++ 运算符重载奇怪的类型转换

c++ - 尝试使用指针迭代数组,但出现浮点指针错误/它无法编译

c++ - OpenGL中n面棱镜的代码

c++ - 为什么我无法从 std::stringstream 二进制流中获得完整精度( double 、 float )

llvm - 使用 opt 运行 LLVM pass

gcc - 如何从 gcc(或其他地方)获取合法 ARM 操作码列表?

c# - 如何在 C# 和 C++ 之间进行互操作