2 * Flow - walk the linearized flowgraph, simplifying it as we
5 * Copyright (C) 2004 Linus Torvalds
16 #include "expression.h"
17 #include "linearize.h"
20 unsigned long bb_generation
;
23 * Dammit, if we have a phi-node followed by a conditional
24 * branch on that phi-node, we should damn well be able to
25 * do something about the source. Maybe.
27 static int rewrite_branch(struct basic_block
*bb
,
28 struct basic_block
**ptr
,
29 struct basic_block
*old
,
30 struct basic_block
*new)
32 if (*ptr
!= old
|| new == old
)
35 /* We might find new if-conversions or non-dominating CSEs */
36 repeat_phase
|= REPEAT_CSE
;
38 replace_bb_in_list(&bb
->children
, old
, new, 1);
39 remove_bb_from_list(&old
->parents
, bb
, 1);
40 add_bb(&new->parents
, bb
);
45 * Return the known truth value of a pseudo, or -1 if
48 static int pseudo_truth_value(pseudo_t pseudo
)
50 switch (pseudo
->type
) {
52 return !!pseudo
->value
;
55 struct instruction
*insn
= pseudo
->def
;
57 /* A symbol address is always considered true.. */
58 if (insn
->opcode
== OP_SYMADDR
&& insn
->target
== pseudo
)
68 * Does a basic block depend on the pseudos that "src" defines?
70 static int bb_depends_on(struct basic_block
*target
, struct basic_block
*src
)
74 FOR_EACH_PTR(src
->defines
, pseudo
) {
75 if (pseudo_in_list(target
->needs
, pseudo
))
77 } END_FOR_EACH_PTR(pseudo
);
82 * When we reach here, we have:
83 * - a basic block that ends in a conditional branch and
84 * that has no side effects apart from the pseudos it
86 * - the phi-node that the conditional branch depends on
87 * - full pseudo liveness information
89 * We need to check if any of the _sources_ of the phi-node
90 * may be constant, and not actually need this block at all.
92 static int try_to_simplify_bb(struct basic_block
*bb
, struct instruction
*first
, struct instruction
*second
)
97 FOR_EACH_PTR(first
->phi_list
, phi
) {
98 struct instruction
*def
= phi
->def
;
99 struct basic_block
*source
, *target
;
101 struct instruction
*br
;
108 if (!pseudo
|| !source
)
110 br
= last_instruction(source
->insns
);
113 if (br
->opcode
!= OP_BR
)
115 true = pseudo_truth_value(pseudo
);
118 target
= true ? second
->bb_true
: second
->bb_false
;
119 if (bb_depends_on(target
, bb
))
121 changed
|= rewrite_branch(source
, &br
->bb_true
, bb
, target
);
122 changed
|= rewrite_branch(source
, &br
->bb_false
, bb
, target
);
123 } END_FOR_EACH_PTR(phi
);
127 static int bb_has_side_effects(struct basic_block
*bb
)
129 struct instruction
*insn
;
130 FOR_EACH_PTR(bb
->insns
, insn
) {
131 switch (insn
->opcode
) {
133 /* Fixme! This should take "const" etc into account */
141 /* Fixme! This should take "volatile" etc into account */
147 } END_FOR_EACH_PTR(insn
);
151 static int simplify_phi_branch(struct basic_block
*bb
, struct instruction
*br
)
153 pseudo_t cond
= br
->cond
;
154 struct instruction
*def
;
156 if (cond
->type
!= PSEUDO_REG
)
159 if (def
->bb
!= bb
|| def
->opcode
!= OP_PHI
)
161 if (bb_has_side_effects(bb
))
163 return try_to_simplify_bb(bb
, def
, br
);
166 static int simplify_branch_branch(struct basic_block
*bb
, struct instruction
*br
,
167 struct basic_block
**target_p
, int true)
169 struct basic_block
*target
= *target_p
, *final
;
170 struct instruction
*insn
;
175 insn
= last_instruction(target
->insns
);
176 if (!insn
|| insn
->opcode
!= OP_BR
|| insn
->cond
!= br
->cond
)
179 * Ahhah! We've found a branch to a branch on the same conditional!
180 * Now we just need to see if we can rewrite the branch..
183 final
= true ? insn
->bb_true
: insn
->bb_false
;
184 if (bb_has_side_effects(target
))
185 goto try_to_rewrite_target
;
186 if (bb_depends_on(final
, target
))
187 goto try_to_rewrite_target
;
188 return rewrite_branch(bb
, target_p
, target
, final
);
190 try_to_rewrite_target
:
192 * If we're the only parent, at least we can rewrite the
193 * now-known second branch.
195 if (bb_list_size(target
->parents
) != 1)
197 insert_branch(target
, insn
, final
);
201 static int simplify_one_branch(struct basic_block
*bb
, struct instruction
*br
)
203 if (simplify_phi_branch(bb
, br
))
205 return simplify_branch_branch(bb
, br
, &br
->bb_true
, 1) |
206 simplify_branch_branch(bb
, br
, &br
->bb_false
, 0);
209 static int simplify_branch_nodes(struct entrypoint
*ep
)
212 struct basic_block
*bb
;
214 FOR_EACH_PTR(ep
->bbs
, bb
) {
215 struct instruction
*br
= last_instruction(bb
->insns
);
217 if (!br
|| br
->opcode
!= OP_BR
|| !br
->bb_false
)
219 changed
|= simplify_one_branch(bb
, br
);
220 } END_FOR_EACH_PTR(bb
);
225 * This is called late - when we have intra-bb liveness information..
227 int simplify_flow(struct entrypoint
*ep
)
229 return simplify_branch_nodes(ep
);
232 static inline void concat_user_list(struct pseudo_ptr_list
*src
, struct pseudo_ptr_list
**dst
)
234 concat_ptr_list((struct ptr_list
*)src
, (struct ptr_list
**)dst
);
237 void convert_instruction_target(struct instruction
*insn
, pseudo_t src
)
239 pseudo_t target
, *usep
;
242 * Go through the "insn->users" list and replace them all..
244 target
= insn
->target
;
247 FOR_EACH_PTR(target
->users
, usep
) {
249 assert(*usep
== target
);
252 } END_FOR_EACH_PTR(usep
);
253 concat_user_list(target
->users
, &src
->users
);
254 target
->users
= NULL
;
257 void convert_load_instruction(struct instruction
*insn
, pseudo_t src
)
259 convert_instruction_target(insn
, src
);
260 /* Turn the load into a no-op */
261 insn
->opcode
= OP_LNOP
;
265 static int overlapping_memop(struct instruction
*a
, struct instruction
*b
)
267 unsigned int a_start
= a
->offset
<< 3;
268 unsigned int b_start
= b
->offset
<< 3;
269 unsigned int a_size
= a
->size
;
270 unsigned int b_size
= b
->size
;
272 if (a_size
+ a_start
<= b_start
)
274 if (b_size
+ b_start
<= a_start
)
279 static inline int same_memop(struct instruction
*a
, struct instruction
*b
)
281 return a
->offset
== b
->offset
&& a
->size
== b
->size
;
285 * Return 1 if "one" dominates the access to 'pseudo'
288 * Return 0 if it doesn't, and -1 if you don't know.
290 int dominates(pseudo_t pseudo
, struct instruction
*insn
, struct instruction
*dom
, int local
)
292 int opcode
= dom
->opcode
;
294 if (opcode
== OP_CALL
|| opcode
== OP_ENTRY
)
295 return local
? 0 : -1;
296 if (opcode
!= OP_LOAD
&& opcode
!= OP_STORE
)
298 if (dom
->src
!= pseudo
) {
301 /* We don't think two explicitly different symbols ever alias */
302 if (dom
->src
->type
== PSEUDO_SYM
)
304 /* We could try to do some alias analysis here */
307 if (!same_memop(insn
, dom
)) {
308 if (dom
->opcode
== OP_LOAD
)
310 if (!overlapping_memop(insn
, dom
))
317 static int find_dominating_parents(pseudo_t pseudo
, struct instruction
*insn
,
318 struct basic_block
*bb
, unsigned long generation
, struct pseudo_list
**dominators
,
319 int local
, int loads
)
321 struct basic_block
*parent
;
326 if (bb_list_size(bb
->parents
) > 1)
328 FOR_EACH_PTR(bb
->parents
, parent
) {
329 struct instruction
*one
;
330 struct instruction
*br
;
333 FOR_EACH_PTR_REVERSE(parent
->insns
, one
) {
337 dominance
= dominates(pseudo
, insn
, one
, local
);
339 if (one
->opcode
== OP_LOAD
)
345 if (one
->opcode
== OP_LOAD
&& !loads
)
347 goto found_dominator
;
348 } END_FOR_EACH_PTR_REVERSE(one
);
350 if (parent
->generation
== generation
)
352 parent
->generation
= generation
;
354 if (!find_dominating_parents(pseudo
, insn
, parent
, generation
, dominators
, local
, loads
))
359 br
= delete_last_instruction(&parent
->insns
);
360 phi
= alloc_phi(parent
, one
->target
, one
->size
);
361 phi
->ident
= phi
->ident
? : pseudo
->ident
;
362 add_instruction(&parent
->insns
, br
);
363 use_pseudo(phi
, add_pseudo(dominators
, phi
));
364 } END_FOR_EACH_PTR(parent
);
369 * We should probably sort the phi list just to make it easier to compare
370 * later for equality.
372 void rewrite_load_instruction(struct instruction
*insn
, struct pseudo_list
*dominators
)
377 * Check for somewhat common case of duplicate
380 new = first_pseudo(dominators
)->def
->src1
;
381 FOR_EACH_PTR(dominators
, phi
) {
382 if (new != phi
->def
->src1
)
384 new->ident
= new->ident
? : phi
->ident
;
385 } END_FOR_EACH_PTR(phi
);
388 * All the same pseudo - mark the phi-nodes unused
389 * and convert the load into a LNOP and replace the
392 FOR_EACH_PTR(dominators
, phi
) {
394 } END_FOR_EACH_PTR(phi
);
395 convert_load_instruction(insn
, new);
399 /* We leave symbol pseudos with a bogus usage list here */
400 if (insn
->src
->type
!= PSEUDO_SYM
)
401 kill_use(&insn
->src
);
402 insn
->opcode
= OP_PHI
;
403 insn
->phi_list
= dominators
;
406 static int find_dominating_stores(pseudo_t pseudo
, struct instruction
*insn
,
407 unsigned long generation
, int local
)
409 struct basic_block
*bb
= insn
->bb
;
410 struct instruction
*one
, *dom
= NULL
;
411 struct pseudo_list
*dominators
;
414 /* Unreachable load? Undo it */
416 insn
->opcode
= OP_LNOP
;
421 FOR_EACH_PTR(bb
->insns
, one
) {
425 dominance
= dominates(pseudo
, insn
, one
, local
);
427 /* Ignore partial load dominators */
428 if (one
->opcode
== OP_LOAD
)
438 } END_FOR_EACH_PTR(one
);
440 warning(pseudo
->sym
->pos
, "unable to find symbol read");
447 convert_load_instruction(insn
, dom
->target
);
451 /* Ok, go find the parents */
452 bb
->generation
= generation
;
455 if (!find_dominating_parents(pseudo
, insn
, bb
, generation
, &dominators
, local
, 1))
458 /* This happens with initial assignments to structures etc.. */
463 convert_load_instruction(insn
, value_pseudo(0));
468 * If we find just one dominating instruction, we
469 * can turn it into a direct thing. Otherwise we'll
470 * have to turn the load into a phi-node of the
473 rewrite_load_instruction(insn
, dominators
);
477 static void kill_store(struct instruction
*insn
)
481 insn
->opcode
= OP_SNOP
;
482 kill_use(&insn
->target
);
486 /* Kill a pseudo that is dead on exit from the bb */
487 static void kill_dead_stores(pseudo_t pseudo
, unsigned long generation
, struct basic_block
*bb
, int local
)
489 struct instruction
*insn
;
490 struct basic_block
*parent
;
492 if (bb
->generation
== generation
)
494 bb
->generation
= generation
;
495 FOR_EACH_PTR_REVERSE(bb
->insns
, insn
) {
496 int opcode
= insn
->opcode
;
498 if (opcode
!= OP_LOAD
&& opcode
!= OP_STORE
) {
501 if (opcode
== OP_CALL
)
505 if (insn
->src
== pseudo
) {
506 if (opcode
== OP_LOAD
)
513 if (insn
->src
->type
!= PSEUDO_SYM
)
515 } END_FOR_EACH_PTR_REVERSE(insn
);
517 FOR_EACH_PTR(bb
->parents
, parent
) {
518 struct basic_block
*child
;
519 FOR_EACH_PTR(parent
->children
, child
) {
520 if (child
&& child
!= bb
)
522 } END_FOR_EACH_PTR(child
);
523 kill_dead_stores(pseudo
, generation
, parent
, local
);
524 } END_FOR_EACH_PTR(parent
);
528 * This should see if the "insn" trivially dominates some previous store, and kill the
529 * store if unnecessary.
531 static void kill_dominated_stores(pseudo_t pseudo
, struct instruction
*insn
,
532 unsigned long generation
, struct basic_block
*bb
, int local
, int found
)
534 struct instruction
*one
;
535 struct basic_block
*parent
;
537 /* Unreachable store? Undo it */
542 if (bb
->generation
== generation
)
544 bb
->generation
= generation
;
545 FOR_EACH_PTR_REVERSE(bb
->insns
, one
) {
553 dominance
= dominates(pseudo
, insn
, one
, local
);
558 if (one
->opcode
== OP_LOAD
)
561 } END_FOR_EACH_PTR_REVERSE(one
);
564 warning(bb
->pos
, "Unable to find instruction");
568 FOR_EACH_PTR(bb
->parents
, parent
) {
569 struct basic_block
*child
;
570 FOR_EACH_PTR(parent
->children
, child
) {
571 if (child
&& child
!= bb
)
573 } END_FOR_EACH_PTR(child
);
574 kill_dominated_stores(pseudo
, insn
, generation
, parent
, local
, found
);
575 } END_FOR_EACH_PTR(parent
);
578 void check_access(struct instruction
*insn
)
580 pseudo_t pseudo
= insn
->src
;
582 if (insn
->bb
&& pseudo
->type
== PSEUDO_SYM
) {
583 int offset
= insn
->offset
, bit
= (offset
<< 3) + insn
->size
;
584 struct symbol
*sym
= pseudo
->sym
;
586 if (sym
->bit_size
> 0 && (offset
< 0 || bit
> sym
->bit_size
))
587 warning(insn
->pos
, "invalid access %s '%s' (%d %d)",
588 offset
< 0 ? "below" : "past the end of",
589 show_ident(sym
->ident
), offset
, sym
->bit_size
>> 3);
593 static void simplify_one_symbol(struct entrypoint
*ep
, struct symbol
*sym
)
595 pseudo_t pseudo
, src
, *pp
;
596 struct instruction
*def
;
598 int all
, stores
, complex;
600 /* Never used as a symbol? */
601 pseudo
= sym
->pseudo
;
605 /* We don't do coverage analysis of volatiles.. */
606 if (sym
->ctype
.modifiers
& MOD_VOLATILE
)
609 /* ..and symbols with external visibility need more care */
610 mod
= sym
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
);
612 goto external_visibility
;
617 FOR_EACH_PTR(pseudo
->users
, pp
) {
618 /* We know that the symbol-pseudo use is the "src" in the instruction */
619 struct instruction
*insn
= container(pp
, struct instruction
, src
);
621 switch (insn
->opcode
) {
631 mod
|= MOD_ADDRESSABLE
;
632 goto external_visibility
;
639 warning(sym
->pos
, "symbol '%s' pseudo used in unexpected way", show_ident(sym
->ident
));
641 complex |= insn
->offset
;
642 } END_FOR_EACH_PTR(pp
);
650 * Goodie, we have a single store (if even that) in the whole
651 * thing. Replace all loads with moves from the pseudo,
652 * replace the store with a def.
658 FOR_EACH_PTR(pseudo
->users
, pp
) {
659 struct instruction
*insn
= container(pp
, struct instruction
, src
);
660 if (insn
->opcode
== OP_LOAD
) {
662 convert_load_instruction(insn
, src
);
664 } END_FOR_EACH_PTR(pp
);
666 /* Turn the store into a no-op */
674 FOR_EACH_PTR_REVERSE(pseudo
->users
, pp
) {
675 struct instruction
*insn
= container(pp
, struct instruction
, src
);
676 if (insn
->opcode
== OP_LOAD
)
677 all
&= find_dominating_stores(pseudo
, insn
, ++bb_generation
, !mod
);
678 } END_FOR_EACH_PTR_REVERSE(pp
);
680 /* If we converted all the loads, remove the stores. They are dead */
682 FOR_EACH_PTR(pseudo
->users
, pp
) {
683 struct instruction
*insn
= container(pp
, struct instruction
, src
);
684 if (insn
->opcode
== OP_STORE
)
686 } END_FOR_EACH_PTR(pp
);
689 * If we couldn't take the shortcut, see if we can at least kill some
692 FOR_EACH_PTR(pseudo
->users
, pp
) {
693 struct instruction
*insn
= container(pp
, struct instruction
, src
);
694 if (insn
->opcode
== OP_STORE
)
695 kill_dominated_stores(pseudo
, insn
, ++bb_generation
, insn
->bb
, !mod
, 0);
696 } END_FOR_EACH_PTR(pp
);
698 if (!(mod
& (MOD_NONLOCAL
| MOD_STATIC
))) {
699 struct basic_block
*bb
;
700 FOR_EACH_PTR(ep
->bbs
, bb
) {
702 kill_dead_stores(pseudo
, ++bb_generation
, bb
, !mod
);
703 } END_FOR_EACH_PTR(bb
);
710 void simplify_symbol_usage(struct entrypoint
*ep
)
714 FOR_EACH_PTR(ep
->accesses
, sym
) {
715 simplify_one_symbol(ep
, sym
);
716 } END_FOR_EACH_PTR(sym
);
719 static void mark_bb_reachable(struct basic_block
*bb
, unsigned long generation
)
721 struct basic_block
*child
;
723 if (bb
->generation
== generation
)
725 bb
->generation
= generation
;
726 FOR_EACH_PTR(bb
->children
, child
) {
727 mark_bb_reachable(child
, generation
);
728 } END_FOR_EACH_PTR(child
);
731 static void kill_defs(struct instruction
*insn
)
733 pseudo_t target
= insn
->target
;
735 if (!has_use_list(target
))
737 if (target
->def
!= insn
)
740 convert_instruction_target(insn
, VOID
);
743 void kill_bb(struct basic_block
*bb
)
745 struct instruction
*insn
;
746 struct basic_block
*child
, *parent
;
748 FOR_EACH_PTR(bb
->insns
, insn
) {
749 kill_instruction(insn
);
752 * We kill unreachable instructions even if they
753 * otherwise aren't "killable". Eg volatile loads
757 } END_FOR_EACH_PTR(insn
);
760 FOR_EACH_PTR(bb
->children
, child
) {
761 remove_bb_from_list(&child
->parents
, bb
, 0);
762 } END_FOR_EACH_PTR(child
);
765 FOR_EACH_PTR(bb
->parents
, parent
) {
766 remove_bb_from_list(&parent
->children
, bb
, 0);
767 } END_FOR_EACH_PTR(parent
);
771 void kill_unreachable_bbs(struct entrypoint
*ep
)
773 struct basic_block
*bb
;
774 unsigned long generation
= ++bb_generation
;
776 mark_bb_reachable(ep
->entry
->bb
, generation
);
777 FOR_EACH_PTR(ep
->bbs
, bb
) {
778 if (bb
->generation
== generation
)
780 /* Mark it as being dead */
783 DELETE_CURRENT_PTR(bb
);
784 } END_FOR_EACH_PTR(bb
);
785 PACK_PTR_LIST(&ep
->bbs
);
788 static int rewrite_parent_branch(struct basic_block
*bb
, struct basic_block
*old
, struct basic_block
*new)
791 struct instruction
*insn
= last_instruction(bb
->insns
);
796 /* Infinite loops: let's not "optimize" them.. */
800 switch (insn
->opcode
) {
802 changed
|= rewrite_branch(bb
, &insn
->bb_true
, old
, new);
803 changed
|= rewrite_branch(bb
, &insn
->bb_false
, old
, new);
807 struct multijmp
*jmp
;
808 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
809 changed
|= rewrite_branch(bb
, &jmp
->target
, old
, new);
810 } END_FOR_EACH_PTR(jmp
);
819 static struct basic_block
* rewrite_branch_bb(struct basic_block
*bb
, struct instruction
*br
)
821 struct basic_block
*parent
;
822 struct basic_block
*target
= br
->bb_true
;
823 struct basic_block
*false = br
->bb_false
;
825 if (target
&& false) {
826 pseudo_t cond
= br
->cond
;
827 if (cond
->type
!= PSEUDO_VAL
)
829 target
= cond
->value
? target
: false;
833 * We can't do FOR_EACH_PTR() here, because the parent list
834 * may change when we rewrite the parent.
836 while ((parent
= first_basic_block(bb
->parents
)) != NULL
) {
837 if (!rewrite_parent_branch(parent
, bb
, target
))
843 static void vrfy_bb_in_list(struct basic_block
*bb
, struct basic_block_list
*list
)
846 struct basic_block
*tmp
;
847 int no_bb_in_list
= 0;
849 FOR_EACH_PTR(list
, tmp
) {
852 } END_FOR_EACH_PTR(tmp
);
853 assert(no_bb_in_list
);
857 static void vrfy_parents(struct basic_block
*bb
)
859 struct basic_block
*tmp
;
860 FOR_EACH_PTR(bb
->parents
, tmp
) {
861 vrfy_bb_in_list(bb
, tmp
->children
);
862 } END_FOR_EACH_PTR(tmp
);
865 static void vrfy_children(struct basic_block
*bb
)
867 struct basic_block
*tmp
;
868 struct instruction
*br
= last_instruction(bb
->insns
);
871 assert(!bb
->children
);
874 switch (br
->opcode
) {
875 struct multijmp
*jmp
;
877 vrfy_bb_in_list(br
->bb_true
, bb
->children
);
878 vrfy_bb_in_list(br
->bb_false
, bb
->children
);
881 case OP_COMPUTEDGOTO
:
882 FOR_EACH_PTR(br
->multijmp_list
, jmp
) {
883 vrfy_bb_in_list(jmp
->target
, bb
->children
);
884 } END_FOR_EACH_PTR(jmp
);
890 FOR_EACH_PTR(bb
->children
, tmp
) {
891 vrfy_bb_in_list(bb
, tmp
->parents
);
892 } END_FOR_EACH_PTR(tmp
);
895 static void vrfy_bb_flow(struct basic_block
*bb
)
901 void vrfy_flow(struct entrypoint
*ep
)
903 struct basic_block
*bb
;
904 struct basic_block
*entry
= ep
->entry
->bb
;
906 FOR_EACH_PTR(ep
->bbs
, bb
) {
910 } END_FOR_EACH_PTR(bb
);
914 void pack_basic_blocks(struct entrypoint
*ep
)
916 struct basic_block
*bb
;
918 /* See if we can merge a bb into another one.. */
919 FOR_EACH_PTR(ep
->bbs
, bb
) {
920 struct instruction
*first
, *insn
;
921 struct basic_block
*parent
, *child
, *last
;
923 if (!bb_reachable(bb
))
929 FOR_EACH_PTR(bb
->insns
, first
) {
932 switch (first
->opcode
) {
933 case OP_NOP
: case OP_LNOP
: case OP_SNOP
:
936 struct basic_block
*replace
;
937 replace
= rewrite_branch_bb(bb
, first
);
947 } END_FOR_EACH_PTR(first
);
951 * See if we only have one parent..
954 FOR_EACH_PTR(bb
->parents
, parent
) {
961 } END_FOR_EACH_PTR(parent
);
964 if (!parent
|| parent
== bb
)
968 * Goodie. See if the parent can merge..
970 FOR_EACH_PTR(parent
->children
, child
) {
973 } END_FOR_EACH_PTR(child
);
978 repeat_phase
|= REPEAT_CSE
;
980 parent
->children
= bb
->children
;
984 FOR_EACH_PTR(parent
->children
, child
) {
985 replace_bb_in_list(&child
->parents
, bb
, parent
, 0);
986 } END_FOR_EACH_PTR(child
);
988 delete_last_instruction(&parent
->insns
);
989 FOR_EACH_PTR(bb
->insns
, insn
) {
991 assert(insn
->bb
== bb
);
994 add_instruction(&parent
->insns
, insn
);
995 } END_FOR_EACH_PTR(insn
);
1000 } END_FOR_EACH_PTR(bb
);