https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103252
--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> --- (In reply to Jason A. Donenfeld from comment #2) > Here's a more minimal test case: https://gcc.godbolt.org/z/15hnsb6of kmovd k0, ecx mov ecx, DWORD PTR __libc_tsd_CTYPE_B@gotntpoff[ebx] kmovd eax, k0 mov ecx, DWORD PTR gs:[ecx] test BYTE PTR 1[ecx+eax*2], 32 vs: mov ecx, DWORD PTR __libc_tsd_CTYPE_B@gotntpoff[ebx] mov ecx, DWORD PTR gs:[ecx] test BYTE PTR 1[ecx+edx*2], 32 Maybe it could have directly used eax in the first instruction but other than that, I don't see any issue with this code generation. Before RA: (insn 162 161 163 15 (set (reg:SI 130 [ c ]) (sign_extend:SI (reg/v:QI 104 [ c ]))) "/app/example.c":81:68 156 {extendqisi2} (expr_list:REG_DEAD (reg/v:QI 104 [ c ]) (nil))) (insn 163 162 273 15 (set (reg:SI 132) (mem/u/c:SI (plus:SI (reg:SI 82) (const:SI (unspec:SI [ (symbol_ref:SI ("__libc_tsd_CTYPE_B") [flags 0x60] <var_decl 0x7f34b0cb4c60 __libc_tsd_CTYPE_B>) ] UNSPEC_GOTNTPOFF))) [7 S4 A8])) "/app/example.c":81:67 77 {*movsi_internal} (expr_list:REG_EQUIV (mem/u/c:SI (plus:SI (reg:SI 82) (const:SI (unspec:SI [ (symbol_ref:SI ("__libc_tsd_CTYPE_B") [flags 0x60] <var_decl 0x7f34b0cb4c60 __libc_tsd_CTYPE_B>) ] UNSPEC_GOTNTPOFF))) [7 S4 A8]) (nil))) (insn 273 163 165 15 (set (reg/f:SI 131 [ __libc_tsd_CTYPE_B ]) (mem/f/c:SI (reg:SI 132) [4 __libc_tsd_CTYPE_B+0 S4 A32 AS2])) "/app/example.c":81:67 77 {*movsi_internal} (expr_list:REG_DEAD (reg:SI 132) (nil))) (note 165 273 166 15 NOTE_INSN_DELETED) (note 166 165 274 15 NOTE_INSN_DELETED) (insn 274 166 168 15 (set (reg:CCZ 17 flags) (compare:CCZ (and:QI (mem:QI (plus:SI (plus:SI (mult:SI (reg:SI 130 [ c ]) (const_int 2 [0x2])) (reg/f:SI 131 [ __libc_tsd_CTYPE_B ])) (const_int 1 [0x1])) [5 *_12+1 S1 A8]) (const_int 32 [0x20])) (const_int 0 [0]))) "/app/example.c":81:43 502 {*testqi_1_maybe_si} (expr_list:REG_DEAD (reg/f:SI 131 [ __libc_tsd_CTYPE_B ]) (expr_list:REG_DEAD (reg:SI 130 [ c ]) (nil)))) After reload: (insn 162 161 298 16 (set (reg:SI 2 cx [orig:130 c ] [130]) (sign_extend:SI (reg/v:QI 2 cx [orig:104 c ] [104]))) "/app/example.c":81:68 156 {extendqisi2} (nil)) (insn 298 162 163 16 (set (reg:SI 68 k0 [orig:130 c ] [130]) (reg:SI 2 cx [orig:130 c ] [130])) "/app/example.c":81:68 77 {*movsi_internal} (nil)) (insn 163 298 273 16 (set (reg:SI 2 cx [132]) (mem/u/c:SI (plus:SI (reg:SI 3 bx [82]) (const:SI (unspec:SI [ (symbol_ref:SI ("__libc_tsd_CTYPE_B") [flags 0x60] <var_decl 0x7fd752e61c60 __libc_tsd_CTYPE_B>) ] UNSPEC_GOTNTPOFF))) [7 S4 A8])) "/app/example.c":81:67 77 {*movsi_internal} (expr_list:REG_EQUIV (mem/u/c:SI (plus:SI (reg:SI 3 bx [82]) (const:SI (unspec:SI [ (symbol_ref:SI ("__libc_tsd_CTYPE_B") [flags 0x60] <var_decl 0x7fd752e61c60 __libc_tsd_CTYPE_B>) ] UNSPEC_GOTNTPOFF))) [7 S4 A8]) (nil))) (insn 273 163 165 16 (set (reg/f:SI 2 cx [orig:131 __libc_tsd_CTYPE_B ] [131]) (mem/f/c:SI (reg:SI 2 cx [132]) [4 __libc_tsd_CTYPE_B+0 S4 A32 AS2])) "/app/example.c":81:67 77 {*movsi_internal} (nil)) (note 165 273 166 16 NOTE_INSN_DELETED) (note 166 165 299 16 NOTE_INSN_DELETED) (insn 299 166 274 16 (set (reg:SI 0 ax [orig:130 c ] [130]) (reg:SI 68 k0 [orig:130 c ] [130])) "/app/example.c":81:43 77 {*movsi_internal} (nil)) (insn 274 299 168 16 (set (reg:CCZ 17 flags) (compare:CCZ (and:QI (mem:QI (plus:SI (plus:SI (mult:SI (reg:SI 0 ax [orig:130 c ] [130]) (const_int 2 [0x2])) (reg/f:SI 2 cx [orig:131 __libc_tsd_CTYPE_B ] [131])) (const_int 1 [0x1])) [5 *_12+1 S1 A8]) (const_int 32 [0x20])) (const_int 0 [0]))) "/app/example.c":81:43 502 {*testqi_1_maybe_si} (nil)) This is odd but not too bad really. It is definitely a register allocator issue but how bad is questionable. Register allocator will definitely do different choices if there are more register available which is what is happening in the AVX case. > And even smaller: https://gcc.godbolt.org/z/KG63ErzEr This one is fine/ok as GCC is using k0 as a spill register rather than spilling to memory. 32bit x86 has limited registers and all. There is nothing odd about this one even. kmovd ecx, k0 vs mov ecx, DWORD PTR 4[esp]