From patchwork Sat Jan 20 09:35:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 84479 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 56A283858409 for ; Sat, 20 Jan 2024 09:36:17 +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 0D6763858D39 for ; Sat, 20 Jan 2024 09:35:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0D6763858D39 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 0D6763858D39 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=1705743344; cv=none; b=i8bq6rD/5TJdAn4wo22tmfnauLScCD+3cIjSoYw8WVXesyj0tqBQrSzVmmo9cDDVahU1t/laDq5PuvE1yqAnV6suuXSLGPN2EbEnuK2YojiDNpVhKr51nF6sjC+l2Mh64kHLN+wFBzbg8CZ8LzINgYrCmihmL3KHatFbNxVnkys= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705743344; c=relaxed/simple; bh=NXNHFTqcjgiDQXaKzJGLQcep9qFMVkBEp3yA4Erebng=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=uyXbSuYV0ApNzYezeid7hvYJ/OgCyZwEA4MdQ1BCXR0fGXR+ZuLTVKMBOqISMfquhsGbSeyWFMISYHU9T7J/IFIghEWRBnvyim8QN1hbUOtn88AVxaI92f/LbtE6+MTrQUV7YKw6ETVRXTaleVLhRbOe72vaEK2ZUj1EHtuQBS0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705743340; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=xS1IB+rO8IwHTfoz0fbiWSFf9Q6KOk/ZalNZhM6k6QQ=; b=WD0QZ1bTXFWMwc21OEvAj4FNqd5OZ/y+KkTiLQZ09wJKO8XBL5+VdgzASOVXqkLXJrWfPR KgnvooMJeL2VBAxkBVn8rmtEWAiaPqK+3k/s+hYfVsS+J/rFpTN80YGhOzRQkixsdr5Qkz FUvGjxYAcaBYVOWIZJwUYjjz6fBgIgE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-211-ofED8Y6KOpq6cFHXs_x6dA-1; Sat, 20 Jan 2024 04:35:38 -0500 X-MC-Unique: ofED8Y6KOpq6cFHXs_x6dA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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 56A308029E6; Sat, 20 Jan 2024 09:35:38 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.70]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F1BEC492BE2; Sat, 20 Jan 2024 09:35:37 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 40K9ZZoE4066118 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 20 Jan 2024 10:35:35 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 40K9ZY7V4066117; Sat, 20 Jan 2024 10:35:34 +0100 Date: Sat, 20 Jan 2024 10:35:34 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-switch-conversion: Bugfixes for _BitInt [PR113491] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_LOTSOFHASH, 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: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Hi! The following patch fixes various issues with large/huge _BitInt used as switch expressions. In particular: 1) the indexes in CONSTRUCTORs shouldn't be types with precision larger than sizetype precision, varasm uses wi::to_offset on those and too large indexes ICE; we've already checked earlier that the cluster is at most sizetype bits and arrays can't be larger than that anyway 2) some spots were using SCALAR_INT_TYPE_MODE or lang_hooks.types.type_for_mode on TYPE_MODE to determine types to use, that obviously doesn't work for the large/huge BITINT_TYPE 3) like the recent change in the C FE, this patch makes sure we don't create ARRAY_REFs with indexes with precision above sizetype precision, because bitint lowering isn't prepared for that and because the indexes can't be larger than sizetype anyway; the subtraction of the cluster minimum from the index obviously needs to be done in unsigned __int128 or large/huge BITINT_TYPE, but then we cast to sizetype if the precision is larger than sizetype Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-01-20 Jakub Jelinek PR tree-optimization/113491 * tree-switch-conversion.cc (switch_conversion::build_constructors): If elt.index has precision higher than sizetype, fold_convert it to sizetype. (switch_conversion::array_value_type): Return type if type is BITINT_TYPE with precision above MAX_FIXED_MODE_SIZE or with BLKmode. (switch_conversion::build_arrays): Use unsigned_type_for rather than lang_hooks.types.type_for_mode if utype is BITINT_TYPE with precision above MAX_FIXED_MODE_SIZE or with BLKmode. If utype has precision higher than sizetype, use sizetype as tidx type and fold_convert the subtraction to sizetype. * gcc.dg/torture/bitint-51.c: New test. Jakub --- gcc/tree-switch-conversion.cc.jj 2024-01-03 11:51:24.210832797 +0100 +++ gcc/tree-switch-conversion.cc 2024-01-19 18:13:12.928850888 +0100 @@ -423,6 +423,9 @@ switch_conversion::build_constructors () constructor_elt elt; elt.index = int_const_binop (MINUS_EXPR, pos, m_range_min); + if (TYPE_PRECISION (TREE_TYPE (elt.index)) + > TYPE_PRECISION (sizetype)) + elt.index = fold_convert (sizetype, elt.index); elt.value = unshare_expr_without_location (m_default_values[k]); m_constructors[k]->quick_push (elt); @@ -452,6 +455,9 @@ switch_conversion::build_constructors () constructor_elt elt; elt.index = int_const_binop (MINUS_EXPR, pos, m_range_min); + if (TYPE_PRECISION (TREE_TYPE (elt.index)) + > TYPE_PRECISION (sizetype)) + elt.index = fold_convert (sizetype, elt.index); elt.value = unshare_expr_without_location (val); m_constructors[j]->quick_push (elt); @@ -543,7 +549,10 @@ switch_conversion::array_value_type (tre type = TYPE_MAIN_VARIANT (type); - if (!INTEGRAL_TYPE_P (type)) + if (!INTEGRAL_TYPE_P (type) + || (TREE_CODE (type) == BITINT_TYPE + && (TYPE_PRECISION (type) > MAX_FIXED_MODE_SIZE + || TYPE_MODE (type) == BLKmode))) return type; scalar_int_mode type_mode = SCALAR_INT_TYPE_MODE (type); @@ -707,7 +716,7 @@ void switch_conversion::build_arrays () { tree arr_index_type; - tree tidx, sub, utype; + tree tidx, sub, utype, tidxtype; gimple *stmt; gimple_stmt_iterator gsi; gphi_iterator gpi; @@ -720,14 +729,23 @@ switch_conversion::build_arrays () utype = TREE_TYPE (m_index_expr); if (TREE_TYPE (utype)) utype = lang_hooks.types.type_for_mode (TYPE_MODE (TREE_TYPE (utype)), 1); + else if (TREE_CODE (utype) == BITINT_TYPE + && (TYPE_PRECISION (utype) > MAX_FIXED_MODE_SIZE + || TYPE_MODE (utype) == BLKmode)) + utype = unsigned_type_for (utype); else utype = lang_hooks.types.type_for_mode (TYPE_MODE (utype), 1); + if (TYPE_PRECISION (utype) > TYPE_PRECISION (sizetype)) + tidxtype = sizetype; + else + tidxtype = utype; arr_index_type = build_index_type (m_range_size); - tidx = make_ssa_name (utype); + tidx = make_ssa_name (tidxtype); sub = fold_build2_loc (loc, MINUS_EXPR, utype, fold_convert_loc (loc, utype, m_index_expr), fold_convert_loc (loc, utype, m_range_min)); + sub = fold_convert (tidxtype, sub); sub = force_gimple_operand_gsi (&gsi, sub, false, NULL, true, GSI_SAME_STMT); stmt = gimple_build_assign (tidx, sub); --- gcc/testsuite/gcc.dg/torture/bitint-51.c.jj 2024-01-19 18:21:53.013463622 +0100 +++ gcc/testsuite/gcc.dg/torture/bitint-51.c 2024-01-19 18:21:21.793906456 +0100 @@ -0,0 +1,88 @@ +/* PR tree-optimization/113491 */ +/* { dg-do run { target bitint } } */ +/* { dg-options "-std=c23 -pedantic-errors" } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */ + +#if __BITINT_MAXWIDTH__ >= 2022 +long a; +_BitInt (2022) b; + +__attribute__((noipa)) void +foo (void) +{ + long e; + switch (b) + { + case 236881099392745653949476092019254703673343952653001806909505866172387514585208299669392000424755912461093218145058550533815207769064511922933470022995816097769135032744756688849360373090388143543095880516133276503510437474354615271669000583762291950374253494970691499065905744428004707234072154531557724197892702081481886955739280363415482836575783505023297999635820743287218509451775188374692338715173350419479853664808666850654180398109587878293637547258643044750595201236843385276543354061680674360041173237330198483330996131123489419291470122643730353591015638976897743657130063384289745055875469886904587wb: + e = 28; + break; + case 236881099392745653949476092019254703673343952653001806909505866172387514585208299669392000424755912461093218145058550533815207769064511922933470022995816097769135032744756688849360373090388143543095880516133276503510437474354615271669000583762291950374253494970691499065905744428004707234072154531557724197892702081481886955739280363415482836575783505023297999635820743287218509451775188374692338715173350419479853664808666850654180398109587878293637547258643044750595201236843385276543354061680674360041173237330198483330996131123489419291470122643730353591015638976897743657130063384289745055875469886904590wb: + e = 6; + break; + case 236881099392745653949476092019254703673343952653001806909505866172387514585208299669392000424755912461093218145058550533815207769064511922933470022995816097769135032744756688849360373090388143543095880516133276503510437474354615271669000583762291950374253494970691499065905744428004707234072154531557724197892702081481886955739280363415482836575783505023297999635820743287218509451775188374692338715173350419479853664808666850654180398109587878293637547258643044750595201236843385276543354061680674360041173237330198483330996131123489419291470122643730353591015638976897743657130063384289745055875469886904607wb: + e = 7; + break; + default: + e = 8; + break; + } + a = e; +} +#endif + +int +main () +{ +#if __BITINT_MAXWIDTH__ >= 2022 + b = 42wb; + foo (); + if (a != 8) + __builtin_abort (); + b = 236881099392745653949476092019254703673343952653001806909505866172387514585208299669392000424755912461093218145058550533815207769064511922933470022995816097769135032744756688849360373090388143543095880516133276503510437474354615271669000583762291950374253494970691499065905744428004707234072154531557724197892702081481886955739280363415482836575783505023297999635820743287218509451775188374692338715173350419479853664808666850654180398109587878293637547258643044750595201236843385276543354061680674360041173237330198483330996131123489419291470122643730353591015638976897743657130063384289745055875469886904586wb; + foo (); + if (a != 8) + __builtin_abort (); + b = 236881099392745653949476092019254703673343952653001806909505866172387514585208299669392000424755912461093218145058550533815207769064511922933470022995816097769135032744756688849360373090388143543095880516133276503510437474354615271669000583762291950374253494970691499065905744428004707234072154531557724197892702081481886955739280363415482836575783505023297999635820743287218509451775188374692338715173350419479853664808666850654180398109587878293637547258643044750595201236843385276543354061680674360041173237330198483330996131123489419291470122643730353591015638976897743657130063384289745055875469886904587wb; + foo (); + if (a != 28) + __builtin_abort (); + b = 236881099392745653949476092019254703673343952653001806909505866172387514585208299669392000424755912461093218145058550533815207769064511922933470022995816097769135032744756688849360373090388143543095880516133276503510437474354615271669000583762291950374253494970691499065905744428004707234072154531557724197892702081481886955739280363415482836575783505023297999635820743287218509451775188374692338715173350419479853664808666850654180398109587878293637547258643044750595201236843385276543354061680674360041173237330198483330996131123489419291470122643730353591015638976897743657130063384289745055875469886904588wb; + foo (); + if (a != 8) + __builtin_abort (); + b = 236881099392745653949476092019254703673343952653001806909505866172387514585208299669392000424755912461093218145058550533815207769064511922933470022995816097769135032744756688849360373090388143543095880516133276503510437474354615271669000583762291950374253494970691499065905744428004707234072154531557724197892702081481886955739280363415482836575783505023297999635820743287218509451775188374692338715173350419479853664808666850654180398109587878293637547258643044750595201236843385276543354061680674360041173237330198483330996131123489419291470122643730353591015638976897743657130063384289745055875469886904589wb; + foo (); + if (a != 8) + __builtin_abort (); + b = 236881099392745653949476092019254703673343952653001806909505866172387514585208299669392000424755912461093218145058550533815207769064511922933470022995816097769135032744756688849360373090388143543095880516133276503510437474354615271669000583762291950374253494970691499065905744428004707234072154531557724197892702081481886955739280363415482836575783505023297999635820743287218509451775188374692338715173350419479853664808666850654180398109587878293637547258643044750595201236843385276543354061680674360041173237330198483330996131123489419291470122643730353591015638976897743657130063384289745055875469886904590wb; + foo (); + if (a != 6) + __builtin_abort (); + b = 236881099392745653949476092019254703673343952653001806909505866172387514585208299669392000424755912461093218145058550533815207769064511922933470022995816097769135032744756688849360373090388143543095880516133276503510437474354615271669000583762291950374253494970691499065905744428004707234072154531557724197892702081481886955739280363415482836575783505023297999635820743287218509451775188374692338715173350419479853664808666850654180398109587878293637547258643044750595201236843385276543354061680674360041173237330198483330996131123489419291470122643730353591015638976897743657130063384289745055875469886904591wb; + foo (); + if (a != 8) + __builtin_abort (); + b = 236881099392745653949476092019254703673343952653001806909505866172387514585208299669392000424755912461093218145058550533815207769064511922933470022995816097769135032744756688849360373090388143543095880516133276503510437474354615271669000583762291950374253494970691499065905744428004707234072154531557724197892702081481886955739280363415482836575783505023297999635820743287218509451775188374692338715173350419479853664808666850654180398109587878293637547258643044750595201236843385276543354061680674360041173237330198483330996131123489419291470122643730353591015638976897743657130063384289745055875469886904606wb; + foo (); + if (a != 8) + __builtin_abort (); + b = 236881099392745653949476092019254703673343952653001806909505866172387514585208299669392000424755912461093218145058550533815207769064511922933470022995816097769135032744756688849360373090388143543095880516133276503510437474354615271669000583762291950374253494970691499065905744428004707234072154531557724197892702081481886955739280363415482836575783505023297999635820743287218509451775188374692338715173350419479853664808666850654180398109587878293637547258643044750595201236843385276543354061680674360041173237330198483330996131123489419291470122643730353591015638976897743657130063384289745055875469886904607wb; + foo (); + if (a != 7) + __builtin_abort (); + b = 236881099392745653949476092019254703673343952653001806909505866172387514585208299669392000424755912461093218145058550533815207769064511922933470022995816097769135032744756688849360373090388143543095880516133276503510437474354615271669000583762291950374253494970691499065905744428004707234072154531557724197892702081481886955739280363415482836575783505023297999635820743287218509451775188374692338715173350419479853664808666850654180398109587878293637547258643044750595201236843385276543354061680674360041173237330198483330996131123489419291470122643730353591015638976897743657130063384289745055875469886904608wb; + foo (); + if (a != 8) + __builtin_abort (); + b = 240780458385579342400393461351617812815637161357071131707208942081962936661153218844512115504763375697200879163458183553026017242301187821441055479544760906104973534996069938628004474568289906582206917095065620305216254432816950650228843795794816095162791355341943390986975847866692139272448065870433527123346286515814575123941041341323886584452213168407427683905346733773730890398535581783579726464034446453496393589067919979673611753823620422962335479358086639875375670825770647896268644196740771259886611570273762180917307714137084771977480688440721015151914970095703226362506437887288273484956889253937151wb; + foo (); + if (a != 8) + __builtin_abort (); + b = -240780458385579342400393461351617812815637161357071131707208942081962936661153218844512115504763375697200879163458183553026017242301187821441055479544760906104973534996069938628004474568289906582206917095065620305216254432816950650228843795794816095162791355341943390986975847866692139272448065870433527123346286515814575123941041341323886584452213168407427683905346733773730890398535581783579726464034446453496393589067919979673611753823620422962335479358086639875375670825770647896268644196740771259886611570273762180917307714137084771977480688440721015151914970095703226362506437887288273484956889253937151wb; + foo (); + if (a != 8) + __builtin_abort (); +#endif + return 0; +}