|
65 | 65 | #include "llvm/Analysis/AssumptionCache.h"
|
66 | 66 | #include "llvm/Analysis/CodeMetrics.h"
|
67 | 67 | #include "llvm/Analysis/DomTreeUpdater.h"
|
| 68 | +#include "llvm/Analysis/LoopInfo.h" |
68 | 69 | #include "llvm/Analysis/OptimizationRemarkEmitter.h"
|
69 | 70 | #include "llvm/Analysis/TargetTransformInfo.h"
|
70 | 71 | #include "llvm/IR/CFG.h"
|
@@ -131,9 +132,9 @@ void unfold(DomTreeUpdater *DTU, SelectInstToUnfold SIToUnfold,
|
131 | 132 |
|
132 | 133 | class DFAJumpThreading {
|
133 | 134 | public:
|
134 |
| - DFAJumpThreading(AssumptionCache *AC, DominatorTree *DT, |
| 135 | + DFAJumpThreading(AssumptionCache *AC, DominatorTree *DT, LoopInfo *LI, |
135 | 136 | TargetTransformInfo *TTI, OptimizationRemarkEmitter *ORE)
|
136 |
| - : AC(AC), DT(DT), TTI(TTI), ORE(ORE) {} |
| 137 | + : AC(AC), DT(DT), LI(LI), TTI(TTI), ORE(ORE) {} |
137 | 138 |
|
138 | 139 | bool run(Function &F);
|
139 | 140 |
|
@@ -161,6 +162,7 @@ class DFAJumpThreading {
|
161 | 162 |
|
162 | 163 | AssumptionCache *AC;
|
163 | 164 | DominatorTree *DT;
|
| 165 | + LoopInfo *LI; |
164 | 166 | TargetTransformInfo *TTI;
|
165 | 167 | OptimizationRemarkEmitter *ORE;
|
166 | 168 | };
|
@@ -378,7 +380,8 @@ inline raw_ostream &operator<<(raw_ostream &OS, const ThreadingPath &TPath) {
|
378 | 380 | #endif
|
379 | 381 |
|
380 | 382 | struct MainSwitch {
|
381 |
| - MainSwitch(SwitchInst *SI, OptimizationRemarkEmitter *ORE) { |
| 383 | + MainSwitch(SwitchInst *SI, LoopInfo *LI, OptimizationRemarkEmitter *ORE) |
| 384 | + : LI(LI) { |
382 | 385 | if (isCandidate(SI)) {
|
383 | 386 | Instr = SI;
|
384 | 387 | } else {
|
@@ -411,6 +414,10 @@ struct MainSwitch {
|
411 | 414 | if (!isa<PHINode>(SICond))
|
412 | 415 | return false;
|
413 | 416 |
|
| 417 | + // The switch must be in a loop. |
| 418 | + if (!LI->getLoopFor(SI->getParent())) |
| 419 | + return false; |
| 420 | + |
414 | 421 | addToQueue(SICond, Q, SeenValues);
|
415 | 422 |
|
416 | 423 | while (!Q.empty()) {
|
@@ -488,6 +495,7 @@ struct MainSwitch {
|
488 | 495 | return true;
|
489 | 496 | }
|
490 | 497 |
|
| 498 | + LoopInfo *LI; |
491 | 499 | SwitchInst *Instr = nullptr;
|
492 | 500 | SmallVector<SelectInstToUnfold, 4> SelectInsts;
|
493 | 501 | };
|
@@ -1262,7 +1270,7 @@ bool DFAJumpThreading::run(Function &F) {
|
1262 | 1270 |
|
1263 | 1271 | LLVM_DEBUG(dbgs() << "\nCheck if SwitchInst in BB " << BB.getName()
|
1264 | 1272 | << " is a candidate\n");
|
1265 |
| - MainSwitch Switch(SI, ORE); |
| 1273 | + MainSwitch Switch(SI, LI, ORE); |
1266 | 1274 |
|
1267 | 1275 | if (!Switch.getInstr())
|
1268 | 1276 | continue;
|
@@ -1315,10 +1323,11 @@ PreservedAnalyses DFAJumpThreadingPass::run(Function &F,
|
1315 | 1323 | FunctionAnalysisManager &AM) {
|
1316 | 1324 | AssumptionCache &AC = AM.getResult<AssumptionAnalysis>(F);
|
1317 | 1325 | DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
|
| 1326 | + LoopInfo &LI = AM.getResult<LoopAnalysis>(F); |
1318 | 1327 | TargetTransformInfo &TTI = AM.getResult<TargetIRAnalysis>(F);
|
1319 | 1328 | OptimizationRemarkEmitter ORE(&F);
|
1320 | 1329 |
|
1321 |
| - if (!DFAJumpThreading(&AC, &DT, &TTI, &ORE).run(F)) |
| 1330 | + if (!DFAJumpThreading(&AC, &DT, &LI, &TTI, &ORE).run(F)) |
1322 | 1331 | return PreservedAnalyses::all();
|
1323 | 1332 |
|
1324 | 1333 | PreservedAnalyses PA;
|
|
0 commit comments