Reid Spencer wrote:> On Wed, 2007-02-14 at 18:54 +0100, Nicola Lugato wrote: > >>I've made some other test and it looks like it don't remove even >>simple malloc/free couple. Maybe there's something wrong in the way i >>use the opt command. > > No, there's not. LLVM doesn't provide the transform you want. As Chris > mentioned, if you open a Bugzilla report (http://llvm.org/bugs) and ask > for the feature we are more likely to get it done than if you don't.That's surprising to me. I thought there was a pass that converts malloc's that trivially dominate all free's and whose pointer doesn't escape would be lowered to alloca's -- but I looked and couldn't find one. Why isn't there one? Because it wouldn't be profitable? Or because the alias analysis is too complex? Or just because nobody's gotten to it yet? I would've thought that this pattern would occur rather often. I know I've written code that strdup's a value, reads/writes it, then frees it. After inlining the strdup, changing the malloc/free to alloca should be easy. That said, you'd have to be careful to ensure that the malloc-free pair isn't inside of a loop before converting it to alloca. Regardless, I'd be interested in implementing this in a pass if noone else does. Nick Lewycky
Robert L. Bocchino Jr.
2007-Feb-17 05:50 UTC
[LLVMdev] Unused malloc/free don't get optimized
Hi,>> That's surprising to me. I thought there was a pass that converts >> malloc's that trivially dominate all free's and whose pointer doesn't >> escape would be lowered to alloca's -- but I looked and couldn't >> find one.I implemented a pass like this a while back. It's fairly sophisticated and relies on DSA to generate a call graph and do some other things. At one point I gave the pass to the PyPy folks; they reported that it worked except for a case that DSA couldn't handle -- I think it was variable length arrays. DSA may be able to handle that case now. I'm happy to give you my code if you want to use it and/or work it up for submission into LLVM. If you don't want the dependence on DSA, you could probably easily modify it to make it simpler and more conservative. Rob Robert L. Bocchino Jr. Ph.D. Student University of Illinois, Urbana-Champaign -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20070216/1c8aecdc/attachment.html>
On Fri, 16 Feb 2007, Nick Lewycky wrote:> That's surprising to me. I thought there was a pass that converts > malloc's that trivially dominate all free's and whose pointer doesn't > escape would be lowered to alloca's -- but I looked and couldn't find one.nope, there isn't one.> Why isn't there one? Because it wouldn't be profitable? Or because the > alias analysis is too complex? Or just because nobody's gotten to it yet?the last one: "nobody's gotten to it yet" :)> I would've thought that this pattern would occur rather often. I know > I've written code that strdup's a value, reads/writes it, then frees it. > After inlining the strdup, changing the malloc/free to alloca should be > easy.Perhaps. I haven't see any C code where this occurs frequently, but the pypy people would like to have this.> That said, you'd have to be careful to ensure that the malloc-free pair > isn't inside of a loop before converting it to alloca. Regardless, I'd > be interested in implementing this in a pass if noone else does.Right, also be careful not to turn 'large' allocations into large stack objects, etc. -Chris -- http://nondot.org/sabre/ http://llvm.org/