[{"id":3685459,"web_url":"http://patchwork.ozlabs.org/comment/3685459/","msgid":"<CALvbMcD=VCe=ZK2x1H6X4=q9K+mmO=YSQR1e2FOb8utfR7BXCA@mail.gmail.com>","list_archive_url":null,"date":"2026-05-03T22:18:11","subject":"Re: [PATCH] match: Optimize `A > B ? ABS(A) : B` to `MAX(A, B)` when\n B >= 0 [PR116700]","submitter":{"id":91428,"url":"http://patchwork.ozlabs.org/api/people/91428/","name":"Andrew Pinski","email":"andrew.pinski@oss.qualcomm.com"},"content":"On Sat, May 2, 2026 at 11:02 AM Avinal Kumar <avinal.xlvii@gmail.com> wrote:\n>\n> When B is known to be non-negative and A > B, A must be positive,\n> so ABS(A) == A.  The whole expression (A > B ? ABS(A) : B) then\n> simplifies to MAX(A, B).  This is caught at -O2 via VRP, but at\n> -O1 phiopt1 produces ABS_EXPR and no later pass simplifies it.\n>\n>         PR tree-optimization/116700\n>\n> gcc/ChangeLog:\n>\n>         * match.pd: (A > B ? ABS(A) : B -> MAX(A, B)): New pattern\n>         for non-negative B.\n>\n> gcc/testsuite/ChangeLog:\n>\n>         * gcc.dg/pr116700.c: New test.\n>         * gcc.dg/tree-ssa/phi-opt-48.c: New test.\n>\n> Signed-off-by: Avinal Kumar <avinal.xlvii@gmail.com>\n> ---\n> I compared the changes in the suggested patch on bugzilla, understood patterns\n> and tried to adapt them. The actual mechanism is still not completely understood\n> to me. The tests are running fine but I think I am missing some cases.\n\nLet me go over the pattern with you (This could be used for future\npart of the tutorials):\n(for cmp (gt ge)\n// this is a for loop which says cmp will either be gt (GT_EXPR,\ngreater than expression) or ge (GE_EXPR, greater than or equal\nexpression).\n  (simplify\n// (simplify a b) means simplify the matching a into b\n  (cond (cmp:c @0 tree_expr_nonnegative_p@1) (abs @0) @1)\n// cond here is matching COND_EXPR which is a 3 operand expressions\nwhich is similar to the C operator `?:`.\n// cmp:c means match cmp and `:c` here means to handle commutative\nexpressions in swapping the lhs and rhs operands of the expressions.\n// But since this is a comparison, it means handling of the swapping\nof the operands and use the corresponding comparison.\n//  That is `@0 < @1` and `@1 > @0` will match.\n// @0/@1 are captures\n// tree_expr_nonnegative_p is a predicate and returns true if the\nvalue is nonnegative. (there are many more predicates)\n// abs is short for ABS_EXPR which similar to the C function abs but\nis a tree expression rather than a function call (matching\nbuiltin/internal functions are supported too but we don't use them in\nthis case).\n   (if (INTEGRAL_TYPE_P (type))\n// This says if the resulting type (type) is a scalar integral type (a\nboolean, integer, bitint or an enum type)\n    (max @0 @1))))\n// This is the resulting expression in this case it is max.\n\n>\n> Here is how I ran the tests:\n>\n> make -j$(nproc)\n> make check-gcc RUNTESTFLAGS=\"tree-ssa.exp=phi-opt-48.c\"\n> make check-gcc RUNTESTFLAGS=\"dg.exp=pr116700.c\"\n>\n> On a side note, is there any trick/tips for building faster? I have a decent\n> system (8C 16T and 64GB RAM) and it takes almost an hour.\n\nYou can use one of the machines in the compile farm,\nhttps://gcc.gnu.org/wiki/CompileFarm.\nI will note you only ran a few testcases and not the full testsuite.\n\nThis patch is ok, I will push it after a full bootstrap test but next\ntime you should run the full testsuite.\n\nThanks,\nAndrew\n\n>\n>  gcc/match.pd                               |  9 ++++++++\n>  gcc/testsuite/gcc.dg/pr116700.c            | 13 +++++++++++\n>  gcc/testsuite/gcc.dg/tree-ssa/phi-opt-48.c | 25 ++++++++++++++++++++++\n>  3 files changed, 47 insertions(+)\n>  create mode 100644 gcc/testsuite/gcc.dg/pr116700.c\n>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-48.c\n>\n> diff --git a/gcc/match.pd b/gcc/match.pd\n> index dd9efb82c59..928f75c5a44 100644\n> --- a/gcc/match.pd\n> +++ b/gcc/match.pd\n> @@ -7276,6 +7276,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)\n>   (if (INTEGRAL_TYPE_P (type))\n>    @3))\n>\n> +/* A > B ? ABS(A) : B -> MAX(A, B) when B is non-negative.\n> +   When A > B >= 0, A is positive so ABS(A) = A = MAX(A, B).\n> +   When A <= B, the result is B = MAX(A, B).  */\n> +(for cmp (gt ge)\n> + (simplify\n> +  (cond (cmp:c @0 tree_expr_nonnegative_p@1) (abs @0) @1)\n> +  (if (INTEGRAL_TYPE_P (type))\n> +   (max @0 @1))))\n> +\n>  /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when\n>     X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */\n>  (simplify\n> diff --git a/gcc/testsuite/gcc.dg/pr116700.c b/gcc/testsuite/gcc.dg/pr116700.c\n> new file mode 100644\n> index 00000000000..a52388249a3\n> --- /dev/null\n> +++ b/gcc/testsuite/gcc.dg/pr116700.c\n> @@ -0,0 +1,13 @@\n> +/* PR tree-optimization/116700 */\n> +/* { dg-do compile } */\n> +/* { dg-options \"-O1 -fdump-tree-phiopt1\" } */\n> +\n> +int f(unsigned char a, int b, int c)\n> +{\n> +  int t = a;\n> +  if (c > t) t = (c > 0 ? c : -c);\n> +  return t;\n> +}\n> +\n> +/* { dg-final { scan-tree-dump \"MAX_EXPR\" \"phiopt1\" } } */\n> +/* { dg-final { scan-tree-dump-not \"ABS_EXPR\" \"phiopt1\" } } */\n> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-48.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-48.c\n> new file mode 100644\n> index 00000000000..06317766457\n> --- /dev/null\n> +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-48.c\n> @@ -0,0 +1,25 @@\n> +/* { dg-do compile } */\n> +/* { dg-options \"-O1 -fdump-tree-phiopt1\" } */\n> +\n> +/* Test that (A > B) ? ABS(A) : B is simplified to MAX(A, B)\n> +   when B is known to be non-negative. */\n> +\n> +int f(unsigned char a, int b, int c)\n> +{\n> +  int t = a;\n> +  if (c > t)\n> +    t = (c > 0 ? c : -c);\n> +  return t;\n> +}\n> +\n> +int f1(unsigned char a, int b, int c)\n> +{\n> +  int t = a;\n> +  if (c > t) return (c > 0 ? c : -c);\n> +  return t;\n> +}\n> +\n> +/* Both functions should be converted to MAX_EXPR by phiopt1.  */\n> +/* { dg-final { scan-tree-dump-times \"MAX_EXPR\" 2 \"phiopt1\" } } */\n> +/* { dg-final { scan-tree-dump-not \"ABS_EXPR\" \"phiopt1\" } } */\n> +/* { dg-final { scan-tree-dump-not \"if \" \"phiopt1\" } } */\n> --\n> 2.54.0\n>","headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256\n header.s=qcppdkim1 header.b=admGkyLl;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com\n header.a=rsa-sha256 header.s=google header.b=BSit3WN6;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=2620:52:6:3111::32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (2048-bit key,\n unprotected) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256\n header.s=qcppdkim1 header.b=admGkyLl;\n\tdkim=pass (2048-bit key,\n unprotected) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com\n header.a=rsa-sha256 header.s=google header.b=BSit3WN6","sourceware.org; dmarc=none (p=none dis=none)\n header.from=oss.qualcomm.com","sourceware.org;\n spf=pass smtp.mailfrom=oss.qualcomm.com","server2.sourceware.org;\n arc=pass smtp.remote-ip=205.220.168.131"],"Received":["from vm01.sourceware.org (vm01.sourceware.org\n [IPv6:2620:52:6:3111::32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g7zhM4SfXz1yJ9\n\tfor <incoming@patchwork.ozlabs.org>; Mon, 04 May 2026 08:18:58 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 147CE4BAE7F1\n\tfor <incoming@patchwork.ozlabs.org>; Sun,  3 May 2026 22:18:56 +0000 (GMT)","from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com\n [205.220.168.131])\n by sourceware.org (Postfix) with ESMTPS id CB46E4BABF3F\n for <gcc-patches@gcc.gnu.org>; Sun,  3 May 2026 22:18:26 +0000 (GMT)","from pps.filterd (m0279865.ppops.net [127.0.0.1])\n by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id\n 643D8M8s3642221\n for <gcc-patches@gcc.gnu.org>; Sun, 3 May 2026 22:18:26 GMT","from mail-dy1-f200.google.com (mail-dy1-f200.google.com\n [74.125.82.200])\n by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4dw87sbt8g-1\n (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT)\n for <gcc-patches@gcc.gnu.org>; Sun, 03 May 2026 22:18:25 +0000 (GMT)","by mail-dy1-f200.google.com with SMTP id\n 5a478bee46e88-2b81ff82e3cso2243491eec.0\n for <gcc-patches@gcc.gnu.org>; Sun, 03 May 2026 15:18:25 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 147CE4BAE7F1","OpenDKIM Filter v2.11.0 sourceware.org CB46E4BABF3F"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org CB46E4BABF3F","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org CB46E4BABF3F","ARC-Seal":["i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1777846707; cv=pass;\n b=JVlBEX4QB42pG60yk64BUA1DjCVD+F7oe8JcG2bhsyVt8wyMASXEVqLgokmLm351BjrdzhvMAvlQRhmPrQ1uiSQ4+39ilJMvj+9xjp1PAbryukpXtmNDM6dkmDJyMuaN+KCJ6IAZKbJ4BiBkV7/BFFMxTniJ8BKmpfMwjyJt0v4=","i=1; a=rsa-sha256; t=1777846705; cv=none;\n d=google.com; s=arc-20240605;\n b=RtkFtC8zss9DMQqEZZVtsO/Y1/Trycm3SyS03btIdgnnVlLkou9SHjoN6Rycp0oHTT\n CbeYCIUNta0VczkqA/LOr65NjpW+Wxcwqimr1de0GrbcJtQwDG8Z/PlzqsvO9jsiifUW\n ylMP42XKxqb7Rc7PGpGK76dXfRNUcAy9tk9y4j1bg7gUbS1PTXD4aakteiHAlDKWmy6P\n 9EG+q5lxfmR4+5fpDOA+5gMfjJC9wUpqH58/zFpXfF5cClTU2CXFIyB4maYv8EZvxqAU\n HHG4TTGrJGSDj2DIu0HWKOUTAyrmZMTNv9joMLIOzmzh6jPhZNS1K+cIViGcKiLvVzyp\n c1NA=="],"ARC-Message-Signature":["i=2; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777846707; c=relaxed/simple;\n bh=K3BI568IApvdJJavG7QRDlWUt2zvj66PdPcZFctMDRI=;\n h=DKIM-Signature:DKIM-Signature:MIME-Version:From:Date:Message-ID:\n Subject:To;\n b=rbYbzgX9G484P/PH6cbi3MI5XNCTHXD/hj63FVCkO2QpjItV8mAG89odGFtTKYUcaThdYjjEtjXMok78nxFcPd9bB8nKUUHcQQT3oHCP2422cfnuRq93ALPciwPvVqmiF7pr9AB/vthO2xNDAjpUeTEQ0D5KPe614ZS0NB4882c=","i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com;\n s=arc-20240605;\n h=content-transfer-encoding:cc:to:subject:message-id:date:from\n :in-reply-to:references:mime-version:dkim-signature;\n bh=0PVMRPqoJfPXYcvoTSE/PvBp0J2cog4zbW7x1KcBjgk=;\n fh=JDakSnM4Jbb2Jc4yB2MgNBsdzZpJwtSDf3Mkwl72JxA=;\n b=g5hiPR7rGuHqev1RlofXi1lcEw1/o7tJL3NltYZ4DGWUoCEPZuizX++ulTOvIpfLzX\n 9ZOypBKsdcqVnmoGM7sffO/JDXFecLmt3OxQPF8IdegJAHcFLQgRfnHlz3HKbUgVGKsm\n 1C5VMZFQQHMo6q/GF7rTtPlOdt7GY7nlmrFcgyX2bFYSyxUT0fJp4i3QG3ba0F/vMWJV\n nR9KeHm4ZgYrtaiHu9BLawJhOc5bbGtfiV9h7Xvwi4221oxbykTACIQ0+bEg7YeoEG7P\n oTA6xxDVD3qq6NCpA2HvlcQAtT0CSFzlJ7F+sG4htupSFh4t7VX1g1UAPbjeHqqHDI97\n M00g==; darn=gcc.gnu.org"],"ARC-Authentication-Results":["i=2; server2.sourceware.org","i=1; mx.google.com; arc=none"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h=\n cc:content-transfer-encoding:content-type:date:from:in-reply-to\n :message-id:mime-version:references:subject:to; s=qcppdkim1; bh=\n 0PVMRPqoJfPXYcvoTSE/PvBp0J2cog4zbW7x1KcBjgk=; b=admGkyLlcRkAGh3w\n kDAHufFlrECdn5s3KYNjDcy7MkKD1jNxdckT/i5KpyJ1fXQLay0NiIyiESmsyytX\n y7P1WPMOBhKtCuNvUDDgu1+5uNB3yCX8aaCrkqsZ94za90eQifO/ti/7om9fEHdu\n Ozx9I5dJCKbjobXTAvvkEjNolQpUQptYCufF/Va47Qv22VgATeuY6862Uph6hynl\n oyt/7LTWK+qe2ZyRdAMD5G/QyoQQcsAfsDX9Ldia1DWRYVgnvJIkEvo0B9ruJVmC\n AWdtYv/HGKAJmAIfWmcPvLs77FA60imVwcyufws2CT5yNP9EBR8Z8XW1VejOPd48\n eeKoNA==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=oss.qualcomm.com; s=google; t=1777846705; x=1778451505; darn=gcc.gnu.org;\n h=content-transfer-encoding:cc:to:subject:message-id:date:from\n :in-reply-to:references:mime-version:from:to:cc:subject:date\n :message-id:reply-to;\n bh=0PVMRPqoJfPXYcvoTSE/PvBp0J2cog4zbW7x1KcBjgk=;\n b=BSit3WN6LkfTdmxUeC1BPmKWrjU1fgU1X++GA9JP4XNARjE+DLYkerqVLLgx64qNDI\n jM3GZfWukh+B5j6E8woODOmE8ixr/WpyTbhDcogqXVhSnO1K7VDnnGcbbAV4adegtN2x\n iNun8m3mW6Z4czGTd90UGQVqccOINfz++mTv2PNthcmgsCOz6mzYxRMeNm6L0VgrlQLG\n 62NGARk59moOlMZmp7bg+YgSQ/+lcLpQ0EDRRfYd7z6ffNuUkdVomeETjBZVkkEIE2yY\n sl0Db2//GInKlKBEIwv0vlH1N2/Ed1MaBSWw3r4/4bQIdeOJB/O9z+G2iXPJ113GI2o6\n hiGw=="],"X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1777846705; x=1778451505;\n h=content-transfer-encoding:cc:to:subject:message-id:date:from\n :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from\n :to:cc:subject:date:message-id:reply-to;\n bh=0PVMRPqoJfPXYcvoTSE/PvBp0J2cog4zbW7x1KcBjgk=;\n b=gBCFmSAg/EUo80FGAzgawP+pOG4+LY4i7aFdqTUpqxQYIAo9fJVfiO8B06Fh+RY2T6\n YTQPxhi0xhj1woImZKeDZUvpwD2qbfrC2DTlzOeRwKFXXeg5ELH7cRn5fdWFjJ3E5wS/\n NuExwAKpqkioGAxIffYsm8f18DkHt4I1thRLyU3K8C/I219Sj0eWdO+7c9zca7tlSauc\n HwFE5FanV7wAsjb8BD7oG/MqnlAleiwkGsUSpodbGAHML4PLS2L87QTOYdCAkn4oQ1TG\n D95x5cLzi0HFfPkrQk0EAK/XL22/yUF7ZE87L7T+Qzss0PVopivTCVoog+OkKNAbZyJn\n FKGg==","X-Gm-Message-State":"AOJu0Yz8csUjmiqHlmMBS3FxB+0WllukiM9EY3wXKyDPdgh1LtpSbdwY\n egzAcZtQJbh54m6l7hE9ZFXP6u8X9XG2bXh/ZjYZg3qelaPim5PVHsVtky3o7Khsj017HQ+Z0E8\n 6hy5FaL9M4Ln49E6O/pASafhu/T7an5D5heBOovtp21DIxJouNpQbU4jFssv4pvxefha04Trr1B\n YtAguisB3KAdKNiuUEAuR3pdlmkL1nn1h9Jc0=","X-Gm-Gg":"AeBDieuOrkBQwAIc41rA1lPTkC1fDd3or1zTNj+kS+bauDiJT14X2nGpO5S36YJm8pF\n UjchbkgGt03lf3ZLN12iSLMpbjZ8smUHlkMU5eCVYxd5XBFmbXn6+nV20sDgZscMRHGhPPetUbN\n LB0Zp3BgnIkKNEGJj4JI9egVXMWUdsIuinZdWbYlI3NlMl4o/MtVP/W9WIEnSsgVg8O4JZLzAwA\n KlXCIeDe+ADTA==","X-Received":["by 2002:a05:7300:1496:b0:2f0:ff1b:b8ec with SMTP id\n 5a478bee46e88-2f0ff1bba28mr1573522eec.16.1777846704490;\n Sun, 03 May 2026 15:18:24 -0700 (PDT)","by 2002:a05:7300:1496:b0:2f0:ff1b:b8ec with SMTP id\n 5a478bee46e88-2f0ff1bba28mr1573513eec.16.1777846703823; Sun, 03 May 2026\n 15:18:23 -0700 (PDT)"],"MIME-Version":"1.0","References":"<20260502180223.410876-1-avinal.xlvii@gmail.com>","In-Reply-To":"<20260502180223.410876-1-avinal.xlvii@gmail.com>","From":"Andrew Pinski <andrew.pinski@oss.qualcomm.com>","Date":"Sun, 3 May 2026 15:18:11 -0700","X-Gm-Features":"AVHnY4IG8MPpC0xbbCy3S0ZOOsz_FWZY-iV7HHdCj5vAhcBBCPw37Pfh4-HqSpE","Message-ID":"\n <CALvbMcD=VCe=ZK2x1H6X4=q9K+mmO=YSQR1e2FOb8utfR7BXCA@mail.gmail.com>","Subject":"Re: [PATCH] match: Optimize `A > B ? ABS(A) : B` to `MAX(A, B)` when\n B >= 0 [PR116700]","To":"Avinal Kumar <avinal.xlvii@gmail.com>","Cc":"gcc-patches@gcc.gnu.org","Content-Type":"text/plain; charset=\"UTF-8\"","Content-Transfer-Encoding":"quoted-printable","X-Proofpoint-ORIG-GUID":"nPNOopv9LWhTRAQsNgb0KJLhZgpghmKU","X-Proofpoint-Spam-Details-Enc":"AW1haW4tMjYwNTAzMDI1MiBTYWx0ZWRfX7oUzwLsEVxb1\n gIY4tXSDLnJV20L0aVbOOGMxD6LImsa3lvrsaHUQ2YQtqbvOuIsuOBrmMkb1tTO4biThJAkm0sR\n dbxG+QKNTDBbBZrD80XuXCHHjne9KLWMXhb9HVM23yZDQsXdq2Ot3Z4z4snYUE7C8J8tri5yDKv\n Wjg1L/VmTZecrDgm2ZAUyCBpl8TQcqaDWKG9aHYbeygZ7KV/ik7S2wUNAN+0dSPB4J6tVjolNfU\n AY/JxIP+rOZlFlflCYXLxE4ZOD61RrwvC1WYr3j4PW3jDlVtUmwHfYMcFl+KNLE9dZa4b4BWW14\n yGc51aLDc8tzCAwyRuoXnIZuoCSIKlIt0weERT+tkdskslPzKQkEeE6arJTAaPk7zgOIYZp6qGl\n aLCGGm6YTsVVMhNcx1rBiiVkKDt7Gyw4cErsxi2XM86x7s30yX54UvBObV8NTl8KpgaHXWCJoOT\n j6aT5N+LG3L36KSVL7Q==","X-Authority-Analysis":"v=2.4 cv=O4IJeh9W c=1 sm=1 tr=0 ts=69f7c9b1 cx=c_pps\n a=PfFC4Oe2JQzmKTvty2cRDw==:117 a=IkcTkHD0fZMA:10 a=NGcC8JguVDcA:10\n a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22\n a=Um2Pa8k9VHT-vaBCBUpS:22 a=mDV3o1hIAAAA:8 a=pGLkceISAAAA:8\n a=lRmpd4woyl_akqqaipkA:9 a=QEXdDO2ut3YA:10 a=6Ab_bkdmUrQuMsNx7PHu:22","X-Proofpoint-GUID":"nPNOopv9LWhTRAQsNgb0KJLhZgpghmKU","X-Proofpoint-Virus-Version":"vendor=baseguard\n engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49\n definitions=2026-05-03_07,2026-04-30_02,2025-10-01_01","X-Proofpoint-Spam-Details":"rule=outbound_notspam policy=outbound score=0\n lowpriorityscore=0 impostorscore=0 adultscore=0 clxscore=1015 suspectscore=0\n priorityscore=1501 malwarescore=0 spamscore=0 phishscore=0 bulkscore=0\n classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0\n reason=mlx scancount=1 engine=8.22.0-2604200000 definitions=main-2605030252","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>,\n <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>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"}},{"id":3685482,"web_url":"http://patchwork.ozlabs.org/comment/3685482/","msgid":"<CALvbMcC98=xG2udQrWYgXqauZ9cTucj3JA6EyuZKCdGKOdomdg@mail.gmail.com>","list_archive_url":null,"date":"2026-05-04T02:59:25","subject":"Re: [PATCH] match: Optimize `A > B ? ABS(A) : B` to `MAX(A, B)` when\n B >= 0 [PR116700]","submitter":{"id":91428,"url":"http://patchwork.ozlabs.org/api/people/91428/","name":"Andrew Pinski","email":"andrew.pinski@oss.qualcomm.com"},"content":"On Sun, May 3, 2026 at 3:18 PM Andrew Pinski\n<andrew.pinski@oss.qualcomm.com> wrote:\n>\n> On Sat, May 2, 2026 at 11:02 AM Avinal Kumar <avinal.xlvii@gmail.com> wrote:\n> >\n> > When B is known to be non-negative and A > B, A must be positive,\n> > so ABS(A) == A.  The whole expression (A > B ? ABS(A) : B) then\n> > simplifies to MAX(A, B).  This is caught at -O2 via VRP, but at\n> > -O1 phiopt1 produces ABS_EXPR and no later pass simplifies it.\n> >\n> >         PR tree-optimization/116700\n> >\n> > gcc/ChangeLog:\n> >\n> >         * match.pd: (A > B ? ABS(A) : B -> MAX(A, B)): New pattern\n> >         for non-negative B.\n> >\n> > gcc/testsuite/ChangeLog:\n> >\n> >         * gcc.dg/pr116700.c: New test.\n> >         * gcc.dg/tree-ssa/phi-opt-48.c: New test.\n> >\n> > Signed-off-by: Avinal Kumar <avinal.xlvii@gmail.com>\n> > ---\n> > I compared the changes in the suggested patch on bugzilla, understood patterns\n> > and tried to adapt them. The actual mechanism is still not completely understood\n> > to me. The tests are running fine but I think I am missing some cases.\n>\n> Let me go over the pattern with you (This could be used for future\n> part of the tutorials):\n> (for cmp (gt ge)\n> // this is a for loop which says cmp will either be gt (GT_EXPR,\n> greater than expression) or ge (GE_EXPR, greater than or equal\n> expression).\n>   (simplify\n> // (simplify a b) means simplify the matching a into b\n>   (cond (cmp:c @0 tree_expr_nonnegative_p@1) (abs @0) @1)\n> // cond here is matching COND_EXPR which is a 3 operand expressions\n> which is similar to the C operator `?:`.\n> // cmp:c means match cmp and `:c` here means to handle commutative\n> expressions in swapping the lhs and rhs operands of the expressions.\n> // But since this is a comparison, it means handling of the swapping\n> of the operands and use the corresponding comparison.\n> //  That is `@0 < @1` and `@1 > @0` will match.\n> // @0/@1 are captures\n> // tree_expr_nonnegative_p is a predicate and returns true if the\n> value is nonnegative. (there are many more predicates)\n> // abs is short for ABS_EXPR which similar to the C function abs but\n> is a tree expression rather than a function call (matching\n> builtin/internal functions are supported too but we don't use them in\n> this case).\n>    (if (INTEGRAL_TYPE_P (type))\n> // This says if the resulting type (type) is a scalar integral type (a\n> boolean, integer, bitint or an enum type)\n>     (max @0 @1))))\n> // This is the resulting expression in this case it is max.\n>\n> >\n> > Here is how I ran the tests:\n> >\n> > make -j$(nproc)\n> > make check-gcc RUNTESTFLAGS=\"tree-ssa.exp=phi-opt-48.c\"\n> > make check-gcc RUNTESTFLAGS=\"dg.exp=pr116700.c\"\n> >\n> > On a side note, is there any trick/tips for building faster? I have a decent\n> > system (8C 16T and 64GB RAM) and it takes almost an hour.\n>\n> You can use one of the machines in the compile farm,\n> https://gcc.gnu.org/wiki/CompileFarm.\n> I will note you only ran a few testcases and not the full testsuite.\n>\n> This patch is ok, I will push it after a full bootstrap test but next\n> time you should run the full testsuite.\n\nNow pushed as r17-292-ge0c4c4cb02382b37a41da098aab3a2446d3cdf3e after\nchanging the whitespaces in the commit message to tabs for the\nchangelog entry part.\n\nThanks,\nAndrea\n\n>\n> Thanks,\n> Andrew\n>\n> >\n> >  gcc/match.pd                               |  9 ++++++++\n> >  gcc/testsuite/gcc.dg/pr116700.c            | 13 +++++++++++\n> >  gcc/testsuite/gcc.dg/tree-ssa/phi-opt-48.c | 25 ++++++++++++++++++++++\n> >  3 files changed, 47 insertions(+)\n> >  create mode 100644 gcc/testsuite/gcc.dg/pr116700.c\n> >  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-48.c\n> >\n> > diff --git a/gcc/match.pd b/gcc/match.pd\n> > index dd9efb82c59..928f75c5a44 100644\n> > --- a/gcc/match.pd\n> > +++ b/gcc/match.pd\n> > @@ -7276,6 +7276,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)\n> >   (if (INTEGRAL_TYPE_P (type))\n> >    @3))\n> >\n> > +/* A > B ? ABS(A) : B -> MAX(A, B) when B is non-negative.\n> > +   When A > B >= 0, A is positive so ABS(A) = A = MAX(A, B).\n> > +   When A <= B, the result is B = MAX(A, B).  */\n> > +(for cmp (gt ge)\n> > + (simplify\n> > +  (cond (cmp:c @0 tree_expr_nonnegative_p@1) (abs @0) @1)\n> > +  (if (INTEGRAL_TYPE_P (type))\n> > +   (max @0 @1))))\n> > +\n> >  /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when\n> >     X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */\n> >  (simplify\n> > diff --git a/gcc/testsuite/gcc.dg/pr116700.c b/gcc/testsuite/gcc.dg/pr116700.c\n> > new file mode 100644\n> > index 00000000000..a52388249a3\n> > --- /dev/null\n> > +++ b/gcc/testsuite/gcc.dg/pr116700.c\n> > @@ -0,0 +1,13 @@\n> > +/* PR tree-optimization/116700 */\n> > +/* { dg-do compile } */\n> > +/* { dg-options \"-O1 -fdump-tree-phiopt1\" } */\n> > +\n> > +int f(unsigned char a, int b, int c)\n> > +{\n> > +  int t = a;\n> > +  if (c > t) t = (c > 0 ? c : -c);\n> > +  return t;\n> > +}\n> > +\n> > +/* { dg-final { scan-tree-dump \"MAX_EXPR\" \"phiopt1\" } } */\n> > +/* { dg-final { scan-tree-dump-not \"ABS_EXPR\" \"phiopt1\" } } */\n> > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-48.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-48.c\n> > new file mode 100644\n> > index 00000000000..06317766457\n> > --- /dev/null\n> > +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-48.c\n> > @@ -0,0 +1,25 @@\n> > +/* { dg-do compile } */\n> > +/* { dg-options \"-O1 -fdump-tree-phiopt1\" } */\n> > +\n> > +/* Test that (A > B) ? ABS(A) : B is simplified to MAX(A, B)\n> > +   when B is known to be non-negative. */\n> > +\n> > +int f(unsigned char a, int b, int c)\n> > +{\n> > +  int t = a;\n> > +  if (c > t)\n> > +    t = (c > 0 ? c : -c);\n> > +  return t;\n> > +}\n> > +\n> > +int f1(unsigned char a, int b, int c)\n> > +{\n> > +  int t = a;\n> > +  if (c > t) return (c > 0 ? c : -c);\n> > +  return t;\n> > +}\n> > +\n> > +/* Both functions should be converted to MAX_EXPR by phiopt1.  */\n> > +/* { dg-final { scan-tree-dump-times \"MAX_EXPR\" 2 \"phiopt1\" } } */\n> > +/* { dg-final { scan-tree-dump-not \"ABS_EXPR\" \"phiopt1\" } } */\n> > +/* { dg-final { scan-tree-dump-not \"if \" \"phiopt1\" } } */\n> > --\n> > 2.54.0\n> >","headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256\n header.s=qcppdkim1 header.b=KWt7vrEz;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com\n header.a=rsa-sha256 header.s=google header.b=VFC1s3Kp;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=2620:52:6:3111::32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (2048-bit key,\n unprotected) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256\n header.s=qcppdkim1 header.b=KWt7vrEz;\n\tdkim=pass (2048-bit key,\n unprotected) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com\n header.a=rsa-sha256 header.s=google header.b=VFC1s3Kp","sourceware.org; dmarc=none (p=none dis=none)\n header.from=oss.qualcomm.com","sourceware.org;\n spf=pass smtp.mailfrom=oss.qualcomm.com","server2.sourceware.org;\n arc=pass smtp.remote-ip=205.220.180.131"],"Received":["from vm01.sourceware.org (vm01.sourceware.org\n [IPv6:2620:52:6:3111::32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g85wr3dm1z1y04\n\tfor <incoming@patchwork.ozlabs.org>; Mon, 04 May 2026 13:00:11 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 0CB774BB3B82\n\tfor <incoming@patchwork.ozlabs.org>; Mon,  4 May 2026 03:00:09 +0000 (GMT)","from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com\n [205.220.180.131])\n by sourceware.org (Postfix) with ESMTPS id F3C284BA23D3\n for <gcc-patches@gcc.gnu.org>; Mon,  4 May 2026 02:59:38 +0000 (GMT)","from pps.filterd (m0279872.ppops.net [127.0.0.1])\n by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id\n 643Djf7I3222945\n for <gcc-patches@gcc.gnu.org>; Mon, 4 May 2026 02:59:38 GMT","from mail-dy1-f198.google.com (mail-dy1-f198.google.com\n [74.125.82.198])\n by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4dw9n8by43-1\n (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT)\n for <gcc-patches@gcc.gnu.org>; Mon, 04 May 2026 02:59:38 +0000 (GMT)","by mail-dy1-f198.google.com with SMTP id\n 5a478bee46e88-2bda35eab74so3108730eec.0\n for <gcc-patches@gcc.gnu.org>; Sun, 03 May 2026 19:59:37 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 0CB774BB3B82","OpenDKIM Filter v2.11.0 sourceware.org F3C284BA23D3"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org F3C284BA23D3","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org F3C284BA23D3","ARC-Seal":["i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1777863579; cv=pass;\n b=DgEkGK1lClsv4j0KBDvN8HDhUNBmqDvJLHd6e2gVyNjARXq2a+CjQK87yoYa9Dvzns6LBOwKu3cPjFd2P9bmtrJyWYvuJOdaHbM606xXVFrP8NhJtBUK8BzpAWvtbHLWM19uSL8xAzUonoVN8XevAN97y+x8Il+1CmCOirHfQNU=","i=1; a=rsa-sha256; t=1777863577; cv=none;\n d=google.com; s=arc-20240605;\n b=K4fGil0g7X7UXf6wIk4F3AIu2PEL5R4rmm8bwCqqA4eQYE7e6XlG/aDD7hvvnwWimb\n PV53cLViCAzARbpDfHp3xCo/5aAAKgW+u1k8QAdAs/IIckQrHZ7b8kROSTvkpqUsKR1H\n ji5uMH/q8SUpyARL4Do1PoR4kfO/7ubQ0uM8q2CXf2w+bgBQb/nDhTvR1JUGLS0woHy6\n 2ep4KD9AC85Nbmg7Y/WDCpfb4voJ8t4sUcXMrKwOIQdPPBCNZg6nVjEw/FqL/5rmPgWh\n y+Vg5cdkj/GIMfeHmvxXJDXUoH0c5/r1yg9sqhcSvCGFq8E8CrwGTqyNYghiJUVuhc8N\n btWw=="],"ARC-Message-Signature":["i=2; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777863579; c=relaxed/simple;\n bh=RDZzSCoyAD+7Hgvg5ZH0VhUYIGUWOZjJXKvZ11PEPKo=;\n h=DKIM-Signature:DKIM-Signature:MIME-Version:From:Date:Message-ID:\n Subject:To;\n b=csHx5RrugajQC7flXtyLsL+G8kKe1Bt7UV5QLUGy0okLDgmfG5iYqE0O3WTl7pMGvwC3yRI2CMQsqKrKUWDBU/fSoXGdXPB6rHYNjmweGWAV5xtUJwpTheY6zBO+Ykn8nEu8QZft4HgiG6/7JFa3o9/cwyn+c9FuMmY7wxru5mE=","i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com;\n s=arc-20240605;\n h=content-transfer-encoding:cc:to:subject:message-id:date:from\n :in-reply-to:references:mime-version:dkim-signature;\n bh=EMCoWFJlmhtdJxM3/X5scYTaw7paNsp/53ZXPtnPldE=;\n fh=JDakSnM4Jbb2Jc4yB2MgNBsdzZpJwtSDf3Mkwl72JxA=;\n b=TP47Pe5lr43OgW0hmu+UvAlwMB88HQbNsxlPlSWquvMAJsWeXejK6m/ZbSGp22R9lc\n 7jJMVrPh0/fjkcm856kIGrTGAV1rIHQiQFAUTcSjxE5nlojdqeGWQKYt+9gwJ51fQ7m3\n kXDToFaGPytkMssWwoekwghZrwt4IBoP6FT/lfk5bDhx4GBQRvLOnHMQXKo0Jd/U8Qk3\n Cz0bMlRY4fod0jAcyPT0oXMYJELdhJApK+E9oPYnTuCYT3y6kpcsST3mZFYrECyZZx6Q\n ZWPpamvfR2tipGrM+Z5t6egRtOEfFojmWiauRXmEZ7WD0sbEAvlx3DqablShi2oZjwUC\n LoyA==; darn=gcc.gnu.org"],"ARC-Authentication-Results":["i=2; server2.sourceware.org","i=1; mx.google.com; arc=none"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h=\n cc:content-transfer-encoding:content-type:date:from:in-reply-to\n :message-id:mime-version:references:subject:to; s=qcppdkim1; bh=\n EMCoWFJlmhtdJxM3/X5scYTaw7paNsp/53ZXPtnPldE=; b=KWt7vrEz9TE/EAd6\n wMNpGJMeQbe9jVlj4UqmKWZ+LC5Ls5Xwd54hwD7vDAIZYq6HpcEnAoaDDS7zzbZA\n XvGqhyoayCecUlgftlNNRG/LAIy2iOMCY7RzpEtEtwlf2Bts3BF4XQXkXE7GpoIG\n 4q/kB4E7E1B7rnURvIc/T6TlDy4+Ch6mF/hpevyZZMorP0RWcajNLqXpK/HbbswL\n iUkv9ZccVF5N48lF6rz0sKn1m090g2R65gvp4j1JfqMhhdP9sd9Bh0u7DEHHScNo\n CL7J5yyvPeUiHDYJX2jhrxbKJIE85Bz/IDfdIyVGIw35yE9bUdJT49wZQ9tWmZuS\n pZ0zVA==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=oss.qualcomm.com; s=google; t=1777863577; x=1778468377; darn=gcc.gnu.org;\n h=content-transfer-encoding:cc:to:subject:message-id:date:from\n :in-reply-to:references:mime-version:from:to:cc:subject:date\n :message-id:reply-to;\n bh=EMCoWFJlmhtdJxM3/X5scYTaw7paNsp/53ZXPtnPldE=;\n b=VFC1s3KpAi3PiH0/+5FNDdYsj1k+Y5N9hK2cuV+5uwXBIef0GBLCn5EeH5Y61sabd3\n pBmiB6cC26v6jkAcBV67O+yZdm1rp7BlfTiw5vifG6q5CigB84sqtBNV1JZV1OifaLLX\n HesxhXo0RdYVdBzIOV810IInhoQBdfC4U/WdTVO2pNzwNVD5IZcF+IrHFjDciNqX1u32\n i3FapoSkNgD/QOCp9hjtyKYVlZ66HCWo1FuYJ7L384RjoK7IGIYr0hrzo5fHUq5C5Tbw\n sIW9ADRz46ggz+tDm41r88eFvXreXe+6aWO/cPF5g/LHJUtNxnBf9258qUG/3PMnhaMP\n AVIQ=="],"X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1777863577; x=1778468377;\n h=content-transfer-encoding:cc:to:subject:message-id:date:from\n :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from\n :to:cc:subject:date:message-id:reply-to;\n bh=EMCoWFJlmhtdJxM3/X5scYTaw7paNsp/53ZXPtnPldE=;\n b=r2sk+K5Kqw6Mg+XLv+Xcfi77ZniDkBG7cS0IeiAQ5JRUtoZlB30A/Gc2zUERZpTLSs\n /xWpM3b+m8uGQ7dKgwpWiRKn0dAPOfs29DGgC4XCMdgf2VW6Nkdq/5xic9FwHPM0c8Lx\n 357jsgzcrkGGt5vFuKB9Lv/ikWnx9cUAvqk6wha4oGkeM54KfzyG5GEhQQo5ZPGNjxIr\n 8vAqh7QK6p1vNXm3HX5LjohGfCN164Fo4c9aD9X21L9CKghKyYmbt0jCsWwNUUYYWjPE\n a7qoBiw0WF7idTHB22lJMi1aHHvzlhqwpAjBnvT1pkzolhs2LlV4jOcRUOTeNUq/4urK\n n3hw==","X-Gm-Message-State":"AOJu0YwrIysDbKSusHY1ECdpXguDQVXGJdP3CQh8eqR4MVVp2ZUhkcVg\n qzAPH5XhBKm6NGRV+lDh/fzMLCF+0JLj8qg1k1DZ7cC4WaHXjWuYvRD0Ly8B7suN4VwuThyTyKN\n ZuIZuSaABx+AyoxQvTHd+g3d6ID0pjlbjKNMQVdsRzatofbpS14UUQVtHUv8AId2ixMYVqAx6nR\n simicpbdv/5gIqNRtkgEVpAff1yfW85/jnzvw=","X-Gm-Gg":"AeBDietOKuAxm14Es00Q+82JGSyNGKga9WfDCjHCnFlfWEPyApbtut+FiFbAiB6O0gp\n MqNFeWnVJUhdJ0WHm2Zb9NWxiGBPQwab2j0ksNqG47mqom8JKl6c7prllP2vwUnTg5MAoYKs/kB\n 7oJvS/L43dEEvju32UEFjwQeoBIaZQZgjvhRsiHmaynVlPaPX2hylezFL6YwpuwKzLTZuWVynHo\n cldsLtx/BKvMA==","X-Received":["by 2002:a05:7301:5784:b0:2ea:ed27:56c7 with SMTP id\n 5a478bee46e88-2ed50cc5edcmr6685404eec.20.1777863576898;\n Sun, 03 May 2026 19:59:36 -0700 (PDT)","by 2002:a05:7301:5784:b0:2ea:ed27:56c7 with SMTP id\n 5a478bee46e88-2ed50cc5edcmr6685396eec.20.1777863576272; Sun, 03 May 2026\n 19:59:36 -0700 (PDT)"],"MIME-Version":"1.0","References":"<20260502180223.410876-1-avinal.xlvii@gmail.com>\n <CALvbMcD=VCe=ZK2x1H6X4=q9K+mmO=YSQR1e2FOb8utfR7BXCA@mail.gmail.com>","In-Reply-To":"\n <CALvbMcD=VCe=ZK2x1H6X4=q9K+mmO=YSQR1e2FOb8utfR7BXCA@mail.gmail.com>","From":"Andrew Pinski <andrew.pinski@oss.qualcomm.com>","Date":"Sun, 3 May 2026 19:59:25 -0700","X-Gm-Features":"AVHnY4ISdye3A7EL2KpYHoT4SFsxARwpXXo-8T28ihclD8Fd1BThR9noWarxRss","Message-ID":"\n <CALvbMcC98=xG2udQrWYgXqauZ9cTucj3JA6EyuZKCdGKOdomdg@mail.gmail.com>","Subject":"Re: [PATCH] match: Optimize `A > B ? ABS(A) : B` to `MAX(A, B)` when\n B >= 0 [PR116700]","To":"Avinal Kumar <avinal.xlvii@gmail.com>","Cc":"gcc-patches@gcc.gnu.org","Content-Type":"text/plain; charset=\"UTF-8\"","Content-Transfer-Encoding":"quoted-printable","X-Proofpoint-GUID":"i9zxfcerh0M5l7Ts-7jAbiA_Ifn4DzEN","X-Proofpoint-ORIG-GUID":"i9zxfcerh0M5l7Ts-7jAbiA_Ifn4DzEN","X-Proofpoint-Spam-Details-Enc":"AW1haW4tMjYwNTA0MDAyOSBTYWx0ZWRfX1IlKBCCJt5Si\n m7wEEXSORhFqNQMyiVbS7/fpQUPvgZpnBxwraXTJ802DkOIW1RZytJnWmaz6ki1TOGHVDgEC0vE\n IzCgSsGqu+CB5EkymTT72xa/RePBQgy74ufbnJf1FarzOYpm+RnpQdefjU0FvpkRyw7aT0EcDAr\n vaY5DX8u1qy6yQ2XuJs9c0WSyz/An1Qesw1J0BFg5PYBQaWvL00xZG2j+mX5nf4uRFOg9/3Di6I\n JDOGDh8uK80AjNUqVd2SGGvxfgez2eiTvPDBOVhEEDdHDtyQ94ZtQd4Czs3Q6eJtV7802QaSPdb\n W4qZz+0ThkHrbb/c29RxkgyJTn4t6upXnlajLSzfIbiZvVarf4/fxV1euwkfRucdAo24/RGTzPP\n VfdmJuWiNOx2ee2AfpHKfOfqqhUsM6iwSUEHjz3D3q26c52O8Qf0ip9KruSirojIk3LSwbxBhVR\n Vel2s6UAJd7WnK7AdQQ==","X-Authority-Analysis":"v=2.4 cv=ScjHsPRu c=1 sm=1 tr=0 ts=69f80b9a cx=c_pps\n a=wEP8DlPgTf/vqF+yE6f9lg==:117 a=IkcTkHD0fZMA:10 a=NGcC8JguVDcA:10\n a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22\n a=yx91gb_oNiZeI1HMLzn7:22 a=mDV3o1hIAAAA:8 a=EUspDBNiAAAA:8 a=pGLkceISAAAA:8\n a=wd4vHVTs4SPde2T76g0A:9 a=QEXdDO2ut3YA:10 a=bBxd6f-gb0O0v-kibOvt:22","X-Proofpoint-Virus-Version":"vendor=baseguard\n engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49\n definitions=2026-05-04_01,2026-04-30_02,2025-10-01_01","X-Proofpoint-Spam-Details":"rule=outbound_notspam policy=outbound score=0\n phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 malwarescore=0\n impostorscore=0 priorityscore=1501 adultscore=0 lowpriorityscore=0\n suspectscore=0 classifier=typeunknown authscore=0 authtc= authcc=\n route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2604200000\n definitions=main-2605040029","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>,\n <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>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"}},{"id":3685518,"web_url":"http://patchwork.ozlabs.org/comment/3685518/","msgid":"<CAJ9xu4zqpAq90ccU+yKquE72ud-F0fMvRvBqAmpJZin=xYy+hA@mail.gmail.com>","list_archive_url":null,"date":"2026-05-04T07:04:58","subject":"Re: [PATCH] match: Optimize `A > B ? ABS(A) : B` to `MAX(A, B)` when\n B >= 0 [PR116700]","submitter":{"id":88549,"url":"http://patchwork.ozlabs.org/api/people/88549/","name":"Avinal Kumar","email":"avinal.xlvii@gmail.com"},"content":"On Mon, May 4, 2026 at 8:29 AM Andrew Pinski\n<andrew.pinski@oss.qualcomm.com> wrote:\n\n> > Let me go over the pattern with you (This could be used for future\n> > part of the tutorials):\n> > (for cmp (gt ge)\n> > // this is a for loop which says cmp will either be gt (GT_EXPR,\n> > greater than expression) or ge (GE_EXPR, greater than or equal\n> > expression).\n> >   (simplify\n> > // (simplify a b) means simplify the matching a into b\n> >   (cond (cmp:c @0 tree_expr_nonnegative_p@1) (abs @0) @1)\n> > // cond here is matching COND_EXPR which is a 3 operand expressions\n> > which is similar to the C operator `?:`.\n> > // cmp:c means match cmp and `:c` here means to handle commutative\n> > expressions in swapping the lhs and rhs operands of the expressions.\n> > // But since this is a comparison, it means handling of the swapping\n> > of the operands and use the corresponding comparison.\n> > //  That is `@0 < @1` and `@1 > @0` will match.\n> > // @0/@1 are captures\n> > // tree_expr_nonnegative_p is a predicate and returns true if the\n> > value is nonnegative. (there are many more predicates)\n> > // abs is short for ABS_EXPR which similar to the C function abs but\n> > is a tree expression rather than a function call (matching\n> > builtin/internal functions are supported too but we don't use them in\n> > this case).\n> >    (if (INTEGRAL_TYPE_P (type))\n> > // This says if the resulting type (type) is a scalar integral type (a\n> > boolean, integer, bitint or an enum type)\n> >     (max @0 @1))))\n> > // This is the resulting expression in this case it is max.\n\nThanks a lot, this is very helpful.\n\n> > > Here is how I ran the tests:\n> > >\n> > > make -j$(nproc)\n> > > make check-gcc RUNTESTFLAGS=\"tree-ssa.exp=phi-opt-48.c\"\n> > > make check-gcc RUNTESTFLAGS=\"dg.exp=pr116700.c\"\n> > >\n> > > On a side note, is there any trick/tips for building faster? I have a decent\n> > > system (8C 16T and 64GB RAM) and it takes almost an hour.\n> >\n> > You can use one of the machines in the compile farm,\n> > https://gcc.gnu.org/wiki/CompileFarm.\n> > I will note you only ran a few testcases and not the full testsuite.\n> >\n> > This patch is ok, I will push it after a full bootstrap test but next\n> > time you should run the full testsuite.\n\nI ran the full testsuite as well but forgot to mention. I will mention\nit from next time.\nI have requested a CompileFarm account. Thanks.","headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256\n header.s=20251104 header.b=mYYFzxHR;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=38.145.34.32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (2048-bit key,\n unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256\n header.s=20251104 header.b=mYYFzxHR","sourceware.org;\n dmarc=pass (p=none dis=none) header.from=gmail.com","sourceware.org; spf=pass smtp.mailfrom=gmail.com","server2.sourceware.org;\n arc=pass smtp.remote-ip=2a00:1450:4864:20::631"],"Received":["from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g8CN74Ch0z1y04\n\tfor <incoming@patchwork.ozlabs.org>; Mon, 04 May 2026 17:05:42 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 316934BAD16A\n\tfor <incoming@patchwork.ozlabs.org>; Mon,  4 May 2026 07:05:40 +0000 (GMT)","from mail-ej1-x631.google.com (mail-ej1-x631.google.com\n [IPv6:2a00:1450:4864:20::631])\n by sourceware.org (Postfix) with ESMTPS id 51B124BA79B2\n for <gcc-patches@gcc.gnu.org>; Mon,  4 May 2026 07:05:12 +0000 (GMT)","by mail-ej1-x631.google.com with SMTP id\n a640c23a62f3a-ba922426c5cso652810366b.3\n for <gcc-patches@gcc.gnu.org>; Mon, 04 May 2026 00:05:12 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 316934BAD16A","OpenDKIM Filter v2.11.0 sourceware.org 51B124BA79B2"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 51B124BA79B2","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 51B124BA79B2","ARC-Seal":["i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1777878312; cv=pass;\n b=cAoGyRFtew5lIMTSn8gGquplck9xbidTRKShkL3KzkIisfdCvajT1ULWroP5dpHuzdSW31xzK2eOq9n9njCPcUQtyvrj+zgyH57HiYjUWPNLG2a0FVcYHKfjn7B8ArwoPc1JH3qRoYAlmRJZUwg2lBtMFJyA8PG20XI0OtEnVa4=","i=1; a=rsa-sha256; t=1777878311; cv=none;\n d=google.com; s=arc-20240605;\n b=Fr5n3isD/iGScCutM8HdpJGyHnUAhcA7syoH3fpZbo+PaEROPXHRyOoNccWwWTVD1z\n 4OGzGFUVeSIc6ztVdg4YnKB3S2tovz1n33nmR2bDna+cSCr678PHsPFcF/1nPG5KRAP4\n xkWufQPTme8XZ0/IgH7++l5XaNRJCg8sED4WBDbfoNpLckO6ri2tHvG8soemyT45UX3i\n wc7Nw8+MNFUToGYe80Y5ObbKH+BS1tTjFRGj/DD9NNs8qA5jllenMszX2xCBCiHv/g+y\n G+lwNHysVQZ4Ilyqau/PoVnZvT4yewjDBsX2oeG0QS6rSEd9nuFssfI8YK1+9D7RLVpl\n GuvQ=="],"ARC-Message-Signature":["i=2; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777878312; c=relaxed/simple;\n bh=vlavOydwgvG6qZNaFvnoGCO207DOgMhrjuKrGLeBxdA=;\n h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To;\n b=xG2sf46L5i9y3vCL//nz2l72X0o6Hznkv6u0oILL11558zK+fzLIvQAeuUR581rJZiAorfbntV7yta1UxMaRxIhNpVhxwydFNyAXWiMwUmzNyMTl7krqEDrYhRfbOXKS8ZzKtTG2sjPSKEDtfWMaqhj6o7xRgDHG92lC/+QQiFo=","i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com;\n s=arc-20240605;\n h=content-transfer-encoding:cc:to:subject:message-id:date:from\n :in-reply-to:references:mime-version:dkim-signature;\n bh=cGzTFgYkMooDyHTn2yMkxIxTtLUkgZJu6TTonV1Ahcw=;\n fh=+uEDwx55CzEWtNA/tzpV1shOHaMh+Xh7AN17D33930Y=;\n b=BH85l57Ci5nrrgDM0XReUWFVE9lr0PaRzHJFDiKtWlBewvmSghdui5R2HazjtY29nt\n 5PWh3c0hGZ+czjZrQijZoM0y0C2p4Jytd1HbrjUvHCQ6AFDJejsf4WrhFZKX6PDevwko\n LpjbvTnqdoMnhihtFbGfQgKheQXI0Jamy452JHhty35yM53H1MyKigcrEq+RMmXxINBd\n dARwC9LzIkX8g5UtE58WLgVPOIwbtUlRxfmV916amhlo1hlrMaMJJB9f+nowxvIi19lx\n HrZXQpJG8CpGBUy58HBI/aHFDeuJURtZfzF6qoqhj++Vv8Q2lzK2Ft+4dhk12CIOrHZK\n 60cA==; darn=gcc.gnu.org"],"ARC-Authentication-Results":["i=2; server2.sourceware.org","i=1; mx.google.com; arc=none"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=gmail.com; s=20251104; t=1777878311; x=1778483111; darn=gcc.gnu.org;\n h=content-transfer-encoding:cc:to:subject:message-id:date:from\n :in-reply-to:references:mime-version:from:to:cc:subject:date\n :message-id:reply-to;\n bh=cGzTFgYkMooDyHTn2yMkxIxTtLUkgZJu6TTonV1Ahcw=;\n b=mYYFzxHRfqRhKorATbDBZ+VewITxiULM5QBHSPgzqgZBegQE1Gbf/hgDzCbn95WlHH\n SykTOrF8MTcclR9oKUFUbNf9LM60b5bRcsE1gJPLC/hSh3b92cmEHaj3t0Fi9xdvQjwT\n m2FUGIU2YVOIlCbXVwSgR3IfdvxVL42bk4n9RRBRByaPqNVZQwQADuwMXOUQpDYtLf9W\n HZhTF1vS7UyMQgIm2nrB4ozIP16k3meFK0eCbgHmRR3myQyV/ofK0SsvpqAAGXCalIb0\n DiGGvv7qAHp1cjaQzGj5SCgarEpR1WwCtLQhjBL0O519TGRPxyBB1dZ9Dnsa+uegjRXi\n 93gQ==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1777878311; x=1778483111;\n h=content-transfer-encoding:cc:to:subject:message-id:date:from\n :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from\n :to:cc:subject:date:message-id:reply-to;\n bh=cGzTFgYkMooDyHTn2yMkxIxTtLUkgZJu6TTonV1Ahcw=;\n b=baCSwSHk2fTN1//jBXs0iazdGteQIqyw7j0q3yQNutZsTLiEkle1+PAq7qq3MrUwLR\n 5jg+d86orWKl6SFjHgTU1AP8kad1ID7AARatZkKbLREYj9z0GbMGAlKbG5MTF03vU7Mp\n HWO91rkTlb0tkJ03j0nG8aShsJIo32AR0e/PdLukq4dKVILhb0zkNR0RVlRHuFzs0gwz\n gifQacAEjNa6CYEAivVSBgXtDJjKBpbQ7oIp7s0rArb2LnzlEdSC33oAqErBpVt4ftd4\n FAdQL+TkQUGfVtyQFwpNxnlGjszZTWYBJkR88yF9zMR8kAaZgc3/3p7ZJTa3+GDZnmsV\n uvKQ==","X-Gm-Message-State":"AOJu0YxX/4BKqEm1SGD7kp+IM79Ji/SM8y6xBe7SYES/tkQDSvMotm7n\n SJO5ckCMGk4KQ3jYSWTmYn7KJJrRpBsRRi4lRHijAnhFPGIRExqgo+W5UBx504qLaEDdafxQVMy\n eE5j0WZA7gjXdthrqlm3SUiU/SGmTjZY=","X-Gm-Gg":"AeBDievSTlglVTghd2oRiYFK/DcsjRtxniGX6n/TA3DChc1ifHAArkurJtdHgxvqTxw\n yXJLxI0bVT/MTDkhZ3awcSr9F0+HJXWtrD3Gc7ymtKpBFE1RseA13C2w/vV+vVEnao3xOBvaU2Y\n xxNCnTAvhmuAS+fToNNPS4Z5a/dNa45nS2qYOnJR2FFv7mUNLFQTCQAkcnuiZKNLZ5vFRI1K/cC\n uaOuMR7z80IyBktHwKnYRNoPixwafXqDZG+otI+GND37S2eZPaODpGy3M6i+juEvxhZlPl4p310\n jnruzUcGOZTEHhUzWuAhVVWFab/kEktmgT4qg2YTy/O7e2oxwSs=","X-Received":"by 2002:a17:906:9fcb:b0:ba3:8a04:7688 with SMTP id\n a640c23a62f3a-bbffe0cc792mr375194766b.36.1777878310307; Mon, 04 May 2026\n 00:05:10 -0700 (PDT)","MIME-Version":"1.0","References":"<20260502180223.410876-1-avinal.xlvii@gmail.com>\n <CALvbMcD=VCe=ZK2x1H6X4=q9K+mmO=YSQR1e2FOb8utfR7BXCA@mail.gmail.com>\n <CALvbMcC98=xG2udQrWYgXqauZ9cTucj3JA6EyuZKCdGKOdomdg@mail.gmail.com>","In-Reply-To":"\n <CALvbMcC98=xG2udQrWYgXqauZ9cTucj3JA6EyuZKCdGKOdomdg@mail.gmail.com>","From":"Avinal Kumar <avinal.xlvii@gmail.com>","Date":"Mon, 4 May 2026 12:34:58 +0530","X-Gm-Features":"AVHnY4LkgZ_aTVvpfWqHBS_LmAlAkwxHH3IVJwVNlCl32950xBKBNIYEJVj36KU","Message-ID":"\n <CAJ9xu4zqpAq90ccU+yKquE72ud-F0fMvRvBqAmpJZin=xYy+hA@mail.gmail.com>","Subject":"Re: [PATCH] match: Optimize `A > B ? ABS(A) : B` to `MAX(A, B)` when\n B >= 0 [PR116700]","To":"Andrew Pinski <andrew.pinski@oss.qualcomm.com>","Cc":"gcc-patches@gcc.gnu.org","Content-Type":"text/plain; charset=\"UTF-8\"","Content-Transfer-Encoding":"quoted-printable","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>,\n <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>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"}}]