This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/66520] bad code generated code for branches with single &


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66520

--- Comment #2 from Fisnik <fkastrati at gmail dot com> ---
(In reply to Eric Botcazou from comment #1)
> > consider the following code snippet (c++):
> > 
> > void ampamp(int x, int y) {
> >   if (x < 3 && y > 10 ) 
> >     printf("%d%d", x, y);
> > }
> > 
> > void amp(int x, int y) {
> >   if ((x < 3) & (y > 10) ) 
> >     printf("%d%d", x, y);
> > }
> > 
> > 
> > the assembly code generated by g++ (all versions I tested with  optimization
> > flag `-O3'), is not optimal (see the link on the bottom of this message).
> > Basically, for both methods, the generated assembly code is identical.
> 
> An optimizing compiler should generate the same code in both cases, either
> with or without branches, whatever form is deemed the fastest for the
> target, since
> there are no side-effects involved in the evaluation of the comparisons.
> 
> > As a side note: the code by intel's  compiler (ICC) is however generating
> > optimal code for such scenarios, at least for versions icc13, and icc15 that
> > I've tested.
> 
> One of the forms is necessarily not optimal (unless they are equivalent).

I do not quite agree. If the first operand is "always" true, than the version
with double ampersand `&&' is faster. But if probability of the first operand
being true is around 0.5, then the version with single `&' is faster, given
that the compiler generates the correct assembly, i.e., a single jump. The
reason for this is the branch misprediction penalty, which is severe at
probabilities around 0.5.

I already did experiments with both methods `&', and `&&', and varying
probabilities for predicates being true.
The difference (in speed) is around factor 2.3, as the selectivity of the first
predicate approaches 0.5, (i.e., the probability of the first operand being
true -> 0.5).
That is, the method with single ampersand with beat clearly the method with
double ampersand.

The predicates I used were simple, but for more expensive predicates the
difference is even worse.
If still not convinced, read the paper: "Conjunctive selection conditions in
main memory" by K.Ross.

There should be at least some flag available, such that we can set such a flag
and have the compiler generate only a single jump for the method with single
ampersand.



[reply] [-] Comment 4


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]