2 * Simplify - do instruction simplification before CSE
4 * Copyright (C) 2004 Linus Torvalds
8 // Instruction simplification
9 // --------------------------
13 // The following conventions are used to describe the simplications:
14 // * Uppercase letters are reserved for constants:
15 // * `M` for a constant mask,
16 // * `S` for a constant shift,
17 // * `N` for a constant number of bits (usually other than a shift),
18 // * `C` or 'K' for others constants.
19 // * Lowercase letters `a`, `b`, `x`, `y`, ... are used for non-constants
20 // or when it doesn't matter if the pseudo is a constant or not.
21 // * Primes are used if needed to distinguish symbols (`M`, `M'`, ...).
22 // * Expressions or sub-expressions involving only constants are
23 // understood to be evaluated.
24 // * `$mask(N)` is used for `((1 << N) -1)`
25 // * `$trunc(x, N)` is used for `(x & $mask(N))`
26 // * Expressions like `(-1 << S)`, `(-1 >> S)` and others formulae are
27 // understood to be truncated to the size of the current instruction
28 // (needed, since in general this size is not the same as the one used
29 // by sparse for the evaluation of arithmetic operations).
30 // * `TRUNC(x, N)` is used for a truncation *to* a size of `N` bits
31 // * `ZEXT(x, N)` is used for a zero-extension *from* a size of `N` bits
32 // * `OP(x, C)` is used to represent some generic operation using a constant,
33 // including when the constant is implicit (e.g. `TRUNC(x, N)`).
34 // * `MASK(x, M)` is used to respresent a 'masking' instruction:
36 // - `LSR(x, S)`, with `M` = (-1 << S)
37 // - `SHL(x, S)`, with `M` = (-1 >> S)
38 // - `TRUNC(x, N)`, with `M` = $mask(N)
39 // - `ZEXT(x, N)`, with `M` = $mask(N)
40 // * `SHIFT(x, S)` is used for `LSR(x, S)` or `SHL(x, S)`.
45 #include "expression.h"
46 #include "linearize.h"
50 #include "flowgraph.h"
57 // check if a pseudo is a power of 2
58 static inline bool is_pow2(pseudo_t src
)
60 if (src
->type
!= PSEUDO_VAL
)
62 return is_power_of_2(src
->value
);
66 // find the trivial parent for a phi-source
67 static struct basic_block
*phi_parent(struct basic_block
*source
, pseudo_t pseudo
)
69 /* Can't go upwards if the pseudo is defined in the bb it came from.. */
70 if (pseudo
->type
== PSEUDO_REG
) {
71 struct instruction
*def
= pseudo
->def
;
72 if (def
->bb
== source
)
75 if (bb_list_size(source
->children
) != 1 || bb_list_size(source
->parents
) != 1)
77 return first_basic_block(source
->parents
);
81 // copy the phi-node's phisrcs into to given array
82 // @return: 0 if the the list contained the expected
83 // number of element, a positive number if there was
84 // more than expected and a negative one if less.
86 // :note: we can't reuse ptr_list_to_array() for the phi-sources
87 // because any VOIDs in the phi-list must be ignored here
88 // as in this context they mean 'entry has been removed'.
89 static int get_phisources(struct instruction
*sources
[], int nbr
, struct instruction
*insn
)
94 assert(insn
->opcode
== OP_PHI
);
95 FOR_EACH_PTR(insn
->phi_list
, phi
) {
96 struct instruction
*def
;
102 assert(def
->opcode
== OP_PHISOURCE
);
104 } END_FOR_EACH_PTR(phi
);
108 static int if_convert_phi(struct instruction
*insn
)
110 struct instruction
*array
[2];
111 struct basic_block
*parents
[2];
112 struct basic_block
*bb
, *bb1
, *bb2
, *source
;
113 struct instruction
*br
;
117 if (get_phisources(array
, 2, insn
))
119 if (ptr_list_to_array(bb
->parents
, parents
, 2) != 2)
121 p1
= array
[0]->phi_src
;
123 p2
= array
[1]->phi_src
;
126 /* Only try the simple "direct parents" case */
127 if ((bb1
!= parents
[0] || bb2
!= parents
[1]) &&
128 (bb1
!= parents
[1] || bb2
!= parents
[0]))
132 * See if we can find a common source for this..
134 source
= phi_parent(bb1
, p1
);
135 if (source
!= phi_parent(bb2
, p2
))
139 * Cool. We now know that 'source' is the exclusive
140 * parent of both phi-nodes, so the exit at the
141 * end of it fully determines which one it is, and
142 * we can turn it into a select.
144 * HOWEVER, right now we only handle regular
145 * conditional branches. No multijumps or computed
146 * stuff. Verify that here.
148 br
= last_instruction(source
->insns
);
149 if (!br
|| br
->opcode
!= OP_CBR
)
153 assert(br
->bb_false
);
156 * We're in business. Match up true/false with p1/p2.
158 if (br
->bb_true
== bb2
|| br
->bb_false
== bb1
) {
165 * OK, we can now replace that last
172 * select pseudo, p1, p2
175 * and remove the phi-node. If it then
176 * turns out that 'a' or 'b' is entirely
177 * empty (common case), and now no longer
178 * a phi-source, we'll be able to simplify
179 * the conditional branch too.
181 insert_select(source
, br
, insn
, p1
, p2
);
182 kill_instruction(insn
);
187 // detect trivial phi-nodes
188 // @insn: the phi-node
189 // @pseudo: the candidate resulting pseudo (NULL when starting)
190 // @return: the unique result if the phi-node is trivial, NULL otherwise
192 // A phi-node is trivial if it has a single possible result:
193 // * all operands are the same
194 // * the operands are themselves defined by a chain or cycle of phi-nodes
195 // and the set of all operands involved contains a single value
196 // not defined by these phi-nodes
198 // Since the result is unique, these phi-nodes can be removed.
199 static pseudo_t
trivial_phi(pseudo_t pseudo
, struct instruction
*insn
, struct pseudo_list
**list
)
201 pseudo_t target
= insn
->target
;
204 add_pseudo(list
, target
);
206 FOR_EACH_PTR(insn
->phi_list
, phi
) {
207 struct instruction
*def
;
215 src
= def
->phi_src
; // bypass OP_PHISRC & get the real source
226 if (DEF_OPCODE(def
, src
) == OP_PHI
) {
227 if (pseudo_in_list(*list
, src
))
229 if ((pseudo
= trivial_phi(pseudo
, def
, list
)))
233 } END_FOR_EACH_PTR(phi
);
235 return pseudo
? pseudo
: VOID
;
238 static int clean_up_phi(struct instruction
*insn
)
240 struct pseudo_list
*list
= NULL
;
243 if ((pseudo
= trivial_phi(NULL
, insn
, &list
))) {
244 convert_instruction_target(insn
, pseudo
);
245 kill_instruction(insn
);
249 return if_convert_phi(insn
);
252 static int delete_pseudo_user_list_entry(struct pseudo_user_list
**list
, pseudo_t
*entry
, int count
)
254 struct pseudo_user
*pu
;
256 FOR_EACH_PTR(*list
, pu
) {
257 if (pu
->userp
== entry
) {
258 MARK_CURRENT_DELETED(pu
);
262 } END_FOR_EACH_PTR(pu
);
265 if (pseudo_user_list_empty(*list
))
270 static inline void rem_usage(pseudo_t p
, pseudo_t
*usep
, int kill
)
272 if (has_use_list(p
)) {
273 delete_pseudo_user_list_entry(&p
->users
, usep
, 1);
274 if (kill
&& !p
->users
&& has_definition(p
))
275 kill_instruction(p
->def
);
279 static inline void remove_usage(pseudo_t p
, pseudo_t
*usep
)
281 rem_usage(p
, usep
, 1);
284 void kill_use(pseudo_t
*usep
)
289 rem_usage(p
, usep
, 1);
293 // Like kill_use() but do not (recursively) kill dead instructions
294 void remove_use(pseudo_t
*usep
)
298 rem_usage(p
, usep
, 0);
301 static void kill_use_list(struct pseudo_list
*list
)
304 FOR_EACH_PTR(list
, p
) {
307 kill_use(THIS_ADDRESS(p
));
308 } END_FOR_EACH_PTR(p
);
311 static void kill_asm(struct instruction
*insn
)
313 struct asm_constraint
*con
;
315 FOR_EACH_PTR(insn
->asm_rules
->inputs
, con
) {
316 kill_use(&con
->pseudo
);
317 } END_FOR_EACH_PTR(con
);
321 // kill an instruction
322 // @insn: the instruction to be killed
323 // @force: if unset, the normal case, the instruction is not killed
324 // if not free of possible side-effect; if set the instruction
325 // is unconditionally killed.
327 // The killed instruction is removed from its BB and the usage
328 // of all its operands are removed. The instruction is also
329 // marked as killed by setting its ->bb to NULL.
330 int kill_insn(struct instruction
*insn
, int force
)
332 if (!insn
|| !insn
->bb
)
335 switch (insn
->opcode
) {
338 kill_use(&insn
->src3
);
341 case OP_BINARY
... OP_BINCMP_END
:
342 kill_use(&insn
->src2
);
345 case OP_UNOP
... OP_UNOP_END
:
351 case OP_COMPUTEDGOTO
:
352 kill_use(&insn
->src1
);
356 kill_use_list(insn
->phi_list
);
361 /* a "pure" function can be killed too */
362 struct symbol
*fntype
= first_symbol(insn
->fntypes
);
364 if (!(fntype
->ctype
.modifiers
& MOD_PURE
))
367 kill_use_list(insn
->arguments
);
368 if (insn
->func
->type
== PSEUDO_REG
)
369 kill_use(&insn
->func
);
373 if (!force
&& insn
->is_volatile
)
375 kill_use(&insn
->src
);
381 kill_use(&insn
->src
);
382 kill_use(&insn
->target
);
404 return repeat_phase
|= REPEAT_CSE
;
407 static inline bool has_target(struct instruction
*insn
)
409 return opcode_table
[insn
->opcode
].flags
& OPF_TARGET
;
412 void remove_dead_insns(struct entrypoint
*ep
)
414 struct basic_block
*bb
;
416 FOR_EACH_PTR_REVERSE(ep
->bbs
, bb
) {
417 struct instruction
*insn
;
418 FOR_EACH_PTR_REVERSE(bb
->insns
, insn
) {
421 if (!has_target(insn
))
423 if (!has_users(insn
->target
))
424 kill_instruction(insn
);
425 } END_FOR_EACH_PTR_REVERSE(insn
);
426 } END_FOR_EACH_PTR_REVERSE(bb
);
429 static inline int constant(pseudo_t pseudo
)
431 return pseudo
->type
== PSEUDO_VAL
;
435 // is this same signed value when interpreted with both size?
436 static inline bool is_signed_constant(long long val
, unsigned osize
, unsigned nsize
)
438 return bits_extend(val
, osize
, 1) == bits_extend(val
, nsize
, 1);
442 // is @src generated by an instruction with the given opcode and size?
443 static inline pseudo_t
is_same_op(pseudo_t src
, int op
, unsigned osize
)
445 struct instruction
*def
;
447 if (src
->type
!= PSEUDO_REG
)
450 if (def
->opcode
!= op
)
452 if (def
->orig_type
->bit_size
!= osize
)
457 static bool is_negate_of(pseudo_t p
, pseudo_t ref
)
459 struct instruction
*def
;
461 return (DEF_OPCODE(def
, p
) == OP_NEG
) && (def
->src
== ref
);
465 // replace the operand of an instruction
466 // @insn: the instruction
467 // @pp: the address of the instruction's operand
468 // @new: the new value for the operand
469 // @return: REPEAT_CSE.
470 static inline int replace_pseudo(struct instruction
*insn
, pseudo_t
*pp
, pseudo_t
new)
473 use_pseudo(insn
, new, pp
);
474 remove_usage(old
, pp
);
478 int replace_with_pseudo(struct instruction
*insn
, pseudo_t pseudo
)
480 convert_instruction_target(insn
, pseudo
);
481 return kill_instruction(insn
);
484 static inline int replace_with_value(struct instruction
*insn
, long long val
)
486 return replace_with_pseudo(insn
, value_pseudo(val
));
490 // replace a binop with an unop
491 // @insn: the instruction to be replaced
492 // @op: the instruction's new opcode
493 // @src: the instruction's new operand
494 // @return: REPEAT_CSE
495 static inline int replace_with_unop(struct instruction
*insn
, int op
, pseudo_t src
)
498 replace_pseudo(insn
, &insn
->src1
, src
);
499 remove_usage(insn
->src2
, &insn
->src2
);
504 // replace rightside's value
505 // @insn: the instruction to be replaced
506 // @op: the instruction's new opcode
507 // @src: the instruction's new operand
508 // @return: REPEAT_CSE
509 static inline int replace_binop_value(struct instruction
*insn
, int op
, long long val
)
512 insn
->src2
= value_pseudo(val
);
517 // replace binop's opcode and values
518 // @insn: the instruction to be replaced
519 // @op: the instruction's new opcode
520 // @return: REPEAT_CSE
521 static inline int replace_binop(struct instruction
*insn
, int op
, pseudo_t
*pa
, pseudo_t a
, pseudo_t
*pb
, pseudo_t b
)
526 use_pseudo(insn
, a
, pa
);
527 use_pseudo(insn
, b
, pb
);
528 remove_usage(olda
, pa
);
529 remove_usage(oldb
, pb
);
534 // replace the opcode of an instruction
535 // @return: REPEAT_CSE
536 static inline int replace_opcode(struct instruction
*insn
, int op
)
543 // create an instruction pair OUT(IN(a, b), c)
544 static int replace_insn_pair(struct instruction
*out
, int op_out
, struct instruction
*in
, int op_in
, pseudo_t a
, pseudo_t b
, pseudo_t c
)
546 pseudo_t old_a
= in
->src1
;
547 pseudo_t old_b
= in
->src2
;
548 pseudo_t old_1
= out
->src1
;
549 pseudo_t old_2
= out
->src2
;
551 use_pseudo(in
, a
, &in
->src1
);
552 use_pseudo(in
, b
, &in
->src2
);
553 use_pseudo(out
, in
->target
, &out
->src1
);
554 use_pseudo(out
, c
, &out
->src2
);
556 remove_usage(old_a
, &in
->src1
);
557 remove_usage(old_b
, &in
->src2
);
558 remove_usage(old_1
, &out
->src1
);
559 remove_usage(old_2
, &out
->src2
);
561 out
->opcode
= op_out
;
567 // create an instruction pair OUT(IN(a, b), c) with swapped opcodes
568 static inline int swap_insn(struct instruction
*out
, struct instruction
*in
, pseudo_t a
, pseudo_t b
, pseudo_t c
)
570 return replace_insn_pair(out
, in
->opcode
, in
, out
->opcode
, a
, b
, c
);
574 // create an instruction pair OUT(SELECT(a, b, c), d)
575 static int swap_select(struct instruction
*out
, struct instruction
*in
, pseudo_t a
, pseudo_t b
, pseudo_t c
, pseudo_t d
)
577 use_pseudo(in
, c
, &in
->src3
);
578 swap_insn(out
, in
, a
, b
, d
);
579 kill_use(&out
->src3
);
583 static inline int def_opcode(pseudo_t p
)
585 if (p
->type
!= PSEUDO_REG
)
587 return p
->def
->opcode
;
590 static unsigned int value_size(long long value
)
605 // try to determine the maximum size of bits in a pseudo
607 // Right now this only follow casts and constant values, but we
608 // could look at things like AND instructions, etc.
609 static unsigned int operand_size(struct instruction
*insn
, pseudo_t pseudo
)
611 unsigned int size
= insn
->size
;
613 if (pseudo
->type
== PSEUDO_REG
) {
614 struct instruction
*src
= pseudo
->def
;
615 if (src
&& src
->opcode
== OP_ZEXT
&& src
->orig_type
) {
616 unsigned int orig_size
= src
->orig_type
->bit_size
;
617 if (orig_size
< size
)
621 if (pseudo
->type
== PSEUDO_VAL
) {
622 unsigned int orig_size
= value_size(pseudo
->value
);
623 if (orig_size
< size
)
629 static pseudo_t
eval_op(int op
, unsigned size
, pseudo_t src1
, pseudo_t src2
)
631 /* FIXME! Verify signs and sizes!! */
632 long long left
= src1
->value
;
633 long long right
= src2
->value
;
634 unsigned long long ul
, ur
;
635 long long res
, mask
, bits
;
637 mask
= 1ULL << (size
-1);
638 bits
= mask
| (mask
-1);
672 if (left
== mask
&& right
== -1)
684 if (left
== mask
&& right
== -1)
714 /* Binary comparison */
749 // Warning: this should be done with the output size which may
750 // be different than the input size used here. But it differs
751 // only for compares which are not concerned since only returning
752 // 0 or 1 and for casts which are not handled here.
755 return value_pseudo(res
);
761 static inline pseudo_t
eval_unop(int op
, unsigned size
, pseudo_t src
)
763 return eval_op(op
, size
, src
, VOID
);
771 // try to simplify MASK(OR(AND(x, M'), b), M)
772 // @insn: the masking instruction
773 // @mask: the associated mask (M)
774 // @ora: one of the OR's operands, guaranteed to be PSEUDO_REG
775 // @orb: the other OR's operand
776 // @return: 0 if no changes have been made, one or more REPEAT_* flags otherwise.
777 static int simplify_mask_or_and(struct instruction
*insn
, unsigned long long mask
,
778 pseudo_t ora
, pseudo_t orb
)
780 unsigned long long omask
, nmask
;
781 struct instruction
*and = ora
->def
;
782 pseudo_t src2
= and->src2
;
784 if (and->opcode
!= OP_AND
)
789 nmask
= omask
& mask
;
791 // if (M' & M) == 0: ((a & M') | b) -> b
792 return replace_pseudo(insn
, &insn
->src1
, orb
);
794 if (!one_use(insn
->src1
))
795 return 0; // can't modify anything inside the OR
797 struct instruction
*or = insn
->src1
->def
;
798 pseudo_t
*arg
= (ora
== or->src1
) ? &or->src1
: &or->src2
;
799 // if (M' & M) == M: ((a & M') | b) -> (a | b)
800 return replace_pseudo(or, arg
, and->src1
);
802 if (nmask
!= omask
&& one_use(ora
)) {
803 // if (M' & M) != M': AND(a, M') -> AND(a, (M' & M))
804 and->src2
= value_pseudo(nmask
);
811 // try to simplify MASK(OR(a, b), M)
812 // @insn: the masking instruction
813 // @mask: the associated mask (M)
814 // @or: the OR instruction
815 // @return: 0 if no changes have been made, one or more REPEAT_* flags otherwise.
816 static int simplify_mask_or(struct instruction
*insn
, unsigned long long mask
, struct instruction
*or)
818 pseudo_t src1
= or->src1
;
819 pseudo_t src2
= or->src2
;
822 if (src1
->type
== PSEUDO_REG
) {
823 if ((rc
= simplify_mask_or_and(insn
, mask
, src1
, src2
)))
826 if (src2
->type
== PSEUDO_REG
) {
827 if ((rc
= simplify_mask_or_and(insn
, mask
, src2
, src1
)))
829 } else if (src2
->type
== PSEUDO_VAL
) {
830 unsigned long long oval
= src2
->value
;
831 unsigned long long nval
= oval
& mask
;
835 // if (C & M) == 0: OR(x, C) -> x
836 return replace_pseudo(insn
, &insn
->src1
, src1
);
839 // if (C & M) == M: OR(x, C) -> M
840 return replace_pseudo(insn
, &insn
->src1
, value_pseudo(mask
));
842 if (nval
!= oval
&& one_use(or->target
)) {
843 // if (C & M) != C: OR(x, C) -> OR(x, (C & M))
844 return replace_pseudo(or, &or->src2
, value_pseudo(nval
));
851 // try to simplify MASK(SHIFT(OR(a, b), S), M)
852 // @sh: the shift instruction
853 // @or: the OR instruction
854 // @mask: the mask associated to MASK (M):
855 // @return: 0 if no changes have been made, one or more REPEAT_* flags otherwise.
856 static int simplify_mask_shift_or(struct instruction
*sh
, struct instruction
*or, unsigned long long mask
)
858 unsigned long long smask
= bits_mask(sh
->size
);
859 int shift
= sh
->src2
->value
;
861 if (sh
->opcode
== OP_LSR
)
865 return simplify_mask_or(sh
, smask
& mask
, or);
868 static int simplify_mask_shift(struct instruction
*sh
, unsigned long long mask
)
870 struct instruction
*inner
;
872 if (!constant(sh
->src2
) || sh
->tainted
)
874 switch (DEF_OPCODE(inner
, sh
->src1
)) {
876 if (one_use(sh
->target
))
877 return simplify_mask_shift_or(sh
, inner
, mask
);
883 static pseudo_t
eval_insn(struct instruction
*insn
)
885 unsigned size
= insn
->size
;
887 if (opcode_table
[insn
->opcode
].flags
& OPF_COMPARE
)
888 size
= insn
->itype
->bit_size
;
889 return eval_op(insn
->opcode
, size
, insn
->src1
, insn
->src2
);
892 static long long check_shift_count(struct instruction
*insn
, unsigned long long uval
)
894 unsigned int size
= insn
->size
;
895 long long sval
= uval
;
904 sval
= sign_extend_safe(sval
, size
);
905 sval
= sign_extend_safe(sval
, bits_in_int
);
907 insn
->src2
= value_pseudo(sval
);
911 static int simplify_shift(struct instruction
*insn
, pseudo_t pseudo
, long long value
)
913 struct instruction
*def
;
914 unsigned long long mask
, omask
, nmask
;
915 unsigned long long nval
;
920 return replace_with_pseudo(insn
, pseudo
);
921 value
= check_shift_count(insn
, value
);
926 switch (insn
->opcode
) {
930 if (pseudo
->type
!= PSEUDO_REG
)
933 switch (def
->opcode
) {
936 if (def
== insn
) // cyclic DAG!
939 if (src2
->type
!= PSEUDO_VAL
)
942 if (nval
> insn
->size
|| nval
== 0)
945 if (def
->opcode
== OP_LSR
)
946 insn
->opcode
= OP_LSR
;
947 else if (value
>= size
)
953 // zext.N %t <- (O) %a
956 // zext.N %t <- (O) %a
958 insn
->opcode
= OP_LSR
;
963 size
= operand_size(insn
, pseudo
);
966 switch(DEF_OPCODE(def
, pseudo
)) {
968 // replace (A & M) >> S
969 // by (A >> S) & (M >> S)
970 if (!constant(def
->src2
))
972 mask
= bits_mask(insn
->size
- value
) << value
;
973 omask
= def
->src2
->value
;
974 nmask
= omask
& mask
;
976 return replace_with_value(insn
, 0);
978 return replace_pseudo(insn
, &insn
->src1
, def
->src1
);
979 if (!one_use(pseudo
))
981 def
->opcode
= OP_LSR
;
982 def
->src2
= insn
->src2
;
983 insn
->opcode
= OP_AND
;
984 insn
->src2
= value_pseudo(omask
>> value
);
987 goto case_shift_shift
;
989 mask
= bits_mask(size
);
990 return simplify_mask_shift_or(insn
, def
, mask
);
992 // replace ((x << S) >> S)
993 // by (x & (-1 >> S))
994 if (def
->src2
!= insn
->src2
)
996 mask
= bits_mask(insn
->size
- value
);
1003 switch(DEF_OPCODE(def
, pseudo
)) {
1005 // simplify (A & M) << S
1006 if (!constant(def
->src2
))
1008 mask
= bits_mask(insn
->size
) >> value
;
1009 omask
= def
->src2
->value
;
1010 nmask
= omask
& mask
;
1012 return replace_with_value(insn
, 0);
1014 return replace_pseudo(insn
, &insn
->src1
, def
->src1
);
1015 // do not simplify into ((A << S) & (M << S))
1018 // replace ((x >> S) << S)
1019 // by (x & (-1 << S))
1020 if (def
->src2
!= insn
->src2
)
1022 mask
= bits_mask(insn
->size
- value
) << value
;
1025 mask
= bits_mask(size
);
1026 return simplify_mask_shift_or(insn
, def
, mask
);
1028 case_shift_shift
: // also for LSR - LSR
1029 if (def
== insn
) // cyclic DAG!
1032 if (src2
->type
!= PSEUDO_VAL
)
1035 if (nval
> insn
->size
)
1046 insn
->src2
= value_pseudo(value
);
1047 return replace_pseudo(insn
, &insn
->src1
, pseudo
->def
->src1
);
1050 return replace_with_value(insn
, 0);
1052 insn
->opcode
= OP_AND
;
1053 insn
->src2
= value_pseudo(mask
);
1054 return replace_pseudo(insn
, &insn
->src1
, def
->src1
);
1057 static int simplify_mul_div(struct instruction
*insn
, long long value
)
1059 unsigned long long sbit
= 1ULL << (insn
->size
- 1);
1060 unsigned long long bits
= sbit
| (sbit
- 1);
1063 return replace_with_pseudo(insn
, insn
->src1
);
1065 switch (insn
->opcode
) {
1068 return replace_with_pseudo(insn
, insn
->src2
);
1071 if (!(value
& sbit
)) // positive
1076 insn
->opcode
= OP_NEG
;
1084 static int simplify_seteq_setne(struct instruction
*insn
, long long value
)
1086 pseudo_t old
= insn
->src1
;
1087 struct instruction
*def
;
1092 if (value
!= 0 && value
!= 1)
1095 if (old
->type
!= PSEUDO_REG
)
1101 inverse
= (insn
->opcode
== OP_SET_NE
) == value
;
1102 if (!inverse
&& def
->size
== 1 && insn
->size
== 1) {
1104 // setne %r <- %s, $0
1106 // seteq %r <- %s, $1
1107 // by %s when boolean
1108 return replace_with_pseudo(insn
, old
);
1110 opcode
= def
->opcode
;
1115 if (def
->size
!= insn
->size
)
1117 if (def
->src2
->type
!= PSEUDO_VAL
)
1119 if (def
->src2
->value
!= 1)
1121 return replace_with_pseudo(insn
, old
);
1122 case OP_FPCMP
... OP_BINCMP_END
:
1124 // setcc.n %t <- %a, %b
1125 // setne.m %r <- %t, $0
1127 // setcc.n %t <- %a, %b
1128 // setcc.m %r <- %a, $b
1129 // and similar for setne/eq ... 0/1
1130 insn
->opcode
= inverse
? opcode_table
[opcode
].negate
: opcode
;
1131 insn
->itype
= def
->itype
;
1132 use_pseudo(insn
, def
->src1
, &insn
->src1
);
1133 use_pseudo(insn
, def
->src2
, &insn
->src2
);
1134 remove_usage(old
, &insn
->src1
);
1138 if (value
&& (def
->orig_type
->bit_size
== 1))
1143 // *ext.m %s <- (1) %a
1144 // setne.1 %r <- %s, $0
1146 // setne.1 %s <- %a, $0
1147 // and same for setne/eq ... 0/1
1148 insn
->itype
= def
->orig_type
;
1149 return replace_pseudo(insn
, &insn
->src1
, def
->src
);
1154 // trunc.n %s <- (o) %a
1155 // setne.m %r <- %s, $0
1157 // and.o %s <- %a, $((1 << o) - 1)
1158 // setne.m %r <- %s, $0
1159 // and same for setne/eq ... 0/1
1161 def
->opcode
= OP_AND
;
1162 def
->type
= def
->orig_type
;
1163 def
->size
= def
->type
->bit_size
;
1164 def
->src2
= value_pseudo(bits_mask(osize
));
1170 static int simplify_compare_constant(struct instruction
*insn
, long long value
)
1172 unsigned size
= insn
->itype
->bit_size
;
1173 unsigned long long bits
= bits_mask(size
);
1174 struct instruction
*def
;
1175 pseudo_t src1
, src2
;
1179 switch (insn
->opcode
) {
1183 if (value
== sign_bit(size
)) // (x < SMIN) --> 0
1184 return replace_with_pseudo(insn
, value_pseudo(0));
1185 if (value
== sign_mask(size
)) // (x < SMAX) --> (x != SMAX)
1186 return replace_opcode(insn
, OP_SET_NE
);
1187 if (value
== sign_bit(size
) + 1)// (x < SMIN + 1) --> (x == SMIN)
1188 return replace_binop_value(insn
, OP_SET_EQ
, sign_bit(size
));
1189 if (!(value
& sign_bit(size
)))
1190 changed
|= replace_binop_value(insn
, OP_SET_LE
, (value
- 1) & bits
);
1195 if (value
== sign_mask(size
)) // (x <= SMAX) --> 1
1196 return replace_with_pseudo(insn
, value_pseudo(1));
1197 if (value
== sign_bit(size
)) // (x <= SMIN) --> (x == SMIN)
1198 return replace_opcode(insn
, OP_SET_EQ
);
1199 if (value
== sign_mask(size
) - 1) // (x <= SMAX - 1) --> (x != SMAX)
1200 return replace_binop_value(insn
, OP_SET_NE
, sign_mask(size
));
1201 if (value
& sign_bit(size
))
1202 changed
|= replace_binop_value(insn
, OP_SET_LT
, (value
+ 1) & bits
);
1207 if (value
== sign_bit(size
)) // (x >= SMIN) --> 1
1208 return replace_with_pseudo(insn
, value_pseudo(1));
1209 if (value
== sign_mask(size
)) // (x >= SMAX) --> (x == SMAX)
1210 return replace_opcode(insn
, OP_SET_EQ
);
1211 if (value
== sign_bit(size
) + 1)// (x >= SMIN + 1) --> (x != SMIN)
1212 return replace_binop_value(insn
, OP_SET_NE
, sign_bit(size
));
1213 if (!(value
& sign_bit(size
)))
1214 changed
|= replace_binop_value(insn
, OP_SET_GT
, (value
- 1) & bits
);
1219 if (value
== sign_mask(size
)) // (x > SMAX) --> 0
1220 return replace_with_pseudo(insn
, value_pseudo(0));
1221 if (value
== sign_bit(size
)) // (x > SMIN) --> (x != SMIN)
1222 return replace_opcode(insn
, OP_SET_NE
);
1223 if (value
== sign_mask(size
) - 1) // (x > SMAX - 1) --> (x == SMAX)
1224 return replace_binop_value(insn
, OP_SET_EQ
, sign_mask(size
));
1225 if (value
& sign_bit(size
))
1226 changed
|= replace_binop_value(insn
, OP_SET_GE
, (value
+ 1) & bits
);
1230 if (!value
) // (x < 0) --> 0
1231 return replace_with_pseudo(insn
, value_pseudo(0));
1232 if (value
== 1) // (x < 1) --> (x == 0)
1233 return replace_binop_value(insn
, OP_SET_EQ
, 0);
1234 else if (value
== bits
) // (x < ~0) --> (x != ~0)
1235 return replace_binop_value(insn
, OP_SET_NE
, value
);
1236 else // (x < y) --> (x <= (y-1))
1237 changed
|= replace_binop_value(insn
, OP_SET_BE
, (value
- 1) & bits
);
1240 if (!value
) // (x >= 0) --> 1
1241 return replace_with_pseudo(insn
, value_pseudo(1));
1242 if (value
== 1) // (x >= 1) --> (x != 0)
1243 return replace_binop_value(insn
, OP_SET_NE
, 0);
1244 else if (value
== bits
) // (x >= ~0) --> (x == ~0)
1245 return replace_binop_value(insn
, OP_SET_EQ
, value
);
1246 else // (x >= y) --> (x > (y-1)
1247 changed
|= replace_binop_value(insn
, OP_SET_A
, (value
- 1) & bits
);
1250 if (!value
) // (x <= 0) --> (x == 0)
1251 return replace_opcode(insn
, OP_SET_EQ
);
1252 if (value
== bits
) // (x <= ~0) --> 1
1253 return replace_with_pseudo(insn
, value_pseudo(1));
1254 if (value
== (bits
- 1)) // (x <= ~1) --> (x != ~0)
1255 return replace_binop_value(insn
, OP_SET_NE
, bits
);
1256 if (value
== (bits
>> 1)) // (x u<= SMAX) --> (x s>= 0)
1257 changed
|= replace_binop_value(insn
, OP_SET_GE
, 0);
1260 if (!value
) // (x > 0) --> (x != 0)
1261 return replace_opcode(insn
, OP_SET_NE
);
1262 if (value
== bits
) // (x > ~0) --> 0
1263 return replace_with_pseudo(insn
, value_pseudo(0));
1264 if (value
== (bits
- 1)) // (x > ~1) --> (x == ~0)
1265 return replace_binop_value(insn
, OP_SET_EQ
, bits
);
1266 if (value
== (bits
>> 1)) // (x u> SMAX) --> (x s< 0)
1267 changed
|= replace_binop_value(insn
, OP_SET_LT
, 0);
1273 value
= src2
->value
;
1274 switch (DEF_OPCODE(def
, src1
)) {
1276 if (!constant(def
->src2
))
1278 bits
= def
->src2
->value
;
1279 switch (insn
->opcode
) {
1281 if ((value
& bits
) != value
)
1282 return replace_with_value(insn
, 0);
1283 if (value
== bits
&& is_power_of_2(bits
))
1284 return replace_binop_value(insn
, OP_SET_NE
, 0);
1287 if ((value
& bits
) != value
)
1288 return replace_with_value(insn
, 1);
1289 if (value
== bits
&& is_power_of_2(bits
))
1290 return replace_binop_value(insn
, OP_SET_EQ
, 0);
1292 case OP_SET_LE
: case OP_SET_LT
:
1293 value
= sign_extend(value
, def
->size
);
1294 if (insn
->opcode
== OP_SET_LT
)
1296 if (bits
& sign_bit(def
->size
))
1299 return replace_with_value(insn
, 0);
1300 if (value
>= (long long)bits
)
1301 return replace_with_value(insn
, 1);
1303 return replace_opcode(insn
, OP_SET_EQ
);
1305 case OP_SET_GT
: case OP_SET_GE
:
1306 value
= sign_extend(value
, def
->size
);
1307 if (insn
->opcode
== OP_SET_GE
)
1309 if (bits
& sign_bit(def
->size
))
1312 return replace_with_value(insn
, 1);
1313 if (value
>= (long long)bits
)
1314 return replace_with_value(insn
, 0);
1316 return replace_opcode(insn
, OP_SET_NE
);
1320 return replace_with_value(insn
, 1);
1324 return replace_with_value(insn
, 1);
1328 return replace_with_value(insn
, 0);
1332 return replace_with_value(insn
, 0);
1337 if (!constant(def
->src2
))
1339 bits
= def
->src2
->value
;
1340 switch (insn
->opcode
) {
1342 if ((value
& bits
) != bits
)
1343 return replace_with_value(insn
, 0);
1346 if ((value
& bits
) != bits
)
1347 return replace_with_value(insn
, 1);
1351 return replace_with_value(insn
, 0);
1355 return replace_with_value(insn
, 0);
1359 return replace_with_value(insn
, 1);
1363 return replace_with_value(insn
, 1);
1368 if (bits
& sign_bit(def
->size
)) {
1369 value
= sign_extend(value
, def
->size
);
1371 return replace_with_value(insn
, 1);
1377 if (bits
& sign_bit(def
->size
)) {
1378 value
= sign_extend(value
, def
->size
);
1380 return replace_with_value(insn
, 0);
1385 case OP_SEXT
: // sext(x) cmp C --> x cmp trunc(C)
1386 osize
= def
->orig_type
->bit_size
;
1387 if (is_signed_constant(value
, osize
, size
)) {
1388 insn
->itype
= def
->orig_type
;
1389 insn
->src2
= value_pseudo(zero_extend(value
, osize
));
1390 return replace_pseudo(insn
, &insn
->src1
, def
->src
);
1392 switch (insn
->opcode
) {
1394 if (value
>= sign_bit(osize
)) {
1395 insn
->itype
= def
->orig_type
;
1396 replace_binop_value(insn
, OP_SET_GE
, 0);
1397 return replace_pseudo(insn
, &insn
->src1
, def
->src
);
1401 if (value
>= sign_bit(osize
)) {
1402 insn
->itype
= def
->orig_type
;
1403 replace_binop_value(insn
, OP_SET_LT
, 0);
1404 return replace_pseudo(insn
, &insn
->src1
, def
->src
);
1407 case OP_SET_LT
: case OP_SET_LE
:
1408 if (value
< sign_bit(size
))
1409 return replace_with_value(insn
, 1);
1411 return replace_with_value(insn
, 0);
1413 case OP_SET_GE
: case OP_SET_GT
:
1414 if (value
< sign_bit(size
))
1415 return replace_with_value(insn
, 0);
1417 return replace_with_value(insn
, 1);
1422 osize
= def
->orig_type
->bit_size
;
1423 switch (insn
->opcode
) {
1424 case OP_SET_EQ
: case OP_SET_NE
:
1425 if (one_use(def
->target
)) {
1426 insn
->itype
= def
->orig_type
;
1427 def
->type
= def
->orig_type
;
1429 def
->src2
= value_pseudo(bits
);
1430 return replace_opcode(def
, OP_AND
);
1436 osize
= def
->orig_type
->bit_size
;
1437 bits
= bits_mask(osize
);
1438 if (value
<= bits
) {
1439 const struct opcode_table
*op
= &opcode_table
[insn
->opcode
];
1440 if (op
->flags
& OPF_SIGNED
)
1441 insn
->opcode
= op
->sign
;
1442 insn
->itype
= def
->orig_type
;
1443 return replace_pseudo(insn
, &insn
->src1
, def
->src
);
1445 switch (insn
->opcode
) {
1446 case OP_SET_LT
: case OP_SET_LE
:
1447 if (sign_extend(value
, size
) > (long long)bits
)
1448 return replace_with_value(insn
, 1);
1450 return replace_with_value(insn
, 0);
1452 case OP_SET_GE
: case OP_SET_GT
:
1453 if (sign_extend(value
, size
) > (long long)bits
)
1454 return replace_with_value(insn
, 0);
1456 return replace_with_value(insn
, 1);
1458 case OP_SET_B
: case OP_SET_BE
:
1459 return replace_with_value(insn
, 1);
1460 case OP_SET_AE
: case OP_SET_A
:
1461 return replace_with_value(insn
, 0);
1468 static int simplify_constant_mask(struct instruction
*insn
, unsigned long long mask
)
1470 pseudo_t old
= insn
->src1
;
1471 unsigned long long omask
;
1472 unsigned long long nmask
;
1473 struct instruction
*def
;
1476 switch (DEF_OPCODE(def
, old
)) {
1477 case OP_FPCMP
... OP_BINCMP_END
:
1481 return simplify_mask_or(insn
, mask
, def
);
1484 return simplify_mask_shift(def
, mask
);
1486 osize
= def
->orig_type
->bit_size
;
1489 omask
= (1ULL << osize
) - 1;
1490 nmask
= mask
& omask
;
1492 // the AND mask is redundant
1493 return replace_with_pseudo(insn
, old
);
1494 if (nmask
!= mask
) {
1495 // can use a smaller mask
1496 insn
->src2
= value_pseudo(nmask
);
1504 static int simplify_const_rightadd(struct instruction
*def
, struct instruction
*insn
)
1506 unsigned size
= insn
->size
;
1507 pseudo_t src2
= insn
->src2
;
1509 switch (def
->opcode
) {
1511 if (constant(def
->src1
)) { // (C - y) + D --> eval(C+D) - y
1512 pseudo_t val
= eval_op(OP_ADD
, size
, def
->src1
, src2
);
1513 insn
->opcode
= OP_SUB
;
1514 use_pseudo(insn
, def
->src2
, &insn
->src2
);
1515 return replace_pseudo(insn
, &insn
->src1
, val
);
1522 static int simplify_constant_rightside(struct instruction
*insn
)
1524 long long value
= insn
->src2
->value
;
1525 long long sbit
= 1ULL << (insn
->size
- 1);
1526 long long bits
= sbit
| (sbit
- 1);
1529 switch (insn
->opcode
) {
1531 if ((value
& bits
) == bits
)
1532 return replace_with_pseudo(insn
, insn
->src2
);
1533 goto case_neutral_zero
;
1536 if ((value
& bits
) == bits
) {
1537 insn
->opcode
= OP_NOT
;
1543 return replace_with_pseudo(insn
, insn
->src1
);
1547 insn
->opcode
= OP_ADD
;
1548 insn
->src2
= eval_unop(OP_NEG
, insn
->size
, insn
->src2
);
1549 changed
= REPEAT_CSE
;
1553 return replace_with_pseudo(insn
, insn
->src1
);
1554 if (insn
->src1
->type
== PSEUDO_REG
) // (x # y) + z
1555 changed
|= simplify_const_rightadd(insn
->src1
->def
, insn
);
1560 return simplify_shift(insn
, insn
->src1
, value
);
1562 case OP_MODU
: case OP_MODS
:
1564 return replace_with_value(insn
, 0);
1567 case OP_DIVU
: case OP_DIVS
:
1569 return simplify_mul_div(insn
, value
);
1573 return replace_with_pseudo(insn
, insn
->src2
);
1574 if ((value
& bits
) == bits
)
1575 return replace_with_pseudo(insn
, insn
->src1
);
1576 return simplify_constant_mask(insn
, value
);
1580 if ((changed
= simplify_seteq_setne(insn
, value
)))
1583 case OP_SET_LT
: case OP_SET_LE
: case OP_SET_GE
: case OP_SET_GT
:
1584 case OP_SET_B
: case OP_SET_BE
: case OP_SET_AE
: case OP_SET_A
:
1585 return simplify_compare_constant(insn
, value
);
1590 static int simplify_const_leftsub(struct instruction
*insn
, struct instruction
*def
)
1592 unsigned size
= insn
->size
;
1593 pseudo_t src1
= insn
->src1
;
1595 switch (def
->opcode
) {
1597 if (constant(def
->src2
)) { // C - (y + D) --> eval(C-D) - y
1598 insn
->src1
= eval_op(OP_SUB
, size
, src1
, def
->src2
);
1599 return replace_pseudo(insn
, &insn
->src2
, def
->src1
);
1603 if (constant(def
->src1
)) { // C - (D - z) --> z + eval(C-D)
1604 pseudo_t val
= eval_op(OP_SUB
, size
, src1
, def
->src1
);
1605 insn
->opcode
= OP_ADD
;
1606 use_pseudo(insn
, def
->src2
, &insn
->src1
);
1607 return replace_pseudo(insn
, &insn
->src2
, val
);
1614 static int simplify_constant_leftside(struct instruction
*insn
)
1616 long long value
= insn
->src1
->value
;
1618 switch (insn
->opcode
) {
1619 case OP_ADD
: case OP_OR
: case OP_XOR
:
1621 return replace_with_pseudo(insn
, insn
->src2
);
1625 case OP_LSR
: case OP_ASR
:
1629 return replace_with_pseudo(insn
, insn
->src1
);
1632 if (!value
) // (0 - x) --> -x
1633 return replace_with_unop(insn
, OP_NEG
, insn
->src2
);
1634 if (insn
->src2
->type
== PSEUDO_REG
)
1635 return simplify_const_leftsub(insn
, insn
->src2
->def
);
1641 static int simplify_constant_binop(struct instruction
*insn
)
1643 pseudo_t res
= eval_insn(insn
);
1648 return replace_with_pseudo(insn
, res
);
1651 static int simplify_binop_same_args(struct instruction
*insn
, pseudo_t arg
)
1653 switch (insn
->opcode
) {
1655 case OP_SET_LT
: case OP_SET_GT
:
1656 case OP_SET_B
: case OP_SET_A
:
1657 if (Wtautological_compare
)
1658 warning(insn
->pos
, "self-comparison always evaluates to false");
1661 return replace_with_value(insn
, 0);
1664 case OP_SET_LE
: case OP_SET_GE
:
1665 case OP_SET_BE
: case OP_SET_AE
:
1666 if (Wtautological_compare
)
1667 warning(insn
->pos
, "self-comparison always evaluates to true");
1668 return replace_with_value(insn
, 1);
1672 return replace_with_pseudo(insn
, arg
);
1681 static int simplify_binop(struct instruction
*insn
)
1683 if (constant(insn
->src1
)) {
1684 if (constant(insn
->src2
))
1685 return simplify_constant_binop(insn
);
1686 return simplify_constant_leftside(insn
);
1688 if (constant(insn
->src2
))
1689 return simplify_constant_rightside(insn
);
1690 if (insn
->src1
== insn
->src2
)
1691 return simplify_binop_same_args(insn
, insn
->src1
);
1695 static int switch_pseudo(struct instruction
*insn1
, pseudo_t
*pp1
, struct instruction
*insn2
, pseudo_t
*pp2
)
1697 pseudo_t p1
= *pp1
, p2
= *pp2
;
1699 use_pseudo(insn1
, p2
, pp1
);
1700 use_pseudo(insn2
, p1
, pp2
);
1701 remove_usage(p1
, pp1
);
1702 remove_usage(p2
, pp2
);
1707 // check if the given pseudos are in canonical order
1709 // The canonical order is VOID < UNDEF < PHI < REG < ARG < SYM < VAL
1710 // The rationale is:
1711 // * VALs at right (they don't need a definition)
1712 // * REGs at left (they need a defining instruction)
1713 // * SYMs & ARGs between REGs & VALs
1714 // * REGs & ARGs are ordered between themselves by their internal number
1715 // * SYMs are ordered between themselves by address
1716 // * VOID, UNDEF and PHI are uninteresting (but VOID should have type 0)
1717 static int canonical_order(pseudo_t p1
, pseudo_t p2
)
1722 /* symbol/constants on the right */
1729 return p1
->sym
<= p2
->sym
;
1732 return p1
->nr
<= p2
->nr
;
1738 static int canonicalize_commutative(struct instruction
*insn
)
1740 if (canonical_order(insn
->src1
, insn
->src2
))
1743 switch_pseudo(insn
, &insn
->src1
, insn
, &insn
->src2
);
1744 return repeat_phase
|= REPEAT_CSE
;
1747 static int canonicalize_compare(struct instruction
*insn
)
1749 if (canonical_order(insn
->src1
, insn
->src2
))
1752 switch_pseudo(insn
, &insn
->src1
, insn
, &insn
->src2
);
1753 insn
->opcode
= opcode_table
[insn
->opcode
].swap
;
1754 return repeat_phase
|= REPEAT_CSE
;
1757 static inline int simple_pseudo(pseudo_t pseudo
)
1759 return pseudo
->type
== PSEUDO_VAL
|| pseudo
->type
== PSEUDO_SYM
;
1763 // test if, in the given BB, the ordering of 2 instructions
1764 static bool insn_before(struct basic_block
*bb
, struct instruction
*x
, struct instruction
*y
)
1766 struct instruction
*insn
;
1768 FOR_EACH_PTR(bb
->insns
, insn
) {
1773 } END_FOR_EACH_PTR(insn
);
1778 // check if it safe for a pseudo to be used by an instruction
1779 static inline bool can_move_to(pseudo_t src
, struct instruction
*dst
)
1781 struct basic_block
*bbs
, *bbd
;
1782 struct instruction
*def
;
1784 if (!one_use(dst
->target
))
1786 if (src
->type
!= PSEUDO_REG
)
1795 return insn_before(bbs
, def
, dst
);
1797 return domtree_dominates(bbs
, bbd
);
1800 static int simplify_associative_binop(struct instruction
*insn
)
1802 struct instruction
*def
;
1803 pseudo_t pseudo
= insn
->src1
;
1805 if (!simple_pseudo(insn
->src2
))
1807 if (pseudo
->type
!= PSEUDO_REG
)
1812 if (def
->opcode
!= insn
->opcode
)
1814 if (!simple_pseudo(def
->src2
))
1816 if (constant(def
->src2
) && constant(insn
->src2
)) {
1817 // (x # C) # K --> x # eval(C # K)
1818 insn
->src2
= eval_op(insn
->opcode
, insn
->size
, insn
->src2
, def
->src2
);
1819 return replace_pseudo(insn
, &insn
->src1
, def
->src1
);
1821 if (!one_use(def
->target
))
1823 switch_pseudo(def
, &def
->src1
, insn
, &insn
->src2
);
1827 static int simplify_add_one_side(struct instruction
*insn
, pseudo_t
*p1
, pseudo_t
*p2
)
1829 struct instruction
*defr
= NULL
;
1830 struct instruction
*def
;
1831 pseudo_t src1
= *p1
;
1832 pseudo_t src2
= *p2
;
1834 switch (DEF_OPCODE(def
, src1
)) {
1836 if (DEF_OPCODE(defr
, *p2
) == OP_MUL
) {
1837 if (defr
->src2
== def
->src2
&& can_move_to(def
->src2
, defr
)) {
1838 // ((x * z) + (y * z)) into ((x + y) * z)
1839 swap_insn(insn
, defr
, def
->src1
, defr
->src1
, def
->src2
);
1842 if (defr
->src1
== def
->src1
&& can_move_to(def
->src1
, defr
)) {
1843 // ((z * x) + (z * y)) into ((x + y) * z)
1844 swap_insn(insn
, defr
, def
->src2
, defr
->src2
, def
->src1
);
1847 if (defr
->src1
== def
->src2
&& can_move_to(def
->src1
, defr
)) {
1848 // ((x * z) + (z * y)) into ((x + y) * z)
1849 swap_insn(insn
, defr
, def
->src1
, defr
->src2
, def
->src2
);
1855 case OP_NEG
: // (-x + y) --> (y - x)
1856 return replace_binop(insn
, OP_SUB
, &insn
->src1
, src2
, &insn
->src2
, def
->src
);
1859 if (def
->src2
== src2
) // (x - y) + y --> x
1860 return replace_with_pseudo(insn
, def
->src1
);
1866 static int simplify_add(struct instruction
*insn
)
1868 return simplify_add_one_side(insn
, &insn
->src1
, &insn
->src2
) ||
1869 simplify_add_one_side(insn
, &insn
->src2
, &insn
->src1
);
1872 static int simplify_sub(struct instruction
*insn
)
1874 pseudo_t src1
= insn
->src1
;
1875 pseudo_t src2
= insn
->src2
;
1876 struct instruction
*def
;
1878 switch (DEF_OPCODE(def
, src1
)) {
1880 if (def
->src1
== src2
) // (x + y) - x --> y
1881 return replace_with_pseudo(insn
, def
->src2
);
1882 if (def
->src2
== src2
) // (x + y) - y --> x
1883 return replace_with_pseudo(insn
, def
->src1
);
1887 switch (DEF_OPCODE(def
, src2
)) {
1889 if (src1
== def
->src1
) // x - (x + z) --> -z
1890 return replace_with_unop(insn
, OP_NEG
, def
->src2
);
1891 if (src1
== def
->src2
) // x - (y + x) --> -y
1892 return replace_with_unop(insn
, OP_NEG
, def
->src1
);
1894 case OP_NEG
: // (x - -y) --> (x + y)
1895 insn
->opcode
= OP_ADD
;
1896 return replace_pseudo(insn
, &insn
->src2
, def
->src
);
1902 static int simplify_compare(struct instruction
*insn
)
1904 pseudo_t src1
= insn
->src1
;
1905 pseudo_t src2
= insn
->src2
;
1906 struct instruction
*def
= NULL
;
1910 switch (DEF_OPCODE(def
, src1
)) {
1911 case OP_SEXT
: case OP_ZEXT
:
1912 osize
= def
->orig_type
->bit_size
;
1913 if ((src
= is_same_op(src2
, def
->opcode
, osize
))) {
1914 const struct opcode_table
*op
= &opcode_table
[insn
->opcode
];
1915 if ((def
->opcode
== OP_ZEXT
) && (op
->flags
& OPF_SIGNED
))
1916 insn
->opcode
= op
->sign
;
1917 insn
->itype
= def
->orig_type
;
1918 replace_pseudo(insn
, &insn
->src1
, def
->src
);
1919 return replace_pseudo(insn
, &insn
->src2
, src
);
1926 static int simplify_and_one_side(struct instruction
*insn
, pseudo_t
*p1
, pseudo_t
*p2
)
1928 struct instruction
*def
, *defr
= NULL
;
1929 pseudo_t src1
= *p1
;
1931 switch (DEF_OPCODE(def
, src1
)) {
1933 if (def
->src
== *p2
)
1934 return replace_with_value(insn
, 0);
1936 case OP_BINCMP
... OP_BINCMP_END
:
1937 if (DEF_OPCODE(defr
, *p2
) == opcode_negate(def
->opcode
)) {
1938 if (def
->src1
== defr
->src1
&& def
->src2
== defr
->src2
)
1939 return replace_with_value(insn
, 0);
1941 if (def
->opcode
== OP_SET_GE
&& is_zero(def
->src2
)) {
1942 switch (DEF_OPCODE(defr
, *p2
)) {
1944 if (!is_positive(defr
->src2
, defr
->itype
->bit_size
))
1946 // (x >= 0) && (x <= C) --> (x u<= C)
1947 insn
->itype
= defr
->itype
;
1948 replace_binop(insn
, OP_SET_BE
, &insn
->src1
, defr
->src1
, &insn
->src2
, defr
->src2
);
1954 if (DEF_OPCODE(defr
, *p2
) == OP_OR
) {
1955 if (defr
->src2
== def
->src2
&& can_move_to(def
->src2
, defr
)) {
1956 // ((x | z) & (y | z)) into ((x & y) | z)
1957 swap_insn(insn
, defr
, def
->src1
, defr
->src1
, def
->src2
);
1960 if (defr
->src1
== def
->src1
&& can_move_to(def
->src1
, defr
)) {
1961 // ((z | x) & (z | y)) into ((x & y) | z)
1962 swap_insn(insn
, defr
, def
->src2
, defr
->src2
, def
->src1
);
1965 if (defr
->src1
== def
->src2
&& can_move_to(def
->src1
, defr
)) {
1966 // ((x | z) & (z | y)) into ((x & y) | z)
1967 swap_insn(insn
, defr
, def
->src1
, defr
->src2
, def
->src2
);
1972 case OP_SHL
: case OP_LSR
: case OP_ASR
:
1973 if (DEF_OPCODE(defr
, *p2
) == def
->opcode
&& defr
->src2
== def
->src2
) {
1974 if (can_move_to(def
->src1
, defr
)) {
1975 // SHIFT(x, s) & SHIFT(y, s) --> SHIFT((x & y), s)
1976 swap_insn(insn
, defr
, def
->src1
, defr
->src1
, def
->src2
);
1985 static int simplify_and(struct instruction
*insn
)
1987 return simplify_and_one_side(insn
, &insn
->src1
, &insn
->src2
) ||
1988 simplify_and_one_side(insn
, &insn
->src2
, &insn
->src1
);
1991 static int simplify_ior_one_side(struct instruction
*insn
, pseudo_t
*p1
, pseudo_t
*p2
)
1993 struct instruction
*def
, *defr
= NULL
;
1994 pseudo_t src1
= *p1
;
1996 switch (DEF_OPCODE(def
, src1
)) {
1998 if (DEF_OPCODE(defr
, *p2
) == OP_AND
) {
1999 if (defr
->src2
== def
->src2
&& can_move_to(def
->src2
, defr
)) {
2000 // ((x & z) | (y & z)) into ((x | y) & z)
2001 swap_insn(insn
, defr
, def
->src1
, defr
->src1
, def
->src2
);
2004 if (defr
->src1
== def
->src1
&& can_move_to(def
->src1
, defr
)) {
2005 // ((z & x) | (z & y)) into ((x | y) & z)
2006 swap_insn(insn
, defr
, def
->src2
, defr
->src2
, def
->src1
);
2009 if (defr
->src1
== def
->src2
&& can_move_to(def
->src1
, defr
)) {
2010 // ((x & z) | (z & y)) into ((x | y) & z)
2011 swap_insn(insn
, defr
, def
->src1
, defr
->src2
, def
->src2
);
2017 if (def
->src
== *p2
)
2018 return replace_with_value(insn
, bits_mask(insn
->size
));
2020 case OP_BINCMP
... OP_BINCMP_END
:
2021 if (DEF_OPCODE(defr
, *p2
) == opcode_negate(def
->opcode
)) {
2022 if (def
->src1
== defr
->src1
&& def
->src2
== defr
->src2
)
2023 return replace_with_value(insn
, 1);
2026 case OP_SHL
: case OP_LSR
: case OP_ASR
:
2027 if (DEF_OPCODE(defr
, *p2
) == def
->opcode
&& defr
->src2
== def
->src2
) {
2028 if (can_move_to(def
->src1
, defr
)) {
2029 // SHIFT(x, s) | SHIFT(y, s) --> SHIFT((x | y), s)
2030 swap_insn(insn
, defr
, def
->src1
, defr
->src1
, def
->src2
);
2039 static int simplify_ior(struct instruction
*insn
)
2041 return simplify_ior_one_side(insn
, &insn
->src1
, &insn
->src2
) ||
2042 simplify_ior_one_side(insn
, &insn
->src2
, &insn
->src1
);
2045 static int simplify_xor_one_side(struct instruction
*insn
, pseudo_t
*p1
, pseudo_t
*p2
)
2047 struct instruction
*def
, *defr
= NULL
;
2048 pseudo_t src1
= *p1
;
2050 switch (DEF_OPCODE(def
, src1
)) {
2052 if (DEF_OPCODE(defr
, *p2
) == OP_AND
) {
2053 if (defr
->src2
== def
->src2
&& can_move_to(def
->src2
, defr
)) {
2054 // ((x & z) ^ (y & z)) into ((x ^ y) & z)
2055 swap_insn(insn
, defr
, def
->src1
, defr
->src1
, def
->src2
);
2058 if (defr
->src1
== def
->src1
&& can_move_to(def
->src1
, defr
)) {
2059 // ((z & x) ^ (z & y)) into ((x ^ y) & z)
2060 swap_insn(insn
, defr
, def
->src2
, defr
->src2
, def
->src1
);
2063 if (defr
->src1
== def
->src2
&& can_move_to(def
->src1
, defr
)) {
2064 // ((x & z) ^ (z & y)) into ((x ^ y) & z)
2065 swap_insn(insn
, defr
, def
->src1
, defr
->src2
, def
->src2
);
2071 if (def
->src
== *p2
)
2072 return replace_with_value(insn
, bits_mask(insn
->size
));
2074 case OP_BINCMP
... OP_BINCMP_END
:
2075 if (DEF_OPCODE(defr
, *p2
) == opcode_negate(def
->opcode
)) {
2076 if (def
->src1
== defr
->src1
&& def
->src2
== defr
->src2
)
2077 return replace_with_value(insn
, 1);
2080 case OP_SHL
: case OP_LSR
: case OP_ASR
:
2081 if (DEF_OPCODE(defr
, *p2
) == def
->opcode
&& defr
->src2
== def
->src2
) {
2082 if (can_move_to(def
->src1
, defr
)) {
2083 // SHIFT(x, s) ^ SHIFT(y, s) --> SHIFT((x ^ y), s)
2084 swap_insn(insn
, defr
, def
->src1
, defr
->src1
, def
->src2
);
2093 static int simplify_xor(struct instruction
*insn
)
2095 return simplify_xor_one_side(insn
, &insn
->src1
, &insn
->src2
) ||
2096 simplify_xor_one_side(insn
, &insn
->src2
, &insn
->src1
);
2099 static int simplify_constant_unop(struct instruction
*insn
)
2101 long long val
= insn
->src1
->value
;
2102 long long res
, mask
;
2104 switch (insn
->opcode
) {
2112 mask
= 1ULL << (insn
->orig_type
->bit_size
-1);
2114 val
|= ~(mask
| (mask
-1));
2123 mask
= 1ULL << (insn
->size
-1);
2124 res
&= mask
| (mask
-1);
2126 return replace_with_value(insn
, res
);
2129 static int simplify_unop(struct instruction
*insn
)
2131 struct instruction
*def
;
2132 pseudo_t src
= insn
->src
;
2135 return simplify_constant_unop(insn
);
2137 switch (insn
->opcode
) {
2139 switch (DEF_OPCODE(def
, src
)) {
2141 if (!constant(def
->src2
))
2143 insn
->opcode
= OP_SUB
; // ~(x + C) --> ~C - x
2144 src
= eval_unop(OP_NOT
, insn
->size
, def
->src2
);
2145 use_pseudo(insn
, def
->src1
, &insn
->src2
);
2146 return replace_pseudo(insn
, &insn
->src1
, src
);
2148 insn
->opcode
= OP_SUB
; // ~(-x) --> x - 1
2149 insn
->src2
= value_pseudo(1);
2150 return replace_pseudo(insn
, &insn
->src1
, def
->src
);
2151 case OP_NOT
: // ~(~x) --> x
2152 return replace_with_pseudo(insn
, def
->src
);
2154 if (!constant(def
->src1
))
2156 insn
->opcode
= OP_ADD
; // ~(C - x) --> x + ~C
2157 insn
->src2
= eval_unop(OP_NOT
, insn
->size
, def
->src1
);
2158 return replace_pseudo(insn
, &insn
->src1
, def
->src2
);
2160 if (!constant(def
->src2
))
2162 insn
->opcode
= OP_XOR
; // ~(x ^ C) --> x ^ ~C
2163 insn
->src2
= eval_unop(OP_NOT
, insn
->size
, def
->src2
);
2164 return replace_pseudo(insn
, &insn
->src1
, def
->src1
);
2168 switch (DEF_OPCODE(def
, src
)) {
2170 if (!constant(def
->src2
))
2172 insn
->opcode
= OP_SUB
; // -(x + C) --> (-C - x)
2173 src
= eval_unop(OP_NEG
, insn
->size
, def
->src2
);
2174 use_pseudo(insn
, def
->src1
, &insn
->src2
);
2175 return replace_pseudo(insn
, &insn
->src1
, src
);
2176 case OP_NEG
: // -(-x) --> x
2177 return replace_with_pseudo(insn
, def
->src
);
2179 insn
->opcode
= OP_ADD
; // -(~x) --> x + 1
2180 insn
->src2
= value_pseudo(1);
2181 return replace_pseudo(insn
, &insn
->src1
, def
->src
);
2183 insn
->opcode
= OP_SUB
; // -(x - y) --> y - x
2184 use_pseudo(insn
, def
->src1
, &insn
->src2
);
2185 return replace_pseudo(insn
, &insn
->src1
, def
->src2
);
2194 static int simplify_one_memop(struct instruction
*insn
, pseudo_t orig
)
2196 pseudo_t addr
= insn
->src
;
2199 if (addr
->type
== PSEUDO_REG
) {
2200 struct instruction
*def
= addr
->def
;
2201 if (def
->opcode
== OP_SYMADDR
&& def
->src
) {
2202 kill_use(&insn
->src
);
2203 use_pseudo(insn
, def
->src
, &insn
->src
);
2206 if (def
->opcode
== OP_ADD
) {
2222 if (new == orig
|| new == addr
) {
2226 * If some BB have been removed it is possible that this
2227 * memop is in fact part of a dead BB. In this case
2228 * we must not warn since nothing is wrong.
2229 * If not part of a dead BB this will be redone after
2230 * the BBs have been cleaned up.
2232 if (repeat_phase
& REPEAT_CFG_CLEANUP
)
2234 warning(insn
->pos
, "crazy programmer");
2235 replace_pseudo(insn
, &insn
->src
, VOID
);
2238 insn
->offset
+= off
->value
;
2239 replace_pseudo(insn
, &insn
->src
, new);
2244 // simplify memops instructions
2246 // :note: We walk the whole chain of adds/subs backwards.
2247 // That's not only more efficient, but it allows us to find loops.
2248 static int simplify_memop(struct instruction
*insn
)
2251 pseudo_t orig
= insn
->src
;
2254 one
= simplify_one_memop(insn
, orig
);
2260 static int simplify_cast(struct instruction
*insn
)
2262 unsigned long long mask
;
2263 struct instruction
*def
, *def2
;
2264 pseudo_t src
= insn
->src
;
2269 /* A cast of a constant? */
2271 return simplify_constant_unop(insn
);
2273 // can merge with the previous instruction?
2276 switch (def_opcode(src
)) {
2279 if (val
->type
!= PSEUDO_VAL
)
2281 /* A cast of a AND might be a no-op.. */
2282 switch (insn
->opcode
) {
2286 def
->opcode
= OP_TRUNC
;
2287 def
->orig_type
= def
->type
;
2288 def
->type
= insn
->type
;
2291 insn
->opcode
= OP_AND
;
2293 mask
&= (1ULL << size
) - 1;
2294 insn
->src2
= value_pseudo(mask
);
2298 if (val
->value
& (1 << (def
->size
- 1)))
2300 // OK, sign bit is 0
2305 // and.n %b <- %a, M
2306 // *ext.m %c <- (n) %b
2309 // and.m %c <- %b, M
2310 // For ZEXT, the mask will always be small
2311 // enough. For SEXT, it can only be done if
2312 // the mask force the sign bit to 0.
2313 def
->opcode
= OP_ZEXT
;
2314 def
->orig_type
= insn
->orig_type
;
2315 def
->type
= insn
->type
;
2316 def
->size
= insn
->size
;
2317 insn
->opcode
= OP_AND
;
2322 case OP_FPCMP
... OP_BINCMP_END
:
2323 switch (insn
->opcode
) {
2325 if (insn
->size
== 1)
2331 // setcc.n %t <- %a, %b
2332 // zext.m %r <- (n) %t
2334 // setcc.m %r <- %a, %b
2335 // and same for s/zext/trunc/
2336 insn
->opcode
= def
->opcode
;
2337 insn
->itype
= def
->itype
;
2338 use_pseudo(insn
, def
->src2
, &insn
->src2
);
2339 return replace_pseudo(insn
, &insn
->src1
, def
->src1
);
2343 switch (insn
->opcode
) {
2346 // TRUNC(NOT(x)) --> NOT(TRUNC(x))
2347 insn
->opcode
= OP_NOT
;
2348 def
->orig_type
= def
->type
;
2349 def
->opcode
= OP_TRUNC
;
2350 def
->type
= insn
->type
;
2351 def
->size
= insn
->size
;
2358 switch (insn
->opcode
) {
2360 mask
= bits_mask(insn
->size
);
2361 return simplify_mask_or(insn
, mask
, def
);
2366 if (insn
->opcode
!= OP_TRUNC
)
2368 mask
= bits_mask(insn
->size
);
2369 return simplify_mask_shift(def
, mask
);
2371 switch (insn
->opcode
) {
2373 insn
->orig_type
= def
->orig_type
;
2374 return replace_pseudo(insn
, &insn
->src1
, def
->src
);
2376 if (size
!= def
->orig_type
->bit_size
)
2378 if (DEF_OPCODE(def2
, def
->src
) != OP_LSR
)
2380 if (def2
->src2
!= value_pseudo(size
- def
->size
))
2382 // SEXT(TRUNC(LSR(x, N))) --> ASR(x, N)
2383 insn
->opcode
= OP_ASR
;
2384 insn
->src2
= def2
->src2
;
2385 return replace_pseudo(insn
, &insn
->src1
, def2
->src1
);
2387 if (size
!= def
->orig_type
->bit_size
)
2389 insn
->opcode
= OP_AND
;
2390 insn
->src2
= value_pseudo((1ULL << def
->size
) - 1);
2391 return replace_pseudo(insn
, &insn
->src1
, def
->src
);
2395 switch (insn
->opcode
) {
2397 insn
->opcode
= OP_ZEXT
;
2400 insn
->orig_type
= def
->orig_type
;
2401 return replace_pseudo(insn
, &insn
->src
, def
->src
);
2405 switch (insn
->opcode
) {
2407 osize
= def
->orig_type
->bit_size
;
2409 return replace_with_pseudo(insn
, def
->src
);
2411 insn
->opcode
= def
->opcode
;
2412 insn
->orig_type
= def
->orig_type
;
2413 return replace_pseudo(insn
, &insn
->src
, def
->src
);
2415 switch (insn
->opcode
) {
2417 insn
->orig_type
= def
->orig_type
;
2418 return replace_pseudo(insn
, &insn
->src
, def
->src
);
2426 static int simplify_select(struct instruction
*insn
)
2428 pseudo_t cond
, src1
, src2
;
2429 struct instruction
*def
;
2436 return replace_with_pseudo(insn
, cond
->value
? src1
: src2
);
2438 return replace_with_pseudo(insn
, src1
);
2440 if (constant(src1
) && constant(src2
)) {
2441 long long val1
= src1
->value
;
2442 long long val2
= src2
->value
;
2444 /* The pair 0/1 is special - replace with SETNE/SETEQ */
2445 if ((val1
| val2
) == 1) {
2446 int opcode
= OP_SET_EQ
;
2451 insn
->opcode
= opcode
;
2452 insn
->itype
= insn
->type
;
2453 /* insn->src1 is already cond */
2454 insn
->src2
= src1
; /* Zero */
2458 if (cond
== src2
&& is_zero(src1
)) // SEL(x, 0, x) --> 0
2459 return replace_with_pseudo(insn
, src1
);
2460 if (cond
== src1
&& is_zero(src2
)) // SEL(x, x, 0) --> x
2461 return replace_with_pseudo(insn
, cond
);
2463 switch (DEF_OPCODE(def
, cond
)) {
2465 if (src1
== def
->src1
&& src2
== def
->src2
)
2466 return replace_with_pseudo(insn
, src2
); // SEL(x==y,x,y) --> y
2467 if (src2
== def
->src1
&& src1
== def
->src2
)
2468 return replace_with_pseudo(insn
, src2
); // SEL(y==x,x,y) --> y
2471 if (src1
== def
->src1
&& src2
== def
->src2
)
2472 return replace_with_pseudo(insn
, src1
); // SEL(x!=y,x,y) --> x
2473 if (src2
== def
->src1
&& src1
== def
->src2
)
2474 return replace_with_pseudo(insn
, src1
); // SEL(y!=x,x,y) --> x
2476 case OP_SET_LE
: case OP_SET_LT
:
2477 case OP_SET_BE
: case OP_SET_B
:
2480 // SEL(x {<,<=} y, a, b) --> SEL(x {>=,>} y, b, a)
2481 def
->opcode
= opcode_negate(def
->opcode
);
2482 return switch_pseudo(insn
, &insn
->src2
, insn
, &insn
->src3
);
2484 if (one_use(cond
) && is_zero(def
->src2
)) {
2485 if (is_negate_of(src2
, src1
))
2486 // SEL(x > 0, a, -a) --> SEL(x >= 0, a, -a)
2487 return replace_opcode(def
, OP_SET_GE
);
2491 if (constant(def
->src2
) && constant(def
->src3
)) {
2492 // Is the def of the conditional another select?
2493 // And if that one results in a "zero or not", use the
2494 // original conditional instead.
2495 // SEL(SEL(x, C, 0), y, z) --> SEL(x, y, z)
2496 // SEL(SEL(x, C, 0), C, 0) --> SEL(x, C, 0) == cond
2497 // SEL(SEL(x, 0, C), y, z) --> SEL(x, z, y)
2498 // SEL(SEL(x, C1, C2), y, z) --> y
2499 if (!def
->src3
->value
) {
2500 if ((src1
== def
->src2
) && (src2
== def
->src3
))
2501 return replace_with_pseudo(insn
, cond
);
2502 return replace_pseudo(insn
, &insn
->cond
, def
->cond
);
2504 if (!def
->src2
->value
) {
2505 switch_pseudo(insn
, &insn
->src2
, insn
, &insn
->src3
);
2506 return replace_pseudo(insn
, &insn
->cond
, def
->cond
);
2508 // both values must be non-zero
2509 return replace_with_pseudo(insn
, src1
);
2512 if (is_pow2(def
->src2
) && is_pow2(src1
) && is_zero(src2
) && insn
->size
== def
->size
&& one_use(cond
)) {
2513 unsigned s1
= log2_exact(def
->src2
->value
);
2514 unsigned s2
= log2_exact(insn
->src2
->value
);
2518 return replace_with_pseudo(insn
, cond
);
2520 // SEL(x & A, B, 0) --> SHIFT(x & A, S)
2521 insn
->opcode
= (s1
< s2
) ? OP_SHL
: OP_LSR
;
2522 shift
= (s1
< s2
) ? (s2
- s1
) : (s1
- s2
);
2523 insn
->src2
= value_pseudo(shift
);
2529 switch (DEF_OPCODE(def
, src1
)) {
2530 case OP_ADD
: case OP_OR
: case OP_XOR
:
2531 if ((def
->src1
== src2
) && can_move_to(cond
, def
)) {
2532 // SEL(x, OP(y,z), y) --> OP(SEL(x, z, 0), y)
2533 swap_select(insn
, def
, cond
, def
->src2
, value_pseudo(0), src2
);
2536 if ((def
->src2
== src2
) && can_move_to(cond
, def
)) {
2537 // SEL(x, OP(z,y), y) --> OP(SEL(x, z, 0), y)
2538 swap_select(insn
, def
, cond
, def
->src1
, value_pseudo(0), src2
);
2544 switch (DEF_OPCODE(def
, src2
)) {
2545 case OP_ADD
: case OP_OR
: case OP_XOR
:
2546 if ((def
->src1
== src1
) && can_move_to(cond
, def
)) {
2547 // SEL(x, y, OP(y,z)) --> OP(SEL(x, 0, z), y)
2548 swap_select(insn
, def
, cond
, value_pseudo(0), def
->src2
, src1
);
2551 if ((def
->src2
== src1
) && can_move_to(cond
, def
)) {
2552 // SEL(x, y, OP(z,y)) --> OP(SEL(x, 0, z), y)
2553 swap_select(insn
, def
, cond
, value_pseudo(0), def
->src1
, src1
);
2561 static int is_in_range(pseudo_t src
, long long low
, long long high
)
2565 switch (src
->type
) {
2568 return value
>= low
&& value
<= high
;
2574 static int simplify_range(struct instruction
*insn
)
2576 pseudo_t src1
, src2
, src3
;
2581 if (src2
->type
!= PSEUDO_VAL
|| src3
->type
!= PSEUDO_VAL
)
2583 if (is_in_range(src1
, src2
->value
, src3
->value
)) {
2584 kill_instruction(insn
);
2591 // simplify SET_NE/EQ $0 + BR
2592 static int simplify_cond_branch(struct instruction
*br
, struct instruction
*def
, pseudo_t newcond
)
2594 replace_pseudo(br
, &br
->cond
, newcond
);
2595 if (def
->opcode
== OP_SET_EQ
) {
2596 struct basic_block
*tmp
= br
->bb_true
;
2597 br
->bb_true
= br
->bb_false
;
2603 static int simplify_branch(struct instruction
*insn
)
2605 pseudo_t cond
= insn
->cond
;
2607 /* Constant conditional */
2609 return convert_to_jump(insn
, cond
->value
? insn
->bb_true
: insn
->bb_false
);
2612 if (insn
->bb_true
== insn
->bb_false
)
2613 return convert_to_jump(insn
, insn
->bb_true
);
2615 /* Conditional on a SETNE $0 or SETEQ $0 */
2616 if (cond
->type
== PSEUDO_REG
) {
2617 struct instruction
*def
= cond
->def
;
2619 if (def
->opcode
== OP_SET_NE
|| def
->opcode
== OP_SET_EQ
) {
2620 if (constant(def
->src1
) && !def
->src1
->value
)
2621 return simplify_cond_branch(insn
, def
, def
->src2
);
2622 if (constant(def
->src2
) && !def
->src2
->value
)
2623 return simplify_cond_branch(insn
, def
, def
->src1
);
2625 if (def
->opcode
== OP_SEL
) {
2626 if (constant(def
->src2
) && constant(def
->src3
)) {
2627 long long val1
= def
->src2
->value
;
2628 long long val2
= def
->src3
->value
;
2630 return convert_to_jump(insn
, insn
->bb_false
);
2632 return convert_to_jump(insn
, insn
->bb_true
);
2634 struct basic_block
*tmp
= insn
->bb_true
;
2635 insn
->bb_true
= insn
->bb_false
;
2636 insn
->bb_false
= tmp
;
2638 return replace_pseudo(insn
, &insn
->cond
, def
->src1
);
2641 if (def
->opcode
== OP_SEXT
|| def
->opcode
== OP_ZEXT
)
2642 return replace_pseudo(insn
, &insn
->cond
, def
->src
);
2647 static int simplify_switch(struct instruction
*insn
)
2649 pseudo_t cond
= insn
->cond
;
2651 struct multijmp
*jmp
;
2653 if (!constant(cond
))
2655 val
= insn
->cond
->value
;
2657 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
2659 if (jmp
->begin
> jmp
->end
)
2661 if (val
>= jmp
->begin
&& val
<= jmp
->end
)
2663 } END_FOR_EACH_PTR(jmp
);
2664 warning(insn
->pos
, "Impossible case statement");
2668 return convert_to_jump(insn
, jmp
->target
);
2671 static struct basic_block
*is_label(pseudo_t pseudo
)
2673 struct instruction
*def
;
2675 if (DEF_OPCODE(def
, pseudo
) != OP_LABEL
)
2677 return def
->bb_true
;
2680 static int simplify_cgoto(struct instruction
*insn
)
2682 struct basic_block
*target
, *bb
= insn
->bb
;
2683 struct basic_block
*bbt
, *bbf
;
2684 struct instruction
*def
;
2685 struct multijmp
*jmp
;
2687 switch (DEF_OPCODE(def
, insn
->src
)) {
2688 case OP_SEL
: // CGOTO(SEL(x, L1, L2)) --> CBR x, L1, L2
2689 if ((bbt
= is_label(def
->src2
)) && (bbf
= is_label(def
->src3
))) {
2690 insn
->opcode
= OP_CBR
;
2691 insn
->bb_true
= bbt
;
2692 insn
->bb_false
= bbf
;
2693 return replace_pseudo(insn
, &insn
->src1
, def
->cond
);
2697 target
= def
->bb_true
;
2700 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
2701 if (jmp
->target
== target
)
2703 remove_bb_from_list(&jmp
->target
->parents
, bb
, 1);
2704 remove_bb_from_list(&bb
->children
, jmp
->target
, 1);
2705 DELETE_CURRENT_PTR(jmp
);
2706 } END_FOR_EACH_PTR(jmp
);
2707 kill_use(&insn
->src
);
2708 insn
->opcode
= OP_BR
;
2709 insn
->bb_true
= target
;
2710 return REPEAT_CSE
|REPEAT_CFG_CLEANUP
;
2715 static int simplify_setval(struct instruction
*insn
)
2717 struct expression
*val
= insn
->val
;
2719 switch (val
->type
) {
2721 insn
->opcode
= OP_LABEL
;
2722 insn
->bb_true
= val
->symbol
->bb_target
;
2730 int simplify_instruction(struct instruction
*insn
)
2735 flags
= opcode_table
[insn
->opcode
].flags
;
2736 if (flags
& OPF_TARGET
) {
2737 if (!has_users(insn
->target
))
2738 return kill_instruction(insn
);
2740 if (flags
& OPF_COMMU
)
2741 canonicalize_commutative(insn
) ;
2742 if (flags
& OPF_COMPARE
)
2743 canonicalize_compare(insn
) ;
2744 if (flags
& OPF_BINOP
) {
2745 if ((changed
= simplify_binop(insn
)))
2748 if (flags
& OPF_ASSOC
) {
2749 if ((changed
= simplify_associative_binop(insn
)))
2752 if (flags
& OPF_UNOP
) {
2753 if ((changed
= simplify_unop(insn
)))
2757 switch (insn
->opcode
) {
2758 case OP_ADD
: return simplify_add(insn
);
2759 case OP_SUB
: return simplify_sub(insn
);
2760 case OP_AND
: return simplify_and(insn
);
2761 case OP_OR
: return simplify_ior(insn
);
2762 case OP_XOR
: return simplify_xor(insn
);
2774 case OP_BINCMP
... OP_BINCMP_END
:
2775 return simplify_compare(insn
);
2778 return simplify_memop(insn
);
2780 return replace_with_pseudo(insn
, insn
->src
);
2781 case OP_SEXT
: case OP_ZEXT
:
2783 return simplify_cast(insn
);
2785 case OP_FCVTU
: case OP_FCVTS
:
2786 case OP_UCVTF
: case OP_SCVTF
:
2792 return replace_with_pseudo(insn
, insn
->src
);
2796 return simplify_setval(insn
);
2801 return clean_up_phi(insn
);
2805 return simplify_select(insn
);
2807 return simplify_branch(insn
);
2809 return simplify_switch(insn
);
2810 case OP_COMPUTEDGOTO
:
2811 return simplify_cgoto(insn
);
2813 return simplify_range(insn
);