Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2374378pxb; Mon, 20 Sep 2021 20:35:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzufn94v17/oSkCjmeS17uyJhUkeUvC96IQiokzlVjsuJegveD4egzYhjmvq7eP62v/0uCz X-Received: by 2002:a17:906:6dc1:: with SMTP id j1mr32742840ejt.324.1632195331223; Mon, 20 Sep 2021 20:35:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632195331; cv=none; d=google.com; s=arc-20160816; b=omIsvs8mxBreOnEwQqsGhkxw7Zo4cS+h0FV7GbxFC9DiG1wTQ7ZkJbP9zlWMNgU3I/ IZAWCrSmiPuf8PJyv1k4DYJy6viXpot5mXTqP1FuZBS7DD8lAsnRFoZWqRm7Uu/kdk7U MwNfHHa8c5NhvUfFXlsouIDCJYFfvOeyMCciAeA0kweJ0dzH2/a88w74wAD02pZW22EC Y3iHk0RYll2nUOSbl40xrEKURRPf4cXTPmHR7jgsWIpcVKsicuXCxO89ZVcn38jDcPlb 0fS1v0T+sgvMUPhktUvSzBBLNuu2t2/jcn3i8aKc5EyE95vAClGPRTypn+vb3ZTfigPA tVKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=TnCJ75BAm/j7qMnlAeBgYvlCk7/MVNum0de+iwrVrF8=; b=BbRdI7hZbu3O87xuNkyu26e8S2kF70ftn4eGY7LVFqUZcbAIsGxKMJiqqNbdATQMC0 q5zyfrLt8uNHeaQj4tBLgLk1BbplOGIh68cFiQA2E9jjfHPkLALeAjCgQrkVyBOf7yZK k+clZw4JQhMiY31lmsMeRkYRgfESMa5mYLlK8ebxxUCddkrJXKk6A4QAllJLlcRX1RG6 vUWZ4NCV9n2qNqNxFU0Qzo3IcKhrS5bCKPWorViwbV/XJVfow6uAVPkQkFiVccmd0TRr xg0GnpzKKLJDc72ypDW4QjLy68pcjDcQfjXA0BT5GcQhnxwNCPzIp2oyAyj/k4vKiqzl xb7w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 21si10747407ejc.493.2021.09.20.20.35.08; Mon, 20 Sep 2021 20:35:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238461AbhITVcF (ORCPT + 99 others); Mon, 20 Sep 2021 17:32:05 -0400 Received: from gate.crashing.org ([63.228.1.57]:39075 "EHLO gate.crashing.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241065AbhITVaE (ORCPT ); Mon, 20 Sep 2021 17:30:04 -0400 Received: from gate.crashing.org (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id 18KLN6Wr012714; Mon, 20 Sep 2021 16:23:06 -0500 Received: (from segher@localhost) by gate.crashing.org (8.14.1/8.14.1/Submit) id 18KLN4s2012713; Mon, 20 Sep 2021 16:23:04 -0500 X-Authentication-Warning: gate.crashing.org: segher set sender to segher@kernel.crashing.org using -f Date: Mon, 20 Sep 2021 16:23:04 -0500 From: Segher Boessenkool To: Christophe Leroy Cc: Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v4 1/3] powerpc/bitops: Use immediate operand when possible Message-ID: <20210920212303.GZ1583@gate.crashing.org> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.3i Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi! On Mon, Sep 20, 2021 at 10:31:17AM +0200, Christophe Leroy wrote: > Today we get the following code generation for bitops like > set or clear bit: > > c0009fe0: 39 40 08 00 li r10,2048 > c0009fe4: 7c e0 40 28 lwarx r7,0,r8 > c0009fe8: 7c e7 53 78 or r7,r7,r10 > c0009fec: 7c e0 41 2d stwcx. r7,0,r8 > > c000d568: 39 00 18 00 li r8,6144 > c000d56c: 7c c0 38 28 lwarx r6,0,r7 > c000d570: 7c c6 40 78 andc r6,r6,r8 > c000d574: 7c c0 39 2d stwcx. r6,0,r7 > > Most set bits are constant on lower 16 bits, so it can easily > be replaced by the "immediate" version of the operation. Allow > GCC to choose between the normal or immediate form. You can also handle the second sixteen bits (the "shifted" half), by using oris etc. The "%eN" output modifier prints an "s" for this: /* If the low 16 bits are 0, but some other bit is set, write 's'. */ But this doesn't handle non-constant arguments, so you're likely better off using what you have noe. > For clear bits, on 32 bits 'rlwinm' can be used instead of 'andc' for > when all bits to be cleared are consecutive. Or when all you want to keep are consecutive (you do handle that now :-) ) > On 64 bits we don't have any equivalent single operation for clearing, > single bits or a few bits, we'd need two 'rldicl' so it is not > worth it, the li/andc sequence is doing the same. You can use rlwinm whenever you want to clear all top 32 bits. A sometimes nice idiom is ori x,x,N ; xori x,x,N to clear the bits N (or oris/xoris). But it's two insns no matter what (but no spare register is needed). > Signed-off-by: Christophe Leroy > +static inline unsigned long test_and_clear_bits(unsigned long mask, volatile unsigned long *_p) > +{ > + unsigned long old, t; > + unsigned long *p = (unsigned long *)_p; > + > + if (IS_ENABLED(CONFIG_PPC32) && > + __builtin_constant_p(mask) && is_rlwinm_mask_valid(mask)) { is_rlwinm_mask_valid(~mask)? So that test_and_clear_bits(0, ...) will work with rlwinm, and test_and_clear_bits(0xffffffff, ...) will not make gas scream bloody murder ("illegal bitmask"). Tha mask you pass to the instruction is ~mask after all. Looks great except that one nit. Thanks :-) Reviewed-by: Segher Boessenkool Segher