|
@@ -5,6 +5,7 @@
|
|
|
#ifndef CARBON_TOOLCHAIN_CHECK_INST_BLOCK_STACK_H_
|
|
#ifndef CARBON_TOOLCHAIN_CHECK_INST_BLOCK_STACK_H_
|
|
|
#define CARBON_TOOLCHAIN_CHECK_INST_BLOCK_STACK_H_
|
|
#define CARBON_TOOLCHAIN_CHECK_INST_BLOCK_STACK_H_
|
|
|
|
|
|
|
|
|
|
+#include "common/array_stack.h"
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
#include "toolchain/sem_ir/file.h"
|
|
#include "toolchain/sem_ir/file.h"
|
|
|
|
|
|
|
@@ -53,50 +54,31 @@ class InstBlockStack {
|
|
|
// Adds the given instruction ID to the block at the top of the stack.
|
|
// Adds the given instruction ID to the block at the top of the stack.
|
|
|
auto AddInstId(SemIR::InstId inst_id) -> void {
|
|
auto AddInstId(SemIR::InstId inst_id) -> void {
|
|
|
CARBON_CHECK(!empty()) << "no current block";
|
|
CARBON_CHECK(!empty()) << "no current block";
|
|
|
- stack_[size_ - 1].content.push_back(inst_id);
|
|
|
|
|
|
|
+ insts_stack_.AppendToTop(inst_id);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Returns whether the current block is statically reachable.
|
|
// Returns whether the current block is statically reachable.
|
|
|
auto is_current_block_reachable() -> bool {
|
|
auto is_current_block_reachable() -> bool {
|
|
|
- return size_ != 0 &&
|
|
|
|
|
- stack_[size_ - 1].id != SemIR::InstBlockId::Unreachable;
|
|
|
|
|
|
|
+ return id_stack_.back() != SemIR::InstBlockId::Unreachable;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Returns a view of the contents of the top instruction block on the stack.
|
|
// Returns a view of the contents of the top instruction block on the stack.
|
|
|
- auto PeekCurrentBlockContents() -> llvm::ArrayRef<SemIR::InstId> {
|
|
|
|
|
|
|
+ auto PeekCurrentBlockContents() const -> llvm::ArrayRef<SemIR::InstId> {
|
|
|
CARBON_CHECK(!empty()) << "no current block";
|
|
CARBON_CHECK(!empty()) << "no current block";
|
|
|
- return stack_[size_ - 1].content;
|
|
|
|
|
|
|
+ return insts_stack_.PeekArray();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Prints the stack for a stack dump.
|
|
// Prints the stack for a stack dump.
|
|
|
auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
|
|
auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
|
|
|
|
|
|
|
|
// Runs verification that the processing cleanly finished.
|
|
// Runs verification that the processing cleanly finished.
|
|
|
- auto VerifyOnFinish() const -> void { CARBON_CHECK(empty()) << size_; }
|
|
|
|
|
|
|
+ auto VerifyOnFinish() const -> void {
|
|
|
|
|
+ CARBON_CHECK(empty()) << id_stack_.size();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- auto empty() const -> bool { return size_ == 0; }
|
|
|
|
|
|
|
+ auto empty() const -> bool { return id_stack_.empty(); }
|
|
|
|
|
|
|
|
private:
|
|
private:
|
|
|
- struct StackEntry {
|
|
|
|
|
- // Preallocate an arbitrary size for the stack entries.
|
|
|
|
|
- // TODO: Perform measurements to pick a good starting size to avoid
|
|
|
|
|
- // reallocation.
|
|
|
|
|
- StackEntry() { content.reserve(32); }
|
|
|
|
|
-
|
|
|
|
|
- auto Reset(SemIR::InstBlockId new_id) {
|
|
|
|
|
- id = new_id;
|
|
|
|
|
- content.clear();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // The block ID, if one has been allocated, Invalid if no block has been
|
|
|
|
|
- // allocated, or Unreachable if this block is known to be unreachable.
|
|
|
|
|
- SemIR::InstBlockId id = SemIR::InstBlockId::Invalid;
|
|
|
|
|
-
|
|
|
|
|
- // The content of the block. Stored as a vector rather than as a SmallVector
|
|
|
|
|
- // to reduce the cost of resizing `stack_` and performing swaps.
|
|
|
|
|
- std::vector<SemIR::InstId> content;
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
// A name for debugging.
|
|
// A name for debugging.
|
|
|
llvm::StringLiteral name_;
|
|
llvm::StringLiteral name_;
|
|
|
|
|
|
|
@@ -106,12 +88,12 @@ class InstBlockStack {
|
|
|
// Whether to print verbose output.
|
|
// Whether to print verbose output.
|
|
|
llvm::raw_ostream* vlog_stream_;
|
|
llvm::raw_ostream* vlog_stream_;
|
|
|
|
|
|
|
|
- // The actual stack.
|
|
|
|
|
- llvm::SmallVector<StackEntry> stack_;
|
|
|
|
|
|
|
+ // The stack of block IDs. Valid if allocated, Invalid if no block has been
|
|
|
|
|
+ // allocated, or Unreachable if this block is known to be unreachable.
|
|
|
|
|
+ llvm::SmallVector<SemIR::InstBlockId> id_stack_;
|
|
|
|
|
|
|
|
- // The size of the stack. Entries after this in `stack_` are kept around so
|
|
|
|
|
- // that we can reuse the allocated buffer for their content.
|
|
|
|
|
- int size_ = 0;
|
|
|
|
|
|
|
+ // The stack of insts in each block.
|
|
|
|
|
+ ArrayStack<SemIR::InstId> insts_stack_;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
} // namespace Carbon::Check
|
|
} // namespace Carbon::Check
|