tree.h (struct tree_decl): Add malloc_flag.
authorAnthony Green <green@cygnus.com>
Sun, 28 Nov 1999 20:45:34 +0000 (20:45 +0000)
committerAnthony Green <green@gcc.gnu.org>
Sun, 28 Nov 1999 20:45:34 +0000 (20:45 +0000)
* tree.h (struct tree_decl): Add malloc_flag.
(DECL_IS_MALLOC): Define.
* c-common.c (attrs): Add A_MALLOC attribute.
(init_attributes): Add this attribute to the table.
(decl_attributes): Handle malloc attribute.
* calls.c (special_function_p): Check for the malloc attribute.
* extend.texi (Function Attributes): Document malloc attribute.

From-SVN: r30689

gcc/ChangeLog
gcc/c-common.c
gcc/calls.c
gcc/extend.texi
gcc/tree.h

index f4f058e9c67fa57e1768f5aa31d80c7257485657..109c83451b389a5030b5f0705bdeae86ea045848 100644 (file)
@@ -1,3 +1,13 @@
+1999-11-28  Anthony Green  <green@cygnus.com>
+
+       * tree.h (struct tree_decl): Add malloc_flag.
+       (DECL_IS_MALLOC): Define.
+       * c-common.c (attrs): Add A_MALLOC attribute.
+       (init_attributes): Add this attribute to the table.
+       (decl_attributes): Handle malloc attribute.
+       * calls.c (special_function_p): Check for the malloc attribute.
+       * extend.texi (Function Attributes): Document malloc attribute.
+
 Sun Nov 28 13:21:00 1999  Jeffrey A Law  (law@cygnus.com)
 
        * pa.md (reload shift-add patterns): Remove.
index 742eba0bfbf5f3f1d249684ac8b8aa2ee0f180bb..10c162402844f6fb57283c70884b71f0d5a9f93e 100644 (file)
@@ -140,7 +140,7 @@ int skip_evaluation;
 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
            A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
            A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
-           A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS};
+           A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC};
 
 enum format_type { printf_format_type, scanf_format_type,
                   strftime_format_type };
@@ -481,6 +481,7 @@ init_attributes ()
   add_attribute (A_ALIAS, "alias", 1, 1, 1);
   add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
   add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
+  add_attribute (A_MALLOC, "malloc", 0, 0, 1);
 }
 \f
 /* Default implementation of valid_lang_attribute, below.  By default, there
@@ -617,6 +618,13 @@ decl_attributes (node, attributes, prefix_attributes)
            warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
          break;
 
+       case A_MALLOC:
+         if (TREE_CODE (decl) == FUNCTION_DECL)
+           DECL_IS_MALLOC (decl) = 1;
+         else
+           warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
+         break;
+
        case A_UNUSED:
          if (is_type)
            TREE_USED (type) = 1;
index 48c0d5e09bd34179c44aef1db836a52118236e2d..d8805955a852d920f7d5b0743b9feccb878d097e 100644 (file)
@@ -546,10 +546,14 @@ special_function_p (name, fndecl, returns_twice, is_longjmp,
 {
   *returns_twice = 0;
   *is_longjmp = 0;
-  *is_malloc = 0;
   *may_be_alloca = 0;
 
-  if (name != 0 && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) <= 17
+  /* The function decl may have the `malloc' attribute.  */
+  *is_malloc = fndecl && DECL_IS_MALLOC (fndecl);
+
+  if (! is_malloc 
+      && name != 0 
+      && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) <= 17
       /* Exclude functions not at the file scope, or not `extern',
         since they are not the magic functions we would otherwise
         think they are.  */
@@ -602,8 +606,8 @@ special_function_p (name, fndecl, returns_twice, is_longjmp,
       else if (tname[0] == 'l' && tname[1] == 'o'
               && ! strcmp (tname, "longjmp"))
        *is_longjmp = 1;
-      /* XXX should have "malloc" attribute on functions instead
-        of recognizing them by name.  */
+      /* Do not add any more malloc-like functions to this list,
+        instead mark as malloc functions using the malloc attribute.  */
       else if (! strcmp (tname, "malloc")
               || ! strcmp (tname, "calloc")
               || ! strcmp (tname, "realloc")
index 17f12c95d92b04b862a13a74696fabb9293798a8..be217a35c8ccef435142fcc757511458002935ef 100644 (file)
@@ -1310,6 +1310,7 @@ hack ((union foo) x);
 @cindex functions that never return
 @cindex functions that have no side effects
 @cindex functions in arbitrary sections
+@cindex functions that bahave like malloc
 @cindex @code{volatile} applied to function
 @cindex @code{const} applied to function
 @cindex functions with @code{printf}, @code{scanf} or @code{strftime} style arguments
@@ -1323,10 +1324,10 @@ carefully.
 
 The keyword @code{__attribute__} allows you to specify special
 attributes when making a declaration.  This keyword is followed by an
-attribute specification inside double parentheses.  Nine attributes,
+attribute specification inside double parentheses.  Ten attributes,
 @code{noreturn}, @code{const}, @code{format},
-@code{no_instrument_function}, @code{section},
-@code{constructor}, @code{destructor}, @code{unused} and @code{weak} are
+@code{no_instrument_function}, @code{section}, @code{constructor},
+@code{destructor}, @code{unused}, @code{weak} and @code{malloc} are
 currently defined for functions.  Other attributes, including
 @code{section} are supported for variables declarations (@pxref{Variable
 Attributes}) and for types (@pxref{Type Attributes}).
@@ -1540,6 +1541,13 @@ also be used with non-function declarations.  Weak symbols are supported
 for ELF targets, and also for a.out targets when using the GNU assembler
 and linker.
 
+@item malloc
+@cindex @code{malloc} attribute
+The @code{malloc} attribute is used to tell the compiler that a function
+may be treated as if it were the malloc function.  The compiler assumes
+that calls to malloc result in a pointers that cannot alias anything.
+This will often improve optimization.
+
 @item alias ("target")
 @cindex @code{alias} attribute
 The @code{alias} attribute causes the declaration to be emitted as an
index d8056891fc0259f13c7edc938642dadf6ad41256..fcfa2190b362bdc36846d9b11dd144ffeca5d33a 100644 (file)
@@ -1238,6 +1238,11 @@ struct tree_type
    to redefine for any purpose whatever.  */
 #define DECL_BUILT_IN_NONANSI(NODE) ((NODE)->common.unsigned_flag)
 
+/* Nonzero in a FUNCTION_DECL means this function should be treated
+   as if it were a malloc, meaning it returns a pointer that is
+   not an alias.  */
+#define DECL_IS_MALLOC(NODE) (DECL_CHECK (NODE)->decl.malloc_flag)
+
 /* Nonzero in a FIELD_DECL means it is a bit field, and must be accessed
    specially.  */
 #define DECL_BIT_FIELD(NODE) (DECL_CHECK (NODE)->decl.bit_field_flag)
@@ -1370,6 +1375,7 @@ struct tree_decl
   unsigned no_instrument_function_entry_exit : 1;
   unsigned no_check_memory_usage : 1;
   unsigned comdat_flag : 1;
+  unsigned malloc_flag : 1;
 
   /* For a FUNCTION_DECL, if inline, this is the size of frame needed.
      If built-in, this is the code for which built-in function.