[llvm] [InstCombine] Extend folding of aggregate construction to cases when source aggregates are partially available (PR #100828)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 19 02:55:59 PST 2024
================
@@ -1117,9 +1124,68 @@ Instruction *InstCombinerImpl::foldAggregateConstructionIntoAggregateReuse(
// aggregate produced by OrigIVI must have been originally extracted from
// the same aggregate. Is that so? Can we find said original aggregate?
SourceAggregate = FindCommonSourceAggregate(UseBB, Pred);
- if (Describe(SourceAggregate) != AggregateDescription::Found)
- return nullptr; // Give up.
- IV.first->second = *SourceAggregate;
+ if (Describe(SourceAggregate) == AggregateDescription::Found) {
+ FoundSrcAgg = true;
+ IV.first->second = *SourceAggregate;
+ } else {
+ // If UseBB is the single successor of Pred, we can add InsertValue to
+ // Pred.
+ auto *BI = dyn_cast<BranchInst>(Pred->getTerminator());
+ if (!BI || !BI->isUnconditional())
+ return nullptr;
+ }
+ }
+
+ if (!FoundSrcAgg)
+ return nullptr;
+
+ // Do some sanity check if we need to add insertvalue into predecessors.
+ auto OrigBB = OrigIVI.getParent();
+ for (auto &It : SourceAggregates) {
+ if (Describe(It.second) == AggregateDescription::Found)
+ continue;
+
+ // Element is defined in UseBB, so it can't be used in predecessors.
+ if (EltDefinedInUseBB)
+ return nullptr;
+
+ // Do this transformation cross loop boundary may cause dead loop. So we
+ // should avoid this situation. But LoopInfo is not generally available, we
+ // must be conservative here.
+ // If OrigIVI is in UseBB and it's the only successor of PredBB, PredBB
+ // can't be in inner loop.
+ if (UseBB != OrigBB)
+ return nullptr;
----------------
nikic wrote:
I have a hard time understanding why this check is sufficient to prevent cycles in all cases, or even how the UseBB != OrigBB condition is related. I guess this is supposed to check the case where OrigBB is a loop header with phis and UseBB is an exit block with inserts -- but couldn't it fail on non-sunk inserts?
https://github.com/llvm/llvm-project/pull/100828
More information about the llvm-commits
mailing list