[llvm] [VPlan] Remove constant branches early. (PR #183397)
Luke Lau via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 16 02:07:09 PDT 2026
================
@@ -2894,16 +2897,45 @@ void VPlanTransforms::removeBranchOnConst(VPlan &Plan, bool OnlyLatches) {
cast<VPBasicBlock>(VPBB->getSuccessors()[RemovedIdx]);
assert(count(RemovedSucc->getPredecessors(), VPBB) == 1 &&
"There must be a single edge between VPBB and its successor");
- // Values coming from VPBB into phi recipes of RemoveSucc are removed from
+ // Values coming from VPBB into phi recipes of RemovedSucc are removed from
// these recipes.
for (VPRecipeBase &R : RemovedSucc->phis())
cast<VPPhiAccessors>(&R)->removeIncomingValueFor(VPBB);
- // Disconnect blocks and remove the terminator. RemovedSucc will be deleted
- // automatically on VPlan destruction if it becomes unreachable.
+ // Disconnect blocks and remove the terminator.
VPBlockUtils::disconnectBlocks(VPBB, RemovedSucc);
VPBB->back().eraseFromParent();
}
+
+ // Compute which blocks are still reachable from the entry after constant
+ // branch removal.
+ SmallPtrSet<VPBlockBase *, 16> Reachable(
+ llvm::from_range, vp_depth_first_shallow(Plan.getEntry()));
+
+ // Detach all unreachable blocks from their successors, removing incoming
+ // values from phi recipes.
+ SmallVector<VPBasicBlock *> DeadBlocks;
+ for (VPBlockBase *B : AllBlocks) {
+ if (Reachable.contains(B))
+ continue;
+ for (VPBlockBase *Succ : to_vector(B->successors())) {
+ if (auto *SuccBB = dyn_cast<VPBasicBlock>(Succ))
+ for (VPRecipeBase &R : SuccBB->phis())
+ cast<VPPhiAccessors>(&R)->removeIncomingValueFor(B);
+ VPBlockUtils::disconnectBlocks(B, Succ);
+ }
+ append_range(DeadBlocks, VPBlockUtils::blocksOnly<VPBasicBlock>(
+ vp_depth_first_deep(B)));
+ }
+
+ // Erase recipes in dead blocks.
+ VPSymbolicValue Tmp;
+ for (VPBasicBlock *DeadBB : DeadBlocks)
----------------
lukel97 wrote:
Why do we have to do this in a separate loop? Can we not erase the recipes inline in the loop above?
https://github.com/llvm/llvm-project/pull/183397
More information about the llvm-commits
mailing list