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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 6A5FF386EC3C for ; Thu, 7 Jan 2021 08:31:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 6A5FF386EC3C Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-287-PUO8zQVyMQSOM7d7cuQNDQ-1; Thu, 07 Jan 2021 03:31:54 -0500 X-MC-Unique: PUO8zQVyMQSOM7d7cuQNDQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 23087100C660; Thu, 7 Jan 2021 08:31:53 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-112-11.ams2.redhat.com [10.36.112.11]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4C1585D9CD; Thu, 7 Jan 2021 08:31:52 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 1078VmuD1886362 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 7 Jan 2021 09:31:49 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 1078VmHg1886361; Thu, 7 Jan 2021 09:31:48 +0100 Date: Thu, 7 Jan 2021 09:31:47 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] bswap: Fix up recent vector CONSTRUCTOR optimization [PR98568] Message-ID: <20210107083147.GK725145@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 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=-6.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Thu, 07 Jan 2021 08:31:58 -0000 Hi! As the testcase shows, bswap can match even byte-swapping or indentity from low part of some wider SSA_NAME. For bswap replacement other than for vector CONSTRUCTOR the code has been using NOP_EXPR casts if the types weren't compatible, but for vectors we need to use VIEW_CONVERT_EXPR. The problem with the latter is that we require that it has the same size, which isn't guaranteed, so this patch in those cases first adds a narrowing NOP_EXPR cast and only afterwards does a VIEW_CONVERT_EXPR. Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-01-06 Jakub Jelinek PR tree-optimization/98568 * gimple-ssa-store-merging.c (bswap_view_convert): New function. (bswap_replace): Use it. * g++.dg/torture/pr98568.C: New test. --- gcc/gimple-ssa-store-merging.c.jj 2021-01-05 16:15:58.965337667 +0100 +++ gcc/gimple-ssa-store-merging.c 2021-01-06 19:20:37.225578412 +0100 @@ -978,6 +978,25 @@ public: }; // class pass_optimize_bswap +/* Helper function for bswap_replace. Build VIEW_CONVERT_EXPR from + VAL to TYPE. If VAL has different type size, emit a NOP_EXPR cast + first. */ + +static tree +bswap_view_convert (gimple_stmt_iterator *gsi, tree type, tree val) +{ + gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (val))); + if (TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (val))) + { + HOST_WIDE_INT prec = TREE_INT_CST_LOW (TYPE_SIZE (type)); + tree itype = build_nonstandard_integer_type (prec, 1); + gimple *g = gimple_build_assign (make_ssa_name (itype), NOP_EXPR, val); + gsi_insert_before (gsi, g, GSI_SAME_STMT); + val = gimple_assign_lhs (g); + } + return build1 (VIEW_CONVERT_EXPR, type, val); +} + /* Perform the bswap optimization: replace the expression computed in the rhs of gsi_stmt (GSI) (or if NULL add instead of replace) by an equivalent bswap, load or load + bswap expression. @@ -1100,7 +1119,7 @@ bswap_replace (gimple_stmt_iterator gsi, gimple_set_vuse (load_stmt, n->vuse); gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT); if (conv_code == VIEW_CONVERT_EXPR) - val_tmp = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (tgt), val_tmp); + val_tmp = bswap_view_convert (&gsi, TREE_TYPE (tgt), val_tmp); gimple_assign_set_rhs_with_ops (&gsi, conv_code, val_tmp); update_stmt (cur_stmt); } @@ -1144,7 +1163,7 @@ bswap_replace (gimple_stmt_iterator gsi, if (!is_gimple_val (src)) return NULL_TREE; if (conv_code == VIEW_CONVERT_EXPR) - src = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (tgt), src); + src = bswap_view_convert (&gsi, TREE_TYPE (tgt), src); g = gimple_build_assign (tgt, conv_code, src); } else if (cur_stmt) @@ -1227,7 +1246,7 @@ bswap_replace (gimple_stmt_iterator gsi, tmp = make_temp_ssa_name (bswap_type, NULL, "bswapdst"); tree atmp = tmp; if (conv_code == VIEW_CONVERT_EXPR) - atmp = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (tgt), tmp); + atmp = bswap_view_convert (&gsi, TREE_TYPE (tgt), tmp); convert_stmt = gimple_build_assign (tgt, conv_code, atmp); gsi_insert_after (&gsi, convert_stmt, GSI_SAME_STMT); } --- gcc/testsuite/g++.dg/torture/pr98568.C.jj 2021-01-06 19:19:25.940377456 +0100 +++ gcc/testsuite/g++.dg/torture/pr98568.C 2021-01-06 19:19:16.608482074 +0100 @@ -0,0 +1,37 @@ +// PR tree-optimization/98568 +// { dg-do compile } + +char a[2]; +char b[4]; + +void +foo (int x) +{ + a[1] = x >> 8; + a[0] = x; +} + +void +bar (long long x) +{ + b[3] = x >> 24; + b[2] = x >> 16; + b[1] = x >> 8; + b[0] = x; +} + +void +baz (int x) +{ + a[0] = x >> 8; + a[1] = x; +} + +void +qux (long long x) +{ + b[0] = x >> 24; + b[1] = x >> 16; + b[2] = x >> 8; + b[3] = x; +} Jakub