From patchwork Sat May 4 08:30:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 1931243 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=ES0XoirT; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4VWgvB63D2z23tp for ; Sat, 4 May 2024 18:34:14 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2230E386F44A for ; Sat, 4 May 2024 08:34:13 +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 [207.211.30.44]) by sourceware.org (Postfix) with ESMTPS id 4FCD3384476A for ; Sat, 4 May 2024 08:31:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4FCD3384476A 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 4FCD3384476A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=207.211.30.44 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714811472; cv=none; b=rcG8mgqLQ0otJvA+/MiZKQjSaTzmrr7Enl3o19J6yeoQQHRA/q3Twv8CgRvlHIadMpm92xtD7XYZ8ygurjiDYTG68C8R2ewG5pCQOkfHUFIXCjOk5gC6C8naQWmBD3Iq8PWHXiNnSQfrxhBiT7ZkoqX97wEtFCl0g+9TNfpv1R8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714811472; c=relaxed/simple; bh=QgcRjhvg80OawwsoVpv7g/T9T9dgfSsHhBdaJSmmucc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=S07kE6cA7y7MyBvr33v8LeoJakOzC6R4eoKY2JFVuv/UoihUNiJyVpfKFvfAcp7iA4mrs7zyuY0mEAsF/dKipdFjwK9nCXQReJvzPgqnrWA4KXelzlK4yFrLk55NWBcVT26ZbN0Eg+0x0FBc3lbQENonujnkZ4TL+V+C6Jo3a/I= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714811463; 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=3FkLwShHqoUssgOqMiJeCTslRf034wxlrtFWQ81Yev0=; b=ES0XoirT9RbbKDuuhBS8qXM4WN2jkJaFXiEV7nKeT0e4aZC0dr1R+2e5+2dpkqee/Ejk5S 2FwEW0dqDPy7k3yyw3oACFS6MX+DToZn+v1f46OErfM6fZIH53F4mDuD+Lxi5mM1P+uCUT nmDYHsc2CI2HeJwMpq1Fkler7CJQ16Y= 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-85-zSRiS33COLeS4AUEaVj_FQ-1; Sat, 04 May 2024 04:31:01 -0400 X-MC-Unique: zSRiS33COLeS4AUEaVj_FQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 9EDE0380621C for ; Sat, 4 May 2024 08:31:01 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.71]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4F3E21C0654E; Sat, 4 May 2024 08:31:01 +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 4448V05l139824 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 4 May 2024 10:31:00 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.18.1/8.18.1/Submit) id 4448V022139823; Sat, 4 May 2024 10:31:00 +0200 From: Aldy Hernandez To: GCC patches Cc: Andrew MacLeod , Aldy Hernandez Subject: [COMMITTED 10/23] Implement operator_cast for prange. Date: Sat, 4 May 2024 10:30:38 +0200 Message-ID: <20240504083056.139719-11-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.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.9 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 (operator_cast::fold_range): New. (operator_cast::op1_range): New. (operator_cast::lhs_op1_relation): New. (operator_cast::pointers_handled_p): New. --- gcc/range-op-mixed.h | 28 +++++ gcc/range-op-ptr.cc | 245 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 273 insertions(+) diff --git a/gcc/range-op-mixed.h b/gcc/range-op-mixed.h index 04c8acbd94a..11b1bf0bca4 100644 --- a/gcc/range-op-mixed.h +++ b/gcc/range-op-mixed.h @@ -400,14 +400,42 @@ public: bool fold_range (irange &r, tree type, const irange &op1, const irange &op2, relation_trio rel = TRIO_VARYING) const final override; + bool fold_range (prange &r, tree type, + const prange &op1, const prange &op2, + relation_trio rel = TRIO_VARYING) const final override; + bool fold_range (irange &r, tree type, + const prange &op1, const irange &op2, + relation_trio rel = TRIO_VARYING) const final override; + bool fold_range (prange &r, tree type, + const irange &op1, const prange &op2, + relation_trio rel = TRIO_VARYING) const final override; bool op1_range (irange &r, tree type, const irange &lhs, const irange &op2, relation_trio rel = TRIO_VARYING) const final override; + bool op1_range (prange &r, tree type, + const prange &lhs, const prange &op2, + relation_trio rel = TRIO_VARYING) const final override; + bool op1_range (irange &r, tree type, + const prange &lhs, const irange &op2, + relation_trio rel = TRIO_VARYING) const final override; + bool op1_range (prange &r, tree type, + const irange &lhs, const prange &op2, + relation_trio rel = TRIO_VARYING) const final override; relation_kind lhs_op1_relation (const irange &lhs, const irange &op1, const irange &op2, relation_kind) const final override; + relation_kind lhs_op1_relation (const prange &lhs, + const prange &op1, const prange &op2, + relation_kind) const final override; + relation_kind lhs_op1_relation (const prange &lhs, + const irange &op1, const irange &op2, + relation_kind) const final override; + relation_kind lhs_op1_relation (const irange &lhs, + const prange &op1, const prange &op2, + relation_kind) const final override; void update_bitmask (irange &r, const irange &lh, const irange &rh) const final override; + bool pointers_handled_p (range_op_dispatch_type, unsigned) const final override; private: bool truncating_cast_p (const irange &inner, const irange &outer) const; bool inside_domain_p (const wide_int &min, const wide_int &max, diff --git a/gcc/range-op-ptr.cc b/gcc/range-op-ptr.cc index e59e278cbd7..b8f86c8e838 100644 --- a/gcc/range-op-ptr.cc +++ b/gcc/range-op-ptr.cc @@ -706,6 +706,251 @@ operator_cst::pointers_handled_p (range_op_dispatch_type type, } } +// Cast between pointers. + +bool +operator_cast::fold_range (prange &r, tree type, + const prange &inner, + const prange &outer, + relation_trio) const +{ + if (empty_range_varying (r, type, inner, outer)) + return true; + + r.set (type, inner.lower_bound (), inner.upper_bound ()); + r.update_bitmask (inner.get_bitmask ()); + return true; +} + +// Cast a pointer to an integer. + +bool +operator_cast::fold_range (irange &r, tree type, + const prange &inner, + const irange &outer, + relation_trio) const +{ + if (empty_range_varying (r, type, inner, outer)) + return true; + + // Represent INNER as an integer of the same size, and then cast it + // to the resulting integer type. + tree pointer_uint_type = make_unsigned_type (TYPE_PRECISION (inner.type ())); + r.set (pointer_uint_type, inner.lower_bound (), inner.upper_bound ()); + r.update_bitmask (inner.get_bitmask ()); + range_cast (r, type); + return true; +} + +// Cast an integer to a pointer. + +bool +operator_cast::fold_range (prange &r, tree type, + const irange &inner, + const prange &outer, + relation_trio) const +{ + if (empty_range_varying (r, type, inner, outer)) + return true; + + // Cast INNER to an integer of the same size as the pointer we want, + // and then copy the bounds to the resulting pointer range. + int_range<2> tmp = inner; + tree pointer_uint_type = make_unsigned_type (TYPE_PRECISION (type)); + range_cast (tmp, pointer_uint_type); + r.set (type, tmp.lower_bound (), tmp.upper_bound ()); + r.update_bitmask (tmp.get_bitmask ()); + return true; +} + +bool +operator_cast::op1_range (prange &r, tree type, + const prange &lhs, + const prange &op2, + relation_trio trio) const +{ + if (lhs.undefined_p ()) + return false; + gcc_checking_assert (types_compatible_p (op2.type(), type)); + + // Conversion from other pointers or a constant (including 0/NULL) + // are straightforward. + if (POINTER_TYPE_P (lhs.type ()) + || (lhs.singleton_p () + && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type))) + fold_range (r, type, lhs, op2, trio); + else + { + // If the LHS is not a pointer nor a singleton, then it is + // either VARYING or non-zero. + if (!lhs.undefined_p () && !range_includes_zero_p (lhs)) + r.set_nonzero (type); + else + r.set_varying (type); + } + r.intersect (op2); + return true; +} + +bool +operator_cast::op1_range (irange &r, tree type, + const prange &lhs, + const irange &op2, + relation_trio trio) const +{ + if (lhs.undefined_p ()) + return false; + gcc_checking_assert (types_compatible_p (op2.type(), type)); + + // Conversion from other pointers or a constant (including 0/NULL) + // are straightforward. + if (POINTER_TYPE_P (lhs.type ()) + || (lhs.singleton_p () + && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type))) + fold_range (r, type, lhs, op2, trio); + else + { + // If the LHS is not a pointer nor a singleton, then it is + // either VARYING or non-zero. + if (!lhs.undefined_p () && !range_includes_zero_p (lhs)) + r.set_nonzero (type); + else + r.set_varying (type); + } + r.intersect (op2); + return true; +} + +bool +operator_cast::op1_range (prange &r, tree type, + const irange &lhs, + const prange &op2, + relation_trio trio) const +{ + if (lhs.undefined_p ()) + return false; + gcc_checking_assert (types_compatible_p (op2.type(), type)); + + // Conversion from other pointers or a constant (including 0/NULL) + // are straightforward. + if (POINTER_TYPE_P (lhs.type ()) + || (lhs.singleton_p () + && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type))) + fold_range (r, type, lhs, op2, trio); + else + { + // If the LHS is not a pointer nor a singleton, then it is + // either VARYING or non-zero. + if (!lhs.undefined_p () && !range_includes_zero_p (lhs)) + r.set_nonzero (type); + else + r.set_varying (type); + } + r.intersect (op2); + return true; +} + +relation_kind +operator_cast::lhs_op1_relation (const prange &lhs, + const prange &op1, + const prange &op2 ATTRIBUTE_UNUSED, + relation_kind) const +{ + if (lhs.undefined_p () || op1.undefined_p ()) + return VREL_VARYING; + unsigned lhs_prec = TYPE_PRECISION (lhs.type ()); + unsigned op1_prec = TYPE_PRECISION (op1.type ()); + // If the result gets sign extended into a larger type check first if this + // qualifies as a partial equivalence. + if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec) + { + // If the result is sign extended, and the LHS is larger than op1, + // check if op1's range can be negative as the sign extension will + // cause the upper bits to be 1 instead of 0, invalidating the PE. + int_range<3> negs = range_negatives (op1.type ()); + negs.intersect (op1); + if (!negs.undefined_p ()) + return VREL_VARYING; + } + + unsigned prec = MIN (lhs_prec, op1_prec); + return bits_to_pe (prec); +} + +relation_kind +operator_cast::lhs_op1_relation (const prange &lhs, + const irange &op1, + const irange &op2 ATTRIBUTE_UNUSED, + relation_kind) const +{ + if (lhs.undefined_p () || op1.undefined_p ()) + return VREL_VARYING; + unsigned lhs_prec = TYPE_PRECISION (lhs.type ()); + unsigned op1_prec = TYPE_PRECISION (op1.type ()); + // If the result gets sign extended into a larger type check first if this + // qualifies as a partial equivalence. + if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec) + { + // If the result is sign extended, and the LHS is larger than op1, + // check if op1's range can be negative as the sign extension will + // cause the upper bits to be 1 instead of 0, invalidating the PE. + int_range<3> negs = range_negatives (op1.type ()); + negs.intersect (op1); + if (!negs.undefined_p ()) + return VREL_VARYING; + } + + unsigned prec = MIN (lhs_prec, op1_prec); + return bits_to_pe (prec); +} + +relation_kind +operator_cast::lhs_op1_relation (const irange &lhs, + const prange &op1, + const prange &op2 ATTRIBUTE_UNUSED, + relation_kind) const +{ + if (lhs.undefined_p () || op1.undefined_p ()) + return VREL_VARYING; + unsigned lhs_prec = TYPE_PRECISION (lhs.type ()); + unsigned op1_prec = TYPE_PRECISION (op1.type ()); + // If the result gets sign extended into a larger type check first if this + // qualifies as a partial equivalence. + if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec) + { + // If the result is sign extended, and the LHS is larger than op1, + // check if op1's range can be negative as the sign extension will + // cause the upper bits to be 1 instead of 0, invalidating the PE. + int_range<3> negs = range_negatives (op1.type ()); + negs.intersect (op1); + if (!negs.undefined_p ()) + return VREL_VARYING; + } + + unsigned prec = MIN (lhs_prec, op1_prec); + return bits_to_pe (prec); +} + +bool +operator_cast::pointers_handled_p (range_op_dispatch_type type, + unsigned dispatch) const +{ + switch (type) + { + case DISPATCH_FOLD_RANGE: + case DISPATCH_OP1_RANGE: + return (dispatch == RO_PPP + || dispatch == RO_IPI + || dispatch == RO_PIP); + case DISPATCH_LHS_OP1_RELATION: + return (dispatch == RO_PPP + || dispatch == RO_PII + || dispatch == RO_IPP); + default: + return true; + } +} + // Initialize any pointer operators to the primary table void