From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from bat.birch.relay.mailchannels.net (bat.birch.relay.mailchannels.net [23.83.209.13]) by sourceware.org (Postfix) with ESMTPS id C960C3858405 for ; Mon, 15 Nov 2021 17:34:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C960C3858405 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id A78246818DD; Mon, 15 Nov 2021 17:34:10 +0000 (UTC) Received: from pdx1-sub0-mail-a307.dreamhost.com (100-96-99-57.trex.outbound.svc.cluster.local [100.96.99.57]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 336DA6817C3; Mon, 15 Nov 2021 17:34:10 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a307.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.96.99.57 (trex/6.4.3); Mon, 15 Nov 2021 17:34:10 +0000 X-MC-Relay: Junk X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Whimsical-Squirrel: 307ae37e7b00690d_1636997650454_4232909302 X-MC-Loop-Signature: 1636997650454:2596464877 X-MC-Ingress-Time: 1636997650453 Received: from rhbox.redhat.com (unknown [1.186.223.29]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a307.dreamhost.com (Postfix) with ESMTPSA id 4HtGX05fnVz2P; Mon, 15 Nov 2021 09:34:08 -0800 (PST) From: Siddhesh Poyarekar To: gcc-patches@gcc.gnu.org Subject: [PATCH v2 1/3] gimple-fold: Transform stp*cpy_chk to str*cpy directly Date: Mon, 15 Nov 2021 23:03:13 +0530 Message-Id: <20211115173315.1857598-2-siddhesh@gotplt.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211115173315.1857598-1-siddhesh@gotplt.org> References: <20211111194116.1626980-1-siddhesh@gotplt.org> <20211115173315.1857598-1-siddhesh@gotplt.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, 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-List-Received-Date: Mon, 15 Nov 2021 17:34:15 -0000 Avoid going through another folding cycle and use the ignore flag to directly transform BUILT_IN_STPCPY_CHK to BUILT_IN_STRCPY when set, likewise for BUILT_IN_STPNCPY_CHK to BUILT_IN_STPNCPY. Dump the transformation in dump_file so that we can verify in tests that the direct transformation actually happened. gcc/ChangeLog: * gimple-fold.c (dump_transformation): New function. (gimple_fold_builtin_stxcpy_chk, gimple_fold_builtin_stxncpy_chk): Use it. Simplify to BUILT_IN_STRNCPY if return value is not used. gcc/testsuite/ChangeLog: * gcc.dg/fold-stringops.c: New test. Signed-off-by: Siddhesh Poyarekar --- gcc/gimple-fold.c | 55 ++++++++++++++++--------- gcc/testsuite/gcc.dg/fold-stringops-1.c | 23 +++++++++++ 2 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/fold-stringops-1.c diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c index 6e25a7c05db..2e92efa7f61 100644 --- a/gcc/gimple-fold.c +++ b/gcc/gimple-fold.c @@ -3088,6 +3088,16 @@ gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi, return true; } +/* Print a message in the dump file recording transformation of FROM to TO. */ + +static void +dump_transformation (gcall *from, gcall *to) +{ + if (dump_enabled_p ()) + dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, from, "simplified %T to %T\n", + gimple_call_fn (from), gimple_call_fn (to)); +} + /* Fold a call to the __st[rp]cpy_chk builtin. DEST, SRC, and SIZE are the arguments to the call. IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_* @@ -3100,7 +3110,7 @@ gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi, tree src, tree size, enum built_in_function fcode) { - gimple *stmt = gsi_stmt (*gsi); + gcall *stmt = as_a (gsi_stmt (*gsi)); location_t loc = gimple_location (stmt); bool ignore = gimple_call_lhs (stmt) == NULL_TREE; tree len, fn; @@ -3184,12 +3194,13 @@ gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi, } /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */ - fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK + fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK && !ignore ? BUILT_IN_STPCPY : BUILT_IN_STRCPY); if (!fn) return false; - gimple *repl = gimple_build_call (fn, 2, dest, src); + gcall *repl = gimple_build_call (fn, 2, dest, src); + dump_transformation (stmt, repl); replace_call_with_call_and_fold (gsi, repl); return true; } @@ -3205,23 +3216,10 @@ gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi, tree len, tree size, enum built_in_function fcode) { - gimple *stmt = gsi_stmt (*gsi); + gcall *stmt = as_a (gsi_stmt (*gsi)); bool ignore = gimple_call_lhs (stmt) == NULL_TREE; tree fn; - if (fcode == BUILT_IN_STPNCPY_CHK && ignore) - { - /* If return value of __stpncpy_chk is ignored, - optimize into __strncpy_chk. */ - fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK); - if (fn) - { - gimple *repl = gimple_build_call (fn, 4, dest, src, len, size); - replace_call_with_call_and_fold (gsi, repl); - return true; - } - } - if (! tree_fits_uhwi_p (size)) return false; @@ -3234,7 +3232,23 @@ gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi, For MAXLEN only allow optimizing into non-_ocs function if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen)) - return false; + { + if (fcode == BUILT_IN_STPNCPY_CHK && ignore) + { + /* If return value of __stpncpy_chk is ignored, + optimize into __strncpy_chk. */ + fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK); + if (fn) + { + gimple *repl = gimple_build_call (fn, 4, dest, src, len, + size); + replace_call_with_call_and_fold (gsi, repl); + return true; + } + } + + return false; + } } else maxlen = len; @@ -3244,12 +3258,13 @@ gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi, } /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */ - fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK + fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK && !ignore ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY); if (!fn) return false; - gimple *repl = gimple_build_call (fn, 3, dest, src, len); + gcall *repl = gimple_build_call (fn, 3, dest, src, len); + dump_transformation (stmt, repl); replace_call_with_call_and_fold (gsi, repl); return true; } diff --git a/gcc/testsuite/gcc.dg/fold-stringops-1.c b/gcc/testsuite/gcc.dg/fold-stringops-1.c new file mode 100644 index 00000000000..e26632afd4e --- /dev/null +++ b/gcc/testsuite/gcc.dg/fold-stringops-1.c @@ -0,0 +1,23 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-lower-details" } */ + +char dst[2048]; + +char * +copy1 (const char *src, int cond) +{ + __builtin___stpncpy_chk (dst, src, 42, __builtin_object_size (dst, 0)); + + return dst; +} + +char * +copy2 (void) +{ + __builtin___stpcpy_chk (dst, "Hello world", __builtin_object_size (dst, 0)); + + return dst; +} +/* { dg-final { scan-tree-dump "simplified __builtin___stpncpy_chk to __builtin_strncpy" "lower" } } */ +/* { dg-final { scan-tree-dump "simplified __builtin___stpcpy_chk to __builtin_strcpy" "lower" } } */ + -- 2.31.1