Duh. When a function returns VOID, we should _not_ add that
[smatch.git] / simplify.c
blob0d0f1b1991349264d79e38c2524ecb512df67585
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;
199 * Kill trivially dead instructions
201 static int dead_insn(struct instruction *insn, pseudo_t *src1, pseudo_t *src2, pseudo_t *src3)
203 pseudo_t *usep;
204 FOR_EACH_PTR(insn->target->users, usep) {
205 if (*usep != VOID)
206 return 0;
207 } END_FOR_EACH_PTR(usep);
209 insn->bb = NULL;
210 kill_use(src1);
211 kill_use(src2);
212 kill_use(src3);
213 return REPEAT_CSE;
216 static inline int constant(pseudo_t pseudo)
218 return pseudo->type == PSEUDO_VAL;
221 static int replace_with_pseudo(struct instruction *insn, pseudo_t pseudo)
223 convert_instruction_target(insn, pseudo);
224 insn->bb = NULL;
225 return REPEAT_CSE;
228 static int simplify_constant_rightside(struct instruction *insn)
230 long long value = insn->src2->value;
232 switch (insn->opcode) {
233 case OP_SUB:
234 if (value) {
235 insn->opcode = OP_ADD;
236 insn->src2 = value_pseudo(-value);
237 return REPEAT_CSE;
239 /* Fallthrough */
240 case OP_ADD:
241 case OP_OR: case OP_XOR:
242 case OP_SHL:
243 case OP_LSR: case OP_ASR:
244 if (!value)
245 return replace_with_pseudo(insn, insn->src1);
246 return 0;
248 case OP_AND:
249 case OP_MULU: case OP_MULS:
250 if (!value)
251 return replace_with_pseudo(insn, insn->src2);
252 return 0;
254 return 0;
257 static int simplify_constant_leftside(struct instruction *insn)
259 long long value = insn->src1->value;
261 switch (insn->opcode) {
262 case OP_ADD: case OP_OR: case OP_XOR:
263 if (!value)
264 return replace_with_pseudo(insn, insn->src2);
265 return 0;
267 case OP_SHL:
268 case OP_LSR: case OP_ASR:
269 case OP_AND:
270 case OP_MULU: case OP_MULS:
271 if (!value)
272 return replace_with_pseudo(insn, insn->src1);
273 return 0;
275 return 0;
278 static int simplify_constant_binop(struct instruction *insn)
280 /* FIXME! Verify signs and sizes!! */
281 long long left = insn->src1->value;
282 long long right = insn->src2->value;
283 unsigned long long ul, ur;
284 long long res, mask, bits;
286 mask = 1ULL << (insn->size-1);
287 bits = mask | (mask-1);
289 if (left & mask)
290 left |= ~bits;
291 if (right & mask)
292 right |= ~bits;
293 ul = left & bits;
294 ur = right & bits;
296 switch (insn->opcode) {
297 case OP_ADD:
298 res = left + right;
299 break;
300 case OP_SUB:
301 res = left - right;
302 break;
303 case OP_MULU:
304 res = ul * ur;
305 break;
306 case OP_MULS:
307 res = left * right;
308 break;
309 case OP_DIVU:
310 if (!ur)
311 return 0;
312 res = ul / ur;
313 break;
314 case OP_DIVS:
315 if (!right)
316 return 0;
317 res = left / right;
318 break;
319 case OP_MODU:
320 if (!ur)
321 return 0;
322 res = ul % ur;
323 break;
324 case OP_MODS:
325 if (!right)
326 return 0;
327 res = left % right;
328 break;
329 case OP_SHL:
330 res = left << right;
331 break;
332 case OP_LSR:
333 res = ul >> ur;
334 break;
335 case OP_ASR:
336 res = left >> right;
337 break;
338 /* Logical */
339 case OP_AND:
340 res = left & right;
341 break;
342 case OP_OR:
343 res = left | right;
344 break;
345 case OP_XOR:
346 res = left ^ right;
347 break;
348 case OP_AND_BOOL:
349 res = left && right;
350 break;
351 case OP_OR_BOOL:
352 res = left || right;
353 break;
355 /* Binary comparison */
356 case OP_SET_EQ:
357 res = left == right;
358 break;
359 case OP_SET_NE:
360 res = left != right;
361 break;
362 case OP_SET_LE:
363 res = left <= right;
364 break;
365 case OP_SET_GE:
366 res = left >= right;
367 break;
368 case OP_SET_LT:
369 res = left < right;
370 break;
371 case OP_SET_GT:
372 res = left > right;
373 break;
374 case OP_SET_B:
375 res = ul < ur;
376 break;
377 case OP_SET_A:
378 res = ul > ur;
379 break;
380 case OP_SET_BE:
381 res = ul <= ur;
382 break;
383 case OP_SET_AE:
384 res = ul >= ur;
385 break;
386 default:
387 return 0;
389 res &= bits;
391 replace_with_pseudo(insn, value_pseudo(res));
392 return REPEAT_CSE;
395 static int simplify_binop(struct instruction *insn)
397 if (dead_insn(insn, &insn->src1, &insn->src2, NULL))
398 return REPEAT_CSE;
399 if (constant(insn->src1)) {
400 if (constant(insn->src2))
401 return simplify_constant_binop(insn);
402 return simplify_constant_leftside(insn);
404 if (constant(insn->src2))
405 return simplify_constant_rightside(insn);
406 return 0;
409 static void switch_pseudo(pseudo_t *pp1, pseudo_t *pp2)
411 pseudo_t p1 = *pp1, p2 = *pp2;
413 use_pseudo(p2, pp1);
414 use_pseudo(p1, pp2);
415 remove_usage(p1, pp1);
416 remove_usage(p2, pp2);
419 static int canonical_order(pseudo_t p1, pseudo_t p2)
421 /* symbol/constants on the right */
422 if (p1->type == PSEUDO_VAL)
423 return p2->type == PSEUDO_VAL;
425 if (p1->type == PSEUDO_SYM)
426 return p2->type == PSEUDO_SYM || p2->type == PSEUDO_VAL;
428 return 1;
431 static int simplify_commutative_binop(struct instruction *insn)
433 if (!canonical_order(insn->src1, insn->src2)) {
434 switch_pseudo(&insn->src1, &insn->src2);
435 return REPEAT_CSE;
437 return 0;
440 static inline int simple_pseudo(pseudo_t pseudo)
442 return pseudo->type == PSEUDO_VAL || pseudo->type == PSEUDO_SYM;
445 static int simplify_associative_binop(struct instruction *insn)
447 struct instruction *def;
448 pseudo_t pseudo = insn->src1;
450 if (!simple_pseudo(insn->src2))
451 return 0;
452 if (pseudo->type != PSEUDO_REG)
453 return 0;
454 def = pseudo->def;
455 if (def == insn)
456 return 0;
457 if (def->opcode != insn->opcode)
458 return 0;
459 if (!simple_pseudo(def->src2))
460 return 0;
461 if (ptr_list_size((struct ptr_list *)def->target->users) != 1)
462 return 0;
463 switch_pseudo(&def->src1, &insn->src2);
464 return REPEAT_CSE;
467 static int simplify_constant_unop(struct instruction *insn)
469 long long val = insn->src1->value;
470 long long res, mask;
472 switch (insn->opcode) {
473 case OP_NOT:
474 res = ~val;
475 break;
476 case OP_NEG:
477 res = -val;
478 break;
479 default:
480 return 0;
482 mask = 1ULL << (insn->size-1);
483 res &= mask | (mask-1);
485 replace_with_pseudo(insn, value_pseudo(res));
486 return REPEAT_CSE;
489 static int simplify_unop(struct instruction *insn)
491 if (dead_insn(insn, &insn->src1, NULL, NULL))
492 return REPEAT_CSE;
493 if (constant(insn->src1))
494 return simplify_constant_unop(insn);
495 return 0;
498 static int simplify_one_memop(struct instruction *insn, pseudo_t orig)
500 pseudo_t addr = insn->src;
501 pseudo_t new, off;
503 if (addr->type == PSEUDO_REG) {
504 struct instruction *def = addr->def;
505 if (def->opcode == OP_SYMADDR && def->src) {
506 kill_use(&insn->src);
507 use_pseudo(def->src, &insn->src);
508 return REPEAT_CSE | REPEAT_SYMBOL_CLEANUP;
510 if (def->opcode == OP_ADD) {
511 new = def->src1;
512 off = def->src2;
513 if (constant(off))
514 goto offset;
515 new = off;
516 off = def->src1;
517 if (constant(off))
518 goto offset;
519 return 0;
522 return 0;
524 offset:
525 /* Invalid code */
526 if (new == orig) {
527 if (new == VOID)
528 return 0;
529 new = VOID;
530 warning(insn->bb->pos, "crazy programmer");
532 insn->offset += off->value;
533 use_pseudo(new, &insn->src);
534 remove_usage(addr, &insn->src);
535 return REPEAT_CSE | REPEAT_SYMBOL_CLEANUP;
539 * We walk the whole chain of adds/subs backwards. That's not
540 * only more efficient, but it allows us to find looops.
542 static int simplify_memop(struct instruction *insn)
544 int one, ret = 0;
545 pseudo_t orig = insn->src;
547 do {
548 one = simplify_one_memop(insn, orig);
549 ret |= one;
550 } while (one);
551 return ret;
554 static int simplify_cast(struct instruction *insn)
556 int orig_size;
558 if (dead_insn(insn, &insn->src, NULL, NULL))
559 return REPEAT_CSE;
560 if (0 && insn->opcode == OP_PTRCAST)
561 return 0;
562 orig_size = insn->orig_type ? insn->orig_type->bit_size : 0;
563 if (orig_size < 0)
564 orig_size = 0;
565 if (insn->size != orig_size)
566 return 0;
567 return replace_with_pseudo(insn, insn->src);
570 static int simplify_select(struct instruction *insn)
572 pseudo_t cond, src1, src2;
574 if (dead_insn(insn, &insn->src1, &insn->src2, &insn->src3))
575 return REPEAT_CSE;
577 cond = insn->src1;
578 src1 = insn->src2;
579 src2 = insn->src3;
580 if (constant(cond) || src1 == src2) {
581 pseudo_t *kill, take;
582 kill_use(&insn->src1);
583 take = cond->value ? src1 : src2;
584 kill = cond->value ? &insn->src3 : &insn->src2;
585 kill_use(kill);
586 replace_with_pseudo(insn, take);
587 return REPEAT_CSE;
589 if (constant(src1) && constant(src2)) {
590 long long val1 = src1->value;
591 long long val2 = src2->value;
593 /* The pair 0/1 is special - replace with SETNE/SETEQ */
594 if ((val1 | val2) == 1) {
595 int opcode = OP_SET_EQ;
596 if (val1) {
597 src1 = src2;
598 opcode = OP_SET_NE;
600 insn->opcode = opcode;
601 /* insn->src1 is already cond */
602 insn->src2 = src1; /* Zero */
603 return REPEAT_CSE;
606 return 0;
610 * Simplify "set_ne/eq $0 + br"
612 static int simplify_cond_branch(struct instruction *br, pseudo_t cond, struct instruction *def, pseudo_t *pp)
614 use_pseudo(*pp, &br->cond);
615 remove_usage(cond, &br->cond);
616 if (def->opcode == OP_SET_EQ) {
617 struct basic_block *true = br->bb_true;
618 struct basic_block *false = br->bb_false;
619 br->bb_false = true;
620 br->bb_true = false;
622 return REPEAT_CSE;
625 static int simplify_branch(struct instruction *insn)
627 pseudo_t cond = insn->cond;
629 if (!cond)
630 return 0;
632 /* Constant conditional */
633 if (constant(cond)) {
634 insert_branch(insn->bb, insn, cond->value ? insn->bb_true : insn->bb_false);
635 return REPEAT_CSE;
638 /* Same target? */
639 if (insn->bb_true == insn->bb_false) {
640 struct basic_block *bb = insn->bb;
641 struct basic_block *target = insn->bb_false;
642 remove_bb_from_list(&target->parents, bb, 1);
643 remove_bb_from_list(&bb->children, target, 1);
644 insn->bb_false = NULL;
645 kill_use(&insn->cond);
646 insn->cond = NULL;
647 return REPEAT_CSE;
650 /* Conditional on a SETNE $0 or SETEQ $0 */
651 if (cond->type == PSEUDO_REG) {
652 struct instruction *def = cond->def;
654 if (def->opcode == OP_SET_NE || def->opcode == OP_SET_EQ) {
655 if (constant(def->src1) && !def->src1->value)
656 return simplify_cond_branch(insn, cond, def, &def->src2);
657 if (constant(def->src2) && !def->src2->value)
658 return simplify_cond_branch(insn, cond, def, &def->src1);
660 if (def->opcode == OP_SEL) {
661 if (constant(def->src2) && constant(def->src3)) {
662 long long val1 = def->src2->value;
663 long long val2 = def->src3->value;
664 if (!val1 && !val2) {
665 insert_branch(insn->bb, insn, insn->bb_false);
666 return REPEAT_CSE;
668 if (val1 && val2) {
669 insert_branch(insn->bb, insn, insn->bb_true);
670 return REPEAT_CSE;
672 if (val2) {
673 struct basic_block *true = insn->bb_true;
674 struct basic_block *false = insn->bb_false;
675 insn->bb_false = true;
676 insn->bb_true = false;
678 use_pseudo(def->src1, &insn->cond);
679 remove_usage(cond, &insn->cond);
680 return REPEAT_CSE;
683 if (def->opcode == OP_CAST) {
684 int orig_size = def->orig_type ? def->orig_type->bit_size : 0;
685 if (def->size > orig_size) {
686 use_pseudo(def->src, &insn->cond);
687 remove_usage(cond, &insn->cond);
688 return REPEAT_CSE;
692 return 0;
695 static int simplify_switch(struct instruction *insn)
697 pseudo_t cond = insn->cond;
698 long long val;
699 struct multijmp *jmp;
701 if (!constant(cond))
702 return 0;
703 val = insn->cond->value;
705 FOR_EACH_PTR(insn->multijmp_list, jmp) {
706 /* Default case */
707 if (jmp->begin > jmp->end)
708 goto found;
709 if (val >= jmp->begin && val <= jmp->end)
710 goto found;
711 } END_FOR_EACH_PTR(jmp);
712 warning(insn->bb->pos, "Impossible case statement");
713 return 0;
715 found:
716 insert_branch(insn->bb, insn, jmp->target);
717 return REPEAT_CSE;
720 int simplify_instruction(struct instruction *insn)
722 if (!insn->bb)
723 return 0;
724 switch (insn->opcode) {
725 case OP_ADD: case OP_MULS:
726 case OP_AND: case OP_OR: case OP_XOR:
727 case OP_AND_BOOL: case OP_OR_BOOL:
728 if (simplify_binop(insn))
729 return REPEAT_CSE;
730 if (simplify_commutative_binop(insn))
731 return REPEAT_CSE;
732 return simplify_associative_binop(insn);
734 case OP_MULU:
735 case OP_SET_EQ: case OP_SET_NE:
736 if (simplify_binop(insn))
737 return REPEAT_CSE;
738 return simplify_commutative_binop(insn);
740 case OP_SUB:
741 case OP_DIVU: case OP_DIVS:
742 case OP_MODU: case OP_MODS:
743 case OP_SHL:
744 case OP_LSR: case OP_ASR:
745 case OP_SET_LE: case OP_SET_GE:
746 case OP_SET_LT: case OP_SET_GT:
747 case OP_SET_B: case OP_SET_A:
748 case OP_SET_BE: case OP_SET_AE:
749 return simplify_binop(insn);
751 case OP_NOT: case OP_NEG:
752 return simplify_unop(insn);
753 case OP_LOAD: case OP_STORE:
754 return simplify_memop(insn);
755 case OP_SYMADDR:
756 if (dead_insn(insn, NULL, NULL, NULL))
757 return REPEAT_CSE | REPEAT_SYMBOL_CLEANUP;
758 return replace_with_pseudo(insn, insn->symbol);
759 case OP_PTRCAST:
760 case OP_CAST:
761 return simplify_cast(insn);
762 case OP_PHI:
763 if (dead_insn(insn, NULL, NULL, NULL)) {
764 clear_phi(insn);
765 return REPEAT_CSE;
767 return clean_up_phi(insn);
768 case OP_PHISOURCE:
769 if (dead_insn(insn, &insn->phi_src, NULL, NULL))
770 return REPEAT_CSE;
771 break;
772 case OP_SEL:
773 return simplify_select(insn);
774 case OP_BR:
775 return simplify_branch(insn);
776 case OP_SWITCH:
777 return simplify_switch(insn);
779 return 0;