diff options
-rw-r--r-- | lib/Transforms/Utils/SimplifyCFG.cpp | 7 | ||||
-rw-r--r-- | test/Transforms/SimplifyCFG/switch-to-icmp.ll | 18 |
2 files changed, 24 insertions, 1 deletions
diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index 681bf9c2b7..6de602e4c3 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -3073,7 +3073,12 @@ static bool TurnSwitchRangeIntoICmp(SwitchInst *SI, IRBuilder<> &Builder) { Value *Sub = SI->getCondition(); if (!Offset->isNullValue()) Sub = Builder.CreateAdd(Sub, Offset, Sub->getName()+".off"); - Value *Cmp = Builder.CreateICmpULT(Sub, NumCases, "switch"); + Value *Cmp; + // If NumCases overflowed, then all possible values jump to the successor. + if (NumCases->isNullValue() && SI->getNumCases() != 0) + Cmp = ConstantInt::getTrue(SI->getContext()); + else + Cmp = Builder.CreateICmpULT(Sub, NumCases, "switch"); BranchInst *NewBI = Builder.CreateCondBr( Cmp, SI->case_begin().getCaseSuccessor(), SI->getDefaultDest()); diff --git a/test/Transforms/SimplifyCFG/switch-to-icmp.ll b/test/Transforms/SimplifyCFG/switch-to-icmp.ll index 414f8475bc..e9a6db45cb 100644 --- a/test/Transforms/SimplifyCFG/switch-to-icmp.ll +++ b/test/Transforms/SimplifyCFG/switch-to-icmp.ll @@ -37,3 +37,21 @@ lor.end: ; CHECK: @test2 ; CHECK: %switch = icmp ult i32 %x, 2 } + +define i32 @test3(i1 %flag) { +entry: + switch i1 %flag, label %bad [ + i1 true, label %good + i1 false, label %good + ] + +good: + ret i32 0 + +bad: + ret i32 1 + +; CHECK: @test3 +; CHECK: entry: +; CHECK-NEXT: ret i32 0 +} |