From patchwork Fri Feb 3 18:59:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 64277 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 309543875DF7 for ; Fri, 3 Feb 2023 20:15:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 309543875DF7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675455306; bh=H5pODeunGHnQIJ+XCQ2abBHLivEQuFSS+pp1XQUEhnU=; h=Resent-From:Resent-Date:Resent-To:Date:To:Cc:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=P6sUYKb1eqrkMtb814Ht+6rApHMRr5Pljxet6UJpudgu2nf0/JPFf/Lc97Z6W0FP7 QMmGbW10E/GpYOqhtJTxwfQpPaZqLepxGJS3weuJrUSw2ORJga0wUn8oEEUsnwYy3A ZtRwY6vufRAisaobEQCZi/yX83q1myNBpBJhhE1Q= 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 A6D63385417F for ; Fri, 3 Feb 2023 20:13:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A6D63385417F 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-539-vt-lycaJMYifhD0hqxLYuQ-1; Fri, 03 Feb 2023 15:13:49 -0500 X-MC-Unique: vt-lycaJMYifhD0hqxLYuQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BFB1D85A5A3 for ; Fri, 3 Feb 2023 20:13:48 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.223]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8036940149B6; Fri, 3 Feb 2023 20:13:48 +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 313KDkT81415124 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 3 Feb 2023 21:13:46 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 313KDj2g1415123; Fri, 3 Feb 2023 21:13:45 +0100 Resent-From: Jakub Jelinek Resent-Date: Fri, 3 Feb 2023 21:13:45 +0100 Resent-Message-ID: Resent-To: Aldy Hernandez , Andrew MacLeod , gcc-patches@gcc.gnu.org Date: Fri, 3 Feb 2023 19:59:39 +0100 To: Aldy Hernandez , Andrew MacLeod Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] range-op: Handle op?.undefined_p () in op[12]_range of comparisons [PR108647] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 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_H2, 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.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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi! As mentioned in the PR, we ICE because lhs is singleton [0, 0] or [1, 1] but op2 (or in other cases op1) is undefined and op?.*_bound () ICEs on those because there are no pairs for UNDEFINED. The following patch makes us set r to varying or return false in those cases. Included is a version of the patch I've bootstrapped/regtested successfully on x86_64-linux and i686-linux, attached is a slight modification more consistent with the range-op-float.cc patch. Ok for trunk (and which one)? 2023-02-03 Jakub Jelinek PR tree-optimization/108647 * range-op.cc (operator_equal::op1_range, operator_not_equal::op1_range): Don't test op2 bound equality if op2.undefined_p (), instead set_varying. (operator_lt::op1_range, operator_le::op1_range, operator_gt::op1_range, operator_ge::op1_range): Return false if op2.undefined_p (). (operator_lt::op2_range, operator_le::op2_range, operator_gt::op2_range, operator_ge::op2_range): Return false if op1.undefined_p (). * g++.dg/torture/pr108647.C: New test. Jakub 2023-02-03 Jakub Jelinek PR tree-optimization/108647 * range-op.cc (operator_equal::op1_range, operator_not_equal::op1_range): Don't test op2 bound equality if op2.undefined_p (), instead set_varying. (operator_lt::op1_range, operator_le::op1_range, operator_gt::op1_range, operator_ge::op1_range): Return false if op2.undefined_p (). (operator_lt::op2_range, operator_le::op2_range, operator_gt::op2_range, operator_ge::op2_range): Return false if op1.undefined_p (). * g++.dg/torture/pr108647.C: New test. --- gcc/range-op.cc.jj 2023-02-03 10:51:40.699003658 +0100 +++ gcc/range-op.cc 2023-02-03 19:43:28.082326069 +0100 @@ -642,7 +642,8 @@ operator_equal::op1_range (irange &r, tr case BRS_FALSE: // If the result is false, the only time we know anything is // if OP2 is a constant. - if (wi::eq_p (op2.lower_bound(), op2.upper_bound())) + if (!op2.undefined_p () + && wi::eq_p (op2.lower_bound(), op2.upper_bound())) { r = op2; r.invert (); @@ -755,7 +756,8 @@ operator_not_equal::op1_range (irange &r case BRS_TRUE: // If the result is true, the only time we know anything is if // OP2 is a constant. - if (wi::eq_p (op2.lower_bound(), op2.upper_bound())) + if (!op2.undefined_p () + && wi::eq_p (op2.lower_bound(), op2.upper_bound())) { r = op2; r.invert (); @@ -923,10 +925,14 @@ operator_lt::op1_range (irange &r, tree switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: + if (op2.undefined_p ()) + return false; build_lt (r, type, op2.upper_bound ()); break; case BRS_FALSE: + if (op2.undefined_p ()) + return false; build_ge (r, type, op2.lower_bound ()); break; @@ -945,10 +951,14 @@ operator_lt::op2_range (irange &r, tree switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: + if (op1.undefined_p ()) + return false; build_gt (r, type, op1.lower_bound ()); break; case BRS_FALSE: + if (op1.undefined_p ()) + return false; build_le (r, type, op1.upper_bound ()); break; @@ -1034,10 +1044,14 @@ operator_le::op1_range (irange &r, tree switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: + if (op2.undefined_p ()) + return false; build_le (r, type, op2.upper_bound ()); break; case BRS_FALSE: + if (op2.undefined_p ()) + return false; build_gt (r, type, op2.lower_bound ()); break; @@ -1056,10 +1070,14 @@ operator_le::op2_range (irange &r, tree switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: + if (op1.undefined_p ()) + return false; build_ge (r, type, op1.lower_bound ()); break; case BRS_FALSE: + if (op1.undefined_p ()) + return false; build_lt (r, type, op1.upper_bound ()); break; @@ -1144,10 +1162,14 @@ operator_gt::op1_range (irange &r, tree switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: + if (op2.undefined_p ()) + return false; build_gt (r, type, op2.lower_bound ()); break; case BRS_FALSE: + if (op2.undefined_p ()) + return false; build_le (r, type, op2.upper_bound ()); break; @@ -1166,10 +1188,14 @@ operator_gt::op2_range (irange &r, tree switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: + if (op1.undefined_p ()) + return false; build_lt (r, type, op1.upper_bound ()); break; case BRS_FALSE: + if (op1.undefined_p ()) + return false; build_ge (r, type, op1.lower_bound ()); break; @@ -1255,10 +1281,14 @@ operator_ge::op1_range (irange &r, tree switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: + if (op2.undefined_p ()) + return false; build_ge (r, type, op2.lower_bound ()); break; case BRS_FALSE: + if (op2.undefined_p ()) + return false; build_lt (r, type, op2.upper_bound ()); break; @@ -1277,10 +1307,14 @@ operator_ge::op2_range (irange &r, tree switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: + if (op1.undefined_p ()) + return false; build_le (r, type, op1.upper_bound ()); break; case BRS_FALSE: + if (op1.undefined_p ()) + return false; build_gt (r, type, op1.lower_bound ()); break; --- gcc/testsuite/g++.dg/torture/pr108647.C.jj 2023-02-03 16:36:18.347255058 +0100 +++ gcc/testsuite/g++.dg/torture/pr108647.C 2023-02-03 16:32:16.338811259 +0100 @@ -0,0 +1,25 @@ +// PR tree-optimization/108647 +// { dg-do compile } + +bool a; +int b, c; + +inline const bool & +foo (bool &e, const bool &f) +{ + return f < e ? f : e; +} + +void +bar (signed char e, bool *f, bool *h, bool *g) +{ + for (;;) + if (g) + for (signed char j = 0; j < 6; + j += ((f[0] & c ? g[0] : int(0 >= e)) + ? 0 : foo (g[0], g[0] > h[0]) + 1)) + { + a = 0; + b = 0; + } +} --- gcc/range-op.cc.jj 2023-02-03 10:51:40.699003658 +0100 +++ gcc/range-op.cc 2023-02-03 17:26:02.204429931 +0100 @@ -642,7 +642,8 @@ operator_equal::op1_range (irange &r, tr case BRS_FALSE: // If the result is false, the only time we know anything is // if OP2 is a constant. - if (wi::eq_p (op2.lower_bound(), op2.upper_bound())) + if (!op2.undefined_p () + && wi::eq_p (op2.lower_bound(), op2.upper_bound())) { r = op2; r.invert (); @@ -755,7 +756,8 @@ operator_not_equal::op1_range (irange &r case BRS_TRUE: // If the result is true, the only time we know anything is if // OP2 is a constant. - if (wi::eq_p (op2.lower_bound(), op2.upper_bound())) + if (!op2.undefined_p () + && wi::eq_p (op2.lower_bound(), op2.upper_bound())) { r = op2; r.invert (); @@ -920,6 +922,9 @@ operator_lt::op1_range (irange &r, tree const irange &op2, relation_trio) const { + if (op2.undefined_p ()) + return false; + switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: @@ -942,6 +947,9 @@ operator_lt::op2_range (irange &r, tree const irange &op1, relation_trio) const { + if (op1.undefined_p ()) + return false; + switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: @@ -1031,6 +1039,9 @@ operator_le::op1_range (irange &r, tree const irange &op2, relation_trio) const { + if (op2.undefined_p ()) + return false; + switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: @@ -1053,6 +1064,9 @@ operator_le::op2_range (irange &r, tree const irange &op1, relation_trio) const { + if (op1.undefined_p ()) + return false; + switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: @@ -1141,6 +1155,9 @@ operator_gt::op1_range (irange &r, tree const irange &lhs, const irange &op2, relation_trio) const { + if (op2.undefined_p ()) + return false; + switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: @@ -1163,6 +1180,9 @@ operator_gt::op2_range (irange &r, tree const irange &op1, relation_trio) const { + if (op1.undefined_p ()) + return false; + switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: @@ -1252,6 +1272,9 @@ operator_ge::op1_range (irange &r, tree const irange &op2, relation_trio) const { + if (op2.undefined_p ()) + return false; + switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: @@ -1274,6 +1297,9 @@ operator_ge::op2_range (irange &r, tree const irange &op1, relation_trio) const { + if (op1.undefined_p ()) + return false; + switch (get_bool_state (r, lhs, type)) { case BRS_TRUE: --- gcc/testsuite/g++.dg/torture/pr108647.C.jj 2023-02-03 16:36:18.347255058 +0100 +++ gcc/testsuite/g++.dg/torture/pr108647.C 2023-02-03 16:32:16.338811259 +0100 @@ -0,0 +1,25 @@ +// PR tree-optimization/108647 +// { dg-do compile } + +bool a; +int b, c; + +inline const bool & +foo (bool &e, const bool &f) +{ + return f < e ? f : e; +} + +void +bar (signed char e, bool *f, bool *h, bool *g) +{ + for (;;) + if (g) + for (signed char j = 0; j < 6; + j += ((f[0] & c ? g[0] : int(0 >= e)) + ? 0 : foo (g[0], g[0] > h[0]) + 1)) + { + a = 0; + b = 0; + } +}