-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[SimplifyCFG] Handle that first matched eq cond in if chain can be Extra condition. #154007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-llvm-transforms Author: Andreas Jonson (andjo403) ChangesProof: https://alive2.llvm.org/ce/z/TozSD6 Noticed as a regression when I tried to add trunc nuw handling. Full diff: https://github.com/llvm/llvm-project/pull/154007.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 1436e479ba091..42b35c0c7e69e 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -564,9 +564,19 @@ struct ConstantComparesGatherer {
/// Number of comparisons matched in the and/or chain
unsigned UsedICmps = 0;
+ // Used to check if the first matched CompValue shall be the Extra check.
+ bool IgnoreFirstMatch = false;
+
/// Construct and compute the result for the comparison instruction Cond
ConstantComparesGatherer(Instruction *Cond, const DataLayout &DL) : DL(DL) {
gather(Cond);
+ if (CompValue)
+ return;
+ Extra = nullptr;
+ Vals.clear();
+ UsedICmps = 0;
+ IgnoreFirstMatch = true;
+ gather(Cond);
}
ConstantComparesGatherer(const ConstantComparesGatherer &) = delete;
@@ -577,6 +587,10 @@ struct ConstantComparesGatherer {
/// Try to set the current value used for the comparison, it succeeds only if
/// it wasn't set before or if the new value is the same as the old one
bool setValueOnce(Value *NewVal) {
+ if (IgnoreFirstMatch && NewVal) {
+ IgnoreFirstMatch = false;
+ return false;
+ }
if (CompValue && CompValue != NewVal)
return false;
CompValue = NewVal;
diff --git a/llvm/test/Transforms/SimplifyCFG/switch_create.ll b/llvm/test/Transforms/SimplifyCFG/switch_create.ll
index f446d718f8206..5f3c7b7c0fd5d 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch_create.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch_create.ll
@@ -1068,3 +1068,71 @@ if:
else:
ret void
}
+
+define void @extra_cond_is_eq_cmp(i8 %c, i32 %x) {
+; CHECK-LABEL: @extra_cond_is_eq_cmp(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 32
+; CHECK-NEXT: [[TMP0:%.*]] = freeze i1 [[CMP]]
+; CHECK-NEXT: br i1 [[TMP0]], label [[IF_THEN:%.*]], label [[SWITCH_EARLY_TEST:%.*]]
+; CHECK: switch.early.test:
+; CHECK-NEXT: switch i8 [[C:%.*]], label [[COMMON_RET:%.*]] [
+; CHECK-NEXT: i8 99, label [[IF_THEN]]
+; CHECK-NEXT: i8 97, label [[IF_THEN]]
+; CHECK-NEXT: ]
+; CHECK: common.ret:
+; CHECK-NEXT: ret void
+; CHECK: if.then:
+; CHECK-NEXT: tail call void @foo1()
+; CHECK-NEXT: br label [[COMMON_RET]]
+;
+entry:
+ %cmp = icmp eq i32 %x, 32
+ %cmp4 = icmp eq i8 %c, 97
+ %or.cond = or i1 %cmp, %cmp4
+ %cmp9 = icmp eq i8 %c, 99
+ %or.cond11 = or i1 %or.cond, %cmp9
+ br i1 %or.cond11, label %if.then, label %if.end
+
+if.then:
+ tail call void @foo1()
+ ret void
+
+if.end:
+ ret void
+
+}
+
+define void @extra_cond_is_eq_cmp_c(i8 %c, i32 %x) {
+; CHECK-LABEL: @extra_cond_is_eq_cmp_c(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 32
+; CHECK-NEXT: [[TMP0:%.*]] = freeze i1 [[CMP]]
+; CHECK-NEXT: br i1 [[TMP0]], label [[IF_THEN:%.*]], label [[SWITCH_EARLY_TEST:%.*]]
+; CHECK: switch.early.test:
+; CHECK-NEXT: switch i8 [[C:%.*]], label [[COMMON_RET:%.*]] [
+; CHECK-NEXT: i8 99, label [[IF_THEN]]
+; CHECK-NEXT: i8 97, label [[IF_THEN]]
+; CHECK-NEXT: ]
+; CHECK: common.ret:
+; CHECK-NEXT: ret void
+; CHECK: if.then:
+; CHECK-NEXT: tail call void @foo1()
+; CHECK-NEXT: br label [[COMMON_RET]]
+;
+entry:
+ %cmp = icmp eq i32 %x, 32
+ %cmp4 = icmp eq i8 %c, 97
+ %or.cond = or i1 %cmp4, %cmp
+ %cmp9 = icmp eq i8 %c, 99
+ %or.cond11 = or i1 %or.cond, %cmp9
+ br i1 %or.cond11, label %if.then, label %if.end
+
+if.then:
+ tail call void @foo1()
+ ret void
+
+if.end:
+ ret void
+
+}
|
That seems like a lot of time for the changes that it give |
with the new commit Compile-time impact: -0.15% |
Split out from #154007 as it showed compile time improvements NFC as there needs to be at least two icmps that is part of the chain.
…133) Split out from llvm/llvm-project#154007 as it showed compile time improvements NFC as there needs to be at least two icmps that is part of the chain.
51eec75
to
3153ab8
Compare
a bit unclear how this is faster now Overall: -0.028% is it to small time to be significant? |
…n be Extra condition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Proof: https://alive2.llvm.org/ce/z/TozSD6
Noticed as a regression when I tried to add trunc nuw handling.