strcmp-1.ll 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ; Test that the strcmp 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 @strcmp(i8*, i8*)
  9. ; strcmp("", x) -> -*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 @strcmp(i8* %str1, i8* %str2)
  18. ret i32 %temp1
  19. }
  20. ; strcmp(x, "") -> *x
  21. define i32 @test2(i8* %str1) {
  22. ; CHECK-LABEL: @test2(
  23. ; CHECK: %strcmpload = load i8, i8* %str
  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 @strcmp(i8* %str1, i8* %str2)
  28. ret i32 %temp1
  29. }
  30. ; strcmp(x, y) -> 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 @strcmp(i8* %str1, i8* %str2)
  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 @strcmp(i8* %str1, i8* %str2)
  45. ret i32 %temp1
  46. }
  47. ; strcmp(x, y) -> memcmp(x, y, <known length>)
  48. ; (This transform is rather difficult to trigger in a useful manner)
  49. define i32 @test5(i1 %b) {
  50. ; CHECK-LABEL: @test5(
  51. ; CHECK: %memcmp = call i32 @memcmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i32 0, i32 0), i8* %str2, i32 5)
  52. ; CHECK: ret i32 %memcmp
  53. %str1 = getelementptr inbounds [6 x i8], [6 x i8]* @hello, i32 0, i32 0
  54. %temp1 = getelementptr inbounds [5 x i8], [5 x i8]* @hell, i32 0, i32 0
  55. %temp2 = getelementptr inbounds [5 x i8], [5 x i8]* @bell, i32 0, i32 0
  56. %str2 = select i1 %b, i8* %temp1, i8* %temp2
  57. %temp3 = call i32 @strcmp(i8* %str1, i8* %str2)
  58. ret i32 %temp3
  59. }
  60. ; strcmp(x,x) -> 0
  61. define i32 @test6(i8* %str) {
  62. ; CHECK-LABEL: @test6(
  63. ; CHECK: ret i32 0
  64. %temp1 = call i32 @strcmp(i8* %str, i8* %str)
  65. ret i32 %temp1
  66. }