Home | History | Annotate | Download | only in InstCombine
      1 ; 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 
      4 declare i32 @memcmp(i8*, i8*, i64)
      5 
      6 ; 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 
     10 define i1 @memcmp_4bytes_unaligned_constant_i8(i8* align 4 %x) {
     11 ; LE-LABEL: @memcmp_4bytes_unaligned_constant_i8(
     12 ; LE-NEXT:    [[TMP1:%.*]] = bitcast i8* %x to i32*
     13 ; LE-NEXT:    [[LHSV:%.*]] = load i32, i32* [[TMP1]], align 4
     14 ; LE-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[LHSV]], 16777216
     15 ; LE-NEXT:    ret i1 [[TMP2]]
     16 ;
     17 ; BE-LABEL: @memcmp_4bytes_unaligned_constant_i8(
     18 ; BE-NEXT:    [[TMP1:%.*]] = bitcast i8* %x to i32*
     19 ; BE-NEXT:    [[LHSV:%.*]] = load i32, i32* [[TMP1]], align 4
     20 ; BE-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[LHSV]], 1
     21 ; BE-NEXT:    ret i1 [[TMP2]]
     22 ;
     23   %call = tail call i32 @memcmp(i8* %x, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @charbuf, i64 0, i64 0), i64 4)
     24   %cmpeq0 = icmp eq i32 %call, 0
     25   ret i1 %cmpeq0
     26 }
     27 
     28 ; We still don't care about alignment of the constant. We are not limited to constant folding only i8 arrays.
     29 ; It doesn't matter if the constant operand is the first operand to the memcmp.
     30 
     31 @intbuf_unaligned = private unnamed_addr constant [4 x i16] [i16 1, i16 2, i16 3, i16 4], align 1
     32 
     33 define i1 @memcmp_4bytes_unaligned_constant_i16(i8* align 4 %x) {
     34 ; LE-LABEL: @memcmp_4bytes_unaligned_constant_i16(
     35 ; LE-NEXT:    [[TMP1:%.*]] = bitcast i8* %x to i32*
     36 ; LE-NEXT:    [[RHSV:%.*]] = load i32, i32* [[TMP1]], align 4
     37 ; LE-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[RHSV]], 131073
     38 ; LE-NEXT:    ret i1 [[TMP2]]
     39 ;
     40 ; BE-LABEL: @memcmp_4bytes_unaligned_constant_i16(
     41 ; BE-NEXT:    [[TMP1:%.*]] = bitcast i8* %x to i32*
     42 ; BE-NEXT:    [[RHSV:%.*]] = load i32, i32* [[TMP1]], align 4
     43 ; BE-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[RHSV]], 65538
     44 ; BE-NEXT:    ret i1 [[TMP2]]
     45 ;
     46   %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)
     47   %cmpeq0 = icmp eq i32 %call, 0
     48   ret i1 %cmpeq0
     49 }
     50 
     51 ; TODO: Any memcmp where all arguments are constants should be constant folded. Currently, we only handle i8 array constants.
     52 
     53 @intbuf = private unnamed_addr constant [2 x i32] [i32 0, i32 1], align 4
     54 
     55 define i1 @memcmp_3bytes_aligned_constant_i32(i8* align 4 %x) {
     56 ; ALL-LABEL: @memcmp_3bytes_aligned_constant_i32(
     57 ; 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)
     58 ; ALL-NEXT:    [[CMPEQ0:%.*]] = icmp eq i32 [[CALL]], 0
     59 ; ALL-NEXT:    ret i1 [[CMPEQ0]]
     60 ;
     61   %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)
     62   %cmpeq0 = icmp eq i32 %call, 0
     63   ret i1 %cmpeq0
     64 }
     65 
     66 ; A sloppy implementation would infinite loop by recreating the unused instructions.
     67 
     68 define i1 @memcmp_4bytes_one_unaligned_i8(i8* align 4 %x, i8* align 1 %y) {
     69 ; ALL-LABEL: @memcmp_4bytes_one_unaligned_i8(
     70 ; ALL-NEXT:    [[CALL:%.*]] = tail call i32 @memcmp(i8* %x, i8* %y, i64 4)
     71 ; ALL-NEXT:    [[CMPEQ0:%.*]] = icmp eq i32 [[CALL]], 0
     72 ; ALL-NEXT:    ret i1 [[CMPEQ0]]
     73 ;
     74   %bc = bitcast i8* %x to i32*
     75   %lhsv = load i32, i32* %bc
     76   %call = tail call i32 @memcmp(i8* %x, i8* %y, i64 4)
     77   %cmpeq0 = icmp eq i32 %call, 0
     78   ret i1 %cmpeq0
     79 }
     80 
     81