OSDN Git Service

PR optimization/13424 (hppa), bootstrap/14462, c/14828
[pf3gnuchains/gcc-fork.git] / gcc / builtins.c
index 895c835..33df5ec 100644 (file)
@@ -261,7 +261,7 @@ c_strlen (tree src, int only_value)
 
       len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
       len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
-      if (tree_int_cst_equal (len1, len2))      
+      if (tree_int_cst_equal (len1, len2))
        return len1;
     }
 
@@ -490,13 +490,9 @@ expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
      the buffer and use the rest of it for the stack save area, which
      is machine-dependent.  */
 
-#ifndef BUILTIN_SETJMP_FRAME_VALUE
-#define BUILTIN_SETJMP_FRAME_VALUE virtual_stack_vars_rtx
-#endif
-
   mem = gen_rtx_MEM (Pmode, buf_addr);
   set_mem_alias_set (mem, setjmp_alias_set);
-  emit_move_insn (mem, BUILTIN_SETJMP_FRAME_VALUE);
+  emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
 
   mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
   set_mem_alias_set (mem, setjmp_alias_set);
@@ -883,20 +879,6 @@ static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
    used for calling a function.  */
 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
 
-/* Return the offset of register REGNO into the block returned by
-   __builtin_apply_args.  This is not declared static, since it is
-   needed in objc-act.c.  */
-
-int
-apply_args_register_offset (int regno)
-{
-  apply_args_size ();
-
-  /* Arguments are always put in outgoing registers (in the argument
-     block) if such make sense.  */
-  return apply_args_reg_offset[OUTGOING_REGNO (regno)];
-}
-
 /* Return the size required for the block returned by __builtin_apply_args,
    and initialize apply_args_mode.  */
 
@@ -922,42 +904,8 @@ apply_args_size (void)
       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
        if (FUNCTION_ARG_REGNO_P (regno))
          {
-           /* Search for the proper mode for copying this register's
-              value.  I'm not sure this is right, but it works so far.  */
-           enum machine_mode best_mode = VOIDmode;
-
-           for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
-                mode != VOIDmode;
-                mode = GET_MODE_WIDER_MODE (mode))
-             if (HARD_REGNO_MODE_OK (regno, mode)
-                 && hard_regno_nregs[regno][mode] == 1)
-               best_mode = mode;
-
-           if (best_mode == VOIDmode)
-             for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
-                  mode != VOIDmode;
-                  mode = GET_MODE_WIDER_MODE (mode))
-               if (HARD_REGNO_MODE_OK (regno, mode)
-                   && have_insn_for (SET, mode))
-                 best_mode = mode;
-
-           if (best_mode == VOIDmode)
-             for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
-                  mode != VOIDmode;
-                  mode = GET_MODE_WIDER_MODE (mode))
-               if (HARD_REGNO_MODE_OK (regno, mode)
-                   && have_insn_for (SET, mode))
-                 best_mode = mode;
-
-           if (best_mode == VOIDmode)
-             for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
-                  mode != VOIDmode;
-                  mode = GET_MODE_WIDER_MODE (mode))
-               if (HARD_REGNO_MODE_OK (regno, mode)
-                   && have_insn_for (SET, mode))
-                 best_mode = mode;
-
-           mode = best_mode;
+           mode = reg_raw_mode[regno];
+
            if (mode == VOIDmode)
              abort ();
 
