Merge trunk version 206792 into gupc branch.
[official-gcc.git] / gcc / expmed.h
blob9681e41fbde599e4d7b1cee35ac5043188c4a508
1 /* Target-dependent costs for expmed.c.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #ifndef EXPMED_H
21 #define EXPMED_H 1
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
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. */
47 struct mult_cost {
48 short cost; /* Total rtx_cost of the multiplication sequence. */
49 short latency; /* The latency of the multiplication sequence. */
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)))
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))
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'.
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;
83 The first operand must be either alg_zero or alg_m. */
85 struct algorithm
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];
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;
103 /* The mode in which we are multiplying something by T. */
104 enum machine_mode mode;
106 /* The best multiplication algorithm for t. */
107 enum alg_code alg;
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;
114 /* Optimized for speed? */
115 bool speed;
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
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)
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)
137 struct expmed_op_cheap {
138 bool cheap[2][NUM_MODE_IPV_INT];
141 struct expmed_op_costs {
142 int cost[2][NUM_MODE_IPV_INT];
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];
152 /* True if x_alg_hash might already have been used. */
153 bool x_alg_hash_used_p;
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;
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];
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];
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
190 /* Return a pointer to the alg_hash_entry at IDX. */
192 static inline struct alg_hash_entry *
193 alg_hash_entry_ptr (int idx)
195 return &this_target_expmed->x_alg_hash[idx];
198 /* Return true if the x_alg_hash field might have been used. */
200 static inline bool
201 alg_hash_used_p (void)
203 return this_target_expmed->x_alg_hash_used_p;
206 /* Set whether the x_alg_hash field might have been used. */
208 static inline void
209 set_alg_hash_used_p (bool usedp)
211 this_target_expmed->x_alg_hash_used_p = usedp;
214 /* Compute an index into the cost arrays by mode class. */
216 static inline int
217 expmed_mode_index (enum machine_mode mode)
219 switch (GET_MODE_CLASS (mode))
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 ();
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. */
236 static inline bool *
237 expmed_op_cheap_ptr (struct expmed_op_cheap *eoc, bool speed,
238 enum machine_mode mode)
240 int idx = expmed_mode_index (mode);
241 return &eoc->cheap[speed][idx];
244 /* Return a pointer to a cost contained in COSTS when a particular
245 operation is performed in MODE when optimizing for SPEED. */
247 static inline int *
248 expmed_op_cost_ptr (struct expmed_op_costs *costs, bool speed,
249 enum machine_mode mode)
251 int idx = expmed_mode_index (mode);
252 return &costs->cost[speed][idx];
255 /* Subroutine of {set_,}sdiv_pow2_cheap. Not to be used otherwise. */
257 static inline bool *
258 sdiv_pow2_cheap_ptr (bool speed, enum machine_mode mode)
260 return expmed_op_cheap_ptr (&this_target_expmed->x_sdiv_pow2_cheap,
261 speed, mode);
264 /* Set whether a signed division by a power of 2 is cheap in MODE
265 when optimizing for SPEED. */
267 static inline void
268 set_sdiv_pow2_cheap (bool speed, enum machine_mode mode, bool cheap_p)
270 *sdiv_pow2_cheap_ptr (speed, mode) = cheap_p;
273 /* Return whether a signed division by a power of 2 is cheap in MODE
274 when optimizing for SPEED. */
276 static inline bool
277 sdiv_pow2_cheap (bool speed, enum machine_mode mode)
279 return *sdiv_pow2_cheap_ptr (speed, mode);
282 /* Subroutine of {set_,}smod_pow2_cheap. Not to be used otherwise. */
284 static inline bool *
285 smod_pow2_cheap_ptr (bool speed, enum machine_mode mode)
287 return expmed_op_cheap_ptr (&this_target_expmed->x_smod_pow2_cheap,
288 speed, mode);
291 /* Set whether a signed modulo by a power of 2 is CHEAP in MODE when
292 optimizing for SPEED. */
294 static inline void
295 set_smod_pow2_cheap (bool speed, enum machine_mode mode, bool cheap)
297 *smod_pow2_cheap_ptr (speed, mode) = cheap;
300 /* Return whether a signed modulo by a power of 2 is cheap in MODE
301 when optimizing for SPEED. */
303 static inline bool
304 smod_pow2_cheap (bool speed, enum machine_mode mode)
306 return *smod_pow2_cheap_ptr (speed, mode);
309 /* Subroutine of {set_,}zero_cost. Not to be used otherwise. */
311 static inline int *
312 zero_cost_ptr (bool speed)
314 return &this_target_expmed->x_zero_cost[speed];
317 /* Set the COST of loading zero when optimizing for SPEED. */
319 static inline void
320 set_zero_cost (bool speed, int cost)
322 *zero_cost_ptr (speed) = cost;
325 /* Return the COST of loading zero when optimizing for SPEED. */
327 static inline int
328 zero_cost (bool speed)
330 return *zero_cost_ptr (speed);
333 /* Subroutine of {set_,}add_cost. Not to be used otherwise. */
335 static inline int *
336 add_cost_ptr (bool speed, enum machine_mode mode)
338 return expmed_op_cost_ptr (&this_target_expmed->x_add_cost, speed, mode);
341 /* Set the COST of computing an add in MODE when optimizing for SPEED. */
343 static inline void
344 set_add_cost (bool speed, enum machine_mode mode, int cost)
346 *add_cost_ptr (speed, mode) = cost;
349 /* Return the cost of computing an add in MODE when optimizing for SPEED. */
351 static inline int
352 add_cost (bool speed, enum machine_mode mode)
354 return *add_cost_ptr (speed, mode);
357 /* Subroutine of {set_,}neg_cost. Not to be used otherwise. */
359 static inline int *
360 neg_cost_ptr (bool speed, enum machine_mode mode)
362 return expmed_op_cost_ptr (&this_target_expmed->x_neg_cost, speed, mode);
365 /* Set the COST of computing a negation in MODE when optimizing for SPEED. */
367 static inline void
368 set_neg_cost (bool speed, enum machine_mode mode, int cost)
370 *neg_cost_ptr (speed, mode) = cost;
373 /* Return the cost of computing a negation in MODE when optimizing for
374 SPEED. */
376 static inline int
377 neg_cost (bool speed, enum machine_mode mode)
379 return *neg_cost_ptr (speed, mode);
382 /* Subroutine of {set_,}shift_cost. Not to be used otherwise. */
384 static inline int *
385 shift_cost_ptr (bool speed, enum machine_mode mode, int bits)
387 return expmed_op_cost_ptr (&this_target_expmed->x_shift_cost[bits],
388 speed, mode);
391 /* Set the COST of doing a shift in MODE by BITS when optimizing for SPEED. */
393 static inline void
394 set_shift_cost (bool speed, enum machine_mode mode, int bits, int cost)
396 *shift_cost_ptr (speed, mode, bits) = cost;
399 /* Return the cost of doing a shift in MODE by BITS when optimizing for
400 SPEED. */
402 static inline int
403 shift_cost (bool speed, enum machine_mode mode, int bits)
405 return *shift_cost_ptr (speed, mode, bits);
408 /* Subroutine of {set_,}shiftadd_cost. Not to be used otherwise. */
410 static inline int *
411 shiftadd_cost_ptr (bool speed, enum machine_mode mode, int bits)
413 return expmed_op_cost_ptr (&this_target_expmed->x_shiftadd_cost[bits],
414 speed, mode);
417 /* Set the COST of doing a shift in MODE by BITS followed by an add when
418 optimizing for SPEED. */
420 static inline void
421 set_shiftadd_cost (bool speed, enum machine_mode mode, int bits, int cost)
423 *shiftadd_cost_ptr (speed, mode, bits) = cost;
426 /* Return the cost of doing a shift in MODE by BITS followed by an add
427 when optimizing for SPEED. */
429 static inline int
430 shiftadd_cost (bool speed, enum machine_mode mode, int bits)
432 return *shiftadd_cost_ptr (speed, mode, bits);
435 /* Subroutine of {set_,}shiftsub0_cost. Not to be used otherwise. */
437 static inline int *
438 shiftsub0_cost_ptr (bool speed, enum machine_mode mode, int bits)
440 return expmed_op_cost_ptr (&this_target_expmed->x_shiftsub0_cost[bits],
441 speed, mode);
444 /* Set the COST of doing a shift in MODE by BITS and then subtracting a
445 value when optimizing for SPEED. */
447 static inline void
448 set_shiftsub0_cost (bool speed, enum machine_mode mode, int bits, int cost)
450 *shiftsub0_cost_ptr (speed, mode, bits) = cost;
453 /* Return the cost of doing a shift in MODE by BITS and then subtracting
454 a value when optimizing for SPEED. */
456 static inline int
457 shiftsub0_cost (bool speed, enum machine_mode mode, int bits)
459 return *shiftsub0_cost_ptr (speed, mode, bits);
462 /* Subroutine of {set_,}shiftsub1_cost. Not to be used otherwise. */
464 static inline int *
465 shiftsub1_cost_ptr (bool speed, enum machine_mode mode, int bits)
467 return expmed_op_cost_ptr (&this_target_expmed->x_shiftsub1_cost[bits],
468 speed, mode);
471 /* Set the COST of subtracting a shift in MODE by BITS from a value when
472 optimizing for SPEED. */
474 static inline void
475 set_shiftsub1_cost (bool speed, enum machine_mode mode, int bits, int cost)
477 *shiftsub1_cost_ptr (speed, mode, bits) = cost;
480 /* Return the cost of subtracting a shift in MODE by BITS from a value
481 when optimizing for SPEED. */
483 static inline int
484 shiftsub1_cost (bool speed, enum machine_mode mode, int bits)
486 return *shiftsub1_cost_ptr (speed, mode, bits);
489 /* Subroutine of {set_,}mul_cost. Not to be used otherwise. */
491 static inline int *
492 mul_cost_ptr (bool speed, enum machine_mode mode)
494 return expmed_op_cost_ptr (&this_target_expmed->x_mul_cost, speed, mode);
497 /* Set the COST of doing a multiplication in MODE when optimizing for
498 SPEED. */
500 static inline void
501 set_mul_cost (bool speed, enum machine_mode mode, int cost)
503 *mul_cost_ptr (speed, mode) = cost;
506 /* Return the cost of doing a multiplication in MODE when optimizing
507 for SPEED. */
509 static inline int
510 mul_cost (bool speed, enum machine_mode mode)
512 return *mul_cost_ptr (speed, mode);
515 /* Subroutine of {set_,}sdiv_cost. Not to be used otherwise. */
517 static inline int *
518 sdiv_cost_ptr (bool speed, enum machine_mode mode)
520 return expmed_op_cost_ptr (&this_target_expmed->x_sdiv_cost, speed, mode);
523 /* Set the COST of doing a signed division in MODE when optimizing
524 for SPEED. */
526 static inline void
527 set_sdiv_cost (bool speed, enum machine_mode mode, int cost)
529 *sdiv_cost_ptr (speed, mode) = cost;
532 /* Return the cost of doing a signed division in MODE when optimizing
533 for SPEED. */
535 static inline int
536 sdiv_cost (bool speed, enum machine_mode mode)
538 return *sdiv_cost_ptr (speed, mode);
541 /* Subroutine of {set_,}udiv_cost. Not to be used otherwise. */
543 static inline int *
544 udiv_cost_ptr (bool speed, enum machine_mode mode)
546 return expmed_op_cost_ptr (&this_target_expmed->x_udiv_cost, speed, mode);
549 /* Set the COST of doing an unsigned division in MODE when optimizing
550 for SPEED. */
552 static inline void
553 set_udiv_cost (bool speed, enum machine_mode mode, int cost)
555 *udiv_cost_ptr (speed, mode) = cost;
558 /* Return the cost of doing an unsigned division in MODE when
559 optimizing for SPEED. */
561 static inline int
562 udiv_cost (bool speed, enum machine_mode mode)
564 return *udiv_cost_ptr (speed, mode);
567 /* Subroutine of {set_,}mul_widen_cost. Not to be used otherwise. */
569 static inline int *
570 mul_widen_cost_ptr (bool speed, enum machine_mode mode)
572 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
574 return &this_target_expmed->x_mul_widen_cost[speed][mode - MIN_MODE_INT];
577 /* Set the COST for computing a widening multiplication in MODE when
578 optimizing for SPEED. */
580 static inline void
581 set_mul_widen_cost (bool speed, enum machine_mode mode, int cost)
583 *mul_widen_cost_ptr (speed, mode) = cost;
586 /* Return the cost for computing a widening multiplication in MODE when
587 optimizing for SPEED. */
589 static inline int
590 mul_widen_cost (bool speed, enum machine_mode mode)
592 return *mul_widen_cost_ptr (speed, mode);
595 /* Subroutine of {set_,}mul_highpart_cost. Not to be used otherwise. */
597 static inline int *
598 mul_highpart_cost_ptr (bool speed, enum machine_mode mode)
600 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
602 return &this_target_expmed->x_mul_highpart_cost[speed][mode - MIN_MODE_INT];
605 /* Set the COST for computing the high part of a multiplication in MODE
606 when optimizing for SPEED. */
608 static inline void
609 set_mul_highpart_cost (bool speed, enum machine_mode mode, int cost)
611 *mul_highpart_cost_ptr (speed, mode) = cost;
614 /* Return the cost for computing the high part of a multiplication in MODE
615 when optimizing for SPEED. */
617 static inline int
618 mul_highpart_cost (bool speed, enum machine_mode mode)
620 return *mul_highpart_cost_ptr (speed, mode);
623 /* Subroutine of {set_,}convert_cost. Not to be used otherwise. */
625 static inline int *
626 convert_cost_ptr (enum machine_mode to_mode, enum machine_mode from_mode,
627 bool speed)
629 int to_idx = expmed_mode_index (to_mode);
630 int from_idx = expmed_mode_index (from_mode);
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));
635 return &this_target_expmed->x_convert_cost[speed][to_idx][from_idx];
638 /* Set the COST for converting from FROM_MODE to TO_MODE when optimizing
639 for SPEED. */
641 static inline void
642 set_convert_cost (enum machine_mode to_mode, enum machine_mode from_mode,
643 bool speed, int cost)
645 *convert_cost_ptr (to_mode, from_mode, speed) = cost;
648 /* Return the cost for converting from FROM_MODE to TO_MODE when optimizing
649 for SPEED. */
651 static inline int
652 convert_cost (enum machine_mode to_mode, enum machine_mode from_mode,
653 bool speed)
655 return *convert_cost_ptr (to_mode, from_mode, speed);
658 extern int mult_by_coeff_cost (HOST_WIDE_INT, enum machine_mode, bool);
659 #endif