set up structure for opt

This commit is contained in:
2024-10-17 14:25:21 +00:00
parent cafd3ccdac
commit 84c92dac81
9 changed files with 182 additions and 8 deletions

View File

@ -84,6 +84,7 @@ class BRAction : public JMPActionItem {
NaiveBackend::FuncLayout &layout,
const std::unordered_map<std::string, IRClassInfo> &low_level_class_info,
bool process_phi);
friend class CFGType BuildCFGForFunction(const std::shared_ptr<class FunctionDefItem> &func);
std::string cond;
std::string true_label_full;
std::string false_label_full;
@ -102,6 +103,7 @@ class UNConditionJMPAction : public JMPActionItem {
NaiveBackend::FuncLayout &layout,
const std::unordered_map<std::string, IRClassInfo> &low_level_class_info,
bool process_phi);
friend class CFGType BuildCFGForFunction(const std::shared_ptr<class FunctionDefItem> &func);
std::string label_full;
public:
@ -308,6 +310,7 @@ class BlockItem : public LLVMIRItemBase {
friend void GenerateNaiveASM(std::ostream &os, std::shared_ptr<ModuleItem> prog);
friend void NaiveBackend::ScanForVar(class NaiveBackend::FuncLayout &layout, std::shared_ptr<ActionItem> action,
const std::unordered_map<std::string, IRClassInfo> &low_level_class_info);
friend class CFGType BuildCFGForFunction(const std::shared_ptr<class FunctionDefItem> &func);
std::string label_full;
std::vector<std::shared_ptr<ActionItem>> actions;
std::shared_ptr<JMPActionItem> exit_action;
@ -447,6 +450,7 @@ class SelectItem : public ActionItem {
class FunctionDefItem : public LLVMIRItemBase {
friend class IRBuilder;
friend void GenerateNaiveASM(std::ostream &os, std::shared_ptr<ModuleItem> prog);
friend class CFGType BuildCFGForFunction(const std::shared_ptr<FunctionDefItem> &func);
LLVMType return_type;
std::string func_name_raw;
std::vector<LLVMType> args;
@ -566,6 +570,7 @@ class ModuleItem : public LLVMIRItemBase {
friend class IRBuilder;
friend std::shared_ptr<ModuleItem> BuildIR(std::shared_ptr<Program_ASTNode> src);
friend void GenerateNaiveASM(std::ostream &os, std::shared_ptr<ModuleItem> prog);
friend std::shared_ptr<ModuleItem> Mem2Reg(std::shared_ptr<ModuleItem> src);
std::vector<std::shared_ptr<ConstStrItem>> const_strs;
std::vector<std::shared_ptr<FunctionDeclareItem>> function_declares;
std::vector<std::shared_ptr<TypeDefItem>> type_defs;

31
include/opt/cfg.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#include <deque>
#include <list>
#include <memory>
#include <unordered_map>
#include <vector>
#include "IR/IR_basic.h"
using CFGNodeCollection = std::list<class CFGNodeType *>;
class CFGNodeType {
public:
std::vector<CFGNodeType *> successors, predecessors;
BlockItem *corresponding_block;
CFGNodeCollection dom;
CFGNodeType *idom;
std::vector<CFGNodeType *> successors_in_dom_tree;
CFGNodeCollection dom_frontier;
};
class CFGType {
public:
std::vector<std::shared_ptr<CFGNodeType>> nodes;
CFGNodeType *entry;
std::unordered_map<BlockItem *, CFGNodeType *> block_to_node;
std::unordered_map<std::string, BlockItem *> label_to_block;
};
CFGNodeCollection GetCFGNodeCollectionsIntersection(const CFGNodeCollection &a, const CFGNodeCollection &b);
CFGNodeCollection GetCFGNodeCollectionsUnion(const CFGNodeCollection &a, const CFGNodeCollection &b);
CFGNodeCollection GetCFGNodeCollectionsDifference(const CFGNodeCollection &a, const CFGNodeCollection &b);
CFGType BuildCFGForFunction(const std::shared_ptr<FunctionDefItem> &func);

4
include/opt/mem2reg.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#include "IR/IR_basic.h"
std::shared_ptr<ModuleItem> Mem2Reg(std::shared_ptr<ModuleItem> src);

2
include/opt/opt.h Normal file
View File

@ -0,0 +1,2 @@
#include "mem2reg.h"
#include "cfg.h"