control_flow.cpp 5.3 KB

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