promise: add is_compatible_exclusive()
[model-checker.git] / cyclegraph.cc
1 #include "cyclegraph.h"
2 #include "action.h"
3 #include "common.h"
4 #include "promise.h"
5 #include "model.h"
6
7 /** Initializes a CycleGraph object. */
8 CycleGraph::CycleGraph() :
9         discovered(new HashTable<const CycleNode *, const CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free>(16)),
10         hasCycles(false),
11         oldCycles(false)
12 {
13 }
14
15 /** CycleGraph destructor */
16 CycleGraph::~CycleGraph()
17 {
18         delete discovered;
19 }
20
21 /**
22  * Add a CycleNode to the graph, corresponding to a store ModelAction
23  * @param act The write action that should be added
24  * @param node The CycleNode that corresponds to the store
25  */
26 void CycleGraph::putNode(const ModelAction *act, CycleNode *node)
27 {
28         actionToNode.put(act, node);
29 #if SUPPORT_MOD_ORDER_DUMP
30         nodeList.push_back(node);
31 #endif
32 }
33
34 /** @return The corresponding CycleNode, if exists; otherwise NULL */
35 CycleNode * CycleGraph::getNode_noCreate(const ModelAction *act) const
36 {
37         return actionToNode.get(act);
38 }
39
40 /** @return The corresponding CycleNode, if exists; otherwise NULL */
41 CycleNode * CycleGraph::getNode_noCreate(const Promise *promise) const
42 {
43         return readerToPromiseNode.get(promise->get_action());
44 }
45
46 /**
47  * @brief Returns the CycleNode corresponding to a given ModelAction
48  *
49  * Gets (or creates, if none exist) a CycleNode corresponding to a ModelAction
50  *
51  * @param action The ModelAction to find a node for
52  * @return The CycleNode paired with this action
53  */
54 CycleNode * CycleGraph::getNode(const ModelAction *action)
55 {
56         CycleNode *node = getNode_noCreate(action);
57         if (node == NULL) {
58                 node = new CycleNode(action);
59                 putNode(action, node);
60         }
61         return node;
62 }
63
64 /**
65  * @brief Returns a CycleNode corresponding to a promise
66  *
67  * Gets (or creates, if none exist) a CycleNode corresponding to a promised
68  * value.
69  *
70  * @param promise The Promise generated by a reader
71  * @return The CycleNode corresponding to the Promise
72  */
73 CycleNode * CycleGraph::getNode(const Promise *promise)
74 {
75         const ModelAction *reader = promise->get_action();
76         CycleNode *node = getNode_noCreate(promise);
77         if (node == NULL) {
78                 node = new CycleNode(promise);
79                 readerToPromiseNode.put(reader, node);
80         }
81         return node;
82 }
83
84 /**
85  * @return false if the resolution results in a cycle; true otherwise
86  */
87 bool CycleGraph::resolvePromise(ModelAction *reader, ModelAction *writer,
88                 promise_list_t *mustResolve)
89 {
90         CycleNode *promise_node = readerToPromiseNode.get(reader);
91         CycleNode *w_node = actionToNode.get(writer);
92         ASSERT(promise_node);
93
94         if (w_node)
95                 return mergeNodes(w_node, promise_node, mustResolve);
96         /* No existing write-node; just convert the promise-node */
97         promise_node->resolvePromise(writer);
98         readerToPromiseNode.put(reader, NULL); /* erase promise_node */
99         putNode(writer, promise_node);
100         return true;
101 }
102
103 /**
104  * @brief Merge two CycleNodes that represent the same write
105  *
106  * Note that this operation cannot be rolled back.
107  *
108  * @param w_node The write ModelAction node with which to merge
109  * @param p_node The Promise node to merge. Will be destroyed after this
110  * function.
111  * @param mustMerge Return (pass-by-reference) any additional Promises that
112  * must also be merged with w_node
113  *
114  * @return false if the merge results in a cycle; true otherwise
115  */
116 bool CycleGraph::mergeNodes(CycleNode *w_node, CycleNode *p_node,
117                 promise_list_t *mustMerge)
118 {
119         ASSERT(!w_node->is_promise());
120         ASSERT(p_node->is_promise());
121         const Promise *promise = p_node->getPromise();
122         if (!promise->is_compatible(w_node->getAction())) {
123                 hasCycles = true;
124                 return false;
125         }
126
127         /* Transfer back edges to w_node */
128         while (p_node->getNumBackEdges() > 0) {
129                 CycleNode *back = p_node->removeBackEdge();
130                 if (back != w_node) {
131                         if (back->is_promise()) {
132                                 if (checkReachable(w_node, back)) {
133                                         /* Edge would create cycle; merge instead */
134                                         mustMerge->push_back(back->getPromise());
135                                         if (!mergeNodes(w_node, back, mustMerge))
136                                                 return false;
137                                 } else
138                                         back->addEdge(w_node);
139                         } else
140                                 addNodeEdge(back, w_node);
141                 }
142         }
143
144         /* Transfer forward edges to w_node */
145         while (p_node->getNumEdges() > 0) {
146                 CycleNode *forward = p_node->removeEdge();
147                 if (forward != w_node) {
148                         if (forward->is_promise()) {
149                                 if (checkReachable(forward, w_node)) {
150                                         mustMerge->push_back(forward->getPromise());
151                                         if (!mergeNodes(w_node, forward, mustMerge))
152                                                 return false;
153                                 } else
154                                         w_node->addEdge(forward);
155                         } else
156                                 addNodeEdge(w_node, forward);
157                 }
158         }
159
160         /* erase p_node */
161         readerToPromiseNode.put(promise->get_action(), NULL);
162         delete p_node;
163
164         return !hasCycles;
165 }
166
167 /**
168  * Adds an edge between two CycleNodes.
169  * @param fromnode The edge comes from this CycleNode
170  * @param tonode The edge points to this CycleNode
171  * @return True, if new edge(s) are added; otherwise false
172  */
173 bool CycleGraph::addNodeEdge(CycleNode *fromnode, CycleNode *tonode)
174 {
175         bool added;
176
177         if (!hasCycles)
178                 hasCycles = checkReachable(tonode, fromnode);
179
180         if ((added = fromnode->addEdge(tonode)))
181                 rollbackvector.push_back(fromnode);
182
183         /*
184          * If the fromnode has a rmwnode that is not the tonode, we should add
185          * an edge between its rmwnode and the tonode
186          */
187         CycleNode *rmwnode = fromnode->getRMW();
188         if (rmwnode && rmwnode != tonode) {
189                 if (!hasCycles)
190                         hasCycles = checkReachable(tonode, rmwnode);
191
192                 if (rmwnode->addEdge(tonode)) {
193                         rollbackvector.push_back(rmwnode);
194                         added = true;
195                 }
196         }
197         return added;
198 }
199
200 /**
201  * @brief Add an edge between a write and the RMW which reads from it
202  *
203  * Handles special case of a RMW action, where the ModelAction rmw reads from
204  * the ModelAction from. The key differences are:
205  * (1) no write can occur in between the rmw and the from action.
206  * (2) Only one RMW action can read from a given write.
207  *
208  * @param from The edge comes from this ModelAction
209  * @param rmw The edge points to this ModelAction; this action must read from
210  * ModelAction from
211  */
212 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw)
213 {
214         ASSERT(from);
215         ASSERT(rmw);
216
217         CycleNode *fromnode = getNode(from);
218         CycleNode *rmwnode = getNode(rmw);
219
220         /* Two RMW actions cannot read from the same write. */
221         if (fromnode->setRMW(rmwnode))
222                 hasCycles = true;
223         else
224                 rmwrollbackvector.push_back(fromnode);
225
226         /* Transfer all outgoing edges from the from node to the rmw node */
227         /* This process should not add a cycle because either:
228          * (1) The rmw should not have any incoming edges yet if it is the
229          * new node or
230          * (2) the fromnode is the new node and therefore it should not
231          * have any outgoing edges.
232          */
233         for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
234                 CycleNode *tonode = fromnode->getEdge(i);
235                 if (tonode != rmwnode) {
236                         if (rmwnode->addEdge(tonode))
237                                 rollbackvector.push_back(rmwnode);
238                 }
239         }
240
241         addNodeEdge(fromnode, rmwnode);
242 }
243
244 #if SUPPORT_MOD_ORDER_DUMP
245 void CycleGraph::dumpNodes(FILE *file) const
246 {
247         for (unsigned int i = 0; i < nodeList.size(); i++) {
248                 CycleNode *cn = nodeList[i];
249                 const ModelAction *action = cn->getAction();
250                 fprintf(file, "N%u [label=\"%u, T%u\"];\n", action->get_seq_number(), action->get_seq_number(), action->get_tid());
251                 if (cn->getRMW() != NULL) {
252                         fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
253                 }
254                 for (unsigned int j = 0; j < cn->getNumEdges(); j++) {
255                         CycleNode *dst = cn->getEdge(j);
256                         const ModelAction *dstaction = dst->getAction();
257                         fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
258                 }
259         }
260 }
261
262 void CycleGraph::dumpGraphToFile(const char *filename) const
263 {
264         char buffer[200];
265         sprintf(buffer, "%s.dot", filename);
266         FILE *file = fopen(buffer, "w");
267         fprintf(file, "digraph %s {\n", filename);
268         dumpNodes(file);
269         fprintf(file, "}\n");
270         fclose(file);
271 }
272 #endif
273
274 /**
275  * Checks whether one CycleNode can reach another.
276  * @param from The CycleNode from which to begin exploration
277  * @param to The CycleNode to reach
278  * @return True, @a from can reach @a to; otherwise, false
279  */
280 bool CycleGraph::checkReachable(const CycleNode *from, const CycleNode *to) const
281 {
282         std::vector< const CycleNode *, ModelAlloc<const CycleNode *> > queue;
283         discovered->reset();
284
285         queue.push_back(from);
286         discovered->put(from, from);
287         while (!queue.empty()) {
288                 const CycleNode *node = queue.back();
289                 queue.pop_back();
290                 if (node == to)
291                         return true;
292
293                 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
294                         CycleNode *next = node->getEdge(i);
295                         if (!discovered->contains(next)) {
296                                 discovered->put(next, next);
297                                 queue.push_back(next);
298                         }
299                 }
300         }
301         return false;
302 }
303
304 /** @return True, if the promise has failed; false otherwise */
305 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
306 {
307         std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
308         discovered->reset();
309         CycleNode *from = actionToNode.get(fromact);
310
311         queue.push_back(from);
312         discovered->put(from, from);
313         while (!queue.empty()) {
314                 CycleNode *node = queue.back();
315                 queue.pop_back();
316
317                 if (!node->is_promise() &&
318                                 promise->eliminate_thread(node->getAction()->get_tid()))
319                         return true;
320
321                 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
322                         CycleNode *next = node->getEdge(i);
323                         if (!discovered->contains(next)) {
324                                 discovered->put(next, next);
325                                 queue.push_back(next);
326                         }
327                 }
328         }
329         return false;
330 }
331
332 void CycleGraph::startChanges()
333 {
334         ASSERT(rollbackvector.empty());
335         ASSERT(rmwrollbackvector.empty());
336         ASSERT(oldCycles == hasCycles);
337 }
338
339 /** Commit changes to the cyclegraph. */
340 void CycleGraph::commitChanges()
341 {
342         rollbackvector.clear();
343         rmwrollbackvector.clear();
344         oldCycles = hasCycles;
345 }
346
347 /** Rollback changes to the previous commit. */
348 void CycleGraph::rollbackChanges()
349 {
350         for (unsigned int i = 0; i < rollbackvector.size(); i++)
351                 rollbackvector[i]->popEdge();
352
353         for (unsigned int i = 0; i < rmwrollbackvector.size(); i++)
354                 rmwrollbackvector[i]->clearRMW();
355
356         hasCycles = oldCycles;
357         rollbackvector.clear();
358         rmwrollbackvector.clear();
359 }
360
361 /** @returns whether a CycleGraph contains cycles. */
362 bool CycleGraph::checkForCycles() const
363 {
364         return hasCycles;
365 }
366
367 /**
368  * @brief Constructor for a CycleNode
369  * @param act The ModelAction for this node
370  */
371 CycleNode::CycleNode(const ModelAction *act) :
372         action(act),
373         promise(NULL),
374         hasRMW(NULL)
375 {
376 }
377
378 /**
379  * @brief Constructor for a Promise CycleNode
380  * @param promise The Promise which was generated
381  */
382 CycleNode::CycleNode(const Promise *promise) :
383         action(NULL),
384         promise(promise),
385         hasRMW(NULL)
386 {
387 }
388
389 /**
390  * @param i The index of the edge to return
391  * @returns The a CycleNode edge indexed by i
392  */
393 CycleNode * CycleNode::getEdge(unsigned int i) const
394 {
395         return edges[i];
396 }
397
398 /** @returns The number of edges leaving this CycleNode */
399 unsigned int CycleNode::getNumEdges() const
400 {
401         return edges.size();
402 }
403
404 CycleNode * CycleNode::getBackEdge(unsigned int i) const
405 {
406         return back_edges[i];
407 }
408
409 unsigned int CycleNode::getNumBackEdges() const
410 {
411         return back_edges.size();
412 }
413
414 /**
415  * @brief Remove an element from a vector
416  * @param v The vector
417  * @param n The element to remove
418  * @return True if the element was found; false otherwise
419  */
420 template <typename T>
421 static bool vector_remove_node(std::vector<T, SnapshotAlloc<T> >& v, const T n)
422 {
423         for (unsigned int i = 0; i < v.size(); i++) {
424                 if (v[i] == n) {
425                         v.erase(v.begin() + i);
426                         return true;
427                 }
428         }
429         return false;
430 }
431
432 /**
433  * @brief Remove a (forward) edge from this CycleNode
434  * @return The CycleNode which was popped, if one exists; otherwise NULL
435  */
436 CycleNode * CycleNode::removeEdge()
437 {
438         if (edges.empty())
439                 return NULL;
440
441         CycleNode *ret = edges.back();
442         edges.pop_back();
443         vector_remove_node(ret->back_edges, this);
444         return ret;
445 }
446
447 /**
448  * @brief Remove a (back) edge from this CycleNode
449  * @return The CycleNode which was popped, if one exists; otherwise NULL
450  */
451 CycleNode * CycleNode::removeBackEdge()
452 {
453         if (back_edges.empty())
454                 return NULL;
455
456         CycleNode *ret = back_edges.back();
457         back_edges.pop_back();
458         vector_remove_node(ret->edges, this);
459         return ret;
460 }
461
462 /**
463  * Adds an edge from this CycleNode to another CycleNode.
464  * @param node The node to which we add a directed edge
465  * @return True if this edge is a new edge; false otherwise
466  */
467 bool CycleNode::addEdge(CycleNode *node)
468 {
469         for (unsigned int i = 0; i < edges.size(); i++)
470                 if (edges[i] == node)
471                         return false;
472         edges.push_back(node);
473         node->back_edges.push_back(this);
474         return true;
475 }
476
477 /** @returns the RMW CycleNode that reads from the current CycleNode */
478 CycleNode * CycleNode::getRMW() const
479 {
480         return hasRMW;
481 }
482
483 /**
484  * Set a RMW action node that reads from the current CycleNode.
485  * @param node The RMW that reads from the current node
486  * @return True, if this node already was read by another RMW; false otherwise
487  * @see CycleGraph::addRMWEdge
488  */
489 bool CycleNode::setRMW(CycleNode *node)
490 {
491         if (hasRMW != NULL)
492                 return true;
493         hasRMW = node;
494         return false;
495 }
496
497 /**
498  * Convert a Promise CycleNode into a concrete-valued CycleNode. Should only be
499  * used when there's no existing ModelAction CycleNode for this write.
500  *
501  * @param writer The ModelAction which wrote the future value represented by
502  * this CycleNode
503  */
504 void CycleNode::resolvePromise(const ModelAction *writer)
505 {
506         ASSERT(is_promise());
507         ASSERT(promise->is_compatible(writer));
508         action = writer;
509         promise = NULL;
510         ASSERT(!is_promise());
511 }