| OLD | NEW |
| (Empty) | |
| 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/preprocess-live-ranges.h" |
| 6 #include "src/compiler/register-allocator.h" |
| 7 |
| 8 namespace v8 { |
| 9 namespace internal { |
| 10 namespace compiler { |
| 11 |
| 12 |
| 13 #define TRACE(...) \ |
| 14 do { \ |
| 15 if (FLAG_trace_alloc) PrintF(__VA_ARGS__); \ |
| 16 } while (false) |
| 17 |
| 18 |
| 19 namespace { |
| 20 |
| 21 LiveRange* Split(LiveRange* range, RegisterAllocationData* data, |
| 22 LifetimePosition pos) { |
| 23 DCHECK(range->Start() < pos && pos < range->End()); |
| 24 DCHECK(pos.IsStart() || pos.IsGapPosition() || |
| 25 (data->code() |
| 26 ->GetInstructionBlock(pos.ToInstructionIndex()) |
| 27 ->last_instruction_index() != pos.ToInstructionIndex())); |
| 28 LiveRange* result = data->NewChildRangeFor(range); |
| 29 range->SplitAt(pos, result, data->allocation_zone()); |
| 30 TRACE("Split range %d(v%d) @%d => %d.\n", range->id(), |
| 31 range->TopLevel()->id(), pos.ToInstructionIndex(), result->id()); |
| 32 return result; |
| 33 } |
| 34 |
| 35 |
| 36 LifetimePosition GetSplitPositionForInstruction(const LiveRange* range, |
| 37 int instruction_index) { |
| 38 LifetimePosition ret = LifetimePosition::Invalid(); |
| 39 |
| 40 ret = LifetimePosition::GapFromInstructionIndex(instruction_index); |
| 41 if (range->Start() >= ret || ret >= range->End()) { |
| 42 return LifetimePosition::Invalid(); |
| 43 } |
| 44 return ret; |
| 45 } |
| 46 |
| 47 |
| 48 LiveRange* SplitRangeAfterBlock(LiveRange* range, RegisterAllocationData* data, |
| 49 const InstructionBlock* block) { |
| 50 const InstructionSequence* code = data->code(); |
| 51 int last_index = block->last_instruction_index(); |
| 52 int outside_index = static_cast<int>(code->instructions().size()); |
| 53 bool has_handler = false; |
| 54 for (auto successor_id : block->successors()) { |
| 55 const InstructionBlock* successor = code->InstructionBlockAt(successor_id); |
| 56 if (successor->IsHandler()) { |
| 57 has_handler = true; |
| 58 } |
| 59 outside_index = Min(outside_index, successor->first_instruction_index()); |
| 60 } |
| 61 int split_at = has_handler ? outside_index : last_index; |
| 62 LifetimePosition after_block = |
| 63 GetSplitPositionForInstruction(range, split_at); |
| 64 |
| 65 if (after_block.IsValid()) { |
| 66 return Split(range, data, after_block); |
| 67 } |
| 68 |
| 69 return range; |
| 70 } |
| 71 |
| 72 |
| 73 int GetFirstInstructionIndex(const UseInterval* interval) { |
| 74 int ret = interval->start().ToInstructionIndex(); |
| 75 if (!interval->start().IsGapPosition() && !interval->start().IsStart()) { |
| 76 ++ret; |
| 77 } |
| 78 return ret; |
| 79 } |
| 80 |
| 81 |
| 82 bool DoesSubsequenceClobber(const InstructionSequence* code, int start, |
| 83 int stop) { |
| 84 for (int i = start; i <= stop; ++i) { |
| 85 if (code->InstructionAt(i)->IsCall()) return true; |
| 86 } |
| 87 return false; |
| 88 } |
| 89 |
| 90 |
| 91 void SplitRangeByClobberingDeferredBlocks(LiveRange* range, |
| 92 RegisterAllocationData* data) { |
| 93 DCHECK(!range->IsFixed()); |
| 94 DCHECK(!range->spilled()); |
| 95 |
| 96 const InstructionSequence* code = data->code(); |
| 97 LiveRange* current_subrange = range; |
| 98 |
| 99 UseInterval* interval = current_subrange->first_interval(); |
| 100 |
| 101 while (interval != nullptr) { |
| 102 int first_index = GetFirstInstructionIndex(interval); |
| 103 int last_index = interval->end().ToInstructionIndex(); |
| 104 |
| 105 if (last_index >= code->InstructionCount()) { |
| 106 last_index = code->LastInstructionIndex(); |
| 107 } |
| 108 interval = interval->next(); |
| 109 |
| 110 for (int index = first_index; index <= last_index;) { |
| 111 const InstructionBlock* block = code->GetInstructionBlock(index); |
| 112 int last_block_index = static_cast<int>(block->last_instruction_index()); |
| 113 int last_covered_index = Min(last_index, last_block_index); |
| 114 int working_index = index; |
| 115 index = block->last_instruction_index() + 1; |
| 116 |
| 117 if (!block->IsDeferred() || |
| 118 !DoesSubsequenceClobber(code, working_index, last_covered_index)) { |
| 119 continue; |
| 120 } |
| 121 |
| 122 TRACE("Deferred block B%d clobbers range %d(v%d).\n", |
| 123 block->rpo_number().ToInt(), current_subrange->id(), |
| 124 current_subrange->TopLevel()->id()); |
| 125 LifetimePosition block_start = |
| 126 GetSplitPositionForInstruction(current_subrange, working_index); |
| 127 LiveRange* block_and_after = nullptr; |
| 128 if (block_start.IsValid()) { |
| 129 block_and_after = Split(current_subrange, data, block_start); |
| 130 } else { |
| 131 block_and_after = current_subrange; |
| 132 } |
| 133 LiveRange* next = SplitRangeAfterBlock(block_and_after, data, block); |
| 134 if (next != current_subrange) interval = next->first_interval(); |
| 135 current_subrange = next; |
| 136 break; |
| 137 } |
| 138 } |
| 139 } |
| 140 } |
| 141 |
| 142 |
| 143 void PreprocessLiveRanges::PreprocessRanges() { |
| 144 SplitRangesAroundDeferredBlocks(); |
| 145 } |
| 146 |
| 147 |
| 148 void PreprocessLiveRanges::SplitRangesAroundDeferredBlocks() { |
| 149 size_t live_range_count = data()->live_ranges().size(); |
| 150 for (size_t i = 0; i < live_range_count; i++) { |
| 151 LiveRange* range = data()->live_ranges()[i]; |
| 152 if (range != nullptr && !range->IsEmpty() && !range->spilled() && |
| 153 !range->IsFixed() && !range->IsChild()) { |
| 154 SplitRangeByClobberingDeferredBlocks(range, data()); |
| 155 } |
| 156 } |
| 157 } |
| 158 |
| 159 } // namespace compiler |
| 160 } // namespace internal |
| 161 } // namespace v8 |
| OLD | NEW |