From patchwork Sat Jan 10 13:44:54 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 127818 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 343B24BA2E06 for ; Sat, 10 Jan 2026 13:45:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 343B24BA2E06 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=cXVre/gx; dkim=pass (2048-bit key, unprotected) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.a=rsa-sha256 header.s=google header.b=T5InHL6t X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by sourceware.org (Postfix) with ESMTPS id 792BA4BA2E04 for ; Sat, 10 Jan 2026 13:45:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 792BA4BA2E04 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=oss.qualcomm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oss.qualcomm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 792BA4BA2E04 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768052702; cv=none; b=QZJl40W1ezqtQ9i1xgi4pikFPxC9GcJSRfhSu+ZiA9mwiyYVEdPWD3Z2zp80ZsD9joQ87+ubtJ556DI23SPGoPSuKMHa5zfq0IAvzJGKTIcm60uZHbGZlWVGq+Xoxosd1cwm6CfqkVBGMaU9qD86MBe6P7gCAag+9TNn24+0fW0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768052702; c=relaxed/simple; bh=7jEw3W6UHxqvwy3cu/XFGOy3HO7QTWO8MmXkXW2+EwQ=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=WBdMmOS8xTrKJIMvpxSInnHnY1uDa+2E+VfTh7Af4OZbWzKZpTAEBnuzyJDN9EMgHh9sLMv8Cy+OetkR43rvTZtEBs+YXDFfiI5jRzINcWwpekSS14vwUcBKC7h6Y56rzLdwuZIL5TPHhYX+n1RmzvCN4vEh5KD0UmMGBSLeVv8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 792BA4BA2E04 Received: from pps.filterd (m0279865.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 60A6dXHu3385690 for ; Sat, 10 Jan 2026 13:45:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:date:from:message-id:mime-version :subject:to; s=qcppdkim1; bh=1CP2dYFU+Yo+5UoQf0as5ODN4w7J6cHsjg7 IE74Yftc=; b=cXVre/gxnFmvqaVnFbWg3RC4HQZ82d3EOb54khml6yxZCuhSTSr 4zWR6PITYwdMltXItXnrh28oF4Erdp84Jzfkdfnx0YqgkRulHNsz7qAM5W7N/Ibh H5kix9GOAl7yvoS1D7V432JumJLhZrRTA9EOc0nyu/7TUA8ylvXHugOdWSLJxcVC 705KjiRa+12ia+lBHVC/mWu2D65WHaNU4K82PyAB/O148NznSuOGi5CZUtXvBLdN ViYd7KxT50Ie0a36vWmPwIrA8s5EWr2VtEoUCUlD/76Y+QolptpdgJVoSD8cK01h pNUMwER0wzFaPW/cEvE2m/ePhbqKbRaFHTQ== Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4bkdqqruan-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Sat, 10 Jan 2026 13:45:01 +0000 (GMT) Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-34e5a9de94bso9475146a91.0 for ; Sat, 10 Jan 2026 05:45:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1768052700; x=1768657500; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=1CP2dYFU+Yo+5UoQf0as5ODN4w7J6cHsjg7IE74Yftc=; b=T5InHL6t2/w+2YP3czAk+ivnWC/ENXZI6Z/I4MTZ3Z3pXLonH38SykkCqSRsDKI0/s wn1Qr+CtZRYMHsynLaUKfNh9TJd4iTaydQN9UawimHSZ5NjRQX2jDO44+Qt01mpMYjqM QRnSouLlGnXvOqJ17EFSW4/dHtgNLiiJZG9pzsXKY7pMxhfcl5Alj1YTwKbdTSIBkwjC 8/DhX+5lEoT2NBJjyOO+qjcd4v1EX72OM0lP2rk3xgoM3nZSWgjQTTdCy/ybSkI1KMqd 9ev8EZES/e6zu1x43dURWmvNDKEXrHEsla91KVfKl82oaEqqLPp5PiG6BTE4UyizqNks hgqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768052700; x=1768657500; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=1CP2dYFU+Yo+5UoQf0as5ODN4w7J6cHsjg7IE74Yftc=; b=LG8JR9qO0eKs+cTSjbSAn+mJO7P6dcJvqr1lP99I/s3QSpHQcVjJwEPySS+dPE1ln6 N6JlU81d/vYQFWPv/FQHsnC2Rj7Fy67In3929Rgg26r6BisNPCN3S1v4lrLs6DZf11BT 4DLWTdtwELIiCWQOIKtiqchsCwRZi4VTMPtTMG6b+l6wcxj8DHEfwR0ybnfhsL8HW2wp B/hy2dFlGl+21XQKYnSIDSNj4EENUz6LEcq1tSmvwBGSrRlsBDn8P3o6pWQzUSkRp3N6 Jd048s8QKLWwdn4nffHLTAvnDneCoOIaCCgSltiPcPRXIQ0HAsVP33SmhBclMxoNHpnC o7vg== X-Gm-Message-State: AOJu0YwYEZZzPy8DCFiJ6SYbkHKYTpwkGCwkoFbo2biZp2EprPpGs/Au tZbhxEaSA8J/8W9lFO8KS64UTdGvNqo0YVIZZatcUn7xDaXIaoLKftjuwZlVP3dwiqQY+iKaBA/ TkZINDcq3apLXrEL7J98gOg/lkAuR16yuBZTTdENJY2dzczEbxQQQ6mgaMHnEg8SmgyUv X-Gm-Gg: AY/fxX7+68TNm/Udvv4NNIIBfI0zfsAEisrZ76bp9pxnD3usIYYAIt+jlB8AhsaEJn0 JXcg1luQEj7L2/KyAZnRif7Yk/F2t9E/7jZruCmqebkaCIt9XLyKchpJBhYi7Lx1dhFNPBCo+RE NYoLWwDsigiClZy/Jx3DpfoagkXhmDNnclFVQCikQplDqkiyBqT9Wr2mFVg0jBkZx7Ed3fqUx1G 0vbKzQeJgkA7hlfhXwrrrjqrxhB+qNxPHCZMO/gbsgP2I2Pg0Uj+GozVGir1e10kFNJilMoHc+T jxgjzhUj2wHaRZ2qoZUagNaB//ZN/ysY7XqJyV0uX6oLze8z1VFKAfZSqySyphPxm3zT0P2ZwA3 zcwMSnXEqr2HnrdaRWmcWQXZ7cpjlWDPe5qb2 X-Received: by 2002:a05:6a21:9999:b0:35e:6a5b:cbc9 with SMTP id adf61e73a8af0-3898f9904f5mr11915319637.50.1768052700279; Sat, 10 Jan 2026 05:45:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IHBNMMO4hM0Q53vyV09m2hhQcuCCGll6yemoE48DYe0HhzlqTGnng9BYzDajy2IL2GhlyXdEA== X-Received: by 2002:a05:6a21:9999:b0:35e:6a5b:cbc9 with SMTP id adf61e73a8af0-3898f9904f5mr11915304637.50.1768052699708; Sat, 10 Jan 2026 05:44:59 -0800 (PST) Received: from xeond2.wrightpinski.org ([98.97.34.185]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a3e3cb2f8asm131524625ad.57.2026.01.10.05.44.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Jan 2026 05:44:59 -0800 (PST) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCH] ifcvt: Improve `cmp?a&b:a` to try with -1 [PR123312] Date: Sat, 10 Jan 2026 05:44:54 -0800 Message-ID: <20260110134454.1224071-1-andrew.pinski@oss.qualcomm.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: 9DvhXJb0TPEVydmwmPY068V4uDMZkiN0 X-Proofpoint-GUID: 9DvhXJb0TPEVydmwmPY068V4uDMZkiN0 X-Authority-Analysis: v=2.4 cv=a9M9NESF c=1 sm=1 tr=0 ts=696257dd cx=c_pps a=RP+M6JBNLl+fLTcSJhASfg==:117 a=2uFKf0rdkymy/JvssHMwyA==:17 a=vUbySO9Y5rIA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=EUspDBNiAAAA:8 a=gyFGeE1oXrISqdU7EOAA:9 a=iS9zxrgQBfv6-_F4QbHw:22 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMTEwMDExOSBTYWx0ZWRfX1/C6ZHAZ6QBc O0ie6Z7jRg5kkqKy/AHXlQ7O+pB1qS62HeUg1jz3jPAbFWKflVX7zyPdn/hmeT7Hjc1RncCTQIw dgUnyweaLVhfQ2PY5Aa73mLrrO+Ysyy4zuu/DHoaEd/DRXZSfQHvER48OIIZON3Aro8fkKzUUT/ z6Y5gLeGg/ZVVFYW1ZUKwLHr5xr89TyX90JsXonw6QcbyX+dmAPogL7a/UfGJ2Hplg6LA2Wvf6g 26RS3giN02VNpY6EmD6dSUPxuIhm9RPHcjeVDb3bX4Eyr2AN8M9BHeAhZZ611AIvROMr43mwifY qxEy55egC8W/6l8plRjUis2PEx0kmj4he8b9LYVUELtgu4sNhftomsRWKK7whGYHOLIRy0lTnYE zbYzJzFfK3P2iWUKAeDJ0QjQF2df9U+hUdmlDOsfx6DiPWaRtL7yqbxSaF8OR8G9YErwZJEvL53 1jQUOEjQ9NPNc/p/fFw== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2026-01-10_04,2026-01-09_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 priorityscore=1501 suspectscore=0 impostorscore=0 adultscore=0 phishscore=0 clxscore=1015 lowpriorityscore=0 malwarescore=0 spamscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2512120000 definitions=main-2601100119 X-Spam-Status: No, score=-16.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org After the current improvements to ifcvt, on some targets for cmp?a&b:a it is better to produce `(cmp?b:-1) & a` rather than `(!cmp?a:0)|(a & b)`. So this extends noce_try_cond_zero_arith (with a rename to noce_try_cond_arith) to see if `cmp ? a : -1` is cheaper than `!cmp?a:0`. Bootstrapped and tested on x86_64-linux-gnu. gcc/ChangeLog: * ifcvt.cc (noce_try_cond_zero_arith): Rename to ... (noce_try_cond_arith): This. For AND try `cmp ? a : -1` also to see which one cost less. (noce_process_if_block): Handle the rename. Signed-off-by: Andrew Pinski --- gcc/ifcvt.cc | 70 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 12 deletions(-) diff --git a/gcc/ifcvt.cc b/gcc/ifcvt.cc index c029a583e84..0858b3d2f6d 100644 --- a/gcc/ifcvt.cc +++ b/gcc/ifcvt.cc @@ -790,7 +790,6 @@ static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx_insn **); static bool noce_try_minmax (struct noce_if_info *); static bool noce_try_abs (struct noce_if_info *); static bool noce_try_sign_mask (struct noce_if_info *); -static int noce_try_cond_zero_arith (struct noce_if_info *); /* Return the comparison code for reversed condition for IF_INFO, or UNKNOWN if reversing the condition is not possible. */ @@ -3135,10 +3134,15 @@ get_base_reg (rtx exp) tmp = !cond ? y : 0 x = (y & z) | tmp + Also for AND try: + tmp = cond ? z : -1 + x = y op tmp + To see if it is cheaper to produce `!cond ? y : 0` + or `cond ? z : -1`. */ static int -noce_try_cond_zero_arith (struct noce_if_info *if_info) +noce_try_cond_arith (struct noce_if_info *if_info) { rtx target, a, b, a_op0, a_op1; rtx cond = if_info->cond; @@ -3230,27 +3234,69 @@ noce_try_cond_zero_arith (struct noce_if_info *if_info) target = rtl_hooks.gen_lowpart_no_emit (GET_MODE (XEXP (a, op != AND)), target); gcc_assert (target); } - if (!target) - goto end_seq_n_fail; + /* For AND, try `cond ? z : -1` to see if that is cheaper or the same cost. + In some cases it will be cheaper to produce the -1 rather than the 0 case. */ if (op == AND) { + rtx_insn *seq0 = end_sequence (); + unsigned cost0 = seq_cost (seq0, if_info->speed_p); + if (!target) + cost0 = -1u; + + /* Produce `cond ? z : -1`. */ + rtx targetm1; + start_sequence (); + targetm1 = gen_reg_rtx (GET_MODE (XEXP (a, 1))); + targetm1 = noce_emit_cmove (if_info, targetm1, code, + XEXP (cond, 0), XEXP (cond, 1), + XEXP (a, 1), constm1_rtx); + rtx_insn *seqm1 = end_sequence (); + unsigned costm1 = seq_cost (seqm1, if_info->speed_p); + if (!targetm1) + costm1 = -1u; + /* If both fails, then there is no costing to be done. */ + if (!targetm1 && !target) + return false; + + /* If -1 is cheaper or the same cost to producing 0, then use that. */ + if (costm1 <= cost0) + { + push_to_sequence (seqm1); + targetm1 = expand_simple_binop (mode, op, a_op0, targetm1, + if_info->x, 0, OPTAB_WIDEN); + if (targetm1) + { + target = targetm1; + goto success; + } + end_sequence (); + } + if (!target) + return false; + /* For 0 the produce sequence is: + tmp = !cond ? y : 0 + x = (y & z) | tmp */ + push_to_sequence (seq0); rtx a_bin = gen_reg_rtx (mode); noce_emit_move_insn (a_bin, a); target = expand_simple_binop (mode, IOR, a_bin, target, if_info->x, 0, OPTAB_WIDEN); - - } - else - { - target = expand_simple_binop (mode, op, a_op0, target, if_info->x, 0, - OPTAB_WIDEN); + if (!target) + goto end_seq_n_fail; + goto success; } + if (!target) + goto end_seq_n_fail; + + target = expand_simple_binop (mode, op, a_op0, target, if_info->x, 0, + OPTAB_WIDEN); if (!target) goto end_seq_n_fail; +success: if (target != if_info->x) noce_emit_move_insn (if_info->x, target); @@ -3259,7 +3305,7 @@ noce_try_cond_zero_arith (struct noce_if_info *if_info) goto fail; emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a)); - if_info->transform_name = "noce_try_cond_zero_arith"; + if_info->transform_name = "noce_try_cond_arith"; return true; end_seq_n_fail: @@ -4431,7 +4477,7 @@ noce_process_if_block (struct noce_if_info *if_info) if (noce_try_store_flag_mask (if_info)) goto success; if (HAVE_conditional_move - && noce_try_cond_zero_arith (if_info)) + && noce_try_cond_arith (if_info)) goto success; if (HAVE_conditional_move && noce_try_cmove_arith (if_info))