1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S -data-layout=e-n32 | FileCheck %s --check-prefix=ALL --check-prefix=LE
3 ; RUN: opt < %s -instcombine -S -data-layout=E-n32 | FileCheck %s --check-prefix=ALL --check-prefix=BE
5 declare i32 @memcmp(i8*, i8*, i64)
7 ; The alignment of this constant does not matter. We constant fold the load.
9 @charbuf = private unnamed_addr constant [4 x i8] [i8 0, i8 0, i8 0, i8 1], align 1
11 define i1 @memcmp_4bytes_unaligned_constant_i8(i8* align 4 %x) {
12 ; LE-LABEL: @memcmp_4bytes_unaligned_constant_i8(
13 ; LE-NEXT: [[TMP1:%.*]] = bitcast i8* [[X:%.*]] to i32*
14 ; LE-NEXT: [[LHSV:%.*]] = load i32, i32* [[TMP1]], align 4
15 ; LE-NEXT: [[DOTNOT:%.*]] = icmp eq i32 [[LHSV]], 16777216
16 ; LE-NEXT: ret i1 [[DOTNOT]]
18 ; BE-LABEL: @memcmp_4bytes_unaligned_constant_i8(
19 ; BE-NEXT: [[TMP1:%.*]] = bitcast i8* [[X:%.*]] to i32*
20 ; BE-NEXT: [[LHSV:%.*]] = load i32, i32* [[TMP1]], align 4
21 ; BE-NEXT: [[DOTNOT:%.*]] = icmp eq i32 [[LHSV]], 1
22 ; BE-NEXT: ret i1 [[DOTNOT]]
24 %call = tail call i32 @memcmp(i8* %x, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @charbuf, i64 0, i64 0), i64 4)
25 %cmpeq0 = icmp eq i32 %call, 0
29 ; We still don't care about alignment of the constant. We are not limited to constant folding only i8 arrays.
30 ; It doesn't matter if the constant operand is the first operand to the memcmp.
32 @intbuf_unaligned = private unnamed_addr constant [4 x i16] [i16 1, i16 2, i16 3, i16 4], align 1
34 define i1 @memcmp_4bytes_unaligned_constant_i16(i8* align 4 %x) {
35 ; LE-LABEL: @memcmp_4bytes_unaligned_constant_i16(
36 ; LE-NEXT: [[TMP1:%.*]] = bitcast i8* [[X:%.*]] to i32*
37 ; LE-NEXT: [[RHSV:%.*]] = load i32, i32* [[TMP1]], align 4
38 ; LE-NEXT: [[DOTNOT:%.*]] = icmp eq i32 [[RHSV]], 131073
39 ; LE-NEXT: ret i1 [[DOTNOT]]
41 ; BE-LABEL: @memcmp_4bytes_unaligned_constant_i16(
42 ; BE-NEXT: [[TMP1:%.*]] = bitcast i8* [[X:%.*]] to i32*
43 ; BE-NEXT: [[RHSV:%.*]] = load i32, i32* [[TMP1]], align 4
44 ; BE-NEXT: [[DOTNOT:%.*]] = icmp eq i32 [[RHSV]], 65538
45 ; BE-NEXT: ret i1 [[DOTNOT]]
47 %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)
48 %cmpeq0 = icmp eq i32 %call, 0
52 ; TODO: Any memcmp where all arguments are constants should be constant folded. Currently, we only handle i8 array constants.
54 @intbuf = private unnamed_addr constant [2 x i32] [i32 0, i32 1], align 4
56 define i1 @memcmp_3bytes_aligned_constant_i32(i8* align 4 %x) {
57 ; ALL-LABEL: @memcmp_3bytes_aligned_constant_i32(
58 ; ALL-NEXT: [[CALL:%.*]] = tail call i32 @memcmp(i8* noundef nonnull dereferenceable(3) bitcast (i32* getelementptr inbounds ([2 x i32], [2 x i32]* @intbuf, i64 0, i64 1) to i8*), i8* noundef nonnull dereferenceable(3) bitcast ([2 x i32]* @intbuf to i8*), i64 3)
59 ; ALL-NEXT: [[CMPEQ0:%.*]] = icmp eq i32 [[CALL]], 0
60 ; ALL-NEXT: ret i1 [[CMPEQ0]]
62 %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)
63 %cmpeq0 = icmp eq i32 %call, 0
67 ; A sloppy implementation would infinite loop by recreating the unused instructions.
69 define i1 @memcmp_4bytes_one_unaligned_i8(i8* align 4 %x, i8* align 1 %y) {
70 ; ALL-LABEL: @memcmp_4bytes_one_unaligned_i8(
71 ; ALL-NEXT: [[CALL:%.*]] = tail call i32 @memcmp(i8* noundef nonnull dereferenceable(4) [[X:%.*]], i8* noundef nonnull dereferenceable(4) [[Y:%.*]], i64 4)
72 ; ALL-NEXT: [[CMPEQ0:%.*]] = icmp eq i32 [[CALL]], 0
73 ; ALL-NEXT: ret i1 [[CMPEQ0]]
75 %bc = bitcast i8* %x to i32*
76 %lhsv = load i32, i32* %bc
77 %call = tail call i32 @memcmp(i8* %x, i8* %y, i64 4)
78 %cmpeq0 = icmp eq i32 %call, 0