bool AvailableCodeEliminationPass::ProcessStatementList(StatementList* s)
{
  assert(s != NULL) ;
  bool changed = false ;
  list<int> statementsToRemove ;
  
  for (int i = 0 ; i < s->get_statement_count() ; ++i)
  {
    Statement* currentStatement = s->get_statement(i) ;
    assert(currentStatement != NULL) ;

    // Collect all output variables
    list<VariableSymbol*> currentOutputs = 
      AllDefinedVariables(currentStatement);

    // See if there are any uses and or definitions
    bool hasUses = false ;
    bool hasDefs = false ;
    list<VariableSymbol*>::iterator varIter = currentOutputs.begin() ;
    while (varIter != currentOutputs.end())
    {
      hasUses |= IsUsedBeforeDefined(s, i+1, *varIter) ;
      hasDefs |= HasDefs(s, i+1, *varIter) ;
      ++varIter ;
    }

    // If this statement happens to be a store statement, call the 
    //  appropriate functions for this as well.
    StoreStatement* currentStore = 
      dynamic_cast<StoreStatement*>(currentStatement) ;
    if (currentStore != NULL)
    {
      hasUses |= HasUses(s, i+1, currentStore->get_destination_address()) ;
      hasDefs |= HasDefs(s, i+1, currentStore->get_destination_address()) ;
    }

    // If no uses, but definitions, mark this statement for removal
    if (hasUses == false && hasDefs == true)
    {
      // Put them in reverse order so when we remove them we don't mess
      //  up the numbering
      statementsToRemove.push_front(i) ;
      changed = true ;
    }
  }

  // Now actually remove all statements that are marked as available
  list<int>::iterator removeIter = statementsToRemove.begin() ;
  while (removeIter != statementsToRemove.end())
  {
    // Delete?
    delete s->remove_statement(*removeIter) ;
    ++removeIter ;
  }

  return changed ;
}
bool IfConversionPass2::ConvertArrayStoreIf(IfStatement* toConvert)
{
  Statement* thenPart = Denormalize(toConvert->get_then_part()) ;
  Statement* elsePart = Denormalize(toConvert->get_else_part()) ;
  StoreStatement* thenStore = dynamic_cast<StoreStatement*>(thenPart) ;
  StoreStatement* elseStore = dynamic_cast<StoreStatement*>(elsePart) ;
  
  assert(thenStore != NULL) ;
  assert(elseStore != NULL) ;

  ArrayReferenceExpression* thenRef = 
    dynamic_cast<ArrayReferenceExpression*>(thenStore->get_destination_address()) ;
  ArrayReferenceExpression* elseRef = 
    dynamic_cast<ArrayReferenceExpression*>(elseStore->get_destination_address()) ;

  assert(thenRef != NULL) ;
  assert(elseRef != NULL) ;

  // Verify that these array references are to the same location.
  if (!EquivalentExpressions(thenRef, elseRef))
  {
    return false ;
  }

  // This will hopefully be scalar replaced later, but for now we just need
  //  to do something...
  //  What I need is the qualified element type of the array.  
  QualifiedType* qualType = create_qualified_type(theEnv,
						  thenRef->get_result_type()) ;

  StoreStatement* replacement = CreateBoolCall(thenRef) ;

  // Append the arguments to the call expression
  CallExpression* callExpr = 
    dynamic_cast<CallExpression*>(replacement->get_value()) ;
  assert(callExpr != NULL) ;

  Expression* thenStoreValue = thenStore->get_value() ;
  thenStore->set_value(NULL) ;
  callExpr->append_argument(thenStoreValue) ;

  Expression* elseStoreValue = elseStore->get_value() ;
  elseStore->set_value(NULL) ;
  callExpr->append_argument(elseStoreValue) ;

  Expression* condition = toConvert->get_condition() ;
  toConvert->set_condition(NULL) ;
  callExpr->append_argument(condition) ;

  // Replace the if
  toConvert->get_parent()->replace(toConvert, replacement) ;
  return true ;
}
Exemple #3
0
static
String handle_static_store_statement(CPrintStyleModule *state,
				   const SuifObject *obj)
{
  StoreStatement *stmt = to<StoreStatement>(obj);
  String return_str = "*(";
  return_str +=
    state->print_to_string(stmt->get_destination_address());

  return_str += ") = ";
  return_str +=
    state->print_to_string(stmt->get_value());
  return(return_str);
}
bool IfConversionPass2::ConvertStoreIf(IfStatement* toConvert)
{
  Statement* thenPart = Denormalize(toConvert->get_then_part()) ;
  StoreStatement* thenStore = dynamic_cast<StoreStatement*>(thenPart) ;
  assert(thenStore != NULL) ;

  Expression* destination = thenStore->get_destination_address() ;
  
  if (dynamic_cast<FieldAccessExpression*>(destination) != NULL)
  {
    return ConvertStructStoreIf(toConvert) ;    
  }
  else if (dynamic_cast<ArrayReferenceExpression*>(destination) != NULL)
  {
    return ConvertArrayStoreIf(toConvert) ;
  }
  else
  {
    OutputError("Unsupported if detected!") ;
    assert(0) ;
    return false ; // To avoid a warning
  }  
}
bool IfConversionPass2::ConvertStructStoreIf(IfStatement* toConvert)
{
  Statement* thenPart = Denormalize(toConvert->get_then_part()) ;
  Statement* elsePart = Denormalize(toConvert->get_else_part()) ;
  StoreStatement* thenStore = dynamic_cast<StoreStatement*>(thenPart) ;
  StoreStatement* elseStore = dynamic_cast<StoreStatement*>(elsePart) ;

  assert(thenStore != NULL) ;
  assert(elseStore != NULL) ;

  FieldAccessExpression* thenField =
    dynamic_cast<FieldAccessExpression*>(thenStore->get_destination_address());
  FieldAccessExpression* elseField = 
    dynamic_cast<FieldAccessExpression*>(elseStore->get_destination_address());

  assert(thenField != NULL) ;
  assert(elseField != NULL) ;

  // Check to make sure that each field access is accessing the same field
  if (thenField->get_field() != elseField->get_field())
  {
    return false ;
  }

  CallStatement* replacement = CreateBoolCall(thenField->get_field()) ;

  // Append the arguments
  Expression* thenStoreValue = thenStore->get_value() ;
  thenStore->set_value(NULL) ;
  replacement->append_argument(thenStoreValue) ;

  Expression* elseStoreValue = elseStore->get_value() ;
  elseStore->set_value(NULL) ;
  replacement->append_argument(elseStoreValue) ;

  Expression* condition = toConvert->get_condition() ;
  toConvert->set_condition(NULL) ;
  replacement->append_argument(condition) ;

  toConvert->get_parent()->replace(toConvert, replacement) ;  
  return true ;
}