blob: 1b7c104051e4789145b8841673aa2cf62880face [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) {
Stephen Linc1c7a132013-07-14 01:42:54 +000021; CHECK-LABEL: @test_simplify1(
Meador Inge1009cec2012-11-29 15:45:33 +000022 %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) {
Stephen Linc1c7a132013-07-14 01:42:54 +000032; CHECK-LABEL: @test_simplify2(
Meador Inge1009cec2012-11-29 15:45:33 +000033 %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).
Meador Ingef8e72502012-11-29 15:45:43 +000041; NOTE: The fputs simplifier simplifies this further to fwrite.
Meador Inge1009cec2012-11-29 15:45:33 +000042
43define void @test_simplify3(%FILE* %fp) {
Stephen Linc1c7a132013-07-14 01:42:54 +000044; CHECK-LABEL: @test_simplify3(
Meador Inge1009cec2012-11-29 15:45:33 +000045 %fmt = getelementptr [3 x i8]* @percent_s, i32 0, i32 0
46 %str = getelementptr [13 x i8]* @hello_world, i32 0, i32 0
47 call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt, i8* %str)
Meador Ingef8e72502012-11-29 15:45:43 +000048; CHECK-NEXT: call i32 @fwrite(i8* getelementptr inbounds ([13 x i8]* @hello_world, i32 0, i32 0), i32 12, i32 1, %FILE* %fp)
Meador Inge1009cec2012-11-29 15:45:33 +000049 ret void
50; CHECK-NEXT: ret void
51}
52
53; Check fprintf(fp, fmt, ...) -> fiprintf(fp, fmt, ...) if no floating point.
54
55define void @test_simplify4(%FILE* %fp) {
Stephen Linc1c7a132013-07-14 01:42:54 +000056; CHECK-IPRINTF-LABEL: @test_simplify4(
Meador Inge1009cec2012-11-29 15:45:33 +000057 %fmt = getelementptr [3 x i8]* @percent_d, i32 0, i32 0
58 call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt, i32 187)
59; 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 ret void
61; CHECK-NEXT-IPRINTF: ret void
62}
63
64define void @test_no_simplify1(%FILE* %fp) {
Stephen Linc1c7a132013-07-14 01:42:54 +000065; CHECK-IPRINTF-LABEL: @test_no_simplify1(
Meador Inge1009cec2012-11-29 15:45:33 +000066 %fmt = getelementptr [3 x i8]* @percent_f, i32 0, i32 0
67 call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt, double 1.87)
68; 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 ret void
70; CHECK-NEXT-IPRINTF: ret void
71}
72
73define void @test_no_simplify2(%FILE* %fp, double %d) {
Stephen Linc1c7a132013-07-14 01:42:54 +000074; CHECK-LABEL: @test_no_simplify2(
Meador Inge1009cec2012-11-29 15:45:33 +000075 %fmt = getelementptr [3 x i8]* @percent_f, i32 0, i32 0
76 call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt, double %d)
77; CHECK-NEXT: call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* getelementptr inbounds ([3 x i8]* @percent_f, i32 0, i32 0), double %d)
78 ret void
79; CHECK-NEXT: ret void
80}
Peter Collingbourne37ae72b2013-04-17 02:01:10 +000081
82define i32 @test_no_simplify3(%FILE* %fp) {
Stephen Linc1c7a132013-07-14 01:42:54 +000083; CHECK-LABEL: @test_no_simplify3(
Peter Collingbourne37ae72b2013-04-17 02:01:10 +000084 %fmt = getelementptr [13 x i8]* @hello_world, i32 0, i32 0
85 %1 = call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* %fmt)
86; CHECK-NEXT: call i32 (%FILE*, i8*, ...)* @fprintf(%FILE* %fp, i8* getelementptr inbounds ([13 x i8]* @hello_world, i32 0, i32 0))
87 ret i32 %1
88; CHECK-NEXT: ret i32 %1
89}