2 * Simplify - do instruction simplification before CSE
4 * Copyright (C) 2004 Linus Torvalds
10 #include "expression.h"
11 #include "linearize.h"
15 /* Find the trivial parent for a phi-source */
16 static struct basic_block
*phi_parent(struct basic_block
*source
, pseudo_t pseudo
)
18 /* Can't go upwards if the pseudo is defined in the bb it came from.. */
19 if (pseudo
->type
== PSEUDO_REG
) {
20 struct instruction
*def
= pseudo
->def
;
21 if (def
->bb
== source
)
24 if (bb_list_size(source
->children
) != 1 || bb_list_size(source
->parents
) != 1)
26 return first_basic_block(source
->parents
);
29 static void clear_phi(struct instruction
*insn
)
34 FOR_EACH_PTR(insn
->phi_list
, phi
) {
35 *THIS_ADDRESS(phi
) = VOID
;
36 } END_FOR_EACH_PTR(phi
);
39 static int if_convert_phi(struct instruction
*insn
)
42 struct basic_block
*parents
[3];
43 struct basic_block
*bb
, *bb1
, *bb2
, *source
;
44 struct instruction
*br
;
48 if (linearize_ptr_list((struct ptr_list
*)insn
->phi_list
, (void **)array
, 3) != 2)
50 if (linearize_ptr_list((struct ptr_list
*)bb
->parents
, (void **)parents
, 3) != 2)
52 p1
= array
[0]->def
->src1
;
53 bb1
= array
[0]->def
->bb
;
54 p2
= array
[1]->def
->src1
;
55 bb2
= array
[1]->def
->bb
;
57 /* Only try the simple "direct parents" case */
58 if ((bb1
!= parents
[0] || bb2
!= parents
[1]) &&
59 (bb1
!= parents
[1] || bb2
!= parents
[0]))
63 * See if we can find a common source for this..
65 source
= phi_parent(bb1
, p1
);
66 if (source
!= phi_parent(bb2
, p2
))
70 * Cool. We now know that 'source' is the exclusive
71 * parent of both phi-nodes, so the exit at the
72 * end of it fully determines which one it is, and
73 * we can turn it into a select.
75 * HOWEVER, right now we only handle regular
76 * conditional branches. No multijumps or computed
77 * stuff. Verify that here.
79 br
= last_instruction(source
->insns
);
80 if (!br
|| br
->opcode
!= OP_BR
)
87 * We're in business. Match up true/false with p1/p2.
89 if (br
->bb_true
== bb2
|| br
->bb_false
== bb1
) {
96 * OK, we can now replace that last
103 * select pseudo, p1, p2
106 * and remove the phi-node. If it then
107 * turns out that 'a' or 'b' is entirely
108 * empty (common case), and now no longer
109 * a phi-source, we'll be able to simplify
110 * the conditional branch too.
112 insert_select(source
, br
, insn
, p1
, p2
);
117 static int clean_up_phi(struct instruction
*insn
)
120 struct instruction
*last
;
125 FOR_EACH_PTR(insn
->phi_list
, phi
) {
126 struct instruction
*def
;
130 if (def
->src1
== VOID
|| !def
->bb
)
133 if (last
->src1
!= def
->src1
)
138 } END_FOR_EACH_PTR(phi
);
141 pseudo_t pseudo
= last
? last
->src1
: VOID
;
142 convert_instruction_target(insn
, pseudo
);
147 return if_convert_phi(insn
);
150 static int delete_pseudo_user_list_entry(struct pseudo_user_list
**list
, pseudo_t
*entry
, int count
)
152 struct pseudo_user
*pu
;
154 FOR_EACH_PTR(*list
, pu
) {
155 if (pu
->userp
== entry
) {
156 DELETE_CURRENT_PTR(pu
);
160 } END_FOR_EACH_PTR(pu
);
163 pack_ptr_list((struct ptr_list
**)list
);
167 static inline void remove_usage(pseudo_t p
, pseudo_t
*usep
)
169 if (has_use_list(p
)) {
170 delete_pseudo_user_list_entry(&p
->users
, usep
, 1);
172 kill_instruction(p
->def
);
176 void kill_use(pseudo_t
*usep
)
181 remove_usage(p
, usep
);
185 void kill_instruction(struct instruction
*insn
)
187 if (!insn
|| !insn
->bb
)
190 switch (insn
->opcode
) {
191 case OP_BINARY
... OP_BINCMP_END
:
193 kill_use(&insn
->src1
);
194 kill_use(&insn
->src2
);
195 repeat_phase
|= REPEAT_CSE
;
198 case OP_NOT
: case OP_NEG
:
200 kill_use(&insn
->src1
);
201 repeat_phase
|= REPEAT_CSE
;
206 repeat_phase
|= REPEAT_CSE
;
211 repeat_phase
|= REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
216 repeat_phase
|= REPEAT_CSE
;
217 kill_use(&insn
->src1
);
218 kill_use(&insn
->src2
);
219 kill_use(&insn
->src3
);
223 repeat_phase
|= REPEAT_CSE
;
225 kill_use(&insn
->cond
);
231 * Kill trivially dead instructions
233 static int dead_insn(struct instruction
*insn
, pseudo_t
*src1
, pseudo_t
*src2
, pseudo_t
*src3
)
235 struct pseudo_user
*pu
;
236 FOR_EACH_PTR(insn
->target
->users
, pu
) {
237 if (*pu
->userp
!= VOID
)
239 } END_FOR_EACH_PTR(pu
);
248 static inline int constant(pseudo_t pseudo
)
250 return pseudo
->type
== PSEUDO_VAL
;
253 static int replace_with_pseudo(struct instruction
*insn
, pseudo_t pseudo
)
255 convert_instruction_target(insn
, pseudo
);
260 static unsigned int value_size(long long value
)
275 * Try to determine the maximum size of bits in a pseudo.
277 * Right now this only follow casts and constant values, but we
278 * could look at things like logical 'and' instructions etc.
280 static unsigned int operand_size(struct instruction
*insn
, pseudo_t pseudo
)
282 unsigned int size
= insn
->size
;
284 if (pseudo
->type
== PSEUDO_REG
) {
285 struct instruction
*src
= pseudo
->def
;
286 if (src
&& src
->opcode
== OP_CAST
&& src
->orig_type
) {
287 unsigned int orig_size
= src
->orig_type
->bit_size
;
288 if (orig_size
< size
)
292 if (pseudo
->type
== PSEUDO_VAL
) {
293 unsigned int orig_size
= value_size(pseudo
->value
);
294 if (orig_size
< size
)
300 static int simplify_asr(struct instruction
*insn
, pseudo_t pseudo
, long long value
)
302 unsigned int size
= operand_size(insn
, pseudo
);
305 warning(insn
->pos
, "right shift by bigger than source value");
306 return replace_with_pseudo(insn
, value_pseudo(0));
309 return replace_with_pseudo(insn
, pseudo
);
313 static int simplify_constant_rightside(struct instruction
*insn
)
315 long long value
= insn
->src2
->value
;
317 switch (insn
->opcode
) {
320 insn
->opcode
= OP_ADD
;
321 insn
->src2
= value_pseudo(-value
);
326 case OP_OR
: case OP_XOR
:
331 return replace_with_pseudo(insn
, insn
->src1
);
334 return simplify_asr(insn
, insn
->src1
, value
);
336 case OP_MULU
: case OP_MULS
:
339 return replace_with_pseudo(insn
, insn
->src1
);
343 return replace_with_pseudo(insn
, insn
->src2
);
349 static int simplify_constant_leftside(struct instruction
*insn
)
351 long long value
= insn
->src1
->value
;
353 switch (insn
->opcode
) {
354 case OP_ADD
: case OP_OR
: case OP_XOR
:
356 return replace_with_pseudo(insn
, insn
->src2
);
360 case OP_LSR
: case OP_ASR
:
362 case OP_MULU
: case OP_MULS
:
364 return replace_with_pseudo(insn
, insn
->src1
);
370 static int simplify_constant_binop(struct instruction
*insn
)
372 /* FIXME! Verify signs and sizes!! */
373 long long left
= insn
->src1
->value
;
374 long long right
= insn
->src2
->value
;
375 unsigned long long ul
, ur
;
376 long long res
, mask
, bits
;
378 mask
= 1ULL << (insn
->size
-1);
379 bits
= mask
| (mask
-1);
388 switch (insn
->opcode
) {
447 /* Binary comparison */
483 replace_with_pseudo(insn
, value_pseudo(res
));
487 static int simplify_binop(struct instruction
*insn
)
489 if (dead_insn(insn
, &insn
->src1
, &insn
->src2
, NULL
))
491 if (constant(insn
->src1
)) {
492 if (constant(insn
->src2
))
493 return simplify_constant_binop(insn
);
494 return simplify_constant_leftside(insn
);
496 if (constant(insn
->src2
))
497 return simplify_constant_rightside(insn
);
501 static void switch_pseudo(struct instruction
*insn1
, pseudo_t
*pp1
, struct instruction
*insn2
, pseudo_t
*pp2
)
503 pseudo_t p1
= *pp1
, p2
= *pp2
;
505 use_pseudo(insn1
, p2
, pp1
);
506 use_pseudo(insn2
, p1
, pp2
);
507 remove_usage(p1
, pp1
);
508 remove_usage(p2
, pp2
);
511 static int canonical_order(pseudo_t p1
, pseudo_t p2
)
513 /* symbol/constants on the right */
514 if (p1
->type
== PSEUDO_VAL
)
515 return p2
->type
== PSEUDO_VAL
;
517 if (p1
->type
== PSEUDO_SYM
)
518 return p2
->type
== PSEUDO_SYM
|| p2
->type
== PSEUDO_VAL
;
523 static int simplify_commutative_binop(struct instruction
*insn
)
525 if (!canonical_order(insn
->src1
, insn
->src2
)) {
526 switch_pseudo(insn
, &insn
->src1
, insn
, &insn
->src2
);
532 static inline int simple_pseudo(pseudo_t pseudo
)
534 return pseudo
->type
== PSEUDO_VAL
|| pseudo
->type
== PSEUDO_SYM
;
537 static int simplify_associative_binop(struct instruction
*insn
)
539 struct instruction
*def
;
540 pseudo_t pseudo
= insn
->src1
;
542 if (!simple_pseudo(insn
->src2
))
544 if (pseudo
->type
!= PSEUDO_REG
)
549 if (def
->opcode
!= insn
->opcode
)
551 if (!simple_pseudo(def
->src2
))
553 if (ptr_list_size((struct ptr_list
*)def
->target
->users
) != 1)
555 switch_pseudo(def
, &def
->src1
, insn
, &insn
->src2
);
559 static int simplify_constant_unop(struct instruction
*insn
)
561 long long val
= insn
->src1
->value
;
564 switch (insn
->opcode
) {
574 mask
= 1ULL << (insn
->size
-1);
575 res
&= mask
| (mask
-1);
577 replace_with_pseudo(insn
, value_pseudo(res
));
581 static int simplify_unop(struct instruction
*insn
)
583 if (dead_insn(insn
, &insn
->src1
, NULL
, NULL
))
585 if (constant(insn
->src1
))
586 return simplify_constant_unop(insn
);
590 static int simplify_one_memop(struct instruction
*insn
, pseudo_t orig
)
592 pseudo_t addr
= insn
->src
;
595 if (addr
->type
== PSEUDO_REG
) {
596 struct instruction
*def
= addr
->def
;
597 if (def
->opcode
== OP_SYMADDR
&& def
->src
) {
598 kill_use(&insn
->src
);
599 use_pseudo(insn
, def
->src
, &insn
->src
);
600 return REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
602 if (def
->opcode
== OP_ADD
) {
622 warning(insn
->pos
, "crazy programmer");
624 insn
->offset
+= off
->value
;
625 use_pseudo(insn
, new, &insn
->src
);
626 remove_usage(addr
, &insn
->src
);
627 return REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
631 * We walk the whole chain of adds/subs backwards. That's not
632 * only more efficient, but it allows us to find loops.
634 static int simplify_memop(struct instruction
*insn
)
637 pseudo_t orig
= insn
->src
;
640 one
= simplify_one_memop(insn
, orig
);
646 static long long get_cast_value(long long val
, int old_size
, int new_size
, int sign
)
650 if (sign
&& new_size
> old_size
) {
651 mask
= 1 << (old_size
-1);
653 val
|= ~(mask
| (mask
-1));
655 mask
= 1 << (new_size
-1);
656 return val
& (mask
| (mask
-1));
659 static int simplify_cast(struct instruction
*insn
)
661 struct symbol
*orig_type
;
665 if (dead_insn(insn
, &insn
->src
, NULL
, NULL
))
668 orig_type
= insn
->orig_type
;
672 /* Keep casts with pointer on either side (not only case of OP_PTRCAST) */
673 if (is_ptr_type(orig_type
) || is_ptr_type(insn
->type
))
676 orig_size
= orig_type
->bit_size
;
680 /* A cast of a constant? */
682 int sign
= orig_type
->ctype
.modifiers
& MOD_SIGNED
;
683 long long val
= get_cast_value(src
->value
, orig_size
, size
, sign
);
684 src
= value_pseudo(val
);
688 /* A cast of a "and" might be a no-op.. */
689 if (src
->type
== PSEUDO_REG
) {
690 struct instruction
*def
= src
->def
;
691 if (def
->opcode
== OP_AND
&& def
->size
>= size
) {
692 pseudo_t val
= def
->src2
;
693 if (val
->type
== PSEUDO_VAL
) {
694 unsigned long long value
= val
->value
;
695 if (!(value
>> (size
-1)))
701 if (size
== orig_size
) {
702 int op
= (orig_type
->ctype
.modifiers
& MOD_SIGNED
) ? OP_SCAST
: OP_CAST
;
703 if (insn
->opcode
== op
)
710 return replace_with_pseudo(insn
, src
);
713 static int simplify_select(struct instruction
*insn
)
715 pseudo_t cond
, src1
, src2
;
717 if (dead_insn(insn
, &insn
->src1
, &insn
->src2
, &insn
->src3
))
723 if (constant(cond
) || src1
== src2
) {
724 pseudo_t
*kill
, take
;
725 kill_use(&insn
->src1
);
726 take
= cond
->value
? src1
: src2
;
727 kill
= cond
->value
? &insn
->src3
: &insn
->src2
;
729 replace_with_pseudo(insn
, take
);
732 if (constant(src1
) && constant(src2
)) {
733 long long val1
= src1
->value
;
734 long long val2
= src2
->value
;
736 /* The pair 0/1 is special - replace with SETNE/SETEQ */
737 if ((val1
| val2
) == 1) {
738 int opcode
= OP_SET_EQ
;
743 insn
->opcode
= opcode
;
744 /* insn->src1 is already cond */
745 insn
->src2
= src1
; /* Zero */
752 static int is_in_range(pseudo_t src
, long long low
, long long high
)
759 return value
>= low
&& value
<= high
;
765 static int simplify_range(struct instruction
*insn
)
767 pseudo_t src1
, src2
, src3
;
772 if (src2
->type
!= PSEUDO_VAL
|| src3
->type
!= PSEUDO_VAL
)
774 if (is_in_range(src1
, src2
->value
, src3
->value
)) {
775 kill_instruction(insn
);
782 * Simplify "set_ne/eq $0 + br"
784 static int simplify_cond_branch(struct instruction
*br
, pseudo_t cond
, struct instruction
*def
, pseudo_t
*pp
)
786 use_pseudo(br
, *pp
, &br
->cond
);
787 remove_usage(cond
, &br
->cond
);
788 if (def
->opcode
== OP_SET_EQ
) {
789 struct basic_block
*true = br
->bb_true
;
790 struct basic_block
*false = br
->bb_false
;
797 static int simplify_branch(struct instruction
*insn
)
799 pseudo_t cond
= insn
->cond
;
804 /* Constant conditional */
805 if (constant(cond
)) {
806 insert_branch(insn
->bb
, insn
, cond
->value
? insn
->bb_true
: insn
->bb_false
);
811 if (insn
->bb_true
== insn
->bb_false
) {
812 struct basic_block
*bb
= insn
->bb
;
813 struct basic_block
*target
= insn
->bb_false
;
814 remove_bb_from_list(&target
->parents
, bb
, 1);
815 remove_bb_from_list(&bb
->children
, target
, 1);
816 insn
->bb_false
= NULL
;
817 kill_use(&insn
->cond
);
822 /* Conditional on a SETNE $0 or SETEQ $0 */
823 if (cond
->type
== PSEUDO_REG
) {
824 struct instruction
*def
= cond
->def
;
826 if (def
->opcode
== OP_SET_NE
|| def
->opcode
== OP_SET_EQ
) {
827 if (constant(def
->src1
) && !def
->src1
->value
)
828 return simplify_cond_branch(insn
, cond
, def
, &def
->src2
);
829 if (constant(def
->src2
) && !def
->src2
->value
)
830 return simplify_cond_branch(insn
, cond
, def
, &def
->src1
);
832 if (def
->opcode
== OP_SEL
) {
833 if (constant(def
->src2
) && constant(def
->src3
)) {
834 long long val1
= def
->src2
->value
;
835 long long val2
= def
->src3
->value
;
836 if (!val1
&& !val2
) {
837 insert_branch(insn
->bb
, insn
, insn
->bb_false
);
841 insert_branch(insn
->bb
, insn
, insn
->bb_true
);
845 struct basic_block
*true = insn
->bb_true
;
846 struct basic_block
*false = insn
->bb_false
;
847 insn
->bb_false
= true;
848 insn
->bb_true
= false;
850 use_pseudo(insn
, def
->src1
, &insn
->cond
);
851 remove_usage(cond
, &insn
->cond
);
855 if (def
->opcode
== OP_CAST
|| def
->opcode
== OP_SCAST
) {
856 int orig_size
= def
->orig_type
? def
->orig_type
->bit_size
: 0;
857 if (def
->size
> orig_size
) {
858 use_pseudo(insn
, def
->src
, &insn
->cond
);
859 remove_usage(cond
, &insn
->cond
);
867 static int simplify_switch(struct instruction
*insn
)
869 pseudo_t cond
= insn
->cond
;
871 struct multijmp
*jmp
;
875 val
= insn
->cond
->value
;
877 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
879 if (jmp
->begin
> jmp
->end
)
881 if (val
>= jmp
->begin
&& val
<= jmp
->end
)
883 } END_FOR_EACH_PTR(jmp
);
884 warning(insn
->pos
, "Impossible case statement");
888 insert_branch(insn
->bb
, insn
, jmp
->target
);
892 int simplify_instruction(struct instruction
*insn
)
896 switch (insn
->opcode
) {
897 case OP_ADD
: case OP_MULS
:
898 case OP_AND
: case OP_OR
: case OP_XOR
:
899 case OP_AND_BOOL
: case OP_OR_BOOL
:
900 if (simplify_binop(insn
))
902 if (simplify_commutative_binop(insn
))
904 return simplify_associative_binop(insn
);
907 case OP_SET_EQ
: case OP_SET_NE
:
908 if (simplify_binop(insn
))
910 return simplify_commutative_binop(insn
);
913 case OP_DIVU
: case OP_DIVS
:
914 case OP_MODU
: case OP_MODS
:
916 case OP_LSR
: case OP_ASR
:
917 case OP_SET_LE
: case OP_SET_GE
:
918 case OP_SET_LT
: case OP_SET_GT
:
919 case OP_SET_B
: case OP_SET_A
:
920 case OP_SET_BE
: case OP_SET_AE
:
921 return simplify_binop(insn
);
923 case OP_NOT
: case OP_NEG
:
924 return simplify_unop(insn
);
925 case OP_LOAD
: case OP_STORE
:
926 return simplify_memop(insn
);
928 if (dead_insn(insn
, NULL
, NULL
, NULL
))
929 return REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
930 return replace_with_pseudo(insn
, insn
->symbol
);
935 return simplify_cast(insn
);
937 if (dead_insn(insn
, NULL
, NULL
, NULL
)) {
941 return clean_up_phi(insn
);
943 if (dead_insn(insn
, &insn
->phi_src
, NULL
, NULL
))
947 return simplify_select(insn
);
949 return simplify_branch(insn
);
951 return simplify_switch(insn
);
953 return simplify_range(insn
);