]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/expmed.h
[multiple changes]
[thirdparty/gcc.git] / gcc / expmed.h
CommitLineData
462f85ce 1/* Target-dependent costs for expmed.c.
23a5b65a 2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
462f85ce
RS
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option; any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along 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
c371bb73
RS
23enum 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
47struct 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
85struct 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. */
99struct 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
91f8035e
RH
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)
2a261cd3
NF
136
137struct expmed_op_cheap {
91f8035e 138 bool cheap[2][NUM_MODE_IPV_INT];
2a261cd3
NF
139};
140
141struct expmed_op_costs {
91f8035e 142 int cost[2][NUM_MODE_IPV_INT];
2a261cd3 143};
6dd8f4bb 144
462f85ce
RS
145/* Target-dependent globals. */
146struct target_expmed {
c371bb73
RS
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
462f85ce
RS
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. */
2a261cd3
NF
159 struct expmed_op_cheap x_sdiv_pow2_cheap;
160 struct expmed_op_cheap x_smod_pow2_cheap;
462f85ce
RS
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];
2a261cd3
NF
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];
6dd8f4bb
BS
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. */
91f8035e 180 int x_convert_cost[2][NUM_MODE_IP_INT][NUM_MODE_IP_INT];
462f85ce
RS
181};
182
183extern struct target_expmed default_target_expmed;
184#if SWITCHABLE_TARGET
185extern struct target_expmed *this_target_expmed;
186#else
187#define this_target_expmed (&default_target_expmed)
188#endif
189
5322d07e 190/* Return a pointer to the alg_hash_entry at IDX. */
462f85ce 191
5322d07e
NF
192static inline struct alg_hash_entry *
193alg_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
200static inline bool
201alg_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
208static inline void
209set_alg_hash_used_p (bool usedp)
210{
211 this_target_expmed->x_alg_hash_used_p = usedp;
212}
213
91f8035e
RH
214/* Compute an index into the cost arrays by mode class. */
215
216static inline int
217expmed_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
2a261cd3
NF
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
236static inline bool *
237expmed_op_cheap_ptr (struct expmed_op_cheap *eoc, bool speed,
238 enum machine_mode mode)
239{
91f8035e
RH
240 int idx = expmed_mode_index (mode);
241 return &eoc->cheap[speed][idx];
2a261cd3
NF
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
247static inline int *
248expmed_op_cost_ptr (struct expmed_op_costs *costs, bool speed,
249 enum machine_mode mode)
250{
91f8035e
RH
251 int idx = expmed_mode_index (mode);
252 return &costs->cost[speed][idx];
2a261cd3
NF
253}
254
5322d07e
NF
255/* Subroutine of {set_,}sdiv_pow2_cheap. Not to be used otherwise. */
256
257static inline bool *
258sdiv_pow2_cheap_ptr (bool speed, enum machine_mode mode)
259{
2a261cd3
NF
260 return expmed_op_cheap_ptr (&this_target_expmed->x_sdiv_pow2_cheap,
261 speed, mode);
5322d07e
NF
262}
263
264/* Set whether a signed division by a power of 2 is cheap in MODE
265 when optimizing for SPEED. */
266
267static inline void
268set_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
276static inline bool
277sdiv_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
284static inline bool *
285smod_pow2_cheap_ptr (bool speed, enum machine_mode mode)
286{
2a261cd3
NF
287 return expmed_op_cheap_ptr (&this_target_expmed->x_smod_pow2_cheap,
288 speed, mode);
5322d07e
NF
289}
290
291/* Set whether a signed modulo by a power of 2 is CHEAP in MODE when
292 optimizing for SPEED. */
293
294static inline void
295set_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
303static inline bool
304smod_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
311static inline int *
312zero_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
319static inline void
320set_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
327static inline int
328zero_cost (bool speed)
329{
330 return *zero_cost_ptr (speed);
331}
332
333/* Subroutine of {set_,}add_cost. Not to be used otherwise. */
334
335static inline int *
336add_cost_ptr (bool speed, enum machine_mode mode)
337{
2a261cd3 338 return expmed_op_cost_ptr (&this_target_expmed->x_add_cost, speed, mode);
5322d07e
NF
339}
340
341/* Set the COST of computing an add in MODE when optimizing for SPEED. */
342
343static inline void
344set_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
351static inline int
352add_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
359static inline int *
360neg_cost_ptr (bool speed, enum machine_mode mode)
361{
2a261cd3 362 return expmed_op_cost_ptr (&this_target_expmed->x_neg_cost, speed, mode);
5322d07e
NF
363}
364
365/* Set the COST of computing a negation in MODE when optimizing for SPEED. */
366
367static inline void
368set_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
376static inline int
377neg_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
384static inline int *
385shift_cost_ptr (bool speed, enum machine_mode mode, int bits)
386{
2a261cd3
NF
387 return expmed_op_cost_ptr (&this_target_expmed->x_shift_cost[bits],
388 speed, mode);
5322d07e
NF
389}
390
391/* Set the COST of doing a shift in MODE by BITS when optimizing for SPEED. */
392
393static inline void
394set_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
402static inline int
403shift_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
410static inline int *
411shiftadd_cost_ptr (bool speed, enum machine_mode mode, int bits)
412{
2a261cd3
NF
413 return expmed_op_cost_ptr (&this_target_expmed->x_shiftadd_cost[bits],
414 speed, mode);
5322d07e
NF
415}
416
417/* Set the COST of doing a shift in MODE by BITS followed by an add when
418 optimizing for SPEED. */
419
420static inline void
421set_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
429static inline int
430shiftadd_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
437static inline int *
438shiftsub0_cost_ptr (bool speed, enum machine_mode mode, int bits)
439{
2a261cd3
NF
440 return expmed_op_cost_ptr (&this_target_expmed->x_shiftsub0_cost[bits],
441 speed, mode);
5322d07e
NF
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
447static inline void
448set_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
456static inline int
457shiftsub0_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
464static inline int *
465shiftsub1_cost_ptr (bool speed, enum machine_mode mode, int bits)
466{
2a261cd3
NF
467 return expmed_op_cost_ptr (&this_target_expmed->x_shiftsub1_cost[bits],
468 speed, mode);
5322d07e
NF
469}
470
471/* Set the COST of subtracting a shift in MODE by BITS from a value when
472 optimizing for SPEED. */
473
474static inline void
475set_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
483static inline int
484shiftsub1_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
491static inline int *
492mul_cost_ptr (bool speed, enum machine_mode mode)
493{
2a261cd3 494 return expmed_op_cost_ptr (&this_target_expmed->x_mul_cost, speed, mode);
5322d07e
NF
495}
496
497/* Set the COST of doing a multiplication in MODE when optimizing for
498 SPEED. */
499
500static inline void
501set_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
509static inline int
510mul_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
517static inline int *
518sdiv_cost_ptr (bool speed, enum machine_mode mode)
519{
2a261cd3 520 return expmed_op_cost_ptr (&this_target_expmed->x_sdiv_cost, speed, mode);
5322d07e
NF
521}
522
523/* Set the COST of doing a signed division in MODE when optimizing
6dd8f4bb
BS
524 for SPEED. */
525
526static inline void
5322d07e
NF
527set_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
535static inline int
536sdiv_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
543static inline int *
544udiv_cost_ptr (bool speed, enum machine_mode mode)
545{
2a261cd3 546 return expmed_op_cost_ptr (&this_target_expmed->x_udiv_cost, speed, mode);
5322d07e
NF
547}
548
549/* Set the COST of doing an unsigned division in MODE when optimizing
550 for SPEED. */
551
552static inline void
553set_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
561static inline int
562udiv_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
569static inline int *
570mul_widen_cost_ptr (bool speed, enum machine_mode mode)
571{
2a261cd3
NF
572 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
573
574 return &this_target_expmed->x_mul_widen_cost[speed][mode - MIN_MODE_INT];
5322d07e
NF
575}
576
577/* Set the COST for computing a widening multiplication in MODE when
578 optimizing for SPEED. */
579
580static inline void
581set_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
589static inline int
590mul_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
597static inline int *
598mul_highpart_cost_ptr (bool speed, enum machine_mode mode)
599{
2a261cd3
NF
600 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
601
602 return &this_target_expmed->x_mul_highpart_cost[speed][mode - MIN_MODE_INT];
5322d07e
NF
603}
604
605/* Set the COST for computing the high part of a multiplication in MODE
606 when optimizing for SPEED. */
607
608static inline void
609set_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
617static inline int
618mul_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
625static inline int *
626convert_cost_ptr (enum machine_mode to_mode, enum machine_mode from_mode,
627 bool speed)
6dd8f4bb 628{
91f8035e
RH
629 int to_idx = expmed_mode_index (to_mode);
630 int from_idx = expmed_mode_index (from_mode);
6dd8f4bb 631
91f8035e
RH
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));
6dd8f4bb 634
5322d07e
NF
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
641static inline void
642set_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;
6dd8f4bb
BS
646}
647
648/* Return the cost for converting from FROM_MODE to TO_MODE when optimizing
649 for SPEED. */
650
651static inline int
652convert_cost (enum machine_mode to_mode, enum machine_mode from_mode,
653 bool speed)
654{
5322d07e 655 return *convert_cost_ptr (to_mode, from_mode, speed);
6dd8f4bb
BS
656}
657
658extern int mult_by_coeff_cost (HOST_WIDE_INT, enum machine_mode, bool);
462f85ce 659#endif