From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7856) id 7676E3858D33; Wed, 15 Mar 2023 09:28:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7676E3858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678872535; bh=3QowMt7Gn3CeL0dqwHXnjCqDiZ8myPqADTjv46bczpQ=; h=From:To:Subject:Date:From; b=f6xGk2lcORLf1WsOD+yBihsIU5f9IJdbJxKxxnKby9YBe1YGyV5G5EqR0RY9qKtFJ sydIXLLmQw9qYhxfhMXtgli9bw2LgmY7E+DtMwd4GeuuUIyGd5xUm6GN7DwYTLyVYJ lWvepQk/VDN8plETaVpvgVnLJSCeLa6CpTVEtQjw= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Xi Ruoyao To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-6690] builtins: Move the character difference into result instead of reassigning result [PR109086] X-Act-Checkin: gcc X-Git-Author: Xi Ruoyao X-Git-Refname: refs/heads/master X-Git-Oldrev: 02fcaf412ae9508b75efa9602cd4ac58bc63d6a4 X-Git-Newrev: 45641f3a99281bb0a429649741a29c2aace4c63e Message-Id: <20230315092855.7676E3858D33@sourceware.org> Date: Wed, 15 Mar 2023 09:28:55 +0000 (GMT) List-Id: https://gcc.gnu.org/g:45641f3a99281bb0a429649741a29c2aace4c63e commit r13-6690-g45641f3a99281bb0a429649741a29c2aace4c63e Author: Xi Ruoyao Date: Wed Mar 15 15:34:52 2023 +0800 builtins: Move the character difference into result instead of reassigning result [PR109086] 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. Diff: --- 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);