Update change log
[platform/upstream/gcc48.git] / gcc / expmed.h
1 /* Target-dependent costs for expmed.c.
2    Copyright (C) 1987-2013 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option; any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 #ifndef EXPMED_H
21 #define EXPMED_H 1
22
23 enum alg_code {
24   alg_unknown,
25   alg_zero,
26   alg_m, alg_shift,
27   alg_add_t_m2,
28   alg_sub_t_m2,
29   alg_add_factor,
30   alg_sub_factor,
31   alg_add_t2_m,
32   alg_sub_t2_m,
33   alg_impossible
34 };
35
36 /* This structure holds the "cost" of a multiply sequence.  The
37    "cost" field holds the total rtx_cost of every operator in the
38    synthetic multiplication sequence, hence cost(a op b) is defined
39    as rtx_cost(op) + cost(a) + cost(b), where cost(leaf) is zero.
40    The "latency" field holds the minimum possible latency of the
41    synthetic multiply, on a hypothetical infinitely parallel CPU.
42    This is the critical path, or the maximum height, of the expression
43    tree which is the sum of rtx_costs on the most expensive path from
44    any leaf to the root.  Hence latency(a op b) is defined as zero for
45    leaves and rtx_cost(op) + max(latency(a), latency(b)) otherwise.  */
46
47 struct mult_cost {
48   short cost;     /* Total rtx_cost of the multiplication sequence.  */
49   short latency;  /* The latency of the multiplication sequence.  */
50 };
51
52 /* This macro is used to compare a pointer to a mult_cost against an
53    single integer "rtx_cost" value.  This is equivalent to the macro
54    CHEAPER_MULT_COST(X,Z) where Z = {Y,Y}.  */
55 #define MULT_COST_LESS(X,Y) ((X)->cost < (Y)    \
56                              || ((X)->cost == (Y) && (X)->latency < (Y)))
57
58 /* This macro is used to compare two pointers to mult_costs against
59    each other.  The macro returns true if X is cheaper than Y.
60    Currently, the cheaper of two mult_costs is the one with the
61    lower "cost".  If "cost"s are tied, the lower latency is cheaper.  */
62 #define CHEAPER_MULT_COST(X,Y)  ((X)->cost < (Y)->cost          \
63                                  || ((X)->cost == (Y)->cost     \
64                                      && (X)->latency < (Y)->latency))
65
66 /* This structure records a sequence of operations.
67    `ops' is the number of operations recorded.
68    `cost' is their total cost.
69    The operations are stored in `op' and the corresponding
70    logarithms of the integer coefficients in `log'.
71
72    These are the operations:
73    alg_zero             total := 0;
74    alg_m                total := multiplicand;
75    alg_shift            total := total * coeff
76    alg_add_t_m2         total := total + multiplicand * coeff;
77    alg_sub_t_m2         total := total - multiplicand * coeff;
78    alg_add_factor       total := total * coeff + total;
79    alg_sub_factor       total := total * coeff - total;
80    alg_add_t2_m         total := total * coeff + multiplicand;
81    alg_sub_t2_m         total := total * coeff - multiplicand;
82
83    The first operand must be either alg_zero or alg_m.  */
84
85 struct algorithm
86 {
87   struct mult_cost cost;
88   short ops;
89   /* The size of the OP and LOG fields are not directly related to the
90      word size, but the worst-case algorithms will be if we have few
91      consecutive ones or zeros, i.e., a multiplicand like 10101010101...
92      In that case we will generate shift-by-2, add, shift-by-2, add,...,
93      in total wordsize operations.  */
94   enum alg_code op[MAX_BITS_PER_WORD];
95   char log[MAX_BITS_PER_WORD];
96 };
97
98 /* The entry for our multiplication cache/hash table.  */
99 struct alg_hash_entry {
100   /* The number we are multiplying by.  */
101   unsigned HOST_WIDE_INT t;
102
103   /* The mode in which we are multiplying something by T.  */
104   enum machine_mode mode;
105
106   /* The best multiplication algorithm for t.  */
107   enum alg_code alg;
108
109   /* The cost of multiplication if ALG_CODE is not alg_impossible.
110      Otherwise, the cost within which multiplication by T is
111      impossible.  */
112   struct mult_cost cost;
113
114   /* Optimized for speed? */
115   bool speed;
116 };
117
118 /* The number of cache/hash entries.  */
119 #if HOST_BITS_PER_WIDE_INT == 64
120 #define NUM_ALG_HASH_ENTRIES 1031
121 #else
122 #define NUM_ALG_HASH_ENTRIES 307
123 #endif
124
125 #define NUM_MODE_INT \
126   (MAX_MODE_INT - MIN_MODE_INT + 1)
127 #define NUM_MODE_PARTIAL_INT \
128   (MIN_MODE_PARTIAL_INT == VOIDmode ? 0 \
129    : MAX_MODE_PARTIAL_INT - MIN_MODE_PARTIAL_INT + 1)
130 #define NUM_MODE_VECTOR_INT \
131   (MIN_MODE_VECTOR_INT == VOIDmode ? 0 \
132    : MAX_MODE_VECTOR_INT - MIN_MODE_VECTOR_INT + 1)
133
134 #define NUM_MODE_IP_INT (NUM_MODE_INT + NUM_MODE_PARTIAL_INT)
135 #define NUM_MODE_IPV_INT (NUM_MODE_IP_INT + NUM_MODE_VECTOR_INT)
136
137 struct expmed_op_cheap {
138   bool cheap[2][NUM_MODE_IPV_INT];
139 };
140
141 struct expmed_op_costs {
142   int cost[2][NUM_MODE_IPV_INT];
143 };
144
145 /* Target-dependent globals.  */
146 struct target_expmed {
147   /* Each entry of ALG_HASH caches alg_code for some integer.  This is
148      actually a hash table.  If we have a collision, that the older
149      entry is kicked out.  */
150   struct alg_hash_entry x_alg_hash[NUM_ALG_HASH_ENTRIES];
151
152   /* True if x_alg_hash might already have been used.  */
153   bool x_alg_hash_used_p;
154
155   /* Nonzero means divides or modulus operations are relatively cheap for
156      powers of two, so don't use branches; emit the operation instead.
157      Usually, this will mean that the MD file will emit non-branch
158      sequences.  */
159   struct expmed_op_cheap x_sdiv_pow2_cheap;
160   struct expmed_op_cheap x_smod_pow2_cheap;
161
162   /* Cost of various pieces of RTL.  Note that some of these are indexed by
163      shift count and some by mode.  */
164   int x_zero_cost[2];
165   struct expmed_op_costs x_add_cost;
166   struct expmed_op_costs x_neg_cost;
167   struct expmed_op_costs x_shift_cost[MAX_BITS_PER_WORD];
168   struct expmed_op_costs x_shiftadd_cost[MAX_BITS_PER_WORD];
169   struct expmed_op_costs x_shiftsub0_cost[MAX_BITS_PER_WORD];
170   struct expmed_op_costs x_shiftsub1_cost[MAX_BITS_PER_WORD];
171   struct expmed_op_costs x_mul_cost;
172   struct expmed_op_costs x_sdiv_cost;
173   struct expmed_op_costs x_udiv_cost;
174   int x_mul_widen_cost[2][NUM_MODE_INT];
175   int x_mul_highpart_cost[2][NUM_MODE_INT];
176
177   /* Conversion costs are only defined between two scalar integer modes
178      of different sizes.  The first machine mode is the destination mode,
179      and the second is the source mode.  */
180   int x_convert_cost[2][NUM_MODE_IP_INT][NUM_MODE_IP_INT];
181 };
182
183 extern struct target_expmed default_target_expmed;
184 #if SWITCHABLE_TARGET
185 extern struct target_expmed *this_target_expmed;
186 #else
187 #define this_target_expmed (&default_target_expmed)
188 #endif
189
190 /* Return a pointer to the alg_hash_entry at IDX.  */
191
192 static inline struct alg_hash_entry *
193 alg_hash_entry_ptr (int idx)
194 {
195   return &this_target_expmed->x_alg_hash[idx];
196 }
197
198 /* Return true if the x_alg_hash field might have been used.  */
199
200 static inline bool
201 alg_hash_used_p (void)
202 {
203   return this_target_expmed->x_alg_hash_used_p;
204 }
205
206 /* Set whether the x_alg_hash field might have been used.  */
207
208 static inline void
209 set_alg_hash_used_p (bool usedp)
210 {
211   this_target_expmed->x_alg_hash_used_p = usedp;
212 }
213
214 /* Compute an index into the cost arrays by mode class.  */
215
216 static inline int
217 expmed_mode_index (enum machine_mode mode)
218 {
219   switch (GET_MODE_CLASS (mode))
220     {
221     case MODE_INT:
222       return mode - MIN_MODE_INT;
223     case MODE_PARTIAL_INT:
224       return mode - MIN_MODE_PARTIAL_INT + NUM_MODE_INT;
225     case MODE_VECTOR_INT:
226       return mode - MIN_MODE_VECTOR_INT + NUM_MODE_IP_INT;
227     default:
228       gcc_unreachable ();
229     }
230 }
231
232 /* Return a pointer to a boolean contained in EOC indicating whether
233    a particular operation performed in MODE is cheap when optimizing
234    for SPEED.  */
235
236 static inline bool *
237 expmed_op_cheap_ptr (struct expmed_op_cheap *eoc, bool speed,
238                      enum machine_mode mode)
239 {
240   int idx = expmed_mode_index (mode);
241   return &eoc->cheap[speed][idx];
242 }
243
244 /* Return a pointer to a cost contained in COSTS when a particular
245    operation is performed in MODE when optimizing for SPEED.  */
246
247 static inline int *
248 expmed_op_cost_ptr (struct expmed_op_costs *costs, bool speed,
249                     enum machine_mode mode)
250 {
251   int idx = expmed_mode_index (mode);
252   return &costs->cost[speed][idx];
253 }
254
255 /* Subroutine of {set_,}sdiv_pow2_cheap.  Not to be used otherwise.  */
256
257 static inline bool *
258 sdiv_pow2_cheap_ptr (bool speed, enum machine_mode mode)
259 {
260   return expmed_op_cheap_ptr (&this_target_expmed->x_sdiv_pow2_cheap,
261                               speed, mode);
262 }
263
264 /* Set whether a signed division by a power of 2 is cheap in MODE
265    when optimizing for SPEED.  */
266
267 static inline void
268 set_sdiv_pow2_cheap (bool speed, enum machine_mode mode, bool cheap_p)
269 {
270   *sdiv_pow2_cheap_ptr (speed, mode) = cheap_p;
271 }
272
273 /* Return whether a signed division by a power of 2 is cheap in MODE
274    when optimizing for SPEED.  */
275
276 static inline bool
277 sdiv_pow2_cheap (bool speed, enum machine_mode mode)
278 {
279   return *sdiv_pow2_cheap_ptr (speed, mode);
280 }
281
282 /* Subroutine of {set_,}smod_pow2_cheap.  Not to be used otherwise.  */
283
284 static inline bool *
285 smod_pow2_cheap_ptr (bool speed, enum machine_mode mode)
286 {
287   return expmed_op_cheap_ptr (&this_target_expmed->x_smod_pow2_cheap,
288                               speed, mode);
289 }
290
291 /* Set whether a signed modulo by a power of 2 is CHEAP in MODE when
292    optimizing for SPEED.  */
293
294 static inline void
295 set_smod_pow2_cheap (bool speed, enum machine_mode mode, bool cheap)
296 {
297   *smod_pow2_cheap_ptr (speed, mode) = cheap;
298 }
299
300 /* Return whether a signed modulo by a power of 2 is cheap in MODE
301    when optimizing for SPEED.  */
302
303 static inline bool
304 smod_pow2_cheap (bool speed, enum machine_mode mode)
305 {
306   return *smod_pow2_cheap_ptr (speed, mode);
307 }
308
309 /* Subroutine of {set_,}zero_cost.  Not to be used otherwise.  */
310
311 static inline int *
312 zero_cost_ptr (bool speed)
313 {
314   return &this_target_expmed->x_zero_cost[speed];
315 }
316
317 /* Set the COST of loading zero when optimizing for SPEED.  */
318
319 static inline void
320 set_zero_cost (bool speed, int cost)
321 {
322   *zero_cost_ptr (speed) = cost;
323 }
324
325 /* Return the COST of loading zero when optimizing for SPEED.  */
326
327 static inline int
328 zero_cost (bool speed)
329 {
330   return *zero_cost_ptr (speed);
331 }
332
333 /* Subroutine of {set_,}add_cost.  Not to be used otherwise.  */
334
335 static inline int *
336 add_cost_ptr (bool speed, enum machine_mode mode)
337 {
338   return expmed_op_cost_ptr (&this_target_expmed->x_add_cost, speed, mode);
339 }
340
341 /* Set the COST of computing an add in MODE when optimizing for SPEED.  */
342
343 static inline void
344 set_add_cost (bool speed, enum machine_mode mode, int cost)
345 {
346   *add_cost_ptr (speed, mode) = cost;
347 }
348
349 /* Return the cost of computing an add in MODE when optimizing for SPEED.  */
350
351 static inline int
352 add_cost (bool speed, enum machine_mode mode)
353 {
354   return *add_cost_ptr (speed, mode);
355 }
356
357 /* Subroutine of {set_,}neg_cost.  Not to be used otherwise.  */
358
359 static inline int *
360 neg_cost_ptr (bool speed, enum machine_mode mode)
361 {
362   return expmed_op_cost_ptr (&this_target_expmed->x_neg_cost, speed, mode);
363 }
364
365 /* Set the COST of computing a negation in MODE when optimizing for SPEED.  */
366
367 static inline void
368 set_neg_cost (bool speed, enum machine_mode mode, int cost)
369 {
370   *neg_cost_ptr (speed, mode) = cost;
371 }
372
373 /* Return the cost of computing a negation in MODE when optimizing for
374    SPEED.  */
375
376 static inline int
377 neg_cost (bool speed, enum machine_mode mode)
378 {
379   return *neg_cost_ptr (speed, mode);
380 }
381
382 /* Subroutine of {set_,}shift_cost.  Not to be used otherwise.  */
383
384 static inline int *
385 shift_cost_ptr (bool speed, enum machine_mode mode, int bits)
386 {
387   return expmed_op_cost_ptr (&this_target_expmed->x_shift_cost[bits],
388                              speed, mode);
389 }
390
391 /* Set the COST of doing a shift in MODE by BITS when optimizing for SPEED.  */
392
393 static inline void
394 set_shift_cost (bool speed, enum machine_mode mode, int bits, int cost)
395 {
396   *shift_cost_ptr (speed, mode, bits) = cost;
397 }
398
399 /* Return the cost of doing a shift in MODE by BITS when optimizing for
400    SPEED.  */
401
402 static inline int
403 shift_cost (bool speed, enum machine_mode mode, int bits)
404 {
405   return *shift_cost_ptr (speed, mode, bits);
406 }
407
408 /* Subroutine of {set_,}shiftadd_cost.  Not to be used otherwise.  */
409
410 static inline int *
411 shiftadd_cost_ptr (bool speed, enum machine_mode mode, int bits)
412 {
413   return expmed_op_cost_ptr (&this_target_expmed->x_shiftadd_cost[bits],
414                              speed, mode);
415 }
416
417 /* Set the COST of doing a shift in MODE by BITS followed by an add when
418    optimizing for SPEED.  */
419
420 static inline void
421 set_shiftadd_cost (bool speed, enum machine_mode mode, int bits, int cost)
422 {
423   *shiftadd_cost_ptr (speed, mode, bits) = cost;
424 }
425
426 /* Return the cost of doing a shift in MODE by BITS followed by an add
427    when optimizing for SPEED.  */
428
429 static inline int
430 shiftadd_cost (bool speed, enum machine_mode mode, int bits)
431 {
432   return *shiftadd_cost_ptr (speed, mode, bits);
433 }
434
435 /* Subroutine of {set_,}shiftsub0_cost.  Not to be used otherwise.  */
436
437 static inline int *
438 shiftsub0_cost_ptr (bool speed, enum machine_mode mode, int bits)
439 {
440   return expmed_op_cost_ptr (&this_target_expmed->x_shiftsub0_cost[bits],
441                              speed, mode);
442 }
443
444 /* Set the COST of doing a shift in MODE by BITS and then subtracting a
445    value when optimizing for SPEED.  */
446
447 static inline void
448 set_shiftsub0_cost (bool speed, enum machine_mode mode, int bits, int cost)
449 {
450   *shiftsub0_cost_ptr (speed, mode, bits) = cost;
451 }
452
453 /* Return the cost of doing a shift in MODE by BITS and then subtracting
454    a value when optimizing for SPEED.  */
455
456 static inline int
457 shiftsub0_cost (bool speed, enum machine_mode mode, int bits)
458 {
459   return *shiftsub0_cost_ptr (speed, mode, bits);
460 }
461
462 /* Subroutine of {set_,}shiftsub1_cost.  Not to be used otherwise.  */
463
464 static inline int *
465 shiftsub1_cost_ptr (bool speed, enum machine_mode mode, int bits)
466 {
467   return expmed_op_cost_ptr (&this_target_expmed->x_shiftsub1_cost[bits],
468                              speed, mode);
469 }
470
471 /* Set the COST of subtracting a shift in MODE by BITS from a value when
472    optimizing for SPEED.  */
473
474 static inline void
475 set_shiftsub1_cost (bool speed, enum machine_mode mode, int bits, int cost)
476 {
477   *shiftsub1_cost_ptr (speed, mode, bits) = cost;
478 }
479
480 /* Return the cost of subtracting a shift in MODE by BITS from a value
481    when optimizing for SPEED.  */
482
483 static inline int
484 shiftsub1_cost (bool speed, enum machine_mode mode, int bits)
485 {
486   return *shiftsub1_cost_ptr (speed, mode, bits);
487 }
488
489 /* Subroutine of {set_,}mul_cost.  Not to be used otherwise.  */
490
491 static inline int *
492 mul_cost_ptr (bool speed, enum machine_mode mode)
493 {
494   return expmed_op_cost_ptr (&this_target_expmed->x_mul_cost, speed, mode);
495 }
496
497 /* Set the COST of doing a multiplication in MODE when optimizing for
498    SPEED.  */
499
500 static inline void
501 set_mul_cost (bool speed, enum machine_mode mode, int cost)
502 {
503   *mul_cost_ptr (speed, mode) = cost;
504 }
505
506 /* Return the cost of doing a multiplication in MODE when optimizing
507    for SPEED.  */
508
509 static inline int
510 mul_cost (bool speed, enum machine_mode mode)
511 {
512   return *mul_cost_ptr (speed, mode);
513 }
514
515 /* Subroutine of {set_,}sdiv_cost.  Not to be used otherwise.  */
516
517 static inline int *
518 sdiv_cost_ptr (bool speed, enum machine_mode mode)
519 {
520   return expmed_op_cost_ptr (&this_target_expmed->x_sdiv_cost, speed, mode);
521 }
522
523 /* Set the COST of doing a signed division in MODE when optimizing
524    for SPEED.  */
525
526 static inline void
527 set_sdiv_cost (bool speed, enum machine_mode mode, int cost)
528 {
529   *sdiv_cost_ptr (speed, mode) = cost;
530 }
531
532 /* Return the cost of doing a signed division in MODE when optimizing
533    for SPEED.  */
534
535 static inline int
536 sdiv_cost (bool speed, enum machine_mode mode)
537 {
538   return *sdiv_cost_ptr (speed, mode);
539 }
540
541 /* Subroutine of {set_,}udiv_cost.  Not to be used otherwise.  */
542
543 static inline int *
544 udiv_cost_ptr (bool speed, enum machine_mode mode)
545 {
546   return expmed_op_cost_ptr (&this_target_expmed->x_udiv_cost, speed, mode);
547 }
548
549 /* Set the COST of doing an unsigned division in MODE when optimizing
550    for SPEED.  */
551
552 static inline void
553 set_udiv_cost (bool speed, enum machine_mode mode, int cost)
554 {
555   *udiv_cost_ptr (speed, mode) = cost;
556 }
557
558 /* Return the cost of doing an unsigned division in MODE when
559    optimizing for SPEED.  */
560
561 static inline int
562 udiv_cost (bool speed, enum machine_mode mode)
563 {
564   return *udiv_cost_ptr (speed, mode);
565 }
566
567 /* Subroutine of {set_,}mul_widen_cost.  Not to be used otherwise.  */
568
569 static inline int *
570 mul_widen_cost_ptr (bool speed, enum machine_mode mode)
571 {
572   gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
573
574   return &this_target_expmed->x_mul_widen_cost[speed][mode - MIN_MODE_INT];
575 }
576
577 /* Set the COST for computing a widening multiplication in MODE when
578    optimizing for SPEED.  */
579
580 static inline void
581 set_mul_widen_cost (bool speed, enum machine_mode mode, int cost)
582 {
583   *mul_widen_cost_ptr (speed, mode) = cost;
584 }
585
586 /* Return the cost for computing a widening multiplication in MODE when
587    optimizing for SPEED.  */
588
589 static inline int
590 mul_widen_cost (bool speed, enum machine_mode mode)
591 {
592   return *mul_widen_cost_ptr (speed, mode);
593 }
594
595 /* Subroutine of {set_,}mul_highpart_cost.  Not to be used otherwise.  */
596
597 static inline int *
598 mul_highpart_cost_ptr (bool speed, enum machine_mode mode)
599 {
600   gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
601
602   return &this_target_expmed->x_mul_highpart_cost[speed][mode - MIN_MODE_INT];
603 }
604
605 /* Set the COST for computing the high part of a multiplication in MODE
606    when optimizing for SPEED.  */
607
608 static inline void
609 set_mul_highpart_cost (bool speed, enum machine_mode mode, int cost)
610 {
611   *mul_highpart_cost_ptr (speed, mode) = cost;
612 }
613
614 /* Return the cost for computing the high part of a multiplication in MODE
615    when optimizing for SPEED.  */
616
617 static inline int
618 mul_highpart_cost (bool speed, enum machine_mode mode)
619 {
620   return *mul_highpart_cost_ptr (speed, mode);
621 }
622
623 /* Subroutine of {set_,}convert_cost.  Not to be used otherwise.  */
624
625 static inline int *
626 convert_cost_ptr (enum machine_mode to_mode, enum machine_mode from_mode,
627                   bool speed)
628 {
629   int to_idx = expmed_mode_index (to_mode);
630   int from_idx = expmed_mode_index (from_mode);
631
632   gcc_assert (IN_RANGE (to_idx, 0, NUM_MODE_IP_INT - 1));
633   gcc_assert (IN_RANGE (from_idx, 0, NUM_MODE_IP_INT - 1));
634
635   return &this_target_expmed->x_convert_cost[speed][to_idx][from_idx];
636 }
637
638 /* Set the COST for converting from FROM_MODE to TO_MODE when optimizing
639    for SPEED.  */
640
641 static inline void
642 set_convert_cost (enum machine_mode to_mode, enum machine_mode from_mode,
643                   bool speed, int cost)
644 {
645   *convert_cost_ptr (to_mode, from_mode, speed) = cost;
646 }
647
648 /* Return the cost for converting from FROM_MODE to TO_MODE when optimizing
649    for SPEED.  */
650
651 static inline int
652 convert_cost (enum machine_mode to_mode, enum machine_mode from_mode,
653               bool speed)
654 {
655   return *convert_cost_ptr (to_mode, from_mode, speed);
656 }
657
658 extern int mult_by_coeff_cost (HOST_WIDE_INT, enum machine_mode, bool);
659 #endif