1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instcombine -S | FileCheck %s
3
4; Result of left shifting a non-negative integer
5; with nsw flag should also be non-negative
6define i1 @test_shift_nonnegative(i32 %a) {
7; CHECK-LABEL: @test_shift_nonnegative(
8; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[A:%.*]], -1
9; CHECK-NEXT:    ret i1 [[CMP]]
10;
11  %b = lshr i32 %a, 2
12  %shift = shl nsw i32 %b, 3
13  %cmp = icmp sge i32 %shift, 0
14  ret i1 %cmp
15}
16
17; Result of left shifting a negative integer with
18; nsw flag should also be negative
19define i1 @test_shift_negative(i32 %a, i32 %b) {
20; CHECK-LABEL: @test_shift_negative(
21; CHECK-NEXT:    ret i1 true
22;
23  %c = or i32 %a, -2147483648
24  %d = and i32 %b, 7
25  %shift = shl nsw i32 %c, %d
26  %cmp = icmp slt i32 %shift, 0
27  ret i1 %cmp
28}
29
30; If sign bit is a known zero, it cannot be a known one.
31; This test should not crash opt. The shift produces poison.
32define i32 @test_no_sign_bit_conflict1(i1 %b) {
33; CHECK-LABEL: @test_no_sign_bit_conflict1(
34; CHECK-NEXT:    ret i32 undef
35;
36  %sel = select i1 %b, i32 8193, i32 8192
37  %mul = shl nsw i32 %sel, 18
38  ret i32 %mul
39}
40
41; If sign bit is a known one, it cannot be a known zero.
42; This test should not crash opt. The shift produces poison.
43define i32 @test_no_sign_bit_conflict2(i1 %b) {
44; CHECK-LABEL: @test_no_sign_bit_conflict2(
45; CHECK-NEXT:    ret i32 undef
46;
47  %sel = select i1 %b, i32 -8193, i32 -8194
48  %mul = shl nsw i32 %sel, 18
49  ret i32 %mul
50}
51