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) ;
}
bool IfConversionPass2::CorrectTypes(Statement* x, Statement* y)
{
  if (dynamic_cast<StoreStatement*>(x) != NULL &&
      dynamic_cast<StoreStatement*>(y) != NULL)
  {
    return true ;
  }
  if (dynamic_cast<StoreVariableStatement*>(x) != NULL &&
      dynamic_cast<StoreVariableStatement*>(y) != NULL)
  {
    StoreVariableStatement* xStoreVar = 
      dynamic_cast<StoreVariableStatement*>(x) ;
    StoreVariableStatement* yStoreVar = 
      dynamic_cast<StoreVariableStatement*>(y) ;

    return (xStoreVar->get_destination() == yStoreVar->get_destination()) ;
  }
  if (dynamic_cast<CallStatement*>(x) != NULL &&
      dynamic_cast<CallStatement*>(y) != NULL)
  {
    CallStatement* xCall = dynamic_cast<CallStatement*>(x) ;
    CallStatement* yCall = dynamic_cast<CallStatement*>(y) ;
    return ((xCall->get_destination() == yCall->get_destination()) && 
	    (xCall->get_destination() != NULL)) ;
  }

  // Now, the mixtures
  if (dynamic_cast<CallStatement*>(x) != NULL &&
      dynamic_cast<StoreVariableStatement*>(y) != NULL)
  {
    CallStatement* xCall = dynamic_cast<CallStatement*>(x) ;
    StoreVariableStatement* yStoreVar = 
      dynamic_cast<StoreVariableStatement*>(y) ;
    return ((xCall->get_destination() == yStoreVar->get_destination()) &&
	    (xCall->get_destination() != NULL)) ;
  }

  if (dynamic_cast<StoreVariableStatement*>(x) != NULL &&
      dynamic_cast<CallStatement*>(y) != NULL)
  {
    StoreVariableStatement* xStoreVar = 
      dynamic_cast<StoreVariableStatement*>(x) ;
    CallStatement* yCall = dynamic_cast<CallStatement*>(y) ;
    return ((xStoreVar->get_destination() == yCall->get_destination()) &&
	    (xStoreVar->get_destination() != NULL)) ;
  }

  return false ;
}
예제 #3
0
파일: cprint_style.cpp 프로젝트: jrk/suif2
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 ;
}
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 CombineSummationPass::do_procedure_definition(ProcedureDefinition* p)
{
  procDef = p ;
  assert(procDef != NULL) ;

  OutputInformation("Combine summation pass begins") ;

  StatementList* innermost = InnermostList(procDef) ;
  assert(innermost != NULL) ;

  bool change = false ;
  do
  {
    // Find the first summation
    StoreVariableStatement* firstStatement = NULL ;
    StoreVariableStatement* secondStatement = NULL ;
    change = false ;
    int i ;
    int firstStatementPosition = -1 ;
    i = 0 ;
    while (i < innermost->get_statement_count())
    {
      StoreVariableStatement* currentStoreVariable =
	dynamic_cast<StoreVariableStatement*>(innermost->get_statement(i)) ;
      if (currentStoreVariable != NULL && IsSummation(currentStoreVariable))
      {
	firstStatement = currentStoreVariable ;
	firstStatementPosition = i ;
	break ;
      }
	++i ;
    }
    
    if (firstStatement != NULL)
    {
      VariableSymbol* firstDest = firstStatement->get_destination() ;
      for (int j = i+1 ; j < innermost->get_statement_count() ; ++j)
      {
	StoreVariableStatement* nextStoreVar = 
	  dynamic_cast<StoreVariableStatement*>(innermost->get_statement(j));
	if (nextStoreVar != NULL && IsSummation(nextStoreVar, firstDest))
	{
	  secondStatement = nextStoreVar ;
	  break ;
	}
	if (IsDefinition(innermost->get_statement(j), firstDest) ||
	    HasUses(innermost->get_statement(j), firstDest))
	{
	  break ;
	}						
      }
    }
    if (secondStatement != NULL)
    {
      // Go through each of the variables used in the first statement and
      //  make sure there are no definitions to any of them.
      //  I only have to worry about variables and not array accesses because
      //  we don't allow them to read and write to array values.
      int originalPosition = DeterminePosition(innermost, firstStatement) ;
      assert(originalPosition >= 0) ;
      list<VariableSymbol*> usedVars = 
	AllUsedVariablesBut(firstStatement, firstStatement->get_destination());
      bool goodPath = true ;
      for (int j = originalPosition ; 
	   j < innermost->get_statement_count() && 
	     innermost->get_statement(j) != secondStatement ; 
	   ++j)
      {
	list<VariableSymbol*>::iterator usedIter = usedVars.begin() ;
	while (usedIter != usedVars.end())
	{
	  if (IsOutputVariable((*usedIter), innermost->get_statement(j)))
	  {
	    goodPath = false ;
	    break ;
	  }
	  ++usedIter ;
	}
	if (!goodPath) 
	{
	  break ;
	}
      }
      if (!goodPath)
      {
	continue ;
      }
      // Actually do the combining here
      change = true ;
      Expression* remains = RemoveValue(firstStatement) ;
      Expression* secondRemains = RemoveValue(secondStatement) ;
      // Create two binary expressions
      BinaryExpression* remainsSum = 
	create_binary_expression(theEnv,
				 remains->get_result_type(),
				 LString("add"),
				 remains,
				 secondRemains) ;
      LoadVariableExpression* loadDest = 
	create_load_variable_expression(theEnv,
	    secondStatement->get_destination()->get_type()->get_base_type(),
					secondStatement->get_destination()) ;
      BinaryExpression* finalSum =
	create_binary_expression(theEnv,
				 remainsSum->get_result_type(),
				 LString("add"),
				 remainsSum,
				 loadDest) ;

      secondStatement->set_value(finalSum) ;
      // Delete?
      innermost->remove_statement(firstStatementPosition) ;
    }
     
  } while (change == true) ;  

  OutputInformation("Combine summation pass ends") ;
}