From 65ab0164bd954909c33843e253302c71a863b068 Mon Sep 17 00:00:00 2001 From: Alex Coplan Date: Mon, 7 Sep 2020 15:20:21 +0100 Subject: [PATCH] aarch64: Don't emit invalid zero/sign-extend syntax Given the following C function: double *f(double *p, unsigned x) { return p + x; } prior to this patch, GCC at -O2 would generate: f: add x0, x0, x1, uxtw 3 ret but this add instruction uses architecturally-invalid syntax: the width of the third operand conflicts with the width of the extension specifier. The third operand is only permitted to be an x register when the extension specifier is (u|s)xtx. This instruction, and analogous insns for adds, sub, subs, and cmp, are rejected by clang, but accepted by binutils. Assembling and disassembling such an insn with binutils gives the architecturally-valid version in the disassembly: 0: 8b214c00 add x0, x0, w1, uxtw #3 This patch fixes several patterns in the AArch64 backend to use the standard syntax as specified in the Arm ARM such that GCC's output can be assembled by assemblers other than GAS. --- gcc/ChangeLog: * config/aarch64/aarch64.md (*adds__): Ensure extended operand agrees with width of extension specifier. (*subs__): Likewise. (*adds__shift_): Likewise. (*subs__shift_): Likewise. (*add__): Likewise. (*add__shft_): Likewise. (*add_uxt_shift2): Likewise. (*sub__): Likewise. (*sub__shft_): Likewise. (*sub_uxt_shift2): Likewise. (*cmp_swp__reg): Likewise. (*cmp_swp__shft_): Likewise. gcc/testsuite/ChangeLog: * gcc.target/aarch64/adds3.c: Fix test w.r.t. new syntax. * gcc.target/aarch64/cmp.c: Likewise. * gcc.target/aarch64/subs3.c: Likewise. * gcc.target/aarch64/subsp.c: Likewise. * gcc.target/aarch64/extend-syntax.c: New test. (cherry picked from commit d4febc75e8dfab23bd3132d5747eded918f85107) Signed-off-by: Kirill A. Korinsky --- gcc/config/aarch64/aarch64.md | 24 ++-- gcc/testsuite/gcc.target/aarch64/adds3.c | 2 +- gcc/testsuite/gcc.target/aarch64/cmp.c | 3 +- .../gcc.target/aarch64/extend-syntax.c | 120 ++++++++++++++++++ gcc/testsuite/gcc.target/aarch64/subs3.c | 2 +- gcc/testsuite/gcc.target/aarch64/subsp.c | 2 +- 6 files changed, 137 insertions(+), 16 deletions(-) create mode 100644 gcc/testsuite/gcc.target/aarch64/extend-syntax.c diff --git gcc/config/aarch64/aarch64.md gcc/config/aarch64/aarch64.md index 95e24541f07..7b5e6313d55 100644 --- gcc/config/aarch64/aarch64.md +++ gcc/config/aarch64/aarch64.md @@ -2366,7 +2366,7 @@ (define_insn "*adds__" (set (match_operand:GPI 0 "register_operand" "=r") (plus:GPI (ANY_EXTEND:GPI (match_dup 1)) (match_dup 2)))] "" - "adds\\t%0, %2, %1, xt" + "adds\\t%0, %2, %w1, xt" [(set_attr "type" "alus_ext")] ) @@ -2380,7 +2380,7 @@ (define_insn "*subs__" (set (match_operand:GPI 0 "register_operand" "=r") (minus:GPI (match_dup 1) (ANY_EXTEND:GPI (match_dup 2))))] "" - "subs\\t%0, %1, %2, xt" + "subs\\t%0, %1, %w2, xt" [(set_attr "type" "alus_ext")] ) @@ -2398,7 +2398,7 @@ (define_insn "*adds__shift_" (match_dup 2)) (match_dup 3)))] "" - "adds\\t%0, %3, %1, xt %2" + "adds\\t%0, %3, %w1, xt %2" [(set_attr "type" "alus_ext")] ) @@ -2416,7 +2416,7 @@ (define_insn "*subs__shift_" (ashift:GPI (ANY_EXTEND:GPI (match_dup 2)) (match_dup 3))))] "" - "subs\\t%0, %1, %2, xt %3" + "subs\\t%0, %1, %w2, xt %3" [(set_attr "type" "alus_ext")] ) @@ -2482,7 +2482,7 @@ (define_insn "*add__" (plus:GPI (ANY_EXTEND:GPI (match_operand:ALLX 1 "register_operand" "r")) (match_operand:GPI 2 "register_operand" "r")))] "" - "add\\t%0, %2, %1, xt" + "add\\t%0, %2, %w1, xt" [(set_attr "type" "alu_ext")] ) @@ -2504,7 +2504,7 @@ (define_insn "*add__shft_" (match_operand 2 "aarch64_imm3" "Ui3")) (match_operand:GPI 3 "register_operand" "r")))] "" - "add\\t%0, %3, %1, xt %2" + "add\\t%0, %3, %w1, xt %2" [(set_attr "type" "alu_ext")] ) @@ -2701,7 +2701,7 @@ (define_insn "*add_uxt_shift2" "* operands[3] = GEN_INT (aarch64_uxt_size (INTVAL(operands[2]), INTVAL (operands[3]))); - return \"add\t%0, %4, %1, uxt%e3 %2\";" + return \"add\t%0, %4, %w1, uxt%e3 %2\";" [(set_attr "type" "alu_ext")] ) @@ -3131,7 +3131,7 @@ (define_insn "*sub__" (ANY_EXTEND:GPI (match_operand:ALLX 2 "register_operand" "r"))))] "" - "sub\\t%0, %1, %2, xt" + "sub\\t%0, %1, %w2, xt" [(set_attr "type" "alu_ext")] ) @@ -3154,7 +3154,7 @@ (define_insn "*sub__shft_" (match_operand:ALLX 2 "register_operand" "r")) (match_operand 3 "aarch64_imm3" "Ui3"))))] "" - "sub\\t%0, %1, %2, xt %3" + "sub\\t%0, %1, %w2, xt %3" [(set_attr "type" "alu_ext")] ) @@ -3405,7 +3405,7 @@ (define_insn "*sub_uxt_shift2" "* operands[3] = GEN_INT (aarch64_uxt_size (INTVAL (operands[2]), INTVAL (operands[3]))); - return \"sub\t%0, %4, %1, uxt%e3 %2\";" + return \"sub\t%0, %4, %w1, uxt%e3 %2\";" [(set_attr "type" "alu_ext")] ) @@ -3798,7 +3798,7 @@ (define_insn "*cmp_swp__reg" (match_operand:ALLX 0 "register_operand" "r")) (match_operand:GPI 1 "register_operand" "r")))] "" - "cmp\\t%1, %0, xt" + "cmp\\t%1, %w0, xt" [(set_attr "type" "alus_ext")] ) @@ -3810,7 +3810,7 @@ (define_insn "*cmp_swp__shft_" (match_operand 1 "aarch64_imm3" "Ui3")) (match_operand:GPI 2 "register_operand" "r")))] "" - "cmp\\t%2, %0, xt %1" + "cmp\\t%2, %w0, xt %1" [(set_attr "type" "alus_ext")] ) diff --git gcc/testsuite/gcc.target/aarch64/adds3.c gcc/testsuite/gcc.target/aarch64/adds3.c index c5518bdcaf2..e938c8049cf 100644 --- gcc/testsuite/gcc.target/aarch64/adds3.c +++ gcc/testsuite/gcc.target/aarch64/adds3.c @@ -58,4 +58,4 @@ int main () return 0; } -/* { dg-final { scan-assembler-times "adds\tx\[0-9\]+, x\[0-9\]+, x\[0-9\]+, sxtw" 2 } } */ +/* { dg-final { scan-assembler-times "adds\tx\[0-9\]+, x\[0-9\]+, w\[0-9\]+, sxtw" 2 } } */ diff --git gcc/testsuite/gcc.target/aarch64/cmp.c gcc/testsuite/gcc.target/aarch64/cmp.c index ee57dd283bf..a6487a4f77a 100644 --- gcc/testsuite/gcc.target/aarch64/cmp.c +++ gcc/testsuite/gcc.target/aarch64/cmp.c @@ -58,4 +58,5 @@ cmp_di_test4 (int a, s64 b, s64 c) } /* { dg-final { scan-assembler-times "cmp\tw\[0-9\]+, w\[0-9\]+" 2 } } */ -/* { dg-final { scan-assembler-times "cmp\tx\[0-9\]+, x\[0-9\]+" 4 } } */ +/* { dg-final { scan-assembler-times "cmp\tx\[0-9\]+, x\[0-9\]+" 2 } } */ +/* { dg-final { scan-assembler-times "cmp\tx\[0-9\]+, w\[0-9\]+, sxtw" 2 } } */ diff --git gcc/testsuite/gcc.target/aarch64/extend-syntax.c gcc/testsuite/gcc.target/aarch64/extend-syntax.c new file mode 100644 index 00000000000..23fa9f4ffc5 --- /dev/null +++ gcc/testsuite/gcc.target/aarch64/extend-syntax.c @@ -0,0 +1,120 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +// Hits *add_uxtdi_shift2 (*add_uxt_shift2). +/* +** add1: +** add x0, x0, w1, uxtw 3 +** ret +*/ +unsigned long long *add1(unsigned long long *p, unsigned x) +{ + return p + x; +} + +// Hits *add_zero_extendsi_di (*add__). +/* +** add2: +** add x0, x0, w1, uxtw +** ret +*/ +unsigned long long add2(unsigned long long x, unsigned y) +{ + return x + y; +} + +// Hits *add_extendsi_shft_di (*add__shft_). +/* +** add3: +** add x0, x0, w1, sxtw 3 +** ret +*/ +double *add3(double *p, int x) +{ + return p + x; +} + +// Hits *sub_zero_extendsi_di (*sub__). +/* +** sub1: +** sub x0, x0, w1, uxtw +** ret +*/ +unsigned long long sub1(unsigned long long x, unsigned n) +{ + return x - n; +} + +// Hits *sub_uxtdi_shift2 (*sub_uxt_shift2). +/* +** sub2: +** sub x0, x0, w1, uxtw 3 +** ret +*/ +double *sub2(double *x, unsigned n) +{ + return x - n; +} + +// Hits *sub_extendsi_shft_di (*sub__shft_). +/* +** sub3: +** sub x0, x0, w1, sxtw 3 +** ret +*/ +double *sub3(double *p, int n) +{ + return p - n; +} + +// Hits *adds_zero_extendsi_di (*adds__). +int adds1(unsigned long long x, unsigned y) +{ + /* { dg-final { scan-assembler-times "adds\tx\[0-9\]+, x\[0-9\]+, w\[0-9\]+, uxtw" 1 } } */ + unsigned long long l = x + y; + return !!l; +} + +// Hits *adds_extendsi_shift_di (*adds__shift_). +int adds2(long long x, int y) +{ + /* { dg-final { scan-assembler-times "adds\tx\[0-9\]+, x\[0-9\]+, w\[0-9\]+, sxtw 3" 1 } } */ + long long t = x + ((long long)y << 3); + return !!t; +} + +// Hits *subs_zero_extendsi_di (*subs__). +unsigned long long z; +int subs1(unsigned long long x, unsigned y) +{ + /* { dg-final { scan-assembler-times "subs\tx\[0-9\]+, x\[0-9\]+, w\[0-9\]+, uxtw" 1 } } */ + unsigned long long t = x - y; + z = t; + return !!t; +} + +// Hits *subs_extendsi_shift_di (*subs__shift_). +unsigned long long *w; +int subs2(unsigned long long *x, int y) +{ + /* { dg-final { scan-assembler-times "subs\tx\[0-9\]+, x\[0-9\]+, w\[0-9\]+, sxtw 3" 1 } } */ + unsigned long long *t = x - y; + w = t; + return !!t; +} + +// Hits *cmp_swp_zero_extendsi_regdi (*cmp_swp__reg). +int cmp(unsigned long long x, unsigned y) +{ + /* { dg-final { scan-assembler-times "cmp\tx\[0-9\]+, w\[0-9\]+, uxtw" 1 } } */ + return !!(x - y); +} + +// Hits *cmp_swp_extendsi_shft_di (*cmp_swp__shft_). +int cmp2(unsigned long long x, int y) +{ + /* { dg-final { scan-assembler-times "cmp\tx\[0-9\]+, w\[0-9\]+, sxtw 3" 1 } } */ + return x == ((unsigned long long)y << 3); +} + +/* { dg-final { check-function-bodies "**" "" "" } } */ diff --git gcc/testsuite/gcc.target/aarch64/subs3.c gcc/testsuite/gcc.target/aarch64/subs3.c index 59581bf1ab7..0470a3bde34 100644 --- gcc/testsuite/gcc.target/aarch64/subs3.c +++ gcc/testsuite/gcc.target/aarch64/subs3.c @@ -58,4 +58,4 @@ int main () return 0; } -/* { dg-final { scan-assembler-times "subs\tx\[0-9\]+, x\[0-9\]+, x\[0-9\]+, sxtw" 2 } } */ +/* { dg-final { scan-assembler-times "subs\tx\[0-9\]+, x\[0-9\]+, w\[0-9\]+, sxtw" 2 } } */ diff --git gcc/testsuite/gcc.target/aarch64/subsp.c gcc/testsuite/gcc.target/aarch64/subsp.c index 6ef6b2c90ae..341b83dca86 100644 --- gcc/testsuite/gcc.target/aarch64/subsp.c +++ gcc/testsuite/gcc.target/aarch64/subsp.c @@ -16,4 +16,4 @@ f2 (int *x, int y) } /* { dg-final { scan-assembler "sub\tsp, sp, x\[0-9\]*\n" } } */ -/* { dg-final { scan-assembler "sub\tsp, sp, x\[0-9\]*, sxtw 4\n" } } */ +/* { dg-final { scan-assembler "sub\tsp, sp, w\[0-9\]*, sxtw 4\n" } } */ -- 2.42.1