OSDN Git Service

* final.c (alter_subreg): Allow the target to hook by-mode subreg
[pf3gnuchains/gcc-fork.git] / gcc / final.c
index c9bc233..6b851f2 100644 (file)
@@ -50,18 +50,7 @@ Boston, MA 02111-1307, USA.  */
 #else
 #include <varargs.h>
 #endif
-#include <stdio.h>
-#include <ctype.h>
-#if HAVE_STDLIB_H
-#include <stdlib.h>
-#endif
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
+#include "system.h"
 
 #include "tree.h"
 #include "rtl.h"
@@ -161,7 +150,7 @@ extern int length_unit_log; /* This is defined in insn-attrtab.c.  */
 static rtx this_is_asm_operands;
 
 /* Number of operands of this insn, for an `asm' with operands.  */
-static int insn_noperands;
+static unsigned int insn_noperands;
 
 /* Compare optimization flag.  */
 
@@ -795,7 +784,7 @@ short *label_align; /* sh.c needs this to calculate constant tables.  */
 
 #define INSN_SHUID(INSN) (uid_shuid[INSN_UID (INSN)])
 
-static int min_labelno;
+static int min_labelno, max_labelno;
 
 #define LABEL_TO_ALIGNMENT(LABEL) \
   (label_align[CODE_LABEL_NUMBER (LABEL) - min_labelno])
@@ -893,17 +882,18 @@ insn_current_reference_address (branch)
        any alignment we'd encounter, so we skip the call to align_fuzz.  */
     return insn_current_address;
   dest = JUMP_LABEL (branch);
+  /* BRANCH has no proper alignment chain set, so use SEQ.  */
   if (INSN_SHUID (branch) < INSN_SHUID (dest))
     {
       /* Forward branch. */
       return (insn_last_address + insn_lengths[seq_uid]
-             - align_fuzz (branch, dest, length_unit_log, ~0));
+             - align_fuzz (seq, dest, length_unit_log, ~0));
     }
   else
     {
       /* Backward branch. */
       return (insn_current_address
-             + align_fuzz (dest, branch, length_unit_log, ~0));
+             + align_fuzz (dest, seq, length_unit_log, ~0));
     }
 }
 #endif /* HAVE_ATTR_length */
@@ -932,7 +922,6 @@ shorten_branches (first)
   rtx insn;
   int max_uid;
   int i;
-  int max_labelno;
   int max_log;
 #ifdef HAVE_ATTR_length
 #define MAX_CODE_ALIGN 16
@@ -983,13 +972,23 @@ shorten_branches (first)
 
   /* Initialize label_align and set up uid_shuid to be strictly
      monotonically rising with insn order.  */
+  /* We use max_log here to keep track of the maximum alignment we want to
+     impose on the next CODE_LABEL (or the current one if we are processing
+     the CODE_LABEL itself).  */
+     
   for (max_log = 0, insn = get_insns (), i = 1; insn; insn = NEXT_INSN (insn))
     {
       int log;
 
       INSN_SHUID (insn) = i++;
       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
-       max_log = 0;
+       {
+         /* reorg might make the first insn of a loop being run once only,
+             and delete the label in front of it.  Then we want to apply
+             the loop alignment to the new label created by reorg, which
+             is separated by the former loop start insn from the
+            NOTE_INSN_LOOP_BEG.  */
+       }
       else if (GET_CODE (insn) == CODE_LABEL)
        {
          rtx next;
@@ -1029,13 +1028,14 @@ shorten_branches (first)
                break;
              }
        }