@@ -995,41 +943,8 @@ apply_result_size (void)
       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
        if (FUNCTION_VALUE_REGNO_P (regno))
          {
-           /* Search for the proper mode for copying this register's
-              value.  I'm not sure this is right, but it works so far.  */
-           enum machine_mode best_mode = VOIDmode;
-
-           for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
-                mode != TImode;
-                mode = GET_MODE_WIDER_MODE (mode))
-             if (HARD_REGNO_MODE_OK (regno, mode))
-               best_mode = mode;
-
-           if (best_mode == VOIDmode)
-             for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
-                  mode != VOIDmode;
-                  mode = GET_MODE_WIDER_MODE (mode))
-               if (HARD_REGNO_MODE_OK (regno, mode)
-                   && have_insn_for (SET, mode))
-                 best_mode = mode;
-
-           if (best_mode == VOIDmode)
-             for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
-                  mode != VOIDmode;
-                  mode = GET_MODE_WIDER_MODE (mode))
-               if (HARD_REGNO_MODE_OK (regno, mode)
-                   && have_insn_for (SET, mode))
-                 best_mode = mode;
-
-           if (best_mode == VOIDmode)
-             for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
-                  mode != VOIDmode;
-                  mode = GET_MODE_WIDER_MODE (mode))
-               if (HARD_REGNO_MODE_OK (regno, mode)
-                   && have_insn_for (SET, mode))
-                 best_mode = mode;
-
-           mode = best_mode;
+           mode = reg_raw_mode[regno];
+
            if (mode == VOIDmode)
              abort ();
 
@@ -1127,7 +1042,7 @@ expand_builtin_apply_args_1 (void)
                     NULL_RTX);
 #endif
   emit_move_insn (adjust_address (registers, Pmode, 0), tem);
-  
+
   size = GET_MODE_SIZE (Pmode);
 
   /* Save the structure value address unless this is passed as an
@@ -1647,6 +1562,17 @@ expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
     case BUILT_IN_EXPF:
     case BUILT_IN_EXPL:
       errno_set = true; builtin_optab = exp_optab; break;
+    case BUILT_IN_EXP10:
+    case BUILT_IN_EXP10F:
+    case BUILT_IN_EXP10L:
+    case BUILT_IN_POW10:
+    case BUILT_IN_POW10F:
+    case BUILT_IN_POW10L:
+      errno_set = true; builtin_optab = exp10_optab; break;
+    case BUILT_IN_EXP2:
+    case BUILT_IN_EXP2F:
+    case BUILT_IN_EXP2L:
+      errno_set = true; builtin_optab = exp2_optab; break;
     case BUILT_IN_LOG:
     case BUILT_IN_LOGF:
     case BUILT_IN_LOGL:
@@ -1902,7 +1828,7 @@ expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
    multiplications to inline before calling the system library's pow
    function.  powi(x,n) requires at worst 2*bits(n)-2 multiplications,
    so this default never requires calling pow, powf or powl.  */
+
 #ifndef POWI_MAX_MULTS
 #define POWI_MAX_MULTS  (2*HOST_BITS_PER_WIDE_INT-2)
 #endif
@@ -2019,7 +1945,7 @@ powi_cost (HOST_WIDE_INT n)
          result++;
        }
     }
-  
+
   return result + powi_lookup_cost (val, cache);
 }
 
@@ -2068,7 +1994,7 @@ expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
 /* Expand the RTL to evaluate powi(x,n) in mode MODE.  X is the
    floating point operand in mode MODE, and N is the exponent.  This
    function needs to be kept in sync with powi_cost above.  */
-   
+
 static rtx
 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
 {
@@ -2629,7 +2555,7 @@ expand_builtin_mempcpy (tree arglist, rtx target, enum machine_mode mode,
       /* If LEN is not constant, call the normal function.  */
       if (! host_integerp (len, 1))
        return 0;
-  
+
       /* If the LEN parameter is zero, return DEST.  */
       if (tree_low_cst (len, 1) == 0)
        {
@@ -3202,7 +3128,7 @@ expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
       insn_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
     else
 #endif
-      return 0;     
+      return 0;
 
     /* If we don't have POINTER_TYPE, call the function.  */
     if (arg1_align == 0 || arg2_align == 0)
@@ -3240,7 +3166,7 @@ expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
                               XEXP (arg1_rtx, 0), Pmode,
                               XEXP (arg2_rtx, 0), Pmode,
                               convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
-                                               TREE_UNSIGNED (sizetype)),
+                                               TYPE_UNSIGNED (sizetype)),
                               TYPE_MODE (sizetype));
 
     /* Return the value in the proper mode for this function.  */
@@ -3604,7 +3530,7 @@ expand_builtin_strcat (tree arglist, rtx target, enum machine_mode mode)
                  pieces into (dst + strlen(dst)).  */
              tree newdst, arglist,
                strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
