5 #include "src/compiler/backend/live-range-separator.h" 6 #include "src/compiler/backend/register-allocator.h" 14 if (FLAG_trace_alloc) PrintF(__VA_ARGS__); \ 19 void CreateSplinter(TopLevelLiveRange* range, RegisterAllocationData* data,
20 LifetimePosition first_cut, LifetimePosition last_cut) {
21 DCHECK(!range->IsSplinter());
27 LifetimePosition max_allowed_end = last_cut.NextFullStart();
29 if (first_cut <= range->Start() && max_allowed_end >= range->End()) {
33 LifetimePosition start = Max(first_cut, range->Start());
34 LifetimePosition end = Min(last_cut, range->End());
41 if (range->MayRequireSpillRange()) {
42 data->CreateSpillRangeForLiveRange(range);
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);
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);
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);
68 void SplinterLiveRange(TopLevelLiveRange* range, RegisterAllocationData* data) {
69 const InstructionSequence* code = data->code();
70 UseInterval* interval = range->first_interval();
72 LifetimePosition first_cut = LifetimePosition::Invalid();
73 LifetimePosition last_cut = LifetimePosition::Invalid();
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());
91 last_cut = LifetimePosition::GapFromInstructionIndex(
92 current_block->last_instruction_index());
94 if (first_cut.IsValid()) {
95 CreateSplinter(range, data, first_cut, last_cut);
96 first_cut = LifetimePosition::Invalid();
97 last_cut = LifetimePosition::Invalid();
101 interval = next_interval;
105 if (first_cut.IsValid()) {
106 CreateSplinter(range, data, first_cut, last_cut);
110 if (range->has_slot_use() && range->splinter() !=
nullptr) {
112 SetSlotUse(range->splinter());
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()) {
125 int first_instr = range->first_interval()->FirstGapIndex();
126 if (!data()->code()->GetInstructionBlock(first_instr)->IsDeferred()) {
127 SplinterLiveRange(range, data());
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()) {
140 LiveRange* child = top;
141 for (; child !=
nullptr; child = child->next()) {
142 if (child->spilled() ||
143 child->NextSlotPosition(child->Start()) !=
nullptr) {
147 if (child ==
nullptr) {
148 top->TreatAsSpilledInDeferredBlock(data()->allocation_zone(),
149 code->InstructionBlockCount());
154 void LiveRangeMerger::Merge() {
155 MarkRangesSpilledInDeferredBlocks();
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()) {
163 TopLevelLiveRange* splinter_parent = range->splintered_from();
165 int to_remove = range->vreg();
166 splinter_parent->Merge(range, data()->allocation_zone());
167 data()->live_ranges()[to_remove] =
nullptr;