LLVM从Value获取常量整数* [英] LLVM get constant integer back from 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 thatfoo
is actually aConstantInt
, I believe that the idiomatic LLVM code approach is to usedyn_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 aConstantInt
and are ready to be hit with an assertion failure if it isn't, you can usecast
instead ofdyn_cast
.
P.S. Do note that
cast
anddyn_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屋!