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
;
744 for (i
= 0; i
< NUM_EDGES (edges
); i
++)
746 if (!TEST_BIT (executable_edges
, i
))
748 edge edge
= INDEX_EDGE (edges
, i
);
750 if (edge
->flags
& EDGE_ABNORMAL
)
753 /* We found an edge that is not executable. First simplify
754 the PHI nodes in the target block. */
755 if (edge
->dest
!= EXIT_BLOCK_PTR
)
757 rtx insn
= first_insn_after_basic_block_note (edge
->dest
);
759 while (PHI_NODE_P (insn
))
761 remove_phi_alternative (PATTERN (insn
), edge
->src
);
763 fprintf (rtl_dump_file
,
764 "Removing alternative for bb %d of phi %d\n",
765 edge
->src
->index
, SSA_NAME (PATTERN (insn
)));
766 insn
= NEXT_INSN (insn
);
770 fprintf (rtl_dump_file
,
771 "Removing unexecutable edge from %d to %d\n",
772 edge
->src
->index
, edge
->dest
->index
);
773 /* Since the edge was not executable, remove it from the CFG. */
778 /* We have removed all the unexecutable edges from the CFG. Fix up
779 the conditional jumps at the end of any affected block.
781 We have three cases to deal with:
783 a. Both outgoing edges are not executable. This happens if the
784 source block is not reachable. We will deal with this by
785 deleting all the insns in the block later.
787 b. The fall-thru edge is not executable. In this case we
788 change the conditional jump into an unconditional jump and
789 add a BARRIER after the unconditional jump. Note that since
790 we are working on generic RTL we can change the jump in-place
791 instead of dealing with the headache of reemitting the jump.
793 c. The branch taken edge is not executable. In this case
794 we turn the jump into (set (pc) (pc)) which is a nop-jump
795 and we will remove the unrecognizable insn later.
797 In cases B & C we are removing uses of registers, so make sure
798 to note those changes for the DF analyzer. */
800 for (i
= 0; i
< n_basic_blocks
; i
++)
802 basic_block bb
= BASIC_BLOCK (i
);
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 (i
= n_basic_blocks
- 1; i
>= 0; --i
)
942 basic_block b
= BASIC_BLOCK (i
);
944 if (!(b
->flags
& BB_REACHABLE
))
950 /* Include any jump table following the basic block. */
952 if (GET_CODE (end
) == JUMP_INSN
953 && (tmp
= JUMP_LABEL (end
)) != NULL_RTX
954 && (tmp
= NEXT_INSN (tmp
)) != NULL_RTX
955 && GET_CODE (tmp
) == JUMP_INSN
956 && (GET_CODE (PATTERN (tmp
)) == ADDR_VEC
957 || GET_CODE (PATTERN (tmp
)) == ADDR_DIFF_VEC
))
962 rtx next
= NEXT_INSN (start
);
964 if (GET_CODE (start
) == INSN
965 || GET_CODE (start
) == CALL_INSN
966 || GET_CODE (start
) == JUMP_INSN
)
967 df_insn_delete (df_analyzer
, BLOCK_FOR_INSN (start
), start
);
978 /* Main entry point for SSA Conditional Constant Propagation.
980 Long term it should accept as input the specific flow graph to
981 operate on so that it can be called for sub-graphs. */
989 /* We need alias analysis (for what?) */
990 init_alias_analysis ();
992 df_analyzer
= df_init ();
993 df_analyse (df_analyzer
, 0,
994 DF_RD_CHAIN
| DF_RU_CHAIN
| DF_REG_INFO
| DF_HARD_REGS
);
996 /* We need mappings from insn to its containing block. */
997 compute_bb_for_insn (get_max_uid ());
999 /* Perform a quick and dirty dead code elimination pass. This is not
1000 as aggressive as it could be, but it's good enough to clean up a
1001 lot of unwanted junk and it is fast. */
1002 ssa_fast_dce (df_analyzer
);
1004 /* Build an edge list from the CFG. */
1005 edges
= create_edge_list ();
1007 /* Initialize the values array with everything as undefined. */
1008 values
= (value
*) xmalloc (VARRAY_SIZE (ssa_definition
) * sizeof (value
));
1009 for (i
= 0; i
< VARRAY_SIZE (ssa_definition
); i
++)
1011 if (i
< FIRST_PSEUDO_REGISTER
)
1012 values
[i
].lattice_val
= VARYING
;
1014 values
[i
].lattice_val
= UNDEFINED
;
1015 values
[i
].const_value
= NULL
;
1018 ssa_edges
= sbitmap_alloc (VARRAY_SIZE (ssa_definition
));
1019 sbitmap_zero (ssa_edges
);
1021 executable_blocks
= sbitmap_alloc (n_basic_blocks
);
1022 sbitmap_zero (executable_blocks
);
1024 executable_edges
= sbitmap_alloc (NUM_EDGES (edges
));
1025 sbitmap_zero (executable_edges
);
1027 edge_info
= (edge
*) xmalloc (NUM_EDGES (edges
) * sizeof (edge
));
1028 flow_edges
= ENTRY_BLOCK_PTR
->succ
;
1030 /* Add the successors of the entry block to the edge worklist. That
1031 is enough of a seed to get SSA-CCP started. */
1032 for (curredge
= ENTRY_BLOCK_PTR
->succ
; curredge
;
1033 curredge
= curredge
->succ_next
)
1035 int index
= EIE (curredge
->src
, curredge
->dest
);
1036 SET_BIT (executable_edges
, index
);
1037 edge_info
[index
] = curredge
->succ_next
;
1040 /* Iterate until until the worklists are empty. */
1043 examine_flow_edges ();
1044 follow_def_use_chains ();
1046 while (flow_edges
!= NULL
);
1048 /* Now perform substitutions based on the known constant values. */
1049 ssa_ccp_substitute_constants ();
1051 /* Remove unexecutable edges from the CFG and make appropriate
1052 adjustments to PHI nodes. */
1053 optimize_unexecutable_edges (edges
, executable_edges
);
1055 /* Now remove all unreachable insns and update the DF information.
1057 ssa_ccp_df_delete_unreachable_insns ();
1060 /* The DF analyzer expects the number of blocks to remain constant,
1061 so we can't remove unreachable blocks.
1063 Code the DF analyzer calls expects there to be no unreachable
1064 blocks in the CFG. So we can't leave unreachable blocks in the
1067 So, there is no way to do an incremental update of the DF data
1069 df_analyse (df_analyzer
, 0,
1070 DF_RD_CHAIN
| DF_RU_CHAIN
| DF_REG_INFO
| DF_HARD_REGS
);
1073 /* Clean up any dead code exposed by SSA-CCP, do this after updating
1074 the dataflow information! */
1075 ssa_fast_dce (df_analyzer
);
1083 sbitmap_free (executable_blocks
);
1084 executable_blocks
= NULL
;
1086 sbitmap_free (ssa_edges
);
1089 free_edge_list (edges
);
1092 sbitmap_free (executable_edges
);
1093 executable_edges
= NULL
;
1095 df_finish (df_analyzer
);
1096 end_alias_analysis ();
1100 mark_references (current_rtx
, data
)
1104 rtx x
= *current_rtx
;
1105 sbitmap worklist
= (sbitmap
) data
;
1110 if (GET_CODE (x
) == SET
)
1112 rtx dest
= SET_DEST (x
);
1114 if (GET_CODE (dest
) == STRICT_LOW_PART
1115 || GET_CODE (dest
) == SUBREG
1116 || GET_CODE (dest
) == SIGN_EXTRACT
1117 || GET_CODE (dest
) == ZERO_EXTRACT
)
1123 while (GET_CODE (reg
) == STRICT_LOW_PART
1124 || GET_CODE (reg
) == SUBREG
1125 || GET_CODE (reg
) == SIGN_EXTRACT
1126 || GET_CODE (reg
) == ZERO_EXTRACT
)
1127 reg
= XEXP (reg
, 0);
1129 if (GET_CODE (reg
) == REG
)
1130 SET_BIT (worklist
, REGNO (reg
));
1133 if (GET_CODE (dest
) == REG
)
1135 for_each_rtx (&SET_SRC (x
), mark_references
, data
);
1141 else if (GET_CODE (x
) == REG
)
1143 SET_BIT (worklist
, REGNO (x
));
1146 else if (GET_CODE (x
) == CLOBBER
)
1156 sbitmap worklist
= sbitmap_alloc (VARRAY_SIZE (ssa_definition
));
1157 sbitmap_ones (worklist
);
1159 /* Iterate on the worklist until there's no definitions left to
1161 while (sbitmap_first_set_bit (worklist
) >= 0)
1163 struct df_link
*curruse
;
1166 /* Remove an item from the worklist. */
1167 reg
= sbitmap_first_set_bit (worklist
);
1168 RESET_BIT (worklist
, reg
);
1170 /* We never consider deleting assignments to hard regs or things
1171 which do not have SSA definitions, or things we have already
1172 deleted, or things with unusual side effects. */
1173 if (reg
< FIRST_PSEUDO_REGISTER
1174 || ! VARRAY_RTX (ssa_definition
, reg
)
1175 || INSN_DELETED_P (VARRAY_RTX (ssa_definition
, reg
))
1176 || (GET_CODE (VARRAY_RTX (ssa_definition
, reg
)) == NOTE
1177 && (NOTE_LINE_NUMBER (VARRAY_RTX (ssa_definition
, reg
))
1178 == NOTE_INSN_DELETED
))
1179 || side_effects_p (PATTERN (VARRAY_RTX (ssa_definition
, reg
))))
1182 /* Iterate over the uses of this register. If we can not find
1183 any uses that have not been deleted, then the definition of
1184 this register is dead. */
1186 for (curruse
= df
->regs
[reg
].uses
; curruse
; curruse
= curruse
->next
)
1189 && DF_REF_INSN (curruse
->ref
)
1190 && ! INSN_DELETED_P (DF_REF_INSN (curruse
->ref
))
1191 && ! (GET_CODE (DF_REF_INSN (curruse
->ref
)) == NOTE
1192 && (NOTE_LINE_NUMBER (DF_REF_INSN (curruse
->ref
))
1193 == NOTE_INSN_DELETED
))
1194 && DF_REF_INSN (curruse
->ref
) != VARRAY_RTX (ssa_definition
, reg
))
1201 /* If we did not find a use of this register, then the definition
1202 of this register is dead. */
1206 rtx def
= VARRAY_RTX (ssa_definition
, reg
);
1208 /* Add all registers referenced by INSN to the work
1210 for_each_rtx (&PATTERN (def
), mark_references
, worklist
);
1212 /* Inform the analyzer that this insn is going to be
1214 df_insn_delete (df
, BLOCK_FOR_INSN (def
), def
);
1216 VARRAY_RTX (ssa_definition
, reg
) = NULL
;
1220 sbitmap_free (worklist
);