From patchwork Tue Oct 24 17:03:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 157642 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2082972vqx; Tue, 24 Oct 2023 10:04:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFHu8C1YQUoGYUI3F19EtEABwX/er4dLQ69YFWj9rAIEl5x84Ys7zDRqCW5DcETnGmB+0+d X-Received: by 2002:a05:622a:287:b0:41e:1790:6cf7 with SMTP id z7-20020a05622a028700b0041e17906cf7mr9812206qtw.37.1698167047306; Tue, 24 Oct 2023 10:04:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698167047; cv=pass; d=google.com; s=arc-20160816; b=mMautlVinHzGEH+pfpsDeu/n7fnvla3JBk8C3+Dxd1gdcLL+O/82Mz/9QF+asZTfQF VF7VHPh7JL3Di2PqUKQb+NHNDZwrUgkecC7lIjHmBTAoZdDh4nigrLLnMvr3UkTGhOkg TDhezoyB8CDmsPB9QBfXuEXfoLIDb5dXKC6FgEa8vqUK1pJr4zY4KILs+Dm8HX0BibbI zzFQPUrLzLjXJpIPc4T0ViletTE5srZBOli/xabL0IC4LwFhXkBgbWwEcPQD1BfkDwOL n+e9vErZEUoK9G7/l/wU2siFUy8j7hBD2EjZ0rm3LnIL5MwsJwpK5fhBA7GoB4yTa+ob 0cWA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=s5LF4J4xps5aQpzAhcDMKyGyVi8I6E0crdKscQezOP4=; fh=1Hi9m88IqcWZGtfSxUrMGG+GBgL26N02X1SANXM6iTs=; b=SSllv3tkmli/sIcjwiiAkTNAm75lm1esRNrgbzt4EKeZ+IO9awEwLkZ5kbegeDwgoP Mx0WqSwi6eOZcqJa1bdPFKJ6WE8NW90soovd3+MN65qMzxQjJyq1sIjLbnNddQjDyHwm LZT51tOArltUBE4dUrP85ojFNTSHvmCMg9l9UlNk6pqtUUsrKZv4bOFKj2NSeTPXKkgN oOSbgxHgXfoDJFyda/qI4lEA+g5oXIoJlr2kjYslZE1ufIqhyE7p4zMZVQ/jQVvaiy1L rNzwe7Y1pO5kiRUJW63JD8DZFQZCIBEOjkMePHasm9vj6YDcl8kgJm9jcJdLDvBUE+DI qSIw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DyM7xGIN; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id i19-20020ac85e53000000b0041b30072e09si7183164qtx.392.2023.10.24.10.04.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 10:04:07 -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=@redhat.com header.s=mimecast20190719 header.b=DyM7xGIN; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0D4223857714 for ; Tue, 24 Oct 2023 17:04:07 +0000 (GMT) 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 071603858CDA for ; Tue, 24 Oct 2023 17:03:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 071603858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 071603858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698167015; cv=none; b=ks0LPpPoIRTNJqeQRBcxJ/omOmA5drg1Bhn/jDV3jDJvzD0/4bZ+jHJjXL3yRgwyIFNdLgk8MDGSlrrmig5Cb1GkBWU7MwNwIq1bB1KTieK0guZSmBImQO9O60dOKs6nTqvtT8Ua48aakYpAJTSgJw7ehDvH67N0BKVLJCnIMIQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698167015; c=relaxed/simple; bh=qcBlgqUX/I8iR9TayGB9YdRHUakj1QJR7vCbOv5Txeo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PnxPnLO91bgbmtHbCxWkJusL/XSDocT2H/da68EqUNbdNviS3OEosoP7CLbyaOSWsxAc1+gv40L6qoHopIogW779hOYdd8cHx8gFSDQdN4ZfL9OYZDWtIXMgBGn/pkJPpkOrryP+dKygxCtNy1yCAhrvLFTd1F3sYBNAI5y3XCc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698167013; 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; bh=s5LF4J4xps5aQpzAhcDMKyGyVi8I6E0crdKscQezOP4=; b=DyM7xGINCTH6bP1XDbF6W0OhmQ5PCGFwLAkqAb8iohE3rPWboolNFHqMpxzIenZxQy8k95 q5BOP4bTr95T2EY+viDQQIM7rvLuHwh0CnjuzKtNgiHAaB6ee/1qTcHoRQ0/bZYSGF6LOy eNwouTABqWs/wmVqVpHE33wOUVpEEMw= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-433-jMGZOdSfOyOSrFldoQPu3A-1; Tue, 24 Oct 2023 13:03:21 -0400 X-MC-Unique: jMGZOdSfOyOSrFldoQPu3A-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-77893b0a2cdso587822785a.2 for ; Tue, 24 Oct 2023 10:03:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698166999; x=1698771799; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=s5LF4J4xps5aQpzAhcDMKyGyVi8I6E0crdKscQezOP4=; b=NBxO/nLgMR/8nER63IqR5lL3R1opjdcjlkLxQlCB91M/+/zEsv8WIHoDiBSoYybg+Z zi1KEAEy8bmW8KxFOZMA/taye0Q81374gh+kZ8E5hBz+cZoAHm6q39HEQQ5vhNe/iM/K vH+cl1TDPHp5ATloFvul2XFv1BBZO9T57Bj7c++GAx277H854SH/P74bBxpD3Ej2AJjJ Q7jciLJWhbBwv505nQpjY71nVIP/hIsSvX3hr3vnmtaR+0Uvirh+MQL5k8O7H5e3/cn9 Os0CrQiN5J+NruMOMgOKV7C1j5btQjgvgUu3Ct8T+74u7K4u48CFOqy/Vg+4jIr4LGkP wUjg== X-Gm-Message-State: AOJu0YwFnWr59pVsJzXdMtb3mNgbWgu6V6UK3R8cZ72ewe2fBy/Xm2ZC DvIxjO+pDfMOZanZwl6aSfSF010TRHR0aQD3fgOEN4kpe0t5/+EAzWLF7wm1cSHXj6U+2QTGWnr VWO6T9kM/JaDwZ3GeaTUgqeFkrdmq1LXSXFATD9erbq8ATvS2vfI+FK1ykYNWTnrAG7tDATWd0h k= X-Received: by 2002:a05:620a:284a:b0:778:91ee:a9fb with SMTP id h10-20020a05620a284a00b0077891eea9fbmr14905908qkp.5.1698166999008; Tue, 24 Oct 2023 10:03:19 -0700 (PDT) X-Received: by 2002:a05:620a:284a:b0:778:91ee:a9fb with SMTP id h10-20020a05620a284a00b0077891eea9fbmr14905873qkp.5.1698166998586; Tue, 24 Oct 2023 10:03:18 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id po10-20020a05620a384a00b007676f3859fasm3564104qkn.30.2023.10.24.10.03.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 10:03:18 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: build_new_1 and non-dep array size [PR111929] Date: Tue, 24 Oct 2023 13:03:16 -0400 Message-ID: <20231024170316.3919946-1-ppalka@redhat.com> X-Mailer: git-send-email 2.42.0.424.gceadf0f3cf MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.6 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_H4, RCVD_IN_MSPIKE_WL, 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780657209817681481 X-GMAIL-MSGID: 1780657209817681481 Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look like the right approach? -- >8 -- This PR is another instance of NON_DEPENDENT_EXPR having acted as an "analysis barrier" for middle-end routines, and now that it's gone we may end up passing weird templated trees (that have a generic tree code) to the middle-end which leads to an ICE. In the testcase below the non-dependent array size 'var + 42' is expressed as an ordinary PLUS_EXPR, but whose operand types have different precisions -- long and int respectively -- naturally because templated trees encode only the syntactic form of an expression devoid of e.g. implicit conversions (typically). This type incoherency triggers a wide_int assert during the call to size_binop in build_new_1 which requires the operand types have the same precision. This patch fixes this by replacing our incremental folding of 'size' within build_new_1 with a single call to cp_fully_fold (which is a no-op in template context) once 'size' is fully built. PR c++/111929 gcc/cp/ChangeLog: * init.cc (build_new_1): Use convert, build2, build3 instead of fold_convert, size_binop and fold_build3 when building 'size'. gcc/testsuite/ChangeLog: * g++.dg/template/non-dependent28.C: New test. --- gcc/cp/init.cc | 9 +++++---- gcc/testsuite/g++.dg/template/non-dependent28.C | 6 ++++++ 2 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/template/non-dependent28.C diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index d48bb16c7c5..56c1b5e9f5e 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -3261,7 +3261,7 @@ build_new_1 (vec **placement, tree type, tree nelts, max_outer_nelts = wi::udiv_trunc (max_size, inner_size); max_outer_nelts_tree = wide_int_to_tree (sizetype, max_outer_nelts); - size = size_binop (MULT_EXPR, size, fold_convert (sizetype, nelts)); + size = build2 (MULT_EXPR, sizetype, size, convert (sizetype, nelts)); if (TREE_CODE (cst_outer_nelts) == INTEGER_CST) { @@ -3344,7 +3344,7 @@ build_new_1 (vec **placement, tree type, tree nelts, /* Use a class-specific operator new. */ /* If a cookie is required, add some extra space. */ if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)) - size = size_binop (PLUS_EXPR, size, cookie_size); + size = build2 (PLUS_EXPR, sizetype, size, cookie_size); else { cookie_size = NULL_TREE; @@ -3358,8 +3358,8 @@ build_new_1 (vec **placement, tree type, tree nelts, if (cxx_dialect >= cxx11 && flag_exceptions) errval = throw_bad_array_new_length (); if (outer_nelts_check != NULL_TREE) - size = fold_build3 (COND_EXPR, sizetype, outer_nelts_check, - size, errval); + size = build3 (COND_EXPR, sizetype, outer_nelts_check, size, errval); + size = cp_fully_fold (size); /* Create the argument list. */ vec_safe_insert (*placement, 0, size); /* Do name-lookup to find the appropriate operator. */ @@ -3418,6 +3418,7 @@ build_new_1 (vec **placement, tree type, tree nelts, /* If size is zero e.g. due to type having zero size, try to preserve outer_nelts for constant expression evaluation purposes. */ + size = cp_fully_fold (size); if (integer_zerop (size) && outer_nelts) size = build2 (MULT_EXPR, TREE_TYPE (size), size, outer_nelts); diff --git a/gcc/testsuite/g++.dg/template/non-dependent28.C b/gcc/testsuite/g++.dg/template/non-dependent28.C new file mode 100644 index 00000000000..3e45154f61d --- /dev/null +++ b/gcc/testsuite/g++.dg/template/non-dependent28.C @@ -0,0 +1,6 @@ +// PR c++/111929 + +template +void f(long var) { + new int[var + 42]; +}