[llvm] [StructurizeCFG] Refactor insertConditions. NFC. (PR #115476)
Jay Foad via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 8 10:07:34 PST 2024
https://github.com/jayfoad updated https://github.com/llvm/llvm-project/pull/115476
>From 994ddbb08d5189d89c008a5141ba6be21d438eac Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Thu, 7 Nov 2024 17:47:48 +0000
Subject: [PATCH] [StructurizeCFG] Refactor insertConditions. NFC.
This just makes it more obvious that having Parent as the single
predecessor is a special case, instead of checking for it in the middle
of a loop that finds the nearest common dominator of multiple
predecessors.
---
llvm/lib/Transforms/Scalar/StructurizeCFG.cpp | 39 ++++++++-----------
1 file changed, 17 insertions(+), 22 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp b/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
index 2a0f0423bbc8f6..1cc1bb6a4e04f2 100644
--- a/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
+++ b/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
@@ -615,34 +615,29 @@ void StructurizeCFG::insertConditions(bool Loops) {
BasicBlock *SuccTrue = Term->getSuccessor(0);
BasicBlock *SuccFalse = Term->getSuccessor(1);
- PhiInserter.Initialize(Boolean, "");
- PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
- PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
-
BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
- NearestCommonDominator Dominator(DT);
- Dominator.addBlock(Parent);
+ if (Preds.size() == 1 && Preds.begin()->first == Parent) {
+ auto [Pred, Weight] = Preds.begin()->second;
+ Term->setCondition(Pred);
+ CondBranchWeights::setMetadata(*Term, Weight);
+ } else {
+ PhiInserter.Initialize(Boolean, "");
+ PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
+ PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
+
+ NearestCommonDominator Dominator(DT);
+ Dominator.addBlock(Parent);
- Value *ParentValue = nullptr;
- MaybeCondBranchWeights ParentWeights = std::nullopt;
- for (std::pair<BasicBlock *, ValueWeightPair> BBAndPred : Preds) {
- BasicBlock *BB = BBAndPred.first;
- auto [Pred, Weight] = BBAndPred.second;
+ for (std::pair<BasicBlock *, ValueWeightPair> BBAndPred : Preds) {
+ BasicBlock *BB = BBAndPred.first;
+ auto [Pred, Weight] = BBAndPred.second;
- if (BB == Parent) {
- ParentValue = Pred;
- ParentWeights = Weight;
- break;
+ assert(BB != Parent);
+ PhiInserter.AddAvailableValue(BB, Pred);
+ Dominator.addAndRememberBlock(BB);
}
- PhiInserter.AddAvailableValue(BB, Pred);
- Dominator.addAndRememberBlock(BB);
- }
- if (ParentValue) {
- Term->setCondition(ParentValue);
- CondBranchWeights::setMetadata(*Term, ParentWeights);
- } else {
if (!Dominator.resultIsRememberedBlock())
PhiInserter.AddAvailableValue(Dominator.result(), Default);
More information about the llvm-commits
mailing list