pending_block.h 3.1 KB

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