Skip to content

[StructurizeCFG] Introduce struct PredInfo. NFC. #115457

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

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 25 additions & 32 deletions llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,12 @@ class CondBranchWeights {
}
};

using ValueWeightPair = std::pair<Value *, MaybeCondBranchWeights>;
struct PredInfo {
Value *Pred;
MaybeCondBranchWeights Weights;
};

using BBPredicates = DenseMap<BasicBlock *, ValueWeightPair>;
using BBPredicates = DenseMap<BasicBlock *, PredInfo>;
using PredMap = DenseMap<BasicBlock *, BBPredicates>;
using BB2BBMap = DenseMap<BasicBlock *, BasicBlock *>;

Expand Down Expand Up @@ -308,7 +311,7 @@ class StructurizeCFG {

void analyzeLoops(RegionNode *N);

ValueWeightPair buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
PredInfo buildCondition(BranchInst *Term, unsigned Idx, bool Invert);

void gatherPredicates(RegionNode *N);

Expand Down Expand Up @@ -490,8 +493,8 @@ void StructurizeCFG::analyzeLoops(RegionNode *N) {
}

/// Build the condition for one edge
ValueWeightPair StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
bool Invert) {
PredInfo StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
bool Invert) {
Value *Cond = Invert ? BoolFalse : BoolTrue;
MaybeCondBranchWeights Weights;

Expand Down Expand Up @@ -624,24 +627,19 @@ void StructurizeCFG::insertConditions(bool Loops) {
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;

PredInfo ParentInfo{nullptr, std::nullopt};
for (auto [BB, PI] : Preds) {
if (BB == Parent) {
ParentValue = Pred;
ParentWeights = Weight;
ParentInfo = PI;
break;
}
PhiInserter.AddAvailableValue(BB, Pred);
PhiInserter.AddAvailableValue(BB, PI.Pred);
Dominator.addAndRememberBlock(BB);
}

if (ParentValue) {
Term->setCondition(ParentValue);
CondBranchWeights::setMetadata(*Term, ParentWeights);
if (ParentInfo.Pred) {
Term->setCondition(ParentInfo.Pred);
CondBranchWeights::setMetadata(*Term, ParentInfo.Weights);
} else {
if (!Dominator.resultIsRememberedBlock())
PhiInserter.AddAvailableValue(Dominator.result(), Default);
Expand All @@ -656,15 +654,14 @@ void StructurizeCFG::simplifyConditions() {
SmallVector<Instruction *> InstToErase;
for (auto &I : concat<PredMap::value_type>(Predicates, LoopPreds)) {
auto &Preds = I.second;
for (auto &J : Preds) {
Value *Cond = J.second.first;
for (auto [BB, PI] : Preds) {
Instruction *Inverted;
if (match(Cond, m_Not(m_OneUse(m_Instruction(Inverted)))) &&
!Cond->use_empty()) {
if (match(PI.Pred, m_Not(m_OneUse(m_Instruction(Inverted)))) &&
!PI.Pred->use_empty()) {
if (auto *InvertedCmp = dyn_cast<CmpInst>(Inverted)) {
InvertedCmp->setPredicate(InvertedCmp->getInversePredicate());
Cond->replaceAllUsesWith(InvertedCmp);
InstToErase.push_back(cast<Instruction>(Cond));
PI.Pred->replaceAllUsesWith(InvertedCmp);
InstToErase.push_back(cast<Instruction>(PI.Pred));
}
}
}
Expand Down Expand Up @@ -1046,10 +1043,9 @@ void StructurizeCFG::setPrevNode(BasicBlock *BB) {
/// Does BB dominate all the predicates of Node?
bool StructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) {
BBPredicates &Preds = Predicates[Node->getEntry()];
return llvm::all_of(Preds,
[&](std::pair<BasicBlock *, ValueWeightPair> Pred) {
return DT->dominates(BB, Pred.first);
});
return llvm::all_of(Preds, [&](std::pair<BasicBlock *, PredInfo> Pred) {
return DT->dominates(BB, Pred.first);
});
}

/// Can we predict that this node will always be called?
Expand All @@ -1061,11 +1057,8 @@ bool StructurizeCFG::isPredictableTrue(RegionNode *Node) {
if (!PrevNode)
return true;

for (std::pair<BasicBlock *, ValueWeightPair> Pred : Preds) {
BasicBlock *BB = Pred.first;
Value *V = Pred.second.first;

if (V != BoolTrue)
for (auto [BB, PI] : Preds) {
if (PI.Pred != BoolTrue)
return false;

if (!Dominated && DT->dominates(BB, PrevNode->getEntry()))
Expand Down
Loading