void Optimizer::deleteUselessMovs(AsmCode& code){ for (int i = 0; i < code.size(); i++){ if (*code[i] != cmdMOV || !code[i]->usedRegister(EAX)) continue; AsmCmd2* cmd = dynamic_cast<AsmCmd2*>(code[i]); if (cmd->secondArg()->usedRegister(EAX)) continue; if (*cmd->firstArg() != EAX) continue; int idx = i + 1; bool deletingNedeed = true; while (idx < code.size() && deletingNedeed){ if (code[idx]->usedRegister(EAX)) if (*code[idx] != cmdMOV) deletingNedeed = false; else { AsmCmd2* tmp = dynamic_cast<AsmCmd2*>(code[idx]); if (tmp->secondArg()->usedRegister(EAX) || dynamic_cast<AsmIndirectArg*>(tmp->firstArg())) deletingNedeed = false; else break; } idx++; } if (deletingNedeed) code.deleteRange(i, i); } }
void Optimizer::pushDownPopUp(AsmCode& code){ for (int i = 0; i < code.size(); i++){ AsmCmd1* cmd = dynamic_cast<AsmCmd1*>(code[i]); int j = i; if (cmd && *cmd == cmdPUSH){ while (j + 1 < code.size() && !code[j + 1]->changeStack() && !code[j + 1]->operateWith(cmd->argument())) j++; code.move(i, j); } else if (cmd && *cmd == cmdPOP) { while (j - 1 > -1 && !code[j - 1]->changeStack() && !code[j - 1]->operateWith(cmd->argument())) j--; code.move(i, j); } } }
void Optimizer::deleteUselessLabels(AsmCode& code){ for (int i = 0; i < code.size(); i++){ AsmInstrLabel* label = dynamic_cast<AsmInstrLabel*>(code[i]); if (!label || *label == string("start")) continue; bool unused = true; for (int j = 0; j < code.size() && unused; j++){ if (j == i) continue; if (code[j]->isJump()){ AsmArgLabel* dstn = dynamic_cast<AsmArgLabel*>(dynamic_cast<AsmCmd1*>(code[j])->argument()); unused = *label->label != dstn; } } if (unused) code.deleteRange(i, i); } }
void Optimizer::optimize(AsmCode& code){ while(true) { pushDownPopUp(code); bool goToNextIteration = false; for (int i = 0; i < code.size(); i++) for (int j = 0; j < oneOpOpts.size(); j++) if (oneOpOpts[j]->optimize(code, i)) goToNextIteration = true; if(goToNextIteration) continue; for(int i = 0; i < code.size() - 1; i++) for (int j = 0; j < twoOpOpts.size(); j++) if (twoOpOpts[j]->optimize(code, i)) goToNextIteration = true; if(goToNextIteration) continue; for(int i = 0; i < code.size() - 2; i++) for(int j = 0; j < threeOpOpts.size(); j++) if (threeOpOpts[j]->optimize(code, i)) goToNextIteration = true; for(int i = 0; i < code.size() - 3; i++) for (int j = 0; j < fourOpOpts.size(); j++) if (fourOpOpts[j]->optimize(code, i)) goToNextIteration = true; if(!goToNextIteration) break; } deleteUselessMovs(code); deleteUselessLabels(code); while(true){ bool goToNextIteration = false; for (int i = 0; i < code.size() - 1; i++) for (int j = 0; j < postTwoOpOpts.size(); j++) if (postTwoOpOpts[j]->optimize(code, i)) goToNextIteration = true; if (goToNextIteration) continue; else break; } }