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