basic-block.h (OBSTACK_ALLOC_REG_SET): Rename to ...
[gcc.git] / gcc / bb-reorder.c
1 /* Basic block reordering routines for the GNU compiler.
2 Copyright (C) 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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)
9 any later version.
10
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.
15
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
19 02111-1307, USA. */
20
21 /* This (greedy) algorithm constructs traces in several rounds.
22 The construction starts from "seeds". The seed for the first round
23 is the entry point of function. When there are more than one seed
24 that one is selected first that has the lowest key in the heap
25 (see function bb_to_key). Then the algorithm repeatedly adds the most
26 probable successor to the end of a trace. Finally it connects the traces.
27
28 There are two parameters: Branch Threshold and Exec Threshold.
29 If the edge to a successor of the actual basic block is lower than
30 Branch Threshold or the frequency of the successor is lower than
31 Exec Threshold the successor will be the seed in one of the next rounds.
32 Each round has these parameters lower than the previous one.
33 The last round has to have these parameters set to zero
34 so that the remaining blocks are picked up.
35
36 The algorithm selects the most probable successor from all unvisited
37 successors and successors that have been added to this trace.
38 The other successors (that has not been "sent" to the next round) will be
39 other seeds for this round and the secondary traces will start in them.
40 If the successor has not been visited in this trace it is added to the trace
41 (however, there is some heuristic for simple branches).
42 If the successor has been visited in this trace the loop has been found.
43 If the loop has many iterations the loop is rotated so that the
44 source block of the most probable edge going out from the loop
45 is the last block of the trace.
46 If the loop has few iterations and there is no edge from the last block of
47 the loop going out from loop the loop header is duplicated.
48 Finally, the construction of the trace is terminated.
49
50 When connecting traces it first checks whether there is an edge from the
51 last block of one trace to the first block of another trace.
52 When there are still some unconnected traces it checks whether there exists
53 a basic block BB such that BB is a successor of the last bb of one trace
54 and BB is a predecessor of the first block of another trace. In this case,
55 BB is duplicated and the traces are connected through this duplicate.
56 The rest of traces are simply connected so there will be a jump to the
57 beginning of the rest of trace.
58
59
60 References:
61
62 "Software Trace Cache"
63 A. Ramirez, J. Larriba-Pey, C. Navarro, J. Torrellas and M. Valero; 1999
64 http://citeseer.nj.nec.com/15361.html
65
66 */
67
68 #include "config.h"
69 #include "system.h"
70 #include "coretypes.h"
71 #include "tm.h"
72 #include "rtl.h"
73 #include "regs.h"
74 #include "flags.h"
75 #include "timevar.h"
76 #include "output.h"
77 #include "cfglayout.h"
78 #include "fibheap.h"
79 #include "target.h"
80 #include "function.h"
81 #include "tm_p.h"
82 #include "obstack.h"
83 #include "expr.h"
84
85 /* The number of rounds. In most cases there will only be 4 rounds, but
86 when partitioning hot and cold basic blocks into separate sections of
87 the .o file there will be an extra round.*/
88 #define N_ROUNDS 5
89
90 /* Stubs in case we don't have a return insn.
91 We have to check at runtime too, not only compiletime. */
92
93 #ifndef HAVE_return
94 #define HAVE_return 0
95 #define gen_return() NULL_RTX
96 #endif
97
98
99 /* Branch thresholds in thousandths (per mille) of the REG_BR_PROB_BASE. */
100 static int branch_threshold[N_ROUNDS] = {400, 200, 100, 0, 0};
101
102 /* Exec thresholds in thousandths (per mille) of the frequency of bb 0. */
103 static int exec_threshold[N_ROUNDS] = {500, 200, 50, 0, 0};
104
105 /* If edge frequency is lower than DUPLICATION_THRESHOLD per mille of entry
106 block the edge destination is not duplicated while connecting traces. */
107 #define DUPLICATION_THRESHOLD 100
108
109 /* Length of unconditional jump instruction. */
110 static int uncond_jump_length;
111
112 /* Structure to hold needed information for each basic block. */
113 typedef struct bbro_basic_block_data_def
114 {
115 /* Which trace is the bb start of (-1 means it is not a start of a trace). */
116 int start_of_trace;
117
118 /* Which trace is the bb end of (-1 means it is not an end of a trace). */
119 int end_of_trace;
120
121 /* Which heap is BB in (if any)? */
122 fibheap_t heap;
123
124 /* Which heap node is BB in (if any)? */
125 fibnode_t node;
126 } bbro_basic_block_data;
127
128 /* The current size of the following dynamic array. */
129 static int array_size;
130
131 /* The array which holds needed information for basic blocks. */
132 static bbro_basic_block_data *bbd;
133
134 /* To avoid frequent reallocation the size of arrays is greater than needed,
135 the number of elements is (not less than) 1.25 * size_wanted. */
136 #define GET_ARRAY_SIZE(X) ((((X) / 4) + 1) * 5)
137
138 /* Free the memory and set the pointer to NULL. */
139 #define FREE(P) (gcc_assert (P), free (P), P = 0)
140
141 /* Structure for holding information about a trace. */
142 struct trace
143 {
144 /* First and last basic block of the trace. */
145 basic_block first, last;
146
147 /* The round of the STC creation which this trace was found in. */
148 int round;
149
150 /* The length (i.e. the number of basic blocks) of the trace. */
151 int length;
152 };
153
154 /* Maximum frequency and count of one of the entry blocks. */
155 int max_entry_frequency;
156 gcov_type max_entry_count;
157
158 /* Local function prototypes. */
159 static void find_traces (int *, struct trace *);
160 static basic_block rotate_loop (edge, struct trace *, int);
161 static void mark_bb_visited (basic_block, int);
162 static void find_traces_1_round (int, int, gcov_type, struct trace *, int *,
163 int, fibheap_t *, int);
164 static basic_block copy_bb (basic_block, edge, basic_block, int);
165 static fibheapkey_t bb_to_key (basic_block);
166 static bool better_edge_p (basic_block, edge, int, int, int, int, edge);
167 static void connect_traces (int, struct trace *);
168 static bool copy_bb_p (basic_block, int);
169 static int get_uncond_jump_length (void);
170 static bool push_to_next_round_p (basic_block, int, int, int, gcov_type);
171 static void add_unlikely_executed_notes (void);
172 static void find_rarely_executed_basic_blocks_and_crossing_edges (edge *,
173 int *,
174 int *);
175 static void mark_bb_for_unlikely_executed_section (basic_block);
176 static void add_labels_and_missing_jumps (edge *, int);
177 static void add_reg_crossing_jump_notes (void);
178 static void fix_up_fall_thru_edges (void);
179 static void fix_edges_for_rarely_executed_code (edge *, int);
180 static void fix_crossing_conditional_branches (void);
181 static void fix_crossing_unconditional_branches (void);
182 \f
183 /* Check to see if bb should be pushed into the next round of trace
184 collections or not. Reasons for pushing the block forward are 1).
185 If the block is cold, we are doing partitioning, and there will be
186 another round (cold partition blocks are not supposed to be
187 collected into traces until the very last round); or 2). There will
188 be another round, and the basic block is not "hot enough" for the
189 current round of trace collection. */
190
191 static bool
192 push_to_next_round_p (basic_block bb, int round, int number_of_rounds,
193 int exec_th, gcov_type count_th)
194 {
195 bool there_exists_another_round;
196 bool cold_block;
197 bool block_not_hot_enough;
198 bool next_round_is_last;
199
200 there_exists_another_round = round < number_of_rounds - 1;
201 next_round_is_last = round + 1 == number_of_rounds - 1;
202
203 cold_block = (flag_reorder_blocks_and_partition
204 && BB_PARTITION (bb) == BB_COLD_PARTITION);
205
206 block_not_hot_enough = (bb->frequency < exec_th
207 || bb->count < count_th
208 || probably_never_executed_bb_p (bb));
209
210 if (flag_reorder_blocks_and_partition
211 && next_round_is_last
212 && BB_PARTITION (bb) != BB_COLD_PARTITION)
213 return false;
214 else if (there_exists_another_round
215 && (cold_block || block_not_hot_enough))
216 return true;
217 else
218 return false;
219 }
220
221 /* Find the traces for Software Trace Cache. Chain each trace through
222 RBI()->next. Store the number of traces to N_TRACES and description of
223 traces to TRACES. */
224
225 static void
226 find_traces (int *n_traces, struct trace *traces)
227 {
228 int i;
229 int number_of_rounds;
230 edge e;
231 edge_iterator ei;
232 fibheap_t heap;
233
234 /* Add one extra round of trace collection when partitioning hot/cold
235 basic blocks into separate sections. The last round is for all the
236 cold blocks (and ONLY the cold blocks). */
237
238 number_of_rounds = N_ROUNDS - 1;
239 if (flag_reorder_blocks_and_partition)
240 number_of_rounds = N_ROUNDS;
241
242 /* Insert entry points of function into heap. */
243 heap = fibheap_new ();
244 max_entry_frequency = 0;
245 max_entry_count = 0;
246 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
247 {
248 bbd[e->dest->index].heap = heap;
249 bbd[e->dest->index].node = fibheap_insert (heap, bb_to_key (e->dest),
250 e->dest);
251 if (e->dest->frequency > max_entry_frequency)
252 max_entry_frequency = e->dest->frequency;
253 if (e->dest->count > max_entry_count)
254 max_entry_count = e->dest->count;
255 }
256
257 /* Find the traces. */
258 for (i = 0; i < number_of_rounds; i++)
259 {
260 gcov_type count_threshold;
261
262 if (dump_file)
263 fprintf (dump_file, "STC - round %d\n", i + 1);
264
265 if (max_entry_count < INT_MAX / 1000)
266 count_threshold = max_entry_count * exec_threshold[i] / 1000;
267 else
268 count_threshold = max_entry_count / 1000 * exec_threshold[i];
269
270 find_traces_1_round (REG_BR_PROB_BASE * branch_threshold[i] / 1000,
271 max_entry_frequency * exec_threshold[i] / 1000,
272 count_threshold, traces, n_traces, i, &heap,
273 number_of_rounds);
274 }
275 fibheap_delete (heap);
276
277 if (dump_file)
278 {
279 for (i = 0; i < *n_traces; i++)
280 {
281 basic_block bb;
282 fprintf (dump_file, "Trace %d (round %d): ", i + 1,
283 traces[i].round + 1);
284 for (bb = traces[i].first; bb != traces[i].last; bb = bb->rbi->next)
285 fprintf (dump_file, "%d [%d] ", bb->index, bb->frequency);
286 fprintf (dump_file, "%d [%d]\n", bb->index, bb->frequency);
287 }
288 fflush (dump_file);
289 }
290 }
291
292 /* Rotate loop whose back edge is BACK_EDGE in the tail of trace TRACE
293 (with sequential number TRACE_N). */
294
295 static basic_block
296 rotate_loop (edge back_edge, struct trace *trace, int trace_n)
297 {
298 basic_block bb;
299
300 /* Information about the best end (end after rotation) of the loop. */
301 basic_block best_bb = NULL;
302 edge best_edge = NULL;
303 int best_freq = -1;
304 gcov_type best_count = -1;
305 /* The best edge is preferred when its destination is not visited yet
306 or is a start block of some trace. */
307 bool is_preferred = false;
308
309 /* Find the most frequent edge that goes out from current trace. */
310 bb = back_edge->dest;
311 do
312 {
313 edge e;
314 edge_iterator ei;
315
316 FOR_EACH_EDGE (e, ei, bb->succs)
317 if (e->dest != EXIT_BLOCK_PTR
318 && e->dest->rbi->visited != trace_n
319 && (e->flags & EDGE_CAN_FALLTHRU)
320 && !(e->flags & EDGE_COMPLEX))
321 {
322 if (is_preferred)
323 {
324 /* The best edge is preferred. */
325 if (!e->dest->rbi->visited
326 || bbd[e->dest->index].start_of_trace >= 0)
327 {
328 /* The current edge E is also preferred. */
329 int freq = EDGE_FREQUENCY (e);
330 if (freq > best_freq || e->count > best_count)
331 {
332 best_freq = freq;
333 best_count = e->count;
334 best_edge = e;
335 best_bb = bb;
336 }
337 }
338 }
339 else
340 {
341 if (!e->dest->rbi->visited
342 || bbd[e->dest->index].start_of_trace >= 0)
343 {
344 /* The current edge E is preferred. */
345 is_preferred = true;
346 best_freq = EDGE_FREQUENCY (e);
347 best_count = e->count;
348 best_edge = e;
349 best_bb = bb;
350 }
351 else
352 {
353 int freq = EDGE_FREQUENCY (e);
354 if (!best_edge || freq > best_freq || e->count > best_count)
355 {
356 best_freq = freq;
357 best_count = e->count;
358 best_edge = e;
359 best_bb = bb;
360 }
361 }
362 }
363 }
364 bb = bb->rbi->next;
365 }
366 while (bb != back_edge->dest);
367
368 if (best_bb)
369 {
370 /* Rotate the loop so that the BEST_EDGE goes out from the last block of
371 the trace. */
372 if (back_edge->dest == trace->first)
373 {
374 trace->first = best_bb->rbi->next;
375 }
376 else
377 {
378 basic_block prev_bb;
379
380 for (prev_bb = trace->first;
381 prev_bb->rbi->next != back_edge->dest;
382 prev_bb = prev_bb->rbi->next)
383 ;
384 prev_bb->rbi->next = best_bb->rbi->next;
385
386 /* Try to get rid of uncond jump to cond jump. */
387 if (EDGE_COUNT (prev_bb->succs) == 1)
388 {
389 basic_block header = EDGE_SUCC (prev_bb, 0)->dest;
390
391 /* Duplicate HEADER if it is a small block containing cond jump
392 in the end. */
393 if (any_condjump_p (BB_END (header)) && copy_bb_p (header, 0)
394 && !find_reg_note (BB_END (header), REG_CROSSING_JUMP,
395 NULL_RTX))
396 {
397 copy_bb (header, EDGE_SUCC (prev_bb, 0), prev_bb, trace_n);
398 }
399 }
400 }
401 }
402 else
403 {
404 /* We have not found suitable loop tail so do no rotation. */
405 best_bb = back_edge->src;
406 }
407 best_bb->rbi->next = NULL;
408 return best_bb;
409 }
410
411 /* This function marks BB that it was visited in trace number TRACE. */
412
413 static void
414 mark_bb_visited (basic_block bb, int trace)
415 {
416 bb->rbi->visited = trace;
417 if (bbd[bb->index].heap)
418 {
419 fibheap_delete_node (bbd[bb->index].heap, bbd[bb->index].node);
420 bbd[bb->index].heap = NULL;
421 bbd[bb->index].node = NULL;
422 }
423 }
424
425 /* One round of finding traces. Find traces for BRANCH_TH and EXEC_TH i.e. do
426 not include basic blocks their probability is lower than BRANCH_TH or their
427 frequency is lower than EXEC_TH into traces (or count is lower than
428 COUNT_TH). It stores the new traces into TRACES and modifies the number of
429 traces *N_TRACES. Sets the round (which the trace belongs to) to ROUND. It
430 expects that starting basic blocks are in *HEAP and at the end it deletes
431 *HEAP and stores starting points for the next round into new *HEAP. */
432
433 static void
434 find_traces_1_round (int branch_th, int exec_th, gcov_type count_th,
435 struct trace *traces, int *n_traces, int round,
436 fibheap_t *heap, int number_of_rounds)
437 {
438 /* The following variable refers to the last round in which non-"cold"
439 blocks may be collected into a trace. */
440
441 int last_round = N_ROUNDS - 1;
442
443 /* Heap for discarded basic blocks which are possible starting points for
444 the next round. */
445 fibheap_t new_heap = fibheap_new ();
446
447 while (!fibheap_empty (*heap))
448 {
449 basic_block bb;
450 struct trace *trace;
451 edge best_edge, e;
452 fibheapkey_t key;
453 edge_iterator ei;
454
455 bb = fibheap_extract_min (*heap);
456 bbd[bb->index].heap = NULL;
457 bbd[bb->index].node = NULL;
458
459 if (dump_file)
460 fprintf (dump_file, "Getting bb %d\n", bb->index);
461
462 /* If the BB's frequency is too low send BB to the next round. When
463 partitioning hot/cold blocks into separate sections, make sure all
464 the cold blocks (and ONLY the cold blocks) go into the (extra) final
465 round. */
466
467 if (push_to_next_round_p (bb, round, number_of_rounds, exec_th,
468 count_th))
469 {
470 int key = bb_to_key (bb);
471 bbd[bb->index].heap = new_heap;
472 bbd[bb->index].node = fibheap_insert (new_heap, key, bb);
473
474 if (dump_file)
475 fprintf (dump_file,
476 " Possible start point of next round: %d (key: %d)\n",
477 bb->index, key);
478 continue;
479 }
480
481 trace = traces + *n_traces;
482 trace->first = bb;
483 trace->round = round;
484 trace->length = 0;
485 (*n_traces)++;
486
487 do
488 {
489 int prob, freq;
490
491 /* The probability and frequency of the best edge. */
492 int best_prob = INT_MIN / 2;
493 int best_freq = INT_MIN / 2;
494
495 best_edge = NULL;
496 mark_bb_visited (bb, *n_traces);
497 trace->length++;
498
499 if (dump_file)
500 fprintf (dump_file, "Basic block %d was visited in trace %d\n",
501 bb->index, *n_traces - 1);
502
503 /* Select the successor that will be placed after BB. */
504 FOR_EACH_EDGE (e, ei, bb->succs)
505 {
506 gcc_assert (!(e->flags & EDGE_FAKE));
507
508 if (e->dest == EXIT_BLOCK_PTR)
509 continue;
510
511 if (e->dest->rbi->visited
512 && e->dest->rbi->visited != *n_traces)
513 continue;
514
515 if (BB_PARTITION (e->dest) == BB_COLD_PARTITION
516 && round < last_round)
517 continue;
518
519 prob = e->probability;
520 freq = EDGE_FREQUENCY (e);
521
522 /* Edge that cannot be fallthru or improbable or infrequent
523 successor (i.e. it is unsuitable successor). */
524 if (!(e->flags & EDGE_CAN_FALLTHRU) || (e->flags & EDGE_COMPLEX)
525 || prob < branch_th || freq < exec_th || e->count < count_th)
526 continue;
527
528 /* If partitioning hot/cold basic blocks, don't consider edges
529 that cross section boundaries. */
530
531 if (better_edge_p (bb, e, prob, freq, best_prob, best_freq,
532 best_edge))
533 {
534 best_edge = e;
535 best_prob = prob;
536 best_freq = freq;
537 }
538 }
539
540 /* If the best destination has multiple predecessors, and can be
541 duplicated cheaper than a jump, don't allow it to be added
542 to a trace. We'll duplicate it when connecting traces. */
543 if (best_edge && EDGE_COUNT (best_edge->dest->preds) >= 2
544 && copy_bb_p (best_edge->dest, 0))
545 best_edge = NULL;
546
547 /* Add all non-selected successors to the heaps. */
548 FOR_EACH_EDGE (e, ei, bb->succs)
549 {
550 if (e == best_edge
551 || e->dest == EXIT_BLOCK_PTR
552 || e->dest->rbi->visited)
553 continue;
554
555 key = bb_to_key (e->dest);
556
557 if (bbd[e->dest->index].heap)
558 {
559 /* E->DEST is already in some heap. */
560 if (key != bbd[e->dest->index].node->key)
561 {
562 if (dump_file)
563 {
564 fprintf (dump_file,
565 "Changing key for bb %d from %ld to %ld.\n",
566 e->dest->index,
567 (long) bbd[e->dest->index].node->key,
568 key);
569 }
570 fibheap_replace_key (bbd[e->dest->index].heap,
571 bbd[e->dest->index].node, key);
572 }
573 }
574 else
575 {
576 fibheap_t which_heap = *heap;
577
578 prob = e->probability;
579 freq = EDGE_FREQUENCY (e);
580
581 if (!(e->flags & EDGE_CAN_FALLTHRU)
582 || (e->flags & EDGE_COMPLEX)
583 || prob < branch_th || freq < exec_th
584 || e->count < count_th)
585 {
586 /* When partitioning hot/cold basic blocks, make sure
587 the cold blocks (and only the cold blocks) all get
588 pushed to the last round of trace collection. */
589
590 if (push_to_next_round_p (e->dest, round,
591 number_of_rounds,
592 exec_th, count_th))
593 which_heap = new_heap;
594 }
595
596 bbd[e->dest->index].heap = which_heap;
597 bbd[e->dest->index].node = fibheap_insert (which_heap,
598 key, e->dest);
599
600 if (dump_file)
601 {
602 fprintf (dump_file,
603 " Possible start of %s round: %d (key: %ld)\n",
604 (which_heap == new_heap) ? "next" : "this",
605 e->dest->index, (long) key);
606 }
607
608 }
609 }
610
611 if (best_edge) /* Suitable successor was found. */
612 {
613 if (best_edge->dest->rbi->visited == *n_traces)
614 {
615 /* We do nothing with one basic block loops. */
616 if (best_edge->dest != bb)
617 {
618 if (EDGE_FREQUENCY (best_edge)
619 > 4 * best_edge->dest->frequency / 5)
620 {
621 /* The loop has at least 4 iterations. If the loop
622 header is not the first block of the function
623 we can rotate the loop. */
624
625 if (best_edge->dest != ENTRY_BLOCK_PTR->next_bb)
626 {
627 if (dump_file)
628 {
629 fprintf (dump_file,
630 "Rotating loop %d - %d\n",
631 best_edge->dest->index, bb->index);
632 }
633 bb->rbi->next = best_edge->dest;
634 bb = rotate_loop (best_edge, trace, *n_traces);
635 }
636 }
637 else
638 {
639 /* The loop has less than 4 iterations. */
640
641 /* Check whether there is another edge from BB. */
642 edge another_edge;
643 FOR_EACH_EDGE (another_edge, ei, bb->succs)
644 if (another_edge != best_edge)
645 break;
646
647 if (!another_edge && copy_bb_p (best_edge->dest,
648 !optimize_size))
649 {
650 bb = copy_bb (best_edge->dest, best_edge, bb,
651 *n_traces);
652 }
653 }
654 }
655
656 /* Terminate the trace. */
657 break;
658 }
659 else
660 {
661 /* Check for a situation
662
663 A
664 /|
665 B |
666 \|
667 C
668
669 where
670 EDGE_FREQUENCY (AB) + EDGE_FREQUENCY (BC)
671 >= EDGE_FREQUENCY (AC).
672 (i.e. 2 * B->frequency >= EDGE_FREQUENCY (AC) )
673 Best ordering is then A B C.
674
675 This situation is created for example by:
676
677 if (A) B;
678 C;
679
680 */
681
682 FOR_EACH_EDGE (e, ei, bb->succs)
683 if (e != best_edge
684 && (e->flags & EDGE_CAN_FALLTHRU)
685 && !(e->flags & EDGE_COMPLEX)
686 && !e->dest->rbi->visited
687 && EDGE_COUNT (e->dest->preds) == 1
688 && !(e->flags & EDGE_CROSSING)
689 && EDGE_COUNT (e->dest->succs) == 1
690 && (EDGE_SUCC (e->dest, 0)->flags & EDGE_CAN_FALLTHRU)
691 && !(EDGE_SUCC (e->dest, 0)->flags & EDGE_COMPLEX)
692 && EDGE_SUCC (e->dest, 0)->dest == best_edge->dest
693 && 2 * e->dest->frequency >= EDGE_FREQUENCY (best_edge))
694 {
695 best_edge = e;
696 if (dump_file)
697 fprintf (dump_file, "Selecting BB %d\n",
698 best_edge->dest->index);
699 break;
700 }
701
702 bb->rbi->next = best_edge->dest;
703 bb = best_edge->dest;
704 }
705 }
706 }
707 while (best_edge);
708 trace->last = bb;
709 bbd[trace->first->index].start_of_trace = *n_traces - 1;
710 bbd[trace->last->index].end_of_trace = *n_traces - 1;
711
712 /* The trace is terminated so we have to recount the keys in heap
713 (some block can have a lower key because now one of its predecessors
714 is an end of the trace). */
715 FOR_EACH_EDGE (e, ei, bb->succs)
716 {
717 if (e->dest == EXIT_BLOCK_PTR
718 || e->dest->rbi->visited)
719 continue;
720
721 if (bbd[e->dest->index].heap)
722 {
723 key = bb_to_key (e->dest);
724 if (key != bbd[e->dest->index].node->key)
725 {
726 if (dump_file)
727 {
728 fprintf (dump_file,
729 "Changing key for bb %d from %ld to %ld.\n",
730 e->dest->index,
731 (long) bbd[e->dest->index].node->key, key);
732 }
733 fibheap_replace_key (bbd[e->dest->index].heap,
734 bbd[e->dest->index].node,
735 key);
736 }
737 }
738 }
739 }
740
741 fibheap_delete (*heap);
742
743 /* "Return" the new heap. */
744 *heap = new_heap;
745 }
746
747 /* Create a duplicate of the basic block OLD_BB and redirect edge E to it, add
748 it to trace after BB, mark OLD_BB visited and update pass' data structures
749 (TRACE is a number of trace which OLD_BB is duplicated to). */
750
751 static basic_block
752 copy_bb (basic_block old_bb, edge e, basic_block bb, int trace)
753 {
754 basic_block new_bb;
755
756 new_bb = duplicate_block (old_bb, e);
757 BB_COPY_PARTITION (new_bb, old_bb);
758
759 gcc_assert (e->dest == new_bb);
760 gcc_assert (!e->dest->rbi->visited);
761
762 if (dump_file)
763 fprintf (dump_file,
764 "Duplicated bb %d (created bb %d)\n",
765 old_bb->index, new_bb->index);
766 new_bb->rbi->visited = trace;
767 new_bb->rbi->next = bb->rbi->next;
768 bb->rbi->next = new_bb;
769
770 if (new_bb->index >= array_size || last_basic_block > array_size)
771 {
772 int i;
773 int new_size;
774
775 new_size = MAX (last_basic_block, new_bb->index + 1);
776 new_size = GET_ARRAY_SIZE (new_size);
777 bbd = xrealloc (bbd, new_size * sizeof (bbro_basic_block_data));
778 for (i = array_size; i < new_size; i++)
779 {
780 bbd[i].start_of_trace = -1;
781 bbd[i].end_of_trace = -1;
782 bbd[i].heap = NULL;
783 bbd[i].node = NULL;
784 }
785 array_size = new_size;
786
787 if (dump_file)
788 {
789 fprintf (dump_file,
790 "Growing the dynamic array to %d elements.\n",
791 array_size);
792 }
793 }
794
795 return new_bb;
796 }
797
798 /* Compute and return the key (for the heap) of the basic block BB. */
799
800 static fibheapkey_t
801 bb_to_key (basic_block bb)
802 {
803 edge e;
804 edge_iterator ei;
805 int priority = 0;
806
807 /* Do not start in probably never executed blocks. */
808
809 if (BB_PARTITION (bb) == BB_COLD_PARTITION
810 || probably_never_executed_bb_p (bb))
811 return BB_FREQ_MAX;
812
813 /* Prefer blocks whose predecessor is an end of some trace
814 or whose predecessor edge is EDGE_DFS_BACK. */
815 FOR_EACH_EDGE (e, ei, bb->preds)
816 {
817 if ((e->src != ENTRY_BLOCK_PTR && bbd[e->src->index].end_of_trace >= 0)
818 || (e->flags & EDGE_DFS_BACK))
819 {
820 int edge_freq = EDGE_FREQUENCY (e);
821
822 if (edge_freq > priority)
823 priority = edge_freq;
824 }
825 }
826
827 if (priority)
828 /* The block with priority should have significantly lower key. */
829 return -(100 * BB_FREQ_MAX + 100 * priority + bb->frequency);
830 return -bb->frequency;
831 }
832
833 /* Return true when the edge E from basic block BB is better than the temporary
834 best edge (details are in function). The probability of edge E is PROB. The
835 frequency of the successor is FREQ. The current best probability is
836 BEST_PROB, the best frequency is BEST_FREQ.
837 The edge is considered to be equivalent when PROB does not differ much from
838 BEST_PROB; similarly for frequency. */
839
840 static bool
841 better_edge_p (basic_block bb, edge e, int prob, int freq, int best_prob,
842 int best_freq, edge cur_best_edge)
843 {
844 bool is_better_edge;
845
846 /* The BEST_* values do not have to be best, but can be a bit smaller than
847 maximum values. */
848 int diff_prob = best_prob / 10;
849 int diff_freq = best_freq / 10;
850
851 if (prob > best_prob + diff_prob)
852 /* The edge has higher probability than the temporary best edge. */
853 is_better_edge = true;
854 else if (prob < best_prob - diff_prob)
855 /* The edge has lower probability than the temporary best edge. */
856 is_better_edge = false;
857 else if (freq < best_freq - diff_freq)
858 /* The edge and the temporary best edge have almost equivalent
859 probabilities. The higher frequency of a successor now means
860 that there is another edge going into that successor.
861 This successor has lower frequency so it is better. */
862 is_better_edge = true;
863 else if (freq > best_freq + diff_freq)
864 /* This successor has higher frequency so it is worse. */
865 is_better_edge = false;
866 else if (e->dest->prev_bb == bb)
867 /* The edges have equivalent probabilities and the successors
868 have equivalent frequencies. Select the previous successor. */
869 is_better_edge = true;
870 else
871 is_better_edge = false;
872
873 /* If we are doing hot/cold partitioning, make sure that we always favor
874 non-crossing edges over crossing edges. */
875
876 if (!is_better_edge
877 && flag_reorder_blocks_and_partition
878 && cur_best_edge
879 && (cur_best_edge->flags & EDGE_CROSSING)
880 && !(e->flags & EDGE_CROSSING))
881 is_better_edge = true;
882
883 return is_better_edge;
884 }
885
886 /* Connect traces in array TRACES, N_TRACES is the count of traces. */
887
888 static void
889 connect_traces (int n_traces, struct trace *traces)
890 {
891 int i;
892 int unconnected_hot_trace_count = 0;
893 bool cold_connected = true;
894 bool *connected;
895 bool *cold_traces;
896 int last_trace;
897 int freq_threshold;
898 gcov_type count_threshold;
899
900 freq_threshold = max_entry_frequency * DUPLICATION_THRESHOLD / 1000;
901 if (max_entry_count < INT_MAX / 1000)
902 count_threshold = max_entry_count * DUPLICATION_THRESHOLD / 1000;
903 else
904 count_threshold = max_entry_count / 1000 * DUPLICATION_THRESHOLD;
905
906 connected = xcalloc (n_traces, sizeof (bool));
907 last_trace = -1;
908
909 /* If we are partitioning hot/cold basic blocks, mark the cold
910 traces as already connected, to remove them from consideration
911 for connection to the hot traces. After the hot traces have all
912 been connected (determined by "unconnected_hot_trace_count"), we
913 will go back and connect the cold traces. */
914
915 cold_traces = xcalloc (n_traces, sizeof (bool));
916
917 if (flag_reorder_blocks_and_partition)
918 for (i = 0; i < n_traces; i++)
919 {
920 if (BB_PARTITION (traces[i].first) == BB_COLD_PARTITION)
921 {
922 connected[i] = true;
923 cold_traces[i] = true;
924 cold_connected = false;
925 }
926 else
927 unconnected_hot_trace_count++;
928 }
929
930 for (i = 0; i < n_traces || !cold_connected ; i++)
931 {
932 int t = i;
933 int t2;
934 edge e, best;
935 int best_len;
936
937 /* If we are partitioning hot/cold basic blocks, check to see
938 if all the hot traces have been connected. If so, go back
939 and mark the cold traces as unconnected so we can connect
940 them up too. Re-set "i" to the first (unconnected) cold
941 trace. Use flag "cold_connected" to make sure we don't do
942 this step more than once. */
943
944 if (flag_reorder_blocks_and_partition
945 && (i >= n_traces || unconnected_hot_trace_count <= 0)
946 && !cold_connected)
947 {
948 int j;
949 int first_cold_trace = -1;
950
951 for (j = 0; j < n_traces; j++)
952 if (cold_traces[j])
953 {
954 connected[j] = false;
955 if (first_cold_trace == -1)
956 first_cold_trace = j;
957 }
958 i = t = first_cold_trace;
959 cold_connected = true;
960 }
961
962 if (connected[t])
963 continue;
964
965 connected[t] = true;
966 if (unconnected_hot_trace_count > 0)
967 unconnected_hot_trace_count--;
968
969 /* Find the predecessor traces. */
970 for (t2 = t; t2 > 0;)
971 {
972 edge_iterator ei;
973 best = NULL;
974 best_len = 0;
975 FOR_EACH_EDGE (e, ei, traces[t2].first->preds)
976 {
977 int si = e->src->index;
978
979 if (e->src != ENTRY_BLOCK_PTR
980 && (e->flags & EDGE_CAN_FALLTHRU)
981 && !(e->flags & EDGE_COMPLEX)
982 && bbd[si].end_of_trace >= 0
983 && !connected[bbd[si].end_of_trace]
984 && (!best
985 || e->probability > best->probability
986 || (e->probability == best->probability
987 && traces[bbd[si].end_of_trace].length > best_len)))
988 {
989 best = e;
990 best_len = traces[bbd[si].end_of_trace].length;
991 }
992 }
993 if (best)
994 {
995 best->src->rbi->next = best->dest;
996 t2 = bbd[best->src->index].end_of_trace;
997 connected[t2] = true;
998
999 if (unconnected_hot_trace_count > 0)
1000 unconnected_hot_trace_count--;
1001
1002 if (dump_file)
1003 {
1004 fprintf (dump_file, "Connection: %d %d\n",
1005 best->src->index, best->dest->index);
1006 }
1007 }
1008 else
1009 break;
1010 }
1011
1012 if (last_trace >= 0)
1013 traces[last_trace].last->rbi->next = traces[t2].first;
1014 last_trace = t;
1015
1016 /* Find the successor traces. */
1017 while (1)
1018 {
1019 /* Find the continuation of the chain. */
1020 edge_iterator ei;
1021 best = NULL;
1022 best_len = 0;
1023 FOR_EACH_EDGE (e, ei, traces[t].last->succs)
1024 {
1025 int di = e->dest->index;
1026
1027 if (e->dest != EXIT_BLOCK_PTR
1028 && (e->flags & EDGE_CAN_FALLTHRU)
1029 && !(e->flags & EDGE_COMPLEX)
1030 && bbd[di].start_of_trace >= 0
1031 && !connected[bbd[di].start_of_trace]
1032 && (!best
1033 || e->probability > best->probability
1034 || (e->probability == best->probability
1035 && traces[bbd[di].start_of_trace].length > best_len)))
1036 {
1037 best = e;
1038 best_len = traces[bbd[di].start_of_trace].length;
1039 }
1040 }
1041
1042 if (best)
1043 {
1044 if (dump_file)
1045 {
1046 fprintf (dump_file, "Connection: %d %d\n",
1047 best->src->index, best->dest->index);
1048 }
1049 t = bbd[best->dest->index].start_of_trace;
1050 traces[last_trace].last->rbi->next = traces[t].first;
1051 connected[t] = true;
1052 if (unconnected_hot_trace_count > 0)
1053 unconnected_hot_trace_count--;
1054 last_trace = t;
1055 }
1056 else
1057 {
1058 /* Try to connect the traces by duplication of 1 block. */
1059 edge e2;
1060 basic_block next_bb = NULL;
1061 bool try_copy = false;
1062
1063 FOR_EACH_EDGE (e, ei, traces[t].last->succs)
1064 if (e->dest != EXIT_BLOCK_PTR
1065 && (e->flags & EDGE_CAN_FALLTHRU)
1066 && !(e->flags & EDGE_COMPLEX)
1067 && (!best || e->probability > best->probability))
1068 {
1069 edge_iterator ei;
1070 edge best2 = NULL;
1071 int best2_len = 0;
1072
1073 /* If the destination is a start of a trace which is only
1074 one block long, then no need to search the successor
1075 blocks of the trace. Accept it. */
1076 if (bbd[e->dest->index].start_of_trace >= 0
1077 && traces[bbd[e->dest->index].start_of_trace].length
1078 == 1)
1079 {
1080 best = e;
1081 try_copy = true;
1082 continue;
1083 }
1084
1085 FOR_EACH_EDGE (e2, ei, e->dest->succs)
1086 {
1087 int di = e2->dest->index;
1088
1089 if (e2->dest == EXIT_BLOCK_PTR
1090 || ((e2->flags & EDGE_CAN_FALLTHRU)
1091 && !(e2->flags & EDGE_COMPLEX)
1092 && bbd[di].start_of_trace >= 0
1093 && !connected[bbd[di].start_of_trace]
1094 && (EDGE_FREQUENCY (e2) >= freq_threshold)
1095 && (e2->count >= count_threshold)
1096 && (!best2
1097 || e2->probability > best2->probability
1098 || (e2->probability == best2->probability
1099 && traces[bbd[di].start_of_trace].length
1100 > best2_len))))
1101 {
1102 best = e;
1103 best2 = e2;
1104 if (e2->dest != EXIT_BLOCK_PTR)
1105 best2_len = traces[bbd[di].start_of_trace].length;
1106 else
1107 best2_len = INT_MAX;
1108 next_bb = e2->dest;
1109 try_copy = true;
1110 }
1111 }
1112 }
1113
1114 if (flag_reorder_blocks_and_partition)
1115 try_copy = false;
1116
1117 /* Copy tiny blocks always; copy larger blocks only when the
1118 edge is traversed frequently enough. */
1119 if (try_copy
1120 && copy_bb_p (best->dest,
1121 !optimize_size
1122 && EDGE_FREQUENCY (best) >= freq_threshold
1123 && best->count >= count_threshold))
1124 {
1125 basic_block new_bb;
1126
1127 if (dump_file)
1128 {
1129 fprintf (dump_file, "Connection: %d %d ",
1130 traces[t].last->index, best->dest->index);
1131 if (!next_bb)
1132 fputc ('\n', dump_file);
1133 else if (next_bb == EXIT_BLOCK_PTR)
1134 fprintf (dump_file, "exit\n");
1135 else
1136 fprintf (dump_file, "%d\n", next_bb->index);
1137 }
1138
1139 new_bb = copy_bb (best->dest, best, traces[t].last, t);
1140 traces[t].last = new_bb;
1141 if (next_bb && next_bb != EXIT_BLOCK_PTR)
1142 {
1143 t = bbd[next_bb->index].start_of_trace;
1144 traces[last_trace].last->rbi->next = traces[t].first;
1145 connected[t] = true;
1146 if (unconnected_hot_trace_count > 0)
1147 unconnected_hot_trace_count--;
1148 last_trace = t;
1149 }
1150 else
1151 break; /* Stop finding the successor traces. */
1152 }
1153 else
1154 break; /* Stop finding the successor traces. */
1155 }
1156 }
1157 }
1158
1159 if (dump_file)
1160 {
1161 basic_block bb;
1162
1163 fprintf (dump_file, "Final order:\n");
1164 for (bb = traces[0].first; bb; bb = bb->rbi->next)
1165 fprintf (dump_file, "%d ", bb->index);
1166 fprintf (dump_file, "\n");
1167 fflush (dump_file);
1168 }
1169
1170 FREE (connected);
1171 FREE (cold_traces);
1172 }
1173
1174 /* Return true when BB can and should be copied. CODE_MAY_GROW is true
1175 when code size is allowed to grow by duplication. */
1176
1177 static bool
1178 copy_bb_p (basic_block bb, int code_may_grow)
1179 {
1180 int size = 0;
1181 int max_size = uncond_jump_length;
1182 rtx insn;
1183
1184 if (!bb->frequency)
1185 return false;
1186 if (EDGE_COUNT (bb->preds) < 2)
1187 return false;
1188 if (!can_duplicate_block_p (bb))
1189 return false;
1190
1191 /* Avoid duplicating blocks which have many successors (PR/13430). */
1192 if (EDGE_COUNT (bb->succs) > 8)
1193 return false;
1194
1195 if (code_may_grow && maybe_hot_bb_p (bb))
1196 max_size *= 8;
1197
1198 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
1199 insn = NEXT_INSN (insn))
1200 {
1201 if (INSN_P (insn))
1202 size += get_attr_length (insn);
1203 }
1204
1205 if (size <= max_size)
1206 return true;
1207
1208 if (dump_file)
1209 {
1210 fprintf (dump_file,
1211 "Block %d can't be copied because its size = %d.\n",
1212 bb->index, size);
1213 }
1214
1215 return false;
1216 }
1217
1218 /* Return the length of unconditional jump instruction. */
1219
1220 static int
1221 get_uncond_jump_length (void)
1222 {
1223 rtx label, jump;
1224 int length;
1225
1226 label = emit_label_before (gen_label_rtx (), get_insns ());
1227 jump = emit_jump_insn (gen_jump (label));
1228
1229 length = get_attr_length (jump);
1230
1231 delete_insn (jump);
1232 delete_insn (label);
1233 return length;
1234 }
1235
1236 static void
1237 add_unlikely_executed_notes (void)
1238 {
1239 basic_block bb;
1240
1241 /* Add the UNLIKELY_EXECUTED_NOTES to each cold basic block. */
1242
1243 FOR_EACH_BB (bb)
1244 if (BB_PARTITION (bb) == BB_COLD_PARTITION)
1245 mark_bb_for_unlikely_executed_section (bb);
1246 }
1247
1248 /* Find the basic blocks that are rarely executed and need to be moved to
1249 a separate section of the .o file (to cut down on paging and improve
1250 cache locality). */
1251
1252 static void
1253 find_rarely_executed_basic_blocks_and_crossing_edges (edge *crossing_edges,
1254 int *n_crossing_edges,
1255 int *max_idx)
1256 {
1257 basic_block bb;
1258 bool has_hot_blocks = false;
1259 edge e;
1260 int i;
1261 edge_iterator ei;
1262
1263 /* Mark which partition (hot/cold) each basic block belongs in. */
1264
1265 FOR_EACH_BB (bb)
1266 {
1267 if (probably_never_executed_bb_p (bb))
1268 BB_SET_PARTITION (bb, BB_COLD_PARTITION);
1269 else
1270 {
1271 BB_SET_PARTITION (bb, BB_HOT_PARTITION);
1272 has_hot_blocks = true;
1273 }
1274 }
1275
1276 /* Since all "hot" basic blocks will eventually be scheduled before all
1277 cold basic blocks, make *sure* the real function entry block is in
1278 the hot partition (if there is one). */
1279
1280 if (has_hot_blocks)
1281 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
1282 if (e->dest->index >= 0)
1283 {
1284 BB_SET_PARTITION (e->dest, BB_HOT_PARTITION);
1285 break;
1286 }
1287
1288 /* Mark every edge that crosses between sections. */
1289
1290 i = 0;
1291 if (targetm.have_named_sections)
1292 {
1293 FOR_EACH_BB (bb)
1294 FOR_EACH_EDGE (e, ei, bb->succs)
1295 {
1296 if (e->src != ENTRY_BLOCK_PTR
1297 && e->dest != EXIT_BLOCK_PTR
1298 && BB_PARTITION (e->src) != BB_PARTITION (e->dest))
1299 {
1300 e->flags |= EDGE_CROSSING;
1301 if (i == *max_idx)
1302 {
1303 *max_idx *= 2;
1304 crossing_edges = xrealloc (crossing_edges,
1305 (*max_idx) * sizeof (edge));
1306 }
1307 crossing_edges[i++] = e;
1308 }
1309 else
1310 e->flags &= ~EDGE_CROSSING;
1311 }
1312 }
1313 *n_crossing_edges = i;
1314 }
1315
1316 /* Add NOTE_INSN_UNLIKELY_EXECUTED_CODE to top of basic block. This note
1317 is later used to mark the basic block to be put in the
1318 unlikely-to-be-executed section of the .o file. */
1319
1320 static void
1321 mark_bb_for_unlikely_executed_section (basic_block bb)
1322 {
1323 rtx cur_insn;
1324 rtx insert_insn = NULL;
1325 rtx new_note;
1326
1327 /* Insert new NOTE immediately after BASIC_BLOCK note. */
1328
1329 for (cur_insn = BB_HEAD (bb); cur_insn != NEXT_INSN (BB_END (bb));
1330 cur_insn = NEXT_INSN (cur_insn))
1331 if (GET_CODE (cur_insn) == NOTE
1332 && NOTE_LINE_NUMBER (cur_insn) == NOTE_INSN_BASIC_BLOCK)
1333 {
1334 insert_insn = cur_insn;
1335 break;
1336 }
1337
1338 /* If basic block does not contain a NOTE_INSN_BASIC_BLOCK, there is
1339 a major problem. */
1340 gcc_assert (insert_insn);
1341
1342 /* Insert note and assign basic block number to it. */
1343
1344 new_note = emit_note_after (NOTE_INSN_UNLIKELY_EXECUTED_CODE,
1345 insert_insn);
1346 NOTE_BASIC_BLOCK (new_note) = bb;
1347 }
1348
1349 /* If any destination of a crossing edge does not have a label, add label;
1350 Convert any fall-through crossing edges (for blocks that do not contain
1351 a jump) to unconditional jumps. */
1352
1353 static void
1354 add_labels_and_missing_jumps (edge *crossing_edges, int n_crossing_edges)
1355 {
1356 int i;
1357 basic_block src;
1358 basic_block dest;
1359 rtx label;
1360 rtx barrier;
1361 rtx new_jump;
1362
1363 for (i=0; i < n_crossing_edges; i++)
1364 {
1365 if (crossing_edges[i])
1366 {
1367 src = crossing_edges[i]->src;
1368 dest = crossing_edges[i]->dest;
1369
1370 /* Make sure dest has a label. */
1371
1372 if (dest && (dest != EXIT_BLOCK_PTR))
1373 {
1374 label = block_label (dest);
1375
1376 /* Make sure source block ends with a jump. */
1377
1378 if (src && (src != ENTRY_BLOCK_PTR))
1379 {
1380 if (!JUMP_P (BB_END (src)))
1381 /* bb just falls through. */
1382 {
1383 /* make sure there's only one successor */
1384 gcc_assert (EDGE_COUNT (src->succs) == 1);
1385
1386 /* Find label in dest block. */
1387 label = block_label (dest);
1388
1389 new_jump = emit_jump_insn_after (gen_jump (label),
1390 BB_END (src));
1391 barrier = emit_barrier_after (new_jump);
1392 JUMP_LABEL (new_jump) = label;
1393 LABEL_NUSES (label) += 1;
1394 src->rbi->footer = unlink_insn_chain (barrier, barrier);
1395 /* Mark edge as non-fallthru. */
1396 crossing_edges[i]->flags &= ~EDGE_FALLTHRU;
1397 } /* end: 'if (GET_CODE ... ' */
1398 } /* end: 'if (src && src->index...' */
1399 } /* end: 'if (dest && dest->index...' */
1400 } /* end: 'if (crossing_edges[i]...' */
1401 } /* end for loop */
1402 }
1403
1404 /* Find any bb's where the fall-through edge is a crossing edge (note that
1405 these bb's must also contain a conditional jump; we've already
1406 dealt with fall-through edges for blocks that didn't have a
1407 conditional jump in the call to add_labels_and_missing_jumps).
1408 Convert the fall-through edge to non-crossing edge by inserting a
1409 new bb to fall-through into. The new bb will contain an
1410 unconditional jump (crossing edge) to the original fall through
1411 destination. */
1412
1413 static void
1414 fix_up_fall_thru_edges (void)
1415 {
1416 basic_block cur_bb;
1417 basic_block new_bb;
1418 edge succ1;
1419 edge succ2;
1420 edge fall_thru;
1421 edge cond_jump = NULL;
1422 edge e;
1423 bool cond_jump_crosses;
1424 int invert_worked;
1425 rtx old_jump;
1426 rtx fall_thru_label;
1427 rtx barrier;
1428
1429 FOR_EACH_BB (cur_bb)
1430 {
1431 fall_thru = NULL;
1432 if (EDGE_COUNT (cur_bb->succs) > 0)
1433 succ1 = EDGE_SUCC (cur_bb, 0);
1434 else
1435 succ1 = NULL;
1436
1437 if (EDGE_COUNT (cur_bb->succs) > 1)
1438 succ2 = EDGE_SUCC (cur_bb, 1);
1439 else
1440 succ2 = NULL;
1441
1442 /* Find the fall-through edge. */
1443
1444 if (succ1
1445 && (succ1->flags & EDGE_FALLTHRU))
1446 {
1447 fall_thru = succ1;
1448 cond_jump = succ2;
1449 }
1450 else if (succ2
1451 && (succ2->flags & EDGE_FALLTHRU))
1452 {
1453 fall_thru = succ2;
1454 cond_jump = succ1;
1455 }
1456
1457 if (fall_thru && (fall_thru->dest != EXIT_BLOCK_PTR))
1458 {
1459 /* Check to see if the fall-thru edge is a crossing edge. */
1460
1461 if (fall_thru->flags & EDGE_CROSSING)
1462 {
1463 /* The fall_thru edge crosses; now check the cond jump edge, if
1464 it exists. */
1465
1466 cond_jump_crosses = true;
1467 invert_worked = 0;
1468 old_jump = BB_END (cur_bb);
1469
1470 /* Find the jump instruction, if there is one. */
1471
1472 if (cond_jump)
1473 {
1474 if (!(cond_jump->flags & EDGE_CROSSING))
1475 cond_jump_crosses = false;
1476
1477 /* We know the fall-thru edge crosses; if the cond
1478 jump edge does NOT cross, and its destination is the
1479 next block in the bb order, invert the jump
1480 (i.e. fix it so the fall thru does not cross and
1481 the cond jump does). */
1482
1483 if (!cond_jump_crosses
1484 && cur_bb->rbi->next == cond_jump->dest)
1485 {
1486 /* Find label in fall_thru block. We've already added
1487 any missing labels, so there must be one. */
1488
1489 fall_thru_label = block_label (fall_thru->dest);
1490
1491 if (old_jump && fall_thru_label)
1492 invert_worked = invert_jump (old_jump,
1493 fall_thru_label,0);
1494 if (invert_worked)
1495 {
1496 fall_thru->flags &= ~EDGE_FALLTHRU;
1497 cond_jump->flags |= EDGE_FALLTHRU;
1498 update_br_prob_note (cur_bb);
1499 e = fall_thru;
1500 fall_thru = cond_jump;
1501 cond_jump = e;
1502 cond_jump->flags |= EDGE_CROSSING;
1503 fall_thru->flags &= ~EDGE_CROSSING;
1504 }
1505 }
1506 }
1507
1508 if (cond_jump_crosses || !invert_worked)
1509 {
1510 /* This is the case where both edges out of the basic
1511 block are crossing edges. Here we will fix up the
1512 fall through edge. The jump edge will be taken care
1513 of later. */
1514
1515 new_bb = force_nonfallthru (fall_thru);
1516
1517 if (new_bb)
1518 {
1519 new_bb->rbi->next = cur_bb->rbi->next;
1520 cur_bb->rbi->next = new_bb;
1521
1522 /* Make sure new fall-through bb is in same
1523 partition as bb it's falling through from. */
1524
1525 BB_COPY_PARTITION (new_bb, cur_bb);
1526 EDGE_SUCC (new_bb, 0)->flags |= EDGE_CROSSING;
1527 }
1528
1529 /* Add barrier after new jump */
1530
1531 if (new_bb)
1532 {
1533 barrier = emit_barrier_after (BB_END (new_bb));
1534 new_bb->rbi->footer = unlink_insn_chain (barrier,
1535 barrier);
1536 }
1537 else
1538 {
1539 barrier = emit_barrier_after (BB_END (cur_bb));
1540 cur_bb->rbi->footer = unlink_insn_chain (barrier,
1541 barrier);
1542 }
1543 }
1544 }
1545 }
1546 }
1547 }
1548
1549 /* This function checks the destination blockof a "crossing jump" to
1550 see if it has any crossing predecessors that begin with a code label
1551 and end with an unconditional jump. If so, it returns that predecessor
1552 block. (This is to avoid creating lots of new basic blocks that all
1553 contain unconditional jumps to the same destination). */
1554
1555 static basic_block
1556 find_jump_block (basic_block jump_dest)
1557 {
1558 basic_block source_bb = NULL;
1559 edge e;
1560 rtx insn;
1561 edge_iterator ei;
1562
1563 FOR_EACH_EDGE (e, ei, jump_dest->preds)
1564 if (e->flags & EDGE_CROSSING)
1565 {
1566 basic_block src = e->src;
1567
1568 /* Check each predecessor to see if it has a label, and contains
1569 only one executable instruction, which is an unconditional jump.
1570 If so, we can use it. */
1571
1572 if (LABEL_P (BB_HEAD (src)))
1573 for (insn = BB_HEAD (src);
1574 !INSN_P (insn) && insn != NEXT_INSN (BB_END (src));
1575 insn = NEXT_INSN (insn))
1576 {
1577 if (INSN_P (insn)
1578 && insn == BB_END (src)
1579 && JUMP_P (insn)
1580 && !any_condjump_p (insn))
1581 {
1582 source_bb = src;
1583 break;
1584 }
1585 }
1586
1587 if (source_bb)
1588 break;
1589 }
1590
1591 return source_bb;
1592 }
1593
1594 /* Find all BB's with conditional jumps that are crossing edges;
1595 insert a new bb and make the conditional jump branch to the new
1596 bb instead (make the new bb same color so conditional branch won't
1597 be a 'crossing' edge). Insert an unconditional jump from the
1598 new bb to the original destination of the conditional jump. */
1599
1600 static void
1601 fix_crossing_conditional_branches (void)
1602 {
1603 basic_block cur_bb;
1604 basic_block new_bb;
1605 basic_block last_bb;
1606 basic_block dest;
1607 basic_block prev_bb;
1608 edge succ1;
1609 edge succ2;
1610 edge crossing_edge;
1611 edge new_edge;
1612 rtx old_jump;
1613 rtx set_src;
1614 rtx old_label = NULL_RTX;
1615 rtx new_label;
1616 rtx new_jump;
1617 rtx barrier;
1618
1619 last_bb = EXIT_BLOCK_PTR->prev_bb;
1620
1621 FOR_EACH_BB (cur_bb)
1622 {
1623 crossing_edge = NULL;
1624 if (EDGE_COUNT (cur_bb->succs) > 0)
1625 succ1 = EDGE_SUCC (cur_bb, 0);
1626 else
1627 succ1 = NULL;
1628
1629 if (EDGE_COUNT (cur_bb->succs) > 1)
1630 succ2 = EDGE_SUCC (cur_bb, 1);
1631 else
1632 succ2 = NULL;
1633
1634 /* We already took care of fall-through edges, so only one successor
1635 can be a crossing edge. */
1636
1637 if (succ1 && (succ1->flags & EDGE_CROSSING))
1638 crossing_edge = succ1;
1639 else if (succ2 && (succ2->flags & EDGE_CROSSING))
1640 crossing_edge = succ2;
1641
1642 if (crossing_edge)
1643 {
1644 old_jump = BB_END (cur_bb);
1645
1646 /* Check to make sure the jump instruction is a
1647 conditional jump. */
1648
1649 set_src = NULL_RTX;
1650
1651 if (any_condjump_p (old_jump))
1652 {
1653 if (GET_CODE (PATTERN (old_jump)) == SET)
1654 set_src = SET_SRC (PATTERN (old_jump));
1655 else if (GET_CODE (PATTERN (old_jump)) == PARALLEL)
1656 {
1657 set_src = XVECEXP (PATTERN (old_jump), 0,0);
1658 if (GET_CODE (set_src) == SET)
1659 set_src = SET_SRC (set_src);
1660 else
1661 set_src = NULL_RTX;
1662 }
1663 }
1664
1665 if (set_src && (GET_CODE (set_src) == IF_THEN_ELSE))
1666 {
1667 if (GET_CODE (XEXP (set_src, 1)) == PC)
1668 old_label = XEXP (set_src, 2);
1669 else if (GET_CODE (XEXP (set_src, 2)) == PC)
1670 old_label = XEXP (set_src, 1);
1671
1672 /* Check to see if new bb for jumping to that dest has
1673 already been created; if so, use it; if not, create
1674 a new one. */
1675
1676 new_bb = find_jump_block (crossing_edge->dest);
1677
1678 if (new_bb)
1679 new_label = block_label (new_bb);
1680 else
1681 {
1682 /* Create new basic block to be dest for
1683 conditional jump. */
1684
1685 new_bb = create_basic_block (NULL, NULL, last_bb);
1686 new_bb->rbi->next = last_bb->rbi->next;
1687 last_bb->rbi->next = new_bb;
1688 prev_bb = last_bb;
1689 last_bb = new_bb;
1690
1691 /* Update register liveness information. */
1692
1693 new_bb->global_live_at_start = ALLOC_REG_SET (&reg_obstack);
1694 new_bb->global_live_at_end = ALLOC_REG_SET (&reg_obstack);
1695 COPY_REG_SET (new_bb->global_live_at_end,
1696 prev_bb->global_live_at_end);
1697 COPY_REG_SET (new_bb->global_live_at_start,
1698 prev_bb->global_live_at_end);
1699
1700 /* Put appropriate instructions in new bb. */
1701
1702 new_label = gen_label_rtx ();
1703 emit_label_before (new_label, BB_HEAD (new_bb));
1704 BB_HEAD (new_bb) = new_label;
1705
1706 if (GET_CODE (old_label) == LABEL_REF)
1707 {
1708 old_label = JUMP_LABEL (old_jump);
1709 new_jump = emit_jump_insn_after (gen_jump
1710 (old_label),
1711 BB_END (new_bb));
1712 }
1713 else
1714 {
1715 gcc_assert (HAVE_return
1716 && GET_CODE (old_label) == RETURN);
1717 new_jump = emit_jump_insn_after (gen_return (),
1718 BB_END (new_bb));
1719 }
1720
1721 barrier = emit_barrier_after (new_jump);
1722 JUMP_LABEL (new_jump) = old_label;
1723 new_bb->rbi->footer = unlink_insn_chain (barrier,
1724 barrier);
1725
1726 /* Make sure new bb is in same partition as source
1727 of conditional branch. */
1728 BB_COPY_PARTITION (new_bb, cur_bb);
1729 }
1730
1731 /* Make old jump branch to new bb. */
1732
1733 redirect_jump (old_jump, new_label, 0);
1734
1735 /* Remove crossing_edge as predecessor of 'dest'. */
1736
1737 dest = crossing_edge->dest;
1738
1739 redirect_edge_succ (crossing_edge, new_bb);
1740
1741 /* Make a new edge from new_bb to old dest; new edge
1742 will be a successor for new_bb and a predecessor
1743 for 'dest'. */
1744
1745 if (EDGE_COUNT (new_bb->succs) == 0)
1746 new_edge = make_edge (new_bb, dest, 0);
1747 else
1748 new_edge = EDGE_SUCC (new_bb, 0);
1749
1750 crossing_edge->flags &= ~EDGE_CROSSING;
1751 new_edge->flags |= EDGE_CROSSING;
1752 }
1753 }
1754 }
1755 }
1756
1757 /* Find any unconditional branches that cross between hot and cold
1758 sections. Convert them into indirect jumps instead. */
1759
1760 static void
1761 fix_crossing_unconditional_branches (void)
1762 {
1763 basic_block cur_bb;
1764 rtx last_insn;
1765 rtx label;
1766 rtx label_addr;
1767 rtx indirect_jump_sequence;
1768 rtx jump_insn = NULL_RTX;
1769 rtx new_reg;
1770 rtx cur_insn;
1771 edge succ;
1772
1773 FOR_EACH_BB (cur_bb)
1774 {
1775 last_insn = BB_END (cur_bb);
1776 succ = EDGE_SUCC (cur_bb, 0);
1777
1778 /* Check to see if bb ends in a crossing (unconditional) jump. At
1779 this point, no crossing jumps should be conditional. */
1780
1781 if (JUMP_P (last_insn)
1782 && (succ->flags & EDGE_CROSSING))
1783 {
1784 rtx label2, table;
1785
1786 gcc_assert (!any_condjump_p (last_insn));
1787
1788 /* Make sure the jump is not already an indirect or table jump. */
1789
1790 if (!computed_jump_p (last_insn)
1791 && !tablejump_p (last_insn, &label2, &table))
1792 {
1793 /* We have found a "crossing" unconditional branch. Now
1794 we must convert it to an indirect jump. First create
1795 reference of label, as target for jump. */
1796
1797 label = JUMP_LABEL (last_insn);
1798 label_addr = gen_rtx_LABEL_REF (Pmode, label);
1799 LABEL_NUSES (label) += 1;
1800
1801 /* Get a register to use for the indirect jump. */
1802
1803 new_reg = gen_reg_rtx (Pmode);
1804
1805 /* Generate indirect the jump sequence. */
1806
1807 start_sequence ();
1808 emit_move_insn (new_reg, label_addr);
1809 emit_indirect_jump (new_reg);
1810 indirect_jump_sequence = get_insns ();
1811 end_sequence ();
1812
1813 /* Make sure every instruction in the new jump sequence has
1814 its basic block set to be cur_bb. */
1815
1816 for (cur_insn = indirect_jump_sequence; cur_insn;
1817 cur_insn = NEXT_INSN (cur_insn))
1818 {
1819 BLOCK_FOR_INSN (cur_insn) = cur_bb;
1820 if (JUMP_P (cur_insn))
1821 jump_insn = cur_insn;
1822 }
1823
1824 /* Insert the new (indirect) jump sequence immediately before
1825 the unconditional jump, then delete the unconditional jump. */
1826
1827 emit_insn_before (indirect_jump_sequence, last_insn);
1828 delete_insn (last_insn);
1829
1830 /* Make BB_END for cur_bb be the jump instruction (NOT the
1831 barrier instruction at the end of the sequence...). */
1832
1833 BB_END (cur_bb) = jump_insn;
1834 }
1835 }
1836 }
1837 }
1838
1839 /* Add REG_CROSSING_JUMP note to all crossing jump insns. */
1840
1841 static void
1842 add_reg_crossing_jump_notes (void)
1843 {
1844 basic_block bb;
1845 edge e;
1846 edge_iterator ei;
1847
1848 FOR_EACH_BB (bb)
1849 FOR_EACH_EDGE (e, ei, bb->succs)
1850 if ((e->flags & EDGE_CROSSING)
1851 && JUMP_P (BB_END (e->src)))
1852 REG_NOTES (BB_END (e->src)) = gen_rtx_EXPR_LIST (REG_CROSSING_JUMP,
1853 NULL_RTX,
1854 REG_NOTES (BB_END
1855 (e->src)));
1856 }
1857
1858 /* Basic blocks containing NOTE_INSN_UNLIKELY_EXECUTED_CODE will be
1859 put in a separate section of the .o file, to reduce paging and
1860 improve cache performance (hopefully). This can result in bits of
1861 code from the same function being widely separated in the .o file.
1862 However this is not obvious to the current bb structure. Therefore
1863 we must take care to ensure that: 1). There are no fall_thru edges
1864 that cross between sections; 2). For those architectures which
1865 have "short" conditional branches, all conditional branches that
1866 attempt to cross between sections are converted to unconditional
1867 branches; and, 3). For those architectures which have "short"
1868 unconditional branches, all unconditional branches that attempt
1869 to cross between sections are converted to indirect jumps.
1870
1871 The code for fixing up fall_thru edges that cross between hot and
1872 cold basic blocks does so by creating new basic blocks containing
1873 unconditional branches to the appropriate label in the "other"
1874 section. The new basic block is then put in the same (hot or cold)
1875 section as the original conditional branch, and the fall_thru edge
1876 is modified to fall into the new basic block instead. By adding
1877 this level of indirection we end up with only unconditional branches
1878 crossing between hot and cold sections.
1879
1880 Conditional branches are dealt with by adding a level of indirection.
1881 A new basic block is added in the same (hot/cold) section as the
1882 conditional branch, and the conditional branch is retargeted to the
1883 new basic block. The new basic block contains an unconditional branch
1884 to the original target of the conditional branch (in the other section).
1885
1886 Unconditional branches are dealt with by converting them into
1887 indirect jumps. */
1888
1889 static void
1890 fix_edges_for_rarely_executed_code (edge *crossing_edges,
1891 int n_crossing_edges)
1892 {
1893 /* Make sure the source of any crossing edge ends in a jump and the
1894 destination of any crossing edge has a label. */
1895
1896 add_labels_and_missing_jumps (crossing_edges, n_crossing_edges);
1897
1898 /* Convert all crossing fall_thru edges to non-crossing fall
1899 thrus to unconditional jumps (that jump to the original fall
1900 thru dest). */
1901
1902 fix_up_fall_thru_edges ();
1903
1904 /* Only do the parts necessary for writing separate sections if
1905 the target architecture has the ability to write separate sections
1906 (i.e. it has named sections). Otherwise, the hot/cold partitioning
1907 information will be used when reordering blocks to try to put all
1908 the hot blocks together, then all the cold blocks, but no actual
1909 section partitioning will be done. */
1910
1911 if (targetm.have_named_sections)
1912 {
1913 /* If the architecture does not have conditional branches that can
1914 span all of memory, convert crossing conditional branches into
1915 crossing unconditional branches. */
1916
1917 if (!HAS_LONG_COND_BRANCH)
1918 fix_crossing_conditional_branches ();
1919
1920 /* If the architecture does not have unconditional branches that
1921 can span all of memory, convert crossing unconditional branches
1922 into indirect jumps. Since adding an indirect jump also adds
1923 a new register usage, update the register usage information as
1924 well. */
1925
1926 if (!HAS_LONG_UNCOND_BRANCH)
1927 {
1928 fix_crossing_unconditional_branches ();
1929 reg_scan (get_insns(), max_reg_num (), 1);
1930 }
1931
1932 add_reg_crossing_jump_notes ();
1933 }
1934 }
1935
1936 /* Reorder basic blocks. The main entry point to this file. FLAGS is
1937 the set of flags to pass to cfg_layout_initialize(). */
1938
1939 void
1940 reorder_basic_blocks (unsigned int flags)
1941 {
1942 int n_traces;
1943 int i;
1944 struct trace *traces;
1945
1946 if (n_basic_blocks <= 1)
1947 return;
1948
1949 if (targetm.cannot_modify_jumps_p ())
1950 return;
1951
1952 timevar_push (TV_REORDER_BLOCKS);
1953
1954 cfg_layout_initialize (flags);
1955
1956 set_edge_can_fallthru_flag ();
1957 mark_dfs_back_edges ();
1958
1959 /* We are estimating the length of uncond jump insn only once since the code
1960 for getting the insn length always returns the minimal length now. */
1961 if (uncond_jump_length == 0)
1962 uncond_jump_length = get_uncond_jump_length ();
1963
1964 /* We need to know some information for each basic block. */
1965 array_size = GET_ARRAY_SIZE (last_basic_block);
1966 bbd = xmalloc (array_size * sizeof (bbro_basic_block_data));
1967 for (i = 0; i < array_size; i++)
1968 {
1969 bbd[i].start_of_trace = -1;
1970 bbd[i].end_of_trace = -1;
1971 bbd[i].heap = NULL;
1972 bbd[i].node = NULL;
1973 }
1974
1975 traces = xmalloc (n_basic_blocks * sizeof (struct trace));
1976 n_traces = 0;
1977 find_traces (&n_traces, traces);
1978 connect_traces (n_traces, traces);
1979 FREE (traces);
1980 FREE (bbd);
1981
1982 if (dump_file)
1983 dump_flow_info (dump_file);
1984
1985 if (flag_reorder_blocks_and_partition
1986 && targetm.have_named_sections)
1987 add_unlikely_executed_notes ();
1988
1989 cfg_layout_finalize ();
1990
1991 timevar_pop (TV_REORDER_BLOCKS);
1992 }
1993
1994 /* This function is the main 'entrance' for the optimization that
1995 partitions hot and cold basic blocks into separate sections of the
1996 .o file (to improve performance and cache locality). Ideally it
1997 would be called after all optimizations that rearrange the CFG have
1998 been called. However part of this optimization may introduce new
1999 register usage, so it must be called before register allocation has
2000 occurred. This means that this optimization is actually called
2001 well before the optimization that reorders basic blocks (see
2002 function above).
2003
2004 This optimization checks the feedback information to determine
2005 which basic blocks are hot/cold and causes reorder_basic_blocks to
2006 add NOTE_INSN_UNLIKELY_EXECUTED_CODE to non-hot basic blocks. The
2007 presence or absence of this note is later used for writing out
2008 sections in the .o file. Because hot and cold sections can be
2009 arbitrarily large (within the bounds of memory), far beyond the
2010 size of a single function, it is necessary to fix up all edges that
2011 cross section boundaries, to make sure the instructions used can
2012 actually span the required distance. The fixes are described
2013 below.
2014
2015 Fall-through edges must be changed into jumps; it is not safe or
2016 legal to fall through across a section boundary. Whenever a
2017 fall-through edge crossing a section boundary is encountered, a new
2018 basic block is inserted (in the same section as the fall-through
2019 source), and the fall through edge is redirected to the new basic
2020 block. The new basic block contains an unconditional jump to the
2021 original fall-through target. (If the unconditional jump is
2022 insufficient to cross section boundaries, that is dealt with a
2023 little later, see below).
2024
2025 In order to deal with architectures that have short conditional
2026 branches (which cannot span all of memory) we take any conditional
2027 jump that attempts to cross a section boundary and add a level of
2028 indirection: it becomes a conditional jump to a new basic block, in
2029 the same section. The new basic block contains an unconditional
2030 jump to the original target, in the other section.
2031
2032 For those architectures whose unconditional branch is also
2033 incapable of reaching all of memory, those unconditional jumps are
2034 converted into indirect jumps, through a register.
2035
2036 IMPORTANT NOTE: This optimization causes some messy interactions
2037 with the cfg cleanup optimizations; those optimizations want to
2038 merge blocks wherever possible, and to collapse indirect jump
2039 sequences (change "A jumps to B jumps to C" directly into "A jumps
2040 to C"). Those optimizations can undo the jump fixes that
2041 partitioning is required to make (see above), in order to ensure
2042 that jumps attempting to cross section boundaries are really able
2043 to cover whatever distance the jump requires (on many architectures
2044 conditional or unconditional jumps are not able to reach all of
2045 memory). Therefore tests have to be inserted into each such
2046 optimization to make sure that it does not undo stuff necessary to
2047 cross partition boundaries. This would be much less of a problem
2048 if we could perform this optimization later in the compilation, but
2049 unfortunately the fact that we may need to create indirect jumps
2050 (through registers) requires that this optimization be performed
2051 before register allocation. */
2052
2053 void
2054 partition_hot_cold_basic_blocks (void)
2055 {
2056 basic_block cur_bb;
2057 edge *crossing_edges;
2058 int n_crossing_edges;
2059 int max_edges = 2 * last_basic_block;
2060
2061 if (n_basic_blocks <= 1)
2062 return;
2063
2064 crossing_edges = xcalloc (max_edges, sizeof (edge));
2065
2066 cfg_layout_initialize (0);
2067
2068 FOR_EACH_BB (cur_bb)
2069 if (cur_bb->index >= 0
2070 && cur_bb->next_bb->index >= 0)
2071 cur_bb->rbi->next = cur_bb->next_bb;
2072
2073 find_rarely_executed_basic_blocks_and_crossing_edges (crossing_edges,
2074 &n_crossing_edges,
2075 &max_edges);
2076
2077 if (n_crossing_edges > 0)
2078 fix_edges_for_rarely_executed_code (crossing_edges, n_crossing_edges);
2079
2080 free (crossing_edges);
2081
2082 cfg_layout_finalize();
2083 }