From patchwork Wed Jan 31 01:26:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 1893183 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=J+1GbWHb; 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 4TPkt55mWsz23fD for ; Wed, 31 Jan 2024 12:27:25 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B8F0D385828C for ; Wed, 31 Jan 2024 01:27:23 +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 9B8483858407 for ; Wed, 31 Jan 2024 01:27:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9B8483858407 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 9B8483858407 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=1706664425; cv=none; b=gBoWA/NQJoNnm5JZCxXJclUFI7PaJMzTjU+i/YMrOCcxxNF/SGzJRhLbN9wC/nQ4AGozNxP4sM4bIvj+IEe7ToGaOM9xMyyHLTHSl/BYSPbN3HqM3uv/JTnLQs8BaXPbhydeSFeGtmpcqpJiU2xZx4OoxOyE20tJMitjhWN1ybU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706664425; c=relaxed/simple; bh=6+VCWlzRaT4LVRw3TPdtL4t1hfRcr88G/SJha5izBGQ=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Va/m2BcuA4FLE65pyJxMGnp1CJt0xVpHYxN0A3mukaU5YutEhVA9ASKfiMgezq8flZjoVSkva+3fROuK1QMNs4WBCM1i9JGA2TptcLOiKCkEl+BJu9CUr5pOETJVrIP2HmFahDgpdaPCztlNLMnfNJUA123u4llPzdDxiTzKJJU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706664421; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=2zJ3iD80nLI3P5mEQCfS4F6idMGzkHZCLv16UZQNqNo=; b=J+1GbWHbue+CxqHTSXzYMQcaecW9VcpMj9WQ8PqUQ89+SXo4Yej8tQmtgT5A7zAB3BrFZ0 dR6S99GeFk+uGtGcgMACqU/fgB5qc8L0WGEGl2vLgOQn9lGTsHsELsu2OxIOxNjvxU3uh/ rIGy4aIP/C3sFQaWCbZrmibVdYm8LPg= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-395-kvXm7_7wPACBmsZYCcIiDg-1; Tue, 30 Jan 2024 20:26:59 -0500 X-MC-Unique: kvXm7_7wPACBmsZYCcIiDg-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-33b0256dd6bso116443f8f.2 for ; Tue, 30 Jan 2024 17:26:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706664417; x=1707269217; h=mime-version:message-id:subject:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=2zJ3iD80nLI3P5mEQCfS4F6idMGzkHZCLv16UZQNqNo=; b=IakTku+KzFNJWcc0OPjq8WxYz5/TsD4k62b10iRpqbV7Iv3aHQOO+hhwNph2Mufxj1 3H+Upm0Xm0KF6GLKHtSXXecpUiKzmSY0ekXxAmG9vZGdzHxDJPPgSngECRuUaNxIQpOa zfXOW35wBuxfgtxgt29KyGvtaEak0xjWeugZmNgeP4u9K9QWdBLEso7jJ2/pVDHFHH6r +rPHagPfbKFKOnZw/bh7JWpPZhzthd3fu16k8ByOzYKXKFFUR/iJFEniaVCXZALFxTrO pMdVEfb6NXNNCfkv2XYYdyt+BrnISSmF39Gdr2fbKBu454f1IUdJ0x/fvzXlEAkRy5BH iacw== X-Gm-Message-State: AOJu0YyQi9g7QUaY0zjO3mI2H1EeSHui95VLLlaGTf/mbHkqKSZTIMR3 euvpF5pkHeywm+WB3ZyKhEz79hJhkYuoWMW8WzyhvX08+/iGMufTG/h4CzX5EK4WrJfuDj82VfF pZjtJiJ17tVaHnPoPy0NSWs3MI72CkhIjrh8bBODmY0k0h4ZMCC8SRd+Bcsx6Y62w1CF5KazhU2 BGAt2P4UENt7h4U2kyhKDPRIEyuY1x4fAsDlJQRqQ= X-Received: by 2002:a05:6000:11c7:b0:33a:f355:79de with SMTP id i7-20020a05600011c700b0033af35579demr81690wrx.65.1706664417537; Tue, 30 Jan 2024 17:26:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IFLW3jnLnPwZWOnyvhG8VdDCXlE5PJca/0TxR/GnBlqsjX2mDjqIuJ+31D9PoaS75zf/5p4mQ== X-Received: by 2002:a05:6000:11c7:b0:33a:f355:79de with SMTP id i7-20020a05600011c700b0033af35579demr81682wrx.65.1706664417025; Tue, 30 Jan 2024 17:26:57 -0800 (PST) Received: from digraph.polyomino.org.uk (digraph.polyomino.org.uk. [2001:8b0:bf73:93f7::51bb:e332]) by smtp.gmail.com with ESMTPSA id bh2-20020a05600005c200b0033afd34781asm2286219wrb.75.2024.01.30.17.26.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 17:26:56 -0800 (PST) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.95) (envelope-from ) id 1rUzMh-0011d1-Sa for gcc-patches@gcc.gnu.org; Wed, 31 Jan 2024 01:26:11 +0000 Date: Wed, 31 Jan 2024 01:26:11 +0000 (UTC) From: Joseph Myers To: gcc-patches@gcc.gnu.org Subject: [committed] c: Fix ICEs casting expressions with integer constant operands to bool [PR111059, PR111911] Message-ID: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.1 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, 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.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 C front-end bugs 111059 and 111911 both report ICEs with conversions to boolean of expressions with integer constant operands that can appear in an integer constant expression as long as they are not evaluated (such as division by zero). The issue is a nested C_MAYBE_CONST_EXPR, with the inner one generated in build_binary_op to indicate that a subexpression has been fully folded and should not be folded again, and the outer one in build_c_cast to indicate that the expression has integer constant operands. To avoid the inner one from build_binary_op, c_objc_common_truthvalue_conversion should be given an argument properly marked as having integer constant operands rather than that information having been removed by the caller - but because c_convert would then also wrap a C_MAYBE_CONST_EXPR with a NOP_EXPR converting to boolean, it seems most convenient to have c_objc_common_truthvalue_conversion produce the NE_EXPR directly in the desired type (boolean in this case), before generating any C_MAYBE_CONST_EXPR there, rather than it always producing a comparison in integer_type_node and doing a conversion to boolean in the caller. The same issue as in those PRs also applies for conversion to enums with a boolean fixed underlying type; that case is also fixed and tests added for it. Note that not all the tests added failed before the patch (in particular, the issue was specific to casts and did not apply for implicit conversions, but some tests of those are added as well). Bootstrapped with no regressions for x86_64-pc-linux-gnu. PR c/111059 PR c/111911 gcc/c/ * c-tree.h (c_objc_common_truthvalue_conversion): Add third argument. * c-convert.cc (c_convert): For conversions to boolean, pass third argument to c_objc_common_truthvalue_conversion rather than converting here. * c-typeck.cc (build_c_cast): Ensure arguments with integer operands are marked as such for conversion to boolean. (c_objc_common_truthvalue_conversion): Add third argument TYPE. gcc/testsuite/ * gcc.c-torture/compile/pr111059-1.c, gcc.c-torture/compile/pr111059-2.c, gcc.c-torture/compile/pr111059-3.c, gcc.c-torture/compile/pr111059-4.c, gcc.c-torture/compile/pr111059-5.c, gcc.c-torture/compile/pr111059-6.c, gcc.c-torture/compile/pr111059-7.c, gcc.c-torture/compile/pr111059-8.c, gcc.c-torture/compile/pr111059-9.c, gcc.c-torture/compile/pr111059-10.c, gcc.c-torture/compile/pr111059-11.c, gcc.c-torture/compile/pr111059-12.c, gcc.c-torture/compile/pr111911-1.c, gcc.c-torture/compile/pr111911-2.c: New tests. --- Applied to mainline. Should be backported to active release branches after waiting to see if any problems arise on mainline (before GCC 13, the parts relating to enums with fixed underlying type will need to be removed; for GCC 13, those tests will need to use -std=gnu2x not -std=gnu23). diff --git a/gcc/c/c-convert.cc b/gcc/c/c-convert.cc index 3e52926f276..7c1064c6691 100644 --- a/gcc/c/c-convert.cc +++ b/gcc/c/c-convert.cc @@ -150,8 +150,7 @@ c_convert (tree type, tree expr, bool init_const) case BOOLEAN_TYPE: convert_to_boolean: - return fold_convert_loc - (loc, type, c_objc_common_truthvalue_conversion (input_location, expr)); + return c_objc_common_truthvalue_conversion (input_location, expr, type); case POINTER_TYPE: /* The type nullptr_t may be converted to a pointer type. The result is diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index cf29534c091..1fba9c8dae7 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -755,7 +755,8 @@ bool c_type_variably_modified_p (tree t) extern bool char_type_p (tree); -extern tree c_objc_common_truthvalue_conversion (location_t, tree); +extern tree c_objc_common_truthvalue_conversion (location_t, tree, + tree = integer_type_node); extern tree require_complete_type (location_t, tree); extern bool same_translation_unit_p (const_tree, const_tree); extern int comptypes (tree, tree); diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 66c6abc9f07..12d1a5a5fa1 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -6363,6 +6363,22 @@ build_c_cast (location_t loc, tree type, tree expr) " from %qT to %qT", otype, type); ovalue = value; + /* If converting to boolean a value with integer operands that + is not itself represented as an INTEGER_CST, the call below + to note_integer_operands may insert a C_MAYBE_CONST_EXPR, but + build_binary_op as called by c_common_truthvalue_conversion + may also insert a C_MAYBE_CONST_EXPR to indicate that a + subexpression has been fully folded. To avoid nested + C_MAYBE_CONST_EXPR, ensure that + c_objc_common_truthvalue_conversion receives an argument + properly marked as having integer operands in that case. */ + if (int_operands + && TREE_CODE (value) != INTEGER_CST + && (TREE_CODE (type) == BOOLEAN_TYPE + || (TREE_CODE (type) == ENUMERAL_TYPE + && ENUM_UNDERLYING_TYPE (type) != NULL_TREE + && TREE_CODE (ENUM_UNDERLYING_TYPE (type)) == BOOLEAN_TYPE))) + value = note_integer_operands (value); value = convert (type, value); /* Ignore any integer overflow caused by the cast. */ @@ -13509,11 +13525,11 @@ build_binary_op (location_t location, enum tree_code code, } -/* Convert EXPR to be a truth-value, validating its type for this +/* Convert EXPR to be a truth-value (type TYPE), validating its type for this purpose. LOCATION is the source location for the expression. */ tree -c_objc_common_truthvalue_conversion (location_t location, tree expr) +c_objc_common_truthvalue_conversion (location_t location, tree expr, tree type) { bool int_const, int_operands; @@ -13556,14 +13572,17 @@ c_objc_common_truthvalue_conversion (location_t location, tree expr) if (int_operands && TREE_CODE (expr) != INTEGER_CST) { expr = remove_c_maybe_const_expr (expr); - expr = build2 (NE_EXPR, integer_type_node, expr, + expr = build2 (NE_EXPR, type, expr, convert (TREE_TYPE (expr), integer_zero_node)); expr = note_integer_operands (expr); } else - /* ??? Should we also give an error for vectors rather than leaving - those to give errors later? */ - expr = c_common_truthvalue_conversion (location, expr); + { + /* ??? Should we also give an error for vectors rather than leaving + those to give errors later? */ + expr = c_common_truthvalue_conversion (location, expr); + expr = fold_convert_loc (location, type, expr); + } if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const) { diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-1.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-1.c new file mode 100644 index 00000000000..5788cb49eba --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-1.c @@ -0,0 +1,5 @@ +void +f () +{ + (_Bool) (1 << -1); +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-10.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-10.c new file mode 100644 index 00000000000..fdd9a29ecf5 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-10.c @@ -0,0 +1,9 @@ +/* { dg-options "-std=gnu23 -Wno-div-by-zero" } */ + +enum e : bool { X }; + +enum e +f () +{ + return 0 / 0; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-11.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-11.c new file mode 100644 index 00000000000..81260345a2c --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-11.c @@ -0,0 +1,9 @@ +/* { dg-options "-std=gnu23 -Wno-overflow" } */ + +enum e : bool { X }; + +void +f () +{ + (enum e) (__INT_MAX__ + 1); +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-12.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-12.c new file mode 100644 index 00000000000..d11fcf654ce --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-12.c @@ -0,0 +1,9 @@ +/* { dg-options "-std=gnu23 -Wno-overflow" } */ + +enum e : bool { X }; + +enum e +f () +{ + return __INT_MAX__ + 1; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-2.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-2.c new file mode 100644 index 00000000000..785e291e1d8 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-2.c @@ -0,0 +1,5 @@ +void +f () +{ + (_Bool) (0 / 0); +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-3.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-3.c new file mode 100644 index 00000000000..21099bc09fb --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-3.c @@ -0,0 +1,5 @@ +_Bool +f () +{ + return 1 << -1; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-4.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-4.c new file mode 100644 index 00000000000..95c68e55ce0 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-4.c @@ -0,0 +1,5 @@ +_Bool +f () +{ + return 0 / 0; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-5.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-5.c new file mode 100644 index 00000000000..10473550d26 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-5.c @@ -0,0 +1,5 @@ +void +f () +{ + (_Bool) (__INT_MAX__ + 1); +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-6.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-6.c new file mode 100644 index 00000000000..213f79bfe72 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-6.c @@ -0,0 +1,5 @@ +_Bool +f () +{ + return __INT_MAX__ + 1; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-7.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-7.c new file mode 100644 index 00000000000..466f45f1270 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-7.c @@ -0,0 +1,9 @@ +/* { dg-options "-std=gnu23 -Wno-shift-count-negative" } */ + +enum e : bool { X }; + +void +f () +{ + (enum e) (1 << -1); +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-8.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-8.c new file mode 100644 index 00000000000..67f339534e3 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-8.c @@ -0,0 +1,9 @@ +/* { dg-options "-std=gnu23 -Wno-div-by-zero" } */ + +enum e : bool { X }; + +void +f () +{ + (enum e) (0 / 0); +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111059-9.c b/gcc/testsuite/gcc.c-torture/compile/pr111059-9.c new file mode 100644 index 00000000000..f88096d4b47 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111059-9.c @@ -0,0 +1,9 @@ +/* { dg-options "-std=gnu23 -Wno-shift-count-negative" } */ + +enum e : bool { X }; + +enum e +f () +{ + return 1 << -1; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111911-1.c b/gcc/testsuite/gcc.c-torture/compile/pr111911-1.c new file mode 100644 index 00000000000..350f4ccd1fd --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111911-1.c @@ -0,0 +1,7 @@ +int +main (void) +{ + if (!(_Bool)(__INT_MAX__ + 1) / !(_Bool)(__INT_MAX__ + 1)) + ; + return 1; +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr111911-2.c b/gcc/testsuite/gcc.c-torture/compile/pr111911-2.c new file mode 100644 index 00000000000..28b69c46bdb --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr111911-2.c @@ -0,0 +1,11 @@ +/* { dg-options "-std=gnu23 -Wno-overflow -Wno-div-by-zero" } */ + +enum e : bool { X }; + +int +main (void) +{ + if (!(enum e)(__INT_MAX__ + 1) / !(enum e)(__INT_MAX__ + 1)) + ; + return 1; +}