Re: Protect some checks of DECL_FUNCTION_CODE

2019-08-13 Thread Jeff Law
On 8/13/19 3:34 AM, Richard Sandiford wrote:
> This patch protects various uses of DECL_FUNCTION_CODE that didn't
> obviously check for BUILT_IN_NORMAL first (either directly or in callers).
> They could therefore trigger for functions that either aren't built-ins
> or are a different kind of built-in.
> 
> Also, the patch removes a redundant GIMPLE_CALL check from
> optimize_stdarg_builtin, since it gave the impression that the stmt
> was less well checked than it actually is.
> 
> Tested on aarch64-linux-gnu and x86_64-linux-gnu (--enable-languages=all
> --enable-host-shared).  OK to install?
> 
> Richard
> 
> 
> 2019-08-13  Richard Sandiford  
> 
> gcc/
>   PR middle-end/91421
>   * attribs.c (decl_attributes): Check the DECL_BUILT_IN_CLASS
>   before the DECL_FUNCTION_CODE.
>   * calls.c (maybe_warn_alloc_args_overflow): Use fndecl_built_in_p
>   to check for a BUILT_IN_ALLOCA call.
>   * ipa-cp.c (ipa_get_indirect_edge_target_1): Likewise for
>   BUILT_IN_UNREACHABLE.  Don't check for a FUNCTION_TYPE.
>   * ipa-devirt.c (possible_polymorphic_call_target_p): Likewise.
>   * ipa-prop.c (try_make_edge_direct_virtual_call): Likewise.
>   * gimple-ssa-isolate-paths.c (is_addr_local): Check specifically
>   for BUILT_IN_NORMAL functions.
>   * trans-mem.c (expand_block_edges): Use gimple_call_builtin_p to
>   test for BUILT_IN_TM_ABORT.
>   * tree-ssa-ccp.c (optimize_stack_restore): Use fndecl_built_in_p
>   to check for a BUILT_IN_STACK_RESTORE call.
>   (optimize_stdarg_builtin): Remove redundant check for GIMPLE_CALL.
>   * tree-ssa-threadedge.c
>   (record_temporary_equivalences_from_stmts_at_dest): Check for a
>   BUILT_IN_NORMAL decl before checking its DECL_FUNCTION_CODE.
>   * tree-vect-patterns.c (vect_recog_pow_pattern): Use a positive
>   test for a BUILT_IN_NORMAL call instead of a negative test for
>   an internal function call.
> 
> gcc/c/
>   PR middle-end/91421
>   * c-decl.c (header_for_builtin_fn): Take a FUNCTION_DECL instead
>   of a built_in_function.
>   (diagnose_mismatched_decls, implicitly_declare): Update accordingly.
OK
jeff


Protect some checks of DECL_FUNCTION_CODE

2019-08-13 Thread Richard Sandiford
This patch protects various uses of DECL_FUNCTION_CODE that didn't
obviously check for BUILT_IN_NORMAL first (either directly or in callers).
They could therefore trigger for functions that either aren't built-ins
or are a different kind of built-in.

Also, the patch removes a redundant GIMPLE_CALL check from
optimize_stdarg_builtin, since it gave the impression that the stmt
was less well checked than it actually is.

Tested on aarch64-linux-gnu and x86_64-linux-gnu (--enable-languages=all
--enable-host-shared).  OK to install?

Richard


2019-08-13  Richard Sandiford  

