From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=xry111.site Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=xry111.site DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1678872824; bh=CZ4quNPBMj6IWdseAFEGPkOMqCGU7bBYZ7FyxeO9edU=; h=From:To:Cc:Subject:Date:From; b=QXUulrRmE/N4T/IvqnDMExHuQHWxtZJnpYEhTVrdYCQ1mUxfUJ4YmIKZx4gr1QC5N jEa+EdKP1FLKl25PklTffQoOynJJA1P7wxqcXOlhCJWxOQtd3tU8ZChoPFaKTw+qEJ qSf72SIB0R8Ll9bDfFXThlppaJF54qpNDWWox+VI= 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) From: Xi Ruoyao 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 Content-Transfer-Encoding: 8bit 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 List-Id: 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); -- 2.40.0