From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 3A5593858400; Fri, 31 Dec 2021 01:18:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3A5593858400 From: "zhaoweiliew at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/95424] Failure to optimize division with numerator of 1 Date: Fri, 31 Dec 2021 01:18:23 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: enhancement X-Bugzilla-Who: zhaoweiliew at gmail dot com X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 31 Dec 2021 01:18:23 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D95424 --- Comment #4 from Zhao Wei Liew --- Comment on attachment 52097 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D52097 Optimization for both unsigned and signed integer X diff --git a/gcc/match.pd b/gcc/match.pd index 0d7b8dd1334..6fbf1071a97 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -349,6 +349,23 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (op @0 integer_onep) (non_lvalue @0))) +/* 1 / X -> X =3D=3D 1 for unsigned integer X + 1 / X -> X >=3D -1 && X <=3D 1 ? X : 0 for signed integer X */ +(for div (trunc_div ceil_div floor_div round_div exact_div) + (simplify + (div integer_onep@0 @1) + (switch + /* 1 / X -> X =3D=3D 1 for unsigned integer X */ + (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@1)) + && TYPE_UNSIGNED (TREE_TYPE (@1))) + (eq @0 @1)) + /* 1 / X -> X >=3D -1 && X <=3D 1 ? X : 0 for signed integer X */ + (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@1)) + && !TYPE_UNSIGNED (TREE_TYPE (@1))) + (cond (bit_and (ge @1 { build_minus_one_cst (integer_type_node); }) + (le @1 { build_one_cst (integer_type_node); })) + @1 { build_zero_cst (type); }))))) + /* (A / (1 << B)) -> (A >> B). Only for unsigned A. For signed A, this would not preserve rounding toward zero.=