Split FuncNode::get_or_add_inst into two separate functions. Call add_inst inside...
[c11tester.git] / funcnode.cc
1 #include "funcnode.h"
2 #include <fcntl.h>
3
4 FuncNode::FuncNode() :
5         predicate_tree_initialized(false),
6         func_inst_map(),
7         inst_list(),
8         entry_insts(),
9         thrd_read_map(),
10         predicate_tree_entry()
11 {}
12
13 /* Check whether FuncInst with the same type, position, and location
14  * as act has been added to func_inst_map or not. If not, add it.
15  *
16  * Note: currently, actions with the same position are filtered out by process_action,
17  * so the collision list of FuncInst is not used. May remove it later. 
18  */
19 void FuncNode::add_inst(ModelAction *act)
20 {
21         ASSERT(act);
22         const char * position = act->get_position();
23
24         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
25          * actions are not tagged with their source line numbers
26          */
27         if (position == NULL)
28                 return;
29
30         if ( func_inst_map.contains(position) ) {
31                 FuncInst * inst = func_inst_map.get(position);
32
33                 if (inst->get_type() != act->get_type() ) {
34                         // model_print("action with a different type occurs at line number %s\n", position);
35                         FuncInst * func_inst = inst->search_in_collision(act);
36
37                         if (func_inst != NULL)
38                                 return;
39
40                         func_inst = new FuncInst(act, this);
41                         inst->get_collisions()->push_back(func_inst);
42                         inst_list.push_back(func_inst); // delete?
43                 }
44
45                 return;
46         }
47
48         FuncInst * func_inst = new FuncInst(act, this);
49
50         func_inst_map.put(position, func_inst);
51         inst_list.push_back(func_inst);
52 }
53
54 /* Get the FuncInst with the same type, position, and location
55  * as act
56  *
57  * @return FuncInst with the same type, position, and location as act */
58 FuncInst * FuncNode::get_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 NULL;
68
69         FuncInst * inst = func_inst_map.get(position);
70         if (inst == NULL)
71                 return NULL;
72
73         action_type inst_type = inst->get_type();
74         action_type act_type = act->get_type();
75
76         // else if branch: an RMWRCAS action is converted to a RMW or READ action
77         if (inst_type == act_type)
78                 return inst;
79         else if (inst_type == ATOMIC_RMWRCAS &&
80                         (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
81                 return inst;
82
83         return NULL;
84 }
85
86
87 void FuncNode::add_entry_inst(FuncInst * inst)
88 {
89         if (inst == NULL)
90                 return;
91
92         mllnode<FuncInst *> * it;
93         for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
94                 if (inst == it->getVal())
95                         return;
96         }
97
98         entry_insts.push_back(inst);
99 }
100
101 /**
102  * @brief Convert ModelAdtion list to FuncInst list 
103  * @param act_list A list of ModelActions
104  */
105 void FuncNode::update_tree(action_list_t * act_list)
106 {
107         if (act_list == NULL)
108                 return;
109         else if (act_list->size() == 0)
110                 return;
111
112         /* build inst_list from act_list for later processing */
113         func_inst_list_t inst_list;
114         func_inst_list_t read_inst_list;
115         HashTable<FuncInst *, uint64_t, uintptr_t, 4> read_val_map;
116
117         for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
118                 ModelAction * act = it->getVal();
119                 FuncInst * func_inst = get_inst(act);
120
121                 if (func_inst == NULL)
122                         continue;
123
124                 inst_list.push_back(func_inst);
125
126 //              model_print("position: %s ", act->get_position());
127 //              act->print();
128
129                 if (func_inst->is_read()) {
130                         read_inst_list.push_back(func_inst);
131                         read_val_map.put(func_inst, act->get_reads_from_value());
132                 }
133         }
134
135         update_inst_tree(&inst_list);
136         init_predicate_tree(&read_inst_list, &read_val_map);
137 }
138
139 /** 
140  * @brief Link FuncInsts in inst_list  - add one FuncInst to another's predecessors and successors
141  * @param inst_list A list of FuncInsts
142  */
143 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
144 {
145         if (inst_list == NULL)
146                 return;
147         else if (inst_list->size() == 0)
148                 return;
149
150         /* start linking */
151         sllnode<FuncInst *>* it = inst_list->begin();
152         sllnode<FuncInst *>* prev;
153
154         /* add the first instruction to the list of entry insts */
155         FuncInst * entry_inst = it->getVal();
156         add_entry_inst(entry_inst);
157
158         it = it->getNext();
159         while (it != NULL) {
160                 prev = it->getPrev();
161
162                 FuncInst * prev_inst = prev->getVal();
163                 FuncInst * curr_inst = it->getVal();
164
165                 prev_inst->add_succ(curr_inst);
166                 curr_inst->add_pred(prev_inst);
167
168                 it = it->getNext();
169         }
170 }
171
172 /* @param tid thread id
173  * Store the values read by atomic read actions into thrd_read_map */
174 void FuncNode::store_read(ModelAction * act, uint32_t tid)
175 {
176         ASSERT(act);
177
178         void * location = act->get_location();
179         uint64_t read_from_val = act->get_reads_from_value();
180
181         /* resize and initialize */
182         uint32_t old_size = thrd_read_map.size();
183         if (old_size <= tid) {
184                 thrd_read_map.resize(tid + 1);
185                 for (uint32_t i = old_size; i < tid + 1;i++)
186                         thrd_read_map[i] = new read_map_t();
187         }
188
189         read_map_t * read_map = thrd_read_map[tid];
190         read_map->put(location, read_from_val);
191
192         /* Store the memory locations where atomic reads happen */
193         // read_locations.add(location);
194 }
195
196 uint64_t FuncNode::query_last_read(void * location, uint32_t tid)
197 {
198         if (thrd_read_map.size() <= tid)
199                 return 0xdeadbeef;
200
201         read_map_t * read_map = thrd_read_map[tid];
202
203         /* last read value not found */
204         if ( !read_map->contains(location) )
205                 return 0xdeadbeef;
206
207         uint64_t read_val = read_map->get(location);
208         return read_val;
209 }
210
211 /* @param tid thread id
212  * Reset read map for a thread. This function shall only be called
213  * when a thread exits a function
214  */
215 void FuncNode::clear_read_map(uint32_t tid)
216 {
217         if (thrd_read_map.size() <= tid)
218                 return;
219
220         thrd_read_map[tid]->reset();
221 }
222
223 void FuncNode::init_predicate_tree(func_inst_list_t * inst_list, HashTable<FuncInst *, uint64_t, uintptr_t, 4> * read_val_map)
224 {
225         if (inst_list == NULL || inst_list->size() == 0)
226                 return;
227
228         if (predicate_tree_initialized) {
229                 return;
230         }
231         predicate_tree_initialized = true;
232
233         // maybe restrict the size of hashtable to save calloc time
234         HashTable<void *, FuncInst *, uintptr_t, 4> loc_inst_map(64);
235
236         sllnode<FuncInst *> *it = inst_list->begin();
237         FuncInst * entry_inst = it->getVal();
238
239         /* get the unique Predicate pointer, assuming entry instructions have no predicate expression */
240         Predicate * curr_pred = NULL;
241         PredSetIter * pit = predicate_tree_entry.iterator();
242         while (pit->hasNext()) {
243                 Predicate * p = pit->next();
244                 p->get_func_inst()->print();
245                 if (p->get_func_inst() == entry_inst) {
246                         curr_pred = p;
247                         break;
248                 }
249         }
250         if (curr_pred == NULL) {
251                 curr_pred = new Predicate(entry_inst);
252                 predicate_tree_entry.add(curr_pred);
253         }
254
255         loc_inst_map.put(entry_inst->get_location(), entry_inst);
256
257         it = it->getNext();
258         while (it != NULL) {
259                 FuncInst * curr_inst = it->getVal();
260                 bool branch_found = false;
261
262                 /* check if a branch with the same func_inst and corresponding predicate exists */
263                 ModelVector<Predicate *> * branches = curr_pred->get_children();
264                 for (uint i = 0; i < branches->size(); i++) {
265                         Predicate * branch = (*branches)[i];
266                         if (branch->get_func_inst() != curr_inst)
267                                 continue;
268
269                         PredExprSet * pred_expressions = branch->get_pred_expressions();
270
271                         /* no predicate, follow the only branch */
272                         if (pred_expressions->getSize() == 0) {
273                                 model_print("no predicate exists: "); curr_inst->print();
274                                 curr_pred = branch;
275                                 branch_found = true;
276                                 break;
277                         }
278
279                         PredExprSetIter * pred_expr_it = pred_expressions->iterator();
280                         while (pred_expr_it->hasNext()) {
281                                 pred_expr * pred_expression = pred_expr_it->next();
282                                 uint64_t last_read, curr_read;
283                                 FuncInst * last_inst;
284                                 bool equality;
285
286                                 switch(pred_expression->token) {
287                                         case EQUALITY:
288                                                 last_inst = loc_inst_map.get(curr_inst->get_location());
289                                                 last_read = read_val_map->get(last_inst);
290                                                 curr_read = read_val_map->get(curr_inst);
291                                                 equality = (last_read == curr_read);
292                                                 if (equality == pred_expression->value) {
293                                                         curr_pred = branch;
294                                                         model_print("predicate: token: %d, location: %p, value: %d - ", pred_expression->token, pred_expression->location, pred_expression->value); curr_inst->print();
295                                                         branch_found = true;
296                                                 }
297                                                 break;
298                                         case NULLITY:
299                                                 break;
300                                         default:
301                                                 model_print("unkown predicate token\n");
302                                                 break;
303                                 }
304                         }
305
306                 }
307
308                 if (!branch_found) {
309                         if ( loc_inst_map.contains(curr_inst->get_location()) ) {
310                                 Predicate * new_pred1 = new Predicate(curr_inst);
311                                 new_pred1->add_predicate(EQUALITY, curr_inst->get_location(), true);
312
313                                 Predicate * new_pred2 = new Predicate(curr_inst);
314                                 new_pred2->add_predicate(EQUALITY, curr_inst->get_location(), false);
315
316                                 curr_pred->add_child(new_pred1);
317                                 curr_pred->add_child(new_pred2);
318
319                                 FuncInst * last_inst = loc_inst_map.get(curr_inst->get_location());
320                                 uint64_t last_read = read_val_map->get(last_inst);
321                                 if ( last_read == read_val_map->get(curr_inst) )
322                                         curr_pred = new_pred1;
323                                 else
324                                         curr_pred = new_pred2;
325                         } else {
326                                 Predicate * new_pred = new Predicate(curr_inst);
327                                 curr_pred->add_child(new_pred);
328                                 curr_pred = new_pred;
329                         }
330                 }
331
332                 loc_inst_map.put(curr_inst->get_location(), curr_inst);
333
334                 it = it->getNext();
335         }
336
337         model_print("function %s\n", func_name);
338         print_predicate_tree();
339 }
340
341
342 void FuncNode::print_predicate_tree()
343 {
344         model_print("digraph function_%s {\n", func_name);
345         PredSetIter * it = predicate_tree_entry.iterator();
346
347         while (it->hasNext()) {
348                 Predicate * p = it->next();
349                 p->print_pred_subtree();
350         }
351         model_print("}\n");     // end of graph
352 }
353
354 /* @param tid thread id
355  * Print the values read by the last read actions for each memory location
356  */
357 /*
358 void FuncNode::print_last_read(uint32_t tid)
359 {
360         ASSERT(thrd_read_map.size() > tid);
361         read_map_t * read_map = thrd_read_map[tid];
362
363         mllnode<void *> * it;
364         for (it = read_locations.begin();it != NULL;it=it->getNext()) {
365                 if ( !read_map->contains(it->getVal()) )
366                         break;
367
368                 uint64_t read_val = read_map->get(it->getVal());
369                 model_print("last read of thread %d at %p: 0x%x\n", tid, it->getVal(), read_val);
370         }
371 }
372 */