[llvm] StructurizeCFG: Optimize phi insertion during ssa reconstruction (PR #101301)

Sameer Sahasrabuddhe via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 1 07:47:31 PDT 2024


================
@@ -710,10 +714,114 @@ void StructurizeCFG::findUndefBlocks(
   }
 }
 
+// Return true if two PHI nodes have compatible incoming values (for each
+// incoming block, either they have the same incoming value or only one PHI
+// node has a incoming value). And return the union of the incoming values
+// through \p Merged.
+bool StructurizeCFG::isCompatible(const BBValueVector &IncomingA,
+                                  const BBValueVector &IncomingB,
+                                  BBValueVector &Merged) {
+  DenseMap<BasicBlock *, Value *> UnionSet;
+  for (auto &V : IncomingA)
+    UnionSet.insert(V);
+
+  for (auto &[BB, V] : IncomingB) {
+    if (UnionSet.contains(BB) && UnionSet[BB] != V)
+      return false;
+    // Either IncomingA does not have this value or IncomingA has the same
+    // value.
+    UnionSet.insert({BB, V});
+  }
+
+  Merged.clear();
+  for (auto &[BB, V] : UnionSet)
----------------
ssahasra wrote:

Can this also be replaced by a single `insert()` that takes begin/end iterators? This time it's in the reverse direction, from map::value_type to vector of pairs, so I am mostly curious if it all works out nicely.

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


More information about the llvm-commits mailing list