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
);
82 hash
+= hashval(insn
->symbol
);
88 FOR_EACH_PTR(insn
->phi_list
, phi
) {
89 struct instruction
*def
;
90 if (phi
== VOID
|| !phi
->def
)
93 hash
+= hashval(def
->src1
);
94 hash
+= hashval(def
->bb
);
95 } END_FOR_EACH_PTR(phi
);
101 * Nothing to do, don't even bother hashing them,
102 * we're not going to try to CSE them
107 hash
&= INSN_HASH_SIZE
-1;
108 add_instruction(insn_hash_table
+ hash
, insn
);
111 static void clean_up_insns(struct entrypoint
*ep
)
113 struct basic_block
*bb
;
115 FOR_EACH_PTR(ep
->bbs
, bb
) {
116 struct instruction
*insn
;
117 FOR_EACH_PTR(bb
->insns
, insn
) {
118 clean_up_one_instruction(bb
, insn
);
119 } END_FOR_EACH_PTR(insn
);
120 } END_FOR_EACH_PTR(bb
);
123 /* Compare two (sorted) phi-lists */
124 static int phi_list_compare(struct pseudo_list
*l1
, struct pseudo_list
*l2
)
128 PREPARE_PTR_LIST(l1
, phi1
);
129 PREPARE_PTR_LIST(l2
, phi2
);
133 while (phi1
&& (phi1
== VOID
|| !phi1
->def
))
135 while (phi2
&& (phi2
== VOID
|| !phi2
->def
))
139 return phi2
? -1 : 0;
142 cmp
= phi_compare(phi1
, phi2
);
148 /* Not reached, but we need to make the nesting come out right */
149 FINISH_PTR_LIST(phi2
);
150 FINISH_PTR_LIST(phi1
);
153 static int insn_compare(const void *_i1
, const void *_i2
)
155 const struct instruction
*i1
= _i1
;
156 const struct instruction
*i2
= _i2
;
158 if (i1
->opcode
!= i2
->opcode
)
159 return i1
->opcode
< i2
->opcode
? -1 : 1;
161 switch (i1
->opcode
) {
163 if (i1
->src3
!= i2
->src3
)
164 return i1
->src3
< i2
->src3
? -1 : 1;
165 /* Fall-through to binops */
167 /* Binary arithmetic */
168 case OP_ADD
: case OP_SUB
:
169 case OP_MUL
: case OP_DIV
:
170 case OP_MOD
: case OP_SHL
:
172 case OP_AND
: case OP_OR
:
175 case OP_XOR
: case OP_AND_BOOL
:
178 /* Binary comparison */
179 case OP_SET_EQ
: case OP_SET_NE
:
180 case OP_SET_LE
: case OP_SET_GE
:
181 case OP_SET_LT
: case OP_SET_GT
:
182 case OP_SET_B
: case OP_SET_A
:
183 case OP_SET_BE
: case OP_SET_AE
:
184 if (i1
->src2
!= i2
->src2
)
185 return i1
->src2
< i2
->src2
? -1 : 1;
186 /* Fall-through to unops */
189 case OP_NOT
: case OP_NEG
:
190 if (i1
->src1
!= i2
->src1
)
191 return i1
->src1
< i2
->src1
? -1 : 1;
195 if (i1
->symbol
!= i2
->symbol
)
196 return i1
->symbol
< i2
->symbol
? -1 : 1;
200 if (i1
->val
!= i2
->val
)
201 return i1
->val
< i2
->val
? -1 : 1;
206 return phi_list_compare(i1
->phi_list
, i2
->phi_list
);
209 warning(i1
->bb
->pos
, "bad instruction on hash chain");
211 if (i1
->size
!= i2
->size
)
212 return i1
->size
< i2
->size
? -1 : 1;
216 static void sort_instruction_list(struct instruction_list
**list
)
218 sort_list((struct ptr_list
**)list
, insn_compare
);
221 static struct instruction
* cse_one_instruction(struct instruction
*insn
, struct instruction
*def
)
223 convert_instruction_target(insn
, def
->target
);
224 insn
->opcode
= OP_NOP
;
226 repeat_phase
|= REPEAT_CSE
;
231 * Does "bb1" dominate "bb2"?
233 static int bb_dominates(struct entrypoint
*ep
, struct basic_block
*bb1
, struct basic_block
*bb2
, unsigned long generation
)
235 struct basic_block
*parent
;
237 /* Nothing dominates the entrypoint.. */
238 if (bb2
== ep
->entry
->bb
)
240 FOR_EACH_PTR(bb2
->parents
, parent
) {
243 if (parent
->generation
== generation
)
245 parent
->generation
= generation
;
246 if (!bb_dominates(ep
, bb1
, parent
, generation
))
248 } END_FOR_EACH_PTR(parent
);
252 static struct basic_block
*trivial_common_parent(struct basic_block
*bb1
, struct basic_block
*bb2
)
254 struct basic_block
*parent
;
256 if (bb_list_size(bb1
->parents
) != 1)
258 parent
= first_basic_block(bb1
->parents
);
259 if (bb_list_size(bb2
->parents
) != 1)
261 if (first_basic_block(bb2
->parents
) != parent
)
266 static inline void remove_instruction(struct instruction_list
**list
, struct instruction
*insn
, int count
)
268 delete_ptr_list_entry((struct ptr_list
**)list
, insn
, count
);
271 static void add_instruction_to_end(struct instruction
*insn
, struct basic_block
*bb
)
273 struct instruction
*br
= delete_last_instruction(&bb
->insns
);
275 add_instruction(&bb
->insns
, insn
);
276 add_instruction(&bb
->insns
, br
);
279 static struct instruction
* try_to_cse(struct entrypoint
*ep
, struct instruction
*i1
, struct instruction
*i2
)
281 struct basic_block
*b1
, *b2
, *common
;
284 * Ok, i1 and i2 are the same instruction, modulo "target".
285 * We should now see if we can combine them.
291 * PHI-nodes do not care where they are - the only thing that matters
292 * are the PHI _sources_.
294 if (i1
->opcode
== OP_PHI
)
295 return cse_one_instruction(i1
, i2
);
298 * Currently we only handle the uninteresting degenerate case where
299 * the CSE is inside one basic-block.
302 struct instruction
*insn
;
303 FOR_EACH_PTR(b1
->insns
, insn
) {
305 return cse_one_instruction(i2
, i1
);
307 return cse_one_instruction(i1
, i2
);
308 } END_FOR_EACH_PTR(insn
);
309 warning(b1
->pos
, "Whaa? unable to find CSE instructions");
312 if (bb_dominates(ep
, b1
, b2
, ++bb_generation
))
313 return cse_one_instruction(i2
, i1
);
315 if (bb_dominates(ep
, b2
, b1
, ++bb_generation
))
316 return cse_one_instruction(i1
, i2
);
318 /* No direct dominance - but we could try to find a common ancestor.. */
319 common
= trivial_common_parent(b1
, b2
);
321 i1
= cse_one_instruction(i2
, i1
);
322 remove_instruction(&b1
->insns
, i1
, 1);
323 add_instruction_to_end(i1
, common
);
329 void cleanup_and_cse(struct entrypoint
*ep
)
337 for (i
= 0; i
< INSN_HASH_SIZE
; i
++) {
338 struct instruction_list
**list
= insn_hash_table
+ i
;
340 if (instruction_list_size(*list
) > 1) {
341 struct instruction
*insn
, *last
;
343 sort_instruction_list(list
);
346 FOR_EACH_PTR(*list
, insn
) {
350 if (!insn_compare(last
, insn
))
351 insn
= try_to_cse(ep
, last
, insn
);
354 } END_FOR_EACH_PTR(insn
);
356 free_ptr_list((struct ptr_list
**)list
);
360 if (repeat_phase
& REPEAT_SYMBOL_CLEANUP
)
363 if (repeat_phase
& REPEAT_CSE
)