- // If there is only the default destination, branch to it if it is not the
- // next basic block. Otherwise, just fall through.
- if (!SI.getNumCases()) {
+ if (isa<UnreachableInst>(SI.getDefaultDest()->getFirstNonPHIOrDbg())) {
+ // Replace an unreachable default destination with the most popular case
+ // destination.
+ DenseMap<const BasicBlock*, uint64_t> Popularity;
+ uint64_t MaxPop = 0;
+ const BasicBlock *MaxBB = nullptr;
+ for (auto I : SI.cases()) {
+ const BasicBlock *BB = I.getCaseSuccessor();
+ if (++Popularity[BB] > MaxPop) {
+ MaxPop = Popularity[BB];
+ MaxBB = BB;
+ }
+ }
+
+ // Set new default.
+ Default = FuncInfo.MBBMap[MaxBB];
+
+ // Remove cases that have been replaced by the default.
+ CaseItr I = Cases.begin();
+ while (I != Cases.end()) {
+ if (I->BB == Default) {
+ I = Cases.erase(I);
+ continue;
+ }
+ ++I;
+ }
+ }
+
+ // If there is only the default destination, go there directly.
+ if (Cases.empty()) {