static void eliminateRedundantInstructions(analysis::DataflowGraph& dfg,
	BlockSet& blocks, iterator block)
{
	typedef std::vector<unsigned int> KillList;
	
	KillList killList;
	
	report("  Propagating constants through instructions in BB_" << block->id());
	unsigned int index = 0;
	for(auto instruction = block->instructions().begin();
		instruction != block->instructions().end(); ++instruction)
	{
		if(!propagateValueToSuccessors(dfg, blocks, instruction))
		{
			++index;
			continue;
		}
		
		if(canRemoveInstruction(block, instruction))
		{
			report("    value is not used, removed it.");
			killList.push_back(index);
			
			// schedule the block for more work
			// TODO: do this when we consider values in multiple blocks
		}
		else
		{
			++index;
		}
	}
	
	for(KillList::iterator killed = killList.begin();
		killed != killList.end(); ++killed)
	{
		dfg.erase(block, *killed);
	}
}
示例#2
0
static void eliminateDeadInstructions(analysis::DataflowGraph& dfg,
	BlockSet& blocks, iterator block)
{
	typedef analysis::DataflowGraph::Block                Block;
	typedef analysis::DataflowGraph::RegisterSet   RegisterSet;
	typedef std::vector<unsigned int>                     KillList;
	typedef std::vector<PhiInstructionVector::iterator>   PhiKillList;
	typedef std::vector<RegisterSet::iterator>            AliveKillList;
	
	report(" Eliminating dead instructions from BB_" << block->id());
	
	report("  Removing dead alive out values");
	AliveKillList aliveOutKillList;
	for(RegisterSet::iterator aliveOut = block->aliveOut().begin();
		aliveOut != block->aliveOut().end(); ++aliveOut)
	{
		if(canRemoveAliveOut(dfg, block, *aliveOut))
		{
			report("   removed " << aliveOut->id);
			aliveOutKillList.push_back(aliveOut);
		}
	}
	
	for(AliveKillList::iterator killed = aliveOutKillList.begin();
		killed != aliveOutKillList.end(); ++killed)
	{
		block->aliveOut().erase(*killed);
	}
	
	KillList killList;
	
	report("  Removing dead instructions");
	unsigned int index = 0;
	for(InstructionVector::iterator instruction = block->instructions().begin();
		instruction != block->instructions().end(); ++instruction)
	{
		if(canRemoveInstruction(block, instruction))
		{
			report("   removed '" << instruction->i->toString() << "'");
			killList.push_back(index);
			
			// schedule the block for more work
			report("    scheduled this block again");
			blocks.insert(block);
		}
		else
		{
			++index;
		}
	}
	
	for(KillList::iterator killed = killList.begin();
		killed != killList.end(); ++killed)
	{
		dfg.erase(block, *killed);
	}
	
	PhiKillList phiKillList;
	
	report("  Removing dead phi instructions");
	for(PhiInstructionVector::iterator phi = block->phis().begin();
		phi != block->phis().end(); ++phi)
	{
		if(canRemovePhi(block, *phi))
		{
			report("   removed " << phi->d.id);
			phiKillList.push_back(phi);
		}
	}
	
	report("  Removing dead alive in values");
	AliveKillList aliveInKillList;
	for(RegisterSet::iterator aliveIn = block->aliveIn().begin();
		aliveIn != block->aliveIn().end(); ++aliveIn)
	{
		if(canRemoveAliveIn(block, *aliveIn))
		{
			report("   removed " << aliveIn->id);
			aliveInKillList.push_back(aliveIn);
			
			// schedule the predecessors for more work
			for(BlockPointerSet::iterator predecessor =
				block->predecessors().begin();
				predecessor != block->predecessors().end(); ++predecessor)
			{
				report("    scheduled predecessor BB_" << (*predecessor)->id());
				blocks.insert(*predecessor);
			}
		}
	}
	
	for(AliveKillList::iterator killed = aliveInKillList.begin();
		killed != aliveInKillList.end(); ++killed)
	{
		block->aliveIn().erase(*killed);
	}
}