ready to conduct Conflict Graph Coloring

This commit is contained in:
2024-10-20 02:51:03 +00:00
parent 18105a9bf5
commit 5468e8468d
5 changed files with 112 additions and 13 deletions

View File

@ -1,6 +1,7 @@
#include "regalloc.h"
#include "IR/IR_basic.h"
#include "liveanalysis.h"
#include "phieliminate.h"
#include "tools.h"
// RISC-V calling convention compatible
@ -69,6 +70,60 @@ void EnforcePhysicalRegs(CFGType &cfg) {
}
}
// TODO: process caller side
for (auto node : cfg.nodes) {
auto block = node->corresponding_block;
for (auto it_act = block->actions.begin(); it_act != block->actions.end(); ++it_act) {
if (!std::dynamic_pointer_cast<CallItem>(*it_act)) continue;
auto it_next = it_act;
++it_next;
auto call_act = std::dynamic_pointer_cast<CallItem>(*it_act);
if (!std::holds_alternative<LLVMVOIDType>(call_act->return_type)) {
auto return_value_collect = std::make_shared<MoveInstruct>();
return_value_collect->ty = call_act->return_type;
return_value_collect->src_full = "%reg.x10";
return_value_collect->dest_full = call_act->result_full;
block->actions.insert(it_next, return_value_collect);
call_act->result_full = "%reg.x10";
}
std::unordered_set<std::string> caller_saved_regs_to_process;
for (auto reg : caller_saved_regs) {
caller_saved_regs_to_process.insert(reg);
}
for (size_t i = 0; i < call_act->args_val_full.size() & i < 8; i++) {
auto new_move = std::make_shared<MoveInstruct>();
new_move->ty = call_act->args_ty[i];
new_move->src_full = call_act->args_val_full[i];
new_move->dest_full = "%reg." + arg_regs[i];
block->actions.insert(it_act, new_move);
call_act->args_val_full[i] = "%reg." + arg_regs[i];
caller_saved_regs_to_process.erase(arg_regs[i]);
}
if (call_act->args_val_full.size() > 8) {
for (size_t i = 8; i < call_act->args_val_full.size(); i++) {
auto new_store = std::make_shared<StoreSpilledArgs>();
new_store->arg_id = i;
new_store->var_full = call_act->args_val_full[i];
new_store->ty = call_act->args_ty[i];
block->actions.insert(it_act, new_store);
}
call_act->args_val_full.resize(8);
call_act->args_ty.resize(8);
}
for (auto reg : caller_saved_regs_to_process) {
auto new_def = std::make_shared<ForceDef>();
new_def->var_full = "%reg." + reg;
new_def->ty = LLVMIRIntType(32);
block->actions.insert(it_act, new_def);
}
for (auto reg : caller_saved_regs) {
auto new_use = std::make_shared<ForceUse>();
new_use->var_full = "%reg." + reg;
block->actions.insert(it_next, new_use);
}
it_act = it_next;
--it_act;
}
}
}
void ConductRegAllocForFunction([[maybe_unused]] std::shared_ptr<FunctionDefItem> func, CFGType &cfg) {
EnforcePhysicalRegs(cfg);