1 /* Integrated Register Allocator. Changing code and generating moves.
2 Copyright (C) 2006-2014 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* When we have more one region, we need to change the original RTL
22 code after coloring. Let us consider two allocnos representing the
23 same pseudo-register outside and inside a region respectively.
24 They can get different hard-registers. The reload pass works on
25 pseudo registers basis and there is no way to say the reload that
26 pseudo could be in different registers and it is even more
27 difficult to say in what places of the code the pseudo should have
28 particular hard-registers. So in this case IRA has to create and
29 use a new pseudo-register inside the region and adds code to move
30 allocno values on the region's borders. This is done by the code
33 The code makes top-down traversal of the regions and generate new
34 pseudos and the move code on the region borders. In some
35 complicated cases IRA can create a new pseudo used temporarily to
36 move allocno values when a swap of values stored in two
37 hard-registers is needed (e.g. two allocnos representing different
38 pseudos outside region got respectively hard registers 1 and 2 and
39 the corresponding allocnos inside the region got respectively hard
40 registers 2 and 1). At this stage, the new pseudo is marked as
43 IRA still creates the pseudo-register and the moves on the region
44 borders even when the both corresponding allocnos were assigned to
45 the same hard-register. It is done because, if the reload pass for
46 some reason spills a pseudo-register representing the original
47 pseudo outside or inside the region, the effect will be smaller
48 because another pseudo will still be in the hard-register. In most
49 cases, this is better then spilling the original pseudo in its
50 whole live-range. If reload does not change the allocation for the
51 two pseudo-registers, the trivial move will be removed by
52 post-reload optimizations.
54 IRA does not generate a new pseudo and moves for the allocno values
55 if the both allocnos representing an original pseudo inside and
56 outside region assigned to the same hard register when the register
57 pressure in the region for the corresponding pressure class is less
58 than number of available hard registers for given pressure class.
60 IRA also does some optimizations to remove redundant moves which is
61 transformed into stores by the reload pass on CFG edges
62 representing exits from the region.
64 IRA tries to reduce duplication of code generated on CFG edges
65 which are enters and exits to/from regions by moving some code to
66 the edge sources or destinations when it is possible. */
70 #include "coretypes.h"
79 #include "hard-reg-set.h"
80 #include "basic-block.h"
89 /* Data used to emit live range split insns and to flattening IR. */
90 ira_emit_data_t ira_allocno_emit_data
;
92 /* Definitions for vectors of pointers. */
95 /* Pointers to data allocated for allocnos being created during
96 emitting. Usually there are quite few such allocnos because they
97 are created only for resolving loop in register shuffling. */
98 static vec
<void_p
> new_allocno_emit_data_vec
;
100 /* Allocate and initiate the emit data. */
102 ira_initiate_emit_data (void)
105 ira_allocno_iterator ai
;
107 ira_allocno_emit_data
108 = (ira_emit_data_t
) ira_allocate (ira_allocnos_num
109 * sizeof (struct ira_emit_data
));
110 memset (ira_allocno_emit_data
, 0,
111 ira_allocnos_num
* sizeof (struct ira_emit_data
));
112 FOR_EACH_ALLOCNO (a
, ai
)
113 ALLOCNO_ADD_DATA (a
) = ira_allocno_emit_data
+ ALLOCNO_NUM (a
);
114 new_allocno_emit_data_vec
.create (50);
118 /* Free the emit data. */
120 ira_finish_emit_data (void)
124 ira_allocno_iterator ai
;
126 ira_free (ira_allocno_emit_data
);
127 FOR_EACH_ALLOCNO (a
, ai
)
128 ALLOCNO_ADD_DATA (a
) = NULL
;
129 for (;new_allocno_emit_data_vec
.length () != 0;)
131 p
= new_allocno_emit_data_vec
.pop ();
134 new_allocno_emit_data_vec
.release ();
137 /* Create and return a new allocno with given REGNO and
138 LOOP_TREE_NODE. Allocate emit data for it. */
140 create_new_allocno (int regno
, ira_loop_tree_node_t loop_tree_node
)
144 a
= ira_create_allocno (regno
, false, loop_tree_node
);
145 ALLOCNO_ADD_DATA (a
) = ira_allocate (sizeof (struct ira_emit_data
));
146 memset (ALLOCNO_ADD_DATA (a
), 0, sizeof (struct ira_emit_data
));
147 new_allocno_emit_data_vec
.safe_push (ALLOCNO_ADD_DATA (a
));
153 /* See comments below. */
154 typedef struct move
*move_t
;
156 /* The structure represents an allocno move. Both allocnos have the
157 same original regno but different allocation. */
160 /* The allocnos involved in the move. */
161 ira_allocno_t from
, to
;
162 /* The next move in the move sequence. */
164 /* Used for finding dependencies. */
166 /* The size of the following array. */
168 /* Moves on which given move depends on. Dependency can be cyclic.
169 It means we need a temporary to generates the moves. Sequence
170 A1->A2, B1->B2 where A1 and B2 are assigned to reg R1 and A2 and
171 B1 are assigned to reg R2 is an example of the cyclic
174 /* First insn generated for the move. */
178 /* Array of moves (indexed by BB index) which should be put at the
179 start/end of the corresponding basic blocks. */
180 static move_t
*at_bb_start
, *at_bb_end
;
182 /* Max regno before renaming some pseudo-registers. For example, the
183 same pseudo-register can be renamed in a loop if its allocation is
184 different outside the loop. */
185 static int max_regno_before_changing
;
187 /* Return new move of allocnos TO and FROM. */
189 create_move (ira_allocno_t to
, ira_allocno_t from
)
193 move
= (move_t
) ira_allocate (sizeof (struct move
));
200 move
->visited_p
= false;
204 /* Free memory for MOVE and its dependencies. */
206 free_move (move_t move
)
208 if (move
->deps
!= NULL
)
209 ira_free (move
->deps
);
213 /* Free memory for list of the moves given by its HEAD. */
215 free_move_list (move_t head
)
219 for (; head
!= NULL
; head
= next
)
226 /* Return TRUE if the move list LIST1 and LIST2 are equal (two
227 moves are equal if they involve the same allocnos). */
229 eq_move_lists_p (move_t list1
, move_t list2
)
231 for (; list1
!= NULL
&& list2
!= NULL
;
232 list1
= list1
->next
, list2
= list2
->next
)
233 if (list1
->from
!= list2
->from
|| list1
->to
!= list2
->to
)
235 return list1
== list2
;
238 /* Print move list LIST into file F. */
240 print_move_list (FILE *f
, move_t list
)
242 for (; list
!= NULL
; list
= list
->next
)
243 fprintf (f
, " a%dr%d->a%dr%d",
244 ALLOCNO_NUM (list
->from
), ALLOCNO_REGNO (list
->from
),
245 ALLOCNO_NUM (list
->to
), ALLOCNO_REGNO (list
->to
));
249 extern void ira_debug_move_list (move_t list
);
251 /* Print move list LIST into stderr. */
253 ira_debug_move_list (move_t list
)
255 print_move_list (stderr
, list
);
258 /* This recursive function changes pseudo-registers in *LOC if it is
259 necessary. The function returns TRUE if a change was done. */
261 change_regs (rtx
*loc
)
263 int i
, regno
, result
= false;
268 if (*loc
== NULL_RTX
)
270 code
= GET_CODE (*loc
);
273 regno
= REGNO (*loc
);
274 if (regno
< FIRST_PSEUDO_REGISTER
)
276 if (regno
>= max_regno_before_changing
)
277 /* It is a shared register which was changed already. */
279 if (ira_curr_regno_allocno_map
[regno
] == NULL
)
281 reg
= allocno_emit_reg (ira_curr_regno_allocno_map
[regno
]);
288 fmt
= GET_RTX_FORMAT (code
);
289 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
292 result
= change_regs (&XEXP (*loc
, i
)) || result
;
293 else if (fmt
[i
] == 'E')
297 for (j
= XVECLEN (*loc
, i
) - 1; j
>= 0; j
--)
298 result
= change_regs (&XVECEXP (*loc
, i
, j
)) || result
;
305 change_regs_in_insn (rtx_insn
**insn_ptr
)
308 bool result
= change_regs (&rtx
);
309 *insn_ptr
= as_a
<rtx_insn
*> (rtx
);
313 /* Attach MOVE to the edge E. The move is attached to the head of the
314 list if HEAD_P is TRUE. */
316 add_to_edge_list (edge e
, move_t move
, bool head_p
)
320 if (head_p
|| e
->aux
== NULL
)
322 move
->next
= (move_t
) e
->aux
;
327 for (last
= (move_t
) e
->aux
; last
->next
!= NULL
; last
= last
->next
)
334 /* Create and return new pseudo-register with the same attributes as
337 ira_create_new_reg (rtx original_reg
)
341 new_reg
= gen_reg_rtx (GET_MODE (original_reg
));
342 ORIGINAL_REGNO (new_reg
) = ORIGINAL_REGNO (original_reg
);
343 REG_USERVAR_P (new_reg
) = REG_USERVAR_P (original_reg
);
344 REG_POINTER (new_reg
) = REG_POINTER (original_reg
);
345 REG_ATTRS (new_reg
) = REG_ATTRS (original_reg
);
346 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
347 fprintf (ira_dump_file
, " Creating newreg=%i from oldreg=%i\n",
348 REGNO (new_reg
), REGNO (original_reg
));
349 ira_expand_reg_equiv ();
353 /* Return TRUE if loop given by SUBNODE inside the loop given by
356 subloop_tree_node_p (ira_loop_tree_node_t subnode
, ira_loop_tree_node_t node
)
358 for (; subnode
!= NULL
; subnode
= subnode
->parent
)
364 /* Set up member `reg' to REG for allocnos which has the same regno as
365 ALLOCNO and which are inside the loop corresponding to ALLOCNO. */
367 set_allocno_reg (ira_allocno_t allocno
, rtx reg
)
371 ira_loop_tree_node_t node
;
373 node
= ALLOCNO_LOOP_TREE_NODE (allocno
);
374 for (a
= ira_regno_allocno_map
[ALLOCNO_REGNO (allocno
)];
376 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
377 if (subloop_tree_node_p (ALLOCNO_LOOP_TREE_NODE (a
), node
))
378 ALLOCNO_EMIT_DATA (a
)->reg
= reg
;
379 for (a
= ALLOCNO_CAP (allocno
); a
!= NULL
; a
= ALLOCNO_CAP (a
))
380 ALLOCNO_EMIT_DATA (a
)->reg
= reg
;
381 regno
= ALLOCNO_REGNO (allocno
);
384 if (a
== NULL
|| (a
= ALLOCNO_CAP (a
)) == NULL
)
389 a
= node
->regno_allocno_map
[regno
];
393 if (ALLOCNO_EMIT_DATA (a
)->child_renamed_p
)
395 ALLOCNO_EMIT_DATA (a
)->child_renamed_p
= true;
399 /* Return true if there is an entry to given loop not from its parent
400 (or grandparent) block. For example, it is possible for two
401 adjacent loops inside another loop. */
403 entered_from_non_parent_p (ira_loop_tree_node_t loop_node
)
405 ira_loop_tree_node_t bb_node
, src_loop_node
, parent
;
409 for (bb_node
= loop_node
->children
;
411 bb_node
= bb_node
->next
)
412 if (bb_node
->bb
!= NULL
)
414 FOR_EACH_EDGE (e
, ei
, bb_node
->bb
->preds
)
415 if (e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
416 && (src_loop_node
= IRA_BB_NODE (e
->src
)->parent
) != loop_node
)
418 for (parent
= src_loop_node
->parent
;
420 parent
= parent
->parent
)
421 if (parent
== loop_node
)
424 /* That is an exit from a nested loop -- skip it. */
426 for (parent
= loop_node
->parent
;
428 parent
= parent
->parent
)
429 if (src_loop_node
== parent
)
438 /* Set up ENTERED_FROM_NON_PARENT_P for each loop region. */
440 setup_entered_from_non_parent_p (void)
445 ira_assert (current_loops
!= NULL
);
446 FOR_EACH_VEC_SAFE_ELT (get_loops (cfun
), i
, loop
)
447 if (ira_loop_nodes
[i
].regno_allocno_map
!= NULL
)
448 ira_loop_nodes
[i
].entered_from_non_parent_p
449 = entered_from_non_parent_p (&ira_loop_nodes
[i
]);
452 /* Return TRUE if move of SRC_ALLOCNO (assigned to hard register) to
453 DEST_ALLOCNO (assigned to memory) can be removed because it does
454 not change value of the destination. One possible reason for this
455 is the situation when SRC_ALLOCNO is not modified in the
456 corresponding loop. */
458 store_can_be_removed_p (ira_allocno_t src_allocno
, ira_allocno_t dest_allocno
)
460 int regno
, orig_regno
;
462 ira_loop_tree_node_t node
;
464 ira_assert (ALLOCNO_CAP_MEMBER (src_allocno
) == NULL
465 && ALLOCNO_CAP_MEMBER (dest_allocno
) == NULL
);
466 orig_regno
= ALLOCNO_REGNO (src_allocno
);
467 regno
= REGNO (allocno_emit_reg (dest_allocno
));
468 for (node
= ALLOCNO_LOOP_TREE_NODE (src_allocno
);
472 a
= node
->regno_allocno_map
[orig_regno
];
473 ira_assert (a
!= NULL
);
474 if (REGNO (allocno_emit_reg (a
)) == (unsigned) regno
)
475 /* We achieved the destination and everything is ok. */
477 else if (bitmap_bit_p (node
->modified_regnos
, orig_regno
))
479 else if (node
->entered_from_non_parent_p
)
480 /* If there is a path from a destination loop block to the
481 source loop header containing basic blocks of non-parents
482 (grandparents) of the source loop, we should have checked
483 modifications of the pseudo on this path too to decide
484 about possibility to remove the store. It could be done by
485 solving a data-flow problem. Unfortunately such global
486 solution would complicate IR flattening. Therefore we just
487 prohibit removal of the store in such complicated case. */
490 /* It is actually a loop entry -- do not remove the store. */
494 /* Generate and attach moves to the edge E. This looks at the final
495 regnos of allocnos living on the edge with the same original regno
496 to figure out when moves should be generated. */
498 generate_edge_moves (edge e
)
500 ira_loop_tree_node_t src_loop_node
, dest_loop_node
;
503 ira_allocno_t src_allocno
, dest_allocno
, *src_map
, *dest_map
;
505 bitmap regs_live_in_dest
, regs_live_out_src
;
507 src_loop_node
= IRA_BB_NODE (e
->src
)->parent
;
508 dest_loop_node
= IRA_BB_NODE (e
->dest
)->parent
;
510 if (src_loop_node
== dest_loop_node
)
512 src_map
= src_loop_node
->regno_allocno_map
;
513 dest_map
= dest_loop_node
->regno_allocno_map
;
514 regs_live_in_dest
= df_get_live_in (e
->dest
);
515 regs_live_out_src
= df_get_live_out (e
->src
);
516 EXECUTE_IF_SET_IN_REG_SET (regs_live_in_dest
,
517 FIRST_PSEUDO_REGISTER
, regno
, bi
)
518 if (bitmap_bit_p (regs_live_out_src
, regno
))
520 src_allocno
= src_map
[regno
];
521 dest_allocno
= dest_map
[regno
];
522 if (REGNO (allocno_emit_reg (src_allocno
))
523 == REGNO (allocno_emit_reg (dest_allocno
)))
525 /* Remove unnecessary stores at the region exit. We should do
526 this for readonly memory for sure and this is guaranteed by
527 that we never generate moves on region borders (see
528 checking in function change_loop). */
529 if (ALLOCNO_HARD_REGNO (dest_allocno
) < 0
530 && ALLOCNO_HARD_REGNO (src_allocno
) >= 0
531 && store_can_be_removed_p (src_allocno
, dest_allocno
))
533 ALLOCNO_EMIT_DATA (src_allocno
)->mem_optimized_dest
= dest_allocno
;
534 ALLOCNO_EMIT_DATA (dest_allocno
)->mem_optimized_dest_p
= true;
535 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
536 fprintf (ira_dump_file
, " Remove r%d:a%d->a%d(mem)\n",
537 regno
, ALLOCNO_NUM (src_allocno
),
538 ALLOCNO_NUM (dest_allocno
));
541 move
= create_move (dest_allocno
, src_allocno
);
542 add_to_edge_list (e
, move
, true);
546 /* Bitmap of allocnos local for the current loop. */
547 static bitmap local_allocno_bitmap
;
549 /* This bitmap is used to find that we need to generate and to use a
550 new pseudo-register when processing allocnos with the same original
552 static bitmap used_regno_bitmap
;
554 /* This bitmap contains regnos of allocnos which were renamed locally
555 because the allocnos correspond to disjoint live ranges in loops
556 with a common parent. */
557 static bitmap renamed_regno_bitmap
;
559 /* Change (if necessary) pseudo-registers inside loop given by loop
562 change_loop (ira_loop_tree_node_t node
)
568 ira_allocno_t allocno
, parent_allocno
, *map
;
571 enum reg_class aclass
, pclass
;
572 ira_loop_tree_node_t parent
;
574 if (node
!= ira_loop_tree_root
)
576 ira_assert (current_loops
!= NULL
);
578 if (node
->bb
!= NULL
)
580 FOR_BB_INSNS (node
->bb
, insn
)
581 if (INSN_P (insn
) && change_regs_in_insn (&insn
))
583 df_insn_rescan (insn
);
584 df_notes_rescan (insn
);
589 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
590 fprintf (ira_dump_file
,
591 " Changing RTL for loop %d (header bb%d)\n",
592 node
->loop_num
, node
->loop
->header
->index
);
594 parent
= ira_curr_loop_tree_node
->parent
;
595 map
= parent
->regno_allocno_map
;
596 EXECUTE_IF_SET_IN_REG_SET (ira_curr_loop_tree_node
->border_allocnos
,
599 allocno
= ira_allocnos
[i
];
600 regno
= ALLOCNO_REGNO (allocno
);
601 aclass
= ALLOCNO_CLASS (allocno
);
602 pclass
= ira_pressure_class_translate
[aclass
];
603 parent_allocno
= map
[regno
];
604 ira_assert (regno
< ira_reg_equiv_len
);
605 /* We generate the same hard register move because the
606 reload pass can put an allocno into memory in this case
607 we will have live range splitting. If it does not happen
608 such the same hard register moves will be removed. The
609 worst case when the both allocnos are put into memory by
610 the reload is very rare. */
611 if (parent_allocno
!= NULL
612 && (ALLOCNO_HARD_REGNO (allocno
)
613 == ALLOCNO_HARD_REGNO (parent_allocno
))
614 && (ALLOCNO_HARD_REGNO (allocno
) < 0
615 || (parent
->reg_pressure
[pclass
] + 1
616 <= ira_class_hard_regs_num
[pclass
])
617 || TEST_HARD_REG_BIT (ira_prohibited_mode_move_regs
618 [ALLOCNO_MODE (allocno
)],
619 ALLOCNO_HARD_REGNO (allocno
))
620 /* don't create copies because reload can spill an
621 allocno set by copy although the allocno will not
623 || ira_equiv_no_lvalue_p (regno
)))
625 original_reg
= allocno_emit_reg (allocno
);
626 if (parent_allocno
== NULL
627 || (REGNO (allocno_emit_reg (parent_allocno
))
628 == REGNO (original_reg
)))
630 if (internal_flag_ira_verbose
> 3 && ira_dump_file
)
631 fprintf (ira_dump_file
, " %i vs parent %i:",
632 ALLOCNO_HARD_REGNO (allocno
),
633 ALLOCNO_HARD_REGNO (parent_allocno
));
634 set_allocno_reg (allocno
, ira_create_new_reg (original_reg
));
638 /* Rename locals: Local allocnos with same regno in different loops
639 might get the different hard register. So we need to change
641 bitmap_and_compl (local_allocno_bitmap
,
642 ira_curr_loop_tree_node
->all_allocnos
,
643 ira_curr_loop_tree_node
->border_allocnos
);
644 EXECUTE_IF_SET_IN_REG_SET (local_allocno_bitmap
, 0, i
, bi
)
646 allocno
= ira_allocnos
[i
];
647 regno
= ALLOCNO_REGNO (allocno
);
648 if (ALLOCNO_CAP_MEMBER (allocno
) != NULL
)
650 used_p
= !bitmap_set_bit (used_regno_bitmap
, regno
);
651 ALLOCNO_EMIT_DATA (allocno
)->somewhere_renamed_p
= true;
654 bitmap_set_bit (renamed_regno_bitmap
, regno
);
655 set_allocno_reg (allocno
, ira_create_new_reg (allocno_emit_reg (allocno
)));
659 /* Process to set up flag somewhere_renamed_p. */
661 set_allocno_somewhere_renamed_p (void)
664 ira_allocno_t allocno
;
665 ira_allocno_iterator ai
;
667 FOR_EACH_ALLOCNO (allocno
, ai
)
669 regno
= ALLOCNO_REGNO (allocno
);
670 if (bitmap_bit_p (renamed_regno_bitmap
, regno
)
671 && REGNO (allocno_emit_reg (allocno
)) == regno
)
672 ALLOCNO_EMIT_DATA (allocno
)->somewhere_renamed_p
= true;
676 /* Return TRUE if move lists on all edges given in vector VEC are
679 eq_edge_move_lists_p (vec
<edge
, va_gc
> *vec
)
684 list
= (move_t
) EDGE_I (vec
, 0)->aux
;
685 for (i
= EDGE_COUNT (vec
) - 1; i
> 0; i
--)
686 if (! eq_move_lists_p (list
, (move_t
) EDGE_I (vec
, i
)->aux
))
691 /* Look at all entry edges (if START_P) or exit edges of basic block
692 BB and put move lists at the BB start or end if it is possible. In
693 other words, this decreases code duplication of allocno moves. */
695 unify_moves (basic_block bb
, bool start_p
)
700 vec
<edge
, va_gc
> *vec
;
702 vec
= (start_p
? bb
->preds
: bb
->succs
);
703 if (EDGE_COUNT (vec
) == 0 || ! eq_edge_move_lists_p (vec
))
706 list
= (move_t
) e
->aux
;
707 if (! start_p
&& control_flow_insn_p (BB_END (bb
)))
710 for (i
= EDGE_COUNT (vec
) - 1; i
> 0; i
--)
713 free_move_list ((move_t
) e
->aux
);
717 at_bb_start
[bb
->index
] = list
;
719 at_bb_end
[bb
->index
] = list
;
722 /* Last move (in move sequence being processed) setting up the
723 corresponding hard register. */
724 static move_t hard_regno_last_set
[FIRST_PSEUDO_REGISTER
];
726 /* If the element value is equal to CURR_TICK then the corresponding
727 element in `hard_regno_last_set' is defined and correct. */
728 static int hard_regno_last_set_check
[FIRST_PSEUDO_REGISTER
];
730 /* Last move (in move sequence being processed) setting up the
731 corresponding allocno. */
732 static move_t
*allocno_last_set
;
734 /* If the element value is equal to CURR_TICK then the corresponding
735 element in . `allocno_last_set' is defined and correct. */
736 static int *allocno_last_set_check
;
738 /* Definition of vector of moves. */
740 /* This vec contains moves sorted topologically (depth-first) on their
742 static vec
<move_t
> move_vec
;
744 /* The variable value is used to check correctness of values of
745 elements of arrays `hard_regno_last_set' and
746 `allocno_last_set_check'. */
747 static int curr_tick
;
749 /* This recursive function traverses dependencies of MOVE and produces
750 topological sorting (in depth-first order). */
752 traverse_moves (move_t move
)
758 move
->visited_p
= true;
759 for (i
= move
->deps_num
- 1; i
>= 0; i
--)
760 traverse_moves (move
->deps
[i
]);
761 move_vec
.safe_push (move
);
764 /* Remove unnecessary moves in the LIST, makes topological sorting,
765 and removes cycles on hard reg dependencies by introducing new
766 allocnos assigned to memory and additional moves. It returns the
769 modify_move_list (move_t list
)
771 int i
, n
, nregs
, hard_regno
;
772 ira_allocno_t to
, from
;
773 move_t move
, new_move
, set_move
, first
, last
;
777 /* Creat move deps. */
779 for (move
= list
; move
!= NULL
; move
= move
->next
)
782 if ((hard_regno
= ALLOCNO_HARD_REGNO (to
)) < 0)
784 nregs
= hard_regno_nregs
[hard_regno
][ALLOCNO_MODE (to
)];
785 for (i
= 0; i
< nregs
; i
++)
787 hard_regno_last_set
[hard_regno
+ i
] = move
;
788 hard_regno_last_set_check
[hard_regno
+ i
] = curr_tick
;
791 for (move
= list
; move
!= NULL
; move
= move
->next
)
795 if ((hard_regno
= ALLOCNO_HARD_REGNO (from
)) >= 0)
797 nregs
= hard_regno_nregs
[hard_regno
][ALLOCNO_MODE (from
)];
798 for (n
= i
= 0; i
< nregs
; i
++)
799 if (hard_regno_last_set_check
[hard_regno
+ i
] == curr_tick
800 && (ALLOCNO_REGNO (hard_regno_last_set
[hard_regno
+ i
]->to
)
801 != ALLOCNO_REGNO (from
)))
803 move
->deps
= (move_t
*) ira_allocate (n
* sizeof (move_t
));
804 for (n
= i
= 0; i
< nregs
; i
++)
805 if (hard_regno_last_set_check
[hard_regno
+ i
] == curr_tick
806 && (ALLOCNO_REGNO (hard_regno_last_set
[hard_regno
+ i
]->to
)
807 != ALLOCNO_REGNO (from
)))
808 move
->deps
[n
++] = hard_regno_last_set
[hard_regno
+ i
];
812 /* Toplogical sorting: */
813 move_vec
.truncate (0);
814 for (move
= list
; move
!= NULL
; move
= move
->next
)
815 traverse_moves (move
);
817 for (i
= (int) move_vec
.length () - 1; i
>= 0; i
--)
825 first
= move_vec
.last ();
826 /* Removing cycles: */
828 move_vec
.truncate (0);
829 for (move
= first
; move
!= NULL
; move
= move
->next
)
833 if ((hard_regno
= ALLOCNO_HARD_REGNO (from
)) >= 0)
835 nregs
= hard_regno_nregs
[hard_regno
][ALLOCNO_MODE (from
)];
836 for (i
= 0; i
< nregs
; i
++)
837 if (hard_regno_last_set_check
[hard_regno
+ i
] == curr_tick
838 && ALLOCNO_HARD_REGNO
839 (hard_regno_last_set
[hard_regno
+ i
]->to
) >= 0)
842 ira_allocno_t new_allocno
;
844 set_move
= hard_regno_last_set
[hard_regno
+ i
];
845 /* It does not matter what loop_tree_node (of TO or
846 FROM) to use for the new allocno because of
847 subsequent IRA internal representation
850 = create_new_allocno (ALLOCNO_REGNO (set_move
->to
),
851 ALLOCNO_LOOP_TREE_NODE (set_move
->to
));
852 ALLOCNO_MODE (new_allocno
) = ALLOCNO_MODE (set_move
->to
);
853 ira_set_allocno_class (new_allocno
,
854 ALLOCNO_CLASS (set_move
->to
));
855 ira_create_allocno_objects (new_allocno
);
856 ALLOCNO_ASSIGNED_P (new_allocno
) = true;
857 ALLOCNO_HARD_REGNO (new_allocno
) = -1;
858 ALLOCNO_EMIT_DATA (new_allocno
)->reg
859 = ira_create_new_reg (allocno_emit_reg (set_move
->to
));
861 /* Make it possibly conflicting with all earlier
862 created allocnos. Cases where temporary allocnos
863 created to remove the cycles are quite rare. */
864 n
= ALLOCNO_NUM_OBJECTS (new_allocno
);
865 gcc_assert (n
== ALLOCNO_NUM_OBJECTS (set_move
->to
));
866 for (j
= 0; j
< n
; j
++)
868 ira_object_t new_obj
= ALLOCNO_OBJECT (new_allocno
, j
);
870 OBJECT_MIN (new_obj
) = 0;
871 OBJECT_MAX (new_obj
) = ira_objects_num
- 1;
874 new_move
= create_move (set_move
->to
, new_allocno
);
875 set_move
->to
= new_allocno
;
876 move_vec
.safe_push (new_move
);
877 ira_move_loops_num
++;
878 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
879 fprintf (ira_dump_file
,
880 " Creating temporary allocno a%dr%d\n",
881 ALLOCNO_NUM (new_allocno
),
882 REGNO (allocno_emit_reg (new_allocno
)));
885 if ((hard_regno
= ALLOCNO_HARD_REGNO (to
)) < 0)
887 nregs
= hard_regno_nregs
[hard_regno
][ALLOCNO_MODE (to
)];
888 for (i
= 0; i
< nregs
; i
++)
890 hard_regno_last_set
[hard_regno
+ i
] = move
;
891 hard_regno_last_set_check
[hard_regno
+ i
] = curr_tick
;
894 for (i
= (int) move_vec
.length () - 1; i
>= 0; i
--)
904 /* Generate RTX move insns from the move list LIST. This updates
905 allocation cost using move execution frequency FREQ. */
907 emit_move_list (move_t list
, int freq
)
910 int to_regno
, from_regno
, cost
, regno
;
911 rtx_insn
*result
, *insn
;
913 enum machine_mode mode
;
914 enum reg_class aclass
;
918 for (; list
!= NULL
; list
= list
->next
)
921 to
= allocno_emit_reg (list
->to
);
922 to_regno
= REGNO (to
);
923 from
= allocno_emit_reg (list
->from
);
924 from_regno
= REGNO (from
);
925 emit_move_insn (to
, from
);
926 list
->insn
= get_insns ();
928 for (insn
= list
->insn
; insn
!= NULL_RTX
; insn
= NEXT_INSN (insn
))
930 /* The reload needs to have set up insn codes. If the
931 reload sets up insn codes by itself, it may fail because
932 insns will have hard registers instead of pseudos and
933 there may be no machine insn with given hard
935 recog_memoized (insn
);
936 /* Add insn to equiv init insn list if it is necessary.
937 Otherwise reload will not remove this insn if it decides
938 to use the equivalence. */
939 if ((set
= single_set (insn
)) != NULL_RTX
)
941 dest
= SET_DEST (set
);
942 if (GET_CODE (dest
) == SUBREG
)
943 dest
= SUBREG_REG (dest
);
944 ira_assert (REG_P (dest
));
945 regno
= REGNO (dest
);
946 if (regno
>= ira_reg_equiv_len
947 || (ira_reg_equiv
[regno
].invariant
== NULL_RTX
948 && ira_reg_equiv
[regno
].constant
== NULL_RTX
))
949 continue; /* regno has no equivalence. */
950 ira_assert ((int) reg_equivs
->length () > regno
);
951 reg_equiv_init (regno
)
952 = gen_rtx_INSN_LIST (VOIDmode
, insn
, reg_equiv_init (regno
));
956 ira_update_equiv_info_by_shuffle_insn (to_regno
, from_regno
, list
->insn
);
957 emit_insn (list
->insn
);
958 mode
= ALLOCNO_MODE (list
->to
);
959 aclass
= ALLOCNO_CLASS (list
->to
);
961 if (ALLOCNO_HARD_REGNO (list
->to
) < 0)
963 if (ALLOCNO_HARD_REGNO (list
->from
) >= 0)
965 cost
= ira_memory_move_cost
[mode
][aclass
][0] * freq
;
966 ira_store_cost
+= cost
;
969 else if (ALLOCNO_HARD_REGNO (list
->from
) < 0)
971 if (ALLOCNO_HARD_REGNO (list
->to
) >= 0)
973 cost
= ira_memory_move_cost
[mode
][aclass
][0] * freq
;
974 ira_load_cost
+= cost
;
979 ira_init_register_move_cost_if_necessary (mode
);
980 cost
= ira_register_move_cost
[mode
][aclass
][aclass
] * freq
;
981 ira_shuffle_cost
+= cost
;
983 ira_overall_cost
+= cost
;
985 result
= get_insns ();
990 /* Generate RTX move insns from move lists attached to basic blocks
998 rtx_insn
*insns
, *tmp
;
1000 FOR_EACH_BB_FN (bb
, cfun
)
1002 if (at_bb_start
[bb
->index
] != NULL
)
1004 at_bb_start
[bb
->index
] = modify_move_list (at_bb_start
[bb
->index
]);
1005 insns
= emit_move_list (at_bb_start
[bb
->index
],
1006 REG_FREQ_FROM_BB (bb
));
1009 tmp
= NEXT_INSN (tmp
);
1010 if (NOTE_INSN_BASIC_BLOCK_P (tmp
))
1011 tmp
= NEXT_INSN (tmp
);
1012 if (tmp
== BB_HEAD (bb
))
1013 emit_insn_before (insns
, tmp
);
1014 else if (tmp
!= NULL_RTX
)
1015 emit_insn_after (insns
, PREV_INSN (tmp
));
1017 emit_insn_after (insns
, get_last_insn ());
1020 if (at_bb_end
[bb
->index
] != NULL
)
1022 at_bb_end
[bb
->index
] = modify_move_list (at_bb_end
[bb
->index
]);
1023 insns
= emit_move_list (at_bb_end
[bb
->index
], REG_FREQ_FROM_BB (bb
));
1024 ira_assert (! control_flow_insn_p (BB_END (bb
)));
1025 emit_insn_after (insns
, BB_END (bb
));
1028 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1032 ira_assert ((e
->flags
& EDGE_ABNORMAL
) == 0
1033 || ! EDGE_CRITICAL_P (e
));
1034 e
->aux
= modify_move_list ((move_t
) e
->aux
);
1036 (emit_move_list ((move_t
) e
->aux
,
1037 REG_FREQ_FROM_EDGE_FREQ (EDGE_FREQUENCY (e
))),
1039 if (e
->src
->next_bb
!= e
->dest
)
1040 ira_additional_jumps_num
++;
1045 /* Update costs of A and corresponding allocnos on upper levels on the
1046 loop tree from reading (if READ_P) or writing A on an execution
1049 update_costs (ira_allocno_t a
, bool read_p
, int freq
)
1051 ira_loop_tree_node_t parent
;
1055 ALLOCNO_NREFS (a
)++;
1056 ALLOCNO_FREQ (a
) += freq
;
1057 ALLOCNO_MEMORY_COST (a
)
1058 += (ira_memory_move_cost
[ALLOCNO_MODE (a
)][ALLOCNO_CLASS (a
)]
1059 [read_p
? 1 : 0] * freq
);
1060 if (ALLOCNO_CAP (a
) != NULL
)
1061 a
= ALLOCNO_CAP (a
);
1062 else if ((parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
) == NULL
1063 || (a
= parent
->regno_allocno_map
[ALLOCNO_REGNO (a
)]) == NULL
)
1068 /* Process moves from LIST with execution FREQ to add ranges, copies,
1069 and modify costs for allocnos involved in the moves. All regnos
1070 living through the list is in LIVE_THROUGH, and the loop tree node
1071 used to find corresponding allocnos is NODE. */
1073 add_range_and_copies_from_move_list (move_t list
, ira_loop_tree_node_t node
,
1074 bitmap live_through
, int freq
)
1083 HARD_REG_SET hard_regs_live
;
1088 EXECUTE_IF_SET_IN_BITMAP (live_through
, FIRST_PSEUDO_REGISTER
, regno
, bi
)
1090 REG_SET_TO_HARD_REG_SET (hard_regs_live
, live_through
);
1091 /* This is a trick to guarantee that new ranges is not merged with
1094 start
= ira_max_point
;
1095 for (move
= list
; move
!= NULL
; move
= move
->next
)
1097 ira_allocno_t from
= move
->from
;
1098 ira_allocno_t to
= move
->to
;
1101 bitmap_clear_bit (live_through
, ALLOCNO_REGNO (from
));
1102 bitmap_clear_bit (live_through
, ALLOCNO_REGNO (to
));
1104 nr
= ALLOCNO_NUM_OBJECTS (to
);
1105 for (i
= 0; i
< nr
; i
++)
1107 ira_object_t to_obj
= ALLOCNO_OBJECT (to
, i
);
1108 if (OBJECT_CONFLICT_ARRAY (to_obj
) == NULL
)
1110 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
1111 fprintf (ira_dump_file
, " Allocate conflicts for a%dr%d\n",
1112 ALLOCNO_NUM (to
), REGNO (allocno_emit_reg (to
)));
1113 ira_allocate_object_conflicts (to_obj
, n
);
1116 ior_hard_reg_conflicts (from
, &hard_regs_live
);
1117 ior_hard_reg_conflicts (to
, &hard_regs_live
);
1119 update_costs (from
, true, freq
);
1120 update_costs (to
, false, freq
);
1121 cp
= ira_add_allocno_copy (from
, to
, freq
, false, move
->insn
, NULL
);
1122 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
1123 fprintf (ira_dump_file
, " Adding cp%d:a%dr%d-a%dr%d\n",
1124 cp
->num
, ALLOCNO_NUM (cp
->first
),
1125 REGNO (allocno_emit_reg (cp
->first
)),
1126 ALLOCNO_NUM (cp
->second
),
1127 REGNO (allocno_emit_reg (cp
->second
)));
1129 nr
= ALLOCNO_NUM_OBJECTS (from
);
1130 for (i
= 0; i
< nr
; i
++)
1132 ira_object_t from_obj
= ALLOCNO_OBJECT (from
, i
);
1133 r
= OBJECT_LIVE_RANGES (from_obj
);
1134 if (r
== NULL
|| r
->finish
>= 0)
1136 ira_add_live_range_to_object (from_obj
, start
, ira_max_point
);
1137 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
1138 fprintf (ira_dump_file
,
1139 " Adding range [%d..%d] to allocno a%dr%d\n",
1140 start
, ira_max_point
, ALLOCNO_NUM (from
),
1141 REGNO (allocno_emit_reg (from
)));
1145 r
->finish
= ira_max_point
;
1146 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
1147 fprintf (ira_dump_file
,
1148 " Adding range [%d..%d] to allocno a%dr%d\n",
1149 r
->start
, ira_max_point
, ALLOCNO_NUM (from
),
1150 REGNO (allocno_emit_reg (from
)));
1154 nr
= ALLOCNO_NUM_OBJECTS (to
);
1155 for (i
= 0; i
< nr
; i
++)
1157 ira_object_t to_obj
= ALLOCNO_OBJECT (to
, i
);
1158 ira_add_live_range_to_object (to_obj
, ira_max_point
, -1);
1162 for (move
= list
; move
!= NULL
; move
= move
->next
)
1165 nr
= ALLOCNO_NUM_OBJECTS (move
->to
);
1166 for (i
= 0; i
< nr
; i
++)
1168 ira_object_t to_obj
= ALLOCNO_OBJECT (move
->to
, i
);
1169 r
= OBJECT_LIVE_RANGES (to_obj
);
1172 r
->finish
= ira_max_point
- 1;
1173 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
1174 fprintf (ira_dump_file
,
1175 " Adding range [%d..%d] to allocno a%dr%d\n",
1176 r
->start
, r
->finish
, ALLOCNO_NUM (move
->to
),
1177 REGNO (allocno_emit_reg (move
->to
)));
1181 EXECUTE_IF_SET_IN_BITMAP (live_through
, FIRST_PSEUDO_REGISTER
, regno
, bi
)
1186 a
= node
->regno_allocno_map
[regno
];
1187 if ((to
= ALLOCNO_EMIT_DATA (a
)->mem_optimized_dest
) != NULL
)
1189 nr
= ALLOCNO_NUM_OBJECTS (a
);
1190 for (i
= 0; i
< nr
; i
++)
1192 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
1193 ira_add_live_range_to_object (obj
, start
, ira_max_point
- 1);
1195 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
1198 " Adding range [%d..%d] to live through %s allocno a%dr%d\n",
1199 start
, ira_max_point
- 1,
1200 to
!= NULL
? "upper level" : "",
1201 ALLOCNO_NUM (a
), REGNO (allocno_emit_reg (a
)));
1205 /* Process all move list to add ranges, conflicts, copies, and modify
1206 costs for allocnos involved in the moves. */
1208 add_ranges_and_copies (void)
1213 ira_loop_tree_node_t node
;
1214 bitmap live_through
;
1216 live_through
= ira_allocate_bitmap ();
1217 FOR_EACH_BB_FN (bb
, cfun
)
1219 /* It does not matter what loop_tree_node (of source or
1220 destination block) to use for searching allocnos by their
1221 regnos because of subsequent IR flattening. */
1222 node
= IRA_BB_NODE (bb
)->parent
;
1223 bitmap_copy (live_through
, df_get_live_in (bb
));
1224 add_range_and_copies_from_move_list
1225 (at_bb_start
[bb
->index
], node
, live_through
, REG_FREQ_FROM_BB (bb
));
1226 bitmap_copy (live_through
, df_get_live_out (bb
));
1227 add_range_and_copies_from_move_list
1228 (at_bb_end
[bb
->index
], node
, live_through
, REG_FREQ_FROM_BB (bb
));
1229 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1231 bitmap_and (live_through
,
1232 df_get_live_in (e
->dest
), df_get_live_out (bb
));
1233 add_range_and_copies_from_move_list
1234 ((move_t
) e
->aux
, node
, live_through
,
1235 REG_FREQ_FROM_EDGE_FREQ (EDGE_FREQUENCY (e
)));
1238 ira_free_bitmap (live_through
);
1241 /* The entry function changes code and generates shuffling allocnos on
1242 region borders for the regional (LOOPS_P is TRUE in this case)
1243 register allocation. */
1245 ira_emit (bool loops_p
)
1252 ira_allocno_iterator ai
;
1255 FOR_EACH_ALLOCNO (a
, ai
)
1256 ALLOCNO_EMIT_DATA (a
)->reg
= regno_reg_rtx
[ALLOCNO_REGNO (a
)];
1259 sz
= sizeof (move_t
) * last_basic_block_for_fn (cfun
);
1260 at_bb_start
= (move_t
*) ira_allocate (sz
);
1261 memset (at_bb_start
, 0, sz
);
1262 at_bb_end
= (move_t
*) ira_allocate (sz
);
1263 memset (at_bb_end
, 0, sz
);
1264 local_allocno_bitmap
= ira_allocate_bitmap ();
1265 used_regno_bitmap
= ira_allocate_bitmap ();
1266 renamed_regno_bitmap
= ira_allocate_bitmap ();
1267 max_regno_before_changing
= max_reg_num ();
1268 ira_traverse_loop_tree (true, ira_loop_tree_root
, change_loop
, NULL
);
1269 set_allocno_somewhere_renamed_p ();
1270 ira_free_bitmap (used_regno_bitmap
);
1271 ira_free_bitmap (renamed_regno_bitmap
);
1272 ira_free_bitmap (local_allocno_bitmap
);
1273 setup_entered_from_non_parent_p ();
1274 FOR_EACH_BB_FN (bb
, cfun
)
1276 at_bb_start
[bb
->index
] = NULL
;
1277 at_bb_end
[bb
->index
] = NULL
;
1278 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1279 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1280 generate_edge_moves (e
);
1283 = (move_t
*) ira_allocate (sizeof (move_t
) * max_reg_num ());
1284 allocno_last_set_check
1285 = (int *) ira_allocate (sizeof (int) * max_reg_num ());
1286 memset (allocno_last_set_check
, 0, sizeof (int) * max_reg_num ());
1287 memset (hard_regno_last_set_check
, 0, sizeof (hard_regno_last_set_check
));
1289 FOR_EACH_BB_FN (bb
, cfun
)
1290 unify_moves (bb
, true);
1291 FOR_EACH_BB_FN (bb
, cfun
)
1292 unify_moves (bb
, false);
1293 move_vec
.create (ira_allocnos_num
);
1295 add_ranges_and_copies ();
1297 FOR_EACH_BB_FN (bb
, cfun
)
1299 free_move_list (at_bb_start
[bb
->index
]);
1300 free_move_list (at_bb_end
[bb
->index
]);
1301 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1303 free_move_list ((move_t
) e
->aux
);
1307 move_vec
.release ();
1308 ira_free (allocno_last_set_check
);
1309 ira_free (allocno_last_set
);
1310 commit_edge_insertions ();
1311 /* Fix insn codes. It is necessary to do it before reload because
1312 reload assumes initial insn codes defined. The insn codes can be
1313 invalidated by CFG infrastructure for example in jump
1315 FOR_EACH_BB_FN (bb
, cfun
)
1316 FOR_BB_INSNS_REVERSE (bb
, insn
)
1318 recog_memoized (insn
);
1319 ira_free (at_bb_end
);
1320 ira_free (at_bb_start
);