summaryrefslogtreecommitdiff
path: root/deps/v8/test/unittests/compiler/live-range-builder.h
blob: 4a5621fab7733578b89aecc5cb1e47c5deec8bec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2015 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_LIVE_RANGE_BUILDER_H_
#define V8_LIVE_RANGE_BUILDER_H_

#include "src/compiler/register-allocator.h"

namespace v8 {
namespace internal {
namespace compiler {


// Utility offering shorthand syntax for building up a range by providing its ID
// and pairs (start, end) specifying intervals. Circumvents current incomplete
// support for C++ features such as instantiation lists, on OS X and Android.
class TestRangeBuilder {
 public:
  explicit TestRangeBuilder(Zone* zone)
      : id_(-1), pairs_(), uses_(), zone_(zone) {}

  TestRangeBuilder& Id(int id) {
    id_ = id;
    return *this;
  }
  TestRangeBuilder& Add(int start, int end) {
    pairs_.push_back({start, end});
    return *this;
  }

  TestRangeBuilder& AddUse(int pos) {
    uses_.insert(pos);
    return *this;
  }

  TopLevelLiveRange* Build(int start, int end) {
    return Add(start, end).Build();
  }

  TopLevelLiveRange* Build() {
    TopLevelLiveRange* range =
        new (zone_) TopLevelLiveRange(id_, MachineRepresentation::kTagged);
    // Traverse the provided interval specifications backwards, because that is
    // what LiveRange expects.
    for (int i = static_cast<int>(pairs_.size()) - 1; i >= 0; --i) {
      Interval pair = pairs_[i];
      LifetimePosition start = LifetimePosition::FromInt(pair.first);
      LifetimePosition end = LifetimePosition::FromInt(pair.second);
      CHECK(start < end);
      range->AddUseInterval(start, end, zone_);
    }
    for (int pos : uses_) {
      UsePosition* use_position =
          new (zone_) UsePosition(LifetimePosition::FromInt(pos), nullptr,
                                  nullptr, UsePositionHintType::kNone);
      range->AddUsePosition(use_position);
    }

    pairs_.clear();
    return range;
  }

 private:
  typedef std::pair<int, int> Interval;
  typedef std::vector<Interval> IntervalList;
  int id_;
  IntervalList pairs_;
  std::set<int> uses_;
  Zone* zone_;
};


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

#endif  // V8_LIVE_RANGE_BUILDER_H_