bytecode-liveness-map.h 3.06 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_BYTECODE_LIVENESS_MAP_H_
#define V8_COMPILER_BYTECODE_LIVENESS_MAP_H_

#include "src/base/hashmap.h"
9
#include "src/utils/bit-vector.h"
10 11 12 13 14 15 16 17 18
#include "src/zone/zone.h"

namespace v8 {
namespace internal {

class Zone;

namespace compiler {

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
class BytecodeLivenessState : public ZoneObject {
 public:
  BytecodeLivenessState(int register_count, Zone* zone)
      : bit_vector_(register_count + 1, zone) {}

  const BitVector& bit_vector() const { return bit_vector_; }

  BitVector& bit_vector() { return bit_vector_; }

  bool RegisterIsLive(int index) const {
    DCHECK_GE(index, 0);
    DCHECK_LT(index, bit_vector_.length() - 1);
    return bit_vector_.Contains(index);
  }

  bool AccumulatorIsLive() const {
    return bit_vector_.Contains(bit_vector_.length() - 1);
  }

  bool Equals(const BytecodeLivenessState& other) const {
    return bit_vector_.Equals(other.bit_vector_);
  }

  void MarkRegisterLive(int index) {
    DCHECK_GE(index, 0);
    DCHECK_LT(index, bit_vector_.length() - 1);
    bit_vector_.Add(index);
  }

  void MarkRegisterDead(int index) {
    DCHECK_GE(index, 0);
    DCHECK_LT(index, bit_vector_.length() - 1);
    bit_vector_.Remove(index);
  }

  void MarkAccumulatorLive() { bit_vector_.Add(bit_vector_.length() - 1); }

  void MarkAccumulatorDead() { bit_vector_.Remove(bit_vector_.length() - 1); }

  void MarkAllLive() { bit_vector_.AddAll(); }
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  void Union(const BytecodeLivenessState& other) {
    bit_vector_.Union(other.bit_vector_);
  }

  bool UnionIsChanged(const BytecodeLivenessState& other) {
    return bit_vector_.UnionIsChanged(other.bit_vector_);
  }

  void CopyFrom(const BytecodeLivenessState& other) {
    bit_vector_.CopyFrom(other.bit_vector_);
  }

 private:
  BitVector bit_vector_;

  DISALLOW_COPY_AND_ASSIGN(BytecodeLivenessState);
};

struct BytecodeLiveness {
  BytecodeLivenessState* in;
  BytecodeLivenessState* out;

  BytecodeLiveness(int register_count, Zone* zone);
83 84 85 86 87 88
};

class V8_EXPORT_PRIVATE BytecodeLivenessMap {
 public:
  BytecodeLivenessMap(int size, Zone* zone);

89 90
  BytecodeLiveness& InitializeLiveness(int offset, int register_count,
                                       Zone* zone);
91

92 93
  BytecodeLiveness& GetLiveness(int offset);
  const BytecodeLiveness& GetLiveness(int offset) const;
94

95 96 97 98
  BytecodeLivenessState* GetInLiveness(int offset) {
    return GetLiveness(offset).in;
  }
  const BytecodeLivenessState* GetInLiveness(int offset) const {
99 100 101
    return GetLiveness(offset).in;
  }

102 103 104 105
  BytecodeLivenessState* GetOutLiveness(int offset) {
    return GetLiveness(offset).out;
  }
  const BytecodeLivenessState* GetOutLiveness(int offset) const {
106 107 108 109
    return GetLiveness(offset).out;
  }

 private:
110 111
  base::TemplateHashMapImpl<int, BytecodeLiveness,
                            base::KeyEqualityMatcher<int>, ZoneAllocationPolicy>
112 113 114 115 116 117 118 119
      liveness_map_;
};

}  // namespace compiler
}  // namespace internal
}  // namespace v8

#endif  // V8_COMPILER_BYTECODE_LIVENESS_MAP_H_