更改前置声明,IR生成更新
This commit is contained in:
@@ -83,6 +83,354 @@ std::any SysYIRGenerator::visitGlobalVarDecl(SysYParser::GlobalVarDeclContext *c
|
|||||||
return std::any();
|
return std::any();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::any SysYIRGenerator::visitConstDecl(SysYParser::ConstDeclContext *ctx){
|
||||||
|
Type* type = std::any_cast<Type *>(visitBType(ctx->bType()));
|
||||||
|
for (const auto constDef : ctx->constDef()) {
|
||||||
|
std::vector<Value *> dims = {};
|
||||||
|
std::string name = constDef->Ident()->getText();
|
||||||
|
auto constExps = constDef->constExp();
|
||||||
|
if (!constExps.empty()) {
|
||||||
|
for (const auto constExp : constExps) {
|
||||||
|
dims.push_back(std::any_cast<Value *>(visitConstExp(constExp)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayValueTree* root = std::any_cast<ArrayValueTree *>(constDef->constInitVal()->accept(this));
|
||||||
|
ValueCounter values;
|
||||||
|
Utils::tree2Array(type, root, dims, dims.size(), values, &builder);
|
||||||
|
delete root;
|
||||||
|
|
||||||
|
module->createConstVar(name, Type::getPointerType(type), values, dims);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx) {
|
||||||
|
Type* type = std::any_cast<Type *>(visitBType(ctx->bType()));
|
||||||
|
for (const auto varDef : ctx->varDef()) {
|
||||||
|
std::vector<Value *> dims = {};
|
||||||
|
std::string name = varDef->Ident()->getText();
|
||||||
|
auto constExps = varDef->constExp();
|
||||||
|
if (!constExps.empty()) {
|
||||||
|
for (const auto &constExp : constExps) {
|
||||||
|
dims.push_back(std::any_cast<Value *>(visitConstExp(constExp)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AllocaInst* alloca =
|
||||||
|
builder.createAllocaInst(Type::getPointerType(type), dims, name);
|
||||||
|
|
||||||
|
if (varDef->initVal() != nullptr) {
|
||||||
|
ValueCounter values;
|
||||||
|
// 这里的varDef->initVal()可能是ScalarInitValue或ArrayInitValue
|
||||||
|
ArrayValueTree* root = std::any_cast<ArrayValueTree *>(varDef->initVal()->accept(this));
|
||||||
|
Utils::tree2Array(type, root, dims, dims.size(), values, &builder);
|
||||||
|
delete root;
|
||||||
|
if (dims.empty()) {
|
||||||
|
builder.createStoreInst(values.getValue(0), alloca);
|
||||||
|
} else {
|
||||||
|
// 对于多维数组,使用memset初始化
|
||||||
|
// 计算每个维度的大小
|
||||||
|
// 这里的values.getNumbers()返回的是每个维度的大小
|
||||||
|
// 这里的values.getValues()返回的是每个维度对应的值
|
||||||
|
// 例如:对于一个二维数组,values.getNumbers()可能是[3, 4],表示3行4列
|
||||||
|
// values.getValues()可能是[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
||||||
|
// 对于每个维度,使用memset将对应的值填充到数组中
|
||||||
|
// 这里的alloca是一个指向数组的指针
|
||||||
|
std::vector<unsigned int> & counterNumbers = values.getNumbers();
|
||||||
|
std::vector<sysy::Value *> & counterValues = values.getValues();
|
||||||
|
unsigned begin = 0;
|
||||||
|
for (size_t i = 0; i < counterNumbers.size(); i++) {
|
||||||
|
|
||||||
|
builder.createMemsetInst(
|
||||||
|
alloca, ConstantValue::get(static_cast<int>(begin)),
|
||||||
|
ConstantValue::get(static_cast<int>(counterNumbers[i])),
|
||||||
|
counterValues[i]);
|
||||||
|
begin += counterNumbers[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
module->addVariable(name, alloca);
|
||||||
|
}
|
||||||
|
return std::any();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::any SysYIRGenerator::visitBType(SysYParser::BTypeContext *ctx) {
|
||||||
|
return ctx->INT() != nullptr ? Type::getIntType() : Type::getFloatType();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::any SysYIRGenerator::visitScalarInitValue(SysYParser::ScalarInitValueContext *ctx) {
|
||||||
|
AllocaInst* alloca = std::any_cast<Value *>(visitExp(ctx->exp()));
|
||||||
|
ArrayValueTree* result = new ArrayValueTree();
|
||||||
|
result->setValue(alloca);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::any SysYIRGenerator::visitArrayInitValue(SysYParser::ArrayInitValueContext *ctx) {
|
||||||
|
std::vector<ArrayValueTree *> children;
|
||||||
|
for (const auto &initVal : ctx->initVal())
|
||||||
|
children.push_back(std::any_cast<ArrayValueTree *>(initVal->accept(this)));
|
||||||
|
ArrayValueTree* result = new ArrayValueTree();
|
||||||
|
result->addChildren(children);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::any SysYIRGenerator::visitConstScalarInitValue(SysYParser::ConstScalarInitValueContext *ctx) {
|
||||||
|
AllocaInst* alloca = std::any_cast<Value *>(visitConstExp(ctx->constExp()));
|
||||||
|
ArrayValueTree* result = new ArrayValueTree();
|
||||||
|
result->setValue(alloca);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::any SysYIRGenerator::visitConstArrayInitValue(SysYParser::ConstArrayInitValueContext *ctx) {
|
||||||
|
std::vector<ArrayValueTree *> children;
|
||||||
|
for (const auto &constInitVal : ctx->constInitVal())
|
||||||
|
children.push_back(std::any_cast<ArrayValueTree *>(constInitVal->accept(this)));
|
||||||
|
ArrayValueTree* result = new ArrayValueTree();
|
||||||
|
result->addChildren(children);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::any SysYIRGenerator::visitFuncType(SysYParser::FuncTypeContext *ctx) {
|
||||||
|
if (ctx->INT() != nullptr)
|
||||||
|
return Type::getIntType();
|
||||||
|
if (ctx->FLOAT() != nullptr)
|
||||||
|
return Type::getFloatType();
|
||||||
|
return Type::getVoidType();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
|
||||||
|
// 更新作用域
|
||||||
|
module->enterNewScope();
|
||||||
|
|
||||||
|
auto name = ctx->Ident()->getText();
|
||||||
|
std::vector<Type *> paramTypes;
|
||||||
|
std::vector<std::string> paramNames;
|
||||||
|
std::vector<std::vector<Value *>> paramDims;
|
||||||
|
|
||||||
|
if (ctx->funcFParams() != nullptr) {
|
||||||
|
auto params = ctx->funcFParams()->funcFParam();
|
||||||
|
for (const auto ¶m : params) {
|
||||||
|
paramTypes.push_back(std::any_cast<Type *>(visitBType(param->bType())));
|
||||||
|
paramNames.push_back(param->Ident()->getText());
|
||||||
|
std::vector<Value *> dims = {};
|
||||||
|
if (param->exp() != nullptr) {
|
||||||
|
dims.push_back(ConstantValue::get(-1)); // 第一个维度不确定
|
||||||
|
for (const auto &exp : param->exp()) {
|
||||||
|
dims.push_back(std::any_cast<Value *>(visitExp(exp)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
paramDims.emplace_back(dims);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Type *returnType = std::any_cast<Type *>(visitFuncType(ctx->funcType()));
|
||||||
|
FunctionType* funcType = Type::getFunctionType(returnType, paramTypes);
|
||||||
|
Function* function = module->createFunction(name, funcType);
|
||||||
|
BasicBlock* entry = function->getEntryBlock();
|
||||||
|
builder.setPosition(entry, entry->end());
|
||||||
|
|
||||||
|
for (size_t i = 0; i < paramTypes.size(); ++i) {
|
||||||
|
AllocaInst* alloca = builder.createAllocaInst(Type::getPointerType(paramTypes[i]),
|
||||||
|
paramDims[i], paramNames[i]);
|
||||||
|
entry->insertArgument(alloca);
|
||||||
|
module->addVariable(paramNames[i], alloca);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto item : ctx->blockStmt()->blockItem()) {
|
||||||
|
visitBlockItem(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
module->leaveScope();
|
||||||
|
|
||||||
|
return std::any;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::any SysYIRGenerator::visitBlockStmt(SysYParser::BlockStmtContext *ctx) {
|
||||||
|
module->enterNewScope();
|
||||||
|
for (auto item : ctx->blockItem())
|
||||||
|
visitBlockItem(item);
|
||||||
|
module->leaveScope();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext *ctx) {
|
||||||
|
auto lVal = ctx->lValue();
|
||||||
|
std::string name = lVal->Ident()->getText();
|
||||||
|
std::vector<Value *> dims;
|
||||||
|
for (const auto &exp : lVal->exp()) {
|
||||||
|
dims.push_back(std::any_cast<Value *>(visitExp(exp)));
|
||||||
|
}
|
||||||
|
|
||||||
|
User* variable = module->getVariable(name);
|
||||||
|
Value* value = std::any_cast<Value *>(visitExp(ctx->exp()));
|
||||||
|
PointerType* variableType =dynamic_cast<PointerType *>(variable->getType())->getBaseType();
|
||||||
|
|
||||||
|
// 左值右值类型不同处理
|
||||||
|
if (variableType != value->getType()) {
|
||||||
|
ConstantValue * constValue = dynamic_cast<ConstantValue *>(value);
|
||||||
|
if (constValue != nullptr) {
|
||||||
|
if (variableType == Type::getFloatType()) {
|
||||||
|
value = ConstantValue::get(static_cast<float>(constValue->getInt()));
|
||||||
|
} else {
|
||||||
|
value = ConstantValue::get(static_cast<int>(constValue->getFloat()));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (variableType == Type::getFloatType()) {
|
||||||
|
value = builder.createIToFInst(value);
|
||||||
|
} else {
|
||||||
|
value = builder.createFtoIInst(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
builder.createStoreInst(value, variable, dims, variable->getName());
|
||||||
|
|
||||||
|
return std::any();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::any SysYIRGenerator::visitIfStmt(SysYParser::IfStmtContext *ctx) {
|
||||||
|
// labels string stream
|
||||||
|
|
||||||
|
std::stringstream labelstring;
|
||||||
|
Function * function = builder.getBasicBlock()->getParent();
|
||||||
|
|
||||||
|
BasicBlock* thenBlock = new BasicBlock(function);
|
||||||
|
BasicBlock* exitBlock = new BasicBlock(function);
|
||||||
|
|
||||||
|
if (ctx->stmt().size() > 1) {
|
||||||
|
BasicBlock* elseBlock = new BasicBlock(function);
|
||||||
|
|
||||||
|
builder.pushTrueBlock(thenBlock);
|
||||||
|
builder.pushFalseBlock(elseBlock);
|
||||||
|
// 访问条件表达式
|
||||||
|
visitCond(ctx->cond());
|
||||||
|
builder.popTrueBlock();
|
||||||
|
builder.popFalseBlock();
|
||||||
|
|
||||||
|
labelstring << "then.L" << builder.getLabelIndex();
|
||||||
|
thenBlock->setName(labelstring.str());
|
||||||
|
labelstring.str("");
|
||||||
|
function->addBasicBlock(thenBlock);
|
||||||
|
builder.setPosition(thenBlock, thenBlock->end());
|
||||||
|
|
||||||
|
auto block = dynamic_cast<SysYParser::BlockStmtContext *>(ctx->stmt(0));
|
||||||
|
// 如果是块语句,直接访问
|
||||||
|
// 否则访问语句
|
||||||
|
if (block != nullptr) {
|
||||||
|
visitBlockStmt(block);
|
||||||
|
} else {
|
||||||
|
module->enterNewScope();
|
||||||
|
ctx->stmt(0)->accept(this);
|
||||||
|
module->leaveScope();
|
||||||
|
}
|
||||||
|
builder.createUncondBrInst(exitBlock, {});
|
||||||
|
BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock);
|
||||||
|
|
||||||
|
labelstring << "else.L" << builder.getLabelIndex();
|
||||||
|
elseBlock->setName(labelstring.str());
|
||||||
|
labelstring.str("");
|
||||||
|
function->addBasicBlock(elseBlock);
|
||||||
|
builder.setPosition(elseBlock, elseBlock->end());
|
||||||
|
|
||||||
|
block = dynamic_cast<SysYParser::BlockStmtContext *>(ctx->stmt(1));
|
||||||
|
if (block != nullptr) {
|
||||||
|
visitBlockStmt(block);
|
||||||
|
} else {
|
||||||
|
module->enterNewScope();
|
||||||
|
ctx->stmt(1)->accept(this);
|
||||||
|
module->leaveScope();
|
||||||
|
}
|
||||||
|
BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock);
|
||||||
|
|
||||||
|
labelstring << "exit.L" << builder.getLabelIndex();
|
||||||
|
exitBlock->setName(labelstring.str());
|
||||||
|
labelstring.str("");
|
||||||
|
function->addBasicBlock(exitBlock);
|
||||||
|
builder.setPosition(exitBlock, exitBlock->end());
|
||||||
|
|
||||||
|
} else {
|
||||||
|
builder.pushTrueBlock(thenBlock);
|
||||||
|
builder.pushFalseBlock(exitBlock);
|
||||||
|
visitCond(ctx->cond());
|
||||||
|
builder.popTrueBlock();
|
||||||
|
builder.popFalseBlock();
|
||||||
|
|
||||||
|
labelstring << "then.L" << builder.getLabelIndex();
|
||||||
|
thenBlock->setName(labelstring.str());
|
||||||
|
labelstring.str("");
|
||||||
|
function->addBasicBlock(thenBlock);
|
||||||
|
builder.setPosition(thenBlock, thenBlock->end());
|
||||||
|
|
||||||
|
auto block = dynamic_cast<SysYParser::BlockStmtContext *>(ctx->stmt(0));
|
||||||
|
if (block != nullptr) {
|
||||||
|
visitBlockStmt(block);
|
||||||
|
} else {
|
||||||
|
module->enterNewScope();
|
||||||
|
ctx->stmt(0)->accept(this);
|
||||||
|
module->leaveScope();
|
||||||
|
}
|
||||||
|
BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock);
|
||||||
|
|
||||||
|
labelstring << "exit.L" << builder.getLabelIndex();
|
||||||
|
exitBlock->setName(labelstring.str());
|
||||||
|
labelstring.str("");
|
||||||
|
function->addBasicBlock(exitBlock);
|
||||||
|
builder.setPosition(exitBlock, exitBlock->end());
|
||||||
|
}
|
||||||
|
return std::any();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::any SysYIRGenerator::visitWhileStmt(SysYParser::WhileStmtContext *ctx) {
|
||||||
|
|
||||||
|
BasicBlock* curBlock = builder.getBasicBlock();
|
||||||
|
Function* function = builder.getBasicBlock()->getParent();
|
||||||
|
|
||||||
|
std::stringstream labelstring;
|
||||||
|
labelstring << "head.L" << builder.getLabelIndex();
|
||||||
|
BasicBlock *headBlock = function->addBasicBlock(labelstring.str());
|
||||||
|
labelstring.str("");
|
||||||
|
|
||||||
|
BasicBlock::conectBlocks(curBlock, headBlock);
|
||||||
|
builder.setPosition(headBlock, headBlock->end())
|
||||||
|
|
||||||
|
function->addBasicBlock(condBlock);
|
||||||
|
builder.setPosition(condBlock, condBlock->end());
|
||||||
|
|
||||||
|
builder.pushTrueBlock(bodyBlock);
|
||||||
|
builder.pushFalseBlock(exitBlock);
|
||||||
|
|
||||||
|
visitCond(ctx->cond());
|
||||||
|
|
||||||
|
builder.popTrueBlock();
|
||||||
|
builder.popFalseBlock();
|
||||||
|
|
||||||
|
labelstring << "body.L" << builder.getLabelIndex();
|
||||||
|
bodyBlock->setName(labelstring.str());
|
||||||
|
labelstring.str("");
|
||||||
|
function->addBasicBlock(bodyBlock);
|
||||||
|
builder.setPosition(bodyBlock, bodyBlock->end());
|
||||||
|
|
||||||
|
module->enterNewScope();
|
||||||
|
for (auto item : ctx->blockStmt()->blockItem()) {
|
||||||
|
visitBlockItem(item);
|
||||||
|
}
|
||||||
|
module->leaveScope();
|
||||||
|
|
||||||
|
builder.createUncondBrInst(condBlock, {});
|
||||||
|
|
||||||
|
BasicBlock::conectBlocks(builder.getBasicBlock(), condBlock);
|
||||||
|
|
||||||
|
labelstring << "exit.L" << builder.getLabelIndex();
|
||||||
|
exitBlock->setName(labelstring.str());
|
||||||
|
labelstring.str("");
|
||||||
|
|
||||||
|
function->addBasicBlock(exitBlock);
|
||||||
|
|
||||||
|
builder.setPosition(exitBlock, exitBlock->end());
|
||||||
|
|
||||||
|
return std::any();
|
||||||
|
}
|
||||||
|
|
||||||
void Utils::tree2Array(Type *type, ArrayValueTree *root,
|
void Utils::tree2Array(Type *type, ArrayValueTree *root,
|
||||||
const std::vector<Value *> &dims, unsigned numDims,
|
const std::vector<Value *> &dims, unsigned numDims,
|
||||||
|
|||||||
386
src/include/IR.h
386
src/include/IR.h
@@ -58,23 +58,23 @@ class Type {
|
|||||||
virtual ~Type() = default;
|
virtual ~Type() = default;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static auto getIntType() -> Type *; ///< 返回表示Int类型的Type指针
|
static Type* getIntType(); ///< 返回表示Int类型的Type指针
|
||||||
static auto getFloatType() -> Type *; ///< 返回表示Float类型的Type指针
|
static Type* getFloatType(); ///< 返回表示Float类型的Type指针
|
||||||
static auto getVoidType() -> Type *; ///< 返回表示Void类型的Type指针
|
static Type* getVoidType(); ///< 返回表示Void类型的Type指针
|
||||||
static auto getLabelType() -> Type *; ///< 返回表示Label类型的Type指针
|
static Type* getLabelType(); ///< 返回表示Label类型的Type指针
|
||||||
static auto getPointerType(Type *baseType) -> Type *; ///< 返回表示指向baseType类型的Pointer类型的Type指针
|
static Type* getPointerType(Type *baseType); ///< 返回表示指向baseType类型的Pointer类型的Type指针
|
||||||
static auto getFunctionType(Type *returnType, const std::vector<Type *> ¶mTypes = {}) -> Type *;
|
static Type* getFunctionType(Type *returnType, const std::vector<Type *> ¶mTypes = {});
|
||||||
///< 返回表示返回类型为returnType,形参类型列表为paramTypes的函数类型的Type指针
|
///< 返回表示返回类型为returnType,形参类型列表为paramTypes的函数类型的Type指针
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto getKind() const -> Kind { return kind; } ///< 返回Type对象代表原始标量类型
|
Kind getKind() const { return kind; } ///< 返回Type对象代表原始标量类型
|
||||||
auto isInt() const -> bool { return kind == kInt; } ///< 判定是否为Int类型
|
bool isInt() const { return kind == kInt; } ///< 判定是否为Int类型
|
||||||
auto isFloat() const -> bool { return kind == kFloat; } ///< 判定是否为Float类型
|
bool isFloat() const { return kind == kFloat; } ///< 判定是否为Float类型
|
||||||
auto isVoid() const -> bool { return kind == kVoid; } ///< 判定是否为Void类型
|
bool isVoid() const { return kind == kVoid; } ///< 判定是否为Void类型
|
||||||
auto isLabel() const -> bool { return kind == kLabel; } ///< 判定是否为Label类型
|
bool isLabel() const { return kind == kLabel; } ///< 判定是否为Label类型
|
||||||
auto isPointer() const -> bool { return kind == kPointer; } ///< 判定是否为Pointer类型
|
bool isPointer() const { return kind == kPointer; } ///< 判定是否为Pointer类型
|
||||||
auto isFunction() const -> bool { return kind == kFunction; } ///< 判定是否为Function类型
|
bool isFunction() const { return kind == kFunction; } ///< 判定是否为Function类型
|
||||||
auto getSize() const -> unsigned; ///< 返回类型所占的空间大小(字节)
|
unsigned getSize() const; ///< 返回类型所占的空间大小(字节)
|
||||||
/// 尝试将一个变量转换为给定的Type及其派生类类型的变量
|
/// 尝试将一个变量转换为给定的Type及其派生类类型的变量
|
||||||
template <typename T>
|
template <typename T>
|
||||||
auto as() const -> std::enable_if_t<std::is_base_of_v<Type, T>, T *> {
|
auto as() const -> std::enable_if_t<std::is_base_of_v<Type, T>, T *> {
|
||||||
@@ -90,10 +90,10 @@ class PointerType : public Type {
|
|||||||
explicit PointerType(Type *baseType) : Type(kPointer), baseType(baseType) {}
|
explicit PointerType(Type *baseType) : Type(kPointer), baseType(baseType) {}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static auto get(Type *baseType) -> PointerType *; ///< 获取指向baseType的Pointer类型
|
static PointerType* get(Type *baseType); ///< 获取指向baseType的Pointer类型
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto getBaseType() const -> Type * { return baseType; } ///< 获取指向的类型
|
Type* getBaseType() const { return baseType; } ///< 获取指向的类型
|
||||||
};
|
};
|
||||||
|
|
||||||
class FunctionType : public Type {
|
class FunctionType : public Type {
|
||||||
@@ -107,12 +107,12 @@ class FunctionType : public Type {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
/// 获取返回值类型为returnType, 形参类型列表为paramTypes的Function类型
|
/// 获取返回值类型为returnType, 形参类型列表为paramTypes的Function类型
|
||||||
static auto get(Type *returnType, const std::vector<Type *> ¶mTypes = {}) -> FunctionType *;
|
static FunctionType* get(Type *returnType, const std::vector<Type *> ¶mTypes = {});
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto getReturnType() const -> Type * { return returnType; } ///< 获取返回值类信息
|
Type* getReturnType() const { return returnType; } ///< 获取返回值类信息
|
||||||
auto getParamTypes() const { return make_range(paramTypes); } ///< 获取形参类型列表
|
auto getParamTypes() const { return make_range(paramTypes); } ///< 获取形参类型列表
|
||||||
auto getNumParams() const -> unsigned { return paramTypes.size(); } ///< 获取形参数量
|
unsigned getNumParams() const { return paramTypes.size(); } ///< 获取形参数量
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -182,9 +182,9 @@ class Use {
|
|||||||
Use(unsigned index, User *user, Value *value) : index(index), user(user), value(value) {}
|
Use(unsigned index, User *user, Value *value) : index(index), user(user), value(value) {}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto getIndex() const -> unsigned { return index; } ///< 返回value在User操作数中的位置
|
unsigned getIndex() const { return index; } ///< 返回value在User操作数中的位置
|
||||||
auto getUser() const -> User * { return user; } ///< 返回使用者
|
User* getUser() const { return user; } ///< 返回使用者
|
||||||
auto getValue() const -> Value * { return value; } ///< 返回被使用的值
|
Value* getValue() const { return value; } ///< 返回被使用的值
|
||||||
void setValue(Value *newValue) { value = newValue; } ///< 将被使用的值设置为newValue
|
void setValue(Value *newValue) { value = newValue; } ///< 将被使用的值设置为newValue
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -202,19 +202,17 @@ class Value {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
void setName(const std::string &newName) { name = newName; } ///< 设置名字
|
void setName(const std::string &newName) { name = newName; } ///< 设置名字
|
||||||
auto getName() const -> const std::string & { return name; } ///< 获取名字
|
const std::string& getName() const { return name; } ///< 获取名字
|
||||||
auto getType() const -> Type * { return type; } ///< 返回值的类型
|
Type* getType() const { return type; } ///< 返回值的类型
|
||||||
auto isInt() const -> bool { return type->isInt(); } ///< 判定是否为Int类型
|
bool isInt() const { return type->isInt(); } ///< 判定是否为Int类型
|
||||||
auto isFloat() const -> bool { return type->isFloat(); } ///< 判定是否为Float类型
|
bool isFloat() const { return type->isFloat(); } ///< 判定是否为Float类型
|
||||||
auto isPointer() const -> bool { return type->isPointer(); } ///< 判定是否为Pointer类型
|
bool isPointer() const { return type->isPointer(); } ///< 判定是否为Pointer类型
|
||||||
auto getUses() -> std::list<std::shared_ptr<Use>> & { return uses; } ///< 获取使用关系列表
|
std::list<std::shared_ptr<Use>>& getUses() { return uses; } ///< 获取使用关系列表
|
||||||
void addUse(const std::shared_ptr<Use> &use) { uses.push_back(use); } ///< 添加使用关系
|
void addUse(const std::shared_ptr<Use> &use) { uses.push_back(use); } ///< 添加使用关系
|
||||||
void replaceAllUsesWith(Value *value); ///< 将原来使用该value的使用者全变为使用给定参数value并修改相应use关系
|
void replaceAllUsesWith(Value *value); ///< 将原来使用该value的使用者全变为使用给定参数value并修改相应use关系
|
||||||
void removeUse(const std::shared_ptr<Use> &use) { uses.remove(use); } ///< 删除使用关系use
|
void removeUse(const std::shared_ptr<Use> &use) { uses.remove(use); } ///< 删除使用关系use
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ValueCounter 需要理解为一个Value *的计数器。
|
* ValueCounter 需要理解为一个Value *的计数器。
|
||||||
* 它的主要目的是为了节省存储空间和方便Memset指令的创建。
|
* 它的主要目的是为了节省存储空间和方便Memset指令的创建。
|
||||||
@@ -236,8 +234,8 @@ class ValueCounter {
|
|||||||
ValueCounter() = default;
|
ValueCounter() = default;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto size() const -> unsigned { return __size; } ///< 返回总的Value数量
|
unsigned size() const { return __size; } ///< 返回总的Value数量
|
||||||
auto getValue(unsigned index) const -> Value * {
|
Value* getValue(unsigned index) const {
|
||||||
if (index >= __size) {
|
if (index >= __size) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -252,8 +250,8 @@ class ValueCounter {
|
|||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
} ///< 根据位置index获取Value *
|
} ///< 根据位置index获取Value *
|
||||||
auto getValues() const -> const std::vector<Value *> & { return __counterValues; } ///< 获取互异Value *列表
|
const std::vector<Value *>& getValues() const { return __counterValues; } ///< 获取互异Value *列表
|
||||||
auto getNumbers() const -> const std::vector<unsigned> & { return __counterNumbers; } ///< 获取Value *重复数量列表
|
const std::vector<unsigned>& getNumbers() const { return __counterNumbers; } ///< 获取Value *重复数量列表
|
||||||
void push_back(Value *value, unsigned num = 1) {
|
void push_back(Value *value, unsigned num = 1) {
|
||||||
if (__size != 0 && __counterValues.back() == value) {
|
if (__size != 0 && __counterValues.back() == value) {
|
||||||
*(__counterNumbers.end() - 1) += num;
|
*(__counterNumbers.end() - 1) += num;
|
||||||
@@ -293,20 +291,20 @@ class ConstantValue : public Value {
|
|||||||
: Value(Type::getFloatType(), name), fScalar(value) {}
|
: Value(Type::getFloatType(), name), fScalar(value) {}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static auto get(int value) -> ConstantValue *; ///< 获取一个int类型的ConstValue *,其值为value
|
static ConstantValue* get(int value); ///< 获取一个int类型的ConstValue *,其值为value
|
||||||
static auto get(float value) -> ConstantValue *; ///< 获取一个float类型的ConstValue *,其值为value
|
static ConstantValue* get(float value); ///< 获取一个float类型的ConstValue *,其值为value
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto getInt() const -> int {
|
int getInt() const {
|
||||||
assert(isInt());
|
assert(isInt());
|
||||||
return iScalar;
|
return iScalar;
|
||||||
} ///< 返回int类型的值
|
} ///< 返回int类型的值
|
||||||
auto getFloat() const -> float {
|
float getFloat() const {
|
||||||
assert(isFloat());
|
assert(isFloat());
|
||||||
return fScalar;
|
return fScalar;
|
||||||
} ///< 返回float类型的值
|
} ///< 返回float类型的值
|
||||||
template <typename T>
|
template <typename T>
|
||||||
auto getValue() const -> T {
|
T getValue() const {
|
||||||
if (std::is_same<T, int>::value && isInt()) {
|
if (std::is_same<T, int>::value && isInt()) {
|
||||||
return getInt();
|
return getInt();
|
||||||
}
|
}
|
||||||
@@ -369,23 +367,23 @@ class BasicBlock;
|
|||||||
} ///< 基本块的析构函数,同时删除其前驱后继关系
|
} ///< 基本块的析构函数,同时删除其前驱后继关系
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto getNumInstructions() const -> unsigned { return instructions.size(); } ///< 获取指令数量
|
unsigned getNumInstructions() const { return instructions.size(); } ///< 获取指令数量
|
||||||
auto getNumArguments() const -> unsigned { return arguments.size(); } ///< 获取形式参数数量
|
unsigned getNumArguments() const { return arguments.size(); } ///< 获取形式参数数量
|
||||||
auto getNumPredecessors() const -> unsigned { return predecessors.size(); } ///< 获取前驱数量
|
unsigned getNumPredecessors() const { return predecessors.size(); } ///< 获取前驱数量
|
||||||
auto getNumSuccessors() const -> unsigned { return successors.size(); } ///< 获取后继数量
|
unsigned getNumSuccessors() const { return successors.size(); } ///< 获取后继数量
|
||||||
auto getParent() const -> Function * { return parent; } ///< 获取父函数
|
Function* getParent() const { return parent; } ///< 获取父函数
|
||||||
void setParent(Function *func) { parent = func; } ///< 设置父函数
|
void setParent(Function *func) { parent = func; } ///< 设置父函数
|
||||||
auto getInstructions() -> inst_list & { return instructions; } ///< 获取指令列表
|
inst_list& getInstructions() { return instructions; } ///< 获取指令列表
|
||||||
auto getArguments() -> arg_list & { return arguments; } ///< 获取分配空间后的形式参数列表
|
arg_list& getArguments() { return arguments; } ///< 获取分配空间后的形式参数列表
|
||||||
auto getPredecessors() const -> const block_list & { return predecessors; } ///< 获取前驱列表
|
const block_list& getPredecessors() const { return predecessors; } ///< 获取前驱列表
|
||||||
auto getSuccessors() -> block_list & { return successors; } ///< 获取后继列表
|
block_list& getSuccessors() { return successors; } ///< 获取后继列表
|
||||||
auto getDominants() -> block_set & { return dominants; }
|
block_set& getDominants() { return dominants; }
|
||||||
auto getIdom() -> BasicBlock * { return idom; }
|
BasicBlock* getIdom() { return idom; }
|
||||||
auto getSdoms() -> block_list & { return sdoms; }
|
block_list& getSdoms() { return sdoms; }
|
||||||
auto getDFs() -> block_set & { return dominant_frontiers; }
|
block_set& getDFs() { return dominant_frontiers; }
|
||||||
auto begin() -> iterator { return instructions.begin(); } ///< 返回指向指令列表开头的迭代器
|
iterator begin() { return instructions.begin(); } ///< 返回指向指令列表开头的迭代器
|
||||||
auto end() -> iterator { return instructions.end(); } ///< 返回指向指令列表末尾的迭代器
|
iterator end() { return instructions.end(); } ///< 返回指向指令列表末尾的迭代器
|
||||||
auto terminator() -> iterator { return std::prev(end()); } ///< 基本块最后的IR
|
iterator terminator() { return std::prev(end()); } ///< 基本块最后的IR
|
||||||
void insertArgument(AllocaInst *inst) { arguments.push_back(inst); } ///< 插入分配空间后的形式参数
|
void insertArgument(AllocaInst *inst) { arguments.push_back(inst); } ///< 插入分配空间后的形式参数
|
||||||
void addPredecessor(BasicBlock *block) {
|
void addPredecessor(BasicBlock *block) {
|
||||||
if (std::find(predecessors.begin(), predecessors.end(), block) == predecessors.end()) {
|
if (std::find(predecessors.begin(), predecessors.end(), block) == predecessors.end()) {
|
||||||
@@ -411,18 +409,18 @@ class BasicBlock;
|
|||||||
void addSdoms(BasicBlock *block) { sdoms.push_back(block); }
|
void addSdoms(BasicBlock *block) { sdoms.push_back(block); }
|
||||||
void clearSdoms() { sdoms.clear(); }
|
void clearSdoms() { sdoms.clear(); }
|
||||||
// 重载1,参数为 BasicBlock*
|
// 重载1,参数为 BasicBlock*
|
||||||
auto addDominants(BasicBlock *block) -> void { dominants.emplace(block); }
|
void addDominants(BasicBlock *block) { dominants.emplace(block); }
|
||||||
// 重载2,参数为 block_set
|
// 重载2,参数为 block_set
|
||||||
auto addDominants(const block_set &blocks) -> void { dominants.insert(blocks.begin(), blocks.end()); }
|
void addDominants(const block_set &blocks) { dominants.insert(blocks.begin(), blocks.end()); }
|
||||||
auto setDominants(BasicBlock *block) -> void {
|
void setDominants(BasicBlock *block) {
|
||||||
dominants.clear();
|
dominants.clear();
|
||||||
addDominants(block);
|
addDominants(block);
|
||||||
}
|
}
|
||||||
auto setDominants(const block_set &doms) -> void {
|
void setDominants(const block_set &doms) {
|
||||||
dominants.clear();
|
dominants.clear();
|
||||||
addDominants(doms);
|
addDominants(doms);
|
||||||
}
|
}
|
||||||
auto setDFs(const block_set &df) -> void {
|
void setDFs(const block_set &df) {
|
||||||
dominant_frontiers.clear();
|
dominant_frontiers.clear();
|
||||||
for (auto elem : df) {
|
for (auto elem : df) {
|
||||||
dominant_frontiers.emplace(elem);
|
dominant_frontiers.emplace(elem);
|
||||||
@@ -453,7 +451,7 @@ class BasicBlock;
|
|||||||
}
|
}
|
||||||
} ///< 替换前驱
|
} ///< 替换前驱
|
||||||
// 获取支配树中该块的所有子节点,包括子节点的子节点等,迭代实现
|
// 获取支配树中该块的所有子节点,包括子节点的子节点等,迭代实现
|
||||||
auto getChildren() -> block_list {
|
block_list getChildren() {
|
||||||
std::queue<BasicBlock *> q;
|
std::queue<BasicBlock *> q;
|
||||||
block_list children;
|
block_list children;
|
||||||
for (auto sdom : sdoms) {
|
for (auto sdom : sdoms) {
|
||||||
@@ -472,22 +470,20 @@ class BasicBlock;
|
|||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto setreachableTrue() -> void { reachable = true; } ///< 设置可达
|
void setreachableTrue() { reachable = true; } ///< 设置可达
|
||||||
|
void setreachableFalse() { reachable = false; } ///< 设置不可达
|
||||||
auto setreachableFalse() -> void { reachable = false; } ///< 设置不可达
|
bool getreachable() { return reachable; } ///< 返回可达状态
|
||||||
|
|
||||||
auto getreachable() -> bool { return reachable; } ///< 返回可达状态
|
|
||||||
|
|
||||||
static void conectBlocks(BasicBlock *prev, BasicBlock *next) {
|
static void conectBlocks(BasicBlock *prev, BasicBlock *next) {
|
||||||
prev->addSuccessor(next);
|
prev->addSuccessor(next);
|
||||||
next->addPredecessor(prev);
|
next->addPredecessor(prev);
|
||||||
} ///< 连接两个块,即设置两个基本块的前驱后继关系
|
} ///< 连接两个块,即设置两个基本块的前驱后继关系
|
||||||
void setLoop(Loop *loop2set) { loopbelong = loop2set; } ///< 设置所属循环
|
void setLoop(Loop *loop2set) { loopbelong = loop2set; } ///< 设置所属循环
|
||||||
auto getLoop() { return loopbelong; } ///< 获得所属循环
|
Loop* getLoop() { return loopbelong; } ///< 获得所属循环
|
||||||
void setLoopDepth(int loopdepth2set) { loopdepth = loopdepth2set; } ///< 设置循环深度
|
void setLoopDepth(int loopdepth2set) { loopdepth = loopdepth2set; } ///< 设置循环深度
|
||||||
auto getLoopDepth() { return loopdepth; } ///< 获得其在循环的深度
|
int getLoopDepth() { return loopdepth; } ///< 获得其在循环的深度
|
||||||
void removeInst(iterator pos) { instructions.erase(pos); } ///< 删除指令
|
void removeInst(iterator pos) { instructions.erase(pos); } ///< 删除指令
|
||||||
auto moveInst(iterator sourcePos, iterator targetPos, BasicBlock *block) -> iterator; ///< 移动指令
|
iterator moveInst(iterator sourcePos, iterator targetPos, BasicBlock *block); ///< 移动指令
|
||||||
};
|
};
|
||||||
|
|
||||||
//! User is the abstract base type of `Value` types which use other `Value` as
|
//! User is the abstract base type of `Value` types which use other `Value` as
|
||||||
@@ -504,11 +500,11 @@ class User : public Value {
|
|||||||
explicit User(Type *type, const std::string &name = "") : Value(type, name) {}
|
explicit User(Type *type, const std::string &name = "") : Value(type, name) {}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto getNumOperands() const -> unsigned { return operands.size(); } ///< 获取操作数数量
|
unsigned getNumOperands() const { return operands.size(); } ///< 获取操作数数量
|
||||||
auto operand_begin() const { return operands.begin(); } ///< 返回操作数列表的开头迭代器
|
auto operand_begin() const { return operands.begin(); } ///< 返回操作数列表的开头迭代器
|
||||||
auto operand_end() const { return operands.end(); } ///< 返回操作数列表的结尾迭代器
|
auto operand_end() const { return operands.end(); } ///< 返回操作数列表的结尾迭代器
|
||||||
auto getOperands() const { return make_range(operand_begin(), operand_end()); } ///< 获取操作数列表
|
auto getOperands() const { return make_range(operand_begin(), operand_end()); } ///< 获取操作数列表
|
||||||
auto getOperand(unsigned index) const -> Value * { return operands[index]->getValue(); } ///< 获取位置为index的操作数
|
Value* getOperand(unsigned index) const { return operands[index]->getValue(); } ///< 获取位置为index的操作数
|
||||||
void addOperand(Value *value) {
|
void addOperand(Value *value) {
|
||||||
operands.emplace_back(std::make_shared<Use>(operands.size(), this, value));
|
operands.emplace_back(std::make_shared<Use>(operands.size(), this, value));
|
||||||
value->addUse(operands.back());
|
value->addUse(operands.back());
|
||||||
@@ -528,8 +524,6 @@ class User : public Value {
|
|||||||
void setOperand(unsigned index, Value *value); ///< 设置操作数
|
void setOperand(unsigned index, Value *value); ///< 设置操作数
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class GetSubArrayInst;
|
class GetSubArrayInst;
|
||||||
/**
|
/**
|
||||||
* 左值 具有地址的对象
|
* 左值 具有地址的对象
|
||||||
@@ -551,11 +545,11 @@ class LVal {
|
|||||||
virtual unsigned getLValNumDims() const = 0; ///< 获取左值的维度数量
|
virtual unsigned getLValNumDims() const = 0; ///< 获取左值的维度数量
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto getFatherLVal() const -> LVal * { return fatherLVal; } ///< 获取父左值
|
LVal* getFatherLVal() const { return fatherLVal; } ///< 获取父左值
|
||||||
auto getChildrenLVals() const -> const std::list<std::unique_ptr<LVal>> & {
|
const std::list<std::unique_ptr<LVal>>& getChildrenLVals() const {
|
||||||
return childrenLVals;
|
return childrenLVals;
|
||||||
} ///< 获取子左值列表
|
} ///< 获取子左值列表
|
||||||
auto getAncestorLVal() const -> LVal * {
|
LVal* getAncestorLVal() const {
|
||||||
auto curLVal = const_cast<LVal *>(this);
|
auto curLVal = const_cast<LVal *>(this);
|
||||||
while (curLVal->getFatherLVal() != nullptr) {
|
while (curLVal->getFatherLVal() != nullptr) {
|
||||||
curLVal = curLVal->getFatherLVal();
|
curLVal = curLVal->getFatherLVal();
|
||||||
@@ -570,7 +564,7 @@ class LVal {
|
|||||||
[child](const std::unique_ptr<LVal> &ptr) { return ptr.get() == child; });
|
[child](const std::unique_ptr<LVal> &ptr) { return ptr.get() == child; });
|
||||||
childrenLVals.erase(iter);
|
childrenLVals.erase(iter);
|
||||||
} ///< 移除子左值
|
} ///< 移除子左值
|
||||||
auto getDefineInst() const -> GetSubArrayInst * { return defineInst; } ///< 获取定义指令
|
GetSubArrayInst* getDefineInst() const { return defineInst; } ///< 获取定义指令
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -734,8 +728,8 @@ public:
|
|||||||
}
|
}
|
||||||
} ///< 根据指令标识码获取字符串
|
} ///< 根据指令标识码获取字符串
|
||||||
|
|
||||||
BasicBlock *getParent() const { return parent; }
|
BasicBlock* getParent() const { return parent; }
|
||||||
Function *getFunction() const { return parent->getParent(); }
|
Function* getFunction() const { return parent->getParent(); }
|
||||||
void setParent(BasicBlock *bb) { parent = bb; }
|
void setParent(BasicBlock *bb) { parent = bb; }
|
||||||
|
|
||||||
bool isBinary() const {
|
bool isBinary() const {
|
||||||
@@ -809,10 +803,10 @@ class LaInst : public Instruction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto getNumIndices() const -> unsigned { return getNumOperands() - 1; } ///< 获取索引长度
|
unsigned getNumIndices() const { return getNumOperands() - 1; } ///< 获取索引长度
|
||||||
auto getPointer() const -> Value * { return getOperand(0); } ///< 获取目标变量的Value指针
|
Value* getPointer() const { return getOperand(0); } ///< 获取目标变量的Value指针
|
||||||
auto getIndices() const { return make_range(std::next(operand_begin()), operand_end()); } ///< 获取索引列表
|
auto getIndices() const { return make_range(std::next(operand_begin()), operand_end()); } ///< 获取索引列表
|
||||||
auto getIndex(unsigned index) const -> Value * { return getOperand(index + 1); } ///< 获取位置为index的索引分量
|
Value* getIndex(unsigned index) const { return getOperand(index + 1); } ///< 获取位置为index的索引分量
|
||||||
};
|
};
|
||||||
|
|
||||||
class PhiInst : public Instruction {
|
class PhiInst : public Instruction {
|
||||||
@@ -832,10 +826,10 @@ class PhiInst : public Instruction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Value * getMapVal() { return map_val; }
|
Value* getMapVal() { return map_val; }
|
||||||
Value * getPointer() const { return getOperand(0); }
|
Value* getPointer() const { return getOperand(0); }
|
||||||
auto getValues() { return make_range(std::next(operand_begin()), operand_end()); }
|
auto getValues() { return make_range(std::next(operand_begin()), operand_end()); }
|
||||||
Value * getValue(unsigned index) const { return getOperand(index + 1); }
|
Value* getValue(unsigned index) const { return getOperand(index + 1); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -849,7 +843,7 @@ protected:
|
|||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Function *getCallee() const;
|
Function* getCallee() const;
|
||||||
auto getArguments() const {
|
auto getArguments() const {
|
||||||
return make_range(std::next(operand_begin()), operand_end());
|
return make_range(std::next(operand_begin()), operand_end());
|
||||||
}
|
}
|
||||||
@@ -870,7 +864,7 @@ protected:
|
|||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Value *getOperand() const { return User::getOperand(0); }
|
Value* getOperand() const { return User::getOperand(0); }
|
||||||
|
|
||||||
}; // class UnaryInst
|
}; // class UnaryInst
|
||||||
|
|
||||||
@@ -887,10 +881,10 @@ class BinaryInst : public Instruction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Value *getLhs() const { return getOperand(0); }
|
Value* getLhs() const { return getOperand(0); }
|
||||||
Value *getRhs() const { return getOperand(1); }
|
Value* getRhs() const { return getOperand(1); }
|
||||||
template <typename T>
|
template <typename T>
|
||||||
auto eval(T lhs, T rhs) -> T {
|
T eval(T lhs, T rhs) {
|
||||||
switch (getKind()) {
|
switch (getKind()) {
|
||||||
case kAdd:
|
case kAdd:
|
||||||
return lhs + rhs;
|
return lhs + rhs;
|
||||||
@@ -963,7 +957,7 @@ class ReturnInst : public Instruction {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
bool hasReturnValue() const { return not operands.empty(); }
|
bool hasReturnValue() const { return not operands.empty(); }
|
||||||
Value *getReturnValue() const {
|
Value* getReturnValue() const {
|
||||||
return hasReturnValue() ? getOperand(0) : nullptr;
|
return hasReturnValue() ? getOperand(0) : nullptr;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -983,7 +977,7 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BasicBlock *getBlock() const { return dynamic_cast<BasicBlock *>(getOperand(0)); }
|
BasicBlock* getBlock() const { return dynamic_cast<BasicBlock *>(getOperand(0)); }
|
||||||
auto getArguments() const {
|
auto getArguments() const {
|
||||||
return make_range(std::next(operand_begin()), operand_end());
|
return make_range(std::next(operand_begin()), operand_end());
|
||||||
}
|
}
|
||||||
@@ -1009,11 +1003,11 @@ protected:
|
|||||||
addOperands(elseArgs);
|
addOperands(elseArgs);
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
Value *getCondition() const { return getOperand(0); }
|
Value* getCondition() const { return getOperand(0); }
|
||||||
BasicBlock *getThenBlock() const {
|
BasicBlock* getThenBlock() const {
|
||||||
return dynamic_cast<BasicBlock *>(getOperand(1));
|
return dynamic_cast<BasicBlock *>(getOperand(1));
|
||||||
}
|
}
|
||||||
BasicBlock *getElseBlock() const {
|
BasicBlock* getElseBlock() const {
|
||||||
return dynamic_cast<BasicBlock *>(getOperand(2));
|
return dynamic_cast<BasicBlock *>(getOperand(2));
|
||||||
}
|
}
|
||||||
auto getThenArguments() const {
|
auto getThenArguments() const {
|
||||||
@@ -1053,7 +1047,7 @@ public:
|
|||||||
|
|
||||||
int getNumDims() const { return getNumOperands(); }
|
int getNumDims() const { return getNumOperands(); }
|
||||||
auto getDims() const { return getOperands(); }
|
auto getDims() const { return getOperands(); }
|
||||||
Value *getDim(int index) { return getOperand(index); }
|
Value* getDim(int index) { return getOperand(index); }
|
||||||
|
|
||||||
}; // class AllocaInst
|
}; // class AllocaInst
|
||||||
|
|
||||||
@@ -1083,12 +1077,12 @@ class GetSubArrayInst : public Instruction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto getFatherArray() const -> Value * { return getOperand(0); } ///< 获取父数组
|
Value* getFatherArray() const { return getOperand(0); } ///< 获取父数组
|
||||||
auto getChildArray() const -> Value * { return getOperand(1); } ///< 获取子数组
|
Value* getChildArray() const { return getOperand(1); } ///< 获取子数组
|
||||||
auto getFatherLVal() const -> LVal * { return dynamic_cast<LVal *>(getOperand(0)); } ///< 获取父左值
|
LVal* getFatherLVal() const { return dynamic_cast<LVal *>(getOperand(0)); } ///< 获取父左值
|
||||||
auto getChildLVal() const -> LVal * { return dynamic_cast<LVal *>(getOperand(1)); } ///< 获取子左值
|
LVal* getChildLVal() const { return dynamic_cast<LVal *>(getOperand(1)); } ///< 获取子左值
|
||||||
auto getIndices() const { return make_range(std::next(operand_begin(), 2), operand_end()); } ///< 获取索引
|
auto getIndices() const { return make_range(std::next(operand_begin(), 2), operand_end()); } ///< 获取索引
|
||||||
auto getNumIndices() const -> unsigned { return getNumOperands() - 2; } ///< 获取索引数量
|
unsigned getNumIndices() const { return getNumOperands() - 2; } ///< 获取索引数量
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Load a value from memory address specified by a pointer value
|
//! Load a value from memory address specified by a pointer value
|
||||||
@@ -1107,11 +1101,11 @@ protected:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
int getNumIndices() const { return getNumOperands() - 1; }
|
int getNumIndices() const { return getNumOperands() - 1; }
|
||||||
Value *getPointer() const { return getOperand(0); }
|
Value* getPointer() const { return getOperand(0); }
|
||||||
auto getIndices() const {
|
auto getIndices() const {
|
||||||
return make_range(std::next(operand_begin()), operand_end());
|
return make_range(std::next(operand_begin()), operand_end());
|
||||||
}
|
}
|
||||||
Value *getIndex(int index) const { return getOperand(index + 1); }
|
Value* getIndex(int index) const { return getOperand(index + 1); }
|
||||||
std::list<Value *> getAncestorIndices() const {
|
std::list<Value *> getAncestorIndices() const {
|
||||||
std::list<Value *> indices;
|
std::list<Value *> indices;
|
||||||
for (const auto &index : getIndices()) {
|
for (const auto &index : getIndices()) {
|
||||||
@@ -1147,12 +1141,12 @@ protected:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
int getNumIndices() const { return getNumOperands() - 2; }
|
int getNumIndices() const { return getNumOperands() - 2; }
|
||||||
Value *getValue() const { return getOperand(0); }
|
Value* getValue() const { return getOperand(0); }
|
||||||
Value *getPointer() const { return getOperand(1); }
|
Value* getPointer() const { return getOperand(1); }
|
||||||
auto getIndices() const {
|
auto getIndices() const {
|
||||||
return make_range(std::next(operand_begin(), 2), operand_end());
|
return make_range(std::next(operand_begin(), 2), operand_end());
|
||||||
}
|
}
|
||||||
Value *getIndex(int index) const { return getOperand(index + 2); }
|
Value* getIndex(int index) const { return getOperand(index + 2); }
|
||||||
std::list<Value *> getAncestorIndices() const {
|
std::list<Value *> getAncestorIndices() const {
|
||||||
std::list<Value *> indices;
|
std::list<Value *> indices;
|
||||||
for (const auto &index : getIndices()) {
|
for (const auto &index : getIndices()) {
|
||||||
@@ -1178,6 +1172,13 @@ class MemsetInst : public Instruction {
|
|||||||
friend class Function;
|
friend class Function;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
//! Create a memset instruction.
|
||||||
|
//! \param pointer The pointer to the memory location to be set.
|
||||||
|
//! \param begin The starting address of the memory region to be set.
|
||||||
|
//! \param size The size of the memory region to be set.
|
||||||
|
//! \param value The value to set the memory region to.
|
||||||
|
//! \param parent The parent basic block of this instruction.
|
||||||
|
//! \param name The name of this instruction.
|
||||||
MemsetInst(Value *pointer, Value *begin, Value *size, Value *value,
|
MemsetInst(Value *pointer, Value *begin, Value *size, Value *value,
|
||||||
BasicBlock *parent = nullptr, const std::string &name = "")
|
BasicBlock *parent = nullptr, const std::string &name = "")
|
||||||
: Instruction(kMemset, Type::getVoidType(), parent, name) {
|
: Instruction(kMemset, Type::getVoidType(), parent, name) {
|
||||||
@@ -1188,10 +1189,10 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Value *getPointer() const { return getOperand(0); }
|
Value* getPointer() const { return getOperand(0); }
|
||||||
Value *getBegin() const { return getOperand(1); }
|
Value* getBegin() const { return getOperand(1); }
|
||||||
Value *getSize() const { return getOperand(2); }
|
Value* getSize() const { return getOperand(2); }
|
||||||
Value *getValue() const { return getOperand(3); }
|
Value* getValue() const { return getOperand(3); }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1241,26 +1242,26 @@ public:
|
|||||||
loopCount = loopCount + 1;
|
loopCount = loopCount + 1;
|
||||||
loopID = loopCount;
|
loopID = loopCount;
|
||||||
}
|
}
|
||||||
auto getindBegin() { return indBegin; } ///< 获得循环开始值
|
ConstantValue* getindBegin() { return indBegin; } ///< 获得循环开始值
|
||||||
auto getindStep() { return indStep; } ///< 获得循环步长
|
ConstantValue* getindStep() { return indStep; } ///< 获得循环步长
|
||||||
auto setindBegin(ConstantValue *indBegin2set) { indBegin = indBegin2set; } ///< 设置循环开始值
|
void setindBegin(ConstantValue *indBegin2set) { indBegin = indBegin2set; } ///< 设置循环开始值
|
||||||
auto setindStep(ConstantValue *indStep2set) { indStep = indStep2set; } ///< 设置循环步长
|
void setindStep(ConstantValue *indStep2set) { indStep = indStep2set; } ///< 设置循环步长
|
||||||
auto setStepType(int StepType2Set) { StepType = StepType2Set; } ///< 设置循环变量规则
|
void setStepType(int StepType2Set) { StepType = StepType2Set; } ///< 设置循环变量规则
|
||||||
auto getStepType() { return StepType; } ///< 获得循环变量规则
|
int getStepType() { return StepType; } ///< 获得循环变量规则
|
||||||
auto getLoopID() -> size_t { return loopID; }
|
size_t getLoopID() { return loopID; }
|
||||||
|
|
||||||
BasicBlock *getHeader() const { return headerBlock; }
|
BasicBlock* getHeader() const { return headerBlock; }
|
||||||
BasicBlock *getPreheaderBlock() const { return preheaderBlock; }
|
BasicBlock* getPreheaderBlock() const { return preheaderBlock; }
|
||||||
block_list &getLatchBlocks() { return latchBlock; }
|
block_list& getLatchBlocks() { return latchBlock; }
|
||||||
block_set &getExitingBlocks() { return exitingBlocks; }
|
block_set& getExitingBlocks() { return exitingBlocks; }
|
||||||
block_set &getExitBlocks() { return exitBlocks; }
|
block_set& getExitBlocks() { return exitBlocks; }
|
||||||
Loop *getParentLoop() const { return parentloop; }
|
Loop* getParentLoop() const { return parentloop; }
|
||||||
void setParentLoop(Loop *parent) { parentloop = parent; }
|
void setParentLoop(Loop *parent) { parentloop = parent; }
|
||||||
void addBasicBlock(BasicBlock *bb) { blocksInLoop.push_back(bb); }
|
void addBasicBlock(BasicBlock *bb) { blocksInLoop.push_back(bb); }
|
||||||
void addSubLoop(Loop *loop) { subLoops.push_back(loop); }
|
void addSubLoop(Loop *loop) { subLoops.push_back(loop); }
|
||||||
void setLoopDepth(unsigned depth) { loopDepth = depth; }
|
void setLoopDepth(unsigned depth) { loopDepth = depth; }
|
||||||
block_list &getBasicBlocks() { return blocksInLoop; }
|
block_list& getBasicBlocks() { return blocksInLoop; }
|
||||||
Loop_list &getSubLoops() { return subLoops; }
|
Loop_list& getSubLoops() { return subLoops; }
|
||||||
unsigned getLoopDepth() const { return loopDepth; }
|
unsigned getLoopDepth() const { return loopDepth; }
|
||||||
|
|
||||||
bool isLoopContainsBasicBlock(BasicBlock *bb) const {
|
bool isLoopContainsBasicBlock(BasicBlock *bb) const {
|
||||||
@@ -1280,14 +1281,14 @@ public:
|
|||||||
|
|
||||||
void setIndEnd(Value *value) { indEnd = value; }
|
void setIndEnd(Value *value) { indEnd = value; }
|
||||||
void setIndPhi(AllocaInst *phi) { IndPhi = phi; }
|
void setIndPhi(AllocaInst *phi) { IndPhi = phi; }
|
||||||
Value *getIndEnd() const { return indEnd; }
|
Value* getIndEnd() const { return indEnd; }
|
||||||
AllocaInst *getIndPhi() const { return IndPhi; }
|
AllocaInst* getIndPhi() const { return IndPhi; }
|
||||||
Instruction *getIndCondVar() const { return indCondVar; }
|
Instruction* getIndCondVar() const { return indCondVar; }
|
||||||
|
|
||||||
auto addGlobalValuechange(GlobalValue *globalvaluechange2add) {
|
void addGlobalValuechange(GlobalValue *globalvaluechange2add) {
|
||||||
GlobalValuechange.insert(globalvaluechange2add);
|
GlobalValuechange.insert(globalvaluechange2add);
|
||||||
} ///<添加在循环中改变的全局变量
|
} ///<添加在循环中改变的全局变量
|
||||||
auto getGlobalValuechange() -> std::set<GlobalValue *> & {
|
std::set<GlobalValue *>& getGlobalValuechange() {
|
||||||
return GlobalValuechange;
|
return GlobalValuechange;
|
||||||
} ///<获得在循环中改变的所有全局变量
|
} ///<获得在循环中改变的所有全局变量
|
||||||
|
|
||||||
@@ -1335,83 +1336,82 @@ protected:
|
|||||||
std::unordered_map<Value *, std::unordered_map<BasicBlock *, int>> value2UseBlocks; //< value -- use blocks mapping
|
std::unordered_map<Value *, std::unordered_map<BasicBlock *, int>> value2UseBlocks; //< value -- use blocks mapping
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static auto getcloneIndex() -> unsigned {
|
static unsigned getcloneIndex() {
|
||||||
static unsigned cloneIndex = 0;
|
static unsigned cloneIndex = 0;
|
||||||
cloneIndex += 1;
|
cloneIndex += 1;
|
||||||
return cloneIndex - 1;
|
return cloneIndex - 1;
|
||||||
}
|
}
|
||||||
auto clone(const std::string &suffix = "_" + std::to_string(getcloneIndex()) + "@") const
|
Function* clone(const std::string &suffix = "_" + std::to_string(getcloneIndex()) + "@") const; ///< 复制函数
|
||||||
-> Function *; ///< 复制函数
|
const std::set<Function *>& getCallees() { return callees; }
|
||||||
auto getCallees() -> const std::set<Function *> & { return callees; }
|
void addCallee(Function *callee) { callees.insert(callee); }
|
||||||
auto addCallee(Function *callee) -> void { callees.insert(callee); }
|
void removeCallee(Function *callee) { callees.erase(callee); }
|
||||||
auto removeCallee(Function *callee) -> void { callees.erase(callee); }
|
void clearCallees() { callees.clear(); }
|
||||||
auto clearCallees() -> void { callees.clear(); }
|
std::set<Function *> getCalleesWithNoExternalAndSelf();
|
||||||
auto getCalleesWithNoExternalAndSelf() -> std::set<Function *>;
|
FunctionAttribute getAttribute() const { return attribute; } ///< 获取函数属性
|
||||||
auto getAttribute() const -> FunctionAttribute { return attribute; } ///< 获取函数属性
|
void setAttribute(FunctionAttribute attr) {
|
||||||
auto setAttribute(FunctionAttribute attr) -> void {
|
|
||||||
attribute = static_cast<FunctionAttribute>(attribute | attr);
|
attribute = static_cast<FunctionAttribute>(attribute | attr);
|
||||||
} ///< 设置函数属性
|
} ///< 设置函数属性
|
||||||
auto clearAttribute() -> void { attribute = PlaceHolder; } ///< 清楚所有函数属性,只保留PlaceHolder
|
void clearAttribute() { attribute = PlaceHolder; } ///< 清楚所有函数属性,只保留PlaceHolder
|
||||||
auto getLoopOfBasicBlock(BasicBlock *bb) -> Loop * {
|
Loop* getLoopOfBasicBlock(BasicBlock *bb) {
|
||||||
return basicblock2Loop.count(bb) != 0 ? basicblock2Loop[bb] : nullptr;
|
return basicblock2Loop.count(bb) != 0 ? basicblock2Loop[bb] : nullptr;
|
||||||
} ///< 获得块所在循环
|
} ///< 获得块所在循环
|
||||||
auto getLoopDepthByBlock(BasicBlock *basicblock2Check) {
|
unsigned getLoopDepthByBlock(BasicBlock *basicblock2Check) {
|
||||||
if (getLoopOfBasicBlock(basicblock2Check) != nullptr) {
|
if (getLoopOfBasicBlock(basicblock2Check) != nullptr) {
|
||||||
auto loop = getLoopOfBasicBlock(basicblock2Check);
|
auto loop = getLoopOfBasicBlock(basicblock2Check);
|
||||||
return loop->getLoopDepth();
|
return loop->getLoopDepth();
|
||||||
}
|
}
|
||||||
return static_cast<unsigned>(0);
|
return static_cast<unsigned>(0);
|
||||||
} ///< 通过块,获得其所在循环深度
|
} ///< 通过块,获得其所在循环深度
|
||||||
auto addBBToLoop(BasicBlock *bb, Loop *LoopToadd) { basicblock2Loop[bb] = LoopToadd; } ///< 添加块与循环的映射
|
void addBBToLoop(BasicBlock *bb, Loop *LoopToadd) { basicblock2Loop[bb] = LoopToadd; } ///< 添加块与循环的映射
|
||||||
auto getBBToLoopRef() -> std::unordered_map<BasicBlock *, Loop *> & {
|
std::unordered_map<BasicBlock *, Loop *>& getBBToLoopRef() {
|
||||||
return basicblock2Loop;
|
return basicblock2Loop;
|
||||||
} ///< 获得块-循环映射表
|
} ///< 获得块-循环映射表
|
||||||
// auto getNewLoopPtr(BasicBlock *header) -> Loop * { return new Loop(header); }
|
// auto getNewLoopPtr(BasicBlock *header) -> Loop * { return new Loop(header); }
|
||||||
auto getReturnType() const -> Type * { return getType()->as<FunctionType>()->getReturnType(); } ///< 获取返回值类型
|
Type* getReturnType() const { return getType()->as<FunctionType>()->getReturnType(); } ///< 获取返回值类型
|
||||||
auto getParamTypes() const { return getType()->as<FunctionType>()->getParamTypes(); } ///< 获取形式参数类型列表
|
auto getParamTypes() const { return getType()->as<FunctionType>()->getParamTypes(); } ///< 获取形式参数类型列表
|
||||||
auto getBasicBlocks() { return make_range(blocks); } ///< 获取基本块列表
|
auto getBasicBlocks() { return make_range(blocks); } ///< 获取基本块列表
|
||||||
auto getBasicBlocks_NoRange() -> block_list & { return blocks; }
|
block_list& getBasicBlocks_NoRange() { return blocks; }
|
||||||
auto getEntryBlock() -> BasicBlock * { return blocks.front().get(); } ///< 获取入口块
|
BasicBlock* getEntryBlock() { return blocks.front().get(); } ///< 获取入口块
|
||||||
auto 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);
|
||||||
// blocks.erase(std::remove_if(blocks.begin(), blocks.end(), is_same_ptr), blocks.end());
|
// blocks.erase(std::remove_if(blocks.begin(), blocks.end(), is_same_ptr), blocks.end());
|
||||||
} ///< 将该块从function的blocks中删除
|
} ///< 将该块从function的blocks中删除
|
||||||
// auto getBasicBlocks_NoRange() -> block_list & { return blocks; }
|
// auto getBasicBlocks_NoRange() -> block_list & { return blocks; }
|
||||||
auto addBasicBlock(const std::string &name = "") -> BasicBlock * {
|
BasicBlock* addBasicBlock(const std::string &name = "") {
|
||||||
blocks.emplace_back(new BasicBlock(this, name));
|
blocks.emplace_back(new BasicBlock(this, name));
|
||||||
return blocks.back().get();
|
return blocks.back().get();
|
||||||
} ///< 添加新的基本块
|
} ///< 添加新的基本块
|
||||||
auto addBasicBlock(BasicBlock *block) -> BasicBlock * {
|
BasicBlock* addBasicBlock(BasicBlock *block) {
|
||||||
blocks.emplace_back(block);
|
blocks.emplace_back(block);
|
||||||
return block;
|
return block;
|
||||||
} ///< 添加基本块到blocks中
|
} ///< 添加基本块到blocks中
|
||||||
auto addBasicBlockFront(BasicBlock *block) -> BasicBlock * {
|
BasicBlock* addBasicBlockFront(BasicBlock *block) {
|
||||||
blocks.emplace_front(block);
|
blocks.emplace_front(block);
|
||||||
return block;
|
return block;
|
||||||
} // 从前端插入新的基本块
|
} // 从前端插入新的基本块
|
||||||
/** value -- alloc blocks mapping */
|
/** value -- alloc blocks mapping */
|
||||||
auto addValue2AllocBlocks(Value *value, BasicBlock *block) -> void {
|
void addValue2AllocBlocks(Value *value, BasicBlock *block) {
|
||||||
value2AllocBlocks[value] = block;
|
value2AllocBlocks[value] = block;
|
||||||
} ///< 添加value -- alloc block mapping
|
} ///< 添加value -- alloc block mapping
|
||||||
auto getAllocBlockByValue(Value *value) -> BasicBlock * {
|
BasicBlock* getAllocBlockByValue(Value *value) {
|
||||||
if (value2AllocBlocks.count(value) > 0) {
|
if (value2AllocBlocks.count(value) > 0) {
|
||||||
return value2AllocBlocks[value];
|
return value2AllocBlocks[value];
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
} ///< 通过value获取alloc block
|
} ///< 通过value获取alloc block
|
||||||
auto getValue2AllocBlocks() -> std::unordered_map<Value *, BasicBlock *> & {
|
std::unordered_map<Value *, BasicBlock *>& getValue2AllocBlocks() {
|
||||||
return value2AllocBlocks;
|
return value2AllocBlocks;
|
||||||
} ///< 获取所有value -- alloc block mappings
|
} ///< 获取所有value -- alloc block mappings
|
||||||
auto removeValue2AllocBlock(Value *value) -> void {
|
void removeValue2AllocBlock(Value *value) {
|
||||||
value2AllocBlocks.erase(value);
|
value2AllocBlocks.erase(value);
|
||||||
} ///< 删除value -- alloc block mapping
|
} ///< 删除value -- alloc block mapping
|
||||||
/** value -- define blocks mapping */
|
/** value -- define blocks mapping */
|
||||||
auto addValue2DefBlocks(Value *value, BasicBlock *block) -> void {
|
void addValue2DefBlocks(Value *value, BasicBlock *block) {
|
||||||
++value2DefBlocks[value][block];
|
++value2DefBlocks[value][block];
|
||||||
} ///< 添加value -- define block mapping
|
} ///< 添加value -- define block mapping
|
||||||
// keep in mind that the return is not a reference.
|
// keep in mind that the return is not a reference.
|
||||||
auto getDefBlocksByValue(Value *value) -> std::unordered_set<BasicBlock *> {
|
std::unordered_set<BasicBlock *> getDefBlocksByValue(Value *value) {
|
||||||
std::unordered_set<BasicBlock *> blocks;
|
std::unordered_set<BasicBlock *> blocks;
|
||||||
if (value2DefBlocks.count(value) > 0) {
|
if (value2DefBlocks.count(value) > 0) {
|
||||||
for (const auto &pair : value2DefBlocks[value]) {
|
for (const auto &pair : value2DefBlocks[value]) {
|
||||||
@@ -1420,10 +1420,10 @@ protected:
|
|||||||
}
|
}
|
||||||
return blocks;
|
return blocks;
|
||||||
} ///< 通过value获取define blocks
|
} ///< 通过value获取define blocks
|
||||||
auto getValue2DefBlocks() -> std::unordered_map<Value *, std::unordered_map<BasicBlock *, int>> & {
|
std::unordered_map<Value *, std::unordered_map<BasicBlock *, int>>& getValue2DefBlocks() {
|
||||||
return value2DefBlocks;
|
return value2DefBlocks;
|
||||||
} ///< 获取所有value -- define blocks mappings
|
} ///< 获取所有value -- define blocks mappings
|
||||||
auto removeValue2DefBlock(Value *value, BasicBlock *block) -> bool {
|
bool removeValue2DefBlock(Value *value, BasicBlock *block) {
|
||||||
bool changed = false;
|
bool changed = false;
|
||||||
if (--value2DefBlocks[value][block] == 0) {
|
if (--value2DefBlocks[value][block] == 0) {
|
||||||
value2DefBlocks[value].erase(block);
|
value2DefBlocks[value].erase(block);
|
||||||
@@ -1434,7 +1434,7 @@ protected:
|
|||||||
}
|
}
|
||||||
return changed;
|
return changed;
|
||||||
} ///< 删除value -- define block mapping
|
} ///< 删除value -- define block mapping
|
||||||
auto getValuesOfDefBlock() -> std::unordered_set<Value *> {
|
std::unordered_set<Value *> getValuesOfDefBlock() {
|
||||||
std::unordered_set<Value *> values;
|
std::unordered_set<Value *> values;
|
||||||
for (const auto &pair : value2DefBlocks) {
|
for (const auto &pair : value2DefBlocks) {
|
||||||
values.insert(pair.first);
|
values.insert(pair.first);
|
||||||
@@ -1442,11 +1442,11 @@ protected:
|
|||||||
return values;
|
return values;
|
||||||
} ///< 获取所有定义过的value
|
} ///< 获取所有定义过的value
|
||||||
/** value -- use blocks mapping */
|
/** value -- use blocks mapping */
|
||||||
auto addValue2UseBlocks(Value *value, BasicBlock *block) -> void {
|
void addValue2UseBlocks(Value *value, BasicBlock *block) {
|
||||||
++value2UseBlocks[value][block];
|
++value2UseBlocks[value][block];
|
||||||
} ///< 添加value -- use block mapping
|
} ///< 添加value -- use block mapping
|
||||||
// keep in mind that the return is not a reference.
|
// keep in mind that the return is not a reference.
|
||||||
auto getUseBlocksByValue(Value *value) -> std::unordered_set<BasicBlock *> {
|
std::unordered_set<BasicBlock *> getUseBlocksByValue(Value *value) {
|
||||||
std::unordered_set<BasicBlock *> blocks;
|
std::unordered_set<BasicBlock *> blocks;
|
||||||
if (value2UseBlocks.count(value) > 0) {
|
if (value2UseBlocks.count(value) > 0) {
|
||||||
for (const auto &pair : value2UseBlocks[value]) {
|
for (const auto &pair : value2UseBlocks[value]) {
|
||||||
@@ -1455,10 +1455,10 @@ protected:
|
|||||||
}
|
}
|
||||||
return blocks;
|
return blocks;
|
||||||
} ///< 通过value获取use blocks
|
} ///< 通过value获取use blocks
|
||||||
auto getValue2UseBlocks() -> std::unordered_map<Value *, std::unordered_map<BasicBlock *, int>> & {
|
std::unordered_map<Value *, std::unordered_map<BasicBlock *, int>>& getValue2UseBlocks() {
|
||||||
return value2UseBlocks;
|
return value2UseBlocks;
|
||||||
} ///< 获取所有value -- use blocks mappings
|
} ///< 获取所有value -- use blocks mappings
|
||||||
auto removeValue2UseBlock(Value *value, BasicBlock *block) -> bool {
|
bool removeValue2UseBlock(Value *value, BasicBlock *block) {
|
||||||
bool changed = false;
|
bool changed = false;
|
||||||
if (--value2UseBlocks[value][block] == 0) {
|
if (--value2UseBlocks[value][block] == 0) {
|
||||||
value2UseBlocks[value].erase(block);
|
value2UseBlocks[value].erase(block);
|
||||||
@@ -1469,8 +1469,8 @@ protected:
|
|||||||
}
|
}
|
||||||
return changed;
|
return changed;
|
||||||
} ///< 删除value -- use block mapping
|
} ///< 删除value -- use block mapping
|
||||||
auto addIndirectAlloca(AllocaInst *alloca) { indirectAllocas.emplace_back(alloca); } ///< 添加间接分配
|
void addIndirectAlloca(AllocaInst *alloca) { indirectAllocas.emplace_back(alloca); } ///< 添加间接分配
|
||||||
auto getIndirectAllocas() -> std::list<std::unique_ptr<AllocaInst>> & {
|
std::list<std::unique_ptr<AllocaInst>>& getIndirectAllocas() {
|
||||||
return indirectAllocas;
|
return indirectAllocas;
|
||||||
} ///< 获取间接分配列表
|
} ///< 获取间接分配列表
|
||||||
|
|
||||||
@@ -1478,8 +1478,8 @@ protected:
|
|||||||
|
|
||||||
void addLoop(Loop *loop) { loops.emplace_back(loop); } ///< 添加循环(非顶层)
|
void addLoop(Loop *loop) { loops.emplace_back(loop); } ///< 添加循环(非顶层)
|
||||||
void addTopLoop(Loop *loop) { topLoops.emplace_back(loop); } ///< 添加顶层循环
|
void addTopLoop(Loop *loop) { topLoops.emplace_back(loop); } ///< 添加顶层循环
|
||||||
auto getLoops() -> Loop_list & { return loops; } ///< 获得循环(非顶层)
|
Loop_list& getLoops() { return loops; } ///< 获得循环(非顶层)
|
||||||
auto getTopLoops() -> Loop_list & { return topLoops; } ///< 获得顶层循环
|
Loop_list& getTopLoops() { return topLoops; } ///< 获得顶层循环
|
||||||
/** loop -- end */
|
/** loop -- end */
|
||||||
|
|
||||||
}; // class Function
|
}; // class Function
|
||||||
@@ -1532,7 +1532,7 @@ public:
|
|||||||
Value* getByIndex(unsigned index) const {
|
Value* getByIndex(unsigned index) const {
|
||||||
return initValues.getValue(index);
|
return initValues.getValue(index);
|
||||||
} ///< 通过一维偏移量index获取初始值
|
} ///< 通过一维偏移量index获取初始值
|
||||||
Value * getByIndices(const std::vector<Value *> &indices) const {
|
Value* getByIndices(const std::vector<Value *> &indices) const {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (size_t i = 0; i < indices.size(); i++) {
|
for (size_t i = 0; i < indices.size(); i++) {
|
||||||
index = dynamic_cast<ConstantValue *>(getDim(i))->getInt() * index +
|
index = dynamic_cast<ConstantValue *>(getDim(i))->getInt() * index +
|
||||||
@@ -1540,7 +1540,7 @@ public:
|
|||||||
}
|
}
|
||||||
return getByIndex(index);
|
return getByIndex(index);
|
||||||
} ///< 通过多维索引indices获取初始值
|
} ///< 通过多维索引indices获取初始值
|
||||||
const ValueCounter &getInitValues() const { return initValues; }
|
const ValueCounter& getInitValues() const { return initValues; }
|
||||||
}; // class GlobalValue
|
}; // class GlobalValue
|
||||||
|
|
||||||
|
|
||||||
@@ -1583,13 +1583,13 @@ class ConstantVariable : public User, public LVal {
|
|||||||
return getByIndex(index);
|
return getByIndex(index);
|
||||||
} ///< 通过多维索引indices获取初始值
|
} ///< 通过多维索引indices获取初始值
|
||||||
unsigned getNumDims() const { return numDims; } ///< 获取维度数量
|
unsigned getNumDims() const { return numDims; } ///< 获取维度数量
|
||||||
Value* getDim(unsigned index) const { return getOperand(index); } ///< 获取位置为index的维度
|
Value* getDim(unsigned index) const { return getOperand(index); } ///< 获取位置为index的维度
|
||||||
auto getDims() const { return getOperands(); } ///< 获取维度列表
|
auto getDims() const { return getOperands(); } ///< 获取维度列表
|
||||||
const ValueCounter& getInitValues() const { return initValues; } ///< 获取初始值
|
const ValueCounter& getInitValues() const { return initValues; } ///< 获取初始值
|
||||||
};
|
};
|
||||||
|
|
||||||
using SymbolTableNode = struct SymbolTableNode {
|
using SymbolTableNode = struct SymbolTableNode {
|
||||||
struct SymbolTableNode *pNode; ///< 父节点
|
SymbolTableNode *pNode; ///< 父节点
|
||||||
std::vector<SymbolTableNode *> children; ///< 子节点列表
|
std::vector<SymbolTableNode *> children; ///< 子节点列表
|
||||||
std::map<std::string, User *> varList; ///< 变量列表
|
std::map<std::string, User *> varList; ///< 变量列表
|
||||||
};
|
};
|
||||||
@@ -1606,15 +1606,15 @@ class SymbolTable {
|
|||||||
public:
|
public:
|
||||||
SymbolTable() = default;
|
SymbolTable() = default;
|
||||||
|
|
||||||
auto getVariable(const std::string &name) const -> User *; ///< 根据名字name以及当前作用域获取变量
|
User* getVariable(const std::string &name) const; ///< 根据名字name以及当前作用域获取变量
|
||||||
auto addVariable(const std::string &name, User *variable) -> User *; ///< 添加变量
|
User* addVariable(const std::string &name, User *variable); ///< 添加变量
|
||||||
auto getGlobals() -> std::vector<std::unique_ptr<GlobalValue>> &; ///< 获取全局变量列表
|
std::vector<std::unique_ptr<GlobalValue>>& getGlobals(); ///< 获取全局变量列表
|
||||||
auto getConsts() const -> const std::vector<std::unique_ptr<ConstantVariable>> &; ///< 获取常量列表
|
const std::vector<std::unique_ptr<ConstantVariable>>& getConsts() const; ///< 获取常量列表
|
||||||
void enterNewScope(); ///< 进入新的作用域
|
void enterNewScope(); ///< 进入新的作用域
|
||||||
void leaveScope(); ///< 离开作用域
|
void leaveScope(); ///< 离开作用域
|
||||||
auto isInGlobalScope() const -> bool; ///< 是否位于全局作用域
|
bool isInGlobalScope() const; ///< 是否位于全局作用域
|
||||||
void enterGlobalScope(); ///< 进入全局作用域
|
void enterGlobalScope(); ///< 进入全局作用域
|
||||||
auto isCurNodeNull() -> bool { return curNode == nullptr; }
|
bool isCurNodeNull() { return curNode == nullptr; }
|
||||||
};
|
};
|
||||||
|
|
||||||
//! IR unit for representing a SysY compile unit
|
//! IR unit for representing a SysY compile unit
|
||||||
@@ -1628,14 +1628,14 @@ class Module {
|
|||||||
Module() = default;
|
Module() = default;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto createFunction(const std::string &name, Type *type) -> Function * {
|
Function* createFunction(const std::string &name, Type *type) {
|
||||||
auto result = functions.try_emplace(name, new Function(this, type, name));
|
auto result = functions.try_emplace(name, new Function(this, type, name));
|
||||||
if (!result.second) {
|
if (!result.second) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return result.first->second.get();
|
return result.first->second.get();
|
||||||
} ///< 创建函数
|
} ///< 创建函数
|
||||||
auto createExternalFunction(const std::string &name, Type *type) -> Function * {
|
Function* createExternalFunction(const std::string &name, Type *type) {
|
||||||
auto result = externalFunctions.try_emplace(name, new Function(this, type, name));
|
auto result = externalFunctions.try_emplace(name, new Function(this, type, name));
|
||||||
if (!result.second) {
|
if (!result.second) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@@ -1643,8 +1643,8 @@ class Module {
|
|||||||
return result.first->second.get();
|
return result.first->second.get();
|
||||||
} ///< 创建外部函数
|
} ///< 创建外部函数
|
||||||
///< 变量创建伴随着符号表的更新
|
///< 变量创建伴随着符号表的更新
|
||||||
auto createGlobalValue(const std::string &name, Type *type, const std::vector<Value *> &dims = {},
|
GlobalValue* createGlobalValue(const std::string &name, Type *type, const std::vector<Value *> &dims = {},
|
||||||
const ValueCounter &init = {}) -> GlobalValue * {
|
const ValueCounter &init = {}) {
|
||||||
bool isFinished = variableTable.isCurNodeNull();
|
bool isFinished = variableTable.isCurNodeNull();
|
||||||
if (isFinished) {
|
if (isFinished) {
|
||||||
variableTable.enterGlobalScope();
|
variableTable.enterGlobalScope();
|
||||||
@@ -1658,8 +1658,8 @@ class Module {
|
|||||||
}
|
}
|
||||||
return dynamic_cast<GlobalValue *>(result);
|
return dynamic_cast<GlobalValue *>(result);
|
||||||
} ///< 创建全局变量
|
} ///< 创建全局变量
|
||||||
auto createConstVar(const std::string &name, Type *type, const ValueCounter &init,
|
ConstantVariable* createConstVar(const std::string &name, Type *type, const ValueCounter &init,
|
||||||
const std::vector<Value *> &dims = {}) -> ConstantVariable * {
|
const std::vector<Value *> &dims = {}) {
|
||||||
auto result = variableTable.addVariable(name, new ConstantVariable(this, type, name, init, dims));
|
auto result = variableTable.addVariable(name, new ConstantVariable(this, type, name, init, dims));
|
||||||
if (result == nullptr) {
|
if (result == nullptr) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@@ -1669,38 +1669,38 @@ class Module {
|
|||||||
void addVariable(const std::string &name, AllocaInst *variable) {
|
void addVariable(const std::string &name, AllocaInst *variable) {
|
||||||
variableTable.addVariable(name, variable);
|
variableTable.addVariable(name, variable);
|
||||||
} ///< 添加变量
|
} ///< 添加变量
|
||||||
auto getVariable(const std::string &name) -> User * {
|
User* getVariable(const std::string &name) {
|
||||||
return variableTable.getVariable(name);
|
return variableTable.getVariable(name);
|
||||||
} ///< 根据名字name和当前作用域获取变量
|
} ///< 根据名字name和当前作用域获取变量
|
||||||
auto getFunction(const std::string &name) const -> Function * {
|
Function* getFunction(const std::string &name) const {
|
||||||
auto result = functions.find(name);
|
auto result = functions.find(name);
|
||||||
if (result == functions.end()) {
|
if (result == functions.end()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return result->second.get();
|
return result->second.get();
|
||||||
} ///< 获取函数
|
} ///< 获取函数
|
||||||
auto getExternalFunction(const std::string &name) const -> Function * {
|
Function* getExternalFunction(const std::string &name) const {
|
||||||
auto result = externalFunctions.find(name);
|
auto result = externalFunctions.find(name);
|
||||||
if (result == functions.end()) {
|
if (result == functions.end()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return result->second.get();
|
return result->second.get();
|
||||||
} ///< 获取外部函数
|
} ///< 获取外部函数
|
||||||
auto getFunctions() -> std::map<std::string, std::unique_ptr<Function>> & { return functions; } ///< 获取函数列表
|
std::map<std::string, std::unique_ptr<Function>>& getFunctions() { return functions; } ///< 获取函数列表
|
||||||
auto getExternalFunctions() const -> const std::map<std::string, std::unique_ptr<Function>> & {
|
const std::map<std::string, std::unique_ptr<Function>>& getExternalFunctions() const {
|
||||||
return externalFunctions;
|
return externalFunctions;
|
||||||
} ///< 获取外部函数列表
|
} ///< 获取外部函数列表
|
||||||
auto getGlobals() -> std::vector<std::unique_ptr<GlobalValue>> & {
|
std::vector<std::unique_ptr<GlobalValue>>& getGlobals() {
|
||||||
return variableTable.getGlobals();
|
return variableTable.getGlobals();
|
||||||
} ///< 获取全局变量列表
|
} ///< 获取全局变量列表
|
||||||
auto getConsts() const -> const std::vector<std::unique_ptr<ConstantVariable>> & {
|
const std::vector<std::unique_ptr<ConstantVariable>>& getConsts() const {
|
||||||
return variableTable.getConsts();
|
return variableTable.getConsts();
|
||||||
} ///< 获取常量列表
|
} ///< 获取常量列表
|
||||||
void enterNewScope() { variableTable.enterNewScope(); } ///< 进入新的作用域
|
void enterNewScope() { variableTable.enterNewScope(); } ///< 进入新的作用域
|
||||||
|
|
||||||
void leaveScope() { variableTable.leaveScope(); } ///< 离开作用域
|
void leaveScope() { variableTable.leaveScope(); } ///< 离开作用域
|
||||||
|
|
||||||
auto isInGlobalArea() const -> bool { return variableTable.isInGlobalScope(); } ///< 是否位于全局作用域
|
bool isInGlobalArea() const { return variableTable.isInGlobalScope(); } ///< 是否位于全局作用域
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|||||||
@@ -39,41 +39,41 @@ class IRBuilder {
|
|||||||
: labelIndex(0), tmpIndex(0), block(block), position(position) {}
|
: labelIndex(0), tmpIndex(0), block(block), position(position) {}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto getLabelIndex() -> unsigned {
|
unsigned getLabelIndex() {
|
||||||
labelIndex += 1;
|
labelIndex += 1;
|
||||||
return labelIndex - 1;
|
return labelIndex - 1;
|
||||||
} ///< 获取基本块标签编号
|
} ///< 获取基本块标签编号
|
||||||
auto getTmpIndex() -> unsigned {
|
unsigned getTmpIndex() {
|
||||||
tmpIndex += 1;
|
tmpIndex += 1;
|
||||||
return tmpIndex - 1;
|
return tmpIndex - 1;
|
||||||
} ///< 获取临时变量编号
|
} ///< 获取临时变量编号
|
||||||
auto getBasicBlock() const -> BasicBlock * { return block; } ///< 获取当前基本块
|
BasicBlock * getBasicBlock() const { return block; } ///< 获取当前基本块
|
||||||
auto getBreakBlock() const -> BasicBlock * { return breakBlocks.back(); } ///< 获取break目标块
|
BasicBlock * getBreakBlock() const { return breakBlocks.back(); } ///< 获取break目标块
|
||||||
auto popBreakBlock() -> BasicBlock * {
|
BasicBlock * popBreakBlock() {
|
||||||
auto result = breakBlocks.back();
|
auto result = breakBlocks.back();
|
||||||
breakBlocks.pop_back();
|
breakBlocks.pop_back();
|
||||||
return result;
|
return result;
|
||||||
} ///< 弹出break目标块
|
} ///< 弹出break目标块
|
||||||
auto getContinueBlock() const -> BasicBlock * { return continueBlocks.back(); } ///< 获取continue目标块
|
BasicBlock * getContinueBlock() const { return continueBlocks.back(); } ///< 获取continue目标块
|
||||||
auto popContinueBlock() -> BasicBlock * {
|
BasicBlock * popContinueBlock() {
|
||||||
auto result = continueBlocks.back();
|
auto result = continueBlocks.back();
|
||||||
continueBlocks.pop_back();
|
continueBlocks.pop_back();
|
||||||
return result;
|
return result;
|
||||||
} ///< 弹出continue目标块
|
} ///< 弹出continue目标块
|
||||||
|
|
||||||
auto getTrueBlock() const -> BasicBlock * { return trueBlocks.back(); } ///< 获取true分支基本块
|
BasicBlock * getTrueBlock() const { return trueBlocks.back(); } ///< 获取true分支基本块
|
||||||
auto getFalseBlock() const -> BasicBlock * { return falseBlocks.back(); } ///< 获取false分支基本块
|
BasicBlock * getFalseBlock() const { return falseBlocks.back(); } ///< 获取false分支基本块
|
||||||
auto popTrueBlock() -> BasicBlock * {
|
BasicBlock * popTrueBlock() {
|
||||||
auto result = trueBlocks.back();
|
auto result = trueBlocks.back();
|
||||||
trueBlocks.pop_back();
|
trueBlocks.pop_back();
|
||||||
return result;
|
return result;
|
||||||
} ///< 弹出true分支基本块
|
} ///< 弹出true分支基本块
|
||||||
auto popFalseBlock() -> BasicBlock * {
|
BasicBlock * popFalseBlock() {
|
||||||
auto result = falseBlocks.back();
|
auto result = falseBlocks.back();
|
||||||
falseBlocks.pop_back();
|
falseBlocks.pop_back();
|
||||||
return result;
|
return result;
|
||||||
} ///< 弹出false分支基本块
|
} ///< 弹出false分支基本块
|
||||||
auto getPosition() const -> BasicBlock::iterator { return position; } ///< 获取当前基本块指令列表位置的迭代器
|
BasicBlock::iterator getPosition() const { return position; } ///< 获取当前基本块指令列表位置的迭代器
|
||||||
void setPosition(BasicBlock *block, BasicBlock::iterator position) {
|
void setPosition(BasicBlock *block, BasicBlock::iterator position) {
|
||||||
this->block = block;
|
this->block = block;
|
||||||
this->position = position;
|
this->position = position;
|
||||||
@@ -87,13 +87,12 @@ class IRBuilder {
|
|||||||
void pushFalseBlock(BasicBlock *block) { falseBlocks.push_back(block); } ///< 压入false分支基本块
|
void pushFalseBlock(BasicBlock *block) { falseBlocks.push_back(block); } ///< 压入false分支基本块
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto insertInst(Instruction *inst) -> Instruction * {
|
Instruction * insertInst(Instruction *inst) {
|
||||||
assert(inst);
|
assert(inst);
|
||||||
block->getInstructions().emplace(position, inst);
|
block->getInstructions().emplace(position, inst);
|
||||||
return inst;
|
return inst;
|
||||||
} ///< 插入指令
|
} ///< 插入指令
|
||||||
auto createUnaryInst(Instruction::Kind kind, Type *type, Value *operand, const std::string &name = "")
|
UnaryInst * createUnaryInst(Instruction::Kind kind, Type *type, Value *operand, const std::string &name = "") {
|
||||||
-> UnaryInst * {
|
|
||||||
std::string newName;
|
std::string newName;
|
||||||
if (name.empty()) {
|
if (name.empty()) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@@ -109,32 +108,31 @@ class IRBuilder {
|
|||||||
block->getInstructions().emplace(position, inst);
|
block->getInstructions().emplace(position, inst);
|
||||||
return inst;
|
return inst;
|
||||||
} ///< 创建一元指令
|
} ///< 创建一元指令
|
||||||
auto createNegInst(Value *operand, const std::string &name = "") -> UnaryInst * {
|
UnaryInst * createNegInst(Value *operand, const std::string &name = "") {
|
||||||
return createUnaryInst(Instruction::kNeg, Type::getIntType(), operand, name);
|
return createUnaryInst(Instruction::kNeg, Type::getIntType(), operand, name);
|
||||||
} ///< 创建取反指令
|
} ///< 创建取反指令
|
||||||
auto createNotInst(Value *operand, const std::string &name = "") -> UnaryInst * {
|
UnaryInst * createNotInst(Value *operand, const std::string &name = "") {
|
||||||
return createUnaryInst(Instruction::kNot, Type::getIntType(), operand, name);
|
return createUnaryInst(Instruction::kNot, Type::getIntType(), operand, name);
|
||||||
} ///< 创建取非指令
|
} ///< 创建取非指令
|
||||||
auto createFtoIInst(Value *operand, const std::string &name = "") -> UnaryInst * {
|
UnaryInst * createFtoIInst(Value *operand, const std::string &name = "") {
|
||||||
return createUnaryInst(Instruction::kFtoI, Type::getIntType(), operand, name);
|
return createUnaryInst(Instruction::kFtoI, Type::getIntType(), operand, name);
|
||||||
} ///< 创建浮点转整型指令
|
} ///< 创建浮点转整型指令
|
||||||
auto createBitFtoIInst(Value *operand, const std::string &name = "") -> UnaryInst * {
|
UnaryInst * createBitFtoIInst(Value *operand, const std::string &name = "") {
|
||||||
return createUnaryInst(Instruction::kBitFtoI, Type::getIntType(), operand, name);
|
return createUnaryInst(Instruction::kBitFtoI, Type::getIntType(), operand, name);
|
||||||
} ///< 创建按位浮点转整型指令
|
} ///< 创建按位浮点转整型指令
|
||||||
auto createFNegInst(Value *operand, const std::string &name = "") -> UnaryInst * {
|
UnaryInst * createFNegInst(Value *operand, const std::string &name = "") {
|
||||||
return createUnaryInst(Instruction::kFNeg, Type::getFloatType(), operand, name);
|
return createUnaryInst(Instruction::kFNeg, Type::getFloatType(), operand, name);
|
||||||
} ///< 创建浮点取反指令
|
} ///< 创建浮点取反指令
|
||||||
auto createFNotInst(Value *operand, const std::string &name = "") -> UnaryInst * {
|
UnaryInst * createFNotInst(Value *operand, const std::string &name = "") {
|
||||||
return createUnaryInst(Instruction::kFNot, Type::getIntType(), operand, name);
|
return createUnaryInst(Instruction::kFNot, Type::getIntType(), operand, name);
|
||||||
} ///< 创建浮点取非指令
|
} ///< 创建浮点取非指令
|
||||||
auto createIToFInst(Value *operand, const std::string &name = "") -> UnaryInst * {
|
UnaryInst * createIToFInst(Value *operand, const std::string &name = "") {
|
||||||
return createUnaryInst(Instruction::kItoF, Type::getFloatType(), operand, name);
|
return createUnaryInst(Instruction::kItoF, Type::getFloatType(), operand, name);
|
||||||
} ///< 创建整型转浮点指令
|
} ///< 创建整型转浮点指令
|
||||||
auto createBitItoFInst(Value *operand, const std::string &name = "") -> UnaryInst * {
|
UnaryInst * createBitItoFInst(Value *operand, const std::string &name = "") {
|
||||||
return createUnaryInst(Instruction::kBitItoF, Type::getFloatType(), operand, name);
|
return createUnaryInst(Instruction::kBitItoF, Type::getFloatType(), operand, name);
|
||||||
} ///< 创建按位整型转浮点指令
|
} ///< 创建按位整型转浮点指令
|
||||||
auto createBinaryInst(Instruction::Kind kind, Type *type, Value *lhs, Value *rhs, const std::string &name = "")
|
BinaryInst * createBinaryInst(Instruction::Kind kind, Type *type, Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
-> BinaryInst * {
|
|
||||||
std::string newName;
|
std::string newName;
|
||||||
if (name.empty()) {
|
if (name.empty()) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@@ -150,76 +148,76 @@ class IRBuilder {
|
|||||||
block->getInstructions().emplace(position, inst);
|
block->getInstructions().emplace(position, inst);
|
||||||
return inst;
|
return inst;
|
||||||
} ///< 创建二元指令
|
} ///< 创建二元指令
|
||||||
auto createAddInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createAddInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kAdd, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kAdd, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建加法指令
|
} ///< 创建加法指令
|
||||||
auto createSubInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createSubInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kSub, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kSub, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建减法指令
|
} ///< 创建减法指令
|
||||||
auto createMulInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createMulInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kMul, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kMul, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建乘法指令
|
} ///< 创建乘法指令
|
||||||
auto createDivInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createDivInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kDiv, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kDiv, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建除法指令
|
} ///< 创建除法指令
|
||||||
auto createRemInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createRemInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kRem, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kRem, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建取余指令
|
} ///< 创建取余指令
|
||||||
auto createICmpEQInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createICmpEQInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kICmpEQ, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kICmpEQ, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建相等设置指令
|
} ///< 创建相等设置指令
|
||||||
auto createICmpNEInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createICmpNEInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kICmpNE, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kICmpNE, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建不相等设置指令
|
} ///< 创建不相等设置指令
|
||||||
auto createICmpLTInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createICmpLTInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kICmpLT, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kICmpLT, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建小于设置指令
|
} ///< 创建小于设置指令
|
||||||
auto createICmpLEInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createICmpLEInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kICmpLE, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kICmpLE, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建小于等于设置指令
|
} ///< 创建小于等于设置指令
|
||||||
auto createICmpGTInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createICmpGTInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kICmpGT, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kICmpGT, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建大于设置指令
|
} ///< 创建大于设置指令
|
||||||
auto createICmpGEInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createICmpGEInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kICmpGE, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kICmpGE, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建大于等于设置指令
|
} ///< 创建大于等于设置指令
|
||||||
auto createFAddInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createFAddInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kFAdd, Type::getFloatType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kFAdd, Type::getFloatType(), lhs, rhs, name);
|
||||||
} ///< 创建浮点加法指令
|
} ///< 创建浮点加法指令
|
||||||
auto createFSubInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createFSubInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kFSub, Type::getFloatType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kFSub, Type::getFloatType(), lhs, rhs, name);
|
||||||
} ///< 创建浮点减法指令
|
} ///< 创建浮点减法指令
|
||||||
auto createFMulInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createFMulInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kFMul, Type::getFloatType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kFMul, Type::getFloatType(), lhs, rhs, name);
|
||||||
} ///< 创建浮点乘法指令
|
} ///< 创建浮点乘法指令
|
||||||
auto createFDivInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createFDivInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kFDiv, Type::getFloatType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kFDiv, Type::getFloatType(), lhs, rhs, name);
|
||||||
} ///< 创建浮点除法指令
|
} ///< 创建浮点除法指令
|
||||||
auto createFCmpEQInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createFCmpEQInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kFCmpEQ, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kFCmpEQ, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建浮点相等设置指令
|
} ///< 创建浮点相等设置指令
|
||||||
auto createFCmpNEInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createFCmpNEInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kFCmpNE, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kFCmpNE, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建浮点不相等设置指令
|
} ///< 创建浮点不相等设置指令
|
||||||
auto createFCmpLTInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createFCmpLTInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kFCmpLT, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kFCmpLT, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建浮点小于设置指令
|
} ///< 创建浮点小于设置指令
|
||||||
auto createFCmpLEInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createFCmpLEInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kFCmpLE, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kFCmpLE, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建浮点小于等于设置指令
|
} ///< 创建浮点小于等于设置指令
|
||||||
auto createFCmpGTInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createFCmpGTInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kFCmpGT, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kFCmpGT, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建浮点大于设置指令
|
} ///< 创建浮点大于设置指令
|
||||||
auto createFCmpGEInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createFCmpGEInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kFCmpGE, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kFCmpGE, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建浮点相大于等于设置指令
|
} ///< 创建浮点相大于等于设置指令
|
||||||
auto createAndInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createAndInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kAnd, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kAnd, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建按位且指令
|
} ///< 创建按位且指令
|
||||||
auto createOrInst(Value *lhs, Value *rhs, const std::string &name = "") -> BinaryInst * {
|
BinaryInst * createOrInst(Value *lhs, Value *rhs, const std::string &name = "") {
|
||||||
return createBinaryInst(Instruction::kOr, Type::getIntType(), lhs, rhs, name);
|
return createBinaryInst(Instruction::kOr, Type::getIntType(), lhs, rhs, name);
|
||||||
} ///< 创建按位或指令
|
} ///< 创建按位或指令
|
||||||
auto createCallInst(Function *callee, const std::vector<Value *> &args, const std::string &name = "") -> CallInst * {
|
CallInst * createCallInst(Function *callee, const std::vector<Value *> &args, const std::string &name = "") {
|
||||||
std::string newName;
|
std::string newName;
|
||||||
if (name.empty() && callee->getReturnType() != Type::getVoidType()) {
|
if (name.empty() && callee->getReturnType() != Type::getVoidType()) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@@ -235,40 +233,38 @@ class IRBuilder {
|
|||||||
block->getInstructions().emplace(position, inst);
|
block->getInstructions().emplace(position, inst);
|
||||||
return inst;
|
return inst;
|
||||||
} ///< 创建Call指令
|
} ///< 创建Call指令
|
||||||
auto createReturnInst(Value *value = nullptr, const std::string &name = "") -> ReturnInst * {
|
ReturnInst * createReturnInst(Value *value = nullptr, const std::string &name = "") {
|
||||||
auto inst = new ReturnInst(value, block, name);
|
auto inst = new ReturnInst(value, block, name);
|
||||||
assert(inst);
|
assert(inst);
|
||||||
block->getInstructions().emplace(position, inst);
|
block->getInstructions().emplace(position, inst);
|
||||||
return inst;
|
return inst;
|
||||||
} ///< 创建return指令
|
} ///< 创建return指令
|
||||||
auto createUncondBrInst(BasicBlock *thenBlock, const std::vector<Value *> &args) -> UncondBrInst * {
|
UncondBrInst * createUncondBrInst(BasicBlock *thenBlock, const std::vector<Value *> &args) {
|
||||||
auto inst = new UncondBrInst(thenBlock, args, block);
|
auto inst = new UncondBrInst(thenBlock, args, block);
|
||||||
assert(inst);
|
assert(inst);
|
||||||
block->getInstructions().emplace(position, inst);
|
block->getInstructions().emplace(position, inst);
|
||||||
return inst;
|
return inst;
|
||||||
} ///< 创建无条件指令
|
} ///< 创建无条件指令
|
||||||
auto createCondBrInst(Value *condition, BasicBlock *thenBlock, BasicBlock *elseBlock,
|
CondBrInst * createCondBrInst(Value *condition, BasicBlock *thenBlock, BasicBlock *elseBlock,
|
||||||
const std::vector<Value *> &thenArgs, const std::vector<Value *> &elseArgs) -> CondBrInst * {
|
const std::vector<Value *> &thenArgs, const std::vector<Value *> &elseArgs) {
|
||||||
auto inst = new CondBrInst(condition, thenBlock, elseBlock, thenArgs, elseArgs, block);
|
auto inst = new CondBrInst(condition, thenBlock, elseBlock, thenArgs, elseArgs, block);
|
||||||
assert(inst);
|
assert(inst);
|
||||||
block->getInstructions().emplace(position, inst);
|
block->getInstructions().emplace(position, inst);
|
||||||
return inst;
|
return inst;
|
||||||
} ///< 创建条件跳转指令
|
} ///< 创建条件跳转指令
|
||||||
auto createAllocaInst(Type *type, const std::vector<Value *> &dims = {}, const std::string &name = "")
|
AllocaInst * createAllocaInst(Type *type, const std::vector<Value *> &dims = {}, const std::string &name = "") {
|
||||||
-> AllocaInst * {
|
|
||||||
auto inst = new AllocaInst(type, dims, block, name);
|
auto inst = new AllocaInst(type, dims, block, name);
|
||||||
assert(inst);
|
assert(inst);
|
||||||
block->getInstructions().emplace(position, inst);
|
block->getInstructions().emplace(position, inst);
|
||||||
return inst;
|
return inst;
|
||||||
} ///< 创建分配指令
|
} ///< 创建分配指令
|
||||||
auto createAllocaInstWithoutInsert(Type *type, const std::vector<Value *> &dims = {}, BasicBlock *parent = nullptr,
|
AllocaInst * createAllocaInstWithoutInsert(Type *type, const std::vector<Value *> &dims = {}, BasicBlock *parent = nullptr,
|
||||||
const std::string &name = "") -> AllocaInst * {
|
const std::string &name = "") {
|
||||||
auto inst = new AllocaInst(type, dims, parent, name);
|
auto inst = new AllocaInst(type, dims, parent, name);
|
||||||
assert(inst);
|
assert(inst);
|
||||||
return inst;
|
return inst;
|
||||||
} ///< 创建不插入指令列表的分配指令
|
} ///< 创建不插入指令列表的分配指令
|
||||||
auto createLoadInst(Value *pointer, const std::vector<Value *> &indices = {}, const std::string &name = "")
|
LoadInst * createLoadInst(Value *pointer, const std::vector<Value *> &indices = {}, const std::string &name = "") {
|
||||||
-> LoadInst * {
|
|
||||||
std::string newName;
|
std::string newName;
|
||||||
if (name.empty()) {
|
if (name.empty()) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@@ -284,8 +280,7 @@ class IRBuilder {
|
|||||||
block->getInstructions().emplace(position, inst);
|
block->getInstructions().emplace(position, inst);
|
||||||
return inst;
|
return inst;
|
||||||
} ///< 创建load指令
|
} ///< 创建load指令
|
||||||
auto createLaInst(Value *pointer, const std::vector<Value *> &indices = {}, const std::string &name = "")
|
LaInst * createLaInst(Value *pointer, const std::vector<Value *> &indices = {}, const std::string &name = "") {
|
||||||
-> LaInst * {
|
|
||||||
std::string newName;
|
std::string newName;
|
||||||
if (name.empty()) {
|
if (name.empty()) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@@ -301,8 +296,7 @@ class IRBuilder {
|
|||||||
block->getInstructions().emplace(position, inst);
|
block->getInstructions().emplace(position, inst);
|
||||||
return inst;
|
return inst;
|
||||||
} ///< 创建la指令
|
} ///< 创建la指令
|
||||||
auto createGetSubArray(LVal *fatherArray, const std::vector<Value *> &indices, const std::string &name = "")
|
GetSubArrayInst * createGetSubArray(LVal *fatherArray, const std::vector<Value *> &indices, const std::string &name = "") {
|
||||||
-> GetSubArrayInst * {
|
|
||||||
assert(fatherArray->getLValNumDims() > indices.size());
|
assert(fatherArray->getLValNumDims() > indices.size());
|
||||||
std::vector<Value *> subDims;
|
std::vector<Value *> subDims;
|
||||||
auto dims = fatherArray->getLValDims();
|
auto dims = fatherArray->getLValDims();
|
||||||
@@ -326,21 +320,20 @@ class IRBuilder {
|
|||||||
block->getInstructions().emplace(position, inst);
|
block->getInstructions().emplace(position, inst);
|
||||||
return inst;
|
return inst;
|
||||||
} ///< 创建获取部分数组指令
|
} ///< 创建获取部分数组指令
|
||||||
auto createMemsetInst(Value *pointer, Value *begin, Value *size, Value *value, const std::string &name = "")
|
MemsetInst * createMemsetInst(Value *pointer, Value *begin, Value *size, Value *value, const std::string &name = "") {
|
||||||
-> MemsetInst * {
|
|
||||||
auto inst = new MemsetInst(pointer, begin, size, value, block, name);
|
auto inst = new MemsetInst(pointer, begin, size, value, block, name);
|
||||||
assert(inst);
|
assert(inst);
|
||||||
block->getInstructions().emplace(position, inst);
|
block->getInstructions().emplace(position, inst);
|
||||||
return inst;
|
return inst;
|
||||||
} ///< 创建memset指令
|
} ///< 创建memset指令
|
||||||
auto createStoreInst(Value *value, Value *pointer, const std::vector<Value *> &indices = {},
|
StoreInst * createStoreInst(Value *value, Value *pointer, const std::vector<Value *> &indices = {},
|
||||||
const std::string &name = "") -> StoreInst * {
|
const std::string &name = "") {
|
||||||
auto inst = new StoreInst(value, pointer, indices, block, name);
|
auto inst = new StoreInst(value, pointer, indices, block, name);
|
||||||
assert(inst);
|
assert(inst);
|
||||||
block->getInstructions().emplace(position, inst);
|
block->getInstructions().emplace(position, inst);
|
||||||
return inst;
|
return inst;
|
||||||
} ///< 创建store指令
|
} ///< 创建store指令
|
||||||
auto createPhiInst(Type *type, Value *lhs, BasicBlock *parent, const std::string &name = "") -> PhiInst * {
|
PhiInst * createPhiInst(Type *type, Value *lhs, BasicBlock *parent, const std::string &name = "") {
|
||||||
auto predNum = parent->getNumPredecessors();
|
auto predNum = parent->getNumPredecessors();
|
||||||
std::vector<Value *> rhs;
|
std::vector<Value *> rhs;
|
||||||
for (size_t i = 0; i < predNum; i++) {
|
for (size_t i = 0; i < predNum; i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user