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

ankur deshwal a.s.deshwal at gmail.com
Fri Sep 6 00:27:23 PDT 2013

Hello Wei,

One of the trick I use to know what and how llvm functions to use to
generate desired llvm instructions, types etc is -

Write a C code which will produce the desired instructions and then compile
using C++ backend.

eg. Write the C code

int arr[10];
main(int argc, char* argv[])
    arr[2] = argc;

Compile it to llvm IR using clang, check if this generate instruction you
want. Then use llc -march=cpp and you get the code you are supposed to
write :)

Something like -

ConstantInt* const_int64_16 = ConstantInt::get(mod->getContext(), APInt(64,
StringRef("2"), 10));
Constant* const_ptr_15 = ConstantExpr::getGetElementPtr(gvar_array_arr,
&const_ptr_15_indices[0], const_ptr_15_indices.size());

Earlier it was even easier with online demo tool http://llvm.org/demo/.

Hope it helps.


On Fri, Sep 6, 2013 at 4:03 PM, weixuegong <weixuegong at gmail.com> wrote:

> 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<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
> ______________________________**_________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/**mailman/listinfo/llvmdev<http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130906/2e015a87/attachment.html>

More information about the llvm-dev mailing list