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 65536
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
);
46 hash
= (insn
->opcode
<< 3) + (insn
->size
>> 3);
47 switch (insn
->opcode
) {
49 hash
+= hashval(insn
->src3
);
52 /* Binary arithmetic */
53 case OP_ADD
: case OP_SUB
:
54 case OP_MUL
: case OP_DIV
:
55 case OP_MOD
: case OP_SHL
:
57 case OP_AND
: case OP_OR
:
60 case OP_XOR
: case OP_AND_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
);
73 case OP_NOT
: case OP_NEG
:
74 hash
+= hashval(insn
->src1
);
78 hash
+= hashval(insn
->val
);
79 hash
+= hashval(insn
->symbol
);
85 FOR_EACH_PTR(insn
->phi_list
, phi
) {
86 struct instruction
*def
;
87 if (phi
== VOID
|| !phi
->def
)
90 hash
+= hashval(def
->src1
);
91 hash
+= hashval(def
->bb
);
92 } END_FOR_EACH_PTR(phi
);
98 * Nothing to do, don't even bother hashing them,
99 * we're not going to try to CSE them
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
)
125 PREPARE_PTR_LIST(l1
, phi1
);
126 PREPARE_PTR_LIST(l2
, phi2
);
130 while (phi1
&& (phi1
== VOID
|| !phi1
->def
))
132 while (phi2
&& (phi2
== VOID
|| !phi2
->def
))
136 return phi2
? -1 : 0;
139 cmp
= phi_compare(phi1
, 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
) {
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
:
169 case OP_AND
: case OP_OR
:
172 case OP_XOR
: case OP_AND_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 */
186 case OP_NOT
: case OP_NEG
:
187 if (i1
->src1
!= i2
->src1
)
188 return i1
->src1
< i2
->src1
? -1 : 1;
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;
200 return phi_list_compare(i1
->phi_list
, i2
->phi_list
);
203 warning(i1
->bb
->pos
, "bad instruction on hash chain");
205 if (i1
->size
!= i2
->size
)
206 return i1
->size
< i2
->size
? -1 : 1;
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
;
220 repeat_phase
|= REPEAT_CSE
;
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
)
234 FOR_EACH_PTR(bb2
->parents
, parent
) {
237 if (parent
->generation
== generation
)
239 parent
->generation
= generation
;
240 if (!bb_dominates(ep
, bb1
, parent
, generation
))
242 } END_FOR_EACH_PTR(parent
);
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)
252 parent
= first_basic_block(bb1
->parents
);
253 if (bb_list_size(bb2
->parents
) != 1)
255 if (first_basic_block(bb2
->parents
) != 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
);
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.
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.
296 struct instruction
*insn
;
297 FOR_EACH_PTR(b1
->insns
, insn
) {
299 return cse_one_instruction(i2
, i1
);
301 return cse_one_instruction(i1
, i2
);
302 } END_FOR_EACH_PTR(insn
);
303 warning(b1
->pos
, "Whaa? unable to find CSE instructions");
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
);
315 i1
= cse_one_instruction(i2
, i1
);
316 remove_instruction(&b1
->insns
, i1
, 1);
317 add_instruction_to_end(i1
, common
);
323 void cleanup_and_cse(struct entrypoint
*ep
)
331 for (i
= 0; i
< INSN_HASH_SIZE
; i
++) {
332 struct instruction_list
**list
= insn_hash_table
+ i
;
334 if (instruction_list_size(*list
) > 1) {
335 struct instruction
*insn
, *last
;
337 sort_instruction_list(list
);
340 FOR_EACH_PTR(*list
, insn
) {
344 if (!insn_compare(last
, insn
))
345 insn
= try_to_cse(ep
, last
, insn
);
348 } END_FOR_EACH_PTR(insn
);
350 free_ptr_list((struct ptr_list
**)list
);
354 if (repeat_phase
& REPEAT_SYMBOL_CLEANUP
)
357 if (repeat_phase
& REPEAT_CSE
)