strncmp-1.ll 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ; Test that the strncmp 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"
  4. @hello = constant [6 x i8] c"hello\00"
  5. @hell = constant [5 x i8] c"hell\00"
  6. @bell = constant [5 x i8] c"bell\00"
  7. @null = constant [1 x i8] zeroinitializer
  8. declare i32 @strncmp(i8*, i8*, i32)
  9. ; strncmp("", x, n) -> -*x
  10. define i32 @test1(i8* %str2) {
  11. ; CHECK-LABEL: @test1(
  12. ; CHECK: %strcmpload = load i8, i8* %str
  13. ; CHECK: %1 = zext i8 %strcmpload to i32
  14. ; CHECK: %2 = sub nsw i32 0, %1
  15. ; CHECK: ret i32 %2
  16. %str1 = getelementptr inbounds [1 x i8], [1 x i8]* @null, i32 0, i32 0
  17. %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 10)
  18. ret i32 %temp1
  19. }
  20. ; strncmp(x, "", n) -> *x
  21. define i32 @test2(i8* %str1) {
  22. ; CHECK-LABEL: @test2(
  23. ; CHECK: %strcmpload = load i8, i8* %str1
  24. ; CHECK: %1 = zext i8 %strcmpload to i32
  25. ; CHECK: ret i32 %1
  26. %str2 = getelementptr inbounds [1 x i8], [1 x i8]* @null, i32 0, i32 0
  27. %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 10)
  28. ret i32 %temp1
  29. }
  30. ; strncmp(x, y, n) -> cnst
  31. define i32 @test3() {
  32. ; CHECK-LABEL: @test3(
  33. ; CHECK: ret i32 -1
  34. %str1 = getelementptr inbounds [5 x i8], [5 x i8]* @hell, i32 0, i32 0
  35. %str2 = getelementptr inbounds [6 x i8], [6 x i8]* @hello, i32 0, i32 0
  36. %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 10)
  37. ret i32 %temp1
  38. }
  39. define i32 @test4() {
  40. ; CHECK-LABEL: @test4(
  41. ; CHECK: ret i32 1
  42. %str1 = getelementptr inbounds [5 x i8], [5 x i8]* @hell, i32 0, i32 0
  43. %str2 = getelementptr inbounds [1 x i8], [1 x i8]* @null, i32 0, i32 0
  44. %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 10)
  45. ret i32 %temp1
  46. }
  47. define i32 @test5() {
  48. ; CHECK-LABEL: @test5(
  49. ; CHECK: ret i32 0
  50. %str1 = getelementptr inbounds [5 x i8], [5 x i8]* @hell, i32 0, i32 0
  51. %str2 = getelementptr inbounds [6 x i8], [6 x i8]* @hello, i32 0, i32 0
  52. %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 4)
  53. ret i32 %temp1
  54. }
  55. ; strncmp(x,y,1) -> memcmp(x,y,1)
  56. define i32 @test6(i8* %str1, i8* %str2) {
  57. ; CHECK-LABEL: @test6(
  58. ; CHECK: [[LOAD1:%[a-z]+]] = load i8, i8* %str1, align 1
  59. ; CHECK: [[ZEXT1:%[a-z]+]] = zext i8 [[LOAD1]] to i32
  60. ; CHECK: [[LOAD2:%[a-z]+]] = load i8, i8* %str2, align 1
  61. ; CHECK: [[ZEXT2:%[a-z]+]] = zext i8 [[LOAD2]] to i32
  62. ; CHECK: [[RET:%[a-z]+]] = sub nsw i32 [[ZEXT1]], [[ZEXT2]]
  63. ; CHECK: ret i32 [[RET]]
  64. %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 1)
  65. ret i32 %temp1
  66. }
  67. ; strncmp(x,y,0) -> 0
  68. define i32 @test7(i8* %str1, i8* %str2) {
  69. ; CHECK-LABEL: @test7(
  70. ; CHECK: ret i32 0
  71. %temp1 = call i32 @strncmp(i8* %str1, i8* %str2, i32 0)
  72. ret i32 %temp1
  73. }
  74. ; strncmp(x,x,n) -> 0
  75. define i32 @test8(i8* %str, i32 %n) {
  76. ; CHECK-LABEL: @test8(
  77. ; CHECK: ret i32 0
  78. %temp1 = call i32 @strncmp(i8* %str, i8* %str, i32 %n)
  79. ret i32 %temp1
  80. }