From patchwork Sun Oct 31 10:12:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Keith Packard X-Patchwork-Id: 46867 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7EBC93858020 for ; Sun, 31 Oct 2021 10:13:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7EBC93858020 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1635675196; bh=DQwO0wV9izowFOa3ipGQVxuO85gr2WqO/QCi13BchEA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=rfpGLNSLKf6Mtx8EZBztR5MirSkAbVM8H7CP5g6cu22FYh+KVnk/fW8wYM4CHl+EC Q0sp/BjA9drICOo9/iZZYVYv/58AZmz48LOPAwh3O7ql6JPpntHwVEGwS2OdG9Npkd Wq5w/gEHU4yAXn3HrJNA6dvVpA76lFXBLhLRU/mM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from elaine.keithp.com (home.keithp.com [63.227.221.253]) by sourceware.org (Postfix) with ESMTPS id 980903858D3C; Sun, 31 Oct 2021 10:12:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 980903858D3C Received: from localhost (localhost [127.0.0.1]) by elaine.keithp.com (Postfix) with ESMTP id EAA4E3F30B17; Sun, 31 Oct 2021 03:12:43 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at keithp.com Received: from elaine.keithp.com ([127.0.0.1]) by localhost (elaine.keithp.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id rNd0BBlES_Zn; Sun, 31 Oct 2021 03:12:43 -0700 (PDT) Received: from keithp.com (koto.keithp.com [192.168.11.2]) by elaine.keithp.com (Postfix) with ESMTPSA id EEB933F30AE5; Sun, 31 Oct 2021 03:12:41 -0700 (PDT) Received: by keithp.com (Postfix, from userid 1000) id 8D89A1E60111; Sun, 31 Oct 2021 03:12:41 -0700 (PDT) To: gcc@gcc.gnu.org Subject: [PATCH] Add -fopt-builtin optimization option Date: Sun, 31 Oct 2021 03:12:38 -0700 Message-Id: <20211031101238.523164-1-keithp@keithp.com> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Keith Packard via Gcc-patches From: Keith Packard Reply-To: Keith Packard Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This option (enabled by default) controls optimizations which convert a sequence of operations into an equivalent sequence that includes calls to builtin functions. Typical cases here are code which matches memcpy, calloc, sincos. The -ftree-loop-distribute-patterns flag only covers converting loops into builtin calls, not numerous other places where knowledge of builtin function semantics changes the generated code. The goal is to allow built-in functions to be declared by the compiler and used directly by the application, but to disable optimizations which create new calls to them, and to allow this optimization behavior to be changed for individual functions by decorating the function definition like this: void attribute((optimize("no-opt-builtin"))) sincos(double x, double *s, double *c) { *s = sin(x); *c = cos(x); } This also avoids converting loops into library calls like this: void * attribute((optimize("no-opt-builtin"))) memcpy(void *__restrict__ dst, const void *__restrict__ src, size_t n) { char *d = dst; const char *s = src; while (n--) *d++ = *s++; return dst; } As well as disabling analysis of memory lifetimes around free as in this example: void * attribute((optimize("no-opt-builtin"))) erase_and_free(void *ptr) { memset(ptr, '\0', malloc_usable_size(ptr)); free(ptr); } Clang has a more sophisticated version of this mechanism which can disable all builtins, or disable a specific builtin: double attribute((no_builtin("exp2"))) exp2(double x) { return pow (2.0, x); } Signed-off-by: Keith Packard --- gcc/builtins.c | 6 ++++++ gcc/common.opt | 4 ++++ gcc/gimple.c | 3 +++ gcc/tree-loop-distribution.c | 2 ++ 4 files changed, 15 insertions(+) diff --git a/gcc/builtins.c b/gcc/builtins.c index 7d0f61fc98b..7aae57deab5 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -1922,6 +1922,9 @@ mathfn_built_in_2 (tree type, combined_fn fn) built_in_function fcodef64x = END_BUILTINS; built_in_function fcodef128x = END_BUILTINS; + if (flag_no_opt_builtin) + return END_BUILTINS; + switch (fn) { #define SEQ_OF_CASE_MATHFN \ @@ -2125,6 +2128,9 @@ mathfn_built_in_type (combined_fn fn) case CFN_BUILT_IN_##MATHFN##L_R: \ return long_double_type_node; + if (flag_no_opt_builtin) + return NULL_TREE; + switch (fn) { SEQ_OF_CASE_MATHFN diff --git a/gcc/common.opt b/gcc/common.opt index eeba1a727f2..d6111cc776a 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -2142,6 +2142,10 @@ fomit-frame-pointer Common Var(flag_omit_frame_pointer) Optimization When possible do not generate stack frames. +fopt-builtin +Common Var(flag_no_opt_builtin, 0) Optimization +Match code sequences equivalent to builtin functions + fopt-info Common Var(flag_opt_info) Optimization Enable all optimization info dumps on stderr. diff --git a/gcc/gimple.c b/gcc/gimple.c index 22dd6417d19..5b82b9409c0 100644 --- a/gcc/gimple.c +++ b/gcc/gimple.c @@ -2790,6 +2790,9 @@ gimple_builtin_call_types_compatible_p (const gimple *stmt, tree fndecl) { gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) != NOT_BUILT_IN); + if (flag_no_opt_builtin) + return false; + tree ret = gimple_call_lhs (stmt); if (ret && !useless_type_conversion_p (TREE_TYPE (ret), diff --git a/gcc/tree-loop-distribution.c b/gcc/tree-loop-distribution.c index 583c01a42d8..43f22a3c7ce 100644 --- a/gcc/tree-loop-distribution.c +++ b/gcc/tree-loop-distribution.c @@ -1859,6 +1859,7 @@ loop_distribution::classify_partition (loop_p loop, /* Perform general partition disqualification for builtins. */ if (volatiles_p + || flag_no_opt_builtin || !flag_tree_loop_distribute_patterns) return has_reduction; @@ -3764,6 +3765,7 @@ loop_distribution::execute (function *fun) /* Don't distribute multiple exit edges loop, or cold loop when not doing pattern detection. */ if (!single_exit (loop) + || flag_no_opt_builtin || (!flag_tree_loop_distribute_patterns && !optimize_loop_for_speed_p (loop))) continue;