strlen-1.ll 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. ; Test that the strlen library call simplifier works correctly.
  2. ;
  3. ; RUN: opt < %s -instcombine -S | FileCheck %s
  4. 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"
  5. @hello = constant [6 x i8] c"hello\00"
  6. @longer = constant [7 x i8] c"longer\00"
  7. @null = constant [1 x i8] zeroinitializer
  8. @null_hello = constant [7 x i8] c"\00hello\00"
  9. @nullstring = constant i8 0
  10. @a = common global [32 x i8] zeroinitializer, align 1
  11. declare i32 @strlen(i8*)
  12. ; Check strlen(string constant) -> integer constant.
  13. define i32 @test_simplify1() {
  14. ; CHECK-LABEL: @test_simplify1(
  15. %hello_p = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
  16. %hello_l = call i32 @strlen(i8* %hello_p)
  17. ret i32 %hello_l
  18. ; CHECK-NEXT: ret i32 5
  19. }
  20. define i32 @test_simplify2() {
  21. ; CHECK-LABEL: @test_simplify2(
  22. %null_p = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
  23. %null_l = call i32 @strlen(i8* %null_p)
  24. ret i32 %null_l
  25. ; CHECK-NEXT: ret i32 0
  26. }
  27. define i32 @test_simplify3() {
  28. ; CHECK-LABEL: @test_simplify3(
  29. %null_hello_p = getelementptr [7 x i8], [7 x i8]* @null_hello, i32 0, i32 0
  30. %null_hello_l = call i32 @strlen(i8* %null_hello_p)
  31. ret i32 %null_hello_l
  32. ; CHECK-NEXT: ret i32 0
  33. }
  34. define i32 @test_simplify4() {
  35. ; CHECK-LABEL: @test_simplify4(
  36. %len = tail call i32 @strlen(i8* @nullstring) nounwind
  37. ret i32 %len
  38. ; CHECK-NEXT: ret i32 0
  39. }
  40. ; Check strlen(x) == 0 --> *x == 0.
  41. define i1 @test_simplify5() {
  42. ; CHECK-LABEL: @test_simplify5(
  43. %hello_p = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
  44. %hello_l = call i32 @strlen(i8* %hello_p)
  45. %eq_hello = icmp eq i32 %hello_l, 0
  46. ret i1 %eq_hello
  47. ; CHECK-NEXT: ret i1 false
  48. }
  49. define i1 @test_simplify6() {
  50. ; CHECK-LABEL: @test_simplify6(
  51. %null_p = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
  52. %null_l = call i32 @strlen(i8* %null_p)
  53. %eq_null = icmp eq i32 %null_l, 0
  54. ret i1 %eq_null
  55. ; CHECK-NEXT: ret i1 true
  56. }
  57. ; Check strlen(x) != 0 --> *x != 0.
  58. define i1 @test_simplify7() {
  59. ; CHECK-LABEL: @test_simplify7(
  60. %hello_p = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
  61. %hello_l = call i32 @strlen(i8* %hello_p)
  62. %ne_hello = icmp ne i32 %hello_l, 0
  63. ret i1 %ne_hello
  64. ; CHECK-NEXT: ret i1 true
  65. }
  66. define i1 @test_simplify8() {
  67. ; CHECK-LABEL: @test_simplify8(
  68. %null_p = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
  69. %null_l = call i32 @strlen(i8* %null_p)
  70. %ne_null = icmp ne i32 %null_l, 0
  71. ret i1 %ne_null
  72. ; CHECK-NEXT: ret i1 false
  73. }
  74. define i32 @test_simplify9(i1 %x) {
  75. ; CHECK-LABEL: @test_simplify9
  76. %hello = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
  77. %longer = getelementptr [7 x i8], [7 x i8]* @longer, i32 0, i32 0
  78. %s = select i1 %x, i8* %hello, i8* %longer
  79. %l = call i32 @strlen(i8* %s)
  80. ; CHECK-NEXT: select i1 %x, i32 5, i32 6
  81. ret i32 %l
  82. ; CHECK-NEXT: ret
  83. }
  84. ; Check cases that shouldn't be simplified.
  85. define i32 @test_no_simplify1() {
  86. ; CHECK-LABEL: @test_no_simplify1(
  87. %a_p = getelementptr [32 x i8], [32 x i8]* @a, i32 0, i32 0
  88. %a_l = call i32 @strlen(i8* %a_p)
  89. ; CHECK-NEXT: %a_l = call i32 @strlen
  90. ret i32 %a_l
  91. ; CHECK-NEXT: ret i32 %a_l
  92. }