pending_block.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_PENDING_BLOCK_H_
  5. #define CARBON_TOOLCHAIN_CHECK_PENDING_BLOCK_H_
  6. #include "llvm/ADT/SmallVector.h"
  7. #include "toolchain/check/context.h"
  8. #include "toolchain/sem_ir/inst.h"
  9. namespace Carbon::Check {
  10. // A block of code that contains pending instructions that might be needed but
  11. // that haven't been inserted yet.
  12. class PendingBlock {
  13. public:
  14. explicit PendingBlock(Context& context) : context_(context) {}
  15. PendingBlock(const PendingBlock&) = delete;
  16. auto operator=(const PendingBlock&) -> PendingBlock& = delete;
  17. // A scope in which we will tentatively add instructions to a pending block.
  18. // If we leave the scope without inserting or merging the block, instructions
  19. // added after this point will be removed again.
  20. class DiscardUnusedInstsScope {
  21. public:
  22. // If `block` is not null, enters the scope. If `block` is null, this object
  23. // has no effect.
  24. explicit DiscardUnusedInstsScope(PendingBlock* block)
  25. : block_(block), size_(block ? block->insts_.size() : 0) {}
  26. ~DiscardUnusedInstsScope() {
  27. if (block_ && block_->insts_.size() > size_) {
  28. block_->insts_.truncate(size_);
  29. }
  30. }
  31. private:
  32. PendingBlock* block_;
  33. size_t size_;
  34. };
  35. template <typename InstT, typename LocT>
  36. auto AddInst(LocT loc_id, InstT inst) -> SemIR::InstId {
  37. auto inst_id = context_.AddInstInNoBlock(loc_id, inst);
  38. insts_.push_back(inst_id);
  39. return inst_id;
  40. }
  41. // Insert the pending block of code at the current position.
  42. auto InsertHere() -> void {
  43. for (auto id : insts_) {
  44. context_.inst_block_stack().AddInstId(id);
  45. }
  46. insts_.clear();
  47. }
  48. // Replace the instruction at target_id with the instructions in this block.
  49. // The new value for target_id should be value_id.
  50. auto MergeReplacing(SemIR::InstId target_id, SemIR::InstId value_id) -> void {
  51. auto value = context_.insts().GetWithLocId(value_id);
  52. // There are three cases here:
  53. if (insts_.empty()) {
  54. // 1) The block is empty. Replace `target_id` with an empty splice
  55. // pointing at `value_id`.
  56. context_.ReplaceLocIdAndInstBeforeConstantUse(
  57. target_id,
  58. SemIR::LocIdAndInst(
  59. value.loc_id,
  60. SemIR::SpliceBlock{.type_id = value.inst.type_id(),
  61. .block_id = SemIR::InstBlockId::Empty,
  62. .result_id = value_id}));
  63. } else if (insts_.size() == 1 && insts_[0] == value_id) {
  64. // 2) The block is {value_id}. Replace `target_id` with the instruction
  65. // referred to by `value_id`. This is intended to be the common case.
  66. context_.ReplaceLocIdAndInstBeforeConstantUse(target_id, value);
  67. } else {
  68. // 3) Anything else: splice it into the IR, replacing `target_id`.
  69. context_.ReplaceLocIdAndInstBeforeConstantUse(
  70. target_id,
  71. SemIR::LocIdAndInst(
  72. value.loc_id,
  73. SemIR::SpliceBlock{.type_id = value.inst.type_id(),
  74. .block_id = context_.inst_blocks().Add(insts_),
  75. .result_id = value_id}));
  76. }
  77. // Prepare to stash more pending instructions.
  78. insts_.clear();
  79. }
  80. private:
  81. Context& context_;
  82. llvm::SmallVector<SemIR::InstId> insts_;
  83. };
  84. } // namespace Carbon::Check
  85. #endif // CARBON_TOOLCHAIN_CHECK_PENDING_BLOCK_H_