builtins.def (BUILT_IN_VA_ARG_PACK_LEN): New builtin.

* builtins.def (BUILT_IN_VA_ARG_PACK_LEN): New builtin.
	* builtins.c (expand_builtin) <case BUILT_IN_VA_ARG_PACK_LEN>: Issue
	error if __builtin_va_arg_pack_len () wasn't optimized out during
	inlining.
	* tree-inline.c (copy_bb): Replace __builtin_va_arg_pack_len ()
	with the number of inline's anonymous arguments.
	* doc/extend.texi: Document __builtin_va_arg_pack_len ().

	* gcc.dg/va-arg-pack-len-1.c: New test.
	* g++.dg/va-arg-pack-len-1.C: New test.

From-SVN: r128376
This commit is contained in:
Jakub Jelinek 2007-09-11 15:40:14 +02:00 committed by Jakub Jelinek
parent e6ebd07f47
commit ab0e176c5b
8 changed files with 324 additions and 0 deletions

View File

@ -1,3 +1,13 @@
2007-09-11 Jakub Jelinek <jakub@redhat.com>
* builtins.def (BUILT_IN_VA_ARG_PACK_LEN): New builtin.
* builtins.c (expand_builtin) <case BUILT_IN_VA_ARG_PACK_LEN>: Issue
error if __builtin_va_arg_pack_len () wasn't optimized out during
inlining.
* tree-inline.c (copy_bb): Replace __builtin_va_arg_pack_len ()
with the number of inline's anonymous arguments.
* doc/extend.texi: Document __builtin_va_arg_pack_len ().
2007-09-11 Zdenek Dvorak <ook@ucw.cz>
* fold-const.c (extract_muldiv_1): Do not simplify

View File

@ -6276,6 +6276,12 @@ expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
error ("invalid use of %<__builtin_va_arg_pack ()%>");
return const0_rtx;
case BUILT_IN_VA_ARG_PACK_LEN:
/* All valid uses of __builtin_va_arg_pack_len () are removed during
inlining. */
error ("invalid use of %<__builtin_va_arg_pack_len ()%>");
return const0_rtx;
/* Return the address of the first anonymous stack arg. */
case BUILT_IN_NEXT_ARG:
if (fold_builtin_next_arg (exp, false))

View File

