Commit 1eba042f authored by 蒋子豪's avatar 蒋子豪
Browse files

Upload New File

parent 84aa433c
No related merge requests found
Showing with 179 additions and 0 deletions
+179 -0
#include "CombineRedundantInstFromCons.h"
#include "Help.h"
#define DBG 0
void CombineRedundantInstFromCons::execute()
{
for (auto fun : module->function_list_)
{
if (fun->basic_blocks_.empty())
continue;
f = fun;
for (BasicBlock *bb : fun->basic_blocks_)
{
// check(bb); //只处理 只用1次的变量
check2(bb); //对每个变量检查use,同类可合并加减法的常数,一个全部use被处理,则可删除
}
}
}
///主要流程:基本块内指令合并
///
///此时已经经过phi前优化(消去了一定的alloca但可能仍有alloca,可能有phi)
int CombineRedundantInstFromCons::check(BasicBlock *bb)
{
// 1.针对相邻加/相邻减
// auto ref = bb->instr_list_.end();
Instruction *inst2 = *(bb->instr_list_.begin());
for (auto it = bb->instr_list_.begin(); it != bb->instr_list_.end();)
{
Instruction *inst = *(it++);
if (inst->name_ == "v56")
inst2 = inst;
if (inst->op_id_ != Instruction::Add && inst->op_id_ != Instruction::Sub) //只处理加减
continue;
if (inst->use_list_.size() != 1) //只处理被用过一次的“临时的”局部变量
continue;
Instruction *next_inst = dynamic_cast<Instruction *>(inst->use_list_.back().val_);
unsigned int arg_no = inst->use_list_.back().arg_no_; //以inst的操作数位置
unsigned int arg_no_combine = (arg_no != 0) ? 0 : 1;
if (!next_inst)
varDBG(next_inst, "常数加减指令合并:use.val为啥不是指令");
if (inst->op_id_ != next_inst->op_id_) //只处理同时加或者同时减法
continue;
//判断常量操作数
Value *opnd_inst0 = inst->get_operand(0);
Value *opnd_inst1 = inst->get_operand(1);
Value *opnd_next_inst = next_inst->get_operand(arg_no_combine); //不能是以inst的操作数位置
Constant *cons_opnd_inst0 = dynamic_cast<Constant *>(opnd_inst0);
Constant *cons_opnd_inst1 = dynamic_cast<Constant *>(opnd_inst1);
Constant *cons_opnd_next_inst = dynamic_cast<Constant *>(opnd_next_inst);
Constant *cont_opnd_inst_combine;
Value *src_opnd_inst;
if (!cons_opnd_next_inst) // nextinst另一个操作数必须是常数
continue;
if (cons_opnd_inst1)
{ // inst中选常数操作数作为被合并的操作数
cont_opnd_inst_combine = cons_opnd_inst1;
src_opnd_inst = opnd_inst0;
}
else if (cons_opnd_inst0)
{
cont_opnd_inst_combine = cons_opnd_inst0;
src_opnd_inst = opnd_inst1;
}
else
{
continue;
}
inst->replace_all_use_with(src_opnd_inst); //删除inst,替换成源头value,之前替换错了
if (constCompute(next_inst, arg_no_combine, cont_opnd_inst_combine, Instruction::Add) != 1){
varDBG(next_inst, "!1准备常数加减指令合并的常量计算出错!!");
continue;
}
bb->delete_instr(inst);
}
#if DBG
// std::cout<<"("<<bb->parent_->name_<<")"<<bb->name_<<" 指令合并个数 "<<deleteInstVec.size()<<std::endl;
#endif
return 0;
}
int CombineRedundantInstFromCons::check2(BasicBlock *bb)
{
// 1.针对相邻加/相邻减
// auto ref = bb->instr_list_.end();
Instruction *inst2 = *(bb->instr_list_.begin());
for (auto it = bb->instr_list_.begin(); it != bb->instr_list_.end();)
{
Instruction *inst = *(it++);
if (inst->name_ == "v56")
inst2 = inst;
if (inst->op_id_ != Instruction::Add && inst->op_id_ != Instruction::Sub) //只处理加减
continue;
if(inst->use_list_.size() == 0) { //死代码删掉
bb->delete_instr(inst);
continue;
}
Value *opnd_inst0 = inst->get_operand(0);
Value *opnd_inst1 = inst->get_operand(1);
Constant *cons_opnd_inst0 = dynamic_cast<Constant *>(opnd_inst0);
Constant *cons_opnd_inst1 = dynamic_cast<Constant *>(opnd_inst1);
Constant *cont_opnd_inst_combine;
Value *src_opnd_inst;
if (cons_opnd_inst1)
{ // inst中选常数操作数作为被合并的操作数
cont_opnd_inst_combine = cons_opnd_inst1;
src_opnd_inst = opnd_inst0;
}
else if (cons_opnd_inst0)
{
cont_opnd_inst_combine = cons_opnd_inst0;
src_opnd_inst = opnd_inst1;
}
else
continue;
unsigned int cnt = 0; //记录一个inst成功处理的nextinst个数
unsigned int original_uselist_size = inst->use_list_.size();
for (auto uit = inst->use_list_.begin(); uit!=inst->use_list_.end() ; ) //因为可能要删除 inst->use_list_ 的元素,不能这么便利
{
auto &u = *(uit++);
Instruction *next_inst = dynamic_cast<Instruction *>(u.val_);
unsigned int arg_no_combine = (u.arg_no_ != 0) ? 0 : 1;
if (!next_inst)
{
varDBG(next_inst, "常数加减指令合并:use.val为啥不是指令");
continue;
}
if (inst->op_id_ != next_inst->op_id_) //只处理同时加或者同时减法
continue;
Value *opnd_next_inst = next_inst->get_operand(arg_no_combine); //不能是以inst的操作数位置
Constant *cons_opnd_next_inst = dynamic_cast<Constant *>(opnd_next_inst);
if (!cons_opnd_next_inst) // nextinst另一个操作数必须是常数
continue;
//处理nextinst,暂时不处理inst
if (constCompute(next_inst, arg_no_combine, cont_opnd_inst_combine, Instruction::Add) != 1)
{
varDBG(next_inst, "!1准备常数加减指令合并的常量计算出错!!");
continue;
}
//next不再使用inst,需要修改inst的(在nextinst处)use记录
inst->remove_used(next_inst,u.arg_no_);
next_inst->set_operand(u.arg_no_, src_opnd_inst);
cnt++;
}
if(cnt == original_uselist_size)//处理了全部use,则可以删除inst
{
bb->delete_instr(inst);
}
// inst->replace_all_use_with(src_opnd_inst); //删除inst,替换成源头value,之前替换错了
// if (constCompute(next_inst, arg_no_combine, cont_opnd_inst_combine, Instruction::Add) != 1)
// varDBG(next_inst, "!1准备常数加减指令合并的常量计算出错!!");
}
#if DBG
// std::cout<<"("<<bb->parent_->name_<<")"<<bb->name_<<" 指令合并个数 "<<deleteInstVec.size()<<std::endl;
#endif
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment