[llvm] [CodeGenPrepare] Use Instruction::comesBefore instead of manual ordering (PR #190485)

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 4 13:41:28 PDT 2026


https://github.com/vitalybuka created https://github.com/llvm/llvm-project/pull/190485

This avoid quadratic complexity. `tryToSinkFreeOperands` can be
called on significant number of instructions of huge basic blocs.
We don't iterate and create DenseMap of all basic each time.

After #172329 we notice some source with Msan take 1000x more time to compile.


>From 8ed677382973b9d0802e5b555a443315525bdecb Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Sat, 4 Apr 2026 13:41:10 -0700
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
 =?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.7
---
 llvm/lib/CodeGen/CodeGenPrepare.cpp | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index aae6e718901fe..2c232c6158749 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -7968,17 +7968,12 @@ bool CodeGenPrepare::tryToSinkFreeOperands(Instruction *I) {
   bool Changed = false;
   SmallVector<Use *, 4> ToReplace;
   Instruction *InsertPoint = I;
-  DenseMap<const Instruction *, unsigned long> InstOrdering;
-  unsigned long InstNumber = 0;
-  for (const auto &I : *TargetBB)
-    InstOrdering[&I] = InstNumber++;
-
   for (Use *U : reverse(OpsToSink)) {
     auto *UI = cast<Instruction>(U->get());
     if (isa<PHINode>(UI) || UI->mayHaveSideEffects() || UI->mayReadFromMemory())
       continue;
     if (UI->getParent() == TargetBB) {
-      if (InstOrdering[UI] < InstOrdering[InsertPoint])
+      if (UI->comesBefore(InsertPoint))
         InsertPoint = UI;
       continue;
     }



More information about the llvm-commits mailing list