Skip to content
Snippets Groups Projects
Commit a0b4e09a authored by Ivan Sorokin's avatar Ivan Sorokin Committed by H.J. Lu
Browse files

x86: Build only one __cpu_model/__cpu_features2 variables

GCC -O2 generated quite bad code for this function:

bool
f (void)
{
  return __builtin_cpu_supports("popcnt")
	 && __builtin_cpu_supports("ssse3");
}

f:
	movl	__cpu_model+12(%rip), %edx
	movl	%edx, %eax
	shrl	$6, %eax
	andl	$1, %eax
	andl	$4, %edx
	movl	$0, %edx
	cmove	%edx, %eax
	ret

The problem was caused by the fact that internally every invocation of
__builtin_cpu_supports built a new variable __cpu_model and a new type
__processor_model.  Because of this, GIMPLE level optimizers weren't able
to CSE the loads of __cpu_model and optimize bit-operations properly.

Improve GCC -O2 code generation by caching __cpu_model and__cpu_features2
variables as well as their types:

f:
        movl    __cpu_model+12(%rip), %eax
        andl    $68, %eax
        cmpl    $68, %eax
        sete    %al
        ret

2021-05-05  Ivan Sorokin <vanyacpp@gmail.com>
	    H.J. Lu <hjl.tools@gmail.com>

gcc/

	PR target/91400
	* config/i386/i386-builtins.c (ix86_cpu_model_type_node): New.
	(ix86_cpu_model_var): Likewise.
	(ix86_cpu_features2_type_node): Likewise.
	(ix86_cpu_features2_var): Likewise.
	(fold_builtin_cpu): Cache __cpu_model and __cpu_features2 with
	their types.

gcc/testsuite/

	PR target/91400
	* gcc.target/i386/pr91400-1.c: New test.
	* gcc.target/i386/pr91400-2.c: Likewise.
parent 2254b323
No related branches found
No related tags found
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment