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
];
23 #define hashval(x) ((unsigned long)(x))
25 int repeat_phase
, merge_phi_sources
;
27 static int phi_compare(pseudo_t phi1
, pseudo_t phi2
)
29 const struct instruction
*def1
= phi1
->def
;
30 const struct instruction
*def2
= phi2
->def
;
32 if (def1
->src1
!= def2
->src1
)
33 return def1
->src1
< def2
->src1
? -1 : 1;
34 if (def1
->bb
!= def2
->bb
)
35 return def1
->bb
< def2
->bb
? -1 : 1;
40 static void clean_up_one_instruction(struct basic_block
*bb
, struct instruction
*insn
)
46 assert(insn
->bb
== bb
);
47 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_MUL
: case OP_DIV
:
57 case OP_MOD
: case OP_SHL
:
59 case OP_AND
: case OP_OR
:
62 case OP_XOR
: case OP_AND_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
);
75 case OP_NOT
: case OP_NEG
:
76 hash
+= hashval(insn
->src1
);
80 hash
+= hashval(insn
->val
);
81 hash
+= hashval(insn
->symbol
);
87 FOR_EACH_PTR(insn
->phi_list
, phi
) {
88 struct instruction
*def
;
92 hash
+= hashval(def
->src1
);
93 hash
+= hashval(def
->bb
);
94 } END_FOR_EACH_PTR(phi
);
99 hash
+= hashval(insn
->src1
);
100 hash
+= hashval(insn
->bb
);
105 * Nothing to do, don't even bother hashing them,
106 * we're not going to try to CSE them
111 hash
&= INSN_HASH_SIZE
-1;
112 add_instruction(insn_hash_table
+ hash
, insn
);
115 static void clean_up_insns(struct entrypoint
*ep
)
117 struct basic_block
*bb
;
119 FOR_EACH_PTR(ep
->bbs
, bb
) {
120 struct instruction
*insn
;
121 FOR_EACH_PTR(bb
->insns
, insn
) {
122 clean_up_one_instruction(bb
, insn
);
123 } END_FOR_EACH_PTR(insn
);
124 } END_FOR_EACH_PTR(bb
);
127 extern void show_instruction(struct instruction
*);
129 /* Compare two (sorted) phi-lists */
130 static int phi_list_compare(struct pseudo_list
*l1
, struct pseudo_list
*l2
)
134 PREPARE_PTR_LIST(l1
, phi1
);
135 PREPARE_PTR_LIST(l2
, phi2
);
145 return phi2
? -1 : 0;
148 cmp
= phi_compare(phi1
, phi2
);
154 /* Not reached, but we need to make the nesting come out right */
155 FINISH_PTR_LIST(phi2
);
156 FINISH_PTR_LIST(phi1
);
159 static int insn_compare(const void *_i1
, const void *_i2
)
161 const struct instruction
*i1
= _i1
;
162 const struct instruction
*i2
= _i2
;
164 if (i1
->opcode
!= i2
->opcode
)
165 return i1
->opcode
< i2
->opcode
? -1 : 1;
167 switch (i1
->opcode
) {
169 if (i1
->src3
!= i2
->src3
)
170 return i1
->src3
< i2
->src3
? -1 : 1;
171 /* Fall-through to binops */
173 /* Binary arithmetic */
174 case OP_ADD
: case OP_SUB
:
175 case OP_MUL
: case OP_DIV
:
176 case OP_MOD
: case OP_SHL
:
178 case OP_AND
: case OP_OR
:
181 case OP_XOR
: case OP_AND_BOOL
:
184 /* Binary comparison */
185 case OP_SET_EQ
: case OP_SET_NE
:
186 case OP_SET_LE
: case OP_SET_GE
:
187 case OP_SET_LT
: case OP_SET_GT
:
188 case OP_SET_B
: case OP_SET_A
:
189 case OP_SET_BE
: case OP_SET_AE
:
190 if (i1
->src2
!= i2
->src2
)
191 return i1
->src2
< i2
->src2
? -1 : 1;
192 /* Fall-through to unops */
195 case OP_NOT
: case OP_NEG
:
196 if (i1
->src1
!= i2
->src1
)
197 return i1
->src1
< i2
->src1
? -1 : 1;
201 if (i1
->val
!= i2
->val
)
202 return i1
->val
< i2
->val
? -1 : 1;
203 if (i1
->symbol
!= i2
->symbol
)
204 return i1
->symbol
< i2
->symbol
? -1 : 1;
209 return phi_list_compare(i1
->phi_list
, i2
->phi_list
);
212 if (i1
->src1
!= i2
->src1
)
213 return i1
->src1
< i2
->src1
? -1 : 1;
214 if (i1
->bb
!= i2
->bb
)
215 return i1
->bb
< i2
->bb
? -1 : 1;
216 if (!merge_phi_sources
) {
218 return i1
< i2
? -1 : 1;
223 warning(i1
->bb
->pos
, "bad instruction on hash chain");
225 if (i1
->size
!= i2
->size
)
226 return i1
->size
< i2
->size
? -1 : 1;
230 static void sort_instruction_list(struct instruction_list
**list
)
232 sort_list((struct ptr_list
**)list
, insn_compare
);
235 static struct instruction
* cse_one_instruction(struct instruction
*insn
, struct instruction
*def
)
237 convert_instruction_target(insn
, def
->target
);
238 insn
->opcode
= OP_NOP
;
240 repeat_phase
|= REPEAT_CSE
;
245 * Does "bb1" dominate "bb2"?
247 static int bb_dominates(struct entrypoint
*ep
, struct basic_block
*bb1
, struct basic_block
*bb2
, unsigned long generation
)
249 struct basic_block
*parent
;
251 /* Nothing dominates the entrypoint.. */
252 if (bb2
== ep
->entry
)
254 FOR_EACH_PTR(bb2
->parents
, parent
) {
257 if (parent
->generation
== generation
)
259 parent
->generation
= generation
;
260 if (!bb_dominates(ep
, bb1
, parent
, generation
))
262 } END_FOR_EACH_PTR(parent
);
266 static struct basic_block
*trivial_common_parent(struct basic_block
*bb1
, struct basic_block
*bb2
)
268 struct basic_block
*parent
;
270 if (bb_list_size(bb1
->parents
) != 1)
272 parent
= first_basic_block(bb1
->parents
);
273 if (bb_list_size(bb2
->parents
) != 1)
275 if (first_basic_block(bb2
->parents
) != parent
)
280 static inline void remove_instruction(struct instruction_list
**list
, struct instruction
*insn
, int count
)
282 delete_ptr_list_entry((struct ptr_list
**)list
, insn
, count
);
285 static void add_instruction_to_end(struct instruction
*insn
, struct basic_block
*bb
)
287 struct instruction
*br
= delete_last_instruction(&bb
->insns
);
289 add_instruction(&bb
->insns
, insn
);
290 add_instruction(&bb
->insns
, br
);
293 static struct instruction
* try_to_cse(struct entrypoint
*ep
, struct instruction
*i1
, struct instruction
*i2
)
295 struct basic_block
*b1
, *b2
, *common
;
298 * Ok, i1 and i2 are the same instruction, modulo "target".
299 * We should now see if we can combine them.
305 * Currently we only handle the uninteresting degenerate case where
306 * the CSE is inside one basic-block.
309 struct instruction
*insn
;
310 FOR_EACH_PTR(b1
->insns
, insn
) {
312 return cse_one_instruction(i2
, i1
);
314 return cse_one_instruction(i1
, i2
);
315 } END_FOR_EACH_PTR(insn
);
316 warning(b1
->pos
, "Whaa? unable to find CSE instructions");
319 if (bb_dominates(ep
, b1
, b2
, ++bb_generation
))
320 return cse_one_instruction(i2
, i1
);
322 if (bb_dominates(ep
, b2
, b1
, ++bb_generation
))
323 return cse_one_instruction(i1
, i2
);
325 /* No direct dominance - but we could try to find a common ancestor.. */
326 common
= trivial_common_parent(b1
, b2
);
328 i1
= cse_one_instruction(i2
, i1
);
329 remove_instruction(&b1
->insns
, i1
, 1);
330 add_instruction_to_end(i1
, common
);
336 void cleanup_and_cse(struct entrypoint
*ep
)
344 for (i
= 0; i
< INSN_HASH_SIZE
; i
++) {
345 struct instruction_list
**list
= insn_hash_table
+ i
;
347 if (instruction_list_size(*list
) > 1) {
348 struct instruction
*insn
, *last
;
350 sort_instruction_list(list
);
353 FOR_EACH_PTR(*list
, insn
) {
357 if (!insn_compare(last
, insn
))
358 insn
= try_to_cse(ep
, last
, insn
);
361 } END_FOR_EACH_PTR(insn
);
363 free_ptr_list((struct ptr_list
**)list
);
367 if (repeat_phase
& REPEAT_SYMBOL_CLEANUP
)
370 if (repeat_phase
& REPEAT_CSE
)