/* Hoist det of loop. e.g: while (a=10,b+=3,c<a) { IR-LIST; } be replaced by a = 10; b += 3; while (c<a) { IR-LIST; a = 10; b += 3; } */ bool IR_CFS_OPT::hoist_loop(IR ** head, IR * ir) { IS_TRUE(IR_type(ir)==IR_DO_WHILE || IR_type(ir)==IR_WHILE_DO || IR_type(ir)==IR_DO_LOOP, ("need LOOP")); IS_TRUE(LOOP_det(ir), ("DET is NULL")); IR * det = LOOP_det(ir); INT i = 0; while (det != NULL) { i++; det = IR_next(det); } IR * new_list = NULL, * new_body_list = NULL; if (i > 1) { det = LOOP_det(ir); while (i > 1) { IR * c = det; IS_TRUE(c->is_stmt(), ("Non-stmt ir should be remove " "during reshape_ir_tree()")); det = IR_next(det); remove(&LOOP_det(ir), c); add_next(&new_list, c); i--; } new_body_list = m_ru->dup_irs_list(new_list); insertbefore(head, ir, new_list); add_next(&LOOP_body(ir), new_body_list); return true; } return false; }
static vnet_config_t * find_config_with_features (vlib_main_t * vm, vnet_config_main_t * cm, vnet_config_feature_t * feature_vector) { u32 last_node_index = ~0; vnet_config_feature_t * f; u32 * config_string; uword * p; vnet_config_t * c; config_string = cm->config_string_temp; cm->config_string_temp = 0; if (config_string) _vec_len (config_string) = 0; vec_foreach (f, feature_vector) { /* Connect node graph. */ f->next_index = add_next (vm, cm, last_node_index, f->node_index); last_node_index = f->node_index; /* Store next index in config string. */ vec_add1 (config_string, f->next_index); /* Store feature config. */ vec_add (config_string, f->feature_config, vec_len (f->feature_config)); }
/* Canonicalize det of IF. e.g: if (a=10,b+=3,c<a) {...} be replaced by a = 10; b += 3; if (c<a) {...} */ bool IR_CFS_OPT::hoist_if(IR ** head, IR * ir) { IS_TRUE(IR_type(ir) == IR_IF, ("need IF")); IS_TRUE(IF_det(ir), ("DET is NULL")); IR * det = IF_det(ir); INT i = 0; while (det != NULL) { i++; det = IR_next(det); } IR * new_list = NULL; if (i > 1) { det = IF_det(ir); while (i > 1) { IR * c = det; IS_TRUE(c->is_stmt(), ("Non-stmt ir should be remove during reshape_ir_tree()")); det = IR_next(det); remove(&IF_det(ir), c); add_next(&new_list, c); i--; } insertbefore(head, ir, new_list); return true; } return false; }
/* Control flow struct optimizations. Transform follow struct to do-while loop LABEL: IR-LIST IF DET GOTO LABEL ...(DEAD CODE) ELSE FALSE-PART ENDIF is replace by DO { IR-LIST } WHILE DET FALSE-PART */ bool IR_CFS_OPT::trans_to_do_while_loop(IR ** head, IR * ir) { IS_TRUE(head != NULL && *head != NULL, ("invalid parameter")); if (ir->is_lab()) { IR * t = ir; LABEL_INFO * li = LAB_lab(ir); while (t != NULL) { if (IR_type(t) == IR_IF) { if (IF_truebody(t) != NULL && IR_type(IF_truebody(t)) == IR_GOTO && is_same_label(LAB_lab(ir), GOTO_lab(IF_truebody(t)))) { //start transform... IR * dowhile = m_ru->new_ir(IR_DO_WHILE); LOOP_det(dowhile) = m_ru->dup_irs(LOOP_det(t)); IR * if_stmt = t; t = IR_next(ir); while (t != NULL && t != if_stmt) { IR * c = t; t = IR_next(t); remove(head, c); add_next(&LOOP_body(dowhile), c); } IS_TRUE(t == if_stmt, ("???")); remove(head, if_stmt); if (IF_falsebody(if_stmt)) { add_next(&dowhile, IF_falsebody(if_stmt)); IF_falsebody(if_stmt) = NULL; } insertafter(&ir, dowhile); m_ru->free_irs(if_stmt); //free IF remove(head, ir); m_ru->free_irs(ir); //free LABEL return true; } } t = IR_next(t); } } return false; }
int main(void) { link *head, *odd = NULL, *even = NULL; head = new_list(); print_list(head); while (head != NULL) { odd = add_next(head->item,odd); head = head ->next; even = add_next(head->item, even); head = head->next; } head =concat_list(odd, even); print_list(head); return 0; }
/* 'is_tmp_sc': true if the new scope is used for temprary. And it will be removed from the sub-scope-list while return to the parent. */ SCOPE * enter_sub_scope(bool is_tmp_sc) { SCOPE * sc = new_scope(); SCOPE_level(sc) = SCOPE_level(g_cur_scope) + 1; SCOPE_parent(sc) = g_cur_scope; SCOPE_is_tmp_sc(sc) = is_tmp_sc; //Add 'sc' as sub scope followed the most right one of subscope list. //e.g: first_sub_scope -> second_sub_scope -> ... add_next(&SCOPE_sub(g_cur_scope), sc); g_cur_scope = sc; return g_cur_scope; }
//Add 'e' into out-edges of 'vex' void GRAPH::add_out_list(VERTEX * vex, EDGE * e) { IS_TRUE(m_pool != NULL, ("not yet initialized.")); if (vex == NULL || e == NULL)return; EDGE_C * el = VERTEX_out_list(vex); while (el != NULL) { if (EC_edge(el) == e) return; el = EC_next(el); } el = new_ec(e); add_next(&VERTEX_out_list(vex), el); }
inline vm::tuple* expand(const vm::predicate *pred, mem::node_allocator *alloc) { if (num_tuples == cap) { if (num_tuples == 0) init(1, pred, alloc); else { const size_t old_cap(cap); utils::byte *old((utils::byte *)data); init(cap * 2, pred, alloc); memcpy(data, old, compute_size(pred, old_cap)); delete_buffer(pred, old, old_cap, alloc); } } return add_next(pred); }
/* The followed forms if (cond) { t=1 a=1 goto L1; } f=1 goto L2; L1: is replaced by if (!cond) { f=1 goto L2; } t=1 a=1 L1: 'goto L1' is removed and free, and L1 is removed if it is not a target of some other instruction. */ bool IR_CFS_OPT::trans_if1(IR ** head, IR * ir) { IS_TRUE(head && *head, ("invalid parameter")); if (ir == NULL || IR_type(ir) != IR_IF) { return false; } //Check true part. IR * t = IF_truebody(ir); while (t != NULL) { if (!is_non_branch_ir(t)) { break; } t = IR_next(t); } if (t != NULL && IR_next(t) == NULL && IR_type(t) == IR_GOTO) { IR * first_goto = t; t = IR_next(ir); while (t != NULL) { if (!is_non_branch_ir(t)) break; t = IR_next(t); } if (t != NULL && IR_type(t) == IR_GOTO) { IR * second_goto = t; if (IR_next(second_goto) != NULL && IR_next(second_goto)->is_lab() && is_same_label(GOTO_lab(first_goto), LAB_lab(IR_next(second_goto)))) { //Here we start to transform... m_ru->invert_cond(&IF_det(ir)); IR * new_list1 = NULL; IR * new_list2 = NULL; t = IF_truebody(ir); //Split true body of IF. IR * last = NULL; while (t != first_goto) { IR * c = t; t = IR_next(t); remove(&IF_truebody(ir), c); add_next(&new_list1, &last, c); } IS_TRUE(t && t == first_goto, ("invalid control flow")); remove(&IF_truebody(ir), first_goto); m_ru->free_irs(first_goto); //Split all irs between IF and L1. t = IR_next(ir); while (t != second_goto) { IR * c = t; t = IR_next(t); remove(head, c); add_next(&new_list2, c); } IS_TRUE(t != NULL && t == second_goto, ("???")); remove(head, second_goto); add_next(&new_list2, second_goto); //Swap new_list1 and new_list2 insertbefore(&IF_truebody(ir), IF_truebody(ir), new_list2); //Update the IR_parent for new_list2. for (IR * tmp = new_list2; tmp != NULL; tmp = IR_next(tmp)) { IR_parent(tmp) = ir; } IS_TRUE(IF_truebody(ir) == new_list2, ("illegal insertbefore<T>")); insertafter(&ir, new_list1); //Update the IR_parent for new_list1. for (IR * tmp = new_list1; tmp != NULL; tmp = IR_next(tmp)) { IR_parent(tmp) = IR_parent(ir); } return true; } } } return false; }