2 * Simplify - do instruction simplification before CSE
4 * Copyright (C) 2004 Linus Torvalds
10 #include "expression.h"
11 #include "linearize.h"
14 /* Find the trivial parent for a phi-source */
15 static struct basic_block
*phi_parent(struct basic_block
*source
, pseudo_t pseudo
)
17 /* Can't go upwards if the pseudo is defined in the bb it came from.. */
18 if (pseudo
->type
== PSEUDO_REG
) {
19 struct instruction
*def
= pseudo
->def
;
20 if (def
->bb
== source
)
23 if (bb_list_size(source
->children
) != 1 || bb_list_size(source
->parents
) != 1)
25 return first_basic_block(source
->parents
);
28 static void clear_phi(struct instruction
*insn
)
33 FOR_EACH_PTR(insn
->phi_list
, phi
) {
34 *THIS_ADDRESS(phi
) = VOID
;
35 } END_FOR_EACH_PTR(phi
);
38 static int if_convert_phi(struct instruction
*insn
)
41 struct basic_block
*parents
[3];
42 struct basic_block
*bb
, *bb1
, *bb2
, *source
;
43 struct instruction
*br
;
47 if (linearize_ptr_list((struct ptr_list
*)insn
->phi_list
, (void **)array
, 3) != 2)
49 if (linearize_ptr_list((struct ptr_list
*)bb
->parents
, (void **)parents
, 3) != 2)
51 p1
= array
[0]->def
->src1
;
52 bb1
= array
[0]->def
->bb
;
53 p2
= array
[1]->def
->src1
;
54 bb2
= array
[1]->def
->bb
;
56 /* Only try the simple "direct parents" case */
57 if ((bb1
!= parents
[0] || bb2
!= parents
[1]) &&
58 (bb1
!= parents
[1] || bb2
!= parents
[0]))
62 * See if we can find a common source for this..
64 source
= phi_parent(bb1
, p1
);
65 if (phi_parent(bb2
, p2
) != source
)
69 * Cool. We now know that 'source' is the exclusive
70 * parent of both phi-nodes, so the exit at the
71 * end of it fully determines which one it is, and
72 * we can turn it into a select.
74 * HOWEVER, right now we only handle regular
75 * conditional branches. No multijumps or computed
76 * stuff. Verify that here.
78 br
= last_instruction(source
->insns
);
79 if (!br
|| br
->opcode
!= OP_BR
)
86 * We're in business. Match up true/false with p1/p2.
88 if (br
->bb_true
== bb2
|| br
->bb_false
== bb1
) {
95 * Ok, we can now replace that last
102 * select pseudo, p1, p2
105 * and remove the phi-node. If it then
106 * turns out that 'a' or 'b' is entirely
107 * empty (common case), and now no longer
108 * a phi-source, we'll be able to simplify
109 * the conditional branch too.
111 insert_select(source
, br
, insn
, p1
, p2
);
116 static int clean_up_phi(struct instruction
*insn
)
119 struct instruction
*last
;
124 FOR_EACH_PTR(insn
->phi_list
, phi
) {
125 struct instruction
*def
;
129 if (def
->src1
== VOID
|| !def
->bb
)
132 if (last
->src1
!= def
->src1
)
137 } END_FOR_EACH_PTR(phi
);
140 pseudo_t pseudo
= last
? last
->src1
: VOID
;
141 convert_instruction_target(insn
, pseudo
);
146 return if_convert_phi(insn
);
149 static inline void remove_usage(pseudo_t p
, pseudo_t
*usep
)
151 if (has_use_list(p
)) {
153 deleted
= delete_ptr_list_entry((struct ptr_list
**)&p
->users
, usep
);
154 assert(deleted
== 1);
156 kill_instruction(p
->def
);
160 void kill_use(pseudo_t
*usep
)
165 remove_usage(p
, usep
);
169 void kill_instruction(struct instruction
*insn
)
171 if (!insn
|| !insn
->bb
)
174 switch (insn
->opcode
) {
175 case OP_BINARY
... OP_BINCMP_END
:
177 kill_use(&insn
->src1
);
178 kill_use(&insn
->src2
);
179 repeat_phase
|= REPEAT_CSE
;
182 case OP_NOT
: case OP_NEG
:
184 kill_use(&insn
->src1
);
185 repeat_phase
|= REPEAT_CSE
;
190 repeat_phase
|= REPEAT_CSE
;
195 repeat_phase
|= REPEAT_CSE
;
197 repeat_phase
|= REPEAT_SYMBOL_CLEANUP
;
203 * Kill trivially dead instructions
205 static int dead_insn(struct instruction
*insn
, pseudo_t
*src1
, pseudo_t
*src2
)
208 FOR_EACH_PTR(insn
->target
->users
, usep
) {
211 } END_FOR_EACH_PTR(usep
);
219 static inline int constant(pseudo_t pseudo
)
221 return pseudo
->type
== PSEUDO_VAL
;
224 static int replace_with_pseudo(struct instruction
*insn
, pseudo_t pseudo
)
226 convert_instruction_target(insn
, pseudo
);
231 static int simplify_constant_rightside(struct instruction
*insn
)
233 long long value
= insn
->src2
->value
;
235 switch (insn
->opcode
) {
236 case OP_ADD
: case OP_SUB
:
237 case OP_OR
: case OP_XOR
:
238 case OP_SHL
: case OP_SHR
:
240 return replace_with_pseudo(insn
, insn
->src1
);
243 case OP_AND
: case OP_MUL
:
245 return replace_with_pseudo(insn
, insn
->src2
);
251 static int simplify_constant_leftside(struct instruction
*insn
)
253 long long value
= insn
->src1
->value
;
255 switch (insn
->opcode
) {
256 case OP_ADD
: case OP_OR
: case OP_XOR
:
258 return replace_with_pseudo(insn
, insn
->src2
);
261 case OP_SHL
: case OP_SHR
:
262 case OP_AND
: case OP_MUL
:
264 return replace_with_pseudo(insn
, insn
->src1
);
270 static int simplify_constant_binop(struct instruction
*insn
)
272 /* FIXME! Verify signs and sizes!! */
273 long long left
= insn
->src1
->value
;
274 long long right
= insn
->src2
->value
;
277 switch (insn
->opcode
) {
285 /* FIXME! Check sign! */
291 /* FIXME! Check sign! */
297 /* FIXME! Check sign! */
304 /* FIXME! Check sign! */
324 /* Binary comparison */
332 /* FIXME! Check sign! */
336 /* FIXME! Check sign! */
340 /* FIXME! Check sign! */
344 /* FIXME! Check sign! */
348 /* FIXME! Check sign! */
349 res
= (unsigned long long) left
< (unsigned long long) right
;
352 /* FIXME! Check sign! */
353 res
= (unsigned long long) left
> (unsigned long long) right
;
356 /* FIXME! Check sign! */
357 res
= (unsigned long long) left
<= (unsigned long long) right
;
360 /* FIXME! Check sign! */
361 res
= (unsigned long long) left
>= (unsigned long long) right
;
366 mask
= 1ULL << (insn
->type
->bit_size
-1);
367 res
&= mask
| (mask
-1);
369 /* FIXME!! Sign??? */
370 replace_with_pseudo(insn
, value_pseudo(res
));
374 static int simplify_binop(struct instruction
*insn
)
376 if (dead_insn(insn
, &insn
->src1
, &insn
->src2
))
378 if (constant(insn
->src1
)) {
379 if (constant(insn
->src2
))
380 return simplify_constant_binop(insn
);
381 return simplify_constant_leftside(insn
);
383 if (constant(insn
->src2
))
384 return simplify_constant_rightside(insn
);
388 static int simplify_constant_unop(struct instruction
*insn
)
393 static int simplify_unop(struct instruction
*insn
)
395 if (dead_insn(insn
, &insn
->src1
, NULL
))
397 if (constant(insn
->src1
))
398 return simplify_constant_unop(insn
);
402 static int simplify_memop(struct instruction
*insn
)
404 pseudo_t addr
= insn
->src
;
405 if (addr
->type
== PSEUDO_REG
) {
406 struct instruction
*def
= addr
->def
;
407 if (def
->opcode
== OP_SETVAL
&& def
->src
) {
408 kill_use(&insn
->src
);
409 use_pseudo(def
->src
, &insn
->src
);
410 return REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
416 int simplify_instruction(struct instruction
*insn
)
419 static struct instruction
*last_setcc
;
420 struct instruction
*setcc
= last_setcc
;
426 switch (insn
->opcode
) {
427 case OP_BINARY
... OP_BINCMP_END
:
428 return simplify_binop(insn
);
430 case OP_NOT
: case OP_NEG
:
431 return simplify_unop(insn
);
432 case OP_LOAD
: case OP_STORE
:
433 return simplify_memop(insn
);
435 if (dead_insn(insn
, NULL
, NULL
))
436 return REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
439 if (dead_insn(insn
, NULL
, NULL
)) {
443 return clean_up_phi(insn
);
445 if (dead_insn(insn
, &insn
->src1
, NULL
))
452 assert(setcc
&& setcc
->bb
);
453 if (dead_insn(insn
, &insn
->src1
, &insn
->src2
)) {
458 if (!constant(cond
) && insn
->src1
!= insn
->src2
)
461 kill_use(&setcc
->cond
);
462 replace_with_pseudo(insn
, cond
->value
? insn
->src1
: insn
->src2
);
466 if (!cond
|| !constant(cond
))
468 insert_branch(insn
->bb
, insn
, cond
->value
? insn
->bb_true
: insn
->bb_false
);