db_info: remove unused print
[smatch.git] / cse.c
blobe8fbe344fe3ddc8651ebd8961c4378b104f1e342
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);
254 if (insn->opcode == OP_PHI) {
255 /* Remove the instruction from PHI users */
256 pseudo_t phi;
257 FOR_EACH_PTR(insn->phi_list, phi) {
258 struct pseudo_user *pu;
259 FOR_EACH_PTR(phi->users, pu) {
260 if (pu->insn == insn)
261 DELETE_CURRENT_PTR(pu);
262 } END_FOR_EACH_PTR(pu);
263 } END_FOR_EACH_PTR(phi);
266 insn->opcode = OP_NOP;
267 insn->bb = NULL;
268 repeat_phase |= REPEAT_CSE;
269 return def;
273 * Does "bb1" dominate "bb2"?
275 static int bb_dominates(struct entrypoint *ep, struct basic_block *bb1, struct basic_block *bb2, unsigned long generation)
277 struct basic_block *parent;
279 /* Nothing dominates the entrypoint.. */
280 if (bb2 == ep->entry->bb)
281 return 0;
282 FOR_EACH_PTR(bb2->parents, parent) {
283 if (parent == bb1)
284 continue;
285 if (parent->generation == generation)
286 continue;
287 parent->generation = generation;
288 if (!bb_dominates(ep, bb1, parent, generation))
289 return 0;
290 } END_FOR_EACH_PTR(parent);
291 return 1;
294 static struct basic_block *trivial_common_parent(struct basic_block *bb1, struct basic_block *bb2)
296 struct basic_block *parent;
298 if (bb_list_size(bb1->parents) != 1)
299 return NULL;
300 parent = first_basic_block(bb1->parents);
301 if (bb_list_size(bb2->parents) != 1)
302 return NULL;
303 if (first_basic_block(bb2->parents) != parent)
304 return NULL;
305 return parent;
308 static inline void remove_instruction(struct instruction_list **list, struct instruction *insn, int count)
310 delete_ptr_list_entry((struct ptr_list **)list, insn, count);
313 static void add_instruction_to_end(struct instruction *insn, struct basic_block *bb)
315 struct instruction *br = delete_last_instruction(&bb->insns);
316 insn->bb = bb;
317 add_instruction(&bb->insns, insn);
318 add_instruction(&bb->insns, br);
321 static struct instruction * try_to_cse(struct entrypoint *ep, struct instruction *i1, struct instruction *i2)
323 struct basic_block *b1, *b2, *common;
326 * OK, i1 and i2 are the same instruction, modulo "target".
327 * We should now see if we can combine them.
329 b1 = i1->bb;
330 b2 = i2->bb;
333 * Currently we only handle the uninteresting degenerate case where
334 * the CSE is inside one basic-block.
336 if (b1 == b2) {
337 struct instruction *insn;
338 FOR_EACH_PTR(b1->insns, insn) {
339 if (insn == i1)
340 return cse_one_instruction(i2, i1);
341 if (insn == i2)
342 return cse_one_instruction(i1, i2);
343 } END_FOR_EACH_PTR(insn);
344 warning(b1->pos, "Whaa? unable to find CSE instructions");
345 return i1;
347 if (bb_dominates(ep, b1, b2, ++bb_generation))
348 return cse_one_instruction(i2, i1);
350 if (bb_dominates(ep, b2, b1, ++bb_generation))
351 return cse_one_instruction(i1, i2);
353 /* No direct dominance - but we could try to find a common ancestor.. */
354 common = trivial_common_parent(b1, b2);
355 if (common) {
356 i1 = cse_one_instruction(i2, i1);
357 remove_instruction(&b1->insns, i1, 1);
358 add_instruction_to_end(i1, common);
361 return i1;
364 void cleanup_and_cse(struct entrypoint *ep)
366 int i;
368 simplify_memops(ep);
369 repeat:
370 repeat_phase = 0;
371 clean_up_insns(ep);
372 for (i = 0; i < INSN_HASH_SIZE; i++) {
373 struct instruction_list **list = insn_hash_table + i;
374 if (*list) {
375 if (instruction_list_size(*list) > 1) {
376 struct instruction *insn, *last;
378 sort_instruction_list(list);
380 last = NULL;
381 FOR_EACH_PTR(*list, insn) {
382 if (!insn->bb)
383 continue;
384 if (last) {
385 if (!insn_compare(last, insn))
386 insn = try_to_cse(ep, last, insn);
388 last = insn;
389 } END_FOR_EACH_PTR(insn);
391 free_ptr_list((struct ptr_list **)list);
395 if (repeat_phase & REPEAT_SYMBOL_CLEANUP)
396 simplify_memops(ep);
398 if (repeat_phase & REPEAT_CSE)
399 goto repeat;