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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 20 08:35:46 PDT 2024


================
@@ -1635,87 +1675,149 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock *BB,
   // many instructions we skip, serving as a compilation time control as well as
   // preventing excessive increase of life ranges.
   unsigned NumSkipped = 0;
-  // 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 (SuccIterPairs.size() > 2) {
-    erase_if(SuccIterPairs,
-             [](const auto &Pair) { return isa<UnreachableInst>(Pair.first); });
-    if (SuccIterPairs.size() < 2)
-      return false;
-  }
 
   bool Changed = false;
+  auto *SuccIterPairBegin = SuccIterPairs.begin();
+  ++SuccIterPairBegin;
+  auto BBItrPair = *SuccIterPairBegin++;
+  auto OtherSuccIterPairRange =
+      iterator_range(SuccIterPairBegin, SuccIterPairs.end());
+
+  DenseMap<llvm::hash_code, SmallVector<Instruction *, 2>> OtherSuccessorsHash;
+  DenseMap<Instruction *, unsigned> InstToSkipFlag;
 
+  unsigned skipFlag = 0;
+  Instruction *I = nullptr;
+  do {
+    I = &*BBItrPair.first;
+    auto HashValue = getHash(I);
+    skipFlag |= skippedInstrFlags(I);
+    // For the first successor we created hashmap for, put all instructions
+    // in the hashmap execept for the ones that have the same hash as some
+    // previous instruction in that BB.
+    if (OtherSuccessorsHash.find(HashValue) == OtherSuccessorsHash.end()) {
+      OtherSuccessorsHash[HashValue] = {I};
----------------
nikic wrote:

You can use try_emplace to avoid duplicate hash lookup.

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


More information about the llvm-commits mailing list