smatch_implied: only print the nr_children message once.
[smatch.git] / cse.c
blob2a1574531993c88eb73019f8a32fa22415c32114
1 /*
2 * CSE - walk the linearized instruction flow, and
3 * see if we can simplify it and apply CSE on it.
5 * Copyright (C) 2004 Linus Torvalds
6 */
8 #include <string.h>
9 #include <stdarg.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <stddef.h>
13 #include <assert.h>
15 #include "parse.h"
16 #include "expression.h"
17 #include "linearize.h"
18 #include "flow.h"
20 #define INSN_HASH_SIZE 256
21 static struct instruction_list *insn_hash_table[INSN_HASH_SIZE];
23 int repeat_phase;
25 static int phi_compare(pseudo_t phi1, pseudo_t phi2)
27 const struct instruction *def1 = phi1->def;
28 const struct instruction *def2 = phi2->def;
30 if (def1->src1 != def2->src1)
31 return def1->src1 < def2->src1 ? -1 : 1;
32 if (def1->bb != def2->bb)
33 return def1->bb < def2->bb ? -1 : 1;
34 return 0;
38 static void clean_up_one_instruction(struct basic_block *bb, struct instruction *insn)
40 unsigned long hash;
42 if (!insn->bb)
43 return;
44 assert(insn->bb == bb);
45 repeat_phase |= simplify_instruction(insn);
46 hash = (insn->opcode << 3) + (insn->size >> 3);
47 switch (insn->opcode) {
48 case OP_SEL:
49 hash += hashval(insn->src3);
50 /* Fall through */
52 /* Binary arithmetic */
53 case OP_ADD: case OP_SUB:
54 case OP_MULU: case OP_MULS:
55 case OP_DIVU: case OP_DIVS:
56 case OP_MODU: case OP_MODS:
57 case OP_SHL:
58 case OP_LSR: case OP_ASR:
59 case OP_AND: case OP_OR:
61 /* Binary logical */
62 case OP_XOR: case OP_AND_BOOL:
63 case OP_OR_BOOL:
65 /* Binary comparison */
66 case OP_SET_EQ: case OP_SET_NE:
67 case OP_SET_LE: case OP_SET_GE:
68 case OP_SET_LT: case OP_SET_GT:
69 case OP_SET_B: case OP_SET_A:
70 case OP_SET_BE: case OP_SET_AE:
71 hash += hashval(insn->src2);
72 /* Fall through */
74 /* Unary */
75 case OP_NOT: case OP_NEG:
76 hash += hashval(insn->src1);
77 break;
79 case OP_SETVAL:
80 hash += hashval(insn->val);
81 break;
83 case OP_SYMADDR:
84 hash += hashval(insn->symbol);
85 break;
87 case OP_CAST:
88 case OP_SCAST:
89 case OP_PTRCAST:
91 * This is crap! Many "orig_types" are the
92 * same as far as casts go, we should generate
93 * some kind of "type hash" that is identical
94 * for identical casts
96 hash += hashval(insn->orig_type);
97 hash += hashval(insn->src);
98 break;
100 /* Other */
101 case OP_PHI: {
102 pseudo_t phi;
103 FOR_EACH_PTR(insn->phi_list, phi) {
104 struct instruction *def;
105 if (phi == VOID || !phi->def)
106 continue;
107 def = phi->def;
108 hash += hashval(def->src1);
109 hash += hashval(def->bb);
110 } END_FOR_EACH_PTR(phi);
111 break;
114 default:
116 * Nothing to do, don't even bother hashing them,
117 * we're not going to try to CSE them
119 return;
121 hash += hash >> 16;
122 hash &= INSN_HASH_SIZE-1;
123 add_instruction(insn_hash_table + hash, insn);
126 static void clean_up_insns(struct entrypoint *ep)
128 struct basic_block *bb;
130 FOR_EACH_PTR(ep->bbs, bb) {
131 struct instruction *insn;
132 FOR_EACH_PTR(bb->insns, insn) {
133 clean_up_one_instruction(bb, insn);
134 } END_FOR_EACH_PTR(insn);
135 } END_FOR_EACH_PTR(bb);
138 /* Compare two (sorted) phi-lists */
139 static int phi_list_compare(struct pseudo_list *l1, struct pseudo_list *l2)
141 pseudo_t phi1, phi2;
143 PREPARE_PTR_LIST(l1, phi1);
144 PREPARE_PTR_LIST(l2, phi2);
145 for (;;) {
146 int cmp;
148 while (phi1 && (phi1 == VOID || !phi1->def))
149 NEXT_PTR_LIST(phi1);
150 while (phi2 && (phi2 == VOID || !phi2->def))
151 NEXT_PTR_LIST(phi2);
153 if (!phi1)
154 return phi2 ? -1 : 0;
155 if (!phi2)
156 return phi1 ? 1 : 0;
157 cmp = phi_compare(phi1, phi2);
158 if (cmp)
159 return cmp;
160 NEXT_PTR_LIST(phi1);
161 NEXT_PTR_LIST(phi2);
163 /* Not reached, but we need to make the nesting come out right */
164 FINISH_PTR_LIST(phi2);
165 FINISH_PTR_LIST(phi1);
168 static int insn_compare(const void *_i1, const void *_i2)
170 const struct instruction *i1 = _i1;
171 const struct instruction *i2 = _i2;
173 if (i1->opcode != i2->opcode)
174 return i1->opcode < i2->opcode ? -1 : 1;
176 switch (i1->opcode) {
177 case OP_SEL:
178 if (i1->src3 != i2->src3)
179 return i1->src3 < i2->src3 ? -1 : 1;
180 /* Fall-through to binops */
182 /* Binary arithmetic */
183 case OP_ADD: case OP_SUB:
184 case OP_MULU: case OP_MULS:
185 case OP_DIVU: case OP_DIVS:
186 case OP_MODU: case OP_MODS:
187 case OP_SHL:
188 case OP_LSR: case OP_ASR:
189 case OP_AND: case OP_OR:
191 /* Binary logical */
192 case OP_XOR: case OP_AND_BOOL:
193 case OP_OR_BOOL:
195 /* Binary comparison */
196 case OP_SET_EQ: case OP_SET_NE:
197 case OP_SET_LE: case OP_SET_GE:
198 case OP_SET_LT: case OP_SET_GT:
199 case OP_SET_B: case OP_SET_A:
200 case OP_SET_BE: case OP_SET_AE:
201 if (i1->src2 != i2->src2)
202 return i1->src2 < i2->src2 ? -1 : 1;
203 /* Fall through to unops */
205 /* Unary */
206 case OP_NOT: case OP_NEG:
207 if (i1->src1 != i2->src1)
208 return i1->src1 < i2->src1 ? -1 : 1;
209 break;
211 case OP_SYMADDR:
212 if (i1->symbol != i2->symbol)
213 return i1->symbol < i2->symbol ? -1 : 1;
214 break;
216 case OP_SETVAL:
217 if (i1->val != i2->val)
218 return i1->val < i2->val ? -1 : 1;
219 break;
221 /* Other */
222 case OP_PHI:
223 return phi_list_compare(i1->phi_list, i2->phi_list);
225 case OP_CAST:
226 case OP_SCAST:
227 case OP_PTRCAST:
229 * This is crap! See the comments on hashing.
231 if (i1->orig_type != i2->orig_type)
232 return i1->orig_type < i2->orig_type ? -1 : 1;
233 if (i1->src != i2->src)
234 return i1->src < i2->src ? -1 : 1;
235 break;
237 default:
238 warning(i1->pos, "bad instruction on hash chain");
240 if (i1->size != i2->size)
241 return i1->size < i2->size ? -1 : 1;
242 return 0;
245 static void sort_instruction_list(struct instruction_list **list)
247 sort_list((struct ptr_list **)list , insn_compare);
250 static struct instruction * cse_one_instruction(struct instruction *insn, struct instruction *def)
252 convert_instruction_target(insn, def->target);
253 insn->opcode = OP_NOP;
254 insn->bb = NULL;
255 repeat_phase |= REPEAT_CSE;
256 return def;
260 * Does "bb1" dominate "bb2"?
262 static int bb_dominates(struct entrypoint *ep, struct basic_block *bb1, struct basic_block *bb2, unsigned long generation)
264 struct basic_block *parent;
266 /* Nothing dominates the entrypoint.. */
267 if (bb2 == ep->entry->bb)
268 return 0;
269 FOR_EACH_PTR(bb2->parents, parent) {
270 if (parent == bb1)
271 continue;
272 if (parent->generation == generation)
273 continue;
274 parent->generation = generation;
275 if (!bb_dominates(ep, bb1, parent, generation))
276 return 0;
277 } END_FOR_EACH_PTR(parent);
278 return 1;
281 static struct basic_block *trivial_common_parent(struct basic_block *bb1, struct basic_block *bb2)
283 struct basic_block *parent;
285 if (bb_list_size(bb1->parents) != 1)
286 return NULL;
287 parent = first_basic_block(bb1->parents);
288 if (bb_list_size(bb2->parents) != 1)
289 return NULL;
290 if (first_basic_block(bb2->parents) != parent)
291 return NULL;
292 return parent;
295 static inline void remove_instruction(struct instruction_list **list, struct instruction *insn, int count)
297 delete_ptr_list_entry((struct ptr_list **)list, insn, count);
300 static void add_instruction_to_end(struct instruction *insn, struct basic_block *bb)
302 struct instruction *br = delete_last_instruction(&bb->insns);
303 insn->bb = bb;
304 add_instruction(&bb->insns, insn);
305 add_instruction(&bb->insns, br);
308 static struct instruction * try_to_cse(struct entrypoint *ep, struct instruction *i1, struct instruction *i2)
310 struct basic_block *b1, *b2, *common;
313 * OK, i1 and i2 are the same instruction, modulo "target".
314 * We should now see if we can combine them.
316 b1 = i1->bb;
317 b2 = i2->bb;
320 * PHI-nodes do not care where they are - the only thing that matters
321 * are the PHI _sources_.
323 if (i1->opcode == OP_PHI)
324 return cse_one_instruction(i1, i2);
327 * Currently we only handle the uninteresting degenerate case where
328 * the CSE is inside one basic-block.
330 if (b1 == b2) {
331 struct instruction *insn;
332 FOR_EACH_PTR(b1->insns, insn) {
333 if (insn == i1)
334 return cse_one_instruction(i2, i1);
335 if (insn == i2)
336 return cse_one_instruction(i1, i2);
337 } END_FOR_EACH_PTR(insn);
338 warning(b1->pos, "Whaa? unable to find CSE instructions");
339 return i1;
341 if (bb_dominates(ep, b1, b2, ++bb_generation))
342 return cse_one_instruction(i2, i1);
344 if (bb_dominates(ep, b2, b1, ++bb_generation))
345 return cse_one_instruction(i1, i2);
347 /* No direct dominance - but we could try to find a common ancestor.. */
348 common = trivial_common_parent(b1, b2);
349 if (common) {
350 i1 = cse_one_instruction(i2, i1);
351 remove_instruction(&b1->insns, i1, 1);
352 add_instruction_to_end(i1, common);
355 return i1;
358 void cleanup_and_cse(struct entrypoint *ep)
360 int i;
362 simplify_memops(ep);
363 repeat:
364 repeat_phase = 0;
365 clean_up_insns(ep);
366 for (i = 0; i < INSN_HASH_SIZE; i++) {
367 struct instruction_list **list = insn_hash_table + i;
368 if (*list) {
369 if (instruction_list_size(*list) > 1) {
370 struct instruction *insn, *last;
372 sort_instruction_list(list);
374 last = NULL;
375 FOR_EACH_PTR(*list, insn) {
376 if (!insn->bb)
377 continue;
378 if (last) {
379 if (!insn_compare(last, insn))
380 insn = try_to_cse(ep, last, insn);
382 last = insn;
383 } END_FOR_EACH_PTR(insn);
385 free_ptr_list((struct ptr_list **)list);
389 if (repeat_phase & REPEAT_SYMBOL_CLEANUP)
390 simplify_memops(ep);
392 if (repeat_phase & REPEAT_CSE)
393 goto repeat;