Skip to content

Commit 3153ab8

Browse files
committed
[SimplifyCFG] Handle that first matched eq cond in if chain can be Extra condition.
1 parent d18a999 commit 3153ab8

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,20 @@ struct ConstantComparesGatherer {
568568
/// If the elements in Vals matches the comparisons
569569
bool IsEq = false;
570570

571+
// Used to check if the first matched CompValue shall be the Extra check.
572+
bool IgnoreFirstMatch = false;
573+
bool MultipleMatches = false;
574+
571575
/// Construct and compute the result for the comparison instruction Cond
572576
ConstantComparesGatherer(Instruction *Cond, const DataLayout &DL) : DL(DL) {
573577
gather(Cond);
578+
if (CompValue || !MultipleMatches)
579+
return;
580+
Extra = nullptr;
581+
Vals.clear();
582+
UsedICmps = 0;
583+
IgnoreFirstMatch = true;
584+
gather(Cond);
574585
}
575586

576587
ConstantComparesGatherer(const ConstantComparesGatherer &) = delete;
@@ -581,8 +592,14 @@ struct ConstantComparesGatherer {
581592
/// Try to set the current value used for the comparison, it succeeds only if
582593
/// it wasn't set before or if the new value is the same as the old one
583594
bool setValueOnce(Value *NewVal) {
584-
if (CompValue && CompValue != NewVal)
595+
if (IgnoreFirstMatch && NewVal) {
596+
IgnoreFirstMatch = false;
585597
return false;
598+
}
599+
if (CompValue && CompValue != NewVal) {
600+
MultipleMatches = true;
601+
return false;
602+
}
586603
CompValue = NewVal;
587604
return (CompValue != nullptr);
588605
}

llvm/test/Transforms/SimplifyCFG/switch_create.ll

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,16 +1129,18 @@ define void @extra_cond_is_eq_cmp(i8 %c, i32 %x) {
11291129
; CHECK-LABEL: @extra_cond_is_eq_cmp(
11301130
; CHECK-NEXT: entry:
11311131
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 32
1132-
; CHECK-NEXT: [[CMP4:%.*]] = icmp eq i8 [[C:%.*]], 97
1133-
; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[CMP]], [[CMP4]]
1134-
; CHECK-NEXT: [[CMP9:%.*]] = icmp eq i8 [[C]], 99
1135-
; CHECK-NEXT: [[TMP0:%.*]] = or i1 [[OR_COND]], [[CMP9]]
1132+
; CHECK-NEXT: [[TMP0:%.*]] = freeze i1 [[CMP]]
11361133
; CHECK-NEXT: br i1 [[TMP0]], label [[IF_THEN:%.*]], label [[SWITCH_EARLY_TEST:%.*]]
1134+
; CHECK: switch.early.test:
1135+
; CHECK-NEXT: switch i8 [[C:%.*]], label [[COMMON_RET:%.*]] [
1136+
; CHECK-NEXT: i8 99, label [[IF_THEN]]
1137+
; CHECK-NEXT: i8 97, label [[IF_THEN]]
1138+
; CHECK-NEXT: ]
11371139
; CHECK: common.ret:
11381140
; CHECK-NEXT: ret void
11391141
; CHECK: if.then:
11401142
; CHECK-NEXT: tail call void @foo1()
1141-
; CHECK-NEXT: br label [[SWITCH_EARLY_TEST]]
1143+
; CHECK-NEXT: br label [[COMMON_RET]]
11421144
;
11431145
entry:
11441146
%cmp = icmp eq i32 %x, 32

0 commit comments

Comments
 (0)