bool CopyPropagationPass2::IsOnlyDefinition(StoreVariableStatement* def,
					    LoadVariableExpression* use)
{
  BrickAnnote* defAnnote =
    to<BrickAnnote>(use->lookup_annote_by_name("reaching_defs")) ;
  assert(defAnnote != NULL) ;

  if (defAnnote->get_brick_count() != 1)
  {
    return false ;
  }

  SuifBrick* defBrick = defAnnote->get_brick(0) ;
  SuifObjectBrick* sob = dynamic_cast<SuifObjectBrick*>(defBrick) ;
  assert(sob != NULL) ;

  if (def == sob->get_object())
  {
    return true ;
  }
  else
  {
    return false ;
  }
}
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 ;
}