blob: b19f17c818d8af822dbf4f426de8963ded946431 [file] [log] [blame]
Sanjay Patelcf081f92017-08-21 16:47:12 +00001; RUN: opt < %s -instcombine -S -data-layout=e-n32 | FileCheck %s --check-prefix=ALL --check-prefix=LE
2; RUN: opt < %s -instcombine -S -data-layout=E-n32 | FileCheck %s --check-prefix=ALL --check-prefix=BE
3
4declare i32 @memcmp(i8*, i8*, i64)
5
6; TODO: The alignment of this constant does not matter. We constant fold the load.
7
8@charbuf = private unnamed_addr constant [4 x i8] [i8 0, i8 0, i8 0, i8 1], align 1
9
10define i1 @memcmp_4bytes_unaligned_constant_i8(i8* align 4 %x) {
11; ALL-LABEL: @memcmp_4bytes_unaligned_constant_i8(
12; ALL-NEXT: [[CALL:%.*]] = tail call i32 @memcmp(i8* %x, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @charbuf, i64 0, i64 0), i64 4)
13; ALL-NEXT: [[CMPEQ0:%.*]] = icmp eq i32 [[CALL]], 0
14; ALL-NEXT: ret i1 [[CMPEQ0]]
15;
16 %call = tail call i32 @memcmp(i8* %x, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @charbuf, i64 0, i64 0), i64 4)
17 %cmpeq0 = icmp eq i32 %call, 0
18 ret i1 %cmpeq0
19}
20
21; TODO: We still don't care about alignment of the constant. We are not limited to constant folding only i8 arrays.
22; It doesn't matter if the constant operand is the first operand to the memcmp.
23
24@intbuf_unaligned = private unnamed_addr constant [4 x i16] [i16 1, i16 2, i16 3, i16 4], align 1
25
26define i1 @memcmp_4bytes_unaligned_constant_i16(i8* align 4 %x) {
27; ALL-LABEL: @memcmp_4bytes_unaligned_constant_i16(
28; ALL-NEXT: [[CALL:%.*]] = tail call i32 @memcmp(i8* bitcast ([4 x i16]* @intbuf_unaligned to i8*), i8* %x, i64 4)
29; ALL-NEXT: [[CMPEQ0:%.*]] = icmp eq i32 [[CALL]], 0
30; ALL-NEXT: ret i1 [[CMPEQ0]]
31;
32 %call = tail call i32 @memcmp(i8* bitcast (i16* getelementptr inbounds ([4 x i16], [4 x i16]* @intbuf_unaligned, i64 0, i64 0) to i8*), i8* %x, i64 4)
33 %cmpeq0 = icmp eq i32 %call, 0
34 ret i1 %cmpeq0
35}
36
37; TODO: Any memcmp where all arguments are constants should be constant folded. Currently, we only handle i8 array constants.
38
39@intbuf = private unnamed_addr constant [2 x i32] [i32 0, i32 1], align 4
40
41define i1 @memcmp_3bytes_aligned_constant_i32(i8* align 4 %x) {
42; ALL-LABEL: @memcmp_3bytes_aligned_constant_i32(
43; ALL-NEXT: [[CALL:%.*]] = tail call i32 @memcmp(i8* bitcast (i32* getelementptr inbounds ([2 x i32], [2 x i32]* @intbuf, i64 0, i64 1) to i8*), i8* bitcast ([2 x i32]* @intbuf to i8*), i64 3)
44; ALL-NEXT: [[CMPEQ0:%.*]] = icmp eq i32 [[CALL]], 0
45; ALL-NEXT: ret i1 [[CMPEQ0]]
46;
47 %call = tail call i32 @memcmp(i8* bitcast (i32* getelementptr inbounds ([2 x i32], [2 x i32]* @intbuf, i64 0, i64 1) to i8*), i8* bitcast (i32* getelementptr inbounds ([2 x i32], [2 x i32]* @intbuf, i64 0, i64 0) to i8*), i64 3)
48 %cmpeq0 = icmp eq i32 %call, 0
49 ret i1 %cmpeq0
50}
51