Line data Source code
1 : // Copyright 2014 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_NODE_AUX_DATA_H_
6 : #define V8_COMPILER_NODE_AUX_DATA_H_
7 :
8 : #include "src/compiler/node.h"
9 : #include "src/zone/zone-containers.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 : namespace compiler {
14 :
15 : // Forward declarations.
16 : class Node;
17 :
18 : template <class T>
19 : T DefaultConstruct() {
20 : return T();
21 : }
22 :
23 : template <class T, T def() = DefaultConstruct<T>>
24 4091523 : class NodeAuxData {
25 : public:
26 : explicit NodeAuxData(Zone* zone) : aux_data_(zone) {}
27 : explicit NodeAuxData(size_t initial_size, Zone* zone)
28 : : aux_data_(initial_size, zone) {}
29 :
30 : // Update entry. Returns true iff entry was changed.
31 182404627 : bool Set(Node* node, T const& data) {
32 182404627 : size_t const id = node->id();
33 182404801 : if (id >= aux_data_.size()) aux_data_.resize(id + 1, def());
34 247699577 : if (aux_data_[id] != data) {
35 77676913 : aux_data_[id] = data;
36 77676913 : return true;
37 : }
38 : return false;
39 : }
40 :
41 : T Get(Node* node) const {
42 233308955 : size_t const id = node->id();
43 470103787 : return (id < aux_data_.size()) ? aux_data_[id] : def();
44 : }
45 :
46 : class const_iterator;
47 : friend class const_iterator;
48 :
49 : const_iterator begin() const;
50 : const_iterator end() const;
51 :
52 : private:
53 : ZoneVector<T> aux_data_;
54 : };
55 :
56 : template <class T, T def()>
57 : class NodeAuxData<T, def>::const_iterator {
58 : public:
59 : using iterator_category = std::forward_iterator_tag;
60 : using difference_type = int;
61 : using value_type = std::pair<size_t, T>;
62 : using pointer = value_type*;
63 : using reference = value_type&;
64 :
65 : const_iterator(const ZoneVector<T>* data, size_t current)
66 : : data_(data), current_(current) {}
67 : const_iterator(const const_iterator& other)
68 : : data_(other.data_), current_(other.current_) {}
69 :
70 : value_type operator*() const {
71 : return std::make_pair(current_, (*data_)[current_]);
72 : }
73 : bool operator==(const const_iterator& other) const {
74 262 : return current_ == other.current_ && data_ == other.data_;
75 : }
76 : bool operator!=(const const_iterator& other) const {
77 262 : return !(*this == other);
78 : }
79 : const_iterator& operator++() {
80 244 : ++current_;
81 : return *this;
82 : }
83 : const_iterator operator++(int);
84 :
85 : private:
86 : const ZoneVector<T>* data_;
87 : size_t current_;
88 : };
89 :
90 : template <class T, T def()>
91 : typename NodeAuxData<T, def>::const_iterator NodeAuxData<T, def>::begin()
92 : const {
93 18 : return typename NodeAuxData<T, def>::const_iterator(&aux_data_, 0);
94 : }
95 :
96 : template <class T, T def()>
97 : typename NodeAuxData<T, def>::const_iterator NodeAuxData<T, def>::end() const {
98 : return typename NodeAuxData<T, def>::const_iterator(&aux_data_,
99 : aux_data_.size());
100 : }
101 :
102 : } // namespace compiler
103 : } // namespace internal
104 : } // namespace v8
105 :
106 : #endif // V8_COMPILER_NODE_AUX_DATA_H_
|