From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id 9BA3F3858D1E for ; Fri, 10 Nov 2023 11:17:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9BA3F3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9BA3F3858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::42d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699615039; cv=none; b=hnw+SLHeJ0RZxNQF5MaSIoRhqFkcAsptT/UZC10bxoOcq2VcZsceRa2uOCKuvvGBamboZFHYN6Wnz09DE2ose8Ig83bH19KIfP7UptRlh8VBnJzfCyeJefC4v3BMS0VXSkSRO8l/5jk0toM12gYrWblHu2ajdaq0TFQ8LqGrpus= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699615039; c=relaxed/simple; bh=Ut9YYoVhUaeDgmjCyys6nS25bUhQV1eenYUC6dZiWIE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=EgL4MzHf50CKmcjMXH2XHRYBh2C2caO7gv8fGIUns0aPqSqIQ893RdqmoCbrhMze8w03WGnD7bkA2FozgR/7RzIyCvmxMOf1ZVa4/m1+geIoUE87mfFkKeZER08ExrE3U5mlFNn01A4mNltbCc1g+QRUVVhZBb01sTlif2QEgXA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-32d849cc152so1220202f8f.1 for ; Fri, 10 Nov 2023 03:17:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1699615036; x=1700219836; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=BATsXr/D9d2zjahWskHD2f4Y1g9jU/Hze1+Yg2yXIqc=; b=IdWCx3vWRj0fBl5myuW5J4+7+NCOl/2gzZSqxQcVOs6hjToyfhN3E6LTPdTM3zwCW0 QVkFQYyTwPtunpvSgBkwKvoBbbAgLhE4bC0bfIMdiYNNoadFmZRGVu7EsMYdcMcVABI5 UzZZ6Ku5PLcA4ewzuT+/e/BfzrIgHJ1hxv9oci1BZT7vOzX3sF3wLd+N7Wp10iTqkH5u Pqel47mwhZUv+o84v1u9/ZsapV2WM0zNW/62ToKZJeRpH18VOP2BQkKM6acaa9jvOWQM yy53dA1EZGP0xdQW+w8cVh7tu+V4eVFq2/94N3ukmJV1lh/5K2GbA0Mg1/Ss8ZJf0dQw eh9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699615036; x=1700219836; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BATsXr/D9d2zjahWskHD2f4Y1g9jU/Hze1+Yg2yXIqc=; b=HRDlMIiosaL5CQPEIA1k7Rl9nK8pKUYt859aOSiWYtCl9U8oEmcB1yaH7bzDZh4kji wOXxfLPrrKpgEghXlLpF7SVn4ZuOj7x8TgjBLOsbD8dlkbLjB+0lDb2y9kQIc0ZLxfu+ ob8D3zl6XN1a44hvyccarKkvIVw4PlHVH703pHJEX1GSV7yGQI7xNcTZIhcH8KgfC4Kv VdGWQmXLZ357NySKQ69C/rbN7nToeLSo1yBqJ9DMpmxA/Mgg5geRKxfoM9pePjtVVMBl f+XBxz7lan+iaj/6Kipf52BVPEpHfuTUhZEm36Jhked0E3HyDB8PSstamwfCZoRAqeux 0Ywg== X-Gm-Message-State: AOJu0YznEaXWbrVpdxWPFXwnv5b42L7PFTMQaasl1vgbW5JMN39pWQyT 3v2ZyWD2EDFZC693g+n4rnoTjDo2v/+dK9ZpLTD43A== X-Google-Smtp-Source: AGHT+IEIkyzUnwwkw0xzFDk86QY3GdrYptIFNXWIif1acw6X/nnQbNt6g7F20b7oz3m061RctqdMrA== X-Received: by 2002:adf:d1c2:0:b0:32d:a0d3:d681 with SMTP id b2-20020adfd1c2000000b0032da0d3d681mr7027851wrd.43.1699615036154; Fri, 10 Nov 2023 03:17:16 -0800 (PST) Received: from fomalhaut.localnet ([2a01:e0a:8d5:d990:e654:e8ff:fe8f:2ce6]) by smtp.gmail.com with ESMTPSA id j17-20020adfea51000000b0032d829e10c0sm1699950wrn.28.2023.11.10.03.17.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Nov 2023 03:17:15 -0800 (PST) From: Eric Botcazou X-Google-Original-From: Eric Botcazou To: gcc-patches@gcc.gnu.org Subject: [PATCH] Handle constant CONSTRUCTORs in operand_compare Date: Fri, 10 Nov 2023 12:17:15 +0100 Message-ID: <4845410.GXAFRqVoOG@fomalhaut> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart1868860.tdWV9SEqCh" Content-Transfer-Encoding: 7Bit X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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 List-Id: This is a multi-part message in MIME format. --nextPart1868860.tdWV9SEqCh Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Hi, this teaches operand_compare to compare constant CONSTRUCTORs, which is quite helpful for so-called fat pointers in Ada, i.e. objects that are semantically pointers but are represented by structures made up of two pointers. This is modeled on the implementation present in the ICF pass. Bootstrapped/regtested on x86-64/Linux, OK for the mainline? 2023-11-10 Eric Botcazou * fold-const.cc (operand_compare::operand_equal_p) : Deal with nonempty constant CONSTRUCTORs. (operand_compare::hash_operand) : Hash DECL_FIELD_OFFSET and DECL_FIELD_BIT_OFFSET for FIELD_DECLs. 2023-11-10 Eric Botcazou * gnat.dg/opt103.ads, gnat.dg/opt103.adb: New test. -- Eric Botcazou --nextPart1868860.tdWV9SEqCh Content-Disposition: attachment; filename="p.diff" Content-Transfer-Encoding: 7Bit Content-Type: text/x-patch; charset="UTF-8"; name="p.diff" diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 40767736389..332bc8aead2 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -3315,9 +3315,65 @@ operand_compare::operand_equal_p (const_tree arg0, const_tree arg1, flags | OEP_ADDRESS_OF | OEP_MATCH_SIDE_EFFECTS); case CONSTRUCTOR: - /* In GIMPLE empty constructors are allowed in initializers of - aggregates. */ - return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1); + { + /* In GIMPLE empty constructors are allowed in initializers of + aggregates. */ + if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1)) + return true; + + /* See sem_variable::equals in ipa-icf for a similar approach. */ + tree typ0 = TREE_TYPE (arg0); + tree typ1 = TREE_TYPE (arg1); + + if (TREE_CODE (typ0) != TREE_CODE (typ1)) + return false; + else if (TREE_CODE (typ0) == ARRAY_TYPE) + { + /* For arrays, check that the sizes all match. */ + const HOST_WIDE_INT siz0 = int_size_in_bytes (typ0); + if (TYPE_MODE (typ0) != TYPE_MODE (typ1) + || siz0 < 0 + || siz0 != int_size_in_bytes (typ1)) + return false; + } + else if (!types_compatible_p (typ0, typ1)) + return false; + + vec *v0 = CONSTRUCTOR_ELTS (arg0); + vec *v1 = CONSTRUCTOR_ELTS (arg1); + if (vec_safe_length (v0) != vec_safe_length (v1)) + return false; + + /* Address of CONSTRUCTOR is defined in GENERIC to mean the value + of the CONSTRUCTOR referenced indirectly. */ + flags &= ~OEP_ADDRESS_OF; + + for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx) + { + constructor_elt *c0 = &(*v0)[idx]; + constructor_elt *c1 = &(*v1)[idx]; + + /* Check that the values are the same... */ + if (c0->value != c1->value + && !operand_equal_p (c0->value, c1->value, flags)) + return false; + + /* ... and that they apply to the same field! */ + if (c0->index != c1->index + && (TREE_CODE (typ0) == ARRAY_TYPE + ? !operand_equal_p (c0->index, c1->index, flags) + : !operand_equal_p (DECL_FIELD_OFFSET (c0->index), + DECL_FIELD_OFFSET (c1->index), + flags) + || !operand_equal_p (DECL_FIELD_BIT_OFFSET (c0->index), + DECL_FIELD_BIT_OFFSET (c1->index), + flags))) + return false; + } + + return true; + } + default: break; } @@ -3703,9 +3759,7 @@ operand_compare::operand_equal_p (const_tree arg0, const_tree arg1, elements. Individual elements in the constructor must be indexed in increasing order and form an initial sequence. - We make no effort to compare constructors in generic. - (see sem_variable::equals in ipa-icf which can do so for - constants). */ + We make no effort to compare nonconstant ones in GENERIC. */ if (!VECTOR_TYPE_P (TREE_TYPE (arg0)) || !VECTOR_TYPE_P (TREE_TYPE (arg1))) return false; @@ -3887,7 +3941,13 @@ operand_compare::hash_operand (const_tree t, inchash::hash &hstate, /* In GIMPLE the indexes can be either NULL or matching i. */ if (field == NULL_TREE) field = bitsize_int (idx); - hash_operand (field, hstate, flags); + if (TREE_CODE (field) == FIELD_DECL) + { + hash_operand (DECL_FIELD_OFFSET (field), hstate, flags); + hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags); + } + else + hash_operand (field, hstate, flags); hash_operand (value, hstate, flags); } return; --nextPart1868860.tdWV9SEqCh Content-Disposition: attachment; filename="opt103.adb" Content-Transfer-Encoding: 7Bit Content-Type: text/x-adasrc; charset="UTF-8"; name="opt103.adb" -- { dg-do compile } -- { dg-options "-O -gnatn -fdump-tree-optimized" } package body Opt103 is function Read return Mode is S : String := Get; M : Mode; begin -- There should be a single call to Value_Enumeration_Pos after inlining if Mode'Valid_Value (S) then M := Mode'Value (S); else raise Program_Error; end if; return M; end; function Translate (S : String) return Mode is M : Mode; begin -- There should be a single call to Value_Enumeration_Pos after inlining if Mode'Valid_Value (S) then M := Mode'Value (S); else raise Program_Error; end if; return M; end; end Opt103; -- { dg-final { scan-tree-dump-times ".value_enumeration_pos" 2 "optimized" } } --nextPart1868860.tdWV9SEqCh Content-Disposition: attachment; filename="opt103.ads" Content-Transfer-Encoding: 7Bit Content-Type: text/x-adasrc; charset="UTF-8"; name="opt103.ads" package Opt103 is type Mode is (Stop, Forward, Backward, Up, Down); function Get return String; pragma Import (Ada, Get); function Read return Mode; function Translate (S : String) return Mode; end Opt103; --nextPart1868860.tdWV9SEqCh--