inst_block_stack.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_INST_BLOCK_STACK_H_
  5. #define CARBON_TOOLCHAIN_CHECK_INST_BLOCK_STACK_H_
  6. #include "llvm/ADT/SmallVector.h"
  7. #include "toolchain/sem_ir/file.h"
  8. namespace Carbon::Check {
  9. // A stack of instruction blocks that are currently being constructed in a
  10. // Context. The contents of the instruction blocks are stored here until the
  11. // instruction block is popped from the stack, at which point they are
  12. // transferred into the SemIR::File for long-term storage.
  13. //
  14. // All pushes and pops will be vlogged.
  15. class InstBlockStack {
  16. public:
  17. explicit InstBlockStack(llvm::StringLiteral name, SemIR::File& sem_ir,
  18. llvm::raw_ostream* vlog_stream)
  19. : name_(name), sem_ir_(&sem_ir), vlog_stream_(vlog_stream) {}
  20. // Pushes an existing instruction block.
  21. auto Push(SemIR::InstBlockId id) -> void;
  22. // Pushes a new instruction block. It will be invalid unless PeekOrAdd is
  23. // called in order to support lazy allocation.
  24. auto Push() -> void { Push(SemIR::InstBlockId::Invalid); }
  25. // Pushes the `GlobalInit` inst block onto the stack, this block is handled
  26. // separately from the rest.
  27. // This method shall be used in conjunction with `PopGlobalInit` method to
  28. // allow emitting initialization instructions to `GlobalInit` block from
  29. // separate parts of the tree, accumulating them all in one block.
  30. auto PushGlobalInit() -> void;
  31. // Pushes a new unreachable code block.
  32. auto PushUnreachable() -> void { Push(SemIR::InstBlockId::Unreachable); }
  33. // Returns the ID of the top instruction block, allocating one if necessary.
  34. // If `depth` is specified, returns the instruction at `depth` levels from the
  35. // top of the stack instead of the top block, where the top block is at depth
  36. // 0.
  37. auto PeekOrAdd(int depth = 0) -> SemIR::InstBlockId;
  38. // Pops the top instruction block. This will always return a valid instruction
  39. // block; SemIR::InstBlockId::Empty is returned if one wasn't allocated.
  40. auto Pop() -> SemIR::InstBlockId;
  41. // Pops the top instruction block, and discards it if it hasn't had an ID
  42. // allocated.
  43. auto PopAndDiscard() -> void;
  44. // Pops the `GlobalInit` inst block from the stack without finalizing it.
  45. // `Pop` should be called at the end of the check phase, while `GlobalInit`
  46. // is pushed, to finalize the block.
  47. auto PopGlobalInit() -> void;
  48. // Adds the given instruction ID to the block at the top of the stack.
  49. auto AddInstId(SemIR::InstId inst_id) -> void {
  50. CARBON_CHECK(!empty()) << "no current block";
  51. stack_[size_ - 1].content.push_back(inst_id);
  52. }
  53. // Adds the given instruction ID to the block at the bottom of the stack.
  54. //
  55. // TODO: We shouldn't need to do this.
  56. auto AddInstIdToFileBlock(SemIR::InstId inst_id) -> void {
  57. CARBON_CHECK(!empty()) << "no current block";
  58. stack_[0].content.push_back(inst_id);
  59. }
  60. // Returns whether the current block is statically reachable.
  61. auto is_current_block_reachable() -> bool {
  62. return size_ != 0 &&
  63. stack_[size_ - 1].id != SemIR::InstBlockId::Unreachable;
  64. }
  65. // Returns a view of the contents of the top instruction block on the stack.
  66. auto PeekCurrentBlockContents() -> llvm::ArrayRef<SemIR::InstId> {
  67. CARBON_CHECK(!empty()) << "no current block";
  68. return stack_[size_ - 1].content;
  69. }
  70. // Prints the stack for a stack dump.
  71. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  72. // Runs verification that the processing cleanly finished.
  73. auto VerifyOnFinish() const -> void { CARBON_CHECK(empty()) << size_; }
  74. auto empty() const -> bool { return size_ == 0; }
  75. private:
  76. struct StackEntry {
  77. // Preallocate an arbitrary size for the stack entries.
  78. // TODO: Perform measurements to pick a good starting size to avoid
  79. // reallocation.
  80. StackEntry() { content.reserve(32); }
  81. auto Reset(SemIR::InstBlockId new_id) {
  82. id = new_id;
  83. content.clear();
  84. }
  85. // The block ID, if one has been allocated, Invalid if no block has been
  86. // allocated, or Unreachable if this block is known to be unreachable.
  87. SemIR::InstBlockId id = SemIR::InstBlockId::Invalid;
  88. // The content of the block. Stored as a vector rather than as a SmallVector
  89. // to reduce the cost of resizing `stack_` and performing swaps.
  90. std::vector<SemIR::InstId> content;
  91. };
  92. // A name for debugging.
  93. llvm::StringLiteral name_;
  94. // The underlying SemIR::File instance. Always non-null.
  95. SemIR::File* sem_ir_;
  96. // Whether to print verbose output.
  97. llvm::raw_ostream* vlog_stream_;
  98. std::vector<SemIR::InstId> init_block_;
  99. // The actual stack.
  100. llvm::SmallVector<StackEntry> stack_;
  101. // The size of the stack. Entries after this in `stack_` are kept around so
  102. // that we can reuse the allocated buffer for their content.
  103. int size_ = 0;
  104. };
  105. } // namespace Carbon::Check
  106. #endif // CARBON_TOOLCHAIN_CHECK_INST_BLOCK_STACK_H_