Skip to content

Commit

Permalink
Some behavior-preserving micro-improvements
Browse files Browse the repository at this point in the history
Reviewed By: NicholasGorski

Differential Revision: D38595125

fbshipit-source-id: 6cb623f4fe2efe02aebe9089cbfec2386c38f1fd
  • Loading branch information
NTillmann authored and facebook-github-bot committed Aug 13, 2022
1 parent c881b43 commit aea0b8b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 27 deletions.
20 changes: 8 additions & 12 deletions service/regalloc/GraphColoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ constexpr int INVALID_SCORE = std::numeric_limits<int>::max();
* move instruction to remap :reg.
*/
bool needs_remap(const transform::RegMap& reg_map, reg_t reg, vreg_t vreg) {
return reg_map.find(reg) != reg_map.end() && reg_map.at(reg) != vreg;
auto it = reg_map.find(reg);
return it != reg_map.end() && it->second != vreg;
}

/*
Expand All @@ -153,8 +154,6 @@ int score_range_fit(
auto reg = range_regs.at(i);
const auto& node = ig.get_node(reg);
const auto& vreg_file = vreg_files.at(reg);
// XXX We could be more precise here by checking the LivenessDomain for the
// given range instruction instead of just using the graph
if (!vreg_file.is_free(vreg, node.width())) {
return INVALID_SCORE;
}
Expand Down Expand Up @@ -208,13 +207,13 @@ void fit_range_instruction(
for (size_t i = 0; i < insn->srcs_size(); ++i) {
auto src = insn->src(i);
const auto& node = ig.get_node(src);
const auto& vreg_file = vreg_files.at(src);
auto& reg_map = reg_transform->map;
// If the vreg we're trying to map the node to is too large, or if the node
// has been mapped to a different vreg already, we need to spill it.
if (vreg > node.max_vreg() || needs_remap(reg_map, src, vreg)) {
spills->range_spills[insn].emplace_back(i);
} else {
const auto& vreg_file = vreg_files.at(src);
always_assert(vreg_file.is_free(vreg, node.width()));
reg_map.emplace(src, vreg);
}
Expand All @@ -239,13 +238,13 @@ void fit_params(
auto* insn = mie.insn;
auto dest = insn->dest();
const auto& node = ig.get_node(dest);
const auto& vreg_file = vreg_files.at(dest);
auto& reg_map = reg_transform->map;
// If the vreg we're trying to map the node to is too large, or if the node
// has been mapped to a different vreg already, we need to spill it.
if (vreg > node.max_vreg() || needs_remap(reg_map, dest, vreg)) {
spills->param_spills.emplace(dest);
} else {
const auto& vreg_file = vreg_files.at(dest);
always_assert(vreg_file.is_free(vreg, node.width()));
reg_map.emplace(dest, vreg);
}
Expand Down Expand Up @@ -577,10 +576,9 @@ void Allocator::select_ranges(const cfg::ControlFlowGraph& cfg,
TRACE(REG, 5, "Allocating %s as range kind", SHOW(insn));
std::unordered_map<reg_t, VirtualRegistersFile> vreg_files;
for (size_t i = 0; i < insn->srcs_size(); ++i) {
VirtualRegistersFile vreg_file;
auto src = insn->src(i);
VirtualRegistersFile& vreg_file = vreg_files[src];
mark_adjacent(ig, src, reg_transform->map, &vreg_file);
vreg_files.emplace(src, std::move(vreg_file));
}

vreg_t range_base =
Expand Down Expand Up @@ -608,9 +606,8 @@ void Allocator::select_params(const cfg::ControlFlowGraph& cfg,
const auto& node = ig.get_node(dest);
params_size += node.width();
param_regs.emplace_back(dest);
VirtualRegistersFile vreg_file;
VirtualRegistersFile& vreg_file = vreg_files[dest];
mark_adjacent(ig, dest, reg_transform->map, &vreg_file);
vreg_files.emplace(dest, std::move(vreg_file));
}

auto min_param_reg =
Expand Down Expand Up @@ -938,8 +935,7 @@ void Allocator::spill(const interference::Graph& ig,
}
auto& node = ig.get_node(src);
auto max_value = max_value_for_src(insn, i, node.width() == 2);
if (sp_it != spill_plan.global_spills.end() &&
sp_it->second > max_value) {
if (sp_it->second > max_value) {
auto temp = cfg.allocate_temp();
insn->set_src(i, temp);
auto mov = gen_move(node.type(), temp, src);
Expand Down Expand Up @@ -1036,6 +1032,7 @@ void Allocator::allocate(cfg::ControlFlowGraph& cfg, bool is_static) {
// monotonically increasing set, i.e. we only add and never remove from it
// in the allocation loop below.
auto range_set = init_range_set(cfg);
range_set.prioritize();

bool no_overwrite_this = m_config.no_overwrite_this && !is_static;
if (no_overwrite_this) {
Expand Down Expand Up @@ -1090,7 +1087,6 @@ void Allocator::allocate(cfg::ControlFlowGraph& cfg, bool is_static) {
select(cfg, ig, &select_stack, &reg_transform, &spill_plan);

TRACE(REG, 5, "Transform before range alloc:\n%s", SHOW(reg_transform));
range_set.prioritize();
select_ranges(cfg, ig, range_set, &reg_transform, &spill_plan);
// Select registers for symregs that can be addressed using all 16 bits.
// These symregs are typically generated during the spilling and splitting
Expand Down
3 changes: 0 additions & 3 deletions service/regalloc/Interference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,6 @@ Graph GraphBuilder::build(const LivenessFixpointIterator& fixpoint_iter,
}
auto insn = it->insn;
auto op = insn->opcode();
if (opcode::has_range_form(op)) {
graph.m_range_liveness.emplace(insn, live_out);
}
if (insn->has_dest()) {
for (auto reg : live_out.elements()) {
if (opcode::is_a_move(op) && reg == insn->src(0)) {
Expand Down
12 changes: 0 additions & 12 deletions service/regalloc/Interference.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,6 @@ class Graph {
m_containment_graph.end();
}

/*
* Returns the live-out info for a given instruction that has a potential
* range encoding. We can use it to make better allocation decisions for
* these instructions.
*/
const LivenessDomain& get_liveness(const IRInstruction* insn) const {
return m_range_liveness.at(const_cast<IRInstruction*>(insn));
}

void remove_node(reg_t);

/*
Expand Down Expand Up @@ -240,9 +231,6 @@ class Graph {
std::unordered_map<reg_t, Node> m_nodes;
std::unordered_map<reg_pair_t, bool> m_adj_matrix;
std::unordered_set<reg_pair_t> m_containment_graph;
// This map contains the LivenessDomains for all instructions which could
// potentialy take on the /range format.
std::unordered_map<IRInstruction*, LivenessDomain> m_range_liveness;

friend class impl::GraphBuilder;
};
Expand Down

0 comments on commit aea0b8b

Please sign in to comment.