-             
+
              /* This is the length argument.  */
              arglist = build_tree_list (NULL_TREE,
                                         fold (size_binop (PLUS_EXPR,
@@ -3612,7 +3538,7 @@ expand_builtin_strcat (tree arglist, rtx target, enum machine_mode mode)
                                                           ssize_int (1))));
              /* Prepend src argument.  */
              arglist = tree_cons (NULL_TREE, src, arglist);
-             
+
              /* We're going to use dst more than once.  */
              dst = save_expr (dst);
 
@@ -3626,7 +3552,7 @@ expand_builtin_strcat (tree arglist, rtx target, enum machine_mode mode)
 
              /* Prepend the new dst argument.  */
              arglist = tree_cons (NULL_TREE, newdst, arglist);
-             
+
              /* We don't want to get turned into a memcpy if the
                  target is const0_rtx, i.e. when the return value
                  isn't used.  That would produce pessimized code so
@@ -4097,7 +4023,7 @@ expand_builtin_va_arg (tree valist, tree type)
 
   /* Generate a diagnostic for requesting data of a type that cannot
      be passed through `...' due to type promotion at the call site.  */
-  else if ((promoted_type = (*lang_hooks.types.type_promotes_to) (type))
+  else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
           != type)
     {
       const char *name = "<anonymous type>", *pname = 0;
@@ -4645,7 +4571,7 @@ build_string_literal (int len, const char *str)
 /* Expand a call to printf or printf_unlocked with argument list ARGLIST.
    Return 0 if a normal call should be emitted rather than transforming
    the function inline.  If convenient, the result should be placed in
-   TARGET with mode MODE.  UNLOCKED indicates this is a printf_unlocked 
+   TARGET with mode MODE.  UNLOCKED indicates this is a printf_unlocked
    call.  */
 static rtx
 expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode,
@@ -4724,7 +4650,7 @@ expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode,
            {
              /* Create a NUL-terminated string that's one char shorter
                 than the original, stripping off the trailing '\n'.  */
-             char *newstr = (char *) alloca (len);
+             char *newstr = alloca (len);
              memcpy (newstr, fmt_str, len - 1);
              newstr[len - 1] = 0;
 
@@ -4748,7 +4674,7 @@ expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode,
 /* Expand a call to fprintf or fprintf_unlocked with argument list ARGLIST.
    Return 0 if a normal call should be emitted rather than transforming
    the function inline.  If convenient, the result should be placed in
-   TARGET with mode MODE.  UNLOCKED indicates this is a fprintf_unlocked 
+   TARGET with mode MODE.  UNLOCKED indicates this is a fprintf_unlocked
    call.  */
 static rtx
 expand_builtin_fprintf (tree arglist, rtx target, enum machine_mode mode,
@@ -5035,7 +4961,7 @@ expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
   emit_queue ();
 
   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
-    return (*targetm.expand_builtin) (exp, target, subtarget, mode, ignore);
+    return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
 
   /* When not optimizing, generate calls to library functions for a certain
      set of builtins.  */
@@ -5125,6 +5051,15 @@ expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
     case BUILT_IN_EXP:
     case BUILT_IN_EXPF:
     case BUILT_IN_EXPL:
+    case BUILT_IN_EXP10:
+    case BUILT_IN_EXP10F:
+    case BUILT_IN_EXP10L:
+    case BUILT_IN_POW10:
+    case BUILT_IN_POW10F:
+    case BUILT_IN_POW10L:
+    case BUILT_IN_EXP2:
+    case BUILT_IN_EXP2F:
+    case BUILT_IN_EXP2L:
     case BUILT_IN_LOG:
     case BUILT_IN_LOGF:
     case BUILT_IN_LOGL:
@@ -5793,6 +5728,9 @@ integer_valued_real_p (tree t)
        case BUILT_IN_NEARBYINT:
        case BUILT_IN_NEARBYINTF:
        case BUILT_IN_NEARBYINTL:
+       case BUILT_IN_RINT:
+       case BUILT_IN_RINTF:
+       case BUILT_IN_RINTL:
        case BUILT_IN_ROUND:
        case BUILT_IN_ROUNDF:
        case BUILT_IN_ROUNDL:
@@ -6190,8 +6128,8 @@ real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
 }
 
 /* A subroutine of fold_builtin to fold the various logarithmic
-   functions.  EXP is the CALL_EXPR of a call to a builtin log*
-   function.  VALUE is the base of the log* function.  */
+   functions.  EXP is the CALL_EXPR of a call to a builtin logN
+   function.  VALUE is the base of the logN function.  */
 
 static tree
 fold_builtin_logarithm (tree exp, const REAL_VALUE_TYPE *value)
@@ -6204,8 +6142,8 @@ fold_builtin_logarithm (tree exp, const REAL_VALUE_TYPE *value)
       tree type = TREE_TYPE (TREE_TYPE (fndecl));
       tree arg = TREE_VALUE (arglist);
       const enum built_in_function fcode = builtin_mathfn_code (arg);
-       
-      /* Optimize log*(1.0) = 0.0.  */
+
+      /* Optimize logN(1.0) = 0.0.  */
       if (real_onep (arg))
        return build_real (type, dconst0);
 
@@ -6219,7 +6157,7 @@ fold_builtin_logarithm (tree exp, const REAL_VALUE_TYPE *value)
          if (real_dconstp (arg, &value_truncate))
            return build_real (type, dconst1);
        }
-      
+
       /* Special case, optimize logN(expN(x)) = x.  */
       if (flag_unsafe_math_optimizations
          && ((value == &dconste
@@ -6230,19 +6168,16 @@ fold_builtin_logarithm (tree exp, const REAL_VALUE_TYPE *value)
                  && (fcode == BUILT_IN_EXP2
                      || fcode == BUILT_IN_EXP2F
                      || fcode == BUILT_IN_EXP2L))
-             || (value == &dconst10
-                 && (fcode == BUILT_IN_EXP10
-                     || fcode == BUILT_IN_EXP10F
-                     || fcode == BUILT_IN_EXP10L))))
+             || (value == &dconst10 && (BUILTIN_EXP10_P (fcode)))))
        return convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
 
