control_flow.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/check/inst.h"
  6. #include "toolchain/sem_ir/typed_insts.h"
  7. namespace Carbon::Check {
  8. template <typename BranchNode, typename... Args>
  9. static auto AddDominatedBlockAndBranchImpl(Context& context,
  10. Parse::NodeId node_id, Args... args)
  11. -> SemIR::InstBlockId {
  12. if (!context.inst_block_stack().is_current_block_reachable()) {
  13. return SemIR::InstBlockId::Unreachable;
  14. }
  15. auto block_id = context.inst_blocks().AddDefaultValue();
  16. AddInst<BranchNode>(context, node_id, {block_id, args...});
  17. return block_id;
  18. }
  19. auto AddDominatedBlockAndBranch(Context& context, Parse::NodeId node_id)
  20. -> SemIR::InstBlockId {
  21. return AddDominatedBlockAndBranchImpl<SemIR::Branch>(context, node_id);
  22. }
  23. auto AddDominatedBlockAndBranchWithArg(Context& context, Parse::NodeId node_id,
  24. SemIR::InstId arg_id)
  25. -> SemIR::InstBlockId {
  26. return AddDominatedBlockAndBranchImpl<SemIR::BranchWithArg>(context, node_id,
  27. arg_id);
  28. }
  29. auto AddDominatedBlockAndBranchIf(Context& context, Parse::NodeId node_id,
  30. SemIR::InstId cond_id) -> SemIR::InstBlockId {
  31. return AddDominatedBlockAndBranchImpl<SemIR::BranchIf>(context, node_id,
  32. cond_id);
  33. }
  34. auto AddConvergenceBlockAndPush(Context& context, Parse::NodeId node_id,
  35. int num_blocks) -> void {
  36. CARBON_CHECK(num_blocks >= 2, "no convergence");
  37. SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
  38. for ([[maybe_unused]] auto _ : llvm::seq(num_blocks)) {
  39. if (context.inst_block_stack().is_current_block_reachable()) {
  40. if (new_block_id == SemIR::InstBlockId::Unreachable) {
  41. new_block_id = context.inst_blocks().AddDefaultValue();
  42. }
  43. CARBON_CHECK(node_id.has_value());
  44. AddInst<SemIR::Branch>(context, node_id, {.target_id = new_block_id});
  45. }
  46. context.inst_block_stack().Pop();
  47. }
  48. context.inst_block_stack().Push(new_block_id);
  49. context.region_stack().AddToRegion(new_block_id, node_id);
  50. }
  51. auto AddConvergenceBlockWithArgAndPush(
  52. Context& context, Parse::NodeId node_id,
  53. std::initializer_list<SemIR::InstId> block_args) -> SemIR::InstId {
  54. CARBON_CHECK(block_args.size() >= 2, "no convergence");
  55. SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
  56. for (auto arg_id : block_args) {
  57. if (context.inst_block_stack().is_current_block_reachable()) {
  58. if (new_block_id == SemIR::InstBlockId::Unreachable) {
  59. new_block_id = context.inst_blocks().AddDefaultValue();
  60. }
  61. AddInst<SemIR::BranchWithArg>(
  62. context, node_id, {.target_id = new_block_id, .arg_id = arg_id});
  63. }
  64. context.inst_block_stack().Pop();
  65. }
  66. context.inst_block_stack().Push(new_block_id);
  67. context.region_stack().AddToRegion(new_block_id, node_id);
  68. // Acquire the result value.
  69. SemIR::TypeId result_type_id =
  70. context.insts().Get(*block_args.begin()).type_id();
  71. return AddInst<SemIR::BlockArg>(
  72. context, node_id, {.type_id = result_type_id, .block_id = new_block_id});
  73. }
  74. auto SetBlockArgResultBeforeConstantUse(Context& context,
  75. SemIR::InstId select_id,
  76. SemIR::InstId cond_id,
  77. SemIR::InstId if_true,
  78. SemIR::InstId if_false) -> void {
  79. CARBON_CHECK(context.insts().Is<SemIR::BlockArg>(select_id));
  80. // Determine the constant result based on the condition value.
  81. SemIR::ConstantId const_id = SemIR::ConstantId::NotConstant;
  82. auto cond_const_id = context.constant_values().Get(cond_id);
  83. if (!cond_const_id.is_concrete()) {
  84. // Symbolic or non-constant condition means a non-constant result.
  85. } else if (auto literal = context.insts().TryGetAs<SemIR::BoolLiteral>(
  86. context.constant_values().GetInstId(cond_const_id))) {
  87. const_id = context.constant_values().Get(
  88. literal.value().value.ToBool() ? if_true : if_false);
  89. } else {
  90. CARBON_CHECK(cond_const_id == SemIR::ErrorInst::SingletonConstantId,
  91. "Unexpected constant branch condition.");
  92. const_id = SemIR::ErrorInst::SingletonConstantId;
  93. }
  94. if (const_id.is_constant()) {
  95. CARBON_VLOG_TO(context.vlog_stream(), "Constant: {0} -> {1}\n",
  96. context.insts().Get(select_id),
  97. context.constant_values().GetInstId(const_id));
  98. context.constant_values().Set(select_id, const_id);
  99. }
  100. }
  101. auto IsCurrentPositionReachable(Context& context) -> bool {
  102. if (!context.inst_block_stack().is_current_block_reachable()) {
  103. return false;
  104. }
  105. // Our current position is at the end of a reachable block. That position is
  106. // reachable unless the previous instruction is a terminator instruction.
  107. auto block_contents = context.inst_block_stack().PeekCurrentBlockContents();
  108. if (block_contents.empty()) {
  109. return true;
  110. }
  111. const auto& last_inst = context.insts().Get(block_contents.back());
  112. return last_inst.kind().terminator_kind() !=
  113. SemIR::TerminatorKind::Terminator;
  114. }
  115. } // namespace Carbon::Check