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