1*404b540aSrobert /* Control flow optimization code for GNU compiler.
2*404b540aSrobert Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3*404b540aSrobert 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4*404b540aSrobert
5*404b540aSrobert This file is part of GCC.
6*404b540aSrobert
7*404b540aSrobert GCC is free software; you can redistribute it and/or modify it under
8*404b540aSrobert the terms of the GNU General Public License as published by the Free
9*404b540aSrobert Software Foundation; either version 2, or (at your option) any later
10*404b540aSrobert version.
11*404b540aSrobert
12*404b540aSrobert GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*404b540aSrobert WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*404b540aSrobert FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15*404b540aSrobert for more details.
16*404b540aSrobert
17*404b540aSrobert You should have received a copy of the GNU General Public License
18*404b540aSrobert along with GCC; see the file COPYING. If not, write to the Free
19*404b540aSrobert Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20*404b540aSrobert 02110-1301, USA. */
21*404b540aSrobert
22*404b540aSrobert /* This file contains optimizer of the control flow. The main entry point is
23*404b540aSrobert cleanup_cfg. Following optimizations are performed:
24*404b540aSrobert
25*404b540aSrobert - Unreachable blocks removal
26*404b540aSrobert - Edge forwarding (edge to the forwarder block is forwarded to its
27*404b540aSrobert successor. Simplification of the branch instruction is performed by
28*404b540aSrobert underlying infrastructure so branch can be converted to simplejump or
29*404b540aSrobert eliminated).
30*404b540aSrobert - Cross jumping (tail merging)
31*404b540aSrobert - Conditional jump-around-simplejump simplification
32*404b540aSrobert - Basic block merging. */
33*404b540aSrobert
34*404b540aSrobert #include "config.h"
35*404b540aSrobert #include "system.h"
36*404b540aSrobert #include "coretypes.h"
37*404b540aSrobert #include "tm.h"
38*404b540aSrobert #include "rtl.h"
39*404b540aSrobert #include "hard-reg-set.h"
40*404b540aSrobert #include "regs.h"
41*404b540aSrobert #include "timevar.h"
42*404b540aSrobert #include "output.h"
43*404b540aSrobert #include "insn-config.h"
44*404b540aSrobert #include "flags.h"
45*404b540aSrobert #include "recog.h"
46*404b540aSrobert #include "toplev.h"
47*404b540aSrobert #include "cselib.h"
48*404b540aSrobert #include "params.h"
49*404b540aSrobert #include "tm_p.h"
50*404b540aSrobert #include "target.h"
51*404b540aSrobert #include "cfglayout.h"
52*404b540aSrobert #include "emit-rtl.h"
53*404b540aSrobert #include "tree-pass.h"
54*404b540aSrobert #include "cfgloop.h"
55*404b540aSrobert #include "expr.h"
56*404b540aSrobert
57*404b540aSrobert #define FORWARDER_BLOCK_P(BB) ((BB)->flags & BB_FORWARDER_BLOCK)
58*404b540aSrobert
59*404b540aSrobert /* Set to true when we are running first pass of try_optimize_cfg loop. */
60*404b540aSrobert static bool first_pass;
61*404b540aSrobert static bool try_crossjump_to_edge (int, edge, edge);
62*404b540aSrobert static bool try_crossjump_bb (int, basic_block);
63*404b540aSrobert static bool outgoing_edges_match (int, basic_block, basic_block);
64*404b540aSrobert static int flow_find_cross_jump (int, basic_block, basic_block, rtx *, rtx *);
65*404b540aSrobert static bool old_insns_match_p (int, rtx, rtx);
66*404b540aSrobert
67*404b540aSrobert static void merge_blocks_move_predecessor_nojumps (basic_block, basic_block);
68*404b540aSrobert static void merge_blocks_move_successor_nojumps (basic_block, basic_block);
69*404b540aSrobert static bool try_optimize_cfg (int);
70*404b540aSrobert static bool try_simplify_condjump (basic_block);
71*404b540aSrobert static bool try_forward_edges (int, basic_block);
72*404b540aSrobert static edge thread_jump (int, edge, basic_block);
73*404b540aSrobert static bool mark_effect (rtx, bitmap);
74*404b540aSrobert static void notice_new_block (basic_block);
75*404b540aSrobert static void update_forwarder_flag (basic_block);
76*404b540aSrobert static int mentions_nonequal_regs (rtx *, void *);
77*404b540aSrobert static void merge_memattrs (rtx, rtx);
78*404b540aSrobert
79*404b540aSrobert /* Set flags for newly created block. */
80*404b540aSrobert
81*404b540aSrobert static void
notice_new_block(basic_block bb)82*404b540aSrobert notice_new_block (basic_block bb)
83*404b540aSrobert {
84*404b540aSrobert if (!bb)
85*404b540aSrobert return;
86*404b540aSrobert
87*404b540aSrobert if (forwarder_block_p (bb))
88*404b540aSrobert bb->flags |= BB_FORWARDER_BLOCK;
89*404b540aSrobert }
90*404b540aSrobert
91*404b540aSrobert /* Recompute forwarder flag after block has been modified. */
92*404b540aSrobert
93*404b540aSrobert static void
update_forwarder_flag(basic_block bb)94*404b540aSrobert update_forwarder_flag (basic_block bb)
95*404b540aSrobert {
96*404b540aSrobert if (forwarder_block_p (bb))
97*404b540aSrobert bb->flags |= BB_FORWARDER_BLOCK;
98*404b540aSrobert else
99*404b540aSrobert bb->flags &= ~BB_FORWARDER_BLOCK;
100*404b540aSrobert }
101*404b540aSrobert
102*404b540aSrobert /* Simplify a conditional jump around an unconditional jump.
103*404b540aSrobert Return true if something changed. */
104*404b540aSrobert
105*404b540aSrobert static bool
try_simplify_condjump(basic_block cbranch_block)106*404b540aSrobert try_simplify_condjump (basic_block cbranch_block)
107*404b540aSrobert {
108*404b540aSrobert basic_block jump_block, jump_dest_block, cbranch_dest_block;
109*404b540aSrobert edge cbranch_jump_edge, cbranch_fallthru_edge;
110*404b540aSrobert rtx cbranch_insn;
111*404b540aSrobert
112*404b540aSrobert /* Verify that there are exactly two successors. */
113*404b540aSrobert if (EDGE_COUNT (cbranch_block->succs) != 2)
114*404b540aSrobert return false;
115*404b540aSrobert
116*404b540aSrobert /* Verify that we've got a normal conditional branch at the end
117*404b540aSrobert of the block. */
118*404b540aSrobert cbranch_insn = BB_END (cbranch_block);
119*404b540aSrobert if (!any_condjump_p (cbranch_insn))
120*404b540aSrobert return false;
121*404b540aSrobert
122*404b540aSrobert cbranch_fallthru_edge = FALLTHRU_EDGE (cbranch_block);
123*404b540aSrobert cbranch_jump_edge = BRANCH_EDGE (cbranch_block);
124*404b540aSrobert
125*404b540aSrobert /* The next block must not have multiple predecessors, must not
126*404b540aSrobert be the last block in the function, and must contain just the
127*404b540aSrobert unconditional jump. */
128*404b540aSrobert jump_block = cbranch_fallthru_edge->dest;
129*404b540aSrobert if (!single_pred_p (jump_block)
130*404b540aSrobert || jump_block->next_bb == EXIT_BLOCK_PTR
131*404b540aSrobert || !FORWARDER_BLOCK_P (jump_block))
132*404b540aSrobert return false;
133*404b540aSrobert jump_dest_block = single_succ (jump_block);
134*404b540aSrobert
135*404b540aSrobert /* If we are partitioning hot/cold basic blocks, we don't want to
136*404b540aSrobert mess up unconditional or indirect jumps that cross between hot
137*404b540aSrobert and cold sections.
138*404b540aSrobert
139*404b540aSrobert Basic block partitioning may result in some jumps that appear to
140*404b540aSrobert be optimizable (or blocks that appear to be mergeable), but which really
141*404b540aSrobert must be left untouched (they are required to make it safely across
142*404b540aSrobert partition boundaries). See the comments at the top of
143*404b540aSrobert bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
144*404b540aSrobert
145*404b540aSrobert if (BB_PARTITION (jump_block) != BB_PARTITION (jump_dest_block)
146*404b540aSrobert || (cbranch_jump_edge->flags & EDGE_CROSSING))
147*404b540aSrobert return false;
148*404b540aSrobert
149*404b540aSrobert /* The conditional branch must target the block after the
150*404b540aSrobert unconditional branch. */
151*404b540aSrobert cbranch_dest_block = cbranch_jump_edge->dest;
152*404b540aSrobert
153*404b540aSrobert if (cbranch_dest_block == EXIT_BLOCK_PTR
154*404b540aSrobert || !can_fallthru (jump_block, cbranch_dest_block))
155*404b540aSrobert return false;
156*404b540aSrobert
157*404b540aSrobert /* Invert the conditional branch. */
158*404b540aSrobert if (!invert_jump (cbranch_insn, block_label (jump_dest_block), 0))
159*404b540aSrobert return false;
160*404b540aSrobert
161*404b540aSrobert if (dump_file)
162*404b540aSrobert fprintf (dump_file, "Simplifying condjump %i around jump %i\n",
163*404b540aSrobert INSN_UID (cbranch_insn), INSN_UID (BB_END (jump_block)));
164*404b540aSrobert
165*404b540aSrobert /* Success. Update the CFG to match. Note that after this point
166*404b540aSrobert the edge variable names appear backwards; the redirection is done
167*404b540aSrobert this way to preserve edge profile data. */
168*404b540aSrobert cbranch_jump_edge = redirect_edge_succ_nodup (cbranch_jump_edge,
169*404b540aSrobert cbranch_dest_block);
170*404b540aSrobert cbranch_fallthru_edge = redirect_edge_succ_nodup (cbranch_fallthru_edge,
171*404b540aSrobert jump_dest_block);
172*404b540aSrobert cbranch_jump_edge->flags |= EDGE_FALLTHRU;
173*404b540aSrobert cbranch_fallthru_edge->flags &= ~EDGE_FALLTHRU;
174*404b540aSrobert update_br_prob_note (cbranch_block);
175*404b540aSrobert
176*404b540aSrobert /* Delete the block with the unconditional jump, and clean up the mess. */
177*404b540aSrobert delete_basic_block (jump_block);
178*404b540aSrobert tidy_fallthru_edge (cbranch_jump_edge);
179*404b540aSrobert update_forwarder_flag (cbranch_block);
180*404b540aSrobert
181*404b540aSrobert return true;
182*404b540aSrobert }
183*404b540aSrobert
184*404b540aSrobert /* Attempt to prove that operation is NOOP using CSElib or mark the effect
185*404b540aSrobert on register. Used by jump threading. */
186*404b540aSrobert
187*404b540aSrobert static bool
mark_effect(rtx exp,regset nonequal)188*404b540aSrobert mark_effect (rtx exp, regset nonequal)
189*404b540aSrobert {
190*404b540aSrobert int regno;
191*404b540aSrobert rtx dest;
192*404b540aSrobert switch (GET_CODE (exp))
193*404b540aSrobert {
194*404b540aSrobert /* In case we do clobber the register, mark it as equal, as we know the
195*404b540aSrobert value is dead so it don't have to match. */
196*404b540aSrobert case CLOBBER:
197*404b540aSrobert if (REG_P (XEXP (exp, 0)))
198*404b540aSrobert {
199*404b540aSrobert dest = XEXP (exp, 0);
200*404b540aSrobert regno = REGNO (dest);
201*404b540aSrobert CLEAR_REGNO_REG_SET (nonequal, regno);
202*404b540aSrobert if (regno < FIRST_PSEUDO_REGISTER)
203*404b540aSrobert {
204*404b540aSrobert int n = hard_regno_nregs[regno][GET_MODE (dest)];
205*404b540aSrobert while (--n > 0)
206*404b540aSrobert CLEAR_REGNO_REG_SET (nonequal, regno + n);
207*404b540aSrobert }
208*404b540aSrobert }
209*404b540aSrobert return false;
210*404b540aSrobert
211*404b540aSrobert case SET:
212*404b540aSrobert if (rtx_equal_for_cselib_p (SET_DEST (exp), SET_SRC (exp)))
213*404b540aSrobert return false;
214*404b540aSrobert dest = SET_DEST (exp);
215*404b540aSrobert if (dest == pc_rtx)
216*404b540aSrobert return false;
217*404b540aSrobert if (!REG_P (dest))
218*404b540aSrobert return true;
219*404b540aSrobert regno = REGNO (dest);
220*404b540aSrobert SET_REGNO_REG_SET (nonequal, regno);
221*404b540aSrobert if (regno < FIRST_PSEUDO_REGISTER)
222*404b540aSrobert {
223*404b540aSrobert int n = hard_regno_nregs[regno][GET_MODE (dest)];
224*404b540aSrobert while (--n > 0)
225*404b540aSrobert SET_REGNO_REG_SET (nonequal, regno + n);
226*404b540aSrobert }
227*404b540aSrobert return false;
228*404b540aSrobert
229*404b540aSrobert default:
230*404b540aSrobert return false;
231*404b540aSrobert }
232*404b540aSrobert }
233*404b540aSrobert
234*404b540aSrobert /* Return nonzero if X is a register set in regset DATA.
235*404b540aSrobert Called via for_each_rtx. */
236*404b540aSrobert static int
mentions_nonequal_regs(rtx * x,void * data)237*404b540aSrobert mentions_nonequal_regs (rtx *x, void *data)
238*404b540aSrobert {
239*404b540aSrobert regset nonequal = (regset) data;
240*404b540aSrobert if (REG_P (*x))
241*404b540aSrobert {
242*404b540aSrobert int regno;
243*404b540aSrobert
244*404b540aSrobert regno = REGNO (*x);
245*404b540aSrobert if (REGNO_REG_SET_P (nonequal, regno))
246*404b540aSrobert return 1;
247*404b540aSrobert if (regno < FIRST_PSEUDO_REGISTER)
248*404b540aSrobert {
249*404b540aSrobert int n = hard_regno_nregs[regno][GET_MODE (*x)];
250*404b540aSrobert while (--n > 0)
251*404b540aSrobert if (REGNO_REG_SET_P (nonequal, regno + n))
252*404b540aSrobert return 1;
253*404b540aSrobert }
254*404b540aSrobert }
255*404b540aSrobert return 0;
256*404b540aSrobert }
257*404b540aSrobert /* Attempt to prove that the basic block B will have no side effects and
258*404b540aSrobert always continues in the same edge if reached via E. Return the edge
259*404b540aSrobert if exist, NULL otherwise. */
260*404b540aSrobert
261*404b540aSrobert static edge
thread_jump(int mode,edge e,basic_block b)262*404b540aSrobert thread_jump (int mode, edge e, basic_block b)
263*404b540aSrobert {
264*404b540aSrobert rtx set1, set2, cond1, cond2, insn;
265*404b540aSrobert enum rtx_code code1, code2, reversed_code2;
266*404b540aSrobert bool reverse1 = false;
267*404b540aSrobert unsigned i;
268*404b540aSrobert regset nonequal;
269*404b540aSrobert bool failed = false;
270*404b540aSrobert reg_set_iterator rsi;
271*404b540aSrobert
272*404b540aSrobert if (b->flags & BB_NONTHREADABLE_BLOCK)
273*404b540aSrobert return NULL;
274*404b540aSrobert
275*404b540aSrobert /* At the moment, we do handle only conditional jumps, but later we may
276*404b540aSrobert want to extend this code to tablejumps and others. */
277*404b540aSrobert if (EDGE_COUNT (e->src->succs) != 2)
278*404b540aSrobert return NULL;
279*404b540aSrobert if (EDGE_COUNT (b->succs) != 2)
280*404b540aSrobert {
281*404b540aSrobert b->flags |= BB_NONTHREADABLE_BLOCK;
282*404b540aSrobert return NULL;
283*404b540aSrobert }
284*404b540aSrobert
285*404b540aSrobert /* Second branch must end with onlyjump, as we will eliminate the jump. */
286*404b540aSrobert if (!any_condjump_p (BB_END (e->src)))
287*404b540aSrobert return NULL;
288*404b540aSrobert
289*404b540aSrobert if (!any_condjump_p (BB_END (b)) || !onlyjump_p (BB_END (b)))
290*404b540aSrobert {
291*404b540aSrobert b->flags |= BB_NONTHREADABLE_BLOCK;
292*404b540aSrobert return NULL;
293*404b540aSrobert }
294*404b540aSrobert
295*404b540aSrobert set1 = pc_set (BB_END (e->src));
296*404b540aSrobert set2 = pc_set (BB_END (b));
297*404b540aSrobert if (((e->flags & EDGE_FALLTHRU) != 0)
298*404b540aSrobert != (XEXP (SET_SRC (set1), 1) == pc_rtx))
299*404b540aSrobert reverse1 = true;
300*404b540aSrobert
301*404b540aSrobert cond1 = XEXP (SET_SRC (set1), 0);
302*404b540aSrobert cond2 = XEXP (SET_SRC (set2), 0);
303*404b540aSrobert if (reverse1)
304*404b540aSrobert code1 = reversed_comparison_code (cond1, BB_END (e->src));
305*404b540aSrobert else
306*404b540aSrobert code1 = GET_CODE (cond1);
307*404b540aSrobert
308*404b540aSrobert code2 = GET_CODE (cond2);
309*404b540aSrobert reversed_code2 = reversed_comparison_code (cond2, BB_END (b));
310*404b540aSrobert
311*404b540aSrobert if (!comparison_dominates_p (code1, code2)
312*404b540aSrobert && !comparison_dominates_p (code1, reversed_code2))
313*404b540aSrobert return NULL;
314*404b540aSrobert
315*404b540aSrobert /* Ensure that the comparison operators are equivalent.
316*404b540aSrobert ??? This is far too pessimistic. We should allow swapped operands,
317*404b540aSrobert different CCmodes, or for example comparisons for interval, that
318*404b540aSrobert dominate even when operands are not equivalent. */
319*404b540aSrobert if (!rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
320*404b540aSrobert || !rtx_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
321*404b540aSrobert return NULL;
322*404b540aSrobert
323*404b540aSrobert /* Short circuit cases where block B contains some side effects, as we can't
324*404b540aSrobert safely bypass it. */
325*404b540aSrobert for (insn = NEXT_INSN (BB_HEAD (b)); insn != NEXT_INSN (BB_END (b));
326*404b540aSrobert insn = NEXT_INSN (insn))
327*404b540aSrobert if (INSN_P (insn) && side_effects_p (PATTERN (insn)))
328*404b540aSrobert {
329*404b540aSrobert b->flags |= BB_NONTHREADABLE_BLOCK;
330*404b540aSrobert return NULL;
331*404b540aSrobert }
332*404b540aSrobert
333*404b540aSrobert cselib_init (false);
334*404b540aSrobert
335*404b540aSrobert /* First process all values computed in the source basic block. */
336*404b540aSrobert for (insn = NEXT_INSN (BB_HEAD (e->src));
337*404b540aSrobert insn != NEXT_INSN (BB_END (e->src));
338*404b540aSrobert insn = NEXT_INSN (insn))
339*404b540aSrobert if (INSN_P (insn))
340*404b540aSrobert cselib_process_insn (insn);
341*404b540aSrobert
342*404b540aSrobert nonequal = BITMAP_ALLOC (NULL);
343*404b540aSrobert CLEAR_REG_SET (nonequal);
344*404b540aSrobert
345*404b540aSrobert /* Now assume that we've continued by the edge E to B and continue
346*404b540aSrobert processing as if it were same basic block.
347*404b540aSrobert Our goal is to prove that whole block is an NOOP. */
348*404b540aSrobert
349*404b540aSrobert for (insn = NEXT_INSN (BB_HEAD (b));
350*404b540aSrobert insn != NEXT_INSN (BB_END (b)) && !failed;
351*404b540aSrobert insn = NEXT_INSN (insn))
352*404b540aSrobert {
353*404b540aSrobert if (INSN_P (insn))
354*404b540aSrobert {
355*404b540aSrobert rtx pat = PATTERN (insn);
356*404b540aSrobert
357*404b540aSrobert if (GET_CODE (pat) == PARALLEL)
358*404b540aSrobert {
359*404b540aSrobert for (i = 0; i < (unsigned)XVECLEN (pat, 0); i++)
360*404b540aSrobert failed |= mark_effect (XVECEXP (pat, 0, i), nonequal);
361*404b540aSrobert }
362*404b540aSrobert else
363*404b540aSrobert failed |= mark_effect (pat, nonequal);
364*404b540aSrobert }
365*404b540aSrobert
366*404b540aSrobert cselib_process_insn (insn);
367*404b540aSrobert }
368*404b540aSrobert
369*404b540aSrobert /* Later we should clear nonequal of dead registers. So far we don't
370*404b540aSrobert have life information in cfg_cleanup. */
371*404b540aSrobert if (failed)
372*404b540aSrobert {
373*404b540aSrobert b->flags |= BB_NONTHREADABLE_BLOCK;
374*404b540aSrobert goto failed_exit;
375*404b540aSrobert }
376*404b540aSrobert
377*404b540aSrobert /* cond2 must not mention any register that is not equal to the
378*404b540aSrobert former block. */
379*404b540aSrobert if (for_each_rtx (&cond2, mentions_nonequal_regs, nonequal))
380*404b540aSrobert goto failed_exit;
381*404b540aSrobert
382*404b540aSrobert /* In case liveness information is available, we need to prove equivalence
383*404b540aSrobert only of the live values. */
384*404b540aSrobert if (mode & CLEANUP_UPDATE_LIFE)
385*404b540aSrobert AND_REG_SET (nonequal, b->il.rtl->global_live_at_end);
386*404b540aSrobert
387*404b540aSrobert EXECUTE_IF_SET_IN_REG_SET (nonequal, 0, i, rsi)
388*404b540aSrobert goto failed_exit;
389*404b540aSrobert
390*404b540aSrobert BITMAP_FREE (nonequal);
391*404b540aSrobert cselib_finish ();
392*404b540aSrobert if ((comparison_dominates_p (code1, code2) != 0)
393*404b540aSrobert != (XEXP (SET_SRC (set2), 1) == pc_rtx))
394*404b540aSrobert return BRANCH_EDGE (b);
395*404b540aSrobert else
396*404b540aSrobert return FALLTHRU_EDGE (b);
397*404b540aSrobert
398*404b540aSrobert failed_exit:
399*404b540aSrobert BITMAP_FREE (nonequal);
400*404b540aSrobert cselib_finish ();
401*404b540aSrobert return NULL;
402*404b540aSrobert }
403*404b540aSrobert
404*404b540aSrobert /* Attempt to forward edges leaving basic block B.
405*404b540aSrobert Return true if successful. */
406*404b540aSrobert
407*404b540aSrobert static bool
try_forward_edges(int mode,basic_block b)408*404b540aSrobert try_forward_edges (int mode, basic_block b)
409*404b540aSrobert {
410*404b540aSrobert bool changed = false;
411*404b540aSrobert edge_iterator ei;
412*404b540aSrobert edge e, *threaded_edges = NULL;
413*404b540aSrobert
414*404b540aSrobert /* If we are partitioning hot/cold basic blocks, we don't want to
415*404b540aSrobert mess up unconditional or indirect jumps that cross between hot
416*404b540aSrobert and cold sections.
417*404b540aSrobert
418*404b540aSrobert Basic block partitioning may result in some jumps that appear to
419*404b540aSrobert be optimizable (or blocks that appear to be mergeable), but which really m
420*404b540aSrobert ust be left untouched (they are required to make it safely across
421*404b540aSrobert partition boundaries). See the comments at the top of
422*404b540aSrobert bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
423*404b540aSrobert
424*404b540aSrobert if (find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX))
425*404b540aSrobert return false;
426*404b540aSrobert
427*404b540aSrobert for (ei = ei_start (b->succs); (e = ei_safe_edge (ei)); )
428*404b540aSrobert {
429*404b540aSrobert basic_block target, first;
430*404b540aSrobert int counter;
431*404b540aSrobert bool threaded = false;
432*404b540aSrobert int nthreaded_edges = 0;
433*404b540aSrobert bool may_thread = first_pass | (b->flags & BB_DIRTY);
434*404b540aSrobert
435*404b540aSrobert /* Skip complex edges because we don't know how to update them.
436*404b540aSrobert
437*404b540aSrobert Still handle fallthru edges, as we can succeed to forward fallthru
438*404b540aSrobert edge to the same place as the branch edge of conditional branch
439*404b540aSrobert and turn conditional branch to an unconditional branch. */
440*404b540aSrobert if (e->flags & EDGE_COMPLEX)
441*404b540aSrobert {
442*404b540aSrobert ei_next (&ei);
443*404b540aSrobert continue;
444*404b540aSrobert }
445*404b540aSrobert
446*404b540aSrobert target = first = e->dest;
447*404b540aSrobert counter = NUM_FIXED_BLOCKS;
448*404b540aSrobert
449*404b540aSrobert /* If we are partitioning hot/cold basic_blocks, we don't want to mess
450*404b540aSrobert up jumps that cross between hot/cold sections.
451*404b540aSrobert
452*404b540aSrobert Basic block partitioning may result in some jumps that appear
453*404b540aSrobert to be optimizable (or blocks that appear to be mergeable), but which
454*404b540aSrobert really must be left untouched (they are required to make it safely
455*404b540aSrobert across partition boundaries). See the comments at the top of
456*404b540aSrobert bb-reorder.c:partition_hot_cold_basic_blocks for complete
457*404b540aSrobert details. */
458*404b540aSrobert
459*404b540aSrobert if (first != EXIT_BLOCK_PTR
460*404b540aSrobert && find_reg_note (BB_END (first), REG_CROSSING_JUMP, NULL_RTX))
461*404b540aSrobert return false;
462*404b540aSrobert
463*404b540aSrobert while (counter < n_basic_blocks)
464*404b540aSrobert {
465*404b540aSrobert basic_block new_target = NULL;
466*404b540aSrobert bool new_target_threaded = false;
467*404b540aSrobert may_thread |= target->flags & BB_DIRTY;
468*404b540aSrobert
469*404b540aSrobert if (FORWARDER_BLOCK_P (target)
470*404b540aSrobert && !(single_succ_edge (target)->flags & EDGE_CROSSING)
471*404b540aSrobert && single_succ (target) != EXIT_BLOCK_PTR)
472*404b540aSrobert {
473*404b540aSrobert /* Bypass trivial infinite loops. */
474*404b540aSrobert new_target = single_succ (target);
475*404b540aSrobert if (target == new_target)
476*404b540aSrobert counter = n_basic_blocks;
477*404b540aSrobert }
478*404b540aSrobert
479*404b540aSrobert /* Allow to thread only over one edge at time to simplify updating
480*404b540aSrobert of probabilities. */
481*404b540aSrobert else if ((mode & CLEANUP_THREADING) && may_thread)
482*404b540aSrobert {
483*404b540aSrobert edge t = thread_jump (mode, e, target);
484*404b540aSrobert if (t)
485*404b540aSrobert {
486*404b540aSrobert if (!threaded_edges)
487*404b540aSrobert threaded_edges = XNEWVEC (edge, n_basic_blocks);
488*404b540aSrobert else
489*404b540aSrobert {
490*404b540aSrobert int i;
491*404b540aSrobert
492*404b540aSrobert /* Detect an infinite loop across blocks not
493*404b540aSrobert including the start block. */
494*404b540aSrobert for (i = 0; i < nthreaded_edges; ++i)
495*404b540aSrobert if (threaded_edges[i] == t)
496*404b540aSrobert break;
497*404b540aSrobert if (i < nthreaded_edges)
498*404b540aSrobert {
499*404b540aSrobert counter = n_basic_blocks;
500*404b540aSrobert break;
501*404b540aSrobert }
502*404b540aSrobert }
503*404b540aSrobert
504*404b540aSrobert /* Detect an infinite loop across the start block. */
505*404b540aSrobert if (t->dest == b)
506*404b540aSrobert break;
507*404b540aSrobert
508*404b540aSrobert gcc_assert (nthreaded_edges < n_basic_blocks - NUM_FIXED_BLOCKS);
509*404b540aSrobert threaded_edges[nthreaded_edges++] = t;
510*404b540aSrobert
511*404b540aSrobert new_target = t->dest;
512*404b540aSrobert new_target_threaded = true;
513*404b540aSrobert }
514*404b540aSrobert }
515*404b540aSrobert
516*404b540aSrobert if (!new_target)
517*404b540aSrobert break;
518*404b540aSrobert
519*404b540aSrobert counter++;
520*404b540aSrobert target = new_target;
521*404b540aSrobert threaded |= new_target_threaded;
522*404b540aSrobert }
523*404b540aSrobert
524*404b540aSrobert if (counter >= n_basic_blocks)
525*404b540aSrobert {
526*404b540aSrobert if (dump_file)
527*404b540aSrobert fprintf (dump_file, "Infinite loop in BB %i.\n",
528*404b540aSrobert target->index);
529*404b540aSrobert }
530*404b540aSrobert else if (target == first)
531*404b540aSrobert ; /* We didn't do anything. */
532*404b540aSrobert else
533*404b540aSrobert {
534*404b540aSrobert /* Save the values now, as the edge may get removed. */
535*404b540aSrobert gcov_type edge_count = e->count;
536*404b540aSrobert int edge_probability = e->probability;
537*404b540aSrobert int edge_frequency;
538*404b540aSrobert int n = 0;
539*404b540aSrobert
540*404b540aSrobert /* Don't force if target is exit block. */
541*404b540aSrobert if (threaded && target != EXIT_BLOCK_PTR)
542*404b540aSrobert {
543*404b540aSrobert notice_new_block (redirect_edge_and_branch_force (e, target));
544*404b540aSrobert if (dump_file)
545*404b540aSrobert fprintf (dump_file, "Conditionals threaded.\n");
546*404b540aSrobert }
547*404b540aSrobert else if (!redirect_edge_and_branch (e, target))
548*404b540aSrobert {
549*404b540aSrobert if (dump_file)
550*404b540aSrobert fprintf (dump_file,
551*404b540aSrobert "Forwarding edge %i->%i to %i failed.\n",
552*404b540aSrobert b->index, e->dest->index, target->index);
553*404b540aSrobert ei_next (&ei);
554*404b540aSrobert continue;
555*404b540aSrobert }
556*404b540aSrobert
557*404b540aSrobert /* We successfully forwarded the edge. Now update profile
558*404b540aSrobert data: for each edge we traversed in the chain, remove
559*404b540aSrobert the original edge's execution count. */
560*404b540aSrobert edge_frequency = ((edge_probability * b->frequency
561*404b540aSrobert + REG_BR_PROB_BASE / 2)
562*404b540aSrobert / REG_BR_PROB_BASE);
563*404b540aSrobert
564*404b540aSrobert if (!FORWARDER_BLOCK_P (b) && forwarder_block_p (b))
565*404b540aSrobert b->flags |= BB_FORWARDER_BLOCK;
566*404b540aSrobert
567*404b540aSrobert do
568*404b540aSrobert {
569*404b540aSrobert edge t;
570*404b540aSrobert
571*404b540aSrobert if (!single_succ_p (first))
572*404b540aSrobert {
573*404b540aSrobert gcc_assert (n < nthreaded_edges);
574*404b540aSrobert t = threaded_edges [n++];
575*404b540aSrobert gcc_assert (t->src == first);
576*404b540aSrobert update_bb_profile_for_threading (first, edge_frequency,
577*404b540aSrobert edge_count, t);
578*404b540aSrobert update_br_prob_note (first);
579*404b540aSrobert }
580*404b540aSrobert else
581*404b540aSrobert {
582*404b540aSrobert first->count -= edge_count;
583*404b540aSrobert if (first->count < 0)
584*404b540aSrobert first->count = 0;
585*404b540aSrobert first->frequency -= edge_frequency;
586*404b540aSrobert if (first->frequency < 0)
587*404b540aSrobert first->frequency = 0;
588*404b540aSrobert /* It is possible that as the result of
589*404b540aSrobert threading we've removed edge as it is
590*404b540aSrobert threaded to the fallthru edge. Avoid
591*404b540aSrobert getting out of sync. */
592*404b540aSrobert if (n < nthreaded_edges
593*404b540aSrobert && first == threaded_edges [n]->src)
594*404b540aSrobert n++;
595*404b540aSrobert t = single_succ_edge (first);
596*404b540aSrobert }
597*404b540aSrobert
598*404b540aSrobert t->count -= edge_count;
599*404b540aSrobert if (t->count < 0)
600*404b540aSrobert t->count = 0;
601*404b540aSrobert first = t->dest;
602*404b540aSrobert }
603*404b540aSrobert while (first != target);
604*404b540aSrobert
605*404b540aSrobert changed = true;
606*404b540aSrobert continue;
607*404b540aSrobert }
608*404b540aSrobert ei_next (&ei);
609*404b540aSrobert }
610*404b540aSrobert
611*404b540aSrobert if (threaded_edges)
612*404b540aSrobert free (threaded_edges);
613*404b540aSrobert return changed;
614*404b540aSrobert }
615*404b540aSrobert
616*404b540aSrobert
617*404b540aSrobert /* Blocks A and B are to be merged into a single block. A has no incoming
618*404b540aSrobert fallthru edge, so it can be moved before B without adding or modifying
619*404b540aSrobert any jumps (aside from the jump from A to B). */
620*404b540aSrobert
621*404b540aSrobert static void
merge_blocks_move_predecessor_nojumps(basic_block a,basic_block b)622*404b540aSrobert merge_blocks_move_predecessor_nojumps (basic_block a, basic_block b)
623*404b540aSrobert {
624*404b540aSrobert rtx barrier;
625*404b540aSrobert bool only_notes;
626*404b540aSrobert
627*404b540aSrobert /* If we are partitioning hot/cold basic blocks, we don't want to
628*404b540aSrobert mess up unconditional or indirect jumps that cross between hot
629*404b540aSrobert and cold sections.
630*404b540aSrobert
631*404b540aSrobert Basic block partitioning may result in some jumps that appear to
632*404b540aSrobert be optimizable (or blocks that appear to be mergeable), but which really
633*404b540aSrobert must be left untouched (they are required to make it safely across
634*404b540aSrobert partition boundaries). See the comments at the top of
635*404b540aSrobert bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
636*404b540aSrobert
637*404b540aSrobert if (BB_PARTITION (a) != BB_PARTITION (b))
638*404b540aSrobert return;
639*404b540aSrobert
640*404b540aSrobert barrier = next_nonnote_insn (BB_END (a));
641*404b540aSrobert gcc_assert (BARRIER_P (barrier));
642*404b540aSrobert delete_insn (barrier);
643*404b540aSrobert
644*404b540aSrobert /* Move block and loop notes out of the chain so that we do not
645*404b540aSrobert disturb their order.
646*404b540aSrobert
647*404b540aSrobert ??? A better solution would be to squeeze out all the non-nested notes
648*404b540aSrobert and adjust the block trees appropriately. Even better would be to have
649*404b540aSrobert a tighter connection between block trees and rtl so that this is not
650*404b540aSrobert necessary. */
651*404b540aSrobert only_notes = squeeze_notes (&BB_HEAD (a), &BB_END (a));
652*404b540aSrobert gcc_assert (!only_notes);
653*404b540aSrobert
654*404b540aSrobert /* Scramble the insn chain. */
655*404b540aSrobert if (BB_END (a) != PREV_INSN (BB_HEAD (b)))
656*404b540aSrobert reorder_insns_nobb (BB_HEAD (a), BB_END (a), PREV_INSN (BB_HEAD (b)));
657*404b540aSrobert a->flags |= BB_DIRTY;
658*404b540aSrobert
659*404b540aSrobert if (dump_file)
660*404b540aSrobert fprintf (dump_file, "Moved block %d before %d and merged.\n",
661*404b540aSrobert a->index, b->index);
662*404b540aSrobert
663*404b540aSrobert /* Swap the records for the two blocks around. */
664*404b540aSrobert
665*404b540aSrobert unlink_block (a);
666*404b540aSrobert link_block (a, b->prev_bb);
667*404b540aSrobert
668*404b540aSrobert /* Now blocks A and B are contiguous. Merge them. */
669*404b540aSrobert merge_blocks (a, b);
670*404b540aSrobert }
671*404b540aSrobert
672*404b540aSrobert /* Blocks A and B are to be merged into a single block. B has no outgoing
673*404b540aSrobert fallthru edge, so it can be moved after A without adding or modifying
674*404b540aSrobert any jumps (aside from the jump from A to B). */
675*404b540aSrobert
676*404b540aSrobert static void
merge_blocks_move_successor_nojumps(basic_block a,basic_block b)677*404b540aSrobert merge_blocks_move_successor_nojumps (basic_block a, basic_block b)
678*404b540aSrobert {
679*404b540aSrobert rtx barrier, real_b_end;
680*404b540aSrobert rtx label, table;
681*404b540aSrobert bool only_notes;
682*404b540aSrobert
683*404b540aSrobert /* If we are partitioning hot/cold basic blocks, we don't want to
684*404b540aSrobert mess up unconditional or indirect jumps that cross between hot
685*404b540aSrobert and cold sections.
686*404b540aSrobert
687*404b540aSrobert Basic block partitioning may result in some jumps that appear to
688*404b540aSrobert be optimizable (or blocks that appear to be mergeable), but which really
689*404b540aSrobert must be left untouched (they are required to make it safely across
690*404b540aSrobert partition boundaries). See the comments at the top of
691*404b540aSrobert bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
692*404b540aSrobert
693*404b540aSrobert if (BB_PARTITION (a) != BB_PARTITION (b))
694*404b540aSrobert return;
695*404b540aSrobert
696*404b540aSrobert real_b_end = BB_END (b);
697*404b540aSrobert
698*404b540aSrobert /* If there is a jump table following block B temporarily add the jump table
699*404b540aSrobert to block B so that it will also be moved to the correct location. */
700*404b540aSrobert if (tablejump_p (BB_END (b), &label, &table)
701*404b540aSrobert && prev_active_insn (label) == BB_END (b))
702*404b540aSrobert {
703*404b540aSrobert BB_END (b) = table;
704*404b540aSrobert }
705*404b540aSrobert
706*404b540aSrobert /* There had better have been a barrier there. Delete it. */
707*404b540aSrobert barrier = NEXT_INSN (BB_END (b));
708*404b540aSrobert if (barrier && BARRIER_P (barrier))
709*404b540aSrobert delete_insn (barrier);
710*404b540aSrobert
711*404b540aSrobert /* Move block and loop notes out of the chain so that we do not
712*404b540aSrobert disturb their order.
713*404b540aSrobert
714*404b540aSrobert ??? A better solution would be to squeeze out all the non-nested notes
715*404b540aSrobert and adjust the block trees appropriately. Even better would be to have
716*404b540aSrobert a tighter connection between block trees and rtl so that this is not
717*404b540aSrobert necessary. */
718*404b540aSrobert only_notes = squeeze_notes (&BB_HEAD (b), &BB_END (b));
719*404b540aSrobert gcc_assert (!only_notes);
720*404b540aSrobert
721*404b540aSrobert
722*404b540aSrobert /* Scramble the insn chain. */
723*404b540aSrobert reorder_insns_nobb (BB_HEAD (b), BB_END (b), BB_END (a));
724*404b540aSrobert
725*404b540aSrobert /* Restore the real end of b. */
726*404b540aSrobert BB_END (b) = real_b_end;
727*404b540aSrobert
728*404b540aSrobert if (dump_file)
729*404b540aSrobert fprintf (dump_file, "Moved block %d after %d and merged.\n",
730*404b540aSrobert b->index, a->index);
731*404b540aSrobert
732*404b540aSrobert /* Now blocks A and B are contiguous. Merge them. */
733*404b540aSrobert merge_blocks (a, b);
734*404b540aSrobert }
735*404b540aSrobert
736*404b540aSrobert /* Attempt to merge basic blocks that are potentially non-adjacent.
737*404b540aSrobert Return NULL iff the attempt failed, otherwise return basic block
738*404b540aSrobert where cleanup_cfg should continue. Because the merging commonly
739*404b540aSrobert moves basic block away or introduces another optimization
740*404b540aSrobert possibility, return basic block just before B so cleanup_cfg don't
741*404b540aSrobert need to iterate.
742*404b540aSrobert
743*404b540aSrobert It may be good idea to return basic block before C in the case
744*404b540aSrobert C has been moved after B and originally appeared earlier in the
745*404b540aSrobert insn sequence, but we have no information available about the
746*404b540aSrobert relative ordering of these two. Hopefully it is not too common. */
747*404b540aSrobert
748*404b540aSrobert static basic_block
merge_blocks_move(edge e,basic_block b,basic_block c,int mode)749*404b540aSrobert merge_blocks_move (edge e, basic_block b, basic_block c, int mode)
750*404b540aSrobert {
751*404b540aSrobert basic_block next;
752*404b540aSrobert
753*404b540aSrobert /* If we are partitioning hot/cold basic blocks, we don't want to
754*404b540aSrobert mess up unconditional or indirect jumps that cross between hot
755*404b540aSrobert and cold sections.
756*404b540aSrobert
757*404b540aSrobert Basic block partitioning may result in some jumps that appear to
758*404b540aSrobert be optimizable (or blocks that appear to be mergeable), but which really
759*404b540aSrobert must be left untouched (they are required to make it safely across
760*404b540aSrobert partition boundaries). See the comments at the top of
761*404b540aSrobert bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
762*404b540aSrobert
763*404b540aSrobert if (BB_PARTITION (b) != BB_PARTITION (c))
764*404b540aSrobert return NULL;
765*404b540aSrobert
766*404b540aSrobert
767*404b540aSrobert
768*404b540aSrobert /* If B has a fallthru edge to C, no need to move anything. */
769*404b540aSrobert if (e->flags & EDGE_FALLTHRU)
770*404b540aSrobert {
771*404b540aSrobert int b_index = b->index, c_index = c->index;
772*404b540aSrobert merge_blocks (b, c);
773*404b540aSrobert update_forwarder_flag (b);
774*404b540aSrobert
775*404b540aSrobert if (dump_file)
776*404b540aSrobert fprintf (dump_file, "Merged %d and %d without moving.\n",
777*404b540aSrobert b_index, c_index);
778*404b540aSrobert
779*404b540aSrobert return b->prev_bb == ENTRY_BLOCK_PTR ? b : b->prev_bb;
780*404b540aSrobert }
781*404b540aSrobert
782*404b540aSrobert /* Otherwise we will need to move code around. Do that only if expensive
783*404b540aSrobert transformations are allowed. */
784*404b540aSrobert else if (mode & CLEANUP_EXPENSIVE)
785*404b540aSrobert {
786*404b540aSrobert edge tmp_edge, b_fallthru_edge;
787*404b540aSrobert bool c_has_outgoing_fallthru;
788*404b540aSrobert bool b_has_incoming_fallthru;
789*404b540aSrobert edge_iterator ei;
790*404b540aSrobert
791*404b540aSrobert /* Avoid overactive code motion, as the forwarder blocks should be
792*404b540aSrobert eliminated by edge redirection instead. One exception might have
793*404b540aSrobert been if B is a forwarder block and C has no fallthru edge, but
794*404b540aSrobert that should be cleaned up by bb-reorder instead. */
795*404b540aSrobert if (FORWARDER_BLOCK_P (b) || FORWARDER_BLOCK_P (c))
796*404b540aSrobert return NULL;
797*404b540aSrobert
798*404b540aSrobert /* We must make sure to not munge nesting of lexical blocks,
799*404b540aSrobert and loop notes. This is done by squeezing out all the notes
800*404b540aSrobert and leaving them there to lie. Not ideal, but functional. */
801*404b540aSrobert
802*404b540aSrobert FOR_EACH_EDGE (tmp_edge, ei, c->succs)
803*404b540aSrobert if (tmp_edge->flags & EDGE_FALLTHRU)
804*404b540aSrobert break;
805*404b540aSrobert
806*404b540aSrobert c_has_outgoing_fallthru = (tmp_edge != NULL);
807*404b540aSrobert
808*404b540aSrobert FOR_EACH_EDGE (tmp_edge, ei, b->preds)
809*404b540aSrobert if (tmp_edge->flags & EDGE_FALLTHRU)
810*404b540aSrobert break;
811*404b540aSrobert
812*404b540aSrobert b_has_incoming_fallthru = (tmp_edge != NULL);
813*404b540aSrobert b_fallthru_edge = tmp_edge;
814*404b540aSrobert next = b->prev_bb;
815*404b540aSrobert if (next == c)
816*404b540aSrobert next = next->prev_bb;
817*404b540aSrobert
818*404b540aSrobert /* Otherwise, we're going to try to move C after B. If C does
819*404b540aSrobert not have an outgoing fallthru, then it can be moved
820*404b540aSrobert immediately after B without introducing or modifying jumps. */
821*404b540aSrobert if (! c_has_outgoing_fallthru)
822*404b540aSrobert {
823*404b540aSrobert merge_blocks_move_successor_nojumps (b, c);
824*404b540aSrobert return next == ENTRY_BLOCK_PTR ? next->next_bb : next;
825*404b540aSrobert }
826*404b540aSrobert
827*404b540aSrobert /* If B does not have an incoming fallthru, then it can be moved
828*404b540aSrobert immediately before C without introducing or modifying jumps.
829*404b540aSrobert C cannot be the first block, so we do not have to worry about
830*404b540aSrobert accessing a non-existent block. */
831*404b540aSrobert
832*404b540aSrobert if (b_has_incoming_fallthru)
833*404b540aSrobert {
834*404b540aSrobert basic_block bb;
835*404b540aSrobert
836*404b540aSrobert if (b_fallthru_edge->src == ENTRY_BLOCK_PTR)
837*404b540aSrobert return NULL;
838*404b540aSrobert bb = force_nonfallthru (b_fallthru_edge);
839*404b540aSrobert if (bb)
840*404b540aSrobert notice_new_block (bb);
841*404b540aSrobert }
842*404b540aSrobert
843*404b540aSrobert merge_blocks_move_predecessor_nojumps (b, c);
844*404b540aSrobert return next == ENTRY_BLOCK_PTR ? next->next_bb : next;
845*404b540aSrobert }
846*404b540aSrobert
847*404b540aSrobert return NULL;
848*404b540aSrobert }
849*404b540aSrobert
850*404b540aSrobert
851*404b540aSrobert /* Removes the memory attributes of MEM expression
852*404b540aSrobert if they are not equal. */
853*404b540aSrobert
854*404b540aSrobert void
merge_memattrs(rtx x,rtx y)855*404b540aSrobert merge_memattrs (rtx x, rtx y)
856*404b540aSrobert {
857*404b540aSrobert int i;
858*404b540aSrobert int j;
859*404b540aSrobert enum rtx_code code;
860*404b540aSrobert const char *fmt;
861*404b540aSrobert
862*404b540aSrobert if (x == y)
863*404b540aSrobert return;
864*404b540aSrobert if (x == 0 || y == 0)
865*404b540aSrobert return;
866*404b540aSrobert
867*404b540aSrobert code = GET_CODE (x);
868*404b540aSrobert
869*404b540aSrobert if (code != GET_CODE (y))
870*404b540aSrobert return;
871*404b540aSrobert
872*404b540aSrobert if (GET_MODE (x) != GET_MODE (y))
873*404b540aSrobert return;
874*404b540aSrobert
875*404b540aSrobert if (code == MEM && MEM_ATTRS (x) != MEM_ATTRS (y))
876*404b540aSrobert {
877*404b540aSrobert if (! MEM_ATTRS (x))
878*404b540aSrobert MEM_ATTRS (y) = 0;
879*404b540aSrobert else if (! MEM_ATTRS (y))
880*404b540aSrobert MEM_ATTRS (x) = 0;
881*404b540aSrobert else
882*404b540aSrobert {
883*404b540aSrobert rtx mem_size;
884*404b540aSrobert
885*404b540aSrobert if (MEM_ALIAS_SET (x) != MEM_ALIAS_SET (y))
886*404b540aSrobert {
887*404b540aSrobert set_mem_alias_set (x, 0);
888*404b540aSrobert set_mem_alias_set (y, 0);
889*404b540aSrobert }
890*404b540aSrobert
891*404b540aSrobert if (! mem_expr_equal_p (MEM_EXPR (x), MEM_EXPR (y)))
892*404b540aSrobert {
893*404b540aSrobert set_mem_expr (x, 0);
894*404b540aSrobert set_mem_expr (y, 0);
895*404b540aSrobert set_mem_offset (x, 0);
896*404b540aSrobert set_mem_offset (y, 0);
897*404b540aSrobert }
898*404b540aSrobert else if (MEM_OFFSET (x) != MEM_OFFSET (y))
899*404b540aSrobert {
900*404b540aSrobert set_mem_offset (x, 0);
901*404b540aSrobert set_mem_offset (y, 0);
902*404b540aSrobert }
903*404b540aSrobert
904*404b540aSrobert if (!MEM_SIZE (x))
905*404b540aSrobert mem_size = NULL_RTX;
906*404b540aSrobert else if (!MEM_SIZE (y))
907*404b540aSrobert mem_size = NULL_RTX;
908*404b540aSrobert else
909*404b540aSrobert mem_size = GEN_INT (MAX (INTVAL (MEM_SIZE (x)),
910*404b540aSrobert INTVAL (MEM_SIZE (y))));
911*404b540aSrobert set_mem_size (x, mem_size);
912*404b540aSrobert set_mem_size (y, mem_size);
913*404b540aSrobert
914*404b540aSrobert set_mem_align (x, MIN (MEM_ALIGN (x), MEM_ALIGN (y)));
915*404b540aSrobert set_mem_align (y, MEM_ALIGN (x));
916*404b540aSrobert }
917*404b540aSrobert }
918*404b540aSrobert
919*404b540aSrobert fmt = GET_RTX_FORMAT (code);
920*404b540aSrobert for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
921*404b540aSrobert {
922*404b540aSrobert switch (fmt[i])
923*404b540aSrobert {
924*404b540aSrobert case 'E':
925*404b540aSrobert /* Two vectors must have the same length. */
926*404b540aSrobert if (XVECLEN (x, i) != XVECLEN (y, i))
927*404b540aSrobert return;
928*404b540aSrobert
929*404b540aSrobert for (j = 0; j < XVECLEN (x, i); j++)
930*404b540aSrobert merge_memattrs (XVECEXP (x, i, j), XVECEXP (y, i, j));
931*404b540aSrobert
932*404b540aSrobert break;
933*404b540aSrobert
934*404b540aSrobert case 'e':
935*404b540aSrobert merge_memattrs (XEXP (x, i), XEXP (y, i));
936*404b540aSrobert }
937*404b540aSrobert }
938*404b540aSrobert return;
939*404b540aSrobert }
940*404b540aSrobert
941*404b540aSrobert
942*404b540aSrobert /* Return true if I1 and I2 are equivalent and thus can be crossjumped. */
943*404b540aSrobert
944*404b540aSrobert static bool
old_insns_match_p(int mode ATTRIBUTE_UNUSED,rtx i1,rtx i2)945*404b540aSrobert old_insns_match_p (int mode ATTRIBUTE_UNUSED, rtx i1, rtx i2)
946*404b540aSrobert {
947*404b540aSrobert rtx p1, p2;
948*404b540aSrobert
949*404b540aSrobert /* Verify that I1 and I2 are equivalent. */
950*404b540aSrobert if (GET_CODE (i1) != GET_CODE (i2))
951*404b540aSrobert return false;
952*404b540aSrobert
953*404b540aSrobert p1 = PATTERN (i1);
954*404b540aSrobert p2 = PATTERN (i2);
955*404b540aSrobert
956*404b540aSrobert if (GET_CODE (p1) != GET_CODE (p2))
957*404b540aSrobert return false;
958*404b540aSrobert
959*404b540aSrobert /* If this is a CALL_INSN, compare register usage information.
960*404b540aSrobert If we don't check this on stack register machines, the two
961*404b540aSrobert CALL_INSNs might be merged leaving reg-stack.c with mismatching
962*404b540aSrobert numbers of stack registers in the same basic block.
963*404b540aSrobert If we don't check this on machines with delay slots, a delay slot may
964*404b540aSrobert be filled that clobbers a parameter expected by the subroutine.
965*404b540aSrobert
966*404b540aSrobert ??? We take the simple route for now and assume that if they're
967*404b540aSrobert equal, they were constructed identically. */
968*404b540aSrobert
969*404b540aSrobert if (CALL_P (i1)
970*404b540aSrobert && (!rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
971*404b540aSrobert CALL_INSN_FUNCTION_USAGE (i2))
972*404b540aSrobert || SIBLING_CALL_P (i1) != SIBLING_CALL_P (i2)))
973*404b540aSrobert return false;
974*404b540aSrobert
975*404b540aSrobert #ifdef STACK_REGS
976*404b540aSrobert /* If cross_jump_death_matters is not 0, the insn's mode
977*404b540aSrobert indicates whether or not the insn contains any stack-like
978*404b540aSrobert regs. */
979*404b540aSrobert
980*404b540aSrobert if ((mode & CLEANUP_POST_REGSTACK) && stack_regs_mentioned (i1))
981*404b540aSrobert {
982*404b540aSrobert /* If register stack conversion has already been done, then
983*404b540aSrobert death notes must also be compared before it is certain that
984*404b540aSrobert the two instruction streams match. */
985*404b540aSrobert
986*404b540aSrobert rtx note;
987*404b540aSrobert HARD_REG_SET i1_regset, i2_regset;
988*404b540aSrobert
989*404b540aSrobert CLEAR_HARD_REG_SET (i1_regset);
990*404b540aSrobert CLEAR_HARD_REG_SET (i2_regset);
991*404b540aSrobert
992*404b540aSrobert for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
993*404b540aSrobert if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
994*404b540aSrobert SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
995*404b540aSrobert
996*404b540aSrobert for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
997*404b540aSrobert if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
998*404b540aSrobert SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
999*404b540aSrobert
1000*404b540aSrobert GO_IF_HARD_REG_EQUAL (i1_regset, i2_regset, done);
1001*404b540aSrobert
1002*404b540aSrobert return false;
1003*404b540aSrobert
1004*404b540aSrobert done:
1005*404b540aSrobert ;
1006*404b540aSrobert }
1007*404b540aSrobert #endif
1008*404b540aSrobert
1009*404b540aSrobert if (reload_completed
1010*404b540aSrobert ? rtx_renumbered_equal_p (p1, p2) : rtx_equal_p (p1, p2))
1011*404b540aSrobert return true;
1012*404b540aSrobert
1013*404b540aSrobert /* Do not do EQUIV substitution after reload. First, we're undoing the
1014*404b540aSrobert work of reload_cse. Second, we may be undoing the work of the post-
1015*404b540aSrobert reload splitting pass. */
1016*404b540aSrobert /* ??? Possibly add a new phase switch variable that can be used by
1017*404b540aSrobert targets to disallow the troublesome insns after splitting. */
1018*404b540aSrobert if (!reload_completed)
1019*404b540aSrobert {
1020*404b540aSrobert /* The following code helps take care of G++ cleanups. */
1021*404b540aSrobert rtx equiv1 = find_reg_equal_equiv_note (i1);
1022*404b540aSrobert rtx equiv2 = find_reg_equal_equiv_note (i2);
1023*404b540aSrobert
1024*404b540aSrobert if (equiv1 && equiv2
1025*404b540aSrobert /* If the equivalences are not to a constant, they may
1026*404b540aSrobert reference pseudos that no longer exist, so we can't
1027*404b540aSrobert use them. */
1028*404b540aSrobert && (! reload_completed
1029*404b540aSrobert || (CONSTANT_P (XEXP (equiv1, 0))
1030*404b540aSrobert && rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))))
1031*404b540aSrobert {
1032*404b540aSrobert rtx s1 = single_set (i1);
1033*404b540aSrobert rtx s2 = single_set (i2);
1034*404b540aSrobert if (s1 != 0 && s2 != 0
1035*404b540aSrobert && rtx_renumbered_equal_p (SET_DEST (s1), SET_DEST (s2)))
1036*404b540aSrobert {
1037*404b540aSrobert validate_change (i1, &SET_SRC (s1), XEXP (equiv1, 0), 1);
1038*404b540aSrobert validate_change (i2, &SET_SRC (s2), XEXP (equiv2, 0), 1);
1039*404b540aSrobert if (! rtx_renumbered_equal_p (p1, p2))
1040*404b540aSrobert cancel_changes (0);
1041*404b540aSrobert else if (apply_change_group ())
1042*404b540aSrobert return true;
1043*404b540aSrobert }
1044*404b540aSrobert }
1045*404b540aSrobert }
1046*404b540aSrobert
1047*404b540aSrobert return false;
1048*404b540aSrobert }
1049*404b540aSrobert
1050*404b540aSrobert /* Look through the insns at the end of BB1 and BB2 and find the longest
1051*404b540aSrobert sequence that are equivalent. Store the first insns for that sequence
1052*404b540aSrobert in *F1 and *F2 and return the sequence length.
1053*404b540aSrobert
1054*404b540aSrobert To simplify callers of this function, if the blocks match exactly,
1055*404b540aSrobert store the head of the blocks in *F1 and *F2. */
1056*404b540aSrobert
1057*404b540aSrobert static int
flow_find_cross_jump(int mode ATTRIBUTE_UNUSED,basic_block bb1,basic_block bb2,rtx * f1,rtx * f2)1058*404b540aSrobert flow_find_cross_jump (int mode ATTRIBUTE_UNUSED, basic_block bb1,
1059*404b540aSrobert basic_block bb2, rtx *f1, rtx *f2)
1060*404b540aSrobert {
1061*404b540aSrobert rtx i1, i2, last1, last2, afterlast1, afterlast2;
1062*404b540aSrobert int ninsns = 0;
1063*404b540aSrobert
1064*404b540aSrobert /* Skip simple jumps at the end of the blocks. Complex jumps still
1065*404b540aSrobert need to be compared for equivalence, which we'll do below. */
1066*404b540aSrobert
1067*404b540aSrobert i1 = BB_END (bb1);
1068*404b540aSrobert last1 = afterlast1 = last2 = afterlast2 = NULL_RTX;
1069*404b540aSrobert if (onlyjump_p (i1)
1070*404b540aSrobert || (returnjump_p (i1) && !side_effects_p (PATTERN (i1))))
1071*404b540aSrobert {
1072*404b540aSrobert last1 = i1;
1073*404b540aSrobert i1 = PREV_INSN (i1);
1074*404b540aSrobert }
1075*404b540aSrobert
1076*404b540aSrobert i2 = BB_END (bb2);
1077*404b540aSrobert if (onlyjump_p (i2)
1078*404b540aSrobert || (returnjump_p (i2) && !side_effects_p (PATTERN (i2))))
1079*404b540aSrobert {
1080*404b540aSrobert last2 = i2;
1081*404b540aSrobert /* Count everything except for unconditional jump as insn. */
1082*404b540aSrobert if (!simplejump_p (i2) && !returnjump_p (i2) && last1)
1083*404b540aSrobert ninsns++;
1084*404b540aSrobert i2 = PREV_INSN (i2);
1085*404b540aSrobert }
1086*404b540aSrobert
1087*404b540aSrobert while (true)
1088*404b540aSrobert {
1089*404b540aSrobert /* Ignore notes. */
1090*404b540aSrobert while (!INSN_P (i1) && i1 != BB_HEAD (bb1))
1091*404b540aSrobert i1 = PREV_INSN (i1);
1092*404b540aSrobert
1093*404b540aSrobert while (!INSN_P (i2) && i2 != BB_HEAD (bb2))
1094*404b540aSrobert i2 = PREV_INSN (i2);
1095*404b540aSrobert
1096*404b540aSrobert if (i1 == BB_HEAD (bb1) || i2 == BB_HEAD (bb2))
1097*404b540aSrobert break;
1098*404b540aSrobert
1099*404b540aSrobert if (!old_insns_match_p (mode, i1, i2))
1100*404b540aSrobert break;
1101*404b540aSrobert
1102*404b540aSrobert merge_memattrs (i1, i2);
1103*404b540aSrobert
1104*404b540aSrobert /* Don't begin a cross-jump with a NOTE insn. */
1105*404b540aSrobert if (INSN_P (i1))
1106*404b540aSrobert {
1107*404b540aSrobert /* If the merged insns have different REG_EQUAL notes, then
1108*404b540aSrobert remove them. */
1109*404b540aSrobert rtx equiv1 = find_reg_equal_equiv_note (i1);
1110*404b540aSrobert rtx equiv2 = find_reg_equal_equiv_note (i2);
1111*404b540aSrobert
1112*404b540aSrobert if (equiv1 && !equiv2)
1113*404b540aSrobert remove_note (i1, equiv1);
1114*404b540aSrobert else if (!equiv1 && equiv2)
1115*404b540aSrobert remove_note (i2, equiv2);
1116*404b540aSrobert else if (equiv1 && equiv2
1117*404b540aSrobert && !rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
1118*404b540aSrobert {
1119*404b540aSrobert remove_note (i1, equiv1);
1120*404b540aSrobert remove_note (i2, equiv2);
1121*404b540aSrobert }
1122*404b540aSrobert
1123*404b540aSrobert afterlast1 = last1, afterlast2 = last2;
1124*404b540aSrobert last1 = i1, last2 = i2;
1125*404b540aSrobert ninsns++;
1126*404b540aSrobert }
1127*404b540aSrobert
1128*404b540aSrobert i1 = PREV_INSN (i1);
1129*404b540aSrobert i2 = PREV_INSN (i2);
1130*404b540aSrobert }
1131*404b540aSrobert
1132*404b540aSrobert #ifdef HAVE_cc0
1133*404b540aSrobert /* Don't allow the insn after a compare to be shared by
1134*404b540aSrobert cross-jumping unless the compare is also shared. */
1135*404b540aSrobert if (ninsns && reg_mentioned_p (cc0_rtx, last1) && ! sets_cc0_p (last1))
1136*404b540aSrobert last1 = afterlast1, last2 = afterlast2, ninsns--;
1137*404b540aSrobert #endif
1138*404b540aSrobert
1139*404b540aSrobert /* Include preceding notes and labels in the cross-jump. One,
1140*404b540aSrobert this may bring us to the head of the blocks as requested above.
1141*404b540aSrobert Two, it keeps line number notes as matched as may be. */
1142*404b540aSrobert if (ninsns)
1143*404b540aSrobert {
1144*404b540aSrobert while (last1 != BB_HEAD (bb1) && !INSN_P (PREV_INSN (last1)))
1145*404b540aSrobert last1 = PREV_INSN (last1);
1146*404b540aSrobert
1147*404b540aSrobert if (last1 != BB_HEAD (bb1) && LABEL_P (PREV_INSN (last1)))
1148*404b540aSrobert last1 = PREV_INSN (last1);
1149*404b540aSrobert
1150*404b540aSrobert while (last2 != BB_HEAD (bb2) && !INSN_P (PREV_INSN (last2)))
1151*404b540aSrobert last2 = PREV_INSN (last2);
1152*404b540aSrobert
1153*404b540aSrobert if (last2 != BB_HEAD (bb2) && LABEL_P (PREV_INSN (last2)))
1154*404b540aSrobert last2 = PREV_INSN (last2);
1155*404b540aSrobert
1156*404b540aSrobert *f1 = last1;
1157*404b540aSrobert *f2 = last2;
1158*404b540aSrobert }
1159*404b540aSrobert
1160*404b540aSrobert return ninsns;
1161*404b540aSrobert }
1162*404b540aSrobert
1163*404b540aSrobert /* Return true iff the condbranches at the end of BB1 and BB2 match. */
1164*404b540aSrobert bool
condjump_equiv_p(struct equiv_info * info,bool call_init)1165*404b540aSrobert condjump_equiv_p (struct equiv_info *info, bool call_init)
1166*404b540aSrobert {
1167*404b540aSrobert basic_block bb1 = info->x_block;
1168*404b540aSrobert basic_block bb2 = info->y_block;
1169*404b540aSrobert edge b1 = BRANCH_EDGE (bb1);
1170*404b540aSrobert edge b2 = BRANCH_EDGE (bb2);
1171*404b540aSrobert edge f1 = FALLTHRU_EDGE (bb1);
1172*404b540aSrobert edge f2 = FALLTHRU_EDGE (bb2);
1173*404b540aSrobert bool reverse, match;
1174*404b540aSrobert rtx set1, set2, cond1, cond2;
1175*404b540aSrobert rtx src1, src2;
1176*404b540aSrobert enum rtx_code code1, code2;
1177*404b540aSrobert
1178*404b540aSrobert /* Get around possible forwarders on fallthru edges. Other cases
1179*404b540aSrobert should be optimized out already. */
1180*404b540aSrobert if (FORWARDER_BLOCK_P (f1->dest))
1181*404b540aSrobert f1 = single_succ_edge (f1->dest);
1182*404b540aSrobert
1183*404b540aSrobert if (FORWARDER_BLOCK_P (f2->dest))
1184*404b540aSrobert f2 = single_succ_edge (f2->dest);
1185*404b540aSrobert
1186*404b540aSrobert /* To simplify use of this function, return false if there are
1187*404b540aSrobert unneeded forwarder blocks. These will get eliminated later
1188*404b540aSrobert during cleanup_cfg. */
1189*404b540aSrobert if (FORWARDER_BLOCK_P (f1->dest)
1190*404b540aSrobert || FORWARDER_BLOCK_P (f2->dest)
1191*404b540aSrobert || FORWARDER_BLOCK_P (b1->dest)
1192*404b540aSrobert || FORWARDER_BLOCK_P (b2->dest))
1193*404b540aSrobert return false;
1194*404b540aSrobert
1195*404b540aSrobert if (f1->dest == f2->dest && b1->dest == b2->dest)
1196*404b540aSrobert reverse = false;
1197*404b540aSrobert else if (f1->dest == b2->dest && b1->dest == f2->dest)
1198*404b540aSrobert reverse = true;
1199*404b540aSrobert else
1200*404b540aSrobert return false;
1201*404b540aSrobert
1202*404b540aSrobert set1 = pc_set (BB_END (bb1));
1203*404b540aSrobert set2 = pc_set (BB_END (bb2));
1204*404b540aSrobert if ((XEXP (SET_SRC (set1), 1) == pc_rtx)
1205*404b540aSrobert != (XEXP (SET_SRC (set2), 1) == pc_rtx))
1206*404b540aSrobert reverse = !reverse;
1207*404b540aSrobert
1208*404b540aSrobert src1 = SET_SRC (set1);
1209*404b540aSrobert src2 = SET_SRC (set2);
1210*404b540aSrobert cond1 = XEXP (src1, 0);
1211*404b540aSrobert cond2 = XEXP (src2, 0);
1212*404b540aSrobert code1 = GET_CODE (cond1);
1213*404b540aSrobert if (reverse)
1214*404b540aSrobert code2 = reversed_comparison_code (cond2, BB_END (bb2));
1215*404b540aSrobert else
1216*404b540aSrobert code2 = GET_CODE (cond2);
1217*404b540aSrobert
1218*404b540aSrobert if (code2 == UNKNOWN)
1219*404b540aSrobert return false;
1220*404b540aSrobert
1221*404b540aSrobert if (call_init && !struct_equiv_init (STRUCT_EQUIV_START | info->mode, info))
1222*404b540aSrobert gcc_unreachable ();
1223*404b540aSrobert /* Make the sources of the pc sets unreadable so that when we call
1224*404b540aSrobert insns_match_p it won't process them.
1225*404b540aSrobert The death_notes_match_p from insns_match_p won't see the local registers
1226*404b540aSrobert used for the pc set, but that could only cause missed optimizations when
1227*404b540aSrobert there are actually condjumps that use stack registers. */
1228*404b540aSrobert SET_SRC (set1) = pc_rtx;
1229*404b540aSrobert SET_SRC (set2) = pc_rtx;
1230*404b540aSrobert /* Verify codes and operands match. */
1231*404b540aSrobert if (code1 == code2)
1232*404b540aSrobert {
1233*404b540aSrobert match = (insns_match_p (BB_END (bb1), BB_END (bb2), info)
1234*404b540aSrobert && rtx_equiv_p (&XEXP (cond1, 0), XEXP (cond2, 0), 1, info)
1235*404b540aSrobert && rtx_equiv_p (&XEXP (cond1, 1), XEXP (cond2, 1), 1, info));
1236*404b540aSrobert
1237*404b540aSrobert }
1238*404b540aSrobert else if (code1 == swap_condition (code2))
1239*404b540aSrobert {
1240*404b540aSrobert match = (insns_match_p (BB_END (bb1), BB_END (bb2), info)
1241*404b540aSrobert && rtx_equiv_p (&XEXP (cond1, 1), XEXP (cond2, 0), 1, info)
1242*404b540aSrobert && rtx_equiv_p (&XEXP (cond1, 0), XEXP (cond2, 1), 1, info));
1243*404b540aSrobert
1244*404b540aSrobert }
1245*404b540aSrobert else
1246*404b540aSrobert match = false;
1247*404b540aSrobert SET_SRC (set1) = src1;
1248*404b540aSrobert SET_SRC (set2) = src2;
1249*404b540aSrobert match &= verify_changes (0);
1250*404b540aSrobert
1251*404b540aSrobert /* If we return true, we will join the blocks. Which means that
1252*404b540aSrobert we will only have one branch prediction bit to work with. Thus
1253*404b540aSrobert we require the existing branches to have probabilities that are
1254*404b540aSrobert roughly similar. */
1255*404b540aSrobert if (match
1256*404b540aSrobert && !optimize_size
1257*404b540aSrobert && maybe_hot_bb_p (bb1)
1258*404b540aSrobert && maybe_hot_bb_p (bb2))
1259*404b540aSrobert {
1260*404b540aSrobert int prob2;
1261*404b540aSrobert
1262*404b540aSrobert if (b1->dest == b2->dest)
1263*404b540aSrobert prob2 = b2->probability;
1264*404b540aSrobert else
1265*404b540aSrobert /* Do not use f2 probability as f2 may be forwarded. */
1266*404b540aSrobert prob2 = REG_BR_PROB_BASE - b2->probability;
1267*404b540aSrobert
1268*404b540aSrobert /* Fail if the difference in probabilities is greater than 50%.
1269*404b540aSrobert This rules out two well-predicted branches with opposite
1270*404b540aSrobert outcomes. */
1271*404b540aSrobert if (abs (b1->probability - prob2) > REG_BR_PROB_BASE / 2)
1272*404b540aSrobert {
1273*404b540aSrobert if (dump_file)
1274*404b540aSrobert fprintf (dump_file,
1275*404b540aSrobert "Outcomes of branch in bb %i and %i differ too much (%i %i)\n",
1276*404b540aSrobert bb1->index, bb2->index, b1->probability, prob2);
1277*404b540aSrobert
1278*404b540aSrobert match = false;
1279*404b540aSrobert }
1280*404b540aSrobert }
1281*404b540aSrobert
1282*404b540aSrobert if (dump_file && match)
1283*404b540aSrobert fprintf (dump_file, "Conditionals in bb %i and %i match.\n",
1284*404b540aSrobert bb1->index, bb2->index);
1285*404b540aSrobert
1286*404b540aSrobert if (!match)
1287*404b540aSrobert cancel_changes (0);
1288*404b540aSrobert return match;
1289*404b540aSrobert }
1290*404b540aSrobert
1291*404b540aSrobert /* Return true iff outgoing edges of BB1 and BB2 match, together with
1292*404b540aSrobert the branch instruction. This means that if we commonize the control
1293*404b540aSrobert flow before end of the basic block, the semantic remains unchanged.
1294*404b540aSrobert
1295*404b540aSrobert We may assume that there exists one edge with a common destination. */
1296*404b540aSrobert
1297*404b540aSrobert static bool
outgoing_edges_match(int mode,basic_block bb1,basic_block bb2)1298*404b540aSrobert outgoing_edges_match (int mode, basic_block bb1, basic_block bb2)
1299*404b540aSrobert {
1300*404b540aSrobert int nehedges1 = 0, nehedges2 = 0;
1301*404b540aSrobert edge fallthru1 = 0, fallthru2 = 0;
1302*404b540aSrobert edge e1, e2;
1303*404b540aSrobert edge_iterator ei;
1304*404b540aSrobert
1305*404b540aSrobert /* If BB1 has only one successor, we may be looking at either an
1306*404b540aSrobert unconditional jump, or a fake edge to exit. */
1307*404b540aSrobert if (single_succ_p (bb1)
1308*404b540aSrobert && (single_succ_edge (bb1)->flags & (EDGE_COMPLEX | EDGE_FAKE)) == 0
1309*404b540aSrobert && (!JUMP_P (BB_END (bb1)) || simplejump_p (BB_END (bb1))))
1310*404b540aSrobert return (single_succ_p (bb2)
1311*404b540aSrobert && (single_succ_edge (bb2)->flags
1312*404b540aSrobert & (EDGE_COMPLEX | EDGE_FAKE)) == 0
1313*404b540aSrobert && (!JUMP_P (BB_END (bb2)) || simplejump_p (BB_END (bb2))));
1314*404b540aSrobert
1315*404b540aSrobert /* Match conditional jumps - this may get tricky when fallthru and branch
1316*404b540aSrobert edges are crossed. */
1317*404b540aSrobert if (EDGE_COUNT (bb1->succs) == 2
1318*404b540aSrobert && any_condjump_p (BB_END (bb1))
1319*404b540aSrobert && onlyjump_p (BB_END (bb1)))
1320*404b540aSrobert {
1321*404b540aSrobert edge b1, f1, b2, f2;
1322*404b540aSrobert bool reverse, match;
1323*404b540aSrobert rtx set1, set2, cond1, cond2;
1324*404b540aSrobert enum rtx_code code1, code2;
1325*404b540aSrobert
1326*404b540aSrobert if (EDGE_COUNT (bb2->succs) != 2
1327*404b540aSrobert || !any_condjump_p (BB_END (bb2))
1328*404b540aSrobert || !onlyjump_p (BB_END (bb2)))
1329*404b540aSrobert return false;
1330*404b540aSrobert
1331*404b540aSrobert b1 = BRANCH_EDGE (bb1);
1332*404b540aSrobert b2 = BRANCH_EDGE (bb2);
1333*404b540aSrobert f1 = FALLTHRU_EDGE (bb1);
1334*404b540aSrobert f2 = FALLTHRU_EDGE (bb2);
1335*404b540aSrobert
1336*404b540aSrobert /* Get around possible forwarders on fallthru edges. Other cases
1337*404b540aSrobert should be optimized out already. */
1338*404b540aSrobert if (FORWARDER_BLOCK_P (f1->dest))
1339*404b540aSrobert f1 = single_succ_edge (f1->dest);
1340*404b540aSrobert
1341*404b540aSrobert if (FORWARDER_BLOCK_P (f2->dest))
1342*404b540aSrobert f2 = single_succ_edge (f2->dest);
1343*404b540aSrobert
1344*404b540aSrobert /* To simplify use of this function, return false if there are
1345*404b540aSrobert unneeded forwarder blocks. These will get eliminated later
1346*404b540aSrobert during cleanup_cfg. */
1347*404b540aSrobert if (FORWARDER_BLOCK_P (f1->dest)
1348*404b540aSrobert || FORWARDER_BLOCK_P (f2->dest)
1349*404b540aSrobert || FORWARDER_BLOCK_P (b1->dest)
1350*404b540aSrobert || FORWARDER_BLOCK_P (b2->dest))
1351*404b540aSrobert return false;
1352*404b540aSrobert
1353*404b540aSrobert if (f1->dest == f2->dest && b1->dest == b2->dest)
1354*404b540aSrobert reverse = false;
1355*404b540aSrobert else if (f1->dest == b2->dest && b1->dest == f2->dest)
1356*404b540aSrobert reverse = true;
1357*404b540aSrobert else
1358*404b540aSrobert return false;
1359*404b540aSrobert
1360*404b540aSrobert set1 = pc_set (BB_END (bb1));
1361*404b540aSrobert set2 = pc_set (BB_END (bb2));
1362*404b540aSrobert if ((XEXP (SET_SRC (set1), 1) == pc_rtx)
1363*404b540aSrobert != (XEXP (SET_SRC (set2), 1) == pc_rtx))
1364*404b540aSrobert reverse = !reverse;
1365*404b540aSrobert
1366*404b540aSrobert cond1 = XEXP (SET_SRC (set1), 0);
1367*404b540aSrobert cond2 = XEXP (SET_SRC (set2), 0);
1368*404b540aSrobert code1 = GET_CODE (cond1);
1369*404b540aSrobert if (reverse)
1370*404b540aSrobert code2 = reversed_comparison_code (cond2, BB_END (bb2));
1371*404b540aSrobert else
1372*404b540aSrobert code2 = GET_CODE (cond2);
1373*404b540aSrobert
1374*404b540aSrobert if (code2 == UNKNOWN)
1375*404b540aSrobert return false;
1376*404b540aSrobert
1377*404b540aSrobert /* Verify codes and operands match. */
1378*404b540aSrobert match = ((code1 == code2
1379*404b540aSrobert && rtx_renumbered_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
1380*404b540aSrobert && rtx_renumbered_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
1381*404b540aSrobert || (code1 == swap_condition (code2)
1382*404b540aSrobert && rtx_renumbered_equal_p (XEXP (cond1, 1),
1383*404b540aSrobert XEXP (cond2, 0))
1384*404b540aSrobert && rtx_renumbered_equal_p (XEXP (cond1, 0),
1385*404b540aSrobert XEXP (cond2, 1))));
1386*404b540aSrobert
1387*404b540aSrobert /* If we return true, we will join the blocks. Which means that
1388*404b540aSrobert we will only have one branch prediction bit to work with. Thus
1389*404b540aSrobert we require the existing branches to have probabilities that are
1390*404b540aSrobert roughly similar. */
1391*404b540aSrobert if (match
1392*404b540aSrobert && !optimize_size
1393*404b540aSrobert && maybe_hot_bb_p (bb1)
1394*404b540aSrobert && maybe_hot_bb_p (bb2))
1395*404b540aSrobert {
1396*404b540aSrobert int prob2;
1397*404b540aSrobert
1398*404b540aSrobert if (b1->dest == b2->dest)
1399*404b540aSrobert prob2 = b2->probability;
1400*404b540aSrobert else
1401*404b540aSrobert /* Do not use f2 probability as f2 may be forwarded. */
1402*404b540aSrobert prob2 = REG_BR_PROB_BASE - b2->probability;
1403*404b540aSrobert
1404*404b540aSrobert /* Fail if the difference in probabilities is greater than 50%.
1405*404b540aSrobert This rules out two well-predicted branches with opposite
1406*404b540aSrobert outcomes. */
1407*404b540aSrobert if (abs (b1->probability - prob2) > REG_BR_PROB_BASE / 2)
1408*404b540aSrobert {
1409*404b540aSrobert if (dump_file)
1410*404b540aSrobert fprintf (dump_file,
1411*404b540aSrobert "Outcomes of branch in bb %i and %i differ too much (%i %i)\n",
1412*404b540aSrobert bb1->index, bb2->index, b1->probability, prob2);
1413*404b540aSrobert
1414*404b540aSrobert return false;
1415*404b540aSrobert }
1416*404b540aSrobert }
1417*404b540aSrobert
1418*404b540aSrobert if (dump_file && match)
1419*404b540aSrobert fprintf (dump_file, "Conditionals in bb %i and %i match.\n",
1420*404b540aSrobert bb1->index, bb2->index);
1421*404b540aSrobert
1422*404b540aSrobert return match;
1423*404b540aSrobert }
1424*404b540aSrobert
1425*404b540aSrobert /* Generic case - we are seeing a computed jump, table jump or trapping
1426*404b540aSrobert instruction. */
1427*404b540aSrobert
1428*404b540aSrobert /* Check whether there are tablejumps in the end of BB1 and BB2.
1429*404b540aSrobert Return true if they are identical. */
1430*404b540aSrobert {
1431*404b540aSrobert rtx label1, label2;
1432*404b540aSrobert rtx table1, table2;
1433*404b540aSrobert
1434*404b540aSrobert if (tablejump_p (BB_END (bb1), &label1, &table1)
1435*404b540aSrobert && tablejump_p (BB_END (bb2), &label2, &table2)
1436*404b540aSrobert && GET_CODE (PATTERN (table1)) == GET_CODE (PATTERN (table2)))
1437*404b540aSrobert {
1438*404b540aSrobert /* The labels should never be the same rtx. If they really are same
1439*404b540aSrobert the jump tables are same too. So disable crossjumping of blocks BB1
1440*404b540aSrobert and BB2 because when deleting the common insns in the end of BB1
1441*404b540aSrobert by delete_basic_block () the jump table would be deleted too. */
1442*404b540aSrobert /* If LABEL2 is referenced in BB1->END do not do anything
1443*404b540aSrobert because we would loose information when replacing
1444*404b540aSrobert LABEL1 by LABEL2 and then LABEL2 by LABEL1 in BB1->END. */
1445*404b540aSrobert if (label1 != label2 && !rtx_referenced_p (label2, BB_END (bb1)))
1446*404b540aSrobert {
1447*404b540aSrobert /* Set IDENTICAL to true when the tables are identical. */
1448*404b540aSrobert bool identical = false;
1449*404b540aSrobert rtx p1, p2;
1450*404b540aSrobert
1451*404b540aSrobert p1 = PATTERN (table1);
1452*404b540aSrobert p2 = PATTERN (table2);
1453*404b540aSrobert if (GET_CODE (p1) == ADDR_VEC && rtx_equal_p (p1, p2))
1454*404b540aSrobert {
1455*404b540aSrobert identical = true;
1456*404b540aSrobert }
1457*404b540aSrobert else if (GET_CODE (p1) == ADDR_DIFF_VEC
1458*404b540aSrobert && (XVECLEN (p1, 1) == XVECLEN (p2, 1))
1459*404b540aSrobert && rtx_equal_p (XEXP (p1, 2), XEXP (p2, 2))
1460*404b540aSrobert && rtx_equal_p (XEXP (p1, 3), XEXP (p2, 3)))
1461*404b540aSrobert {
1462*404b540aSrobert int i;
1463*404b540aSrobert
1464*404b540aSrobert identical = true;
1465*404b540aSrobert for (i = XVECLEN (p1, 1) - 1; i >= 0 && identical; i--)
1466*404b540aSrobert if (!rtx_equal_p (XVECEXP (p1, 1, i), XVECEXP (p2, 1, i)))
1467*404b540aSrobert identical = false;
1468*404b540aSrobert }
1469*404b540aSrobert
1470*404b540aSrobert if (identical)
1471*404b540aSrobert {
1472*404b540aSrobert replace_label_data rr;
1473*404b540aSrobert bool match;
1474*404b540aSrobert
1475*404b540aSrobert /* Temporarily replace references to LABEL1 with LABEL2
1476*404b540aSrobert in BB1->END so that we could compare the instructions. */
1477*404b540aSrobert rr.r1 = label1;
1478*404b540aSrobert rr.r2 = label2;
1479*404b540aSrobert rr.update_label_nuses = false;
1480*404b540aSrobert for_each_rtx (&BB_END (bb1), replace_label, &rr);
1481*404b540aSrobert
1482*404b540aSrobert match = old_insns_match_p (mode, BB_END (bb1), BB_END (bb2));
1483*404b540aSrobert if (dump_file && match)
1484*404b540aSrobert fprintf (dump_file,
1485*404b540aSrobert "Tablejumps in bb %i and %i match.\n",
1486*404b540aSrobert bb1->index, bb2->index);
1487*404b540aSrobert
1488*404b540aSrobert /* Set the original label in BB1->END because when deleting
1489*404b540aSrobert a block whose end is a tablejump, the tablejump referenced
1490*404b540aSrobert from the instruction is deleted too. */
1491*404b540aSrobert rr.r1 = label2;
1492*404b540aSrobert rr.r2 = label1;
1493*404b540aSrobert for_each_rtx (&BB_END (bb1), replace_label, &rr);
1494*404b540aSrobert
1495*404b540aSrobert return match;
1496*404b540aSrobert }
1497*404b540aSrobert }
1498*404b540aSrobert return false;
1499*404b540aSrobert }
1500*404b540aSrobert }
1501*404b540aSrobert
1502*404b540aSrobert /* First ensure that the instructions match. There may be many outgoing
1503*404b540aSrobert edges so this test is generally cheaper. */
1504*404b540aSrobert if (!old_insns_match_p (mode, BB_END (bb1), BB_END (bb2)))
1505*404b540aSrobert return false;
1506*404b540aSrobert
1507*404b540aSrobert /* Search the outgoing edges, ensure that the counts do match, find possible
1508*404b540aSrobert fallthru and exception handling edges since these needs more
1509*404b540aSrobert validation. */
1510*404b540aSrobert if (EDGE_COUNT (bb1->succs) != EDGE_COUNT (bb2->succs))
1511*404b540aSrobert return false;
1512*404b540aSrobert
1513*404b540aSrobert FOR_EACH_EDGE (e1, ei, bb1->succs)
1514*404b540aSrobert {
1515*404b540aSrobert e2 = EDGE_SUCC (bb2, ei.index);
1516*404b540aSrobert
1517*404b540aSrobert if (e1->flags & EDGE_EH)
1518*404b540aSrobert nehedges1++;
1519*404b540aSrobert
1520*404b540aSrobert if (e2->flags & EDGE_EH)
1521*404b540aSrobert nehedges2++;
1522*404b540aSrobert
1523*404b540aSrobert if (e1->flags & EDGE_FALLTHRU)
1524*404b540aSrobert fallthru1 = e1;
1525*404b540aSrobert if (e2->flags & EDGE_FALLTHRU)
1526*404b540aSrobert fallthru2 = e2;
1527*404b540aSrobert }
1528*404b540aSrobert
1529*404b540aSrobert /* If number of edges of various types does not match, fail. */
1530*404b540aSrobert if (nehedges1 != nehedges2
1531*404b540aSrobert || (fallthru1 != 0) != (fallthru2 != 0))
1532*404b540aSrobert return false;
1533*404b540aSrobert
1534*404b540aSrobert /* fallthru edges must be forwarded to the same destination. */
1535*404b540aSrobert if (fallthru1)
1536*404b540aSrobert {
1537*404b540aSrobert basic_block d1 = (forwarder_block_p (fallthru1->dest)
1538*404b540aSrobert ? single_succ (fallthru1->dest): fallthru1->dest);
1539*404b540aSrobert basic_block d2 = (forwarder_block_p (fallthru2->dest)
1540*404b540aSrobert ? single_succ (fallthru2->dest): fallthru2->dest);
1541*404b540aSrobert
1542*404b540aSrobert if (d1 != d2)
1543*404b540aSrobert return false;
1544*404b540aSrobert }
1545*404b540aSrobert
1546*404b540aSrobert /* Ensure the same EH region. */
1547*404b540aSrobert {
1548*404b540aSrobert rtx n1 = find_reg_note (BB_END (bb1), REG_EH_REGION, 0);
1549*404b540aSrobert rtx n2 = find_reg_note (BB_END (bb2), REG_EH_REGION, 0);
1550*404b540aSrobert
1551*404b540aSrobert if (!n1 && n2)
1552*404b540aSrobert return false;
1553*404b540aSrobert
1554*404b540aSrobert if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
1555*404b540aSrobert return false;
1556*404b540aSrobert }
1557*404b540aSrobert
1558*404b540aSrobert /* The same checks as in try_crossjump_to_edge. It is required for RTL
1559*404b540aSrobert version of sequence abstraction. */
1560*404b540aSrobert FOR_EACH_EDGE (e1, ei, bb2->succs)
1561*404b540aSrobert {
1562*404b540aSrobert edge e2;
1563*404b540aSrobert edge_iterator ei;
1564*404b540aSrobert basic_block d1 = e1->dest;
1565*404b540aSrobert
1566*404b540aSrobert if (FORWARDER_BLOCK_P (d1))
1567*404b540aSrobert d1 = EDGE_SUCC (d1, 0)->dest;
1568*404b540aSrobert
1569*404b540aSrobert FOR_EACH_EDGE (e2, ei, bb1->succs)
1570*404b540aSrobert {
1571*404b540aSrobert basic_block d2 = e2->dest;
1572*404b540aSrobert if (FORWARDER_BLOCK_P (d2))
1573*404b540aSrobert d2 = EDGE_SUCC (d2, 0)->dest;
1574*404b540aSrobert if (d1 == d2)
1575*404b540aSrobert break;
1576*404b540aSrobert }
1577*404b540aSrobert
1578*404b540aSrobert if (!e2)
1579*404b540aSrobert return false;
1580*404b540aSrobert }
1581*404b540aSrobert
1582*404b540aSrobert return true;
1583*404b540aSrobert }
1584*404b540aSrobert
1585*404b540aSrobert /* Returns true if BB basic block has a preserve label. */
1586*404b540aSrobert
1587*404b540aSrobert static bool
block_has_preserve_label(basic_block bb)1588*404b540aSrobert block_has_preserve_label (basic_block bb)
1589*404b540aSrobert {
1590*404b540aSrobert return (bb
1591*404b540aSrobert && block_label (bb)
1592*404b540aSrobert && LABEL_PRESERVE_P (block_label (bb)));
1593*404b540aSrobert }
1594*404b540aSrobert
1595*404b540aSrobert /* E1 and E2 are edges with the same destination block. Search their
1596*404b540aSrobert predecessors for common code. If found, redirect control flow from
1597*404b540aSrobert (maybe the middle of) E1->SRC to (maybe the middle of) E2->SRC. */
1598*404b540aSrobert
1599*404b540aSrobert static bool
try_crossjump_to_edge(int mode,edge e1,edge e2)1600*404b540aSrobert try_crossjump_to_edge (int mode, edge e1, edge e2)
1601*404b540aSrobert {
1602*404b540aSrobert int nmatch;
1603*404b540aSrobert basic_block src1 = e1->src, src2 = e2->src;
1604*404b540aSrobert basic_block redirect_to, redirect_from, to_remove;
1605*404b540aSrobert rtx newpos1, newpos2;
1606*404b540aSrobert edge s;
1607*404b540aSrobert edge_iterator ei;
1608*404b540aSrobert
1609*404b540aSrobert newpos1 = newpos2 = NULL_RTX;
1610*404b540aSrobert
1611*404b540aSrobert /* If we have partitioned hot/cold basic blocks, it is a bad idea
1612*404b540aSrobert to try this optimization.
1613*404b540aSrobert
1614*404b540aSrobert Basic block partitioning may result in some jumps that appear to
1615*404b540aSrobert be optimizable (or blocks that appear to be mergeable), but which really
1616*404b540aSrobert must be left untouched (they are required to make it safely across
1617*404b540aSrobert partition boundaries). See the comments at the top of
1618*404b540aSrobert bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
1619*404b540aSrobert
1620*404b540aSrobert if (flag_reorder_blocks_and_partition && no_new_pseudos)
1621*404b540aSrobert return false;
1622*404b540aSrobert
1623*404b540aSrobert /* Search backward through forwarder blocks. We don't need to worry
1624*404b540aSrobert about multiple entry or chained forwarders, as they will be optimized
1625*404b540aSrobert away. We do this to look past the unconditional jump following a
1626*404b540aSrobert conditional jump that is required due to the current CFG shape. */
1627*404b540aSrobert if (single_pred_p (src1)
1628*404b540aSrobert && FORWARDER_BLOCK_P (src1))
1629*404b540aSrobert e1 = single_pred_edge (src1), src1 = e1->src;
1630*404b540aSrobert
1631*404b540aSrobert if (single_pred_p (src2)
1632*404b540aSrobert && FORWARDER_BLOCK_P (src2))
1633*404b540aSrobert e2 = single_pred_edge (src2), src2 = e2->src;
1634*404b540aSrobert
1635*404b540aSrobert /* Nothing to do if we reach ENTRY, or a common source block. */
1636*404b540aSrobert if (src1 == ENTRY_BLOCK_PTR || src2 == ENTRY_BLOCK_PTR)
1637*404b540aSrobert return false;
1638*404b540aSrobert if (src1 == src2)
1639*404b540aSrobert return false;
1640*404b540aSrobert
1641*404b540aSrobert /* Seeing more than 1 forwarder blocks would confuse us later... */
1642*404b540aSrobert if (FORWARDER_BLOCK_P (e1->dest)
1643*404b540aSrobert && FORWARDER_BLOCK_P (single_succ (e1->dest)))
1644*404b540aSrobert return false;
1645*404b540aSrobert
1646*404b540aSrobert if (FORWARDER_BLOCK_P (e2->dest)
1647*404b540aSrobert && FORWARDER_BLOCK_P (single_succ (e2->dest)))
1648*404b540aSrobert return false;
1649*404b540aSrobert
1650*404b540aSrobert /* Likewise with dead code (possibly newly created by the other optimizations
1651*404b540aSrobert of cfg_cleanup). */
1652*404b540aSrobert if (EDGE_COUNT (src1->preds) == 0 || EDGE_COUNT (src2->preds) == 0)
1653*404b540aSrobert return false;
1654*404b540aSrobert
1655*404b540aSrobert /* Look for the common insn sequence, part the first ... */
1656*404b540aSrobert if (!outgoing_edges_match (mode, src1, src2))
1657*404b540aSrobert return false;
1658*404b540aSrobert
1659*404b540aSrobert /* ... and part the second. */
1660*404b540aSrobert nmatch = flow_find_cross_jump (mode, src1, src2, &newpos1, &newpos2);
1661*404b540aSrobert
1662*404b540aSrobert /* Don't proceed with the crossjump unless we found a sufficient number
1663*404b540aSrobert of matching instructions or the 'from' block was totally matched
1664*404b540aSrobert (such that its predecessors will hopefully be redirected and the
1665*404b540aSrobert block removed). */
1666*404b540aSrobert if ((nmatch < PARAM_VALUE (PARAM_MIN_CROSSJUMP_INSNS))
1667*404b540aSrobert && (newpos1 != BB_HEAD (src1)))
1668*404b540aSrobert return false;
1669*404b540aSrobert
1670*404b540aSrobert /* Avoid deleting preserve label when redirecting ABNORMAL edges. */
1671*404b540aSrobert if (block_has_preserve_label (e1->dest)
1672*404b540aSrobert && (e1->flags & EDGE_ABNORMAL))
1673*404b540aSrobert return false;
1674*404b540aSrobert
1675*404b540aSrobert /* Here we know that the insns in the end of SRC1 which are common with SRC2
1676*404b540aSrobert will be deleted.
1677*404b540aSrobert If we have tablejumps in the end of SRC1 and SRC2
1678*404b540aSrobert they have been already compared for equivalence in outgoing_edges_match ()
1679*404b540aSrobert so replace the references to TABLE1 by references to TABLE2. */
1680*404b540aSrobert {
1681*404b540aSrobert rtx label1, label2;
1682*404b540aSrobert rtx table1, table2;
1683*404b540aSrobert
1684*404b540aSrobert if (tablejump_p (BB_END (src1), &label1, &table1)
1685*404b540aSrobert && tablejump_p (BB_END (src2), &label2, &table2)
1686*404b540aSrobert && label1 != label2)
1687*404b540aSrobert {
1688*404b540aSrobert replace_label_data rr;
1689*404b540aSrobert rtx insn;
1690*404b540aSrobert
1691*404b540aSrobert /* Replace references to LABEL1 with LABEL2. */
1692*404b540aSrobert rr.r1 = label1;
1693*404b540aSrobert rr.r2 = label2;
1694*404b540aSrobert rr.update_label_nuses = true;
1695*404b540aSrobert for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1696*404b540aSrobert {
1697*404b540aSrobert /* Do not replace the label in SRC1->END because when deleting
1698*404b540aSrobert a block whose end is a tablejump, the tablejump referenced
1699*404b540aSrobert from the instruction is deleted too. */
1700*404b540aSrobert if (insn != BB_END (src1))
1701*404b540aSrobert for_each_rtx (&insn, replace_label, &rr);
1702*404b540aSrobert }
1703*404b540aSrobert }
1704*404b540aSrobert }
1705*404b540aSrobert
1706*404b540aSrobert /* Avoid splitting if possible. We must always split when SRC2 has
1707*404b540aSrobert EH predecessor edges, or we may end up with basic blocks with both
1708*404b540aSrobert normal and EH predecessor edges. */
1709*404b540aSrobert if (newpos2 == BB_HEAD (src2)
1710*404b540aSrobert && !(EDGE_PRED (src2, 0)->flags & EDGE_EH))
1711*404b540aSrobert redirect_to = src2;
1712*404b540aSrobert else
1713*404b540aSrobert {
1714*404b540aSrobert if (newpos2 == BB_HEAD (src2))
1715*404b540aSrobert {
1716*404b540aSrobert /* Skip possible basic block header. */
1717*404b540aSrobert if (LABEL_P (newpos2))
1718*404b540aSrobert newpos2 = NEXT_INSN (newpos2);
1719*404b540aSrobert if (NOTE_P (newpos2))
1720*404b540aSrobert newpos2 = NEXT_INSN (newpos2);
1721*404b540aSrobert }
1722*404b540aSrobert
1723*404b540aSrobert if (dump_file)
1724*404b540aSrobert fprintf (dump_file, "Splitting bb %i before %i insns\n",
1725*404b540aSrobert src2->index, nmatch);
1726*404b540aSrobert redirect_to = split_block (src2, PREV_INSN (newpos2))->dest;
1727*404b540aSrobert }
1728*404b540aSrobert
1729*404b540aSrobert if (dump_file)
1730*404b540aSrobert fprintf (dump_file,
1731*404b540aSrobert "Cross jumping from bb %i to bb %i; %i common insns\n",
1732*404b540aSrobert src1->index, src2->index, nmatch);
1733*404b540aSrobert
1734*404b540aSrobert redirect_to->count += src1->count;
1735*404b540aSrobert redirect_to->frequency += src1->frequency;
1736*404b540aSrobert /* We may have some registers visible through the block. */
1737*404b540aSrobert redirect_to->flags |= BB_DIRTY;
1738*404b540aSrobert
1739*404b540aSrobert /* Recompute the frequencies and counts of outgoing edges. */
1740*404b540aSrobert FOR_EACH_EDGE (s, ei, redirect_to->succs)
1741*404b540aSrobert {
1742*404b540aSrobert edge s2;
1743*404b540aSrobert edge_iterator ei;
1744*404b540aSrobert basic_block d = s->dest;
1745*404b540aSrobert
1746*404b540aSrobert if (FORWARDER_BLOCK_P (d))
1747*404b540aSrobert d = single_succ (d);
1748*404b540aSrobert
1749*404b540aSrobert FOR_EACH_EDGE (s2, ei, src1->succs)
1750*404b540aSrobert {
1751*404b540aSrobert basic_block d2 = s2->dest;
1752*404b540aSrobert if (FORWARDER_BLOCK_P (d2))
1753*404b540aSrobert d2 = single_succ (d2);
1754*404b540aSrobert if (d == d2)
1755*404b540aSrobert break;
1756*404b540aSrobert }
1757*404b540aSrobert
1758*404b540aSrobert s->count += s2->count;
1759*404b540aSrobert
1760*404b540aSrobert /* Take care to update possible forwarder blocks. We verified
1761*404b540aSrobert that there is no more than one in the chain, so we can't run
1762*404b540aSrobert into infinite loop. */
1763*404b540aSrobert if (FORWARDER_BLOCK_P (s->dest))
1764*404b540aSrobert {
1765*404b540aSrobert single_succ_edge (s->dest)->count += s2->count;
1766*404b540aSrobert s->dest->count += s2->count;
1767*404b540aSrobert s->dest->frequency += EDGE_FREQUENCY (s);
1768*404b540aSrobert }
1769*404b540aSrobert
1770*404b540aSrobert if (FORWARDER_BLOCK_P (s2->dest))
1771*404b540aSrobert {
1772*404b540aSrobert single_succ_edge (s2->dest)->count -= s2->count;
1773*404b540aSrobert if (single_succ_edge (s2->dest)->count < 0)
1774*404b540aSrobert single_succ_edge (s2->dest)->count = 0;
1775*404b540aSrobert s2->dest->count -= s2->count;
1776*404b540aSrobert s2->dest->frequency -= EDGE_FREQUENCY (s);
1777*404b540aSrobert if (s2->dest->frequency < 0)
1778*404b540aSrobert s2->dest->frequency = 0;
1779*404b540aSrobert if (s2->dest->count < 0)
1780*404b540aSrobert s2->dest->count = 0;
1781*404b540aSrobert }
1782*404b540aSrobert
1783*404b540aSrobert if (!redirect_to->frequency && !src1->frequency)
1784*404b540aSrobert s->probability = (s->probability + s2->probability) / 2;
1785*404b540aSrobert else
1786*404b540aSrobert s->probability
1787*404b540aSrobert = ((s->probability * redirect_to->frequency +
1788*404b540aSrobert s2->probability * src1->frequency)
1789*404b540aSrobert / (redirect_to->frequency + src1->frequency));
1790*404b540aSrobert }
1791*404b540aSrobert
1792*404b540aSrobert update_br_prob_note (redirect_to);
1793*404b540aSrobert
1794*404b540aSrobert /* Edit SRC1 to go to REDIRECT_TO at NEWPOS1. */
1795*404b540aSrobert
1796*404b540aSrobert /* Skip possible basic block header. */
1797*404b540aSrobert if (LABEL_P (newpos1))
1798*404b540aSrobert newpos1 = NEXT_INSN (newpos1);
1799*404b540aSrobert
1800*404b540aSrobert if (NOTE_P (newpos1))
1801*404b540aSrobert newpos1 = NEXT_INSN (newpos1);
1802*404b540aSrobert
1803*404b540aSrobert redirect_from = split_block (src1, PREV_INSN (newpos1))->src;
1804*404b540aSrobert to_remove = single_succ (redirect_from);
1805*404b540aSrobert
1806*404b540aSrobert redirect_edge_and_branch_force (single_succ_edge (redirect_from), redirect_to);
1807*404b540aSrobert delete_basic_block (to_remove);
1808*404b540aSrobert
1809*404b540aSrobert update_forwarder_flag (redirect_from);
1810*404b540aSrobert if (redirect_to != src2)
1811*404b540aSrobert update_forwarder_flag (src2);
1812*404b540aSrobert
1813*404b540aSrobert return true;
1814*404b540aSrobert }
1815*404b540aSrobert
1816*404b540aSrobert /* Search the predecessors of BB for common insn sequences. When found,
1817*404b540aSrobert share code between them by redirecting control flow. Return true if
1818*404b540aSrobert any changes made. */
1819*404b540aSrobert
1820*404b540aSrobert static bool
try_crossjump_bb(int mode,basic_block bb)1821*404b540aSrobert try_crossjump_bb (int mode, basic_block bb)
1822*404b540aSrobert {
1823*404b540aSrobert edge e, e2, fallthru;
1824*404b540aSrobert bool changed;
1825*404b540aSrobert unsigned max, ix, ix2;
1826*404b540aSrobert basic_block ev, ev2;
1827*404b540aSrobert edge_iterator ei;
1828*404b540aSrobert
1829*404b540aSrobert /* Nothing to do if there is not at least two incoming edges. */
1830*404b540aSrobert if (EDGE_COUNT (bb->preds) < 2)
1831*404b540aSrobert return false;
1832*404b540aSrobert
1833*404b540aSrobert /* Don't crossjump if this block ends in a computed jump,
1834*404b540aSrobert unless we are optimizing for size. */
1835*404b540aSrobert if (!optimize_size
1836*404b540aSrobert && bb != EXIT_BLOCK_PTR
1837*404b540aSrobert && computed_jump_p (BB_END (bb)))
1838*404b540aSrobert return false;
1839*404b540aSrobert
1840*404b540aSrobert /* If we are partitioning hot/cold basic blocks, we don't want to
1841*404b540aSrobert mess up unconditional or indirect jumps that cross between hot
1842*404b540aSrobert and cold sections.
1843*404b540aSrobert
1844*404b540aSrobert Basic block partitioning may result in some jumps that appear to
1845*404b540aSrobert be optimizable (or blocks that appear to be mergeable), but which really
1846*404b540aSrobert must be left untouched (they are required to make it safely across
1847*404b540aSrobert partition boundaries). See the comments at the top of
1848*404b540aSrobert bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
1849*404b540aSrobert
1850*404b540aSrobert if (BB_PARTITION (EDGE_PRED (bb, 0)->src) !=
1851*404b540aSrobert BB_PARTITION (EDGE_PRED (bb, 1)->src)
1852*404b540aSrobert || (EDGE_PRED (bb, 0)->flags & EDGE_CROSSING))
1853*404b540aSrobert return false;
1854*404b540aSrobert
1855*404b540aSrobert /* It is always cheapest to redirect a block that ends in a branch to
1856*404b540aSrobert a block that falls through into BB, as that adds no branches to the
1857*404b540aSrobert program. We'll try that combination first. */
1858*404b540aSrobert fallthru = NULL;
1859*404b540aSrobert max = PARAM_VALUE (PARAM_MAX_CROSSJUMP_EDGES);
1860*404b540aSrobert
1861*404b540aSrobert if (EDGE_COUNT (bb->preds) > max)
1862*404b540aSrobert return false;
1863*404b540aSrobert
1864*404b540aSrobert FOR_EACH_EDGE (e, ei, bb->preds)
1865*404b540aSrobert {
1866*404b540aSrobert if (e->flags & EDGE_FALLTHRU)
1867*404b540aSrobert fallthru = e;
1868*404b540aSrobert }
1869*404b540aSrobert
1870*404b540aSrobert changed = false;
1871*404b540aSrobert for (ix = 0, ev = bb; ix < EDGE_COUNT (ev->preds); )
1872*404b540aSrobert {
1873*404b540aSrobert e = EDGE_PRED (ev, ix);
1874*404b540aSrobert ix++;
1875*404b540aSrobert
1876*404b540aSrobert /* As noted above, first try with the fallthru predecessor. */
1877*404b540aSrobert if (fallthru)
1878*404b540aSrobert {
1879*404b540aSrobert /* Don't combine the fallthru edge into anything else.
1880*404b540aSrobert If there is a match, we'll do it the other way around. */
1881*404b540aSrobert if (e == fallthru)
1882*404b540aSrobert continue;
1883*404b540aSrobert /* If nothing changed since the last attempt, there is nothing
1884*404b540aSrobert we can do. */
1885*404b540aSrobert if (!first_pass
1886*404b540aSrobert && (!(e->src->flags & BB_DIRTY)
1887*404b540aSrobert && !(fallthru->src->flags & BB_DIRTY)))
1888*404b540aSrobert continue;
1889*404b540aSrobert
1890*404b540aSrobert if (try_crossjump_to_edge (mode, e, fallthru))
1891*404b540aSrobert {
1892*404b540aSrobert changed = true;
1893*404b540aSrobert ix = 0;
1894*404b540aSrobert ev = bb;
1895*404b540aSrobert continue;
1896*404b540aSrobert }
1897*404b540aSrobert }
1898*404b540aSrobert
1899*404b540aSrobert /* Non-obvious work limiting check: Recognize that we're going
1900*404b540aSrobert to call try_crossjump_bb on every basic block. So if we have
1901*404b540aSrobert two blocks with lots of outgoing edges (a switch) and they
1902*404b540aSrobert share lots of common destinations, then we would do the
1903*404b540aSrobert cross-jump check once for each common destination.
1904*404b540aSrobert
1905*404b540aSrobert Now, if the blocks actually are cross-jump candidates, then
1906*404b540aSrobert all of their destinations will be shared. Which means that
1907*404b540aSrobert we only need check them for cross-jump candidacy once. We
1908*404b540aSrobert can eliminate redundant checks of crossjump(A,B) by arbitrarily
1909*404b540aSrobert choosing to do the check from the block for which the edge
1910*404b540aSrobert in question is the first successor of A. */
1911*404b540aSrobert if (EDGE_SUCC (e->src, 0) != e)
1912*404b540aSrobert continue;
1913*404b540aSrobert
1914*404b540aSrobert for (ix2 = 0, ev2 = bb; ix2 < EDGE_COUNT (ev2->preds); )
1915*404b540aSrobert {
1916*404b540aSrobert e2 = EDGE_PRED (ev2, ix2);
1917*404b540aSrobert ix2++;
1918*404b540aSrobert
1919*404b540aSrobert if (e2 == e)
1920*404b540aSrobert continue;
1921*404b540aSrobert
1922*404b540aSrobert /* We've already checked the fallthru edge above. */
1923*404b540aSrobert if (e2 == fallthru)
1924*404b540aSrobert continue;
1925*404b540aSrobert
1926*404b540aSrobert /* The "first successor" check above only prevents multiple
1927*404b540aSrobert checks of crossjump(A,B). In order to prevent redundant
1928*404b540aSrobert checks of crossjump(B,A), require that A be the block
1929*404b540aSrobert with the lowest index. */
1930*404b540aSrobert if (e->src->index > e2->src->index)
1931*404b540aSrobert continue;
1932*404b540aSrobert
1933*404b540aSrobert /* If nothing changed since the last attempt, there is nothing
1934*404b540aSrobert we can do. */
1935*404b540aSrobert if (!first_pass
1936*404b540aSrobert && (!(e->src->flags & BB_DIRTY)
1937*404b540aSrobert && !(e2->src->flags & BB_DIRTY)))
1938*404b540aSrobert continue;
1939*404b540aSrobert
1940*404b540aSrobert if (try_crossjump_to_edge (mode, e, e2))
1941*404b540aSrobert {
1942*404b540aSrobert changed = true;
1943*404b540aSrobert ev2 = bb;
1944*404b540aSrobert ix = 0;
1945*404b540aSrobert break;
1946*404b540aSrobert }
1947*404b540aSrobert }
1948*404b540aSrobert }
1949*404b540aSrobert
1950*404b540aSrobert return changed;
1951*404b540aSrobert }
1952*404b540aSrobert
1953*404b540aSrobert /* Do simple CFG optimizations - basic block merging, simplifying of jump
1954*404b540aSrobert instructions etc. Return nonzero if changes were made. */
1955*404b540aSrobert
1956*404b540aSrobert static bool
try_optimize_cfg(int mode)1957*404b540aSrobert try_optimize_cfg (int mode)
1958*404b540aSrobert {
1959*404b540aSrobert bool changed_overall = false;
1960*404b540aSrobert bool changed;
1961*404b540aSrobert int iterations = 0;
1962*404b540aSrobert basic_block bb, b, next;
1963*404b540aSrobert
1964*404b540aSrobert if (mode & CLEANUP_CROSSJUMP)
1965*404b540aSrobert add_noreturn_fake_exit_edges ();
1966*404b540aSrobert
1967*404b540aSrobert if (mode & (CLEANUP_UPDATE_LIFE | CLEANUP_CROSSJUMP | CLEANUP_THREADING))
1968*404b540aSrobert clear_bb_flags ();
1969*404b540aSrobert
1970*404b540aSrobert FOR_EACH_BB (bb)
1971*404b540aSrobert update_forwarder_flag (bb);
1972*404b540aSrobert
1973*404b540aSrobert if (! targetm.cannot_modify_jumps_p ())
1974*404b540aSrobert {
1975*404b540aSrobert first_pass = true;
1976*404b540aSrobert /* Attempt to merge blocks as made possible by edge removal. If
1977*404b540aSrobert a block has only one successor, and the successor has only
1978*404b540aSrobert one predecessor, they may be combined. */
1979*404b540aSrobert do
1980*404b540aSrobert {
1981*404b540aSrobert changed = false;
1982*404b540aSrobert iterations++;
1983*404b540aSrobert
1984*404b540aSrobert if (dump_file)
1985*404b540aSrobert fprintf (dump_file,
1986*404b540aSrobert "\n\ntry_optimize_cfg iteration %i\n\n",
1987*404b540aSrobert iterations);
1988*404b540aSrobert
1989*404b540aSrobert for (b = ENTRY_BLOCK_PTR->next_bb; b != EXIT_BLOCK_PTR;)
1990*404b540aSrobert {
1991*404b540aSrobert basic_block c;
1992*404b540aSrobert edge s;
1993*404b540aSrobert bool changed_here = false;
1994*404b540aSrobert
1995*404b540aSrobert /* Delete trivially dead basic blocks. */
1996*404b540aSrobert while (EDGE_COUNT (b->preds) == 0)
1997*404b540aSrobert {
1998*404b540aSrobert c = b->prev_bb;
1999*404b540aSrobert if (dump_file)
2000*404b540aSrobert fprintf (dump_file, "Deleting block %i.\n",
2001*404b540aSrobert b->index);
2002*404b540aSrobert
2003*404b540aSrobert delete_basic_block (b);
2004*404b540aSrobert if (!(mode & CLEANUP_CFGLAYOUT))
2005*404b540aSrobert changed = true;
2006*404b540aSrobert b = c;
2007*404b540aSrobert }
2008*404b540aSrobert
2009*404b540aSrobert /* Remove code labels no longer used. */
2010*404b540aSrobert if (single_pred_p (b)
2011*404b540aSrobert && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
2012*404b540aSrobert && !(single_pred_edge (b)->flags & EDGE_COMPLEX)
2013*404b540aSrobert && LABEL_P (BB_HEAD (b))
2014*404b540aSrobert /* If the previous block ends with a branch to this
2015*404b540aSrobert block, we can't delete the label. Normally this
2016*404b540aSrobert is a condjump that is yet to be simplified, but
2017*404b540aSrobert if CASE_DROPS_THRU, this can be a tablejump with
2018*404b540aSrobert some element going to the same place as the
2019*404b540aSrobert default (fallthru). */
2020*404b540aSrobert && (single_pred (b) == ENTRY_BLOCK_PTR
2021*404b540aSrobert || !JUMP_P (BB_END (single_pred (b)))
2022*404b540aSrobert || ! label_is_jump_target_p (BB_HEAD (b),
2023*404b540aSrobert BB_END (single_pred (b)))))
2024*404b540aSrobert {
2025*404b540aSrobert rtx label = BB_HEAD (b);
2026*404b540aSrobert
2027*404b540aSrobert delete_insn_chain (label, label);
2028*404b540aSrobert /* In the case label is undeletable, move it after the
2029*404b540aSrobert BASIC_BLOCK note. */
2030*404b540aSrobert if (NOTE_LINE_NUMBER (BB_HEAD (b)) == NOTE_INSN_DELETED_LABEL)
2031*404b540aSrobert {
2032*404b540aSrobert rtx bb_note = NEXT_INSN (BB_HEAD (b));
2033*404b540aSrobert
2034*404b540aSrobert reorder_insns_nobb (label, label, bb_note);
2035*404b540aSrobert BB_HEAD (b) = bb_note;
2036*404b540aSrobert }
2037*404b540aSrobert if (dump_file)
2038*404b540aSrobert fprintf (dump_file, "Deleted label in block %i.\n",
2039*404b540aSrobert b->index);
2040*404b540aSrobert }
2041*404b540aSrobert
2042*404b540aSrobert /* If we fall through an empty block, we can remove it. */
2043*404b540aSrobert if (!(mode & CLEANUP_CFGLAYOUT)
2044*404b540aSrobert && single_pred_p (b)
2045*404b540aSrobert && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
2046*404b540aSrobert && !LABEL_P (BB_HEAD (b))
2047*404b540aSrobert && FORWARDER_BLOCK_P (b)
2048*404b540aSrobert /* Note that forwarder_block_p true ensures that
2049*404b540aSrobert there is a successor for this block. */
2050*404b540aSrobert && (single_succ_edge (b)->flags & EDGE_FALLTHRU)
2051*404b540aSrobert && n_basic_blocks > NUM_FIXED_BLOCKS + 1)
2052*404b540aSrobert {
2053*404b540aSrobert if (dump_file)
2054*404b540aSrobert fprintf (dump_file,
2055*404b540aSrobert "Deleting fallthru block %i.\n",
2056*404b540aSrobert b->index);
2057*404b540aSrobert
2058*404b540aSrobert c = b->prev_bb == ENTRY_BLOCK_PTR ? b->next_bb : b->prev_bb;
2059*404b540aSrobert redirect_edge_succ_nodup (single_pred_edge (b),
2060*404b540aSrobert single_succ (b));
2061*404b540aSrobert delete_basic_block (b);
2062*404b540aSrobert changed = true;
2063*404b540aSrobert b = c;
2064*404b540aSrobert }
2065*404b540aSrobert
2066*404b540aSrobert if (single_succ_p (b)
2067*404b540aSrobert && (s = single_succ_edge (b))
2068*404b540aSrobert && !(s->flags & EDGE_COMPLEX)
2069*404b540aSrobert && (c = s->dest) != EXIT_BLOCK_PTR
2070*404b540aSrobert && single_pred_p (c)
2071*404b540aSrobert && b != c)
2072*404b540aSrobert {
2073*404b540aSrobert /* When not in cfg_layout mode use code aware of reordering
2074*404b540aSrobert INSN. This code possibly creates new basic blocks so it
2075*404b540aSrobert does not fit merge_blocks interface and is kept here in
2076*404b540aSrobert hope that it will become useless once more of compiler
2077*404b540aSrobert is transformed to use cfg_layout mode. */
2078*404b540aSrobert
2079*404b540aSrobert if ((mode & CLEANUP_CFGLAYOUT)
2080*404b540aSrobert && can_merge_blocks_p (b, c))
2081*404b540aSrobert {
2082*404b540aSrobert merge_blocks (b, c);
2083*404b540aSrobert update_forwarder_flag (b);
2084*404b540aSrobert changed_here = true;
2085*404b540aSrobert }
2086*404b540aSrobert else if (!(mode & CLEANUP_CFGLAYOUT)
2087*404b540aSrobert /* If the jump insn has side effects,
2088*404b540aSrobert we can't kill the edge. */
2089*404b540aSrobert && (!JUMP_P (BB_END (b))
2090*404b540aSrobert || (reload_completed
2091*404b540aSrobert ? simplejump_p (BB_END (b))
2092*404b540aSrobert : (onlyjump_p (BB_END (b))
2093*404b540aSrobert && !tablejump_p (BB_END (b),
2094*404b540aSrobert NULL, NULL))))
2095*404b540aSrobert && (next = merge_blocks_move (s, b, c, mode)))
2096*404b540aSrobert {
2097*404b540aSrobert b = next;
2098*404b540aSrobert changed_here = true;
2099*404b540aSrobert }
2100*404b540aSrobert }
2101*404b540aSrobert
2102*404b540aSrobert /* Simplify branch over branch. */
2103*404b540aSrobert if ((mode & CLEANUP_EXPENSIVE)
2104*404b540aSrobert && !(mode & CLEANUP_CFGLAYOUT)
2105*404b540aSrobert && try_simplify_condjump (b))
2106*404b540aSrobert changed_here = true;
2107*404b540aSrobert
2108*404b540aSrobert /* If B has a single outgoing edge, but uses a
2109*404b540aSrobert non-trivial jump instruction without side-effects, we
2110*404b540aSrobert can either delete the jump entirely, or replace it
2111*404b540aSrobert with a simple unconditional jump. */
2112*404b540aSrobert if (single_succ_p (b)
2113*404b540aSrobert && single_succ (b) != EXIT_BLOCK_PTR
2114*404b540aSrobert && onlyjump_p (BB_END (b))
2115*404b540aSrobert && !find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX)
2116*404b540aSrobert && try_redirect_by_replacing_jump (single_succ_edge (b),
2117*404b540aSrobert single_succ (b),
2118*404b540aSrobert (mode & CLEANUP_CFGLAYOUT) != 0))
2119*404b540aSrobert {
2120*404b540aSrobert update_forwarder_flag (b);
2121*404b540aSrobert changed_here = true;
2122*404b540aSrobert }
2123*404b540aSrobert
2124*404b540aSrobert /* Simplify branch to branch. */
2125*404b540aSrobert if (try_forward_edges (mode, b))
2126*404b540aSrobert changed_here = true;
2127*404b540aSrobert
2128*404b540aSrobert /* Look for shared code between blocks. */
2129*404b540aSrobert if ((mode & CLEANUP_CROSSJUMP)
2130*404b540aSrobert && try_crossjump_bb (mode, b))
2131*404b540aSrobert changed_here = true;
2132*404b540aSrobert
2133*404b540aSrobert /* Don't get confused by the index shift caused by
2134*404b540aSrobert deleting blocks. */
2135*404b540aSrobert if (!changed_here)
2136*404b540aSrobert b = b->next_bb;
2137*404b540aSrobert else
2138*404b540aSrobert changed = true;
2139*404b540aSrobert }
2140*404b540aSrobert
2141*404b540aSrobert if ((mode & CLEANUP_CROSSJUMP)
2142*404b540aSrobert && try_crossjump_bb (mode, EXIT_BLOCK_PTR))
2143*404b540aSrobert changed = true;
2144*404b540aSrobert
2145*404b540aSrobert #ifdef ENABLE_CHECKING
2146*404b540aSrobert if (changed)
2147*404b540aSrobert verify_flow_info ();
2148*404b540aSrobert #endif
2149*404b540aSrobert
2150*404b540aSrobert changed_overall |= changed;
2151*404b540aSrobert first_pass = false;
2152*404b540aSrobert }
2153*404b540aSrobert while (changed);
2154*404b540aSrobert }
2155*404b540aSrobert
2156*404b540aSrobert if (mode & CLEANUP_CROSSJUMP)
2157*404b540aSrobert remove_fake_exit_edges ();
2158*404b540aSrobert
2159*404b540aSrobert FOR_ALL_BB (b)
2160*404b540aSrobert b->flags &= ~(BB_FORWARDER_BLOCK | BB_NONTHREADABLE_BLOCK);
2161*404b540aSrobert
2162*404b540aSrobert return changed_overall;
2163*404b540aSrobert }
2164*404b540aSrobert
2165*404b540aSrobert /* Delete all unreachable basic blocks. */
2166*404b540aSrobert
2167*404b540aSrobert bool
delete_unreachable_blocks(void)2168*404b540aSrobert delete_unreachable_blocks (void)
2169*404b540aSrobert {
2170*404b540aSrobert bool changed = false;
2171*404b540aSrobert basic_block b, next_bb;
2172*404b540aSrobert
2173*404b540aSrobert find_unreachable_blocks ();
2174*404b540aSrobert
2175*404b540aSrobert /* Delete all unreachable basic blocks. */
2176*404b540aSrobert
2177*404b540aSrobert for (b = ENTRY_BLOCK_PTR->next_bb; b != EXIT_BLOCK_PTR; b = next_bb)
2178*404b540aSrobert {
2179*404b540aSrobert next_bb = b->next_bb;
2180*404b540aSrobert
2181*404b540aSrobert if (!(b->flags & BB_REACHABLE))
2182*404b540aSrobert {
2183*404b540aSrobert delete_basic_block (b);
2184*404b540aSrobert changed = true;
2185*404b540aSrobert }
2186*404b540aSrobert }
2187*404b540aSrobert
2188*404b540aSrobert if (changed)
2189*404b540aSrobert tidy_fallthru_edges ();
2190*404b540aSrobert return changed;
2191*404b540aSrobert }
2192*404b540aSrobert
2193*404b540aSrobert /* Merges sequential blocks if possible. */
2194*404b540aSrobert
2195*404b540aSrobert bool
merge_seq_blocks(void)2196*404b540aSrobert merge_seq_blocks (void)
2197*404b540aSrobert {
2198*404b540aSrobert basic_block bb;
2199*404b540aSrobert bool changed = false;
2200*404b540aSrobert
2201*404b540aSrobert for (bb = ENTRY_BLOCK_PTR->next_bb; bb != EXIT_BLOCK_PTR; )
2202*404b540aSrobert {
2203*404b540aSrobert if (single_succ_p (bb)
2204*404b540aSrobert && can_merge_blocks_p (bb, single_succ (bb)))
2205*404b540aSrobert {
2206*404b540aSrobert /* Merge the blocks and retry. */
2207*404b540aSrobert merge_blocks (bb, single_succ (bb));
2208*404b540aSrobert changed = true;
2209*404b540aSrobert continue;
2210*404b540aSrobert }
2211*404b540aSrobert
2212*404b540aSrobert bb = bb->next_bb;
2213*404b540aSrobert }
2214*404b540aSrobert
2215*404b540aSrobert return changed;
2216*404b540aSrobert }
2217*404b540aSrobert
2218*404b540aSrobert /* Tidy the CFG by deleting unreachable code and whatnot. */
2219*404b540aSrobert
2220*404b540aSrobert bool
cleanup_cfg(int mode)2221*404b540aSrobert cleanup_cfg (int mode)
2222*404b540aSrobert {
2223*404b540aSrobert bool changed = false;
2224*404b540aSrobert
2225*404b540aSrobert timevar_push (TV_CLEANUP_CFG);
2226*404b540aSrobert if (delete_unreachable_blocks ())
2227*404b540aSrobert {
2228*404b540aSrobert changed = true;
2229*404b540aSrobert /* We've possibly created trivially dead code. Cleanup it right
2230*404b540aSrobert now to introduce more opportunities for try_optimize_cfg. */
2231*404b540aSrobert if (!(mode & (CLEANUP_NO_INSN_DEL | CLEANUP_UPDATE_LIFE))
2232*404b540aSrobert && !reload_completed)
2233*404b540aSrobert delete_trivially_dead_insns (get_insns(), max_reg_num ());
2234*404b540aSrobert }
2235*404b540aSrobert
2236*404b540aSrobert compact_blocks ();
2237*404b540aSrobert
2238*404b540aSrobert while (try_optimize_cfg (mode))
2239*404b540aSrobert {
2240*404b540aSrobert delete_unreachable_blocks (), changed = true;
2241*404b540aSrobert if (mode & CLEANUP_UPDATE_LIFE)
2242*404b540aSrobert {
2243*404b540aSrobert /* Cleaning up CFG introduces more opportunities for dead code
2244*404b540aSrobert removal that in turn may introduce more opportunities for
2245*404b540aSrobert cleaning up the CFG. */
2246*404b540aSrobert if (!update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
2247*404b540aSrobert PROP_DEATH_NOTES
2248*404b540aSrobert | PROP_SCAN_DEAD_CODE
2249*404b540aSrobert | PROP_KILL_DEAD_CODE
2250*404b540aSrobert | ((mode & CLEANUP_LOG_LINKS)
2251*404b540aSrobert ? PROP_LOG_LINKS : 0)))
2252*404b540aSrobert break;
2253*404b540aSrobert }
2254*404b540aSrobert else if (!(mode & CLEANUP_NO_INSN_DEL)
2255*404b540aSrobert && (mode & CLEANUP_EXPENSIVE)
2256*404b540aSrobert && !reload_completed)
2257*404b540aSrobert {
2258*404b540aSrobert if (!delete_trivially_dead_insns (get_insns(), max_reg_num ()))
2259*404b540aSrobert break;
2260*404b540aSrobert }
2261*404b540aSrobert else
2262*404b540aSrobert break;
2263*404b540aSrobert delete_dead_jumptables ();
2264*404b540aSrobert }
2265*404b540aSrobert
2266*404b540aSrobert timevar_pop (TV_CLEANUP_CFG);
2267*404b540aSrobert
2268*404b540aSrobert return changed;
2269*404b540aSrobert }
2270*404b540aSrobert
2271*404b540aSrobert static unsigned int
rest_of_handle_jump(void)2272*404b540aSrobert rest_of_handle_jump (void)
2273*404b540aSrobert {
2274*404b540aSrobert delete_unreachable_blocks ();
2275*404b540aSrobert
2276*404b540aSrobert if (cfun->tail_call_emit)
2277*404b540aSrobert fixup_tail_calls ();
2278*404b540aSrobert return 0;
2279*404b540aSrobert }
2280*404b540aSrobert
2281*404b540aSrobert struct tree_opt_pass pass_jump =
2282*404b540aSrobert {
2283*404b540aSrobert "sibling", /* name */
2284*404b540aSrobert NULL, /* gate */
2285*404b540aSrobert rest_of_handle_jump, /* execute */
2286*404b540aSrobert NULL, /* sub */
2287*404b540aSrobert NULL, /* next */
2288*404b540aSrobert 0, /* static_pass_number */
2289*404b540aSrobert TV_JUMP, /* tv_id */
2290*404b540aSrobert 0, /* properties_required */
2291*404b540aSrobert 0, /* properties_provided */
2292*404b540aSrobert 0, /* properties_destroyed */
2293*404b540aSrobert TODO_ggc_collect, /* todo_flags_start */
2294*404b540aSrobert TODO_dump_func |
2295*404b540aSrobert TODO_verify_flow, /* todo_flags_finish */
2296*404b540aSrobert 'i' /* letter */
2297*404b540aSrobert };
2298*404b540aSrobert
2299*404b540aSrobert
2300*404b540aSrobert static unsigned int
rest_of_handle_jump2(void)2301*404b540aSrobert rest_of_handle_jump2 (void)
2302*404b540aSrobert {
2303*404b540aSrobert /* Turn NOTE_INSN_EXPECTED_VALUE into REG_BR_PROB. Do this
2304*404b540aSrobert before jump optimization switches branch directions. */
2305*404b540aSrobert if (flag_guess_branch_prob)
2306*404b540aSrobert expected_value_to_br_prob ();
2307*404b540aSrobert
2308*404b540aSrobert delete_trivially_dead_insns (get_insns (), max_reg_num ());
2309*404b540aSrobert reg_scan (get_insns (), max_reg_num ());
2310*404b540aSrobert if (dump_file)
2311*404b540aSrobert dump_flow_info (dump_file, dump_flags);
2312*404b540aSrobert cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0)
2313*404b540aSrobert | (flag_thread_jumps ? CLEANUP_THREADING : 0));
2314*404b540aSrobert
2315*404b540aSrobert purge_line_number_notes ();
2316*404b540aSrobert
2317*404b540aSrobert if (optimize)
2318*404b540aSrobert cleanup_cfg (CLEANUP_EXPENSIVE);
2319*404b540aSrobert
2320*404b540aSrobert /* Jump optimization, and the removal of NULL pointer checks, may
2321*404b540aSrobert have reduced the number of instructions substantially. CSE, and
2322*404b540aSrobert future passes, allocate arrays whose dimensions involve the
2323*404b540aSrobert maximum instruction UID, so if we can reduce the maximum UID
2324*404b540aSrobert we'll save big on memory. */
2325*404b540aSrobert renumber_insns ();
2326*404b540aSrobert return 0;
2327*404b540aSrobert }
2328*404b540aSrobert
2329*404b540aSrobert
2330*404b540aSrobert struct tree_opt_pass pass_jump2 =
2331*404b540aSrobert {
2332*404b540aSrobert "jump", /* name */
2333*404b540aSrobert NULL, /* gate */
2334*404b540aSrobert rest_of_handle_jump2, /* execute */
2335*404b540aSrobert NULL, /* sub */
2336*404b540aSrobert NULL, /* next */
2337*404b540aSrobert 0, /* static_pass_number */
2338*404b540aSrobert TV_JUMP, /* tv_id */
2339*404b540aSrobert 0, /* properties_required */
2340*404b540aSrobert 0, /* properties_provided */
2341*404b540aSrobert 0, /* properties_destroyed */
2342*404b540aSrobert TODO_ggc_collect, /* todo_flags_start */
2343*404b540aSrobert TODO_dump_func, /* todo_flags_finish */
2344*404b540aSrobert 'j' /* letter */
2345*404b540aSrobert };
2346*404b540aSrobert
2347*404b540aSrobert
2348