[LLVMdev] optimization assumes malloc return is non-null

Denys Vlasenko vda.linux at googlemail.com
Mon Jun 23 08:37:15 PDT 2008


On Thursday 01 May 2008 19:14, Jonathan S. Shapiro wrote:
> On Thu, 2008-05-01 at 12:00 -0500, David Greene wrote:
> > On Wednesday 30 April 2008 21:21, Chris Lattner wrote:
> > 
> > > If LLVM is able to eliminate all users of the malloc assuming the
> > > malloc succeeded (as in this case), then it is safe to assume the malloc
> > > returned success.
> > 
> > Ah, I missed this bit.  I didn't see that the result of malloc was not used
> > outside the if test.
> > 
> > But is the if test considered a "use?"  If so, it's a use in the context of
> > success or failure, so how can LLVM assume success?
> 
> Small correction. The pivotal issue isn't "use", it is "capture". In
> this case, the value returned by malloc is not bound to any variable
> that survives, so there is no capture of the return value.
> 
> > 1. LLVM can't assume malloc success
> 
> I was confused about this too. LLVM actually *can* assume malloc success
> in this case, using any of several arguments that came up in my exchange
> with Daveed.

Under this reasoning, it can equally assume malloc *failure*.

Under this assumption, it can eliminate malloc exactly as in
your arguments and then

int main(int argc, char** argv){
   if(malloc(sizeof(int)) == NULL){ return 0; }
   else{ return 1; }
}

is optimized to returning 0.

Which leads us to the same programs being validly optimized to returning 0
and to returning 1. Which is an observable (and nonsensical) behavior.
--
vda



More information about the llvm-dev mailing list