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

weixuegong weixuegong at gmail.com
Fri Sep 6 00:03:42 PDT 2013

Thank you, Chuck! It works.

And, please forgive my greed : ), another question:
I found that: In IR of llvm which generated by my testing code, there is 
a Inst said " getelementptr inbounds [5 x i32]* %idxarr, i32 0, i64 3".
The idxarr is my array name, and I think Inst means getting the ptr of 
the 4th (3+1) element of array "idxarr", am I right?
So the question is: how can I add this Inst in IR (In other words, is 
there an API method in IRBuilder or other class to generate 
"getelementptr"?). I searched for a while, but I found nothing.
I will appreciate if you could give me some suggestion on this.

Thank you.

>> 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