This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Patch: malloc function attribute
- To: gcc-patches at gcc dot gnu dot org
- Subject: Patch: malloc function attribute
- From: Anthony Green <green at cygnus dot com>
- Date: Sat, 27 Nov 1999 19:52:54 -0800 (PST)
The java runtime library has many functions for allocating memory in
different ways. They all behave like `malloc' in that they always
return pointers that cannot alias anything. The compiler should be
taking advantage of this information. It already tries to identify
malloc-like functions by looking for special function names (see
special_function_p in calls.c). Rather than add more magic function
names, I've added a `malloc' function attribute for annotating source
- as is suggested by this comment in special_function_p:
/* XXX should have "malloc" attribute on functions instead
of recognizing them by name. */
I have correspoding patches to annotate jc1's builtin decls, and the
libgcj header files. It definitely results in better code.
1999-11-27 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.
Index: tree.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/tree.h,v
retrieving revision 1.104
diff -u -r1.104 tree.h
--- tree.h 1999/11/24 01:19:04 1.104
+++ tree.h 1999/11/28 03:24:09
@@ -1238,6 +1238,11 @@
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)
@@ -1347,6 +1352,7 @@
unsigned virtual_flag : 1;
unsigned ignored_flag : 1;
unsigned abstract_flag : 1;
+ unsigned malloc_flag : 1;
unsigned in_system_header_flag : 1;
unsigned common_flag : 1;
Index: c-common.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/c-common.c,v
retrieving revision 1.76
diff -u -r1.76 c-common.c
--- c-common.c 1999/11/25 16:58:31 1.76
+++ c-common.c 1999/11/28 03:24:10
@@ -140,7 +140,7 @@
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 @@
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);
}
/* Default implementation of valid_lang_attribute, below. By default, there
@@ -613,6 +614,13 @@
= build_pointer_type
(build_type_variant (TREE_TYPE (type),
TREE_READONLY (TREE_TYPE (type)), 1));
+ else
+ 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;
Index: calls.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/calls.c,v
retrieving revision 1.69
diff -u -r1.69 calls.c
--- calls.c 1999/11/16 17:43:39 1.69
+++ calls.c 1999/11/28 03:24:12
@@ -546,10 +546,14 @@
{
*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. */
Index: extend.texi
===================================================================
RCS file: /cvs/gcc/egcs/gcc/extend.texi,v
retrieving revision 1.40
diff -u -r1.40 extend.texi
--- extend.texi 1999/11/19 13:02:59 1.40
+++ extend.texi 1999/11/28 03:24:15
@@ -1310,6 +1310,7 @@
@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 @@
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}).
@@ -1539,6 +1540,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 pointers that cannot alias anything.
+This will often improve optimization.
@item alias ("target")
@cindex @code{alias} attribute
--
Anthony Green Cygnus Solutions
Sunnyvale, California