Skip to content

Commit

Permalink
[InstCombine] Add simplification of two logical and/ors
Browse files Browse the repository at this point in the history
This is a patch that adds folding of two logical and/ors that share one variable:

a && (a && b) -> a && b
a && (a & b)  -> a && b
...

This is towards removing the poison-unsafe select optimization (D93065 has more context).

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D96945
  • Loading branch information
aqjune committed Mar 7, 2021
1 parent fdbd5d3 commit 07c3b97
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 55 deletions.
13 changes: 13 additions & 0 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ static Value *SimplifyCastInst(unsigned, Value *, Type *,
const SimplifyQuery &, unsigned);
static Value *SimplifyGEPInst(Type *, ArrayRef<Value *>, const SimplifyQuery &,
unsigned);
static Value *SimplifySelectInst(Value *, Value *, Value *,
const SimplifyQuery &, unsigned);

static Value *foldSelectWithBinaryOp(Value *Cond, Value *TrueVal,
Value *FalseVal) {
Expand Down Expand Up @@ -3992,6 +3994,17 @@ static Value *SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
}
}

// Same for Selects.
if (auto *SI = dyn_cast<SelectInst>(I)) {
if (MaxRecurse) {
SmallVector<Value *, 3> NewOps(3);
transform(SI->operands(), NewOps.begin(),
[&](Value *V) { return V == Op ? RepOp : V; });
return PreventSelfSimplify(SimplifySelectInst(
NewOps[0], NewOps[1], NewOps[2], Q, MaxRecurse - 1));
}
}

// TODO: We could hand off more cases to instsimplify here.

// If all operands are constant after substituting Op for RepOp then we can
Expand Down
36 changes: 26 additions & 10 deletions llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2600,34 +2600,50 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
return BinaryOperator::CreateAnd(CondVal, TrueVal);
}

auto *One = ConstantInt::getTrue(SelType);
auto *Zero = ConstantInt::getFalse(SelType);

// select a, false, b -> select !a, b, false
if (match(TrueVal, m_Zero())) {
Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
return SelectInst::Create(NotCond, FalseVal,
ConstantInt::getFalse(SelType));
return SelectInst::Create(NotCond, FalseVal, Zero);
}
// select a, b, true -> select !a, true, b
if (match(FalseVal, m_One())) {
Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
return SelectInst::Create(NotCond, ConstantInt::getTrue(SelType),
TrueVal);
return SelectInst::Create(NotCond, One, TrueVal);
}

// select a, a, b -> select a, true, b
if (CondVal == TrueVal)
return replaceOperand(SI, 1, ConstantInt::getTrue(SelType));
return replaceOperand(SI, 1, One);
// select a, b, a -> select a, b, false
if (CondVal == FalseVal)
return replaceOperand(SI, 2, ConstantInt::getFalse(SelType));
return replaceOperand(SI, 2, Zero);

// select a, !a, b -> select !a, b, false
if (match(TrueVal, m_Not(m_Specific(CondVal))))
return SelectInst::Create(TrueVal, FalseVal,
ConstantInt::getFalse(SelType));
return SelectInst::Create(TrueVal, FalseVal, Zero);
// select a, b, !a -> select !a, true, b
if (match(FalseVal, m_Not(m_Specific(CondVal))))
return SelectInst::Create(FalseVal, ConstantInt::getTrue(SelType),
TrueVal);
return SelectInst::Create(FalseVal, One, TrueVal);

Value *A, *B;
// select (select a, true, b), true, b -> select a, true, b
if (match(CondVal, m_Select(m_Value(A), m_One(), m_Value(B))) &&
match(TrueVal, m_One()) && match(FalseVal, m_Specific(B)))
return replaceOperand(SI, 0, A);
// select (select a, b, false), b, false -> select a, b, false
if (match(CondVal, m_Select(m_Value(A), m_Value(B), m_Zero())) &&
match(TrueVal, m_Specific(B)) && match(FalseVal, m_Zero()))
return replaceOperand(SI, 0, A);

if (Value *S = SimplifyWithOpReplaced(TrueVal, CondVal, One, SQ,
/* AllowRefinement */ true))
return replaceOperand(SI, 1, S);
if (Value *S = SimplifyWithOpReplaced(FalseVal, CondVal, Zero, SQ,
/* AllowRefinement */ true))
return replaceOperand(SI, 2, S);
}

