[LLVMdev] Unused malloc/free don't get optimized

Chris Lattner sabre at nondot.org
Fri Feb 16 22:58:31 PST 2007

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.



More information about the llvm-dev mailing list