call_recursive_mutual.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/full.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/function/generic/call_recursive_mutual.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/function/generic/call_recursive_mutual.carbon
  12. // Builds on `call_recursive_basic.carbon`.
  13. // The two specifics for mutual recursive functions F and G,
  14. // when T is a pointer, could be deduplicated.
  15. fn G[T:! Core.Copy](x: T, count: i32) -> T;
  16. fn F[T:! Core.Copy](x: T, count: i32) -> T {
  17. if (count > 3) {
  18. return x;
  19. }
  20. return G(x, count + 1);
  21. }
  22. fn G[T:! Core.Copy](x: T, count: i32) -> T {
  23. if (count > 4) {
  24. return x;
  25. }
  26. return F(x, count + 1);
  27. }
  28. fn M() {
  29. var n: i32 = 0;
  30. var m: f64 = 1.0;
  31. var ptr_i32 : i32*;
  32. var ptr_f64 : f64*;
  33. F(n, 0);
  34. F(m, 0);
  35. F(ptr_i32, 0);
  36. F(ptr_f64, 0);
  37. }
  38. // CHECK:STDOUT: ; ModuleID = 'call_recursive_mutual.carbon'
  39. // CHECK:STDOUT: source_filename = "call_recursive_mutual.carbon"
  40. // CHECK:STDOUT:
  41. // CHECK:STDOUT: ; Function Attrs: nounwind
  42. // CHECK:STDOUT: define void @_CM.Main() #0 !dbg !4 {
  43. // CHECK:STDOUT: entry:
  44. // CHECK:STDOUT: %n.var = alloca i32, align 4, !dbg !7
  45. // CHECK:STDOUT: %m.var = alloca double, align 8, !dbg !8
  46. // CHECK:STDOUT: %ptr_i32.var = alloca ptr, align 8, !dbg !9
  47. // CHECK:STDOUT: %ptr_f64.var = alloca ptr, align 8, !dbg !10
  48. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %n.var), !dbg !7
  49. // CHECK:STDOUT: store i32 0, ptr %n.var, align 4, !dbg !7
  50. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %m.var), !dbg !8
  51. // CHECK:STDOUT: store double 1.000000e+00, ptr %m.var, align 8, !dbg !8
  52. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %ptr_i32.var), !dbg !9
  53. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %ptr_f64.var), !dbg !10
  54. // CHECK:STDOUT: %.loc38_5 = load i32, ptr %n.var, align 4, !dbg !11
  55. // CHECK:STDOUT: %F.call.loc38 = call i32 @_CF.Main.d35bd5561e71da34(i32 %.loc38_5, i32 0), !dbg !12
  56. // CHECK:STDOUT: %.loc39_5 = load double, ptr %m.var, align 8, !dbg !13
  57. // CHECK:STDOUT: %F.call.loc39 = call double @_CF.Main.d5f190bd0393ce29(double %.loc39_5, i32 0), !dbg !14
  58. // CHECK:STDOUT: %.loc40_5 = load ptr, ptr %ptr_i32.var, align 8, !dbg !15
  59. // CHECK:STDOUT: %F.call.loc40 = call ptr @_CF.Main.1aa5241d905aea1e(ptr %.loc40_5, i32 0), !dbg !16
  60. // CHECK:STDOUT: %.loc41_5 = load ptr, ptr %ptr_f64.var, align 8, !dbg !17
  61. // CHECK:STDOUT: %F.call.loc41 = call ptr @_CF.Main.1aa5241d905aea1e(ptr %.loc41_5, i32 0), !dbg !18
  62. // CHECK:STDOUT: ret void, !dbg !19
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
  66. // CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #1
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: ; Function Attrs: nounwind
  69. // CHECK:STDOUT: define linkonce_odr i32 @_CF.Main.d35bd5561e71da34(i32 %x, i32 %count) #0 !dbg !20 {
  70. // CHECK:STDOUT: entry:
  71. // CHECK:STDOUT: %Int.as.OrderedWith.impl.Greater.call = icmp sgt i32 %count, 3, !dbg !27
  72. // CHECK:STDOUT: br i1 %Int.as.OrderedWith.impl.Greater.call, label %if.then, label %if.else, !dbg !28
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: if.then: ; preds = %entry
  75. // CHECK:STDOUT: ret i32 %x, !dbg !29
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: if.else: ; preds = %entry
  78. // CHECK:STDOUT: %Int.as.AddWith.impl.Op.call = add i32 %count, 1, !dbg !30
  79. // CHECK:STDOUT: %G.call = call i32 @_CG.Main.d35bd5561e71da34(i32 %x, i32 %Int.as.AddWith.impl.Op.call), !dbg !31
  80. // CHECK:STDOUT: ret i32 %G.call, !dbg !32
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: ; Function Attrs: nounwind
  84. // CHECK:STDOUT: define linkonce_odr double @_CF.Main.d5f190bd0393ce29(double %x, i32 %count) #0 !dbg !33 {
  85. // CHECK:STDOUT: entry:
  86. // CHECK:STDOUT: %Int.as.OrderedWith.impl.Greater.call = icmp sgt i32 %count, 3, !dbg !40
  87. // CHECK:STDOUT: br i1 %Int.as.OrderedWith.impl.Greater.call, label %if.then, label %if.else, !dbg !41
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: if.then: ; preds = %entry
  90. // CHECK:STDOUT: ret double %x, !dbg !42
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: if.else: ; preds = %entry
  93. // CHECK:STDOUT: %Int.as.AddWith.impl.Op.call = add i32 %count, 1, !dbg !43
  94. // CHECK:STDOUT: %G.call = call double @_CG.Main.d5f190bd0393ce29(double %x, i32 %Int.as.AddWith.impl.Op.call), !dbg !44
  95. // CHECK:STDOUT: ret double %G.call, !dbg !45
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: ; Function Attrs: nounwind
  99. // CHECK:STDOUT: define linkonce_odr ptr @_CF.Main.1aa5241d905aea1e(ptr %x, i32 %count) #0 !dbg !46 {
  100. // CHECK:STDOUT: entry:
  101. // CHECK:STDOUT: %Int.as.OrderedWith.impl.Greater.call = icmp sgt i32 %count, 3, !dbg !50
  102. // CHECK:STDOUT: br i1 %Int.as.OrderedWith.impl.Greater.call, label %if.then, label %if.else, !dbg !51
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: if.then: ; preds = %entry
  105. // CHECK:STDOUT: ret ptr %x, !dbg !52
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: if.else: ; preds = %entry
  108. // CHECK:STDOUT: %Int.as.AddWith.impl.Op.call = add i32 %count, 1, !dbg !53
  109. // CHECK:STDOUT: %G.call = call ptr @_CG.Main.1aa5241d905aea1e(ptr %x, i32 %Int.as.AddWith.impl.Op.call), !dbg !54
  110. // CHECK:STDOUT: ret ptr %G.call, !dbg !55
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: ; Function Attrs: nounwind
  114. // CHECK:STDOUT: define linkonce_odr i32 @_CG.Main.d35bd5561e71da34(i32 %x, i32 %count) #0 !dbg !56 {
  115. // CHECK:STDOUT: entry:
  116. // CHECK:STDOUT: %Int.as.OrderedWith.impl.Greater.call = icmp sgt i32 %count, 4, !dbg !60
  117. // CHECK:STDOUT: br i1 %Int.as.OrderedWith.impl.Greater.call, label %if.then, label %if.else, !dbg !61
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: if.then: ; preds = %entry
  120. // CHECK:STDOUT: ret i32 %x, !dbg !62
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: if.else: ; preds = %entry
  123. // CHECK:STDOUT: %Int.as.AddWith.impl.Op.call = add i32 %count, 1, !dbg !63
  124. // CHECK:STDOUT: %F.call = call i32 @_CF.Main.d35bd5561e71da34(i32 %x, i32 %Int.as.AddWith.impl.Op.call), !dbg !64
  125. // CHECK:STDOUT: ret i32 %F.call, !dbg !65
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: ; Function Attrs: nounwind
  129. // CHECK:STDOUT: define linkonce_odr double @_CG.Main.d5f190bd0393ce29(double %x, i32 %count) #0 !dbg !66 {
  130. // CHECK:STDOUT: entry:
  131. // CHECK:STDOUT: %Int.as.OrderedWith.impl.Greater.call = icmp sgt i32 %count, 4, !dbg !70
  132. // CHECK:STDOUT: br i1 %Int.as.OrderedWith.impl.Greater.call, label %if.then, label %if.else, !dbg !71
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: if.then: ; preds = %entry
  135. // CHECK:STDOUT: ret double %x, !dbg !72
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: if.else: ; preds = %entry
  138. // CHECK:STDOUT: %Int.as.AddWith.impl.Op.call = add i32 %count, 1, !dbg !73
  139. // CHECK:STDOUT: %F.call = call double @_CF.Main.d5f190bd0393ce29(double %x, i32 %Int.as.AddWith.impl.Op.call), !dbg !74
  140. // CHECK:STDOUT: ret double %F.call, !dbg !75
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: ; Function Attrs: nounwind
  144. // CHECK:STDOUT: define linkonce_odr ptr @_CG.Main.1aa5241d905aea1e(ptr %x, i32 %count) #0 !dbg !76 {
  145. // CHECK:STDOUT: entry:
  146. // CHECK:STDOUT: %Int.as.OrderedWith.impl.Greater.call = icmp sgt i32 %count, 4, !dbg !80
  147. // CHECK:STDOUT: br i1 %Int.as.OrderedWith.impl.Greater.call, label %if.then, label %if.else, !dbg !81
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: if.then: ; preds = %entry
  150. // CHECK:STDOUT: ret ptr %x, !dbg !82
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: if.else: ; preds = %entry
  153. // CHECK:STDOUT: %Int.as.AddWith.impl.Op.call = add i32 %count, 1, !dbg !83
  154. // CHECK:STDOUT: %F.call = call ptr @_CF.Main.1aa5241d905aea1e(ptr %x, i32 %Int.as.AddWith.impl.Op.call), !dbg !84
  155. // CHECK:STDOUT: ret ptr %F.call, !dbg !85
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: ; uselistorder directives
  159. // CHECK:STDOUT: uselistorder ptr @llvm.lifetime.start.p0, { 3, 2, 1, 0 }
  160. // CHECK:STDOUT: uselistorder ptr @_CF.Main.1aa5241d905aea1e, { 1, 2, 0 }
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: attributes #0 = { nounwind }
  163. // CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  166. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  169. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  170. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  171. // CHECK:STDOUT: !3 = !DIFile(filename: "call_recursive_mutual.carbon", directory: "")
  172. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "M", linkageName: "_CM.Main", scope: null, file: !3, line: 32, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  173. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  174. // CHECK:STDOUT: !6 = !{null}
  175. // CHECK:STDOUT: !7 = !DILocation(line: 33, column: 3, scope: !4)
  176. // CHECK:STDOUT: !8 = !DILocation(line: 34, column: 3, scope: !4)
  177. // CHECK:STDOUT: !9 = !DILocation(line: 35, column: 3, scope: !4)
  178. // CHECK:STDOUT: !10 = !DILocation(line: 36, column: 3, scope: !4)
  179. // CHECK:STDOUT: !11 = !DILocation(line: 38, column: 5, scope: !4)
  180. // CHECK:STDOUT: !12 = !DILocation(line: 38, column: 3, scope: !4)
  181. // CHECK:STDOUT: !13 = !DILocation(line: 39, column: 5, scope: !4)
  182. // CHECK:STDOUT: !14 = !DILocation(line: 39, column: 3, scope: !4)
  183. // CHECK:STDOUT: !15 = !DILocation(line: 40, column: 5, scope: !4)
  184. // CHECK:STDOUT: !16 = !DILocation(line: 40, column: 3, scope: !4)
  185. // CHECK:STDOUT: !17 = !DILocation(line: 41, column: 5, scope: !4)
  186. // CHECK:STDOUT: !18 = !DILocation(line: 41, column: 3, scope: !4)
  187. // CHECK:STDOUT: !19 = !DILocation(line: 32, column: 1, scope: !4)
  188. // CHECK:STDOUT: !20 = distinct !DISubprogram(name: "F", linkageName: "_CF.Main.d35bd5561e71da34", scope: null, file: !3, line: 18, type: !21, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !24)
  189. // CHECK:STDOUT: !21 = !DISubroutineType(types: !22)
  190. // CHECK:STDOUT: !22 = !{!23, !23, !23}
  191. // CHECK:STDOUT: !23 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
  192. // CHECK:STDOUT: !24 = !{!25, !26}
  193. // CHECK:STDOUT: !25 = !DILocalVariable(arg: 1, scope: !20, type: !23)
  194. // CHECK:STDOUT: !26 = !DILocalVariable(arg: 2, scope: !20, type: !23)
  195. // CHECK:STDOUT: !27 = !DILocation(line: 19, column: 7, scope: !20)
  196. // CHECK:STDOUT: !28 = !DILocation(line: 19, column: 6, scope: !20)
  197. // CHECK:STDOUT: !29 = !DILocation(line: 20, column: 5, scope: !20)
  198. // CHECK:STDOUT: !30 = !DILocation(line: 22, column: 15, scope: !20)
  199. // CHECK:STDOUT: !31 = !DILocation(line: 22, column: 10, scope: !20)
  200. // CHECK:STDOUT: !32 = !DILocation(line: 22, column: 3, scope: !20)
  201. // CHECK:STDOUT: !33 = distinct !DISubprogram(name: "F", linkageName: "_CF.Main.d5f190bd0393ce29", scope: null, file: !3, line: 18, type: !34, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !37)
  202. // CHECK:STDOUT: !34 = !DISubroutineType(types: !35)
  203. // CHECK:STDOUT: !35 = !{!36, !36, !23}
  204. // CHECK:STDOUT: !36 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 8)
  205. // CHECK:STDOUT: !37 = !{!38, !39}
  206. // CHECK:STDOUT: !38 = !DILocalVariable(arg: 1, scope: !33, type: !36)
  207. // CHECK:STDOUT: !39 = !DILocalVariable(arg: 2, scope: !33, type: !23)
  208. // CHECK:STDOUT: !40 = !DILocation(line: 19, column: 7, scope: !33)
  209. // CHECK:STDOUT: !41 = !DILocation(line: 19, column: 6, scope: !33)
  210. // CHECK:STDOUT: !42 = !DILocation(line: 20, column: 5, scope: !33)
  211. // CHECK:STDOUT: !43 = !DILocation(line: 22, column: 15, scope: !33)
  212. // CHECK:STDOUT: !44 = !DILocation(line: 22, column: 10, scope: !33)
  213. // CHECK:STDOUT: !45 = !DILocation(line: 22, column: 3, scope: !33)
  214. // CHECK:STDOUT: !46 = distinct !DISubprogram(name: "F", linkageName: "_CF.Main.1aa5241d905aea1e", scope: null, file: !3, line: 18, type: !34, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !47)
  215. // CHECK:STDOUT: !47 = !{!48, !49}
  216. // CHECK:STDOUT: !48 = !DILocalVariable(arg: 1, scope: !46, type: !36)
  217. // CHECK:STDOUT: !49 = !DILocalVariable(arg: 2, scope: !46, type: !23)
  218. // CHECK:STDOUT: !50 = !DILocation(line: 19, column: 7, scope: !46)
  219. // CHECK:STDOUT: !51 = !DILocation(line: 19, column: 6, scope: !46)
  220. // CHECK:STDOUT: !52 = !DILocation(line: 20, column: 5, scope: !46)
  221. // CHECK:STDOUT: !53 = !DILocation(line: 22, column: 15, scope: !46)
  222. // CHECK:STDOUT: !54 = !DILocation(line: 22, column: 10, scope: !46)
  223. // CHECK:STDOUT: !55 = !DILocation(line: 22, column: 3, scope: !46)
  224. // CHECK:STDOUT: !56 = distinct !DISubprogram(name: "G", linkageName: "_CG.Main.d35bd5561e71da34", scope: null, file: !3, line: 25, type: !21, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !57)
  225. // CHECK:STDOUT: !57 = !{!58, !59}
  226. // CHECK:STDOUT: !58 = !DILocalVariable(arg: 1, scope: !56, type: !23)
  227. // CHECK:STDOUT: !59 = !DILocalVariable(arg: 2, scope: !56, type: !23)
  228. // CHECK:STDOUT: !60 = !DILocation(line: 26, column: 7, scope: !56)
  229. // CHECK:STDOUT: !61 = !DILocation(line: 26, column: 6, scope: !56)
  230. // CHECK:STDOUT: !62 = !DILocation(line: 27, column: 5, scope: !56)
  231. // CHECK:STDOUT: !63 = !DILocation(line: 29, column: 15, scope: !56)
  232. // CHECK:STDOUT: !64 = !DILocation(line: 29, column: 10, scope: !56)
  233. // CHECK:STDOUT: !65 = !DILocation(line: 29, column: 3, scope: !56)
  234. // CHECK:STDOUT: !66 = distinct !DISubprogram(name: "G", linkageName: "_CG.Main.d5f190bd0393ce29", scope: null, file: !3, line: 25, type: !34, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !67)
  235. // CHECK:STDOUT: !67 = !{!68, !69}
  236. // CHECK:STDOUT: !68 = !DILocalVariable(arg: 1, scope: !66, type: !36)
  237. // CHECK:STDOUT: !69 = !DILocalVariable(arg: 2, scope: !66, type: !23)
  238. // CHECK:STDOUT: !70 = !DILocation(line: 26, column: 7, scope: !66)
  239. // CHECK:STDOUT: !71 = !DILocation(line: 26, column: 6, scope: !66)
  240. // CHECK:STDOUT: !72 = !DILocation(line: 27, column: 5, scope: !66)
  241. // CHECK:STDOUT: !73 = !DILocation(line: 29, column: 15, scope: !66)
  242. // CHECK:STDOUT: !74 = !DILocation(line: 29, column: 10, scope: !66)
  243. // CHECK:STDOUT: !75 = !DILocation(line: 29, column: 3, scope: !66)
  244. // CHECK:STDOUT: !76 = distinct !DISubprogram(name: "G", linkageName: "_CG.Main.1aa5241d905aea1e", scope: null, file: !3, line: 25, type: !34, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !77)
  245. // CHECK:STDOUT: !77 = !{!78, !79}
  246. // CHECK:STDOUT: !78 = !DILocalVariable(arg: 1, scope: !76, type: !36)
  247. // CHECK:STDOUT: !79 = !DILocalVariable(arg: 2, scope: !76, type: !23)
  248. // CHECK:STDOUT: !80 = !DILocation(line: 26, column: 7, scope: !76)
  249. // CHECK:STDOUT: !81 = !DILocation(line: 26, column: 6, scope: !76)
  250. // CHECK:STDOUT: !82 = !DILocation(line: 27, column: 5, scope: !76)
  251. // CHECK:STDOUT: !83 = !DILocation(line: 29, column: 15, scope: !76)
  252. // CHECK:STDOUT: !84 = !DILocation(line: 29, column: 10, scope: !76)
  253. // CHECK:STDOUT: !85 = !DILocation(line: 29, column: 3, scope: !76)