1// Copyright 2015 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_LIVE_RANGE_BUILDER_H_
6#define V8_LIVE_RANGE_BUILDER_H_
7
8#include "src/compiler/register-allocator.h"
9
10namespace v8 {
11namespace internal {
12namespace compiler {
13
14
15// Utility offering shorthand syntax for building up a range by providing its ID
16// and pairs (start, end) specifying intervals. Circumvents current incomplete
17// support for C++ features such as instantiation lists, on OS X and Android.
18class TestRangeBuilder {
19 public:
20  explicit TestRangeBuilder(Zone* zone)
21      : id_(-1), pairs_(), uses_(), zone_(zone) {}
22
23  TestRangeBuilder& Id(int id) {
24    id_ = id;
25    return *this;
26  }
27  TestRangeBuilder& Add(int start, int end) {
28    pairs_.push_back({start, end});
29    return *this;
30  }
31
32  TestRangeBuilder& AddUse(int pos) {
33    uses_.insert(pos);
34    return *this;
35  }
36
37  TopLevelLiveRange* Build(int start, int end) {
38    return Add(start, end).Build();
39  }
40
41  TopLevelLiveRange* Build() {
42    TopLevelLiveRange* range =
43        new (zone_) TopLevelLiveRange(id_, MachineRepresentation::kTagged);
44    // Traverse the provided interval specifications backwards, because that is
45    // what LiveRange expects.
46    for (int i = static_cast<int>(pairs_.size()) - 1; i >= 0; --i) {
47      Interval pair = pairs_[i];
48      LifetimePosition start = LifetimePosition::FromInt(pair.first);
49      LifetimePosition end = LifetimePosition::FromInt(pair.second);
50      CHECK(start < end);
51      range->AddUseInterval(start, end, zone_);
52    }
53    for (int pos : uses_) {
54      UsePosition* use_position =
55          new (zone_) UsePosition(LifetimePosition::FromInt(pos), nullptr,
56                                  nullptr, UsePositionHintType::kNone);
57      range->AddUsePosition(use_position);
58    }
59
60    pairs_.clear();
61    return range;
62  }
63
64 private:
65  typedef std::pair<int, int> Interval;
66  typedef std::vector<Interval> IntervalList;
67  int id_;
68  IntervalList pairs_;
69  std::set<int> uses_;
70  Zone* zone_;
71};
72
73
74}  // namespace compiler
75}  // namespace internal
76}  // namespace v8
77
78#endif  // V8_LIVE_RANGE_BUILDER_H_
79