live-range-separator.cc 6.14 KB
Newer Older
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
// 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.

#include "src/compiler/live-range-separator.h"
#include "src/compiler/register-allocator.h"

namespace v8 {
namespace internal {
namespace compiler {


#define TRACE(...)                             \
  do {                                         \
    if (FLAG_trace_alloc) PrintF(__VA_ARGS__); \
  } while (false)


namespace {

// Starting from a deferred block, find the last consecutive deferred block.
RpoNumber GetLastDeferredBlock(const InstructionBlock *block,
                               const InstructionSequence *code) {
  DCHECK(block->IsDeferred());
  RpoNumber first = block->rpo_number();

  RpoNumber last = first;
  for (int i = first.ToInt(); i < code->InstructionBlockCount(); ++i) {
    RpoNumber at_i = RpoNumber::FromInt(i);
    const InstructionBlock *block_at_i = code->InstructionBlockAt(at_i);
    if (!block_at_i->IsDeferred()) break;
    last = at_i;
  }

  return last;
}


// Delimits consecutive deferred block sequences.
void AssociateDeferredBlockSequences(InstructionSequence *code) {
  for (int blk_id = 0; blk_id < code->InstructionBlockCount(); ++blk_id) {
    InstructionBlock *block =
        code->InstructionBlockAt(RpoNumber::FromInt(blk_id));
    if (!block->IsDeferred()) continue;
    RpoNumber last = GetLastDeferredBlock(block, code);
    block->set_last_deferred(last);
    // We know last is still deferred, and that last + 1, is not (or is an
    // invalid index). So skip over last + 1 and continue from last + 2. This
    // way, we visit each block exactly once, and the total complexity of this
    // function is O(n), n being jthe number of blocks.
    blk_id = last.ToInt() + 1;
  }
}


56
void CreateSplinter(TopLevelLiveRange *range, RegisterAllocationData *data,
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
                    LifetimePosition first_cut, LifetimePosition last_cut) {
  DCHECK(!range->IsSplinter());
  // We can ignore ranges that live solely in deferred blocks.
  // If a range ends right at the end of a deferred block, it is marked by
  // the range builder as ending at gap start of the next block - since the
  // end is a position where the variable isn't live. We need to take that
  // into consideration.
  LifetimePosition max_allowed_end = last_cut.NextFullStart();

  if (first_cut <= range->Start() && max_allowed_end >= range->End()) {
    return;
  }

  LifetimePosition start = Max(first_cut, range->Start());
  LifetimePosition end = Min(last_cut, range->End());

  if (start < end) {
    // Ensure the original range has a spill range associated, before it gets
    // splintered. Splinters will point to it. This way, when attempting to
    // reuse spill slots of splinters, during allocation, we avoid clobbering
    // such slots.
    if (range->MayRequireSpillRange()) {
      data->CreateSpillRangeForLiveRange(range);
    }
81 82 83 84 85 86
    if (range->splinter() == nullptr) {
      TopLevelLiveRange *splinter = data->NextLiveRange(range->machine_type());
      DCHECK_NULL(data->live_ranges()[splinter->vreg()]);
      data->live_ranges()[splinter->vreg()] = splinter;
      range->SetSplinter(splinter);
    }
87
    Zone *zone = data->allocation_zone();
88
    range->Splinter(start, end, zone);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  }
}


// Splinter all ranges live inside successive deferred blocks.
// No control flow analysis is performed. After the register allocation, we will
// merge the splinters back into the original ranges, and then rely on the
// range connector to properly connect them.
void SplinterRangesInDeferredBlocks(RegisterAllocationData *data) {
  InstructionSequence *code = data->code();
  int code_block_count = code->InstructionBlockCount();
  Zone *zone = data->allocation_zone();
  ZoneVector<BitVector *> &in_sets = data->live_in_sets();

  for (int i = 0; i < code_block_count; ++i) {
    InstructionBlock *block = code->InstructionBlockAt(RpoNumber::FromInt(i));
    if (!block->IsDeferred()) continue;

    RpoNumber last_deferred = block->last_deferred();
108 109
    // last_deferred + 1 is not deferred, so no point in visiting it.
    i = last_deferred.ToInt() + 1;
110 111 112 113 114 115 116

    LifetimePosition first_cut = LifetimePosition::GapFromInstructionIndex(
        block->first_instruction_index());

    LifetimePosition last_cut = LifetimePosition::GapFromInstructionIndex(
        static_cast<int>(code->instructions().size()));

117
    const BitVector *in_set = in_sets[block->rpo_number().ToInt()];
118
    BitVector ranges_to_splinter(*in_set, zone);
119
    InstructionBlock *last = code->InstructionBlockAt(last_deferred);
120 121 122 123 124 125 126 127 128
    for (int deferred_id = block->rpo_number().ToInt();
         deferred_id <= last->rpo_number().ToInt(); ++deferred_id) {
      const BitVector *ins = in_sets[deferred_id];
      ranges_to_splinter.Union(*ins);
      const BitVector *outs = LiveRangeBuilder::ComputeLiveOut(
          code->InstructionBlockAt(RpoNumber::FromInt(deferred_id)), data);
      ranges_to_splinter.Union(*outs);
    }

129 130 131 132 133 134
    int last_index = last->last_instruction_index();
    if (code->InstructionAt(last_index)->opcode() ==
        ArchOpcode::kArchDeoptimize) {
      ++last_index;
    }
    last_cut = LifetimePosition::GapFromInstructionIndex(last_index);
135 136 137 138 139 140 141

    BitVector::Iterator iterator(&ranges_to_splinter);

    while (!iterator.Done()) {
      int range_id = iterator.Current();
      iterator.Advance();

142
      TopLevelLiveRange *range = data->live_ranges()[range_id];
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      CreateSplinter(range, data, first_cut, last_cut);
    }
  }
}
}  // namespace


void LiveRangeSeparator::Splinter() {
  AssociateDeferredBlockSequences(data()->code());
  SplinterRangesInDeferredBlocks(data());
}


void LiveRangeMerger::Merge() {
  int live_range_count = static_cast<int>(data()->live_ranges().size());
  for (int i = 0; i < live_range_count; ++i) {
159 160
    TopLevelLiveRange *range = data()->live_ranges()[i];
    if (range == nullptr || range->IsEmpty() || !range->IsSplinter()) {
161 162
      continue;
    }
163
    TopLevelLiveRange *splinter_parent = range->splintered_from();
164

165 166 167
    int to_remove = range->vreg();
    splinter_parent->Merge(range, data()->allocation_zone());
    data()->live_ranges()[to_remove] = nullptr;
168 169 170 171 172 173 174
  }
}


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