strcpy_chk-1.ll 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ; Test lib call simplification of __strcpy_chk calls with various values
  2. ; for src, dst, and slen.
  3. ;
  4. ; RUN: opt < %s -instcombine -S | FileCheck %s
  5. 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"
  6. @a = common global [60 x i8] zeroinitializer, align 1
  7. @b = common global [60 x i8] zeroinitializer, align 1
  8. @.str = private constant [12 x i8] c"abcdefghijk\00"
  9. ; Check cases where slen >= strlen (src).
  10. define i8* @test_simplify1() {
  11. ; CHECK-LABEL: @test_simplify1(
  12. %dst = getelementptr inbounds [60 x i8], [60 x i8]* @a, i32 0, i32 0
  13. %src = getelementptr inbounds [12 x i8], [12 x i8]* @.str, i32 0, i32 0
  14. ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0), i32 12, i32 1, i1 false)
  15. ; CHECK-NEXT: ret i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0)
  16. %ret = call i8* @__strcpy_chk(i8* %dst, i8* %src, i32 60)
  17. ret i8* %ret
  18. }
  19. define i8* @test_simplify2() {
  20. ; CHECK-LABEL: @test_simplify2(
  21. %dst = getelementptr inbounds [60 x i8], [60 x i8]* @a, i32 0, i32 0
  22. %src = getelementptr inbounds [12 x i8], [12 x i8]* @.str, i32 0, i32 0
  23. ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0), i32 12, i32 1, i1 false)
  24. ; CHECK-NEXT: ret i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0)
  25. %ret = call i8* @__strcpy_chk(i8* %dst, i8* %src, i32 12)
  26. ret i8* %ret
  27. }
  28. define i8* @test_simplify3() {
  29. ; CHECK-LABEL: @test_simplify3(
  30. %dst = getelementptr inbounds [60 x i8], [60 x i8]* @a, i32 0, i32 0
  31. %src = getelementptr inbounds [12 x i8], [12 x i8]* @.str, i32 0, i32 0
  32. ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0), i32 12, i32 1, i1 false)
  33. ; CHECK-NEXT: ret i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0)
  34. %ret = call i8* @__strcpy_chk(i8* %dst, i8* %src, i32 -1)
  35. ret i8* %ret
  36. }
  37. ; Check cases where there are no string constants.
  38. define i8* @test_simplify4() {
  39. ; CHECK-LABEL: @test_simplify4(
  40. %dst = getelementptr inbounds [60 x i8], [60 x i8]* @a, i32 0, i32 0
  41. %src = getelementptr inbounds [60 x i8], [60 x i8]* @b, i32 0, i32 0
  42. ; CHECK-NEXT: %strcpy = call i8* @strcpy(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i8* getelementptr inbounds ([60 x i8], [60 x i8]* @b, i32 0, i32 0))
  43. ; CHECK-NEXT: ret i8* %strcpy
  44. %ret = call i8* @__strcpy_chk(i8* %dst, i8* %src, i32 -1)
  45. ret i8* %ret
  46. }
  47. ; Check case where the string length is not constant.
  48. define i8* @test_simplify5() {
  49. ; CHECK-LABEL: @test_simplify5(
  50. %dst = getelementptr inbounds [60 x i8], [60 x i8]* @a, i32 0, i32 0
  51. %src = getelementptr inbounds [12 x i8], [12 x i8]* @.str, i32 0, i32 0
  52. ; CHECK-NEXT: %len = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i1 false)
  53. ; CHECK-NEXT: %1 = call i8* @__memcpy_chk(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0), i32 12, i32 %len)
  54. ; CHECK-NEXT: ret i8* %1
  55. %len = call i32 @llvm.objectsize.i32.p0i8(i8* %dst, i1 false)
  56. %ret = call i8* @__strcpy_chk(i8* %dst, i8* %src, i32 %len)
  57. ret i8* %ret
  58. }
  59. ; Check case where the source and destination are the same.
  60. define i8* @test_simplify6() {
  61. ; CHECK-LABEL: @test_simplify6(
  62. %dst = getelementptr inbounds [60 x i8], [60 x i8]* @a, i32 0, i32 0
  63. ; CHECK-NEXT: %len = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i1 false)
  64. ; CHECK-NEXT: %ret = call i8* @__strcpy_chk(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i32 %len)
  65. ; CHECK-NEXT: ret i8* %ret
  66. %len = call i32 @llvm.objectsize.i32.p0i8(i8* %dst, i1 false)
  67. %ret = call i8* @__strcpy_chk(i8* %dst, i8* %dst, i32 %len)
  68. ret i8* %ret
  69. }
  70. ; Check case where slen < strlen (src).
  71. define i8* @test_no_simplify1() {
  72. ; CHECK-LABEL: @test_no_simplify1(
  73. %dst = getelementptr inbounds [60 x i8], [60 x i8]* @a, i32 0, i32 0
  74. %src = getelementptr inbounds [60 x i8], [60 x i8]* @b, i32 0, i32 0
  75. ; CHECK-NEXT: %ret = call i8* @__strcpy_chk(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i8* getelementptr inbounds ([60 x i8], [60 x i8]* @b, i32 0, i32 0), i32 8)
  76. ; CHECK-NEXT: ret i8* %ret
  77. %ret = call i8* @__strcpy_chk(i8* %dst, i8* %src, i32 8)
  78. ret i8* %ret
  79. }
  80. declare i8* @__strcpy_chk(i8*, i8*, i32) nounwind
  81. declare i32 @llvm.objectsize.i32.p0i8(i8*, i1) nounwind readonly