]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/expmed.h
Allow MODE_PARTIAL_INT in expmed costs
[thirdparty/gcc.git] / gcc / expmed.h
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.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option; any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #ifndef EXPMED_H
23 #define EXPMED_H 1
24
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
36 };
37
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. */
48
49 struct mult_cost {
50 short cost; /* Total rtx_cost of the multiplication sequence. */
51 short latency; /* The latency of the multiplication sequence. */
52 };
53
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)))
59
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))
67
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'.
73
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;
84
85 The first operand must be either alg_zero or alg_m. */
86
87 struct algorithm
88 {
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];
98 };
99
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;
104
105 /* The mode in which we are multiplying something by T. */
106 enum machine_mode mode;
107
108 /* The best multiplication algorithm for t. */
109 enum alg_code alg;
110
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;
115
116 /* Optimized for speed? */
117 bool speed;
118 };
119
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
126
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)
135
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)
138
139 struct expmed_op_cheap {
140 bool cheap[2][NUM_MODE_IPV_INT];
141 };
142
143 struct expmed_op_costs {
144 int cost[2][NUM_MODE_IPV_INT];
145 };
146
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];
153
154 /* True if x_alg_hash might already have been used. */
155 bool x_alg_hash_used_p;
156
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;
163
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];
178
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];
183 };
184
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
191
192 /* Return a pointer to the alg_hash_entry at IDX. */
193
194 static inline struct alg_hash_entry *
195 alg_hash_entry_ptr (int idx)
196 {
197 return &this_target_expmed->x_alg_hash[idx];
198 }
199
200 /* Return true if the x_alg_hash field might have been used. */
201
202 static inline bool
203 alg_hash_used_p (void)
204 {
205 return this_target_expmed->x_alg_hash_used_p;
206 }
207
208 /* Set whether the x_alg_hash field might have been used. */
209
210 static inline void
211 set_alg_hash_used_p (bool usedp)
212 {
213 this_target_expmed->x_alg_hash_used_p = usedp;
214 }
215
216 /* Compute an index into the cost arrays by mode class. */
217
218 static inline int
219 expmed_mode_index (enum machine_mode mode)
220 {
221 switch (GET_MODE_CLASS (mode))
222 {
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 ();
231 }
232 }
233
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. */
237
238 static inline bool *
239 expmed_op_cheap_ptr (struct expmed_op_cheap *eoc, bool speed,
240 enum machine_mode mode)
241 {
242 int idx = expmed_mode_index (mode);
243 return &eoc->cheap[speed][idx];
244 }
245
246 /* Return a pointer to a cost contained in COSTS when a particular
247 operation is performed in MODE when optimizing for SPEED. */
248
249 static inline int *
250 expmed_op_cost_ptr (struct expmed_op_costs *costs, bool speed,
251 enum machine_mode mode)
252 {
253 int idx = expmed_mode_index (mode);
254 return &costs->cost[speed][idx];
255 }
256
257 /* Subroutine of {set_,}sdiv_pow2_cheap. Not to be used otherwise. */
258
259 static inline bool *
260 sdiv_pow2_cheap_ptr (bool speed, enum machine_mode mode)
261 {
262 return expmed_op_cheap_ptr (&this_target_expmed->x_sdiv_pow2_cheap,
263 speed, mode);
264 }
265
266 /* Set whether a signed division by a power of 2 is cheap in MODE
267 when optimizing for SPEED. */
268
269 static inline void
270 set_sdiv_pow2_cheap (bool speed, enum machine_mode mode, bool cheap_p)
271 {
272 *sdiv_pow2_cheap_ptr (speed, mode) = cheap_p;
273 }
274
275 /* Return whether a signed division by a power of 2 is cheap in MODE
276 when optimizing for SPEED. */
277
278 static inline bool
279 sdiv_pow2_cheap (bool speed, enum machine_mode mode)
280 {
281 return *sdiv_pow2_cheap_ptr (speed, mode);
282 }
283
284 /* Subroutine of {set_,}smod_pow2_cheap. Not to be used otherwise. */
285
286 static inline bool *
287 smod_pow2_cheap_ptr (bool speed, enum machine_mode mode)
288 {
289 return expmed_op_cheap_ptr (&this_target_expmed->x_smod_pow2_cheap,
290 speed, mode);
291 }
292
293 /* Set whether a signed modulo by a power of 2 is CHEAP in MODE when
294 optimizing for SPEED. */
295
296 static inline void
297 set_smod_pow2_cheap (bool speed, enum machine_mode mode, bool cheap)
298 {
299 *smod_pow2_cheap_ptr (speed, mode) = cheap;
300 }
301
302 /* Return whether a signed modulo by a power of 2 is cheap in MODE
303 when optimizing for SPEED. */
304
305 static inline bool
306 smod_pow2_cheap (bool speed, enum machine_mode mode)
307 {
308 return *smod_pow2_cheap_ptr (speed, mode);
309 }
310
311 /* Subroutine of {set_,}zero_cost. Not to be used otherwise. */
312
313 static inline int *
314 zero_cost_ptr (bool speed)
315 {
316 return &this_target_expmed->x_zero_cost[speed];
317 }
318
319 /* Set the COST of loading zero when optimizing for SPEED. */
320
321 static inline void
322 set_zero_cost (bool speed, int cost)
323 {
324 *zero_cost_ptr (speed) = cost;
325 }
326
327 /* Return the COST of loading zero when optimizing for SPEED. */
328
329 static inline int
330 zero_cost (bool speed)
331 {
332 return *zero_cost_ptr (speed);
333 }
334
335 /* Subroutine of {set_,}add_cost. Not to be used otherwise. */
336
337 static inline int *
338 add_cost_ptr (bool speed, enum machine_mode mode)
339 {
340 return expmed_op_cost_ptr (&this_target_expmed->x_add_cost, speed, mode);
341 }
342
343 /* Set the COST of computing an add in MODE when optimizing for SPEED. */
344
345 static inline void
346 set_add_cost (bool speed, enum machine_mode mode, int cost)
347 {
348 *add_cost_ptr (speed, mode) = cost;
349 }
350
351 /* Return the cost of computing an add in MODE when optimizing for SPEED. */
352
353 static inline int
354 add_cost (bool speed, enum machine_mode mode)
355 {
356 return *add_cost_ptr (speed, mode);
357 }
358
359 /* Subroutine of {set_,}neg_cost. Not to be used otherwise. */
360
361 static inline int *
362 neg_cost_ptr (bool speed, enum machine_mode mode)
363 {
364 return expmed_op_cost_ptr (&this_target_expmed->x_neg_cost, speed, mode);
365 }
366
367 /* Set the COST of computing a negation in MODE when optimizing for SPEED. */
368
369 static inline void
370 set_neg_cost (bool speed, enum machine_mode mode, int cost)
371 {
372 *neg_cost_ptr (speed, mode) = cost;
373 }
374
375 /* Return the cost of computing a negation in MODE when optimizing for
376 SPEED. */
377
378 static inline int
379 neg_cost (bool speed, enum machine_mode mode)
380 {
381 return *neg_cost_ptr (speed, mode);
382 }
383
384 /* Subroutine of {set_,}shift_cost. Not to be used otherwise. */
385
386 static inline int *
387 shift_cost_ptr (bool speed, enum machine_mode mode, int bits)
388 {
389 return expmed_op_cost_ptr (&this_target_expmed->x_shift_cost[bits],
390 speed, mode);
391 }
392
393 /* Set the COST of doing a shift in MODE by BITS when optimizing for SPEED. */
394
395 static inline void
396 set_shift_cost (bool speed, enum machine_mode mode, int bits, int cost)
397 {
398 *shift_cost_ptr (speed, mode, bits) = cost;
399 }
400
401 /* Return the cost of doing a shift in MODE by BITS when optimizing for
402 SPEED. */
403
404 static inline int
405 shift_cost (bool speed, enum machine_mode mode, int bits)
406 {
407 return *shift_cost_ptr (speed, mode, bits);
408 }
409
410 /* Subroutine of {set_,}shiftadd_cost. Not to be used otherwise. */
411
412 static inline int *
413 shiftadd_cost_ptr (bool speed, enum machine_mode mode, int bits)
414 {
415 return expmed_op_cost_ptr (&this_target_expmed->x_shiftadd_cost[bits],
416 speed, mode);
417 }
418
419 /* Set the COST of doing a shift in MODE by BITS followed by an add when
420 optimizing for SPEED. */
421
422 static inline void
423 set_shiftadd_cost (bool speed, enum machine_mode mode, int bits, int cost)
424 {
425 *shiftadd_cost_ptr (speed, mode, bits) = cost;
426 }
427
428 /* Return the cost of doing a shift in MODE by BITS followed by an add
429 when optimizing for SPEED. */
430
431 static inline int
432 shiftadd_cost (bool speed, enum machine_mode mode, int bits)
433 {
434 return *shiftadd_cost_ptr (speed, mode, bits);
435 }
436
437 /* Subroutine of {set_,}shiftsub0_cost. Not to be used otherwise. */
438
439 static inline int *
440 shiftsub0_cost_ptr (bool speed, enum machine_mode mode, int bits)
441 {
442 return expmed_op_cost_ptr (&this_target_expmed->x_shiftsub0_cost[bits],
443 speed, mode);
444 }
445
446 /* Set the COST of doing a shift in MODE by BITS and then subtracting a
447 value when optimizing for SPEED. */
448
449 static inline void
450 set_shiftsub0_cost (bool speed, enum machine_mode mode, int bits, int cost)
451 {
452 *shiftsub0_cost_ptr (speed, mode, bits) = cost;
453 }
454
455 /* Return the cost of doing a shift in MODE by BITS and then subtracting
456 a value when optimizing for SPEED. */
457
458 static inline int
459 shiftsub0_cost (bool speed, enum machine_mode mode, int bits)
460 {
461 return *shiftsub0_cost_ptr (speed, mode, bits);
462 }
463
464 /* Subroutine of {set_,}shiftsub1_cost. Not to be used otherwise. */
465
466 static inline int *
467 shiftsub1_cost_ptr (bool speed, enum machine_mode mode, int bits)
468 {
469 return expmed_op_cost_ptr (&this_target_expmed->x_shiftsub1_cost[bits],
470 speed, mode);
471 }
472
473 /* Set the COST of subtracting a shift in MODE by BITS from a value when
474 optimizing for SPEED. */
475
476 static inline void
477 set_shiftsub1_cost (bool speed, enum machine_mode mode, int bits, int cost)
478 {
479 *shiftsub1_cost_ptr (speed, mode, bits) = cost;
480 }
481
482 /* Return the cost of subtracting a shift in MODE by BITS from a value
483 when optimizing for SPEED. */
484
485 static inline int
486 shiftsub1_cost (bool speed, enum machine_mode mode, int bits)
487 {
488 return *shiftsub1_cost_ptr (speed, mode, bits);
489 }
490
491 /* Subroutine of {set_,}mul_cost. Not to be used otherwise. */
492
493 static inline int *
494 mul_cost_ptr (bool speed, enum machine_mode mode)
495 {
496 return expmed_op_cost_ptr (&this_target_expmed->x_mul_cost, speed, mode);
497 }
498
499 /* Set the COST of doing a multiplication in MODE when optimizing for
500 SPEED. */
501
502 static inline void
503 set_mul_cost (bool speed, enum machine_mode mode, int cost)
504 {
505 *mul_cost_ptr (speed, mode) = cost;
506 }
507
508 /* Return the cost of doing a multiplication in MODE when optimizing
509 for SPEED. */
510
511 static inline int
512 mul_cost (bool speed, enum machine_mode mode)
513 {
514 return *mul_cost_ptr (speed, mode);
515 }
516
517 /* Subroutine of {set_,}sdiv_cost. Not to be used otherwise. */
518
519 static inline int *
520 sdiv_cost_ptr (bool speed, enum machine_mode mode)
521 {
522 return expmed_op_cost_ptr (&this_target_expmed->x_sdiv_cost, speed, mode);
523 }
524
525 /* Set the COST of doing a signed division in MODE when optimizing
526 for SPEED. */
527
528 static inline void
529 set_sdiv_cost (bool speed, enum machine_mode mode, int cost)
530 {
531 *sdiv_cost_ptr (speed, mode) = cost;
532 }
533
534 /* Return the cost of doing a signed division in MODE when optimizing
535 for SPEED. */
536
537 static inline int
538 sdiv_cost (bool speed, enum machine_mode mode)
539 {
540 return *sdiv_cost_ptr (speed, mode);
541 }
542
543 /* Subroutine of {set_,}udiv_cost. Not to be used otherwise. */
544
545 static inline int *
546 udiv_cost_ptr (bool speed, enum machine_mode mode)
547 {
548 return expmed_op_cost_ptr (&this_target_expmed->x_udiv_cost, speed, mode);
549 }
550
551 /* Set the COST of doing an unsigned division in MODE when optimizing
552 for SPEED. */
553
554 static inline void
555 set_udiv_cost (bool speed, enum machine_mode mode, int cost)
556 {
557 *udiv_cost_ptr (speed, mode) = cost;
558 }
559
560 /* Return the cost of doing an unsigned division in MODE when
561 optimizing for SPEED. */
562
563 static inline int
564 udiv_cost (bool speed, enum machine_mode mode)
565 {
566 return *udiv_cost_ptr (speed, mode);
567 }
568
569 /* Subroutine of {set_,}mul_widen_cost. Not to be used otherwise. */
570
571 static inline int *
572 mul_widen_cost_ptr (bool speed, enum machine_mode mode)
573 {
574 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
575
576 return &this_target_expmed->x_mul_widen_cost[speed][mode - MIN_MODE_INT];
577 }
578
579 /* Set the COST for computing a widening multiplication in MODE when
580 optimizing for SPEED. */
581
582 static inline void
583 set_mul_widen_cost (bool speed, enum machine_mode mode, int cost)
584 {
585 *mul_widen_cost_ptr (speed, mode) = cost;
586 }
587
588 /* Return the cost for computing a widening multiplication in MODE when
589 optimizing for SPEED. */
590
591 static inline int
592 mul_widen_cost (bool speed, enum machine_mode mode)
593 {
594 return *mul_widen_cost_ptr (speed, mode);
595 }
596
597 /* Subroutine of {set_,}mul_highpart_cost. Not to be used otherwise. */
598
599 static inline int *
600 mul_highpart_cost_ptr (bool speed, enum machine_mode mode)
601 {
602 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
603
604 return &this_target_expmed->x_mul_highpart_cost[speed][mode - MIN_MODE_INT];
605 }
606
607 /* Set the COST for computing the high part of a multiplication in MODE
608 when optimizing for SPEED. */
609
610 static inline void
611 set_mul_highpart_cost (bool speed, enum machine_mode mode, int cost)
612 {
613 *mul_highpart_cost_ptr (speed, mode) = cost;
614 }
615
616 /* Return the cost for computing the high part of a multiplication in MODE
617 when optimizing for SPEED. */
618
619 static inline int
620 mul_highpart_cost (bool speed, enum machine_mode mode)
621 {
622 return *mul_highpart_cost_ptr (speed, mode);
623 }
624
625 /* Subroutine of {set_,}convert_cost. Not to be used otherwise. */
626
627 static inline int *
628 convert_cost_ptr (enum machine_mode to_mode, enum machine_mode from_mode,
629 bool speed)
630 {
631 int to_idx = expmed_mode_index (to_mode);
632 int from_idx = expmed_mode_index (from_mode);
633
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));
636
637 return &this_target_expmed->x_convert_cost[speed][to_idx][from_idx];
638 }
639
640 /* Set the COST for converting from FROM_MODE to TO_MODE when optimizing
641 for SPEED. */
642
643 static inline void
644 set_convert_cost (enum machine_mode to_mode, enum machine_mode from_mode,
645 bool speed, int cost)
646 {
647 *convert_cost_ptr (to_mode, from_mode, speed) = cost;
648 }
649
650 /* Return the cost for converting from FROM_MODE to TO_MODE when optimizing
651 for SPEED. */
652
653 static inline int
654 convert_cost (enum machine_mode to_mode, enum machine_mode from_mode,
655 bool speed)
656 {
657 return *convert_cost_ptr (to_mode, from_mode, speed);
658 }
659
660 extern int mult_by_coeff_cost (HOST_WIDE_INT, enum machine_mode, bool);
661 #endif