Refactor InstBlockStack to use ArrayStack. (#4104)

The use of ArrayStack here is intended to simplify the logic, and also
make better use of the inst heap allocations. Prior changes #4101 and
#4103 removed the less related logic from InstBlockStack, although #4103
is the actual part that blocked using ArrayStack.

BTW, note the PrintForStackDump implementation was incorrect because it
didn't apply size_. This simplification fixes the issue.
This commit is contained in:
Jon Ross-Perkins
2024-07-03 19:19:57 +00:00
committed by GitHub
parent 9581a1867d
commit efa158d496
5 changed files with 88 additions and 62 deletions
+16
View File
@@ -47,6 +47,15 @@ class ArrayStack {
return llvm::ArrayRef(values_).slice(array_offsets_.back());
}
// Returns the array at a specific index.
auto PeekArrayAt(int index) const -> llvm::ArrayRef<ValueT> {
auto ref = llvm::ArrayRef(values_).slice(array_offsets_[index]);
if (index + 1 < static_cast<int>(array_offsets_.size())) {
ref = ref.take_front(array_offsets_[index + 1] - array_offsets_[index]);
}
return ref;
}
// Returns the full set of values on the stack, regardless of whether any
// arrays are pushed.
auto PeekAllValues() const -> llvm::ArrayRef<ValueT> { return values_; }
@@ -58,6 +67,13 @@ class ArrayStack {
values_.push_back(value);
}
// Adds multiple values to the top array on the stack.
auto AppendToTop(llvm::ArrayRef<ValueT> values) -> void {
CARBON_CHECK(!array_offsets_.empty())
<< "Must call PushArray before PushValues.";
values_.append(values.begin(), values.end());
}
// Returns the current number of values in all arrays.
auto all_values_size() const -> size_t { return values_.size(); }