xref: /llvm-project/llvm/test/Transforms/InstCombine/simplify-libcalls.ll (revision 10f315dc9c96ec2413881ab55a285e35d80def88)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -S < %s -mtriple=unknown -passes=instcombine | FileCheck -check-prefixes=CHECK,CHECK32 %s
3; RUN: opt -S < %s -mtriple=msp430 -passes=instcombine | FileCheck -check-prefixes=CHECK,CHECK16 %s
4target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-f80:128:128-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S32"
5
6@G = constant [3 x i8] c"%s\00"		; <ptr> [#uses=1]
7
8; A 32-bit compatible sprintf is not recognized as the standard library
9; function on 16-bit targets.
10declare i32 @sprintf(ptr, ptr, ...)
11
12define void @foo(ptr %P, ptr %X) {
13; CHECK32-LABEL: @foo(
14; CHECK32-NEXT:    [[STRCPY:%.*]] = call ptr @strcpy(ptr noundef nonnull dereferenceable(1) [[P:%.*]], ptr noundef nonnull dereferenceable(1) [[X:%.*]])
15; CHECK32-NEXT:    ret void
16;
17; CHECK16-LABEL: @foo(
18; CHECK16-NEXT:    [[TMP1:%.*]] = call i32 (ptr, ptr, ...) @sprintf(ptr [[P:%.*]], ptr nonnull @G, ptr [[X:%.*]])
19; CHECK16-NEXT:    ret void
20;
21  call i32 (ptr, ptr, ...) @sprintf( ptr %P, ptr @G, ptr %X )		; <i32>:1 [#uses=0]
22  ret void
23}
24
25; PR1307
26@str = internal constant [5 x i8] c"foog\00"
27@str1 = internal constant [8 x i8] c"blahhh!\00"
28@str2 = internal constant [5 x i8] c"Ponk\00"
29
30define ptr @test1() {
31; CHECK32-LABEL: @test1(
32; CHECK32-NEXT:    ret ptr getelementptr inbounds nuw (i8, ptr @str, i32 3)
33;
34; CHECK16-LABEL: @test1(
35; CHECK16-NEXT:    [[TMP3:%.*]] = tail call ptr @strchr(ptr nonnull getelementptr inbounds nuw (i8, ptr @str, i32 2), i32 103)
36; CHECK16-NEXT:    ret ptr [[TMP3]]
37;
38  %tmp3 = tail call ptr @strchr( ptr getelementptr ([5 x i8], ptr @str, i32 0, i32 2), i32 103 )              ; <ptr> [#uses=1]
39  ret ptr %tmp3
40}
41
42; A 32-bit compatible strchr is not recognized as the standard library
43; function on 16-bit targets.
44declare ptr @strchr(ptr, i32)
45
46define ptr @test2() {
47; CHECK32-LABEL: @test2(
48; CHECK32-NEXT:    ret ptr getelementptr inbounds nuw (i8, ptr @str1, i32 7)
49;
50; CHECK16-LABEL: @test2(
51; CHECK16-NEXT:    [[TMP3:%.*]] = tail call ptr @strchr(ptr nonnull getelementptr inbounds nuw (i8, ptr @str1, i32 2), i32 0)
52; CHECK16-NEXT:    ret ptr [[TMP3]]
53;
54  %tmp3 = tail call ptr @strchr( ptr getelementptr ([8 x i8], ptr @str1, i32 0, i32 2), i32 0 )               ; <ptr> [#uses=1]
55  ret ptr %tmp3
56}
57
58define ptr @test3() {
59; CHECK32-LABEL: @test3(
60; CHECK32-NEXT:  entry:
61; CHECK32-NEXT:    ret ptr null
62;
63; CHECK16-LABEL: @test3(
64; CHECK16-NEXT:  entry:
65; CHECK16-NEXT:    [[TMP3:%.*]] = tail call ptr @strchr(ptr nonnull getelementptr inbounds nuw (i8, ptr @str2, i32 1), i32 80)
66; CHECK16-NEXT:    ret ptr [[TMP3]]
67;
68entry:
69  %tmp3 = tail call ptr @strchr( ptr getelementptr ([5 x i8], ptr @str2, i32 0, i32 1), i32 80 )              ; <ptr> [#uses=1]
70  ret ptr %tmp3
71
72}
73
74@_2E_str = external constant [5 x i8]		; <ptr> [#uses=1]
75
76; A 32-bit compatible memcmp is not recognized as the standard library
77; function on 16-bit targets.
78declare i32 @memcmp(ptr, ptr, i32) nounwind readonly
79
80define i1 @PR2341(ptr %start_addr) {
81; CHECK32-LABEL: @PR2341(
82; CHECK32-NEXT:  entry:
83; CHECK32-NEXT:    [[TMP4:%.*]] = load ptr, ptr [[START_ADDR:%.*]], align 4
84; CHECK32-NEXT:    [[TMP5:%.*]] = call i32 @memcmp(ptr noundef nonnull dereferenceable(4) [[TMP4]], ptr noundef nonnull dereferenceable(4) @_2E_str, i32 4) #[[ATTR0:[0-9]+]]
85; CHECK32-NEXT:    [[TMP6:%.*]] = icmp eq i32 [[TMP5]], 0
86; CHECK32-NEXT:    ret i1 [[TMP6]]
87;
88; CHECK16-LABEL: @PR2341(
89; CHECK16-NEXT:  entry:
90; CHECK16-NEXT:    [[TMP4:%.*]] = load ptr, ptr [[START_ADDR:%.*]], align 4
91; CHECK16-NEXT:    [[TMP5:%.*]] = call i32 @memcmp(ptr [[TMP4]], ptr nonnull @_2E_str, i32 4) #[[ATTR0:[0-9]+]]
92; CHECK16-NEXT:    [[TMP6:%.*]] = icmp eq i32 [[TMP5]], 0
93; CHECK16-NEXT:    ret i1 [[TMP6]]
94;
95entry:
96  %tmp4 = load ptr, ptr %start_addr, align 4		; <ptr> [#uses=1]
97  %tmp5 = call i32 @memcmp( ptr %tmp4, ptr @_2E_str, i32 4 ) nounwind readonly 		; <i32> [#uses=1]
98  %tmp6 = icmp eq i32 %tmp5, 0		; <i1> [#uses=1]
99  ret i1 %tmp6
100
101}
102
103define i32 @PR4284() nounwind {
104; CHECK32-LABEL: @PR4284(
105; CHECK32-NEXT:  entry:
106; CHECK32-NEXT:    ret i32 -65
107;
108; CHECK16-LABEL: @PR4284(
109; CHECK16-NEXT:  entry:
110; CHECK16-NEXT:    [[C0:%.*]] = alloca i8, align 1
111; CHECK16-NEXT:    [[C2:%.*]] = alloca i8, align 1
112; CHECK16-NEXT:    store i8 64, ptr [[C0]], align 1
113; CHECK16-NEXT:    store i8 -127, ptr [[C2]], align 1
114; CHECK16-NEXT:    [[CALL:%.*]] = call i32 @memcmp(ptr nonnull [[C0]], ptr nonnull [[C2]], i32 1)
115; CHECK16-NEXT:    ret i32 [[CALL]]
116;
117entry:
118  %c0 = alloca i8, align 1		; <ptr> [#uses=2]
119  %c2 = alloca i8, align 1		; <ptr> [#uses=2]
120  store i8 64, ptr %c0
121  store i8 -127, ptr %c2
122  %call = call i32 @memcmp(ptr %c0, ptr %c2, i32 1)		; <i32> [#uses=1]
123  ret i32 %call
124
125}
126
127%struct.__sFILE = type { ptr, i32, i32, i16, i16, %struct.__sbuf, i32, ptr, ptr, ptr, ptr, ptr, %struct.__sbuf, ptr, i32, [3 x i8], [1 x i8], %struct.__sbuf, i32, i64, ptr, ptr, i32, i32, %union.anon }
128%struct.__sbuf = type { ptr, i32, [4 x i8] }
129%struct.pthread = type opaque
130%struct.pthread_mutex = type opaque
131%union.anon = type { i64, [120 x i8] }
132@.str13 = external constant [2 x i8]		; <ptr> [#uses=1]
133@.str14 = external constant [2 x i8]		; <ptr> [#uses=1]
134
135define i32 @PR4641(i32 %argc, ptr %argv, i1 %c1, ptr %ptr) nounwind {
136; CHECK-LABEL: @PR4641(
137; CHECK-NEXT:  entry:
138; CHECK-NEXT:    call void @exit(i32 0) #[[ATTR1:[0-9]+]]
139; CHECK-NEXT:    [[COND392:%.*]] = select i1 [[C1:%.*]], ptr @.str13, ptr @.str14
140; CHECK-NEXT:    [[CALL393:%.*]] = call ptr @fopen(ptr [[PTR:%.*]], ptr nonnull [[COND392]]) #[[ATTR1]]
141; CHECK-NEXT:    unreachable
142;
143entry:
144  call void @exit(i32 0) nounwind
145  %cond392 = select i1 %c1, ptr @.str13, ptr @.str14		; <ptr> [#uses=1]
146  %call393 = call ptr @fopen(ptr %ptr, ptr %cond392) nounwind		; <ptr> [#uses=0]
147  unreachable
148}
149
150declare ptr @fopen(ptr, ptr)
151
152; A 32-bit compatible exit is not recognized as the standard library
153; function on 16-bit targets.
154declare void @exit(i32)
155
156define i32 @PR4645(i1 %c1) {
157; CHECK32-LABEL: @PR4645(
158; CHECK32-NEXT:  entry:
159; CHECK32-NEXT:    br label [[IF_THEN:%.*]]
160; CHECK32:       lor.lhs.false:
161; CHECK32-NEXT:    br i1 [[C1:%.*]], label [[IF_THEN]], label [[FOR_COND:%.*]]
162; CHECK32:       if.then:
163; CHECK32-NEXT:    call void @exit(i32 1) #[[ATTR6:[0-9]+]]
164; CHECK32-NEXT:    br label [[FOR_COND]]
165; CHECK32:       for.cond:
166; CHECK32-NEXT:    unreachable
167; CHECK32:       for.end:
168; CHECK32-NEXT:    br label [[FOR_COND]]
169;
170; CHECK16-LABEL: @PR4645(
171; CHECK16-NEXT:  entry:
172; CHECK16-NEXT:    br label [[IF_THEN:%.*]]
173; CHECK16:       lor.lhs.false:
174; CHECK16-NEXT:    br i1 [[C1:%.*]], label [[IF_THEN]], label [[FOR_COND:%.*]]
175; CHECK16:       if.then:
176; CHECK16-NEXT:    call void @exit(i32 1)
177; CHECK16-NEXT:    br label [[FOR_COND]]
178; CHECK16:       for.cond:
179; CHECK16-NEXT:    unreachable
180; CHECK16:       for.end:
181; CHECK16-NEXT:    br label [[FOR_COND]]
182;
183entry:
184  br label %if.then
185
186lor.lhs.false:		; preds = %while.body
187  br i1 %c1, label %if.then, label %for.cond
188
189if.then:		; preds = %lor.lhs.false, %while.body
190  call void @exit(i32 1)
191  br label %for.cond
192
193for.cond:		; preds = %for.end, %if.then, %lor.lhs.false
194  %j.0 = phi i32 [ %inc47, %for.end ], [ 0, %if.then ], [ 0, %lor.lhs.false ]		; <i32> [#uses=1]
195  unreachable
196
197for.end:		; preds = %for.cond20
198  %inc47 = add i32 %j.0, 1		; <i32> [#uses=1]
199  br label %for.cond
200}
201
202@h = constant [2 x i8] c"h\00"		; <ptr> [#uses=1]
203@hel = constant [4 x i8] c"hel\00"		; <ptr> [#uses=1]
204@hello_u = constant [8 x i8] c"hello_u\00"		; <ptr> [#uses=1]
205
206define i32 @MemCpy() {
207; CHECK-LABEL: @MemCpy(
208; CHECK-NEXT:    ret i32 0
209;
210  %target = alloca [1024 x i8]
211  call void @llvm.memcpy.p0.p0.i32(ptr align 2 %target, ptr align 2 @h, i32 2, i1 false)
212  call void @llvm.memcpy.p0.p0.i32(ptr align 4 %target, ptr align 4 @hel, i32 4, i1 false)
213  call void @llvm.memcpy.p0.p0.i32(ptr align 8 %target, ptr align 8 @hello_u, i32 8, i1 false)
214  ret i32 0
215
216}
217
218declare void @llvm.memcpy.p0.p0.i32(ptr nocapture, ptr nocapture, i32, i1) nounwind
219
220; A 32-bit compatible strcmp is not recognized as the standard library
221; function on 16-bit targets.
222declare i32 @strcmp(ptr, ptr) #0
223
224define void @test9(ptr %x) {
225; CHECK32-LABEL: @test9(
226; CHECK32-NEXT:    ret void
227;
228; CHECK16-LABEL: @test9(
229; CHECK16-NEXT:    [[Y:%.*]] = call i32 @strcmp(ptr [[X:%.*]], ptr [[X]]) #[[ATTR6:[0-9]+]]
230; CHECK16-NEXT:    ret void
231;
232  %y = call i32 @strcmp(ptr %x, ptr %x) #1
233  ret void
234}
235
236; PR30484 - https://llvm.org/bugs/show_bug.cgi?id=30484
237; These aren't the library functions you're looking for...
238
239declare i32 @isdigit(i8)
240declare i32 @isascii(i8)
241declare i32 @toascii(i8)
242
243define i32 @fake_isdigit(i8 %x) {
244; CHECK-LABEL: @fake_isdigit(
245; CHECK-NEXT:    [[Y:%.*]] = call i32 @isdigit(i8 [[X:%.*]])
246; CHECK-NEXT:    ret i32 [[Y]]
247;
248  %y = call i32 @isdigit(i8 %x)
249  ret i32 %y
250}
251
252define i32 @fake_isascii(i8 %x) {
253; CHECK-LABEL: @fake_isascii(
254; CHECK-NEXT:    [[Y:%.*]] = call i32 @isascii(i8 [[X:%.*]])
255; CHECK-NEXT:    ret i32 [[Y]]
256;
257  %y = call i32 @isascii(i8 %x)
258  ret i32 %y
259}
260
261define i32 @fake_toascii(i8 %x) {
262; CHECK-LABEL: @fake_toascii(
263; CHECK-NEXT:    [[Y:%.*]] = call i32 @toascii(i8 [[X:%.*]])
264; CHECK-NEXT:    ret i32 [[Y]]
265;
266  %y = call i32 @toascii(i8 %x)
267  ret i32 %y
268}
269
270declare double @pow(double, double)
271declare double @exp2(double)
272
273; check to make sure only the correct libcall attributes are used
274define double @fake_exp2(double %x) {
275; CHECK-LABEL: @fake_exp2(
276; CHECK-NEXT:    [[EXP2:%.*]] = call double @exp2(double [[X:%.*]])
277; CHECK-NEXT:    ret double [[EXP2]]
278;
279
280  %y = call inreg double @pow(double inreg 2.0, double inreg %x)
281  ret double %y
282}
283define double @fake_ldexp(i32 %x) {
284; CHECK32-LABEL: @fake_ldexp(
285; CHECK32-NEXT:    [[LDEXP:%.*]] = call double @ldexp(double 1.000000e+00, i32 [[X:%.*]])
286; CHECK32-NEXT:    ret double [[LDEXP]]
287;
288; CHECK16-LABEL: @fake_ldexp(
289; CHECK16-NEXT:    [[Y:%.*]] = sitofp i32 [[X:%.*]] to double
290; CHECK16-NEXT:    [[Z:%.*]] = call inreg double @exp2(double [[Y]])
291; CHECK16-NEXT:    ret double [[Z]]
292;
293
294
295  %y = sitofp i32 %x to double
296  %z = call inreg double @exp2(double %y)
297  ret double %z
298}
299define double @fake_ldexp_16(i16 %x) {
300; CHECK32-LABEL: @fake_ldexp_16(
301; CHECK32-NEXT:    [[TMP1:%.*]] = sext i16 [[X:%.*]] to i32
302; CHECK32-NEXT:    [[LDEXP:%.*]] = call double @ldexp(double 1.000000e+00, i32 [[TMP1]])
303; CHECK32-NEXT:    ret double [[LDEXP]]
304;
305; CHECK16-LABEL: @fake_ldexp_16(
306; CHECK16-NEXT:    [[LDEXP:%.*]] = call double @ldexp(double 1.000000e+00, i16 [[X:%.*]])
307; CHECK16-NEXT:    ret double [[LDEXP]]
308;
309
310
311  %y = sitofp i16 %x to double
312  %z = call inreg double @exp2(double %y)
313  ret double %z
314}
315
316; PR50885 - this would crash in ValueTracking.
317
318; A 32-bit compatible snprintf is not recognized as the standard library
319; function on 16-bit targets.
320declare i32 @snprintf(ptr, double, ptr)
321
322define i32 @fake_snprintf(i32 %buf, double %len, ptr %str, ptr %ptr) {
323; CHECK-LABEL: @fake_snprintf(
324; CHECK-NEXT:    [[CALL:%.*]] = call i32 @snprintf(ptr [[PTR:%.*]], double [[LEN:%.*]], ptr [[STR:%.*]])
325; CHECK-NEXT:    ret i32 [[CALL]]
326;
327  %call = call i32 @snprintf(ptr %ptr, double %len, ptr %str)
328  ret i32 %call
329}
330
331; Wrong return type for the real strlen.
332; https://llvm.org/PR50836
333
334define i4 @strlen(ptr %s) {
335; CHECK-LABEL: @strlen(
336; CHECK-NEXT:    [[R:%.*]] = call i4 @strlen(ptr [[S:%.*]])
337; CHECK-NEXT:    ret i4 0
338;
339  %r = call i4 @strlen(ptr %s)
340  ret i4 0
341}
342
343; Test emission of stpncpy.
344@a = dso_local global [4 x i8] c"123\00"
345@b = dso_local global [5 x i8] zeroinitializer
346declare ptr @__stpncpy_chk(ptr noundef, ptr noundef, i32 noundef, i32 noundef)
347define signext i32 @emit_stpncpy() {
348; CHECK-LABEL: @emit_stpncpy(
349; CHECK-NEXT:    [[STPNCPY:%.*]] = call ptr @stpncpy(ptr noundef nonnull dereferenceable(1) @b, ptr noundef nonnull dereferenceable(1) @a, i32 2)
350; CHECK-NEXT:    ret i32 0
351;
352  %call = call ptr @__stpncpy_chk(ptr noundef @b,
353  ptr noundef @a,
354  i32 noundef 2, i32 noundef 5)
355  ret i32 0
356}
357
358define void @simplify_memset_chk_pr112633(ptr %p, i32 %conv) {
359; CHECK-LABEL: @simplify_memset_chk_pr112633(
360; CHECK-NEXT:    [[CALL_I:%.*]] = tail call ptr @__memset_chk(ptr [[P:%.*]], i32 range(i32 0, 123) [[CONV:%.*]], i64 1, i64 1)
361; CHECK-NEXT:    ret void
362;
363  %call.i = tail call ptr @__memset_chk(ptr %p, i32 range(i32 0, 123) %conv, i64 1, i64 1)
364  ret void
365}
366
367declare ptr @__memset_chk(ptr, i32, i64, i64)
368
369
370attributes #0 = { nobuiltin }
371attributes #1 = { builtin }
372