Complete the transfer of deletions of some actions
[c11tester.git] / funcnode.cc
index a3407b44a3ea561088287bdd8c00d3dfe7f9b842..691b2f6924a9f4427f9720f90d439390fb65cb28 100644 (file)
@@ -6,25 +6,37 @@
 #include "concretepredicate.h"
 
 #include "model.h"
+#include <cmath>
 
 FuncNode::FuncNode(ModelHistory * history) :
        history(history),
        exit_count(0),
+       marker(1),
        func_inst_map(),
        inst_list(),
        entry_insts(),
+       inst_pred_map(128),
+       inst_id_map(128),
+       loc_act_map(128),
        predicate_tree_position(),
+       predicate_leaves(),
+       leaves_tmp_storage(),
+       weight_debug_vec(),
+       failed_predicates(),
        edge_table(32),
        out_edges()
 {
        predicate_tree_entry = new Predicate(NULL, true);
        predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
 
-       // Memories that are reclaimed after each execution
+       predicate_tree_exit = new Predicate(NULL, false, true);
+       predicate_tree_exit->set_depth(MAX_DEPTH);
+
+       /* Snapshot data structures below */
        action_list_buffer = new SnapList<action_list_t *>();
        read_locations = new loc_set_t();
        write_locations = new loc_set_t();
-       val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
+       val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
        loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
 
        //values_may_read_from = new value_set_t();
@@ -36,7 +48,7 @@ void FuncNode::set_new_exec_flag()
        action_list_buffer = new SnapList<action_list_t *>();
        read_locations = new loc_set_t();
        write_locations = new loc_set_t();
-       val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
+       val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
        loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
 
        //values_may_read_from = new value_set_t();
@@ -44,9 +56,6 @@ void FuncNode::set_new_exec_flag()
 
 /* Check whether FuncInst with the same type, position, and location
  * as act has been added to func_inst_map or not. If not, add it.
- *
- * Note: currently, actions with the same position are filtered out by process_action,
- * so the collision list of FuncInst is not used. May remove it later. 
  */
 void FuncNode::add_inst(ModelAction *act)
 {
@@ -59,30 +68,54 @@ void FuncNode::add_inst(ModelAction *act)
        if (position == NULL)
                return;
 
-       if ( func_inst_map.contains(position) ) {
-               FuncInst * inst = func_inst_map.get(position);
+       FuncInst * func_inst = func_inst_map.get(position);
+
+       /* This position has not been inserted into hashtable before */
+       if (func_inst == NULL) {
+               func_inst = create_new_inst(act);
+               func_inst_map.put(position, func_inst);
+               return;
+       }
 
-               ASSERT(inst->get_type() == act->get_type());
-               int curr_execution_number = model->get_execution_number();
+       /* Volatile variables that use ++ or -- syntax may result in read and write actions with the same position */
+       if (func_inst->get_type() != act->get_type()) {
+               FuncInst * collision_inst = func_inst->search_in_collision(act);
 
-               /* Reset locations when new executions start */
-               if (inst->get_execution_number() != curr_execution_number) {
-                       inst->set_location(act->get_location());
-                       inst->set_execution_number(curr_execution_number);
+               if (collision_inst == NULL) {
+                       collision_inst = create_new_inst(act);
+                       func_inst->add_to_collision(collision_inst);
+                       return;
+               } else {
+                       func_inst = collision_inst;
                }
+       }
 
-               if (inst->get_location() != act->get_location())
-                       inst->not_single_location();
+       ASSERT(func_inst->get_type() == act->get_type());
+       int curr_execution_number = model->get_execution_number();
 
-               return;
+       /* Reset locations when new executions start */
+       if (func_inst->get_execution_number() != curr_execution_number) {
+               func_inst->set_location(act->get_location());
+               func_inst->set_execution_number(curr_execution_number);
        }
 
+       /* Mark the memory location of such inst as not unique */
+       if (func_inst->get_location() != act->get_location())
+               func_inst->not_single_location();
+}
+
+FuncInst * FuncNode::create_new_inst(ModelAction * act)
+{
        FuncInst * func_inst = new FuncInst(act, this);
+       int exec_num = model->get_execution_number();
+       func_inst->set_execution_number(exec_num);
 
-       func_inst_map.put(position, func_inst);
        inst_list.push_back(func_inst);
+
+       return func_inst;
 }
 
+
 /* Get the FuncInst with the same type, position, and location
  * as act
  *
@@ -105,14 +138,18 @@ FuncInst * FuncNode::get_inst(ModelAction *act)
        action_type inst_type = inst->get_type();
        action_type act_type = act->get_type();
 
-       // else if branch: an RMWRCAS action is converted to a RMW or READ action
-       if (inst_type == act_type)
+       if (inst_type == act_type) {
                return inst;
+       }
+       /* RMWRCAS actions are converted to RMW or READ actions */
        else if (inst_type == ATOMIC_RMWRCAS &&
-                       (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
+                                        (act_type == ATOMIC_RMW || act_type == ATOMIC_READ)) {
                return inst;
-
-       return NULL;
+       }
+       /* Return the FuncInst in the collision list */
+       else {
+               return inst->search_in_collision(act);
+       }
 }
 
 
@@ -122,7 +159,7 @@ void FuncNode::add_entry_inst(FuncInst * inst)
                return;
 
        mllnode<FuncInst *> * it;
-       for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
+       for (it = entry_insts.begin();it != NULL;it = it->getNext()) {
                if (inst == it->getVal())
                        return;
        }
@@ -131,7 +168,7 @@ void FuncNode::add_entry_inst(FuncInst * inst)
 }
 
 /**
- * @brief Convert ModelAdtion list to FuncInst list 
+ * @brief Convert ModelAdtion list to FuncInst list
  * @param act_list A list of ModelActions
  */
 void FuncNode::update_tree(action_list_t * act_list)
@@ -139,14 +176,33 @@ void FuncNode::update_tree(action_list_t * act_list)
        if (act_list == NULL || act_list->size() == 0)
                return;
 
-       HashTable<void *, value_set_t *, uintptr_t, 4> * write_history = history->getWriteHistory();
+       HashTable<void *, value_set_t *, uintptr_t, 0> * write_history = history->getWriteHistory();
+       HashSet<ModelAction *, uintptr_t, 2> write_actions;
 
        /* build inst_list from act_list for later processing */
        func_inst_list_t inst_list;
        action_list_t rw_act_list;
 
-       for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
+       for (sllnode<ModelAction *> * it = act_list->begin();it != NULL;it = it->getNext()) {
                ModelAction * act = it->getVal();
+
+               // Use the original action type and decrement ref count
+               // so that actions may be deleted by Execution::collectActions
+               if (act->get_original_type() != ATOMIC_NOP && act->get_swap_flag() == false)
+                       act->use_original_type();
+
+               act->decr_read_ref_count();
+
+               if (act->is_read()) {
+                       // For every read or rmw actions in this list, the reads_from was marked, and not deleted.
+                       // So it is safe to call get_reads_from
+                       ModelAction * rf = act->get_reads_from();
+                       if (rf->get_original_type() != ATOMIC_NOP && rf->get_swap_flag() == false)
+                               rf->use_original_type();
+
+                       rf->decr_read_ref_count();
+               }
+
                FuncInst * func_inst = get_inst(act);
                void * loc = act->get_location();
 
@@ -163,7 +219,6 @@ void FuncNode::update_tree(action_list_t * act_list)
                                write_locations->add(loc);
                                history->update_loc_wr_func_nodes_map(loc, this);
                        }
-
                }
 
                if (act->is_read()) {
@@ -186,16 +241,65 @@ void FuncNode::update_tree(action_list_t * act_list)
                }
        }
 
