control_flow.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include "toolchain/check/control_flow.h"
  5. #include "toolchain/base/kind_switch.h"
  6. #include "toolchain/check/call.h"
  7. #include "toolchain/check/inst.h"
  8. #include "toolchain/check/member_access.h"
  9. #include "toolchain/check/name_lookup.h"
  10. #include "toolchain/sem_ir/ids.h"
  11. #include "toolchain/sem_ir/typed_insts.h"
  12. namespace Carbon::Check {
  13. template <typename BranchNode, typename... Args>
  14. static auto AddDominatedBlockAndBranchImpl(Context& context,
  15. Parse::NodeId node_id, Args... args)
  16. -> SemIR::InstBlockId {
  17. if (!context.inst_block_stack().is_current_block_reachable()) {
  18. return SemIR::InstBlockId::Unreachable;
  19. }
  20. auto block_id = context.inst_blocks().AddPlaceholder();
  21. AddInst<BranchNode>(context, node_id, {block_id, args...});
  22. return block_id;
  23. }
  24. auto AddDominatedBlockAndBranch(Context& context, Parse::NodeId node_id)
  25. -> SemIR::InstBlockId {
  26. return AddDominatedBlockAndBranchImpl<SemIR::Branch>(context, node_id);
  27. }
  28. auto AddDominatedBlockAndBranchWithArg(Context& context, Parse::NodeId node_id,
  29. SemIR::InstId arg_id)
  30. -> SemIR::InstBlockId {
  31. return AddDominatedBlockAndBranchImpl<SemIR::BranchWithArg>(context, node_id,
  32. arg_id);
  33. }
  34. auto AddDominatedBlockAndBranchIf(Context& context, Parse::NodeId node_id,
  35. SemIR::InstId cond_id) -> SemIR::InstBlockId {
  36. return AddDominatedBlockAndBranchImpl<SemIR::BranchIf>(context, node_id,
  37. cond_id);
  38. }
  39. auto AddConvergenceBlockAndPush(Context& context, Parse::NodeId node_id,
  40. int num_blocks) -> void {
  41. CARBON_CHECK(num_blocks >= 2, "no convergence");
  42. SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
  43. for ([[maybe_unused]] auto _ : llvm::seq(num_blocks)) {
  44. if (context.inst_block_stack().is_current_block_reachable()) {
  45. if (new_block_id == SemIR::InstBlockId::Unreachable) {
  46. new_block_id = context.inst_blocks().AddPlaceholder();
  47. }
  48. CARBON_CHECK(node_id.has_value());
  49. AddInst<SemIR::Branch>(context, node_id, {.target_id = new_block_id});
  50. }
  51. context.inst_block_stack().Pop();
  52. }
  53. context.inst_block_stack().Push(new_block_id);
  54. context.region_stack().AddToRegion(new_block_id, node_id);
  55. }
  56. auto AddConvergenceBlockWithArgAndPush(
  57. Context& context, Parse::NodeId node_id,
  58. std::initializer_list<SemIR::InstId> block_args) -> SemIR::InstId {
  59. CARBON_CHECK(block_args.size() >= 2, "no convergence");
  60. SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
  61. for (auto arg_id : block_args) {
  62. if (context.inst_block_stack().is_current_block_reachable()) {
  63. if (new_block_id == SemIR::InstBlockId::Unreachable) {
  64. new_block_id = context.inst_blocks().AddPlaceholder();
  65. }
  66. AddInst<SemIR::BranchWithArg>(
  67. context, node_id, {.target_id = new_block_id, .arg_id = arg_id});
  68. }
  69. context.inst_block_stack().Pop();
  70. }
  71. context.inst_block_stack().Push(new_block_id);
  72. context.region_stack().AddToRegion(new_block_id, node_id);
  73. // Acquire the result value.
  74. SemIR::TypeId result_type_id =
  75. context.insts().Get(*block_args.begin()).type_id();
  76. return AddInst<SemIR::BlockArg>(
  77. context, node_id, {.type_id = result_type_id, .block_id = new_block_id});
  78. }
  79. auto SetBlockArgResultBeforeConstantUse(Context& context,
  80. SemIR::InstId select_id,
  81. SemIR::InstId cond_id,
  82. SemIR::InstId if_true,
  83. SemIR::InstId if_false) -> void {
  84. CARBON_CHECK(context.insts().Is<SemIR::BlockArg>(select_id));
  85. // Determine the constant result based on the condition value.
  86. SemIR::ConstantId const_id = SemIR::ConstantId::NotConstant;
  87. auto cond_const_id = context.constant_values().Get(cond_id);
  88. if (!cond_const_id.is_concrete()) {
  89. // Symbolic or non-constant condition means a non-constant result.
  90. } else if (auto literal = context.insts().TryGetAs<SemIR::BoolLiteral>(
  91. context.constant_values().GetInstId(cond_const_id))) {
  92. const_id = context.constant_values().Get(
  93. literal.value().value.ToBool() ? if_true : if_false);
  94. } else {
  95. CARBON_CHECK(cond_const_id == SemIR::ErrorInst::SingletonConstantId,
  96. "Unexpected constant branch condition.");
  97. const_id = SemIR::ErrorInst::SingletonConstantId;
  98. }
  99. if (const_id.is_constant()) {
  100. CARBON_VLOG_TO(context.vlog_stream(), "Constant: {0} -> {1}\n",
  101. context.insts().Get(select_id),
  102. context.constant_values().GetInstId(const_id));
  103. context.constant_values().Set(select_id, const_id);
  104. }
  105. }
  106. auto IsCurrentPositionReachable(Context& context) -> bool {
  107. if (!context.inst_block_stack().is_current_block_reachable()) {
  108. return false;
  109. }
  110. // Our current position is at the end of a reachable block. That position is
  111. // reachable unless the previous instruction is a terminator instruction.
  112. auto block_contents = context.inst_block_stack().PeekCurrentBlockContents();
  113. if (block_contents.empty()) {
  114. return true;
  115. }
  116. const auto& last_inst = context.insts().Get(block_contents.back());
  117. return last_inst.kind().terminator_kind() !=
  118. SemIR::TerminatorKind::Terminator;
  119. }
  120. auto MaybeAddCleanupForInst(Context& context, SemIR::TypeId type_id,
  121. SemIR::InstId inst_id) -> void {
  122. if (!context.scope_stack().PeekIsLexicalScope()) {
  123. // Cleanup can only occur in lexical scopes.
  124. return;
  125. }
  126. // TODO: Add destruction of members of ArrayType, StructType, and
  127. // TupleType. Includes refactoring MaybeAddCleanupForInst to remain
  128. // non-recursive.
  129. auto type_inst = context.types().GetAsInst(type_id);
  130. CARBON_KIND_SWITCH(type_inst) {
  131. case SemIR::ClassType::Kind: {
  132. // TODO: Figure out what destruction of classes with destroyable members
  133. // should look like (maybe an implicit `fn destroy` added by
  134. // `handle_class.cpp`?).
  135. auto destroy_id =
  136. PerformMemberAccess(context, SemIR::LocId::None, inst_id,
  137. SemIR::NameId::Destroy, /*required=*/false);
  138. if (destroy_id.has_value()) {
  139. context.scope_stack().destroy_id_stack().AppendToTop(destroy_id);
  140. }
  141. break;
  142. }
  143. default:
  144. // Not interesting storage for destruction.
  145. return;
  146. }
  147. }
  148. // Common support for cleanup blocks.
  149. static auto AddCleanupBlock(Context& context) -> void {
  150. auto destroy_ids = context.scope_stack().destroy_id_stack().PeekArray();
  151. // If there's nothing to destroy, add the final instruction to the current
  152. // block.
  153. if (destroy_ids.empty()) {
  154. return;
  155. }
  156. for (auto destroy_id : llvm::reverse(destroy_ids)) {
  157. PerformCall(context, SemIR::LocId::None, destroy_id, {});
  158. }
  159. }
  160. auto AddReturnCleanupBlock(
  161. Context& context,
  162. typename decltype(SemIR::Return::Kind)::TypedNodeId node_id) -> void {
  163. AddCleanupBlock(context);
  164. AddInst(context, node_id, SemIR::Return{});
  165. }
  166. } // namespace Carbon::Check