Switch-related cleanup
[hiphop-php.git] / hphp / runtime / vm / jit / state-vector.h
blob2890bf860e4df05967fea0b7d8c05b949ea49d7c
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_JIT_STATE_VECTOR_H_
18 #define incl_HPHP_JIT_STATE_VECTOR_H_
20 #include <type_traits>
22 #include "hphp/runtime/vm/jit/containers.h"
23 #include "hphp/runtime/vm/jit/ir-unit.h"
25 namespace HPHP { namespace jit {
27 //////////////////////////////////////////////////////////////////////
30 * Utility to keep a vector of state about each key, indexed by
31 * key->id(), where key can be an IRInstruction, Block, or SSATmp.
33 * Takes an `init' element, which everything is defaulted to.
35 template<class Key, class Info>
36 struct StateVector {
37 using InfoVector = jit::vector<Info>;
38 using iterator = typename InfoVector::iterator;
39 using const_iterator = typename InfoVector::const_iterator;
40 using reference = typename InfoVector::reference;
41 using const_reference = typename InfoVector::const_reference;
43 static_assert(
44 std::is_same<Key,Block>::value ||
45 std::is_same<Key,IRInstruction>::value ||
46 std::is_same<Key,SSATmp>::value,
47 "StateVector can only be used with Block, IRInstruction, or SSATmp"
50 StateVector(const IRUnit& unit, Info init)
51 : m_unit(&unit)
52 , m_init(std::move(init))
53 , m_info(unit.numIds(nullKey), m_init)
56 StateVector(const StateVector&) = default;
57 StateVector(StateVector&&) = default;
58 StateVector& operator=(const StateVector&) = delete;
59 StateVector& operator=(StateVector&&) = default;
61 reference operator[](uint32_t id) {
62 if (id >= m_info.size()) grow();
63 return m_info[id];
66 const_reference operator[](uint32_t id) const {
67 assertx(id < m_unit->numIds(nullKey));
68 return id < m_info.size() ? m_info[id] : m_init;
71 reference operator[](const Key& k) { return (*this)[k.id()]; }
72 reference operator[](const Key* k) { return (*this)[k->id()]; }
74 const_reference operator[](const Key& k) const { return (*this)[k.id()]; }
75 const_reference operator[](const Key* k) const { return (*this)[k->id()]; }
77 iterator begin() { return m_info.begin(); }
78 iterator end() { return m_info.end(); }
79 const_iterator begin() const { return m_info.begin(); }
80 const_iterator end() const { return m_info.end(); }
81 const_iterator cbegin() const { return m_info.cbegin(); }
82 const_iterator cend() const { return m_info.cend(); }
84 private:
85 void grow() {
86 m_info.resize(m_unit->numIds(nullKey), m_init);
89 private:
90 static constexpr Key* nullKey { nullptr };
91 const IRUnit* m_unit;
92 Info m_init;
93 InfoVector m_info;
96 //////////////////////////////////////////////////////////////////////
100 #endif