setup structure for graph coloring

This commit is contained in:
2024-10-20 15:49:06 +00:00
parent 5468e8468d
commit 1073750a8d
4 changed files with 40 additions and 5 deletions

21
include/opt/confgraph.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include "IR/IR_basic.h"
#include "cfg.h"
class ConfGraphNode {
public:
std::string var_name;
size_t color;
bool is_move_related;
bool move_related_but_frozen;
bool is_binded_with_physical_reg;
std::vector<ConfGraphNode *> neighbors;
ConfGraphNode(std::string var_name) : var_name(var_name) {}
};
class ConfGraph {
public:
std::unordered_map<std::string, ConfGraphNode *> name_to_node;
std::vector<std::shared_ptr<ConfGraphNode>> nodes;
};
ConfGraph BuildConfGraph(CFGType &cfg);
bool TryColoring(std::shared_ptr<FunctionDefItem> src, CFGType &cfg, ConfGraph& confgraph);

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "liveanalysis.h" #include "liveanalysis.h"
#include "phieliminate.h" #include "phieliminate.h"
#include "confgraph.h"
std::shared_ptr<ModuleItem> RegAlloc(std::shared_ptr<ModuleItem> src); std::shared_ptr<ModuleItem> RegAlloc(std::shared_ptr<ModuleItem> src);

6
src/opt/confgraph.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "confgraph.h"
#include <unordered_map>
#include "cfg.h"
ConfGraph BuildConfGraph(CFGType &cfg) { ; }
bool TryColoring(std::shared_ptr<FunctionDefItem> src, CFGType &cfg, ConfGraph& confgraph) { return false; }

View File

@ -1,5 +1,7 @@
#include "regalloc.h" #include "regalloc.h"
#include "IR/IR_basic.h" #include "IR/IR_basic.h"
#include "cfg.h"
#include "confgraph.h"
#include "liveanalysis.h" #include "liveanalysis.h"
#include "phieliminate.h" #include "phieliminate.h"
#include "tools.h" #include "tools.h"
@ -125,17 +127,22 @@ void EnforcePhysicalRegs(CFGType &cfg) {
} }
} }
} }
void ConductRegAllocForFunction([[maybe_unused]] std::shared_ptr<FunctionDefItem> func, CFGType &cfg) { void ConductRegAllocForFunction(std::shared_ptr<FunctionDefItem> func) {
EnforcePhysicalRegs(cfg); CFGType cfg;
LiveAnalysis(cfg); ConfGraph confgraph;
do {
cfg = BuildCFGForFunction(func);
EnforcePhysicalRegs(cfg);
LiveAnalysis(cfg);
confgraph = BuildConfGraph(cfg);
} while (TryColoring(func, cfg, confgraph));
} }
std::shared_ptr<ModuleItem> RegAlloc(std::shared_ptr<ModuleItem> src) { std::shared_ptr<ModuleItem> RegAlloc(std::shared_ptr<ModuleItem> src) {
auto res = src; auto res = src;
for (auto &func : res->function_defs) { for (auto &func : res->function_defs) {
// func = std::make_shared<FunctionDefItem>(*func); // func = std::make_shared<FunctionDefItem>(*func);
auto cfg = BuildCFGForFunction(func); ConductRegAllocForFunction(func);
ConductRegAllocForFunction(func, cfg);
} }
return res; return res;
} }