printf-1.ll 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ; Test that the printf 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. @hello_world = constant [13 x i8] c"hello world\0A\00"
  7. @h = constant [2 x i8] c"h\00"
  8. @percent = constant [2 x i8] c"%\00"
  9. @percent_c = constant [3 x i8] c"%c\00"
  10. @percent_d = constant [3 x i8] c"%d\00"
  11. @percent_f = constant [3 x i8] c"%f\00"
  12. @percent_s = constant [4 x i8] c"%s\0A\00"
  13. @empty = constant [1 x i8] c"\00"
  14. ; CHECK: [[STR:@[a-z0-9]+]] = private unnamed_addr constant [12 x i8] c"hello world\00"
  15. declare i32 @printf(i8*, ...)
  16. ; Check printf("") -> noop.
  17. define void @test_simplify1() {
  18. ; CHECK-LABEL: @test_simplify1(
  19. %fmt = getelementptr [1 x i8], [1 x i8]* @empty, i32 0, i32 0
  20. call i32 (i8*, ...) @printf(i8* %fmt)
  21. ret void
  22. ; CHECK-NEXT: ret void
  23. }
  24. ; Check printf("x") -> putchar('x'), even for '%'.
  25. define void @test_simplify2() {
  26. ; CHECK-LABEL: @test_simplify2(
  27. %fmt = getelementptr [2 x i8], [2 x i8]* @h, i32 0, i32 0
  28. call i32 (i8*, ...) @printf(i8* %fmt)
  29. ; CHECK-NEXT: call i32 @putchar(i32 104)
  30. ret void
  31. ; CHECK-NEXT: ret void
  32. }
  33. define void @test_simplify3() {
  34. ; CHECK-LABEL: @test_simplify3(
  35. %fmt = getelementptr [2 x i8], [2 x i8]* @percent, i32 0, i32 0
  36. call i32 (i8*, ...) @printf(i8* %fmt)
  37. ; CHECK-NEXT: call i32 @putchar(i32 37)
  38. ret void
  39. ; CHECK-NEXT: ret void
  40. }
  41. ; Check printf("foo\n") -> puts("foo").
  42. define void @test_simplify4() {
  43. ; CHECK-LABEL: @test_simplify4(
  44. %fmt = getelementptr [13 x i8], [13 x i8]* @hello_world, i32 0, i32 0
  45. call i32 (i8*, ...) @printf(i8* %fmt)
  46. ; CHECK-NEXT: call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* [[STR]], i32 0, i32 0))
  47. ret void
  48. ; CHECK-NEXT: ret void
  49. }
  50. ; Check printf("%c", chr) -> putchar(chr).
  51. define void @test_simplify5() {
  52. ; CHECK-LABEL: @test_simplify5(
  53. %fmt = getelementptr [3 x i8], [3 x i8]* @percent_c, i32 0, i32 0
  54. call i32 (i8*, ...) @printf(i8* %fmt, i8 104)
  55. ; CHECK-NEXT: call i32 @putchar(i32 104)
  56. ret void
  57. ; CHECK-NEXT: ret void
  58. }
  59. ; Check printf("%s\n", str) -> puts(str).
  60. define void @test_simplify6() {
  61. ; CHECK-LABEL: @test_simplify6(
  62. %fmt = getelementptr [4 x i8], [4 x i8]* @percent_s, i32 0, i32 0
  63. %str = getelementptr [13 x i8], [13 x i8]* @hello_world, i32 0, i32 0
  64. call i32 (i8*, ...) @printf(i8* %fmt, i8* %str)
  65. ; CHECK-NEXT: call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @hello_world, i32 0, i32 0))
  66. ret void
  67. ; CHECK-NEXT: ret void
  68. }
  69. ; Check printf(format, ...) -> iprintf(format, ...) if no floating point.
  70. define void @test_simplify7() {
  71. ; CHECK-IPRINTF-LABEL: @test_simplify7(
  72. %fmt = getelementptr [3 x i8], [3 x i8]* @percent_d, i32 0, i32 0
  73. call i32 (i8*, ...) @printf(i8* %fmt, i32 187)
  74. ; CHECK-IPRINTF-NEXT: call i32 (i8*, ...) @iprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @percent_d, i32 0, i32 0), i32 187)
  75. ret void
  76. ; CHECK-IPRINTF-NEXT: ret void
  77. }
  78. define void @test_no_simplify1() {
  79. ; CHECK-IPRINTF-LABEL: @test_no_simplify1(
  80. %fmt = getelementptr [3 x i8], [3 x i8]* @percent_f, i32 0, i32 0
  81. call i32 (i8*, ...) @printf(i8* %fmt, double 1.87)
  82. ; CHECK-IPRINTF-NEXT: call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @percent_f, i32 0, i32 0), double 1.870000e+00)
  83. ret void
  84. ; CHECK-IPRINTF-NEXT: ret void
  85. }
  86. define void @test_no_simplify2(i8* %fmt, double %d) {
  87. ; CHECK-LABEL: @test_no_simplify2(
  88. call i32 (i8*, ...) @printf(i8* %fmt, double %d)
  89. ; CHECK-NEXT: call i32 (i8*, ...) @printf(i8* %fmt, double %d)
  90. ret void
  91. ; CHECK-NEXT: ret void
  92. }
  93. define i32 @test_no_simplify3() {
  94. ; CHECK-LABEL: @test_no_simplify3(
  95. %fmt = getelementptr [2 x i8], [2 x i8]* @h, i32 0, i32 0
  96. %ret = call i32 (i8*, ...) @printf(i8* %fmt)
  97. ; CHECK-NEXT: call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @h, i32 0, i32 0))
  98. ret i32 %ret
  99. ; CHECK-NEXT: ret i32 %ret
  100. }