Why gcc's code is so much slower than clang's?

Xi Ruoyao xry111@xry111.site
Mon Oct 23 15:50:16 GMT 2023


On Mon, 2023-10-23 at 17:27 +0300, Alexander Monakov wrote:
> 
> On Mon, 23 Oct 2023, Georg-Johann Lay wrote:
> 
> > For the C code below, I am getting execution times of around 8 to
> > 8.3
> > seconds with gcc (v11.3 and v13.2) and around 5 seconds with clang
> > v17.
> > 
> > Only options I used were -O3 (-Os or -Ofast didn't make a
> > difference).
> > 
> > Architecture is x86_64-linux-gnu, Dual Core Intel Core2
> > 
> > I ran the code below with
> > 
> > > gcc coll.c -o coll.x -O3 && time -p ./coll.x
> > 
> > I wouldn't ask this question if I hadn't observed similar thing
> > with other programs already, any I am wondering if I am missing
> > something crucial like supplying GCC with better options?
> 
> In this specific program, Clang translates
> 
>     i = (i & 1)
>         ? (3 * i + 1) >> 1
>         : i >> 1;
> 
> into a straight-line code involving a conditional move, while GCC
> emits a conditional branch. That branch turns out to be poorly
> predictable, causing GCC-compiled program to run slower, even though
> it executes much fewer instructions (~13 billion vs ~20 billion).
> 
> (I used 'perf stat' to obtain the instruction counts)

GCC even uses branch instead of cmov for:

int test(int x)
{
	return x & 1 ? 3 * x + 1 : x / 2;
}

Should we create a ticket in bugzilla or is there some reason not to use
cmov here?

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University


More information about the Gcc-help mailing list