Do some kind of signed cast too.
[smatch.git] / cse.c
blob31ceafdff49423e48bc5996d31fc1cb90123944d
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 65536
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 /* Fallthrough */
52 /* Binary arithmetic */
53 case OP_ADD: case OP_SUB:
54 case OP_MUL: case OP_DIV:
55 case OP_MOD: case OP_SHL:
56 case OP_SHR:
57 case OP_AND: case OP_OR:
59 /* Binary logical */
60 case OP_XOR: case OP_AND_BOOL:
61 case OP_OR_BOOL:
63 /* Binary comparison */
64 case OP_SET_EQ: case OP_SET_NE:
65 case OP_SET_LE: case OP_SET_GE:
66 case OP_SET_LT: case OP_SET_GT:
67 case OP_SET_B: case OP_SET_A:
68 case OP_SET_BE: case OP_SET_AE:
69 hash += hashval(insn->src2);
70 /* Fallthrough */
72 /* Unary */
73 case OP_NOT: case OP_NEG:
74 hash += hashval(insn->src1);
75 break;
77 case OP_SETVAL:
78 hash += hashval(insn->val);
79 hash += hashval(insn->symbol);
80 break;
82 /* Other */
83 case OP_PHI: {
84 pseudo_t phi;
85 FOR_EACH_PTR(insn->phi_list, phi) {
86 struct instruction *def;
87 if (phi == VOID || !phi->def)
88 continue;
89 def = phi->def;
90 hash += hashval(def->src1);
91 hash += hashval(def->bb);
92 } END_FOR_EACH_PTR(phi);
93 break;
96 default:
98 * Nothing to do, don't even bother hashing them,
99 * we're not going to try to CSE them
101 return;
103 hash += hash >> 16;
104 hash &= INSN_HASH_SIZE-1;
105 add_instruction(insn_hash_table + hash, insn);
108 static void clean_up_insns(struct entrypoint *ep)
110 struct basic_block *bb;
112 FOR_EACH_PTR(ep->bbs, bb) {
113 struct instruction *insn;
114 FOR_EACH_PTR(bb->insns, insn) {
115 clean_up_one_instruction(bb, insn);
116 } END_FOR_EACH_PTR(insn);
117 } END_FOR_EACH_PTR(bb);
120 /* Compare two (sorted) phi-lists */
121 static int phi_list_compare(struct pseudo_list *l1, struct pseudo_list *l2)
123 pseudo_t phi1, phi2;
125 PREPARE_PTR_LIST(l1, phi1);
126 PREPARE_PTR_LIST(l2, phi2);
127 for (;;) {
128 int cmp;
130 while (phi1 && (phi1 == VOID || !phi1->def))
131 NEXT_PTR_LIST(phi1);
132 while (phi2 && (phi2 == VOID || !phi2->def))
133 NEXT_PTR_LIST(phi2);
135 if (!phi1)
136 return phi2 ? -1 : 0;
137 if (!phi2)
138 return phi1 ? 1 : 0;
139 cmp = phi_compare(phi1, phi2);
140 if (cmp)
141 return cmp;
142 NEXT_PTR_LIST(phi1);
143 NEXT_PTR_LIST(phi2);
145 /* Not reached, but we need to make the nesting come out right */
146 FINISH_PTR_LIST(phi2);
147 FINISH_PTR_LIST(phi1);
150 static int insn_compare(const void *_i1, const void *_i2)
152 const struct instruction *i1 = _i1;
153 const struct instruction *i2 = _i2;
155 if (i1->opcode != i2->opcode)
156 return i1->opcode < i2->opcode ? -1 : 1;
158 switch (i1->opcode) {
159 case OP_SEL:
160 if (i1->src3 != i2->src3)
161 return i1->src3 < i2->src3 ? -1 : 1;
162 /* Fall-through to binops */
164 /* Binary arithmetic */
165 case OP_ADD: case OP_SUB:
166 case OP_MUL: case OP_DIV:
167 case OP_MOD: case OP_SHL:
168 case OP_SHR:
169 case OP_AND: case OP_OR:
171 /* Binary logical */
172 case OP_XOR: case OP_AND_BOOL:
173 case OP_OR_BOOL:
175 /* Binary comparison */
176 case OP_SET_EQ: case OP_SET_NE:
177 case OP_SET_LE: case OP_SET_GE:
178 case OP_SET_LT: case OP_SET_GT:
179 case OP_SET_B: case OP_SET_A:
180 case OP_SET_BE: case OP_SET_AE:
181 if (i1->src2 != i2->src2)
182 return i1->src2 < i2->src2 ? -1 : 1;
183 /* Fall-through to unops */
185 /* Unary */
186 case OP_NOT: case OP_NEG:
187 if (i1->src1 != i2->src1)
188 return i1->src1 < i2->src1 ? -1 : 1;
189 break;
191 case OP_SETVAL:
192 if (i1->val != i2->val)
193 return i1->val < i2->val ? -1 : 1;
194 if (i1->symbol != i2->symbol)
195 return i1->symbol < i2->symbol ? -1 : 1;
196 break;
198 /* Other */
199 case OP_PHI:
200 return phi_list_compare(i1->phi_list, i2->phi_list);
202 default:
203 warning(i1->bb->pos, "bad instruction on hash chain");
205 if (i1->size != i2->size)
206 return i1->size < i2->size ? -1 : 1;
207 return 0;
210 static void sort_instruction_list(struct instruction_list **list)
212 sort_list((struct ptr_list **)list , insn_compare);
215 static struct instruction * cse_one_instruction(struct instruction *insn, struct instruction *def)
217 convert_instruction_target(insn, def->target);
218 insn->opcode = OP_NOP;
219 insn->bb = NULL;
220 repeat_phase |= REPEAT_CSE;
221 return def;
225 * Does "bb1" dominate "bb2"?
227 static int bb_dominates(struct entrypoint *ep, struct basic_block *bb1, struct basic_block *bb2, unsigned long generation)
229 struct basic_block *parent;
231 /* Nothing dominates the entrypoint.. */
232 if (bb2 == ep->entry->bb)
233 return 0;
234 FOR_EACH_PTR(bb2->parents, parent) {
235 if (parent == bb1)
236 continue;
237 if (parent->generation == generation)
238 continue;
239 parent->generation = generation;
240 if (!bb_dominates(ep, bb1, parent, generation))
241 return 0;
242 } END_FOR_EACH_PTR(parent);
243 return 1;
246 static struct basic_block *trivial_common_parent(struct basic_block *bb1, struct basic_block *bb2)
248 struct basic_block *parent;
250 if (bb_list_size(bb1->parents) != 1)
251 return 0;
252 parent = first_basic_block(bb1->parents);
253 if (bb_list_size(bb2->parents) != 1)
254 return 0;
255 if (first_basic_block(bb2->parents) != parent)
256 return 0;
257 return parent;
260 static inline void remove_instruction(struct instruction_list **list, struct instruction *insn, int count)
262 delete_ptr_list_entry((struct ptr_list **)list, insn, count);
265 static void add_instruction_to_end(struct instruction *insn, struct basic_block *bb)
267 struct instruction *br = delete_last_instruction(&bb->insns);
268 insn->bb = bb;
269 add_instruction(&bb->insns, insn);
270 add_instruction(&bb->insns, br);
273 static struct instruction * try_to_cse(struct entrypoint *ep, struct instruction *i1, struct instruction *i2)
275 struct basic_block *b1, *b2, *common;
278 * Ok, i1 and i2 are the same instruction, modulo "target".
279 * We should now see if we can combine them.
281 b1 = i1->bb;
282 b2 = i2->bb;
285 * PHI-nodes do not care where they are - the only thing that matters
286 * are the PHI _sources_.
288 if (i1->opcode == OP_PHI)
289 return cse_one_instruction(i1, i2);
292 * Currently we only handle the uninteresting degenerate case where
293 * the CSE is inside one basic-block.
295 if (b1 == b2) {
296 struct instruction *insn;
297 FOR_EACH_PTR(b1->insns, insn) {
298 if (insn == i1)
299 return cse_one_instruction(i2, i1);
300 if (insn == i2)
301 return cse_one_instruction(i1, i2);
302 } END_FOR_EACH_PTR(insn);
303 warning(b1->pos, "Whaa? unable to find CSE instructions");
304 return i1;
306 if (bb_dominates(ep, b1, b2, ++bb_generation))
307 return cse_one_instruction(i2, i1);
309 if (bb_dominates(ep, b2, b1, ++bb_generation))
310 return cse_one_instruction(i1, i2);
312 /* No direct dominance - but we could try to find a common ancestor.. */
313 common = trivial_common_parent(b1, b2);
314 if (common) {
315 i1 = cse_one_instruction(i2, i1);
316 remove_instruction(&b1->insns, i1, 1);
317 add_instruction_to_end(i1, common);
320 return i1;
323 void cleanup_and_cse(struct entrypoint *ep)
325 int i;
327 simplify_memops(ep);
328 repeat:
329 repeat_phase = 0;
330 clean_up_insns(ep);
331 for (i = 0; i < INSN_HASH_SIZE; i++) {
332 struct instruction_list **list = insn_hash_table + i;
333 if (*list) {
334 if (instruction_list_size(*list) > 1) {
335 struct instruction *insn, *last;
337 sort_instruction_list(list);
339 last = NULL;
340 FOR_EACH_PTR(*list, insn) {
341 if (!insn->bb)
342 continue;
343 if (last) {
344 if (!insn_compare(last, insn))
345 insn = try_to_cse(ep, last, insn);
347 last = insn;
348 } END_FOR_EACH_PTR(insn);
350 free_ptr_list((struct ptr_list **)list);
354 if (repeat_phase & REPEAT_SYMBOL_CLEANUP)
355 simplify_memops(ep);
357 if (repeat_phase & REPEAT_CSE)
358 goto repeat;