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 (source
!= phi_parent(bb2
, p2
))
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
)) {
152 delete_ptr_list_entry((struct ptr_list
**)&p
->users
, usep
, 1);
154 kill_instruction(p
->def
);
158 void kill_use(pseudo_t
*usep
)
163 remove_usage(p
, usep
);
167 void kill_instruction(struct instruction
*insn
)
169 if (!insn
|| !insn
->bb
)
172 switch (insn
->opcode
) {
173 case OP_BINARY
... OP_BINCMP_END
:
175 kill_use(&insn
->src1
);
176 kill_use(&insn
->src2
);
177 repeat_phase
|= REPEAT_CSE
;
180 case OP_NOT
: case OP_NEG
:
182 kill_use(&insn
->src1
);
183 repeat_phase
|= REPEAT_CSE
;
188 repeat_phase
|= REPEAT_CSE
;
193 repeat_phase
|= REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
199 * Kill trivially dead instructions
201 static int dead_insn(struct instruction
*insn
, pseudo_t
*src1
, pseudo_t
*src2
, pseudo_t
*src3
)
204 FOR_EACH_PTR(insn
->target
->users
, usep
) {
207 } END_FOR_EACH_PTR(usep
);
216 static inline int constant(pseudo_t pseudo
)
218 return pseudo
->type
== PSEUDO_VAL
;
221 static int replace_with_pseudo(struct instruction
*insn
, pseudo_t pseudo
)
223 convert_instruction_target(insn
, pseudo
);
228 static int simplify_constant_rightside(struct instruction
*insn
)
230 long long value
= insn
->src2
->value
;
232 switch (insn
->opcode
) {
235 insn
->opcode
= OP_ADD
;
236 insn
->src2
= value_pseudo(-value
);
241 case OP_OR
: case OP_XOR
:
242 case OP_SHL
: case OP_SHR
:
244 return replace_with_pseudo(insn
, insn
->src1
);
247 case OP_AND
: case OP_MUL
:
249 return replace_with_pseudo(insn
, insn
->src2
);
255 static int simplify_constant_leftside(struct instruction
*insn
)
257 long long value
= insn
->src1
->value
;
259 switch (insn
->opcode
) {
260 case OP_ADD
: case OP_OR
: case OP_XOR
:
262 return replace_with_pseudo(insn
, insn
->src2
);
265 case OP_SHL
: case OP_SHR
:
266 case OP_AND
: case OP_MUL
:
268 return replace_with_pseudo(insn
, insn
->src1
);
274 static int simplify_constant_binop(struct instruction
*insn
)
276 /* FIXME! Verify signs and sizes!! */
277 long long left
= insn
->src1
->value
;
278 long long right
= insn
->src2
->value
;
281 switch (insn
->opcode
) {
289 /* FIXME! Check sign! */
295 /* FIXME! Check sign! */
301 /* FIXME! Check sign! */
308 /* FIXME! Check sign! */
328 /* Binary comparison */
336 /* FIXME! Check sign! */
340 /* FIXME! Check sign! */
344 /* FIXME! Check sign! */
348 /* FIXME! Check sign! */
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
;
364 /* FIXME! Check sign! */
365 res
= (unsigned long long) left
>= (unsigned long long) right
;
370 mask
= 1ULL << (insn
->size
-1);
371 res
&= mask
| (mask
-1);
373 /* FIXME!! Sign??? */
374 replace_with_pseudo(insn
, value_pseudo(res
));
378 static int simplify_binop(struct instruction
*insn
)
380 if (dead_insn(insn
, &insn
->src1
, &insn
->src2
, NULL
))
382 if (constant(insn
->src1
)) {
383 if (constant(insn
->src2
))
384 return simplify_constant_binop(insn
);
385 return simplify_constant_leftside(insn
);
387 if (constant(insn
->src2
))
388 return simplify_constant_rightside(insn
);
392 static void switch_pseudo(pseudo_t
*pp1
, pseudo_t
*pp2
)
394 pseudo_t p1
= *pp1
, p2
= *pp2
;
398 remove_usage(p1
, pp1
);
399 remove_usage(p2
, pp2
);
402 static int canonical_order(pseudo_t p1
, pseudo_t p2
)
404 /* symbol/constants on the right */
405 if (p1
->type
== PSEUDO_VAL
)
406 return p2
->type
== PSEUDO_VAL
;
408 if (p1
->type
== PSEUDO_SYM
)
409 return p2
->type
== PSEUDO_SYM
|| p2
->type
== PSEUDO_VAL
;
414 static int simplify_commutative_binop(struct instruction
*insn
)
416 if (!canonical_order(insn
->src1
, insn
->src2
)) {
417 switch_pseudo(&insn
->src1
, &insn
->src2
);
423 static inline int simple_pseudo(pseudo_t pseudo
)
425 return pseudo
->type
== PSEUDO_VAL
|| pseudo
->type
== PSEUDO_SYM
;
428 static int simplify_associative_binop(struct instruction
*insn
)
430 struct instruction
*def
;
431 pseudo_t pseudo
= insn
->src1
;
433 if (!simple_pseudo(insn
->src2
))
435 if (pseudo
->type
!= PSEUDO_REG
)
440 if (def
->opcode
!= insn
->opcode
)
442 if (!simple_pseudo(def
->src2
))
444 if (ptr_list_size((struct ptr_list
*)def
->target
->users
) != 1)
446 switch_pseudo(&def
->src1
, &insn
->src2
);
450 static int simplify_constant_unop(struct instruction
*insn
)
452 long long val
= insn
->src1
->value
;
455 switch (insn
->opcode
) {
465 mask
= 1ULL << (insn
->size
-1);
466 res
&= mask
| (mask
-1);
468 replace_with_pseudo(insn
, value_pseudo(res
));
472 static int simplify_unop(struct instruction
*insn
)
474 if (dead_insn(insn
, &insn
->src1
, NULL
, NULL
))
476 if (constant(insn
->src1
))
477 return simplify_constant_unop(insn
);
481 static int simplify_one_memop(struct instruction
*insn
, pseudo_t orig
)
483 pseudo_t addr
= insn
->src
;
486 if (addr
->type
== PSEUDO_REG
) {
487 struct instruction
*def
= addr
->def
;
488 if (def
->opcode
== OP_SYMADDR
&& def
->src
) {
489 kill_use(&insn
->src
);
490 use_pseudo(def
->src
, &insn
->src
);
491 return REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
493 if (def
->opcode
== OP_ADD
) {
513 warning(insn
->bb
->pos
, "crazy programmer");
515 insn
->offset
+= off
->value
;
516 use_pseudo(new, &insn
->src
);
517 remove_usage(addr
, &insn
->src
);
518 return REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
522 * We walk the whole chain of adds/subs backwards. That's not
523 * only more efficient, but it allows us to find looops.
525 static int simplify_memop(struct instruction
*insn
)
528 pseudo_t orig
= insn
->src
;
531 one
= simplify_one_memop(insn
, orig
);
537 static int simplify_cast(struct instruction
*insn
)
541 if (dead_insn(insn
, &insn
->src
, NULL
, NULL
))
543 if (0 && insn
->opcode
== OP_PTRCAST
)
545 orig_size
= insn
->orig_type
? insn
->orig_type
->bit_size
: 0;
548 if (insn
->size
!= orig_size
)
550 return replace_with_pseudo(insn
, insn
->src
);
553 static int simplify_select(struct instruction
*insn
)
555 pseudo_t cond
, src1
, src2
;
557 if (dead_insn(insn
, &insn
->src1
, &insn
->src2
, &insn
->src3
))
563 if (constant(cond
) || src1
== src2
) {
564 pseudo_t
*kill
, take
;
565 kill_use(&insn
->src1
);
566 take
= cond
->value
? src1
: src2
;
567 kill
= cond
->value
? &insn
->src3
: &insn
->src2
;
569 replace_with_pseudo(insn
, take
);
572 if (constant(src1
) && constant(src2
)) {
573 long long val1
= src1
->value
;
574 long long val2
= src2
->value
;
576 /* The pair 0/1 is special - replace with SETNE/SETEQ */
577 if ((val1
| val2
) == 1) {
578 int opcode
= OP_SET_EQ
;
583 insn
->opcode
= opcode
;
584 /* insn->src1 is already cond */
585 insn
->src2
= src1
; /* Zero */
593 * Simplify "set_ne/eq $0 + br"
595 static int simplify_cond_branch(struct instruction
*br
, pseudo_t cond
, struct instruction
*def
, pseudo_t
*pp
)
597 use_pseudo(*pp
, &br
->cond
);
598 remove_usage(cond
, &br
->cond
);
599 if (def
->opcode
== OP_SET_EQ
) {
600 struct basic_block
*true = br
->bb_true
;
601 struct basic_block
*false = br
->bb_false
;
608 static int simplify_branch(struct instruction
*insn
)
610 pseudo_t cond
= insn
->cond
;
615 /* Constant conditional */
616 if (constant(cond
)) {
617 insert_branch(insn
->bb
, insn
, cond
->value
? insn
->bb_true
: insn
->bb_false
);
622 if (insn
->bb_true
== insn
->bb_false
) {
623 struct basic_block
*bb
= insn
->bb
;
624 struct basic_block
*target
= insn
->bb_false
;
625 remove_bb_from_list(&target
->parents
, bb
, 1);
626 remove_bb_from_list(&bb
->children
, target
, 1);
627 insn
->bb_false
= NULL
;
628 kill_use(&insn
->cond
);
633 /* Conditional on a SETNE $0 or SETEQ $0 */
634 if (cond
->type
== PSEUDO_REG
) {
635 struct instruction
*def
= cond
->def
;
637 if (def
->opcode
== OP_SET_NE
|| def
->opcode
== OP_SET_EQ
) {
638 if (constant(def
->src1
) && !def
->src1
->value
)
639 return simplify_cond_branch(insn
, cond
, def
, &def
->src2
);
640 if (constant(def
->src2
) && !def
->src2
->value
)
641 return simplify_cond_branch(insn
, cond
, def
, &def
->src1
);
643 if (def
->opcode
== OP_SEL
) {
644 if (constant(def
->src2
) && constant(def
->src3
)) {
645 long long val1
= def
->src2
->value
;
646 long long val2
= def
->src3
->value
;
647 if (!val1
&& !val2
) {
648 insert_branch(insn
->bb
, insn
, insn
->bb_false
);
652 insert_branch(insn
->bb
, insn
, insn
->bb_true
);
656 struct basic_block
*true = insn
->bb_true
;
657 struct basic_block
*false = insn
->bb_false
;
658 insn
->bb_false
= true;
659 insn
->bb_true
= false;
661 use_pseudo(def
->src1
, &insn
->cond
);
662 remove_usage(cond
, &insn
->cond
);
666 if (def
->opcode
== OP_CAST
) {
667 int orig_size
= def
->orig_type
? def
->orig_type
->bit_size
: 0;
668 if (def
->size
> orig_size
) {
669 use_pseudo(def
->src
, &insn
->cond
);
670 remove_usage(cond
, &insn
->cond
);
678 static int simplify_switch(struct instruction
*insn
)
680 pseudo_t cond
= insn
->cond
;
682 struct multijmp
*jmp
;
686 val
= insn
->cond
->value
;
688 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
690 if (jmp
->begin
> jmp
->end
)
692 if (val
>= jmp
->begin
&& val
<= jmp
->end
)
694 } END_FOR_EACH_PTR(jmp
);
695 warning(insn
->bb
->pos
, "Impossible case statement");
699 insert_branch(insn
->bb
, insn
, jmp
->target
);
703 int simplify_instruction(struct instruction
*insn
)
707 switch (insn
->opcode
) {
709 case OP_AND
: case OP_OR
: case OP_XOR
:
710 case OP_AND_BOOL
: case OP_OR_BOOL
:
711 if (simplify_binop(insn
))
713 if (simplify_commutative_binop(insn
))
715 return simplify_associative_binop(insn
);
718 * Multiplication isn't associative in 2's complement,
719 * but we could do it if signed.
722 case OP_SET_EQ
: case OP_SET_NE
:
723 if (simplify_binop(insn
))
725 return simplify_commutative_binop(insn
);
727 case OP_SUB
: case OP_DIV
: case OP_MOD
:
728 case OP_SHL
: case OP_SHR
:
729 case OP_SET_LE
: case OP_SET_GE
:
730 case OP_SET_LT
: case OP_SET_GT
:
731 case OP_SET_B
: case OP_SET_A
:
732 case OP_SET_BE
: case OP_SET_AE
:
733 return simplify_binop(insn
);
735 case OP_NOT
: case OP_NEG
:
736 return simplify_unop(insn
);
737 case OP_LOAD
: case OP_STORE
:
738 return simplify_memop(insn
);
740 if (dead_insn(insn
, NULL
, NULL
, NULL
))
741 return REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
742 return replace_with_pseudo(insn
, insn
->symbol
);
745 return simplify_cast(insn
);
747 if (dead_insn(insn
, NULL
, NULL
, NULL
)) {
751 return clean_up_phi(insn
);
753 if (dead_insn(insn
, &insn
->phi_src
, NULL
, NULL
))
757 return simplify_select(insn
);
759 return simplify_branch(insn
);
761 return simplify_switch(insn
);