6 #include "concretepredicate.h"
11 FuncNode::FuncNode(ModelHistory * history) :
21 predicate_tree_position(),
29 predicate_tree_entry = new Predicate(NULL, true);
30 predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
32 predicate_tree_exit = new Predicate(NULL, false, true);
33 predicate_tree_exit->set_depth(MAX_DEPTH);
35 /* Snapshot data structures below */
36 action_list_buffer = new SnapList<action_list_t *>();
37 read_locations = new loc_set_t();
38 write_locations = new loc_set_t();
39 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
40 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
42 //values_may_read_from = new value_set_t();
45 /* Reallocate snapshotted memories when new executions start */
46 void FuncNode::set_new_exec_flag()
48 action_list_buffer = new SnapList<action_list_t *>();
49 read_locations = new loc_set_t();
50 write_locations = new loc_set_t();
51 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
52 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
54 //values_may_read_from = new value_set_t();
57 /* Check whether FuncInst with the same type, position, and location
58 * as act has been added to func_inst_map or not. If not, add it.
60 void FuncNode::add_inst(ModelAction *act)
63 const char * position = act->get_position();
65 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
66 * actions are not tagged with their source line numbers
71 FuncInst * func_inst = func_inst_map.get(position);
73 /* This position has not been inserted into hashtable before */
74 if (func_inst == NULL) {
75 func_inst = create_new_inst(act);
76 func_inst_map.put(position, func_inst);
80 /* Volatile variables that use ++ or -- syntax may result in read and write actions with the same position */
81 if (func_inst->get_type() != act->get_type()) {
82 FuncInst * collision_inst = func_inst->search_in_collision(act);
84 if (collision_inst == NULL) {
85 collision_inst = create_new_inst(act);
86 func_inst->add_to_collision(collision_inst);
89 func_inst = collision_inst;
93 ASSERT(func_inst->get_type() == act->get_type());
94 int curr_execution_number = model->get_execution_number();
96 /* Reset locations when new executions start */
97 if (func_inst->get_execution_number() != curr_execution_number) {
98 func_inst->set_location(act->get_location());
99 func_inst->set_execution_number(curr_execution_number);
102 /* Mark the memory location of such inst as not unique */
103 if (func_inst->get_location() != act->get_location())
104 func_inst->not_single_location();
107 FuncInst * FuncNode::create_new_inst(ModelAction * act)
109 FuncInst * func_inst = new FuncInst(act, this);
110 int exec_num = model->get_execution_number();
111 func_inst->set_execution_number(exec_num);
113 inst_list.push_back(func_inst);
119 /* Get the FuncInst with the same type, position, and location
122 * @return FuncInst with the same type, position, and location as act */
123 FuncInst * FuncNode::get_inst(ModelAction *act)
126 const char * position = act->get_position();
128 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
129 * actions are not tagged with their source line numbers
131 if (position == NULL)
134 FuncInst * inst = func_inst_map.get(position);
138 action_type inst_type = inst->get_type();
139 action_type act_type = act->get_type();
141 if (inst_type == act_type) {
144 /* RMWRCAS actions are converted to RMW or READ actions */
145 else if (inst_type == ATOMIC_RMWRCAS &&
146 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ)) {
149 /* Return the FuncInst in the collision list */
151 return inst->search_in_collision(act);
156 void FuncNode::add_entry_inst(FuncInst * inst)
161 mllnode<FuncInst *> * it;
162 for (it = entry_insts.begin();it != NULL;it = it->getNext()) {
163 if (inst == it->getVal())
167 entry_insts.push_back(inst);
171 * @brief Convert ModelAdtion list to FuncInst list
172 * @param act_list A list of ModelActions
174 void FuncNode::update_tree(action_list_t * act_list)
176 if (act_list == NULL || act_list->size() == 0)
179 HashTable<void *, value_set_t *, uintptr_t, 0> * write_history = history->getWriteHistory();
181 /* build inst_list from act_list for later processing */
182 func_inst_list_t inst_list;
183 action_list_t rw_act_list;
185 for (sllnode<ModelAction *> * it = act_list->begin();it != NULL;it = it->getNext()) {
186 ModelAction * act = it->getVal();
188 if (act->get_original_type() != ATOMIC_NOP && act->get_swap_flag() == false)
189 act->use_original_type();
191 // Remove func_act_ref so that actions can be deleted by Execution::collectActions
192 if (act->is_read()) {
193 // For every read or rmw actions in this list, the reads_from was marked, and not deleted.
194 // So it is safe to call get_reads_from
195 ModelAction * rf = act->get_reads_from();
196 if (rf->get_original_type() != ATOMIC_NOP && rf->get_swap_flag() == false)
197 rf->use_original_type();
199 rf->setFuncActRef(NULL);
201 act->setFuncActRef(NULL);
203 FuncInst * func_inst = get_inst(act);
204 void * loc = act->get_location();
206 if (func_inst == NULL)
209 inst_list.push_back(func_inst);
210 bool act_added = false;
212 if (act->is_write()) {
213 rw_act_list.push_back(act);
215 if (!write_locations->contains(loc)) {
216 write_locations->add(loc);
217 history->update_loc_wr_func_nodes_map(loc, this);
221 if (act->is_read()) {
223 rw_act_list.push_back(act);
225 /* If func_inst may only read_from a single location, then:
227 * The first time an action reads from some location,
228 * import all the values that have been written to this
229 * location from ModelHistory and notify ModelHistory
230 * that this FuncNode may read from this location.
232 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
233 read_locations->add(loc);
234 value_set_t * write_values = write_history->get(loc);
235 add_to_val_loc_map(write_values, loc);
236 history->update_loc_rd_func_nodes_map(loc, this);
241 update_inst_tree(&inst_list);
242 update_predicate_tree(&rw_act_list);
244 // Revert back action types and free
245 for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL;) {
246 ModelAction * act = it->getVal();
247 // Do iteration early in case we delete act
250 // Revert back action types for actions whose types have been changed.
251 if (act->is_read()) {
252 ModelAction * rf = act->get_reads_from();
253 if (rf->get_swap_flag() == true)
254 rf->use_original_type();
257 if (act->get_swap_flag() == true)
258 act->use_original_type();
260 if (act->is_free()) {
262 } else if (act->is_read()) {
264 // reads_from can not be READY_FREE
266 ModelAction *rf = act->get_reads_from();
268 model_print("delete read %d; %p\n", act->get_seq_number(), act);
275 // print_predicate_tree();
279 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
280 * @param inst_list A list of FuncInsts
282 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
284 if (inst_list == NULL)
286 else if (inst_list->size() == 0)
290 sllnode<FuncInst *>* it = inst_list->begin();
291 sllnode<FuncInst *>* prev;
293 /* add the first instruction to the list of entry insts */
294 FuncInst * entry_inst = it->getVal();
295 add_entry_inst(entry_inst);
299 prev = it->getPrev();
301 FuncInst * prev_inst = prev->getVal();
302 FuncInst * curr_inst = it->getVal();
304 prev_inst->add_succ(curr_inst);
305 curr_inst->add_pred(prev_inst);
311 void FuncNode::update_predicate_tree(action_list_t * act_list)
313 if (act_list == NULL || act_list->size() == 0)
317 uint32_t inst_counter = 0;
321 inst_pred_map.reset();
324 // Clear the set of leaves encountered in this path
325 leaves_tmp_storage.clear();
327 sllnode<ModelAction *> *it = act_list->begin();
328 Predicate * curr_pred = predicate_tree_entry;
330 ModelAction * next_act = it->getVal();
331 FuncInst * next_inst = get_inst(next_act);
332 next_inst->set_associated_act(next_act, marker);
334 Predicate * unset_predicate = NULL;
335 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicate);
337 // A branch with unset predicate expression is detected
338 if (!branch_found && unset_predicate != NULL) {
339 bool amended = amend_predicate_expr(curr_pred, next_inst, next_act);
343 curr_pred = unset_predicate;
349 if (!branch_found && inst_id_map.contains(next_inst)) {
350 FuncInst * curr_inst = curr_pred->get_func_inst();
351 uint32_t curr_id = inst_id_map.get(curr_inst);
352 uint32_t next_id = inst_id_map.get(next_inst);
354 if (curr_id >= next_id) {
355 Predicate * old_pred = inst_pred_map.get(next_inst);
356 Predicate * back_pred = old_pred->get_parent();
358 // For updating weights
359 leaves_tmp_storage.push_back(curr_pred);
361 // Add to the set of backedges
362 curr_pred->add_backedge(back_pred);
363 curr_pred = back_pred;
368 // Generate new branches
370 SnapVector<struct half_pred_expr *> half_pred_expressions;
371 infer_predicates(next_inst, next_act, &half_pred_expressions);
372 generate_predicates(curr_pred, next_inst, &half_pred_expressions);
376 if (next_act->is_write())
377 curr_pred->set_write(true);
379 if (next_act->is_read()) {
380 /* Only need to store the locations of read actions */
381 loc_act_map.put(next_act->get_location(), next_act);
384 inst_pred_map.put(next_inst, curr_pred);
385 if (!inst_id_map.contains(next_inst))
386 inst_id_map.put(next_inst, inst_counter++);
389 curr_pred->incr_expl_count();
392 if (curr_pred->get_exit() == NULL) {
393 // Exit predicate is unset yet
394 curr_pred->set_exit(predicate_tree_exit);
397 leaves_tmp_storage.push_back(curr_pred);
398 update_predicate_tree_weight();
401 /* Given curr_pred and next_inst, find the branch following curr_pred that
402 * contains next_inst and the correct predicate.
403 * @return true if branch found, false otherwise.
405 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
406 ModelAction * next_act, Predicate ** unset_predicate)
408 /* Check if a branch with func_inst and corresponding predicate exists */
409 bool branch_found = false;
410 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
411 for (uint i = 0;i < branches->size();i++) {
412 Predicate * branch = (*branches)[i];
413 if (branch->get_func_inst() != next_inst)
416 /* Check against predicate expressions */
417 bool predicate_correct = true;
418 PredExprSet * pred_expressions = branch->get_pred_expressions();
420 /* Only read and rmw actions my have unset predicate expressions */
421 if (pred_expressions->getSize() == 0) {
422 predicate_correct = false;
423 if (*unset_predicate == NULL)
424 *unset_predicate = branch;
431 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
432 while (pred_expr_it->hasNext()) {
433 pred_expr * pred_expression = pred_expr_it->next();
434 uint64_t last_read, next_read;
437 switch(pred_expression->token) {
439 predicate_correct = true;
442 FuncInst * to_be_compared;
443 ModelAction * last_act;
445 to_be_compared = pred_expression->func_inst;
446 last_act = to_be_compared->get_associated_act(marker);
448 last_read = last_act->get_reads_from_value();
449 next_read = next_act->get_reads_from_value();
450 equality = (last_read == next_read);
451 if (equality != pred_expression->value)
452 predicate_correct = false;
456 next_read = next_act->get_reads_from_value();
457 // TODO: implement likely to be null
458 equality = ( (void*) (next_read & 0xffffffff) == NULL);
459 if (equality != pred_expression->value)
460 predicate_correct = false;
463 predicate_correct = false;
464 model_print("unkown predicate token\n");
471 if (predicate_correct) {
481 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
482 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
483 SnapVector<struct half_pred_expr *> * half_pred_expressions)
485 void * loc = next_act->get_location();
487 if (next_inst->is_read()) {
489 if ( loc_act_map.contains(loc) ) {
490 ModelAction * last_act = loc_act_map.get(loc);
491 FuncInst * last_inst = get_inst(last_act);
492 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
493 half_pred_expressions->push_back(expression);
494 } else if ( next_inst->is_single_location() ) {
495 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
497 if (loc_may_equal != NULL) {
498 loc_set_iter * loc_it = loc_may_equal->iterator();
499 while (loc_it->hasNext()) {
500 void * neighbor = loc_it->next();
501 if (loc_act_map.contains(neighbor)) {
502 ModelAction * last_act = loc_act_map.get(neighbor);
503 FuncInst * last_inst = get_inst(last_act);
505 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
506 half_pred_expressions->push_back(expression);
513 // next_inst is not single location
514 uint64_t read_val = next_act->get_reads_from_value();
516 // only infer NULLITY predicate when it is actually NULL.
517 if ( (void*)read_val == NULL) {
518 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
519 half_pred_expressions->push_back(expression);
524 // TODO: do anything here?
528 /* Able to generate complex predicates when there are multiple predciate expressions */
529 void FuncNode::generate_predicates(Predicate * curr_pred, FuncInst * next_inst,
530 SnapVector<struct half_pred_expr *> * half_pred_expressions)
532 if (half_pred_expressions->size() == 0) {
533 Predicate * new_pred = new Predicate(next_inst);
534 curr_pred->add_child(new_pred);
535 new_pred->set_parent(curr_pred);
537 /* Maintain predicate leaves */
538 predicate_leaves.add(new_pred);
539 predicate_leaves.remove(curr_pred);
541 /* entry predicates and predicates containing pure write actions
542 * have no predicate expressions */
543 if ( curr_pred->is_entry_predicate() )
544 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
545 else if (next_inst->is_write()) {
546 /* next_inst->is_write() <==> pure writes */
547 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
553 SnapVector<Predicate *> predicates;
555 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
556 predicates.push_back(new Predicate(next_inst));
557 predicates.push_back(new Predicate(next_inst));
559 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
560 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
562 for (uint i = 1;i < half_pred_expressions->size();i++) {
563 half_expr = (*half_pred_expressions)[i];
565 uint old_size = predicates.size();
566 for (uint j = 0;j < old_size;j++) {
567 Predicate * pred = predicates[j];
568 Predicate * new_pred = new Predicate(next_inst);
569 new_pred->copy_predicate_expr(pred);
571 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
572 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
574 predicates.push_back(new_pred);
578 for (uint i = 0;i < predicates.size();i++) {
579 Predicate * pred= predicates[i];
580 curr_pred->add_child(pred);
581 pred->set_parent(curr_pred);
583 /* Add new predicate leaves */
584 predicate_leaves.add(pred);
587 /* Remove predicate node that has children */
588 predicate_leaves.remove(curr_pred);
590 /* Free memories allocated by infer_predicate */
591 for (uint i = 0;i < half_pred_expressions->size();i++) {
592 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
597 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
598 bool FuncNode::amend_predicate_expr(Predicate * curr_pred, FuncInst * next_inst, ModelAction * next_act)
600 ModelVector<Predicate *> * children = curr_pred->get_children();
602 Predicate * unset_pred = NULL;
603 for (uint i = 0;i < children->size();i++) {
604 Predicate * child = (*children)[i];
605 if (child->get_func_inst() == next_inst) {
611 uint64_t read_val = next_act->get_reads_from_value();
613 // only generate NULLITY predicate when it is actually NULL.
614 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
615 Predicate * new_pred = new Predicate(next_inst);
617 curr_pred->add_child(new_pred);
618 new_pred->set_parent(curr_pred);
620 unset_pred->add_predicate_expr(NULLITY, NULL, false);
621 new_pred->add_predicate_expr(NULLITY, NULL, true);
629 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
631 loc_set_t * locations = val_loc_map->get(val);
633 if (locations == NULL) {
634 locations = new loc_set_t();
635 val_loc_map->put(val, locations);
638 update_loc_may_equal_map(loc, locations);
640 // values_may_read_from->add(val);
643 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
648 value_set_iter * it = values->iterator();
649 while (it->hasNext()) {
650 uint64_t val = it->next();
651 add_to_val_loc_map(val, loc);
657 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
659 if ( old_locations->contains(new_loc) )
662 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
664 if (neighbors == NULL) {
665 neighbors = new loc_set_t();
666 loc_may_equal_map->put(new_loc, neighbors);
669 loc_set_iter * loc_it = old_locations->iterator();
670 while (loc_it->hasNext()) {
671 // new_loc: { old_locations, ... }
672 void * member = loc_it->next();
673 neighbors->add(member);
675 // for each i in old_locations, i : { new_loc, ... }
676 loc_set_t * _neighbors = loc_may_equal_map->get(member);
677 if (_neighbors == NULL) {
678 _neighbors = new loc_set_t();
679 loc_may_equal_map->put(member, _neighbors);
681 _neighbors->add(new_loc);
687 /* Every time a thread enters a function, set its position to the predicate tree entry */
688 void FuncNode::init_predicate_tree_position(thread_id_t tid)
690 int thread_id = id_to_int(tid);
691 if (predicate_tree_position.size() <= (uint) thread_id)
692 predicate_tree_position.resize(thread_id + 1);
694 predicate_tree_position[thread_id] = predicate_tree_entry;
697 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
699 int thread_id = id_to_int(tid);
700 predicate_tree_position[thread_id] = pred;
703 /* @return The position of a thread in a predicate tree */
704 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
706 int thread_id = id_to_int(tid);
707 return predicate_tree_position[thread_id];
710 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
711 void FuncNode::init_inst_act_map(thread_id_t tid)
713 int thread_id = id_to_int(tid);
714 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
715 uint old_size = thrd_inst_act_map->size();
717 if (thrd_inst_act_map->size() <= (uint) thread_id) {
718 uint new_size = thread_id + 1;
719 thrd_inst_act_map->resize(new_size);
721 for (uint i = old_size;i < new_size;i++)
722 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
726 /* Reset elements of thrd_inst_act_map when threads exit functions */
727 void FuncNode::reset_inst_act_map(thread_id_t tid)
729 int thread_id = id_to_int(tid);
730 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
732 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
736 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
738 int thread_id = id_to_int(tid);
739 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
741 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
742 FuncInst * read_inst = get_inst(read_act);
743 map->put(read_inst, read_act);
746 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
748 int thread_id = id_to_int(tid);
749 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
751 return (*thrd_inst_act_map)[thread_id];
754 /* Add FuncNodes that this node may follow */
755 void FuncNode::add_out_edge(FuncNode * other)
757 if ( !edge_table.contains(other) ) {
758 edge_table.put(other, OUT_EDGE);
759 out_edges.push_back(other);
763 edge_type_t edge = edge_table.get(other);
764 if (edge == IN_EDGE) {
765 edge_table.put(other, BI_EDGE);
766 out_edges.push_back(other);
770 /* Compute the distance between this FuncNode and the target node.
771 * Return -1 if the target node is unreachable or the actual distance
772 * is greater than max_step.
774 int FuncNode::compute_distance(FuncNode * target, int max_step)
778 else if (target == this)
781 SnapList<FuncNode *> queue;
782 HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
784 queue.push_back(this);
785 distances.put(this, 0);
787 while (!queue.empty()) {
788 FuncNode * curr = queue.front();
790 int dist = distances.get(curr);
792 if (max_step <= dist)
795 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
796 mllnode<FuncNode *> * it;
797 for (it = outEdges->begin();it != NULL;it = it->getNext()) {
798 FuncNode * out_node = it->getVal();
800 /* This node has not been visited before */
801 if ( !distances.contains(out_node) ) {
802 if (out_node == target)
805 queue.push_back(out_node);
806 distances.put(out_node, dist + 1);
811 /* Target node is unreachable */
815 void FuncNode::add_failed_predicate(Predicate * pred)
817 failed_predicates.add(pred);
820 /* Implement quick sort to sort leaves before assigning base scores */
821 template<typename _Tp>
822 static int partition(ModelVector<_Tp *> * arr, int low, int high)
824 unsigned int pivot = (*arr)[high] -> get_depth();
827 for (int j = low;j <= high - 1;j ++) {
828 if ( (*arr)[j] -> get_depth() < pivot ) {
830 _Tp * tmp = (*arr)[i];
831 (*arr)[i] = (*arr)[j];
836 _Tp * tmp = (*arr)[i + 1];
837 (*arr)[i + 1] = (*arr)[high];
843 /* Implement quick sort to sort leaves before assigning base scores */
844 template<typename _Tp>
845 static void quickSort(ModelVector<_Tp *> * arr, int low, int high)
848 int pi = partition(arr, low, high);
850 quickSort(arr, low, pi - 1);
851 quickSort(arr, pi + 1, high);
855 void FuncNode::assign_initial_weight()
857 PredSetIter * it = predicate_leaves.iterator();
858 leaves_tmp_storage.clear();
860 while (it->hasNext()) {
861 Predicate * pred = it->next();
862 double weight = 100.0 / sqrt(pred->get_expl_count() + pred->get_fail_count() + 1);
863 pred->set_weight(weight);
864 leaves_tmp_storage.push_back(pred);
868 quickSort(&leaves_tmp_storage, 0, leaves_tmp_storage.size() - 1);
870 // assign scores for internal nodes;
871 while ( !leaves_tmp_storage.empty() ) {
872 Predicate * leaf = leaves_tmp_storage.back();
873 leaves_tmp_storage.pop_back();
875 Predicate * curr = leaf->get_parent();
876 while (curr != NULL) {
877 if (curr->get_weight() != 0) {
882 ModelVector<Predicate *> * children = curr->get_children();
883 double weight_sum = 0;
884 bool has_unassigned_node = false;
886 for (uint i = 0;i < children->size();i++) {
887 Predicate * child = (*children)[i];
889 // If a child has unassigned weight
890 double weight = child->get_weight();
892 has_unassigned_node = true;
895 weight_sum += weight;
898 if (!has_unassigned_node) {
899 double average_weight = (double) weight_sum / (double) children->size();
900 double weight = average_weight * pow(0.9, curr->get_depth());
901 curr->set_weight(weight);
905 curr = curr->get_parent();
910 void FuncNode::update_predicate_tree_weight()
913 // Predicate tree is initially built
914 assign_initial_weight();
918 weight_debug_vec.clear();
920 PredSetIter * it = failed_predicates.iterator();
921 while (it->hasNext()) {
922 Predicate * pred = it->next();
923 leaves_tmp_storage.push_back(pred);
926 failed_predicates.reset();
928 quickSort(&leaves_tmp_storage, 0, leaves_tmp_storage.size() - 1);
929 for (uint i = 0;i < leaves_tmp_storage.size();i++) {
930 Predicate * pred = leaves_tmp_storage[i];
931 double weight = 100.0 / sqrt(pred->get_expl_count() + pred->get_fail_count() + 1);
932 pred->set_weight(weight);
935 // Update weights in internal nodes
936 while ( !leaves_tmp_storage.empty() ) {
937 Predicate * leaf = leaves_tmp_storage.back();
938 leaves_tmp_storage.pop_back();
940 Predicate * curr = leaf->get_parent();
941 while (curr != NULL) {
942 ModelVector<Predicate *> * children = curr->get_children();
943 double weight_sum = 0;
944 bool has_unassigned_node = false;
946 for (uint i = 0;i < children->size();i++) {
947 Predicate * child = (*children)[i];
949 double weight = child->get_weight();
951 weight_sum += weight;
952 else if ( predicate_leaves.contains(child) ) {
953 // If this child is a leaf
954 double weight = 100.0 / sqrt(child->get_expl_count() + 1);
955 child->set_weight(weight);
956 weight_sum += weight;
958 has_unassigned_node = true;
959 weight_debug_vec.push_back(child); // For debugging purpose
964 if (!has_unassigned_node) {
965 double average_weight = (double) weight_sum / (double) children->size();
966 double weight = average_weight * pow(0.9, curr->get_depth());
967 curr->set_weight(weight);
971 curr = curr->get_parent();
975 for (uint i = 0;i < weight_debug_vec.size();i++) {
976 Predicate * tmp = weight_debug_vec[i];
977 ASSERT( tmp->get_weight() != 0 );
981 void FuncNode::print_predicate_tree()
983 model_print("digraph function_%s {\n", func_name);
984 predicate_tree_entry->print_pred_subtree();
985 predicate_tree_exit->print_predicate();
986 model_print("}\n"); // end of graph