[llvm] [CodeGen] Avoid quadratic operand removal in MachineInstr::insert (PR #220165)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 6 10:24:58 PDT 2026


https://github.com/LittleJianCH updated https://github.com/llvm/llvm-project/pull/220165

>From e29d6d2eca71c6a1838a46f5038f85c58ba7d302 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=AE=80?= <chenhowe0529 at gmail.com>
Date: Tue, 1 Sep 2026 13:49:10 +0800
Subject: [PATCH] [CodeGen] Avoid quadratic operand removal in
 MachineInstr::insert

---
 llvm/lib/CodeGen/MachineInstr.cpp | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp
index e94de37261c53..36a08ea0774b3 100644
--- a/llvm/lib/CodeGen/MachineInstr.cpp
+++ b/llvm/lib/CodeGen/MachineInstr.cpp
@@ -2734,16 +2734,10 @@ void MachineInstr::insert(mop_iterator InsertBefore,
   }
 
   unsigned OpIdx = getOperandNo(InsertBefore);
-  unsigned NumOperands = getNumOperands();
-  unsigned OpsToMove = NumOperands - OpIdx;
+  SmallVector<MachineOperand> MovingOps(InsertBefore, operands_end());
 
-  SmallVector<MachineOperand> MovingOps;
-  MovingOps.reserve(OpsToMove);
-
-  for (unsigned I = 0; I < OpsToMove; ++I) {
-    MovingOps.emplace_back(getOperand(OpIdx));
-    removeOperand(OpIdx);
-  }
+  for (unsigned I = getNumOperands(); I > OpIdx; --I)
+    removeOperand(I - 1);
   for (const MachineOperand &MO : Ops)
     addOperand(MO);
   for (const MachineOperand &OpMoved : MovingOps)



More information about the llvm-commits mailing list