call_recursive_basic.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_basic.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_basic.carbon
  12. class C {
  13. impl as Core.Copy {
  14. fn Op[self: Self]() -> Self { return {}; }
  15. }
  16. }
  17. // The two specifics for recursive function F, when T is a pointer, could be
  18. // deduplicated.
  19. fn F[T:! Core.Copy](x: T, count: i32) -> T {
  20. if (count > 0) {
  21. return x;
  22. }
  23. return F(x, count + 1);
  24. }
  25. fn M() {
  26. var n: i32 = 0;
  27. var m: f64 = 1.0;
  28. var ptr_i32 : i32*;
  29. var ptr_f64 : f64*;
  30. F(n, 0);
  31. F(m, 0);
  32. F(ptr_i32, 0);
  33. F(ptr_f64, 0);
  34. var c: C;
  35. F(c, 0);
  36. }
  37. // CHECK:STDOUT: ; ModuleID = 'call_recursive_basic.carbon'
  38. // CHECK:STDOUT: source_filename = "call_recursive_basic.carbon"
  39. // CHECK:STDOUT:
  40. // CHECK:STDOUT: @C.val.loc15_44 = internal constant {} zeroinitializer
  41. // CHECK:STDOUT:
  42. // CHECK:STDOUT: define void @"_COp.C.Main:Copy.Core"(ptr sret({}) %return, ptr %self) !dbg !4 {
  43. // CHECK:STDOUT: entry:
  44. // CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %return, ptr align 1 @C.val.loc15_44, i64 0, i1 false), !dbg !7
  45. // CHECK:STDOUT: ret void, !dbg !7
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: define void @_CM.Main() !dbg !8 {
  49. // CHECK:STDOUT: entry:
  50. // CHECK:STDOUT: %n.var = alloca i32, align 4, !dbg !9
  51. // CHECK:STDOUT: %m.var = alloca double, align 8, !dbg !10
  52. // CHECK:STDOUT: %ptr_i32.var = alloca ptr, align 8, !dbg !11
  53. // CHECK:STDOUT: %ptr_f64.var = alloca ptr, align 8, !dbg !12
  54. // CHECK:STDOUT: %c.var = alloca {}, align 8, !dbg !13
  55. // CHECK:STDOUT: %.loc40_9.5.temp = alloca {}, align 8, !dbg !14
  56. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %n.var), !dbg !9
  57. // CHECK:STDOUT: store i32 0, ptr %n.var, align 4, !dbg !9
  58. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %m.var), !dbg !10
  59. // CHECK:STDOUT: store double 1.000000e+00, ptr %m.var, align 8, !dbg !10
  60. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %ptr_i32.var), !dbg !11
  61. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %ptr_f64.var), !dbg !12
  62. // CHECK:STDOUT: %.loc34_5 = load i32, ptr %n.var, align 4, !dbg !15
  63. // CHECK:STDOUT: %F.call.loc34 = call i32 @_CF.Main.de631560529e9861(i32 %.loc34_5, i32 0), !dbg !16
  64. // CHECK:STDOUT: %.loc35_5 = load double, ptr %m.var, align 8, !dbg !17
  65. // CHECK:STDOUT: %F.call.loc35 = call double @_CF.Main.3836ccc537683c19(double %.loc35_5, i32 0), !dbg !18
  66. // CHECK:STDOUT: %.loc36_5 = load ptr, ptr %ptr_i32.var, align 8, !dbg !19
  67. // CHECK:STDOUT: %F.call.loc36 = call ptr @_CF.Main.a194de4a3b8dbd0c(ptr %.loc36_5, i32 0), !dbg !20
  68. // CHECK:STDOUT: %.loc37_5 = load ptr, ptr %ptr_f64.var, align 8, !dbg !21
  69. // CHECK:STDOUT: %F.call.loc37 = call ptr @_CF.Main.a194de4a3b8dbd0c(ptr %.loc37_5, i32 0), !dbg !22
  70. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %c.var), !dbg !13
  71. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc40_9.5.temp), !dbg !14
  72. // CHECK:STDOUT: call void @_CF.Main.936d996ea935415c(ptr %.loc40_9.5.temp, ptr %c.var, i32 0), !dbg !14
  73. // CHECK:STDOUT: ret void, !dbg !23
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
  77. // CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias writeonly captures(none), ptr noalias readonly captures(none), i64, i1 immarg) #0
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
  80. // CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #1
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: define linkonce_odr i32 @_CF.Main.de631560529e9861(i32 %x, i32 %count) !dbg !24 {
  83. // CHECK:STDOUT: entry:
  84. // CHECK:STDOUT: %Int.as.OrderedWith.impl.Greater.call = icmp sgt i32 %count, 0, !dbg !25
  85. // CHECK:STDOUT: br i1 %Int.as.OrderedWith.impl.Greater.call, label %if.then, label %if.else, !dbg !26
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: if.then: ; preds = %entry
  88. // CHECK:STDOUT: ret i32 %x, !dbg !27
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: if.else: ; preds = %entry
  91. // CHECK:STDOUT: %Int.as.AddWith.impl.Op.call = add i32 %count, 1, !dbg !28
  92. // CHECK:STDOUT: %F.call = call i32 @_CF.Main.de631560529e9861(i32 %x, i32 %Int.as.AddWith.impl.Op.call), !dbg !29
  93. // CHECK:STDOUT: ret i32 %F.call, !dbg !30
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: define linkonce_odr double @_CF.Main.3836ccc537683c19(double %x, i32 %count) !dbg !31 {
  97. // CHECK:STDOUT: entry:
  98. // CHECK:STDOUT: %Int.as.OrderedWith.impl.Greater.call = icmp sgt i32 %count, 0, !dbg !32
  99. // CHECK:STDOUT: br i1 %Int.as.OrderedWith.impl.Greater.call, label %if.then, label %if.else, !dbg !33
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: if.then: ; preds = %entry
  102. // CHECK:STDOUT: ret double %x, !dbg !34
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: if.else: ; preds = %entry
  105. // CHECK:STDOUT: %Int.as.AddWith.impl.Op.call = add i32 %count, 1, !dbg !35
  106. // CHECK:STDOUT: %F.call = call double @_CF.Main.3836ccc537683c19(double %x, i32 %Int.as.AddWith.impl.Op.call), !dbg !36
  107. // CHECK:STDOUT: ret double %F.call, !dbg !37
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: define linkonce_odr ptr @_CF.Main.a194de4a3b8dbd0c(ptr %x, i32 %count) !dbg !38 {
  111. // CHECK:STDOUT: entry:
  112. // CHECK:STDOUT: %Int.as.OrderedWith.impl.Greater.call = icmp sgt i32 %count, 0, !dbg !39
  113. // CHECK:STDOUT: br i1 %Int.as.OrderedWith.impl.Greater.call, label %if.then, label %if.else, !dbg !40
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: if.then: ; preds = %entry
  116. // CHECK:STDOUT: ret ptr %x, !dbg !41
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: if.else: ; preds = %entry
  119. // CHECK:STDOUT: %Int.as.AddWith.impl.Op.call = add i32 %count, 1, !dbg !42
  120. // CHECK:STDOUT: %F.call = call ptr @_CF.Main.a194de4a3b8dbd0c(ptr %x, i32 %Int.as.AddWith.impl.Op.call), !dbg !43
  121. // CHECK:STDOUT: ret ptr %F.call, !dbg !44
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: define linkonce_odr void @_CF.Main.936d996ea935415c(ptr sret({}) %return, ptr %x, i32 %count) !dbg !45 {
  125. // CHECK:STDOUT: entry:
  126. // CHECK:STDOUT: %Int.as.OrderedWith.impl.Greater.call = icmp sgt i32 %count, 0, !dbg !46
  127. // CHECK:STDOUT: br i1 %Int.as.OrderedWith.impl.Greater.call, label %if.then, label %if.else, !dbg !47
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: if.then: ; preds = %entry
  130. // CHECK:STDOUT: call void @"_COp.C.Main:Copy.Core"(ptr %return, ptr %x), !dbg !48
  131. // CHECK:STDOUT: ret void, !dbg !49
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: if.else: ; preds = %entry
  134. // CHECK:STDOUT: %Int.as.AddWith.impl.Op.call = add i32 %count, 1, !dbg !50
  135. // CHECK:STDOUT: call void @_CF.Main.936d996ea935415c(ptr %return, ptr %x, i32 %Int.as.AddWith.impl.Op.call), !dbg !51
  136. // CHECK:STDOUT: ret void, !dbg !52
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: ; uselistorder directives
  140. // CHECK:STDOUT: uselistorder ptr @llvm.lifetime.start.p0, { 5, 4, 3, 2, 1, 0 }
  141. // CHECK:STDOUT: uselistorder ptr @_CF.Main.a194de4a3b8dbd0c, { 1, 2, 0 }
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
  144. // CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  147. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  150. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  151. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  152. // CHECK:STDOUT: !3 = !DIFile(filename: "call_recursive_basic.carbon", directory: "")
  153. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Op", linkageName: "_COp.C.Main:Copy.Core", scope: null, file: !3, line: 15, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  154. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  155. // CHECK:STDOUT: !6 = !{}
  156. // CHECK:STDOUT: !7 = !DILocation(line: 15, column: 35, scope: !4)
  157. // CHECK:STDOUT: !8 = distinct !DISubprogram(name: "M", linkageName: "_CM.Main", scope: null, file: !3, line: 28, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  158. // CHECK:STDOUT: !9 = !DILocation(line: 29, column: 3, scope: !8)
  159. // CHECK:STDOUT: !10 = !DILocation(line: 30, column: 3, scope: !8)
  160. // CHECK:STDOUT: !11 = !DILocation(line: 31, column: 3, scope: !8)
  161. // CHECK:STDOUT: !12 = !DILocation(line: 32, column: 3, scope: !8)
  162. // CHECK:STDOUT: !13 = !DILocation(line: 39, column: 3, scope: !8)
  163. // CHECK:STDOUT: !14 = !DILocation(line: 40, column: 3, scope: !8)
  164. // CHECK:STDOUT: !15 = !DILocation(line: 34, column: 5, scope: !8)
  165. // CHECK:STDOUT: !16 = !DILocation(line: 34, column: 3, scope: !8)
  166. // CHECK:STDOUT: !17 = !DILocation(line: 35, column: 5, scope: !8)
  167. // CHECK:STDOUT: !18 = !DILocation(line: 35, column: 3, scope: !8)
  168. // CHECK:STDOUT: !19 = !DILocation(line: 36, column: 5, scope: !8)
  169. // CHECK:STDOUT: !20 = !DILocation(line: 36, column: 3, scope: !8)
  170. // CHECK:STDOUT: !21 = !DILocation(line: 37, column: 5, scope: !8)
  171. // CHECK:STDOUT: !22 = !DILocation(line: 37, column: 3, scope: !8)
  172. // CHECK:STDOUT: !23 = !DILocation(line: 28, column: 1, scope: !8)
  173. // CHECK:STDOUT: !24 = distinct !DISubprogram(name: "F", linkageName: "_CF.Main.de631560529e9861", scope: null, file: !3, line: 21, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  174. // CHECK:STDOUT: !25 = !DILocation(line: 22, column: 7, scope: !24)
  175. // CHECK:STDOUT: !26 = !DILocation(line: 22, column: 6, scope: !24)
  176. // CHECK:STDOUT: !27 = !DILocation(line: 23, column: 5, scope: !24)
  177. // CHECK:STDOUT: !28 = !DILocation(line: 25, column: 15, scope: !24)
  178. // CHECK:STDOUT: !29 = !DILocation(line: 25, column: 10, scope: !24)
  179. // CHECK:STDOUT: !30 = !DILocation(line: 25, column: 3, scope: !24)
  180. // CHECK:STDOUT: !31 = distinct !DISubprogram(name: "F", linkageName: "_CF.Main.3836ccc537683c19", scope: null, file: !3, line: 21, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  181. // CHECK:STDOUT: !32 = !DILocation(line: 22, column: 7, scope: !31)
  182. // CHECK:STDOUT: !33 = !DILocation(line: 22, column: 6, scope: !31)
  183. // CHECK:STDOUT: !34 = !DILocation(line: 23, column: 5, scope: !31)
  184. // CHECK:STDOUT: !35 = !DILocation(line: 25, column: 15, scope: !31)
  185. // CHECK:STDOUT: !36 = !DILocation(line: 25, column: 10, scope: !31)
  186. // CHECK:STDOUT: !37 = !DILocation(line: 25, column: 3, scope: !31)
  187. // CHECK:STDOUT: !38 = distinct !DISubprogram(name: "F", linkageName: "_CF.Main.a194de4a3b8dbd0c", scope: null, file: !3, line: 21, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  188. // CHECK:STDOUT: !39 = !DILocation(line: 22, column: 7, scope: !38)
  189. // CHECK:STDOUT: !40 = !DILocation(line: 22, column: 6, scope: !38)
  190. // CHECK:STDOUT: !41 = !DILocation(line: 23, column: 5, scope: !38)
  191. // CHECK:STDOUT: !42 = !DILocation(line: 25, column: 15, scope: !38)
  192. // CHECK:STDOUT: !43 = !DILocation(line: 25, column: 10, scope: !38)
  193. // CHECK:STDOUT: !44 = !DILocation(line: 25, column: 3, scope: !38)
  194. // CHECK:STDOUT: !45 = distinct !DISubprogram(name: "F", linkageName: "_CF.Main.936d996ea935415c", scope: null, file: !3, line: 21, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  195. // CHECK:STDOUT: !46 = !DILocation(line: 22, column: 7, scope: !45)
  196. // CHECK:STDOUT: !47 = !DILocation(line: 22, column: 6, scope: !45)
  197. // CHECK:STDOUT: !48 = !DILocation(line: 23, column: 12, scope: !45)
  198. // CHECK:STDOUT: !49 = !DILocation(line: 23, column: 5, scope: !45)
  199. // CHECK:STDOUT: !50 = !DILocation(line: 25, column: 15, scope: !45)
  200. // CHECK:STDOUT: !51 = !DILocation(line: 25, column: 10, scope: !45)
  201. // CHECK:STDOUT: !52 = !DILocation(line: 25, column: 3, scope: !45)