xref: /llvm-project/llvm/test/Transforms/InstCombine/fprintf-1.ll (revision 89274584eeca019d1ebe6e22ebfa9d03ce8563f2)
1; Test that the fprintf library call simplifier works correctly.
2;
3; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4; RUN: opt < %s -mtriple xcore-xmos-elf -passes=instcombine -S | FileCheck %s -check-prefix=CHECK-IPRINTF
5
6target 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
8%FILE = type { }
9
10@hello_world = constant [13 x i8] c"hello world\0A\00"
11@percent_c = constant [3 x i8] c"%c\00"
12@percent_d = constant [3 x i8] c"%d\00"
13@percent_f = constant [3 x i8] c"%f\00"
14@percent_s = constant [3 x i8] c"%s\00"
15@percent_m = constant [3 x i8] c"%m\00"
16
17declare i32 @fprintf(ptr, ptr, ...)
18
19; Check fprintf(fp, "foo") -> fwrite("foo", 3, 1, fp).
20
21define void @test_simplify1(ptr %fp) {
22; CHECK-LABEL: @test_simplify1(
23  call i32 (ptr, ptr, ...) @fprintf(ptr %fp, ptr @hello_world)
24; CHECK-NEXT: call i32 @fwrite(ptr nonnull @hello_world, i32 12, i32 1, ptr %fp)
25  ret void
26; CHECK-NEXT: ret void
27}
28
29; Check fprintf(fp, "%c", chr) -> fputc(chr, fp).
30
31define void @test_simplify2(ptr %fp) {
32; CHECK-LABEL: @test_simplify2(
33  call i32 (ptr, ptr, ...) @fprintf(ptr %fp, ptr @percent_c, i8 104)
34; CHECK-NEXT: call i32 @fputc(i32 104, ptr %fp)
35  ret void
36; CHECK-NEXT: ret void
37}
38
39; Check fprintf(fp, "%s", str) -> fputs(str, fp).
40; NOTE: The fputs simplifier simplifies this further to fwrite.
41
42define void @test_simplify3(ptr %fp) {
43; CHECK-LABEL: @test_simplify3(
44  call i32 (ptr, ptr, ...) @fprintf(ptr %fp, ptr @percent_s, ptr @hello_world)
45; CHECK-NEXT: call i32 @fwrite(ptr nonnull @hello_world, i32 12, i32 1, ptr %fp)
46  ret void
47; CHECK-NEXT: ret void
48}
49
50; Check fprintf(fp, fmt, ...) -> fiprintf(fp, fmt, ...) if no floating point.
51
52define void @test_simplify4(ptr %fp) {
53; CHECK-IPRINTF-LABEL: @test_simplify4(
54  call i32 (ptr, ptr, ...) @fprintf(ptr %fp, ptr @percent_d, i32 187)
55; CHECK-IPRINTF-NEXT: call i32 (ptr, ptr, ...) @fiprintf(ptr %fp, ptr nonnull @percent_d, i32 187)
56  ret void
57; CHECK-IPRINTF-NEXT: ret void
58}
59
60define void @test_simplify5(ptr %fp) {
61; CHECK-LABEL: @test_simplify5(
62  call i32 (ptr, ptr, ...) @fprintf(ptr %fp, ptr @hello_world) [ "deopt"() ]
63; CHECK-NEXT: call i32 @fwrite(ptr nonnull @hello_world, i32 12, i32 1, ptr %fp) [ "deopt"() ]
64  ret void
65; CHECK-NEXT: ret void
66}
67
68define void @test_no_simplify1(ptr %fp) {
69; CHECK-IPRINTF-LABEL: @test_no_simplify1(
70  call i32 (ptr, ptr, ...) @fprintf(ptr %fp, ptr @percent_f, double 1.87)
71; CHECK-IPRINTF-NEXT: call i32 (ptr, ptr, ...) @fprintf(ptr %fp, ptr nonnull @percent_f, double 1.870000e+00)
72  ret void
73; CHECK-IPRINTF-NEXT: ret void
74}
75
76define void @test_no_simplify2(ptr %fp, double %d) {
77; CHECK-LABEL: @test_no_simplify2(
78  call i32 (ptr, ptr, ...) @fprintf(ptr %fp, ptr @percent_f, double %d)
79; CHECK-NEXT: call i32 (ptr, ptr, ...) @fprintf(ptr %fp, ptr nonnull @percent_f, double %d)
80  ret void
81; CHECK-NEXT: ret void
82}
83
84define i32 @test_no_simplify3(ptr %fp) {
85; CHECK-LABEL: @test_no_simplify3(
86  %1 = call i32 (ptr, ptr, ...) @fprintf(ptr %fp, ptr @hello_world)
87; CHECK-NEXT: call i32 (ptr, ptr, ...) @fprintf(ptr %fp, ptr nonnull @hello_world)
88  ret i32 %1
89; CHECK-NEXT: ret i32 %1
90}
91
92; Verify that a call with a format string containing just the %m directive
93; and no arguments is not simplified.
94
95define void @test_no_simplify4(ptr %fp) {
96; CHECK-LABEL: @test_no_simplify4(
97  call i32 (ptr, ptr, ...) @fprintf(ptr %fp, ptr @percent_m)
98; CHECK-NEXT: call i32 (ptr, ptr, ...) @fprintf(ptr %fp, ptr nonnull @percent_m)
99  ret void
100; CHECK-NEXT: ret void
101}
102