2
0

memchr.ll 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ; Test that the memchr 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. @hellonull = constant [14 x i8] c"hello\00world\5Cn\00"
  6. @null = constant [1 x i8] zeroinitializer
  7. @newlines = constant [3 x i8] c"\0D\0A\00"
  8. @single = constant [2 x i8] c"\1F\00"
  9. @spaces = constant [4 x i8] c" \0D\0A\00"
  10. @negative = constant [3 x i8] c"\FF\FE\00"
  11. @chp = global i8* zeroinitializer
  12. declare i8* @memchr(i8*, i32, i32)
  13. define void @test1() {
  14. ; CHECK-LABEL: @test1
  15. ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 6)
  16. ; CHECK-NOT: call i8* @memchr
  17. ; CHECK: ret void
  18. %str = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
  19. %dst = call i8* @memchr(i8* %str, i32 119, i32 14)
  20. store i8* %dst, i8** @chp
  21. ret void
  22. }
  23. define void @test2() {
  24. ; CHECK-LABEL: @test2
  25. ; CHECK: store i8* null, i8** @chp, align 4
  26. ; CHECK-NOT: call i8* @memchr
  27. ; CHECK: ret void
  28. %str = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
  29. %dst = call i8* @memchr(i8* %str, i32 119, i32 1)
  30. store i8* %dst, i8** @chp
  31. ret void
  32. }
  33. define void @test3() {
  34. ; CHECK-LABEL: @test3
  35. ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 13)
  36. ; CHECK-NOT: call i8* @memchr
  37. ; CHECK: ret void
  38. %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
  39. %dst = call i8* @memchr(i8* %src, i32 0, i32 14)
  40. store i8* %dst, i8** @chp
  41. ret void
  42. }
  43. define void @test4(i32 %chr) {
  44. ; CHECK-LABEL: @test4
  45. ; CHECK: call i8* @memchr
  46. ; CHECK-NOT: call i8* @memchr
  47. ; CHECK: ret void
  48. %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
  49. %dst = call i8* @memchr(i8* %src, i32 %chr, i32 14)
  50. store i8* %dst, i8** @chp
  51. ret void
  52. }
  53. define void @test5() {
  54. ; CHECK-LABEL: @test5
  55. ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 13)
  56. ; CHECK-NOT: call i8* @memchr
  57. ; CHECK: ret void
  58. %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
  59. %dst = call i8* @memchr(i8* %src, i32 65280, i32 14)
  60. store i8* %dst, i8** @chp
  61. ret void
  62. }
  63. define void @test6() {
  64. ; CHECK-LABEL: @test6
  65. ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 6)
  66. ; CHECK-NOT: call i8* @memchr
  67. ; CHECK: ret void
  68. %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
  69. ; Overflow, but we still find the right thing.
  70. %dst = call i8* @memchr(i8* %src, i32 119, i32 100)
  71. store i8* %dst, i8** @chp
  72. ret void
  73. }
  74. define void @test7() {
  75. ; CHECK-LABEL: @test7
  76. ; CHECK: store i8* null, i8** @chp, align 4
  77. ; CHECK-NOT: call i8* @memchr
  78. ; CHECK: ret void
  79. %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
  80. ; Overflow
  81. %dst = call i8* @memchr(i8* %src, i32 120, i32 100)
  82. store i8* %dst, i8** @chp
  83. ret void
  84. }
  85. define void @test8() {
  86. ; CHECK-LABEL: @test8
  87. ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hellonull, i32 0, i32 6)
  88. ; CHECK-NOT: call i8* @memchr
  89. ; CHECK: ret void
  90. %str = getelementptr [14 x i8], [14 x i8]* @hellonull, i32 0, i32 0
  91. %dst = call i8* @memchr(i8* %str, i32 119, i32 14)
  92. store i8* %dst, i8** @chp
  93. ret void
  94. }
  95. define void @test9() {
  96. ; CHECK-LABEL: @test9
  97. ; CHECK: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hellonull, i32 0, i32 6)
  98. ; CHECK-NOT: call i8* @memchr
  99. ; CHECK: ret void
  100. %str = getelementptr [14 x i8], [14 x i8]* @hellonull, i32 0, i32 2
  101. %dst = call i8* @memchr(i8* %str, i32 119, i32 12)
  102. store i8* %dst, i8** @chp
  103. ret void
  104. }
  105. define void @test10() {
  106. ; CHECK-LABEL: @test10
  107. ; CHECK: store i8* null, i8** @chp, align 4
  108. ; CHECK-NOT: call i8* @memchr
  109. ; CHECK: ret void
  110. %str = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
  111. %dst = call i8* @memchr(i8* %str, i32 119, i32 6)
  112. store i8* %dst, i8** @chp
  113. ret void
  114. }
  115. ; Check transformation memchr("\r\n", C, 2) != nullptr -> (C & 9216) != 0
  116. define i1 @test11(i32 %C) {
  117. ; CHECK-LABEL: @test11
  118. ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %C to i16
  119. ; CHECK-NEXT: %memchr.bounds = icmp ult i16 [[TRUNC]], 16
  120. ; CHECK-NEXT: [[SHL:%.*]] = shl i16 1, [[TRUNC]]
  121. ; CHECK-NEXT: [[AND:%.*]] = and i16 [[SHL]], 9216
  122. ; CHECK-NEXT: %memchr.bits = icmp ne i16 [[AND]], 0
  123. ; CHECK-NEXT: %memchr = and i1 %memchr.bounds, %memchr.bits
  124. ; CHECK-NEXT: ret i1 %memchr
  125. %dst = call i8* @memchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @newlines, i64 0, i64 0), i32 %C, i32 2)
  126. %cmp = icmp ne i8* %dst, null
  127. ret i1 %cmp
  128. }
  129. ; No 64 bits here
  130. define i1 @test12(i32 %C) {
  131. ; CHECK-LABEL: @test12
  132. ; CHECK-NEXT: %dst = call i8* @memchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @spaces, i32 0, i32 0), i32 %C, i32 3)
  133. ; CHECK-NEXT: %cmp = icmp ne i8* %dst, null
  134. ; CHECK-NEXT: ret i1 %cmp
  135. %dst = call i8* @memchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @spaces, i64 0, i64 0), i32 %C, i32 3)
  136. %cmp = icmp ne i8* %dst, null
  137. ret i1 %cmp
  138. }
  139. define i1 @test13(i32 %C) {
  140. ; CHECK-LABEL: @test13
  141. ; CHECK-NEXT: %memchr.bounds = icmp ult i32 %C, 32
  142. ; CHECK-NEXT: [[SHL:%.*]] = shl i32 1, %C
  143. ; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHL]], -2147483647
  144. ; CHECK-NEXT: %memchr.bits = icmp ne i32 [[AND]], 0
  145. ; CHECK-NEXT: %memchr = and i1 %memchr.bounds, %memchr.bits
  146. ; CHECK-NEXT: ret i1 %memchr
  147. %dst = call i8* @memchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @single, i64 0, i64 0), i32 %C, i32 2)
  148. %cmp = icmp ne i8* %dst, null
  149. ret i1 %cmp
  150. }
  151. define i1 @test14(i32 %C) {
  152. ; CHECK-LABEL: @test14
  153. ; CHECK-NEXT: icmp eq i32 %C, 31
  154. ; CHECK-NEXT: ret
  155. %dst = call i8* @memchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @single, i64 0, i64 0), i32 %C, i32 1)
  156. %cmp = icmp ne i8* %dst, null
  157. ret i1 %cmp
  158. }
  159. define i1 @test15(i32 %C) {
  160. ; CHECK-LABEL: @test15
  161. ; CHECK-NEXT: %dst = call i8* @memchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @negative, i32 0, i32 0), i32 %C, i32 3)
  162. ; CHECK-NEXT: %cmp = icmp ne i8* %dst, null
  163. ; CHECK-NEXT: ret i1 %cmp
  164. %dst = call i8* @memchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @negative, i64 0, i64 0), i32 %C, i32 3)
  165. %cmp = icmp ne i8* %dst, null
  166. ret i1 %cmp
  167. }