Able to evaluate predicate expression against a 'context' and generate concrete predi...
[c11tester.git] / predicate.h
1 #ifndef __PREDICTAE_H__
2 #define __PREDICATE_H__
3
4 #include "funcinst.h"
5 #include "hashset.h"
6
7 unsigned int pred_expr_hash (struct pred_expr *);
8 bool pred_expr_equal(struct pred_expr *, struct pred_expr *);
9 typedef HashSet<struct pred_expr *, uintptr_t, 0, model_malloc, model_calloc, model_free, pred_expr_hash, pred_expr_equal> PredExprSet;
10 typedef HSIterator<struct pred_expr *, uintptr_t, 0, model_malloc, model_calloc, model_free, pred_expr_hash, pred_expr_equal> PredExprSetIter;
11
12 typedef enum predicate_token {
13         NOPREDICATE, EQUALITY, NULLITY
14 } token_t;
15
16 /* If token is EQUALITY, then the predicate asserts whether
17  * this load should read the same value as the last value
18  * read at memory location specified in predicate_expr.
19  */
20 struct pred_expr {
21         pred_expr(token_t token, FuncInst * inst, bool value) :
22                 token(token),
23                 func_inst(inst),
24                 value(value)
25         {}
26
27         token_t token;
28         FuncInst * func_inst;
29         bool value;
30
31         MEMALLOC
32 };
33
34 /* Used by predicate generator */
35 struct half_pred_expr {
36         half_pred_expr(token_t token, FuncInst * inst) :
37                 token(token),
38                 func_inst(inst)
39         {}
40
41         token_t token;
42         FuncInst * func_inst;
43
44         SNAPSHOTALLOC
45 };
46
47 struct concrete_pred_expr {
48         concrete_pred_expr(token_t token, uint64_t value, bool equality) :
49                 token(token),
50                 value(value),
51                 equality(equality)
52         {}
53
54         token_t token;
55         uint64_t value;
56         bool equality;
57
58         SNAPSHOTALLOC
59 };
60
61 class Predicate {
62 public:
63         Predicate(FuncInst * func_inst, bool is_entry = false);
64         ~Predicate();
65
66         FuncInst * get_func_inst() { return func_inst; }
67         PredExprSet * get_pred_expressions() { return &pred_expressions; }
68
69         void add_predicate_expr(token_t token, FuncInst * func_inst, bool value);
70         void add_child(Predicate * child);
71         void set_parent(Predicate * parent_pred) { parent = parent_pred; }
72         void add_backedge(Predicate * back_pred) { backedges.add(back_pred); }
73         void copy_predicate_expr(Predicate * other);
74
75         ModelVector<Predicate *> * get_children() { return &children; }
76         Predicate * get_parent() { return parent; }
77         PredSet * get_backedges() { return &backedges; }
78
79         bool is_entry_predicate() { return entry_predicate; }
80         void set_entry_predicate() { entry_predicate = true; }
81
82         /* Whether func_inst does write or not */
83         bool is_write() { return does_write; }
84         void set_write(bool is_write) { does_write = is_write; }
85
86         SnapVector<struct concrete_pred_expr> evaluate(inst_act_map_t * inst_act_map);
87
88         void print_predicate();
89         void print_pred_subtree();
90
91         MEMALLOC
92 private:
93         FuncInst * func_inst;
94         bool entry_predicate;
95         bool does_write;
96
97         /* May have multiple predicate expressions */
98         PredExprSet pred_expressions;
99         ModelVector<Predicate *> children;
100
101         /* Only a single parent may exist */
102         Predicate * parent;
103
104         /* May have multiple back edges, e.g. nested loops */
105         PredSet backedges;
106 };
107
108 #endif /* __PREDICATE_H__ */