chentommy
2013-Aug-19 21:26 UTC
[LLVMdev] Generating GetElementPtr inlined in a function argument list programmatically
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 0call 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"?- 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 Chen08-19-2013 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130820/ea3df464/attachment.html>
Matt Arsenault
2013-Aug-19 21:37 UTC
[LLVMdev] Generating GetElementPtr inlined in a function argument list programmatically
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>
Matt Arsenault
2013-Aug-19 21:38 UTC
[LLVMdev] Generating GetElementPtr inlined in a function argument list programmatically
On Aug 19, 2013, at 14:37 , Matt Arsenault <arsenm2 at gmail.com> wrote:> > This is a constant expression cast.Err not a cast, just a constant expression getelementptr
Matt Arsenault
2013-Aug-19 21:42 UTC
[LLVMdev] Generating GetElementPtr inlined in a function argument list programmatically
On Aug 19, 2013, at 14:37 , Matt Arsenault <arsenm2 at gmail.com> wrote:> > 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 >I forgot to mention you can also create ConstantExprs directly if you want -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130819/a749a46d/attachment.html>
Tim Northover
2013-Aug-20 09:25 UTC
[LLVMdev] Generating GetElementPtr inlined in a function argument list programmatically
Hi Tommy,> %3 = call i32 @_Z4funcPKc(i8* getelementptr inbounds ([5 x i8]* @.str2, i32 > 0, i32 0))> - What is this phenomenon called, is it called "inlined/expanded function > argument"?This is the syntax used when the getelementptr is a GetElementPtrConstantExpr rather than an Instruction, and that's generally true for the inlined forms: if you can make an expression out of Constants in some way then it'll be "inlined".> - 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?Yep, the function you want is GetElementPtrConstantExpr::Create. Incidentally, in these cases the "cpp" backend can be very useful. If you can write LLVM IR to do what you want it will give you back some C++ code that emits the IR. For example: $ cat simple.ll @var = global [5 x i32] zeroinitializer define i32* @foo() { ret i32* getelementptr(i32* @var, i32 0, i32 1) } $ llc simple.ll -o - -march=cpp [...] GlobalVariable* gvar_array_var = new GlobalVariable(/*Module=*/*mod, [...]); [...] std::vector<Constant*> const_ptr_5_indices; ConstantInt* const_int32_6 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("0"), 10)); const_ptr_5_indices.push_back(const_int32_6); ConstantInt* const_int32_7 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("1"), 10)); const_ptr_5_indices.push_back(const_int32_7); Constant* const_ptr_5 ConstantExpr::getGetElementPtr(gvar_array_var, const_ptr_5_indices); [...] ReturnInst::Create(mod->getContext(), const_ptr_5, label_8); Obviously the code is auto-generated and usually not the best, but it usually gives you enough information about what you need to do. Cheers. Tim.
Maybe Matching Threads
- [LLVMdev] Generating GetElementPtr inlined in a function argument list programmatically
- [LLVMdev] Crash in CreateShl() method
- [LLVMdev] support for attach embedded metadata to function/argument/basicblock proposal
- [LLVMdev] use function address as global variable initializer
- [RFC] mir-canon: A new tool for canonicalizing MIR for cleaner diffing.