From patchwork Mon May 13 08:09:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1934582 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=YOL7HgXr; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VdBx45Lp0z1ymw for ; Mon, 13 May 2024 18:10:00 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5D9C7386F452 for ; Mon, 13 May 2024 08:09:58 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 953953858C52 for ; Mon, 13 May 2024 08:09:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 953953858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 953953858C52 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715587769; cv=none; b=hN8EhYn4pfiKu62Yj4RHKbspgBwUGXRZNsapRcZMmPv86uxz4HiM5ItM+njE3kiDYsYaUlbJsGH08Aq17gKZEIUiqtaMXKcusIe9tuUvqVtyCK3aUvjtUCIQFhnlVd5asGhirzCGL16kmogNn3dfPIOEPXke8t+SoMeOFjIhtRs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715587769; c=relaxed/simple; bh=/o7OhOkRf/8CY1lowqpLHHiPShIcwsuilIYJ4Iwvd9I=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=qjkMT241bvA6630E4GcfVlOiu/9VzeusNDvSAaVuEtDNqyeq/E+s8rCVoeEGLjv3JLcWqG76J2FVPBfeFCY01Y54/LQHRowzu4AYBQO+jBLHYqDCy/wZmZO7QZvRT/n6BM8lGPg+q7Cj3nzDM9qorWZxyJEtO4wXAyegUl+QVLw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715587763; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=hdmBUH8vEOb5gNNHLboldbaH5LgX1MWjp2fo9EI1MRs=; b=YOL7HgXrWlrkaXguX5zpn6qgaAQDa5eu1SHfpeRDBLarSMFV9NsdtxVjdaT12GU6lWIExF f3bwxss+KD6uDKROaFcyZoZTHOnm7FUJqtcN3adScEXiv6W7eGPWnr8v3BUim5oUEtIBlv u2CQ/qs6R+d4YE6EW1tytO1Q9jsvRa4= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-634-CFr-Bh8zP_6ZGbQJZryIhw-1; Mon, 13 May 2024 04:09:21 -0400 X-MC-Unique: CFr-Bh8zP_6ZGbQJZryIhw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B20B03C025C6; Mon, 13 May 2024 08:09:21 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.31]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 75A3F105480A; Mon, 13 May 2024 08:09:21 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 44D89JCT1950000 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 13 May 2024 10:09:19 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 44D89JAX1949999; Mon, 13 May 2024 10:09:19 +0200 Date: Mon, 13 May 2024 10:09:19 +0200 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-ssa-math-opts: Pattern recognize yet another .ADD_OVERFLOW pattern [PR113982] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.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: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! We pattern recognize already many different patterns, and closest to the requested one also yc = (type) y; zc = (type) z; x = yc + zc; w = (typeof_y) x; if (x > max) where y/z has the same unsigned type and type is a wider unsigned type and max is maximum value of the narrower unsigned type. But apparently people are creative in writing this in diffent ways, this requests yc = (type) y; zc = (type) z; x = yc + zc; w = (typeof_y) x; if (x >> narrower_type_bits) The following patch implements that. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-05-13 Jakub Jelinek PR middle-end/113982 * tree-ssa-math-opts.cc (arith_overflow_check_p): Also return 1 for RSHIFT_EXPR by precision of maxval if shift result is only used in a cast or comparison against zero. (match_arith_overflow): Handle the RSHIFT_EXPR use case. * gcc.dg/pr113982.c: New test. Jakub --- gcc/tree-ssa-math-opts.cc.jj 2024-04-11 09:26:36.318369218 +0200 +++ gcc/tree-ssa-math-opts.cc 2024-05-10 18:17:08.795744811 +0200 @@ -3947,6 +3947,66 @@ arith_overflow_check_p (gimple *stmt, gi else return 0; + if (maxval + && ccode == RSHIFT_EXPR + && crhs1 == lhs + && TREE_CODE (crhs2) == INTEGER_CST + && wi::to_widest (crhs2) == TYPE_PRECISION (TREE_TYPE (maxval))) + { + tree shiftlhs = gimple_assign_lhs (use_stmt); + if (!shiftlhs) + return 0; + use_operand_p use; + if (!single_imm_use (shiftlhs, &use, &cur_use_stmt)) + return 0; + if (gimple_code (cur_use_stmt) == GIMPLE_COND) + { + ccode = gimple_cond_code (cur_use_stmt); + crhs1 = gimple_cond_lhs (cur_use_stmt); + crhs2 = gimple_cond_rhs (cur_use_stmt); + } + else if (is_gimple_assign (cur_use_stmt)) + { + if (gimple_assign_rhs_class (cur_use_stmt) == GIMPLE_BINARY_RHS) + { + ccode = gimple_assign_rhs_code (cur_use_stmt); + crhs1 = gimple_assign_rhs1 (cur_use_stmt); + crhs2 = gimple_assign_rhs2 (cur_use_stmt); + } + else if (gimple_assign_rhs_code (cur_use_stmt) == COND_EXPR) + { + tree cond = gimple_assign_rhs1 (cur_use_stmt); + if (COMPARISON_CLASS_P (cond)) + { + ccode = TREE_CODE (cond); + crhs1 = TREE_OPERAND (cond, 0); + crhs2 = TREE_OPERAND (cond, 1); + } + else + return 0; + } + else + { + enum tree_code sc = gimple_assign_rhs_code (cur_use_stmt); + tree castlhs = gimple_assign_lhs (cur_use_stmt); + if (!CONVERT_EXPR_CODE_P (sc) + || !castlhs + || !INTEGRAL_TYPE_P (TREE_TYPE (castlhs)) + || (TYPE_PRECISION (TREE_TYPE (castlhs)) + > TYPE_PRECISION (TREE_TYPE (maxval)))) + return 0; + return 1; + } + } + else + return 0; + if ((ccode != EQ_EXPR && ccode != NE_EXPR) + || crhs1 != shiftlhs + || !integer_zerop (crhs2)) + return 0; + return 1; + } + if (TREE_CODE_CLASS (ccode) != tcc_comparison) return 0; @@ -4049,6 +4109,7 @@ arith_overflow_check_p (gimple *stmt, gi _8 = IMAGPART_EXPR <_7>; if (_8) and replace (utype) x with _9. + Or with x >> popcount (max) instead of x > max. Also recognize: x = ~z; @@ -4481,10 +4542,62 @@ match_arith_overflow (gimple_stmt_iterat gcc_checking_assert (is_gimple_assign (use_stmt)); if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS) { - gimple_assign_set_rhs1 (use_stmt, ovf); - gimple_assign_set_rhs2 (use_stmt, build_int_cst (type, 0)); - gimple_assign_set_rhs_code (use_stmt, - ovf_use == 1 ? NE_EXPR : EQ_EXPR); + if (gimple_assign_rhs_code (use_stmt) == RSHIFT_EXPR) + { + g2 = gimple_build_assign (make_ssa_name (boolean_type_node), + ovf_use == 1 ? NE_EXPR : EQ_EXPR, + ovf, build_int_cst (type, 0)); + gimple_stmt_iterator gsiu = gsi_for_stmt (use_stmt); + gsi_insert_before (&gsiu, g2, GSI_SAME_STMT); + gimple_assign_set_rhs_with_ops (&gsiu, NOP_EXPR, + gimple_assign_lhs (g2)); + update_stmt (use_stmt); + use_operand_p use; + single_imm_use (gimple_assign_lhs (use_stmt), &use, + &use_stmt); + if (gimple_code (use_stmt) == GIMPLE_COND) + { + gcond *cond_stmt = as_a (use_stmt); + gimple_cond_set_lhs (cond_stmt, ovf); + gimple_cond_set_rhs (cond_stmt, build_int_cst (type, 0)); + } + else + { + gcc_checking_assert (is_gimple_assign (use_stmt)); + if (gimple_assign_rhs_class (use_stmt) + == GIMPLE_BINARY_RHS) + { + gimple_assign_set_rhs1 (use_stmt, ovf); + gimple_assign_set_rhs2 (use_stmt, + build_int_cst (type, 0)); + } + else if (gimple_assign_cast_p (use_stmt)) + gimple_assign_set_rhs1 (use_stmt, ovf); + else + { + tree_code sc = gimple_assign_rhs_code (use_stmt); + gcc_checking_assert (sc == COND_EXPR); + tree cond = gimple_assign_rhs1 (use_stmt); + cond = build2 (TREE_CODE (cond), + boolean_type_node, ovf, + build_int_cst (type, 0)); + gimple_assign_set_rhs1 (use_stmt, cond); + } + } + update_stmt (use_stmt); + gsi_remove (&gsiu, true); + gsiu = gsi_for_stmt (g2); + gsi_remove (&gsiu, true); + continue; + } + else + { + gimple_assign_set_rhs1 (use_stmt, ovf); + gimple_assign_set_rhs2 (use_stmt, build_int_cst (type, 0)); + gimple_assign_set_rhs_code (use_stmt, + ovf_use == 1 + ? NE_EXPR : EQ_EXPR); + } } else { --- gcc/testsuite/gcc.dg/pr113982.c.jj 2024-05-10 15:00:28.536651833 +0200 +++ gcc/testsuite/gcc.dg/pr113982.c 2024-05-10 15:01:49.721570343 +0200 @@ -0,0 +1,60 @@ +/* PR middle-end/113982 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-widening_mul" } */ + +#if __SIZEOF_INT128__ +typedef __uint128_t W; +typedef unsigned long long T; +#else +typedef unsigned long long W; +typedef unsigned int T; +#endif +#define B __CHAR_BIT__ * sizeof (T) + +struct S { int p; T r; }; + +struct S +foo (T x, T y) +{ + W z = (W) x + y; + return (struct S) { z >> B, (T) z }; +} + +struct S +bar (T x) +{ + W z = (W) x + 132; + return (struct S) { z >> B, (T) z }; +} + +struct S +baz (T x, unsigned short y) +{ + W z = (W) x + y; + return (struct S) { z >> B, (T) z }; +} + +struct S +qux (unsigned short x, T y) +{ + W z = (W) x + y; + return (struct S) { z >> B, (T) z }; +} + +struct S +corge (T x, T y) +{ + T w = x + y; + W z = (W) x + y; + return (struct S) { z >> B, w }; +} + +struct S +garple (T x, T y) +{ + W z = (W) x + y; + T w = x + y; + return (struct S) { z >> B, w }; +} + +/* { dg-final { scan-tree-dump-times "ADD_OVERFLOW" 6 "widening_mul" { target { i?86-*-* x86_64-*-* } } } } */