[InstCombine] Signed saturation patterns
[llvm-core.git] / test / Transforms / InstCombine / memcmp-1.ll
bloba82f861697cc2dc08fb4ab958015e7fefbca64d8
1 ; Test that the memcmp library call simplifier works correctly.
3 ; RUN: opt < %s -instcombine -S | FileCheck --check-prefix=CHECK --check-prefix=NOBCMP %s
4 ; RUN: opt < %s -instcombine -mtriple=x86_64-unknown-linux-gnu -S | FileCheck --check-prefix=CHECK --check-prefix=BCMP %s
6 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32:64"
8 @foo = constant [4 x i8] c"foo\00"
9 @hel = constant [4 x i8] c"hel\00"
10 @hello_u = constant [8 x i8] c"hello_u\00"
12 declare i32 @memcmp(i8*, i8*, i32)
14 ; Check memcmp(mem, mem, size) -> 0.
16 define i32 @test_simplify1(i8* %mem, i32 %size) {
17 ; CHECK-LABEL: @test_simplify1(
18 ; CHECK-NEXT:    ret i32 0
20   %ret = call i32 @memcmp(i8* %mem, i8* %mem, i32 %size)
21   ret i32 %ret
24 ; Check memcmp(mem1, mem2, 0) -> 0.
26 define i32 @test_simplify2(i8* %mem1, i8* %mem2) {
27 ; CHECK-LABEL: @test_simplify2(
28 ; CHECK-NEXT:    ret i32 0
30   %ret = call i32 @memcmp(i8* %mem1, i8* %mem2, i32 0)
31   ret i32 %ret
34 ;; Check memcmp(mem1, mem2, 1) -> *(unsigned char*)mem1 - *(unsigned char*)mem2.
36 define i32 @test_simplify3(i8* %mem1, i8* %mem2) {
37 ; CHECK-LABEL: @test_simplify3(
38 ; CHECK-NEXT:    [[LHSC:%.*]] = load i8, i8* %mem1, align 1
39 ; CHECK-NEXT:    [[LHSV:%.*]] = zext i8 [[LHSC]] to i32
40 ; CHECK-NEXT:    [[RHSC:%.*]] = load i8, i8* %mem2, align 1
41 ; CHECK-NEXT:    [[RHSV:%.*]] = zext i8 [[RHSC]] to i32
42 ; CHECK-NEXT:    [[CHARDIFF:%.*]] = sub nsw i32 [[LHSV]], [[RHSV]]
43 ; CHECK-NEXT:    ret i32 [[CHARDIFF]]
45   %ret = call i32 @memcmp(i8* %mem1, i8* %mem2, i32 1)
46   ret i32 %ret
49 ; Check memcmp(mem1, mem2, size) -> cnst, where all arguments are constants.
51 define i32 @test_simplify4() {
52 ; CHECK-LABEL: @test_simplify4(
53 ; CHECK-NEXT:    ret i32 0
55   %mem1 = getelementptr [4 x i8], [4 x i8]* @hel, i32 0, i32 0
56   %mem2 = getelementptr [8 x i8], [8 x i8]* @hello_u, i32 0, i32 0
57   %ret = call i32 @memcmp(i8* %mem1, i8* %mem2, i32 3)
58   ret i32 %ret
61 define i32 @test_simplify5() {
62 ; CHECK-LABEL: @test_simplify5(
63 ; CHECK-NEXT:    ret i32 1
65   %mem1 = getelementptr [4 x i8], [4 x i8]* @hel, i32 0, i32 0
66   %mem2 = getelementptr [4 x i8], [4 x i8]* @foo, i32 0, i32 0
67   %ret = call i32 @memcmp(i8* %mem1, i8* %mem2, i32 3)
68   ret i32 %ret
71 define i32 @test_simplify6() {
72 ; CHECK-LABEL: @test_simplify6(
73 ; CHECK-NEXT:    ret i32 -1
75   %mem1 = getelementptr [4 x i8], [4 x i8]* @foo, i32 0, i32 0
76   %mem2 = getelementptr [4 x i8], [4 x i8]* @hel, i32 0, i32 0
77   %ret = call i32 @memcmp(i8* %mem1, i8* %mem2, i32 3)
78   ret i32 %ret
81 ; Check memcmp(mem1, mem2, 8)==0 -> *(int64_t*)mem1 == *(int64_t*)mem2
83 define i1 @test_simplify7(i64 %x, i64 %y) {
84 ; CHECK-LABEL: @test_simplify7(
85 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 %x, %y
86 ; CHECK-NEXT:    ret i1 [[CMP]]
88   %x.addr = alloca i64, align 8
89   %y.addr = alloca i64, align 8
90   store i64 %x, i64* %x.addr, align 8
91   store i64 %y, i64* %y.addr, align 8
92   %xptr = bitcast i64* %x.addr to i8*
93   %yptr = bitcast i64* %y.addr to i8*
94   %call = call i32 @memcmp(i8* %xptr, i8* %yptr, i32 8)
95   %cmp = icmp eq i32 %call, 0
96   ret i1 %cmp
99 ; Check memcmp(mem1, mem2, 4)==0 -> *(int32_t*)mem1 == *(int32_t*)mem2
101 define i1 @test_simplify8(i32 %x, i32 %y) {
102 ; CHECK-LABEL: @test_simplify8(
103 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 %x, %y
104 ; CHECK-NEXT:    ret i1 [[CMP]]
106   %x.addr = alloca i32, align 4
107   %y.addr = alloca i32, align 4
108   store i32 %x, i32* %x.addr, align 4
109   store i32 %y, i32* %y.addr, align 4
110   %xptr = bitcast i32* %x.addr to i8*
111   %yptr = bitcast i32* %y.addr to i8*
112   %call = call i32 @memcmp(i8* %xptr, i8* %yptr, i32 4)
113   %cmp = icmp eq i32 %call, 0
114   ret i1 %cmp
117 ; Check memcmp(mem1, mem2, 2)==0 -> *(int16_t*)mem1 == *(int16_t*)mem2
119 define i1 @test_simplify9(i16 %x, i16 %y) {
120 ; CHECK-LABEL: @test_simplify9(
121 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i16 %x, %y
122 ; CHECK-NEXT:    ret i1 [[CMP]]
124   %x.addr = alloca i16, align 2
125   %y.addr = alloca i16, align 2
126   store i16 %x, i16* %x.addr, align 2
127   store i16 %y, i16* %y.addr, align 2
128   %xptr = bitcast i16* %x.addr to i8*
129   %yptr = bitcast i16* %y.addr to i8*
130   %call = call i32 @memcmp(i8* %xptr, i8* %yptr, i32 2)
131   %cmp = icmp eq i32 %call, 0
132   ret i1 %cmp
135 ; Check memcmp(mem1, mem2, size)==0 -> bcmp(mem1, mem2, size)==0
137 define i1 @test_simplify10(i8* %mem1, i8* %mem2, i32 %size) {
138 ; NOBCMP-LABEL: @test_simplify10(
139 ; NOBCMP-NEXT:    [[CALL:%.*]] = call i32 @memcmp(i8* %mem1, i8* %mem2, i32 %size)
140 ; NOBCMP-NEXT:    [[CMP:%.*]] = icmp eq i32 [[CALL]], 0
141 ; NOBCMP-NEXT:    ret i1 [[CMP]]
143 ; BCMP-LABEL: @test_simplify10(
144 ; BCMP-NEXT:    [[CALL:%.*]] = call i32 @bcmp(i8* %mem1, i8* %mem2, i32 %size)
145 ; BCMP-NEXT:    [[CMP:%.*]] = icmp eq i32 [[CALL]], 0
146 ; BCMP-NEXT:    ret i1 [[CMP]]
148   %call = call i32 @memcmp(i8* %mem1, i8* %mem2, i32 %size)
149   %cmp = icmp eq i32 %call, 0
150   ret i1 %cmp