control_flow.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/name_lookup.h"
  10. #include "toolchain/check/operator.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::InstId inst_id) -> void {
  122. if (!context.scope_stack().IsInFunctionScope()) {
  123. // Cleanup can only occur in function scopes.
  124. return;
  125. }
  126. context.scope_stack().destroy_id_stack().AppendToTop(inst_id);
  127. }
  128. // Common support for cleanup blocks.
  129. static auto AddCleanupBlock(Context& context) -> void {
  130. auto destroy_ids = context.scope_stack().destroy_id_stack().PeekArray();
  131. // If there's nothing to destroy, add the final instruction to the current
  132. // block.
  133. if (destroy_ids.empty()) {
  134. return;
  135. }
  136. for (auto destroy_id : llvm::reverse(destroy_ids)) {
  137. // TODO: This does the `Destroy` lookup and call at every cleanup block.
  138. // Control flow can lead to the same variable being destroyed by multiple
  139. // cleanup blocks, so we'll want to avoid this in the future.
  140. BuildUnaryOperator(context,
  141. context.insts().GetLocIdForDesugaring(destroy_id),
  142. {.interface_name = CoreIdentifier::Destroy}, destroy_id);
  143. }
  144. }
  145. auto AddReturnCleanupBlock(Context& context,
  146. SemIR::LocIdAndInst loc_id_and_inst) -> void {
  147. AddCleanupBlock(context);
  148. AddInst(context, loc_id_and_inst);
  149. }
  150. } // namespace Carbon::Check