• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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_BYTECODE_LIVENESS_MAP_H_
6 #define V8_COMPILER_BYTECODE_LIVENESS_MAP_H_
7 
8 #include "src/base/hashmap.h"
9 #include "src/utils/bit-vector.h"
10 #include "src/zone/zone.h"
11 
12 namespace v8 {
13 namespace internal {
14 
15 class Zone;
16 
17 namespace compiler {
18 
19 class BytecodeLivenessState : public ZoneObject {
20  public:
BytecodeLivenessState(int register_count,Zone * zone)21   BytecodeLivenessState(int register_count, Zone* zone)
22       : bit_vector_(register_count + 1, zone) {}
23   BytecodeLivenessState(const BytecodeLivenessState&) = delete;
24   BytecodeLivenessState& operator=(const BytecodeLivenessState&) = delete;
25 
bit_vector()26   const BitVector& bit_vector() const { return bit_vector_; }
27 
bit_vector()28   BitVector& bit_vector() { return bit_vector_; }
29 
RegisterIsLive(int index)30   bool RegisterIsLive(int index) const {
31     DCHECK_GE(index, 0);
32     DCHECK_LT(index, bit_vector_.length() - 1);
33     return bit_vector_.Contains(index);
34   }
35 
AccumulatorIsLive()36   bool AccumulatorIsLive() const {
37     return bit_vector_.Contains(bit_vector_.length() - 1);
38   }
39 
Equals(const BytecodeLivenessState & other)40   bool Equals(const BytecodeLivenessState& other) const {
41     return bit_vector_.Equals(other.bit_vector_);
42   }
43 
MarkRegisterLive(int index)44   void MarkRegisterLive(int index) {
45     DCHECK_GE(index, 0);
46     DCHECK_LT(index, bit_vector_.length() - 1);
47     bit_vector_.Add(index);
48   }
49 
MarkRegisterDead(int index)50   void MarkRegisterDead(int index) {
51     DCHECK_GE(index, 0);
52     DCHECK_LT(index, bit_vector_.length() - 1);
53     bit_vector_.Remove(index);
54   }
55 
MarkAccumulatorLive()56   void MarkAccumulatorLive() { bit_vector_.Add(bit_vector_.length() - 1); }
57 
MarkAccumulatorDead()58   void MarkAccumulatorDead() { bit_vector_.Remove(bit_vector_.length() - 1); }
59 
MarkAllLive()60   void MarkAllLive() { bit_vector_.AddAll(); }
61 
Union(const BytecodeLivenessState & other)62   void Union(const BytecodeLivenessState& other) {
63     bit_vector_.Union(other.bit_vector_);
64   }
65 
UnionIsChanged(const BytecodeLivenessState & other)66   bool UnionIsChanged(const BytecodeLivenessState& other) {
67     return bit_vector_.UnionIsChanged(other.bit_vector_);
68   }
69 
CopyFrom(const BytecodeLivenessState & other)70   void CopyFrom(const BytecodeLivenessState& other) {
71     bit_vector_.CopyFrom(other.bit_vector_);
72   }
73 
74  private:
75   BitVector bit_vector_;
76 };
77 
78 struct BytecodeLiveness {
79   BytecodeLivenessState* in;
80   BytecodeLivenessState* out;
81 
82   BytecodeLiveness(int register_count, Zone* zone);
83 };
84 
85 class V8_EXPORT_PRIVATE BytecodeLivenessMap {
86  public:
87   BytecodeLivenessMap(int size, Zone* zone);
88 
89   BytecodeLiveness& InitializeLiveness(int offset, int register_count,
90                                        Zone* zone);
91 
92   BytecodeLiveness& GetLiveness(int offset);
93   const BytecodeLiveness& GetLiveness(int offset) const;
94 
GetInLiveness(int offset)95   BytecodeLivenessState* GetInLiveness(int offset) {
96     return GetLiveness(offset).in;
97   }
GetInLiveness(int offset)98   const BytecodeLivenessState* GetInLiveness(int offset) const {
99     return GetLiveness(offset).in;
100   }
101 
GetOutLiveness(int offset)102   BytecodeLivenessState* GetOutLiveness(int offset) {
103     return GetLiveness(offset).out;
104   }
GetOutLiveness(int offset)105   const BytecodeLivenessState* GetOutLiveness(int offset) const {
106     return GetLiveness(offset).out;
107   }
108 
109  private:
110   base::TemplateHashMapImpl<int, BytecodeLiveness,
111                             base::KeyEqualityMatcher<int>, ZoneAllocationPolicy>
112       liveness_map_;
113 };
114 
115 }  // namespace compiler
116 }  // namespace internal
117 }  // namespace v8
118 
119 #endif  // V8_COMPILER_BYTECODE_LIVENESS_MAP_H_
120