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?
Eli's answer is great, but it's missing the final part, which is getting the integer back. The full picture should look like this:
Of course, you can also change it to
<= 64
ifconstIntValue
is a 64-bit integer, etc.And as Eli wrote, if you are confident the value is indeed of type
ConstInt
, you can usecast<ConstantInt>
instead ofdyn_cast
.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 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).