Leave symbol pseudo usage intact when doing phi-node conversion.
[smatch.git] / linearize.h
blob5c9a414ecef10201c7d63ff77e60634f52557006
1 #ifndef LINEARIZE_H
2 #define LINEARIZE_H
4 #include "lib.h"
5 #include "token.h"
6 #include "parse.h"
7 #include "symbol.h"
9 struct instruction;
10 DECLARE_PTR_LIST(pseudo_ptr_list, pseudo_t);
12 enum pseudo_type {
13 PSEUDO_VOID,
14 PSEUDO_REG,
15 PSEUDO_SYM,
16 PSEUDO_VAL,
17 PSEUDO_ARG,
18 PSEUDO_PHI,
21 struct pseudo {
22 int nr;
23 enum pseudo_type type;
24 struct pseudo_ptr_list *users;
25 struct instruction_list *insns;
26 union {
27 struct symbol *sym;
28 struct instruction *def;
29 long long value;
33 extern struct pseudo void_pseudo;
35 #define VOID (&void_pseudo)
37 struct multijmp {
38 struct basic_block *target;
39 int begin, end;
42 struct instruction {
43 struct symbol *type;
44 struct basic_block *bb;
45 int opcode;
46 union {
47 pseudo_t target;
48 pseudo_t cond; /* for branch and switch */
50 union {
51 struct /* branch */ {
52 struct basic_block *bb_true, *bb_false;
54 struct /* switch */ {
55 struct multijmp_list *multijmp_list;
57 struct /* phi_node */ {
58 struct pseudo_list *phi_list;
60 struct /* unops */ {
61 pseudo_t src;
62 struct symbol *orig_type; /* casts */
63 unsigned int offset; /* memops */
65 struct /* binops */ {
66 pseudo_t src1, src2;
68 struct /* slice */ {
69 pseudo_t base;
70 unsigned from, len;
72 struct /* multijump */ {
73 int begin, end;
75 struct /* setval */ {
76 pseudo_t symbol; /* Subtle: same offset as "src" !! */
77 struct expression *val;
79 struct /* call */ {
80 pseudo_t func;
81 struct pseudo_list *arguments;
83 struct /* context */ {
84 int increment;
89 enum opcode {
90 OP_BADOP,
91 /* Terminator */
92 OP_TERMINATOR,
93 OP_RET = OP_TERMINATOR,
94 OP_BR,
95 OP_SWITCH,
96 OP_INVOKE,
97 OP_COMPUTEDGOTO,
98 OP_UNWIND,
99 OP_TERMINATOR_END = OP_UNWIND,
101 /* Binary */
102 OP_BINARY,
103 OP_ADD = OP_BINARY,
104 OP_SUB,
105 OP_MUL,
106 OP_DIV,
107 OP_MOD,
108 OP_SHL,
109 OP_SHR,
111 /* Logical */
112 OP_AND,
113 OP_OR,
114 OP_XOR,
115 OP_AND_BOOL,
116 OP_OR_BOOL,
117 OP_BINARY_END = OP_OR_BOOL,
119 /* Binary comparison */
120 OP_BINCMP,
121 OP_SET_EQ = OP_BINCMP,
122 OP_SET_NE,
123 OP_SET_LE,
124 OP_SET_GE,
125 OP_SET_LT,
126 OP_SET_GT,
127 OP_SET_B,
128 OP_SET_A,
129 OP_SET_BE,
130 OP_SET_AE,
131 OP_BINCMP_END = OP_SET_AE,
133 /* Uni */
134 OP_NOT,
135 OP_NEG,
137 /* Setcc - always in combination with a select or conditional branch */
138 OP_SETCC,
139 OP_SEL,
141 /* Memory */
142 OP_MALLOC,
143 OP_FREE,
144 OP_ALLOCA,
145 OP_LOAD,
146 OP_STORE,
147 OP_SETVAL,
148 OP_GET_ELEMENT_PTR,
150 /* Other */
151 OP_PHI,
152 OP_PHISOURCE,
153 OP_CAST,
154 OP_CALL,
155 OP_VANEXT,
156 OP_VAARG,
157 OP_SLICE,
158 OP_SNOP,
159 OP_LNOP,
160 OP_NOP,
162 /* Sparse tagging (line numbers, context, whatever) */
163 OP_CONTEXT,
166 struct basic_block_list;
167 struct instruction_list;
169 struct basic_block {
170 struct position pos;
171 unsigned long generation;
172 int context;
173 struct basic_block_list *parents; /* sources */
174 struct basic_block_list *children; /* destinations */
175 struct instruction_list *insns; /* Linear list of instructions */
178 static inline int is_branch_goto(struct instruction *br)
180 return br && br->opcode==OP_BR && (!br->bb_true || !br->bb_false);
183 static inline void add_bb(struct basic_block_list **list, struct basic_block *bb)
185 add_ptr_list(list, bb);
188 static inline void add_instruction(struct instruction_list **list, struct instruction *insn)
190 add_ptr_list(list, insn);
193 static inline void add_multijmp(struct multijmp_list **list, struct multijmp *multijmp)
195 add_ptr_list(list, multijmp);
198 static inline void *add_pseudo(struct pseudo_list **list, struct pseudo *pseudo)
200 return add_ptr_list(list, pseudo);
204 static inline int bb_terminated(struct basic_block *bb)
206 struct instruction *insn;
207 if (!bb)
208 return 0;
209 insn = last_instruction(bb->insns);
210 return insn && insn->opcode >= OP_RET && insn->opcode <= OP_UNWIND;
213 static inline int bb_reachable(struct basic_block *bb)
215 return bb != NULL;
218 static inline void add_pseudo_ptr(pseudo_t *ptr, struct pseudo_ptr_list **list)
220 add_ptr_list(list, ptr);
223 static inline int has_use_list(pseudo_t p)
225 return (p && p->type != PSEUDO_VOID && p->type != PSEUDO_VAL);
228 static inline void use_pseudo(pseudo_t p, pseudo_t *pp)
230 *pp = p;
231 if (has_use_list(p))
232 add_pseudo_ptr(pp, &p->users);
235 static inline int remove_bb_from_list(struct basic_block_list **list, struct basic_block *entry)
237 return delete_ptr_list_entry((struct ptr_list **)list, entry);
240 static inline int replace_bb_in_list(struct basic_block_list **list,
241 struct basic_block *old, struct basic_block *new)
243 return replace_ptr_list_entry((struct ptr_list **)list, old, new);
246 struct entrypoint {
247 struct symbol *name;
248 struct symbol_list *syms;
249 struct symbol_list *accesses;
250 struct basic_block_list *bbs;
251 struct basic_block *active;
252 struct basic_block *entry;
253 struct pseudo_list *pseudos;
256 extern void insert_select(struct basic_block *bb, struct instruction *br, struct instruction *phi, pseudo_t true, pseudo_t false);
257 extern void insert_branch(struct basic_block *bb, struct instruction *br, struct basic_block *target);
259 pseudo_t alloc_phi(struct basic_block *source, pseudo_t pseudo);
260 pseudo_t alloc_pseudo(struct instruction *def);
261 pseudo_t value_pseudo(long long val);
263 struct entrypoint *linearize_symbol(struct symbol *sym);
264 void show_entry(struct entrypoint *ep);
266 #endif /* LINEARIZE_H */