Skip to content
Snippets Groups Projects
Commit e2886967 authored by Roger Sayle's avatar Roger Sayle
Browse files

PR 106245: Split (x<<31)>>31 as -(x&1) in i386.md

This patch is the backend piece of a solution to PRs 101955 and 106245,
that adds a define_insn_and_split to the i386 backend, to perform sign
extension of a single (least significant) bit using and $1 then neg.

Previously, (x<<31)>>31 would be generated as

        sall    $31, %eax	// 3 bytes
        sarl    $31, %eax	// 3 bytes

with this patch the backend now generates:

        andl    $1, %eax	// 3 bytes
        negl    %eax		// 2 bytes

Not only is this smaller in size, but microbenchmarking confirms
that it's a performance win on both Intel and AMD; Intel sees only a
2% improvement (perhaps just a size effect), but AMD sees a 7% win.

2023-10-21  Roger Sayle  <roger@nextmovesoftware.com>
	    Uros Bizjak  <ubizjak@gmail.com>

gcc/ChangeLog
	PR middle-end/101955
	PR tree-optimization/106245
	* config/i386/i386.md (*extv<mode>_1_0): New define_insn_and_split.

gcc/testsuite/ChangeLog
	PR middle-end/101955
	PR tree-optimization/106245
	* gcc.target/i386/pr106245-2.c: New test case.
	* gcc.target/i386/pr106245-3.c: New 32-bit test case.
	* gcc.target/i386/pr106245-4.c: New 64-bit test case.
	* gcc.target/i386/pr106245-5.c: Likewise.
parent 55731b59
No related branches found
No related tags found
No related merge requests found
......@@ -3414,6 +3414,21 @@
[(set_attr "type" "imovx")
(set_attr "mode" "SI")])
 
;; Split sign-extension of single least significant bit as and x,$1;neg x
(define_insn_and_split "*extv<mode>_1_0"
[(set (match_operand:SWI48 0 "register_operand" "=r")
(sign_extract:SWI48 (match_operand:SWI48 1 "register_operand" "0")
(const_int 1)
(const_int 0)))
(clobber (reg:CC FLAGS_REG))]
""
"#"
""
[(parallel [(set (match_dup 0) (and:SWI48 (match_dup 1) (const_int 1)))
(clobber (reg:CC FLAGS_REG))])
(parallel [(set (match_dup 0) (neg:SWI48 (match_dup 0)))
(clobber (reg:CC FLAGS_REG))])])
(define_expand "extzv<mode>"
[(set (match_operand:SWI248 0 "register_operand")
(zero_extract:SWI248 (match_operand:SWI248 1 "register_operand")
......
/* { dg-do compile } */
/* { dg-options "-O2" } */
int f(int a)
{
return (a << 31) >> 31;
}
/* { dg-final { scan-assembler "andl" } } */
/* { dg-final { scan-assembler "negl" } } */
/* { dg-do compile { target ia32 } } */
/* { dg-options "-O2" } */
long long f(long long a)
{
return (a << 63) >> 63;
}
/* { dg-final { scan-assembler "andl" } } */
/* { dg-final { scan-assembler "negl" } } */
/* { dg-final { scan-assembler "cltd" } } */
/* { dg-do compile { target { ! ia32 } } } */
/* { dg-options "-O2" } */
long long f(long long a)
{
return (a << 63) >> 63;
}
/* { dg-final { scan-assembler "andl" } } */
/* { dg-final { scan-assembler "negq" } } */
/* { dg-do compile { target int128 } } */
/* { dg-options "-O2" } */
__int128 f(__int128 a)
{
return (a << 127) >> 127;
}
/* { dg-final { scan-assembler "andl" } } */
/* { dg-final { scan-assembler "negq" } } */
/* { dg-final { scan-assembler "cqto" } } */
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