The alloca builtin has the attribute __malloc__ set on it but GCC doesn't seem
to take advantage of that fact for optimization. The testcase gcc.dg/builtins-
13.c tests an optimization when calling the malloc/calloc functions. When I
try a similar case for alloca such as in the patch below, the testcase fails.
diff -rup orig/egcc-CVS20050323/gcc/testsuite/gcc.dg/builtins-13.c egcc-
--- orig/egcc-CVS20050323/gcc/testsuite/gcc.dg/builtins-13.c 2003-04-13
+++ egcc-CVS20050323/gcc/testsuite/gcc.dg/builtins-13.c 2005-03-24
@@ -1,4 +1,4 @@
-/* Copyright (C) 2003 Free Software Foundation.
+/* Copyright (C) 2003, 2005 Free Software Foundation.
Verify that the malloc-like __builtin_ allocation functions are
correctly aliased by the compiler.
@@ -12,6 +12,7 @@ typedef __SIZE_TYPE__ size_t;
extern void abort (void);
extern void *malloc (size_t);
extern void *calloc (size_t,size_t);
+extern void *alloca (size_t);
extern void link_error (void);
@@ -45,10 +46,25 @@ void test2(void)
+ int *ptr1, *ptr2;
+ ptr1 = &x;
+ ptr2 = (int*) alloca (sizeof (int));
+ *ptr1 = 12;
+ *ptr2 = 8;
+ if (*ptr1 != 12)
+ test3 ();
Confirmed, I also noticed that we don't take advantage of the malloc attribute on the tree level either,
see PR 20641.
Roger Sayle provided this feedback to me in private email and agreed to have it
I've glanced through the code and I suspect I know why this isn't
working. The MALLOC attribute is traditionally handled in calls.c
where it attaches a REG_NOALIAS note to the relevant call_insn.
In the case of alloca, however, we don't go through calls.c and
instead inline calls to alloca via expand_builtin_alloca in
builtins.c instead. My guess is that we need to add a REG_NOALIAS
note, or mark/assign the alias set of this pointer in builtins.c.
Interestingly, this doesn't explain why the builtins-13.c tests
aren't now being optimized by tree-ssa, where the "malloc" attribute
should now be getting used during the early alias analysis passes.
Unfortunately, I'm not an expert of how to present aliasing information
at the RTL-level. But the above should help explain why alloca behaves
differently to malloc/calloc/strdup etc.
Note this is now fixed on the mainline by tree optimizers.
Fixed on the mainline.