@@ -5836,6 +5836,8 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
58365836 BasicBlock *ContiguousDest = nullptr ;
58375837 BasicBlock *OtherDest = nullptr ;
58385838 bool IsWrapping = false ;
5839+ SmallVectorImpl<ConstantInt *> *ContiguousCases = &CasesA;
5840+ SmallVectorImpl<ConstantInt *> *OtherCases = &CasesB;
58395841 if (!CasesA.empty () &&
58405842 casesAreContiguous (SI->getCondition (), CasesA, ContiguousCasesMin,
58415843 ContiguousCasesMax, IsWrapping)) {
@@ -5845,28 +5847,29 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
58455847 ContiguousCasesMax, IsWrapping)) {
58465848 ContiguousDest = DestB;
58475849 OtherDest = DestA;
5850+ std::swap (ContiguousCases, OtherCases);
58485851 } else
58495852 return false ;
58505853
5851- if (IsWrapping)
5854+ if (IsWrapping) {
58525855 std::swap (ContiguousDest, OtherDest);
5856+ std::swap (ContiguousCases, OtherCases);
5857+ }
58535858
58545859 // Start building the compare and branch.
58555860
5856- auto ContiguousCasesSize =
5857- (ContiguousCasesMax->getValue () - ContiguousCasesMin->getValue ())
5858- .getZExtValue () +
5859- 1 ;
58605861 Constant *Offset = ConstantExpr::getNeg (ContiguousCasesMin);
5861- Constant *NumCases = ConstantInt::get (Offset->getType (), ContiguousCasesSize);
5862+ Constant *NumCases = ConstantInt::get (Offset->getType (),
5863+ ContiguousCasesMax->getValue () -
5864+ ContiguousCasesMin->getValue () + 1 );
58625865
58635866 Value *Sub = SI->getCondition ();
58645867 if (!Offset->isNullValue ())
58655868 Sub = Builder.CreateAdd (Sub, Offset, Sub->getName () + " .off" );
58665869
58675870 Value *Cmp;
58685871 // If NumCases overflowed, then all possible values jump to the successor.
5869- if (NumCases->isNullValue () && ContiguousCasesSize != 0 )
5872+ if (NumCases->isNullValue () && !ContiguousCases-> empty () )
58705873 Cmp = ConstantInt::getTrue (SI->getContext ());
58715874 else
58725875 Cmp = Builder.CreateICmpULT (Sub, NumCases, " switch" );
@@ -5896,14 +5899,14 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
58965899
58975900 // Prune obsolete incoming values off the successors' PHI nodes.
58985901 for (auto BBI = ContiguousDest->begin (); isa<PHINode>(BBI); ++BBI) {
5899- unsigned PreviousEdges = ContiguousCasesSize ;
5902+ unsigned PreviousEdges = ContiguousCases-> size () ;
59005903 if (ContiguousDest == SI->getDefaultDest ())
59015904 ++PreviousEdges;
59025905 for (unsigned I = 0 , E = PreviousEdges - 1 ; I != E; ++I)
59035906 cast<PHINode>(BBI)->removeIncomingValue (SI->getParent ());
59045907 }
59055908 for (auto BBI = OtherDest->begin (); isa<PHINode>(BBI); ++BBI) {
5906- unsigned PreviousEdges = SI-> getNumCases () - ContiguousCasesSize ;
5909+ unsigned PreviousEdges = OtherCases-> size () ;
59075910 if (OtherDest == SI->getDefaultDest ())
59085911 ++PreviousEdges;
59095912 for (unsigned I = 0 , E = PreviousEdges - 1 ; I != E; ++I)
0 commit comments