-//     model_print("function %s\n", func_name);
-//     print_val_loc_map();
-
        update_inst_tree(&inst_list);
        update_predicate_tree(&rw_act_list);
 
+       // Revert back action types and free
+       for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL;) {
+               ModelAction * act = it->getVal();
+               // Do iteration early in case we delete read actions
+               it = it->getNext();
+
+               // Collect write actions and reads_froms
+               if (act->is_read()) {
+                       if (act->is_rmw()) {
+                               write_actions.add(act);
+                       }
+
+                       ModelAction * rf = act->get_reads_from();
+                       write_actions.add(rf);
+               } else if (act->is_write()) {
+                       write_actions.add(act);
+               }
+
+               // Revert back action types
+               if (act->is_read()) {
+                       ModelAction * rf = act->get_reads_from();
+                       if (rf->get_swap_flag() == true)
+                               rf->use_original_type();
+               }
+
+               if (act->get_swap_flag() == true)
+                       act->use_original_type();
+
+               //  Free read actions
+               if (act->is_read()) {
+                       if (act->is_rmw()) {
+                               // Do nothing. Its reads_from can not be READY_FREE
+                       } else {
+                               ModelAction *rf = act->get_reads_from();
+                               if (rf->is_free()) {
+                                       model_print("delete read %d; %p\n", act->get_seq_number(), act);
+                                       delete act;
+                               }
+                       }
+               }
+       }
+
+       // Free write actions if possible
+       HSIterator<ModelAction *, uintptr_t, 2> * it = write_actions.iterator();
+       while (it->hasNext()) {
+               ModelAction * act = it->next();
+
+               if (act->is_free() && act->get_read_ref_count() == 0)
+                       delete act;
+       }
+       delete it;
+
 //     print_predicate_tree();
 }
 
