void dFlowControlBlock::ApplyLocalOptimizations(dCIL& program)
{
//static int xxx = 0;
//xxx ++;
//if(xxx != 3)
//return;

//Trace();
	bool optimized = true;
	while (optimized) {
		optimized = false;
		optimized |= ApplySubExpressionsElimination(program);
		optimized |= ApplyCopyPropagation(program);
		optimized |= ApplyConstantFolding(program);
	}
//Trace();
}
void dDataFlowGraph::ApplyLocalOptimizations()
{
	m_mark += 2;
	BuildBasicBlockGraph();
//m_cil->Trace();

	ApplyIfStatementsSimplification();
//m_cil->Trace();

	CalculateReachingDefinitions();
	for (bool optimized = true; optimized;) {
		optimized = false;
		optimized |= ApplySubExpresionToCopyPropagation();
//m_cil->Trace();
//m_basicBlocks.Trace();
		if (optimized) {
			ApplyRemoveDeadCode();
//m_cil->Trace();
//m_basicBlocks.Trace();
		}
	}

	// apply generic data flow optimization for single blocks
	for (bool optimized = true; optimized;) {
		optimized = false;
		UpdateReachingDefinitions();

		optimized |= ApplyConstantPropagation();
//m_cil->Trace();
//m_basicBlocks.Trace();
		optimized |= ApplyCopyPropagation();
//m_cil->Trace();
//m_basicBlocks.Trace();
		optimized |= ApplyConstantFolding();
//m_cil->Trace();
//m_basicBlocks.Trace();
		optimized |= ApplySemanticInstructionReordering();
//m_cil->Trace();
//m_basicBlocks.Trace();
		optimized |= ApplyRemoveDeadCode();
//m_cil->Trace();
//m_basicBlocks.Trace();
	}

	// apply peephole optimizations
	for (bool optimized = true; optimized;) {
		optimized = false;
		UpdateReachingDefinitions();

		optimized |= ApplySubExpresionToCopyPropagation();
//m_cil->Trace();
//m_basicBlocks.Trace();
		if (optimized) {
			ApplyRemoveDeadCode();
//m_cil->Trace();
//m_basicBlocks.Trace();
		}
	}

	for (bool optimized = true; optimized;) {
		optimized = false;
		UpdateReachingDefinitions();

		optimized |= ApplyCommonSubExpresion();
//m_cil->Trace();
//m_basicBlocks.Trace();
		optimized |= ApplyCopyPropagation();
//m_cil->Trace();
//m_basicBlocks.Trace();
		optimized |= ApplyRemoveDeadCode();
//m_cil->Trace();
//m_basicBlocks.Trace();
	}

/*
	// apply loops optimizations here
	dList<dLoop> loops;
	GetLoops (loops);
	for (bool optimized = true; optimized;) {
		optimized = false;
		for (dList<dLoop>::dListNode* loopNode = loops.GetFirst(); loopNode; loopNode = loopNode->GetNext()) {
			dLoop& loop = loopNode->GetInfo();
			optimized |= ApplyLoopOptimization(loop);
m_cil->Trace();
		}
	}
*/

}