<div dir="ltr"><div>You can use IRBuilder to create calls to any functions. You just need a reference to the Function*.</div><div><br></div><div>builder.CreateCall(module->getFunction("malloc"), <span style="font-size:12.8000001907349px">AllocSize</span>);<br></div><div><br></div><div>or </div><div><br></div><div><div>builder.CreateCall(module->getOrInsertFunction("malloc", [todo], ...), <span style="font-size:12.8000001907349px">AllocSize</span>);<br></div></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 27, 2015 at 4:49 PM, Dingbao Xie <span dir="ltr"><<a href="mailto:xiedingbao@gmail.com" target="_blank">xiedingbao@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks for your reply.<div>I'm running the program in a 64 bit machine.</div><div>I think I get the correct result:</div><div><div> %malloccall = tail call i8* @malloc(i64 ptrtoint (i32* getelementptr (i32* null, i32 1) to i64))</div><div> %0 = bitcast i8* %malloccall to i32*</div></div><div><br></div><div>Another problem is that I like using IRbuilder to insert instructions, but IRbuilder has no method to create a call malloc .</div><div> AllocaInst* arg_alloc = builder.CreateAlloca(tp); //allocate memory to the pointer</div><div><br></div><div>Is there any way to insert the malloc instruction after the arg_alloc instruction?</div><div>The only way that I can think of is not using IRbuilder.</div><div><br></div></div><div class="gmail_extra"><div><div class="h5"><br><div class="gmail_quote">On Mon, Jan 26, 2015 at 8:13 PM, Tim Northover <span dir="ltr"><<a href="mailto:t.p.northover@gmail.com" target="_blank">t.p.northover@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<span><br>
On 26 January 2015 at 17:50, Dingbao Xie <<a href="mailto:xiedingbao@gmail.com" target="_blank">xiedingbao@gmail.com</a>> wrote:<br>
> Is the second argument type of p and the third one type of the element<br>
> that p points to?<br>
<br>
</span>Looking at <a href="http://llvm.org/docs/doxygen/html/classllvm_1_1CallInst.html" target="_blank">http://llvm.org/docs/doxygen/html/classllvm_1_1CallInst.html</a>:<br>
<br>
The second argument is an integer on your machine that can hold a<br>
pointer. Most likely i64 on a 64-bit CPU and i32 on a 32-bit CPU. It<br>
corresponds to the type of argument malloc expects for its size.<br>
You've passed i32 here, but it looks like your malloc has been<br>
previously declared to take an i64 so there's a good chance that's<br>
wrong.<br>
<br>
The third argument is the type being allocated. It's not used for size<br>
calculations (oddly, in my view), but just to cast the Malloc result<br>
back to something your program is expecting. So in C terms, if you<br>
were allocating space for "struct Foo", that's what you'd pass here.<br>
<span><br>
> The size of an integer variable is 4, but if I pass 4 as the last argument<br>
> to CallInst::CreateMalloc, then an assertion failure will occur.<br>
<br>
</span>I'm not *quite* sure what you're doing here, but it sounds like you<br>
may have passed an integer constant 4 to a function expecting a<br>
pointer to a Value. That's going to interpret "4" as a pointer, which<br>
is almost guaranteed to go very badly indeed. What you might be able<br>
to do is pass "ConstantInt::get(ITy, 4)" (though your getSizeOf looks<br>
like a better idea all round).<br>
<br>
Putting it all together, I'd expect the output (assuming tpp == i32,<br>
as seems to be the case here) to be:<br>
<br>
%malloccall = call i8* @malloc(i64 ptrtoint(i32*<br>
getelementptr(i32* null, i32 1) to i64)<br>
%res = bitcast i8* %malloccall to i32*<br>
<br>
(If you don't actually use the result, later optimisations may remove<br>
that final bitcast and even convert the malloc to a "tail call").<br>
<br>
Cheers.<br>
<span><font color="#888888"><br>
Tim.<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div></div></div><span class="HOEnZb"><font color="#888888">-- <br><div><div dir="ltr"><div>Dingbao Xie<br></div></div></div>
</font></span></div>
<br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">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/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br></div>