-      /* Optimize log*(func()) for various exponential functions.  We
+      /* Optimize logN(func()) for various exponential functions.  We
          want to determine the value "x" and the power "exponent" in
          order to transform logN(x**exponent) into exponent*logN(x).  */
       if (flag_unsafe_math_optimizations)
         {
          tree exponent = 0, x = 0;
-         
+
          switch (fcode)
          {
          case BUILT_IN_EXP:
@@ -6309,7 +6244,7 @@ fold_builtin_logarithm (tree exp, const REAL_VALUE_TYPE *value)
 
   return 0;
 }
-         
+
 /* A subroutine of fold_builtin to fold the various exponent
    functions.  EXP is the CALL_EXPR of a call to a builtin function.
    VALUE is the value which will be raised to a power.  */
@@ -6743,12 +6678,9 @@ fold_builtin (tree exp)
                return build_real (type, r);
            }
 
-         /* Optimize sqrt(exp(x)) = exp(x*0.5).  */
+         /* Optimize sqrt(expN(x)) = expN(x*0.5).  */
          fcode = builtin_mathfn_code (arg);
-         if (flag_unsafe_math_optimizations
-             && (fcode == BUILT_IN_EXP
-                 || fcode == BUILT_IN_EXPF
-                 || fcode == BUILT_IN_EXPL))
+         if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
            {
              tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
              arg = fold (build (MULT_EXPR, type,
@@ -6758,6 +6690,29 @@ fold_builtin (tree exp)
              return build_function_call_expr (expfn, arglist);
            }
 
+         /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)).  */
+         if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
+           {
+             tree powfn = mathfn_built_in (type, BUILT_IN_POW);
+             
+             if (powfn)
+               {
+                 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
+                 tree tree_root;
+                 /* The inner root was either sqrt or cbrt.  */
+                 REAL_VALUE_TYPE dconstroot =
+                   BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
+                 
+                 /* Adjust for the outer root.  */
+                 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
+                 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
+                 tree_root = build_real (type, dconstroot);
+                 arglist = tree_cons (NULL_TREE, arg0,
+                                      build_tree_list (NULL_TREE, tree_root));
+                 return build_function_call_expr (powfn, arglist);
+               }
+           }
+
          /* Optimize sqrt(pow(x,y)) = pow(x,y*0.5).  */
          if (flag_unsafe_math_optimizations
              && (fcode == BUILT_IN_POW
@@ -6776,6 +6731,56 @@ fold_builtin (tree exp)
        }
       break;
 
+    case BUILT_IN_CBRT:
+    case BUILT_IN_CBRTF:
+    case BUILT_IN_CBRTL:
+      if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
+       {
+         tree arg = TREE_VALUE (arglist);
+         const enum built_in_function fcode = builtin_mathfn_code (arg);
+
+         /* Optimize cbrt of constant value.  */
+         if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
+           return arg;
+
+         /* Optimize cbrt(expN(x)) -> expN(x/3).  */
+         if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
+           {
+             tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
+             const REAL_VALUE_TYPE third_trunc =
+               real_value_truncate (TYPE_MODE (type), dconstthird);
+             arg = fold (build (MULT_EXPR, type,
+                                TREE_VALUE (TREE_OPERAND (arg, 1)),
+                                build_real (type, third_trunc)));
+             arglist = build_tree_list (NULL_TREE, arg);
+             return build_function_call_expr (expfn, arglist);
+           }
+
+         /* Optimize cbrt(sqrt(x)) -> pow(x,1/6).  */
+         /* We don't optimize cbrt(cbrt(x)) -> pow(x,1/9) because if
+             x is negative pow will error but cbrt won't.  */
+         if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
+           {
+             tree powfn = mathfn_built_in (type, BUILT_IN_POW);
+
+             if (powfn)
+               {
+                 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
+                 tree tree_root;
+                 REAL_VALUE_TYPE dconstroot = dconstthird;
+
+                 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
+                 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
+                 tree_root = build_real (type, dconstroot);
+                 arglist = tree_cons (NULL_TREE, arg0,
+                                      build_tree_list (NULL_TREE, tree_root));
+                 return build_function_call_expr (powfn, arglist);
+               }
+             
+           }
+       }
+      break;
+
     case BUILT_IN_SIN:
     case BUILT_IN_SINF:
     case BUILT_IN_SINL:
@@ -6880,7 +6885,7 @@ fold_builtin (tree exp)
              REAL_VALUE_TYPE cst;
 
              real_convert (&cst, TYPE_MODE (type), &dconstpi);
-             cst.exp -= 2;
+             SET_REAL_EXP (&cst, REAL_EXP (&cst) - 2);
              return build_real (type, cst);
            }
        }
