From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 81FBC3857BB2 for ; Tue, 31 May 2022 09:32:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 81FBC3857BB2 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-640-XGGrO6R4NuelXMh83b5zug-1; Tue, 31 May 2022 05:32:56 -0400 X-MC-Unique: XGGrO6R4NuelXMh83b5zug-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BD38F8027EE; Tue, 31 May 2022 09:32:55 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.33.36.77]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7DDAF492C3B; Tue, 31 May 2022 09:32:55 +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 24V9WqHI1277876 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 31 May 2022 11:32:53 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 24V9WqJk1277875; Tue, 31 May 2022 11:32:52 +0200 Date: Tue, 31 May 2022 11:32:51 +0200 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] unswitch: Fold case label lo/hi values to index type [PR105770] Message-ID: Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, 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-List-Received-Date: Tue, 31 May 2022 09:32:59 -0000 Hi! The following testcase ICEs because we use different types in comparison, idx has int type, while CASE_LOW has char type. While I believe all CASE_{LOW,HIGH} in the same switch have to use the same or compatible type, the index expression can have a promoted type as happens in this testcase. Other spots that handle switches do such foldings too. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-05-31 Jakub Jelinek PR tree-optimization/105770 * tree-ssa-loop-unswitch.cc (find_unswitching_predicates_for_bb): Cast CASE_LOW and CASE_HIGH to TREE_TYPE (idx) before comparisons with idx. * gcc.dg/pr105770.c: New test. --- gcc/tree-ssa-loop-unswitch.cc.jj 2022-05-25 11:07:29.754185772 +0200 +++ gcc/tree-ssa-loop-unswitch.cc 2022-05-30 10:57:23.165131441 +0200 @@ -494,6 +494,7 @@ find_unswitching_predicates_for_bb (basi { unsigned nlabels = gimple_switch_num_labels (stmt); tree idx = gimple_switch_index (stmt); + tree idx_type = TREE_TYPE (idx); if (TREE_CODE (idx) != SSA_NAME || nlabels < 1) return; @@ -526,16 +527,18 @@ find_unswitching_predicates_for_bb (basi if (CASE_HIGH (lab) != NULL_TREE) { tree cmp1 = fold_build2 (GE_EXPR, boolean_type_node, idx, - CASE_LOW (lab)); + fold_convert (idx_type, + CASE_LOW (lab))); tree cmp2 = fold_build2 (LE_EXPR, boolean_type_node, idx, - CASE_HIGH (lab)); + fold_convert (idx_type, + CASE_HIGH (lab))); cmp = fold_build2 (BIT_AND_EXPR, boolean_type_node, cmp1, cmp2); lab_range.set (CASE_LOW (lab), CASE_HIGH (lab)); } else { cmp = fold_build2 (EQ_EXPR, boolean_type_node, idx, - CASE_LOW (lab)); + fold_convert (idx_type, CASE_LOW (lab))); lab_range.set (CASE_LOW (lab)); } --- gcc/testsuite/gcc.dg/pr105770.c.jj 2022-05-30 11:08:30.603530499 +0200 +++ gcc/testsuite/gcc.dg/pr105770.c 2022-05-30 11:07:12.066406193 +0200 @@ -0,0 +1,19 @@ +/* PR tree-optimization/105770 */ +/* { dg-do compile } */ +/* { dg-options "-O1 -funswitch-loops -fno-tree-forwprop" } */ + +char a; + +void +foo (void) +{ + while (a) + switch (a) + { + case ' ': + case '\t': + return; + } + + __builtin_unreachable (); +} Jakub