[PATCH] D133362: [InstCombine] Fold x + (x | -x) to x & (x - 1)
Marc Auberer via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 6 08:34:44 PDT 2022
marcauberer created this revision.
marcauberer added reviewers: RKSimon, spatel, dblaikie, MaskRay.
Herald added subscribers: StephenFan, pengfei, hiraditya.
Herald added a project: All.
marcauberer requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Fixes #57531
This transformation may be particularly useful on x86-64, because x & (x - 1) can be performed by a single blsr instruction.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D133362
Files:
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/test/Transforms/InstCombine/add_or_sub.ll
Index: llvm/test/Transforms/InstCombine/add_or_sub.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/add_or_sub.ll
@@ -0,0 +1,26 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i32 @add_or_sub_i32_comb(i32 %x) {
+; CHECK-LABEL: @add_or_sub_i32_comb(
+; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[X:%.*]], -1
+; CHECK-NEXT: [[ADD:%.*]] = and i32 [[TMP1]], [[X]]
+; CHECK-NEXT: ret i32 [[ADD]]
+;
+ %sub = sub i32 0, %x
+ %or = or i32 %sub, %x
+ %add = add i32 %or, %x
+ ret i32 %add
+}
+
+define i8 @add_or_sub_i8_comb(i8 %x) {
+; CHECK-LABEL: @add_or_sub_i8_comb(
+; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X:%.*]], -1
+; CHECK-NEXT: [[ADD:%.*]] = and i8 [[TMP1]], [[X]]
+; CHECK-NEXT: ret i8 [[ADD]]
+;
+ %sub = sub i8 0, %x
+ %or = or i8 %sub, %x
+ %add = add i8 %or, %x
+ ret i8 %add
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1421,6 +1421,15 @@
replaceOperand(I, 1, B);
return &I;
}
+
+ // (add (or (sub 0, A) A) A) --> (and (add A, -1) A)
+ // (add (or A (sub 0, A)) A) --> (and (add A, -1) A)
+ // (add A (or (sub 0, A) A)) --> (and (add A, -1) A)
+ // (add A (or A (sub 0, A))) --> (and (add A, -1) A)
+ if (match(&I, m_c_BinOp(m_Or(m_Sub(m_ZeroInt(), m_Value(A)), m_Value(A)), m_Value(A)))) {
+ Value *AddInst = Builder.CreateAdd(A, Constant::getAllOnesValue(A->getType()));
+ return BinaryOperator::CreateAnd(AddInst, A);
+ }
// Canonicalize ((A & -A) - 1) --> ((A - 1) & ~A)
// Forms all commutable operations, and simplifies ctpop -> cttz folds.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133362.458183.patch
Type: text/x-patch
Size: 1884 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220906/c87b16fe/attachment.bin>
More information about the llvm-commits
mailing list