[llvm] [PowerPC] Use add_like pattern for ADDI/ADDIS add-immediate matching (PR #187326)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 24 15:57:55 PDT 2026


https://github.com/RKSimon updated https://github.com/llvm/llvm-project/pull/187326

>From afb23184dea608771ba7aa3eece44723b485d4c2 Mon Sep 17 00:00:00 2001
From: Simon Pilgrim <llvm-dev at redking.me.uk>
Date: Wed, 18 Mar 2026 17:03:23 +0000
Subject: [PATCH] [PowerPC] Add add_like pattern and use for ADDI/ADDIS
 add-immediate matching

Allow or_disjoint nodes with sext-immediates to make use of the ADD instructions instead or OR (which use zext-immediates) to potentially allow further folding

Prep work to help with regressions identified on #186461
---
 .../include/llvm/Target/TargetSelectionDAG.td |  4 +++
 llvm/lib/Target/PowerPC/PPCInstrInfo.td       |  6 ++--
 .../PowerPC/fp128-bitcast-after-operation.ll  |  2 +-
 llvm/test/CodeGen/PowerPC/signbit-shift.ll    |  2 +-
 ...lar-shift-by-byte-multiple-legalization.ll | 29 +++++++++----------
 5 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/llvm/include/llvm/Target/TargetSelectionDAG.td b/llvm/include/llvm/Target/TargetSelectionDAG.td
index d689b3c1beda9..5802098627add 100644
--- a/llvm/include/llvm/Target/TargetSelectionDAG.td
+++ b/llvm/include/llvm/Target/TargetSelectionDAG.td
@@ -1317,6 +1317,10 @@ def or_disjoint : PatFrag<(ops node:$lhs, node:$rhs),
   }];
 }
 
+def add_like : PatFrags<(ops node:$lhs, node:$rhs),
+                        [(add node:$lhs, node:$rhs),
+                         (or_disjoint node:$lhs, node:$rhs)]>;
+
 def xor_like : PatFrags<(ops node:$lhs, node:$rhs),
                         [(xor node:$lhs, node:$rhs),
                          (or_disjoint node:$lhs, node:$rhs)]>;
