Bug 20638 - gcc doesn't take advantage of attribute malloc on alloca
Summary: gcc doesn't take advantage of attribute malloc on alloca
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.1.0
: P2 normal
Target Milestone: 4.1.0
Assignee: Not yet assigned to anyone
URL:
Keywords: alias, missed-optimization
Depends on: 20641
Blocks:
  Show dependency treegraph
 
Reported: 2005-03-25 20:02 UTC by Kaveh Ghazi
Modified: 2005-10-09 16:54 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-03-25 21:00:42


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kaveh Ghazi 2005-03-25 20:02:17 UTC
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-
CVS20050323/gcc/testsuite/gcc.dg/builtins-13.c
--- orig/egcc-CVS20050323/gcc/testsuite/gcc.dg/builtins-13.c    2003-04-13 
22:55:31.000000000 -0400
+++ egcc-CVS20050323/gcc/testsuite/gcc.dg/builtins-13.c 2005-03-24 
17:16:50.098461000 -0500
@@ -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)
     link_error ();
 }

+void test3(void)
+{
+  int *ptr1, *ptr2;
+
+  ptr1 = &x;
+  ptr2 = (int*) alloca (sizeof (int));
+
+  *ptr1 = 12;
+  *ptr2 = 8;
+
+  if (*ptr1 != 12)
+    link_error();
+}
+
 int main()
 {
   test1 ();
   test2 ();
+  test3 ();
   return 0;
 }
Comment 1 Andrew Pinski 2005-03-25 21:00:42 UTC
Confirmed, I also noticed that we don't take advantage of the malloc attribute on the tree level either, 
see PR 20641.
Comment 2 Kaveh Ghazi 2005-04-28 13:11:40 UTC
Roger Sayle provided this feedback to me in private email and agreed to have it 
copied here:

------------------------------------------------------------------

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.

Roger
Comment 3 Andrew Pinski 2005-09-21 02:13:28 UTC
Note this is now fixed on the mainline by tree optimizers.
Comment 4 Andrew Pinski 2005-10-09 16:54:12 UTC
Fixed on the mainline.