[llvm] StructurizeCFG: Optimize phi insertion during ssa reconstruction (PR #101301)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 2 09:55:13 PDT 2024
================
@@ -710,10 +717,104 @@ void StructurizeCFG::findUndefBlocks(
}
}
+// 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), let them share the merged incoming values.
+void StructurizeCFG::mergeIfCompatible(PHINode *A, PHINode *B) {
+
+ auto ItA = MergedPHIMap.find(A);
+ auto ItB = MergedPHIMap.find(B);
+ bool FoundA = ItA != MergedPHIMap.end();
+ bool FoundB = ItB != MergedPHIMap.end();
+
+ // Skip phis that were both already merged with others.
+ if (FoundA && FoundB)
+ return;
+
+ const auto &IncomingA =
+ FoundA ? BBValuesPool[ItA->second] : DeletedPhis[A->getParent()][A];
+ const auto &IncomingB =
+ FoundB ? BBValuesPool[ItB->second] : DeletedPhis[B->getParent()][B];
+
+ DenseMap<BasicBlock *, Value *> Mergeable(IncomingA.begin(), IncomingA.end());
+ for (auto &[BB, V] : IncomingB) {
+ if (Mergeable.contains(BB) && Mergeable[BB] != V)
+ return;
+ // Either IncomingA does not have this value or IncomingA has the same
+ // value.
+ Mergeable.insert({BB, V});
+ }
+
+ unsigned PoolIndex;
+
+ if (FoundA || FoundB) {
+ PoolIndex = FoundA ? ItA->second : ItB->second;
+ BBValuesPool[PoolIndex].clear();
+ BBValuesPool[PoolIndex].append(Mergeable.begin(), Mergeable.end());
+ } else {
+ PoolIndex = BBValuesPool.size();
+ BBValuesPool.emplace_back(Mergeable.begin(), Mergeable.end());
+ }
+
+ // Skip insertion if Phi was already merged with other phi node.
+ if (!FoundA)
+ MergedPHIMap.insert({A, PoolIndex});
+
+ if (!FoundB)
+ MergedPHIMap.insert({B, PoolIndex});
+
+ return;
+}
+
/// Add the real PHI value as soon as everything is set up
void StructurizeCFG::setPhiValues() {
SmallVector<PHINode *, 8> InsertedPhis;
SSAUpdater Updater(&InsertedPhis);
+
+ // Find out phi nodes that have compatible incoming values (either they have
----------------
arsenm wrote:
```suggestion
// Find phi nodes that have compatible incoming values (either they have
```
https://github.com/llvm/llvm-project/pull/101301
More information about the llvm-commits
mailing list