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

Chris Lattner sabre at nondot.org
Tue Feb 13 16:42:51 PST 2007


On Tue, 13 Feb 2007, Nicola Lugato wrote:
> Hi, i have some code that allocate some memory, store the pointer to a
> variable, read it back and deallocates it, like this:

ok

> i expected the optimized to remove everything, but after running it
> the code i get is:
>
> int %main(int %argc, ubyte** %argv) {
> 	%malloc_206 = malloc [10 x ubyte]
> 	%malloc_206.sub = getelementptr [10 x ubyte]* %malloc_206, int 0, int 0
> 	free ubyte* %malloc_206.sub
> 	ret int 0
> }
>
> Why didn't he optimized it out?

This looks like a simple missed optimization.

> and where did that getelementptr came from?

This is just passing a pointer to the first byte of the array into the 
free instruction.  This has the same semantics as freeing the pointer to 
the array, since they are the same value.

> I have the feeling that i'm missing something.

Nope, I don't think so.  Please feel free to file an enhancement request 
for us to catch this case!

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/



More information about the llvm-dev mailing list