From patchwork Fri May 19 08:06:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 96251 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1064433vqo; Fri, 19 May 2023 01:06:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6qt8R0ZRd5+6nerj9+OrxyFUNNkSe7LCzgaDi+vcWRkBrFlPlBUFbvp/s7gdgOqNdnGj4S X-Received: by 2002:aa7:d3d3:0:b0:510:503e:1a1b with SMTP id o19-20020aa7d3d3000000b00510503e1a1bmr1261593edr.25.1684483617608; Fri, 19 May 2023 01:06:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684483617; cv=none; d=google.com; s=arc-20160816; b=uLc72heQnIzmWMMjm5H6G8usyFzCy/WLXSQiaQkqJN4h2WRmDaAqry2JTufIDxyx3T RcVjQQLYMagyLhiqxLQ3NE3M0BDv420Q3MXYE9f+OEHeHC1QEwStszUm+z/7Xvoo9YmQ F/KUMCLFxTI+uZZAInMV7TE5VTcg7A1RzCW0c3toSHcTbzmZBjJlqWhZ9oss1dLCbqqE YkuM8BlsXddm8IUTeDGmm0qCHAcZJ58zcgxyYgwFoFWi0bOKoabMOtDeplgk1M7fPwju gXYNxOaTNz6xpPQ9ZKiKQSAyq9KBVnEiS68MWOber6DSAISNB5BwAeN0nVouIKlLRui6 v/Yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=Q/amxlICHhRN/c0XO3zA2XFiB8aaQpRrOc/4cHF3Rto=; b=XHfdvhNTmfHt+0u8XxI9LGBfV20PcmD4hrmKEqsUFV3quIQYgJXtOd3TRrKNfnycTX OcvzXzLw57GCd8mL+B7sjynAObFtRZxL6Ig9QGuXjByw1+YQUYLWpUbfIoXySbgsvfnl E9fSXro8pQrrcRkocBsEPEb4dLxBzJ04edWNKmZ3Ca2NGzRCh1GembHJ2xHTwdR//mf0 Ph15omyDeus8aQza5o53XUxsYPo/MEPttWwXluj1u7DJQEi+3FHBi89P4DQ5R5M8w4Ao 1msH8gunOT2zRY9zn9SVExCIQOexIQ3bX3xHGdx5CzBDyBdZsA0eUrxvzX8vgDX5e+9y wr4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=iFNo+Cyc; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id c16-20020a056402121000b005083e031fd0si2563884edw.321.2023.05.19.01.06.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 May 2023 01:06:57 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=iFNo+Cyc; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3C2593858415 for ; Fri, 19 May 2023 08:06:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3C2593858415 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684483616; bh=Q/amxlICHhRN/c0XO3zA2XFiB8aaQpRrOc/4cHF3Rto=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=iFNo+Cyc2bTn872Fce2pkugXvxjNYerjUxH8AH1qGiAfeVeKVGwPlBnAVSbej6kIG AxIAMUmj+r+8ut4HSYeoECPv4ijanpFKVXO9ewm8VKoxmCxY9SrY1U5hZwrzBTO4bm wXwpERWyFtWczyXofx1BLY5DHQ69YHazDKRg0NyU= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 2F37F3858D28 for ; Fri, 19 May 2023 08:06:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2F37F3858D28 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-451-HlwXEtX7NPqLKYMfzZ2OMw-1; Fri, 19 May 2023 04:06:08 -0400 X-MC-Unique: HlwXEtX7NPqLKYMfzZ2OMw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 03A4B8032F1; Fri, 19 May 2023 08:06:08 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B8E9F2026D49; Fri, 19 May 2023 08:06:07 +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 34J865aJ3569547 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 19 May 2023 10:06:05 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 34J864hC3569546; Fri, 19 May 2023 10:06:04 +0200 Date: Fri, 19 May 2023 10:06:04 +0200 To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-ssa-math-opts: Pattern recognize some further hand written forms of signed __builtin_mul_overflow{,_p} [PR105776] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766309093817247913?= X-GMAIL-MSGID: =?utf-8?q?1766309093817247913?= Hi! In the pattern recognition of signed __builtin_mul_overflow{,_p} we check for result of unsigned division (which follows unsigned multiplication) being equality compared against one of the multiplication's argument (the one not used in the division) and check for the comparison to be done against same precision cast of the argument (because division's result is unsigned and the argument is signed). But as shown in this PR, one can write it equally as comparison done in the signed type, i.e. compare division's result cast to corresponding signed type against the argument. The following patch handles even those cases. Bootstrapped/regtested on x86_64-linux, i686-linux, aarch64-linux and powerpc64le-linux, ok for trunk? 2023-05-19 Jakub Jelinek PR tree-optimization/105776 * tree-ssa-math-opts.cc (arith_overflow_check_p): If cast_stmt is non-NULL, allow division statement to have a cast as single imm use rather than comparison/condition. (match_arith_overflow): In that case remove the cast stmt in addition to the division statement. * gcc.target/i386/pr105776.c: New test. Jakub --- gcc/tree-ssa-math-opts.cc.jj 2023-05-18 14:57:13.216409685 +0200 +++ gcc/tree-ssa-math-opts.cc 2023-05-18 15:45:34.077177053 +0200 @@ -3802,6 +3802,21 @@ arith_overflow_check_p (gimple *stmt, gi use_operand_p use; if (!single_imm_use (divlhs, &use, &cur_use_stmt)) return 0; + if (cast_stmt && gimple_assign_cast_p (cur_use_stmt)) + { + tree cast_lhs = gimple_assign_lhs (cur_use_stmt); + if (INTEGRAL_TYPE_P (TREE_TYPE (cast_lhs)) + && TYPE_UNSIGNED (TREE_TYPE (cast_lhs)) + && (TYPE_PRECISION (TREE_TYPE (cast_lhs)) + == TYPE_PRECISION (TREE_TYPE (divlhs))) + && single_imm_use (cast_lhs, &use, &cur_use_stmt)) + { + cast_stmt = NULL; + divlhs = cast_lhs; + } + else + return 0; + } } if (gimple_code (cur_use_stmt) == GIMPLE_COND) { @@ -4390,6 +4405,16 @@ match_arith_overflow (gimple_stmt_iterat gimple_stmt_iterator gsi2 = gsi_for_stmt (orig_use_stmt); maybe_optimize_guarding_check (mul_stmts, use_stmt, orig_use_stmt, cfg_changed); + use_operand_p use; + gimple *cast_stmt; + if (single_imm_use (gimple_assign_lhs (orig_use_stmt), &use, + &cast_stmt) + && gimple_assign_cast_p (cast_stmt)) + { + gimple_stmt_iterator gsi3 = gsi_for_stmt (cast_stmt); + gsi_remove (&gsi3, true); + release_ssa_name (gimple_assign_lhs (cast_stmt)); + } gsi_remove (&gsi2, true); release_ssa_name (gimple_assign_lhs (orig_use_stmt)); } --- gcc/testsuite/gcc.target/i386/pr105776.c.jj 2023-05-18 15:57:15.570218802 +0200 +++ gcc/testsuite/gcc.target/i386/pr105776.c 2023-05-18 15:56:55.273506918 +0200 @@ -0,0 +1,43 @@ +/* PR tree-optimization/105776 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized -masm=att" } */ +/* { dg-final { scan-tree-dump-times " = \.MUL_OVERFLOW " 5 "optimized" } } */ +/* { dg-final { scan-assembler-times "\timull\t" 5 } } */ +/* { dg-final { scan-assembler-times "\tsetno\t" 5 } } */ + +int +foo (unsigned x, unsigned y) +{ + unsigned int r = x * y; + return !x || ((int) r / (int) x) == (int) y; +} + +int +bar (unsigned x, unsigned y) +{ + return !x || ((int) (x * y) / (int) x) == (int) y; +} + +int +baz (unsigned x, unsigned y) +{ + if (x == 0) + return 1; + return ((int) (x * y) / (int) x) == y; +} + +int +qux (unsigned x, unsigned y, unsigned *z) +{ + unsigned int r = x * y; + *z = r; + return !x || ((int) r / (int) x) == (int) y; +} + +int +corge (unsigned x, unsigned y, unsigned *z) +{ + unsigned int r = x * y; + *z = r; + return !x || ((int) r / (int) x) == y; +}