81#ifdef EXPENSIVE_CHECKS
87#define DEBUG_TYPE "dfa-jump-threading"
89STATISTIC(NumTransforms,
"Number of transformations done");
91STATISTIC(NumPaths,
"Number of individual paths threaded");
96 cl::desc(
"View the CFG before DFA Jump Threading"),
100 "dfa-early-exit-heuristic",
101 cl::desc(
"Exit early if an unpredictable value come from the same loop"),
105 "dfa-max-path-length",
106 cl::desc(
"Max number of blocks searched to find a threading path"),
110 "dfa-max-num-visited-paths",
112 "Max number of blocks visited while enumerating paths around a switch"),
117 cl::desc(
"Max number of paths enumerated around a switch"),
122 cl::desc(
"Maximum cost accepted for the transformation"),
126 "dfa-max-cloned-rate",
128 "Maximum cloned instructions rate accepted for the transformation"),
133 cl::desc(
"Maximum unduplicated blocks with outer uses "
134 "accepted for the transformation"),
142class SelectInstToUnfold {
149 SelectInst *getInst() {
return SI; }
150 PHINode *getUse() {
return SIUse; }
152 explicit operator bool()
const {
return SI && SIUse; }
155class DFAJumpThreading {
157 DFAJumpThreading(AssumptionCache *AC, DomTreeUpdater *DTU, LoopInfo *LI,
158 TargetTransformInfo *TTI, OptimizationRemarkEmitter *ORE)
159 : AC(AC), DTU(DTU), LI(LI), TTI(TTI), ORE(ORE) {}
161 bool run(Function &
F);
169 while (!
Stack.empty()) {
170 SelectInstToUnfold SIToUnfold =
Stack.pop_back_val();
172 std::vector<SelectInstToUnfold> NewSIsToUnfold;
173 std::vector<BasicBlock *> NewBBs;
174 unfold(DTU, LI, SIToUnfold, &NewSIsToUnfold, &NewBBs);
181 static void unfold(DomTreeUpdater *DTU, LoopInfo *LI,
182 SelectInstToUnfold SIToUnfold,
183 std::vector<SelectInstToUnfold> *NewSIsToUnfold,
184 std::vector<BasicBlock *> *NewBBs);
189 TargetTransformInfo *TTI;
190 OptimizationRemarkEmitter *ORE;
202 SelectInstToUnfold SIToUnfold,
203 std::vector<SelectInstToUnfold> *NewSIsToUnfold,
204 std::vector<BasicBlock *> *NewBBs) {
205 SelectInst *
SI = SIToUnfold.getInst();
206 PHINode *SIUse = SIToUnfold.getUse();
211 if (UncondBrInst *StartBlockTerm =
216 SI->getContext(), Twine(
SI->getName(),
".si.unfold.false"),
218 NewBBs->push_back(NewBlock);
220 DTU->
applyUpdates({{DominatorTree::Insert, NewBlock, EndBlock}});
227 Value *SIOp1 =
SI->getTrueValue();
228 Value *SIOp2 =
SI->getFalseValue();
231 Twine(SIOp2->
getName(),
".si.unfold.phi"),
236 for (PHINode &Phi : EndBlock->
phis()) {
239 Phi.addIncoming(
Phi.getIncomingValueForBlock(StartBlock), NewBlock);
248 Twine(
SI->getName(),
".si.unfold.phi"),
251 if (Pred != StartBlock && Pred != NewBlock)
262 NewSIsToUnfold->push_back(SelectInstToUnfold(OpSi, SIUse));
264 NewSIsToUnfold->push_back(SelectInstToUnfold(OpSi, NewPhi));
267 StartBlockTerm->eraseFromParent();
271 BI->setMetadata(LLVMContext::MD_prof,
272 SI->getMetadata(LLVMContext::MD_prof));
273 DTU->
applyUpdates({{DominatorTree::Insert, StartBlock, NewBlock}});
277 SI->getContext(), Twine(
SI->getName(),
".si.unfold.true"),
280 SI->getContext(), Twine(
SI->getName(),
".si.unfold.false"),
283 NewBBs->push_back(NewBlockT);
284 NewBBs->push_back(NewBlockF);
309 BI->setMetadata(LLVMContext::MD_prof,
310 SI->getMetadata(LLVMContext::MD_prof));
311 DTU->
applyUpdates({{DominatorTree::Insert, NewBlockT, NewBlockF},
312 {DominatorTree::Insert, NewBlockT, EndBlock},
313 {DominatorTree::Insert, NewBlockF, EndBlock}});
328 NewSIsToUnfold->push_back(SelectInstToUnfold(TrueSI, NewPhiT));
330 NewSIsToUnfold->push_back(SelectInstToUnfold(FalseSi, NewPhiF));
337 for (PHINode &Phi : EndBlock->
phis()) {
340 Phi.addIncoming(
Phi.getIncomingValueForBlock(StartBlock), NewBlockT);
341 Phi.addIncoming(
Phi.getIncomingValueForBlock(StartBlock), NewBlockF);
342 Phi.removeIncomingValue(StartBlock);
348 unsigned SuccNum = CondBr->
getSuccessor(1) == EndBlock ? 1 : 0;
350 DTU->
applyUpdates({{DominatorTree::Delete, StartBlock, EndBlock},
351 {DominatorTree::Insert, StartBlock, NewBlockT}});
356 for (BasicBlock *NewBB : *NewBBs)
357 L->addBasicBlockToLoop(NewBB, *LI);
361 assert(
SI->use_empty() &&
"Select must be dead now");
362 SI->eraseFromParent();
389 OS <<
"< " <<
llvm::join(BBNames,
", ") <<
" >";
398struct ThreadingPath {
400 APInt getExitValue()
const {
return ExitVal; }
401 void setExitValue(
const ConstantInt *V) {
402 ExitVal =
V->getValue();
405 void setExitValue(
const APInt &V) {
409 bool isExitValueSet()
const {
return IsExitValSet; }
412 const BasicBlock *getDeterminatorBB()
const {
return DBB; }
413 void setDeterminator(
const BasicBlock *BB) { DBB = BB; }
416 const PathType &getPath()
const {
return Path; }
417 void setPath(
const PathType &NewPath) { Path = NewPath; }
418 void push_back(BasicBlock *BB) { Path.push_back(BB); }
419 void push_front(BasicBlock *BB) { Path.push_front(BB); }
420 void appendExcludingFirst(
const PathType &OtherPath) {
424 void print(raw_ostream &OS)
const {
432 bool IsExitValSet =
false;
436inline raw_ostream &
operator<<(raw_ostream &OS,
const ThreadingPath &TPath) {
443 MainSwitch(SwitchInst *SI, LoopInfo *LI, OptimizationRemarkEmitter *ORE)
449 return OptimizationRemarkMissed(
DEBUG_TYPE,
"SwitchNotPredictable", SI)
450 <<
"Switch instruction is not predictable.";
455 virtual ~MainSwitch() =
default;
457 SwitchInst *getInstr()
const {
return Instr; }
468 std::deque<std::pair<Value *, BasicBlock *>> Q;
469 SmallPtrSet<Value *, 16> SeenValues;
472 Value *SICond =
SI->getCondition();
482 addToQueue(SICond,
nullptr, Q, SeenValues);
485 Value *Current = Q.front().first;
486 BasicBlock *CurrentIncomingBB = Q.front().second;
490 for (BasicBlock *IncomingBB :
Phi->blocks()) {
491 Value *Incoming =
Phi->getIncomingValueForBlock(IncomingBB);
492 addToQueue(Incoming, IncomingBB, Q, SeenValues);
496 if (!isValidSelectInst(SelI))
498 addToQueue(SelI->getTrueValue(), CurrentIncomingBB, Q, SeenValues);
499 addToQueue(SelI->getFalseValue(), CurrentIncomingBB, Q, SeenValues);
502 SelectInsts.push_back(SelectInstToUnfold(SelI, SelIUse));
520 <<
"\tExiting early due to unpredictability heuristic.\n");
531 void addToQueue(
Value *Val, BasicBlock *BB,
532 std::deque<std::pair<Value *, BasicBlock *>> &Q,
533 SmallPtrSet<Value *, 16> &SeenValues) {
534 if (SeenValues.
insert(Val).second)
535 Q.push_back({Val, BB});
538 bool isValidSelectInst(SelectInst *SI) {
539 if (!
SI->hasOneUse())
564 for (SelectInstToUnfold SIToUnfold : SelectInsts) {
565 SelectInst *PrevSI = SIToUnfold.getInst();
575 SwitchInst *Instr =
nullptr;
579struct AllSwitchPaths {
580 AllSwitchPaths(
const MainSwitch *MSwitch, OptimizationRemarkEmitter *ORE,
581 LoopInfo *LI, Loop *L)
582 : Switch(MSwitch->getInstr()), SwitchBlock(Switch->
getParent()), ORE(ORE),
583 LI(LI), SwitchOuterLoop(
L) {}
585 std::vector<ThreadingPath> &getThreadingPaths() {
return TPaths; }
586 unsigned getNumThreadingPaths() {
return TPaths.size(); }
587 SwitchInst *getSwitchInst() {
return Switch; }
588 BasicBlock *getSwitchBlock() {
return SwitchBlock; }
598 typedef DenseMap<const BasicBlock *, const PHINode *> StateDefMap;
599 std::vector<ThreadingPath> getPathsFromStateDefMap(StateDefMap &StateDef,
602 unsigned PathsLimit) {
603 std::vector<ThreadingPath> Res;
604 auto *PhiBB =
Phi->getParent();
608 for (
auto *IncomingBB :
Phi->blocks()) {
609 if (Res.size() >= PathsLimit)
611 if (!UniqueBlocks.
insert(IncomingBB).second)
613 if (!SwitchOuterLoop->
contains(IncomingBB))
616 Value *IncomingValue =
Phi->getIncomingValueForBlock(IncomingBB);
620 if (PhiBB == SwitchBlock &&
623 ThreadingPath NewPath;
624 NewPath.setDeterminator(PhiBB);
625 NewPath.setExitValue(
C);
627 if (IncomingBB != SwitchBlock) {
631 NewPath.push_back(IncomingBB);
633 NewPath.push_back(PhiBB);
634 Res.push_back(NewPath);
638 if (VB.
contains(IncomingBB) || IncomingBB == SwitchBlock)
644 auto *IncomingPhiDefBB = IncomingPhi->getParent();
645 if (!StateDef.contains(IncomingPhiDefBB))
649 if (IncomingPhiDefBB == IncomingBB) {
650 assert(PathsLimit > Res.size());
651 std::vector<ThreadingPath> PredPaths = getPathsFromStateDefMap(
652 StateDef, IncomingPhi, VB, PathsLimit - Res.size());
653 for (ThreadingPath &Path : PredPaths) {
654 Path.push_back(PhiBB);
655 Res.push_back(std::move(Path));
665 assert(PathsLimit > Res.size());
666 auto InterPathLimit = PathsLimit - Res.size();
667 IntermediatePaths = paths(IncomingPhiDefBB, IncomingBB, VB,
669 if (IntermediatePaths.empty())
672 assert(InterPathLimit >= IntermediatePaths.size());
673 auto PredPathLimit = InterPathLimit / IntermediatePaths.size();
674 std::vector<ThreadingPath> PredPaths =
675 getPathsFromStateDefMap(StateDef, IncomingPhi, VB, PredPathLimit);
676 for (
const ThreadingPath &Path : PredPaths) {
677 for (
const PathType &IPath : IntermediatePaths) {
678 ThreadingPath NewPath(Path);
679 NewPath.appendExcludingFirst(IPath);
680 NewPath.push_back(PhiBB);
681 Res.push_back(NewPath);
690 unsigned PathDepth,
unsigned PathsLimit) {
696 return OptimizationRemarkAnalysis(
DEBUG_TYPE,
"MaxPathLengthReached",
698 <<
"Exploration stopped after visiting MaxPathLength="
715 SmallPtrSet<BasicBlock *, 4> Successors;
717 if (Res.size() >= PathsLimit)
719 if (!Successors.
insert(Succ).second)
724 Res.push_back({BB, ToBB});
734 if (Succ == CurrLoop->getHeader())
740 assert(PathsLimit > Res.size());
742 paths(Succ, ToBB, Visited, PathDepth + 1, PathsLimit - Res.size());
757 StateDefMap getStateDefMap()
const {
760 assert(FirstDef &&
"The first definition must be a phi.");
763 Stack.push_back(FirstDef);
764 SmallPtrSet<Value *, 16> SeenValues;
766 while (!
Stack.empty()) {
767 PHINode *CurPhi =
Stack.pop_back_val();
770 SeenValues.
insert(CurPhi);
772 for (BasicBlock *IncomingBB : CurPhi->
blocks()) {
773 PHINode *IncomingPhi =
777 bool IsOutsideLoops = !SwitchOuterLoop->
contains(IncomingBB);
778 if (SeenValues.
contains(IncomingPhi) || IsOutsideLoops)
781 Stack.push_back(IncomingPhi);
790 StateDefMap StateDef = getStateDefMap();
791 if (StateDef.empty()) {
793 return OptimizationRemarkMissed(
DEBUG_TYPE,
"SwitchNotPredictable",
795 <<
"Switch instruction is not predictable.";
801 auto *SwitchPhiDefBB = SwitchPhi->getParent();
804 std::vector<ThreadingPath> PathsToPhiDef =
805 getPathsFromStateDefMap(StateDef, SwitchPhi, VB,
MaxNumPaths);
806 if (SwitchPhiDefBB == SwitchBlock || PathsToPhiDef.empty()) {
807 TPaths = std::move(PathsToPhiDef);
812 auto PathsLimit =
MaxNumPaths / PathsToPhiDef.size();
815 paths(SwitchPhiDefBB, SwitchBlock, VB, 1, PathsLimit);
816 if (PathsToSwitchBB.empty())
819 std::vector<ThreadingPath> TempList;
820 for (
const ThreadingPath &Path : PathsToPhiDef) {
821 SmallPtrSet<BasicBlock *, 32> PathSet(
Path.getPath().begin(),
822 Path.getPath().end());
823 for (
const PathType &PathToSw : PathsToSwitchBB) {
825 [&](
const BasicBlock *BB) {
return PathSet.contains(BB); }))
827 ThreadingPath PathCopy(Path);
828 PathCopy.appendExcludingFirst(PathToSw);
829 TempList.push_back(PathCopy);
832 TPaths = std::move(TempList);
837 BasicBlock *getNextCaseSuccessor(
const APInt &NextState) {
839 if (CaseValToDest.empty()) {
840 for (
auto Case : Switch->
cases()) {
841 APInt CaseVal = Case.getCaseValue()->getValue();
842 CaseValToDest[CaseVal] = Case.getCaseSuccessor();
846 auto SuccIt = CaseValToDest.find(NextState);
854 SmallDenseMap<BasicBlock *, APInt> DestToState;
855 for (ThreadingPath &Path : TPaths) {
856 APInt NextState =
Path.getExitValue();
857 BasicBlock *Dest = getNextCaseSuccessor(NextState);
861 if (NextState != StateIt->second) {
862 LLVM_DEBUG(
dbgs() <<
"Next state in " << Path <<
" is equivalent to "
863 << StateIt->second <<
"\n");
864 Path.setExitValue(StateIt->second);
869 unsigned NumVisited = 0;
872 OptimizationRemarkEmitter *ORE;
873 std::vector<ThreadingPath> TPaths;
874 DenseMap<APInt, BasicBlock *> CaseValToDest;
876 Loop *SwitchOuterLoop;
880 TransformDFA(AllSwitchPaths *SwitchPaths, DomTreeUpdater *DTU,
881 AssumptionCache *AC, TargetTransformInfo *
TTI,
882 OptimizationRemarkEmitter *ORE,
883 SmallPtrSet<const Value *, 32> EphValues)
884 : SwitchPaths(SwitchPaths), DTU(DTU), AC(AC),
TTI(
TTI), ORE(ORE),
885 EphValues(EphValues) {}
888 if (isLegalAndProfitableToTransform()) {
889 createAllExitPaths();
901 bool isLegalAndProfitableToTransform() {
903 uint64_t NumClonedInst = 0;
904 SwitchInst *
Switch = SwitchPaths->getSwitchInst();
907 if (
Switch->getNumSuccessors() <= 1)
913 for (ThreadingPath &TPath : SwitchPaths->getThreadingPaths()) {
915 APInt NextState = TPath.getExitValue();
916 const BasicBlock *Determinator = TPath.getDeterminatorBB();
919 BasicBlock *BB = SwitchPaths->getSwitchBlock();
920 BasicBlock *VisitedBB = getClonedBB(BB, NextState, DuplicateMap);
922 Metrics.analyzeBasicBlock(BB, *
TTI, EphValues);
923 NumClonedInst += BB->
size();
924 DuplicateMap[BB].push_back({BB, NextState});
929 if (PathBBs.front() == Determinator)
934 auto DetIt =
llvm::find(PathBBs, Determinator);
935 for (
auto BBIt = DetIt; BBIt != PathBBs.end(); BBIt++) {
937 VisitedBB = getClonedBB(BB, NextState, DuplicateMap);
940 Metrics.analyzeBasicBlock(BB, *
TTI, EphValues);
941 NumClonedInst += BB->
size();
942 DuplicateMap[BB].push_back({BB, NextState});
946 LLVM_DEBUG(
dbgs() <<
"DFA Jump Threading: Not jump threading, contains "
947 <<
"non-duplicatable instructions.\n");
949 return OptimizationRemarkMissed(
DEBUG_TYPE,
"NonDuplicatableInst",
951 <<
"Contains non-duplicatable instructions.";
957 if (
Metrics.Convergence != ConvergenceKind::None) {
958 LLVM_DEBUG(
dbgs() <<
"DFA Jump Threading: Not jump threading, contains "
959 <<
"convergent instructions.\n");
961 return OptimizationRemarkMissed(
DEBUG_TYPE,
"ConvergentInst", Switch)
962 <<
"Contains convergent instructions.";
967 if (!
Metrics.NumInsts.isValid()) {
968 LLVM_DEBUG(
dbgs() <<
"DFA Jump Threading: Not jump threading, contains "
969 <<
"instructions with invalid cost.\n");
971 return OptimizationRemarkMissed(
DEBUG_TYPE,
"ConvergentInst", Switch)
972 <<
"Contains instructions with invalid cost.";
981 uint64_t NumOrigInst = 0;
982 uint64_t NumOuterUseBlock = 0;
983 for (
auto *BB : DuplicateMap.
keys()) {
984 NumOrigInst += BB->
size();
988 if (!DuplicateMap.
count(Succ) && Succ->getSinglePredecessor())
992 if (
double(NumClonedInst) /
double(NumOrigInst) >
MaxClonedRate) {
993 LLVM_DEBUG(
dbgs() <<
"DFA Jump Threading: Not jump threading, too much "
994 "instructions wll be cloned\n");
996 return OptimizationRemarkMissed(
DEBUG_TYPE,
"NotProfitable", Switch)
997 <<
"Too much instructions will be cloned.";
1007 LLVM_DEBUG(
dbgs() <<
"DFA Jump Threading: Not jump threading, too much "
1008 "blocks with outer uses\n");
1010 return OptimizationRemarkMissed(
DEBUG_TYPE,
"NotProfitable", Switch)
1011 <<
"Too much blocks with outer uses.";
1018 unsigned JumpTableSize = 0;
1021 if (JumpTableSize == 0) {
1025 unsigned CondBranches =
1026 APInt(32,
Switch->getNumSuccessors()).ceilLogBase2();
1027 assert(CondBranches > 0 &&
1028 "The threaded switch must have multiple branches");
1029 DuplicationCost =
Metrics.NumInsts / CondBranches;
1037 DuplicationCost =
Metrics.NumInsts / JumpTableSize;
1040 LLVM_DEBUG(
dbgs() <<
"\nDFA Jump Threading: Cost to jump thread block "
1041 << SwitchPaths->getSwitchBlock()->getName()
1042 <<
" is: " << DuplicationCost <<
"\n\n");
1045 LLVM_DEBUG(
dbgs() <<
"Not jump threading, duplication cost exceeds the "
1046 <<
"cost threshold.\n");
1048 return OptimizationRemarkMissed(
DEBUG_TYPE,
"NotProfitable", Switch)
1049 <<
"Duplication cost exceeds the cost threshold (cost="
1050 <<
ore::NV(
"Cost", DuplicationCost)
1057 return OptimizationRemark(
DEBUG_TYPE,
"JumpThreaded", Switch)
1058 <<
"Switch statement jump-threaded.";
1065 void createAllExitPaths() {
1067 BasicBlock *SwitchBlock = SwitchPaths->getSwitchBlock();
1068 for (ThreadingPath &TPath : SwitchPaths->getThreadingPaths()) {
1072 TPath.push_front(SwitchBlock);
1079 SmallPtrSet<BasicBlock *, 16> BlocksToClean;
1082 for (
const ThreadingPath &TPath : SwitchPaths->getThreadingPaths()) {
1083 createExitPath(NewDefs, TPath, DuplicateMap, BlocksToClean, DTU);
1089 for (
const ThreadingPath &TPath : SwitchPaths->getThreadingPaths())
1090 updateLastSuccessor(TPath, DuplicateMap, DTU);
1096 for (BasicBlock *BB : BlocksToClean)
1106 void createExitPath(
DefMap &NewDefs,
const ThreadingPath &Path,
1108 SmallPtrSet<BasicBlock *, 16> &BlocksToClean,
1109 DomTreeUpdater *DTU) {
1110 APInt NextState =
Path.getExitValue();
1115 if (PathBBs.front() == Determinator)
1116 PathBBs.pop_front();
1118 auto DetIt =
llvm::find(PathBBs, Determinator);
1121 BasicBlock *PrevBB = PathBBs.size() == 1 ? *DetIt : *std::prev(DetIt);
1122 for (
auto BBIt = DetIt; BBIt != PathBBs.end(); BBIt++) {
1124 BlocksToClean.
insert(BB);
1128 BasicBlock *NextBB = getClonedBB(BB, NextState, DuplicateMap);
1130 updatePredecessor(PrevBB, BB, NextBB, DTU);
1136 BasicBlock *NewBB = cloneBlockAndUpdatePredecessor(
1137 BB, PrevBB, NextState, DuplicateMap, NewDefs, DTU);
1138 DuplicateMap[BB].push_back({NewBB, NextState});
1139 BlocksToClean.
insert(NewBB);
1150 void updateSSA(
DefMap &NewDefs) {
1151 SSAUpdaterBulk SSAUpdate;
1152 SmallVector<Use *, 16> UsesToRename;
1154 for (
const auto &KV : NewDefs) {
1157 std::vector<Instruction *> Cloned = KV.second;
1161 for (Use &U :
I->uses()) {
1164 if (UserPN->getIncomingBlock(U) == BB)
1166 }
else if (
User->getParent() == BB) {
1175 if (UsesToRename.
empty())
1183 unsigned VarNum = SSAUpdate.
AddVariable(
I->getName(),
I->getType());
1185 for (Instruction *New : Cloned)
1188 while (!UsesToRename.
empty())
1202 static BasicBlock *getNextCaseSuccessor(SwitchInst *Switch,
1203 const APInt &NextState) {
1205 for (
auto Case :
Switch->cases()) {
1206 if (Case.getCaseValue()->getValue() == NextState) {
1207 NextCase = Case.getCaseSuccessor();
1212 NextCase =
Switch->getDefaultDest();
1220 BasicBlock *cloneBlockAndUpdatePredecessor(BasicBlock *BB, BasicBlock *PrevBB,
1221 const APInt &NextState,
1224 DomTreeUpdater *DTU) {
1232 for (Instruction &
I : *NewBB) {
1244 updateSuccessorPhis(BB, NewBB, NextState, VMap, DuplicateMap);
1245 updatePredecessor(PrevBB, BB, NewBB, DTU);
1246 updateDefMap(NewDefs, VMap);
1249 SmallPtrSet<BasicBlock *, 4> SuccSet;
1251 if (SuccSet.
insert(SuccBB).second)
1252 DTU->
applyUpdates({{DominatorTree::Insert, NewBB, SuccBB}});
1262 void updateSuccessorPhis(BasicBlock *BB, BasicBlock *ClonedBB,
1265 std::vector<BasicBlock *> BlocksToUpdate;
1269 if (BB == SwitchPaths->getSwitchBlock()) {
1270 SwitchInst *
Switch = SwitchPaths->getSwitchInst();
1271 BasicBlock *NextCase = getNextCaseSuccessor(Switch, NextState);
1272 BlocksToUpdate.push_back(NextCase);
1273 BasicBlock *ClonedSucc = getClonedBB(NextCase, NextState, DuplicateMap);
1275 BlocksToUpdate.push_back(ClonedSucc);
1280 BlocksToUpdate.push_back(Succ);
1285 BasicBlock *ClonedSucc = getClonedBB(Succ, NextState, DuplicateMap);
1287 BlocksToUpdate.push_back(ClonedSucc);
1294 for (BasicBlock *Succ : BlocksToUpdate) {
1295 for (PHINode &Phi : Succ->phis()) {
1296 Value *Incoming =
Phi.getIncomingValueForBlock(BB);
1299 Phi.addIncoming(Incoming, ClonedBB);
1302 Value *ClonedVal = VMap[Incoming];
1304 Phi.addIncoming(ClonedVal, ClonedBB);
1306 Phi.addIncoming(Incoming, ClonedBB);
1314 void updatePredecessor(BasicBlock *PrevBB, BasicBlock *OldBB,
1315 BasicBlock *NewBB, DomTreeUpdater *DTU) {
1318 if (!isPredecessor(OldBB, PrevBB))
1328 DTU->
applyUpdates({{DominatorTree::Delete, PrevBB, OldBB},
1329 {DominatorTree::Insert, PrevBB, NewBB}});
1338 for (
auto Entry : VMap) {
1341 if (!Inst || !
Entry.second ||
1349 NewDefsVector.
push_back({Inst, Cloned});
1353 sort(NewDefsVector, [](
const auto &
LHS,
const auto &
RHS) {
1354 if (
LHS.first ==
RHS.first)
1355 return LHS.second->comesBefore(
RHS.second);
1356 return LHS.first->comesBefore(
RHS.first);
1359 for (
const auto &KV : NewDefsVector)
1360 NewDefs[KV.first].push_back(KV.second);
1368 void updateLastSuccessor(
const ThreadingPath &TPath,
1370 DomTreeUpdater *DTU) {
1371 APInt NextState = TPath.getExitValue();
1373 BasicBlock *LastBlock = getClonedBB(BB, NextState, DuplicateMap);
1380 BasicBlock *NextCase = getNextCaseSuccessor(Switch, NextState);
1382 std::vector<DominatorTree::UpdateType> DTUpdates;
1383 SmallPtrSet<BasicBlock *, 4> SuccSet;
1384 for (BasicBlock *Succ :
successors(LastBlock)) {
1385 if (Succ != NextCase && SuccSet.
insert(Succ).second)
1386 DTUpdates.push_back({DominatorTree::Delete, LastBlock, Succ});
1389 Switch->eraseFromParent();
1397 void cleanPhiNodes(BasicBlock *BB) {
1402 PN.eraseFromParent();
1408 for (PHINode &Phi : BB->
phis())
1409 Phi.removeIncomingValueIf([&](
unsigned Index) {
1411 return !isPredecessor(BB, IncomingBB);
1417 BasicBlock *getClonedBB(BasicBlock *BB,
const APInt &NextState,
1423 auto It =
llvm::find_if(ClonedBBs, [NextState](
const ClonedBlock &
C) {
1424 return C.State == NextState;
1426 return It != ClonedBBs.end() ? (*It).BB :
nullptr;
1430 bool isPredecessor(BasicBlock *BB, BasicBlock *IncomingBB) {
1434 AllSwitchPaths *SwitchPaths;
1435 DomTreeUpdater *DTU;
1436 AssumptionCache *AC;
1437 TargetTransformInfo *
TTI;
1438 OptimizationRemarkEmitter *ORE;
1439 SmallPtrSet<const Value *, 32> EphValues;
1440 std::vector<ThreadingPath> TPaths;
1444bool DFAJumpThreading::run(Function &
F) {
1445 LLVM_DEBUG(
dbgs() <<
"\nDFA Jump threading: " <<
F.getName() <<
"\n");
1447 if (
F.hasOptSize()) {
1448 LLVM_DEBUG(
dbgs() <<
"Skipping due to the 'minsize' attribute\n");
1456 bool MadeChanges =
false;
1457 LoopInfoBroken =
false;
1459 for (BasicBlock &BB :
F) {
1465 <<
" is a candidate\n");
1466 MainSwitch
Switch(SI, LI, ORE);
1468 if (!
Switch.getInstr()) {
1470 <<
"candidate for jump threading\n");
1475 <<
"candidate for jump threading\n");
1478 unfoldSelectInstrs(
Switch.getSelectInsts());
1479 if (!
Switch.getSelectInsts().empty())
1482 AllSwitchPaths SwitchPaths(&Switch, ORE, LI,
1486 if (SwitchPaths.getNumThreadingPaths() > 0) {
1503 SmallPtrSet<const Value *, 32> EphValues;
1504 if (ThreadableLoops.
size() > 0)
1507 for (AllSwitchPaths SwitchPaths : ThreadableLoops) {
1508 TransformDFA Transform(&SwitchPaths, DTU, AC,
TTI, ORE, EphValues);
1509 if (Transform.run())
1510 MadeChanges = LoopInfoBroken =
true;
1515#ifdef EXPENSIVE_CHECKS
1521 "Failed to maintain validity of domtree!");
1536 DFAJumpThreading ThreadImpl(&AC, &DTU, &LI, &
TTI, &ORE);
1537 if (!ThreadImpl.run(
F))
1542 if (!ThreadImpl.LoopInfoBroken)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static const Function * getParent(const Value *V)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
SmallPtrSet< const BasicBlock *, 8 > VisitedBlocks
std::deque< BasicBlock * > PathType
std::vector< PathType > PathsType
MapVector< Instruction *, std::vector< Instruction * > > DefMap
std::vector< ClonedBlock > CloneList
DenseMap< BasicBlock *, CloneList > DuplicateBlockMap
This file defines the DenseMap class.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
static bool isCandidate(const MachineInstr *MI, Register &DefedReg, Register FrameReg)
uint64_t IntrinsicInst * II
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
LLVM_ABI void registerAssumption(AssumeInst *CI)
Add an @llvm.assume intrinsic to this function's cache.
LLVM Basic Block Representation.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
const Function * getParent() const
Return the enclosing method, or null if none.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
LLVM_ABI void moveAfter(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it right after MovePos in the function M...
LLVM_ABI const BasicBlock * getUniqueSuccessor() const
Return the successor of this block if it has a unique successor.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
LLVM_ABI void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
static CondBrInst * Create(Value *Cond, BasicBlock *IfTrue, BasicBlock *IfFalse, InsertPosition InsertBefore=nullptr)
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
BasicBlock * getSuccessor(unsigned i) const
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Analysis pass which computes a DominatorTree.
bool verify(VerificationLevel VL=VerificationLevel::Full) const
verify - checks if the tree is correct.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
DomTreeT & getDomTree()
Flush DomTree updates and return DomTree.
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
void flush()
Apply all pending updates to available trees and flush all BasicBlocks awaiting deletion.
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
LLVM_ABI BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
LLVM_ABI void setSuccessor(unsigned Idx, BasicBlock *BB)
Update the specified successor to point at the provided block.
Analysis pass that exposes the LoopInfo for a function.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
const LoopT * getOutermostLoop() const
Get the outermost loop in which this loop is contained.
void verify(const DominatorTreeBase< BlockT, false > &DomTree) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
This class implements a map that also provides access to all stored values in a deterministic order.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
iterator_range< const_block_iterator > blocks() const
LLVM_ABI Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
Remove an incoming value.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserve()
Mark an analysis as preserved.
LLVM_ABI unsigned AddVariable(StringRef Name, Type *Ty)
Add a new variable to the SSA rewriter.
LLVM_ABI void AddAvailableValue(unsigned Var, BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value.
LLVM_ABI void RewriteAllUses(DominatorTree *DT, SmallVectorImpl< PHINode * > *InsertedPHIs=nullptr)
Perform all the necessary updates, including new PHI-nodes insertion and the requested uses update.
LLVM_ABI void AddUse(unsigned Var, Use *U)
Record a use of the symbolic value.
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getTrueValue() const
bool erase(PtrType Ptr)
Remove pointer from the set.
void insert_range(Range &&R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void reserve(size_type N)
void push_back(const T &Elt)
BasicBlock * getDefaultDest() const
iterator_range< CaseIt > cases()
Iteration adapter for range-for loops.
Analysis pass providing the TargetTransformInfo.
static UncondBrInst * Create(BasicBlock *Target, InsertPosition InsertBefore=nullptr)
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Value * getOperand(unsigned i) const
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI std::string getNameOrAsOperand() const
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
const ParentTy * getParent() const
This class implements an extremely fast bulk output stream that can only output to a stream.
@ C
The default llvm calling convention, compatible with C.
@ BasicBlock
Various leaf nodes.
initializer< Ty > init(const Ty &Val)
@ Switch
The "resume-switch" lowering, where there are separate resume and destroy functions that are shared b...
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
@ User
could "use" a pointer
DiagnosticInfoOptimizationBase::Argument NV
NodeAddr< PhiNode * > Phi
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
FunctionAddr VTableAddr Value
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
cl::opt< bool > ProfcheckDisableMetadataFixes
static cl::opt< unsigned > MaxNumPaths("dfa-max-num-paths", cl::desc("Max number of paths enumerated around a switch"), cl::Hidden, cl::init(200))
LLVM_ABI BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=nullptr, ClonedCodeInfo *CodeInfo=nullptr, bool MapAtoms=true)
Return a copy of the specified basic block, but without embedding the block into a particular functio...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
auto successors(const MachineBasicBlock *BB)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
auto pred_size(const MachineBasicBlock *BB)
static cl::opt< bool > ClViewCfgBefore("dfa-jump-view-cfg-before", cl::desc("View the CFG before DFA Jump Threading"), cl::Hidden, cl::init(false))
auto map_range(ContainerTy &&C, FuncTy F)
Return a range that applies F to the elements of C.
static cl::opt< double > MaxClonedRate("dfa-max-cloned-rate", cl::desc("Maximum cloned instructions rate accepted for the transformation"), cl::Hidden, cl::init(7.5))
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
void sort(IteratorTy Start, IteratorTy End)
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
@ RF_NoModuleLevelChanges
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
static cl::opt< unsigned > MaxNumVisitiedPaths("dfa-max-num-visited-paths", cl::desc("Max number of blocks visited while enumerating paths around a switch"), cl::Hidden, cl::init(2500))
std::string join(IteratorT Begin, IteratorT End, StringRef Separator)
Joins the strings in the range [Begin, End), adding Separator between the elements.
static cl::opt< bool > EarlyExitHeuristic("dfa-early-exit-heuristic", cl::desc("Exit early if an unpredictable value come from the same loop"), cl::Hidden, cl::init(true))
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataPredicate *IdentityMD=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
LLVM_ABI bool VerifyDomInfo
Enables verification of dominator trees.
static cl::opt< unsigned > MaxOuterUseBlocks("dfa-max-out-use-blocks", cl::desc("Maximum unduplicated blocks with outer uses " "accepted for the transformation"), cl::Hidden, cl::init(40))
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
static cl::opt< unsigned > MaxPathLength("dfa-max-path-length", cl::desc("Max number of blocks searched to find a threading path"), cl::Hidden, cl::init(20))
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
bool pred_empty(const BasicBlock *BB)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
static cl::opt< unsigned > CostThreshold("dfa-cost-threshold", cl::desc("Maximum cost accepted for the transformation"), cl::Hidden, cl::init(50))
static LLVM_ABI void collectEphemeralValues(const Loop *L, AssumptionCache *AC, SmallPtrSetImpl< const Value * > &EphValues)
Collect a loop's ephemeral values (those used only by an assume or similar intrinsics in the loop).
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Integrate with the new Pass Manager.