[Ada] Crash on if expression inside declare expression
[gcc.git] / gcc / machmode.h
1 /* Machine mode definitions for GCC; included by rtl.h and tree.h.
2 Copyright (C) 1991-2020 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 #ifndef HAVE_MACHINE_MODES
21 #define HAVE_MACHINE_MODES
22
23 typedef opt_mode<machine_mode> opt_machine_mode;
24
25 extern CONST_MODE_SIZE poly_uint16_pod mode_size[NUM_MACHINE_MODES];
26 extern CONST_MODE_PRECISION poly_uint16_pod mode_precision[NUM_MACHINE_MODES];
27 extern const unsigned char mode_inner[NUM_MACHINE_MODES];
28 extern CONST_MODE_NUNITS poly_uint16_pod mode_nunits[NUM_MACHINE_MODES];
29 extern CONST_MODE_UNIT_SIZE unsigned char mode_unit_size[NUM_MACHINE_MODES];
30 extern const unsigned short mode_unit_precision[NUM_MACHINE_MODES];
31 extern const unsigned char mode_wider[NUM_MACHINE_MODES];
32 extern const unsigned char mode_2xwider[NUM_MACHINE_MODES];
33
34 template<typename T>
35 struct mode_traits
36 {
37 /* For use by the machmode support code only.
38
39 There are cases in which the machmode support code needs to forcibly
40 convert a machine_mode to a specific mode class T, and in which the
41 context guarantees that this is valid without the need for an assert.
42 This can be done using:
43
44 return typename mode_traits<T>::from_int (mode);
45
46 when returning a T and:
47
48 res = T (typename mode_traits<T>::from_int (mode));
49
50 when assigning to a value RES that must be assignment-compatible
51 with (but possibly not the same as) T. */
52 #ifdef USE_ENUM_MODES
53 /* Allow direct conversion of enums to specific mode classes only
54 when USE_ENUM_MODES is defined. This is only intended for use
55 by gencondmd, so that it can tell more easily when .md conditions
56 are always false. */
57 typedef machine_mode from_int;
58 #else
59 /* Here we use an enum type distinct from machine_mode but with the
60 same range as machine_mode. T should have a constructor that
61 accepts this enum type; it should not have a constructor that
62 accepts machine_mode.
63
64 We use this somewhat indirect approach to avoid too many constructor
65 calls when the compiler is built with -O0. For example, even in
66 unoptimized code, the return statement above would construct the
67 returned T directly from the numerical value of MODE. */
68 enum from_int { dummy = MAX_MACHINE_MODE };
69 #endif
70 };
71
72 template<>
73 struct mode_traits<machine_mode>
74 {
75 /* machine_mode itself needs no conversion. */
76 typedef machine_mode from_int;
77 };
78
79 /* Always treat machine modes as fixed-size while compiling code specific
80 to targets that have no variable-size modes. */
81 #if defined (IN_TARGET_CODE) && NUM_POLY_INT_COEFFS == 1
82 #define ONLY_FIXED_SIZE_MODES 1
83 #else
84 #define ONLY_FIXED_SIZE_MODES 0
85 #endif
86
87 /* Get the name of mode MODE as a string. */
88
89 extern const char * const mode_name[NUM_MACHINE_MODES];
90 #define GET_MODE_NAME(MODE) mode_name[MODE]
91
92 /* Mode classes. */
93
94 #include "mode-classes.def"
95 #define DEF_MODE_CLASS(M) M
96 enum mode_class { MODE_CLASSES, MAX_MODE_CLASS };
97 #undef DEF_MODE_CLASS
98 #undef MODE_CLASSES
99
100 /* Get the general kind of object that mode MODE represents
101 (integer, floating, complex, etc.) */
102
103 extern const unsigned char mode_class[NUM_MACHINE_MODES];
104 #define GET_MODE_CLASS(MODE) ((enum mode_class) mode_class[MODE])
105
106 /* Nonzero if MODE is an integral mode. */
107 #define INTEGRAL_MODE_P(MODE) \
108 (GET_MODE_CLASS (MODE) == MODE_INT \
109 || GET_MODE_CLASS (MODE) == MODE_PARTIAL_INT \
110 || GET_MODE_CLASS (MODE) == MODE_COMPLEX_INT \
111 || GET_MODE_CLASS (MODE) == MODE_VECTOR_BOOL \
112 || GET_MODE_CLASS (MODE) == MODE_VECTOR_INT)
113
114 /* Nonzero if MODE is a floating-point mode. */
115 #define FLOAT_MODE_P(MODE) \
116 (GET_MODE_CLASS (MODE) == MODE_FLOAT \
117 || GET_MODE_CLASS (MODE) == MODE_DECIMAL_FLOAT \
118 || GET_MODE_CLASS (MODE) == MODE_COMPLEX_FLOAT \
119 || GET_MODE_CLASS (MODE) == MODE_VECTOR_FLOAT)
120
121 /* Nonzero if MODE is a complex mode. */
122 #define COMPLEX_MODE_P(MODE) \
123 (GET_MODE_CLASS (MODE) == MODE_COMPLEX_INT \
124 || GET_MODE_CLASS (MODE) == MODE_COMPLEX_FLOAT)
125
126 /* Nonzero if MODE is a vector mode. */
127 #define VECTOR_MODE_P(MODE) \
128 (GET_MODE_CLASS (MODE) == MODE_VECTOR_BOOL \
129 || GET_MODE_CLASS (MODE) == MODE_VECTOR_INT \
130 || GET_MODE_CLASS (MODE) == MODE_VECTOR_FLOAT \
131 || GET_MODE_CLASS (MODE) == MODE_VECTOR_FRACT \
132 || GET_MODE_CLASS (MODE) == MODE_VECTOR_UFRACT \
133 || GET_MODE_CLASS (MODE) == MODE_VECTOR_ACCUM \
134 || GET_MODE_CLASS (MODE) == MODE_VECTOR_UACCUM)
135
136 /* Nonzero if MODE is a scalar integral mode. */
137 #define SCALAR_INT_MODE_P(MODE) \
138 (GET_MODE_CLASS (MODE) == MODE_INT \
139 || GET_MODE_CLASS (MODE) == MODE_PARTIAL_INT)
140
141 /* Nonzero if MODE is a scalar floating point mode. */
142 #define SCALAR_FLOAT_MODE_P(MODE) \
143 (GET_MODE_CLASS (MODE) == MODE_FLOAT \
144 || GET_MODE_CLASS (MODE) == MODE_DECIMAL_FLOAT)
145
146 /* Nonzero if MODE is a decimal floating point mode. */
147 #define DECIMAL_FLOAT_MODE_P(MODE) \
148 (GET_MODE_CLASS (MODE) == MODE_DECIMAL_FLOAT)
149
150 /* Nonzero if MODE is a scalar fract mode. */
151 #define SCALAR_FRACT_MODE_P(MODE) \
152 (GET_MODE_CLASS (MODE) == MODE_FRACT)
153
154 /* Nonzero if MODE is a scalar ufract mode. */
155 #define SCALAR_UFRACT_MODE_P(MODE) \
156 (GET_MODE_CLASS (MODE) == MODE_UFRACT)
157
158 /* Nonzero if MODE is a scalar fract or ufract mode. */
159 #define ALL_SCALAR_FRACT_MODE_P(MODE) \
160 (SCALAR_FRACT_MODE_P (MODE) || SCALAR_UFRACT_MODE_P (MODE))
161
162 /* Nonzero if MODE is a scalar accum mode. */
163 #define SCALAR_ACCUM_MODE_P(MODE) \
164 (GET_MODE_CLASS (MODE) == MODE_ACCUM)
165
166 /* Nonzero if MODE is a scalar uaccum mode. */
167 #define SCALAR_UACCUM_MODE_P(MODE) \
168 (GET_MODE_CLASS (MODE) == MODE_UACCUM)
169
170 /* Nonzero if MODE is a scalar accum or uaccum mode. */
171 #define ALL_SCALAR_ACCUM_MODE_P(MODE) \
172 (SCALAR_ACCUM_MODE_P (MODE) || SCALAR_UACCUM_MODE_P (MODE))
173
174 /* Nonzero if MODE is a scalar fract or accum mode. */
175 #define SIGNED_SCALAR_FIXED_POINT_MODE_P(MODE) \
176 (SCALAR_FRACT_MODE_P (MODE) || SCALAR_ACCUM_MODE_P (MODE))
177
178 /* Nonzero if MODE is a scalar ufract or uaccum mode. */
179 #define UNSIGNED_SCALAR_FIXED_POINT_MODE_P(MODE) \
180 (SCALAR_UFRACT_MODE_P (MODE) || SCALAR_UACCUM_MODE_P (MODE))
181
182 /* Nonzero if MODE is a scalar fract, ufract, accum or uaccum mode. */
183 #define ALL_SCALAR_FIXED_POINT_MODE_P(MODE) \
184 (SIGNED_SCALAR_FIXED_POINT_MODE_P (MODE) \
185 || UNSIGNED_SCALAR_FIXED_POINT_MODE_P (MODE))
186
187 /* Nonzero if MODE is a scalar/vector fract mode. */
188 #define FRACT_MODE_P(MODE) \
189 (GET_MODE_CLASS (MODE) == MODE_FRACT \
190 || GET_MODE_CLASS (MODE) == MODE_VECTOR_FRACT)
191
192 /* Nonzero if MODE is a scalar/vector ufract mode. */
193 #define UFRACT_MODE_P(MODE) \
194 (GET_MODE_CLASS (MODE) == MODE_UFRACT \
195 || GET_MODE_CLASS (MODE) == MODE_VECTOR_UFRACT)
196
197 /* Nonzero if MODE is a scalar/vector fract or ufract mode. */
198 #define ALL_FRACT_MODE_P(MODE) \
199 (FRACT_MODE_P (MODE) || UFRACT_MODE_P (MODE))
200
201 /* Nonzero if MODE is a scalar/vector accum mode. */
202 #define ACCUM_MODE_P(MODE) \
203 (GET_MODE_CLASS (MODE) == MODE_ACCUM \
204 || GET_MODE_CLASS (MODE) == MODE_VECTOR_ACCUM)
205
206 /* Nonzero if MODE is a scalar/vector uaccum mode. */
207 #define UACCUM_MODE_P(MODE) \
208 (GET_MODE_CLASS (MODE) == MODE_UACCUM \
209 || GET_MODE_CLASS (MODE) == MODE_VECTOR_UACCUM)
210
211 /* Nonzero if MODE is a scalar/vector accum or uaccum mode. */
212 #define ALL_ACCUM_MODE_P(MODE) \
213 (ACCUM_MODE_P (MODE) || UACCUM_MODE_P (MODE))
214
215 /* Nonzero if MODE is a scalar/vector fract or accum mode. */
216 #define SIGNED_FIXED_POINT_MODE_P(MODE) \
217 (FRACT_MODE_P (MODE) || ACCUM_MODE_P (MODE))
218
219 /* Nonzero if MODE is a scalar/vector ufract or uaccum mode. */
220 #define UNSIGNED_FIXED_POINT_MODE_P(MODE) \
221 (UFRACT_MODE_P (MODE) || UACCUM_MODE_P (MODE))
222
223 /* Nonzero if MODE is a scalar/vector fract, ufract, accum or uaccum mode. */
224 #define ALL_FIXED_POINT_MODE_P(MODE) \
225 (SIGNED_FIXED_POINT_MODE_P (MODE) \
226 || UNSIGNED_FIXED_POINT_MODE_P (MODE))
227
228 /* Nonzero if MODE is opaque. */
229 #define OPAQUE_MODE_P(MODE) \
230 (GET_MODE_CLASS (MODE) == MODE_OPAQUE)
231
232 /* Nonzero if CLASS modes can be widened. */
233 #define CLASS_HAS_WIDER_MODES_P(CLASS) \
234 (CLASS == MODE_INT \
235 || CLASS == MODE_PARTIAL_INT \
236 || CLASS == MODE_FLOAT \
237 || CLASS == MODE_DECIMAL_FLOAT \
238 || CLASS == MODE_COMPLEX_FLOAT \
239 || CLASS == MODE_FRACT \
240 || CLASS == MODE_UFRACT \
241 || CLASS == MODE_ACCUM \
242 || CLASS == MODE_UACCUM)
243
244 /* An optional T (i.e. a T or nothing), where T is some form of mode class. */
245 template<typename T>
246 class opt_mode
247 {
248 public:
249 enum from_int { dummy = MAX_MACHINE_MODE };
250
251 ALWAYS_INLINE CONSTEXPR opt_mode () : m_mode (E_VOIDmode) {}
252 ALWAYS_INLINE CONSTEXPR opt_mode (const T &m) : m_mode (m) {}
253 template<typename U>
254 ALWAYS_INLINE CONSTEXPR opt_mode (const U &m) : m_mode (T (m)) {}
255 ALWAYS_INLINE CONSTEXPR opt_mode (from_int m) : m_mode (machine_mode (m)) {}
256
257 machine_mode else_void () const;
258 machine_mode else_blk () const { return else_mode (BLKmode); }
259 machine_mode else_mode (machine_mode) const;
260 T require () const;
261
262 bool exists () const;
263 template<typename U> bool exists (U *) const;
264
265 bool operator== (const T &m) const { return m_mode == m; }
266 bool operator!= (const T &m) const { return m_mode != m; }
267
268 private:
269 machine_mode m_mode;
270 };
271
272 /* If the object contains a T, return its enum value, otherwise return
273 E_VOIDmode. */
274
275 template<typename T>
276 ALWAYS_INLINE machine_mode
277 opt_mode<T>::else_void () const
278 {
279 return m_mode;
280 }
281
282 /* If the T exists, return its enum value, otherwise return FALLBACK. */
283
284 template<typename T>
285 inline machine_mode
286 opt_mode<T>::else_mode (machine_mode fallback) const
287 {
288 return m_mode == E_VOIDmode ? fallback : m_mode;
289 }
290
291 /* Assert that the object contains a T and return it. */
292
293 template<typename T>
294 inline T
295 opt_mode<T>::require () const
296 {
297 gcc_checking_assert (m_mode != E_VOIDmode);
298 return typename mode_traits<T>::from_int (m_mode);
299 }
300
301 /* Return true if the object contains a T rather than nothing. */
302
303 template<typename T>
304 ALWAYS_INLINE bool
305 opt_mode<T>::exists () const
306 {
307 return m_mode != E_VOIDmode;
308 }
309
310 /* Return true if the object contains a T, storing it in *MODE if so. */
311
312 template<typename T>
313 template<typename U>
314 inline bool
315 opt_mode<T>::exists (U *mode) const
316 {
317 if (m_mode != E_VOIDmode)
318 {
319 *mode = T (typename mode_traits<T>::from_int (m_mode));
320 return true;
321 }
322 return false;
323 }
324
325 /* A POD version of mode class T. */
326
327 template<typename T>
328 struct pod_mode
329 {
330 typedef typename mode_traits<T>::from_int from_int;
331 typedef typename T::measurement_type measurement_type;
332
333 machine_mode m_mode;
334 ALWAYS_INLINE CONSTEXPR
335 operator machine_mode () const { return m_mode; }
336
337 ALWAYS_INLINE CONSTEXPR
338 operator T () const { return from_int (m_mode); }
339
340 ALWAYS_INLINE pod_mode &operator = (const T &m) { m_mode = m; return *this; }
341 };
342
343 /* Return true if mode M has type T. */
344
345 template<typename T>
346 inline bool
347 is_a (machine_mode m)
348 {
349 return T::includes_p (m);
350 }
351
352 template<typename T, typename U>
353 inline bool
354 is_a (const opt_mode<U> &m)
355 {
356 return T::includes_p (m.else_void ());
357 }
358
359 /* Assert that mode M has type T, and return it in that form. */
360
361 template<typename T>
362 inline T
363 as_a (machine_mode m)
364 {
365 gcc_checking_assert (T::includes_p (m));
366 return typename mode_traits<T>::from_int (m);
367 }
368
369 template<typename T, typename U>
370 inline T
371 as_a (const opt_mode<U> &m)
372 {
373 return as_a <T> (m.else_void ());
374 }
375
376 /* Convert M to an opt_mode<T>. */
377
378 template<typename T>
379 inline opt_mode<T>
380 dyn_cast (machine_mode m)
381 {
382 if (T::includes_p (m))
383 return T (typename mode_traits<T>::from_int (m));
384 return opt_mode<T> ();
385 }
386
387 template<typename T, typename U>
388 inline opt_mode<T>
389 dyn_cast (const opt_mode<U> &m)
390 {
391 return dyn_cast <T> (m.else_void ());
392 }
393
394 /* Return true if mode M has type T, storing it as a T in *RESULT
395 if so. */
396
397 template<typename T, typename U>
398 inline bool
399 is_a (machine_mode m, U *result)
400 {
401 if (T::includes_p (m))
402 {
403 *result = T (typename mode_traits<T>::from_int (m));
404 return true;
405 }
406 return false;
407 }
408
409 /* Represents a machine mode that is known to be a SCALAR_INT_MODE_P. */
410 class scalar_int_mode
411 {
412 public:
413 typedef mode_traits<scalar_int_mode>::from_int from_int;
414 typedef unsigned short measurement_type;
415
416 ALWAYS_INLINE scalar_int_mode () {}
417
418 ALWAYS_INLINE CONSTEXPR
419 scalar_int_mode (from_int m) : m_mode (machine_mode (m)) {}
420
421 ALWAYS_INLINE CONSTEXPR operator machine_mode () const { return m_mode; }
422
423 static bool includes_p (machine_mode);
424
425 protected:
426 machine_mode m_mode;
427 };
428
429 /* Return true if M is a scalar_int_mode. */
430
431 inline bool
432 scalar_int_mode::includes_p (machine_mode m)
433 {
434 return SCALAR_INT_MODE_P (m);
435 }
436
437 /* Represents a machine mode that is known to be a SCALAR_FLOAT_MODE_P. */
438 class scalar_float_mode
439 {
440 public:
441 typedef mode_traits<scalar_float_mode>::from_int from_int;
442 typedef unsigned short measurement_type;
443
444 ALWAYS_INLINE scalar_float_mode () {}
445
446 ALWAYS_INLINE CONSTEXPR
447 scalar_float_mode (from_int m) : m_mode (machine_mode (m)) {}
448
449 ALWAYS_INLINE CONSTEXPR operator machine_mode () const { return m_mode; }
450
451 static bool includes_p (machine_mode);
452
453 protected:
454 machine_mode m_mode;
455 };
456
457 /* Return true if M is a scalar_float_mode. */
458
459 inline bool
460 scalar_float_mode::includes_p (machine_mode m)
461 {
462 return SCALAR_FLOAT_MODE_P (m);
463 }
464
465 /* Represents a machine mode that is known to be scalar. */
466 class scalar_mode
467 {
468 public:
469 typedef mode_traits<scalar_mode>::from_int from_int;
470 typedef unsigned short measurement_type;
471
472 ALWAYS_INLINE scalar_mode () {}
473
474 ALWAYS_INLINE CONSTEXPR
475 scalar_mode (from_int m) : m_mode (machine_mode (m)) {}
476
477 ALWAYS_INLINE CONSTEXPR
478 scalar_mode (const scalar_int_mode &m) : m_mode (m) {}
479
480 ALWAYS_INLINE CONSTEXPR
481 scalar_mode (const scalar_float_mode &m) : m_mode (m) {}
482
483 ALWAYS_INLINE CONSTEXPR
484 scalar_mode (const scalar_int_mode_pod &m) : m_mode (m) {}
485
486 ALWAYS_INLINE CONSTEXPR operator machine_mode () const { return m_mode; }
487
488 static bool includes_p (machine_mode);
489
490 protected:
491 machine_mode m_mode;
492 };
493
494 /* Return true if M represents some kind of scalar value. */
495
496 inline bool
497 scalar_mode::includes_p (machine_mode m)
498 {
499 switch (GET_MODE_CLASS (m))
500 {
501 case MODE_INT:
502 case MODE_PARTIAL_INT:
503 case MODE_FRACT:
504 case MODE_UFRACT:
505 case MODE_ACCUM:
506 case MODE_UACCUM:
507 case MODE_FLOAT:
508 case MODE_DECIMAL_FLOAT:
509 return true;
510 default:
511 return false;
512 }
513 }
514
515 /* Represents a machine mode that is known to be a COMPLEX_MODE_P. */
516 class complex_mode
517 {
518 public:
519 typedef mode_traits<complex_mode>::from_int from_int;
520 typedef unsigned short measurement_type;
521
522 ALWAYS_INLINE complex_mode () {}
523
524 ALWAYS_INLINE CONSTEXPR
525 complex_mode (from_int m) : m_mode (machine_mode (m)) {}
526
527 ALWAYS_INLINE CONSTEXPR operator machine_mode () const { return m_mode; }
528
529 static bool includes_p (machine_mode);
530
531 protected:
532 machine_mode m_mode;
533 };
534
535 /* Return true if M is a complex_mode. */
536
537 inline bool
538 complex_mode::includes_p (machine_mode m)
539 {
540 return COMPLEX_MODE_P (m);
541 }
542
543 /* Return the base GET_MODE_SIZE value for MODE. */
544
545 ALWAYS_INLINE poly_uint16
546 mode_to_bytes (machine_mode mode)
547 {
548 #if GCC_VERSION >= 4001
549 return (__builtin_constant_p (mode)
550 ? mode_size_inline (mode) : mode_size[mode]);
551 #else
552 return mode_size[mode];
553 #endif
554 }
555
556 /* Return the base GET_MODE_BITSIZE value for MODE. */
557
558 ALWAYS_INLINE poly_uint16
559 mode_to_bits (machine_mode mode)
560 {
561 return mode_to_bytes (mode) * BITS_PER_UNIT;
562 }
563
564 /* Return the base GET_MODE_PRECISION value for MODE. */
565
566 ALWAYS_INLINE poly_uint16
567 mode_to_precision (machine_mode mode)
568 {
569 return mode_precision[mode];
570 }
571
572 /* Return the base GET_MODE_INNER value for MODE. */
573
574 ALWAYS_INLINE scalar_mode
575 mode_to_inner (machine_mode mode)
576 {
577 #if GCC_VERSION >= 4001
578 return scalar_mode::from_int (__builtin_constant_p (mode)
579 ? mode_inner_inline (mode)
580 : mode_inner[mode]);
581 #else
582 return scalar_mode::from_int (mode_inner[mode]);
583 #endif
584 }
585
586 /* Return the base GET_MODE_UNIT_SIZE value for MODE. */
587
588 ALWAYS_INLINE unsigned char
589 mode_to_unit_size (machine_mode mode)
590 {
591 #if GCC_VERSION >= 4001
592 return (__builtin_constant_p (mode)
593 ? mode_unit_size_inline (mode) : mode_unit_size[mode]);
594 #else
595 return mode_unit_size[mode];
596 #endif
597 }
598
599 /* Return the base GET_MODE_UNIT_PRECISION value for MODE. */
600
601 ALWAYS_INLINE unsigned short
602 mode_to_unit_precision (machine_mode mode)
603 {
604 #if GCC_VERSION >= 4001
605 return (__builtin_constant_p (mode)
606 ? mode_unit_precision_inline (mode) : mode_unit_precision[mode]);
607 #else
608 return mode_unit_precision[mode];
609 #endif
610 }
611
612 /* Return the base GET_MODE_NUNITS value for MODE. */
613
614 ALWAYS_INLINE poly_uint16
615 mode_to_nunits (machine_mode mode)
616 {
617 #if GCC_VERSION >= 4001
618 return (__builtin_constant_p (mode)
619 ? mode_nunits_inline (mode) : mode_nunits[mode]);
620 #else
621 return mode_nunits[mode];
622 #endif
623 }
624
625 /* Get the size in bytes of an object of mode MODE. */
626
627 #if ONLY_FIXED_SIZE_MODES
628 #define GET_MODE_SIZE(MODE) ((unsigned short) mode_to_bytes (MODE).coeffs[0])
629 #else
630 ALWAYS_INLINE poly_uint16
631 GET_MODE_SIZE (machine_mode mode)
632 {
633 return mode_to_bytes (mode);
634 }
635
636 template<typename T>
637 ALWAYS_INLINE typename if_poly<typename T::measurement_type>::type
638 GET_MODE_SIZE (const T &mode)
639 {
640 return mode_to_bytes (mode);
641 }
642
643 template<typename T>
644 ALWAYS_INLINE typename if_nonpoly<typename T::measurement_type>::type
645 GET_MODE_SIZE (const T &mode)
646 {
647 return mode_to_bytes (mode).coeffs[0];
648 }
649 #endif
650
651 /* Get the size in bits of an object of mode MODE. */
652
653 #if ONLY_FIXED_SIZE_MODES
654 #define GET_MODE_BITSIZE(MODE) ((unsigned short) mode_to_bits (MODE).coeffs[0])
655 #else
656 ALWAYS_INLINE poly_uint16
657 GET_MODE_BITSIZE (machine_mode mode)
658 {
659 return mode_to_bits (mode);
660 }
661
662 template<typename T>
663 ALWAYS_INLINE typename if_poly<typename T::measurement_type>::type
664 GET_MODE_BITSIZE (const T &mode)
665 {
666 return mode_to_bits (mode);
667 }
668
669 template<typename T>
670 ALWAYS_INLINE typename if_nonpoly<typename T::measurement_type>::type
671 GET_MODE_BITSIZE (const T &mode)
672 {
673 return mode_to_bits (mode).coeffs[0];
674 }
675 #endif
676
677 /* Get the number of value bits of an object of mode MODE. */
678
679 #if ONLY_FIXED_SIZE_MODES
680 #define GET_MODE_PRECISION(MODE) \
681 ((unsigned short) mode_to_precision (MODE).coeffs[0])
682 #else
683 ALWAYS_INLINE poly_uint16
684 GET_MODE_PRECISION (machine_mode mode)
685 {
686 return mode_to_precision (mode);
687 }
688
689 template<typename T>
690 ALWAYS_INLINE typename if_poly<typename T::measurement_type>::type
691 GET_MODE_PRECISION (const T &mode)
692 {
693 return mode_to_precision (mode);
694 }
695
696 template<typename T>
697 ALWAYS_INLINE typename if_nonpoly<typename T::measurement_type>::type
698 GET_MODE_PRECISION (const T &mode)
699 {
700 return mode_to_precision (mode).coeffs[0];
701 }
702 #endif
703
704 /* Get the number of integral bits of an object of mode MODE. */
705 extern CONST_MODE_IBIT unsigned char mode_ibit[NUM_MACHINE_MODES];
706 #define GET_MODE_IBIT(MODE) mode_ibit[MODE]
707
708 /* Get the number of fractional bits of an object of mode MODE. */
709 extern CONST_MODE_FBIT unsigned char mode_fbit[NUM_MACHINE_MODES];
710 #define GET_MODE_FBIT(MODE) mode_fbit[MODE]
711
712 /* Get a bitmask containing 1 for all bits in a word
713 that fit within mode MODE. */
714
715 extern const unsigned HOST_WIDE_INT mode_mask_array[NUM_MACHINE_MODES];
716
717 #define GET_MODE_MASK(MODE) mode_mask_array[MODE]
718
719 /* Return the mode of the basic parts of MODE. For vector modes this is the
720 mode of the vector elements. For complex modes it is the mode of the real
721 and imaginary parts. For other modes it is MODE itself. */
722
723 #define GET_MODE_INNER(MODE) (mode_to_inner (MODE))
724
725 /* Get the size in bytes or bits of the basic parts of an
726 object of mode MODE. */
727
728 #define GET_MODE_UNIT_SIZE(MODE) mode_to_unit_size (MODE)
729
730 #define GET_MODE_UNIT_BITSIZE(MODE) \
731 ((unsigned short) (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT))
732
733 #define GET_MODE_UNIT_PRECISION(MODE) (mode_to_unit_precision (MODE))
734
735 /* Get the number of units in an object of mode MODE. This is 2 for
736 complex modes and the number of elements for vector modes. */
737
738 #if ONLY_FIXED_SIZE_MODES
739 #define GET_MODE_NUNITS(MODE) (mode_to_nunits (MODE).coeffs[0])
740 #else
741 ALWAYS_INLINE poly_uint16
742 GET_MODE_NUNITS (machine_mode mode)
743 {
744 return mode_to_nunits (mode);
745 }
746
747 template<typename T>
748 ALWAYS_INLINE typename if_poly<typename T::measurement_type>::type
749 GET_MODE_NUNITS (const T &mode)
750 {
751 return mode_to_nunits (mode);
752 }
753
754 template<typename T>
755 ALWAYS_INLINE typename if_nonpoly<typename T::measurement_type>::type
756 GET_MODE_NUNITS (const T &mode)
757 {
758 return mode_to_nunits (mode).coeffs[0];
759 }
760 #endif
761
762 /* Get the next wider natural mode (eg, QI -> HI -> SI -> DI -> TI). */
763
764 template<typename T>
765 ALWAYS_INLINE opt_mode<T>
766 GET_MODE_WIDER_MODE (const T &m)
767 {
768 return typename opt_mode<T>::from_int (mode_wider[m]);
769 }
770
771 /* For scalars, this is a mode with twice the precision. For vectors,
772 this is a mode with the same inner mode but with twice the elements. */
773
774 template<typename T>
775 ALWAYS_INLINE opt_mode<T>
776 GET_MODE_2XWIDER_MODE (const T &m)
777 {
778 return typename opt_mode<T>::from_int (mode_2xwider[m]);
779 }
780
781 /* Get the complex mode from the component mode. */
782 extern const unsigned char mode_complex[NUM_MACHINE_MODES];
783 #define GET_MODE_COMPLEX_MODE(MODE) ((machine_mode) mode_complex[MODE])
784
785 /* Represents a machine mode that must have a fixed size. The main
786 use of this class is to represent the modes of objects that always
787 have static storage duration, such as constant pool entries.
788 (No current target supports the concept of variable-size static data.) */
789 class fixed_size_mode
790 {
791 public:
792 typedef mode_traits<fixed_size_mode>::from_int from_int;
793 typedef unsigned short measurement_type;
794
795 ALWAYS_INLINE fixed_size_mode () {}
796
797 ALWAYS_INLINE CONSTEXPR
798 fixed_size_mode (from_int m) : m_mode (machine_mode (m)) {}
799
800 ALWAYS_INLINE CONSTEXPR
801 fixed_size_mode (const scalar_mode &m) : m_mode (m) {}
802
803 ALWAYS_INLINE CONSTEXPR
804 fixed_size_mode (const scalar_int_mode &m) : m_mode (m) {}
805
806 ALWAYS_INLINE CONSTEXPR
807 fixed_size_mode (const scalar_float_mode &m) : m_mode (m) {}
808
809 ALWAYS_INLINE CONSTEXPR
810 fixed_size_mode (const scalar_mode_pod &m) : m_mode (m) {}
811
812 ALWAYS_INLINE CONSTEXPR
813 fixed_size_mode (const scalar_int_mode_pod &m) : m_mode (m) {}
814
815 ALWAYS_INLINE CONSTEXPR
816 fixed_size_mode (const complex_mode &m) : m_mode (m) {}
817
818 ALWAYS_INLINE CONSTEXPR operator machine_mode () const { return m_mode; }
819
820 static bool includes_p (machine_mode);
821
822 protected:
823 machine_mode m_mode;
824 };
825
826 /* Return true if MODE has a fixed size. */
827
828 inline bool
829 fixed_size_mode::includes_p (machine_mode mode)
830 {
831 return mode_to_bytes (mode).is_constant ();
832 }
833
834 /* Wrapper for mode arguments to target macros, so that if a target
835 doesn't need polynomial-sized modes, its header file can continue
836 to treat everything as fixed_size_mode. This should go away once
837 macros are moved to target hooks. It shouldn't be used in other
838 contexts. */
839 #if NUM_POLY_INT_COEFFS == 1
840 #define MACRO_MODE(MODE) (as_a <fixed_size_mode> (MODE))
841 #else
842 #define MACRO_MODE(MODE) (MODE)
843 #endif
844
845 extern opt_machine_mode mode_for_size (poly_uint64, enum mode_class, int);
846
847 /* Return the machine mode to use for a MODE_INT of SIZE bits, if one
848 exists. If LIMIT is nonzero, modes wider than MAX_FIXED_MODE_SIZE
849 will not be used. */
850
851 inline opt_scalar_int_mode
852 int_mode_for_size (poly_uint64 size, int limit)
853 {
854 return dyn_cast <scalar_int_mode> (mode_for_size (size, MODE_INT, limit));
855 }
856
857 /* Return the machine mode to use for a MODE_FLOAT of SIZE bits, if one
858 exists. */
859
860 inline opt_scalar_float_mode
861 float_mode_for_size (poly_uint64 size)
862 {
863 return dyn_cast <scalar_float_mode> (mode_for_size (size, MODE_FLOAT, 0));
864 }
865
866 /* Likewise for MODE_DECIMAL_FLOAT. */
867
868 inline opt_scalar_float_mode
869 decimal_float_mode_for_size (unsigned int size)
870 {
871 return dyn_cast <scalar_float_mode>
872 (mode_for_size (size, MODE_DECIMAL_FLOAT, 0));
873 }
874
875 extern machine_mode smallest_mode_for_size (poly_uint64, enum mode_class);
876
877 /* Find the narrowest integer mode that contains at least SIZE bits.
878 Such a mode must exist. */
879
880 inline scalar_int_mode
881 smallest_int_mode_for_size (poly_uint64 size)
882 {
883 return as_a <scalar_int_mode> (smallest_mode_for_size (size, MODE_INT));
884 }
885
886 extern opt_scalar_int_mode int_mode_for_mode (machine_mode);
887 extern opt_machine_mode bitwise_mode_for_mode (machine_mode);
888 extern opt_machine_mode mode_for_vector (scalar_mode, poly_uint64);
889 extern opt_machine_mode related_vector_mode (machine_mode, scalar_mode,
890 poly_uint64 = 0);
891 extern opt_machine_mode related_int_vector_mode (machine_mode);
892
893 /* A class for iterating through possible bitfield modes. */
894 class bit_field_mode_iterator
895 {
896 public:
897 bit_field_mode_iterator (HOST_WIDE_INT, HOST_WIDE_INT,
898 poly_int64, poly_int64,
899 unsigned int, bool);
900 bool next_mode (scalar_int_mode *);
901 bool prefer_smaller_modes ();
902
903 private:
904 opt_scalar_int_mode m_mode;
905 /* We use signed values here because the bit position can be negative
906 for invalid input such as gcc.dg/pr48335-8.c. */
907 HOST_WIDE_INT m_bitsize;
908 HOST_WIDE_INT m_bitpos;
909 poly_int64 m_bitregion_start;
910 poly_int64 m_bitregion_end;
911 unsigned int m_align;
912 bool m_volatilep;
913 int m_count;
914 };
915
916 /* Find the best mode to use to access a bit field. */
917
918 extern bool get_best_mode (int, int, poly_uint64, poly_uint64, unsigned int,
919 unsigned HOST_WIDE_INT, bool, scalar_int_mode *);
920
921 /* Determine alignment, 1<=result<=BIGGEST_ALIGNMENT. */
922
923 extern CONST_MODE_BASE_ALIGN unsigned short mode_base_align[NUM_MACHINE_MODES];
924
925 extern unsigned get_mode_alignment (machine_mode);
926
927 #define GET_MODE_ALIGNMENT(MODE) get_mode_alignment (MODE)
928
929 /* For each class, get the narrowest mode in that class. */
930
931 extern const unsigned char class_narrowest_mode[MAX_MODE_CLASS];
932 #define GET_CLASS_NARROWEST_MODE(CLASS) \
933 ((machine_mode) class_narrowest_mode[CLASS])
934
935 /* The narrowest full integer mode available on the target. */
936
937 #define NARROWEST_INT_MODE \
938 (scalar_int_mode \
939 (scalar_int_mode::from_int (class_narrowest_mode[MODE_INT])))
940
941 /* Return the narrowest mode in T's class. */
942
943 template<typename T>
944 inline T
945 get_narrowest_mode (T mode)
946 {
947 return typename mode_traits<T>::from_int
948 (class_narrowest_mode[GET_MODE_CLASS (mode)]);
949 }
950
951 /* Define the integer modes whose sizes are BITS_PER_UNIT and BITS_PER_WORD
952 and the mode whose class is Pmode and whose size is POINTER_SIZE. */
953
954 extern scalar_int_mode byte_mode;
955 extern scalar_int_mode word_mode;
956 extern scalar_int_mode ptr_mode;
957
958 /* Target-dependent machine mode initialization - in insn-modes.c. */
959 extern void init_adjust_machine_modes (void);
960
961 #define TRULY_NOOP_TRUNCATION_MODES_P(MODE1, MODE2) \
962 (targetm.truly_noop_truncation (GET_MODE_PRECISION (MODE1), \
963 GET_MODE_PRECISION (MODE2)))
964
965 /* Return true if MODE is a scalar integer mode that fits in a
966 HOST_WIDE_INT. */
967
968 inline bool
969 HWI_COMPUTABLE_MODE_P (machine_mode mode)
970 {
971 machine_mode mme = mode;
972 return (SCALAR_INT_MODE_P (mme)
973 && mode_to_precision (mme).coeffs[0] <= HOST_BITS_PER_WIDE_INT);
974 }
975
976 inline bool
977 HWI_COMPUTABLE_MODE_P (scalar_int_mode mode)
978 {
979 return GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT;
980 }
981
982 struct int_n_data_t {
983 /* These parts are initailized by genmodes output */
984 unsigned int bitsize;
985 scalar_int_mode_pod m;
986 /* RID_* is RID_INTN_BASE + index into this array */
987 };
988
989 /* This is also in tree.h. genmodes.c guarantees the're sorted from
990 smallest bitsize to largest bitsize. */
991 extern bool int_n_enabled_p[NUM_INT_N_ENTS];
992 extern const int_n_data_t int_n_data[NUM_INT_N_ENTS];
993
994 /* Return true if MODE has class MODE_INT, storing it as a scalar_int_mode
995 in *INT_MODE if so. */
996
997 template<typename T>
998 inline bool
999 is_int_mode (machine_mode mode, T *int_mode)
1000 {
1001 if (GET_MODE_CLASS (mode) == MODE_INT)
1002 {
1003 *int_mode = scalar_int_mode (scalar_int_mode::from_int (mode));
1004 return true;
1005 }
1006 return false;
1007 }
1008
1009 /* Return true if MODE has class MODE_FLOAT, storing it as a
1010 scalar_float_mode in *FLOAT_MODE if so. */
1011
1012 template<typename T>
1013 inline bool
1014 is_float_mode (machine_mode mode, T *float_mode)
1015 {
1016 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
1017 {
1018 *float_mode = scalar_float_mode (scalar_float_mode::from_int (mode));
1019 return true;
1020 }
1021 return false;
1022 }
1023
1024 /* Return true if MODE has class MODE_COMPLEX_INT, storing it as
1025 a complex_mode in *CMODE if so. */
1026
1027 template<typename T>
1028 inline bool
1029 is_complex_int_mode (machine_mode mode, T *cmode)
1030 {
1031 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
1032 {
1033 *cmode = complex_mode (complex_mode::from_int (mode));
1034 return true;
1035 }
1036 return false;
1037 }
1038
1039 /* Return true if MODE has class MODE_COMPLEX_FLOAT, storing it as
1040 a complex_mode in *CMODE if so. */
1041
1042 template<typename T>
1043 inline bool
1044 is_complex_float_mode (machine_mode mode, T *cmode)
1045 {
1046 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
1047 {
1048 *cmode = complex_mode (complex_mode::from_int (mode));
1049 return true;
1050 }
1051 return false;
1052 }
1053
1054 /* Return true if MODE is a scalar integer mode with a precision
1055 smaller than LIMIT's precision. */
1056
1057 inline bool
1058 is_narrower_int_mode (machine_mode mode, scalar_int_mode limit)
1059 {
1060 scalar_int_mode int_mode;
1061 return (is_a <scalar_int_mode> (mode, &int_mode)
1062 && GET_MODE_PRECISION (int_mode) < GET_MODE_PRECISION (limit));
1063 }
1064
1065 namespace mode_iterator
1066 {
1067 /* Start mode iterator *ITER at the first mode in class MCLASS, if any. */
1068
1069 template<typename T>
1070 inline void
1071 start (opt_mode<T> *iter, enum mode_class mclass)
1072 {
1073 if (GET_CLASS_NARROWEST_MODE (mclass) == E_VOIDmode)
1074 *iter = opt_mode<T> ();
1075 else
1076 *iter = as_a<T> (GET_CLASS_NARROWEST_MODE (mclass));
1077 }
1078
1079 inline void
1080 start (machine_mode *iter, enum mode_class mclass)
1081 {
1082 *iter = GET_CLASS_NARROWEST_MODE (mclass);
1083 }
1084
1085 /* Return true if mode iterator *ITER has not reached the end. */
1086
1087 template<typename T>
1088 inline bool
1089 iterate_p (opt_mode<T> *iter)
1090 {
1091 return iter->exists ();
1092 }
1093
1094 inline bool
1095 iterate_p (machine_mode *iter)
1096 {
1097 return *iter != E_VOIDmode;
1098 }
1099
1100 /* Set mode iterator *ITER to the next widest mode in the same class,
1101 if any. */
1102
1103 template<typename T>
1104 inline void
1105 get_wider (opt_mode<T> *iter)
1106 {
1107 *iter = GET_MODE_WIDER_MODE (iter->require ());
1108 }
1109
1110 inline void
1111 get_wider (machine_mode *iter)
1112 {
1113 *iter = GET_MODE_WIDER_MODE (*iter).else_void ();
1114 }
1115
1116 /* Set mode iterator *ITER to the next widest mode in the same class.
1117 Such a mode is known to exist. */
1118
1119 template<typename T>
1120 inline void
1121 get_known_wider (T *iter)
1122 {
1123 *iter = GET_MODE_WIDER_MODE (*iter).require ();
1124 }
1125
1126 /* Set mode iterator *ITER to the mode that is two times wider than the
1127 current one, if such a mode exists. */
1128
1129 template<typename T>
1130 inline void
1131 get_2xwider (opt_mode<T> *iter)
1132 {
1133 *iter = GET_MODE_2XWIDER_MODE (iter->require ());
1134 }
1135
1136 inline void
1137 get_2xwider (machine_mode *iter)
1138 {
1139 *iter = GET_MODE_2XWIDER_MODE (*iter).else_void ();
1140 }
1141 }
1142
1143 /* Make ITERATOR iterate over all the modes in mode class CLASS,
1144 from narrowest to widest. */
1145 #define FOR_EACH_MODE_IN_CLASS(ITERATOR, CLASS) \
1146 for (mode_iterator::start (&(ITERATOR), CLASS); \
1147 mode_iterator::iterate_p (&(ITERATOR)); \
1148 mode_iterator::get_wider (&(ITERATOR)))
1149
1150 /* Make ITERATOR iterate over all the modes in the range [START, END),
1151 in order of increasing width. */
1152 #define FOR_EACH_MODE(ITERATOR, START, END) \
1153 for ((ITERATOR) = (START); \
1154 (ITERATOR) != (END); \
1155 mode_iterator::get_known_wider (&(ITERATOR)))
1156
1157 /* Make ITERATOR iterate over START and all wider modes in the same
1158 class, in order of increasing width. */
1159 #define FOR_EACH_MODE_FROM(ITERATOR, START) \
1160 for ((ITERATOR) = (START); \
1161 mode_iterator::iterate_p (&(ITERATOR)); \
1162 mode_iterator::get_wider (&(ITERATOR)))
1163
1164 /* Make ITERATOR iterate over modes in the range [NARROWEST, END)
1165 in order of increasing width, where NARROWEST is the narrowest mode
1166 in END's class. */
1167 #define FOR_EACH_MODE_UNTIL(ITERATOR, END) \
1168 FOR_EACH_MODE (ITERATOR, get_narrowest_mode (END), END)
1169
1170 /* Make ITERATOR iterate over modes in the same class as MODE, in order
1171 of increasing width. Start at the first mode wider than START,
1172 or don't iterate at all if there is no wider mode. */
1173 #define FOR_EACH_WIDER_MODE(ITERATOR, START) \
1174 for ((ITERATOR) = (START), mode_iterator::get_wider (&(ITERATOR)); \
1175 mode_iterator::iterate_p (&(ITERATOR)); \
1176 mode_iterator::get_wider (&(ITERATOR)))
1177
1178 /* Make ITERATOR iterate over modes in the same class as MODE, in order
1179 of increasing width, and with each mode being twice the width of the
1180 previous mode. Start at the mode that is two times wider than START,
1181 or don't iterate at all if there is no such mode. */
1182 #define FOR_EACH_2XWIDER_MODE(ITERATOR, START) \
1183 for ((ITERATOR) = (START), mode_iterator::get_2xwider (&(ITERATOR)); \
1184 mode_iterator::iterate_p (&(ITERATOR)); \
1185 mode_iterator::get_2xwider (&(ITERATOR)))
1186
1187 template<typename T>
1188 void
1189 gt_ggc_mx (pod_mode<T> *)
1190 {
1191 }
1192
1193 template<typename T>
1194 void
1195 gt_pch_nx (pod_mode<T> *)
1196 {
1197 }
1198
1199 template<typename T>
1200 void
1201 gt_pch_nx (pod_mode<T> *, void (*) (void *, void *), void *)
1202 {
1203 }
1204
1205 #endif /* not HAVE_MACHINE_MODES */