Skip to content

Commit 84a3be8

Browse files
LebedevRItstellar
authored andcommitted
[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 (cherry picked from commit 909cba9)
1 parent bd8cc85 commit 84a3be8

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
@@ -1094,17 +1094,24 @@ static void CloneInstructionsIntoPredecessorBlockAndUpdateSSAUses(
10941094

10951095
// Update (liveout) uses of bonus instructions,
10961096
// now that the bonus instruction has been cloned into predecessor.
1097-
SSAUpdater SSAUpdate;
1098-
SSAUpdate.Initialize(BonusInst.getType(),
1099-
(NewBonusInst->getName() + ".merge").str());
1100-
SSAUpdate.AddAvailableValue(BB, &BonusInst);
1101-
SSAUpdate.AddAvailableValue(PredBlock, NewBonusInst);
1097+
// Note that we expect to be in a block-closed SSA form for this to work!
11021098
for (Use &U : make_early_inc_range(BonusInst.uses())) {
11031099
auto *UI = cast<Instruction>(U.getUser());
1104-
if (UI->getParent() != PredBlock)
1105-
SSAUpdate.RewriteUseAfterInsertions(U);
1106-
else // Use is in the same block as, and comes before, NewBonusInst.
1107-
SSAUpdate.RewriteUse(U);
1100+
auto *PN = dyn_cast<PHINode>(UI);
1101+
if (!PN) {
1102+
assert(UI->getParent() == BB && BonusInst.comesBefore(UI) &&
1103+
"If the user is not a PHI node, then it should be in the same "
1104+
"block as, and come after, the original bonus instruction.");
1105+
continue; // Keep using the original bonus instruction.
1106+
}
1107+
// Is this the block-closed SSA form PHI node?
1108+
if (PN->getIncomingBlock(U) == BB)
1109+
continue; // Great, keep using the original bonus instruction.
1110+
// The only other alternative is an "use" when coming from
1111+
// the predecessor block - here we should refer to the cloned bonus instr.
1112+
assert(PN->getIncomingBlock(U) == PredBlock &&
1113+
"Not in block-closed SSA form?");
1114+
U.set(NewBonusInst);
11081115
}
11091116
}
11101117
}
@@ -3207,6 +3214,17 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
32073214
// Early exits once we reach the limit.
32083215
if (NumBonusInsts > BonusInstThreshold)
32093216
return false;
3217+
3218+
auto IsBCSSAUse = [BB, &I](Use &U) {
3219+
auto *UI = cast<Instruction>(U.getUser());
3220+
if (auto *PN = dyn_cast<PHINode>(UI))
3221+
return PN->getIncomingBlock(U) == BB;
3222+
return UI->getParent() == BB && I.comesBefore(UI);
3223+
};
3224+
3225+
// Does this instruction require rewriting of uses?
3226+
if (!all_of(I.uses(), IsBCSSAUse))
3227+
return false;
32103228
}
32113229

32123230
// 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, #20] @ 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, #32
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)