[llvm] [BOLT] Optimize basic block loops to avoid n^2 loop (PR #156243)

Paschalis Mpeis via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 8 03:51:14 PDT 2025


================
@@ -3591,6 +3591,18 @@ void BinaryFunction::fixBranches() {
   auto &MIB = BC.MIB;
   MCContext *Ctx = BC.Ctx.get();
 
+  // Caches `FunctionLayout::nextBasicBlock(IgnoreSplits = false)`.
+  // nextBasicBlock uses linear search to find the next block, so the loop
+  // below becomes O(n^2). This avoids that.
+  DenseMap<BinaryBasicBlock *, BinaryBasicBlock *> nextBasicBlock(
+      Layout.block_size());
+  for (size_t i = 0; i + 1 < Layout.block_size(); i++) {
+    auto current = Layout.block_begin() + i;
+    auto next = Layout.block_begin() + i + 1;
----------------
paschalis-mpeis wrote:

Can you pleas capitalize the variable names as per the [Coding Standard](https://llvm.org/docs/CodingStandards.html#name-types-functions-variables-and-enumerators-properly)? 

https://github.com/llvm/llvm-project/pull/156243


More information about the llvm-commits mailing list