Skip to content

Commit 909cba9

Browse files
committed
[SimplifyCFG] performBranchToCommonDestFolding(): require block-closed SSA form for bonus instructions (PR51125)
I can't seem to wrap my head around the proper fix here, we should be fine without this requirement, iff we can form this form, but the naive attempt (https://reviews.llvm.org/D106317) has failed. So just to unblock the release, put up a restriction. Fixes https://bugs.llvm.org/show_bug.cgi?id=51125
1 parent 8ba2adc commit 909cba9

File tree

7 files changed

+240
-212
lines changed

7 files changed

+240
-212
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,17 +1095,24 @@ static void CloneInstructionsIntoPredecessorBlockAndUpdateSSAUses(
10951095

10961096
// Update (liveout) uses of bonus instructions,
10971097
// now that the bonus instruction has been cloned into predecessor.
1098-
SSAUpdater SSAUpdate;
1099-
SSAUpdate.Initialize(BonusInst.getType(),
1100-
(NewBonusInst->getName() + ".merge").str());
1101-
SSAUpdate.AddAvailableValue(BB, &BonusInst);
1102-
SSAUpdate.AddAvailableValue(PredBlock, NewBonusInst);
1098+
// Note that we expect to be in a block-closed SSA form for this to work!
11031099
for (Use &U : make_early_inc_range(BonusInst.uses())) {
11041100
auto *UI = cast<Instruction>(U.getUser());
1105-
if (UI->getParent() != PredBlock)
1106-
SSAUpdate.RewriteUseAfterInsertions(U);
1107-
else // Use is in the same block as, and comes before, NewBonusInst.
1108-
SSAUpdate.RewriteUse(U);
1101+
auto *PN = dyn_cast<PHINode>(UI);
1102+
if (!PN) {
1103+
assert(UI->getParent() == BB && BonusInst.comesBefore(UI) &&
1104+
"If the user is not a PHI node, then it should be in the same "
1105+
"block as, and come after, the original bonus instruction.");
1106+
continue; // Keep using the original bonus instruction.
1107+
}
1108+
// Is this the block-closed SSA form PHI node?
1109+
if (PN->getIncomingBlock(U) == BB)
1110+
continue; // Great, keep using the original bonus instruction.
1111+
// The only other alternative is an "use" when coming from
1112+
// the predecessor block - here we should refer to the cloned bonus instr.
1113+
assert(PN->getIncomingBlock(U) == PredBlock &&
1114+
"Not in block-closed SSA form?");
1115+
U.set(NewBonusInst);
11091116
}
11101117
}
11111118
}
@@ -3239,6 +3246,17 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
32393246
// Early exits once we reach the limit.
32403247
if (NumBonusInsts > BonusInstThreshold)
32413248
return false;
3249+
3250+
auto IsBCSSAUse = [BB, &I](Use &U) {
3251+
auto *UI = cast<Instruction>(U.getUser());
3252+
if (auto *PN = dyn_cast<PHINode>(UI))
3253+
return PN->getIncomingBlock(U) == BB;
3254+
return UI->getParent() == BB && I.comesBefore(UI);
3255+
};
3256+
3257+
// Does this instruction require rewriting of uses?
3258+
if (!all_of(I.uses(), IsBCSSAUse))
3259+
return false;
32423260
}
32433261

32443262
// Ok, we have the budget. Perform the transformation.

llvm/test/CodeGen/Thumb2/mve-float16regloops.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ define void @fir(%struct.arm_fir_instance_f32* nocapture readonly %S, half* noca
10541054
; CHECK-NEXT: cmp r3, #8
10551055
; CHECK-NEXT: str r1, [sp, #16] @ 4-byte Spill
10561056
; CHECK-NEXT: blo.w .LBB16_12
1057-
; CHECK-NEXT: @ %bb.1: @ %entry
1057+
; CHECK-NEXT: @ %bb.1: @ %if.then
10581058
; CHECK-NEXT: lsrs.w r12, r3, #2
10591059
; CHECK-NEXT: beq.w .LBB16_12
10601060
; CHECK-NEXT: @ %bb.2: @ %while.body.lr.ph

llvm/test/CodeGen/Thumb2/mve-float32regloops.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ define void @fir(%struct.arm_fir_instance_f32* nocapture readonly %S, float* noc
10481048
; CHECK-NEXT: sub sp, #24
10491049
; CHECK-NEXT: cmp r3, #8
10501050
; CHECK-NEXT: blo.w .LBB16_12
1051-
; CHECK-NEXT: @ %bb.1: @ %entry
1051+
; CHECK-NEXT: @ %bb.1: @ %if.then
10521052
; CHECK-NEXT: lsrs.w r12, r3, #2
10531053
; CHECK-NEXT: beq.w .LBB16_12
10541054
; CHECK-NEXT: @ %bb.2: @ %while.body.lr.ph

0 commit comments

Comments
 (0)