Warn about implicit casts to/from "nocast" types
[smatch.git] / simplify.c
blob33786000023cc962cf5230e446af5a373c9ee7e4
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"
14 /* Find the trivial parent for a phi-source */
15 static struct basic_block *phi_parent(struct basic_block *source, pseudo_t pseudo)
17 /* Can't go upwards if the pseudo is defined in the bb it came from.. */
18 if (pseudo->type == PSEUDO_REG) {
19 struct instruction *def = pseudo->def;
20 if (def->bb == source)
21 return source;
23 if (bb_list_size(source->children) != 1 || bb_list_size(source->parents) != 1)
24 return source;
25 return first_basic_block(source->parents);
28 static void clear_phi(struct instruction *insn)
30 pseudo_t phi;
32 insn->bb = NULL;
33 FOR_EACH_PTR(insn->phi_list, phi) {
34 *THIS_ADDRESS(phi) = VOID;
35 } END_FOR_EACH_PTR(phi);
38 static int if_convert_phi(struct instruction *insn)
40 pseudo_t array[3];
41 struct basic_block *parents[3];
42 struct basic_block *bb, *bb1, *bb2, *source;
43 struct instruction *br;
44 pseudo_t p1, p2;
46 bb = insn->bb;
47 if (linearize_ptr_list((struct ptr_list *)insn->phi_list, (void **)array, 3) != 2)
48 return 0;
49 if (linearize_ptr_list((struct ptr_list *)bb->parents, (void **)parents, 3) != 2)
50 return 0;
51 p1 = array[0]->def->src1;
52 bb1 = array[0]->def->bb;
53 p2 = array[1]->def->src1;
54 bb2 = array[1]->def->bb;
56 /* Only try the simple "direct parents" case */
57 if ((bb1 != parents[0] || bb2 != parents[1]) &&
58 (bb1 != parents[1] || bb2 != parents[0]))
59 return 0;
62 * See if we can find a common source for this..
64 source = phi_parent(bb1, p1);
65 if (source != phi_parent(bb2, p2))
66 return 0;
69 * Cool. We now know that 'source' is the exclusive
70 * parent of both phi-nodes, so the exit at the
71 * end of it fully determines which one it is, and
72 * we can turn it into a select.
74 * HOWEVER, right now we only handle regular
75 * conditional branches. No multijumps or computed
76 * stuff. Verify that here.
78 br = last_instruction(source->insns);
79 if (!br || br->opcode != OP_BR)
80 return 0;
82 assert(br->cond);
83 assert(br->bb_false);
86 * We're in business. Match up true/false with p1/p2.
88 if (br->bb_true == bb2 || br->bb_false == bb1) {
89 pseudo_t p = p1;
90 p1 = p2;
91 p2 = p;
95 * Ok, we can now replace that last
97 * br cond, a, b
99 * with the sequence
101 * setcc cond
102 * select pseudo, p1, p2
103 * br cond, a, b
105 * and remove the phi-node. If it then
106 * turns out that 'a' or 'b' is entirely
107 * empty (common case), and now no longer
108 * a phi-source, we'll be able to simplify
109 * the conditional branch too.
111 insert_select(source, br, insn, p1, p2);
112 clear_phi(insn);
113 return REPEAT_CSE;
116 static int clean_up_phi(struct instruction *insn)
118 pseudo_t phi;
119 struct instruction *last;
120 int same;
122 last = NULL;
123 same = 1;
124 FOR_EACH_PTR(insn->phi_list, phi) {
125 struct instruction *def;
126 if (phi == VOID)
127 continue;
128 def = phi->def;
129 if (def->src1 == VOID || !def->bb)
130 continue;
131 if (last) {
132 if (last->src1 != def->src1)
133 same = 0;
134 continue;
136 last = def;
137 } END_FOR_EACH_PTR(phi);
139 if (same) {
140 pseudo_t pseudo = last ? last->src1 : VOID;
141 convert_instruction_target(insn, pseudo);
142 clear_phi(insn);
143 return REPEAT_CSE;
146 return if_convert_phi(insn);
149 static inline void remove_usage(pseudo_t p, pseudo_t *usep)
151 if (has_use_list(p)) {
152 delete_ptr_list_entry((struct ptr_list **)&p->users, usep, 1);
153 if (!p->users)
154 kill_instruction(p->def);
158 void kill_use(pseudo_t *usep)
160 if (usep) {
161 pseudo_t p = *usep;
162 *usep = VOID;
163 remove_usage(p, usep);
167 void kill_instruction(struct instruction *insn)
169 if (!insn || !insn->bb)
170 return;
172 switch (insn->opcode) {
173 case OP_BINARY ... OP_BINCMP_END:
174 insn->bb = NULL;
175 kill_use(&insn->src1);
176 kill_use(&insn->src2);
177 repeat_phase |= REPEAT_CSE;
178 return;
180 case OP_NOT: case OP_NEG:
181 insn->bb = NULL;
182 kill_use(&insn->src1);
183 repeat_phase |= REPEAT_CSE;
184 return;
186 case OP_PHI:
187 insn->bb = NULL;
188 repeat_phase |= REPEAT_CSE;
189 return;
191 case OP_SYMADDR:
192 insn->bb = NULL;
193 repeat_phase |= REPEAT_CSE | REPEAT_SYMBOL_CLEANUP;
194 return;
196 case OP_RANGE:
197 insn->bb = NULL;
198 repeat_phase |= REPEAT_CSE;
199 kill_use(&insn->src1);
200 kill_use(&insn->src2);
201 kill_use(&insn->src3);
202 return;
207 * Kill trivially dead instructions
209 static int dead_insn(struct instruction *insn, pseudo_t *src1, pseudo_t *src2, pseudo_t *src3)
211 pseudo_t *usep;
212 FOR_EACH_PTR(insn->target->users, usep) {
213 if (*usep != VOID)
214 return 0;
215 } END_FOR_EACH_PTR(usep);
217 insn->bb = NULL;
218 kill_use(src1);
219 kill_use(src2);
220 kill_use(src3);
221 return REPEAT_CSE;
224 static inline int constant(pseudo_t pseudo)
226 return pseudo->type == PSEUDO_VAL;
229 static int replace_with_pseudo(struct instruction *insn, pseudo_t pseudo)
231 convert_instruction_target(insn, pseudo);
232 insn->bb = NULL;
233 return REPEAT_CSE;
236 static int simplify_constant_rightside(struct instruction *insn)
238 long long value = insn->src2->value;
240 switch (insn->opcode) {
241 case OP_SUB:
242 if (value) {
243 insn->opcode = OP_ADD;
244 insn->src2 = value_pseudo(-value);
245 return REPEAT_CSE;
247 /* Fallthrough */
248 case OP_ADD:
249 case OP_OR: case OP_XOR:
250 case OP_SHL:
251 case OP_LSR: case OP_ASR:
252 if (!value)
253 return replace_with_pseudo(insn, insn->src1);
254 return 0;
256 case OP_AND:
257 case OP_MULU: case OP_MULS:
258 if (!value)
259 return replace_with_pseudo(insn, insn->src2);
260 return 0;
262 return 0;
265 static int simplify_constant_leftside(struct instruction *insn)
267 long long value = insn->src1->value;
269 switch (insn->opcode) {
270 case OP_ADD: case OP_OR: case OP_XOR:
271 if (!value)
272 return replace_with_pseudo(insn, insn->src2);
273 return 0;
275 case OP_SHL:
276 case OP_LSR: case OP_ASR:
277 case OP_AND:
278 case OP_MULU: case OP_MULS:
279 if (!value)
280 return replace_with_pseudo(insn, insn->src1);
281 return 0;
283 return 0;
286 static int simplify_constant_binop(struct instruction *insn)
288 /* FIXME! Verify signs and sizes!! */
289 long long left = insn->src1->value;
290 long long right = insn->src2->value;
291 unsigned long long ul, ur;
292 long long res, mask, bits;
294 mask = 1ULL << (insn->size-1);
295 bits = mask | (mask-1);
297 if (left & mask)
298 left |= ~bits;
299 if (right & mask)
300 right |= ~bits;
301 ul = left & bits;
302 ur = right & bits;
304 switch (insn->opcode) {
305 case OP_ADD:
306 res = left + right;
307 break;
308 case OP_SUB:
309 res = left - right;
310 break;
311 case OP_MULU:
312 res = ul * ur;
313 break;
314 case OP_MULS:
315 res = left * right;
316 break;
317 case OP_DIVU:
318 if (!ur)
319 return 0;
320 res = ul / ur;
321 break;
322 case OP_DIVS:
323 if (!right)
324 return 0;
325 res = left / right;
326 break;
327 case OP_MODU:
328 if (!ur)
329 return 0;
330 res = ul % ur;
331 break;
332 case OP_MODS:
333 if (!right)
334 return 0;
335 res = left % right;
336 break;
337 case OP_SHL:
338 res = left << right;
339 break;
340 case OP_LSR:
341 res = ul >> ur;
342 break;
343 case OP_ASR:
344 res = left >> right;
345 break;
346 /* Logical */
347 case OP_AND:
348 res = left & right;
349 break;
350 case OP_OR:
351 res = left | right;
352 break;
353 case OP_XOR:
354 res = left ^ right;
355 break;
356 case OP_AND_BOOL:
357 res = left && right;
358 break;
359 case OP_OR_BOOL:
360 res = left || right;
361 break;
363 /* Binary comparison */
364 case OP_SET_EQ:
365 res = left == right;
366 break;
367 case OP_SET_NE:
368 res = left != right;
369 break;
370 case OP_SET_LE:
371 res = left <= right;
372 break;
373 case OP_SET_GE:
374 res = left >= right;
375 break;
376 case OP_SET_LT:
377 res = left < right;
378 break;
379 case OP_SET_GT:
380 res = left > right;
381 break;
382 case OP_SET_B:
383 res = ul < ur;
384 break;
385 case OP_SET_A:
386 res = ul > ur;
387 break;
388 case OP_SET_BE:
389 res = ul <= ur;
390 break;
391 case OP_SET_AE:
392 res = ul >= ur;
393 break;
394 default:
395 return 0;
397 res &= bits;
399 replace_with_pseudo(insn, value_pseudo(res));
400 return REPEAT_CSE;
403 static int simplify_binop(struct instruction *insn)
405 if (dead_insn(insn, &insn->src1, &insn->src2, NULL))
406 return REPEAT_CSE;
407 if (constant(insn->src1)) {
408 if (constant(insn->src2))
409 return simplify_constant_binop(insn);
410 return simplify_constant_leftside(insn);
412 if (constant(insn->src2))
413 return simplify_constant_rightside(insn);
414 return 0;
417 static void switch_pseudo(pseudo_t *pp1, pseudo_t *pp2)
419 pseudo_t p1 = *pp1, p2 = *pp2;
421 use_pseudo(p2, pp1);
422 use_pseudo(p1, pp2);
423 remove_usage(p1, pp1);
424 remove_usage(p2, pp2);
427 static int canonical_order(pseudo_t p1, pseudo_t p2)
429 /* symbol/constants on the right */
430 if (p1->type == PSEUDO_VAL)
431 return p2->type == PSEUDO_VAL;
433 if (p1->type == PSEUDO_SYM)
434 return p2->type == PSEUDO_SYM || p2->type == PSEUDO_VAL;
436 return 1;
439 static int simplify_commutative_binop(struct instruction *insn)
441 if (!canonical_order(insn->src1, insn->src2)) {
442 switch_pseudo(&insn->src1, &insn->src2);
443 return REPEAT_CSE;
445 return 0;
448 static inline int simple_pseudo(pseudo_t pseudo)
450 return pseudo->type == PSEUDO_VAL || pseudo->type == PSEUDO_SYM;
453 static int simplify_associative_binop(struct instruction *insn)
455 struct instruction *def;
456 pseudo_t pseudo = insn->src1;
458 if (!simple_pseudo(insn->src2))
459 return 0;
460 if (pseudo->type != PSEUDO_REG)
461 return 0;
462 def = pseudo->def;
463 if (def == insn)
464 return 0;
465 if (def->opcode != insn->opcode)
466 return 0;
467 if (!simple_pseudo(def->src2))
468 return 0;
469 if (ptr_list_size((struct ptr_list *)def->target->users) != 1)
470 return 0;
471 switch_pseudo(&def->src1, &insn->src2);
472 return REPEAT_CSE;
475 static int simplify_constant_unop(struct instruction *insn)
477 long long val = insn->src1->value;
478 long long res, mask;
480 switch (insn->opcode) {
481 case OP_NOT:
482 res = ~val;
483 break;
484 case OP_NEG:
485 res = -val;
486 break;
487 default:
488 return 0;
490 mask = 1ULL << (insn->size-1);
491 res &= mask | (mask-1);
493 replace_with_pseudo(insn, value_pseudo(res));
494 return REPEAT_CSE;
497 static int simplify_unop(struct instruction *insn)
499 if (dead_insn(insn, &insn->src1, NULL, NULL))
500 return REPEAT_CSE;
501 if (constant(insn->src1))
502 return simplify_constant_unop(insn);
503 return 0;
506 static int simplify_one_memop(struct instruction *insn, pseudo_t orig)
508 pseudo_t addr = insn->src;
509 pseudo_t new, off;
511 if (addr->type == PSEUDO_REG) {
512 struct instruction *def = addr->def;
513 if (def->opcode == OP_SYMADDR && def->src) {
514 kill_use(&insn->src);
515 use_pseudo(def->src, &insn->src);
516 return REPEAT_CSE | REPEAT_SYMBOL_CLEANUP;
518 if (def->opcode == OP_ADD) {
519 new = def->src1;
520 off = def->src2;
521 if (constant(off))
522 goto offset;
523 new = off;
524 off = def->src1;
525 if (constant(off))
526 goto offset;
527 return 0;
530 return 0;
532 offset:
533 /* Invalid code */
534 if (new == orig) {
535 if (new == VOID)
536 return 0;
537 new = VOID;
538 warning(insn->pos, "crazy programmer");
540 insn->offset += off->value;
541 use_pseudo(new, &insn->src);
542 remove_usage(addr, &insn->src);
543 return REPEAT_CSE | REPEAT_SYMBOL_CLEANUP;
547 * We walk the whole chain of adds/subs backwards. That's not
548 * only more efficient, but it allows us to find looops.
550 static int simplify_memop(struct instruction *insn)
552 int one, ret = 0;
553 pseudo_t orig = insn->src;
555 do {
556 one = simplify_one_memop(insn, orig);
557 ret |= one;
558 } while (one);
559 return ret;
562 static int simplify_cast(struct instruction *insn)
564 struct symbol *orig_type;
565 int orig_size, size;
566 pseudo_t src;
568 if (dead_insn(insn, &insn->src, NULL, NULL))
569 return REPEAT_CSE;
571 orig_type = insn->orig_type;
572 if (!orig_type)
573 return 0;
574 orig_size = orig_type->bit_size;
575 size = insn->size;
576 src = insn->src;
578 /* A cast of a "and" might be a no-op.. */
579 if (src->type == PSEUDO_REG) {
580 struct instruction *def = src->def;
581 if (def->opcode == OP_AND && def->size >= size) {
582 pseudo_t val = def->src2;
583 if (val->type == PSEUDO_VAL) {
584 unsigned long long value = val->value;
585 if (!(value >> (size-1)))
586 goto simplify;
591 if (size == orig_size) {
592 int op = (orig_type->ctype.modifiers & MOD_SIGNED) ? OP_SCAST : OP_CAST;
593 if (insn->opcode == op)
594 goto simplify;
597 return 0;
599 simplify:
600 return replace_with_pseudo(insn, insn->src);
603 static int simplify_select(struct instruction *insn)
605 pseudo_t cond, src1, src2;
607 if (dead_insn(insn, &insn->src1, &insn->src2, &insn->src3))
608 return REPEAT_CSE;
610 cond = insn->src1;
611 src1 = insn->src2;
612 src2 = insn->src3;
613 if (constant(cond) || src1 == src2) {
614 pseudo_t *kill, take;
615 kill_use(&insn->src1);
616 take = cond->value ? src1 : src2;
617 kill = cond->value ? &insn->src3 : &insn->src2;
618 kill_use(kill);
619 replace_with_pseudo(insn, take);
620 return REPEAT_CSE;
622 if (constant(src1) && constant(src2)) {
623 long long val1 = src1->value;
624 long long val2 = src2->value;
626 /* The pair 0/1 is special - replace with SETNE/SETEQ */
627 if ((val1 | val2) == 1) {
628 int opcode = OP_SET_EQ;
629 if (val1) {
630 src1 = src2;
631 opcode = OP_SET_NE;
633 insn->opcode = opcode;
634 /* insn->src1 is already cond */
635 insn->src2 = src1; /* Zero */
636 return REPEAT_CSE;
639 return 0;
642 static int is_in_range(pseudo_t src, long long low, long long high)
644 long long value;
646 switch (src->type) {
647 case PSEUDO_VAL:
648 value = src->value;
649 return value >= low && value <= high;
650 default:
651 return 0;
655 static int simplify_range(struct instruction *insn)
657 pseudo_t src1, src2, src3;
659 src1 = insn->src1;
660 src2 = insn->src2;
661 src3 = insn->src3;
662 if (src2->type != PSEUDO_VAL || src3->type != PSEUDO_VAL)
663 return 0;
664 if (is_in_range(src1, src2->value, src3->value)) {
665 kill_instruction(insn);
666 return REPEAT_CSE;
668 return 0;
672 * Simplify "set_ne/eq $0 + br"
674 static int simplify_cond_branch(struct instruction *br, pseudo_t cond, struct instruction *def, pseudo_t *pp)
676 use_pseudo(*pp, &br->cond);
677 remove_usage(cond, &br->cond);
678 if (def->opcode == OP_SET_EQ) {
679 struct basic_block *true = br->bb_true;
680 struct basic_block *false = br->bb_false;
681 br->bb_false = true;
682 br->bb_true = false;
684 return REPEAT_CSE;
687 static int simplify_branch(struct instruction *insn)
689 pseudo_t cond = insn->cond;
691 if (!cond)
692 return 0;
694 /* Constant conditional */
695 if (constant(cond)) {
696 insert_branch(insn->bb, insn, cond->value ? insn->bb_true : insn->bb_false);
697 return REPEAT_CSE;
700 /* Same target? */
701 if (insn->bb_true == insn->bb_false) {
702 struct basic_block *bb = insn->bb;
703 struct basic_block *target = insn->bb_false;
704 remove_bb_from_list(&target->parents, bb, 1);
705 remove_bb_from_list(&bb->children, target, 1);
706 insn->bb_false = NULL;
707 kill_use(&insn->cond);
708 insn->cond = NULL;
709 return REPEAT_CSE;
712 /* Conditional on a SETNE $0 or SETEQ $0 */
713 if (cond->type == PSEUDO_REG) {
714 struct instruction *def = cond->def;
716 if (def->opcode == OP_SET_NE || def->opcode == OP_SET_EQ) {
717 if (constant(def->src1) && !def->src1->value)
718 return simplify_cond_branch(insn, cond, def, &def->src2);
719 if (constant(def->src2) && !def->src2->value)
720 return simplify_cond_branch(insn, cond, def, &def->src1);
722 if (def->opcode == OP_SEL) {
723 if (constant(def->src2) && constant(def->src3)) {
724 long long val1 = def->src2->value;
725 long long val2 = def->src3->value;
726 if (!val1 && !val2) {
727 insert_branch(insn->bb, insn, insn->bb_false);
728 return REPEAT_CSE;
730 if (val1 && val2) {
731 insert_branch(insn->bb, insn, insn->bb_true);
732 return REPEAT_CSE;
734 if (val2) {
735 struct basic_block *true = insn->bb_true;
736 struct basic_block *false = insn->bb_false;
737 insn->bb_false = true;
738 insn->bb_true = false;
740 use_pseudo(def->src1, &insn->cond);
741 remove_usage(cond, &insn->cond);
742 return REPEAT_CSE;
745 if (def->opcode == OP_CAST || def->opcode == OP_SCAST) {
746 int orig_size = def->orig_type ? def->orig_type->bit_size : 0;
747 if (def->size > orig_size) {
748 use_pseudo(def->src, &insn->cond);
749 remove_usage(cond, &insn->cond);
750 return REPEAT_CSE;
754 return 0;
757 static int simplify_switch(struct instruction *insn)
759 pseudo_t cond = insn->cond;
760 long long val;
761 struct multijmp *jmp;
763 if (!constant(cond))
764 return 0;
765 val = insn->cond->value;
767 FOR_EACH_PTR(insn->multijmp_list, jmp) {
768 /* Default case */
769 if (jmp->begin > jmp->end)
770 goto found;
771 if (val >= jmp->begin && val <= jmp->end)
772 goto found;
773 } END_FOR_EACH_PTR(jmp);
774 warning(insn->pos, "Impossible case statement");
775 return 0;
777 found:
778 insert_branch(insn->bb, insn, jmp->target);
779 return REPEAT_CSE;
782 int simplify_instruction(struct instruction *insn)
784 if (!insn->bb)
785 return 0;
786 switch (insn->opcode) {
787 case OP_ADD: case OP_MULS:
788 case OP_AND: case OP_OR: case OP_XOR:
789 case OP_AND_BOOL: case OP_OR_BOOL:
790 if (simplify_binop(insn))
791 return REPEAT_CSE;
792 if (simplify_commutative_binop(insn))
793 return REPEAT_CSE;
794 return simplify_associative_binop(insn);
796 case OP_MULU:
797 case OP_SET_EQ: case OP_SET_NE:
798 if (simplify_binop(insn))
799 return REPEAT_CSE;
800 return simplify_commutative_binop(insn);
802 case OP_SUB:
803 case OP_DIVU: case OP_DIVS:
804 case OP_MODU: case OP_MODS:
805 case OP_SHL:
806 case OP_LSR: case OP_ASR:
807 case OP_SET_LE: case OP_SET_GE:
808 case OP_SET_LT: case OP_SET_GT:
809 case OP_SET_B: case OP_SET_A:
810 case OP_SET_BE: case OP_SET_AE:
811 return simplify_binop(insn);
813 case OP_NOT: case OP_NEG:
814 return simplify_unop(insn);
815 case OP_LOAD: case OP_STORE:
816 return simplify_memop(insn);
817 case OP_SYMADDR:
818 if (dead_insn(insn, NULL, NULL, NULL))
819 return REPEAT_CSE | REPEAT_SYMBOL_CLEANUP;
820 return replace_with_pseudo(insn, insn->symbol);
821 case OP_CAST:
822 case OP_SCAST:
823 case OP_FPCAST:
824 case OP_PTRCAST:
825 return simplify_cast(insn);
826 case OP_PHI:
827 if (dead_insn(insn, NULL, NULL, NULL)) {
828 clear_phi(insn);
829 return REPEAT_CSE;
831 return clean_up_phi(insn);
832 case OP_PHISOURCE:
833 if (dead_insn(insn, &insn->phi_src, NULL, NULL))
834 return REPEAT_CSE;
835 break;
836 case OP_SEL:
837 return simplify_select(insn);
838 case OP_BR:
839 return simplify_branch(insn);
840 case OP_SWITCH:
841 return simplify_switch(insn);
842 case OP_RANGE:
843 return simplify_range(insn);
845 return 0;