1; RUN: opt < %s -instcombine -S | FileCheck %s
2
3target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128:n8:16:32:64"
4
5define i32 @test1(i32 %x) {
6        %tmp.1 = and i32 %x, 65535              ; <i32> [#uses=1]
7        %tmp.2 = xor i32 %tmp.1, -32768         ; <i32> [#uses=1]
8        %tmp.3 = add i32 %tmp.2, 32768          ; <i32> [#uses=1]
9        ret i32 %tmp.3
10; CHECK-LABEL: @test1(
11; CHECK: %sext = shl i32 %x, 16
12; CHECK: %tmp.3 = ashr exact i32 %sext, 16
13; CHECK: ret i32 %tmp.3
14}
15
16define i32 @test2(i32 %x) {
17        %tmp.1 = and i32 %x, 65535              ; <i32> [#uses=1]
18        %tmp.2 = xor i32 %tmp.1, 32768          ; <i32> [#uses=1]
19        %tmp.3 = add i32 %tmp.2, -32768         ; <i32> [#uses=1]
20        ret i32 %tmp.3
21; CHECK-LABEL: @test2(
22; CHECK: %sext = shl i32 %x, 16
23; CHECK: %tmp.3 = ashr exact i32 %sext, 16
24; CHECK: ret i32 %tmp.3
25}
26
27define i32 @test3(i16 %P) {
28        %tmp.1 = zext i16 %P to i32             ; <i32> [#uses=1]
29        %tmp.4 = xor i32 %tmp.1, 32768          ; <i32> [#uses=1]
30        %tmp.5 = add i32 %tmp.4, -32768         ; <i32> [#uses=1]
31        ret i32 %tmp.5
32; CHECK-LABEL: @test3(
33; CHECK: %tmp.5 = sext i16 %P to i32
34; CHECK: ret i32 %tmp.5
35}
36
37define i32 @test4(i32 %x) {
38        %tmp.1 = and i32 %x, 255                ; <i32> [#uses=1]
39        %tmp.2 = xor i32 %tmp.1, 128            ; <i32> [#uses=1]
40        %tmp.3 = add i32 %tmp.2, -128           ; <i32> [#uses=1]
41        ret i32 %tmp.3
42; CHECK-LABEL: @test4(
43; CHECK: %sext = shl i32 %x, 24
44; CHECK: %tmp.3 = ashr exact i32 %sext, 24
45; CHECK: ret i32 %tmp.3
46}
47
48define i32 @test5(i32 %x) {
49        %tmp.2 = shl i32 %x, 16         ; <i32> [#uses=1]
50        %tmp.4 = ashr i32 %tmp.2, 16            ; <i32> [#uses=1]
51        ret i32 %tmp.4
52; CHECK-LABEL: @test5(
53; CHECK: %tmp.2 = shl i32 %x, 16
54; CHECK: %tmp.4 = ashr exact i32 %tmp.2, 16
55; CHECK: ret i32 %tmp.4
56}
57
58define i32 @test6(i16 %P) {
59  %tmp.1 = zext i16 %P to i32                     ; <i32> [#uses=1]
60  %sext1 = shl i32 %tmp.1, 16                     ; <i32> [#uses=1]
61  %tmp.5 = ashr i32 %sext1, 16                    ; <i32> [#uses=1]
62  ret i32 %tmp.5
63; CHECK-LABEL: @test6(
64; CHECK: %tmp.5 = sext i16 %P to i32
65; CHECK: ret i32 %tmp.5
66}
67
68define i32 @test7(i32 %x) nounwind readnone {
69entry:
70  %shr = lshr i32 %x, 5                           ; <i32> [#uses=1]
71  %xor = xor i32 %shr, 67108864                   ; <i32> [#uses=1]
72  %sub = add i32 %xor, -67108864                  ; <i32> [#uses=1]
73  ret i32 %sub
74; CHECK-LABEL: @test7(
75; CHECK: %sub = ashr i32 %x, 5
76; CHECK: ret i32 %sub
77}
78
79