strchr-1.ll 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ; Test that the strchr library call simplifier works correctly.
  2. ; RUN: opt < %s -instcombine -S | FileCheck %s
  3. 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-n8:16:32"
  4. @hello = constant [14 x i8] c"hello world\5Cn\00"
  5. @null = constant [1 x i8] zeroinitializer
  6. @newlines = constant [3 x i8] c"\0D\0A\00"
  7. @chp = global i8* zeroinitializer
  8. declare i8* @strchr(i8*, i32)
  9. define void @test_simplify1() {
  10. ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 6)
  11. ; CHECK-NOT: call i8* @strchr
  12. ; CHECK: ret void
  13. %str = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
  14. %dst = call i8* @strchr(i8* %str, i32 119)
  15. store i8* %dst, i8** @chp
  16. ret void
  17. }
  18. define void @test_simplify2() {
  19. ; CHECK: store i8* null, i8** @chp, align 4
  20. ; CHECK-NOT: call i8* @strchr
  21. ; CHECK: ret void
  22. %str = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
  23. %dst = call i8* @strchr(i8* %str, i32 119)
  24. store i8* %dst, i8** @chp
  25. ret void
  26. }
  27. define void @test_simplify3() {
  28. ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 13)
  29. ; CHECK-NOT: call i8* @strchr
  30. ; CHECK: ret void
  31. %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
  32. %dst = call i8* @strchr(i8* %src, i32 0)
  33. store i8* %dst, i8** @chp
  34. ret void
  35. }
  36. define void @test_simplify4(i32 %chr) {
  37. ; CHECK: call i8* @memchr
  38. ; CHECK-NOT: call i8* @strchr
  39. ; CHECK: ret void
  40. %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
  41. %dst = call i8* @strchr(i8* %src, i32 %chr)
  42. store i8* %dst, i8** @chp
  43. ret void
  44. }
  45. define void @test_simplify5() {
  46. ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 13)
  47. ; CHECK-NOT: call i8* @strchr
  48. ; CHECK: ret void
  49. %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
  50. %dst = call i8* @strchr(i8* %src, i32 65280)
  51. store i8* %dst, i8** @chp
  52. ret void
  53. }
  54. ; Check transformation strchr(p, 0) -> p + strlen(p)
  55. define void @test_simplify6(i8* %str) {
  56. ; CHECK: %strlen = call i32 @strlen(i8* %str)
  57. ; CHECK-NOT: call i8* @strchr
  58. ; CHECK: %strchr = getelementptr i8, i8* %str, i32 %strlen
  59. ; CHECK: store i8* %strchr, i8** @chp, align 4
  60. ; CHECK: ret void
  61. %dst = call i8* @strchr(i8* %str, i32 0)
  62. store i8* %dst, i8** @chp
  63. ret void
  64. }
  65. ; Check transformation strchr("\r\n", C) != nullptr -> (C & 9217) != 0
  66. define i1 @test_simplify7(i32 %C) {
  67. ; CHECK-LABEL: @test_simplify7
  68. ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %C to i16
  69. ; CHECK-NEXT: %memchr.bounds = icmp ult i16 [[TRUNC]], 16
  70. ; CHECK-NEXT: [[SHL:%.*]] = shl i16 1, [[TRUNC]]
  71. ; CHECK-NEXT: [[AND:%.*]] = and i16 [[SHL]], 9217
  72. ; CHECK-NEXT: %memchr.bits = icmp ne i16 [[AND]], 0
  73. ; CHECK-NEXT: %memchr1 = and i1 %memchr.bounds, %memchr.bits
  74. ; CHECK-NEXT: ret i1 %memchr1
  75. %dst = call i8* @strchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @newlines, i64 0, i64 0), i32 %C)
  76. %cmp = icmp ne i8* %dst, null
  77. ret i1 %cmp
  78. }