<div class="gmail_quote">On 13 July 2010 00:25, Duncan Sands <span dir="ltr"><<a href="mailto:baldrick@free.fr">baldrick@free.fr</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">Hi Nick,<br>
<br>
>>> The attribute you're looking for, "delete if result is unused" doesn't<br>
>>> exist in LLVM. I've considered it in the past, but the truth is that<br>
>>> most of the time I want to eliminate dead malloc's, they *do* have a<br>
>>> use: the matching free. At some point I expect I'm going to teach LLVM<br>
>>> to remove dead malloc+free / new+delete / new[]+delete[] pairings, but I<br>
>>> haven't gotten around to it yet. Teaching it to kill an allocation with<br>
>>> no uses at all will be dead simple too.<br>
>><br>
>> LLVM already removes malloc+(perhaps compare with null)+free.<br>
><br>
> What?<br>
><br>
> declare noalias i8* @malloc() nounwind<br>
> declare void @free(i8* nocapture) nounwind<br>
> define i8 @test() {<br>
> %A = call i8* @malloc()<br>
<br>
</div>^ malloc with wrong signature<br></blockquote><div><br></div><div>Duh, thanks!</div><div><br></div><div>Then at some point, I should generalize this to handle C++ allocators too. :)</div><div><br></div><div>Nick</div>

<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">> call void @free(i8* %A)<br>
> ret i8 undef<br>
> }<br>
><br>
> doesn't optimize at all. While we're at it, we also don't get:<br>
><br>
> declare noalias i8* @malloc() nounwind<br>
> define i8 @test() {<br>
> %A = call i8* @malloc()<br>
> %B = load i8* %A<br>
> ret i8 %B<br>
> }<br>
><br>
> either, though that's fixable by inserting llvm.lifetime.start right<br>
> after the malloc.<br>
<br>
</div>Take a look at:<br>
<br>
Instruction *InstCombiner::visitMalloc(Instruction &MI) {<br>
   // If we have a malloc call which is only used in any amount of comparisons<br>
   // to null and free calls, delete the calls and replace the comparisons with<br>
   // true or false as appropriate.<br>
   if (IsOnlyNullComparedAndFreed(MI)) {<br>
...<br>
<br>
Ciao,<br>
<font color="#888888"><br>
Duncan.<br>
</font><div><div></div><div class="h5">_______________________________________________<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>
</div></div></blockquote></div><br>