Skip to content

Commit 6b53ada

Browse files
authored
[DFAJumpThreading] Early exit if switch is not in a loop (#85360)
This patch prevents taking non-loop switch as candidate.
1 parent f337525 commit 6b53ada

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "llvm/Analysis/AssumptionCache.h"
6666
#include "llvm/Analysis/CodeMetrics.h"
6767
#include "llvm/Analysis/DomTreeUpdater.h"
68+
#include "llvm/Analysis/LoopInfo.h"
6869
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
6970
#include "llvm/Analysis/TargetTransformInfo.h"
7071
#include "llvm/IR/CFG.h"
@@ -131,9 +132,9 @@ void unfold(DomTreeUpdater *DTU, SelectInstToUnfold SIToUnfold,
131132

132133
class DFAJumpThreading {
133134
public:
134-
DFAJumpThreading(AssumptionCache *AC, DominatorTree *DT,
135+
DFAJumpThreading(AssumptionCache *AC, DominatorTree *DT, LoopInfo *LI,
135136
TargetTransformInfo *TTI, OptimizationRemarkEmitter *ORE)
136-
: AC(AC), DT(DT), TTI(TTI), ORE(ORE) {}
137+
: AC(AC), DT(DT), LI(LI), TTI(TTI), ORE(ORE) {}
137138

138139
bool run(Function &F);
139140

@@ -161,6 +162,7 @@ class DFAJumpThreading {
161162

162163
AssumptionCache *AC;
163164
DominatorTree *DT;
165+
LoopInfo *LI;
164166
TargetTransformInfo *TTI;
165167
OptimizationRemarkEmitter *ORE;
166168
};
@@ -378,7 +380,8 @@ inline raw_ostream &operator<<(raw_ostream &OS, const ThreadingPath &TPath) {
378380
#endif
379381

380382
struct MainSwitch {
381-
MainSwitch(SwitchInst *SI, OptimizationRemarkEmitter *ORE) {
383+
MainSwitch(SwitchInst *SI, LoopInfo *LI, OptimizationRemarkEmitter *ORE)
384+
: LI(LI) {
382385
if (isCandidate(SI)) {
383386
Instr = SI;
384387
} else {
@@ -411,6 +414,10 @@ struct MainSwitch {
411414
if (!isa<PHINode>(SICond))
412415
return false;
413416

417+
// The switch must be in a loop.
418+
if (!LI->getLoopFor(SI->getParent()))
419+
return false;
420+
414421
addToQueue(SICond, Q, SeenValues);
415422

416423
while (!Q.empty()) {
@@ -488,6 +495,7 @@ struct MainSwitch {
488495
return true;
489496
}
490497

498+
LoopInfo *LI;
491499
SwitchInst *Instr = nullptr;
492500
SmallVector<SelectInstToUnfold, 4> SelectInsts;
493501
};
@@ -1262,7 +1270,7 @@ bool DFAJumpThreading::run(Function &F) {
12621270

12631271
LLVM_DEBUG(dbgs() << "\nCheck if SwitchInst in BB " << BB.getName()
12641272
<< " is a candidate\n");
1265-
MainSwitch Switch(SI, ORE);
1273+
MainSwitch Switch(SI, LI, ORE);
12661274

12671275
if (!Switch.getInstr())
12681276
continue;
@@ -1315,10 +1323,11 @@ PreservedAnalyses DFAJumpThreadingPass::run(Function &F,
13151323
FunctionAnalysisManager &AM) {
13161324
AssumptionCache &AC = AM.getResult<AssumptionAnalysis>(F);
13171325
DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
1326+
LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
13181327
TargetTransformInfo &TTI = AM.getResult<TargetIRAnalysis>(F);
13191328
OptimizationRemarkEmitter ORE(&F);
13201329

1321-
if (!DFAJumpThreading(&AC, &DT, &TTI, &ORE).run(F))
1330+
if (!DFAJumpThreading(&AC, &DT, &LI, &TTI, &ORE).run(F))
13221331
return PreservedAnalyses::all();
13231332

13241333
PreservedAnalyses PA;

0 commit comments

Comments
 (0)