[PATCH] D61307: [InstCombine] Add new combine to sub folding
Chris Dawson via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 30 06:31:11 PDT 2019
cdawson created this revision.
cdawson added reviewers: spatel, craig.topper, majnemer.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.
cdawson retitled this revision from "[InstCombine] Add new combine to sub folding." to "[InstCombine] Add new combine to sub folding".
(X | Y) - Y --> (X | Y) ^ Y
(Y | X) - Y --> (X | Y) ^ Y
I verified the correctness using Alive:
https://rise4fun.com/Alive/czes
This transform enables these further transforms that already exist in instcombine:
(X | Y) ^ Y --> X & ~Y
(Y | X) ^ Y --> X & ~Y
As a result, the full expected transform is:
(X | Y) - Y --> X & ~Y
(Y | X) - Y --> X & ~Y
I've added tests for cases where Y is constant and where Y is non-constant (with operands in either order).
In the constant case the optimisation is a clear win as we go from 2 instructions to 1 as we can pre-compute ~Y.
I checked that the combine still appears to be profitable when Y is non-constant, by compiling for x86_64 -mpcu=btver2 where I observed that we go from generating
movl %ecx, %eax
orl %edx, %eax
subl %edx, %eax
to
andnl %ecx, %edx, %eax
https://reviews.llvm.org/D61307
Files:
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/test/Transforms/InstCombine/sub.ll
Index: llvm/test/Transforms/InstCombine/sub.ll
===================================================================
--- llvm/test/Transforms/InstCombine/sub.ll
+++ llvm/test/Transforms/InstCombine/sub.ll
@@ -1267,6 +1267,38 @@
ret <2 x i32> %res
}
+define i32 @test70(i32 %A) {
+; CHECK-LABEL: @test70(
+; CHECK-NEXT: [[A:%.*]] = and i32 [[TMP1:%.*]], -124
+; CHECK-NEXT: ret i32 [[A]]
+;
+ %B = or i32 %A, 123
+ %C = sub i32 %B, 123
+ ret i32 %C
+}
+
+define i32 @test71(i32 %A, i32 %B) {
+; CHECK-LABEL: @test71(
+; CHECK-NEXT: [[A:%.*]] = xor i32 [[TMP1:%.*]], -1
+; CHECK-NEXT: [[B:%.*]] = and i32 [[A]], [[TMP2:%.*]]
+; CHECK-NEXT: ret i32 [[B]]
+;
+ %C = or i32 %A, %B
+ %D = sub i32 %C, %B
+ ret i32 %D
+}
+
+define i32 @test72(i32 %A, i32 %B) {
+; CHECK-LABEL: @test72(
+; CHECK-NEXT: [[A:%.*]] = xor i32 [[TMP1:%.*]], -1
+; CHECK-NEXT: [[B:%.*]] = and i32 [[A]], [[TMP2:%.*]]
+; CHECK-NEXT: ret i32 [[B]]
+;
+ %C = or i32 %B, %A
+ %D = sub i32 %C, %B
+ ret i32 %D
+}
+
define i32 @nsw_inference1(i32 %x, i32 %y) {
; CHECK-LABEL: @nsw_inference1(
; CHECK-NEXT: [[X2:%.*]] = or i32 [[X:%.*]], 1024
Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1528,8 +1528,14 @@
if (Value *V = SimplifyUsingDistributiveLaws(I))
return replaceInstUsesWith(I, V);
- // If this is a 'B = x-(-A)', change to B = x+A.
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
+ Value *X;
+ // (X | Y) - Y --> (X | Y) ^ Y
+ // (Y | X) - Y --> (Y | X) ^ Y
+ if (match(Op0, m_OneUse(m_c_Or(m_Value(X), m_Specific(Op1)))))
+ return BinaryOperator::CreateXor(Builder.CreateOr(X, Op1), Op1);
+
+ // If this is a 'B = x-(-A)', change to B = x+A.
if (Value *V = dyn_castNegVal(Op1)) {
BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V);
@@ -1554,7 +1560,7 @@
return BinaryOperator::CreateNot(Op1);
// (~X) - (~Y) --> Y - X
- Value *X, *Y;
+ Value *Y;
if (match(Op0, m_Not(m_Value(X))) && match(Op1, m_Not(m_Value(Y))))
return BinaryOperator::CreateSub(Y, X);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61307.197307.patch
Type: text/x-patch
Size: 2215 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190430/4ae5de9e/attachment.bin>
More information about the llvm-commits
mailing list