From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 81529 invoked by alias); 17 Sep 2019 14:49:35 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 81520 invoked by uid 89); 17 Sep 2019 14:49:34 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-8.9 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_2,GIT_PATCH_3,KAM_ASCII_DIVIDERS,SPF_PASS autolearn=ham version=3.3.1 spammy=target.def, UD:target.def, high-level, UD:tm.texi.in X-HELO: foss.arm.com Received: from foss.arm.com (HELO foss.arm.com) (217.140.110.172) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 17 Sep 2019 14:49:32 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C7C3F28; Tue, 17 Sep 2019 07:49:30 -0700 (PDT) Received: from localhost (dashpi.manchester.arm.com [10.32.98.126]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id F258D3F575; Tue, 17 Sep 2019 07:49:29 -0700 (PDT) From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org,joseph@codesourcery.com, polacek@redhat.com, jason@redhat.com, nathan@acm.org, richard.sandiford@arm.com Cc: joseph@codesourcery.com, polacek@redhat.com, jason@redhat.com, nathan@acm.org Subject: [C][C++] Allow targets to check calls to BUILT_IN_MD functions Date: Tue, 17 Sep 2019 14:49:00 -0000 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-IsSubscribed: yes X-SW-Source: 2019-09/txt/msg01005.txt.bz2 For SVE, we'd like the frontends to check calls to target-specific built-in functions in the same way that they already do for "normal" builtins. This patch adds a target hook for that and extends check_builtin_function_arguments accordingly. A slight complication is that when TARGET_RESOLVE_OVERLOADED_BUILTIN has resolved an overload, it can use build_function_call_vec to build the call to the underlying non-overloaded function decl. This in turn coerces the arguments to the function type and then calls check_builtin_function_arguments to check the final call. If the target does find a problem in this final call, it can be useful to refer to the original overloaded function decl in diagnostics, since that's what the user wrote. The patch therefore passes the original decl as a final optional parameter to build_function_call_vec. Tested on aarch64-linux-gnu and x86_64-linux-gnu. Also tested with the follow-on SVE changes that make use of this. OK to install? Richard 2019-09-17 Richard Sandiford gcc/ * target.def (check_builtin_call): New target hook. * doc/tm.texi.in (TARGET_CHECK_BUILTIN_CALL): New @hook. * doc/tm.texi: Regenerate. gcc/c-family/ * c-common.h (build_function_call_vec): Take the original function decl as an optional final parameter. (check_builtin_function_arguments): Take the original function decl. * c-common.c (check_builtin_function_arguments): Likewise. Handle all built-in functions, not just BUILT_IN_NORMAL ones. Use targetm.check_builtin_call to check BUILT_IN_MD functions. gcc/c/ * c-typeck.c (build_function_call_vec): Take the original function decl as an optional final parameter. Pass all built-in calls to check_builtin_function_arguments. gcc/cp/ * cp-tree.h (build_cxx_call): Take the original function decl as an optional final parameter. (cp_build_function_call_vec): Likewise. * call.c (build_cxx_call): Likewise. Pass all built-in calls to check_builtin_function_arguments. * typeck.c (build_function_call_vec): Take the original function decl as an optional final parameter and pass it to cp_build_function_call_vec. (cp_build_function_call_vec): Take the original function decl as an optional final parameter and pass it to build_cxx_call. Index: gcc/target.def =================================================================== --- gcc/target.def 2019-09-17 15:27:10.918070039 +0100 +++ gcc/target.def 2019-09-17 15:44:25.934426237 +0100 @@ -2397,6 +2397,24 @@ another @code{CALL_EXPR}.\n\ @var{arglist} really has type @samp{VEC(tree,gc)*}", tree, (unsigned int /*location_t*/ loc, tree fndecl, void *arglist), NULL) +DEFHOOK +(check_builtin_call, + "Perform semantic checking on a call to a machine-specific built-in\n\ +function after its arguments have been constrained to the function\n\ +signature. Return true if the call is valid, otherwise report an error\n\ +and return false.\n\ +\n\ +This hook is called after @code{TARGET_RESOLVE_OVERLOADED_BUILTIN}.\n\ +The call was originally to built-in function @var{orig_fndecl},\n\ +but after the optional @code{TARGET_RESOLVE_OVERLOADED_BUILTIN}\n\ +step is now to built-in function @var{fndecl}. @var{loc} is the\n\ +location of the call and @var{args} is an array of function arguments,\n\ +of which there are @var{nargs}. @var{arg_loc} specifies the location\n\ +of each argument.", + bool, (location_t loc, vec arg_loc, tree fndecl, + tree orig_fndecl, unsigned int nargs, tree *args), + NULL) + /* Fold a target-specific builtin to a tree valid for both GIMPLE and GENERIC. */ DEFHOOK Index: gcc/doc/tm.texi.in =================================================================== --- gcc/doc/tm.texi.in 2019-09-17 15:27:09.910077504 +0100 +++ gcc/doc/tm.texi.in 2019-09-17 15:44:25.934426237 +0100 @@ -7938,6 +7938,8 @@ to by @var{ce_info}. @hook TARGET_RESOLVE_OVERLOADED_BUILTIN +@hook TARGET_CHECK_BUILTIN_CALL + @hook TARGET_FOLD_BUILTIN @hook TARGET_GIMPLE_FOLD_BUILTIN Index: gcc/doc/tm.texi =================================================================== --- gcc/doc/tm.texi 2019-09-17 15:27:09.894077623 +0100 +++ gcc/doc/tm.texi 2019-09-17 15:44:25.934426237 +0100 @@ -11567,6 +11567,21 @@ another @code{CALL_EXPR}. @var{arglist} really has type @samp{VEC(tree,gc)*} @end deftypefn +@deftypefn {Target Hook} bool TARGET_CHECK_BUILTIN_CALL (location_t @var{loc}, vec @var{arg_loc}, tree @var{fndecl}, tree @var{orig_fndecl}, unsigned int @var{nargs}, tree *@var{args}) +Perform semantic checking on a call to a machine-specific built-in +function after its arguments have been constrained to the function +signature. Return true if the call is valid, otherwise report an error +and return false. + +This hook is called after @code{TARGET_RESOLVE_OVERLOADED_BUILTIN}. +The call was originally to built-in function @var{orig_fndecl}, +but after the optional @code{TARGET_RESOLVE_OVERLOADED_BUILTIN} +step is now to built-in function @var{fndecl}. @var{loc} is the +location of the call and @var{args} is an array of function arguments, +of which there are @var{nargs}. @var{arg_loc} specifies the location +of each argument. +@end deftypefn + @deftypefn {Target Hook} tree TARGET_FOLD_BUILTIN (tree @var{fndecl}, int @var{n_args}, tree *@var{argp}, bool @var{ignore}) Fold a call to a machine specific built-in function that was set up by @samp{TARGET_INIT_BUILTINS}. @var{fndecl} is the declaration of the Index: gcc/c-family/c-common.h =================================================================== --- gcc/c-family/c-common.h 2019-09-09 12:19:39.361586540 +0100 +++ gcc/c-family/c-common.h 2019-09-17 15:44:25.922426328 +0100 @@ -818,7 +818,7 @@ extern void check_function_arguments_rec void *, tree, unsigned HOST_WIDE_INT); extern bool check_builtin_function_arguments (location_t, vec, - tree, int, tree *); + tree, tree, int, tree *); extern void check_function_format (const_tree, tree, int, tree *, vec *); extern bool attribute_fallthrough_p (tree); @@ -995,7 +995,8 @@ extern bool c_switch_covers_all_cases_p extern tree build_function_call (location_t, tree, tree); extern tree build_function_call_vec (location_t, vec, tree, - vec *, vec *); + vec *, vec *, + tree = NULL_TREE); extern tree resolve_overloaded_builtin (location_t, tree, vec *); Index: gcc/c-family/c-common.c =================================================================== --- gcc/c-family/c-common.c 2019-09-09 12:19:39.361586540 +0100 +++ gcc/c-family/c-common.c 2019-09-17 15:44:25.922426328 +0100 @@ -5856,15 +5856,27 @@ #define ARG_LOCATION(N) \ /* Verifies the NARGS arguments ARGS to the builtin function FNDECL. Returns false if there was an error, otherwise true. LOC is the location of the function; ARG_LOC is a vector of locations of the - arguments. */ + arguments. If FNDECL is the result of resolving an overloaded + target built-in, ORIG_FNDECL is the original function decl, + otherwise it is null. */ bool check_builtin_function_arguments (location_t loc, vec arg_loc, - tree fndecl, int nargs, tree *args) + tree fndecl, tree orig_fndecl, + int nargs, tree *args) { - if (!fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) + if (!fndecl_built_in_p (fndecl)) return true; + if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) + return (!targetm.check_builtin_call + || targetm.check_builtin_call (loc, arg_loc, fndecl, + orig_fndecl, nargs, args)); + + if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_FRONTEND) + return true; + + gcc_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL); switch (DECL_FUNCTION_CODE (fndecl)) { case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX: Index: gcc/c/c-typeck.c =================================================================== --- gcc/c/c-typeck.c 2019-09-05 08:49:25.933774585 +0100 +++ gcc/c/c-typeck.c 2019-09-17 15:44:25.926426298 +0100 @@ -3003,6 +3003,8 @@ inform_declaration (tree decl) } /* Build a function call to function FUNCTION with parameters PARAMS. + If FUNCTION is the result of resolving an overloaded target built-in, + ORIG_FUNDECL is the original function decl, otherwise it is null. ORIGTYPES, if not NULL, is a vector of types; each element is either NULL or the original type of the corresponding element in PARAMS. The original type may differ from TREE_TYPE of the @@ -3013,7 +3015,7 @@ inform_declaration (tree decl) tree build_function_call_vec (location_t loc, vec arg_loc, tree function, vec *params, - vec *origtypes) + vec *origtypes, tree orig_fundecl) { tree fntype, fundecl = NULL_TREE; tree name = NULL_TREE, result; @@ -3033,6 +3035,8 @@ build_function_call_vec (location_t loc, if (flag_tm) tm_malloc_replacement (function); fundecl = function; + if (!orig_fundecl) + orig_fundecl = fundecl; /* Atomic functions have type checking/casting already done. They are often rewritten and don't match the original parameter list. */ if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9)) @@ -3110,9 +3114,10 @@ build_function_call_vec (location_t loc, argarray = vec_safe_address (params); /* Check that arguments to builtin functions match the expectations. */ - if (fundecl && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL) - && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs, - argarray)) + if (fundecl + && fndecl_built_in_p (fundecl) + && !check_builtin_function_arguments (loc, arg_loc, fundecl, + orig_fundecl, nargs, argarray)) return error_mark_node; /* Check that the arguments to the function are valid. */ Index: gcc/cp/cp-tree.h =================================================================== --- gcc/cp/cp-tree.h 2019-09-17 15:27:10.974069625 +0100 +++ gcc/cp/cp-tree.h 2019-09-17 15:44:25.930426268 +0100 @@ -6257,7 +6257,8 @@ extern tree perform_direct_initializatio tsubst_flags_t); extern tree in_charge_arg_for_name (tree); extern tree build_cxx_call (tree, int, tree *, - tsubst_flags_t); + tsubst_flags_t, + tree = NULL_TREE); extern bool is_std_init_list (tree); extern bool is_list_ctor (tree); extern void validate_conversion_obstack (void); @@ -7390,7 +7391,8 @@ extern tree get_member_function_from_ptr extern tree cp_build_function_call_nary (tree, tsubst_flags_t, ...) ATTRIBUTE_SENTINEL; extern tree cp_build_function_call_vec (tree, vec **, - tsubst_flags_t); + tsubst_flags_t, + tree = NULL_TREE); extern tree build_x_binary_op (const op_location_t &, enum tree_code, tree, enum tree_code, tree, Index: gcc/cp/call.c =================================================================== --- gcc/cp/call.c 2019-09-17 15:27:10.974069625 +0100 +++ gcc/cp/call.c 2019-09-17 15:44:25.926426298 +0100 @@ -9102,12 +9102,14 @@ maybe_warn_class_memaccess (location_t l } /* Build and return a call to FN, using NARGS arguments in ARGARRAY. + If FN is the result of resolving an overloaded target built-in, + ORIG_FNDECL is the original function decl, otherwise it is null. This function performs no overload resolution, conversion, or other high-level operations. */ tree build_cxx_call (tree fn, int nargs, tree *argarray, - tsubst_flags_t complain) + tsubst_flags_t complain, tree orig_fndecl) { tree fndecl; @@ -9117,11 +9119,13 @@ build_cxx_call (tree fn, int nargs, tree SET_EXPR_LOCATION (fn, loc); fndecl = get_callee_fndecl (fn); + if (!orig_fndecl) + orig_fndecl = fndecl; /* Check that arguments to builtin functions match the expectations. */ if (fndecl && !processing_template_decl - && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) + && fndecl_built_in_p (fndecl)) { int i; @@ -9131,7 +9135,7 @@ build_cxx_call (tree fn, int nargs, tree argarray[i] = maybe_constant_value (argarray[i]); if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl, - nargs, argarray)) + orig_fndecl, nargs, argarray)) return error_mark_node; } Index: gcc/cp/typeck.c =================================================================== --- gcc/cp/typeck.c 2019-09-17 15:27:10.954069772 +0100 +++ gcc/cp/typeck.c 2019-09-17 15:44:25.930426268 +0100 @@ -3746,11 +3746,11 @@ build_function_call (location_t /*loc*/, tree build_function_call_vec (location_t /*loc*/, vec /*arg_loc*/, tree function, vec *params, - vec * /*origtypes*/) + vec * /*origtypes*/, tree orig_function) { vec *orig_params = params; tree ret = cp_build_function_call_vec (function, ¶ms, - tf_warning_or_error); + tf_warning_or_error, orig_function); /* cp_build_function_call_vec can reallocate PARAMS by adding default arguments. That should never happen here. Verify @@ -3791,13 +3791,15 @@ cp_build_function_call_nary (tree functi return ret; } -/* Build a function call using a vector of arguments. PARAMS may be - NULL if there are no parameters. This changes the contents of - PARAMS. */ +/* Build a function call using a vector of arguments. + If FUNCTION is the result of resolving an overloaded target built-in, + ORIG_FNDECL is the original function decl, otherwise it is null. + PARAMS may be NULL if there are no parameters. This changes the + contents of PARAMS. */ tree cp_build_function_call_vec (tree function, vec **params, - tsubst_flags_t complain) + tsubst_flags_t complain, tree orig_fndecl) { tree fntype, fndecl; int is_method; @@ -3922,7 +3924,7 @@ cp_build_function_call_vec (tree functio bool warned_p = check_function_arguments (input_location, fndecl, fntype, nargs, argarray, NULL); - ret = build_cxx_call (function, nargs, argarray, complain); + ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl); if (warned_p) {