-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[SimplifyCFG] Avoid threading for loop headers #151142
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
base: main
Are you sure you want to change the base?
[SimplifyCFG] Avoid threading for loop headers #151142
Conversation
Updates SimplifyCFG to avoid jump threading through loop headers if -keep-loops is requested. Canonical loop form requires a loop header that dominates all blocks in the loop. If we thread through a header, we risk breaking its domination of the loop. This change avoids this issue by conservatively avoiding threading through headers entirely.
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-transforms @llvm/pr-subscribers-backend-arm Author: Arne Stenkrona (ArneStenkrona2) ChangesUpdates SimplifyCFG to avoid jump threading through loop headers if -keep-loops is requested. Canonical loop form requires a loop header that dominates all blocks in the loop. If we thread through a header, we risk breaking its domination of the loop. This change avoids this issue by conservatively avoiding threading through headers entirely. Full diff: https://github.com/llvm/llvm-project/pull/151142.diff 7 Files Affected:
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 94b0ab892f2dd..d8385beb349d6 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -8030,8 +8030,14 @@ bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
// If this is a branch on something for which we know the constant value in
// predecessors (e.g. a phi node in the current block), thread control
// through this block.
- if (foldCondBranchOnValueKnownInPredecessor(BI, DTU, DL, Options.AC))
- return requestResimplify();
+ // Note: If BB is a loop header then there is a risk that threading introduces
+ // a non-canonical loop by moving a back edge. So we avoid this optimization
+ // for loop headers if NeedCanonicalLoop is set.
+ bool InHeader = !LoopHeaders.empty() && is_contained(LoopHeaders, BB);
+ bool AvoidThreading = Options.NeedCanonicalLoop && InHeader;
+ if (!AvoidThreading)
+ if (foldCondBranchOnValueKnownInPredecessor(BI, DTU, DL, Options.AC))
+ return requestResimplify();
// Scan predecessor blocks for conditional branches.
for (BasicBlock *Pred : predecessors(BB))
diff --git a/llvm/test/CodeGen/ARM/2013-05-05-IfConvertBug.ll b/llvm/test/CodeGen/ARM/2013-05-05-IfConvertBug.ll
index 344bb15d2a8b8..8f798fac06f54 100644
--- a/llvm/test/CodeGen/ARM/2013-05-05-IfConvertBug.ll
+++ b/llvm/test/CodeGen/ARM/2013-05-05-IfConvertBug.ll
@@ -1,7 +1,7 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
-; RUN: llc < %s -mtriple=thumbv7-apple-ios -mcpu=cortex-a8 | FileCheck %s
-; RUN: llc < %s -mtriple=thumbv8 | FileCheck -check-prefix=CHECK-V8 %s
-; RUN: llc < %s -mtriple=thumbv7 -arm-restrict-it | FileCheck -check-prefix=CHECK-RESTRICT-IT %s
+; RUN: llc -keep-loops="false" < %s -mtriple=thumbv7-apple-ios -mcpu=cortex-a8 | FileCheck %s
+; RUN: llc -keep-loops="false" < %s -mtriple=thumbv8 | FileCheck -check-prefix=CHECK-V8 %s
+; RUN: llc -keep-loops="false" < %s -mtriple=thumbv7 -arm-restrict-it | FileCheck -check-prefix=CHECK-RESTRICT-IT %s
define i32 @t1(i32 %a, i32 %b, ptr %retaddr) {
; CHECK-LABEL: t1:
diff --git a/llvm/test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll b/llvm/test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll
index 2e9e7b19c73e2..44d92e1a1c210 100644
--- a/llvm/test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll
+++ b/llvm/test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll
@@ -1,5 +1,5 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
+; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -keep-loops="false" -S | FileCheck %s
; PR2540
; Outval should end up with a select from 0/2, not all constants.
@@ -52,4 +52,3 @@ func_1.exit: ; preds = %cowblock, %entry
}
declare i32 @printf(ptr, ...) nounwind
-
diff --git a/llvm/test/Transforms/SimplifyCFG/2025-07-29-non-canoncial-loop.ll b/llvm/test/Transforms/SimplifyCFG/2025-07-29-non-canoncial-loop.ll
new file mode 100644
index 0000000000000..a64108f6346b9
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/2025-07-29-non-canoncial-loop.ll
@@ -0,0 +1,37 @@
+; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck --check-prefix=NO-THREADING %s
+; Checks that we do not thread the control flow through the loop header bb1 as
+; that will introduce a non-canonical loop
+
+; NO-THREADING-LABEL: define void @__start
+; NO-THREADING: bb3:
+; NO-THREADING-NEXT: br i1 %cond, label %bb1, label %bb5
+
+; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 --keep-loops="false" -S | FileCheck --check-prefix=THREADING %s
+; Checks that we thread the control flow through the loop header bb1 since we
+; do not request --keep-loops
+
+; THREADING-LABEL: define void @__start
+; THREADING: bb3:
+; THREADING-NEXT: br i1 %cond, label %bb4, label %bb5
+
+define void @__start(i1 %cond) {
+entry:
+ br label %bb1
+
+bb1: ; preds = %bb3, %entry
+ br i1 %cond, label %bb4, label %bb2
+
+bb2: ; preds = %bb1
+ %_0_ = add i16 0, 0
+ br label %bb3
+
+bb3: ; preds = %bb4, %bb2
+ br i1 %cond, label %bb1, label %bb5
+
+bb4: ; preds = %bb1
+ %_1_ = add i32 0, 1
+ br label %bb3
+
+bb5: ; preds = %bb3
+ ret void
+}
diff --git a/llvm/test/Transforms/SimplifyCFG/branch-phi-thread.ll b/llvm/test/Transforms/SimplifyCFG/branch-phi-thread.ll
index 0afec05ecbd6a..ec9423bd81675 100644
--- a/llvm/test/Transforms/SimplifyCFG/branch-phi-thread.ll
+++ b/llvm/test/Transforms/SimplifyCFG/branch-phi-thread.ll
@@ -1,5 +1,5 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -passes=simplifycfg,adce -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
+; RUN: opt < %s -passes=simplifycfg,adce -simplifycfg-require-and-preserve-domtree=1 -keep-loops="false" -S | FileCheck %s
declare void @f1()
diff --git a/llvm/test/Transforms/SimplifyCFG/jump-threading.ll b/llvm/test/Transforms/SimplifyCFG/jump-threading.ll
index 50a32413a0551..a4073ae6eb0b4 100644
--- a/llvm/test/Transforms/SimplifyCFG/jump-threading.ll
+++ b/llvm/test/Transforms/SimplifyCFG/jump-threading.ll
@@ -1,5 +1,5 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -S -passes=simplifycfg < %s | FileCheck %s
+; RUN: opt -S -passes=simplifycfg -keep-loops="false" < %s | FileCheck %s
declare void @foo()
declare void @bar()
diff --git a/llvm/test/Transforms/SimplifyCFG/two-entry-phi-return.ll b/llvm/test/Transforms/SimplifyCFG/two-entry-phi-return.ll
index 57930c91b9796..f6d71ddda74fe 100644
--- a/llvm/test/Transforms/SimplifyCFG/two-entry-phi-return.ll
+++ b/llvm/test/Transforms/SimplifyCFG/two-entry-phi-return.ll
@@ -1,5 +1,5 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
+; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -keep-loops="false" -S | FileCheck %s
define i1 @qux(ptr %m, ptr %n, ptr %o, ptr %p) nounwind {
; CHECK-LABEL: @qux(
|
bool InHeader = !LoopHeaders.empty() && is_contained(LoopHeaders, BB); | ||
bool AvoidThreading = Options.NeedCanonicalLoop && InHeader; | ||
if (!AvoidThreading) | ||
if (foldCondBranchOnValueKnownInPredecessor(BI, DTU, DL, Options.AC)) |
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.
I think it would be cleaner to move these checks in foldCondBranchOnValueKnownInPredecessor.
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.
Agreed. I changed foldCondBranchOnValueKnownInPredecessor to a method of SimplifyCFGOpt and moved the checks inside it.
// Note: If BB is a loop header then there is a risk that threading introduces | ||
// a non-canonical loop by moving a back edge. So we avoid this optimization | ||
// for loop headers if NeedCanonicalLoop is set. | ||
bool InHeader = !LoopHeaders.empty() && is_contained(LoopHeaders, BB); |
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.
The empty check is redundant?
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.
Noted. Removed.
@@ -0,0 +1,37 @@ | |||
; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck --check-prefix=NO-THREADING %s |
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.
Please use update_test_checks.py.
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.
Done.
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.
This generally sounds like a sensible thing to do. The dedicated JumpThreading pass checks this as well.
DomTreeUpdater *DTU, | ||
const DataLayout &DL, | ||
AssumptionCache *AC) { | ||
#include <iostream> |
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.
Stray iostream?
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.
Yes, sorry about that. Local debugging. Will remove right away.
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.
Done
// for loop headers if NeedCanonicalLoop is set. | ||
bool InHeader = is_contained(LoopHeaders, BI->getParent()); | ||
bool AvoidThreading = Options.NeedCanonicalLoop && InHeader; | ||
if (AvoidThreading) { |
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.
Omit braces for one-line if.
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.
Done
// Note: If BB is a loop header then there is a risk that threading introduces | ||
// a non-canonical loop by moving a back edge. So we avoid this optimization | ||
// for loop headers if NeedCanonicalLoop is set. | ||
bool InHeader = is_contained(LoopHeaders, BI->getParent()); |
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.
Avoid the variable and move this behind the &&
for short-circuiting. No need to do this linear scan if !NeedCanonicalLoop.
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.
Done
✅ With the latest revision this PR passed the C/C++ code formatter. |
@nikic Let me know if there is anything else you'd like to see changed with the PR. |
Updates SimplifyCFG to avoid jump threading through loop headers if -keep-loops is requested. Canonical loop form requires a loop header that dominates all blocks in the loop. If we thread through a header, we risk breaking its domination of the loop. This change avoids this issue by conservatively avoiding threading through headers entirely.
Fixes: #151144