Skip to content

Commit

Permalink
[vm/compiler] Use loop framework for register allocator
Browse files Browse the repository at this point in the history
Rationale:
Rather than relying on a separate loop detector, rely
on the new loop framework, which avoids code duplication
and ensures any improvement in loop detection/handling
will benefit this phase too. Note, most of the time, the
same loops are discovered with a few exceptions (which
is okay, since this is "just" heuristic usage). This CL
also simplifies loop detection a bit.

dart-lang/sdk#34473

Change-Id: I1a1b19b99a698c74822473d2a1fe370287c1ade4
Reviewed-on: https://dart-review.googlesource.com/c/80523
Commit-Queue: Aart Bik <ajcbik@google.com>
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
Reviewed-by: Alexander Markov <alexmarkov@google.com>
  • Loading branch information
aartbik authored and commit-bot@chromium.org committed Oct 22, 2018
1 parent 7fd78ed commit 914065d
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 255 deletions.
24 changes: 2 additions & 22 deletions runtime/vm/compiler/backend/flow_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1604,25 +1604,6 @@ BitVector* FlowGraph::FindLoopBlocks(BlockEntryInstr* m,
return loop_blocks;
}

void FlowGraph::LinkToInner(LoopInfo* loop) const {
for (; loop != nullptr; loop = loop->next()) {
if (FLAG_trace_optimization) {
THR_Print("%s {", loop->ToCString());
for (BitVector::Iterator it(loop->blocks()); !it.Done(); it.Advance()) {
THR_Print(" B%" Pd, preorder_[it.Current()]->block_id());
}
THR_Print(" }\n");
}
LinkToInner(loop->inner());
for (BitVector::Iterator it(loop->blocks()); !it.Done(); it.Advance()) {
BlockEntryInstr* block = preorder_[it.Current()];
if (block->loop_info() == nullptr) {
block->set_loop_info(loop);
}
}
}
}

LoopHierarchy* FlowGraph::ComputeLoops() const {
// Iterate over all entry blocks in the flow graph to attach
// loop information to each loop header.
Expand All @@ -1649,15 +1630,14 @@ LoopHierarchy* FlowGraph::ComputeLoops() const {
ASSERT(block->loop_info()->header() == block);
block->loop_info()->AddBlocks(loop_blocks);
}
block->loop_info()->AddBackEdge(pred);
}
}
}

// Build the loop hierarchy and link every entry block to
// the closest enveloping loop in loop hierarchy.
LoopHierarchy* loop_hierarchy = new (zone()) LoopHierarchy(loop_headers);
LinkToInner(loop_hierarchy->first());
return loop_hierarchy;
return new (zone()) LoopHierarchy(loop_headers, preorder_);
}

intptr_t FlowGraph::InstructionCount() const {
Expand Down
17 changes: 9 additions & 8 deletions runtime/vm/compiler/backend/flow_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,11 @@ class FlowGraph : public ZoneAllocated {
if (loop_hierarchy_ == nullptr) {
loop_hierarchy_ = ComputeLoops();
}
return *loop_hierarchy_;
return loop_hierarchy();
}

const LoopHierarchy& loop_hierarchy() const { return *loop_hierarchy_; }

// Resets the loop hierarchy of the flow graph. Use this to
// force a recomputation of loop detection by the next call
// to GetLoopHierarchy() (note that this does not immediately
Expand All @@ -305,10 +307,6 @@ class FlowGraph : public ZoneAllocated {
loop_invariant_loads_ = nullptr;
}

// Helper method to find the natural loops in the flow graph and attach
// the loop information to each entry block. Returns the loop hierarchy.
LoopHierarchy* ComputeLoops() const;

// Per loop header invariant loads sets. Each set contains load id for
// those loads that are not affected by anything in the loop and can be
// hoisted out. Sets are computed by LoadOptimizer.
Expand Down Expand Up @@ -407,10 +405,12 @@ class FlowGraph : public ZoneAllocated {
PhiInstr* AddPhi(JoinEntryInstr* join, Definition* d1, Definition* d2);

private:
friend class FlowGraphCompiler; // TODO(ajcbik): restructure
friend class IfConverter;
friend class BranchSimplifier;
friend class ConstantPropagator;
friend class DeadCodeElimination;
friend class Intrinsifier;

// SSA transformation methods and fields.
void ComputeDominators(GrowableArray<BitVector*>* dominance_frontier);
Expand Down Expand Up @@ -454,14 +454,15 @@ class FlowGraph : public ZoneAllocated {
void ReplacePredecessor(BlockEntryInstr* old_block,
BlockEntryInstr* new_block);

// Find the blocks in the natural loop for the back edge m->n. The
// Finds the blocks in the natural loop for the back edge m->n. The
// algorithm is described in "Advanced Compiler Design & Implementation"
// (Muchnick) p192. Returns a BitVector indexed by block pre-order
// number where each bit indicates membership in the loop.
BitVector* FindLoopBlocks(BlockEntryInstr* m, BlockEntryInstr* n) const;

// Attaches closest enveloping loop in the hierarchy to each block entry.
void LinkToInner(LoopInfo* loop) const;
// Finds the natural loops in the flow graph and attaches the loop
// information to each entry block. Returns the loop hierarchy.
LoopHierarchy* ComputeLoops() const;

void InsertConversionsFor(Definition* def);
void ConvertUse(Value* use, Representation from);
Expand Down
5 changes: 5 additions & 0 deletions runtime/vm/compiler/backend/il.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "vm/compiler/backend/flow_graph_compiler.h"
#include "vm/compiler/backend/linearscan.h"
#include "vm/compiler/backend/locations.h"
#include "vm/compiler/backend/loops.h"
#include "vm/compiler/backend/range_analysis.h"
#include "vm/compiler/frontend/flow_graph_builder.h"
#include "vm/compiler/jit/compiler.h"
Expand Down Expand Up @@ -1595,6 +1596,10 @@ BlockEntryInstr* BlockEntryInstr::ImmediateDominator() const {
return NULL;
}

bool BlockEntryInstr::IsLoopHeader() const {
return loop_info_ != nullptr && loop_info_->header() == this;
}

// Helper to mutate the graph during inlining. This block should be
// replaced with new_block as a predecessor of all of this block's
// successors. For each successor, the predecessors will be reordered
Expand Down
1 change: 1 addition & 0 deletions runtime/vm/compiler/backend/il.h
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,7 @@ class BlockEntryInstr : public Instruction {
// Loop related methods.
LoopInfo* loop_info() const { return loop_info_; }
void set_loop_info(LoopInfo* loop_info) { loop_info_ = loop_info; }
bool IsLoopHeader() const;

virtual BlockEntryInstr* GetBlock() { return this; }

Expand Down
Loading

0 comments on commit 914065d

Please sign in to comment.