// Selecting between two integer or vector splat integer constants?
Expand Down
50 changes: 16 additions & 34 deletions llvm/test/Transforms/InstCombine/select-safe-bool-transforms.ll
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ define i1 @land_land_left1(i1 %A, i1 %B) {
}
define i1 @land_land_left2(i1 %A, i1 %B) {
; CHECK-LABEL: @land_land_left2(
; CHECK-NEXT: [[C:%.*]] = select i1 [[B:%.*]], i1 [[A:%.*]], i1 false
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C]], i1 [[A]], i1 false
; CHECK-NEXT: [[RES:%.*]] = select i1 [[B:%.*]], i1 [[A:%.*]], i1 false
; CHECK-NEXT: ret i1 [[RES]]
;
%c = select i1 %B, i1 %A, i1 false
Expand Down Expand Up @@ -181,8 +180,7 @@ define i1 @lor_lor_left1(i1 %A, i1 %B) {
}
define i1 @lor_lor_left2(i1 %A, i1 %B) {
; CHECK-LABEL: @lor_lor_left2(
; CHECK-NEXT: [[C:%.*]] = select i1 [[B:%.*]], i1 true, i1 [[A:%.*]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C]], i1 true, i1 [[A]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[B:%.*]], i1 true, i1 [[A:%.*]]
; CHECK-NEXT: ret i1 [[RES]]
;
%c = select i1 %B, i1 true, i1 %A
Expand Down Expand Up @@ -262,8 +260,8 @@ define i1 @land_land_right1(i1 %A, i1 %B) {
}
define i1 @land_land_right2(i1 %A, i1 %B) {
; CHECK-LABEL: @land_land_right2(
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[A:%.*]], i1 [[B:%.*]], i1 false
; CHECK-NEXT: ret i1 [[TMP1]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A:%.*]], i1 [[B:%.*]], i1 false
; CHECK-NEXT: ret i1 [[RES]]
;
%c = select i1 %B, i1 %A, i1 false
%res = select i1 %A, i1 %c, i1 false
Expand Down Expand Up @@ -301,9 +299,7 @@ define i1 @land_lor_right1(i1 %A, i1 %B) {
}
define i1 @land_lor_right2(i1 %A, i1 %B) {
; CHECK-LABEL: @land_lor_right2(
; CHECK-NEXT: [[C:%.*]] = select i1 [[B:%.*]], i1 [[A:%.*]], i1 false
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A]], i1 true, i1 [[C]]
; CHECK-NEXT: ret i1 [[RES]]
; CHECK-NEXT: ret i1 [[A:%.*]]
;
%c = select i1 %B, i1 %A, i1 false
%res = select i1 %A, i1 true, i1 %c
Expand Down Expand Up @@ -333,8 +329,7 @@ define i1 @land_bor_right2(i1 %A, i1 %B) {
; A land (A band B)
define i1 @band_land_right1(i1 %A, i1 %B) {
; CHECK-LABEL: @band_land_right1(
; CHECK-NEXT: [[C:%.*]] = and i1 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A]], i1 [[C]], i1 false
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A:%.*]], i1 [[B:%.*]], i1 false
; CHECK-NEXT: ret i1 [[RES]]
;
%c = and i1 %A, %B
Expand All @@ -343,8 +338,7 @@ define i1 @band_land_right1(i1 %A, i1 %B) {
}
define i1 @band_land_right2(i1 %A, i1 %B) {
; CHECK-LABEL: @band_land_right2(
; CHECK-NEXT: [[C:%.*]] = and i1 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A]], i1 [[C]], i1 false
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A:%.*]], i1 [[B:%.*]], i1 false
; CHECK-NEXT: ret i1 [[RES]]
;
%c = and i1 %B, %A
Expand All @@ -355,19 +349,15 @@ define i1 @band_land_right2(i1 %A, i1 %B) {
; A lor (A band B)
define i1 @band_lor_right1(i1 %A, i1 %B) {
; CHECK-LABEL: @band_lor_right1(
; CHECK-NEXT: [[C:%.*]] = and i1 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A]], i1 true, i1 [[C]]
; CHECK-NEXT: ret i1 [[RES]]
; CHECK-NEXT: ret i1 [[A:%.*]]
;
%c = and i1 %A, %B
%res = select i1 %A, i1 true, i1 %c
ret i1 %res
}
define i1 @band_lor_right2(i1 %A, i1 %B) {
; CHECK-LABEL: @band_lor_right2(
; CHECK-NEXT: [[C:%.*]] = and i1 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A]], i1 true, i1 [[C]]
; CHECK-NEXT: ret i1 [[RES]]
; CHECK-NEXT: ret i1 [[A:%.*]]
;
%c = and i1 %B, %A
%res = select i1 %A, i1 true, i1 %c
Expand All @@ -385,9 +375,7 @@ define i1 @lor_land_right1(i1 %A, i1 %B) {
}
define i1 @lor_land_right2(i1 %A, i1 %B) {
; CHECK-LABEL: @lor_land_right2(
; CHECK-NEXT: [[C:%.*]] = select i1 [[B:%.*]], i1 true, i1 [[A:%.*]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A]], i1 [[C]], i1 false
; CHECK-NEXT: ret i1 [[RES]]
; CHECK-NEXT: ret i1 [[A:%.*]]
;
%c = select i1 %B, i1 true, i1 %A
%res = select i1 %A, i1 %c, i1 false
Expand Down Expand Up @@ -426,8 +414,8 @@ define i1 @lor_lor_right1(i1 %A, i1 %B) {
}
define i1 @lor_lor_right2(i1 %A, i1 %B) {
; CHECK-LABEL: @lor_lor_right2(
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[A:%.*]], i1 true, i1 [[B:%.*]]
; CHECK-NEXT: ret i1 [[TMP1]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A:%.*]], i1 true, i1 [[B:%.*]]
; CHECK-NEXT: ret i1 [[RES]]
;
%c = select i1 %B, i1 true, i1 %A
%res = select i1 %A, i1 true, i1 %c
Expand Down Expand Up @@ -457,19 +445,15 @@ define i1 @lor_bor_right2(i1 %A, i1 %B) {
; A land (A bor B)
define i1 @bor_land_right1(i1 %A, i1 %B) {
; CHECK-LABEL: @bor_land_right1(
; CHECK-NEXT: [[C:%.*]] = or i1 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A]], i1 [[C]], i1 false
; CHECK-NEXT: ret i1 [[RES]]
; CHECK-NEXT: ret i1 [[A:%.*]]
;
%c = or i1 %A, %B
%res = select i1 %A, i1 %c, i1 false
ret i1 %res
}
define i1 @bor_land_right2(i1 %A, i1 %B) {
; CHECK-LABEL: @bor_land_right2(
; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A]], i1 [[C]], i1 false
; CHECK-NEXT: ret i1 [[RES]]
; CHECK-NEXT: ret i1 [[A:%.*]]
;
%c = or i1 %B, %A
%res = select i1 %A, i1 %c, i1 false
Expand All @@ -479,8 +463,7 @@ define i1 @bor_land_right2(i1 %A, i1 %B) {
; A lor (A bor B)
define i1 @bor_lor_right1(i1 %A, i1 %B) {
; CHECK-LABEL: @bor_lor_right1(
; CHECK-NEXT: [[C:%.*]] = or i1 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A]], i1 true, i1 [[C]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A:%.*]], i1 true, i1 [[B:%.*]]
; CHECK-NEXT: ret i1 [[RES]]
;
%c = or i1 %A, %B
Expand All @@ -489,8 +472,7 @@ define i1 @bor_lor_right1(i1 %A, i1 %B) {
}
define i1 @bor_lor_right2(i1 %A, i1 %B) {
; CHECK-LABEL: @bor_lor_right2(
; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A]], i1 true, i1 [[C]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A:%.*]], i1 true, i1 [[B:%.*]]
; CHECK-NEXT: ret i1 [[RES]]
;
%c = or i1 %B, %A
Expand Down
21 changes: 10 additions & 11 deletions llvm/test/Transforms/SimplifyCFG/merge-cond-stores.ll
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,17 @@ declare void @f()
define i32 @test_diamond_simple(i32* %p, i32* %q, i32 %a, i32 %b) {
; CHECK-LABEL: @test_diamond_simple(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[X1:%.*]] = icmp eq i32 [[A:%.*]], 0
; CHECK-NEXT: [[Z2:%.*]] = select i1 [[X1]], i32 [[B:%.*]], i32 0
; CHECK-NEXT: [[X2:%.*]] = icmp eq i32 [[B]], 0
; CHECK-NEXT: [[TMP0:%.*]] = or i32 [[A]], [[B]]
; CHECK-NEXT: [[DOTNOT:%.*]] = icmp eq i32 [[TMP0]], 0
; CHECK-NEXT: br i1 [[DOTNOT]], label [[TMP2:%.*]], label [[TMP1:%.*]]
; CHECK: 1:
; CHECK-NEXT: [[SIMPLIFYCFG_MERGE:%.*]] = select i1 [[X2]], i32 [[Z2]], i32 1
; CHECK-NEXT: store i32 [[SIMPLIFYCFG_MERGE]], i32* [[P:%.*]], align 4
; CHECK-NEXT: br label [[TMP2]]
; CHECK-NEXT: [[X2:%.*]] = icmp eq i32 [[B:%.*]], 0
; CHECK-NEXT: [[TMP0:%.*]] = or i32 [[B]], [[A:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[TMP0]], 0
; CHECK-NEXT: br i1 [[TMP1]], label [[TMP3:%.*]], label [[TMP2:%.*]]
; CHECK: 2:
; CHECK-NEXT: [[Z4:%.*]] = select i1 [[X2]], i32 [[Z2]], i32 3
; CHECK-NEXT: [[NOT_X2:%.*]] = xor i1 [[X2]], true
; CHECK-NEXT: [[SIMPLIFYCFG_MERGE:%.*]] = zext i1 [[NOT_X2]] to i32
; CHECK-NEXT: store i32 [[SIMPLIFYCFG_MERGE]], i32* [[P:%.*]], align 4
; CHECK-NEXT: br label [[TMP3]]
; CHECK: 3:
; CHECK-NEXT: [[Z4:%.*]] = select i1 [[X2]], i32 0, i32 3
; CHECK-NEXT: ret i32 [[Z4]]
;
entry:
Expand Down

0 comments on commit 07c3b97

Please sign in to comment.