control_flow.cpp 7.2 KB

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