Skip to content

Conversation

@kika
Copy link
Contributor

@kika kika commented Jan 11, 2026

Add a hasInsertionPt() helper, which is equivalent to getFirstInsertionPt() != end(), but performs the check in O(1) instead of O(n). In particular, this avoids quadratic complexity inside SliceUpIllegalIntegerPHI().

Fixes #175465.
Should fix rust-lang/rust#129713.

@kika kika requested a review from nikic as a code owner January 11, 2026 22:59
@github-actions
Copy link

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 @ followed by their GitHub username.

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.

@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Jan 11, 2026
@llvmbot
Copy link
Member

llvmbot commented Jan 11, 2026

@llvm/pr-subscribers-llvm-ir

@llvm/pr-subscribers-llvm-transforms

Author: Kirill Pertsev (kika)

Changes

Fixes #175465

This is all based on my understanding of the LLVM codebase and reading comments in the code.
Take it with a grain of salt. But I does what it promises to do and do not fail any tests.
Test is not added, because there's no change in functionality and no new features, the only possible test is the repro case for the fixed issue.

InstCombine pass

The catchswitch predecessor check added in LLVM 15 was placed inside the per-PHI processing loop, causing getFirstInsertionPt() to be called repeatedly for the same predecessor blocks across all PHIs in a network.

For code patterns with many PHIs sharing common predecessors (e.g., Rust drop flags for large structs), this resulted in O(PHIs × blocks) complexity.

Move the check outside the loop by collecting all unique predecessor blocks first and checking each only once, reducing complexity to O(unique blocks).

Should fix rust-lang/rust#129713


Full diff: https://github.com/llvm/llvm-project/pull/175468.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp (+15-7)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
index ba1865a2b5469..78d45b691c8fc 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
@@ -1129,13 +1129,6 @@ Instruction *InstCombinerImpl::SliceUpIllegalIntegerPHI(PHINode &FirstPhi) {
       return nullptr;
     }
 
-    // If the incoming value is a PHI node before a catchswitch, we cannot
-    // extract the value within that BB because we cannot insert any non-PHI
-    // instructions in the BB.
-    for (auto *Pred : PN->blocks())
-      if (Pred->getFirstInsertionPt() == Pred->end())
-        return nullptr;
-
     for (User *U : PN->users()) {
       Instruction *UserI = cast<Instruction>(U);
 
@@ -1172,6 +1165,21 @@ Instruction *InstCombinerImpl::SliceUpIllegalIntegerPHI(PHINode &FirstPhi) {
   if (PHIUsers.empty())
     return replaceInstUsesWith(FirstPhi, PoisonValue::get(FirstPhi.getType()));
 
+  // If the incoming value is a PHI node before a catchswitch, we cannot
+  // extract the value within that BB because we cannot insert any non-PHI
+  // instructions in the BB. Check all unique predecessor blocks once.
+  {
+    SmallPtrSet<BasicBlock *, 16> CheckedBlocks;
+    for (PHINode *PN : PHIsToSlice) {
+      for (BasicBlock *Pred : PN->blocks()) {
+        if (CheckedBlocks.insert(Pred).second) {
+          if (Pred->getFirstInsertionPt() == Pred->end())
+            return nullptr;
+        }
+      }
+    }
+  }
+
   // If this phi node is transformable, create new PHIs for all the pieces
   // extracted out of it.  First, sort the users by their offset and size.
   array_pod_sort(PHIUsers.begin(), PHIUsers.end());

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does fix the problem, but I'd like to suggest an alternative: There is really no reason why this check should be expensive in the first place. Checking whether a block has an insertion point is really just "is the terminator catchswitch" (or maybe phrased more generically, "is the terminator an EH pad"), which is a cheap O(1) operation.

The only reason this ends up expensive here is that finding the insertion point (if it exists) requires scanning through all the phi nodes in the block. But just checking if there is one is cheap.

What I'd suggest doing is add a method BasicBlock::hasInsertionPt() and then use it here (can then keep the code in the original position). Then we can also update other occurrences of this pattern like these:

if (isa<PHINode>(Inst) && BB->getFirstInsertionPt() == BB->end())

PN.getParent()->getFirstInsertionPt() != PN.getParent()->end()) {

I.getParent()->getFirstInsertionPt() == I.getParent()->end())

I haven't checked if any of those could have compile-time scalability issues, but better safe than sorry.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Oops, inline is not necessary in the header

Co-authored-by: Nikita Popov <github@npopov.com>
@nikic nikic changed the title Fix O(n^2) complexity in SliceUpIllegalIntegerPHI [IR][InstCombine] Fix O(n^2) complexity in SliceUpIllegalIntegerPHI Jan 12, 2026
@nikic nikic enabled auto-merge (squash) January 12, 2026 20:23
@nikic nikic merged commit 5334c51 into llvm:main Jan 12, 2026
9 of 10 checks passed
@github-actions
Copy link

@kika Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Priyanshu3820 pushed a commit to Priyanshu3820/llvm-project that referenced this pull request Jan 18, 2026
…lvm#175468)

Add a hasInsertionPt() helper, which is equivalent to
getFirstInsertionPt() != end(), but performs the check in O(1) instead
of O(n). In particular, this avoids quadratic complexity inside
SliceUpIllegalIntegerPHI().

Fixes llvm#175465.
Should fix rust-lang/rust#129713.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:ir llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[rust] Performance degradation from LLVM14->15 still present in 21 Very long compilation time on Apple Silicon platform

3 participants