ClearBlue
SEGRegionNode.h
1 /*
2  * SEGRegionNode.h
3  *
4  * Andy
5  *
6  * This node is to model a region.
7  */
8 
9 #ifndef IR_SEG_SEGREGIONNODE_H
10 #define IR_SEG_SEGREGIONNODE_H
11 
12 #include <llvm/IR/BasicBlock.h>
13 #include <llvm/IR/Function.h>
14 #include <llvm/IR/Instructions.h>
15 #include <llvm/IR/Value.h>
16 
17 #include <unordered_set>
18 
19 #include "IR/SEG/SEGMapBase.h"
20 #include "IR/SEG/SymbolicExprGraph.h"
21 
22 using namespace llvm;
23 
32 //. This means that the region node is an iter-face region node, which uses a
33 // condition from other functions
34 class SEGRegionNode : public SEGOperandNode {
35  class Clause {
36  private:
37  std::unordered_set<SEGNodeBase *> positive_items;
38  std::unordered_set<SEGNodeBase *> negative_items;
39 
40  public:
41  // return true if the clause is satisfiable after the change
42  // return false if the clause is infeasible after the change
43  bool add_positive(SEGNodeBase *);
44  bool add_negative(SEGNodeBase *);
45 
46  // insert the constraints c into this clause
47  bool and_clause(Clause *c);
48 
49  // result = c1 && c2
50  // return true if the result is satisfiable
51  // return false if the result is not satisfiable
52  static bool and_clause(Clause *c1, Clause *c2, Clause *result);
53 
54  friend class SEGRegionNode;
55  friend class SEGSerializer;
56  friend class SEGHash;
57  };
58 
59 private:
60  // The region is valid when value_of(cond_node) == cond is satisfied
61  SEGNodeBase *cond_node;
62  bool cond;
63 
64  bool is_always_true;
65  bool is_always_false;
66 
67  // Cache the constraints for quick elimination of invalid/unsatisfiable
68  // regions
69  std::unordered_set<Clause *> constraints;
70  Clause constraint;
71  // SMTExpr constraint_expr;
72 
73  std::string dbg_str;
74 
75  // static SMTFactory sfty;
76 
77  // Set the composed constraint of a region node for fast elimination of
78  // infeasible regions "is_and = true" means region1 and region2 otherwise, it
79  // means region1 or region2 We compare three types of implementations
80  void set_constraint_complex(SEGRegionNode *region1, SEGRegionNode *region2,
81  bool is_and);
82  void set_constraint_simple(SEGRegionNode *region1, SEGRegionNode *region2,
83  bool is_and);
84  void set_constraint_sat(SEGRegionNode *region1, SEGRegionNode *region2,
85  bool is_and);
86  void set_constraint_baseline(SEGRegionNode *region1, SEGRegionNode *region2,
87  bool is_and);
88 
89  // Initialize constraint
90  void set_default_constraint();
91 
92  // seg : the parent SEG
93  // cond_node: the SEG node generating the control dependence constraint
94  // cond: the condition (true/false) of cond_node for this region to be valid
95  SEGRegionNode(SymbolicExprGraph *SEG, SEGNodeBase *CondNode = nullptr,
96  bool Cond = false, bool fromDisk = false);
97 
98  friend class SymbolicExprGraph;
99  friend class SEGSerializer;
100  friend class SEGHash;
101 
102 public:
103  ~SEGRegionNode();
104 
105  // Get the cond given that the constraint for the region node is
106  // "cond_node==cond"
107  bool get_cond() const { return cond; }
108 
109  // wrapper to set constraint
110  // see comments on "set_constraint_complex"
111  void set_constraint(SEGRegionNode *region1, SEGRegionNode *region2,
112  bool is_and);
113 
114  // Get the cond_node given that the constraint for the region node is
115  // "cond_node==cond"
116  SEGNodeBase *get_cond_node() const { return cond_node; }
117 
118  // Set the debug str
119  void set_dbg_str(std::string str);
120 
121  // get the debug str
122  std::string get_dbg_str() const;
123 
124  // Return if the region constraint is satisfiable
125  bool is_satisfiable() const;
126 
127  // Return if the region constraint is always satisfied
128  bool is_always_satisfied() const;
129 
130  // Return if the region is a compound region derived from other regions
131  bool is_compound() const;
132 
133  // Check if the region is an interface region
134  // If the region is an interface region, the cond_node(condition) is from
135  // another function, which may require additional treatment
136  bool is_interface_region();
137 
138  // Return true if the regions (1st argument) and the singled region (2nd
139  // argument) have contradict constraints
140  static bool has_contradictory(kvec<SEGRegionNode *> &regions,
141  SEGRegionNode *additional_region = nullptr);
142 
143  // Dump the clauses for debugging
144  void dump_clause();
145 
146 public:
147  static bool classof(const SEGObject *O) {
148  return O->getKind() == SEGOBJK_Region;
149  }
150 };
151 
152 #endif
SEGOperandNode
Definition: SymbolicExprGraph.h:456
SymbolicExprGraph
Definition: SymbolicExprGraph.h:708
SEGObject
Definition: SymbolicExprGraph.h:76
SEGRegionNode
Definition: SEGRegionNode.h:34
SEGNodeBase
The node base of symbolic expression graph.
Definition: SymbolicExprGraph.h:244