fprintf-1.ll 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. 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"
  6. %FILE = type { }
  7. @hello_world = constant [13 x i8] c"hello world\0A\00"
  8. @percent_c = constant [3 x i8] c"%c\00"
  9. @percent_d = constant [3 x i8] c"%d\00"
  10. @percent_f = constant [3 x i8] c"%f\00"
  11. @percent_s = constant [3 x i8] c"%s\00"
  12. declare i32 @fprintf(%FILE*, i8*, ...)
  13. ; Check fprintf(fp, "foo") -> fwrite("foo", 3, 1, fp).
  14. define void @test_simplify1(%FILE* %fp) {
  15. ; CHECK-LABEL: @test_simplify1(
  16. %fmt = getelementptr [13 x i8], [13 x i8]* @hello_world, i32 0, i32 0
  17. call i32 (%FILE*, i8*, ...) @fprintf(%FILE* %fp, i8* %fmt)
  18. ; 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)
  19. ret void
  20. ; CHECK-NEXT: ret void
  21. }
  22. ; Check fprintf(fp, "%c", chr) -> fputc(chr, fp).
  23. define void @test_simplify2(%FILE* %fp) {
  24. ; CHECK-LABEL: @test_simplify2(
  25. %fmt = getelementptr [3 x i8], [3 x i8]* @percent_c, i32 0, i32 0
  26. call i32 (%FILE*, i8*, ...) @fprintf(%FILE* %fp, i8* %fmt, i8 104)
  27. ; CHECK-NEXT: call i32 @fputc(i32 104, %FILE* %fp)
  28. ret void
  29. ; CHECK-NEXT: ret void
  30. }
  31. ; Check fprintf(fp, "%s", str) -> fputs(str, fp).
  32. ; NOTE: The fputs simplifier simplifies this further to fwrite.
  33. define void @test_simplify3(%FILE* %fp) {
  34. ; CHECK-LABEL: @test_simplify3(
  35. %fmt = getelementptr [3 x i8], [3 x i8]* @percent_s, i32 0, i32 0
  36. %str = getelementptr [13 x i8], [13 x i8]* @hello_world, i32 0, i32 0
  37. call i32 (%FILE*, i8*, ...) @fprintf(%FILE* %fp, i8* %fmt, i8* %str)
  38. ; 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)
  39. ret void
  40. ; CHECK-NEXT: ret void
  41. }
  42. ; Check fprintf(fp, fmt, ...) -> fiprintf(fp, fmt, ...) if no floating point.
  43. define void @test_simplify4(%FILE* %fp) {
  44. ; CHECK-IPRINTF-LABEL: @test_simplify4(
  45. %fmt = getelementptr [3 x i8], [3 x i8]* @percent_d, i32 0, i32 0
  46. call i32 (%FILE*, i8*, ...) @fprintf(%FILE* %fp, i8* %fmt, i32 187)
  47. ; 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)
  48. ret void
  49. ; CHECK-IPRINTF-NEXT: ret void
  50. }
  51. define void @test_no_simplify1(%FILE* %fp) {
  52. ; CHECK-IPRINTF-LABEL: @test_no_simplify1(
  53. %fmt = getelementptr [3 x i8], [3 x i8]* @percent_f, i32 0, i32 0
  54. call i32 (%FILE*, i8*, ...) @fprintf(%FILE* %fp, i8* %fmt, double 1.87)
  55. ; 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)
  56. ret void
  57. ; CHECK-IPRINTF-NEXT: ret void
  58. }
  59. define void @test_no_simplify2(%FILE* %fp, double %d) {
  60. ; CHECK-LABEL: @test_no_simplify2(
  61. %fmt = getelementptr [3 x i8], [3 x i8]* @percent_f, i32 0, i32 0
  62. call i32 (%FILE*, i8*, ...) @fprintf(%FILE* %fp, i8* %fmt, double %d)
  63. ; 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)
  64. ret void
  65. ; CHECK-NEXT: ret void
  66. }
  67. define i32 @test_no_simplify3(%FILE* %fp) {
  68. ; CHECK-LABEL: @test_no_simplify3(
  69. %fmt = getelementptr [13 x i8], [13 x i8]* @hello_world, i32 0, i32 0
  70. %1 = call i32 (%FILE*, i8*, ...) @fprintf(%FILE* %fp, i8* %fmt)
  71. ; CHECK-NEXT: call i32 (%FILE*, i8*, ...) @fprintf(%FILE* %fp, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @hello_world, i32 0, i32 0))
  72. ret i32 %1
  73. ; CHECK-NEXT: ret i32 %1
  74. }