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

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 31 03:02:47 PDT 2024


================
@@ -710,10 +714,107 @@ 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) {
+  MapVector<BasicBlock *, Value *> UnionSet;
+  for (auto &V : IncomingA)
+    UnionSet.insert(V);
+
+  for (auto &V : IncomingB) {
+    if (UnionSet.contains(V.first) && UnionSet[V.first] != V.second)
+      return false;
+    // Either IncomingA does not have this value or IncomingA has the same
+    // value.
+    UnionSet.insert(V);
+  }
+
+  Merged.clear();
+  Merged.append(UnionSet.takeVector());
+  return true;
+}
+
 /// Add the real PHI value as soon as everything is set up
 void StructurizeCFG::setPhiValues() {
   SmallVector<PHINode *, 8> InsertedPhis;
   SSAUpdater Updater(&InsertedPhis);
+
+  DenseMap<PHINode *, std::shared_ptr<BBValueVector>> MergedPHIMap;
----------------
ruiling wrote:

I chose to use shared_ptr because the BBValueVector were shared by different phi nodes, and need to be updated in several rounds. Directly use BBValueVector does not work because we first let `phi1` and `phi2` share the same vector of incoming values, later we may find that we can also let `phi2` and `phi3` share the same set of incoming values. This also means that we need to update the incoming values of `phi1/phi2`. So by using shared_ptr, it's easy to update the incoming values for all the phi nodes (like here phi1&phi2&phi3). unique_ptr seems not work in the case?

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


More information about the llvm-commits mailing list