PR debug/66535
[official-gcc.git] / gcc / mode-switching.c
blobbe26d15d6aeb0a7888f4749db2a002eb5e62ae75
1 /* CPU mode switching
2 Copyright (C) 1998-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "flags.h"
29 #include "insn-config.h"
30 #include "recog.h"
31 #include "predict.h"
32 #include "input.h"
33 #include "function.h"
34 #include "dominance.h"
35 #include "cfg.h"
36 #include "cfgrtl.h"
37 #include "cfganal.h"
38 #include "lcm.h"
39 #include "cfgcleanup.h"
40 #include "basic-block.h"
41 #include "tm_p.h"
42 #include "tree-pass.h"
43 #include "df.h"
44 #include "emit-rtl.h"
46 /* We want target macros for the mode switching code to be able to refer
47 to instruction attribute values. */
48 #include "insn-attr.h"
50 #ifdef OPTIMIZE_MODE_SWITCHING
52 /* The algorithm for setting the modes consists of scanning the insn list
53 and finding all the insns which require a specific mode. Each insn gets
54 a unique struct seginfo element. These structures are inserted into a list
55 for each basic block. For each entity, there is an array of bb_info over
56 the flow graph basic blocks (local var 'bb_info'), which contains a list
57 of all insns within that basic block, in the order they are encountered.
59 For each entity, any basic block WITHOUT any insns requiring a specific
60 mode are given a single entry without a mode (each basic block in the
61 flow graph must have at least one entry in the segment table).
63 The LCM algorithm is then run over the flow graph to determine where to
64 place the sets to the highest-priority mode with respect to the first
65 insn in any one block. Any adjustments required to the transparency
66 vectors are made, then the next iteration starts for the next-lower
67 priority mode, till for each entity all modes are exhausted.
69 More details can be found in the code of optimize_mode_switching. */
71 /* This structure contains the information for each insn which requires
72 either single or double mode to be set.
73 MODE is the mode this insn must be executed in.
74 INSN_PTR is the insn to be executed (may be the note that marks the
75 beginning of a basic block).
76 BBNUM is the flow graph basic block this insn occurs in.
77 NEXT is the next insn in the same basic block. */
78 struct seginfo
80 int mode;
81 rtx_insn *insn_ptr;
82 int bbnum;
83 struct seginfo *next;
84 HARD_REG_SET regs_live;
87 struct bb_info
89 struct seginfo *seginfo;
90 int computing;
91 int mode_out;
92 int mode_in;
95 static struct seginfo * new_seginfo (int, rtx_insn *, int, HARD_REG_SET);
96 static void add_seginfo (struct bb_info *, struct seginfo *);
97 static void reg_dies (rtx, HARD_REG_SET *);
98 static void reg_becomes_live (rtx, const_rtx, void *);
100 /* Clear ode I from entity J in bitmap B. */
101 #define clear_mode_bit(b, j, i) \
102 bitmap_clear_bit (b, (j * max_num_modes) + i)
104 /* Test mode I from entity J in bitmap B. */
105 #define mode_bit_p(b, j, i) \
106 bitmap_bit_p (b, (j * max_num_modes) + i)
108 /* Set mode I from entity J in bitmal B. */
109 #define set_mode_bit(b, j, i) \
110 bitmap_set_bit (b, (j * max_num_modes) + i)
112 /* Emit modes segments from EDGE_LIST associated with entity E.
113 INFO gives mode availability for each mode. */
115 static bool
116 commit_mode_sets (struct edge_list *edge_list, int e, struct bb_info *info)
118 bool need_commit = false;
120 for (int ed = NUM_EDGES (edge_list) - 1; ed >= 0; ed--)
122 edge eg = INDEX_EDGE (edge_list, ed);
123 int mode;
125 if ((mode = (int)(intptr_t)(eg->aux)) != -1)
127 HARD_REG_SET live_at_edge;
128 basic_block src_bb = eg->src;
129 int cur_mode = info[src_bb->index].mode_out;
130 rtx_insn *mode_set;
132 REG_SET_TO_HARD_REG_SET (live_at_edge, df_get_live_out (src_bb));
134 rtl_profile_for_edge (eg);
135 start_sequence ();
137 targetm.mode_switching.emit (e, mode, cur_mode, live_at_edge);
139 mode_set = get_insns ();
140 end_sequence ();
141 default_rtl_profile ();
143 /* Do not bother to insert empty sequence. */
144 if (mode_set == NULL)
145 continue;
147 /* We should not get an abnormal edge here. */
148 gcc_assert (! (eg->flags & EDGE_ABNORMAL));
150 need_commit = true;
151 insert_insn_on_edge (mode_set, eg);
155 return need_commit;
158 /* Allocate a new BBINFO structure, initialized with the MODE, INSN,
159 and basic block BB parameters.
160 INSN may not be a NOTE_INSN_BASIC_BLOCK, unless it is an empty
161 basic block; that allows us later to insert instructions in a FIFO-like
162 manner. */
164 static struct seginfo *
165 new_seginfo (int mode, rtx_insn *insn, int bb, HARD_REG_SET regs_live)
167 struct seginfo *ptr;
169 gcc_assert (!NOTE_INSN_BASIC_BLOCK_P (insn)
170 || insn == BB_END (NOTE_BASIC_BLOCK (insn)));
171 ptr = XNEW (struct seginfo);
172 ptr->mode = mode;
173 ptr->insn_ptr = insn;
174 ptr->bbnum = bb;
175 ptr->next = NULL;
176 COPY_HARD_REG_SET (ptr->regs_live, regs_live);
177 return ptr;
180 /* Add a seginfo element to the end of a list.
181 HEAD is a pointer to the list beginning.
182 INFO is the structure to be linked in. */
184 static void
185 add_seginfo (struct bb_info *head, struct seginfo *info)
187 struct seginfo *ptr;
189 if (head->seginfo == NULL)
190 head->seginfo = info;
191 else
193 ptr = head->seginfo;
194 while (ptr->next != NULL)
195 ptr = ptr->next;
196 ptr->next = info;
200 /* Record in LIVE that register REG died. */
202 static void
203 reg_dies (rtx reg, HARD_REG_SET *live)
205 int regno;
207 if (!REG_P (reg))
208 return;
210 regno = REGNO (reg);
211 if (regno < FIRST_PSEUDO_REGISTER)
212 remove_from_hard_reg_set (live, GET_MODE (reg), regno);
215 /* Record in LIVE that register REG became live.
216 This is called via note_stores. */
218 static void
219 reg_becomes_live (rtx reg, const_rtx setter ATTRIBUTE_UNUSED, void *live)
221 int regno;
223 if (GET_CODE (reg) == SUBREG)
224 reg = SUBREG_REG (reg);
226 if (!REG_P (reg))
227 return;
229 regno = REGNO (reg);
230 if (regno < FIRST_PSEUDO_REGISTER)
231 add_to_hard_reg_set ((HARD_REG_SET *) live, GET_MODE (reg), regno);
234 /* Split the fallthrough edge to the exit block, so that we can note
235 that there NORMAL_MODE is required. Return the new block if it's
236 inserted before the exit block. Otherwise return null. */
238 static basic_block
239 create_pre_exit (int n_entities, int *entity_map, const int *num_modes)
241 edge eg;
242 edge_iterator ei;
243 basic_block pre_exit;
245 /* The only non-call predecessor at this stage is a block with a
246 fallthrough edge; there can be at most one, but there could be
247 none at all, e.g. when exit is called. */
248 pre_exit = 0;
249 FOR_EACH_EDGE (eg, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
250 if (eg->flags & EDGE_FALLTHRU)
252 basic_block src_bb = eg->src;
253 rtx_insn *last_insn;
254 rtx ret_reg;
256 gcc_assert (!pre_exit);
257 /* If this function returns a value at the end, we have to
258 insert the final mode switch before the return value copy
259 to its hard register. */
260 if (EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) == 1
261 && NONJUMP_INSN_P ((last_insn = BB_END (src_bb)))
262 && GET_CODE (PATTERN (last_insn)) == USE
263 && GET_CODE ((ret_reg = XEXP (PATTERN (last_insn), 0))) == REG)
265 int ret_start = REGNO (ret_reg);
266 int nregs = REG_NREGS (ret_reg);
267 int ret_end = ret_start + nregs;
268 bool short_block = false;
269 bool multi_reg_return = false;
270 bool forced_late_switch = false;
271 rtx_insn *before_return_copy;
275 rtx_insn *return_copy = PREV_INSN (last_insn);
276 rtx return_copy_pat, copy_reg;
277 int copy_start, copy_num;
278 int j;
280 if (NONDEBUG_INSN_P (return_copy))
282 /* When using SJLJ exceptions, the call to the
283 unregister function is inserted between the
284 clobber of the return value and the copy.
285 We do not want to split the block before this
286 or any other call; if we have not found the
287 copy yet, the copy must have been deleted. */
288 if (CALL_P (return_copy))
290 short_block = true;
291 break;
293 return_copy_pat = PATTERN (return_copy);
294 switch (GET_CODE (return_copy_pat))
296 case USE:
297 /* Skip USEs of multiple return registers.
298 __builtin_apply pattern is also handled here. */
299 if (GET_CODE (XEXP (return_copy_pat, 0)) == REG
300 && (targetm.calls.function_value_regno_p
301 (REGNO (XEXP (return_copy_pat, 0)))))
303 multi_reg_return = true;
304 last_insn = return_copy;
305 continue;
307 break;
309 case ASM_OPERANDS:
310 /* Skip barrier insns. */
311 if (!MEM_VOLATILE_P (return_copy_pat))
312 break;
314 /* Fall through. */
316 case ASM_INPUT:
317 case UNSPEC_VOLATILE:
318 last_insn = return_copy;
319 continue;
321 default:
322 break;
325 /* If the return register is not (in its entirety)
326 likely spilled, the return copy might be
327 partially or completely optimized away. */
328 return_copy_pat = single_set (return_copy);
329 if (!return_copy_pat)
331 return_copy_pat = PATTERN (return_copy);
332 if (GET_CODE (return_copy_pat) != CLOBBER)
333 break;
334 else if (!optimize)
336 /* This might be (clobber (reg [<result>]))
337 when not optimizing. Then check if
338 the previous insn is the clobber for
339 the return register. */
340 copy_reg = SET_DEST (return_copy_pat);
341 if (GET_CODE (copy_reg) == REG
342 && !HARD_REGISTER_NUM_P (REGNO (copy_reg)))
344 if (INSN_P (PREV_INSN (return_copy)))
346 return_copy = PREV_INSN (return_copy);
347 return_copy_pat = PATTERN (return_copy);
348 if (GET_CODE (return_copy_pat) != CLOBBER)
349 break;
354 copy_reg = SET_DEST (return_copy_pat);
355 if (GET_CODE (copy_reg) == REG)
356 copy_start = REGNO (copy_reg);
357 else if (GET_CODE (copy_reg) == SUBREG
358 && GET_CODE (SUBREG_REG (copy_reg)) == REG)
359 copy_start = REGNO (SUBREG_REG (copy_reg));
360 else
362 /* When control reaches end of non-void function,
363 there are no return copy insns at all. This
364 avoids an ice on that invalid function. */
365 if (ret_start + nregs == ret_end)
366 short_block = true;
367 break;
369 if (!targetm.calls.function_value_regno_p (copy_start))
370 copy_num = 0;
371 else
372 copy_num
373 = hard_regno_nregs[copy_start][GET_MODE (copy_reg)];
375 /* If the return register is not likely spilled, - as is
376 the case for floating point on SH4 - then it might
377 be set by an arithmetic operation that needs a
378 different mode than the exit block. */
379 for (j = n_entities - 1; j >= 0; j--)
381 int e = entity_map[j];
382 int mode =
383 targetm.mode_switching.needed (e, return_copy);
385 if (mode != num_modes[e]
386 && mode != targetm.mode_switching.exit (e))
387 break;
389 if (j >= 0)
391 /* __builtin_return emits a sequence of loads to all
392 return registers. One of them might require
393 another mode than MODE_EXIT, even if it is
394 unrelated to the return value, so we want to put
395 the final mode switch after it. */
396 if (multi_reg_return
397 && targetm.calls.function_value_regno_p
398 (copy_start))
399 forced_late_switch = true;
401 /* For the SH4, floating point loads depend on fpscr,
402 thus we might need to put the final mode switch
403 after the return value copy. That is still OK,
404 because a floating point return value does not
405 conflict with address reloads. */
406 if (copy_start >= ret_start
407 && copy_start + copy_num <= ret_end
408 && OBJECT_P (SET_SRC (return_copy_pat)))
409 forced_late_switch = true;
410 break;
412 if (copy_num == 0)
414 last_insn = return_copy;
415 continue;
418 if (copy_start >= ret_start
419 && copy_start + copy_num <= ret_end)
420 nregs -= copy_num;
421 else if (!multi_reg_return
422 || !targetm.calls.function_value_regno_p
423 (copy_start))
424 break;
425 last_insn = return_copy;
427 /* ??? Exception handling can lead to the return value
428 copy being already separated from the return value use,
429 as in unwind-dw2.c .
430 Similarly, conditionally returning without a value,
431 and conditionally using builtin_return can lead to an
432 isolated use. */
433 if (return_copy == BB_HEAD (src_bb))
435 short_block = true;
436 break;
438 last_insn = return_copy;
440 while (nregs);
442 /* If we didn't see a full return value copy, verify that there
443 is a plausible reason for this. If some, but not all of the
444 return register is likely spilled, we can expect that there
445 is a copy for the likely spilled part. */
446 gcc_assert (!nregs
447 || forced_late_switch
448 || short_block
449 || !(targetm.class_likely_spilled_p
450 (REGNO_REG_CLASS (ret_start)))
451 || (nregs
452 != hard_regno_nregs[ret_start][GET_MODE (ret_reg)])
453 /* For multi-hard-register floating point
454 values, sometimes the likely-spilled part
455 is ordinarily copied first, then the other
456 part is set with an arithmetic operation.
457 This doesn't actually cause reload
458 failures, so let it pass. */
459 || (GET_MODE_CLASS (GET_MODE (ret_reg)) != MODE_INT
460 && nregs != 1));
462 if (!NOTE_INSN_BASIC_BLOCK_P (last_insn))
464 before_return_copy
465 = emit_note_before (NOTE_INSN_DELETED, last_insn);
466 /* Instructions preceding LAST_INSN in the same block might
467 require a different mode than MODE_EXIT, so if we might
468 have such instructions, keep them in a separate block
469 from pre_exit. */
470 src_bb = split_block (src_bb,
471 PREV_INSN (before_return_copy))->dest;
473 else
474 before_return_copy = last_insn;
475 pre_exit = split_block (src_bb, before_return_copy)->src;
477 else
479 pre_exit = split_edge (eg);
483 return pre_exit;
486 /* Find all insns that need a particular mode setting, and insert the
487 necessary mode switches. Return true if we did work. */
489 static int
490 optimize_mode_switching (void)
492 int e;
493 basic_block bb;
494 bool need_commit = false;
495 static const int num_modes[] = NUM_MODES_FOR_MODE_SWITCHING;
496 #define N_ENTITIES ARRAY_SIZE (num_modes)
497 int entity_map[N_ENTITIES];
498 struct bb_info *bb_info[N_ENTITIES];
499 int i, j;
500 int n_entities = 0;
501 int max_num_modes = 0;
502 bool emitted ATTRIBUTE_UNUSED = false;
503 basic_block post_entry = 0;
504 basic_block pre_exit = 0;
505 struct edge_list *edge_list = 0;
507 /* These bitmaps are used for the LCM algorithm. */
508 sbitmap *kill, *del, *insert, *antic, *transp, *comp;
509 sbitmap *avin, *avout;
511 for (e = N_ENTITIES - 1; e >= 0; e--)
512 if (OPTIMIZE_MODE_SWITCHING (e))
514 int entry_exit_extra = 0;
516 /* Create the list of segments within each basic block.
517 If NORMAL_MODE is defined, allow for two extra
518 blocks split from the entry and exit block. */
519 if (targetm.mode_switching.entry && targetm.mode_switching.exit)
520 entry_exit_extra = 3;
522 bb_info[n_entities]
523 = XCNEWVEC (struct bb_info,
524 last_basic_block_for_fn (cfun) + entry_exit_extra);
525 entity_map[n_entities++] = e;
526 if (num_modes[e] > max_num_modes)
527 max_num_modes = num_modes[e];
530 if (! n_entities)
531 return 0;
533 /* Make sure if MODE_ENTRY is defined MODE_EXIT is defined. */
534 gcc_assert ((targetm.mode_switching.entry && targetm.mode_switching.exit)
535 || (!targetm.mode_switching.entry
536 && !targetm.mode_switching.exit));
538 if (targetm.mode_switching.entry && targetm.mode_switching.exit)
540 /* Split the edge from the entry block, so that we can note that
541 there NORMAL_MODE is supplied. */
542 post_entry = split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
543 pre_exit = create_pre_exit (n_entities, entity_map, num_modes);
546 df_analyze ();
548 /* Create the bitmap vectors. */
549 antic = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
550 n_entities * max_num_modes);
551 transp = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
552 n_entities * max_num_modes);
553 comp = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
554 n_entities * max_num_modes);
555 avin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
556 n_entities * max_num_modes);
557 avout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
558 n_entities * max_num_modes);
559 kill = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
560 n_entities * max_num_modes);
562 bitmap_vector_ones (transp, last_basic_block_for_fn (cfun));
563 bitmap_vector_clear (antic, last_basic_block_for_fn (cfun));
564 bitmap_vector_clear (comp, last_basic_block_for_fn (cfun));
566 for (j = n_entities - 1; j >= 0; j--)
568 int e = entity_map[j];
569 int no_mode = num_modes[e];
570 struct bb_info *info = bb_info[j];
571 rtx_insn *insn;
573 /* Determine what the first use (if any) need for a mode of entity E is.
574 This will be the mode that is anticipatable for this block.
575 Also compute the initial transparency settings. */
576 FOR_EACH_BB_FN (bb, cfun)
578 struct seginfo *ptr;
579 int last_mode = no_mode;
580 bool any_set_required = false;
581 HARD_REG_SET live_now;
583 info[bb->index].mode_out = info[bb->index].mode_in = no_mode;
585 REG_SET_TO_HARD_REG_SET (live_now, df_get_live_in (bb));
587 /* Pretend the mode is clobbered across abnormal edges. */
589 edge_iterator ei;
590 edge eg;
591 FOR_EACH_EDGE (eg, ei, bb->preds)
592 if (eg->flags & EDGE_COMPLEX)
593 break;
594 if (eg)
596 rtx_insn *ins_pos = BB_HEAD (bb);
597 if (LABEL_P (ins_pos))
598 ins_pos = NEXT_INSN (ins_pos);
599 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (ins_pos));
600 if (ins_pos != BB_END (bb))
601 ins_pos = NEXT_INSN (ins_pos);
602 ptr = new_seginfo (no_mode, ins_pos, bb->index, live_now);
603 add_seginfo (info + bb->index, ptr);
604 for (i = 0; i < no_mode; i++)
605 clear_mode_bit (transp[bb->index], j, i);
609 FOR_BB_INSNS (bb, insn)
611 if (INSN_P (insn))
613 int mode = targetm.mode_switching.needed (e, insn);
614 rtx link;
616 if (mode != no_mode && mode != last_mode)
618 any_set_required = true;
619 last_mode = mode;
620 ptr = new_seginfo (mode, insn, bb->index, live_now);
621 add_seginfo (info + bb->index, ptr);
622 for (i = 0; i < no_mode; i++)
623 clear_mode_bit (transp[bb->index], j, i);
626 if (targetm.mode_switching.after)
627 last_mode = targetm.mode_switching.after (e, last_mode,
628 insn);
630 /* Update LIVE_NOW. */
631 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
632 if (REG_NOTE_KIND (link) == REG_DEAD)
633 reg_dies (XEXP (link, 0), &live_now);
635 note_stores (PATTERN (insn), reg_becomes_live, &live_now);
636 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
637 if (REG_NOTE_KIND (link) == REG_UNUSED)
638 reg_dies (XEXP (link, 0), &live_now);
642 info[bb->index].computing = last_mode;
643 /* Check for blocks without ANY mode requirements.
644 N.B. because of MODE_AFTER, last_mode might still
645 be different from no_mode, in which case we need to
646 mark the block as nontransparent. */
647 if (!any_set_required)
649 ptr = new_seginfo (no_mode, BB_END (bb), bb->index, live_now);
650 add_seginfo (info + bb->index, ptr);
651 if (last_mode != no_mode)
652 for (i = 0; i < no_mode; i++)
653 clear_mode_bit (transp[bb->index], j, i);
656 if (targetm.mode_switching.entry && targetm.mode_switching.exit)
658 int mode = targetm.mode_switching.entry (e);
660 info[post_entry->index].mode_out =
661 info[post_entry->index].mode_in = no_mode;
662 if (pre_exit)
664 info[pre_exit->index].mode_out =
665 info[pre_exit->index].mode_in = no_mode;
668 if (mode != no_mode)
670 bb = post_entry;
672 /* By always making this nontransparent, we save
673 an extra check in make_preds_opaque. We also
674 need this to avoid confusing pre_edge_lcm when
675 antic is cleared but transp and comp are set. */
676 for (i = 0; i < no_mode; i++)
677 clear_mode_bit (transp[bb->index], j, i);
679 /* Insert a fake computing definition of MODE into entry
680 blocks which compute no mode. This represents the mode on
681 entry. */
682 info[bb->index].computing = mode;
684 if (pre_exit)
685 info[pre_exit->index].seginfo->mode =
686 targetm.mode_switching.exit (e);
690 /* Set the anticipatable and computing arrays. */
691 for (i = 0; i < no_mode; i++)
693 int m = targetm.mode_switching.priority (entity_map[j], i);
695 FOR_EACH_BB_FN (bb, cfun)
697 if (info[bb->index].seginfo->mode == m)
698 set_mode_bit (antic[bb->index], j, m);
700 if (info[bb->index].computing == m)
701 set_mode_bit (comp[bb->index], j, m);
706 /* Calculate the optimal locations for the
707 placement mode switches to modes with priority I. */
709 FOR_EACH_BB_FN (bb, cfun)
710 bitmap_not (kill[bb->index], transp[bb->index]);
712 edge_list = pre_edge_lcm_avs (n_entities * max_num_modes, transp, comp, antic,
713 kill, avin, avout, &insert, &del);
715 for (j = n_entities - 1; j >= 0; j--)
717 int no_mode = num_modes[entity_map[j]];
719 /* Insert all mode sets that have been inserted by lcm. */
721 for (int ed = NUM_EDGES (edge_list) - 1; ed >= 0; ed--)
723 edge eg = INDEX_EDGE (edge_list, ed);
725 eg->aux = (void *)(intptr_t)-1;
727 for (i = 0; i < no_mode; i++)
729 int m = targetm.mode_switching.priority (entity_map[j], i);
730 if (mode_bit_p (insert[ed], j, m))
732 eg->aux = (void *)(intptr_t)m;
733 break;
738 FOR_EACH_BB_FN (bb, cfun)
740 struct bb_info *info = bb_info[j];
741 int last_mode = no_mode;
743 /* intialize mode in availability for bb. */
744 for (i = 0; i < no_mode; i++)
745 if (mode_bit_p (avout[bb->index], j, i))
747 if (last_mode == no_mode)
748 last_mode = i;
749 if (last_mode != i)
751 last_mode = no_mode;
752 break;
755 info[bb->index].mode_out = last_mode;
757 /* intialize mode out availability for bb. */
758 last_mode = no_mode;
759 for (i = 0; i < no_mode; i++)
760 if (mode_bit_p (avin[bb->index], j, i))
762 if (last_mode == no_mode)
763 last_mode = i;
764 if (last_mode != i)
766 last_mode = no_mode;
767 break;
770 info[bb->index].mode_in = last_mode;
772 for (i = 0; i < no_mode; i++)
773 if (mode_bit_p (del[bb->index], j, i))
774 info[bb->index].seginfo->mode = no_mode;
777 /* Now output the remaining mode sets in all the segments. */
779 /* In case there was no mode inserted. the mode information on the edge
780 might not be complete.
781 Update mode info on edges and commit pending mode sets. */
782 need_commit |= commit_mode_sets (edge_list, entity_map[j], bb_info[j]);
784 /* Reset modes for next entity. */
785 clear_aux_for_edges ();
787 FOR_EACH_BB_FN (bb, cfun)
789 struct seginfo *ptr, *next;
790 int cur_mode = bb_info[j][bb->index].mode_in;
792 for (ptr = bb_info[j][bb->index].seginfo; ptr; ptr = next)
794 next = ptr->next;
795 if (ptr->mode != no_mode)
797 rtx_insn *mode_set;
799 rtl_profile_for_bb (bb);
800 start_sequence ();
802 targetm.mode_switching.emit (entity_map[j], ptr->mode,
803 cur_mode, ptr->regs_live);
804 mode_set = get_insns ();
805 end_sequence ();
807 /* modes kill each other inside a basic block. */
808 cur_mode = ptr->mode;
810 /* Insert MODE_SET only if it is nonempty. */
811 if (mode_set != NULL_RTX)
813 emitted = true;
814 if (NOTE_INSN_BASIC_BLOCK_P (ptr->insn_ptr))
815 /* We need to emit the insns in a FIFO-like manner,
816 i.e. the first to be emitted at our insertion
817 point ends up first in the instruction steam.
818 Because we made sure that NOTE_INSN_BASIC_BLOCK is
819 only used for initially empty basic blocks, we
820 can achieve this by appending at the end of
821 the block. */
822 emit_insn_after
823 (mode_set, BB_END (NOTE_BASIC_BLOCK (ptr->insn_ptr)));
824 else
825 emit_insn_before (mode_set, ptr->insn_ptr);
828 default_rtl_profile ();
831 free (ptr);
835 free (bb_info[j]);
838 free_edge_list (edge_list);
840 /* Finished. Free up all the things we've allocated. */
841 sbitmap_vector_free (del);
842 sbitmap_vector_free (insert);
843 sbitmap_vector_free (kill);
844 sbitmap_vector_free (antic);
845 sbitmap_vector_free (transp);
846 sbitmap_vector_free (comp);
847 sbitmap_vector_free (avin);
848 sbitmap_vector_free (avout);
850 if (need_commit)
851 commit_edge_insertions ();
853 if (targetm.mode_switching.entry && targetm.mode_switching.exit)
854 cleanup_cfg (CLEANUP_NO_INSN_DEL);
855 else if (!need_commit && !emitted)
856 return 0;
858 return 1;
861 #endif /* OPTIMIZE_MODE_SWITCHING */
863 namespace {
865 const pass_data pass_data_mode_switching =
867 RTL_PASS, /* type */
868 "mode_sw", /* name */
869 OPTGROUP_NONE, /* optinfo_flags */
870 TV_MODE_SWITCH, /* tv_id */
871 0, /* properties_required */
872 0, /* properties_provided */
873 0, /* properties_destroyed */
874 0, /* todo_flags_start */
875 TODO_df_finish, /* todo_flags_finish */
878 class pass_mode_switching : public rtl_opt_pass
880 public:
881 pass_mode_switching (gcc::context *ctxt)
882 : rtl_opt_pass (pass_data_mode_switching, ctxt)
885 /* opt_pass methods: */
886 /* The epiphany backend creates a second instance of this pass, so we need
887 a clone method. */
888 opt_pass * clone () { return new pass_mode_switching (m_ctxt); }
889 virtual bool gate (function *)
891 #ifdef OPTIMIZE_MODE_SWITCHING
892 return true;
893 #else
894 return false;
895 #endif
898 virtual unsigned int execute (function *)
900 #ifdef OPTIMIZE_MODE_SWITCHING
901 optimize_mode_switching ();
902 #endif /* OPTIMIZE_MODE_SWITCHING */
903 return 0;
906 }; // class pass_mode_switching
908 } // anon namespace
910 rtl_opt_pass *
911 make_pass_mode_switching (gcc::context *ctxt)
913 return new pass_mode_switching (ctxt);