<div dir="ltr">Hello Wei,<div><br></div><div>One of the trick I use to know what and how llvm functions to use to generate desired llvm instructions, types etc is - </div><div><br></div><div>Write a C code which will produce the desired instructions and then compile using C++ backend. </div>
<div><br></div><div>eg. Write the C code</div><div><div><br></div><div>int arr[10];</div><div>main(int argc, char* argv[])<br></div><div>{</div><div>    arr[2] = argc;</div><div>    printf("%d",arr[2]);</div><div>
}</div></div><div><br></div><div>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 :)</div><div><br></div><div>Something like -</div>
<div><br></div><div><div>const_ptr_15_indices.push_back(const_int32_14);</div><div>ConstantInt* const_int64_16 = ConstantInt::get(mod->getContext(), APInt(64, StringRef("2"), 10));</div><div>const_ptr_15_indices.push_back(const_int64_16);</div>
<div>Constant* const_ptr_15 = ConstantExpr::getGetElementPtr(gvar_array_arr, &const_ptr_15_indices[0], const_ptr_15_indices.size());</div></div><div><br></div><div>Earlier it was even easier with online demo tool <a href="http://llvm.org/demo/">http://llvm.org/demo/</a>. </div>
<div><br></div><div>Hope it helps.</div><div><br></div><div>Regards,</div><div>Ankur</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Sep 6, 2013 at 4:03 PM, weixuegong <span dir="ltr"><<a href="mailto:weixuegong@gmail.com" target="_blank">weixuegong@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thank you, Chuck! It works.<br>
<br>
And, please forgive my greed : ), another question:<br>
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".<br>
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?<br>
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.<br>
I will appreciate if you could give me some suggestion on this.<br>
<br>
Thank you.<div class="HOEnZb"><div class="h5"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
From: <a href="mailto:llvmdev-bounces@cs.uiuc.edu" target="_blank">llvmdev-bounces@cs.uiuc.edu</a> [mailto:<a href="mailto:llvmdev-bounces@cs.uiuc.edu" target="_blank">llvmdev-bounces@cs.<u></u>uiuc.edu</a>]<br>
On Behalf Of weixuegong<br>
Subject: [LLVMdev] How to get the exact integer from a Value<br>
I have a Value* type named indexValue, and the type is i32.<br>
I think "indexValue" must hold a number which type is int.<br>
</blockquote>
Correct, but it might not be a constant.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
ConstantInt* CI = llvm::cast<llvm::ConstantInt>(<u></u>indexValue); //This is wrong, so is dyn_cast.<br>
uint64_t index = indexValue->getZExtValue();<br>
uint64_t size = index + 1;<br>
</blockquote>
Actually dyn_cast<> is what you want, used like this:<br>
<br>
     ConstantInt* CI = dyn_cast<ConstantInt>(<u></u>indexValue);<br>
     int64_t index = (CI == NULL) ? -1 : CI->getZExtValue();<br>
<br>
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.<br>
<br>
  - Chuck<br>
<br>
<br>
</blockquote>
<br>
______________________________<u></u>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/llvmdev</a><br>
</div></div></blockquote></div><br></div>