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.129.124]) by sourceware.org (Postfix) with ESMTPS id 7A8803858D39 for ; Tue, 1 Aug 2023 21:36:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7A8803858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690925804; 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:in-reply-to:in-reply-to: references:references; bh=wUqObf8LYel2khsZ7A4YZmqQ3W0JOd2sCsRx4xbE9vk=; b=ZWHgaSDNr3oPjDquVmdU9pF9NaBQ27iZnHjBaGcCiQBTVwU75+uYrb0XUWJ/4NvZbBbM1V 6nx/rUvsf38L98O5sRuuVGjMDsAIYkiqtb3WIrwnwUmgQpZerZn8g0lsvLdQWycrRfzRy2 wG/vQx93XrM05VmPEGSjXbbCpiABTdM= 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-515-bvWjAxI_OuWDeUiprm90fQ-1; Tue, 01 Aug 2023 17:36:42 -0400 X-MC-Unique: bvWjAxI_OuWDeUiprm90fQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 540C4805C10 for ; Tue, 1 Aug 2023 21:36:42 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 19D02F7821; Tue, 1 Aug 2023 21:36:41 +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 371Ladnt889652 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 1 Aug 2023 23:36:40 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 371LadRw889567; Tue, 1 Aug 2023 23:36:39 +0200 Date: Tue, 1 Aug 2023 23:36:38 +0200 From: Jakub Jelinek To: Drew Ross Cc: gcc-patches@gcc.gnu.org Subject: Re: [PATCH] match.pd: Canonicalize (signed x << c) >> c [PR101955] Message-ID: Reply-To: Jakub Jelinek References: <20230801192033.432742-1-drross@redhat.com> MIME-Version: 1.0 In-Reply-To: <20230801192033.432742-1-drross@redhat.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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=-9.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE,WEIRD_PORT autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Tue, Aug 01, 2023 at 03:20:33PM -0400, Drew Ross via Gcc-patches wrote: > Canonicalizes (signed x << c) >> c into the lowest > precision(type) - c bits of x IF those bits have a mode precision or a > precision of 1. Also combines this rule with (unsigned x << c) >> c -> x & > ((unsigned)-1 >> c) to prevent duplicate pattern. Tested successfully on > x86_64 and x86 targets. > > PR middle-end/101955 > > gcc/ChangeLog: > > * match.pd ((signed x << c) >> c): New canonicalization. > > gcc/testsuite/ChangeLog: > > * gcc.dg/pr101955.c: New test. > --- > gcc/match.pd | 20 +++++++---- > gcc/testsuite/gcc.dg/pr101955.c | 63 +++++++++++++++++++++++++++++++++ > 2 files changed, 77 insertions(+), 6 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/pr101955.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 8543f777a28..62f7c84f565 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -3758,13 +3758,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > - TYPE_PRECISION (TREE_TYPE (@2))))) > (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1)))) > > -/* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned > - types. */ > +/* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for > + unsigned x OR truncate into the precision(type) - c lowest bits > + of signed x (if they have mode precision or a precision of 1) */ There should be . between ) and " */" above. > (simplify > - (rshift (lshift @0 INTEGER_CST@1) @1) > - (if (TYPE_UNSIGNED (type) > - && (wi::ltu_p (wi::to_wide (@1), element_precision (type)))) > - (bit_and @0 (rshift { build_minus_one_cst (type); } @1)))) > + (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1) > + (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))) > + (if (TYPE_UNSIGNED (type)) > + (bit_and @0 (rshift { build_minus_one_cst (type); } @1)) This needs to be (convert @0) instead of @0, because now that there is the nop_convert? in between, @0 could have different type than type. I certainly see regressions on gcc.c-torture/compile/950612-1.c on i686-linux because of this: /home/jakub/src/gcc/gcc/testsuite/gcc.c-torture/compile/950612-1.c:17:1: error: type mismatch in binary expression long long unsigned int long long int long long unsigned int _346 = _3 & 4294967295; during GIMPLE pass: forwprop /home/jakub/src/gcc/gcc/testsuite/gcc.c-torture/compile/950612-1.c:17:1: internal compiler error: verify_gimple failed 0x9018a4e verify_gimple_in_cfg(function*, bool, bool) ../../gcc/tree-cfg.cc:5646 0x8e81eb5 execute_function_todo ../../gcc/passes.cc:2088 0x8e8234c do_per_function ../../gcc/passes.cc:1687 0x8e82431 execute_todo ../../gcc/passes.cc:2142 Please submit a full bug report, with preprocessed source (by using -freport-bug). Please include the complete backtrace with any bug report. See for instructions. > + (if (INTEGRAL_TYPE_P (type)) > + (with { > + int width = element_precision (type) - tree_to_uhwi (@1); > + tree stype = build_nonstandard_integer_type (width, 0); > + } > + (if (width == 1 || type_has_mode_precision_p (stype)) > + (convert (convert:stype @0)))))))) just one space before == instead of two > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr101955.c > @@ -0,0 +1,63 @@ > +/* { dg-do compile } */ The above line should be /* { dg-do compile { target int32 } } */ because the test relies on 32-bit int, some targets have just 16-bit int. Of course, unless you want to make the testcase more portable, by using say #define CHAR_BITS __CHAR_BIT__ #define INT_BITS (__SIZEOF_INT__ * __CHAR_BIT__) #define LLONG_BITS (__SIZEOF_LONGLONG__ * __CHAR_BIT__) and replacing all the 31, 24, 56 etc. constants with (INT_BITS - 1), (INT_BITS - CHAR_BITS), (LLONG_BITS - CHAR_BITS) etc. Though, it would still fail on some AVR configurations which have (invalid for C) just 8-bit int, and the question is what to do with that 16, because (INT_BITS - 2 * CHAR_BITS) is 0 on 16-bit ints, so it would need to be (INT_BITS / 2) instead. C requires that long long is at least 64-bit, so that is less problematic (no known target to have > 64-bit long long, though theoretically possible). > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > + Jakub