+      /* Again, we allow NOTE_INSN_LOOP_BEG - INSN - CODE_LABEL
+        sequences in order to handle reorg output efficiently.  */
       else if (GET_CODE (insn) == NOTE
               && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
        {
          rtx label;
 
-         for (label = insn; label && GET_RTX_CLASS (GET_CODE (label)) != 'i';
-              label = NEXT_INSN (label))
+         for (label = insn; label; label = NEXT_INSN (label))
            if (GET_CODE (label) == CODE_LABEL)
              {
                log = LOOP_ALIGN (insn);
@@ -1056,6 +1056,9 @@ shorten_branches (first)
   if (insn_addresses)
     free (insn_addresses);
   insn_addresses = (int *) xmalloc (max_uid * sizeof (int));
+  /* Syntax errors can lead to labels being outside of the main insn stream.
+     Initialize insn_addresses, so that we get reproducible results.  */
+  bzero ((char *)insn_addresses, max_uid * sizeof *insn_addresses);
   if (uid_align)
     free (uid_align);
   uid_align = (rtx *) xmalloc (max_uid * sizeof *uid_align);
@@ -1075,14 +1078,12 @@ shorten_branches (first)
   for (i = MAX_CODE_ALIGN; --i >= 0; )
     align_tab[i] = NULL_RTX;
   seq = get_last_insn ();
-  for (insn_current_address = 0; seq; seq = PREV_INSN (seq))
+  for (; seq; seq = PREV_INSN (seq))
     {
       int uid = INSN_UID (seq);
       int log;
-      int length_align;
       log = (GET_CODE (seq) == CODE_LABEL ? LABEL_TO_ALIGNMENT (seq) : 0);
       uid_align[uid] = align_tab[0];
-      insn_addresses[uid] = --insn_current_address;
       if (log)
        {
          /* Found an alignment label.  */
@@ -1090,14 +1091,63 @@ shorten_branches (first)
          for (i = log - 1; i >= 0; i--)
            align_tab[i] = seq;
        }
-      if (GET_CODE (seq) != INSN || GET_CODE (PATTERN (seq)) != SEQUENCE)
-       insn = seq;
-      else
+    }
+#ifdef CASE_VECTOR_SHORTEN_MODE
+  if (optimize)
+    {
+      /* Look for ADDR_DIFF_VECs, and initialize their minimum and maximum
+         label fields.  */
+
+      int min_shuid = INSN_SHUID (get_insns ()) - 1;
+      int max_shuid = INSN_SHUID (get_last_insn ()) + 1;
+      int rel;
+
+      for (insn = first; insn != 0; insn = NEXT_INSN (insn))
        {
-         insn = XVECEXP (PATTERN (seq), 0, 0);
-         uid = INSN_UID (insn);
+         rtx min_lab = NULL_RTX, max_lab = NULL_RTX, pat;
+         int len, i, min, max, insn_shuid;
+         int min_align;
+         addr_diff_vec_flags flags;
+
+         if (GET_CODE (insn) != JUMP_INSN
+             || GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
+           continue;
+         pat = PATTERN (insn);
+         len = XVECLEN (pat, 1);
+         if (len <= 0)
+           abort ();
+         min_align = MAX_CODE_ALIGN;
+         for (min = max_shuid, max = min_shuid, i = len - 1; i >= 0; i--)
+           {
+             rtx lab = XEXP (XVECEXP (pat, 1, i), 0);
+             int shuid = INSN_SHUID (lab);
+             if (shuid < min)
+               {
+                 min = shuid;
+                 min_lab = lab;
+               }
+             if (shuid > max)
+               {
+                 max = shuid;
+                 max_lab = lab;
+               }
+             if (min_align > LABEL_TO_ALIGNMENT (lab))
+               min_align = LABEL_TO_ALIGNMENT (lab);
+           }
+         XEXP (pat, 2) = gen_rtx_LABEL_REF (VOIDmode, min_lab);
+         XEXP (pat, 3) = gen_rtx_LABEL_REF (VOIDmode, max_lab);
+         insn_shuid = INSN_SHUID (insn);
+         rel = INSN_SHUID (XEXP (XEXP (pat, 0), 0));
+         flags.min_align = min_align;
+         flags.base_after_vec = rel > insn_shuid;
+         flags.min_after_vec  = min > insn_shuid;
+         flags.max_after_vec  = max > insn_shuid;
+         flags.min_after_base = min > rel;
+         flags.max_after_base = max > rel;
+         ADDR_DIFF_VEC_FLAGS (pat) = flags;
        }
     }
+#endif /* CASE_VECTOR_SHORTEN_MODE */
 
 
   /* Compute initial lengths, addresses, and varying flags for each insn.  */
@@ -1115,7 +1165,7 @@ shorten_branches (first)
          if (log)
            {
              int align = 1 << log;
-             int new_address = insn_current_address + align - 1 & -align;
+             int new_address = (insn_current_address + align - 1) & -align;
              insn_lengths[uid] = new_address - insn_current_address;
              insn_current_address = new_address;
            }
@@ -1131,7 +1181,15 @@ shorten_branches (first)
 
       body = PATTERN (insn);
       if (GET_CODE (body) == ADDR_VEC || GET_CODE (body) == ADDR_DIFF_VEC)
-       ; /* This should be handled by LABEL_ALIGN.  */
+       {
+         /* This only takes room if read-only data goes into the text
+            section.  */
+#if !defined(READONLY_DATA_SECTION) || defined(JUMP_TABLES_IN_TEXT_SECTION)
+         insn_lengths[uid] = (XVECLEN (body, GET_CODE (body) == ADDR_DIFF_VEC)
+                              * GET_MODE_SIZE (GET_MODE (body)));
+         /* Alignment is handled by ADDR_VEC_ALIGN.  */
+#endif
+       }
       else if (asm_noperands (body) >= 0)
        insn_lengths[uid] = asm_insn_count (body) * insn_default_length (insn);
       else if (GET_CODE (body) == SEQUENCE)
@@ -1197,11 +1255,9 @@ shorten_branches (first)
           insn = NEXT_INSN (insn))
        {
          int new_length;
-#ifdef SHORTEN_WITH_ADJUST_INSN_LENGTH
 #ifdef ADJUST_INSN_LENGTH
          int tmp_length;
 #endif
-#endif
          int length_align;
 
          uid = INSN_UID (insn);
@@ -1212,7 +1268,7 @@ shorten_branches (first)
              if (log > insn_current_align)
                {
                  int align = 1 << log;
-                 int new_address= insn_current_address + align - 1 & -align;
+                 int new_address= (insn_current_address + align - 1) & -align;
                  insn_lengths[uid] = new_address - insn_current_address;
                  insn_current_align = log;
                  insn_current_address = new_address;
@@ -1230,7 +1286,111 @@ shorten_branches (first)
          insn_last_address = insn_addresses[uid];
          insn_addresses[uid] = insn_current_address;
 
-         if (! varying_length[uid])
+#ifdef CASE_VECTOR_SHORTEN_MODE
+         if (optimize && GET_CODE (insn) == JUMP_INSN
+             && GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
+           {
+             rtx body = PATTERN (insn);
+             int old_length = insn_lengths[uid];
+             rtx rel_lab = XEXP (XEXP (body, 0), 0);
+             rtx min_lab = XEXP (XEXP (body, 2), 0);
+             rtx max_lab = XEXP (XEXP (body, 3), 0);
+             addr_diff_vec_flags flags = ADDR_DIFF_VEC_FLAGS (body);
+             int rel_addr = insn_addresses[INSN_UID (rel_lab)];
+             int min_addr = insn_addresses[INSN_UID (min_lab)];
+             int max_addr = insn_addresses[INSN_UID (max_lab)];
+             rtx prev;
+             int rel_align = 0;
+
+             /* Try to find a known alignment for rel_lab.  */
+             for (prev = rel_lab;
+                  prev
+                  && ! insn_lengths[INSN_UID (prev)]
+                  && ! (varying_length[INSN_UID (prev)] & 1);
+                  prev = PREV_INSN (prev))
+               if (varying_length[INSN_UID (prev)] & 2)
+                 {
+                   rel_align = LABEL_TO_ALIGNMENT (prev);
+                   break;
+                 }
+
+             /* See the comment on addr_diff_vec_flags in rtl.h for the
+                meaning of the flags values.  base: REL_LAB   vec: INSN  */
+             /* Anything after INSN has still addresses from the last
+                pass; adjust these so that they reflect our current
+                estimate for this pass.  */
+             if (flags.base_after_vec)
+               rel_addr += insn_current_address - insn_last_address;
+             if (flags.min_after_vec)
+               min_addr += insn_current_address - insn_last_address;
+             if (flags.max_after_vec)
+               max_addr += insn_current_address - insn_last_address;
+             /* We want to know the worst case, i.e. lowest possible value
+                for the offset of MIN_LAB.  If MIN_LAB is after REL_LAB,
+                its offset is positive, and we have to be wary of code shrink;
+                otherwise, it is negative, and we have to be vary of code
+                size increase.  */
+             if (flags.min_after_base)
+               {
+                 /* If INSN is between REL_LAB and MIN_LAB, the size
+                    changes we are about to make can change the alignment
+                    within the observed offset, therefore we have to break
+                    it up into two parts that are independent.  */
+                 if (! flags.base_after_vec && flags.min_after_vec)
+                   {
+                     min_addr -= align_fuzz (rel_lab, insn, rel_align, 0);
+                     min_addr -= align_fuzz (insn, min_lab, 0, 0);
+                   }
+                 else
+                   min_addr -= align_fuzz (rel_lab, min_lab, rel_align, 0);
+               }
+             else
+               {
+                 if (flags.base_after_vec && ! flags.min_after_vec)
+                   {
+                     min_addr -= align_fuzz (min_lab, insn, 0, ~0);
+                     min_addr -= align_fuzz (insn, rel_lab, 0, ~0);
+                   }
+                 else
+                   min_addr -= align_fuzz (min_lab, rel_lab, 0, ~0);
+               }
+             /* Likewise, determine the highest lowest possible value
+                for the offset of MAX_LAB.  */
+             if (flags.max_after_base)
+               {
+                 if (! flags.base_after_vec && flags.max_after_vec)
+                   {
+                     max_addr += align_fuzz (rel_lab, insn, rel_align, ~0);
+                     max_addr += align_fuzz (insn, max_lab, 0, ~0);
+                   }
+                 else
+                   max_addr += align_fuzz (rel_lab, max_lab, rel_align, ~0);
+               }
+             else
+               {
+                 if (flags.base_after_vec && ! flags.max_after_vec)
+                   {
+                     max_addr += align_fuzz (max_lab, insn, 0, 0);
+                     max_addr += align_fuzz (insn, rel_lab, 0, 0);
+                   }
+                 else
+                   max_addr += align_fuzz (max_lab, rel_lab, 0, 0);
+               }
+             PUT_MODE (body, CASE_VECTOR_SHORTEN_MODE (min_addr - rel_addr,
+                                                       max_addr - rel_addr,
+                                                       body));
+#if !defined(READONLY_DATA_SECTION) || defined(JUMP_TABLES_IN_TEXT_SECTION)
+             insn_lengths[uid]
+               = (XVECLEN (body, 1) * GET_MODE_SIZE (GET_MODE (body)));
+             insn_current_address += insn_lengths[uid];
+             if (insn_lengths[uid] != old_length)
+               something_changed = 1;
+#endif
+             continue;
+           }
+#endif /* CASE_VECTOR_SHORTEN_MODE */
+
+         if (! (varying_length[uid]))
            {
              insn_current_address += insn_lengths[uid];
              continue;
@@ -1271,14 +1431,12 @@ shorten_branches (first)
              insn_current_address += new_length;
            }
 
-#ifdef SHORTEN_WITH_ADJUST_INSN_LENGTH
 #ifdef ADJUST_INSN_LENGTH
          /* If needed, do any adjustment.  */
          tmp_length = new_length;
          ADJUST_INSN_LENGTH (insn, new_length);
          insn_current_address += (new_length - tmp_length);
 #endif
-#endif
 
          if (new_length != insn_lengths[uid])
            {
@@ -1688,6 +1846,7 @@ final (first, file, optimize, prescan)
 {
   register rtx insn;
   int max_line = 0;
+  int max_uid = 0;
 
   last_ignored_compare = 0;
   new_block = 1;
@@ -1732,8 +1891,16 @@ final (first, file, optimize, prescan)
   bzero (line_note_exists, max_line + 1);
 
   for (insn = first; insn; insn = NEXT_INSN (insn))
-    if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
-      line_note_exists[NOTE_LINE_NUMBER (insn)] = 1;
+    {
+      if (INSN_UID (insn) > max_uid)       /* find largest UID */
+        max_uid = INSN_UID (insn);
+      if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
+        line_note_exists[NOTE_LINE_NUMBER (insn)] = 1;
+    }
+
+  /* Initialize insn_eh_region table if eh is being used. */
+  
+  init_insn_eh_region (first, max_uid);
 
   init_recog ();
 
@@ -1752,6 +1919,8 @@ final (first, file, optimize, prescan)
      if the last insn was a conditional branch.  */
   if (profile_block_flag && new_block)
     add_bb (file);
+
+  free_insn_eh_region ();
 }
 \f
 /* The final scan for one insn, INSN.
@@ -2011,12 +2180,15 @@ final_scan_insn (insn, file, optimize, prescan, nopeepholes)
       break;
 
     case CODE_LABEL:
-      {
-       int align = LABEL_TO_ALIGNMENT (insn);
+      /* The target port might emit labels in the output function for
+        some insn, e.g. sh.c output_branchy_insn.  */
+      if (CODE_LABEL_NUMBER (insn) <= max_labelno)
+       {
+         int align = LABEL_TO_ALIGNMENT (insn);
 
-       if (align && NEXT_INSN (insn))
-         ASM_OUTPUT_ALIGN (file, align);
-      }
+         if (align && NEXT_INSN (insn))
+           ASM_OUTPUT_ALIGN (file, align);
+       }
       CC_STATUS_INIT;
       if (prescan > 0)
        break;
@@ -2141,6 +2313,7 @@ final_scan_insn (insn, file, optimize, prescan, nopeepholes)
 #ifdef ASM_OUTPUT_ADDR_DIFF_ELT
                    ASM_OUTPUT_ADDR_DIFF_ELT
                      (file,
+                      body,
                       CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 1, idx), 0)),
                       CODE_LABEL_NUMBER (XEXP (XEXP (body, 0), 0)));
 #else
@@ -2182,7 +2355,7 @@ final_scan_insn (insn, file, optimize, prescan, nopeepholes)
        /* Detect `asm' construct with operands.  */
        if (asm_noperands (body) >= 0)
          {
-           int noperands = asm_noperands (body);
+           unsigned int noperands = asm_noperands (body);
            rtx *ops = (rtx *) alloca (noperands * sizeof (rtx));
            char *string;
 
@@ -2718,9 +2891,20 @@ alter_subreg (x)
 
   if (GET_CODE (y) == REG)
     {
-      /* If the containing reg really gets a hard reg, so do we.  */
+      /* If the word size is larger than the size of this register,
+        adjust the register number to compensate.  */
+      /* ??? Note that this just catches stragglers created by/for
+        integrate.  It would be better if we either caught these
+        earlier, or kept _all_ subregs until now and eliminate
+        gen_lowpart and friends.  */
+
       PUT_CODE (x, REG);
+#ifdef ALTER_HARD_SUBREG
+      REGNO (x) = ALTER_HARD_SUBREG(GET_MODE (x), SUBREG_WORD (x),
+                                   GET_MODE (y), REGNO (y));
+#else
       REGNO (x) = REGNO (y) + SUBREG_WORD (x);
+#endif
     }
   else if (GET_CODE (y) == MEM)
     {
@@ -3071,7 +3255,7 @@ output_asm_insn (template, operands)
 
            if (! (*p >= '0' && *p <= '9'))
              output_operand_lossage ("operand number missing after %-letter");
-           else if (this_is_asm_operands && c >= (unsigned) insn_noperands)
+           else if (this_is_asm_operands && (c < 0 || (unsigned int) c >= insn_noperands))
              output_operand_lossage ("operand number out of range");
            else if (letter == 'l')
              output_asm_label (operands[c]);
@@ -3104,7 +3288,7 @@ output_asm_insn (template, operands)
        else if (*p >= '0' && *p <= '9')
          {
            c = atoi (p);
-           if (this_is_asm_operands && c >= (unsigned) insn_noperands)
+           if (this_is_asm_operands && (c < 0 || (unsigned int) c >= insn_noperands))
              output_operand_lossage ("operand number out of range");
            else
              output_operand (operands[c], 0);