v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
snapshot-table-opindex.h
Go to the documentation of this file.
1// Copyright 2022 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_COMPILER_TURBOSHAFT_SNAPSHOT_TABLE_OPINDEX_H_
6#define V8_COMPILER_TURBOSHAFT_SNAPSHOT_TABLE_OPINDEX_H_
7
8#include <optional>
9
12
14// A Wrapper around a SnapshotTable, which takes care of mapping OpIndex to Key.
15// It uses a ZoneAbslFlatHashMap to store this mapping, and is thus more
16// appropriate for cases where not many OpIndex have a corresponding key.
17template <class Value, class KeyData = NoKeyData>
18class SparseOpIndexSnapshotTable : public SnapshotTable<Value, KeyData> {
19 public:
22
24 : Base(zone), indices_to_keys_(zone) {}
25
26 using Base::Get;
27 Value Get(OpIndex idx) const {
28 auto it = indices_to_keys_.find(idx);
29 if (it == indices_to_keys_.end()) return Value{};
30 return Base::Get(it->second);
31 }
32
33 Value GetPredecessorValue(OpIndex idx, int predecessor_index) {
34 auto it = indices_to_keys_.find(idx);
35 if (it == indices_to_keys_.end()) return Value{};
36 return Base::GetPredecessorValue(it->second, predecessor_index);
37 }
38
39 using Base::Set;
40 bool Set(OpIndex idx, Value new_value) {
41 Key key = GetOrCreateKey(idx);
42 return Base::Set(key, new_value);
43 }
44
45 void NewKey(OpIndex idx, KeyData data, Value initial_value = Value{}) {
46 DCHECK(!indices_to_keys_[idx].has_value());
47 indices_to_keys_[idx] = Base::NewKey(data, initial_value);
48 }
49 void NewKey(OpIndex idx, Value initial_value = Value{}) {
50 NewKey(idx, KeyData{}, initial_value);
51 }
52
53 bool HasKeyFor(OpIndex idx) const {
54 return indices_to_keys_.find(idx) != indices_to_keys_.end();
55 }
56
57 std::optional<Key> TryGetKeyFor(OpIndex idx) const {
58 auto it = indices_to_keys_.find(idx);
59 if (it != indices_to_keys_.end()) return it->second;
60 return std::nullopt;
61 }
62
63 private:
65 auto it = indices_to_keys_.find(idx);
66 if (it != indices_to_keys_.end()) return it->second;
68 indices_to_keys_.insert({idx, key});
69 return key;
70 }
72};
73
74} // namespace v8::internal::compiler::turboshaft
75
76#endif // V8_COMPILER_TURBOSHAFT_SNAPSHOT_TABLE_OPINDEX_H_
Key NewKey(KeyData data, Value initial_value=Value{})
const Value & GetPredecessorValue(Key key, int predecessor_index)
void NewKey(OpIndex idx, KeyData data, Value initial_value=Value{})
#define DCHECK(condition)
Definition logging.h:482