simplify '(x / 1)' to 'x'
[smatch.git] / simplify.c
blobed6cc41b63ff47fbe937384ba738578a22c63e9f
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 if (value == 1)
343 return replace_with_pseudo(insn, insn->src1);
345 switch (insn->opcode) {
346 case OP_MULS:
347 case OP_MULU:
348 if (value == 0)
349 return replace_with_pseudo(insn, insn->src2);
352 return 0;
355 static int simplify_constant_rightside(struct instruction *insn)
357 long long value = insn->src2->value;
359 switch (insn->opcode) {
360 case OP_SUB:
361 if (value) {
362 insn->opcode = OP_ADD;
363 insn->src2 = value_pseudo(-value);
364 return REPEAT_CSE;
366 /* Fall through */
367 case OP_ADD:
368 case OP_OR: case OP_XOR:
369 case OP_OR_BOOL:
370 case OP_SHL:
371 case OP_LSR:
372 if (!value)
373 return replace_with_pseudo(insn, insn->src1);
374 return 0;
375 case OP_ASR:
376 return simplify_asr(insn, insn->src1, value);
378 case OP_DIVU: case OP_DIVS:
379 case OP_MULU: case OP_MULS:
380 return simplify_mul_div(insn, value);
382 case OP_AND_BOOL:
383 if (value == 1)
384 return replace_with_pseudo(insn, insn->src1);
385 /* Fall through */
386 case OP_AND:
387 if (!value)
388 return replace_with_pseudo(insn, insn->src2);
389 return 0;
391 return 0;
394 static int simplify_constant_leftside(struct instruction *insn)
396 long long value = insn->src1->value;
398 switch (insn->opcode) {
399 case OP_ADD: case OP_OR: case OP_XOR:
400 if (!value)
401 return replace_with_pseudo(insn, insn->src2);
402 return 0;
404 case OP_SHL:
405 case OP_LSR: case OP_ASR:
406 case OP_AND:
407 case OP_MULU: case OP_MULS:
408 if (!value)
409 return replace_with_pseudo(insn, insn->src1);
410 return 0;
412 return 0;
415 static int simplify_constant_binop(struct instruction *insn)
417 /* FIXME! Verify signs and sizes!! */
418 long long left = insn->src1->value;
419 long long right = insn->src2->value;
420 unsigned long long ul, ur;
421 long long res, mask, bits;
423 mask = 1ULL << (insn->size-1);
424 bits = mask | (mask-1);
426 if (left & mask)
427 left |= ~bits;
428 if (right & mask)
429 right |= ~bits;
430 ul = left & bits;
431 ur = right & bits;
433 switch (insn->opcode) {
434 case OP_ADD:
435 res = left + right;
436 break;
437 case OP_SUB:
438 res = left - right;
439 break;
440 case OP_MULU:
441 res = ul * ur;
442 break;
443 case OP_MULS:
444 res = left * right;
445 break;
446 case OP_DIVU:
447 if (!ur)
448 return 0;
449 res = ul / ur;
450 break;
451 case OP_DIVS:
452 if (!right)
453 return 0;
454 if (left == mask && right == -1)
455 return 0;
456 res = left / right;
457 break;
458 case OP_MODU:
459 if (!ur)
460 return 0;
461 res = ul % ur;
462 break;
463 case OP_MODS:
464 if (!right)
465 return 0;
466 if (left == mask && right == -1)
467 return 0;
468 res = left % right;
469 break;
470 case OP_SHL:
471 res = left << right;
472 break;
473 case OP_LSR:
474 res = ul >> ur;
475 break;
476 case OP_ASR:
477 res = left >> right;
478 break;
479 /* Logical */
480 case OP_AND:
481 res = left & right;
482 break;
483 case OP_OR:
484 res = left | right;
485 break;
486 case OP_XOR:
487 res = left ^ right;
488 break;
489 case OP_AND_BOOL:
490 res = left && right;
491 break;
492 case OP_OR_BOOL:
493 res = left || right;
494 break;
496 /* Binary comparison */
497 case OP_SET_EQ:
498 res = left == right;
499 break;
500 case OP_SET_NE:
501 res = left != right;
502 break;
503 case OP_SET_LE:
504 res = left <= right;
505 break;
506 case OP_SET_GE:
507 res = left >= right;
508 break;
509 case OP_SET_LT:
510 res = left < right;
511 break;
512 case OP_SET_GT:
513 res = left > right;
514 break;
515 case OP_SET_B:
516 res = ul < ur;
517 break;
518 case OP_SET_A:
519 res = ul > ur;
520 break;
521 case OP_SET_BE:
522 res = ul <= ur;
523 break;
524 case OP_SET_AE:
525 res = ul >= ur;
526 break;
527 default:
528 return 0;
530 res &= bits;
532 replace_with_pseudo(insn, value_pseudo(res));
533 return REPEAT_CSE;
536 static int simplify_binop(struct instruction *insn)
538 if (dead_insn(insn, &insn->src1, &insn->src2, NULL))
539 return REPEAT_CSE;
540 if (constant(insn->src1)) {
541 if (constant(insn->src2))
542 return simplify_constant_binop(insn);
543 return simplify_constant_leftside(insn);
545 if (constant(insn->src2))
546 return simplify_constant_rightside(insn);
547 return 0;
550 static void switch_pseudo(struct instruction *insn1, pseudo_t *pp1, struct instruction *insn2, pseudo_t *pp2)
552 pseudo_t p1 = *pp1, p2 = *pp2;
554 use_pseudo(insn1, p2, pp1);
555 use_pseudo(insn2, p1, pp2);
556 remove_usage(p1, pp1);
557 remove_usage(p2, pp2);
560 static int canonical_order(pseudo_t p1, pseudo_t p2)
562 /* symbol/constants on the right */
563 if (p1->type == PSEUDO_VAL)
564 return p2->type == PSEUDO_VAL;
566 if (p1->type == PSEUDO_SYM)
567 return p2->type == PSEUDO_SYM || p2->type == PSEUDO_VAL;
569 return 1;
572 static int simplify_commutative_binop(struct instruction *insn)
574 if (!canonical_order(insn->src1, insn->src2)) {
575 switch_pseudo(insn, &insn->src1, insn, &insn->src2);
576 return REPEAT_CSE;
578 return 0;
581 static inline int simple_pseudo(pseudo_t pseudo)
583 return pseudo->type == PSEUDO_VAL || pseudo->type == PSEUDO_SYM;
586 static int simplify_associative_binop(struct instruction *insn)
588 struct instruction *def;
589 pseudo_t pseudo = insn->src1;
591 if (!simple_pseudo(insn->src2))
592 return 0;
593 if (pseudo->type != PSEUDO_REG)
594 return 0;
595 def = pseudo->def;
596 if (def == insn)
597 return 0;
598 if (def->opcode != insn->opcode)
599 return 0;
600 if (!simple_pseudo(def->src2))
601 return 0;
602 if (ptr_list_size((struct ptr_list *)def->target->users) != 1)
603 return 0;
604 switch_pseudo(def, &def->src1, insn, &insn->src2);
605 return REPEAT_CSE;
608 static int simplify_constant_unop(struct instruction *insn)
610 long long val = insn->src1->value;
611 long long res, mask;
613 switch (insn->opcode) {
614 case OP_NOT:
615 res = ~val;
616 break;
617 case OP_NEG:
618 res = -val;
619 break;
620 default:
621 return 0;
623 mask = 1ULL << (insn->size-1);
624 res &= mask | (mask-1);
626 replace_with_pseudo(insn, value_pseudo(res));
627 return REPEAT_CSE;
630 static int simplify_unop(struct instruction *insn)
632 if (dead_insn(insn, &insn->src1, NULL, NULL))
633 return REPEAT_CSE;
634 if (constant(insn->src1))
635 return simplify_constant_unop(insn);
636 return 0;
639 static int simplify_one_memop(struct instruction *insn, pseudo_t orig)
641 pseudo_t addr = insn->src;
642 pseudo_t new, off;
644 if (addr->type == PSEUDO_REG) {
645 struct instruction *def = addr->def;
646 if (def->opcode == OP_SYMADDR && def->src) {
647 kill_use(&insn->src);
648 use_pseudo(insn, def->src, &insn->src);
649 return REPEAT_CSE | REPEAT_SYMBOL_CLEANUP;
651 if (def->opcode == OP_ADD) {
652 new = def->src1;
653 off = def->src2;
654 if (constant(off))
655 goto offset;
656 new = off;
657 off = def->src1;
658 if (constant(off))
659 goto offset;
660 return 0;
663 return 0;
665 offset:
666 /* Invalid code */
667 if (new == orig) {
668 if (new == VOID)
669 return 0;
670 new = VOID;
671 warning(insn->pos, "crazy programmer");
673 insn->offset += off->value;
674 use_pseudo(insn, new, &insn->src);
675 remove_usage(addr, &insn->src);
676 return REPEAT_CSE | REPEAT_SYMBOL_CLEANUP;
680 * We walk the whole chain of adds/subs backwards. That's not
681 * only more efficient, but it allows us to find loops.
683 static int simplify_memop(struct instruction *insn)
685 int one, ret = 0;
686 pseudo_t orig = insn->src;
688 do {
689 one = simplify_one_memop(insn, orig);
690 ret |= one;
691 } while (one);
692 return ret;
695 static long long get_cast_value(long long val, int old_size, int new_size, int sign)
697 long long mask;
699 if (sign && new_size > old_size) {
700 mask = 1 << (old_size-1);
701 if (val & mask)
702 val |= ~(mask | (mask-1));
704 mask = 1 << (new_size-1);
705 return val & (mask | (mask-1));
708 static int simplify_cast(struct instruction *insn)
710 struct symbol *orig_type;
711 int orig_size, size;
712 pseudo_t src;
714 if (dead_insn(insn, &insn->src, NULL, NULL))
715 return REPEAT_CSE;
717 orig_type = insn->orig_type;
718 if (!orig_type)
719 return 0;
721 /* Keep casts with pointer on either side (not only case of OP_PTRCAST) */
722 if (is_ptr_type(orig_type) || is_ptr_type(insn->type))
723 return 0;
725 orig_size = orig_type->bit_size;
726 size = insn->size;
727 src = insn->src;
729 /* A cast of a constant? */
730 if (constant(src)) {
731 int sign = orig_type->ctype.modifiers & MOD_SIGNED;
732 long long val = get_cast_value(src->value, orig_size, size, sign);
733 src = value_pseudo(val);
734 goto simplify;
737 /* A cast of a "and" might be a no-op.. */
738 if (src->type == PSEUDO_REG) {
739 struct instruction *def = src->def;
740 if (def->opcode == OP_AND && def->size >= size) {
741 pseudo_t val = def->src2;
742 if (val->type == PSEUDO_VAL) {
743 unsigned long long value = val->value;
744 if (!(value >> (size-1)))
745 goto simplify;
750 if (size == orig_size) {
751 int op = (orig_type->ctype.modifiers & MOD_SIGNED) ? OP_SCAST : OP_CAST;
752 if (insn->opcode == op)
753 goto simplify;
756 return 0;
758 simplify:
759 return replace_with_pseudo(insn, src);
762 static int simplify_select(struct instruction *insn)
764 pseudo_t cond, src1, src2;
766 if (dead_insn(insn, &insn->src1, &insn->src2, &insn->src3))
767 return REPEAT_CSE;
769 cond = insn->src1;
770 src1 = insn->src2;
771 src2 = insn->src3;
772 if (constant(cond) || src1 == src2) {
773 pseudo_t *kill, take;
774 kill_use(&insn->src1);
775 take = cond->value ? src1 : src2;
776 kill = cond->value ? &insn->src3 : &insn->src2;
777 kill_use(kill);
778 replace_with_pseudo(insn, take);
779 return REPEAT_CSE;
781 if (constant(src1) && constant(src2)) {
782 long long val1 = src1->value;
783 long long val2 = src2->value;
785 /* The pair 0/1 is special - replace with SETNE/SETEQ */
786 if ((val1 | val2) == 1) {
787 int opcode = OP_SET_EQ;
788 if (val1) {
789 src1 = src2;
790 opcode = OP_SET_NE;
792 insn->opcode = opcode;
793 /* insn->src1 is already cond */
794 insn->src2 = src1; /* Zero */
795 return REPEAT_CSE;
798 return 0;
801 static int is_in_range(pseudo_t src, long long low, long long high)
803 long long value;
805 switch (src->type) {
806 case PSEUDO_VAL:
807 value = src->value;
808 return value >= low && value <= high;
809 default:
810 return 0;
814 static int simplify_range(struct instruction *insn)
816 pseudo_t src1, src2, src3;
818 src1 = insn->src1;
819 src2 = insn->src2;
820 src3 = insn->src3;
821 if (src2->type != PSEUDO_VAL || src3->type != PSEUDO_VAL)
822 return 0;
823 if (is_in_range(src1, src2->value, src3->value)) {
824 kill_instruction(insn);
825 return REPEAT_CSE;
827 return 0;
831 * Simplify "set_ne/eq $0 + br"
833 static int simplify_cond_branch(struct instruction *br, pseudo_t cond, struct instruction *def, pseudo_t *pp)
835 use_pseudo(br, *pp, &br->cond);
836 remove_usage(cond, &br->cond);
837 if (def->opcode == OP_SET_EQ) {
838 struct basic_block *true = br->bb_true;
839 struct basic_block *false = br->bb_false;
840 br->bb_false = true;
841 br->bb_true = false;
843 return REPEAT_CSE;
846 static int simplify_branch(struct instruction *insn)
848 pseudo_t cond = insn->cond;
850 if (!cond)
851 return 0;
853 /* Constant conditional */
854 if (constant(cond)) {
855 insert_branch(insn->bb, insn, cond->value ? insn->bb_true : insn->bb_false);
856 return REPEAT_CSE;
859 /* Same target? */
860 if (insn->bb_true == insn->bb_false) {
861 struct basic_block *bb = insn->bb;
862 struct basic_block *target = insn->bb_false;
863 remove_bb_from_list(&target->parents, bb, 1);
864 remove_bb_from_list(&bb->children, target, 1);
865 insn->bb_false = NULL;
866 kill_use(&insn->cond);
867 insn->cond = NULL;
868 return REPEAT_CSE;
871 /* Conditional on a SETNE $0 or SETEQ $0 */
872 if (cond->type == PSEUDO_REG) {
873 struct instruction *def = cond->def;
875 if (def->opcode == OP_SET_NE || def->opcode == OP_SET_EQ) {
876 if (constant(def->src1) && !def->src1->value)
877 return simplify_cond_branch(insn, cond, def, &def->src2);
878 if (constant(def->src2) && !def->src2->value)
879 return simplify_cond_branch(insn, cond, def, &def->src1);
881 if (def->opcode == OP_SEL) {
882 if (constant(def->src2) && constant(def->src3)) {
883 long long val1 = def->src2->value;
884 long long val2 = def->src3->value;
885 if (!val1 && !val2) {
886 insert_branch(insn->bb, insn, insn->bb_false);
887 return REPEAT_CSE;
889 if (val1 && val2) {
890 insert_branch(insn->bb, insn, insn->bb_true);
891 return REPEAT_CSE;
893 if (val2) {
894 struct basic_block *true = insn->bb_true;
895 struct basic_block *false = insn->bb_false;
896 insn->bb_false = true;
897 insn->bb_true = false;
899 use_pseudo(insn, def->src1, &insn->cond);
900 remove_usage(cond, &insn->cond);
901 return REPEAT_CSE;
904 if (def->opcode == OP_CAST || def->opcode == OP_SCAST) {
905 int orig_size = def->orig_type ? def->orig_type->bit_size : 0;
906 if (def->size > orig_size) {
907 use_pseudo(insn, def->src, &insn->cond);
908 remove_usage(cond, &insn->cond);
909 return REPEAT_CSE;
913 return 0;
916 static int simplify_switch(struct instruction *insn)
918 pseudo_t cond = insn->cond;
919 long long val;
920 struct multijmp *jmp;
922 if (!constant(cond))
923 return 0;
924 val = insn->cond->value;
926 FOR_EACH_PTR(insn->multijmp_list, jmp) {
927 /* Default case */
928 if (jmp->begin > jmp->end)
929 goto found;
930 if (val >= jmp->begin && val <= jmp->end)
931 goto found;
932 } END_FOR_EACH_PTR(jmp);
933 warning(insn->pos, "Impossible case statement");
934 return 0;
936 found:
937 insert_branch(insn->bb, insn, jmp->target);
938 return REPEAT_CSE;
941 int simplify_instruction(struct instruction *insn)
943 if (!insn->bb)
944 return 0;
945 switch (insn->opcode) {
946 case OP_ADD: case OP_MULS:
947 case OP_AND: case OP_OR: case OP_XOR:
948 case OP_AND_BOOL: case OP_OR_BOOL:
949 if (simplify_binop(insn))
950 return REPEAT_CSE;
951 if (simplify_commutative_binop(insn))
952 return REPEAT_CSE;
953 return simplify_associative_binop(insn);
955 case OP_MULU:
956 case OP_SET_EQ: case OP_SET_NE:
957 if (simplify_binop(insn))
958 return REPEAT_CSE;
959 return simplify_commutative_binop(insn);
961 case OP_SUB:
962 case OP_DIVU: case OP_DIVS:
963 case OP_MODU: case OP_MODS:
964 case OP_SHL:
965 case OP_LSR: case OP_ASR:
966 case OP_SET_LE: case OP_SET_GE:
967 case OP_SET_LT: case OP_SET_GT:
968 case OP_SET_B: case OP_SET_A:
969 case OP_SET_BE: case OP_SET_AE:
970 return simplify_binop(insn);
972 case OP_NOT: case OP_NEG:
973 return simplify_unop(insn);
974 case OP_LOAD: case OP_STORE:
975 return simplify_memop(insn);
976 case OP_SYMADDR:
977 if (dead_insn(insn, NULL, NULL, NULL))
978 return REPEAT_CSE | REPEAT_SYMBOL_CLEANUP;
979 return replace_with_pseudo(insn, insn->symbol);
980 case OP_CAST:
981 case OP_SCAST:
982 case OP_FPCAST:
983 case OP_PTRCAST:
984 return simplify_cast(insn);
985 case OP_PHI:
986 if (dead_insn(insn, NULL, NULL, NULL)) {
987 clear_phi(insn);
988 return REPEAT_CSE;
990 return clean_up_phi(insn);
991 case OP_PHISOURCE:
992 if (dead_insn(insn, &insn->phi_src, NULL, NULL))
993 return REPEAT_CSE;
994 break;
995 case OP_SEL:
996 return simplify_select(insn);
997 case OP_BR:
998 return simplify_branch(insn);
999 case OP_SWITCH:
1000 return simplify_switch(insn);
1001 case OP_RANGE:
1002 return simplify_range(insn);
1004 return 0;