[llvm] r373230 - [InstCombine] fold negate disguised as select+mul
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 30 10:02:27 PDT 2019
Author: spatel
Date: Mon Sep 30 10:02:26 2019
New Revision: 373230
URL: http://llvm.org/viewvc/llvm-project?rev=373230&view=rev
Log:
[InstCombine] fold negate disguised as select+mul
Name: negate if true
%sel = select i1 %cond, i32 -1, i32 1
%r = mul i32 %sel, %x
=>
%m = sub i32 0, %x
%r = select i1 %cond, i32 %m, i32 %x
Name: negate if false
%sel = select i1 %cond, i32 1, i32 -1
%r = mul i32 %sel, %x
=>
%m = sub i32 0, %x
%r = select i1 %cond, i32 %x, i32 %m
https://rise4fun.com/Alive/Nlh
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/trunk/test/Transforms/InstCombine/mul.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp?rev=373230&r1=373229&r2=373230&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Mon Sep 30 10:02:26 2019
@@ -213,6 +213,25 @@ Instruction *InstCombiner::visitMul(Bina
if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I))
return FoldedMul;
+ // TODO: This is a specific form of a much more general pattern.
+ // We could detect a select with any binop identity constant, or we
+ // could use SimplifyBinOp to see if either arm of the select reduces.
+ // But that needs to be done carefully and/or while removing potential
+ // reverse canonicalizations as in InstCombiner::foldSelectIntoOp().
+ // mul (select Cond, 1, -1), Op1 --> select Cond, Op1, -Op1
+ // mul (select Cond, -1, 1), Op1 --> select Cond, -Op1, Op1
+ // mul Op0, (select Cond, 1, -1) --> select Cond, Op0, -Op0
+ // mul Op0, (select Cond, -1, 1) --> select Cond, -Op0, Op0
+ Value *Cond;
+ if (match(Op0, m_OneUse(m_Select(m_Value(Cond), m_One(), m_AllOnes()))))
+ return SelectInst::Create(Cond, Op1, Builder.CreateNeg(Op1));
+ if (match(Op0, m_OneUse(m_Select(m_Value(Cond), m_AllOnes(), m_One()))))
+ return SelectInst::Create(Cond, Builder.CreateNeg(Op1), Op1);
+ if (match(Op1, m_OneUse(m_Select(m_Value(Cond), m_One(), m_AllOnes()))))
+ return SelectInst::Create(Cond, Op0, Builder.CreateNeg(Op0));
+ if (match(Op1, m_OneUse(m_Select(m_Value(Cond), m_AllOnes(), m_One()))))
+ return SelectInst::Create(Cond, Builder.CreateNeg(Op0), Op0);
+
// Simplify mul instructions with a constant RHS.
if (isa<Constant>(Op1)) {
// Canonicalize (X+C1)*CI -> X*CI+C1*CI.
Modified: llvm/trunk/test/Transforms/InstCombine/mul.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/mul.ll?rev=373230&r1=373229&r2=373230&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/mul.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/mul.ll Mon Sep 30 10:02:26 2019
@@ -520,8 +520,8 @@ define i64 @test_mul_canonicalize_neg_is
define i32 @negate_if_true(i32 %x, i1 %cond) {
; CHECK-LABEL: @negate_if_true(
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], i32 -1, i32 1
-; CHECK-NEXT: [[R:%.*]] = mul i32 [[SEL]], [[X:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = sub i32 0, [[X:%.*]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[COND:%.*]], i32 [[TMP1]], i32 [[X]]
; CHECK-NEXT: ret i32 [[R]]
;
%sel = select i1 %cond, i32 -1, i32 1
@@ -531,8 +531,8 @@ define i32 @negate_if_true(i32 %x, i1 %c
define i32 @negate_if_false(i32 %x, i1 %cond) {
; CHECK-LABEL: @negate_if_false(
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], i32 1, i32 -1
-; CHECK-NEXT: [[R:%.*]] = mul i32 [[SEL]], [[X:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = sub i32 0, [[X:%.*]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[COND:%.*]], i32 [[X]], i32 [[TMP1]]
; CHECK-NEXT: ret i32 [[R]]
;
%sel = select i1 %cond, i32 1, i32 -1
@@ -543,8 +543,8 @@ define i32 @negate_if_false(i32 %x, i1 %
define <2 x i8> @negate_if_true_commute(<2 x i8> %px, i1 %cond) {
; CHECK-LABEL: @negate_if_true_commute(
; CHECK-NEXT: [[X:%.*]] = sdiv <2 x i8> <i8 42, i8 42>, [[PX:%.*]]
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], <2 x i8> <i8 -1, i8 -1>, <2 x i8> <i8 1, i8 1>
-; CHECK-NEXT: [[R:%.*]] = mul <2 x i8> [[X]], [[SEL]]
+; CHECK-NEXT: [[TMP1:%.*]] = sub nsw <2 x i8> zeroinitializer, [[X]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[COND:%.*]], <2 x i8> [[TMP1]], <2 x i8> [[X]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%x = sdiv <2 x i8> <i8 42, i8 42>, %px ; thwart complexity-based canonicalization
@@ -556,8 +556,8 @@ define <2 x i8> @negate_if_true_commute(
define <2 x i8> @negate_if_false_commute(<2 x i8> %px, <2 x i1> %cond) {
; CHECK-LABEL: @negate_if_false_commute(
; CHECK-NEXT: [[X:%.*]] = sdiv <2 x i8> <i8 42, i8 5>, [[PX:%.*]]
-; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[COND:%.*]], <2 x i8> <i8 1, i8 undef>, <2 x i8> <i8 -1, i8 -1>
-; CHECK-NEXT: [[R:%.*]] = mul <2 x i8> [[X]], [[SEL]]
+; CHECK-NEXT: [[TMP1:%.*]] = sub <2 x i8> zeroinitializer, [[X]]
+; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[COND:%.*]], <2 x i8> [[X]], <2 x i8> [[TMP1]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%x = sdiv <2 x i8> <i8 42, i8 5>, %px ; thwart complexity-based canonicalization
@@ -566,6 +566,8 @@ define <2 x i8> @negate_if_false_commute
ret <2 x i8> %r
}
+; Negative test
+
define i32 @negate_if_true_extra_use(i32 %x, i1 %cond) {
; CHECK-LABEL: @negate_if_true_extra_use(
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], i32 -1, i32 1
@@ -579,6 +581,8 @@ define i32 @negate_if_true_extra_use(i32
ret i32 %r
}
+; Negative test
+
define <2 x i8> @negate_if_true_wrong_constant(<2 x i8> %px, i1 %cond) {
; CHECK-LABEL: @negate_if_true_wrong_constant(
; CHECK-NEXT: [[X:%.*]] = sdiv <2 x i8> <i8 42, i8 42>, [[PX:%.*]]
More information about the llvm-commits
mailing list