When replacing OP_SWITCH with OP_BR, make sure to remove
[smatch.git] / linearize.h
blob4fc7ce49a5d86fa9f5922641d2863c82701a696c
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 entrypoint *ep;
174 struct basic_block_list *parents; /* sources */
175 struct basic_block_list *children; /* destinations */
176 struct instruction_list *insns; /* Linear list of instructions */
179 static inline int is_branch_goto(struct instruction *br)
181 return br && br->opcode==OP_BR && (!br->bb_true || !br->bb_false);
184 static inline void add_bb(struct basic_block_list **list, struct basic_block *bb)
186 add_ptr_list(list, bb);
189 static inline void add_instruction(struct instruction_list **list, struct instruction *insn)
191 add_ptr_list(list, insn);
194 static inline void add_multijmp(struct multijmp_list **list, struct multijmp *multijmp)
196 add_ptr_list(list, multijmp);
199 static inline void *add_pseudo(struct pseudo_list **list, struct pseudo *pseudo)
201 return add_ptr_list(list, pseudo);
205 static inline int bb_terminated(struct basic_block *bb)
207 struct instruction *insn;
208 if (!bb)
209 return 0;
210 insn = last_instruction(bb->insns);
211 return insn && insn->opcode >= OP_RET && insn->opcode <= OP_UNWIND;
214 static inline int bb_reachable(struct basic_block *bb)
216 return bb != NULL;
219 static inline void add_pseudo_ptr(pseudo_t *ptr, struct pseudo_ptr_list **list)
221 add_ptr_list(list, ptr);
224 static inline int has_use_list(pseudo_t p)
226 return (p && p->type != PSEUDO_VOID && p->type != PSEUDO_VAL);
229 static inline void use_pseudo(pseudo_t p, pseudo_t *pp)
231 *pp = p;
232 if (has_use_list(p))
233 add_pseudo_ptr(pp, &p->users);
236 static inline void remove_bb_from_list(struct basic_block_list **list, struct basic_block *entry, int count)
238 delete_ptr_list_entry((struct ptr_list **)list, entry, count);
241 static inline void replace_bb_in_list(struct basic_block_list **list,
242 struct basic_block *old, struct basic_block *new, int count)
244 replace_ptr_list_entry((struct ptr_list **)list, old, new, count);
247 struct entrypoint {
248 struct symbol *name;
249 struct symbol_list *syms;
250 struct symbol_list *accesses;
251 struct basic_block_list *bbs;
252 struct basic_block *active;
253 struct basic_block *entry;
254 struct pseudo_list *pseudos;
257 extern void insert_select(struct basic_block *bb, struct instruction *br, struct instruction *phi, pseudo_t true, pseudo_t false);
258 extern void insert_branch(struct basic_block *bb, struct instruction *br, struct basic_block *target);
260 pseudo_t alloc_phi(struct basic_block *source, pseudo_t pseudo);
261 pseudo_t alloc_pseudo(struct instruction *def);
262 pseudo_t value_pseudo(long long val);
264 struct entrypoint *linearize_symbol(struct symbol *sym);
265 void show_entry(struct entrypoint *ep);
267 #endif /* LINEARIZE_H */