From patchwork Wed Mar 15 09:33:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 66402 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 5E049385841C for ; Wed, 15 Mar 2023 09:34:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5E049385841C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678872855; bh=kiWkcxBn3Pt/96TlcHhj+1YtT6C0YtadEgRZic60fYg=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=mWws6CbVOIj/faYmwrYgw64ajNdqhIlLyYr//gf81ntBtTtvBKNgwozos8PUsQc+H x5K65bqbbNP57O34jN5bK/HTF8G+WEQdZbhAI7DXg/m+aNXeFQC47PnivNTROYanA/ w5XaoXJDkk+crQ8hdFSeI+UTYfH9uFxjiZk3u9+M= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from xry111.site (xry111.site [IPv6:2001:470:683e::1]) by sourceware.org (Postfix) with ESMTPS id E805D3858D33 for ; Wed, 15 Mar 2023 09:33:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E805D3858D33 Received: from stargazer.. (unknown [IPv6:240e:358:11bd:3e00:dc73:854d:832e:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id A0B3165A58; Wed, 15 Mar 2023 05:33:41 -0400 (EDT) To: gcc-patches@gcc.gnu.org Cc: Xi Ruoyao Subject: Pushed: [PATCH] builtins: Move the character difference into result instead of reassigning result [PR109086] Date: Wed, 15 Mar 2023 17:33:21 +0800 Message-Id: <20230315093321.555944-1-xry111@xry111.site> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-8.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, LIKELY_SPAM_FROM, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Xi Ruoyao via Gcc-patches From: Xi Ruoyao Reply-To: Xi Ruoyao Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Already approved in bugzilla and bootstrapped on x86_64-linux-gnu. Pushed. expand_simple_binop() is allowed to allocate a new pseudo-register and return it, instead of forcing the result into the provided pseudo-register. This can cause a problem when we expand the unrolled loop for __builtin_strcmp: the compiler always generates code for all n iterations of the loop, so "result" will be an alias of the pseudo-register allocated and used in the last iteration; but at runtime the loop can break early, causing this pseudo-register uninitialized. Emit a move instruction in the iteration to force the difference into one register which has been allocated before the loop, to avoid this issue. gcc/ChangeLog: PR other/109086 * builtins.cc (inline_string_cmp): Force the character difference into "result" pseudo-register, instead of reassign the pseudo-register. --- gcc/builtins.cc | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/gcc/builtins.cc b/gcc/builtins.cc index 305c65c29be..90246e214d6 100644 --- a/gcc/builtins.cc +++ b/gcc/builtins.cc @@ -7142,8 +7142,16 @@ inline_string_cmp (rtx target, tree var_str, const char *const_str, op0 = convert_modes (mode, unit_mode, op0, 1); op1 = convert_modes (mode, unit_mode, op1, 1); - result = expand_simple_binop (mode, MINUS, op0, op1, - result, 1, OPTAB_WIDEN); + rtx diff = expand_simple_binop (mode, MINUS, op0, op1, + result, 1, OPTAB_WIDEN); + + /* Force the difference into result register. We cannot reassign + result here ("result = diff") or we may end up returning + uninitialized result when expand_simple_binop allocates a new + pseudo-register for returning. */ + if (diff != result) + emit_move_insn (result, diff); + if (i < length - 1) emit_cmp_and_jump_insns (result, CONST0_RTX (mode), NE, NULL_RTX, mode, true, ne_label);