void MiniConstantPropagationPass::ProcessList(StatementList* s) { assert(s != NULL) ; bool changed ; do { changed = false ; // Go through each statement until we either hit the end or we // hit a branch point. Keep track of all constants we have identified // and propagate them as appropriate std::map<VariableSymbol*, Constant*> propagateConstants ; for (int i = 0 ; i < s->get_statement_count() ; ++i) { Statement* currentStatement = s->get_statement(i) ; // Check to see if we are at a break point if (dynamic_cast<StoreStatement*>(currentStatement) == NULL && dynamic_cast<StoreVariableStatement*>(currentStatement) == NULL && dynamic_cast<CallStatement*>(currentStatement) == NULL && dynamic_cast<LabelLocationStatement*>(currentStatement) == NULL) { break ; } // Make any replacements as necessary list<LoadVariableExpression*>* allLoadVars = collect_objects<LoadVariableExpression>(currentStatement) ; list<LoadVariableExpression*>::iterator loadVarIter = allLoadVars->begin() ; while (loadVarIter != allLoadVars->end()) { if (propagateConstants[(*loadVarIter)->get_source()] != NULL) { (*loadVarIter)->get_parent()->replace((*loadVarIter), dynamic_cast<Expression*>(propagateConstants[(*loadVarIter)->get_source()]->deep_clone())) ; changed = true ; } ++loadVarIter ; } delete allLoadVars ; // Adjust the map depending on the variable stored if applicable if (dynamic_cast<StoreVariableStatement*>(currentStatement) != NULL) { StoreVariableStatement* currentStoreVar = dynamic_cast<StoreVariableStatement*>(currentStatement) ; if (dynamic_cast<Constant*>(currentStoreVar->get_value()) != NULL) { propagateConstants[currentStoreVar->get_destination()] = dynamic_cast<Constant*>(currentStoreVar->get_value()) ; } else { propagateConstants[currentStoreVar->get_destination()] = NULL ; } } } changed |= MiniFold(s) ; } while (changed == true) ; }
static String handle_static_store_variable_statement(CPrintStyleModule *state, const SuifObject *obj) { StoreVariableStatement *stmt = to<StoreVariableStatement>(obj); String return_str = state->print_to_string(stmt->get_destination()) + " = "; String op = state->print_to_string(stmt->get_value()); return_str += op; return(return_str); }
bool IfConversionPass2::ConvertStoreVariableIf(IfStatement* toConvert) { Statement* thenPart = Denormalize(toConvert->get_then_part()) ; Statement* elsePart = Denormalize(toConvert->get_else_part()) ; StoreVariableStatement* thenStoreVar = dynamic_cast<StoreVariableStatement*>(thenPart) ; StoreVariableStatement* elseStoreVar = dynamic_cast<StoreVariableStatement*>(elsePart) ; assert(thenStoreVar != NULL) ; assert(elseStoreVar != NULL) ; // Make sure that the variables are the same. if (thenStoreVar->get_destination() != elseStoreVar->get_destination()) { return false ; } CallStatement* boolSelectCall = CreateBoolCall(thenStoreVar->get_destination()) ; // Append the arguments Expression* thenStoreVarValue = thenStoreVar->get_value() ; thenStoreVar->set_value(NULL) ; boolSelectCall->append_argument(thenStoreVarValue) ; Expression* elseStoreVarValue = elseStoreVar->get_value() ; elseStoreVar->set_value(NULL) ; boolSelectCall->append_argument(elseStoreVarValue) ; Expression* condition = toConvert->get_condition() ; toConvert->set_condition(NULL) ; boolSelectCall->append_argument(condition) ; toConvert->get_parent()->replace(toConvert, boolSelectCall) ; return true ; }
void force_call_dest_not_expr(CallExpression *the_call, list<CallExpression*> *call_list) { call_list->clear_list(); list<StoreVariableStatement *> *store_list = force_dest_not_expr(the_call); if (!store_list) { call_list->push_back(the_call); } else { for (list<StoreVariableStatement*>::iterator siter = store_list->begin(); siter != store_list->end(); siter++) { StoreVariableStatement *store = *siter; CallExpression *cal_expr = to<CallExpression>(store->get_value()); call_list->push_back(cal_expr); } } delete store_list; }
bool IfConversionPass2::ConvertStoreVarCall(IfStatement* toConvert) { Statement* thenPart = Denormalize(toConvert->get_then_part()) ; Statement* elsePart = Denormalize(toConvert->get_else_part()) ; StoreVariableStatement* thenStoreVar = dynamic_cast<StoreVariableStatement*>(thenPart) ; CallStatement* elseCall = dynamic_cast<CallStatement*>(elsePart) ; assert(thenStoreVar != NULL) ; assert(elseCall != NULL) ; CallStatement* boolSelectCall = CreateBoolCall(thenStoreVar->get_destination()) ; // Append the arguments Expression* thenStoreVarValue = thenStoreVar->get_value() ; thenStoreVar->set_value(NULL) ; boolSelectCall->append_argument(thenStoreVarValue) ; Expression* elseCallValue = elseCall->get_callee_address() ; elseCall->set_callee_address(NULL) ; CallExpression* elseCallExp = create_call_expression(theEnv, elseCall->get_destination()->get_type()->get_base_type(), elseCallValue) ; // Append all of the call arguments to the expression for (int i = 0 ; i < elseCall->get_argument_count() ; ++i) { Expression* nextArg = elseCall->get_argument(i) ; nextArg->set_parent(NULL) ; elseCallExp->append_argument(nextArg) ; } boolSelectCall->append_argument(elseCallExp) ; Expression* condition = toConvert->get_condition() ; toConvert->set_condition(NULL) ; boolSelectCall->append_argument(condition) ; toConvert->get_parent()->replace(toConvert, boolSelectCall) ; return true ; }
void CopyPropagationPass2::ProcessSpecialIfs() { list<IfStatement*>* allIfs = collect_objects<IfStatement>(procDef->get_body()) ; assert(allIfs != NULL) ; list<IfStatement*>::iterator ifIter = allIfs->begin() ; while (ifIter != allIfs->end()) { Statement* elsePart = (*ifIter)->get_else_part() ; assert(elsePart != NULL) ; StatementList* elseList = dynamic_cast<StatementList*>(elsePart) ; if (elseList == NULL) { ++ifIter ; continue ; } assert(elseList != NULL) ; if (elseList->get_statement_count() == 2) { // Process this if statement Statement* thenPart = (*ifIter)->get_then_part() ; assert(thenPart != NULL) ; /* StatementList* thenList = dynamic_cast<StatementList*>(thenPart) ; assert(thenList != NULL) ; assert(thenList->get_statement_count() == 1) ; Statement* thenStatement = thenList->get_statement(0) ; assert(thenStatement != NULL) ; */ StoreVariableStatement* thenStoreVar = dynamic_cast<StoreVariableStatement*>(thenPart) ; assert(thenStoreVar != NULL) ; Statement* firstElseStatement = elseList->get_statement(0) ; Statement* secondElseStatement = elseList->get_statement(1) ; assert(firstElseStatement != NULL && secondElseStatement != NULL) ; // We are definitely going to break the rules here // We know that the destination has to be replaced with // the source StoreVariableStatement* secondElseStore = dynamic_cast<StoreVariableStatement*>(secondElseStatement) ; assert(secondElseStore != NULL) ; Expression* source = secondElseStore->get_value() ; assert(source != NULL) ; LoadVariableExpression* sourceLoadExp = dynamic_cast<LoadVariableExpression*>(source) ; assert(sourceLoadExp != NULL) ; VariableSymbol* sourceVariable = sourceLoadExp->get_source() ; assert(sourceVariable != NULL) ; // First, find the use of the then portion and replace that use // with the source variable BrickAnnote* ba = to<BrickAnnote>(thenStoreVar->lookup_annote_by_name("reached_uses")) ; assert(ba != NULL) ; assert(ba->get_brick_count() == 1) ; Iter<SuifBrick*> tmpIter = ba->get_brick_iterator() ; SuifObjectBrick* sob = to<SuifObjectBrick>(tmpIter.current()) ; assert(sob != NULL) ; SuifObject* finalDest = sob->get_object() ; LoadVariableExpression* finalLoad = dynamic_cast<LoadVariableExpression*>(finalDest) ; assert(finalLoad != NULL) ; // Before we make the change, mark the variable we are replacing as // removed. finalLoad->get_source()->append_annote(create_brick_annote(theEnv, "RemovedVariable")) ; finalLoad->set_source(sourceVariable) ; // Now, change the then portion thenStoreVar->set_destination(sourceVariable) ; // Now, remove the second else statement elseList->remove_statement(1) ; // We should be done. } ++ifIter ; } delete allIfs ; }