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.. */
462 convert_load_instruction(insn
, value_pseudo(0));
467 * If we find just one dominating instruction, we
468 * can turn it into a direct thing. Otherwise we'll
469 * have to turn the load into a phi-node of the
472 rewrite_load_instruction(insn
, dominators
);
476 static void kill_store(struct instruction
*insn
)
480 insn
->opcode
= OP_SNOP
;
481 kill_use(&insn
->target
);
485 /* Kill a pseudo that is dead on exit from the bb */
486 static void kill_dead_stores(pseudo_t pseudo
, unsigned long generation
, struct basic_block
*bb
, int local
)
488 struct instruction
*insn
;
489 struct basic_block
*parent
;
491 if (bb
->generation
== generation
)
493 bb
->generation
= generation
;
494 FOR_EACH_PTR_REVERSE(bb
->insns
, insn
) {
495 int opcode
= insn
->opcode
;
497 if (opcode
!= OP_LOAD
&& opcode
!= OP_STORE
) {
500 if (opcode
== OP_CALL
)
504 if (insn
->src
== pseudo
) {
505 if (opcode
== OP_LOAD
)
512 if (insn
->src
->type
!= PSEUDO_SYM
)
514 } END_FOR_EACH_PTR_REVERSE(insn
);
516 FOR_EACH_PTR(bb
->parents
, parent
) {
517 struct basic_block
*child
;
518 FOR_EACH_PTR(parent
->children
, child
) {
519 if (child
&& child
!= bb
)
521 } END_FOR_EACH_PTR(child
);
522 kill_dead_stores(pseudo
, generation
, parent
, local
);
523 } END_FOR_EACH_PTR(parent
);
527 * This should see if the "insn" trivially dominates some previous store, and kill the
528 * store if unnecessary.
530 static void kill_dominated_stores(pseudo_t pseudo
, struct instruction
*insn
,
531 unsigned long generation
, struct basic_block
*bb
, int local
, int found
)
533 struct instruction
*one
;
534 struct basic_block
*parent
;
536 /* Unreachable store? Undo it */
541 if (bb
->generation
== generation
)
543 bb
->generation
= generation
;
544 FOR_EACH_PTR_REVERSE(bb
->insns
, one
) {
552 dominance
= dominates(pseudo
, insn
, one
, local
);
557 if (one
->opcode
== OP_LOAD
)
560 } END_FOR_EACH_PTR_REVERSE(one
);
563 warning(bb
->pos
, "Unable to find instruction");
567 FOR_EACH_PTR(bb
->parents
, parent
) {
568 struct basic_block
*child
;
569 FOR_EACH_PTR(parent
->children
, child
) {
570 if (child
&& child
!= bb
)
572 } END_FOR_EACH_PTR(child
);
573 kill_dominated_stores(pseudo
, insn
, generation
, parent
, local
, found
);
574 } END_FOR_EACH_PTR(parent
);
577 static void simplify_one_symbol(struct entrypoint
*ep
, struct symbol
*sym
)
579 pseudo_t pseudo
, src
, *pp
;
580 struct instruction
*def
;
582 int all
, stores
, complex;
584 /* Never used as a symbol? */
585 pseudo
= sym
->pseudo
;
589 /* We don't do coverage analysis of volatiles.. */
590 if (sym
->ctype
.modifiers
& MOD_VOLATILE
)
593 /* ..and symbols with external visibility need more care */
594 mod
= sym
->ctype
.modifiers
& (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
);
596 goto external_visibility
;
601 FOR_EACH_PTR(pseudo
->users
, pp
) {
602 /* We know that the symbol-pseudo use is the "src" in the instruction */
603 struct instruction
*insn
= container(pp
, struct instruction
, src
);
605 switch (insn
->opcode
) {
615 mod
|= MOD_ADDRESSABLE
;
616 goto external_visibility
;
623 warning(sym
->pos
, "symbol '%s' pseudo used in unexpected way", show_ident(sym
->ident
));
625 complex |= insn
->offset
;
626 } END_FOR_EACH_PTR(pp
);
634 * Goodie, we have a single store (if even that) in the whole
635 * thing. Replace all loads with moves from the pseudo,
636 * replace the store with a def.
642 FOR_EACH_PTR(pseudo
->users
, pp
) {
643 struct instruction
*insn
= container(pp
, struct instruction
, src
);
644 if (insn
->opcode
== OP_LOAD
)
645 convert_load_instruction(insn
, src
);
646 } END_FOR_EACH_PTR(pp
);
648 /* Turn the store into a no-op */
656 FOR_EACH_PTR_REVERSE(pseudo
->users
, pp
) {
657 struct instruction
*insn
= container(pp
, struct instruction
, src
);
658 if (insn
->opcode
== OP_LOAD
)
659 all
&= find_dominating_stores(pseudo
, insn
, ++bb_generation
, !mod
);
660 } END_FOR_EACH_PTR_REVERSE(pp
);
662 /* If we converted all the loads, remove the stores. They are dead */
664 FOR_EACH_PTR(pseudo
->users
, pp
) {
665 struct instruction
*insn
= container(pp
, struct instruction
, src
);
666 if (insn
->opcode
== OP_STORE
)
668 } END_FOR_EACH_PTR(pp
);
671 * If we couldn't take the shortcut, see if we can at least kill some
674 FOR_EACH_PTR(pseudo
->users
, pp
) {
675 struct instruction
*insn
= container(pp
, struct instruction
, src
);
676 if (insn
->opcode
== OP_STORE
)
677 kill_dominated_stores(pseudo
, insn
, ++bb_generation
, insn
->bb
, !mod
, 0);
678 } END_FOR_EACH_PTR(pp
);
680 if (!(mod
& (MOD_NONLOCAL
| MOD_STATIC
))) {
681 struct basic_block
*bb
;
682 FOR_EACH_PTR(ep
->bbs
, bb
) {
684 kill_dead_stores(pseudo
, ++bb_generation
, bb
, !mod
);
685 } END_FOR_EACH_PTR(bb
);
692 void simplify_symbol_usage(struct entrypoint
*ep
)
696 FOR_EACH_PTR(ep
->accesses
, sym
) {
697 simplify_one_symbol(ep
, sym
);
698 } END_FOR_EACH_PTR(sym
);
701 static void mark_bb_reachable(struct basic_block
*bb
, unsigned long generation
)
703 struct basic_block
*child
;
705 if (bb
->generation
== generation
)
707 bb
->generation
= generation
;
708 FOR_EACH_PTR(bb
->children
, child
) {
709 mark_bb_reachable(child
, generation
);
710 } END_FOR_EACH_PTR(child
);
713 static void kill_defs(struct instruction
*insn
)
715 pseudo_t target
= insn
->target
;
717 if (!has_use_list(target
))
719 if (target
->def
!= insn
)
722 convert_instruction_target(insn
, VOID
);
725 void kill_bb(struct basic_block
*bb
)
727 struct instruction
*insn
;
728 struct basic_block
*child
, *parent
;
730 FOR_EACH_PTR(bb
->insns
, insn
) {
731 kill_instruction(insn
);
734 * We kill unreachable instructions even if they
735 * otherwise aren't "killable". Eg volatile loads
739 } END_FOR_EACH_PTR(insn
);
742 FOR_EACH_PTR(bb
->children
, child
) {
743 remove_bb_from_list(&child
->parents
, bb
, 0);
744 } END_FOR_EACH_PTR(child
);
747 FOR_EACH_PTR(bb
->parents
, parent
) {
748 remove_bb_from_list(&parent
->children
, bb
, 0);
749 } END_FOR_EACH_PTR(parent
);
753 void kill_unreachable_bbs(struct entrypoint
*ep
)
755 struct basic_block
*bb
;
756 unsigned long generation
= ++bb_generation
;
758 mark_bb_reachable(ep
->entry
->bb
, generation
);
759 FOR_EACH_PTR(ep
->bbs
, bb
) {
760 if (bb
->generation
== generation
)
762 /* Mark it as being dead */
765 DELETE_CURRENT_PTR(bb
);
766 } END_FOR_EACH_PTR(bb
);
767 PACK_PTR_LIST(&ep
->bbs
);
770 static int rewrite_parent_branch(struct basic_block
*bb
, struct basic_block
*old
, struct basic_block
*new)
773 struct instruction
*insn
= last_instruction(bb
->insns
);
778 /* Infinite loops: let's not "optimize" them.. */
782 switch (insn
->opcode
) {
784 changed
|= rewrite_branch(bb
, &insn
->bb_true
, old
, new);
785 changed
|= rewrite_branch(bb
, &insn
->bb_false
, old
, new);
789 struct multijmp
*jmp
;
790 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
791 changed
|= rewrite_branch(bb
, &jmp
->target
, old
, new);
792 } END_FOR_EACH_PTR(jmp
);
801 static struct basic_block
* rewrite_branch_bb(struct basic_block
*bb
, struct instruction
*br
)
803 struct basic_block
*parent
;
804 struct basic_block
*target
= br
->bb_true
;
805 struct basic_block
*false = br
->bb_false
;
807 if (target
&& false) {
808 pseudo_t cond
= br
->cond
;
809 if (cond
->type
!= PSEUDO_VAL
)
811 target
= cond
->value
? target
: false;
815 * We can't do FOR_EACH_PTR() here, because the parent list
816 * may change when we rewrite the parent.
818 while ((parent
= first_basic_block(bb
->parents
)) != NULL
) {
819 if (!rewrite_parent_branch(parent
, bb
, target
))
825 static void vrfy_bb_in_list(struct basic_block
*bb
, struct basic_block_list
*list
)
828 struct basic_block
*tmp
;
829 int no_bb_in_list
= 0;
831 FOR_EACH_PTR(list
, tmp
) {
834 } END_FOR_EACH_PTR(tmp
);
835 assert(no_bb_in_list
);
839 static void vrfy_parents(struct basic_block
*bb
)
841 struct basic_block
*tmp
;
842 FOR_EACH_PTR(bb
->parents
, tmp
) {
843 vrfy_bb_in_list(bb
, tmp
->children
);
844 } END_FOR_EACH_PTR(tmp
);
847 static void vrfy_children(struct basic_block
*bb
)
849 struct basic_block
*tmp
;
850 struct instruction
*br
= last_instruction(bb
->insns
);
853 assert(!bb
->children
);
856 switch (br
->opcode
) {
857 struct multijmp
*jmp
;
859 vrfy_bb_in_list(br
->bb_true
, bb
->children
);
860 vrfy_bb_in_list(br
->bb_false
, bb
->children
);
863 case OP_COMPUTEDGOTO
:
864 FOR_EACH_PTR(br
->multijmp_list
, jmp
) {
865 vrfy_bb_in_list(jmp
->target
, bb
->children
);
866 } END_FOR_EACH_PTR(jmp
);
872 FOR_EACH_PTR(bb
->children
, tmp
) {
873 vrfy_bb_in_list(bb
, tmp
->parents
);
874 } END_FOR_EACH_PTR(tmp
);
877 static void vrfy_bb_flow(struct basic_block
*bb
)
883 void vrfy_flow(struct entrypoint
*ep
)
885 struct basic_block
*bb
;
886 struct basic_block
*entry
= ep
->entry
->bb
;
888 FOR_EACH_PTR(ep
->bbs
, bb
) {
892 } END_FOR_EACH_PTR(bb
);
896 void pack_basic_blocks(struct entrypoint
*ep
)
898 struct basic_block
*bb
;
900 /* See if we can merge a bb into another one.. */
901 FOR_EACH_PTR(ep
->bbs
, bb
) {
902 struct instruction
*first
, *insn
;
903 struct basic_block
*parent
, *child
, *last
;
905 if (!bb_reachable(bb
))
911 FOR_EACH_PTR(bb
->insns
, first
) {
914 switch (first
->opcode
) {
915 case OP_NOP
: case OP_LNOP
: case OP_SNOP
:
918 struct basic_block
*replace
;
919 replace
= rewrite_branch_bb(bb
, first
);
929 } END_FOR_EACH_PTR(first
);
933 * See if we only have one parent..
936 FOR_EACH_PTR(bb
->parents
, parent
) {
943 } END_FOR_EACH_PTR(parent
);
946 if (!parent
|| parent
== bb
)
950 * Goodie. See if the parent can merge..
952 FOR_EACH_PTR(parent
->children
, child
) {
955 } END_FOR_EACH_PTR(child
);
960 repeat_phase
|= REPEAT_CSE
;
962 parent
->children
= bb
->children
;
966 FOR_EACH_PTR(parent
->children
, child
) {
967 replace_bb_in_list(&child
->parents
, bb
, parent
, 0);
968 } END_FOR_EACH_PTR(child
);
970 delete_last_instruction(&parent
->insns
);
971 FOR_EACH_PTR(bb
->insns
, insn
) {
973 assert(insn
->bb
== bb
);
976 add_instruction(&parent
->insns
, insn
);
977 } END_FOR_EACH_PTR(insn
);
982 } END_FOR_EACH_PTR(bb
);