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