control_flow.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #ifndef CARBON_TOOLCHAIN_CHECK_CONTROL_FLOW_H_
  5. #define CARBON_TOOLCHAIN_CHECK_CONTROL_FLOW_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::Check {
  9. // Adds a `Branch` instruction branching to a new instruction block, and
  10. // returns the ID of the new block. All paths to the branch target must go
  11. // through the current block, though not necessarily through this branch.
  12. auto AddDominatedBlockAndBranch(Context& context, Parse::NodeId node_id)
  13. -> SemIR::InstBlockId;
  14. // Adds a `Branch` instruction branching to a new instruction block with a
  15. // value, and returns the ID of the new block. All paths to the branch target
  16. // must go through the current block.
  17. auto AddDominatedBlockAndBranchWithArg(Context& context, Parse::NodeId node_id,
  18. SemIR::InstId arg_id)
  19. -> SemIR::InstBlockId;
  20. // Adds a `BranchIf` instruction branching to a new instruction block, and
  21. // returns the ID of the new block. All paths to the branch target must go
  22. // through the current block.
  23. auto AddDominatedBlockAndBranchIf(Context& context, Parse::NodeId node_id,
  24. SemIR::InstId cond_id) -> SemIR::InstBlockId;
  25. // Handles recovergence of control flow. Adds branches from the top
  26. // `num_blocks` on the instruction block stack to a new block, pops the
  27. // existing blocks, pushes the new block onto the instruction block stack,
  28. // and adds it to the most recently pushed region.
  29. auto AddConvergenceBlockAndPush(Context& context, Parse::NodeId node_id,
  30. int num_blocks) -> void;
  31. // Handles recovergence of control flow with a result value. Adds branches
  32. // from the top few blocks on the instruction block stack to a new block, pops
  33. // the existing blocks, pushes the new block onto the instruction block
  34. // stack, and adds it to the most recently pushed region. The number of blocks
  35. // popped is the size of `block_args`, and the corresponding result values are
  36. // the elements of `block_args`. Returns an instruction referring to the
  37. // result value.
  38. auto AddConvergenceBlockWithArgAndPush(
  39. Context& context, Parse::NodeId node_id,
  40. std::initializer_list<SemIR::InstId> block_args) -> SemIR::InstId;
  41. // Sets the constant value of a block argument created as the result of a
  42. // branch. `select_id` should be a `BlockArg` that selects between two
  43. // values. `cond_id` is the condition, `if_false` is the value to use if the
  44. // condition is false, and `if_true` is the value to use if the condition is
  45. // true. We don't track enough information in the `BlockArg` inst for
  46. // `TryEvalInst` to do this itself.
  47. auto SetBlockArgResultBeforeConstantUse(Context& context,
  48. SemIR::InstId select_id,
  49. SemIR::InstId cond_id,
  50. SemIR::InstId if_true,
  51. SemIR::InstId if_false) -> void;
  52. // Returns whether the current position in the current block is reachable.
  53. auto IsCurrentPositionReachable(Context& context) -> bool;
  54. } // namespace Carbon::Check
  55. #endif // CARBON_TOOLCHAIN_CHECK_CONTROL_FLOW_H_