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  void clear_constraint();
115 
116  // Get the cond_node given that the constraint for the region node is
117  // "cond_node==cond"
118  SEGNodeBase *get_cond_node() const { return cond_node; }
119 
120  // Set the debug str
121  void set_dbg_str(std::string str);
122 
123  // get the debug str
124  std::string get_dbg_str() const;
125 
126  // Return if the region constraint is satisfiable
127  bool is_satisfiable() const;
128 
129  // Return if the region constraint is always satisfied
130  bool is_always_satisfied() const;
131 
132  // Return if the region is a compound region derived from other regions
133  bool is_compound() const;
134 
135  // Check if the region is an interface region
136  // If the region is an interface region, the cond_node(condition) is from
137  // another function, which may require additional treatment
138  bool is_interface_region();
139 
140  // Return true if the regions (1st argument) and the singled region (2nd
141  // argument) have contradict constraints
142  static bool has_contradictory(kvec<SEGRegionNode *> &regions,
143  SEGRegionNode *additional_region = nullptr);
144 
145  // Dump the clauses for debugging
146  void dump_clause();
147 
148 public:
149  static bool classof(const SEGObject *O) {
150  return O->getKind() == SEGOBJK_Region;
151  }
152 };
153 
154 #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