OSDN Git Service

2012-05-22 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / convert.c
1 /* Utility routines for data type conversion for GCC.
2    Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1997, 1998,
3    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22
23 /* These routines are somewhat language-independent utility function
24    intended to be called by the language-specific convert () functions.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "flags.h"
32 #include "convert.h"
33 #include "diagnostic-core.h"
34 #include "langhooks.h"
35
36 /* Convert EXPR to some pointer or reference type TYPE.
37    EXPR must be pointer, reference, integer, enumeral, or literal zero;
38    in other cases error is called.  */
39
40 tree
41 convert_to_pointer (tree type, tree expr)
42 {
43   location_t loc = EXPR_LOCATION (expr);
44   if (TREE_TYPE (expr) == type)
45     return expr;
46
47   switch (TREE_CODE (TREE_TYPE (expr)))
48     {
49     case POINTER_TYPE:
50     case REFERENCE_TYPE:
51       {
52         /* If the pointers point to different address spaces, conversion needs
53            to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR.  */
54         addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
55         addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
56
57         if (to_as == from_as)
58           return fold_build1_loc (loc, NOP_EXPR, type, expr);
59         else
60           return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
61       }
62
63     case INTEGER_TYPE:
64     case ENUMERAL_TYPE:
65     case BOOLEAN_TYPE:
66       {
67         /* If the input precision differs from the target pointer type
68            precision, first convert the input expression to an integer type of
69            the target precision.  Some targets, e.g. VMS, need several pointer
70            sizes to coexist so the latter isn't necessarily POINTER_SIZE.  */
71         unsigned int pprec = TYPE_PRECISION (type);
72         unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
73
74         if (eprec != pprec)
75           expr = fold_build1_loc (loc, NOP_EXPR,
76                               lang_hooks.types.type_for_size (pprec, 0),
77                               expr);
78       }
79
80       return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
81
82     default:
83       error ("cannot convert to a pointer type");
84       return convert_to_pointer (type, integer_zero_node);
85     }
86 }
87
88 /* Avoid any floating point extensions from EXP.  */
89 tree
90 strip_float_extensions (tree exp)
91 {
92   tree sub, expt, subt;
93
94   /*  For floating point constant look up the narrowest type that can hold
95       it properly and handle it like (type)(narrowest_type)constant.
96       This way we can optimize for instance a=a*2.0 where "a" is float
97       but 2.0 is double constant.  */
98   if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
99     {
100       REAL_VALUE_TYPE orig;
101       tree type = NULL;
102
103       orig = TREE_REAL_CST (exp);
104       if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
105           && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
106         type = float_type_node;
107       else if (TYPE_PRECISION (TREE_TYPE (exp))
108                > TYPE_PRECISION (double_type_node)
109                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
110         type = double_type_node;
111       if (type)
112         return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
113     }
114
115   if (!CONVERT_EXPR_P (exp))
116     return exp;
117
118   sub = TREE_OPERAND (exp, 0);
119   subt = TREE_TYPE (sub);
120   expt = TREE_TYPE (exp);
121
122   if (!FLOAT_TYPE_P (subt))
123     return exp;
124
125   if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
126     return exp;
127
128   if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
129     return exp;
130
131   return strip_float_extensions (sub);
132 }
133
134
135 /* Convert EXPR to some floating-point type TYPE.
136
137    EXPR must be float, fixed-point, integer, or enumeral;
138    in other cases error is called.  */
139
140 tree
141 convert_to_real (tree type, tree expr)
142 {
143   enum built_in_function fcode = builtin_mathfn_code (expr);
144   tree itype = TREE_TYPE (expr);
145
146   /* Disable until we figure out how to decide whether the functions are
147      present in runtime.  */
148   /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
149   if (optimize
150       && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
151           || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
152     {
153       switch (fcode)
154         {
155 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
156           CASE_MATHFN (COSH)
157           CASE_MATHFN (EXP)
158           CASE_MATHFN (EXP10)
159           CASE_MATHFN (EXP2)
160           CASE_MATHFN (EXPM1)
161           CASE_MATHFN (GAMMA)
162           CASE_MATHFN (J0)
163           CASE_MATHFN (J1)
164           CASE_MATHFN (LGAMMA)
165           CASE_MATHFN (POW10)
166           CASE_MATHFN (SINH)
167           CASE_MATHFN (TGAMMA)
168           CASE_MATHFN (Y0)
169           CASE_MATHFN (Y1)
170             /* The above functions may set errno differently with float
171                input or output so this transformation is not safe with
172                -fmath-errno.  */
173             if (flag_errno_math)
174               break;
175           CASE_MATHFN (ACOS)
176           CASE_MATHFN (ACOSH)
177           CASE_MATHFN (ASIN)
178           CASE_MATHFN (ASINH)
179           CASE_MATHFN (ATAN)
180           CASE_MATHFN (ATANH)
181           CASE_MATHFN (CBRT)
182           CASE_MATHFN (COS)
183           CASE_MATHFN (ERF)
184           CASE_MATHFN (ERFC)
185           CASE_MATHFN (FABS)
186           CASE_MATHFN (LOG)
187           CASE_MATHFN (LOG10)
188           CASE_MATHFN (LOG2)
189           CASE_MATHFN (LOG1P)
190           CASE_MATHFN (LOGB)
191           CASE_MATHFN (SIN)
192           CASE_MATHFN (SQRT)
193           CASE_MATHFN (TAN)
194           CASE_MATHFN (TANH)
195 #undef CASE_MATHFN
196             {
197               tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
198               tree newtype = type;
199
200               /* We have (outertype)sqrt((innertype)x).  Choose the wider mode from
201                  the both as the safe type for operation.  */
202               if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
203                 newtype = TREE_TYPE (arg0);
204
205               /* Be careful about integer to fp conversions.
206                  These may overflow still.  */
207               if (FLOAT_TYPE_P (TREE_TYPE (arg0))
208                   && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
209                   && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
210                       || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
211                 {
212                   tree fn = mathfn_built_in (newtype, fcode);
213
214                   if (fn)
215                   {
216                     tree arg = fold (convert_to_real (newtype, arg0));
217                     expr = build_call_expr (fn, 1, arg);
218                     if (newtype == type)
219                       return expr;
220                   }
221                 }
222             }
223         default:
224           break;
225         }
226     }
227   if (optimize
228       && (((fcode == BUILT_IN_FLOORL
229            || fcode == BUILT_IN_CEILL
230            || fcode == BUILT_IN_ROUNDL
231            || fcode == BUILT_IN_RINTL
232            || fcode == BUILT_IN_TRUNCL
233            || fcode == BUILT_IN_NEARBYINTL)
234           && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
235               || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
236           || ((fcode == BUILT_IN_FLOOR
237                || fcode == BUILT_IN_CEIL
238                || fcode == BUILT_IN_ROUND
239                || fcode == BUILT_IN_RINT
240                || fcode == BUILT_IN_TRUNC
241                || fcode == BUILT_IN_NEARBYINT)
242               && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
243     {
244       tree fn = mathfn_built_in (type, fcode);
245
246       if (fn)
247         {
248           tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
249
250           /* Make sure (type)arg0 is an extension, otherwise we could end up
251              changing (float)floor(double d) into floorf((float)d), which is
252              incorrect because (float)d uses round-to-nearest and can round
253              up to the next integer.  */
254           if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
255             return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
256         }
257     }
258
259   /* Propagate the cast into the operation.  */
260   if (itype != type && FLOAT_TYPE_P (type))
261     switch (TREE_CODE (expr))
262       {
263         /* Convert (float)-x into -(float)x.  This is safe for
264            round-to-nearest rounding mode.  */
265         case ABS_EXPR:
266         case NEGATE_EXPR:
267           if (!flag_rounding_math
268               && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
269             return build1 (TREE_CODE (expr), type,
270                            fold (convert_to_real (type,
271                                                   TREE_OPERAND (expr, 0))));
272           break;
273         /* Convert (outertype)((innertype0)a+(innertype1)b)
274            into ((newtype)a+(newtype)b) where newtype
275            is the widest mode from all of these.  */
276         case PLUS_EXPR:
277         case MINUS_EXPR:
278         case MULT_EXPR:
279         case RDIV_EXPR:
280            {
281              tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
282              tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
283
284              if (FLOAT_TYPE_P (TREE_TYPE (arg0))
285                  && FLOAT_TYPE_P (TREE_TYPE (arg1))
286                  && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
287                {
288                   tree newtype = type;
289
290                   if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
291                       || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
292                       || TYPE_MODE (type) == SDmode)
293                     newtype = dfloat32_type_node;
294                   if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
295                       || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
296                       || TYPE_MODE (type) == DDmode)
297                     newtype = dfloat64_type_node;
298                   if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
299                       || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
300                       || TYPE_MODE (type) == TDmode)
301                     newtype = dfloat128_type_node;
302                   if (newtype == dfloat32_type_node
303                       || newtype == dfloat64_type_node
304                       || newtype == dfloat128_type_node)
305                     {
306                       expr = build2 (TREE_CODE (expr), newtype,
307                                      fold (convert_to_real (newtype, arg0)),
308                                      fold (convert_to_real (newtype, arg1)));
309                       if (newtype == type)
310                         return expr;
311                       break;
312                     }
313
314                   if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
315                     newtype = TREE_TYPE (arg0);
316                   if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
317                     newtype = TREE_TYPE (arg1);
318                   /* Sometimes this transformation is safe (cannot
319                      change results through affecting double rounding
320                      cases) and sometimes it is not.  If NEWTYPE is
321                      wider than TYPE, e.g. (float)((long double)double
322                      + (long double)double) converted to
323                      (float)(double + double), the transformation is
324                      unsafe regardless of the details of the types
325                      involved; double rounding can arise if the result
326                      of NEWTYPE arithmetic is a NEWTYPE value half way
327                      between two representable TYPE values but the
328                      exact value is sufficiently different (in the
329                      right direction) for this difference to be
330                      visible in ITYPE arithmetic.  If NEWTYPE is the
331                      same as TYPE, however, the transformation may be
332                      safe depending on the types involved: it is safe
333                      if the ITYPE has strictly more than twice as many
334                      mantissa bits as TYPE, can represent infinities
335                      and NaNs if the TYPE can, and has sufficient
336                      exponent range for the product or ratio of two
337                      values representable in the TYPE to be within the
338                      range of normal values of ITYPE.  */
339                   if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
340                       && (flag_unsafe_math_optimizations
341                           || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
342                               && real_can_shorten_arithmetic (TYPE_MODE (itype),
343                                                               TYPE_MODE (type))
344                               && !excess_precision_type (newtype))))
345                     {
346                       expr = build2 (TREE_CODE (expr), newtype,
347                                      fold (convert_to_real (newtype, arg0)),
348                                      fold (convert_to_real (newtype, arg1)));
349                       if (newtype == type)
350                         return expr;
351                     }
352                }
353            }
354           break;
355         default:
356           break;
357       }
358
359   switch (TREE_CODE (TREE_TYPE (expr)))
360     {
361     case REAL_TYPE:
362       /* Ignore the conversion if we don't need to store intermediate
363          results and neither type is a decimal float.  */
364       return build1 ((flag_float_store
365                      || DECIMAL_FLOAT_TYPE_P (type)
366                      || DECIMAL_FLOAT_TYPE_P (itype))
367                      ? CONVERT_EXPR : NOP_EXPR, type, expr);
368
369     case INTEGER_TYPE:
370     case ENUMERAL_TYPE:
371     case BOOLEAN_TYPE:
372       return build1 (FLOAT_EXPR, type, expr);
373
374     case FIXED_POINT_TYPE:
375       return build1 (FIXED_CONVERT_EXPR, type, expr);
376
377     case COMPLEX_TYPE:
378       return convert (type,
379                       fold_build1 (REALPART_EXPR,
380                                    TREE_TYPE (TREE_TYPE (expr)), expr));
381
382     case POINTER_TYPE:
383     case REFERENCE_TYPE:
384       error ("pointer value used where a floating point value was expected");
385       return convert_to_real (type, integer_zero_node);
386
387     default:
388       error ("aggregate value used where a float was expected");
389       return convert_to_real (type, integer_zero_node);
390     }
391 }
392
393 /* Convert EXPR to some integer (or enum) type TYPE.
394
395    EXPR must be pointer, integer, discrete (enum, char, or bool), float,
396    fixed-point or vector; in other cases error is called.
397
398    The result of this is always supposed to be a newly created tree node
399    not in use in any existing structure.  */
400
401 tree
402 convert_to_integer (tree type, tree expr)
403 {
404   enum tree_code ex_form = TREE_CODE (expr);
405   tree intype = TREE_TYPE (expr);
406   unsigned int inprec = TYPE_PRECISION (intype);
407   unsigned int outprec = TYPE_PRECISION (type);
408
409   /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
410      be.  Consider `enum E = { a, b = (enum E) 3 };'.  */
411   if (!COMPLETE_TYPE_P (type))
412     {
413       error ("conversion to incomplete type");
414       return error_mark_node;
415     }
416
417   /* Convert e.g. (long)round(d) -> lround(d).  */
418   /* If we're converting to char, we may encounter differing behavior
419      between converting from double->char vs double->long->char.
420      We're in "undefined" territory but we prefer to be conservative,
421      so only proceed in "unsafe" math mode.  */
422   if (optimize
423       && (flag_unsafe_math_optimizations
424           || (long_integer_type_node
425               && outprec >= TYPE_PRECISION (long_integer_type_node))))
426     {
427       tree s_expr = strip_float_extensions (expr);
428       tree s_intype = TREE_TYPE (s_expr);
429       const enum built_in_function fcode = builtin_mathfn_code (s_expr);
430       tree fn = 0;
431
432       switch (fcode)
433         {
434         CASE_FLT_FN (BUILT_IN_CEIL):
435           /* Only convert in ISO C99 mode.  */
436           if (!TARGET_C99_FUNCTIONS)
437             break;
438           if (outprec < TYPE_PRECISION (long_integer_type_node)
439               || (outprec == TYPE_PRECISION (long_integer_type_node)
440                   && !TYPE_UNSIGNED (type)))
441             fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
442           else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
443                    && !TYPE_UNSIGNED (type))
444             fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
445           break;
446
447         CASE_FLT_FN (BUILT_IN_FLOOR):
448           /* Only convert in ISO C99 mode.  */
449           if (!TARGET_C99_FUNCTIONS)
450             break;
451           if (outprec < TYPE_PRECISION (long_integer_type_node)
452               || (outprec == TYPE_PRECISION (long_integer_type_node)
453                   && !TYPE_UNSIGNED (type)))
454             fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
455           else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
456                    && !TYPE_UNSIGNED (type))
457             fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
458           break;
459
460         CASE_FLT_FN (BUILT_IN_ROUND):
461           if (outprec < TYPE_PRECISION (long_integer_type_node)
462               || (outprec == TYPE_PRECISION (long_integer_type_node)
463                   && !TYPE_UNSIGNED (type)))
464             fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
465           else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
466                    && !TYPE_UNSIGNED (type))
467             fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
468           break;
469
470         CASE_FLT_FN (BUILT_IN_NEARBYINT):
471           /* Only convert nearbyint* if we can ignore math exceptions.  */
472           if (flag_trapping_math)
473             break;
474           /* ... Fall through ...  */
475         CASE_FLT_FN (BUILT_IN_RINT):
476           if (outprec < TYPE_PRECISION (long_integer_type_node)
477               || (outprec == TYPE_PRECISION (long_integer_type_node)
478                   && !TYPE_UNSIGNED (type)))
479             fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
480           else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
481                    && !TYPE_UNSIGNED (type))
482             fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
483           break;
484
485         CASE_FLT_FN (BUILT_IN_TRUNC):
486           return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
487
488         default:
489           break;
490         }
491
492       if (fn)
493         {
494           tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
495           return convert_to_integer (type, newexpr);
496         }
497     }
498
499   /* Convert (int)logb(d) -> ilogb(d).  */
500   if (optimize
501       && flag_unsafe_math_optimizations
502       && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
503       && integer_type_node
504       && (outprec > TYPE_PRECISION (integer_type_node)
505           || (outprec == TYPE_PRECISION (integer_type_node)
506               && !TYPE_UNSIGNED (type))))
507     {
508       tree s_expr = strip_float_extensions (expr);
509       tree s_intype = TREE_TYPE (s_expr);
510       const enum built_in_function fcode = builtin_mathfn_code (s_expr);
511       tree fn = 0;
512
513       switch (fcode)
514         {
515         CASE_FLT_FN (BUILT_IN_LOGB):
516           fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
517           break;
518
519         default:
520           break;
521         }
522
523       if (fn)
524         {
525           tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
526           return convert_to_integer (type, newexpr);
527         }
528     }
529
530   switch (TREE_CODE (intype))
531     {
532     case POINTER_TYPE:
533     case REFERENCE_TYPE:
534       if (integer_zerop (expr))
535         return build_int_cst (type, 0);
536
537       /* Convert to an unsigned integer of the correct width first, and from
538          there widen/truncate to the required type.  Some targets support the
539          coexistence of multiple valid pointer sizes, so fetch the one we need
540          from the type.  */
541       expr = fold_build1 (CONVERT_EXPR,
542                           lang_hooks.types.type_for_size
543                             (TYPE_PRECISION (intype), 0),
544                           expr);
545       return fold_convert (type, expr);
546
547     case INTEGER_TYPE:
548     case ENUMERAL_TYPE:
549     case BOOLEAN_TYPE:
550     case OFFSET_TYPE:
551       /* If this is a logical operation, which just returns 0 or 1, we can
552          change the type of the expression.  */
553
554       if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
555         {
556           expr = copy_node (expr);
557           TREE_TYPE (expr) = type;
558           return expr;
559         }
560
561       /* If we are widening the type, put in an explicit conversion.
562          Similarly if we are not changing the width.  After this, we know
563          we are truncating EXPR.  */
564
565       else if (outprec >= inprec)
566         {
567           enum tree_code code;
568           tree tem;
569
570           /* If the precision of the EXPR's type is K bits and the
571              destination mode has more bits, and the sign is changing,
572              it is not safe to use a NOP_EXPR.  For example, suppose
573              that EXPR's type is a 3-bit unsigned integer type, the
574              TYPE is a 3-bit signed integer type, and the machine mode
575              for the types is 8-bit QImode.  In that case, the
576              conversion necessitates an explicit sign-extension.  In
577              the signed-to-unsigned case the high-order bits have to
578              be cleared.  */
579           if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
580               && (TYPE_PRECISION (TREE_TYPE (expr))
581                   != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)))))
582             code = CONVERT_EXPR;
583           else
584             code = NOP_EXPR;
585
586           tem = fold_unary (code, type, expr);
587           if (tem)
588             return tem;
589
590           tem = build1 (code, type, expr);
591           TREE_NO_WARNING (tem) = 1;
592           return tem;
593         }
594
595       /* If TYPE is an enumeral type or a type with a precision less
596          than the number of bits in its mode, do the conversion to the
597          type corresponding to its mode, then do a nop conversion
598          to TYPE.  */
599       else if (TREE_CODE (type) == ENUMERAL_TYPE
600                || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
601         return build1 (NOP_EXPR, type,
602                        convert (lang_hooks.types.type_for_mode
603                                 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
604                                 expr));
605
606       /* Here detect when we can distribute the truncation down past some
607          arithmetic.  For example, if adding two longs and converting to an
608          int, we can equally well convert both to ints and then add.
609          For the operations handled here, such truncation distribution
610          is always safe.
611          It is desirable in these cases:
612          1) when truncating down to full-word from a larger size
613          2) when truncating takes no work.
614          3) when at least one operand of the arithmetic has been extended
615          (as by C's default conversions).  In this case we need two conversions
616          if we do the arithmetic as already requested, so we might as well
617          truncate both and then combine.  Perhaps that way we need only one.
618
619          Note that in general we cannot do the arithmetic in a type
620          shorter than the desired result of conversion, even if the operands
621          are both extended from a shorter type, because they might overflow
622          if combined in that type.  The exceptions to this--the times when
623          two narrow values can be combined in their narrow type even to
624          make a wider result--are handled by "shorten" in build_binary_op.  */
625
626       switch (ex_form)
627         {
628         case RSHIFT_EXPR:
629           /* We can pass truncation down through right shifting
630              when the shift count is a nonpositive constant.  */
631           if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
632               && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
633             goto trunc1;
634           break;
635
636         case LSHIFT_EXPR:
637           /* We can pass truncation down through left shifting
638              when the shift count is a nonnegative constant and
639              the target type is unsigned.  */
640           if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
641               && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
642               && TYPE_UNSIGNED (type)
643               && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
644             {
645               /* If shift count is less than the width of the truncated type,
646                  really shift.  */
647               if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
648                 /* In this case, shifting is like multiplication.  */
649                 goto trunc1;
650               else
651                 {
652                   /* If it is >= that width, result is zero.
653                      Handling this with trunc1 would give the wrong result:
654                      (int) ((long long) a << 32) is well defined (as 0)
655                      but (int) a << 32 is undefined and would get a
656                      warning.  */
657
658                   tree t = build_int_cst (type, 0);
659
660                   /* If the original expression had side-effects, we must
661                      preserve it.  */
662                   if (TREE_SIDE_EFFECTS (expr))
663                     return build2 (COMPOUND_EXPR, type, expr, t);
664                   else
665                     return t;
666                 }
667             }
668           break;
669
670         case TRUNC_DIV_EXPR:
671           {
672             tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
673             tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
674
675             /* Don't distribute unless the output precision is at least as big
676                as the actual inputs and it has the same signedness.  */
677             if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
678                 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
679                 /* If signedness of arg0 and arg1 don't match,
680                    we can't necessarily find a type to compare them in.  */
681                 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
682                     == TYPE_UNSIGNED (TREE_TYPE (arg1)))
683                 /* Do not change the sign of the division.  */
684                 && (TYPE_UNSIGNED (TREE_TYPE (expr))
685                     == TYPE_UNSIGNED (TREE_TYPE (arg0)))
686                 /* Either require unsigned division or a division by
687                    a constant that is not -1.  */
688                 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
689                     || (TREE_CODE (arg1) == INTEGER_CST
690                         && !integer_all_onesp (arg1))))
691               goto trunc1;
692             break;
693           }
694
695         case MAX_EXPR:
696         case MIN_EXPR:
697         case MULT_EXPR:
698           {
699             tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
700             tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
701
702             /* Don't distribute unless the output precision is at least as big
703                as the actual inputs.  Otherwise, the comparison of the
704                truncated values will be wrong.  */
705             if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
706                 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
707                 /* If signedness of arg0 and arg1 don't match,
708                    we can't necessarily find a type to compare them in.  */
709                 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
710                     == TYPE_UNSIGNED (TREE_TYPE (arg1))))
711               goto trunc1;
712             break;
713           }
714
715         case PLUS_EXPR:
716         case MINUS_EXPR:
717         case BIT_AND_EXPR:
718         case BIT_IOR_EXPR:
719         case BIT_XOR_EXPR:
720         trunc1:
721           {
722             tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
723             tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
724
725             /* Do not try to narrow operands of pointer subtraction;
726                that will interfere with other folding.  */
727             if (ex_form == MINUS_EXPR
728                 && CONVERT_EXPR_P (arg0)
729                 && CONVERT_EXPR_P (arg1)
730                 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
731                 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
732               break;
733
734             if (outprec >= BITS_PER_WORD
735                 || TRULY_NOOP_TRUNCATION (outprec, inprec)
736                 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
737                 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
738               {
739                 /* Do the arithmetic in type TYPEX,
740                    then convert result to TYPE.  */
741                 tree typex = type;
742
743                 /* Can't do arithmetic in enumeral types
744                    so use an integer type that will hold the values.  */
745                 if (TREE_CODE (typex) == ENUMERAL_TYPE)
746                   typex = lang_hooks.types.type_for_size
747                     (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
748
749                 /* But now perhaps TYPEX is as wide as INPREC.
750                    In that case, do nothing special here.
751                    (Otherwise would recurse infinitely in convert.  */
752                 if (TYPE_PRECISION (typex) != inprec)
753                   {
754                     /* Don't do unsigned arithmetic where signed was wanted,
755                        or vice versa.
756                        Exception: if both of the original operands were
757                        unsigned then we can safely do the work as unsigned.
758                        Exception: shift operations take their type solely
759                        from the first argument.
760                        Exception: the LSHIFT_EXPR case above requires that
761                        we perform this operation unsigned lest we produce
762                        signed-overflow undefinedness.
763                        And we may need to do it as unsigned
764                        if we truncate to the original size.  */
765                     if (TYPE_UNSIGNED (TREE_TYPE (expr))
766                         || (TYPE_UNSIGNED (TREE_TYPE (arg0))
767                             && (TYPE_UNSIGNED (TREE_TYPE (arg1))
768                                 || ex_form == LSHIFT_EXPR
769                                 || ex_form == RSHIFT_EXPR
770                                 || ex_form == LROTATE_EXPR
771                                 || ex_form == RROTATE_EXPR))
772                         || ex_form == LSHIFT_EXPR
773                         /* If we have !flag_wrapv, and either ARG0 or
774                            ARG1 is of a signed type, we have to do
775                            PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
776                            type in case the operation in outprec precision
777                            could overflow.  Otherwise, we would introduce
778                            signed-overflow undefinedness.  */
779                         || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
780                              || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
781                             && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
782                                  > outprec)
783                                 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
784                                     > outprec))
785                             && (ex_form == PLUS_EXPR
786                                 || ex_form == MINUS_EXPR
787                                 || ex_form == MULT_EXPR)))
788                       typex = unsigned_type_for (typex);
789                     else
790                       typex = signed_type_for (typex);
791                     return convert (type,
792                                     fold_build2 (ex_form, typex,
793                                                  convert (typex, arg0),
794                                                  convert (typex, arg1)));
795                   }
796               }
797           }
798           break;
799
800         case NEGATE_EXPR:
801         case BIT_NOT_EXPR:
802           /* This is not correct for ABS_EXPR,
803              since we must test the sign before truncation.  */
804           {
805             tree typex = unsigned_type_for (type);
806             return convert (type,
807                             fold_build1 (ex_form, typex,
808                                          convert (typex,
809                                                   TREE_OPERAND (expr, 0))));
810           }
811
812         case NOP_EXPR:
813           /* Don't introduce a
814              "can't convert between vector values of different size" error.  */
815           if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
816               && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
817                   != GET_MODE_SIZE (TYPE_MODE (type))))
818             break;
819           /* If truncating after truncating, might as well do all at once.
820              If truncating after extending, we may get rid of wasted work.  */
821           return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
822
823         case COND_EXPR:
824           /* It is sometimes worthwhile to push the narrowing down through
825              the conditional and never loses.  A COND_EXPR may have a throw
826              as one operand, which then has void type.  Just leave void
827              operands as they are.  */
828           return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
829                               VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
830                               ? TREE_OPERAND (expr, 1)
831                               : convert (type, TREE_OPERAND (expr, 1)),
832                               VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
833                               ? TREE_OPERAND (expr, 2)
834                               : convert (type, TREE_OPERAND (expr, 2)));
835
836         default:
837           break;
838         }
839
840       return build1 (CONVERT_EXPR, type, expr);
841
842     case REAL_TYPE:
843       return build1 (FIX_TRUNC_EXPR, type, expr);
844
845     case FIXED_POINT_TYPE:
846       return build1 (FIXED_CONVERT_EXPR, type, expr);
847
848     case COMPLEX_TYPE:
849       return convert (type,
850                       fold_build1 (REALPART_EXPR,
851                                    TREE_TYPE (TREE_TYPE (expr)), expr));
852
853     case VECTOR_TYPE:
854       if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
855         {
856           error ("can%'t convert between vector values of different size");
857           return error_mark_node;
858         }
859       return build1 (VIEW_CONVERT_EXPR, type, expr);
860
861     default:
862       error ("aggregate value used where an integer was expected");
863       return convert (type, integer_zero_node);
864     }
865 }
866
867 /* Convert EXPR to the complex type TYPE in the usual ways.  */
868
869 tree
870 convert_to_complex (tree type, tree expr)
871 {
872   tree subtype = TREE_TYPE (type);
873
874   switch (TREE_CODE (TREE_TYPE (expr)))
875     {
876     case REAL_TYPE:
877     case FIXED_POINT_TYPE:
878     case INTEGER_TYPE:
879     case ENUMERAL_TYPE:
880     case BOOLEAN_TYPE:
881       return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
882                      convert (subtype, integer_zero_node));
883
884     case COMPLEX_TYPE:
885       {
886         tree elt_type = TREE_TYPE (TREE_TYPE (expr));
887
888         if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
889           return expr;
890         else if (TREE_CODE (expr) == COMPLEX_EXPR)
891           return fold_build2 (COMPLEX_EXPR, type,
892                               convert (subtype, TREE_OPERAND (expr, 0)),
893                               convert (subtype, TREE_OPERAND (expr, 1)));
894         else
895           {
896             expr = save_expr (expr);
897             return
898               fold_build2 (COMPLEX_EXPR, type,
899                            convert (subtype,
900                                     fold_build1 (REALPART_EXPR,
901                                                  TREE_TYPE (TREE_TYPE (expr)),
902                                                  expr)),
903                            convert (subtype,
904                                     fold_build1 (IMAGPART_EXPR,
905                                                  TREE_TYPE (TREE_TYPE (expr)),
906                                                  expr)));
907           }
908       }
909
910     case POINTER_TYPE:
911     case REFERENCE_TYPE:
912       error ("pointer value used where a complex was expected");
913       return convert_to_complex (type, integer_zero_node);
914
915     default:
916       error ("aggregate value used where a complex was expected");
917       return convert_to_complex (type, integer_zero_node);
918     }
919 }
920
921 /* Convert EXPR to the vector type TYPE in the usual ways.  */
922
923 tree
924 convert_to_vector (tree type, tree expr)
925 {
926   switch (TREE_CODE (TREE_TYPE (expr)))
927     {
928     case INTEGER_TYPE:
929     case VECTOR_TYPE:
930       if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
931         {
932           error ("can%'t convert between vector values of different size");
933           return error_mark_node;
934         }
935       return build1 (VIEW_CONVERT_EXPR, type, expr);
936
937     default:
938       error ("can%'t convert value to a vector");
939       return error_mark_node;
940     }
941 }
942
943 /* Convert EXPR to some fixed-point type TYPE.
944
945    EXPR must be fixed-point, float, integer, or enumeral;
946    in other cases error is called.  */
947
948 tree
949 convert_to_fixed (tree type, tree expr)
950 {
951   if (integer_zerop (expr))
952     {
953       tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
954       return fixed_zero_node;
955     }
956   else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
957     {
958       tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
959       return fixed_one_node;
960     }
961
962   switch (TREE_CODE (TREE_TYPE (expr)))
963     {
964     case FIXED_POINT_TYPE:
965     case INTEGER_TYPE:
966     case ENUMERAL_TYPE:
967     case BOOLEAN_TYPE:
968     case REAL_TYPE:
969       return build1 (FIXED_CONVERT_EXPR, type, expr);
970
971     case COMPLEX_TYPE:
972       return convert (type,
973                       fold_build1 (REALPART_EXPR,
974                                    TREE_TYPE (TREE_TYPE (expr)), expr));
975
976     default:
977       error ("aggregate value used where a fixed-point was expected");
978       return error_mark_node;
979     }
980 }