* 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
+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.
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 };
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
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;
{
*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. */
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")
@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
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}).
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
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)
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.