| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2026 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
|
| 9 |
/*========================== begin_copyright_notice ============================ |
| 10 |
|
| 11 |
Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 12 |
See https://llvm.org/LICENSE.txt for license information. |
| 13 |
SPDX-License-Identifier: Apache-2.0 with LLVM-exception |
| 14 |
|
| 15 |
============================= end_copyright_notice ===========================*/ |
| 16 |
|
| 17 |
# Backport of upstream commit 96605c1665a4 (PR llvm/llvm-project#194341, |
| 18 |
# fixes llvm/llvm-project#194007) onto llvmorg-17.0.6. |
| 19 |
# |
| 20 |
# The 17.0.6 matcher uses a flat m_BinOp(ShiftedX, Mask) shape, while |
| 21 |
# upstream main wraps the later m_c_BinOp(m_CombineAnd(...), Mask) variant. |
| 22 |
# The semantic change is identical: require the outer binop to be one-use |
| 23 |
# so the rewrite only fires when it can actually eliminate instructions. |
| 24 |
# |
| 25 |
# Supersedes the equivalent downstream-only patch |
| 26 |
# InstCombine-foldBinOpShiftWithShift-require-oneuse-binop.patch. |
| 27 |
|
| 28 |
From 8d4993cd491070f12480698f64dcaac49ce36974 Mon Sep 17 00:00:00 2001 |
| 29 |
From: "Grabezhnoy, Andrey" <[email protected]> |
| 30 |
Date: Mon, 27 Apr 2026 11:41:48 +0200 |
| 31 |
Subject: [PATCH] [InstCombine] Require one-use intermediate binop in |
| 32 |
foldBinOpShiftWithShift |
| 33 |
|
| 34 |
foldBinOpShiftWithShift rewrites |
| 35 |
binop(shift(X,C) op Mask, shift(Y,C)) |
| 36 |
to |
| 37 |
binop(shift(X+Y,C), Mask). |
| 38 |
|
| 39 |
Both shifts are matched with m_OneUse, but the enclosing binop that |
| 40 |
wraps (shift(X,C), Mask) is not. When that binop has additional users |
| 41 |
the rewrite cannot eliminate the originals and only adds new |
| 42 |
instructions. The reproducer in the issue shows this: a shared base |
| 43 |
with several downstream consumers gets duplicated on each one, so the |
| 44 |
fold grows the IR instead of shrinking it. |
| 45 |
|
| 46 |
Wrap the outer m_BinOp in m_OneUse so the fold only fires when the |
| 47 |
binop can actually be replaced. Mirrors the one-use discipline already |
| 48 |
applied to both shifts in the same matcher. |
| 49 |
|
| 50 |
Fixes llvm/llvm-project#194007. |
| 51 |
--- |
| 52 |
.../InstCombine/InstructionCombining.cpp | 2 +- |
| 53 |
.../test/Transforms/InstCombine/and-xor-or.ll | 5 ++-- |
| 54 |
.../InstCombine/binop-and-shifts.ll | 23 +++++++++++++++++++ |
| 55 |
3 files changed, 26 insertions(+), 4 deletions(-) |
| 56 |
|
| 57 |
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp |
| 58 |
index 767b7c7defbb..ef8b827b9d86 100644 |
| 59 |
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp |
| 60 |
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp |
| 61 |
@@ -808,7 +808,7 @@ Instruction *InstCombinerImpl::foldBinOpShiftWithShift(BinaryOperator &I) { |
| 62 |
m_OneUse(m_LogicalShift(m_Value(Y), m_Value(Shift))))) |
| 63 |
return nullptr; |
| 64 |
if (!match(I.getOperand(1 - ShOpnum), |
| 65 |
- m_BinOp(m_Value(ShiftedX), m_Value(Mask)))) |
| 66 |
+ m_OneUse(m_BinOp(m_Value(ShiftedX), m_Value(Mask))))) |
| 67 |
return nullptr; |
| 68 |
|
| 69 |
if (!match(ShiftedX, |
| 70 |
diff --git a/llvm/test/Transforms/InstCombine/and-xor-or.ll b/llvm/test/Transforms/InstCombine/and-xor-or.ll |
| 71 |
index e03a768ac36d..669264a19914 100644 |
| 72 |
--- a/llvm/test/Transforms/InstCombine/and-xor-or.ll |
| 73 |
+++ b/llvm/test/Transforms/InstCombine/and-xor-or.ll |
| 74 |
@@ -561,10 +561,9 @@ define i8 @xor_lshr_multiuse(i8 %x, i8 %y, i8 %z, i8 %shamt) { |
| 75 |
; CHECK-LABEL: define {{[^@]+}}@xor_lshr_multiuse |
| 76 |
; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]], i8 [[Z:%.*]], i8 [[SHAMT:%.*]]) { |
| 77 |
; CHECK-NEXT: [[SX:%.*]] = lshr i8 [[X]], [[SHAMT]] |
| 78 |
+; CHECK-NEXT: [[SY:%.*]] = lshr i8 [[Y]], [[SHAMT]] |
| 79 |
; CHECK-NEXT: [[A:%.*]] = xor i8 [[SX]], [[Z]] |
| 80 |
-; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[X]], [[Y]] |
| 81 |
-; CHECK-NEXT: [[TMP2:%.*]] = lshr i8 [[TMP1]], [[SHAMT]] |
| 82 |
-; CHECK-NEXT: [[R:%.*]] = xor i8 [[TMP2]], [[Z]] |
| 83 |
+; CHECK-NEXT: [[R:%.*]] = xor i8 [[A]], [[SY]] |
| 84 |
; CHECK-NEXT: [[R2:%.*]] = sdiv i8 [[A]], [[R]] |
| 85 |
; CHECK-NEXT: ret i8 [[R2]] |
| 86 |
; |
| 87 |
diff --git a/llvm/test/Transforms/InstCombine/binop-and-shifts.ll b/llvm/test/Transforms/InstCombine/binop-and-shifts.ll |
| 88 |
index f1814e3a6934..24f908c0c8d5 100644 |
| 89 |
--- a/llvm/test/Transforms/InstCombine/binop-and-shifts.ll |
| 90 |
+++ b/llvm/test/Transforms/InstCombine/binop-and-shifts.ll |
| 91 |
@@ -549,3 +549,26 @@ define i8 @shl_add_and_fail_mismatch_shift(i8 %x, i8 %y) { |
| 92 |
%bw1 = and i8 %shift1, %bw2 |
| 93 |
ret i8 %bw1 |
| 94 |
} |
| 95 |
+ |
| 96 |
+; Negative test: outer binop has multiple users |
| 97 |
+ |
| 98 |
+define i8 @shl_add_add_multiuse_binop(i8 %x, i8 %y0, i8 %y1) { |
| 99 |
+; CHECK-LABEL: @shl_add_add_multiuse_binop( |
| 100 |
+; CHECK-NEXT: [[SHIFT1:%.*]] = shl i8 [[X:%.*]], 2 |
| 101 |
+; CHECK-NEXT: [[BASE:%.*]] = add i8 [[SHIFT1]], 48 |
| 102 |
+; CHECK-NEXT: [[SHIFT_Y0:%.*]] = shl i8 [[Y0:%.*]], 2 |
| 103 |
+; CHECK-NEXT: [[SHIFT_Y1:%.*]] = shl i8 [[Y1:%.*]], 2 |
| 104 |
+; CHECK-NEXT: [[R0:%.*]] = add i8 [[BASE]], [[SHIFT_Y0]] |
| 105 |
+; CHECK-NEXT: [[R1:%.*]] = add i8 [[BASE]], [[SHIFT_Y1]] |
| 106 |
+; CHECK-NEXT: [[R:%.*]] = xor i8 [[R0]], [[R1]] |
| 107 |
+; CHECK-NEXT: ret i8 [[R]] |
| 108 |
+; |
| 109 |
+ %shift1 = shl i8 %x, 2 |
| 110 |
+ %base = add i8 %shift1, 48 |
| 111 |
+ %shift.y0 = shl i8 %y0, 2 |
| 112 |
+ %shift.y1 = shl i8 %y1, 2 |
| 113 |
+ %r0 = add i8 %base, %shift.y0 |
| 114 |
+ %r1 = add i8 %base, %shift.y1 |
| 115 |
+ %r = xor i8 %r0, %r1 |
| 116 |
+ ret i8 %r |
| 117 |
+} |
| 118 |
-- |
| 119 |
2.43.0 |
| 120 |
|