https://gcc.gnu.org/g:8b38049be18d5cfc89f1c2b274753722906c0bfc

commit r17-6419-g8b38049be18d5cfc89f1c2b274753722906c0bfc
Author: Kyrylo Tkachov <[email protected]>
Date:   Tue Nov 17 07:29:10 2026 -0800

    aarch64: Expand any-or-plus as IOR or ADD
    
    Since 53c3316a8c79 (PR tree-optimization/108477), expand uses
    aop<mode>3 for IOR and XOR of bitwise-disjoint operands.  AArch64 does
    not define it, so the middle-end defaults to PLUS for SImode and DImode.
    The AArch64 patterns for bitfield insert, shifted operands and ORR
    immediates are written for IOR.  PLUS also loses one sign-bit copy,
    so exact_int_to_float_conversion_p sees 25 significant bits instead of
    24 and keeps the float/fcvt pair.  For fcvt_int_float_double3.c:
    
      double foo (int x)
      { return (double) (float) ((x & -16) | (int) 0xf0000000); }
    
    before this patch:
    
      and   w0, w0, 268435440
      mov   w1, -268435456
      add   w0, w0, w1
      scvtf s0, w0
      fcvt  d0, s0
    
    after this patch:
    
      and   w0, w0, 268435440
      orr   w0, w0, -268435456
      scvtf d0, w0
    
    Define aop<mode>3 for GPI to emit ADD when operand 2 is an ADD
    immediate that is not a logical immediate, and IOR otherwise.  This
    keeps "add w0, w0, 1000" for (x & ~0xfff) | 1000 instead of needing a
    mov and an orr.
    
    This also fixes combine_bfi_1.c, combine_bfi_2.c, combine_bfxil.c and
    movk_2.c, which regressed with the PLUS default.
    
    Bootstrapped and tested on aarch64-none-linux-gnu.
    
    gcc/ChangeLog:
    
            * config/aarch64/aarch64.md (aop<mode>3): New expander.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/aarch64/aop_add_imm_1.c: New test.
    
    Signed-off-by: Kyrylo Tkachov <[email protected]>

Diff:
---
 gcc/config/aarch64/aarch64.md                    | 21 +++++++++++++++++++++
 gcc/testsuite/gcc.target/aarch64/aop_add_imm_1.c | 20 ++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/gcc/config/aarch64/aarch64.md b/gcc/config/aarch64/aarch64.md
index 9c8c24f4a709..4d9a794e2929 100644
--- a/gcc/config/aarch64/aarch64.md
+++ b/gcc/config/aarch64/aarch64.md
@@ -5340,6 +5340,27 @@
   }
 )
 
+;; Expand any-or-plus of bitwise-disjoint operands as ADD when operand 2
+;; is an ADD immediate that is not a logical immediate, otherwise as IOR.
+(define_expand "aop<mode>3"
+  [(set (match_operand:GPI 0 "register_operand")
+       (ior:GPI (match_operand:GPI 1 "register_operand")
+                (match_operand:GPI 2 "nonmemory_operand")))]
+  ""
+  {
+    if (aarch64_plus_immediate (operands[2], <MODE>mode)
+       && !aarch64_logical_immediate (operands[2], <MODE>mode))
+      emit_insn (gen_add<mode>3 (operands[0], operands[1], operands[2]));
+    else
+      {
+       if (!aarch64_logical_operand (operands[2], <MODE>mode))
+         operands[2] = force_reg (<MODE>mode, operands[2]);
+       emit_insn (gen_ior<mode>3 (operands[0], operands[1], operands[2]));
+      }
+    DONE;
+  }
+)
+
 ;; zero_extend version of above
 (define_insn "*<optab>si3_uxtw"
   [(set (match_operand:DI 0 "register_operand")
diff --git a/gcc/testsuite/gcc.target/aarch64/aop_add_imm_1.c 
b/gcc/testsuite/gcc.target/aarch64/aop_add_imm_1.c
new file mode 100644
index 000000000000..c4c46c547a74
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/aop_add_imm_1.c
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+/* Disjoint constants that are ADD but not logical immediates should
+   not need a separate move.  */
+
+int
+f (int x)
+{
+  return (x & ~0xfff) | 1000;
+}
+
+long
+h (long x)
+{
+  return (x & ~0xfffL) | 0x123;
+}
+
+/* { dg-final { scan-assembler-times {\tadd\t[wx][0-9]+, [wx][0-9]+, \d+\n} 2 
} } */
+/* { dg-final { scan-assembler-not {\tmov\t} } } */

Reply via email to