Skip to content

Commit c8ec9ed

Browse files
committed
[SimplifyCFG] Support trunc nuw in chain of comparisons.
1 parent 3a67ccf commit c8ec9ed

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,15 @@ struct ConstantComparesGatherer {
612612
/// If CompValue is already set, the function is expected to fail if a match
613613
/// is found but the value compared to is different.
614614
bool matchInstruction(Instruction *I, bool isEQ) {
615+
Value *Val;
616+
if (match(I, m_NUWTrunc(m_Value(Val)))) {
617+
// If we already have a value for the switch, it has to match!
618+
if (!setValueOnce(Val))
619+
return false;
620+
UsedICmps++;
621+
Vals.push_back(ConstantInt::get(cast<IntegerType>(Val->getType()), isEQ));
622+
return true;
623+
}
615624
// If this is an icmp against a constant, handle this as one of the cases.
616625
ICmpInst *ICI;
617626
ConstantInt *C;

llvm/test/Transforms/SimplifyCFG/switch_create.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,11 +1195,12 @@ if.end:
11951195

11961196
define void @and_chain_trunc_nuw_i1_condition(i8 %x) {
11971197
; CHECK-LABEL: @and_chain_trunc_nuw_i1_condition(
1198-
; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[X:%.*]], -2
1199-
; CHECK-NEXT: [[ICMP:%.*]] = icmp ugt i8 [[ADD]], 2
1200-
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i8 [[X]] to i1
1201-
; CHECK-NEXT: [[AND:%.*]] = select i1 [[ICMP]], i1 [[TRUNC]], i1 false
1202-
; CHECK-NEXT: br i1 [[AND]], label [[IF_THEN:%.*]], label [[COMMON_RET:%.*]]
1198+
; CHECK-NEXT: switch i8 [[X:%.*]], label [[IF_THEN:%.*]] [
1199+
; CHECK-NEXT: i8 4, label [[COMMON_RET:%.*]]
1200+
; CHECK-NEXT: i8 3, label [[COMMON_RET]]
1201+
; CHECK-NEXT: i8 2, label [[COMMON_RET]]
1202+
; CHECK-NEXT: i8 0, label [[COMMON_RET]]
1203+
; CHECK-NEXT: ]
12031204
; CHECK: common.ret:
12041205
; CHECK-NEXT: ret void
12051206
; CHECK: if.then:
@@ -1220,9 +1221,8 @@ if.end:
12201221

12211222
define void @or_chain_trunc_nuw_i1_condition(i8 %x) {
12221223
; CHECK-LABEL: @or_chain_trunc_nuw_i1_condition(
1223-
; CHECK-NEXT: [[ICMP:%.*]] = icmp eq i8 [[X:%.*]], 2
1224-
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i8 [[X]] to i1
1225-
; CHECK-NEXT: [[SWITCH:%.*]] = select i1 [[ICMP]], i1 true, i1 [[TRUNC]]
1224+
; CHECK-NEXT: [[X_OFF:%.*]] = add i8 [[X:%.*]], -1
1225+
; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i8 [[X_OFF]], 2
12261226
; CHECK-NEXT: br i1 [[SWITCH]], label [[IF_THEN:%.*]], label [[COMMON_RET:%.*]]
12271227
; CHECK: common.ret:
12281228
; CHECK-NEXT: ret void

0 commit comments

Comments
 (0)