inst_block_stack.h 4.3 KB

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