missing Changelog
[official-gcc.git] / gcc / expmed.h
blob05d0c15bf5ef17e70845804c2a9586189e3e212e
1 /* Target-dependent costs for expmed.c.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
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.
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.
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/>. */
22 #ifndef EXPMED_H
23 #define EXPMED_H 1
25 enum alg_code {
26 alg_unknown,
27 alg_zero,
28 alg_m, alg_shift,
29 alg_add_t_m2,
30 alg_sub_t_m2,
31 alg_add_factor,
32 alg_sub_factor,
33 alg_add_t2_m,
34 alg_sub_t2_m,
35 alg_impossible
38 /* This structure holds the "cost" of a multiply sequence. The
39 "cost" field holds the total rtx_cost of every operator in the
40 synthetic multiplication sequence, hence cost(a op b) is defined
41 as rtx_cost(op) + cost(a) + cost(b), where cost(leaf) is zero.
42 The "latency" field holds the minimum possible latency of the
43 synthetic multiply, on a hypothetical infinitely parallel CPU.
44 This is the critical path, or the maximum height, of the expression
45 tree which is the sum of rtx_costs on the most expensive path from
46 any leaf to the root. Hence latency(a op b) is defined as zero for
47 leaves and rtx_cost(op) + max(latency(a), latency(b)) otherwise. */
49 struct mult_cost {
50 short cost; /* Total rtx_cost of the multiplication sequence. */
51 short latency; /* The latency of the multiplication sequence. */
54 /* This macro is used to compare a pointer to a mult_cost against an
55 single integer "rtx_cost" value. This is equivalent to the macro
56 CHEAPER_MULT_COST(X,Z) where Z = {Y,Y}. */
57 #define MULT_COST_LESS(X,Y) ((X)->cost < (Y) \
58 || ((X)->cost == (Y) && (X)->latency < (Y)))
60 /* This macro is used to compare two pointers to mult_costs against
61 each other. The macro returns true if X is cheaper than Y.
62 Currently, the cheaper of two mult_costs is the one with the
63 lower "cost". If "cost"s are tied, the lower latency is cheaper. */
64 #define CHEAPER_MULT_COST(X,Y) ((X)->cost < (Y)->cost \
65 || ((X)->cost == (Y)->cost \
66 && (X)->latency < (Y)->latency))
68 /* This structure records a sequence of operations.
69 `ops' is the number of operations recorded.
70 `cost' is their total cost.
71 The operations are stored in `op' and the corresponding
72 logarithms of the integer coefficients in `log'.
74 These are the operations:
75 alg_zero total := 0;
76 alg_m total := multiplicand;
77 alg_shift total := total * coeff
78 alg_add_t_m2 total := total + multiplicand * coeff;
79 alg_sub_t_m2 total := total - multiplicand * coeff;
80 alg_add_factor total := total * coeff + total;
81 alg_sub_factor total := total * coeff - total;
82 alg_add_t2_m total := total * coeff + multiplicand;
83 alg_sub_t2_m total := total * coeff - multiplicand;
85 The first operand must be either alg_zero or alg_m. */
87 struct algorithm
89 struct mult_cost cost;
90 short ops;
91 /* The size of the OP and LOG fields are not directly related to the
92 word size, but the worst-case algorithms will be if we have few
93 consecutive ones or zeros, i.e., a multiplicand like 10101010101...
94 In that case we will generate shift-by-2, add, shift-by-2, add,...,
95 in total wordsize operations. */
96 enum alg_code op[MAX_BITS_PER_WORD];
97 char log[MAX_BITS_PER_WORD];
100 /* The entry for our multiplication cache/hash table. */
101 struct alg_hash_entry {
102 /* The number we are multiplying by. */
103 unsigned HOST_WIDE_INT t;
105 /* The mode in which we are multiplying something by T. */
106 enum machine_mode mode;
108 /* The best multiplication algorithm for t. */
109 enum alg_code alg;
111 /* The cost of multiplication if ALG_CODE is not alg_impossible.
112 Otherwise, the cost within which multiplication by T is
113 impossible. */
114 struct mult_cost cost;
116 /* Optimized for speed? */
117 bool speed;
120 /* The number of cache/hash entries. */
121 #if HOST_BITS_PER_WIDE_INT == 64
122 #define NUM_ALG_HASH_ENTRIES 1031
123 #else
124 #define NUM_ALG_HASH_ENTRIES 307
125 #endif
127 #define NUM_MODE_INT \
128 (MAX_MODE_INT - MIN_MODE_INT + 1)
129 #define NUM_MODE_PARTIAL_INT \
130 (MIN_MODE_PARTIAL_INT == VOIDmode ? 0 \
131 : MAX_MODE_PARTIAL_INT - MIN_MODE_PARTIAL_INT + 1)
132 #define NUM_MODE_VECTOR_INT \
133 (MIN_MODE_VECTOR_INT == VOIDmode ? 0 \
134 : MAX_MODE_VECTOR_INT - MIN_MODE_VECTOR_INT + 1)
136 #define NUM_MODE_IP_INT (NUM_MODE_INT + NUM_MODE_PARTIAL_INT)
137 #define NUM_MODE_IPV_INT (NUM_MODE_IP_INT + NUM_MODE_VECTOR_INT)
139 struct expmed_op_cheap {
140 bool cheap[2][NUM_MODE_IPV_INT];
143 struct expmed_op_costs {
144 int cost[2][NUM_MODE_IPV_INT];
147 /* Target-dependent globals. */
148 struct target_expmed {
149 /* Each entry of ALG_HASH caches alg_code for some integer. This is
150 actually a hash table. If we have a collision, that the older
151 entry is kicked out. */
152 struct alg_hash_entry x_alg_hash[NUM_ALG_HASH_ENTRIES];
154 /* True if x_alg_hash might already have been used. */
155 bool x_alg_hash_used_p;
157 /* Nonzero means divides or modulus operations are relatively cheap for
158 powers of two, so don't use branches; emit the operation instead.
159 Usually, this will mean that the MD file will emit non-branch
160 sequences. */
161 struct expmed_op_cheap x_sdiv_pow2_cheap;
162 struct expmed_op_cheap x_smod_pow2_cheap;
164 /* Cost of various pieces of RTL. Note that some of these are indexed by
165 shift count and some by mode. */
166 int x_zero_cost[2];
167 struct expmed_op_costs x_add_cost;
168 struct expmed_op_costs x_neg_cost;
169 struct expmed_op_costs x_shift_cost[MAX_BITS_PER_WORD];
170 struct expmed_op_costs x_shiftadd_cost[MAX_BITS_PER_WORD];
171 struct expmed_op_costs x_shiftsub0_cost[MAX_BITS_PER_WORD];
172 struct expmed_op_costs x_shiftsub1_cost[MAX_BITS_PER_WORD];
173 struct expmed_op_costs x_mul_cost;
174 struct expmed_op_costs x_sdiv_cost;
175 struct expmed_op_costs x_udiv_cost;
176 int x_mul_widen_cost[2][NUM_MODE_INT];
177 int x_mul_highpart_cost[2][NUM_MODE_INT];
179 /* Conversion costs are only defined between two scalar integer modes
180 of different sizes. The first machine mode is the destination mode,
181 and the second is the source mode. */
182 int x_convert_cost[2][NUM_MODE_IP_INT][NUM_MODE_IP_INT];
185 extern struct target_expmed default_target_expmed;
186 #if SWITCHABLE_TARGET
187 extern struct target_expmed *this_target_expmed;
188 #else
189 #define this_target_expmed (&default_target_expmed)
190 #endif
192 /* Return a pointer to the alg_hash_entry at IDX. */
194 static inline struct alg_hash_entry *
195 alg_hash_entry_ptr (int idx)
197 return &this_target_expmed->x_alg_hash[idx];
200 /* Return true if the x_alg_hash field might have been used. */
202 static inline bool
203 alg_hash_used_p (void)
205 return this_target_expmed->x_alg_hash_used_p;
208 /* Set whether the x_alg_hash field might have been used. */
210 static inline void
211 set_alg_hash_used_p (bool usedp)
213 this_target_expmed->x_alg_hash_used_p = usedp;
216 /* Compute an index into the cost arrays by mode class. */
218 static inline int
219 expmed_mode_index (enum machine_mode mode)
221 switch (GET_MODE_CLASS (mode))
223 case MODE_INT:
224 return mode - MIN_MODE_INT;
225 case MODE_PARTIAL_INT:
226 return mode - MIN_MODE_PARTIAL_INT + NUM_MODE_INT;
227 case MODE_VECTOR_INT:
228 return mode - MIN_MODE_VECTOR_INT + NUM_MODE_IP_INT;
229 default:
230 gcc_unreachable ();
234 /* Return a pointer to a boolean contained in EOC indicating whether
235 a particular operation performed in MODE is cheap when optimizing
236 for SPEED. */
238 static inline bool *
239 expmed_op_cheap_ptr (struct expmed_op_cheap *eoc, bool speed,
240 enum machine_mode mode)
242 int idx = expmed_mode_index (mode);
243 return &eoc->cheap[speed][idx];
246 /* Return a pointer to a cost contained in COSTS when a particular
247 operation is performed in MODE when optimizing for SPEED. */
249 static inline int *
250 expmed_op_cost_ptr (struct expmed_op_costs *costs, bool speed,
251 enum machine_mode mode)
253 int idx = expmed_mode_index (mode);
254 return &costs->cost[speed][idx];
257 /* Subroutine of {set_,}sdiv_pow2_cheap. Not to be used otherwise. */
259 static inline bool *
260 sdiv_pow2_cheap_ptr (bool speed, enum machine_mode mode)
262 return expmed_op_cheap_ptr (&this_target_expmed->x_sdiv_pow2_cheap,
263 speed, mode);
266 /* Set whether a signed division by a power of 2 is cheap in MODE
267 when optimizing for SPEED. */
269 static inline void
270 set_sdiv_pow2_cheap (bool speed, enum machine_mode mode, bool cheap_p)
272 *sdiv_pow2_cheap_ptr (speed, mode) = cheap_p;
275 /* Return whether a signed division by a power of 2 is cheap in MODE
276 when optimizing for SPEED. */
278 static inline bool
279 sdiv_pow2_cheap (bool speed, enum machine_mode mode)
281 return *sdiv_pow2_cheap_ptr (speed, mode);
284 /* Subroutine of {set_,}smod_pow2_cheap. Not to be used otherwise. */
286 static inline bool *
287 smod_pow2_cheap_ptr (bool speed, enum machine_mode mode)
289 return expmed_op_cheap_ptr (&this_target_expmed->x_smod_pow2_cheap,
290 speed, mode);
293 /* Set whether a signed modulo by a power of 2 is CHEAP in MODE when
294 optimizing for SPEED. */
296 static inline void
297 set_smod_pow2_cheap (bool speed, enum machine_mode mode, bool cheap)
299 *smod_pow2_cheap_ptr (speed, mode) = cheap;
302 /* Return whether a signed modulo by a power of 2 is cheap in MODE
303 when optimizing for SPEED. */
305 static inline bool
306 smod_pow2_cheap (bool speed, enum machine_mode mode)
308 return *smod_pow2_cheap_ptr (speed, mode);
311 /* Subroutine of {set_,}zero_cost. Not to be used otherwise. */
313 static inline int *
314 zero_cost_ptr (bool speed)
316 return &this_target_expmed->x_zero_cost[speed];
319 /* Set the COST of loading zero when optimizing for SPEED. */
321 static inline void
322 set_zero_cost (bool speed, int cost)
324 *zero_cost_ptr (speed) = cost;
327 /* Return the COST of loading zero when optimizing for SPEED. */
329 static inline int
330 zero_cost (bool speed)
332 return *zero_cost_ptr (speed);
335 /* Subroutine of {set_,}add_cost. Not to be used otherwise. */
337 static inline int *
338 add_cost_ptr (bool speed, enum machine_mode mode)
340 return expmed_op_cost_ptr (&this_target_expmed->x_add_cost, speed, mode);
343 /* Set the COST of computing an add in MODE when optimizing for SPEED. */
345 static inline void
346 set_add_cost (bool speed, enum machine_mode mode, int cost)
348 *add_cost_ptr (speed, mode) = cost;
351 /* Return the cost of computing an add in MODE when optimizing for SPEED. */
353 static inline int
354 add_cost (bool speed, enum machine_mode mode)
356 return *add_cost_ptr (speed, mode);
359 /* Subroutine of {set_,}neg_cost. Not to be used otherwise. */
361 static inline int *
362 neg_cost_ptr (bool speed, enum machine_mode mode)
364 return expmed_op_cost_ptr (&this_target_expmed->x_neg_cost, speed, mode);
367 /* Set the COST of computing a negation in MODE when optimizing for SPEED. */
369 static inline void
370 set_neg_cost (bool speed, enum machine_mode mode, int cost)
372 *neg_cost_ptr (speed, mode) = cost;
375 /* Return the cost of computing a negation in MODE when optimizing for
376 SPEED. */
378 static inline int
379 neg_cost (bool speed, enum machine_mode mode)
381 return *neg_cost_ptr (speed, mode);
384 /* Subroutine of {set_,}shift_cost. Not to be used otherwise. */
386 static inline int *
387 shift_cost_ptr (bool speed, enum machine_mode mode, int bits)
389 return expmed_op_cost_ptr (&this_target_expmed->x_shift_cost[bits],
390 speed, mode);
393 /* Set the COST of doing a shift in MODE by BITS when optimizing for SPEED. */
395 static inline void
396 set_shift_cost (bool speed, enum machine_mode mode, int bits, int cost)
398 *shift_cost_ptr (speed, mode, bits) = cost;
401 /* Return the cost of doing a shift in MODE by BITS when optimizing for
402 SPEED. */
404 static inline int
405 shift_cost (bool speed, enum machine_mode mode, int bits)
407 return *shift_cost_ptr (speed, mode, bits);
410 /* Subroutine of {set_,}shiftadd_cost. Not to be used otherwise. */
412 static inline int *
413 shiftadd_cost_ptr (bool speed, enum machine_mode mode, int bits)
415 return expmed_op_cost_ptr (&this_target_expmed->x_shiftadd_cost[bits],
416 speed, mode);
419 /* Set the COST of doing a shift in MODE by BITS followed by an add when
420 optimizing for SPEED. */
422 static inline void
423 set_shiftadd_cost (bool speed, enum machine_mode mode, int bits, int cost)
425 *shiftadd_cost_ptr (speed, mode, bits) = cost;
428 /* Return the cost of doing a shift in MODE by BITS followed by an add
429 when optimizing for SPEED. */
431 static inline int
432 shiftadd_cost (bool speed, enum machine_mode mode, int bits)
434 return *shiftadd_cost_ptr (speed, mode, bits);
437 /* Subroutine of {set_,}shiftsub0_cost. Not to be used otherwise. */
439 static inline int *
440 shiftsub0_cost_ptr (bool speed, enum machine_mode mode, int bits)
442 return expmed_op_cost_ptr (&this_target_expmed->x_shiftsub0_cost[bits],
443 speed, mode);
446 /* Set the COST of doing a shift in MODE by BITS and then subtracting a
447 value when optimizing for SPEED. */
449 static inline void
450 set_shiftsub0_cost (bool speed, enum machine_mode mode, int bits, int cost)
452 *shiftsub0_cost_ptr (speed, mode, bits) = cost;
455 /* Return the cost of doing a shift in MODE by BITS and then subtracting
456 a value when optimizing for SPEED. */
458 static inline int
459 shiftsub0_cost (bool speed, enum machine_mode mode, int bits)
461 return *shiftsub0_cost_ptr (speed, mode, bits);
464 /* Subroutine of {set_,}shiftsub1_cost. Not to be used otherwise. */
466 static inline int *
467 shiftsub1_cost_ptr (bool speed, enum machine_mode mode, int bits)
469 return expmed_op_cost_ptr (&this_target_expmed->x_shiftsub1_cost[bits],
470 speed, mode);
473 /* Set the COST of subtracting a shift in MODE by BITS from a value when
474 optimizing for SPEED. */
476 static inline void
477 set_shiftsub1_cost (bool speed, enum machine_mode mode, int bits, int cost)
479 *shiftsub1_cost_ptr (speed, mode, bits) = cost;
482 /* Return the cost of subtracting a shift in MODE by BITS from a value
483 when optimizing for SPEED. */
485 static inline int
486 shiftsub1_cost (bool speed, enum machine_mode mode, int bits)
488 return *shiftsub1_cost_ptr (speed, mode, bits);
491 /* Subroutine of {set_,}mul_cost. Not to be used otherwise. */
493 static inline int *
494 mul_cost_ptr (bool speed, enum machine_mode mode)
496 return expmed_op_cost_ptr (&this_target_expmed->x_mul_cost, speed, mode);
499 /* Set the COST of doing a multiplication in MODE when optimizing for
500 SPEED. */
502 static inline void
503 set_mul_cost (bool speed, enum machine_mode mode, int cost)
505 *mul_cost_ptr (speed, mode) = cost;
508 /* Return the cost of doing a multiplication in MODE when optimizing
509 for SPEED. */
511 static inline int
512 mul_cost (bool speed, enum machine_mode mode)
514 return *mul_cost_ptr (speed, mode);
517 /* Subroutine of {set_,}sdiv_cost. Not to be used otherwise. */
519 static inline int *
520 sdiv_cost_ptr (bool speed, enum machine_mode mode)
522 return expmed_op_cost_ptr (&this_target_expmed->x_sdiv_cost, speed, mode);
525 /* Set the COST of doing a signed division in MODE when optimizing
526 for SPEED. */
528 static inline void
529 set_sdiv_cost (bool speed, enum machine_mode mode, int cost)
531 *sdiv_cost_ptr (speed, mode) = cost;
534 /* Return the cost of doing a signed division in MODE when optimizing
535 for SPEED. */
537 static inline int
538 sdiv_cost (bool speed, enum machine_mode mode)
540 return *sdiv_cost_ptr (speed, mode);
543 /* Subroutine of {set_,}udiv_cost. Not to be used otherwise. */
545 static inline int *
546 udiv_cost_ptr (bool speed, enum machine_mode mode)
548 return expmed_op_cost_ptr (&this_target_expmed->x_udiv_cost, speed, mode);
551 /* Set the COST of doing an unsigned division in MODE when optimizing
552 for SPEED. */
554 static inline void
555 set_udiv_cost (bool speed, enum machine_mode mode, int cost)
557 *udiv_cost_ptr (speed, mode) = cost;
560 /* Return the cost of doing an unsigned division in MODE when
561 optimizing for SPEED. */
563 static inline int
564 udiv_cost (bool speed, enum machine_mode mode)
566 return *udiv_cost_ptr (speed, mode);
569 /* Subroutine of {set_,}mul_widen_cost. Not to be used otherwise. */
571 static inline int *
572 mul_widen_cost_ptr (bool speed, enum machine_mode mode)
574 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
576 return &this_target_expmed->x_mul_widen_cost[speed][mode - MIN_MODE_INT];
579 /* Set the COST for computing a widening multiplication in MODE when
580 optimizing for SPEED. */
582 static inline void
583 set_mul_widen_cost (bool speed, enum machine_mode mode, int cost)
585 *mul_widen_cost_ptr (speed, mode) = cost;
588 /* Return the cost for computing a widening multiplication in MODE when
589 optimizing for SPEED. */
591 static inline int
592 mul_widen_cost (bool speed, enum machine_mode mode)
594 return *mul_widen_cost_ptr (speed, mode);
597 /* Subroutine of {set_,}mul_highpart_cost. Not to be used otherwise. */
599 static inline int *
600 mul_highpart_cost_ptr (bool speed, enum machine_mode mode)
602 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
604 return &this_target_expmed->x_mul_highpart_cost[speed][mode - MIN_MODE_INT];
607 /* Set the COST for computing the high part of a multiplication in MODE
608 when optimizing for SPEED. */
610 static inline void
611 set_mul_highpart_cost (bool speed, enum machine_mode mode, int cost)
613 *mul_highpart_cost_ptr (speed, mode) = cost;
616 /* Return the cost for computing the high part of a multiplication in MODE
617 when optimizing for SPEED. */
619 static inline int
620 mul_highpart_cost (bool speed, enum machine_mode mode)
622 return *mul_highpart_cost_ptr (speed, mode);
625 /* Subroutine of {set_,}convert_cost. Not to be used otherwise. */
627 static inline int *
628 convert_cost_ptr (enum machine_mode to_mode, enum machine_mode from_mode,
629 bool speed)
631 int to_idx = expmed_mode_index (to_mode);
632 int from_idx = expmed_mode_index (from_mode);
634 gcc_assert (IN_RANGE (to_idx, 0, NUM_MODE_IP_INT - 1));
635 gcc_assert (IN_RANGE (from_idx, 0, NUM_MODE_IP_INT - 1));
637 return &this_target_expmed->x_convert_cost[speed][to_idx][from_idx];
640 /* Set the COST for converting from FROM_MODE to TO_MODE when optimizing
641 for SPEED. */
643 static inline void
644 set_convert_cost (enum machine_mode to_mode, enum machine_mode from_mode,
645 bool speed, int cost)
647 *convert_cost_ptr (to_mode, from_mode, speed) = cost;
650 /* Return the cost for converting from FROM_MODE to TO_MODE when optimizing
651 for SPEED. */
653 static inline int
654 convert_cost (enum machine_mode to_mode, enum machine_mode from_mode,
655 bool speed)
657 return *convert_cost_ptr (to_mode, from_mode, speed);
660 extern int mult_by_coeff_cost (HOST_WIDE_INT, enum machine_mode, bool);
661 #endif