blob: 97cd7708f9d98f5e1cb6d209189843483716fc96 [file] [log] [blame]
Meador Inge1009cec2012-11-29 15:45:33 +00001; Test that the fprintf library call simplifier works correctly.
2;
3; RUN: opt < %s -instcombine -S | FileCheck %s
4; RUN: opt < %s -mtriple xcore-xmos-elf -instcombine -S | FileCheck %s -check-prefix=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
16declare i32 @fprintf(%FILE*, i8*, ...)
17
18; Check fprintf(fp, "foo") -> fwrite("foo", 3, 1, fp).
19
20define void @test_simplify1(%FILE* %fp) {
21; CHECK: @test_simplify1
22 %fmt = getelementptr [13 x i8]* @hello_world, i32 0, i32 0
23 call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt)
24; CHECK-NEXT: call i32 @fwrite(i8* getelementptr inbounds ([13 x i8]* @hello_world, i32 0, i32 0), i32 12, i32 1, %FILE* %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(%FILE* %fp) {
32; CHECK: @test_simplify2
33 %fmt = getelementptr [3 x i8]* @percent_c, i32 0, i32 0
34 call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt, i8 104)
35; CHECK-NEXT: call i32 @fputc(i32 104, %FILE* %fp)
36 ret void
37; CHECK-NEXT: ret void
38}
39
40; Check fprintf(fp, "%s", str) -> fputs(str, fp).
41
42define void @test_simplify3(%FILE* %fp) {
43; CHECK: @test_simplify3
44 %fmt = getelementptr [3 x i8]* @percent_s, i32 0, i32 0
45 %str = getelementptr [13 x i8]* @hello_world, i32 0, i32 0
46 call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt, i8* %str)
47; CHECK-NEXT: call i32 @fputs(i8* getelementptr inbounds ([13 x i8]* @hello_world, i32 0, i32 0), %FILE* %fp)
48 ret void
49; CHECK-NEXT: ret void
50}
51
52; Check fprintf(fp, fmt, ...) -> fiprintf(fp, fmt, ...) if no floating point.
53
54define void @test_simplify4(%FILE* %fp) {
55; CHECK-IPRINTF: @test_simplify4
56 %fmt = getelementptr [3 x i8]* @percent_d, i32 0, i32 0
57 call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt, i32 187)
58; CHECK-NEXT-IPRINTF: call i32 (%FILE*, i8*, ...)* @fiprintf(%FILE* %fp, i8* getelementptr inbounds ([3 x i8]* @percent_d, i32 0, i32 0), i32 187)
59 ret void
60; CHECK-NEXT-IPRINTF: ret void
61}
62
63define void @test_no_simplify1(%FILE* %fp) {
64; CHECK-IPRINTF: @test_no_simplify1
65 %fmt = getelementptr [3 x i8]* @percent_f, i32 0, i32 0
66 call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt, double 1.87)
67; CHECK-NEXT-IPRINTF: call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* getelementptr inbounds ([3 x i8]* @percent_f, i32 0, i32 0), double 1.870000e+00)
68 ret void
69; CHECK-NEXT-IPRINTF: ret void
70}
71
72define void @test_no_simplify2(%FILE* %fp, double %d) {
73; CHECK: @test_no_simplify2
74 %fmt = getelementptr [3 x i8]* @percent_f, i32 0, i32 0
75 call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt, double %d)
76; CHECK-NEXT: call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* getelementptr inbounds ([3 x i8]* @percent_f, i32 0, i32 0), double %d)
77 ret void
78; CHECK-NEXT: ret void
79}