From patchwork Tue Mar 24 00:00:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 1260405 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=IJt6nL0g; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48mWbS3mHMz9sR4 for ; Tue, 24 Mar 2020 11:00:17 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 974E9385B834; Tue, 24 Mar 2020 00:00:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 974E9385B834 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1585008013; bh=wSrsCeN+kwRZupXrl7LczSm0/0fnVsi9YoOgvZHlF5g=; h=Subject:To:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IJt6nL0gbI7j4iAslJsKGO7Ax4Peh/Kq/PyarRlKLZzHdNh8TSgxXBlGbx4mHWADp bcElCHEPJRBB2Zvse4sX94e3FkVihyAftssMPGhcxep7UeVbQKmjjEMJzVZftjjRRi Ii2BhZoaIpwJkaQnt/MYgRbFJNqNKCR6aMeedzHk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [63.128.21.74]) by sourceware.org (Postfix) with ESMTP id 3401B385B833 for ; Tue, 24 Mar 2020 00:00:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3401B385B833 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-408-IDlDV1k4PcezQ81OUzfrlQ-1; Mon, 23 Mar 2020 20:00:07 -0400 X-MC-Unique: IDlDV1k4PcezQ81OUzfrlQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C56A8107ACC4 for ; Tue, 24 Mar 2020 00:00:06 +0000 (UTC) Received: from ovpn-114-136.phx2.redhat.com (ovpn-114-136.phx2.redhat.com [10.3.114.136]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8DE11A0A66 for ; Tue, 24 Mar 2020 00:00:06 +0000 (UTC) Message-ID: Subject: [committed] [P1][PR target/94238] Don't create invalid comparisons To: gcc-patches List Date: Mon, 23 Mar 2020 18:00:06 -0600 Organization: Red Hat User-Agent: Evolution 3.34.4 (3.34.4-1.fc31) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-33.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Jeff Law via Gcc-patches From: "Li, Pan2 via Gcc-patches" Reply-To: law@redhat.com Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" As outlined in the BZ simplify_logical_relational_operation does not validate that the comparison code it selects is ultimately valid for the mode of the comparison. So we could have something like: (ior (lt (...)) (gt (...)) Which it happily turns into (ltgt (...)) Which is not valid for integral modes. This patch validates the selected comparison code is valid for the comparison's mode using the rules in rtl.def. It's always safe to reject a particular simplification. I've also verified this fixes 94144 which is a similar problem showing up on aarch64 and that it doesn't cause regressions in the tester. I'm committing this to the trunk. Jeff commit fd25ae64e3ffaa95abb60e74af45031d2f5f900f Author: Jeff Law Date: Mon Mar 23 17:55:20 2020 -0600 Verify the code used for the optimized comparison is valid for the comparison's mode. PR rtl-optimization/90275 PR target/94238 PR target/94144 * simplify-rtx.c (comparison_code_valid_for_mode): New function. (simplify_logical_relational_operation): Use it. PR target/94144 PR target/94238 * gcc.c-torture/compile/pr94144.c: New test. * gcc.c-torture/compile/pr94238.c: New test. diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 9ba58273c21..3b926703957 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2020-03-23 Jeff Law + + PR rtl-optimization/90275 + PR target/94238 + PR target/94144 + * simplify-rtx.c (comparison_code_valid_for_mode): New function. + (simplify_logical_relational_operation): Use it. + 2020-03-23 Jakub Jelinek PR c++/91993 diff --git a/gcc/simplify-rtx.c b/gcc/simplify-rtx.c index dd3d85156c3..28c2dc69ae7 100644 --- a/gcc/simplify-rtx.c +++ b/gcc/simplify-rtx.c @@ -2215,6 +2215,53 @@ mask_to_comparison (int mask) } } +/* Return true if CODE is valid for comparisons of mode MODE, false + otherwise. + + It is always safe to return false, even if the code was valid for the + given mode as that will merely suppress optimizations. */ + +static bool +comparison_code_valid_for_mode (enum rtx_code code, enum machine_mode mode) +{ + switch (code) + { + /* These are valid for integral, floating and vector modes. */ + case NE: + case EQ: + case GE: + case GT: + case LE: + case LT: + return (INTEGRAL_MODE_P (mode) + || FLOAT_MODE_P (mode) + || VECTOR_MODE_P (mode)); + + /* These are valid for floating point modes. */ + case LTGT: + case UNORDERED: + case ORDERED: + case UNEQ: + case UNGE: + case UNGT: + case UNLE: + case UNLT: + return FLOAT_MODE_P (mode); + + /* These are filtered out in simplify_logical_operation, but + we check for them too as a matter of safety. They are valid + for integral and vector modes. */ + case GEU: + case GTU: + case LEU: + case LTU: + return INTEGRAL_MODE_P (mode) || VECTOR_MODE_P (mode); + + default: + gcc_unreachable (); + } +} + /* Simplify a logical operation CODE with result mode MODE, operating on OP0 and OP1, which should be both relational operations. Return 0 if no such simplification is possible. */ @@ -2252,6 +2299,10 @@ simplify_logical_relational_operation (enum rtx_code code, machine_mode mode, code = mask_to_comparison (mask); + /* Many comparison codes are only valid for certain mode classes. */ + if (!comparison_code_valid_for_mode (code, mode)) + return 0; + op0 = XEXP (op1, 0); op1 = XEXP (op1, 1); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 5f079f1fca9..28adfd8b580 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2020-03-23 Jeff Law + + PR target/94144 + PR target/94238 + * gcc.c-torture/compile/pr94144.c: New test. + * gcc.c-torture/compile/pr94238.c: New test. + 2020-03-23 Patrick Palka PR c++/93805 diff --git a/gcc/testsuite/gcc.c-torture/compile/pr94144.c b/gcc/testsuite/gcc.c-torture/compile/pr94144.c new file mode 100644 index 00000000000..4358e0a7b00 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr94144.c @@ -0,0 +1,18 @@ + +int a, b, z; +int c(int d, int e) { return d && e > 0 && d > 5 - e ? 0 : d + e; } +int k(); +void h(int); +void f(short d) { + int g = !(0 < d); + h(d); + if (b) { + unsigned i[1]; + i[0] = g = 0; + for (; g <= 8; g++) + d || k(); + if (c(!(i[0] <= z) >= d, d) != a) + k(); + } +} + diff --git a/gcc/testsuite/gcc.c-torture/compile/pr94238.c b/gcc/testsuite/gcc.c-torture/compile/pr94238.c new file mode 100644 index 00000000000..5a96a64d85b --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr94238.c @@ -0,0 +1,22 @@ +enum { false, true } a; +int b, c, d, e, f; +int fn3(); +void fn2(); + +void fn1() { + _Bool g, h = false, i = false; + int j; + c = b && f || d; + if (c) { + if (d) + i = true; + _Bool k = b; + int l = e, m = a; + g = k && l < m || l > m; + } + if (g) + h = true; + if (i) + fn2(); + h &&j &&fn3(); +}