From patchwork Tue May 2 20:25:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 89443 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp880969vqo; Tue, 2 May 2023 13:37:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7tzAHJ+dyiSsABgeLgef8N+HWaAJ7hIkiJG0c5yr327aKagCMW0nNm3nk8LejVlos/9yHa X-Received: by 2002:aa7:df94:0:b0:4be:b39b:ea8f with SMTP id b20-20020aa7df94000000b004beb39bea8fmr10389543edy.2.1683059877754; Tue, 02 May 2023 13:37:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683059877; cv=none; d=google.com; s=arc-20160816; b=rSq4q6BiWRyOealecLP/e0vtCOgJd8ZiKPhoTzJ5DK5Fli/fXvoLyXMdyIp1147x47 Abvnf1RtmQzbRi6EAjjFFcc0rcp1QU1OS6ap3kWI6/K0eA2So8qoQ+fdzaUN/Jprim+9 mMn2Ks7FbNc+sDoTjZvV5t06p/+8fhYDvKgUYuc6x8twGxe6nfg3Vo27WzBYn8Z2TVqP IxElHygBQbm1r9vf2lU56yek7apUc8lJ8wJZNfZ61INLVWgVKSnE5rAYi6wWodrty9ve 0+ww1mB/ZXSpZoZ2xYBdsZ414AKvxwpaP9+VGFEMyrlhzEpSUOwb/5y1zos7PtmR9Axf ts5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=OQ5RhG3bz76gvrJ9XBq+ocfTACzGBmhItO3UW8YvDLo=; b=sNzUH4UmZqnd2SpFC9TmgOANRntvyqgEtgTNVI3m5GJN6B3cQeaO/KmhiXTPc5vXjg 7StBeH7PHVDVKqLVUvMvKX45Ij5JSeJ8bN5KNdy7ovxr/ltnAdPn5V0SihfA8Pp7bzXl /BhNw4isPkwbjECMAeAt7diPC9IEV0cndSw0murykVzvu2nAVtBZC+qs8eEpEZ+Kl4cs P3bhiUpJ4lBBNELL7BjYUbzwP9USuHT1UQNMC2NtUxDv4Sz0yCpIAEnz4T8TvYSZLhq5 eL7bjbQrFtyUHzzqethEJD9xN0FPhzZOCsT5OihkWtQILmN02CXicwJTiDbmy35q0OL6 WmnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=k0yTrA1c; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id c7-20020aa7c747000000b0050bd4b8ca87si1377924eds.170.2023.05.02.13.37.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 May 2023 13:37:57 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=k0yTrA1c; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0B168393BA4F for ; Tue, 2 May 2023 20:26:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0B168393BA4F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683059192; bh=OQ5RhG3bz76gvrJ9XBq+ocfTACzGBmhItO3UW8YvDLo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=k0yTrA1cdyeRNRIGA1NNFPFfPPloTKUcfLpm1WLQkowyyley5eBG5KO2K4unY6KXa Jhb/OjWFKK/Ffy0YMRSUml4LzLbk21dfUrKuKhh4KOpuj7kW94zZl+zHxD7A378y/G pW2xHxXhWvXnT2uq3cPP1QhBQ0sRKjGidNZdFFig= 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 E87B83858C83 for ; Tue, 2 May 2023 20:25:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E87B83858C83 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-151-OLNxPPTYNgiZeUv_hU1lFw-1; Tue, 02 May 2023 16:25:35 -0400 X-MC-Unique: OLNxPPTYNgiZeUv_hU1lFw-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-74df1fa3064so273963985a.1 for ; Tue, 02 May 2023 13:25:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683059134; x=1685651134; 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=OQ5RhG3bz76gvrJ9XBq+ocfTACzGBmhItO3UW8YvDLo=; b=gv1bNK5W4zrwXunw7VVDvH23rs5hmqq61O/98nDnqmaS1MzLUqnAmu7t9bz1loLDmV toHsEp/7/94xCW65edI904nVoxchzLzUq1lbTq8RZoVDEXonvk48XIcYZrrcT+SHwGiw HYwKxPeHh6FR90Jbea5iNr/JOdPVhi3HIcM0pjQTKFvCJoP6ybI3/dSVD9k74KqCZPLl CpmJGLSeHzI1D5tE1dIjapjfvc17D0gcew7KzL5H/1M+EOMiRdSlim4ZUFtQqMLM8Is6 jgFXqPuN6YFODD5qsgB1whtj/UM9UzNTkTetE3ON6rdjo/MUIeqzD23Cc5q0CWpW4CN/ pJJw== X-Gm-Message-State: AC+VfDwACEGPPb08c8+dSZjgOUgaQttx5LYfkpvxxHRpl1O69o3UBa8v jTjCvFfkPXfwevRWRvcSUPJcQId7os9joxy6l5755TkXu7tFstTWT+QyP8A/d4p1ltrQm5ein7X 3nPpQxl4AjwhlxBd291Qe/t51Bm0Ia2t9di52wuF/3LXKPkpLkZGfvpQJ8TZy9t8beaPkX4y2nQ == X-Received: by 2002:a05:622a:f:b0:3e3:937b:2a64 with SMTP id x15-20020a05622a000f00b003e3937b2a64mr30344449qtw.18.1683059134641; Tue, 02 May 2023 13:25:34 -0700 (PDT) X-Received: by 2002:a05:622a:f:b0:3e3:937b:2a64 with SMTP id x15-20020a05622a000f00b003e3937b2a64mr30344388qtw.18.1683059134071; Tue, 02 May 2023 13:25:34 -0700 (PDT) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id y11-20020ac8128b000000b003ef6582d971sm9060696qti.47.2023.05.02.13.25.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 May 2023 13:25:33 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH 1/2] c++: std::variant slow to compile [PR109678] Date: Tue, 2 May 2023 16:25:24 -0400 Message-Id: <20230502202525.1964821-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 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, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764816194338905406?= X-GMAIL-MSGID: =?utf-8?q?1764816194338905406?= Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- Here, when dealing with a class with a complex subobject structure, we would try and fail to find the relevant FIELD_DECL for an empty base before giving up. And we would do this at each level, in a combinatorially problematic way. Instead, we should check for an empty base first. PR c++/109678 gcc/cp/ChangeLog: * constexpr.cc (cxx_fold_indirect_ref_1): Handle empty base first. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/variant1.C: New test. --- gcc/cp/constexpr.cc | 23 +++++++------ gcc/testsuite/g++.dg/cpp1z/variant1.C | 47 +++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/variant1.C base-commit: bc24c51c0ccd64617864897ad071c98004ffc0a4 diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index d1097764b10..37d1c444c9e 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -5446,6 +5446,19 @@ cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, return ret; } } + + /* Handle conversion to an empty base class, which is represented with a + NOP_EXPR. Do this before spelunking into the non-empty subobjects, + which is likely to be a waste of time (109678). */ + if (is_empty_class (type) + && CLASS_TYPE_P (optype) + && DERIVED_FROM_P (type, optype)) + { + if (empty_base) + *empty_base = true; + return op; + } + for (tree field = TYPE_FIELDS (optype); field; field = DECL_CHAIN (field)) if (TREE_CODE (field) == FIELD_DECL @@ -5468,16 +5481,6 @@ cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, return ret; } } - /* Also handle conversion to an empty base class, which - is represented with a NOP_EXPR. */ - if (is_empty_class (type) - && CLASS_TYPE_P (optype) - && DERIVED_FROM_P (type, optype)) - { - if (empty_base) - *empty_base = true; - return op; - } } return NULL_TREE; diff --git a/gcc/testsuite/g++.dg/cpp1z/variant1.C b/gcc/testsuite/g++.dg/cpp1z/variant1.C new file mode 100644 index 00000000000..9b18cc233ca --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/variant1.C @@ -0,0 +1,47 @@ +// PR c++/109678 +// With the bug, compiling this testcase takes more than the typical timeout. +// { dg-do compile { target c++17 } } + +#include + +struct A {}; +struct B {}; +struct C {}; +struct D {}; +struct E {}; +struct F {}; +struct G {}; +struct H {}; +struct I {}; +struct J {}; +struct K {}; +struct L {}; +struct M {}; +struct N {}; +struct O {}; +struct P {}; +struct Q {}; +struct R {}; +struct S {}; +struct T {}; +struct U {}; +struct V {}; +struct W { + // gcc13 + compiler explorer = 20000ms + // gcc12.2 + compiler explorer = 400ms + int i; +}; +struct X {}; +struct Y {}; +struct Z {}; + +using Foo = std::variant; + +struct Bar { + Foo f; + static Bar dummy() { + // issue is triggered by this initialization + return {Z{}}; + // return {A{}}; // would be very quick + } +}; From patchwork Tue May 2 20:25:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 89444 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp881008vqo; Tue, 2 May 2023 13:38:03 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7ceWK9W8J41f2JYX5MVnDhmyFaqym4h+co0VCRISB1ZIwjuVnmd8xsnLqQ973SNwNEjjpC X-Received: by 2002:a17:907:ea3:b0:949:cb6a:b6f7 with SMTP id ho35-20020a1709070ea300b00949cb6ab6f7mr1210067ejc.56.1683059883689; Tue, 02 May 2023 13:38:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683059883; cv=none; d=google.com; s=arc-20160816; b=PLvYMNJnOxlzbF/s8jC4kNt3Qz1uKWA8hl/I9Dr4MoVuKBVTWiGUiKpFvMZIL8BDJu GTYo6p+0nFBrbnmjNNsMyyr0bZH30b/c4Ri6N3IN/UrXxS51DS6QGqUVfm3aXIJet1n5 NfXehb0qCvlzSmtElRk+mLDbIgTxNpHPD1jew7mInU/OR4zRnKoCKhaLYWTKqeSNkWFx v6NlHFlGq92xIdHkE2DFG84N8yhkVs76oKxjf9iI4dKAvpJ0xgJepwcjMH4nDZNli5XD 3HFcL44c5rbfIPBQgLYS51FgBI3IEBfu75kJKicnLdWtd/BXjm7bydlb8REwOA7hDHIv j7pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=VVuFHAE0rP1g/XNpuKejiYl7ZF60yMd6DYmjFEVXL88=; b=bApZOqfLL5mdHnWx7ZTnA6IHlJP5qyBAU/vWC3cKvuKbUvTegdMDERhp5H06N5N3SR dPF5FhHW+xiacBZXODM370jtJiTyR/EUpIbuzFlybm99rZZTosasYgFMJ9DL6Wvwu/3C 0rKe7u3LsKt/kTa1nMAFvqc/1qNyy30U+W62pESDY2rhpP5m3Y6fXxu52NwNp7nXMzy6 NcjzE4E7PsqTBmMbKi4BEHRHZgPckDDWj/vOi/JR8DtRDkY3rQ8+b3zjYPcRYvRxQw1i /LwYQSqJXjgrfATh5UwxTwHu6X4vWQPe0VAlqCOFwm9paE513A6sSOC9GFkIDfJi28bg i+XQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=JZu6C+nt; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id tl16-20020a170907c31000b0095f96af851dsi11837591ejc.219.2023.05.02.13.38.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 May 2023 13:38:03 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=JZu6C+nt; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B0BC83942013 for ; Tue, 2 May 2023 20:26:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B0BC83942013 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683059194; bh=VVuFHAE0rP1g/XNpuKejiYl7ZF60yMd6DYmjFEVXL88=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=JZu6C+ntgoup6BhrzcTsHkkQR2JushAOsvQxqq1pv2E3Jy88EGfDKdB0sWOG7uAaZ R8Suj3MC6yN3FncRWpwOKtVB0nbvIo0cMrBgdBLZMRgzkL1tkO7B/yd7cEbOEzrJZQ +6RZE4ZW51t/sDmbJgaqNt1SqvJr9Zk7Yiq7kkOk= 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 66B96385840D for ; Tue, 2 May 2023 20:25:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 66B96385840D Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-183-7YyJyejPNq-rTYNC8ll-ag-1; Tue, 02 May 2023 16:25:36 -0400 X-MC-Unique: 7YyJyejPNq-rTYNC8ll-ag-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-751319bd93cso2160185a.0 for ; Tue, 02 May 2023 13:25:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683059135; x=1685651135; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VVuFHAE0rP1g/XNpuKejiYl7ZF60yMd6DYmjFEVXL88=; b=km4AJPJzxK13PKIn1u6rpxymixViai0zBtK/B0jK0sdvYCyx0K520Hax+6+F6n/i/n uMsvzz9ju8ANGXzZ6p1dcVvgPAYk8+bOn/9C1i+BMJarTOxHCpZqJYKtzl0BRKxLxO95 H0DR0Ln0bQfPvmfW+v3zFLsKWu+vIINA6vPA6uqyh6kIJ6AI+9J83jHaNC/IJHhWz3jg jQn6Cph2vttTVI2rHYlqgTnpPXDuTOiNl2JLwLP4zivbmbULbSzlYMXMrNWGDuhM7jk8 9WmcvAA0A8B4hPEUohuR1glhBdQ28LaMYGsdF6RTYFb/wn/ulIMT8YQTgqJh5P6U2G2+ OuEw== X-Gm-Message-State: AC+VfDyYT8xO1QVtt/V8DrJzyETHViKwlqMkVfCUgEyWtRnLedrsP7Ad uYQR/kQRiEkvtry78lZd8yc9hfLG/H7C8W/dfGFV+h2LfnHQXt4YHQPEhCJRFVYbXvWBYSRPssJ mQ9mInIg5XEKj5eKUAF78J7o42jpvnYq8ljLQaHGNtOZX5fEsrRWMlFZDQAvRvcAT8agSLO5iGQ == X-Received: by 2002:a05:6214:529c:b0:61b:6382:4579 with SMTP id kj28-20020a056214529c00b0061b63824579mr5466702qvb.9.1683059135428; Tue, 02 May 2023 13:25:35 -0700 (PDT) X-Received: by 2002:a05:6214:529c:b0:61b:6382:4579 with SMTP id kj28-20020a056214529c00b0061b63824579mr5466660qvb.9.1683059134898; Tue, 02 May 2023 13:25:34 -0700 (PDT) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id y11-20020ac8128b000000b003ef6582d971sm9060696qti.47.2023.05.02.13.25.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 May 2023 13:25:34 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH 2/2] c++: look for empty base at specific offset [PR109678] Date: Tue, 2 May 2023 16:25:25 -0400 Message-Id: <20230502202525.1964821-2-jason@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230502202525.1964821-1-jason@redhat.com> References: <20230502202525.1964821-1-jason@redhat.com> MIME-Version: 1.0 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, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764816200775214753?= X-GMAIL-MSGID: =?utf-8?q?1764816200775214753?= Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- While looking at the empty base handling for 109678, it occurred to me that we ought to be able to look for an empty base at a specific offset, not just in general. PR c++/109678 gcc/cp/ChangeLog: * cp-tree.h (lookup_base): Add offset parm. * constexpr.cc (cxx_fold_indirect_ref_1): Pass it. * search.cc (struct lookup_base_data_s): Add offset. (dfs_lookup_base): Handle it. (lookup_base): Pass it. --- gcc/cp/cp-tree.h | 3 ++- gcc/cp/constexpr.cc | 2 +- gcc/cp/search.cc | 25 ++++++++++++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index c9c4cd6f32f..406a5508ce7 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7515,7 +7515,8 @@ extern tree build_if_nonnull (tree, tree, tsubst_flags_t); extern tree get_parent_with_private_access (tree decl, tree binfo); extern bool accessible_base_p (tree, tree, bool); extern tree lookup_base (tree, tree, base_access, - base_kind *, tsubst_flags_t); + base_kind *, tsubst_flags_t, + HOST_WIDE_INT = -1); extern tree dcast_base_hint (tree, tree); extern int accessible_p (tree, tree, bool); extern int accessible_in_template_p (tree, tree); diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 37d1c444c9e..70dd6cf4d90 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -5452,7 +5452,7 @@ cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, which is likely to be a waste of time (109678). */ if (is_empty_class (type) && CLASS_TYPE_P (optype) - && DERIVED_FROM_P (type, optype)) + && lookup_base (optype, type, ba_any, NULL, tf_none, off)) { if (empty_base) *empty_base = true; diff --git a/gcc/cp/search.cc b/gcc/cp/search.cc index 3f521b3bd72..cd80f285ac9 100644 --- a/gcc/cp/search.cc +++ b/gcc/cp/search.cc @@ -56,6 +56,7 @@ static tree dfs_get_pure_virtuals (tree, void *); struct lookup_base_data_s { + HOST_WIDE_INT offset; /* Offset we want, or -1 if any. */ tree t; /* type being searched. */ tree base; /* The base type we're looking for. */ tree binfo; /* Found binfo. */ @@ -74,6 +75,22 @@ dfs_lookup_base (tree binfo, void *data_) { struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_; + if (data->offset != -1) + { + /* We're looking for the type at a particular offset. */ + int comp = compare_tree_int (BINFO_OFFSET (binfo), data->offset); + if (comp > 0) + /* Don't bother looking into bases laid out later; even if they + do virtually inherit from the base we want, we can get there + by another path. */ + return dfs_skip_bases; + else if (comp != 0 + && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base)) + /* Right type, wrong offset. */ + return dfs_skip_bases; + /* Fall through. */ + } + if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base)) { if (!data->binfo) @@ -190,7 +207,7 @@ accessible_base_p (tree t, tree base, bool consider_local_p) /* Lookup BASE in the hierarchy dominated by T. Do access checking as ACCESS specifies. Return the binfo we discover. If KIND_PTR is non-NULL, fill with information about what kind of base we - discovered. + discovered. If OFFSET is other than -1, only match at that offset. If the base is inaccessible, or ambiguous, then error_mark_node is returned. If the tf_error bit of COMPLAIN is not set, no error @@ -198,7 +215,8 @@ accessible_base_p (tree t, tree base, bool consider_local_p) tree lookup_base (tree t, tree base, base_access access, - base_kind *kind_ptr, tsubst_flags_t complain) + base_kind *kind_ptr, tsubst_flags_t complain, + HOST_WIDE_INT offset /* = -1 */) { tree binfo; tree t_binfo; @@ -246,8 +264,9 @@ lookup_base (tree t, tree base, base_access access, data.base = base; data.binfo = NULL_TREE; data.ambiguous = data.via_virtual = false; - data.repeated_base = CLASSTYPE_REPEATED_BASE_P (t); + data.repeated_base = (offset == -1) && CLASSTYPE_REPEATED_BASE_P (t); data.want_any = access == ba_any; + data.offset = offset; dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data); binfo = data.binfo;