5a0969d2168d0162b53d5e778c5b090962338d0e
[c11tester.git] / funcnode.cc
1 #include "action.h"
2 #include "history.h"
3 #include "funcnode.h"
4 #include "funcinst.h"
5 #include "predicate.h"
6 #include "concretepredicate.h"
7
8 #include "model.h"
9 #include <cmath>
10
11 FuncNode::FuncNode(ModelHistory * history) :
12         history(history),
13         exit_count(0),
14         inst_counter(1),
15         marker(1),
16         thrd_marker(),
17         func_inst_map(),
18         inst_list(),
19         entry_insts(),
20         thrd_inst_pred_map(),
21         thrd_inst_id_map(),
22         thrd_loc_inst_map(),
23         thrd_predicate_tree_position(),
24         thrd_predicate_trace(),
25         failed_predicates(),
26         edge_table(32),
27         out_edges()
28 {
29         predicate_tree_entry = new Predicate(NULL, true);
30         predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
31
32         predicate_tree_exit = new Predicate(NULL, false, true);
33         predicate_tree_exit->set_depth(MAX_DEPTH);
34
35         /* Snapshot data structures below */
36         read_locations = new loc_set_t();
37         write_locations = new loc_set_t();
38         val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
39         loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
40
41         //values_may_read_from = new value_set_t();
42 }
43
44 /* Reallocate snapshotted memories when new executions start */
45 void FuncNode::set_new_exec_flag()
46 {
47         read_locations = new loc_set_t();
48         write_locations = new loc_set_t();
49         val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
50         loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
51
52         //values_may_read_from = new value_set_t();
53 }
54
55 /* Check whether FuncInst with the same type, position, and location
56  * as act has been added to func_inst_map or not. If not, add it.
57  */
58 void FuncNode::add_inst(ModelAction *act)
59 {
60         ASSERT(act);
61         const char * position = act->get_position();
62
63         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
64          * actions are not tagged with their source line numbers
65          */
66         if (position == NULL)
67                 return;
68
69         FuncInst * func_inst = func_inst_map.get(position);
70
71         /* This position has not been inserted into hashtable before */
72         if (func_inst == NULL) {
73                 func_inst = create_new_inst(act);
74                 func_inst_map.put(position, func_inst);
75                 return;
76         }
77
78         /* Volatile variables that use ++ or -- syntax may result in read and write actions with the same position */
79         if (func_inst->get_type() != act->get_type()) {
80                 FuncInst * collision_inst = func_inst->search_in_collision(act);
81
82                 if (collision_inst == NULL) {
83                         collision_inst = create_new_inst(act);
84                         func_inst->add_to_collision(collision_inst);
85                         return;
86                 } else {
87                         func_inst = collision_inst;
88                 }
89         }
90
91         ASSERT(func_inst->get_type() == act->get_type());
92         int curr_execution_number = model->get_execution_number();
93
94         /* Reset locations when new executions start */
95         if (func_inst->get_execution_number() != curr_execution_number) {
96                 func_inst->set_location(act->get_location());
97                 func_inst->set_execution_number(curr_execution_number);
98         }
99
100         /* Mark the memory location of such inst as not unique */
101         if (func_inst->get_location() != act->get_location())
102                 func_inst->not_single_location();
103 }
104
105 FuncInst * FuncNode::create_new_inst(ModelAction * act)
106 {
107         FuncInst * func_inst = new FuncInst(act, this);
108         int exec_num = model->get_execution_number();
109         func_inst->set_execution_number(exec_num);
110
111         inst_list.push_back(func_inst);
112
113         return func_inst;
114 }
115
116
117 /* Get the FuncInst with the same type, position, and location
118  * as act
119  *
120  * @return FuncInst with the same type, position, and location as act */
121 FuncInst * FuncNode::get_inst(ModelAction *act)
122 {
123         ASSERT(act);
124         const char * position = act->get_position();
125
126         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
127          * actions are not tagged with their source line numbers
128          */
129         if (position == NULL)
130                 return NULL;
131
132         FuncInst * inst = func_inst_map.get(position);
133         if (inst == NULL)
134                 return NULL;
135
136         action_type inst_type = inst->get_type();
137         action_type act_type = act->get_type();
138
139         if (inst_type == act_type) {
140                 return inst;
141         }
142         /* RMWRCAS actions are converted to RMW or READ actions */
143         else if (inst_type == ATOMIC_RMWRCAS &&
144                                          (act_type == ATOMIC_RMW || act_type == ATOMIC_READ)) {
145                 return inst;
146         }
147         /* Return the FuncInst in the collision list */
148         else {
149                 return inst->search_in_collision(act);
150         }
151 }
152
153 void FuncNode::add_entry_inst(FuncInst * inst)
154 {
155         if (inst == NULL)
156                 return;
157
158         mllnode<FuncInst *> * it;
159         for (it = entry_insts.begin();it != NULL;it = it->getNext()) {
160                 if (inst == it->getVal())
161                         return;
162         }
163
164         entry_insts.push_back(inst);
165 }
166
167 void FuncNode::function_entry_handler(thread_id_t tid)
168 {
169         set_marker(tid);
170         init_inst_act_map(tid);
171         init_local_maps(tid);
172         init_predicate_tree_data_structure(tid);
173 }
174
175 void FuncNode::function_exit_handler(thread_id_t tid)
176 {
177         exit_count++;
178
179         reset_inst_act_map(tid);
180         reset_local_maps(tid);
181
182         Predicate * exit_pred = get_predicate_tree_position(tid);
183         if (exit_pred->get_exit() == NULL) {
184                 // Exit predicate is unset yet
185                 exit_pred->set_exit(predicate_tree_exit);
186         }
187
188         update_predicate_tree_weight(tid);
189         reset_predicate_tree_data_structure(tid);
190 }
191
192 /**
193  * @brief Convert ModelAdtion list to FuncInst list
194  * @param act_list A list of ModelActions
195  */
196 void FuncNode::update_tree(ModelAction * act)
197 {
198         bool should_process = act->is_read() || act->is_write();
199         if (!should_process)
200                 return;
201
202         HashTable<void *, value_set_t *, uintptr_t, 0> * write_history = history->getWriteHistory();
203
204         /* build inst_list from act_list for later processing */
205 //      func_inst_list_t inst_list;
206
207         FuncInst * func_inst = get_inst(act);
208         void * loc = act->get_location();
209
210         if (func_inst == NULL)
211                 return;
212
213 //      inst_list.push_back(func_inst);
214
215         if (act->is_write()) {
216                 if (!write_locations->contains(loc)) {
217                         write_locations->add(loc);
218                         history->update_loc_wr_func_nodes_map(loc, this);
219                 }
220
221                 // Do not process writes for now
222                 return;
223         }
224
225         if (act->is_read()) {
226
227                 /* If func_inst may only read_from a single location, then:
228                  *
229                  * The first time an action reads from some location,
230                  * import all the values that have been written to this
231                  * location from ModelHistory and notify ModelHistory
232                  * that this FuncNode may read from this location.
233                  */
234                 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
235                         read_locations->add(loc);
236                         value_set_t * write_values = write_history->get(loc);
237                         add_to_val_loc_map(write_values, loc);
238                         history->update_loc_rd_func_nodes_map(loc, this);
239                 }
240         }
241
242 //      update_inst_tree(&inst_list); TODO
243         update_predicate_tree(act);
244
245 //      print_predicate_tree();
246 }
247
248 /**
249  * @brief Link FuncInsts in inst_list  - add one FuncInst to another's predecessors and successors
250  * @param inst_list A list of FuncInsts
251  */
252 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
253 {
254         if (inst_list == NULL)
255                 return;
256         else if (inst_list->size() == 0)
257                 return;
258
259         /* start linking */
260         sllnode<FuncInst *>* it = inst_list->begin();
261         sllnode<FuncInst *>* prev;
262
263         /* add the first instruction to the list of entry insts */
264         FuncInst * entry_inst = it->getVal();
265         add_entry_inst(entry_inst);
266
267         it = it->getNext();
268         while (it != NULL) {
269                 prev = it->getPrev();
270
271                 FuncInst * prev_inst = prev->getVal();
272                 FuncInst * curr_inst = it->getVal();
273
274                 prev_inst->add_succ(curr_inst);
275                 curr_inst->add_pred(prev_inst);
276
277                 it = it->getNext();
278         }
279 }
280
281 void FuncNode::update_predicate_tree(ModelAction * next_act)
282 {
283         thread_id_t tid = next_act->get_tid();
284         int thread_id = id_to_int(tid);
285         int this_marker = thrd_marker[thread_id];
286
287         loc_inst_map_t * loc_inst_map = thrd_loc_inst_map[thread_id];
288         inst_pred_map_t * inst_pred_map = thrd_inst_pred_map[thread_id];
289         inst_id_map_t * inst_id_map = thrd_inst_id_map[thread_id];
290
291         Predicate * curr_pred = get_predicate_tree_position(tid);
292         while (true) {
293                 FuncInst * next_inst = get_inst(next_act);
294                 next_inst->set_associated_read(tid, next_act->get_reads_from_value(), this_marker);
295
296                 Predicate * unset_predicate = NULL;
297                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicate);
298
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);
302                         if (amended)
303                                 continue;
304                         else {
305                                 curr_pred = unset_predicate;
306                                 branch_found = true;
307                         }
308                 }
309
310                 // Detect loops
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);
315
316                         if (curr_id >= next_id) {
317                                 Predicate * old_pred = inst_pred_map->get(next_inst);
318                                 Predicate * back_pred = old_pred->get_parent();
319
320                                 // Add to the set of backedges
321                                 curr_pred->add_backedge(back_pred);
322                                 curr_pred = back_pred;
323
324                                 continue;
325                         }
326                 }
327
328                 // Generate new branches
329                 if (!branch_found) {
330                         SnapVector<struct half_pred_expr *> half_pred_expressions;
331                         infer_predicates(next_inst, next_act, &half_pred_expressions);
332                         generate_predicates(curr_pred, next_inst, &half_pred_expressions);
333                         continue;
334                 }
335
336                 if (next_act->is_write())
337                         curr_pred->set_write(true);
338
339                 if (next_act->is_read()) {
340                         /* Only need to store the locations of read actions */
341                         loc_inst_map->put(next_inst->get_location(), next_inst);
342                 }
343
344                 inst_pred_map->put(next_inst, curr_pred);
345                 set_predicate_tree_position(tid, curr_pred);
346
347                 if (!inst_id_map->contains(next_inst))
348                         inst_id_map->put(next_inst, inst_counter++);
349
350                 curr_pred->incr_expl_count();
351                 add_predicate_to_trace(tid, curr_pred);
352                 break;
353         }
354 }
355
356 /* Given curr_pred and next_inst, find the branch following curr_pred that
357  * contains next_inst and the correct predicate.
358  * @return true if branch found, false otherwise.
359  */
360 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
361                                                                                                                  ModelAction * next_act, Predicate ** unset_predicate)
362 {
363         /* Check if a branch with func_inst and corresponding predicate exists */
364         bool branch_found = false;
365         thread_id_t tid = next_act->get_tid();
366         int this_marker = thrd_marker[id_to_int(tid)];
367
368         ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
369         for (uint i = 0;i < branches->size();i++) {
370                 Predicate * branch = (*branches)[i];
371                 if (branch->get_func_inst() != next_inst)
372                         continue;
373
374                 /* Check against predicate expressions */
375                 bool predicate_correct = true;
376                 PredExprSet * pred_expressions = branch->get_pred_expressions();
377
378                 /* Only read and rmw actions my have unset predicate expressions */
379                 if (pred_expressions->getSize() == 0) {
380                         predicate_correct = false;
381
382                         if (*unset_predicate == NULL)
383                                 *unset_predicate = branch;
384                         else
385                                 ASSERT(false);
386
387                         continue;
388                 }
389
390                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
391                 while (pred_expr_it->hasNext()) {
392                         pred_expr * pred_expression = pred_expr_it->next();
393                         uint64_t last_read, next_read;
394                         bool equality;
395
396                         switch(pred_expression->token) {
397                         case NOPREDICATE:
398                                 predicate_correct = true;
399                                 break;
400                         case EQUALITY:
401                                 FuncInst * to_be_compared;
402                                 to_be_compared = pred_expression->func_inst;
403
404                                 last_read = to_be_compared->get_associated_read(tid, this_marker);
405                                 ASSERT(last_read != VALUE_NONE);
406
407                                 next_read = next_act->get_reads_from_value();
408                                 equality = (last_read == next_read);
409                                 if (equality != pred_expression->value)
410                                         predicate_correct = false;
411
412                                 break;
413                         case NULLITY:
414                                 next_read = next_act->get_reads_from_value();
415                                 // TODO: implement likely to be null
416                                 equality = ( (void*) (next_read & 0xffffffff) == NULL);
417                                 if (equality != pred_expression->value)
418                                         predicate_correct = false;
419                                 break;
420                         default:
421                                 predicate_correct = false;
422                                 model_print("unkown predicate token\n");
423                                 break;
424                         }
425                 }
426
427                 delete pred_expr_it;
428
429                 if (predicate_correct) {
430                         *curr_pred = branch;
431                         branch_found = true;
432                         break;
433                 }
434         }
435
436         return branch_found;
437 }
438
439 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
440 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
441                                                                                                                                 SnapVector<struct half_pred_expr *> * half_pred_expressions)
442 {
443         void * loc = next_act->get_location();
444         int thread_id = id_to_int(next_act->get_tid());
445         loc_inst_map_t * loc_inst_map = thrd_loc_inst_map[thread_id];
446
447         if (next_inst->is_read()) {
448                 /* read + rmw */
449                 if ( loc_inst_map->contains(loc) ) {
450                         FuncInst * last_inst = loc_inst_map->get(loc);
451                         struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
452                         half_pred_expressions->push_back(expression);
453                 } else if ( next_inst->is_single_location() ) {
454                         loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
455
456                         if (loc_may_equal != NULL) {
457                                 loc_set_iter * loc_it = loc_may_equal->iterator();
458                                 while (loc_it->hasNext()) {
459                                         void * neighbor = loc_it->next();
460                                         if (loc_inst_map->contains(neighbor)) {
461                                                 FuncInst * last_inst = loc_inst_map->get(neighbor);
462
463                                                 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
464                                                 half_pred_expressions->push_back(expression);
465                                         }
466                                 }
467
468                                 delete loc_it;
469                         }
470                 } else {
471                         // next_inst is not single location
472                         uint64_t read_val = next_act->get_reads_from_value();
473
474                         // only infer NULLITY predicate when it is actually NULL.
475                         if ( (void*)read_val == NULL) {
476                                 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
477                                 half_pred_expressions->push_back(expression);
478                         }
479                 }
480         } else {
481                 /* Pure writes */
482                 // TODO: do anything here?
483         }
484 }
485
486 /* Able to generate complex predicates when there are multiple predciate expressions */
487 void FuncNode::generate_predicates(Predicate * curr_pred, FuncInst * next_inst,
488                                                                                                                                          SnapVector<struct half_pred_expr *> * half_pred_expressions)
489 {
490         if (half_pred_expressions->size() == 0) {
491                 Predicate * new_pred = new Predicate(next_inst);
492                 curr_pred->add_child(new_pred);
493                 new_pred->set_parent(curr_pred);
494
495                 /* entry predicates and predicates containing pure write actions
496                  * have no predicate expressions */
497                 if ( curr_pred->is_entry_predicate() )
498                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
499                 else if (next_inst->is_write()) {
500                         /* next_inst->is_write() <==> pure writes */
501                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
502                 }
503
504                 return;
505         }
506
507         SnapVector<Predicate *> predicates;
508
509         struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
510         predicates.push_back(new Predicate(next_inst));
511         predicates.push_back(new Predicate(next_inst));
512
513         predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
514         predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
515
516         for (uint i = 1;i < half_pred_expressions->size();i++) {
517                 half_expr = (*half_pred_expressions)[i];
518
519                 uint old_size = predicates.size();
520                 for (uint j = 0;j < old_size;j++) {
521                         Predicate * pred = predicates[j];
522                         Predicate * new_pred = new Predicate(next_inst);
523                         new_pred->copy_predicate_expr(pred);
524
525                         pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
526                         new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
527
528                         predicates.push_back(new_pred);
529                 }
530         }
531
532         for (uint i = 0;i < predicates.size();i++) {
533                 Predicate * pred= predicates[i];
534                 curr_pred->add_child(pred);
535                 pred->set_parent(curr_pred);
536         }
537
538         /* Free memories allocated by infer_predicate */
539         for (uint i = 0;i < half_pred_expressions->size();i++) {
540                 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
541                 snapshot_free(tmp);
542         }
543 }
544
545 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
546 bool FuncNode::amend_predicate_expr(Predicate * curr_pred, FuncInst * next_inst, ModelAction * next_act)
547 {
548         ModelVector<Predicate *> * children = curr_pred->get_children();
549
550         Predicate * unset_pred = NULL;
551         for (uint i = 0;i < children->size();i++) {
552                 Predicate * child = (*children)[i];
553                 if (child->get_func_inst() == next_inst) {
554                         unset_pred = child;
555                         break;
556                 }
557         }
558
559         uint64_t read_val = next_act->get_reads_from_value();
560
561         // only generate NULLITY predicate when it is actually NULL.
562         if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
563                 Predicate * new_pred = new Predicate(next_inst);
564
565                 curr_pred->add_child(new_pred);
566                 new_pred->set_parent(curr_pred);
567
568                 unset_pred->add_predicate_expr(NULLITY, NULL, false);
569                 new_pred->add_predicate_expr(NULLITY, NULL, true);
570
571                 return true;
572         }
573
574         return false;
575 }
576
577 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
578 {
579         loc_set_t * locations = val_loc_map->get(val);
580
581         if (locations == NULL) {
582                 locations = new loc_set_t();
583                 val_loc_map->put(val, locations);
584         }
585
586         update_loc_may_equal_map(loc, locations);
587         locations->add(loc);
588         // values_may_read_from->add(val);
589 }
590
591 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
592 {
593         if (values == NULL)
594                 return;
595
596         value_set_iter * it = values->iterator();
597         while (it->hasNext()) {
598                 uint64_t val = it->next();
599                 add_to_val_loc_map(val, loc);
600         }
601
602         delete it;
603 }
604
605 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
606 {
607         if ( old_locations->contains(new_loc) )
608                 return;
609
610         loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
611
612         if (neighbors == NULL) {
613                 neighbors = new loc_set_t();
614                 loc_may_equal_map->put(new_loc, neighbors);
615         }
616
617         loc_set_iter * loc_it = old_locations->iterator();
618         while (loc_it->hasNext()) {
619                 // new_loc: { old_locations, ... }
620                 void * member = loc_it->next();
621                 neighbors->add(member);
622
623                 // for each i in old_locations, i : { new_loc, ... }
624                 loc_set_t * _neighbors = loc_may_equal_map->get(member);
625                 if (_neighbors == NULL) {
626                         _neighbors = new loc_set_t();
627                         loc_may_equal_map->put(member, _neighbors);
628                 }
629                 _neighbors->add(new_loc);
630         }
631
632         delete loc_it;
633 }
634
635 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
636 {
637         int thread_id = id_to_int(tid);
638         ModelVector<Predicate *> * stack = thrd_predicate_tree_position[thread_id];
639         (*stack)[stack->size() - 1] = pred;
640 }
641
642 /* @return The position of a thread in a predicate tree */
643 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
644 {
645         int thread_id = id_to_int(tid);
646         return thrd_predicate_tree_position[thread_id]->back();
647 }
648
649 void FuncNode::add_predicate_to_trace(thread_id_t tid, Predicate * pred)
650 {
651         int thread_id = id_to_int(tid);
652         thrd_predicate_trace[thread_id]->back()->push_back(pred);
653 }
654
655 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
656 void FuncNode::init_inst_act_map(thread_id_t tid)
657 {
658         int thread_id = id_to_int(tid);
659         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
660         uint old_size = thrd_inst_act_map->size();
661
662         if (old_size <= (uint) thread_id) {
663                 uint new_size = thread_id + 1;
664                 thrd_inst_act_map->resize(new_size);
665
666                 for (uint i = old_size;i < new_size;i++)
667                         (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
668         }
669 }
670
671 /* Reset elements of thrd_inst_act_map when threads exit functions */
672 void FuncNode::reset_inst_act_map(thread_id_t tid)
673 {
674         int thread_id = id_to_int(tid);
675         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
676
677         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
678         map->reset();
679 }
680
681 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
682 {
683         int thread_id = id_to_int(tid);
684         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
685
686         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
687         FuncInst * read_inst = get_inst(read_act);
688         map->put(read_inst, read_act);
689 }
690
691 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
692 {
693         int thread_id = id_to_int(tid);
694         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
695
696         return (*thrd_inst_act_map)[thread_id];
697 }
698
699 void FuncNode::set_marker(thread_id_t tid)
700 {
701         marker++;
702         uint thread_id = id_to_int(tid);
703         for (uint i = thrd_marker.size(); i < thread_id + 1; i++) {
704                 thrd_marker.push_back(0);
705         }
706
707         thrd_marker[thread_id] = marker;
708 }
709
710 /* Make sure elements of maps are initialized properly when threads enter functions */
711 void FuncNode::init_local_maps(thread_id_t tid)
712 {
713         int thread_id = id_to_int(tid);
714         uint old_size = thrd_loc_inst_map.size();
715
716         if (old_size <= (uint) thread_id) {
717                 uint new_size = thread_id + 1;
718
719                 thrd_loc_inst_map.resize(new_size);
720                 thrd_inst_id_map.resize(new_size);
721                 thrd_inst_pred_map.resize(new_size);
722
723                 for (uint i = old_size; i < new_size; i++) {
724                         thrd_loc_inst_map[i] = new loc_inst_map_t(128);
725                         thrd_inst_id_map[i] = new inst_id_map_t(128);
726                         thrd_inst_pred_map[i] = new inst_pred_map_t(128);
727                 }
728         }
729 }
730
731 /* Reset elements of maps when threads exit functions */
732 void FuncNode::reset_local_maps(thread_id_t tid)
733 {
734         int thread_id = id_to_int(tid);
735         thrd_loc_inst_map[thread_id]->reset();
736         thrd_inst_id_map[thread_id]->reset();
737         thrd_inst_pred_map[thread_id]->reset();
738 }
739
740 void FuncNode::init_predicate_tree_data_structure(thread_id_t tid)
741 {
742         int thread_id = id_to_int(tid);
743         int old_size = thrd_predicate_tree_position.size();
744
745         if (old_size <= thread_id + 1) {
746                 thrd_predicate_tree_position.resize(thread_id + 1);
747                 thrd_predicate_trace.resize(thread_id + 1);
748
749                 for (int i = old_size; i < thread_id + 1; i++) {
750                         thrd_predicate_tree_position[i] = new ModelVector<Predicate *>();
751                         thrd_predicate_trace[i] = new ModelVector<predicate_trace_t *>();
752                 }
753         }
754
755         thrd_predicate_tree_position[thread_id]->push_back(predicate_tree_entry);
756         thrd_predicate_trace[thread_id]->push_back(new predicate_trace_t());
757 }
758
759 void FuncNode::reset_predicate_tree_data_structure(thread_id_t tid)
760 {
761         int thread_id = id_to_int(tid);
762         thrd_predicate_tree_position[thread_id]->pop_back();
763
764         predicate_trace_t * trace = thrd_predicate_trace[thread_id]->back();
765         delete trace;
766         thrd_predicate_trace[thread_id]->pop_back();
767 }
768
769 /* Add FuncNodes that this node may follow */
770 void FuncNode::add_out_edge(FuncNode * other)
771 {
772         if ( !edge_table.contains(other) ) {
773                 edge_table.put(other, OUT_EDGE);
774                 out_edges.push_back(other);
775                 return;
776         }
777
778         edge_type_t edge = edge_table.get(other);
779         if (edge == IN_EDGE) {
780                 edge_table.put(other, BI_EDGE);
781                 out_edges.push_back(other);
782         }
783 }
784
785 /* Compute the distance between this FuncNode and the target node.
786  * Return -1 if the target node is unreachable or the actual distance
787  * is greater than max_step.
788  */
789 int FuncNode::compute_distance(FuncNode * target, int max_step)
790 {
791         if (target == NULL)
792                 return -1;
793         else if (target == this)
794                 return 0;
795
796         SnapList<FuncNode *> queue;
797         HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
798
799         queue.push_back(this);
800         distances.put(this, 0);
801
802         while (!queue.empty()) {
803                 FuncNode * curr = queue.front();
804                 queue.pop_front();
805                 int dist = distances.get(curr);
806
807                 if (max_step <= dist)
808                         return -1;
809
810                 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
811                 mllnode<FuncNode *> * it;
812                 for (it = outEdges->begin();it != NULL;it = it->getNext()) {
813                         FuncNode * out_node = it->getVal();
814
815                         /* This node has not been visited before */
816                         if ( !distances.contains(out_node) ) {
817                                 if (out_node == target)
818                                         return dist + 1;
819
820                                 queue.push_back(out_node);
821                                 distances.put(out_node, dist + 1);
822                         }
823                 }
824         }
825
826         /* Target node is unreachable */
827         return -1;
828 }
829
830 void FuncNode::add_failed_predicate(Predicate * pred)
831 {
832         failed_predicates.add(pred);
833 }
834
835 void FuncNode::update_predicate_tree_weight(thread_id_t tid)
836 {
837         failed_predicates.reset();
838
839         predicate_trace_t * trace = thrd_predicate_trace[id_to_int(tid)]->back();
840
841         // Update predicate weights based on prediate trace
842         for (mllnode<Predicate *> * rit = trace->end(); rit != NULL; rit = rit->getPrev()) {
843                 Predicate * node = rit->getVal();
844                 ModelVector<Predicate *> * children = node->get_children();
845
846                 if (children->size() == 0) {
847                         double weight = 100.0 / sqrt(node->get_expl_count() + node->get_fail_count() + 1);
848                         node->set_weight(weight);
849                 } else {
850                         double weight_sum = 0.0;
851                         for (uint i = 0;i < children->size();i++) {
852                                 Predicate * child = (*children)[i];
853                                 double weight = child->get_weight();
854                                 weight_sum += weight;
855                         }
856
857                         double average_weight = (double) weight_sum / (double) children->size();
858                         double weight = average_weight * pow(0.9, node->get_depth());
859                         node->set_weight(weight);
860                 }
861         }
862 }
863
864 void FuncNode::print_predicate_tree()
865 {
866         model_print("digraph function_%s {\n", func_name);
867         predicate_tree_entry->print_pred_subtree();
868         predicate_tree_exit->print_predicate();
869         model_print("}\n");     // end of graph
870 }