[LLVMdev] How to get the exact integer from a Value

Caldarale, Charles R Chuck.Caldarale at unisys.com
Thu Sep 5 20:25:21 PDT 2013

> From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu]
> On Behalf Of weixuegong
> Subject: [LLVMdev] How to get the exact integer from a Value

> I have a Value* type named indexValue, and the type is i32.
> I think "indexValue" must hold a number which type is int.

Correct, but it might not be a constant.

> ConstantInt* CI = llvm::cast<llvm::ConstantInt>(indexValue); //This is wrong, so is dyn_cast. 
> uint64_t index = indexValue->getZExtValue();
> uint64_t size = index + 1;

Actually dyn_cast<> is what you want, used like this:

    ConstantInt* CI = dyn_cast<ConstantInt>(indexValue);
    int64_t index = (CI == NULL) ? -1 : CI->getZExtValue();

Note that you must use the result of the cast, not the original Value* variable.  Also, you must deal with the fact that the result of the cast might be NULL.

 - Chuck

More information about the llvm-dev mailing list