6 #include "concretepredicate.h"
10 FuncNode::FuncNode(ModelHistory * history) :
16 predicate_tree_position(),
20 predicate_tree_entry = new Predicate(NULL, true);
21 predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
23 // Memories that are reclaimed after each execution
24 action_list_buffer = new SnapList<action_list_t *>();
25 read_locations = new loc_set_t();
26 write_locations = new loc_set_t();
27 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
28 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
30 //values_may_read_from = new value_set_t();
33 /* Reallocate snapshotted memories when new executions start */
34 void FuncNode::set_new_exec_flag()
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>();
40 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
42 //values_may_read_from = new value_set_t();
45 /* Check whether FuncInst with the same type, position, and location
46 * as act has been added to func_inst_map or not. If not, add it.
48 * Note: currently, actions with the same position are filtered out by process_action,
49 * so the collision list of FuncInst is not used. May remove it later.
51 void FuncNode::add_inst(ModelAction *act)
54 const char * position = act->get_position();
56 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
57 * actions are not tagged with their source line numbers
62 if ( func_inst_map.contains(position) ) {
63 FuncInst * inst = func_inst_map.get(position);
65 ASSERT(inst->get_type() == act->get_type());
66 int curr_execution_number = model->get_execution_number();
68 /* Reset locations when new executions start */
69 if (inst->get_execution_number() != curr_execution_number) {
70 inst->set_location(act->get_location());
71 inst->set_execution_number(curr_execution_number);
74 if (inst->get_location() != act->get_location())
75 inst->not_single_location();
80 FuncInst * func_inst = new FuncInst(act, this);
82 func_inst_map.put(position, func_inst);
83 inst_list.push_back(func_inst);
86 /* Get the FuncInst with the same type, position, and location
89 * @return FuncInst with the same type, position, and location as act */
90 FuncInst * FuncNode::get_inst(ModelAction *act)
93 const char * position = act->get_position();
95 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
96 * actions are not tagged with their source line numbers
101 FuncInst * inst = func_inst_map.get(position);
105 action_type inst_type = inst->get_type();
106 action_type act_type = act->get_type();
108 // else if branch: an RMWRCAS action is converted to a RMW or READ action
109 if (inst_type == act_type)
111 else if (inst_type == ATOMIC_RMWRCAS &&
112 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
119 void FuncNode::add_entry_inst(FuncInst * inst)
124 mllnode<FuncInst *> * it;
125 for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
126 if (inst == it->getVal())
130 entry_insts.push_back(inst);
134 * @brief Convert ModelAdtion list to FuncInst list
135 * @param act_list A list of ModelActions
137 void FuncNode::update_tree(action_list_t * act_list)
139 if (act_list == NULL || act_list->size() == 0)
142 HashTable<void *, value_set_t *, uintptr_t, 4> * write_history = history->getWriteHistory();
144 /* build inst_list from act_list for later processing */
145 func_inst_list_t inst_list;
146 action_list_t rw_act_list;
148 for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
149 ModelAction * act = it->getVal();
150 FuncInst * func_inst = get_inst(act);
151 void * loc = act->get_location();
153 if (func_inst == NULL)
156 inst_list.push_back(func_inst);
157 bool act_added = false;
159 if (act->is_write()) {
160 rw_act_list.push_back(act);
162 if (!write_locations->contains(loc)) {
163 write_locations->add(loc);
164 history->update_loc_wr_func_nodes_map(loc, this);
169 if (act->is_read()) {
171 rw_act_list.push_back(act);
173 /* If func_inst may only read_from a single location, then:
175 * The first time an action reads from some location,
176 * import all the values that have been written to this
177 * location from ModelHistory and notify ModelHistory
178 * that this FuncNode may read from this location.
180 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
181 read_locations->add(loc);
182 value_set_t * write_values = write_history->get(loc);
183 add_to_val_loc_map(write_values, loc);
184 history->update_loc_func_nodes_map(loc, this);
189 // model_print("function %s\n", func_name);
190 // print_val_loc_map();
192 update_inst_tree(&inst_list);
193 update_predicate_tree(&rw_act_list);
195 // print_predicate_tree();
199 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
200 * @param inst_list A list of FuncInsts
202 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
204 if (inst_list == NULL)
206 else if (inst_list->size() == 0)
210 sllnode<FuncInst *>* it = inst_list->begin();
211 sllnode<FuncInst *>* prev;
213 /* add the first instruction to the list of entry insts */
214 FuncInst * entry_inst = it->getVal();
215 add_entry_inst(entry_inst);
219 prev = it->getPrev();
221 FuncInst * prev_inst = prev->getVal();
222 FuncInst * curr_inst = it->getVal();
224 prev_inst->add_succ(curr_inst);
225 curr_inst->add_pred(prev_inst);
231 void FuncNode::update_predicate_tree(action_list_t * act_list)
233 if (act_list == NULL || act_list->size() == 0)
236 /* Map a FuncInst to the its predicate */
237 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
239 // Number FuncInsts to detect loops
240 HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
241 uint32_t inst_counter = 0;
243 /* Only need to store the locations of read actions */
244 HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
245 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map(128);
247 sllnode<ModelAction *> *it = act_list->begin();
248 Predicate * curr_pred = predicate_tree_entry;
250 ModelAction * next_act = it->getVal();
251 FuncInst * next_inst = get_inst(next_act);
253 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
254 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, &unset_predicates);
256 // A branch with unset predicate expression is detected
257 if (!branch_found && unset_predicates.size() != 0) {
258 ASSERT(unset_predicates.size() == 1);
259 Predicate * one_branch = unset_predicates[0];
261 bool amended = amend_predicate_expr(&curr_pred, next_inst, next_act);
265 curr_pred = one_branch;
271 if (!branch_found && inst_id_map.contains(next_inst)) {
272 FuncInst * curr_inst = curr_pred->get_func_inst();
273 uint32_t curr_id = inst_id_map.get(curr_inst);
274 uint32_t next_id = inst_id_map.get(next_inst);
276 if (curr_id >= next_id) {
277 Predicate * old_pred = inst_pred_map.get(next_inst);
278 Predicate * back_pred = old_pred->get_parent();
280 curr_pred->add_backedge(back_pred);
281 curr_pred = back_pred;
287 // Generate new branches
289 SnapVector<struct half_pred_expr *> half_pred_expressions;
290 infer_predicates(next_inst, next_act, &loc_act_map, &half_pred_expressions);
291 generate_predicates(&curr_pred, next_inst, &half_pred_expressions);
295 if (next_act->is_write())
296 curr_pred->set_write(true);
298 if (next_act->is_read()) {
299 loc_act_map.put(next_act->get_location(), next_act);
302 inst_act_map.put(next_inst, next_act);
303 inst_pred_map.put(next_inst, curr_pred);
304 if (!inst_id_map.contains(next_inst))
305 inst_id_map.put(next_inst, inst_counter++);
311 /* Given curr_pred and next_inst, find the branch following curr_pred that
312 * contains next_inst and the correct predicate.
313 * @return true if branch found, false otherwise.
315 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
316 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> * inst_act_map,
317 SnapVector<Predicate *> * unset_predicates)
319 /* check if a branch with func_inst and corresponding predicate exists */
320 bool branch_found = false;
321 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
322 for (uint i = 0; i < branches->size(); i++) {
323 Predicate * branch = (*branches)[i];
324 if (branch->get_func_inst() != next_inst)
327 /* Check against predicate expressions */
328 bool predicate_correct = true;
329 PredExprSet * pred_expressions = branch->get_pred_expressions();
331 /* Only read and rmw actions my have unset predicate expressions */
332 if (pred_expressions->getSize() == 0) {
333 predicate_correct = false;
334 unset_predicates->push_back(branch);
337 ConcretePredicate * concrete_pred = branch->evaluate(inst_act_map, next_act->get_tid());
338 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
339 for (uint i = 0; i < concrete_exprs->size(); i++) {
340 struct concrete_pred_expr concrete = (*concrete_exprs)[i];
344 switch (concrete.token) {
346 predicate_correct = true;
349 next_read = next_act->get_reads_from_value();
350 equality = (next_read == concrete.value);
351 if (equality != concrete.equality)
352 predicate_correct = false;
355 next_read = next_act->get_reads_from_value();
356 equality = ((void*)next_read == NULL);
357 if (equality != concrete.equality)
358 predicate_correct = false;
361 predicate_correct = false;
362 model_print("unkown predicate token\n");
366 delete concrete_pred;
368 if (predicate_correct) {
378 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
379 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
380 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
381 SnapVector<struct half_pred_expr *> * half_pred_expressions)
383 void * loc = next_act->get_location();
385 if (next_inst->is_read()) {
387 if ( loc_act_map->contains(loc) ) {
388 ModelAction * last_act = loc_act_map->get(loc);
389 FuncInst * last_inst = get_inst(last_act);
390 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
391 half_pred_expressions->push_back(expression);
392 } else if ( next_inst->is_single_location() ){
393 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
395 if (loc_may_equal != NULL) {
396 loc_set_iter * loc_it = loc_may_equal->iterator();
397 while (loc_it->hasNext()) {
398 void * neighbor = loc_it->next();
399 if (loc_act_map->contains(neighbor)) {
400 ModelAction * last_act = loc_act_map->get(neighbor);
401 FuncInst * last_inst = get_inst(last_act);
403 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
404 half_pred_expressions->push_back(expression);
409 // next_inst is not single location
410 uint64_t read_val = next_act->get_reads_from_value();
412 // only infer NULLITY predicate when it is actually NULL.
413 if ( (void*)read_val == NULL) {
414 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
415 half_pred_expressions->push_back(expression);
420 // TODO: do anything here?
424 /* Able to generate complex predicates when there are multiple predciate expressions */
425 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
426 SnapVector<struct half_pred_expr *> * half_pred_expressions)
428 if (half_pred_expressions->size() == 0) {
429 Predicate * new_pred = new Predicate(next_inst);
430 (*curr_pred)->add_child(new_pred);
431 new_pred->set_parent(*curr_pred);
433 /* entry predicates and predicates containing pure write actions
434 * have no predicate expressions */
435 if ( (*curr_pred)->is_entry_predicate() )
436 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
437 else if (next_inst->is_write()) {
438 /* next_inst->is_write() <==> pure writes */
439 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
445 SnapVector<Predicate *> predicates;
447 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
448 predicates.push_back(new Predicate(next_inst));
449 predicates.push_back(new Predicate(next_inst));
451 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
452 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
454 for (uint i = 1; i < half_pred_expressions->size(); i++) {
455 half_expr = (*half_pred_expressions)[i];
457 uint old_size = predicates.size();
458 for (uint j = 0; j < old_size; j++) {
459 Predicate * pred = predicates[j];
460 Predicate * new_pred = new Predicate(next_inst);
461 new_pred->copy_predicate_expr(pred);
463 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
464 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
466 predicates.push_back(new_pred);
470 for (uint i = 0; i < predicates.size(); i++) {
471 Predicate * pred= predicates[i];
472 (*curr_pred)->add_child(pred);
473 pred->set_parent(*curr_pred);
476 /* Free memories allocated by infer_predicate */
477 for (uint i = 0; i < half_pred_expressions->size(); i++) {
478 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
483 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
484 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
486 // there should only be only child
487 Predicate * unset_pred = (*curr_pred)->get_children()->back();
488 uint64_t read_val = next_act->get_reads_from_value();
490 // only generate NULLITY predicate when it is actually NULL.
491 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
492 Predicate * new_pred = new Predicate(next_inst);
494 (*curr_pred)->add_child(new_pred);
495 new_pred->set_parent(*curr_pred);
497 unset_pred->add_predicate_expr(NULLITY, NULL, false);
498 new_pred->add_predicate_expr(NULLITY, NULL, true);
506 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
508 loc_set_t * locations = val_loc_map->get(val);
510 if (locations == NULL) {
511 locations = new loc_set_t();
512 val_loc_map->put(val, locations);
515 update_loc_may_equal_map(loc, locations);
517 // values_may_read_from->add(val);
520 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
525 value_set_iter * it = values->iterator();
526 while (it->hasNext()) {
527 uint64_t val = it->next();
528 add_to_val_loc_map(val, loc);
532 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
534 if ( old_locations->contains(new_loc) )
537 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
539 if (neighbors == NULL) {
540 neighbors = new loc_set_t();
541 loc_may_equal_map->put(new_loc, neighbors);
544 loc_set_iter * loc_it = old_locations->iterator();
545 while (loc_it->hasNext()) {
546 // new_loc: { old_locations, ... }
547 void * member = loc_it->next();
548 neighbors->add(member);
550 // for each i in old_locations, i : { new_loc, ... }
551 loc_set_t * _neighbors = loc_may_equal_map->get(member);
552 if (_neighbors == NULL) {
553 _neighbors = new loc_set_t();
554 loc_may_equal_map->put(member, _neighbors);
556 _neighbors->add(new_loc);
560 /* Every time a thread enters a function, set its position to the predicate tree entry */
561 void FuncNode::init_predicate_tree_position(thread_id_t tid)
563 int thread_id = id_to_int(tid);
564 if (predicate_tree_position.size() <= (uint) thread_id)
565 predicate_tree_position.resize(thread_id + 1);
567 predicate_tree_position[thread_id] = predicate_tree_entry;
570 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
572 int thread_id = id_to_int(tid);
573 predicate_tree_position[thread_id] = pred;
576 /* @return The position of a thread in a predicate tree */
577 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
579 int thread_id = id_to_int(tid);
580 return predicate_tree_position[thread_id];
583 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
584 void FuncNode::init_inst_act_map(thread_id_t tid)
586 int thread_id = id_to_int(tid);
587 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
588 uint old_size = thrd_inst_act_map->size();
590 if (thrd_inst_act_map->size() <= (uint) thread_id) {
591 uint new_size = thread_id + 1;
592 thrd_inst_act_map->resize(new_size);
594 for (uint i = old_size; i < new_size; i++)
595 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
599 /* Reset elements of thrd_inst_act_map when threads exit functions */
600 void FuncNode::reset_inst_act_map(thread_id_t tid)
602 int thread_id = id_to_int(tid);
603 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
605 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
609 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
611 int thread_id = id_to_int(tid);
612 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
614 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
615 FuncInst * read_inst = get_inst(read_act);
616 map->put(read_inst, read_act);
619 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
621 int thread_id = id_to_int(tid);
622 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
624 return (*thrd_inst_act_map)[thread_id];
627 /* Add FuncNodes that this node may follow */
628 void FuncNode::add_out_edge(FuncNode * other)
630 if ( !edge_table.contains(other) ) {
631 edge_table.put(other, OUT_EDGE);
632 out_edges.push_back(other);
636 edge_type_t edge = edge_table.get(other);
637 if (edge == IN_EDGE) {
638 edge_table.put(other, BI_EDGE);
639 out_edges.push_back(other);
643 void FuncNode::print_predicate_tree()
645 model_print("digraph function_%s {\n", func_name);
646 predicate_tree_entry->print_pred_subtree();
647 model_print("}\n"); // end of graph
650 void FuncNode::print_val_loc_map()
653 value_set_iter * val_it = values_may_read_from->iterator();
654 while (val_it->hasNext()) {
655 uint64_t value = val_it->next();
656 model_print("val %llx: ", value);
658 loc_set_t * locations = val_loc_map->get(value);
659 loc_set_iter * loc_it = locations->iterator();
660 while (loc_it->hasNext()) {
661 void * location = loc_it->next();
662 model_print("%p ", location);