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