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
16 #include "expression.h"
17 #include "linearize.h"
20 #define INSN_HASH_SIZE 256
21 static struct instruction_list
*insn_hash_table
[INSN_HASH_SIZE
];
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;
38 static void clean_up_one_instruction(struct basic_block
*bb
, struct instruction
*insn
)
44 assert(insn
->bb
== bb
);
45 repeat_phase
|= simplify_instruction(insn
);
48 hash
= (insn
->opcode
<< 3) + (insn
->size
>> 3);
49 switch (insn
->opcode
) {
51 hash
+= hashval(insn
->src3
);
54 /* Binary arithmetic */
55 case OP_ADD
: case OP_SUB
:
56 case OP_MULU
: case OP_MULS
:
57 case OP_DIVU
: case OP_DIVS
:
58 case OP_MODU
: case OP_MODS
:
60 case OP_LSR
: case OP_ASR
:
61 case OP_AND
: case OP_OR
:
64 case OP_XOR
: case OP_AND_BOOL
:
67 /* Binary comparison */
68 case OP_SET_EQ
: case OP_SET_NE
:
69 case OP_SET_LE
: case OP_SET_GE
:
70 case OP_SET_LT
: case OP_SET_GT
:
71 case OP_SET_B
: case OP_SET_A
:
72 case OP_SET_BE
: case OP_SET_AE
:
73 hash
+= hashval(insn
->src2
);
77 case OP_NOT
: case OP_NEG
:
78 hash
+= hashval(insn
->src1
);
82 hash
+= hashval(insn
->val
);
86 hash
+= hashval(insn
->symbol
);
93 * This is crap! Many "orig_types" are the
94 * same as far as casts go, we should generate
95 * some kind of "type hash" that is identical
98 hash
+= hashval(insn
->orig_type
);
99 hash
+= hashval(insn
->src
);
105 FOR_EACH_PTR(insn
->phi_list
, phi
) {
106 struct instruction
*def
;
107 if (phi
== VOID
|| !phi
->def
)
110 hash
+= hashval(def
->src1
);
111 hash
+= hashval(def
->bb
);
112 } END_FOR_EACH_PTR(phi
);
118 * Nothing to do, don't even bother hashing them,
119 * we're not going to try to CSE them
124 hash
&= INSN_HASH_SIZE
-1;
125 add_instruction(insn_hash_table
+ hash
, insn
);
128 static void clean_up_insns(struct entrypoint
*ep
)
130 struct basic_block
*bb
;
132 FOR_EACH_PTR(ep
->bbs
, bb
) {
133 struct instruction
*insn
;
134 FOR_EACH_PTR(bb
->insns
, insn
) {
135 clean_up_one_instruction(bb
, insn
);
136 } END_FOR_EACH_PTR(insn
);
137 } END_FOR_EACH_PTR(bb
);
140 /* Compare two (sorted) phi-lists */
141 static int phi_list_compare(struct pseudo_list
*l1
, struct pseudo_list
*l2
)
145 PREPARE_PTR_LIST(l1
, phi1
);
146 PREPARE_PTR_LIST(l2
, phi2
);
150 while (phi1
&& (phi1
== VOID
|| !phi1
->def
))
152 while (phi2
&& (phi2
== VOID
|| !phi2
->def
))
156 return phi2
? -1 : 0;
159 cmp
= phi_compare(phi1
, phi2
);
165 /* Not reached, but we need to make the nesting come out right */
166 FINISH_PTR_LIST(phi2
);
167 FINISH_PTR_LIST(phi1
);
170 static int insn_compare(const void *_i1
, const void *_i2
)
172 const struct instruction
*i1
= _i1
;
173 const struct instruction
*i2
= _i2
;
175 if (i1
->opcode
!= i2
->opcode
)
176 return i1
->opcode
< i2
->opcode
? -1 : 1;
178 switch (i1
->opcode
) {
180 /* commutative binop */
182 case OP_MULU
: case OP_MULS
:
183 case OP_AND_BOOL
: case OP_OR_BOOL
:
184 case OP_AND
: case OP_OR
:
186 case OP_SET_EQ
: case OP_SET_NE
:
187 if (i1
->src1
== i2
->src2
&& i1
->src2
== i2
->src1
)
192 if (i1
->src3
!= i2
->src3
)
193 return i1
->src3
< i2
->src3
? -1 : 1;
194 /* Fall-through to binops */
196 /* Binary arithmetic */
198 case OP_DIVU
: case OP_DIVS
:
199 case OP_MODU
: case OP_MODS
:
201 case OP_LSR
: case OP_ASR
:
203 /* Binary comparison */
204 case OP_SET_LE
: case OP_SET_GE
:
205 case OP_SET_LT
: case OP_SET_GT
:
206 case OP_SET_B
: case OP_SET_A
:
207 case OP_SET_BE
: case OP_SET_AE
:
209 if (i1
->src2
!= i2
->src2
)
210 return i1
->src2
< i2
->src2
? -1 : 1;
211 /* Fall through to unops */
214 case OP_NOT
: case OP_NEG
:
215 if (i1
->src1
!= i2
->src1
)
216 return i1
->src1
< i2
->src1
? -1 : 1;
220 if (i1
->symbol
!= i2
->symbol
)
221 return i1
->symbol
< i2
->symbol
? -1 : 1;
225 if (i1
->val
!= i2
->val
)
226 return i1
->val
< i2
->val
? -1 : 1;
231 return phi_list_compare(i1
->phi_list
, i2
->phi_list
);
237 * This is crap! See the comments on hashing.
239 if (i1
->orig_type
!= i2
->orig_type
)
240 return i1
->orig_type
< i2
->orig_type
? -1 : 1;
241 if (i1
->src
!= i2
->src
)
242 return i1
->src
< i2
->src
? -1 : 1;
246 warning(i1
->pos
, "bad instruction on hash chain");
248 if (i1
->size
!= i2
->size
)
249 return i1
->size
< i2
->size
? -1 : 1;
253 static void sort_instruction_list(struct instruction_list
**list
)
255 sort_list((struct ptr_list
**)list
, insn_compare
);
258 static struct instruction
* cse_one_instruction(struct instruction
*insn
, struct instruction
*def
)
260 convert_instruction_target(insn
, def
->target
);
262 kill_instruction(insn
);
263 repeat_phase
|= REPEAT_CSE
;
268 * Does "bb1" dominate "bb2"?
270 static int bb_dominates(struct entrypoint
*ep
, struct basic_block
*bb1
, struct basic_block
*bb2
, unsigned long generation
)
272 struct basic_block
*parent
;
274 /* Nothing dominates the entrypoint.. */
275 if (bb2
== ep
->entry
->bb
)
277 FOR_EACH_PTR(bb2
->parents
, parent
) {
280 if (parent
->generation
== generation
)
282 parent
->generation
= generation
;
283 if (!bb_dominates(ep
, bb1
, parent
, generation
))
285 } END_FOR_EACH_PTR(parent
);
289 static struct basic_block
*trivial_common_parent(struct basic_block
*bb1
, struct basic_block
*bb2
)
291 struct basic_block
*parent
;
293 if (bb_list_size(bb1
->parents
) != 1)
295 parent
= first_basic_block(bb1
->parents
);
296 if (bb_list_size(bb2
->parents
) != 1)
298 if (first_basic_block(bb2
->parents
) != parent
)
303 static inline void remove_instruction(struct instruction_list
**list
, struct instruction
*insn
, int count
)
305 delete_ptr_list_entry((struct ptr_list
**)list
, insn
, count
);
308 static void add_instruction_to_end(struct instruction
*insn
, struct basic_block
*bb
)
310 struct instruction
*br
= delete_last_instruction(&bb
->insns
);
312 add_instruction(&bb
->insns
, insn
);
313 add_instruction(&bb
->insns
, br
);
316 static struct instruction
* try_to_cse(struct entrypoint
*ep
, struct instruction
*i1
, struct instruction
*i2
)
318 struct basic_block
*b1
, *b2
, *common
;
321 * OK, i1 and i2 are the same instruction, modulo "target".
322 * We should now see if we can combine them.
328 * Currently we only handle the uninteresting degenerate case where
329 * the CSE is inside one basic-block.
332 struct instruction
*insn
;
333 FOR_EACH_PTR(b1
->insns
, insn
) {
335 return cse_one_instruction(i2
, i1
);
337 return cse_one_instruction(i1
, i2
);
338 } END_FOR_EACH_PTR(insn
);
339 warning(b1
->pos
, "Whaa? unable to find CSE instructions");
342 if (bb_dominates(ep
, b1
, b2
, ++bb_generation
))
343 return cse_one_instruction(i2
, i1
);
345 if (bb_dominates(ep
, b2
, b1
, ++bb_generation
))
346 return cse_one_instruction(i1
, i2
);
348 /* No direct dominance - but we could try to find a common ancestor.. */
349 common
= trivial_common_parent(b1
, b2
);
351 i1
= cse_one_instruction(i2
, i1
);
352 remove_instruction(&b1
->insns
, i1
, 1);
353 add_instruction_to_end(i1
, common
);
359 void cleanup_and_cse(struct entrypoint
*ep
)
367 for (i
= 0; i
< INSN_HASH_SIZE
; i
++) {
368 struct instruction_list
**list
= insn_hash_table
+ i
;
370 if (instruction_list_size(*list
) > 1) {
371 struct instruction
*insn
, *last
;
373 sort_instruction_list(list
);
376 FOR_EACH_PTR(*list
, insn
) {
380 if (!insn_compare(last
, insn
))
381 insn
= try_to_cse(ep
, last
, insn
);
384 } END_FOR_EACH_PTR(insn
);
386 free_ptr_list((struct ptr_list
**)list
);
390 if (repeat_phase
& REPEAT_SYMBOL_CLEANUP
)
393 if (repeat_phase
& REPEAT_CSE
)