On 10/1/2012 9:40 PM, Nick Lewycky wrote:> > As far as I know, this optimization is legal.It's not legal. 1. malloc is not known to always return a non-null pointer. 2. malloc has side-effects and hence cannot be eliminated. -K -- Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation
On Mon, Oct 15, 2012 at 2:28 PM, Krzysztof Parzyszek <kparzysz at codeaurora.org> wrote:> On 10/1/2012 9:40 PM, Nick Lewycky wrote: >> >> >> As far as I know, this optimization is legal. > > > It's not legal. > 1. malloc is not known to always return a non-null pointer.The system malloc isn't, but the compiler can change any given call to malloc to call an implementation which is known to return a non-null pointer. -Eli
On 10/15/2012 4:39 PM, Eli Friedman wrote:> > The system malloc isn't, but the compiler can change any given call to > malloc to call an implementation which is known to return a non-null > pointer.Do we do that in LLVM? That would be surprising... Optimizing calls to malloc (like memory pooling for example) is not a trivial thing to do, and it requires a fairly strong interprocedural analysis. The fact that this seems to happen with LLVM at -O2 looks more like a bug than a clever optimization. -K -- Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation
On Oct 15, 2012, at 2:28 PM, Krzysztof Parzyszek <kparzysz at codeaurora.org> wrote:> On 10/1/2012 9:40 PM, Nick Lewycky wrote: >> >> As far as I know, this optimization is legal. > > It's not legal. > 1. malloc is not known to always return a non-null pointer. > 2. malloc has side-effects and hence cannot be eliminated.You've made a number of claims on this thread, but I'm not sure what you're basing these claims on - certainly not the C standard. If you don't want the compiler to touch well known functions like malloc, build with -fno-builtin and you'll get the behavior you want. -Chris
On 10/15/2012 7:20 PM, Chris Lattner wrote:> > You've made a number of claims on this thread, but I'm not sure what you're basing these claims on - certainly not the C standard. > > If you don't want the compiler to touch well known functions like malloc, build with -fno-builtin and you'll get the behavior you want.The only claim you can object to is about side-effects. It was more of a general remark, but yes, the compiler can delete calls where the result is not used. This is indeed a case of a "dead malloc", which Eli pointed out. Is there anything else you disagree with? -K -- Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation