2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / final.c
blob399c2554ae0811a25649a824ea9db17c92b9ba25
1 /* Convert RTL to assembler code and output it, for GNU compiler.
2 Copyright (C) 1987-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 /* This is the final pass of the compiler.
21 It looks at the rtl code for a function and outputs assembler code.
23 Call `final_start_function' to output the assembler code for function entry,
24 `final' to output assembler code for some RTL code,
25 `final_end_function' to output assembler code for function exit.
26 If a function is compiled in several pieces, each piece is
27 output separately with `final'.
29 Some optimizations are also done at this level.
30 Move instructions that were made unnecessary by good register allocation
31 are detected and omitted from the output. (Though most of these
32 are removed by the last jump pass.)
34 Instructions to set the condition codes are omitted when it can be
35 seen that the condition codes already had the desired values.
37 In some cases it is sufficient if the inherited condition codes
38 have related values, but this may require the following insn
39 (the one that tests the condition codes) to be modified.
41 The code for the function prologue and epilogue are generated
42 directly in assembler by the target functions function_prologue and
43 function_epilogue. Those instructions never exist as rtl. */
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "tm.h"
49 #include "alias.h"
50 #include "symtab.h"
51 #include "tree.h"
52 #include "varasm.h"
53 #include "hard-reg-set.h"
54 #include "rtl.h"
55 #include "tm_p.h"
56 #include "regs.h"
57 #include "insn-config.h"
58 #include "insn-attr.h"
59 #include "recog.h"
60 #include "conditions.h"
61 #include "flags.h"
62 #include "output.h"
63 #include "except.h"
64 #include "function.h"
65 #include "rtl-error.h"
66 #include "toplev.h" /* exact_log2, floor_log2 */
67 #include "reload.h"
68 #include "intl.h"
69 #include "predict.h"
70 #include "dominance.h"
71 #include "cfg.h"
72 #include "cfgrtl.h"
73 #include "basic-block.h"
74 #include "target.h"
75 #include "targhooks.h"
76 #include "debug.h"
77 #include "expmed.h"
78 #include "dojump.h"
79 #include "explow.h"
80 #include "calls.h"
81 #include "emit-rtl.h"
82 #include "stmt.h"
83 #include "expr.h"
84 #include "tree-pass.h"
85 #include "plugin-api.h"
86 #include "ipa-ref.h"
87 #include "cgraph.h"
88 #include "tree-ssa.h"
89 #include "coverage.h"
90 #include "df.h"
91 #include "cfgloop.h"
92 #include "params.h"
93 #include "tree-pretty-print.h" /* for dump_function_header */
94 #include "asan.h"
95 #include "wide-int-print.h"
96 #include "rtl-iter.h"
98 #ifdef XCOFF_DEBUGGING_INFO
99 #include "xcoffout.h" /* Needed for external data
100 declarations for e.g. AIX 4.x. */
101 #endif
103 #include "dwarf2out.h"
105 #ifdef DBX_DEBUGGING_INFO
106 #include "dbxout.h"
107 #endif
109 #ifdef SDB_DEBUGGING_INFO
110 #include "sdbout.h"
111 #endif
113 /* Most ports that aren't using cc0 don't need to define CC_STATUS_INIT.
114 So define a null default for it to save conditionalization later. */
115 #ifndef CC_STATUS_INIT
116 #define CC_STATUS_INIT
117 #endif
119 /* Is the given character a logical line separator for the assembler? */
120 #ifndef IS_ASM_LOGICAL_LINE_SEPARATOR
121 #define IS_ASM_LOGICAL_LINE_SEPARATOR(C, STR) ((C) == ';')
122 #endif
124 #ifndef JUMP_TABLES_IN_TEXT_SECTION
125 #define JUMP_TABLES_IN_TEXT_SECTION 0
126 #endif
128 /* Bitflags used by final_scan_insn. */
129 #define SEEN_NOTE 1
130 #define SEEN_EMITTED 2
132 /* Last insn processed by final_scan_insn. */
133 static rtx_insn *debug_insn;
134 rtx_insn *current_output_insn;
136 /* Line number of last NOTE. */
137 static int last_linenum;
139 /* Last discriminator written to assembly. */
140 static int last_discriminator;
142 /* Discriminator of current block. */
143 static int discriminator;
145 /* Highest line number in current block. */
146 static int high_block_linenum;
148 /* Likewise for function. */
149 static int high_function_linenum;
151 /* Filename of last NOTE. */
152 static const char *last_filename;
154 /* Override filename and line number. */
155 static const char *override_filename;
156 static int override_linenum;
158 /* Whether to force emission of a line note before the next insn. */
159 static bool force_source_line = false;
161 extern const int length_unit_log; /* This is defined in insn-attrtab.c. */
163 /* Nonzero while outputting an `asm' with operands.
164 This means that inconsistencies are the user's fault, so don't die.
165 The precise value is the insn being output, to pass to error_for_asm. */
166 const rtx_insn *this_is_asm_operands;
168 /* Number of operands of this insn, for an `asm' with operands. */
169 static unsigned int insn_noperands;
171 /* Compare optimization flag. */
173 static rtx last_ignored_compare = 0;
175 /* Assign a unique number to each insn that is output.
176 This can be used to generate unique local labels. */
178 static int insn_counter = 0;
180 /* This variable contains machine-dependent flags (defined in tm.h)
181 set and examined by output routines
182 that describe how to interpret the condition codes properly. */
184 CC_STATUS cc_status;
186 /* During output of an insn, this contains a copy of cc_status
187 from before the insn. */
189 CC_STATUS cc_prev_status;
191 /* Number of unmatched NOTE_INSN_BLOCK_BEG notes we have seen. */
193 static int block_depth;
195 /* Nonzero if have enabled APP processing of our assembler output. */
197 static int app_on;
199 /* If we are outputting an insn sequence, this contains the sequence rtx.
200 Zero otherwise. */
202 rtx_sequence *final_sequence;
204 #ifdef ASSEMBLER_DIALECT
206 /* Number of the assembler dialect to use, starting at 0. */
207 static int dialect_number;
208 #endif
210 /* Nonnull if the insn currently being emitted was a COND_EXEC pattern. */
211 rtx current_insn_predicate;
213 /* True if printing into -fdump-final-insns= dump. */
214 bool final_insns_dump_p;
216 /* True if profile_function should be called, but hasn't been called yet. */
217 static bool need_profile_function;
219 static int asm_insn_count (rtx);
220 static void profile_function (FILE *);
221 static void profile_after_prologue (FILE *);
222 static bool notice_source_line (rtx_insn *, bool *);
223 static rtx walk_alter_subreg (rtx *, bool *);
224 static void output_asm_name (void);
225 static void output_alternate_entry_point (FILE *, rtx_insn *);
226 static tree get_mem_expr_from_op (rtx, int *);
227 static void output_asm_operand_names (rtx *, int *, int);
228 #ifdef LEAF_REGISTERS
229 static void leaf_renumber_regs (rtx_insn *);
230 #endif
231 #if HAVE_cc0
232 static int alter_cond (rtx);
233 #endif
234 #ifndef ADDR_VEC_ALIGN
235 static int final_addr_vec_align (rtx);
236 #endif
237 static int align_fuzz (rtx, rtx, int, unsigned);
238 static void collect_fn_hard_reg_usage (void);
239 static tree get_call_fndecl (rtx_insn *);
241 /* Initialize data in final at the beginning of a compilation. */
243 void
244 init_final (const char *filename ATTRIBUTE_UNUSED)
246 app_on = 0;
247 final_sequence = 0;
249 #ifdef ASSEMBLER_DIALECT
250 dialect_number = ASSEMBLER_DIALECT;
251 #endif
254 /* Default target function prologue and epilogue assembler output.
256 If not overridden for epilogue code, then the function body itself
257 contains return instructions wherever needed. */
258 void
259 default_function_pro_epilogue (FILE *file ATTRIBUTE_UNUSED,
260 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
264 void
265 default_function_switched_text_sections (FILE *file ATTRIBUTE_UNUSED,
266 tree decl ATTRIBUTE_UNUSED,
267 bool new_is_cold ATTRIBUTE_UNUSED)
271 /* Default target hook that outputs nothing to a stream. */
272 void
273 no_asm_to_stream (FILE *file ATTRIBUTE_UNUSED)
277 /* Enable APP processing of subsequent output.
278 Used before the output from an `asm' statement. */
280 void
281 app_enable (void)
283 if (! app_on)
285 fputs (ASM_APP_ON, asm_out_file);
286 app_on = 1;
290 /* Disable APP processing of subsequent output.
291 Called from varasm.c before most kinds of output. */
293 void
294 app_disable (void)
296 if (app_on)
298 fputs (ASM_APP_OFF, asm_out_file);
299 app_on = 0;
303 /* Return the number of slots filled in the current
304 delayed branch sequence (we don't count the insn needing the
305 delay slot). Zero if not in a delayed branch sequence. */
307 #ifdef DELAY_SLOTS
309 dbr_sequence_length (void)
311 if (final_sequence != 0)
312 return XVECLEN (final_sequence, 0) - 1;
313 else
314 return 0;
316 #endif
318 /* The next two pages contain routines used to compute the length of an insn
319 and to shorten branches. */
321 /* Arrays for insn lengths, and addresses. The latter is referenced by
322 `insn_current_length'. */
324 static int *insn_lengths;
326 vec<int> insn_addresses_;
328 /* Max uid for which the above arrays are valid. */
329 static int insn_lengths_max_uid;
331 /* Address of insn being processed. Used by `insn_current_length'. */
332 int insn_current_address;
334 /* Address of insn being processed in previous iteration. */
335 int insn_last_address;
337 /* known invariant alignment of insn being processed. */
338 int insn_current_align;
340 /* After shorten_branches, for any insn, uid_align[INSN_UID (insn)]
341 gives the next following alignment insn that increases the known
342 alignment, or NULL_RTX if there is no such insn.
343 For any alignment obtained this way, we can again index uid_align with
344 its uid to obtain the next following align that in turn increases the
345 alignment, till we reach NULL_RTX; the sequence obtained this way
346 for each insn we'll call the alignment chain of this insn in the following
347 comments. */
349 struct label_alignment
351 short alignment;
352 short max_skip;
355 static rtx *uid_align;
356 static int *uid_shuid;
357 static struct label_alignment *label_align;
359 /* Indicate that branch shortening hasn't yet been done. */
361 void
362 init_insn_lengths (void)
364 if (uid_shuid)
366 free (uid_shuid);
367 uid_shuid = 0;
369 if (insn_lengths)
371 free (insn_lengths);
372 insn_lengths = 0;
373 insn_lengths_max_uid = 0;
375 if (HAVE_ATTR_length)
376 INSN_ADDRESSES_FREE ();
377 if (uid_align)
379 free (uid_align);
380 uid_align = 0;
384 /* Obtain the current length of an insn. If branch shortening has been done,
385 get its actual length. Otherwise, use FALLBACK_FN to calculate the
386 length. */
387 static int
388 get_attr_length_1 (rtx_insn *insn, int (*fallback_fn) (rtx_insn *))
390 rtx body;
391 int i;
392 int length = 0;
394 if (!HAVE_ATTR_length)
395 return 0;
397 if (insn_lengths_max_uid > INSN_UID (insn))
398 return insn_lengths[INSN_UID (insn)];
399 else
400 switch (GET_CODE (insn))
402 case NOTE:
403 case BARRIER:
404 case CODE_LABEL:
405 case DEBUG_INSN:
406 return 0;
408 case CALL_INSN:
409 case JUMP_INSN:
410 length = fallback_fn (insn);
411 break;
413 case INSN:
414 body = PATTERN (insn);
415 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
416 return 0;
418 else if (GET_CODE (body) == ASM_INPUT || asm_noperands (body) >= 0)
419 length = asm_insn_count (body) * fallback_fn (insn);
420 else if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (body))
421 for (i = 0; i < seq->len (); i++)
422 length += get_attr_length_1 (seq->insn (i), fallback_fn);
423 else
424 length = fallback_fn (insn);
425 break;
427 default:
428 break;
431 #ifdef ADJUST_INSN_LENGTH
432 ADJUST_INSN_LENGTH (insn, length);
433 #endif
434 return length;
437 /* Obtain the current length of an insn. If branch shortening has been done,
438 get its actual length. Otherwise, get its maximum length. */
440 get_attr_length (rtx_insn *insn)
442 return get_attr_length_1 (insn, insn_default_length);
445 /* Obtain the current length of an insn. If branch shortening has been done,
446 get its actual length. Otherwise, get its minimum length. */
448 get_attr_min_length (rtx_insn *insn)
450 return get_attr_length_1 (insn, insn_min_length);
453 /* Code to handle alignment inside shorten_branches. */
455 /* Here is an explanation how the algorithm in align_fuzz can give
456 proper results:
458 Call a sequence of instructions beginning with alignment point X
459 and continuing until the next alignment point `block X'. When `X'
460 is used in an expression, it means the alignment value of the
461 alignment point.
463 Call the distance between the start of the first insn of block X, and
464 the end of the last insn of block X `IX', for the `inner size of X'.
465 This is clearly the sum of the instruction lengths.
467 Likewise with the next alignment-delimited block following X, which we
468 shall call block Y.
470 Call the distance between the start of the first insn of block X, and
471 the start of the first insn of block Y `OX', for the `outer size of X'.
473 The estimated padding is then OX - IX.
475 OX can be safely estimated as
477 if (X >= Y)
478 OX = round_up(IX, Y)
479 else
480 OX = round_up(IX, X) + Y - X
482 Clearly est(IX) >= real(IX), because that only depends on the
483 instruction lengths, and those being overestimated is a given.
485 Clearly round_up(foo, Z) >= round_up(bar, Z) if foo >= bar, so
486 we needn't worry about that when thinking about OX.
488 When X >= Y, the alignment provided by Y adds no uncertainty factor
489 for branch ranges starting before X, so we can just round what we have.
490 But when X < Y, we don't know anything about the, so to speak,
491 `middle bits', so we have to assume the worst when aligning up from an
492 address mod X to one mod Y, which is Y - X. */
494 #ifndef LABEL_ALIGN
495 #define LABEL_ALIGN(LABEL) align_labels_log
496 #endif
498 #ifndef LOOP_ALIGN
499 #define LOOP_ALIGN(LABEL) align_loops_log
500 #endif
502 #ifndef LABEL_ALIGN_AFTER_BARRIER
503 #define LABEL_ALIGN_AFTER_BARRIER(LABEL) 0
504 #endif
506 #ifndef JUMP_ALIGN
507 #define JUMP_ALIGN(LABEL) align_jumps_log
508 #endif
511 default_label_align_after_barrier_max_skip (rtx_insn *insn ATTRIBUTE_UNUSED)
513 return 0;
517 default_loop_align_max_skip (rtx_insn *insn ATTRIBUTE_UNUSED)
519 return align_loops_max_skip;
523 default_label_align_max_skip (rtx_insn *insn ATTRIBUTE_UNUSED)
525 return align_labels_max_skip;
529 default_jump_align_max_skip (rtx_insn *insn ATTRIBUTE_UNUSED)
531 return align_jumps_max_skip;
534 #ifndef ADDR_VEC_ALIGN
535 static int
536 final_addr_vec_align (rtx addr_vec)
538 int align = GET_MODE_SIZE (GET_MODE (PATTERN (addr_vec)));
540 if (align > BIGGEST_ALIGNMENT / BITS_PER_UNIT)
541 align = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
542 return exact_log2 (align);
546 #define ADDR_VEC_ALIGN(ADDR_VEC) final_addr_vec_align (ADDR_VEC)
547 #endif
549 #ifndef INSN_LENGTH_ALIGNMENT
550 #define INSN_LENGTH_ALIGNMENT(INSN) length_unit_log
551 #endif
553 #define INSN_SHUID(INSN) (uid_shuid[INSN_UID (INSN)])
555 static int min_labelno, max_labelno;
557 #define LABEL_TO_ALIGNMENT(LABEL) \
558 (label_align[CODE_LABEL_NUMBER (LABEL) - min_labelno].alignment)
560 #define LABEL_TO_MAX_SKIP(LABEL) \
561 (label_align[CODE_LABEL_NUMBER (LABEL) - min_labelno].max_skip)
563 /* For the benefit of port specific code do this also as a function. */
566 label_to_alignment (rtx label)
568 if (CODE_LABEL_NUMBER (label) <= max_labelno)
569 return LABEL_TO_ALIGNMENT (label);
570 return 0;
574 label_to_max_skip (rtx label)
576 if (CODE_LABEL_NUMBER (label) <= max_labelno)
577 return LABEL_TO_MAX_SKIP (label);
578 return 0;
581 /* The differences in addresses
582 between a branch and its target might grow or shrink depending on
583 the alignment the start insn of the range (the branch for a forward
584 branch or the label for a backward branch) starts out on; if these
585 differences are used naively, they can even oscillate infinitely.
586 We therefore want to compute a 'worst case' address difference that
587 is independent of the alignment the start insn of the range end
588 up on, and that is at least as large as the actual difference.
589 The function align_fuzz calculates the amount we have to add to the
590 naively computed difference, by traversing the part of the alignment
591 chain of the start insn of the range that is in front of the end insn
592 of the range, and considering for each alignment the maximum amount
593 that it might contribute to a size increase.
595 For casesi tables, we also want to know worst case minimum amounts of
596 address difference, in case a machine description wants to introduce
597 some common offset that is added to all offsets in a table.
598 For this purpose, align_fuzz with a growth argument of 0 computes the
599 appropriate adjustment. */
601 /* Compute the maximum delta by which the difference of the addresses of
602 START and END might grow / shrink due to a different address for start
603 which changes the size of alignment insns between START and END.
604 KNOWN_ALIGN_LOG is the alignment known for START.
605 GROWTH should be ~0 if the objective is to compute potential code size
606 increase, and 0 if the objective is to compute potential shrink.
607 The return value is undefined for any other value of GROWTH. */
609 static int
610 align_fuzz (rtx start, rtx end, int known_align_log, unsigned int growth)
612 int uid = INSN_UID (start);
613 rtx align_label;
614 int known_align = 1 << known_align_log;
615 int end_shuid = INSN_SHUID (end);
616 int fuzz = 0;
618 for (align_label = uid_align[uid]; align_label; align_label = uid_align[uid])
620 int align_addr, new_align;
622 uid = INSN_UID (align_label);
623 align_addr = INSN_ADDRESSES (uid) - insn_lengths[uid];
624 if (uid_shuid[uid] > end_shuid)
625 break;
626 known_align_log = LABEL_TO_ALIGNMENT (align_label);
627 new_align = 1 << known_align_log;
628 if (new_align < known_align)
629 continue;
630 fuzz += (-align_addr ^ growth) & (new_align - known_align);
631 known_align = new_align;
633 return fuzz;
636 /* Compute a worst-case reference address of a branch so that it
637 can be safely used in the presence of aligned labels. Since the
638 size of the branch itself is unknown, the size of the branch is
639 not included in the range. I.e. for a forward branch, the reference
640 address is the end address of the branch as known from the previous
641 branch shortening pass, minus a value to account for possible size
642 increase due to alignment. For a backward branch, it is the start
643 address of the branch as known from the current pass, plus a value
644 to account for possible size increase due to alignment.
645 NB.: Therefore, the maximum offset allowed for backward branches needs
646 to exclude the branch size. */
649 insn_current_reference_address (rtx_insn *branch)
651 rtx dest;
652 int seq_uid;
654 if (! INSN_ADDRESSES_SET_P ())
655 return 0;
657 rtx_insn *seq = NEXT_INSN (PREV_INSN (branch));
658 seq_uid = INSN_UID (seq);
659 if (!JUMP_P (branch))
660 /* This can happen for example on the PA; the objective is to know the
661 offset to address something in front of the start of the function.
662 Thus, we can treat it like a backward branch.
663 We assume here that FUNCTION_BOUNDARY / BITS_PER_UNIT is larger than
664 any alignment we'd encounter, so we skip the call to align_fuzz. */
665 return insn_current_address;
666 dest = JUMP_LABEL (branch);
668 /* BRANCH has no proper alignment chain set, so use SEQ.
669 BRANCH also has no INSN_SHUID. */
670 if (INSN_SHUID (seq) < INSN_SHUID (dest))
672 /* Forward branch. */
673 return (insn_last_address + insn_lengths[seq_uid]
674 - align_fuzz (seq, dest, length_unit_log, ~0));
676 else
678 /* Backward branch. */
679 return (insn_current_address
680 + align_fuzz (dest, seq, length_unit_log, ~0));
684 /* Compute branch alignments based on frequency information in the
685 CFG. */
687 unsigned int
688 compute_alignments (void)
690 int log, max_skip, max_log;
691 basic_block bb;
692 int freq_max = 0;
693 int freq_threshold = 0;
695 if (label_align)
697 free (label_align);
698 label_align = 0;
701 max_labelno = max_label_num ();
702 min_labelno = get_first_label_num ();
703 label_align = XCNEWVEC (struct label_alignment, max_labelno - min_labelno + 1);
705 /* If not optimizing or optimizing for size, don't assign any alignments. */
706 if (! optimize || optimize_function_for_size_p (cfun))
707 return 0;
709 if (dump_file)
711 dump_reg_info (dump_file);
712 dump_flow_info (dump_file, TDF_DETAILS);
713 flow_loops_dump (dump_file, NULL, 1);
715 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
716 FOR_EACH_BB_FN (bb, cfun)
717 if (bb->frequency > freq_max)
718 freq_max = bb->frequency;
719 freq_threshold = freq_max / PARAM_VALUE (PARAM_ALIGN_THRESHOLD);
721 if (dump_file)
722 fprintf (dump_file, "freq_max: %i\n",freq_max);
723 FOR_EACH_BB_FN (bb, cfun)
725 rtx_insn *label = BB_HEAD (bb);
726 int fallthru_frequency = 0, branch_frequency = 0, has_fallthru = 0;
727 edge e;
728 edge_iterator ei;
730 if (!LABEL_P (label)
731 || optimize_bb_for_size_p (bb))
733 if (dump_file)
734 fprintf (dump_file,
735 "BB %4i freq %4i loop %2i loop_depth %2i skipped.\n",
736 bb->index, bb->frequency, bb->loop_father->num,
737 bb_loop_depth (bb));
738 continue;
740 max_log = LABEL_ALIGN (label);
741 max_skip = targetm.asm_out.label_align_max_skip (label);
743 FOR_EACH_EDGE (e, ei, bb->preds)
745 if (e->flags & EDGE_FALLTHRU)
746 has_fallthru = 1, fallthru_frequency += EDGE_FREQUENCY (e);
747 else
748 branch_frequency += EDGE_FREQUENCY (e);
750 if (dump_file)
752 fprintf (dump_file, "BB %4i freq %4i loop %2i loop_depth"
753 " %2i fall %4i branch %4i",
754 bb->index, bb->frequency, bb->loop_father->num,
755 bb_loop_depth (bb),
756 fallthru_frequency, branch_frequency);
757 if (!bb->loop_father->inner && bb->loop_father->num)
758 fprintf (dump_file, " inner_loop");
759 if (bb->loop_father->header == bb)
760 fprintf (dump_file, " loop_header");
761 fprintf (dump_file, "\n");
764 /* There are two purposes to align block with no fallthru incoming edge:
765 1) to avoid fetch stalls when branch destination is near cache boundary
766 2) to improve cache efficiency in case the previous block is not executed
767 (so it does not need to be in the cache).
769 We to catch first case, we align frequently executed blocks.
770 To catch the second, we align blocks that are executed more frequently
771 than the predecessor and the predecessor is likely to not be executed
772 when function is called. */
774 if (!has_fallthru
775 && (branch_frequency > freq_threshold
776 || (bb->frequency > bb->prev_bb->frequency * 10
777 && (bb->prev_bb->frequency
778 <= ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency / 2))))
780 log = JUMP_ALIGN (label);
781 if (dump_file)
782 fprintf (dump_file, " jump alignment added.\n");
783 if (max_log < log)
785 max_log = log;
786 max_skip = targetm.asm_out.jump_align_max_skip (label);
789 /* In case block is frequent and reached mostly by non-fallthru edge,
790 align it. It is most likely a first block of loop. */
791 if (has_fallthru
792 && !(single_succ_p (bb)
793 && single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun))
794 && optimize_bb_for_speed_p (bb)
795 && branch_frequency + fallthru_frequency > freq_threshold
796 && (branch_frequency
797 > fallthru_frequency * PARAM_VALUE (PARAM_ALIGN_LOOP_ITERATIONS)))
799 log = LOOP_ALIGN (label);
800 if (dump_file)
801 fprintf (dump_file, " internal loop alignment added.\n");
802 if (max_log < log)
804 max_log = log;
805 max_skip = targetm.asm_out.loop_align_max_skip (label);
808 LABEL_TO_ALIGNMENT (label) = max_log;
809 LABEL_TO_MAX_SKIP (label) = max_skip;
812 loop_optimizer_finalize ();
813 free_dominance_info (CDI_DOMINATORS);
814 return 0;
817 /* Grow the LABEL_ALIGN array after new labels are created. */
819 static void
820 grow_label_align (void)
822 int old = max_labelno;
823 int n_labels;
824 int n_old_labels;
826 max_labelno = max_label_num ();
828 n_labels = max_labelno - min_labelno + 1;
829 n_old_labels = old - min_labelno + 1;
831 label_align = XRESIZEVEC (struct label_alignment, label_align, n_labels);
833 /* Range of labels grows monotonically in the function. Failing here
834 means that the initialization of array got lost. */
835 gcc_assert (n_old_labels <= n_labels);
837 memset (label_align + n_old_labels, 0,
838 (n_labels - n_old_labels) * sizeof (struct label_alignment));
841 /* Update the already computed alignment information. LABEL_PAIRS is a vector
842 made up of pairs of labels for which the alignment information of the first
843 element will be copied from that of the second element. */
845 void
846 update_alignments (vec<rtx> &label_pairs)
848 unsigned int i = 0;
849 rtx iter, label = NULL_RTX;
851 if (max_labelno != max_label_num ())
852 grow_label_align ();
854 FOR_EACH_VEC_ELT (label_pairs, i, iter)
855 if (i & 1)
857 LABEL_TO_ALIGNMENT (label) = LABEL_TO_ALIGNMENT (iter);
858 LABEL_TO_MAX_SKIP (label) = LABEL_TO_MAX_SKIP (iter);
860 else
861 label = iter;
864 namespace {
866 const pass_data pass_data_compute_alignments =
868 RTL_PASS, /* type */
869 "alignments", /* name */
870 OPTGROUP_NONE, /* optinfo_flags */
871 TV_NONE, /* tv_id */
872 0, /* properties_required */
873 0, /* properties_provided */
874 0, /* properties_destroyed */
875 0, /* todo_flags_start */
876 0, /* todo_flags_finish */
879 class pass_compute_alignments : public rtl_opt_pass
881 public:
882 pass_compute_alignments (gcc::context *ctxt)
883 : rtl_opt_pass (pass_data_compute_alignments, ctxt)
886 /* opt_pass methods: */
887 virtual unsigned int execute (function *) { return compute_alignments (); }
889 }; // class pass_compute_alignments
891 } // anon namespace
893 rtl_opt_pass *
894 make_pass_compute_alignments (gcc::context *ctxt)
896 return new pass_compute_alignments (ctxt);
900 /* Make a pass over all insns and compute their actual lengths by shortening
901 any branches of variable length if possible. */
903 /* shorten_branches might be called multiple times: for example, the SH
904 port splits out-of-range conditional branches in MACHINE_DEPENDENT_REORG.
905 In order to do this, it needs proper length information, which it obtains
906 by calling shorten_branches. This cannot be collapsed with
907 shorten_branches itself into a single pass unless we also want to integrate
908 reorg.c, since the branch splitting exposes new instructions with delay
909 slots. */
911 void
912 shorten_branches (rtx_insn *first)
914 rtx_insn *insn;
915 int max_uid;
916 int i;
917 int max_log;
918 int max_skip;
919 #define MAX_CODE_ALIGN 16
920 rtx_insn *seq;
921 int something_changed = 1;
922 char *varying_length;
923 rtx body;
924 int uid;
925 rtx align_tab[MAX_CODE_ALIGN];
927 /* Compute maximum UID and allocate label_align / uid_shuid. */
928 max_uid = get_max_uid ();
930 /* Free uid_shuid before reallocating it. */
931 free (uid_shuid);
933 uid_shuid = XNEWVEC (int, max_uid);
935 if (max_labelno != max_label_num ())
936 grow_label_align ();
938 /* Initialize label_align and set up uid_shuid to be strictly
939 monotonically rising with insn order. */
940 /* We use max_log here to keep track of the maximum alignment we want to
941 impose on the next CODE_LABEL (or the current one if we are processing
942 the CODE_LABEL itself). */
944 max_log = 0;
945 max_skip = 0;
947 for (insn = get_insns (), i = 1; insn; insn = NEXT_INSN (insn))
949 int log;
951 INSN_SHUID (insn) = i++;
952 if (INSN_P (insn))
953 continue;
955 if (LABEL_P (insn))
957 rtx_insn *next;
958 bool next_is_jumptable;
960 /* Merge in alignments computed by compute_alignments. */
961 log = LABEL_TO_ALIGNMENT (insn);
962 if (max_log < log)
964 max_log = log;
965 max_skip = LABEL_TO_MAX_SKIP (insn);
968 next = next_nonnote_insn (insn);
969 next_is_jumptable = next && JUMP_TABLE_DATA_P (next);
970 if (!next_is_jumptable)
972 log = LABEL_ALIGN (insn);
973 if (max_log < log)
975 max_log = log;
976 max_skip = targetm.asm_out.label_align_max_skip (insn);
979 /* ADDR_VECs only take room if read-only data goes into the text
980 section. */
981 if ((JUMP_TABLES_IN_TEXT_SECTION
982 || readonly_data_section == text_section)
983 && next_is_jumptable)
985 log = ADDR_VEC_ALIGN (next);
986 if (max_log < log)
988 max_log = log;
989 max_skip = targetm.asm_out.label_align_max_skip (insn);
992 LABEL_TO_ALIGNMENT (insn) = max_log;
993 LABEL_TO_MAX_SKIP (insn) = max_skip;
994 max_log = 0;
995 max_skip = 0;
997 else if (BARRIER_P (insn))
999 rtx_insn *label;
1001 for (label = insn; label && ! INSN_P (label);
1002 label = NEXT_INSN (label))
1003 if (LABEL_P (label))
1005 log = LABEL_ALIGN_AFTER_BARRIER (insn);
1006 if (max_log < log)
1008 max_log = log;
1009 max_skip = targetm.asm_out.label_align_after_barrier_max_skip (label);
1011 break;
1015 if (!HAVE_ATTR_length)
1016 return;
1018 /* Allocate the rest of the arrays. */
1019 insn_lengths = XNEWVEC (int, max_uid);
1020 insn_lengths_max_uid = max_uid;
1021 /* Syntax errors can lead to labels being outside of the main insn stream.
1022 Initialize insn_addresses, so that we get reproducible results. */
1023 INSN_ADDRESSES_ALLOC (max_uid);
1025 varying_length = XCNEWVEC (char, max_uid);
1027 /* Initialize uid_align. We scan instructions
1028 from end to start, and keep in align_tab[n] the last seen insn
1029 that does an alignment of at least n+1, i.e. the successor
1030 in the alignment chain for an insn that does / has a known
1031 alignment of n. */
1032 uid_align = XCNEWVEC (rtx, max_uid);
1034 for (i = MAX_CODE_ALIGN; --i >= 0;)
1035 align_tab[i] = NULL_RTX;
1036 seq = get_last_insn ();
1037 for (; seq; seq = PREV_INSN (seq))
1039 int uid = INSN_UID (seq);
1040 int log;
1041 log = (LABEL_P (seq) ? LABEL_TO_ALIGNMENT (seq) : 0);
1042 uid_align[uid] = align_tab[0];
1043 if (log)
1045 /* Found an alignment label. */
1046 uid_align[uid] = align_tab[log];
1047 for (i = log - 1; i >= 0; i--)
1048 align_tab[i] = seq;
1052 /* When optimizing, we start assuming minimum length, and keep increasing
1053 lengths as we find the need for this, till nothing changes.
1054 When not optimizing, we start assuming maximum lengths, and
1055 do a single pass to update the lengths. */
1056 bool increasing = optimize != 0;
1058 #ifdef CASE_VECTOR_SHORTEN_MODE
1059 if (optimize)
1061 /* Look for ADDR_DIFF_VECs, and initialize their minimum and maximum
1062 label fields. */
1064 int min_shuid = INSN_SHUID (get_insns ()) - 1;
1065 int max_shuid = INSN_SHUID (get_last_insn ()) + 1;
1066 int rel;
1068 for (insn = first; insn != 0; insn = NEXT_INSN (insn))
1070 rtx min_lab = NULL_RTX, max_lab = NULL_RTX, pat;
1071 int len, i, min, max, insn_shuid;
1072 int min_align;
1073 addr_diff_vec_flags flags;
1075 if (! JUMP_TABLE_DATA_P (insn)
1076 || GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
1077 continue;
1078 pat = PATTERN (insn);
1079 len = XVECLEN (pat, 1);
1080 gcc_assert (len > 0);
1081 min_align = MAX_CODE_ALIGN;
1082 for (min = max_shuid, max = min_shuid, i = len - 1; i >= 0; i--)
1084 rtx lab = XEXP (XVECEXP (pat, 1, i), 0);
1085 int shuid = INSN_SHUID (lab);
1086 if (shuid < min)
1088 min = shuid;
1089 min_lab = lab;
1091 if (shuid > max)
1093 max = shuid;
1094 max_lab = lab;
1096 if (min_align > LABEL_TO_ALIGNMENT (lab))
1097 min_align = LABEL_TO_ALIGNMENT (lab);
1099 XEXP (pat, 2) = gen_rtx_LABEL_REF (Pmode, min_lab);
1100 XEXP (pat, 3) = gen_rtx_LABEL_REF (Pmode, max_lab);
1101 insn_shuid = INSN_SHUID (insn);
1102 rel = INSN_SHUID (XEXP (XEXP (pat, 0), 0));
1103 memset (&flags, 0, sizeof (flags));
1104 flags.min_align = min_align;
1105 flags.base_after_vec = rel > insn_shuid;
1106 flags.min_after_vec = min > insn_shuid;
1107 flags.max_after_vec = max > insn_shuid;
1108 flags.min_after_base = min > rel;
1109 flags.max_after_base = max > rel;
1110 ADDR_DIFF_VEC_FLAGS (pat) = flags;
1112 if (increasing)
1113 PUT_MODE (pat, CASE_VECTOR_SHORTEN_MODE (0, 0, pat));
1116 #endif /* CASE_VECTOR_SHORTEN_MODE */
1118 /* Compute initial lengths, addresses, and varying flags for each insn. */
1119 int (*length_fun) (rtx_insn *) = increasing ? insn_min_length : insn_default_length;
1121 for (insn_current_address = 0, insn = first;
1122 insn != 0;
1123 insn_current_address += insn_lengths[uid], insn = NEXT_INSN (insn))
1125 uid = INSN_UID (insn);
1127 insn_lengths[uid] = 0;
1129 if (LABEL_P (insn))
1131 int log = LABEL_TO_ALIGNMENT (insn);
1132 if (log)
1134 int align = 1 << log;
1135 int new_address = (insn_current_address + align - 1) & -align;
1136 insn_lengths[uid] = new_address - insn_current_address;
1140 INSN_ADDRESSES (uid) = insn_current_address + insn_lengths[uid];
1142 if (NOTE_P (insn) || BARRIER_P (insn)
1143 || LABEL_P (insn) || DEBUG_INSN_P (insn))
1144 continue;
1145 if (insn->deleted ())
1146 continue;
1148 body = PATTERN (insn);
1149 if (JUMP_TABLE_DATA_P (insn))
1151 /* This only takes room if read-only data goes into the text
1152 section. */
1153 if (JUMP_TABLES_IN_TEXT_SECTION
1154 || readonly_data_section == text_section)
1155 insn_lengths[uid] = (XVECLEN (body,
1156 GET_CODE (body) == ADDR_DIFF_VEC)
1157 * GET_MODE_SIZE (GET_MODE (body)));
1158 /* Alignment is handled by ADDR_VEC_ALIGN. */
1160 else if (GET_CODE (body) == ASM_INPUT || asm_noperands (body) >= 0)
1161 insn_lengths[uid] = asm_insn_count (body) * insn_default_length (insn);
1162 else if (rtx_sequence *body_seq = dyn_cast <rtx_sequence *> (body))
1164 int i;
1165 int const_delay_slots;
1166 #ifdef DELAY_SLOTS
1167 const_delay_slots = const_num_delay_slots (body_seq->insn (0));
1168 #else
1169 const_delay_slots = 0;
1170 #endif
1171 int (*inner_length_fun) (rtx_insn *)
1172 = const_delay_slots ? length_fun : insn_default_length;
1173 /* Inside a delay slot sequence, we do not do any branch shortening
1174 if the shortening could change the number of delay slots
1175 of the branch. */
1176 for (i = 0; i < body_seq->len (); i++)
1178 rtx_insn *inner_insn = body_seq->insn (i);
1179 int inner_uid = INSN_UID (inner_insn);
1180 int inner_length;
1182 if (GET_CODE (body) == ASM_INPUT
1183 || asm_noperands (PATTERN (inner_insn)) >= 0)
1184 inner_length = (asm_insn_count (PATTERN (inner_insn))
1185 * insn_default_length (inner_insn));
1186 else
1187 inner_length = inner_length_fun (inner_insn);
1189 insn_lengths[inner_uid] = inner_length;
1190 if (const_delay_slots)
1192 if ((varying_length[inner_uid]
1193 = insn_variable_length_p (inner_insn)) != 0)
1194 varying_length[uid] = 1;
1195 INSN_ADDRESSES (inner_uid) = (insn_current_address
1196 + insn_lengths[uid]);
1198 else
1199 varying_length[inner_uid] = 0;
1200 insn_lengths[uid] += inner_length;
1203 else if (GET_CODE (body) != USE && GET_CODE (body) != CLOBBER)
1205 insn_lengths[uid] = length_fun (insn);
1206 varying_length[uid] = insn_variable_length_p (insn);
1209 /* If needed, do any adjustment. */
1210 #ifdef ADJUST_INSN_LENGTH
1211 ADJUST_INSN_LENGTH (insn, insn_lengths[uid]);
1212 if (insn_lengths[uid] < 0)
1213 fatal_insn ("negative insn length", insn);
1214 #endif
1217 /* Now loop over all the insns finding varying length insns. For each,
1218 get the current insn length. If it has changed, reflect the change.
1219 When nothing changes for a full pass, we are done. */
1221 while (something_changed)
1223 something_changed = 0;
1224 insn_current_align = MAX_CODE_ALIGN - 1;
1225 for (insn_current_address = 0, insn = first;
1226 insn != 0;
1227 insn = NEXT_INSN (insn))
1229 int new_length;
1230 #ifdef ADJUST_INSN_LENGTH
1231 int tmp_length;
1232 #endif
1233 int length_align;
1235 uid = INSN_UID (insn);
1237 if (LABEL_P (insn))
1239 int log = LABEL_TO_ALIGNMENT (insn);
1241 #ifdef CASE_VECTOR_SHORTEN_MODE
1242 /* If the mode of a following jump table was changed, we
1243 may need to update the alignment of this label. */
1244 rtx_insn *next;
1245 bool next_is_jumptable;
1247 next = next_nonnote_insn (insn);
1248 next_is_jumptable = next && JUMP_TABLE_DATA_P (next);
1249 if ((JUMP_TABLES_IN_TEXT_SECTION
1250 || readonly_data_section == text_section)
1251 && next_is_jumptable)
1253 int newlog = ADDR_VEC_ALIGN (next);
1254 if (newlog != log)
1256 log = newlog;
1257 LABEL_TO_ALIGNMENT (insn) = log;
1258 something_changed = 1;
1261 #endif
1263 if (log > insn_current_align)
1265 int align = 1 << log;
1266 int new_address= (insn_current_address + align - 1) & -align;
1267 insn_lengths[uid] = new_address - insn_current_address;
1268 insn_current_align = log;
1269 insn_current_address = new_address;
1271 else
1272 insn_lengths[uid] = 0;
1273 INSN_ADDRESSES (uid) = insn_current_address;
1274 continue;
1277 length_align = INSN_LENGTH_ALIGNMENT (insn);
1278 if (length_align < insn_current_align)
1279 insn_current_align = length_align;
1281 insn_last_address = INSN_ADDRESSES (uid);
1282 INSN_ADDRESSES (uid) = insn_current_address;
1284 #ifdef CASE_VECTOR_SHORTEN_MODE
1285 if (optimize
1286 && JUMP_TABLE_DATA_P (insn)
1287 && GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
1289 rtx body = PATTERN (insn);
1290 int old_length = insn_lengths[uid];
1291 rtx_insn *rel_lab =
1292 safe_as_a <rtx_insn *> (XEXP (XEXP (body, 0), 0));
1293 rtx min_lab = XEXP (XEXP (body, 2), 0);
1294 rtx max_lab = XEXP (XEXP (body, 3), 0);
1295 int rel_addr = INSN_ADDRESSES (INSN_UID (rel_lab));
1296 int min_addr = INSN_ADDRESSES (INSN_UID (min_lab));
1297 int max_addr = INSN_ADDRESSES (INSN_UID (max_lab));
1298 rtx_insn *prev;
1299 int rel_align = 0;
1300 addr_diff_vec_flags flags;
1301 machine_mode vec_mode;
1303 /* Avoid automatic aggregate initialization. */
1304 flags = ADDR_DIFF_VEC_FLAGS (body);
1306 /* Try to find a known alignment for rel_lab. */
1307 for (prev = rel_lab;
1308 prev
1309 && ! insn_lengths[INSN_UID (prev)]
1310 && ! (varying_length[INSN_UID (prev)] & 1);
1311 prev = PREV_INSN (prev))
1312 if (varying_length[INSN_UID (prev)] & 2)
1314 rel_align = LABEL_TO_ALIGNMENT (prev);
1315 break;
1318 /* See the comment on addr_diff_vec_flags in rtl.h for the
1319 meaning of the flags values. base: REL_LAB vec: INSN */
1320 /* Anything after INSN has still addresses from the last
1321 pass; adjust these so that they reflect our current
1322 estimate for this pass. */
1323 if (flags.base_after_vec)
1324 rel_addr += insn_current_address - insn_last_address;
1325 if (flags.min_after_vec)
1326 min_addr += insn_current_address - insn_last_address;
1327 if (flags.max_after_vec)
1328 max_addr += insn_current_address - insn_last_address;
1329 /* We want to know the worst case, i.e. lowest possible value
1330 for the offset of MIN_LAB. If MIN_LAB is after REL_LAB,
1331 its offset is positive, and we have to be wary of code shrink;
1332 otherwise, it is negative, and we have to be vary of code
1333 size increase. */
1334 if (flags.min_after_base)
1336 /* If INSN is between REL_LAB and MIN_LAB, the size
1337 changes we are about to make can change the alignment
1338 within the observed offset, therefore we have to break
1339 it up into two parts that are independent. */
1340 if (! flags.base_after_vec && flags.min_after_vec)
1342 min_addr -= align_fuzz (rel_lab, insn, rel_align, 0);
1343 min_addr -= align_fuzz (insn, min_lab, 0, 0);
1345 else
1346 min_addr -= align_fuzz (rel_lab, min_lab, rel_align, 0);
1348 else
1350 if (flags.base_after_vec && ! flags.min_after_vec)
1352 min_addr -= align_fuzz (min_lab, insn, 0, ~0);
1353 min_addr -= align_fuzz (insn, rel_lab, 0, ~0);
1355 else
1356 min_addr -= align_fuzz (min_lab, rel_lab, 0, ~0);
1358 /* Likewise, determine the highest lowest possible value
1359 for the offset of MAX_LAB. */
1360 if (flags.max_after_base)
1362 if (! flags.base_after_vec && flags.max_after_vec)
1364 max_addr += align_fuzz (rel_lab, insn, rel_align, ~0);
1365 max_addr += align_fuzz (insn, max_lab, 0, ~0);
1367 else
1368 max_addr += align_fuzz (rel_lab, max_lab, rel_align, ~0);
1370 else
1372 if (flags.base_after_vec && ! flags.max_after_vec)
1374 max_addr += align_fuzz (max_lab, insn, 0, 0);
1375 max_addr += align_fuzz (insn, rel_lab, 0, 0);
1377 else
1378 max_addr += align_fuzz (max_lab, rel_lab, 0, 0);
1380 vec_mode = CASE_VECTOR_SHORTEN_MODE (min_addr - rel_addr,
1381 max_addr - rel_addr, body);
1382 if (!increasing
1383 || (GET_MODE_SIZE (vec_mode)
1384 >= GET_MODE_SIZE (GET_MODE (body))))
1385 PUT_MODE (body, vec_mode);
1386 if (JUMP_TABLES_IN_TEXT_SECTION
1387 || readonly_data_section == text_section)
1389 insn_lengths[uid]
1390 = (XVECLEN (body, 1) * GET_MODE_SIZE (GET_MODE (body)));
1391 insn_current_address += insn_lengths[uid];
1392 if (insn_lengths[uid] != old_length)
1393 something_changed = 1;
1396 continue;
1398 #endif /* CASE_VECTOR_SHORTEN_MODE */
1400 if (! (varying_length[uid]))
1402 if (NONJUMP_INSN_P (insn)
1403 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1405 int i;
1407 body = PATTERN (insn);
1408 for (i = 0; i < XVECLEN (body, 0); i++)
1410 rtx inner_insn = XVECEXP (body, 0, i);
1411 int inner_uid = INSN_UID (inner_insn);
1413 INSN_ADDRESSES (inner_uid) = insn_current_address;
1415 insn_current_address += insn_lengths[inner_uid];
1418 else
1419 insn_current_address += insn_lengths[uid];
1421 continue;
1424 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
1426 rtx_sequence *seqn = as_a <rtx_sequence *> (PATTERN (insn));
1427 int i;
1429 body = PATTERN (insn);
1430 new_length = 0;
1431 for (i = 0; i < seqn->len (); i++)
1433 rtx_insn *inner_insn = seqn->insn (i);
1434 int inner_uid = INSN_UID (inner_insn);
1435 int inner_length;
1437 INSN_ADDRESSES (inner_uid) = insn_current_address;
1439 /* insn_current_length returns 0 for insns with a
1440 non-varying length. */
1441 if (! varying_length[inner_uid])
1442 inner_length = insn_lengths[inner_uid];
1443 else
1444 inner_length = insn_current_length (inner_insn);
1446 if (inner_length != insn_lengths[inner_uid])
1448 if (!increasing || inner_length > insn_lengths[inner_uid])
1450 insn_lengths[inner_uid] = inner_length;
1451 something_changed = 1;
1453 else
1454 inner_length = insn_lengths[inner_uid];
1456 insn_current_address += inner_length;
1457 new_length += inner_length;
1460 else
1462 new_length = insn_current_length (insn);
1463 insn_current_address += new_length;
1466 #ifdef ADJUST_INSN_LENGTH
1467 /* If needed, do any adjustment. */
1468 tmp_length = new_length;
1469 ADJUST_INSN_LENGTH (insn, new_length);
1470 insn_current_address += (new_length - tmp_length);
1471 #endif
1473 if (new_length != insn_lengths[uid]
1474 && (!increasing || new_length > insn_lengths[uid]))
1476 insn_lengths[uid] = new_length;
1477 something_changed = 1;
1479 else
1480 insn_current_address += insn_lengths[uid] - new_length;
1482 /* For a non-optimizing compile, do only a single pass. */
1483 if (!increasing)
1484 break;
1487 free (varying_length);
1490 /* Given the body of an INSN known to be generated by an ASM statement, return
1491 the number of machine instructions likely to be generated for this insn.
1492 This is used to compute its length. */
1494 static int
1495 asm_insn_count (rtx body)
1497 const char *templ;
1499 if (GET_CODE (body) == ASM_INPUT)
1500 templ = XSTR (body, 0);
1501 else
1502 templ = decode_asm_operands (body, NULL, NULL, NULL, NULL, NULL);
1504 return asm_str_count (templ);
1507 /* Return the number of machine instructions likely to be generated for the
1508 inline-asm template. */
1510 asm_str_count (const char *templ)
1512 int count = 1;
1514 if (!*templ)
1515 return 0;
1517 for (; *templ; templ++)
1518 if (IS_ASM_LOGICAL_LINE_SEPARATOR (*templ, templ)
1519 || *templ == '\n')
1520 count++;
1522 return count;
1525 /* ??? This is probably the wrong place for these. */
1526 /* Structure recording the mapping from source file and directory
1527 names at compile time to those to be embedded in debug
1528 information. */
1529 typedef struct debug_prefix_map
1531 const char *old_prefix;
1532 const char *new_prefix;
1533 size_t old_len;
1534 size_t new_len;
1535 struct debug_prefix_map *next;
1536 } debug_prefix_map;
1538 /* Linked list of such structures. */
1539 static debug_prefix_map *debug_prefix_maps;
1542 /* Record a debug file prefix mapping. ARG is the argument to
1543 -fdebug-prefix-map and must be of the form OLD=NEW. */
1545 void
1546 add_debug_prefix_map (const char *arg)
1548 debug_prefix_map *map;
1549 const char *p;
1551 p = strchr (arg, '=');
1552 if (!p)
1554 error ("invalid argument %qs to -fdebug-prefix-map", arg);
1555 return;
1557 map = XNEW (debug_prefix_map);
1558 map->old_prefix = xstrndup (arg, p - arg);
1559 map->old_len = p - arg;
1560 p++;
1561 map->new_prefix = xstrdup (p);
1562 map->new_len = strlen (p);
1563 map->next = debug_prefix_maps;
1564 debug_prefix_maps = map;
1567 /* Perform user-specified mapping of debug filename prefixes. Return
1568 the new name corresponding to FILENAME. */
1570 const char *
1571 remap_debug_filename (const char *filename)
1573 debug_prefix_map *map;
1574 char *s;
1575 const char *name;
1576 size_t name_len;
1578 for (map = debug_prefix_maps; map; map = map->next)
1579 if (filename_ncmp (filename, map->old_prefix, map->old_len) == 0)
1580 break;
1581 if (!map)
1582 return filename;
1583 name = filename + map->old_len;
1584 name_len = strlen (name) + 1;
1585 s = (char *) alloca (name_len + map->new_len);
1586 memcpy (s, map->new_prefix, map->new_len);
1587 memcpy (s + map->new_len, name, name_len);
1588 return ggc_strdup (s);
1591 /* Return true if DWARF2 debug info can be emitted for DECL. */
1593 static bool
1594 dwarf2_debug_info_emitted_p (tree decl)
1596 if (write_symbols != DWARF2_DEBUG && write_symbols != VMS_AND_DWARF2_DEBUG)
1597 return false;
1599 if (DECL_IGNORED_P (decl))
1600 return false;
1602 return true;
1605 /* Return scope resulting from combination of S1 and S2. */
1606 static tree
1607 choose_inner_scope (tree s1, tree s2)
1609 if (!s1)
1610 return s2;
1611 if (!s2)
1612 return s1;
1613 if (BLOCK_NUMBER (s1) > BLOCK_NUMBER (s2))
1614 return s1;
1615 return s2;
1618 /* Emit lexical block notes needed to change scope from S1 to S2. */
1620 static void
1621 change_scope (rtx_insn *orig_insn, tree s1, tree s2)
1623 rtx_insn *insn = orig_insn;
1624 tree com = NULL_TREE;
1625 tree ts1 = s1, ts2 = s2;
1626 tree s;
1628 while (ts1 != ts2)
1630 gcc_assert (ts1 && ts2);
1631 if (BLOCK_NUMBER (ts1) > BLOCK_NUMBER (ts2))
1632 ts1 = BLOCK_SUPERCONTEXT (ts1);
1633 else if (BLOCK_NUMBER (ts1) < BLOCK_NUMBER (ts2))
1634 ts2 = BLOCK_SUPERCONTEXT (ts2);
1635 else
1637 ts1 = BLOCK_SUPERCONTEXT (ts1);
1638 ts2 = BLOCK_SUPERCONTEXT (ts2);
1641 com = ts1;
1643 /* Close scopes. */
1644 s = s1;
1645 while (s != com)
1647 rtx_note *note = emit_note_before (NOTE_INSN_BLOCK_END, insn);
1648 NOTE_BLOCK (note) = s;
1649 s = BLOCK_SUPERCONTEXT (s);
1652 /* Open scopes. */
1653 s = s2;
1654 while (s != com)
1656 insn = emit_note_before (NOTE_INSN_BLOCK_BEG, insn);
1657 NOTE_BLOCK (insn) = s;
1658 s = BLOCK_SUPERCONTEXT (s);
1662 /* Rebuild all the NOTE_INSN_BLOCK_BEG and NOTE_INSN_BLOCK_END notes based
1663 on the scope tree and the newly reordered instructions. */
1665 static void
1666 reemit_insn_block_notes (void)
1668 tree cur_block = DECL_INITIAL (cfun->decl);
1669 rtx_insn *insn;
1670 rtx_note *note;
1672 insn = get_insns ();
1673 for (; insn; insn = NEXT_INSN (insn))
1675 tree this_block;
1677 /* Prevent lexical blocks from straddling section boundaries. */
1678 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
1680 for (tree s = cur_block; s != DECL_INITIAL (cfun->decl);
1681 s = BLOCK_SUPERCONTEXT (s))
1683 rtx_note *note = emit_note_before (NOTE_INSN_BLOCK_END, insn);
1684 NOTE_BLOCK (note) = s;
1685 note = emit_note_after (NOTE_INSN_BLOCK_BEG, insn);
1686 NOTE_BLOCK (note) = s;
1690 if (!active_insn_p (insn))
1691 continue;
1693 /* Avoid putting scope notes between jump table and its label. */
1694 if (JUMP_TABLE_DATA_P (insn))
1695 continue;
1697 this_block = insn_scope (insn);
1698 /* For sequences compute scope resulting from merging all scopes
1699 of instructions nested inside. */
1700 if (rtx_sequence *body = dyn_cast <rtx_sequence *> (PATTERN (insn)))
1702 int i;
1704 this_block = NULL;
1705 for (i = 0; i < body->len (); i++)
1706 this_block = choose_inner_scope (this_block,
1707 insn_scope (body->insn (i)));
1709 if (! this_block)
1711 if (INSN_LOCATION (insn) == UNKNOWN_LOCATION)
1712 continue;
1713 else
1714 this_block = DECL_INITIAL (cfun->decl);
1717 if (this_block != cur_block)
1719 change_scope (insn, cur_block, this_block);
1720 cur_block = this_block;
1724 /* change_scope emits before the insn, not after. */
1725 note = emit_note (NOTE_INSN_DELETED);
1726 change_scope (note, cur_block, DECL_INITIAL (cfun->decl));
1727 delete_insn (note);
1729 reorder_blocks ();
1732 static const char *some_local_dynamic_name;
1734 /* Locate some local-dynamic symbol still in use by this function
1735 so that we can print its name in local-dynamic base patterns.
1736 Return null if there are no local-dynamic references. */
1738 const char *
1739 get_some_local_dynamic_name ()
1741 subrtx_iterator::array_type array;
1742 rtx_insn *insn;
1744 if (some_local_dynamic_name)
1745 return some_local_dynamic_name;
1747 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1748 if (NONDEBUG_INSN_P (insn))
1749 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
1751 const_rtx x = *iter;
1752 if (GET_CODE (x) == SYMBOL_REF)
1754 if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
1755 return some_local_dynamic_name = XSTR (x, 0);
1756 if (CONSTANT_POOL_ADDRESS_P (x))
1757 iter.substitute (get_pool_constant (x));
1761 return 0;
1764 /* Output assembler code for the start of a function,
1765 and initialize some of the variables in this file
1766 for the new function. The label for the function and associated
1767 assembler pseudo-ops have already been output in `assemble_start_function'.
1769 FIRST is the first insn of the rtl for the function being compiled.
1770 FILE is the file to write assembler code to.
1771 OPTIMIZE_P is nonzero if we should eliminate redundant
1772 test and compare insns. */
1774 void
1775 final_start_function (rtx_insn *first, FILE *file,
1776 int optimize_p ATTRIBUTE_UNUSED)
1778 block_depth = 0;
1780 this_is_asm_operands = 0;
1782 need_profile_function = false;
1784 last_filename = LOCATION_FILE (prologue_location);
1785 last_linenum = LOCATION_LINE (prologue_location);
1786 last_discriminator = discriminator = 0;
1788 high_block_linenum = high_function_linenum = last_linenum;
1790 if (flag_sanitize & SANITIZE_ADDRESS)
1791 asan_function_start ();
1793 if (!DECL_IGNORED_P (current_function_decl))
1794 debug_hooks->begin_prologue (last_linenum, last_filename);
1796 if (!dwarf2_debug_info_emitted_p (current_function_decl))
1797 dwarf2out_begin_prologue (0, NULL);
1799 #ifdef LEAF_REG_REMAP
1800 if (crtl->uses_only_leaf_regs)
1801 leaf_renumber_regs (first);
1802 #endif
1804 /* The Sun386i and perhaps other machines don't work right
1805 if the profiling code comes after the prologue. */
1806 if (targetm.profile_before_prologue () && crtl->profile)
1808 if (targetm.asm_out.function_prologue
1809 == default_function_pro_epilogue
1810 #ifdef HAVE_prologue
1811 && HAVE_prologue
1812 #endif
1815 rtx_insn *insn;
1816 for (insn = first; insn; insn = NEXT_INSN (insn))
1817 if (!NOTE_P (insn))
1819 insn = NULL;
1820 break;
1822 else if (NOTE_KIND (insn) == NOTE_INSN_BASIC_BLOCK
1823 || NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
1824 break;
1825 else if (NOTE_KIND (insn) == NOTE_INSN_DELETED
1826 || NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION)
1827 continue;
1828 else
1830 insn = NULL;
1831 break;
1834 if (insn)
1835 need_profile_function = true;
1836 else
1837 profile_function (file);
1839 else
1840 profile_function (file);
1843 /* If debugging, assign block numbers to all of the blocks in this
1844 function. */
1845 if (write_symbols)
1847 reemit_insn_block_notes ();
1848 number_blocks (current_function_decl);
1849 /* We never actually put out begin/end notes for the top-level
1850 block in the function. But, conceptually, that block is
1851 always needed. */
1852 TREE_ASM_WRITTEN (DECL_INITIAL (current_function_decl)) = 1;
1855 if (warn_frame_larger_than
1856 && get_frame_size () > frame_larger_than_size)
1858 /* Issue a warning */
1859 warning (OPT_Wframe_larger_than_,
1860 "the frame size of %wd bytes is larger than %wd bytes",
1861 get_frame_size (), frame_larger_than_size);
1864 /* First output the function prologue: code to set up the stack frame. */
1865 targetm.asm_out.function_prologue (file, get_frame_size ());
1867 /* If the machine represents the prologue as RTL, the profiling code must
1868 be emitted when NOTE_INSN_PROLOGUE_END is scanned. */
1869 #ifdef HAVE_prologue
1870 if (! HAVE_prologue)
1871 #endif
1872 profile_after_prologue (file);
1875 static void
1876 profile_after_prologue (FILE *file ATTRIBUTE_UNUSED)
1878 if (!targetm.profile_before_prologue () && crtl->profile)
1879 profile_function (file);
1882 static void
1883 profile_function (FILE *file ATTRIBUTE_UNUSED)
1885 #ifndef NO_PROFILE_COUNTERS
1886 # define NO_PROFILE_COUNTERS 0
1887 #endif
1888 #ifdef ASM_OUTPUT_REG_PUSH
1889 rtx sval = NULL, chain = NULL;
1891 if (cfun->returns_struct)
1892 sval = targetm.calls.struct_value_rtx (TREE_TYPE (current_function_decl),
1893 true);
1894 if (cfun->static_chain_decl)
1895 chain = targetm.calls.static_chain (current_function_decl, true);
1896 #endif /* ASM_OUTPUT_REG_PUSH */
1898 if (! NO_PROFILE_COUNTERS)
1900 int align = MIN (BIGGEST_ALIGNMENT, LONG_TYPE_SIZE);
1901 switch_to_section (data_section);
1902 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
1903 targetm.asm_out.internal_label (file, "LP", current_function_funcdef_no);
1904 assemble_integer (const0_rtx, LONG_TYPE_SIZE / BITS_PER_UNIT, align, 1);
1907 switch_to_section (current_function_section ());
1909 #ifdef ASM_OUTPUT_REG_PUSH
1910 if (sval && REG_P (sval))
1911 ASM_OUTPUT_REG_PUSH (file, REGNO (sval));
1912 if (chain && REG_P (chain))
1913 ASM_OUTPUT_REG_PUSH (file, REGNO (chain));
1914 #endif
1916 FUNCTION_PROFILER (file, current_function_funcdef_no);
1918 #ifdef ASM_OUTPUT_REG_PUSH
1919 if (chain && REG_P (chain))
1920 ASM_OUTPUT_REG_POP (file, REGNO (chain));
1921 if (sval && REG_P (sval))
1922 ASM_OUTPUT_REG_POP (file, REGNO (sval));
1923 #endif
1926 /* Output assembler code for the end of a function.
1927 For clarity, args are same as those of `final_start_function'
1928 even though not all of them are needed. */
1930 void
1931 final_end_function (void)
1933 app_disable ();
1935 if (!DECL_IGNORED_P (current_function_decl))
1936 debug_hooks->end_function (high_function_linenum);
1938 /* Finally, output the function epilogue:
1939 code to restore the stack frame and return to the caller. */
1940 targetm.asm_out.function_epilogue (asm_out_file, get_frame_size ());
1942 /* And debug output. */
1943 if (!DECL_IGNORED_P (current_function_decl))
1944 debug_hooks->end_epilogue (last_linenum, last_filename);
1946 if (!dwarf2_debug_info_emitted_p (current_function_decl)
1947 && dwarf2out_do_frame ())
1948 dwarf2out_end_epilogue (last_linenum, last_filename);
1950 some_local_dynamic_name = 0;
1954 /* Dumper helper for basic block information. FILE is the assembly
1955 output file, and INSN is the instruction being emitted. */
1957 static void
1958 dump_basic_block_info (FILE *file, rtx_insn *insn, basic_block *start_to_bb,
1959 basic_block *end_to_bb, int bb_map_size, int *bb_seqn)
1961 basic_block bb;
1963 if (!flag_debug_asm)
1964 return;
1966 if (INSN_UID (insn) < bb_map_size
1967 && (bb = start_to_bb[INSN_UID (insn)]) != NULL)
1969 edge e;
1970 edge_iterator ei;
1972 fprintf (file, "%s BLOCK %d", ASM_COMMENT_START, bb->index);
1973 if (bb->frequency)
1974 fprintf (file, " freq:%d", bb->frequency);
1975 if (bb->count)
1976 fprintf (file, " count:%" PRId64,
1977 bb->count);
1978 fprintf (file, " seq:%d", (*bb_seqn)++);
1979 fprintf (file, "\n%s PRED:", ASM_COMMENT_START);
1980 FOR_EACH_EDGE (e, ei, bb->preds)
1982 dump_edge_info (file, e, TDF_DETAILS, 0);
1984 fprintf (file, "\n");
1986 if (INSN_UID (insn) < bb_map_size
1987 && (bb = end_to_bb[INSN_UID (insn)]) != NULL)
1989 edge e;
1990 edge_iterator ei;
1992 fprintf (asm_out_file, "%s SUCC:", ASM_COMMENT_START);
1993 FOR_EACH_EDGE (e, ei, bb->succs)
1995 dump_edge_info (asm_out_file, e, TDF_DETAILS, 1);
1997 fprintf (file, "\n");
2001 /* Output assembler code for some insns: all or part of a function.
2002 For description of args, see `final_start_function', above. */
2004 void
2005 final (rtx_insn *first, FILE *file, int optimize_p)
2007 rtx_insn *insn, *next;
2008 int seen = 0;
2010 /* Used for -dA dump. */
2011 basic_block *start_to_bb = NULL;
2012 basic_block *end_to_bb = NULL;
2013 int bb_map_size = 0;
2014 int bb_seqn = 0;
2016 last_ignored_compare = 0;
2018 if (HAVE_cc0)
2019 for (insn = first; insn; insn = NEXT_INSN (insn))
2021 /* If CC tracking across branches is enabled, record the insn which
2022 jumps to each branch only reached from one place. */
2023 if (optimize_p && JUMP_P (insn))
2025 rtx lab = JUMP_LABEL (insn);
2026 if (lab && LABEL_P (lab) && LABEL_NUSES (lab) == 1)
2028 LABEL_REFS (lab) = insn;
2033 init_recog ();
2035 CC_STATUS_INIT;
2037 if (flag_debug_asm)
2039 basic_block bb;
2041 bb_map_size = get_max_uid () + 1;
2042 start_to_bb = XCNEWVEC (basic_block, bb_map_size);
2043 end_to_bb = XCNEWVEC (basic_block, bb_map_size);
2045 /* There is no cfg for a thunk. */
2046 if (!cfun->is_thunk)
2047 FOR_EACH_BB_REVERSE_FN (bb, cfun)
2049 start_to_bb[INSN_UID (BB_HEAD (bb))] = bb;
2050 end_to_bb[INSN_UID (BB_END (bb))] = bb;
2054 /* Output the insns. */
2055 for (insn = first; insn;)
2057 if (HAVE_ATTR_length)
2059 if ((unsigned) INSN_UID (insn) >= INSN_ADDRESSES_SIZE ())
2061 /* This can be triggered by bugs elsewhere in the compiler if
2062 new insns are created after init_insn_lengths is called. */
2063 gcc_assert (NOTE_P (insn));
2064 insn_current_address = -1;
2066 else
2067 insn_current_address = INSN_ADDRESSES (INSN_UID (insn));
2070 dump_basic_block_info (file, insn, start_to_bb, end_to_bb,
2071 bb_map_size, &bb_seqn);
2072 insn = final_scan_insn (insn, file, optimize_p, 0, &seen);
2075 if (flag_debug_asm)
2077 free (start_to_bb);
2078 free (end_to_bb);
2081 /* Remove CFI notes, to avoid compare-debug failures. */
2082 for (insn = first; insn; insn = next)
2084 next = NEXT_INSN (insn);
2085 if (NOTE_P (insn)
2086 && (NOTE_KIND (insn) == NOTE_INSN_CFI
2087 || NOTE_KIND (insn) == NOTE_INSN_CFI_LABEL))
2088 delete_insn (insn);
2092 const char *
2093 get_insn_template (int code, rtx insn)
2095 switch (insn_data[code].output_format)
2097 case INSN_OUTPUT_FORMAT_SINGLE:
2098 return insn_data[code].output.single;
2099 case INSN_OUTPUT_FORMAT_MULTI:
2100 return insn_data[code].output.multi[which_alternative];
2101 case INSN_OUTPUT_FORMAT_FUNCTION:
2102 gcc_assert (insn);
2103 return (*insn_data[code].output.function) (recog_data.operand,
2104 as_a <rtx_insn *> (insn));
2106 default:
2107 gcc_unreachable ();
2111 /* Emit the appropriate declaration for an alternate-entry-point
2112 symbol represented by INSN, to FILE. INSN is a CODE_LABEL with
2113 LABEL_KIND != LABEL_NORMAL.
2115 The case fall-through in this function is intentional. */
2116 static void
2117 output_alternate_entry_point (FILE *file, rtx_insn *insn)
2119 const char *name = LABEL_NAME (insn);
2121 switch (LABEL_KIND (insn))
2123 case LABEL_WEAK_ENTRY:
2124 #ifdef ASM_WEAKEN_LABEL
2125 ASM_WEAKEN_LABEL (file, name);
2126 #endif
2127 case LABEL_GLOBAL_ENTRY:
2128 targetm.asm_out.globalize_label (file, name);
2129 case LABEL_STATIC_ENTRY:
2130 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
2131 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
2132 #endif
2133 ASM_OUTPUT_LABEL (file, name);
2134 break;
2136 case LABEL_NORMAL:
2137 default:
2138 gcc_unreachable ();
2142 /* Given a CALL_INSN, find and return the nested CALL. */
2143 static rtx
2144 call_from_call_insn (rtx_call_insn *insn)
2146 rtx x;
2147 gcc_assert (CALL_P (insn));
2148 x = PATTERN (insn);
2150 while (GET_CODE (x) != CALL)
2152 switch (GET_CODE (x))
2154 default:
2155 gcc_unreachable ();
2156 case COND_EXEC:
2157 x = COND_EXEC_CODE (x);
2158 break;
2159 case PARALLEL:
2160 x = XVECEXP (x, 0, 0);
2161 break;
2162 case SET:
2163 x = XEXP (x, 1);
2164 break;
2167 return x;
2170 /* The final scan for one insn, INSN.
2171 Args are same as in `final', except that INSN
2172 is the insn being scanned.
2173 Value returned is the next insn to be scanned.
2175 NOPEEPHOLES is the flag to disallow peephole processing (currently
2176 used for within delayed branch sequence output).
2178 SEEN is used to track the end of the prologue, for emitting
2179 debug information. We force the emission of a line note after
2180 both NOTE_INSN_PROLOGUE_END and NOTE_INSN_FUNCTION_BEG. */
2182 rtx_insn *
2183 final_scan_insn (rtx_insn *insn, FILE *file, int optimize_p ATTRIBUTE_UNUSED,
2184 int nopeepholes ATTRIBUTE_UNUSED, int *seen)
2186 #if HAVE_cc0
2187 rtx set;
2188 #endif
2189 rtx_insn *next;
2191 insn_counter++;
2193 /* Ignore deleted insns. These can occur when we split insns (due to a
2194 template of "#") while not optimizing. */
2195 if (insn->deleted ())
2196 return NEXT_INSN (insn);
2198 switch (GET_CODE (insn))
2200 case NOTE:
2201 switch (NOTE_KIND (insn))
2203 case NOTE_INSN_DELETED:
2204 case NOTE_INSN_UPDATE_SJLJ_CONTEXT:
2205 break;
2207 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
2208 in_cold_section_p = !in_cold_section_p;
2210 if (dwarf2out_do_frame ())
2211 dwarf2out_switch_text_section ();
2212 else if (!DECL_IGNORED_P (current_function_decl))
2213 debug_hooks->switch_text_section ();
2215 switch_to_section (current_function_section ());
2216 targetm.asm_out.function_switched_text_sections (asm_out_file,
2217 current_function_decl,
2218 in_cold_section_p);
2219 /* Emit a label for the split cold section. Form label name by
2220 suffixing "cold" to the original function's name. */
2221 if (in_cold_section_p)
2223 cold_function_name
2224 = clone_function_name (current_function_decl, "cold");
2225 #ifdef ASM_DECLARE_COLD_FUNCTION_NAME
2226 ASM_DECLARE_COLD_FUNCTION_NAME (asm_out_file,
2227 IDENTIFIER_POINTER
2228 (cold_function_name),
2229 current_function_decl);
2230 #else
2231 ASM_OUTPUT_LABEL (asm_out_file,
2232 IDENTIFIER_POINTER (cold_function_name));
2233 #endif
2235 break;
2237 case NOTE_INSN_BASIC_BLOCK:
2238 if (need_profile_function)
2240 profile_function (asm_out_file);
2241 need_profile_function = false;
2244 if (targetm.asm_out.unwind_emit)
2245 targetm.asm_out.unwind_emit (asm_out_file, insn);
2247 discriminator = NOTE_BASIC_BLOCK (insn)->discriminator;
2249 break;
2251 case NOTE_INSN_EH_REGION_BEG:
2252 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LEHB",
2253 NOTE_EH_HANDLER (insn));
2254 break;
2256 case NOTE_INSN_EH_REGION_END:
2257 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LEHE",
2258 NOTE_EH_HANDLER (insn));
2259 break;
2261 case NOTE_INSN_PROLOGUE_END:
2262 targetm.asm_out.function_end_prologue (file);
2263 profile_after_prologue (file);
2265 if ((*seen & (SEEN_EMITTED | SEEN_NOTE)) == SEEN_NOTE)
2267 *seen |= SEEN_EMITTED;
2268 force_source_line = true;
2270 else
2271 *seen |= SEEN_NOTE;
2273 break;
2275 case NOTE_INSN_EPILOGUE_BEG:
2276 if (!DECL_IGNORED_P (current_function_decl))
2277 (*debug_hooks->begin_epilogue) (last_linenum, last_filename);
2278 targetm.asm_out.function_begin_epilogue (file);
2279 break;
2281 case NOTE_INSN_CFI:
2282 dwarf2out_emit_cfi (NOTE_CFI (insn));
2283 break;
2285 case NOTE_INSN_CFI_LABEL:
2286 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI",
2287 NOTE_LABEL_NUMBER (insn));
2288 break;
2290 case NOTE_INSN_FUNCTION_BEG:
2291 if (need_profile_function)
2293 profile_function (asm_out_file);
2294 need_profile_function = false;
2297 app_disable ();
2298 if (!DECL_IGNORED_P (current_function_decl))
2299 debug_hooks->end_prologue (last_linenum, last_filename);
2301 if ((*seen & (SEEN_EMITTED | SEEN_NOTE)) == SEEN_NOTE)
2303 *seen |= SEEN_EMITTED;
2304 force_source_line = true;
2306 else
2307 *seen |= SEEN_NOTE;
2309 break;
2311 case NOTE_INSN_BLOCK_BEG:
2312 if (debug_info_level == DINFO_LEVEL_NORMAL
2313 || debug_info_level == DINFO_LEVEL_VERBOSE
2314 || write_symbols == DWARF2_DEBUG
2315 || write_symbols == VMS_AND_DWARF2_DEBUG
2316 || write_symbols == VMS_DEBUG)
2318 int n = BLOCK_NUMBER (NOTE_BLOCK (insn));
2320 app_disable ();
2321 ++block_depth;
2322 high_block_linenum = last_linenum;
2324 /* Output debugging info about the symbol-block beginning. */
2325 if (!DECL_IGNORED_P (current_function_decl))
2326 debug_hooks->begin_block (last_linenum, n);
2328 /* Mark this block as output. */
2329 TREE_ASM_WRITTEN (NOTE_BLOCK (insn)) = 1;
2331 if (write_symbols == DBX_DEBUG
2332 || write_symbols == SDB_DEBUG)
2334 location_t *locus_ptr
2335 = block_nonartificial_location (NOTE_BLOCK (insn));
2337 if (locus_ptr != NULL)
2339 override_filename = LOCATION_FILE (*locus_ptr);
2340 override_linenum = LOCATION_LINE (*locus_ptr);
2343 break;
2345 case NOTE_INSN_BLOCK_END:
2346 if (debug_info_level == DINFO_LEVEL_NORMAL
2347 || debug_info_level == DINFO_LEVEL_VERBOSE
2348 || write_symbols == DWARF2_DEBUG
2349 || write_symbols == VMS_AND_DWARF2_DEBUG
2350 || write_symbols == VMS_DEBUG)
2352 int n = BLOCK_NUMBER (NOTE_BLOCK (insn));
2354 app_disable ();
2356 /* End of a symbol-block. */
2357 --block_depth;
2358 gcc_assert (block_depth >= 0);
2360 if (!DECL_IGNORED_P (current_function_decl))
2361 debug_hooks->end_block (high_block_linenum, n);
2363 if (write_symbols == DBX_DEBUG
2364 || write_symbols == SDB_DEBUG)
2366 tree outer_block = BLOCK_SUPERCONTEXT (NOTE_BLOCK (insn));
2367 location_t *locus_ptr
2368 = block_nonartificial_location (outer_block);
2370 if (locus_ptr != NULL)
2372 override_filename = LOCATION_FILE (*locus_ptr);
2373 override_linenum = LOCATION_LINE (*locus_ptr);
2375 else
2377 override_filename = NULL;
2378 override_linenum = 0;
2381 break;
2383 case NOTE_INSN_DELETED_LABEL:
2384 /* Emit the label. We may have deleted the CODE_LABEL because
2385 the label could be proved to be unreachable, though still
2386 referenced (in the form of having its address taken. */
2387 ASM_OUTPUT_DEBUG_LABEL (file, "L", CODE_LABEL_NUMBER (insn));
2388 break;
2390 case NOTE_INSN_DELETED_DEBUG_LABEL:
2391 /* Similarly, but need to use different namespace for it. */
2392 if (CODE_LABEL_NUMBER (insn) != -1)
2393 ASM_OUTPUT_DEBUG_LABEL (file, "LDL", CODE_LABEL_NUMBER (insn));
2394 break;
2396 case NOTE_INSN_VAR_LOCATION:
2397 case NOTE_INSN_CALL_ARG_LOCATION:
2398 if (!DECL_IGNORED_P (current_function_decl))
2399 debug_hooks->var_location (insn);
2400 break;
2402 default:
2403 gcc_unreachable ();
2404 break;
2406 break;
2408 case BARRIER:
2409 break;
2411 case CODE_LABEL:
2412 /* The target port might emit labels in the output function for
2413 some insn, e.g. sh.c output_branchy_insn. */
2414 if (CODE_LABEL_NUMBER (insn) <= max_labelno)
2416 int align = LABEL_TO_ALIGNMENT (insn);
2417 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
2418 int max_skip = LABEL_TO_MAX_SKIP (insn);
2419 #endif
2421 if (align && NEXT_INSN (insn))
2423 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
2424 ASM_OUTPUT_MAX_SKIP_ALIGN (file, align, max_skip);
2425 #else
2426 #ifdef ASM_OUTPUT_ALIGN_WITH_NOP
2427 ASM_OUTPUT_ALIGN_WITH_NOP (file, align);
2428 #else
2429 ASM_OUTPUT_ALIGN (file, align);
2430 #endif
2431 #endif
2434 CC_STATUS_INIT;
2436 if (!DECL_IGNORED_P (current_function_decl) && LABEL_NAME (insn))
2437 debug_hooks->label (as_a <rtx_code_label *> (insn));
2439 app_disable ();
2441 next = next_nonnote_insn (insn);
2442 /* If this label is followed by a jump-table, make sure we put
2443 the label in the read-only section. Also possibly write the
2444 label and jump table together. */
2445 if (next != 0 && JUMP_TABLE_DATA_P (next))
2447 #if defined(ASM_OUTPUT_ADDR_VEC) || defined(ASM_OUTPUT_ADDR_DIFF_VEC)
2448 /* In this case, the case vector is being moved by the
2449 target, so don't output the label at all. Leave that
2450 to the back end macros. */
2451 #else
2452 if (! JUMP_TABLES_IN_TEXT_SECTION)
2454 int log_align;
2456 switch_to_section (targetm.asm_out.function_rodata_section
2457 (current_function_decl));
2459 #ifdef ADDR_VEC_ALIGN
2460 log_align = ADDR_VEC_ALIGN (next);
2461 #else
2462 log_align = exact_log2 (BIGGEST_ALIGNMENT / BITS_PER_UNIT);
2463 #endif
2464 ASM_OUTPUT_ALIGN (file, log_align);
2466 else
2467 switch_to_section (current_function_section ());
2469 #ifdef ASM_OUTPUT_CASE_LABEL
2470 ASM_OUTPUT_CASE_LABEL (file, "L", CODE_LABEL_NUMBER (insn),
2471 next);
2472 #else
2473 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (insn));
2474 #endif
2475 #endif
2476 break;
2478 if (LABEL_ALT_ENTRY_P (insn))
2479 output_alternate_entry_point (file, insn);
2480 else
2481 targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (insn));
2482 break;
2484 default:
2486 rtx body = PATTERN (insn);
2487 int insn_code_number;
2488 const char *templ;
2489 bool is_stmt;
2491 /* Reset this early so it is correct for ASM statements. */
2492 current_insn_predicate = NULL_RTX;
2494 /* An INSN, JUMP_INSN or CALL_INSN.
2495 First check for special kinds that recog doesn't recognize. */
2497 if (GET_CODE (body) == USE /* These are just declarations. */
2498 || GET_CODE (body) == CLOBBER)
2499 break;
2501 #if HAVE_cc0
2503 /* If there is a REG_CC_SETTER note on this insn, it means that
2504 the setting of the condition code was done in the delay slot
2505 of the insn that branched here. So recover the cc status
2506 from the insn that set it. */
2508 rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
2509 if (note)
2511 rtx_insn *other = as_a <rtx_insn *> (XEXP (note, 0));
2512 NOTICE_UPDATE_CC (PATTERN (other), other);
2513 cc_prev_status = cc_status;
2516 #endif
2518 /* Detect insns that are really jump-tables
2519 and output them as such. */
2521 if (JUMP_TABLE_DATA_P (insn))
2523 #if !(defined(ASM_OUTPUT_ADDR_VEC) || defined(ASM_OUTPUT_ADDR_DIFF_VEC))
2524 int vlen, idx;
2525 #endif
2527 if (! JUMP_TABLES_IN_TEXT_SECTION)
2528 switch_to_section (targetm.asm_out.function_rodata_section
2529 (current_function_decl));
2530 else
2531 switch_to_section (current_function_section ());
2533 app_disable ();
2535 #if defined(ASM_OUTPUT_ADDR_VEC) || defined(ASM_OUTPUT_ADDR_DIFF_VEC)
2536 if (GET_CODE (body) == ADDR_VEC)
2538 #ifdef ASM_OUTPUT_ADDR_VEC
2539 ASM_OUTPUT_ADDR_VEC (PREV_INSN (insn), body);
2540 #else
2541 gcc_unreachable ();
2542 #endif
2544 else
2546 #ifdef ASM_OUTPUT_ADDR_DIFF_VEC
2547 ASM_OUTPUT_ADDR_DIFF_VEC (PREV_INSN (insn), body);
2548 #else
2549 gcc_unreachable ();
2550 #endif
2552 #else
2553 vlen = XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC);
2554 for (idx = 0; idx < vlen; idx++)
2556 if (GET_CODE (body) == ADDR_VEC)
2558 #ifdef ASM_OUTPUT_ADDR_VEC_ELT
2559 ASM_OUTPUT_ADDR_VEC_ELT
2560 (file, CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 0, idx), 0)));
2561 #else
2562 gcc_unreachable ();
2563 #endif
2565 else
2567 #ifdef ASM_OUTPUT_ADDR_DIFF_ELT
2568 ASM_OUTPUT_ADDR_DIFF_ELT
2569 (file,
2570 body,
2571 CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 1, idx), 0)),
2572 CODE_LABEL_NUMBER (XEXP (XEXP (body, 0), 0)));
2573 #else
2574 gcc_unreachable ();
2575 #endif
2578 #ifdef ASM_OUTPUT_CASE_END
2579 ASM_OUTPUT_CASE_END (file,
2580 CODE_LABEL_NUMBER (PREV_INSN (insn)),
2581 insn);
2582 #endif
2583 #endif
2585 switch_to_section (current_function_section ());
2587 break;
2589 /* Output this line note if it is the first or the last line
2590 note in a row. */
2591 if (!DECL_IGNORED_P (current_function_decl)
2592 && notice_source_line (insn, &is_stmt))
2593 (*debug_hooks->source_line) (last_linenum, last_filename,
2594 last_discriminator, is_stmt);
2596 if (GET_CODE (body) == ASM_INPUT)
2598 const char *string = XSTR (body, 0);
2600 /* There's no telling what that did to the condition codes. */
2601 CC_STATUS_INIT;
2603 if (string[0])
2605 expanded_location loc;
2607 app_enable ();
2608 loc = expand_location (ASM_INPUT_SOURCE_LOCATION (body));
2609 if (*loc.file && loc.line)
2610 fprintf (asm_out_file, "%s %i \"%s\" 1\n",
2611 ASM_COMMENT_START, loc.line, loc.file);
2612 fprintf (asm_out_file, "\t%s\n", string);
2613 #if HAVE_AS_LINE_ZERO
2614 if (*loc.file && loc.line)
2615 fprintf (asm_out_file, "%s 0 \"\" 2\n", ASM_COMMENT_START);
2616 #endif
2618 break;
2621 /* Detect `asm' construct with operands. */
2622 if (asm_noperands (body) >= 0)
2624 unsigned int noperands = asm_noperands (body);
2625 rtx *ops = XALLOCAVEC (rtx, noperands);
2626 const char *string;
2627 location_t loc;
2628 expanded_location expanded;
2630 /* There's no telling what that did to the condition codes. */
2631 CC_STATUS_INIT;
2633 /* Get out the operand values. */
2634 string = decode_asm_operands (body, ops, NULL, NULL, NULL, &loc);
2635 /* Inhibit dying on what would otherwise be compiler bugs. */
2636 insn_noperands = noperands;
2637 this_is_asm_operands = insn;
2638 expanded = expand_location (loc);
2640 #ifdef FINAL_PRESCAN_INSN
2641 FINAL_PRESCAN_INSN (insn, ops, insn_noperands);
2642 #endif
2644 /* Output the insn using them. */
2645 if (string[0])
2647 app_enable ();
2648 if (expanded.file && expanded.line)
2649 fprintf (asm_out_file, "%s %i \"%s\" 1\n",
2650 ASM_COMMENT_START, expanded.line, expanded.file);
2651 output_asm_insn (string, ops);
2652 #if HAVE_AS_LINE_ZERO
2653 if (expanded.file && expanded.line)
2654 fprintf (asm_out_file, "%s 0 \"\" 2\n", ASM_COMMENT_START);
2655 #endif
2658 if (targetm.asm_out.final_postscan_insn)
2659 targetm.asm_out.final_postscan_insn (file, insn, ops,
2660 insn_noperands);
2662 this_is_asm_operands = 0;
2663 break;
2666 app_disable ();
2668 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (body))
2670 /* A delayed-branch sequence */
2671 int i;
2673 final_sequence = seq;
2675 /* The first insn in this SEQUENCE might be a JUMP_INSN that will
2676 force the restoration of a comparison that was previously
2677 thought unnecessary. If that happens, cancel this sequence
2678 and cause that insn to be restored. */
2680 next = final_scan_insn (seq->insn (0), file, 0, 1, seen);
2681 if (next != seq->insn (1))
2683 final_sequence = 0;
2684 return next;
2687 for (i = 1; i < seq->len (); i++)
2689 rtx_insn *insn = seq->insn (i);
2690 rtx_insn *next = NEXT_INSN (insn);
2691 /* We loop in case any instruction in a delay slot gets
2692 split. */
2694 insn = final_scan_insn (insn, file, 0, 1, seen);
2695 while (insn != next);
2697 #ifdef DBR_OUTPUT_SEQEND
2698 DBR_OUTPUT_SEQEND (file);
2699 #endif
2700 final_sequence = 0;
2702 /* If the insn requiring the delay slot was a CALL_INSN, the
2703 insns in the delay slot are actually executed before the
2704 called function. Hence we don't preserve any CC-setting
2705 actions in these insns and the CC must be marked as being
2706 clobbered by the function. */
2707 if (CALL_P (seq->insn (0)))
2709 CC_STATUS_INIT;
2711 break;
2714 /* We have a real machine instruction as rtl. */
2716 body = PATTERN (insn);
2718 #if HAVE_cc0
2719 set = single_set (insn);
2721 /* Check for redundant test and compare instructions
2722 (when the condition codes are already set up as desired).
2723 This is done only when optimizing; if not optimizing,
2724 it should be possible for the user to alter a variable
2725 with the debugger in between statements
2726 and the next statement should reexamine the variable
2727 to compute the condition codes. */
2729 if (optimize_p)
2731 if (set
2732 && GET_CODE (SET_DEST (set)) == CC0
2733 && insn != last_ignored_compare)
2735 rtx src1, src2;
2736 if (GET_CODE (SET_SRC (set)) == SUBREG)
2737 SET_SRC (set) = alter_subreg (&SET_SRC (set), true);
2739 src1 = SET_SRC (set);
2740 src2 = NULL_RTX;
2741 if (GET_CODE (SET_SRC (set)) == COMPARE)
2743 if (GET_CODE (XEXP (SET_SRC (set), 0)) == SUBREG)
2744 XEXP (SET_SRC (set), 0)
2745 = alter_subreg (&XEXP (SET_SRC (set), 0), true);
2746 if (GET_CODE (XEXP (SET_SRC (set), 1)) == SUBREG)
2747 XEXP (SET_SRC (set), 1)
2748 = alter_subreg (&XEXP (SET_SRC (set), 1), true);
2749 if (XEXP (SET_SRC (set), 1)
2750 == CONST0_RTX (GET_MODE (XEXP (SET_SRC (set), 0))))
2751 src2 = XEXP (SET_SRC (set), 0);
2753 if ((cc_status.value1 != 0
2754 && rtx_equal_p (src1, cc_status.value1))
2755 || (cc_status.value2 != 0
2756 && rtx_equal_p (src1, cc_status.value2))
2757 || (src2 != 0 && cc_status.value1 != 0
2758 && rtx_equal_p (src2, cc_status.value1))
2759 || (src2 != 0 && cc_status.value2 != 0
2760 && rtx_equal_p (src2, cc_status.value2)))
2762 /* Don't delete insn if it has an addressing side-effect. */
2763 if (! FIND_REG_INC_NOTE (insn, NULL_RTX)
2764 /* or if anything in it is volatile. */
2765 && ! volatile_refs_p (PATTERN (insn)))
2767 /* We don't really delete the insn; just ignore it. */
2768 last_ignored_compare = insn;
2769 break;
2775 /* If this is a conditional branch, maybe modify it
2776 if the cc's are in a nonstandard state
2777 so that it accomplishes the same thing that it would
2778 do straightforwardly if the cc's were set up normally. */
2780 if (cc_status.flags != 0
2781 && JUMP_P (insn)
2782 && GET_CODE (body) == SET
2783 && SET_DEST (body) == pc_rtx
2784 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE
2785 && COMPARISON_P (XEXP (SET_SRC (body), 0))
2786 && XEXP (XEXP (SET_SRC (body), 0), 0) == cc0_rtx)
2788 /* This function may alter the contents of its argument
2789 and clear some of the cc_status.flags bits.
2790 It may also return 1 meaning condition now always true
2791 or -1 meaning condition now always false
2792 or 2 meaning condition nontrivial but altered. */
2793 int result = alter_cond (XEXP (SET_SRC (body), 0));
2794 /* If condition now has fixed value, replace the IF_THEN_ELSE
2795 with its then-operand or its else-operand. */
2796 if (result == 1)
2797 SET_SRC (body) = XEXP (SET_SRC (body), 1);
2798 if (result == -1)
2799 SET_SRC (body) = XEXP (SET_SRC (body), 2);
2801 /* The jump is now either unconditional or a no-op.
2802 If it has become a no-op, don't try to output it.
2803 (It would not be recognized.) */
2804 if (SET_SRC (body) == pc_rtx)
2806 delete_insn (insn);
2807 break;
2809 else if (ANY_RETURN_P (SET_SRC (body)))
2810 /* Replace (set (pc) (return)) with (return). */
2811 PATTERN (insn) = body = SET_SRC (body);
2813 /* Rerecognize the instruction if it has changed. */
2814 if (result != 0)
2815 INSN_CODE (insn) = -1;
2818 /* If this is a conditional trap, maybe modify it if the cc's
2819 are in a nonstandard state so that it accomplishes the same
2820 thing that it would do straightforwardly if the cc's were
2821 set up normally. */
2822 if (cc_status.flags != 0
2823 && NONJUMP_INSN_P (insn)
2824 && GET_CODE (body) == TRAP_IF
2825 && COMPARISON_P (TRAP_CONDITION (body))
2826 && XEXP (TRAP_CONDITION (body), 0) == cc0_rtx)
2828 /* This function may alter the contents of its argument
2829 and clear some of the cc_status.flags bits.
2830 It may also return 1 meaning condition now always true
2831 or -1 meaning condition now always false
2832 or 2 meaning condition nontrivial but altered. */
2833 int result = alter_cond (TRAP_CONDITION (body));
2835 /* If TRAP_CONDITION has become always false, delete the
2836 instruction. */
2837 if (result == -1)
2839 delete_insn (insn);
2840 break;
2843 /* If TRAP_CONDITION has become always true, replace
2844 TRAP_CONDITION with const_true_rtx. */
2845 if (result == 1)
2846 TRAP_CONDITION (body) = const_true_rtx;
2848 /* Rerecognize the instruction if it has changed. */
2849 if (result != 0)
2850 INSN_CODE (insn) = -1;
2853 /* Make same adjustments to instructions that examine the
2854 condition codes without jumping and instructions that
2855 handle conditional moves (if this machine has either one). */
2857 if (cc_status.flags != 0
2858 && set != 0)
2860 rtx cond_rtx, then_rtx, else_rtx;
2862 if (!JUMP_P (insn)
2863 && GET_CODE (SET_SRC (set)) == IF_THEN_ELSE)
2865 cond_rtx = XEXP (SET_SRC (set), 0);
2866 then_rtx = XEXP (SET_SRC (set), 1);
2867 else_rtx = XEXP (SET_SRC (set), 2);
2869 else
2871 cond_rtx = SET_SRC (set);
2872 then_rtx = const_true_rtx;
2873 else_rtx = const0_rtx;
2876 if (COMPARISON_P (cond_rtx)
2877 && XEXP (cond_rtx, 0) == cc0_rtx)
2879 int result;
2880 result = alter_cond (cond_rtx);
2881 if (result == 1)
2882 validate_change (insn, &SET_SRC (set), then_rtx, 0);
2883 else if (result == -1)
2884 validate_change (insn, &SET_SRC (set), else_rtx, 0);
2885 else if (result == 2)
2886 INSN_CODE (insn) = -1;
2887 if (SET_DEST (set) == SET_SRC (set))
2888 delete_insn (insn);
2892 #endif
2894 /* Do machine-specific peephole optimizations if desired. */
2896 if (HAVE_peephole && optimize_p && !flag_no_peephole && !nopeepholes)
2898 rtx_insn *next = peephole (insn);
2899 /* When peepholing, if there were notes within the peephole,
2900 emit them before the peephole. */
2901 if (next != 0 && next != NEXT_INSN (insn))
2903 rtx_insn *note, *prev = PREV_INSN (insn);
2905 for (note = NEXT_INSN (insn); note != next;
2906 note = NEXT_INSN (note))
2907 final_scan_insn (note, file, optimize_p, nopeepholes, seen);
2909 /* Put the notes in the proper position for a later
2910 rescan. For example, the SH target can do this
2911 when generating a far jump in a delayed branch
2912 sequence. */
2913 note = NEXT_INSN (insn);
2914 SET_PREV_INSN (note) = prev;
2915 SET_NEXT_INSN (prev) = note;
2916 SET_NEXT_INSN (PREV_INSN (next)) = insn;
2917 SET_PREV_INSN (insn) = PREV_INSN (next);
2918 SET_NEXT_INSN (insn) = next;
2919 SET_PREV_INSN (next) = insn;
2922 /* PEEPHOLE might have changed this. */
2923 body = PATTERN (insn);
2926 /* Try to recognize the instruction.
2927 If successful, verify that the operands satisfy the
2928 constraints for the instruction. Crash if they don't,
2929 since `reload' should have changed them so that they do. */
2931 insn_code_number = recog_memoized (insn);
2932 cleanup_subreg_operands (insn);
2934 /* Dump the insn in the assembly for debugging (-dAP).
2935 If the final dump is requested as slim RTL, dump slim
2936 RTL to the assembly file also. */
2937 if (flag_dump_rtl_in_asm)
2939 print_rtx_head = ASM_COMMENT_START;
2940 if (! (dump_flags & TDF_SLIM))
2941 print_rtl_single (asm_out_file, insn);
2942 else
2943 dump_insn_slim (asm_out_file, insn);
2944 print_rtx_head = "";
2947 if (! constrain_operands_cached (insn, 1))
2948 fatal_insn_not_found (insn);
2950 /* Some target machines need to prescan each insn before
2951 it is output. */
2953 #ifdef FINAL_PRESCAN_INSN
2954 FINAL_PRESCAN_INSN (insn, recog_data.operand, recog_data.n_operands);
2955 #endif
2957 if (targetm.have_conditional_execution ()
2958 && GET_CODE (PATTERN (insn)) == COND_EXEC)
2959 current_insn_predicate = COND_EXEC_TEST (PATTERN (insn));
2961 #if HAVE_cc0
2962 cc_prev_status = cc_status;
2964 /* Update `cc_status' for this instruction.
2965 The instruction's output routine may change it further.
2966 If the output routine for a jump insn needs to depend
2967 on the cc status, it should look at cc_prev_status. */
2969 NOTICE_UPDATE_CC (body, insn);
2970 #endif
2972 current_output_insn = debug_insn = insn;
2974 /* Find the proper template for this insn. */
2975 templ = get_insn_template (insn_code_number, insn);
2977 /* If the C code returns 0, it means that it is a jump insn
2978 which follows a deleted test insn, and that test insn
2979 needs to be reinserted. */
2980 if (templ == 0)
2982 rtx_insn *prev;
2984 gcc_assert (prev_nonnote_insn (insn) == last_ignored_compare);
2986 /* We have already processed the notes between the setter and
2987 the user. Make sure we don't process them again, this is
2988 particularly important if one of the notes is a block
2989 scope note or an EH note. */
2990 for (prev = insn;
2991 prev != last_ignored_compare;
2992 prev = PREV_INSN (prev))
2994 if (NOTE_P (prev))
2995 delete_insn (prev); /* Use delete_note. */
2998 return prev;
3001 /* If the template is the string "#", it means that this insn must
3002 be split. */
3003 if (templ[0] == '#' && templ[1] == '\0')
3005 rtx_insn *new_rtx = try_split (body, insn, 0);
3007 /* If we didn't split the insn, go away. */
3008 if (new_rtx == insn && PATTERN (new_rtx) == body)
3009 fatal_insn ("could not split insn", insn);
3011 /* If we have a length attribute, this instruction should have
3012 been split in shorten_branches, to ensure that we would have
3013 valid length info for the splitees. */
3014 gcc_assert (!HAVE_ATTR_length);
3016 return new_rtx;
3019 /* ??? This will put the directives in the wrong place if
3020 get_insn_template outputs assembly directly. However calling it
3021 before get_insn_template breaks if the insns is split. */
3022 if (targetm.asm_out.unwind_emit_before_insn
3023 && targetm.asm_out.unwind_emit)
3024 targetm.asm_out.unwind_emit (asm_out_file, insn);
3026 if (rtx_call_insn *call_insn = dyn_cast <rtx_call_insn *> (insn))
3028 rtx x = call_from_call_insn (call_insn);
3029 x = XEXP (x, 0);
3030 if (x && MEM_P (x) && GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
3032 tree t;
3033 x = XEXP (x, 0);
3034 t = SYMBOL_REF_DECL (x);
3035 if (t)
3036 assemble_external (t);
3038 if (!DECL_IGNORED_P (current_function_decl))
3039 debug_hooks->var_location (insn);
3042 /* Output assembler code from the template. */
3043 output_asm_insn (templ, recog_data.operand);
3045 /* Some target machines need to postscan each insn after
3046 it is output. */
3047 if (targetm.asm_out.final_postscan_insn)
3048 targetm.asm_out.final_postscan_insn (file, insn, recog_data.operand,
3049 recog_data.n_operands);
3051 if (!targetm.asm_out.unwind_emit_before_insn
3052 && targetm.asm_out.unwind_emit)
3053 targetm.asm_out.unwind_emit (asm_out_file, insn);
3055 current_output_insn = debug_insn = 0;
3058 return NEXT_INSN (insn);
3061 /* Return whether a source line note needs to be emitted before INSN.
3062 Sets IS_STMT to TRUE if the line should be marked as a possible
3063 breakpoint location. */
3065 static bool
3066 notice_source_line (rtx_insn *insn, bool *is_stmt)
3068 const char *filename;
3069 int linenum;
3071 if (override_filename)
3073 filename = override_filename;
3074 linenum = override_linenum;
3076 else if (INSN_HAS_LOCATION (insn))
3078 expanded_location xloc = insn_location (insn);
3079 filename = xloc.file;
3080 linenum = xloc.line;
3082 else
3084 filename = NULL;
3085 linenum = 0;
3088 if (filename == NULL)
3089 return false;
3091 if (force_source_line
3092 || filename != last_filename
3093 || last_linenum != linenum)
3095 force_source_line = false;
3096 last_filename = filename;
3097 last_linenum = linenum;
3098 last_discriminator = discriminator;
3099 *is_stmt = true;
3100 high_block_linenum = MAX (last_linenum, high_block_linenum);
3101 high_function_linenum = MAX (last_linenum, high_function_linenum);
3102 return true;
3105 if (SUPPORTS_DISCRIMINATOR && last_discriminator != discriminator)
3107 /* If the discriminator changed, but the line number did not,
3108 output the line table entry with is_stmt false so the
3109 debugger does not treat this as a breakpoint location. */
3110 last_discriminator = discriminator;
3111 *is_stmt = false;
3112 return true;
3115 return false;
3118 /* For each operand in INSN, simplify (subreg (reg)) so that it refers
3119 directly to the desired hard register. */
3121 void
3122 cleanup_subreg_operands (rtx_insn *insn)
3124 int i;
3125 bool changed = false;
3126 extract_insn_cached (insn);
3127 for (i = 0; i < recog_data.n_operands; i++)
3129 /* The following test cannot use recog_data.operand when testing
3130 for a SUBREG: the underlying object might have been changed
3131 already if we are inside a match_operator expression that
3132 matches the else clause. Instead we test the underlying
3133 expression directly. */
3134 if (GET_CODE (*recog_data.operand_loc[i]) == SUBREG)
3136 recog_data.operand[i] = alter_subreg (recog_data.operand_loc[i], true);
3137 changed = true;
3139 else if (GET_CODE (recog_data.operand[i]) == PLUS
3140 || GET_CODE (recog_data.operand[i]) == MULT
3141 || MEM_P (recog_data.operand[i]))
3142 recog_data.operand[i] = walk_alter_subreg (recog_data.operand_loc[i], &changed);
3145 for (i = 0; i < recog_data.n_dups; i++)
3147 if (GET_CODE (*recog_data.dup_loc[i]) == SUBREG)
3149 *recog_data.dup_loc[i] = alter_subreg (recog_data.dup_loc[i], true);
3150 changed = true;
3152 else if (GET_CODE (*recog_data.dup_loc[i]) == PLUS
3153 || GET_CODE (*recog_data.dup_loc[i]) == MULT
3154 || MEM_P (*recog_data.dup_loc[i]))
3155 *recog_data.dup_loc[i] = walk_alter_subreg (recog_data.dup_loc[i], &changed);
3157 if (changed)
3158 df_insn_rescan (insn);
3161 /* If X is a SUBREG, try to replace it with a REG or a MEM, based on
3162 the thing it is a subreg of. Do it anyway if FINAL_P. */
3165 alter_subreg (rtx *xp, bool final_p)
3167 rtx x = *xp;
3168 rtx y = SUBREG_REG (x);
3170 /* simplify_subreg does not remove subreg from volatile references.
3171 We are required to. */
3172 if (MEM_P (y))
3174 int offset = SUBREG_BYTE (x);
3176 /* For paradoxical subregs on big-endian machines, SUBREG_BYTE
3177 contains 0 instead of the proper offset. See simplify_subreg. */
3178 if (offset == 0
3179 && GET_MODE_SIZE (GET_MODE (y)) < GET_MODE_SIZE (GET_MODE (x)))
3181 int difference = GET_MODE_SIZE (GET_MODE (y))
3182 - GET_MODE_SIZE (GET_MODE (x));
3183 if (WORDS_BIG_ENDIAN)
3184 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3185 if (BYTES_BIG_ENDIAN)
3186 offset += difference % UNITS_PER_WORD;
3189 if (final_p)
3190 *xp = adjust_address (y, GET_MODE (x), offset);
3191 else
3192 *xp = adjust_address_nv (y, GET_MODE (x), offset);
3194 else if (REG_P (y) && HARD_REGISTER_P (y))
3196 rtx new_rtx = simplify_subreg (GET_MODE (x), y, GET_MODE (y),
3197 SUBREG_BYTE (x));
3199 if (new_rtx != 0)
3200 *xp = new_rtx;
3201 else if (final_p && REG_P (y))
3203 /* Simplify_subreg can't handle some REG cases, but we have to. */
3204 unsigned int regno;
3205 HOST_WIDE_INT offset;
3207 regno = subreg_regno (x);
3208 if (subreg_lowpart_p (x))
3209 offset = byte_lowpart_offset (GET_MODE (x), GET_MODE (y));
3210 else
3211 offset = SUBREG_BYTE (x);
3212 *xp = gen_rtx_REG_offset (y, GET_MODE (x), regno, offset);
3216 return *xp;
3219 /* Do alter_subreg on all the SUBREGs contained in X. */
3221 static rtx
3222 walk_alter_subreg (rtx *xp, bool *changed)
3224 rtx x = *xp;
3225 switch (GET_CODE (x))
3227 case PLUS:
3228 case MULT:
3229 case AND:
3230 XEXP (x, 0) = walk_alter_subreg (&XEXP (x, 0), changed);
3231 XEXP (x, 1) = walk_alter_subreg (&XEXP (x, 1), changed);
3232 break;
3234 case MEM:
3235 case ZERO_EXTEND:
3236 XEXP (x, 0) = walk_alter_subreg (&XEXP (x, 0), changed);
3237 break;
3239 case SUBREG:
3240 *changed = true;
3241 return alter_subreg (xp, true);
3243 default:
3244 break;
3247 return *xp;
3250 #if HAVE_cc0
3252 /* Given BODY, the body of a jump instruction, alter the jump condition
3253 as required by the bits that are set in cc_status.flags.
3254 Not all of the bits there can be handled at this level in all cases.
3256 The value is normally 0.
3257 1 means that the condition has become always true.
3258 -1 means that the condition has become always false.
3259 2 means that COND has been altered. */
3261 static int
3262 alter_cond (rtx cond)
3264 int value = 0;
3266 if (cc_status.flags & CC_REVERSED)
3268 value = 2;
3269 PUT_CODE (cond, swap_condition (GET_CODE (cond)));
3272 if (cc_status.flags & CC_INVERTED)
3274 value = 2;
3275 PUT_CODE (cond, reverse_condition (GET_CODE (cond)));
3278 if (cc_status.flags & CC_NOT_POSITIVE)
3279 switch (GET_CODE (cond))
3281 case LE:
3282 case LEU:
3283 case GEU:
3284 /* Jump becomes unconditional. */
3285 return 1;
3287 case GT:
3288 case GTU:
3289 case LTU:
3290 /* Jump becomes no-op. */
3291 return -1;
3293 case GE:
3294 PUT_CODE (cond, EQ);
3295 value = 2;
3296 break;
3298 case LT:
3299 PUT_CODE (cond, NE);
3300 value = 2;
3301 break;
3303 default:
3304 break;
3307 if (cc_status.flags & CC_NOT_NEGATIVE)
3308 switch (GET_CODE (cond))
3310 case GE:
3311 case GEU:
3312 /* Jump becomes unconditional. */
3313 return 1;
3315 case LT:
3316 case LTU:
3317 /* Jump becomes no-op. */
3318 return -1;
3320 case LE:
3321 case LEU:
3322 PUT_CODE (cond, EQ);
3323 value = 2;
3324 break;
3326 case GT:
3327 case GTU:
3328 PUT_CODE (cond, NE);
3329 value = 2;
3330 break;
3332 default:
3333 break;
3336 if (cc_status.flags & CC_NO_OVERFLOW)
3337 switch (GET_CODE (cond))
3339 case GEU:
3340 /* Jump becomes unconditional. */
3341 return 1;
3343 case LEU:
3344 PUT_CODE (cond, EQ);
3345 value = 2;
3346 break;
3348 case GTU:
3349 PUT_CODE (cond, NE);
3350 value = 2;
3351 break;
3353 case LTU:
3354 /* Jump becomes no-op. */
3355 return -1;
3357 default:
3358 break;
3361 if (cc_status.flags & (CC_Z_IN_NOT_N | CC_Z_IN_N))
3362 switch (GET_CODE (cond))
3364 default:
3365 gcc_unreachable ();
3367 case NE:
3368 PUT_CODE (cond, cc_status.flags & CC_Z_IN_N ? GE : LT);
3369 value = 2;
3370 break;
3372 case EQ:
3373 PUT_CODE (cond, cc_status.flags & CC_Z_IN_N ? LT : GE);
3374 value = 2;
3375 break;
3378 if (cc_status.flags & CC_NOT_SIGNED)
3379 /* The flags are valid if signed condition operators are converted
3380 to unsigned. */
3381 switch (GET_CODE (cond))
3383 case LE:
3384 PUT_CODE (cond, LEU);
3385 value = 2;
3386 break;
3388 case LT:
3389 PUT_CODE (cond, LTU);
3390 value = 2;
3391 break;
3393 case GT:
3394 PUT_CODE (cond, GTU);
3395 value = 2;
3396 break;
3398 case GE:
3399 PUT_CODE (cond, GEU);
3400 value = 2;
3401 break;
3403 default:
3404 break;
3407 return value;
3409 #endif
3411 /* Report inconsistency between the assembler template and the operands.
3412 In an `asm', it's the user's fault; otherwise, the compiler's fault. */
3414 void
3415 output_operand_lossage (const char *cmsgid, ...)
3417 char *fmt_string;
3418 char *new_message;
3419 const char *pfx_str;
3420 va_list ap;
3422 va_start (ap, cmsgid);
3424 pfx_str = this_is_asm_operands ? _("invalid 'asm': ") : "output_operand: ";
3425 fmt_string = xasprintf ("%s%s", pfx_str, _(cmsgid));
3426 new_message = xvasprintf (fmt_string, ap);
3428 if (this_is_asm_operands)
3429 error_for_asm (this_is_asm_operands, "%s", new_message);
3430 else
3431 internal_error ("%s", new_message);
3433 free (fmt_string);
3434 free (new_message);
3435 va_end (ap);
3438 /* Output of assembler code from a template, and its subroutines. */
3440 /* Annotate the assembly with a comment describing the pattern and
3441 alternative used. */
3443 static void
3444 output_asm_name (void)
3446 if (debug_insn)
3448 int num = INSN_CODE (debug_insn);
3449 fprintf (asm_out_file, "\t%s %d\t%s",
3450 ASM_COMMENT_START, INSN_UID (debug_insn),
3451 insn_data[num].name);
3452 if (insn_data[num].n_alternatives > 1)
3453 fprintf (asm_out_file, "/%d", which_alternative + 1);
3455 if (HAVE_ATTR_length)
3456 fprintf (asm_out_file, "\t[length = %d]",
3457 get_attr_length (debug_insn));
3459 /* Clear this so only the first assembler insn
3460 of any rtl insn will get the special comment for -dp. */
3461 debug_insn = 0;
3465 /* If OP is a REG or MEM and we can find a MEM_EXPR corresponding to it
3466 or its address, return that expr . Set *PADDRESSP to 1 if the expr
3467 corresponds to the address of the object and 0 if to the object. */
3469 static tree
3470 get_mem_expr_from_op (rtx op, int *paddressp)
3472 tree expr;
3473 int inner_addressp;
3475 *paddressp = 0;
3477 if (REG_P (op))
3478 return REG_EXPR (op);
3479 else if (!MEM_P (op))
3480 return 0;
3482 if (MEM_EXPR (op) != 0)
3483 return MEM_EXPR (op);
3485 /* Otherwise we have an address, so indicate it and look at the address. */
3486 *paddressp = 1;
3487 op = XEXP (op, 0);
3489 /* First check if we have a decl for the address, then look at the right side
3490 if it is a PLUS. Otherwise, strip off arithmetic and keep looking.
3491 But don't allow the address to itself be indirect. */
3492 if ((expr = get_mem_expr_from_op (op, &inner_addressp)) && ! inner_addressp)
3493 return expr;
3494 else if (GET_CODE (op) == PLUS
3495 && (expr = get_mem_expr_from_op (XEXP (op, 1), &inner_addressp)))
3496 return expr;
3498 while (UNARY_P (op)
3499 || GET_RTX_CLASS (GET_CODE (op)) == RTX_BIN_ARITH)
3500 op = XEXP (op, 0);
3502 expr = get_mem_expr_from_op (op, &inner_addressp);
3503 return inner_addressp ? 0 : expr;
3506 /* Output operand names for assembler instructions. OPERANDS is the
3507 operand vector, OPORDER is the order to write the operands, and NOPS
3508 is the number of operands to write. */
3510 static void
3511 output_asm_operand_names (rtx *operands, int *oporder, int nops)
3513 int wrote = 0;
3514 int i;
3516 for (i = 0; i < nops; i++)
3518 int addressp;
3519 rtx op = operands[oporder[i]];
3520 tree expr = get_mem_expr_from_op (op, &addressp);
3522 fprintf (asm_out_file, "%c%s",
3523 wrote ? ',' : '\t', wrote ? "" : ASM_COMMENT_START);
3524 wrote = 1;
3525 if (expr)
3527 fprintf (asm_out_file, "%s",
3528 addressp ? "*" : "");
3529 print_mem_expr (asm_out_file, expr);
3530 wrote = 1;
3532 else if (REG_P (op) && ORIGINAL_REGNO (op)
3533 && ORIGINAL_REGNO (op) != REGNO (op))
3534 fprintf (asm_out_file, " tmp%i", ORIGINAL_REGNO (op));
3538 #ifdef ASSEMBLER_DIALECT
3539 /* Helper function to parse assembler dialects in the asm string.
3540 This is called from output_asm_insn and asm_fprintf. */
3541 static const char *
3542 do_assembler_dialects (const char *p, int *dialect)
3544 char c = *(p - 1);
3546 switch (c)
3548 case '{':
3550 int i;
3552 if (*dialect)
3553 output_operand_lossage ("nested assembly dialect alternatives");
3554 else
3555 *dialect = 1;
3557 /* If we want the first dialect, do nothing. Otherwise, skip
3558 DIALECT_NUMBER of strings ending with '|'. */
3559 for (i = 0; i < dialect_number; i++)
3561 while (*p && *p != '}')
3563 if (*p == '|')
3565 p++;
3566 break;
3569 /* Skip over any character after a percent sign. */
3570 if (*p == '%')
3571 p++;
3572 if (*p)
3573 p++;
3576 if (*p == '}')
3577 break;
3580 if (*p == '\0')
3581 output_operand_lossage ("unterminated assembly dialect alternative");
3583 break;
3585 case '|':
3586 if (*dialect)
3588 /* Skip to close brace. */
3591 if (*p == '\0')
3593 output_operand_lossage ("unterminated assembly dialect alternative");
3594 break;
3597 /* Skip over any character after a percent sign. */
3598 if (*p == '%' && p[1])
3600 p += 2;
3601 continue;
3604 if (*p++ == '}')
3605 break;
3607 while (1);
3609 *dialect = 0;
3611 else
3612 putc (c, asm_out_file);
3613 break;
3615 case '}':
3616 if (! *dialect)
3617 putc (c, asm_out_file);
3618 *dialect = 0;
3619 break;
3620 default:
3621 gcc_unreachable ();
3624 return p;
3626 #endif
3628 /* Output text from TEMPLATE to the assembler output file,
3629 obeying %-directions to substitute operands taken from
3630 the vector OPERANDS.
3632 %N (for N a digit) means print operand N in usual manner.
3633 %lN means require operand N to be a CODE_LABEL or LABEL_REF
3634 and print the label name with no punctuation.
3635 %cN means require operand N to be a constant
3636 and print the constant expression with no punctuation.
3637 %aN means expect operand N to be a memory address
3638 (not a memory reference!) and print a reference
3639 to that address.
3640 %nN means expect operand N to be a constant
3641 and print a constant expression for minus the value
3642 of the operand, with no other punctuation. */
3644 void
3645 output_asm_insn (const char *templ, rtx *operands)
3647 const char *p;
3648 int c;
3649 #ifdef ASSEMBLER_DIALECT
3650 int dialect = 0;
3651 #endif
3652 int oporder[MAX_RECOG_OPERANDS];
3653 char opoutput[MAX_RECOG_OPERANDS];
3654 int ops = 0;
3656 /* An insn may return a null string template
3657 in a case where no assembler code is needed. */
3658 if (*templ == 0)
3659 return;
3661 memset (opoutput, 0, sizeof opoutput);
3662 p = templ;
3663 putc ('\t', asm_out_file);
3665 #ifdef ASM_OUTPUT_OPCODE
3666 ASM_OUTPUT_OPCODE (asm_out_file, p);
3667 #endif
3669 while ((c = *p++))
3670 switch (c)
3672 case '\n':
3673 if (flag_verbose_asm)
3674 output_asm_operand_names (operands, oporder, ops);
3675 if (flag_print_asm_name)
3676 output_asm_name ();
3678 ops = 0;
3679 memset (opoutput, 0, sizeof opoutput);
3681 putc (c, asm_out_file);
3682 #ifdef ASM_OUTPUT_OPCODE
3683 while ((c = *p) == '\t')
3685 putc (c, asm_out_file);
3686 p++;
3688 ASM_OUTPUT_OPCODE (asm_out_file, p);
3689 #endif
3690 break;
3692 #ifdef ASSEMBLER_DIALECT
3693 case '{':
3694 case '}':
3695 case '|':
3696 p = do_assembler_dialects (p, &dialect);
3697 break;
3698 #endif
3700 case '%':
3701 /* %% outputs a single %. %{, %} and %| print {, } and | respectively
3702 if ASSEMBLER_DIALECT defined and these characters have a special
3703 meaning as dialect delimiters.*/
3704 if (*p == '%'
3705 #ifdef ASSEMBLER_DIALECT
3706 || *p == '{' || *p == '}' || *p == '|'
3707 #endif
3710 putc (*p, asm_out_file);
3711 p++;
3713 /* %= outputs a number which is unique to each insn in the entire
3714 compilation. This is useful for making local labels that are
3715 referred to more than once in a given insn. */
3716 else if (*p == '=')
3718 p++;
3719 fprintf (asm_out_file, "%d", insn_counter);
3721 /* % followed by a letter and some digits
3722 outputs an operand in a special way depending on the letter.
3723 Letters `acln' are implemented directly.
3724 Other letters are passed to `output_operand' so that
3725 the TARGET_PRINT_OPERAND hook can define them. */
3726 else if (ISALPHA (*p))
3728 int letter = *p++;
3729 unsigned long opnum;
3730 char *endptr;
3732 opnum = strtoul (p, &endptr, 10);
3734 if (endptr == p)
3735 output_operand_lossage ("operand number missing "
3736 "after %%-letter");
3737 else if (this_is_asm_operands && opnum >= insn_noperands)
3738 output_operand_lossage ("operand number out of range");
3739 else if (letter == 'l')
3740 output_asm_label (operands[opnum]);
3741 else if (letter == 'a')
3742 output_address (operands[opnum]);
3743 else if (letter == 'c')
3745 if (CONSTANT_ADDRESS_P (operands[opnum]))
3746 output_addr_const (asm_out_file, operands[opnum]);
3747 else
3748 output_operand (operands[opnum], 'c');
3750 else if (letter == 'n')
3752 if (CONST_INT_P (operands[opnum]))
3753 fprintf (asm_out_file, HOST_WIDE_INT_PRINT_DEC,
3754 - INTVAL (operands[opnum]));
3755 else
3757 putc ('-', asm_out_file);
3758 output_addr_const (asm_out_file, operands[opnum]);
3761 else
3762 output_operand (operands[opnum], letter);
3764 if (!opoutput[opnum])
3765 oporder[ops++] = opnum;
3766 opoutput[opnum] = 1;
3768 p = endptr;
3769 c = *p;
3771 /* % followed by a digit outputs an operand the default way. */
3772 else if (ISDIGIT (*p))
3774 unsigned long opnum;
3775 char *endptr;
3777 opnum = strtoul (p, &endptr, 10);
3778 if (this_is_asm_operands && opnum >= insn_noperands)
3779 output_operand_lossage ("operand number out of range");
3780 else
3781 output_operand (operands[opnum], 0);
3783 if (!opoutput[opnum])
3784 oporder[ops++] = opnum;
3785 opoutput[opnum] = 1;
3787 p = endptr;
3788 c = *p;
3790 /* % followed by punctuation: output something for that
3791 punctuation character alone, with no operand. The
3792 TARGET_PRINT_OPERAND hook decides what is actually done. */
3793 else if (targetm.asm_out.print_operand_punct_valid_p ((unsigned char) *p))
3794 output_operand (NULL_RTX, *p++);
3795 else
3796 output_operand_lossage ("invalid %%-code");
3797 break;
3799 default:
3800 putc (c, asm_out_file);
3803 /* Write out the variable names for operands, if we know them. */
3804 if (flag_verbose_asm)
3805 output_asm_operand_names (operands, oporder, ops);
3806 if (flag_print_asm_name)
3807 output_asm_name ();
3809 putc ('\n', asm_out_file);
3812 /* Output a LABEL_REF, or a bare CODE_LABEL, as an assembler symbol. */
3814 void
3815 output_asm_label (rtx x)
3817 char buf[256];
3819 if (GET_CODE (x) == LABEL_REF)
3820 x = LABEL_REF_LABEL (x);
3821 if (LABEL_P (x)
3822 || (NOTE_P (x)
3823 && NOTE_KIND (x) == NOTE_INSN_DELETED_LABEL))
3824 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
3825 else
3826 output_operand_lossage ("'%%l' operand isn't a label");
3828 assemble_name (asm_out_file, buf);
3831 /* Marks SYMBOL_REFs in x as referenced through use of assemble_external. */
3833 void
3834 mark_symbol_refs_as_used (rtx x)
3836 subrtx_iterator::array_type array;
3837 FOR_EACH_SUBRTX (iter, array, x, ALL)
3839 const_rtx x = *iter;
3840 if (GET_CODE (x) == SYMBOL_REF)
3841 if (tree t = SYMBOL_REF_DECL (x))
3842 assemble_external (t);
3846 /* Print operand X using machine-dependent assembler syntax.
3847 CODE is a non-digit that preceded the operand-number in the % spec,
3848 such as 'z' if the spec was `%z3'. CODE is 0 if there was no char
3849 between the % and the digits.
3850 When CODE is a non-letter, X is 0.
3852 The meanings of the letters are machine-dependent and controlled
3853 by TARGET_PRINT_OPERAND. */
3855 void
3856 output_operand (rtx x, int code ATTRIBUTE_UNUSED)
3858 if (x && GET_CODE (x) == SUBREG)
3859 x = alter_subreg (&x, true);
3861 /* X must not be a pseudo reg. */
3862 if (!targetm.no_register_allocation)
3863 gcc_assert (!x || !REG_P (x) || REGNO (x) < FIRST_PSEUDO_REGISTER);
3865 targetm.asm_out.print_operand (asm_out_file, x, code);
3867 if (x == NULL_RTX)
3868 return;
3870 mark_symbol_refs_as_used (x);
3873 /* Print a memory reference operand for address X using
3874 machine-dependent assembler syntax. */
3876 void
3877 output_address (rtx x)
3879 bool changed = false;
3880 walk_alter_subreg (&x, &changed);
3881 targetm.asm_out.print_operand_address (asm_out_file, x);
3884 /* Print an integer constant expression in assembler syntax.
3885 Addition and subtraction are the only arithmetic
3886 that may appear in these expressions. */
3888 void
3889 output_addr_const (FILE *file, rtx x)
3891 char buf[256];
3893 restart:
3894 switch (GET_CODE (x))
3896 case PC:
3897 putc ('.', file);
3898 break;
3900 case SYMBOL_REF:
3901 if (SYMBOL_REF_DECL (x))
3902 assemble_external (SYMBOL_REF_DECL (x));
3903 #ifdef ASM_OUTPUT_SYMBOL_REF
3904 ASM_OUTPUT_SYMBOL_REF (file, x);
3905 #else
3906 assemble_name (file, XSTR (x, 0));
3907 #endif
3908 break;
3910 case LABEL_REF:
3911 x = LABEL_REF_LABEL (x);
3912 /* Fall through. */
3913 case CODE_LABEL:
3914 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
3915 #ifdef ASM_OUTPUT_LABEL_REF
3916 ASM_OUTPUT_LABEL_REF (file, buf);
3917 #else
3918 assemble_name (file, buf);
3919 #endif
3920 break;
3922 case CONST_INT:
3923 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
3924 break;
3926 case CONST:
3927 /* This used to output parentheses around the expression,
3928 but that does not work on the 386 (either ATT or BSD assembler). */
3929 output_addr_const (file, XEXP (x, 0));
3930 break;
3932 case CONST_WIDE_INT:
3933 /* We do not know the mode here so we have to use a round about
3934 way to build a wide-int to get it printed properly. */
3936 wide_int w = wide_int::from_array (&CONST_WIDE_INT_ELT (x, 0),
3937 CONST_WIDE_INT_NUNITS (x),
3938 CONST_WIDE_INT_NUNITS (x)
3939 * HOST_BITS_PER_WIDE_INT,
3940 false);
3941 print_decs (w, file);
3943 break;
3945 case CONST_DOUBLE:
3946 if (CONST_DOUBLE_AS_INT_P (x))
3948 /* We can use %d if the number is one word and positive. */
3949 if (CONST_DOUBLE_HIGH (x))
3950 fprintf (file, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
3951 (unsigned HOST_WIDE_INT) CONST_DOUBLE_HIGH (x),
3952 (unsigned HOST_WIDE_INT) CONST_DOUBLE_LOW (x));
3953 else if (CONST_DOUBLE_LOW (x) < 0)
3954 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
3955 (unsigned HOST_WIDE_INT) CONST_DOUBLE_LOW (x));
3956 else
3957 fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
3959 else
3960 /* We can't handle floating point constants;
3961 PRINT_OPERAND must handle them. */
3962 output_operand_lossage ("floating constant misused");
3963 break;
3965 case CONST_FIXED:
3966 fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_FIXED_VALUE_LOW (x));
3967 break;
3969 case PLUS:
3970 /* Some assemblers need integer constants to appear last (eg masm). */
3971 if (CONST_INT_P (XEXP (x, 0)))
3973 output_addr_const (file, XEXP (x, 1));
3974 if (INTVAL (XEXP (x, 0)) >= 0)
3975 fprintf (file, "+");
3976 output_addr_const (file, XEXP (x, 0));
3978 else
3980 output_addr_const (file, XEXP (x, 0));
3981 if (!CONST_INT_P (XEXP (x, 1))
3982 || INTVAL (XEXP (x, 1)) >= 0)
3983 fprintf (file, "+");
3984 output_addr_const (file, XEXP (x, 1));
3986 break;
3988 case MINUS:
3989 /* Avoid outputting things like x-x or x+5-x,
3990 since some assemblers can't handle that. */
3991 x = simplify_subtraction (x);
3992 if (GET_CODE (x) != MINUS)
3993 goto restart;
3995 output_addr_const (file, XEXP (x, 0));
3996 fprintf (file, "-");
3997 if ((CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0)
3998 || GET_CODE (XEXP (x, 1)) == PC
3999 || GET_CODE (XEXP (x, 1)) == SYMBOL_REF)
4000 output_addr_const (file, XEXP (x, 1));
4001 else
4003 fputs (targetm.asm_out.open_paren, file);
4004 output_addr_const (file, XEXP (x, 1));
4005 fputs (targetm.asm_out.close_paren, file);
4007 break;
4009 case ZERO_EXTEND:
4010 case SIGN_EXTEND:
4011 case SUBREG:
4012 case TRUNCATE:
4013 output_addr_const (file, XEXP (x, 0));
4014 break;
4016 default:
4017 if (targetm.asm_out.output_addr_const_extra (file, x))
4018 break;
4020 output_operand_lossage ("invalid expression as operand");
4024 /* Output a quoted string. */
4026 void
4027 output_quoted_string (FILE *asm_file, const char *string)
4029 #ifdef OUTPUT_QUOTED_STRING
4030 OUTPUT_QUOTED_STRING (asm_file, string);
4031 #else
4032 char c;
4034 putc ('\"', asm_file);
4035 while ((c = *string++) != 0)
4037 if (ISPRINT (c))
4039 if (c == '\"' || c == '\\')
4040 putc ('\\', asm_file);
4041 putc (c, asm_file);
4043 else
4044 fprintf (asm_file, "\\%03o", (unsigned char) c);
4046 putc ('\"', asm_file);
4047 #endif
4050 /* Write a HOST_WIDE_INT number in hex form 0x1234, fast. */
4052 void
4053 fprint_whex (FILE *f, unsigned HOST_WIDE_INT value)
4055 char buf[2 + CHAR_BIT * sizeof (value) / 4];
4056 if (value == 0)
4057 putc ('0', f);
4058 else
4060 char *p = buf + sizeof (buf);
4062 *--p = "0123456789abcdef"[value % 16];
4063 while ((value /= 16) != 0);
4064 *--p = 'x';
4065 *--p = '0';
4066 fwrite (p, 1, buf + sizeof (buf) - p, f);
4070 /* Internal function that prints an unsigned long in decimal in reverse.
4071 The output string IS NOT null-terminated. */
4073 static int
4074 sprint_ul_rev (char *s, unsigned long value)
4076 int i = 0;
4079 s[i] = "0123456789"[value % 10];
4080 value /= 10;
4081 i++;
4082 /* alternate version, without modulo */
4083 /* oldval = value; */
4084 /* value /= 10; */
4085 /* s[i] = "0123456789" [oldval - 10*value]; */
4086 /* i++ */
4088 while (value != 0);
4089 return i;
4092 /* Write an unsigned long as decimal to a file, fast. */
4094 void
4095 fprint_ul (FILE *f, unsigned long value)
4097 /* python says: len(str(2**64)) == 20 */
4098 char s[20];
4099 int i;
4101 i = sprint_ul_rev (s, value);
4103 /* It's probably too small to bother with string reversal and fputs. */
4106 i--;
4107 putc (s[i], f);
4109 while (i != 0);
4112 /* Write an unsigned long as decimal to a string, fast.
4113 s must be wide enough to not overflow, at least 21 chars.
4114 Returns the length of the string (without terminating '\0'). */
4117 sprint_ul (char *s, unsigned long value)
4119 int len = sprint_ul_rev (s, value);
4120 s[len] = '\0';
4122 std::reverse (s, s + len);
4123 return len;
4126 /* A poor man's fprintf, with the added features of %I, %R, %L, and %U.
4127 %R prints the value of REGISTER_PREFIX.
4128 %L prints the value of LOCAL_LABEL_PREFIX.
4129 %U prints the value of USER_LABEL_PREFIX.
4130 %I prints the value of IMMEDIATE_PREFIX.
4131 %O runs ASM_OUTPUT_OPCODE to transform what follows in the string.
4132 Also supported are %d, %i, %u, %x, %X, %o, %c, %s and %%.
4134 We handle alternate assembler dialects here, just like output_asm_insn. */
4136 void
4137 asm_fprintf (FILE *file, const char *p, ...)
4139 char buf[10];
4140 char *q, c;
4141 #ifdef ASSEMBLER_DIALECT
4142 int dialect = 0;
4143 #endif
4144 va_list argptr;
4146 va_start (argptr, p);
4148 buf[0] = '%';
4150 while ((c = *p++))
4151 switch (c)
4153 #ifdef ASSEMBLER_DIALECT
4154 case '{':
4155 case '}':
4156 case '|':
4157 p = do_assembler_dialects (p, &dialect);
4158 break;
4159 #endif
4161 case '%':
4162 c = *p++;
4163 q = &buf[1];
4164 while (strchr ("-+ #0", c))
4166 *q++ = c;
4167 c = *p++;
4169 while (ISDIGIT (c) || c == '.')
4171 *q++ = c;
4172 c = *p++;
4174 switch (c)
4176 case '%':
4177 putc ('%', file);
4178 break;
4180 case 'd': case 'i': case 'u':
4181 case 'x': case 'X': case 'o':
4182 case 'c':
4183 *q++ = c;
4184 *q = 0;
4185 fprintf (file, buf, va_arg (argptr, int));
4186 break;
4188 case 'w':
4189 /* This is a prefix to the 'd', 'i', 'u', 'x', 'X', and
4190 'o' cases, but we do not check for those cases. It
4191 means that the value is a HOST_WIDE_INT, which may be
4192 either `long' or `long long'. */
4193 memcpy (q, HOST_WIDE_INT_PRINT, strlen (HOST_WIDE_INT_PRINT));
4194 q += strlen (HOST_WIDE_INT_PRINT);
4195 *q++ = *p++;
4196 *q = 0;
4197 fprintf (file, buf, va_arg (argptr, HOST_WIDE_INT));
4198 break;
4200 case 'l':
4201 *q++ = c;
4202 #ifdef HAVE_LONG_LONG
4203 if (*p == 'l')
4205 *q++ = *p++;
4206 *q++ = *p++;
4207 *q = 0;
4208 fprintf (file, buf, va_arg (argptr, long long));
4210 else
4211 #endif
4213 *q++ = *p++;
4214 *q = 0;
4215 fprintf (file, buf, va_arg (argptr, long));
4218 break;
4220 case 's':
4221 *q++ = c;
4222 *q = 0;
4223 fprintf (file, buf, va_arg (argptr, char *));
4224 break;
4226 case 'O':
4227 #ifdef ASM_OUTPUT_OPCODE
4228 ASM_OUTPUT_OPCODE (asm_out_file, p);
4229 #endif
4230 break;
4232 case 'R':
4233 #ifdef REGISTER_PREFIX
4234 fprintf (file, "%s", REGISTER_PREFIX);
4235 #endif
4236 break;
4238 case 'I':
4239 #ifdef IMMEDIATE_PREFIX
4240 fprintf (file, "%s", IMMEDIATE_PREFIX);
4241 #endif
4242 break;
4244 case 'L':
4245 #ifdef LOCAL_LABEL_PREFIX
4246 fprintf (file, "%s", LOCAL_LABEL_PREFIX);
4247 #endif
4248 break;
4250 case 'U':
4251 fputs (user_label_prefix, file);
4252 break;
4254 #ifdef ASM_FPRINTF_EXTENSIONS
4255 /* Uppercase letters are reserved for general use by asm_fprintf
4256 and so are not available to target specific code. In order to
4257 prevent the ASM_FPRINTF_EXTENSIONS macro from using them then,
4258 they are defined here. As they get turned into real extensions
4259 to asm_fprintf they should be removed from this list. */
4260 case 'A': case 'B': case 'C': case 'D': case 'E':
4261 case 'F': case 'G': case 'H': case 'J': case 'K':
4262 case 'M': case 'N': case 'P': case 'Q': case 'S':
4263 case 'T': case 'V': case 'W': case 'Y': case 'Z':
4264 break;
4266 ASM_FPRINTF_EXTENSIONS (file, argptr, p)
4267 #endif
4268 default:
4269 gcc_unreachable ();
4271 break;
4273 default:
4274 putc (c, file);
4276 va_end (argptr);
4279 /* Return nonzero if this function has no function calls. */
4282 leaf_function_p (void)
4284 rtx_insn *insn;
4286 /* Some back-ends (e.g. s390) want leaf functions to stay leaf
4287 functions even if they call mcount. */
4288 if (crtl->profile && !targetm.keep_leaf_when_profiled ())
4289 return 0;
4291 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4293 if (CALL_P (insn)
4294 && ! SIBLING_CALL_P (insn))
4295 return 0;
4296 if (NONJUMP_INSN_P (insn)
4297 && GET_CODE (PATTERN (insn)) == SEQUENCE
4298 && CALL_P (XVECEXP (PATTERN (insn), 0, 0))
4299 && ! SIBLING_CALL_P (XVECEXP (PATTERN (insn), 0, 0)))
4300 return 0;
4303 return 1;
4306 /* Return 1 if branch is a forward branch.
4307 Uses insn_shuid array, so it works only in the final pass. May be used by
4308 output templates to customary add branch prediction hints.
4311 final_forward_branch_p (rtx_insn *insn)
4313 int insn_id, label_id;
4315 gcc_assert (uid_shuid);
4316 insn_id = INSN_SHUID (insn);
4317 label_id = INSN_SHUID (JUMP_LABEL (insn));
4318 /* We've hit some insns that does not have id information available. */
4319 gcc_assert (insn_id && label_id);
4320 return insn_id < label_id;
4323 /* On some machines, a function with no call insns
4324 can run faster if it doesn't create its own register window.
4325 When output, the leaf function should use only the "output"
4326 registers. Ordinarily, the function would be compiled to use
4327 the "input" registers to find its arguments; it is a candidate
4328 for leaf treatment if it uses only the "input" registers.
4329 Leaf function treatment means renumbering so the function
4330 uses the "output" registers instead. */
4332 #ifdef LEAF_REGISTERS
4334 /* Return 1 if this function uses only the registers that can be
4335 safely renumbered. */
4338 only_leaf_regs_used (void)
4340 int i;
4341 const char *const permitted_reg_in_leaf_functions = LEAF_REGISTERS;
4343 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4344 if ((df_regs_ever_live_p (i) || global_regs[i])
4345 && ! permitted_reg_in_leaf_functions[i])
4346 return 0;
4348 if (crtl->uses_pic_offset_table
4349 && pic_offset_table_rtx != 0
4350 && REG_P (pic_offset_table_rtx)
4351 && ! permitted_reg_in_leaf_functions[REGNO (pic_offset_table_rtx)])
4352 return 0;
4354 return 1;
4357 /* Scan all instructions and renumber all registers into those
4358 available in leaf functions. */
4360 static void
4361 leaf_renumber_regs (rtx_insn *first)
4363 rtx_insn *insn;
4365 /* Renumber only the actual patterns.
4366 The reg-notes can contain frame pointer refs,
4367 and renumbering them could crash, and should not be needed. */
4368 for (insn = first; insn; insn = NEXT_INSN (insn))
4369 if (INSN_P (insn))
4370 leaf_renumber_regs_insn (PATTERN (insn));
4373 /* Scan IN_RTX and its subexpressions, and renumber all regs into those
4374 available in leaf functions. */
4376 void
4377 leaf_renumber_regs_insn (rtx in_rtx)
4379 int i, j;
4380 const char *format_ptr;
4382 if (in_rtx == 0)
4383 return;
4385 /* Renumber all input-registers into output-registers.
4386 renumbered_regs would be 1 for an output-register;
4387 they */
4389 if (REG_P (in_rtx))
4391 int newreg;
4393 /* Don't renumber the same reg twice. */
4394 if (in_rtx->used)
4395 return;
4397 newreg = REGNO (in_rtx);
4398 /* Don't try to renumber pseudo regs. It is possible for a pseudo reg
4399 to reach here as part of a REG_NOTE. */
4400 if (newreg >= FIRST_PSEUDO_REGISTER)
4402 in_rtx->used = 1;
4403 return;
4405 newreg = LEAF_REG_REMAP (newreg);
4406 gcc_assert (newreg >= 0);
4407 df_set_regs_ever_live (REGNO (in_rtx), false);
4408 df_set_regs_ever_live (newreg, true);
4409 SET_REGNO (in_rtx, newreg);
4410 in_rtx->used = 1;
4411 return;
4414 if (INSN_P (in_rtx))
4416 /* Inside a SEQUENCE, we find insns.
4417 Renumber just the patterns of these insns,
4418 just as we do for the top-level insns. */
4419 leaf_renumber_regs_insn (PATTERN (in_rtx));
4420 return;
4423 format_ptr = GET_RTX_FORMAT (GET_CODE (in_rtx));
4425 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (in_rtx)); i++)
4426 switch (*format_ptr++)
4428 case 'e':
4429 leaf_renumber_regs_insn (XEXP (in_rtx, i));
4430 break;
4432 case 'E':
4433 if (NULL != XVEC (in_rtx, i))
4435 for (j = 0; j < XVECLEN (in_rtx, i); j++)
4436 leaf_renumber_regs_insn (XVECEXP (in_rtx, i, j));
4438 break;
4440 case 'S':
4441 case 's':
4442 case '0':
4443 case 'i':
4444 case 'w':
4445 case 'n':
4446 case 'u':
4447 break;
4449 default:
4450 gcc_unreachable ();
4453 #endif
4455 /* Turn the RTL into assembly. */
4456 static unsigned int
4457 rest_of_handle_final (void)
4459 const char *fnname = get_fnname_from_decl (current_function_decl);
4461 assemble_start_function (current_function_decl, fnname);
4462 final_start_function (get_insns (), asm_out_file, optimize);
4463 final (get_insns (), asm_out_file, optimize);
4464 if (flag_ipa_ra)
4465 collect_fn_hard_reg_usage ();
4466 final_end_function ();
4468 /* The IA-64 ".handlerdata" directive must be issued before the ".endp"
4469 directive that closes the procedure descriptor. Similarly, for x64 SEH.
4470 Otherwise it's not strictly necessary, but it doesn't hurt either. */
4471 output_function_exception_table (fnname);
4473 assemble_end_function (current_function_decl, fnname);
4475 user_defined_section_attribute = false;
4477 /* Free up reg info memory. */
4478 free_reg_info ();
4480 if (! quiet_flag)
4481 fflush (asm_out_file);
4483 /* Write DBX symbols if requested. */
4485 /* Note that for those inline functions where we don't initially
4486 know for certain that we will be generating an out-of-line copy,
4487 the first invocation of this routine (rest_of_compilation) will
4488 skip over this code by doing a `goto exit_rest_of_compilation;'.
4489 Later on, wrapup_global_declarations will (indirectly) call
4490 rest_of_compilation again for those inline functions that need
4491 to have out-of-line copies generated. During that call, we
4492 *will* be routed past here. */
4494 timevar_push (TV_SYMOUT);
4495 if (!DECL_IGNORED_P (current_function_decl))
4496 debug_hooks->function_decl (current_function_decl);
4497 timevar_pop (TV_SYMOUT);
4499 /* Release the blocks that are linked to DECL_INITIAL() to free the memory. */
4500 DECL_INITIAL (current_function_decl) = error_mark_node;
4502 if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
4503 && targetm.have_ctors_dtors)
4504 targetm.asm_out.constructor (XEXP (DECL_RTL (current_function_decl), 0),
4505 decl_init_priority_lookup
4506 (current_function_decl));
4507 if (DECL_STATIC_DESTRUCTOR (current_function_decl)
4508 && targetm.have_ctors_dtors)
4509 targetm.asm_out.destructor (XEXP (DECL_RTL (current_function_decl), 0),
4510 decl_fini_priority_lookup
4511 (current_function_decl));
4512 return 0;
4515 namespace {
4517 const pass_data pass_data_final =
4519 RTL_PASS, /* type */
4520 "final", /* name */
4521 OPTGROUP_NONE, /* optinfo_flags */
4522 TV_FINAL, /* tv_id */
4523 0, /* properties_required */
4524 0, /* properties_provided */
4525 0, /* properties_destroyed */
4526 0, /* todo_flags_start */
4527 0, /* todo_flags_finish */
4530 class pass_final : public rtl_opt_pass
4532 public:
4533 pass_final (gcc::context *ctxt)
4534 : rtl_opt_pass (pass_data_final, ctxt)
4537 /* opt_pass methods: */
4538 virtual unsigned int execute (function *) { return rest_of_handle_final (); }
4540 }; // class pass_final
4542 } // anon namespace
4544 rtl_opt_pass *
4545 make_pass_final (gcc::context *ctxt)
4547 return new pass_final (ctxt);
4551 static unsigned int
4552 rest_of_handle_shorten_branches (void)
4554 /* Shorten branches. */
4555 shorten_branches (get_insns ());
4556 return 0;
4559 namespace {
4561 const pass_data pass_data_shorten_branches =
4563 RTL_PASS, /* type */
4564 "shorten", /* name */
4565 OPTGROUP_NONE, /* optinfo_flags */
4566 TV_SHORTEN_BRANCH, /* tv_id */
4567 0, /* properties_required */
4568 0, /* properties_provided */
4569 0, /* properties_destroyed */
4570 0, /* todo_flags_start */
4571 0, /* todo_flags_finish */
4574 class pass_shorten_branches : public rtl_opt_pass
4576 public:
4577 pass_shorten_branches (gcc::context *ctxt)
4578 : rtl_opt_pass (pass_data_shorten_branches, ctxt)
4581 /* opt_pass methods: */
4582 virtual unsigned int execute (function *)
4584 return rest_of_handle_shorten_branches ();
4587 }; // class pass_shorten_branches
4589 } // anon namespace
4591 rtl_opt_pass *
4592 make_pass_shorten_branches (gcc::context *ctxt)
4594 return new pass_shorten_branches (ctxt);
4598 static unsigned int
4599 rest_of_clean_state (void)
4601 rtx_insn *insn, *next;
4602 FILE *final_output = NULL;
4603 int save_unnumbered = flag_dump_unnumbered;
4604 int save_noaddr = flag_dump_noaddr;
4606 if (flag_dump_final_insns)
4608 final_output = fopen (flag_dump_final_insns, "a");
4609 if (!final_output)
4611 error ("could not open final insn dump file %qs: %m",
4612 flag_dump_final_insns);
4613 flag_dump_final_insns = NULL;
4615 else
4617 flag_dump_noaddr = flag_dump_unnumbered = 1;
4618 if (flag_compare_debug_opt || flag_compare_debug)
4619 dump_flags |= TDF_NOUID;
4620 dump_function_header (final_output, current_function_decl,
4621 dump_flags);
4622 final_insns_dump_p = true;
4624 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4625 if (LABEL_P (insn))
4626 INSN_UID (insn) = CODE_LABEL_NUMBER (insn);
4627 else
4629 if (NOTE_P (insn))
4630 set_block_for_insn (insn, NULL);
4631 INSN_UID (insn) = 0;
4636 /* It is very important to decompose the RTL instruction chain here:
4637 debug information keeps pointing into CODE_LABEL insns inside the function
4638 body. If these remain pointing to the other insns, we end up preserving
4639 whole RTL chain and attached detailed debug info in memory. */
4640 for (insn = get_insns (); insn; insn = next)
4642 next = NEXT_INSN (insn);
4643 SET_NEXT_INSN (insn) = NULL;
4644 SET_PREV_INSN (insn) = NULL;
4646 if (final_output
4647 && (!NOTE_P (insn) ||
4648 (NOTE_KIND (insn) != NOTE_INSN_VAR_LOCATION
4649 && NOTE_KIND (insn) != NOTE_INSN_CALL_ARG_LOCATION
4650 && NOTE_KIND (insn) != NOTE_INSN_BLOCK_BEG
4651 && NOTE_KIND (insn) != NOTE_INSN_BLOCK_END
4652 && NOTE_KIND (insn) != NOTE_INSN_DELETED_DEBUG_LABEL)))
4653 print_rtl_single (final_output, insn);
4656 if (final_output)
4658 flag_dump_noaddr = save_noaddr;
4659 flag_dump_unnumbered = save_unnumbered;
4660 final_insns_dump_p = false;
4662 if (fclose (final_output))
4664 error ("could not close final insn dump file %qs: %m",
4665 flag_dump_final_insns);
4666 flag_dump_final_insns = NULL;
4670 /* In case the function was not output,
4671 don't leave any temporary anonymous types
4672 queued up for sdb output. */
4673 #ifdef SDB_DEBUGGING_INFO
4674 if (write_symbols == SDB_DEBUG)
4675 sdbout_types (NULL_TREE);
4676 #endif
4678 flag_rerun_cse_after_global_opts = 0;
4679 reload_completed = 0;
4680 epilogue_completed = 0;
4681 #ifdef STACK_REGS
4682 regstack_completed = 0;
4683 #endif
4685 /* Clear out the insn_length contents now that they are no
4686 longer valid. */
4687 init_insn_lengths ();
4689 /* Show no temporary slots allocated. */
4690 init_temp_slots ();
4692 free_bb_for_insn ();
4694 delete_tree_ssa ();
4696 /* We can reduce stack alignment on call site only when we are sure that
4697 the function body just produced will be actually used in the final
4698 executable. */
4699 if (decl_binds_to_current_def_p (current_function_decl))
4701 unsigned int pref = crtl->preferred_stack_boundary;
4702 if (crtl->stack_alignment_needed > crtl->preferred_stack_boundary)
4703 pref = crtl->stack_alignment_needed;
4704 cgraph_node::rtl_info (current_function_decl)
4705 ->preferred_incoming_stack_boundary = pref;
4708 /* Make sure volatile mem refs aren't considered valid operands for
4709 arithmetic insns. We must call this here if this is a nested inline
4710 function, since the above code leaves us in the init_recog state,
4711 and the function context push/pop code does not save/restore volatile_ok.
4713 ??? Maybe it isn't necessary for expand_start_function to call this
4714 anymore if we do it here? */
4716 init_recog_no_volatile ();
4718 /* We're done with this function. Free up memory if we can. */
4719 free_after_parsing (cfun);
4720 free_after_compilation (cfun);
4721 return 0;
4724 namespace {
4726 const pass_data pass_data_clean_state =
4728 RTL_PASS, /* type */
4729 "*clean_state", /* name */
4730 OPTGROUP_NONE, /* optinfo_flags */
4731 TV_FINAL, /* tv_id */
4732 0, /* properties_required */
4733 0, /* properties_provided */
4734 PROP_rtl, /* properties_destroyed */
4735 0, /* todo_flags_start */
4736 0, /* todo_flags_finish */
4739 class pass_clean_state : public rtl_opt_pass
4741 public:
4742 pass_clean_state (gcc::context *ctxt)
4743 : rtl_opt_pass (pass_data_clean_state, ctxt)
4746 /* opt_pass methods: */
4747 virtual unsigned int execute (function *)
4749 return rest_of_clean_state ();
4752 }; // class pass_clean_state
4754 } // anon namespace
4756 rtl_opt_pass *
4757 make_pass_clean_state (gcc::context *ctxt)
4759 return new pass_clean_state (ctxt);
4762 /* Return true if INSN is a call to the the current function. */
4764 static bool
4765 self_recursive_call_p (rtx_insn *insn)
4767 tree fndecl = get_call_fndecl (insn);
4768 return (fndecl == current_function_decl
4769 && decl_binds_to_current_def_p (fndecl));
4772 /* Collect hard register usage for the current function. */
4774 static void
4775 collect_fn_hard_reg_usage (void)
4777 rtx_insn *insn;
4778 #ifdef STACK_REGS
4779 int i;
4780 #endif
4781 struct cgraph_rtl_info *node;
4782 HARD_REG_SET function_used_regs;
4784 /* ??? To be removed when all the ports have been fixed. */
4785 if (!targetm.call_fusage_contains_non_callee_clobbers)
4786 return;
4788 CLEAR_HARD_REG_SET (function_used_regs);
4790 for (insn = get_insns (); insn != NULL_RTX; insn = next_insn (insn))
4792 HARD_REG_SET insn_used_regs;
4794 if (!NONDEBUG_INSN_P (insn))
4795 continue;
4797 if (CALL_P (insn)
4798 && !self_recursive_call_p (insn))
4800 if (!get_call_reg_set_usage (insn, &insn_used_regs,
4801 call_used_reg_set))
4802 return;
4804 IOR_HARD_REG_SET (function_used_regs, insn_used_regs);
4807 find_all_hard_reg_sets (insn, &insn_used_regs, false);
4808 IOR_HARD_REG_SET (function_used_regs, insn_used_regs);
4811 /* Be conservative - mark fixed and global registers as used. */
4812 IOR_HARD_REG_SET (function_used_regs, fixed_reg_set);
4814 #ifdef STACK_REGS
4815 /* Handle STACK_REGS conservatively, since the df-framework does not
4816 provide accurate information for them. */
4818 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
4819 SET_HARD_REG_BIT (function_used_regs, i);
4820 #endif
4822 /* The information we have gathered is only interesting if it exposes a
4823 register from the call_used_regs that is not used in this function. */
4824 if (hard_reg_set_subset_p (call_used_reg_set, function_used_regs))
4825 return;
4827 node = cgraph_node::rtl_info (current_function_decl);
4828 gcc_assert (node != NULL);
4830 COPY_HARD_REG_SET (node->function_used_regs, function_used_regs);
4831 node->function_used_regs_valid = 1;
4834 /* Get the declaration of the function called by INSN. */
4836 static tree
4837 get_call_fndecl (rtx_insn *insn)
4839 rtx note, datum;
4841 note = find_reg_note (insn, REG_CALL_DECL, NULL_RTX);
4842 if (note == NULL_RTX)
4843 return NULL_TREE;
4845 datum = XEXP (note, 0);
4846 if (datum != NULL_RTX)
4847 return SYMBOL_REF_DECL (datum);
4849 return NULL_TREE;
4852 /* Return the cgraph_rtl_info of the function called by INSN. Returns NULL for
4853 call targets that can be overwritten. */
4855 static struct cgraph_rtl_info *
4856 get_call_cgraph_rtl_info (rtx_insn *insn)
4858 tree fndecl;
4860 if (insn == NULL_RTX)
4861 return NULL;
4863 fndecl = get_call_fndecl (insn);
4864 if (fndecl == NULL_TREE
4865 || !decl_binds_to_current_def_p (fndecl))
4866 return NULL;
4868 return cgraph_node::rtl_info (fndecl);
4871 /* Find hard registers used by function call instruction INSN, and return them
4872 in REG_SET. Return DEFAULT_SET in REG_SET if not found. */
4874 bool
4875 get_call_reg_set_usage (rtx_insn *insn, HARD_REG_SET *reg_set,
4876 HARD_REG_SET default_set)
4878 if (flag_ipa_ra)
4880 struct cgraph_rtl_info *node = get_call_cgraph_rtl_info (insn);
4881 if (node != NULL
4882 && node->function_used_regs_valid)
4884 COPY_HARD_REG_SET (*reg_set, node->function_used_regs);
4885 AND_HARD_REG_SET (*reg_set, default_set);
4886 return true;
4890 COPY_HARD_REG_SET (*reg_set, default_set);
4891 return false;