@@ -5836,6 +5836,8 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
5836
5836
BasicBlock *ContiguousDest = nullptr ;
5837
5837
BasicBlock *OtherDest = nullptr ;
5838
5838
bool IsWrapping = false ;
5839
+ SmallVectorImpl<ConstantInt *> *ContiguousCases = &CasesA;
5840
+ SmallVectorImpl<ConstantInt *> *OtherCases = &CasesB;
5839
5841
if (!CasesA.empty () &&
5840
5842
casesAreContiguous (SI->getCondition (), CasesA, ContiguousCasesMin,
5841
5843
ContiguousCasesMax, IsWrapping)) {
@@ -5845,28 +5847,29 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
5845
5847
ContiguousCasesMax, IsWrapping)) {
5846
5848
ContiguousDest = DestB;
5847
5849
OtherDest = DestA;
5850
+ std::swap (ContiguousCases, OtherCases);
5848
5851
} else
5849
5852
return false ;
5850
5853
5851
- if (IsWrapping)
5854
+ if (IsWrapping) {
5852
5855
std::swap (ContiguousDest, OtherDest);
5856
+ std::swap (ContiguousCases, OtherCases);
5857
+ }
5853
5858
5854
5859
// Start building the compare and branch.
5855
5860
5856
- auto ContiguousCasesSize =
5857
- (ContiguousCasesMax->getValue () - ContiguousCasesMin->getValue ())
5858
- .getZExtValue () +
5859
- 1 ;
5860
5861
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 );
5862
5865
5863
5866
Value *Sub = SI->getCondition ();
5864
5867
if (!Offset->isNullValue ())
5865
5868
Sub = Builder.CreateAdd (Sub, Offset, Sub->getName () + " .off" );
5866
5869
5867
5870
Value *Cmp;
5868
5871
// If NumCases overflowed, then all possible values jump to the successor.
5869
- if (NumCases->isNullValue () && ContiguousCasesSize != 0 )
5872
+ if (NumCases->isNullValue () && !ContiguousCases-> empty () )
5870
5873
Cmp = ConstantInt::getTrue (SI->getContext ());
5871
5874
else
5872
5875
Cmp = Builder.CreateICmpULT (Sub, NumCases, " switch" );
@@ -5896,14 +5899,14 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
5896
5899
5897
5900
// Prune obsolete incoming values off the successors' PHI nodes.
5898
5901
for (auto BBI = ContiguousDest->begin (); isa<PHINode>(BBI); ++BBI) {
5899
- unsigned PreviousEdges = ContiguousCasesSize ;
5902
+ unsigned PreviousEdges = ContiguousCases-> size () ;
5900
5903
if (ContiguousDest == SI->getDefaultDest ())
5901
5904
++PreviousEdges;
5902
5905
for (unsigned I = 0 , E = PreviousEdges - 1 ; I != E; ++I)
5903
5906
cast<PHINode>(BBI)->removeIncomingValue (SI->getParent ());
5904
5907
}
5905
5908
for (auto BBI = OtherDest->begin (); isa<PHINode>(BBI); ++BBI) {
5906
- unsigned PreviousEdges = SI-> getNumCases () - ContiguousCasesSize ;
5909
+ unsigned PreviousEdges = OtherCases-> size () ;
5907
5910
if (OtherDest == SI->getDefaultDest ())
5908
5911
++PreviousEdges;
5909
5912
for (unsigned I = 0 , E = PreviousEdges - 1 ; I != E; ++I)
0 commit comments