V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
live-range-separator.cc
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 #include "src/compiler/backend/live-range-separator.h"
6 #include "src/compiler/backend/register-allocator.h"
7 
8 namespace v8 {
9 namespace internal {
10 namespace compiler {
11 
12 #define TRACE(...) \
13  do { \
14  if (FLAG_trace_alloc) PrintF(__VA_ARGS__); \
15  } while (false)
16 
17 namespace {
18 
19 void CreateSplinter(TopLevelLiveRange* range, RegisterAllocationData* data,
20  LifetimePosition first_cut, LifetimePosition last_cut) {
21  DCHECK(!range->IsSplinter());
22  // We can ignore ranges that live solely in deferred blocks.
23  // If a range ends right at the end of a deferred block, it is marked by
24  // the range builder as ending at gap start of the next block - since the
25  // end is a position where the variable isn't live. We need to take that
26  // into consideration.
27  LifetimePosition max_allowed_end = last_cut.NextFullStart();
28 
29  if (first_cut <= range->Start() && max_allowed_end >= range->End()) {
30  return;
31  }
32 
33  LifetimePosition start = Max(first_cut, range->Start());
34  LifetimePosition end = Min(last_cut, range->End());
35 
36  if (start < end) {
37  // Ensure the original range has a spill range associated, before it gets
38  // splintered. Splinters will point to it. This way, when attempting to
39  // reuse spill slots of splinters, during allocation, we avoid clobbering
40  // such slots.
41  if (range->MayRequireSpillRange()) {
42  data->CreateSpillRangeForLiveRange(range);
43  }
44  if (range->splinter() == nullptr) {
45  TopLevelLiveRange* splinter =
46  data->NextLiveRange(range->representation());
47  DCHECK_NULL(data->live_ranges()[splinter->vreg()]);
48  data->live_ranges()[splinter->vreg()] = splinter;
49  range->SetSplinter(splinter);
50  }
51  Zone* zone = data->allocation_zone();
52  TRACE("creating splinter for range %d between %d and %d\n", range->vreg(),
53  start.ToInstructionIndex(), end.ToInstructionIndex());
54  range->Splinter(start, end, zone);
55  }
56 }
57 
58 void SetSlotUse(TopLevelLiveRange* range) {
59  range->set_has_slot_use(false);
60  for (const UsePosition* pos = range->first_pos();
61  !range->has_slot_use() && pos != nullptr; pos = pos->next()) {
62  if (pos->type() == UsePositionType::kRequiresSlot) {
63  range->set_has_slot_use(true);
64  }
65  }
66 }
67 
68 void SplinterLiveRange(TopLevelLiveRange* range, RegisterAllocationData* data) {
69  const InstructionSequence* code = data->code();
70  UseInterval* interval = range->first_interval();
71 
72  LifetimePosition first_cut = LifetimePosition::Invalid();
73  LifetimePosition last_cut = LifetimePosition::Invalid();
74 
75  while (interval != nullptr) {
76  UseInterval* next_interval = interval->next();
77  const InstructionBlock* first_block =
78  code->GetInstructionBlock(interval->FirstGapIndex());
79  const InstructionBlock* last_block =
80  code->GetInstructionBlock(interval->LastGapIndex());
81  int first_block_nr = first_block->rpo_number().ToInt();
82  int last_block_nr = last_block->rpo_number().ToInt();
83  for (int block_id = first_block_nr; block_id <= last_block_nr; ++block_id) {
84  const InstructionBlock* current_block =
85  code->InstructionBlockAt(RpoNumber::FromInt(block_id));
86  if (current_block->IsDeferred()) {
87  if (!first_cut.IsValid()) {
88  first_cut = LifetimePosition::GapFromInstructionIndex(
89  current_block->first_instruction_index());
90  }
91  last_cut = LifetimePosition::GapFromInstructionIndex(
92  current_block->last_instruction_index());
93  } else {
94  if (first_cut.IsValid()) {
95  CreateSplinter(range, data, first_cut, last_cut);
96  first_cut = LifetimePosition::Invalid();
97  last_cut = LifetimePosition::Invalid();
98  }
99  }
100  }
101  interval = next_interval;
102  }
103  // When the range ends in deferred blocks, first_cut will be valid here.
104  // Splinter from there to the last instruction that was in a deferred block.
105  if (first_cut.IsValid()) {
106  CreateSplinter(range, data, first_cut, last_cut);
107  }
108 
109  // Redo has_slot_use
110  if (range->has_slot_use() && range->splinter() != nullptr) {
111  SetSlotUse(range);
112  SetSlotUse(range->splinter());
113  }
114 }
115 
116 } // namespace
117 
118 void LiveRangeSeparator::Splinter() {
119  size_t virt_reg_count = data()->live_ranges().size();
120  for (size_t vreg = 0; vreg < virt_reg_count; ++vreg) {
121  TopLevelLiveRange* range = data()->live_ranges()[vreg];
122  if (range == nullptr || range->IsEmpty() || range->IsSplinter()) {
123  continue;
124  }
125  int first_instr = range->first_interval()->FirstGapIndex();
126  if (!data()->code()->GetInstructionBlock(first_instr)->IsDeferred()) {
127  SplinterLiveRange(range, data());
128  }
129  }
130 }
131 
132 void LiveRangeMerger::MarkRangesSpilledInDeferredBlocks() {
133  const InstructionSequence* code = data()->code();
134  for (TopLevelLiveRange* top : data()->live_ranges()) {
135  if (top == nullptr || top->IsEmpty() || top->splinter() == nullptr ||
136  top->HasSpillOperand() || !top->splinter()->HasSpillRange()) {
137  continue;
138  }
139 
140  LiveRange* child = top;
141  for (; child != nullptr; child = child->next()) {
142  if (child->spilled() ||
143  child->NextSlotPosition(child->Start()) != nullptr) {
144  break;
145  }
146  }
147  if (child == nullptr) {
148  top->TreatAsSpilledInDeferredBlock(data()->allocation_zone(),
149  code->InstructionBlockCount());
150  }
151  }
152 }
153 
154 void LiveRangeMerger::Merge() {
155  MarkRangesSpilledInDeferredBlocks();
156 
157  int live_range_count = static_cast<int>(data()->live_ranges().size());
158  for (int i = 0; i < live_range_count; ++i) {
159  TopLevelLiveRange* range = data()->live_ranges()[i];
160  if (range == nullptr || range->IsEmpty() || !range->IsSplinter()) {
161  continue;
162  }
163  TopLevelLiveRange* splinter_parent = range->splintered_from();
164 
165  int to_remove = range->vreg();
166  splinter_parent->Merge(range, data()->allocation_zone());
167  data()->live_ranges()[to_remove] = nullptr;
168  }
169 }
170 
171 #undef TRACE
172 
173 } // namespace compiler
174 } // namespace internal
175 } // namespace v8
Definition: libplatform.h:13