basic.ll 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. ; RUN: opt < %s -tailcallelim -S | FileCheck %s
  2. declare void @noarg()
  3. declare void @use(i32*)
  4. declare void @use_nocapture(i32* nocapture)
  5. declare void @use2_nocapture(i32* nocapture, i32* nocapture)
  6. ; Trivial case. Mark @noarg with tail call.
  7. define void @test0() {
  8. ; CHECK: tail call void @noarg()
  9. call void @noarg()
  10. ret void
  11. }
  12. ; PR615. Make sure that we do not move the alloca so that it interferes with the tail call.
  13. define i32 @test1() {
  14. ; CHECK: i32 @test1()
  15. ; CHECK-NEXT: alloca
  16. %A = alloca i32 ; <i32*> [#uses=2]
  17. store i32 5, i32* %A
  18. call void @use(i32* %A)
  19. ; CHECK: tail call i32 @test1
  20. %X = tail call i32 @test1() ; <i32> [#uses=1]
  21. ret i32 %X
  22. }
  23. ; This function contains intervening instructions which should be moved out of the way
  24. define i32 @test2(i32 %X) {
  25. ; CHECK: i32 @test2
  26. ; CHECK-NOT: call
  27. ; CHECK: ret i32
  28. entry:
  29. %tmp.1 = icmp eq i32 %X, 0 ; <i1> [#uses=1]
  30. br i1 %tmp.1, label %then.0, label %endif.0
  31. then.0: ; preds = %entry
  32. %tmp.4 = add i32 %X, 1 ; <i32> [#uses=1]
  33. ret i32 %tmp.4
  34. endif.0: ; preds = %entry
  35. %tmp.10 = add i32 %X, -1 ; <i32> [#uses=1]
  36. %tmp.8 = call i32 @test2(i32 %tmp.10) ; <i32> [#uses=1]
  37. %DUMMY = add i32 %X, 1 ; <i32> [#uses=0]
  38. ret i32 %tmp.8
  39. }
  40. ; Though this case seems to be fairly unlikely to occur in the wild, someone
  41. ; plunked it into the demo script, so maybe they care about it.
  42. define i32 @test3(i32 %c) {
  43. ; CHECK: i32 @test3
  44. ; CHECK-NOT: call
  45. ; CHECK: ret i32 0
  46. entry:
  47. %tmp.1 = icmp eq i32 %c, 0 ; <i1> [#uses=1]
  48. br i1 %tmp.1, label %return, label %else
  49. else: ; preds = %entry
  50. %tmp.5 = add i32 %c, -1 ; <i32> [#uses=1]
  51. %tmp.3 = call i32 @test3(i32 %tmp.5) ; <i32> [#uses=0]
  52. ret i32 0
  53. return: ; preds = %entry
  54. ret i32 0
  55. }
  56. ; Make sure that a nocapture pointer does not stop adding a tail call marker to
  57. ; an unrelated call and additionally that we do not mark the nocapture call with
  58. ; a tail call.
  59. ;
  60. ; rdar://14324281
  61. define void @test4() {
  62. ; CHECK: void @test4
  63. ; CHECK-NOT: tail call void @use_nocapture
  64. ; CHECK: tail call void @noarg()
  65. ; CHECK: ret void
  66. %a = alloca i32
  67. call void @use_nocapture(i32* %a)
  68. call void @noarg()
  69. ret void
  70. }
  71. ; Make sure that we do not perform TRE even with a nocapture use. This is due to
  72. ; bad codegen caused by PR962.
  73. ;
  74. ; rdar://14324281.
  75. define i32* @test5(i32* nocapture %A, i1 %cond) {
  76. ; CHECK: i32* @test5
  77. ; CHECK-NOT: tailrecurse:
  78. ; CHECK: ret i32* null
  79. %B = alloca i32
  80. br i1 %cond, label %cond_true, label %cond_false
  81. cond_true:
  82. call i32* @test5(i32* %B, i1 false)
  83. ret i32* null
  84. cond_false:
  85. call void @use2_nocapture(i32* %A, i32* %B)
  86. call void @noarg()
  87. ret i32* null
  88. }
  89. ; PR14143: Make sure that we do not mark functions with nocapture allocas with tail.
  90. ;
  91. ; rdar://14324281.
  92. define void @test6(i32* %a, i32* %b) {
  93. ; CHECK-LABEL: @test6(
  94. ; CHECK-NOT: tail call
  95. ; CHECK: ret void
  96. %c = alloca [100 x i8], align 16
  97. %tmp = bitcast [100 x i8]* %c to i32*
  98. call void @use2_nocapture(i32* %b, i32* %tmp)
  99. ret void
  100. }
  101. ; PR14143: Make sure that we do not mark functions with nocapture allocas with tail.
  102. ;
  103. ; rdar://14324281
  104. define void @test7(i32* %a, i32* %b) nounwind uwtable {
  105. entry:
  106. ; CHECK-LABEL: @test7(
  107. ; CHECK-NOT: tail call
  108. ; CHECK: ret void
  109. %c = alloca [100 x i8], align 16
  110. %0 = bitcast [100 x i8]* %c to i32*
  111. call void @use2_nocapture(i32* %0, i32* %a)
  112. call void @use2_nocapture(i32* %b, i32* %0)
  113. ret void
  114. }
  115. ; If we have a mix of escaping captured/non-captured allocas, ensure that we do
  116. ; not do anything including marking callsites with the tail call marker.
  117. ;
  118. ; rdar://14324281.
  119. define i32* @test8(i32* nocapture %A, i1 %cond) {
  120. ; CHECK: i32* @test8
  121. ; CHECK-NOT: tailrecurse:
  122. ; CHECK-NOT: tail call
  123. ; CHECK: ret i32* null
  124. %B = alloca i32
  125. %B2 = alloca i32
  126. br i1 %cond, label %cond_true, label %cond_false
  127. cond_true:
  128. call void @use(i32* %B2)
  129. call i32* @test8(i32* %B, i1 false)
  130. ret i32* null
  131. cond_false:
  132. call void @use2_nocapture(i32* %A, i32* %B)
  133. call void @noarg()
  134. ret i32* null
  135. }
  136. ; Don't tail call if a byval arg is captured.
  137. define void @test9(i32* byval %a) {
  138. ; CHECK-LABEL: define void @test9(
  139. ; CHECK: {{^ *}}call void @use(
  140. call void @use(i32* %a)
  141. ret void
  142. }
  143. %struct.X = type { i8* }
  144. declare void @ctor(%struct.X*)
  145. define void @test10(%struct.X* noalias sret %agg.result, i1 zeroext %b) {
  146. ; CHECK-LABEL @test10
  147. entry:
  148. %x = alloca %struct.X, align 8
  149. br i1 %b, label %if.then, label %if.end
  150. if.then: ; preds = %entry
  151. call void @ctor(%struct.X* %agg.result)
  152. ; CHECK: tail call void @ctor
  153. br label %return
  154. if.end:
  155. call void @ctor(%struct.X* %x)
  156. ; CHECK: call void @ctor
  157. br label %return
  158. return:
  159. ret void
  160. }
  161. declare void @test11_helper1(i8** nocapture, i8*)
  162. declare void @test11_helper2(i8*)
  163. define void @test11() {
  164. ; CHECK-LABEL: @test11
  165. ; CHECK-NOT: tail
  166. %a = alloca i8*
  167. %b = alloca i8
  168. call void @test11_helper1(i8** %a, i8* %b) ; a = &b
  169. %c = load i8*, i8** %a
  170. call void @test11_helper2(i8* %c)
  171. ; CHECK: call void @test11_helper2
  172. ret void
  173. }