Skip to content

Commit 5c7bb7a

Browse files
authored
Clean up ConstantValueStore getters. (#6377)
Move `GetWithDefault` into the `ValueStore` base class, and avoid doing the tag -> index mapping twice. Call `ValueStore::Get` instead of `ConstantValueStore::GetAttached` in `GetUnattachedConstant`. This is equivalent, since we never need a default value here, and should be faster and less surprising.
1 parent 6451ae6 commit 5c7bb7a

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

toolchain/base/value_store.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,20 @@ class ValueStore
221221
return chunks_[chunk_index].Get(pos);
222222
}
223223

224+
// Returns the value for an ID, or a specified default value if a value has
225+
// not yet been added for this ID.
226+
auto GetWithDefault(IdType id, //
227+
ConstRefType default_value [[clang::lifetimebound]]) const
228+
-> ConstRefType {
229+
auto index = tag_.Remove(id.index);
230+
if (index >= size_) {
231+
return default_value;
232+
}
233+
CARBON_DCHECK(index >= 0, "{0}", id);
234+
auto [chunk_index, pos] = RawIndexToChunkIndices(index);
235+
return chunks_[chunk_index].Get(pos);
236+
}
237+
224238
// Reserves space.
225239
auto Reserve(int32_t size) -> void {
226240
if (size <= size_) {

toolchain/sem_ir/constant.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ class ConstantValueStore {
136136
auto GetAttached(InstId inst_id) const -> ConstantId {
137137
CARBON_CHECK(insts_,
138138
"Used ConstantValueStores must have an associated InstStore.");
139-
auto index = insts_->GetRawIndex(inst_id);
140-
return static_cast<size_t>(index) >= values_.size() ? default_
141-
: values_.Get(inst_id);
139+
return values_.GetWithDefault(inst_id, default_);
142140
}
143141

144142
// Sets the constant value of the given instruction, or sets that it is known
@@ -189,7 +187,7 @@ class ConstantValueStore {
189187
// For any other constant ID, returns the ID unchanged.
190188
auto GetUnattachedConstant(ConstantId const_id) const -> ConstantId {
191189
if (const_id.is_symbolic()) {
192-
return GetAttached(GetSymbolicConstant(const_id).inst_id);
190+
return values_.Get(GetSymbolicConstant(const_id).inst_id);
193191
}
194192
return const_id;
195193
}

0 commit comments

Comments
 (0)