Hi,all I have a Value* type named indexValue, and the type is i32. I think "indexValue" must hold a number which type is int. Now I want to use the exact number which pointed by indexValue , so I do like this: ConstantInt* CI = llvm::cast<llvm::ConstantInt>(indexValue); //This is wrong, so is dyn_cast. uint64_t index = indexValue->getZExtValue(); uint64_t size = index + 1; I don't know if it is the right way. So, could anybody tell the way to get the integer content from a Value* which type is i32? I'll be very very grateful if there is any answer. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130906/84ad0bf0/attachment.html>
Caldarale, Charles R
2013-Sep-06 03:25 UTC
[LLVMdev] How to get the exact integer from a Value
> 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
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 > >
Apparently Analagous Threads
- [LLVMdev] How to get the exact integer from a Value
- [LLVMdev] How to get the exact integer from a Value
- [LLVMdev] Confuse on ptrtoint and load
- [LLVMdev] Is GEP safe if not know the size of an array?
- [LLVMdev] Question on bit layout of array after bitcasting in llvm