@ -702,6 +702,7 @@ DEF_GCC_BUILTIN (BUILT_IN_VA_COPY, "va_copy", BT_FN_VOID_VALIST_REF_VALIS
DEF_GCC_BUILTIN (BUILT_IN_VA_END, "va_end", BT_FN_VOID_VALIST_REF, ATTR_NULL)
DEF_GCC_BUILTIN (BUILT_IN_VA_START, "va_start", BT_FN_VOID_VALIST_REF_VAR, ATTR_NULL)
DEF_GCC_BUILTIN (BUILT_IN_VA_ARG_PACK, "va_arg_pack", BT_FN_INT, ATTR_PURE_NOTHROW_LIST)
DEF_GCC_BUILTIN (BUILT_IN_VA_ARG_PACK_LEN, "va_arg_pack_len", BT_FN_INT, ATTR_PURE_NOTHROW_LIST)
DEF_EXT_LIB_BUILTIN (BUILT_IN__EXIT, "_exit", BT_FN_VOID_INT, ATTR_NORETURN_NOTHROW_LIST)
DEF_C99_BUILTIN (BUILT_IN__EXIT2, "_Exit", BT_FN_VOID_INT, ATTR_NORETURN_NOTHROW_LIST)

View File

@ -583,6 +583,41 @@ myprintf (FILE *f, const char *format, ...)
@end smallexample
@end deftypefn
@deftypefn {Built-in Function} __builtin_va_arg_pack_len ()
This built-in function returns the number of anonymous arguments of
an inline function. It can be used only in inline functions which
will be always inlined, never compiled as a separate function, such
as those using @code{__attribute__ ((__always_inline__))} or
@code{__attribute__ ((__gnu_inline__))} extern inline functions.
For example following will do link or runtime checking of open
arguments for optimized code:
@smallexample
#ifdef __OPTIMIZE__
extern inline __attribute__((__gnu_inline__)) int
myopen (const char *path, int oflag, ...)
@{
if (__builtin_va_arg_pack_len () > 1)
warn_open_too_many_arguments ();
if (__builtin_constant_p (oflag))
@{
if ((oflag & O_CREAT) != 0 && __builtin_va_arg_pack_len () < 1)
@{
warn_open_missing_mode ();
return __open_2 (path, oflag);
@}
return open (path, oflag, __builtin_va_arg_pack ());
@}
if (__builtin_va_arg_pack_len () < 1)
return __open_2 (path, oflag);
return open (path, oflag, __builtin_va_arg_pack ());
@}
#endif
@end smallexample
@end deftypefn
@node Typeof
@section Referring to a Type with @code{typeof}
@findex typeof

View File

@ -1,3 +1,8 @@
2007-09-11 Jakub Jelinek <jakub@redhat.com>
* gcc.dg/va-arg-pack-len-1.c: New test.
* g++.dg/va-arg-pack-len-1.C: New test.
2007-09-11 Michael Matz <matz@suse.de>
* gcc.dg/tree-ssa/loadpre11.c: Add -fno-tree-cselim to flags.

View File

@ -0,0 +1,120 @@
// { dg-do run }
// { dg-options "-O2" }
#include <stdarg.h>
extern "C" int warn_open_missing_mode (void);
extern "C" int warn_open_too_many_arguments (void);
extern "C" void abort (void);
char expected_char;
__attribute__((noinline)) int
myopen2 (const char *path, int oflag)
{
if (expected_char++ != path[0] || path[1] != '\0')
abort ();
switch (path[0])
{
case 'f':
if (oflag != 0x2)
abort ();
break;
case 'g':
if (oflag != 0x43)
abort ();
// In real __open_2 this would terminate the program:
// open with O_CREAT without third argument.
return -6;
default:
abort ();
}
return 0;
}
__attribute__((noinline)) int
myopenva (const char *path, int oflag, ...)
{
int mode = 0;
va_list ap;
if ((oflag & 0x40) != 0)
{
va_start (ap, oflag);
mode = va_arg (ap, int);
va_end (ap);
}
if (expected_char++ != path[0] || path[1] != '\0')
abort ();
switch (path[0])
{
case 'a':
if (oflag != 0x43 || mode != 0644)
abort ();
break;
case 'b':
if (oflag != 0x3)
abort ();
break;
case 'c':
if (oflag != 0x2)
abort ();
break;
case 'd':
if (oflag != 0x43 || mode != 0600)
abort ();
break;
case 'e':
if (oflag != 0x3)
abort ();
break;
default:
abort ();
}
return 0;
}
extern inline __attribute__((always_inline, gnu_inline)) int
myopen (const char *path, int oflag, ...)
{
if (__builtin_va_arg_pack_len () > 1)
warn_open_too_many_arguments ();
if (__builtin_constant_p (oflag))
{
if ((oflag & 0x40) != 0 && __builtin_va_arg_pack_len () < 1)
{
warn_open_missing_mode ();
return myopen2 (path, oflag);
}
return myopenva (path, oflag, __builtin_va_arg_pack ());
}
if (__builtin_va_arg_pack_len () < 1)
return myopen2 (path, oflag);
return myopenva (path, oflag, __builtin_va_arg_pack ());
}
volatile int l0;
int
main (void)
{
expected_char = 'a';
if (myopen ("a", 0x43, 0644))
abort ();
if (myopen ("b", 0x3, 0755))
abort ();
if (myopen ("c", 0x2))
abort ();
if (myopen ("d", l0 + 0x43, 0600))
abort ();
if (myopen ("e", l0 + 0x3, 0700))
abort ();
if (myopen ("f", l0 + 0x2))
abort ();
// Invalid use of myopen, but only detectable at runtime.
if (myopen ("g", l0 + 0x43) != -6)
abort ();
return 0;
}

View File

@ -0,0 +1,120 @@
/* { dg-do run } */
/* { dg-options "-O2" } */
#include <stdarg.h>
extern int warn_open_missing_mode (void);
extern int warn_open_too_many_arguments (void);
extern void abort (void);
char expected_char;
__attribute__((noinline)) int
myopen2 (const char *path, int oflag)
{
if (expected_char++ != path[0] || path[1] != '\0')
abort ();
switch (path[0])
{
case 'f':
if (oflag != 0x2)
abort ();
break;
case 'g':
if (oflag != 0x43)
abort ();
/* In real __open_2 this would terminate the program:
open with O_CREAT without third argument. */
return -6;
default:
abort ();
}
return 0;
}
__attribute__((noinline)) int
myopenva (const char *path, int oflag, ...)
{
int mode = 0;
va_list ap;
if ((oflag & 0x40) != 0)
{
va_start (ap, oflag);
mode = va_arg (ap, int);
va_end (ap);
}
if (expected_char++ != path[0] || path[1] != '\0')
abort ();
switch (path[0])
{
case 'a':
if (oflag != 0x43 || mode != 0644)
abort ();
break;
case 'b':
if (oflag != 0x3)
abort ();
break;
case 'c':
if (oflag != 0x2)
abort ();
break;
case 'd':
if (oflag != 0x43 || mode != 0600)
abort ();
break;
case 'e':
if (oflag != 0x3)
abort ();
break;
default:
abort ();
}
return 0;
}
extern inline __attribute__((always_inline, gnu_inline)) int
myopen (const char *path, int oflag, ...)
{
if (__builtin_va_arg_pack_len () > 1)
warn_open_too_many_arguments ();
if (__builtin_constant_p (oflag))
{
if ((oflag & 0x40) != 0 && __builtin_va_arg_pack_len () < 1)
{
warn_open_missing_mode ();
return myopen2 (path, oflag);
}
return myopenva (path, oflag, __builtin_va_arg_pack ());
}
if (__builtin_va_arg_pack_len () < 1)
return myopen2 (path, oflag);
return myopenva (path, oflag, __builtin_va_arg_pack ());
}
volatile int l0;
int
main (void)
{
expected_char = 'a';
if (myopen ("a", 0x43, 0644))
abort ();
if (myopen ("b", 0x3, 0755))
abort ();
if (myopen ("c", 0x2))
abort ();
if (myopen ("d", l0 + 0x43, 0600))
abort ();
if (myopen ("e", l0 + 0x3, 0700))
abort ();
if (myopen ("f", l0 + 0x2))
abort ();
/* Invalid use of myopen, but only detectable at runtime. */
if (myopen ("g", l0 + 0x43) != -6)
abort ();
return 0;
}

View File

@ -867,6 +867,33 @@ copy_bb (copy_body_data *id, basic_block bb, int frequency_scale, int count_scal
stmt = *stmtp;
update_stmt (stmt);
}
else if (call
&& id->call_expr
&& (decl = get_callee_fndecl (call))
&& DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
&& DECL_FUNCTION_CODE (decl)
== BUILT_IN_VA_ARG_PACK_LEN)
{
/* __builtin_va_arg_pack_len () should be replaced by
the number of anonymous arguments. */
int nargs = call_expr_nargs (id->call_expr);
tree count, *call_ptr, p;
for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
nargs--;
count = build_int_cst (integer_type_node, nargs);
call_ptr = stmtp;
if (TREE_CODE (*call_ptr) == GIMPLE_MODIFY_STMT)
call_ptr = &GIMPLE_STMT_OPERAND (*call_ptr, 1);
if (TREE_CODE (*call_ptr) == WITH_SIZE_EXPR)
call_ptr = &TREE_OPERAND (*call_ptr, 0);
gcc_assert (*call_ptr == call && call_ptr != stmtp);
*call_ptr = count;
stmt = *stmtp;
update_stmt (stmt);
call = NULL_TREE;
}
/* Statements produced by inlining can be unfolded, especially
when we constant propagated some operands. We can't fold