gcc/
PR middle-end/91421
* attribs.c (decl_attributes): Check the DECL_BUILT_IN_CLASS
before the DECL_FUNCTION_CODE.
* calls.c (maybe_warn_alloc_args_overflow): Use fndecl_built_in_p
to check for a BUILT_IN_ALLOCA call.
* ipa-cp.c (ipa_get_indirect_edge_target_1): Likewise for
BUILT_IN_UNREACHABLE.  Don't check for a FUNCTION_TYPE.
* ipa-devirt.c (possible_polymorphic_call_target_p): Likewise.
* ipa-prop.c (try_make_edge_direct_virtual_call): Likewise.
* gimple-ssa-isolate-paths.c (is_addr_local): Check specifically
for BUILT_IN_NORMAL functions.
* trans-mem.c (expand_block_edges): Use gimple_call_builtin_p to
test for BUILT_IN_TM_ABORT.
* tree-ssa-ccp.c (optimize_stack_restore): Use fndecl_built_in_p
to check for a BUILT_IN_STACK_RESTORE call.
(optimize_stdarg_builtin): Remove redundant check for GIMPLE_CALL.
* tree-ssa-threadedge.c
(record_temporary_equivalences_from_stmts_at_dest): Check for a
BUILT_IN_NORMAL decl before checking its DECL_FUNCTION_CODE.
* tree-vect-patterns.c (vect_recog_pow_pattern): Use a positive
test for a BUILT_IN_NORMAL call instead of a negative test for
an internal function call.

gcc/c/
PR middle-end/91421
* c-decl.c (header_for_builtin_fn): Take a FUNCTION_DECL instead
of a built_in_function.
(diagnose_mismatched_decls, implicitly_declare): Update accordingly.

Index: gcc/attribs.c
===
--- gcc/attribs.c   2019-07-18 09:22:13.409771785 +0100
+++ gcc/attribs.c   2019-08-13 10:31:18.211019996 +0100
@@ -691,6 +691,7 @@ decl_attributes (tree *node, tree attrib
 
  if (!built_in
  || !DECL_P (*anode)
+ || DECL_BUILT_IN_CLASS (*anode) != BUILT_IN_NORMAL
  || (DECL_FUNCTION_CODE (*anode) != BUILT_IN_UNREACHABLE
  && (DECL_FUNCTION_CODE (*anode)
  != BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE)))
Index: gcc/calls.c
===
--- gcc/calls.c 2019-07-29 09:39:49.750165306 +0100
+++ gcc/calls.c 2019-08-13 10:31:18.215019968 +0100
@@ -1350,7 +1350,6 @@ maybe_warn_alloc_args_overflow (tree fn,
   location_t loc = EXPR_LOCATION (exp);
 
   tree fntype = fn ? TREE_TYPE (fn) : TREE_TYPE (TREE_TYPE (exp));
-  built_in_function fncode = fn ? DECL_FUNCTION_CODE (fn) : BUILT_IN_NONE;
   bool warned = false;
 
   /* Validate each argument individually.  */
@@ -1376,11 +1375,10 @@ maybe_warn_alloc_args_overflow (tree fn,
 friends.
 Also avoid issuing the warning for calls to function named
 "alloca".  */
- if ((fncode == BUILT_IN_ALLOCA
-  && IDENTIFIER_LENGTH (DECL_NAME (fn)) != 6)
- || (fncode != BUILT_IN_ALLOCA
- && !lookup_attribute ("returns_nonnull",
-   TYPE_ATTRIBUTES (fntype
+ if (fn && fndecl_built_in_p (fn, BUILT_IN_ALLOCA)
+ ? IDENTIFIER_LENGTH (DECL_NAME (fn)) != 6
+ : !lookup_attribute ("returns_nonnull",
+  TYPE_ATTRIBUTES (fntype)))
warned = warning_at (loc, OPT_Walloc_zero,
 "%Kargument %i value is zero",
 exp, idx[i] + 1);
Index: gcc/ipa-cp.c
===
--- gcc/ipa-cp.c2019-07-16 09:11:05.561423510 +0100
+++ gcc/ipa-cp.c2019-08-13 10:31:18.219019937 +0100
@@ -2436,8 +2436,7 @@ ipa_get_indirect_edge_target_1 (struct c
  if (can_refer)
{
  if (!target
- || (TREE_CODE (TREE_TYPE (target)) == FUNCTION_TYPE
- && DECL_FUNCTION_CODE (target) == BUILT_IN_UNREACHABLE)
+ || fndecl_built_in_p (target, BUILT_IN_UNREACHABLE)
  || !possible_polymorphic_call_target_p
   (ie, cgraph_node::get (target)))
{
Index: gcc/ipa-devirt.c
===
--- gcc/ipa-devirt.c2019-08-05 17:46:20.173727579 +0100
+++ gcc/ipa-devirt.c2019-08-13