+void FuncNode::update_predicate_tree(action_list_t * act_list)
+{
+ if (act_list == NULL || act_list->size() == 0)
+ return;
+/*
+ if (predicate_tree_initialized) {
+ return;
+ }
+ predicate_tree_initialized = true;
+*/
+ /* map a FuncInst to the its predicate */
+ HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
+ HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
+ HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map(128);
+
+ 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);
+ SnapVector<Predicate *> * unset_predicates = new SnapVector<Predicate *>();
+
+ bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, unset_predicates);
+
+ /* no predicate, follow the only branch */
+ if (!branch_found && unset_predicates->size() != 0) {
+ ASSERT(unset_predicates->size() == 1);
+ Predicate * one_branch = (*unset_predicates)[0];
+
+ if (!next_inst->is_single_location()) {
+ Predicate * another_branch = new Predicate(next_inst);
+ // another_branch->copy_predicate_expr(one_branch);
+
+ uint64_t next_read = next_act->get_reads_from_value();
+ bool isnull = ((void*)next_read == NULL);
+ if (isnull) {
+ one_branch->add_predicate_expr(NULLITY, NULL, 1);
+ another_branch->add_predicate_expr(NULLITY, NULL, 0);
+ } else {
+ another_branch->add_predicate_expr(NULLITY, NULL, 1);
+ one_branch->add_predicate_expr(NULLITY, NULL, 0);
+ }
+
+ curr_pred->add_child(another_branch);
+ another_branch->set_parent(curr_pred);
+ }
+
+ curr_pred = one_branch;
+ branch_found = true;
+ }
+
+ delete unset_predicates;
+
+ // check back edges
+ if (!branch_found) {
+ bool backedge_found = false;
+ Predicate * back_pred = curr_pred->get_backedge();
+ if (back_pred != NULL) {
+ curr_pred = back_pred;
+ backedge_found = true;
+ } else if (inst_pred_map.contains(next_inst)) {
+ inst_pred_map.remove(curr_pred->get_func_inst());
+ Predicate * old_pred = inst_pred_map.get(next_inst);
+ back_pred = old_pred->get_parent();
+
+ curr_pred->set_backedge(back_pred);
+ curr_pred = back_pred;
+ backedge_found = true;
+ }
+
+ if (backedge_found)
+ continue;
+ }
+
+ if (!branch_found) {
+ if ( loc_act_map.contains(next_act->get_location()) ) {
+ ModelAction * last_act = loc_act_map.get(next_act->get_location());
+ FuncInst * last_inst = get_inst(last_act);
+
+ Predicate * new_pred1 = new Predicate(next_inst);
+ new_pred1->add_predicate_expr(EQUALITY, last_inst, true);
+
+ Predicate * new_pred2 = new Predicate(next_inst);
+ new_pred2->add_predicate_expr(EQUALITY, last_inst, false);
+
+ curr_pred->add_child(new_pred1);
+ curr_pred->add_child(new_pred2);
+ new_pred1->set_parent(curr_pred);
+ new_pred2->set_parent(curr_pred);
+
+ uint64_t last_read = last_act->get_reads_from_value();
+ uint64_t next_read = next_act->get_reads_from_value();
+
+ if ( last_read == next_read )
+ curr_pred = new_pred1;
+ else
+ curr_pred = new_pred2;
+ } else {
+ Predicate * new_pred = new Predicate(next_inst);
+ curr_pred->add_child(new_pred);
+ new_pred->set_parent(curr_pred);
+
+ if (curr_pred->is_entry_predicate())
+ new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
+
+ curr_pred = new_pred;
+ }
+ }
+
+ if (!inst_pred_map.contains(next_inst))
+ inst_pred_map.put(next_inst, curr_pred);
+
+ loc_act_map.put(next_act->get_location(), next_act);
+ inst_act_map.put(next_inst, next_act);
+ it = it->getNext();
+ }
+}
+
+void FuncNode::deep_update(Predicate * curr_pred)
+{
+ FuncInst * func_inst = curr_pred->get_func_inst();
+ if (func_inst != NULL && !func_inst->is_single_location()) {
+ bool has_null_pred = false;
+ PredExprSet * pred_expressions = curr_pred->get_pred_expressions();
+ PredExprSetIter * pred_expr_it = pred_expressions->iterator();
+ while (pred_expr_it->hasNext()) {
+ pred_expr * pred_expression = pred_expr_it->next();
+ if (pred_expression->token == NULLITY) {
+ has_null_pred = true;
+ break;
+ }
+ }
+
+ if (!has_null_pred) {
+// func_inst->print();
+ Predicate * another_branch = new Predicate(func_inst);
+ another_branch->copy_predicate_expr(curr_pred);
+ another_branch->add_predicate_expr(NULLITY, NULL, 1);
+ curr_pred->add_predicate_expr(NULLITY, NULL, 0);
+
+ Predicate * parent = curr_pred->get_parent();
+ parent->add_child(another_branch);
+// another_branch.add_children(i);
+ }
+ }
+
+ ModelVector<Predicate *> * branches = curr_pred->get_children();
+ for (uint i = 0; i < branches->size(); i++) {
+ Predicate * branch = (*branches)[i];
+ deep_update(branch);
+ }
+}
+
+/* Given curr_pred and next_inst, find the branch following curr_pred that
+ * 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)
+{
+ /* 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++) {
+ Predicate * branch = (*branches)[i];
+ if (branch->get_func_inst() != next_inst)
+ continue;
+
+ /* check against predicate expressions */
+ bool predicate_correct = true;
+ PredExprSet * pred_expressions = branch->get_pred_expressions();
+ PredExprSetIter * pred_expr_it = pred_expressions->iterator();
+
+ if (pred_expressions->getSize() == 0) {
+ predicate_correct = false;
+ unset_predicates->push_back(branch);
+ }
+
+ while (pred_expr_it->hasNext()) {
+ pred_expr * pred_expression = pred_expr_it->next();
+ uint64_t last_read, next_read;
+ bool equality;
+
+ 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 = inst_act_map->get(to_be_compared);
+
+ 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;
+
+ break;
+ case NULLITY:
+ next_read = next_act->get_reads_from_value();
+ equality = ((void*)next_read == NULL);
+ if (equality != pred_expression->value)
+ predicate_correct = false;
+ break;
+ default:
+ predicate_correct = false;
+ model_print("unkown predicate token\n");
+ break;
+ }
+ }
+
+ if (predicate_correct) {
+ *curr_pred = branch;
+ branch_found = true;
+ break;
+ }
+ }
+
+ return branch_found;
+}
+
+void FuncNode::print_predicate_tree()
+{
+ model_print("digraph function_%s {\n", func_name);
+ predicate_tree_entry->print_pred_subtree();
+ model_print("}\n"); // end of graph
+}
+