CSE: use commutativity to identify equivalent instructions
[smatch.git] / cse.c
blob5380f4ccd2383813e7ff69a9875dc5f28b607ce8
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) {
178 /* commutative binop */
179 case OP_ADD:
180 case OP_MULU: case OP_MULS:
181 case OP_AND_BOOL: case OP_OR_BOOL:
182 case OP_AND: case OP_OR:
183 case OP_XOR:
184 case OP_SET_EQ: case OP_SET_NE:
185 if (i1->src1 == i2->src2 && i1->src2 == i2->src1)
186 return 0;
187 goto case_binops;
189 case OP_SEL:
190 if (i1->src3 != i2->src3)
191 return i1->src3 < i2->src3 ? -1 : 1;
192 /* Fall-through to binops */
194 /* Binary arithmetic */
195 case OP_SUB:
196 case OP_DIVU: case OP_DIVS:
197 case OP_MODU: case OP_MODS:
198 case OP_SHL:
199 case OP_LSR: case OP_ASR:
201 /* Binary comparison */
202 case OP_SET_LE: case OP_SET_GE:
203 case OP_SET_LT: case OP_SET_GT:
204 case OP_SET_B: case OP_SET_A:
205 case OP_SET_BE: case OP_SET_AE:
206 case_binops:
207 if (i1->src2 != i2->src2)
208 return i1->src2 < i2->src2 ? -1 : 1;
209 /* Fall through to unops */
211 /* Unary */
212 case OP_NOT: case OP_NEG:
213 if (i1->src1 != i2->src1)
214 return i1->src1 < i2->src1 ? -1 : 1;
215 break;
217 case OP_SYMADDR:
218 if (i1->symbol != i2->symbol)
219 return i1->symbol < i2->symbol ? -1 : 1;
220 break;
222 case OP_SETVAL:
223 if (i1->val != i2->val)
224 return i1->val < i2->val ? -1 : 1;
225 break;
227 /* Other */
228 case OP_PHI:
229 return phi_list_compare(i1->phi_list, i2->phi_list);
231 case OP_CAST:
232 case OP_SCAST:
233 case OP_PTRCAST:
235 * This is crap! See the comments on hashing.
237 if (i1->orig_type != i2->orig_type)
238 return i1->orig_type < i2->orig_type ? -1 : 1;
239 if (i1->src != i2->src)
240 return i1->src < i2->src ? -1 : 1;
241 break;
243 default:
244 warning(i1->pos, "bad instruction on hash chain");
246 if (i1->size != i2->size)
247 return i1->size < i2->size ? -1 : 1;
248 return 0;
251 static void sort_instruction_list(struct instruction_list **list)
253 sort_list((struct ptr_list **)list , insn_compare);
256 static struct instruction * cse_one_instruction(struct instruction *insn, struct instruction *def)
258 convert_instruction_target(insn, def->target);
260 kill_instruction(insn);
261 repeat_phase |= REPEAT_CSE;
262 return def;
266 * Does "bb1" dominate "bb2"?
268 static int bb_dominates(struct entrypoint *ep, struct basic_block *bb1, struct basic_block *bb2, unsigned long generation)
270 struct basic_block *parent;
272 /* Nothing dominates the entrypoint.. */
273 if (bb2 == ep->entry->bb)
274 return 0;
275 FOR_EACH_PTR(bb2->parents, parent) {
276 if (parent == bb1)
277 continue;
278 if (parent->generation == generation)
279 continue;
280 parent->generation = generation;
281 if (!bb_dominates(ep, bb1, parent, generation))
282 return 0;
283 } END_FOR_EACH_PTR(parent);
284 return 1;
287 static struct basic_block *trivial_common_parent(struct basic_block *bb1, struct basic_block *bb2)
289 struct basic_block *parent;
291 if (bb_list_size(bb1->parents) != 1)
292 return NULL;
293 parent = first_basic_block(bb1->parents);
294 if (bb_list_size(bb2->parents) != 1)
295 return NULL;
296 if (first_basic_block(bb2->parents) != parent)
297 return NULL;
298 return parent;
301 static inline void remove_instruction(struct instruction_list **list, struct instruction *insn, int count)
303 delete_ptr_list_entry((struct ptr_list **)list, insn, count);
306 static void add_instruction_to_end(struct instruction *insn, struct basic_block *bb)
308 struct instruction *br = delete_last_instruction(&bb->insns);
309 insn->bb = bb;
310 add_instruction(&bb->insns, insn);
311 add_instruction(&bb->insns, br);
314 static struct instruction * try_to_cse(struct entrypoint *ep, struct instruction *i1, struct instruction *i2)
316 struct basic_block *b1, *b2, *common;
319 * OK, i1 and i2 are the same instruction, modulo "target".
320 * We should now see if we can combine them.
322 b1 = i1->bb;
323 b2 = i2->bb;
326 * Currently we only handle the uninteresting degenerate case where
327 * the CSE is inside one basic-block.
329 if (b1 == b2) {
330 struct instruction *insn;
331 FOR_EACH_PTR(b1->insns, insn) {
332 if (insn == i1)
333 return cse_one_instruction(i2, i1);
334 if (insn == i2)
335 return cse_one_instruction(i1, i2);
336 } END_FOR_EACH_PTR(insn);
337 warning(b1->pos, "Whaa? unable to find CSE instructions");
338 return i1;
340 if (bb_dominates(ep, b1, b2, ++bb_generation))
341 return cse_one_instruction(i2, i1);
343 if (bb_dominates(ep, b2, b1, ++bb_generation))
344 return cse_one_instruction(i1, i2);
346 /* No direct dominance - but we could try to find a common ancestor.. */
347 common = trivial_common_parent(b1, b2);
348 if (common) {
349 i1 = cse_one_instruction(i2, i1);
350 remove_instruction(&b1->insns, i1, 1);
351 add_instruction_to_end(i1, common);
354 return i1;
357 void cleanup_and_cse(struct entrypoint *ep)
359 int i;
361 simplify_memops(ep);
362 repeat:
363 repeat_phase = 0;
364 clean_up_insns(ep);
365 for (i = 0; i < INSN_HASH_SIZE; i++) {
366 struct instruction_list **list = insn_hash_table + i;
367 if (*list) {
368 if (instruction_list_size(*list) > 1) {
369 struct instruction *insn, *last;
371 sort_instruction_list(list);
373 last = NULL;
374 FOR_EACH_PTR(*list, insn) {
375 if (!insn->bb)
376 continue;
377 if (last) {
378 if (!insn_compare(last, insn))
379 insn = try_to_cse(ep, last, insn);
381 last = insn;
382 } END_FOR_EACH_PTR(insn);
384 free_ptr_list((struct ptr_list **)list);
388 if (repeat_phase & REPEAT_SYMBOL_CLEANUP)
389 simplify_memops(ep);
391 if (repeat_phase & REPEAT_CSE)
392 goto repeat;