[PATCH] D114729: [InstCombine] try to fold 'or' into 'mul' operand
Sanjay Patel via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 29 12:17:36 PST 2021
spatel updated this revision to Diff 390437.
spatel marked an inline comment as done.
spatel added a comment.
Patch updated:
Removed constant restriction on the pattern - any value can be folded into an add+mul pair (and that was already shown correct in the Alive2 link).
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D114729/new/
https://reviews.llvm.org/D114729
Files:
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
llvm/test/Transforms/InstCombine/or.ll
Index: llvm/test/Transforms/InstCombine/or.ll
===================================================================
--- llvm/test/Transforms/InstCombine/or.ll
+++ llvm/test/Transforms/InstCombine/or.ll
@@ -1446,8 +1446,8 @@
; CHECK-LABEL: @mul_no_common_bits(
; CHECK-NEXT: [[X:%.*]] = and i32 [[P1:%.*]], 7
; CHECK-NEXT: [[Y:%.*]] = shl i32 [[P2:%.*]], 3
-; CHECK-NEXT: [[M:%.*]] = mul i32 [[X]], [[Y]]
-; CHECK-NEXT: [[R:%.*]] = or i32 [[M]], [[X]]
+; CHECK-NEXT: [[TMP1:%.*]] = or i32 [[Y]], 1
+; CHECK-NEXT: [[R:%.*]] = mul i32 [[X]], [[TMP1]]
; CHECK-NEXT: ret i32 [[R]]
;
%x = and i32 %p1, 7
@@ -1460,8 +1460,7 @@
define i32 @mul_no_common_bits_const_op(i32 %p) {
; CHECK-LABEL: @mul_no_common_bits_const_op(
; CHECK-NEXT: [[X:%.*]] = and i32 [[P:%.*]], 7
-; CHECK-NEXT: [[M:%.*]] = mul nuw nsw i32 [[X]], 24
-; CHECK-NEXT: [[R:%.*]] = or i32 [[M]], [[X]]
+; CHECK-NEXT: [[R:%.*]] = mul nuw nsw i32 [[X]], 25
; CHECK-NEXT: ret i32 [[R]]
;
%x = and i32 %p, 7
@@ -1473,8 +1472,7 @@
define <2 x i12> @mul_no_common_bits_commute(<2 x i12> %p) {
; CHECK-LABEL: @mul_no_common_bits_commute(
; CHECK-NEXT: [[X:%.*]] = and <2 x i12> [[P:%.*]], <i12 1, i12 1>
-; CHECK-NEXT: [[M:%.*]] = mul nuw nsw <2 x i12> [[X]], <i12 14, i12 16>
-; CHECK-NEXT: [[R:%.*]] = or <2 x i12> [[X]], [[M]]
+; CHECK-NEXT: [[R:%.*]] = mul nuw nsw <2 x i12> [[X]], <i12 15, i12 17>
; CHECK-NEXT: ret <2 x i12> [[R]]
;
%x = and <2 x i12> %p, <i12 1, i12 1>
@@ -1483,6 +1481,8 @@
ret <2 x i12> %r
}
+; negative test - probably not good to create an extra mul
+
define i32 @mul_no_common_bits_uses(i32 %p) {
; CHECK-LABEL: @mul_no_common_bits_uses(
; CHECK-NEXT: [[X:%.*]] = and i32 [[P:%.*]], 7
@@ -1498,6 +1498,8 @@
ret i32 %r
}
+; negative test - %x and %m may have set 3rd bit
+
define i32 @mul_common_bits(i32 %p) {
; CHECK-LABEL: @mul_common_bits(
; CHECK-NEXT: [[X:%.*]] = and i32 [[P:%.*]], 7
Index: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2639,6 +2639,15 @@
return BinaryOperator::CreateXor(Or, ConstantInt::get(Ty, *CV));
}
+ // If the operands have no common bits set:
+ // or (mul X, Y), X --> add (mul X, Y), X --> mul X, (Y + 1)
+ if (match(&I,
+ m_c_Or(m_OneUse(m_Mul(m_Value(X), m_Value(Y))), m_Deferred(X))) &&
+ haveNoCommonBitsSet(Op0, Op1, DL)) {
+ Value *IncrementY = Builder.CreateAdd(Y, ConstantInt::get(Ty, 1));
+ return BinaryOperator::CreateMul(X, IncrementY);
+ }
+
// (A & C) | (B & D)
Value *A, *B, *C, *D;
if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114729.390437.patch
Type: text/x-patch
Size: 2806 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211129/4e3f10f7/attachment.bin>
More information about the llvm-commits
mailing list