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

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 6 10:19:00 PDT 2026


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

>From 57406d12c0ce97f65db43fa87ff139474c910e3d 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 | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp
index e94de37261c53..52fb5924696e5 100644
--- a/llvm/lib/CodeGen/MachineInstr.cpp
+++ b/llvm/lib/CodeGen/MachineInstr.cpp
@@ -2733,17 +2733,12 @@ void MachineInstr::insert(mop_iterator InsertBefore,
     }
   }
 
-  unsigned OpIdx = getOperandNo(InsertBefore);
-  unsigned NumOperands = getNumOperands();
-  unsigned OpsToMove = NumOperands - OpIdx;
 
-  SmallVector<MachineOperand> MovingOps;
-  MovingOps.reserve(OpsToMove);
+  unsigned OpIdx = getOperandNo(InsertBefore);
+  SmallVector<MachineOperand> MovingOps(InsertBefore, operands_end());
 
-  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