3 #include "concretepredicate.h"
5 FuncNode::FuncNode(ModelHistory * history) :
11 predicate_tree_position(),
15 predicate_tree_entry = new Predicate(NULL, true);
16 predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
18 // Memories that are reclaimed after each execution
19 action_list_buffer = new SnapList<action_list_t *>();
20 read_locations = new loc_set_t();
21 write_locations = new loc_set_t();
22 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
23 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
24 thrd_inst_act_map = new SnapVector<inst_act_map_t *>();
26 //values_may_read_from = new value_set_t();
29 /* Reallocate snapshotted memories when new executions start */
30 void FuncNode::set_new_exec_flag()
32 for (mllnode<FuncInst *> * it = inst_list.begin(); it != NULL; it = it->getNext()) {
33 FuncInst * inst = it->getVal();
34 inst->unset_location();
37 action_list_buffer = new SnapList<action_list_t *>();
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>();
41 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
42 thrd_inst_act_map = new SnapVector<inst_act_map_t *>();
44 //values_may_read_from = new value_set_t();
47 /* Check whether FuncInst with the same type, position, and location
48 * as act has been added to func_inst_map or not. If not, add it.
50 * Note: currently, actions with the same position are filtered out by process_action,
51 * so the collision list of FuncInst is not used. May remove it later.
53 void FuncNode::add_inst(ModelAction *act)
56 const char * position = act->get_position();
58 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
59 * actions are not tagged with their source line numbers
64 if ( func_inst_map.contains(position) ) {
65 FuncInst * inst = func_inst_map.get(position);
67 ASSERT(inst->get_type() == act->get_type());
69 // locations are set to NULL when new executions start
70 if (inst->get_location() == NULL)
71 inst->set_location(act->get_location());
73 if (inst->get_location() != act->get_location())
74 inst->not_single_location();
79 FuncInst * func_inst = new FuncInst(act, this);
81 func_inst_map.put(position, func_inst);
82 inst_list.push_back(func_inst);
85 /* Get the FuncInst with the same type, position, and location
88 * @return FuncInst with the same type, position, and location as act */
89 FuncInst * FuncNode::get_inst(ModelAction *act)
92 const char * position = act->get_position();
94 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
95 * actions are not tagged with their source line numbers
100 FuncInst * inst = func_inst_map.get(position);
104 action_type inst_type = inst->get_type();
105 action_type act_type = act->get_type();
107 // else if branch: an RMWRCAS action is converted to a RMW or READ action
108 if (inst_type == act_type)
110 else if (inst_type == ATOMIC_RMWRCAS &&
111 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
118 void FuncNode::add_entry_inst(FuncInst * inst)
123 mllnode<FuncInst *> * it;
124 for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
125 if (inst == it->getVal())
129 entry_insts.push_back(inst);
133 * @brief Convert ModelAdtion list to FuncInst list
134 * @param act_list A list of ModelActions
136 void FuncNode::update_tree(action_list_t * act_list)
138 if (act_list == NULL || act_list->size() == 0)
141 HashTable<void *, value_set_t *, uintptr_t, 4> * write_history = history->getWriteHistory();
143 /* build inst_list from act_list for later processing */
144 func_inst_list_t inst_list;
145 action_list_t rw_act_list;
147 for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
148 ModelAction * act = it->getVal();
149 FuncInst * func_inst = get_inst(act);
150 void * loc = act->get_location();
152 if (func_inst == NULL)
155 inst_list.push_back(func_inst);
156 bool act_added = false;
158 if (act->is_write()) {
159 rw_act_list.push_back(act);
161 if (!write_locations->contains(loc)) {
162 write_locations->add(loc);
163 history->update_loc_wr_func_nodes_map(loc, this);
168 if (act->is_read()) {
170 rw_act_list.push_back(act);
172 /* If func_inst may only read_from a single location, then:
174 * The first time an action reads from some location,
175 * import all the values that have been written to this
176 * location from ModelHistory and notify ModelHistory
177 * that this FuncNode may read from this location.
179 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
180 read_locations->add(loc);
181 value_set_t * write_values = write_history->get(loc);
182 add_to_val_loc_map(write_values, loc);
183 history->update_loc_func_nodes_map(loc, this);
188 // model_print("function %s\n", func_name);
189 // print_val_loc_map();
191 update_inst_tree(&inst_list);
192 update_predicate_tree(&rw_act_list);
194 // print_predicate_tree();
198 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
199 * @param inst_list A list of FuncInsts
201 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
203 if (inst_list == NULL)
205 else if (inst_list->size() == 0)
209 sllnode<FuncInst *>* it = inst_list->begin();
210 sllnode<FuncInst *>* prev;
212 /* add the first instruction to the list of entry insts */
213 FuncInst * entry_inst = it->getVal();
214 add_entry_inst(entry_inst);
218 prev = it->getPrev();
220 FuncInst * prev_inst = prev->getVal();
221 FuncInst * curr_inst = it->getVal();
223 prev_inst->add_succ(curr_inst);
224 curr_inst->add_pred(prev_inst);
230 void FuncNode::update_predicate_tree(action_list_t * act_list)
232 if (act_list == NULL || act_list->size() == 0)
235 /* Map a FuncInst to the its predicate */
236 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
238 // Number FuncInsts to detect loops
239 HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
240 uint32_t inst_counter = 0;
242 /* Only need to store the locations of read actions */
243 HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
244 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map(128);
246 sllnode<ModelAction *> *it = act_list->begin();
247 Predicate * curr_pred = predicate_tree_entry;
249 ModelAction * next_act = it->getVal();
250 FuncInst * next_inst = get_inst(next_act);
252 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
253 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, &unset_predicates);
255 // A branch with unset predicate expression is detected
256 if (!branch_found && unset_predicates.size() != 0) {
257 ASSERT(unset_predicates.size() == 1);
258 Predicate * one_branch = unset_predicates[0];
260 bool amended = amend_predicate_expr(&curr_pred, next_inst, next_act);
264 curr_pred = one_branch;
270 if (!branch_found && inst_id_map.contains(next_inst)) {
271 FuncInst * curr_inst = curr_pred->get_func_inst();
272 uint32_t curr_id = inst_id_map.get(curr_inst);
273 uint32_t next_id = inst_id_map.get(next_inst);
275 if (curr_id >= next_id) {
276 Predicate * old_pred = inst_pred_map.get(next_inst);
277 Predicate * back_pred = old_pred->get_parent();
279 curr_pred->add_backedge(back_pred);
280 curr_pred = back_pred;
286 // Generate new branches
288 SnapVector<struct half_pred_expr *> half_pred_expressions;
289 infer_predicates(next_inst, next_act, &loc_act_map, &half_pred_expressions);
290 generate_predicates(&curr_pred, next_inst, &half_pred_expressions);
294 if (next_act->is_write())
295 curr_pred->set_write(true);
297 if (next_act->is_read()) {
298 loc_act_map.put(next_act->get_location(), next_act);
301 inst_act_map.put(next_inst, next_act);
302 inst_pred_map.put(next_inst, curr_pred);
303 if (!inst_id_map.contains(next_inst))
304 inst_id_map.put(next_inst, inst_counter++);
310 /* Given curr_pred and next_inst, find the branch following curr_pred that
311 * contains next_inst and the correct predicate.
312 * @return true if branch found, false otherwise.
314 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
315 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> * inst_act_map,
316 SnapVector<Predicate *> * unset_predicates)
318 /* check if a branch with func_inst and corresponding predicate exists */
319 bool branch_found = false;
320 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
321 for (uint i = 0; i < branches->size(); i++) {
322 Predicate * branch = (*branches)[i];
323 if (branch->get_func_inst() != next_inst)
326 /* Check against predicate expressions */
327 bool predicate_correct = true;
328 PredExprSet * pred_expressions = branch->get_pred_expressions();
330 /* Only read and rmw actions my have unset predicate expressions */
331 if (pred_expressions->getSize() == 0) {
332 predicate_correct = false;
333 unset_predicates->push_back(branch);
336 ConcretePredicate * concrete_pred = branch->evaluate(inst_act_map, next_act->get_tid());
337 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
338 for (uint i = 0; i < concrete_exprs->size(); i++) {
339 struct concrete_pred_expr concrete = (*concrete_exprs)[i];
343 switch (concrete.token) {
345 predicate_correct = true;
348 next_read = next_act->get_reads_from_value();
349 equality = (next_read == concrete.value);
350 if (equality != concrete.equality)
351 predicate_correct = false;
354 next_read = next_act->get_reads_from_value();
355 equality = ((void*)next_read == NULL);
356 if (equality != concrete.equality)
357 predicate_correct = false;
360 predicate_correct = false;
361 model_print("unkown predicate token\n");
366 if (predicate_correct) {
376 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
377 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
378 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
379 SnapVector<struct half_pred_expr *> * half_pred_expressions)
381 void * loc = next_act->get_location();
383 if (next_inst->is_read()) {
385 if ( loc_act_map->contains(loc) ) {
386 ModelAction * last_act = loc_act_map->get(loc);
387 FuncInst * last_inst = get_inst(last_act);
388 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
389 half_pred_expressions->push_back(expression);
390 } else if ( next_inst->is_single_location() ){
391 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
393 if (loc_may_equal != NULL) {
394 loc_set_iter * loc_it = loc_may_equal->iterator();
395 while (loc_it->hasNext()) {
396 void * neighbor = loc_it->next();
397 if (loc_act_map->contains(neighbor)) {
398 ModelAction * last_act = loc_act_map->get(neighbor);
399 FuncInst * last_inst = get_inst(last_act);
401 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
402 half_pred_expressions->push_back(expression);
407 // next_inst is not single location
408 uint64_t read_val = next_act->get_reads_from_value();
410 // only infer NULLITY predicate when it is actually NULL.
411 if ( (void*)read_val == NULL) {
412 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
413 half_pred_expressions->push_back(expression);
418 // TODO: do anything here?
422 /* Able to generate complex predicates when there are multiple predciate expressions */
423 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
424 SnapVector<struct half_pred_expr *> * half_pred_expressions)
426 if (half_pred_expressions->size() == 0) {
427 Predicate * new_pred = new Predicate(next_inst);
428 (*curr_pred)->add_child(new_pred);
429 new_pred->set_parent(*curr_pred);
431 /* entry predicates and predicates containing pure write actions
432 * have no predicate expressions */
433 if ( (*curr_pred)->is_entry_predicate() )
434 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
435 else if (next_inst->is_write()) {
436 /* next_inst->is_write() <==> pure writes */
437 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
443 SnapVector<Predicate *> predicates;
445 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
446 predicates.push_back(new Predicate(next_inst));
447 predicates.push_back(new Predicate(next_inst));
449 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
450 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
452 for (uint i = 1; i < half_pred_expressions->size(); i++) {
453 half_expr = (*half_pred_expressions)[i];
455 uint old_size = predicates.size();
456 for (uint j = 0; j < old_size; j++) {
457 Predicate * pred = predicates[j];
458 Predicate * new_pred = new Predicate(next_inst);
459 new_pred->copy_predicate_expr(pred);
461 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
462 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
464 predicates.push_back(new_pred);
468 for (uint i = 0; i < predicates.size(); i++) {
469 Predicate * pred= predicates[i];
470 (*curr_pred)->add_child(pred);
471 pred->set_parent(*curr_pred);
474 /* Free memories allocated by infer_predicate */
475 for (uint i = 0; i < half_pred_expressions->size(); i++) {
476 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
481 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
482 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
484 // there should only be only child
485 Predicate * unset_pred = (*curr_pred)->get_children()->back();
486 uint64_t read_val = next_act->get_reads_from_value();
488 // only generate NULLITY predicate when it is actually NULL.
489 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
490 Predicate * new_pred = new Predicate(next_inst);
492 (*curr_pred)->add_child(new_pred);
493 new_pred->set_parent(*curr_pred);
495 unset_pred->add_predicate_expr(NULLITY, NULL, false);
496 new_pred->add_predicate_expr(NULLITY, NULL, true);
504 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
506 loc_set_t * locations = val_loc_map->get(val);
508 if (locations == NULL) {
509 locations = new loc_set_t();
510 val_loc_map->put(val, locations);
513 update_loc_may_equal_map(loc, locations);
515 // values_may_read_from->add(val);
518 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
523 value_set_iter * it = values->iterator();
524 while (it->hasNext()) {
525 uint64_t val = it->next();
526 add_to_val_loc_map(val, loc);
530 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
532 if ( old_locations->contains(new_loc) )
535 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
537 if (neighbors == NULL) {
538 neighbors = new loc_set_t();
539 loc_may_equal_map->put(new_loc, neighbors);
542 loc_set_iter * loc_it = old_locations->iterator();
543 while (loc_it->hasNext()) {
544 // new_loc: { old_locations, ... }
545 void * member = loc_it->next();
546 neighbors->add(member);
548 // for each i in old_locations, i : { new_loc, ... }
549 loc_set_t * _neighbors = loc_may_equal_map->get(member);
550 if (_neighbors == NULL) {
551 _neighbors = new loc_set_t();
552 loc_may_equal_map->put(member, _neighbors);
554 _neighbors->add(new_loc);
558 /* Every time a thread enters a function, set its position to the predicate tree entry */
559 void FuncNode::init_predicate_tree_position(thread_id_t tid)
561 int thread_id = id_to_int(tid);
562 if (predicate_tree_position.size() <= (uint) thread_id)
563 predicate_tree_position.resize(thread_id + 1);
565 predicate_tree_position[thread_id] = predicate_tree_entry;
568 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
570 int thread_id = id_to_int(tid);
571 predicate_tree_position[thread_id] = pred;
574 /* @return The position of a thread in a predicate tree */
575 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
577 int thread_id = id_to_int(tid);
578 return predicate_tree_position[thread_id];
581 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
582 void FuncNode::init_inst_act_map(thread_id_t tid)
584 int thread_id = id_to_int(tid);
585 uint old_size = thrd_inst_act_map->size();
587 if (thrd_inst_act_map->size() <= (uint) thread_id) {
588 uint new_size = thread_id + 1;
589 thrd_inst_act_map->resize(new_size);
591 for (uint i = old_size; i < new_size; i++)
592 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
596 /* Reset elements of thrd_inst_act_map when threads exit functions */
597 void FuncNode::reset_inst_act_map(thread_id_t tid)
599 int thread_id = id_to_int(tid);
600 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
604 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
606 int thread_id = id_to_int(tid);
607 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
608 FuncInst * read_inst = get_inst(read_act);
609 map->put(read_inst, read_act);
612 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
614 int thread_id = id_to_int(tid);
615 return (*thrd_inst_act_map)[thread_id];
618 /* Add FuncNodes that this node may follow */
619 void FuncNode::add_out_edge(FuncNode * other)
621 if ( !edge_table.contains(other) ) {
622 edge_table.put(other, OUT_EDGE);
623 out_edges.push_back(other);
627 edge_type_t edge = edge_table.get(other);
628 if (edge == IN_EDGE) {
629 edge_table.put(other, BI_EDGE);
630 out_edges.push_back(other);
634 void FuncNode::print_predicate_tree()
636 model_print("digraph function_%s {\n", func_name);
637 predicate_tree_entry->print_pred_subtree();
638 model_print("}\n"); // end of graph
641 void FuncNode::print_val_loc_map()
644 value_set_iter * val_it = values_may_read_from->iterator();
645 while (val_it->hasNext()) {
646 uint64_t value = val_it->next();
647 model_print("val %llx: ", value);
649 loc_set_t * locations = val_loc_map->get(value);
650 loc_set_iter * loc_it = locations->iterator();
651 while (loc_it->hasNext()) {
652 void * location = loc_it->next();
653 model_print("%p ", location);