From: Denis Efremov <efremov@linux.com>
To: Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Julia Lawall <julia.lawall@lip6.fr>
Cc: Joe Perches <joe@perches.com>,
linux-kernel@vger.kernel.org,
Gilles Muller <Gilles.Muller@lip6.fr>,
Nicolas Palix <nicolas.palix@imag.fr>,
Michal Marek <michal.lkml@markovi.net>
Subject: Re: [PATCH] scripts: coccinelle: check for !(un)?likely usage
Date: Wed, 28 Aug 2019 16:14:32 +0300 [thread overview]
Message-ID: <40010cac-8a49-9d82-4cde-e14a0216c340@linux.com> (raw)
In-Reply-To: <75f5210f-43a3-ab0d-912a-6dff6163fd9a@rasmusvillemoes.dk>
On 8/28/19 4:05 PM, Rasmus Villemoes wrote:
> On 28/08/2019 14.33, Denis Efremov wrote:
>> On 8/28/19 2:33 PM, Rasmus Villemoes wrote:
>>> On 25/08/2019 21.19, Julia Lawall wrote:
>>>>
>>>>
>>>>> On 26 Aug 2019, at 02:59, Denis Efremov <efremov@linux.com> wrote:
>>>>>
>>>>>
>>>>>
>>>>>> On 25.08.2019 19:37, Joe Perches wrote:
>>>>>>> On Sun, 2019-08-25 at 16:05 +0300, Denis Efremov wrote:
>>>>>>> This patch adds coccinelle script for detecting !likely and !unlikely
>>>>>>> usage. It's better to use unlikely instead of !likely and vice versa.
>>>>>>
>>>>>> Please explain _why_ is it better in the changelog.
>>>>>>
>>>>>
>>>>> In my naive understanding the negation (!) before the likely/unlikely
>>>>> could confuse the compiler
>>>>
>>>> As a human I am confused. Is !likely(x) equivalent to x or !x?
>>>
>>> #undef likely
>>> #undef unlikely
>>> #define likely(x) (x)
>>> #define unlikely(x) (x)
>>>
>>> should be a semantic no-op. So changing !likely(x) to unlikely(x) is
>>> completely wrong. If anything, !likely(x) can be transformed to
>>> unlikely(!x).
>>
>> As far as I could understand it:
>>
>> # define likely(x) __builtin_expect(!!(x), 1)
>> # define unlikely(x) __builtin_expect(!!(x), 0)
>>
>> From GCC doc:
>> __builtin_expect compares the values. The semantics of the built-in are that it is expected that exp == c.
>
> When I said "semantic" I meant from the C language point of view. Yes,
> of course, the whole reason for having these is that we can give hints
> to gcc as to which branch is more likely. Replace the dummy defines by
> #define likely(x) (!!(x)) if you like - it amounts to the same thing
> when it's only ever used in a boolean context.
>
>> if (!likely(cond))
>> if (!__builtin_expect(!!(cond), 1))
>> if (!((!!(cond)) == 1))
>
> You're inventing this comparison to 1. It should be "if (!(!!(cond)))",
> but it ends up being equivalent in C.
>
>> if ((!!(cond)) != 1) and since !! could result in 0 or 1
>> if ((!!(cond)) == 0)
>
> which in turn is equivalent to !(cond).
>
>>
>> if (unlikely(cond))
>> if (__builtin_expect(!!(cond), 0))
>> if ((!!(cond)) == 0))
>
> No, that last transformation is wrong. Yes, the _expectation_ is that
> !!(cond) has the value 0, but that does not mean that the whole
> condition turns into "does !!(cond) compare equal to 0?" - we _expect_
> that it does, meaning that we expect not to enter the if block. Read the
> docs, the value of __builtin_expect(whatever, foobar) is whatever, so a
> correct third line above would be
>
> "if (!!(cond))"
>
> which is of course not at all the same as
>
> "if (!!(cond) == 0)" aka "if (!(cond))"
I get it, you are right. Thank you for the explanation.
Denis
next prev parent reply other threads:[~2019-08-28 13:14 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-25 13:05 [PATCH] scripts: coccinelle: check for !(un)?likely usage Denis Efremov
2019-08-25 15:30 ` Markus Elfring
2019-08-25 15:30 ` Markus Elfring
2019-08-25 21:06 ` Denis Efremov
2019-08-25 16:37 ` Joe Perches
2019-08-25 18:59 ` Denis Efremov
2019-08-25 19:19 ` Julia Lawall
2019-08-28 11:33 ` Rasmus Villemoes
2019-08-28 11:59 ` Joe Perches
2019-08-28 12:33 ` Denis Efremov
2019-08-28 13:05 ` Rasmus Villemoes
2019-08-28 13:14 ` Denis Efremov [this message]
2019-08-28 12:33 ` Julia Lawall
2019-08-28 12:41 ` Denis Efremov
2019-08-28 13:57 ` Denis Efremov
2019-08-25 21:19 ` Denis Efremov
2019-09-01 17:24 ` Pavel Machek
2019-09-01 17:39 ` Denis Efremov
2019-08-29 17:10 ` [PATCH v2] " Denis Efremov
2019-08-29 17:13 ` Denis Efremov
2019-08-30 0:42 ` Julia Lawall
2019-08-30 6:56 ` Denis Efremov
2019-08-30 8:06 ` Rasmus Villemoes
2019-08-29 20:07 ` Markus Elfring
2019-08-30 7:55 ` Markus Elfring
2019-09-06 20:19 ` Julia Lawall
2019-09-06 20:55 ` Denis Efremov
2019-09-07 8:05 ` [v2] " Markus Elfring
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=40010cac-8a49-9d82-4cde-e14a0216c340@linux.com \
--to=efremov@linux.com \
--cc=Gilles.Muller@lip6.fr \
--cc=joe@perches.com \
--cc=julia.lawall@lip6.fr \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=michal.lkml@markovi.net \
--cc=nicolas.palix@imag.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox