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