diff --git a/llvm/lib/Target/PowerPC/PPCInstrInfo.td b/llvm/lib/Target/PowerPC/PPCInstrInfo.td
index 3271e4d279f56..e4eb55ae8d0cc 100644
--- a/llvm/lib/Target/PowerPC/PPCInstrInfo.td
+++ b/llvm/lib/Target/PowerPC/PPCInstrInfo.td
@@ -2551,7 +2551,7 @@ def : Pat<(int_ppc_iospace_eieio),  (PseudoEIEIO)>;
 let PPC970_Unit = 1 in {  // FXU Operations.
 def ADDI   : DForm_2<14, (outs gprc:$RST), (ins gprc_nor0:$RA, s16imm:$D),
                      "addi $RST, $RA, $D", IIC_IntSimple,
-                     [(set i32:$RST, (add i32:$RA, imm32SExt16:$D))]>;
+                     [(set i32:$RST, (add_like i32:$RA, imm32SExt16:$D))]>;
 let BaseName = "addic" in {
 let Defs = [CARRY] in
 def ADDIC  : DForm_2<12, (outs gprc:$RST), (ins gprc:$RA, s16imm:$D),
@@ -2565,7 +2565,7 @@ def ADDIC_rec : DForm_2<13, (outs gprc:$RST), (ins gprc:$RA, s16imm:$D),
 }
 def ADDIS  : DForm_2<15, (outs gprc:$RST), (ins gprc_nor0:$RA, s17imm:$D),
                      "addis $RST, $RA, $D", IIC_IntSimple,
-                     [(set i32:$RST, (add i32:$RA, imm16ShiftedSExt:$D))]>;
+                     [(set i32:$RST, (add_like i32:$RA, imm16ShiftedSExt:$D))]>;
 let isCodeGenOnly = 1 in
 def LA     : DForm_2<14, (outs gprc:$RST), (ins gprc_nor0:$RA, s16imm:$D),
                      "la $RST, $D($RA)", IIC_IntGeneral,
@@ -3400,7 +3400,7 @@ def        : Pat<(not i32:$in),
                  (i32not $in)>;
 
 // ADD an arbitrary immediate.
-def : Pat<(add i32:$in, imm:$imm),
+def : Pat<(add_like i32:$in, imm:$imm),
           (ADDIS (ADDI $in, (LO16 imm:$imm)), (HA16 imm:$imm))>;
 // OR an arbitrary immediate.
 def : Pat<(or i32:$in, imm:$imm),
diff --git a/llvm/test/CodeGen/PowerPC/fp128-bitcast-after-operation.ll b/llvm/test/CodeGen/PowerPC/fp128-bitcast-after-operation.ll
index ebec8c1c4d654..4608b19bfe954 100644
--- a/llvm/test/CodeGen/PowerPC/fp128-bitcast-after-operation.ll
+++ b/llvm/test/CodeGen/PowerPC/fp128-bitcast-after-operation.ll
@@ -223,7 +223,7 @@ define i128 @test_copysign_const(ppc_fp128 %x) nounwind  {
 ; PPC32-NEXT:    li 6, 0
 ; PPC32-NEXT:    lwz 3, 24(1)
 ; PPC32-NEXT:    rlwinm 4, 3, 0, 0, 0
-; PPC32-NEXT:    oris 3, 4, 16399
+; PPC32-NEXT:    addis 3, 4, 16399
 ; PPC32-NEXT:    xoris 5, 4, 48304
 ; PPC32-NEXT:    li 4, 0
 ; PPC32-NEXT:    addi 1, 1, 32
diff --git a/llvm/test/CodeGen/PowerPC/signbit-shift.ll b/llvm/test/CodeGen/PowerPC/signbit-shift.ll
index f8838b50816e1..9af6f97b1c879 100644
--- a/llvm/test/CodeGen/PowerPC/signbit-shift.ll
+++ b/llvm/test/CodeGen/PowerPC/signbit-shift.ll
@@ -205,7 +205,7 @@ define i32 @sub_lshr_not(i32 %x) {
 ; CHECK-LABEL: sub_lshr_not:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    srwi 3, 3, 31
-; CHECK-NEXT:    ori 3, 3, 42
+; CHECK-NEXT:    addi 3, 3, 42
 ; CHECK-NEXT:    blr
   %not = xor i32 %x, -1
   %sh = lshr i32 %not, 31
diff --git a/llvm/test/CodeGen/PowerPC/wide-scalar-shift-by-byte-multiple-legalization.ll b/llvm/test/CodeGen/PowerPC/wide-scalar-shift-by-byte-multiple-legalization.ll
index 12976e838f3ca..6e66f44663295 100644
--- a/llvm/test/CodeGen/PowerPC/wide-scalar-shift-by-byte-multiple-legalization.ll
+++ b/llvm/test/CodeGen/PowerPC/wide-scalar-shift-by-byte-multiple-legalization.ll
@@ -1504,22 +1504,21 @@ define void @shl_32bytes_dwordOff(ptr %src.ptr, ptr %dwordOff.ptr, ptr %dst) nou
 ; LE-32BIT-NEXT:    stw 8, 20(1)
 ; LE-32BIT-NEXT:    stw 7, 16(1)
 ; LE-32BIT-NEXT:    lwzux 3, 4, 3
-; LE-32BIT-NEXT:    lwz 6, 12(4)
-; LE-32BIT-NEXT:    lwz 7, 8(4)
-; LE-32BIT-NEXT:    lwz 8, 20(4)
-; LE-32BIT-NEXT:    lwz 9, 16(4)
-; LE-32BIT-NEXT:    lwz 10, 28(4)
-; LE-32BIT-NEXT:    lwz 11, 24(4)
-; LE-32BIT-NEXT:    ori 4, 4, 4
-; LE-32BIT-NEXT:    lwz 4, 0(4)
+; LE-32BIT-NEXT:    lwz 6, 4(4)
+; LE-32BIT-NEXT:    lwz 7, 12(4)
+; LE-32BIT-NEXT:    lwz 8, 8(4)
+; LE-32BIT-NEXT:    lwz 9, 20(4)
+; LE-32BIT-NEXT:    lwz 10, 16(4)
+; LE-32BIT-NEXT:    lwz 11, 28(4)
+; LE-32BIT-NEXT:    lwz 4, 24(4)
 ; LE-32BIT-NEXT:    stw 3, 0(5)
-; LE-32BIT-NEXT:    stw 11, 24(5)
-; LE-32BIT-NEXT:    stw 10, 28(5)
-; LE-32BIT-NEXT:    stw 9, 16(5)
-; LE-32BIT-NEXT:    stw 8, 20(5)
-; LE-32BIT-NEXT:    stw 7, 8(5)
-; LE-32BIT-NEXT:    stw 6, 12(5)
-; LE-32BIT-NEXT:    stw 4, 4(5)
+; LE-32BIT-NEXT:    stw 4, 24(5)
+; LE-32BIT-NEXT:    stw 11, 28(5)
+; LE-32BIT-NEXT:    stw 10, 16(5)
+; LE-32BIT-NEXT:    stw 9, 20(5)
+; LE-32BIT-NEXT:    stw 8, 8(5)
+; LE-32BIT-NEXT:    stw 7, 12(5)
+; LE-32BIT-NEXT:    stw 6, 4(5)
 ; LE-32BIT-NEXT:    addi 1, 1, 80
 ; LE-32BIT-NEXT:    blr
   %src = load i256, ptr %src.ptr, align 1



More information about the llvm-commits mailing list