From patchwork Sat May 4 08:30:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 1931244 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=eBUDQKxk; 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 4VWgvM2cl6z1xnS for ; Sat, 4 May 2024 18:34:23 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8C8CE386F467 for ; Sat, 4 May 2024 08:34:21 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-44.mimecast.com (us-smtp-delivery-44.mimecast.com [205.139.111.44]) by sourceware.org (Postfix) with ESMTPS id 11434384477E for ; Sat, 4 May 2024 08:31:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 11434384477E Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=localhost.redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 11434384477E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.139.111.44 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714811476; cv=none; b=ZoJHQOwj5lhI5kGTMHodFvYPYMllwFgT3BZmJ9OsRW3nxDC5cU/w0X7oLgftMgChNITHlKWUuMAqoLFSAxbbt0vCOfoQQY6q4Rgv4Jn/8hzGqIUJpTaA97HXn2h3Ci1R/dIREK3SP3lSc3nZzcWY7YOX5HN0XkqRXy59+F5TSAk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714811476; c=relaxed/simple; bh=Bs8cfFEZMvYED9G5Iz4ZmoacuN42K2x67SB6HZ5AhbM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=XKd+N+cdNfkhtAP/+TVglH7NiDdqTl5oqssbOEi2CElKSguIXrqEAulpV/53dcvTR5L7pLXlfNfFQdk7a1O/4oGcX4iYV8YR13/Nq7Qn4OThhclicXY6QjJGXVTVvDB+E/rDh4Tbo0NyW3hZFKKlFYq0w1ZWe5823UFbX91ptwQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714811464; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FedtA4gKRdO4adKqhCcyCkUqACmU8Hg3QiKa8Xz/gaM=; b=eBUDQKxkB8ExnEN2WbNgClP8PufCtXzSJtkIz5T0avqQS3kLt2slli20G8WvG0TNISolBL hXk1Xx00JWdQ3r12NtQDEh/q0oyh0Dg20/hEXhw+2Ev0GTLwPPIQ/eCMHMXE385JoFFE8H BztnlwyhkM1JfzLz3o38rFvhqli/yrc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-596-1VZ8BxupMKGr6pQSrXfwoQ-1; Sat, 04 May 2024 04:31:03 -0400 X-MC-Unique: 1VZ8BxupMKGr6pQSrXfwoQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (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 E7F9F18065AB for ; Sat, 4 May 2024 08:31:02 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.71]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A420A16D94; Sat, 4 May 2024 08:31:02 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.18.1/8.17.1) with ESMTPS id 4448V1YS139860 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 4 May 2024 10:31:01 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.18.1/8.18.1/Submit) id 4448V13K139859; Sat, 4 May 2024 10:31:01 +0200 From: Aldy Hernandez To: GCC patches Cc: Andrew MacLeod , Aldy Hernandez Subject: [COMMITTED 19/23] Implement operator_lt for prange. Date: Sat, 4 May 2024 10:30:47 +0200 Message-ID: <20240504083056.139719-20-aldyh@redhat.com> In-Reply-To: <20240504083056.139719-1-aldyh@redhat.com> References: <20240504083056.139719-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NO_DNS_FOR_FROM, RCVD_IN_DNSWL_LOW, 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org gcc/ChangeLog: * range-op-mixed.h: Add overloaded declarations for pointer variants. * range-op-ptr.cc (max_limit): New. (min_limit): New. (build_lt): New. (build_le): New. (build_gt): New. (build_ge): New. (operator_lt::fold_range): New. (operator_lt::op1_range): New. (operator_lt::op2_range): New. (operator_lt::op1_op2_relation): New. (operator_lt::pointers_handled_p): New. --- gcc/range-op-mixed.h | 12 +++ gcc/range-op-ptr.cc | 174 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 186 insertions(+) diff --git a/gcc/range-op-mixed.h b/gcc/range-op-mixed.h index ee8d9dd328f..b82d06572a7 100644 --- a/gcc/range-op-mixed.h +++ b/gcc/range-op-mixed.h @@ -219,23 +219,34 @@ public: bool fold_range (irange &r, tree type, const irange &op1, const irange &op2, relation_trio = TRIO_VARYING) const final override; + bool fold_range (irange &r, tree type, + const prange &op1, const prange &op2, + relation_trio = TRIO_VARYING) const final override; bool fold_range (irange &r, tree type, const frange &op1, const frange &op2, relation_trio = TRIO_VARYING) const final override; bool op1_range (irange &r, tree type, const irange &lhs, const irange &op2, relation_trio = TRIO_VARYING) const final override; + bool op1_range (prange &r, tree type, + const irange &lhs, const prange &op2, + relation_trio = TRIO_VARYING) const final override; bool op1_range (frange &r, tree type, const irange &lhs, const frange &op2, relation_trio = TRIO_VARYING) const final override; bool op2_range (irange &r, tree type, const irange &lhs, const irange &op1, relation_trio = TRIO_VARYING) const final override; + bool op2_range (prange &r, tree type, + const irange &lhs, const prange &op1, + relation_trio = TRIO_VARYING) const final override; bool op2_range (frange &r, tree type, const irange &lhs, const frange &op1, relation_trio = TRIO_VARYING) const final override; relation_kind op1_op2_relation (const irange &lhs, const irange &, const irange &) const final override; + relation_kind op1_op2_relation (const irange &lhs, const prange &, + const prange &) const final override; relation_kind op1_op2_relation (const irange &lhs, const frange &, const frange &) const final override; void update_bitmask (irange &r, const irange &lh, @@ -243,6 +254,7 @@ public: // Check op1 and op2 for compatibility. bool operand_check_p (tree, tree t1, tree t2) const final override { return range_compatible_p (t1, t2); } + bool pointers_handled_p (range_op_dispatch_type, unsigned) const final override; }; class operator_le : public range_operator diff --git a/gcc/range-op-ptr.cc b/gcc/range-op-ptr.cc index fb2888bf079..11629ba6d8d 100644 --- a/gcc/range-op-ptr.cc +++ b/gcc/range-op-ptr.cc @@ -262,6 +262,69 @@ range_operator::update_bitmask (irange &, { } +// Return the upper limit for a type. + +static inline wide_int +max_limit (const_tree type) +{ + return wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type)); +} + +// Return the lower limit for a type. + +static inline wide_int +min_limit (const_tree type) +{ + return wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type)); +} + +// Build a range that is < VAL and store it in R. + +static void +build_lt (prange &r, tree type, const prange &val) +{ + wi::overflow_type ov; + wide_int lim = wi::sub (val.upper_bound (), 1, UNSIGNED, &ov); + + // If val - 1 underflows, check if X < MIN, which is an empty range. + if (ov) + r.set_undefined (); + else + r.set (type, min_limit (type), lim); +} + +// Build a range that is <= VAL and store it in R. + +static void +build_le (prange &r, tree type, const prange &val) +{ + r.set (type, min_limit (type), val.upper_bound ()); +} + +// Build a range that is > VAL and store it in R. + +static void +build_gt (prange &r, tree type, const prange &val) +{ + wi::overflow_type ov; + wide_int lim = wi::add (val.lower_bound (), 1, UNSIGNED, &ov); + + // If val + 1 overflows, check is for X > MAX, which is an empty range. + if (ov) + r.set_undefined (); + else + r.set (type, lim, max_limit (type)); + +} + +// Build a range that is >= VAL and store it in R. + +static void +build_ge (prange &r, tree type, const prange &val) +{ + r.set (type, val.lower_bound (), max_limit (type)); +} + class pointer_plus_operator : public range_operator { using range_operator::update_bitmask; @@ -1465,6 +1528,117 @@ operator_not_equal::pointers_handled_p (range_op_dispatch_type type, } } +bool +operator_lt::fold_range (irange &r, tree type, + const prange &op1, + const prange &op2, + relation_trio rel) const +{ + if (relop_early_resolve (r, type, op1, op2, rel, VREL_LT)) + return true; + + signop sign = TYPE_SIGN (op1.type ()); + gcc_checking_assert (sign == TYPE_SIGN (op2.type ())); + + if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign)) + r = range_true (); + else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign)) + r = range_false (); + // Use nonzero bits to determine if < 0 is false. + else if (op2.zero_p () && !wi::neg_p (op1.get_nonzero_bits (), sign)) + r = range_false (); + else + r = range_true_and_false (); + + //update_known_bitmask (r, LT_EXPR, op1, op2); + return true; +} + +bool +operator_lt::op1_range (prange &r, tree type, + const irange &lhs, + const prange &op2, + relation_trio) const +{ + if (op2.undefined_p ()) + return false; + + switch (get_bool_state (r, lhs, type)) + { + case BRS_TRUE: + build_lt (r, type, op2); + break; + + case BRS_FALSE: + build_ge (r, type, op2); + break; + + default: + break; + } + return true; +} + +bool +operator_lt::op2_range (prange &r, tree type, + const irange &lhs, + const prange &op1, + relation_trio) const +{ + if (op1.undefined_p ()) + return false; + + switch (get_bool_state (r, lhs, type)) + { + case BRS_TRUE: + build_gt (r, type, op1); + break; + + case BRS_FALSE: + build_le (r, type, op1); + break; + + default: + break; + } + return true; +} + +relation_kind +operator_lt::op1_op2_relation (const irange &lhs, const prange &, + const prange &) const +{ + if (lhs.undefined_p ()) + return VREL_UNDEFINED; + + // FALSE = op1 < op2 indicates GE_EXPR. + if (lhs.zero_p ()) + return VREL_GE; + + // TRUE = op1 < op2 indicates LT_EXPR. + if (!range_includes_zero_p (lhs)) + return VREL_LT; + return VREL_VARYING; +} + +bool +operator_lt::pointers_handled_p (range_op_dispatch_type type, + unsigned dispatch) const +{ + switch (type) + { + case DISPATCH_FOLD_RANGE: + return dispatch == RO_IPP; + case DISPATCH_OP1_RANGE: + case DISPATCH_OP2_RANGE: + return dispatch == RO_PIP; + case DISPATCH_OP1_OP2_RELATION: + return dispatch == RO_IPP; + default: + return true; + } +} + // Initialize any pointer operators to the primary table void