When converting switch range into an icmp, the initial case value may be lost,
which may prevent instructions in successors from being optimized.
For example:
define i64 @demo(i64 %x) { entry: switch i64 %x, label %bb3 [ i64 0, label %bb1 i64 1, label %bb2 ] bb1: ret i64 0 bb2: ; this will necessarily be false because %x == 1 %0 = icmp eq i64 %x, 100 br i1 %0, label %bb4, label %bb5 bb3: unreachable bb4: ret i64 200 bb5: ret i64 10 }
Ideally, the result after SimplifyCFG should be as follows:
define i64 @demo(i64 %x) { %entry: %switch = icmp eq i64 %x, 0 %. = select i1 %switch, i64 0, i64 10 ret i64 %. }
After applying this patch, the case value will be Propagated to the instructions in successors.
issue: SimplifyCFG Need to remove useless comparison after turning switch to icmp
Perhaps TurnSwitchRangeIntoICmp() simply should have similar treatment to SwitchToLookupTable()?