-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[StructurizeCFG] Refactor insertConditions. NFC. #115476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[StructurizeCFG] Refactor insertConditions. NFC. #115476
Conversation
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/pr-subscribers-llvm-transforms Author: Jay Foad (jayfoad) ChangesThis just makes it more obvious that having Parent as the single Full diff: https://github.com/llvm/llvm-project/pull/115476.diff 1 Files Affected:
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);
|
…-insertconditions
…-insertconditions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old code is indeed confusing if the block has multiple predecessors. I think this is good to go?
This reverts commit `231e63d8162a1c78a973c6e546bea39d04fefd67` because it was intended to be NFC, but it actually introduces a semantic change. The behavior is not equivalent to the original code. Specifically, the change attempts to make the special case of `Parent` being the only predecessor more explicit by isolating it, and then asserts in the `else` branch that `BB` can't be `Parent`. However, it's still possible for `Preds.size() > 1`, in which case `BB` can be `Parent`, causing the assertion to trigger. Eventually, this introduced a regression (though the related test case wasn't in the repo at the time). To fix it properly, we'd have to reintroduce logic in the `else` block to handle the case where `BB` is `Parent`, which counters the intent of the original refactor. Because of that, I think it's cleaner and more reliable to simply revert the commit. Fixes #126534.
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.