1 /* Conditional constant propagation pass for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3 Original framework by Daniel Berlin <dan@cgsoftware.com>
4 Fleshed out and major cleanups by Jeff Law <law@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* Conditional constant propagation.
27 Constant propagation with conditional branches,
28 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
30 Building an Optimizing Compiler,
31 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
33 Advanced Compiler Design and Implementation,
34 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6
36 The overall structure is as follows:
38 1. Run a simple SSA based DCE pass to remove any dead code.
39 2. Run CCP to compute what registers are known constants
40 and what edges are not executable. Remove unexecutable
41 edges from the CFG and simplify PHI nodes.
42 3. Replace registers with constants where possible.
43 4. Remove unreachable blocks computed in step #2.
44 5. Another simple SSA DCE pass to remove dead code exposed
47 When we exit, we are still in SSA form.
50 Potential further enhancements:
52 1. Handle SUBREGs, STRICT_LOW_PART, etc in destinations more
55 2. Handle insns with multiple outputs more gracefully.
57 3. Handle CONST_DOUBLE and symbolic constants.
59 4. Fold expressions after performing constant substitutions. */
66 #include "hard-reg-set.h"
67 #include "basic-block.h"
69 #include "insn-config.h"
77 /* Possible lattice values. */
86 /* Main structure for CCP.
88 Contains the lattice value and, if it's a constant, the constant
92 latticevalue lattice_val
;
96 /* Array of values indexed by register number. */
99 /* A bitmap to keep track of executable blocks in the CFG. */
100 static sbitmap executable_blocks
;
102 /* A bitmap for all executable edges in the CFG. */
103 static sbitmap executable_edges
;
105 /* Array of edges on the work list. */
106 static edge
*edge_info
;
108 /* We need an edge list to be able to get indexes easily. */
109 static struct edge_list
*edges
;
111 /* For building/following use-def and def-use chains. */
112 static struct df
*df_analyzer
;
114 /* Current edge we are operating on, from the worklist */
115 static edge flow_edges
;
117 /* Bitmap of SSA edges which will need reexamination as their definition
119 static sbitmap ssa_edges
;
121 /* Simple macros to simplify code */
122 #define SSA_NAME(x) REGNO (SET_DEST (x))
123 #define PHI_PARMS(x) XVEC (SET_SRC (x), 0)
124 #define EIE(x,y) EDGE_INDEX (edges, x, y)
126 static void visit_phi_node
PARAMS ((rtx
, basic_block
));
127 static void visit_expression
PARAMS ((rtx
, basic_block
));
128 static void defs_to_undefined
PARAMS ((rtx
));
129 static void defs_to_varying
PARAMS ((rtx
));
130 static void examine_flow_edges
PARAMS ((void));
131 static int mark_references
PARAMS ((rtx
*, void *));
132 static void follow_def_use_chains
PARAMS ((void));
133 static void optimize_unexecutable_edges
PARAMS ((struct edge_list
*, sbitmap
));
134 static void ssa_ccp_substitute_constants
PARAMS ((void));
135 static void ssa_ccp_df_delete_unreachable_insns
PARAMS ((void));
136 static void ssa_fast_dce
PARAMS ((struct df
*));
138 /* Loop through the PHI_NODE's parameters for BLOCK and compare their
139 lattice values to determine PHI_NODE's lattice value. */
141 visit_phi_node (phi_node
, block
)
146 rtx phi_node_expr
= NULL
;
147 unsigned int phi_node_name
= SSA_NAME (PATTERN (phi_node
));
148 latticevalue phi_node_lattice_val
= UNDEFINED
;
149 rtx pat
= PATTERN (phi_node
);
150 rtvec phi_vec
= XVEC (SET_SRC (pat
), 0);
151 unsigned int num_elem
= GET_NUM_ELEM (phi_vec
);
153 for (i
= 0; i
< num_elem
; i
+= 2)
155 if (TEST_BIT (executable_edges
,
156 EIE (BASIC_BLOCK (INTVAL (RTVEC_ELT (phi_vec
, i
+ 1))),
159 unsigned int current_parm
160 = REGNO (RTVEC_ELT (phi_vec
, i
));
162 latticevalue current_parm_lattice_val
163 = values
[current_parm
].lattice_val
;
165 /* If any node is VARYING, then new value of PHI_NODE
167 if (current_parm_lattice_val
== VARYING
)
169 phi_node_lattice_val
= VARYING
;
170 phi_node_expr
= NULL
;
174 /* If we have more than one distinct constant, then the new
175 value of PHI_NODE is VARYING. */
176 if (current_parm_lattice_val
== CONSTANT
177 && phi_node_lattice_val
== CONSTANT
178 && values
[current_parm
].const_value
!= phi_node_expr
)
180 phi_node_lattice_val
= VARYING
;
181 phi_node_expr
= NULL
;
185 /* If the current value of PHI_NODE is UNDEFINED and one
186 node in PHI_NODE is CONSTANT, then the new value of the
187 PHI is that CONSTANT. Note this can turn into VARYING
188 if we find another distinct constant later. */
189 if (phi_node_lattice_val
== UNDEFINED
190 && phi_node_expr
== NULL
191 && current_parm_lattice_val
== CONSTANT
)
193 phi_node_expr
= values
[current_parm
].const_value
;
194 phi_node_lattice_val
= CONSTANT
;
200 /* If the value of PHI_NODE changed, then we will need to
201 re-execute uses of the output of PHI_NODE. */
202 if (phi_node_lattice_val
!= values
[phi_node_name
].lattice_val
)
204 values
[phi_node_name
].lattice_val
= phi_node_lattice_val
;
205 values
[phi_node_name
].const_value
= phi_node_expr
;
206 SET_BIT (ssa_edges
, phi_node_name
);
210 /* Sets all defs in an insn to UNDEFINED. */
212 defs_to_undefined (insn
)
215 struct df_link
*currdef
;
216 for (currdef
= DF_INSN_DEFS (df_analyzer
, insn
); currdef
;
217 currdef
= currdef
->next
)
219 if (values
[DF_REF_REGNO (currdef
->ref
)].lattice_val
!= UNDEFINED
)
220 SET_BIT (ssa_edges
, DF_REF_REGNO (currdef
->ref
));
221 values
[DF_REF_REGNO (currdef
->ref
)].lattice_val
= UNDEFINED
;
225 /* Sets all defs in an insn to VARYING. */
227 defs_to_varying (insn
)
230 struct df_link
*currdef
;
231 for (currdef
= DF_INSN_DEFS (df_analyzer
, insn
); currdef
;
232 currdef
= currdef
->next
)
234 if (values
[DF_REF_REGNO (currdef
->ref
)].lattice_val
!= VARYING
)
235 SET_BIT (ssa_edges
, DF_REF_REGNO (currdef
->ref
));
236 values
[DF_REF_REGNO (currdef
->ref
)].lattice_val
= VARYING
;
240 /* Go through the expression, call the appropriate evaluation routines
243 visit_expression (insn
, block
)
250 /* Ugh. CALL_INSNs may end a basic block and have multiple edges
251 leading out from them.
253 Mark all the outgoing edges as executable, then fall into the
254 normal processing below. */
255 if (GET_CODE (insn
) == CALL_INSN
&& block
->end
== insn
)
259 for (curredge
= block
->succ
; curredge
;
260 curredge
= curredge
->succ_next
)
262 int index
= EIE (curredge
->src
, curredge
->dest
);
264 if (TEST_BIT (executable_edges
, index
))
267 SET_BIT (executable_edges
, index
);
268 edge_info
[index
] = flow_edges
;
269 flow_edges
= curredge
;
273 set
= single_set (insn
);
276 defs_to_varying (insn
);
281 dest
= SET_DEST (set
);
283 /* We may want to refine this some day. */
284 if (GET_CODE (dest
) != REG
&& dest
!= pc_rtx
)
286 defs_to_varying (insn
);
290 /* Hard registers are not put in SSA form and thus we must consider
291 them varying. All the more reason to avoid hard registers in
292 RTL until as late as possible in the compilation. */
293 if (GET_CODE (dest
) == REG
&& REGNO (dest
) < FIRST_PSEUDO_REGISTER
)
295 defs_to_varying (insn
);
299 /* If this is assigning DEST to a constant, record that fact. */
300 if (GET_CODE (src
) == CONST_INT
&& GET_CODE (insn
) == INSN
)
302 unsigned int resultreg
= REGNO (dest
);
304 values
[resultreg
].lattice_val
= CONSTANT
;
305 values
[resultreg
].const_value
= SET_SRC (PATTERN (insn
));
306 SET_BIT (ssa_edges
, resultreg
);
309 /* If this is a copy operation, then we can copy the lattice values. */
310 else if (GET_CODE (src
) == REG
&& GET_CODE (dest
) == REG
)
312 unsigned int old_value
= REGNO (src
);
313 latticevalue old_lattice_value
= values
[old_value
].lattice_val
;
314 unsigned int new_value
= REGNO (dest
);
316 /* Unless the lattice value is going to change, don't bother
317 adding the "new value" into the worklist. */
318 if (values
[new_value
].lattice_val
!= old_lattice_value
319 || values
[new_value
].const_value
!= values
[old_value
].const_value
)
320 SET_BIT (ssa_edges
, new_value
);
322 /* Copy the old lattice node info into the new value lattice node. */
323 values
[new_value
].lattice_val
= old_lattice_value
;
324 values
[new_value
].const_value
= values
[old_value
].const_value
;
328 else if (GET_CODE (insn
) == JUMP_INSN
)
330 rtx x
= pc_set (insn
);
331 if (GET_CODE (src
) != IF_THEN_ELSE
)
335 /* This is a computed jump, table jump, or an unconditional
336 jump. For all these cases we want to mark all successor
337 blocks as executable if they have not already been
340 One day we may try do better with swtich tables and
341 other computed jumps. */
342 for (curredge
= block
->succ
; curredge
;
343 curredge
= curredge
->succ_next
)
345 int index
= EIE (curredge
->src
, curredge
->dest
);
347 if (TEST_BIT (executable_edges
, index
))
350 SET_BIT (executable_edges
, index
);
351 edge_info
[index
] = flow_edges
;
352 flow_edges
= curredge
;
358 enum rtx_code comparison_code
;
362 comparison_code
= GET_CODE (XEXP (src
, 0));
363 comparison_src0
= XEXP (XEXP (src
, 0), 0);
364 comparison_src1
= XEXP (XEXP (src
, 0), 1);
366 /* If either operand is undefined, then there is nothing to
367 do right now. If/when operands are later defined we will
368 revaluate the condition and take the appropriate action. */
369 if ((GET_CODE (comparison_src0
) == REG
370 && values
[REGNO (comparison_src0
)].lattice_val
== UNDEFINED
)
371 || (GET_CODE (comparison_src1
) == REG
372 && values
[REGNO (comparison_src1
)].lattice_val
== UNDEFINED
))
375 /* If either operand is varying, then we must consider all
376 paths as executable. */
377 if ((GET_CODE (comparison_src0
) == REG
378 && values
[REGNO (comparison_src0
)].lattice_val
== VARYING
)
379 || (GET_CODE (comparison_src1
) == REG
380 && values
[REGNO (comparison_src1
)].lattice_val
== VARYING
))
382 for (curredge
= block
->succ
; curredge
;
383 curredge
= curredge
->succ_next
)
385 int index
= EIE (curredge
->src
, curredge
->dest
);
387 if (TEST_BIT (executable_edges
, index
))
390 SET_BIT (executable_edges
, index
);
391 edge_info
[index
] = flow_edges
;
392 flow_edges
= curredge
;
397 /* Try to simplify the comparison. */
398 if (GET_CODE (comparison_src0
) == REG
399 && values
[REGNO (comparison_src0
)].lattice_val
== CONSTANT
)
400 comparison_src0
= values
[REGNO (comparison_src0
)].const_value
;
402 if (GET_CODE (comparison_src1
) == REG
403 && values
[REGNO (comparison_src1
)].lattice_val
== CONSTANT
)
404 comparison_src1
= values
[REGNO (comparison_src1
)].const_value
;
406 x
= simplify_ternary_operation (IF_THEN_ELSE
,
408 GET_MODE (XEXP (src
, 0)),
409 gen_rtx (comparison_code
,
410 GET_MODE (XEXP (src
, 0)),
416 /* Walk through all the outgoing edges from this block and see
417 which (if any) we should mark as executable. */
418 for (curredge
= block
->succ
; curredge
;
419 curredge
= curredge
->succ_next
)
421 int index
= EIE (curredge
->src
, curredge
->dest
);
423 if (TEST_BIT (executable_edges
, index
))
426 /* If we were unable to simplify the expression at this
427 point, it's highly unlikely we'll be able to simplify
428 it later. So consider all edges as executable if the
429 expression did not simplify.
431 If the expression simplified to (pc), then we know we
432 will take the fall-thru edge, so mark it. Similarly,
433 if the expression simplified to (label_ref ...), then
434 we know the branch will be taken and we mark that
438 && (curredge
->flags
& EDGE_FALLTHRU
))
439 || (GET_CODE (x
) == LABEL_REF
440 && ! (curredge
->flags
& EDGE_FALLTHRU
)))
442 SET_BIT (executable_edges
, index
);
443 edge_info
[index
] = flow_edges
;
444 flow_edges
= curredge
;
449 else if (!PHI_NODE_P (insn
))
451 rtx simplified
= NULL
;
453 /* We've got some kind of INSN. If it's simple, try to evaluate
454 it and record the results.
456 We already know this insn is a single_set and that it sets
457 a pseudo register. So we just need to extract the source
458 arguments, simplify them to constants if possible, then
459 simplify the expression as a whole if possible. */
460 switch (GET_RTX_CLASS (GET_CODE (src
)))
464 rtx src0
= XEXP (src
, 0);
465 rtx src1
= XEXP (src
, 1);
466 enum machine_mode mode
;
468 /* If either is undefined, then the result is undefined. */
469 if ((GET_CODE (src0
) == REG
470 && values
[REGNO (src0
)].lattice_val
== UNDEFINED
)
471 || (GET_CODE (src1
) == REG
472 && values
[REGNO (src1
)].lattice_val
== UNDEFINED
))
474 defs_to_undefined (insn
);
478 /* Determine the mode for the operation before we simplify
479 our arguments to constants. */
480 mode
= GET_MODE (src0
);
481 if (mode
== VOIDmode
)
482 mode
= GET_MODE (src1
);
484 /* Simplify source operands to whatever known values they
486 if (GET_CODE (src0
) == REG
487 && values
[REGNO (src0
)].lattice_val
== CONSTANT
)
488 src0
= values
[REGNO (src0
)].const_value
;
490 if (GET_CODE (src1
) == REG
491 && values
[REGNO (src1
)].lattice_val
== CONSTANT
)
492 src1
= values
[REGNO (src1
)].const_value
;
494 /* See if the simplifier can determine if this operation
495 computes a constant value. */
496 simplified
= simplify_relational_operation (GET_CODE (src
),
504 rtx src0
= XEXP (src
, 0);
505 enum machine_mode mode0
= GET_MODE (src0
);
507 /* If the operand is undefined, then the result is undefined. */
508 if (GET_CODE (src0
) == REG
509 && values
[REGNO (src0
)].lattice_val
== UNDEFINED
)
511 defs_to_undefined (insn
);
515 /* Simplify source operands to whatever known values they
517 if (GET_CODE (src0
) == REG
518 && values
[REGNO (src0
)].lattice_val
== CONSTANT
)
519 src0
= values
[REGNO (src0
)].const_value
;
521 /* See if the simplifier can determine if this operation
522 computes a constant value. */
523 simplified
= simplify_unary_operation (GET_CODE (src
),
533 rtx src0
= XEXP (src
, 0);
534 rtx src1
= XEXP (src
, 1);
536 /* If either is undefined, then the result is undefined. */
537 if ((GET_CODE (src0
) == REG
538 && values
[REGNO (src0
)].lattice_val
== UNDEFINED
)
539 || (GET_CODE (src1
) == REG
540 && values
[REGNO (src1
)].lattice_val
== UNDEFINED
))
542 defs_to_undefined (insn
);
546 /* Simplify source operands to whatever known values they
548 if (GET_CODE (src0
) == REG
549 && values
[REGNO (src0
)].lattice_val
== CONSTANT
)
550 src0
= values
[REGNO (src0
)].const_value
;
552 if (GET_CODE (src1
) == REG
553 && values
[REGNO (src1
)].lattice_val
== CONSTANT
)
554 src1
= values
[REGNO (src1
)].const_value
;
556 /* See if the simplifier can determine if this operation
557 computes a constant value. */
558 simplified
= simplify_binary_operation (GET_CODE (src
),
567 rtx src0
= XEXP (src
, 0);
568 rtx src1
= XEXP (src
, 1);
569 rtx src2
= XEXP (src
, 2);
571 /* If either is undefined, then the result is undefined. */
572 if ((GET_CODE (src0
) == REG
573 && values
[REGNO (src0
)].lattice_val
== UNDEFINED
)
574 || (GET_CODE (src1
) == REG
575 && values
[REGNO (src1
)].lattice_val
== UNDEFINED
)
576 || (GET_CODE (src2
) == REG
577 && values
[REGNO (src2
)].lattice_val
== UNDEFINED
))
579 defs_to_undefined (insn
);
583 /* Simplify source operands to whatever known values they
585 if (GET_CODE (src0
) == REG
586 && values
[REGNO (src0
)].lattice_val
== CONSTANT
)
587 src0
= values
[REGNO (src0
)].const_value
;
589 if (GET_CODE (src1
) == REG
590 && values
[REGNO (src1
)].lattice_val
== CONSTANT
)
591 src1
= values
[REGNO (src1
)].const_value
;
593 if (GET_CODE (src2
) == REG
594 && values
[REGNO (src2
)].lattice_val
== CONSTANT
)
595 src2
= values
[REGNO (src2
)].const_value
;
597 /* See if the simplifier can determine if this operation
598 computes a constant value. */
599 simplified
= simplify_ternary_operation (GET_CODE (src
),
607 defs_to_varying (insn
);
610 if (simplified
&& GET_CODE (simplified
) == CONST_INT
)
612 if (values
[REGNO (dest
)].lattice_val
!= CONSTANT
613 || values
[REGNO (dest
)].const_value
!= simplified
)
614 SET_BIT (ssa_edges
, REGNO (dest
));
616 values
[REGNO (dest
)].lattice_val
= CONSTANT
;
617 values
[REGNO (dest
)].const_value
= simplified
;
620 defs_to_varying (insn
);
624 /* Iterate over the FLOW_EDGES work list. Simulate the target block
627 examine_flow_edges ()
629 while (flow_edges
!= NULL
)
631 basic_block succ_block
;
634 /* Pull the next block to simulate off the worklist. */
635 succ_block
= flow_edges
->dest
;
636 flow_edges
= edge_info
[EIE (flow_edges
->src
, flow_edges
->dest
)];
638 /* There is nothing to do for the exit block. */
639 if (succ_block
== EXIT_BLOCK_PTR
)
642 /* Always simulate PHI nodes, even if we have simulated this block
643 before. Note that all PHI nodes are consecutive within a block. */
644 for (curr_phi_node
= first_insn_after_basic_block_note (succ_block
);
645 PHI_NODE_P (curr_phi_node
);
646 curr_phi_node
= NEXT_INSN (curr_phi_node
))
647 visit_phi_node (curr_phi_node
, succ_block
);
649 /* If this is the first time we've simulated this block, then we
650 must simulate each of its insns. */
651 if (!TEST_BIT (executable_blocks
, succ_block
->index
))
654 edge succ_edge
= succ_block
->succ
;
656 /* Note that we have simulated this block. */
657 SET_BIT (executable_blocks
, succ_block
->index
);
659 /* Simulate each insn within the block. */
660 currinsn
= succ_block
->head
;
661 while (currinsn
!= succ_block
->end
)
663 if (INSN_P (currinsn
))
664 visit_expression (currinsn
, succ_block
);
666 currinsn
= NEXT_INSN (currinsn
);
669 /* Don't forget the last insn in the block. */
670 if (INSN_P (currinsn
))
671 visit_expression (currinsn
, succ_block
);
673 /* If we haven't looked at the next block, and it has a
674 single successor, add it onto the worklist. This is because
675 if we only have one successor, we know it gets executed,
676 so we don't have to wait for cprop to tell us. */
677 if (succ_edge
!= NULL
678 && succ_edge
->succ_next
== NULL
679 && !TEST_BIT (executable_edges
,
680 EIE (succ_edge
->src
, succ_edge
->dest
)))
682 SET_BIT (executable_edges
,
683 EIE (succ_edge
->src
, succ_edge
->dest
));
684 edge_info
[EIE (succ_edge
->src
, succ_edge
->dest
)] = flow_edges
;
685 flow_edges
= succ_edge
;
691 /* Follow the def-use chains for each definition on the worklist and
692 simulate the uses of the definition. */
695 follow_def_use_chains ()
697 /* Iterate over all the entries on the SSA_EDGES worklist. */
698 while (sbitmap_first_set_bit (ssa_edges
) >= 0)
701 struct df_link
*curruse
;
703 /* Pick an entry off the worklist (it does not matter which
705 member
= sbitmap_first_set_bit (ssa_edges
);
706 RESET_BIT (ssa_edges
, member
);
708 /* Iterate through all the uses of this entry. */
709 for (curruse
= df_analyzer
->regs
[member
].uses
; curruse
;
710 curruse
= curruse
->next
)
714 useinsn
= DF_REF_INSN (curruse
->ref
);
715 if (PHI_NODE_P (useinsn
))
717 if (TEST_BIT (executable_blocks
, BLOCK_NUM (useinsn
)))
718 visit_phi_node (useinsn
, BLOCK_FOR_INSN (useinsn
));
722 if (TEST_BIT (executable_blocks
, BLOCK_NUM (useinsn
)))
723 visit_expression (useinsn
, BLOCK_FOR_INSN (useinsn
));
729 /* Examine each edge to see if we were able to prove any were
732 If an edge is not executable, then we can remove its alternative
733 in PHI nodes as the destination of the edge, we can simplify the
734 conditional branch at the source of the edge, and we can remove
735 the edge from the CFG. Note we do not delete unreachable blocks
736 yet as the DF analyzer can not deal with that yet. */
738 optimize_unexecutable_edges (edges
, executable_edges
)
739 struct edge_list
*edges
;
740 sbitmap executable_edges
;
745 for (i
= 0; i
< NUM_EDGES (edges
); i
++)
747 if (!TEST_BIT (executable_edges
, i
))
749 edge edge
= INDEX_EDGE (edges
, i
);
751 if (edge
->flags
& EDGE_ABNORMAL
)
754 /* We found an edge that is not executable. First simplify
755 the PHI nodes in the target block. */
756 if (edge
->dest
!= EXIT_BLOCK_PTR
)
758 rtx insn
= first_insn_after_basic_block_note (edge
->dest
);
760 while (PHI_NODE_P (insn
))
762 remove_phi_alternative (PATTERN (insn
), edge
->src
);
764 fprintf (rtl_dump_file
,
765 "Removing alternative for bb %d of phi %d\n",
766 edge
->src
->index
, SSA_NAME (PATTERN (insn
)));
767 insn
= NEXT_INSN (insn
);
771 fprintf (rtl_dump_file
,
772 "Removing unexecutable edge from %d to %d\n",
773 edge
->src
->index
, edge
->dest
->index
);
774 /* Since the edge was not executable, remove it from the CFG. */
779 /* We have removed all the unexecutable edges from the CFG. Fix up
780 the conditional jumps at the end of any affected block.
782 We have three cases to deal with:
784 a. Both outgoing edges are not executable. This happens if the
785 source block is not reachable. We will deal with this by
786 deleting all the insns in the block later.
788 b. The fall-thru edge is not executable. In this case we
789 change the conditional jump into an unconditional jump and
790 add a BARRIER after the unconditional jump. Note that since
791 we are working on generic RTL we can change the jump in-place
792 instead of dealing with the headache of reemitting the jump.
794 c. The branch taken edge is not executable. In this case
795 we turn the jump into (set (pc) (pc)) which is a nop-jump
796 and we will remove the unrecognizable insn later.
798 In cases B & C we are removing uses of registers, so make sure
799 to note those changes for the DF analyzer. */
804 edge edge
= bb
->succ
;
806 /* If we have no predecessors, then this block is unreachable and
807 will be cleaned up when we remove unreachable blocks. */
808 if (bb
->pred
== NULL
|| GET_CODE (insn
) != JUMP_INSN
)
811 /* If this block ends in a conditional jump, but only has one
812 successor, then the jump needs adjustment. */
813 if (condjump_p (insn
) && ! simplejump_p (insn
)
814 && bb
->succ
&& bb
->succ
->succ_next
== NULL
)
816 /* If the fallthru edge is the executable edge, then turn
817 this jump into a nop jump, otherwise make it an unconditinoal
818 jump to its target. */
819 if (edge
->flags
& EDGE_FALLTHRU
)
821 PUT_CODE (insn
, NOTE
);
822 NOTE_LINE_NUMBER (insn
) = NOTE_INSN_DELETED
;
826 SET_SRC (PATTERN (insn
)) = gen_rtx_LABEL_REF (Pmode
,
828 emit_barrier_after (insn
);
829 INSN_CODE (insn
) = -1;
832 /* Inform the DF analyzer that this insn changed. */
833 df_insn_modify (df_analyzer
, BLOCK_FOR_INSN (insn
), insn
);
838 /* Perform substitution of known values for pseudo registers.
840 ??? Note we do not do simplifications or constant folding here, it
841 is unlikely that any significant simplifications can be done here
842 anyway. Consider that if the simplification would result in an
843 expression that produces a constant value that the value would
844 have been discovered and recorded already.
846 We perform two transformations. First, we initialize pseudos to their
847 known constant values at their definition point. Second, we try to
848 replace uses with the known constant value. */
851 ssa_ccp_substitute_constants ()
855 for (i
= FIRST_PSEUDO_REGISTER
; i
< VARRAY_SIZE (ssa_definition
); i
++)
857 if (values
[i
].lattice_val
== CONSTANT
)
859 rtx def
= VARRAY_RTX (ssa_definition
, i
);
860 rtx set
= single_set (def
);
861 struct df_link
*curruse
;
866 /* Do not try to simplify PHI nodes down to a constant load.
867 That will be done later as we translate out of SSA. Also,
868 doing that here could violate the rule that all PHI nodes
869 are consecutive at the start of the basic block.
871 Don't do anything to nodes that were already sets to
873 if (! PHI_NODE_P (def
)
874 && ! ((GET_CODE (def
) == INSN
875 && GET_CODE (SET_SRC (set
)) == CONST_INT
)))
878 fprintf (rtl_dump_file
,
879 "Register %d is now set to a constant\n",
880 SSA_NAME (PATTERN (def
)));
881 SET_SRC (set
) = values
[i
].const_value
;
882 INSN_CODE (def
) = -1;
883 df_insn_modify (df_analyzer
, BLOCK_FOR_INSN (def
), def
);
886 /* Iterate through all the uses of this entry and try replacements
887 there too. Note it is not particularly profitable to try
888 and fold/simplify expressions here as most of the common
889 cases were handled above. */
890 for (curruse
= df_analyzer
->regs
[i
].uses
;
892 curruse
= curruse
->next
)
896 useinsn
= DF_REF_INSN (curruse
->ref
);
898 if (!INSN_DELETED_P (useinsn
)
899 && ! (GET_CODE (useinsn
) == NOTE
900 && NOTE_LINE_NUMBER (useinsn
) == NOTE_INSN_DELETED
)
901 && (GET_CODE (useinsn
) == INSN
902 || GET_CODE (useinsn
) == JUMP_INSN
))
905 if (validate_replace_src (regno_reg_rtx
[i
],
906 values
[i
].const_value
,
910 fprintf (rtl_dump_file
,
911 "Register %d in insn %d replaced with constant\n",
912 i
, INSN_UID (useinsn
));
913 INSN_CODE (useinsn
) = -1;
914 df_insn_modify (df_analyzer
,
915 BLOCK_FOR_INSN (useinsn
),
925 /* Now find all unreachable basic blocks. All the insns in those
926 blocks are unreachable, so delete them and mark any necessary
927 updates for the DF analyzer. */
930 ssa_ccp_df_delete_unreachable_insns ()
934 /* Use the CFG to find all the reachable blocks. */
935 find_unreachable_blocks ();
937 /* Now we know what blocks are not reachable. Mark all the insns
938 in those blocks as deleted for the DF analyzer. We'll let the
939 normal flow code actually remove the unreachable blocks. */
940 FOR_EACH_BB_REVERSE (b
)
942 if (!(b
->flags
& BB_REACHABLE
))
948 /* Include any jump table following the basic block. */
950 if (GET_CODE (end
) == JUMP_INSN
951 && (tmp
= JUMP_LABEL (end
)) != NULL_RTX
952 && (tmp
= NEXT_INSN (tmp
)) != NULL_RTX
953 && GET_CODE (tmp
) == JUMP_INSN
954 && (GET_CODE (PATTERN (tmp
)) == ADDR_VEC
955 || GET_CODE (PATTERN (tmp
)) == ADDR_DIFF_VEC
))
960 rtx next
= NEXT_INSN (start
);
962 if (GET_CODE (start
) == INSN
963 || GET_CODE (start
) == CALL_INSN
964 || GET_CODE (start
) == JUMP_INSN
)
965 df_insn_delete (df_analyzer
, BLOCK_FOR_INSN (start
), start
);
976 /* Main entry point for SSA Conditional Constant Propagation.
978 Long term it should accept as input the specific flow graph to
979 operate on so that it can be called for sub-graphs. */
987 /* We need alias analysis (for what?) */
988 init_alias_analysis ();
990 df_analyzer
= df_init ();
991 df_analyse (df_analyzer
, 0,
992 DF_RD_CHAIN
| DF_RU_CHAIN
| DF_REG_INFO
| DF_HARD_REGS
);
994 /* Perform a quick and dirty dead code elimination pass. This is not
995 as aggressive as it could be, but it's good enough to clean up a
996 lot of unwanted junk and it is fast. */
997 ssa_fast_dce (df_analyzer
);
999 /* Build an edge list from the CFG. */
1000 edges
= create_edge_list ();
1002 /* Initialize the values array with everything as undefined. */
1003 values
= (value
*) xmalloc (VARRAY_SIZE (ssa_definition
) * sizeof (value
));
1004 for (i
= 0; i
< VARRAY_SIZE (ssa_definition
); i
++)
1006 if (i
< FIRST_PSEUDO_REGISTER
)
1007 values
[i
].lattice_val
= VARYING
;
1009 values
[i
].lattice_val
= UNDEFINED
;
1010 values
[i
].const_value
= NULL
;
1013 ssa_edges
= sbitmap_alloc (VARRAY_SIZE (ssa_definition
));
1014 sbitmap_zero (ssa_edges
);
1016 executable_blocks
= sbitmap_alloc (last_basic_block
);
1017 sbitmap_zero (executable_blocks
);
1019 executable_edges
= sbitmap_alloc (NUM_EDGES (edges
));
1020 sbitmap_zero (executable_edges
);
1022 edge_info
= (edge
*) xmalloc (NUM_EDGES (edges
) * sizeof (edge
));
1023 flow_edges
= ENTRY_BLOCK_PTR
->succ
;
1025 /* Add the successors of the entry block to the edge worklist. That
1026 is enough of a seed to get SSA-CCP started. */
1027 for (curredge
= ENTRY_BLOCK_PTR
->succ
; curredge
;
1028 curredge
= curredge
->succ_next
)
1030 int index
= EIE (curredge
->src
, curredge
->dest
);
1031 SET_BIT (executable_edges
, index
);
1032 edge_info
[index
] = curredge
->succ_next
;
1035 /* Iterate until until the worklists are empty. */
1038 examine_flow_edges ();
1039 follow_def_use_chains ();
1041 while (flow_edges
!= NULL
);
1043 /* Now perform substitutions based on the known constant values. */
1044 ssa_ccp_substitute_constants ();
1046 /* Remove unexecutable edges from the CFG and make appropriate
1047 adjustments to PHI nodes. */
1048 optimize_unexecutable_edges (edges
, executable_edges
);
1050 /* Now remove all unreachable insns and update the DF information.
1052 ssa_ccp_df_delete_unreachable_insns ();
1055 /* The DF analyzer expects the number of blocks to remain constant,
1056 so we can't remove unreachable blocks.
1058 Code the DF analyzer calls expects there to be no unreachable
1059 blocks in the CFG. So we can't leave unreachable blocks in the
1062 So, there is no way to do an incremental update of the DF data
1064 df_analyse (df_analyzer
, 0,
1065 DF_RD_CHAIN
| DF_RU_CHAIN
| DF_REG_INFO
| DF_HARD_REGS
);
1068 /* Clean up any dead code exposed by SSA-CCP, do this after updating
1069 the dataflow information! */
1070 ssa_fast_dce (df_analyzer
);
1078 sbitmap_free (executable_blocks
);
1079 executable_blocks
= NULL
;
1081 sbitmap_free (ssa_edges
);
1084 free_edge_list (edges
);
1087 sbitmap_free (executable_edges
);
1088 executable_edges
= NULL
;
1090 df_finish (df_analyzer
);
1091 end_alias_analysis ();
1095 mark_references (current_rtx
, data
)
1099 rtx x
= *current_rtx
;
1100 sbitmap worklist
= (sbitmap
) data
;
1105 if (GET_CODE (x
) == SET
)
1107 rtx dest
= SET_DEST (x
);
1109 if (GET_CODE (dest
) == STRICT_LOW_PART
1110 || GET_CODE (dest
) == SUBREG
1111 || GET_CODE (dest
) == SIGN_EXTRACT
1112 || GET_CODE (dest
) == ZERO_EXTRACT
)
1118 while (GET_CODE (reg
) == STRICT_LOW_PART
1119 || GET_CODE (reg
) == SUBREG
1120 || GET_CODE (reg
) == SIGN_EXTRACT
1121 || GET_CODE (reg
) == ZERO_EXTRACT
)
1122 reg
= XEXP (reg
, 0);
1124 if (GET_CODE (reg
) == REG
)
1125 SET_BIT (worklist
, REGNO (reg
));
1128 if (GET_CODE (dest
) == REG
)
1130 for_each_rtx (&SET_SRC (x
), mark_references
, data
);
1136 else if (GET_CODE (x
) == REG
)
1138 SET_BIT (worklist
, REGNO (x
));
1141 else if (GET_CODE (x
) == CLOBBER
)
1151 sbitmap worklist
= sbitmap_alloc (VARRAY_SIZE (ssa_definition
));
1152 sbitmap_ones (worklist
);
1154 /* Iterate on the worklist until there's no definitions left to
1156 while (sbitmap_first_set_bit (worklist
) >= 0)
1158 struct df_link
*curruse
;
1161 /* Remove an item from the worklist. */
1162 reg
= sbitmap_first_set_bit (worklist
);
1163 RESET_BIT (worklist
, reg
);
1165 /* We never consider deleting assignments to hard regs or things
1166 which do not have SSA definitions, or things we have already
1167 deleted, or things with unusual side effects. */
1168 if (reg
< FIRST_PSEUDO_REGISTER
1169 || ! VARRAY_RTX (ssa_definition
, reg
)
1170 || INSN_DELETED_P (VARRAY_RTX (ssa_definition
, reg
))
1171 || (GET_CODE (VARRAY_RTX (ssa_definition
, reg
)) == NOTE
1172 && (NOTE_LINE_NUMBER (VARRAY_RTX (ssa_definition
, reg
))
1173 == NOTE_INSN_DELETED
))
1174 || side_effects_p (PATTERN (VARRAY_RTX (ssa_definition
, reg
))))
1177 /* Iterate over the uses of this register. If we can not find
1178 any uses that have not been deleted, then the definition of
1179 this register is dead. */
1181 for (curruse
= df
->regs
[reg
].uses
; curruse
; curruse
= curruse
->next
)
1184 && DF_REF_INSN (curruse
->ref
)
1185 && ! INSN_DELETED_P (DF_REF_INSN (curruse
->ref
))
1186 && ! (GET_CODE (DF_REF_INSN (curruse
->ref
)) == NOTE
1187 && (NOTE_LINE_NUMBER (DF_REF_INSN (curruse
->ref
))
1188 == NOTE_INSN_DELETED
))
1189 && DF_REF_INSN (curruse
->ref
) != VARRAY_RTX (ssa_definition
, reg
))
1196 /* If we did not find a use of this register, then the definition
1197 of this register is dead. */
1201 rtx def
= VARRAY_RTX (ssa_definition
, reg
);
1203 /* Add all registers referenced by INSN to the work
1205 for_each_rtx (&PATTERN (def
), mark_references
, worklist
);
1207 /* Inform the analyzer that this insn is going to be
1209 df_insn_delete (df
, BLOCK_FOR_INSN (def
), def
);
1211 VARRAY_RTX (ssa_definition
, reg
) = NULL
;
1215 sbitmap_free (worklist
);
1217 /* Update the use-def chains in the df_analyzer as needed. */
1218 df_analyse (df_analyzer
, 0,
1219 DF_RD_CHAIN
| DF_RU_CHAIN
| DF_REG_INFO
| DF_HARD_REGS
);