[llvm] [SimplifyCFG] Use hash map to continue hoisting the common instructions (PR #78615)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 9 04:10:56 PST 2024


================
@@ -1608,16 +1618,39 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock *BB,
 
   auto *TI = BB->getTerminator();
 
+  SmallVector<BasicBlock *, 8> SuccessorBBs;
+  for (auto *Succ : successors(BB)) {
+    BasicBlock::iterator SuccItr = Succ->begin();
+    // If we find an unreachable instruction at the beginning of a basic block,
+    // we can still hoist instructions from the rest of the basic blocks.
+    if (isa<UnreachableInst>(*SuccItr))
+      continue;
+    SuccessorBBs.push_back(Succ);
+  }
+
+  // Find the smallest BB because we always want to iterate over instructions
+  // of the smallest Successor.
+  auto *SmallestBB = *std::min_element(SuccessorBBs.begin(), SuccessorBBs.end(),
+                                       [](BasicBlock *BB1, BasicBlock *BB2) {
+                                         return BB1->size() < BB2->size();
+                                       });
+  std::iter_swap(
+      SuccessorBBs.begin(),
+      std::find(SuccessorBBs.begin(), SuccessorBBs.end(), SmallestBB));
+
----------------
dtcxzyw wrote:

Should we set a limit on the size of `SmallestBB`? It should be a bit larger than `HoistCommonSkipLimit`.


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


More information about the llvm-commits mailing list