-/** 
+/**
  * @brief Link FuncInsts in inst_list  - add one FuncInst to another's predecessors and successors
  * @param inst_list A list of FuncInsts
  */
@@ -233,36 +337,34 @@ void FuncNode::update_predicate_tree(action_list_t * act_list)
        if (act_list == NULL || act_list->size() == 0)
                return;
 
-       /* Map a FuncInst to the its predicate */
-       HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
-
-       // Number FuncInsts to detect loops
-       HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
+       incr_marker();
        uint32_t inst_counter = 0;
 
-       /* Only need to store the locations of read actions */
-       HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
-       HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map(128);
+       // Clear hashtables
+       loc_act_map.reset();
+       inst_pred_map.reset();
+       inst_id_map.reset();
+
+       // Clear the set of leaves encountered in this path
+       leaves_tmp_storage.clear();
 
        sllnode<ModelAction *> *it = act_list->begin();
        Predicate * curr_pred = predicate_tree_entry;
        while (it != NULL) {
                ModelAction * next_act = it->getVal();
                FuncInst * next_inst = get_inst(next_act);
+               next_inst->set_associated_act(next_act, marker);
 
-               SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
-               bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, &unset_predicates);
+               Predicate * unset_predicate = NULL;
+               bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicate);
 
                // A branch with unset predicate expression is detected
-               if (!branch_found && unset_predicates.size() != 0) {
-                       ASSERT(unset_predicates.size() == 1);
-                       Predicate * one_branch = unset_predicates[0];
-
-                       bool amended = amend_predicate_expr(&curr_pred, next_inst, next_act);
+               if (!branch_found && unset_predicate != NULL) {
+                       bool amended = amend_predicate_expr(curr_pred, next_inst, next_act);
                        if (amended)
                                continue;
                        else {
-                               curr_pred = one_branch;
+                               curr_pred = unset_predicate;
                                branch_found = true;
                        }
                }
@@ -277,9 +379,12 @@ void FuncNode::update_predicate_tree(action_list_t * act_list)
                                Predicate * old_pred = inst_pred_map.get(next_inst);
                                Predicate * back_pred = old_pred->get_parent();
 
+                               // For updating weights
+                               leaves_tmp_storage.push_back(curr_pred);
+
+                               // Add to the set of backedges
                                curr_pred->add_backedge(back_pred);
                                curr_pred = back_pred;
-
                                continue;
                        }
                }
