1 ; 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=CHECK-IPRINTF 5 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" 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 16 declare i32 @fprintf(%FILE*, i8*, ...) 17 18 ; Check fprintf(fp, "foo") -> fwrite("foo", 3, 1, fp). 19 20 define void @test_simplify1(%FILE* %fp) { 21 ; CHECK-LABEL: @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 31 define void @test_simplify2(%FILE* %fp) { 32 ; CHECK-LABEL: @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 ; NOTE: The fputs simplifier simplifies this further to fwrite. 42 43 define void @test_simplify3(%FILE* %fp) { 44 ; CHECK-LABEL: @test_simplify3( 45 %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) 48 ; CHECK-NEXT: call i32 @fwrite(i8* getelementptr inbounds ([13 x i8]* @hello_world, i32 0, i32 0), i32 12, i32 1, %FILE* %fp) 49 ret void 50 ; CHECK-NEXT: ret void 51 } 52 53 ; Check fprintf(fp, fmt, ...) -> fiprintf(fp, fmt, ...) if no floating point. 54 55 define void @test_simplify4(%FILE* %fp) { 56 ; CHECK-IPRINTF-LABEL: @test_simplify4( 57 %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-IPRINTF-NEXT: 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-IPRINTF-NEXT: ret void 62 } 63 64 define void @test_no_simplify1(%FILE* %fp) { 65 ; CHECK-IPRINTF-LABEL: @test_no_simplify1( 66 %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-IPRINTF-NEXT: 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-IPRINTF-NEXT: ret void 71 } 72 73 define void @test_no_simplify2(%FILE* %fp, double %d) { 74 ; CHECK-LABEL: @test_no_simplify2( 75 %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 } 81 82 define i32 @test_no_simplify3(%FILE* %fp) { 83 ; CHECK-LABEL: @test_no_simplify3( 84 %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 } 90