Skip to content

Commit b893822

Browse files
committed
Revert "[InstCombine] Simplify boolean Phis with const inputs using CFG"
This reverts commit 0047206. Need to fix failing clang tests.
1 parent fc55308 commit b893822

File tree

8 files changed

+46
-106
lines changed

8 files changed

+46
-106
lines changed

llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,75 +1129,6 @@ Instruction *InstCombiner::SliceUpIllegalIntegerPHI(PHINode &FirstPhi) {
11291129
return replaceInstUsesWith(FirstPhi, Undef);
11301130
}
11311131

1132-
static Value *SimplifyUsingControlFlow(InstCombiner &Self, PHINode &PN,
1133-
const DominatorTree &DT) {
1134-
// Simplify the following patterns:
1135-
// if (cond)
1136-
// / \
1137-
// ... ...
1138-
// \ /
1139-
// phi [true] [false]
1140-
if (!PN.getType()->isIntegerTy(1))
1141-
return nullptr;
1142-
1143-
if (PN.getNumOperands() != 2)
1144-
return nullptr;
1145-
1146-
// Make sure all inputs are constants.
1147-
if (!all_of(PN.operands(), [](Value *V) { return isa<ConstantInt>(V); }))
1148-
return nullptr;
1149-
1150-
BasicBlock *BB = PN.getParent();
1151-
// Do not bother with unreachable instructions.
1152-
if (!DT.isReachableFromEntry(BB))
1153-
return nullptr;
1154-
1155-
// Same inputs.
1156-
if (PN.getOperand(0) == PN.getOperand(1))
1157-
return PN.getOperand(0);
1158-
1159-
BasicBlock *TruePred = nullptr, *FalsePred = nullptr;
1160-
for (auto *Pred : predecessors(BB)) {
1161-
auto *Input = cast<ConstantInt>(PN.getIncomingValueForBlock(Pred));
1162-
if (Input->isAllOnesValue())
1163-
TruePred = Pred;
1164-
else
1165-
FalsePred = Pred;
1166-
}
1167-
assert(TruePred && FalsePred && "Must be!");
1168-
1169-
// Check which edge of the dominator dominates the true input. If it is the
1170-
// false edge, we should invert the condition.
1171-
auto *IDom = DT.getNode(BB)->getIDom()->getBlock();
1172-
auto *BI = dyn_cast<BranchInst>(IDom->getTerminator());
1173-
if (!BI || BI->isUnconditional())
1174-
return nullptr;
1175-
1176-
// Check that edges outgoing from the idom's terminators dominate respective
1177-
// inputs of the Phi.
1178-
BasicBlockEdge TrueOutEdge(IDom, BI->getSuccessor(0));
1179-
BasicBlockEdge FalseOutEdge(IDom, BI->getSuccessor(1));
1180-
1181-
BasicBlockEdge TrueIncEdge(TruePred, BB);
1182-
BasicBlockEdge FalseIncEdge(FalsePred, BB);
1183-
1184-
auto *Cond = BI->getCondition();
1185-
if (DT.dominates(TrueOutEdge, TrueIncEdge) &&
1186-
DT.dominates(FalseOutEdge, FalseIncEdge))
1187-
// This Phi is actually equivalent to branching condition of IDom.
1188-
return Cond;
1189-
else if (DT.dominates(TrueOutEdge, FalseIncEdge) &&
1190-
DT.dominates(FalseOutEdge, TrueIncEdge)) {
1191-
// This Phi is actually opposite to branching condition of IDom. We invert
1192-
// the condition that will potentially open up some opportunities for
1193-
// sinking.
1194-
Self.Builder.SetInsertPoint(BB->getFirstNonPHI());
1195-
return Self.Builder.CreateNot(Cond);
1196-
}
1197-
1198-
return nullptr;
1199-
}
1200-
12011132
// PHINode simplification
12021133
//
12031134
Instruction *InstCombiner::visitPHINode(PHINode &PN) {
@@ -1345,9 +1276,5 @@ Instruction *InstCombiner::visitPHINode(PHINode &PN) {
13451276
if (Instruction *Res = SliceUpIllegalIntegerPHI(PN))
13461277
return Res;
13471278

1348-
// Ultimately, try to replace this Phi with a dominating condition.
1349-
if (auto *V = SimplifyUsingControlFlow(*this, PN, DT))
1350-
return replaceInstUsesWith(PN, V);
1351-
13521279
return nullptr;
13531280
}

llvm/test/Transforms/CallSiteSplitting/callsite-split.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ declare void @dummy1(%struct.bitmap*, %struct.bitmap*, %struct.bitmap*, %struct.
7474
;CHECK-LABEL: NextCond.split:
7575
;CHECK: call void @dummy3()
7676
;CheCK-LABEL: CallSiteBB:
77-
;CHECK: [[NEG:%.*]] = xor i1 %tobool1, true
78-
;CHECK: call void @foo(i1 [[NEG]])
77+
;CHECK: %phi.call = phi i1 [ true, %NextCond.split ], [ false, %Top.split ]
78+
;CHECK: call void @foo(i1 %phi.call)
7979
define void @caller2(i1 %c, %struct.bitmap* %a_elt, %struct.bitmap* %b_elt, %struct.bitmap* %c_elt) {
8080
entry:
8181
br label %Top

llvm/test/Transforms/InstCombine/branch.ll

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ patatino:
3232
ret i32 %x
3333
}
3434

35+
; TODO: Simplify this to "ret cond".
3536
define i1 @test01(i1 %cond) {
3637
; CHECK-LABEL: @test01(
3738
; CHECK-NEXT: entry:
@@ -41,13 +42,15 @@ define i1 @test01(i1 %cond) {
4142
; CHECK: if.false.1:
4243
; CHECK-NEXT: br label [[MERGE_1]]
4344
; CHECK: merge.1:
44-
; CHECK-NEXT: br i1 [[COND]], label [[IF_TRUE_2:%.*]], label [[IF_FALSE_2:%.*]]
45+
; CHECK-NEXT: [[MERGE_COND_1:%.*]] = phi i1 [ true, [[IF_TRUE_1]] ], [ false, [[IF_FALSE_1]] ]
46+
; CHECK-NEXT: br i1 [[MERGE_COND_1]], label [[IF_TRUE_2:%.*]], label [[IF_FALSE_2:%.*]]
4547
; CHECK: if.true.2:
4648
; CHECK-NEXT: br label [[MERGE_2:%.*]]
4749
; CHECK: if.false.2:
4850
; CHECK-NEXT: br label [[MERGE_2]]
4951
; CHECK: merge.2:
50-
; CHECK-NEXT: ret i1 [[COND]]
52+
; CHECK-NEXT: [[MERGE_COND_2:%.*]] = phi i1 [ true, [[IF_TRUE_2]] ], [ false, [[IF_FALSE_2]] ]
53+
; CHECK-NEXT: ret i1 [[MERGE_COND_2]]
5154
;
5255
entry:
5356
br i1 %cond, label %if.true.1, label %if.false.1
@@ -73,6 +76,7 @@ merge.2:
7376
ret i1 %merge.cond.2
7477
}
7578

79+
; TODO: Simplify this to "ret %cond".
7680
define i1 @test02(i1 %cond) {
7781
; CHECK-LABEL: @test02(
7882
; CHECK-NEXT: entry:
@@ -82,13 +86,15 @@ define i1 @test02(i1 %cond) {
8286
; CHECK: if.false.1:
8387
; CHECK-NEXT: br label [[MERGE_1]]
8488
; CHECK: merge.1:
85-
; CHECK-NEXT: br i1 [[COND]], label [[IF_FALSE_2:%.*]], label [[IF_TRUE_2:%.*]]
89+
; CHECK-NEXT: [[MERGE_COND_1:%.*]] = phi i1 [ false, [[IF_TRUE_1]] ], [ true, [[IF_FALSE_1]] ]
90+
; CHECK-NEXT: br i1 [[MERGE_COND_1]], label [[IF_TRUE_2:%.*]], label [[IF_FALSE_2:%.*]]
8691
; CHECK: if.true.2:
8792
; CHECK-NEXT: br label [[MERGE_2:%.*]]
8893
; CHECK: if.false.2:
8994
; CHECK-NEXT: br label [[MERGE_2]]
9095
; CHECK: merge.2:
91-
; CHECK-NEXT: ret i1 [[COND]]
96+
; CHECK-NEXT: [[MERGE_COND_2:%.*]] = phi i1 [ false, [[IF_TRUE_2]] ], [ true, [[IF_FALSE_2]] ]
97+
; CHECK-NEXT: ret i1 [[MERGE_COND_2]]
9298
;
9399
entry:
94100
br i1 %cond, label %if.true.1, label %if.false.1

llvm/test/Transforms/InstCombine/icmp-constant-phi.ll

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ define i1 @test_eq(i1 %cond) {
1111
; CHECK: if.false:
1212
; CHECK-NEXT: br label [[MERGE]]
1313
; CHECK: merge:
14+
; CHECK-NEXT: [[COMPARE:%.*]] = phi i1 [ true, [[IF_FALSE]] ], [ false, [[IF_TRUE]] ]
1415
; CHECK-NEXT: br label [[EXIT:%.*]]
1516
; CHECK: exit:
16-
; CHECK-NEXT: [[TMP0:%.*]] = xor i1 [[COND]], true
17-
; CHECK-NEXT: ret i1 [[TMP0]]
17+
; CHECK-NEXT: ret i1 [[COMPARE]]
1818
;
1919
entry:
2020
br i1 %cond, label %if.true, label %if.false
@@ -43,9 +43,10 @@ define i1 @test_slt(i1 %cond) {
4343
; CHECK: if.false:
4444
; CHECK-NEXT: br label [[MERGE]]
4545
; CHECK: merge:
46+
; CHECK-NEXT: [[COMPARE:%.*]] = phi i1 [ false, [[IF_FALSE]] ], [ true, [[IF_TRUE]] ]
4647
; CHECK-NEXT: br label [[EXIT:%.*]]
4748
; CHECK: exit:
48-
; CHECK-NEXT: ret i1 [[COND]]
49+
; CHECK-NEXT: ret i1 [[COMPARE]]
4950
;
5051
entry:
5152
br i1 %cond, label %if.true, label %if.false
@@ -105,9 +106,10 @@ define i1 @test_ne(i1 %cond) {
105106
; CHECK: if.false:
106107
; CHECK-NEXT: br label [[MERGE]]
107108
; CHECK: merge:
109+
; CHECK-NEXT: [[COMPARE:%.*]] = phi i1 [ false, [[IF_FALSE]] ], [ true, [[IF_TRUE]] ]
108110
; CHECK-NEXT: br label [[EXIT:%.*]]
109111
; CHECK: exit:
110-
; CHECK-NEXT: ret i1 [[COND]]
112+
; CHECK-NEXT: ret i1 [[COMPARE]]
111113
;
112114
entry:
113115
br i1 %cond, label %if.true, label %if.false

llvm/test/Transforms/InstCombine/phi.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,10 @@ bb1: ; preds = %entry
416416

417417
bb2: ; preds = %bb1, %entry
418418
%cond = phi i1 [ true, %bb1 ], [ false, %entry ] ; <i1> [#uses=1]
419-
; CHECK-NOT: phi i1
420-
; CHECK: %res = phi i32 [ %0, %bb1 ], [ 0, %entry ]
421-
; CHECK: ret i32 %res
419+
; CHECK-NOT: %val = phi i32 [ %0, %bb1 ], [ 0, %entry ]
422420
%val = phi i32 [ %0, %bb1 ], [ 0, %entry ] ; <i32> [#uses=1]
423421
%res = select i1 %cond, i32 %val, i32 0 ; <i32> [#uses=1]
422+
; CHECK: ret i32 %cond
424423
ret i32 %res
425424
}
426425

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,8 @@ define i32 @test25(i1 %c) {
448448
; CHECK: jump:
449449
; CHECK-NEXT: br label [[RET]]
450450
; CHECK: ret:
451-
; CHECK-NEXT: [[B:%.*]] = phi i32 [ 10, [[JUMP]] ], [ 20, [[ENTRY:%.*]] ]
452-
; CHECK-NEXT: ret i32 [[B]]
451+
; CHECK-NEXT: [[A:%.*]] = phi i32 [ 10, [[JUMP]] ], [ 20, [[ENTRY:%.*]] ]
452+
; CHECK-NEXT: ret i32 [[A]]
453453
;
454454
entry:
455455
br i1 %c, label %jump, label %ret
@@ -468,8 +468,8 @@ define i32 @test26(i1 %cond) {
468468
; CHECK: jump:
469469
; CHECK-NEXT: br label [[RET]]
470470
; CHECK: ret:
471-
; CHECK-NEXT: [[B:%.*]] = phi i32 [ 10, [[JUMP]] ], [ 20, [[ENTRY:%.*]] ]
472-
; CHECK-NEXT: ret i32 [[B]]
471+
; CHECK-NEXT: [[A:%.*]] = phi i32 [ 20, [[ENTRY:%.*]] ], [ 10, [[JUMP]] ]
472+
; CHECK-NEXT: ret i32 [[A]]
473473
;
474474
entry:
475475
br i1 %cond, label %jump, label %ret
@@ -489,8 +489,8 @@ define i32 @test27(i1 %c, i32 %A, i32 %B) {
489489
; CHECK: jump:
490490
; CHECK-NEXT: br label [[RET]]
491491
; CHECK: ret:
492-
; CHECK-NEXT: [[S:%.*]] = phi i32 [ [[A:%.*]], [[JUMP]] ], [ [[B:%.*]], [[ENTRY:%.*]] ]
493-
; CHECK-NEXT: ret i32 [[S]]
492+
; CHECK-NEXT: [[P:%.*]] = phi i32 [ [[A:%.*]], [[JUMP]] ], [ [[B:%.*]], [[ENTRY:%.*]] ]
493+
; CHECK-NEXT: ret i32 [[P]]
494494
;
495495
entry:
496496
br i1 %c, label %jump, label %ret
@@ -509,8 +509,8 @@ define i32 @test28(i1 %cond, i32 %A, i32 %B) {
509509
; CHECK: jump:
510510
; CHECK-NEXT: br label [[RET]]
511511
; CHECK: ret:
512-
; CHECK-NEXT: [[S:%.*]] = phi i32 [ [[A:%.*]], [[JUMP]] ], [ [[B:%.*]], [[ENTRY:%.*]] ]
513-
; CHECK-NEXT: ret i32 [[S]]
512+
; CHECK-NEXT: [[P:%.*]] = phi i32 [ [[A:%.*]], [[JUMP]] ], [ [[B:%.*]], [[ENTRY:%.*]] ]
513+
; CHECK-NEXT: ret i32 [[P]]
514514
;
515515
entry:
516516
br i1 %cond, label %jump, label %ret
@@ -530,10 +530,10 @@ define i32 @test29(i1 %cond, i32 %A, i32 %B) {
530530
; CHECK: jump:
531531
; CHECK-NEXT: br label [[RET]]
532532
; CHECK: ret:
533-
; CHECK-NEXT: [[S:%.*]] = phi i32 [ [[A:%.*]], [[JUMP]] ], [ [[B:%.*]], [[ENTRY:%.*]] ]
533+
; CHECK-NEXT: [[P:%.*]] = phi i32 [ [[A:%.*]], [[JUMP]] ], [ [[B:%.*]], [[ENTRY:%.*]] ]
534534
; CHECK-NEXT: br label [[NEXT:%.*]]
535535
; CHECK: next:
536-
; CHECK-NEXT: ret i32 [[S]]
536+
; CHECK-NEXT: ret i32 [[P]]
537537
;
538538
entry:
539539
br i1 %cond, label %jump, label %ret

llvm/test/Transforms/InstCombine/simple_phi_condition.ll

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
; RUN: opt -S < %s -instcombine | FileCheck %s
33
; RUN: opt -S < %s -passes=instcombine | FileCheck %s
44

5+
; TODO: Simplify to "ret cond".
56
define i1 @test_direct_implication(i1 %cond) {
67
; CHECK-LABEL: @test_direct_implication(
78
; CHECK-NEXT: entry:
@@ -11,7 +12,8 @@ define i1 @test_direct_implication(i1 %cond) {
1112
; CHECK: if.false:
1213
; CHECK-NEXT: br label [[MERGE]]
1314
; CHECK: merge:
14-
; CHECK-NEXT: ret i1 [[COND]]
15+
; CHECK-NEXT: [[RET:%.*]] = phi i1 [ true, [[IF_TRUE]] ], [ false, [[IF_FALSE]] ]
16+
; CHECK-NEXT: ret i1 [[RET]]
1517
;
1618
entry:
1719
br i1 %cond, label %if.true, label %if.false
@@ -27,6 +29,7 @@ merge:
2729
ret i1 %ret
2830
}
2931

32+
; TODO: Simplify to "ret !cond".
3033
define i1 @test_inverted_implication(i1 %cond) {
3134
; CHECK-LABEL: @test_inverted_implication(
3235
; CHECK-NEXT: entry:
@@ -36,8 +39,8 @@ define i1 @test_inverted_implication(i1 %cond) {
3639
; CHECK: if.false:
3740
; CHECK-NEXT: br label [[MERGE]]
3841
; CHECK: merge:
39-
; CHECK-NEXT: [[TMP0:%.*]] = xor i1 [[COND]], true
40-
; CHECK-NEXT: ret i1 [[TMP0]]
42+
; CHECK-NEXT: [[RET:%.*]] = phi i1 [ false, [[IF_TRUE]] ], [ true, [[IF_FALSE]] ]
43+
; CHECK-NEXT: ret i1 [[RET]]
4144
;
4245
entry:
4346
br i1 %cond, label %if.true, label %if.false
@@ -53,6 +56,7 @@ merge:
5356
ret i1 %ret
5457
}
5558

59+
; TODO: Simplify to "ret cond".
5660
define i1 @test_direct_implication_complex_cfg(i1 %cond, i32 %cnt1) {
5761
; CHECK-LABEL: @test_direct_implication_complex_cfg(
5862
; CHECK-NEXT: entry:
@@ -69,7 +73,8 @@ define i1 @test_direct_implication_complex_cfg(i1 %cond, i32 %cnt1) {
6973
; CHECK: if.false:
7074
; CHECK-NEXT: br label [[MERGE]]
7175
; CHECK: merge:
72-
; CHECK-NEXT: ret i1 [[COND]]
76+
; CHECK-NEXT: [[RET:%.*]] = phi i1 [ true, [[IF_TRUE_END]] ], [ false, [[IF_FALSE]] ]
77+
; CHECK-NEXT: ret i1 [[RET]]
7378
;
7479
entry:
7580
br i1 %cond, label %if.true, label %if.false
@@ -94,6 +99,7 @@ merge:
9499
ret i1 %ret
95100
}
96101

102+
; TODO: Simplify to "ret !cond".
97103
define i1 @test_inverted_implication_complex_cfg(i1 %cond, i32 %cnt1) {
98104
; CHECK-LABEL: @test_inverted_implication_complex_cfg(
99105
; CHECK-NEXT: entry:
@@ -110,8 +116,8 @@ define i1 @test_inverted_implication_complex_cfg(i1 %cond, i32 %cnt1) {
110116
; CHECK: if.false:
111117
; CHECK-NEXT: br label [[MERGE]]
112118
; CHECK: merge:
113-
; CHECK-NEXT: [[TMP0:%.*]] = xor i1 [[COND]], true
114-
; CHECK-NEXT: ret i1 [[TMP0]]
119+
; CHECK-NEXT: [[RET:%.*]] = phi i1 [ false, [[IF_TRUE_END]] ], [ true, [[IF_FALSE]] ]
120+
; CHECK-NEXT: ret i1 [[RET]]
115121
;
116122
entry:
117123
br i1 %cond, label %if.true, label %if.false

llvm/test/Transforms/PhaseOrdering/simplifycfg-options.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ define i1 @PR33605(i32 %a, i32 %b, i32* %c) {
1818
; ALL-NEXT: call void @foo()
1919
; ALL-NEXT: br label [[IF_END]]
2020
; ALL: if.end:
21-
; ALL-NEXT: [[TMP1:%.*]] = xor i1 [[CMP]], true
22-
; ALL-NEXT: [[TMP2:%.*]] = load i32, i32* [[C]], align 4
23-
; ALL-NEXT: [[CMP_1:%.*]] = icmp eq i32 [[OR]], [[TMP2]]
21+
; ALL-NEXT: [[CHANGED_1_OFF0:%.*]] = phi i1 [ true, [[IF_THEN]] ], [ false, [[ENTRY:%.*]] ]
22+
; ALL-NEXT: [[TMP1:%.*]] = load i32, i32* [[C]], align 4
23+
; ALL-NEXT: [[CMP_1:%.*]] = icmp eq i32 [[OR]], [[TMP1]]
2424
; ALL-NEXT: br i1 [[CMP_1]], label [[IF_END_1:%.*]], label [[IF_THEN_1:%.*]]
2525
; ALL: if.then.1:
2626
; ALL-NEXT: store i32 [[OR]], i32* [[C]], align 4
2727
; ALL-NEXT: call void @foo()
2828
; ALL-NEXT: br label [[IF_END_1]]
2929
; ALL: if.end.1:
30-
; ALL-NEXT: [[CHANGED_1_OFF0_1:%.*]] = phi i1 [ true, [[IF_THEN_1]] ], [ [[TMP1]], [[IF_END]] ]
30+
; ALL-NEXT: [[CHANGED_1_OFF0_1:%.*]] = phi i1 [ true, [[IF_THEN_1]] ], [ [[CHANGED_1_OFF0]], [[IF_END]] ]
3131
; ALL-NEXT: ret i1 [[CHANGED_1_OFF0_1]]
3232
;
3333
entry:

0 commit comments

Comments
 (0)