From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id 316743858D32 for ; Mon, 12 Jun 2023 21:29:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 316743858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-652a6cf1918so3851021b3a.1 for ; Mon, 12 Jun 2023 14:29:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686605343; x=1689197343; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=uKSODivfmiNjWT/fW/E3gPbTIPQII+bjVuu46fBlv5A=; b=M9ROd8ZuCb0gqQd7sI+Tx4Q4T1AyxIAjgLkMZlPbB+g6ny+dohzqJpBOs+mQeXXl88 PrxFohzKnKbpPII2OklC05DCdZrmXJJrHWOd0hM/0/jSYtbL3RlEIBPusBWZd8PmXR2F ypqE3nquppDUtcUNCi+Kd593IrpcxrZxt6YSGvQF7H8SV0XhQiR69K7uiBjSBWsXk5Fn ZgghT2gOfWlqCar2Gd/2NokHIGLHq2aFSIDRXuUisIp1jonJhWBfEMa1qXKIdeR+Y2rV 9rprZY1N+HkC8djzABun92H3OZYBMWeG3B7+WKNVxwPqj6ij2H/kmqnQamF1kaNTU9CV /dRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686605343; x=1689197343; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uKSODivfmiNjWT/fW/E3gPbTIPQII+bjVuu46fBlv5A=; b=b58/HaDxetAbNo7TQQuLzdfBR7T5CUFA0/iyxx+pX9NMs1DM2gqpqbrKidSpwiRFsQ hZTgLrW8YpOrYq46CIqo3aDEJUz+FNqsYeewRDUWxy2kvWVxtNLayukP735WSMKxVdet 5V5kKEASN1HKP4jlupPOdhPWIzJGZBAiQ/gojKRJBe6HJ+4ksgfRv/wXyOSdrItNwGph rGGy0Z1U3TLyA7SW/pILS5jfrgNp2ILYUkjZJvPnXaCa5tR1CnO80H/CmS8b4/D2uDMd 0LZO1yxdqs1/g+Rusq8nPWCSV3GWu58/W6huCVRjfo5R3V3hA0BBc1mmHdHbOT8CrMiO RMvw== X-Gm-Message-State: AC+VfDzplL5u1xyUKTS7jw/VyNNEp0/bGOltdJhNgtCKNhQKSMvzfmmu TlblXVmqw8BYuBPYJwQXr7Obs5Gjli0= X-Google-Smtp-Source: ACHHUZ4DaO/VQXSU93IQRn98kFkPvpgQgdFvOJly0eqs8xRpMCBaLiR/KPG9n/bI0j1S/kRwnYUuow== X-Received: by 2002:a05:6a00:a1f:b0:65c:e35b:b13a with SMTP id p31-20020a056a000a1f00b0065ce35bb13amr12999269pfh.14.1686605342838; Mon, 12 Jun 2023 14:29:02 -0700 (PDT) Received: from [172.31.0.109] ([136.36.130.248]) by smtp.gmail.com with ESMTPSA id d4-20020aa78e44000000b00662c4ca18ebsm86746pfr.128.2023.06.12.14.29.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 12 Jun 2023 14:29:02 -0700 (PDT) Message-ID: <94dfe022-e9e6-c30f-b906-81b681fa5ba8@gmail.com> Date: Mon, 12 Jun 2023 15:29:00 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Subject: Re: [PATCH] combine: Narrow comparison of memory and constant Content-Language: en-US To: Stefan Schulze Frielinghaus , gcc-patches@gcc.gnu.org References: <20230612075737.1801-1-stefansf@linux.ibm.com> From: Jeff Law In-Reply-To: <20230612075737.1801-1-stefansf@linux.ibm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-8.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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 List-Id: On 6/12/23 01:57, Stefan Schulze Frielinghaus via Gcc-patches wrote: > Comparisons between memory and constants might be done in a smaller mode > resulting in smaller constants which might finally end up as immediates > instead of in the literal pool. > > For example, on s390x a non-symmetric comparison like > x <= 0x3fffffffffffffff > results in the constant being spilled to the literal pool and an 8 byte > memory comparison is emitted. Ideally, an equivalent comparison > x0 <= 0x3f > where x0 is the most significant byte of x, is emitted where the > constant is smaller and more likely to materialize as an immediate. > > Similarly, comparisons of the form > x >= 0x4000000000000000 > can be shortened into x0 >= 0x40. > > I'm not entirely sure whether combine is the right place to implement > something like this. In my first try I implemented it in > TARGET_CANONICALIZE_COMPARISON but then thought other targets might > profit from it, too. simplify_context::simplify_relational_operation_1 > seems to be the wrong place since code/mode may change. Any opinions? > > gcc/ChangeLog: > > * combine.cc (simplify_compare_const): Narrow comparison of > memory and constant. > (try_combine): Adapt new function signature. > (simplify_comparison): Adapt new function signature. > > gcc/testsuite/ChangeLog: > > * gcc.target/s390/cmp-mem-const-1.c: New test. > * gcc.target/s390/cmp-mem-const-2.c: New test. This does seem more general than we'd want to do in the canonicalization hook. So thanks for going the extra mile and doing a generic implementation. > @@ -11987,6 +11988,79 @@ simplify_compare_const (enum rtx_code code, machine_mode mode, > break; > } > > + /* Narrow non-symmetric comparison of memory and constant as e.g. > + x0...x7 <= 0x3fffffffffffffff into x0 <= 0x3f where x0 is the most > + significant byte. Likewise, transform x0...x7 >= 0x4000000000000000 into > + x0 >= 0x40. */ > + if ((code == LEU || code == LTU || code == GEU || code == GTU) > + && is_a (GET_MODE (op0), &int_mode) > + && MEM_P (op0) > + && !MEM_VOLATILE_P (op0) > + && (unsigned HOST_WIDE_INT)const_op > 0xff) > + { > + unsigned HOST_WIDE_INT n = (unsigned HOST_WIDE_INT)const_op; > + enum rtx_code adjusted_code = code; > + > + /* If the least significant bit is already zero, then adjust the > + comparison in the hope that we hit cases like > + op0 <= 0x3ffffdfffffffffe > + where the adjusted comparison > + op0 < 0x3ffffdffffffffff > + can be shortened into > + op0' < 0x3ffffd. */ > + if (code == LEU && (n & 1) == 0) > + { > + ++n; > + adjusted_code = LTU; > + } > + /* or e.g. op0 < 0x4020000000000000 */ > + else if (code == LTU && (n & 1) == 0) > + { > + --n; > + adjusted_code = LEU; > + } > + /* or op0 >= 0x4000000000000001 */ > + else if (code == GEU && (n & 1) == 1) > + { > + --n; > + adjusted_code = GTU; > + } > + /* or op0 > 0x3fffffffffffffff. */ > + else if (code == GTU && (n & 1) == 1) > + { > + ++n; > + adjusted_code = GEU; > + } > + > + scalar_int_mode narrow_mode_iter; > + bool lower_p = code == LEU || code == LTU; > + bool greater_p = !lower_p; > + FOR_EACH_MODE_UNTIL (narrow_mode_iter, int_mode) > + { > + unsigned nbits = GET_MODE_PRECISION (int_mode) > + - GET_MODE_PRECISION (narrow_mode_iter); > + unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << nbits) - 1; > + unsigned HOST_WIDE_INT lower_bits = n & mask; > + if ((lower_p && lower_bits == mask) > + || (greater_p && lower_bits == 0)) > + { > + n >>= nbits; > + break; > + } > + } > + > + if (narrow_mode_iter < int_mode) > + { > + poly_int64 offset = BYTES_BIG_ENDIAN > + ? 0 > + : GET_MODE_SIZE (int_mode) > + - GET_MODE_SIZE (narrow_mode_iter); Go ahead and add some parenthesis here. I'd add one pair around the whole RHS of that assignment. The '?' and ':' would line up under the 'B' in that case. Similarly add them around the false arm of the ternary. The '-' will line up under the 'G'. Going to trust you got the little endian adjustment correct here ;-) > > /* Compute some predicates to simplify code below. */ > diff --git a/gcc/testsuite/gcc.target/s390/cmp-mem-const-1.c b/gcc/testsuite/gcc.target/s390/cmp-mem-const-1.c > new file mode 100644 > index 00000000000..b90c2a8c224 > --- /dev/null > +++ b/gcc/testsuite/gcc.target/s390/cmp-mem-const-1.c > @@ -0,0 +1,99 @@ > +/* { dg-do compile { target { lp64 } } } */ > +/* { dg-options "-O1 -march=z13 -mzarch" } */ > +/* { dg-final { scan-assembler-not {\tclc\t} } } */ > + > +int > +ge_8b (unsigned long *x) > +{ > + return *x >= 0x4000000000000000; > +} Would it be possible to add some debugging output in simplify_compare_const so that you could search for that debugging output and make these tests generic? Jeff