From patchwork Fri Oct 20 11:19:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 156012 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2010:b0:403:3b70:6f57 with SMTP id fe16csp981167vqb; Fri, 20 Oct 2023 04:20:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEud0TJ+u363vO37s5B0UueTrBXIcJZjTxBALXMnNUoXMHEoNOjBAWd1K9GGX4xXRa7Lppp X-Received: by 2002:a0d:f085:0:b0:589:e4aa:7b67 with SMTP id z127-20020a0df085000000b00589e4aa7b67mr1623298ywe.41.1697800807670; Fri, 20 Oct 2023 04:20:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697800807; cv=pass; d=google.com; s=arc-20160816; b=oLnWFpqatS8KsngYIsLlZT/JkbYiuXuPKFOYeErXicYC29Xse/7Mk7yNoV8r8Sfx5Z aJJrTLazQ+GP40mHVmOE0qDi8FIiFaaVO40l88Oot8ufDKWYSb460BVHF5lgCSwu7rYV uBpyK1dNtPGuDkkwQphbgRCqhLUji8+f9G4ag4vgcP+vT7qQqGHl8IX4lgaHtoTAqxqw NTBIx7sk1owJrsHCiGHoxNwRatBfDj/DTAoJ07DhzhRnYVNO8vbwpfDXjMPzOL3Mo4Dt CB6iujLuYLfoRZuv2SqlpUQXN899OI+HZX3GHqfQg5O/Bnqoncwhbgw8feOXctbqoxU1 9ziw== 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:message-id:mime-version:subject :to:from:date:dkim-signature:dkim-signature:arc-filter:dmarc-filter :delivered-to; bh=WcKNwIzQt0EZChvsif6iNd0cHxOHa0mRWr9JUcFNYG4=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=UeVDDuVXJSxjMiuQ4mmfD2qpNeL1aO8/zojWimF2EcJJr4d93OQU4i1eBDoJ+C8+kk I/FJ2BT3DWgVRHIlaexrOEBPLGuClqH0brmiTU9qUrpV3DD6fx0vgSUV1DAassVD+Dmx WUWBWwUGfx6VjWymXV+2gphbNAVTcLnS/PacrHQDOCi/2KZRkBlLC9vRkK8Dh8JQi10N MSRgoDx6cozixZ427ATWbdIlPprJY1txR0jdlUjvxJkORPBJ9yh9sFRfV62/b0e6GeI1 /5AQOGKjDxYQcnoffBRLB/V2VjONWs2zbdW0YmY9qoWlusT0p1anj5EqqPJ4P9tX6zbY cUtw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=ySmwxRGI; dkim=neutral (no key) header.i=@suse.de header.b=3MlmYrNJ; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id k6-20020a05620a0b8600b00770f3ea0864si1044957qkh.783.2023.10.20.04.20.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 04:20:07 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=ySmwxRGI; dkim=neutral (no key) header.i=@suse.de header.b=3MlmYrNJ; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6C7DD3858C78 for ; Fri, 20 Oct 2023 11:20:07 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id D11403858CDA for ; Fri, 20 Oct 2023 11:19:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D11403858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D11403858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.220.28 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697800783; cv=none; b=XciuhyDx5WtUSpHGkk+6C0cA2eGz0rXaJQKY/KpmLAmTivWEoc+QOGCcLi6i/Hr/pSUWRK7W7bnZarCGMltfAleBgUvpBNL6ssWiGu+ZZ74eSvIIy9lOhUO6nFNR8B4GciZnWTsU9lF4dLPSeEQDoI9cbZxyeBeWWD6ERipQnzY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697800783; c=relaxed/simple; bh=ebpxaWVxfuXY5/tvyInJh2wgJKDOrCbo6UONKGLct0g=; h=DKIM-Signature:DKIM-Signature:Date:From:To:Subject:MIME-Version: Message-Id; b=G/thwUnfcDMy9BodBckN8S0AqG7r1z54DW35WVZwqGRgsZw15/1PT3bdw9g9uMTc20lG7zxS0mJmTgtz9kmurWWdd6CGttwZ7lFH/u3ChShpfKlPDiDdHqjWBcwNHFdiX75OQPdXTiqxiB9LDDyR6N1XcqGKMBJo7muAMRaRBI0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id A364B21A0C for ; Fri, 20 Oct 2023 11:19:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1697800780; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=WcKNwIzQt0EZChvsif6iNd0cHxOHa0mRWr9JUcFNYG4=; b=ySmwxRGIZTVC5966Unh0HggSaUx0nXHlyN76IAC371Q0b91RJ/SP5byQ0ygj/WzWe0REXY VNTnoD/KmihIscmptSmxnr0+g/hMmqq9L2EdoyX5VECNabktcFi3SOAY7MLQGme9waxqmn MIf7dWUAwPGWCvEWZ/Cuy77dsrlEIKU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1697800780; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=WcKNwIzQt0EZChvsif6iNd0cHxOHa0mRWr9JUcFNYG4=; b=3MlmYrNJC/zDNJArZgYBP5CcdLobSH5ZRR3/xarZk6WyAcw33wkhLIdvFuckcqdTMw1V2e Ye1BaZILiD9b2MDw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 8FD0A13584 for ; Fri, 20 Oct 2023 11:19:40 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id w6/BIUxiMmUbagAAMHmgww (envelope-from ) for ; Fri, 20 Oct 2023 11:19:40 +0000 Date: Fri, 20 Oct 2023 13:19:40 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/111000 - restrict invariant motion of shifts MIME-Version: 1.0 Message-Id: <20231020111940.8FD0A13584@imap2.suse-dmz.suse.de> Authentication-Results: smtp-out1.suse.de; none X-Spam-Level: X-Spam-Score: -7.10 X-Spamd-Result: default: False [-7.10 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-3.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[gcc-patches@gcc.gnu.org]; RCPT_COUNT_ONE(0.00)[1]; MID_RHS_MATCH_FROMTLD(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-1.00)[-1.000]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, 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: 1780273179621529797 X-GMAIL-MSGID: 1780273179621529797 The following restricts moving variable shifts to when they are always executed in the loop as we currently do not have an efficient way to rewrite them to something that is unconditionally well-defined and value range analysis will otherwise compute invalid ranges for the shift operand. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/111000 * stor-layout.h (element_precision): Move .. * tree.h (element_precision): .. here. * tree-ssa-loop-im.cc (movement_possibility_1): Restrict motion of shifts and rotates. * gcc.dg/torture/pr111000.c: New testcase. --- gcc/stor-layout.h | 1 - gcc/testsuite/gcc.dg/torture/pr111000.c | 21 +++++++++++++++++++++ gcc/tree-ssa-loop-im.cc | 18 ++++++++++++++++++ gcc/tree.h | 1 + 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr111000.c diff --git a/gcc/stor-layout.h b/gcc/stor-layout.h index e7768921c22..589ce33c950 100644 --- a/gcc/stor-layout.h +++ b/gcc/stor-layout.h @@ -36,7 +36,6 @@ extern void place_field (record_layout_info, tree); extern void compute_record_mode (tree); extern void finish_bitfield_layout (tree); extern void finish_record_layout (record_layout_info, int); -extern unsigned int element_precision (const_tree); extern void finalize_size_functions (void); extern void fixup_unsigned_type (tree); extern void initialize_sizetypes (void); diff --git a/gcc/testsuite/gcc.dg/torture/pr111000.c b/gcc/testsuite/gcc.dg/torture/pr111000.c new file mode 100644 index 00000000000..e6821e1618d --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr111000.c @@ -0,0 +1,21 @@ +/* { dg-do run } */ + +volatile int a = 68; +int b, d, e; +int main() +{ + int t = a; + for (; d <= 6; d++) { + for (b = 0; b <= 6; b++) { + if (t >= 31) + e = d; + else if (d > (647 >> t)) + e = d; + else + e = 0; + } + } + if (e != 6) + __builtin_abort(); + return 0; +} diff --git a/gcc/tree-ssa-loop-im.cc b/gcc/tree-ssa-loop-im.cc index 49aeb685773..396963b6754 100644 --- a/gcc/tree-ssa-loop-im.cc +++ b/gcc/tree-ssa-loop-im.cc @@ -400,6 +400,24 @@ movement_possibility_1 (gimple *stmt) || gimple_could_trap_p (stmt)) return MOVE_PRESERVE_EXECUTION; + if (is_gimple_assign (stmt)) + { + auto code = gimple_assign_rhs_code (stmt); + tree type = TREE_TYPE (gimple_assign_rhs1 (stmt)); + /* For shifts and rotates and possibly out-of-bound shift operands + we currently cannot rewrite them into something unconditionally + well-defined. */ + if ((code == LSHIFT_EXPR + || code == RSHIFT_EXPR + || code == LROTATE_EXPR + || code == RROTATE_EXPR) + && (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST + /* We cannot use ranges at 'stmt' here. */ + || wi::ltu_p (wi::to_wide (gimple_assign_rhs2 (stmt)), + element_precision (type)))) + ret = MOVE_PRESERVE_EXECUTION; + } + /* Non local loads in a transaction cannot be hoisted out. Well, unless the load happens on every path out of the loop, but we don't take this into account yet. */ diff --git a/gcc/tree.h b/gcc/tree.h index 31ea52a5d6b..781297c7e77 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -2235,6 +2235,7 @@ class auto_suppress_location_wrappers #define SET_TYPE_MODE(NODE, MODE) \ (TYPE_CHECK (NODE)->type_common.mode = (MODE)) +extern unsigned int element_precision (const_tree); extern machine_mode element_mode (const_tree); extern machine_mode vector_type_mode (const_tree); extern unsigned int vector_element_bits (const_tree);