void
TR_ExpressionsSimplification::removeUncertainBlocks(TR_RegionStructure* region, List<TR::Block> *candidateBlocksList)
   {
   // Examine the top region block first
   //
   TR::Block *entryBlock = _currentRegion->getEntryBlock();
   ListIterator<TR::Block> blocks;
   blocks.set(candidateBlocksList);

   if (trace())
      traceMsg(comp(), "Number of blocks %d, entry block number %d\n", candidateBlocksList->getSize(), entryBlock->getNumber());

   for (TR::Block *block = blocks.getFirst(); block; block = blocks.getNext())
      {
      TR::CFGNode *cfgNode = block;
      if (!(cfgNode->getExceptionSuccessors().empty()) || blockHasCalls(block, comp()))
         {
         if (trace())
            traceMsg(comp(), "An exception can be thrown from block_%d. Removing all the blocks, since we cannot know the number of iterations.\n", block->getNumber());
         candidateBlocksList->deleteAll();
         break;
         }
      }

   TR_PostDominators postDominators(comp());
   if (postDominators.isValid())
      {
	  postDominators.findControlDependents();
      for (TR::Block *block = blocks.getFirst(); block; block = blocks.getNext())
         {
         if (postDominators.dominates(block, entryBlock) == 0)
            {
            candidateBlocksList->remove(block);
            if (trace())
               traceMsg(comp(), "Block_%d is not guaranteed to be executed at least once. Removing it from the list.\n", block->getNumber());
            }
         }
      }
   else
      {
	  if (trace())
	     traceMsg(comp(), "There is no post dominators information. Removing all the blocks.\n");
	  for (TR::Block *block = blocks.getFirst(); block; block = blocks.getNext())
	     {
	     candidateBlocksList->remove(block);
	     if (trace())
	        traceMsg(comp(), "Block_%d is removed from the list\n", block->getNumber());
	     }
      }
   }
void
TR_ExpressionsSimplification::findAndSimplifyInvariantLoopExpressions(TR_RegionStructure * region)
   {
   _currentRegion = region;
   TR::Block *entryBlock = _currentRegion->getEntryBlock();
   if (trace())
      traceMsg(comp(), "Entry block: %p in loop region %p\n", entryBlock, region);


   // Generate a list of blocks that can be processed
   // Criteria: the block must be excucted exactly once
   //
   TR_ScratchList<TR::Block> candidateBlocksList(trMemory());
   _currentRegion->getBlocks(&candidateBlocksList);

   if (candidateBlocksList.getSize() > 1)
      {
      if (trace())
         traceMsg(comp(), "More than 1 blocks in the natural loop, need to remove uncertain blocks\n");

      removeUncertainBlocks(_currentRegion, &candidateBlocksList);

      if (candidateBlocksList.getSize() < 1)
         return;
      }

   _currentRegion->resetInvariance();
   _currentRegion->computeInvariantExpressions();

   // The rest is the transformation
   //
   // For each block that is definitely executed once
   // analyze its nodes
   //
   ListIterator<TR::Block> candidateBlocks;
   candidateBlocks.set(&candidateBlocksList);
   simplifyInvariantLoopExpressions(candidateBlocks);
   }