src/lima/pp_hir/reorder.c
//Reorders a program's basic blocks using a reverse-postordering
//Uses lima_pp_hir_cfg_traverse to do the real "heavy lifting"
//(i.e. actually traversing the control-flow graph with a depth-first search)
Reorders a program's basic blocks using a reverse-postordering
Uses lima_pp_hir_cfg_traverse to do the real "heavy lifting"
Reorders a program ' s basic blocks using a reverse - postordering Uses lima_pp_hir_cfg_traverse to do the real " heavy lifting "
bool lima_pp_hir_prog_reorder(lima_pp_hir_prog_t* prog)
{
	reordering_visitor_state state;
	state.blocks = NULL;
	if (!lima_pp_hir_cfg_traverse(prog, (lima_pp_hir_cfg_visitor_state*)&state, 
							  reordering_visitor, false))
	{
		delete_block_list(state.blocks);
		return false;
	}
	lima_pp_hir_block_t* block, *tmp_block;
	pp_hir_prog_for_each_block_safe(prog, tmp_block, block)
	{
		bool found = false;
		block_list* cur_block = state.blocks;
		while (cur_block != NULL)
		{
			if (cur_block->block == block)
			{
				found = true;
				break;
			}
			cur_block = cur_block->next;
		}
		if (!found)
			lima_pp_hir_block_delete(block);
	}
	while (prog->num_blocks > 0)
	{
		lima_pp_hir_block_t* block = pp_hir_first_block(prog);
		list_del(&block->block_list);
		prog->num_blocks--;
	}
	block_list* cur_block = state.blocks;
	while (cur_block != NULL)
	{
		list_add(&cur_block->block->block_list, prog->block_list.prev);
		cur_block = cur_block->next;
		prog->num_blocks++;
	}
	delete_block_list(state.blocks);
	return true;
}
bool lima_pp_hir_prog_reorder ( lima_pp_hir_prog_t * prog ) { reordering_visitor_state state ; state . blocks = NULL ; if ( ! lima_pp_hir_cfg_traverse ( prog , ( lima_pp_hir_cfg_visitor_state * ) & state , reordering_visitor , false ) ) { delete_block_list ( state . blocks ) ; return false ; } lima_pp_hir_block_t * block , * tmp_block ; pp_hir_prog_for_each_block_safe ( prog , tmp_block , block ) { bool found = false ; block_list * cur_block = state . blocks ; while ( cur_block != NULL ) { if ( cur_block -> block == block ) { found = true ; break ; } cur_block = cur_block -> next ; } if ( ! found ) lima_pp_hir_block_delete ( block ) ; } while ( prog -> num_blocks > 0 ) { lima_pp_hir_block_t * block = pp_hir_first_block ( prog ) ; list_del ( & block -> block_list ) ; prog -> num_blocks -- ; } block_list * cur_block = state . blocks ; while ( cur_block != NULL ) { list_add ( & cur_block -> block -> block_list , prog -> block_list . prev ) ; cur_block = cur_block -> next ; prog -> num_blocks ++ ; } delete_block_list ( state . blocks ) ; return true ; }
Reorders a program's basic blocks using a reverse-postordering
Uses lima_pp_hir_cfg_traverse to do the real "heavy lifting"
(i.e.
Reorders a program ' s basic blocks using a reverse - postordering Uses lima_pp_hir_cfg_traverse to do the real " heavy lifting " ( i . e .
//Delete dead blocks (i.e. blocks we didn't traverse) //Finally, replace the list of basic blocks