alloc-pool.c, [...]: Add missing whitespace before "(".
[platform/upstream/gcc.git] / gcc / final.c
1 /* Convert RTL to assembler code and output it, for GNU compiler.
2    Copyright (C) 1987-2013 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 /* This is the final pass of the compiler.
21    It looks at the rtl code for a function and outputs assembler code.
22
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'.
28
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.)
33
34    Instructions to set the condition codes are omitted when it can be
35    seen that the condition codes already had the desired values.
36
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.
40
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.  */
44
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "tm.h"
49
50 #include "tree.h"
51 #include "rtl.h"
52 #include "tm_p.h"
53 #include "regs.h"
54 #include "insn-config.h"
55 #include "insn-attr.h"
56 #include "recog.h"
57 #include "conditions.h"
58 #include "flags.h"
59 #include "hard-reg-set.h"
60 #include "output.h"
61 #include "except.h"
62 #include "function.h"
63 #include "rtl-error.h"
64 #include "toplev.h" /* exact_log2, floor_log2 */
65 #include "reload.h"
66 #include "intl.h"
67 #include "basic-block.h"
68 #include "target.h"
69 #include "targhooks.h"
70 #include "debug.h"
71 #include "expr.h"
72 #include "tree-pass.h"
73 #include "tree-ssa.h"
74 #include "cgraph.h"
75 #include "coverage.h"
76 #include "df.h"
77 #include "ggc.h"
78 #include "cfgloop.h"
79 #include "params.h"
80 #include "tree-pretty-print.h" /* for dump_function_header */
81
82 #ifdef XCOFF_DEBUGGING_INFO
83 #include "xcoffout.h"           /* Needed for external data
84                                    declarations for e.g. AIX 4.x.  */
85 #endif
86
87 #include "dwarf2out.h"
88
89 #ifdef DBX_DEBUGGING_INFO
90 #include "dbxout.h"
91 #endif
92
93 #ifdef SDB_DEBUGGING_INFO
94 #include "sdbout.h"
95 #endif
96
97 /* Most ports that aren't using cc0 don't need to define CC_STATUS_INIT.
98    So define a null default for it to save conditionalization later.  */
99 #ifndef CC_STATUS_INIT
100 #define CC_STATUS_INIT
101 #endif
102
103 /* Is the given character a logical line separator for the assembler?  */
104 #ifndef IS_ASM_LOGICAL_LINE_SEPARATOR
105 #define IS_ASM_LOGICAL_LINE_SEPARATOR(C, STR) ((C) == ';')
106 #endif
107
108 #ifndef JUMP_TABLES_IN_TEXT_SECTION
109 #define JUMP_TABLES_IN_TEXT_SECTION 0
110 #endif
111
112 /* Bitflags used by final_scan_insn.  */
113 #define SEEN_BB         1
114 #define SEEN_NOTE       2
115 #define SEEN_EMITTED    4
116
117 /* Last insn processed by final_scan_insn.  */
118 static rtx debug_insn;
119 rtx current_output_insn;
120
121 /* Line number of last NOTE.  */
122 static int last_linenum;
123
124 /* Last discriminator written to assembly.  */
125 static int last_discriminator;
126
127 /* Discriminator of current block.  */
128 static int discriminator;
129
130 /* Highest line number in current block.  */
131 static int high_block_linenum;
132
133 /* Likewise for function.  */
134 static int high_function_linenum;
135
136 /* Filename of last NOTE.  */
137 static const char *last_filename;
138
139 /* Override filename and line number.  */
140 static const char *override_filename;
141 static int override_linenum;
142
143 /* Whether to force emission of a line note before the next insn.  */
144 static bool force_source_line = false;
145
146 extern const int length_unit_log; /* This is defined in insn-attrtab.c.  */
147
148 /* Nonzero while outputting an `asm' with operands.
149    This means that inconsistencies are the user's fault, so don't die.
150    The precise value is the insn being output, to pass to error_for_asm.  */
151 rtx this_is_asm_operands;
152
153 /* Number of operands of this insn, for an `asm' with operands.  */
154 static unsigned int insn_noperands;
155
156 /* Compare optimization flag.  */
157
158 static rtx last_ignored_compare = 0;
159
160 /* Assign a unique number to each insn that is output.
161    This can be used to generate unique local labels.  */
162
163 static int insn_counter = 0;
164
165 #ifdef HAVE_cc0
166 /* This variable contains machine-dependent flags (defined in tm.h)
167    set and examined by output routines
168    that describe how to interpret the condition codes properly.  */
169
170 CC_STATUS cc_status;
171
172 /* During output of an insn, this contains a copy of cc_status
173    from before the insn.  */
174
175 CC_STATUS cc_prev_status;
176 #endif
177
178 /* Number of unmatched NOTE_INSN_BLOCK_BEG notes we have seen.  */
179
180 static int block_depth;
181
182 /* Nonzero if have enabled APP processing of our assembler output.  */
183
184 static int app_on;
185
186 /* If we are outputting an insn sequence, this contains the sequence rtx.
187    Zero otherwise.  */
188
189 rtx final_sequence;
190
191 #ifdef ASSEMBLER_DIALECT
192
193 /* Number of the assembler dialect to use, starting at 0.  */
194 static int dialect_number;
195 #endif
196
197 /* Nonnull if the insn currently being emitted was a COND_EXEC pattern.  */
198 rtx current_insn_predicate;
199
200 /* True if printing into -fdump-final-insns= dump.  */   
201 bool final_insns_dump_p;
202
203 /* True if profile_function should be called, but hasn't been called yet.  */
204 static bool need_profile_function;
205
206 static int asm_insn_count (rtx);
207 static void profile_function (FILE *);
208 static void profile_after_prologue (FILE *);
209 static bool notice_source_line (rtx, bool *);
210 static rtx walk_alter_subreg (rtx *, bool *);
211 static void output_asm_name (void);
212 static void output_alternate_entry_point (FILE *, rtx);
213 static tree get_mem_expr_from_op (rtx, int *);
214 static void output_asm_operand_names (rtx *, int *, int);
215 #ifdef LEAF_REGISTERS
216 static void leaf_renumber_regs (rtx);
217 #endif
218 #ifdef HAVE_cc0
219 static int alter_cond (rtx);
220 #endif
221 #ifndef ADDR_VEC_ALIGN
222 static int final_addr_vec_align (rtx);
223 #endif
224 static int align_fuzz (rtx, rtx, int, unsigned);
225 \f
226 /* Initialize data in final at the beginning of a compilation.  */
227
228 void
229 init_final (const char *filename ATTRIBUTE_UNUSED)
230 {
231   app_on = 0;
232   final_sequence = 0;
233
234 #ifdef ASSEMBLER_DIALECT
235   dialect_number = ASSEMBLER_DIALECT;
236 #endif
237 }
238
239 /* Default target function prologue and epilogue assembler output.
240
241    If not overridden for epilogue code, then the function body itself
242    contains return instructions wherever needed.  */
243 void
244 default_function_pro_epilogue (FILE *file ATTRIBUTE_UNUSED,
245                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
246 {
247 }
248
249 void
250 default_function_switched_text_sections (FILE *file ATTRIBUTE_UNUSED,
251                                          tree decl ATTRIBUTE_UNUSED,
252                                          bool new_is_cold ATTRIBUTE_UNUSED)
253 {
254 }
255
256 /* Default target hook that outputs nothing to a stream.  */
257 void
258 no_asm_to_stream (FILE *file ATTRIBUTE_UNUSED)
259 {
260 }
261
262 /* Enable APP processing of subsequent output.
263    Used before the output from an `asm' statement.  */
264
265 void
266 app_enable (void)
267 {
268   if (! app_on)
269     {
270       fputs (ASM_APP_ON, asm_out_file);
271       app_on = 1;
272     }
273 }
274
275 /* Disable APP processing of subsequent output.
276    Called from varasm.c before most kinds of output.  */
277
278 void
279 app_disable (void)
280 {
281   if (app_on)
282     {
283       fputs (ASM_APP_OFF, asm_out_file);
284       app_on = 0;
285     }
286 }
287 \f
288 /* Return the number of slots filled in the current
289    delayed branch sequence (we don't count the insn needing the
290    delay slot).   Zero if not in a delayed branch sequence.  */
291
292 #ifdef DELAY_SLOTS
293 int
294 dbr_sequence_length (void)
295 {
296   if (final_sequence != 0)
297     return XVECLEN (final_sequence, 0) - 1;
298   else
299     return 0;
300 }
301 #endif
302 \f
303 /* The next two pages contain routines used to compute the length of an insn
304    and to shorten branches.  */
305
306 /* Arrays for insn lengths, and addresses.  The latter is referenced by
307    `insn_current_length'.  */
308
309 static int *insn_lengths;
310
311 vec<int> insn_addresses_;
312
313 /* Max uid for which the above arrays are valid.  */
314 static int insn_lengths_max_uid;
315
316 /* Address of insn being processed.  Used by `insn_current_length'.  */
317 int insn_current_address;
318
319 /* Address of insn being processed in previous iteration.  */
320 int insn_last_address;
321
322 /* known invariant alignment of insn being processed.  */
323 int insn_current_align;
324
325 /* After shorten_branches, for any insn, uid_align[INSN_UID (insn)]
326    gives the next following alignment insn that increases the known
327    alignment, or NULL_RTX if there is no such insn.
328    For any alignment obtained this way, we can again index uid_align with
329    its uid to obtain the next following align that in turn increases the
330    alignment, till we reach NULL_RTX; the sequence obtained this way
331    for each insn we'll call the alignment chain of this insn in the following
332    comments.  */
333
334 struct label_alignment
335 {
336   short alignment;
337   short max_skip;
338 };
339
340 static rtx *uid_align;
341 static int *uid_shuid;
342 static struct label_alignment *label_align;
343
344 /* Indicate that branch shortening hasn't yet been done.  */
345
346 void
347 init_insn_lengths (void)
348 {
349   if (uid_shuid)
350     {
351       free (uid_shuid);
352       uid_shuid = 0;
353     }
354   if (insn_lengths)
355     {
356       free (insn_lengths);
357       insn_lengths = 0;
358       insn_lengths_max_uid = 0;
359     }
360   if (HAVE_ATTR_length)
361     INSN_ADDRESSES_FREE ();
362   if (uid_align)
363     {
364       free (uid_align);
365       uid_align = 0;
366     }
367 }
368
369 /* Obtain the current length of an insn.  If branch shortening has been done,
370    get its actual length.  Otherwise, use FALLBACK_FN to calculate the
371    length.  */
372 static inline int
373 get_attr_length_1 (rtx insn, int (*fallback_fn) (rtx))
374 {
375   rtx body;
376   int i;
377   int length = 0;
378
379   if (!HAVE_ATTR_length)
380     return 0;
381
382   if (insn_lengths_max_uid > INSN_UID (insn))
383     return insn_lengths[INSN_UID (insn)];
384   else
385     switch (GET_CODE (insn))
386       {
387       case NOTE:
388       case BARRIER:
389       case CODE_LABEL:
390       case DEBUG_INSN:
391         return 0;
392
393       case CALL_INSN:
394       case JUMP_INSN:
395         length = fallback_fn (insn);
396         break;
397
398       case INSN:
399         body = PATTERN (insn);
400         if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
401           return 0;
402
403         else if (GET_CODE (body) == ASM_INPUT || asm_noperands (body) >= 0)
404           length = asm_insn_count (body) * fallback_fn (insn);
405         else if (GET_CODE (body) == SEQUENCE)
406           for (i = 0; i < XVECLEN (body, 0); i++)
407             length += get_attr_length_1 (XVECEXP (body, 0, i), fallback_fn);
408         else
409           length = fallback_fn (insn);
410         break;
411
412       default:
413         break;
414       }
415
416 #ifdef ADJUST_INSN_LENGTH
417   ADJUST_INSN_LENGTH (insn, length);
418 #endif
419   return length;
420 }
421
422 /* Obtain the current length of an insn.  If branch shortening has been done,
423    get its actual length.  Otherwise, get its maximum length.  */
424 int
425 get_attr_length (rtx insn)
426 {
427   return get_attr_length_1 (insn, insn_default_length);
428 }
429
430 /* Obtain the current length of an insn.  If branch shortening has been done,
431    get its actual length.  Otherwise, get its minimum length.  */
432 int
433 get_attr_min_length (rtx insn)
434 {
435   return get_attr_length_1 (insn, insn_min_length);
436 }
437 \f
438 /* Code to handle alignment inside shorten_branches.  */
439
440 /* Here is an explanation how the algorithm in align_fuzz can give
441    proper results:
442
443    Call a sequence of instructions beginning with alignment point X
444    and continuing until the next alignment point `block X'.  When `X'
445    is used in an expression, it means the alignment value of the
446    alignment point.
447
448    Call the distance between the start of the first insn of block X, and
449    the end of the last insn of block X `IX', for the `inner size of X'.
450    This is clearly the sum of the instruction lengths.
451
452    Likewise with the next alignment-delimited block following X, which we
453    shall call block Y.
454
455    Call the distance between the start of the first insn of block X, and
456    the start of the first insn of block Y `OX', for the `outer size of X'.
457
458    The estimated padding is then OX - IX.
459
460    OX can be safely estimated as
461
462            if (X >= Y)
463                    OX = round_up(IX, Y)
464            else
465                    OX = round_up(IX, X) + Y - X
466
467    Clearly est(IX) >= real(IX), because that only depends on the
468    instruction lengths, and those being overestimated is a given.
469
470    Clearly round_up(foo, Z) >= round_up(bar, Z) if foo >= bar, so
471    we needn't worry about that when thinking about OX.
472
473    When X >= Y, the alignment provided by Y adds no uncertainty factor
474    for branch ranges starting before X, so we can just round what we have.
475    But when X < Y, we don't know anything about the, so to speak,
476    `middle bits', so we have to assume the worst when aligning up from an
477    address mod X to one mod Y, which is Y - X.  */
478
479 #ifndef LABEL_ALIGN
480 #define LABEL_ALIGN(LABEL) align_labels_log
481 #endif
482
483 #ifndef LOOP_ALIGN
484 #define LOOP_ALIGN(LABEL) align_loops_log
485 #endif
486
487 #ifndef LABEL_ALIGN_AFTER_BARRIER
488 #define LABEL_ALIGN_AFTER_BARRIER(LABEL) 0
489 #endif
490
491 #ifndef JUMP_ALIGN
492 #define JUMP_ALIGN(LABEL) align_jumps_log
493 #endif
494
495 int
496 default_label_align_after_barrier_max_skip (rtx insn ATTRIBUTE_UNUSED)
497 {
498   return 0;
499 }
500
501 int
502 default_loop_align_max_skip (rtx insn ATTRIBUTE_UNUSED)
503 {
504   return align_loops_max_skip;
505 }
506
507 int
508 default_label_align_max_skip (rtx insn ATTRIBUTE_UNUSED)
509 {
510   return align_labels_max_skip;
511 }
512
513 int
514 default_jump_align_max_skip (rtx insn ATTRIBUTE_UNUSED)
515 {
516   return align_jumps_max_skip;
517 }
518
519 #ifndef ADDR_VEC_ALIGN
520 static int
521 final_addr_vec_align (rtx addr_vec)
522 {
523   int align = GET_MODE_SIZE (GET_MODE (PATTERN (addr_vec)));
524
525   if (align > BIGGEST_ALIGNMENT / BITS_PER_UNIT)
526     align = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
527   return exact_log2 (align);
528
529 }
530
531 #define ADDR_VEC_ALIGN(ADDR_VEC) final_addr_vec_align (ADDR_VEC)
532 #endif
533
534 #ifndef INSN_LENGTH_ALIGNMENT
535 #define INSN_LENGTH_ALIGNMENT(INSN) length_unit_log
536 #endif
537
538 #define INSN_SHUID(INSN) (uid_shuid[INSN_UID (INSN)])
539
540 static int min_labelno, max_labelno;
541
542 #define LABEL_TO_ALIGNMENT(LABEL) \
543   (label_align[CODE_LABEL_NUMBER (LABEL) - min_labelno].alignment)
544
545 #define LABEL_TO_MAX_SKIP(LABEL) \
546   (label_align[CODE_LABEL_NUMBER (LABEL) - min_labelno].max_skip)
547
548 /* For the benefit of port specific code do this also as a function.  */
549
550 int
551 label_to_alignment (rtx label)
552 {
553   if (CODE_LABEL_NUMBER (label) <= max_labelno)
554     return LABEL_TO_ALIGNMENT (label);
555   return 0;
556 }
557
558 int
559 label_to_max_skip (rtx label)
560 {
561   if (CODE_LABEL_NUMBER (label) <= max_labelno)
562     return LABEL_TO_MAX_SKIP (label);
563   return 0;
564 }
565
566 /* The differences in addresses
567    between a branch and its target might grow or shrink depending on
568    the alignment the start insn of the range (the branch for a forward
569    branch or the label for a backward branch) starts out on; if these
570    differences are used naively, they can even oscillate infinitely.
571    We therefore want to compute a 'worst case' address difference that
572    is independent of the alignment the start insn of the range end
573    up on, and that is at least as large as the actual difference.
574    The function align_fuzz calculates the amount we have to add to the
575    naively computed difference, by traversing the part of the alignment
576    chain of the start insn of the range that is in front of the end insn
577    of the range, and considering for each alignment the maximum amount
578    that it might contribute to a size increase.
579
580    For casesi tables, we also want to know worst case minimum amounts of
581    address difference, in case a machine description wants to introduce
582    some common offset that is added to all offsets in a table.
583    For this purpose, align_fuzz with a growth argument of 0 computes the
584    appropriate adjustment.  */
585
586 /* Compute the maximum delta by which the difference of the addresses of
587    START and END might grow / shrink due to a different address for start
588    which changes the size of alignment insns between START and END.
589    KNOWN_ALIGN_LOG is the alignment known for START.
590    GROWTH should be ~0 if the objective is to compute potential code size
591    increase, and 0 if the objective is to compute potential shrink.
592    The return value is undefined for any other value of GROWTH.  */
593
594 static int
595 align_fuzz (rtx start, rtx end, int known_align_log, unsigned int growth)
596 {
597   int uid = INSN_UID (start);
598   rtx align_label;
599   int known_align = 1 << known_align_log;
600   int end_shuid = INSN_SHUID (end);
601   int fuzz = 0;
602
603   for (align_label = uid_align[uid]; align_label; align_label = uid_align[uid])
604     {
605       int align_addr, new_align;
606
607       uid = INSN_UID (align_label);
608       align_addr = INSN_ADDRESSES (uid) - insn_lengths[uid];
609       if (uid_shuid[uid] > end_shuid)
610         break;
611       known_align_log = LABEL_TO_ALIGNMENT (align_label);
612       new_align = 1 << known_align_log;
613       if (new_align < known_align)
614         continue;
615       fuzz += (-align_addr ^ growth) & (new_align - known_align);
616       known_align = new_align;
617     }
618   return fuzz;
619 }
620
621 /* Compute a worst-case reference address of a branch so that it
622    can be safely used in the presence of aligned labels.  Since the
623    size of the branch itself is unknown, the size of the branch is
624    not included in the range.  I.e. for a forward branch, the reference
625    address is the end address of the branch as known from the previous
626    branch shortening pass, minus a value to account for possible size
627    increase due to alignment.  For a backward branch, it is the start
628    address of the branch as known from the current pass, plus a value
629    to account for possible size increase due to alignment.
630    NB.: Therefore, the maximum offset allowed for backward branches needs
631    to exclude the branch size.  */
632
633 int
634 insn_current_reference_address (rtx branch)
635 {
636   rtx dest, seq;
637   int seq_uid;
638
639   if (! INSN_ADDRESSES_SET_P ())
640     return 0;
641
642   seq = NEXT_INSN (PREV_INSN (branch));
643   seq_uid = INSN_UID (seq);
644   if (!JUMP_P (branch))
645     /* This can happen for example on the PA; the objective is to know the
646        offset to address something in front of the start of the function.
647        Thus, we can treat it like a backward branch.
648        We assume here that FUNCTION_BOUNDARY / BITS_PER_UNIT is larger than
649        any alignment we'd encounter, so we skip the call to align_fuzz.  */
650     return insn_current_address;
651   dest = JUMP_LABEL (branch);
652
653   /* BRANCH has no proper alignment chain set, so use SEQ.
654      BRANCH also has no INSN_SHUID.  */
655   if (INSN_SHUID (seq) < INSN_SHUID (dest))
656     {
657       /* Forward branch.  */
658       return (insn_last_address + insn_lengths[seq_uid]
659               - align_fuzz (seq, dest, length_unit_log, ~0));
660     }
661   else
662     {
663       /* Backward branch.  */
664       return (insn_current_address
665               + align_fuzz (dest, seq, length_unit_log, ~0));
666     }
667 }
668 \f
669 /* Compute branch alignments based on frequency information in the
670    CFG.  */
671
672 unsigned int
673 compute_alignments (void)
674 {
675   int log, max_skip, max_log;
676   basic_block bb;
677   int freq_max = 0;
678   int freq_threshold = 0;
679
680   if (label_align)
681     {
682       free (label_align);
683       label_align = 0;
684     }
685
686   max_labelno = max_label_num ();
687   min_labelno = get_first_label_num ();
688   label_align = XCNEWVEC (struct label_alignment, max_labelno - min_labelno + 1);
689
690   /* If not optimizing or optimizing for size, don't assign any alignments.  */
691   if (! optimize || optimize_function_for_size_p (cfun))
692     return 0;
693
694   if (dump_file)
695     {
696       dump_reg_info (dump_file);
697       dump_flow_info (dump_file, TDF_DETAILS);
698       flow_loops_dump (dump_file, NULL, 1);
699     }
700   loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
701   FOR_EACH_BB (bb)
702     if (bb->frequency > freq_max)
703       freq_max = bb->frequency;
704   freq_threshold = freq_max / PARAM_VALUE (PARAM_ALIGN_THRESHOLD);
705
706   if (dump_file)
707     fprintf (dump_file, "freq_max: %i\n",freq_max);
708   FOR_EACH_BB (bb)
709     {
710       rtx label = BB_HEAD (bb);
711       int fallthru_frequency = 0, branch_frequency = 0, has_fallthru = 0;
712       edge e;
713       edge_iterator ei;
714
715       if (!LABEL_P (label)
716           || optimize_bb_for_size_p (bb))
717         {
718           if (dump_file)
719             fprintf (dump_file,
720                      "BB %4i freq %4i loop %2i loop_depth %2i skipped.\n",
721                      bb->index, bb->frequency, bb->loop_father->num,
722                      bb_loop_depth (bb));
723           continue;
724         }
725       max_log = LABEL_ALIGN (label);
726       max_skip = targetm.asm_out.label_align_max_skip (label);
727
728       FOR_EACH_EDGE (e, ei, bb->preds)
729         {
730           if (e->flags & EDGE_FALLTHRU)
731             has_fallthru = 1, fallthru_frequency += EDGE_FREQUENCY (e);
732           else
733             branch_frequency += EDGE_FREQUENCY (e);
734         }
735       if (dump_file)
736         {
737           fprintf (dump_file, "BB %4i freq %4i loop %2i loop_depth"
738                    " %2i fall %4i branch %4i",
739                    bb->index, bb->frequency, bb->loop_father->num,
740                    bb_loop_depth (bb),
741                    fallthru_frequency, branch_frequency);
742           if (!bb->loop_father->inner && bb->loop_father->num)
743             fprintf (dump_file, " inner_loop");
744           if (bb->loop_father->header == bb)
745             fprintf (dump_file, " loop_header");
746           fprintf (dump_file, "\n");
747         }
748
749       /* There are two purposes to align block with no fallthru incoming edge:
750          1) to avoid fetch stalls when branch destination is near cache boundary
751          2) to improve cache efficiency in case the previous block is not executed
752             (so it does not need to be in the cache).
753
754          We to catch first case, we align frequently executed blocks.
755          To catch the second, we align blocks that are executed more frequently
756          than the predecessor and the predecessor is likely to not be executed
757          when function is called.  */
758
759       if (!has_fallthru
760           && (branch_frequency > freq_threshold
761               || (bb->frequency > bb->prev_bb->frequency * 10
762                   && (bb->prev_bb->frequency
763                       <= ENTRY_BLOCK_PTR->frequency / 2))))
764         {
765           log = JUMP_ALIGN (label);
766           if (dump_file)
767             fprintf (dump_file, "  jump alignment added.\n");
768           if (max_log < log)
769             {
770               max_log = log;
771               max_skip = targetm.asm_out.jump_align_max_skip (label);
772             }
773         }
774       /* In case block is frequent and reached mostly by non-fallthru edge,
775          align it.  It is most likely a first block of loop.  */
776       if (has_fallthru
777           && optimize_bb_for_speed_p (bb)
778           && branch_frequency + fallthru_frequency > freq_threshold
779           && (branch_frequency
780               > fallthru_frequency * PARAM_VALUE (PARAM_ALIGN_LOOP_ITERATIONS)))
781         {
782           log = LOOP_ALIGN (label);
783           if (dump_file)
784             fprintf (dump_file, "  internal loop alignment added.\n");
785           if (max_log < log)
786             {
787               max_log = log;
788               max_skip = targetm.asm_out.loop_align_max_skip (label);
789             }
790         }
791       LABEL_TO_ALIGNMENT (label) = max_log;
792       LABEL_TO_MAX_SKIP (label) = max_skip;
793     }
794
795   loop_optimizer_finalize ();
796   free_dominance_info (CDI_DOMINATORS);
797   return 0;
798 }
799
800 /* Grow the LABEL_ALIGN array after new labels are created.  */
801
802 static void 
803 grow_label_align (void)
804 {
805   int old = max_labelno;
806   int n_labels;
807   int n_old_labels;
808
809   max_labelno = max_label_num ();
810
811   n_labels = max_labelno - min_labelno + 1;
812   n_old_labels = old - min_labelno + 1;
813
814   label_align = XRESIZEVEC (struct label_alignment, label_align, n_labels);
815
816   /* Range of labels grows monotonically in the function.  Failing here
817      means that the initialization of array got lost.  */
818   gcc_assert (n_old_labels <= n_labels);
819
820   memset (label_align + n_old_labels, 0,
821           (n_labels - n_old_labels) * sizeof (struct label_alignment));
822 }
823
824 /* Update the already computed alignment information.  LABEL_PAIRS is a vector
825    made up of pairs of labels for which the alignment information of the first
826    element will be copied from that of the second element.  */
827
828 void
829 update_alignments (vec<rtx> &label_pairs)
830 {
831   unsigned int i = 0;
832   rtx iter, label;
833
834   if (max_labelno != max_label_num ())
835     grow_label_align ();
836
837   FOR_EACH_VEC_ELT (label_pairs, i, iter)
838     if (i & 1)
839       {
840         LABEL_TO_ALIGNMENT (label) = LABEL_TO_ALIGNMENT (iter);
841         LABEL_TO_MAX_SKIP (label) = LABEL_TO_MAX_SKIP (iter);
842       }
843     else
844       label = iter;
845 }
846
847 namespace {
848
849 const pass_data pass_data_compute_alignments =
850 {
851   RTL_PASS, /* type */
852   "alignments", /* name */
853   OPTGROUP_NONE, /* optinfo_flags */
854   false, /* has_gate */
855   true, /* has_execute */
856   TV_NONE, /* tv_id */
857   0, /* properties_required */
858   0, /* properties_provided */
859   0, /* properties_destroyed */
860   0, /* todo_flags_start */
861   TODO_verify_rtl_sharing, /* todo_flags_finish */
862 };
863
864 class pass_compute_alignments : public rtl_opt_pass
865 {
866 public:
867   pass_compute_alignments (gcc::context *ctxt)
868     : rtl_opt_pass (pass_data_compute_alignments, ctxt)
869   {}
870
871   /* opt_pass methods: */
872   unsigned int execute () { return compute_alignments (); }
873
874 }; // class pass_compute_alignments
875
876 } // anon namespace
877
878 rtl_opt_pass *
879 make_pass_compute_alignments (gcc::context *ctxt)
880 {
881   return new pass_compute_alignments (ctxt);
882 }
883
884 \f
885 /* Make a pass over all insns and compute their actual lengths by shortening
886    any branches of variable length if possible.  */
887
888 /* shorten_branches might be called multiple times:  for example, the SH
889    port splits out-of-range conditional branches in MACHINE_DEPENDENT_REORG.
890    In order to do this, it needs proper length information, which it obtains
891    by calling shorten_branches.  This cannot be collapsed with
892    shorten_branches itself into a single pass unless we also want to integrate
893    reorg.c, since the branch splitting exposes new instructions with delay
894    slots.  */
895
896 void
897 shorten_branches (rtx first)
898 {
899   rtx insn;
900   int max_uid;
901   int i;
902   int max_log;
903   int max_skip;
904 #define MAX_CODE_ALIGN 16
905   rtx seq;
906   int something_changed = 1;
907   char *varying_length;
908   rtx body;
909   int uid;
910   rtx align_tab[MAX_CODE_ALIGN];
911
912   /* Compute maximum UID and allocate label_align / uid_shuid.  */
913   max_uid = get_max_uid ();
914
915   /* Free uid_shuid before reallocating it.  */
916   free (uid_shuid);
917
918   uid_shuid = XNEWVEC (int, max_uid);
919
920   if (max_labelno != max_label_num ())
921     grow_label_align ();
922
923   /* Initialize label_align and set up uid_shuid to be strictly
924      monotonically rising with insn order.  */
925   /* We use max_log here to keep track of the maximum alignment we want to
926      impose on the next CODE_LABEL (or the current one if we are processing
927      the CODE_LABEL itself).  */
928
929   max_log = 0;
930   max_skip = 0;
931
932   for (insn = get_insns (), i = 1; insn; insn = NEXT_INSN (insn))
933     {
934       int log;
935
936       INSN_SHUID (insn) = i++;
937       if (INSN_P (insn))
938         continue;
939
940       if (LABEL_P (insn))
941         {
942           rtx next;
943           bool next_is_jumptable;
944
945           /* Merge in alignments computed by compute_alignments.  */
946           log = LABEL_TO_ALIGNMENT (insn);
947           if (max_log < log)
948             {
949               max_log = log;
950               max_skip = LABEL_TO_MAX_SKIP (insn);
951             }
952
953           next = next_nonnote_insn (insn);
954           next_is_jumptable = next && JUMP_TABLE_DATA_P (next);
955           if (!next_is_jumptable)
956             {
957               log = LABEL_ALIGN (insn);
958               if (max_log < log)
959                 {
960                   max_log = log;
961                   max_skip = targetm.asm_out.label_align_max_skip (insn);
962                 }
963             }
964           /* ADDR_VECs only take room if read-only data goes into the text
965              section.  */
966           if ((JUMP_TABLES_IN_TEXT_SECTION
967                || readonly_data_section == text_section)
968               && next_is_jumptable)
969             {
970               log = ADDR_VEC_ALIGN (next);
971               if (max_log < log)
972                 {
973                   max_log = log;
974                   max_skip = targetm.asm_out.label_align_max_skip (insn);
975                 }
976             }
977           LABEL_TO_ALIGNMENT (insn) = max_log;
978           LABEL_TO_MAX_SKIP (insn) = max_skip;
979           max_log = 0;
980           max_skip = 0;
981         }
982       else if (BARRIER_P (insn))
983         {
984           rtx label;
985
986           for (label = insn; label && ! INSN_P (label);
987                label = NEXT_INSN (label))
988             if (LABEL_P (label))
989               {
990                 log = LABEL_ALIGN_AFTER_BARRIER (insn);
991                 if (max_log < log)
992                   {
993                     max_log = log;
994                     max_skip = targetm.asm_out.label_align_after_barrier_max_skip (label);
995                   }
996                 break;
997               }
998         }
999     }
1000   if (!HAVE_ATTR_length)
1001     return;
1002
1003   /* Allocate the rest of the arrays.  */
1004   insn_lengths = XNEWVEC (int, max_uid);
1005   insn_lengths_max_uid = max_uid;
1006   /* Syntax errors can lead to labels being outside of the main insn stream.
1007      Initialize insn_addresses, so that we get reproducible results.  */
1008   INSN_ADDRESSES_ALLOC (max_uid);
1009
1010   varying_length = XCNEWVEC (char, max_uid);
1011
1012   /* Initialize uid_align.  We scan instructions
1013      from end to start, and keep in align_tab[n] the last seen insn
1014      that does an alignment of at least n+1, i.e. the successor
1015      in the alignment chain for an insn that does / has a known
1016      alignment of n.  */
1017   uid_align = XCNEWVEC (rtx, max_uid);
1018
1019   for (i = MAX_CODE_ALIGN; --i >= 0;)
1020     align_tab[i] = NULL_RTX;
1021   seq = get_last_insn ();
1022   for (; seq; seq = PREV_INSN (seq))
1023     {
1024       int uid = INSN_UID (seq);
1025       int log;
1026       log = (LABEL_P (seq) ? LABEL_TO_ALIGNMENT (seq) : 0);
1027       uid_align[uid] = align_tab[0];
1028       if (log)
1029         {
1030           /* Found an alignment label.  */
1031           uid_align[uid] = align_tab[log];
1032           for (i = log - 1; i >= 0; i--)
1033             align_tab[i] = seq;
1034         }
1035     }
1036
1037   /* When optimizing, we start assuming minimum length, and keep increasing
1038      lengths as we find the need for this, till nothing changes.
1039      When not optimizing, we start assuming maximum lengths, and
1040      do a single pass to update the lengths.  */
1041   bool increasing = optimize != 0;
1042
1043 #ifdef CASE_VECTOR_SHORTEN_MODE
1044   if (optimize)
1045     {
1046       /* Look for ADDR_DIFF_VECs, and initialize their minimum and maximum
1047          label fields.  */
1048
1049       int min_shuid = INSN_SHUID (get_insns ()) - 1;
1050       int max_shuid = INSN_SHUID (get_last_insn ()) + 1;
1051       int rel;
1052
1053       for (insn = first; insn != 0; insn = NEXT_INSN (insn))
1054         {
1055           rtx min_lab = NULL_RTX, max_lab = NULL_RTX, pat;
1056           int len, i, min, max, insn_shuid;
1057           int min_align;
1058           addr_diff_vec_flags flags;
1059
1060           if (! JUMP_TABLE_DATA_P (insn)
1061               || GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
1062             continue;
1063           pat = PATTERN (insn);
1064           len = XVECLEN (pat, 1);
1065           gcc_assert (len > 0);
1066           min_align = MAX_CODE_ALIGN;
1067           for (min = max_shuid, max = min_shuid, i = len - 1; i >= 0; i--)
1068             {
1069               rtx lab = XEXP (XVECEXP (pat, 1, i), 0);
1070               int shuid = INSN_SHUID (lab);
1071               if (shuid < min)
1072                 {
1073                   min = shuid;
1074                   min_lab = lab;
1075                 }
1076               if (shuid > max)
1077                 {
1078                   max = shuid;
1079                   max_lab = lab;
1080                 }
1081               if (min_align > LABEL_TO_ALIGNMENT (lab))
1082                 min_align = LABEL_TO_ALIGNMENT (lab);
1083             }
1084           XEXP (pat, 2) = gen_rtx_LABEL_REF (Pmode, min_lab);
1085           XEXP (pat, 3) = gen_rtx_LABEL_REF (Pmode, max_lab);
1086           insn_shuid = INSN_SHUID (insn);
1087           rel = INSN_SHUID (XEXP (XEXP (pat, 0), 0));
1088           memset (&flags, 0, sizeof (flags));
1089           flags.min_align = min_align;
1090           flags.base_after_vec = rel > insn_shuid;
1091           flags.min_after_vec  = min > insn_shuid;
1092           flags.max_after_vec  = max > insn_shuid;
1093           flags.min_after_base = min > rel;
1094           flags.max_after_base = max > rel;
1095           ADDR_DIFF_VEC_FLAGS (pat) = flags;
1096
1097           if (increasing)
1098             PUT_MODE (pat, CASE_VECTOR_SHORTEN_MODE (0, 0, pat));
1099         }
1100     }
1101 #endif /* CASE_VECTOR_SHORTEN_MODE */
1102
1103   /* Compute initial lengths, addresses, and varying flags for each insn.  */
1104   int (*length_fun) (rtx) = increasing ? insn_min_length : insn_default_length;
1105
1106   for (insn_current_address = 0, insn = first;
1107        insn != 0;
1108        insn_current_address += insn_lengths[uid], insn = NEXT_INSN (insn))
1109     {
1110       uid = INSN_UID (insn);
1111
1112       insn_lengths[uid] = 0;
1113
1114       if (LABEL_P (insn))
1115         {
1116           int log = LABEL_TO_ALIGNMENT (insn);
1117           if (log)
1118             {
1119               int align = 1 << log;
1120               int new_address = (insn_current_address + align - 1) & -align;
1121               insn_lengths[uid] = new_address - insn_current_address;
1122             }
1123         }
1124
1125       INSN_ADDRESSES (uid) = insn_current_address + insn_lengths[uid];
1126
1127       if (NOTE_P (insn) || BARRIER_P (insn)
1128           || LABEL_P (insn) || DEBUG_INSN_P (insn))
1129         continue;
1130       if (INSN_DELETED_P (insn))
1131         continue;
1132
1133       body = PATTERN (insn);
1134       if (JUMP_TABLE_DATA_P (insn))
1135         {
1136           /* This only takes room if read-only data goes into the text
1137              section.  */
1138           if (JUMP_TABLES_IN_TEXT_SECTION
1139               || readonly_data_section == text_section)
1140             insn_lengths[uid] = (XVECLEN (body,
1141                                           GET_CODE (body) == ADDR_DIFF_VEC)
1142                                  * GET_MODE_SIZE (GET_MODE (body)));
1143           /* Alignment is handled by ADDR_VEC_ALIGN.  */
1144         }
1145       else if (GET_CODE (body) == ASM_INPUT || asm_noperands (body) >= 0)
1146         insn_lengths[uid] = asm_insn_count (body) * insn_default_length (insn);
1147       else if (GET_CODE (body) == SEQUENCE)
1148         {
1149           int i;
1150           int const_delay_slots;
1151 #ifdef DELAY_SLOTS
1152           const_delay_slots = const_num_delay_slots (XVECEXP (body, 0, 0));
1153 #else
1154           const_delay_slots = 0;
1155 #endif
1156           int (*inner_length_fun) (rtx)
1157             = const_delay_slots ? length_fun : insn_default_length;
1158           /* Inside a delay slot sequence, we do not do any branch shortening
1159              if the shortening could change the number of delay slots
1160              of the branch.  */
1161           for (i = 0; i < XVECLEN (body, 0); i++)
1162             {
1163               rtx inner_insn = XVECEXP (body, 0, i);
1164               int inner_uid = INSN_UID (inner_insn);
1165               int inner_length;
1166
1167               if (GET_CODE (body) == ASM_INPUT
1168                   || asm_noperands (PATTERN (XVECEXP (body, 0, i))) >= 0)
1169                 inner_length = (asm_insn_count (PATTERN (inner_insn))
1170                                 * insn_default_length (inner_insn));
1171               else
1172                 inner_length = inner_length_fun (inner_insn);
1173
1174               insn_lengths[inner_uid] = inner_length;
1175               if (const_delay_slots)
1176                 {
1177                   if ((varying_length[inner_uid]
1178                        = insn_variable_length_p (inner_insn)) != 0)
1179                     varying_length[uid] = 1;
1180                   INSN_ADDRESSES (inner_uid) = (insn_current_address
1181                                                 + insn_lengths[uid]);
1182                 }
1183               else
1184                 varying_length[inner_uid] = 0;
1185               insn_lengths[uid] += inner_length;
1186             }
1187         }
1188       else if (GET_CODE (body) != USE && GET_CODE (body) != CLOBBER)
1189         {
1190           insn_lengths[uid] = length_fun (insn);
1191           varying_length[uid] = insn_variable_length_p (insn);
1192         }
1193
1194       /* If needed, do any adjustment.  */
1195 #ifdef ADJUST_INSN_LENGTH
1196       ADJUST_INSN_LENGTH (insn, insn_lengths[uid]);
1197       if (insn_lengths[uid] < 0)
1198         fatal_insn ("negative insn length", insn);
1199 #endif
1200     }
1201
1202   /* Now loop over all the insns finding varying length insns.  For each,
1203      get the current insn length.  If it has changed, reflect the change.
1204      When nothing changes for a full pass, we are done.  */
1205
1206   while (something_changed)
1207     {
1208       something_changed = 0;
1209       insn_current_align = MAX_CODE_ALIGN - 1;
1210       for (insn_current_address = 0, insn = first;
1211            insn != 0;
1212            insn = NEXT_INSN (insn))
1213         {
1214           int new_length;
1215 #ifdef ADJUST_INSN_LENGTH
1216           int tmp_length;
1217 #endif
1218           int length_align;
1219
1220           uid = INSN_UID (insn);
1221
1222           if (LABEL_P (insn))
1223             {
1224               int log = LABEL_TO_ALIGNMENT (insn);
1225
1226 #ifdef CASE_VECTOR_SHORTEN_MODE
1227               /* If the mode of a following jump table was changed, we
1228                  may need to update the alignment of this label.  */
1229               rtx next;
1230               bool next_is_jumptable;
1231
1232               next = next_nonnote_insn (insn);
1233               next_is_jumptable = next && JUMP_TABLE_DATA_P (next);
1234               if ((JUMP_TABLES_IN_TEXT_SECTION
1235                    || readonly_data_section == text_section)
1236                   && next_is_jumptable)
1237                 {
1238                   int newlog = ADDR_VEC_ALIGN (next);
1239                   if (newlog != log)
1240                     {
1241                       log = newlog;
1242                       LABEL_TO_ALIGNMENT (insn) = log;
1243                       something_changed = 1;
1244                     }
1245                 }
1246 #endif
1247
1248               if (log > insn_current_align)
1249                 {
1250                   int align = 1 << log;
1251                   int new_address= (insn_current_address + align - 1) & -align;
1252                   insn_lengths[uid] = new_address - insn_current_address;
1253                   insn_current_align = log;
1254                   insn_current_address = new_address;
1255                 }
1256               else
1257                 insn_lengths[uid] = 0;
1258               INSN_ADDRESSES (uid) = insn_current_address;
1259               continue;
1260             }
1261
1262           length_align = INSN_LENGTH_ALIGNMENT (insn);
1263           if (length_align < insn_current_align)
1264             insn_current_align = length_align;
1265
1266           insn_last_address = INSN_ADDRESSES (uid);
1267           INSN_ADDRESSES (uid) = insn_current_address;
1268
1269 #ifdef CASE_VECTOR_SHORTEN_MODE
1270           if (optimize
1271               && JUMP_TABLE_DATA_P (insn)
1272               && GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
1273             {
1274               rtx body = PATTERN (insn);
1275               int old_length = insn_lengths[uid];
1276               rtx rel_lab = XEXP (XEXP (body, 0), 0);
1277               rtx min_lab = XEXP (XEXP (body, 2), 0);
1278               rtx max_lab = XEXP (XEXP (body, 3), 0);
1279               int rel_addr = INSN_ADDRESSES (INSN_UID (rel_lab));
1280               int min_addr = INSN_ADDRESSES (INSN_UID (min_lab));
1281               int max_addr = INSN_ADDRESSES (INSN_UID (max_lab));
1282               rtx prev;
1283               int rel_align = 0;
1284               addr_diff_vec_flags flags;
1285               enum machine_mode vec_mode;
1286
1287               /* Avoid automatic aggregate initialization.  */
1288               flags = ADDR_DIFF_VEC_FLAGS (body);
1289
1290               /* Try to find a known alignment for rel_lab.  */
1291               for (prev = rel_lab;
1292                    prev
1293                    && ! insn_lengths[INSN_UID (prev)]
1294                    && ! (varying_length[INSN_UID (prev)] & 1);
1295                    prev = PREV_INSN (prev))
1296                 if (varying_length[INSN_UID (prev)] & 2)
1297                   {
1298                     rel_align = LABEL_TO_ALIGNMENT (prev);
1299                     break;
1300                   }
1301
1302               /* See the comment on addr_diff_vec_flags in rtl.h for the
1303                  meaning of the flags values.  base: REL_LAB   vec: INSN  */
1304               /* Anything after INSN has still addresses from the last
1305                  pass; adjust these so that they reflect our current
1306                  estimate for this pass.  */
1307               if (flags.base_after_vec)
1308                 rel_addr += insn_current_address - insn_last_address;
1309               if (flags.min_after_vec)
1310                 min_addr += insn_current_address - insn_last_address;
1311               if (flags.max_after_vec)
1312                 max_addr += insn_current_address - insn_last_address;
1313               /* We want to know the worst case, i.e. lowest possible value
1314                  for the offset of MIN_LAB.  If MIN_LAB is after REL_LAB,
1315                  its offset is positive, and we have to be wary of code shrink;
1316                  otherwise, it is negative, and we have to be vary of code
1317                  size increase.  */
1318               if (flags.min_after_base)
1319                 {
1320                   /* If INSN is between REL_LAB and MIN_LAB, the size
1321                      changes we are about to make can change the alignment
1322                      within the observed offset, therefore we have to break
1323                      it up into two parts that are independent.  */
1324                   if (! flags.base_after_vec && flags.min_after_vec)
1325                     {
1326                       min_addr -= align_fuzz (rel_lab, insn, rel_align, 0);
1327                       min_addr -= align_fuzz (insn, min_lab, 0, 0);
1328                     }
1329                   else
1330                     min_addr -= align_fuzz (rel_lab, min_lab, rel_align, 0);
1331                 }
1332               else
1333                 {
1334                   if (flags.base_after_vec && ! flags.min_after_vec)
1335                     {
1336                       min_addr -= align_fuzz (min_lab, insn, 0, ~0);
1337                       min_addr -= align_fuzz (insn, rel_lab, 0, ~0);
1338                     }
1339                   else
1340                     min_addr -= align_fuzz (min_lab, rel_lab, 0, ~0);
1341                 }
1342               /* Likewise, determine the highest lowest possible value
1343                  for the offset of MAX_LAB.  */
1344               if (flags.max_after_base)
1345                 {
1346                   if (! flags.base_after_vec && flags.max_after_vec)
1347                     {
1348                       max_addr += align_fuzz (rel_lab, insn, rel_align, ~0);
1349                       max_addr += align_fuzz (insn, max_lab, 0, ~0);
1350                     }
1351                   else
1352                     max_addr += align_fuzz (rel_lab, max_lab, rel_align, ~0);
1353                 }
1354               else
1355                 {
1356                   if (flags.base_after_vec && ! flags.max_after_vec)
1357                     {
1358                       max_addr += align_fuzz (max_lab, insn, 0, 0);
1359                       max_addr += align_fuzz (insn, rel_lab, 0, 0);
1360                     }
1361                   else
1362                     max_addr += align_fuzz (max_lab, rel_lab, 0, 0);
1363                 }
1364               vec_mode = CASE_VECTOR_SHORTEN_MODE (min_addr - rel_addr,
1365                                                    max_addr - rel_addr, body);
1366               if (!increasing
1367                   || (GET_MODE_SIZE (vec_mode)
1368                       >= GET_MODE_SIZE (GET_MODE (body))))
1369                 PUT_MODE (body, vec_mode);
1370               if (JUMP_TABLES_IN_TEXT_SECTION
1371                   || readonly_data_section == text_section)
1372                 {
1373                   insn_lengths[uid]
1374                     = (XVECLEN (body, 1) * GET_MODE_SIZE (GET_MODE (body)));
1375                   insn_current_address += insn_lengths[uid];
1376                   if (insn_lengths[uid] != old_length)
1377                     something_changed = 1;
1378                 }
1379
1380               continue;
1381             }
1382 #endif /* CASE_VECTOR_SHORTEN_MODE */
1383
1384           if (! (varying_length[uid]))
1385             {
1386               if (NONJUMP_INSN_P (insn)
1387                   && GET_CODE (PATTERN (insn)) == SEQUENCE)
1388                 {
1389                   int i;
1390
1391                   body = PATTERN (insn);
1392                   for (i = 0; i < XVECLEN (body, 0); i++)
1393                     {
1394                       rtx inner_insn = XVECEXP (body, 0, i);
1395                       int inner_uid = INSN_UID (inner_insn);
1396
1397                       INSN_ADDRESSES (inner_uid) = insn_current_address;
1398
1399                       insn_current_address += insn_lengths[inner_uid];
1400                     }
1401                 }
1402               else
1403                 insn_current_address += insn_lengths[uid];
1404
1405               continue;
1406             }
1407
1408           if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE)
1409             {
1410               int i;
1411
1412               body = PATTERN (insn);
1413               new_length = 0;
1414               for (i = 0; i < XVECLEN (body, 0); i++)
1415                 {
1416                   rtx inner_insn = XVECEXP (body, 0, i);
1417                   int inner_uid = INSN_UID (inner_insn);
1418                   int inner_length;
1419
1420                   INSN_ADDRESSES (inner_uid) = insn_current_address;
1421
1422                   /* insn_current_length returns 0 for insns with a
1423                      non-varying length.  */
1424                   if (! varying_length[inner_uid])
1425                     inner_length = insn_lengths[inner_uid];
1426                   else
1427                     inner_length = insn_current_length (inner_insn);
1428
1429                   if (inner_length != insn_lengths[inner_uid])
1430                     {
1431                       if (!increasing || inner_length > insn_lengths[inner_uid])
1432                         {
1433                           insn_lengths[inner_uid] = inner_length;
1434                           something_changed = 1;
1435                         }
1436                       else
1437                         inner_length = insn_lengths[inner_uid];
1438                     }
1439                   insn_current_address += inner_length;
1440                   new_length += inner_length;
1441                 }
1442             }
1443           else
1444             {
1445               new_length = insn_current_length (insn);
1446               insn_current_address += new_length;
1447             }
1448
1449 #ifdef ADJUST_INSN_LENGTH
1450           /* If needed, do any adjustment.  */
1451           tmp_length = new_length;
1452           ADJUST_INSN_LENGTH (insn, new_length);
1453           insn_current_address += (new_length - tmp_length);
1454 #endif
1455
1456           if (new_length != insn_lengths[uid]
1457               && (!increasing || new_length > insn_lengths[uid]))
1458             {
1459               insn_lengths[uid] = new_length;
1460               something_changed = 1;
1461             }
1462           else
1463             insn_current_address += insn_lengths[uid] - new_length;
1464         }
1465       /* For a non-optimizing compile, do only a single pass.  */
1466       if (!increasing)
1467         break;
1468     }
1469
1470   free (varying_length);
1471 }
1472
1473 /* Given the body of an INSN known to be generated by an ASM statement, return
1474    the number of machine instructions likely to be generated for this insn.
1475    This is used to compute its length.  */
1476
1477 static int
1478 asm_insn_count (rtx body)
1479 {
1480   const char *templ;
1481
1482   if (GET_CODE (body) == ASM_INPUT)
1483     templ = XSTR (body, 0);
1484   else
1485     templ = decode_asm_operands (body, NULL, NULL, NULL, NULL, NULL);
1486
1487   return asm_str_count (templ);
1488 }
1489
1490 /* Return the number of machine instructions likely to be generated for the
1491    inline-asm template. */
1492 int
1493 asm_str_count (const char *templ)
1494 {
1495   int count = 1;
1496
1497   if (!*templ)
1498     return 0;
1499
1500   for (; *templ; templ++)
1501     if (IS_ASM_LOGICAL_LINE_SEPARATOR (*templ, templ)
1502         || *templ == '\n')
1503       count++;
1504
1505   return count;
1506 }
1507 \f
1508 /* ??? This is probably the wrong place for these.  */
1509 /* Structure recording the mapping from source file and directory
1510    names at compile time to those to be embedded in debug
1511    information.  */
1512 typedef struct debug_prefix_map
1513 {
1514   const char *old_prefix;
1515   const char *new_prefix;
1516   size_t old_len;
1517   size_t new_len;
1518   struct debug_prefix_map *next;
1519 } debug_prefix_map;
1520
1521 /* Linked list of such structures.  */
1522 static debug_prefix_map *debug_prefix_maps;
1523
1524
1525 /* Record a debug file prefix mapping.  ARG is the argument to
1526    -fdebug-prefix-map and must be of the form OLD=NEW.  */
1527
1528 void
1529 add_debug_prefix_map (const char *arg)
1530 {
1531   debug_prefix_map *map;
1532   const char *p;
1533
1534   p = strchr (arg, '=');
1535   if (!p)
1536     {
1537       error ("invalid argument %qs to -fdebug-prefix-map", arg);
1538       return;
1539     }
1540   map = XNEW (debug_prefix_map);
1541   map->old_prefix = xstrndup (arg, p - arg);
1542   map->old_len = p - arg;
1543   p++;
1544   map->new_prefix = xstrdup (p);
1545   map->new_len = strlen (p);
1546   map->next = debug_prefix_maps;
1547   debug_prefix_maps = map;
1548 }
1549
1550 /* Perform user-specified mapping of debug filename prefixes.  Return
1551    the new name corresponding to FILENAME.  */
1552
1553 const char *
1554 remap_debug_filename (const char *filename)
1555 {
1556   debug_prefix_map *map;
1557   char *s;
1558   const char *name;
1559   size_t name_len;
1560
1561   for (map = debug_prefix_maps; map; map = map->next)
1562     if (filename_ncmp (filename, map->old_prefix, map->old_len) == 0)
1563       break;
1564   if (!map)
1565     return filename;
1566   name = filename + map->old_len;
1567   name_len = strlen (name) + 1;
1568   s = (char *) alloca (name_len + map->new_len);
1569   memcpy (s, map->new_prefix, map->new_len);
1570   memcpy (s + map->new_len, name, name_len);
1571   return ggc_strdup (s);
1572 }
1573 \f
1574 /* Return true if DWARF2 debug info can be emitted for DECL.  */
1575
1576 static bool
1577 dwarf2_debug_info_emitted_p (tree decl)
1578 {
1579   if (write_symbols != DWARF2_DEBUG && write_symbols != VMS_AND_DWARF2_DEBUG)
1580     return false;
1581
1582   if (DECL_IGNORED_P (decl))
1583     return false;
1584
1585   return true;
1586 }
1587
1588 /* Return scope resulting from combination of S1 and S2.  */
1589 static tree
1590 choose_inner_scope (tree s1, tree s2)
1591 {
1592    if (!s1)
1593      return s2;
1594    if (!s2)
1595      return s1;
1596    if (BLOCK_NUMBER (s1) > BLOCK_NUMBER (s2))
1597      return s1;
1598    return s2;
1599 }
1600
1601 /* Emit lexical block notes needed to change scope from S1 to S2.  */
1602
1603 static void
1604 change_scope (rtx orig_insn, tree s1, tree s2)
1605 {
1606   rtx insn = orig_insn;
1607   tree com = NULL_TREE;
1608   tree ts1 = s1, ts2 = s2;
1609   tree s;
1610
1611   while (ts1 != ts2)
1612     {
1613       gcc_assert (ts1 && ts2);
1614       if (BLOCK_NUMBER (ts1) > BLOCK_NUMBER (ts2))
1615         ts1 = BLOCK_SUPERCONTEXT (ts1);
1616       else if (BLOCK_NUMBER (ts1) < BLOCK_NUMBER (ts2))
1617         ts2 = BLOCK_SUPERCONTEXT (ts2);
1618       else
1619         {
1620           ts1 = BLOCK_SUPERCONTEXT (ts1);
1621           ts2 = BLOCK_SUPERCONTEXT (ts2);
1622         }
1623     }
1624   com = ts1;
1625
1626   /* Close scopes.  */
1627   s = s1;
1628   while (s != com)
1629     {
1630       rtx note = emit_note_before (NOTE_INSN_BLOCK_END, insn);
1631       NOTE_BLOCK (note) = s;
1632       s = BLOCK_SUPERCONTEXT (s);
1633     }
1634
1635   /* Open scopes.  */
1636   s = s2;
1637   while (s != com)
1638     {
1639       insn = emit_note_before (NOTE_INSN_BLOCK_BEG, insn);
1640       NOTE_BLOCK (insn) = s;
1641       s = BLOCK_SUPERCONTEXT (s);
1642     }
1643 }
1644
1645 /* Rebuild all the NOTE_INSN_BLOCK_BEG and NOTE_INSN_BLOCK_END notes based
1646    on the scope tree and the newly reordered instructions.  */
1647
1648 static void
1649 reemit_insn_block_notes (void)
1650 {
1651   tree cur_block = DECL_INITIAL (cfun->decl);
1652   rtx insn, note;
1653
1654   insn = get_insns ();
1655   for (; insn; insn = NEXT_INSN (insn))
1656     {
1657       tree this_block;
1658
1659       /* Prevent lexical blocks from straddling section boundaries.  */
1660       if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
1661         {
1662           for (tree s = cur_block; s != DECL_INITIAL (cfun->decl);
1663                s = BLOCK_SUPERCONTEXT (s))
1664             {
1665               rtx note = emit_note_before (NOTE_INSN_BLOCK_END, insn);
1666               NOTE_BLOCK (note) = s;
1667               note = emit_note_after (NOTE_INSN_BLOCK_BEG, insn);
1668               NOTE_BLOCK (note) = s;
1669             }
1670         }
1671
1672       if (!active_insn_p (insn))
1673         continue;
1674
1675       /* Avoid putting scope notes between jump table and its label.  */
1676       if (JUMP_TABLE_DATA_P (insn))
1677         continue;
1678
1679       this_block = insn_scope (insn);
1680       /* For sequences compute scope resulting from merging all scopes
1681          of instructions nested inside.  */
1682       if (GET_CODE (PATTERN (insn)) == SEQUENCE)
1683         {
1684           int i;
1685           rtx body = PATTERN (insn);
1686
1687           this_block = NULL;
1688           for (i = 0; i < XVECLEN (body, 0); i++)
1689             this_block = choose_inner_scope (this_block,
1690                                              insn_scope (XVECEXP (body, 0, i)));
1691         }
1692       if (! this_block)
1693         {
1694           if (INSN_LOCATION (insn) == UNKNOWN_LOCATION)
1695             continue;
1696           else
1697             this_block = DECL_INITIAL (cfun->decl);
1698         }
1699
1700       if (this_block != cur_block)
1701         {
1702           change_scope (insn, cur_block, this_block);
1703           cur_block = this_block;
1704         }
1705     }
1706
1707   /* change_scope emits before the insn, not after.  */
1708   note = emit_note (NOTE_INSN_DELETED);
1709   change_scope (note, cur_block, DECL_INITIAL (cfun->decl));
1710   delete_insn (note);
1711
1712   reorder_blocks ();
1713 }
1714
1715 /* Output assembler code for the start of a function,
1716    and initialize some of the variables in this file
1717    for the new function.  The label for the function and associated
1718    assembler pseudo-ops have already been output in `assemble_start_function'.
1719
1720    FIRST is the first insn of the rtl for the function being compiled.
1721    FILE is the file to write assembler code to.
1722    OPTIMIZE_P is nonzero if we should eliminate redundant
1723      test and compare insns.  */
1724
1725 void
1726 final_start_function (rtx first, FILE *file,
1727                       int optimize_p ATTRIBUTE_UNUSED)
1728 {
1729   block_depth = 0;
1730
1731   this_is_asm_operands = 0;
1732
1733   need_profile_function = false;
1734
1735   last_filename = LOCATION_FILE (prologue_location);
1736   last_linenum = LOCATION_LINE (prologue_location);
1737   last_discriminator = discriminator = 0;
1738
1739   high_block_linenum = high_function_linenum = last_linenum;
1740
1741   if (!DECL_IGNORED_P (current_function_decl))
1742     debug_hooks->begin_prologue (last_linenum, last_filename);
1743
1744   if (!dwarf2_debug_info_emitted_p (current_function_decl))
1745     dwarf2out_begin_prologue (0, NULL);
1746
1747 #ifdef LEAF_REG_REMAP
1748   if (crtl->uses_only_leaf_regs)
1749     leaf_renumber_regs (first);
1750 #endif
1751
1752   /* The Sun386i and perhaps other machines don't work right
1753      if the profiling code comes after the prologue.  */
1754   if (targetm.profile_before_prologue () && crtl->profile)
1755     {
1756       if (targetm.asm_out.function_prologue
1757           == default_function_pro_epilogue
1758 #ifdef HAVE_prologue
1759           && HAVE_prologue
1760 #endif
1761          )
1762         {
1763           rtx insn;
1764           for (insn = first; insn; insn = NEXT_INSN (insn))
1765             if (!NOTE_P (insn))
1766               {
1767                 insn = NULL_RTX;
1768                 break;
1769               }
1770             else if (NOTE_KIND (insn) == NOTE_INSN_BASIC_BLOCK
1771                      || NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
1772               break;
1773             else if (NOTE_KIND (insn) == NOTE_INSN_DELETED
1774                      || NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION)
1775               continue;
1776             else
1777               {
1778                 insn = NULL_RTX;
1779                 break;
1780               }
1781
1782           if (insn)
1783             need_profile_function = true;
1784           else
1785             profile_function (file);
1786         }
1787       else
1788         profile_function (file);
1789     }
1790
1791   /* If debugging, assign block numbers to all of the blocks in this
1792      function.  */
1793   if (write_symbols)
1794     {
1795       reemit_insn_block_notes ();
1796       number_blocks (current_function_decl);
1797       /* We never actually put out begin/end notes for the top-level
1798          block in the function.  But, conceptually, that block is
1799          always needed.  */
1800       TREE_ASM_WRITTEN (DECL_INITIAL (current_function_decl)) = 1;
1801     }
1802
1803   if (warn_frame_larger_than
1804     && get_frame_size () > frame_larger_than_size)
1805   {
1806       /* Issue a warning */
1807       warning (OPT_Wframe_larger_than_,
1808                "the frame size of %wd bytes is larger than %wd bytes",
1809                get_frame_size (), frame_larger_than_size);
1810   }
1811
1812   /* First output the function prologue: code to set up the stack frame.  */
1813   targetm.asm_out.function_prologue (file, get_frame_size ());
1814
1815   /* If the machine represents the prologue as RTL, the profiling code must
1816      be emitted when NOTE_INSN_PROLOGUE_END is scanned.  */
1817 #ifdef HAVE_prologue
1818   if (! HAVE_prologue)
1819 #endif
1820     profile_after_prologue (file);
1821 }
1822
1823 static void
1824 profile_after_prologue (FILE *file ATTRIBUTE_UNUSED)
1825 {
1826   if (!targetm.profile_before_prologue () && crtl->profile)
1827     profile_function (file);
1828 }
1829
1830 static void
1831 profile_function (FILE *file ATTRIBUTE_UNUSED)
1832 {
1833 #ifndef NO_PROFILE_COUNTERS
1834 # define NO_PROFILE_COUNTERS    0
1835 #endif
1836 #ifdef ASM_OUTPUT_REG_PUSH
1837   rtx sval = NULL, chain = NULL;
1838
1839   if (cfun->returns_struct)
1840     sval = targetm.calls.struct_value_rtx (TREE_TYPE (current_function_decl),
1841                                            true);
1842   if (cfun->static_chain_decl)
1843     chain = targetm.calls.static_chain (current_function_decl, true);
1844 #endif /* ASM_OUTPUT_REG_PUSH */
1845
1846   if (! NO_PROFILE_COUNTERS)
1847     {
1848       int align = MIN (BIGGEST_ALIGNMENT, LONG_TYPE_SIZE);
1849       switch_to_section (data_section);
1850       ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
1851       targetm.asm_out.internal_label (file, "LP", current_function_funcdef_no);
1852       assemble_integer (const0_rtx, LONG_TYPE_SIZE / BITS_PER_UNIT, align, 1);
1853     }
1854
1855   switch_to_section (current_function_section ());
1856
1857 #ifdef ASM_OUTPUT_REG_PUSH
1858   if (sval && REG_P (sval))
1859     ASM_OUTPUT_REG_PUSH (file, REGNO (sval));
1860   if (chain && REG_P (chain))
1861     ASM_OUTPUT_REG_PUSH (file, REGNO (chain));
1862 #endif
1863
1864   FUNCTION_PROFILER (file, current_function_funcdef_no);
1865
1866 #ifdef ASM_OUTPUT_REG_PUSH
1867   if (chain && REG_P (chain))
1868     ASM_OUTPUT_REG_POP (file, REGNO (chain));
1869   if (sval && REG_P (sval))
1870     ASM_OUTPUT_REG_POP (file, REGNO (sval));
1871 #endif
1872 }
1873
1874 /* Output assembler code for the end of a function.
1875    For clarity, args are same as those of `final_start_function'
1876    even though not all of them are needed.  */
1877
1878 void
1879 final_end_function (void)
1880 {
1881   app_disable ();
1882
1883   if (!DECL_IGNORED_P (current_function_decl))
1884     debug_hooks->end_function (high_function_linenum);
1885
1886   /* Finally, output the function epilogue:
1887      code to restore the stack frame and return to the caller.  */
1888   targetm.asm_out.function_epilogue (asm_out_file, get_frame_size ());
1889
1890   /* And debug output.  */
1891   if (!DECL_IGNORED_P (current_function_decl))
1892     debug_hooks->end_epilogue (last_linenum, last_filename);
1893
1894   if (!dwarf2_debug_info_emitted_p (current_function_decl)
1895       && dwarf2out_do_frame ())
1896     dwarf2out_end_epilogue (last_linenum, last_filename);
1897 }
1898 \f
1899
1900 /* Dumper helper for basic block information. FILE is the assembly
1901    output file, and INSN is the instruction being emitted.  */
1902
1903 static void
1904 dump_basic_block_info (FILE *file, rtx insn, basic_block *start_to_bb,
1905                        basic_block *end_to_bb, int bb_map_size, int *bb_seqn)
1906 {
1907   basic_block bb;
1908
1909   if (!flag_debug_asm)
1910     return;
1911
1912   if (INSN_UID (insn) < bb_map_size
1913       && (bb = start_to_bb[INSN_UID (insn)]) != NULL)
1914     {
1915       edge e;
1916       edge_iterator ei;
1917
1918       fprintf (file, "%s BLOCK %d", ASM_COMMENT_START, bb->index);
1919       if (bb->frequency)
1920         fprintf (file, " freq:%d", bb->frequency);
1921       if (bb->count)
1922         fprintf (file, " count:" HOST_WIDEST_INT_PRINT_DEC,
1923                  bb->count);
1924       fprintf (file, " seq:%d", (*bb_seqn)++);
1925       fprintf (file, "\n%s PRED:", ASM_COMMENT_START);
1926       FOR_EACH_EDGE (e, ei, bb->preds)
1927         {
1928           dump_edge_info (file, e, TDF_DETAILS, 0);
1929         }
1930       fprintf (file, "\n");
1931     }
1932   if (INSN_UID (insn) < bb_map_size
1933       && (bb = end_to_bb[INSN_UID (insn)]) != NULL)
1934     {
1935       edge e;
1936       edge_iterator ei;
1937
1938       fprintf (asm_out_file, "%s SUCC:", ASM_COMMENT_START);
1939       FOR_EACH_EDGE (e, ei, bb->succs)
1940        {
1941          dump_edge_info (asm_out_file, e, TDF_DETAILS, 1);
1942        }
1943       fprintf (file, "\n");
1944     }
1945 }
1946
1947 /* Output assembler code for some insns: all or part of a function.
1948    For description of args, see `final_start_function', above.  */
1949
1950 void
1951 final (rtx first, FILE *file, int optimize_p)
1952 {
1953   rtx insn, next;
1954   int seen = 0;
1955
1956   /* Used for -dA dump.  */
1957   basic_block *start_to_bb = NULL;
1958   basic_block *end_to_bb = NULL;
1959   int bb_map_size = 0;
1960   int bb_seqn = 0;
1961
1962   last_ignored_compare = 0;
1963
1964 #ifdef HAVE_cc0
1965   for (insn = first; insn; insn = NEXT_INSN (insn))
1966     {
1967       /* If CC tracking across branches is enabled, record the insn which
1968          jumps to each branch only reached from one place.  */
1969       if (optimize_p && JUMP_P (insn))
1970         {
1971           rtx lab = JUMP_LABEL (insn);
1972           if (lab && LABEL_P (lab) && LABEL_NUSES (lab) == 1)
1973             {
1974               LABEL_REFS (lab) = insn;
1975             }
1976         }
1977     }
1978 #endif
1979
1980   init_recog ();
1981
1982   CC_STATUS_INIT;
1983
1984   if (flag_debug_asm)
1985     {
1986       basic_block bb;
1987
1988       bb_map_size = get_max_uid () + 1;
1989       start_to_bb = XCNEWVEC (basic_block, bb_map_size);
1990       end_to_bb = XCNEWVEC (basic_block, bb_map_size);
1991
1992       /* There is no cfg for a thunk.  */
1993       if (!cfun->is_thunk)
1994         FOR_EACH_BB_REVERSE (bb)
1995           {
1996             start_to_bb[INSN_UID (BB_HEAD (bb))] = bb;
1997             end_to_bb[INSN_UID (BB_END (bb))] = bb;
1998           }
1999     }
2000
2001   /* Output the insns.  */
2002   for (insn = first; insn;)
2003     {
2004       if (HAVE_ATTR_length)
2005         {
2006           if ((unsigned) INSN_UID (insn) >= INSN_ADDRESSES_SIZE ())
2007             {
2008               /* This can be triggered by bugs elsewhere in the compiler if
2009                  new insns are created after init_insn_lengths is called.  */
2010               gcc_assert (NOTE_P (insn));
2011               insn_current_address = -1;
2012             }
2013           else
2014             insn_current_address = INSN_ADDRESSES (INSN_UID (insn));
2015         }
2016
2017       dump_basic_block_info (file, insn, start_to_bb, end_to_bb,
2018                              bb_map_size, &bb_seqn);
2019       insn = final_scan_insn (insn, file, optimize_p, 0, &seen);
2020     }
2021
2022   if (flag_debug_asm)
2023     {
2024       free (start_to_bb);
2025       free (end_to_bb);
2026     }
2027
2028   /* Remove CFI notes, to avoid compare-debug failures.  */
2029   for (insn = first; insn; insn = next)
2030     {
2031       next = NEXT_INSN (insn);
2032       if (NOTE_P (insn)
2033           && (NOTE_KIND (insn) == NOTE_INSN_CFI
2034               || NOTE_KIND (insn) == NOTE_INSN_CFI_LABEL))
2035         delete_insn (insn);
2036     }
2037 }
2038 \f
2039 const char *
2040 get_insn_template (int code, rtx insn)
2041 {
2042   switch (insn_data[code].output_format)
2043     {
2044     case INSN_OUTPUT_FORMAT_SINGLE:
2045       return insn_data[code].output.single;
2046     case INSN_OUTPUT_FORMAT_MULTI:
2047       return insn_data[code].output.multi[which_alternative];
2048     case INSN_OUTPUT_FORMAT_FUNCTION:
2049       gcc_assert (insn);
2050       return (*insn_data[code].output.function) (recog_data.operand, insn);
2051
2052     default:
2053       gcc_unreachable ();
2054     }
2055 }
2056
2057 /* Emit the appropriate declaration for an alternate-entry-point
2058    symbol represented by INSN, to FILE.  INSN is a CODE_LABEL with
2059    LABEL_KIND != LABEL_NORMAL.
2060
2061    The case fall-through in this function is intentional.  */
2062 static void
2063 output_alternate_entry_point (FILE *file, rtx insn)
2064 {
2065   const char *name = LABEL_NAME (insn);
2066
2067   switch (LABEL_KIND (insn))
2068     {
2069     case LABEL_WEAK_ENTRY:
2070 #ifdef ASM_WEAKEN_LABEL
2071       ASM_WEAKEN_LABEL (file, name);
2072 #endif
2073     case LABEL_GLOBAL_ENTRY:
2074       targetm.asm_out.globalize_label (file, name);
2075     case LABEL_STATIC_ENTRY:
2076 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
2077       ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
2078 #endif
2079       ASM_OUTPUT_LABEL (file, name);
2080       break;
2081
2082     case LABEL_NORMAL:
2083     default:
2084       gcc_unreachable ();
2085     }
2086 }
2087
2088 /* Given a CALL_INSN, find and return the nested CALL. */
2089 static rtx
2090 call_from_call_insn (rtx insn)
2091 {
2092   rtx x;
2093   gcc_assert (CALL_P (insn));
2094   x = PATTERN (insn);
2095
2096   while (GET_CODE (x) != CALL)
2097     {
2098       switch (GET_CODE (x))
2099         {
2100         default:
2101           gcc_unreachable ();
2102         case COND_EXEC:
2103           x = COND_EXEC_CODE (x);
2104           break;
2105         case PARALLEL:
2106           x = XVECEXP (x, 0, 0);
2107           break;
2108         case SET:
2109           x = XEXP (x, 1);
2110           break;
2111         }
2112     }
2113   return x;
2114 }
2115
2116 /* The final scan for one insn, INSN.
2117    Args are same as in `final', except that INSN
2118    is the insn being scanned.
2119    Value returned is the next insn to be scanned.
2120
2121    NOPEEPHOLES is the flag to disallow peephole processing (currently
2122    used for within delayed branch sequence output).
2123
2124    SEEN is used to track the end of the prologue, for emitting
2125    debug information.  We force the emission of a line note after
2126    both NOTE_INSN_PROLOGUE_END and NOTE_INSN_FUNCTION_BEG, or
2127    at the beginning of the second basic block, whichever comes
2128    first.  */
2129
2130 rtx
2131 final_scan_insn (rtx insn, FILE *file, int optimize_p ATTRIBUTE_UNUSED,
2132                  int nopeepholes ATTRIBUTE_UNUSED, int *seen)
2133 {
2134 #ifdef HAVE_cc0
2135   rtx set;
2136 #endif
2137   rtx next;
2138
2139   insn_counter++;
2140
2141   /* Ignore deleted insns.  These can occur when we split insns (due to a
2142      template of "#") while not optimizing.  */
2143   if (INSN_DELETED_P (insn))
2144     return NEXT_INSN (insn);
2145
2146   switch (GET_CODE (insn))
2147     {
2148     case NOTE:
2149       switch (NOTE_KIND (insn))
2150         {
2151         case NOTE_INSN_DELETED:
2152           break;
2153
2154         case NOTE_INSN_SWITCH_TEXT_SECTIONS:
2155           in_cold_section_p = !in_cold_section_p;
2156
2157           if (dwarf2out_do_frame ())
2158             dwarf2out_switch_text_section ();
2159           else if (!DECL_IGNORED_P (current_function_decl))
2160             debug_hooks->switch_text_section ();
2161
2162           switch_to_section (current_function_section ());
2163           targetm.asm_out.function_switched_text_sections (asm_out_file,
2164                                                            current_function_decl,
2165                                                            in_cold_section_p);
2166           break;
2167
2168         case NOTE_INSN_BASIC_BLOCK:
2169           if (need_profile_function)
2170             {
2171               profile_function (asm_out_file);
2172               need_profile_function = false;
2173             }
2174
2175           if (targetm.asm_out.unwind_emit)
2176             targetm.asm_out.unwind_emit (asm_out_file, insn);
2177
2178           if ((*seen & (SEEN_EMITTED | SEEN_BB)) == SEEN_BB)
2179             {
2180               *seen |= SEEN_EMITTED;
2181               force_source_line = true;
2182             }
2183           else
2184             *seen |= SEEN_BB;
2185
2186           discriminator = NOTE_BASIC_BLOCK (insn)->discriminator;
2187
2188           break;
2189
2190         case NOTE_INSN_EH_REGION_BEG:
2191           ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LEHB",
2192                                   NOTE_EH_HANDLER (insn));
2193           break;
2194
2195         case NOTE_INSN_EH_REGION_END:
2196           ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LEHE",
2197                                   NOTE_EH_HANDLER (insn));
2198           break;
2199
2200         case NOTE_INSN_PROLOGUE_END:
2201           targetm.asm_out.function_end_prologue (file);
2202           profile_after_prologue (file);
2203
2204           if ((*seen & (SEEN_EMITTED | SEEN_NOTE)) == SEEN_NOTE)
2205             {
2206               *seen |= SEEN_EMITTED;
2207               force_source_line = true;
2208             }
2209           else
2210             *seen |= SEEN_NOTE;
2211
2212           break;
2213
2214         case NOTE_INSN_EPILOGUE_BEG:
2215           if (!DECL_IGNORED_P (current_function_decl))
2216             (*debug_hooks->begin_epilogue) (last_linenum, last_filename);
2217           targetm.asm_out.function_begin_epilogue (file);
2218           break;
2219
2220         case NOTE_INSN_CFI:
2221           dwarf2out_emit_cfi (NOTE_CFI (insn));
2222           break;
2223
2224         case NOTE_INSN_CFI_LABEL:
2225           ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI",
2226                                   NOTE_LABEL_NUMBER (insn));
2227           break;
2228
2229         case NOTE_INSN_FUNCTION_BEG:
2230           if (need_profile_function)
2231             {
2232               profile_function (asm_out_file);
2233               need_profile_function = false;
2234             }
2235
2236           app_disable ();
2237           if (!DECL_IGNORED_P (current_function_decl))
2238             debug_hooks->end_prologue (last_linenum, last_filename);
2239
2240           if ((*seen & (SEEN_EMITTED | SEEN_NOTE)) == SEEN_NOTE)
2241             {
2242               *seen |= SEEN_EMITTED;
2243               force_source_line = true;
2244             }
2245           else
2246             *seen |= SEEN_NOTE;
2247
2248           break;
2249
2250         case NOTE_INSN_BLOCK_BEG:
2251           if (debug_info_level == DINFO_LEVEL_NORMAL
2252               || debug_info_level == DINFO_LEVEL_VERBOSE
2253               || write_symbols == DWARF2_DEBUG
2254               || write_symbols == VMS_AND_DWARF2_DEBUG
2255               || write_symbols == VMS_DEBUG)
2256             {
2257               int n = BLOCK_NUMBER (NOTE_BLOCK (insn));
2258
2259               app_disable ();
2260               ++block_depth;
2261               high_block_linenum = last_linenum;
2262
2263               /* Output debugging info about the symbol-block beginning.  */
2264               if (!DECL_IGNORED_P (current_function_decl))
2265                 debug_hooks->begin_block (last_linenum, n);
2266
2267               /* Mark this block as output.  */
2268               TREE_ASM_WRITTEN (NOTE_BLOCK (insn)) = 1;
2269             }
2270           if (write_symbols == DBX_DEBUG
2271               || write_symbols == SDB_DEBUG)
2272             {
2273               location_t *locus_ptr
2274                 = block_nonartificial_location (NOTE_BLOCK (insn));
2275
2276               if (locus_ptr != NULL)
2277                 {
2278                   override_filename = LOCATION_FILE (*locus_ptr);
2279                   override_linenum = LOCATION_LINE (*locus_ptr);
2280                 }
2281             }
2282           break;
2283
2284         case NOTE_INSN_BLOCK_END:
2285           if (debug_info_level == DINFO_LEVEL_NORMAL
2286               || debug_info_level == DINFO_LEVEL_VERBOSE
2287               || write_symbols == DWARF2_DEBUG
2288               || write_symbols == VMS_AND_DWARF2_DEBUG
2289               || write_symbols == VMS_DEBUG)
2290             {
2291               int n = BLOCK_NUMBER (NOTE_BLOCK (insn));
2292
2293               app_disable ();
2294
2295               /* End of a symbol-block.  */
2296               --block_depth;
2297               gcc_assert (block_depth >= 0);
2298
2299               if (!DECL_IGNORED_P (current_function_decl))
2300                 debug_hooks->end_block (high_block_linenum, n);
2301             }
2302           if (write_symbols == DBX_DEBUG
2303               || write_symbols == SDB_DEBUG)
2304             {
2305               tree outer_block = BLOCK_SUPERCONTEXT (NOTE_BLOCK (insn));
2306               location_t *locus_ptr
2307                 = block_nonartificial_location (outer_block);
2308
2309               if (locus_ptr != NULL)
2310                 {
2311                   override_filename = LOCATION_FILE (*locus_ptr);
2312                   override_linenum = LOCATION_LINE (*locus_ptr);
2313                 }
2314               else
2315                 {
2316                   override_filename = NULL;
2317                   override_linenum = 0;
2318                 }
2319             }
2320           break;
2321
2322         case NOTE_INSN_DELETED_LABEL:
2323           /* Emit the label.  We may have deleted the CODE_LABEL because
2324              the label could be proved to be unreachable, though still
2325              referenced (in the form of having its address taken.  */
2326           ASM_OUTPUT_DEBUG_LABEL (file, "L", CODE_LABEL_NUMBER (insn));
2327           break;
2328
2329         case NOTE_INSN_DELETED_DEBUG_LABEL:
2330           /* Similarly, but need to use different namespace for it.  */
2331           if (CODE_LABEL_NUMBER (insn) != -1)
2332             ASM_OUTPUT_DEBUG_LABEL (file, "LDL", CODE_LABEL_NUMBER (insn));
2333           break;
2334
2335         case NOTE_INSN_VAR_LOCATION:
2336         case NOTE_INSN_CALL_ARG_LOCATION:
2337           if (!DECL_IGNORED_P (current_function_decl))
2338             debug_hooks->var_location (insn);
2339           break;
2340
2341         default:
2342           gcc_unreachable ();
2343           break;
2344         }
2345       break;
2346
2347     case BARRIER:
2348       break;
2349
2350     case CODE_LABEL:
2351       /* The target port might emit labels in the output function for
2352          some insn, e.g. sh.c output_branchy_insn.  */
2353       if (CODE_LABEL_NUMBER (insn) <= max_labelno)
2354         {
2355           int align = LABEL_TO_ALIGNMENT (insn);
2356 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
2357           int max_skip = LABEL_TO_MAX_SKIP (insn);
2358 #endif
2359
2360           if (align && NEXT_INSN (insn))
2361             {
2362 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
2363               ASM_OUTPUT_MAX_SKIP_ALIGN (file, align, max_skip);
2364 #else
2365 #ifdef ASM_OUTPUT_ALIGN_WITH_NOP
2366               ASM_OUTPUT_ALIGN_WITH_NOP (file, align);
2367 #else
2368               ASM_OUTPUT_ALIGN (file, align);
2369 #endif
2370 #endif
2371             }
2372         }
2373       CC_STATUS_INIT;
2374
2375       if (!DECL_IGNORED_P (current_function_decl) && LABEL_NAME (insn))
2376         debug_hooks->label (insn);
2377
2378       app_disable ();
2379
2380       next = next_nonnote_insn (insn);
2381       /* If this label is followed by a jump-table, make sure we put
2382          the label in the read-only section.  Also possibly write the
2383          label and jump table together.  */
2384       if (next != 0 && JUMP_TABLE_DATA_P (next))
2385         {
2386 #if defined(ASM_OUTPUT_ADDR_VEC) || defined(ASM_OUTPUT_ADDR_DIFF_VEC)
2387           /* In this case, the case vector is being moved by the
2388              target, so don't output the label at all.  Leave that
2389              to the back end macros.  */
2390 #else
2391           if (! JUMP_TABLES_IN_TEXT_SECTION)
2392             {
2393               int log_align;
2394
2395               switch_to_section (targetm.asm_out.function_rodata_section
2396                                  (current_function_decl));
2397
2398 #ifdef ADDR_VEC_ALIGN
2399               log_align = ADDR_VEC_ALIGN (next);
2400 #else
2401               log_align = exact_log2 (BIGGEST_ALIGNMENT / BITS_PER_UNIT);
2402 #endif
2403               ASM_OUTPUT_ALIGN (file, log_align);
2404             }
2405           else
2406             switch_to_section (current_function_section ());
2407
2408 #ifdef ASM_OUTPUT_CASE_LABEL
2409           ASM_OUTPUT_CASE_LABEL (file, "L", CODE_LABEL_NUMBER (insn),
2410                                  next);
2411 #else
2412           targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (insn));
2413 #endif
2414 #endif
2415           break;
2416         }
2417       if (LABEL_ALT_ENTRY_P (insn))
2418         output_alternate_entry_point (file, insn);
2419       else
2420         targetm.asm_out.internal_label (file, "L", CODE_LABEL_NUMBER (insn));
2421       break;
2422
2423     default:
2424       {
2425         rtx body = PATTERN (insn);
2426         int insn_code_number;
2427         const char *templ;
2428         bool is_stmt;
2429
2430         /* Reset this early so it is correct for ASM statements.  */
2431         current_insn_predicate = NULL_RTX;
2432
2433         /* An INSN, JUMP_INSN or CALL_INSN.
2434            First check for special kinds that recog doesn't recognize.  */
2435
2436         if (GET_CODE (body) == USE /* These are just declarations.  */
2437             || GET_CODE (body) == CLOBBER)
2438           break;
2439
2440 #ifdef HAVE_cc0
2441         {
2442           /* If there is a REG_CC_SETTER note on this insn, it means that
2443              the setting of the condition code was done in the delay slot
2444              of the insn that branched here.  So recover the cc status
2445              from the insn that set it.  */
2446
2447           rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
2448           if (note)
2449             {
2450               NOTICE_UPDATE_CC (PATTERN (XEXP (note, 0)), XEXP (note, 0));
2451               cc_prev_status = cc_status;
2452             }
2453         }
2454 #endif
2455
2456         /* Detect insns that are really jump-tables
2457            and output them as such.  */
2458
2459         if (JUMP_TABLE_DATA_P (insn))
2460           {
2461 #if !(defined(ASM_OUTPUT_ADDR_VEC) || defined(ASM_OUTPUT_ADDR_DIFF_VEC))
2462             int vlen, idx;
2463 #endif
2464
2465             if (! JUMP_TABLES_IN_TEXT_SECTION)
2466               switch_to_section (targetm.asm_out.function_rodata_section
2467                                  (current_function_decl));
2468             else
2469               switch_to_section (current_function_section ());
2470
2471             app_disable ();
2472
2473 #if defined(ASM_OUTPUT_ADDR_VEC) || defined(ASM_OUTPUT_ADDR_DIFF_VEC)
2474             if (GET_CODE (body) == ADDR_VEC)
2475               {
2476 #ifdef ASM_OUTPUT_ADDR_VEC
2477                 ASM_OUTPUT_ADDR_VEC (PREV_INSN (insn), body);
2478 #else
2479                 gcc_unreachable ();
2480 #endif
2481               }
2482             else
2483               {
2484 #ifdef ASM_OUTPUT_ADDR_DIFF_VEC
2485                 ASM_OUTPUT_ADDR_DIFF_VEC (PREV_INSN (insn), body);
2486 #else
2487                 gcc_unreachable ();
2488 #endif
2489               }
2490 #else
2491             vlen = XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC);
2492             for (idx = 0; idx < vlen; idx++)
2493               {
2494                 if (GET_CODE (body) == ADDR_VEC)
2495                   {
2496 #ifdef ASM_OUTPUT_ADDR_VEC_ELT
2497                     ASM_OUTPUT_ADDR_VEC_ELT
2498                       (file, CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 0, idx), 0)));
2499 #else
2500                     gcc_unreachable ();
2501 #endif
2502                   }
2503                 else
2504                   {
2505 #ifdef ASM_OUTPUT_ADDR_DIFF_ELT
2506                     ASM_OUTPUT_ADDR_DIFF_ELT
2507                       (file,
2508                        body,
2509                        CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 1, idx), 0)),
2510                        CODE_LABEL_NUMBER (XEXP (XEXP (body, 0), 0)));
2511 #else
2512                     gcc_unreachable ();
2513 #endif
2514                   }
2515               }
2516 #ifdef ASM_OUTPUT_CASE_END
2517             ASM_OUTPUT_CASE_END (file,
2518                                  CODE_LABEL_NUMBER (PREV_INSN (insn)),
2519                                  insn);
2520 #endif
2521 #endif
2522
2523             switch_to_section (current_function_section ());
2524
2525             break;
2526           }
2527         /* Output this line note if it is the first or the last line
2528            note in a row.  */
2529         if (!DECL_IGNORED_P (current_function_decl)
2530             && notice_source_line (insn, &is_stmt))
2531           (*debug_hooks->source_line) (last_linenum, last_filename,
2532                                        last_discriminator, is_stmt);
2533
2534         if (GET_CODE (body) == ASM_INPUT)
2535           {
2536             const char *string = XSTR (body, 0);
2537
2538             /* There's no telling what that did to the condition codes.  */
2539             CC_STATUS_INIT;
2540
2541             if (string[0])
2542               {
2543                 expanded_location loc;
2544
2545                 app_enable ();
2546                 loc = expand_location (ASM_INPUT_SOURCE_LOCATION (body));
2547                 if (*loc.file && loc.line)
2548                   fprintf (asm_out_file, "%s %i \"%s\" 1\n",
2549                            ASM_COMMENT_START, loc.line, loc.file);
2550                 fprintf (asm_out_file, "\t%s\n", string);
2551 #if HAVE_AS_LINE_ZERO
2552                 if (*loc.file && loc.line)
2553                   fprintf (asm_out_file, "%s 0 \"\" 2\n", ASM_COMMENT_START);
2554 #endif
2555               }
2556             break;
2557           }
2558
2559         /* Detect `asm' construct with operands.  */
2560         if (asm_noperands (body) >= 0)
2561           {
2562             unsigned int noperands = asm_noperands (body);
2563             rtx *ops = XALLOCAVEC (rtx, noperands);
2564             const char *string;
2565             location_t loc;
2566             expanded_location expanded;
2567
2568             /* There's no telling what that did to the condition codes.  */
2569             CC_STATUS_INIT;
2570
2571             /* Get out the operand values.  */
2572             string = decode_asm_operands (body, ops, NULL, NULL, NULL, &loc);
2573             /* Inhibit dying on what would otherwise be compiler bugs.  */
2574             insn_noperands = noperands;
2575             this_is_asm_operands = insn;
2576             expanded = expand_location (loc);
2577
2578 #ifdef FINAL_PRESCAN_INSN
2579             FINAL_PRESCAN_INSN (insn, ops, insn_noperands);
2580 #endif
2581
2582             /* Output the insn using them.  */
2583             if (string[0])
2584               {
2585                 app_enable ();
2586                 if (expanded.file && expanded.line)
2587                   fprintf (asm_out_file, "%s %i \"%s\" 1\n",
2588                            ASM_COMMENT_START, expanded.line, expanded.file);
2589                 output_asm_insn (string, ops);
2590 #if HAVE_AS_LINE_ZERO
2591                 if (expanded.file && expanded.line)
2592                   fprintf (asm_out_file, "%s 0 \"\" 2\n", ASM_COMMENT_START);
2593 #endif
2594               }
2595
2596             if (targetm.asm_out.final_postscan_insn)
2597               targetm.asm_out.final_postscan_insn (file, insn, ops,
2598                                                    insn_noperands);
2599
2600             this_is_asm_operands = 0;
2601             break;
2602           }
2603
2604         app_disable ();
2605
2606         if (GET_CODE (body) == SEQUENCE)
2607           {
2608             /* A delayed-branch sequence */
2609             int i;
2610
2611             final_sequence = body;
2612
2613             /* The first insn in this SEQUENCE might be a JUMP_INSN that will
2614                force the restoration of a comparison that was previously
2615                thought unnecessary.  If that happens, cancel this sequence
2616                and cause that insn to be restored.  */
2617
2618             next = final_scan_insn (XVECEXP (body, 0, 0), file, 0, 1, seen);
2619             if (next != XVECEXP (body, 0, 1))
2620               {
2621                 final_sequence = 0;
2622                 return next;
2623               }
2624
2625             for (i = 1; i < XVECLEN (body, 0); i++)
2626               {
2627                 rtx insn = XVECEXP (body, 0, i);
2628                 rtx next = NEXT_INSN (insn);
2629                 /* We loop in case any instruction in a delay slot gets
2630                    split.  */
2631                 do
2632                   insn = final_scan_insn (insn, file, 0, 1, seen);
2633                 while (insn != next);
2634               }
2635 #ifdef DBR_OUTPUT_SEQEND
2636             DBR_OUTPUT_SEQEND (file);
2637 #endif
2638             final_sequence = 0;
2639
2640             /* If the insn requiring the delay slot was a CALL_INSN, the
2641                insns in the delay slot are actually executed before the
2642                called function.  Hence we don't preserve any CC-setting
2643                actions in these insns and the CC must be marked as being
2644                clobbered by the function.  */
2645             if (CALL_P (XVECEXP (body, 0, 0)))
2646               {
2647                 CC_STATUS_INIT;
2648               }
2649             break;
2650           }
2651
2652         /* We have a real machine instruction as rtl.  */
2653
2654         body = PATTERN (insn);
2655
2656 #ifdef HAVE_cc0
2657         set = single_set (insn);
2658
2659         /* Check for redundant test and compare instructions
2660            (when the condition codes are already set up as desired).
2661            This is done only when optimizing; if not optimizing,
2662            it should be possible for the user to alter a variable
2663            with the debugger in between statements
2664            and the next statement should reexamine the variable
2665            to compute the condition codes.  */
2666
2667         if (optimize_p)
2668           {
2669             if (set
2670                 && GET_CODE (SET_DEST (set)) == CC0
2671                 && insn != last_ignored_compare)
2672               {
2673                 rtx src1, src2;
2674                 if (GET_CODE (SET_SRC (set)) == SUBREG)
2675                   SET_SRC (set) = alter_subreg (&SET_SRC (set), true);
2676
2677                 src1 = SET_SRC (set);
2678                 src2 = NULL_RTX;
2679                 if (GET_CODE (SET_SRC (set)) == COMPARE)
2680                   {
2681                     if (GET_CODE (XEXP (SET_SRC (set), 0)) == SUBREG)
2682                       XEXP (SET_SRC (set), 0)
2683                         = alter_subreg (&XEXP (SET_SRC (set), 0), true);
2684                     if (GET_CODE (XEXP (SET_SRC (set), 1)) == SUBREG)
2685                       XEXP (SET_SRC (set), 1)
2686                         = alter_subreg (&XEXP (SET_SRC (set), 1), true);
2687                     if (XEXP (SET_SRC (set), 1)
2688                         == CONST0_RTX (GET_MODE (XEXP (SET_SRC (set), 0))))
2689                       src2 = XEXP (SET_SRC (set), 0);
2690                   }
2691                 if ((cc_status.value1 != 0
2692                      && rtx_equal_p (src1, cc_status.value1))
2693                     || (cc_status.value2 != 0
2694                         && rtx_equal_p (src1, cc_status.value2))
2695                     || (src2 != 0 && cc_status.value1 != 0
2696                         && rtx_equal_p (src2, cc_status.value1))
2697                     || (src2 != 0 && cc_status.value2 != 0
2698                         && rtx_equal_p (src2, cc_status.value2)))
2699                   {
2700                     /* Don't delete insn if it has an addressing side-effect.  */
2701                     if (! FIND_REG_INC_NOTE (insn, NULL_RTX)
2702                         /* or if anything in it is volatile.  */
2703                         && ! volatile_refs_p (PATTERN (insn)))
2704                       {
2705                         /* We don't really delete the insn; just ignore it.  */
2706                         last_ignored_compare = insn;
2707                         break;
2708                       }
2709                   }
2710               }
2711           }
2712
2713         /* If this is a conditional branch, maybe modify it
2714            if the cc's are in a nonstandard state
2715            so that it accomplishes the same thing that it would
2716            do straightforwardly if the cc's were set up normally.  */
2717
2718         if (cc_status.flags != 0
2719             && JUMP_P (insn)
2720             && GET_CODE (body) == SET
2721             && SET_DEST (body) == pc_rtx
2722             && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE
2723             && COMPARISON_P (XEXP (SET_SRC (body), 0))
2724             && XEXP (XEXP (SET_SRC (body), 0), 0) == cc0_rtx)
2725           {
2726             /* This function may alter the contents of its argument
2727                and clear some of the cc_status.flags bits.
2728                It may also return 1 meaning condition now always true
2729                or -1 meaning condition now always false
2730                or 2 meaning condition nontrivial but altered.  */
2731             int result = alter_cond (XEXP (SET_SRC (body), 0));
2732             /* If condition now has fixed value, replace the IF_THEN_ELSE
2733                with its then-operand or its else-operand.  */
2734             if (result == 1)
2735               SET_SRC (body) = XEXP (SET_SRC (body), 1);
2736             if (result == -1)
2737               SET_SRC (body) = XEXP (SET_SRC (body), 2);
2738
2739             /* The jump is now either unconditional or a no-op.
2740                If it has become a no-op, don't try to output it.
2741                (It would not be recognized.)  */
2742             if (SET_SRC (body) == pc_rtx)
2743               {
2744                 delete_insn (insn);
2745                 break;
2746               }
2747             else if (ANY_RETURN_P (SET_SRC (body)))
2748               /* Replace (set (pc) (return)) with (return).  */
2749               PATTERN (insn) = body = SET_SRC (body);
2750
2751             /* Rerecognize the instruction if it has changed.  */
2752             if (result != 0)
2753               INSN_CODE (insn) = -1;
2754           }
2755
2756         /* If this is a conditional trap, maybe modify it if the cc's
2757            are in a nonstandard state so that it accomplishes the same
2758            thing that it would do straightforwardly if the cc's were
2759            set up normally.  */
2760         if (cc_status.flags != 0
2761             && NONJUMP_INSN_P (insn)
2762             && GET_CODE (body) == TRAP_IF
2763             && COMPARISON_P (TRAP_CONDITION (body))
2764             && XEXP (TRAP_CONDITION (body), 0) == cc0_rtx)
2765           {
2766             /* This function may alter the contents of its argument
2767                and clear some of the cc_status.flags bits.
2768                It may also return 1 meaning condition now always true
2769                or -1 meaning condition now always false
2770                or 2 meaning condition nontrivial but altered.  */
2771             int result = alter_cond (TRAP_CONDITION (body));
2772
2773             /* If TRAP_CONDITION has become always false, delete the
2774                instruction.  */
2775             if (result == -1)
2776               {
2777                 delete_insn (insn);
2778                 break;
2779               }
2780
2781             /* If TRAP_CONDITION has become always true, replace
2782                TRAP_CONDITION with const_true_rtx.  */
2783             if (result == 1)
2784               TRAP_CONDITION (body) = const_true_rtx;
2785
2786             /* Rerecognize the instruction if it has changed.  */
2787             if (result != 0)
2788               INSN_CODE (insn) = -1;
2789           }
2790
2791         /* Make same adjustments to instructions that examine the
2792            condition codes without jumping and instructions that
2793            handle conditional moves (if this machine has either one).  */
2794
2795         if (cc_status.flags != 0
2796             && set != 0)
2797           {
2798             rtx cond_rtx, then_rtx, else_rtx;
2799
2800             if (!JUMP_P (insn)
2801                 && GET_CODE (SET_SRC (set)) == IF_THEN_ELSE)
2802               {
2803                 cond_rtx = XEXP (SET_SRC (set), 0);
2804                 then_rtx = XEXP (SET_SRC (set), 1);
2805                 else_rtx = XEXP (SET_SRC (set), 2);
2806               }
2807             else
2808               {
2809                 cond_rtx = SET_SRC (set);
2810                 then_rtx = const_true_rtx;
2811                 else_rtx = const0_rtx;
2812               }
2813
2814             if (COMPARISON_P (cond_rtx)
2815                 && XEXP (cond_rtx, 0) == cc0_rtx)
2816               {
2817                 int result;
2818                 result = alter_cond (cond_rtx);
2819                 if (result == 1)
2820                   validate_change (insn, &SET_SRC (set), then_rtx, 0);
2821                 else if (result == -1)
2822                   validate_change (insn, &SET_SRC (set), else_rtx, 0);
2823                 else if (result == 2)
2824                   INSN_CODE (insn) = -1;
2825                 if (SET_DEST (set) == SET_SRC (set))
2826                   delete_insn (insn);
2827               }
2828           }
2829
2830 #endif
2831
2832 #ifdef HAVE_peephole
2833         /* Do machine-specific peephole optimizations if desired.  */
2834
2835         if (optimize_p && !flag_no_peephole && !nopeepholes)
2836           {
2837             rtx next = peephole (insn);
2838             /* When peepholing, if there were notes within the peephole,
2839                emit them before the peephole.  */
2840             if (next != 0 && next != NEXT_INSN (insn))
2841               {
2842                 rtx note, prev = PREV_INSN (insn);
2843
2844                 for (note = NEXT_INSN (insn); note != next;
2845                      note = NEXT_INSN (note))
2846                   final_scan_insn (note, file, optimize_p, nopeepholes, seen);
2847
2848                 /* Put the notes in the proper position for a later
2849                    rescan.  For example, the SH target can do this
2850                    when generating a far jump in a delayed branch
2851                    sequence.  */
2852                 note = NEXT_INSN (insn);
2853                 PREV_INSN (note) = prev;
2854                 NEXT_INSN (prev) = note;
2855                 NEXT_INSN (PREV_INSN (next)) = insn;
2856                 PREV_INSN (insn) = PREV_INSN (next);
2857                 NEXT_INSN (insn) = next;
2858                 PREV_INSN (next) = insn;
2859               }
2860
2861             /* PEEPHOLE might have changed this.  */
2862             body = PATTERN (insn);
2863           }
2864 #endif
2865
2866         /* Try to recognize the instruction.
2867            If successful, verify that the operands satisfy the
2868            constraints for the instruction.  Crash if they don't,
2869            since `reload' should have changed them so that they do.  */
2870
2871         insn_code_number = recog_memoized (insn);
2872         cleanup_subreg_operands (insn);
2873
2874         /* Dump the insn in the assembly for debugging (-dAP).
2875            If the final dump is requested as slim RTL, dump slim
2876            RTL to the assembly file also.  */
2877         if (flag_dump_rtl_in_asm)
2878           {
2879             print_rtx_head = ASM_COMMENT_START;
2880             if (! (dump_flags & TDF_SLIM))
2881               print_rtl_single (asm_out_file, insn);
2882             else
2883               dump_insn_slim (asm_out_file, insn);
2884             print_rtx_head = "";
2885           }
2886
2887         if (! constrain_operands_cached (1))
2888           fatal_insn_not_found (insn);
2889
2890         /* Some target machines need to prescan each insn before
2891            it is output.  */
2892
2893 #ifdef FINAL_PRESCAN_INSN
2894         FINAL_PRESCAN_INSN (insn, recog_data.operand, recog_data.n_operands);
2895 #endif
2896
2897         if (targetm.have_conditional_execution ()
2898             && GET_CODE (PATTERN (insn)) == COND_EXEC)
2899           current_insn_predicate = COND_EXEC_TEST (PATTERN (insn));
2900
2901 #ifdef HAVE_cc0
2902         cc_prev_status = cc_status;
2903
2904         /* Update `cc_status' for this instruction.
2905            The instruction's output routine may change it further.
2906            If the output routine for a jump insn needs to depend
2907            on the cc status, it should look at cc_prev_status.  */
2908
2909         NOTICE_UPDATE_CC (body, insn);
2910 #endif
2911
2912         current_output_insn = debug_insn = insn;
2913
2914         /* Find the proper template for this insn.  */
2915         templ = get_insn_template (insn_code_number, insn);
2916
2917         /* If the C code returns 0, it means that it is a jump insn
2918            which follows a deleted test insn, and that test insn
2919            needs to be reinserted.  */
2920         if (templ == 0)
2921           {
2922             rtx prev;
2923
2924             gcc_assert (prev_nonnote_insn (insn) == last_ignored_compare);
2925
2926             /* We have already processed the notes between the setter and
2927                the user.  Make sure we don't process them again, this is
2928                particularly important if one of the notes is a block
2929                scope note or an EH note.  */
2930             for (prev = insn;
2931                  prev != last_ignored_compare;
2932                  prev = PREV_INSN (prev))
2933               {
2934                 if (NOTE_P (prev))
2935                   delete_insn (prev);   /* Use delete_note.  */
2936               }
2937
2938             return prev;
2939           }
2940
2941         /* If the template is the string "#", it means that this insn must
2942            be split.  */
2943         if (templ[0] == '#' && templ[1] == '\0')
2944           {
2945             rtx new_rtx = try_split (body, insn, 0);
2946
2947             /* If we didn't split the insn, go away.  */
2948             if (new_rtx == insn && PATTERN (new_rtx) == body)
2949               fatal_insn ("could not split insn", insn);
2950
2951             /* If we have a length attribute, this instruction should have
2952                been split in shorten_branches, to ensure that we would have
2953                valid length info for the splitees.  */
2954             gcc_assert (!HAVE_ATTR_length);
2955
2956             return new_rtx;
2957           }
2958
2959         /* ??? This will put the directives in the wrong place if
2960            get_insn_template outputs assembly directly.  However calling it
2961            before get_insn_template breaks if the insns is split.  */
2962         if (targetm.asm_out.unwind_emit_before_insn
2963             && targetm.asm_out.unwind_emit)
2964           targetm.asm_out.unwind_emit (asm_out_file, insn);
2965
2966         if (CALL_P (insn))
2967           {
2968             rtx x = call_from_call_insn (insn);
2969             x = XEXP (x, 0);
2970             if (x && MEM_P (x) && GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2971               {
2972                 tree t;
2973                 x = XEXP (x, 0);
2974                 t = SYMBOL_REF_DECL (x);
2975                 if (t)
2976                   assemble_external (t);
2977               }
2978             if (!DECL_IGNORED_P (current_function_decl))
2979               debug_hooks->var_location (insn);
2980           }
2981
2982         /* Output assembler code from the template.  */
2983         output_asm_insn (templ, recog_data.operand);
2984
2985         /* Some target machines need to postscan each insn after
2986            it is output.  */
2987         if (targetm.asm_out.final_postscan_insn)
2988           targetm.asm_out.final_postscan_insn (file, insn, recog_data.operand,
2989                                                recog_data.n_operands);
2990
2991         if (!targetm.asm_out.unwind_emit_before_insn
2992             && targetm.asm_out.unwind_emit)
2993           targetm.asm_out.unwind_emit (asm_out_file, insn);
2994
2995         current_output_insn = debug_insn = 0;
2996       }
2997     }
2998   return NEXT_INSN (insn);
2999 }
3000 \f
3001 /* Return whether a source line note needs to be emitted before INSN.
3002    Sets IS_STMT to TRUE if the line should be marked as a possible
3003    breakpoint location.  */
3004
3005 static bool
3006 notice_source_line (rtx insn, bool *is_stmt)
3007 {
3008   const char *filename;
3009   int linenum;
3010
3011   if (override_filename)
3012     {
3013       filename = override_filename;
3014       linenum = override_linenum;
3015     }
3016   else
3017     {
3018       filename = insn_file (insn);
3019       linenum = insn_line (insn);
3020     }
3021
3022   if (filename == NULL)
3023     return false;
3024
3025   if (force_source_line
3026       || filename != last_filename
3027       || last_linenum != linenum)
3028     {
3029       force_source_line = false;
3030       last_filename = filename;
3031       last_linenum = linenum;
3032       last_discriminator = discriminator;
3033       *is_stmt = true;
3034       high_block_linenum = MAX (last_linenum, high_block_linenum);
3035       high_function_linenum = MAX (last_linenum, high_function_linenum);
3036       return true;
3037     }
3038
3039   if (SUPPORTS_DISCRIMINATOR && last_discriminator != discriminator)
3040     {
3041       /* If the discriminator changed, but the line number did not,
3042          output the line table entry with is_stmt false so the
3043          debugger does not treat this as a breakpoint location.  */
3044       last_discriminator = discriminator;
3045       *is_stmt = false;
3046       return true;
3047     }
3048
3049   return false;
3050 }
3051 \f
3052 /* For each operand in INSN, simplify (subreg (reg)) so that it refers
3053    directly to the desired hard register.  */
3054
3055 void
3056 cleanup_subreg_operands (rtx insn)
3057 {
3058   int i;
3059   bool changed = false;
3060   extract_insn_cached (insn);
3061   for (i = 0; i < recog_data.n_operands; i++)
3062     {
3063       /* The following test cannot use recog_data.operand when testing
3064          for a SUBREG: the underlying object might have been changed
3065          already if we are inside a match_operator expression that
3066          matches the else clause.  Instead we test the underlying
3067          expression directly.  */
3068       if (GET_CODE (*recog_data.operand_loc[i]) == SUBREG)
3069         {
3070           recog_data.operand[i] = alter_subreg (recog_data.operand_loc[i], true);
3071           changed = true;
3072         }
3073       else if (GET_CODE (recog_data.operand[i]) == PLUS
3074                || GET_CODE (recog_data.operand[i]) == MULT
3075                || MEM_P (recog_data.operand[i]))
3076         recog_data.operand[i] = walk_alter_subreg (recog_data.operand_loc[i], &changed);
3077     }
3078
3079   for (i = 0; i < recog_data.n_dups; i++)
3080     {
3081       if (GET_CODE (*recog_data.dup_loc[i]) == SUBREG)
3082         {
3083           *recog_data.dup_loc[i] = alter_subreg (recog_data.dup_loc[i], true);
3084           changed = true;
3085         }
3086       else if (GET_CODE (*recog_data.dup_loc[i]) == PLUS
3087                || GET_CODE (*recog_data.dup_loc[i]) == MULT
3088                || MEM_P (*recog_data.dup_loc[i]))
3089         *recog_data.dup_loc[i] = walk_alter_subreg (recog_data.dup_loc[i], &changed);
3090     }
3091   if (changed)
3092     df_insn_rescan (insn);
3093 }
3094
3095 /* If X is a SUBREG, try to replace it with a REG or a MEM, based on
3096    the thing it is a subreg of.  Do it anyway if FINAL_P.  */
3097
3098 rtx
3099 alter_subreg (rtx *xp, bool final_p)
3100 {
3101   rtx x = *xp;
3102   rtx y = SUBREG_REG (x);
3103
3104   /* simplify_subreg does not remove subreg from volatile references.
3105      We are required to.  */
3106   if (MEM_P (y))
3107     {
3108       int offset = SUBREG_BYTE (x);
3109
3110       /* For paradoxical subregs on big-endian machines, SUBREG_BYTE
3111          contains 0 instead of the proper offset.  See simplify_subreg.  */
3112       if (offset == 0
3113           && GET_MODE_SIZE (GET_MODE (y)) < GET_MODE_SIZE (GET_MODE (x)))
3114         {
3115           int difference = GET_MODE_SIZE (GET_MODE (y))
3116                            - GET_MODE_SIZE (GET_MODE (x));
3117           if (WORDS_BIG_ENDIAN)
3118             offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3119           if (BYTES_BIG_ENDIAN)
3120             offset += difference % UNITS_PER_WORD;
3121         }
3122
3123       if (final_p)
3124         *xp = adjust_address (y, GET_MODE (x), offset);
3125       else
3126         *xp = adjust_address_nv (y, GET_MODE (x), offset);
3127     }
3128   else
3129     {
3130       rtx new_rtx = simplify_subreg (GET_MODE (x), y, GET_MODE (y),
3131                                      SUBREG_BYTE (x));
3132
3133       if (new_rtx != 0)
3134         *xp = new_rtx;
3135       else if (final_p && REG_P (y))
3136         {
3137           /* Simplify_subreg can't handle some REG cases, but we have to.  */
3138           unsigned int regno;
3139           HOST_WIDE_INT offset;
3140
3141           regno = subreg_regno (x);
3142           if (subreg_lowpart_p (x))
3143             offset = byte_lowpart_offset (GET_MODE (x), GET_MODE (y));
3144           else
3145             offset = SUBREG_BYTE (x);
3146           *xp = gen_rtx_REG_offset (y, GET_MODE (x), regno, offset);
3147         }
3148     }
3149
3150   return *xp;
3151 }
3152
3153 /* Do alter_subreg on all the SUBREGs contained in X.  */
3154
3155 static rtx
3156 walk_alter_subreg (rtx *xp, bool *changed)
3157 {
3158   rtx x = *xp;
3159   switch (GET_CODE (x))
3160     {
3161     case PLUS:
3162     case MULT:
3163     case AND:
3164       XEXP (x, 0) = walk_alter_subreg (&XEXP (x, 0), changed);
3165       XEXP (x, 1) = walk_alter_subreg (&XEXP (x, 1), changed);
3166       break;
3167
3168     case MEM:
3169     case ZERO_EXTEND:
3170       XEXP (x, 0) = walk_alter_subreg (&XEXP (x, 0), changed);
3171       break;
3172
3173     case SUBREG:
3174       *changed = true;
3175       return alter_subreg (xp, true);
3176
3177     default:
3178       break;
3179     }
3180
3181   return *xp;
3182 }
3183 \f
3184 #ifdef HAVE_cc0
3185
3186 /* Given BODY, the body of a jump instruction, alter the jump condition
3187    as required by the bits that are set in cc_status.flags.
3188    Not all of the bits there can be handled at this level in all cases.
3189
3190    The value is normally 0.
3191    1 means that the condition has become always true.
3192    -1 means that the condition has become always false.
3193    2 means that COND has been altered.  */
3194
3195 static int
3196 alter_cond (rtx cond)
3197 {
3198   int value = 0;
3199
3200   if (cc_status.flags & CC_REVERSED)
3201     {
3202       value = 2;
3203       PUT_CODE (cond, swap_condition (GET_CODE (cond)));
3204     }
3205
3206   if (cc_status.flags & CC_INVERTED)
3207     {
3208       value = 2;
3209       PUT_CODE (cond, reverse_condition (GET_CODE (cond)));
3210     }
3211
3212   if (cc_status.flags & CC_NOT_POSITIVE)
3213     switch (GET_CODE (cond))
3214       {
3215       case LE:
3216       case LEU:
3217       case GEU:
3218         /* Jump becomes unconditional.  */
3219         return 1;
3220
3221       case GT:
3222       case GTU:
3223       case LTU:
3224         /* Jump becomes no-op.  */
3225         return -1;
3226
3227       case GE:
3228         PUT_CODE (cond, EQ);
3229         value = 2;
3230         break;
3231
3232       case LT:
3233         PUT_CODE (cond, NE);
3234         value = 2;
3235         break;
3236
3237       default:
3238         break;
3239       }
3240
3241   if (cc_status.flags & CC_NOT_NEGATIVE)
3242     switch (GET_CODE (cond))
3243       {
3244       case GE:
3245       case GEU:
3246         /* Jump becomes unconditional.  */
3247         return 1;
3248
3249       case LT:
3250       case LTU:
3251         /* Jump becomes no-op.  */
3252         return -1;
3253
3254       case LE:
3255       case LEU:
3256         PUT_CODE (cond, EQ);
3257         value = 2;
3258         break;
3259
3260       case GT:
3261       case GTU:
3262         PUT_CODE (cond, NE);
3263         value = 2;
3264         break;
3265
3266       default:
3267         break;
3268       }
3269
3270   if (cc_status.flags & CC_NO_OVERFLOW)
3271     switch (GET_CODE (cond))
3272       {
3273       case GEU:
3274         /* Jump becomes unconditional.  */
3275         return 1;
3276
3277       case LEU:
3278         PUT_CODE (cond, EQ);
3279         value = 2;
3280         break;
3281
3282       case GTU:
3283         PUT_CODE (cond, NE);
3284         value = 2;
3285         break;
3286
3287       case LTU:
3288         /* Jump becomes no-op.  */
3289         return -1;
3290
3291       default:
3292         break;
3293       }
3294
3295   if (cc_status.flags & (CC_Z_IN_NOT_N | CC_Z_IN_N))
3296     switch (GET_CODE (cond))
3297       {
3298       default:
3299         gcc_unreachable ();
3300
3301       case NE:
3302         PUT_CODE (cond, cc_status.flags & CC_Z_IN_N ? GE : LT);
3303         value = 2;
3304         break;
3305
3306       case EQ:
3307         PUT_CODE (cond, cc_status.flags & CC_Z_IN_N ? LT : GE);
3308         value = 2;
3309         break;
3310       }
3311
3312   if (cc_status.flags & CC_NOT_SIGNED)
3313     /* The flags are valid if signed condition operators are converted
3314        to unsigned.  */
3315     switch (GET_CODE (cond))
3316       {
3317       case LE:
3318         PUT_CODE (cond, LEU);
3319         value = 2;
3320         break;
3321
3322       case LT:
3323         PUT_CODE (cond, LTU);
3324         value = 2;
3325         break;
3326
3327       case GT:
3328         PUT_CODE (cond, GTU);
3329         value = 2;
3330         break;
3331
3332       case GE:
3333         PUT_CODE (cond, GEU);
3334         value = 2;
3335         break;
3336
3337       default:
3338         break;
3339       }
3340
3341   return value;
3342 }
3343 #endif
3344 \f
3345 /* Report inconsistency between the assembler template and the operands.
3346    In an `asm', it's the user's fault; otherwise, the compiler's fault.  */
3347
3348 void
3349 output_operand_lossage (const char *cmsgid, ...)
3350 {
3351   char *fmt_string;
3352   char *new_message;
3353   const char *pfx_str;
3354   va_list ap;
3355
3356   va_start (ap, cmsgid);
3357
3358   pfx_str = this_is_asm_operands ? _("invalid 'asm': ") : "output_operand: ";
3359   asprintf (&fmt_string, "%s%s", pfx_str, _(cmsgid));
3360   vasprintf (&new_message, fmt_string, ap);
3361
3362   if (this_is_asm_operands)
3363     error_for_asm (this_is_asm_operands, "%s", new_message);
3364   else
3365     internal_error ("%s", new_message);
3366
3367   free (fmt_string);
3368   free (new_message);
3369   va_end (ap);
3370 }
3371 \f
3372 /* Output of assembler code from a template, and its subroutines.  */
3373
3374 /* Annotate the assembly with a comment describing the pattern and
3375    alternative used.  */
3376
3377 static void
3378 output_asm_name (void)
3379 {
3380   if (debug_insn)
3381     {
3382       int num = INSN_CODE (debug_insn);
3383       fprintf (asm_out_file, "\t%s %d\t%s",
3384                ASM_COMMENT_START, INSN_UID (debug_insn),
3385                insn_data[num].name);
3386       if (insn_data[num].n_alternatives > 1)
3387         fprintf (asm_out_file, "/%d", which_alternative + 1);
3388
3389       if (HAVE_ATTR_length)
3390         fprintf (asm_out_file, "\t[length = %d]",
3391                  get_attr_length (debug_insn));
3392
3393       /* Clear this so only the first assembler insn
3394          of any rtl insn will get the special comment for -dp.  */
3395       debug_insn = 0;
3396     }
3397 }
3398
3399 /* If OP is a REG or MEM and we can find a MEM_EXPR corresponding to it
3400    or its address, return that expr .  Set *PADDRESSP to 1 if the expr
3401    corresponds to the address of the object and 0 if to the object.  */
3402
3403 static tree
3404 get_mem_expr_from_op (rtx op, int *paddressp)
3405 {
3406   tree expr;
3407   int inner_addressp;
3408
3409   *paddressp = 0;
3410
3411   if (REG_P (op))
3412     return REG_EXPR (op);
3413   else if (!MEM_P (op))
3414     return 0;
3415
3416   if (MEM_EXPR (op) != 0)
3417     return MEM_EXPR (op);
3418
3419   /* Otherwise we have an address, so indicate it and look at the address.  */
3420   *paddressp = 1;
3421   op = XEXP (op, 0);
3422
3423   /* First check if we have a decl for the address, then look at the right side
3424      if it is a PLUS.  Otherwise, strip off arithmetic and keep looking.
3425      But don't allow the address to itself be indirect.  */
3426   if ((expr = get_mem_expr_from_op (op, &inner_addressp)) && ! inner_addressp)
3427     return expr;
3428   else if (GET_CODE (op) == PLUS
3429            && (expr = get_mem_expr_from_op (XEXP (op, 1), &inner_addressp)))
3430     return expr;
3431
3432   while (UNARY_P (op)
3433          || GET_RTX_CLASS (GET_CODE (op)) == RTX_BIN_ARITH)
3434     op = XEXP (op, 0);
3435
3436   expr = get_mem_expr_from_op (op, &inner_addressp);
3437   return inner_addressp ? 0 : expr;
3438 }
3439
3440 /* Output operand names for assembler instructions.  OPERANDS is the
3441    operand vector, OPORDER is the order to write the operands, and NOPS
3442    is the number of operands to write.  */
3443
3444 static void
3445 output_asm_operand_names (rtx *operands, int *oporder, int nops)
3446 {
3447   int wrote = 0;
3448   int i;
3449
3450   for (i = 0; i < nops; i++)
3451     {
3452       int addressp;
3453       rtx op = operands[oporder[i]];
3454       tree expr = get_mem_expr_from_op (op, &addressp);
3455
3456       fprintf (asm_out_file, "%c%s",
3457                wrote ? ',' : '\t', wrote ? "" : ASM_COMMENT_START);
3458       wrote = 1;
3459       if (expr)
3460         {
3461           fprintf (asm_out_file, "%s",
3462                    addressp ? "*" : "");
3463           print_mem_expr (asm_out_file, expr);
3464           wrote = 1;
3465         }
3466       else if (REG_P (op) && ORIGINAL_REGNO (op)
3467                && ORIGINAL_REGNO (op) != REGNO (op))
3468         fprintf (asm_out_file, " tmp%i", ORIGINAL_REGNO (op));
3469     }
3470 }
3471
3472 #ifdef ASSEMBLER_DIALECT
3473 /* Helper function to parse assembler dialects in the asm string.
3474    This is called from output_asm_insn and asm_fprintf.  */
3475 static const char *
3476 do_assembler_dialects (const char *p, int *dialect)
3477 {
3478   char c = *(p - 1);
3479
3480   switch (c)
3481     {
3482     case '{':
3483       {
3484         int i;
3485
3486         if (*dialect)
3487           output_operand_lossage ("nested assembly dialect alternatives");
3488         else
3489           *dialect = 1;
3490
3491         /* If we want the first dialect, do nothing.  Otherwise, skip
3492            DIALECT_NUMBER of strings ending with '|'.  */
3493         for (i = 0; i < dialect_number; i++)
3494           {
3495             while (*p && *p != '}')
3496               {
3497                 if (*p == '|')
3498                   {
3499                     p++;
3500                     break;
3501                   }
3502
3503                 /* Skip over any character after a percent sign.  */
3504                 if (*p == '%')
3505                   p++;
3506                 if (*p)
3507                   p++;
3508               }
3509
3510             if (*p == '}')
3511               break;
3512           }
3513
3514         if (*p == '\0')
3515           output_operand_lossage ("unterminated assembly dialect alternative");
3516       }
3517       break;
3518
3519     case '|':
3520       if (*dialect)
3521         {
3522           /* Skip to close brace.  */
3523           do
3524             {
3525               if (*p == '\0')
3526                 {
3527                   output_operand_lossage ("unterminated assembly dialect alternative");
3528                   break;
3529                 }
3530
3531               /* Skip over any character after a percent sign.  */
3532               if (*p == '%' && p[1])
3533                 {
3534                   p += 2;
3535                   continue;
3536                 }
3537
3538               if (*p++ == '}')
3539                 break;
3540             }
3541           while (1);
3542
3543           *dialect = 0;
3544         }
3545       else
3546         putc (c, asm_out_file);
3547       break;
3548
3549     case '}':
3550       if (! *dialect)
3551         putc (c, asm_out_file);
3552       *dialect = 0;
3553       break;
3554     default:
3555       gcc_unreachable ();
3556     }
3557
3558   return p;
3559 }
3560 #endif
3561
3562 /* Output text from TEMPLATE to the assembler output file,
3563    obeying %-directions to substitute operands taken from
3564    the vector OPERANDS.
3565
3566    %N (for N a digit) means print operand N in usual manner.
3567    %lN means require operand N to be a CODE_LABEL or LABEL_REF
3568       and print the label name with no punctuation.
3569    %cN means require operand N to be a constant
3570       and print the constant expression with no punctuation.
3571    %aN means expect operand N to be a memory address
3572       (not a memory reference!) and print a reference
3573       to that address.
3574    %nN means expect operand N to be a constant
3575       and print a constant expression for minus the value
3576       of the operand, with no other punctuation.  */
3577
3578 void
3579 output_asm_insn (const char *templ, rtx *operands)
3580 {
3581   const char *p;
3582   int c;
3583 #ifdef ASSEMBLER_DIALECT
3584   int dialect = 0;
3585 #endif
3586   int oporder[MAX_RECOG_OPERANDS];
3587   char opoutput[MAX_RECOG_OPERANDS];
3588   int ops = 0;
3589
3590   /* An insn may return a null string template
3591      in a case where no assembler code is needed.  */
3592   if (*templ == 0)
3593     return;
3594
3595   memset (opoutput, 0, sizeof opoutput);
3596   p = templ;
3597   putc ('\t', asm_out_file);
3598
3599 #ifdef ASM_OUTPUT_OPCODE
3600   ASM_OUTPUT_OPCODE (asm_out_file, p);
3601 #endif
3602
3603   while ((c = *p++))
3604     switch (c)
3605       {
3606       case '\n':
3607         if (flag_verbose_asm)
3608           output_asm_operand_names (operands, oporder, ops);
3609         if (flag_print_asm_name)
3610           output_asm_name ();
3611
3612         ops = 0;
3613         memset (opoutput, 0, sizeof opoutput);
3614
3615         putc (c, asm_out_file);
3616 #ifdef ASM_OUTPUT_OPCODE
3617         while ((c = *p) == '\t')
3618           {
3619             putc (c, asm_out_file);
3620             p++;
3621           }
3622         ASM_OUTPUT_OPCODE (asm_out_file, p);
3623 #endif
3624         break;
3625
3626 #ifdef ASSEMBLER_DIALECT
3627       case '{':
3628       case '}':
3629       case '|':
3630         p = do_assembler_dialects (p, &dialect);
3631         break;
3632 #endif
3633
3634       case '%':
3635         /* %% outputs a single %.  %{, %} and %| print {, } and | respectively
3636            if ASSEMBLER_DIALECT defined and these characters have a special
3637            meaning as dialect delimiters.*/
3638         if (*p == '%'
3639 #ifdef ASSEMBLER_DIALECT
3640             || *p == '{' || *p == '}' || *p == '|'
3641 #endif
3642             )
3643           {
3644             putc (*p, asm_out_file);
3645             p++;
3646           }
3647         /* %= outputs a number which is unique to each insn in the entire
3648            compilation.  This is useful for making local labels that are
3649            referred to more than once in a given insn.  */
3650         else if (*p == '=')
3651           {
3652             p++;
3653             fprintf (asm_out_file, "%d", insn_counter);
3654           }
3655         /* % followed by a letter and some digits
3656            outputs an operand in a special way depending on the letter.
3657            Letters `acln' are implemented directly.
3658            Other letters are passed to `output_operand' so that
3659            the TARGET_PRINT_OPERAND hook can define them.  */
3660         else if (ISALPHA (*p))
3661           {
3662             int letter = *p++;
3663             unsigned long opnum;
3664             char *endptr;
3665
3666             opnum = strtoul (p, &endptr, 10);
3667
3668             if (endptr == p)
3669               output_operand_lossage ("operand number missing "
3670                                       "after %%-letter");
3671             else if (this_is_asm_operands && opnum >= insn_noperands)
3672               output_operand_lossage ("operand number out of range");
3673             else if (letter == 'l')
3674               output_asm_label (operands[opnum]);
3675             else if (letter == 'a')
3676               output_address (operands[opnum]);
3677             else if (letter == 'c')
3678               {
3679                 if (CONSTANT_ADDRESS_P (operands[opnum]))
3680                   output_addr_const (asm_out_file, operands[opnum]);
3681                 else
3682                   output_operand (operands[opnum], 'c');
3683               }
3684             else if (letter == 'n')
3685               {
3686                 if (CONST_INT_P (operands[opnum]))
3687                   fprintf (asm_out_file, HOST_WIDE_INT_PRINT_DEC,
3688                            - INTVAL (operands[opnum]));
3689                 else
3690                   {
3691                     putc ('-', asm_out_file);
3692                     output_addr_const (asm_out_file, operands[opnum]);
3693                   }
3694               }
3695             else
3696               output_operand (operands[opnum], letter);
3697
3698             if (!opoutput[opnum])
3699               oporder[ops++] = opnum;
3700             opoutput[opnum] = 1;
3701
3702             p = endptr;
3703             c = *p;
3704           }
3705         /* % followed by a digit outputs an operand the default way.  */
3706         else if (ISDIGIT (*p))
3707           {
3708             unsigned long opnum;
3709             char *endptr;
3710
3711             opnum = strtoul (p, &endptr, 10);
3712             if (this_is_asm_operands && opnum >= insn_noperands)
3713               output_operand_lossage ("operand number out of range");
3714             else
3715               output_operand (operands[opnum], 0);
3716
3717             if (!opoutput[opnum])
3718               oporder[ops++] = opnum;
3719             opoutput[opnum] = 1;
3720
3721             p = endptr;
3722             c = *p;
3723           }
3724         /* % followed by punctuation: output something for that
3725            punctuation character alone, with no operand.  The
3726            TARGET_PRINT_OPERAND hook decides what is actually done.  */
3727         else if (targetm.asm_out.print_operand_punct_valid_p ((unsigned char) *p))
3728           output_operand (NULL_RTX, *p++);
3729         else
3730           output_operand_lossage ("invalid %%-code");
3731         break;
3732
3733       default:
3734         putc (c, asm_out_file);
3735       }
3736
3737   /* Write out the variable names for operands, if we know them.  */
3738   if (flag_verbose_asm)
3739     output_asm_operand_names (operands, oporder, ops);
3740   if (flag_print_asm_name)
3741     output_asm_name ();
3742
3743   putc ('\n', asm_out_file);
3744 }
3745 \f
3746 /* Output a LABEL_REF, or a bare CODE_LABEL, as an assembler symbol.  */
3747
3748 void
3749 output_asm_label (rtx x)
3750 {
3751   char buf[256];
3752
3753   if (GET_CODE (x) == LABEL_REF)
3754     x = XEXP (x, 0);
3755   if (LABEL_P (x)
3756       || (NOTE_P (x)
3757           && NOTE_KIND (x) == NOTE_INSN_DELETED_LABEL))
3758     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
3759   else
3760     output_operand_lossage ("'%%l' operand isn't a label");
3761
3762   assemble_name (asm_out_file, buf);
3763 }
3764
3765 /* Helper rtx-iteration-function for mark_symbol_refs_as_used and
3766    output_operand.  Marks SYMBOL_REFs as referenced through use of
3767    assemble_external.  */
3768
3769 static int
3770 mark_symbol_ref_as_used (rtx *xp, void *dummy ATTRIBUTE_UNUSED)
3771 {
3772   rtx x = *xp;
3773
3774   /* If we have a used symbol, we may have to emit assembly
3775      annotations corresponding to whether the symbol is external, weak
3776      or has non-default visibility.  */
3777   if (GET_CODE (x) == SYMBOL_REF)
3778     {
3779       tree t;
3780
3781       t = SYMBOL_REF_DECL (x);
3782       if (t)
3783         assemble_external (t);
3784
3785       return -1;
3786     }
3787
3788   return 0;
3789 }
3790
3791 /* Marks SYMBOL_REFs in x as referenced through use of assemble_external.  */
3792
3793 void
3794 mark_symbol_refs_as_used (rtx x)
3795 {
3796   for_each_rtx (&x, mark_symbol_ref_as_used, NULL);
3797 }
3798
3799 /* Print operand X using machine-dependent assembler syntax.
3800    CODE is a non-digit that preceded the operand-number in the % spec,
3801    such as 'z' if the spec was `%z3'.  CODE is 0 if there was no char
3802    between the % and the digits.
3803    When CODE is a non-letter, X is 0.
3804
3805    The meanings of the letters are machine-dependent and controlled
3806    by TARGET_PRINT_OPERAND.  */
3807
3808 void
3809 output_operand (rtx x, int code ATTRIBUTE_UNUSED)
3810 {
3811   if (x && GET_CODE (x) == SUBREG)
3812     x = alter_subreg (&x, true);
3813
3814   /* X must not be a pseudo reg.  */
3815   gcc_assert (!x || !REG_P (x) || REGNO (x) < FIRST_PSEUDO_REGISTER);
3816
3817   targetm.asm_out.print_operand (asm_out_file, x, code);
3818
3819   if (x == NULL_RTX)
3820     return;
3821
3822   for_each_rtx (&x, mark_symbol_ref_as_used, NULL);
3823 }
3824
3825 /* Print a memory reference operand for address X using
3826    machine-dependent assembler syntax.  */
3827
3828 void
3829 output_address (rtx x)
3830 {
3831   bool changed = false;
3832   walk_alter_subreg (&x, &changed);
3833   targetm.asm_out.print_operand_address (asm_out_file, x);
3834 }
3835 \f
3836 /* Print an integer constant expression in assembler syntax.
3837    Addition and subtraction are the only arithmetic
3838    that may appear in these expressions.  */
3839
3840 void
3841 output_addr_const (FILE *file, rtx x)
3842 {
3843   char buf[256];
3844
3845  restart:
3846   switch (GET_CODE (x))
3847     {
3848     case PC:
3849       putc ('.', file);
3850       break;
3851
3852     case SYMBOL_REF:
3853       if (SYMBOL_REF_DECL (x))
3854         assemble_external (SYMBOL_REF_DECL (x));
3855 #ifdef ASM_OUTPUT_SYMBOL_REF
3856       ASM_OUTPUT_SYMBOL_REF (file, x);
3857 #else
3858       assemble_name (file, XSTR (x, 0));
3859 #endif
3860       break;
3861
3862     case LABEL_REF:
3863       x = XEXP (x, 0);
3864       /* Fall through.  */
3865     case CODE_LABEL:
3866       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
3867 #ifdef ASM_OUTPUT_LABEL_REF
3868       ASM_OUTPUT_LABEL_REF (file, buf);
3869 #else
3870       assemble_name (file, buf);
3871 #endif
3872       break;
3873
3874     case CONST_INT:
3875       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
3876       break;
3877
3878     case CONST:
3879       /* This used to output parentheses around the expression,
3880          but that does not work on the 386 (either ATT or BSD assembler).  */
3881       output_addr_const (file, XEXP (x, 0));
3882       break;
3883
3884     case CONST_DOUBLE:
3885       if (GET_MODE (x) == VOIDmode)
3886         {
3887           /* We can use %d if the number is one word and positive.  */
3888           if (CONST_DOUBLE_HIGH (x))
3889             fprintf (file, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
3890                      (unsigned HOST_WIDE_INT) CONST_DOUBLE_HIGH (x),
3891                      (unsigned HOST_WIDE_INT) CONST_DOUBLE_LOW (x));
3892           else if (CONST_DOUBLE_LOW (x) < 0)
3893             fprintf (file, HOST_WIDE_INT_PRINT_HEX,
3894                      (unsigned HOST_WIDE_INT) CONST_DOUBLE_LOW (x));
3895           else
3896             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
3897         }
3898       else
3899         /* We can't handle floating point constants;
3900            PRINT_OPERAND must handle them.  */
3901         output_operand_lossage ("floating constant misused");
3902       break;
3903
3904     case CONST_FIXED:
3905       fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_FIXED_VALUE_LOW (x));
3906       break;
3907
3908     case PLUS:
3909       /* Some assemblers need integer constants to appear last (eg masm).  */
3910       if (CONST_INT_P (XEXP (x, 0)))
3911         {
3912           output_addr_const (file, XEXP (x, 1));
3913           if (INTVAL (XEXP (x, 0)) >= 0)
3914             fprintf (file, "+");
3915           output_addr_const (file, XEXP (x, 0));
3916         }
3917       else
3918         {
3919           output_addr_const (file, XEXP (x, 0));
3920           if (!CONST_INT_P (XEXP (x, 1))
3921               || INTVAL (XEXP (x, 1)) >= 0)
3922             fprintf (file, "+");
3923           output_addr_const (file, XEXP (x, 1));
3924         }
3925       break;
3926
3927     case MINUS:
3928       /* Avoid outputting things like x-x or x+5-x,
3929          since some assemblers can't handle that.  */
3930       x = simplify_subtraction (x);
3931       if (GET_CODE (x) != MINUS)
3932         goto restart;
3933
3934       output_addr_const (file, XEXP (x, 0));
3935       fprintf (file, "-");
3936       if ((CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0)
3937           || GET_CODE (XEXP (x, 1)) == PC
3938           || GET_CODE (XEXP (x, 1)) == SYMBOL_REF)
3939         output_addr_const (file, XEXP (x, 1));
3940       else
3941         {
3942           fputs (targetm.asm_out.open_paren, file);
3943           output_addr_const (file, XEXP (x, 1));
3944           fputs (targetm.asm_out.close_paren, file);
3945         }
3946       break;
3947
3948     case ZERO_EXTEND:
3949     case SIGN_EXTEND:
3950     case SUBREG:
3951     case TRUNCATE:
3952       output_addr_const (file, XEXP (x, 0));
3953       break;
3954
3955     default:
3956       if (targetm.asm_out.output_addr_const_extra (file, x))
3957         break;
3958
3959       output_operand_lossage ("invalid expression as operand");
3960     }
3961 }
3962 \f
3963 /* Output a quoted string.  */
3964
3965 void
3966 output_quoted_string (FILE *asm_file, const char *string)
3967 {
3968 #ifdef OUTPUT_QUOTED_STRING
3969   OUTPUT_QUOTED_STRING (asm_file, string);
3970 #else
3971   char c;
3972
3973   putc ('\"', asm_file);
3974   while ((c = *string++) != 0)
3975     {
3976       if (ISPRINT (c))
3977         {
3978           if (c == '\"' || c == '\\')
3979             putc ('\\', asm_file);
3980           putc (c, asm_file);
3981         }
3982       else
3983         fprintf (asm_file, "\\%03o", (unsigned char) c);
3984     }
3985   putc ('\"', asm_file);
3986 #endif
3987 }
3988 \f
3989 /* Write a HOST_WIDE_INT number in hex form 0x1234, fast. */
3990
3991 void
3992 fprint_whex (FILE *f, unsigned HOST_WIDE_INT value)
3993 {
3994   char buf[2 + CHAR_BIT * sizeof (value) / 4];
3995   if (value == 0)
3996     putc ('0', f);
3997   else
3998     {
3999       char *p = buf + sizeof (buf);
4000       do
4001         *--p = "0123456789abcdef"[value % 16];
4002       while ((value /= 16) != 0);
4003       *--p = 'x';
4004       *--p = '0';
4005       fwrite (p, 1, buf + sizeof (buf) - p, f);
4006     }
4007 }
4008
4009 /* Internal function that prints an unsigned long in decimal in reverse.
4010    The output string IS NOT null-terminated. */
4011
4012 static int
4013 sprint_ul_rev (char *s, unsigned long value)
4014 {
4015   int i = 0;
4016   do
4017     {
4018       s[i] = "0123456789"[value % 10];
4019       value /= 10;
4020       i++;
4021       /* alternate version, without modulo */
4022       /* oldval = value; */
4023       /* value /= 10; */
4024       /* s[i] = "0123456789" [oldval - 10*value]; */
4025       /* i++ */
4026     }
4027   while (value != 0);
4028   return i;
4029 }
4030
4031 /* Write an unsigned long as decimal to a file, fast. */
4032
4033 void
4034 fprint_ul (FILE *f, unsigned long value)
4035 {
4036   /* python says: len(str(2**64)) == 20 */
4037   char s[20];
4038   int i;
4039
4040   i = sprint_ul_rev (s, value);
4041
4042   /* It's probably too small to bother with string reversal and fputs. */
4043   do
4044     {
4045       i--;
4046       putc (s[i], f);
4047     }
4048   while (i != 0);
4049 }
4050
4051 /* Write an unsigned long as decimal to a string, fast.
4052    s must be wide enough to not overflow, at least 21 chars.
4053    Returns the length of the string (without terminating '\0'). */
4054
4055 int
4056 sprint_ul (char *s, unsigned long value)
4057 {
4058   int len;
4059   char tmp_c;
4060   int i;
4061   int j;
4062
4063   len = sprint_ul_rev (s, value);
4064   s[len] = '\0';
4065
4066   /* Reverse the string. */
4067   i = 0;
4068   j = len - 1;
4069   while (i < j)
4070     {
4071       tmp_c = s[i];
4072       s[i] = s[j];
4073       s[j] = tmp_c;
4074       i++; j--;
4075     }
4076
4077   return len;
4078 }
4079
4080 /* A poor man's fprintf, with the added features of %I, %R, %L, and %U.
4081    %R prints the value of REGISTER_PREFIX.
4082    %L prints the value of LOCAL_LABEL_PREFIX.
4083    %U prints the value of USER_LABEL_PREFIX.
4084    %I prints the value of IMMEDIATE_PREFIX.
4085    %O runs ASM_OUTPUT_OPCODE to transform what follows in the string.
4086    Also supported are %d, %i, %u, %x, %X, %o, %c, %s and %%.
4087
4088    We handle alternate assembler dialects here, just like output_asm_insn.  */
4089
4090 void
4091 asm_fprintf (FILE *file, const char *p, ...)
4092 {
4093   char buf[10];
4094   char *q, c;
4095 #ifdef ASSEMBLER_DIALECT
4096   int dialect = 0;
4097 #endif
4098   va_list argptr;
4099
4100   va_start (argptr, p);
4101
4102   buf[0] = '%';
4103
4104   while ((c = *p++))
4105     switch (c)
4106       {
4107 #ifdef ASSEMBLER_DIALECT
4108       case '{':
4109       case '}':
4110       case '|':
4111         p = do_assembler_dialects (p, &dialect);
4112         break;
4113 #endif
4114
4115       case '%':
4116         c = *p++;
4117         q = &buf[1];
4118         while (strchr ("-+ #0", c))
4119           {
4120             *q++ = c;
4121             c = *p++;
4122           }
4123         while (ISDIGIT (c) || c == '.')
4124           {
4125             *q++ = c;
4126             c = *p++;
4127           }
4128         switch (c)
4129           {
4130           case '%':
4131             putc ('%', file);
4132             break;
4133
4134           case 'd':  case 'i':  case 'u':
4135           case 'x':  case 'X':  case 'o':
4136           case 'c':
4137             *q++ = c;
4138             *q = 0;
4139             fprintf (file, buf, va_arg (argptr, int));
4140             break;
4141
4142           case 'w':
4143             /* This is a prefix to the 'd', 'i', 'u', 'x', 'X', and
4144                'o' cases, but we do not check for those cases.  It
4145                means that the value is a HOST_WIDE_INT, which may be
4146                either `long' or `long long'.  */
4147             memcpy (q, HOST_WIDE_INT_PRINT, strlen (HOST_WIDE_INT_PRINT));
4148             q += strlen (HOST_WIDE_INT_PRINT);
4149             *q++ = *p++;
4150             *q = 0;
4151             fprintf (file, buf, va_arg (argptr, HOST_WIDE_INT));
4152             break;
4153
4154           case 'l':
4155             *q++ = c;
4156 #ifdef HAVE_LONG_LONG
4157             if (*p == 'l')
4158               {
4159                 *q++ = *p++;
4160                 *q++ = *p++;
4161                 *q = 0;
4162                 fprintf (file, buf, va_arg (argptr, long long));
4163               }
4164             else
4165 #endif
4166               {
4167                 *q++ = *p++;
4168                 *q = 0;
4169                 fprintf (file, buf, va_arg (argptr, long));
4170               }
4171
4172             break;
4173
4174           case 's':
4175             *q++ = c;
4176             *q = 0;
4177             fprintf (file, buf, va_arg (argptr, char *));
4178             break;
4179
4180           case 'O':
4181 #ifdef ASM_OUTPUT_OPCODE
4182             ASM_OUTPUT_OPCODE (asm_out_file, p);
4183 #endif
4184             break;
4185
4186           case 'R':
4187 #ifdef REGISTER_PREFIX
4188             fprintf (file, "%s", REGISTER_PREFIX);
4189 #endif
4190             break;
4191
4192           case 'I':
4193 #ifdef IMMEDIATE_PREFIX
4194             fprintf (file, "%s", IMMEDIATE_PREFIX);
4195 #endif
4196             break;
4197
4198           case 'L':
4199 #ifdef LOCAL_LABEL_PREFIX
4200             fprintf (file, "%s", LOCAL_LABEL_PREFIX);
4201 #endif
4202             break;
4203
4204           case 'U':
4205             fputs (user_label_prefix, file);
4206             break;
4207
4208 #ifdef ASM_FPRINTF_EXTENSIONS
4209             /* Uppercase letters are reserved for general use by asm_fprintf
4210                and so are not available to target specific code.  In order to
4211                prevent the ASM_FPRINTF_EXTENSIONS macro from using them then,
4212                they are defined here.  As they get turned into real extensions
4213                to asm_fprintf they should be removed from this list.  */
4214           case 'A': case 'B': case 'C': case 'D': case 'E':
4215           case 'F': case 'G': case 'H': case 'J': case 'K':
4216           case 'M': case 'N': case 'P': case 'Q': case 'S':
4217           case 'T': case 'V': case 'W': case 'Y': case 'Z':
4218             break;
4219
4220           ASM_FPRINTF_EXTENSIONS (file, argptr, p)
4221 #endif
4222           default:
4223             gcc_unreachable ();
4224           }
4225         break;
4226
4227       default:
4228         putc (c, file);
4229       }
4230   va_end (argptr);
4231 }
4232 \f
4233 /* Return nonzero if this function has no function calls.  */
4234
4235 int
4236 leaf_function_p (void)
4237 {
4238   rtx insn;
4239
4240   if (crtl->profile || profile_arc_flag)
4241     return 0;
4242
4243   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4244     {
4245       if (CALL_P (insn)
4246           && ! SIBLING_CALL_P (insn))
4247         return 0;
4248       if (NONJUMP_INSN_P (insn)
4249           && GET_CODE (PATTERN (insn)) == SEQUENCE
4250           && CALL_P (XVECEXP (PATTERN (insn), 0, 0))
4251           && ! SIBLING_CALL_P (XVECEXP (PATTERN (insn), 0, 0)))
4252         return 0;
4253     }
4254
4255   return 1;
4256 }
4257
4258 /* Return 1 if branch is a forward branch.
4259    Uses insn_shuid array, so it works only in the final pass.  May be used by
4260    output templates to customary add branch prediction hints.
4261  */
4262 int
4263 final_forward_branch_p (rtx insn)
4264 {
4265   int insn_id, label_id;
4266
4267   gcc_assert (uid_shuid);
4268   insn_id = INSN_SHUID (insn);
4269   label_id = INSN_SHUID (JUMP_LABEL (insn));
4270   /* We've hit some insns that does not have id information available.  */
4271   gcc_assert (insn_id && label_id);
4272   return insn_id < label_id;
4273 }
4274
4275 /* On some machines, a function with no call insns
4276    can run faster if it doesn't create its own register window.
4277    When output, the leaf function should use only the "output"
4278    registers.  Ordinarily, the function would be compiled to use
4279    the "input" registers to find its arguments; it is a candidate
4280    for leaf treatment if it uses only the "input" registers.
4281    Leaf function treatment means renumbering so the function
4282    uses the "output" registers instead.  */
4283
4284 #ifdef LEAF_REGISTERS
4285
4286 /* Return 1 if this function uses only the registers that can be
4287    safely renumbered.  */
4288
4289 int
4290 only_leaf_regs_used (void)
4291 {
4292   int i;
4293   const char *const permitted_reg_in_leaf_functions = LEAF_REGISTERS;
4294
4295   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4296     if ((df_regs_ever_live_p (i) || global_regs[i])
4297         && ! permitted_reg_in_leaf_functions[i])
4298       return 0;
4299
4300   if (crtl->uses_pic_offset_table
4301       && pic_offset_table_rtx != 0
4302       && REG_P (pic_offset_table_rtx)
4303       && ! permitted_reg_in_leaf_functions[REGNO (pic_offset_table_rtx)])
4304     return 0;
4305
4306   return 1;
4307 }
4308
4309 /* Scan all instructions and renumber all registers into those
4310    available in leaf functions.  */
4311
4312 static void
4313 leaf_renumber_regs (rtx first)
4314 {
4315   rtx insn;
4316
4317   /* Renumber only the actual patterns.
4318      The reg-notes can contain frame pointer refs,
4319      and renumbering them could crash, and should not be needed.  */
4320   for (insn = first; insn; insn = NEXT_INSN (insn))
4321     if (INSN_P (insn))
4322       leaf_renumber_regs_insn (PATTERN (insn));
4323 }
4324
4325 /* Scan IN_RTX and its subexpressions, and renumber all regs into those
4326    available in leaf functions.  */
4327
4328 void
4329 leaf_renumber_regs_insn (rtx in_rtx)
4330 {
4331   int i, j;
4332   const char *format_ptr;
4333
4334   if (in_rtx == 0)
4335     return;
4336
4337   /* Renumber all input-registers into output-registers.
4338      renumbered_regs would be 1 for an output-register;
4339      they  */
4340
4341   if (REG_P (in_rtx))
4342     {
4343       int newreg;
4344
4345       /* Don't renumber the same reg twice.  */
4346       if (in_rtx->used)
4347         return;
4348
4349       newreg = REGNO (in_rtx);
4350       /* Don't try to renumber pseudo regs.  It is possible for a pseudo reg
4351          to reach here as part of a REG_NOTE.  */
4352       if (newreg >= FIRST_PSEUDO_REGISTER)
4353         {
4354           in_rtx->used = 1;
4355           return;
4356         }
4357       newreg = LEAF_REG_REMAP (newreg);
4358       gcc_assert (newreg >= 0);
4359       df_set_regs_ever_live (REGNO (in_rtx), false);
4360       df_set_regs_ever_live (newreg, true);
4361       SET_REGNO (in_rtx, newreg);
4362       in_rtx->used = 1;
4363     }
4364
4365   if (INSN_P (in_rtx))
4366     {
4367       /* Inside a SEQUENCE, we find insns.
4368          Renumber just the patterns of these insns,
4369          just as we do for the top-level insns.  */
4370       leaf_renumber_regs_insn (PATTERN (in_rtx));
4371       return;
4372     }
4373
4374   format_ptr = GET_RTX_FORMAT (GET_CODE (in_rtx));
4375
4376   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (in_rtx)); i++)
4377     switch (*format_ptr++)
4378       {
4379       case 'e':
4380         leaf_renumber_regs_insn (XEXP (in_rtx, i));
4381         break;
4382
4383       case 'E':
4384         if (NULL != XVEC (in_rtx, i))
4385           {
4386             for (j = 0; j < XVECLEN (in_rtx, i); j++)
4387               leaf_renumber_regs_insn (XVECEXP (in_rtx, i, j));
4388           }
4389         break;
4390
4391       case 'S':
4392       case 's':
4393       case '0':
4394       case 'i':
4395       case 'w':
4396       case 'n':
4397       case 'u':
4398         break;
4399
4400       default:
4401         gcc_unreachable ();
4402       }
4403 }
4404 #endif
4405 \f
4406 /* Turn the RTL into assembly.  */
4407 static unsigned int
4408 rest_of_handle_final (void)
4409 {
4410   rtx x;
4411   const char *fnname;
4412
4413   /* Get the function's name, as described by its RTL.  This may be
4414      different from the DECL_NAME name used in the source file.  */
4415
4416   x = DECL_RTL (current_function_decl);
4417   gcc_assert (MEM_P (x));
4418   x = XEXP (x, 0);
4419   gcc_assert (GET_CODE (x) == SYMBOL_REF);
4420   fnname = XSTR (x, 0);
4421
4422   assemble_start_function (current_function_decl, fnname);
4423   final_start_function (get_insns (), asm_out_file, optimize);
4424   final (get_insns (), asm_out_file, optimize);
4425   final_end_function ();
4426
4427   /* The IA-64 ".handlerdata" directive must be issued before the ".endp"
4428      directive that closes the procedure descriptor.  Similarly, for x64 SEH.
4429      Otherwise it's not strictly necessary, but it doesn't hurt either.  */
4430   output_function_exception_table (fnname);
4431
4432   assemble_end_function (current_function_decl, fnname);
4433
4434   user_defined_section_attribute = false;
4435
4436   /* Free up reg info memory.  */
4437   free_reg_info ();
4438
4439   if (! quiet_flag)
4440     fflush (asm_out_file);
4441
4442   /* Write DBX symbols if requested.  */
4443
4444   /* Note that for those inline functions where we don't initially
4445      know for certain that we will be generating an out-of-line copy,
4446      the first invocation of this routine (rest_of_compilation) will
4447      skip over this code by doing a `goto exit_rest_of_compilation;'.
4448      Later on, wrapup_global_declarations will (indirectly) call
4449      rest_of_compilation again for those inline functions that need
4450      to have out-of-line copies generated.  During that call, we
4451      *will* be routed past here.  */
4452
4453   timevar_push (TV_SYMOUT);
4454   if (!DECL_IGNORED_P (current_function_decl))
4455     debug_hooks->function_decl (current_function_decl);
4456   timevar_pop (TV_SYMOUT);
4457
4458   /* Release the blocks that are linked to DECL_INITIAL() to free the memory.  */
4459   DECL_INITIAL (current_function_decl) = error_mark_node;
4460
4461   if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
4462       && targetm.have_ctors_dtors)
4463     targetm.asm_out.constructor (XEXP (DECL_RTL (current_function_decl), 0),
4464                                  decl_init_priority_lookup
4465                                    (current_function_decl));
4466   if (DECL_STATIC_DESTRUCTOR (current_function_decl)
4467       && targetm.have_ctors_dtors)
4468     targetm.asm_out.destructor (XEXP (DECL_RTL (current_function_decl), 0),
4469                                 decl_fini_priority_lookup
4470                                   (current_function_decl));
4471   return 0;
4472 }
4473
4474 namespace {
4475
4476 const pass_data pass_data_final =
4477 {
4478   RTL_PASS, /* type */
4479   "final", /* name */
4480   OPTGROUP_NONE, /* optinfo_flags */
4481   false, /* has_gate */
4482   true, /* has_execute */
4483   TV_FINAL, /* tv_id */
4484   0, /* properties_required */
4485   0, /* properties_provided */
4486   0, /* properties_destroyed */
4487   0, /* todo_flags_start */
4488   0, /* todo_flags_finish */
4489 };
4490
4491 class pass_final : public rtl_opt_pass
4492 {
4493 public:
4494   pass_final (gcc::context *ctxt)
4495     : rtl_opt_pass (pass_data_final, ctxt)
4496   {}
4497
4498   /* opt_pass methods: */
4499   unsigned int execute () { return rest_of_handle_final (); }
4500
4501 }; // class pass_final
4502
4503 } // anon namespace
4504
4505 rtl_opt_pass *
4506 make_pass_final (gcc::context *ctxt)
4507 {
4508   return new pass_final (ctxt);
4509 }
4510
4511
4512 static unsigned int
4513 rest_of_handle_shorten_branches (void)
4514 {
4515   /* Shorten branches.  */
4516   shorten_branches (get_insns ());
4517   return 0;
4518 }
4519
4520 namespace {
4521
4522 const pass_data pass_data_shorten_branches =
4523 {
4524   RTL_PASS, /* type */
4525   "shorten", /* name */
4526   OPTGROUP_NONE, /* optinfo_flags */
4527   false, /* has_gate */
4528   true, /* has_execute */
4529   TV_SHORTEN_BRANCH, /* tv_id */
4530   0, /* properties_required */
4531   0, /* properties_provided */
4532   0, /* properties_destroyed */
4533   0, /* todo_flags_start */
4534   0, /* todo_flags_finish */
4535 };
4536
4537 class pass_shorten_branches : public rtl_opt_pass
4538 {
4539 public:
4540   pass_shorten_branches (gcc::context *ctxt)
4541     : rtl_opt_pass (pass_data_shorten_branches, ctxt)
4542   {}
4543
4544   /* opt_pass methods: */
4545   unsigned int execute () { return rest_of_handle_shorten_branches (); }
4546
4547 }; // class pass_shorten_branches
4548
4549 } // anon namespace
4550
4551 rtl_opt_pass *
4552 make_pass_shorten_branches (gcc::context *ctxt)
4553 {
4554   return new pass_shorten_branches (ctxt);
4555 }
4556
4557
4558 static unsigned int
4559 rest_of_clean_state (void)
4560 {
4561   rtx insn, next;
4562   FILE *final_output = NULL;
4563   int save_unnumbered = flag_dump_unnumbered;
4564   int save_noaddr = flag_dump_noaddr;
4565
4566   if (flag_dump_final_insns)
4567     {
4568       final_output = fopen (flag_dump_final_insns, "a");
4569       if (!final_output)
4570         {
4571           error ("could not open final insn dump file %qs: %m",
4572                  flag_dump_final_insns);
4573           flag_dump_final_insns = NULL;
4574         }
4575       else
4576         {
4577           flag_dump_noaddr = flag_dump_unnumbered = 1;
4578           if (flag_compare_debug_opt || flag_compare_debug)
4579             dump_flags |= TDF_NOUID;
4580           dump_function_header (final_output, current_function_decl,
4581                                 dump_flags);
4582           final_insns_dump_p = true;
4583
4584           for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4585             if (LABEL_P (insn))
4586               INSN_UID (insn) = CODE_LABEL_NUMBER (insn);
4587             else
4588               {
4589                 if (NOTE_P (insn))
4590                   set_block_for_insn (insn, NULL);
4591                 INSN_UID (insn) = 0;
4592               }
4593         }
4594     }
4595
4596   /* It is very important to decompose the RTL instruction chain here:
4597      debug information keeps pointing into CODE_LABEL insns inside the function
4598      body.  If these remain pointing to the other insns, we end up preserving
4599      whole RTL chain and attached detailed debug info in memory.  */
4600   for (insn = get_insns (); insn; insn = next)
4601     {
4602       next = NEXT_INSN (insn);
4603       NEXT_INSN (insn) = NULL;
4604       PREV_INSN (insn) = NULL;
4605
4606       if (final_output
4607           && (!NOTE_P (insn) ||
4608               (NOTE_KIND (insn) != NOTE_INSN_VAR_LOCATION
4609                && NOTE_KIND (insn) != NOTE_INSN_CALL_ARG_LOCATION
4610                && NOTE_KIND (insn) != NOTE_INSN_BLOCK_BEG
4611                && NOTE_KIND (insn) != NOTE_INSN_BLOCK_END
4612                && NOTE_KIND (insn) != NOTE_INSN_DELETED_DEBUG_LABEL)))
4613         print_rtl_single (final_output, insn);
4614     }
4615
4616   if (final_output)
4617     {
4618       flag_dump_noaddr = save_noaddr;
4619       flag_dump_unnumbered = save_unnumbered;
4620       final_insns_dump_p = false;
4621
4622       if (fclose (final_output))
4623         {
4624           error ("could not close final insn dump file %qs: %m",
4625                  flag_dump_final_insns);
4626           flag_dump_final_insns = NULL;
4627         }
4628     }
4629
4630   /* In case the function was not output,
4631      don't leave any temporary anonymous types
4632      queued up for sdb output.  */
4633 #ifdef SDB_DEBUGGING_INFO
4634   if (write_symbols == SDB_DEBUG)
4635     sdbout_types (NULL_TREE);
4636 #endif
4637
4638   flag_rerun_cse_after_global_opts = 0;
4639   reload_completed = 0;
4640   epilogue_completed = 0;
4641 #ifdef STACK_REGS
4642   regstack_completed = 0;
4643 #endif
4644
4645   /* Clear out the insn_length contents now that they are no
4646      longer valid.  */
4647   init_insn_lengths ();
4648
4649   /* Show no temporary slots allocated.  */
4650   init_temp_slots ();
4651
4652   free_bb_for_insn ();
4653
4654   delete_tree_ssa ();
4655
4656   /* We can reduce stack alignment on call site only when we are sure that
4657      the function body just produced will be actually used in the final
4658      executable.  */
4659   if (decl_binds_to_current_def_p (current_function_decl))
4660     {
4661       unsigned int pref = crtl->preferred_stack_boundary;
4662       if (crtl->stack_alignment_needed > crtl->preferred_stack_boundary)
4663         pref = crtl->stack_alignment_needed;
4664       cgraph_rtl_info (current_function_decl)->preferred_incoming_stack_boundary
4665         = pref;
4666     }
4667
4668   /* Make sure volatile mem refs aren't considered valid operands for
4669      arithmetic insns.  We must call this here if this is a nested inline
4670      function, since the above code leaves us in the init_recog state,
4671      and the function context push/pop code does not save/restore volatile_ok.
4672
4673      ??? Maybe it isn't necessary for expand_start_function to call this
4674      anymore if we do it here?  */
4675
4676   init_recog_no_volatile ();
4677
4678   /* We're done with this function.  Free up memory if we can.  */
4679   free_after_parsing (cfun);
4680   free_after_compilation (cfun);
4681   return 0;
4682 }
4683
4684 namespace {
4685
4686 const pass_data pass_data_clean_state =
4687 {
4688   RTL_PASS, /* type */
4689   "*clean_state", /* name */
4690   OPTGROUP_NONE, /* optinfo_flags */
4691   false, /* has_gate */
4692   true, /* has_execute */
4693   TV_FINAL, /* tv_id */
4694   0, /* properties_required */
4695   0, /* properties_provided */
4696   PROP_rtl, /* properties_destroyed */
4697   0, /* todo_flags_start */
4698   0, /* todo_flags_finish */
4699 };
4700
4701 class pass_clean_state : public rtl_opt_pass
4702 {
4703 public:
4704   pass_clean_state (gcc::context *ctxt)
4705     : rtl_opt_pass (pass_data_clean_state, ctxt)
4706   {}
4707
4708   /* opt_pass methods: */
4709   unsigned int execute () { return rest_of_clean_state (); }
4710
4711 }; // class pass_clean_state
4712
4713 } // anon namespace
4714
4715 rtl_opt_pass *
4716 make_pass_clean_state (gcc::context *ctxt)
4717 {
4718   return new pass_clean_state (ctxt);
4719 }