simplify '(x op x)' to '0', '1' or 'x'
[smatch.git] / simplify.c
blob0ca45a7d5332980e65100b612de369bfbcc6dd7a
1 /*
2 * Simplify - do instruction simplification before CSE
4 * Copyright (C) 2004 Linus Torvalds
5 */
7 #include <assert.h>
9 #include "parse.h"
10 #include "expression.h"
11 #include "linearize.h"
12 #include "flow.h"
13 #include "symbol.h"
15 /* Find the trivial parent for a phi-source */
16 static struct basic_block *phi_parent(struct basic_block *source, pseudo_t pseudo)
18 /* Can't go upwards if the pseudo is defined in the bb it came from.. */
19 if (pseudo->type == PSEUDO_REG) {
20 struct instruction *def = pseudo->def;
21 if (def->bb == source)
22 return source;
24 if (bb_list_size(source->children) != 1 || bb_list_size(source->parents) != 1)
25 return source;
26 return first_basic_block(source->parents);
29 static void clear_phi(struct instruction *insn)
31 pseudo_t phi;
33 insn->bb = NULL;
34 FOR_EACH_PTR(insn->phi_list, phi) {
35 *THIS_ADDRESS(phi) = VOID;
36 } END_FOR_EACH_PTR(phi);
39 static int if_convert_phi(struct instruction *insn)
41 pseudo_t array[3];
42 struct basic_block *parents[3];
43 struct basic_block *bb, *bb1, *bb2, *source;
44 struct instruction *br;
45 pseudo_t p1, p2;
47 bb = insn->bb;
48 if (linearize_ptr_list((struct ptr_list *)insn->phi_list, (void **)array, 3) != 2)
49 return 0;
50 if (linearize_ptr_list((struct ptr_list *)bb->parents, (void **)parents, 3) != 2)
51 return 0;
52 p1 = array[0]->def->src1;
53 bb1 = array[0]->def->bb;
54 p2 = array[1]->def->src1;
55 bb2 = array[1]->def->bb;
57 /* Only try the simple "direct parents" case */
58 if ((bb1 != parents[0] || bb2 != parents[1]) &&
59 (bb1 != parents[1] || bb2 != parents[0]))
60 return 0;
63 * See if we can find a common source for this..
65 source = phi_parent(bb1, p1);
66 if (source != phi_parent(bb2, p2))
67 return 0;
70 * Cool. We now know that 'source' is the exclusive
71 * parent of both phi-nodes, so the exit at the
72 * end of it fully determines which one it is, and
73 * we can turn it into a select.
75 * HOWEVER, right now we only handle regular
76 * conditional branches. No multijumps or computed
77 * stuff. Verify that here.
79 br = last_instruction(source->insns);
80 if (!br || br->opcode != OP_BR)
81 return 0;
83 assert(br->cond);
84 assert(br->bb_false);
87 * We're in business. Match up true/false with p1/p2.
89 if (br->bb_true == bb2 || br->bb_false == bb1) {
90 pseudo_t p = p1;
91 p1 = p2;
92 p2 = p;
96 * OK, we can now replace that last
98 * br cond, a, b
100 * with the sequence
102 * setcc cond
103 * select pseudo, p1, p2
104 * br cond, a, b
106 * and remove the phi-node. If it then
107 * turns out that 'a' or 'b' is entirely
108 * empty (common case), and now no longer
109 * a phi-source, we'll be able to simplify
110 * the conditional branch too.
112 insert_select(source, br, insn, p1, p2);
113 clear_phi(insn);
114 return REPEAT_CSE;
117 static int clean_up_phi(struct instruction *insn)
119 pseudo_t phi;
120 struct instruction *last;
121 int same;
123 last = NULL;
124 same = 1;
125 FOR_EACH_PTR(insn->phi_list, phi) {
126 struct instruction *def;
127 if (phi == VOID)
128 continue;
129 def = phi->def;
130 if (def->src1 == VOID || !def->bb)
131 continue;
132 if (last) {
133 if (last->src1 != def->src1)
134 same = 0;
135 continue;
137 last = def;
138 } END_FOR_EACH_PTR(phi);
140 if (same) {
141 pseudo_t pseudo = last ? last->src1 : VOID;
142 convert_instruction_target(insn, pseudo);
143 clear_phi(insn);
144 return REPEAT_CSE;
147 return if_convert_phi(insn);
150 static int delete_pseudo_user_list_entry(struct pseudo_user_list **list, pseudo_t *entry, int count)
152 struct pseudo_user *pu;
154 FOR_EACH_PTR(*list, pu) {
155 if (pu->userp == entry) {
156 DELETE_CURRENT_PTR(pu);
157 if (!--count)
158 goto out;
160 } END_FOR_EACH_PTR(pu);
161 assert(count <= 0);
162 out:
163 pack_ptr_list((struct ptr_list **)list);
164 return count;
167 static inline void remove_usage(pseudo_t p, pseudo_t *usep)
169 if (has_use_list(p)) {
170 delete_pseudo_user_list_entry(&p->users, usep, 1);
171 if (!p->users)
172 kill_instruction(p->def);
176 void kill_use(pseudo_t *usep)
178 if (usep) {
179 pseudo_t p = *usep;
180 *usep = VOID;
181 remove_usage(p, usep);
185 void kill_instruction(struct instruction *insn)
187 if (!insn || !insn->bb)
188 return;
190 switch (insn->opcode) {
191 case OP_SEL:
192 case OP_RANGE:
193 kill_use(&insn->src3);
194 /* fall through */
196 case OP_BINARY ... OP_BINCMP_END:
197 kill_use(&insn->src2);
198 /* fall through */
200 case OP_CAST:
201 case OP_SCAST:
202 case OP_FPCAST:
203 case OP_PTRCAST:
204 case OP_SETVAL:
205 case OP_NOT: case OP_NEG:
206 kill_use(&insn->src1);
207 break;
209 case OP_PHI:
210 clear_phi(insn);
211 break;
213 case OP_SYMADDR:
214 repeat_phase |= REPEAT_SYMBOL_CLEANUP;
215 break;
217 case OP_BR:
218 if (!insn->bb_true || !insn->bb_false)
219 break;
220 /* fall through */
222 case OP_COMPUTEDGOTO:
223 kill_use(&insn->cond);
224 break;
226 case OP_ENTRY:
227 default:
228 /* ignore */
229 return;
232 insn->bb = NULL;
233 repeat_phase |= REPEAT_CSE;
234 return;
238 * Kill trivially dead instructions
240 static int dead_insn(struct instruction *insn, pseudo_t *src1, pseudo_t *src2, pseudo_t *src3)
242 struct pseudo_user *pu;
243 FOR_EACH_PTR(insn->target->users, pu) {
244 if (*pu->userp != VOID)
245 return 0;
246 } END_FOR_EACH_PTR(pu);
248 insn->bb = NULL;
249 kill_use(src1);
250 kill_use(src2);
251 kill_use(src3);
252 return REPEAT_CSE;
255 static inline int constant(pseudo_t pseudo)
257 return pseudo->type == PSEUDO_VAL;
260 static int replace_with_pseudo(struct instruction *insn, pseudo_t pseudo)
262 convert_instruction_target(insn, pseudo);
264 switch (insn->opcode) {
265 case OP_SEL:
266 case OP_RANGE:
267 kill_use(&insn->src3);
268 case OP_BINARY ... OP_BINCMP_END:
269 kill_use(&insn->src2);
270 case OP_NOT:
271 case OP_NEG:
272 case OP_SYMADDR:
273 case OP_CAST:
274 case OP_SCAST:
275 case OP_FPCAST:
276 case OP_PTRCAST:
277 kill_use(&insn->src1);
278 break;
280 default:
281 assert(0);
283 insn->bb = NULL;
284 return REPEAT_CSE;
287 static unsigned int value_size(long long value)
289 value >>= 8;
290 if (!value)
291 return 8;
292 value >>= 8;
293 if (!value)
294 return 16;
295 value >>= 16;
296 if (!value)
297 return 32;
298 return 64;
302 * Try to determine the maximum size of bits in a pseudo.
304 * Right now this only follow casts and constant values, but we
305 * could look at things like logical 'and' instructions etc.
307 static unsigned int operand_size(struct instruction *insn, pseudo_t pseudo)
309 unsigned int size = insn->size;
311 if (pseudo->type == PSEUDO_REG) {
312 struct instruction *src = pseudo->def;
313 if (src && src->opcode == OP_CAST && src->orig_type) {
314 unsigned int orig_size = src->orig_type->bit_size;
315 if (orig_size < size)
316 size = orig_size;
319 if (pseudo->type == PSEUDO_VAL) {
320 unsigned int orig_size = value_size(pseudo->value);
321 if (orig_size < size)
322 size = orig_size;
324 return size;
327 static int simplify_asr(struct instruction *insn, pseudo_t pseudo, long long value)
329 unsigned int size = operand_size(insn, pseudo);
331 if (value >= size) {
332 warning(insn->pos, "right shift by bigger than source value");
333 return replace_with_pseudo(insn, value_pseudo(0));
335 if (!value)
336 return replace_with_pseudo(insn, pseudo);
337 return 0;
340 static int simplify_mul_div(struct instruction *insn, long long value)
342 unsigned long long sbit = 1ULL << (insn->size - 1);
343 unsigned long long bits = sbit | (sbit - 1);
345 if (value == 1)
346 return replace_with_pseudo(insn, insn->src1);
348 switch (insn->opcode) {
349 case OP_MULS:
350 case OP_MULU:
351 if (value == 0)
352 return replace_with_pseudo(insn, insn->src2);
353 /* Fall through */
354 case OP_DIVS:
355 if (!(value & sbit)) // positive
356 break;
358 value |= ~bits;
359 if (value == -1) {
360 insn->opcode = OP_NEG;
361 return REPEAT_CSE;
365 return 0;
368 static int simplify_constant_rightside(struct instruction *insn)
370 long long value = insn->src2->value;
372 switch (insn->opcode) {
373 case OP_OR_BOOL:
374 if (value == 1)
375 return replace_with_pseudo(insn, insn->src2);
376 goto case_neutral_zero;
378 case OP_SUB:
379 if (value) {
380 insn->opcode = OP_ADD;
381 insn->src2 = value_pseudo(-value);
382 return REPEAT_CSE;
384 /* Fall through */
385 case OP_ADD:
386 case OP_OR: case OP_XOR:
387 case OP_SHL:
388 case OP_LSR:
389 case_neutral_zero:
390 if (!value)
391 return replace_with_pseudo(insn, insn->src1);
392 return 0;
393 case OP_ASR:
394 return simplify_asr(insn, insn->src1, value);
396 case OP_MODU: case OP_MODS:
397 if (value == 1)
398 return replace_with_pseudo(insn, value_pseudo(0));
399 return 0;
401 case OP_DIVU: case OP_DIVS:
402 case OP_MULU: case OP_MULS:
403 return simplify_mul_div(insn, value);
405 case OP_AND_BOOL:
406 if (value == 1)
407 return replace_with_pseudo(insn, insn->src1);
408 /* Fall through */
409 case OP_AND:
410 if (!value)
411 return replace_with_pseudo(insn, insn->src2);
412 return 0;
414 return 0;
417 static int simplify_constant_leftside(struct instruction *insn)
419 long long value = insn->src1->value;
421 switch (insn->opcode) {
422 case OP_ADD: case OP_OR: case OP_XOR:
423 if (!value)
424 return replace_with_pseudo(insn, insn->src2);
425 return 0;
427 case OP_SHL:
428 case OP_LSR: case OP_ASR:
429 case OP_AND:
430 case OP_MULU: case OP_MULS:
431 if (!value)
432 return replace_with_pseudo(insn, insn->src1);
433 return 0;
435 return 0;
438 static int simplify_constant_binop(struct instruction *insn)
440 /* FIXME! Verify signs and sizes!! */
441 long long left = insn->src1->value;
442 long long right = insn->src2->value;
443 unsigned long long ul, ur;
444 long long res, mask, bits;
446 mask = 1ULL << (insn->size-1);
447 bits = mask | (mask-1);
449 if (left & mask)
450 left |= ~bits;
451 if (right & mask)
452 right |= ~bits;
453 ul = left & bits;
454 ur = right & bits;
456 switch (insn->opcode) {
457 case OP_ADD:
458 res = left + right;
459 break;
460 case OP_SUB:
461 res = left - right;
462 break;
463 case OP_MULU:
464 res = ul * ur;
465 break;
466 case OP_MULS:
467 res = left * right;
468 break;
469 case OP_DIVU:
470 if (!ur)
471 return 0;
472 res = ul / ur;
473 break;
474 case OP_DIVS:
475 if (!right)
476 return 0;
477 if (left == mask && right == -1)
478 return 0;
479 res = left / right;
480 break;
481 case OP_MODU:
482 if (!ur)
483 return 0;
484 res = ul % ur;
485 break;
486 case OP_MODS:
487 if (!right)
488 return 0;
489 if (left == mask && right == -1)
490 return 0;
491 res = left % right;
492 break;
493 case OP_SHL:
494 res = left << right;
495 break;
496 case OP_LSR:
497 res = ul >> ur;
498 break;
499 case OP_ASR:
500 res = left >> right;
501 break;
502 /* Logical */
503 case OP_AND:
504 res = left & right;
505 break;
506 case OP_OR:
507 res = left | right;
508 break;
509 case OP_XOR:
510 res = left ^ right;
511 break;
512 case OP_AND_BOOL:
513 res = left && right;
514 break;
515 case OP_OR_BOOL:
516 res = left || right;
517 break;
519 /* Binary comparison */
520 case OP_SET_EQ:
521 res = left == right;
522 break;
523 case OP_SET_NE:
524 res = left != right;
525 break;
526 case OP_SET_LE:
527 res = left <= right;
528 break;
529 case OP_SET_GE:
530 res = left >= right;
531 break;
532 case OP_SET_LT:
533 res = left < right;
534 break;
535 case OP_SET_GT:
536 res = left > right;
537 break;
538 case OP_SET_B:
539 res = ul < ur;
540 break;
541 case OP_SET_A:
542 res = ul > ur;
543 break;
544 case OP_SET_BE:
545 res = ul <= ur;
546 break;
547 case OP_SET_AE:
548 res = ul >= ur;
549 break;
550 default:
551 return 0;
553 res &= bits;
555 replace_with_pseudo(insn, value_pseudo(res));
556 return REPEAT_CSE;
559 static int simplify_binop_same_args(struct instruction *insn, pseudo_t arg)
561 switch (insn->opcode) {
562 case OP_SET_NE:
563 case OP_SET_LT: case OP_SET_GT:
564 case OP_SET_B: case OP_SET_A:
565 case OP_SUB:
566 case OP_XOR:
567 return replace_with_pseudo(insn, value_pseudo(0));
569 case OP_SET_EQ:
570 case OP_SET_LE: case OP_SET_GE:
571 case OP_SET_BE: case OP_SET_AE:
572 return replace_with_pseudo(insn, value_pseudo(1));
574 case OP_AND:
575 case OP_OR:
576 return replace_with_pseudo(insn, arg);
578 case OP_AND_BOOL:
579 case OP_OR_BOOL:
580 // simplification is correct only if the operands
581 // have already been compared against zero which
582 // is not enforced.
583 break;
585 default:
586 break;
589 return 0;
592 static int simplify_binop(struct instruction *insn)
594 if (dead_insn(insn, &insn->src1, &insn->src2, NULL))
595 return REPEAT_CSE;
596 if (constant(insn->src1)) {
597 if (constant(insn->src2))
598 return simplify_constant_binop(insn);
599 return simplify_constant_leftside(insn);
601 if (constant(insn->src2))
602 return simplify_constant_rightside(insn);
603 if (insn->src1 == insn->src2)
604 return simplify_binop_same_args(insn, insn->src1);
605 return 0;
608 static void switch_pseudo(struct instruction *insn1, pseudo_t *pp1, struct instruction *insn2, pseudo_t *pp2)
610 pseudo_t p1 = *pp1, p2 = *pp2;
612 use_pseudo(insn1, p2, pp1);
613 use_pseudo(insn2, p1, pp2);
614 remove_usage(p1, pp1);
615 remove_usage(p2, pp2);
618 static int canonical_order(pseudo_t p1, pseudo_t p2)
620 /* symbol/constants on the right */
621 if (p1->type == PSEUDO_VAL)
622 return p2->type == PSEUDO_VAL;
624 if (p1->type == PSEUDO_SYM)
625 return p2->type == PSEUDO_SYM || p2->type == PSEUDO_VAL;
627 return 1;
630 static int simplify_commutative_binop(struct instruction *insn)
632 if (!canonical_order(insn->src1, insn->src2)) {
633 switch_pseudo(insn, &insn->src1, insn, &insn->src2);
634 return REPEAT_CSE;
636 return 0;
639 static inline int simple_pseudo(pseudo_t pseudo)
641 return pseudo->type == PSEUDO_VAL || pseudo->type == PSEUDO_SYM;
644 static int simplify_associative_binop(struct instruction *insn)
646 struct instruction *def;
647 pseudo_t pseudo = insn->src1;
649 if (!simple_pseudo(insn->src2))
650 return 0;
651 if (pseudo->type != PSEUDO_REG)
652 return 0;
653 def = pseudo->def;
654 if (def == insn)
655 return 0;
656 if (def->opcode != insn->opcode)
657 return 0;
658 if (!simple_pseudo(def->src2))
659 return 0;
660 if (ptr_list_size((struct ptr_list *)def->target->users) != 1)
661 return 0;
662 switch_pseudo(def, &def->src1, insn, &insn->src2);
663 return REPEAT_CSE;
666 static int simplify_constant_unop(struct instruction *insn)
668 long long val = insn->src1->value;
669 long long res, mask;
671 switch (insn->opcode) {
672 case OP_NOT:
673 res = ~val;
674 break;
675 case OP_NEG:
676 res = -val;
677 break;
678 default:
679 return 0;
681 mask = 1ULL << (insn->size-1);
682 res &= mask | (mask-1);
684 replace_with_pseudo(insn, value_pseudo(res));
685 return REPEAT_CSE;
688 static int simplify_unop(struct instruction *insn)
690 if (dead_insn(insn, &insn->src1, NULL, NULL))
691 return REPEAT_CSE;
692 if (constant(insn->src1))
693 return simplify_constant_unop(insn);
695 switch (insn->opcode) {
696 struct instruction *def;
698 case OP_NOT:
699 def = insn->src->def;
700 if (def && def->opcode == OP_NOT)
701 return replace_with_pseudo(insn, def->src);
702 break;
703 case OP_NEG:
704 def = insn->src->def;
705 if (def && def->opcode == OP_NEG)
706 return replace_with_pseudo(insn, def->src);
707 break;
708 default:
709 return 0;
711 return 0;
714 static int simplify_one_memop(struct instruction *insn, pseudo_t orig)
716 pseudo_t addr = insn->src;
717 pseudo_t new, off;
719 if (addr->type == PSEUDO_REG) {
720 struct instruction *def = addr->def;
721 if (def->opcode == OP_SYMADDR && def->src) {
722 kill_use(&insn->src);
723 use_pseudo(insn, def->src, &insn->src);
724 return REPEAT_CSE | REPEAT_SYMBOL_CLEANUP;
726 if (def->opcode == OP_ADD) {
727 new = def->src1;
728 off = def->src2;
729 if (constant(off))
730 goto offset;
731 new = off;
732 off = def->src1;
733 if (constant(off))
734 goto offset;
735 return 0;
738 return 0;
740 offset:
741 /* Invalid code */
742 if (new == orig) {
743 if (new == VOID)
744 return 0;
745 new = VOID;
746 warning(insn->pos, "crazy programmer");
748 insn->offset += off->value;
749 use_pseudo(insn, new, &insn->src);
750 remove_usage(addr, &insn->src);
751 return REPEAT_CSE | REPEAT_SYMBOL_CLEANUP;
755 * We walk the whole chain of adds/subs backwards. That's not
756 * only more efficient, but it allows us to find loops.
758 static int simplify_memop(struct instruction *insn)
760 int one, ret = 0;
761 pseudo_t orig = insn->src;
763 do {
764 one = simplify_one_memop(insn, orig);
765 ret |= one;
766 } while (one);
767 return ret;
770 static long long get_cast_value(long long val, int old_size, int new_size, int sign)
772 long long mask;
774 if (sign && new_size > old_size) {
775 mask = 1 << (old_size-1);
776 if (val & mask)
777 val |= ~(mask | (mask-1));
779 mask = 1 << (new_size-1);
780 return val & (mask | (mask-1));
783 static int simplify_cast(struct instruction *insn)
785 struct symbol *orig_type;
786 int orig_size, size;
787 pseudo_t src;
789 if (dead_insn(insn, &insn->src, NULL, NULL))
790 return REPEAT_CSE;
792 orig_type = insn->orig_type;
793 if (!orig_type)
794 return 0;
796 /* Keep casts with pointer on either side (not only case of OP_PTRCAST) */
797 if (is_ptr_type(orig_type) || is_ptr_type(insn->type))
798 return 0;
800 orig_size = orig_type->bit_size;
801 size = insn->size;
802 src = insn->src;
804 /* A cast of a constant? */
805 if (constant(src)) {
806 int sign = orig_type->ctype.modifiers & MOD_SIGNED;
807 long long val = get_cast_value(src->value, orig_size, size, sign);
808 src = value_pseudo(val);
809 goto simplify;
812 /* A cast of a "and" might be a no-op.. */
813 if (src->type == PSEUDO_REG) {
814 struct instruction *def = src->def;
815 if (def->opcode == OP_AND && def->size >= size) {
816 pseudo_t val = def->src2;
817 if (val->type == PSEUDO_VAL) {
818 unsigned long long value = val->value;
819 if (!(value >> (size-1)))
820 goto simplify;
825 if (size == orig_size) {
826 int op = (orig_type->ctype.modifiers & MOD_SIGNED) ? OP_SCAST : OP_CAST;
827 if (insn->opcode == op)
828 goto simplify;
831 return 0;
833 simplify:
834 return replace_with_pseudo(insn, src);
837 static int simplify_select(struct instruction *insn)
839 pseudo_t cond, src1, src2;
841 if (dead_insn(insn, &insn->src1, &insn->src2, &insn->src3))
842 return REPEAT_CSE;
844 cond = insn->src1;
845 src1 = insn->src2;
846 src2 = insn->src3;
847 if (constant(cond) || src1 == src2) {
848 pseudo_t *kill, take;
849 kill_use(&insn->src1);
850 take = cond->value ? src1 : src2;
851 kill = cond->value ? &insn->src3 : &insn->src2;
852 kill_use(kill);
853 replace_with_pseudo(insn, take);
854 return REPEAT_CSE;
856 if (constant(src1) && constant(src2)) {
857 long long val1 = src1->value;
858 long long val2 = src2->value;
860 /* The pair 0/1 is special - replace with SETNE/SETEQ */
861 if ((val1 | val2) == 1) {
862 int opcode = OP_SET_EQ;
863 if (val1) {
864 src1 = src2;
865 opcode = OP_SET_NE;
867 insn->opcode = opcode;
868 /* insn->src1 is already cond */
869 insn->src2 = src1; /* Zero */
870 return REPEAT_CSE;
873 return 0;
876 static int is_in_range(pseudo_t src, long long low, long long high)
878 long long value;
880 switch (src->type) {
881 case PSEUDO_VAL:
882 value = src->value;
883 return value >= low && value <= high;
884 default:
885 return 0;
889 static int simplify_range(struct instruction *insn)
891 pseudo_t src1, src2, src3;
893 src1 = insn->src1;
894 src2 = insn->src2;
895 src3 = insn->src3;
896 if (src2->type != PSEUDO_VAL || src3->type != PSEUDO_VAL)
897 return 0;
898 if (is_in_range(src1, src2->value, src3->value)) {
899 kill_instruction(insn);
900 return REPEAT_CSE;
902 return 0;
906 * Simplify "set_ne/eq $0 + br"
908 static int simplify_cond_branch(struct instruction *br, pseudo_t cond, struct instruction *def, pseudo_t *pp)
910 use_pseudo(br, *pp, &br->cond);
911 remove_usage(cond, &br->cond);
912 if (def->opcode == OP_SET_EQ) {
913 struct basic_block *true = br->bb_true;
914 struct basic_block *false = br->bb_false;
915 br->bb_false = true;
916 br->bb_true = false;
918 return REPEAT_CSE;
921 static int simplify_branch(struct instruction *insn)
923 pseudo_t cond = insn->cond;
925 if (!cond)
926 return 0;
928 /* Constant conditional */
929 if (constant(cond)) {
930 insert_branch(insn->bb, insn, cond->value ? insn->bb_true : insn->bb_false);
931 return REPEAT_CSE;
934 /* Same target? */
935 if (insn->bb_true == insn->bb_false) {
936 struct basic_block *bb = insn->bb;
937 struct basic_block *target = insn->bb_false;
938 remove_bb_from_list(&target->parents, bb, 1);
939 remove_bb_from_list(&bb->children, target, 1);
940 insn->bb_false = NULL;
941 kill_use(&insn->cond);
942 insn->cond = NULL;
943 return REPEAT_CSE;
946 /* Conditional on a SETNE $0 or SETEQ $0 */
947 if (cond->type == PSEUDO_REG) {
948 struct instruction *def = cond->def;
950 if (def->opcode == OP_SET_NE || def->opcode == OP_SET_EQ) {
951 if (constant(def->src1) && !def->src1->value)
952 return simplify_cond_branch(insn, cond, def, &def->src2);
953 if (constant(def->src2) && !def->src2->value)
954 return simplify_cond_branch(insn, cond, def, &def->src1);
956 if (def->opcode == OP_SEL) {
957 if (constant(def->src2) && constant(def->src3)) {
958 long long val1 = def->src2->value;
959 long long val2 = def->src3->value;
960 if (!val1 && !val2) {
961 insert_branch(insn->bb, insn, insn->bb_false);
962 return REPEAT_CSE;
964 if (val1 && val2) {
965 insert_branch(insn->bb, insn, insn->bb_true);
966 return REPEAT_CSE;
968 if (val2) {
969 struct basic_block *true = insn->bb_true;
970 struct basic_block *false = insn->bb_false;
971 insn->bb_false = true;
972 insn->bb_true = false;
974 use_pseudo(insn, def->src1, &insn->cond);
975 remove_usage(cond, &insn->cond);
976 return REPEAT_CSE;
979 if (def->opcode == OP_CAST || def->opcode == OP_SCAST) {
980 int orig_size = def->orig_type ? def->orig_type->bit_size : 0;
981 if (def->size > orig_size) {
982 use_pseudo(insn, def->src, &insn->cond);
983 remove_usage(cond, &insn->cond);
984 return REPEAT_CSE;
988 return 0;
991 static int simplify_switch(struct instruction *insn)
993 pseudo_t cond = insn->cond;
994 long long val;
995 struct multijmp *jmp;
997 if (!constant(cond))
998 return 0;
999 val = insn->cond->value;
1001 FOR_EACH_PTR(insn->multijmp_list, jmp) {
1002 /* Default case */
1003 if (jmp->begin > jmp->end)
1004 goto found;
1005 if (val >= jmp->begin && val <= jmp->end)
1006 goto found;
1007 } END_FOR_EACH_PTR(jmp);
1008 warning(insn->pos, "Impossible case statement");
1009 return 0;
1011 found:
1012 insert_branch(insn->bb, insn, jmp->target);
1013 return REPEAT_CSE;
1016 int simplify_instruction(struct instruction *insn)
1018 if (!insn->bb)
1019 return 0;
1020 switch (insn->opcode) {
1021 case OP_ADD: case OP_MULS:
1022 case OP_AND: case OP_OR: case OP_XOR:
1023 case OP_AND_BOOL: case OP_OR_BOOL:
1024 if (simplify_binop(insn))
1025 return REPEAT_CSE;
1026 if (simplify_commutative_binop(insn))
1027 return REPEAT_CSE;
1028 return simplify_associative_binop(insn);
1030 case OP_MULU:
1031 case OP_SET_EQ: case OP_SET_NE:
1032 if (simplify_binop(insn))
1033 return REPEAT_CSE;
1034 return simplify_commutative_binop(insn);
1036 case OP_SUB:
1037 case OP_DIVU: case OP_DIVS:
1038 case OP_MODU: case OP_MODS:
1039 case OP_SHL:
1040 case OP_LSR: case OP_ASR:
1041 case OP_SET_LE: case OP_SET_GE:
1042 case OP_SET_LT: case OP_SET_GT:
1043 case OP_SET_B: case OP_SET_A:
1044 case OP_SET_BE: case OP_SET_AE:
1045 return simplify_binop(insn);
1047 case OP_NOT: case OP_NEG:
1048 return simplify_unop(insn);
1049 case OP_LOAD: case OP_STORE:
1050 return simplify_memop(insn);
1051 case OP_SYMADDR:
1052 if (dead_insn(insn, NULL, NULL, NULL))
1053 return REPEAT_CSE | REPEAT_SYMBOL_CLEANUP;
1054 return replace_with_pseudo(insn, insn->symbol);
1055 case OP_CAST:
1056 case OP_SCAST:
1057 case OP_FPCAST:
1058 case OP_PTRCAST:
1059 return simplify_cast(insn);
1060 case OP_PHI:
1061 if (dead_insn(insn, NULL, NULL, NULL)) {
1062 clear_phi(insn);
1063 return REPEAT_CSE;
1065 return clean_up_phi(insn);
1066 case OP_PHISOURCE:
1067 if (dead_insn(insn, &insn->phi_src, NULL, NULL))
1068 return REPEAT_CSE;
1069 break;
1070 case OP_SEL:
1071 return simplify_select(insn);
1072 case OP_BR:
1073 return simplify_branch(insn);
1074 case OP_SWITCH:
1075 return simplify_switch(insn);
1076 case OP_RANGE:
1077 return simplify_range(insn);
1079 return 0;