1 ; Test that the fprintf library call simplifier works correctly.
3 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 ; RUN: opt < %s -mtriple xcore-xmos-elf -instcombine -S | FileCheck %s -check-prefix=IPRINTF
6 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"
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"
16 declare i32 @fprintf(%FILE*, i8*, ...)
18 ; Check fprintf(fp, "foo") -> fwrite("foo", 3, 1, fp).
20 define 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)
26 ; CHECK-NEXT: ret void
29 ; Check fprintf(fp, "%c", chr) -> fputc(chr, fp).
31 define 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)
37 ; CHECK-NEXT: ret void
40 ; Check fprintf(fp, "%s", str) -> fputs(str, fp).
42 define 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)
49 ; CHECK-NEXT: ret void
52 ; Check fprintf(fp, fmt, ...) -> fiprintf(fp, fmt, ...) if no floating point.
54 define 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)
60 ; CHECK-NEXT-IPRINTF: ret void
63 define 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)
69 ; CHECK-NEXT-IPRINTF: ret void
72 define 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)
78 ; CHECK-NEXT: ret void