1 /* Basic block reordering routines for the GNU compiler.
2 Copyright (C) 2000, 2002 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 "Profile Guided Code Positioning"
24 Pettis and Hanson; PLDI '90.
30 if (p) goto A; // predict taken
33 if (q) goto B; // predict taken
39 We'll currently reorder this as
68 This requires that we be able to duplicate the jump at A, and
69 adjust the graph traversal such that greedy placement doesn't
70 fix D before C is considered.
72 (2) Coordinate with shorten_branches to minimize the number of
75 (3) Invent a method by which sufficiently non-predicted code can
76 be moved to either the end of the section or another section
77 entirely. Some sort of NOTE_INSN note would work fine.
79 This completely scroggs all debugging formats, so the user
80 would have to explicitly ask for it.
85 #include "coretypes.h"
89 #include "hard-reg-set.h"
90 #include "basic-block.h"
93 #include "cfglayout.h"
96 /* Local function prototypes. */
97 static void make_reorder_chain
PARAMS ((void));
98 static basic_block make_reorder_chain_1
PARAMS ((basic_block
, basic_block
));
100 /* Compute an ordering for a subgraph beginning with block BB. Record the
101 ordering in RBI()->index and chained through RBI()->next. */
104 make_reorder_chain ()
106 basic_block prev
= NULL
;
107 basic_block next
, bb
;
109 /* Loop until we've placed every block. */
114 /* Find the next unplaced block. */
115 /* ??? Get rid of this loop, and track which blocks are not yet
116 placed more directly, so as to avoid the O(N^2) worst case.
117 Perhaps keep a doubly-linked list of all to-be-placed blocks;
118 remove from the list as we place. The head of that list is
119 what we're looking for here. */
122 if (! RBI (bb
)->visited
)
129 prev
= make_reorder_chain_1 (next
, prev
);
132 RBI (prev
)->next
= NULL
;
135 /* A helper function for make_reorder_chain.
137 We do not follow EH edges, or non-fallthru edges to noreturn blocks.
138 These are assumed to be the error condition and we wish to cluster
139 all of them at the very end of the function for the benefit of cache
140 locality for the rest of the function.
142 ??? We could do slightly better by noticing earlier that some subgraph
143 has all paths leading to noreturn functions, but for there to be more
144 than one block in such a subgraph is rare. */
147 make_reorder_chain_1 (bb
, prev
)
155 /* Mark this block visited. */
159 RBI (prev
)->next
= bb
;
161 if (rtl_dump_file
&& prev
->next_bb
!= bb
)
162 fprintf (rtl_dump_file
, "Reordering block %d after %d\n",
163 bb
->index
, prev
->index
);
167 if (bb
->prev_bb
!= ENTRY_BLOCK_PTR
)
170 RBI (bb
)->visited
= 1;
173 if (bb
->succ
== NULL
)
176 /* Find the most probable block. */
179 if (any_condjump_p (bb
->end
)
180 && (note
= find_reg_note (bb
->end
, REG_BR_PROB
, 0)) != NULL
)
182 int taken
, probability
;
183 edge e_taken
, e_fall
;
185 probability
= INTVAL (XEXP (note
, 0));
186 taken
= probability
> REG_BR_PROB_BASE
/ 2;
188 /* Find the normal taken edge and the normal fallthru edge.
190 Note, conditional jumps with other side effects may not
191 be fully optimized. In this case it is possible for
192 the conditional jump to branch to the same location as
195 We should probably work to improve optimization of that
196 case; however, it seems silly not to also deal with such
197 problems here if they happen to occur. */
199 e_taken
= e_fall
= NULL
;
200 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
202 if (e
->flags
& EDGE_FALLTHRU
)
204 else if (! (e
->flags
& EDGE_EH
))
208 next
= ((taken
&& e_taken
) ? e_taken
: e_fall
)->dest
;
211 /* In the absence of a prediction, disturb things as little as possible
212 by selecting the old "next" block from the list of successors. If
213 there had been a fallthru edge, that will be the one. */
214 /* Note that the fallthru block may not be next any time we eliminate
218 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
219 if (e
->flags
& EDGE_FALLTHRU
)
224 else if (e
->dest
== bb
->next_bb
)
226 if (! (e
->flags
& (EDGE_ABNORMAL_CALL
| EDGE_EH
)))
231 /* Make sure we didn't select a silly next block. */
232 if (! next
|| next
== EXIT_BLOCK_PTR
|| RBI (next
)->visited
)
235 /* Recurse on the successors. Unroll the last call, as the normal
236 case is exactly one or two edges, and we can tail recurse. */
237 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
238 if (e
->dest
!= EXIT_BLOCK_PTR
239 && ! RBI (e
->dest
)->visited
241 && ! (e
->flags
& (EDGE_ABNORMAL_CALL
| EDGE_EH
)))
245 prev
= make_reorder_chain_1 (next
, prev
);
246 next
= RBI (e
->dest
)->visited
? NULL
: e
->dest
;
260 /* Reorder basic blocks. The main entry point to this file. */
263 reorder_basic_blocks ()
265 if (n_basic_blocks
<= 1)
268 if ((* targetm
.cannot_modify_jumps_p
) ())
271 cfg_layout_initialize (NULL
);
273 make_reorder_chain ();
276 dump_flow_info (rtl_dump_file
);
278 cfg_layout_finalize ();