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"
55 // find the trivial parent for a phi-source
56 static struct basic_block
*phi_parent(struct basic_block
*source
, pseudo_t pseudo
)
58 /* Can't go upwards if the pseudo is defined in the bb it came from.. */
59 if (pseudo
->type
== PSEUDO_REG
) {
60 struct instruction
*def
= pseudo
->def
;
61 if (def
->bb
== source
)
64 if (bb_list_size(source
->children
) != 1 || bb_list_size(source
->parents
) != 1)
66 return first_basic_block(source
->parents
);
70 // copy the phi-node's phisrcs into to given array
71 // @return: 0 if the the list contained the expected
72 // number of element, a positive number if there was
73 // more than expected and a negative one if less.
75 // :note: we can't reuse a function like linearize_ptr_list()
76 // because any VOIDs in the phi-list must be ignored here
77 // as in this context they mean 'entry has been removed'.
78 static int get_phisources(struct instruction
*sources
[], int nbr
, struct instruction
*insn
)
83 assert(insn
->opcode
== OP_PHI
);
84 FOR_EACH_PTR(insn
->phi_list
, phi
) {
85 struct instruction
*def
;
91 assert(def
->opcode
== OP_PHISOURCE
);
93 } END_FOR_EACH_PTR(phi
);
97 static int if_convert_phi(struct instruction
*insn
)
99 struct instruction
*array
[2];
100 struct basic_block
*parents
[3];
101 struct basic_block
*bb
, *bb1
, *bb2
, *source
;
102 struct instruction
*br
;
106 if (get_phisources(array
, 2, insn
))
108 if (linearize_ptr_list((struct ptr_list
*)bb
->parents
, (void **)parents
, 3) != 2)
110 p1
= array
[0]->phi_src
;
112 p2
= array
[1]->phi_src
;
115 /* Only try the simple "direct parents" case */
116 if ((bb1
!= parents
[0] || bb2
!= parents
[1]) &&
117 (bb1
!= parents
[1] || bb2
!= parents
[0]))
121 * See if we can find a common source for this..
123 source
= phi_parent(bb1
, p1
);
124 if (source
!= phi_parent(bb2
, p2
))
128 * Cool. We now know that 'source' is the exclusive
129 * parent of both phi-nodes, so the exit at the
130 * end of it fully determines which one it is, and
131 * we can turn it into a select.
133 * HOWEVER, right now we only handle regular
134 * conditional branches. No multijumps or computed
135 * stuff. Verify that here.
137 br
= last_instruction(source
->insns
);
138 if (!br
|| br
->opcode
!= OP_CBR
)
142 assert(br
->bb_false
);
145 * We're in business. Match up true/false with p1/p2.
147 if (br
->bb_true
== bb2
|| br
->bb_false
== bb1
) {
154 * OK, we can now replace that last
161 * select pseudo, p1, p2
164 * and remove the phi-node. If it then
165 * turns out that 'a' or 'b' is entirely
166 * empty (common case), and now no longer
167 * a phi-source, we'll be able to simplify
168 * the conditional branch too.
170 insert_select(source
, br
, insn
, p1
, p2
);
171 kill_instruction(insn
);
176 // detect trivial phi-nodes
177 // @insn: the phi-node
178 // @pseudo: the candidate resulting pseudo (NULL when starting)
179 // @return: the unique result if the phi-node is trivial, NULL otherwise
181 // A phi-node is trivial if it has a single possible result:
182 // * all operands are the same
183 // * the operands are themselves defined by a chain or cycle of phi-nodes
184 // and the set of all operands involved contains a single value
185 // not defined by these phi-nodes
187 // Since the result is unique, these phi-nodes can be removed.
188 static pseudo_t
trivial_phi(pseudo_t pseudo
, struct instruction
*insn
, struct pseudo_list
**list
)
190 pseudo_t target
= insn
->target
;
193 add_pseudo(list
, target
);
195 FOR_EACH_PTR(insn
->phi_list
, phi
) {
196 struct instruction
*def
;
204 src
= def
->phi_src
; // bypass OP_PHISRC & get the real source
215 if (DEF_OPCODE(def
, src
) == OP_PHI
) {
216 if (pseudo_in_list(*list
, src
))
218 if ((pseudo
= trivial_phi(pseudo
, def
, list
)))
222 } END_FOR_EACH_PTR(phi
);
224 return pseudo
? pseudo
: VOID
;
227 static int clean_up_phi(struct instruction
*insn
)
229 struct pseudo_list
*list
= NULL
;
232 if ((pseudo
= trivial_phi(NULL
, insn
, &list
))) {
233 convert_instruction_target(insn
, pseudo
);
234 kill_instruction(insn
);
238 return if_convert_phi(insn
);
241 static int delete_pseudo_user_list_entry(struct pseudo_user_list
**list
, pseudo_t
*entry
, int count
)
243 struct pseudo_user
*pu
;
245 FOR_EACH_PTR(*list
, pu
) {
246 if (pu
->userp
== entry
) {
247 MARK_CURRENT_DELETED(pu
);
251 } END_FOR_EACH_PTR(pu
);
254 if (pseudo_user_list_empty(*list
))
259 static inline void rem_usage(pseudo_t p
, pseudo_t
*usep
, int kill
)
261 if (has_use_list(p
)) {
262 if (p
->type
== PSEUDO_SYM
)
263 repeat_phase
|= REPEAT_SYMBOL_CLEANUP
;
264 delete_pseudo_user_list_entry(&p
->users
, usep
, 1);
265 if (kill
&& !p
->users
)
266 kill_instruction(p
->def
);
270 static inline void remove_usage(pseudo_t p
, pseudo_t
*usep
)
272 rem_usage(p
, usep
, 1);
275 void kill_use(pseudo_t
*usep
)
280 rem_usage(p
, usep
, 1);
284 // Like kill_use() but do not (recursively) kill dead instructions
285 void remove_use(pseudo_t
*usep
)
289 rem_usage(p
, usep
, 0);
292 static void kill_use_list(struct pseudo_list
*list
)
295 FOR_EACH_PTR(list
, p
) {
298 kill_use(THIS_ADDRESS(p
));
299 } END_FOR_EACH_PTR(p
);
303 // kill an instruction
304 // @insn: the instruction to be killed
305 // @force: if unset, the normal case, the instruction is not killed
306 // if not free of possible side-effect; if set the instruction
307 // is unconditionally killed.
309 // The killed instruction is removed from its BB and the usage
310 // of all its operands are removed. The instruction is also
311 // marked as killed by setting its ->bb to NULL.
312 int kill_insn(struct instruction
*insn
, int force
)
314 if (!insn
|| !insn
->bb
)
317 switch (insn
->opcode
) {
320 kill_use(&insn
->src3
);
323 case OP_BINARY
... OP_BINCMP_END
:
324 kill_use(&insn
->src2
);
327 case OP_UNOP
... OP_UNOP_END
:
330 kill_use(&insn
->src1
);
334 kill_use_list(insn
->phi_list
);
337 kill_use(&insn
->phi_src
);
341 kill_use(&insn
->src
);
342 repeat_phase
|= REPEAT_SYMBOL_CLEANUP
;
347 case OP_COMPUTEDGOTO
:
348 kill_use(&insn
->cond
);
353 /* a "pure" function can be killed too */
354 if (!(insn
->func
->type
== PSEUDO_SYM
))
356 if (!(insn
->func
->sym
->ctype
.modifiers
& MOD_PURE
))
359 kill_use_list(insn
->arguments
);
360 if (insn
->func
->type
== PSEUDO_REG
)
361 kill_use(&insn
->func
);
365 if (!force
&& insn
->is_volatile
)
367 kill_use(&insn
->src
);
373 kill_use(&insn
->src
);
374 kill_use(&insn
->target
);
388 return repeat_phase
|= REPEAT_CSE
;
392 // kill trivially dead instructions
393 static int dead_insn(struct instruction
*insn
, pseudo_t
*src1
, pseudo_t
*src2
, pseudo_t
*src3
)
395 if (has_users(insn
->target
))
405 static inline bool has_target(struct instruction
*insn
)
407 return opcode_table
[insn
->opcode
].flags
& OPF_TARGET
;
410 void remove_dead_insns(struct entrypoint
*ep
)
412 struct basic_block
*bb
;
414 FOR_EACH_PTR_REVERSE(ep
->bbs
, bb
) {
415 struct instruction
*insn
;
416 FOR_EACH_PTR_REVERSE(bb
->insns
, insn
) {
419 if (!has_target(insn
))
421 if (!has_users(insn
->target
))
422 kill_instruction(insn
);
423 } END_FOR_EACH_PTR_REVERSE(insn
);
424 } END_FOR_EACH_PTR_REVERSE(bb
);
427 static inline int constant(pseudo_t pseudo
)
429 return pseudo
->type
== PSEUDO_VAL
;
433 // replace the operand of an instruction
434 // @insn: the instruction
435 // @pp: the address of the instruction's operand
436 // @new: the new value for the operand
437 // @return: REPEAT_CSE.
438 static inline int replace_pseudo(struct instruction
*insn
, pseudo_t
*pp
, pseudo_t
new)
441 use_pseudo(insn
, new, pp
);
442 remove_usage(old
, pp
);
446 static int replace_with_pseudo(struct instruction
*insn
, pseudo_t pseudo
)
448 convert_instruction_target(insn
, pseudo
);
450 switch (insn
->opcode
) {
453 kill_use(&insn
->src3
);
454 case OP_BINARY
... OP_BINCMP_END
:
455 kill_use(&insn
->src2
);
456 case OP_UNOP
... OP_UNOP_END
:
458 kill_use(&insn
->src1
);
468 static inline int def_opcode(pseudo_t p
)
470 if (p
->type
!= PSEUDO_REG
)
472 return p
->def
->opcode
;
475 static unsigned int value_size(long long value
)
490 // try to determine the maximum size of bits in a pseudo
492 // Right now this only follow casts and constant values, but we
493 // could look at things like AND instructions, etc.
494 static unsigned int operand_size(struct instruction
*insn
, pseudo_t pseudo
)
496 unsigned int size
= insn
->size
;
498 if (pseudo
->type
== PSEUDO_REG
) {
499 struct instruction
*src
= pseudo
->def
;
500 if (src
&& src
->opcode
== OP_ZEXT
&& src
->orig_type
) {
501 unsigned int orig_size
= src
->orig_type
->bit_size
;
502 if (orig_size
< size
)
506 if (pseudo
->type
== PSEUDO_VAL
) {
507 unsigned int orig_size
= value_size(pseudo
->value
);
508 if (orig_size
< size
)
514 static pseudo_t
eval_insn(struct instruction
*insn
)
516 /* FIXME! Verify signs and sizes!! */
517 unsigned int size
= insn
->size
;
518 long long left
= insn
->src1
->value
;
519 long long right
= insn
->src2
->value
;
520 unsigned long long ul
, ur
;
521 long long res
, mask
, bits
;
523 mask
= 1ULL << (size
-1);
524 bits
= mask
| (mask
-1);
533 switch (insn
->opcode
) {
551 if (left
== mask
&& right
== -1)
563 if (left
== mask
&& right
== -1)
593 /* Binary comparison */
629 return value_pseudo(res
);
640 // try to simplify MASK(OR(AND(x, M'), b), M)
641 // @insn: the masking instruction
642 // @mask: the associated mask (M)
643 // @ora: one of the OR's operands, guaranteed to be PSEUDO_REG
644 // @orb: the other OR's operand
645 // @return: 0 if no changes have been made, one or more REPEAT_* flags otherwise.
646 static int simplify_mask_or_and(struct instruction
*insn
, unsigned long long mask
,
647 pseudo_t ora
, pseudo_t orb
)
649 unsigned long long omask
, nmask
;
650 struct instruction
*and = ora
->def
;
651 pseudo_t src2
= and->src2
;
653 if (and->opcode
!= OP_AND
)
658 nmask
= omask
& mask
;
660 // if (M' & M) == 0: ((a & M') | b) -> b
661 return replace_pseudo(insn
, &insn
->src1
, orb
);
663 if (multi_users(insn
->src1
))
664 return 0; // can't modify anything inside the OR
666 struct instruction
*or = insn
->src1
->def
;
667 pseudo_t
*arg
= (ora
== or->src1
) ? &or->src1
: &or->src2
;
668 // if (M' & M) == M: ((a & M') | b) -> (a | b)
669 return replace_pseudo(or, arg
, and->src1
);
671 if (nmask
!= omask
&& !multi_users(ora
)) {
672 // if (M' & M) != M': AND(a, M') -> AND(a, (M' & M))
673 and->src2
= value_pseudo(nmask
);
680 // try to simplify MASK(OR(a, b), M)
681 // @insn: the masking instruction
682 // @mask: the associated mask (M)
683 // @or: the OR instruction
684 // @return: 0 if no changes have been made, one or more REPEAT_* flags otherwise.
685 static int simplify_mask_or(struct instruction
*insn
, unsigned long long mask
, struct instruction
*or)
687 pseudo_t src1
= or->src1
;
688 pseudo_t src2
= or->src2
;
691 if (src1
->type
== PSEUDO_REG
) {
692 if ((rc
= simplify_mask_or_and(insn
, mask
, src1
, src2
)))
695 if (src2
->type
== PSEUDO_REG
) {
696 if ((rc
= simplify_mask_or_and(insn
, mask
, src2
, src1
)))
698 } else if (src2
->type
== PSEUDO_VAL
) {
699 unsigned long long oval
= src2
->value
;
700 unsigned long long nval
= oval
& mask
;
704 // if (C & M) == 0: OR(x, C) -> x
705 return replace_pseudo(insn
, &insn
->src1
, src1
);
708 // if (C & M) == M: OR(x, C) -> M
709 return replace_pseudo(insn
, &insn
->src1
, value_pseudo(mask
));
711 if (nval
!= oval
&& !multi_users(or->target
)) {
712 // if (C & M) != C: OR(x, C) -> OR(x, (C & M))
713 return replace_pseudo(or, &or->src2
, value_pseudo(nval
));
720 // try to simplify MASK(SHIFT(OR(a, b), S), M)
721 // @sh: the shift instruction
722 // @or: the OR instruction
723 // @mask: the mask associated to MASK (M):
724 // @return: 0 if no changes have been made, one or more REPEAT_* flags otherwise.
725 static int simplify_mask_shift_or(struct instruction
*sh
, struct instruction
*or, unsigned long long mask
)
727 unsigned long long smask
= bits_mask(sh
->size
);
728 int shift
= sh
->src2
->value
;
730 if (sh
->opcode
== OP_LSR
)
734 return simplify_mask_or(sh
, smask
& mask
, or);
737 static int simplify_mask_shift(struct instruction
*sh
, unsigned long long mask
)
739 struct instruction
*inner
;
741 if (!constant(sh
->src2
) || sh
->tainted
)
743 switch (DEF_OPCODE(inner
, sh
->src1
)) {
745 if (!multi_users(sh
->target
))
746 return simplify_mask_shift_or(sh
, inner
, mask
);
752 static long long check_shift_count(struct instruction
*insn
, unsigned long long uval
)
754 unsigned int size
= insn
->size
;
755 long long sval
= uval
;
760 sval
= sign_extend_safe(sval
, size
);
761 sval
= sign_extend_safe(sval
, bits_in_int
);
763 insn
->src2
= value_pseudo(sval
);
767 if (sval
< 0 && Wshift_count_negative
)
768 warning(insn
->pos
, "shift count is negative (%lld)", sval
);
769 if (sval
> 0 && Wshift_count_overflow
) {
770 struct symbol
*ctype
= insn
->type
;
772 if (ctype
->type
== SYM_NODE
)
773 ctype
= ctype
->ctype
.base_type
;
774 tname
= show_typename(ctype
);
775 warning(insn
->pos
, "shift too big (%llu) for type %s", sval
, tname
);
781 static int simplify_shift(struct instruction
*insn
, pseudo_t pseudo
, long long value
)
783 struct instruction
*def
;
784 unsigned long long mask
, omask
, nmask
;
785 unsigned long long nval
;
790 return replace_with_pseudo(insn
, pseudo
);
791 value
= check_shift_count(insn
, value
);
796 switch (insn
->opcode
) {
800 if (pseudo
->type
!= PSEUDO_REG
)
803 switch (def
->opcode
) {
806 if (def
== insn
) // cyclic DAG!
809 if (src2
->type
!= PSEUDO_VAL
)
812 if (nval
> insn
->size
|| nval
== 0)
815 if (def
->opcode
== OP_LSR
)
816 insn
->opcode
= OP_LSR
;
817 else if (value
>= size
)
823 // zext.N %t <- (O) %a
826 // zext.N %t <- (O) %a
828 insn
->opcode
= OP_LSR
;
833 size
= operand_size(insn
, pseudo
);
836 switch(DEF_OPCODE(def
, pseudo
)) {
838 // replace (A & M) >> S
839 // by (A >> S) & (M >> S)
840 if (!constant(def
->src2
))
842 mask
= bits_mask(insn
->size
- value
) << value
;
843 omask
= def
->src2
->value
;
844 nmask
= omask
& mask
;
846 return replace_with_pseudo(insn
, value_pseudo(0));
848 return replace_pseudo(insn
, &insn
->src1
, def
->src1
);
849 if (nbr_users(pseudo
) > 1)
851 def
->opcode
= OP_LSR
;
852 def
->src2
= insn
->src2
;
853 insn
->opcode
= OP_AND
;
854 insn
->src2
= value_pseudo(omask
>> value
);
857 goto case_shift_shift
;
859 mask
= bits_mask(size
);
860 return simplify_mask_shift_or(insn
, def
, mask
);
862 // replace ((x << S) >> S)
863 // by (x & (-1 >> S))
864 if (def
->src2
!= insn
->src2
)
866 mask
= bits_mask(insn
->size
- value
);
873 switch(DEF_OPCODE(def
, pseudo
)) {
875 // simplify (A & M) << S
876 if (!constant(def
->src2
))
878 mask
= bits_mask(insn
->size
) >> value
;
879 omask
= def
->src2
->value
;
880 nmask
= omask
& mask
;
882 return replace_with_pseudo(insn
, value_pseudo(0));
884 return replace_pseudo(insn
, &insn
->src1
, def
->src1
);
885 // do not simplify into ((A << S) & (M << S))
888 // replace ((x >> S) << S)
889 // by (x & (-1 << S))
890 if (def
->src2
!= insn
->src2
)
892 mask
= bits_mask(insn
->size
- value
) << value
;
895 mask
= bits_mask(size
);
896 return simplify_mask_shift_or(insn
, def
, mask
);
898 case_shift_shift
: // also for LSR - LSR
899 if (def
== insn
) // cyclic DAG!
902 if (src2
->type
!= PSEUDO_VAL
)
905 if (nval
> insn
->size
)
916 insn
->src2
= value_pseudo(value
);
917 return replace_pseudo(insn
, &insn
->src1
, pseudo
->def
->src1
);
920 return replace_with_pseudo(insn
, value_pseudo(0));
922 insn
->opcode
= OP_AND
;
923 insn
->src2
= value_pseudo(mask
);
924 return replace_pseudo(insn
, &insn
->src1
, def
->src1
);
927 static int simplify_mul_div(struct instruction
*insn
, long long value
)
929 unsigned long long sbit
= 1ULL << (insn
->size
- 1);
930 unsigned long long bits
= sbit
| (sbit
- 1);
933 return replace_with_pseudo(insn
, insn
->src1
);
935 switch (insn
->opcode
) {
938 return replace_with_pseudo(insn
, insn
->src2
);
941 if (!(value
& sbit
)) // positive
946 insn
->opcode
= OP_NEG
;
954 static int simplify_seteq_setne(struct instruction
*insn
, long long value
)
956 pseudo_t old
= insn
->src1
;
957 struct instruction
*def
;
962 if (value
!= 0 && value
!= 1)
965 if (old
->type
!= PSEUDO_REG
)
971 inverse
= (insn
->opcode
== OP_SET_NE
) == value
;
972 if (!inverse
&& def
->size
== 1 && insn
->size
== 1) {
974 // setne %r <- %s, $0
976 // seteq %r <- %s, $1
977 // by %s when boolean
978 return replace_with_pseudo(insn
, old
);
980 opcode
= def
->opcode
;
985 if (def
->size
!= insn
->size
)
987 if (def
->src2
->type
!= PSEUDO_VAL
)
989 if (def
->src2
->value
!= 1)
991 return replace_with_pseudo(insn
, old
);
992 case OP_FPCMP
... OP_BINCMP_END
:
994 // setcc.n %t <- %a, %b
995 // setne.m %r <- %t, $0
997 // setcc.n %t <- %a, %b
998 // setcc.m %r <- %a, $b
999 // and similar for setne/eq ... 0/1
1000 insn
->opcode
= inverse
? opcode_table
[opcode
].negate
: opcode
;
1001 use_pseudo(insn
, def
->src1
, &insn
->src1
);
1002 use_pseudo(insn
, def
->src2
, &insn
->src2
);
1003 remove_usage(old
, &insn
->src1
);
1007 if (value
&& (def
->orig_type
->bit_size
== 1))
1012 // *ext.m %s <- (1) %a
1013 // setne.1 %r <- %s, $0
1015 // setne.1 %s <- %a, $0
1016 // and same for setne/eq ... 0/1
1017 return replace_pseudo(insn
, &insn
->src1
, def
->src
);
1019 if (multi_users(old
))
1022 // trunc.n %s <- (o) %a
1023 // setne.m %r <- %s, $0
1025 // and.o %s <- %a, $((1 << o) - 1)
1026 // setne.m %r <- %s, $0
1027 // and same for setne/eq ... 0/1
1029 def
->opcode
= OP_AND
;
1030 def
->type
= def
->orig_type
;
1031 def
->size
= def
->type
->bit_size
;
1032 def
->src2
= value_pseudo(bits_mask(osize
));
1038 static int simplify_constant_mask(struct instruction
*insn
, unsigned long long mask
)
1040 pseudo_t old
= insn
->src1
;
1041 unsigned long long omask
;
1042 unsigned long long nmask
;
1043 struct instruction
*def
;
1046 switch (DEF_OPCODE(def
, old
)) {
1047 case OP_FPCMP
... OP_BINCMP_END
:
1051 return simplify_mask_or(insn
, mask
, def
);
1054 return simplify_mask_shift(def
, mask
);
1056 osize
= def
->orig_type
->bit_size
;
1059 omask
= (1ULL << osize
) - 1;
1060 nmask
= mask
& omask
;
1062 // the AND mask is redundant
1063 return replace_with_pseudo(insn
, old
);
1064 if (nmask
!= mask
) {
1065 // can use a smaller mask
1066 insn
->src2
= value_pseudo(nmask
);
1074 static int simplify_constant_rightside(struct instruction
*insn
)
1076 long long value
= insn
->src2
->value
;
1077 long long sbit
= 1ULL << (insn
->size
- 1);
1078 long long bits
= sbit
| (sbit
- 1);
1080 switch (insn
->opcode
) {
1082 if ((value
& bits
) == bits
)
1083 return replace_with_pseudo(insn
, insn
->src2
);
1084 goto case_neutral_zero
;
1087 if ((value
& bits
) == bits
) {
1088 insn
->opcode
= OP_NOT
;
1091 goto case_neutral_zero
;
1095 insn
->opcode
= OP_ADD
;
1096 insn
->src2
= value_pseudo(-value
);
1103 return replace_with_pseudo(insn
, insn
->src1
);
1108 return simplify_shift(insn
, insn
->src1
, value
);
1110 case OP_MODU
: case OP_MODS
:
1112 return replace_with_pseudo(insn
, value_pseudo(0));
1115 case OP_DIVU
: case OP_DIVS
:
1117 return simplify_mul_div(insn
, value
);
1121 return replace_with_pseudo(insn
, insn
->src2
);
1122 if ((value
& bits
) == bits
)
1123 return replace_with_pseudo(insn
, insn
->src1
);
1124 return simplify_constant_mask(insn
, value
);
1128 return simplify_seteq_setne(insn
, value
);
1133 static int simplify_constant_leftside(struct instruction
*insn
)
1135 long long value
= insn
->src1
->value
;
1137 switch (insn
->opcode
) {
1138 case OP_ADD
: case OP_OR
: case OP_XOR
:
1140 return replace_with_pseudo(insn
, insn
->src2
);
1144 case OP_LSR
: case OP_ASR
:
1148 return replace_with_pseudo(insn
, insn
->src1
);
1154 static int simplify_constant_binop(struct instruction
*insn
)
1156 pseudo_t res
= eval_insn(insn
);
1161 replace_with_pseudo(insn
, res
);
1165 static int simplify_binop_same_args(struct instruction
*insn
, pseudo_t arg
)
1167 switch (insn
->opcode
) {
1169 case OP_SET_LT
: case OP_SET_GT
:
1170 case OP_SET_B
: case OP_SET_A
:
1171 if (Wtautological_compare
)
1172 warning(insn
->pos
, "self-comparison always evaluates to false");
1175 return replace_with_pseudo(insn
, value_pseudo(0));
1178 case OP_SET_LE
: case OP_SET_GE
:
1179 case OP_SET_BE
: case OP_SET_AE
:
1180 if (Wtautological_compare
)
1181 warning(insn
->pos
, "self-comparison always evaluates to true");
1182 return replace_with_pseudo(insn
, value_pseudo(1));
1186 return replace_with_pseudo(insn
, arg
);
1195 static int simplify_binop(struct instruction
*insn
)
1197 if (dead_insn(insn
, &insn
->src1
, &insn
->src2
, NULL
))
1199 if (constant(insn
->src1
)) {
1200 if (constant(insn
->src2
))
1201 return simplify_constant_binop(insn
);
1202 return simplify_constant_leftside(insn
);
1204 if (constant(insn
->src2
))
1205 return simplify_constant_rightside(insn
);
1206 if (insn
->src1
== insn
->src2
)
1207 return simplify_binop_same_args(insn
, insn
->src1
);
1211 static void switch_pseudo(struct instruction
*insn1
, pseudo_t
*pp1
, struct instruction
*insn2
, pseudo_t
*pp2
)
1213 pseudo_t p1
= *pp1
, p2
= *pp2
;
1215 use_pseudo(insn1
, p2
, pp1
);
1216 use_pseudo(insn2
, p1
, pp2
);
1217 remove_usage(p1
, pp1
);
1218 remove_usage(p2
, pp2
);
1221 static int canonical_order(pseudo_t p1
, pseudo_t p2
)
1223 /* symbol/constants on the right */
1224 if (p1
->type
== PSEUDO_VAL
)
1225 return p2
->type
== PSEUDO_VAL
;
1227 if (p1
->type
== PSEUDO_SYM
)
1228 return p2
->type
== PSEUDO_SYM
|| p2
->type
== PSEUDO_VAL
;
1233 static int canonicalize_commutative(struct instruction
*insn
)
1235 if (canonical_order(insn
->src1
, insn
->src2
))
1238 switch_pseudo(insn
, &insn
->src1
, insn
, &insn
->src2
);
1239 return repeat_phase
|= REPEAT_CSE
;
1242 static int canonicalize_compare(struct instruction
*insn
)
1244 if (canonical_order(insn
->src1
, insn
->src2
))
1247 switch_pseudo(insn
, &insn
->src1
, insn
, &insn
->src2
);
1248 insn
->opcode
= opcode_table
[insn
->opcode
].swap
;
1249 return repeat_phase
|= REPEAT_CSE
;
1252 static inline int simple_pseudo(pseudo_t pseudo
)
1254 return pseudo
->type
== PSEUDO_VAL
|| pseudo
->type
== PSEUDO_SYM
;
1257 static int simplify_associative_binop(struct instruction
*insn
)
1259 struct instruction
*def
;
1260 pseudo_t pseudo
= insn
->src1
;
1262 if (!simple_pseudo(insn
->src2
))
1264 if (pseudo
->type
!= PSEUDO_REG
)
1269 if (def
->opcode
!= insn
->opcode
)
1271 if (!simple_pseudo(def
->src2
))
1273 if (multi_users(def
->target
))
1275 switch_pseudo(def
, &def
->src1
, insn
, &insn
->src2
);
1279 static int simplify_constant_unop(struct instruction
*insn
)
1281 long long val
= insn
->src1
->value
;
1282 long long res
, mask
;
1284 switch (insn
->opcode
) {
1292 mask
= 1ULL << (insn
->orig_type
->bit_size
-1);
1294 val
|= ~(mask
| (mask
-1));
1303 mask
= 1ULL << (insn
->size
-1);
1304 res
&= mask
| (mask
-1);
1306 replace_with_pseudo(insn
, value_pseudo(res
));
1310 static int simplify_unop(struct instruction
*insn
)
1312 if (dead_insn(insn
, &insn
->src1
, NULL
, NULL
))
1314 if (constant(insn
->src1
))
1315 return simplify_constant_unop(insn
);
1317 switch (insn
->opcode
) {
1318 struct instruction
*def
;
1321 def
= insn
->src
->def
;
1322 if (def
&& def
->opcode
== OP_NOT
)
1323 return replace_with_pseudo(insn
, def
->src
);
1326 def
= insn
->src
->def
;
1327 if (def
&& def
->opcode
== OP_NEG
)
1328 return replace_with_pseudo(insn
, def
->src
);
1336 static int simplify_one_memop(struct instruction
*insn
, pseudo_t orig
)
1338 pseudo_t addr
= insn
->src
;
1341 if (addr
->type
== PSEUDO_REG
) {
1342 struct instruction
*def
= addr
->def
;
1343 if (def
->opcode
== OP_SYMADDR
&& def
->src
) {
1344 kill_use(&insn
->src
);
1345 use_pseudo(insn
, def
->src
, &insn
->src
);
1346 return REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
1348 if (def
->opcode
== OP_ADD
) {
1364 if (new == orig
|| new == addr
) {
1368 * If some BB have been removed it is possible that this
1369 * memop is in fact part of a dead BB. In this case
1370 * we must not warn since nothing is wrong.
1371 * If not part of a dead BB this will be redone after
1372 * the BBs have been cleaned up.
1374 if (repeat_phase
& REPEAT_CFG_CLEANUP
)
1376 warning(insn
->pos
, "crazy programmer");
1377 replace_pseudo(insn
, &insn
->src
, VOID
);
1380 insn
->offset
+= off
->value
;
1381 replace_pseudo(insn
, &insn
->src
, new);
1382 return REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
1386 // simplify memops instructions
1388 // :note: We walk the whole chain of adds/subs backwards.
1389 // That's not only more efficient, but it allows us to find loops.
1390 static int simplify_memop(struct instruction
*insn
)
1393 pseudo_t orig
= insn
->src
;
1396 one
= simplify_one_memop(insn
, orig
);
1402 static int simplify_cast(struct instruction
*insn
)
1404 unsigned long long mask
;
1405 struct instruction
*def
;
1411 if (dead_insn(insn
, &insn
->src
, NULL
, NULL
))
1416 /* A cast of a constant? */
1418 return simplify_constant_unop(insn
);
1420 // can merge with the previous instruction?
1423 switch (def_opcode(src
)) {
1426 if (val
->type
!= PSEUDO_VAL
)
1428 /* A cast of a AND might be a no-op.. */
1429 switch (insn
->opcode
) {
1431 if (multi_users(src
))
1433 def
->opcode
= OP_TRUNC
;
1434 def
->orig_type
= def
->type
;
1435 def
->type
= insn
->type
;
1438 insn
->opcode
= OP_AND
;
1440 mask
&= (1ULL << size
) - 1;
1441 insn
->src2
= value_pseudo(mask
);
1445 if (val
->value
& (1 << (def
->size
- 1)))
1447 // OK, sign bit is 0
1449 if (multi_users(src
))
1452 // and.n %b <- %a, M
1453 // *ext.m %c <- (n) %b
1456 // and.m %c <- %b, M
1457 // For ZEXT, the mask will always be small
1458 // enough. For SEXT, it can only be done if
1459 // the mask force the sign bit to 0.
1460 def
->opcode
= OP_ZEXT
;
1461 def
->orig_type
= insn
->orig_type
;
1462 def
->type
= insn
->type
;
1463 def
->size
= insn
->size
;
1464 insn
->opcode
= OP_AND
;
1469 case OP_FPCMP
... OP_BINCMP_END
:
1470 switch (insn
->opcode
) {
1472 if (insn
->size
== 1)
1478 // setcc.n %t <- %a, %b
1479 // zext.m %r <- (n) %t
1481 // setcc.m %r <- %a, %b
1482 // and same for s/zext/trunc/
1483 insn
->opcode
= def
->opcode
;
1484 use_pseudo(insn
, def
->src2
, &insn
->src2
);
1485 return replace_pseudo(insn
, &insn
->src1
, def
->src1
);
1489 switch (insn
->opcode
) {
1491 mask
= bits_mask(insn
->size
);
1492 return simplify_mask_or(insn
, mask
, def
);
1497 if (insn
->opcode
!= OP_TRUNC
)
1499 mask
= bits_mask(insn
->size
);
1500 return simplify_mask_shift(def
, mask
);
1502 switch (insn
->opcode
) {
1504 insn
->orig_type
= def
->orig_type
;
1505 return replace_pseudo(insn
, &insn
->src1
, def
->src
);
1507 if (size
!= def
->orig_type
->bit_size
)
1509 insn
->opcode
= OP_AND
;
1510 insn
->src2
= value_pseudo((1ULL << def
->size
) - 1);
1511 return replace_pseudo(insn
, &insn
->src1
, def
->src
);
1515 switch (insn
->opcode
) {
1517 insn
->opcode
= OP_ZEXT
;
1520 insn
->orig_type
= def
->orig_type
;
1521 return replace_pseudo(insn
, &insn
->src
, def
->src
);
1525 switch (insn
->opcode
) {
1527 osize
= def
->orig_type
->bit_size
;
1529 return replace_with_pseudo(insn
, def
->src
);
1531 insn
->opcode
= def
->opcode
;
1532 insn
->orig_type
= def
->orig_type
;
1533 return replace_pseudo(insn
, &insn
->src
, def
->src
);
1535 switch (insn
->opcode
) {
1537 insn
->orig_type
= def
->orig_type
;
1538 return replace_pseudo(insn
, &insn
->src
, def
->src
);
1546 static int simplify_select(struct instruction
*insn
)
1548 pseudo_t cond
, src1
, src2
;
1550 if (dead_insn(insn
, &insn
->src1
, &insn
->src2
, &insn
->src3
))
1556 if (constant(cond
) || src1
== src2
) {
1557 pseudo_t
*kill
, take
;
1558 kill_use(&insn
->src1
);
1559 take
= cond
->value
? src1
: src2
;
1560 kill
= cond
->value
? &insn
->src3
: &insn
->src2
;
1562 replace_with_pseudo(insn
, take
);
1565 if (constant(src1
) && constant(src2
)) {
1566 long long val1
= src1
->value
;
1567 long long val2
= src2
->value
;
1569 /* The pair 0/1 is special - replace with SETNE/SETEQ */
1570 if ((val1
| val2
) == 1) {
1571 int opcode
= OP_SET_EQ
;
1576 insn
->opcode
= opcode
;
1577 /* insn->src1 is already cond */
1578 insn
->src2
= src1
; /* Zero */
1582 if (cond
== src2
&& is_zero(src1
)) {
1583 kill_use(&insn
->src1
);
1584 kill_use(&insn
->src3
);
1585 replace_with_pseudo(insn
, value_pseudo(0));
1591 static int is_in_range(pseudo_t src
, long long low
, long long high
)
1595 switch (src
->type
) {
1598 return value
>= low
&& value
<= high
;
1604 static int simplify_range(struct instruction
*insn
)
1606 pseudo_t src1
, src2
, src3
;
1611 if (src2
->type
!= PSEUDO_VAL
|| src3
->type
!= PSEUDO_VAL
)
1613 if (is_in_range(src1
, src2
->value
, src3
->value
)) {
1614 kill_instruction(insn
);
1621 // simplify SET_NE/EQ $0 + BR
1622 static int simplify_cond_branch(struct instruction
*br
, struct instruction
*def
, pseudo_t newcond
)
1624 replace_pseudo(br
, &br
->cond
, newcond
);
1625 if (def
->opcode
== OP_SET_EQ
) {
1626 struct basic_block
*tmp
= br
->bb_true
;
1627 br
->bb_true
= br
->bb_false
;
1633 static int simplify_branch(struct instruction
*insn
)
1635 pseudo_t cond
= insn
->cond
;
1637 /* Constant conditional */
1638 if (constant(cond
)) {
1639 insert_branch(insn
->bb
, insn
, cond
->value
? insn
->bb_true
: insn
->bb_false
);
1644 if (insn
->bb_true
== insn
->bb_false
) {
1645 struct basic_block
*bb
= insn
->bb
;
1646 struct basic_block
*target
= insn
->bb_false
;
1647 remove_bb_from_list(&target
->parents
, bb
, 1);
1648 remove_bb_from_list(&bb
->children
, target
, 1);
1649 insn
->bb_false
= NULL
;
1650 kill_use(&insn
->cond
);
1652 insn
->opcode
= OP_BR
;
1656 /* Conditional on a SETNE $0 or SETEQ $0 */
1657 if (cond
->type
== PSEUDO_REG
) {
1658 struct instruction
*def
= cond
->def
;
1660 if (def
->opcode
== OP_SET_NE
|| def
->opcode
== OP_SET_EQ
) {
1661 if (constant(def
->src1
) && !def
->src1
->value
)
1662 return simplify_cond_branch(insn
, def
, def
->src2
);
1663 if (constant(def
->src2
) && !def
->src2
->value
)
1664 return simplify_cond_branch(insn
, def
, def
->src1
);
1666 if (def
->opcode
== OP_SEL
) {
1667 if (constant(def
->src2
) && constant(def
->src3
)) {
1668 long long val1
= def
->src2
->value
;
1669 long long val2
= def
->src3
->value
;
1670 if (!val1
&& !val2
) {
1671 insert_branch(insn
->bb
, insn
, insn
->bb_false
);
1675 insert_branch(insn
->bb
, insn
, insn
->bb_true
);
1679 struct basic_block
*tmp
= insn
->bb_true
;
1680 insn
->bb_true
= insn
->bb_false
;
1681 insn
->bb_false
= tmp
;
1683 return replace_pseudo(insn
, &insn
->cond
, def
->src1
);
1686 if (def
->opcode
== OP_SEXT
|| def
->opcode
== OP_ZEXT
)
1687 return replace_pseudo(insn
, &insn
->cond
, def
->src
);
1692 static int simplify_switch(struct instruction
*insn
)
1694 pseudo_t cond
= insn
->cond
;
1696 struct multijmp
*jmp
;
1698 if (!constant(cond
))
1700 val
= insn
->cond
->value
;
1702 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
1704 if (jmp
->begin
> jmp
->end
)
1706 if (val
>= jmp
->begin
&& val
<= jmp
->end
)
1708 } END_FOR_EACH_PTR(jmp
);
1709 warning(insn
->pos
, "Impossible case statement");
1713 insert_branch(insn
->bb
, insn
, jmp
->target
);
1717 int simplify_instruction(struct instruction
*insn
)
1721 switch (insn
->opcode
) {
1722 case OP_ADD
: case OP_MUL
:
1723 case OP_AND
: case OP_OR
: case OP_XOR
:
1724 canonicalize_commutative(insn
);
1725 if (simplify_binop(insn
))
1727 return simplify_associative_binop(insn
);
1729 case OP_SET_EQ
: case OP_SET_NE
:
1730 canonicalize_commutative(insn
);
1731 return simplify_binop(insn
);
1733 case OP_SET_LE
: case OP_SET_GE
:
1734 case OP_SET_LT
: case OP_SET_GT
:
1735 case OP_SET_B
: case OP_SET_A
:
1736 case OP_SET_BE
: case OP_SET_AE
:
1737 canonicalize_compare(insn
);
1740 case OP_DIVU
: case OP_DIVS
:
1741 case OP_MODU
: case OP_MODS
:
1743 case OP_LSR
: case OP_ASR
:
1744 return simplify_binop(insn
);
1746 case OP_NOT
: case OP_NEG
: case OP_FNEG
:
1747 return simplify_unop(insn
);
1749 if (!has_users(insn
->target
))
1750 return kill_instruction(insn
);
1753 return simplify_memop(insn
);
1755 if (dead_insn(insn
, &insn
->src
, NULL
, NULL
))
1756 return REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
1757 return replace_with_pseudo(insn
, insn
->src
);
1758 case OP_SEXT
: case OP_ZEXT
:
1760 return simplify_cast(insn
);
1761 case OP_FCVTU
: case OP_FCVTS
:
1762 case OP_UCVTF
: case OP_SCVTF
:
1765 if (dead_insn(insn
, &insn
->src
, NULL
, NULL
))
1770 return replace_with_pseudo(insn
, insn
->src
);
1772 if (dead_insn(insn
, &insn
->src
, NULL
, NULL
))
1777 if (dead_insn(insn
, NULL
, NULL
, NULL
))
1781 if (dead_insn(insn
, NULL
, NULL
, NULL
)) {
1782 kill_use_list(insn
->phi_list
);
1785 return clean_up_phi(insn
);
1787 if (dead_insn(insn
, &insn
->phi_src
, NULL
, NULL
))
1791 return simplify_select(insn
);
1793 return simplify_branch(insn
);
1795 return simplify_switch(insn
);
1797 return simplify_range(insn
);
1802 if (dead_insn(insn
, &insn
->src1
, &insn
->src2
, NULL
))