byval.ll 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ; RUN: opt < %s -inline -S | FileCheck %s
  2. ; Inlining a byval struct should cause an explicit copy into an alloca.
  3. %struct.ss = type { i32, i64 }
  4. @.str = internal constant [10 x i8] c"%d, %lld\0A\00" ; <[10 x i8]*> [#uses=1]
  5. define internal void @f(%struct.ss* byval %b) nounwind {
  6. entry:
  7. %tmp = getelementptr %struct.ss, %struct.ss* %b, i32 0, i32 0 ; <i32*> [#uses=2]
  8. %tmp1 = load i32, i32* %tmp, align 4 ; <i32> [#uses=1]
  9. %tmp2 = add i32 %tmp1, 1 ; <i32> [#uses=1]
  10. store i32 %tmp2, i32* %tmp, align 4
  11. ret void
  12. }
  13. declare i32 @printf(i8*, ...) nounwind
  14. define i32 @test1() nounwind {
  15. entry:
  16. %S = alloca %struct.ss ; <%struct.ss*> [#uses=4]
  17. %tmp1 = getelementptr %struct.ss, %struct.ss* %S, i32 0, i32 0 ; <i32*> [#uses=1]
  18. store i32 1, i32* %tmp1, align 8
  19. %tmp4 = getelementptr %struct.ss, %struct.ss* %S, i32 0, i32 1 ; <i64*> [#uses=1]
  20. store i64 2, i64* %tmp4, align 4
  21. call void @f( %struct.ss* byval %S ) nounwind
  22. ret i32 0
  23. ; CHECK: @test1()
  24. ; CHECK: %S1 = alloca %struct.ss
  25. ; CHECK: %S = alloca %struct.ss
  26. ; CHECK: call void @llvm.memcpy
  27. ; CHECK: ret i32 0
  28. }
  29. ; Inlining a byval struct should NOT cause an explicit copy
  30. ; into an alloca if the function is readonly
  31. define internal i32 @f2(%struct.ss* byval %b) nounwind readonly {
  32. entry:
  33. %tmp = getelementptr %struct.ss, %struct.ss* %b, i32 0, i32 0 ; <i32*> [#uses=2]
  34. %tmp1 = load i32, i32* %tmp, align 4 ; <i32> [#uses=1]
  35. %tmp2 = add i32 %tmp1, 1 ; <i32> [#uses=1]
  36. ret i32 %tmp2
  37. }
  38. define i32 @test2() nounwind {
  39. entry:
  40. %S = alloca %struct.ss ; <%struct.ss*> [#uses=4]
  41. %tmp1 = getelementptr %struct.ss, %struct.ss* %S, i32 0, i32 0 ; <i32*> [#uses=1]
  42. store i32 1, i32* %tmp1, align 8
  43. %tmp4 = getelementptr %struct.ss, %struct.ss* %S, i32 0, i32 1 ; <i64*> [#uses=1]
  44. store i64 2, i64* %tmp4, align 4
  45. %X = call i32 @f2( %struct.ss* byval %S ) nounwind
  46. ret i32 %X
  47. ; CHECK: @test2()
  48. ; CHECK: %S = alloca %struct.ss
  49. ; CHECK-NOT: call void @llvm.memcpy
  50. ; CHECK: ret i32
  51. }
  52. ; Inlining a byval with an explicit alignment needs to use *at least* that
  53. ; alignment on the generated alloca.
  54. ; PR8769
  55. declare void @g3(%struct.ss* %p)
  56. define internal void @f3(%struct.ss* byval align 64 %b) nounwind {
  57. call void @g3(%struct.ss* %b) ;; Could make alignment assumptions!
  58. ret void
  59. }
  60. define void @test3() nounwind {
  61. entry:
  62. %S = alloca %struct.ss, align 1 ;; May not be aligned.
  63. call void @f3( %struct.ss* byval align 64 %S) nounwind
  64. ret void
  65. ; CHECK: @test3()
  66. ; CHECK: %S1 = alloca %struct.ss, align 64
  67. ; CHECK: %S = alloca %struct.ss
  68. ; CHECK: call void @llvm.memcpy
  69. ; CHECK: call void @g3(%struct.ss* %S1)
  70. ; CHECK: ret void
  71. }
  72. ; Inlining a byval struct should NOT cause an explicit copy
  73. ; into an alloca if the function is readonly, but should increase an alloca's
  74. ; alignment to satisfy an explicit alignment request.
  75. define internal i32 @f4(%struct.ss* byval align 64 %b) nounwind readonly {
  76. call void @g3(%struct.ss* %b)
  77. ret i32 4
  78. }
  79. define i32 @test4() nounwind {
  80. entry:
  81. %S = alloca %struct.ss, align 2 ; <%struct.ss*> [#uses=4]
  82. %X = call i32 @f4( %struct.ss* byval align 64 %S ) nounwind
  83. ret i32 %X
  84. ; CHECK: @test4()
  85. ; CHECK: %S = alloca %struct.ss, align 64
  86. ; CHECK-NOT: call void @llvm.memcpy
  87. ; CHECK: call void @g3
  88. ; CHECK: ret i32 4
  89. }
  90. %struct.S0 = type { i32 }
  91. @b = global %struct.S0 { i32 1 }, align 4
  92. @a = common global i32 0, align 4
  93. define internal void @f5(%struct.S0* byval nocapture readonly align 4 %p) {
  94. entry:
  95. store i32 0, i32* getelementptr inbounds (%struct.S0, %struct.S0* @b, i64 0, i32 0), align 4
  96. %f2 = getelementptr inbounds %struct.S0, %struct.S0* %p, i64 0, i32 0
  97. %0 = load i32, i32* %f2, align 4
  98. store i32 %0, i32* @a, align 4
  99. ret void
  100. }
  101. define i32 @test5() {
  102. entry:
  103. tail call void @f5(%struct.S0* byval align 4 @b)
  104. %0 = load i32, i32* @a, align 4
  105. ret i32 %0
  106. ; CHECK: @test5()
  107. ; CHECK: store i32 0, i32* getelementptr inbounds (%struct.S0, %struct.S0* @b, i64 0, i32 0), align 4
  108. ; CHECK-NOT: load i32, i32* getelementptr inbounds (%struct.S0, %struct.S0* @b, i64 0, i32 0), align 4
  109. }