inst_block_stack.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 a new unreachable code block.
  26. auto PushUnreachable() -> void { Push(SemIR::InstBlockId::Unreachable); }
  27. // Returns the ID of the top instruction block, allocating one if necessary.
  28. // If `depth` is specified, returns the instruction at `depth` levels from the
  29. // top of the stack instead of the top block, where the top block is at depth
  30. // 0.
  31. auto PeekOrAdd(int depth = 0) -> SemIR::InstBlockId;
  32. // Pops the top instruction block. This will always return a valid instruction
  33. // block; SemIR::InstBlockId::Empty is returned if one wasn't allocated.
  34. auto Pop() -> SemIR::InstBlockId;
  35. // Pops the top instruction block, and discards it if it hasn't had an ID
  36. // allocated.
  37. auto PopAndDiscard() -> void;
  38. // Adds the given instruction ID to the block at the top of the stack.
  39. auto AddInstId(SemIR::InstId inst_id) -> void {
  40. CARBON_CHECK(!empty()) << "no current block";
  41. stack_[size_ - 1].content.push_back(inst_id);
  42. }
  43. // Returns whether the current block is statically reachable.
  44. auto is_current_block_reachable() -> bool {
  45. return size_ != 0 &&
  46. stack_[size_ - 1].id != SemIR::InstBlockId::Unreachable;
  47. }
  48. // Returns a view of the contents of the top instruction block on the stack.
  49. auto PeekCurrentBlockContents() -> llvm::ArrayRef<SemIR::InstId> {
  50. CARBON_CHECK(!empty()) << "no current block";
  51. return stack_[size_ - 1].content;
  52. }
  53. // Prints the stack for a stack dump.
  54. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  55. auto empty() const -> bool { return size() == 0; }
  56. auto size() const -> int { return size_; }
  57. private:
  58. struct StackEntry {
  59. // Preallocate an arbitrary size for the stack entries.
  60. // TODO: Perform measurements to pick a good starting size to avoid
  61. // reallocation.
  62. StackEntry() { content.reserve(32); }
  63. auto Reset(SemIR::InstBlockId new_id) {
  64. id = new_id;
  65. content.clear();
  66. }
  67. // The block ID, if one has been allocated, Invalid if no block has been
  68. // allocated, or Unreachable if this block is known to be unreachable.
  69. SemIR::InstBlockId id = SemIR::InstBlockId::Invalid;
  70. // The content of the block. Stored as a vector rather than as a SmallVector
  71. // to reduce the cost of resizing `stack_` and performing swaps.
  72. std::vector<SemIR::InstId> content;
  73. };
  74. // A name for debugging.
  75. llvm::StringLiteral name_;
  76. // The underlying SemIR::File instance. Always non-null.
  77. SemIR::File* sem_ir_;
  78. // Whether to print verbose output.
  79. llvm::raw_ostream* vlog_stream_;
  80. // The actual stack.
  81. llvm::SmallVector<StackEntry> stack_;
  82. // The size of the stack. Entries after this in `stack_` are kept around so
  83. // that we can reuse the allocated buffer for their content.
  84. int size_ = 0;
  85. };
  86. } // namespace Carbon::Check
  87. #endif // CARBON_TOOLCHAIN_CHECK_INST_BLOCK_STACK_H_