[llvm] [SimplifyCFG] Fix hoisting problem in SimplifyCFG (PR #78615)

Quentin Dian via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 21 17:45:39 PST 2024


================
@@ -1671,25 +1671,51 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock *BB,
 
   bool Changed = false;
   auto *SuccIterPairBegin = SuccIterPairs.begin();
-  SuccIterPairBegin++;
+  ++SuccIterPairBegin;
+  auto BBItrPair = *SuccIterPairBegin++;
   auto OtherSuccIterPairRange =
       iterator_range(SuccIterPairBegin, SuccIterPairs.end());
-  auto OtherSuccIterRange = make_first_range(OtherSuccIterPairRange);
-  using InstrFlagPair = std::pair<Instruction *, unsigned>;
-  SmallVector<DenseMap<llvm::hash_code, InstrFlagPair>, 2> OtherSuccessorsHash;
 
-  for (auto BBItrPair : OtherSuccIterRange) {
+  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()) {
+      SmallVector<Instruction *, 2> vec{I};
+      OtherSuccessorsHash[HashValue] = vec;
+      InstToSkipFlag[I] = skipFlag;
+    }
+    BBItrPair.first++;
+  } while (!I->isTerminator());
+
+  unsigned Index = 1;
+  for (auto BBItrPair : OtherSuccIterPairRange) {
     // Fill the hashmap for every other successor
-    DenseMap<llvm::hash_code, InstrFlagPair> hashMap;
     unsigned skipFlag = 0;
     Instruction *I = nullptr;
     do {
-      I = &*BBItrPair;
+      I = &*BBItrPair.first;
+      auto HashValue = getHash(I);
       skipFlag |= skippedInstrFlags(I);
-      hashMap[getHash(I)] = InstrFlagPair(I, skipFlag);
-      BBItrPair++;
+      // For other successors put the instrcution in the map only if there are
+      // instructions with the same hash from other successors and this is the
+      // first instruction with this hash value from current successor.
+      if (OtherSuccessorsHash.find(HashValue) != OtherSuccessorsHash.end() &&
+          OtherSuccessorsHash[HashValue].size() == Index) {
+        OtherSuccessorsHash[HashValue].push_back(I);
----------------
DianQK wrote:

Here, too. This seems to trigger three lookups?

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


More information about the llvm-commits mailing list