2007-05-30 H.J. Lu <hongjiu.lu@intel.com>
[official-gcc.git] / gcc / mode-switching.c
blob448192a9b4eb5144f1f9eb1f85518a457be290e6
1 /* CPU mode switching
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
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 2, or (at your option) any later
10 version.
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
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "flags.h"
30 #include "real.h"
31 #include "insn-config.h"
32 #include "recog.h"
33 #include "basic-block.h"
34 #include "output.h"
35 #include "tm_p.h"
36 #include "function.h"
37 #include "tree-pass.h"
38 #include "timevar.h"
40 /* We want target macros for the mode switching code to be able to refer
41 to instruction attribute values. */
42 #include "insn-attr.h"
44 #ifdef OPTIMIZE_MODE_SWITCHING
46 /* The algorithm for setting the modes consists of scanning the insn list
47 and finding all the insns which require a specific mode. Each insn gets
48 a unique struct seginfo element. These structures are inserted into a list
49 for each basic block. For each entity, there is an array of bb_info over
50 the flow graph basic blocks (local var 'bb_info'), and contains a list
51 of all insns within that basic block, in the order they are encountered.
53 For each entity, any basic block WITHOUT any insns requiring a specific
54 mode are given a single entry, without a mode. (Each basic block
55 in the flow graph must have at least one entry in the segment table.)
57 The LCM algorithm is then run over the flow graph to determine where to
58 place the sets to the highest-priority value in respect of first the first
59 insn in any one block. Any adjustments required to the transparency
60 vectors are made, then the next iteration starts for the next-lower
61 priority mode, till for each entity all modes are exhausted.
63 More details are located in the code for optimize_mode_switching(). */
65 /* This structure contains the information for each insn which requires
66 either single or double mode to be set.
67 MODE is the mode this insn must be executed in.
68 INSN_PTR is the insn to be executed (may be the note that marks the
69 beginning of a basic block).
70 BBNUM is the flow graph basic block this insn occurs in.
71 NEXT is the next insn in the same basic block. */
72 struct seginfo
74 int mode;
75 rtx insn_ptr;
76 int bbnum;
77 struct seginfo *next;
78 HARD_REG_SET regs_live;
81 struct bb_info
83 struct seginfo *seginfo;
84 int computing;
87 /* These bitmaps are used for the LCM algorithm. */
89 static sbitmap *antic;
90 static sbitmap *transp;
91 static sbitmap *comp;
93 static struct seginfo * new_seginfo (int, rtx, int, HARD_REG_SET);
94 static void add_seginfo (struct bb_info *, struct seginfo *);
95 static void reg_dies (rtx, HARD_REG_SET *);
96 static void reg_becomes_live (rtx, rtx, void *);
97 static void make_preds_opaque (basic_block, int);
100 /* This function will allocate a new BBINFO structure, initialized
101 with the MODE, INSN, and basic block BB parameters. */
103 static struct seginfo *
104 new_seginfo (int mode, rtx insn, int bb, HARD_REG_SET regs_live)
106 struct seginfo *ptr;
107 ptr = XNEW (struct seginfo);
108 ptr->mode = mode;
109 ptr->insn_ptr = insn;
110 ptr->bbnum = bb;
111 ptr->next = NULL;
112 COPY_HARD_REG_SET (ptr->regs_live, regs_live);
113 return ptr;
116 /* Add a seginfo element to the end of a list.
117 HEAD is a pointer to the list beginning.
118 INFO is the structure to be linked in. */
120 static void
121 add_seginfo (struct bb_info *head, struct seginfo *info)
123 struct seginfo *ptr;
125 if (head->seginfo == NULL)
126 head->seginfo = info;
127 else
129 ptr = head->seginfo;
130 while (ptr->next != NULL)
131 ptr = ptr->next;
132 ptr->next = info;
136 /* Make all predecessors of basic block B opaque, recursively, till we hit
137 some that are already non-transparent, or an edge where aux is set; that
138 denotes that a mode set is to be done on that edge.
139 J is the bit number in the bitmaps that corresponds to the entity that
140 we are currently handling mode-switching for. */
142 static void
143 make_preds_opaque (basic_block b, int j)
145 edge e;
146 edge_iterator ei;
148 FOR_EACH_EDGE (e, ei, b->preds)
150 basic_block pb = e->src;
152 if (e->aux || ! TEST_BIT (transp[pb->index], j))
153 continue;
155 RESET_BIT (transp[pb->index], j);
156 make_preds_opaque (pb, j);
160 /* Record in LIVE that register REG died. */
162 static void
163 reg_dies (rtx reg, HARD_REG_SET *live)
165 int regno;
167 if (!REG_P (reg))
168 return;
170 regno = REGNO (reg);
171 if (regno < FIRST_PSEUDO_REGISTER)
172 remove_from_hard_reg_set (live, GET_MODE (reg), regno);
175 /* Record in LIVE that register REG became live.
176 This is called via note_stores. */
178 static void
179 reg_becomes_live (rtx reg, rtx setter ATTRIBUTE_UNUSED, void *live)
181 int regno;
183 if (GET_CODE (reg) == SUBREG)
184 reg = SUBREG_REG (reg);
186 if (!REG_P (reg))
187 return;
189 regno = REGNO (reg);
190 if (regno < FIRST_PSEUDO_REGISTER)
191 add_to_hard_reg_set ((HARD_REG_SET *) live, GET_MODE (reg), regno);
194 /* Make sure if MODE_ENTRY is defined the MODE_EXIT is defined
195 and vice versa. */
196 #if defined (MODE_ENTRY) != defined (MODE_EXIT)
197 #error "Both MODE_ENTRY and MODE_EXIT must be defined"
198 #endif
200 #if defined (MODE_ENTRY) && defined (MODE_EXIT)
201 /* Split the fallthrough edge to the exit block, so that we can note
202 that there NORMAL_MODE is required. Return the new block if it's
203 inserted before the exit block. Otherwise return null. */
205 static basic_block
206 create_pre_exit (int n_entities, int *entity_map, const int *num_modes)
208 edge eg;
209 edge_iterator ei;
210 basic_block pre_exit;
212 /* The only non-call predecessor at this stage is a block with a
213 fallthrough edge; there can be at most one, but there could be
214 none at all, e.g. when exit is called. */
215 pre_exit = 0;
216 FOR_EACH_EDGE (eg, ei, EXIT_BLOCK_PTR->preds)
217 if (eg->flags & EDGE_FALLTHRU)
219 basic_block src_bb = eg->src;
220 regset live_at_end = src_bb->il.rtl->global_live_at_end;
221 rtx last_insn, ret_reg;
223 gcc_assert (!pre_exit);
224 /* If this function returns a value at the end, we have to
225 insert the final mode switch before the return value copy
226 to its hard register. */
227 if (EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 1
228 && NONJUMP_INSN_P ((last_insn = BB_END (src_bb)))
229 && GET_CODE (PATTERN (last_insn)) == USE
230 && GET_CODE ((ret_reg = XEXP (PATTERN (last_insn), 0))) == REG)
232 int ret_start = REGNO (ret_reg);
233 int nregs = hard_regno_nregs[ret_start][GET_MODE (ret_reg)];
234 int ret_end = ret_start + nregs;
235 int short_block = 0;
236 int maybe_builtin_apply = 0;
237 int forced_late_switch = 0;
238 rtx before_return_copy;
242 rtx return_copy = PREV_INSN (last_insn);
243 rtx return_copy_pat, copy_reg;
244 int copy_start, copy_num;
245 int j;
247 if (INSN_P (return_copy))
249 if (GET_CODE (PATTERN (return_copy)) == USE
250 && GET_CODE (XEXP (PATTERN (return_copy), 0)) == REG
251 && (FUNCTION_VALUE_REGNO_P
252 (REGNO (XEXP (PATTERN (return_copy), 0)))))
254 maybe_builtin_apply = 1;
255 last_insn = return_copy;
256 continue;
258 if (GET_CODE (PATTERN (return_copy)) == ASM_INPUT
259 && strcmp (XSTR (PATTERN (return_copy), 0), "") == 0)
261 last_insn = return_copy;
262 continue;
264 /* If the return register is not (in its entirety)
265 likely spilled, the return copy might be
266 partially or completely optimized away. */
267 return_copy_pat = single_set (return_copy);
268 if (!return_copy_pat)
270 return_copy_pat = PATTERN (return_copy);
271 if (GET_CODE (return_copy_pat) != CLOBBER)
272 break;
274 copy_reg = SET_DEST (return_copy_pat);
275 if (GET_CODE (copy_reg) == REG)
276 copy_start = REGNO (copy_reg);
277 else if (GET_CODE (copy_reg) == SUBREG
278 && GET_CODE (SUBREG_REG (copy_reg)) == REG)
279 copy_start = REGNO (SUBREG_REG (copy_reg));
280 else
281 break;
282 if (copy_start >= FIRST_PSEUDO_REGISTER)
283 break;
284 copy_num
285 = hard_regno_nregs[copy_start][GET_MODE (copy_reg)];
287 /* If the return register is not likely spilled, - as is
288 the case for floating point on SH4 - then it might
289 be set by an arithmetic operation that needs a
290 different mode than the exit block. */
291 for (j = n_entities - 1; j >= 0; j--)
293 int e = entity_map[j];
294 int mode = MODE_NEEDED (e, return_copy);
296 if (mode != num_modes[e] && mode != MODE_EXIT (e))
297 break;
299 if (j >= 0)
301 /* For the SH4, floating point loads depend on fpscr,
302 thus we might need to put the final mode switch
303 after the return value copy. That is still OK,
304 because a floating point return value does not
305 conflict with address reloads. */
306 if (copy_start >= ret_start
307 && copy_start + copy_num <= ret_end
308 && OBJECT_P (SET_SRC (return_copy_pat)))
309 forced_late_switch = 1;
310 break;
313 if (copy_start >= ret_start
314 && copy_start + copy_num <= ret_end)
315 nregs -= copy_num;
316 else if (!maybe_builtin_apply
317 || !FUNCTION_VALUE_REGNO_P (copy_start))
318 break;
319 last_insn = return_copy;
321 /* ??? Exception handling can lead to the return value
322 copy being already separated from the return value use,
323 as in unwind-dw2.c .
324 Similarly, conditionally returning without a value,
325 and conditionally using builtin_return can lead to an
326 isolated use. */
327 if (return_copy == BB_HEAD (src_bb))
329 short_block = 1;
330 break;
332 last_insn = return_copy;
334 while (nregs);
336 /* If we didn't see a full return value copy, verify that there
337 is a plausible reason for this. If some, but not all of the
338 return register is likely spilled, we can expect that there
339 is a copy for the likely spilled part. */
340 gcc_assert (!nregs
341 || forced_late_switch
342 || short_block
343 || !(CLASS_LIKELY_SPILLED_P
344 (REGNO_REG_CLASS (ret_start)))
345 || (nregs
346 != hard_regno_nregs[ret_start][GET_MODE (ret_reg)])
347 /* For multi-hard-register floating point
348 values, sometimes the likely-spilled part
349 is ordinarily copied first, then the other
350 part is set with an arithmetic operation.
351 This doesn't actually cause reload
352 failures, so let it pass. */
353 || (GET_MODE_CLASS (GET_MODE (ret_reg)) != MODE_INT
354 && nregs != 1));
356 if (INSN_P (last_insn))
358 before_return_copy
359 = emit_note_before (NOTE_INSN_DELETED, last_insn);
360 /* Instructions preceding LAST_INSN in the same block might
361 require a different mode than MODE_EXIT, so if we might
362 have such instructions, keep them in a separate block
363 from pre_exit. */
364 if (last_insn != BB_HEAD (src_bb))
365 src_bb = split_block (src_bb,
366 PREV_INSN (before_return_copy))->dest;
368 else
369 before_return_copy = last_insn;
370 pre_exit = split_block (src_bb, before_return_copy)->src;
372 else
374 pre_exit = split_edge (eg);
375 COPY_REG_SET (pre_exit->il.rtl->global_live_at_start, live_at_end);
376 COPY_REG_SET (pre_exit->il.rtl->global_live_at_end, live_at_end);
380 return pre_exit;
382 #endif
384 /* Find all insns that need a particular mode setting, and insert the
385 necessary mode switches. Return true if we did work. */
387 static int
388 optimize_mode_switching (void)
390 rtx insn;
391 int e;
392 basic_block bb;
393 int need_commit = 0;
394 sbitmap *kill;
395 struct edge_list *edge_list;
396 static const int num_modes[] = NUM_MODES_FOR_MODE_SWITCHING;
397 #define N_ENTITIES ARRAY_SIZE (num_modes)
398 int entity_map[N_ENTITIES];
399 struct bb_info *bb_info[N_ENTITIES];
400 int i, j;
401 int n_entities;
402 int max_num_modes = 0;
403 bool emited = false;
404 basic_block post_entry ATTRIBUTE_UNUSED, pre_exit ATTRIBUTE_UNUSED;
406 clear_bb_flags ();
408 for (e = N_ENTITIES - 1, n_entities = 0; e >= 0; e--)
409 if (OPTIMIZE_MODE_SWITCHING (e))
411 int entry_exit_extra = 0;
413 /* Create the list of segments within each basic block.
414 If NORMAL_MODE is defined, allow for two extra
415 blocks split from the entry and exit block. */
416 #if defined (MODE_ENTRY) && defined (MODE_EXIT)
417 entry_exit_extra = 3;
418 #endif
419 bb_info[n_entities]
420 = XCNEWVEC (struct bb_info, last_basic_block + entry_exit_extra);
421 entity_map[n_entities++] = e;
422 if (num_modes[e] > max_num_modes)
423 max_num_modes = num_modes[e];
426 if (! n_entities)
427 return 0;
429 #if defined (MODE_ENTRY) && defined (MODE_EXIT)
430 /* Split the edge from the entry block, so that we can note that
431 there NORMAL_MODE is supplied. */
432 post_entry = split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
433 pre_exit = create_pre_exit (n_entities, entity_map, num_modes);
434 #endif
436 /* Create the bitmap vectors. */
438 antic = sbitmap_vector_alloc (last_basic_block, n_entities);
439 transp = sbitmap_vector_alloc (last_basic_block, n_entities);
440 comp = sbitmap_vector_alloc (last_basic_block, n_entities);
442 sbitmap_vector_ones (transp, last_basic_block);
444 for (j = n_entities - 1; j >= 0; j--)
446 int e = entity_map[j];
447 int no_mode = num_modes[e];
448 struct bb_info *info = bb_info[j];
450 /* Determine what the first use (if any) need for a mode of entity E is.
451 This will be the mode that is anticipatable for this block.
452 Also compute the initial transparency settings. */
453 FOR_EACH_BB (bb)
455 struct seginfo *ptr;
456 int last_mode = no_mode;
457 HARD_REG_SET live_now;
459 REG_SET_TO_HARD_REG_SET (live_now,
460 bb->il.rtl->global_live_at_start);
462 /* Pretend the mode is clobbered across abnormal edges. */
464 edge_iterator ei;
465 edge e;
466 FOR_EACH_EDGE (e, ei, bb->preds)
467 if (e->flags & EDGE_COMPLEX)
468 break;
469 if (e)
471 ptr = new_seginfo (no_mode, BB_HEAD (bb), bb->index, live_now);
472 add_seginfo (info + bb->index, ptr);
473 RESET_BIT (transp[bb->index], j);
477 for (insn = BB_HEAD (bb);
478 insn != NULL && insn != NEXT_INSN (BB_END (bb));
479 insn = NEXT_INSN (insn))
481 if (INSN_P (insn))
483 int mode = MODE_NEEDED (e, insn);
484 rtx link;
486 if (mode != no_mode && mode != last_mode)
488 last_mode = mode;
489 ptr = new_seginfo (mode, insn, bb->index, live_now);
490 add_seginfo (info + bb->index, ptr);
491 RESET_BIT (transp[bb->index], j);
493 #ifdef MODE_AFTER
494 last_mode = MODE_AFTER (last_mode, insn);
495 #endif
496 /* Update LIVE_NOW. */
497 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
498 if (REG_NOTE_KIND (link) == REG_DEAD)
499 reg_dies (XEXP (link, 0), &live_now);
501 note_stores (PATTERN (insn), reg_becomes_live, &live_now);
502 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
503 if (REG_NOTE_KIND (link) == REG_UNUSED)
504 reg_dies (XEXP (link, 0), &live_now);
508 info[bb->index].computing = last_mode;
509 /* Check for blocks without ANY mode requirements. */
510 if (last_mode == no_mode)
512 ptr = new_seginfo (no_mode, BB_END (bb), bb->index, live_now);
513 add_seginfo (info + bb->index, ptr);
516 #if defined (MODE_ENTRY) && defined (MODE_EXIT)
518 int mode = MODE_ENTRY (e);
520 if (mode != no_mode)
522 bb = post_entry;
524 /* By always making this nontransparent, we save
525 an extra check in make_preds_opaque. We also
526 need this to avoid confusing pre_edge_lcm when
527 antic is cleared but transp and comp are set. */
528 RESET_BIT (transp[bb->index], j);
530 /* Insert a fake computing definition of MODE into entry
531 blocks which compute no mode. This represents the mode on
532 entry. */
533 info[bb->index].computing = mode;
535 if (pre_exit)
536 info[pre_exit->index].seginfo->mode = MODE_EXIT (e);
539 #endif /* NORMAL_MODE */
542 kill = sbitmap_vector_alloc (last_basic_block, n_entities);
543 for (i = 0; i < max_num_modes; i++)
545 int current_mode[N_ENTITIES];
546 sbitmap *delete;
547 sbitmap *insert;
549 /* Set the anticipatable and computing arrays. */
550 sbitmap_vector_zero (antic, last_basic_block);
551 sbitmap_vector_zero (comp, last_basic_block);
552 for (j = n_entities - 1; j >= 0; j--)
554 int m = current_mode[j] = MODE_PRIORITY_TO_MODE (entity_map[j], i);
555 struct bb_info *info = bb_info[j];
557 FOR_EACH_BB (bb)
559 if (info[bb->index].seginfo->mode == m)
560 SET_BIT (antic[bb->index], j);
562 if (info[bb->index].computing == m)
563 SET_BIT (comp[bb->index], j);
567 /* Calculate the optimal locations for the
568 placement mode switches to modes with priority I. */
570 FOR_EACH_BB (bb)
571 sbitmap_not (kill[bb->index], transp[bb->index]);
572 edge_list = pre_edge_lcm (n_entities, transp, comp, antic,
573 kill, &insert, &delete);
575 for (j = n_entities - 1; j >= 0; j--)
577 /* Insert all mode sets that have been inserted by lcm. */
578 int no_mode = num_modes[entity_map[j]];
580 /* Wherever we have moved a mode setting upwards in the flow graph,
581 the blocks between the new setting site and the now redundant
582 computation ceases to be transparent for any lower-priority
583 mode of the same entity. First set the aux field of each
584 insertion site edge non-transparent, then propagate the new
585 non-transparency from the redundant computation upwards till
586 we hit an insertion site or an already non-transparent block. */
587 for (e = NUM_EDGES (edge_list) - 1; e >= 0; e--)
589 edge eg = INDEX_EDGE (edge_list, e);
590 int mode;
591 basic_block src_bb;
592 HARD_REG_SET live_at_edge;
593 rtx mode_set;
595 eg->aux = 0;
597 if (! TEST_BIT (insert[e], j))
598 continue;
600 eg->aux = (void *)1;
602 mode = current_mode[j];
603 src_bb = eg->src;
605 REG_SET_TO_HARD_REG_SET (live_at_edge,
606 src_bb->il.rtl->global_live_at_end);
608 start_sequence ();
609 EMIT_MODE_SET (entity_map[j], mode, live_at_edge);
610 mode_set = get_insns ();
611 end_sequence ();
613 /* Do not bother to insert empty sequence. */
614 if (mode_set == NULL_RTX)
615 continue;
617 /* We should not get an abnormal edge here. */
618 gcc_assert (! (eg->flags & EDGE_ABNORMAL));
620 need_commit = 1;
621 insert_insn_on_edge (mode_set, eg);
624 FOR_EACH_BB_REVERSE (bb)
625 if (TEST_BIT (delete[bb->index], j))
627 make_preds_opaque (bb, j);
628 /* Cancel the 'deleted' mode set. */
629 bb_info[j][bb->index].seginfo->mode = no_mode;
633 sbitmap_vector_free (delete);
634 sbitmap_vector_free (insert);
635 clear_aux_for_edges ();
636 free_edge_list (edge_list);
639 /* Now output the remaining mode sets in all the segments. */
640 for (j = n_entities - 1; j >= 0; j--)
642 int no_mode = num_modes[entity_map[j]];
644 FOR_EACH_BB_REVERSE (bb)
646 struct seginfo *ptr, *next;
647 for (ptr = bb_info[j][bb->index].seginfo; ptr; ptr = next)
649 next = ptr->next;
650 if (ptr->mode != no_mode)
652 rtx mode_set;
654 start_sequence ();
655 EMIT_MODE_SET (entity_map[j], ptr->mode, ptr->regs_live);
656 mode_set = get_insns ();
657 end_sequence ();
659 /* Insert MODE_SET only if it is nonempty. */
660 if (mode_set != NULL_RTX)
662 emited = true;
663 if (NOTE_INSN_BASIC_BLOCK_P (ptr->insn_ptr))
664 emit_insn_after (mode_set, ptr->insn_ptr);
665 else
666 emit_insn_before (mode_set, ptr->insn_ptr);
670 free (ptr);
674 free (bb_info[j]);
677 /* Finished. Free up all the things we've allocated. */
679 sbitmap_vector_free (kill);
680 sbitmap_vector_free (antic);
681 sbitmap_vector_free (transp);
682 sbitmap_vector_free (comp);
684 if (need_commit)
685 commit_edge_insertions ();
687 #if defined (MODE_ENTRY) && defined (MODE_EXIT)
688 cleanup_cfg (CLEANUP_NO_INSN_DEL);
689 #else
690 if (!need_commit && !emited)
691 return 0;
692 #endif
694 max_regno = max_reg_num ();
695 allocate_reg_info (max_regno, FALSE, FALSE);
696 update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
697 (PROP_DEATH_NOTES | PROP_KILL_DEAD_CODE
698 | PROP_SCAN_DEAD_CODE));
700 return 1;
703 #endif /* OPTIMIZE_MODE_SWITCHING */
705 static bool
706 gate_mode_switching (void)
708 #ifdef OPTIMIZE_MODE_SWITCHING
709 return true;
710 #else
711 return false;
712 #endif
715 static unsigned int
716 rest_of_handle_mode_switching (void)
718 #ifdef OPTIMIZE_MODE_SWITCHING
719 no_new_pseudos = 0;
720 optimize_mode_switching ();
721 no_new_pseudos = 1;
722 #endif /* OPTIMIZE_MODE_SWITCHING */
723 return 0;
727 struct tree_opt_pass pass_mode_switching =
729 "mode-sw", /* name */
730 gate_mode_switching, /* gate */
731 rest_of_handle_mode_switching, /* execute */
732 NULL, /* sub */
733 NULL, /* next */
734 0, /* static_pass_number */
735 TV_MODE_SWITCH, /* tv_id */
736 0, /* properties_required */
737 0, /* properties_provided */
738 0, /* properties_destroyed */
739 0, /* todo_flags_start */
740 TODO_dump_func, /* todo_flags_finish */
741 0 /* letter */