| Message ID | 20261003081313.1609669-1-andrew.pinski@oss.qualcomm.com |
|---|---|
| State | New |
| Headers |
Return-Path: <gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 104CC4BA9001 for <patchwork@sourceware.org>; Sat, 3 Oct 2026 08:14:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 104CC4BA9001 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=N7TozTmx; 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=OZCnAV6m 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 746CA4B9DB46 for <gcc-patches@gcc.gnu.org>; Sat, 3 Oct 2026 08:13:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 746CA4B9DB46 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 746CA4B9DB46 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1791015199; cv=none; b=NUJsCVUpzBAGOuFIVJMaZEBfwGbyWKgD/J86JgmpsnZ+NgJ6gCf9qd9yE9PZhPU9WDmVNNVIG5E3YH1ChPgMDlVasMhhH8Wy2RgznIzVtWXiFeOhCqjsg6e35aVMRdlJMV2d1cFNxc1LBGPedhtdiGUQ8QXiiWUbsuFWz9vqJt0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1791015199; c=relaxed/simple; bh=mkGG4AUnZF3+nh5zRwYmkB/o7kxPwHtPSTQ0z8mJq+0=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=TWmG0s3Oy10Zofb7qeBsCboXxGreFrdv8DtXWchm+mLJV7npa7BPAk8h5+eCUqTZm716hnG0uo0uyEeo2Oh5cBVEYeJnspZG9DyElMTKz0zJl40u57RxMnOZnckLVcD57XUYYDSqrH5r6lh055/7xnhJeH8E5j/Fj89HP3brXpk= ARC-Authentication-Results: i=1; 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=N7TozTmx; 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=OZCnAV6m DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 746CA4B9DB46 Received: from pps.filterd (m0279866.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6934moC22726650 for <gcc-patches@gcc.gnu.org>; Sat, 3 Oct 2026 08:13:18 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=+Xs5vso2cQHF5wqDXw2ueqqMZpbdliLk6qK hEib3onE=; b=N7TozTmxeZACcxaABB4E4hwkXDNkSQ2c8MOuM8BkwMvPEYo7u8h MN7KlY67bV0VWEhLJFXopXvAyKtCJQXOlQ5LBxoeVtz+QEh56eRRTfQAeNqVHcvm TsPDIgfMNYJsQJnZQyiJ6Tvstsk+YyxpQtKf0baVX0QqdyfgLj1QDYWeTERMKXcu BKI3DtSiisuwz76dtZcDF+CUsPjxNOBeQ6YJt24duO0tbUaQeP6MkbjWqPr4RGpj EsjsC4xv8v0YKnWEytXgLVyKQJsioqHJyYNtr3R/ewxjpJWosi/bUWMXBLE8+a7H NhWMdoYLEghgOzh1E06cNUQJW/iyjVXnrGA== Received: from mail-dy1-f198.google.com (mail-dy1-f198.google.com [74.125.82.198]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4h2u08gam5-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for <gcc-patches@gcc.gnu.org>; Sat, 03 Oct 2026 08:13:17 +0000 (GMT) Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-3510a481ae9so227318eec.1 for <gcc-patches@gcc.gnu.org>; Sat, 03 Oct 2026 01:13:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1791015197; x=1791619997; 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:content-type; bh=+Xs5vso2cQHF5wqDXw2ueqqMZpbdliLk6qKhEib3onE=; b=OZCnAV6mygnQDpuZKURP4rVHmYH64eBepbYQRs8WReBHbB4zfNGiCJuOx7qRela070 YzfyOFdZw1sFqqg1U9x+vvkJIZueK0ek5inCdhrxDRJKgDHWIRleShFNDPgH7C8NN7XS 9K0WB2e3iVmlZQ7073h4TF86DEhDu/ALzkB6knaTiXTGPf5YWloOGbjpr2gJQOP9I5Md kzZcKmRP+Hljt8kx067VMw8ThIC+ct95Sb0zN+rhIqTuqpblSKA04lZQnyU5ToF3sOzW I8xHKwGPku4j4xr5q4ghSe6KoLUMlGWd4Wp5V88pgSKHmQdOrrdChN/ucBkpJxDzhfCU r9rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791015197; x=1791619997; 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:content-type; bh=+Xs5vso2cQHF5wqDXw2ueqqMZpbdliLk6qKhEib3onE=; b=zPWfYkR8eCcFuXU8/23AARkZeTNVsfX2U12xP+554Cxn/D8MU2AsBJ4o8hrioJUEFI Z4u6uoaIlbTODAMdEbTW0/E/vXbTiE7Ik/W9YzxRj161GKQYJwTJ+F+kJI+jcwLSMmS7 zbll1UhemM7oPI4GWQ3OCPReCtWA8yzVuAaaxsHveYhCdeT0zPYPWiTW4yAct8MN2H2m VoSGLtTCyPPwORj/tOkLVGXvS1CH9kWdtdqKLrtjWJGH5Q7Ws2ddH+siMAxPKcft+WT9 W/rmEhcJk+cgMUrZODZoh/G24P/ihKaN4p5/J43XYqivB8wGFDwoY/p3+iqyMe8EyG2J 9C2A== X-Gm-Message-State: AFq9FYIxZYQX7sfccqmcPPd4d4q/g5EoYVNwlN7HVtaMk9thGGwkwUtx d/1QnAnTemaFDtToLBJKbWlP6994qUBpVp5y8q7AgYD5RoWcNe7SAhbaTBUmjKxQArUzyhNWgQv bR5vtflaWomtrK4KLlTcq9ifLjwVy+oS4YgptJ8ukRKxQouYl6rKyajwUAvtjNLk1irbg X-Gm-Gg: AYBFou3/ZQf9gpuqMTSf0wSDdUXZsRI8G12IgNcuKh99ehgcub/BkPmIaJuiz/I1sBf xwS9hf7Wd1H5hyyeDBTyXiQBpZbS5vdbgsH+cdAmS7FryRLZr3tLKbmPqmzTB568eagT0NnYlbU GlohmK2xlBQul23gEL3cH6rvGT77vf76KRT8Ep6aiuey3z/xedQglxuu81lAPzmMJoWxrc88Nf8 4Pxd00u7C3SBx70IYEbPxLJiCbOwfOSsyzE+HuZySdj9KUsZkKRKsDpbYNBPAMhhRAM/0zFXb+1 JkNfymOreuOj+yXV5jWvrpjNgHW5G/5aCUtTeWE62qpzbka675vM8yfXGu+cbfODTNorG5SkiDq q5OCaKnqpTh6r+0NO/0eNheVVwDCW4YN7M/Aizy7bUYSKWvg3kxOqhQ== X-Received: by 2002:a05:7300:d084:b0:33b:e264:474a with SMTP id 5a478bee46e88-34f2185cb3dmr5225807eec.31.1791015197008; Sat, 03 Oct 2026 01:13:17 -0700 (PDT) X-Received: by 2002:a05:7300:d084:b0:33b:e264:474a with SMTP id 5a478bee46e88-34f2185cb3dmr5225781eec.31.1791015196411; Sat, 03 Oct 2026 01:13:16 -0700 (PDT) Received: from hu-apinski-lv.qualcomm.com (Global_NAT1.qualcomm.com. [129.46.96.20]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-35105835bf8sm11010664eec.9.2026.10.03.01.13.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Oct 2026 01:13:15 -0700 (PDT) From: Andrea Pinski <andrew.pinski@oss.qualcomm.com> To: gcc-patches@gcc.gnu.org Cc: Andrea Pinski <andrew.pinski@oss.qualcomm.com> Subject: [PATCH] match: Fix `(convert)abs((convert)a)` pattern for integer overflow [PR127689] Date: Sat, 3 Oct 2026 01:13:13 -0700 Message-Id: <20261003081313.1609669-1-andrew.pinski@oss.qualcomm.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-ORIG-GUID: jdH2vocjXCCc3Xm-jWLVDT0N46QA5KnW X-Proofpoint-GUID: jdH2vocjXCCc3Xm-jWLVDT0N46QA5KnW X-Authority-Analysis: v=2.4 cv=JqlkP84C c=1 sm=1 tr=0 ts=6ac0b91d cx=c_pps a=wEP8DlPgTf/vqF+yE6f9lg==:117 a=ouPCqIW2jiPt+lZRy3xVPw==:17 a=660iZSQnnn4A:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=YMgV9FUhrdKAYTUUvYB2:22 a=EUspDBNiAAAA:8 a=vRVyW_Bjp52Pf59SmcwA:9 a=bBxd6f-gb0O0v-kibOvt:22 X-Proofpoint-Spam-Info: AW1haW4tMjYxMDAzMDAzMiBTYWx0ZWRfX0Y1pXsl8fncs umrWmIIkXiXb8cxKVcuNFX2N2kS1c0mc/xEM+0wmFy4W7k5rmk+iWC8jEQQg8IMpdRV5Um4nxVb 4TTYUTwkPHoajeT30LF2L8IuJZAXdQ4= X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMDAzMDAzMiBTYWx0ZWRfXx29/BnUFbigR 9IE1DYgtBsiUvQtKoZ7bbnoHXGa5BXfsC4q63mdPIDstolboAT7H5VzALr9yysnb2ha1Nje/Wxp pQ1P67dbQ1BxxCXAjsTI/bpT5A6xdVi3kgupieiPZe3vOsPMMqzbq7NHEmLsDjhkLdefpFUjyBN o+v+1YGeZWD3fRDOWp+E9Uat2KilDiEHFERjEM1jZL+blAygQRt2PdLgvJTwkp0T0I/8/BTCxK5 qgKn6HEWsRn59Q1BUowGS/cgREu2Siw2X/gyuPMmJL9aqYZ76mam3ty4rH62Ioh47+T58AZcZLJ a7hq0c4D2ypY/isCz1ir2bIByp2L4xD5iJwp26Qa9Kr6wD03DO7g/pNKiTcK3mUO8rjcsW+cF+3 2Im4z90X5BTk3uHeTeCj8X8daoD03wFreXJh8ToNlB3hgzmPpYTXGdrA2AFrROH7WJWlbaI+TWM dTD1gJ/j4S7tCl5Ug9A== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-10-02_07,2026-10-02_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 malwarescore=0 phishscore=0 suspectscore=0 lowpriorityscore=0 impostorscore=0 priorityscore=1501 adultscore=0 clxscore=1015 spamscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2609040000 definitions=main-2610030032 X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org |
| Series |
match: Fix `(convert)abs((convert)a)` pattern for integer overflow [PR127689]
|
|
Commit Message
Andrea Pinski
Oct. 3, 2026, 8:13 a.m. UTC
There are 2 patterns that are similar in match.pd for changing into
a smaller abs. One handles `abs((ext)a)` and one for `(trunc)abs((ext)a)`.
The one with the truncate afterwards does not use absu for integer types
which can introduce undefined behavior in some cases.
So let's restrict the second one to non integral types as the first one
will handle those always; the check for casting from unsigned is another
difference but the casting from unsigned is handled by a different abs
pattern for non-negative values.
Bootstrapped and tested on x86_64-linux-gnu.
PR tree-optimization/127689
gcc/ChangeLog:
* match.pd (`(trunc)abs((ext)a)`): Move right
after `abs((ext)a)` pattern. Restrict to
non integral types.
gcc/testsuite/ChangeLog:
* gcc.dg/torture/pr127689-1.c: New test.
Signed-off-by: Andrea Pinski <andrew.pinski@oss.qualcomm.com>
---
gcc/match.pd | 22 +++++++++---------
gcc/testsuite/gcc.dg/torture/pr127689-1.c | 28 +++++++++++++++++++++++
2 files changed, 39 insertions(+), 11 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/torture/pr127689-1.c
Comments
> Am 03.10.2026 um 10:15 schrieb Andrea Pinski <andrew.pinski@oss.qualcomm.com>: > > There are 2 patterns that are similar in match.pd for changing into > a smaller abs. One handles `abs((ext)a)` and one for `(trunc)abs((ext)a)`. > The one with the truncate afterwards does not use absu for integer types > which can introduce undefined behavior in some cases. > So let's restrict the second one to non integral types as the first one > will handle those always; the check for casting from unsigned is another > difference but the casting from unsigned is handled by a different abs > pattern for non-negative values. > > Bootstrapped and tested on x86_64-linux-gnu. Ok Richard > PR tree-optimization/127689 > > gcc/ChangeLog: > > * match.pd (`(trunc)abs((ext)a)`): Move right > after `abs((ext)a)` pattern. Restrict to > non integral types. > > gcc/testsuite/ChangeLog: > > * gcc.dg/torture/pr127689-1.c: New test. > > Signed-off-by: Andrea Pinski <andrew.pinski@oss.qualcomm.com> > --- > gcc/match.pd | 22 +++++++++--------- > gcc/testsuite/gcc.dg/torture/pr127689-1.c | 28 +++++++++++++++++++++++ > 2 files changed, 39 insertions(+), 11 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/torture/pr127689-1.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 2d1161b860b..648bda9bf11 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -214,6 +214,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } > (convert (absu:utype @0))))) > > +/* (trunc)abs (extend x) --> abs (x) for non-integral types. */ > +(simplify > + (convert (abs (convert@1 @0))) > + (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) > + && !HONOR_SNANS (@1) > + && types_match (type, TREE_TYPE (@0)) > + && element_precision (type) < element_precision (TREE_TYPE (@1)) > + && (!VECTOR_TYPE_P (type) > + || target_supports_op_p (type, ABS_EXPR, optab_default))) > + (abs @0))) > + > #if GIMPLE > /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */ > (simplify > @@ -10340,17 +10351,6 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > (IFN_COPYSIGN @0 { build_minus_one_cst (type); }) > (IFN_COPYSIGN @0 { build_one_cst (type); }))))) > > -/* (trunc)abs (extend x) --> abs (x) */ > -(simplify > - (convert (abs (convert@1 @0))) > - (if (optimize > - && !HONOR_SNANS (@1) > - && types_match (type, TREE_TYPE (@0)) > - && element_precision (type) < element_precision (TREE_TYPE (@1)) > - && (!VECTOR_TYPE_P (type) > - || target_supports_op_p (type, ABS_EXPR, optab_default))) > - (abs @0))) > - > (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL) > tos (IFN_FMA IFN_FMA IFN_FMA) > (simplify > diff --git a/gcc/testsuite/gcc.dg/torture/pr127689-1.c b/gcc/testsuite/gcc.dg/torture/pr127689-1.c > new file mode 100644 > index 00000000000..cc169744bf2 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/torture/pr127689-1.c > @@ -0,0 +1,28 @@ > +/* { dg-do run } */ > +/* { dg-additional-options "-fno-tree-forwprop" } */ > +/* PR tree-optimization/127689 */ > + > + > +__attribute__((noipa))void check(int v) > +{ > + if (v != -4) > + __builtin_exit(1); > +} > +int p; > +signed char s, v; > +signed char t = 0x80; > +__attribute__((noipa)) > +void y(signed char w) { > + int t1; > + t1 = __builtin_abs(t1); //256, 0x80 > + p = t1; > + s = p; > + signed char t2 = t1; // -256, 0x80 > + t = t2 % 3; // -2 > + v = 2 * t; // -4 > + check(v); > +} > +int main() > +{ > + y(0x80); > +} > -- > 2.34.1 >
On Sat, Oct 3, 2026 at 1:13 AM Andrea Pinski <andrew.pinski@oss.qualcomm.com> wrote: > > There are 2 patterns that are similar in match.pd for changing into > a smaller abs. One handles `abs((ext)a)` and one for `(trunc)abs((ext)a)`. > The one with the truncate afterwards does not use absu for integer types > which can introduce undefined behavior in some cases. > So let's restrict the second one to non integral types as the first one > will handle those always; the check for casting from unsigned is another > difference but the casting from unsigned is handled by a different abs > pattern for non-negative values. > > Bootstrapped and tested on x86_64-linux-gnu. > > PR tree-optimization/127689 > > gcc/ChangeLog: > > * match.pd (`(trunc)abs((ext)a)`): Move right > after `abs((ext)a)` pattern. Restrict to > non integral types. > > gcc/testsuite/ChangeLog: > > * gcc.dg/torture/pr127689-1.c: New test. > > Signed-off-by: Andrea Pinski <andrew.pinski@oss.qualcomm.com> > --- > gcc/match.pd | 22 +++++++++--------- > gcc/testsuite/gcc.dg/torture/pr127689-1.c | 28 +++++++++++++++++++++++ > 2 files changed, 39 insertions(+), 11 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/torture/pr127689-1.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 2d1161b860b..648bda9bf11 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -214,6 +214,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } > (convert (absu:utype @0))))) > > +/* (trunc)abs (extend x) --> abs (x) for non-integral types. */ > +(simplify > + (convert (abs (convert@1 @0))) > + (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) > + && !HONOR_SNANS (@1) > + && types_match (type, TREE_TYPE (@0)) > + && element_precision (type) < element_precision (TREE_TYPE (@1)) > + && (!VECTOR_TYPE_P (type) > + || target_supports_op_p (type, ABS_EXPR, optab_default))) > + (abs @0))) > + > #if GIMPLE > /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */ > (simplify > @@ -10340,17 +10351,6 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > (IFN_COPYSIGN @0 { build_minus_one_cst (type); }) > (IFN_COPYSIGN @0 { build_one_cst (type); }))))) > > -/* (trunc)abs (extend x) --> abs (x) */ > -(simplify > - (convert (abs (convert@1 @0))) > - (if (optimize > - && !HONOR_SNANS (@1) > - && types_match (type, TREE_TYPE (@0)) > - && element_precision (type) < element_precision (TREE_TYPE (@1)) > - && (!VECTOR_TYPE_P (type) > - || target_supports_op_p (type, ABS_EXPR, optab_default))) > - (abs @0))) > - > (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL) > tos (IFN_FMA IFN_FMA IFN_FMA) > (simplify > diff --git a/gcc/testsuite/gcc.dg/torture/pr127689-1.c b/gcc/testsuite/gcc.dg/torture/pr127689-1.c > new file mode 100644 > index 00000000000..cc169744bf2 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/torture/pr127689-1.c > @@ -0,0 +1,28 @@ > +/* { dg-do run } */ > +/* { dg-additional-options "-fno-tree-forwprop" } */ > +/* PR tree-optimization/127689 */ > + > + > +__attribute__((noipa))void check(int v) > +{ > + if (v != -4) > + __builtin_exit(1); > +} > +int p; > +signed char s, v; > +signed char t = 0x80; > +__attribute__((noipa)) > +void y(signed char w) { > + int t1; Pushed with a small update to the testcase (which I missed when I was moving from godbolt to the testsuite; and then forgot to test the testcase until right before pushing the commit). Thanks, Andrea > + t1 = __builtin_abs(t1); //256, 0x80 > + p = t1; > + s = p; > + signed char t2 = t1; // -256, 0x80 > + t = t2 % 3; // -2 > + v = 2 * t; // -4 > + check(v); > +} > +int main() > +{ > + y(0x80); > +} > -- > 2.34.1 >
On Sat, Oct 3, 2026 at 4:27 AM Andrea Pinski <andrew.pinski@oss.qualcomm.com> wrote: > > On Sat, Oct 3, 2026 at 1:13 AM Andrea Pinski > <andrew.pinski@oss.qualcomm.com> wrote: > > > > There are 2 patterns that are similar in match.pd for changing into > > a smaller abs. One handles `abs((ext)a)` and one for `(trunc)abs((ext)a)`. > > The one with the truncate afterwards does not use absu for integer types > > which can introduce undefined behavior in some cases. > > So let's restrict the second one to non integral types as the first one > > will handle those always; the check for casting from unsigned is another > > difference but the casting from unsigned is handled by a different abs > > pattern for non-negative values. > > > > Bootstrapped and tested on x86_64-linux-gnu. > > > > PR tree-optimization/127689 > > > > gcc/ChangeLog: > > > > * match.pd (`(trunc)abs((ext)a)`): Move right > > after `abs((ext)a)` pattern. Restrict to > > non integral types. > > > > gcc/testsuite/ChangeLog: > > > > * gcc.dg/torture/pr127689-1.c: New test. > > > > Signed-off-by: Andrea Pinski <andrew.pinski@oss.qualcomm.com> > > --- > > gcc/match.pd | 22 +++++++++--------- > > gcc/testsuite/gcc.dg/torture/pr127689-1.c | 28 +++++++++++++++++++++++ > > 2 files changed, 39 insertions(+), 11 deletions(-) > > create mode 100644 gcc/testsuite/gcc.dg/torture/pr127689-1.c > > > > diff --git a/gcc/match.pd b/gcc/match.pd > > index 2d1161b860b..648bda9bf11 100644 > > --- a/gcc/match.pd > > +++ b/gcc/match.pd > > @@ -214,6 +214,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > > (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } > > (convert (absu:utype @0))))) > > > > +/* (trunc)abs (extend x) --> abs (x) for non-integral types. */ > > +(simplify > > + (convert (abs (convert@1 @0))) > > + (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) > > + && !HONOR_SNANS (@1) > > + && types_match (type, TREE_TYPE (@0)) > > + && element_precision (type) < element_precision (TREE_TYPE (@1)) > > + && (!VECTOR_TYPE_P (type) > > + || target_supports_op_p (type, ABS_EXPR, optab_default))) > > + (abs @0))) > > + > > #if GIMPLE > > /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */ > > (simplify > > @@ -10340,17 +10351,6 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > > (IFN_COPYSIGN @0 { build_minus_one_cst (type); }) > > (IFN_COPYSIGN @0 { build_one_cst (type); }))))) > > > > -/* (trunc)abs (extend x) --> abs (x) */ > > -(simplify > > - (convert (abs (convert@1 @0))) > > - (if (optimize > > - && !HONOR_SNANS (@1) > > - && types_match (type, TREE_TYPE (@0)) > > - && element_precision (type) < element_precision (TREE_TYPE (@1)) > > - && (!VECTOR_TYPE_P (type) > > - || target_supports_op_p (type, ABS_EXPR, optab_default))) > > - (abs @0))) > > - > > (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL) > > tos (IFN_FMA IFN_FMA IFN_FMA) > > (simplify > > diff --git a/gcc/testsuite/gcc.dg/torture/pr127689-1.c b/gcc/testsuite/gcc.dg/torture/pr127689-1.c > > new file mode 100644 > > index 00000000000..cc169744bf2 > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/torture/pr127689-1.c > > @@ -0,0 +1,28 @@ > > +/* { dg-do run } */ > > +/* { dg-additional-options "-fno-tree-forwprop" } */ > > +/* PR tree-optimization/127689 */ > > + > > + > > +__attribute__((noipa))void check(int v) > > +{ > > + if (v != -4) > > + __builtin_exit(1); > > +} > > +int p; > > +signed char s, v; > > +signed char t = 0x80; > > +__attribute__((noipa)) > > +void y(signed char w) { > > + int t1; > > Pushed with a small update to the testcase (which I missed when I was > moving from godbolt to the testsuite; and then forgot to test the > testcase until right before pushing the commit). Oh I forgot to say what the change was. Initialize t1 to w. That is: int t1; replaced with: int t1 = w; > > Thanks, > Andrea > > > + t1 = __builtin_abs(t1); //256, 0x80 > > + p = t1; > > + s = p; > > + signed char t2 = t1; // -256, 0x80 > > + t = t2 % 3; // -2 > > + v = 2 * t; // -4 > > + check(v); > > +} > > +int main() > > +{ > > + y(0x80); > > +} > > -- > > 2.34.1 > >
diff --git a/gcc/match.pd b/gcc/match.pd index 2d1161b860b..648bda9bf11 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -214,6 +214,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } (convert (absu:utype @0))))) +/* (trunc)abs (extend x) --> abs (x) for non-integral types. */ +(simplify + (convert (abs (convert@1 @0))) + (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && !HONOR_SNANS (@1) + && types_match (type, TREE_TYPE (@0)) + && element_precision (type) < element_precision (TREE_TYPE (@1)) + && (!VECTOR_TYPE_P (type) + || target_supports_op_p (type, ABS_EXPR, optab_default))) + (abs @0))) + #if GIMPLE /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */ (simplify @@ -10340,17 +10351,6 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (IFN_COPYSIGN @0 { build_minus_one_cst (type); }) (IFN_COPYSIGN @0 { build_one_cst (type); }))))) -/* (trunc)abs (extend x) --> abs (x) */ -(simplify - (convert (abs (convert@1 @0))) - (if (optimize - && !HONOR_SNANS (@1) - && types_match (type, TREE_TYPE (@0)) - && element_precision (type) < element_precision (TREE_TYPE (@1)) - && (!VECTOR_TYPE_P (type) - || target_supports_op_p (type, ABS_EXPR, optab_default))) - (abs @0))) - (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL) tos (IFN_FMA IFN_FMA IFN_FMA) (simplify diff --git a/gcc/testsuite/gcc.dg/torture/pr127689-1.c b/gcc/testsuite/gcc.dg/torture/pr127689-1.c new file mode 100644 index 00000000000..cc169744bf2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr127689-1.c @@ -0,0 +1,28 @@ +/* { dg-do run } */ +/* { dg-additional-options "-fno-tree-forwprop" } */ +/* PR tree-optimization/127689 */ + + +__attribute__((noipa))void check(int v) +{ + if (v != -4) + __builtin_exit(1); +} +int p; +signed char s, v; +signed char t = 0x80; +__attribute__((noipa)) +void y(signed char w) { + int t1; + t1 = __builtin_abs(t1); //256, 0x80 + p = t1; + s = p; + signed char t2 = t1; // -256, 0x80 + t = t2 % 3; // -2 + v = 2 * t; // -4 + check(v); +} +int main() +{ + y(0x80); +}