inline-tail.ll 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ; RUN: opt < %s -inline -S | FileCheck %s
  2. ; We have to apply the less restrictive TailCallKind of the call site being
  3. ; inlined and any call sites cloned into the caller.
  4. ; No tail marker after inlining, since test_capture_c captures an alloca.
  5. ; CHECK: define void @test_capture_a(
  6. ; CHECK-NOT: tail
  7. ; CHECK: call void @test_capture_c(
  8. declare void @test_capture_c(i32*)
  9. define internal void @test_capture_b(i32* %P) {
  10. tail call void @test_capture_c(i32* %P)
  11. ret void
  12. }
  13. define void @test_capture_a() {
  14. %A = alloca i32 ; captured by test_capture_b
  15. call void @test_capture_b(i32* %A)
  16. ret void
  17. }
  18. ; No musttail marker after inlining, since the prototypes don't match.
  19. ; CHECK: define void @test_proto_mismatch_a(
  20. ; CHECK-NOT: musttail
  21. ; CHECK: call void @test_proto_mismatch_c(
  22. declare void @test_proto_mismatch_c(i32*)
  23. define internal void @test_proto_mismatch_b(i32* %p) {
  24. musttail call void @test_proto_mismatch_c(i32* %p)
  25. ret void
  26. }
  27. define void @test_proto_mismatch_a() {
  28. call void @test_proto_mismatch_b(i32* null)
  29. ret void
  30. }
  31. ; After inlining through a musttail call site, we need to keep musttail markers
  32. ; to prevent unbounded stack growth.
  33. ; CHECK: define void @test_musttail_basic_a(
  34. ; CHECK: musttail call void @test_musttail_basic_c(
  35. declare void @test_musttail_basic_c(i32* %p)
  36. define internal void @test_musttail_basic_b(i32* %p) {
  37. musttail call void @test_musttail_basic_c(i32* %p)
  38. ret void
  39. }
  40. define void @test_musttail_basic_a(i32* %p) {
  41. musttail call void @test_musttail_basic_b(i32* %p)
  42. ret void
  43. }
  44. ; Don't insert lifetime end markers here, the lifetime is trivially over due
  45. ; the return.
  46. ; CHECK: define void @test_byval_a(
  47. ; CHECK: musttail call void @test_byval_c(
  48. ; CHECK-NEXT: ret void
  49. declare void @test_byval_c(i32* byval %p)
  50. define internal void @test_byval_b(i32* byval %p) {
  51. musttail call void @test_byval_c(i32* byval %p)
  52. ret void
  53. }
  54. define void @test_byval_a(i32* byval %p) {
  55. musttail call void @test_byval_b(i32* byval %p)
  56. ret void
  57. }
  58. ; Don't insert a stack restore, we're about to return.
  59. ; CHECK: define void @test_dynalloca_a(
  60. ; CHECK: call i8* @llvm.stacksave(
  61. ; CHECK: alloca i8, i32 %n
  62. ; CHECK: musttail call void @test_dynalloca_c(
  63. ; CHECK-NEXT: ret void
  64. declare void @escape(i8* %buf)
  65. declare void @test_dynalloca_c(i32* byval %p, i32 %n)
  66. define internal void @test_dynalloca_b(i32* byval %p, i32 %n) alwaysinline {
  67. %buf = alloca i8, i32 %n ; dynamic alloca
  68. call void @escape(i8* %buf) ; escape it
  69. musttail call void @test_dynalloca_c(i32* byval %p, i32 %n)
  70. ret void
  71. }
  72. define void @test_dynalloca_a(i32* byval %p, i32 %n) {
  73. musttail call void @test_dynalloca_b(i32* byval %p, i32 %n)
  74. ret void
  75. }
  76. ; We can't merge the returns.
  77. ; CHECK: define void @test_multiret_a(
  78. ; CHECK: musttail call void @test_multiret_c(
  79. ; CHECK-NEXT: ret void
  80. ; CHECK: musttail call void @test_multiret_d(
  81. ; CHECK-NEXT: ret void
  82. declare void @test_multiret_c(i1 zeroext %b)
  83. declare void @test_multiret_d(i1 zeroext %b)
  84. define internal void @test_multiret_b(i1 zeroext %b) {
  85. br i1 %b, label %c, label %d
  86. c:
  87. musttail call void @test_multiret_c(i1 zeroext %b)
  88. ret void
  89. d:
  90. musttail call void @test_multiret_d(i1 zeroext %b)
  91. ret void
  92. }
  93. define void @test_multiret_a(i1 zeroext %b) {
  94. musttail call void @test_multiret_b(i1 zeroext %b)
  95. ret void
  96. }
  97. ; We have to avoid bitcast chains.
  98. ; CHECK: define i32* @test_retptr_a(
  99. ; CHECK: musttail call i8* @test_retptr_c(
  100. ; CHECK-NEXT: bitcast i8* {{.*}} to i32*
  101. ; CHECK-NEXT: ret i32*
  102. declare i8* @test_retptr_c()
  103. define internal i16* @test_retptr_b() {
  104. %rv = musttail call i8* @test_retptr_c()
  105. %v = bitcast i8* %rv to i16*
  106. ret i16* %v
  107. }
  108. define i32* @test_retptr_a() {
  109. %rv = musttail call i16* @test_retptr_b()
  110. %v = bitcast i16* %rv to i32*
  111. ret i32* %v
  112. }
  113. ; Combine the last two cases: multiple returns with pointer bitcasts.
  114. ; CHECK: define i32* @test_multiptrret_a(
  115. ; CHECK: musttail call i8* @test_multiptrret_c(
  116. ; CHECK-NEXT: bitcast i8* {{.*}} to i32*
  117. ; CHECK-NEXT: ret i32*
  118. ; CHECK: musttail call i8* @test_multiptrret_d(
  119. ; CHECK-NEXT: bitcast i8* {{.*}} to i32*
  120. ; CHECK-NEXT: ret i32*
  121. declare i8* @test_multiptrret_c(i1 zeroext %b)
  122. declare i8* @test_multiptrret_d(i1 zeroext %b)
  123. define internal i16* @test_multiptrret_b(i1 zeroext %b) {
  124. br i1 %b, label %c, label %d
  125. c:
  126. %c_rv = musttail call i8* @test_multiptrret_c(i1 zeroext %b)
  127. %c_v = bitcast i8* %c_rv to i16*
  128. ret i16* %c_v
  129. d:
  130. %d_rv = musttail call i8* @test_multiptrret_d(i1 zeroext %b)
  131. %d_v = bitcast i8* %d_rv to i16*
  132. ret i16* %d_v
  133. }
  134. define i32* @test_multiptrret_a(i1 zeroext %b) {
  135. %rv = musttail call i16* @test_multiptrret_b(i1 zeroext %b)
  136. %v = bitcast i16* %rv to i32*
  137. ret i32* %v
  138. }
  139. ; Inline a musttail call site which contains a normal return and a musttail call.
  140. ; CHECK: define i32 @test_mixedret_a(
  141. ; CHECK: br i1 %b
  142. ; CHECK: musttail call i32 @test_mixedret_c(
  143. ; CHECK-NEXT: ret i32
  144. ; CHECK: call i32 @test_mixedret_d(i1 zeroext %b)
  145. ; CHECK: add i32 1,
  146. ; CHECK-NOT: br
  147. ; CHECK: ret i32
  148. declare i32 @test_mixedret_c(i1 zeroext %b)
  149. declare i32 @test_mixedret_d(i1 zeroext %b)
  150. define internal i32 @test_mixedret_b(i1 zeroext %b) {
  151. br i1 %b, label %c, label %d
  152. c:
  153. %c_rv = musttail call i32 @test_mixedret_c(i1 zeroext %b)
  154. ret i32 %c_rv
  155. d:
  156. %d_rv = call i32 @test_mixedret_d(i1 zeroext %b)
  157. %d_rv1 = add i32 1, %d_rv
  158. ret i32 %d_rv1
  159. }
  160. define i32 @test_mixedret_a(i1 zeroext %b) {
  161. %rv = musttail call i32 @test_mixedret_b(i1 zeroext %b)
  162. ret i32 %rv
  163. }