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=CHECK-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-LABEL: @test_simplify1(
22
%fmt = getelementptr [13 x i8], [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], [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-LABEL: @test_simplify2(
33
%fmt = getelementptr [3 x i8], [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).
41
; NOTE: The fputs simplifier simplifies this further to fwrite.
43
define void @test_simplify3(%FILE* %fp) {
44
; CHECK-LABEL: @test_simplify3(
45
%fmt = getelementptr [3 x i8], [3 x i8]* @percent_s, i32 0, i32 0
46
%str = getelementptr [13 x i8], [13 x i8]* @hello_world, i32 0, i32 0
47
call i32 (%FILE*, i8*, ...) @fprintf(%FILE* %fp, i8* %fmt, i8* %str)
48
; CHECK-NEXT: call i32 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @hello_world, i32 0, i32 0), i32 12, i32 1, %FILE* %fp)
50
; CHECK-NEXT: ret void
53
; Check fprintf(fp, fmt, ...) -> fiprintf(fp, fmt, ...) if no floating point.
55
define void @test_simplify4(%FILE* %fp) {
56
; CHECK-IPRINTF-LABEL: @test_simplify4(
57
%fmt = getelementptr [3 x i8], [3 x i8]* @percent_d, i32 0, i32 0
58
call i32 (%FILE*, i8*, ...) @fprintf(%FILE* %fp, i8* %fmt, i32 187)
59
; CHECK-IPRINTF-NEXT: call i32 (%FILE*, i8*, ...) @fiprintf(%FILE* %fp, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @percent_d, i32 0, i32 0), i32 187)
61
; CHECK-IPRINTF-NEXT: ret void
64
define void @test_no_simplify1(%FILE* %fp) {
65
; CHECK-IPRINTF-LABEL: @test_no_simplify1(
66
%fmt = getelementptr [3 x i8], [3 x i8]* @percent_f, i32 0, i32 0
67
call i32 (%FILE*, i8*, ...) @fprintf(%FILE* %fp, i8* %fmt, double 1.87)
68
; CHECK-IPRINTF-NEXT: call i32 (%FILE*, i8*, ...) @fprintf(%FILE* %fp, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @percent_f, i32 0, i32 0), double 1.870000e+00)
70
; CHECK-IPRINTF-NEXT: ret void
73
define void @test_no_simplify2(%FILE* %fp, double %d) {
74
; CHECK-LABEL: @test_no_simplify2(
75
%fmt = getelementptr [3 x i8], [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], [3 x i8]* @percent_f, i32 0, i32 0), double %d)
79
; CHECK-NEXT: ret void
82
define i32 @test_no_simplify3(%FILE* %fp) {
83
; CHECK-LABEL: @test_no_simplify3(
84
%fmt = getelementptr [13 x i8], [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], [13 x i8]* @hello_world, i32 0, i32 0))
88
; CHECK-NEXT: ret i32 %1