1; Test that the memcmp library call simplifier works correctly.
2;
3; RUN: opt < %s -instcombine -S | FileCheck %s
4
5target 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"
6
7@foo = constant [4 x i8] c"foo\00"
8@hel = constant [4 x i8] c"hel\00"
9@hello_u = constant [8 x i8] c"hello_u\00"
10
11declare i32 @memcmp(i8*, i8*, i32)
12
13; Check memcmp(mem, mem, size) -> 0.
14
15define i32 @test_simplify1(i8* %mem, i32 %size) {
16; CHECK-LABEL: @test_simplify1(
17  %ret = call i32 @memcmp(i8* %mem, i8* %mem, i32 %size)
18  ret i32 %ret
19; CHECK: ret i32 0
20}
21
22; Check memcmp(mem1, mem2, 0) -> 0.
23
24define i32 @test_simplify2(i8* %mem1, i8* %mem2) {
25; CHECK-LABEL: @test_simplify2(
26  %ret = call i32 @memcmp(i8* %mem1, i8* %mem2, i32 0)
27  ret i32 %ret
28; CHECK: ret i32 0
29}
30
31;; Check memcmp(mem1, mem2, 1) -> *(unsigned char*)mem1 - *(unsigned char*)mem2.
32
33define i32 @test_simplify3(i8* %mem1, i8* %mem2) {
34; CHECK-LABEL: @test_simplify3(
35  %ret = call i32 @memcmp(i8* %mem1, i8* %mem2, i32 1)
36; CHECK: [[LOAD1:%[a-z]+]] = load i8, i8* %mem1, align 1
37; CHECK: [[ZEXT1:%[a-z]+]] = zext i8 [[LOAD1]] to i32
38; CHECK: [[LOAD2:%[a-z]+]] = load i8, i8* %mem2, align 1
39; CHECK: [[ZEXT2:%[a-z]+]] = zext i8 [[LOAD2]] to i32
40; CHECK: [[RET:%[a-z]+]] = sub nsw i32 [[ZEXT1]], [[ZEXT2]]
41  ret i32 %ret
42; CHECK: ret i32 [[RET]]
43}
44
45; Check memcmp(mem1, mem2, size) -> cnst, where all arguments are constants.
46
47define i32 @test_simplify4() {
48; CHECK-LABEL: @test_simplify4(
49  %mem1 = getelementptr [4 x i8], [4 x i8]* @hel, i32 0, i32 0
50  %mem2 = getelementptr [8 x i8], [8 x i8]* @hello_u, i32 0, i32 0
51  %ret = call i32 @memcmp(i8* %mem1, i8* %mem2, i32 3)
52  ret i32 %ret
53; CHECK: ret i32 0
54}
55
56define i32 @test_simplify5() {
57; CHECK-LABEL: @test_simplify5(
58  %mem1 = getelementptr [4 x i8], [4 x i8]* @hel, i32 0, i32 0
59  %mem2 = getelementptr [4 x i8], [4 x i8]* @foo, i32 0, i32 0
60  %ret = call i32 @memcmp(i8* %mem1, i8* %mem2, i32 3)
61  ret i32 %ret
62; CHECK: ret i32 1
63}
64
65define i32 @test_simplify6() {
66; CHECK-LABEL: @test_simplify6(
67  %mem1 = getelementptr [4 x i8], [4 x i8]* @foo, i32 0, i32 0
68  %mem2 = getelementptr [4 x i8], [4 x i8]* @hel, i32 0, i32 0
69  %ret = call i32 @memcmp(i8* %mem1, i8* %mem2, i32 3)
70  ret i32 %ret
71; CHECK: ret i32 -1
72}
73
74; Check memcmp(mem1, mem2, 8)==0 -> *(int64_t*)mem1 == *(int64_t*)mem2
75
76define i1 @test_simplify7(i64 %x, i64 %y) {
77; CHECK-LABEL: @test_simplify7(
78  %x.addr = alloca i64, align 8
79  %y.addr = alloca i64, align 8
80  store i64 %x, i64* %x.addr, align 8
81  store i64 %y, i64* %y.addr, align 8
82  %xptr = bitcast i64* %x.addr to i8*
83  %yptr = bitcast i64* %y.addr to i8*
84  %call = call i32 @memcmp(i8* %xptr, i8* %yptr, i32 8)
85  %cmp = icmp eq i32 %call, 0
86  ret i1 %cmp
87; CHECK: %cmp = icmp eq i64 %x, %y
88; CHECK: ret i1 %cmp
89}
90
91; Check memcmp(mem1, mem2, 4)==0 -> *(int32_t*)mem1 == *(int32_t*)mem2
92
93define i1 @test_simplify8(i32 %x, i32 %y) {
94; CHECK-LABEL: @test_simplify8(
95  %x.addr = alloca i32, align 4
96  %y.addr = alloca i32, align 4
97  store i32 %x, i32* %x.addr, align 4
98  store i32 %y, i32* %y.addr, align 4
99  %xptr = bitcast i32* %x.addr to i8*
100  %yptr = bitcast i32* %y.addr to i8*
101  %call = call i32 @memcmp(i8* %xptr, i8* %yptr, i32 4)
102  %cmp = icmp eq i32 %call, 0
103  ret i1 %cmp
104; CHECK: %cmp = icmp eq i32 %x, %y
105; CHECK: ret i1 %cmp
106}
107
108; Check memcmp(mem1, mem2, 2)==0 -> *(int16_t*)mem1 == *(int16_t*)mem2
109
110define i1 @test_simplify9(i16 %x, i16 %y) {
111; CHECK-LABEL: @test_simplify9(
112  %x.addr = alloca i16, align 2
113  %y.addr = alloca i16, align 2
114  store i16 %x, i16* %x.addr, align 2
115  store i16 %y, i16* %y.addr, align 2
116  %xptr = bitcast i16* %x.addr to i8*
117  %yptr = bitcast i16* %y.addr to i8*
118  %call = call i32 @memcmp(i8* %xptr, i8* %yptr, i32 2)
119  %cmp = icmp eq i32 %call, 0
120  ret i1 %cmp
121; CHECK: %cmp = icmp eq i16 %x, %y
122; CHECK: ret i1 %cmp
123}
124