Avoid andb %dil when optimizing for size.
authorRoger Sayle <roger@nextmovesoftware.com>
Tue, 10 May 2022 08:44:34 +0000 (09:44 +0100)
committerRoger Sayle <roger@nextmovesoftware.com>
Tue, 10 May 2022 08:44:34 +0000 (09:44 +0100)
commit5b7a9751f55bcdb7d9a69345e02f62aaa7035d6b
treed09f6d7423de293ca33821a440e5b0663f6b1406
parentdd3c7873a61019e993ee8b79e3695722b13cf945
Avoid andb %dil when optimizing for size.

The simple test case below has the unfortunate property that on x86_64,
it is larger when compiled with -Os than when compiled with -O2.

int foo(char x)
{
  return (x & 123) != 0;
}

The issue is x86's complex instruction encoding, where andb $XX,%dil
requires more bytes than andl $XX,%edi.  This patch adds logic to
i386.md's *testqi_1_maybe_si and *andqi_2_maybe_si define_insn patterns
to prefer the shorter SImode alternative when optimizing for size.

2022-05-10  Uroš Bizjak  <ubizjak@gmail.com>
    Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
* config/i386/i386.md (*testqi_1_maybe_si): Prefer shorter SImode
alternative when optimizing for size and the immediate operand is
const_0_to_127_operand.
(*andqi_2_maybe_si): Likewise.
* config/i386/predicates.md (const_0_to_127_operand): New predicate.

gcc/testsuite/ChangeLog
* gcc.target/i386/and-1.c: New test case.
gcc/config/i386/i386.md
gcc/config/i386/predicates.md
gcc/testsuite/gcc.target/i386/and-1.c [new file with mode: 0644]