1 //===- DFAJumpThreading.cpp - Threads a switch statement inside a loop ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Transform each threading path to effectively jump thread the DFA. For
10 // example, the CFG below could be transformed as follows, where the cloned
11 // blocks unconditionally branch to the next correct case based on what is
12 // identified in the analysis.
13 //
14 //          sw.bb                        sw.bb
15 //        /   |   \                    /   |   \
16 //   case1  case2  case3          case1  case2  case3
17 //        \   |   /                 |      |      |
18 //       determinator            det.2   det.3  det.1
19 //        br sw.bb                /        |        \
20 //                          sw.bb.2     sw.bb.3     sw.bb.1
21 //                           br case2    br case3    br case1§
22 //
23 // Definitions and Terminology:
24 //
25 // * Threading path:
26 //   a list of basic blocks, the exit state, and the block that determines
27 //   the next state, for which the following notation will be used:
28 //   < path of BBs that form a cycle > [ state, determinator ]
29 //
30 // * Predictable switch:
31 //   The switch variable is always a known constant so that all conditional
32 //   jumps based on switch variable can be converted to unconditional jump.
33 //
34 // * Determinator:
35 //   The basic block that determines the next state of the DFA.
36 //
37 // Representing the optimization in C-like pseudocode: the code pattern on the
38 // left could functionally be transformed to the right pattern if the switch
39 // condition is predictable.
40 //
41 //  X = A                       goto A
42 //  for (...)                   A:
43 //    switch (X)                  ...
44 //      case A                    goto B
45 //        X = B                 B:
46 //      case B                    ...
47 //        X = C                   goto C
48 //
49 // The pass first checks that switch variable X is decided by the control flow
50 // path taken in the loop; for example, in case B, the next value of X is
51 // decided to be C. It then enumerates through all paths in the loop and labels
52 // the basic blocks where the next state is decided.
53 //
54 // Using this information it creates new paths that unconditionally branch to
55 // the next case. This involves cloning code, so it only gets triggered if the
56 // amount of code duplicated is below a threshold.
57 //
58 //===----------------------------------------------------------------------===//
59 
60 #include "llvm/Transforms/Scalar/DFAJumpThreading.h"
61 #include "llvm/ADT/APInt.h"
62 #include "llvm/ADT/DenseMap.h"
63 #include "llvm/ADT/SmallSet.h"
64 #include "llvm/ADT/Statistic.h"
65 #include "llvm/Analysis/AssumptionCache.h"
66 #include "llvm/Analysis/CodeMetrics.h"
67 #include "llvm/Analysis/DomTreeUpdater.h"
68 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
69 #include "llvm/Analysis/TargetTransformInfo.h"
70 #include "llvm/IR/CFG.h"
71 #include "llvm/IR/Constants.h"
72 #include "llvm/IR/IntrinsicInst.h"
73 #include "llvm/InitializePasses.h"
74 #include "llvm/Pass.h"
75 #include "llvm/Support/CommandLine.h"
76 #include "llvm/Support/Debug.h"
77 #include "llvm/Transforms/Scalar.h"
78 #include "llvm/Transforms/Utils/Cloning.h"
79 #include "llvm/Transforms/Utils/SSAUpdaterBulk.h"
80 #include "llvm/Transforms/Utils/ValueMapper.h"
81 #include <algorithm>
82 #include <deque>
83 
84 #ifdef EXPENSIVE_CHECKS
85 #include "llvm/IR/Verifier.h"
86 #endif
87 
88 using namespace llvm;
89 
90 #define DEBUG_TYPE "dfa-jump-threading"
91 
92 STATISTIC(NumTransforms, "Number of transformations done");
93 STATISTIC(NumCloned, "Number of blocks cloned");
94 STATISTIC(NumPaths, "Number of individual paths threaded");
95 
96 static cl::opt<bool>
97     ClViewCfgBefore("dfa-jump-view-cfg-before",
98                     cl::desc("View the CFG before DFA Jump Threading"),
99                     cl::Hidden, cl::init(false));
100 
101 static cl::opt<unsigned> MaxPathLength(
102     "dfa-max-path-length",
103     cl::desc("Max number of blocks searched to find a threading path"),
104     cl::Hidden, cl::init(20));
105 
106 static cl::opt<unsigned> MaxNumPaths(
107     "dfa-max-num-paths",
108     cl::desc("Max number of paths enumerated around a switch"),
109     cl::Hidden, cl::init(200));
110 
111 static cl::opt<unsigned>
112     CostThreshold("dfa-cost-threshold",
113                   cl::desc("Maximum cost accepted for the transformation"),
114                   cl::Hidden, cl::init(50));
115 
116 namespace {
117 
118 class SelectInstToUnfold {
119   SelectInst *SI;
120   PHINode *SIUse;
121 
122 public:
123   SelectInstToUnfold(SelectInst *SI, PHINode *SIUse) : SI(SI), SIUse(SIUse) {}
124 
125   SelectInst *getInst() { return SI; }
126   PHINode *getUse() { return SIUse; }
127 
128   explicit operator bool() const { return SI && SIUse; }
129 };
130 
131 void unfold(DomTreeUpdater *DTU, SelectInstToUnfold SIToUnfold,
132             std::vector<SelectInstToUnfold> *NewSIsToUnfold,
133             std::vector<BasicBlock *> *NewBBs);
134 
135 class DFAJumpThreading {
136 public:
137   DFAJumpThreading(AssumptionCache *AC, DominatorTree *DT,
138                    TargetTransformInfo *TTI, OptimizationRemarkEmitter *ORE)
139       : AC(AC), DT(DT), TTI(TTI), ORE(ORE) {}
140 
141   bool run(Function &F);
142 
143 private:
144   void
145   unfoldSelectInstrs(DominatorTree *DT,
146                      const SmallVector<SelectInstToUnfold, 4> &SelectInsts) {
147     DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
148     SmallVector<SelectInstToUnfold, 4> Stack;
149     for (SelectInstToUnfold SIToUnfold : SelectInsts)
150       Stack.push_back(SIToUnfold);
151 
152     while (!Stack.empty()) {
153       SelectInstToUnfold SIToUnfold = Stack.pop_back_val();
154 
155       std::vector<SelectInstToUnfold> NewSIsToUnfold;
156       std::vector<BasicBlock *> NewBBs;
157       unfold(&DTU, SIToUnfold, &NewSIsToUnfold, &NewBBs);
158 
159       // Put newly discovered select instructions into the work list.
160       for (const SelectInstToUnfold &NewSIToUnfold : NewSIsToUnfold)
161         Stack.push_back(NewSIToUnfold);
162     }
163   }
164 
165   AssumptionCache *AC;
166   DominatorTree *DT;
167   TargetTransformInfo *TTI;
168   OptimizationRemarkEmitter *ORE;
169 };
170 
171 class DFAJumpThreadingLegacyPass : public FunctionPass {
172 public:
173   static char ID; // Pass identification
174   DFAJumpThreadingLegacyPass() : FunctionPass(ID) {}
175 
176   void getAnalysisUsage(AnalysisUsage &AU) const override {
177     AU.addRequired<AssumptionCacheTracker>();
178     AU.addRequired<DominatorTreeWrapperPass>();
179     AU.addPreserved<DominatorTreeWrapperPass>();
180     AU.addRequired<TargetTransformInfoWrapperPass>();
181     AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
182   }
183 
184   bool runOnFunction(Function &F) override {
185     if (skipFunction(F))
186       return false;
187 
188     AssumptionCache *AC =
189         &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
190     DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
191     TargetTransformInfo *TTI =
192         &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
193     OptimizationRemarkEmitter *ORE =
194         &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
195 
196     return DFAJumpThreading(AC, DT, TTI, ORE).run(F);
197   }
198 };
199 } // end anonymous namespace
200 
201 char DFAJumpThreadingLegacyPass::ID = 0;
202 INITIALIZE_PASS_BEGIN(DFAJumpThreadingLegacyPass, "dfa-jump-threading",
203                       "DFA Jump Threading", false, false)
204 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
205 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
206 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
207 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
208 INITIALIZE_PASS_END(DFAJumpThreadingLegacyPass, "dfa-jump-threading",
209                     "DFA Jump Threading", false, false)
210 
211 // Public interface to the DFA Jump Threading pass
212 FunctionPass *llvm::createDFAJumpThreadingPass() {
213   return new DFAJumpThreadingLegacyPass();
214 }
215 
216 namespace {
217 
218 /// Create a new basic block and sink \p SIToSink into it.
219 void createBasicBlockAndSinkSelectInst(
220     DomTreeUpdater *DTU, SelectInst *SI, PHINode *SIUse, SelectInst *SIToSink,
221     BasicBlock *EndBlock, StringRef NewBBName, BasicBlock **NewBlock,
222     BranchInst **NewBranch, std::vector<SelectInstToUnfold> *NewSIsToUnfold,
223     std::vector<BasicBlock *> *NewBBs) {
224   assert(SIToSink->hasOneUse());
225   assert(NewBlock);
226   assert(NewBranch);
227   *NewBlock = BasicBlock::Create(SI->getContext(), NewBBName,
228                                  EndBlock->getParent(), EndBlock);
229   NewBBs->push_back(*NewBlock);
230   *NewBranch = BranchInst::Create(EndBlock, *NewBlock);
231   SIToSink->moveBefore(*NewBranch);
232   NewSIsToUnfold->push_back(SelectInstToUnfold(SIToSink, SIUse));
233   DTU->applyUpdates({{DominatorTree::Insert, *NewBlock, EndBlock}});
234 }
235 
236 /// Unfold the select instruction held in \p SIToUnfold by replacing it with
237 /// control flow.
238 ///
239 /// Put newly discovered select instructions into \p NewSIsToUnfold. Put newly
240 /// created basic blocks into \p NewBBs.
241 ///
242 /// TODO: merge it with CodeGenPrepare::optimizeSelectInst() if possible.
243 void unfold(DomTreeUpdater *DTU, SelectInstToUnfold SIToUnfold,
244             std::vector<SelectInstToUnfold> *NewSIsToUnfold,
245             std::vector<BasicBlock *> *NewBBs) {
246   SelectInst *SI = SIToUnfold.getInst();
247   PHINode *SIUse = SIToUnfold.getUse();
248   BasicBlock *StartBlock = SI->getParent();
249   BasicBlock *EndBlock = SIUse->getParent();
250   BranchInst *StartBlockTerm =
251       dyn_cast<BranchInst>(StartBlock->getTerminator());
252 
253   assert(StartBlockTerm && StartBlockTerm->isUnconditional());
254   assert(SI->hasOneUse());
255 
256   // These are the new basic blocks for the conditional branch.
257   // At least one will become an actual new basic block.
258   BasicBlock *TrueBlock = nullptr;
259   BasicBlock *FalseBlock = nullptr;
260   BranchInst *TrueBranch = nullptr;
261   BranchInst *FalseBranch = nullptr;
262 
263   // Sink select instructions to be able to unfold them later.
264   if (SelectInst *SIOp = dyn_cast<SelectInst>(SI->getTrueValue())) {
265     createBasicBlockAndSinkSelectInst(DTU, SI, SIUse, SIOp, EndBlock,
266                                       "si.unfold.true", &TrueBlock, &TrueBranch,
267                                       NewSIsToUnfold, NewBBs);
268   }
269   if (SelectInst *SIOp = dyn_cast<SelectInst>(SI->getFalseValue())) {
270     createBasicBlockAndSinkSelectInst(DTU, SI, SIUse, SIOp, EndBlock,
271                                       "si.unfold.false", &FalseBlock,
272                                       &FalseBranch, NewSIsToUnfold, NewBBs);
273   }
274 
275   // If there was nothing to sink, then arbitrarily choose the 'false' side
276   // for a new input value to the PHI.
277   if (!TrueBlock && !FalseBlock) {
278     FalseBlock = BasicBlock::Create(SI->getContext(), "si.unfold.false",
279                                     EndBlock->getParent(), EndBlock);
280     NewBBs->push_back(FalseBlock);
281     BranchInst::Create(EndBlock, FalseBlock);
282     DTU->applyUpdates({{DominatorTree::Insert, FalseBlock, EndBlock}});
283   }
284 
285   // Insert the real conditional branch based on the original condition.
286   // If we did not create a new block for one of the 'true' or 'false' paths
287   // of the condition, it means that side of the branch goes to the end block
288   // directly and the path originates from the start block from the point of
289   // view of the new PHI.
290   BasicBlock *TT = EndBlock;
291   BasicBlock *FT = EndBlock;
292   if (TrueBlock && FalseBlock) {
293     // A diamond.
294     TT = TrueBlock;
295     FT = FalseBlock;
296 
297     // Update the phi node of SI.
298     SIUse->removeIncomingValue(StartBlock, /* DeletePHIIfEmpty = */ false);
299     SIUse->addIncoming(SI->getTrueValue(), TrueBlock);
300     SIUse->addIncoming(SI->getFalseValue(), FalseBlock);
301 
302     // Update any other PHI nodes in EndBlock.
303     for (PHINode &Phi : EndBlock->phis()) {
304       if (&Phi != SIUse) {
305         Phi.addIncoming(Phi.getIncomingValueForBlock(StartBlock), TrueBlock);
306         Phi.addIncoming(Phi.getIncomingValueForBlock(StartBlock), FalseBlock);
307       }
308     }
309   } else {
310     BasicBlock *NewBlock = nullptr;
311     Value *SIOp1 = SI->getTrueValue();
312     Value *SIOp2 = SI->getFalseValue();
313 
314     // A triangle pointing right.
315     if (!TrueBlock) {
316       NewBlock = FalseBlock;
317       FT = FalseBlock;
318     }
319     // A triangle pointing left.
320     else {
321       NewBlock = TrueBlock;
322       TT = TrueBlock;
323       std::swap(SIOp1, SIOp2);
324     }
325 
326     // Update the phi node of SI.
327     for (unsigned Idx = 0; Idx < SIUse->getNumIncomingValues(); ++Idx) {
328       if (SIUse->getIncomingBlock(Idx) == StartBlock)
329         SIUse->setIncomingValue(Idx, SIOp1);
330     }
331     SIUse->addIncoming(SIOp2, NewBlock);
332 
333     // Update any other PHI nodes in EndBlock.
334     for (auto II = EndBlock->begin(); PHINode *Phi = dyn_cast<PHINode>(II);
335          ++II) {
336       if (Phi != SIUse)
337         Phi->addIncoming(Phi->getIncomingValueForBlock(StartBlock), NewBlock);
338     }
339   }
340   StartBlockTerm->eraseFromParent();
341   BranchInst::Create(TT, FT, SI->getCondition(), StartBlock);
342   DTU->applyUpdates({{DominatorTree::Insert, StartBlock, TT},
343                      {DominatorTree::Insert, StartBlock, FT}});
344 
345   // The select is now dead.
346   SI->eraseFromParent();
347 }
348 
349 struct ClonedBlock {
350   BasicBlock *BB;
351   uint64_t State; ///< \p State corresponds to the next value of a switch stmnt.
352 };
353 
354 typedef std::deque<BasicBlock *> PathType;
355 typedef std::vector<PathType> PathsType;
356 typedef SmallPtrSet<const BasicBlock *, 8> VisitedBlocks;
357 typedef std::vector<ClonedBlock> CloneList;
358 
359 // This data structure keeps track of all blocks that have been cloned.  If two
360 // different ThreadingPaths clone the same block for a certain state it should
361 // be reused, and it can be looked up in this map.
362 typedef DenseMap<BasicBlock *, CloneList> DuplicateBlockMap;
363 
364 // This map keeps track of all the new definitions for an instruction. This
365 // information is needed when restoring SSA form after cloning blocks.
366 typedef MapVector<Instruction *, std::vector<Instruction *>> DefMap;
367 
368 inline raw_ostream &operator<<(raw_ostream &OS, const PathType &Path) {
369   OS << "< ";
370   for (const BasicBlock *BB : Path) {
371     std::string BBName;
372     if (BB->hasName())
373       raw_string_ostream(BBName) << BB->getName();
374     else
375       raw_string_ostream(BBName) << BB;
376     OS << BBName << " ";
377   }
378   OS << ">";
379   return OS;
380 }
381 
382 /// ThreadingPath is a path in the control flow of a loop that can be threaded
383 /// by cloning necessary basic blocks and replacing conditional branches with
384 /// unconditional ones. A threading path includes a list of basic blocks, the
385 /// exit state, and the block that determines the next state.
386 struct ThreadingPath {
387   /// Exit value is DFA's exit state for the given path.
388   uint64_t getExitValue() const { return ExitVal; }
389   void setExitValue(const ConstantInt *V) {
390     ExitVal = V->getZExtValue();
391     IsExitValSet = true;
392   }
393   bool isExitValueSet() const { return IsExitValSet; }
394 
395   /// Determinator is the basic block that determines the next state of the DFA.
396   const BasicBlock *getDeterminatorBB() const { return DBB; }
397   void setDeterminator(const BasicBlock *BB) { DBB = BB; }
398 
399   /// Path is a list of basic blocks.
400   const PathType &getPath() const { return Path; }
401   void setPath(const PathType &NewPath) { Path = NewPath; }
402 
403   void print(raw_ostream &OS) const {
404     OS << Path << " [ " << ExitVal << ", " << DBB->getName() << " ]";
405   }
406 
407 private:
408   PathType Path;
409   uint64_t ExitVal;
410   const BasicBlock *DBB = nullptr;
411   bool IsExitValSet = false;
412 };
413 
414 #ifndef NDEBUG
415 inline raw_ostream &operator<<(raw_ostream &OS, const ThreadingPath &TPath) {
416   TPath.print(OS);
417   return OS;
418 }
419 #endif
420 
421 struct MainSwitch {
422   MainSwitch(SwitchInst *SI, OptimizationRemarkEmitter *ORE) {
423     if (isCandidate(SI)) {
424       Instr = SI;
425     } else {
426       ORE->emit([&]() {
427         return OptimizationRemarkMissed(DEBUG_TYPE, "SwitchNotPredictable", SI)
428                << "Switch instruction is not predictable.";
429       });
430     }
431   }
432 
433   virtual ~MainSwitch() = default;
434 
435   SwitchInst *getInstr() const { return Instr; }
436   const SmallVector<SelectInstToUnfold, 4> getSelectInsts() {
437     return SelectInsts;
438   }
439 
440 private:
441   /// Do a use-def chain traversal starting from the switch condition to see if
442   /// \p SI is a potential condidate.
443   ///
444   /// Also, collect select instructions to unfold.
445   bool isCandidate(const SwitchInst *SI) {
446     std::deque<Value *> Q;
447     SmallSet<Value *, 16> SeenValues;
448     SelectInsts.clear();
449 
450     Value *SICond = SI->getCondition();
451     LLVM_DEBUG(dbgs() << "\tSICond: " << *SICond << "\n");
452     if (!isa<PHINode>(SICond))
453       return false;
454 
455     addToQueue(SICond, Q, SeenValues);
456 
457     while (!Q.empty()) {
458       Value *Current = Q.front();
459       Q.pop_front();
460 
461       if (auto *Phi = dyn_cast<PHINode>(Current)) {
462         for (Value *Incoming : Phi->incoming_values()) {
463           addToQueue(Incoming, Q, SeenValues);
464         }
465         LLVM_DEBUG(dbgs() << "\tphi: " << *Phi << "\n");
466       } else if (SelectInst *SelI = dyn_cast<SelectInst>(Current)) {
467         if (!isValidSelectInst(SelI))
468           return false;
469         addToQueue(SelI->getTrueValue(), Q, SeenValues);
470         addToQueue(SelI->getFalseValue(), Q, SeenValues);
471         LLVM_DEBUG(dbgs() << "\tselect: " << *SelI << "\n");
472         if (auto *SelIUse = dyn_cast<PHINode>(SelI->user_back()))
473           SelectInsts.push_back(SelectInstToUnfold(SelI, SelIUse));
474       } else if (isa<Constant>(Current)) {
475         LLVM_DEBUG(dbgs() << "\tconst: " << *Current << "\n");
476         continue;
477       } else {
478         LLVM_DEBUG(dbgs() << "\tother: " << *Current << "\n");
479         // Allow unpredictable values. The hope is that those will be the
480         // initial switch values that can be ignored (they will hit the
481         // unthreaded switch) but this assumption will get checked later after
482         // paths have been enumerated (in function getStateDefMap).
483         continue;
484       }
485     }
486 
487     return true;
488   }
489 
490   void addToQueue(Value *Val, std::deque<Value *> &Q,
491                   SmallSet<Value *, 16> &SeenValues) {
492     if (SeenValues.contains(Val))
493       return;
494     Q.push_back(Val);
495     SeenValues.insert(Val);
496   }
497 
498   bool isValidSelectInst(SelectInst *SI) {
499     if (!SI->hasOneUse())
500       return false;
501 
502     Instruction *SIUse = dyn_cast<Instruction>(SI->user_back());
503     // The use of the select inst should be either a phi or another select.
504     if (!SIUse && !(isa<PHINode>(SIUse) || isa<SelectInst>(SIUse)))
505       return false;
506 
507     BasicBlock *SIBB = SI->getParent();
508 
509     // Currently, we can only expand select instructions in basic blocks with
510     // one successor.
511     BranchInst *SITerm = dyn_cast<BranchInst>(SIBB->getTerminator());
512     if (!SITerm || !SITerm->isUnconditional())
513       return false;
514 
515     if (isa<PHINode>(SIUse) &&
516         SIBB->getSingleSuccessor() != cast<Instruction>(SIUse)->getParent())
517       return false;
518 
519     // If select will not be sunk during unfolding, and it is in the same basic
520     // block as another state defining select, then cannot unfold both.
521     for (SelectInstToUnfold SIToUnfold : SelectInsts) {
522       SelectInst *PrevSI = SIToUnfold.getInst();
523       if (PrevSI->getTrueValue() != SI && PrevSI->getFalseValue() != SI &&
524           PrevSI->getParent() == SI->getParent())
525         return false;
526     }
527 
528     return true;
529   }
530 
531   SwitchInst *Instr = nullptr;
532   SmallVector<SelectInstToUnfold, 4> SelectInsts;
533 };
534 
535 struct AllSwitchPaths {
536   AllSwitchPaths(const MainSwitch *MSwitch, OptimizationRemarkEmitter *ORE)
537       : Switch(MSwitch->getInstr()), SwitchBlock(Switch->getParent()),
538         ORE(ORE) {}
539 
540   std::vector<ThreadingPath> &getThreadingPaths() { return TPaths; }
541   unsigned getNumThreadingPaths() { return TPaths.size(); }
542   SwitchInst *getSwitchInst() { return Switch; }
543   BasicBlock *getSwitchBlock() { return SwitchBlock; }
544 
545   void run() {
546     VisitedBlocks Visited;
547     PathsType LoopPaths = paths(SwitchBlock, Visited, /* PathDepth = */ 1);
548     StateDefMap StateDef = getStateDefMap(LoopPaths);
549 
550     if (StateDef.empty()) {
551       ORE->emit([&]() {
552         return OptimizationRemarkMissed(DEBUG_TYPE, "SwitchNotPredictable",
553                                         Switch)
554                << "Switch instruction is not predictable.";
555       });
556       return;
557     }
558 
559     for (PathType Path : LoopPaths) {
560       ThreadingPath TPath;
561 
562       const BasicBlock *PrevBB = Path.back();
563       for (const BasicBlock *BB : Path) {
564         if (StateDef.count(BB) != 0) {
565           const PHINode *Phi = dyn_cast<PHINode>(StateDef[BB]);
566           assert(Phi && "Expected a state-defining instr to be a phi node.");
567 
568           const Value *V = Phi->getIncomingValueForBlock(PrevBB);
569           if (const ConstantInt *C = dyn_cast<const ConstantInt>(V)) {
570             TPath.setExitValue(C);
571             TPath.setDeterminator(BB);
572             TPath.setPath(Path);
573           }
574         }
575 
576         // Switch block is the determinator, this is the final exit value.
577         if (TPath.isExitValueSet() && BB == Path.front())
578           break;
579 
580         PrevBB = BB;
581       }
582 
583       if (TPath.isExitValueSet() && isSupported(TPath))
584         TPaths.push_back(TPath);
585     }
586   }
587 
588 private:
589   // Value: an instruction that defines a switch state;
590   // Key: the parent basic block of that instruction.
591   typedef DenseMap<const BasicBlock *, const PHINode *> StateDefMap;
592 
593   PathsType paths(BasicBlock *BB, VisitedBlocks &Visited,
594                   unsigned PathDepth) const {
595     PathsType Res;
596 
597     // Stop exploring paths after visiting MaxPathLength blocks
598     if (PathDepth > MaxPathLength) {
599       ORE->emit([&]() {
600         return OptimizationRemarkAnalysis(DEBUG_TYPE, "MaxPathLengthReached",
601                                           Switch)
602                << "Exploration stopped after visiting MaxPathLength="
603                << ore::NV("MaxPathLength", MaxPathLength) << " blocks.";
604       });
605       return Res;
606     }
607 
608     Visited.insert(BB);
609 
610     // Some blocks have multiple edges to the same successor, and this set
611     // is used to prevent a duplicate path from being generated
612     SmallSet<BasicBlock *, 4> Successors;
613     for (BasicBlock *Succ : successors(BB)) {
614       if (!Successors.insert(Succ).second)
615         continue;
616 
617       // Found a cycle through the SwitchBlock
618       if (Succ == SwitchBlock) {
619         Res.push_back({BB});
620         continue;
621       }
622 
623       // We have encountered a cycle, do not get caught in it
624       if (Visited.contains(Succ))
625         continue;
626 
627       PathsType SuccPaths = paths(Succ, Visited, PathDepth + 1);
628       for (PathType Path : SuccPaths) {
629         PathType NewPath(Path);
630         NewPath.push_front(BB);
631         Res.push_back(NewPath);
632         if (Res.size() >= MaxNumPaths) {
633           return Res;
634         }
635       }
636     }
637     // This block could now be visited again from a different predecessor. Note
638     // that this will result in exponential runtime. Subpaths could possibly be
639     // cached but it takes a lot of memory to store them.
640     Visited.erase(BB);
641     return Res;
642   }
643 
644   /// Walk the use-def chain and collect all the state-defining instructions.
645   ///
646   /// Return an empty map if unpredictable values encountered inside the basic
647   /// blocks of \p LoopPaths.
648   StateDefMap getStateDefMap(const PathsType &LoopPaths) const {
649     StateDefMap Res;
650 
651     // Basic blocks belonging to any of the loops around the switch statement.
652     SmallPtrSet<BasicBlock *, 16> LoopBBs;
653     for (const PathType &Path : LoopPaths) {
654       for (BasicBlock *BB : Path)
655         LoopBBs.insert(BB);
656     }
657 
658     Value *FirstDef = Switch->getOperand(0);
659 
660     assert(isa<PHINode>(FirstDef) && "The first definition must be a phi.");
661 
662     SmallVector<PHINode *, 8> Stack;
663     Stack.push_back(dyn_cast<PHINode>(FirstDef));
664     SmallSet<Value *, 16> SeenValues;
665 
666     while (!Stack.empty()) {
667       PHINode *CurPhi = Stack.pop_back_val();
668 
669       Res[CurPhi->getParent()] = CurPhi;
670       SeenValues.insert(CurPhi);
671 
672       for (BasicBlock *IncomingBB : CurPhi->blocks()) {
673         Value *Incoming = CurPhi->getIncomingValueForBlock(IncomingBB);
674         bool IsOutsideLoops = LoopBBs.count(IncomingBB) == 0;
675         if (Incoming == FirstDef || isa<ConstantInt>(Incoming) ||
676             SeenValues.contains(Incoming) || IsOutsideLoops) {
677           continue;
678         }
679 
680         // Any unpredictable value inside the loops means we must bail out.
681         if (!isa<PHINode>(Incoming))
682           return StateDefMap();
683 
684         Stack.push_back(cast<PHINode>(Incoming));
685       }
686     }
687 
688     return Res;
689   }
690 
691   /// The determinator BB should precede the switch-defining BB.
692   ///
693   /// Otherwise, it is possible that the state defined in the determinator block
694   /// defines the state for the next iteration of the loop, rather than for the
695   /// current one.
696   ///
697   /// Currently supported paths:
698   /// \code
699   /// < switch bb1 determ def > [ 42, determ ]
700   /// < switch_and_def bb1 determ > [ 42, determ ]
701   /// < switch_and_def_and_determ bb1 > [ 42, switch_and_def_and_determ ]
702   /// \endcode
703   ///
704   /// Unsupported paths:
705   /// \code
706   /// < switch bb1 def determ > [ 43, determ ]
707   /// < switch_and_determ bb1 def > [ 43, switch_and_determ ]
708   /// \endcode
709   bool isSupported(const ThreadingPath &TPath) {
710     Instruction *SwitchCondI = dyn_cast<Instruction>(Switch->getCondition());
711     assert(SwitchCondI);
712     if (!SwitchCondI)
713       return false;
714 
715     const BasicBlock *SwitchCondDefBB = SwitchCondI->getParent();
716     const BasicBlock *SwitchCondUseBB = Switch->getParent();
717     const BasicBlock *DeterminatorBB = TPath.getDeterminatorBB();
718 
719     assert(
720         SwitchCondUseBB == TPath.getPath().front() &&
721         "The first BB in a threading path should have the switch instruction");
722     if (SwitchCondUseBB != TPath.getPath().front())
723       return false;
724 
725     // Make DeterminatorBB the first element in Path.
726     PathType Path = TPath.getPath();
727     auto ItDet = std::find(Path.begin(), Path.end(), DeterminatorBB);
728     std::rotate(Path.begin(), ItDet, Path.end());
729 
730     bool IsDetBBSeen = false;
731     bool IsDefBBSeen = false;
732     bool IsUseBBSeen = false;
733     for (BasicBlock *BB : Path) {
734       if (BB == DeterminatorBB)
735         IsDetBBSeen = true;
736       if (BB == SwitchCondDefBB)
737         IsDefBBSeen = true;
738       if (BB == SwitchCondUseBB)
739         IsUseBBSeen = true;
740       if (IsDetBBSeen && IsUseBBSeen && !IsDefBBSeen)
741         return false;
742     }
743 
744     return true;
745   }
746 
747   SwitchInst *Switch;
748   BasicBlock *SwitchBlock;
749   OptimizationRemarkEmitter *ORE;
750   std::vector<ThreadingPath> TPaths;
751 };
752 
753 struct TransformDFA {
754   TransformDFA(AllSwitchPaths *SwitchPaths, DominatorTree *DT,
755                AssumptionCache *AC, TargetTransformInfo *TTI,
756                OptimizationRemarkEmitter *ORE,
757                SmallPtrSet<const Value *, 32> EphValues)
758       : SwitchPaths(SwitchPaths), DT(DT), AC(AC), TTI(TTI), ORE(ORE),
759         EphValues(EphValues) {}
760 
761   void run() {
762     if (isLegalAndProfitableToTransform()) {
763       createAllExitPaths();
764       NumTransforms++;
765     }
766   }
767 
768 private:
769   /// This function performs both a legality check and profitability check at
770   /// the same time since it is convenient to do so. It iterates through all
771   /// blocks that will be cloned, and keeps track of the duplication cost. It
772   /// also returns false if it is illegal to clone some required block.
773   bool isLegalAndProfitableToTransform() {
774     CodeMetrics Metrics;
775     SwitchInst *Switch = SwitchPaths->getSwitchInst();
776 
777     // Note that DuplicateBlockMap is not being used as intended here. It is
778     // just being used to ensure (BB, State) pairs are only counted once.
779     DuplicateBlockMap DuplicateMap;
780 
781     for (ThreadingPath &TPath : SwitchPaths->getThreadingPaths()) {
782       PathType PathBBs = TPath.getPath();
783       uint64_t NextState = TPath.getExitValue();
784       const BasicBlock *Determinator = TPath.getDeterminatorBB();
785 
786       // Update Metrics for the Switch block, this is always cloned
787       BasicBlock *BB = SwitchPaths->getSwitchBlock();
788       BasicBlock *VisitedBB = getClonedBB(BB, NextState, DuplicateMap);
789       if (!VisitedBB) {
790         Metrics.analyzeBasicBlock(BB, *TTI, EphValues);
791         DuplicateMap[BB].push_back({BB, NextState});
792       }
793 
794       // If the Switch block is the Determinator, then we can continue since
795       // this is the only block that is cloned and we already counted for it.
796       if (PathBBs.front() == Determinator)
797         continue;
798 
799       // Otherwise update Metrics for all blocks that will be cloned. If any
800       // block is already cloned and would be reused, don't double count it.
801       auto DetIt = std::find(PathBBs.begin(), PathBBs.end(), Determinator);
802       for (auto BBIt = DetIt; BBIt != PathBBs.end(); BBIt++) {
803         BB = *BBIt;
804         VisitedBB = getClonedBB(BB, NextState, DuplicateMap);
805         if (VisitedBB)
806           continue;
807         Metrics.analyzeBasicBlock(BB, *TTI, EphValues);
808         DuplicateMap[BB].push_back({BB, NextState});
809       }
810 
811       if (Metrics.notDuplicatable) {
812         LLVM_DEBUG(dbgs() << "DFA Jump Threading: Not jump threading, contains "
813                           << "non-duplicatable instructions.\n");
814         ORE->emit([&]() {
815           return OptimizationRemarkMissed(DEBUG_TYPE, "NonDuplicatableInst",
816                                           Switch)
817                  << "Contains non-duplicatable instructions.";
818         });
819         return false;
820       }
821 
822       if (Metrics.convergent) {
823         LLVM_DEBUG(dbgs() << "DFA Jump Threading: Not jump threading, contains "
824                           << "convergent instructions.\n");
825         ORE->emit([&]() {
826           return OptimizationRemarkMissed(DEBUG_TYPE, "ConvergentInst", Switch)
827                  << "Contains convergent instructions.";
828         });
829         return false;
830       }
831     }
832 
833     unsigned DuplicationCost = 0;
834 
835     unsigned JumpTableSize = 0;
836     TTI->getEstimatedNumberOfCaseClusters(*Switch, JumpTableSize, nullptr,
837                                           nullptr);
838     if (JumpTableSize == 0) {
839       // Factor in the number of conditional branches reduced from jump
840       // threading. Assume that lowering the switch block is implemented by
841       // using binary search, hence the LogBase2().
842       unsigned CondBranches =
843           APInt(32, Switch->getNumSuccessors()).ceilLogBase2();
844       DuplicationCost = Metrics.NumInsts / CondBranches;
845     } else {
846       // Compared with jump tables, the DFA optimizer removes an indirect branch
847       // on each loop iteration, thus making branch prediction more precise. The
848       // more branch targets there are, the more likely it is for the branch
849       // predictor to make a mistake, and the more benefit there is in the DFA
850       // optimizer. Thus, the more branch targets there are, the lower is the
851       // cost of the DFA opt.
852       DuplicationCost = Metrics.NumInsts / JumpTableSize;
853     }
854 
855     LLVM_DEBUG(dbgs() << "\nDFA Jump Threading: Cost to jump thread block "
856                       << SwitchPaths->getSwitchBlock()->getName()
857                       << " is: " << DuplicationCost << "\n\n");
858 
859     if (DuplicationCost > CostThreshold) {
860       LLVM_DEBUG(dbgs() << "Not jump threading, duplication cost exceeds the "
861                         << "cost threshold.\n");
862       ORE->emit([&]() {
863         return OptimizationRemarkMissed(DEBUG_TYPE, "NotProfitable", Switch)
864                << "Duplication cost exceeds the cost threshold (cost="
865                << ore::NV("Cost", DuplicationCost)
866                << ", threshold=" << ore::NV("Threshold", CostThreshold) << ").";
867       });
868       return false;
869     }
870 
871     ORE->emit([&]() {
872       return OptimizationRemark(DEBUG_TYPE, "JumpThreaded", Switch)
873              << "Switch statement jump-threaded.";
874     });
875 
876     return true;
877   }
878 
879   /// Transform each threading path to effectively jump thread the DFA.
880   void createAllExitPaths() {
881     DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Eager);
882 
883     // Move the switch block to the end of the path, since it will be duplicated
884     BasicBlock *SwitchBlock = SwitchPaths->getSwitchBlock();
885     for (ThreadingPath &TPath : SwitchPaths->getThreadingPaths()) {
886       LLVM_DEBUG(dbgs() << TPath << "\n");
887       PathType NewPath(TPath.getPath());
888       NewPath.push_back(SwitchBlock);
889       TPath.setPath(NewPath);
890     }
891 
892     // Transform the ThreadingPaths and keep track of the cloned values
893     DuplicateBlockMap DuplicateMap;
894     DefMap NewDefs;
895 
896     SmallSet<BasicBlock *, 16> BlocksToClean;
897     for (BasicBlock *BB : successors(SwitchBlock))
898       BlocksToClean.insert(BB);
899 
900     for (ThreadingPath &TPath : SwitchPaths->getThreadingPaths()) {
901       createExitPath(NewDefs, TPath, DuplicateMap, BlocksToClean, &DTU);
902       NumPaths++;
903     }
904 
905     // After all paths are cloned, now update the last successor of the cloned
906     // path so it skips over the switch statement
907     for (ThreadingPath &TPath : SwitchPaths->getThreadingPaths())
908       updateLastSuccessor(TPath, DuplicateMap, &DTU);
909 
910     // For each instruction that was cloned and used outside, update its uses
911     updateSSA(NewDefs);
912 
913     // Clean PHI Nodes for the newly created blocks
914     for (BasicBlock *BB : BlocksToClean)
915       cleanPhiNodes(BB);
916   }
917 
918   /// For a specific ThreadingPath \p Path, create an exit path starting from
919   /// the determinator block.
920   ///
921   /// To remember the correct destination, we have to duplicate blocks
922   /// corresponding to each state. Also update the terminating instruction of
923   /// the predecessors, and phis in the successor blocks.
924   void createExitPath(DefMap &NewDefs, ThreadingPath &Path,
925                       DuplicateBlockMap &DuplicateMap,
926                       SmallSet<BasicBlock *, 16> &BlocksToClean,
927                       DomTreeUpdater *DTU) {
928     uint64_t NextState = Path.getExitValue();
929     const BasicBlock *Determinator = Path.getDeterminatorBB();
930     PathType PathBBs = Path.getPath();
931 
932     // Don't select the placeholder block in front
933     if (PathBBs.front() == Determinator)
934       PathBBs.pop_front();
935 
936     auto DetIt = std::find(PathBBs.begin(), PathBBs.end(), Determinator);
937     auto Prev = std::prev(DetIt);
938     BasicBlock *PrevBB = *Prev;
939     for (auto BBIt = DetIt; BBIt != PathBBs.end(); BBIt++) {
940       BasicBlock *BB = *BBIt;
941       BlocksToClean.insert(BB);
942 
943       // We already cloned BB for this NextState, now just update the branch
944       // and continue.
945       BasicBlock *NextBB = getClonedBB(BB, NextState, DuplicateMap);
946       if (NextBB) {
947         updatePredecessor(PrevBB, BB, NextBB, DTU);
948         PrevBB = NextBB;
949         continue;
950       }
951 
952       // Clone the BB and update the successor of Prev to jump to the new block
953       BasicBlock *NewBB = cloneBlockAndUpdatePredecessor(
954           BB, PrevBB, NextState, DuplicateMap, NewDefs, DTU);
955       DuplicateMap[BB].push_back({NewBB, NextState});
956       BlocksToClean.insert(NewBB);
957       PrevBB = NewBB;
958     }
959   }
960 
961   /// Restore SSA form after cloning blocks.
962   ///
963   /// Each cloned block creates new defs for a variable, and the uses need to be
964   /// updated to reflect this. The uses may be replaced with a cloned value, or
965   /// some derived phi instruction. Note that all uses of a value defined in the
966   /// same block were already remapped when cloning the block.
967   void updateSSA(DefMap &NewDefs) {
968     SSAUpdaterBulk SSAUpdate;
969     SmallVector<Use *, 16> UsesToRename;
970 
971     for (auto KV : NewDefs) {
972       Instruction *I = KV.first;
973       BasicBlock *BB = I->getParent();
974       std::vector<Instruction *> Cloned = KV.second;
975 
976       // Scan all uses of this instruction to see if it is used outside of its
977       // block, and if so, record them in UsesToRename.
978       for (Use &U : I->uses()) {
979         Instruction *User = cast<Instruction>(U.getUser());
980         if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
981           if (UserPN->getIncomingBlock(U) == BB)
982             continue;
983         } else if (User->getParent() == BB) {
984           continue;
985         }
986 
987         UsesToRename.push_back(&U);
988       }
989 
990       // If there are no uses outside the block, we're done with this
991       // instruction.
992       if (UsesToRename.empty())
993         continue;
994       LLVM_DEBUG(dbgs() << "DFA-JT: Renaming non-local uses of: " << *I
995                         << "\n");
996 
997       // We found a use of I outside of BB.  Rename all uses of I that are
998       // outside its block to be uses of the appropriate PHI node etc.  See
999       // ValuesInBlocks with the values we know.
1000       unsigned VarNum = SSAUpdate.AddVariable(I->getName(), I->getType());
1001       SSAUpdate.AddAvailableValue(VarNum, BB, I);
1002       for (Instruction *New : Cloned)
1003         SSAUpdate.AddAvailableValue(VarNum, New->getParent(), New);
1004 
1005       while (!UsesToRename.empty())
1006         SSAUpdate.AddUse(VarNum, UsesToRename.pop_back_val());
1007 
1008       LLVM_DEBUG(dbgs() << "\n");
1009     }
1010     // SSAUpdater handles phi placement and renaming uses with the appropriate
1011     // value.
1012     SSAUpdate.RewriteAllUses(DT);
1013   }
1014 
1015   /// Clones a basic block, and adds it to the CFG.
1016   ///
1017   /// This function also includes updating phi nodes in the successors of the
1018   /// BB, and remapping uses that were defined locally in the cloned BB.
1019   BasicBlock *cloneBlockAndUpdatePredecessor(BasicBlock *BB, BasicBlock *PrevBB,
1020                                              uint64_t NextState,
1021                                              DuplicateBlockMap &DuplicateMap,
1022                                              DefMap &NewDefs,
1023                                              DomTreeUpdater *DTU) {
1024     ValueToValueMapTy VMap;
1025     BasicBlock *NewBB = CloneBasicBlock(
1026         BB, VMap, ".jt" + std::to_string(NextState), BB->getParent());
1027     NewBB->moveAfter(BB);
1028     NumCloned++;
1029 
1030     for (Instruction &I : *NewBB) {
1031       // Do not remap operands of PHINode in case a definition in BB is an
1032       // incoming value to a phi in the same block. This incoming value will
1033       // be renamed later while restoring SSA.
1034       if (isa<PHINode>(&I))
1035         continue;
1036       RemapInstruction(&I, VMap,
1037                        RF_IgnoreMissingLocals | RF_NoModuleLevelChanges);
1038       if (AssumeInst *II = dyn_cast<AssumeInst>(&I))
1039         AC->registerAssumption(II);
1040     }
1041 
1042     updateSuccessorPhis(BB, NewBB, NextState, VMap, DuplicateMap);
1043     updatePredecessor(PrevBB, BB, NewBB, DTU);
1044     updateDefMap(NewDefs, VMap);
1045 
1046     // Add all successors to the DominatorTree
1047     SmallPtrSet<BasicBlock *, 4> SuccSet;
1048     for (auto *SuccBB : successors(NewBB)) {
1049       if (SuccSet.insert(SuccBB).second)
1050         DTU->applyUpdates({{DominatorTree::Insert, NewBB, SuccBB}});
1051     }
1052     SuccSet.clear();
1053     return NewBB;
1054   }
1055 
1056   /// Update the phi nodes in BB's successors.
1057   ///
1058   /// This means creating a new incoming value from NewBB with the new
1059   /// instruction wherever there is an incoming value from BB.
1060   void updateSuccessorPhis(BasicBlock *BB, BasicBlock *ClonedBB,
1061                            uint64_t NextState, ValueToValueMapTy &VMap,
1062                            DuplicateBlockMap &DuplicateMap) {
1063     std::vector<BasicBlock *> BlocksToUpdate;
1064 
1065     // If BB is the last block in the path, we can simply update the one case
1066     // successor that will be reached.
1067     if (BB == SwitchPaths->getSwitchBlock()) {
1068       SwitchInst *Switch = SwitchPaths->getSwitchInst();
1069       BasicBlock *NextCase = getNextCaseSuccessor(Switch, NextState);
1070       BlocksToUpdate.push_back(NextCase);
1071       BasicBlock *ClonedSucc = getClonedBB(NextCase, NextState, DuplicateMap);
1072       if (ClonedSucc)
1073         BlocksToUpdate.push_back(ClonedSucc);
1074     }
1075     // Otherwise update phis in all successors.
1076     else {
1077       for (BasicBlock *Succ : successors(BB)) {
1078         BlocksToUpdate.push_back(Succ);
1079 
1080         // Check if a successor has already been cloned for the particular exit
1081         // value. In this case if a successor was already cloned, the phi nodes
1082         // in the cloned block should be updated directly.
1083         BasicBlock *ClonedSucc = getClonedBB(Succ, NextState, DuplicateMap);
1084         if (ClonedSucc)
1085           BlocksToUpdate.push_back(ClonedSucc);
1086       }
1087     }
1088 
1089     // If there is a phi with an incoming value from BB, create a new incoming
1090     // value for the new predecessor ClonedBB. The value will either be the same
1091     // value from BB or a cloned value.
1092     for (BasicBlock *Succ : BlocksToUpdate) {
1093       for (auto II = Succ->begin(); PHINode *Phi = dyn_cast<PHINode>(II);
1094            ++II) {
1095         Value *Incoming = Phi->getIncomingValueForBlock(BB);
1096         if (Incoming) {
1097           if (isa<Constant>(Incoming)) {
1098             Phi->addIncoming(Incoming, ClonedBB);
1099             continue;
1100           }
1101           Value *ClonedVal = VMap[Incoming];
1102           if (ClonedVal)
1103             Phi->addIncoming(ClonedVal, ClonedBB);
1104           else
1105             Phi->addIncoming(Incoming, ClonedBB);
1106         }
1107       }
1108     }
1109   }
1110 
1111   /// Sets the successor of PrevBB to be NewBB instead of OldBB. Note that all
1112   /// other successors are kept as well.
1113   void updatePredecessor(BasicBlock *PrevBB, BasicBlock *OldBB,
1114                          BasicBlock *NewBB, DomTreeUpdater *DTU) {
1115     // When a path is reused, there is a chance that predecessors were already
1116     // updated before. Check if the predecessor needs to be updated first.
1117     if (!isPredecessor(OldBB, PrevBB))
1118       return;
1119 
1120     Instruction *PrevTerm = PrevBB->getTerminator();
1121     for (unsigned Idx = 0; Idx < PrevTerm->getNumSuccessors(); Idx++) {
1122       if (PrevTerm->getSuccessor(Idx) == OldBB) {
1123         OldBB->removePredecessor(PrevBB, /* KeepOneInputPHIs = */ true);
1124         PrevTerm->setSuccessor(Idx, NewBB);
1125       }
1126     }
1127     DTU->applyUpdates({{DominatorTree::Delete, PrevBB, OldBB},
1128                        {DominatorTree::Insert, PrevBB, NewBB}});
1129   }
1130 
1131   /// Add new value mappings to the DefMap to keep track of all new definitions
1132   /// for a particular instruction. These will be used while updating SSA form.
1133   void updateDefMap(DefMap &NewDefs, ValueToValueMapTy &VMap) {
1134     SmallVector<std::pair<Instruction *, Instruction *>> NewDefsVector;
1135     NewDefsVector.reserve(VMap.size());
1136 
1137     for (auto Entry : VMap) {
1138       Instruction *Inst =
1139           dyn_cast<Instruction>(const_cast<Value *>(Entry.first));
1140       if (!Inst || !Entry.second || isa<BranchInst>(Inst) ||
1141           isa<SwitchInst>(Inst)) {
1142         continue;
1143       }
1144 
1145       Instruction *Cloned = dyn_cast<Instruction>(Entry.second);
1146       if (!Cloned)
1147         continue;
1148 
1149       NewDefsVector.push_back({Inst, Cloned});
1150     }
1151 
1152     // Sort the defs to get deterministic insertion order into NewDefs.
1153     sort(NewDefsVector, [](const auto &LHS, const auto &RHS) {
1154       if (LHS.first == RHS.first)
1155         return LHS.second->comesBefore(RHS.second);
1156       return LHS.first->comesBefore(RHS.first);
1157     });
1158 
1159     for (const auto &KV : NewDefsVector)
1160       NewDefs[KV.first].push_back(KV.second);
1161   }
1162 
1163   /// Update the last branch of a particular cloned path to point to the correct
1164   /// case successor.
1165   ///
1166   /// Note that this is an optional step and would have been done in later
1167   /// optimizations, but it makes the CFG significantly easier to work with.
1168   void updateLastSuccessor(ThreadingPath &TPath,
1169                            DuplicateBlockMap &DuplicateMap,
1170                            DomTreeUpdater *DTU) {
1171     uint64_t NextState = TPath.getExitValue();
1172     BasicBlock *BB = TPath.getPath().back();
1173     BasicBlock *LastBlock = getClonedBB(BB, NextState, DuplicateMap);
1174 
1175     // Note multiple paths can end at the same block so check that it is not
1176     // updated yet
1177     if (!isa<SwitchInst>(LastBlock->getTerminator()))
1178       return;
1179     SwitchInst *Switch = cast<SwitchInst>(LastBlock->getTerminator());
1180     BasicBlock *NextCase = getNextCaseSuccessor(Switch, NextState);
1181 
1182     std::vector<DominatorTree::UpdateType> DTUpdates;
1183     SmallPtrSet<BasicBlock *, 4> SuccSet;
1184     for (BasicBlock *Succ : successors(LastBlock)) {
1185       if (Succ != NextCase && SuccSet.insert(Succ).second)
1186         DTUpdates.push_back({DominatorTree::Delete, LastBlock, Succ});
1187     }
1188 
1189     Switch->eraseFromParent();
1190     BranchInst::Create(NextCase, LastBlock);
1191 
1192     DTU->applyUpdates(DTUpdates);
1193   }
1194 
1195   /// After cloning blocks, some of the phi nodes have extra incoming values
1196   /// that are no longer used. This function removes them.
1197   void cleanPhiNodes(BasicBlock *BB) {
1198     // If BB is no longer reachable, remove any remaining phi nodes
1199     if (pred_empty(BB)) {
1200       std::vector<PHINode *> PhiToRemove;
1201       for (auto II = BB->begin(); PHINode *Phi = dyn_cast<PHINode>(II); ++II) {
1202         PhiToRemove.push_back(Phi);
1203       }
1204       for (PHINode *PN : PhiToRemove) {
1205         PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
1206         PN->eraseFromParent();
1207       }
1208       return;
1209     }
1210 
1211     // Remove any incoming values that come from an invalid predecessor
1212     for (auto II = BB->begin(); PHINode *Phi = dyn_cast<PHINode>(II); ++II) {
1213       std::vector<BasicBlock *> BlocksToRemove;
1214       for (BasicBlock *IncomingBB : Phi->blocks()) {
1215         if (!isPredecessor(BB, IncomingBB))
1216           BlocksToRemove.push_back(IncomingBB);
1217       }
1218       for (BasicBlock *BB : BlocksToRemove)
1219         Phi->removeIncomingValue(BB);
1220     }
1221   }
1222 
1223   /// Checks if BB was already cloned for a particular next state value. If it
1224   /// was then it returns this cloned block, and otherwise null.
1225   BasicBlock *getClonedBB(BasicBlock *BB, uint64_t NextState,
1226                           DuplicateBlockMap &DuplicateMap) {
1227     CloneList ClonedBBs = DuplicateMap[BB];
1228 
1229     // Find an entry in the CloneList with this NextState. If it exists then
1230     // return the corresponding BB
1231     auto It = llvm::find_if(ClonedBBs, [NextState](const ClonedBlock &C) {
1232       return C.State == NextState;
1233     });
1234     return It != ClonedBBs.end() ? (*It).BB : nullptr;
1235   }
1236 
1237   /// Helper to get the successor corresponding to a particular case value for
1238   /// a switch statement.
1239   BasicBlock *getNextCaseSuccessor(SwitchInst *Switch, uint64_t NextState) {
1240     BasicBlock *NextCase = nullptr;
1241     for (auto Case : Switch->cases()) {
1242       if (Case.getCaseValue()->getZExtValue() == NextState) {
1243         NextCase = Case.getCaseSuccessor();
1244         break;
1245       }
1246     }
1247     if (!NextCase)
1248       NextCase = Switch->getDefaultDest();
1249     return NextCase;
1250   }
1251 
1252   /// Returns true if IncomingBB is a predecessor of BB.
1253   bool isPredecessor(BasicBlock *BB, BasicBlock *IncomingBB) {
1254     return llvm::find(predecessors(BB), IncomingBB) != pred_end(BB);
1255   }
1256 
1257   AllSwitchPaths *SwitchPaths;
1258   DominatorTree *DT;
1259   AssumptionCache *AC;
1260   TargetTransformInfo *TTI;
1261   OptimizationRemarkEmitter *ORE;
1262   SmallPtrSet<const Value *, 32> EphValues;
1263   std::vector<ThreadingPath> TPaths;
1264 };
1265 
1266 bool DFAJumpThreading::run(Function &F) {
1267   LLVM_DEBUG(dbgs() << "\nDFA Jump threading: " << F.getName() << "\n");
1268 
1269   if (F.hasOptSize()) {
1270     LLVM_DEBUG(dbgs() << "Skipping due to the 'minsize' attribute\n");
1271     return false;
1272   }
1273 
1274   if (ClViewCfgBefore)
1275     F.viewCFG();
1276 
1277   SmallVector<AllSwitchPaths, 2> ThreadableLoops;
1278   bool MadeChanges = false;
1279 
1280   for (BasicBlock &BB : F) {
1281     auto *SI = dyn_cast<SwitchInst>(BB.getTerminator());
1282     if (!SI)
1283       continue;
1284 
1285     LLVM_DEBUG(dbgs() << "\nCheck if SwitchInst in BB " << BB.getName()
1286                       << " is a candidate\n");
1287     MainSwitch Switch(SI, ORE);
1288 
1289     if (!Switch.getInstr())
1290       continue;
1291 
1292     LLVM_DEBUG(dbgs() << "\nSwitchInst in BB " << BB.getName() << " is a "
1293                       << "candidate for jump threading\n");
1294     LLVM_DEBUG(SI->dump());
1295 
1296     unfoldSelectInstrs(DT, Switch.getSelectInsts());
1297     if (!Switch.getSelectInsts().empty())
1298       MadeChanges = true;
1299 
1300     AllSwitchPaths SwitchPaths(&Switch, ORE);
1301     SwitchPaths.run();
1302 
1303     if (SwitchPaths.getNumThreadingPaths() > 0) {
1304       ThreadableLoops.push_back(SwitchPaths);
1305 
1306       // For the time being limit this optimization to occurring once in a
1307       // function since it can change the CFG significantly. This is not a
1308       // strict requirement but it can cause buggy behavior if there is an
1309       // overlap of blocks in different opportunities. There is a lot of room to
1310       // experiment with catching more opportunities here.
1311       break;
1312     }
1313   }
1314 
1315   SmallPtrSet<const Value *, 32> EphValues;
1316   if (ThreadableLoops.size() > 0)
1317     CodeMetrics::collectEphemeralValues(&F, AC, EphValues);
1318 
1319   for (AllSwitchPaths SwitchPaths : ThreadableLoops) {
1320     TransformDFA Transform(&SwitchPaths, DT, AC, TTI, ORE, EphValues);
1321     Transform.run();
1322     MadeChanges = true;
1323   }
1324 
1325 #ifdef EXPENSIVE_CHECKS
1326   assert(DT->verify(DominatorTree::VerificationLevel::Full));
1327   verifyFunction(F, &dbgs());
1328 #endif
1329 
1330   return MadeChanges;
1331 }
1332 
1333 } // end anonymous namespace
1334 
1335 /// Integrate with the new Pass Manager
1336 PreservedAnalyses DFAJumpThreadingPass::run(Function &F,
1337                                             FunctionAnalysisManager &AM) {
1338   AssumptionCache &AC = AM.getResult<AssumptionAnalysis>(F);
1339   DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
1340   TargetTransformInfo &TTI = AM.getResult<TargetIRAnalysis>(F);
1341   OptimizationRemarkEmitter ORE(&F);
1342 
1343   if (!DFAJumpThreading(&AC, &DT, &TTI, &ORE).run(F))
1344     return PreservedAnalyses::all();
1345 
1346   PreservedAnalyses PA;
1347   PA.preserve<DominatorTreeAnalysis>();
1348   return PA;
1349 }
1350