From: Christopher Li <sparse@chrisli.org>
To: Bernd Petrovitsch <bernd@petrovitsch.priv.at>
Cc: "Jacek Śliwerski" <sliwers@googlemail.com>, linux-sparse@vger.kernel.org
Subject: Re: Defect in linearization of short circuit &&
Date: Tue, 16 Feb 2010 11:02:40 -0800 [thread overview]
Message-ID: <70318cbf1002161102n63995222o914299dd358f594d@mail.gmail.com> (raw)
In-Reply-To: <1266312482.3433.33.camel@thorin>
On Tue, Feb 16, 2010 at 1:28 AM, Bernd Petrovitsch
<bernd@petrovitsch.priv.at> wrote:
>> $ ./test-linearize parser_check.c
>> parser_check:
>> .L0x7f4e12de3130:
>> <entry-point>
>> br %arg1, .L0x7f4e12de32e0, .L0x7f4e12de3250
> I assume this means "if %arg1 == NULL goto .L0x7f4e12de32e0 else goto .L0x7f4e12de3250"
That is right.
> I assume this is the "i > 0" check.
No, this is the not the i > 0 check yet. This is setting the expression value
of the false branch to 0.
>> phisrc.1 %phi2 <- $0
>> br .L0x7f4e12de3298
>>
>> .L0x7f4e12de3298:
>> phi.1 %r8 <- %phi1, %phi2
>> setgt.32 %r10 <- %arg2, $0
This is the "i > 0" check. (%arg2 is "i")
>> and-bool.1 %r11 <- %r8, %r10
>> br %r11, .L0x7f4e12de3178, .L0x7f4e12de31c0
>>
>> .L0x7f4e12de3178:
>> call execute_a, %arg1, %arg2
>> br .L0x7f4e12de3328
>>
>> In the fast test, the false branch is L0x7f4e12de3250.
>> Which is doing the (i > 0) part and it is safe to do so.
> Are saying that he "i >0 " test done while "st == NULL"?
> This is actually wrong as it shouldn't be done (independent of the used
> variables and especially if the expression has side effects).
That is right. "i > 0" is still being tested.
Consider this example. The same source code and be compile into two
different piece of machine code. They produce the exact same result.
The one with this optimization runs faster than the other one. Do you still
thing that is wrong? How about x86 CPU internally re-ordering the instruction,
is that wrong too?
So I think as long as this behavior is *internal* and *correct*. It is fine.
Of course it'd better be faster as well. Otherwise, what is the point.
The goal of the this optimization is trading the branch instruction
with cheaper arithmetic instruction.
For modern CPU, the branch instruction is likely more expensive
than normal arithmetic instruction. In X86, branch instruction *might*
flush the instruction pipe line while arithmetic operation can usually done
in one cycle.
That is why this optimization is using a cost model. If the left hand
side cost is lower than the branch instruction cost, and it is *safe* to
do so. It turn the branch instruction into arithmetic instruction.
However, in this case, the optimization did not consider this expression
is used in the if statement, and if statement has build-in branch instruction
associate with it. So this optimization might not worthy while here.
But for this expression:
a = st && st->foo && st->bar && i > 0;
This optimization might be worth while.
If the optimization produce incorrect result. That is fatal and that
is a serious bug.
I just want to confirm that is no the case here.
Chris
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
next prev parent reply other threads:[~2010-02-16 19:02 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-02-14 13:39 Defect in linearization of short circuit && Jacek Śliwerski
2010-02-14 21:04 ` Jacek Śliwerski
2010-02-14 23:09 ` Christopher Li
2010-02-15 19:12 ` Jacek Śliwerski
2010-02-15 19:41 ` Christopher Li
2010-02-15 20:18 ` Jacek Śliwerski
2010-02-15 21:11 ` Christopher Li
2010-02-16 9:28 ` Bernd Petrovitsch
2010-02-16 19:02 ` Christopher Li [this message]
2010-02-16 19:10 ` Christopher Li
2010-02-16 19:19 ` Jacek Śliwerski
2010-02-16 19:36 ` Christopher Li
2010-02-16 20:11 ` enum warning patch (was Re: Defect in linearization of short circuit &&) Kamil Dudka
2010-02-16 20:18 ` Kamil Dudka
2010-02-16 22:44 ` Christopher Li
2010-02-17 14:00 ` Kamil Dudka
2010-02-17 11:47 ` Defect in linearization of short circuit && Bernd Petrovitsch
2010-02-17 20:22 ` Christopher Li
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=70318cbf1002161102n63995222o914299dd358f594d@mail.gmail.com \
--to=sparse@chrisli.org \
--cc=bernd@petrovitsch.priv.at \
--cc=linux-sparse@vger.kernel.org \
--cc=sliwers@googlemail.com \
/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;
as well as URLs for NNTP newsgroup(s).