1 //===- DFAJumpThreading.cpp - Threads a switch statement inside a loop ----===//
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
7 //===----------------------------------------------------------------------===//
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.
16 // case1 case2 case3 case1 case2 case3
18 // determinator det.2 det.3 det.1
20 // sw.bb.2 sw.bb.3 sw.bb.1
21 // br case2 br case3 br case1ยง
23 // Definitions and Terminology:
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 ]
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.
35 // The basic block that determines the next state of the DFA.
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.
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.
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.
58 //===----------------------------------------------------------------------===//
60 #include "llvm/Transforms/Scalar/DFAJumpThreading.h"
61 #include "llvm/ADT/APInt.h"
62 #include "llvm/ADT/DenseMap.h"
63 #include "llvm/ADT/DepthFirstIterator.h"
64 #include "llvm/ADT/SmallSet.h"
65 #include "llvm/ADT/Statistic.h"
66 #include "llvm/Analysis/AssumptionCache.h"
67 #include "llvm/Analysis/CodeMetrics.h"
68 #include "llvm/Analysis/LoopIterator.h"
69 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
70 #include "llvm/Analysis/TargetTransformInfo.h"
71 #include "llvm/IR/CFG.h"
72 #include "llvm/IR/Constants.h"
73 #include "llvm/IR/IntrinsicInst.h"
74 #include "llvm/IR/Verifier.h"
75 #include "llvm/InitializePasses.h"
76 #include "llvm/Pass.h"
77 #include "llvm/Support/CommandLine.h"
78 #include "llvm/Support/Debug.h"
79 #include "llvm/Transforms/Scalar.h"
80 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
81 #include "llvm/Transforms/Utils/Cloning.h"
82 #include "llvm/Transforms/Utils/SSAUpdaterBulk.h"
83 #include "llvm/Transforms/Utils/ValueMapper.h"
89 #define DEBUG_TYPE "dfa-jump-threading"
91 STATISTIC(NumTransforms
, "Number of transformations done");
92 STATISTIC(NumCloned
, "Number of blocks cloned");
93 STATISTIC(NumPaths
, "Number of individual paths threaded");
96 ClViewCfgBefore("dfa-jump-view-cfg-before",
97 cl::desc("View the CFG before DFA Jump Threading"),
98 cl::Hidden
, cl::init(false));
100 static cl::opt
<unsigned> MaxPathLength(
101 "dfa-max-path-length",
102 cl::desc("Max number of blocks searched to find a threading path"),
103 cl::Hidden
, cl::init(20));
105 static cl::opt
<unsigned>
106 CostThreshold("dfa-cost-threshold",
107 cl::desc("Maximum cost accepted for the transformation"),
108 cl::Hidden
, cl::init(50));
112 class SelectInstToUnfold
{
117 SelectInstToUnfold(SelectInst
*SI
, PHINode
*SIUse
) : SI(SI
), SIUse(SIUse
) {}
119 SelectInst
*getInst() { return SI
; }
120 PHINode
*getUse() { return SIUse
; }
122 explicit operator bool() const { return SI
&& SIUse
; }
125 void unfold(DomTreeUpdater
*DTU
, SelectInstToUnfold SIToUnfold
,
126 std::vector
<SelectInstToUnfold
> *NewSIsToUnfold
,
127 std::vector
<BasicBlock
*> *NewBBs
);
129 class DFAJumpThreading
{
131 DFAJumpThreading(AssumptionCache
*AC
, DominatorTree
*DT
,
132 TargetTransformInfo
*TTI
, OptimizationRemarkEmitter
*ORE
)
133 : AC(AC
), DT(DT
), TTI(TTI
), ORE(ORE
) {}
135 bool run(Function
&F
);
139 unfoldSelectInstrs(DominatorTree
*DT
,
140 const SmallVector
<SelectInstToUnfold
, 4> &SelectInsts
) {
141 DomTreeUpdater
DTU(DT
, DomTreeUpdater::UpdateStrategy::Eager
);
142 SmallVector
<SelectInstToUnfold
, 4> Stack
;
143 for (SelectInstToUnfold SIToUnfold
: SelectInsts
)
144 Stack
.push_back(SIToUnfold
);
146 while (!Stack
.empty()) {
147 SelectInstToUnfold SIToUnfold
= Stack
.back();
150 std::vector
<SelectInstToUnfold
> NewSIsToUnfold
;
151 std::vector
<BasicBlock
*> NewBBs
;
152 unfold(&DTU
, SIToUnfold
, &NewSIsToUnfold
, &NewBBs
);
154 // Put newly discovered select instructions into the work list.
155 for (const SelectInstToUnfold
&NewSIToUnfold
: NewSIsToUnfold
)
156 Stack
.push_back(NewSIToUnfold
);
162 TargetTransformInfo
*TTI
;
163 OptimizationRemarkEmitter
*ORE
;
166 class DFAJumpThreadingLegacyPass
: public FunctionPass
{
168 static char ID
; // Pass identification
169 DFAJumpThreadingLegacyPass() : FunctionPass(ID
) {}
171 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
172 AU
.addRequired
<AssumptionCacheTracker
>();
173 AU
.addRequired
<DominatorTreeWrapperPass
>();
174 AU
.addPreserved
<DominatorTreeWrapperPass
>();
175 AU
.addRequired
<TargetTransformInfoWrapperPass
>();
176 AU
.addRequired
<OptimizationRemarkEmitterWrapperPass
>();
179 bool runOnFunction(Function
&F
) override
{
183 AssumptionCache
*AC
=
184 &getAnalysis
<AssumptionCacheTracker
>().getAssumptionCache(F
);
185 DominatorTree
*DT
= &getAnalysis
<DominatorTreeWrapperPass
>().getDomTree();
186 TargetTransformInfo
*TTI
=
187 &getAnalysis
<TargetTransformInfoWrapperPass
>().getTTI(F
);
188 OptimizationRemarkEmitter
*ORE
=
189 &getAnalysis
<OptimizationRemarkEmitterWrapperPass
>().getORE();
191 return DFAJumpThreading(AC
, DT
, TTI
, ORE
).run(F
);
194 } // end anonymous namespace
196 char DFAJumpThreadingLegacyPass::ID
= 0;
197 INITIALIZE_PASS_BEGIN(DFAJumpThreadingLegacyPass
, "dfa-jump-threading",
198 "DFA Jump Threading", false, false)
199 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker
)
200 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass
)
201 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass
)
202 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass
)
203 INITIALIZE_PASS_END(DFAJumpThreadingLegacyPass
, "dfa-jump-threading",
204 "DFA Jump Threading", false, false)
206 // Public interface to the DFA Jump Threading pass
207 FunctionPass
*llvm::createDFAJumpThreadingPass() {
208 return new DFAJumpThreadingLegacyPass();
213 /// Create a new basic block and sink \p SIToSink into it.
214 void createBasicBlockAndSinkSelectInst(
215 DomTreeUpdater
*DTU
, SelectInst
*SI
, PHINode
*SIUse
, SelectInst
*SIToSink
,
216 BasicBlock
*EndBlock
, StringRef NewBBName
, BasicBlock
**NewBlock
,
217 BranchInst
**NewBranch
, std::vector
<SelectInstToUnfold
> *NewSIsToUnfold
,
218 std::vector
<BasicBlock
*> *NewBBs
) {
219 assert(SIToSink
->hasOneUse());
222 *NewBlock
= BasicBlock::Create(SI
->getContext(), NewBBName
,
223 EndBlock
->getParent(), EndBlock
);
224 NewBBs
->push_back(*NewBlock
);
225 *NewBranch
= BranchInst::Create(EndBlock
, *NewBlock
);
226 SIToSink
->moveBefore(*NewBranch
);
227 NewSIsToUnfold
->push_back(SelectInstToUnfold(SIToSink
, SIUse
));
228 DTU
->applyUpdates({{DominatorTree::Insert
, *NewBlock
, EndBlock
}});
231 /// Unfold the select instruction held in \p SIToUnfold by replacing it with
234 /// Put newly discovered select instructions into \p NewSIsToUnfold. Put newly
235 /// created basic blocks into \p NewBBs.
237 /// TODO: merge it with CodeGenPrepare::optimizeSelectInst() if possible.
238 void unfold(DomTreeUpdater
*DTU
, SelectInstToUnfold SIToUnfold
,
239 std::vector
<SelectInstToUnfold
> *NewSIsToUnfold
,
240 std::vector
<BasicBlock
*> *NewBBs
) {
241 SelectInst
*SI
= SIToUnfold
.getInst();
242 PHINode
*SIUse
= SIToUnfold
.getUse();
243 BasicBlock
*StartBlock
= SI
->getParent();
244 BasicBlock
*EndBlock
= SIUse
->getParent();
245 BranchInst
*StartBlockTerm
=
246 dyn_cast
<BranchInst
>(StartBlock
->getTerminator());
248 assert(StartBlockTerm
&& StartBlockTerm
->isUnconditional());
249 assert(SI
->hasOneUse());
251 // These are the new basic blocks for the conditional branch.
252 // At least one will become an actual new basic block.
253 BasicBlock
*TrueBlock
= nullptr;
254 BasicBlock
*FalseBlock
= nullptr;
255 BranchInst
*TrueBranch
= nullptr;
256 BranchInst
*FalseBranch
= nullptr;
258 // Sink select instructions to be able to unfold them later.
259 if (SelectInst
*SIOp
= dyn_cast
<SelectInst
>(SI
->getTrueValue())) {
260 createBasicBlockAndSinkSelectInst(DTU
, SI
, SIUse
, SIOp
, EndBlock
,
261 "si.unfold.true", &TrueBlock
, &TrueBranch
,
262 NewSIsToUnfold
, NewBBs
);
264 if (SelectInst
*SIOp
= dyn_cast
<SelectInst
>(SI
->getFalseValue())) {
265 createBasicBlockAndSinkSelectInst(DTU
, SI
, SIUse
, SIOp
, EndBlock
,
266 "si.unfold.false", &FalseBlock
,
267 &FalseBranch
, NewSIsToUnfold
, NewBBs
);
270 // If there was nothing to sink, then arbitrarily choose the 'false' side
271 // for a new input value to the PHI.
272 if (!TrueBlock
&& !FalseBlock
) {
273 FalseBlock
= BasicBlock::Create(SI
->getContext(), "si.unfold.false",
274 EndBlock
->getParent(), EndBlock
);
275 NewBBs
->push_back(FalseBlock
);
276 BranchInst::Create(EndBlock
, FalseBlock
);
277 DTU
->applyUpdates({{DominatorTree::Insert
, FalseBlock
, EndBlock
}});
280 // Insert the real conditional branch based on the original condition.
281 // If we did not create a new block for one of the 'true' or 'false' paths
282 // of the condition, it means that side of the branch goes to the end block
283 // directly and the path originates from the start block from the point of
284 // view of the new PHI.
285 BasicBlock
*TT
= EndBlock
;
286 BasicBlock
*FT
= EndBlock
;
287 if (TrueBlock
&& FalseBlock
) {
292 // Update the phi node of SI.
293 SIUse
->removeIncomingValue(StartBlock
, /* DeletePHIIfEmpty = */ false);
294 SIUse
->addIncoming(SI
->getTrueValue(), TrueBlock
);
295 SIUse
->addIncoming(SI
->getFalseValue(), FalseBlock
);
297 // Update any other PHI nodes in EndBlock.
298 for (PHINode
&Phi
: EndBlock
->phis()) {
300 Phi
.addIncoming(Phi
.getIncomingValueForBlock(StartBlock
), TrueBlock
);
301 Phi
.addIncoming(Phi
.getIncomingValueForBlock(StartBlock
), FalseBlock
);
305 BasicBlock
*NewBlock
= nullptr;
306 Value
*SIOp1
= SI
->getTrueValue();
307 Value
*SIOp2
= SI
->getFalseValue();
309 // A triangle pointing right.
311 NewBlock
= FalseBlock
;
314 // A triangle pointing left.
316 NewBlock
= TrueBlock
;
318 std::swap(SIOp1
, SIOp2
);
321 // Update the phi node of SI.
322 for (unsigned Idx
= 0; Idx
< SIUse
->getNumIncomingValues(); ++Idx
) {
323 if (SIUse
->getIncomingBlock(Idx
) == StartBlock
)
324 SIUse
->setIncomingValue(Idx
, SIOp1
);
326 SIUse
->addIncoming(SIOp2
, NewBlock
);
328 // Update any other PHI nodes in EndBlock.
329 for (auto II
= EndBlock
->begin(); PHINode
*Phi
= dyn_cast
<PHINode
>(II
);
332 Phi
->addIncoming(Phi
->getIncomingValueForBlock(StartBlock
), NewBlock
);
335 StartBlockTerm
->eraseFromParent();
336 BranchInst::Create(TT
, FT
, SI
->getCondition(), StartBlock
);
337 DTU
->applyUpdates({{DominatorTree::Insert
, StartBlock
, TT
},
338 {DominatorTree::Insert
, StartBlock
, FT
}});
340 // The select is now dead.
341 SI
->eraseFromParent();
346 uint64_t State
; ///< \p State corresponds to the next value of a switch stmnt.
349 typedef std::deque
<BasicBlock
*> PathType
;
350 typedef std::vector
<PathType
> PathsType
;
351 typedef SmallPtrSet
<const BasicBlock
*, 8> VisitedBlocks
;
352 typedef std::vector
<ClonedBlock
> CloneList
;
354 // This data structure keeps track of all blocks that have been cloned. If two
355 // different ThreadingPaths clone the same block for a certain state it should
356 // be reused, and it can be looked up in this map.
357 typedef DenseMap
<BasicBlock
*, CloneList
> DuplicateBlockMap
;
359 // This map keeps track of all the new definitions for an instruction. This
360 // information is needed when restoring SSA form after cloning blocks.
361 typedef DenseMap
<Instruction
*, std::vector
<Instruction
*>> DefMap
;
363 inline raw_ostream
&operator<<(raw_ostream
&OS
, const PathType
&Path
) {
365 for (const BasicBlock
*BB
: Path
) {
368 raw_string_ostream(BBName
) << BB
->getName();
370 raw_string_ostream(BBName
) << BB
;
377 /// ThreadingPath is a path in the control flow of a loop that can be threaded
378 /// by cloning necessary basic blocks and replacing conditional branches with
379 /// unconditional ones. A threading path includes a list of basic blocks, the
380 /// exit state, and the block that determines the next state.
381 struct ThreadingPath
{
382 /// Exit value is DFA's exit state for the given path.
383 uint64_t getExitValue() const { return ExitVal
; }
384 void setExitValue(const ConstantInt
*V
) {
385 ExitVal
= V
->getZExtValue();
388 bool isExitValueSet() const { return IsExitValSet
; }
390 /// Determinator is the basic block that determines the next state of the DFA.
391 const BasicBlock
*getDeterminatorBB() const { return DBB
; }
392 void setDeterminator(const BasicBlock
*BB
) { DBB
= BB
; }
394 /// Path is a list of basic blocks.
395 const PathType
&getPath() const { return Path
; }
396 void setPath(const PathType
&NewPath
) { Path
= NewPath
; }
398 void print(raw_ostream
&OS
) const {
399 OS
<< Path
<< " [ " << ExitVal
<< ", " << DBB
->getName() << " ]";
405 const BasicBlock
*DBB
= nullptr;
406 bool IsExitValSet
= false;
410 inline raw_ostream
&operator<<(raw_ostream
&OS
, const ThreadingPath
&TPath
) {
417 MainSwitch(SwitchInst
*SI
, OptimizationRemarkEmitter
*ORE
) {
418 if (isPredictable(SI
)) {
422 return OptimizationRemarkMissed(DEBUG_TYPE
, "SwitchNotPredictable", SI
)
423 << "Switch instruction is not predictable.";
428 virtual ~MainSwitch() = default;
430 SwitchInst
*getInstr() const { return Instr
; }
431 const SmallVector
<SelectInstToUnfold
, 4> getSelectInsts() {
436 /// Do a use-def chain traversal. Make sure the value of the switch variable
437 /// is always a known constant. This means that all conditional jumps based on
438 /// switch variable can be converted to unconditional jumps.
439 bool isPredictable(const SwitchInst
*SI
) {
440 std::deque
<Instruction
*> Q
;
441 SmallSet
<Value
*, 16> SeenValues
;
444 Value
*FirstDef
= SI
->getOperand(0);
445 auto *Inst
= dyn_cast
<Instruction
>(FirstDef
);
447 // If this is a function argument or another non-instruction, then give up.
448 // We are interested in loop local variables.
452 // Require the first definition to be a PHINode
453 if (!isa
<PHINode
>(Inst
))
456 LLVM_DEBUG(dbgs() << "\tisPredictable() FirstDef: " << *Inst
<< "\n");
459 SeenValues
.insert(FirstDef
);
462 Instruction
*Current
= Q
.front();
465 if (auto *Phi
= dyn_cast
<PHINode
>(Current
)) {
466 for (Value
*Incoming
: Phi
->incoming_values()) {
467 if (!isPredictableValue(Incoming
, SeenValues
))
469 addInstToQueue(Incoming
, Q
, SeenValues
);
471 LLVM_DEBUG(dbgs() << "\tisPredictable() phi: " << *Phi
<< "\n");
472 } else if (SelectInst
*SelI
= dyn_cast
<SelectInst
>(Current
)) {
473 if (!isValidSelectInst(SelI
))
475 if (!isPredictableValue(SelI
->getTrueValue(), SeenValues
) ||
476 !isPredictableValue(SelI
->getFalseValue(), SeenValues
)) {
479 addInstToQueue(SelI
->getTrueValue(), Q
, SeenValues
);
480 addInstToQueue(SelI
->getFalseValue(), Q
, SeenValues
);
481 LLVM_DEBUG(dbgs() << "\tisPredictable() select: " << *SelI
<< "\n");
482 if (auto *SelIUse
= dyn_cast
<PHINode
>(SelI
->user_back()))
483 SelectInsts
.push_back(SelectInstToUnfold(SelI
, SelIUse
));
485 // If it is neither a phi nor a select, then we give up.
493 bool isPredictableValue(Value
*InpVal
, SmallSet
<Value
*, 16> &SeenValues
) {
494 if (SeenValues
.find(InpVal
) != SeenValues
.end())
497 if (isa
<ConstantInt
>(InpVal
))
500 // If this is a function argument or another non-instruction, then give up.
501 if (!isa
<Instruction
>(InpVal
))
507 void addInstToQueue(Value
*Val
, std::deque
<Instruction
*> &Q
,
508 SmallSet
<Value
*, 16> &SeenValues
) {
509 if (SeenValues
.find(Val
) != SeenValues
.end())
511 if (Instruction
*I
= dyn_cast
<Instruction
>(Val
))
513 SeenValues
.insert(Val
);
516 bool isValidSelectInst(SelectInst
*SI
) {
517 if (!SI
->hasOneUse())
520 Instruction
*SIUse
= dyn_cast
<Instruction
>(SI
->user_back());
521 // The use of the select inst should be either a phi or another select.
522 if (!SIUse
&& !(isa
<PHINode
>(SIUse
) || isa
<SelectInst
>(SIUse
)))
525 BasicBlock
*SIBB
= SI
->getParent();
527 // Currently, we can only expand select instructions in basic blocks with
529 BranchInst
*SITerm
= dyn_cast
<BranchInst
>(SIBB
->getTerminator());
530 if (!SITerm
|| !SITerm
->isUnconditional())
533 if (isa
<PHINode
>(SIUse
) &&
534 SIBB
->getSingleSuccessor() != cast
<Instruction
>(SIUse
)->getParent())
537 // If select will not be sunk during unfolding, and it is in the same basic
538 // block as another state defining select, then cannot unfold both.
539 for (SelectInstToUnfold SIToUnfold
: SelectInsts
) {
540 SelectInst
*PrevSI
= SIToUnfold
.getInst();
541 if (PrevSI
->getTrueValue() != SI
&& PrevSI
->getFalseValue() != SI
&&
542 PrevSI
->getParent() == SI
->getParent())
549 SwitchInst
*Instr
= nullptr;
550 SmallVector
<SelectInstToUnfold
, 4> SelectInsts
;
553 struct AllSwitchPaths
{
554 AllSwitchPaths(const MainSwitch
*MSwitch
, OptimizationRemarkEmitter
*ORE
)
555 : Switch(MSwitch
->getInstr()), SwitchBlock(Switch
->getParent()),
558 std::vector
<ThreadingPath
> &getThreadingPaths() { return TPaths
; }
559 unsigned getNumThreadingPaths() { return TPaths
.size(); }
560 SwitchInst
*getSwitchInst() { return Switch
; }
561 BasicBlock
*getSwitchBlock() { return SwitchBlock
; }
564 VisitedBlocks Visited
;
565 PathsType LoopPaths
= paths(SwitchBlock
, Visited
, /* PathDepth = */ 1);
566 StateDefMap StateDef
= getStateDefMap();
568 for (PathType Path
: LoopPaths
) {
571 const BasicBlock
*PrevBB
= Path
.back();
572 for (const BasicBlock
*BB
: Path
) {
573 if (StateDef
.count(BB
) != 0) {
574 const PHINode
*Phi
= dyn_cast
<PHINode
>(StateDef
[BB
]);
575 assert(Phi
&& "Expected a state-defining instr to be a phi node.");
577 const Value
*V
= Phi
->getIncomingValueForBlock(PrevBB
);
578 if (const ConstantInt
*C
= dyn_cast
<const ConstantInt
>(V
)) {
579 TPath
.setExitValue(C
);
580 TPath
.setDeterminator(BB
);
585 // Switch block is the determinator, this is the final exit value.
586 if (TPath
.isExitValueSet() && BB
== Path
.front())
592 if (TPath
.isExitValueSet())
593 TPaths
.push_back(TPath
);
598 // Value: an instruction that defines a switch state;
599 // Key: the parent basic block of that instruction.
600 typedef DenseMap
<const BasicBlock
*, const PHINode
*> StateDefMap
;
602 PathsType
paths(BasicBlock
*BB
, VisitedBlocks
&Visited
,
603 unsigned PathDepth
) const {
606 // Stop exploring paths after visiting MaxPathLength blocks
607 if (PathDepth
> MaxPathLength
) {
609 return OptimizationRemarkAnalysis(DEBUG_TYPE
, "MaxPathLengthReached",
611 << "Exploration stopped after visiting MaxPathLength="
612 << ore::NV("MaxPathLength", MaxPathLength
) << " blocks.";
619 // Some blocks have multiple edges to the same successor, and this set
620 // is used to prevent a duplicate path from being generated
621 SmallSet
<BasicBlock
*, 4> Successors
;
622 for (BasicBlock
*Succ
: successors(BB
)) {
623 if (!Successors
.insert(Succ
).second
)
626 // Found a cycle through the SwitchBlock
627 if (Succ
== SwitchBlock
) {
632 // We have encountered a cycle, do not get caught in it
633 if (Visited
.contains(Succ
))
636 PathsType SuccPaths
= paths(Succ
, Visited
, PathDepth
+ 1);
637 for (PathType Path
: SuccPaths
) {
638 PathType
NewPath(Path
);
639 NewPath
.push_front(BB
);
640 Res
.push_back(NewPath
);
643 // This block could now be visited again from a different predecessor. Note
644 // that this will result in exponential runtime. Subpaths could possibly be
645 // cached but it takes a lot of memory to store them.
650 /// Walk the use-def chain and collect all the state-defining instructions.
651 StateDefMap
getStateDefMap() const {
654 Value
*FirstDef
= Switch
->getOperand(0);
656 assert(isa
<PHINode
>(FirstDef
) && "After select unfolding, all state "
657 "definitions are expected to be phi "
660 SmallVector
<PHINode
*, 8> Stack
;
661 Stack
.push_back(dyn_cast
<PHINode
>(FirstDef
));
662 SmallSet
<Value
*, 16> SeenValues
;
664 while (!Stack
.empty()) {
665 PHINode
*CurPhi
= Stack
.back();
668 Res
[CurPhi
->getParent()] = CurPhi
;
669 SeenValues
.insert(CurPhi
);
671 for (Value
*Incoming
: CurPhi
->incoming_values()) {
672 if (Incoming
== FirstDef
|| isa
<ConstantInt
>(Incoming
) ||
673 SeenValues
.find(Incoming
) != SeenValues
.end()) {
677 assert(isa
<PHINode
>(Incoming
) && "After select unfolding, all state "
678 "definitions are expected to be phi "
681 Stack
.push_back(cast
<PHINode
>(Incoming
));
689 BasicBlock
*SwitchBlock
;
690 OptimizationRemarkEmitter
*ORE
;
691 std::vector
<ThreadingPath
> TPaths
;
694 struct TransformDFA
{
695 TransformDFA(AllSwitchPaths
*SwitchPaths
, DominatorTree
*DT
,
696 AssumptionCache
*AC
, TargetTransformInfo
*TTI
,
697 OptimizationRemarkEmitter
*ORE
,
698 SmallPtrSet
<const Value
*, 32> EphValues
)
699 : SwitchPaths(SwitchPaths
), DT(DT
), AC(AC
), TTI(TTI
), ORE(ORE
),
700 EphValues(EphValues
) {}
703 if (isLegalAndProfitableToTransform()) {
704 createAllExitPaths();
710 /// This function performs both a legality check and profitability check at
711 /// the same time since it is convenient to do so. It iterates through all
712 /// blocks that will be cloned, and keeps track of the duplication cost. It
713 /// also returns false if it is illegal to clone some required block.
714 bool isLegalAndProfitableToTransform() {
716 SwitchInst
*Switch
= SwitchPaths
->getSwitchInst();
718 // Note that DuplicateBlockMap is not being used as intended here. It is
719 // just being used to ensure (BB, State) pairs are only counted once.
720 DuplicateBlockMap DuplicateMap
;
722 for (ThreadingPath
&TPath
: SwitchPaths
->getThreadingPaths()) {
723 PathType PathBBs
= TPath
.getPath();
724 uint64_t NextState
= TPath
.getExitValue();
725 const BasicBlock
*Determinator
= TPath
.getDeterminatorBB();
727 // Update Metrics for the Switch block, this is always cloned
728 BasicBlock
*BB
= SwitchPaths
->getSwitchBlock();
729 BasicBlock
*VisitedBB
= getClonedBB(BB
, NextState
, DuplicateMap
);
731 Metrics
.analyzeBasicBlock(BB
, *TTI
, EphValues
);
732 DuplicateMap
[BB
].push_back({BB
, NextState
});
735 // If the Switch block is the Determinator, then we can continue since
736 // this is the only block that is cloned and we already counted for it.
737 if (PathBBs
.front() == Determinator
)
740 // Otherwise update Metrics for all blocks that will be cloned. If any
741 // block is already cloned and would be reused, don't double count it.
742 auto DetIt
= std::find(PathBBs
.begin(), PathBBs
.end(), Determinator
);
743 for (auto BBIt
= DetIt
; BBIt
!= PathBBs
.end(); BBIt
++) {
745 VisitedBB
= getClonedBB(BB
, NextState
, DuplicateMap
);
748 Metrics
.analyzeBasicBlock(BB
, *TTI
, EphValues
);
749 DuplicateMap
[BB
].push_back({BB
, NextState
});
752 if (Metrics
.notDuplicatable
) {
753 LLVM_DEBUG(dbgs() << "DFA Jump Threading: Not jump threading, contains "
754 << "non-duplicatable instructions.\n");
756 return OptimizationRemarkMissed(DEBUG_TYPE
, "NonDuplicatableInst",
758 << "Contains non-duplicatable instructions.";
763 if (Metrics
.convergent
) {
764 LLVM_DEBUG(dbgs() << "DFA Jump Threading: Not jump threading, contains "
765 << "convergent instructions.\n");
767 return OptimizationRemarkMissed(DEBUG_TYPE
, "ConvergentInst", Switch
)
768 << "Contains convergent instructions.";
774 unsigned DuplicationCost
= 0;
776 unsigned JumpTableSize
= 0;
777 TTI
->getEstimatedNumberOfCaseClusters(*Switch
, JumpTableSize
, nullptr,
779 if (JumpTableSize
== 0) {
780 // Factor in the number of conditional branches reduced from jump
781 // threading. Assume that lowering the switch block is implemented by
782 // using binary search, hence the LogBase2().
783 unsigned CondBranches
=
784 APInt(32, Switch
->getNumSuccessors()).ceilLogBase2();
785 DuplicationCost
= Metrics
.NumInsts
/ CondBranches
;
787 // Compared with jump tables, the DFA optimizer removes an indirect branch
788 // on each loop iteration, thus making branch prediction more precise. The
789 // more branch targets there are, the more likely it is for the branch
790 // predictor to make a mistake, and the more benefit there is in the DFA
791 // optimizer. Thus, the more branch targets there are, the lower is the
792 // cost of the DFA opt.
793 DuplicationCost
= Metrics
.NumInsts
/ JumpTableSize
;
796 LLVM_DEBUG(dbgs() << "\nDFA Jump Threading: Cost to jump thread block "
797 << SwitchPaths
->getSwitchBlock()->getName()
798 << " is: " << DuplicationCost
<< "\n\n");
800 if (DuplicationCost
> CostThreshold
) {
801 LLVM_DEBUG(dbgs() << "Not jump threading, duplication cost exceeds the "
802 << "cost threshold.\n");
804 return OptimizationRemarkMissed(DEBUG_TYPE
, "NotProfitable", Switch
)
805 << "Duplication cost exceeds the cost threshold (cost="
806 << ore::NV("Cost", DuplicationCost
)
807 << ", threshold=" << ore::NV("Threshold", CostThreshold
) << ").";
813 return OptimizationRemark(DEBUG_TYPE
, "JumpThreaded", Switch
)
814 << "Switch statement jump-threaded.";
820 /// Transform each threading path to effectively jump thread the DFA.
821 void createAllExitPaths() {
822 DomTreeUpdater
DTU(*DT
, DomTreeUpdater::UpdateStrategy::Eager
);
824 // Move the switch block to the end of the path, since it will be duplicated
825 BasicBlock
*SwitchBlock
= SwitchPaths
->getSwitchBlock();
826 for (ThreadingPath
&TPath
: SwitchPaths
->getThreadingPaths()) {
827 LLVM_DEBUG(dbgs() << TPath
<< "\n");
828 PathType
NewPath(TPath
.getPath());
829 NewPath
.push_back(SwitchBlock
);
830 TPath
.setPath(NewPath
);
833 // Transform the ThreadingPaths and keep track of the cloned values
834 DuplicateBlockMap DuplicateMap
;
837 SmallSet
<BasicBlock
*, 16> BlocksToClean
;
838 for (BasicBlock
*BB
: successors(SwitchBlock
))
839 BlocksToClean
.insert(BB
);
841 for (ThreadingPath
&TPath
: SwitchPaths
->getThreadingPaths()) {
842 createExitPath(NewDefs
, TPath
, DuplicateMap
, BlocksToClean
, &DTU
);
846 // After all paths are cloned, now update the last successor of the cloned
847 // path so it skips over the switch statement
848 for (ThreadingPath
&TPath
: SwitchPaths
->getThreadingPaths())
849 updateLastSuccessor(TPath
, DuplicateMap
, &DTU
);
851 // For each instruction that was cloned and used outside, update its uses
854 // Clean PHI Nodes for the newly created blocks
855 for (BasicBlock
*BB
: BlocksToClean
)
859 /// For a specific ThreadingPath \p Path, create an exit path starting from
860 /// the determinator block.
862 /// To remember the correct destination, we have to duplicate blocks
863 /// corresponding to each state. Also update the terminating instruction of
864 /// the predecessors, and phis in the successor blocks.
865 void createExitPath(DefMap
&NewDefs
, ThreadingPath
&Path
,
866 DuplicateBlockMap
&DuplicateMap
,
867 SmallSet
<BasicBlock
*, 16> &BlocksToClean
,
868 DomTreeUpdater
*DTU
) {
869 uint64_t NextState
= Path
.getExitValue();
870 const BasicBlock
*Determinator
= Path
.getDeterminatorBB();
871 PathType PathBBs
= Path
.getPath();
873 // Don't select the placeholder block in front
874 if (PathBBs
.front() == Determinator
)
877 auto DetIt
= std::find(PathBBs
.begin(), PathBBs
.end(), Determinator
);
878 auto Prev
= std::prev(DetIt
);
879 BasicBlock
*PrevBB
= *Prev
;
880 for (auto BBIt
= DetIt
; BBIt
!= PathBBs
.end(); BBIt
++) {
881 BasicBlock
*BB
= *BBIt
;
882 BlocksToClean
.insert(BB
);
884 // We already cloned BB for this NextState, now just update the branch
886 BasicBlock
*NextBB
= getClonedBB(BB
, NextState
, DuplicateMap
);
888 updatePredecessor(PrevBB
, BB
, NextBB
, DTU
);
893 // Clone the BB and update the successor of Prev to jump to the new block
894 BasicBlock
*NewBB
= cloneBlockAndUpdatePredecessor(
895 BB
, PrevBB
, NextState
, DuplicateMap
, NewDefs
, DTU
);
896 DuplicateMap
[BB
].push_back({NewBB
, NextState
});
897 BlocksToClean
.insert(NewBB
);
902 /// Restore SSA form after cloning blocks.
904 /// Each cloned block creates new defs for a variable, and the uses need to be
905 /// updated to reflect this. The uses may be replaced with a cloned value, or
906 /// some derived phi instruction. Note that all uses of a value defined in the
907 /// same block were already remapped when cloning the block.
908 void updateSSA(DefMap
&NewDefs
) {
909 SSAUpdaterBulk SSAUpdate
;
910 SmallVector
<Use
*, 16> UsesToRename
;
912 for (auto KV
: NewDefs
) {
913 Instruction
*I
= KV
.first
;
914 BasicBlock
*BB
= I
->getParent();
915 std::vector
<Instruction
*> Cloned
= KV
.second
;
917 // Scan all uses of this instruction to see if it is used outside of its
918 // block, and if so, record them in UsesToRename.
919 for (Use
&U
: I
->uses()) {
920 Instruction
*User
= cast
<Instruction
>(U
.getUser());
921 if (PHINode
*UserPN
= dyn_cast
<PHINode
>(User
)) {
922 if (UserPN
->getIncomingBlock(U
) == BB
)
924 } else if (User
->getParent() == BB
) {
928 UsesToRename
.push_back(&U
);
931 // If there are no uses outside the block, we're done with this
933 if (UsesToRename
.empty())
935 LLVM_DEBUG(dbgs() << "DFA-JT: Renaming non-local uses of: " << *I
938 // We found a use of I outside of BB. Rename all uses of I that are
939 // outside its block to be uses of the appropriate PHI node etc. See
940 // ValuesInBlocks with the values we know.
941 unsigned VarNum
= SSAUpdate
.AddVariable(I
->getName(), I
->getType());
942 SSAUpdate
.AddAvailableValue(VarNum
, BB
, I
);
943 for (Instruction
*New
: Cloned
)
944 SSAUpdate
.AddAvailableValue(VarNum
, New
->getParent(), New
);
946 while (!UsesToRename
.empty())
947 SSAUpdate
.AddUse(VarNum
, UsesToRename
.pop_back_val());
949 LLVM_DEBUG(dbgs() << "\n");
951 // SSAUpdater handles phi placement and renaming uses with the appropriate
953 SSAUpdate
.RewriteAllUses(DT
);
956 /// Clones a basic block, and adds it to the CFG.
958 /// This function also includes updating phi nodes in the successors of the
959 /// BB, and remapping uses that were defined locally in the cloned BB.
960 BasicBlock
*cloneBlockAndUpdatePredecessor(BasicBlock
*BB
, BasicBlock
*PrevBB
,
962 DuplicateBlockMap
&DuplicateMap
,
964 DomTreeUpdater
*DTU
) {
965 ValueToValueMapTy VMap
;
966 BasicBlock
*NewBB
= CloneBasicBlock(
967 BB
, VMap
, ".jt" + std::to_string(NextState
), BB
->getParent());
968 NewBB
->moveAfter(BB
);
971 for (Instruction
&I
: *NewBB
) {
972 // Do not remap operands of PHINode in case a definition in BB is an
973 // incoming value to a phi in the same block. This incoming value will
974 // be renamed later while restoring SSA.
975 if (isa
<PHINode
>(&I
))
977 RemapInstruction(&I
, VMap
,
978 RF_IgnoreMissingLocals
| RF_NoModuleLevelChanges
);
979 if (AssumeInst
*II
= dyn_cast
<AssumeInst
>(&I
))
980 AC
->registerAssumption(II
);
983 updateSuccessorPhis(BB
, NewBB
, NextState
, VMap
, DuplicateMap
);
984 updatePredecessor(PrevBB
, BB
, NewBB
, DTU
);
985 updateDefMap(NewDefs
, VMap
);
987 // Add all successors to the DominatorTree
988 SmallPtrSet
<BasicBlock
*, 4> SuccSet
;
989 for (auto *SuccBB
: successors(NewBB
)) {
990 if (SuccSet
.insert(SuccBB
).second
)
991 DTU
->applyUpdates({{DominatorTree::Insert
, NewBB
, SuccBB
}});
997 /// Update the phi nodes in BB's successors.
999 /// This means creating a new incoming value from NewBB with the new
1000 /// instruction wherever there is an incoming value from BB.
1001 void updateSuccessorPhis(BasicBlock
*BB
, BasicBlock
*ClonedBB
,
1002 uint64_t NextState
, ValueToValueMapTy
&VMap
,
1003 DuplicateBlockMap
&DuplicateMap
) {
1004 std::vector
<BasicBlock
*> BlocksToUpdate
;
1006 // If BB is the last block in the path, we can simply update the one case
1007 // successor that will be reached.
1008 if (BB
== SwitchPaths
->getSwitchBlock()) {
1009 SwitchInst
*Switch
= SwitchPaths
->getSwitchInst();
1010 BasicBlock
*NextCase
= getNextCaseSuccessor(Switch
, NextState
);
1011 BlocksToUpdate
.push_back(NextCase
);
1012 BasicBlock
*ClonedSucc
= getClonedBB(NextCase
, NextState
, DuplicateMap
);
1014 BlocksToUpdate
.push_back(ClonedSucc
);
1016 // Otherwise update phis in all successors.
1018 for (BasicBlock
*Succ
: successors(BB
)) {
1019 BlocksToUpdate
.push_back(Succ
);
1021 // Check if a successor has already been cloned for the particular exit
1022 // value. In this case if a successor was already cloned, the phi nodes
1023 // in the cloned block should be updated directly.
1024 BasicBlock
*ClonedSucc
= getClonedBB(Succ
, NextState
, DuplicateMap
);
1026 BlocksToUpdate
.push_back(ClonedSucc
);
1030 // If there is a phi with an incoming value from BB, create a new incoming
1031 // value for the new predecessor ClonedBB. The value will either be the same
1032 // value from BB or a cloned value.
1033 for (BasicBlock
*Succ
: BlocksToUpdate
) {
1034 for (auto II
= Succ
->begin(); PHINode
*Phi
= dyn_cast
<PHINode
>(II
);
1036 Value
*Incoming
= Phi
->getIncomingValueForBlock(BB
);
1038 if (isa
<Constant
>(Incoming
)) {
1039 Phi
->addIncoming(Incoming
, ClonedBB
);
1042 Value
*ClonedVal
= VMap
[Incoming
];
1044 Phi
->addIncoming(ClonedVal
, ClonedBB
);
1046 Phi
->addIncoming(Incoming
, ClonedBB
);
1052 /// Sets the successor of PrevBB to be NewBB instead of OldBB. Note that all
1053 /// other successors are kept as well.
1054 void updatePredecessor(BasicBlock
*PrevBB
, BasicBlock
*OldBB
,
1055 BasicBlock
*NewBB
, DomTreeUpdater
*DTU
) {
1056 // When a path is reused, there is a chance that predecessors were already
1057 // updated before. Check if the predecessor needs to be updated first.
1058 if (!isPredecessor(OldBB
, PrevBB
))
1061 Instruction
*PrevTerm
= PrevBB
->getTerminator();
1062 for (unsigned Idx
= 0; Idx
< PrevTerm
->getNumSuccessors(); Idx
++) {
1063 if (PrevTerm
->getSuccessor(Idx
) == OldBB
) {
1064 OldBB
->removePredecessor(PrevBB
, /* KeepOneInputPHIs = */ true);
1065 PrevTerm
->setSuccessor(Idx
, NewBB
);
1068 DTU
->applyUpdates({{DominatorTree::Delete
, PrevBB
, OldBB
},
1069 {DominatorTree::Insert
, PrevBB
, NewBB
}});
1072 /// Add new value mappings to the DefMap to keep track of all new definitions
1073 /// for a particular instruction. These will be used while updating SSA form.
1074 void updateDefMap(DefMap
&NewDefs
, ValueToValueMapTy
&VMap
) {
1075 for (auto Entry
: VMap
) {
1077 dyn_cast
<Instruction
>(const_cast<Value
*>(Entry
.first
));
1078 if (!Inst
|| !Entry
.second
|| isa
<BranchInst
>(Inst
) ||
1079 isa
<SwitchInst
>(Inst
)) {
1083 Instruction
*Cloned
= dyn_cast
<Instruction
>(Entry
.second
);
1087 if (NewDefs
.find(Inst
) == NewDefs
.end())
1088 NewDefs
[Inst
] = {Cloned
};
1090 NewDefs
[Inst
].push_back(Cloned
);
1094 /// Update the last branch of a particular cloned path to point to the correct
1097 /// Note that this is an optional step and would have been done in later
1098 /// optimizations, but it makes the CFG significantly easier to work with.
1099 void updateLastSuccessor(ThreadingPath
&TPath
,
1100 DuplicateBlockMap
&DuplicateMap
,
1101 DomTreeUpdater
*DTU
) {
1102 uint64_t NextState
= TPath
.getExitValue();
1103 BasicBlock
*BB
= TPath
.getPath().back();
1104 BasicBlock
*LastBlock
= getClonedBB(BB
, NextState
, DuplicateMap
);
1106 // Note multiple paths can end at the same block so check that it is not
1108 if (!isa
<SwitchInst
>(LastBlock
->getTerminator()))
1110 SwitchInst
*Switch
= cast
<SwitchInst
>(LastBlock
->getTerminator());
1111 BasicBlock
*NextCase
= getNextCaseSuccessor(Switch
, NextState
);
1113 std::vector
<DominatorTree::UpdateType
> DTUpdates
;
1114 SmallPtrSet
<BasicBlock
*, 4> SuccSet
;
1115 for (BasicBlock
*Succ
: successors(LastBlock
)) {
1116 if (Succ
!= NextCase
&& SuccSet
.insert(Succ
).second
)
1117 DTUpdates
.push_back({DominatorTree::Delete
, LastBlock
, Succ
});
1120 Switch
->eraseFromParent();
1121 BranchInst::Create(NextCase
, LastBlock
);
1123 DTU
->applyUpdates(DTUpdates
);
1126 /// After cloning blocks, some of the phi nodes have extra incoming values
1127 /// that are no longer used. This function removes them.
1128 void cleanPhiNodes(BasicBlock
*BB
) {
1129 // If BB is no longer reachable, remove any remaining phi nodes
1130 if (pred_empty(BB
)) {
1131 std::vector
<PHINode
*> PhiToRemove
;
1132 for (auto II
= BB
->begin(); PHINode
*Phi
= dyn_cast
<PHINode
>(II
); ++II
) {
1133 PhiToRemove
.push_back(Phi
);
1135 for (PHINode
*PN
: PhiToRemove
) {
1136 PN
->replaceAllUsesWith(UndefValue::get(PN
->getType()));
1137 PN
->eraseFromParent();
1142 // Remove any incoming values that come from an invalid predecessor
1143 for (auto II
= BB
->begin(); PHINode
*Phi
= dyn_cast
<PHINode
>(II
); ++II
) {
1144 std::vector
<BasicBlock
*> BlocksToRemove
;
1145 for (BasicBlock
*IncomingBB
: Phi
->blocks()) {
1146 if (!isPredecessor(BB
, IncomingBB
))
1147 BlocksToRemove
.push_back(IncomingBB
);
1149 for (BasicBlock
*BB
: BlocksToRemove
)
1150 Phi
->removeIncomingValue(BB
);
1154 /// Checks if BB was already cloned for a particular next state value. If it
1155 /// was then it returns this cloned block, and otherwise null.
1156 BasicBlock
*getClonedBB(BasicBlock
*BB
, uint64_t NextState
,
1157 DuplicateBlockMap
&DuplicateMap
) {
1158 CloneList ClonedBBs
= DuplicateMap
[BB
];
1160 // Find an entry in the CloneList with this NextState. If it exists then
1161 // return the corresponding BB
1162 auto It
= llvm::find_if(ClonedBBs
, [NextState
](const ClonedBlock
&C
) {
1163 return C
.State
== NextState
;
1165 return It
!= ClonedBBs
.end() ? (*It
).BB
: nullptr;
1168 /// Helper to get the successor corresponding to a particular case value for
1169 /// a switch statement.
1170 BasicBlock
*getNextCaseSuccessor(SwitchInst
*Switch
, uint64_t NextState
) {
1171 BasicBlock
*NextCase
= nullptr;
1172 for (auto Case
: Switch
->cases()) {
1173 if (Case
.getCaseValue()->getZExtValue() == NextState
) {
1174 NextCase
= Case
.getCaseSuccessor();
1179 NextCase
= Switch
->getDefaultDest();
1183 /// Returns true if IncomingBB is a predecessor of BB.
1184 bool isPredecessor(BasicBlock
*BB
, BasicBlock
*IncomingBB
) {
1185 return llvm::find(predecessors(BB
), IncomingBB
) != pred_end(BB
);
1188 AllSwitchPaths
*SwitchPaths
;
1190 AssumptionCache
*AC
;
1191 TargetTransformInfo
*TTI
;
1192 OptimizationRemarkEmitter
*ORE
;
1193 SmallPtrSet
<const Value
*, 32> EphValues
;
1194 std::vector
<ThreadingPath
> TPaths
;
1197 bool DFAJumpThreading::run(Function
&F
) {
1198 LLVM_DEBUG(dbgs() << "\nDFA Jump threading: " << F
.getName() << "\n");
1200 if (F
.hasOptSize()) {
1201 LLVM_DEBUG(dbgs() << "Skipping due to the 'minsize' attribute\n");
1205 if (ClViewCfgBefore
)
1208 SmallVector
<AllSwitchPaths
, 2> ThreadableLoops
;
1209 bool MadeChanges
= false;
1211 for (BasicBlock
&BB
: F
) {
1212 auto *SI
= dyn_cast
<SwitchInst
>(BB
.getTerminator());
1216 LLVM_DEBUG(dbgs() << "\nCheck if SwitchInst in BB " << BB
.getName()
1217 << " is predictable\n");
1218 MainSwitch
Switch(SI
, ORE
);
1220 if (!Switch
.getInstr())
1223 LLVM_DEBUG(dbgs() << "\nSwitchInst in BB " << BB
.getName() << " is a "
1224 << "candidate for jump threading\n");
1225 LLVM_DEBUG(SI
->dump());
1227 unfoldSelectInstrs(DT
, Switch
.getSelectInsts());
1228 if (!Switch
.getSelectInsts().empty())
1231 AllSwitchPaths
SwitchPaths(&Switch
, ORE
);
1234 if (SwitchPaths
.getNumThreadingPaths() > 0) {
1235 ThreadableLoops
.push_back(SwitchPaths
);
1237 // For the time being limit this optimization to occurring once in a
1238 // function since it can change the CFG significantly. This is not a
1239 // strict requirement but it can cause buggy behavior if there is an
1240 // overlap of blocks in different opportunities. There is a lot of room to
1241 // experiment with catching more opportunities here.
1246 SmallPtrSet
<const Value
*, 32> EphValues
;
1247 if (ThreadableLoops
.size() > 0)
1248 CodeMetrics::collectEphemeralValues(&F
, AC
, EphValues
);
1250 for (AllSwitchPaths SwitchPaths
: ThreadableLoops
) {
1251 TransformDFA
Transform(&SwitchPaths
, DT
, AC
, TTI
, ORE
, EphValues
);
1256 #ifdef EXPENSIVE_CHECKS
1257 assert(DT
->verify(DominatorTree::VerificationLevel::Full
));
1258 verifyFunction(F
, &dbgs());
1264 } // end anonymous namespace
1266 /// Integrate with the new Pass Manager
1267 PreservedAnalyses
DFAJumpThreadingPass::run(Function
&F
,
1268 FunctionAnalysisManager
&AM
) {
1269 AssumptionCache
&AC
= AM
.getResult
<AssumptionAnalysis
>(F
);
1270 DominatorTree
&DT
= AM
.getResult
<DominatorTreeAnalysis
>(F
);
1271 TargetTransformInfo
&TTI
= AM
.getResult
<TargetIRAnalysis
>(F
);
1272 OptimizationRemarkEmitter
ORE(&F
);
1274 if (!DFAJumpThreading(&AC
, &DT
, &TTI
, &ORE
).run(F
))
1275 return PreservedAnalyses::all();
1277 PreservedAnalyses PA
;
1278 PA
.preserve
<DominatorTreeAnalysis
>();