Skip to content

Commit 22a02b6

Browse files
authored
[DFAJumpThreading] Unify equivalent states (#162447)
DFAJumpThreading determines the switch destination of a threadable path based on its next state and cannot reuse cloned blocks if their destinations differ. However, different states may lead to the same destination. This patch unifies equivalent states, thereby avoiding redundant duplication of cloned blocks.
1 parent 0f3d0cf commit 22a02b6

File tree

3 files changed

+377
-84
lines changed

3 files changed

+377
-84
lines changed

llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp

Lines changed: 85 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,22 @@ inline raw_ostream &operator<<(raw_ostream &OS, const PathType &Path) {
389389
return OS;
390390
}
391391

392+
/// Helper to get the successor corresponding to a particular case value for
393+
/// a switch statement.
394+
static BasicBlock *getNextCaseSuccessor(SwitchInst *Switch,
395+
const APInt &NextState) {
396+
BasicBlock *NextCase = nullptr;
397+
for (auto Case : Switch->cases()) {
398+
if (Case.getCaseValue()->getValue() == NextState) {
399+
NextCase = Case.getCaseSuccessor();
400+
break;
401+
}
402+
}
403+
if (!NextCase)
404+
NextCase = Switch->getDefaultDest();
405+
return NextCase;
406+
}
407+
392408
namespace {
393409
/// ThreadingPath is a path in the control flow of a loop that can be threaded
394410
/// by cloning necessary basic blocks and replacing conditional branches with
@@ -401,6 +417,10 @@ struct ThreadingPath {
401417
ExitVal = V->getValue();
402418
IsExitValSet = true;
403419
}
420+
void setExitValue(const APInt &V) {
421+
ExitVal = V;
422+
IsExitValSet = true;
423+
}
404424
bool isExitValueSet() const { return IsExitValSet; }
405425

406426
/// Determinator is the basic block that determines the next state of the DFA.
@@ -583,44 +603,8 @@ struct AllSwitchPaths {
583603
BasicBlock *getSwitchBlock() { return SwitchBlock; }
584604

585605
void run() {
586-
StateDefMap StateDef = getStateDefMap();
587-
if (StateDef.empty()) {
588-
ORE->emit([&]() {
589-
return OptimizationRemarkMissed(DEBUG_TYPE, "SwitchNotPredictable",
590-
Switch)
591-
<< "Switch instruction is not predictable.";
592-
});
593-
return;
594-
}
595-
596-
auto *SwitchPhi = cast<PHINode>(Switch->getOperand(0));
597-
auto *SwitchPhiDefBB = SwitchPhi->getParent();
598-
VisitedBlocks VB;
599-
// Get paths from the determinator BBs to SwitchPhiDefBB
600-
std::vector<ThreadingPath> PathsToPhiDef =
601-
getPathsFromStateDefMap(StateDef, SwitchPhi, VB, MaxNumPaths);
602-
if (SwitchPhiDefBB == SwitchBlock || PathsToPhiDef.empty()) {
603-
TPaths = std::move(PathsToPhiDef);
604-
return;
605-
}
606-
607-
assert(MaxNumPaths >= PathsToPhiDef.size() && !PathsToPhiDef.empty());
608-
auto PathsLimit = MaxNumPaths / PathsToPhiDef.size();
609-
// Find and append paths from SwitchPhiDefBB to SwitchBlock.
610-
PathsType PathsToSwitchBB =
611-
paths(SwitchPhiDefBB, SwitchBlock, VB, /* PathDepth = */ 1, PathsLimit);
612-
if (PathsToSwitchBB.empty())
613-
return;
614-
615-
std::vector<ThreadingPath> TempList;
616-
for (const ThreadingPath &Path : PathsToPhiDef) {
617-
for (const PathType &PathToSw : PathsToSwitchBB) {
618-
ThreadingPath PathCopy(Path);
619-
PathCopy.appendExcludingFirst(PathToSw);
620-
TempList.push_back(PathCopy);
621-
}
622-
}
623-
TPaths = std::move(TempList);
606+
findTPaths();
607+
unifyTPaths();
624608
}
625609

626610
private:
@@ -812,6 +796,69 @@ struct AllSwitchPaths {
812796
return Res;
813797
}
814798

799+
// Find all threadable paths.
800+
void findTPaths() {
801+
StateDefMap StateDef = getStateDefMap();
802+
if (StateDef.empty()) {
803+
ORE->emit([&]() {
804+
return OptimizationRemarkMissed(DEBUG_TYPE, "SwitchNotPredictable",
805+
Switch)
806+
<< "Switch instruction is not predictable.";
807+
});
808+
return;
809+
}
810+
811+
auto *SwitchPhi = cast<PHINode>(Switch->getOperand(0));
812+
auto *SwitchPhiDefBB = SwitchPhi->getParent();
813+
VisitedBlocks VB;
814+
// Get paths from the determinator BBs to SwitchPhiDefBB
815+
std::vector<ThreadingPath> PathsToPhiDef =
816+
getPathsFromStateDefMap(StateDef, SwitchPhi, VB, MaxNumPaths);
817+
if (SwitchPhiDefBB == SwitchBlock || PathsToPhiDef.empty()) {
818+
TPaths = std::move(PathsToPhiDef);
819+
return;
820+
}
821+
822+
assert(MaxNumPaths >= PathsToPhiDef.size() && !PathsToPhiDef.empty());
823+
auto PathsLimit = MaxNumPaths / PathsToPhiDef.size();
824+
// Find and append paths from SwitchPhiDefBB to SwitchBlock.
825+
PathsType PathsToSwitchBB =
826+
paths(SwitchPhiDefBB, SwitchBlock, VB, /* PathDepth = */ 1, PathsLimit);
827+
if (PathsToSwitchBB.empty())
828+
return;
829+
830+
std::vector<ThreadingPath> TempList;
831+
for (const ThreadingPath &Path : PathsToPhiDef) {
832+
for (const PathType &PathToSw : PathsToSwitchBB) {
833+
ThreadingPath PathCopy(Path);
834+
PathCopy.appendExcludingFirst(PathToSw);
835+
TempList.push_back(PathCopy);
836+
}
837+
}
838+
TPaths = std::move(TempList);
839+
}
840+
841+
// Two states are equivalent if they have the same switch destination.
842+
// Unify the states in different threading path if the states are equivalent.
843+
void unifyTPaths() {
844+
llvm::SmallDenseMap<BasicBlock *, APInt> DestToState;
845+
for (ThreadingPath &Path : TPaths) {
846+
APInt NextState = Path.getExitValue();
847+
BasicBlock *Dest = getNextCaseSuccessor(Switch, NextState);
848+
auto StateIt = DestToState.find(Dest);
849+
if (StateIt == DestToState.end()) {
850+
DestToState.insert({Dest, NextState});
851+
continue;
852+
}
853+
854+
if (NextState != StateIt->second) {
855+
LLVM_DEBUG(dbgs() << "Next state in " << Path << " is equivalent to "
856+
<< StateIt->second << "\n");
857+
Path.setExitValue(StateIt->second);
858+
}
859+
}
860+
}
861+
815862
unsigned NumVisited = 0;
816863
SwitchInst *Switch;
817864
BasicBlock *SwitchBlock;
@@ -1335,21 +1382,6 @@ struct TransformDFA {
13351382
return It != ClonedBBs.end() ? (*It).BB : nullptr;
13361383
}
13371384

1338-
/// Helper to get the successor corresponding to a particular case value for
1339-
/// a switch statement.
1340-
BasicBlock *getNextCaseSuccessor(SwitchInst *Switch, const APInt &NextState) {
1341-
BasicBlock *NextCase = nullptr;
1342-
for (auto Case : Switch->cases()) {
1343-
if (Case.getCaseValue()->getValue() == NextState) {
1344-
NextCase = Case.getCaseSuccessor();
1345-
break;
1346-
}
1347-
}
1348-
if (!NextCase)
1349-
NextCase = Switch->getDefaultDest();
1350-
return NextCase;
1351-
}
1352-
13531385
/// Returns true if IncomingBB is a predecessor of BB.
13541386
bool isPredecessor(BasicBlock *BB, BasicBlock *IncomingBB) {
13551387
return llvm::is_contained(predecessors(BB), IncomingBB);

llvm/test/Transforms/DFAJumpThreading/dfa-unfold-select.ll

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,6 @@ define i32 @test3(i32 %num) {
227227
; CHECK-NEXT: i32 1, label [[CASE1:%.*]]
228228
; CHECK-NEXT: i32 2, label [[CASE2:%.*]]
229229
; CHECK-NEXT: ]
230-
; CHECK: for.body.jt4:
231-
; CHECK-NEXT: [[COUNT_JT4:%.*]] = phi i32 [ [[INC_JT4:%.*]], [[FOR_INC_JT4:%.*]] ]
232-
; CHECK-NEXT: [[STATE_JT4:%.*]] = phi i32 [ [[STATE_NEXT_JT4:%.*]], [[FOR_INC_JT4]] ]
233-
; CHECK-NEXT: br label [[FOR_INC_JT1]]
234230
; CHECK: for.body.jt3:
235231
; CHECK-NEXT: [[COUNT_JT3:%.*]] = phi i32 [ [[INC_JT3:%.*]], [[FOR_INC_JT3:%.*]] ]
236232
; CHECK-NEXT: [[STATE_JT3:%.*]] = phi i32 [ [[STATE_NEXT_JT3:%.*]], [[FOR_INC_JT3]] ]
@@ -261,17 +257,14 @@ define i32 @test3(i32 %num) {
261257
; CHECK: sel.2.si.unfold.false:
262258
; CHECK-NEXT: [[DOTSI_UNFOLD_PHI1:%.*]] = phi i32 [ 4, [[SEL_2_SI_UNFOLD_TRUE_JT3]] ]
263259
; CHECK-NEXT: br label [[SEL_3_SI_UNFOLD_FALSE]]
264-
; CHECK: sel.2.si.unfold.false.jt4:
260+
; CHECK: sel.2.si.unfold.false.jt3:
265261
; CHECK-NEXT: [[DOTSI_UNFOLD_PHI1_JT4:%.*]] = phi i32 [ 4, [[SEL_2_SI_UNFOLD_TRUE:%.*]] ]
266-
; CHECK-NEXT: br label [[SEL_3_SI_UNFOLD_FALSE_JT4:%.*]]
262+
; CHECK-NEXT: br label [[SEL_3_SI_UNFOLD_FALSE_JT3]]
267263
; CHECK: sel.3.si.unfold.false:
268264
; CHECK-NEXT: [[SEL_2_SI_UNFOLD_PHI:%.*]] = phi i32 [ poison, [[SEL_2_SI_UNFOLD_TRUE]] ], [ [[DOTSI_UNFOLD_PHI1]], [[SEL_2_SI_UNFOLD_FALSE]] ]
269265
; CHECK-NEXT: br label [[FOR_INC]]
270-
; CHECK: sel.3.si.unfold.false.jt4:
271-
; CHECK-NEXT: [[SEL_2_SI_UNFOLD_PHI_JT4:%.*]] = phi i32 [ [[DOTSI_UNFOLD_PHI1_JT4]], [[SEL_2_SI_UNFOLD_FALSE_JT4]] ]
272-
; CHECK-NEXT: br label [[FOR_INC_JT4]]
273266
; CHECK: sel.3.si.unfold.false.jt3:
274-
; CHECK-NEXT: [[SEL_2_SI_UNFOLD_PHI_JT3:%.*]] = phi i32 [ [[DOTSI_UNFOLD_PHI_JT3]], [[SEL_2_SI_UNFOLD_TRUE_JT3]] ]
267+
; CHECK-NEXT: [[SEL_2_SI_UNFOLD_PHI_JT3:%.*]] = phi i32 [ [[DOTSI_UNFOLD_PHI_JT3]], [[SEL_2_SI_UNFOLD_TRUE_JT3]] ], [ [[DOTSI_UNFOLD_PHI1_JT4]], [[SEL_2_SI_UNFOLD_FALSE_JT4]] ]
275268
; CHECK-NEXT: br label [[FOR_INC_JT3]]
276269
; CHECK: sel.1.si.unfold.true:
277270
; CHECK-NEXT: br i1 [[CMP_1]], label [[FOR_INC]], label [[SEL_1_SI_UNFOLD_FALSE_JT2:%.*]]
@@ -289,11 +282,6 @@ define i32 @test3(i32 %num) {
289282
; CHECK-NEXT: [[INC]] = add nsw i32 [[COUNT5]], 1
290283
; CHECK-NEXT: [[CMP_EXIT:%.*]] = icmp slt i32 [[INC]], [[NUM:%.*]]
291284
; CHECK-NEXT: br i1 [[CMP_EXIT]], label [[FOR_BODY]], label [[FOR_END:%.*]]
292-
; CHECK: for.inc.jt4:
293-
; CHECK-NEXT: [[STATE_NEXT_JT4]] = phi i32 [ [[SEL_2_SI_UNFOLD_PHI_JT4]], [[SEL_3_SI_UNFOLD_FALSE_JT4]] ]
294-
; CHECK-NEXT: [[INC_JT4]] = add nsw i32 undef, 1
295-
; CHECK-NEXT: [[CMP_EXIT_JT4:%.*]] = icmp slt i32 [[INC_JT4]], [[NUM]]
296-
; CHECK-NEXT: br i1 [[CMP_EXIT_JT4]], label [[FOR_BODY_JT4:%.*]], label [[FOR_END]]
297285
; CHECK: for.inc.jt3:
298286
; CHECK-NEXT: [[STATE_NEXT_JT3]] = phi i32 [ [[SEL_2_SI_UNFOLD_PHI_JT3]], [[SEL_3_SI_UNFOLD_FALSE_JT3]] ]
299287
; CHECK-NEXT: [[INC_JT3]] = add nsw i32 [[COUNT5]], 1
@@ -305,8 +293,8 @@ define i32 @test3(i32 %num) {
305293
; CHECK-NEXT: [[CMP_EXIT_JT2:%.*]] = icmp slt i32 [[INC_JT2]], [[NUM]]
306294
; CHECK-NEXT: br i1 [[CMP_EXIT_JT2]], label [[FOR_BODY_JT2]], label [[FOR_END]]
307295
; CHECK: for.inc.jt1:
308-
; CHECK-NEXT: [[COUNT4:%.*]] = phi i32 [ [[COUNT_JT4]], [[FOR_BODY_JT4]] ], [ [[COUNT_JT3]], [[FOR_BODY_JT3]] ], [ [[COUNT5]], [[SEL_1_SI_UNFOLD_TRUE_JT1]] ], [ [[COUNT]], [[FOR_BODY]] ]
309-
; CHECK-NEXT: [[STATE_NEXT_JT1]] = phi i32 [ 1, [[FOR_BODY]] ], [ 1, [[FOR_BODY_JT3]] ], [ 1, [[FOR_BODY_JT4]] ], [ [[DOTSI_UNFOLD_PHI2_JT1]], [[SEL_1_SI_UNFOLD_TRUE_JT1]] ]
296+
; CHECK-NEXT: [[COUNT4:%.*]] = phi i32 [ [[COUNT_JT3]], [[FOR_BODY_JT3]] ], [ [[COUNT5]], [[SEL_1_SI_UNFOLD_TRUE_JT1]] ], [ [[COUNT]], [[FOR_BODY]] ]
297+
; CHECK-NEXT: [[STATE_NEXT_JT1]] = phi i32 [ 1, [[FOR_BODY]] ], [ 1, [[FOR_BODY_JT3]] ], [ [[DOTSI_UNFOLD_PHI2_JT1]], [[SEL_1_SI_UNFOLD_TRUE_JT1]] ]
310298
; CHECK-NEXT: [[INC_JT1]] = add nsw i32 [[COUNT4]], 1
311299
; CHECK-NEXT: [[CMP_EXIT_JT1:%.*]] = icmp slt i32 [[INC_JT1]], [[NUM]]
312300
; CHECK-NEXT: br i1 [[CMP_EXIT_JT1]], label [[FOR_BODY_JT1]], label [[FOR_END]]
@@ -402,36 +390,28 @@ define void @pr65222(i32 %flags, i1 %cmp, i1 %tobool.not) {
402390
; CHECK-NEXT: br label [[IF_END_JT2:%.*]]
403391
; CHECK: cond1.si.unfold.true:
404392
; CHECK-NEXT: br i1 [[CMP]], label [[IF_END]], label [[COND1_SI_UNFOLD_FALSE_JT1:%.*]]
405-
; CHECK: cond1.si.unfold.true.jt3:
393+
; CHECK: cond1.si.unfold.true.jt2:
406394
; CHECK-NEXT: [[DOTSI_UNFOLD_PHI2:%.*]] = phi i32 [ 3, [[THEN]] ]
407-
; CHECK-NEXT: br i1 [[CMP]], label [[IF_END_JT3:%.*]], label [[COND1_SI_UNFOLD_FALSE:%.*]]
395+
; CHECK-NEXT: br i1 [[CMP]], label [[IF_END_JT2]], label [[COND1_SI_UNFOLD_FALSE:%.*]]
408396
; CHECK: cond1.si.unfold.false:
409397
; CHECK-NEXT: [[DOTSI_UNFOLD_PHI3:%.*]] = phi i32 [ 1, [[COND1_SI_UNFOLD_TRUE]] ]
410398
; CHECK-NEXT: br label [[IF_END]]
411-
; CHECK: cond1.si.unfold.false.jt1:
399+
; CHECK: cond1.si.unfold.false.jt2:
412400
; CHECK-NEXT: [[DOTSI_UNFOLD_PHI3_JT1:%.*]] = phi i32 [ 1, [[COND1_SI_UNFOLD_TRUE1:%.*]] ]
413-
; CHECK-NEXT: br label [[IF_END_JT1:%.*]]
401+
; CHECK-NEXT: br label [[IF_END_JT2]]
414402
; CHECK: if.end:
415403
; CHECK-NEXT: [[UNFOLDED:%.*]] = phi i32 [ [[FLAGS:%.*]], [[WHILE_COND]] ], [ [[COND_SI_UNFOLD_PHI]], [[TOUNFOLD_SI_UNFOLD_FALSE1]] ], [ poison, [[COND1_SI_UNFOLD_TRUE1]] ], [ [[DOTSI_UNFOLD_PHI3]], [[COND1_SI_UNFOLD_FALSE]] ]
416404
; CHECK-NEXT: [[OTHER:%.*]] = phi i32 [ [[FLAGS]], [[WHILE_COND]] ], [ 0, [[TOUNFOLD_SI_UNFOLD_FALSE1]] ], [ 0, [[COND1_SI_UNFOLD_TRUE1]] ], [ 0, [[COND1_SI_UNFOLD_FALSE]] ]
417405
; CHECK-NEXT: switch i32 [[UNFOLDED]], label [[UNREACHABLE:%.*]] [
418406
; CHECK-NEXT: i32 0, label [[SW_BB:%.*]]
419407
; CHECK-NEXT: ]
420-
; CHECK: if.end.jt1:
421-
; CHECK-NEXT: [[UNFOLDED_JT1:%.*]] = phi i32 [ [[DOTSI_UNFOLD_PHI3_JT1]], [[COND1_SI_UNFOLD_FALSE_JT1]] ]
422-
; CHECK-NEXT: [[OTHER_JT1:%.*]] = phi i32 [ 0, [[COND1_SI_UNFOLD_FALSE_JT1]] ]
423-
; CHECK-NEXT: br label [[UNREACHABLE]]
424-
; CHECK: if.end.jt3:
425-
; CHECK-NEXT: [[UNFOLDED_JT3:%.*]] = phi i32 [ [[DOTSI_UNFOLD_PHI2]], [[COND1_SI_UNFOLD_TRUE]] ]
426-
; CHECK-NEXT: [[OTHER_JT3:%.*]] = phi i32 [ 0, [[COND1_SI_UNFOLD_TRUE]] ]
427-
; CHECK-NEXT: br label [[UNREACHABLE]]
428408
; CHECK: if.end.jt0:
429409
; CHECK-NEXT: [[UNFOLDED_JT0:%.*]] = phi i32 [ [[COND_SI_UNFOLD_PHI_JT0]], [[TOUNFOLD_SI_UNFOLD_FALSE_JT0]] ]
430410
; CHECK-NEXT: [[OTHER_JT0:%.*]] = phi i32 [ 0, [[TOUNFOLD_SI_UNFOLD_FALSE_JT0]] ]
431411
; CHECK-NEXT: br label [[SW_BB]]
432412
; CHECK: if.end.jt2:
433-
; CHECK-NEXT: [[UNFOLDED_JT2:%.*]] = phi i32 [ [[COND_SI_UNFOLD_PHI_JT2]], [[TOUNFOLD_SI_UNFOLD_FALSE]] ]
434-
; CHECK-NEXT: [[OTHER_JT2:%.*]] = phi i32 [ 0, [[TOUNFOLD_SI_UNFOLD_FALSE]] ]
413+
; CHECK-NEXT: [[UNFOLDED_JT2:%.*]] = phi i32 [ [[COND_SI_UNFOLD_PHI_JT2]], [[TOUNFOLD_SI_UNFOLD_FALSE]] ], [ [[DOTSI_UNFOLD_PHI2]], [[COND1_SI_UNFOLD_TRUE]] ], [ [[DOTSI_UNFOLD_PHI3_JT1]], [[COND1_SI_UNFOLD_FALSE_JT1]] ]
414+
; CHECK-NEXT: [[OTHER_JT2:%.*]] = phi i32 [ 0, [[TOUNFOLD_SI_UNFOLD_FALSE]] ], [ 0, [[COND1_SI_UNFOLD_TRUE]] ], [ 0, [[COND1_SI_UNFOLD_FALSE_JT1]] ]
435415
; CHECK-NEXT: br label [[UNREACHABLE]]
436416
; CHECK: unreachable:
437417
; CHECK-NEXT: unreachable

0 commit comments

Comments
 (0)