@@ -6966,12 +6971,9 @@ fold_builtin (tree exp)
                }
            }
 
-         /* Optimize pow(exp(x),y) = exp(x*y).  */
+         /* Optimize pow(expN(x),y) = expN(x*y).  */
          fcode = builtin_mathfn_code (arg0);
-         if (flag_unsafe_math_optimizations
-             && (fcode == BUILT_IN_EXP
-                 || fcode == BUILT_IN_EXPF
-                 || fcode == BUILT_IN_EXPL))
+         if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
            {
              tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
              tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
@@ -6981,10 +6983,7 @@ fold_builtin (tree exp)
            }
 
          /* Optimize pow(sqrt(x),y) = pow(x,y*0.5).  */
-         if (flag_unsafe_math_optimizations
-             && (fcode == BUILT_IN_SQRT
-                 || fcode == BUILT_IN_SQRTF
-                 || fcode == BUILT_IN_SQRTL))
+         if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
            {
              tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
              tree narg1 = fold (build (MULT_EXPR, type, arg1,
@@ -7054,6 +7053,9 @@ fold_builtin (tree exp)
     case BUILT_IN_NEARBYINT:
     case BUILT_IN_NEARBYINTF:
     case BUILT_IN_NEARBYINTL:
+    case BUILT_IN_RINT:
+    case BUILT_IN_RINTF:
+    case BUILT_IN_RINTL:
       return fold_trunc_transparent_mathfn (exp);
 
     case BUILT_IN_FFS: