6 #include "concretepredicate.h"
11 FuncNode::FuncNode(ModelHistory * history) :
23 predicate_tree_position(),
31 predicate_tree_entry = new Predicate(NULL, true);
32 predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
34 predicate_tree_exit = new Predicate(NULL, false, true);
35 predicate_tree_exit->set_depth(MAX_DEPTH);
37 /* Snapshot data structures below */
38 read_locations = new loc_set_t();
39 write_locations = new loc_set_t();
40 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
41 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
43 //values_may_read_from = new value_set_t();
46 /* Reallocate snapshotted memories when new executions start */
47 void FuncNode::set_new_exec_flag()
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);
155 void FuncNode::add_entry_inst(FuncInst * inst)
160 mllnode<FuncInst *> * it;
161 for (it = entry_insts.begin();it != NULL;it = it->getNext()) {
162 if (inst == it->getVal())
166 entry_insts.push_back(inst);
169 void FuncNode::function_entry_handler(thread_id_t tid)
172 set_predicate_tree_position(tid, predicate_tree_entry);
173 init_inst_act_map(tid);
177 void FuncNode::function_exit_handler(thread_id_t tid)
181 reset_inst_act_map(tid);
184 Predicate * exit_pred = get_predicate_tree_position(tid);
185 if (exit_pred->get_exit() == NULL) {
186 // Exit predicate is unset yet
187 exit_pred->set_exit(predicate_tree_exit);
190 set_predicate_tree_position(tid, NULL);
194 * @brief Convert ModelAdtion list to FuncInst list
195 * @param act_list A list of ModelActions
197 void FuncNode::update_tree(ModelAction * act)
199 HashTable<void *, value_set_t *, uintptr_t, 0> * write_history = history->getWriteHistory();
201 /* build inst_list from act_list for later processing */
202 // func_inst_list_t inst_list;
204 FuncInst * func_inst = get_inst(act);
205 void * loc = act->get_location();
207 if (func_inst == NULL)
210 // inst_list.push_back(func_inst);
212 if (act->is_write()) {
213 if (!write_locations->contains(loc)) {
214 write_locations->add(loc);
215 history->update_loc_wr_func_nodes_map(loc, this);
218 // Do not process writes for now
222 if (act->is_read()) {
224 /* If func_inst may only read_from a single location, then:
226 * The first time an action reads from some location,
227 * import all the values that have been written to this
228 * location from ModelHistory and notify ModelHistory
229 * that this FuncNode may read from this location.
231 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
232 read_locations->add(loc);
233 value_set_t * write_values = write_history->get(loc);
234 add_to_val_loc_map(write_values, loc);
235 history->update_loc_rd_func_nodes_map(loc, this);
239 // update_inst_tree(&inst_list); TODO
240 update_predicate_tree(act);
242 // print_predicate_tree();
246 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
247 * @param inst_list A list of FuncInsts
249 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
251 if (inst_list == NULL)
253 else if (inst_list->size() == 0)
257 sllnode<FuncInst *>* it = inst_list->begin();
258 sllnode<FuncInst *>* prev;
260 /* add the first instruction to the list of entry insts */
261 FuncInst * entry_inst = it->getVal();
262 add_entry_inst(entry_inst);
266 prev = it->getPrev();
268 FuncInst * prev_inst = prev->getVal();
269 FuncInst * curr_inst = it->getVal();
271 prev_inst->add_succ(curr_inst);
272 curr_inst->add_pred(prev_inst);
278 void FuncNode::update_predicate_tree(ModelAction * next_act)
280 thread_id_t tid = next_act->get_tid();
281 int thread_id = id_to_int(tid);
282 int this_marker = thrd_marker[thread_id];
284 loc_inst_map_t * loc_inst_map = thrd_loc_inst_map[thread_id];
285 inst_pred_map_t * inst_pred_map = thrd_inst_pred_map[thread_id];
286 inst_id_map_t * inst_id_map = thrd_inst_id_map[thread_id];
288 // Clear the set of leaves encountered in this path
289 // leaves_tmp_storage.clear();
291 Predicate * curr_pred = get_predicate_tree_position(tid);
293 FuncInst * next_inst = get_inst(next_act);
294 next_inst->set_associated_read(tid, next_act->get_reads_from_value(), this_marker);
296 Predicate * unset_predicate = NULL;
297 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicate);
299 // A branch with unset predicate expression is detected
300 if (!branch_found && unset_predicate != NULL) {
301 bool amended = amend_predicate_expr(curr_pred, next_inst, next_act);
305 curr_pred = unset_predicate;
311 if (!branch_found && inst_id_map->contains(next_inst)) {
312 FuncInst * curr_inst = curr_pred->get_func_inst();
313 uint32_t curr_id = inst_id_map->get(curr_inst);
314 uint32_t next_id = inst_id_map->get(next_inst);
316 if (curr_id >= next_id) {
317 Predicate * old_pred = inst_pred_map->get(next_inst);
318 Predicate * back_pred = old_pred->get_parent();
320 // For updating weights
321 leaves_tmp_storage.push_back(curr_pred);
323 // Add to the set of backedges
324 curr_pred->add_backedge(back_pred);
325 curr_pred = back_pred;
331 // Generate new branches
333 SnapVector<struct half_pred_expr *> half_pred_expressions;
334 infer_predicates(next_inst, next_act, &half_pred_expressions);
335 generate_predicates(curr_pred, next_inst, &half_pred_expressions);
339 if (next_act->is_write())
340 curr_pred->set_write(true);
342 if (next_act->is_read()) {
343 /* Only need to store the locations of read actions */
344 loc_inst_map->put(next_inst->get_location(), next_inst);
347 inst_pred_map->put(next_inst, curr_pred);
348 set_predicate_tree_position(tid, curr_pred);
350 if (!inst_id_map->contains(next_inst))
351 inst_id_map->put(next_inst, inst_counter++);
353 curr_pred->incr_expl_count();
357 // leaves_tmp_storage.push_back(curr_pred);
358 // update_predicate_tree_weight();
361 /* Given curr_pred and next_inst, find the branch following curr_pred that
362 * contains next_inst and the correct predicate.
363 * @return true if branch found, false otherwise.
365 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
366 ModelAction * next_act, Predicate ** unset_predicate)
368 /* Check if a branch with func_inst and corresponding predicate exists */
369 bool branch_found = false;
370 thread_id_t tid = next_act->get_tid();
371 int this_marker = thrd_marker[id_to_int(tid)];
373 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
374 for (uint i = 0;i < branches->size();i++) {
375 Predicate * branch = (*branches)[i];
376 if (branch->get_func_inst() != next_inst)
379 /* Check against predicate expressions */
380 bool predicate_correct = true;
381 PredExprSet * pred_expressions = branch->get_pred_expressions();
383 /* Only read and rmw actions my have unset predicate expressions */
384 if (pred_expressions->getSize() == 0) {
385 predicate_correct = false;
387 if (*unset_predicate == NULL)
388 *unset_predicate = branch;
395 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
396 while (pred_expr_it->hasNext()) {
397 pred_expr * pred_expression = pred_expr_it->next();
398 uint64_t last_read, next_read;
401 switch(pred_expression->token) {
403 predicate_correct = true;
406 FuncInst * to_be_compared;
407 to_be_compared = pred_expression->func_inst;
409 last_read = to_be_compared->get_associated_read(tid, this_marker);
410 ASSERT(last_read != VALUE_NONE);
412 next_read = next_act->get_reads_from_value();
413 equality = (last_read == next_read);
414 if (equality != pred_expression->value)
415 predicate_correct = false;
419 next_read = next_act->get_reads_from_value();
420 // TODO: implement likely to be null
421 equality = ( (void*) (next_read & 0xffffffff) == NULL);
422 if (equality != pred_expression->value)
423 predicate_correct = false;
426 predicate_correct = false;
427 model_print("unkown predicate token\n");
434 if (predicate_correct) {
444 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
445 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
446 SnapVector<struct half_pred_expr *> * half_pred_expressions)
448 void * loc = next_act->get_location();
449 int thread_id = id_to_int(next_act->get_tid());
450 loc_inst_map_t * loc_inst_map = thrd_loc_inst_map[thread_id];
452 if (next_inst->is_read()) {
454 if ( loc_inst_map->contains(loc) ) {
455 FuncInst * last_inst = loc_inst_map->get(loc);
456 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
457 half_pred_expressions->push_back(expression);
458 } else if ( next_inst->is_single_location() ) {
459 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
461 if (loc_may_equal != NULL) {
462 loc_set_iter * loc_it = loc_may_equal->iterator();
463 while (loc_it->hasNext()) {
464 void * neighbor = loc_it->next();
465 if (loc_inst_map->contains(neighbor)) {
466 FuncInst * last_inst = loc_inst_map->get(neighbor);
468 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
469 half_pred_expressions->push_back(expression);
476 // next_inst is not single location
477 uint64_t read_val = next_act->get_reads_from_value();
479 // only infer NULLITY predicate when it is actually NULL.
480 if ( (void*)read_val == NULL) {
481 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
482 half_pred_expressions->push_back(expression);
487 // TODO: do anything here?
491 /* Able to generate complex predicates when there are multiple predciate expressions */
492 void FuncNode::generate_predicates(Predicate * curr_pred, FuncInst * next_inst,
493 SnapVector<struct half_pred_expr *> * half_pred_expressions)
495 if (half_pred_expressions->size() == 0) {
496 Predicate * new_pred = new Predicate(next_inst);
497 curr_pred->add_child(new_pred);
498 new_pred->set_parent(curr_pred);
500 /* Maintain predicate leaves */
501 predicate_leaves.add(new_pred);
502 predicate_leaves.remove(curr_pred);
504 /* entry predicates and predicates containing pure write actions
505 * have no predicate expressions */
506 if ( curr_pred->is_entry_predicate() )
507 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
508 else if (next_inst->is_write()) {
509 /* next_inst->is_write() <==> pure writes */
510 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
516 SnapVector<Predicate *> predicates;
518 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
519 predicates.push_back(new Predicate(next_inst));
520 predicates.push_back(new Predicate(next_inst));
522 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
523 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
525 for (uint i = 1;i < half_pred_expressions->size();i++) {
526 half_expr = (*half_pred_expressions)[i];
528 uint old_size = predicates.size();
529 for (uint j = 0;j < old_size;j++) {
530 Predicate * pred = predicates[j];
531 Predicate * new_pred = new Predicate(next_inst);
532 new_pred->copy_predicate_expr(pred);
534 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
535 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
537 predicates.push_back(new_pred);
541 for (uint i = 0;i < predicates.size();i++) {
542 Predicate * pred= predicates[i];
543 curr_pred->add_child(pred);
544 pred->set_parent(curr_pred);
546 /* Add new predicate leaves */
547 predicate_leaves.add(pred);
550 /* Remove predicate node that has children */
551 predicate_leaves.remove(curr_pred);
553 /* Free memories allocated by infer_predicate */
554 for (uint i = 0;i < half_pred_expressions->size();i++) {
555 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
560 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
561 bool FuncNode::amend_predicate_expr(Predicate * curr_pred, FuncInst * next_inst, ModelAction * next_act)
563 ModelVector<Predicate *> * children = curr_pred->get_children();
565 Predicate * unset_pred = NULL;
566 for (uint i = 0;i < children->size();i++) {
567 Predicate * child = (*children)[i];
568 if (child->get_func_inst() == next_inst) {
574 uint64_t read_val = next_act->get_reads_from_value();
576 // only generate NULLITY predicate when it is actually NULL.
577 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
578 Predicate * new_pred = new Predicate(next_inst);
580 curr_pred->add_child(new_pred);
581 new_pred->set_parent(curr_pred);
583 unset_pred->add_predicate_expr(NULLITY, NULL, false);
584 new_pred->add_predicate_expr(NULLITY, NULL, true);
592 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
594 loc_set_t * locations = val_loc_map->get(val);
596 if (locations == NULL) {
597 locations = new loc_set_t();
598 val_loc_map->put(val, locations);
601 update_loc_may_equal_map(loc, locations);
603 // values_may_read_from->add(val);
606 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
611 value_set_iter * it = values->iterator();
612 while (it->hasNext()) {
613 uint64_t val = it->next();
614 add_to_val_loc_map(val, loc);
620 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
622 if ( old_locations->contains(new_loc) )
625 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
627 if (neighbors == NULL) {
628 neighbors = new loc_set_t();
629 loc_may_equal_map->put(new_loc, neighbors);
632 loc_set_iter * loc_it = old_locations->iterator();
633 while (loc_it->hasNext()) {
634 // new_loc: { old_locations, ... }
635 void * member = loc_it->next();
636 neighbors->add(member);
638 // for each i in old_locations, i : { new_loc, ... }
639 loc_set_t * _neighbors = loc_may_equal_map->get(member);
640 if (_neighbors == NULL) {
641 _neighbors = new loc_set_t();
642 loc_may_equal_map->put(member, _neighbors);
644 _neighbors->add(new_loc);
650 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
652 int thread_id = id_to_int(tid);
653 if (predicate_tree_position.size() <= (uint) thread_id)
654 predicate_tree_position.resize(thread_id + 1);
656 predicate_tree_position[thread_id] = pred;
659 /* @return The position of a thread in a predicate tree */
660 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
662 int thread_id = id_to_int(tid);
663 return predicate_tree_position[thread_id];
666 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
667 void FuncNode::init_inst_act_map(thread_id_t tid)
669 int thread_id = id_to_int(tid);
670 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
671 uint old_size = thrd_inst_act_map->size();
673 if (old_size <= (uint) thread_id) {
674 uint new_size = thread_id + 1;
675 thrd_inst_act_map->resize(new_size);
677 for (uint i = old_size;i < new_size;i++)
678 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
682 /* Reset elements of thrd_inst_act_map when threads exit functions */
683 void FuncNode::reset_inst_act_map(thread_id_t tid)
685 int thread_id = id_to_int(tid);
686 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
688 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
692 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
694 int thread_id = id_to_int(tid);
695 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
697 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
698 FuncInst * read_inst = get_inst(read_act);
699 map->put(read_inst, read_act);
702 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
704 int thread_id = id_to_int(tid);
705 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
707 return (*thrd_inst_act_map)[thread_id];
710 void FuncNode::set_marker(thread_id_t tid)
713 uint thread_id = id_to_int(tid);
714 for (uint i = thrd_marker.size(); i < thread_id + 1; i++) {
715 thrd_marker.push_back(0);
718 thrd_marker[thread_id] = marker;
721 /* Make sure elements of maps are initialized properly when threads enter functions */
722 void FuncNode::init_maps(thread_id_t tid)
724 int thread_id = id_to_int(tid);
725 uint old_size = thrd_loc_inst_map.size();
727 if (old_size <= (uint) thread_id) {
728 uint new_size = thread_id + 1;
729 thrd_loc_inst_map.resize(new_size);
730 thrd_inst_id_map.resize(new_size);
731 thrd_inst_pred_map.resize(new_size);
733 for (uint i = old_size; i < new_size; i++) {
734 thrd_loc_inst_map[i] = new loc_inst_map_t(128);
735 thrd_inst_id_map[i] = new inst_id_map_t(128);
736 thrd_inst_pred_map[i] = new inst_pred_map_t(128);
741 /* Reset elements of maps when threads exit functions */
742 void FuncNode::reset_maps(thread_id_t tid)
744 int thread_id = id_to_int(tid);
745 thrd_loc_inst_map[thread_id]->reset();
746 thrd_inst_id_map[thread_id]->reset();
747 thrd_inst_pred_map[thread_id]->reset();
750 /* Add FuncNodes that this node may follow */
751 void FuncNode::add_out_edge(FuncNode * other)
753 if ( !edge_table.contains(other) ) {
754 edge_table.put(other, OUT_EDGE);
755 out_edges.push_back(other);
759 edge_type_t edge = edge_table.get(other);
760 if (edge == IN_EDGE) {
761 edge_table.put(other, BI_EDGE);
762 out_edges.push_back(other);
766 /* Compute the distance between this FuncNode and the target node.
767 * Return -1 if the target node is unreachable or the actual distance
768 * is greater than max_step.
770 int FuncNode::compute_distance(FuncNode * target, int max_step)
774 else if (target == this)
777 SnapList<FuncNode *> queue;
778 HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
780 queue.push_back(this);
781 distances.put(this, 0);
783 while (!queue.empty()) {
784 FuncNode * curr = queue.front();
786 int dist = distances.get(curr);
788 if (max_step <= dist)
791 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
792 mllnode<FuncNode *> * it;
793 for (it = outEdges->begin();it != NULL;it = it->getNext()) {
794 FuncNode * out_node = it->getVal();
796 /* This node has not been visited before */
797 if ( !distances.contains(out_node) ) {
798 if (out_node == target)
801 queue.push_back(out_node);
802 distances.put(out_node, dist + 1);
807 /* Target node is unreachable */
811 void FuncNode::add_failed_predicate(Predicate * pred)
813 failed_predicates.add(pred);
816 /* Implement quick sort to sort leaves before assigning base scores */
817 template<typename _Tp>
818 static int partition(ModelVector<_Tp *> * arr, int low, int high)
820 unsigned int pivot = (*arr)[high] -> get_depth();
823 for (int j = low;j <= high - 1;j ++) {
824 if ( (*arr)[j] -> get_depth() < pivot ) {
826 _Tp * tmp = (*arr)[i];
827 (*arr)[i] = (*arr)[j];
832 _Tp * tmp = (*arr)[i + 1];
833 (*arr)[i + 1] = (*arr)[high];
839 /* Implement quick sort to sort leaves before assigning base scores */
840 template<typename _Tp>
841 static void quickSort(ModelVector<_Tp *> * arr, int low, int high)
844 int pi = partition(arr, low, high);
846 quickSort(arr, low, pi - 1);
847 quickSort(arr, pi + 1, high);
851 void FuncNode::assign_initial_weight()
853 PredSetIter * it = predicate_leaves.iterator();
854 leaves_tmp_storage.clear();
856 while (it->hasNext()) {
857 Predicate * pred = it->next();
858 double weight = 100.0 / sqrt(pred->get_expl_count() + pred->get_fail_count() + 1);
859 pred->set_weight(weight);
860 leaves_tmp_storage.push_back(pred);
864 quickSort(&leaves_tmp_storage, 0, leaves_tmp_storage.size() - 1);
866 // assign scores for internal nodes;
867 while ( !leaves_tmp_storage.empty() ) {
868 Predicate * leaf = leaves_tmp_storage.back();
869 leaves_tmp_storage.pop_back();
871 Predicate * curr = leaf->get_parent();
872 while (curr != NULL) {
873 if (curr->get_weight() != 0) {
878 ModelVector<Predicate *> * children = curr->get_children();
879 double weight_sum = 0;
880 bool has_unassigned_node = false;
882 for (uint i = 0;i < children->size();i++) {
883 Predicate * child = (*children)[i];
885 // If a child has unassigned weight
886 double weight = child->get_weight();
888 has_unassigned_node = true;
891 weight_sum += weight;
894 if (!has_unassigned_node) {
895 double average_weight = (double) weight_sum / (double) children->size();
896 double weight = average_weight * pow(0.9, curr->get_depth());
897 curr->set_weight(weight);
901 curr = curr->get_parent();
906 void FuncNode::update_predicate_tree_weight()
909 // Predicate tree is initially built
910 assign_initial_weight();
914 weight_debug_vec.clear();
916 PredSetIter * it = failed_predicates.iterator();
917 while (it->hasNext()) {
918 Predicate * pred = it->next();
919 leaves_tmp_storage.push_back(pred);
922 failed_predicates.reset();
924 quickSort(&leaves_tmp_storage, 0, leaves_tmp_storage.size() - 1);
925 for (uint i = 0;i < leaves_tmp_storage.size();i++) {
926 Predicate * pred = leaves_tmp_storage[i];
927 double weight = 100.0 / sqrt(pred->get_expl_count() + pred->get_fail_count() + 1);
928 pred->set_weight(weight);
931 // Update weights in internal nodes
932 while ( !leaves_tmp_storage.empty() ) {
933 Predicate * leaf = leaves_tmp_storage.back();
934 leaves_tmp_storage.pop_back();
936 Predicate * curr = leaf->get_parent();
937 while (curr != NULL) {
938 ModelVector<Predicate *> * children = curr->get_children();
939 double weight_sum = 0;
940 bool has_unassigned_node = false;
942 for (uint i = 0;i < children->size();i++) {
943 Predicate * child = (*children)[i];
945 double weight = child->get_weight();
947 weight_sum += weight;
948 else if ( predicate_leaves.contains(child) ) {
949 // If this child is a leaf
950 double weight = 100.0 / sqrt(child->get_expl_count() + 1);
951 child->set_weight(weight);
952 weight_sum += weight;
954 has_unassigned_node = true;
955 weight_debug_vec.push_back(child); // For debugging purpose
960 if (!has_unassigned_node) {
961 double average_weight = (double) weight_sum / (double) children->size();
962 double weight = average_weight * pow(0.9, curr->get_depth());
963 curr->set_weight(weight);
967 curr = curr->get_parent();
971 for (uint i = 0;i < weight_debug_vec.size();i++) {
972 Predicate * tmp = weight_debug_vec[i];
973 ASSERT( tmp->get_weight() != 0 );
977 void FuncNode::print_predicate_tree()
979 model_print("digraph function_%s {\n", func_name);
980 predicate_tree_entry->print_pred_subtree();
981 predicate_tree_exit->print_predicate();
982 model_print("}\n"); // end of graph