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