xref: /dflybsd-src/contrib/gcc-4.7/gcc/expmed.h (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* Target-dependent costs for expmed.c.
2*e4b17023SJohn Marino    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3*e4b17023SJohn Marino    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4*e4b17023SJohn Marino    Free Software Foundation, Inc.
5*e4b17023SJohn Marino 
6*e4b17023SJohn Marino This file is part of GCC.
7*e4b17023SJohn Marino 
8*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
9*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
10*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option; any later
11*e4b17023SJohn Marino version.
12*e4b17023SJohn Marino 
13*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16*e4b17023SJohn Marino for more details.
17*e4b17023SJohn Marino 
18*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
19*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
20*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
21*e4b17023SJohn Marino 
22*e4b17023SJohn Marino #ifndef EXPMED_H
23*e4b17023SJohn Marino #define EXPMED_H 1
24*e4b17023SJohn Marino 
25*e4b17023SJohn Marino enum alg_code {
26*e4b17023SJohn Marino   alg_unknown,
27*e4b17023SJohn Marino   alg_zero,
28*e4b17023SJohn Marino   alg_m, alg_shift,
29*e4b17023SJohn Marino   alg_add_t_m2,
30*e4b17023SJohn Marino   alg_sub_t_m2,
31*e4b17023SJohn Marino   alg_add_factor,
32*e4b17023SJohn Marino   alg_sub_factor,
33*e4b17023SJohn Marino   alg_add_t2_m,
34*e4b17023SJohn Marino   alg_sub_t2_m,
35*e4b17023SJohn Marino   alg_impossible
36*e4b17023SJohn Marino };
37*e4b17023SJohn Marino 
38*e4b17023SJohn Marino /* This structure holds the "cost" of a multiply sequence.  The
39*e4b17023SJohn Marino    "cost" field holds the total rtx_cost of every operator in the
40*e4b17023SJohn Marino    synthetic multiplication sequence, hence cost(a op b) is defined
41*e4b17023SJohn Marino    as rtx_cost(op) + cost(a) + cost(b), where cost(leaf) is zero.
42*e4b17023SJohn Marino    The "latency" field holds the minimum possible latency of the
43*e4b17023SJohn Marino    synthetic multiply, on a hypothetical infinitely parallel CPU.
44*e4b17023SJohn Marino    This is the critical path, or the maximum height, of the expression
45*e4b17023SJohn Marino    tree which is the sum of rtx_costs on the most expensive path from
46*e4b17023SJohn Marino    any leaf to the root.  Hence latency(a op b) is defined as zero for
47*e4b17023SJohn Marino    leaves and rtx_cost(op) + max(latency(a), latency(b)) otherwise.  */
48*e4b17023SJohn Marino 
49*e4b17023SJohn Marino struct mult_cost {
50*e4b17023SJohn Marino   short cost;     /* Total rtx_cost of the multiplication sequence.  */
51*e4b17023SJohn Marino   short latency;  /* The latency of the multiplication sequence.  */
52*e4b17023SJohn Marino };
53*e4b17023SJohn Marino 
54*e4b17023SJohn Marino /* This macro is used to compare a pointer to a mult_cost against an
55*e4b17023SJohn Marino    single integer "rtx_cost" value.  This is equivalent to the macro
56*e4b17023SJohn Marino    CHEAPER_MULT_COST(X,Z) where Z = {Y,Y}.  */
57*e4b17023SJohn Marino #define MULT_COST_LESS(X,Y) ((X)->cost < (Y)	\
58*e4b17023SJohn Marino 			     || ((X)->cost == (Y) && (X)->latency < (Y)))
59*e4b17023SJohn Marino 
60*e4b17023SJohn Marino /* This macro is used to compare two pointers to mult_costs against
61*e4b17023SJohn Marino    each other.  The macro returns true if X is cheaper than Y.
62*e4b17023SJohn Marino    Currently, the cheaper of two mult_costs is the one with the
63*e4b17023SJohn Marino    lower "cost".  If "cost"s are tied, the lower latency is cheaper.  */
64*e4b17023SJohn Marino #define CHEAPER_MULT_COST(X,Y)  ((X)->cost < (Y)->cost		\
65*e4b17023SJohn Marino 				 || ((X)->cost == (Y)->cost	\
66*e4b17023SJohn Marino 				     && (X)->latency < (Y)->latency))
67*e4b17023SJohn Marino 
68*e4b17023SJohn Marino /* This structure records a sequence of operations.
69*e4b17023SJohn Marino    `ops' is the number of operations recorded.
70*e4b17023SJohn Marino    `cost' is their total cost.
71*e4b17023SJohn Marino    The operations are stored in `op' and the corresponding
72*e4b17023SJohn Marino    logarithms of the integer coefficients in `log'.
73*e4b17023SJohn Marino 
74*e4b17023SJohn Marino    These are the operations:
75*e4b17023SJohn Marino    alg_zero		total := 0;
76*e4b17023SJohn Marino    alg_m		total := multiplicand;
77*e4b17023SJohn Marino    alg_shift		total := total * coeff
78*e4b17023SJohn Marino    alg_add_t_m2		total := total + multiplicand * coeff;
79*e4b17023SJohn Marino    alg_sub_t_m2		total := total - multiplicand * coeff;
80*e4b17023SJohn Marino    alg_add_factor	total := total * coeff + total;
81*e4b17023SJohn Marino    alg_sub_factor	total := total * coeff - total;
82*e4b17023SJohn Marino    alg_add_t2_m		total := total * coeff + multiplicand;
83*e4b17023SJohn Marino    alg_sub_t2_m		total := total * coeff - multiplicand;
84*e4b17023SJohn Marino 
85*e4b17023SJohn Marino    The first operand must be either alg_zero or alg_m.  */
86*e4b17023SJohn Marino 
87*e4b17023SJohn Marino struct algorithm
88*e4b17023SJohn Marino {
89*e4b17023SJohn Marino   struct mult_cost cost;
90*e4b17023SJohn Marino   short ops;
91*e4b17023SJohn Marino   /* The size of the OP and LOG fields are not directly related to the
92*e4b17023SJohn Marino      word size, but the worst-case algorithms will be if we have few
93*e4b17023SJohn Marino      consecutive ones or zeros, i.e., a multiplicand like 10101010101...
94*e4b17023SJohn Marino      In that case we will generate shift-by-2, add, shift-by-2, add,...,
95*e4b17023SJohn Marino      in total wordsize operations.  */
96*e4b17023SJohn Marino   enum alg_code op[MAX_BITS_PER_WORD];
97*e4b17023SJohn Marino   char log[MAX_BITS_PER_WORD];
98*e4b17023SJohn Marino };
99*e4b17023SJohn Marino 
100*e4b17023SJohn Marino /* The entry for our multiplication cache/hash table.  */
101*e4b17023SJohn Marino struct alg_hash_entry {
102*e4b17023SJohn Marino   /* The number we are multiplying by.  */
103*e4b17023SJohn Marino   unsigned HOST_WIDE_INT t;
104*e4b17023SJohn Marino 
105*e4b17023SJohn Marino   /* The mode in which we are multiplying something by T.  */
106*e4b17023SJohn Marino   enum machine_mode mode;
107*e4b17023SJohn Marino 
108*e4b17023SJohn Marino   /* The best multiplication algorithm for t.  */
109*e4b17023SJohn Marino   enum alg_code alg;
110*e4b17023SJohn Marino 
111*e4b17023SJohn Marino   /* The cost of multiplication if ALG_CODE is not alg_impossible.
112*e4b17023SJohn Marino      Otherwise, the cost within which multiplication by T is
113*e4b17023SJohn Marino      impossible.  */
114*e4b17023SJohn Marino   struct mult_cost cost;
115*e4b17023SJohn Marino 
116*e4b17023SJohn Marino   /* Optimized for speed? */
117*e4b17023SJohn Marino   bool speed;
118*e4b17023SJohn Marino };
119*e4b17023SJohn Marino 
120*e4b17023SJohn Marino /* The number of cache/hash entries.  */
121*e4b17023SJohn Marino #if HOST_BITS_PER_WIDE_INT == 64
122*e4b17023SJohn Marino #define NUM_ALG_HASH_ENTRIES 1031
123*e4b17023SJohn Marino #else
124*e4b17023SJohn Marino #define NUM_ALG_HASH_ENTRIES 307
125*e4b17023SJohn Marino #endif
126*e4b17023SJohn Marino 
127*e4b17023SJohn Marino /* Target-dependent globals.  */
128*e4b17023SJohn Marino struct target_expmed {
129*e4b17023SJohn Marino   /* Each entry of ALG_HASH caches alg_code for some integer.  This is
130*e4b17023SJohn Marino      actually a hash table.  If we have a collision, that the older
131*e4b17023SJohn Marino      entry is kicked out.  */
132*e4b17023SJohn Marino   struct alg_hash_entry x_alg_hash[NUM_ALG_HASH_ENTRIES];
133*e4b17023SJohn Marino 
134*e4b17023SJohn Marino   /* True if x_alg_hash might already have been used.  */
135*e4b17023SJohn Marino   bool x_alg_hash_used_p;
136*e4b17023SJohn Marino 
137*e4b17023SJohn Marino   /* Nonzero means divides or modulus operations are relatively cheap for
138*e4b17023SJohn Marino      powers of two, so don't use branches; emit the operation instead.
139*e4b17023SJohn Marino      Usually, this will mean that the MD file will emit non-branch
140*e4b17023SJohn Marino      sequences.  */
141*e4b17023SJohn Marino   bool x_sdiv_pow2_cheap[2][NUM_MACHINE_MODES];
142*e4b17023SJohn Marino   bool x_smod_pow2_cheap[2][NUM_MACHINE_MODES];
143*e4b17023SJohn Marino 
144*e4b17023SJohn Marino   /* Cost of various pieces of RTL.  Note that some of these are indexed by
145*e4b17023SJohn Marino      shift count and some by mode.  */
146*e4b17023SJohn Marino   int x_zero_cost[2];
147*e4b17023SJohn Marino   int x_add_cost[2][NUM_MACHINE_MODES];
148*e4b17023SJohn Marino   int x_neg_cost[2][NUM_MACHINE_MODES];
149*e4b17023SJohn Marino   int x_shift_cost[2][NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
150*e4b17023SJohn Marino   int x_shiftadd_cost[2][NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
151*e4b17023SJohn Marino   int x_shiftsub0_cost[2][NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
152*e4b17023SJohn Marino   int x_shiftsub1_cost[2][NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
153*e4b17023SJohn Marino   int x_mul_cost[2][NUM_MACHINE_MODES];
154*e4b17023SJohn Marino   int x_sdiv_cost[2][NUM_MACHINE_MODES];
155*e4b17023SJohn Marino   int x_udiv_cost[2][NUM_MACHINE_MODES];
156*e4b17023SJohn Marino   int x_mul_widen_cost[2][NUM_MACHINE_MODES];
157*e4b17023SJohn Marino   int x_mul_highpart_cost[2][NUM_MACHINE_MODES];
158*e4b17023SJohn Marino };
159*e4b17023SJohn Marino 
160*e4b17023SJohn Marino extern struct target_expmed default_target_expmed;
161*e4b17023SJohn Marino #if SWITCHABLE_TARGET
162*e4b17023SJohn Marino extern struct target_expmed *this_target_expmed;
163*e4b17023SJohn Marino #else
164*e4b17023SJohn Marino #define this_target_expmed (&default_target_expmed)
165*e4b17023SJohn Marino #endif
166*e4b17023SJohn Marino 
167*e4b17023SJohn Marino #define alg_hash \
168*e4b17023SJohn Marino   (this_target_expmed->x_alg_hash)
169*e4b17023SJohn Marino #define alg_hash_used_p \
170*e4b17023SJohn Marino   (this_target_expmed->x_alg_hash_used_p)
171*e4b17023SJohn Marino #define sdiv_pow2_cheap \
172*e4b17023SJohn Marino   (this_target_expmed->x_sdiv_pow2_cheap)
173*e4b17023SJohn Marino #define smod_pow2_cheap \
174*e4b17023SJohn Marino   (this_target_expmed->x_smod_pow2_cheap)
175*e4b17023SJohn Marino #define zero_cost \
176*e4b17023SJohn Marino   (this_target_expmed->x_zero_cost)
177*e4b17023SJohn Marino #define add_cost \
178*e4b17023SJohn Marino   (this_target_expmed->x_add_cost)
179*e4b17023SJohn Marino #define neg_cost \
180*e4b17023SJohn Marino   (this_target_expmed->x_neg_cost)
181*e4b17023SJohn Marino #define shift_cost \
182*e4b17023SJohn Marino   (this_target_expmed->x_shift_cost)
183*e4b17023SJohn Marino #define shiftadd_cost \
184*e4b17023SJohn Marino   (this_target_expmed->x_shiftadd_cost)
185*e4b17023SJohn Marino #define shiftsub0_cost \
186*e4b17023SJohn Marino   (this_target_expmed->x_shiftsub0_cost)
187*e4b17023SJohn Marino #define shiftsub1_cost \
188*e4b17023SJohn Marino   (this_target_expmed->x_shiftsub1_cost)
189*e4b17023SJohn Marino #define mul_cost \
190*e4b17023SJohn Marino   (this_target_expmed->x_mul_cost)
191*e4b17023SJohn Marino #define sdiv_cost \
192*e4b17023SJohn Marino   (this_target_expmed->x_sdiv_cost)
193*e4b17023SJohn Marino #define udiv_cost \
194*e4b17023SJohn Marino   (this_target_expmed->x_udiv_cost)
195*e4b17023SJohn Marino #define mul_widen_cost \
196*e4b17023SJohn Marino   (this_target_expmed->x_mul_widen_cost)
197*e4b17023SJohn Marino #define mul_highpart_cost \
198*e4b17023SJohn Marino   (this_target_expmed->x_mul_highpart_cost)
199*e4b17023SJohn Marino 
200*e4b17023SJohn Marino #endif
201