pending_block.h 3.3 KB

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