[LLVMdev] Generating GetElementPtr inlined in a function argument list programmatically

Matt Arsenault arsenm2 at gmail.com
Mon Aug 19 14:37:20 PDT 2013


On Aug 19, 2013, at 14:26 , chentommy <baiypwup at hotmail.com> wrote:

> Hello LLVMDev List,
> 
> 
> It's my first time sending a message to the List - I have been working on a tool for my research project using LLVM. Thanks for your awesome work!
> 
> I have come across some bytecode like the following with an GetElementPtr instruction in brackets:
> 
> Bytecode:
> %3 = call i32 @_Z4funcPKc(i8* getelementptr inbounds ([5 x i8]* @.str2, i32 0, i32 0))
> 
> C++ code:
> func("bleh");
> 
> However when I am generating bytecode programmatically, I can not have the GetElementPtr value "inlined" in the function argument list:
> 
> Bytecode:
> %bbname = getelementptr inbounds [29 x i8]* @bbname10, i32 0, i32 0
> call void @incrementFaultSiteCount(i8* %bbname, i32 2)
> 
> C++ code: (What I mean to generate)
> incrementFaultSiteCount("BBName", 2);
> 
> My question is:
> - What is this phenomenon called, is it called "inlined/expanded function argument"?
This is a constant expression cast. It will only happen for a constant arguments, in this case the address of global @bbname. You should get these if you run instcombine on it.

http://llvm.org/docs/LangRef.html#constant-expressions


> - It seems the "inlined" GetElementPtr instruction is just more concise and saves the need for a name for a register holding the value of the pointer. Is this correct?
> - I'm now generating the aforementioned instructions (non-inlined) with the following code snippet with LLVM 3.2. Is there a way to generate "inlined" GetElementPtr instructions when calling a function just like the first example programmatically?
> 
> -------------------------------------------------------------------
> BasicBlock* bb = (omitted)
> IRBuilder<> irb(bb);
> Value* global_str_ptr = irb.CreateGlobalString(bbn, "bbname");
> std::vector<Value*> indices;
> indices.push_back(ConstantInt::get(IntegerType::getInt32Ty(getGlobalContext()), 0));
> indices.push_back(ConstantInt::get(IntegerType::getInt32Ty(getGlobalContext()), 0));
> GetElementPtrInst* gep_str = GetElementPtrInst::CreateInBounds(global_str_ptr,
> 	indices,
> 	"bbname", bb->getFirstNonPHI());
> args.push_back(gep_str);
> args.push_back(ConstantInt::get(IntegerType::getInt32Ty(getGlobalContext()),
> 	size));
> CallInst* inc_call = CallInst::Create(func_incrementFaultSitesEnumerated, args,
> 	"", first_inst);
> -------------------------------------------------------------------
> 
> Thanks in advance!
> 
> Tommy Chen
> 08-19-2013
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130819/28e86216/attachment.html>


More information about the llvm-dev mailing list