LLVM从Value获取常量整数* [英] LLVM get constant integer back from Value*

查看:1649
本文介绍了LLVM从Value获取常量整数*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个整数常量创建一个llvm :: Value *,如下所示:

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

现在我想要检索编译时常量值;

  int constIntValue = constValue->? 

LLVM程序员手册似乎暗示cast<>将接受一个指针,当使用类型(而不是类型加指针)模板参数,但是我很确定的失败从2.8:

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

//构建错误:
//错误:没有匹配的函数调用'cast(llvm :: Value *&)'



这里正确的方法是什么?

解决方案

给定 llvm :: Value * foo ,你知道 foo 实际上是一个 ConstantInt ,我相信惯用的LLVM代码方法是使用 dyn_cast 如下:

  if(llvm :: ConstantInt * CI = dyn_cast< llvm :: ConstantInt>(foo)){
// foo确实是一个ConstantInt,我们可以使用CI here
}
else {
// foo实际上不是ConstantInt
}


$ b b

如果您确定 foo ConstantInt 如果不是,则可以使用 cast 而不是 dyn_cast 。 p>




PS请注意, cast dyn_cast 是LLVM自己的RTTI实现的一部分。 dyn_cast 的行为与标准C ++ dynamic_cast 有些类似,尽管在实现和性能方面存在差异href =http://llvm.org/docs/ProgrammersManual.html#isa>在此处阅读)。


I create a llvm::Value* from a integer constant like this:

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

now i want to retrieve the compile-time constant value back;

int constIntValue = constValue->???

The examples shown in LLVM Programmer manual seem to imply that cast<> will accept a pointer when using the type (rather than the type plus pointer) template parameter, however i'm pretty sure thats failing as from 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*&)’

What would be the correct approach here?

解决方案

Given llvm::Value* foo and you know that foo is actually a ConstantInt, I believe that the idiomatic LLVM code approach is to use dyn_cast as follows:

if (llvm::ConstantInt* CI = dyn_cast<llvm::ConstantInt>(foo)) {
  // foo indeed is a ConstantInt, we can use CI here
}
else {
  // foo was not actually a ConstantInt
}

If you're absolutely sure that foo is a ConstantInt and are ready to be hit with an assertion failure if it isn't, you can use cast instead of dyn_cast.


P.S. Do note that cast and dyn_cast are part of LLVM's own implementation of RTTI. dyn_cast acts somewhat similarly to the standard C++ dynamic_cast, though there are differences in implementation and performance (as can be read here).

这篇关于LLVM从Value获取常量整数*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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