Merge remote-tracking branch 'origin/midend-mem2reg' into midend
This commit is contained in:
10
src/IR.cpp
10
src/IR.cpp
@@ -530,8 +530,14 @@ Function * Function::clone(const std::string &suffix) const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto ¶m : blocks.front()->getArguments()) {
|
// for (const auto ¶m : blocks.front()->getArguments()) {
|
||||||
newFunction->getEntryBlock()->insertArgument(dynamic_cast<AllocaInst *>(oldNewValueMap.at(param)));
|
// newFunction->getEntryBlock()->insertArgument(dynamic_cast<AllocaInst *>(oldNewValueMap.at(param)));
|
||||||
|
// }
|
||||||
|
for (const auto &arg : arguments) {
|
||||||
|
auto newArg = dynamic_cast<Argument *>(oldNewValueMap.at(arg));
|
||||||
|
if (newArg != nullptr) {
|
||||||
|
newFunction->insertArgument(newArg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return newFunction;
|
return newFunction;
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ bool SysYCFGOptUtils::SysYBlockMerge(Function *func) {
|
|||||||
// std::cout << "merge block: " << blockiter->get()->getName() << std::endl;
|
// std::cout << "merge block: " << blockiter->get()->getName() << std::endl;
|
||||||
BasicBlock* block = blockiter->get();
|
BasicBlock* block = blockiter->get();
|
||||||
BasicBlock* nextBlock = blockiter->get()->getSuccessors()[0];
|
BasicBlock* nextBlock = blockiter->get()->getSuccessors()[0];
|
||||||
auto nextarguments = nextBlock->getArguments();
|
// auto nextarguments = nextBlock->getArguments();
|
||||||
// 删除br指令
|
// 删除br指令
|
||||||
if (block->getNumInstructions() != 0) {
|
if (block->getNumInstructions() != 0) {
|
||||||
auto thelastinstinst = block->end();
|
auto thelastinstinst = block->end();
|
||||||
@@ -108,12 +108,6 @@ bool SysYCFGOptUtils::SysYBlockMerge(Function *func) {
|
|||||||
block->getInstructions().emplace_back(institer->release());
|
block->getInstructions().emplace_back(institer->release());
|
||||||
institer = nextBlock->getInstructions().erase(institer);
|
institer = nextBlock->getInstructions().erase(institer);
|
||||||
}
|
}
|
||||||
// 合并参数
|
|
||||||
// TODO:是否需要去重?
|
|
||||||
for (auto &argm : nextarguments) {
|
|
||||||
argm->setParent(block);
|
|
||||||
block->insertArgument(argm);
|
|
||||||
}
|
|
||||||
// 更新前驱后继关系,类似树节点操作
|
// 更新前驱后继关系,类似树节点操作
|
||||||
block->removeSuccessor(nextBlock);
|
block->removeSuccessor(nextBlock);
|
||||||
nextBlock->removePredecessor(block);
|
nextBlock->removePredecessor(block);
|
||||||
|
|||||||
@@ -335,7 +335,6 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
|
|||||||
module->enterNewScope();
|
module->enterNewScope();
|
||||||
|
|
||||||
auto name = ctx->Ident()->getText();
|
auto name = ctx->Ident()->getText();
|
||||||
std::vector<Type *> paramTypes;
|
|
||||||
std::vector<Type *> paramActualTypes;
|
std::vector<Type *> paramActualTypes;
|
||||||
std::vector<std::string> paramNames;
|
std::vector<std::string> paramNames;
|
||||||
std::vector<std::vector<Value *>> paramDims;
|
std::vector<std::vector<Value *>> paramDims;
|
||||||
@@ -400,12 +399,25 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
|
|||||||
BasicBlock* entry = function->getEntryBlock();
|
BasicBlock* entry = function->getEntryBlock();
|
||||||
builder.setPosition(entry, entry->end());
|
builder.setPosition(entry, entry->end());
|
||||||
|
|
||||||
|
for(int i = 0; i < paramActualTypes.size(); ++i) {
|
||||||
|
Argument* arg = new Argument(paramActualTypes[i], function, i, paramNames[i]);
|
||||||
|
function->insertArgument(arg);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
auto funcArgs = function->getArguments();
|
||||||
|
std::vector<AllocaInst *> allocas;
|
||||||
for (int i = 0; i < paramActualTypes.size(); ++i) {
|
for (int i = 0; i < paramActualTypes.size(); ++i) {
|
||||||
AllocaInst* alloca = builder.createAllocaInst(Type::getPointerType(paramActualTypes[i]), {},paramNames[i]);
|
AllocaInst *alloca = builder.createAllocaInst(paramActualTypes[i], {}, paramNames[i]);
|
||||||
entry->insertArgument(alloca);
|
allocas.push_back(alloca);
|
||||||
module->addVariable(paramNames[i], alloca);
|
module->addVariable(paramNames[i], alloca);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < paramActualTypes.size(); ++i) {
|
||||||
|
Value *argValue = funcArgs[i];
|
||||||
|
builder.createStoreInst(argValue, allocas[i]);
|
||||||
|
}
|
||||||
|
|
||||||
// 在处理函数体之前,创建一个新的基本块作为函数体的实际入口
|
// 在处理函数体之前,创建一个新的基本块作为函数体的实际入口
|
||||||
// 这样 entryBB 就可以在完成初始化后跳转到这里
|
// 这样 entryBB 就可以在完成初始化后跳转到这里
|
||||||
BasicBlock* funcBodyEntry = function->addBasicBlock("funcBodyEntry_" + name);
|
BasicBlock* funcBodyEntry = function->addBasicBlock("funcBodyEntry_" + name);
|
||||||
@@ -902,18 +914,18 @@ std::any SysYIRGenerator::visitCall(SysYParser::CallContext *ctx) {
|
|||||||
|
|
||||||
// 获取形参列表。`getArguments()` 返回的是 `Argument*` 的集合,
|
// 获取形参列表。`getArguments()` 返回的是 `Argument*` 的集合,
|
||||||
// 每个 `Argument` 代表一个函数形参,其 `getType()` 就是指向形参的类型的指针类型。
|
// 每个 `Argument` 代表一个函数形参,其 `getType()` 就是指向形参的类型的指针类型。
|
||||||
auto formalParamsAlloca = function->getEntryBlock()->getArguments();
|
auto formalParams = function->getArguments();
|
||||||
|
|
||||||
// 检查实参和形参数量是否匹配。
|
// 检查实参和形参数量是否匹配。
|
||||||
if (args.size() != formalParamsAlloca.size()) {
|
if (args.size() != formalParams.size()) {
|
||||||
std::cerr << "Error: Function call argument count mismatch for function '" << funcName << "'." << std::endl;
|
std::cerr << "Error: Function call argument count mismatch for function '" << funcName << "'." << std::endl;
|
||||||
assert(false && "Function call argument count mismatch!");
|
assert(false && "Function call argument count mismatch!");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < args.size(); i++) {
|
for (int i = 0; i < args.size(); i++) {
|
||||||
// 形参的类型 (e.g., i32, float, i32*, [10 x i32]*)
|
// 形参的类型 (e.g., i32, float, i32*, [10 x i32]*)
|
||||||
Type* formalParamExpectedValueType = formalParamsAlloca[i]->getType()->as<PointerType>()->getBaseType();
|
Type* formalParamExpectedValueType = formalParams[i]->getType();
|
||||||
// 实参的实际类型 (e.g., i32, float, i32*, [67 x i32]*)
|
// 实参的实际类型 (e.g., i32, float, i32*, [67 x i32]*)
|
||||||
Type* actualArgType = args[i]->getType();
|
Type* actualArgType = args[i]->getType();
|
||||||
// 如果实参类型与形参类型不匹配,则尝试进行类型转换
|
// 如果实参类型与形参类型不匹配,则尝试进行类型转换
|
||||||
if (formalParamExpectedValueType != actualArgType) {
|
if (formalParamExpectedValueType != actualArgType) {
|
||||||
@@ -1422,10 +1434,12 @@ void Utils::createExternalFunction(
|
|||||||
pBuilder->setPosition(entry, entry->end());
|
pBuilder->setPosition(entry, entry->end());
|
||||||
|
|
||||||
for (int i = 0; i < paramTypes.size(); ++i) {
|
for (int i = 0; i < paramTypes.size(); ++i) {
|
||||||
|
auto arg = new Argument(paramTypes[i], function, i, paramNames[i]);
|
||||||
auto alloca = pBuilder->createAllocaInst(
|
auto alloca = pBuilder->createAllocaInst(
|
||||||
Type::getPointerType(paramTypes[i]), {}, paramNames[i]);
|
Type::getPointerType(paramTypes[i]), {}, paramNames[i]);
|
||||||
entry->insertArgument(alloca);
|
function->insertArgument(arg);
|
||||||
// pModule->addVariable(paramNames[i], alloca);
|
auto store = pBuilder->createStoreInst(arg, alloca);
|
||||||
|
pModule->addVariable(paramNames[i], alloca);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ void SysYPrinter::printFunction(Function *function) {
|
|||||||
|
|
||||||
auto entryBlock = function->getEntryBlock();
|
auto entryBlock = function->getEntryBlock();
|
||||||
const auto &args_types = function->getParamTypes();
|
const auto &args_types = function->getParamTypes();
|
||||||
auto &args = entryBlock->getArguments();
|
auto &args = function->getArguments();
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (const auto &args_type : args_types) {
|
for (const auto &args_type : args_types) {
|
||||||
|
|||||||
@@ -468,7 +468,6 @@ public:
|
|||||||
|
|
||||||
// --- End of refactored ConstantValue and related classes ---
|
// --- End of refactored ConstantValue and related classes ---
|
||||||
|
|
||||||
|
|
||||||
class Instruction;
|
class Instruction;
|
||||||
class Function;
|
class Function;
|
||||||
class BasicBlock;
|
class BasicBlock;
|
||||||
@@ -487,7 +486,6 @@ public:
|
|||||||
|
|
||||||
using inst_list = std::list<std::unique_ptr<Instruction>>;
|
using inst_list = std::list<std::unique_ptr<Instruction>>;
|
||||||
using iterator = inst_list::iterator;
|
using iterator = inst_list::iterator;
|
||||||
using arg_list = std::vector<AllocaInst *>;
|
|
||||||
using block_list = std::vector<BasicBlock *>;
|
using block_list = std::vector<BasicBlock *>;
|
||||||
using block_set = std::unordered_set<BasicBlock *>;
|
using block_set = std::unordered_set<BasicBlock *>;
|
||||||
|
|
||||||
@@ -495,7 +493,6 @@ protected:
|
|||||||
|
|
||||||
Function *parent; ///< 从属的函数
|
Function *parent; ///< 从属的函数
|
||||||
inst_list instructions; ///< 拥有的指令序列
|
inst_list instructions; ///< 拥有的指令序列
|
||||||
arg_list arguments; ///< 分配空间后的形式参数列表
|
|
||||||
block_list successors; ///< 前驱列表
|
block_list successors; ///< 前驱列表
|
||||||
block_list predecessors; ///< 后继列表
|
block_list predecessors; ///< 后继列表
|
||||||
bool reachable = false;
|
bool reachable = false;
|
||||||
@@ -515,14 +512,12 @@ public:
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
unsigned getNumInstructions() const { return instructions.size(); }
|
unsigned getNumInstructions() const { return instructions.size(); }
|
||||||
unsigned getNumArguments() const { return arguments.size(); }
|
|
||||||
unsigned getNumPredecessors() const { return predecessors.size(); }
|
unsigned getNumPredecessors() const { return predecessors.size(); }
|
||||||
unsigned getNumSuccessors() const { return successors.size(); }
|
unsigned getNumSuccessors() const { return successors.size(); }
|
||||||
Function* getParent() const { return parent; }
|
Function* getParent() const { return parent; }
|
||||||
void setParent(Function *func) { parent = func; }
|
void setParent(Function *func) { parent = func; }
|
||||||
inst_list& getInstructions() { return instructions; }
|
inst_list& getInstructions() { return instructions; }
|
||||||
auto getInstructions_Range() const { return make_range(instructions); }
|
auto getInstructions_Range() const { return make_range(instructions); }
|
||||||
arg_list& getArguments() { return arguments; }
|
|
||||||
block_list& getPredecessors() { return predecessors; }
|
block_list& getPredecessors() { return predecessors; }
|
||||||
void clearPredecessors() { predecessors.clear(); }
|
void clearPredecessors() { predecessors.clear(); }
|
||||||
block_list& getSuccessors() { return successors; }
|
block_list& getSuccessors() { return successors; }
|
||||||
@@ -530,7 +525,6 @@ public:
|
|||||||
iterator begin() { return instructions.begin(); }
|
iterator begin() { return instructions.begin(); }
|
||||||
iterator end() { return instructions.end(); }
|
iterator end() { return instructions.end(); }
|
||||||
iterator terminator() { return std::prev(end()); }
|
iterator terminator() { return std::prev(end()); }
|
||||||
void insertArgument(AllocaInst *inst) { arguments.push_back(inst); }
|
|
||||||
bool hasSuccessor(BasicBlock *block) const {
|
bool hasSuccessor(BasicBlock *block) const {
|
||||||
return std::find(successors.begin(), successors.end(), block) != successors.end();
|
return std::find(successors.begin(), successors.end(), block) != successors.end();
|
||||||
} ///< 判断是否有后继块
|
} ///< 判断是否有后继块
|
||||||
@@ -1065,6 +1059,8 @@ public:
|
|||||||
}; // class UncondBrInst
|
}; // class UncondBrInst
|
||||||
|
|
||||||
//! Conditional branch
|
//! Conditional branch
|
||||||
|
// 这里的args是指向条件分支的两个分支的参数列表但是现在弃用了
|
||||||
|
// 通过mem2reg优化后,数据流分析将不会由arguments来传递
|
||||||
class CondBrInst : public Instruction {
|
class CondBrInst : public Instruction {
|
||||||
friend class IRBuilder;
|
friend class IRBuilder;
|
||||||
friend class Function;
|
friend class Function;
|
||||||
@@ -1090,17 +1086,17 @@ public:
|
|||||||
BasicBlock* getElseBlock() const {
|
BasicBlock* getElseBlock() const {
|
||||||
return dynamic_cast<BasicBlock *>(getOperand(2));
|
return dynamic_cast<BasicBlock *>(getOperand(2));
|
||||||
}
|
}
|
||||||
auto getThenArguments() const {
|
// auto getThenArguments() const {
|
||||||
auto begin = std::next(operand_begin(), 3);
|
// auto begin = std::next(operand_begin(), 3);
|
||||||
auto end = std::next(begin, getThenBlock()->getNumArguments());
|
// // auto end = std::next(begin, getThenBlock()->getNumArguments());
|
||||||
return make_range(begin, end);
|
// return make_range(begin, end);
|
||||||
}
|
// }
|
||||||
auto getElseArguments() const {
|
// auto getElseArguments() const {
|
||||||
auto begin =
|
// auto begin =
|
||||||
std::next(operand_begin(), 3 + getThenBlock()->getNumArguments());
|
// std::next(operand_begin(), 3 + getThenBlock()->getNumArguments());
|
||||||
auto end = operand_end();
|
// auto end = operand_end();
|
||||||
return make_range(begin, end);
|
// return make_range(begin, end);
|
||||||
}
|
// }
|
||||||
|
|
||||||
}; // class CondBrInst
|
}; // class CondBrInst
|
||||||
|
|
||||||
@@ -1243,6 +1239,21 @@ public:
|
|||||||
class GlobalValue;
|
class GlobalValue;
|
||||||
|
|
||||||
|
|
||||||
|
class Argument : public Value {
|
||||||
|
protected:
|
||||||
|
Function *func;
|
||||||
|
int index;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Argument(Type *type, Function *func, int index, const std::string &name = "")
|
||||||
|
: Value(type, name), func(func), index(index) {}
|
||||||
|
|
||||||
|
public:
|
||||||
|
Function* getParent() const { return func; }
|
||||||
|
int getIndex() const { return index; }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class Module;
|
class Module;
|
||||||
//! Function definitionclass
|
//! Function definitionclass
|
||||||
class Function : public Value {
|
class Function : public Value {
|
||||||
@@ -1254,6 +1265,7 @@ protected:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
using block_list = std::list<std::unique_ptr<BasicBlock>>;
|
using block_list = std::list<std::unique_ptr<BasicBlock>>;
|
||||||
|
using arg_list = std::vector<Argument *>;
|
||||||
enum FunctionAttribute : uint64_t {
|
enum FunctionAttribute : uint64_t {
|
||||||
PlaceHolder = 0x0UL,
|
PlaceHolder = 0x0UL,
|
||||||
Pure = 0x1UL << 0,
|
Pure = 0x1UL << 0,
|
||||||
@@ -1265,6 +1277,7 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
Module *parent; ///< 函数的父模块
|
Module *parent; ///< 函数的父模块
|
||||||
block_list blocks; ///< 函数包含的基本块列表
|
block_list blocks; ///< 函数包含的基本块列表
|
||||||
|
arg_list arguments; ///< 函数参数列表
|
||||||
FunctionAttribute attribute = PlaceHolder; ///< 函数属性
|
FunctionAttribute attribute = PlaceHolder; ///< 函数属性
|
||||||
std::set<Function *> callees; ///< 函数调用的函数集合
|
std::set<Function *> callees; ///< 函数调用的函数集合
|
||||||
public:
|
public:
|
||||||
@@ -1289,6 +1302,16 @@ protected:
|
|||||||
auto getBasicBlocks() { return make_range(blocks); }
|
auto getBasicBlocks() { return make_range(blocks); }
|
||||||
block_list& getBasicBlocks_NoRange() { return blocks; }
|
block_list& getBasicBlocks_NoRange() { return blocks; }
|
||||||
BasicBlock* getEntryBlock() { return blocks.front().get(); }
|
BasicBlock* getEntryBlock() { return blocks.front().get(); }
|
||||||
|
void insertArgument(Argument *arg) { arguments.push_back(arg); }
|
||||||
|
arg_list& getArguments() { return arguments; }
|
||||||
|
unsigned getNumArguments() const { return arguments.size(); }
|
||||||
|
Argument* getArgument(unsigned index) const {
|
||||||
|
assert(index < arguments.size() && "Argument index out of bounds");
|
||||||
|
return arguments[index];
|
||||||
|
} ///< 获取位置为index的参数
|
||||||
|
auto getArgumentsRange() const {
|
||||||
|
return make_range(arguments.begin(), arguments.end());
|
||||||
|
} ///< 获取参数列表的范围
|
||||||
void removeBasicBlock(BasicBlock *blockToRemove) {
|
void removeBasicBlock(BasicBlock *blockToRemove) {
|
||||||
auto is_same_ptr = [blockToRemove](const std::unique_ptr<BasicBlock> &ptr) { return ptr.get() == blockToRemove; };
|
auto is_same_ptr = [blockToRemove](const std::unique_ptr<BasicBlock> &ptr) { return ptr.get() == blockToRemove; };
|
||||||
blocks.remove_if(is_same_ptr);
|
blocks.remove_if(is_same_ptr);
|
||||||
|
|||||||
Reference in New Issue
Block a user