[PATCH] D139930: [InstCombine] Combine ZExt (B - A) + ZExt(A) to ZExt(B)

luxufan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 13 05:03:53 PST 2022


StephenFan created this revision.
StephenFan added reviewers: nikic, spatel.
Herald added a subscriber: hiraditya.
Herald added a project: All.
StephenFan requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Combine ZExt (B - A) + ZExt(A) to ZExt(B)
https://alive2.llvm.org/ce/z/ESUwPi


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139930

Files:
  llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
  llvm/test/Transforms/InstCombine/add.ll


Index: llvm/test/Transforms/InstCombine/add.ll
===================================================================
--- llvm/test/Transforms/InstCombine/add.ll
+++ llvm/test/Transforms/InstCombine/add.ll
@@ -2335,6 +2335,41 @@
   ret i8 %a
 }
 
+define i16 @add_sub_zext_1(i8 %x, i8 %y) {
+; CHECK-LABEL: @add_sub_zext_1(
+; CHECK-NEXT:    [[TMP1:%.*]] = zext i8 [[Y:%.*]] to i16
+; CHECK-NEXT:    ret i16 [[TMP1]]
+;
+  %1 = sub nuw i8 %y, %x
+  %2 = zext i8 %1 to i16
+  %3 = zext i8 %x to i16
+  %4 = add nuw nsw i16 %2, %3
+  ret i16 %4
+}
+
+define i16 @add_sub_zext_2(i8 %x, i8 %y) {
+; CHECK-LABEL: @add_sub_zext_2(
+; CHECK-NEXT:    [[TMP1:%.*]] = zext i8 [[Y:%.*]] to i16
+; CHECK-NEXT:    ret i16 [[TMP1]]
+;
+  %1 = sub nuw i8 %y, %x
+  %2 = zext i8 %1 to i16
+  %3 = zext i8 %x to i16
+  %4 = add nuw nsw i16 %3, %2
+  ret i16 %4
+}
+
+define i16 @add_sub_zext_constant(i8 %x) {
+; CHECK-LABEL: @add_sub_zext_constant(
+; CHECK-NEXT:    ret i16 254
+;
+  %1 = sub i8 254, %x
+  %2 = zext i8 %1 to i16
+  %3 = zext i8 %x to i16
+  %4 = add i16 %2, %3
+  ret i16 %4
+}
+
 define <vscale x 1 x i32> @add_to_or_scalable(<vscale x 1 x i32> %in) {
 ; CHECK-LABEL: @add_to_or_scalable(
 ; CHECK-NEXT:    [[SHL:%.*]] = shl <vscale x 1 x i32> [[IN:%.*]], shufflevector (<vscale x 1 x i32> insertelement (<vscale x 1 x i32> poison, i32 1, i32 0), <vscale x 1 x i32> poison, <vscale x 1 x i32> zeroinitializer)
Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1415,6 +1415,15 @@
     return BinaryOperator::CreateAnd(A, NewMask);
   }
 
+  // ZExt (B - A) + ZExt(A) --> ZExt(B)
+  if ((match(RHS, m_ZExt(m_Value(A))) &&
+       match(LHS, m_ZExt(m_Sub(m_Value(B), m_Specific(A))))) ||
+      (match(LHS, m_ZExt(m_Value(A))) &&
+       match(RHS, m_ZExt(m_Sub(m_Value(B), m_Specific(A)))))) {
+    Value *Res = Builder.CreateZExt(B, LHS->getType());
+    return replaceInstUsesWith(I, Res);
+  }
+
   // A+B --> A|B iff A and B have no bits set in common.
   if (haveNoCommonBitsSet(LHS, RHS, DL, &AC, &I, &DT))
     return BinaryOperator::CreateOr(LHS, RHS);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139930.482435.patch
Type: text/x-patch
Size: 2259 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221213/57046522/attachment.bin>


More information about the llvm-commits mailing list