1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; Test that the strncmp library call simplifier works correctly.
3 ; RUN: opt < %s -instcombine -S | FileCheck %s
5 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"
7 @hello = constant [6 x i8] c"hello\00"
8 @hell = constant [5 x i8] c"hell\00"
9 @bell = constant [5 x i8] c"bell\00"
10 @null = constant [1 x i8] zeroinitializer
12 declare i32 @strncmp(i8*, i8*, i32)
14 ; strncmp("", x, n) -> -*x
15 define i32 @test1(i8* %str2) {
16 ; CHECK-LABEL: @test1(
17 ; CHECK-NEXT: [[STRCMPLOAD:%.*]] = load i8, i8* [[STR2:%.*]], align 1
18 ; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[STRCMPLOAD]] to i32
19 ; CHECK-NEXT: [[TMP2:%.*]] = sub nsw i32 0, [[TMP1]]
20 ; CHECK-NEXT: ret i32 [[TMP2]]
23 %str1 = getelementptr inbounds [1 x i8], [1 x i8]* @null, i32 0, i32 0
24 %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 10)
28 ; strncmp(x, "", n) -> *x
29 define i32 @test2(i8* %str1) {
30 ; CHECK-LABEL: @test2(
31 ; CHECK-NEXT: [[STRCMPLOAD:%.*]] = load i8, i8* [[STR1:%.*]], align 1
32 ; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[STRCMPLOAD]] to i32
33 ; CHECK-NEXT: ret i32 [[TMP1]]
36 %str2 = getelementptr inbounds [1 x i8], [1 x i8]* @null, i32 0, i32 0
37 %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 10)
41 ; strncmp(x, y, n) -> cnst
43 ; CHECK-LABEL: @test3(
44 ; CHECK-NEXT: ret i32 -1
47 %str1 = getelementptr inbounds [5 x i8], [5 x i8]* @hell, i32 0, i32 0
48 %str2 = getelementptr inbounds [6 x i8], [6 x i8]* @hello, i32 0, i32 0
49 %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 10)
54 ; CHECK-LABEL: @test4(
55 ; CHECK-NEXT: ret i32 1
58 %str1 = getelementptr inbounds [5 x i8], [5 x i8]* @hell, i32 0, i32 0
59 %str2 = getelementptr inbounds [1 x i8], [1 x i8]* @null, i32 0, i32 0
60 %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 10)
65 ; CHECK-LABEL: @test5(
66 ; CHECK-NEXT: ret i32 0
69 %str1 = getelementptr inbounds [5 x i8], [5 x i8]* @hell, i32 0, i32 0
70 %str2 = getelementptr inbounds [6 x i8], [6 x i8]* @hello, i32 0, i32 0
71 %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 4)
75 ; strncmp(x,y,1) -> memcmp(x,y,1)
76 define i32 @test6(i8* %str1, i8* %str2) {
77 ; CHECK-LABEL: @test6(
78 ; CHECK-NEXT: [[LHSC:%.*]] = load i8, i8* [[STR1:%.*]], align 1
79 ; CHECK-NEXT: [[LHSV:%.*]] = zext i8 [[LHSC]] to i32
80 ; CHECK-NEXT: [[RHSC:%.*]] = load i8, i8* [[STR2:%.*]], align 1
81 ; CHECK-NEXT: [[RHSV:%.*]] = zext i8 [[RHSC]] to i32
82 ; CHECK-NEXT: [[CHARDIFF:%.*]] = sub nsw i32 [[LHSV]], [[RHSV]]
83 ; CHECK-NEXT: ret i32 [[CHARDIFF]]
86 %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 1)
91 define i32 @test7(i8* %str1, i8* %str2) {
92 ; CHECK-LABEL: @test7(
93 ; CHECK-NEXT: ret i32 0
96 %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 0)
100 ; strncmp(x,x,n) -> 0
101 define i32 @test8(i8* %str, i32 %n) {
102 ; CHECK-LABEL: @test8(
103 ; CHECK-NEXT: ret i32 0
106 %temp1 = call i32 @strncmp(i8* %str, i8* %str, i32 %n)
110 ; strncmp(nonnull x, nonnull y, n) -> strncmp(x, y, n)
111 define i32 @test9(i8* %str1, i8* %str2, i32 %n) {
112 ; CHECK-LABEL: @test9(
113 ; CHECK-NEXT: [[TEMP1:%.*]] = call i32 @strncmp(i8* nonnull [[STR1:%.*]], i8* nonnull [[STR2:%.*]], i32 [[N:%.*]])
114 ; CHECK-NEXT: ret i32 [[TEMP1]]
117 %temp1 = call i32 @strncmp(i8* nonnull %str1, i8* nonnull %str2, i32 %n)
121 ; strncmp(nonnull x, nonnull y, 0) -> 0
122 define i32 @test10(i8* %str1, i8* %str2, i32 %n) {
123 ; CHECK-LABEL: @test10(
124 ; CHECK-NEXT: ret i32 0
127 %temp1 = call i32 @strncmp(i8* nonnull %str1, i8* nonnull %str2, i32 0)
131 ; strncmp(x, y, 5) -> strncmp(nonnull x, nonnull y, 5)
132 define i32 @test11(i8* %str1, i8* %str2, i32 %n) {
133 ; CHECK-LABEL: @test11(
134 ; CHECK-NEXT: [[TEMP1:%.*]] = call i32 @strncmp(i8* nonnull dereferenceable(1) [[STR1:%.*]], i8* nonnull dereferenceable(1) [[STR2:%.*]], i32 5)
135 ; CHECK-NEXT: ret i32 [[TEMP1]]
138 %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 5)
142 define i32 @test12(i8* %str1, i8* %str2, i32 %n) "null-pointer-is-valid"="true" {
143 ; CHECK-LABEL: @test12(
144 ; CHECK-NEXT: [[TEMP1:%.*]] = call i32 @strncmp(i8* [[STR1:%.*]], i8* [[STR2:%.*]], i32 [[N:%.*]])
145 ; CHECK-NEXT: ret i32 [[TEMP1]]
148 %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 %n)