constant_condition.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/if_expr/constant_condition.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/if_expr/constant_condition.carbon
  10. fn A() -> i32 { return 1; }
  11. fn B() -> i32 { return 2; }
  12. fn F() -> i32 {
  13. return if true then A() else B();
  14. }
  15. fn G() -> i32 {
  16. return if false then A() else B();
  17. }
  18. fn Constant() -> i32 {
  19. var v: if true then i32 else i32* = 1;
  20. var w: if false then i32 else i32* = &v;
  21. return *w;
  22. }
  23. fn PartiallyConstant(t: type) -> i32 {
  24. var v: if true then i32 else t = 1;
  25. var w: if false then t else i32* = &v;
  26. return *w;
  27. }
  28. // CHECK:STDOUT: --- constant_condition.carbon
  29. // CHECK:STDOUT:
  30. // CHECK:STDOUT: constants {
  31. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  32. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  33. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  34. // CHECK:STDOUT: %A.type: type = fn_type @A [template]
  35. // CHECK:STDOUT: %A: %A.type = struct_value () [template]
  36. // CHECK:STDOUT: %.2: i32 = int_literal 1 [template]
  37. // CHECK:STDOUT: %B.type: type = fn_type @B [template]
  38. // CHECK:STDOUT: %B: %B.type = struct_value () [template]
  39. // CHECK:STDOUT: %.3: i32 = int_literal 2 [template]
  40. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  41. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  42. // CHECK:STDOUT: %.4: bool = bool_literal true [template]
  43. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  44. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  45. // CHECK:STDOUT: %.5: bool = bool_literal false [template]
  46. // CHECK:STDOUT: %Constant.type: type = fn_type @Constant [template]
  47. // CHECK:STDOUT: %Constant: %Constant.type = struct_value () [template]
  48. // CHECK:STDOUT: %.6: type = ptr_type i32 [template]
  49. // CHECK:STDOUT: %PartiallyConstant.type: type = fn_type @PartiallyConstant [template]
  50. // CHECK:STDOUT: %PartiallyConstant: %PartiallyConstant.type = struct_value () [template]
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: file {
  54. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  55. // CHECK:STDOUT: .Core = %Core
  56. // CHECK:STDOUT: .A = %A.decl
  57. // CHECK:STDOUT: .B = %B.decl
  58. // CHECK:STDOUT: .F = %F.decl
  59. // CHECK:STDOUT: .G = %G.decl
  60. // CHECK:STDOUT: .Constant = %Constant.decl
  61. // CHECK:STDOUT: .PartiallyConstant = %PartiallyConstant.decl
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  64. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  65. // CHECK:STDOUT: %A.decl: %A.type = fn_decl @A [template = constants.%A] {
  66. // CHECK:STDOUT: %int.make_type_32.loc11: init type = call constants.%Int32() [template = i32]
  67. // CHECK:STDOUT: %.loc11_11.1: type = value_of_initializer %int.make_type_32.loc11 [template = i32]
  68. // CHECK:STDOUT: %.loc11_11.2: type = converted %int.make_type_32.loc11, %.loc11_11.1 [template = i32]
  69. // CHECK:STDOUT: @A.%return: ref i32 = var <return slot>
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  72. // CHECK:STDOUT: %B.decl: %B.type = fn_decl @B [template = constants.%B] {
  73. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  74. // CHECK:STDOUT: %.loc12_11.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  75. // CHECK:STDOUT: %.loc12_11.2: type = converted %int.make_type_32.loc12, %.loc12_11.1 [template = i32]
  76. // CHECK:STDOUT: @B.%return: ref i32 = var <return slot>
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  79. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  80. // CHECK:STDOUT: %int.make_type_32.loc14: init type = call constants.%Int32() [template = i32]
  81. // CHECK:STDOUT: %.loc14_11.1: type = value_of_initializer %int.make_type_32.loc14 [template = i32]
  82. // CHECK:STDOUT: %.loc14_11.2: type = converted %int.make_type_32.loc14, %.loc14_11.1 [template = i32]
  83. // CHECK:STDOUT: @F.%return: ref i32 = var <return slot>
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  86. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  87. // CHECK:STDOUT: %int.make_type_32.loc18: init type = call constants.%Int32() [template = i32]
  88. // CHECK:STDOUT: %.loc18_11.1: type = value_of_initializer %int.make_type_32.loc18 [template = i32]
  89. // CHECK:STDOUT: %.loc18_11.2: type = converted %int.make_type_32.loc18, %.loc18_11.1 [template = i32]
  90. // CHECK:STDOUT: @G.%return: ref i32 = var <return slot>
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: %import_ref.5: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  93. // CHECK:STDOUT: %Constant.decl: %Constant.type = fn_decl @Constant [template = constants.%Constant] {
  94. // CHECK:STDOUT: %int.make_type_32.loc22: init type = call constants.%Int32() [template = i32]
  95. // CHECK:STDOUT: %.loc22_18.1: type = value_of_initializer %int.make_type_32.loc22 [template = i32]
  96. // CHECK:STDOUT: %.loc22_18.2: type = converted %int.make_type_32.loc22, %.loc22_18.1 [template = i32]
  97. // CHECK:STDOUT: @Constant.%return: ref i32 = var <return slot>
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: %import_ref.6: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  100. // CHECK:STDOUT: %import_ref.7: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  101. // CHECK:STDOUT: %import_ref.8: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  102. // CHECK:STDOUT: %import_ref.9: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  103. // CHECK:STDOUT: %import_ref.10: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  104. // CHECK:STDOUT: %PartiallyConstant.decl: %PartiallyConstant.type = fn_decl @PartiallyConstant [template = constants.%PartiallyConstant] {
  105. // CHECK:STDOUT: %t.loc28_22.1: type = param t
  106. // CHECK:STDOUT: @PartiallyConstant.%t: type = bind_name t, %t.loc28_22.1
  107. // CHECK:STDOUT: %int.make_type_32.loc28: init type = call constants.%Int32() [template = i32]
  108. // CHECK:STDOUT: %.loc28_34.1: type = value_of_initializer %int.make_type_32.loc28 [template = i32]
  109. // CHECK:STDOUT: %.loc28_34.2: type = converted %int.make_type_32.loc28, %.loc28_34.1 [template = i32]
  110. // CHECK:STDOUT: @PartiallyConstant.%return: ref i32 = var <return slot>
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT: %import_ref.11: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  113. // CHECK:STDOUT: %import_ref.12: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: fn @A() -> i32 {
  119. // CHECK:STDOUT: !entry:
  120. // CHECK:STDOUT: %.loc11: i32 = int_literal 1 [template = constants.%.2]
  121. // CHECK:STDOUT: return %.loc11
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: fn @B() -> i32 {
  125. // CHECK:STDOUT: !entry:
  126. // CHECK:STDOUT: %.loc12: i32 = int_literal 2 [template = constants.%.3]
  127. // CHECK:STDOUT: return %.loc12
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: fn @F() -> i32 {
  131. // CHECK:STDOUT: !entry:
  132. // CHECK:STDOUT: %.loc15_13: bool = bool_literal true [template = constants.%.4]
  133. // CHECK:STDOUT: if %.loc15_13 br !if.expr.then else br !if.expr.else
  134. // CHECK:STDOUT:
  135. // CHECK:STDOUT: !if.expr.then:
  136. // CHECK:STDOUT: %A.ref: %A.type = name_ref A, file.%A.decl [template = constants.%A]
  137. // CHECK:STDOUT: %A.call: init i32 = call %A.ref()
  138. // CHECK:STDOUT: %.loc15_24.1: i32 = value_of_initializer %A.call
  139. // CHECK:STDOUT: %.loc15_24.2: i32 = converted %A.call, %.loc15_24.1
  140. // CHECK:STDOUT: br !if.expr.result(%.loc15_24.2)
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: !if.expr.else:
  143. // CHECK:STDOUT: %B.ref: %B.type = name_ref B, file.%B.decl [template = constants.%B]
  144. // CHECK:STDOUT: %B.call: init i32 = call %B.ref()
  145. // CHECK:STDOUT: %.loc15_27.1: i32 = value_of_initializer %B.call
  146. // CHECK:STDOUT: %.loc15_27.2: i32 = converted %B.call, %.loc15_27.1
  147. // CHECK:STDOUT: br !if.expr.result(%.loc15_27.2)
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: !if.expr.result:
  150. // CHECK:STDOUT: %.loc15_10: i32 = block_arg !if.expr.result
  151. // CHECK:STDOUT: return %.loc15_10
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: fn @G() -> i32 {
  155. // CHECK:STDOUT: !entry:
  156. // CHECK:STDOUT: %.loc19_13: bool = bool_literal false [template = constants.%.5]
  157. // CHECK:STDOUT: if %.loc19_13 br !if.expr.then else br !if.expr.else
  158. // CHECK:STDOUT:
  159. // CHECK:STDOUT: !if.expr.then:
  160. // CHECK:STDOUT: %A.ref: %A.type = name_ref A, file.%A.decl [template = constants.%A]
  161. // CHECK:STDOUT: %A.call: init i32 = call %A.ref()
  162. // CHECK:STDOUT: %.loc19_25.1: i32 = value_of_initializer %A.call
  163. // CHECK:STDOUT: %.loc19_25.2: i32 = converted %A.call, %.loc19_25.1
  164. // CHECK:STDOUT: br !if.expr.result(%.loc19_25.2)
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: !if.expr.else:
  167. // CHECK:STDOUT: %B.ref: %B.type = name_ref B, file.%B.decl [template = constants.%B]
  168. // CHECK:STDOUT: %B.call: init i32 = call %B.ref()
  169. // CHECK:STDOUT: %.loc19_28.1: i32 = value_of_initializer %B.call
  170. // CHECK:STDOUT: %.loc19_28.2: i32 = converted %B.call, %.loc19_28.1
  171. // CHECK:STDOUT: br !if.expr.result(%.loc19_28.2)
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: !if.expr.result:
  174. // CHECK:STDOUT: %.loc19_10: i32 = block_arg !if.expr.result
  175. // CHECK:STDOUT: return %.loc19_10
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: fn @Constant() -> i32 {
  179. // CHECK:STDOUT: !entry:
  180. // CHECK:STDOUT: %.loc23_13: bool = bool_literal true [template = constants.%.4]
  181. // CHECK:STDOUT: if %.loc23_13 br !if.expr.then.loc23 else br !if.expr.else.loc23
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: !if.expr.then.loc23:
  184. // CHECK:STDOUT: %int.make_type_32.loc23_23: init type = call constants.%Int32() [template = i32]
  185. // CHECK:STDOUT: %.loc23_23.1: type = value_of_initializer %int.make_type_32.loc23_23 [template = i32]
  186. // CHECK:STDOUT: %.loc23_23.2: type = converted %int.make_type_32.loc23_23, %.loc23_23.1 [template = i32]
  187. // CHECK:STDOUT: br !if.expr.result.loc23(%.loc23_23.2)
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: !if.expr.else.loc23:
  190. // CHECK:STDOUT: %int.make_type_32.loc23_32: init type = call constants.%Int32() [template = i32]
  191. // CHECK:STDOUT: %.loc23_35.1: type = value_of_initializer %int.make_type_32.loc23_32 [template = i32]
  192. // CHECK:STDOUT: %.loc23_35.2: type = converted %int.make_type_32.loc23_32, %.loc23_35.1 [template = i32]
  193. // CHECK:STDOUT: %.loc23_35.3: type = ptr_type i32 [template = constants.%.6]
  194. // CHECK:STDOUT: br !if.expr.result.loc23(%.loc23_35.3)
  195. // CHECK:STDOUT:
  196. // CHECK:STDOUT: !if.expr.result.loc23:
  197. // CHECK:STDOUT: %.loc23_10: type = block_arg !if.expr.result.loc23 [template = i32]
  198. // CHECK:STDOUT: %v.var: ref i32 = var v
  199. // CHECK:STDOUT: %v: ref i32 = bind_name v, %v.var
  200. // CHECK:STDOUT: %.loc23_39: i32 = int_literal 1 [template = constants.%.2]
  201. // CHECK:STDOUT: assign %v.var, %.loc23_39
  202. // CHECK:STDOUT: %.loc24_13: bool = bool_literal false [template = constants.%.5]
  203. // CHECK:STDOUT: if %.loc24_13 br !if.expr.then.loc24 else br !if.expr.else.loc24
  204. // CHECK:STDOUT:
  205. // CHECK:STDOUT: !if.expr.then.loc24:
  206. // CHECK:STDOUT: %int.make_type_32.loc24_24: init type = call constants.%Int32() [template = i32]
  207. // CHECK:STDOUT: %.loc24_24.1: type = value_of_initializer %int.make_type_32.loc24_24 [template = i32]
  208. // CHECK:STDOUT: %.loc24_24.2: type = converted %int.make_type_32.loc24_24, %.loc24_24.1 [template = i32]
  209. // CHECK:STDOUT: br !if.expr.result.loc24(%.loc24_24.2)
  210. // CHECK:STDOUT:
  211. // CHECK:STDOUT: !if.expr.else.loc24:
  212. // CHECK:STDOUT: %int.make_type_32.loc24_33: init type = call constants.%Int32() [template = i32]
  213. // CHECK:STDOUT: %.loc24_36.1: type = value_of_initializer %int.make_type_32.loc24_33 [template = i32]
  214. // CHECK:STDOUT: %.loc24_36.2: type = converted %int.make_type_32.loc24_33, %.loc24_36.1 [template = i32]
  215. // CHECK:STDOUT: %.loc24_36.3: type = ptr_type i32 [template = constants.%.6]
  216. // CHECK:STDOUT: br !if.expr.result.loc24(%.loc24_36.3)
  217. // CHECK:STDOUT:
  218. // CHECK:STDOUT: !if.expr.result.loc24:
  219. // CHECK:STDOUT: %.loc24_10: type = block_arg !if.expr.result.loc24 [template = constants.%.6]
  220. // CHECK:STDOUT: %w.var: ref %.6 = var w
  221. // CHECK:STDOUT: %w: ref %.6 = bind_name w, %w.var
  222. // CHECK:STDOUT: %v.ref: ref i32 = name_ref v, %v
  223. // CHECK:STDOUT: %.loc24_40: %.6 = addr_of %v.ref
  224. // CHECK:STDOUT: assign %w.var, %.loc24_40
  225. // CHECK:STDOUT: %w.ref: ref %.6 = name_ref w, %w
  226. // CHECK:STDOUT: %.loc25_11: %.6 = bind_value %w.ref
  227. // CHECK:STDOUT: %.loc25_10.1: ref i32 = deref %.loc25_11
  228. // CHECK:STDOUT: %.loc25_10.2: i32 = bind_value %.loc25_10.1
  229. // CHECK:STDOUT: return %.loc25_10.2
  230. // CHECK:STDOUT: }
  231. // CHECK:STDOUT:
  232. // CHECK:STDOUT: fn @PartiallyConstant(%t: type) -> i32 {
  233. // CHECK:STDOUT: !entry:
  234. // CHECK:STDOUT: %.loc29_13: bool = bool_literal true [template = constants.%.4]
  235. // CHECK:STDOUT: if %.loc29_13 br !if.expr.then.loc29 else br !if.expr.else.loc29
  236. // CHECK:STDOUT:
  237. // CHECK:STDOUT: !if.expr.then.loc29:
  238. // CHECK:STDOUT: %int.make_type_32.loc29: init type = call constants.%Int32() [template = i32]
  239. // CHECK:STDOUT: %.loc29_23.1: type = value_of_initializer %int.make_type_32.loc29 [template = i32]
  240. // CHECK:STDOUT: %.loc29_23.2: type = converted %int.make_type_32.loc29, %.loc29_23.1 [template = i32]
  241. // CHECK:STDOUT: br !if.expr.result.loc29(%.loc29_23.2)
  242. // CHECK:STDOUT:
  243. // CHECK:STDOUT: !if.expr.else.loc29:
  244. // CHECK:STDOUT: %t.ref.loc29: type = name_ref t, %t
  245. // CHECK:STDOUT: br !if.expr.result.loc29(%t.ref.loc29)
  246. // CHECK:STDOUT:
  247. // CHECK:STDOUT: !if.expr.result.loc29:
  248. // CHECK:STDOUT: %.loc29_10: type = block_arg !if.expr.result.loc29 [template = i32]
  249. // CHECK:STDOUT: %v.var: ref i32 = var v
  250. // CHECK:STDOUT: %v: ref i32 = bind_name v, %v.var
  251. // CHECK:STDOUT: %.loc29_36: i32 = int_literal 1 [template = constants.%.2]
  252. // CHECK:STDOUT: assign %v.var, %.loc29_36
  253. // CHECK:STDOUT: %.loc30_13: bool = bool_literal false [template = constants.%.5]
  254. // CHECK:STDOUT: if %.loc30_13 br !if.expr.then.loc30 else br !if.expr.else.loc30
  255. // CHECK:STDOUT:
  256. // CHECK:STDOUT: !if.expr.then.loc30:
  257. // CHECK:STDOUT: %t.ref.loc30: type = name_ref t, %t
  258. // CHECK:STDOUT: br !if.expr.result.loc30(%t.ref.loc30)
  259. // CHECK:STDOUT:
  260. // CHECK:STDOUT: !if.expr.else.loc30:
  261. // CHECK:STDOUT: %int.make_type_32.loc30: init type = call constants.%Int32() [template = i32]
  262. // CHECK:STDOUT: %.loc30_34.1: type = value_of_initializer %int.make_type_32.loc30 [template = i32]
  263. // CHECK:STDOUT: %.loc30_34.2: type = converted %int.make_type_32.loc30, %.loc30_34.1 [template = i32]
  264. // CHECK:STDOUT: %.loc30_34.3: type = ptr_type i32 [template = constants.%.6]
  265. // CHECK:STDOUT: br !if.expr.result.loc30(%.loc30_34.3)
  266. // CHECK:STDOUT:
  267. // CHECK:STDOUT: !if.expr.result.loc30:
  268. // CHECK:STDOUT: %.loc30_10: type = block_arg !if.expr.result.loc30 [template = constants.%.6]
  269. // CHECK:STDOUT: %w.var: ref %.6 = var w
  270. // CHECK:STDOUT: %w: ref %.6 = bind_name w, %w.var
  271. // CHECK:STDOUT: %v.ref: ref i32 = name_ref v, %v
  272. // CHECK:STDOUT: %.loc30_38: %.6 = addr_of %v.ref
  273. // CHECK:STDOUT: assign %w.var, %.loc30_38
  274. // CHECK:STDOUT: %w.ref: ref %.6 = name_ref w, %w
  275. // CHECK:STDOUT: %.loc31_11: %.6 = bind_value %w.ref
  276. // CHECK:STDOUT: %.loc31_10.1: ref i32 = deref %.loc31_11
  277. // CHECK:STDOUT: %.loc31_10.2: i32 = bind_value %.loc31_10.1
  278. // CHECK:STDOUT: return %.loc31_10.2
  279. // CHECK:STDOUT: }
  280. // CHECK:STDOUT: