begin to fix stupid Coalesce

This commit is contained in:
2024-10-22 07:00:48 +00:00
parent bce4a64c00
commit 4c9d010ff7
4 changed files with 466 additions and 1 deletions

View File

@ -286,6 +286,7 @@ void Simplify(std::shared_ptr<FunctionDefItem> src, CFGType &cfg, ConfGraph &con
confgraph.low_degree_and_not_move_related.erase(u);
DetachNode(u, confgraph);
confgraph.stack.push_back(u);
confgraph.awful_moves.clear();
}
void Coalesce(std::shared_ptr<FunctionDefItem> src, CFGType &cfg, ConfGraph &confgraph) {
@ -324,8 +325,10 @@ void Coalesce(std::shared_ptr<FunctionDefItem> src, CFGType &cfg, ConfGraph &con
}
if (condition_satisfied) {
MergeNodeInto(src_node, dest_node, confgraph);
confgraph.awful_moves.clear();
} else {
confgraph.potential_moves.insert(move);
confgraph.awful_moves.insert(move);
}
} else {
// Briggs condition
@ -342,8 +345,10 @@ void Coalesce(std::shared_ptr<FunctionDefItem> src, CFGType &cfg, ConfGraph &con
}
if (dangerous_neighbors.size() < kMaxRegs) {
MergeNodeInto(src_node, dest_node, confgraph);
confgraph.awful_moves.clear();
} else {
confgraph.potential_moves.insert(move);
confgraph.awful_moves.insert(move);
}
}
}
@ -362,6 +367,7 @@ void PotentailSpill(std::shared_ptr<FunctionDefItem> src, CFGType &cfg, ConfGrap
confgraph.high_degree_nodes.erase(u);
DetachNode(u, confgraph);
confgraph.stack.push_back(u);
confgraph.awful_moves.clear();
}
void GraphCheck(ConfGraph &confgraph) {
@ -457,6 +463,7 @@ bool ConductColoring(std::shared_ptr<FunctionDefItem> src, CFGType &cfg, ConfGra
// std::cerr << "confgraph.low_degree_and_move_related.size()=" << confgraph.low_degree_and_move_related.size()
// << std::endl;
// std::cerr << "confgraph.high_degree_nodes.size()=" << confgraph.high_degree_nodes.size() << std::endl;
// std::cerr << "confgraph.potential_moves.size()=" << confgraph.potential_moves.size() << std::endl;
// std::cerr << "pending moves" << std::endl;
// for (auto mode : confgraph.pending_moves) {
// std::cerr << '\t';
@ -469,7 +476,7 @@ bool ConductColoring(std::shared_ptr<FunctionDefItem> src, CFGType &cfg, ConfGra
// mode->RecursivePrint(std::cerr);
// }
// std::cerr << std::endl;
GraphCheck(confgraph);
// GraphCheck(confgraph);
for (auto node : confgraph.low_degree_and_not_move_related) {
if (node->is_binded_with_physical_reg) {
throw std::runtime_error("something strange happened: node is binded with physical reg");
@ -494,6 +501,9 @@ bool ConductColoring(std::shared_ptr<FunctionDefItem> src, CFGType &cfg, ConfGra
if (confgraph.potential_moves.size() > 0) {
std::vector<opt::MoveInstruct *> removed;
for (auto move : confgraph.potential_moves) {
if (confgraph.awful_moves.find(move) != confgraph.awful_moves.end()) {
continue;
}
auto src_node = confgraph.id_to_node[cfg.var_to_id[move->src_full]];
auto dest_node = confgraph.id_to_node[cfg.var_to_id[move->dest_full]];
src_node = ConfGraphNode::FindFather(src_node);

View File

@ -299,6 +299,34 @@ void TranslateColorResult(std::shared_ptr<FunctionDefItem> func, CFGType &cfg, C
}
}
}
void PairMoveEliminate(std::shared_ptr<FunctionDefItem> func, CFGType &cfg, ConfGraph &confgraph) {
for (auto node : cfg.nodes) {
auto block = node->corresponding_block;
auto it = block->actions.begin();
while (it != block->actions.end()) {
while (it != block->actions.end() && !std::dynamic_pointer_cast<opt::MoveInstruct>(*it)) {
++it;
}
if (it == block->actions.end()) break;
std::unordered_map<std::string, std::list<std::shared_ptr<ActionItem>>::iterator> defined_by;
do {
auto cur_move = std::dynamic_pointer_cast<opt::MoveInstruct>(*it);
if (defined_by.find(cur_move->src_full) != defined_by.end() &&
std::dynamic_pointer_cast<opt::MoveInstruct>(*defined_by[cur_move->src_full])->src_full ==
cur_move->dest_full) {
auto it_next = it;
++it_next;
block->actions.erase(it);
it = it_next;
} else {
defined_by[cur_move->dest_full] = it;
++it;
}
} while (it != block->actions.end() && std::dynamic_pointer_cast<opt::MoveInstruct>(*it));
}
}
}
void ConductRegAllocForFunction(std::shared_ptr<FunctionDefItem> func) {
std::cerr << "processing function " << func->func_name_raw << std::endl;
CFGType cfg;
@ -314,6 +342,7 @@ void ConductRegAllocForFunction(std::shared_ptr<FunctionDefItem> func) {
confgraph = BuildConfGraph(cfg);
} while (ConductColoring(func, cfg, confgraph));
TranslateColorResult(func, cfg, confgraph);
// PairMoveEliminate(func, cfg, confgraph);
func->RecursivePrint(std::cerr);
}