@@ -287,8 +392,8 @@ void FuncNode::update_predicate_tree(action_list_t * act_list)
                // Generate new branches
                if (!branch_found) {
                        SnapVector<struct half_pred_expr *> half_pred_expressions;
-                       infer_predicates(next_inst, next_act, &loc_act_map, &half_pred_expressions);
-                       generate_predicates(&curr_pred, next_inst, &half_pred_expressions);
+                       infer_predicates(next_inst, next_act, &half_pred_expressions);
+                       generate_predicates(curr_pred, next_inst, &half_pred_expressions);
                        continue;
                }
 
@@ -296,30 +401,38 @@ void FuncNode::update_predicate_tree(action_list_t * act_list)
                        curr_pred->set_write(true);
 
                if (next_act->is_read()) {
+                       /* Only need to store the locations of read actions */
                        loc_act_map.put(next_act->get_location(), next_act);
                }
 
-               inst_act_map.put(next_inst, next_act);
                inst_pred_map.put(next_inst, curr_pred);
                if (!inst_id_map.contains(next_inst))
                        inst_id_map.put(next_inst, inst_counter++);
 
                it = it->getNext();
+               curr_pred->incr_expl_count();
+       }
+
+       if (curr_pred->get_exit() == NULL) {
+               // Exit predicate is unset yet
+               curr_pred->set_exit(predicate_tree_exit);
        }
+
+       leaves_tmp_storage.push_back(curr_pred);
+       update_predicate_tree_weight();
 }
 
 /* Given curr_pred and next_inst, find the branch following curr_pred that
- * contains next_inst and the correct predicate. 
+ * contains next_inst and the correct predicate.
  * @return true if branch found, false otherwise.
  */
-bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
-       HashTable<FuncInst *, ModelAction *, uintptr_t, 0> * inst_act_map,
-       SnapVector<Predicate *> * unset_predicates)
+bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
+                                                                                                                ModelAction * next_act, Predicate ** unset_predicate)
 {
-       /* check if a branch with func_inst and corresponding predicate exists */
+       /* Check if a branch with func_inst and corresponding predicate exists */
        bool branch_found = false;
        ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
-       for (uint i = 0; i < branches->size(); i++) {
+       for (uint i = 0;i < branches->size();i++) {
                Predicate * branch = (*branches)[i];
                if (branch->get_func_inst() != next_inst)
                        continue;
@@ -331,39 +444,53 @@ bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, Model
                /* Only read and rmw actions my have unset predicate expressions */
                if (pred_expressions->getSize() == 0) {
                        predicate_correct = false;
-                       unset_predicates->push_back(branch);
+                       if (*unset_predicate == NULL)
+                               *unset_predicate = branch;
+                       else
+                               ASSERT(false);
+
+                       continue;
                }
 
-               ConcretePredicate * concrete_pred = branch->evaluate(inst_act_map, next_act->get_tid());
-               SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
-               for (uint i = 0; i < concrete_exprs->size(); i++) {
-                       struct concrete_pred_expr concrete = (*concrete_exprs)[i];
-                       uint64_t next_read;
+               PredExprSetIter * pred_expr_it = pred_expressions->iterator();
+               while (pred_expr_it->hasNext()) {
+                       pred_expr * pred_expression = pred_expr_it->next();
+                       uint64_t last_read, next_read;
                        bool equality;
 
-                       switch (concrete.token) {
-                               case NOPREDICATE:
-                                       predicate_correct = true;
-                                       break;
-                               case EQUALITY:
-                                       next_read = next_act->get_reads_from_value();
-                                       equality = (next_read == concrete.value);
-                                       if (equality != concrete.equality)
-                                               predicate_correct = false;
-                                       break;
-                               case NULLITY:
-                                       next_read = next_act->get_reads_from_value();
-                                       equality = ((void*)next_read == NULL);
-                                       if (equality != concrete.equality)
-                                               predicate_correct = false;
-                                       break;
-                               default:
+                       switch(pred_expression->token) {
+                       case NOPREDICATE:
+                               predicate_correct = true;
+                               break;
+                       case EQUALITY:
+                               FuncInst * to_be_compared;
+                               ModelAction * last_act;
+
+                               to_be_compared = pred_expression->func_inst;
+                               last_act = to_be_compared->get_associated_act(marker);
+
+                               last_read = last_act->get_reads_from_value();
+                               next_read = next_act->get_reads_from_value();
+                               equality = (last_read == next_read);
+                               if (equality != pred_expression->value)
                                        predicate_correct = false;
-                                       model_print("unkown predicate token\n");
-                                       break;
+
+                               break;
+                       case NULLITY:
+                               next_read = next_act->get_reads_from_value();
+                               // TODO: implement likely to be null
+                               equality = ( (void*) (next_read & 0xffffffff) == NULL);
+                               if (equality != pred_expression->value)
+                                       predicate_correct = false;
+                               break;
+                       default:
+                               predicate_correct = false;
+                               model_print("unkown predicate token\n");
+                               break;
                        }
                }
-               delete concrete_pred;
+
+               delete pred_expr_it;
 
                if (predicate_correct) {
                        *curr_pred = branch;
@@ -377,33 +504,34 @@ bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, Model
 
 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
-       HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
-       SnapVector<struct half_pred_expr *> * half_pred_expressions)
+                                                                                                                               SnapVector<struct half_pred_expr *> * half_pred_expressions)
 {
        void * loc = next_act->get_location();
 
        if (next_inst->is_read()) {
                /* read + rmw */
-               if ( loc_act_map->contains(loc) ) {
-                       ModelAction * last_act = loc_act_map->get(loc);
+               if ( loc_act_map.contains(loc) ) {
+                       ModelAction * last_act = loc_act_map.get(loc);
                        FuncInst * last_inst = get_inst(last_act);
                        struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
                        half_pred_expressions->push_back(expression);
-               } else if ( next_inst->is_single_location() ){
+               } else if ( next_inst->is_single_location() ) {
                        loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
 
                        if (loc_may_equal != NULL) {
                                loc_set_iter * loc_it = loc_may_equal->iterator();
                                while (loc_it->hasNext()) {
                                        void * neighbor = loc_it->next();
-                                       if (loc_act_map->contains(neighbor)) {
-                                               ModelAction * last_act = loc_act_map->get(neighbor);
+                                       if (loc_act_map.contains(neighbor)) {
+                                               ModelAction * last_act = loc_act_map.get(neighbor);
                                                FuncInst * last_inst = get_inst(last_act);
 
                                                struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
                                                half_pred_expressions->push_back(expression);
                                        }
                                }
+
+                               delete loc_it;
                        }
                } else {
                        // next_inst is not single location
@@ -422,17 +550,21 @@ void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
 }
 
 /* Able to generate complex predicates when there are multiple predciate expressions */
-void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
-       SnapVector<struct half_pred_expr *> * half_pred_expressions)
+void FuncNode::generate_predicates(Predicate * curr_pred, FuncInst * next_inst,
+                                                                                                                                        SnapVector<struct half_pred_expr *> * half_pred_expressions)
 {
        if (half_pred_expressions->size() == 0) {
                Predicate * new_pred = new Predicate(next_inst);
-               (*curr_pred)->add_child(new_pred);
-               new_pred->set_parent(*curr_pred);
+               curr_pred->add_child(new_pred);
+               new_pred->set_parent(curr_pred);
+
+               /* Maintain predicate leaves */
+               predicate_leaves.add(new_pred);
+               predicate_leaves.remove(curr_pred);
 
                /* entry predicates and predicates containing pure write actions
                 * have no predicate expressions */
-               if ( (*curr_pred)->is_entry_predicate() )
+               if ( curr_pred->is_entry_predicate() )
                        new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
                else if (next_inst->is_write()) {
                        /* next_inst->is_write() <==> pure writes */
@@ -451,11 +583,11 @@ void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
        predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
        predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
 
-       for (uint i = 1; i < half_pred_expressions->size(); i++) {
+       for (uint i = 1;i < half_pred_expressions->size();i++) {
                half_expr = (*half_pred_expressions)[i];
 
                uint old_size = predicates.size();
-               for (uint j = 0; j < old_size; j++) {
+               for (uint j = 0;j < old_size;j++) {
                        Predicate * pred = predicates[j];
                        Predicate * new_pred = new Predicate(next_inst);
                        new_pred->copy_predicate_expr(pred);
@@ -467,32 +599,47 @@ void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
                }
        }
 
-       for (uint i = 0; i < predicates.size(); i++) {
+       for (uint i = 0;i < predicates.size();i++) {
                Predicate * pred= predicates[i];
-               (*curr_pred)->add_child(pred);
-               pred->set_parent(*curr_pred);
+               curr_pred->add_child(pred);
+               pred->set_parent(curr_pred);
+
+               /* Add new predicate leaves */
+               predicate_leaves.add(pred);
        }
 
+       /* Remove predicate node that has children */
+       predicate_leaves.remove(curr_pred);
+
        /* Free memories allocated by infer_predicate */
-       for (uint i = 0; i < half_pred_expressions->size(); i++) {
+       for (uint i = 0;i < half_pred_expressions->size();i++) {
                struct half_pred_expr * tmp = (*half_pred_expressions)[i];
                snapshot_free(tmp);
        }
 }
 
 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
-bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
+bool FuncNode::amend_predicate_expr(Predicate * curr_pred, FuncInst * next_inst, ModelAction * next_act)
 {
-       // there should only be only child
-       Predicate * unset_pred = (*curr_pred)->get_children()->back();
+       ModelVector<Predicate *> * children = curr_pred->get_children();
+
+       Predicate * unset_pred = NULL;
+       for (uint i = 0;i < children->size();i++) {
+               Predicate * child = (*children)[i];
+               if (child->get_func_inst() == next_inst) {
+                       unset_pred = child;
+                       break;
+               }
+       }
+
        uint64_t read_val = next_act->get_reads_from_value();
 
        // only generate NULLITY predicate when it is actually NULL.
        if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
                Predicate * new_pred = new Predicate(next_inst);
 
-               (*curr_pred)->add_child(new_pred);
-               new_pred->set_parent(*curr_pred);
+               curr_pred->add_child(new_pred);
+               new_pred->set_parent(curr_pred);
 
                unset_pred->add_predicate_expr(NULLITY, NULL, false);
                new_pred->add_predicate_expr(NULLITY, NULL, true);
@@ -527,6 +674,8 @@ void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
                uint64_t val = it->next();
                add_to_val_loc_map(val, loc);
        }
+
+       delete it;
 }
 
 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
@@ -555,6 +704,8 @@ void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_location
                }
                _neighbors->add(new_loc);
        }
+
+       delete loc_it;
 }
 
 /* Every time a thread enters a function, set its position to the predicate tree entry */
@@ -591,7 +742,7 @@ void FuncNode::init_inst_act_map(thread_id_t tid)
                uint new_size = thread_id + 1;
                thrd_inst_act_map->resize(new_size);
 
-               for (uint i = old_size; i < new_size; i++)
+               for (uint i = old_size;i < new_size;i++)
                        (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
        }
 }
@@ -648,31 +799,35 @@ int FuncNode::compute_distance(FuncNode * target, int max_step)
 {
        if (target == NULL)
                return -1;
+       else if (target == this)
+               return 0;
 
        SnapList<FuncNode *> queue;
-       HashTable<FuncNode *, int, uintptr_t, 0> distances;
+       HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
 
-       int dist = 0;
        queue.push_back(this);
-       distances.put(this, dist);
+       distances.put(this, 0);
 
        while (!queue.empty()) {
                FuncNode * curr = queue.front();
                queue.pop_front();
+               int dist = distances.get(curr);
 
-               if (max_step < dist)
+               if (max_step <= dist)
                        return -1;
-               else if (curr == target)
-                       return dist;
 
-               dist++;
                ModelList<FuncNode *> * outEdges = curr->get_out_edges();
                mllnode<FuncNode *> * it;
-               for (it = outEdges->begin(); it != NULL; it = it->getNext()) {
+               for (it = outEdges->begin();it != NULL;it = it->getNext()) {
                        FuncNode * out_node = it->getVal();
+
+                       /* This node has not been visited before */
                        if ( !distances.contains(out_node) ) {
+                               if (out_node == target)
+                                       return dist + 1;
+
                                queue.push_back(out_node);
-                               distances.put(out_node, dist);
+                               distances.put(out_node, dist + 1);
                        }
                }
        }
@@ -681,28 +836,176 @@ int FuncNode::compute_distance(FuncNode * target, int max_step)
        return -1;
 }
 
-void FuncNode::print_predicate_tree()
+void FuncNode::add_failed_predicate(Predicate * pred)
 {
-       model_print("digraph function_%s {\n", func_name);
-       predicate_tree_entry->print_pred_subtree();
-       model_print("}\n");     // end of graph
+       failed_predicates.add(pred);
+}
+
+/* Implement quick sort to sort leaves before assigning base scores */
+template<typename _Tp>
+static int partition(ModelVector<_Tp *> * arr, int low, int high)
+{
+       unsigned int pivot = (*arr)[high] -> get_depth();
+       int i = low - 1;
+
+       for (int j = low;j <= high - 1;j ++) {
+               if ( (*arr)[j] -> get_depth() < pivot ) {
+                       i ++;
+                       _Tp * tmp = (*arr)[i];
+                       (*arr)[i] = (*arr)[j];
+                       (*arr)[j] = tmp;
+               }
+       }
+
+       _Tp * tmp = (*arr)[i + 1];
+       (*arr)[i + 1] = (*arr)[high];
+       (*arr)[high] = tmp;
+
+       return i + 1;
+}
+
+/* Implement quick sort to sort leaves before assigning base scores */
+template<typename _Tp>
+static void quickSort(ModelVector<_Tp *> * arr, int low, int high)
+{
+       if (low < high) {
+               int pi = partition(arr, low, high);
+
+               quickSort(arr, low, pi - 1);
+               quickSort(arr, pi + 1, high);
+       }
+}
+
+void FuncNode::assign_initial_weight()
+{
+       PredSetIter * it = predicate_leaves.iterator();
+       leaves_tmp_storage.clear();
+
+       while (it->hasNext()) {
+               Predicate * pred = it->next();
+               double weight = 100.0 / sqrt(pred->get_expl_count() + pred->get_fail_count() + 1);
+               pred->set_weight(weight);
+               leaves_tmp_storage.push_back(pred);
+       }
+       delete it;
+
+       quickSort(&leaves_tmp_storage, 0, leaves_tmp_storage.size() - 1);
+
+       // assign scores for internal nodes;
+       while ( !leaves_tmp_storage.empty() ) {
+               Predicate * leaf = leaves_tmp_storage.back();
+               leaves_tmp_storage.pop_back();
+
+               Predicate * curr = leaf->get_parent();
+               while (curr != NULL) {
+                       if (curr->get_weight() != 0) {
+                               // Has been exlpored
+                               break;
+                       }
+
+                       ModelVector<Predicate *> * children = curr->get_children();
+                       double weight_sum = 0;
+                       bool has_unassigned_node = false;
+
+                       for (uint i = 0;i < children->size();i++) {
+                               Predicate * child = (*children)[i];
+
+                               // If a child has unassigned weight
+                               double weight = child->get_weight();
+                               if (weight == 0) {
+                                       has_unassigned_node = true;
+                                       break;
+                               } else
+                                       weight_sum += weight;
+                       }
+
+                       if (!has_unassigned_node) {
+                               double average_weight = (double) weight_sum / (double) children->size();
+                               double weight = average_weight * pow(0.9, curr->get_depth());
+                               curr->set_weight(weight);
+                       } else
+                               break;
+
+                       curr = curr->get_parent();
+               }
+       }
 }
 
-void FuncNode::print_val_loc_map()
+void FuncNode::update_predicate_tree_weight()
 {
-/*
-       value_set_iter * val_it = values_may_read_from->iterator();
-       while (val_it->hasNext()) {
-               uint64_t value = val_it->next();
-               model_print("val %llx: ", value);
-
-               loc_set_t * locations = val_loc_map->get(value);
-               loc_set_iter * loc_it = locations->iterator();
-               while (loc_it->hasNext()) {
-                       void * location = loc_it->next();
-                       model_print("%p ", location);
+       if (marker == 2) {
+               // Predicate tree is initially built
+               assign_initial_weight();
+               return;
+       }
+
+       weight_debug_vec.clear();
+
+       PredSetIter * it = failed_predicates.iterator();
+       while (it->hasNext()) {
+               Predicate * pred = it->next();
+               leaves_tmp_storage.push_back(pred);
+       }
+       delete it;
+       failed_predicates.reset();
+
+       quickSort(&leaves_tmp_storage, 0, leaves_tmp_storage.size() - 1);
+       for (uint i = 0;i < leaves_tmp_storage.size();i++) {
+               Predicate * pred = leaves_tmp_storage[i];
+               double weight = 100.0 / sqrt(pred->get_expl_count() + pred->get_fail_count() + 1);
+               pred->set_weight(weight);
+       }
+
+       // Update weights in internal nodes
+       while ( !leaves_tmp_storage.empty() ) {
+               Predicate * leaf = leaves_tmp_storage.back();
+               leaves_tmp_storage.pop_back();
+
+               Predicate * curr = leaf->get_parent();
+               while (curr != NULL) {
+                       ModelVector<Predicate *> * children = curr->get_children();
+                       double weight_sum = 0;
+                       bool has_unassigned_node = false;
+
+                       for (uint i = 0;i < children->size();i++) {
+                               Predicate * child = (*children)[i];
+
+                               double weight = child->get_weight();
+                               if (weight != 0)
+                                       weight_sum += weight;
+                               else if ( predicate_leaves.contains(child) ) {
+                                       // If this child is a leaf
+                                       double weight = 100.0 / sqrt(child->get_expl_count() + 1);
+                                       child->set_weight(weight);
+                                       weight_sum += weight;
+                               } else {
+                                       has_unassigned_node = true;
+                                       weight_debug_vec.push_back(child);      // For debugging purpose
+                                       break;
+                               }
+                       }
+
+                       if (!has_unassigned_node) {
+                               double average_weight = (double) weight_sum / (double) children->size();
+                               double weight = average_weight * pow(0.9, curr->get_depth());
+                               curr->set_weight(weight);
+                       } else
+                               break;
+
+                       curr = curr->get_parent();
                }
-               model_print("\n");
        }
-*/
+
+       for (uint i = 0;i < weight_debug_vec.size();i++) {
+               Predicate * tmp = weight_debug_vec[i];
+               ASSERT( tmp->get_weight() != 0 );
+       }
+}
+
+void FuncNode::print_predicate_tree()
+{
+       model_print("digraph function_%s {\n", func_name);
+       predicate_tree_entry->print_pred_subtree();
+       predicate_tree_exit->print_predicate();
+       model_print("}\n");     // end of graph
 }