📄 cfuncoptim.cpp
字号:
}
}
else if (p->type == i_Label)
{
log_prtl(" %x:", p->label.label_off);
}
else if (p->type == i_Return)
{
log_prtl(" ret");
}
else
{
CFunc_Prt theprt(this->Q);
CString s = theprt.prt_the_instr(p);
log_prtl(" %s", (PCSTR)s);
}
}
log_prtl("%s","===========");
}
bool RemoveOneInstr_1(VAROPTM_LIST& used_list, PINSTR p)
{
assert(p->type == i_Jump);
POSITION pos = used_list.GetHeadPosition();
while (pos)
{
POSITION savpos = pos;
st_VarOptm* the = used_list.GetNext(pos);
if (p == the->pinstr && the->bJxx)
{
used_list.RemoveAt(savpos);
return true;
}
}
return false;
}
bool RemoveOneInstr(VAROPTM_LIST& used_list, PINSTR p)
{
bool rtn = false;
rtn |= RemoveOneInstr_1(used_list, p);
while (p->jmp.next_ref_of_this_label)
{
p = p->jmp.next_ref_of_this_label;
rtn |= RemoveOneInstr_1(used_list, p);
}
return rtn;
}
bool CFuncOptim::Optim_Var_Flow_3(VAROPTM_LIST& used_list)
{
//连续两个EE,要删掉一个
bool FindE = false;
POSITION pos = used_list.GetHeadPosition();
while (pos)
{
POSITION savpos = pos;
st_VarOptm* the = used_list.GetNext(pos);
if (the->pinstr->type == i_End || the->pinstr->type == i_Return)
{
if (the->rw == 0)
{
if (FindE)
{
used_list.RemoveAt(savpos);
return true;
}
FindE = true;
continue;
}
}
FindE = false;
}
return false;
}
bool CFuncOptim::Optim_Var_Flow_2(VAROPTM_LIST& used_list)
{
//如果有一个LE,则删掉这个指向LE的jmp
//如果有一个L6,则删掉这个指向L6的jmp
PINSTR lastlabelinstr = NULL;
POSITION pos = used_list.GetHeadPosition();
while (pos)
{
st_VarOptm* the = used_list.GetNext(pos);
if (lastlabelinstr != NULL)
{
bool f = false;
if (the->pinstr->type == i_End || the->pinstr->type == i_Return)
{
if (the->rw == 0)
{//找到一个LE
f=true;
}
}
if (the->rw == 2) //rw == 2 means write
{
f=true;
}
if (f)
{
PINSTR p = lastlabelinstr->label.ref_instr;
if (RemoveOneInstr(used_list, p)) //删掉这个指向LE的jmp
return true;
}
}
lastlabelinstr = NULL;
if (the->pinstr->type == i_Label)
lastlabelinstr = the->pinstr;
}
return false;
}
bool CFuncOptim::Optim_Var_Flow_1(VAROPTM_LIST& used_list)
{
//第一条就是条件跳,可去掉
//跳到第一条了,可去掉
POSITION pos = used_list.GetHeadPosition();
POSITION headpos = pos;
PINSTR head_label_instr = NULL;
while (pos)
{
POSITION savpos = pos;
st_VarOptm* the = used_list.GetNext(pos);
PINSTR p = the->pinstr;
if (headpos == savpos)
{
if (p->type == i_Return)
{
used_list.RemoveAt(savpos); //remove me
return true; //第一条就是Return,可去掉
}
if (the->IsJump())
{
used_list.RemoveAt(savpos); //remove me
return true; //第一条就是条件跳,可去掉
}
if (p->type == i_Label)
{
head_label_instr = p;
continue;
}
break;
}
if (the->IsJump())
{
if (p->jmp.the_label == head_label_instr)
{
used_list.RemoveAt(savpos); //remove me
return true; //跳到第一条了,可去掉
}
}
}
return false;
}
bool CFuncOptim::Optim_Var_Flow(VAROPTM_LIST& used_list)
{
//这些函数,都只允许删used_list中的项,不允许删instr_list中的项
if (this->Optim_Var_Flow_1(used_list))
return true;
if (this->Optim_Var_Flow_2(used_list))
return true;
if (this->Optim_Var_Flow_3(used_list))
return true;
if (this->Optim_Var_Flow_4(used_list))
return true;
if (this->Optim_Var_Flow_5(used_list))
return true;
return false;
}
st_VarOptm* Find_Instr(VAROPTM_LIST& used_list, PINSTR pinstr)
{
POSITION pos = used_list.GetHeadPosition();
while (pos)
{
st_VarOptm* the = used_list.GetNext(pos);
if (the->pinstr == pinstr)
return the;
}
return NULL;
}
bool CFuncOptim::SureNotUse_1(VAROPTM_LIST& used_list, st_VarOptm* the)
{
if (the->tem_1)
return true;
the->tem_1 = true;
if (the->rw == 2)
return true;
if (the->rw & 1)
return false;
if (the->pinstr->type == i_Return)
return true;
if (the->bJxx)
{
if (!SureNotUse_1(used_list,
Find_Instr(used_list, the->pinstr->jmp.the_label)
))
return false;
}
POSITION pos = used_list.Find(the);
used_list.GetNext(pos);
if (pos == NULL)
return true;
st_VarOptm* the1 = used_list.GetNext(pos);
return SureNotUse_1(used_list,the1);
}
bool CFuncOptim::SureNotUse(VAROPTM_LIST& used_list, st_VarOptm* j)
{
assert(j->bJxx);
{
POSITION pos = used_list.GetHeadPosition();
while (pos)
{
st_VarOptm* the = used_list.GetNext(pos);
the->tem_1 = false;
}
}
return SureNotUse_1(used_list,j);
}
bool CFuncOptim::Optim_Var_Flow_5(VAROPTM_LIST& used_list)
{//这是最难的
POSITION pos = used_list.GetHeadPosition();
while (pos)
{
POSITION savpos = pos;
st_VarOptm* the = used_list.GetNext(pos);
if (the->bJxx)
{
if (this->SureNotUse(used_list, the))
{
used_list.RemoveAt(savpos);
return true;
}
}
}
return false;
}
bool CFuncOptim::Optim_Var_Flow_4(VAROPTM_LIST& used_list)
{
//空的下跳,要删
//空的上跳,要删
//没人用的Label,要删
POSITION last_pos;
PINSTR last_jmp = NULL;
PINSTR last_label[256];
int n_label = 0;
POSITION pos = used_list.GetHeadPosition();
while (pos)
{
POSITION savpos = pos;
st_VarOptm* the = used_list.GetNext(pos);
PINSTR p = the->pinstr;
if (the->IsJump())
{
for (int i=0; i<n_label; i++)
{
if (last_label[i] != NULL && p->jmp.the_label == last_label[i])
{
used_list.RemoveAt(savpos); //remove me
return true;
}
}
last_jmp = p;
last_pos = savpos;
n_label = 0;
}
else if (p->type == i_Label)
{
if (last_jmp != NULL && last_jmp->jmp.the_label == p)
{
used_list.RemoveAt(last_pos);
return true;
}
last_label[n_label++] = p;
}
else
{
last_jmp = NULL;
n_label = 0;
}
}
//没人用的空的label要删掉
pos = used_list.GetHeadPosition();
while (pos)
{
POSITION savpos = pos;
st_VarOptm* the = used_list.GetNext(pos);
PINSTR p = the->pinstr;
if (p->type == i_Label)
{
bool used = false;
POSITION pos1 = used_list.GetHeadPosition();
while (pos1)
{
st_VarOptm* the1 = used_list.GetNext(pos1);
PINSTR p1 = the1->pinstr;
if (the1->IsJump())
{
if (p1->jmp.the_label == p)
{
used = true;
break;
}
}
}
if (!used)
{
used_list.RemoveAt(savpos);
return true;
}
}
}
return false;
}
char HowVarUse_Char(st_VarOptm* the);
CString CFuncOptim::Get_var_finger_NT(VAROPTM_LIST& volist, M_t* pvar)
{
CString tbl_c;
POSITION pos = volist.GetHeadPosition();
while (pos)
{
st_VarOptm* the = volist.GetNext(pos);
char b = HowVarUse_Char(the);
tbl_c += b;
}
return tbl_c;
}
char HowVarUse_Char(st_VarOptm* the)
{
if (the->bJxx)
return 'J';
PINSTR pinstr = the->pinstr;
BYTE rw = the->rw;
if (pinstr->type == i_Label) return 'L';
if (pinstr->type == i_Return) return 'E';
if (pinstr->type == i_Assign)
{
if (rw == 1) return '5'; //Read
if (rw == 2) return '6'; //Write
if (rw == 3) return '7'; //Read and Write
}
else
{
if (rw == 1) return '1'; //Read
if (rw == 2) return '2'; //Write
if (rw == 3) return '3'; //Read and Write
}
if (rw >= 4) return 'u';
assert(0);
return 'U'; //不应该到这里
}
bool CFuncOptim::Optim_var_flow_NT(VAROPTM_LIST& volist, M_t* pvar)
//SuperC_func: 只在<CFuncOptim::Optim_var_NT>中使用
{
int n = 0;
char tbl_c[256];
PINSTR tbl_pinstr[256];
POSITION pos = volist.GetHeadPosition();
while (pos)
{
st_VarOptm* the = volist.GetNext(pos);
char b = HowVarUse_Char(the);
tbl_c[n] = b; tbl_c[n+1] = '\0';
tbl_pinstr[n] = the->pinstr;
n++;
if (n >= 1)
{
const char* pb = tbl_c + n - 1;
PINSTR* pi = tbl_pinstr + n - 1;
if (strcmp(pb, "7") == 0 && pi[0]->va_r1.pao == NULL)
{//自己assign自己
g_CStrategy.AddOne_CanDelete(pvar, pi[0], "assiang self, delete it");
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -