OSDN Git Service

* hooks.c (hook_bool_mode_rtx_true): New function.
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / i386.c
1 /* Subroutines used for code generation on IA-32.
2    Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-codes.h"
36 #include "insn-attr.h"
37 #include "flags.h"
38 #include "except.h"
39 #include "function.h"
40 #include "recog.h"
41 #include "expr.h"
42 #include "optabs.h"
43 #include "toplev.h"
44 #include "basic-block.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "target-def.h"
48 #include "langhooks.h"
49 #include "cgraph.h"
50 #include "tree-gimple.h"
51 #include "dwarf2.h"
52
53 #ifndef CHECK_STACK_LIMIT
54 #define CHECK_STACK_LIMIT (-1)
55 #endif
56
57 /* Return index of given mode in mult and division cost tables.  */
58 #define MODE_INDEX(mode)                                        \
59   ((mode) == QImode ? 0                                         \
60    : (mode) == HImode ? 1                                       \
61    : (mode) == SImode ? 2                                       \
62    : (mode) == DImode ? 3                                       \
63    : 4)
64
65 /* Processor costs (relative to an add) */
66 /* We assume COSTS_N_INSNS is defined as (N)*4 and an addition is 2 bytes.  */
67 #define COSTS_N_BYTES(N) ((N) * 2)
68
69 static const
70 struct processor_costs size_cost = {    /* costs for tunning for size */
71   COSTS_N_BYTES (2),                    /* cost of an add instruction */
72   COSTS_N_BYTES (3),                    /* cost of a lea instruction */
73   COSTS_N_BYTES (2),                    /* variable shift costs */
74   COSTS_N_BYTES (3),                    /* constant shift costs */
75   {COSTS_N_BYTES (3),                   /* cost of starting multiply for QI */
76    COSTS_N_BYTES (3),                   /*                               HI */
77    COSTS_N_BYTES (3),                   /*                               SI */
78    COSTS_N_BYTES (3),                   /*                               DI */
79    COSTS_N_BYTES (5)},                  /*                            other */
80   0,                                    /* cost of multiply per each bit set */
81   {COSTS_N_BYTES (3),                   /* cost of a divide/mod for QI */
82    COSTS_N_BYTES (3),                   /*                          HI */
83    COSTS_N_BYTES (3),                   /*                          SI */
84    COSTS_N_BYTES (3),                   /*                          DI */
85    COSTS_N_BYTES (5)},                  /*                       other */
86   COSTS_N_BYTES (3),                    /* cost of movsx */
87   COSTS_N_BYTES (3),                    /* cost of movzx */
88   0,                                    /* "large" insn */
89   2,                                    /* MOVE_RATIO */
90   2,                                    /* cost for loading QImode using movzbl */
91   {2, 2, 2},                            /* cost of loading integer registers
92                                            in QImode, HImode and SImode.
93                                            Relative to reg-reg move (2).  */
94   {2, 2, 2},                            /* cost of storing integer registers */
95   2,                                    /* cost of reg,reg fld/fst */
96   {2, 2, 2},                            /* cost of loading fp registers
97                                            in SFmode, DFmode and XFmode */
98   {2, 2, 2},                            /* cost of loading integer registers */
99   3,                                    /* cost of moving MMX register */
100   {3, 3},                               /* cost of loading MMX registers
101                                            in SImode and DImode */
102   {3, 3},                               /* cost of storing MMX registers
103                                            in SImode and DImode */
104   3,                                    /* cost of moving SSE register */
105   {3, 3, 3},                            /* cost of loading SSE registers
106                                            in SImode, DImode and TImode */
107   {3, 3, 3},                            /* cost of storing SSE registers
108                                            in SImode, DImode and TImode */
109   3,                                    /* MMX or SSE register to integer */
110   0,                                    /* size of prefetch block */
111   0,                                    /* number of parallel prefetches */
112   2,                                    /* Branch cost */
113   COSTS_N_BYTES (2),                    /* cost of FADD and FSUB insns.  */
114   COSTS_N_BYTES (2),                    /* cost of FMUL instruction.  */
115   COSTS_N_BYTES (2),                    /* cost of FDIV instruction.  */
116   COSTS_N_BYTES (2),                    /* cost of FABS instruction.  */
117   COSTS_N_BYTES (2),                    /* cost of FCHS instruction.  */
118   COSTS_N_BYTES (2),                    /* cost of FSQRT instruction.  */
119 };
120
121 /* Processor costs (relative to an add) */
122 static const
123 struct processor_costs i386_cost = {    /* 386 specific costs */
124   COSTS_N_INSNS (1),                    /* cost of an add instruction */
125   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
126   COSTS_N_INSNS (3),                    /* variable shift costs */
127   COSTS_N_INSNS (2),                    /* constant shift costs */
128   {COSTS_N_INSNS (6),                   /* cost of starting multiply for QI */
129    COSTS_N_INSNS (6),                   /*                               HI */
130    COSTS_N_INSNS (6),                   /*                               SI */
131    COSTS_N_INSNS (6),                   /*                               DI */
132    COSTS_N_INSNS (6)},                  /*                               other */
133   COSTS_N_INSNS (1),                    /* cost of multiply per each bit set */
134   {COSTS_N_INSNS (23),                  /* cost of a divide/mod for QI */
135    COSTS_N_INSNS (23),                  /*                          HI */
136    COSTS_N_INSNS (23),                  /*                          SI */
137    COSTS_N_INSNS (23),                  /*                          DI */
138    COSTS_N_INSNS (23)},                 /*                          other */
139   COSTS_N_INSNS (3),                    /* cost of movsx */
140   COSTS_N_INSNS (2),                    /* cost of movzx */
141   15,                                   /* "large" insn */
142   3,                                    /* MOVE_RATIO */
143   4,                                    /* cost for loading QImode using movzbl */
144   {2, 4, 2},                            /* cost of loading integer registers
145                                            in QImode, HImode and SImode.
146                                            Relative to reg-reg move (2).  */
147   {2, 4, 2},                            /* cost of storing integer registers */
148   2,                                    /* cost of reg,reg fld/fst */
149   {8, 8, 8},                            /* cost of loading fp registers
150                                            in SFmode, DFmode and XFmode */
151   {8, 8, 8},                            /* cost of loading integer registers */
152   2,                                    /* cost of moving MMX register */
153   {4, 8},                               /* cost of loading MMX registers
154                                            in SImode and DImode */
155   {4, 8},                               /* cost of storing MMX registers
156                                            in SImode and DImode */
157   2,                                    /* cost of moving SSE register */
158   {4, 8, 16},                           /* cost of loading SSE registers
159                                            in SImode, DImode and TImode */
160   {4, 8, 16},                           /* cost of storing SSE registers
161                                            in SImode, DImode and TImode */
162   3,                                    /* MMX or SSE register to integer */
163   0,                                    /* size of prefetch block */
164   0,                                    /* number of parallel prefetches */
165   1,                                    /* Branch cost */
166   COSTS_N_INSNS (23),                   /* cost of FADD and FSUB insns.  */
167   COSTS_N_INSNS (27),                   /* cost of FMUL instruction.  */
168   COSTS_N_INSNS (88),                   /* cost of FDIV instruction.  */
169   COSTS_N_INSNS (22),                   /* cost of FABS instruction.  */
170   COSTS_N_INSNS (24),                   /* cost of FCHS instruction.  */
171   COSTS_N_INSNS (122),                  /* cost of FSQRT instruction.  */
172 };
173
174 static const
175 struct processor_costs i486_cost = {    /* 486 specific costs */
176   COSTS_N_INSNS (1),                    /* cost of an add instruction */
177   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
178   COSTS_N_INSNS (3),                    /* variable shift costs */
179   COSTS_N_INSNS (2),                    /* constant shift costs */
180   {COSTS_N_INSNS (12),                  /* cost of starting multiply for QI */
181    COSTS_N_INSNS (12),                  /*                               HI */
182    COSTS_N_INSNS (12),                  /*                               SI */
183    COSTS_N_INSNS (12),                  /*                               DI */
184    COSTS_N_INSNS (12)},                 /*                               other */
185   1,                                    /* cost of multiply per each bit set */
186   {COSTS_N_INSNS (40),                  /* cost of a divide/mod for QI */
187    COSTS_N_INSNS (40),                  /*                          HI */
188    COSTS_N_INSNS (40),                  /*                          SI */
189    COSTS_N_INSNS (40),                  /*                          DI */
190    COSTS_N_INSNS (40)},                 /*                          other */
191   COSTS_N_INSNS (3),                    /* cost of movsx */
192   COSTS_N_INSNS (2),                    /* cost of movzx */
193   15,                                   /* "large" insn */
194   3,                                    /* MOVE_RATIO */
195   4,                                    /* cost for loading QImode using movzbl */
196   {2, 4, 2},                            /* cost of loading integer registers
197                                            in QImode, HImode and SImode.
198                                            Relative to reg-reg move (2).  */
199   {2, 4, 2},                            /* cost of storing integer registers */
200   2,                                    /* cost of reg,reg fld/fst */
201   {8, 8, 8},                            /* cost of loading fp registers
202                                            in SFmode, DFmode and XFmode */
203   {8, 8, 8},                            /* cost of loading integer registers */
204   2,                                    /* cost of moving MMX register */
205   {4, 8},                               /* cost of loading MMX registers
206                                            in SImode and DImode */
207   {4, 8},                               /* cost of storing MMX registers
208                                            in SImode and DImode */
209   2,                                    /* cost of moving SSE register */
210   {4, 8, 16},                           /* cost of loading SSE registers
211                                            in SImode, DImode and TImode */
212   {4, 8, 16},                           /* cost of storing SSE registers
213                                            in SImode, DImode and TImode */
214   3,                                    /* MMX or SSE register to integer */
215   0,                                    /* size of prefetch block */
216   0,                                    /* number of parallel prefetches */
217   1,                                    /* Branch cost */
218   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
219   COSTS_N_INSNS (16),                   /* cost of FMUL instruction.  */
220   COSTS_N_INSNS (73),                   /* cost of FDIV instruction.  */
221   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
222   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
223   COSTS_N_INSNS (83),                   /* cost of FSQRT instruction.  */
224 };
225
226 static const
227 struct processor_costs pentium_cost = {
228   COSTS_N_INSNS (1),                    /* cost of an add instruction */
229   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
230   COSTS_N_INSNS (4),                    /* variable shift costs */
231   COSTS_N_INSNS (1),                    /* constant shift costs */
232   {COSTS_N_INSNS (11),                  /* cost of starting multiply for QI */
233    COSTS_N_INSNS (11),                  /*                               HI */
234    COSTS_N_INSNS (11),                  /*                               SI */
235    COSTS_N_INSNS (11),                  /*                               DI */
236    COSTS_N_INSNS (11)},                 /*                               other */
237   0,                                    /* cost of multiply per each bit set */
238   {COSTS_N_INSNS (25),                  /* cost of a divide/mod for QI */
239    COSTS_N_INSNS (25),                  /*                          HI */
240    COSTS_N_INSNS (25),                  /*                          SI */
241    COSTS_N_INSNS (25),                  /*                          DI */
242    COSTS_N_INSNS (25)},                 /*                          other */
243   COSTS_N_INSNS (3),                    /* cost of movsx */
244   COSTS_N_INSNS (2),                    /* cost of movzx */
245   8,                                    /* "large" insn */
246   6,                                    /* MOVE_RATIO */
247   6,                                    /* cost for loading QImode using movzbl */
248   {2, 4, 2},                            /* cost of loading integer registers
249                                            in QImode, HImode and SImode.
250                                            Relative to reg-reg move (2).  */
251   {2, 4, 2},                            /* cost of storing integer registers */
252   2,                                    /* cost of reg,reg fld/fst */
253   {2, 2, 6},                            /* cost of loading fp registers
254                                            in SFmode, DFmode and XFmode */
255   {4, 4, 6},                            /* cost of loading integer registers */
256   8,                                    /* cost of moving MMX register */
257   {8, 8},                               /* cost of loading MMX registers
258                                            in SImode and DImode */
259   {8, 8},                               /* cost of storing MMX registers
260                                            in SImode and DImode */
261   2,                                    /* cost of moving SSE register */
262   {4, 8, 16},                           /* cost of loading SSE registers
263                                            in SImode, DImode and TImode */
264   {4, 8, 16},                           /* cost of storing SSE registers
265                                            in SImode, DImode and TImode */
266   3,                                    /* MMX or SSE register to integer */
267   0,                                    /* size of prefetch block */
268   0,                                    /* number of parallel prefetches */
269   2,                                    /* Branch cost */
270   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
271   COSTS_N_INSNS (3),                    /* cost of FMUL instruction.  */
272   COSTS_N_INSNS (39),                   /* cost of FDIV instruction.  */
273   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
274   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
275   COSTS_N_INSNS (70),                   /* cost of FSQRT instruction.  */
276 };
277
278 static const
279 struct processor_costs pentiumpro_cost = {
280   COSTS_N_INSNS (1),                    /* cost of an add instruction */
281   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
282   COSTS_N_INSNS (1),                    /* variable shift costs */
283   COSTS_N_INSNS (1),                    /* constant shift costs */
284   {COSTS_N_INSNS (4),                   /* cost of starting multiply for QI */
285    COSTS_N_INSNS (4),                   /*                               HI */
286    COSTS_N_INSNS (4),                   /*                               SI */
287    COSTS_N_INSNS (4),                   /*                               DI */
288    COSTS_N_INSNS (4)},                  /*                               other */
289   0,                                    /* cost of multiply per each bit set */
290   {COSTS_N_INSNS (17),                  /* cost of a divide/mod for QI */
291    COSTS_N_INSNS (17),                  /*                          HI */
292    COSTS_N_INSNS (17),                  /*                          SI */
293    COSTS_N_INSNS (17),                  /*                          DI */
294    COSTS_N_INSNS (17)},                 /*                          other */
295   COSTS_N_INSNS (1),                    /* cost of movsx */
296   COSTS_N_INSNS (1),                    /* cost of movzx */
297   8,                                    /* "large" insn */
298   6,                                    /* MOVE_RATIO */
299   2,                                    /* cost for loading QImode using movzbl */
300   {4, 4, 4},                            /* cost of loading integer registers
301                                            in QImode, HImode and SImode.
302                                            Relative to reg-reg move (2).  */
303   {2, 2, 2},                            /* cost of storing integer registers */
304   2,                                    /* cost of reg,reg fld/fst */
305   {2, 2, 6},                            /* cost of loading fp registers
306                                            in SFmode, DFmode and XFmode */
307   {4, 4, 6},                            /* cost of loading integer registers */
308   2,                                    /* cost of moving MMX register */
309   {2, 2},                               /* cost of loading MMX registers
310                                            in SImode and DImode */
311   {2, 2},                               /* cost of storing MMX registers
312                                            in SImode and DImode */
313   2,                                    /* cost of moving SSE register */
314   {2, 2, 8},                            /* cost of loading SSE registers
315                                            in SImode, DImode and TImode */
316   {2, 2, 8},                            /* cost of storing SSE registers
317                                            in SImode, DImode and TImode */
318   3,                                    /* MMX or SSE register to integer */
319   32,                                   /* size of prefetch block */
320   6,                                    /* number of parallel prefetches */
321   2,                                    /* Branch cost */
322   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
323   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
324   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
325   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
326   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
327   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
328 };
329
330 static const
331 struct processor_costs k6_cost = {
332   COSTS_N_INSNS (1),                    /* cost of an add instruction */
333   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
334   COSTS_N_INSNS (1),                    /* variable shift costs */
335   COSTS_N_INSNS (1),                    /* constant shift costs */
336   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
337    COSTS_N_INSNS (3),                   /*                               HI */
338    COSTS_N_INSNS (3),                   /*                               SI */
339    COSTS_N_INSNS (3),                   /*                               DI */
340    COSTS_N_INSNS (3)},                  /*                               other */
341   0,                                    /* cost of multiply per each bit set */
342   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
343    COSTS_N_INSNS (18),                  /*                          HI */
344    COSTS_N_INSNS (18),                  /*                          SI */
345    COSTS_N_INSNS (18),                  /*                          DI */
346    COSTS_N_INSNS (18)},                 /*                          other */
347   COSTS_N_INSNS (2),                    /* cost of movsx */
348   COSTS_N_INSNS (2),                    /* cost of movzx */
349   8,                                    /* "large" insn */
350   4,                                    /* MOVE_RATIO */
351   3,                                    /* cost for loading QImode using movzbl */
352   {4, 5, 4},                            /* cost of loading integer registers
353                                            in QImode, HImode and SImode.
354                                            Relative to reg-reg move (2).  */
355   {2, 3, 2},                            /* cost of storing integer registers */
356   4,                                    /* cost of reg,reg fld/fst */
357   {6, 6, 6},                            /* cost of loading fp registers
358                                            in SFmode, DFmode and XFmode */
359   {4, 4, 4},                            /* cost of loading integer registers */
360   2,                                    /* cost of moving MMX register */
361   {2, 2},                               /* cost of loading MMX registers
362                                            in SImode and DImode */
363   {2, 2},                               /* cost of storing MMX registers
364                                            in SImode and DImode */
365   2,                                    /* cost of moving SSE register */
366   {2, 2, 8},                            /* cost of loading SSE registers
367                                            in SImode, DImode and TImode */
368   {2, 2, 8},                            /* cost of storing SSE registers
369                                            in SImode, DImode and TImode */
370   6,                                    /* MMX or SSE register to integer */
371   32,                                   /* size of prefetch block */
372   1,                                    /* number of parallel prefetches */
373   1,                                    /* Branch cost */
374   COSTS_N_INSNS (2),                    /* cost of FADD and FSUB insns.  */
375   COSTS_N_INSNS (2),                    /* cost of FMUL instruction.  */
376   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
377   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
378   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
379   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
380 };
381
382 static const
383 struct processor_costs athlon_cost = {
384   COSTS_N_INSNS (1),                    /* cost of an add instruction */
385   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
386   COSTS_N_INSNS (1),                    /* variable shift costs */
387   COSTS_N_INSNS (1),                    /* constant shift costs */
388   {COSTS_N_INSNS (5),                   /* cost of starting multiply for QI */
389    COSTS_N_INSNS (5),                   /*                               HI */
390    COSTS_N_INSNS (5),                   /*                               SI */
391    COSTS_N_INSNS (5),                   /*                               DI */
392    COSTS_N_INSNS (5)},                  /*                               other */
393   0,                                    /* cost of multiply per each bit set */
394   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
395    COSTS_N_INSNS (26),                  /*                          HI */
396    COSTS_N_INSNS (42),                  /*                          SI */
397    COSTS_N_INSNS (74),                  /*                          DI */
398    COSTS_N_INSNS (74)},                 /*                          other */
399   COSTS_N_INSNS (1),                    /* cost of movsx */
400   COSTS_N_INSNS (1),                    /* cost of movzx */
401   8,                                    /* "large" insn */
402   9,                                    /* MOVE_RATIO */
403   4,                                    /* cost for loading QImode using movzbl */
404   {3, 4, 3},                            /* cost of loading integer registers
405                                            in QImode, HImode and SImode.
406                                            Relative to reg-reg move (2).  */
407   {3, 4, 3},                            /* cost of storing integer registers */
408   4,                                    /* cost of reg,reg fld/fst */
409   {4, 4, 12},                           /* cost of loading fp registers
410                                            in SFmode, DFmode and XFmode */
411   {6, 6, 8},                            /* cost of loading integer registers */
412   2,                                    /* cost of moving MMX register */
413   {4, 4},                               /* cost of loading MMX registers
414                                            in SImode and DImode */
415   {4, 4},                               /* cost of storing MMX registers
416                                            in SImode and DImode */
417   2,                                    /* cost of moving SSE register */
418   {4, 4, 6},                            /* cost of loading SSE registers
419                                            in SImode, DImode and TImode */
420   {4, 4, 5},                            /* cost of storing SSE registers
421                                            in SImode, DImode and TImode */
422   5,                                    /* MMX or SSE register to integer */
423   64,                                   /* size of prefetch block */
424   6,                                    /* number of parallel prefetches */
425   5,                                    /* Branch cost */
426   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
427   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
428   COSTS_N_INSNS (24),                   /* cost of FDIV instruction.  */
429   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
430   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
431   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
432 };
433
434 static const
435 struct processor_costs k8_cost = {
436   COSTS_N_INSNS (1),                    /* cost of an add instruction */
437   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
438   COSTS_N_INSNS (1),                    /* variable shift costs */
439   COSTS_N_INSNS (1),                    /* constant shift costs */
440   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
441    COSTS_N_INSNS (4),                   /*                               HI */
442    COSTS_N_INSNS (3),                   /*                               SI */
443    COSTS_N_INSNS (4),                   /*                               DI */
444    COSTS_N_INSNS (5)},                  /*                               other */
445   0,                                    /* cost of multiply per each bit set */
446   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
447    COSTS_N_INSNS (26),                  /*                          HI */
448    COSTS_N_INSNS (42),                  /*                          SI */
449    COSTS_N_INSNS (74),                  /*                          DI */
450    COSTS_N_INSNS (74)},                 /*                          other */
451   COSTS_N_INSNS (1),                    /* cost of movsx */
452   COSTS_N_INSNS (1),                    /* cost of movzx */
453   8,                                    /* "large" insn */
454   9,                                    /* MOVE_RATIO */
455   4,                                    /* cost for loading QImode using movzbl */
456   {3, 4, 3},                            /* cost of loading integer registers
457                                            in QImode, HImode and SImode.
458                                            Relative to reg-reg move (2).  */
459   {3, 4, 3},                            /* cost of storing integer registers */
460   4,                                    /* cost of reg,reg fld/fst */
461   {4, 4, 12},                           /* cost of loading fp registers
462                                            in SFmode, DFmode and XFmode */
463   {6, 6, 8},                            /* cost of loading integer registers */
464   2,                                    /* cost of moving MMX register */
465   {3, 3},                               /* cost of loading MMX registers
466                                            in SImode and DImode */
467   {4, 4},                               /* cost of storing MMX registers
468                                            in SImode and DImode */
469   2,                                    /* cost of moving SSE register */
470   {4, 3, 6},                            /* cost of loading SSE registers
471                                            in SImode, DImode and TImode */
472   {4, 4, 5},                            /* cost of storing SSE registers
473                                            in SImode, DImode and TImode */
474   5,                                    /* MMX or SSE register to integer */
475   64,                                   /* size of prefetch block */
476   6,                                    /* number of parallel prefetches */
477   5,                                    /* Branch cost */
478   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
479   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
480   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
481   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
482   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
483   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
484 };
485
486 static const
487 struct processor_costs pentium4_cost = {
488   COSTS_N_INSNS (1),                    /* cost of an add instruction */
489   COSTS_N_INSNS (3),                    /* cost of a lea instruction */
490   COSTS_N_INSNS (4),                    /* variable shift costs */
491   COSTS_N_INSNS (4),                    /* constant shift costs */
492   {COSTS_N_INSNS (15),                  /* cost of starting multiply for QI */
493    COSTS_N_INSNS (15),                  /*                               HI */
494    COSTS_N_INSNS (15),                  /*                               SI */
495    COSTS_N_INSNS (15),                  /*                               DI */
496    COSTS_N_INSNS (15)},                 /*                               other */
497   0,                                    /* cost of multiply per each bit set */
498   {COSTS_N_INSNS (56),                  /* cost of a divide/mod for QI */
499    COSTS_N_INSNS (56),                  /*                          HI */
500    COSTS_N_INSNS (56),                  /*                          SI */
501    COSTS_N_INSNS (56),                  /*                          DI */
502    COSTS_N_INSNS (56)},                 /*                          other */
503   COSTS_N_INSNS (1),                    /* cost of movsx */
504   COSTS_N_INSNS (1),                    /* cost of movzx */
505   16,                                   /* "large" insn */
506   6,                                    /* MOVE_RATIO */
507   2,                                    /* cost for loading QImode using movzbl */
508   {4, 5, 4},                            /* cost of loading integer registers
509                                            in QImode, HImode and SImode.
510                                            Relative to reg-reg move (2).  */
511   {2, 3, 2},                            /* cost of storing integer registers */
512   2,                                    /* cost of reg,reg fld/fst */
513   {2, 2, 6},                            /* cost of loading fp registers
514                                            in SFmode, DFmode and XFmode */
515   {4, 4, 6},                            /* cost of loading integer registers */
516   2,                                    /* cost of moving MMX register */
517   {2, 2},                               /* cost of loading MMX registers
518                                            in SImode and DImode */
519   {2, 2},                               /* cost of storing MMX registers
520                                            in SImode and DImode */
521   12,                                   /* cost of moving SSE register */
522   {12, 12, 12},                         /* cost of loading SSE registers
523                                            in SImode, DImode and TImode */
524   {2, 2, 8},                            /* cost of storing SSE registers
525                                            in SImode, DImode and TImode */
526   10,                                   /* MMX or SSE register to integer */
527   64,                                   /* size of prefetch block */
528   6,                                    /* number of parallel prefetches */
529   2,                                    /* Branch cost */
530   COSTS_N_INSNS (5),                    /* cost of FADD and FSUB insns.  */
531   COSTS_N_INSNS (7),                    /* cost of FMUL instruction.  */
532   COSTS_N_INSNS (43),                   /* cost of FDIV instruction.  */
533   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
534   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
535   COSTS_N_INSNS (43),                   /* cost of FSQRT instruction.  */
536 };
537
538 static const
539 struct processor_costs nocona_cost = {
540   COSTS_N_INSNS (1),                    /* cost of an add instruction */
541   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
542   COSTS_N_INSNS (1),                    /* variable shift costs */
543   COSTS_N_INSNS (1),                    /* constant shift costs */
544   {COSTS_N_INSNS (10),                  /* cost of starting multiply for QI */
545    COSTS_N_INSNS (10),                  /*                               HI */
546    COSTS_N_INSNS (10),                  /*                               SI */
547    COSTS_N_INSNS (10),                  /*                               DI */
548    COSTS_N_INSNS (10)},                 /*                               other */
549   0,                                    /* cost of multiply per each bit set */
550   {COSTS_N_INSNS (66),                  /* cost of a divide/mod for QI */
551    COSTS_N_INSNS (66),                  /*                          HI */
552    COSTS_N_INSNS (66),                  /*                          SI */
553    COSTS_N_INSNS (66),                  /*                          DI */
554    COSTS_N_INSNS (66)},                 /*                          other */
555   COSTS_N_INSNS (1),                    /* cost of movsx */
556   COSTS_N_INSNS (1),                    /* cost of movzx */
557   16,                                   /* "large" insn */
558   17,                                   /* MOVE_RATIO */
559   4,                                    /* cost for loading QImode using movzbl */
560   {4, 4, 4},                            /* cost of loading integer registers
561                                            in QImode, HImode and SImode.
562                                            Relative to reg-reg move (2).  */
563   {4, 4, 4},                            /* cost of storing integer registers */
564   3,                                    /* cost of reg,reg fld/fst */
565   {12, 12, 12},                         /* cost of loading fp registers
566                                            in SFmode, DFmode and XFmode */
567   {4, 4, 4},                            /* cost of loading integer registers */
568   6,                                    /* cost of moving MMX register */
569   {12, 12},                             /* cost of loading MMX registers
570                                            in SImode and DImode */
571   {12, 12},                             /* cost of storing MMX registers
572                                            in SImode and DImode */
573   6,                                    /* cost of moving SSE register */
574   {12, 12, 12},                         /* cost of loading SSE registers
575                                            in SImode, DImode and TImode */
576   {12, 12, 12},                         /* cost of storing SSE registers
577                                            in SImode, DImode and TImode */
578   8,                                    /* MMX or SSE register to integer */
579   128,                                  /* size of prefetch block */
580   8,                                    /* number of parallel prefetches */
581   1,                                    /* Branch cost */
582   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
583   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
584   COSTS_N_INSNS (40),                   /* cost of FDIV instruction.  */
585   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
586   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
587   COSTS_N_INSNS (44),                   /* cost of FSQRT instruction.  */
588 };
589
590 /* Generic64 should produce code tuned for Nocona and K8.  */
591 static const
592 struct processor_costs generic64_cost = {
593   COSTS_N_INSNS (1),                    /* cost of an add instruction */
594   /* On all chips taken into consideration lea is 2 cycles and more.  With
595      this cost however our current implementation of synth_mult results in
596      use of unnecesary temporary registers causing regression on several
597      SPECfp benchmarks.  */
598   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
599   COSTS_N_INSNS (1),                    /* variable shift costs */
600   COSTS_N_INSNS (1),                    /* constant shift costs */
601   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
602    COSTS_N_INSNS (4),                   /*                               HI */
603    COSTS_N_INSNS (3),                   /*                               SI */
604    COSTS_N_INSNS (4),                   /*                               DI */
605    COSTS_N_INSNS (2)},                  /*                               other */
606   0,                                    /* cost of multiply per each bit set */
607   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
608    COSTS_N_INSNS (26),                  /*                          HI */
609    COSTS_N_INSNS (42),                  /*                          SI */
610    COSTS_N_INSNS (74),                  /*                          DI */
611    COSTS_N_INSNS (74)},                 /*                          other */
612   COSTS_N_INSNS (1),                    /* cost of movsx */
613   COSTS_N_INSNS (1),                    /* cost of movzx */
614   8,                                    /* "large" insn */
615   17,                                   /* MOVE_RATIO */
616   4,                                    /* cost for loading QImode using movzbl */
617   {4, 4, 4},                            /* cost of loading integer registers
618                                            in QImode, HImode and SImode.
619                                            Relative to reg-reg move (2).  */
620   {4, 4, 4},                            /* cost of storing integer registers */
621   4,                                    /* cost of reg,reg fld/fst */
622   {12, 12, 12},                         /* cost of loading fp registers
623                                            in SFmode, DFmode and XFmode */
624   {6, 6, 8},                            /* cost of loading integer registers */
625   2,                                    /* cost of moving MMX register */
626   {8, 8},                               /* cost of loading MMX registers
627                                            in SImode and DImode */
628   {8, 8},                               /* cost of storing MMX registers
629                                            in SImode and DImode */
630   2,                                    /* cost of moving SSE register */
631   {8, 8, 8},                            /* cost of loading SSE registers
632                                            in SImode, DImode and TImode */
633   {8, 8, 8},                            /* cost of storing SSE registers
634                                            in SImode, DImode and TImode */
635   5,                                    /* MMX or SSE register to integer */
636   64,                                   /* size of prefetch block */
637   6,                                    /* number of parallel prefetches */
638   /* Benchmarks shows large regressions on K8 sixtrack benchmark when this value
639      is increased to perhaps more appropriate value of 5.  */
640   3,                                    /* Branch cost */
641   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
642   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
643   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
644   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
645   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
646   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
647 };
648
649 /* Generic32 should produce code tuned for Athlon, PPro, Pentium4, Nocona and K8.  */
650 static const
651 struct processor_costs generic32_cost = {
652   COSTS_N_INSNS (1),                    /* cost of an add instruction */
653   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
654   COSTS_N_INSNS (1),                    /* variable shift costs */
655   COSTS_N_INSNS (1),                    /* constant shift costs */
656   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
657    COSTS_N_INSNS (4),                   /*                               HI */
658    COSTS_N_INSNS (3),                   /*                               SI */
659    COSTS_N_INSNS (4),                   /*                               DI */
660    COSTS_N_INSNS (2)},                  /*                               other */
661   0,                                    /* cost of multiply per each bit set */
662   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
663    COSTS_N_INSNS (26),                  /*                          HI */
664    COSTS_N_INSNS (42),                  /*                          SI */
665    COSTS_N_INSNS (74),                  /*                          DI */
666    COSTS_N_INSNS (74)},                 /*                          other */
667   COSTS_N_INSNS (1),                    /* cost of movsx */
668   COSTS_N_INSNS (1),                    /* cost of movzx */
669   8,                                    /* "large" insn */
670   17,                                   /* MOVE_RATIO */
671   4,                                    /* cost for loading QImode using movzbl */
672   {4, 4, 4},                            /* cost of loading integer registers
673                                            in QImode, HImode and SImode.
674                                            Relative to reg-reg move (2).  */
675   {4, 4, 4},                            /* cost of storing integer registers */
676   4,                                    /* cost of reg,reg fld/fst */
677   {12, 12, 12},                         /* cost of loading fp registers
678                                            in SFmode, DFmode and XFmode */
679   {6, 6, 8},                            /* cost of loading integer registers */
680   2,                                    /* cost of moving MMX register */
681   {8, 8},                               /* cost of loading MMX registers
682                                            in SImode and DImode */
683   {8, 8},                               /* cost of storing MMX registers
684                                            in SImode and DImode */
685   2,                                    /* cost of moving SSE register */
686   {8, 8, 8},                            /* cost of loading SSE registers
687                                            in SImode, DImode and TImode */
688   {8, 8, 8},                            /* cost of storing SSE registers
689                                            in SImode, DImode and TImode */
690   5,                                    /* MMX or SSE register to integer */
691   64,                                   /* size of prefetch block */
692   6,                                    /* number of parallel prefetches */
693   3,                                    /* Branch cost */
694   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
695   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
696   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
697   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
698   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
699   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
700 };
701
702 const struct processor_costs *ix86_cost = &pentium_cost;
703
704 /* Processor feature/optimization bitmasks.  */
705 #define m_386 (1<<PROCESSOR_I386)
706 #define m_486 (1<<PROCESSOR_I486)
707 #define m_PENT (1<<PROCESSOR_PENTIUM)
708 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
709 #define m_K6  (1<<PROCESSOR_K6)
710 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
711 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
712 #define m_K8  (1<<PROCESSOR_K8)
713 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
714 #define m_NOCONA  (1<<PROCESSOR_NOCONA)
715 #define m_GENERIC32 (1<<PROCESSOR_GENERIC32)
716 #define m_GENERIC64 (1<<PROCESSOR_GENERIC64)
717 #define m_GENERIC (m_GENERIC32 | m_GENERIC64)
718
719 /* Generic instruction choice should be common subset of supported CPUs
720    (PPro/PENT4/NOCONA/Athlon/K8).  */
721
722 /* Leave is not affecting Nocona SPEC2000 results negatively, so enabling for
723    Generic64 seems like good code size tradeoff.  We can't enable it for 32bit
724    generic because it is not working well with PPro base chips.  */
725 const int x86_use_leave = m_386 | m_K6 | m_ATHLON_K8 | m_GENERIC64;
726 const int x86_push_memory = m_386 | m_K6 | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
727 const int x86_zero_extend_with_and = m_486 | m_PENT;
728 const int x86_movx = m_ATHLON_K8 | m_PPRO | m_PENT4 | m_NOCONA | m_GENERIC /* m_386 | m_K6 */;
729 const int x86_double_with_add = ~m_386;
730 const int x86_use_bit_test = m_386;
731 const int x86_unroll_strlen = m_486 | m_PENT | m_PPRO | m_ATHLON_K8 | m_K6 | m_GENERIC;
732 const int x86_cmove = m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA; 
733 const int x86_fisttp = m_NOCONA;
734 const int x86_3dnow_a = m_ATHLON_K8;
735 const int x86_deep_branch = m_PPRO | m_K6 | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
736 /* Branch hints were put in P4 based on simulation result. But
737    after P4 was made, no performance benefit was observed with
738    branch hints. It also increases the code size. As the result,
739    icc never generates branch hints.  */
740 const int x86_branch_hints = 0;
741 const int x86_use_sahf = m_PPRO | m_K6 | m_PENT4 | m_NOCONA | m_GENERIC32; /*m_GENERIC | m_ATHLON_K8 ? */
742 /* We probably ought to watch for partial register stalls on Generic32
743    compilation setting as well.  However in current implementation the
744    partial register stalls are not eliminated very well - they can
745    be introduced via subregs synthesized by combine and can happen
746    in caller/callee saving sequences.
747    Because this option pays back little on PPro based chips and is in conflict
748    with partial reg. dependencies used by Athlon/P4 based chips, it is better
749    to leave it off for generic32 for now.  */
750 const int x86_partial_reg_stall = m_PPRO;
751 const int x86_use_himode_fiop = m_386 | m_486 | m_K6;
752 const int x86_use_simode_fiop = ~(m_PPRO | m_ATHLON_K8 | m_PENT | m_GENERIC);
753 const int x86_use_mov0 = m_K6;
754 const int x86_use_cltd = ~(m_PENT | m_K6 | m_GENERIC);
755 const int x86_read_modify_write = ~m_PENT;
756 const int x86_read_modify = ~(m_PENT | m_PPRO);
757 const int x86_split_long_moves = m_PPRO;
758 const int x86_promote_QImode = m_K6 | m_PENT | m_386 | m_486 | m_ATHLON_K8 | m_GENERIC; /* m_PENT4 ? */
759 const int x86_fast_prefix = ~(m_PENT | m_486 | m_386);
760 const int x86_single_stringop = m_386 | m_PENT4 | m_NOCONA;
761 const int x86_qimode_math = ~(0);
762 const int x86_promote_qi_regs = 0;
763 /* On PPro this flag is meant to avoid partial register stalls.  Just like
764    the x86_partial_reg_stall this option might be considered for Generic32
765    if our scheme for avoiding partial stalls was more effective.  */
766 const int x86_himode_math = ~(m_PPRO);
767 const int x86_promote_hi_regs = m_PPRO;
768 const int x86_sub_esp_4 = m_ATHLON_K8 | m_PPRO | m_PENT4 | m_NOCONA | m_GENERIC;
769 const int x86_sub_esp_8 = m_ATHLON_K8 | m_PPRO | m_386 | m_486 | m_PENT4 | m_NOCONA | m_GENERIC;
770 const int x86_add_esp_4 = m_ATHLON_K8 | m_K6 | m_PENT4 | m_NOCONA | m_GENERIC;
771 const int x86_add_esp_8 = m_ATHLON_K8 | m_PPRO | m_K6 | m_386 | m_486 | m_PENT4 | m_NOCONA | m_GENERIC;
772 const int x86_integer_DFmode_moves = ~(m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO | m_GENERIC);
773 const int x86_partial_reg_dependency = m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
774 const int x86_memory_mismatch_stall = m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
775 const int x86_accumulate_outgoing_args = m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO | m_GENERIC;
776 const int x86_prologue_using_move = m_ATHLON_K8 | m_PPRO | m_GENERIC;
777 const int x86_epilogue_using_move = m_ATHLON_K8 | m_PPRO | m_GENERIC;
778 const int x86_shift1 = ~m_486;
779 const int x86_arch_always_fancy_math_387 = m_PENT | m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
780 /* In Generic model we have an conflict here in between PPro/Pentium4 based chips
781    that thread 128bit SSE registers as single units versus K8 based chips that
782    divide SSE registers to two 64bit halves.
783    x86_sse_partial_reg_dependency promote all store destinations to be 128bit
784    to allow register renaming on 128bit SSE units, but usually results in one
785    extra microop on 64bit SSE units.  Experimental results shows that disabling
786    this option on P4 brings over 20% SPECfp regression, while enabling it on
787    K8 brings roughly 2.4% regression that can be partly masked by careful scheduling
788    of moves.  */
789 const int x86_sse_partial_reg_dependency = m_PENT4 | m_NOCONA | m_PPRO | m_GENERIC;
790 /* Set for machines where the type and dependencies are resolved on SSE
791    register parts instead of whole registers, so we may maintain just
792    lower part of scalar values in proper format leaving the upper part
793    undefined.  */
794 const int x86_sse_split_regs = m_ATHLON_K8;
795 const int x86_sse_typeless_stores = m_ATHLON_K8;
796 const int x86_sse_load0_by_pxor = m_PPRO | m_PENT4 | m_NOCONA;
797 const int x86_use_ffreep = m_ATHLON_K8;
798 const int x86_rep_movl_optimal = m_386 | m_PENT | m_PPRO | m_K6;
799 const int x86_use_incdec = ~(m_PENT4 | m_NOCONA | m_GENERIC);
800
801 /* ??? Allowing interunit moves makes it all too easy for the compiler to put
802    integer data in xmm registers.  Which results in pretty abysmal code.  */
803 const int x86_inter_unit_moves = 0 /* ~(m_ATHLON_K8) */;
804
805 const int x86_ext_80387_constants = m_K6 | m_ATHLON | m_PENT4 | m_NOCONA | m_PPRO | m_GENERIC32;
806 /* Some CPU cores are not able to predict more than 4 branch instructions in
807    the 16 byte window.  */
808 const int x86_four_jump_limit = m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
809 const int x86_schedule = m_PPRO | m_ATHLON_K8 | m_K6 | m_PENT | m_GENERIC;
810 const int x86_use_bt = m_ATHLON_K8;
811 /* Compare and exchange was added for 80486.  */
812 const int x86_cmpxchg = ~m_386;
813 /* Compare and exchange 8 bytes was added for pentium.  */
814 const int x86_cmpxchg8b = ~(m_386 | m_486);
815 /* Compare and exchange 16 bytes was added for nocona.  */
816 const int x86_cmpxchg16b = m_NOCONA;
817 /* Exchange and add was added for 80486.  */
818 const int x86_xadd = ~m_386;
819 const int x86_pad_returns = m_ATHLON_K8 | m_GENERIC;
820
821 /* In case the average insn count for single function invocation is
822    lower than this constant, emit fast (but longer) prologue and
823    epilogue code.  */
824 #define FAST_PROLOGUE_INSN_COUNT 20
825
826 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
827 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
828 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
829 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
830
831 /* Array of the smallest class containing reg number REGNO, indexed by
832    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
833
834 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
835 {
836   /* ax, dx, cx, bx */
837   AREG, DREG, CREG, BREG,
838   /* si, di, bp, sp */
839   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
840   /* FP registers */
841   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
842   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
843   /* arg pointer */
844   NON_Q_REGS,
845   /* flags, fpsr, dirflag, frame */
846   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
847   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
848   SSE_REGS, SSE_REGS,
849   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
850   MMX_REGS, MMX_REGS,
851   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
852   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
853   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
854   SSE_REGS, SSE_REGS,
855 };
856
857 /* The "default" register map used in 32bit mode.  */
858
859 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
860 {
861   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
862   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
863   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, dir, frame */
864   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
865   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
866   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
867   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
868 };
869
870 static int const x86_64_int_parameter_registers[6] =
871 {
872   5 /*RDI*/, 4 /*RSI*/, 1 /*RDX*/, 2 /*RCX*/,
873   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
874 };
875
876 static int const x86_64_int_return_registers[4] =
877 {
878   0 /*RAX*/, 1 /*RDI*/, 5 /*RDI*/, 4 /*RSI*/
879 };
880
881 /* The "default" register map used in 64bit mode.  */
882 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
883 {
884   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
885   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
886   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, dir, frame */
887   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
888   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
889   8,9,10,11,12,13,14,15,                /* extended integer registers */
890   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
891 };
892
893 /* Define the register numbers to be used in Dwarf debugging information.
894    The SVR4 reference port C compiler uses the following register numbers
895    in its Dwarf output code:
896         0 for %eax (gcc regno = 0)
897         1 for %ecx (gcc regno = 2)
898         2 for %edx (gcc regno = 1)
899         3 for %ebx (gcc regno = 3)
900         4 for %esp (gcc regno = 7)
901         5 for %ebp (gcc regno = 6)
902         6 for %esi (gcc regno = 4)
903         7 for %edi (gcc regno = 5)
904    The following three DWARF register numbers are never generated by
905    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
906    believes these numbers have these meanings.
907         8  for %eip    (no gcc equivalent)
908         9  for %eflags (gcc regno = 17)
909         10 for %trapno (no gcc equivalent)
910    It is not at all clear how we should number the FP stack registers
911    for the x86 architecture.  If the version of SDB on x86/svr4 were
912    a bit less brain dead with respect to floating-point then we would
913    have a precedent to follow with respect to DWARF register numbers
914    for x86 FP registers, but the SDB on x86/svr4 is so completely
915    broken with respect to FP registers that it is hardly worth thinking
916    of it as something to strive for compatibility with.
917    The version of x86/svr4 SDB I have at the moment does (partially)
918    seem to believe that DWARF register number 11 is associated with
919    the x86 register %st(0), but that's about all.  Higher DWARF
920    register numbers don't seem to be associated with anything in
921    particular, and even for DWARF regno 11, SDB only seems to under-
922    stand that it should say that a variable lives in %st(0) (when
923    asked via an `=' command) if we said it was in DWARF regno 11,
924    but SDB still prints garbage when asked for the value of the
925    variable in question (via a `/' command).
926    (Also note that the labels SDB prints for various FP stack regs
927    when doing an `x' command are all wrong.)
928    Note that these problems generally don't affect the native SVR4
929    C compiler because it doesn't allow the use of -O with -g and
930    because when it is *not* optimizing, it allocates a memory
931    location for each floating-point variable, and the memory
932    location is what gets described in the DWARF AT_location
933    attribute for the variable in question.
934    Regardless of the severe mental illness of the x86/svr4 SDB, we
935    do something sensible here and we use the following DWARF
936    register numbers.  Note that these are all stack-top-relative
937    numbers.
938         11 for %st(0) (gcc regno = 8)
939         12 for %st(1) (gcc regno = 9)
940         13 for %st(2) (gcc regno = 10)
941         14 for %st(3) (gcc regno = 11)
942         15 for %st(4) (gcc regno = 12)
943         16 for %st(5) (gcc regno = 13)
944         17 for %st(6) (gcc regno = 14)
945         18 for %st(7) (gcc regno = 15)
946 */
947 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
948 {
949   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
950   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
951   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, dir, frame */
952   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
953   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
954   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
955   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
956 };
957
958 /* Test and compare insns in i386.md store the information needed to
959    generate branch and scc insns here.  */
960
961 rtx ix86_compare_op0 = NULL_RTX;
962 rtx ix86_compare_op1 = NULL_RTX;
963 rtx ix86_compare_emitted = NULL_RTX;
964
965 /* Size of the register save area.  */
966 #define X86_64_VARARGS_SIZE (REGPARM_MAX * UNITS_PER_WORD + SSE_REGPARM_MAX * 16)
967
968 /* Define the structure for the machine field in struct function.  */
969
970 struct stack_local_entry GTY(())
971 {
972   unsigned short mode;
973   unsigned short n;
974   rtx rtl;
975   struct stack_local_entry *next;
976 };
977
978 /* Structure describing stack frame layout.
979    Stack grows downward:
980
981    [arguments]
982                                               <- ARG_POINTER
983    saved pc
984
985    saved frame pointer if frame_pointer_needed
986                                               <- HARD_FRAME_POINTER
987    [saved regs]
988
989    [padding1]          \
990                         )
991    [va_arg registers]  (
992                         > to_allocate         <- FRAME_POINTER
993    [frame]             (
994                         )
995    [padding2]          /
996   */
997 struct ix86_frame
998 {
999   int nregs;
1000   int padding1;
1001   int va_arg_size;
1002   HOST_WIDE_INT frame;
1003   int padding2;
1004   int outgoing_arguments_size;
1005   int red_zone_size;
1006
1007   HOST_WIDE_INT to_allocate;
1008   /* The offsets relative to ARG_POINTER.  */
1009   HOST_WIDE_INT frame_pointer_offset;
1010   HOST_WIDE_INT hard_frame_pointer_offset;
1011   HOST_WIDE_INT stack_pointer_offset;
1012
1013   /* When save_regs_using_mov is set, emit prologue using
1014      move instead of push instructions.  */
1015   bool save_regs_using_mov;
1016 };
1017
1018 /* Code model option.  */
1019 enum cmodel ix86_cmodel;
1020 /* Asm dialect.  */
1021 enum asm_dialect ix86_asm_dialect = ASM_ATT;
1022 /* TLS dialects.  */
1023 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
1024
1025 /* Which unit we are generating floating point math for.  */
1026 enum fpmath_unit ix86_fpmath;
1027
1028 /* Which cpu are we scheduling for.  */
1029 enum processor_type ix86_tune;
1030 /* Which instruction set architecture to use.  */
1031 enum processor_type ix86_arch;
1032
1033 /* true if sse prefetch instruction is not NOOP.  */
1034 int x86_prefetch_sse;
1035
1036 /* ix86_regparm_string as a number */
1037 static int ix86_regparm;
1038
1039 /* Preferred alignment for stack boundary in bits.  */
1040 unsigned int ix86_preferred_stack_boundary;
1041
1042 /* Values 1-5: see jump.c */
1043 int ix86_branch_cost;
1044
1045 /* Variables which are this size or smaller are put in the data/bss
1046    or ldata/lbss sections.  */
1047
1048 int ix86_section_threshold = 65536;
1049
1050 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
1051 char internal_label_prefix[16];
1052 int internal_label_prefix_len;
1053
1054 /* Table for BUILT_IN_NORMAL to BUILT_IN_MD mapping.  */
1055 static GTY(()) tree ix86_builtin_function_variants[(int) END_BUILTINS];
1056 \f
1057 static bool ix86_handle_option (size_t, const char *, int);
1058 static void output_pic_addr_const (FILE *, rtx, int);
1059 static void put_condition_code (enum rtx_code, enum machine_mode,
1060                                 int, int, FILE *);
1061 static const char *get_some_local_dynamic_name (void);
1062 static int get_some_local_dynamic_name_1 (rtx *, void *);
1063 static rtx ix86_expand_int_compare (enum rtx_code, rtx, rtx);
1064 static enum rtx_code ix86_prepare_fp_compare_args (enum rtx_code, rtx *,
1065                                                    rtx *);
1066 static bool ix86_fixed_condition_code_regs (unsigned int *, unsigned int *);
1067 static enum machine_mode ix86_cc_modes_compatible (enum machine_mode,
1068                                                    enum machine_mode);
1069 static rtx get_thread_pointer (int);
1070 static rtx legitimize_tls_address (rtx, enum tls_model, int);
1071 static void get_pc_thunk_name (char [32], unsigned int);
1072 static rtx gen_push (rtx);
1073 static int ix86_flags_dependant (rtx, rtx, enum attr_type);
1074 static int ix86_agi_dependant (rtx, rtx, enum attr_type);
1075 static struct machine_function * ix86_init_machine_status (void);
1076 static int ix86_split_to_parts (rtx, rtx *, enum machine_mode);
1077 static int ix86_nsaved_regs (void);
1078 static void ix86_emit_save_regs (void);
1079 static void ix86_emit_save_regs_using_mov (rtx, HOST_WIDE_INT);
1080 static void ix86_emit_restore_regs_using_mov (rtx, HOST_WIDE_INT, int);
1081 static void ix86_output_function_epilogue (FILE *, HOST_WIDE_INT);
1082 static HOST_WIDE_INT ix86_GOT_alias_set (void);
1083 static void ix86_adjust_counter (rtx, HOST_WIDE_INT);
1084 static rtx ix86_expand_aligntest (rtx, int);
1085 static void ix86_expand_strlensi_unroll_1 (rtx, rtx, rtx);
1086 static int ix86_issue_rate (void);
1087 static int ix86_adjust_cost (rtx, rtx, rtx, int);
1088 static int ia32_multipass_dfa_lookahead (void);
1089 static void ix86_init_mmx_sse_builtins (void);
1090 static void ix86_init_sse_abi_builtins (void);
1091 static rtx x86_this_parameter (tree);
1092 static void x86_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
1093                                  HOST_WIDE_INT, tree);
1094 static bool x86_can_output_mi_thunk (tree, HOST_WIDE_INT, HOST_WIDE_INT, tree);
1095 static void x86_file_start (void);
1096 static void ix86_reorg (void);
1097 static bool ix86_expand_carry_flag_compare (enum rtx_code, rtx, rtx, rtx*);
1098 static tree ix86_build_builtin_va_list (void);
1099 static void ix86_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
1100                                          tree, int *, int);
1101 static tree ix86_gimplify_va_arg (tree, tree, tree *, tree *);
1102 static bool ix86_scalar_mode_supported_p (enum machine_mode);
1103 static bool ix86_vector_mode_supported_p (enum machine_mode);
1104
1105 static int ix86_address_cost (rtx);
1106 static bool ix86_cannot_force_const_mem (rtx);
1107 static rtx ix86_delegitimize_address (rtx);
1108
1109 static void i386_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
1110
1111 struct builtin_description;
1112 static rtx ix86_expand_sse_comi (const struct builtin_description *,
1113                                  tree, rtx);
1114 static rtx ix86_expand_sse_compare (const struct builtin_description *,
1115                                     tree, rtx);
1116 static rtx ix86_expand_unop1_builtin (enum insn_code, tree, rtx);
1117 static rtx ix86_expand_unop_builtin (enum insn_code, tree, rtx, int);
1118 static rtx ix86_expand_binop_builtin (enum insn_code, tree, rtx);
1119 static rtx ix86_expand_store_builtin (enum insn_code, tree);
1120 static rtx safe_vector_operand (rtx, enum machine_mode);
1121 static rtx ix86_expand_fp_compare (enum rtx_code, rtx, rtx, rtx, rtx *, rtx *);
1122 static int ix86_fp_comparison_arithmetics_cost (enum rtx_code code);
1123 static int ix86_fp_comparison_fcomi_cost (enum rtx_code code);
1124 static int ix86_fp_comparison_sahf_cost (enum rtx_code code);
1125 static int ix86_fp_comparison_cost (enum rtx_code code);
1126 static unsigned int ix86_select_alt_pic_regnum (void);
1127 static int ix86_save_reg (unsigned int, int);
1128 static void ix86_compute_frame_layout (struct ix86_frame *);
1129 static int ix86_comp_type_attributes (tree, tree);
1130 static int ix86_function_regparm (tree, tree);
1131 const struct attribute_spec ix86_attribute_table[];
1132 static bool ix86_function_ok_for_sibcall (tree, tree);
1133 static tree ix86_handle_cconv_attribute (tree *, tree, tree, int, bool *);
1134 static int ix86_value_regno (enum machine_mode, tree, tree);
1135 static bool contains_128bit_aligned_vector_p (tree);
1136 static rtx ix86_struct_value_rtx (tree, int);
1137 static bool ix86_ms_bitfield_layout_p (tree);
1138 static tree ix86_handle_struct_attribute (tree *, tree, tree, int, bool *);
1139 static int extended_reg_mentioned_1 (rtx *, void *);
1140 static bool ix86_rtx_costs (rtx, int, int, int *);
1141 static int min_insn_size (rtx);
1142 static tree ix86_md_asm_clobbers (tree outputs, tree inputs, tree clobbers);
1143 static bool ix86_must_pass_in_stack (enum machine_mode mode, tree type);
1144 static bool ix86_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
1145                                     tree, bool);
1146 static void ix86_init_builtins (void);
1147 static rtx ix86_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
1148 static rtx ix86_expand_library_builtin (tree, rtx, rtx, enum machine_mode, int);
1149 static const char *ix86_mangle_fundamental_type (tree);
1150 static tree ix86_stack_protect_fail (void);
1151 static rtx ix86_internal_arg_pointer (void);
1152 static void ix86_dwarf_handle_frame_unspec (const char *, rtx, int);
1153
1154 /* This function is only used on Solaris.  */
1155 static void i386_solaris_elf_named_section (const char *, unsigned int, tree)
1156   ATTRIBUTE_UNUSED;
1157
1158 /* Register class used for passing given 64bit part of the argument.
1159    These represent classes as documented by the PS ABI, with the exception
1160    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
1161    use SF or DFmode move instead of DImode to avoid reformatting penalties.
1162
1163    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
1164    whenever possible (upper half does contain padding).
1165  */
1166 enum x86_64_reg_class
1167   {
1168     X86_64_NO_CLASS,
1169     X86_64_INTEGER_CLASS,
1170     X86_64_INTEGERSI_CLASS,
1171     X86_64_SSE_CLASS,
1172     X86_64_SSESF_CLASS,
1173     X86_64_SSEDF_CLASS,
1174     X86_64_SSEUP_CLASS,
1175     X86_64_X87_CLASS,
1176     X86_64_X87UP_CLASS,
1177     X86_64_COMPLEX_X87_CLASS,
1178     X86_64_MEMORY_CLASS
1179   };
1180 static const char * const x86_64_reg_class_name[] = {
1181   "no", "integer", "integerSI", "sse", "sseSF", "sseDF",
1182   "sseup", "x87", "x87up", "cplx87", "no"
1183 };
1184
1185 #define MAX_CLASSES 4
1186
1187 /* Table of constants used by fldpi, fldln2, etc....  */
1188 static REAL_VALUE_TYPE ext_80387_constants_table [5];
1189 static bool ext_80387_constants_init = 0;
1190 static void init_ext_80387_constants (void);
1191 static bool ix86_in_large_data_p (tree) ATTRIBUTE_UNUSED;
1192 static void ix86_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
1193 static void x86_64_elf_unique_section (tree decl, int reloc) ATTRIBUTE_UNUSED;
1194 static section *x86_64_elf_select_section (tree decl, int reloc,
1195                                            unsigned HOST_WIDE_INT align)
1196                                              ATTRIBUTE_UNUSED;
1197 \f
1198 /* Initialize the GCC target structure.  */
1199 #undef TARGET_ATTRIBUTE_TABLE
1200 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
1201 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1202 #  undef TARGET_MERGE_DECL_ATTRIBUTES
1203 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
1204 #endif
1205
1206 #undef TARGET_COMP_TYPE_ATTRIBUTES
1207 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
1208
1209 #undef TARGET_INIT_BUILTINS
1210 #define TARGET_INIT_BUILTINS ix86_init_builtins
1211 #undef TARGET_EXPAND_BUILTIN
1212 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
1213 #undef TARGET_EXPAND_LIBRARY_BUILTIN
1214 #define TARGET_EXPAND_LIBRARY_BUILTIN ix86_expand_library_builtin
1215
1216 #undef TARGET_ASM_FUNCTION_EPILOGUE
1217 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
1218
1219 #undef TARGET_ENCODE_SECTION_INFO
1220 #ifndef SUBTARGET_ENCODE_SECTION_INFO
1221 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
1222 #else
1223 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
1224 #endif
1225
1226 #undef TARGET_ASM_OPEN_PAREN
1227 #define TARGET_ASM_OPEN_PAREN ""
1228 #undef TARGET_ASM_CLOSE_PAREN
1229 #define TARGET_ASM_CLOSE_PAREN ""
1230
1231 #undef TARGET_ASM_ALIGNED_HI_OP
1232 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
1233 #undef TARGET_ASM_ALIGNED_SI_OP
1234 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
1235 #ifdef ASM_QUAD
1236 #undef TARGET_ASM_ALIGNED_DI_OP
1237 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
1238 #endif
1239
1240 #undef TARGET_ASM_UNALIGNED_HI_OP
1241 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
1242 #undef TARGET_ASM_UNALIGNED_SI_OP
1243 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
1244 #undef TARGET_ASM_UNALIGNED_DI_OP
1245 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
1246
1247 #undef TARGET_SCHED_ADJUST_COST
1248 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
1249 #undef TARGET_SCHED_ISSUE_RATE
1250 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
1251 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1252 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
1253   ia32_multipass_dfa_lookahead
1254
1255 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1256 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
1257
1258 #ifdef HAVE_AS_TLS
1259 #undef TARGET_HAVE_TLS
1260 #define TARGET_HAVE_TLS true
1261 #endif
1262 #undef TARGET_CANNOT_FORCE_CONST_MEM
1263 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
1264 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1265 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_rtx_true
1266
1267 #undef TARGET_DELEGITIMIZE_ADDRESS
1268 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
1269
1270 #undef TARGET_MS_BITFIELD_LAYOUT_P
1271 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
1272
1273 #if TARGET_MACHO
1274 #undef TARGET_BINDS_LOCAL_P
1275 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1276 #endif
1277
1278 #undef TARGET_ASM_OUTPUT_MI_THUNK
1279 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
1280 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1281 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
1282
1283 #undef TARGET_ASM_FILE_START
1284 #define TARGET_ASM_FILE_START x86_file_start
1285
1286 #undef TARGET_DEFAULT_TARGET_FLAGS
1287 #define TARGET_DEFAULT_TARGET_FLAGS     \
1288   (TARGET_DEFAULT                       \
1289    | TARGET_64BIT_DEFAULT               \
1290    | TARGET_SUBTARGET_DEFAULT           \
1291    | TARGET_TLS_DIRECT_SEG_REFS_DEFAULT)
1292
1293 #undef TARGET_HANDLE_OPTION
1294 #define TARGET_HANDLE_OPTION ix86_handle_option
1295
1296 #undef TARGET_RTX_COSTS
1297 #define TARGET_RTX_COSTS ix86_rtx_costs
1298 #undef TARGET_ADDRESS_COST
1299 #define TARGET_ADDRESS_COST ix86_address_cost
1300
1301 #undef TARGET_FIXED_CONDITION_CODE_REGS
1302 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
1303 #undef TARGET_CC_MODES_COMPATIBLE
1304 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
1305
1306 #undef TARGET_MACHINE_DEPENDENT_REORG
1307 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
1308
1309 #undef TARGET_BUILD_BUILTIN_VA_LIST
1310 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
1311
1312 #undef TARGET_MD_ASM_CLOBBERS
1313 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
1314
1315 #undef TARGET_PROMOTE_PROTOTYPES
1316 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
1317 #undef TARGET_STRUCT_VALUE_RTX
1318 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
1319 #undef TARGET_SETUP_INCOMING_VARARGS
1320 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
1321 #undef TARGET_MUST_PASS_IN_STACK
1322 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
1323 #undef TARGET_PASS_BY_REFERENCE
1324 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
1325 #undef TARGET_INTERNAL_ARG_POINTER
1326 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
1327 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
1328 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC ix86_dwarf_handle_frame_unspec
1329
1330 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1331 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
1332
1333 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1334 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
1335
1336 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1337 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
1338
1339 #ifdef HAVE_AS_TLS
1340 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1341 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
1342 #endif
1343
1344 #ifdef SUBTARGET_INSERT_ATTRIBUTES
1345 #undef TARGET_INSERT_ATTRIBUTES
1346 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
1347 #endif
1348
1349 #undef TARGET_MANGLE_FUNDAMENTAL_TYPE
1350 #define TARGET_MANGLE_FUNDAMENTAL_TYPE ix86_mangle_fundamental_type
1351
1352 #undef TARGET_STACK_PROTECT_FAIL
1353 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
1354
1355 #undef TARGET_FUNCTION_VALUE
1356 #define TARGET_FUNCTION_VALUE ix86_function_value
1357
1358 struct gcc_target targetm = TARGET_INITIALIZER;
1359
1360 \f
1361 /* The svr4 ABI for the i386 says that records and unions are returned
1362    in memory.  */
1363 #ifndef DEFAULT_PCC_STRUCT_RETURN
1364 #define DEFAULT_PCC_STRUCT_RETURN 1
1365 #endif
1366
1367 /* Implement TARGET_HANDLE_OPTION.  */
1368
1369 static bool
1370 ix86_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED, int value)
1371 {
1372   switch (code)
1373     {
1374     case OPT_m3dnow:
1375       if (!value)
1376         {
1377           target_flags &= ~MASK_3DNOW_A;
1378           target_flags_explicit |= MASK_3DNOW_A;
1379         }
1380       return true;
1381
1382     case OPT_mmmx:
1383       if (!value)
1384         {
1385           target_flags &= ~(MASK_3DNOW | MASK_3DNOW_A);
1386           target_flags_explicit |= MASK_3DNOW | MASK_3DNOW_A;
1387         }
1388       return true;
1389
1390     case OPT_msse:
1391       if (!value)
1392         {
1393           target_flags &= ~(MASK_SSE2 | MASK_SSE3);
1394           target_flags_explicit |= MASK_SSE2 | MASK_SSE3;
1395         }
1396       return true;
1397
1398     case OPT_msse2:
1399       if (!value)
1400         {
1401           target_flags &= ~MASK_SSE3;
1402           target_flags_explicit |= MASK_SSE3;
1403         }
1404       return true;
1405
1406     default:
1407       return true;
1408     }
1409 }
1410
1411 /* Sometimes certain combinations of command options do not make
1412    sense on a particular target machine.  You can define a macro
1413    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
1414    defined, is executed once just after all the command options have
1415    been parsed.
1416
1417    Don't use this macro to turn on various extra optimizations for
1418    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
1419
1420 void
1421 override_options (void)
1422 {
1423   int i;
1424   int ix86_tune_defaulted = 0;
1425
1426   /* Comes from final.c -- no real reason to change it.  */
1427 #define MAX_CODE_ALIGN 16
1428
1429   static struct ptt
1430     {
1431       const struct processor_costs *cost;       /* Processor costs */
1432       const int target_enable;                  /* Target flags to enable.  */
1433       const int target_disable;                 /* Target flags to disable.  */
1434       const int align_loop;                     /* Default alignments.  */
1435       const int align_loop_max_skip;
1436       const int align_jump;
1437       const int align_jump_max_skip;
1438       const int align_func;
1439     }
1440   const processor_target_table[PROCESSOR_max] =
1441     {
1442       {&i386_cost, 0, 0, 4, 3, 4, 3, 4},
1443       {&i486_cost, 0, 0, 16, 15, 16, 15, 16},
1444       {&pentium_cost, 0, 0, 16, 7, 16, 7, 16},
1445       {&pentiumpro_cost, 0, 0, 16, 15, 16, 7, 16},
1446       {&k6_cost, 0, 0, 32, 7, 32, 7, 32},
1447       {&athlon_cost, 0, 0, 16, 7, 16, 7, 16},
1448       {&pentium4_cost, 0, 0, 0, 0, 0, 0, 0},
1449       {&k8_cost, 0, 0, 16, 7, 16, 7, 16},
1450       {&nocona_cost, 0, 0, 0, 0, 0, 0, 0},
1451       {&generic32_cost, 0, 0, 16, 7, 16, 7, 16},
1452       {&generic64_cost, 0, 0, 16, 7, 16, 7, 16}
1453     };
1454
1455   static const char * const cpu_names[] = TARGET_CPU_DEFAULT_NAMES;
1456   static struct pta
1457     {
1458       const char *const name;           /* processor name or nickname.  */
1459       const enum processor_type processor;
1460       const enum pta_flags
1461         {
1462           PTA_SSE = 1,
1463           PTA_SSE2 = 2,
1464           PTA_SSE3 = 4,
1465           PTA_MMX = 8,
1466           PTA_PREFETCH_SSE = 16,
1467           PTA_3DNOW = 32,
1468           PTA_3DNOW_A = 64,
1469           PTA_64BIT = 128
1470         } flags;
1471     }
1472   const processor_alias_table[] =
1473     {
1474       {"i386", PROCESSOR_I386, 0},
1475       {"i486", PROCESSOR_I486, 0},
1476       {"i586", PROCESSOR_PENTIUM, 0},
1477       {"pentium", PROCESSOR_PENTIUM, 0},
1478       {"pentium-mmx", PROCESSOR_PENTIUM, PTA_MMX},
1479       {"winchip-c6", PROCESSOR_I486, PTA_MMX},
1480       {"winchip2", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1481       {"c3", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1482       {"c3-2", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_PREFETCH_SSE | PTA_SSE},
1483       {"i686", PROCESSOR_PENTIUMPRO, 0},
1484       {"pentiumpro", PROCESSOR_PENTIUMPRO, 0},
1485       {"pentium2", PROCESSOR_PENTIUMPRO, PTA_MMX},
1486       {"pentium3", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1487       {"pentium3m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1488       {"pentium-m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE | PTA_SSE2},
1489       {"pentium4", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1490                                        | PTA_MMX | PTA_PREFETCH_SSE},
1491       {"pentium4m", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1492                                         | PTA_MMX | PTA_PREFETCH_SSE},
1493       {"prescott", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3
1494                                         | PTA_MMX | PTA_PREFETCH_SSE},
1495       {"nocona", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3 | PTA_64BIT
1496                                         | PTA_MMX | PTA_PREFETCH_SSE},
1497       {"k6", PROCESSOR_K6, PTA_MMX},
1498       {"k6-2", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1499       {"k6-3", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1500       {"athlon", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1501                                    | PTA_3DNOW_A},
1502       {"athlon-tbird", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE
1503                                          | PTA_3DNOW | PTA_3DNOW_A},
1504       {"athlon-4", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1505                                     | PTA_3DNOW_A | PTA_SSE},
1506       {"athlon-xp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1507                                       | PTA_3DNOW_A | PTA_SSE},
1508       {"athlon-mp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1509                                       | PTA_3DNOW_A | PTA_SSE},
1510       {"x86-64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_64BIT
1511                                | PTA_SSE | PTA_SSE2 },
1512       {"k8", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1513                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1514       {"opteron", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1515                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1516       {"athlon64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1517                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1518       {"athlon-fx", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1519                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1520       {"generic32", PROCESSOR_GENERIC32, 0 /* flags are only used for -march switch.  */ },
1521       {"generic64", PROCESSOR_GENERIC64, PTA_64BIT /* flags are only used for -march switch.  */ },
1522     };
1523
1524   int const pta_size = ARRAY_SIZE (processor_alias_table);
1525
1526 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1527   SUBTARGET_OVERRIDE_OPTIONS;
1528 #endif
1529
1530   /* Set the default values for switches whose default depends on TARGET_64BIT
1531      in case they weren't overwritten by command line options.  */
1532   if (TARGET_64BIT)
1533     {
1534       if (flag_omit_frame_pointer == 2)
1535         flag_omit_frame_pointer = 1;
1536       if (flag_asynchronous_unwind_tables == 2)
1537         flag_asynchronous_unwind_tables = 1;
1538       if (flag_pcc_struct_return == 2)
1539         flag_pcc_struct_return = 0;
1540     }
1541   else
1542     {
1543       if (flag_omit_frame_pointer == 2)
1544         flag_omit_frame_pointer = 0;
1545       if (flag_asynchronous_unwind_tables == 2)
1546         flag_asynchronous_unwind_tables = 0;
1547       if (flag_pcc_struct_return == 2)
1548         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
1549     }
1550
1551   /* Need to check -mtune=generic first.  */
1552   if (ix86_tune_string)
1553     {
1554       if (!strcmp (ix86_tune_string, "generic")
1555           || !strcmp (ix86_tune_string, "i686"))
1556         {
1557           if (TARGET_64BIT)
1558             ix86_tune_string = "generic64";
1559           else
1560             ix86_tune_string = "generic32";
1561         }
1562       else if (!strncmp (ix86_tune_string, "generic", 7))
1563         error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1564     }
1565   else
1566     {
1567       if (ix86_arch_string)
1568         ix86_tune_string = ix86_arch_string;
1569       if (!ix86_tune_string)
1570         {
1571           ix86_tune_string = cpu_names [TARGET_CPU_DEFAULT];
1572           ix86_tune_defaulted = 1;
1573         }
1574
1575       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
1576          need to use a sensible tune option.  */
1577       if (!strcmp (ix86_tune_string, "generic")
1578           || !strcmp (ix86_tune_string, "x86-64")
1579           || !strcmp (ix86_tune_string, "i686"))
1580         {
1581           if (TARGET_64BIT)
1582             ix86_tune_string = "generic64";
1583           else
1584             ix86_tune_string = "generic32";
1585         }
1586     }
1587   if (!strcmp (ix86_tune_string, "x86-64"))
1588     warning (OPT_Wdeprecated, "-mtune=x86-64 is deprecated.  Use -mtune=k8 or "
1589              "-mtune=generic instead as appropriate.");
1590
1591   if (!ix86_arch_string)
1592     ix86_arch_string = TARGET_64BIT ? "x86-64" : "i386";
1593   if (!strcmp (ix86_arch_string, "generic"))
1594     error ("generic CPU can be used only for -mtune= switch");
1595   if (!strncmp (ix86_arch_string, "generic", 7))
1596     error ("bad value (%s) for -march= switch", ix86_arch_string);
1597
1598   if (ix86_cmodel_string != 0)
1599     {
1600       if (!strcmp (ix86_cmodel_string, "small"))
1601         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1602       else if (!strcmp (ix86_cmodel_string, "medium"))
1603         ix86_cmodel = flag_pic ? CM_MEDIUM_PIC : CM_MEDIUM;
1604       else if (flag_pic)
1605         sorry ("code model %s not supported in PIC mode", ix86_cmodel_string);
1606       else if (!strcmp (ix86_cmodel_string, "32"))
1607         ix86_cmodel = CM_32;
1608       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
1609         ix86_cmodel = CM_KERNEL;
1610       else if (!strcmp (ix86_cmodel_string, "large") && !flag_pic)
1611         ix86_cmodel = CM_LARGE;
1612       else
1613         error ("bad value (%s) for -mcmodel= switch", ix86_cmodel_string);
1614     }
1615   else
1616     {
1617       ix86_cmodel = CM_32;
1618       if (TARGET_64BIT)
1619         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1620     }
1621   if (ix86_asm_string != 0)
1622     {
1623       if (! TARGET_MACHO
1624           && !strcmp (ix86_asm_string, "intel"))
1625         ix86_asm_dialect = ASM_INTEL;
1626       else if (!strcmp (ix86_asm_string, "att"))
1627         ix86_asm_dialect = ASM_ATT;
1628       else
1629         error ("bad value (%s) for -masm= switch", ix86_asm_string);
1630     }
1631   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
1632     error ("code model %qs not supported in the %s bit mode",
1633            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
1634   if (ix86_cmodel == CM_LARGE)
1635     sorry ("code model %<large%> not supported yet");
1636   if ((TARGET_64BIT != 0) != ((target_flags & MASK_64BIT) != 0))
1637     sorry ("%i-bit mode not compiled in",
1638            (target_flags & MASK_64BIT) ? 64 : 32);
1639
1640   for (i = 0; i < pta_size; i++)
1641     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
1642       {
1643         ix86_arch = processor_alias_table[i].processor;
1644         /* Default cpu tuning to the architecture.  */
1645         ix86_tune = ix86_arch;
1646         if (processor_alias_table[i].flags & PTA_MMX
1647             && !(target_flags_explicit & MASK_MMX))
1648           target_flags |= MASK_MMX;
1649         if (processor_alias_table[i].flags & PTA_3DNOW
1650             && !(target_flags_explicit & MASK_3DNOW))
1651           target_flags |= MASK_3DNOW;
1652         if (processor_alias_table[i].flags & PTA_3DNOW_A
1653             && !(target_flags_explicit & MASK_3DNOW_A))
1654           target_flags |= MASK_3DNOW_A;
1655         if (processor_alias_table[i].flags & PTA_SSE
1656             && !(target_flags_explicit & MASK_SSE))
1657           target_flags |= MASK_SSE;
1658         if (processor_alias_table[i].flags & PTA_SSE2
1659             && !(target_flags_explicit & MASK_SSE2))
1660           target_flags |= MASK_SSE2;
1661         if (processor_alias_table[i].flags & PTA_SSE3
1662             && !(target_flags_explicit & MASK_SSE3))
1663           target_flags |= MASK_SSE3;
1664         if (processor_alias_table[i].flags & PTA_PREFETCH_SSE)
1665           x86_prefetch_sse = true;
1666         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1667           error ("CPU you selected does not support x86-64 "
1668                  "instruction set");
1669         break;
1670       }
1671
1672   if (i == pta_size)
1673     error ("bad value (%s) for -march= switch", ix86_arch_string);
1674
1675   for (i = 0; i < pta_size; i++)
1676     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
1677       {
1678         ix86_tune = processor_alias_table[i].processor;
1679         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1680           {
1681             if (ix86_tune_defaulted)
1682               {
1683                 ix86_tune_string = "x86-64";
1684                 for (i = 0; i < pta_size; i++)
1685                   if (! strcmp (ix86_tune_string,
1686                                 processor_alias_table[i].name))
1687                     break;
1688                 ix86_tune = processor_alias_table[i].processor;
1689               }
1690             else
1691               error ("CPU you selected does not support x86-64 "
1692                      "instruction set");
1693           }
1694         /* Intel CPUs have always interpreted SSE prefetch instructions as
1695            NOPs; so, we can enable SSE prefetch instructions even when
1696            -mtune (rather than -march) points us to a processor that has them.
1697            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
1698            higher processors.  */
1699         if (TARGET_CMOVE && (processor_alias_table[i].flags & PTA_PREFETCH_SSE))
1700           x86_prefetch_sse = true;
1701         break;
1702       }
1703   if (i == pta_size)
1704     error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1705
1706   if (optimize_size)
1707     ix86_cost = &size_cost;
1708   else
1709     ix86_cost = processor_target_table[ix86_tune].cost;
1710   target_flags |= processor_target_table[ix86_tune].target_enable;
1711   target_flags &= ~processor_target_table[ix86_tune].target_disable;
1712
1713   /* Arrange to set up i386_stack_locals for all functions.  */
1714   init_machine_status = ix86_init_machine_status;
1715
1716   /* Validate -mregparm= value.  */
1717   if (ix86_regparm_string)
1718     {
1719       i = atoi (ix86_regparm_string);
1720       if (i < 0 || i > REGPARM_MAX)
1721         error ("-mregparm=%d is not between 0 and %d", i, REGPARM_MAX);
1722       else
1723         ix86_regparm = i;
1724     }
1725   else
1726    if (TARGET_64BIT)
1727      ix86_regparm = REGPARM_MAX;
1728
1729   /* If the user has provided any of the -malign-* options,
1730      warn and use that value only if -falign-* is not set.
1731      Remove this code in GCC 3.2 or later.  */
1732   if (ix86_align_loops_string)
1733     {
1734       warning (0, "-malign-loops is obsolete, use -falign-loops");
1735       if (align_loops == 0)
1736         {
1737           i = atoi (ix86_align_loops_string);
1738           if (i < 0 || i > MAX_CODE_ALIGN)
1739             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1740           else
1741             align_loops = 1 << i;
1742         }
1743     }
1744
1745   if (ix86_align_jumps_string)
1746     {
1747       warning (0, "-malign-jumps is obsolete, use -falign-jumps");
1748       if (align_jumps == 0)
1749         {
1750           i = atoi (ix86_align_jumps_string);
1751           if (i < 0 || i > MAX_CODE_ALIGN)
1752             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1753           else
1754             align_jumps = 1 << i;
1755         }
1756     }
1757
1758   if (ix86_align_funcs_string)
1759     {
1760       warning (0, "-malign-functions is obsolete, use -falign-functions");
1761       if (align_functions == 0)
1762         {
1763           i = atoi (ix86_align_funcs_string);
1764           if (i < 0 || i > MAX_CODE_ALIGN)
1765             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1766           else
1767             align_functions = 1 << i;
1768         }
1769     }
1770
1771   /* Default align_* from the processor table.  */
1772   if (align_loops == 0)
1773     {
1774       align_loops = processor_target_table[ix86_tune].align_loop;
1775       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
1776     }
1777   if (align_jumps == 0)
1778     {
1779       align_jumps = processor_target_table[ix86_tune].align_jump;
1780       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
1781     }
1782   if (align_functions == 0)
1783     {
1784       align_functions = processor_target_table[ix86_tune].align_func;
1785     }
1786
1787   /* Validate -mpreferred-stack-boundary= value, or provide default.
1788      The default of 128 bits is for Pentium III's SSE __m128, but we
1789      don't want additional code to keep the stack aligned when
1790      optimizing for code size.  */
1791   ix86_preferred_stack_boundary = ((TARGET_64BIT || TARGET_MACHO || !optimize_size)
1792                                    ? 128 : 32);
1793   if (ix86_preferred_stack_boundary_string)
1794     {
1795       i = atoi (ix86_preferred_stack_boundary_string);
1796       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
1797         error ("-mpreferred-stack-boundary=%d is not between %d and 12", i,
1798                TARGET_64BIT ? 4 : 2);
1799       else
1800         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
1801     }
1802
1803   /* Validate -mbranch-cost= value, or provide default.  */
1804   ix86_branch_cost = ix86_cost->branch_cost;
1805   if (ix86_branch_cost_string)
1806     {
1807       i = atoi (ix86_branch_cost_string);
1808       if (i < 0 || i > 5)
1809         error ("-mbranch-cost=%d is not between 0 and 5", i);
1810       else
1811         ix86_branch_cost = i;
1812     }
1813   if (ix86_section_threshold_string)
1814     {
1815       i = atoi (ix86_section_threshold_string);
1816       if (i < 0)
1817         error ("-mlarge-data-threshold=%d is negative", i);
1818       else
1819         ix86_section_threshold = i;
1820     }
1821
1822   if (ix86_tls_dialect_string)
1823     {
1824       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
1825         ix86_tls_dialect = TLS_DIALECT_GNU;
1826       else if (strcmp (ix86_tls_dialect_string, "gnu2") == 0)
1827         ix86_tls_dialect = TLS_DIALECT_GNU2;
1828       else if (strcmp (ix86_tls_dialect_string, "sun") == 0)
1829         ix86_tls_dialect = TLS_DIALECT_SUN;
1830       else
1831         error ("bad value (%s) for -mtls-dialect= switch",
1832                ix86_tls_dialect_string);
1833     }
1834
1835   /* Keep nonleaf frame pointers.  */
1836   if (flag_omit_frame_pointer)
1837     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
1838   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
1839     flag_omit_frame_pointer = 1;
1840
1841   /* If we're doing fast math, we don't care about comparison order
1842      wrt NaNs.  This lets us use a shorter comparison sequence.  */
1843   if (flag_unsafe_math_optimizations)
1844     target_flags &= ~MASK_IEEE_FP;
1845
1846   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
1847      since the insns won't need emulation.  */
1848   if (x86_arch_always_fancy_math_387 & (1 << ix86_arch))
1849     target_flags &= ~MASK_NO_FANCY_MATH_387;
1850
1851   /* Likewise, if the target doesn't have a 387, or we've specified
1852      software floating point, don't use 387 inline intrinsics.  */
1853   if (!TARGET_80387)
1854     target_flags |= MASK_NO_FANCY_MATH_387;
1855
1856   /* Turn on SSE2 builtins for -msse3.  */
1857   if (TARGET_SSE3)
1858     target_flags |= MASK_SSE2;
1859
1860   /* Turn on SSE builtins for -msse2.  */
1861   if (TARGET_SSE2)
1862     target_flags |= MASK_SSE;
1863
1864   /* Turn on MMX builtins for -msse.  */
1865   if (TARGET_SSE)
1866     {
1867       target_flags |= MASK_MMX & ~target_flags_explicit;
1868       x86_prefetch_sse = true;
1869     }
1870
1871   /* Turn on MMX builtins for 3Dnow.  */
1872   if (TARGET_3DNOW)
1873     target_flags |= MASK_MMX;
1874
1875   if (TARGET_64BIT)
1876     {
1877       if (TARGET_ALIGN_DOUBLE)
1878         error ("-malign-double makes no sense in the 64bit mode");
1879       if (TARGET_RTD)
1880         error ("-mrtd calling convention not supported in the 64bit mode");
1881
1882       /* Enable by default the SSE and MMX builtins.  Do allow the user to
1883          explicitly disable any of these.  In particular, disabling SSE and
1884          MMX for kernel code is extremely useful.  */
1885       target_flags
1886         |= ((MASK_SSE2 | MASK_SSE | MASK_MMX | MASK_128BIT_LONG_DOUBLE)
1887             & ~target_flags_explicit);
1888      }
1889   else
1890     {
1891       /* i386 ABI does not specify red zone.  It still makes sense to use it
1892          when programmer takes care to stack from being destroyed.  */
1893       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
1894         target_flags |= MASK_NO_RED_ZONE;
1895     }
1896
1897   /* Accept -msseregparm only if at least SSE support is enabled.  */
1898   if (TARGET_SSEREGPARM
1899       && ! TARGET_SSE)
1900     error ("-msseregparm used without SSE enabled");
1901
1902   /* Accept -msselibm only if at least SSE support is enabled.  */
1903   if (TARGET_SSELIBM
1904       && ! TARGET_SSE2)
1905     error ("-msselibm used without SSE2 enabled");
1906
1907   /* Ignore -msselibm on 64bit targets.  */
1908   if (TARGET_SSELIBM
1909       && TARGET_64BIT)
1910     error ("-msselibm used on a 64bit target");
1911
1912   ix86_fpmath = TARGET_FPMATH_DEFAULT;
1913
1914   if (ix86_fpmath_string != 0)
1915     {
1916       if (! strcmp (ix86_fpmath_string, "387"))
1917         ix86_fpmath = FPMATH_387;
1918       else if (! strcmp (ix86_fpmath_string, "sse"))
1919         {
1920           if (!TARGET_SSE)
1921             {
1922               warning (0, "SSE instruction set disabled, using 387 arithmetics");
1923               ix86_fpmath = FPMATH_387;
1924             }
1925           else
1926             ix86_fpmath = FPMATH_SSE;
1927         }
1928       else if (! strcmp (ix86_fpmath_string, "387,sse")
1929                || ! strcmp (ix86_fpmath_string, "sse,387"))
1930         {
1931           if (!TARGET_SSE)
1932             {
1933               warning (0, "SSE instruction set disabled, using 387 arithmetics");
1934               ix86_fpmath = FPMATH_387;
1935             }
1936           else if (!TARGET_80387)
1937             {
1938               warning (0, "387 instruction set disabled, using SSE arithmetics");
1939               ix86_fpmath = FPMATH_SSE;
1940             }
1941           else
1942             ix86_fpmath = FPMATH_SSE | FPMATH_387;
1943         }
1944       else
1945         error ("bad value (%s) for -mfpmath= switch", ix86_fpmath_string);
1946     }
1947
1948   /* If the i387 is disabled, then do not return values in it. */
1949   if (!TARGET_80387)
1950     target_flags &= ~MASK_FLOAT_RETURNS;
1951
1952   if ((x86_accumulate_outgoing_args & TUNEMASK)
1953       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
1954       && !optimize_size)
1955     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
1956
1957   /* ??? Unwind info is not correct around the CFG unless either a frame
1958      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
1959      unwind info generation to be aware of the CFG and propagating states
1960      around edges.  */
1961   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
1962        || flag_exceptions || flag_non_call_exceptions)
1963       && flag_omit_frame_pointer
1964       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
1965     {
1966       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
1967         warning (0, "unwind tables currently require either a frame pointer "
1968                  "or -maccumulate-outgoing-args for correctness");
1969       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
1970     }
1971
1972   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
1973   {
1974     char *p;
1975     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
1976     p = strchr (internal_label_prefix, 'X');
1977     internal_label_prefix_len = p - internal_label_prefix;
1978     *p = '\0';
1979   }
1980
1981   /* When scheduling description is not available, disable scheduler pass
1982      so it won't slow down the compilation and make x87 code slower.  */
1983   if (!TARGET_SCHEDULE)
1984     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
1985 }
1986 \f
1987 /* switch to the appropriate section for output of DECL.
1988    DECL is either a `VAR_DECL' node or a constant of some sort.
1989    RELOC indicates whether forming the initial value of DECL requires
1990    link-time relocations.  */
1991
1992 static section *
1993 x86_64_elf_select_section (tree decl, int reloc,
1994                            unsigned HOST_WIDE_INT align)
1995 {
1996   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
1997       && ix86_in_large_data_p (decl))
1998     {
1999       const char *sname = NULL;
2000       unsigned int flags = SECTION_WRITE;
2001       switch (categorize_decl_for_section (decl, reloc, flag_pic))
2002         {
2003         case SECCAT_DATA:
2004           sname = ".ldata";
2005           break;
2006         case SECCAT_DATA_REL:
2007           sname = ".ldata.rel";
2008           break;
2009         case SECCAT_DATA_REL_LOCAL:
2010           sname = ".ldata.rel.local";
2011           break;
2012         case SECCAT_DATA_REL_RO:
2013           sname = ".ldata.rel.ro";
2014           break;
2015         case SECCAT_DATA_REL_RO_LOCAL:
2016           sname = ".ldata.rel.ro.local";
2017           break;
2018         case SECCAT_BSS:
2019           sname = ".lbss";
2020           flags |= SECTION_BSS;
2021           break;
2022         case SECCAT_RODATA:
2023         case SECCAT_RODATA_MERGE_STR:
2024         case SECCAT_RODATA_MERGE_STR_INIT:
2025         case SECCAT_RODATA_MERGE_CONST:
2026           sname = ".lrodata";
2027           flags = 0;
2028           break;
2029         case SECCAT_SRODATA:
2030         case SECCAT_SDATA:
2031         case SECCAT_SBSS:
2032           gcc_unreachable ();
2033         case SECCAT_TEXT:
2034         case SECCAT_TDATA:
2035         case SECCAT_TBSS:
2036           /* We don't split these for medium model.  Place them into
2037              default sections and hope for best.  */
2038           break;
2039         }
2040       if (sname)
2041         {
2042           /* We might get called with string constants, but get_named_section
2043              doesn't like them as they are not DECLs.  Also, we need to set
2044              flags in that case.  */
2045           if (!DECL_P (decl))
2046             return get_section (sname, flags, NULL);
2047           return get_named_section (decl, sname, reloc);
2048         }
2049     }
2050   return default_elf_select_section (decl, reloc, align);
2051 }
2052
2053 /* Build up a unique section name, expressed as a
2054    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
2055    RELOC indicates whether the initial value of EXP requires
2056    link-time relocations.  */
2057
2058 static void
2059 x86_64_elf_unique_section (tree decl, int reloc)
2060 {
2061   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2062       && ix86_in_large_data_p (decl))
2063     {
2064       const char *prefix = NULL;
2065       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
2066       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
2067
2068       switch (categorize_decl_for_section (decl, reloc, flag_pic))
2069         {
2070         case SECCAT_DATA:
2071         case SECCAT_DATA_REL:
2072         case SECCAT_DATA_REL_LOCAL:
2073         case SECCAT_DATA_REL_RO:
2074         case SECCAT_DATA_REL_RO_LOCAL:
2075           prefix = one_only ? ".gnu.linkonce.ld." : ".ldata.";
2076           break;
2077         case SECCAT_BSS:
2078           prefix = one_only ? ".gnu.linkonce.lb." : ".lbss.";
2079           break;
2080         case SECCAT_RODATA:
2081         case SECCAT_RODATA_MERGE_STR:
2082         case SECCAT_RODATA_MERGE_STR_INIT:
2083         case SECCAT_RODATA_MERGE_CONST:
2084           prefix = one_only ? ".gnu.linkonce.lr." : ".lrodata.";
2085           break;
2086         case SECCAT_SRODATA:
2087         case SECCAT_SDATA:
2088         case SECCAT_SBSS:
2089           gcc_unreachable ();
2090         case SECCAT_TEXT:
2091         case SECCAT_TDATA:
2092         case SECCAT_TBSS:
2093           /* We don't split these for medium model.  Place them into
2094              default sections and hope for best.  */
2095           break;
2096         }
2097       if (prefix)
2098         {
2099           const char *name;
2100           size_t nlen, plen;
2101           char *string;
2102           plen = strlen (prefix);
2103
2104           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2105           name = targetm.strip_name_encoding (name);
2106           nlen = strlen (name);
2107
2108           string = alloca (nlen + plen + 1);
2109           memcpy (string, prefix, plen);
2110           memcpy (string + plen, name, nlen + 1);
2111
2112           DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
2113           return;
2114         }
2115     }
2116   default_unique_section (decl, reloc);
2117 }
2118
2119 #ifdef COMMON_ASM_OP
2120 /* This says how to output assembler code to declare an
2121    uninitialized external linkage data object.
2122
2123    For medium model x86-64 we need to use .largecomm opcode for
2124    large objects.  */
2125 void
2126 x86_elf_aligned_common (FILE *file,
2127                         const char *name, unsigned HOST_WIDE_INT size,
2128                         int align)
2129 {
2130   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2131       && size > (unsigned int)ix86_section_threshold)
2132     fprintf (file, ".largecomm\t");
2133   else
2134     fprintf (file, "%s", COMMON_ASM_OP);
2135   assemble_name (file, name);
2136   fprintf (file, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2137            size, align / BITS_PER_UNIT);
2138 }
2139
2140 /* Utility function for targets to use in implementing
2141    ASM_OUTPUT_ALIGNED_BSS.  */
2142
2143 void
2144 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
2145                         const char *name, unsigned HOST_WIDE_INT size,
2146                         int align)
2147 {
2148   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2149       && size > (unsigned int)ix86_section_threshold)
2150     switch_to_section (get_named_section (decl, ".lbss", 0));
2151   else
2152     switch_to_section (bss_section);
2153   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2154 #ifdef ASM_DECLARE_OBJECT_NAME
2155   last_assemble_variable_decl = decl;
2156   ASM_DECLARE_OBJECT_NAME (file, name, decl);
2157 #else
2158   /* Standard thing is just output label for the object.  */
2159   ASM_OUTPUT_LABEL (file, name);
2160 #endif /* ASM_DECLARE_OBJECT_NAME */
2161   ASM_OUTPUT_SKIP (file, size ? size : 1);
2162 }
2163 #endif
2164 \f
2165 void
2166 optimization_options (int level, int size ATTRIBUTE_UNUSED)
2167 {
2168   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
2169      make the problem with not enough registers even worse.  */
2170 #ifdef INSN_SCHEDULING
2171   if (level > 1)
2172     flag_schedule_insns = 0;
2173 #endif
2174
2175   if (TARGET_MACHO)
2176     /* The Darwin libraries never set errno, so we might as well
2177        avoid calling them when that's the only reason we would.  */
2178     flag_errno_math = 0;
2179
2180   /* The default values of these switches depend on the TARGET_64BIT
2181      that is not known at this moment.  Mark these values with 2 and
2182      let user the to override these.  In case there is no command line option
2183      specifying them, we will set the defaults in override_options.  */
2184   if (optimize >= 1)
2185     flag_omit_frame_pointer = 2;
2186   flag_pcc_struct_return = 2;
2187   flag_asynchronous_unwind_tables = 2;
2188 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
2189   SUBTARGET_OPTIMIZATION_OPTIONS;
2190 #endif
2191 }
2192 \f
2193 /* Table of valid machine attributes.  */
2194 const struct attribute_spec ix86_attribute_table[] =
2195 {
2196   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2197   /* Stdcall attribute says callee is responsible for popping arguments
2198      if they are not variable.  */
2199   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
2200   /* Fastcall attribute says callee is responsible for popping arguments
2201      if they are not variable.  */
2202   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
2203   /* Cdecl attribute says the callee is a normal C declaration */
2204   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
2205   /* Regparm attribute specifies how many integer arguments are to be
2206      passed in registers.  */
2207   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
2208   /* Sseregparm attribute says we are using x86_64 calling conventions
2209      for FP arguments.  */
2210   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
2211 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2212   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
2213   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
2214   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
2215 #endif
2216   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
2217   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
2218 #ifdef SUBTARGET_ATTRIBUTE_TABLE
2219   SUBTARGET_ATTRIBUTE_TABLE,
2220 #endif
2221   { NULL,        0, 0, false, false, false, NULL }
2222 };
2223
2224 /* Decide whether we can make a sibling call to a function.  DECL is the
2225    declaration of the function being targeted by the call and EXP is the
2226    CALL_EXPR representing the call.  */
2227
2228 static bool
2229 ix86_function_ok_for_sibcall (tree decl, tree exp)
2230 {
2231   tree func;
2232   rtx a, b;
2233
2234   /* If we are generating position-independent code, we cannot sibcall
2235      optimize any indirect call, or a direct call to a global function,
2236      as the PLT requires %ebx be live.  */
2237   if (!TARGET_64BIT && flag_pic && (!decl || !targetm.binds_local_p (decl)))
2238     return false;
2239
2240   if (decl)
2241     func = decl;
2242   else
2243     {
2244       func = TREE_TYPE (TREE_OPERAND (exp, 0));
2245       if (POINTER_TYPE_P (func))
2246         func = TREE_TYPE (func);
2247     }
2248
2249   /* Check that the return value locations are the same.  Like
2250      if we are returning floats on the 80387 register stack, we cannot
2251      make a sibcall from a function that doesn't return a float to a
2252      function that does or, conversely, from a function that does return
2253      a float to a function that doesn't; the necessary stack adjustment
2254      would not be executed.  This is also the place we notice
2255      differences in the return value ABI.  Note that it is ok for one
2256      of the functions to have void return type as long as the return
2257      value of the other is passed in a register.  */
2258   a = ix86_function_value (TREE_TYPE (exp), func, false);
2259   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
2260                            cfun->decl, false);
2261   if (STACK_REG_P (a) || STACK_REG_P (b))
2262     {
2263       if (!rtx_equal_p (a, b))
2264         return false;
2265     }
2266   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
2267     ;
2268   else if (!rtx_equal_p (a, b))
2269     return false;
2270
2271   /* If this call is indirect, we'll need to be able to use a call-clobbered
2272      register for the address of the target function.  Make sure that all
2273      such registers are not used for passing parameters.  */
2274   if (!decl && !TARGET_64BIT)
2275     {
2276       tree type;
2277
2278       /* We're looking at the CALL_EXPR, we need the type of the function.  */
2279       type = TREE_OPERAND (exp, 0);             /* pointer expression */
2280       type = TREE_TYPE (type);                  /* pointer type */
2281       type = TREE_TYPE (type);                  /* function type */
2282
2283       if (ix86_function_regparm (type, NULL) >= 3)
2284         {
2285           /* ??? Need to count the actual number of registers to be used,
2286              not the possible number of registers.  Fix later.  */
2287           return false;
2288         }
2289     }
2290
2291 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2292   /* Dllimport'd functions are also called indirectly.  */
2293   if (decl && DECL_DLLIMPORT_P (decl)
2294       && ix86_function_regparm (TREE_TYPE (decl), NULL) >= 3)
2295     return false;
2296 #endif
2297
2298   /* If we forced aligned the stack, then sibcalling would unalign the
2299      stack, which may break the called function.  */
2300   if (cfun->machine->force_align_arg_pointer)
2301     return false;
2302
2303   /* Otherwise okay.  That also includes certain types of indirect calls.  */
2304   return true;
2305 }
2306
2307 /* Handle "cdecl", "stdcall", "fastcall", "regparm" and "sseregparm"
2308    calling convention attributes;
2309    arguments as in struct attribute_spec.handler.  */
2310
2311 static tree
2312 ix86_handle_cconv_attribute (tree *node, tree name,
2313                                    tree args,
2314                                    int flags ATTRIBUTE_UNUSED,
2315                                    bool *no_add_attrs)
2316 {
2317   if (TREE_CODE (*node) != FUNCTION_TYPE
2318       && TREE_CODE (*node) != METHOD_TYPE
2319       && TREE_CODE (*node) != FIELD_DECL
2320       && TREE_CODE (*node) != TYPE_DECL)
2321     {
2322       warning (OPT_Wattributes, "%qs attribute only applies to functions",
2323                IDENTIFIER_POINTER (name));
2324       *no_add_attrs = true;
2325       return NULL_TREE;
2326     }
2327
2328   /* Can combine regparm with all attributes but fastcall.  */
2329   if (is_attribute_p ("regparm", name))
2330     {
2331       tree cst;
2332
2333       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2334         {
2335           error ("fastcall and regparm attributes are not compatible");
2336         }
2337
2338       cst = TREE_VALUE (args);
2339       if (TREE_CODE (cst) != INTEGER_CST)
2340         {
2341           warning (OPT_Wattributes,
2342                    "%qs attribute requires an integer constant argument",
2343                    IDENTIFIER_POINTER (name));
2344           *no_add_attrs = true;
2345         }
2346       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
2347         {
2348           warning (OPT_Wattributes, "argument to %qs attribute larger than %d",
2349                    IDENTIFIER_POINTER (name), REGPARM_MAX);
2350           *no_add_attrs = true;
2351         }
2352
2353       return NULL_TREE;
2354     }
2355
2356   if (TARGET_64BIT)
2357     {
2358       warning (OPT_Wattributes, "%qs attribute ignored",
2359                IDENTIFIER_POINTER (name));
2360       *no_add_attrs = true;
2361       return NULL_TREE;
2362     }
2363
2364   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
2365   if (is_attribute_p ("fastcall", name))
2366     {
2367       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2368         {
2369           error ("fastcall and cdecl attributes are not compatible");
2370         }
2371       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2372         {
2373           error ("fastcall and stdcall attributes are not compatible");
2374         }
2375       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
2376         {
2377           error ("fastcall and regparm attributes are not compatible");
2378         }
2379     }
2380
2381   /* Can combine stdcall with fastcall (redundant), regparm and
2382      sseregparm.  */
2383   else if (is_attribute_p ("stdcall", name))
2384     {
2385       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2386         {
2387           error ("stdcall and cdecl attributes are not compatible");
2388         }
2389       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2390         {
2391           error ("stdcall and fastcall attributes are not compatible");
2392         }
2393     }
2394
2395   /* Can combine cdecl with regparm and sseregparm.  */
2396   else if (is_attribute_p ("cdecl", name))
2397     {
2398       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2399         {
2400           error ("stdcall and cdecl attributes are not compatible");
2401         }
2402       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2403         {
2404           error ("fastcall and cdecl attributes are not compatible");
2405         }
2406     }
2407
2408   /* Can combine sseregparm with all attributes.  */
2409
2410   return NULL_TREE;
2411 }
2412
2413 /* Return 0 if the attributes for two types are incompatible, 1 if they
2414    are compatible, and 2 if they are nearly compatible (which causes a
2415    warning to be generated).  */
2416
2417 static int
2418 ix86_comp_type_attributes (tree type1, tree type2)
2419 {
2420   /* Check for mismatch of non-default calling convention.  */
2421   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
2422
2423   if (TREE_CODE (type1) != FUNCTION_TYPE)
2424     return 1;
2425
2426   /* Check for mismatched fastcall/regparm types.  */
2427   if ((!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
2428        != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
2429       || (ix86_function_regparm (type1, NULL)
2430           != ix86_function_regparm (type2, NULL)))
2431     return 0;
2432
2433   /* Check for mismatched sseregparm types.  */
2434   if (!lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type1))
2435       != !lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type2)))
2436     return 0;
2437
2438   /* Check for mismatched return types (cdecl vs stdcall).  */
2439   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
2440       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
2441     return 0;
2442
2443   return 1;
2444 }
2445 \f
2446 /* Return the regparm value for a function with the indicated TYPE and DECL.
2447    DECL may be NULL when calling function indirectly
2448    or considering a libcall.  */
2449
2450 static int
2451 ix86_function_regparm (tree type, tree decl)
2452 {
2453   tree attr;
2454   int regparm = ix86_regparm;
2455   bool user_convention = false;
2456
2457   if (!TARGET_64BIT)
2458     {
2459       attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
2460       if (attr)
2461         {
2462           regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
2463           user_convention = true;
2464         }
2465
2466       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
2467         {
2468           regparm = 2;
2469           user_convention = true;
2470         }
2471
2472       /* Use register calling convention for local functions when possible.  */
2473       if (!TARGET_64BIT && !user_convention && decl
2474           && flag_unit_at_a_time && !profile_flag)
2475         {
2476           struct cgraph_local_info *i = cgraph_local_info (decl);
2477           if (i && i->local)
2478             {
2479               int local_regparm, globals = 0, regno;
2480
2481               /* Make sure no regparm register is taken by a global register
2482                  variable.  */
2483               for (local_regparm = 0; local_regparm < 3; local_regparm++)
2484                 if (global_regs[local_regparm])
2485                   break;
2486               /* We can't use regparm(3) for nested functions as these use
2487                  static chain pointer in third argument.  */
2488               if (local_regparm == 3
2489                   && decl_function_context (decl)
2490                   && !DECL_NO_STATIC_CHAIN (decl))
2491                 local_regparm = 2;
2492               /* Each global register variable increases register preassure,
2493                  so the more global reg vars there are, the smaller regparm
2494                  optimization use, unless requested by the user explicitly.  */
2495               for (regno = 0; regno < 6; regno++)
2496                 if (global_regs[regno])
2497                   globals++;
2498               local_regparm
2499                 = globals < local_regparm ? local_regparm - globals : 0;
2500
2501               if (local_regparm > regparm)
2502                 regparm = local_regparm;
2503             }
2504         }
2505     }
2506   return regparm;
2507 }
2508
2509 /* Return 1 or 2, if we can pass up to 8 SFmode (1) and DFmode (2) arguments
2510    in SSE registers for a function with the indicated TYPE and DECL.
2511    DECL may be NULL when calling function indirectly
2512    or considering a libcall.  Otherwise return 0.  */
2513
2514 static int
2515 ix86_function_sseregparm (tree type, tree decl)
2516 {
2517   /* Use SSE registers to pass SFmode and DFmode arguments if requested
2518      by the sseregparm attribute.  */
2519   if (TARGET_SSEREGPARM
2520       || (type
2521           && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
2522     {
2523       if (!TARGET_SSE)
2524         {
2525           if (decl)
2526             error ("Calling %qD with attribute sseregparm without "
2527                    "SSE/SSE2 enabled", decl);
2528           else
2529             error ("Calling %qT with attribute sseregparm without "
2530                    "SSE/SSE2 enabled", type);
2531           return 0;
2532         }
2533
2534       return 2;
2535     }
2536
2537   /* For local functions, pass SFmode (and DFmode for SSE2) arguments
2538      in SSE registers even for 32-bit mode and not just 3, but up to
2539      8 SSE arguments in registers.  */
2540   if (!TARGET_64BIT && decl
2541       && TARGET_SSE_MATH && flag_unit_at_a_time && !profile_flag)
2542     {
2543       struct cgraph_local_info *i = cgraph_local_info (decl);
2544       if (i && i->local)
2545         return TARGET_SSE2 ? 2 : 1;
2546     }
2547
2548   return 0;
2549 }
2550
2551 /* Return true if EAX is live at the start of the function.  Used by
2552    ix86_expand_prologue to determine if we need special help before
2553    calling allocate_stack_worker.  */
2554
2555 static bool
2556 ix86_eax_live_at_start_p (void)
2557 {
2558   /* Cheat.  Don't bother working forward from ix86_function_regparm
2559      to the function type to whether an actual argument is located in
2560      eax.  Instead just look at cfg info, which is still close enough
2561      to correct at this point.  This gives false positives for broken
2562      functions that might use uninitialized data that happens to be
2563      allocated in eax, but who cares?  */
2564   return REGNO_REG_SET_P (ENTRY_BLOCK_PTR->il.rtl->global_live_at_end, 0);
2565 }
2566
2567 /* Value is the number of bytes of arguments automatically
2568    popped when returning from a subroutine call.
2569    FUNDECL is the declaration node of the function (as a tree),
2570    FUNTYPE is the data type of the function (as a tree),
2571    or for a library call it is an identifier node for the subroutine name.
2572    SIZE is the number of bytes of arguments passed on the stack.
2573
2574    On the 80386, the RTD insn may be used to pop them if the number
2575      of args is fixed, but if the number is variable then the caller
2576      must pop them all.  RTD can't be used for library calls now
2577      because the library is compiled with the Unix compiler.
2578    Use of RTD is a selectable option, since it is incompatible with
2579    standard Unix calling sequences.  If the option is not selected,
2580    the caller must always pop the args.
2581
2582    The attribute stdcall is equivalent to RTD on a per module basis.  */
2583
2584 int
2585 ix86_return_pops_args (tree fundecl, tree funtype, int size)
2586 {
2587   int rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
2588
2589   /* Cdecl functions override -mrtd, and never pop the stack.  */
2590   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype))) {
2591
2592     /* Stdcall and fastcall functions will pop the stack if not
2593        variable args.  */
2594     if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
2595         || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype)))
2596       rtd = 1;
2597
2598     if (rtd
2599         && (TYPE_ARG_TYPES (funtype) == NULL_TREE
2600             || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (funtype)))
2601                 == void_type_node)))
2602       return size;
2603   }
2604
2605   /* Lose any fake structure return argument if it is passed on the stack.  */
2606   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
2607       && !TARGET_64BIT
2608       && !KEEP_AGGREGATE_RETURN_POINTER)
2609     {
2610       int nregs = ix86_function_regparm (funtype, fundecl);
2611
2612       if (!nregs)
2613         return GET_MODE_SIZE (Pmode);
2614     }
2615
2616   return 0;
2617 }
2618 \f
2619 /* Argument support functions.  */
2620
2621 /* Return true when register may be used to pass function parameters.  */
2622 bool
2623 ix86_function_arg_regno_p (int regno)
2624 {
2625   int i;
2626   if (!TARGET_64BIT)
2627     return (regno < REGPARM_MAX
2628             || (TARGET_MMX && MMX_REGNO_P (regno)
2629                 && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
2630             || (TARGET_SSE && SSE_REGNO_P (regno)
2631                 && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
2632
2633   if (TARGET_SSE && SSE_REGNO_P (regno)
2634       && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
2635     return true;
2636   /* RAX is used as hidden argument to va_arg functions.  */
2637   if (!regno)
2638     return true;
2639   for (i = 0; i < REGPARM_MAX; i++)
2640     if (regno == x86_64_int_parameter_registers[i])
2641       return true;
2642   return false;
2643 }
2644
2645 /* Return if we do not know how to pass TYPE solely in registers.  */
2646
2647 static bool
2648 ix86_must_pass_in_stack (enum machine_mode mode, tree type)
2649 {
2650   if (must_pass_in_stack_var_size_or_pad (mode, type))
2651     return true;
2652
2653   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
2654      The layout_type routine is crafty and tries to trick us into passing
2655      currently unsupported vector types on the stack by using TImode.  */
2656   return (!TARGET_64BIT && mode == TImode
2657           && type && TREE_CODE (type) != VECTOR_TYPE);
2658 }
2659
2660 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2661    for a call to a function whose data type is FNTYPE.
2662    For a library call, FNTYPE is 0.  */
2663
2664 void
2665 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
2666                       tree fntype,      /* tree ptr for function decl */
2667                       rtx libname,      /* SYMBOL_REF of library name or 0 */
2668                       tree fndecl)
2669 {
2670   static CUMULATIVE_ARGS zero_cum;
2671   tree param, next_param;
2672
2673   if (TARGET_DEBUG_ARG)
2674     {
2675       fprintf (stderr, "\ninit_cumulative_args (");
2676       if (fntype)
2677         fprintf (stderr, "fntype code = %s, ret code = %s",
2678                  tree_code_name[(int) TREE_CODE (fntype)],
2679                  tree_code_name[(int) TREE_CODE (TREE_TYPE (fntype))]);
2680       else
2681         fprintf (stderr, "no fntype");
2682
2683       if (libname)
2684         fprintf (stderr, ", libname = %s", XSTR (libname, 0));
2685     }
2686
2687   *cum = zero_cum;
2688
2689   /* Set up the number of registers to use for passing arguments.  */
2690   cum->nregs = ix86_regparm;
2691   if (TARGET_SSE)
2692     cum->sse_nregs = SSE_REGPARM_MAX;
2693   if (TARGET_MMX)
2694     cum->mmx_nregs = MMX_REGPARM_MAX;
2695   cum->warn_sse = true;
2696   cum->warn_mmx = true;
2697   cum->maybe_vaarg = false;
2698
2699   /* Use ecx and edx registers if function has fastcall attribute,
2700      else look for regparm information.  */
2701   if (fntype && !TARGET_64BIT)
2702     {
2703       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
2704         {
2705           cum->nregs = 2;
2706           cum->fastcall = 1;
2707         }
2708       else
2709         cum->nregs = ix86_function_regparm (fntype, fndecl);
2710     }
2711
2712   /* Set up the number of SSE registers used for passing SFmode
2713      and DFmode arguments.  Warn for mismatching ABI.  */
2714   cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl);
2715
2716   /* Determine if this function has variable arguments.  This is
2717      indicated by the last argument being 'void_type_mode' if there
2718      are no variable arguments.  If there are variable arguments, then
2719      we won't pass anything in registers in 32-bit mode. */
2720
2721   if (cum->nregs || cum->mmx_nregs || cum->sse_nregs)
2722     {
2723       for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
2724            param != 0; param = next_param)
2725         {
2726           next_param = TREE_CHAIN (param);
2727           if (next_param == 0 && TREE_VALUE (param) != void_type_node)
2728             {
2729               if (!TARGET_64BIT)
2730                 {
2731                   cum->nregs = 0;
2732                   cum->sse_nregs = 0;
2733                   cum->mmx_nregs = 0;
2734                   cum->warn_sse = 0;
2735                   cum->warn_mmx = 0;
2736                   cum->fastcall = 0;
2737                   cum->float_in_sse = 0;
2738                 }
2739               cum->maybe_vaarg = true;
2740             }
2741         }
2742     }
2743   if ((!fntype && !libname)
2744       || (fntype && !TYPE_ARG_TYPES (fntype)))
2745     cum->maybe_vaarg = true;
2746
2747   if (TARGET_DEBUG_ARG)
2748     fprintf (stderr, ", nregs=%d )\n", cum->nregs);
2749
2750   return;
2751 }
2752
2753 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
2754    But in the case of vector types, it is some vector mode.
2755
2756    When we have only some of our vector isa extensions enabled, then there
2757    are some modes for which vector_mode_supported_p is false.  For these
2758    modes, the generic vector support in gcc will choose some non-vector mode
2759    in order to implement the type.  By computing the natural mode, we'll 
2760    select the proper ABI location for the operand and not depend on whatever
2761    the middle-end decides to do with these vector types.  */
2762
2763 static enum machine_mode
2764 type_natural_mode (tree type)
2765 {
2766   enum machine_mode mode = TYPE_MODE (type);
2767
2768   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
2769     {
2770       HOST_WIDE_INT size = int_size_in_bytes (type);
2771       if ((size == 8 || size == 16)
2772           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
2773           && TYPE_VECTOR_SUBPARTS (type) > 1)
2774         {
2775           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
2776
2777           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
2778             mode = MIN_MODE_VECTOR_FLOAT;
2779           else
2780             mode = MIN_MODE_VECTOR_INT;
2781
2782           /* Get the mode which has this inner mode and number of units.  */
2783           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2784             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
2785                 && GET_MODE_INNER (mode) == innermode)
2786               return mode;
2787
2788           gcc_unreachable ();
2789         }
2790     }
2791
2792   return mode;
2793 }
2794
2795 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
2796    this may not agree with the mode that the type system has chosen for the
2797    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
2798    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
2799
2800 static rtx
2801 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
2802                      unsigned int regno)
2803 {
2804   rtx tmp;
2805
2806   if (orig_mode != BLKmode)
2807     tmp = gen_rtx_REG (orig_mode, regno);
2808   else
2809     {
2810       tmp = gen_rtx_REG (mode, regno);
2811       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
2812       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
2813     }
2814
2815   return tmp;
2816 }
2817
2818 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
2819    of this code is to classify each 8bytes of incoming argument by the register
2820    class and assign registers accordingly.  */
2821
2822 /* Return the union class of CLASS1 and CLASS2.
2823    See the x86-64 PS ABI for details.  */
2824
2825 static enum x86_64_reg_class
2826 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
2827 {
2828   /* Rule #1: If both classes are equal, this is the resulting class.  */
2829   if (class1 == class2)
2830     return class1;
2831
2832   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
2833      the other class.  */
2834   if (class1 == X86_64_NO_CLASS)
2835     return class2;
2836   if (class2 == X86_64_NO_CLASS)
2837     return class1;
2838
2839   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
2840   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
2841     return X86_64_MEMORY_CLASS;
2842
2843   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
2844   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
2845       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
2846     return X86_64_INTEGERSI_CLASS;
2847   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
2848       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
2849     return X86_64_INTEGER_CLASS;
2850
2851   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
2852      MEMORY is used.  */
2853   if (class1 == X86_64_X87_CLASS
2854       || class1 == X86_64_X87UP_CLASS
2855       || class1 == X86_64_COMPLEX_X87_CLASS
2856       || class2 == X86_64_X87_CLASS
2857       || class2 == X86_64_X87UP_CLASS
2858       || class2 == X86_64_COMPLEX_X87_CLASS)
2859     return X86_64_MEMORY_CLASS;
2860
2861   /* Rule #6: Otherwise class SSE is used.  */
2862   return X86_64_SSE_CLASS;
2863 }
2864
2865 /* Classify the argument of type TYPE and mode MODE.
2866    CLASSES will be filled by the register class used to pass each word
2867    of the operand.  The number of words is returned.  In case the parameter
2868    should be passed in memory, 0 is returned. As a special case for zero
2869    sized containers, classes[0] will be NO_CLASS and 1 is returned.
2870
2871    BIT_OFFSET is used internally for handling records and specifies offset
2872    of the offset in bits modulo 256 to avoid overflow cases.
2873
2874    See the x86-64 PS ABI for details.
2875 */
2876
2877 static int
2878 classify_argument (enum machine_mode mode, tree type,
2879                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
2880 {
2881   HOST_WIDE_INT bytes =
2882     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2883   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2884
2885   /* Variable sized entities are always passed/returned in memory.  */
2886   if (bytes < 0)
2887     return 0;
2888
2889   if (mode != VOIDmode
2890       && targetm.calls.must_pass_in_stack (mode, type))
2891     return 0;
2892
2893   if (type && AGGREGATE_TYPE_P (type))
2894     {
2895       int i;
2896       tree field;
2897       enum x86_64_reg_class subclasses[MAX_CLASSES];
2898
2899       /* On x86-64 we pass structures larger than 16 bytes on the stack.  */
2900       if (bytes > 16)
2901         return 0;
2902
2903       for (i = 0; i < words; i++)
2904         classes[i] = X86_64_NO_CLASS;
2905
2906       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
2907          signalize memory class, so handle it as special case.  */
2908       if (!words)
2909         {
2910           classes[0] = X86_64_NO_CLASS;
2911           return 1;
2912         }
2913
2914       /* Classify each field of record and merge classes.  */
2915       switch (TREE_CODE (type))
2916         {
2917         case RECORD_TYPE:
2918           /* For classes first merge in the field of the subclasses.  */
2919           if (TYPE_BINFO (type))
2920             {
2921               tree binfo, base_binfo;
2922               int basenum;
2923
2924               for (binfo = TYPE_BINFO (type), basenum = 0;
2925                    BINFO_BASE_ITERATE (binfo, basenum, base_binfo); basenum++)
2926                 {
2927                    int num;
2928                    int offset = tree_low_cst (BINFO_OFFSET (base_binfo), 0) * 8;
2929                    tree type = BINFO_TYPE (base_binfo);
2930
2931                    num = classify_argument (TYPE_MODE (type),
2932                                             type, subclasses,
2933                                             (offset + bit_offset) % 256);
2934                    if (!num)
2935                      return 0;
2936                    for (i = 0; i < num; i++)
2937                      {
2938                        int pos = (offset + (bit_offset % 64)) / 8 / 8;
2939                        classes[i + pos] =
2940                          merge_classes (subclasses[i], classes[i + pos]);
2941                      }
2942                 }
2943             }
2944           /* And now merge the fields of structure.  */
2945           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2946             {
2947               if (TREE_CODE (field) == FIELD_DECL)
2948                 {
2949                   int num;
2950
2951                   /* Bitfields are always classified as integer.  Handle them
2952                      early, since later code would consider them to be
2953                      misaligned integers.  */
2954                   if (DECL_BIT_FIELD (field))
2955                     {
2956                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
2957                            i < ((int_bit_position (field) + (bit_offset % 64))
2958                                 + tree_low_cst (DECL_SIZE (field), 0)
2959                                 + 63) / 8 / 8; i++)
2960                         classes[i] =
2961                           merge_classes (X86_64_INTEGER_CLASS,
2962                                          classes[i]);
2963                     }
2964                   else
2965                     {
2966                       num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
2967                                                TREE_TYPE (field), subclasses,
2968                                                (int_bit_position (field)
2969                                                 + bit_offset) % 256);
2970                       if (!num)
2971                         return 0;
2972                       for (i = 0; i < num; i++)
2973                         {
2974                           int pos =
2975                             (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
2976                           classes[i + pos] =
2977                             merge_classes (subclasses[i], classes[i + pos]);
2978                         }
2979                     }
2980                 }
2981             }
2982           break;
2983
2984         case ARRAY_TYPE:
2985           /* Arrays are handled as small records.  */
2986           {
2987             int num;
2988             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
2989                                      TREE_TYPE (type), subclasses, bit_offset);
2990             if (!num)
2991               return 0;
2992
2993             /* The partial classes are now full classes.  */
2994             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
2995               subclasses[0] = X86_64_SSE_CLASS;
2996             if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
2997               subclasses[0] = X86_64_INTEGER_CLASS;
2998             
2999             for (i = 0; i < words; i++)
3000               classes[i] = subclasses[i % num];
3001             
3002             break;
3003           }
3004         case UNION_TYPE:
3005         case QUAL_UNION_TYPE:
3006           /* Unions are similar to RECORD_TYPE but offset is always 0.
3007              */
3008
3009           /* Unions are not derived.  */
3010           gcc_assert (!TYPE_BINFO (type)
3011                       || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
3012           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3013             {
3014               if (TREE_CODE (field) == FIELD_DECL)
3015                 {
3016                   int num;
3017                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3018                                            TREE_TYPE (field), subclasses,
3019                                            bit_offset);
3020                   if (!num)
3021                     return 0;
3022                   for (i = 0; i < num; i++)
3023                     classes[i] = merge_classes (subclasses[i], classes[i]);
3024                 }
3025             }
3026           break;
3027
3028         default:
3029           gcc_unreachable ();
3030         }
3031
3032       /* Final merger cleanup.  */
3033       for (i = 0; i < words; i++)
3034         {
3035           /* If one class is MEMORY, everything should be passed in
3036              memory.  */
3037           if (classes[i] == X86_64_MEMORY_CLASS)
3038             return 0;
3039
3040           /* The X86_64_SSEUP_CLASS should be always preceded by
3041              X86_64_SSE_CLASS.  */
3042           if (classes[i] == X86_64_SSEUP_CLASS
3043               && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
3044             classes[i] = X86_64_SSE_CLASS;
3045
3046           /*  X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS.  */
3047           if (classes[i] == X86_64_X87UP_CLASS
3048               && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
3049             classes[i] = X86_64_SSE_CLASS;
3050         }
3051       return words;
3052     }
3053
3054   /* Compute alignment needed.  We align all types to natural boundaries with
3055      exception of XFmode that is aligned to 64bits.  */
3056   if (mode != VOIDmode && mode != BLKmode)
3057     {
3058       int mode_alignment = GET_MODE_BITSIZE (mode);
3059
3060       if (mode == XFmode)
3061         mode_alignment = 128;
3062       else if (mode == XCmode)
3063         mode_alignment = 256;
3064       if (COMPLEX_MODE_P (mode))
3065         mode_alignment /= 2;
3066       /* Misaligned fields are always returned in memory.  */
3067       if (bit_offset % mode_alignment)
3068         return 0;
3069     }
3070
3071   /* for V1xx modes, just use the base mode */
3072   if (VECTOR_MODE_P (mode)
3073       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
3074     mode = GET_MODE_INNER (mode);
3075
3076   /* Classification of atomic types.  */
3077   switch (mode)
3078     {
3079     case SDmode:
3080     case DDmode:
3081       classes[0] = X86_64_SSE_CLASS;
3082       return 1;
3083     case TDmode:
3084       classes[0] = X86_64_SSE_CLASS;
3085       classes[1] = X86_64_SSEUP_CLASS;
3086       return 2;
3087     case DImode:
3088     case SImode:
3089     case HImode:
3090     case QImode:
3091     case CSImode:
3092     case CHImode:
3093     case CQImode:
3094       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3095         classes[0] = X86_64_INTEGERSI_CLASS;
3096       else
3097         classes[0] = X86_64_INTEGER_CLASS;
3098       return 1;
3099     case CDImode:
3100     case TImode:
3101       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
3102       return 2;
3103     case CTImode:
3104       return 0;
3105     case SFmode:
3106       if (!(bit_offset % 64))
3107         classes[0] = X86_64_SSESF_CLASS;
3108       else
3109         classes[0] = X86_64_SSE_CLASS;
3110       return 1;
3111     case DFmode:
3112       classes[0] = X86_64_SSEDF_CLASS;
3113       return 1;
3114     case XFmode:
3115       classes[0] = X86_64_X87_CLASS;
3116       classes[1] = X86_64_X87UP_CLASS;
3117       return 2;
3118     case TFmode:
3119       classes[0] = X86_64_SSE_CLASS;
3120       classes[1] = X86_64_SSEUP_CLASS;
3121       return 2;
3122     case SCmode:
3123       classes[0] = X86_64_SSE_CLASS;
3124       return 1;
3125     case DCmode:
3126       classes[0] = X86_64_SSEDF_CLASS;
3127       classes[1] = X86_64_SSEDF_CLASS;
3128       return 2;
3129     case XCmode:
3130       classes[0] = X86_64_COMPLEX_X87_CLASS;
3131       return 1;
3132     case TCmode:
3133       /* This modes is larger than 16 bytes.  */
3134       return 0;
3135     case V4SFmode:
3136     case V4SImode:
3137     case V16QImode:
3138     case V8HImode:
3139     case V2DFmode:
3140     case V2DImode:
3141       classes[0] = X86_64_SSE_CLASS;
3142       classes[1] = X86_64_SSEUP_CLASS;
3143       return 2;
3144     case V2SFmode:
3145     case V2SImode:
3146     case V4HImode:
3147     case V8QImode:
3148       classes[0] = X86_64_SSE_CLASS;
3149       return 1;
3150     case BLKmode:
3151     case VOIDmode:
3152       return 0;
3153     default:
3154       gcc_assert (VECTOR_MODE_P (mode));
3155       
3156       if (bytes > 16)
3157         return 0;
3158       
3159       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
3160       
3161       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3162         classes[0] = X86_64_INTEGERSI_CLASS;
3163       else
3164         classes[0] = X86_64_INTEGER_CLASS;
3165       classes[1] = X86_64_INTEGER_CLASS;
3166       return 1 + (bytes > 8);
3167     }
3168 }
3169
3170 /* Examine the argument and return set number of register required in each
3171    class.  Return 0 iff parameter should be passed in memory.  */
3172 static int
3173 examine_argument (enum machine_mode mode, tree type, int in_return,
3174                   int *int_nregs, int *sse_nregs)
3175 {
3176   enum x86_64_reg_class class[MAX_CLASSES];
3177   int n = classify_argument (mode, type, class, 0);
3178
3179   *int_nregs = 0;
3180   *sse_nregs = 0;
3181   if (!n)
3182     return 0;
3183   for (n--; n >= 0; n--)
3184     switch (class[n])
3185       {
3186       case X86_64_INTEGER_CLASS:
3187       case X86_64_INTEGERSI_CLASS:
3188         (*int_nregs)++;
3189         break;
3190       case X86_64_SSE_CLASS:
3191       case X86_64_SSESF_CLASS:
3192       case X86_64_SSEDF_CLASS:
3193         (*sse_nregs)++;
3194         break;
3195       case X86_64_NO_CLASS:
3196       case X86_64_SSEUP_CLASS:
3197         break;
3198       case X86_64_X87_CLASS:
3199       case X86_64_X87UP_CLASS:
3200         if (!in_return)
3201           return 0;
3202         break;
3203       case X86_64_COMPLEX_X87_CLASS:
3204         return in_return ? 2 : 0;
3205       case X86_64_MEMORY_CLASS:
3206         gcc_unreachable ();
3207       }
3208   return 1;
3209 }
3210
3211 /* Construct container for the argument used by GCC interface.  See
3212    FUNCTION_ARG for the detailed description.  */
3213
3214 static rtx
3215 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
3216                      tree type, int in_return, int nintregs, int nsseregs,
3217                      const int *intreg, int sse_regno)
3218 {
3219   enum machine_mode tmpmode;
3220   int bytes =
3221     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3222   enum x86_64_reg_class class[MAX_CLASSES];
3223   int n;
3224   int i;
3225   int nexps = 0;
3226   int needed_sseregs, needed_intregs;
3227   rtx exp[MAX_CLASSES];
3228   rtx ret;
3229
3230   n = classify_argument (mode, type, class, 0);
3231   if (TARGET_DEBUG_ARG)
3232     {
3233       if (!n)
3234         fprintf (stderr, "Memory class\n");
3235       else
3236         {
3237           fprintf (stderr, "Classes:");
3238           for (i = 0; i < n; i++)
3239             {
3240               fprintf (stderr, " %s", x86_64_reg_class_name[class[i]]);
3241             }
3242            fprintf (stderr, "\n");
3243         }
3244     }
3245   if (!n)
3246     return NULL;
3247   if (!examine_argument (mode, type, in_return, &needed_intregs,
3248                          &needed_sseregs))
3249     return NULL;
3250   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
3251     return NULL;
3252
3253   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
3254      some less clueful developer tries to use floating-point anyway.  */
3255   if (needed_sseregs && !TARGET_SSE)
3256     {
3257       static bool issued_error;
3258       if (!issued_error)
3259         {
3260           issued_error = true;
3261           if (in_return)
3262             error ("SSE register return with SSE disabled");
3263           else
3264             error ("SSE register argument with SSE disabled");
3265         }
3266       return NULL;
3267     }
3268
3269   /* First construct simple cases.  Avoid SCmode, since we want to use
3270      single register to pass this type.  */
3271   if (n == 1 && mode != SCmode)
3272     switch (class[0])
3273       {
3274       case X86_64_INTEGER_CLASS:
3275       case X86_64_INTEGERSI_CLASS:
3276         return gen_rtx_REG (mode, intreg[0]);
3277       case X86_64_SSE_CLASS:
3278       case X86_64_SSESF_CLASS:
3279       case X86_64_SSEDF_CLASS:
3280         return gen_reg_or_parallel (mode, orig_mode, SSE_REGNO (sse_regno));
3281       case X86_64_X87_CLASS:
3282       case X86_64_COMPLEX_X87_CLASS:
3283         return gen_rtx_REG (mode, FIRST_STACK_REG);
3284       case X86_64_NO_CLASS:
3285         /* Zero sized array, struct or class.  */
3286         return NULL;
3287       default:
3288         gcc_unreachable ();
3289       }
3290   if (n == 2 && class[0] == X86_64_SSE_CLASS && class[1] == X86_64_SSEUP_CLASS
3291       && mode != BLKmode)
3292     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
3293   if (n == 2
3294       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS)
3295     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
3296   if (n == 2 && class[0] == X86_64_INTEGER_CLASS
3297       && class[1] == X86_64_INTEGER_CLASS
3298       && (mode == CDImode || mode == TImode || mode == TFmode)
3299       && intreg[0] + 1 == intreg[1])
3300     return gen_rtx_REG (mode, intreg[0]);
3301
3302   /* Otherwise figure out the entries of the PARALLEL.  */
3303   for (i = 0; i < n; i++)
3304     {
3305       switch (class[i])
3306         {
3307           case X86_64_NO_CLASS:
3308             break;
3309           case X86_64_INTEGER_CLASS:
3310           case X86_64_INTEGERSI_CLASS:
3311             /* Merge TImodes on aligned occasions here too.  */
3312             if (i * 8 + 8 > bytes)
3313               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
3314             else if (class[i] == X86_64_INTEGERSI_CLASS)
3315               tmpmode = SImode;
3316             else
3317               tmpmode = DImode;
3318             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
3319             if (tmpmode == BLKmode)
3320               tmpmode = DImode;
3321             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3322                                                gen_rtx_REG (tmpmode, *intreg),
3323                                                GEN_INT (i*8));
3324             intreg++;
3325             break;
3326           case X86_64_SSESF_CLASS:
3327             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3328                                                gen_rtx_REG (SFmode,
3329                                                             SSE_REGNO (sse_regno)),
3330                                                GEN_INT (i*8));
3331             sse_regno++;
3332             break;
3333           case X86_64_SSEDF_CLASS:
3334             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3335                                                gen_rtx_REG (DFmode,
3336                                                             SSE_REGNO (sse_regno)),
3337                                                GEN_INT (i*8));
3338             sse_regno++;
3339             break;
3340           case X86_64_SSE_CLASS:
3341             if (i < n - 1 && class[i + 1] == X86_64_SSEUP_CLASS)
3342               tmpmode = TImode;
3343             else
3344               tmpmode = DImode;
3345             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3346                                                gen_rtx_REG (tmpmode,
3347                                                             SSE_REGNO (sse_regno)),
3348                                                GEN_INT (i*8));
3349             if (tmpmode == TImode)
3350               i++;
3351             sse_regno++;
3352             break;
3353           default:
3354             gcc_unreachable ();
3355         }
3356     }
3357
3358   /* Empty aligned struct, union or class.  */
3359   if (nexps == 0)
3360     return NULL;
3361
3362   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
3363   for (i = 0; i < nexps; i++)
3364     XVECEXP (ret, 0, i) = exp [i];
3365   return ret;
3366 }
3367
3368 /* Update the data in CUM to advance over an argument
3369    of mode MODE and data type TYPE.
3370    (TYPE is null for libcalls where that information may not be available.)  */
3371
3372 void
3373 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3374                       tree type, int named)
3375 {
3376   int bytes =
3377     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3378   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3379
3380   if (type)
3381     mode = type_natural_mode (type);
3382
3383   if (TARGET_DEBUG_ARG)
3384     fprintf (stderr, "function_adv (sz=%d, wds=%2d, nregs=%d, ssenregs=%d, "
3385              "mode=%s, named=%d)\n\n",
3386              words, cum->words, cum->nregs, cum->sse_nregs,
3387              GET_MODE_NAME (mode), named);
3388
3389   if (TARGET_64BIT)
3390     {
3391       int int_nregs, sse_nregs;
3392       if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
3393         cum->words += words;
3394       else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
3395         {
3396           cum->nregs -= int_nregs;
3397           cum->sse_nregs -= sse_nregs;
3398           cum->regno += int_nregs;
3399           cum->sse_regno += sse_nregs;
3400         }
3401       else
3402         cum->words += words;
3403     }
3404   else
3405     {
3406       switch (mode)
3407         {
3408         default:
3409           break;
3410
3411         case BLKmode:
3412           if (bytes < 0)
3413             break;
3414           /* FALLTHRU */
3415
3416         case DImode:
3417         case SImode:
3418         case HImode:
3419         case QImode:
3420           cum->words += words;
3421           cum->nregs -= words;
3422           cum->regno += words;
3423
3424           if (cum->nregs <= 0)
3425             {
3426               cum->nregs = 0;
3427               cum->regno = 0;
3428             }
3429           break;
3430
3431         case DFmode:
3432           if (cum->float_in_sse < 2)
3433             break;
3434         case SFmode:
3435           if (cum->float_in_sse < 1)
3436             break;
3437           /* FALLTHRU */
3438
3439         case TImode:
3440         case V16QImode:
3441         case V8HImode:
3442         case V4SImode:
3443         case V2DImode:
3444         case V4SFmode:
3445         case V2DFmode:
3446           if (!type || !AGGREGATE_TYPE_P (type))
3447             {
3448               cum->sse_words += words;
3449               cum->sse_nregs -= 1;
3450               cum->sse_regno += 1;
3451               if (cum->sse_nregs <= 0)
3452                 {
3453                   cum->sse_nregs = 0;
3454                   cum->sse_regno = 0;
3455                 }
3456             }
3457           break;
3458
3459         case V8QImode:
3460         case V4HImode:
3461         case V2SImode:
3462         case V2SFmode:
3463           if (!type || !AGGREGATE_TYPE_P (type))
3464             {
3465               cum->mmx_words += words;
3466               cum->mmx_nregs -= 1;
3467               cum->mmx_regno += 1;
3468               if (cum->mmx_nregs <= 0)
3469                 {
3470                   cum->mmx_nregs = 0;
3471                   cum->mmx_regno = 0;
3472                 }
3473             }
3474           break;
3475         }
3476     }
3477 }
3478
3479 /* Define where to put the arguments to a function.
3480    Value is zero to push the argument on the stack,
3481    or a hard register in which to store the argument.
3482
3483    MODE is the argument's machine mode.
3484    TYPE is the data type of the argument (as a tree).
3485     This is null for libcalls where that information may
3486     not be available.
3487    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3488     the preceding args and about the function being called.
3489    NAMED is nonzero if this argument is a named parameter
3490     (otherwise it is an extra parameter matching an ellipsis).  */
3491
3492 rtx
3493 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode orig_mode,
3494               tree type, int named)
3495 {
3496   enum machine_mode mode = orig_mode;
3497   rtx ret = NULL_RTX;
3498   int bytes =
3499     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3500   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3501   static bool warnedsse, warnedmmx;
3502
3503   /* To simplify the code below, represent vector types with a vector mode
3504      even if MMX/SSE are not active.  */
3505   if (type && TREE_CODE (type) == VECTOR_TYPE)
3506     mode = type_natural_mode (type);
3507
3508   /* Handle a hidden AL argument containing number of registers for varargs
3509      x86-64 functions.  For i386 ABI just return constm1_rtx to avoid
3510      any AL settings.  */
3511   if (mode == VOIDmode)
3512     {
3513       if (TARGET_64BIT)
3514         return GEN_INT (cum->maybe_vaarg
3515                         ? (cum->sse_nregs < 0
3516                            ? SSE_REGPARM_MAX
3517                            : cum->sse_regno)
3518                         : -1);
3519       else
3520         return constm1_rtx;
3521     }
3522   if (TARGET_64BIT)
3523     ret = construct_container (mode, orig_mode, type, 0, cum->nregs,
3524                                cum->sse_nregs,
3525                                &x86_64_int_parameter_registers [cum->regno],
3526                                cum->sse_regno);
3527   else
3528     switch (mode)
3529       {
3530         /* For now, pass fp/complex values on the stack.  */
3531       default:
3532         break;
3533
3534       case BLKmode:
3535         if (bytes < 0)
3536           break;
3537         /* FALLTHRU */
3538       case DImode:
3539       case SImode:
3540       case HImode:
3541       case QImode:
3542         if (words <= cum->nregs)
3543           {
3544             int regno = cum->regno;
3545
3546             /* Fastcall allocates the first two DWORD (SImode) or
3547                smaller arguments to ECX and EDX.  */
3548             if (cum->fastcall)
3549               {
3550                 if (mode == BLKmode || mode == DImode)
3551                   break;
3552
3553                 /* ECX not EAX is the first allocated register.  */
3554                 if (regno == 0)
3555                   regno = 2;
3556               }
3557             ret = gen_rtx_REG (mode, regno);
3558           }
3559         break;
3560       case DFmode:
3561         if (cum->float_in_sse < 2)
3562           break;
3563       case SFmode:
3564         if (cum->float_in_sse < 1)
3565           break;
3566         /* FALLTHRU */
3567       case TImode:
3568       case V16QImode:
3569       case V8HImode:
3570       case V4SImode:
3571       case V2DImode:
3572       case V4SFmode:
3573       case V2DFmode:
3574         if (!type || !AGGREGATE_TYPE_P (type))
3575           {
3576             if (!TARGET_SSE && !warnedsse && cum->warn_sse)
3577               {
3578                 warnedsse = true;
3579                 warning (0, "SSE vector argument without SSE enabled "
3580                          "changes the ABI");
3581               }
3582             if (cum->sse_nregs)
3583               ret = gen_reg_or_parallel (mode, orig_mode,
3584                                          cum->sse_regno + FIRST_SSE_REG);
3585           }
3586         break;
3587       case V8QImode:
3588       case V4HImode:
3589       case V2SImode:
3590       case V2SFmode:
3591         if (!type || !AGGREGATE_TYPE_P (type))
3592           {
3593             if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
3594               {
3595                 warnedmmx = true;
3596                 warning (0, "MMX vector argument without MMX enabled "
3597                          "changes the ABI");
3598               }
3599             if (cum->mmx_nregs)
3600               ret = gen_reg_or_parallel (mode, orig_mode,
3601                                          cum->mmx_regno + FIRST_MMX_REG);
3602           }
3603         break;
3604       }
3605
3606   if (TARGET_DEBUG_ARG)
3607     {
3608       fprintf (stderr,
3609                "function_arg (size=%d, wds=%2d, nregs=%d, mode=%4s, named=%d, ",
3610                words, cum->words, cum->nregs, GET_MODE_NAME (mode), named);
3611
3612       if (ret)
3613         print_simple_rtl (stderr, ret);
3614       else
3615         fprintf (stderr, ", stack");
3616
3617       fprintf (stderr, " )\n");
3618     }
3619
3620   return ret;
3621 }
3622
3623 /* A C expression that indicates when an argument must be passed by
3624    reference.  If nonzero for an argument, a copy of that argument is
3625    made in memory and a pointer to the argument is passed instead of
3626    the argument itself.  The pointer is passed in whatever way is
3627    appropriate for passing a pointer to that type.  */
3628
3629 static bool
3630 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3631                         enum machine_mode mode ATTRIBUTE_UNUSED,
3632                         tree type, bool named ATTRIBUTE_UNUSED)
3633 {
3634   if (!TARGET_64BIT)
3635     return 0;
3636
3637   if (type && int_size_in_bytes (type) == -1)
3638     {
3639       if (TARGET_DEBUG_ARG)
3640         fprintf (stderr, "function_arg_pass_by_reference\n");
3641       return 1;
3642     }
3643
3644   return 0;
3645 }
3646
3647 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
3648    ABI.  Only called if TARGET_SSE.  */
3649 static bool
3650 contains_128bit_aligned_vector_p (tree type)
3651 {
3652   enum machine_mode mode = TYPE_MODE (type);
3653   if (SSE_REG_MODE_P (mode)
3654       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
3655     return true;
3656   if (TYPE_ALIGN (type) < 128)
3657     return false;
3658
3659   if (AGGREGATE_TYPE_P (type))
3660     {
3661       /* Walk the aggregates recursively.  */
3662       switch (TREE_CODE (type))
3663         {
3664         case RECORD_TYPE:
3665         case UNION_TYPE:
3666         case QUAL_UNION_TYPE:
3667           {
3668             tree field;
3669             
3670             if (TYPE_BINFO (type))
3671               {
3672                 tree binfo, base_binfo;
3673                 int i;
3674                 
3675                 for (binfo = TYPE_BINFO (type), i = 0;
3676                      BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
3677                   if (contains_128bit_aligned_vector_p
3678                       (BINFO_TYPE (base_binfo)))
3679                     return true;
3680               }
3681             /* And now merge the fields of structure.  */
3682             for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3683               {
3684                 if (TREE_CODE (field) == FIELD_DECL
3685                     && contains_128bit_aligned_vector_p (TREE_TYPE (field)))
3686                   return true;
3687               }
3688             break;
3689           }
3690
3691         case ARRAY_TYPE:
3692           /* Just for use if some languages passes arrays by value.  */
3693           if (contains_128bit_aligned_vector_p (TREE_TYPE (type)))
3694             return true;
3695           break;
3696           
3697         default:
3698           gcc_unreachable ();
3699         }
3700     }
3701   return false;
3702 }
3703
3704 /* Gives the alignment boundary, in bits, of an argument with the
3705    specified mode and type.  */
3706
3707 int
3708 ix86_function_arg_boundary (enum machine_mode mode, tree type)
3709 {
3710   int align;
3711   if (type)
3712     align = TYPE_ALIGN (type);
3713   else
3714     align = GET_MODE_ALIGNMENT (mode);
3715   if (align < PARM_BOUNDARY)
3716     align = PARM_BOUNDARY;
3717   if (!TARGET_64BIT)
3718     {
3719       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
3720          make an exception for SSE modes since these require 128bit
3721          alignment.
3722
3723          The handling here differs from field_alignment.  ICC aligns MMX
3724          arguments to 4 byte boundaries, while structure fields are aligned
3725          to 8 byte boundaries.  */
3726       if (!TARGET_SSE)
3727         align = PARM_BOUNDARY;
3728       else if (!type)
3729         {
3730           if (!SSE_REG_MODE_P (mode))
3731             align = PARM_BOUNDARY;
3732         }
3733       else
3734         {
3735           if (!contains_128bit_aligned_vector_p (type))
3736             align = PARM_BOUNDARY;
3737         }
3738     }
3739   if (align > 128)
3740     align = 128;
3741   return align;
3742 }
3743
3744 /* Return true if N is a possible register number of function value.  */
3745 bool
3746 ix86_function_value_regno_p (int regno)
3747 {
3748   if (regno == 0
3749       || (regno == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387)
3750       || (regno == FIRST_SSE_REG && TARGET_SSE))
3751     return true;
3752
3753   if (!TARGET_64BIT
3754       && (regno == FIRST_MMX_REG && TARGET_MMX))
3755         return true;
3756
3757   return false;
3758 }
3759
3760 /* Define how to find the value returned by a function.
3761    VALTYPE is the data type of the value (as a tree).
3762    If the precise function being called is known, FUNC is its FUNCTION_DECL;
3763    otherwise, FUNC is 0.  */
3764 rtx
3765 ix86_function_value (tree valtype, tree fntype_or_decl,
3766                      bool outgoing ATTRIBUTE_UNUSED)
3767 {
3768   enum machine_mode natmode = type_natural_mode (valtype);
3769
3770   if (TARGET_64BIT)
3771     {
3772       rtx ret = construct_container (natmode, TYPE_MODE (valtype), valtype,
3773                                      1, REGPARM_MAX, SSE_REGPARM_MAX,
3774                                      x86_64_int_return_registers, 0);
3775       /* For zero sized structures, construct_container return NULL, but we
3776          need to keep rest of compiler happy by returning meaningful value.  */
3777       if (!ret)
3778         ret = gen_rtx_REG (TYPE_MODE (valtype), 0);
3779       return ret;
3780     }
3781   else
3782     {
3783       tree fn = NULL_TREE, fntype;
3784       if (fntype_or_decl
3785           && DECL_P (fntype_or_decl))
3786         fn = fntype_or_decl;
3787       fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
3788       return gen_rtx_REG (TYPE_MODE (valtype),
3789                           ix86_value_regno (natmode, fn, fntype));
3790     }
3791 }
3792
3793 /* Return true iff type is returned in memory.  */
3794 int
3795 ix86_return_in_memory (tree type)
3796 {
3797   int needed_intregs, needed_sseregs, size;
3798   enum machine_mode mode = type_natural_mode (type);
3799
3800   if (TARGET_64BIT)
3801     return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
3802
3803   if (mode == BLKmode)
3804     return 1;
3805
3806   size = int_size_in_bytes (type);
3807
3808   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
3809     return 0;
3810
3811   if (VECTOR_MODE_P (mode) || mode == TImode)
3812     {
3813       /* User-created vectors small enough to fit in EAX.  */
3814       if (size < 8)
3815         return 0;
3816
3817       /* MMX/3dNow values are returned in MM0,
3818          except when it doesn't exits.  */
3819       if (size == 8)
3820         return (TARGET_MMX ? 0 : 1);
3821
3822       /* SSE values are returned in XMM0, except when it doesn't exist.  */
3823       if (size == 16)
3824         return (TARGET_SSE ? 0 : 1);
3825     }
3826
3827   if (mode == XFmode)
3828     return 0;
3829
3830   if (mode == TDmode)
3831     return 1;
3832
3833   if (size > 12)
3834     return 1;
3835   return 0;
3836 }
3837
3838 /* When returning SSE vector types, we have a choice of either
3839      (1) being abi incompatible with a -march switch, or
3840      (2) generating an error.
3841    Given no good solution, I think the safest thing is one warning.
3842    The user won't be able to use -Werror, but....
3843
3844    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
3845    called in response to actually generating a caller or callee that
3846    uses such a type.  As opposed to RETURN_IN_MEMORY, which is called
3847    via aggregate_value_p for general type probing from tree-ssa.  */
3848
3849 static rtx
3850 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
3851 {
3852   static bool warnedsse, warnedmmx;
3853
3854   if (type)
3855     {
3856       /* Look at the return type of the function, not the function type.  */
3857       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
3858
3859       if (!TARGET_SSE && !warnedsse)
3860         {
3861           if (mode == TImode
3862               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
3863             {
3864               warnedsse = true;
3865               warning (0, "SSE vector return without SSE enabled "
3866                        "changes the ABI");
3867             }
3868         }
3869
3870       if (!TARGET_MMX && !warnedmmx)
3871         {
3872           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
3873             {
3874               warnedmmx = true;
3875               warning (0, "MMX vector return without MMX enabled "
3876                        "changes the ABI");
3877             }
3878         }
3879     }
3880
3881   return NULL;
3882 }
3883
3884 /* Define how to find the value returned by a library function
3885    assuming the value has mode MODE.  */
3886 rtx
3887 ix86_libcall_value (enum machine_mode mode)
3888 {
3889   if (TARGET_64BIT)
3890     {
3891       switch (mode)
3892         {
3893         case SFmode:
3894         case SCmode:
3895         case DFmode:
3896         case DCmode:
3897         case TFmode:
3898         case SDmode:
3899         case DDmode:
3900         case TDmode:
3901           return gen_rtx_REG (mode, FIRST_SSE_REG);
3902         case XFmode:
3903         case XCmode:
3904           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
3905         case TCmode:
3906           return NULL;
3907         default:
3908           return gen_rtx_REG (mode, 0);
3909         }
3910     }
3911   else
3912     return gen_rtx_REG (mode, ix86_value_regno (mode, NULL, NULL));
3913 }
3914
3915 /* Given a mode, return the register to use for a return value.  */
3916
3917 static int
3918 ix86_value_regno (enum machine_mode mode, tree func, tree fntype)
3919 {
3920   gcc_assert (!TARGET_64BIT);
3921
3922   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
3923      we prevent this case when mmx is not available.  */
3924   if ((VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8))
3925     return FIRST_MMX_REG;
3926
3927   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
3928      we prevent this case when sse is not available.  */
3929   if (mode == TImode || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
3930     return FIRST_SSE_REG;
3931
3932   /* Decimal floating point values can go in %eax, unlike other float modes.  */
3933   if (DECIMAL_FLOAT_MODE_P (mode))
3934     return 0;
3935
3936   /* Most things go in %eax, except (unless -mno-fp-ret-in-387) fp values.  */
3937   if (!SCALAR_FLOAT_MODE_P (mode) || !TARGET_FLOAT_RETURNS_IN_80387)
3938     return 0;
3939
3940   /* Floating point return values in %st(0), except for local functions when
3941      SSE math is enabled or for functions with sseregparm attribute.  */
3942   if ((func || fntype)
3943       && (mode == SFmode || mode == DFmode))
3944     {
3945       int sse_level = ix86_function_sseregparm (fntype, func);
3946       if ((sse_level >= 1 && mode == SFmode)
3947           || (sse_level == 2 && mode == DFmode))
3948         return FIRST_SSE_REG;
3949     }
3950
3951   return FIRST_FLOAT_REG;
3952 }
3953 \f
3954 /* Create the va_list data type.  */
3955
3956 static tree
3957 ix86_build_builtin_va_list (void)
3958 {
3959   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
3960
3961   /* For i386 we use plain pointer to argument area.  */
3962   if (!TARGET_64BIT)
3963     return build_pointer_type (char_type_node);
3964
3965   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
3966   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
3967
3968   f_gpr = build_decl (FIELD_DECL, get_identifier ("gp_offset"),
3969                       unsigned_type_node);
3970   f_fpr = build_decl (FIELD_DECL, get_identifier ("fp_offset"),
3971                       unsigned_type_node);
3972   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
3973                       ptr_type_node);
3974   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
3975                       ptr_type_node);
3976
3977   va_list_gpr_counter_field = f_gpr;
3978   va_list_fpr_counter_field = f_fpr;
3979
3980   DECL_FIELD_CONTEXT (f_gpr) = record;
3981   DECL_FIELD_CONTEXT (f_fpr) = record;
3982   DECL_FIELD_CONTEXT (f_ovf) = record;
3983   DECL_FIELD_CONTEXT (f_sav) = record;
3984
3985   TREE_CHAIN (record) = type_decl;
3986   TYPE_NAME (record) = type_decl;
3987   TYPE_FIELDS (record) = f_gpr;
3988   TREE_CHAIN (f_gpr) = f_fpr;
3989   TREE_CHAIN (f_fpr) = f_ovf;
3990   TREE_CHAIN (f_ovf) = f_sav;
3991
3992   layout_type (record);
3993
3994   /* The correct type is an array type of one element.  */
3995   return build_array_type (record, build_index_type (size_zero_node));
3996 }
3997
3998 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
3999
4000 static void
4001 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4002                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
4003                              int no_rtl)
4004 {
4005   CUMULATIVE_ARGS next_cum;
4006   rtx save_area = NULL_RTX, mem;
4007   rtx label;
4008   rtx label_ref;
4009   rtx tmp_reg;
4010   rtx nsse_reg;
4011   int set;
4012   tree fntype;
4013   int stdarg_p;
4014   int i;
4015
4016   if (!TARGET_64BIT)
4017     return;
4018
4019   if (! cfun->va_list_gpr_size && ! cfun->va_list_fpr_size)
4020     return;
4021
4022   /* Indicate to allocate space on the stack for varargs save area.  */
4023   ix86_save_varrargs_registers = 1;
4024
4025   cfun->stack_alignment_needed = 128;
4026
4027   fntype = TREE_TYPE (current_function_decl);
4028   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
4029               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4030                   != void_type_node));
4031
4032   /* For varargs, we do not want to skip the dummy va_dcl argument.
4033      For stdargs, we do want to skip the last named argument.  */
4034   next_cum = *cum;
4035   if (stdarg_p)
4036     function_arg_advance (&next_cum, mode, type, 1);
4037
4038   if (!no_rtl)
4039     save_area = frame_pointer_rtx;
4040
4041   set = get_varargs_alias_set ();
4042
4043   for (i = next_cum.regno;
4044        i < ix86_regparm
4045        && i < next_cum.regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
4046        i++)
4047     {
4048       mem = gen_rtx_MEM (Pmode,
4049                          plus_constant (save_area, i * UNITS_PER_WORD));
4050       MEM_NOTRAP_P (mem) = 1;
4051       set_mem_alias_set (mem, set);
4052       emit_move_insn (mem, gen_rtx_REG (Pmode,
4053                                         x86_64_int_parameter_registers[i]));
4054     }
4055
4056   if (next_cum.sse_nregs && cfun->va_list_fpr_size)
4057     {
4058       /* Now emit code to save SSE registers.  The AX parameter contains number
4059          of SSE parameter registers used to call this function.  We use
4060          sse_prologue_save insn template that produces computed jump across
4061          SSE saves.  We need some preparation work to get this working.  */
4062
4063       label = gen_label_rtx ();
4064       label_ref = gen_rtx_LABEL_REF (Pmode, label);
4065
4066       /* Compute address to jump to :
4067          label - 5*eax + nnamed_sse_arguments*5  */
4068       tmp_reg = gen_reg_rtx (Pmode);
4069       nsse_reg = gen_reg_rtx (Pmode);
4070       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, 0)));
4071       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
4072                               gen_rtx_MULT (Pmode, nsse_reg,
4073                                             GEN_INT (4))));
4074       if (next_cum.sse_regno)
4075         emit_move_insn
4076           (nsse_reg,
4077            gen_rtx_CONST (DImode,
4078                           gen_rtx_PLUS (DImode,
4079                                         label_ref,
4080                                         GEN_INT (next_cum.sse_regno * 4))));
4081       else
4082         emit_move_insn (nsse_reg, label_ref);
4083       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
4084
4085       /* Compute address of memory block we save into.  We always use pointer
4086          pointing 127 bytes after first byte to store - this is needed to keep
4087          instruction size limited by 4 bytes.  */
4088       tmp_reg = gen_reg_rtx (Pmode);
4089       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
4090                               plus_constant (save_area,
4091                                              8 * REGPARM_MAX + 127)));
4092       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
4093       MEM_NOTRAP_P (mem) = 1;
4094       set_mem_alias_set (mem, set);
4095       set_mem_align (mem, BITS_PER_WORD);
4096
4097       /* And finally do the dirty job!  */
4098       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
4099                                         GEN_INT (next_cum.sse_regno), label));
4100     }
4101
4102 }
4103
4104 /* Implement va_start.  */
4105
4106 void
4107 ix86_va_start (tree valist, rtx nextarg)
4108 {
4109   HOST_WIDE_INT words, n_gpr, n_fpr;
4110   tree f_gpr, f_fpr, f_ovf, f_sav;
4111   tree gpr, fpr, ovf, sav, t;
4112
4113   /* Only 64bit target needs something special.  */
4114   if (!TARGET_64BIT)
4115     {
4116       std_expand_builtin_va_start (valist, nextarg);
4117       return;
4118     }
4119
4120   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4121   f_fpr = TREE_CHAIN (f_gpr);
4122   f_ovf = TREE_CHAIN (f_fpr);
4123   f_sav = TREE_CHAIN (f_ovf);
4124
4125   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
4126   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
4127   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
4128   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
4129   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
4130
4131   /* Count number of gp and fp argument registers used.  */
4132   words = current_function_args_info.words;
4133   n_gpr = current_function_args_info.regno;
4134   n_fpr = current_function_args_info.sse_regno;
4135
4136   if (TARGET_DEBUG_ARG)
4137     fprintf (stderr, "va_start: words = %d, n_gpr = %d, n_fpr = %d\n",
4138              (int) words, (int) n_gpr, (int) n_fpr);
4139
4140   if (cfun->va_list_gpr_size)
4141     {
4142       t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
4143                   build_int_cst (NULL_TREE, n_gpr * 8));
4144       TREE_SIDE_EFFECTS (t) = 1;
4145       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4146     }
4147
4148   if (cfun->va_list_fpr_size)
4149     {
4150       t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
4151                   build_int_cst (NULL_TREE, n_fpr * 16 + 8*REGPARM_MAX));
4152       TREE_SIDE_EFFECTS (t) = 1;
4153       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4154     }
4155
4156   /* Find the overflow area.  */
4157   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
4158   if (words != 0)
4159     t = build2 (PLUS_EXPR, TREE_TYPE (ovf), t,
4160                 build_int_cst (NULL_TREE, words * UNITS_PER_WORD));
4161   t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
4162   TREE_SIDE_EFFECTS (t) = 1;
4163   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4164
4165   if (cfun->va_list_gpr_size || cfun->va_list_fpr_size)
4166     {
4167       /* Find the register save area.
4168          Prologue of the function save it right above stack frame.  */
4169       t = make_tree (TREE_TYPE (sav), frame_pointer_rtx);
4170       t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
4171       TREE_SIDE_EFFECTS (t) = 1;
4172       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4173     }
4174 }
4175
4176 /* Implement va_arg.  */
4177
4178 tree
4179 ix86_gimplify_va_arg (tree valist, tree type, tree *pre_p, tree *post_p)
4180 {
4181   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
4182   tree f_gpr, f_fpr, f_ovf, f_sav;
4183   tree gpr, fpr, ovf, sav, t;
4184   int size, rsize;
4185   tree lab_false, lab_over = NULL_TREE;
4186   tree addr, t2;
4187   rtx container;
4188   int indirect_p = 0;
4189   tree ptrtype;
4190   enum machine_mode nat_mode;
4191
4192   /* Only 64bit target needs something special.  */
4193   if (!TARGET_64BIT)
4194     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
4195
4196   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4197   f_fpr = TREE_CHAIN (f_gpr);
4198   f_ovf = TREE_CHAIN (f_fpr);
4199   f_sav = TREE_CHAIN (f_ovf);
4200
4201   valist = build_va_arg_indirect_ref (valist);
4202   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
4203   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
4204   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
4205   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
4206
4207   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4208   if (indirect_p)
4209     type = build_pointer_type (type);
4210   size = int_size_in_bytes (type);
4211   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4212
4213   nat_mode = type_natural_mode (type);
4214   container = construct_container (nat_mode, TYPE_MODE (type), type, 0,
4215                                    REGPARM_MAX, SSE_REGPARM_MAX, intreg, 0);
4216
4217   /* Pull the value out of the saved registers.  */
4218
4219   addr = create_tmp_var (ptr_type_node, "addr");
4220   DECL_POINTER_ALIAS_SET (addr) = get_varargs_alias_set ();
4221
4222   if (container)
4223     {
4224       int needed_intregs, needed_sseregs;
4225       bool need_temp;
4226       tree int_addr, sse_addr;
4227
4228       lab_false = create_artificial_label ();
4229       lab_over = create_artificial_label ();
4230
4231       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
4232
4233       need_temp = (!REG_P (container)
4234                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
4235                        || TYPE_ALIGN (type) > 128));
4236
4237       /* In case we are passing structure, verify that it is consecutive block
4238          on the register save area.  If not we need to do moves.  */
4239       if (!need_temp && !REG_P (container))
4240         {
4241           /* Verify that all registers are strictly consecutive  */
4242           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
4243             {
4244               int i;
4245
4246               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
4247                 {
4248                   rtx slot = XVECEXP (container, 0, i);
4249                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
4250                       || INTVAL (XEXP (slot, 1)) != i * 16)
4251                     need_temp = 1;
4252                 }
4253             }
4254           else
4255             {
4256               int i;
4257
4258               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
4259                 {
4260                   rtx slot = XVECEXP (container, 0, i);
4261                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
4262                       || INTVAL (XEXP (slot, 1)) != i * 8)
4263                     need_temp = 1;
4264                 }
4265             }
4266         }
4267       if (!need_temp)
4268         {
4269           int_addr = addr;
4270           sse_addr = addr;
4271         }
4272       else
4273         {
4274           int_addr = create_tmp_var (ptr_type_node, "int_addr");
4275           DECL_POINTER_ALIAS_SET (int_addr) = get_varargs_alias_set ();
4276           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
4277           DECL_POINTER_ALIAS_SET (sse_addr) = get_varargs_alias_set ();
4278         }
4279
4280       /* First ensure that we fit completely in registers.  */
4281       if (needed_intregs)
4282         {
4283           t = build_int_cst (TREE_TYPE (gpr),
4284                              (REGPARM_MAX - needed_intregs + 1) * 8);
4285           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
4286           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
4287           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
4288           gimplify_and_add (t, pre_p);
4289         }
4290       if (needed_sseregs)
4291         {
4292           t = build_int_cst (TREE_TYPE (fpr),
4293                              (SSE_REGPARM_MAX - needed_sseregs + 1) * 16
4294                              + REGPARM_MAX * 8);
4295           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
4296           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
4297           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
4298           gimplify_and_add (t, pre_p);
4299         }
4300
4301       /* Compute index to start of area used for integer regs.  */
4302       if (needed_intregs)
4303         {
4304           /* int_addr = gpr + sav; */
4305           t = fold_convert (ptr_type_node, gpr);
4306           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4307           t = build2 (MODIFY_EXPR, void_type_node, int_addr, t);
4308           gimplify_and_add (t, pre_p);
4309         }
4310       if (needed_sseregs)
4311         {
4312           /* sse_addr = fpr + sav; */
4313           t = fold_convert (ptr_type_node, fpr);
4314           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4315           t = build2 (MODIFY_EXPR, void_type_node, sse_addr, t);
4316           gimplify_and_add (t, pre_p);
4317         }
4318       if (need_temp)
4319         {
4320           int i;
4321           tree temp = create_tmp_var (type, "va_arg_tmp");
4322
4323           /* addr = &temp; */
4324           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
4325           t = build2 (MODIFY_EXPR, void_type_node, addr, t);
4326           gimplify_and_add (t, pre_p);
4327
4328           for (i = 0; i < XVECLEN (container, 0); i++)
4329             {
4330               rtx slot = XVECEXP (container, 0, i);
4331               rtx reg = XEXP (slot, 0);
4332               enum machine_mode mode = GET_MODE (reg);
4333               tree piece_type = lang_hooks.types.type_for_mode (mode, 1);
4334               tree addr_type = build_pointer_type (piece_type);
4335               tree src_addr, src;
4336               int src_offset;
4337               tree dest_addr, dest;
4338
4339               if (SSE_REGNO_P (REGNO (reg)))
4340                 {
4341                   src_addr = sse_addr;
4342                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
4343                 }
4344               else
4345                 {
4346                   src_addr = int_addr;
4347                   src_offset = REGNO (reg) * 8;
4348                 }
4349               src_addr = fold_convert (addr_type, src_addr);
4350               src_addr = fold (build2 (PLUS_EXPR, addr_type, src_addr,
4351                                        size_int (src_offset)));
4352               src = build_va_arg_indirect_ref (src_addr);
4353
4354               dest_addr = fold_convert (addr_type, addr);
4355               dest_addr = fold (build2 (PLUS_EXPR, addr_type, dest_addr,
4356                                         size_int (INTVAL (XEXP (slot, 1)))));
4357               dest = build_va_arg_indirect_ref (dest_addr);
4358
4359               t = build2 (MODIFY_EXPR, void_type_node, dest, src);
4360               gimplify_and_add (t, pre_p);
4361             }
4362         }
4363
4364       if (needed_intregs)
4365         {
4366           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
4367                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
4368           t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
4369           gimplify_and_add (t, pre_p);
4370         }
4371       if (needed_sseregs)
4372         {
4373           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
4374                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
4375           t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr, t);
4376           gimplify_and_add (t, pre_p);
4377         }
4378
4379       t = build1 (GOTO_EXPR, void_type_node, lab_over);
4380       gimplify_and_add (t, pre_p);
4381
4382       t = build1 (LABEL_EXPR, void_type_node, lab_false);
4383       append_to_statement_list (t, pre_p);
4384     }
4385
4386   /* ... otherwise out of the overflow area.  */
4387
4388   /* Care for on-stack alignment if needed.  */
4389   if (FUNCTION_ARG_BOUNDARY (VOIDmode, type) <= 64
4390       || integer_zerop (TYPE_SIZE (type)))
4391     t = ovf;
4392   else
4393     {
4394       HOST_WIDE_INT align = FUNCTION_ARG_BOUNDARY (VOIDmode, type) / 8;
4395       t = build2 (PLUS_EXPR, TREE_TYPE (ovf), ovf,
4396                   build_int_cst (TREE_TYPE (ovf), align - 1));
4397       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
4398                   build_int_cst (TREE_TYPE (t), -align));
4399     }
4400   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
4401
4402   t2 = build2 (MODIFY_EXPR, void_type_node, addr, t);
4403   gimplify_and_add (t2, pre_p);
4404
4405   t = build2 (PLUS_EXPR, TREE_TYPE (t), t,
4406               build_int_cst (TREE_TYPE (t), rsize * UNITS_PER_WORD));
4407   t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
4408   gimplify_and_add (t, pre_p);
4409
4410   if (container)
4411     {
4412       t = build1 (LABEL_EXPR, void_type_node, lab_over);
4413       append_to_statement_list (t, pre_p);
4414     }
4415
4416   ptrtype = build_pointer_type (type);
4417   addr = fold_convert (ptrtype, addr);
4418
4419   if (indirect_p)
4420     addr = build_va_arg_indirect_ref (addr);
4421   return build_va_arg_indirect_ref (addr);
4422 }
4423 \f
4424 /* Return nonzero if OPNUM's MEM should be matched
4425    in movabs* patterns.  */
4426
4427 int
4428 ix86_check_movabs (rtx insn, int opnum)
4429 {
4430   rtx set, mem;
4431
4432   set = PATTERN (insn);
4433   if (GET_CODE (set) == PARALLEL)
4434     set = XVECEXP (set, 0, 0);
4435   gcc_assert (GET_CODE (set) == SET);
4436   mem = XEXP (set, opnum);
4437   while (GET_CODE (mem) == SUBREG)
4438     mem = SUBREG_REG (mem);
4439   gcc_assert (GET_CODE (mem) == MEM);
4440   return (volatile_ok || !MEM_VOLATILE_P (mem));
4441 }
4442 \f
4443 /* Initialize the table of extra 80387 mathematical constants.  */
4444
4445 static void
4446 init_ext_80387_constants (void)
4447 {
4448   static const char * cst[5] =
4449   {
4450     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
4451     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
4452     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
4453     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
4454     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
4455   };
4456   int i;
4457
4458   for (i = 0; i < 5; i++)
4459     {
4460       real_from_string (&ext_80387_constants_table[i], cst[i]);
4461       /* Ensure each constant is rounded to XFmode precision.  */
4462       real_convert (&ext_80387_constants_table[i],
4463                     XFmode, &ext_80387_constants_table[i]);
4464     }
4465
4466   ext_80387_constants_init = 1;
4467 }
4468
4469 /* Return true if the constant is something that can be loaded with
4470    a special instruction.  */
4471
4472 int
4473 standard_80387_constant_p (rtx x)
4474 {
4475   if (GET_CODE (x) != CONST_DOUBLE || !FLOAT_MODE_P (GET_MODE (x)))
4476     return -1;
4477
4478   if (x == CONST0_RTX (GET_MODE (x)))
4479     return 1;
4480   if (x == CONST1_RTX (GET_MODE (x)))
4481     return 2;
4482
4483   /* For XFmode constants, try to find a special 80387 instruction when
4484      optimizing for size or on those CPUs that benefit from them.  */
4485   if (GET_MODE (x) == XFmode
4486       && (optimize_size || x86_ext_80387_constants & TUNEMASK))
4487     {
4488       REAL_VALUE_TYPE r;
4489       int i;
4490
4491       if (! ext_80387_constants_init)
4492         init_ext_80387_constants ();
4493
4494       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4495       for (i = 0; i < 5; i++)
4496         if (real_identical (&r, &ext_80387_constants_table[i]))
4497           return i + 3;
4498     }
4499
4500   return 0;
4501 }
4502
4503 /* Return the opcode of the special instruction to be used to load
4504    the constant X.  */
4505
4506 const char *
4507 standard_80387_constant_opcode (rtx x)
4508 {
4509   switch (standard_80387_constant_p (x))
4510     {
4511     case 1:
4512       return "fldz";
4513     case 2:
4514       return "fld1";
4515     case 3:
4516       return "fldlg2";
4517     case 4:
4518       return "fldln2";
4519     case 5:
4520       return "fldl2e";
4521     case 6:
4522       return "fldl2t";
4523     case 7:
4524       return "fldpi";
4525     default:
4526       gcc_unreachable ();
4527     }
4528 }
4529
4530 /* Return the CONST_DOUBLE representing the 80387 constant that is
4531    loaded by the specified special instruction.  The argument IDX
4532    matches the return value from standard_80387_constant_p.  */
4533
4534 rtx
4535 standard_80387_constant_rtx (int idx)
4536 {
4537   int i;
4538
4539   if (! ext_80387_constants_init)
4540     init_ext_80387_constants ();
4541
4542   switch (idx)
4543     {
4544     case 3:
4545     case 4:
4546     case 5:
4547     case 6:
4548     case 7:
4549       i = idx - 3;
4550       break;
4551
4552     default:
4553       gcc_unreachable ();
4554     }
4555
4556   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
4557                                        XFmode);
4558 }
4559
4560 /* Return 1 if X is FP constant we can load to SSE register w/o using memory.
4561  */
4562 int
4563 standard_sse_constant_p (rtx x)
4564 {
4565   if (x == const0_rtx)
4566     return 1;
4567   return (x == CONST0_RTX (GET_MODE (x)));
4568 }
4569
4570 /* Returns 1 if OP contains a symbol reference */
4571
4572 int
4573 symbolic_reference_mentioned_p (rtx op)
4574 {
4575   const char *fmt;
4576   int i;
4577
4578   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
4579     return 1;
4580
4581   fmt = GET_RTX_FORMAT (GET_CODE (op));
4582   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
4583     {
4584       if (fmt[i] == 'E')
4585         {
4586           int j;
4587
4588           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
4589             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
4590               return 1;
4591         }
4592
4593       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
4594         return 1;
4595     }
4596
4597   return 0;
4598 }
4599
4600 /* Return 1 if it is appropriate to emit `ret' instructions in the
4601    body of a function.  Do this only if the epilogue is simple, needing a
4602    couple of insns.  Prior to reloading, we can't tell how many registers
4603    must be saved, so return 0 then.  Return 0 if there is no frame
4604    marker to de-allocate.  */
4605
4606 int
4607 ix86_can_use_return_insn_p (void)
4608 {
4609   struct ix86_frame frame;
4610
4611   if (! reload_completed || frame_pointer_needed)
4612     return 0;
4613
4614   /* Don't allow more than 32 pop, since that's all we can do
4615      with one instruction.  */
4616   if (current_function_pops_args
4617       && current_function_args_size >= 32768)
4618     return 0;
4619
4620   ix86_compute_frame_layout (&frame);
4621   return frame.to_allocate == 0 && frame.nregs == 0;
4622 }
4623 \f
4624 /* Value should be nonzero if functions must have frame pointers.
4625    Zero means the frame pointer need not be set up (and parms may
4626    be accessed via the stack pointer) in functions that seem suitable.  */
4627
4628 int
4629 ix86_frame_pointer_required (void)
4630 {
4631   /* If we accessed previous frames, then the generated code expects
4632      to be able to access the saved ebp value in our frame.  */
4633   if (cfun->machine->accesses_prev_frame)
4634     return 1;
4635
4636   /* Several x86 os'es need a frame pointer for other reasons,
4637      usually pertaining to setjmp.  */
4638   if (SUBTARGET_FRAME_POINTER_REQUIRED)
4639     return 1;
4640
4641   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
4642      the frame pointer by default.  Turn it back on now if we've not
4643      got a leaf function.  */
4644   if (TARGET_OMIT_LEAF_FRAME_POINTER
4645       && (!current_function_is_leaf
4646           || ix86_current_function_calls_tls_descriptor))
4647     return 1;
4648
4649   if (current_function_profile)
4650     return 1;
4651
4652   return 0;
4653 }
4654
4655 /* Record that the current function accesses previous call frames.  */
4656
4657 void
4658 ix86_setup_frame_addresses (void)
4659 {
4660   cfun->machine->accesses_prev_frame = 1;
4661 }
4662 \f
4663 #if (defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)) || TARGET_MACHO
4664 # define USE_HIDDEN_LINKONCE 1
4665 #else
4666 # define USE_HIDDEN_LINKONCE 0
4667 #endif
4668
4669 static int pic_labels_used;
4670
4671 /* Fills in the label name that should be used for a pc thunk for
4672    the given register.  */
4673
4674 static void
4675 get_pc_thunk_name (char name[32], unsigned int regno)
4676 {
4677   if (USE_HIDDEN_LINKONCE)
4678     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
4679   else
4680     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
4681 }
4682
4683
4684 /* This function generates code for -fpic that loads %ebx with
4685    the return address of the caller and then returns.  */
4686
4687 void
4688 ix86_file_end (void)
4689 {
4690   rtx xops[2];
4691   int regno;
4692
4693   for (regno = 0; regno < 8; ++regno)
4694     {
4695       char name[32];
4696
4697       if (! ((pic_labels_used >> regno) & 1))
4698         continue;
4699
4700       get_pc_thunk_name (name, regno);
4701
4702 #if TARGET_MACHO
4703       if (TARGET_MACHO)
4704         {
4705           switch_to_section (darwin_sections[text_coal_section]);
4706           fputs ("\t.weak_definition\t", asm_out_file);
4707           assemble_name (asm_out_file, name);
4708           fputs ("\n\t.private_extern\t", asm_out_file);
4709           assemble_name (asm_out_file, name);
4710           fputs ("\n", asm_out_file);
4711           ASM_OUTPUT_LABEL (asm_out_file, name);
4712         }
4713       else
4714 #endif
4715       if (USE_HIDDEN_LINKONCE)
4716         {
4717           tree decl;
4718
4719           decl = build_decl (FUNCTION_DECL, get_identifier (name),
4720                              error_mark_node);
4721           TREE_PUBLIC (decl) = 1;
4722           TREE_STATIC (decl) = 1;
4723           DECL_ONE_ONLY (decl) = 1;
4724
4725           (*targetm.asm_out.unique_section) (decl, 0);
4726           switch_to_section (get_named_section (decl, NULL, 0));
4727
4728           (*targetm.asm_out.globalize_label) (asm_out_file, name);
4729           fputs ("\t.hidden\t", asm_out_file);
4730           assemble_name (asm_out_file, name);
4731           fputc ('\n', asm_out_file);
4732           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
4733         }
4734       else
4735         {
4736           switch_to_section (text_section);
4737           ASM_OUTPUT_LABEL (asm_out_file, name);
4738         }
4739
4740       xops[0] = gen_rtx_REG (SImode, regno);
4741       xops[1] = gen_rtx_MEM (SImode, stack_pointer_rtx);
4742       output_asm_insn ("mov{l}\t{%1, %0|%0, %1}", xops);
4743       output_asm_insn ("ret", xops);
4744     }
4745
4746   if (NEED_INDICATE_EXEC_STACK)
4747     file_end_indicate_exec_stack ();
4748 }
4749
4750 /* Emit code for the SET_GOT patterns.  */
4751
4752 const char *
4753 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
4754 {
4755   rtx xops[3];
4756
4757   xops[0] = dest;
4758   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
4759
4760   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
4761     {
4762       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
4763
4764       if (!flag_pic)
4765         output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
4766       else
4767         output_asm_insn ("call\t%a2", xops);
4768
4769 #if TARGET_MACHO
4770       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
4771          is what will be referenced by the Mach-O PIC subsystem.  */
4772       if (!label)
4773         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
4774 #endif
4775
4776       (*targetm.asm_out.internal_label) (asm_out_file, "L",
4777                                  CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
4778
4779       if (flag_pic)
4780         output_asm_insn ("pop{l}\t%0", xops);
4781     }
4782   else
4783     {
4784       char name[32];
4785       get_pc_thunk_name (name, REGNO (dest));
4786       pic_labels_used |= 1 << REGNO (dest);
4787
4788       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
4789       xops[2] = gen_rtx_MEM (QImode, xops[2]);
4790       output_asm_insn ("call\t%X2", xops);
4791       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
4792          is what will be referenced by the Mach-O PIC subsystem.  */
4793 #if TARGET_MACHO
4794       if (!label)
4795         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
4796       else
4797         targetm.asm_out.internal_label (asm_out_file, "L",
4798                                            CODE_LABEL_NUMBER (label));
4799 #endif
4800     }
4801
4802   if (TARGET_MACHO)
4803     return "";
4804
4805   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
4806     output_asm_insn ("add{l}\t{%1, %0|%0, %1}", xops);
4807   else
4808     output_asm_insn ("add{l}\t{%1+[.-%a2], %0|%0, %1+(.-%a2)}", xops);
4809
4810   return "";
4811 }
4812
4813 /* Generate an "push" pattern for input ARG.  */
4814
4815 static rtx
4816 gen_push (rtx arg)
4817 {
4818   return gen_rtx_SET (VOIDmode,
4819                       gen_rtx_MEM (Pmode,
4820                                    gen_rtx_PRE_DEC (Pmode,
4821                                                     stack_pointer_rtx)),
4822                       arg);
4823 }
4824
4825 /* Return >= 0 if there is an unused call-clobbered register available
4826    for the entire function.  */
4827
4828 static unsigned int
4829 ix86_select_alt_pic_regnum (void)
4830 {
4831   if (current_function_is_leaf && !current_function_profile
4832       && !ix86_current_function_calls_tls_descriptor)
4833     {
4834       int i;
4835       for (i = 2; i >= 0; --i)
4836         if (!regs_ever_live[i])
4837           return i;
4838     }
4839
4840   return INVALID_REGNUM;
4841 }
4842
4843 /* Return 1 if we need to save REGNO.  */
4844 static int
4845 ix86_save_reg (unsigned int regno, int maybe_eh_return)
4846 {
4847   if (pic_offset_table_rtx
4848       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
4849       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
4850           || current_function_profile
4851           || current_function_calls_eh_return
4852           || current_function_uses_const_pool))
4853     {
4854       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
4855         return 0;
4856       return 1;
4857     }
4858
4859   if (current_function_calls_eh_return && maybe_eh_return)
4860     {
4861       unsigned i;
4862       for (i = 0; ; i++)
4863         {
4864           unsigned test = EH_RETURN_DATA_REGNO (i);
4865           if (test == INVALID_REGNUM)
4866             break;
4867           if (test == regno)
4868             return 1;
4869         }
4870     }
4871
4872   if (cfun->machine->force_align_arg_pointer
4873       && regno == REGNO (cfun->machine->force_align_arg_pointer))
4874     return 1;
4875
4876   return (regs_ever_live[regno]
4877           && !call_used_regs[regno]
4878           && !fixed_regs[regno]
4879           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
4880 }
4881
4882 /* Return number of registers to be saved on the stack.  */
4883
4884 static int
4885 ix86_nsaved_regs (void)
4886 {
4887   int nregs = 0;
4888   int regno;
4889
4890   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
4891     if (ix86_save_reg (regno, true))
4892       nregs++;
4893   return nregs;
4894 }
4895
4896 /* Return the offset between two registers, one to be eliminated, and the other
4897    its replacement, at the start of a routine.  */
4898
4899 HOST_WIDE_INT
4900 ix86_initial_elimination_offset (int from, int to)
4901 {
4902   struct ix86_frame frame;
4903   ix86_compute_frame_layout (&frame);
4904
4905   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
4906     return frame.hard_frame_pointer_offset;
4907   else if (from == FRAME_POINTER_REGNUM
4908            && to == HARD_FRAME_POINTER_REGNUM)
4909     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
4910   else
4911     {
4912       gcc_assert (to == STACK_POINTER_REGNUM);
4913
4914       if (from == ARG_POINTER_REGNUM)
4915         return frame.stack_pointer_offset;
4916       
4917       gcc_assert (from == FRAME_POINTER_REGNUM);
4918       return frame.stack_pointer_offset - frame.frame_pointer_offset;
4919     }
4920 }
4921
4922 /* Fill structure ix86_frame about frame of currently computed function.  */
4923
4924 static void
4925 ix86_compute_frame_layout (struct ix86_frame *frame)
4926 {
4927   HOST_WIDE_INT total_size;
4928   unsigned int stack_alignment_needed;
4929   HOST_WIDE_INT offset;
4930   unsigned int preferred_alignment;
4931   HOST_WIDE_INT size = get_frame_size ();
4932
4933   frame->nregs = ix86_nsaved_regs ();
4934   total_size = size;
4935
4936   stack_alignment_needed = cfun->stack_alignment_needed / BITS_PER_UNIT;
4937   preferred_alignment = cfun->preferred_stack_boundary / BITS_PER_UNIT;
4938
4939   /* During reload iteration the amount of registers saved can change.
4940      Recompute the value as needed.  Do not recompute when amount of registers
4941      didn't change as reload does multiple calls to the function and does not
4942      expect the decision to change within single iteration.  */
4943   if (!optimize_size
4944       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
4945     {
4946       int count = frame->nregs;
4947
4948       cfun->machine->use_fast_prologue_epilogue_nregs = count;
4949       /* The fast prologue uses move instead of push to save registers.  This
4950          is significantly longer, but also executes faster as modern hardware
4951          can execute the moves in parallel, but can't do that for push/pop.
4952
4953          Be careful about choosing what prologue to emit:  When function takes
4954          many instructions to execute we may use slow version as well as in
4955          case function is known to be outside hot spot (this is known with
4956          feedback only).  Weight the size of function by number of registers
4957          to save as it is cheap to use one or two push instructions but very
4958          slow to use many of them.  */
4959       if (count)
4960         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
4961       if (cfun->function_frequency < FUNCTION_FREQUENCY_NORMAL
4962           || (flag_branch_probabilities
4963               && cfun->function_frequency < FUNCTION_FREQUENCY_HOT))
4964         cfun->machine->use_fast_prologue_epilogue = false;
4965       else
4966         cfun->machine->use_fast_prologue_epilogue
4967            = !expensive_function_p (count);
4968     }
4969   if (TARGET_PROLOGUE_USING_MOVE
4970       && cfun->machine->use_fast_prologue_epilogue)
4971     frame->save_regs_using_mov = true;
4972   else
4973     frame->save_regs_using_mov = false;
4974
4975
4976   /* Skip return address and saved base pointer.  */
4977   offset = frame_pointer_needed ? UNITS_PER_WORD * 2 : UNITS_PER_WORD;
4978
4979   frame->hard_frame_pointer_offset = offset;
4980
4981   /* Do some sanity checking of stack_alignment_needed and
4982      preferred_alignment, since i386 port is the only using those features
4983      that may break easily.  */
4984
4985   gcc_assert (!size || stack_alignment_needed);
4986   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
4987   gcc_assert (preferred_alignment <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
4988   gcc_assert (stack_alignment_needed
4989               <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
4990
4991   if (stack_alignment_needed < STACK_BOUNDARY / BITS_PER_UNIT)
4992     stack_alignment_needed = STACK_BOUNDARY / BITS_PER_UNIT;
4993
4994   /* Register save area */
4995   offset += frame->nregs * UNITS_PER_WORD;
4996
4997   /* Va-arg area */
4998   if (ix86_save_varrargs_registers)
4999     {
5000       offset += X86_64_VARARGS_SIZE;
5001       frame->va_arg_size = X86_64_VARARGS_SIZE;
5002     }
5003   else
5004     frame->va_arg_size = 0;
5005
5006   /* Align start of frame for local function.  */
5007   frame->padding1 = ((offset + stack_alignment_needed - 1)
5008                      & -stack_alignment_needed) - offset;
5009
5010   offset += frame->padding1;
5011
5012   /* Frame pointer points here.  */
5013   frame->frame_pointer_offset = offset;
5014
5015   offset += size;
5016
5017   /* Add outgoing arguments area.  Can be skipped if we eliminated
5018      all the function calls as dead code.
5019      Skipping is however impossible when function calls alloca.  Alloca
5020      expander assumes that last current_function_outgoing_args_size
5021      of stack frame are unused.  */
5022   if (ACCUMULATE_OUTGOING_ARGS
5023       && (!current_function_is_leaf || current_function_calls_alloca
5024           || ix86_current_function_calls_tls_descriptor))
5025     {
5026       offset += current_function_outgoing_args_size;
5027       frame->outgoing_arguments_size = current_function_outgoing_args_size;
5028     }
5029   else
5030     frame->outgoing_arguments_size = 0;
5031
5032   /* Align stack boundary.  Only needed if we're calling another function
5033      or using alloca.  */
5034   if (!current_function_is_leaf || current_function_calls_alloca
5035       || ix86_current_function_calls_tls_descriptor)
5036     frame->padding2 = ((offset + preferred_alignment - 1)
5037                        & -preferred_alignment) - offset;
5038   else
5039     frame->padding2 = 0;
5040
5041   offset += frame->padding2;
5042
5043   /* We've reached end of stack frame.  */
5044   frame->stack_pointer_offset = offset;
5045
5046   /* Size prologue needs to allocate.  */
5047   frame->to_allocate =
5048     (size + frame->padding1 + frame->padding2
5049      + frame->outgoing_arguments_size + frame->va_arg_size);
5050
5051   if ((!frame->to_allocate && frame->nregs <= 1)
5052       || (TARGET_64BIT && frame->to_allocate >= (HOST_WIDE_INT) 0x80000000))
5053     frame->save_regs_using_mov = false;
5054
5055   if (TARGET_RED_ZONE && current_function_sp_is_unchanging
5056       && current_function_is_leaf
5057       && !ix86_current_function_calls_tls_descriptor)
5058     {
5059       frame->red_zone_size = frame->to_allocate;
5060       if (frame->save_regs_using_mov)
5061         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
5062       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
5063         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
5064     }
5065   else
5066     frame->red_zone_size = 0;
5067   frame->to_allocate -= frame->red_zone_size;
5068   frame->stack_pointer_offset -= frame->red_zone_size;
5069 #if 0
5070   fprintf (stderr, "nregs: %i\n", frame->nregs);
5071   fprintf (stderr, "size: %i\n", size);
5072   fprintf (stderr, "alignment1: %i\n", stack_alignment_needed);
5073   fprintf (stderr, "padding1: %i\n", frame->padding1);
5074   fprintf (stderr, "va_arg: %i\n", frame->va_arg_size);
5075   fprintf (stderr, "padding2: %i\n", frame->padding2);
5076   fprintf (stderr, "to_allocate: %i\n", frame->to_allocate);
5077   fprintf (stderr, "red_zone_size: %i\n", frame->red_zone_size);
5078   fprintf (stderr, "frame_pointer_offset: %i\n", frame->frame_pointer_offset);
5079   fprintf (stderr, "hard_frame_pointer_offset: %i\n",
5080            frame->hard_frame_pointer_offset);
5081   fprintf (stderr, "stack_pointer_offset: %i\n", frame->stack_pointer_offset);
5082 #endif
5083 }
5084
5085 /* Emit code to save registers in the prologue.  */
5086
5087 static void
5088 ix86_emit_save_regs (void)
5089 {
5090   unsigned int regno;
5091   rtx insn;
5092
5093   for (regno = FIRST_PSEUDO_REGISTER; regno-- > 0; )
5094     if (ix86_save_reg (regno, true))
5095       {
5096         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
5097         RTX_FRAME_RELATED_P (insn) = 1;
5098       }
5099 }
5100
5101 /* Emit code to save registers using MOV insns.  First register
5102    is restored from POINTER + OFFSET.  */
5103 static void
5104 ix86_emit_save_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
5105 {
5106   unsigned int regno;
5107   rtx insn;
5108
5109   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5110     if (ix86_save_reg (regno, true))
5111       {
5112         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
5113                                                Pmode, offset),
5114                                gen_rtx_REG (Pmode, regno));
5115         RTX_FRAME_RELATED_P (insn) = 1;
5116         offset += UNITS_PER_WORD;
5117       }
5118 }
5119
5120 /* Expand prologue or epilogue stack adjustment.
5121    The pattern exist to put a dependency on all ebp-based memory accesses.
5122    STYLE should be negative if instructions should be marked as frame related,
5123    zero if %r11 register is live and cannot be freely used and positive
5124    otherwise.  */
5125
5126 static void
5127 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset, int style)
5128 {
5129   rtx insn;
5130
5131   if (! TARGET_64BIT)
5132     insn = emit_insn (gen_pro_epilogue_adjust_stack_1 (dest, src, offset));
5133   else if (x86_64_immediate_operand (offset, DImode))
5134     insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64 (dest, src, offset));
5135   else
5136     {
5137       rtx r11;
5138       /* r11 is used by indirect sibcall return as well, set before the
5139          epilogue and used after the epilogue.  ATM indirect sibcall
5140          shouldn't be used together with huge frame sizes in one
5141          function because of the frame_size check in sibcall.c.  */
5142       gcc_assert (style);
5143       r11 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
5144       insn = emit_insn (gen_rtx_SET (DImode, r11, offset));
5145       if (style < 0)
5146         RTX_FRAME_RELATED_P (insn) = 1;
5147       insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64_2 (dest, src, r11,
5148                                                                offset));
5149     }
5150   if (style < 0)
5151     RTX_FRAME_RELATED_P (insn) = 1;
5152 }
5153
5154 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
5155
5156 static rtx
5157 ix86_internal_arg_pointer (void)
5158 {
5159   if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
5160       && DECL_NAME (current_function_decl)
5161       && MAIN_NAME_P (DECL_NAME (current_function_decl))
5162       && DECL_FILE_SCOPE_P (current_function_decl))
5163     {
5164       cfun->machine->force_align_arg_pointer = gen_rtx_REG (Pmode, 2);
5165       return copy_to_reg (cfun->machine->force_align_arg_pointer);
5166     }
5167   else
5168     return virtual_incoming_args_rtx;
5169 }
5170
5171 /* Handle the TARGET_DWARF_HANDLE_FRAME_UNSPEC hook.
5172    This is called from dwarf2out.c to emit call frame instructions
5173    for frame-related insns containing UNSPECs and UNSPEC_VOLATILEs. */
5174 static void
5175 ix86_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
5176 {
5177   rtx unspec = SET_SRC (pattern);
5178   gcc_assert (GET_CODE (unspec) == UNSPEC);
5179
5180   switch (index)
5181     {
5182     case UNSPEC_REG_SAVE:
5183       dwarf2out_reg_save_reg (label, XVECEXP (unspec, 0, 0),
5184                               SET_DEST (pattern));
5185       break;
5186     case UNSPEC_DEF_CFA:
5187       dwarf2out_def_cfa (label, REGNO (SET_DEST (pattern)),
5188                          INTVAL (XVECEXP (unspec, 0, 0)));
5189       break;
5190     default:
5191       gcc_unreachable ();
5192     }
5193 }
5194
5195 /* Expand the prologue into a bunch of separate insns.  */
5196
5197 void
5198 ix86_expand_prologue (void)
5199 {
5200   rtx insn;
5201   bool pic_reg_used;
5202   struct ix86_frame frame;
5203   HOST_WIDE_INT allocate;
5204
5205   ix86_compute_frame_layout (&frame);
5206
5207   if (cfun->machine->force_align_arg_pointer)
5208     {
5209       rtx x, y;
5210
5211       /* Grab the argument pointer.  */
5212       x = plus_constant (stack_pointer_rtx, 4);
5213       y = cfun->machine->force_align_arg_pointer;
5214       insn = emit_insn (gen_rtx_SET (VOIDmode, y, x));
5215       RTX_FRAME_RELATED_P (insn) = 1;
5216
5217       /* The unwind info consists of two parts: install the fafp as the cfa,
5218          and record the fafp as the "save register" of the stack pointer.
5219          The later is there in order that the unwinder can see where it
5220          should restore the stack pointer across the and insn.  */
5221       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx), UNSPEC_DEF_CFA);
5222       x = gen_rtx_SET (VOIDmode, y, x);
5223       RTX_FRAME_RELATED_P (x) = 1;
5224       y = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, stack_pointer_rtx),
5225                           UNSPEC_REG_SAVE);
5226       y = gen_rtx_SET (VOIDmode, cfun->machine->force_align_arg_pointer, y);
5227       RTX_FRAME_RELATED_P (y) = 1;
5228       x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x, y));
5229       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
5230       REG_NOTES (insn) = x;
5231
5232       /* Align the stack.  */
5233       emit_insn (gen_andsi3 (stack_pointer_rtx, stack_pointer_rtx,
5234                              GEN_INT (-16)));
5235
5236       /* And here we cheat like madmen with the unwind info.  We force the
5237          cfa register back to sp+4, which is exactly what it was at the
5238          start of the function.  Re-pushing the return address results in
5239          the return at the same spot relative to the cfa, and thus is 
5240          correct wrt the unwind info.  */
5241       x = cfun->machine->force_align_arg_pointer;
5242       x = gen_frame_mem (Pmode, plus_constant (x, -4));
5243       insn = emit_insn (gen_push (x));
5244       RTX_FRAME_RELATED_P (insn) = 1;
5245
5246       x = GEN_INT (4);
5247       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, x), UNSPEC_DEF_CFA);
5248       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
5249       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
5250       REG_NOTES (insn) = x;
5251     }
5252
5253   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
5254      slower on all targets.  Also sdb doesn't like it.  */
5255
5256   if (frame_pointer_needed)
5257     {
5258       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
5259       RTX_FRAME_RELATED_P (insn) = 1;
5260
5261       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
5262       RTX_FRAME_RELATED_P (insn) = 1;
5263     }
5264
5265   allocate = frame.to_allocate;
5266
5267   if (!frame.save_regs_using_mov)
5268     ix86_emit_save_regs ();
5269   else
5270     allocate += frame.nregs * UNITS_PER_WORD;
5271
5272   /* When using red zone we may start register saving before allocating
5273      the stack frame saving one cycle of the prologue.  */
5274   if (TARGET_RED_ZONE && frame.save_regs_using_mov)
5275     ix86_emit_save_regs_using_mov (frame_pointer_needed ? hard_frame_pointer_rtx
5276                                    : stack_pointer_rtx,
5277                                    -frame.nregs * UNITS_PER_WORD);
5278
5279   if (allocate == 0)
5280     ;
5281   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
5282     pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5283                                GEN_INT (-allocate), -1);
5284   else
5285     {
5286       /* Only valid for Win32.  */
5287       rtx eax = gen_rtx_REG (SImode, 0);
5288       bool eax_live = ix86_eax_live_at_start_p ();
5289       rtx t;
5290
5291       gcc_assert (!TARGET_64BIT);
5292
5293       if (eax_live)
5294         {
5295           emit_insn (gen_push (eax));
5296           allocate -= 4;
5297         }
5298
5299       emit_move_insn (eax, GEN_INT (allocate));
5300
5301       insn = emit_insn (gen_allocate_stack_worker (eax));
5302       RTX_FRAME_RELATED_P (insn) = 1;
5303       t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (-allocate));
5304       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
5305       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
5306                                             t, REG_NOTES (insn));
5307
5308       if (eax_live)
5309         {
5310           if (frame_pointer_needed)
5311             t = plus_constant (hard_frame_pointer_rtx,
5312                                allocate
5313                                - frame.to_allocate
5314                                - frame.nregs * UNITS_PER_WORD);
5315           else
5316             t = plus_constant (stack_pointer_rtx, allocate);
5317           emit_move_insn (eax, gen_rtx_MEM (SImode, t));
5318         }
5319     }
5320
5321   if (frame.save_regs_using_mov && !TARGET_RED_ZONE)
5322     {
5323       if (!frame_pointer_needed || !frame.to_allocate)
5324         ix86_emit_save_regs_using_mov (stack_pointer_rtx, frame.to_allocate);
5325       else
5326         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
5327                                        -frame.nregs * UNITS_PER_WORD);
5328     }
5329
5330   pic_reg_used = false;
5331   if (pic_offset_table_rtx
5332       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
5333           || current_function_profile))
5334     {
5335       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
5336
5337       if (alt_pic_reg_used != INVALID_REGNUM)
5338         REGNO (pic_offset_table_rtx) = alt_pic_reg_used;
5339
5340       pic_reg_used = true;
5341     }
5342
5343   if (pic_reg_used)
5344     {
5345       if (TARGET_64BIT)
5346         insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
5347       else
5348         insn = emit_insn (gen_set_got (pic_offset_table_rtx));
5349
5350       /* Even with accurate pre-reload life analysis, we can wind up
5351          deleting all references to the pic register after reload.
5352          Consider if cross-jumping unifies two sides of a branch
5353          controlled by a comparison vs the only read from a global.
5354          In which case, allow the set_got to be deleted, though we're
5355          too late to do anything about the ebx save in the prologue.  */
5356       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5357     }
5358
5359   /* Prevent function calls from be scheduled before the call to mcount.
5360      In the pic_reg_used case, make sure that the got load isn't deleted.  */
5361   if (current_function_profile)
5362     emit_insn (gen_blockage (pic_reg_used ? pic_offset_table_rtx : const0_rtx));
5363 }
5364
5365 /* Emit code to restore saved registers using MOV insns.  First register
5366    is restored from POINTER + OFFSET.  */
5367 static void
5368 ix86_emit_restore_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
5369                                   int maybe_eh_return)
5370 {
5371   int regno;
5372   rtx base_address = gen_rtx_MEM (Pmode, pointer);
5373
5374   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5375     if (ix86_save_reg (regno, maybe_eh_return))
5376       {
5377         /* Ensure that adjust_address won't be forced to produce pointer
5378            out of range allowed by x86-64 instruction set.  */
5379         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
5380           {
5381             rtx r11;
5382
5383             r11 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
5384             emit_move_insn (r11, GEN_INT (offset));
5385             emit_insn (gen_adddi3 (r11, r11, pointer));
5386             base_address = gen_rtx_MEM (Pmode, r11);
5387             offset = 0;
5388           }
5389         emit_move_insn (gen_rtx_REG (Pmode, regno),
5390                         adjust_address (base_address, Pmode, offset));
5391         offset += UNITS_PER_WORD;
5392       }
5393 }
5394
5395 /* Restore function stack, frame, and registers.  */
5396
5397 void
5398 ix86_expand_epilogue (int style)
5399 {
5400   int regno;
5401   int sp_valid = !frame_pointer_needed || current_function_sp_is_unchanging;
5402   struct ix86_frame frame;
5403   HOST_WIDE_INT offset;
5404
5405   ix86_compute_frame_layout (&frame);
5406
5407   /* Calculate start of saved registers relative to ebp.  Special care
5408      must be taken for the normal return case of a function using
5409      eh_return: the eax and edx registers are marked as saved, but not
5410      restored along this path.  */
5411   offset = frame.nregs;
5412   if (current_function_calls_eh_return && style != 2)
5413     offset -= 2;
5414   offset *= -UNITS_PER_WORD;
5415
5416   /* If we're only restoring one register and sp is not valid then
5417      using a move instruction to restore the register since it's
5418      less work than reloading sp and popping the register.
5419
5420      The default code result in stack adjustment using add/lea instruction,
5421      while this code results in LEAVE instruction (or discrete equivalent),
5422      so it is profitable in some other cases as well.  Especially when there
5423      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
5424      and there is exactly one register to pop. This heuristic may need some
5425      tuning in future.  */
5426   if ((!sp_valid && frame.nregs <= 1)
5427       || (TARGET_EPILOGUE_USING_MOVE
5428           && cfun->machine->use_fast_prologue_epilogue
5429           && (frame.nregs > 1 || frame.to_allocate))
5430       || (frame_pointer_needed && !frame.nregs && frame.to_allocate)
5431       || (frame_pointer_needed && TARGET_USE_LEAVE
5432           && cfun->machine->use_fast_prologue_epilogue
5433           && frame.nregs == 1)
5434       || current_function_calls_eh_return)
5435     {
5436       /* Restore registers.  We can use ebp or esp to address the memory
5437          locations.  If both are available, default to ebp, since offsets
5438          are known to be small.  Only exception is esp pointing directly to the
5439          end of block of saved registers, where we may simplify addressing
5440          mode.  */
5441
5442       if (!frame_pointer_needed || (sp_valid && !frame.to_allocate))
5443         ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
5444                                           frame.to_allocate, style == 2);
5445       else
5446         ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
5447                                           offset, style == 2);
5448
5449       /* eh_return epilogues need %ecx added to the stack pointer.  */
5450       if (style == 2)
5451         {
5452           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
5453
5454           if (frame_pointer_needed)
5455             {
5456               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
5457               tmp = plus_constant (tmp, UNITS_PER_WORD);
5458               emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
5459
5460               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
5461               emit_move_insn (hard_frame_pointer_rtx, tmp);
5462
5463               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
5464                                          const0_rtx, style);
5465             }
5466           else
5467             {
5468               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
5469               tmp = plus_constant (tmp, (frame.to_allocate
5470                                          + frame.nregs * UNITS_PER_WORD));
5471               emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
5472             }
5473         }
5474       else if (!frame_pointer_needed)
5475         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5476                                    GEN_INT (frame.to_allocate
5477                                             + frame.nregs * UNITS_PER_WORD),
5478                                    style);
5479       /* If not an i386, mov & pop is faster than "leave".  */
5480       else if (TARGET_USE_LEAVE || optimize_size
5481                || !cfun->machine->use_fast_prologue_epilogue)
5482         emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5483       else
5484         {
5485           pro_epilogue_adjust_stack (stack_pointer_rtx,
5486                                      hard_frame_pointer_rtx,
5487                                      const0_rtx, style);
5488           if (TARGET_64BIT)
5489             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5490           else
5491             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5492         }
5493     }
5494   else
5495     {
5496       /* First step is to deallocate the stack frame so that we can
5497          pop the registers.  */
5498       if (!sp_valid)
5499         {
5500           gcc_assert (frame_pointer_needed);
5501           pro_epilogue_adjust_stack (stack_pointer_rtx,
5502                                      hard_frame_pointer_rtx,
5503                                      GEN_INT (offset), style);
5504         }
5505       else if (frame.to_allocate)
5506         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5507                                    GEN_INT (frame.to_allocate), style);
5508
5509       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5510         if (ix86_save_reg (regno, false))
5511           {
5512             if (TARGET_64BIT)
5513               emit_insn (gen_popdi1 (gen_rtx_REG (Pmode, regno)));
5514             else
5515               emit_insn (gen_popsi1 (gen_rtx_REG (Pmode, regno)));
5516           }
5517       if (frame_pointer_needed)
5518         {
5519           /* Leave results in shorter dependency chains on CPUs that are
5520              able to grok it fast.  */
5521           if (TARGET_USE_LEAVE)
5522             emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5523           else if (TARGET_64BIT)
5524             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5525           else
5526             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5527         }
5528     }
5529
5530   if (cfun->machine->force_align_arg_pointer)
5531     {
5532       emit_insn (gen_addsi3 (stack_pointer_rtx,
5533                              cfun->machine->force_align_arg_pointer,
5534                              GEN_INT (-4)));
5535     }
5536
5537   /* Sibcall epilogues don't want a return instruction.  */
5538   if (style == 0)
5539     return;
5540
5541   if (current_function_pops_args && current_function_args_size)
5542     {
5543       rtx popc = GEN_INT (current_function_pops_args);
5544
5545       /* i386 can only pop 64K bytes.  If asked to pop more, pop
5546          return address, do explicit add, and jump indirectly to the
5547          caller.  */
5548
5549       if (current_function_pops_args >= 65536)
5550         {
5551           rtx ecx = gen_rtx_REG (SImode, 2);
5552
5553           /* There is no "pascal" calling convention in 64bit ABI.  */
5554           gcc_assert (!TARGET_64BIT);
5555
5556           emit_insn (gen_popsi1 (ecx));
5557           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, popc));
5558           emit_jump_insn (gen_return_indirect_internal (ecx));
5559         }
5560       else
5561         emit_jump_insn (gen_return_pop_internal (popc));
5562     }
5563   else
5564     emit_jump_insn (gen_return_internal ());
5565 }
5566
5567 /* Reset from the function's potential modifications.  */
5568
5569 static void
5570 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
5571                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
5572 {
5573   if (pic_offset_table_rtx)
5574     REGNO (pic_offset_table_rtx) = REAL_PIC_OFFSET_TABLE_REGNUM;
5575 }
5576 \f
5577 /* Extract the parts of an RTL expression that is a valid memory address
5578    for an instruction.  Return 0 if the structure of the address is
5579    grossly off.  Return -1 if the address contains ASHIFT, so it is not
5580    strictly valid, but still used for computing length of lea instruction.  */
5581
5582 int
5583 ix86_decompose_address (rtx addr, struct ix86_address *out)
5584 {
5585   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
5586   rtx base_reg, index_reg;
5587   HOST_WIDE_INT scale = 1;
5588   rtx scale_rtx = NULL_RTX;
5589   int retval = 1;
5590   enum ix86_address_seg seg = SEG_DEFAULT;
5591
5592   if (GET_CODE (addr) == REG || GET_CODE (addr) == SUBREG)
5593     base = addr;
5594   else if (GET_CODE (addr) == PLUS)
5595     {
5596       rtx addends[4], op;
5597       int n = 0, i;
5598
5599       op = addr;
5600       do
5601         {
5602           if (n >= 4)
5603             return 0;
5604           addends[n++] = XEXP (op, 1);
5605           op = XEXP (op, 0);
5606         }
5607       while (GET_CODE (op) == PLUS);
5608       if (n >= 4)
5609         return 0;
5610       addends[n] = op;
5611
5612       for (i = n; i >= 0; --i)
5613         {
5614           op = addends[i];
5615           switch (GET_CODE (op))
5616             {
5617             case MULT:
5618               if (index)
5619                 return 0;
5620               index = XEXP (op, 0);
5621               scale_rtx = XEXP (op, 1);
5622               break;
5623
5624             case UNSPEC:
5625               if (XINT (op, 1) == UNSPEC_TP
5626                   && TARGET_TLS_DIRECT_SEG_REFS
5627                   && seg == SEG_DEFAULT)
5628                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
5629               else
5630                 return 0;
5631               break;
5632
5633             case REG:
5634             case SUBREG:
5635               if (!base)
5636                 base = op;
5637               else if (!index)
5638                 index = op;
5639               else
5640                 return 0;
5641               break;
5642
5643             case CONST:
5644             case CONST_INT:
5645             case SYMBOL_REF:
5646             case LABEL_REF:
5647               if (disp)
5648                 return 0;
5649               disp = op;
5650               break;
5651
5652             default:
5653               return 0;
5654             }
5655         }
5656     }
5657   else if (GET_CODE (addr) == MULT)
5658     {
5659       index = XEXP (addr, 0);           /* index*scale */
5660       scale_rtx = XEXP (addr, 1);
5661     }
5662   else if (GET_CODE (addr) == ASHIFT)
5663     {
5664       rtx tmp;
5665
5666       /* We're called for lea too, which implements ashift on occasion.  */
5667       index = XEXP (addr, 0);
5668       tmp = XEXP (addr, 1);
5669       if (GET_CODE (tmp) != CONST_INT)
5670         return 0;
5671       scale = INTVAL (tmp);
5672       if ((unsigned HOST_WIDE_INT) scale > 3)
5673         return 0;
5674       scale = 1 << scale;
5675       retval = -1;
5676     }
5677   else
5678     disp = addr;                        /* displacement */
5679
5680   /* Extract the integral value of scale.  */
5681   if (scale_rtx)
5682     {
5683       if (GET_CODE (scale_rtx) != CONST_INT)
5684         return 0;
5685       scale = INTVAL (scale_rtx);
5686     }
5687
5688   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
5689   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
5690
5691   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
5692   if (base_reg && index_reg && scale == 1
5693       && (index_reg == arg_pointer_rtx
5694           || index_reg == frame_pointer_rtx
5695           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
5696     {
5697       rtx tmp;
5698       tmp = base, base = index, index = tmp;
5699       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
5700     }
5701
5702   /* Special case: %ebp cannot be encoded as a base without a displacement.  */
5703   if ((base_reg == hard_frame_pointer_rtx
5704        || base_reg == frame_pointer_rtx
5705        || base_reg == arg_pointer_rtx) && !disp)
5706     disp = const0_rtx;
5707
5708   /* Special case: on K6, [%esi] makes the instruction vector decoded.
5709      Avoid this by transforming to [%esi+0].  */
5710   if (ix86_tune == PROCESSOR_K6 && !optimize_size
5711       && base_reg && !index_reg && !disp
5712       && REG_P (base_reg)
5713       && REGNO_REG_CLASS (REGNO (base_reg)) == SIREG)
5714     disp = const0_rtx;
5715
5716   /* Special case: encode reg+reg instead of reg*2.  */
5717   if (!base && index && scale && scale == 2)
5718     base = index, base_reg = index_reg, scale = 1;
5719
5720   /* Special case: scaling cannot be encoded without base or displacement.  */
5721   if (!base && !disp && index && scale != 1)
5722     disp = const0_rtx;
5723
5724   out->base = base;
5725   out->index = index;
5726   out->disp = disp;
5727   out->scale = scale;
5728   out->seg = seg;
5729
5730   return retval;
5731 }
5732 \f
5733 /* Return cost of the memory address x.
5734    For i386, it is better to use a complex address than let gcc copy
5735    the address into a reg and make a new pseudo.  But not if the address
5736    requires to two regs - that would mean more pseudos with longer
5737    lifetimes.  */
5738 static int
5739 ix86_address_cost (rtx x)
5740 {
5741   struct ix86_address parts;
5742   int cost = 1;
5743   int ok = ix86_decompose_address (x, &parts);
5744
5745   gcc_assert (ok);
5746
5747   if (parts.base && GET_CODE (parts.base) == SUBREG)
5748     parts.base = SUBREG_REG (parts.base);
5749   if (parts.index && GET_CODE (parts.index) == SUBREG)
5750     parts.index = SUBREG_REG (parts.index);
5751
5752   /* More complex memory references are better.  */
5753   if (parts.disp && parts.disp != const0_rtx)
5754     cost--;
5755   if (parts.seg != SEG_DEFAULT)
5756     cost--;
5757
5758   /* Attempt to minimize number of registers in the address.  */
5759   if ((parts.base
5760        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
5761       || (parts.index
5762           && (!REG_P (parts.index)
5763               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
5764     cost++;
5765
5766   if (parts.base
5767       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
5768       && parts.index
5769       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
5770       && parts.base != parts.index)
5771     cost++;
5772
5773   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
5774      since it's predecode logic can't detect the length of instructions
5775      and it degenerates to vector decoded.  Increase cost of such
5776      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
5777      to split such addresses or even refuse such addresses at all.
5778
5779      Following addressing modes are affected:
5780       [base+scale*index]
5781       [scale*index+disp]
5782       [base+index]
5783
5784      The first and last case  may be avoidable by explicitly coding the zero in
5785      memory address, but I don't have AMD-K6 machine handy to check this
5786      theory.  */
5787
5788   if (TARGET_K6
5789       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
5790           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
5791           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
5792     cost += 10;
5793
5794   return cost;
5795 }
5796 \f
5797 /* If X is a machine specific address (i.e. a symbol or label being
5798    referenced as a displacement from the GOT implemented using an
5799    UNSPEC), then return the base term.  Otherwise return X.  */
5800
5801 rtx
5802 ix86_find_base_term (rtx x)
5803 {
5804   rtx term;
5805
5806   if (TARGET_64BIT)
5807     {
5808       if (GET_CODE (x) != CONST)
5809         return x;
5810       term = XEXP (x, 0);
5811       if (GET_CODE (term) == PLUS
5812           && (GET_CODE (XEXP (term, 1)) == CONST_INT
5813               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
5814         term = XEXP (term, 0);
5815       if (GET_CODE (term) != UNSPEC
5816           || XINT (term, 1) != UNSPEC_GOTPCREL)
5817         return x;
5818
5819       term = XVECEXP (term, 0, 0);
5820
5821       if (GET_CODE (term) != SYMBOL_REF
5822           && GET_CODE (term) != LABEL_REF)
5823         return x;
5824
5825       return term;
5826     }
5827
5828   term = ix86_delegitimize_address (x);
5829
5830   if (GET_CODE (term) != SYMBOL_REF
5831       && GET_CODE (term) != LABEL_REF)
5832     return x;
5833
5834   return term;
5835 }
5836
5837 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
5838    this is used for to form addresses to local data when -fPIC is in
5839    use.  */
5840
5841 static bool
5842 darwin_local_data_pic (rtx disp)
5843 {
5844   if (GET_CODE (disp) == MINUS)
5845     {
5846       if (GET_CODE (XEXP (disp, 0)) == LABEL_REF
5847           || GET_CODE (XEXP (disp, 0)) == SYMBOL_REF)
5848         if (GET_CODE (XEXP (disp, 1)) == SYMBOL_REF)
5849           {
5850             const char *sym_name = XSTR (XEXP (disp, 1), 0);
5851             if (! strcmp (sym_name, "<pic base>"))
5852               return true;
5853           }
5854     }
5855
5856   return false;
5857 }
5858 \f
5859 /* Determine if a given RTX is a valid constant.  We already know this
5860    satisfies CONSTANT_P.  */
5861
5862 bool
5863 legitimate_constant_p (rtx x)
5864 {
5865   switch (GET_CODE (x))
5866     {
5867     case CONST:
5868       x = XEXP (x, 0);
5869
5870       if (GET_CODE (x) == PLUS)
5871         {
5872           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5873             return false;
5874           x = XEXP (x, 0);
5875         }
5876
5877       if (TARGET_MACHO && darwin_local_data_pic (x))
5878         return true;
5879
5880       /* Only some unspecs are valid as "constants".  */
5881       if (GET_CODE (x) == UNSPEC)
5882         switch (XINT (x, 1))
5883           {
5884           case UNSPEC_GOTOFF:
5885             return TARGET_64BIT;
5886           case UNSPEC_TPOFF:
5887           case UNSPEC_NTPOFF:
5888             x = XVECEXP (x, 0, 0);
5889             return (GET_CODE (x) == SYMBOL_REF
5890                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
5891           case UNSPEC_DTPOFF:
5892             x = XVECEXP (x, 0, 0);
5893             return (GET_CODE (x) == SYMBOL_REF
5894                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
5895           default:
5896             return false;
5897           }
5898
5899       /* We must have drilled down to a symbol.  */
5900       if (GET_CODE (x) == LABEL_REF)
5901         return true;
5902       if (GET_CODE (x) != SYMBOL_REF)
5903         return false;
5904       /* FALLTHRU */
5905
5906     case SYMBOL_REF:
5907       /* TLS symbols are never valid.  */
5908       if (SYMBOL_REF_TLS_MODEL (x))
5909         return false;
5910       break;
5911
5912     default:
5913       break;
5914     }
5915
5916   /* Otherwise we handle everything else in the move patterns.  */
5917   return true;
5918 }
5919
5920 /* Determine if it's legal to put X into the constant pool.  This
5921    is not possible for the address of thread-local symbols, which
5922    is checked above.  */
5923
5924 static bool
5925 ix86_cannot_force_const_mem (rtx x)
5926 {
5927   return !legitimate_constant_p (x);
5928 }
5929
5930 /* Determine if a given RTX is a valid constant address.  */
5931
5932 bool
5933 constant_address_p (rtx x)
5934 {
5935   return CONSTANT_P (x) && legitimate_address_p (Pmode, x, 1);
5936 }
5937
5938 /* Nonzero if the constant value X is a legitimate general operand
5939    when generating PIC code.  It is given that flag_pic is on and
5940    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
5941
5942 bool
5943 legitimate_pic_operand_p (rtx x)
5944 {
5945   rtx inner;
5946
5947   switch (GET_CODE (x))
5948     {
5949     case CONST:
5950       inner = XEXP (x, 0);
5951       if (GET_CODE (inner) == PLUS
5952           && GET_CODE (XEXP (inner, 1)) == CONST_INT)
5953         inner = XEXP (inner, 0);
5954
5955       /* Only some unspecs are valid as "constants".  */
5956       if (GET_CODE (inner) == UNSPEC)
5957         switch (XINT (inner, 1))
5958           {
5959           case UNSPEC_GOTOFF:
5960             return TARGET_64BIT;
5961           case UNSPEC_TPOFF:
5962             x = XVECEXP (inner, 0, 0);
5963             return (GET_CODE (x) == SYMBOL_REF
5964                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
5965           default:
5966             return false;
5967           }
5968       /* FALLTHRU */
5969
5970     case SYMBOL_REF:
5971     case LABEL_REF:
5972       return legitimate_pic_address_disp_p (x);
5973
5974     default:
5975       return true;
5976     }
5977 }
5978
5979 /* Determine if a given CONST RTX is a valid memory displacement
5980    in PIC mode.  */
5981
5982 int
5983 legitimate_pic_address_disp_p (rtx disp)
5984 {
5985   bool saw_plus;
5986
5987   /* In 64bit mode we can allow direct addresses of symbols and labels
5988      when they are not dynamic symbols.  */
5989   if (TARGET_64BIT)
5990     {
5991       rtx op0 = disp, op1;
5992
5993       switch (GET_CODE (disp))
5994         {
5995         case LABEL_REF:
5996           return true;
5997
5998         case CONST:
5999           if (GET_CODE (XEXP (disp, 0)) != PLUS)
6000             break;
6001           op0 = XEXP (XEXP (disp, 0), 0);
6002           op1 = XEXP (XEXP (disp, 0), 1);
6003           if (GET_CODE (op1) != CONST_INT
6004               || INTVAL (op1) >= 16*1024*1024
6005               || INTVAL (op1) < -16*1024*1024)
6006             break;
6007           if (GET_CODE (op0) == LABEL_REF)
6008             return true;
6009           if (GET_CODE (op0) != SYMBOL_REF)
6010             break;
6011           /* FALLTHRU */
6012
6013         case SYMBOL_REF:
6014           /* TLS references should always be enclosed in UNSPEC.  */
6015           if (SYMBOL_REF_TLS_MODEL (op0))
6016             return false;
6017           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0))
6018             return true;
6019           break;
6020
6021         default:
6022           break;
6023         }
6024     }
6025   if (GET_CODE (disp) != CONST)
6026     return 0;
6027   disp = XEXP (disp, 0);
6028
6029   if (TARGET_64BIT)
6030     {
6031       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
6032          of GOT tables.  We should not need these anyway.  */
6033       if (GET_CODE (disp) != UNSPEC
6034           || (XINT (disp, 1) != UNSPEC_GOTPCREL
6035               && XINT (disp, 1) != UNSPEC_GOTOFF))
6036         return 0;
6037
6038       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
6039           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
6040         return 0;
6041       return 1;
6042     }
6043
6044   saw_plus = false;
6045   if (GET_CODE (disp) == PLUS)
6046     {
6047       if (GET_CODE (XEXP (disp, 1)) != CONST_INT)
6048         return 0;
6049       disp = XEXP (disp, 0);
6050       saw_plus = true;
6051     }
6052
6053   if (TARGET_MACHO && darwin_local_data_pic (disp))
6054     return 1;
6055
6056   if (GET_CODE (disp) != UNSPEC)
6057     return 0;
6058
6059   switch (XINT (disp, 1))
6060     {
6061     case UNSPEC_GOT:
6062       if (saw_plus)
6063         return false;
6064       return GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF;
6065     case UNSPEC_GOTOFF:
6066       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
6067          While ABI specify also 32bit relocation but we don't produce it in
6068          small PIC model at all.  */
6069       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
6070            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
6071           && !TARGET_64BIT)
6072         return local_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
6073       return false;
6074     case UNSPEC_GOTTPOFF:
6075     case UNSPEC_GOTNTPOFF:
6076     case UNSPEC_INDNTPOFF:
6077       if (saw_plus)
6078         return false;
6079       disp = XVECEXP (disp, 0, 0);
6080       return (GET_CODE (disp) == SYMBOL_REF
6081               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
6082     case UNSPEC_NTPOFF:
6083       disp = XVECEXP (disp, 0, 0);
6084       return (GET_CODE (disp) == SYMBOL_REF
6085               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
6086     case UNSPEC_DTPOFF:
6087       disp = XVECEXP (disp, 0, 0);
6088       return (GET_CODE (disp) == SYMBOL_REF
6089               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
6090     }
6091
6092   return 0;
6093 }
6094
6095 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression that is a valid
6096    memory address for an instruction.  The MODE argument is the machine mode
6097    for the MEM expression that wants to use this address.
6098
6099    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
6100    convert common non-canonical forms to canonical form so that they will
6101    be recognized.  */
6102
6103 int
6104 legitimate_address_p (enum machine_mode mode, rtx addr, int strict)
6105 {
6106   struct ix86_address parts;
6107   rtx base, index, disp;
6108   HOST_WIDE_INT scale;
6109   const char *reason = NULL;
6110   rtx reason_rtx = NULL_RTX;
6111
6112   if (TARGET_DEBUG_ADDR)
6113     {
6114       fprintf (stderr,
6115                "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
6116                GET_MODE_NAME (mode), strict);
6117       debug_rtx (addr);
6118     }
6119
6120   if (ix86_decompose_address (addr, &parts) <= 0)
6121     {
6122       reason = "decomposition failed";
6123       goto report_error;
6124     }
6125
6126   base = parts.base;
6127   index = parts.index;
6128   disp = parts.disp;
6129   scale = parts.scale;
6130
6131   /* Validate base register.
6132
6133      Don't allow SUBREG's that span more than a word here.  It can lead to spill
6134      failures when the base is one word out of a two word structure, which is
6135      represented internally as a DImode int.  */
6136
6137   if (base)
6138     {
6139       rtx reg;
6140       reason_rtx = base;
6141   
6142       if (REG_P (base))
6143         reg = base;
6144       else if (GET_CODE (base) == SUBREG
6145                && REG_P (SUBREG_REG (base))
6146                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
6147                   <= UNITS_PER_WORD)
6148         reg = SUBREG_REG (base);
6149       else
6150         {
6151           reason = "base is not a register";
6152           goto report_error;
6153         }
6154
6155       if (GET_MODE (base) != Pmode)
6156         {
6157           reason = "base is not in Pmode";
6158           goto report_error;
6159         }
6160
6161       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
6162           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
6163         {
6164           reason = "base is not valid";
6165           goto report_error;
6166         }
6167     }
6168
6169   /* Validate index register.
6170
6171      Don't allow SUBREG's that span more than a word here -- same as above.  */
6172
6173   if (index)
6174     {
6175       rtx reg;
6176       reason_rtx = index;
6177
6178       if (REG_P (index))
6179         reg = index;
6180       else if (GET_CODE (index) == SUBREG
6181                && REG_P (SUBREG_REG (index))
6182                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
6183                   <= UNITS_PER_WORD)
6184         reg = SUBREG_REG (index);
6185       else
6186         {
6187           reason = "index is not a register";
6188           goto report_error;
6189         }
6190
6191       if (GET_MODE (index) != Pmode)
6192         {
6193           reason = "index is not in Pmode";
6194           goto report_error;
6195         }
6196
6197       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
6198           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
6199         {
6200           reason = "index is not valid";
6201           goto report_error;
6202         }
6203     }
6204
6205   /* Validate scale factor.  */
6206   if (scale != 1)
6207     {
6208       reason_rtx = GEN_INT (scale);
6209       if (!index)
6210         {
6211           reason = "scale without index";
6212           goto report_error;
6213         }
6214
6215       if (scale != 2 && scale != 4 && scale != 8)
6216         {
6217           reason = "scale is not a valid multiplier";
6218           goto report_error;
6219         }
6220     }
6221
6222   /* Validate displacement.  */
6223   if (disp)
6224     {
6225       reason_rtx = disp;
6226
6227       if (GET_CODE (disp) == CONST
6228           && GET_CODE (XEXP (disp, 0)) == UNSPEC)
6229         switch (XINT (XEXP (disp, 0), 1))
6230           {
6231           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
6232              used.  While ABI specify also 32bit relocations, we don't produce
6233              them at all and use IP relative instead.  */
6234           case UNSPEC_GOT:
6235           case UNSPEC_GOTOFF:
6236             gcc_assert (flag_pic);
6237             if (!TARGET_64BIT)
6238               goto is_legitimate_pic;
6239             reason = "64bit address unspec";
6240             goto report_error;
6241  
6242           case UNSPEC_GOTPCREL:
6243             gcc_assert (flag_pic);
6244             goto is_legitimate_pic;
6245
6246           case UNSPEC_GOTTPOFF:
6247           case UNSPEC_GOTNTPOFF:
6248           case UNSPEC_INDNTPOFF:
6249           case UNSPEC_NTPOFF:
6250           case UNSPEC_DTPOFF:
6251             break;
6252
6253           default:
6254             reason = "invalid address unspec";
6255             goto report_error;
6256           }
6257
6258       else if (flag_pic && (SYMBOLIC_CONST (disp)
6259 #if TARGET_MACHO
6260                             && !machopic_operand_p (disp)
6261 #endif
6262                             ))
6263         {
6264         is_legitimate_pic:
6265           if (TARGET_64BIT && (index || base))
6266             {
6267               /* foo@dtpoff(%rX) is ok.  */
6268               if (GET_CODE (disp) != CONST
6269                   || GET_CODE (XEXP (disp, 0)) != PLUS
6270                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
6271                   || GET_CODE (XEXP (XEXP (disp, 0), 1)) != CONST_INT
6272                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
6273                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
6274                 {
6275                   reason = "non-constant pic memory reference";
6276                   goto report_error;
6277                 }
6278             }
6279           else if (! legitimate_pic_address_disp_p (disp))
6280             {
6281               reason = "displacement is an invalid pic construct";
6282               goto report_error;
6283             }
6284
6285           /* This code used to verify that a symbolic pic displacement
6286              includes the pic_offset_table_rtx register.
6287
6288              While this is good idea, unfortunately these constructs may
6289              be created by "adds using lea" optimization for incorrect
6290              code like:
6291
6292              int a;
6293              int foo(int i)
6294                {
6295                  return *(&a+i);
6296                }
6297
6298              This code is nonsensical, but results in addressing
6299              GOT table with pic_offset_table_rtx base.  We can't
6300              just refuse it easily, since it gets matched by
6301              "addsi3" pattern, that later gets split to lea in the
6302              case output register differs from input.  While this
6303              can be handled by separate addsi pattern for this case
6304              that never results in lea, this seems to be easier and
6305              correct fix for crash to disable this test.  */
6306         }
6307       else if (GET_CODE (disp) != LABEL_REF
6308                && GET_CODE (disp) != CONST_INT
6309                && (GET_CODE (disp) != CONST
6310                    || !legitimate_constant_p (disp))
6311                && (GET_CODE (disp) != SYMBOL_REF
6312                    || !legitimate_constant_p (disp)))
6313         {
6314           reason = "displacement is not constant";
6315           goto report_error;
6316         }
6317       else if (TARGET_64BIT
6318                && !x86_64_immediate_operand (disp, VOIDmode))
6319         {
6320           reason = "displacement is out of range";
6321           goto report_error;
6322         }
6323     }
6324
6325   /* Everything looks valid.  */
6326   if (TARGET_DEBUG_ADDR)
6327     fprintf (stderr, "Success.\n");
6328   return TRUE;
6329
6330  report_error:
6331   if (TARGET_DEBUG_ADDR)
6332     {
6333       fprintf (stderr, "Error: %s\n", reason);
6334       debug_rtx (reason_rtx);
6335     }
6336   return FALSE;
6337 }
6338 \f
6339 /* Return a unique alias set for the GOT.  */
6340
6341 static HOST_WIDE_INT
6342 ix86_GOT_alias_set (void)
6343 {
6344   static HOST_WIDE_INT set = -1;
6345   if (set == -1)
6346     set = new_alias_set ();
6347   return set;
6348 }
6349
6350 /* Return a legitimate reference for ORIG (an address) using the
6351    register REG.  If REG is 0, a new pseudo is generated.
6352
6353    There are two types of references that must be handled:
6354
6355    1. Global data references must load the address from the GOT, via
6356       the PIC reg.  An insn is emitted to do this load, and the reg is
6357       returned.
6358
6359    2. Static data references, constant pool addresses, and code labels
6360       compute the address as an offset from the GOT, whose base is in
6361       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
6362       differentiate them from global data objects.  The returned
6363       address is the PIC reg + an unspec constant.
6364
6365    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
6366    reg also appears in the address.  */
6367
6368 static rtx
6369 legitimize_pic_address (rtx orig, rtx reg)
6370 {
6371   rtx addr = orig;
6372   rtx new = orig;
6373   rtx base;
6374
6375 #if TARGET_MACHO
6376   if (reg == 0)
6377     reg = gen_reg_rtx (Pmode);
6378   /* Use the generic Mach-O PIC machinery.  */
6379   return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
6380 #endif
6381
6382   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
6383     new = addr;
6384   else if (TARGET_64BIT
6385            && ix86_cmodel != CM_SMALL_PIC
6386            && local_symbolic_operand (addr, Pmode))
6387     {
6388       rtx tmpreg;
6389       /* This symbol may be referenced via a displacement from the PIC
6390          base address (@GOTOFF).  */
6391
6392       if (reload_in_progress)
6393         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6394       if (GET_CODE (addr) == CONST)
6395         addr = XEXP (addr, 0);
6396       if (GET_CODE (addr) == PLUS)
6397           {
6398             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
6399             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
6400           }
6401         else
6402           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
6403       new = gen_rtx_CONST (Pmode, new);
6404       if (!reg)
6405         tmpreg = gen_reg_rtx (Pmode);
6406       else
6407         tmpreg = reg;
6408       emit_move_insn (tmpreg, new);
6409
6410       if (reg != 0)
6411         {
6412           new = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
6413                                      tmpreg, 1, OPTAB_DIRECT);
6414           new = reg;
6415         }
6416       else new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
6417     }
6418   else if (!TARGET_64BIT && local_symbolic_operand (addr, Pmode))
6419     {
6420       /* This symbol may be referenced via a displacement from the PIC
6421          base address (@GOTOFF).  */
6422
6423       if (reload_in_progress)
6424         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6425       if (GET_CODE (addr) == CONST)
6426         addr = XEXP (addr, 0);
6427       if (GET_CODE (addr) == PLUS)
6428           {
6429             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
6430             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
6431           }
6432         else
6433           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
6434       new = gen_rtx_CONST (Pmode, new);
6435       new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6436
6437       if (reg != 0)
6438         {
6439           emit_move_insn (reg, new);
6440           new = reg;
6441         }
6442     }
6443   else if (GET_CODE (addr) == SYMBOL_REF)
6444     {
6445       if (TARGET_64BIT)
6446         {
6447           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
6448           new = gen_rtx_CONST (Pmode, new);
6449           new = gen_const_mem (Pmode, new);
6450           set_mem_alias_set (new, ix86_GOT_alias_set ());
6451
6452           if (reg == 0)
6453             reg = gen_reg_rtx (Pmode);
6454           /* Use directly gen_movsi, otherwise the address is loaded
6455              into register for CSE.  We don't want to CSE this addresses,
6456              instead we CSE addresses from the GOT table, so skip this.  */
6457           emit_insn (gen_movsi (reg, new));
6458           new = reg;
6459         }
6460       else
6461         {
6462           /* This symbol must be referenced via a load from the
6463              Global Offset Table (@GOT).  */
6464
6465           if (reload_in_progress)
6466             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6467           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
6468           new = gen_rtx_CONST (Pmode, new);
6469           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6470           new = gen_const_mem (Pmode, new);
6471           set_mem_alias_set (new, ix86_GOT_alias_set ());
6472
6473           if (reg == 0)
6474             reg = gen_reg_rtx (Pmode);
6475           emit_move_insn (reg, new);
6476           new = reg;
6477         }
6478     }
6479   else
6480     {
6481       if (GET_CODE (addr) == CONST_INT
6482           && !x86_64_immediate_operand (addr, VOIDmode))
6483         {
6484           if (reg)
6485             {
6486               emit_move_insn (reg, addr);
6487               new = reg;
6488             }
6489           else
6490             new = force_reg (Pmode, addr);
6491         }
6492       else if (GET_CODE (addr) == CONST)
6493         {
6494           addr = XEXP (addr, 0);
6495
6496           /* We must match stuff we generate before.  Assume the only
6497              unspecs that can get here are ours.  Not that we could do
6498              anything with them anyway....  */
6499           if (GET_CODE (addr) == UNSPEC
6500               || (GET_CODE (addr) == PLUS
6501                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
6502             return orig;
6503           gcc_assert (GET_CODE (addr) == PLUS);
6504         }
6505       if (GET_CODE (addr) == PLUS)
6506         {
6507           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
6508
6509           /* Check first to see if this is a constant offset from a @GOTOFF
6510              symbol reference.  */
6511           if (local_symbolic_operand (op0, Pmode)
6512               && GET_CODE (op1) == CONST_INT)
6513             {
6514               if (!TARGET_64BIT)
6515                 {
6516                   if (reload_in_progress)
6517                     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6518                   new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
6519                                         UNSPEC_GOTOFF);
6520                   new = gen_rtx_PLUS (Pmode, new, op1);
6521                   new = gen_rtx_CONST (Pmode, new);
6522                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6523
6524                   if (reg != 0)
6525                     {
6526                       emit_move_insn (reg, new);
6527                       new = reg;
6528                     }
6529                 }
6530               else
6531                 {
6532                   if (INTVAL (op1) < -16*1024*1024
6533                       || INTVAL (op1) >= 16*1024*1024)
6534                     {
6535                       if (!x86_64_immediate_operand (op1, Pmode))
6536                         op1 = force_reg (Pmode, op1);
6537                       new = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
6538                     }
6539                 }
6540             }
6541           else
6542             {
6543               base = legitimize_pic_address (XEXP (addr, 0), reg);
6544               new  = legitimize_pic_address (XEXP (addr, 1),
6545                                              base == reg ? NULL_RTX : reg);
6546
6547               if (GET_CODE (new) == CONST_INT)
6548                 new = plus_constant (base, INTVAL (new));
6549               else
6550                 {
6551                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
6552                     {
6553                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
6554                       new = XEXP (new, 1);
6555                     }
6556                   new = gen_rtx_PLUS (Pmode, base, new);
6557                 }
6558             }
6559         }
6560     }
6561   return new;
6562 }
6563 \f
6564 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
6565
6566 static rtx
6567 get_thread_pointer (int to_reg)
6568 {
6569   rtx tp, reg, insn;
6570
6571   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
6572   if (!to_reg)
6573     return tp;
6574
6575   reg = gen_reg_rtx (Pmode);
6576   insn = gen_rtx_SET (VOIDmode, reg, tp);
6577   insn = emit_insn (insn);
6578
6579   return reg;
6580 }
6581
6582 /* A subroutine of legitimize_address and ix86_expand_move.  FOR_MOV is
6583    false if we expect this to be used for a memory address and true if
6584    we expect to load the address into a register.  */
6585
6586 static rtx
6587 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
6588 {
6589   rtx dest, base, off, pic, tp;
6590   int type;
6591
6592   switch (model)
6593     {
6594     case TLS_MODEL_GLOBAL_DYNAMIC:
6595       dest = gen_reg_rtx (Pmode);
6596       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
6597
6598       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
6599         {
6600           rtx rax = gen_rtx_REG (Pmode, 0), insns;
6601
6602           start_sequence ();
6603           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
6604           insns = get_insns ();
6605           end_sequence ();
6606
6607           emit_libcall_block (insns, dest, rax, x);
6608         }
6609       else if (TARGET_64BIT && TARGET_GNU2_TLS)
6610         emit_insn (gen_tls_global_dynamic_64 (dest, x));
6611       else
6612         emit_insn (gen_tls_global_dynamic_32 (dest, x));
6613
6614       if (TARGET_GNU2_TLS)
6615         {
6616           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
6617
6618           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
6619         }
6620       break;
6621
6622     case TLS_MODEL_LOCAL_DYNAMIC:
6623       base = gen_reg_rtx (Pmode);
6624       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
6625
6626       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
6627         {
6628           rtx rax = gen_rtx_REG (Pmode, 0), insns, note;
6629
6630           start_sequence ();
6631           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
6632           insns = get_insns ();
6633           end_sequence ();
6634
6635           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
6636           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
6637           emit_libcall_block (insns, base, rax, note);
6638         }
6639       else if (TARGET_64BIT && TARGET_GNU2_TLS)
6640         emit_insn (gen_tls_local_dynamic_base_64 (base));
6641       else
6642         emit_insn (gen_tls_local_dynamic_base_32 (base));
6643
6644       if (TARGET_GNU2_TLS)
6645         {
6646           rtx x = ix86_tls_module_base ();
6647
6648           base = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, base));
6649
6650           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
6651         }
6652
6653       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
6654       off = gen_rtx_CONST (Pmode, off);
6655
6656       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
6657       break;
6658
6659     case TLS_MODEL_INITIAL_EXEC:
6660       if (TARGET_64BIT)
6661         {
6662           pic = NULL;
6663           type = UNSPEC_GOTNTPOFF;
6664         }
6665       else if (flag_pic)
6666         {
6667           if (reload_in_progress)
6668             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6669           pic = pic_offset_table_rtx;
6670           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
6671         }
6672       else if (!TARGET_ANY_GNU_TLS)
6673         {
6674           pic = gen_reg_rtx (Pmode);
6675           emit_insn (gen_set_got (pic));
6676           type = UNSPEC_GOTTPOFF;
6677         }
6678       else
6679         {
6680           pic = NULL;
6681           type = UNSPEC_INDNTPOFF;
6682         }
6683
6684       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
6685       off = gen_rtx_CONST (Pmode, off);
6686       if (pic)
6687         off = gen_rtx_PLUS (Pmode, pic, off);
6688       off = gen_const_mem (Pmode, off);
6689       set_mem_alias_set (off, ix86_GOT_alias_set ());
6690
6691       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
6692         {
6693           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
6694           off = force_reg (Pmode, off);
6695           return gen_rtx_PLUS (Pmode, base, off);
6696         }
6697       else
6698         {
6699           base = get_thread_pointer (true);
6700           dest = gen_reg_rtx (Pmode);
6701           emit_insn (gen_subsi3 (dest, base, off));
6702         }
6703       break;
6704
6705     case TLS_MODEL_LOCAL_EXEC:
6706       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
6707                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
6708                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
6709       off = gen_rtx_CONST (Pmode, off);
6710
6711       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
6712         {
6713           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
6714           return gen_rtx_PLUS (Pmode, base, off);
6715         }
6716       else
6717         {
6718           base = get_thread_pointer (true);
6719           dest = gen_reg_rtx (Pmode);
6720           emit_insn (gen_subsi3 (dest, base, off));
6721         }
6722       break;
6723
6724     default:
6725       gcc_unreachable ();
6726     }
6727
6728   return dest;
6729 }
6730
6731 /* Try machine-dependent ways of modifying an illegitimate address
6732    to be legitimate.  If we find one, return the new, valid address.
6733    This macro is used in only one place: `memory_address' in explow.c.
6734
6735    OLDX is the address as it was before break_out_memory_refs was called.
6736    In some cases it is useful to look at this to decide what needs to be done.
6737
6738    MODE and WIN are passed so that this macro can use
6739    GO_IF_LEGITIMATE_ADDRESS.
6740
6741    It is always safe for this macro to do nothing.  It exists to recognize
6742    opportunities to optimize the output.
6743
6744    For the 80386, we handle X+REG by loading X into a register R and
6745    using R+REG.  R will go in a general reg and indexing will be used.
6746    However, if REG is a broken-out memory address or multiplication,
6747    nothing needs to be done because REG can certainly go in a general reg.
6748
6749    When -fpic is used, special handling is needed for symbolic references.
6750    See comments by legitimize_pic_address in i386.c for details.  */
6751
6752 rtx
6753 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED, enum machine_mode mode)
6754 {
6755   int changed = 0;
6756   unsigned log;
6757
6758   if (TARGET_DEBUG_ADDR)
6759     {
6760       fprintf (stderr, "\n==========\nLEGITIMIZE_ADDRESS, mode = %s\n",
6761                GET_MODE_NAME (mode));
6762       debug_rtx (x);
6763     }
6764
6765   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
6766   if (log)
6767     return legitimize_tls_address (x, log, false);
6768   if (GET_CODE (x) == CONST
6769       && GET_CODE (XEXP (x, 0)) == PLUS
6770       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6771       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
6772     {
6773       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0), log, false);
6774       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
6775     }
6776
6777   if (flag_pic && SYMBOLIC_CONST (x))
6778     return legitimize_pic_address (x, 0);
6779
6780   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
6781   if (GET_CODE (x) == ASHIFT
6782       && GET_CODE (XEXP (x, 1)) == CONST_INT
6783       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
6784     {
6785       changed = 1;
6786       log = INTVAL (XEXP (x, 1));
6787       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
6788                         GEN_INT (1 << log));
6789     }
6790
6791   if (GET_CODE (x) == PLUS)
6792     {
6793       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
6794
6795       if (GET_CODE (XEXP (x, 0)) == ASHIFT
6796           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6797           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
6798         {
6799           changed = 1;
6800           log = INTVAL (XEXP (XEXP (x, 0), 1));
6801           XEXP (x, 0) = gen_rtx_MULT (Pmode,
6802                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
6803                                       GEN_INT (1 << log));
6804         }
6805
6806       if (GET_CODE (XEXP (x, 1)) == ASHIFT
6807           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
6808           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
6809         {
6810           changed = 1;
6811           log = INTVAL (XEXP (XEXP (x, 1), 1));
6812           XEXP (x, 1) = gen_rtx_MULT (Pmode,
6813                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
6814                                       GEN_INT (1 << log));
6815         }
6816
6817       /* Put multiply first if it isn't already.  */
6818       if (GET_CODE (XEXP (x, 1)) == MULT)
6819         {
6820           rtx tmp = XEXP (x, 0);
6821           XEXP (x, 0) = XEXP (x, 1);
6822           XEXP (x, 1) = tmp;
6823           changed = 1;
6824         }
6825
6826       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
6827          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
6828          created by virtual register instantiation, register elimination, and
6829          similar optimizations.  */
6830       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
6831         {
6832           changed = 1;
6833           x = gen_rtx_PLUS (Pmode,
6834                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
6835                                           XEXP (XEXP (x, 1), 0)),
6836                             XEXP (XEXP (x, 1), 1));
6837         }
6838
6839       /* Canonicalize
6840          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
6841          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
6842       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
6843                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
6844                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
6845                && CONSTANT_P (XEXP (x, 1)))
6846         {
6847           rtx constant;
6848           rtx other = NULL_RTX;
6849
6850           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6851             {
6852               constant = XEXP (x, 1);
6853               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
6854             }
6855           else if (GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 1)) == CONST_INT)
6856             {
6857               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
6858               other = XEXP (x, 1);
6859             }
6860           else
6861             constant = 0;
6862
6863           if (constant)
6864             {
6865               changed = 1;
6866               x = gen_rtx_PLUS (Pmode,
6867                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
6868                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
6869                                 plus_constant (other, INTVAL (constant)));
6870             }
6871         }
6872
6873       if (changed && legitimate_address_p (mode, x, FALSE))
6874         return x;
6875
6876       if (GET_CODE (XEXP (x, 0)) == MULT)
6877         {
6878           changed = 1;
6879           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
6880         }
6881
6882       if (GET_CODE (XEXP (x, 1)) == MULT)
6883         {
6884           changed = 1;
6885           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
6886         }
6887
6888       if (changed
6889           && GET_CODE (XEXP (x, 1)) == REG
6890           && GET_CODE (XEXP (x, 0)) == REG)
6891         return x;
6892
6893       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
6894         {
6895           changed = 1;
6896           x = legitimize_pic_address (x, 0);
6897         }
6898
6899       if (changed && legitimate_address_p (mode, x, FALSE))
6900         return x;
6901
6902       if (GET_CODE (XEXP (x, 0)) == REG)
6903         {
6904           rtx temp = gen_reg_rtx (Pmode);
6905           rtx val  = force_operand (XEXP (x, 1), temp);
6906           if (val != temp)
6907             emit_move_insn (temp, val);
6908
6909           XEXP (x, 1) = temp;
6910           return x;
6911         }
6912
6913       else if (GET_CODE (XEXP (x, 1)) == REG)
6914         {
6915           rtx temp = gen_reg_rtx (Pmode);
6916           rtx val  = force_operand (XEXP (x, 0), temp);
6917           if (val != temp)
6918             emit_move_insn (temp, val);
6919
6920           XEXP (x, 0) = temp;
6921           return x;
6922         }
6923     }
6924
6925   return x;
6926 }
6927 \f
6928 /* Print an integer constant expression in assembler syntax.  Addition
6929    and subtraction are the only arithmetic that may appear in these
6930    expressions.  FILE is the stdio stream to write to, X is the rtx, and
6931    CODE is the operand print code from the output string.  */
6932
6933 static void
6934 output_pic_addr_const (FILE *file, rtx x, int code)
6935 {
6936   char buf[256];
6937
6938   switch (GET_CODE (x))
6939     {
6940     case PC:
6941       gcc_assert (flag_pic);
6942       putc ('.', file);
6943       break;
6944
6945     case SYMBOL_REF:
6946       output_addr_const (file, x);
6947       if (!TARGET_MACHO && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
6948         fputs ("@PLT", file);
6949       break;
6950
6951     case LABEL_REF:
6952       x = XEXP (x, 0);
6953       /* FALLTHRU */
6954     case CODE_LABEL:
6955       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
6956       assemble_name (asm_out_file, buf);
6957       break;
6958
6959     case CONST_INT:
6960       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
6961       break;
6962
6963     case CONST:
6964       /* This used to output parentheses around the expression,
6965          but that does not work on the 386 (either ATT or BSD assembler).  */
6966       output_pic_addr_const (file, XEXP (x, 0), code);
6967       break;
6968
6969     case CONST_DOUBLE:
6970       if (GET_MODE (x) == VOIDmode)
6971         {
6972           /* We can use %d if the number is <32 bits and positive.  */
6973           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
6974             fprintf (file, "0x%lx%08lx",
6975                      (unsigned long) CONST_DOUBLE_HIGH (x),
6976                      (unsigned long) CONST_DOUBLE_LOW (x));
6977           else
6978             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
6979         }
6980       else
6981         /* We can't handle floating point constants;
6982            PRINT_OPERAND must handle them.  */
6983         output_operand_lossage ("floating constant misused");
6984       break;
6985
6986     case PLUS:
6987       /* Some assemblers need integer constants to appear first.  */
6988       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
6989         {
6990           output_pic_addr_const (file, XEXP (x, 0), code);
6991           putc ('+', file);
6992           output_pic_addr_const (file, XEXP (x, 1), code);
6993         }
6994       else 
6995         {
6996           gcc_assert (GET_CODE (XEXP (x, 1)) == CONST_INT);
6997           output_pic_addr_const (file, XEXP (x, 1), code);
6998           putc ('+', file);
6999           output_pic_addr_const (file, XEXP (x, 0), code);
7000         }
7001       break;
7002
7003     case MINUS:
7004       if (!TARGET_MACHO)
7005         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
7006       output_pic_addr_const (file, XEXP (x, 0), code);
7007       putc ('-', file);
7008       output_pic_addr_const (file, XEXP (x, 1), code);
7009       if (!TARGET_MACHO)
7010         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
7011       break;
7012
7013      case UNSPEC:
7014        gcc_assert (XVECLEN (x, 0) == 1);
7015        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
7016        switch (XINT (x, 1))
7017         {
7018         case UNSPEC_GOT:
7019           fputs ("@GOT", file);
7020           break;
7021         case UNSPEC_GOTOFF:
7022           fputs ("@GOTOFF", file);
7023           break;
7024         case UNSPEC_GOTPCREL:
7025           fputs ("@GOTPCREL(%rip)", file);
7026           break;
7027         case UNSPEC_GOTTPOFF:
7028           /* FIXME: This might be @TPOFF in Sun ld too.  */
7029           fputs ("@GOTTPOFF", file);
7030           break;
7031         case UNSPEC_TPOFF:
7032           fputs ("@TPOFF", file);
7033           break;
7034         case UNSPEC_NTPOFF:
7035           if (TARGET_64BIT)
7036             fputs ("@TPOFF", file);
7037           else
7038             fputs ("@NTPOFF", file);
7039           break;
7040         case UNSPEC_DTPOFF:
7041           fputs ("@DTPOFF", file);
7042           break;
7043         case UNSPEC_GOTNTPOFF:
7044           if (TARGET_64BIT)
7045             fputs ("@GOTTPOFF(%rip)", file);
7046           else
7047             fputs ("@GOTNTPOFF", file);
7048           break;
7049         case UNSPEC_INDNTPOFF:
7050           fputs ("@INDNTPOFF", file);
7051           break;
7052         default:
7053           output_operand_lossage ("invalid UNSPEC as operand");
7054           break;
7055         }
7056        break;
7057
7058     default:
7059       output_operand_lossage ("invalid expression as operand");
7060     }
7061 }
7062
7063 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
7064    We need to emit DTP-relative relocations.  */
7065
7066 static void
7067 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
7068 {
7069   fputs (ASM_LONG, file);
7070   output_addr_const (file, x);
7071   fputs ("@DTPOFF", file);
7072   switch (size)
7073     {
7074     case 4:
7075       break;
7076     case 8:
7077       fputs (", 0", file);
7078       break;
7079     default:
7080       gcc_unreachable ();
7081    }
7082 }
7083
7084 /* In the name of slightly smaller debug output, and to cater to
7085    general assembler lossage, recognize PIC+GOTOFF and turn it back
7086    into a direct symbol reference.  */
7087
7088 static rtx
7089 ix86_delegitimize_address (rtx orig_x)
7090 {
7091   rtx x = orig_x, y;
7092
7093   if (GET_CODE (x) == MEM)
7094     x = XEXP (x, 0);
7095
7096   if (TARGET_64BIT)
7097     {
7098       if (GET_CODE (x) != CONST
7099           || GET_CODE (XEXP (x, 0)) != UNSPEC
7100           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
7101           || GET_CODE (orig_x) != MEM)
7102         return orig_x;
7103       return XVECEXP (XEXP (x, 0), 0, 0);
7104     }
7105
7106   if (GET_CODE (x) != PLUS
7107       || GET_CODE (XEXP (x, 1)) != CONST)
7108     return orig_x;
7109
7110   if (GET_CODE (XEXP (x, 0)) == REG
7111       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
7112     /* %ebx + GOT/GOTOFF */
7113     y = NULL;
7114   else if (GET_CODE (XEXP (x, 0)) == PLUS)
7115     {
7116       /* %ebx + %reg * scale + GOT/GOTOFF */
7117       y = XEXP (x, 0);
7118       if (GET_CODE (XEXP (y, 0)) == REG
7119           && REGNO (XEXP (y, 0)) == PIC_OFFSET_TABLE_REGNUM)
7120         y = XEXP (y, 1);
7121       else if (GET_CODE (XEXP (y, 1)) == REG
7122                && REGNO (XEXP (y, 1)) == PIC_OFFSET_TABLE_REGNUM)
7123         y = XEXP (y, 0);
7124       else
7125         return orig_x;
7126       if (GET_CODE (y) != REG
7127           && GET_CODE (y) != MULT
7128           && GET_CODE (y) != ASHIFT)
7129         return orig_x;
7130     }
7131   else
7132     return orig_x;
7133
7134   x = XEXP (XEXP (x, 1), 0);
7135   if (GET_CODE (x) == UNSPEC
7136       && ((XINT (x, 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
7137           || (XINT (x, 1) == UNSPEC_GOTOFF && GET_CODE (orig_x) != MEM)))
7138     {
7139       if (y)
7140         return gen_rtx_PLUS (Pmode, y, XVECEXP (x, 0, 0));
7141       return XVECEXP (x, 0, 0);
7142     }
7143
7144   if (GET_CODE (x) == PLUS
7145       && GET_CODE (XEXP (x, 0)) == UNSPEC
7146       && GET_CODE (XEXP (x, 1)) == CONST_INT
7147       && ((XINT (XEXP (x, 0), 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
7148           || (XINT (XEXP (x, 0), 1) == UNSPEC_GOTOFF
7149               && GET_CODE (orig_x) != MEM)))
7150     {
7151       x = gen_rtx_PLUS (VOIDmode, XVECEXP (XEXP (x, 0), 0, 0), XEXP (x, 1));
7152       if (y)
7153         return gen_rtx_PLUS (Pmode, y, x);
7154       return x;
7155     }
7156
7157   if (TARGET_MACHO && darwin_local_data_pic (x)
7158       && GET_CODE (orig_x) != MEM)
7159     {
7160       x = XEXP (x, 0);
7161       if (y)
7162         return gen_rtx_PLUS (Pmode, y, x);
7163       return x;
7164     }
7165   return orig_x;
7166 }
7167 \f
7168 static void
7169 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
7170                     int fp, FILE *file)
7171 {
7172   const char *suffix;
7173
7174   if (mode == CCFPmode || mode == CCFPUmode)
7175     {
7176       enum rtx_code second_code, bypass_code;
7177       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
7178       gcc_assert (bypass_code == UNKNOWN && second_code == UNKNOWN);
7179       code = ix86_fp_compare_code_to_integer (code);
7180       mode = CCmode;
7181     }
7182   if (reverse)
7183     code = reverse_condition (code);
7184
7185   switch (code)
7186     {
7187     case EQ:
7188       suffix = "e";
7189       break;
7190     case NE:
7191       suffix = "ne";
7192       break;
7193     case GT:
7194       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
7195       suffix = "g";
7196       break;
7197     case GTU:
7198       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
7199          Those same assemblers have the same but opposite lossage on cmov.  */
7200       gcc_assert (mode == CCmode);
7201       suffix = fp ? "nbe" : "a";
7202       break;
7203     case LT:
7204       switch (mode)
7205         {
7206         case CCNOmode:
7207         case CCGOCmode:
7208           suffix = "s";
7209           break;
7210
7211         case CCmode:
7212         case CCGCmode:
7213           suffix = "l";
7214           break;
7215
7216         default:
7217           gcc_unreachable ();
7218         }
7219       break;
7220     case LTU:
7221       gcc_assert (mode == CCmode);
7222       suffix = "b";
7223       break;
7224     case GE:
7225       switch (mode)
7226         {
7227         case CCNOmode:
7228         case CCGOCmode:
7229           suffix = "ns";
7230           break;
7231
7232         case CCmode:
7233         case CCGCmode:
7234           suffix = "ge";
7235           break;
7236
7237         default:
7238           gcc_unreachable ();
7239         }
7240       break;
7241     case GEU:
7242       /* ??? As above.  */
7243       gcc_assert (mode == CCmode);
7244       suffix = fp ? "nb" : "ae";
7245       break;
7246     case LE:
7247       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
7248       suffix = "le";
7249       break;
7250     case LEU:
7251       gcc_assert (mode == CCmode);
7252       suffix = "be";
7253       break;
7254     case UNORDERED:
7255       suffix = fp ? "u" : "p";
7256       break;
7257     case ORDERED:
7258       suffix = fp ? "nu" : "np";
7259       break;
7260     default:
7261       gcc_unreachable ();
7262     }
7263   fputs (suffix, file);
7264 }
7265
7266 /* Print the name of register X to FILE based on its machine mode and number.
7267    If CODE is 'w', pretend the mode is HImode.
7268    If CODE is 'b', pretend the mode is QImode.
7269    If CODE is 'k', pretend the mode is SImode.
7270    If CODE is 'q', pretend the mode is DImode.
7271    If CODE is 'h', pretend the reg is the 'high' byte register.
7272    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.  */
7273
7274 void
7275 print_reg (rtx x, int code, FILE *file)
7276 {
7277   gcc_assert (REGNO (x) != ARG_POINTER_REGNUM
7278               && REGNO (x) != FRAME_POINTER_REGNUM
7279               && REGNO (x) != FLAGS_REG
7280               && REGNO (x) != FPSR_REG);
7281
7282   if (ASSEMBLER_DIALECT == ASM_ATT || USER_LABEL_PREFIX[0] == 0)
7283     putc ('%', file);
7284
7285   if (code == 'w' || MMX_REG_P (x))
7286     code = 2;
7287   else if (code == 'b')
7288     code = 1;
7289   else if (code == 'k')
7290     code = 4;
7291   else if (code == 'q')
7292     code = 8;
7293   else if (code == 'y')
7294     code = 3;
7295   else if (code == 'h')
7296     code = 0;
7297   else
7298     code = GET_MODE_SIZE (GET_MODE (x));
7299
7300   /* Irritatingly, AMD extended registers use different naming convention
7301      from the normal registers.  */
7302   if (REX_INT_REG_P (x))
7303     {
7304       gcc_assert (TARGET_64BIT);
7305       switch (code)
7306         {
7307           case 0:
7308             error ("extended registers have no high halves");
7309             break;
7310           case 1:
7311             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
7312             break;
7313           case 2:
7314             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
7315             break;
7316           case 4:
7317             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
7318             break;
7319           case 8:
7320             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
7321             break;
7322           default:
7323             error ("unsupported operand size for extended register");
7324             break;
7325         }
7326       return;
7327     }
7328   switch (code)
7329     {
7330     case 3:
7331       if (STACK_TOP_P (x))
7332         {
7333           fputs ("st(0)", file);
7334           break;
7335         }
7336       /* FALLTHRU */
7337     case 8:
7338     case 4:
7339     case 12:
7340       if (! ANY_FP_REG_P (x))
7341         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
7342       /* FALLTHRU */
7343     case 16:
7344     case 2:
7345     normal:
7346       fputs (hi_reg_name[REGNO (x)], file);
7347       break;
7348     case 1:
7349       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
7350         goto normal;
7351       fputs (qi_reg_name[REGNO (x)], file);
7352       break;
7353     case 0:
7354       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
7355         goto normal;
7356       fputs (qi_high_reg_name[REGNO (x)], file);
7357       break;
7358     default:
7359       gcc_unreachable ();
7360     }
7361 }
7362
7363 /* Locate some local-dynamic symbol still in use by this function
7364    so that we can print its name in some tls_local_dynamic_base
7365    pattern.  */
7366
7367 static const char *
7368 get_some_local_dynamic_name (void)
7369 {
7370   rtx insn;
7371
7372   if (cfun->machine->some_ld_name)
7373     return cfun->machine->some_ld_name;
7374
7375   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
7376     if (INSN_P (insn)
7377         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
7378       return cfun->machine->some_ld_name;
7379
7380   gcc_unreachable ();
7381 }
7382
7383 static int
7384 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
7385 {
7386   rtx x = *px;
7387
7388   if (GET_CODE (x) == SYMBOL_REF
7389       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
7390     {
7391       cfun->machine->some_ld_name = XSTR (x, 0);
7392       return 1;
7393     }
7394
7395   return 0;
7396 }
7397
7398 /* Meaning of CODE:
7399    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
7400    C -- print opcode suffix for set/cmov insn.
7401    c -- like C, but print reversed condition
7402    F,f -- likewise, but for floating-point.
7403    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
7404         otherwise nothing
7405    R -- print the prefix for register names.
7406    z -- print the opcode suffix for the size of the current operand.
7407    * -- print a star (in certain assembler syntax)
7408    A -- print an absolute memory reference.
7409    w -- print the operand as if it's a "word" (HImode) even if it isn't.
7410    s -- print a shift double count, followed by the assemblers argument
7411         delimiter.
7412    b -- print the QImode name of the register for the indicated operand.
7413         %b0 would print %al if operands[0] is reg 0.
7414    w --  likewise, print the HImode name of the register.
7415    k --  likewise, print the SImode name of the register.
7416    q --  likewise, print the DImode name of the register.
7417    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
7418    y -- print "st(0)" instead of "st" as a register.
7419    D -- print condition for SSE cmp instruction.
7420    P -- if PIC, print an @PLT suffix.
7421    X -- don't print any sort of PIC '@' suffix for a symbol.
7422    & -- print some in-use local-dynamic symbol name.
7423    H -- print a memory address offset by 8; used for sse high-parts
7424  */
7425
7426 void
7427 print_operand (FILE *file, rtx x, int code)
7428 {
7429   if (code)
7430     {
7431       switch (code)
7432         {
7433         case '*':
7434           if (ASSEMBLER_DIALECT == ASM_ATT)
7435             putc ('*', file);
7436           return;
7437
7438         case '&':
7439           assemble_name (file, get_some_local_dynamic_name ());
7440           return;
7441
7442         case 'A':
7443           switch (ASSEMBLER_DIALECT)
7444             {
7445             case ASM_ATT:
7446               putc ('*', file);
7447               break;
7448
7449             case ASM_INTEL:
7450               /* Intel syntax. For absolute addresses, registers should not
7451                  be surrounded by braces.  */
7452               if (GET_CODE (x) != REG)
7453                 {
7454                   putc ('[', file);
7455                   PRINT_OPERAND (file, x, 0);
7456                   putc (']', file);
7457                   return;
7458                 }
7459               break;
7460
7461             default:
7462               gcc_unreachable ();
7463             }
7464
7465           PRINT_OPERAND (file, x, 0);
7466           return;
7467
7468
7469         case 'L':
7470           if (ASSEMBLER_DIALECT == ASM_ATT)
7471             putc ('l', file);
7472           return;
7473
7474         case 'W':
7475           if (ASSEMBLER_DIALECT == ASM_ATT)
7476             putc ('w', file);
7477           return;
7478
7479         case 'B':
7480           if (ASSEMBLER_DIALECT == ASM_ATT)
7481             putc ('b', file);
7482           return;
7483
7484         case 'Q':
7485           if (ASSEMBLER_DIALECT == ASM_ATT)
7486             putc ('l', file);
7487           return;
7488
7489         case 'S':
7490           if (ASSEMBLER_DIALECT == ASM_ATT)
7491             putc ('s', file);
7492           return;
7493
7494         case 'T':
7495           if (ASSEMBLER_DIALECT == ASM_ATT)
7496             putc ('t', file);
7497           return;
7498
7499         case 'z':
7500           /* 387 opcodes don't get size suffixes if the operands are
7501              registers.  */
7502           if (STACK_REG_P (x))
7503             return;
7504
7505           /* Likewise if using Intel opcodes.  */
7506           if (ASSEMBLER_DIALECT == ASM_INTEL)
7507             return;
7508
7509           /* This is the size of op from size of operand.  */
7510           switch (GET_MODE_SIZE (GET_MODE (x)))
7511             {
7512             case 2:
7513 #ifdef HAVE_GAS_FILDS_FISTS
7514               putc ('s', file);
7515 #endif
7516               return;
7517
7518             case 4:
7519               if (GET_MODE (x) == SFmode)
7520                 {
7521                   putc ('s', file);
7522                   return;
7523                 }
7524               else
7525                 putc ('l', file);
7526               return;
7527
7528             case 12:
7529             case 16:
7530               putc ('t', file);
7531               return;
7532
7533             case 8:
7534               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
7535                 {
7536 #ifdef GAS_MNEMONICS
7537                   putc ('q', file);
7538 #else
7539                   putc ('l', file);
7540                   putc ('l', file);
7541 #endif
7542                 }
7543               else
7544                 putc ('l', file);
7545               return;
7546
7547             default:
7548               gcc_unreachable ();
7549             }
7550
7551         case 'b':
7552         case 'w':
7553         case 'k':
7554         case 'q':
7555         case 'h':
7556         case 'y':
7557         case 'X':
7558         case 'P':
7559           break;
7560
7561         case 's':
7562           if (GET_CODE (x) == CONST_INT || ! SHIFT_DOUBLE_OMITS_COUNT)
7563             {
7564               PRINT_OPERAND (file, x, 0);
7565               putc (',', file);
7566             }
7567           return;
7568
7569         case 'D':
7570           /* Little bit of braindamage here.  The SSE compare instructions
7571              does use completely different names for the comparisons that the
7572              fp conditional moves.  */
7573           switch (GET_CODE (x))
7574             {
7575             case EQ:
7576             case UNEQ:
7577               fputs ("eq", file);
7578               break;
7579             case LT:
7580             case UNLT:
7581               fputs ("lt", file);
7582               break;
7583             case LE:
7584             case UNLE:
7585               fputs ("le", file);
7586               break;
7587             case UNORDERED:
7588               fputs ("unord", file);
7589               break;
7590             case NE:
7591             case LTGT:
7592               fputs ("neq", file);
7593               break;
7594             case UNGE:
7595             case GE:
7596               fputs ("nlt", file);
7597               break;
7598             case UNGT:
7599             case GT:
7600               fputs ("nle", file);
7601               break;
7602             case ORDERED:
7603               fputs ("ord", file);
7604               break;
7605             default:
7606               gcc_unreachable ();
7607             }
7608           return;
7609         case 'O':
7610 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7611           if (ASSEMBLER_DIALECT == ASM_ATT)
7612             {
7613               switch (GET_MODE (x))
7614                 {
7615                 case HImode: putc ('w', file); break;
7616                 case SImode:
7617                 case SFmode: putc ('l', file); break;
7618                 case DImode:
7619                 case DFmode: putc ('q', file); break;
7620                 default: gcc_unreachable ();
7621                 }
7622               putc ('.', file);
7623             }
7624 #endif
7625           return;
7626         case 'C':
7627           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
7628           return;
7629         case 'F':
7630 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7631           if (ASSEMBLER_DIALECT == ASM_ATT)
7632             putc ('.', file);
7633 #endif
7634           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
7635           return;
7636
7637           /* Like above, but reverse condition */
7638         case 'c':
7639           /* Check to see if argument to %c is really a constant
7640              and not a condition code which needs to be reversed.  */
7641           if (!COMPARISON_P (x))
7642           {
7643             output_operand_lossage ("operand is neither a constant nor a condition code, invalid operand code 'c'");
7644              return;
7645           }
7646           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
7647           return;
7648         case 'f':
7649 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7650           if (ASSEMBLER_DIALECT == ASM_ATT)
7651             putc ('.', file);
7652 #endif
7653           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
7654           return;
7655
7656         case 'H':
7657           /* It doesn't actually matter what mode we use here, as we're
7658              only going to use this for printing.  */
7659           x = adjust_address_nv (x, DImode, 8);
7660           break;
7661
7662         case '+':
7663           {
7664             rtx x;
7665
7666             if (!optimize || optimize_size || !TARGET_BRANCH_PREDICTION_HINTS)
7667               return;
7668
7669             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
7670             if (x)
7671               {
7672                 int pred_val = INTVAL (XEXP (x, 0));
7673
7674                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
7675                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
7676                   {
7677                     int taken = pred_val > REG_BR_PROB_BASE / 2;
7678                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
7679
7680                     /* Emit hints only in the case default branch prediction
7681                        heuristics would fail.  */
7682                     if (taken != cputaken)
7683                       {
7684                         /* We use 3e (DS) prefix for taken branches and
7685                            2e (CS) prefix for not taken branches.  */
7686                         if (taken)
7687                           fputs ("ds ; ", file);
7688                         else
7689                           fputs ("cs ; ", file);
7690                       }
7691                   }
7692               }
7693             return;
7694           }
7695         default:
7696             output_operand_lossage ("invalid operand code '%c'", code);
7697         }
7698     }
7699
7700   if (GET_CODE (x) == REG)
7701     print_reg (x, code, file);
7702
7703   else if (GET_CODE (x) == MEM)
7704     {
7705       /* No `byte ptr' prefix for call instructions.  */
7706       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P')
7707         {
7708           const char * size;
7709           switch (GET_MODE_SIZE (GET_MODE (x)))
7710             {
7711             case 1: size = "BYTE"; break;
7712             case 2: size = "WORD"; break;
7713             case 4: size = "DWORD"; break;
7714             case 8: size = "QWORD"; break;
7715             case 12: size = "XWORD"; break;
7716             case 16: size = "XMMWORD"; break;
7717             default:
7718               gcc_unreachable ();
7719             }
7720
7721           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
7722           if (code == 'b')
7723             size = "BYTE";
7724           else if (code == 'w')
7725             size = "WORD";
7726           else if (code == 'k')
7727             size = "DWORD";
7728
7729           fputs (size, file);
7730           fputs (" PTR ", file);
7731         }
7732
7733       x = XEXP (x, 0);
7734       /* Avoid (%rip) for call operands.  */
7735       if (CONSTANT_ADDRESS_P (x) && code == 'P'
7736                && GET_CODE (x) != CONST_INT)
7737         output_addr_const (file, x);
7738       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
7739         output_operand_lossage ("invalid constraints for operand");
7740       else
7741         output_address (x);
7742     }
7743
7744   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
7745     {
7746       REAL_VALUE_TYPE r;
7747       long l;
7748
7749       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7750       REAL_VALUE_TO_TARGET_SINGLE (r, l);
7751
7752       if (ASSEMBLER_DIALECT == ASM_ATT)
7753         putc ('$', file);
7754       fprintf (file, "0x%08lx", l);
7755     }
7756
7757   /* These float cases don't actually occur as immediate operands.  */
7758   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
7759     {
7760       char dstr[30];
7761
7762       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
7763       fprintf (file, "%s", dstr);
7764     }
7765
7766   else if (GET_CODE (x) == CONST_DOUBLE
7767            && GET_MODE (x) == XFmode)
7768     {
7769       char dstr[30];
7770
7771       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
7772       fprintf (file, "%s", dstr);
7773     }
7774
7775   else
7776     {
7777       /* We have patterns that allow zero sets of memory, for instance.
7778          In 64-bit mode, we should probably support all 8-byte vectors,
7779          since we can in fact encode that into an immediate.  */
7780       if (GET_CODE (x) == CONST_VECTOR)
7781         {
7782           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
7783           x = const0_rtx;
7784         }
7785
7786       if (code != 'P')
7787         {
7788           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
7789             {
7790               if (ASSEMBLER_DIALECT == ASM_ATT)
7791                 putc ('$', file);
7792             }
7793           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
7794                    || GET_CODE (x) == LABEL_REF)
7795             {
7796               if (ASSEMBLER_DIALECT == ASM_ATT)
7797                 putc ('$', file);
7798               else
7799                 fputs ("OFFSET FLAT:", file);
7800             }
7801         }
7802       if (GET_CODE (x) == CONST_INT)
7803         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
7804       else if (flag_pic)
7805         output_pic_addr_const (file, x, code);
7806       else
7807         output_addr_const (file, x);
7808     }
7809 }
7810 \f
7811 /* Print a memory operand whose address is ADDR.  */
7812
7813 void
7814 print_operand_address (FILE *file, rtx addr)
7815 {
7816   struct ix86_address parts;
7817   rtx base, index, disp;
7818   int scale;
7819   int ok = ix86_decompose_address (addr, &parts);
7820
7821   gcc_assert (ok);
7822
7823   base = parts.base;
7824   index = parts.index;
7825   disp = parts.disp;
7826   scale = parts.scale;
7827
7828   switch (parts.seg)
7829     {
7830     case SEG_DEFAULT:
7831       break;
7832     case SEG_FS:
7833     case SEG_GS:
7834       if (USER_LABEL_PREFIX[0] == 0)
7835         putc ('%', file);
7836       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
7837       break;
7838     default:
7839       gcc_unreachable ();
7840     }
7841
7842   if (!base && !index)
7843     {
7844       /* Displacement only requires special attention.  */
7845
7846       if (GET_CODE (disp) == CONST_INT)
7847         {
7848           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
7849             {
7850               if (USER_LABEL_PREFIX[0] == 0)
7851                 putc ('%', file);
7852               fputs ("ds:", file);
7853             }
7854           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
7855         }
7856       else if (flag_pic)
7857         output_pic_addr_const (file, disp, 0);
7858       else
7859         output_addr_const (file, disp);
7860
7861       /* Use one byte shorter RIP relative addressing for 64bit mode.  */
7862       if (TARGET_64BIT)
7863         {
7864           if (GET_CODE (disp) == CONST
7865               && GET_CODE (XEXP (disp, 0)) == PLUS
7866               && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
7867             disp = XEXP (XEXP (disp, 0), 0);
7868           if (GET_CODE (disp) == LABEL_REF
7869               || (GET_CODE (disp) == SYMBOL_REF
7870                   && SYMBOL_REF_TLS_MODEL (disp) == 0))
7871             fputs ("(%rip)", file);
7872         }
7873     }
7874   else
7875     {
7876       if (ASSEMBLER_DIALECT == ASM_ATT)
7877         {
7878           if (disp)
7879             {
7880               if (flag_pic)
7881                 output_pic_addr_const (file, disp, 0);
7882               else if (GET_CODE (disp) == LABEL_REF)
7883                 output_asm_label (disp);
7884               else
7885                 output_addr_const (file, disp);
7886             }
7887
7888           putc ('(', file);
7889           if (base)
7890             print_reg (base, 0, file);
7891           if (index)
7892             {
7893               putc (',', file);
7894               print_reg (index, 0, file);
7895               if (scale != 1)
7896                 fprintf (file, ",%d", scale);
7897             }
7898           putc (')', file);
7899         }
7900       else
7901         {
7902           rtx offset = NULL_RTX;
7903
7904           if (disp)
7905             {
7906               /* Pull out the offset of a symbol; print any symbol itself.  */
7907               if (GET_CODE (disp) == CONST
7908                   && GET_CODE (XEXP (disp, 0)) == PLUS
7909                   && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
7910                 {
7911                   offset = XEXP (XEXP (disp, 0), 1);
7912                   disp = gen_rtx_CONST (VOIDmode,
7913                                         XEXP (XEXP (disp, 0), 0));
7914                 }
7915
7916               if (flag_pic)
7917                 output_pic_addr_const (file, disp, 0);
7918               else if (GET_CODE (disp) == LABEL_REF)
7919                 output_asm_label (disp);
7920               else if (GET_CODE (disp) == CONST_INT)
7921                 offset = disp;
7922               else
7923                 output_addr_const (file, disp);
7924             }
7925
7926           putc ('[', file);
7927           if (base)
7928             {
7929               print_reg (base, 0, file);
7930               if (offset)
7931                 {
7932                   if (INTVAL (offset) >= 0)
7933                     putc ('+', file);
7934                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
7935                 }
7936             }
7937           else if (offset)
7938             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
7939           else
7940             putc ('0', file);
7941
7942           if (index)
7943             {
7944               putc ('+', file);
7945               print_reg (index, 0, file);
7946               if (scale != 1)
7947                 fprintf (file, "*%d", scale);
7948             }
7949           putc (']', file);
7950         }
7951     }
7952 }
7953
7954 bool
7955 output_addr_const_extra (FILE *file, rtx x)
7956 {
7957   rtx op;
7958
7959   if (GET_CODE (x) != UNSPEC)
7960     return false;
7961
7962   op = XVECEXP (x, 0, 0);
7963   switch (XINT (x, 1))
7964     {
7965     case UNSPEC_GOTTPOFF:
7966       output_addr_const (file, op);
7967       /* FIXME: This might be @TPOFF in Sun ld.  */
7968       fputs ("@GOTTPOFF", file);
7969       break;
7970     case UNSPEC_TPOFF:
7971       output_addr_const (file, op);
7972       fputs ("@TPOFF", file);
7973       break;
7974     case UNSPEC_NTPOFF:
7975       output_addr_const (file, op);
7976       if (TARGET_64BIT)
7977         fputs ("@TPOFF", file);
7978       else
7979         fputs ("@NTPOFF", file);
7980       break;
7981     case UNSPEC_DTPOFF:
7982       output_addr_const (file, op);
7983       fputs ("@DTPOFF", file);
7984       break;
7985     case UNSPEC_GOTNTPOFF:
7986       output_addr_const (file, op);
7987       if (TARGET_64BIT)
7988         fputs ("@GOTTPOFF(%rip)", file);
7989       else
7990         fputs ("@GOTNTPOFF", file);
7991       break;
7992     case UNSPEC_INDNTPOFF:
7993       output_addr_const (file, op);
7994       fputs ("@INDNTPOFF", file);
7995       break;
7996
7997     default:
7998       return false;
7999     }
8000
8001   return true;
8002 }
8003 \f
8004 /* Split one or more DImode RTL references into pairs of SImode
8005    references.  The RTL can be REG, offsettable MEM, integer constant, or
8006    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
8007    split and "num" is its length.  lo_half and hi_half are output arrays
8008    that parallel "operands".  */
8009
8010 void
8011 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
8012 {
8013   while (num--)
8014     {
8015       rtx op = operands[num];
8016
8017       /* simplify_subreg refuse to split volatile memory addresses,
8018          but we still have to handle it.  */
8019       if (GET_CODE (op) == MEM)
8020         {
8021           lo_half[num] = adjust_address (op, SImode, 0);
8022           hi_half[num] = adjust_address (op, SImode, 4);
8023         }
8024       else
8025         {
8026           lo_half[num] = simplify_gen_subreg (SImode, op,
8027                                               GET_MODE (op) == VOIDmode
8028                                               ? DImode : GET_MODE (op), 0);
8029           hi_half[num] = simplify_gen_subreg (SImode, op,
8030                                               GET_MODE (op) == VOIDmode
8031                                               ? DImode : GET_MODE (op), 4);
8032         }
8033     }
8034 }
8035 /* Split one or more TImode RTL references into pairs of DImode
8036    references.  The RTL can be REG, offsettable MEM, integer constant, or
8037    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
8038    split and "num" is its length.  lo_half and hi_half are output arrays
8039    that parallel "operands".  */
8040
8041 void
8042 split_ti (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
8043 {
8044   while (num--)
8045     {
8046       rtx op = operands[num];
8047
8048       /* simplify_subreg refuse to split volatile memory addresses, but we
8049          still have to handle it.  */
8050       if (GET_CODE (op) == MEM)
8051         {
8052           lo_half[num] = adjust_address (op, DImode, 0);
8053           hi_half[num] = adjust_address (op, DImode, 8);
8054         }
8055       else
8056         {
8057           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
8058           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
8059         }
8060     }
8061 }
8062 \f
8063 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
8064    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
8065    is the expression of the binary operation.  The output may either be
8066    emitted here, or returned to the caller, like all output_* functions.
8067
8068    There is no guarantee that the operands are the same mode, as they
8069    might be within FLOAT or FLOAT_EXTEND expressions.  */
8070
8071 #ifndef SYSV386_COMPAT
8072 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
8073    wants to fix the assemblers because that causes incompatibility
8074    with gcc.  No-one wants to fix gcc because that causes
8075    incompatibility with assemblers...  You can use the option of
8076    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
8077 #define SYSV386_COMPAT 1
8078 #endif
8079
8080 const char *
8081 output_387_binary_op (rtx insn, rtx *operands)
8082 {
8083   static char buf[30];
8084   const char *p;
8085   const char *ssep;
8086   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
8087
8088 #ifdef ENABLE_CHECKING
8089   /* Even if we do not want to check the inputs, this documents input
8090      constraints.  Which helps in understanding the following code.  */
8091   if (STACK_REG_P (operands[0])
8092       && ((REG_P (operands[1])
8093            && REGNO (operands[0]) == REGNO (operands[1])
8094            && (STACK_REG_P (operands[2]) || GET_CODE (operands[2]) == MEM))
8095           || (REG_P (operands[2])
8096               && REGNO (operands[0]) == REGNO (operands[2])
8097               && (STACK_REG_P (operands[1]) || GET_CODE (operands[1]) == MEM)))
8098       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
8099     ; /* ok */
8100   else
8101     gcc_assert (is_sse);
8102 #endif
8103
8104   switch (GET_CODE (operands[3]))
8105     {
8106     case PLUS:
8107       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8108           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8109         p = "fiadd";
8110       else
8111         p = "fadd";
8112       ssep = "add";
8113       break;
8114
8115     case MINUS:
8116       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8117           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8118         p = "fisub";
8119       else
8120         p = "fsub";
8121       ssep = "sub";
8122       break;
8123
8124     case MULT:
8125       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8126           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8127         p = "fimul";
8128       else
8129         p = "fmul";
8130       ssep = "mul";
8131       break;
8132
8133     case DIV:
8134       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8135           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8136         p = "fidiv";
8137       else
8138         p = "fdiv";
8139       ssep = "div";
8140       break;
8141
8142     default:
8143       gcc_unreachable ();
8144     }
8145
8146   if (is_sse)
8147    {
8148       strcpy (buf, ssep);
8149       if (GET_MODE (operands[0]) == SFmode)
8150         strcat (buf, "ss\t{%2, %0|%0, %2}");
8151       else
8152         strcat (buf, "sd\t{%2, %0|%0, %2}");
8153       return buf;
8154    }
8155   strcpy (buf, p);
8156
8157   switch (GET_CODE (operands[3]))
8158     {
8159     case MULT:
8160     case PLUS:
8161       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
8162         {
8163           rtx temp = operands[2];
8164           operands[2] = operands[1];
8165           operands[1] = temp;
8166         }
8167
8168       /* know operands[0] == operands[1].  */
8169
8170       if (GET_CODE (operands[2]) == MEM)
8171         {
8172           p = "%z2\t%2";
8173           break;
8174         }
8175
8176       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
8177         {
8178           if (STACK_TOP_P (operands[0]))
8179             /* How is it that we are storing to a dead operand[2]?
8180                Well, presumably operands[1] is dead too.  We can't
8181                store the result to st(0) as st(0) gets popped on this
8182                instruction.  Instead store to operands[2] (which I
8183                think has to be st(1)).  st(1) will be popped later.
8184                gcc <= 2.8.1 didn't have this check and generated
8185                assembly code that the Unixware assembler rejected.  */
8186             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
8187           else
8188             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
8189           break;
8190         }
8191
8192       if (STACK_TOP_P (operands[0]))
8193         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
8194       else
8195         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
8196       break;
8197
8198     case MINUS:
8199     case DIV:
8200       if (GET_CODE (operands[1]) == MEM)
8201         {
8202           p = "r%z1\t%1";
8203           break;
8204         }
8205
8206       if (GET_CODE (operands[2]) == MEM)
8207         {
8208           p = "%z2\t%2";
8209           break;
8210         }
8211
8212       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
8213         {
8214 #if SYSV386_COMPAT
8215           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
8216              derived assemblers, confusingly reverse the direction of
8217              the operation for fsub{r} and fdiv{r} when the
8218              destination register is not st(0).  The Intel assembler
8219              doesn't have this brain damage.  Read !SYSV386_COMPAT to
8220              figure out what the hardware really does.  */
8221           if (STACK_TOP_P (operands[0]))
8222             p = "{p\t%0, %2|rp\t%2, %0}";
8223           else
8224             p = "{rp\t%2, %0|p\t%0, %2}";
8225 #else
8226           if (STACK_TOP_P (operands[0]))
8227             /* As above for fmul/fadd, we can't store to st(0).  */
8228             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
8229           else
8230             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
8231 #endif
8232           break;
8233         }
8234
8235       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
8236         {
8237 #if SYSV386_COMPAT
8238           if (STACK_TOP_P (operands[0]))
8239             p = "{rp\t%0, %1|p\t%1, %0}";
8240           else
8241             p = "{p\t%1, %0|rp\t%0, %1}";
8242 #else
8243           if (STACK_TOP_P (operands[0]))
8244             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
8245           else
8246             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
8247 #endif
8248           break;
8249         }
8250
8251       if (STACK_TOP_P (operands[0]))
8252         {
8253           if (STACK_TOP_P (operands[1]))
8254             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
8255           else
8256             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
8257           break;
8258         }
8259       else if (STACK_TOP_P (operands[1]))
8260         {
8261 #if SYSV386_COMPAT
8262           p = "{\t%1, %0|r\t%0, %1}";
8263 #else
8264           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
8265 #endif
8266         }
8267       else
8268         {
8269 #if SYSV386_COMPAT
8270           p = "{r\t%2, %0|\t%0, %2}";
8271 #else
8272           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
8273 #endif
8274         }
8275       break;
8276
8277     default:
8278       gcc_unreachable ();
8279     }
8280
8281   strcat (buf, p);
8282   return buf;
8283 }
8284
8285 /* Return needed mode for entity in optimize_mode_switching pass.  */
8286
8287 int
8288 ix86_mode_needed (int entity, rtx insn)
8289 {
8290   enum attr_i387_cw mode;
8291
8292   /* The mode UNINITIALIZED is used to store control word after a
8293      function call or ASM pattern.  The mode ANY specify that function
8294      has no requirements on the control word and make no changes in the
8295      bits we are interested in.  */
8296
8297   if (CALL_P (insn)
8298       || (NONJUMP_INSN_P (insn)
8299           && (asm_noperands (PATTERN (insn)) >= 0
8300               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
8301     return I387_CW_UNINITIALIZED;
8302
8303   if (recog_memoized (insn) < 0)
8304     return I387_CW_ANY;
8305
8306   mode = get_attr_i387_cw (insn);
8307
8308   switch (entity)
8309     {
8310     case I387_TRUNC:
8311       if (mode == I387_CW_TRUNC)
8312         return mode;
8313       break;
8314
8315     case I387_FLOOR:
8316       if (mode == I387_CW_FLOOR)
8317         return mode;
8318       break;
8319
8320     case I387_CEIL:
8321       if (mode == I387_CW_CEIL)
8322         return mode;
8323       break;
8324
8325     case I387_MASK_PM:
8326       if (mode == I387_CW_MASK_PM)
8327         return mode;
8328       break;
8329
8330     default:
8331       gcc_unreachable ();
8332     }
8333
8334   return I387_CW_ANY;
8335 }
8336
8337 /* Output code to initialize control word copies used by trunc?f?i and
8338    rounding patterns.  CURRENT_MODE is set to current control word,
8339    while NEW_MODE is set to new control word.  */
8340
8341 void
8342 emit_i387_cw_initialization (int mode)
8343 {
8344   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
8345   rtx new_mode;
8346
8347   int slot;
8348
8349   rtx reg = gen_reg_rtx (HImode);
8350
8351   emit_insn (gen_x86_fnstcw_1 (stored_mode));
8352   emit_move_insn (reg, stored_mode);
8353
8354   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL || optimize_size)
8355     {
8356       switch (mode)
8357         {
8358         case I387_CW_TRUNC:
8359           /* round toward zero (truncate) */
8360           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
8361           slot = SLOT_CW_TRUNC;
8362           break;
8363
8364         case I387_CW_FLOOR:
8365           /* round down toward -oo */
8366           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
8367           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
8368           slot = SLOT_CW_FLOOR;
8369           break;
8370
8371         case I387_CW_CEIL:
8372           /* round up toward +oo */
8373           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
8374           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
8375           slot = SLOT_CW_CEIL;
8376           break;
8377
8378         case I387_CW_MASK_PM:
8379           /* mask precision exception for nearbyint() */
8380           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
8381           slot = SLOT_CW_MASK_PM;
8382           break;
8383
8384         default:
8385           gcc_unreachable ();
8386         }
8387     }
8388   else
8389     {
8390       switch (mode)
8391         {
8392         case I387_CW_TRUNC:
8393           /* round toward zero (truncate) */
8394           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
8395           slot = SLOT_CW_TRUNC;
8396           break;
8397
8398         case I387_CW_FLOOR:
8399           /* round down toward -oo */
8400           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
8401           slot = SLOT_CW_FLOOR;
8402           break;
8403
8404         case I387_CW_CEIL:
8405           /* round up toward +oo */
8406           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
8407           slot = SLOT_CW_CEIL;
8408           break;
8409  
8410         case I387_CW_MASK_PM:
8411           /* mask precision exception for nearbyint() */
8412           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
8413           slot = SLOT_CW_MASK_PM;
8414           break;
8415
8416         default:
8417           gcc_unreachable ();
8418         }
8419     }
8420
8421   gcc_assert (slot < MAX_386_STACK_LOCALS);
8422
8423   new_mode = assign_386_stack_local (HImode, slot);
8424   emit_move_insn (new_mode, reg);
8425 }
8426
8427 /* Output code for INSN to convert a float to a signed int.  OPERANDS
8428    are the insn operands.  The output may be [HSD]Imode and the input
8429    operand may be [SDX]Fmode.  */
8430
8431 const char *
8432 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
8433 {
8434   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
8435   int dimode_p = GET_MODE (operands[0]) == DImode;
8436   int round_mode = get_attr_i387_cw (insn);
8437
8438   /* Jump through a hoop or two for DImode, since the hardware has no
8439      non-popping instruction.  We used to do this a different way, but
8440      that was somewhat fragile and broke with post-reload splitters.  */
8441   if ((dimode_p || fisttp) && !stack_top_dies)
8442     output_asm_insn ("fld\t%y1", operands);
8443
8444   gcc_assert (STACK_TOP_P (operands[1]));
8445   gcc_assert (GET_CODE (operands[0]) == MEM);
8446
8447   if (fisttp)
8448       output_asm_insn ("fisttp%z0\t%0", operands);
8449   else
8450     {
8451       if (round_mode != I387_CW_ANY)
8452         output_asm_insn ("fldcw\t%3", operands);
8453       if (stack_top_dies || dimode_p)
8454         output_asm_insn ("fistp%z0\t%0", operands);
8455       else
8456         output_asm_insn ("fist%z0\t%0", operands);
8457       if (round_mode != I387_CW_ANY)
8458         output_asm_insn ("fldcw\t%2", operands);
8459     }
8460
8461   return "";
8462 }
8463
8464 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
8465    should be used.  UNORDERED_P is true when fucom should be used.  */
8466
8467 const char *
8468 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
8469 {
8470   int stack_top_dies;
8471   rtx cmp_op0, cmp_op1;
8472   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
8473
8474   if (eflags_p)
8475     {
8476       cmp_op0 = operands[0];
8477       cmp_op1 = operands[1];
8478     }
8479   else
8480     {
8481       cmp_op0 = operands[1];
8482       cmp_op1 = operands[2];
8483     }
8484
8485   if (is_sse)
8486     {
8487       if (GET_MODE (operands[0]) == SFmode)
8488         if (unordered_p)
8489           return "ucomiss\t{%1, %0|%0, %1}";
8490         else
8491           return "comiss\t{%1, %0|%0, %1}";
8492       else
8493         if (unordered_p)
8494           return "ucomisd\t{%1, %0|%0, %1}";
8495         else
8496           return "comisd\t{%1, %0|%0, %1}";
8497     }
8498
8499   gcc_assert (STACK_TOP_P (cmp_op0));
8500
8501   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
8502
8503   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
8504     {
8505       if (stack_top_dies)
8506         {
8507           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
8508           return TARGET_USE_FFREEP ? "ffreep\t%y1" : "fstp\t%y1";
8509         }
8510       else
8511         return "ftst\n\tfnstsw\t%0";
8512     }
8513
8514   if (STACK_REG_P (cmp_op1)
8515       && stack_top_dies
8516       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
8517       && REGNO (cmp_op1) != FIRST_STACK_REG)
8518     {
8519       /* If both the top of the 387 stack dies, and the other operand
8520          is also a stack register that dies, then this must be a
8521          `fcompp' float compare */
8522
8523       if (eflags_p)
8524         {
8525           /* There is no double popping fcomi variant.  Fortunately,
8526              eflags is immune from the fstp's cc clobbering.  */
8527           if (unordered_p)
8528             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
8529           else
8530             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
8531           return TARGET_USE_FFREEP ? "ffreep\t%y0" : "fstp\t%y0";
8532         }
8533       else
8534         {
8535           if (unordered_p)
8536             return "fucompp\n\tfnstsw\t%0";
8537           else
8538             return "fcompp\n\tfnstsw\t%0";
8539         }
8540     }
8541   else
8542     {
8543       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
8544
8545       static const char * const alt[16] =
8546       {
8547         "fcom%z2\t%y2\n\tfnstsw\t%0",
8548         "fcomp%z2\t%y2\n\tfnstsw\t%0",
8549         "fucom%z2\t%y2\n\tfnstsw\t%0",
8550         "fucomp%z2\t%y2\n\tfnstsw\t%0",
8551
8552         "ficom%z2\t%y2\n\tfnstsw\t%0",
8553         "ficomp%z2\t%y2\n\tfnstsw\t%0",
8554         NULL,
8555         NULL,
8556
8557         "fcomi\t{%y1, %0|%0, %y1}",
8558         "fcomip\t{%y1, %0|%0, %y1}",
8559         "fucomi\t{%y1, %0|%0, %y1}",
8560         "fucomip\t{%y1, %0|%0, %y1}",
8561
8562         NULL,
8563         NULL,
8564         NULL,
8565         NULL
8566       };
8567
8568       int mask;
8569       const char *ret;
8570
8571       mask  = eflags_p << 3;
8572       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
8573       mask |= unordered_p << 1;
8574       mask |= stack_top_dies;
8575
8576       gcc_assert (mask < 16);
8577       ret = alt[mask];
8578       gcc_assert (ret);
8579
8580       return ret;
8581     }
8582 }
8583
8584 void
8585 ix86_output_addr_vec_elt (FILE *file, int value)
8586 {
8587   const char *directive = ASM_LONG;
8588
8589 #ifdef ASM_QUAD
8590   if (TARGET_64BIT)
8591     directive = ASM_QUAD;
8592 #else
8593   gcc_assert (!TARGET_64BIT);
8594 #endif
8595
8596   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
8597 }
8598
8599 void
8600 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
8601 {
8602   if (TARGET_64BIT)
8603     fprintf (file, "%s%s%d-%s%d\n",
8604              ASM_LONG, LPREFIX, value, LPREFIX, rel);
8605   else if (HAVE_AS_GOTOFF_IN_DATA)
8606     fprintf (file, "%s%s%d@GOTOFF\n", ASM_LONG, LPREFIX, value);
8607 #if TARGET_MACHO
8608   else if (TARGET_MACHO)
8609     {
8610       fprintf (file, "%s%s%d-", ASM_LONG, LPREFIX, value);
8611       machopic_output_function_base_name (file);
8612       fprintf(file, "\n");
8613     }
8614 #endif
8615   else
8616     asm_fprintf (file, "%s%U%s+[.-%s%d]\n",
8617                  ASM_LONG, GOT_SYMBOL_NAME, LPREFIX, value);
8618 }
8619 \f
8620 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
8621    for the target.  */
8622
8623 void
8624 ix86_expand_clear (rtx dest)
8625 {
8626   rtx tmp;
8627
8628   /* We play register width games, which are only valid after reload.  */
8629   gcc_assert (reload_completed);
8630
8631   /* Avoid HImode and its attendant prefix byte.  */
8632   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
8633     dest = gen_rtx_REG (SImode, REGNO (dest));
8634
8635   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
8636
8637   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
8638   if (reload_completed && (!TARGET_USE_MOV0 || optimize_size))
8639     {
8640       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, 17));
8641       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
8642     }
8643
8644   emit_insn (tmp);
8645 }
8646
8647 /* X is an unchanging MEM.  If it is a constant pool reference, return
8648    the constant pool rtx, else NULL.  */
8649
8650 rtx
8651 maybe_get_pool_constant (rtx x)
8652 {
8653   x = ix86_delegitimize_address (XEXP (x, 0));
8654
8655   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
8656     return get_pool_constant (x);
8657
8658   return NULL_RTX;
8659 }
8660
8661 void
8662 ix86_expand_move (enum machine_mode mode, rtx operands[])
8663 {
8664   int strict = (reload_in_progress || reload_completed);
8665   rtx op0, op1;
8666   enum tls_model model;
8667
8668   op0 = operands[0];
8669   op1 = operands[1];
8670
8671   if (GET_CODE (op1) == SYMBOL_REF)
8672     {
8673       model = SYMBOL_REF_TLS_MODEL (op1);
8674       if (model)
8675         {
8676           op1 = legitimize_tls_address (op1, model, true);
8677           op1 = force_operand (op1, op0);
8678           if (op1 == op0)
8679             return;
8680         }
8681     }
8682   else if (GET_CODE (op1) == CONST
8683            && GET_CODE (XEXP (op1, 0)) == PLUS
8684            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
8685     {
8686       model = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (op1, 0), 0));
8687       if (model)
8688         {
8689           rtx addend = XEXP (XEXP (op1, 0), 1);
8690           op1 = legitimize_tls_address (XEXP (XEXP (op1, 0), 0), model, true);
8691           op1 = force_operand (op1, NULL);
8692           op1 = expand_simple_binop (Pmode, PLUS, op1, addend,
8693                                      op0, 1, OPTAB_DIRECT);
8694           if (op1 == op0)
8695             return;
8696         }
8697     }
8698
8699   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
8700     {
8701 #if TARGET_MACHO
8702       if (MACHOPIC_PURE)
8703         {
8704           rtx temp = ((reload_in_progress
8705                        || ((op0 && GET_CODE (op0) == REG)
8706                            && mode == Pmode))
8707                       ? op0 : gen_reg_rtx (Pmode));
8708           op1 = machopic_indirect_data_reference (op1, temp);
8709           op1 = machopic_legitimize_pic_address (op1, mode,
8710                                                  temp == op1 ? 0 : temp);
8711         }
8712       else if (MACHOPIC_INDIRECT)
8713         op1 = machopic_indirect_data_reference (op1, 0);
8714       if (op0 == op1)
8715         return;
8716 #else
8717       if (GET_CODE (op0) == MEM)
8718         op1 = force_reg (Pmode, op1);
8719       else 
8720         op1 = legitimize_address (op1, op1, Pmode);
8721 #endif /* TARGET_MACHO */
8722     }
8723   else
8724     {
8725       if (GET_CODE (op0) == MEM
8726           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
8727               || !push_operand (op0, mode))
8728           && GET_CODE (op1) == MEM)
8729         op1 = force_reg (mode, op1);
8730
8731       if (push_operand (op0, mode)
8732           && ! general_no_elim_operand (op1, mode))
8733         op1 = copy_to_mode_reg (mode, op1);
8734
8735       /* Force large constants in 64bit compilation into register
8736          to get them CSEed.  */
8737       if (TARGET_64BIT && mode == DImode
8738           && immediate_operand (op1, mode)
8739           && !x86_64_zext_immediate_operand (op1, VOIDmode)
8740           && !register_operand (op0, mode)
8741           && optimize && !reload_completed && !reload_in_progress)
8742         op1 = copy_to_mode_reg (mode, op1);
8743
8744       if (FLOAT_MODE_P (mode))
8745         {
8746           /* If we are loading a floating point constant to a register,
8747              force the value to memory now, since we'll get better code
8748              out the back end.  */
8749
8750           if (strict)
8751             ;
8752           else if (GET_CODE (op1) == CONST_DOUBLE)
8753             {
8754               op1 = validize_mem (force_const_mem (mode, op1));
8755               if (!register_operand (op0, mode))
8756                 {
8757                   rtx temp = gen_reg_rtx (mode);
8758                   emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
8759                   emit_move_insn (op0, temp);
8760                   return;
8761                 }
8762             }
8763         }
8764     }
8765
8766   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
8767 }
8768
8769 void
8770 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
8771 {
8772   rtx op0 = operands[0], op1 = operands[1];
8773
8774   /* Force constants other than zero into memory.  We do not know how
8775      the instructions used to build constants modify the upper 64 bits
8776      of the register, once we have that information we may be able
8777      to handle some of them more efficiently.  */
8778   if ((reload_in_progress | reload_completed) == 0
8779       && register_operand (op0, mode)
8780       && CONSTANT_P (op1) && op1 != CONST0_RTX (mode))
8781     op1 = validize_mem (force_const_mem (mode, op1));
8782
8783   /* Make operand1 a register if it isn't already.  */
8784   if (!no_new_pseudos
8785       && !register_operand (op0, mode)
8786       && !register_operand (op1, mode))
8787     {
8788       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
8789       return;
8790     }
8791
8792   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
8793 }
8794
8795 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go 
8796    straight to ix86_expand_vector_move.  */
8797
8798 void
8799 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
8800 {
8801   rtx op0, op1, m;
8802
8803   op0 = operands[0];
8804   op1 = operands[1];
8805
8806   if (MEM_P (op1))
8807     {
8808       /* If we're optimizing for size, movups is the smallest.  */
8809       if (optimize_size)
8810         {
8811           op0 = gen_lowpart (V4SFmode, op0);
8812           op1 = gen_lowpart (V4SFmode, op1);
8813           emit_insn (gen_sse_movups (op0, op1));
8814           return;
8815         }
8816
8817       /* ??? If we have typed data, then it would appear that using
8818          movdqu is the only way to get unaligned data loaded with
8819          integer type.  */
8820       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
8821         {
8822           op0 = gen_lowpart (V16QImode, op0);
8823           op1 = gen_lowpart (V16QImode, op1);
8824           emit_insn (gen_sse2_movdqu (op0, op1));
8825           return;
8826         }
8827
8828       if (TARGET_SSE2 && mode == V2DFmode)
8829         {
8830           rtx zero;
8831
8832           /* When SSE registers are split into halves, we can avoid
8833              writing to the top half twice.  */
8834           if (TARGET_SSE_SPLIT_REGS)
8835             {
8836               emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
8837               zero = op0;
8838             }
8839           else
8840             {
8841               /* ??? Not sure about the best option for the Intel chips.
8842                  The following would seem to satisfy; the register is
8843                  entirely cleared, breaking the dependency chain.  We
8844                  then store to the upper half, with a dependency depth
8845                  of one.  A rumor has it that Intel recommends two movsd
8846                  followed by an unpacklpd, but this is unconfirmed.  And
8847                  given that the dependency depth of the unpacklpd would
8848                  still be one, I'm not sure why this would be better.  */
8849               zero = CONST0_RTX (V2DFmode);
8850             }
8851
8852           m = adjust_address (op1, DFmode, 0);
8853           emit_insn (gen_sse2_loadlpd (op0, zero, m));
8854           m = adjust_address (op1, DFmode, 8);
8855           emit_insn (gen_sse2_loadhpd (op0, op0, m));
8856         }
8857       else
8858         {
8859           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
8860             emit_move_insn (op0, CONST0_RTX (mode));
8861           else
8862             emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
8863
8864           if (mode != V4SFmode)
8865             op0 = gen_lowpart (V4SFmode, op0);
8866           m = adjust_address (op1, V2SFmode, 0);
8867           emit_insn (gen_sse_loadlps (op0, op0, m));
8868           m = adjust_address (op1, V2SFmode, 8);
8869           emit_insn (gen_sse_loadhps (op0, op0, m));
8870         }
8871     }
8872   else if (MEM_P (op0))
8873     {
8874       /* If we're optimizing for size, movups is the smallest.  */
8875       if (optimize_size)
8876         {
8877           op0 = gen_lowpart (V4SFmode, op0);
8878           op1 = gen_lowpart (V4SFmode, op1);
8879           emit_insn (gen_sse_movups (op0, op1));
8880           return;
8881         }
8882
8883       /* ??? Similar to above, only less clear because of quote
8884          typeless stores unquote.  */
8885       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
8886           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
8887         {
8888           op0 = gen_lowpart (V16QImode, op0);
8889           op1 = gen_lowpart (V16QImode, op1);
8890           emit_insn (gen_sse2_movdqu (op0, op1));
8891           return;
8892         }
8893
8894       if (TARGET_SSE2 && mode == V2DFmode)
8895         {
8896           m = adjust_address (op0, DFmode, 0);
8897           emit_insn (gen_sse2_storelpd (m, op1));
8898           m = adjust_address (op0, DFmode, 8);
8899           emit_insn (gen_sse2_storehpd (m, op1));
8900         }
8901       else
8902         {
8903           if (mode != V4SFmode)
8904             op1 = gen_lowpart (V4SFmode, op1);
8905           m = adjust_address (op0, V2SFmode, 0);
8906           emit_insn (gen_sse_storelps (m, op1));
8907           m = adjust_address (op0, V2SFmode, 8);
8908           emit_insn (gen_sse_storehps (m, op1));
8909         }
8910     }
8911   else
8912     gcc_unreachable ();
8913 }
8914
8915 /* Expand a push in MODE.  This is some mode for which we do not support
8916    proper push instructions, at least from the registers that we expect
8917    the value to live in.  */
8918
8919 void
8920 ix86_expand_push (enum machine_mode mode, rtx x)
8921 {
8922   rtx tmp;
8923
8924   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
8925                              GEN_INT (-GET_MODE_SIZE (mode)),
8926                              stack_pointer_rtx, 1, OPTAB_DIRECT);
8927   if (tmp != stack_pointer_rtx)
8928     emit_move_insn (stack_pointer_rtx, tmp);
8929
8930   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
8931   emit_move_insn (tmp, x);
8932 }
8933
8934 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
8935    destination to use for the operation.  If different from the true
8936    destination in operands[0], a copy operation will be required.  */
8937
8938 rtx
8939 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
8940                             rtx operands[])
8941 {
8942   int matching_memory;
8943   rtx src1, src2, dst;
8944
8945   dst = operands[0];
8946   src1 = operands[1];
8947   src2 = operands[2];
8948
8949   /* Recognize <var1> = <value> <op> <var1> for commutative operators */
8950   if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
8951       && (rtx_equal_p (dst, src2)
8952           || immediate_operand (src1, mode)))
8953     {
8954       rtx temp = src1;
8955       src1 = src2;
8956       src2 = temp;
8957     }
8958
8959   /* If the destination is memory, and we do not have matching source
8960      operands, do things in registers.  */
8961   matching_memory = 0;
8962   if (GET_CODE (dst) == MEM)
8963     {
8964       if (rtx_equal_p (dst, src1))
8965         matching_memory = 1;
8966       else if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
8967                && rtx_equal_p (dst, src2))
8968         matching_memory = 2;
8969       else
8970         dst = gen_reg_rtx (mode);
8971     }
8972
8973   /* Both source operands cannot be in memory.  */
8974   if (GET_CODE (src1) == MEM && GET_CODE (src2) == MEM)
8975     {
8976       if (matching_memory != 2)
8977         src2 = force_reg (mode, src2);
8978       else
8979         src1 = force_reg (mode, src1);
8980     }
8981
8982   /* If the operation is not commutable, source 1 cannot be a constant
8983      or non-matching memory.  */
8984   if ((CONSTANT_P (src1)
8985        || (!matching_memory && GET_CODE (src1) == MEM))
8986       && GET_RTX_CLASS (code) != RTX_COMM_ARITH)
8987     src1 = force_reg (mode, src1);
8988
8989   src1 = operands[1] = src1;
8990   src2 = operands[2] = src2;
8991   return dst;
8992 }
8993
8994 /* Similarly, but assume that the destination has already been
8995    set up properly.  */
8996
8997 void
8998 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
8999                                     enum machine_mode mode, rtx operands[])
9000 {
9001   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
9002   gcc_assert (dst == operands[0]);
9003 }
9004
9005 /* Attempt to expand a binary operator.  Make the expansion closer to the
9006    actual machine, then just general_operand, which will allow 3 separate
9007    memory references (one output, two input) in a single insn.  */
9008
9009 void
9010 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
9011                              rtx operands[])
9012 {
9013   rtx src1, src2, dst, op, clob;
9014
9015   dst = ix86_fixup_binary_operands (code, mode, operands);
9016   src1 = operands[1];
9017   src2 = operands[2];
9018
9019  /* Emit the instruction.  */
9020
9021   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
9022   if (reload_in_progress)
9023     {
9024       /* Reload doesn't know about the flags register, and doesn't know that
9025          it doesn't want to clobber it.  We can only do this with PLUS.  */
9026       gcc_assert (code == PLUS);
9027       emit_insn (op);
9028     }
9029   else
9030     {
9031       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9032       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
9033     }
9034
9035   /* Fix up the destination if needed.  */
9036   if (dst != operands[0])
9037     emit_move_insn (operands[0], dst);
9038 }
9039
9040 /* Return TRUE or FALSE depending on whether the binary operator meets the
9041    appropriate constraints.  */
9042
9043 int
9044 ix86_binary_operator_ok (enum rtx_code code,
9045                          enum machine_mode mode ATTRIBUTE_UNUSED,
9046                          rtx operands[3])
9047 {
9048   /* Both source operands cannot be in memory.  */
9049   if (GET_CODE (operands[1]) == MEM && GET_CODE (operands[2]) == MEM)
9050     return 0;
9051   /* If the operation is not commutable, source 1 cannot be a constant.  */
9052   if (CONSTANT_P (operands[1]) && GET_RTX_CLASS (code) != RTX_COMM_ARITH)
9053     return 0;
9054   /* If the destination is memory, we must have a matching source operand.  */
9055   if (GET_CODE (operands[0]) == MEM
9056       && ! (rtx_equal_p (operands[0], operands[1])
9057             || (GET_RTX_CLASS (code) == RTX_COMM_ARITH
9058                 && rtx_equal_p (operands[0], operands[2]))))
9059     return 0;
9060   /* If the operation is not commutable and the source 1 is memory, we must
9061      have a matching destination.  */
9062   if (GET_CODE (operands[1]) == MEM
9063       && GET_RTX_CLASS (code) != RTX_COMM_ARITH
9064       && ! rtx_equal_p (operands[0], operands[1]))
9065     return 0;
9066   return 1;
9067 }
9068
9069 /* Attempt to expand a unary operator.  Make the expansion closer to the
9070    actual machine, then just general_operand, which will allow 2 separate
9071    memory references (one output, one input) in a single insn.  */
9072
9073 void
9074 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
9075                             rtx operands[])
9076 {
9077   int matching_memory;
9078   rtx src, dst, op, clob;
9079
9080   dst = operands[0];
9081   src = operands[1];
9082
9083   /* If the destination is memory, and we do not have matching source
9084      operands, do things in registers.  */
9085   matching_memory = 0;
9086   if (MEM_P (dst))
9087     {
9088       if (rtx_equal_p (dst, src))
9089         matching_memory = 1;
9090       else
9091         dst = gen_reg_rtx (mode);
9092     }
9093
9094   /* When source operand is memory, destination must match.  */
9095   if (MEM_P (src) && !matching_memory)
9096     src = force_reg (mode, src);
9097
9098   /* Emit the instruction.  */
9099
9100   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
9101   if (reload_in_progress || code == NOT)
9102     {
9103       /* Reload doesn't know about the flags register, and doesn't know that
9104          it doesn't want to clobber it.  */
9105       gcc_assert (code == NOT);
9106       emit_insn (op);
9107     }
9108   else
9109     {
9110       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9111       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
9112     }
9113
9114   /* Fix up the destination if needed.  */
9115   if (dst != operands[0])
9116     emit_move_insn (operands[0], dst);
9117 }
9118
9119 /* Return TRUE or FALSE depending on whether the unary operator meets the
9120    appropriate constraints.  */
9121
9122 int
9123 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
9124                         enum machine_mode mode ATTRIBUTE_UNUSED,
9125                         rtx operands[2] ATTRIBUTE_UNUSED)
9126 {
9127   /* If one of operands is memory, source and destination must match.  */
9128   if ((GET_CODE (operands[0]) == MEM
9129        || GET_CODE (operands[1]) == MEM)
9130       && ! rtx_equal_p (operands[0], operands[1]))
9131     return FALSE;
9132   return TRUE;
9133 }
9134
9135 /* A subroutine of ix86_expand_fp_absneg_operator and copysign expanders.
9136    Create a mask for the sign bit in MODE for an SSE register.  If VECT is
9137    true, then replicate the mask for all elements of the vector register.
9138    If INVERT is true, then create a mask excluding the sign bit.  */
9139
9140 rtx
9141 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
9142 {
9143   enum machine_mode vec_mode;
9144   HOST_WIDE_INT hi, lo;
9145   int shift = 63;
9146   rtvec v;
9147   rtx mask;
9148
9149   /* Find the sign bit, sign extended to 2*HWI.  */
9150   if (mode == SFmode)
9151     lo = 0x80000000, hi = lo < 0;
9152   else if (HOST_BITS_PER_WIDE_INT >= 64)
9153     lo = (HOST_WIDE_INT)1 << shift, hi = -1;
9154   else
9155     lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
9156
9157   if (invert)
9158     lo = ~lo, hi = ~hi;
9159
9160   /* Force this value into the low part of a fp vector constant.  */
9161   mask = immed_double_const (lo, hi, mode == SFmode ? SImode : DImode);
9162   mask = gen_lowpart (mode, mask);
9163
9164   if (mode == SFmode)
9165     {
9166       if (vect)
9167         v = gen_rtvec (4, mask, mask, mask, mask);
9168       else
9169         v = gen_rtvec (4, mask, CONST0_RTX (SFmode),
9170                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
9171       vec_mode = V4SFmode;
9172     }
9173   else
9174     {
9175       if (vect)
9176         v = gen_rtvec (2, mask, mask);
9177       else
9178         v = gen_rtvec (2, mask, CONST0_RTX (DFmode));
9179       vec_mode = V2DFmode;
9180     }
9181
9182   return force_reg (vec_mode, gen_rtx_CONST_VECTOR (vec_mode, v));
9183 }
9184
9185 /* Generate code for floating point ABS or NEG.  */
9186
9187 void
9188 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
9189                                 rtx operands[])
9190 {
9191   rtx mask, set, use, clob, dst, src;
9192   bool matching_memory;
9193   bool use_sse = false;
9194   bool vector_mode = VECTOR_MODE_P (mode);
9195   enum machine_mode elt_mode = mode;
9196
9197   if (vector_mode)
9198     {
9199       elt_mode = GET_MODE_INNER (mode);
9200       use_sse = true;
9201     }
9202   else if (TARGET_SSE_MATH)
9203     use_sse = SSE_FLOAT_MODE_P (mode);
9204
9205   /* NEG and ABS performed with SSE use bitwise mask operations.
9206      Create the appropriate mask now.  */
9207   if (use_sse)
9208     mask = ix86_build_signbit_mask (elt_mode, vector_mode, code == ABS);
9209   else
9210     {
9211       /* When not using SSE, we don't use the mask, but prefer to keep the
9212          same general form of the insn pattern to reduce duplication when
9213          it comes time to split.  */
9214       mask = const0_rtx;
9215     }
9216
9217   dst = operands[0];
9218   src = operands[1];
9219
9220   /* If the destination is memory, and we don't have matching source
9221      operands, do things in registers.  */
9222   matching_memory = false;
9223   if (MEM_P (dst))
9224     {
9225       if (rtx_equal_p (dst, src))
9226         matching_memory = true;
9227       else
9228         dst = gen_reg_rtx (mode);
9229     }
9230   if (MEM_P (src) && !matching_memory)
9231     src = force_reg (mode, src);
9232
9233   if (vector_mode)
9234     {
9235       set = gen_rtx_fmt_ee (code == NEG ? XOR : AND, mode, src, mask);
9236       set = gen_rtx_SET (VOIDmode, dst, set);
9237       emit_insn (set);
9238     }
9239   else
9240     {
9241       set = gen_rtx_fmt_e (code, mode, src);
9242       set = gen_rtx_SET (VOIDmode, dst, set);
9243       use = gen_rtx_USE (VOIDmode, mask);
9244       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9245       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (3, set, use, clob)));
9246     }
9247
9248   if (dst != operands[0])
9249     emit_move_insn (operands[0], dst);
9250 }
9251
9252 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
9253
9254 void
9255 ix86_expand_copysign (rtx operands[])
9256 {
9257   enum machine_mode mode, vmode;
9258   rtx dest, op0, op1, mask, nmask;
9259
9260   dest = operands[0];
9261   op0 = operands[1];
9262   op1 = operands[2];
9263
9264   mode = GET_MODE (dest);
9265   vmode = mode == SFmode ? V4SFmode : V2DFmode;
9266
9267   if (GET_CODE (op0) == CONST_DOUBLE)
9268     {
9269       rtvec v;
9270
9271       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
9272         op0 = simplify_unary_operation (ABS, mode, op0, mode);
9273
9274       if (op0 == CONST0_RTX (mode))
9275         op0 = CONST0_RTX (vmode);
9276       else
9277         {
9278           if (mode == SFmode)
9279             v = gen_rtvec (4, op0, CONST0_RTX (SFmode),
9280                            CONST0_RTX (SFmode), CONST0_RTX (SFmode));
9281           else
9282             v = gen_rtvec (2, op0, CONST0_RTX (DFmode));
9283           op0 = force_reg (vmode, gen_rtx_CONST_VECTOR (vmode, v));
9284         }
9285
9286       mask = ix86_build_signbit_mask (mode, 0, 0);
9287
9288       if (mode == SFmode)
9289         emit_insn (gen_copysignsf3_const (dest, op0, op1, mask));
9290       else
9291         emit_insn (gen_copysigndf3_const (dest, op0, op1, mask));
9292     }
9293   else
9294     {
9295       nmask = ix86_build_signbit_mask (mode, 0, 1);
9296       mask = ix86_build_signbit_mask (mode, 0, 0);
9297
9298       if (mode == SFmode)
9299         emit_insn (gen_copysignsf3_var (dest, NULL, op0, op1, nmask, mask));
9300       else
9301         emit_insn (gen_copysigndf3_var (dest, NULL, op0, op1, nmask, mask));
9302     }
9303 }
9304
9305 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
9306    be a constant, and so has already been expanded into a vector constant.  */
9307
9308 void
9309 ix86_split_copysign_const (rtx operands[])
9310 {
9311   enum machine_mode mode, vmode;
9312   rtx dest, op0, op1, mask, x;
9313
9314   dest = operands[0];
9315   op0 = operands[1];
9316   op1 = operands[2];
9317   mask = operands[3];
9318
9319   mode = GET_MODE (dest);
9320   vmode = GET_MODE (mask);
9321
9322   dest = simplify_gen_subreg (vmode, dest, mode, 0);
9323   x = gen_rtx_AND (vmode, dest, mask);
9324   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9325
9326   if (op0 != CONST0_RTX (vmode))
9327     {
9328       x = gen_rtx_IOR (vmode, dest, op0);
9329       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9330     }
9331 }
9332
9333 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
9334    so we have to do two masks.  */
9335
9336 void
9337 ix86_split_copysign_var (rtx operands[])
9338 {
9339   enum machine_mode mode, vmode;
9340   rtx dest, scratch, op0, op1, mask, nmask, x;
9341
9342   dest = operands[0];
9343   scratch = operands[1];
9344   op0 = operands[2];
9345   op1 = operands[3];
9346   nmask = operands[4];
9347   mask = operands[5];
9348
9349   mode = GET_MODE (dest);
9350   vmode = GET_MODE (mask);
9351
9352   if (rtx_equal_p (op0, op1))
9353     {
9354       /* Shouldn't happen often (it's useless, obviously), but when it does
9355          we'd generate incorrect code if we continue below.  */
9356       emit_move_insn (dest, op0);
9357       return;
9358     }
9359
9360   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
9361     {
9362       gcc_assert (REGNO (op1) == REGNO (scratch));
9363
9364       x = gen_rtx_AND (vmode, scratch, mask);
9365       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
9366
9367       dest = mask;
9368       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
9369       x = gen_rtx_NOT (vmode, dest);
9370       x = gen_rtx_AND (vmode, x, op0);
9371       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9372     }
9373   else
9374     {
9375       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
9376         {
9377           x = gen_rtx_AND (vmode, scratch, mask);
9378         }
9379       else                                              /* alternative 2,4 */
9380         {
9381           gcc_assert (REGNO (mask) == REGNO (scratch));
9382           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
9383           x = gen_rtx_AND (vmode, scratch, op1);
9384         }
9385       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
9386
9387       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
9388         {
9389           dest = simplify_gen_subreg (vmode, op0, mode, 0);
9390           x = gen_rtx_AND (vmode, dest, nmask);
9391         }
9392       else                                              /* alternative 3,4 */
9393         {
9394           gcc_assert (REGNO (nmask) == REGNO (dest));
9395           dest = nmask;
9396           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
9397           x = gen_rtx_AND (vmode, dest, op0);
9398         }
9399       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9400     }
9401
9402   x = gen_rtx_IOR (vmode, dest, scratch);
9403   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9404 }
9405
9406 /* Return TRUE or FALSE depending on whether the first SET in INSN
9407    has source and destination with matching CC modes, and that the
9408    CC mode is at least as constrained as REQ_MODE.  */
9409
9410 int
9411 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
9412 {
9413   rtx set;
9414   enum machine_mode set_mode;
9415
9416   set = PATTERN (insn);
9417   if (GET_CODE (set) == PARALLEL)
9418     set = XVECEXP (set, 0, 0);
9419   gcc_assert (GET_CODE (set) == SET);
9420   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
9421
9422   set_mode = GET_MODE (SET_DEST (set));
9423   switch (set_mode)
9424     {
9425     case CCNOmode:
9426       if (req_mode != CCNOmode
9427           && (req_mode != CCmode
9428               || XEXP (SET_SRC (set), 1) != const0_rtx))
9429         return 0;
9430       break;
9431     case CCmode:
9432       if (req_mode == CCGCmode)
9433         return 0;
9434       /* FALLTHRU */
9435     case CCGCmode:
9436       if (req_mode == CCGOCmode || req_mode == CCNOmode)
9437         return 0;
9438       /* FALLTHRU */
9439     case CCGOCmode:
9440       if (req_mode == CCZmode)
9441         return 0;
9442       /* FALLTHRU */
9443     case CCZmode:
9444       break;
9445
9446     default:
9447       gcc_unreachable ();
9448     }
9449
9450   return (GET_MODE (SET_SRC (set)) == set_mode);
9451 }
9452
9453 /* Generate insn patterns to do an integer compare of OPERANDS.  */
9454
9455 static rtx
9456 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
9457 {
9458   enum machine_mode cmpmode;
9459   rtx tmp, flags;
9460
9461   cmpmode = SELECT_CC_MODE (code, op0, op1);
9462   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
9463
9464   /* This is very simple, but making the interface the same as in the
9465      FP case makes the rest of the code easier.  */
9466   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
9467   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
9468
9469   /* Return the test that should be put into the flags user, i.e.
9470      the bcc, scc, or cmov instruction.  */
9471   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
9472 }
9473
9474 /* Figure out whether to use ordered or unordered fp comparisons.
9475    Return the appropriate mode to use.  */
9476
9477 enum machine_mode
9478 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
9479 {
9480   /* ??? In order to make all comparisons reversible, we do all comparisons
9481      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
9482      all forms trapping and nontrapping comparisons, we can make inequality
9483      comparisons trapping again, since it results in better code when using
9484      FCOM based compares.  */
9485   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
9486 }
9487
9488 enum machine_mode
9489 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
9490 {
9491   if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
9492     return ix86_fp_compare_mode (code);
9493   switch (code)
9494     {
9495       /* Only zero flag is needed.  */
9496     case EQ:                    /* ZF=0 */
9497     case NE:                    /* ZF!=0 */
9498       return CCZmode;
9499       /* Codes needing carry flag.  */
9500     case GEU:                   /* CF=0 */
9501     case GTU:                   /* CF=0 & ZF=0 */
9502     case LTU:                   /* CF=1 */
9503     case LEU:                   /* CF=1 | ZF=1 */
9504       return CCmode;
9505       /* Codes possibly doable only with sign flag when
9506          comparing against zero.  */
9507     case GE:                    /* SF=OF   or   SF=0 */
9508     case LT:                    /* SF<>OF  or   SF=1 */
9509       if (op1 == const0_rtx)
9510         return CCGOCmode;
9511       else
9512         /* For other cases Carry flag is not required.  */
9513         return CCGCmode;
9514       /* Codes doable only with sign flag when comparing
9515          against zero, but we miss jump instruction for it
9516          so we need to use relational tests against overflow
9517          that thus needs to be zero.  */
9518     case GT:                    /* ZF=0 & SF=OF */
9519     case LE:                    /* ZF=1 | SF<>OF */
9520       if (op1 == const0_rtx)
9521         return CCNOmode;
9522       else
9523         return CCGCmode;
9524       /* strcmp pattern do (use flags) and combine may ask us for proper
9525          mode.  */
9526     case USE:
9527       return CCmode;
9528     default:
9529       gcc_unreachable ();
9530     }
9531 }
9532
9533 /* Return the fixed registers used for condition codes.  */
9534
9535 static bool
9536 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
9537 {
9538   *p1 = FLAGS_REG;
9539   *p2 = FPSR_REG;
9540   return true;
9541 }
9542
9543 /* If two condition code modes are compatible, return a condition code
9544    mode which is compatible with both.  Otherwise, return
9545    VOIDmode.  */
9546
9547 static enum machine_mode
9548 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
9549 {
9550   if (m1 == m2)
9551     return m1;
9552
9553   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
9554     return VOIDmode;
9555
9556   if ((m1 == CCGCmode && m2 == CCGOCmode)
9557       || (m1 == CCGOCmode && m2 == CCGCmode))
9558     return CCGCmode;
9559
9560   switch (m1)
9561     {
9562     default:
9563       gcc_unreachable ();
9564
9565     case CCmode:
9566     case CCGCmode:
9567     case CCGOCmode:
9568     case CCNOmode:
9569     case CCZmode:
9570       switch (m2)
9571         {
9572         default:
9573           return VOIDmode;
9574
9575         case CCmode:
9576         case CCGCmode:
9577         case CCGOCmode:
9578         case CCNOmode:
9579         case CCZmode:
9580           return CCmode;
9581         }
9582
9583     case CCFPmode:
9584     case CCFPUmode:
9585       /* These are only compatible with themselves, which we already
9586          checked above.  */
9587       return VOIDmode;
9588     }
9589 }
9590
9591 /* Return true if we should use an FCOMI instruction for this fp comparison.  */
9592
9593 int
9594 ix86_use_fcomi_compare (enum rtx_code code ATTRIBUTE_UNUSED)
9595 {
9596   enum rtx_code swapped_code = swap_condition (code);
9597   return ((ix86_fp_comparison_cost (code) == ix86_fp_comparison_fcomi_cost (code))
9598           || (ix86_fp_comparison_cost (swapped_code)
9599               == ix86_fp_comparison_fcomi_cost (swapped_code)));
9600 }
9601
9602 /* Swap, force into registers, or otherwise massage the two operands
9603    to a fp comparison.  The operands are updated in place; the new
9604    comparison code is returned.  */
9605
9606 static enum rtx_code
9607 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
9608 {
9609   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
9610   rtx op0 = *pop0, op1 = *pop1;
9611   enum machine_mode op_mode = GET_MODE (op0);
9612   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
9613
9614   /* All of the unordered compare instructions only work on registers.
9615      The same is true of the fcomi compare instructions.  The XFmode
9616      compare instructions require registers except when comparing
9617      against zero or when converting operand 1 from fixed point to
9618      floating point.  */
9619
9620   if (!is_sse
9621       && (fpcmp_mode == CCFPUmode
9622           || (op_mode == XFmode
9623               && ! (standard_80387_constant_p (op0) == 1
9624                     || standard_80387_constant_p (op1) == 1)
9625               && GET_CODE (op1) != FLOAT)
9626           || ix86_use_fcomi_compare (code)))
9627     {
9628       op0 = force_reg (op_mode, op0);
9629       op1 = force_reg (op_mode, op1);
9630     }
9631   else
9632     {
9633       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
9634          things around if they appear profitable, otherwise force op0
9635          into a register.  */
9636
9637       if (standard_80387_constant_p (op0) == 0
9638           || (GET_CODE (op0) == MEM
9639               && ! (standard_80387_constant_p (op1) == 0
9640                     || GET_CODE (op1) == MEM)))
9641         {
9642           rtx tmp;
9643           tmp = op0, op0 = op1, op1 = tmp;
9644           code = swap_condition (code);
9645         }
9646
9647       if (GET_CODE (op0) != REG)
9648         op0 = force_reg (op_mode, op0);
9649
9650       if (CONSTANT_P (op1))
9651         {
9652           int tmp = standard_80387_constant_p (op1);
9653           if (tmp == 0)
9654             op1 = validize_mem (force_const_mem (op_mode, op1));
9655           else if (tmp == 1)
9656             {
9657               if (TARGET_CMOVE)
9658                 op1 = force_reg (op_mode, op1);
9659             }
9660           else
9661             op1 = force_reg (op_mode, op1);
9662         }
9663     }
9664
9665   /* Try to rearrange the comparison to make it cheaper.  */
9666   if (ix86_fp_comparison_cost (code)
9667       > ix86_fp_comparison_cost (swap_condition (code))
9668       && (GET_CODE (op1) == REG || !no_new_pseudos))
9669     {
9670       rtx tmp;
9671       tmp = op0, op0 = op1, op1 = tmp;
9672       code = swap_condition (code);
9673       if (GET_CODE (op0) != REG)
9674         op0 = force_reg (op_mode, op0);
9675     }
9676
9677   *pop0 = op0;
9678   *pop1 = op1;
9679   return code;
9680 }
9681
9682 /* Convert comparison codes we use to represent FP comparison to integer
9683    code that will result in proper branch.  Return UNKNOWN if no such code
9684    is available.  */
9685
9686 enum rtx_code
9687 ix86_fp_compare_code_to_integer (enum rtx_code code)
9688 {
9689   switch (code)
9690     {
9691     case GT:
9692       return GTU;
9693     case GE:
9694       return GEU;
9695     case ORDERED:
9696     case UNORDERED:
9697       return code;
9698       break;
9699     case UNEQ:
9700       return EQ;
9701       break;
9702     case UNLT:
9703       return LTU;
9704       break;
9705     case UNLE:
9706       return LEU;
9707       break;
9708     case LTGT:
9709       return NE;
9710       break;
9711     default:
9712       return UNKNOWN;
9713     }
9714 }
9715
9716 /* Split comparison code CODE into comparisons we can do using branch
9717    instructions.  BYPASS_CODE is comparison code for branch that will
9718    branch around FIRST_CODE and SECOND_CODE.  If some of branches
9719    is not required, set value to UNKNOWN.
9720    We never require more than two branches.  */
9721
9722 void
9723 ix86_fp_comparison_codes (enum rtx_code code, enum rtx_code *bypass_code,
9724                           enum rtx_code *first_code,
9725                           enum rtx_code *second_code)
9726 {
9727   *first_code = code;
9728   *bypass_code = UNKNOWN;
9729   *second_code = UNKNOWN;
9730
9731   /* The fcomi comparison sets flags as follows:
9732
9733      cmp    ZF PF CF
9734      >      0  0  0
9735      <      0  0  1
9736      =      1  0  0
9737      un     1  1  1 */
9738
9739   switch (code)
9740     {
9741     case GT:                    /* GTU - CF=0 & ZF=0 */
9742     case GE:                    /* GEU - CF=0 */
9743     case ORDERED:               /* PF=0 */
9744     case UNORDERED:             /* PF=1 */
9745     case UNEQ:                  /* EQ - ZF=1 */
9746     case UNLT:                  /* LTU - CF=1 */
9747     case UNLE:                  /* LEU - CF=1 | ZF=1 */
9748     case LTGT:                  /* EQ - ZF=0 */
9749       break;
9750     case LT:                    /* LTU - CF=1 - fails on unordered */
9751       *first_code = UNLT;
9752       *bypass_code = UNORDERED;
9753       break;
9754     case LE:                    /* LEU - CF=1 | ZF=1 - fails on unordered */
9755       *first_code = UNLE;
9756       *bypass_code = UNORDERED;
9757       break;
9758     case EQ:                    /* EQ - ZF=1 - fails on unordered */
9759       *first_code = UNEQ;
9760       *bypass_code = UNORDERED;
9761       break;
9762     case NE:                    /* NE - ZF=0 - fails on unordered */
9763       *first_code = LTGT;
9764       *second_code = UNORDERED;
9765       break;
9766     case UNGE:                  /* GEU - CF=0 - fails on unordered */
9767       *first_code = GE;
9768       *second_code = UNORDERED;
9769       break;
9770     case UNGT:                  /* GTU - CF=0 & ZF=0 - fails on unordered */
9771       *first_code = GT;
9772       *second_code = UNORDERED;
9773       break;
9774     default:
9775       gcc_unreachable ();
9776     }
9777   if (!TARGET_IEEE_FP)
9778     {
9779       *second_code = UNKNOWN;
9780       *bypass_code = UNKNOWN;
9781     }
9782 }
9783
9784 /* Return cost of comparison done fcom + arithmetics operations on AX.
9785    All following functions do use number of instructions as a cost metrics.
9786    In future this should be tweaked to compute bytes for optimize_size and
9787    take into account performance of various instructions on various CPUs.  */
9788 static int
9789 ix86_fp_comparison_arithmetics_cost (enum rtx_code code)
9790 {
9791   if (!TARGET_IEEE_FP)
9792     return 4;
9793   /* The cost of code output by ix86_expand_fp_compare.  */
9794   switch (code)
9795     {
9796     case UNLE:
9797     case UNLT:
9798     case LTGT:
9799     case GT:
9800     case GE:
9801     case UNORDERED:
9802     case ORDERED:
9803     case UNEQ:
9804       return 4;
9805       break;
9806     case LT:
9807     case NE:
9808     case EQ:
9809     case UNGE:
9810       return 5;
9811       break;
9812     case LE:
9813     case UNGT:
9814       return 6;
9815       break;
9816     default:
9817       gcc_unreachable ();
9818     }
9819 }
9820
9821 /* Return cost of comparison done using fcomi operation.
9822    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
9823 static int
9824 ix86_fp_comparison_fcomi_cost (enum rtx_code code)
9825 {
9826   enum rtx_code bypass_code, first_code, second_code;
9827   /* Return arbitrarily high cost when instruction is not supported - this
9828      prevents gcc from using it.  */
9829   if (!TARGET_CMOVE)
9830     return 1024;
9831   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
9832   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 2;
9833 }
9834
9835 /* Return cost of comparison done using sahf operation.
9836    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
9837 static int
9838 ix86_fp_comparison_sahf_cost (enum rtx_code code)
9839 {
9840   enum rtx_code bypass_code, first_code, second_code;
9841   /* Return arbitrarily high cost when instruction is not preferred - this
9842      avoids gcc from using it.  */
9843   if (!TARGET_USE_SAHF && !optimize_size)
9844     return 1024;
9845   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
9846   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 3;
9847 }
9848
9849 /* Compute cost of the comparison done using any method.
9850    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
9851 static int
9852 ix86_fp_comparison_cost (enum rtx_code code)
9853 {
9854   int fcomi_cost, sahf_cost, arithmetics_cost = 1024;
9855   int min;
9856
9857   fcomi_cost = ix86_fp_comparison_fcomi_cost (code);
9858   sahf_cost = ix86_fp_comparison_sahf_cost (code);
9859
9860   min = arithmetics_cost = ix86_fp_comparison_arithmetics_cost (code);
9861   if (min > sahf_cost)
9862     min = sahf_cost;
9863   if (min > fcomi_cost)
9864     min = fcomi_cost;
9865   return min;
9866 }
9867
9868 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
9869
9870 static rtx
9871 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch,
9872                         rtx *second_test, rtx *bypass_test)
9873 {
9874   enum machine_mode fpcmp_mode, intcmp_mode;
9875   rtx tmp, tmp2;
9876   int cost = ix86_fp_comparison_cost (code);
9877   enum rtx_code bypass_code, first_code, second_code;
9878
9879   fpcmp_mode = ix86_fp_compare_mode (code);
9880   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
9881
9882   if (second_test)
9883     *second_test = NULL_RTX;
9884   if (bypass_test)
9885     *bypass_test = NULL_RTX;
9886
9887   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
9888
9889   /* Do fcomi/sahf based test when profitable.  */
9890   if ((bypass_code == UNKNOWN || bypass_test)
9891       && (second_code == UNKNOWN || second_test)
9892       && ix86_fp_comparison_arithmetics_cost (code) > cost)
9893     {
9894       if (TARGET_CMOVE)
9895         {
9896           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
9897           tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
9898                              tmp);
9899           emit_insn (tmp);
9900         }
9901       else
9902         {
9903           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
9904           tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
9905           if (!scratch)
9906             scratch = gen_reg_rtx (HImode);
9907           emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
9908           emit_insn (gen_x86_sahf_1 (scratch));
9909         }
9910
9911       /* The FP codes work out to act like unsigned.  */
9912       intcmp_mode = fpcmp_mode;
9913       code = first_code;
9914       if (bypass_code != UNKNOWN)
9915         *bypass_test = gen_rtx_fmt_ee (bypass_code, VOIDmode,
9916                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
9917                                        const0_rtx);
9918       if (second_code != UNKNOWN)
9919         *second_test = gen_rtx_fmt_ee (second_code, VOIDmode,
9920                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
9921                                        const0_rtx);
9922     }
9923   else
9924     {
9925       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
9926       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
9927       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
9928       if (!scratch)
9929         scratch = gen_reg_rtx (HImode);
9930       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
9931
9932       /* In the unordered case, we have to check C2 for NaN's, which
9933          doesn't happen to work out to anything nice combination-wise.
9934          So do some bit twiddling on the value we've got in AH to come
9935          up with an appropriate set of condition codes.  */
9936
9937       intcmp_mode = CCNOmode;
9938       switch (code)
9939         {
9940         case GT:
9941         case UNGT:
9942           if (code == GT || !TARGET_IEEE_FP)
9943             {
9944               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
9945               code = EQ;
9946             }
9947           else
9948             {
9949               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9950               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
9951               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
9952               intcmp_mode = CCmode;
9953               code = GEU;
9954             }
9955           break;
9956         case LT:
9957         case UNLT:
9958           if (code == LT && TARGET_IEEE_FP)
9959             {
9960               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9961               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x01)));
9962               intcmp_mode = CCmode;
9963               code = EQ;
9964             }
9965           else
9966             {
9967               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x01)));
9968               code = NE;
9969             }
9970           break;
9971         case GE:
9972         case UNGE:
9973           if (code == GE || !TARGET_IEEE_FP)
9974             {
9975               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
9976               code = EQ;
9977             }
9978           else
9979             {
9980               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9981               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
9982                                              GEN_INT (0x01)));
9983               code = NE;
9984             }
9985           break;
9986         case LE:
9987         case UNLE:
9988           if (code == LE && TARGET_IEEE_FP)
9989             {
9990               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9991               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
9992               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
9993               intcmp_mode = CCmode;
9994               code = LTU;
9995             }
9996           else
9997             {
9998               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
9999               code = NE;
10000             }
10001           break;
10002         case EQ:
10003         case UNEQ:
10004           if (code == EQ && TARGET_IEEE_FP)
10005             {
10006               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10007               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
10008               intcmp_mode = CCmode;
10009               code = EQ;
10010             }
10011           else
10012             {
10013               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
10014               code = NE;
10015               break;
10016             }
10017           break;
10018         case NE:
10019         case LTGT:
10020           if (code == NE && TARGET_IEEE_FP)
10021             {
10022               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10023               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
10024                                              GEN_INT (0x40)));
10025               code = NE;
10026             }
10027           else
10028             {
10029               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
10030               code = EQ;
10031             }
10032           break;
10033
10034         case UNORDERED:
10035           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
10036           code = NE;
10037           break;
10038         case ORDERED:
10039           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
10040           code = EQ;
10041           break;
10042
10043         default:
10044           gcc_unreachable ();
10045         }
10046     }
10047
10048   /* Return the test that should be put into the flags user, i.e.
10049      the bcc, scc, or cmov instruction.  */
10050   return gen_rtx_fmt_ee (code, VOIDmode,
10051                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
10052                          const0_rtx);
10053 }
10054
10055 rtx
10056 ix86_expand_compare (enum rtx_code code, rtx *second_test, rtx *bypass_test)
10057 {
10058   rtx op0, op1, ret;
10059   op0 = ix86_compare_op0;
10060   op1 = ix86_compare_op1;
10061
10062   if (second_test)
10063     *second_test = NULL_RTX;
10064   if (bypass_test)
10065     *bypass_test = NULL_RTX;
10066
10067   if (ix86_compare_emitted)
10068     {
10069       ret = gen_rtx_fmt_ee (code, VOIDmode, ix86_compare_emitted, const0_rtx);
10070       ix86_compare_emitted = NULL_RTX;
10071     }
10072   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
10073     ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
10074                                   second_test, bypass_test);
10075   else
10076     ret = ix86_expand_int_compare (code, op0, op1);
10077
10078   return ret;
10079 }
10080
10081 /* Return true if the CODE will result in nontrivial jump sequence.  */
10082 bool
10083 ix86_fp_jump_nontrivial_p (enum rtx_code code)
10084 {
10085   enum rtx_code bypass_code, first_code, second_code;
10086   if (!TARGET_CMOVE)
10087     return true;
10088   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10089   return bypass_code != UNKNOWN || second_code != UNKNOWN;
10090 }
10091
10092 void
10093 ix86_expand_branch (enum rtx_code code, rtx label)
10094 {
10095   rtx tmp;
10096
10097   switch (GET_MODE (ix86_compare_op0))
10098     {
10099     case QImode:
10100     case HImode:
10101     case SImode:
10102       simple:
10103       tmp = ix86_expand_compare (code, NULL, NULL);
10104       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
10105                                   gen_rtx_LABEL_REF (VOIDmode, label),
10106                                   pc_rtx);
10107       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
10108       return;
10109
10110     case SFmode:
10111     case DFmode:
10112     case XFmode:
10113       {
10114         rtvec vec;
10115         int use_fcomi;
10116         enum rtx_code bypass_code, first_code, second_code;
10117
10118         code = ix86_prepare_fp_compare_args (code, &ix86_compare_op0,
10119                                              &ix86_compare_op1);
10120
10121         ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10122
10123         /* Check whether we will use the natural sequence with one jump.  If
10124            so, we can expand jump early.  Otherwise delay expansion by
10125            creating compound insn to not confuse optimizers.  */
10126         if (bypass_code == UNKNOWN && second_code == UNKNOWN
10127             && TARGET_CMOVE)
10128           {
10129             ix86_split_fp_branch (code, ix86_compare_op0, ix86_compare_op1,
10130                                   gen_rtx_LABEL_REF (VOIDmode, label),
10131                                   pc_rtx, NULL_RTX, NULL_RTX);
10132           }
10133         else
10134           {
10135             tmp = gen_rtx_fmt_ee (code, VOIDmode,
10136                                   ix86_compare_op0, ix86_compare_op1);
10137             tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
10138                                         gen_rtx_LABEL_REF (VOIDmode, label),
10139                                         pc_rtx);
10140             tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);
10141
10142             use_fcomi = ix86_use_fcomi_compare (code);
10143             vec = rtvec_alloc (3 + !use_fcomi);
10144             RTVEC_ELT (vec, 0) = tmp;
10145             RTVEC_ELT (vec, 1)
10146               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 18));
10147             RTVEC_ELT (vec, 2)
10148               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 17));
10149             if (! use_fcomi)
10150               RTVEC_ELT (vec, 3)
10151                 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));
10152
10153             emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
10154           }
10155         return;
10156       }
10157
10158     case DImode:
10159       if (TARGET_64BIT)
10160         goto simple;
10161     case TImode:
10162       /* Expand DImode branch into multiple compare+branch.  */
10163       {
10164         rtx lo[2], hi[2], label2;
10165         enum rtx_code code1, code2, code3;
10166         enum machine_mode submode;
10167
10168         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
10169           {
10170             tmp = ix86_compare_op0;
10171             ix86_compare_op0 = ix86_compare_op1;
10172             ix86_compare_op1 = tmp;
10173             code = swap_condition (code);
10174           }
10175         if (GET_MODE (ix86_compare_op0) == DImode)
10176           {
10177             split_di (&ix86_compare_op0, 1, lo+0, hi+0);
10178             split_di (&ix86_compare_op1, 1, lo+1, hi+1);
10179             submode = SImode;
10180           }
10181         else
10182           {
10183             split_ti (&ix86_compare_op0, 1, lo+0, hi+0);
10184             split_ti (&ix86_compare_op1, 1, lo+1, hi+1);
10185             submode = DImode;
10186           }
10187
10188         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
10189            avoid two branches.  This costs one extra insn, so disable when
10190            optimizing for size.  */
10191
10192         if ((code == EQ || code == NE)
10193             && (!optimize_size
10194                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
10195           {
10196             rtx xor0, xor1;
10197
10198             xor1 = hi[0];
10199             if (hi[1] != const0_rtx)
10200               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
10201                                    NULL_RTX, 0, OPTAB_WIDEN);
10202
10203             xor0 = lo[0];
10204             if (lo[1] != const0_rtx)
10205               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
10206                                    NULL_RTX, 0, OPTAB_WIDEN);
10207
10208             tmp = expand_binop (submode, ior_optab, xor1, xor0,
10209                                 NULL_RTX, 0, OPTAB_WIDEN);
10210
10211             ix86_compare_op0 = tmp;
10212             ix86_compare_op1 = const0_rtx;
10213             ix86_expand_branch (code, label);
10214             return;
10215           }
10216
10217         /* Otherwise, if we are doing less-than or greater-or-equal-than,
10218            op1 is a constant and the low word is zero, then we can just
10219            examine the high word.  */
10220
10221         if (GET_CODE (hi[1]) == CONST_INT && lo[1] == const0_rtx)
10222           switch (code)
10223             {
10224             case LT: case LTU: case GE: case GEU:
10225               ix86_compare_op0 = hi[0];
10226               ix86_compare_op1 = hi[1];
10227               ix86_expand_branch (code, label);
10228               return;
10229             default:
10230               break;
10231             }
10232
10233         /* Otherwise, we need two or three jumps.  */
10234
10235         label2 = gen_label_rtx ();
10236
10237         code1 = code;
10238         code2 = swap_condition (code);
10239         code3 = unsigned_condition (code);
10240
10241         switch (code)
10242           {
10243           case LT: case GT: case LTU: case GTU:
10244             break;
10245
10246           case LE:   code1 = LT;  code2 = GT;  break;
10247           case GE:   code1 = GT;  code2 = LT;  break;
10248           case LEU:  code1 = LTU; code2 = GTU; break;
10249           case GEU:  code1 = GTU; code2 = LTU; break;
10250
10251           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
10252           case NE:   code2 = UNKNOWN; break;
10253
10254           default:
10255             gcc_unreachable ();
10256           }
10257
10258         /*
10259          * a < b =>
10260          *    if (hi(a) < hi(b)) goto true;
10261          *    if (hi(a) > hi(b)) goto false;
10262          *    if (lo(a) < lo(b)) goto true;
10263          *  false:
10264          */
10265
10266         ix86_compare_op0 = hi[0];
10267         ix86_compare_op1 = hi[1];
10268
10269         if (code1 != UNKNOWN)
10270           ix86_expand_branch (code1, label);
10271         if (code2 != UNKNOWN)
10272           ix86_expand_branch (code2, label2);
10273
10274         ix86_compare_op0 = lo[0];
10275         ix86_compare_op1 = lo[1];
10276         ix86_expand_branch (code3, label);
10277
10278         if (code2 != UNKNOWN)
10279           emit_label (label2);
10280         return;
10281       }
10282
10283     default:
10284       gcc_unreachable ();
10285     }
10286 }
10287
10288 /* Split branch based on floating point condition.  */
10289 void
10290 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
10291                       rtx target1, rtx target2, rtx tmp, rtx pushed)
10292 {
10293   rtx second, bypass;
10294   rtx label = NULL_RTX;
10295   rtx condition;
10296   int bypass_probability = -1, second_probability = -1, probability = -1;
10297   rtx i;
10298
10299   if (target2 != pc_rtx)
10300     {
10301       rtx tmp = target2;
10302       code = reverse_condition_maybe_unordered (code);
10303       target2 = target1;
10304       target1 = tmp;
10305     }
10306
10307   condition = ix86_expand_fp_compare (code, op1, op2,
10308                                       tmp, &second, &bypass);
10309
10310   /* Remove pushed operand from stack.  */
10311   if (pushed)
10312     ix86_free_from_memory (GET_MODE (pushed));
10313
10314   if (split_branch_probability >= 0)
10315     {
10316       /* Distribute the probabilities across the jumps.
10317          Assume the BYPASS and SECOND to be always test
10318          for UNORDERED.  */
10319       probability = split_branch_probability;
10320
10321       /* Value of 1 is low enough to make no need for probability
10322          to be updated.  Later we may run some experiments and see
10323          if unordered values are more frequent in practice.  */
10324       if (bypass)
10325         bypass_probability = 1;
10326       if (second)
10327         second_probability = 1;
10328     }
10329   if (bypass != NULL_RTX)
10330     {
10331       label = gen_label_rtx ();
10332       i = emit_jump_insn (gen_rtx_SET
10333                           (VOIDmode, pc_rtx,
10334                            gen_rtx_IF_THEN_ELSE (VOIDmode,
10335                                                  bypass,
10336                                                  gen_rtx_LABEL_REF (VOIDmode,
10337                                                                     label),
10338                                                  pc_rtx)));
10339       if (bypass_probability >= 0)
10340         REG_NOTES (i)
10341           = gen_rtx_EXPR_LIST (REG_BR_PROB,
10342                                GEN_INT (bypass_probability),
10343                                REG_NOTES (i));
10344     }
10345   i = emit_jump_insn (gen_rtx_SET
10346                       (VOIDmode, pc_rtx,
10347                        gen_rtx_IF_THEN_ELSE (VOIDmode,
10348                                              condition, target1, target2)));
10349   if (probability >= 0)
10350     REG_NOTES (i)
10351       = gen_rtx_EXPR_LIST (REG_BR_PROB,
10352                            GEN_INT (probability),
10353                            REG_NOTES (i));
10354   if (second != NULL_RTX)
10355     {
10356       i = emit_jump_insn (gen_rtx_SET
10357                           (VOIDmode, pc_rtx,
10358                            gen_rtx_IF_THEN_ELSE (VOIDmode, second, target1,
10359                                                  target2)));
10360       if (second_probability >= 0)
10361         REG_NOTES (i)
10362           = gen_rtx_EXPR_LIST (REG_BR_PROB,
10363                                GEN_INT (second_probability),
10364                                REG_NOTES (i));
10365     }
10366   if (label != NULL_RTX)
10367     emit_label (label);
10368 }
10369
10370 int
10371 ix86_expand_setcc (enum rtx_code code, rtx dest)
10372 {
10373   rtx ret, tmp, tmpreg, equiv;
10374   rtx second_test, bypass_test;
10375
10376   if (GET_MODE (ix86_compare_op0) == (TARGET_64BIT ? TImode : DImode))
10377     return 0; /* FAIL */
10378
10379   gcc_assert (GET_MODE (dest) == QImode);
10380
10381   ret = ix86_expand_compare (code, &second_test, &bypass_test);
10382   PUT_MODE (ret, QImode);
10383
10384   tmp = dest;
10385   tmpreg = dest;
10386
10387   emit_insn (gen_rtx_SET (VOIDmode, tmp, ret));
10388   if (bypass_test || second_test)
10389     {
10390       rtx test = second_test;
10391       int bypass = 0;
10392       rtx tmp2 = gen_reg_rtx (QImode);
10393       if (bypass_test)
10394         {
10395           gcc_assert (!second_test);
10396           test = bypass_test;
10397           bypass = 1;
10398           PUT_CODE (test, reverse_condition_maybe_unordered (GET_CODE (test)));
10399         }
10400       PUT_MODE (test, QImode);
10401       emit_insn (gen_rtx_SET (VOIDmode, tmp2, test));
10402
10403       if (bypass)
10404         emit_insn (gen_andqi3 (tmp, tmpreg, tmp2));
10405       else
10406         emit_insn (gen_iorqi3 (tmp, tmpreg, tmp2));
10407     }
10408
10409   /* Attach a REG_EQUAL note describing the comparison result.  */
10410   if (ix86_compare_op0 && ix86_compare_op1)
10411     {
10412       equiv = simplify_gen_relational (code, QImode,
10413                                        GET_MODE (ix86_compare_op0),
10414                                        ix86_compare_op0, ix86_compare_op1);
10415       set_unique_reg_note (get_last_insn (), REG_EQUAL, equiv);
10416     }
10417
10418   return 1; /* DONE */
10419 }
10420
10421 /* Expand comparison setting or clearing carry flag.  Return true when
10422    successful and set pop for the operation.  */
10423 static bool
10424 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
10425 {
10426   enum machine_mode mode =
10427     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
10428
10429   /* Do not handle DImode compares that go trought special path.  Also we can't
10430      deal with FP compares yet.  This is possible to add.  */
10431   if (mode == (TARGET_64BIT ? TImode : DImode))
10432     return false;
10433   if (FLOAT_MODE_P (mode))
10434     {
10435       rtx second_test = NULL, bypass_test = NULL;
10436       rtx compare_op, compare_seq;
10437
10438       /* Shortcut:  following common codes never translate into carry flag compares.  */
10439       if (code == EQ || code == NE || code == UNEQ || code == LTGT
10440           || code == ORDERED || code == UNORDERED)
10441         return false;
10442
10443       /* These comparisons require zero flag; swap operands so they won't.  */
10444       if ((code == GT || code == UNLE || code == LE || code == UNGT)
10445           && !TARGET_IEEE_FP)
10446         {
10447           rtx tmp = op0;
10448           op0 = op1;
10449           op1 = tmp;
10450           code = swap_condition (code);
10451         }
10452
10453       /* Try to expand the comparison and verify that we end up with carry flag
10454          based comparison.  This is fails to be true only when we decide to expand
10455          comparison using arithmetic that is not too common scenario.  */
10456       start_sequence ();
10457       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
10458                                            &second_test, &bypass_test);
10459       compare_seq = get_insns ();
10460       end_sequence ();
10461
10462       if (second_test || bypass_test)
10463         return false;
10464       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
10465           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
10466         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
10467       else
10468         code = GET_CODE (compare_op);
10469       if (code != LTU && code != GEU)
10470         return false;
10471       emit_insn (compare_seq);
10472       *pop = compare_op;
10473       return true;
10474     }
10475   if (!INTEGRAL_MODE_P (mode))
10476     return false;
10477   switch (code)
10478     {
10479     case LTU:
10480     case GEU:
10481       break;
10482
10483     /* Convert a==0 into (unsigned)a<1.  */
10484     case EQ:
10485     case NE:
10486       if (op1 != const0_rtx)
10487         return false;
10488       op1 = const1_rtx;
10489       code = (code == EQ ? LTU : GEU);
10490       break;
10491
10492     /* Convert a>b into b<a or a>=b-1.  */
10493     case GTU:
10494     case LEU:
10495       if (GET_CODE (op1) == CONST_INT)
10496         {
10497           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
10498           /* Bail out on overflow.  We still can swap operands but that
10499              would force loading of the constant into register.  */
10500           if (op1 == const0_rtx
10501               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
10502             return false;
10503           code = (code == GTU ? GEU : LTU);
10504         }
10505       else
10506         {
10507           rtx tmp = op1;
10508           op1 = op0;
10509           op0 = tmp;
10510           code = (code == GTU ? LTU : GEU);
10511         }
10512       break;
10513
10514     /* Convert a>=0 into (unsigned)a<0x80000000.  */
10515     case LT:
10516     case GE:
10517       if (mode == DImode || op1 != const0_rtx)
10518         return false;
10519       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
10520       code = (code == LT ? GEU : LTU);
10521       break;
10522     case LE:
10523     case GT:
10524       if (mode == DImode || op1 != constm1_rtx)
10525         return false;
10526       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
10527       code = (code == LE ? GEU : LTU);
10528       break;
10529
10530     default:
10531       return false;
10532     }
10533   /* Swapping operands may cause constant to appear as first operand.  */
10534   if (!nonimmediate_operand (op0, VOIDmode))
10535     {
10536       if (no_new_pseudos)
10537         return false;
10538       op0 = force_reg (mode, op0);
10539     }
10540   ix86_compare_op0 = op0;
10541   ix86_compare_op1 = op1;
10542   *pop = ix86_expand_compare (code, NULL, NULL);
10543   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
10544   return true;
10545 }
10546
10547 int
10548 ix86_expand_int_movcc (rtx operands[])
10549 {
10550   enum rtx_code code = GET_CODE (operands[1]), compare_code;
10551   rtx compare_seq, compare_op;
10552   rtx second_test, bypass_test;
10553   enum machine_mode mode = GET_MODE (operands[0]);
10554   bool sign_bit_compare_p = false;;
10555
10556   start_sequence ();
10557   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
10558   compare_seq = get_insns ();
10559   end_sequence ();
10560
10561   compare_code = GET_CODE (compare_op);
10562
10563   if ((ix86_compare_op1 == const0_rtx && (code == GE || code == LT))
10564       || (ix86_compare_op1 == constm1_rtx && (code == GT || code == LE)))
10565     sign_bit_compare_p = true;
10566
10567   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
10568      HImode insns, we'd be swallowed in word prefix ops.  */
10569
10570   if ((mode != HImode || TARGET_FAST_PREFIX)
10571       && (mode != (TARGET_64BIT ? TImode : DImode))
10572       && GET_CODE (operands[2]) == CONST_INT
10573       && GET_CODE (operands[3]) == CONST_INT)
10574     {
10575       rtx out = operands[0];
10576       HOST_WIDE_INT ct = INTVAL (operands[2]);
10577       HOST_WIDE_INT cf = INTVAL (operands[3]);
10578       HOST_WIDE_INT diff;
10579
10580       diff = ct - cf;
10581       /*  Sign bit compares are better done using shifts than we do by using
10582           sbb.  */
10583       if (sign_bit_compare_p
10584           || ix86_expand_carry_flag_compare (code, ix86_compare_op0,
10585                                              ix86_compare_op1, &compare_op))
10586         {
10587           /* Detect overlap between destination and compare sources.  */
10588           rtx tmp = out;
10589
10590           if (!sign_bit_compare_p)
10591             {
10592               bool fpcmp = false;
10593
10594               compare_code = GET_CODE (compare_op);
10595
10596               if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
10597                   || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
10598                 {
10599                   fpcmp = true;
10600                   compare_code = ix86_fp_compare_code_to_integer (compare_code);
10601                 }
10602
10603               /* To simplify rest of code, restrict to the GEU case.  */
10604               if (compare_code == LTU)
10605                 {
10606                   HOST_WIDE_INT tmp = ct;
10607                   ct = cf;
10608                   cf = tmp;
10609                   compare_code = reverse_condition (compare_code);
10610                   code = reverse_condition (code);
10611                 }
10612               else
10613                 {
10614                   if (fpcmp)
10615                     PUT_CODE (compare_op,
10616                               reverse_condition_maybe_unordered
10617                                 (GET_CODE (compare_op)));
10618                   else
10619                     PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
10620                 }
10621               diff = ct - cf;
10622
10623               if (reg_overlap_mentioned_p (out, ix86_compare_op0)
10624                   || reg_overlap_mentioned_p (out, ix86_compare_op1))
10625                 tmp = gen_reg_rtx (mode);
10626
10627               if (mode == DImode)
10628                 emit_insn (gen_x86_movdicc_0_m1_rex64 (tmp, compare_op));
10629               else
10630                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp), compare_op));
10631             }
10632           else
10633             {
10634               if (code == GT || code == GE)
10635                 code = reverse_condition (code);
10636               else
10637                 {
10638                   HOST_WIDE_INT tmp = ct;
10639                   ct = cf;
10640                   cf = tmp;
10641                   diff = ct - cf;
10642                 }
10643               tmp = emit_store_flag (tmp, code, ix86_compare_op0,
10644                                      ix86_compare_op1, VOIDmode, 0, -1);
10645             }
10646
10647           if (diff == 1)
10648             {
10649               /*
10650                * cmpl op0,op1
10651                * sbbl dest,dest
10652                * [addl dest, ct]
10653                *
10654                * Size 5 - 8.
10655                */
10656               if (ct)
10657                 tmp = expand_simple_binop (mode, PLUS,
10658                                            tmp, GEN_INT (ct),
10659                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
10660             }
10661           else if (cf == -1)
10662             {
10663               /*
10664                * cmpl op0,op1
10665                * sbbl dest,dest
10666                * orl $ct, dest
10667                *
10668                * Size 8.
10669                */
10670               tmp = expand_simple_binop (mode, IOR,
10671                                          tmp, GEN_INT (ct),
10672                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
10673             }
10674           else if (diff == -1 && ct)
10675             {
10676               /*
10677                * cmpl op0,op1
10678                * sbbl dest,dest
10679                * notl dest
10680                * [addl dest, cf]
10681                *
10682                * Size 8 - 11.
10683                */
10684               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
10685               if (cf)
10686                 tmp = expand_simple_binop (mode, PLUS,
10687                                            copy_rtx (tmp), GEN_INT (cf),
10688                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
10689             }
10690           else
10691             {
10692               /*
10693                * cmpl op0,op1
10694                * sbbl dest,dest
10695                * [notl dest]
10696                * andl cf - ct, dest
10697                * [addl dest, ct]
10698                *
10699                * Size 8 - 11.
10700                */
10701
10702               if (cf == 0)
10703                 {
10704                   cf = ct;
10705                   ct = 0;
10706                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
10707                 }
10708
10709               tmp = expand_simple_binop (mode, AND,
10710                                          copy_rtx (tmp),
10711                                          gen_int_mode (cf - ct, mode),
10712                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
10713               if (ct)
10714                 tmp = expand_simple_binop (mode, PLUS,
10715                                            copy_rtx (tmp), GEN_INT (ct),
10716                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
10717             }
10718
10719           if (!rtx_equal_p (tmp, out))
10720             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
10721
10722           return 1; /* DONE */
10723         }
10724
10725       if (diff < 0)
10726         {
10727           HOST_WIDE_INT tmp;
10728           tmp = ct, ct = cf, cf = tmp;
10729           diff = -diff;
10730           if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
10731             {
10732               /* We may be reversing unordered compare to normal compare, that
10733                  is not valid in general (we may convert non-trapping condition
10734                  to trapping one), however on i386 we currently emit all
10735                  comparisons unordered.  */
10736               compare_code = reverse_condition_maybe_unordered (compare_code);
10737               code = reverse_condition_maybe_unordered (code);
10738             }
10739           else
10740             {
10741               compare_code = reverse_condition (compare_code);
10742               code = reverse_condition (code);
10743             }
10744         }
10745
10746       compare_code = UNKNOWN;
10747       if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_INT
10748           && GET_CODE (ix86_compare_op1) == CONST_INT)
10749         {
10750           if (ix86_compare_op1 == const0_rtx
10751               && (code == LT || code == GE))
10752             compare_code = code;
10753           else if (ix86_compare_op1 == constm1_rtx)
10754             {
10755               if (code == LE)
10756                 compare_code = LT;
10757               else if (code == GT)
10758                 compare_code = GE;
10759             }
10760         }
10761
10762       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
10763       if (compare_code != UNKNOWN
10764           && GET_MODE (ix86_compare_op0) == GET_MODE (out)
10765           && (cf == -1 || ct == -1))
10766         {
10767           /* If lea code below could be used, only optimize
10768              if it results in a 2 insn sequence.  */
10769
10770           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
10771                  || diff == 3 || diff == 5 || diff == 9)
10772               || (compare_code == LT && ct == -1)
10773               || (compare_code == GE && cf == -1))
10774             {
10775               /*
10776                * notl op1       (if necessary)
10777                * sarl $31, op1
10778                * orl cf, op1
10779                */
10780               if (ct != -1)
10781                 {
10782                   cf = ct;
10783                   ct = -1;
10784                   code = reverse_condition (code);
10785                 }
10786
10787               out = emit_store_flag (out, code, ix86_compare_op0,
10788                                      ix86_compare_op1, VOIDmode, 0, -1);
10789
10790               out = expand_simple_binop (mode, IOR,
10791                                          out, GEN_INT (cf),
10792                                          out, 1, OPTAB_DIRECT);
10793               if (out != operands[0])
10794                 emit_move_insn (operands[0], out);
10795
10796               return 1; /* DONE */
10797             }
10798         }
10799
10800
10801       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
10802            || diff == 3 || diff == 5 || diff == 9)
10803           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
10804           && (mode != DImode
10805               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
10806         {
10807           /*
10808            * xorl dest,dest
10809            * cmpl op1,op2
10810            * setcc dest
10811            * lea cf(dest*(ct-cf)),dest
10812            *
10813            * Size 14.
10814            *
10815            * This also catches the degenerate setcc-only case.
10816            */
10817
10818           rtx tmp;
10819           int nops;
10820
10821           out = emit_store_flag (out, code, ix86_compare_op0,
10822                                  ix86_compare_op1, VOIDmode, 0, 1);
10823
10824           nops = 0;
10825           /* On x86_64 the lea instruction operates on Pmode, so we need
10826              to get arithmetics done in proper mode to match.  */
10827           if (diff == 1)
10828             tmp = copy_rtx (out);
10829           else
10830             {
10831               rtx out1;
10832               out1 = copy_rtx (out);
10833               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
10834               nops++;
10835               if (diff & 1)
10836                 {
10837                   tmp = gen_rtx_PLUS (mode, tmp, out1);
10838                   nops++;
10839                 }
10840             }
10841           if (cf != 0)
10842             {
10843               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
10844               nops++;
10845             }
10846           if (!rtx_equal_p (tmp, out))
10847             {
10848               if (nops == 1)
10849                 out = force_operand (tmp, copy_rtx (out));
10850               else
10851                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
10852             }
10853           if (!rtx_equal_p (out, operands[0]))
10854             emit_move_insn (operands[0], copy_rtx (out));
10855
10856           return 1; /* DONE */
10857         }
10858
10859       /*
10860        * General case:                  Jumpful:
10861        *   xorl dest,dest               cmpl op1, op2
10862        *   cmpl op1, op2                movl ct, dest
10863        *   setcc dest                   jcc 1f
10864        *   decl dest                    movl cf, dest
10865        *   andl (cf-ct),dest            1:
10866        *   addl ct,dest
10867        *
10868        * Size 20.                       Size 14.
10869        *
10870        * This is reasonably steep, but branch mispredict costs are
10871        * high on modern cpus, so consider failing only if optimizing
10872        * for space.
10873        */
10874
10875       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
10876           && BRANCH_COST >= 2)
10877         {
10878           if (cf == 0)
10879             {
10880               cf = ct;
10881               ct = 0;
10882               if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
10883                 /* We may be reversing unordered compare to normal compare,
10884                    that is not valid in general (we may convert non-trapping
10885                    condition to trapping one), however on i386 we currently
10886                    emit all comparisons unordered.  */
10887                 code = reverse_condition_maybe_unordered (code);
10888               else
10889                 {
10890                   code = reverse_condition (code);
10891                   if (compare_code != UNKNOWN)
10892                     compare_code = reverse_condition (compare_code);
10893                 }
10894             }
10895
10896           if (compare_code != UNKNOWN)
10897             {
10898               /* notl op1       (if needed)
10899                  sarl $31, op1
10900                  andl (cf-ct), op1
10901                  addl ct, op1
10902
10903                  For x < 0 (resp. x <= -1) there will be no notl,
10904                  so if possible swap the constants to get rid of the
10905                  complement.
10906                  True/false will be -1/0 while code below (store flag
10907                  followed by decrement) is 0/-1, so the constants need
10908                  to be exchanged once more.  */
10909
10910               if (compare_code == GE || !cf)
10911                 {
10912                   code = reverse_condition (code);
10913                   compare_code = LT;
10914                 }
10915               else
10916                 {
10917                   HOST_WIDE_INT tmp = cf;
10918                   cf = ct;
10919                   ct = tmp;
10920                 }
10921
10922               out = emit_store_flag (out, code, ix86_compare_op0,
10923                                      ix86_compare_op1, VOIDmode, 0, -1);
10924             }
10925           else
10926             {
10927               out = emit_store_flag (out, code, ix86_compare_op0,
10928                                      ix86_compare_op1, VOIDmode, 0, 1);
10929
10930               out = expand_simple_binop (mode, PLUS, copy_rtx (out), constm1_rtx,
10931                                          copy_rtx (out), 1, OPTAB_DIRECT);
10932             }
10933
10934           out = expand_simple_binop (mode, AND, copy_rtx (out),
10935                                      gen_int_mode (cf - ct, mode),
10936                                      copy_rtx (out), 1, OPTAB_DIRECT);
10937           if (ct)
10938             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
10939                                        copy_rtx (out), 1, OPTAB_DIRECT);
10940           if (!rtx_equal_p (out, operands[0]))
10941             emit_move_insn (operands[0], copy_rtx (out));
10942
10943           return 1; /* DONE */
10944         }
10945     }
10946
10947   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
10948     {
10949       /* Try a few things more with specific constants and a variable.  */
10950
10951       optab op;
10952       rtx var, orig_out, out, tmp;
10953
10954       if (BRANCH_COST <= 2)
10955         return 0; /* FAIL */
10956
10957       /* If one of the two operands is an interesting constant, load a
10958          constant with the above and mask it in with a logical operation.  */
10959
10960       if (GET_CODE (operands[2]) == CONST_INT)
10961         {
10962           var = operands[3];
10963           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
10964             operands[3] = constm1_rtx, op = and_optab;
10965           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
10966             operands[3] = const0_rtx, op = ior_optab;
10967           else
10968             return 0; /* FAIL */
10969         }
10970       else if (GET_CODE (operands[3]) == CONST_INT)
10971         {
10972           var = operands[2];
10973           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
10974             operands[2] = constm1_rtx, op = and_optab;
10975           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
10976             operands[2] = const0_rtx, op = ior_optab;
10977           else
10978             return 0; /* FAIL */
10979         }
10980       else
10981         return 0; /* FAIL */
10982
10983       orig_out = operands[0];
10984       tmp = gen_reg_rtx (mode);
10985       operands[0] = tmp;
10986
10987       /* Recurse to get the constant loaded.  */
10988       if (ix86_expand_int_movcc (operands) == 0)
10989         return 0; /* FAIL */
10990
10991       /* Mask in the interesting variable.  */
10992       out = expand_binop (mode, op, var, tmp, orig_out, 0,
10993                           OPTAB_WIDEN);
10994       if (!rtx_equal_p (out, orig_out))
10995         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
10996
10997       return 1; /* DONE */
10998     }
10999
11000   /*
11001    * For comparison with above,
11002    *
11003    * movl cf,dest
11004    * movl ct,tmp
11005    * cmpl op1,op2
11006    * cmovcc tmp,dest
11007    *
11008    * Size 15.
11009    */
11010
11011   if (! nonimmediate_operand (operands[2], mode))
11012     operands[2] = force_reg (mode, operands[2]);
11013   if (! nonimmediate_operand (operands[3], mode))
11014     operands[3] = force_reg (mode, operands[3]);
11015
11016   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
11017     {
11018       rtx tmp = gen_reg_rtx (mode);
11019       emit_move_insn (tmp, operands[3]);
11020       operands[3] = tmp;
11021     }
11022   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
11023     {
11024       rtx tmp = gen_reg_rtx (mode);
11025       emit_move_insn (tmp, operands[2]);
11026       operands[2] = tmp;
11027     }
11028
11029   if (! register_operand (operands[2], VOIDmode)
11030       && (mode == QImode
11031           || ! register_operand (operands[3], VOIDmode)))
11032     operands[2] = force_reg (mode, operands[2]);
11033
11034   if (mode == QImode
11035       && ! register_operand (operands[3], VOIDmode))
11036     operands[3] = force_reg (mode, operands[3]);
11037
11038   emit_insn (compare_seq);
11039   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11040                           gen_rtx_IF_THEN_ELSE (mode,
11041                                                 compare_op, operands[2],
11042                                                 operands[3])));
11043   if (bypass_test)
11044     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
11045                             gen_rtx_IF_THEN_ELSE (mode,
11046                                   bypass_test,
11047                                   copy_rtx (operands[3]),
11048                                   copy_rtx (operands[0]))));
11049   if (second_test)
11050     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
11051                             gen_rtx_IF_THEN_ELSE (mode,
11052                                   second_test,
11053                                   copy_rtx (operands[2]),
11054                                   copy_rtx (operands[0]))));
11055
11056   return 1; /* DONE */
11057 }
11058
11059 /* Swap, force into registers, or otherwise massage the two operands
11060    to an sse comparison with a mask result.  Thus we differ a bit from
11061    ix86_prepare_fp_compare_args which expects to produce a flags result.
11062
11063    The DEST operand exists to help determine whether to commute commutative
11064    operators.  The POP0/POP1 operands are updated in place.  The new
11065    comparison code is returned, or UNKNOWN if not implementable.  */
11066
11067 static enum rtx_code
11068 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
11069                                   rtx *pop0, rtx *pop1)
11070 {
11071   rtx tmp;
11072
11073   switch (code)
11074     {
11075     case LTGT:
11076     case UNEQ:
11077       /* We have no LTGT as an operator.  We could implement it with
11078          NE & ORDERED, but this requires an extra temporary.  It's
11079          not clear that it's worth it.  */
11080       return UNKNOWN;
11081
11082     case LT:
11083     case LE:
11084     case UNGT:
11085     case UNGE:
11086       /* These are supported directly.  */
11087       break;
11088
11089     case EQ:
11090     case NE:
11091     case UNORDERED:
11092     case ORDERED:
11093       /* For commutative operators, try to canonicalize the destination
11094          operand to be first in the comparison - this helps reload to
11095          avoid extra moves.  */
11096       if (!dest || !rtx_equal_p (dest, *pop1))
11097         break;
11098       /* FALLTHRU */
11099
11100     case GE:
11101     case GT:
11102     case UNLE:
11103     case UNLT:
11104       /* These are not supported directly.  Swap the comparison operands
11105          to transform into something that is supported.  */
11106       tmp = *pop0;
11107       *pop0 = *pop1;
11108       *pop1 = tmp;
11109       code = swap_condition (code);
11110       break;
11111
11112     default:
11113       gcc_unreachable ();
11114     }
11115
11116   return code;
11117 }
11118
11119 /* Detect conditional moves that exactly match min/max operational
11120    semantics.  Note that this is IEEE safe, as long as we don't
11121    interchange the operands.
11122
11123    Returns FALSE if this conditional move doesn't match a MIN/MAX,
11124    and TRUE if the operation is successful and instructions are emitted.  */
11125
11126 static bool
11127 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
11128                            rtx cmp_op1, rtx if_true, rtx if_false)
11129 {
11130   enum machine_mode mode;
11131   bool is_min;
11132   rtx tmp;
11133
11134   if (code == LT)
11135     ;
11136   else if (code == UNGE)
11137     {
11138       tmp = if_true;
11139       if_true = if_false;
11140       if_false = tmp;
11141     }
11142   else
11143     return false;
11144
11145   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
11146     is_min = true;
11147   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
11148     is_min = false;
11149   else
11150     return false;
11151
11152   mode = GET_MODE (dest);
11153
11154   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
11155      but MODE may be a vector mode and thus not appropriate.  */
11156   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
11157     {
11158       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
11159       rtvec v;
11160
11161       if_true = force_reg (mode, if_true);
11162       v = gen_rtvec (2, if_true, if_false);
11163       tmp = gen_rtx_UNSPEC (mode, v, u);
11164     }
11165   else
11166     {
11167       code = is_min ? SMIN : SMAX;
11168       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
11169     }
11170
11171   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
11172   return true;
11173 }
11174
11175 /* Expand an sse vector comparison.  Return the register with the result.  */
11176
11177 static rtx
11178 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
11179                      rtx op_true, rtx op_false)
11180 {
11181   enum machine_mode mode = GET_MODE (dest);
11182   rtx x;
11183
11184   cmp_op0 = force_reg (mode, cmp_op0);
11185   if (!nonimmediate_operand (cmp_op1, mode))
11186     cmp_op1 = force_reg (mode, cmp_op1);
11187
11188   if (optimize
11189       || reg_overlap_mentioned_p (dest, op_true)
11190       || reg_overlap_mentioned_p (dest, op_false))
11191     dest = gen_reg_rtx (mode);
11192
11193   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
11194   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11195
11196   return dest;
11197 }
11198
11199 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
11200    operations.  This is used for both scalar and vector conditional moves.  */
11201
11202 static void
11203 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
11204 {
11205   enum machine_mode mode = GET_MODE (dest);
11206   rtx t2, t3, x;
11207
11208   if (op_false == CONST0_RTX (mode))
11209     {
11210       op_true = force_reg (mode, op_true);
11211       x = gen_rtx_AND (mode, cmp, op_true);
11212       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11213     }
11214   else if (op_true == CONST0_RTX (mode))
11215     {
11216       op_false = force_reg (mode, op_false);
11217       x = gen_rtx_NOT (mode, cmp);
11218       x = gen_rtx_AND (mode, x, op_false);
11219       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11220     }
11221   else
11222     {
11223       op_true = force_reg (mode, op_true);
11224       op_false = force_reg (mode, op_false);
11225
11226       t2 = gen_reg_rtx (mode);
11227       if (optimize)
11228         t3 = gen_reg_rtx (mode);
11229       else
11230         t3 = dest;
11231
11232       x = gen_rtx_AND (mode, op_true, cmp);
11233       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
11234
11235       x = gen_rtx_NOT (mode, cmp);
11236       x = gen_rtx_AND (mode, x, op_false);
11237       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
11238
11239       x = gen_rtx_IOR (mode, t3, t2);
11240       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11241     }
11242 }
11243
11244 /* Expand a floating-point conditional move.  Return true if successful.  */
11245
11246 int
11247 ix86_expand_fp_movcc (rtx operands[])
11248 {
11249   enum machine_mode mode = GET_MODE (operands[0]);
11250   enum rtx_code code = GET_CODE (operands[1]);
11251   rtx tmp, compare_op, second_test, bypass_test;
11252
11253   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
11254     {
11255       enum machine_mode cmode;
11256
11257       /* Since we've no cmove for sse registers, don't force bad register
11258          allocation just to gain access to it.  Deny movcc when the
11259          comparison mode doesn't match the move mode.  */
11260       cmode = GET_MODE (ix86_compare_op0);
11261       if (cmode == VOIDmode)
11262         cmode = GET_MODE (ix86_compare_op1);
11263       if (cmode != mode)
11264         return 0;
11265
11266       code = ix86_prepare_sse_fp_compare_args (operands[0], code,
11267                                                &ix86_compare_op0,
11268                                                &ix86_compare_op1);
11269       if (code == UNKNOWN)
11270         return 0;
11271
11272       if (ix86_expand_sse_fp_minmax (operands[0], code, ix86_compare_op0,
11273                                      ix86_compare_op1, operands[2],
11274                                      operands[3]))
11275         return 1;
11276
11277       tmp = ix86_expand_sse_cmp (operands[0], code, ix86_compare_op0,
11278                                  ix86_compare_op1, operands[2], operands[3]);
11279       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
11280       return 1;
11281     }
11282
11283   /* The floating point conditional move instructions don't directly
11284      support conditions resulting from a signed integer comparison.  */
11285
11286   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
11287
11288   /* The floating point conditional move instructions don't directly
11289      support signed integer comparisons.  */
11290
11291   if (!fcmov_comparison_operator (compare_op, VOIDmode))
11292     {
11293       gcc_assert (!second_test && !bypass_test);
11294       tmp = gen_reg_rtx (QImode);
11295       ix86_expand_setcc (code, tmp);
11296       code = NE;
11297       ix86_compare_op0 = tmp;
11298       ix86_compare_op1 = const0_rtx;
11299       compare_op = ix86_expand_compare (code,  &second_test, &bypass_test);
11300     }
11301   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
11302     {
11303       tmp = gen_reg_rtx (mode);
11304       emit_move_insn (tmp, operands[3]);
11305       operands[3] = tmp;
11306     }
11307   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
11308     {
11309       tmp = gen_reg_rtx (mode);
11310       emit_move_insn (tmp, operands[2]);
11311       operands[2] = tmp;
11312     }
11313
11314   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11315                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
11316                                                 operands[2], operands[3])));
11317   if (bypass_test)
11318     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11319                             gen_rtx_IF_THEN_ELSE (mode, bypass_test,
11320                                                   operands[3], operands[0])));
11321   if (second_test)
11322     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11323                             gen_rtx_IF_THEN_ELSE (mode, second_test,
11324                                                   operands[2], operands[0])));
11325
11326   return 1;
11327 }
11328
11329 /* Expand a floating-point vector conditional move; a vcond operation
11330    rather than a movcc operation.  */
11331
11332 bool
11333 ix86_expand_fp_vcond (rtx operands[])
11334 {
11335   enum rtx_code code = GET_CODE (operands[3]);
11336   rtx cmp;
11337
11338   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
11339                                            &operands[4], &operands[5]);
11340   if (code == UNKNOWN)
11341     return false;
11342
11343   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
11344                                  operands[5], operands[1], operands[2]))
11345     return true;
11346
11347   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
11348                              operands[1], operands[2]);
11349   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
11350   return true;
11351 }
11352
11353 /* Expand a signed integral vector conditional move.  */
11354
11355 bool
11356 ix86_expand_int_vcond (rtx operands[])
11357 {
11358   enum machine_mode mode = GET_MODE (operands[0]);
11359   enum rtx_code code = GET_CODE (operands[3]);
11360   bool negate = false;
11361   rtx x, cop0, cop1;
11362
11363   cop0 = operands[4];
11364   cop1 = operands[5];
11365
11366   /* Canonicalize the comparison to EQ, GT, GTU.  */
11367   switch (code)
11368     {
11369     case EQ:
11370     case GT:
11371     case GTU:
11372       break;
11373
11374     case NE:
11375     case LE:
11376     case LEU:
11377       code = reverse_condition (code);
11378       negate = true;
11379       break;
11380
11381     case GE:
11382     case GEU:
11383       code = reverse_condition (code);
11384       negate = true;
11385       /* FALLTHRU */
11386
11387     case LT:
11388     case LTU:
11389       code = swap_condition (code);
11390       x = cop0, cop0 = cop1, cop1 = x;
11391       break;
11392
11393     default:
11394       gcc_unreachable ();
11395     }
11396
11397   /* Unsigned parallel compare is not supported by the hardware.  Play some
11398      tricks to turn this into a signed comparison against 0.  */
11399   if (code == GTU)
11400     {
11401       switch (mode)
11402         {
11403         case V4SImode:
11404           {
11405             rtx t1, t2, mask;
11406
11407             /* Perform a parallel modulo subtraction.  */
11408             t1 = gen_reg_rtx (mode);
11409             emit_insn (gen_subv4si3 (t1, cop0, cop1));
11410
11411             /* Extract the original sign bit of op0.  */
11412             mask = GEN_INT (-0x80000000);
11413             mask = gen_rtx_CONST_VECTOR (mode,
11414                         gen_rtvec (4, mask, mask, mask, mask));
11415             mask = force_reg (mode, mask);
11416             t2 = gen_reg_rtx (mode);
11417             emit_insn (gen_andv4si3 (t2, cop0, mask));
11418
11419             /* XOR it back into the result of the subtraction.  This results
11420                in the sign bit set iff we saw unsigned underflow.  */
11421             x = gen_reg_rtx (mode);
11422             emit_insn (gen_xorv4si3 (x, t1, t2));
11423
11424             code = GT;
11425           }
11426           break;
11427
11428         case V16QImode:
11429         case V8HImode:
11430           /* Perform a parallel unsigned saturating subtraction.  */
11431           x = gen_reg_rtx (mode);
11432           emit_insn (gen_rtx_SET (VOIDmode, x,
11433                                   gen_rtx_US_MINUS (mode, cop0, cop1)));
11434
11435           code = EQ;
11436           negate = !negate;
11437           break;
11438
11439         default:
11440           gcc_unreachable ();
11441         }
11442
11443       cop0 = x;
11444       cop1 = CONST0_RTX (mode);
11445     }
11446
11447   x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
11448                            operands[1+negate], operands[2-negate]);
11449
11450   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
11451                          operands[2-negate]);
11452   return true;
11453 }
11454
11455 /* Expand conditional increment or decrement using adb/sbb instructions.
11456    The default case using setcc followed by the conditional move can be
11457    done by generic code.  */
11458 int
11459 ix86_expand_int_addcc (rtx operands[])
11460 {
11461   enum rtx_code code = GET_CODE (operands[1]);
11462   rtx compare_op;
11463   rtx val = const0_rtx;
11464   bool fpcmp = false;
11465   enum machine_mode mode = GET_MODE (operands[0]);
11466
11467   if (operands[3] != const1_rtx
11468       && operands[3] != constm1_rtx)
11469     return 0;
11470   if (!ix86_expand_carry_flag_compare (code, ix86_compare_op0,
11471                                        ix86_compare_op1, &compare_op))
11472      return 0;
11473   code = GET_CODE (compare_op);
11474
11475   if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
11476       || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
11477     {
11478       fpcmp = true;
11479       code = ix86_fp_compare_code_to_integer (code);
11480     }
11481
11482   if (code != LTU)
11483     {
11484       val = constm1_rtx;
11485       if (fpcmp)
11486         PUT_CODE (compare_op,
11487                   reverse_condition_maybe_unordered
11488                     (GET_CODE (compare_op)));
11489       else
11490         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
11491     }
11492   PUT_MODE (compare_op, mode);
11493
11494   /* Construct either adc or sbb insn.  */
11495   if ((code == LTU) == (operands[3] == constm1_rtx))
11496     {
11497       switch (GET_MODE (operands[0]))
11498         {
11499           case QImode:
11500             emit_insn (gen_subqi3_carry (operands[0], operands[2], val, compare_op));
11501             break;
11502           case HImode:
11503             emit_insn (gen_subhi3_carry (operands[0], operands[2], val, compare_op));
11504             break;
11505           case SImode:
11506             emit_insn (gen_subsi3_carry (operands[0], operands[2], val, compare_op));
11507             break;
11508           case DImode:
11509             emit_insn (gen_subdi3_carry_rex64 (operands[0], operands[2], val, compare_op));
11510             break;
11511           default:
11512             gcc_unreachable ();
11513         }
11514     }
11515   else
11516     {
11517       switch (GET_MODE (operands[0]))
11518         {
11519           case QImode:
11520             emit_insn (gen_addqi3_carry (operands[0], operands[2], val, compare_op));
11521             break;
11522           case HImode:
11523             emit_insn (gen_addhi3_carry (operands[0], operands[2], val, compare_op));
11524             break;
11525           case SImode:
11526             emit_insn (gen_addsi3_carry (operands[0], operands[2], val, compare_op));
11527             break;
11528           case DImode:
11529             emit_insn (gen_adddi3_carry_rex64 (operands[0], operands[2], val, compare_op));
11530             break;
11531           default:
11532             gcc_unreachable ();
11533         }
11534     }
11535   return 1; /* DONE */
11536 }
11537
11538
11539 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
11540    works for floating pointer parameters and nonoffsetable memories.
11541    For pushes, it returns just stack offsets; the values will be saved
11542    in the right order.  Maximally three parts are generated.  */
11543
11544 static int
11545 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
11546 {
11547   int size;
11548
11549   if (!TARGET_64BIT)
11550     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
11551   else
11552     size = (GET_MODE_SIZE (mode) + 4) / 8;
11553
11554   gcc_assert (GET_CODE (operand) != REG || !MMX_REGNO_P (REGNO (operand)));
11555   gcc_assert (size >= 2 && size <= 3);
11556
11557   /* Optimize constant pool reference to immediates.  This is used by fp
11558      moves, that force all constants to memory to allow combining.  */
11559   if (GET_CODE (operand) == MEM && MEM_READONLY_P (operand))
11560     {
11561       rtx tmp = maybe_get_pool_constant (operand);
11562       if (tmp)
11563         operand = tmp;
11564     }
11565
11566   if (GET_CODE (operand) == MEM && !offsettable_memref_p (operand))
11567     {
11568       /* The only non-offsetable memories we handle are pushes.  */
11569       int ok = push_operand (operand, VOIDmode);
11570       
11571       gcc_assert (ok);
11572       
11573       operand = copy_rtx (operand);
11574       PUT_MODE (operand, Pmode);
11575       parts[0] = parts[1] = parts[2] = operand;
11576       return size;
11577     }
11578
11579   if (GET_CODE (operand) == CONST_VECTOR)
11580     {
11581       enum machine_mode imode = int_mode_for_mode (mode);
11582       /* Caution: if we looked through a constant pool memory above,
11583          the operand may actually have a different mode now.  That's
11584          ok, since we want to pun this all the way back to an integer.  */
11585       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
11586       gcc_assert (operand != NULL);
11587       mode = imode;
11588     }
11589
11590   if (!TARGET_64BIT)
11591     {
11592       if (mode == DImode)
11593         split_di (&operand, 1, &parts[0], &parts[1]);
11594       else
11595         {
11596           if (REG_P (operand))
11597             {
11598               gcc_assert (reload_completed);
11599               parts[0] = gen_rtx_REG (SImode, REGNO (operand) + 0);
11600               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
11601               if (size == 3)
11602                 parts[2] = gen_rtx_REG (SImode, REGNO (operand) + 2);
11603             }
11604           else if (offsettable_memref_p (operand))
11605             {
11606               operand = adjust_address (operand, SImode, 0);
11607               parts[0] = operand;
11608               parts[1] = adjust_address (operand, SImode, 4);
11609               if (size == 3)
11610                 parts[2] = adjust_address (operand, SImode, 8);
11611             }
11612           else if (GET_CODE (operand) == CONST_DOUBLE)
11613             {
11614               REAL_VALUE_TYPE r;
11615               long l[4];
11616
11617               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
11618               switch (mode)
11619                 {
11620                 case XFmode:
11621                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
11622                   parts[2] = gen_int_mode (l[2], SImode);
11623                   break;
11624                 case DFmode:
11625                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
11626                   break;
11627                 default:
11628                   gcc_unreachable ();
11629                 }
11630               parts[1] = gen_int_mode (l[1], SImode);
11631               parts[0] = gen_int_mode (l[0], SImode);
11632             }
11633           else
11634             gcc_unreachable ();
11635         }
11636     }
11637   else
11638     {
11639       if (mode == TImode)
11640         split_ti (&operand, 1, &parts[0], &parts[1]);
11641       if (mode == XFmode || mode == TFmode)
11642         {
11643           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
11644           if (REG_P (operand))
11645             {
11646               gcc_assert (reload_completed);
11647               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
11648               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
11649             }
11650           else if (offsettable_memref_p (operand))
11651             {
11652               operand = adjust_address (operand, DImode, 0);
11653               parts[0] = operand;
11654               parts[1] = adjust_address (operand, upper_mode, 8);
11655             }
11656           else if (GET_CODE (operand) == CONST_DOUBLE)
11657             {
11658               REAL_VALUE_TYPE r;
11659               long l[4];
11660
11661               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
11662               real_to_target (l, &r, mode);
11663
11664               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
11665               if (HOST_BITS_PER_WIDE_INT >= 64)
11666                 parts[0]
11667                   = gen_int_mode
11668                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
11669                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
11670                        DImode);
11671               else
11672                 parts[0] = immed_double_const (l[0], l[1], DImode);
11673
11674               if (upper_mode == SImode)
11675                 parts[1] = gen_int_mode (l[2], SImode);
11676               else if (HOST_BITS_PER_WIDE_INT >= 64)
11677                 parts[1]
11678                   = gen_int_mode
11679                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
11680                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
11681                        DImode);
11682               else
11683                 parts[1] = immed_double_const (l[2], l[3], DImode);
11684             }
11685           else
11686             gcc_unreachable ();
11687         }
11688     }
11689
11690   return size;
11691 }
11692
11693 /* Emit insns to perform a move or push of DI, DF, and XF values.
11694    Return false when normal moves are needed; true when all required
11695    insns have been emitted.  Operands 2-4 contain the input values
11696    int the correct order; operands 5-7 contain the output values.  */
11697
11698 void
11699 ix86_split_long_move (rtx operands[])
11700 {
11701   rtx part[2][3];
11702   int nparts;
11703   int push = 0;
11704   int collisions = 0;
11705   enum machine_mode mode = GET_MODE (operands[0]);
11706
11707   /* The DFmode expanders may ask us to move double.
11708      For 64bit target this is single move.  By hiding the fact
11709      here we simplify i386.md splitters.  */
11710   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
11711     {
11712       /* Optimize constant pool reference to immediates.  This is used by
11713          fp moves, that force all constants to memory to allow combining.  */
11714
11715       if (GET_CODE (operands[1]) == MEM
11716           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
11717           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
11718         operands[1] = get_pool_constant (XEXP (operands[1], 0));
11719       if (push_operand (operands[0], VOIDmode))
11720         {
11721           operands[0] = copy_rtx (operands[0]);
11722           PUT_MODE (operands[0], Pmode);
11723         }
11724       else
11725         operands[0] = gen_lowpart (DImode, operands[0]);
11726       operands[1] = gen_lowpart (DImode, operands[1]);
11727       emit_move_insn (operands[0], operands[1]);
11728       return;
11729     }
11730
11731   /* The only non-offsettable memory we handle is push.  */
11732   if (push_operand (operands[0], VOIDmode))
11733     push = 1;
11734   else
11735     gcc_assert (GET_CODE (operands[0]) != MEM
11736                 || offsettable_memref_p (operands[0]));
11737
11738   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
11739   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
11740
11741   /* When emitting push, take care for source operands on the stack.  */
11742   if (push && GET_CODE (operands[1]) == MEM
11743       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
11744     {
11745       if (nparts == 3)
11746         part[1][1] = change_address (part[1][1], GET_MODE (part[1][1]),
11747                                      XEXP (part[1][2], 0));
11748       part[1][0] = change_address (part[1][0], GET_MODE (part[1][0]),
11749                                    XEXP (part[1][1], 0));
11750     }
11751
11752   /* We need to do copy in the right order in case an address register
11753      of the source overlaps the destination.  */
11754   if (REG_P (part[0][0]) && GET_CODE (part[1][0]) == MEM)
11755     {
11756       if (reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0)))
11757         collisions++;
11758       if (reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
11759         collisions++;
11760       if (nparts == 3
11761           && reg_overlap_mentioned_p (part[0][2], XEXP (part[1][0], 0)))
11762         collisions++;
11763
11764       /* Collision in the middle part can be handled by reordering.  */
11765       if (collisions == 1 && nparts == 3
11766           && reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
11767         {
11768           rtx tmp;
11769           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
11770           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
11771         }
11772
11773       /* If there are more collisions, we can't handle it by reordering.
11774          Do an lea to the last part and use only one colliding move.  */
11775       else if (collisions > 1)
11776         {
11777           rtx base;
11778
11779           collisions = 1;
11780
11781           base = part[0][nparts - 1];
11782
11783           /* Handle the case when the last part isn't valid for lea.
11784              Happens in 64-bit mode storing the 12-byte XFmode.  */
11785           if (GET_MODE (base) != Pmode)
11786             base = gen_rtx_REG (Pmode, REGNO (base));
11787
11788           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
11789           part[1][0] = replace_equiv_address (part[1][0], base);
11790           part[1][1] = replace_equiv_address (part[1][1],
11791                                       plus_constant (base, UNITS_PER_WORD));
11792           if (nparts == 3)
11793             part[1][2] = replace_equiv_address (part[1][2],
11794                                       plus_constant (base, 8));
11795         }
11796     }
11797
11798   if (push)
11799     {
11800       if (!TARGET_64BIT)
11801         {
11802           if (nparts == 3)
11803             {
11804               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
11805                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, GEN_INT (-4)));
11806               emit_move_insn (part[0][2], part[1][2]);
11807             }
11808         }
11809       else
11810         {
11811           /* In 64bit mode we don't have 32bit push available.  In case this is
11812              register, it is OK - we will just use larger counterpart.  We also
11813              retype memory - these comes from attempt to avoid REX prefix on
11814              moving of second half of TFmode value.  */
11815           if (GET_MODE (part[1][1]) == SImode)
11816             {
11817               switch (GET_CODE (part[1][1]))
11818                 {
11819                 case MEM:
11820                   part[1][1] = adjust_address (part[1][1], DImode, 0);
11821                   break;
11822
11823                 case REG:
11824                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
11825                   break;
11826
11827                 default:
11828                   gcc_unreachable ();
11829                 }
11830               
11831               if (GET_MODE (part[1][0]) == SImode)
11832                 part[1][0] = part[1][1];
11833             }
11834         }
11835       emit_move_insn (part[0][1], part[1][1]);
11836       emit_move_insn (part[0][0], part[1][0]);
11837       return;
11838     }
11839
11840   /* Choose correct order to not overwrite the source before it is copied.  */
11841   if ((REG_P (part[0][0])
11842        && REG_P (part[1][1])
11843        && (REGNO (part[0][0]) == REGNO (part[1][1])
11844            || (nparts == 3
11845                && REGNO (part[0][0]) == REGNO (part[1][2]))))
11846       || (collisions > 0
11847           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
11848     {
11849       if (nparts == 3)
11850         {
11851           operands[2] = part[0][2];
11852           operands[3] = part[0][1];
11853           operands[4] = part[0][0];
11854           operands[5] = part[1][2];
11855           operands[6] = part[1][1];
11856           operands[7] = part[1][0];
11857         }
11858       else
11859         {
11860           operands[2] = part[0][1];
11861           operands[3] = part[0][0];
11862           operands[5] = part[1][1];
11863           operands[6] = part[1][0];
11864         }
11865     }
11866   else
11867     {
11868       if (nparts == 3)
11869         {
11870           operands[2] = part[0][0];
11871           operands[3] = part[0][1];
11872           operands[4] = part[0][2];
11873           operands[5] = part[1][0];
11874           operands[6] = part[1][1];
11875           operands[7] = part[1][2];
11876         }
11877       else
11878         {
11879           operands[2] = part[0][0];
11880           operands[3] = part[0][1];
11881           operands[5] = part[1][0];
11882           operands[6] = part[1][1];
11883         }
11884     }
11885
11886   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
11887   if (optimize_size)
11888     {
11889       if (GET_CODE (operands[5]) == CONST_INT
11890           && operands[5] != const0_rtx
11891           && REG_P (operands[2]))
11892         {
11893           if (GET_CODE (operands[6]) == CONST_INT
11894               && INTVAL (operands[6]) == INTVAL (operands[5]))
11895             operands[6] = operands[2];
11896
11897           if (nparts == 3
11898               && GET_CODE (operands[7]) == CONST_INT
11899               && INTVAL (operands[7]) == INTVAL (operands[5]))
11900             operands[7] = operands[2];
11901         }
11902
11903       if (nparts == 3
11904           && GET_CODE (operands[6]) == CONST_INT
11905           && operands[6] != const0_rtx
11906           && REG_P (operands[3])
11907           && GET_CODE (operands[7]) == CONST_INT
11908           && INTVAL (operands[7]) == INTVAL (operands[6]))
11909         operands[7] = operands[3];
11910     }
11911
11912   emit_move_insn (operands[2], operands[5]);
11913   emit_move_insn (operands[3], operands[6]);
11914   if (nparts == 3)
11915     emit_move_insn (operands[4], operands[7]);
11916
11917   return;
11918 }
11919
11920 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
11921    left shift by a constant, either using a single shift or
11922    a sequence of add instructions.  */
11923
11924 static void
11925 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
11926 {
11927   if (count == 1)
11928     {
11929       emit_insn ((mode == DImode
11930                   ? gen_addsi3
11931                   : gen_adddi3) (operand, operand, operand));
11932     }
11933   else if (!optimize_size
11934            && count * ix86_cost->add <= ix86_cost->shift_const)
11935     {
11936       int i;
11937       for (i=0; i<count; i++)
11938         {
11939           emit_insn ((mode == DImode
11940                       ? gen_addsi3
11941                       : gen_adddi3) (operand, operand, operand));
11942         }
11943     }
11944   else
11945     emit_insn ((mode == DImode
11946                 ? gen_ashlsi3
11947                 : gen_ashldi3) (operand, operand, GEN_INT (count)));
11948 }
11949
11950 void
11951 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
11952 {
11953   rtx low[2], high[2];
11954   int count;
11955   const int single_width = mode == DImode ? 32 : 64;
11956
11957   if (GET_CODE (operands[2]) == CONST_INT)
11958     {
11959       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
11960       count = INTVAL (operands[2]) & (single_width * 2 - 1);
11961
11962       if (count >= single_width)
11963         {
11964           emit_move_insn (high[0], low[1]);
11965           emit_move_insn (low[0], const0_rtx);
11966
11967           if (count > single_width)
11968             ix86_expand_ashl_const (high[0], count - single_width, mode);
11969         }
11970       else
11971         {
11972           if (!rtx_equal_p (operands[0], operands[1]))
11973             emit_move_insn (operands[0], operands[1]);
11974           emit_insn ((mode == DImode
11975                      ? gen_x86_shld_1
11976                      : gen_x86_64_shld) (high[0], low[0], GEN_INT (count)));
11977           ix86_expand_ashl_const (low[0], count, mode);
11978         }
11979       return;
11980     }
11981
11982   (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
11983
11984   if (operands[1] == const1_rtx)
11985     {
11986       /* Assuming we've chosen a QImode capable registers, then 1 << N
11987          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
11988       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
11989         {
11990           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
11991
11992           ix86_expand_clear (low[0]);
11993           ix86_expand_clear (high[0]);
11994           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (single_width)));
11995           
11996           d = gen_lowpart (QImode, low[0]);
11997           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
11998           s = gen_rtx_EQ (QImode, flags, const0_rtx);
11999           emit_insn (gen_rtx_SET (VOIDmode, d, s));
12000
12001           d = gen_lowpart (QImode, high[0]);
12002           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
12003           s = gen_rtx_NE (QImode, flags, const0_rtx);
12004           emit_insn (gen_rtx_SET (VOIDmode, d, s));
12005         }
12006
12007       /* Otherwise, we can get the same results by manually performing
12008          a bit extract operation on bit 5/6, and then performing the two
12009          shifts.  The two methods of getting 0/1 into low/high are exactly
12010          the same size.  Avoiding the shift in the bit extract case helps
12011          pentium4 a bit; no one else seems to care much either way.  */
12012       else
12013         {
12014           rtx x;
12015
12016           if (TARGET_PARTIAL_REG_STALL && !optimize_size)
12017             x = gen_rtx_ZERO_EXTEND (mode == DImode ? SImode : DImode, operands[2]);
12018           else
12019             x = gen_lowpart (mode == DImode ? SImode : DImode, operands[2]);
12020           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
12021
12022           emit_insn ((mode == DImode
12023                       ? gen_lshrsi3
12024                       : gen_lshrdi3) (high[0], high[0], GEN_INT (mode == DImode ? 5 : 6)));
12025           emit_insn ((mode == DImode
12026                       ? gen_andsi3
12027                       : gen_anddi3) (high[0], high[0], GEN_INT (1)));
12028           emit_move_insn (low[0], high[0]);
12029           emit_insn ((mode == DImode
12030                       ? gen_xorsi3
12031                       : gen_xordi3) (low[0], low[0], GEN_INT (1)));
12032         }
12033
12034       emit_insn ((mode == DImode
12035                     ? gen_ashlsi3
12036                     : gen_ashldi3) (low[0], low[0], operands[2]));
12037       emit_insn ((mode == DImode
12038                     ? gen_ashlsi3
12039                     : gen_ashldi3) (high[0], high[0], operands[2]));
12040       return;
12041     }
12042
12043   if (operands[1] == constm1_rtx)
12044     {
12045       /* For -1 << N, we can avoid the shld instruction, because we
12046          know that we're shifting 0...31/63 ones into a -1.  */
12047       emit_move_insn (low[0], constm1_rtx);
12048       if (optimize_size)
12049         emit_move_insn (high[0], low[0]);
12050       else
12051         emit_move_insn (high[0], constm1_rtx);
12052     }
12053   else
12054     {
12055       if (!rtx_equal_p (operands[0], operands[1]))
12056         emit_move_insn (operands[0], operands[1]);
12057
12058       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12059       emit_insn ((mode == DImode
12060                   ? gen_x86_shld_1
12061                   : gen_x86_64_shld) (high[0], low[0], operands[2]));
12062     }
12063
12064   emit_insn ((mode == DImode ? gen_ashlsi3 : gen_ashldi3) (low[0], low[0], operands[2]));
12065
12066   if (TARGET_CMOVE && scratch)
12067     {
12068       ix86_expand_clear (scratch);
12069       emit_insn ((mode == DImode
12070                   ? gen_x86_shift_adj_1
12071                   : gen_x86_64_shift_adj) (high[0], low[0], operands[2], scratch));
12072     }
12073   else
12074     emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
12075 }
12076
12077 void
12078 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
12079 {
12080   rtx low[2], high[2];
12081   int count;
12082   const int single_width = mode == DImode ? 32 : 64;
12083
12084   if (GET_CODE (operands[2]) == CONST_INT)
12085     {
12086       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
12087       count = INTVAL (operands[2]) & (single_width * 2 - 1);
12088
12089       if (count == single_width * 2 - 1)
12090         {
12091           emit_move_insn (high[0], high[1]);
12092           emit_insn ((mode == DImode
12093                       ? gen_ashrsi3
12094                       : gen_ashrdi3) (high[0], high[0],
12095                                       GEN_INT (single_width - 1)));
12096           emit_move_insn (low[0], high[0]);
12097
12098         }
12099       else if (count >= single_width)
12100         {
12101           emit_move_insn (low[0], high[1]);
12102           emit_move_insn (high[0], low[0]);
12103           emit_insn ((mode == DImode
12104                       ? gen_ashrsi3
12105                       : gen_ashrdi3) (high[0], high[0],
12106                                       GEN_INT (single_width - 1)));
12107           if (count > single_width)
12108             emit_insn ((mode == DImode
12109                         ? gen_ashrsi3
12110                         : gen_ashrdi3) (low[0], low[0],
12111                                         GEN_INT (count - single_width)));
12112         }
12113       else
12114         {
12115           if (!rtx_equal_p (operands[0], operands[1]))
12116             emit_move_insn (operands[0], operands[1]);
12117           emit_insn ((mode == DImode
12118                       ? gen_x86_shrd_1
12119                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
12120           emit_insn ((mode == DImode
12121                       ? gen_ashrsi3
12122                       : gen_ashrdi3) (high[0], high[0], GEN_INT (count)));
12123         }
12124     }
12125   else
12126     {
12127       if (!rtx_equal_p (operands[0], operands[1]))
12128         emit_move_insn (operands[0], operands[1]);
12129
12130       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12131
12132       emit_insn ((mode == DImode
12133                   ? gen_x86_shrd_1
12134                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
12135       emit_insn ((mode == DImode
12136                   ? gen_ashrsi3
12137                   : gen_ashrdi3)  (high[0], high[0], operands[2]));
12138
12139       if (TARGET_CMOVE && scratch)
12140         {
12141           emit_move_insn (scratch, high[0]);
12142           emit_insn ((mode == DImode
12143                       ? gen_ashrsi3
12144                       : gen_ashrdi3) (scratch, scratch,
12145                                       GEN_INT (single_width - 1)));
12146           emit_insn ((mode == DImode
12147                       ? gen_x86_shift_adj_1
12148                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
12149                                          scratch));
12150         }
12151       else
12152         emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
12153     }
12154 }
12155
12156 void
12157 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
12158 {
12159   rtx low[2], high[2];
12160   int count;
12161   const int single_width = mode == DImode ? 32 : 64;
12162
12163   if (GET_CODE (operands[2]) == CONST_INT)
12164     {
12165       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
12166       count = INTVAL (operands[2]) & (single_width * 2 - 1);
12167
12168       if (count >= single_width)
12169         {
12170           emit_move_insn (low[0], high[1]);
12171           ix86_expand_clear (high[0]);
12172
12173           if (count > single_width)
12174             emit_insn ((mode == DImode
12175                         ? gen_lshrsi3
12176                         : gen_lshrdi3) (low[0], low[0],
12177                                         GEN_INT (count - single_width)));
12178         }
12179       else
12180         {
12181           if (!rtx_equal_p (operands[0], operands[1]))
12182             emit_move_insn (operands[0], operands[1]);
12183           emit_insn ((mode == DImode
12184                       ? gen_x86_shrd_1
12185                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
12186           emit_insn ((mode == DImode
12187                       ? gen_lshrsi3
12188                       : gen_lshrdi3) (high[0], high[0], GEN_INT (count)));
12189         }
12190     }
12191   else
12192     {
12193       if (!rtx_equal_p (operands[0], operands[1]))
12194         emit_move_insn (operands[0], operands[1]);
12195
12196       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12197
12198       emit_insn ((mode == DImode
12199                   ? gen_x86_shrd_1
12200                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
12201       emit_insn ((mode == DImode
12202                   ? gen_lshrsi3
12203                   : gen_lshrdi3) (high[0], high[0], operands[2]));
12204
12205       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
12206       if (TARGET_CMOVE && scratch)
12207         {
12208           ix86_expand_clear (scratch);
12209           emit_insn ((mode == DImode
12210                       ? gen_x86_shift_adj_1
12211                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
12212                                                scratch));
12213         }
12214       else
12215         emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
12216     }
12217 }
12218
12219 /* Helper function for the string operations below.  Dest VARIABLE whether
12220    it is aligned to VALUE bytes.  If true, jump to the label.  */
12221 static rtx
12222 ix86_expand_aligntest (rtx variable, int value)
12223 {
12224   rtx label = gen_label_rtx ();
12225   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
12226   if (GET_MODE (variable) == DImode)
12227     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
12228   else
12229     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
12230   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
12231                            1, label);
12232   return label;
12233 }
12234
12235 /* Adjust COUNTER by the VALUE.  */
12236 static void
12237 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
12238 {
12239   if (GET_MODE (countreg) == DImode)
12240     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
12241   else
12242     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
12243 }
12244
12245 /* Zero extend possibly SImode EXP to Pmode register.  */
12246 rtx
12247 ix86_zero_extend_to_Pmode (rtx exp)
12248 {
12249   rtx r;
12250   if (GET_MODE (exp) == VOIDmode)
12251     return force_reg (Pmode, exp);
12252   if (GET_MODE (exp) == Pmode)
12253     return copy_to_mode_reg (Pmode, exp);
12254   r = gen_reg_rtx (Pmode);
12255   emit_insn (gen_zero_extendsidi2 (r, exp));
12256   return r;
12257 }
12258
12259 /* Expand string move (memcpy) operation.  Use i386 string operations when
12260    profitable.  expand_clrmem contains similar code.  */
12261 int
12262 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp)
12263 {
12264   rtx srcreg, destreg, countreg, srcexp, destexp;
12265   enum machine_mode counter_mode;
12266   HOST_WIDE_INT align = 0;
12267   unsigned HOST_WIDE_INT count = 0;
12268
12269   if (GET_CODE (align_exp) == CONST_INT)
12270     align = INTVAL (align_exp);
12271
12272   /* Can't use any of this if the user has appropriated esi or edi.  */
12273   if (global_regs[4] || global_regs[5])
12274     return 0;
12275
12276   /* This simple hack avoids all inlining code and simplifies code below.  */
12277   if (!TARGET_ALIGN_STRINGOPS)
12278     align = 64;
12279
12280   if (GET_CODE (count_exp) == CONST_INT)
12281     {
12282       count = INTVAL (count_exp);
12283       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
12284         return 0;
12285     }
12286
12287   /* Figure out proper mode for counter.  For 32bits it is always SImode,
12288      for 64bits use SImode when possible, otherwise DImode.
12289      Set count to number of bytes copied when known at compile time.  */
12290   if (!TARGET_64BIT
12291       || GET_MODE (count_exp) == SImode
12292       || x86_64_zext_immediate_operand (count_exp, VOIDmode))
12293     counter_mode = SImode;
12294   else
12295     counter_mode = DImode;
12296
12297   gcc_assert (counter_mode == SImode || counter_mode == DImode);
12298
12299   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
12300   if (destreg != XEXP (dst, 0))
12301     dst = replace_equiv_address_nv (dst, destreg);
12302   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
12303   if (srcreg != XEXP (src, 0))
12304     src = replace_equiv_address_nv (src, srcreg);
12305
12306   /* When optimizing for size emit simple rep ; movsb instruction for
12307      counts not divisible by 4, except when (movsl;)*(movsw;)?(movsb;)?
12308      sequence is shorter than mov{b,l} $count, %{ecx,cl}; rep; movsb.
12309      Sice of (movsl;)*(movsw;)?(movsb;)? sequence is
12310      count / 4 + (count & 3), the other sequence is either 4 or 7 bytes,
12311      but we don't know whether upper 24 (resp. 56) bits of %ecx will be
12312      known to be zero or not.  The rep; movsb sequence causes higher
12313      register pressure though, so take that into account.  */
12314
12315   if ((!optimize || optimize_size)
12316       && (count == 0
12317           || ((count & 0x03)
12318               && (!optimize_size
12319                   || count > 5 * 4
12320                   || (count & 3) + count / 4 > 6))))
12321     {
12322       emit_insn (gen_cld ());
12323       countreg = ix86_zero_extend_to_Pmode (count_exp);
12324       destexp = gen_rtx_PLUS (Pmode, destreg, countreg);
12325       srcexp = gen_rtx_PLUS (Pmode, srcreg, countreg);
12326       emit_insn (gen_rep_mov (destreg, dst, srcreg, src, countreg,
12327                               destexp, srcexp));
12328     }
12329
12330   /* For constant aligned (or small unaligned) copies use rep movsl
12331      followed by code copying the rest.  For PentiumPro ensure 8 byte
12332      alignment to allow rep movsl acceleration.  */
12333
12334   else if (count != 0
12335            && (align >= 8
12336                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
12337                || optimize_size || count < (unsigned int) 64))
12338     {
12339       unsigned HOST_WIDE_INT offset = 0;
12340       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
12341       rtx srcmem, dstmem;
12342
12343       emit_insn (gen_cld ());
12344       if (count & ~(size - 1))
12345         {
12346           if ((TARGET_SINGLE_STRINGOP || optimize_size) && count < 5 * 4)
12347             {
12348               enum machine_mode movs_mode = size == 4 ? SImode : DImode;
12349
12350               while (offset < (count & ~(size - 1)))
12351                 {
12352                   srcmem = adjust_automodify_address_nv (src, movs_mode,
12353                                                          srcreg, offset);
12354                   dstmem = adjust_automodify_address_nv (dst, movs_mode,
12355                                                          destreg, offset);
12356                   emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12357                   offset += size;
12358                 }
12359             }
12360           else
12361             {
12362               countreg = GEN_INT ((count >> (size == 4 ? 2 : 3))
12363                                   & (TARGET_64BIT ? -1 : 0x3fffffff));
12364               countreg = copy_to_mode_reg (counter_mode, countreg);
12365               countreg = ix86_zero_extend_to_Pmode (countreg);
12366
12367               destexp = gen_rtx_ASHIFT (Pmode, countreg,
12368                                         GEN_INT (size == 4 ? 2 : 3));
12369               srcexp = gen_rtx_PLUS (Pmode, destexp, srcreg);
12370               destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
12371
12372               emit_insn (gen_rep_mov (destreg, dst, srcreg, src,
12373                                       countreg, destexp, srcexp));
12374               offset = count & ~(size - 1);
12375             }
12376         }
12377       if (size == 8 && (count & 0x04))
12378         {
12379           srcmem = adjust_automodify_address_nv (src, SImode, srcreg,
12380                                                  offset);
12381           dstmem = adjust_automodify_address_nv (dst, SImode, destreg,
12382                                                  offset);
12383           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12384           offset += 4;
12385         }
12386       if (count & 0x02)
12387         {
12388           srcmem = adjust_automodify_address_nv (src, HImode, srcreg,
12389                                                  offset);
12390           dstmem = adjust_automodify_address_nv (dst, HImode, destreg,
12391                                                  offset);
12392           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12393           offset += 2;
12394         }
12395       if (count & 0x01)
12396         {
12397           srcmem = adjust_automodify_address_nv (src, QImode, srcreg,
12398                                                  offset);
12399           dstmem = adjust_automodify_address_nv (dst, QImode, destreg,
12400                                                  offset);
12401           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12402         }
12403     }
12404   /* The generic code based on the glibc implementation:
12405      - align destination to 4 bytes (8 byte alignment is used for PentiumPro
12406      allowing accelerated copying there)
12407      - copy the data using rep movsl
12408      - copy the rest.  */
12409   else
12410     {
12411       rtx countreg2;
12412       rtx label = NULL;
12413       rtx srcmem, dstmem;
12414       int desired_alignment = (TARGET_PENTIUMPRO
12415                                && (count == 0 || count >= (unsigned int) 260)
12416                                ? 8 : UNITS_PER_WORD);
12417       /* Get rid of MEM_OFFSETs, they won't be accurate.  */
12418       dst = change_address (dst, BLKmode, destreg);
12419       src = change_address (src, BLKmode, srcreg);
12420
12421       /* In case we don't know anything about the alignment, default to
12422          library version, since it is usually equally fast and result in
12423          shorter code.
12424
12425          Also emit call when we know that the count is large and call overhead
12426          will not be important.  */
12427       if (!TARGET_INLINE_ALL_STRINGOPS
12428           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
12429         return 0;
12430
12431       if (TARGET_SINGLE_STRINGOP)
12432         emit_insn (gen_cld ());
12433
12434       countreg2 = gen_reg_rtx (Pmode);
12435       countreg = copy_to_mode_reg (counter_mode, count_exp);
12436
12437       /* We don't use loops to align destination and to copy parts smaller
12438          than 4 bytes, because gcc is able to optimize such code better (in
12439          the case the destination or the count really is aligned, gcc is often
12440          able to predict the branches) and also it is friendlier to the
12441          hardware branch prediction.
12442
12443          Using loops is beneficial for generic case, because we can
12444          handle small counts using the loops.  Many CPUs (such as Athlon)
12445          have large REP prefix setup costs.
12446
12447          This is quite costly.  Maybe we can revisit this decision later or
12448          add some customizability to this code.  */
12449
12450       if (count == 0 && align < desired_alignment)
12451         {
12452           label = gen_label_rtx ();
12453           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
12454                                    LEU, 0, counter_mode, 1, label);
12455         }
12456       if (align <= 1)
12457         {
12458           rtx label = ix86_expand_aligntest (destreg, 1);
12459           srcmem = change_address (src, QImode, srcreg);
12460           dstmem = change_address (dst, QImode, destreg);
12461           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12462           ix86_adjust_counter (countreg, 1);
12463           emit_label (label);
12464           LABEL_NUSES (label) = 1;
12465         }
12466       if (align <= 2)
12467         {
12468           rtx label = ix86_expand_aligntest (destreg, 2);
12469           srcmem = change_address (src, HImode, srcreg);
12470           dstmem = change_address (dst, HImode, destreg);
12471           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12472           ix86_adjust_counter (countreg, 2);
12473           emit_label (label);
12474           LABEL_NUSES (label) = 1;
12475         }
12476       if (align <= 4 && desired_alignment > 4)
12477         {
12478           rtx label = ix86_expand_aligntest (destreg, 4);
12479           srcmem = change_address (src, SImode, srcreg);
12480           dstmem = change_address (dst, SImode, destreg);
12481           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12482           ix86_adjust_counter (countreg, 4);
12483           emit_label (label);
12484           LABEL_NUSES (label) = 1;
12485         }
12486
12487       if (label && desired_alignment > 4 && !TARGET_64BIT)
12488         {
12489           emit_label (label);
12490           LABEL_NUSES (label) = 1;
12491           label = NULL_RTX;
12492         }
12493       if (!TARGET_SINGLE_STRINGOP)
12494         emit_insn (gen_cld ());
12495       if (TARGET_64BIT)
12496         {
12497           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
12498                                   GEN_INT (3)));
12499           destexp = gen_rtx_ASHIFT (Pmode, countreg2, GEN_INT (3));
12500         }
12501       else
12502         {
12503           emit_insn (gen_lshrsi3 (countreg2, countreg, const2_rtx));
12504           destexp = gen_rtx_ASHIFT (Pmode, countreg2, const2_rtx);
12505         }
12506       srcexp = gen_rtx_PLUS (Pmode, destexp, srcreg);
12507       destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
12508       emit_insn (gen_rep_mov (destreg, dst, srcreg, src,
12509                               countreg2, destexp, srcexp));
12510
12511       if (label)
12512         {
12513           emit_label (label);
12514           LABEL_NUSES (label) = 1;
12515         }
12516       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
12517         {
12518           srcmem = change_address (src, SImode, srcreg);
12519           dstmem = change_address (dst, SImode, destreg);
12520           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12521         }
12522       if ((align <= 4 || count == 0) && TARGET_64BIT)
12523         {
12524           rtx label = ix86_expand_aligntest (countreg, 4);
12525           srcmem = change_address (src, SImode, srcreg);
12526           dstmem = change_address (dst, SImode, destreg);
12527           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12528           emit_label (label);
12529           LABEL_NUSES (label) = 1;
12530         }
12531       if (align > 2 && count != 0 && (count & 2))
12532         {
12533           srcmem = change_address (src, HImode, srcreg);
12534           dstmem = change_address (dst, HImode, destreg);
12535           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12536         }
12537       if (align <= 2 || count == 0)
12538         {
12539           rtx label = ix86_expand_aligntest (countreg, 2);
12540           srcmem = change_address (src, HImode, srcreg);
12541           dstmem = change_address (dst, HImode, destreg);
12542           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12543           emit_label (label);
12544           LABEL_NUSES (label) = 1;
12545         }
12546       if (align > 1 && count != 0 && (count & 1))
12547         {
12548           srcmem = change_address (src, QImode, srcreg);
12549           dstmem = change_address (dst, QImode, destreg);
12550           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12551         }
12552       if (align <= 1 || count == 0)
12553         {
12554           rtx label = ix86_expand_aligntest (countreg, 1);
12555           srcmem = change_address (src, QImode, srcreg);
12556           dstmem = change_address (dst, QImode, destreg);
12557           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12558           emit_label (label);
12559           LABEL_NUSES (label) = 1;
12560         }
12561     }
12562
12563   return 1;
12564 }
12565
12566 /* Expand string clear operation (bzero).  Use i386 string operations when
12567    profitable.  expand_movmem contains similar code.  */
12568 int
12569 ix86_expand_clrmem (rtx dst, rtx count_exp, rtx align_exp)
12570 {
12571   rtx destreg, zeroreg, countreg, destexp;
12572   enum machine_mode counter_mode;
12573   HOST_WIDE_INT align = 0;
12574   unsigned HOST_WIDE_INT count = 0;
12575
12576   if (GET_CODE (align_exp) == CONST_INT)
12577     align = INTVAL (align_exp);
12578
12579   /* Can't use any of this if the user has appropriated esi.  */
12580   if (global_regs[4])
12581     return 0;
12582
12583   /* This simple hack avoids all inlining code and simplifies code below.  */
12584   if (!TARGET_ALIGN_STRINGOPS)
12585     align = 32;
12586
12587   if (GET_CODE (count_exp) == CONST_INT)
12588     {
12589       count = INTVAL (count_exp);
12590       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
12591         return 0;
12592     }
12593   /* Figure out proper mode for counter.  For 32bits it is always SImode,
12594      for 64bits use SImode when possible, otherwise DImode.
12595      Set count to number of bytes copied when known at compile time.  */
12596   if (!TARGET_64BIT
12597       || GET_MODE (count_exp) == SImode
12598       || x86_64_zext_immediate_operand (count_exp, VOIDmode))
12599     counter_mode = SImode;
12600   else
12601     counter_mode = DImode;
12602
12603   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
12604   if (destreg != XEXP (dst, 0))
12605     dst = replace_equiv_address_nv (dst, destreg);
12606
12607
12608   /* When optimizing for size emit simple rep ; movsb instruction for
12609      counts not divisible by 4.  The movl $N, %ecx; rep; stosb
12610      sequence is 7 bytes long, so if optimizing for size and count is
12611      small enough that some stosl, stosw and stosb instructions without
12612      rep are shorter, fall back into the next if.  */
12613
12614   if ((!optimize || optimize_size)
12615       && (count == 0
12616           || ((count & 0x03)
12617               && (!optimize_size || (count & 0x03) + (count >> 2) > 7))))
12618     {
12619       emit_insn (gen_cld ());
12620
12621       countreg = ix86_zero_extend_to_Pmode (count_exp);
12622       zeroreg = copy_to_mode_reg (QImode, const0_rtx);
12623       destexp = gen_rtx_PLUS (Pmode, destreg, countreg);
12624       emit_insn (gen_rep_stos (destreg, countreg, dst, zeroreg, destexp));
12625     }
12626   else if (count != 0
12627            && (align >= 8
12628                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
12629                || optimize_size || count < (unsigned int) 64))
12630     {
12631       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
12632       unsigned HOST_WIDE_INT offset = 0;
12633
12634       emit_insn (gen_cld ());
12635
12636       zeroreg = copy_to_mode_reg (size == 4 ? SImode : DImode, const0_rtx);
12637       if (count & ~(size - 1))
12638         {
12639           unsigned HOST_WIDE_INT repcount;
12640           unsigned int max_nonrep;
12641
12642           repcount = count >> (size == 4 ? 2 : 3);
12643           if (!TARGET_64BIT)
12644             repcount &= 0x3fffffff;
12645
12646           /* movl $N, %ecx; rep; stosl is 7 bytes, while N x stosl is N bytes.
12647              movl $N, %ecx; rep; stosq is 8 bytes, while N x stosq is 2xN
12648              bytes.  In both cases the latter seems to be faster for small
12649              values of N.  */
12650           max_nonrep = size == 4 ? 7 : 4;
12651           if (!optimize_size)
12652             switch (ix86_tune)
12653               {
12654               case PROCESSOR_PENTIUM4:
12655               case PROCESSOR_NOCONA:
12656                 max_nonrep = 3;
12657                 break;
12658               default:
12659                 break;
12660               }
12661
12662           if (repcount <= max_nonrep)
12663             while (repcount-- > 0)
12664               {
12665                 rtx mem = adjust_automodify_address_nv (dst,
12666                                                         GET_MODE (zeroreg),
12667                                                         destreg, offset);
12668                 emit_insn (gen_strset (destreg, mem, zeroreg));
12669                 offset += size;
12670               }
12671           else
12672             {
12673               countreg = copy_to_mode_reg (counter_mode, GEN_INT (repcount));
12674               countreg = ix86_zero_extend_to_Pmode (countreg);
12675               destexp = gen_rtx_ASHIFT (Pmode, countreg,
12676                                         GEN_INT (size == 4 ? 2 : 3));
12677               destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
12678               emit_insn (gen_rep_stos (destreg, countreg, dst, zeroreg,
12679                                        destexp));
12680               offset = count & ~(size - 1);
12681             }
12682         }
12683       if (size == 8 && (count & 0x04))
12684         {
12685           rtx mem = adjust_automodify_address_nv (dst, SImode, destreg,
12686                                                   offset);
12687           emit_insn (gen_strset (destreg, mem,
12688                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
12689           offset += 4;
12690         }
12691       if (count & 0x02)
12692         {
12693           rtx mem = adjust_automodify_address_nv (dst, HImode, destreg,
12694                                                   offset);
12695           emit_insn (gen_strset (destreg, mem,
12696                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
12697           offset += 2;
12698         }
12699       if (count & 0x01)
12700         {
12701           rtx mem = adjust_automodify_address_nv (dst, QImode, destreg,
12702                                                   offset);
12703           emit_insn (gen_strset (destreg, mem,
12704                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
12705         }
12706     }
12707   else
12708     {
12709       rtx countreg2;
12710       rtx label = NULL;
12711       /* Compute desired alignment of the string operation.  */
12712       int desired_alignment = (TARGET_PENTIUMPRO
12713                                && (count == 0 || count >= (unsigned int) 260)
12714                                ? 8 : UNITS_PER_WORD);
12715
12716       /* In case we don't know anything about the alignment, default to
12717          library version, since it is usually equally fast and result in
12718          shorter code.
12719
12720          Also emit call when we know that the count is large and call overhead
12721          will not be important.  */
12722       if (!TARGET_INLINE_ALL_STRINGOPS
12723           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
12724         return 0;
12725
12726       if (TARGET_SINGLE_STRINGOP)
12727         emit_insn (gen_cld ());
12728
12729       countreg2 = gen_reg_rtx (Pmode);
12730       countreg = copy_to_mode_reg (counter_mode, count_exp);
12731       zeroreg = copy_to_mode_reg (Pmode, const0_rtx);
12732       /* Get rid of MEM_OFFSET, it won't be accurate.  */
12733       dst = change_address (dst, BLKmode, destreg);
12734
12735       if (count == 0 && align < desired_alignment)
12736         {
12737           label = gen_label_rtx ();
12738           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
12739                                    LEU, 0, counter_mode, 1, label);
12740         }
12741       if (align <= 1)
12742         {
12743           rtx label = ix86_expand_aligntest (destreg, 1);
12744           emit_insn (gen_strset (destreg, dst,
12745                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
12746           ix86_adjust_counter (countreg, 1);
12747           emit_label (label);
12748           LABEL_NUSES (label) = 1;
12749         }
12750       if (align <= 2)
12751         {
12752           rtx label = ix86_expand_aligntest (destreg, 2);
12753           emit_insn (gen_strset (destreg, dst,
12754                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
12755           ix86_adjust_counter (countreg, 2);
12756           emit_label (label);
12757           LABEL_NUSES (label) = 1;
12758         }
12759       if (align <= 4 && desired_alignment > 4)
12760         {
12761           rtx label = ix86_expand_aligntest (destreg, 4);
12762           emit_insn (gen_strset (destreg, dst,
12763                                  (TARGET_64BIT
12764                                   ? gen_rtx_SUBREG (SImode, zeroreg, 0)
12765                                   : zeroreg)));
12766           ix86_adjust_counter (countreg, 4);
12767           emit_label (label);
12768           LABEL_NUSES (label) = 1;
12769         }
12770
12771       if (label && desired_alignment > 4 && !TARGET_64BIT)
12772         {
12773           emit_label (label);
12774           LABEL_NUSES (label) = 1;
12775           label = NULL_RTX;
12776         }
12777
12778       if (!TARGET_SINGLE_STRINGOP)
12779         emit_insn (gen_cld ());
12780       if (TARGET_64BIT)
12781         {
12782           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
12783                                   GEN_INT (3)));
12784           destexp = gen_rtx_ASHIFT (Pmode, countreg2, GEN_INT (3));
12785         }
12786       else
12787         {
12788           emit_insn (gen_lshrsi3 (countreg2, countreg, const2_rtx));
12789           destexp = gen_rtx_ASHIFT (Pmode, countreg2, const2_rtx);
12790         }
12791       destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
12792       emit_insn (gen_rep_stos (destreg, countreg2, dst, zeroreg, destexp));
12793
12794       if (label)
12795         {
12796           emit_label (label);
12797           LABEL_NUSES (label) = 1;
12798         }
12799
12800       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
12801         emit_insn (gen_strset (destreg, dst,
12802                                gen_rtx_SUBREG (SImode, zeroreg, 0)));
12803       if (TARGET_64BIT && (align <= 4 || count == 0))
12804         {
12805           rtx label = ix86_expand_aligntest (countreg, 4);
12806           emit_insn (gen_strset (destreg, dst,
12807                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
12808           emit_label (label);
12809           LABEL_NUSES (label) = 1;
12810         }
12811       if (align > 2 && count != 0 && (count & 2))
12812         emit_insn (gen_strset (destreg, dst,
12813                                gen_rtx_SUBREG (HImode, zeroreg, 0)));
12814       if (align <= 2 || count == 0)
12815         {
12816           rtx label = ix86_expand_aligntest (countreg, 2);
12817           emit_insn (gen_strset (destreg, dst,
12818                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
12819           emit_label (label);
12820           LABEL_NUSES (label) = 1;
12821         }
12822       if (align > 1 && count != 0 && (count & 1))
12823         emit_insn (gen_strset (destreg, dst,
12824                                gen_rtx_SUBREG (QImode, zeroreg, 0)));
12825       if (align <= 1 || count == 0)
12826         {
12827           rtx label = ix86_expand_aligntest (countreg, 1);
12828           emit_insn (gen_strset (destreg, dst,
12829                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
12830           emit_label (label);
12831           LABEL_NUSES (label) = 1;
12832         }
12833     }
12834   return 1;
12835 }
12836
12837 /* Expand strlen.  */
12838 int
12839 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
12840 {
12841   rtx addr, scratch1, scratch2, scratch3, scratch4;
12842
12843   /* The generic case of strlen expander is long.  Avoid it's
12844      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
12845
12846   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
12847       && !TARGET_INLINE_ALL_STRINGOPS
12848       && !optimize_size
12849       && (GET_CODE (align) != CONST_INT || INTVAL (align) < 4))
12850     return 0;
12851
12852   addr = force_reg (Pmode, XEXP (src, 0));
12853   scratch1 = gen_reg_rtx (Pmode);
12854
12855   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
12856       && !optimize_size)
12857     {
12858       /* Well it seems that some optimizer does not combine a call like
12859          foo(strlen(bar), strlen(bar));
12860          when the move and the subtraction is done here.  It does calculate
12861          the length just once when these instructions are done inside of
12862          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
12863          often used and I use one fewer register for the lifetime of
12864          output_strlen_unroll() this is better.  */
12865
12866       emit_move_insn (out, addr);
12867
12868       ix86_expand_strlensi_unroll_1 (out, src, align);
12869
12870       /* strlensi_unroll_1 returns the address of the zero at the end of
12871          the string, like memchr(), so compute the length by subtracting
12872          the start address.  */
12873       if (TARGET_64BIT)
12874         emit_insn (gen_subdi3 (out, out, addr));
12875       else
12876         emit_insn (gen_subsi3 (out, out, addr));
12877     }
12878   else
12879     {
12880       rtx unspec;
12881       scratch2 = gen_reg_rtx (Pmode);
12882       scratch3 = gen_reg_rtx (Pmode);
12883       scratch4 = force_reg (Pmode, constm1_rtx);
12884
12885       emit_move_insn (scratch3, addr);
12886       eoschar = force_reg (QImode, eoschar);
12887
12888       emit_insn (gen_cld ());
12889       src = replace_equiv_address_nv (src, scratch3);
12890
12891       /* If .md starts supporting :P, this can be done in .md.  */
12892       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
12893                                                  scratch4), UNSPEC_SCAS);
12894       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
12895       if (TARGET_64BIT)
12896         {
12897           emit_insn (gen_one_cmpldi2 (scratch2, scratch1));
12898           emit_insn (gen_adddi3 (out, scratch2, constm1_rtx));
12899         }
12900       else
12901         {
12902           emit_insn (gen_one_cmplsi2 (scratch2, scratch1));
12903           emit_insn (gen_addsi3 (out, scratch2, constm1_rtx));
12904         }
12905     }
12906   return 1;
12907 }
12908
12909 /* Expand the appropriate insns for doing strlen if not just doing
12910    repnz; scasb
12911
12912    out = result, initialized with the start address
12913    align_rtx = alignment of the address.
12914    scratch = scratch register, initialized with the startaddress when
12915         not aligned, otherwise undefined
12916
12917    This is just the body. It needs the initializations mentioned above and
12918    some address computing at the end.  These things are done in i386.md.  */
12919
12920 static void
12921 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
12922 {
12923   int align;
12924   rtx tmp;
12925   rtx align_2_label = NULL_RTX;
12926   rtx align_3_label = NULL_RTX;
12927   rtx align_4_label = gen_label_rtx ();
12928   rtx end_0_label = gen_label_rtx ();
12929   rtx mem;
12930   rtx tmpreg = gen_reg_rtx (SImode);
12931   rtx scratch = gen_reg_rtx (SImode);
12932   rtx cmp;
12933
12934   align = 0;
12935   if (GET_CODE (align_rtx) == CONST_INT)
12936     align = INTVAL (align_rtx);
12937
12938   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
12939
12940   /* Is there a known alignment and is it less than 4?  */
12941   if (align < 4)
12942     {
12943       rtx scratch1 = gen_reg_rtx (Pmode);
12944       emit_move_insn (scratch1, out);
12945       /* Is there a known alignment and is it not 2? */
12946       if (align != 2)
12947         {
12948           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
12949           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
12950
12951           /* Leave just the 3 lower bits.  */
12952           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
12953                                     NULL_RTX, 0, OPTAB_WIDEN);
12954
12955           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
12956                                    Pmode, 1, align_4_label);
12957           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
12958                                    Pmode, 1, align_2_label);
12959           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
12960                                    Pmode, 1, align_3_label);
12961         }
12962       else
12963         {
12964           /* Since the alignment is 2, we have to check 2 or 0 bytes;
12965              check if is aligned to 4 - byte.  */
12966
12967           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
12968                                     NULL_RTX, 0, OPTAB_WIDEN);
12969
12970           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
12971                                    Pmode, 1, align_4_label);
12972         }
12973
12974       mem = change_address (src, QImode, out);
12975
12976       /* Now compare the bytes.  */
12977
12978       /* Compare the first n unaligned byte on a byte per byte basis.  */
12979       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
12980                                QImode, 1, end_0_label);
12981
12982       /* Increment the address.  */
12983       if (TARGET_64BIT)
12984         emit_insn (gen_adddi3 (out, out, const1_rtx));
12985       else
12986         emit_insn (gen_addsi3 (out, out, const1_rtx));
12987
12988       /* Not needed with an alignment of 2 */
12989       if (align != 2)
12990         {
12991           emit_label (align_2_label);
12992
12993           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
12994                                    end_0_label);
12995
12996           if (TARGET_64BIT)
12997             emit_insn (gen_adddi3 (out, out, const1_rtx));
12998           else
12999             emit_insn (gen_addsi3 (out, out, const1_rtx));
13000
13001           emit_label (align_3_label);
13002         }
13003
13004       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
13005                                end_0_label);
13006
13007       if (TARGET_64BIT)
13008         emit_insn (gen_adddi3 (out, out, const1_rtx));
13009       else
13010         emit_insn (gen_addsi3 (out, out, const1_rtx));
13011     }
13012
13013   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
13014      align this loop.  It gives only huge programs, but does not help to
13015      speed up.  */
13016   emit_label (align_4_label);
13017
13018   mem = change_address (src, SImode, out);
13019   emit_move_insn (scratch, mem);
13020   if (TARGET_64BIT)
13021     emit_insn (gen_adddi3 (out, out, GEN_INT (4)));
13022   else
13023     emit_insn (gen_addsi3 (out, out, GEN_INT (4)));
13024
13025   /* This formula yields a nonzero result iff one of the bytes is zero.
13026      This saves three branches inside loop and many cycles.  */
13027
13028   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
13029   emit_insn (gen_one_cmplsi2 (scratch, scratch));
13030   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
13031   emit_insn (gen_andsi3 (tmpreg, tmpreg,
13032                          gen_int_mode (0x80808080, SImode)));
13033   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
13034                            align_4_label);
13035
13036   if (TARGET_CMOVE)
13037     {
13038        rtx reg = gen_reg_rtx (SImode);
13039        rtx reg2 = gen_reg_rtx (Pmode);
13040        emit_move_insn (reg, tmpreg);
13041        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
13042
13043        /* If zero is not in the first two bytes, move two bytes forward.  */
13044        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
13045        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
13046        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
13047        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
13048                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
13049                                                      reg,
13050                                                      tmpreg)));
13051        /* Emit lea manually to avoid clobbering of flags.  */
13052        emit_insn (gen_rtx_SET (SImode, reg2,
13053                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
13054
13055        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
13056        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
13057        emit_insn (gen_rtx_SET (VOIDmode, out,
13058                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
13059                                                      reg2,
13060                                                      out)));
13061
13062     }
13063   else
13064     {
13065        rtx end_2_label = gen_label_rtx ();
13066        /* Is zero in the first two bytes? */
13067
13068        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
13069        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
13070        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
13071        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
13072                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
13073                             pc_rtx);
13074        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
13075        JUMP_LABEL (tmp) = end_2_label;
13076
13077        /* Not in the first two.  Move two bytes forward.  */
13078        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
13079        if (TARGET_64BIT)
13080          emit_insn (gen_adddi3 (out, out, const2_rtx));
13081        else
13082          emit_insn (gen_addsi3 (out, out, const2_rtx));
13083
13084        emit_label (end_2_label);
13085
13086     }
13087
13088   /* Avoid branch in fixing the byte.  */
13089   tmpreg = gen_lowpart (QImode, tmpreg);
13090   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
13091   cmp = gen_rtx_LTU (Pmode, gen_rtx_REG (CCmode, 17), const0_rtx);
13092   if (TARGET_64BIT)
13093     emit_insn (gen_subdi3_carry_rex64 (out, out, GEN_INT (3), cmp));
13094   else
13095     emit_insn (gen_subsi3_carry (out, out, GEN_INT (3), cmp));
13096
13097   emit_label (end_0_label);
13098 }
13099
13100 void
13101 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
13102                   rtx callarg2 ATTRIBUTE_UNUSED,
13103                   rtx pop, int sibcall)
13104 {
13105   rtx use = NULL, call;
13106
13107   if (pop == const0_rtx)
13108     pop = NULL;
13109   gcc_assert (!TARGET_64BIT || !pop);
13110
13111 #if TARGET_MACHO
13112   if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
13113     fnaddr = machopic_indirect_call_target (fnaddr);
13114 #else
13115   /* Static functions and indirect calls don't need the pic register.  */
13116   if (! TARGET_64BIT && flag_pic
13117       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
13118       && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
13119     use_reg (&use, pic_offset_table_rtx);
13120
13121   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
13122     {
13123       rtx al = gen_rtx_REG (QImode, 0);
13124       emit_move_insn (al, callarg2);
13125       use_reg (&use, al);
13126     }
13127 #endif /* TARGET_MACHO */
13128
13129   if (! call_insn_operand (XEXP (fnaddr, 0), Pmode))
13130     {
13131       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
13132       fnaddr = gen_rtx_MEM (QImode, fnaddr);
13133     }
13134   if (sibcall && TARGET_64BIT
13135       && !constant_call_address_operand (XEXP (fnaddr, 0), Pmode))
13136     {
13137       rtx addr;
13138       addr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
13139       fnaddr = gen_rtx_REG (Pmode, FIRST_REX_INT_REG + 3 /* R11 */);
13140       emit_move_insn (fnaddr, addr);
13141       fnaddr = gen_rtx_MEM (QImode, fnaddr);
13142     }
13143
13144   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
13145   if (retval)
13146     call = gen_rtx_SET (VOIDmode, retval, call);
13147   if (pop)
13148     {
13149       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
13150       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
13151       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
13152     }
13153
13154   call = emit_call_insn (call);
13155   if (use)
13156     CALL_INSN_FUNCTION_USAGE (call) = use;
13157 }
13158
13159 \f
13160 /* Clear stack slot assignments remembered from previous functions.
13161    This is called from INIT_EXPANDERS once before RTL is emitted for each
13162    function.  */
13163
13164 static struct machine_function *
13165 ix86_init_machine_status (void)
13166 {
13167   struct machine_function *f;
13168
13169   f = ggc_alloc_cleared (sizeof (struct machine_function));
13170   f->use_fast_prologue_epilogue_nregs = -1;
13171   f->tls_descriptor_call_expanded_p = 0;
13172
13173   return f;
13174 }
13175
13176 /* Return a MEM corresponding to a stack slot with mode MODE.
13177    Allocate a new slot if necessary.
13178
13179    The RTL for a function can have several slots available: N is
13180    which slot to use.  */
13181
13182 rtx
13183 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
13184 {
13185   struct stack_local_entry *s;
13186
13187   gcc_assert (n < MAX_386_STACK_LOCALS);
13188
13189   for (s = ix86_stack_locals; s; s = s->next)
13190     if (s->mode == mode && s->n == n)
13191       return s->rtl;
13192
13193   s = (struct stack_local_entry *)
13194     ggc_alloc (sizeof (struct stack_local_entry));
13195   s->n = n;
13196   s->mode = mode;
13197   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
13198
13199   s->next = ix86_stack_locals;
13200   ix86_stack_locals = s;
13201   return s->rtl;
13202 }
13203
13204 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
13205
13206 static GTY(()) rtx ix86_tls_symbol;
13207 rtx
13208 ix86_tls_get_addr (void)
13209 {
13210
13211   if (!ix86_tls_symbol)
13212     {
13213       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
13214                                             (TARGET_ANY_GNU_TLS
13215                                              && !TARGET_64BIT)
13216                                             ? "___tls_get_addr"
13217                                             : "__tls_get_addr");
13218     }
13219
13220   return ix86_tls_symbol;
13221 }
13222
13223 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
13224
13225 static GTY(()) rtx ix86_tls_module_base_symbol;
13226 rtx
13227 ix86_tls_module_base (void)
13228 {
13229
13230   if (!ix86_tls_module_base_symbol)
13231     {
13232       ix86_tls_module_base_symbol = gen_rtx_SYMBOL_REF (Pmode,
13233                                                         "_TLS_MODULE_BASE_");
13234       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
13235         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
13236     }
13237
13238   return ix86_tls_module_base_symbol;
13239 }
13240 \f
13241 /* Calculate the length of the memory address in the instruction
13242    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
13243
13244 int
13245 memory_address_length (rtx addr)
13246 {
13247   struct ix86_address parts;
13248   rtx base, index, disp;
13249   int len;
13250   int ok;
13251
13252   if (GET_CODE (addr) == PRE_DEC
13253       || GET_CODE (addr) == POST_INC
13254       || GET_CODE (addr) == PRE_MODIFY
13255       || GET_CODE (addr) == POST_MODIFY)
13256     return 0;
13257
13258   ok = ix86_decompose_address (addr, &parts);
13259   gcc_assert (ok);
13260
13261   if (parts.base && GET_CODE (parts.base) == SUBREG)
13262     parts.base = SUBREG_REG (parts.base);
13263   if (parts.index && GET_CODE (parts.index) == SUBREG)
13264     parts.index = SUBREG_REG (parts.index);
13265
13266   base = parts.base;
13267   index = parts.index;
13268   disp = parts.disp;
13269   len = 0;
13270
13271   /* Rule of thumb:
13272        - esp as the base always wants an index,
13273        - ebp as the base always wants a displacement.  */
13274
13275   /* Register Indirect.  */
13276   if (base && !index && !disp)
13277     {
13278       /* esp (for its index) and ebp (for its displacement) need
13279          the two-byte modrm form.  */
13280       if (addr == stack_pointer_rtx
13281           || addr == arg_pointer_rtx
13282           || addr == frame_pointer_rtx
13283           || addr == hard_frame_pointer_rtx)
13284         len = 1;
13285     }
13286
13287   /* Direct Addressing.  */
13288   else if (disp && !base && !index)
13289     len = 4;
13290
13291   else
13292     {
13293       /* Find the length of the displacement constant.  */
13294       if (disp)
13295         {
13296           if (base && satisfies_constraint_K (disp))
13297             len = 1;
13298           else
13299             len = 4;
13300         }
13301       /* ebp always wants a displacement.  */
13302       else if (base == hard_frame_pointer_rtx)
13303         len = 1;
13304
13305       /* An index requires the two-byte modrm form....  */
13306       if (index
13307           /* ...like esp, which always wants an index.  */
13308           || base == stack_pointer_rtx
13309           || base == arg_pointer_rtx
13310           || base == frame_pointer_rtx)
13311         len += 1;
13312     }
13313
13314   return len;
13315 }
13316
13317 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
13318    is set, expect that insn have 8bit immediate alternative.  */
13319 int
13320 ix86_attr_length_immediate_default (rtx insn, int shortform)
13321 {
13322   int len = 0;
13323   int i;
13324   extract_insn_cached (insn);
13325   for (i = recog_data.n_operands - 1; i >= 0; --i)
13326     if (CONSTANT_P (recog_data.operand[i]))
13327       {
13328         gcc_assert (!len);
13329         if (shortform && satisfies_constraint_K (recog_data.operand[i]))
13330           len = 1;
13331         else
13332           {
13333             switch (get_attr_mode (insn))
13334               {
13335                 case MODE_QI:
13336                   len+=1;
13337                   break;
13338                 case MODE_HI:
13339                   len+=2;
13340                   break;
13341                 case MODE_SI:
13342                   len+=4;
13343                   break;
13344                 /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
13345                 case MODE_DI:
13346                   len+=4;
13347                   break;
13348                 default:
13349                   fatal_insn ("unknown insn mode", insn);
13350               }
13351           }
13352       }
13353   return len;
13354 }
13355 /* Compute default value for "length_address" attribute.  */
13356 int
13357 ix86_attr_length_address_default (rtx insn)
13358 {
13359   int i;
13360
13361   if (get_attr_type (insn) == TYPE_LEA)
13362     {
13363       rtx set = PATTERN (insn);
13364
13365       if (GET_CODE (set) == PARALLEL)
13366         set = XVECEXP (set, 0, 0);
13367
13368       gcc_assert (GET_CODE (set) == SET);
13369
13370       return memory_address_length (SET_SRC (set));
13371     }
13372
13373   extract_insn_cached (insn);
13374   for (i = recog_data.n_operands - 1; i >= 0; --i)
13375     if (GET_CODE (recog_data.operand[i]) == MEM)
13376       {
13377         return memory_address_length (XEXP (recog_data.operand[i], 0));
13378         break;
13379       }
13380   return 0;
13381 }
13382 \f
13383 /* Return the maximum number of instructions a cpu can issue.  */
13384
13385 static int
13386 ix86_issue_rate (void)
13387 {
13388   switch (ix86_tune)
13389     {
13390     case PROCESSOR_PENTIUM:
13391     case PROCESSOR_K6:
13392       return 2;
13393
13394     case PROCESSOR_PENTIUMPRO:
13395     case PROCESSOR_PENTIUM4:
13396     case PROCESSOR_ATHLON:
13397     case PROCESSOR_K8:
13398     case PROCESSOR_NOCONA:
13399     case PROCESSOR_GENERIC32:
13400     case PROCESSOR_GENERIC64:
13401       return 3;
13402
13403     default:
13404       return 1;
13405     }
13406 }
13407
13408 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
13409    by DEP_INSN and nothing set by DEP_INSN.  */
13410
13411 static int
13412 ix86_flags_dependant (rtx insn, rtx dep_insn, enum attr_type insn_type)
13413 {
13414   rtx set, set2;
13415
13416   /* Simplify the test for uninteresting insns.  */
13417   if (insn_type != TYPE_SETCC
13418       && insn_type != TYPE_ICMOV
13419       && insn_type != TYPE_FCMOV
13420       && insn_type != TYPE_IBR)
13421     return 0;
13422
13423   if ((set = single_set (dep_insn)) != 0)
13424     {
13425       set = SET_DEST (set);
13426       set2 = NULL_RTX;
13427     }
13428   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
13429            && XVECLEN (PATTERN (dep_insn), 0) == 2
13430            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
13431            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
13432     {
13433       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
13434       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
13435     }
13436   else
13437     return 0;
13438
13439   if (GET_CODE (set) != REG || REGNO (set) != FLAGS_REG)
13440     return 0;
13441
13442   /* This test is true if the dependent insn reads the flags but
13443      not any other potentially set register.  */
13444   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
13445     return 0;
13446
13447   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
13448     return 0;
13449
13450   return 1;
13451 }
13452
13453 /* A subroutine of ix86_adjust_cost -- return true iff INSN has a memory
13454    address with operands set by DEP_INSN.  */
13455
13456 static int
13457 ix86_agi_dependant (rtx insn, rtx dep_insn, enum attr_type insn_type)
13458 {
13459   rtx addr;
13460
13461   if (insn_type == TYPE_LEA
13462       && TARGET_PENTIUM)
13463     {
13464       addr = PATTERN (insn);
13465
13466       if (GET_CODE (addr) == PARALLEL)
13467         addr = XVECEXP (addr, 0, 0);
13468       
13469       gcc_assert (GET_CODE (addr) == SET);
13470       
13471       addr = SET_SRC (addr);
13472     }
13473   else
13474     {
13475       int i;
13476       extract_insn_cached (insn);
13477       for (i = recog_data.n_operands - 1; i >= 0; --i)
13478         if (GET_CODE (recog_data.operand[i]) == MEM)
13479           {
13480             addr = XEXP (recog_data.operand[i], 0);
13481             goto found;
13482           }
13483       return 0;
13484     found:;
13485     }
13486
13487   return modified_in_p (addr, dep_insn);
13488 }
13489
13490 static int
13491 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
13492 {
13493   enum attr_type insn_type, dep_insn_type;
13494   enum attr_memory memory;
13495   rtx set, set2;
13496   int dep_insn_code_number;
13497
13498   /* Anti and output dependencies have zero cost on all CPUs.  */
13499   if (REG_NOTE_KIND (link) != 0)
13500     return 0;
13501
13502   dep_insn_code_number = recog_memoized (dep_insn);
13503
13504   /* If we can't recognize the insns, we can't really do anything.  */
13505   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
13506     return cost;
13507
13508   insn_type = get_attr_type (insn);
13509   dep_insn_type = get_attr_type (dep_insn);
13510
13511   switch (ix86_tune)
13512     {
13513     case PROCESSOR_PENTIUM:
13514       /* Address Generation Interlock adds a cycle of latency.  */
13515       if (ix86_agi_dependant (insn, dep_insn, insn_type))
13516         cost += 1;
13517
13518       /* ??? Compares pair with jump/setcc.  */
13519       if (ix86_flags_dependant (insn, dep_insn, insn_type))
13520         cost = 0;
13521
13522       /* Floating point stores require value to be ready one cycle earlier.  */
13523       if (insn_type == TYPE_FMOV
13524           && get_attr_memory (insn) == MEMORY_STORE
13525           && !ix86_agi_dependant (insn, dep_insn, insn_type))
13526         cost += 1;
13527       break;
13528
13529     case PROCESSOR_PENTIUMPRO:
13530       memory = get_attr_memory (insn);
13531
13532       /* INT->FP conversion is expensive.  */
13533       if (get_attr_fp_int_src (dep_insn))
13534         cost += 5;
13535
13536       /* There is one cycle extra latency between an FP op and a store.  */
13537       if (insn_type == TYPE_FMOV
13538           && (set = single_set (dep_insn)) != NULL_RTX
13539           && (set2 = single_set (insn)) != NULL_RTX
13540           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
13541           && GET_CODE (SET_DEST (set2)) == MEM)
13542         cost += 1;
13543
13544       /* Show ability of reorder buffer to hide latency of load by executing
13545          in parallel with previous instruction in case
13546          previous instruction is not needed to compute the address.  */
13547       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
13548           && !ix86_agi_dependant (insn, dep_insn, insn_type))
13549         {
13550           /* Claim moves to take one cycle, as core can issue one load
13551              at time and the next load can start cycle later.  */
13552           if (dep_insn_type == TYPE_IMOV
13553               || dep_insn_type == TYPE_FMOV)
13554             cost = 1;
13555           else if (cost > 1)
13556             cost--;
13557         }
13558       break;
13559
13560     case PROCESSOR_K6:
13561       memory = get_attr_memory (insn);
13562
13563       /* The esp dependency is resolved before the instruction is really
13564          finished.  */
13565       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
13566           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
13567         return 1;
13568
13569       /* INT->FP conversion is expensive.  */
13570       if (get_attr_fp_int_src (dep_insn))
13571         cost += 5;
13572
13573       /* Show ability of reorder buffer to hide latency of load by executing
13574          in parallel with previous instruction in case
13575          previous instruction is not needed to compute the address.  */
13576       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
13577           && !ix86_agi_dependant (insn, dep_insn, insn_type))
13578         {
13579           /* Claim moves to take one cycle, as core can issue one load
13580              at time and the next load can start cycle later.  */
13581           if (dep_insn_type == TYPE_IMOV
13582               || dep_insn_type == TYPE_FMOV)
13583             cost = 1;
13584           else if (cost > 2)
13585             cost -= 2;
13586           else
13587             cost = 1;
13588         }
13589       break;
13590
13591     case PROCESSOR_ATHLON:
13592     case PROCESSOR_K8:
13593     case PROCESSOR_GENERIC32:
13594     case PROCESSOR_GENERIC64:
13595       memory = get_attr_memory (insn);
13596
13597       /* Show ability of reorder buffer to hide latency of load by executing
13598          in parallel with previous instruction in case
13599          previous instruction is not needed to compute the address.  */
13600       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
13601           && !ix86_agi_dependant (insn, dep_insn, insn_type))
13602         {
13603           enum attr_unit unit = get_attr_unit (insn);
13604           int loadcost = 3;
13605
13606           /* Because of the difference between the length of integer and
13607              floating unit pipeline preparation stages, the memory operands
13608              for floating point are cheaper.
13609
13610              ??? For Athlon it the difference is most probably 2.  */
13611           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
13612             loadcost = 3;
13613           else
13614             loadcost = TARGET_ATHLON ? 2 : 0;
13615
13616           if (cost >= loadcost)
13617             cost -= loadcost;
13618           else
13619             cost = 0;
13620         }
13621
13622     default:
13623       break;
13624     }
13625
13626   return cost;
13627 }
13628
13629 /* How many alternative schedules to try.  This should be as wide as the
13630    scheduling freedom in the DFA, but no wider.  Making this value too
13631    large results extra work for the scheduler.  */
13632
13633 static int
13634 ia32_multipass_dfa_lookahead (void)
13635 {
13636   if (ix86_tune == PROCESSOR_PENTIUM)
13637     return 2;
13638
13639   if (ix86_tune == PROCESSOR_PENTIUMPRO
13640       || ix86_tune == PROCESSOR_K6)
13641     return 1;
13642
13643   else
13644     return 0;
13645 }
13646
13647 \f
13648 /* Compute the alignment given to a constant that is being placed in memory.
13649    EXP is the constant and ALIGN is the alignment that the object would
13650    ordinarily have.
13651    The value of this function is used instead of that alignment to align
13652    the object.  */
13653
13654 int
13655 ix86_constant_alignment (tree exp, int align)
13656 {
13657   if (TREE_CODE (exp) == REAL_CST)
13658     {
13659       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
13660         return 64;
13661       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
13662         return 128;
13663     }
13664   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
13665            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
13666     return BITS_PER_WORD;
13667
13668   return align;
13669 }
13670
13671 /* Compute the alignment for a static variable.
13672    TYPE is the data type, and ALIGN is the alignment that
13673    the object would ordinarily have.  The value of this function is used
13674    instead of that alignment to align the object.  */
13675
13676 int
13677 ix86_data_alignment (tree type, int align)
13678 {
13679   int max_align = optimize_size ? BITS_PER_WORD : 256;
13680
13681   if (AGGREGATE_TYPE_P (type)
13682       && TYPE_SIZE (type)
13683       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
13684       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
13685           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
13686       && align < max_align)
13687     align = max_align;
13688
13689   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
13690      to 16byte boundary.  */
13691   if (TARGET_64BIT)
13692     {
13693       if (AGGREGATE_TYPE_P (type)
13694            && TYPE_SIZE (type)
13695            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
13696            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
13697                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
13698         return 128;
13699     }
13700
13701   if (TREE_CODE (type) == ARRAY_TYPE)
13702     {
13703       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
13704         return 64;
13705       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
13706         return 128;
13707     }
13708   else if (TREE_CODE (type) == COMPLEX_TYPE)
13709     {
13710
13711       if (TYPE_MODE (type) == DCmode && align < 64)
13712         return 64;
13713       if (TYPE_MODE (type) == XCmode && align < 128)
13714         return 128;
13715     }
13716   else if ((TREE_CODE (type) == RECORD_TYPE
13717             || TREE_CODE (type) == UNION_TYPE
13718             || TREE_CODE (type) == QUAL_UNION_TYPE)
13719            && TYPE_FIELDS (type))
13720     {
13721       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
13722         return 64;
13723       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
13724         return 128;
13725     }
13726   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
13727            || TREE_CODE (type) == INTEGER_TYPE)
13728     {
13729       if (TYPE_MODE (type) == DFmode && align < 64)
13730         return 64;
13731       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
13732         return 128;
13733     }
13734
13735   return align;
13736 }
13737
13738 /* Compute the alignment for a local variable.
13739    TYPE is the data type, and ALIGN is the alignment that
13740    the object would ordinarily have.  The value of this macro is used
13741    instead of that alignment to align the object.  */
13742
13743 int
13744 ix86_local_alignment (tree type, int align)
13745 {
13746   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
13747      to 16byte boundary.  */
13748   if (TARGET_64BIT)
13749     {
13750       if (AGGREGATE_TYPE_P (type)
13751            && TYPE_SIZE (type)
13752            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
13753            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
13754                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
13755         return 128;
13756     }
13757   if (TREE_CODE (type) == ARRAY_TYPE)
13758     {
13759       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
13760         return 64;
13761       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
13762         return 128;
13763     }
13764   else if (TREE_CODE (type) == COMPLEX_TYPE)
13765     {
13766       if (TYPE_MODE (type) == DCmode && align < 64)
13767         return 64;
13768       if (TYPE_MODE (type) == XCmode && align < 128)
13769         return 128;
13770     }
13771   else if ((TREE_CODE (type) == RECORD_TYPE
13772             || TREE_CODE (type) == UNION_TYPE
13773             || TREE_CODE (type) == QUAL_UNION_TYPE)
13774            && TYPE_FIELDS (type))
13775     {
13776       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
13777         return 64;
13778       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
13779         return 128;
13780     }
13781   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
13782            || TREE_CODE (type) == INTEGER_TYPE)
13783     {
13784
13785       if (TYPE_MODE (type) == DFmode && align < 64)
13786         return 64;
13787       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
13788         return 128;
13789     }
13790   return align;
13791 }
13792 \f
13793 /* Emit RTL insns to initialize the variable parts of a trampoline.
13794    FNADDR is an RTX for the address of the function's pure code.
13795    CXT is an RTX for the static chain value for the function.  */
13796 void
13797 x86_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
13798 {
13799   if (!TARGET_64BIT)
13800     {
13801       /* Compute offset from the end of the jmp to the target function.  */
13802       rtx disp = expand_binop (SImode, sub_optab, fnaddr,
13803                                plus_constant (tramp, 10),
13804                                NULL_RTX, 1, OPTAB_DIRECT);
13805       emit_move_insn (gen_rtx_MEM (QImode, tramp),
13806                       gen_int_mode (0xb9, QImode));
13807       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 1)), cxt);
13808       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 5)),
13809                       gen_int_mode (0xe9, QImode));
13810       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 6)), disp);
13811     }
13812   else
13813     {
13814       int offset = 0;
13815       /* Try to load address using shorter movl instead of movabs.
13816          We may want to support movq for kernel mode, but kernel does not use
13817          trampolines at the moment.  */
13818       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
13819         {
13820           fnaddr = copy_to_mode_reg (DImode, fnaddr);
13821           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
13822                           gen_int_mode (0xbb41, HImode));
13823           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, offset + 2)),
13824                           gen_lowpart (SImode, fnaddr));
13825           offset += 6;
13826         }
13827       else
13828         {
13829           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
13830                           gen_int_mode (0xbb49, HImode));
13831           emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
13832                           fnaddr);
13833           offset += 10;
13834         }
13835       /* Load static chain using movabs to r10.  */
13836       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
13837                       gen_int_mode (0xba49, HImode));
13838       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
13839                       cxt);
13840       offset += 10;
13841       /* Jump to the r11 */
13842       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
13843                       gen_int_mode (0xff49, HImode));
13844       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, offset+2)),
13845                       gen_int_mode (0xe3, QImode));
13846       offset += 3;
13847       gcc_assert (offset <= TRAMPOLINE_SIZE);
13848     }
13849
13850 #ifdef ENABLE_EXECUTE_STACK
13851   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
13852                      LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
13853 #endif
13854 }
13855 \f
13856 /* Codes for all the SSE/MMX builtins.  */
13857 enum ix86_builtins
13858 {
13859   IX86_BUILTIN_ADDPS,
13860   IX86_BUILTIN_ADDSS,
13861   IX86_BUILTIN_DIVPS,
13862   IX86_BUILTIN_DIVSS,
13863   IX86_BUILTIN_MULPS,
13864   IX86_BUILTIN_MULSS,
13865   IX86_BUILTIN_SUBPS,
13866   IX86_BUILTIN_SUBSS,
13867
13868   IX86_BUILTIN_CMPEQPS,
13869   IX86_BUILTIN_CMPLTPS,
13870   IX86_BUILTIN_CMPLEPS,
13871   IX86_BUILTIN_CMPGTPS,
13872   IX86_BUILTIN_CMPGEPS,
13873   IX86_BUILTIN_CMPNEQPS,
13874   IX86_BUILTIN_CMPNLTPS,
13875   IX86_BUILTIN_CMPNLEPS,
13876   IX86_BUILTIN_CMPNGTPS,
13877   IX86_BUILTIN_CMPNGEPS,
13878   IX86_BUILTIN_CMPORDPS,
13879   IX86_BUILTIN_CMPUNORDPS,
13880   IX86_BUILTIN_CMPEQSS,
13881   IX86_BUILTIN_CMPLTSS,
13882   IX86_BUILTIN_CMPLESS,
13883   IX86_BUILTIN_CMPNEQSS,
13884   IX86_BUILTIN_CMPNLTSS,
13885   IX86_BUILTIN_CMPNLESS,
13886   IX86_BUILTIN_CMPNGTSS,
13887   IX86_BUILTIN_CMPNGESS,
13888   IX86_BUILTIN_CMPORDSS,
13889   IX86_BUILTIN_CMPUNORDSS,
13890
13891   IX86_BUILTIN_COMIEQSS,
13892   IX86_BUILTIN_COMILTSS,
13893   IX86_BUILTIN_COMILESS,
13894   IX86_BUILTIN_COMIGTSS,
13895   IX86_BUILTIN_COMIGESS,
13896   IX86_BUILTIN_COMINEQSS,
13897   IX86_BUILTIN_UCOMIEQSS,
13898   IX86_BUILTIN_UCOMILTSS,
13899   IX86_BUILTIN_UCOMILESS,
13900   IX86_BUILTIN_UCOMIGTSS,
13901   IX86_BUILTIN_UCOMIGESS,
13902   IX86_BUILTIN_UCOMINEQSS,
13903
13904   IX86_BUILTIN_CVTPI2PS,
13905   IX86_BUILTIN_CVTPS2PI,
13906   IX86_BUILTIN_CVTSI2SS,
13907   IX86_BUILTIN_CVTSI642SS,
13908   IX86_BUILTIN_CVTSS2SI,
13909   IX86_BUILTIN_CVTSS2SI64,
13910   IX86_BUILTIN_CVTTPS2PI,
13911   IX86_BUILTIN_CVTTSS2SI,
13912   IX86_BUILTIN_CVTTSS2SI64,
13913
13914   IX86_BUILTIN_MAXPS,
13915   IX86_BUILTIN_MAXSS,
13916   IX86_BUILTIN_MINPS,
13917   IX86_BUILTIN_MINSS,
13918
13919   IX86_BUILTIN_LOADUPS,
13920   IX86_BUILTIN_STOREUPS,
13921   IX86_BUILTIN_MOVSS,
13922
13923   IX86_BUILTIN_MOVHLPS,
13924   IX86_BUILTIN_MOVLHPS,
13925   IX86_BUILTIN_LOADHPS,
13926   IX86_BUILTIN_LOADLPS,
13927   IX86_BUILTIN_STOREHPS,
13928   IX86_BUILTIN_STORELPS,
13929
13930   IX86_BUILTIN_MASKMOVQ,
13931   IX86_BUILTIN_MOVMSKPS,
13932   IX86_BUILTIN_PMOVMSKB,
13933
13934   IX86_BUILTIN_MOVNTPS,
13935   IX86_BUILTIN_MOVNTQ,
13936
13937   IX86_BUILTIN_LOADDQU,
13938   IX86_BUILTIN_STOREDQU,
13939
13940   IX86_BUILTIN_PACKSSWB,
13941   IX86_BUILTIN_PACKSSDW,
13942   IX86_BUILTIN_PACKUSWB,
13943
13944   IX86_BUILTIN_PADDB,
13945   IX86_BUILTIN_PADDW,
13946   IX86_BUILTIN_PADDD,
13947   IX86_BUILTIN_PADDQ,
13948   IX86_BUILTIN_PADDSB,
13949   IX86_BUILTIN_PADDSW,
13950   IX86_BUILTIN_PADDUSB,
13951   IX86_BUILTIN_PADDUSW,
13952   IX86_BUILTIN_PSUBB,
13953   IX86_BUILTIN_PSUBW,
13954   IX86_BUILTIN_PSUBD,
13955   IX86_BUILTIN_PSUBQ,
13956   IX86_BUILTIN_PSUBSB,
13957   IX86_BUILTIN_PSUBSW,
13958   IX86_BUILTIN_PSUBUSB,
13959   IX86_BUILTIN_PSUBUSW,
13960
13961   IX86_BUILTIN_PAND,
13962   IX86_BUILTIN_PANDN,
13963   IX86_BUILTIN_POR,
13964   IX86_BUILTIN_PXOR,
13965
13966   IX86_BUILTIN_PAVGB,
13967   IX86_BUILTIN_PAVGW,
13968
13969   IX86_BUILTIN_PCMPEQB,
13970   IX86_BUILTIN_PCMPEQW,
13971   IX86_BUILTIN_PCMPEQD,
13972   IX86_BUILTIN_PCMPGTB,
13973   IX86_BUILTIN_PCMPGTW,
13974   IX86_BUILTIN_PCMPGTD,
13975
13976   IX86_BUILTIN_PMADDWD,
13977
13978   IX86_BUILTIN_PMAXSW,
13979   IX86_BUILTIN_PMAXUB,
13980   IX86_BUILTIN_PMINSW,
13981   IX86_BUILTIN_PMINUB,
13982
13983   IX86_BUILTIN_PMULHUW,
13984   IX86_BUILTIN_PMULHW,
13985   IX86_BUILTIN_PMULLW,
13986
13987   IX86_BUILTIN_PSADBW,
13988   IX86_BUILTIN_PSHUFW,
13989
13990   IX86_BUILTIN_PSLLW,
13991   IX86_BUILTIN_PSLLD,
13992   IX86_BUILTIN_PSLLQ,
13993   IX86_BUILTIN_PSRAW,
13994   IX86_BUILTIN_PSRAD,
13995   IX86_BUILTIN_PSRLW,
13996   IX86_BUILTIN_PSRLD,
13997   IX86_BUILTIN_PSRLQ,
13998   IX86_BUILTIN_PSLLWI,
13999   IX86_BUILTIN_PSLLDI,
14000   IX86_BUILTIN_PSLLQI,
14001   IX86_BUILTIN_PSRAWI,
14002   IX86_BUILTIN_PSRADI,
14003   IX86_BUILTIN_PSRLWI,
14004   IX86_BUILTIN_PSRLDI,
14005   IX86_BUILTIN_PSRLQI,
14006
14007   IX86_BUILTIN_PUNPCKHBW,
14008   IX86_BUILTIN_PUNPCKHWD,
14009   IX86_BUILTIN_PUNPCKHDQ,
14010   IX86_BUILTIN_PUNPCKLBW,
14011   IX86_BUILTIN_PUNPCKLWD,
14012   IX86_BUILTIN_PUNPCKLDQ,
14013
14014   IX86_BUILTIN_SHUFPS,
14015
14016   IX86_BUILTIN_RCPPS,
14017   IX86_BUILTIN_RCPSS,
14018   IX86_BUILTIN_RSQRTPS,
14019   IX86_BUILTIN_RSQRTSS,
14020   IX86_BUILTIN_SQRTPS,
14021   IX86_BUILTIN_SQRTSS,
14022
14023   IX86_BUILTIN_UNPCKHPS,
14024   IX86_BUILTIN_UNPCKLPS,
14025
14026   IX86_BUILTIN_ANDPS,
14027   IX86_BUILTIN_ANDNPS,
14028   IX86_BUILTIN_ORPS,
14029   IX86_BUILTIN_XORPS,
14030
14031   IX86_BUILTIN_EMMS,
14032   IX86_BUILTIN_LDMXCSR,
14033   IX86_BUILTIN_STMXCSR,
14034   IX86_BUILTIN_SFENCE,
14035
14036   /* 3DNow! Original */
14037   IX86_BUILTIN_FEMMS,
14038   IX86_BUILTIN_PAVGUSB,
14039   IX86_BUILTIN_PF2ID,
14040   IX86_BUILTIN_PFACC,
14041   IX86_BUILTIN_PFADD,
14042   IX86_BUILTIN_PFCMPEQ,
14043   IX86_BUILTIN_PFCMPGE,
14044   IX86_BUILTIN_PFCMPGT,
14045   IX86_BUILTIN_PFMAX,
14046   IX86_BUILTIN_PFMIN,
14047   IX86_BUILTIN_PFMUL,
14048   IX86_BUILTIN_PFRCP,
14049   IX86_BUILTIN_PFRCPIT1,
14050   IX86_BUILTIN_PFRCPIT2,
14051   IX86_BUILTIN_PFRSQIT1,
14052   IX86_BUILTIN_PFRSQRT,
14053   IX86_BUILTIN_PFSUB,
14054   IX86_BUILTIN_PFSUBR,
14055   IX86_BUILTIN_PI2FD,
14056   IX86_BUILTIN_PMULHRW,
14057
14058   /* 3DNow! Athlon Extensions */
14059   IX86_BUILTIN_PF2IW,
14060   IX86_BUILTIN_PFNACC,
14061   IX86_BUILTIN_PFPNACC,
14062   IX86_BUILTIN_PI2FW,
14063   IX86_BUILTIN_PSWAPDSI,
14064   IX86_BUILTIN_PSWAPDSF,
14065
14066   /* SSE2 */
14067   IX86_BUILTIN_ADDPD,
14068   IX86_BUILTIN_ADDSD,
14069   IX86_BUILTIN_DIVPD,
14070   IX86_BUILTIN_DIVSD,
14071   IX86_BUILTIN_MULPD,
14072   IX86_BUILTIN_MULSD,
14073   IX86_BUILTIN_SUBPD,
14074   IX86_BUILTIN_SUBSD,
14075
14076   IX86_BUILTIN_CMPEQPD,
14077   IX86_BUILTIN_CMPLTPD,
14078   IX86_BUILTIN_CMPLEPD,
14079   IX86_BUILTIN_CMPGTPD,
14080   IX86_BUILTIN_CMPGEPD,
14081   IX86_BUILTIN_CMPNEQPD,
14082   IX86_BUILTIN_CMPNLTPD,
14083   IX86_BUILTIN_CMPNLEPD,
14084   IX86_BUILTIN_CMPNGTPD,
14085   IX86_BUILTIN_CMPNGEPD,
14086   IX86_BUILTIN_CMPORDPD,
14087   IX86_BUILTIN_CMPUNORDPD,
14088   IX86_BUILTIN_CMPNEPD,
14089   IX86_BUILTIN_CMPEQSD,
14090   IX86_BUILTIN_CMPLTSD,
14091   IX86_BUILTIN_CMPLESD,
14092   IX86_BUILTIN_CMPNEQSD,
14093   IX86_BUILTIN_CMPNLTSD,
14094   IX86_BUILTIN_CMPNLESD,
14095   IX86_BUILTIN_CMPORDSD,
14096   IX86_BUILTIN_CMPUNORDSD,
14097   IX86_BUILTIN_CMPNESD,
14098
14099   IX86_BUILTIN_COMIEQSD,
14100   IX86_BUILTIN_COMILTSD,
14101   IX86_BUILTIN_COMILESD,
14102   IX86_BUILTIN_COMIGTSD,
14103   IX86_BUILTIN_COMIGESD,
14104   IX86_BUILTIN_COMINEQSD,
14105   IX86_BUILTIN_UCOMIEQSD,
14106   IX86_BUILTIN_UCOMILTSD,
14107   IX86_BUILTIN_UCOMILESD,
14108   IX86_BUILTIN_UCOMIGTSD,
14109   IX86_BUILTIN_UCOMIGESD,
14110   IX86_BUILTIN_UCOMINEQSD,
14111
14112   IX86_BUILTIN_MAXPD,
14113   IX86_BUILTIN_MAXSD,
14114   IX86_BUILTIN_MINPD,
14115   IX86_BUILTIN_MINSD,
14116
14117   IX86_BUILTIN_ANDPD,
14118   IX86_BUILTIN_ANDNPD,
14119   IX86_BUILTIN_ORPD,
14120   IX86_BUILTIN_XORPD,
14121
14122   IX86_BUILTIN_SQRTPD,
14123   IX86_BUILTIN_SQRTSD,
14124
14125   IX86_BUILTIN_UNPCKHPD,
14126   IX86_BUILTIN_UNPCKLPD,
14127
14128   IX86_BUILTIN_SHUFPD,
14129
14130   IX86_BUILTIN_LOADUPD,
14131   IX86_BUILTIN_STOREUPD,
14132   IX86_BUILTIN_MOVSD,
14133
14134   IX86_BUILTIN_LOADHPD,
14135   IX86_BUILTIN_LOADLPD,
14136
14137   IX86_BUILTIN_CVTDQ2PD,
14138   IX86_BUILTIN_CVTDQ2PS,
14139
14140   IX86_BUILTIN_CVTPD2DQ,
14141   IX86_BUILTIN_CVTPD2PI,
14142   IX86_BUILTIN_CVTPD2PS,
14143   IX86_BUILTIN_CVTTPD2DQ,
14144   IX86_BUILTIN_CVTTPD2PI,
14145
14146   IX86_BUILTIN_CVTPI2PD,
14147   IX86_BUILTIN_CVTSI2SD,
14148   IX86_BUILTIN_CVTSI642SD,
14149
14150   IX86_BUILTIN_CVTSD2SI,
14151   IX86_BUILTIN_CVTSD2SI64,
14152   IX86_BUILTIN_CVTSD2SS,
14153   IX86_BUILTIN_CVTSS2SD,
14154   IX86_BUILTIN_CVTTSD2SI,
14155   IX86_BUILTIN_CVTTSD2SI64,
14156
14157   IX86_BUILTIN_CVTPS2DQ,
14158   IX86_BUILTIN_CVTPS2PD,
14159   IX86_BUILTIN_CVTTPS2DQ,
14160
14161   IX86_BUILTIN_MOVNTI,
14162   IX86_BUILTIN_MOVNTPD,
14163   IX86_BUILTIN_MOVNTDQ,
14164
14165   /* SSE2 MMX */
14166   IX86_BUILTIN_MASKMOVDQU,
14167   IX86_BUILTIN_MOVMSKPD,
14168   IX86_BUILTIN_PMOVMSKB128,
14169
14170   IX86_BUILTIN_PACKSSWB128,
14171   IX86_BUILTIN_PACKSSDW128,
14172   IX86_BUILTIN_PACKUSWB128,
14173
14174   IX86_BUILTIN_PADDB128,
14175   IX86_BUILTIN_PADDW128,
14176   IX86_BUILTIN_PADDD128,
14177   IX86_BUILTIN_PADDQ128,
14178   IX86_BUILTIN_PADDSB128,
14179   IX86_BUILTIN_PADDSW128,
14180   IX86_BUILTIN_PADDUSB128,
14181   IX86_BUILTIN_PADDUSW128,
14182   IX86_BUILTIN_PSUBB128,
14183   IX86_BUILTIN_PSUBW128,
14184   IX86_BUILTIN_PSUBD128,
14185   IX86_BUILTIN_PSUBQ128,
14186   IX86_BUILTIN_PSUBSB128,
14187   IX86_BUILTIN_PSUBSW128,
14188   IX86_BUILTIN_PSUBUSB128,
14189   IX86_BUILTIN_PSUBUSW128,
14190
14191   IX86_BUILTIN_PAND128,
14192   IX86_BUILTIN_PANDN128,
14193   IX86_BUILTIN_POR128,
14194   IX86_BUILTIN_PXOR128,
14195
14196   IX86_BUILTIN_PAVGB128,
14197   IX86_BUILTIN_PAVGW128,
14198
14199   IX86_BUILTIN_PCMPEQB128,
14200   IX86_BUILTIN_PCMPEQW128,
14201   IX86_BUILTIN_PCMPEQD128,
14202   IX86_BUILTIN_PCMPGTB128,
14203   IX86_BUILTIN_PCMPGTW128,
14204   IX86_BUILTIN_PCMPGTD128,
14205
14206   IX86_BUILTIN_PMADDWD128,
14207
14208   IX86_BUILTIN_PMAXSW128,
14209   IX86_BUILTIN_PMAXUB128,
14210   IX86_BUILTIN_PMINSW128,
14211   IX86_BUILTIN_PMINUB128,
14212
14213   IX86_BUILTIN_PMULUDQ,
14214   IX86_BUILTIN_PMULUDQ128,
14215   IX86_BUILTIN_PMULHUW128,
14216   IX86_BUILTIN_PMULHW128,
14217   IX86_BUILTIN_PMULLW128,
14218
14219   IX86_BUILTIN_PSADBW128,
14220   IX86_BUILTIN_PSHUFHW,
14221   IX86_BUILTIN_PSHUFLW,
14222   IX86_BUILTIN_PSHUFD,
14223
14224   IX86_BUILTIN_PSLLW128,
14225   IX86_BUILTIN_PSLLD128,
14226   IX86_BUILTIN_PSLLQ128,
14227   IX86_BUILTIN_PSRAW128,
14228   IX86_BUILTIN_PSRAD128,
14229   IX86_BUILTIN_PSRLW128,
14230   IX86_BUILTIN_PSRLD128,
14231   IX86_BUILTIN_PSRLQ128,
14232   IX86_BUILTIN_PSLLDQI128,
14233   IX86_BUILTIN_PSLLWI128,
14234   IX86_BUILTIN_PSLLDI128,
14235   IX86_BUILTIN_PSLLQI128,
14236   IX86_BUILTIN_PSRAWI128,
14237   IX86_BUILTIN_PSRADI128,
14238   IX86_BUILTIN_PSRLDQI128,
14239   IX86_BUILTIN_PSRLWI128,
14240   IX86_BUILTIN_PSRLDI128,
14241   IX86_BUILTIN_PSRLQI128,
14242
14243   IX86_BUILTIN_PUNPCKHBW128,
14244   IX86_BUILTIN_PUNPCKHWD128,
14245   IX86_BUILTIN_PUNPCKHDQ128,
14246   IX86_BUILTIN_PUNPCKHQDQ128,
14247   IX86_BUILTIN_PUNPCKLBW128,
14248   IX86_BUILTIN_PUNPCKLWD128,
14249   IX86_BUILTIN_PUNPCKLDQ128,
14250   IX86_BUILTIN_PUNPCKLQDQ128,
14251
14252   IX86_BUILTIN_CLFLUSH,
14253   IX86_BUILTIN_MFENCE,
14254   IX86_BUILTIN_LFENCE,
14255
14256   /* Prescott New Instructions.  */
14257   IX86_BUILTIN_ADDSUBPS,
14258   IX86_BUILTIN_HADDPS,
14259   IX86_BUILTIN_HSUBPS,
14260   IX86_BUILTIN_MOVSHDUP,
14261   IX86_BUILTIN_MOVSLDUP,
14262   IX86_BUILTIN_ADDSUBPD,
14263   IX86_BUILTIN_HADDPD,
14264   IX86_BUILTIN_HSUBPD,
14265   IX86_BUILTIN_LDDQU,
14266
14267   IX86_BUILTIN_MONITOR,
14268   IX86_BUILTIN_MWAIT,
14269
14270   IX86_BUILTIN_VEC_INIT_V2SI,
14271   IX86_BUILTIN_VEC_INIT_V4HI,
14272   IX86_BUILTIN_VEC_INIT_V8QI,
14273   IX86_BUILTIN_VEC_EXT_V2DF,
14274   IX86_BUILTIN_VEC_EXT_V2DI,
14275   IX86_BUILTIN_VEC_EXT_V4SF,
14276   IX86_BUILTIN_VEC_EXT_V4SI,
14277   IX86_BUILTIN_VEC_EXT_V8HI,
14278   IX86_BUILTIN_VEC_EXT_V2SI,
14279   IX86_BUILTIN_VEC_EXT_V4HI,
14280   IX86_BUILTIN_VEC_SET_V8HI,
14281   IX86_BUILTIN_VEC_SET_V4HI,
14282
14283   /* SSE2 ABI functions.  */
14284   IX86_BUILTIN_SSE2_ACOS,
14285   IX86_BUILTIN_SSE2_ACOSF,
14286   IX86_BUILTIN_SSE2_ASIN,
14287   IX86_BUILTIN_SSE2_ASINF,
14288   IX86_BUILTIN_SSE2_ATAN,
14289   IX86_BUILTIN_SSE2_ATANF,
14290   IX86_BUILTIN_SSE2_ATAN2,
14291   IX86_BUILTIN_SSE2_ATAN2F,
14292   IX86_BUILTIN_SSE2_COS,
14293   IX86_BUILTIN_SSE2_COSF,
14294   IX86_BUILTIN_SSE2_EXP,
14295   IX86_BUILTIN_SSE2_EXPF,
14296   IX86_BUILTIN_SSE2_LOG10,
14297   IX86_BUILTIN_SSE2_LOG10F,
14298   IX86_BUILTIN_SSE2_LOG,
14299   IX86_BUILTIN_SSE2_LOGF,
14300   IX86_BUILTIN_SSE2_SIN,
14301   IX86_BUILTIN_SSE2_SINF,
14302   IX86_BUILTIN_SSE2_TAN,
14303   IX86_BUILTIN_SSE2_TANF,
14304
14305   IX86_BUILTIN_MAX
14306 };
14307
14308 #define def_builtin(MASK, NAME, TYPE, CODE)                             \
14309 do {                                                                    \
14310   if ((MASK) & target_flags                                             \
14311       && (!((MASK) & MASK_64BIT) || TARGET_64BIT))                      \
14312     lang_hooks.builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,   \
14313                                  NULL, NULL_TREE);                      \
14314 } while (0)
14315
14316 /* Bits for builtin_description.flag.  */
14317
14318 /* Set when we don't support the comparison natively, and should
14319    swap_comparison in order to support it.  */
14320 #define BUILTIN_DESC_SWAP_OPERANDS      1
14321
14322 struct builtin_description
14323 {
14324   const unsigned int mask;
14325   const enum insn_code icode;
14326   const char *const name;
14327   const enum ix86_builtins code;
14328   const enum rtx_code comparison;
14329   const unsigned int flag;
14330 };
14331
14332 static const struct builtin_description bdesc_comi[] =
14333 {
14334   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
14335   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
14336   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
14337   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
14338   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
14339   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
14340   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
14341   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
14342   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
14343   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
14344   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
14345   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
14346   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
14347   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
14348   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
14349   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
14350   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
14351   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
14352   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
14353   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
14354   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
14355   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
14356   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
14357   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
14358 };
14359
14360 static const struct builtin_description bdesc_2arg[] =
14361 {
14362   /* SSE */
14363   { MASK_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, 0, 0 },
14364   { MASK_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, 0, 0 },
14365   { MASK_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, 0, 0 },
14366   { MASK_SSE, CODE_FOR_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, 0, 0 },
14367   { MASK_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, 0, 0 },
14368   { MASK_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, 0, 0 },
14369   { MASK_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, 0, 0 },
14370   { MASK_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, 0, 0 },
14371
14372   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, 0 },
14373   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, 0 },
14374   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, 0 },
14375   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT,
14376     BUILTIN_DESC_SWAP_OPERANDS },
14377   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE,
14378     BUILTIN_DESC_SWAP_OPERANDS },
14379   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, 0 },
14380   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, 0 },
14381   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, 0 },
14382   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, 0 },
14383   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE,
14384     BUILTIN_DESC_SWAP_OPERANDS },
14385   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT,
14386     BUILTIN_DESC_SWAP_OPERANDS },
14387   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, 0 },
14388   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, 0 },
14389   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, 0 },
14390   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, 0 },
14391   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, 0 },
14392   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, 0 },
14393   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, 0 },
14394   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, 0 },
14395   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE,
14396     BUILTIN_DESC_SWAP_OPERANDS },
14397   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT,
14398     BUILTIN_DESC_SWAP_OPERANDS },
14399   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, UNORDERED, 0 },
14400
14401   { MASK_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, 0, 0 },
14402   { MASK_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, 0, 0 },
14403   { MASK_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, 0, 0 },
14404   { MASK_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, 0, 0 },
14405
14406   { MASK_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, 0, 0 },
14407   { MASK_SSE, CODE_FOR_sse_nandv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, 0, 0 },
14408   { MASK_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, 0, 0 },
14409   { MASK_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, 0, 0 },
14410
14411   { MASK_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, 0, 0 },
14412   { MASK_SSE, CODE_FOR_sse_movhlps,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, 0, 0 },
14413   { MASK_SSE, CODE_FOR_sse_movlhps,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, 0, 0 },
14414   { MASK_SSE, CODE_FOR_sse_unpckhps, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, 0, 0 },
14415   { MASK_SSE, CODE_FOR_sse_unpcklps, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, 0, 0 },
14416
14417   /* MMX */
14418   { MASK_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, 0, 0 },
14419   { MASK_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, 0, 0 },
14420   { MASK_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, 0, 0 },
14421   { MASK_SSE2, CODE_FOR_mmx_adddi3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, 0, 0 },
14422   { MASK_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, 0, 0 },
14423   { MASK_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, 0, 0 },
14424   { MASK_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, 0, 0 },
14425   { MASK_SSE2, CODE_FOR_mmx_subdi3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, 0, 0 },
14426
14427   { MASK_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, 0, 0 },
14428   { MASK_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, 0, 0 },
14429   { MASK_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, 0, 0 },
14430   { MASK_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, 0, 0 },
14431   { MASK_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, 0, 0 },
14432   { MASK_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, 0, 0 },
14433   { MASK_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, 0, 0 },
14434   { MASK_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, 0, 0 },
14435
14436   { MASK_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, 0, 0 },
14437   { MASK_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, 0, 0 },
14438   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, 0, 0 },
14439
14440   { MASK_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, 0, 0 },
14441   { MASK_MMX, CODE_FOR_mmx_nandv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, 0, 0 },
14442   { MASK_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, 0, 0 },
14443   { MASK_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, 0, 0 },
14444
14445   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, 0, 0 },
14446   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, 0, 0 },
14447
14448   { MASK_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, 0, 0 },
14449   { MASK_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, 0, 0 },
14450   { MASK_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, 0, 0 },
14451   { MASK_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, 0, 0 },
14452   { MASK_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, 0, 0 },
14453   { MASK_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, 0, 0 },
14454
14455   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, 0, 0 },
14456   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, 0, 0 },
14457   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, 0, 0 },
14458   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, 0, 0 },
14459
14460   { MASK_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, 0, 0 },
14461   { MASK_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, 0, 0 },
14462   { MASK_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, 0, 0 },
14463   { MASK_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, 0, 0 },
14464   { MASK_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, 0, 0 },
14465   { MASK_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, 0, 0 },
14466
14467   /* Special.  */
14468   { MASK_MMX, CODE_FOR_mmx_packsswb, 0, IX86_BUILTIN_PACKSSWB, 0, 0 },
14469   { MASK_MMX, CODE_FOR_mmx_packssdw, 0, IX86_BUILTIN_PACKSSDW, 0, 0 },
14470   { MASK_MMX, CODE_FOR_mmx_packuswb, 0, IX86_BUILTIN_PACKUSWB, 0, 0 },
14471
14472   { MASK_SSE, CODE_FOR_sse_cvtpi2ps, 0, IX86_BUILTIN_CVTPI2PS, 0, 0 },
14473   { MASK_SSE, CODE_FOR_sse_cvtsi2ss, 0, IX86_BUILTIN_CVTSI2SS, 0, 0 },
14474   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtsi2ssq, 0, IX86_BUILTIN_CVTSI642SS, 0, 0 },
14475
14476   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLW, 0, 0 },
14477   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLWI, 0, 0 },
14478   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLD, 0, 0 },
14479   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLDI, 0, 0 },
14480   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQ, 0, 0 },
14481   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQI, 0, 0 },
14482
14483   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLW, 0, 0 },
14484   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLWI, 0, 0 },
14485   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLD, 0, 0 },
14486   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLDI, 0, 0 },
14487   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQ, 0, 0 },
14488   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQI, 0, 0 },
14489
14490   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAW, 0, 0 },
14491   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAWI, 0, 0 },
14492   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRAD, 0, 0 },
14493   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRADI, 0, 0 },
14494
14495   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_psadbw, 0, IX86_BUILTIN_PSADBW, 0, 0 },
14496   { MASK_MMX, CODE_FOR_mmx_pmaddwd, 0, IX86_BUILTIN_PMADDWD, 0, 0 },
14497
14498   /* SSE2 */
14499   { MASK_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, 0, 0 },
14500   { MASK_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, 0, 0 },
14501   { MASK_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, 0, 0 },
14502   { MASK_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, 0, 0 },
14503   { MASK_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, 0, 0 },
14504   { MASK_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, 0, 0 },
14505   { MASK_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, 0, 0 },
14506   { MASK_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, 0, 0 },
14507
14508   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, 0 },
14509   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, 0 },
14510   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, 0 },
14511   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT,
14512     BUILTIN_DESC_SWAP_OPERANDS },
14513   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE,
14514     BUILTIN_DESC_SWAP_OPERANDS },
14515   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, 0 },
14516   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, 0 },
14517   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, 0 },
14518   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, 0 },
14519   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE,
14520     BUILTIN_DESC_SWAP_OPERANDS },
14521   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT,
14522     BUILTIN_DESC_SWAP_OPERANDS },
14523   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, 0 },
14524   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, 0 },
14525   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, 0 },
14526   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, 0 },
14527   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, 0 },
14528   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, 0 },
14529   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, 0 },
14530   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, 0 },
14531   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, 0 },
14532
14533   { MASK_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, 0, 0 },
14534   { MASK_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, 0, 0 },
14535   { MASK_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, 0, 0 },
14536   { MASK_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, 0, 0 },
14537
14538   { MASK_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, 0, 0 },
14539   { MASK_SSE2, CODE_FOR_sse2_nandv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, 0, 0 },
14540   { MASK_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, 0, 0 },
14541   { MASK_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, 0, 0 },
14542
14543   { MASK_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, 0, 0 },
14544   { MASK_SSE2, CODE_FOR_sse2_unpckhpd, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, 0, 0 },
14545   { MASK_SSE2, CODE_FOR_sse2_unpcklpd, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, 0, 0 },
14546
14547   /* SSE2 MMX */
14548   { MASK_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, 0, 0 },
14549   { MASK_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, 0, 0 },
14550   { MASK_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, 0, 0 },
14551   { MASK_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, 0, 0 },
14552   { MASK_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, 0, 0 },
14553   { MASK_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, 0, 0 },
14554   { MASK_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, 0, 0 },
14555   { MASK_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, 0, 0 },
14556
14557   { MASK_MMX, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, 0, 0 },
14558   { MASK_MMX, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, 0, 0 },
14559   { MASK_MMX, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, 0, 0 },
14560   { MASK_MMX, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, 0, 0 },
14561   { MASK_MMX, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, 0, 0 },
14562   { MASK_MMX, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, 0, 0 },
14563   { MASK_MMX, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, 0, 0 },
14564   { MASK_MMX, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, 0, 0 },
14565
14566   { MASK_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, 0, 0 },
14567   { MASK_SSE2, CODE_FOR_sse2_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, 0, 0 },
14568
14569   { MASK_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, 0, 0 },
14570   { MASK_SSE2, CODE_FOR_sse2_nandv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, 0, 0 },
14571   { MASK_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, 0, 0 },
14572   { MASK_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, 0, 0 },
14573
14574   { MASK_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, 0, 0 },
14575   { MASK_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, 0, 0 },
14576
14577   { MASK_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, 0, 0 },
14578   { MASK_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, 0, 0 },
14579   { MASK_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, 0, 0 },
14580   { MASK_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, 0, 0 },
14581   { MASK_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, 0, 0 },
14582   { MASK_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, 0, 0 },
14583
14584   { MASK_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, 0, 0 },
14585   { MASK_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, 0, 0 },
14586   { MASK_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, 0, 0 },
14587   { MASK_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, 0, 0 },
14588
14589   { MASK_SSE2, CODE_FOR_sse2_punpckhbw, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, 0, 0 },
14590   { MASK_SSE2, CODE_FOR_sse2_punpckhwd, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, 0, 0 },
14591   { MASK_SSE2, CODE_FOR_sse2_punpckhdq, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, 0, 0 },
14592   { MASK_SSE2, CODE_FOR_sse2_punpckhqdq, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, 0, 0 },
14593   { MASK_SSE2, CODE_FOR_sse2_punpcklbw, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, 0, 0 },
14594   { MASK_SSE2, CODE_FOR_sse2_punpcklwd, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, 0, 0 },
14595   { MASK_SSE2, CODE_FOR_sse2_punpckldq, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, 0, 0 },
14596   { MASK_SSE2, CODE_FOR_sse2_punpcklqdq, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, 0, 0 },
14597
14598   { MASK_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, 0, 0 },
14599   { MASK_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, 0, 0 },
14600   { MASK_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, 0, 0 },
14601
14602   { MASK_SSE2, CODE_FOR_sse2_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, 0, 0 },
14603   { MASK_SSE2, CODE_FOR_sse2_psadbw, 0, IX86_BUILTIN_PSADBW128, 0, 0 },
14604
14605   { MASK_SSE2, CODE_FOR_sse2_umulsidi3, 0, IX86_BUILTIN_PMULUDQ, 0, 0 },
14606   { MASK_SSE2, CODE_FOR_sse2_umulv2siv2di3, 0, IX86_BUILTIN_PMULUDQ128, 0, 0 },
14607
14608   { MASK_SSE2, CODE_FOR_ashlv8hi3, 0, IX86_BUILTIN_PSLLWI128, 0, 0 },
14609   { MASK_SSE2, CODE_FOR_ashlv4si3, 0, IX86_BUILTIN_PSLLDI128, 0, 0 },
14610   { MASK_SSE2, CODE_FOR_ashlv2di3, 0, IX86_BUILTIN_PSLLQI128, 0, 0 },
14611
14612   { MASK_SSE2, CODE_FOR_lshrv8hi3, 0, IX86_BUILTIN_PSRLWI128, 0, 0 },
14613   { MASK_SSE2, CODE_FOR_lshrv4si3, 0, IX86_BUILTIN_PSRLDI128, 0, 0 },
14614   { MASK_SSE2, CODE_FOR_lshrv2di3, 0, IX86_BUILTIN_PSRLQI128, 0, 0 },
14615
14616   { MASK_SSE2, CODE_FOR_ashrv8hi3, 0, IX86_BUILTIN_PSRAWI128, 0, 0 },
14617   { MASK_SSE2, CODE_FOR_ashrv4si3, 0, IX86_BUILTIN_PSRADI128, 0, 0 },
14618
14619   { MASK_SSE2, CODE_FOR_sse2_pmaddwd, 0, IX86_BUILTIN_PMADDWD128, 0, 0 },
14620
14621   { MASK_SSE2, CODE_FOR_sse2_cvtsi2sd, 0, IX86_BUILTIN_CVTSI2SD, 0, 0 },
14622   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsi2sdq, 0, IX86_BUILTIN_CVTSI642SD, 0, 0 },
14623   { MASK_SSE2, CODE_FOR_sse2_cvtsd2ss, 0, IX86_BUILTIN_CVTSD2SS, 0, 0 },
14624   { MASK_SSE2, CODE_FOR_sse2_cvtss2sd, 0, IX86_BUILTIN_CVTSS2SD, 0, 0 },
14625
14626   /* SSE3 MMX */
14627   { MASK_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, 0, 0 },
14628   { MASK_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, 0, 0 },
14629   { MASK_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, 0, 0 },
14630   { MASK_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, 0, 0 },
14631   { MASK_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, 0, 0 },
14632   { MASK_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, 0, 0 }
14633 };
14634
14635 static const struct builtin_description bdesc_1arg[] =
14636 {
14637   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB, 0, 0 },
14638   { MASK_SSE, CODE_FOR_sse_movmskps, 0, IX86_BUILTIN_MOVMSKPS, 0, 0 },
14639
14640   { MASK_SSE, CODE_FOR_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS, 0, 0 },
14641   { MASK_SSE, CODE_FOR_sse_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS, 0, 0 },
14642   { MASK_SSE, CODE_FOR_sse_rcpv4sf2, 0, IX86_BUILTIN_RCPPS, 0, 0 },
14643
14644   { MASK_SSE, CODE_FOR_sse_cvtps2pi, 0, IX86_BUILTIN_CVTPS2PI, 0, 0 },
14645   { MASK_SSE, CODE_FOR_sse_cvtss2si, 0, IX86_BUILTIN_CVTSS2SI, 0, 0 },
14646   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtss2siq, 0, IX86_BUILTIN_CVTSS2SI64, 0, 0 },
14647   { MASK_SSE, CODE_FOR_sse_cvttps2pi, 0, IX86_BUILTIN_CVTTPS2PI, 0, 0 },
14648   { MASK_SSE, CODE_FOR_sse_cvttss2si, 0, IX86_BUILTIN_CVTTSS2SI, 0, 0 },
14649   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvttss2siq, 0, IX86_BUILTIN_CVTTSS2SI64, 0, 0 },
14650
14651   { MASK_SSE2, CODE_FOR_sse2_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB128, 0, 0 },
14652   { MASK_SSE2, CODE_FOR_sse2_movmskpd, 0, IX86_BUILTIN_MOVMSKPD, 0, 0 },
14653
14654   { MASK_SSE2, CODE_FOR_sqrtv2df2, 0, IX86_BUILTIN_SQRTPD, 0, 0 },
14655
14656   { MASK_SSE2, CODE_FOR_sse2_cvtdq2pd, 0, IX86_BUILTIN_CVTDQ2PD, 0, 0 },
14657   { MASK_SSE2, CODE_FOR_sse2_cvtdq2ps, 0, IX86_BUILTIN_CVTDQ2PS, 0, 0 },
14658
14659   { MASK_SSE2, CODE_FOR_sse2_cvtpd2dq, 0, IX86_BUILTIN_CVTPD2DQ, 0, 0 },
14660   { MASK_SSE2, CODE_FOR_sse2_cvtpd2pi, 0, IX86_BUILTIN_CVTPD2PI, 0, 0 },
14661   { MASK_SSE2, CODE_FOR_sse2_cvtpd2ps, 0, IX86_BUILTIN_CVTPD2PS, 0, 0 },
14662   { MASK_SSE2, CODE_FOR_sse2_cvttpd2dq, 0, IX86_BUILTIN_CVTTPD2DQ, 0, 0 },
14663   { MASK_SSE2, CODE_FOR_sse2_cvttpd2pi, 0, IX86_BUILTIN_CVTTPD2PI, 0, 0 },
14664
14665   { MASK_SSE2, CODE_FOR_sse2_cvtpi2pd, 0, IX86_BUILTIN_CVTPI2PD, 0, 0 },
14666
14667   { MASK_SSE2, CODE_FOR_sse2_cvtsd2si, 0, IX86_BUILTIN_CVTSD2SI, 0, 0 },
14668   { MASK_SSE2, CODE_FOR_sse2_cvttsd2si, 0, IX86_BUILTIN_CVTTSD2SI, 0, 0 },
14669   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsd2siq, 0, IX86_BUILTIN_CVTSD2SI64, 0, 0 },
14670   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvttsd2siq, 0, IX86_BUILTIN_CVTTSD2SI64, 0, 0 },
14671
14672   { MASK_SSE2, CODE_FOR_sse2_cvtps2dq, 0, IX86_BUILTIN_CVTPS2DQ, 0, 0 },
14673   { MASK_SSE2, CODE_FOR_sse2_cvtps2pd, 0, IX86_BUILTIN_CVTPS2PD, 0, 0 },
14674   { MASK_SSE2, CODE_FOR_sse2_cvttps2dq, 0, IX86_BUILTIN_CVTTPS2DQ, 0, 0 },
14675
14676   /* SSE3 */
14677   { MASK_SSE3, CODE_FOR_sse3_movshdup, 0, IX86_BUILTIN_MOVSHDUP, 0, 0 },
14678   { MASK_SSE3, CODE_FOR_sse3_movsldup, 0, IX86_BUILTIN_MOVSLDUP, 0, 0 },
14679 };
14680
14681 static void
14682 ix86_init_builtins (void)
14683 {
14684   if (TARGET_MMX)
14685     ix86_init_mmx_sse_builtins ();
14686   if (TARGET_SSE2)
14687     ix86_init_sse_abi_builtins ();
14688 }
14689
14690 /* Set up all the MMX/SSE builtins.  This is not called if TARGET_MMX
14691    is zero.  Otherwise, if TARGET_SSE is not set, only expand the MMX
14692    builtins.  */
14693 static void
14694 ix86_init_mmx_sse_builtins (void)
14695 {
14696   const struct builtin_description * d;
14697   size_t i;
14698
14699   tree V16QI_type_node = build_vector_type_for_mode (intQI_type_node, V16QImode);
14700   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
14701   tree V2SF_type_node = build_vector_type_for_mode (float_type_node, V2SFmode);
14702   tree V2DI_type_node
14703     = build_vector_type_for_mode (long_long_integer_type_node, V2DImode);
14704   tree V2DF_type_node = build_vector_type_for_mode (double_type_node, V2DFmode);
14705   tree V4SF_type_node = build_vector_type_for_mode (float_type_node, V4SFmode);
14706   tree V4SI_type_node = build_vector_type_for_mode (intSI_type_node, V4SImode);
14707   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
14708   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
14709   tree V8HI_type_node = build_vector_type_for_mode (intHI_type_node, V8HImode);
14710
14711   tree pchar_type_node = build_pointer_type (char_type_node);
14712   tree pcchar_type_node = build_pointer_type (
14713                              build_type_variant (char_type_node, 1, 0));
14714   tree pfloat_type_node = build_pointer_type (float_type_node);
14715   tree pcfloat_type_node = build_pointer_type (
14716                              build_type_variant (float_type_node, 1, 0));
14717   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
14718   tree pv2di_type_node = build_pointer_type (V2DI_type_node);
14719   tree pdi_type_node = build_pointer_type (long_long_unsigned_type_node);
14720
14721   /* Comparisons.  */
14722   tree int_ftype_v4sf_v4sf
14723     = build_function_type_list (integer_type_node,
14724                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
14725   tree v4si_ftype_v4sf_v4sf
14726     = build_function_type_list (V4SI_type_node,
14727                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
14728   /* MMX/SSE/integer conversions.  */
14729   tree int_ftype_v4sf
14730     = build_function_type_list (integer_type_node,
14731                                 V4SF_type_node, NULL_TREE);
14732   tree int64_ftype_v4sf
14733     = build_function_type_list (long_long_integer_type_node,
14734                                 V4SF_type_node, NULL_TREE);
14735   tree int_ftype_v8qi
14736     = build_function_type_list (integer_type_node, V8QI_type_node, NULL_TREE);
14737   tree v4sf_ftype_v4sf_int
14738     = build_function_type_list (V4SF_type_node,
14739                                 V4SF_type_node, integer_type_node, NULL_TREE);
14740   tree v4sf_ftype_v4sf_int64
14741     = build_function_type_list (V4SF_type_node,
14742                                 V4SF_type_node, long_long_integer_type_node,
14743                                 NULL_TREE);
14744   tree v4sf_ftype_v4sf_v2si
14745     = build_function_type_list (V4SF_type_node,
14746                                 V4SF_type_node, V2SI_type_node, NULL_TREE);
14747
14748   /* Miscellaneous.  */
14749   tree v8qi_ftype_v4hi_v4hi
14750     = build_function_type_list (V8QI_type_node,
14751                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
14752   tree v4hi_ftype_v2si_v2si
14753     = build_function_type_list (V4HI_type_node,
14754                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
14755   tree v4sf_ftype_v4sf_v4sf_int
14756     = build_function_type_list (V4SF_type_node,
14757                                 V4SF_type_node, V4SF_type_node,
14758                                 integer_type_node, NULL_TREE);
14759   tree v2si_ftype_v4hi_v4hi
14760     = build_function_type_list (V2SI_type_node,
14761                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
14762   tree v4hi_ftype_v4hi_int
14763     = build_function_type_list (V4HI_type_node,
14764                                 V4HI_type_node, integer_type_node, NULL_TREE);
14765   tree v4hi_ftype_v4hi_di
14766     = build_function_type_list (V4HI_type_node,
14767                                 V4HI_type_node, long_long_unsigned_type_node,
14768                                 NULL_TREE);
14769   tree v2si_ftype_v2si_di
14770     = build_function_type_list (V2SI_type_node,
14771                                 V2SI_type_node, long_long_unsigned_type_node,
14772                                 NULL_TREE);
14773   tree void_ftype_void
14774     = build_function_type (void_type_node, void_list_node);
14775   tree void_ftype_unsigned
14776     = build_function_type_list (void_type_node, unsigned_type_node, NULL_TREE);
14777   tree void_ftype_unsigned_unsigned
14778     = build_function_type_list (void_type_node, unsigned_type_node,
14779                                 unsigned_type_node, NULL_TREE);
14780   tree void_ftype_pcvoid_unsigned_unsigned
14781     = build_function_type_list (void_type_node, const_ptr_type_node,
14782                                 unsigned_type_node, unsigned_type_node,
14783                                 NULL_TREE);
14784   tree unsigned_ftype_void
14785     = build_function_type (unsigned_type_node, void_list_node);
14786   tree v2si_ftype_v4sf
14787     = build_function_type_list (V2SI_type_node, V4SF_type_node, NULL_TREE);
14788   /* Loads/stores.  */
14789   tree void_ftype_v8qi_v8qi_pchar
14790     = build_function_type_list (void_type_node,
14791                                 V8QI_type_node, V8QI_type_node,
14792                                 pchar_type_node, NULL_TREE);
14793   tree v4sf_ftype_pcfloat
14794     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
14795   /* @@@ the type is bogus */
14796   tree v4sf_ftype_v4sf_pv2si
14797     = build_function_type_list (V4SF_type_node,
14798                                 V4SF_type_node, pv2si_type_node, NULL_TREE);
14799   tree void_ftype_pv2si_v4sf
14800     = build_function_type_list (void_type_node,
14801                                 pv2si_type_node, V4SF_type_node, NULL_TREE);
14802   tree void_ftype_pfloat_v4sf
14803     = build_function_type_list (void_type_node,
14804                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
14805   tree void_ftype_pdi_di
14806     = build_function_type_list (void_type_node,
14807                                 pdi_type_node, long_long_unsigned_type_node,
14808                                 NULL_TREE);
14809   tree void_ftype_pv2di_v2di
14810     = build_function_type_list (void_type_node,
14811                                 pv2di_type_node, V2DI_type_node, NULL_TREE);
14812   /* Normal vector unops.  */
14813   tree v4sf_ftype_v4sf
14814     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
14815
14816   /* Normal vector binops.  */
14817   tree v4sf_ftype_v4sf_v4sf
14818     = build_function_type_list (V4SF_type_node,
14819                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
14820   tree v8qi_ftype_v8qi_v8qi
14821     = build_function_type_list (V8QI_type_node,
14822                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
14823   tree v4hi_ftype_v4hi_v4hi
14824     = build_function_type_list (V4HI_type_node,
14825                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
14826   tree v2si_ftype_v2si_v2si
14827     = build_function_type_list (V2SI_type_node,
14828                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
14829   tree di_ftype_di_di
14830     = build_function_type_list (long_long_unsigned_type_node,
14831                                 long_long_unsigned_type_node,
14832                                 long_long_unsigned_type_node, NULL_TREE);
14833
14834   tree v2si_ftype_v2sf
14835     = build_function_type_list (V2SI_type_node, V2SF_type_node, NULL_TREE);
14836   tree v2sf_ftype_v2si
14837     = build_function_type_list (V2SF_type_node, V2SI_type_node, NULL_TREE);
14838   tree v2si_ftype_v2si
14839     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
14840   tree v2sf_ftype_v2sf
14841     = build_function_type_list (V2SF_type_node, V2SF_type_node, NULL_TREE);
14842   tree v2sf_ftype_v2sf_v2sf
14843     = build_function_type_list (V2SF_type_node,
14844                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
14845   tree v2si_ftype_v2sf_v2sf
14846     = build_function_type_list (V2SI_type_node,
14847                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
14848   tree pint_type_node    = build_pointer_type (integer_type_node);
14849   tree pdouble_type_node = build_pointer_type (double_type_node);
14850   tree pcdouble_type_node = build_pointer_type (
14851                                 build_type_variant (double_type_node, 1, 0));
14852   tree int_ftype_v2df_v2df
14853     = build_function_type_list (integer_type_node,
14854                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
14855
14856   tree void_ftype_pcvoid
14857     = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE);
14858   tree v4sf_ftype_v4si
14859     = build_function_type_list (V4SF_type_node, V4SI_type_node, NULL_TREE);
14860   tree v4si_ftype_v4sf
14861     = build_function_type_list (V4SI_type_node, V4SF_type_node, NULL_TREE);
14862   tree v2df_ftype_v4si
14863     = build_function_type_list (V2DF_type_node, V4SI_type_node, NULL_TREE);
14864   tree v4si_ftype_v2df
14865     = build_function_type_list (V4SI_type_node, V2DF_type_node, NULL_TREE);
14866   tree v2si_ftype_v2df
14867     = build_function_type_list (V2SI_type_node, V2DF_type_node, NULL_TREE);
14868   tree v4sf_ftype_v2df
14869     = build_function_type_list (V4SF_type_node, V2DF_type_node, NULL_TREE);
14870   tree v2df_ftype_v2si
14871     = build_function_type_list (V2DF_type_node, V2SI_type_node, NULL_TREE);
14872   tree v2df_ftype_v4sf
14873     = build_function_type_list (V2DF_type_node, V4SF_type_node, NULL_TREE);
14874   tree int_ftype_v2df
14875     = build_function_type_list (integer_type_node, V2DF_type_node, NULL_TREE);
14876   tree int64_ftype_v2df
14877     = build_function_type_list (long_long_integer_type_node,
14878                                 V2DF_type_node, NULL_TREE);
14879   tree v2df_ftype_v2df_int
14880     = build_function_type_list (V2DF_type_node,
14881                                 V2DF_type_node, integer_type_node, NULL_TREE);
14882   tree v2df_ftype_v2df_int64
14883     = build_function_type_list (V2DF_type_node,
14884                                 V2DF_type_node, long_long_integer_type_node,
14885                                 NULL_TREE);
14886   tree v4sf_ftype_v4sf_v2df
14887     = build_function_type_list (V4SF_type_node,
14888                                 V4SF_type_node, V2DF_type_node, NULL_TREE);
14889   tree v2df_ftype_v2df_v4sf
14890     = build_function_type_list (V2DF_type_node,
14891                                 V2DF_type_node, V4SF_type_node, NULL_TREE);
14892   tree v2df_ftype_v2df_v2df_int
14893     = build_function_type_list (V2DF_type_node,
14894                                 V2DF_type_node, V2DF_type_node,
14895                                 integer_type_node,
14896                                 NULL_TREE);
14897   tree v2df_ftype_v2df_pcdouble
14898     = build_function_type_list (V2DF_type_node,
14899                                 V2DF_type_node, pcdouble_type_node, NULL_TREE);
14900   tree void_ftype_pdouble_v2df
14901     = build_function_type_list (void_type_node,
14902                                 pdouble_type_node, V2DF_type_node, NULL_TREE);
14903   tree void_ftype_pint_int
14904     = build_function_type_list (void_type_node,
14905                                 pint_type_node, integer_type_node, NULL_TREE);
14906   tree void_ftype_v16qi_v16qi_pchar
14907     = build_function_type_list (void_type_node,
14908                                 V16QI_type_node, V16QI_type_node,
14909                                 pchar_type_node, NULL_TREE);
14910   tree v2df_ftype_pcdouble
14911     = build_function_type_list (V2DF_type_node, pcdouble_type_node, NULL_TREE);
14912   tree v2df_ftype_v2df_v2df
14913     = build_function_type_list (V2DF_type_node,
14914                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
14915   tree v16qi_ftype_v16qi_v16qi
14916     = build_function_type_list (V16QI_type_node,
14917                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
14918   tree v8hi_ftype_v8hi_v8hi
14919     = build_function_type_list (V8HI_type_node,
14920                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
14921   tree v4si_ftype_v4si_v4si
14922     = build_function_type_list (V4SI_type_node,
14923                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
14924   tree v2di_ftype_v2di_v2di
14925     = build_function_type_list (V2DI_type_node,
14926                                 V2DI_type_node, V2DI_type_node, NULL_TREE);
14927   tree v2di_ftype_v2df_v2df
14928     = build_function_type_list (V2DI_type_node,
14929                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
14930   tree v2df_ftype_v2df
14931     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
14932   tree v2di_ftype_v2di_int
14933     = build_function_type_list (V2DI_type_node,
14934                                 V2DI_type_node, integer_type_node, NULL_TREE);
14935   tree v4si_ftype_v4si_int
14936     = build_function_type_list (V4SI_type_node,
14937                                 V4SI_type_node, integer_type_node, NULL_TREE);
14938   tree v8hi_ftype_v8hi_int
14939     = build_function_type_list (V8HI_type_node,
14940                                 V8HI_type_node, integer_type_node, NULL_TREE);
14941   tree v8hi_ftype_v8hi_v2di
14942     = build_function_type_list (V8HI_type_node,
14943                                 V8HI_type_node, V2DI_type_node, NULL_TREE);
14944   tree v4si_ftype_v4si_v2di
14945     = build_function_type_list (V4SI_type_node,
14946                                 V4SI_type_node, V2DI_type_node, NULL_TREE);
14947   tree v4si_ftype_v8hi_v8hi
14948     = build_function_type_list (V4SI_type_node,
14949                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
14950   tree di_ftype_v8qi_v8qi
14951     = build_function_type_list (long_long_unsigned_type_node,
14952                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
14953   tree di_ftype_v2si_v2si
14954     = build_function_type_list (long_long_unsigned_type_node,
14955                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
14956   tree v2di_ftype_v16qi_v16qi
14957     = build_function_type_list (V2DI_type_node,
14958                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
14959   tree v2di_ftype_v4si_v4si
14960     = build_function_type_list (V2DI_type_node,
14961                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
14962   tree int_ftype_v16qi
14963     = build_function_type_list (integer_type_node, V16QI_type_node, NULL_TREE);
14964   tree v16qi_ftype_pcchar
14965     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
14966   tree void_ftype_pchar_v16qi
14967     = build_function_type_list (void_type_node,
14968                                 pchar_type_node, V16QI_type_node, NULL_TREE);
14969
14970   tree float80_type;
14971   tree float128_type;
14972   tree ftype;
14973
14974   /* The __float80 type.  */
14975   if (TYPE_MODE (long_double_type_node) == XFmode)
14976     (*lang_hooks.types.register_builtin_type) (long_double_type_node,
14977                                                "__float80");
14978   else
14979     {
14980       /* The __float80 type.  */
14981       float80_type = make_node (REAL_TYPE);
14982       TYPE_PRECISION (float80_type) = 80;
14983       layout_type (float80_type);
14984       (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
14985     }
14986
14987   if (TARGET_64BIT)
14988     {
14989       float128_type = make_node (REAL_TYPE);
14990       TYPE_PRECISION (float128_type) = 128;
14991       layout_type (float128_type);
14992       (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
14993     }
14994
14995   /* Add all builtins that are more or less simple operations on two
14996      operands.  */
14997   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
14998     {
14999       /* Use one of the operands; the target can have a different mode for
15000          mask-generating compares.  */
15001       enum machine_mode mode;
15002       tree type;
15003
15004       if (d->name == 0)
15005         continue;
15006       mode = insn_data[d->icode].operand[1].mode;
15007
15008       switch (mode)
15009         {
15010         case V16QImode:
15011           type = v16qi_ftype_v16qi_v16qi;
15012           break;
15013         case V8HImode:
15014           type = v8hi_ftype_v8hi_v8hi;
15015           break;
15016         case V4SImode:
15017           type = v4si_ftype_v4si_v4si;
15018           break;
15019         case V2DImode:
15020           type = v2di_ftype_v2di_v2di;
15021           break;
15022         case V2DFmode:
15023           type = v2df_ftype_v2df_v2df;
15024           break;
15025         case V4SFmode:
15026           type = v4sf_ftype_v4sf_v4sf;
15027           break;
15028         case V8QImode:
15029           type = v8qi_ftype_v8qi_v8qi;
15030           break;
15031         case V4HImode:
15032           type = v4hi_ftype_v4hi_v4hi;
15033           break;
15034         case V2SImode:
15035           type = v2si_ftype_v2si_v2si;
15036           break;
15037         case DImode:
15038           type = di_ftype_di_di;
15039           break;
15040
15041         default:
15042           gcc_unreachable ();
15043         }
15044
15045       /* Override for comparisons.  */
15046       if (d->icode == CODE_FOR_sse_maskcmpv4sf3
15047           || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3)
15048         type = v4si_ftype_v4sf_v4sf;
15049
15050       if (d->icode == CODE_FOR_sse2_maskcmpv2df3
15051           || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
15052         type = v2di_ftype_v2df_v2df;
15053
15054       def_builtin (d->mask, d->name, type, d->code);
15055     }
15056
15057   /* Add the remaining MMX insns with somewhat more complicated types.  */
15058   def_builtin (MASK_MMX, "__builtin_ia32_emms", void_ftype_void, IX86_BUILTIN_EMMS);
15059   def_builtin (MASK_MMX, "__builtin_ia32_psllw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSLLW);
15060   def_builtin (MASK_MMX, "__builtin_ia32_pslld", v2si_ftype_v2si_di, IX86_BUILTIN_PSLLD);
15061   def_builtin (MASK_MMX, "__builtin_ia32_psllq", di_ftype_di_di, IX86_BUILTIN_PSLLQ);
15062
15063   def_builtin (MASK_MMX, "__builtin_ia32_psrlw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRLW);
15064   def_builtin (MASK_MMX, "__builtin_ia32_psrld", v2si_ftype_v2si_di, IX86_BUILTIN_PSRLD);
15065   def_builtin (MASK_MMX, "__builtin_ia32_psrlq", di_ftype_di_di, IX86_BUILTIN_PSRLQ);
15066
15067   def_builtin (MASK_MMX, "__builtin_ia32_psraw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRAW);
15068   def_builtin (MASK_MMX, "__builtin_ia32_psrad", v2si_ftype_v2si_di, IX86_BUILTIN_PSRAD);
15069
15070   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pshufw", v4hi_ftype_v4hi_int, IX86_BUILTIN_PSHUFW);
15071   def_builtin (MASK_MMX, "__builtin_ia32_pmaddwd", v2si_ftype_v4hi_v4hi, IX86_BUILTIN_PMADDWD);
15072
15073   /* comi/ucomi insns.  */
15074   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
15075     if (d->mask == MASK_SSE2)
15076       def_builtin (d->mask, d->name, int_ftype_v2df_v2df, d->code);
15077     else
15078       def_builtin (d->mask, d->name, int_ftype_v4sf_v4sf, d->code);
15079
15080   def_builtin (MASK_MMX, "__builtin_ia32_packsswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKSSWB);
15081   def_builtin (MASK_MMX, "__builtin_ia32_packssdw", v4hi_ftype_v2si_v2si, IX86_BUILTIN_PACKSSDW);
15082   def_builtin (MASK_MMX, "__builtin_ia32_packuswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKUSWB);
15083
15084   def_builtin (MASK_SSE, "__builtin_ia32_ldmxcsr", void_ftype_unsigned, IX86_BUILTIN_LDMXCSR);
15085   def_builtin (MASK_SSE, "__builtin_ia32_stmxcsr", unsigned_ftype_void, IX86_BUILTIN_STMXCSR);
15086   def_builtin (MASK_SSE, "__builtin_ia32_cvtpi2ps", v4sf_ftype_v4sf_v2si, IX86_BUILTIN_CVTPI2PS);
15087   def_builtin (MASK_SSE, "__builtin_ia32_cvtps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTPS2PI);
15088   def_builtin (MASK_SSE, "__builtin_ia32_cvtsi2ss", v4sf_ftype_v4sf_int, IX86_BUILTIN_CVTSI2SS);
15089   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtsi642ss", v4sf_ftype_v4sf_int64, IX86_BUILTIN_CVTSI642SS);
15090   def_builtin (MASK_SSE, "__builtin_ia32_cvtss2si", int_ftype_v4sf, IX86_BUILTIN_CVTSS2SI);
15091   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTSS2SI64);
15092   def_builtin (MASK_SSE, "__builtin_ia32_cvttps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTTPS2PI);
15093   def_builtin (MASK_SSE, "__builtin_ia32_cvttss2si", int_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI);
15094   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvttss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI64);
15095
15096   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_maskmovq", void_ftype_v8qi_v8qi_pchar, IX86_BUILTIN_MASKMOVQ);
15097
15098   def_builtin (MASK_SSE, "__builtin_ia32_loadups", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADUPS);
15099   def_builtin (MASK_SSE, "__builtin_ia32_storeups", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREUPS);
15100
15101   def_builtin (MASK_SSE, "__builtin_ia32_loadhps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADHPS);
15102   def_builtin (MASK_SSE, "__builtin_ia32_loadlps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADLPS);
15103   def_builtin (MASK_SSE, "__builtin_ia32_storehps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STOREHPS);
15104   def_builtin (MASK_SSE, "__builtin_ia32_storelps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STORELPS);
15105
15106   def_builtin (MASK_SSE, "__builtin_ia32_movmskps", int_ftype_v4sf, IX86_BUILTIN_MOVMSKPS);
15107   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pmovmskb", int_ftype_v8qi, IX86_BUILTIN_PMOVMSKB);
15108   def_builtin (MASK_SSE, "__builtin_ia32_movntps", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTPS);
15109   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_movntq", void_ftype_pdi_di, IX86_BUILTIN_MOVNTQ);
15110
15111   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_sfence", void_ftype_void, IX86_BUILTIN_SFENCE);
15112
15113   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_psadbw", di_ftype_v8qi_v8qi, IX86_BUILTIN_PSADBW);
15114
15115   def_builtin (MASK_SSE, "__builtin_ia32_rcpps", v4sf_ftype_v4sf, IX86_BUILTIN_RCPPS);
15116   def_builtin (MASK_SSE, "__builtin_ia32_rcpss", v4sf_ftype_v4sf, IX86_BUILTIN_RCPSS);
15117   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS);
15118   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTSS);
15119   def_builtin (MASK_SSE, "__builtin_ia32_sqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS);
15120   def_builtin (MASK_SSE, "__builtin_ia32_sqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTSS);
15121
15122   def_builtin (MASK_SSE, "__builtin_ia32_shufps", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_SHUFPS);
15123
15124   /* Original 3DNow!  */
15125   def_builtin (MASK_3DNOW, "__builtin_ia32_femms", void_ftype_void, IX86_BUILTIN_FEMMS);
15126   def_builtin (MASK_3DNOW, "__builtin_ia32_pavgusb", v8qi_ftype_v8qi_v8qi, IX86_BUILTIN_PAVGUSB);
15127   def_builtin (MASK_3DNOW, "__builtin_ia32_pf2id", v2si_ftype_v2sf, IX86_BUILTIN_PF2ID);
15128   def_builtin (MASK_3DNOW, "__builtin_ia32_pfacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFACC);
15129   def_builtin (MASK_3DNOW, "__builtin_ia32_pfadd", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFADD);
15130   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpeq", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPEQ);
15131   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpge", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGE);
15132   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpgt", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGT);
15133   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmax", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMAX);
15134   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmin", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMIN);
15135   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmul", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMUL);
15136   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcp", v2sf_ftype_v2sf, IX86_BUILTIN_PFRCP);
15137   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT1);
15138   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit2", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT2);
15139   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqrt", v2sf_ftype_v2sf, IX86_BUILTIN_PFRSQRT);
15140   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRSQIT1);
15141   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsub", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUB);
15142   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsubr", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUBR);
15143   def_builtin (MASK_3DNOW, "__builtin_ia32_pi2fd", v2sf_ftype_v2si, IX86_BUILTIN_PI2FD);
15144   def_builtin (MASK_3DNOW, "__builtin_ia32_pmulhrw", v4hi_ftype_v4hi_v4hi, IX86_BUILTIN_PMULHRW);
15145
15146   /* 3DNow! extension as used in the Athlon CPU.  */
15147   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pf2iw", v2si_ftype_v2sf, IX86_BUILTIN_PF2IW);
15148   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFNACC);
15149   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfpnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFPNACC);
15150   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pi2fw", v2sf_ftype_v2si, IX86_BUILTIN_PI2FW);
15151   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsf", v2sf_ftype_v2sf, IX86_BUILTIN_PSWAPDSF);
15152   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsi", v2si_ftype_v2si, IX86_BUILTIN_PSWAPDSI);
15153
15154   /* SSE2 */
15155   def_builtin (MASK_SSE2, "__builtin_ia32_maskmovdqu", void_ftype_v16qi_v16qi_pchar, IX86_BUILTIN_MASKMOVDQU);
15156
15157   def_builtin (MASK_SSE2, "__builtin_ia32_loadupd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADUPD);
15158   def_builtin (MASK_SSE2, "__builtin_ia32_storeupd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREUPD);
15159
15160   def_builtin (MASK_SSE2, "__builtin_ia32_loadhpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADHPD);
15161   def_builtin (MASK_SSE2, "__builtin_ia32_loadlpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADLPD);
15162
15163   def_builtin (MASK_SSE2, "__builtin_ia32_movmskpd", int_ftype_v2df, IX86_BUILTIN_MOVMSKPD);
15164   def_builtin (MASK_SSE2, "__builtin_ia32_pmovmskb128", int_ftype_v16qi, IX86_BUILTIN_PMOVMSKB128);
15165   def_builtin (MASK_SSE2, "__builtin_ia32_movnti", void_ftype_pint_int, IX86_BUILTIN_MOVNTI);
15166   def_builtin (MASK_SSE2, "__builtin_ia32_movntpd", void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTPD);
15167   def_builtin (MASK_SSE2, "__builtin_ia32_movntdq", void_ftype_pv2di_v2di, IX86_BUILTIN_MOVNTDQ);
15168
15169   def_builtin (MASK_SSE2, "__builtin_ia32_pshufd", v4si_ftype_v4si_int, IX86_BUILTIN_PSHUFD);
15170   def_builtin (MASK_SSE2, "__builtin_ia32_pshuflw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFLW);
15171   def_builtin (MASK_SSE2, "__builtin_ia32_pshufhw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFHW);
15172   def_builtin (MASK_SSE2, "__builtin_ia32_psadbw128", v2di_ftype_v16qi_v16qi, IX86_BUILTIN_PSADBW128);
15173
15174   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtpd", v2df_ftype_v2df, IX86_BUILTIN_SQRTPD);
15175   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtsd", v2df_ftype_v2df, IX86_BUILTIN_SQRTSD);
15176
15177   def_builtin (MASK_SSE2, "__builtin_ia32_shufpd", v2df_ftype_v2df_v2df_int, IX86_BUILTIN_SHUFPD);
15178
15179   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2pd", v2df_ftype_v4si, IX86_BUILTIN_CVTDQ2PD);
15180   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2ps", v4sf_ftype_v4si, IX86_BUILTIN_CVTDQ2PS);
15181
15182   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTPD2DQ);
15183   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTPD2PI);
15184   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2ps", v4sf_ftype_v2df, IX86_BUILTIN_CVTPD2PS);
15185   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTTPD2DQ);
15186   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTTPD2PI);
15187
15188   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpi2pd", v2df_ftype_v2si, IX86_BUILTIN_CVTPI2PD);
15189
15190   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2si", int_ftype_v2df, IX86_BUILTIN_CVTSD2SI);
15191   def_builtin (MASK_SSE2, "__builtin_ia32_cvttsd2si", int_ftype_v2df, IX86_BUILTIN_CVTTSD2SI);
15192   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTSD2SI64);
15193   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvttsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTTSD2SI64);
15194
15195   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTPS2DQ);
15196   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2pd", v2df_ftype_v4sf, IX86_BUILTIN_CVTPS2PD);
15197   def_builtin (MASK_SSE2, "__builtin_ia32_cvttps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTTPS2DQ);
15198
15199   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsi2sd", v2df_ftype_v2df_int, IX86_BUILTIN_CVTSI2SD);
15200   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsi642sd", v2df_ftype_v2df_int64, IX86_BUILTIN_CVTSI642SD);
15201   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2ss", v4sf_ftype_v4sf_v2df, IX86_BUILTIN_CVTSD2SS);
15202   def_builtin (MASK_SSE2, "__builtin_ia32_cvtss2sd", v2df_ftype_v2df_v4sf, IX86_BUILTIN_CVTSS2SD);
15203
15204   def_builtin (MASK_SSE2, "__builtin_ia32_clflush", void_ftype_pcvoid, IX86_BUILTIN_CLFLUSH);
15205   def_builtin (MASK_SSE2, "__builtin_ia32_lfence", void_ftype_void, IX86_BUILTIN_LFENCE);
15206   def_builtin (MASK_SSE2, "__builtin_ia32_mfence", void_ftype_void, IX86_BUILTIN_MFENCE);
15207
15208   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqu", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQU);
15209   def_builtin (MASK_SSE2, "__builtin_ia32_storedqu", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQU);
15210
15211   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq", di_ftype_v2si_v2si, IX86_BUILTIN_PMULUDQ);
15212   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq128", v2di_ftype_v4si_v4si, IX86_BUILTIN_PMULUDQ128);
15213
15214   def_builtin (MASK_SSE2, "__builtin_ia32_psllw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSLLW128);
15215   def_builtin (MASK_SSE2, "__builtin_ia32_pslld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSLLD128);
15216   def_builtin (MASK_SSE2, "__builtin_ia32_psllq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSLLQ128);
15217
15218   def_builtin (MASK_SSE2, "__builtin_ia32_psrlw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRLW128);
15219   def_builtin (MASK_SSE2, "__builtin_ia32_psrld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRLD128);
15220   def_builtin (MASK_SSE2, "__builtin_ia32_psrlq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSRLQ128);
15221
15222   def_builtin (MASK_SSE2, "__builtin_ia32_psraw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRAW128);
15223   def_builtin (MASK_SSE2, "__builtin_ia32_psrad128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRAD128);
15224
15225   def_builtin (MASK_SSE2, "__builtin_ia32_pslldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLDQI128);
15226   def_builtin (MASK_SSE2, "__builtin_ia32_psllwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSLLWI128);
15227   def_builtin (MASK_SSE2, "__builtin_ia32_pslldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSLLDI128);
15228   def_builtin (MASK_SSE2, "__builtin_ia32_psllqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLQI128);
15229
15230   def_builtin (MASK_SSE2, "__builtin_ia32_psrldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLDQI128);
15231   def_builtin (MASK_SSE2, "__builtin_ia32_psrlwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRLWI128);
15232   def_builtin (MASK_SSE2, "__builtin_ia32_psrldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRLDI128);
15233   def_builtin (MASK_SSE2, "__builtin_ia32_psrlqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLQI128);
15234
15235   def_builtin (MASK_SSE2, "__builtin_ia32_psrawi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRAWI128);
15236   def_builtin (MASK_SSE2, "__builtin_ia32_psradi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRADI128);
15237
15238   def_builtin (MASK_SSE2, "__builtin_ia32_pmaddwd128", v4si_ftype_v8hi_v8hi, IX86_BUILTIN_PMADDWD128);
15239
15240   /* Prescott New Instructions.  */
15241   def_builtin (MASK_SSE3, "__builtin_ia32_monitor",
15242                void_ftype_pcvoid_unsigned_unsigned,
15243                IX86_BUILTIN_MONITOR);
15244   def_builtin (MASK_SSE3, "__builtin_ia32_mwait",
15245                void_ftype_unsigned_unsigned,
15246                IX86_BUILTIN_MWAIT);
15247   def_builtin (MASK_SSE3, "__builtin_ia32_movshdup",
15248                v4sf_ftype_v4sf,
15249                IX86_BUILTIN_MOVSHDUP);
15250   def_builtin (MASK_SSE3, "__builtin_ia32_movsldup",
15251                v4sf_ftype_v4sf,
15252                IX86_BUILTIN_MOVSLDUP);
15253   def_builtin (MASK_SSE3, "__builtin_ia32_lddqu",
15254                v16qi_ftype_pcchar, IX86_BUILTIN_LDDQU);
15255
15256   /* Access to the vec_init patterns.  */
15257   ftype = build_function_type_list (V2SI_type_node, integer_type_node,
15258                                     integer_type_node, NULL_TREE);
15259   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v2si",
15260                ftype, IX86_BUILTIN_VEC_INIT_V2SI);
15261
15262   ftype = build_function_type_list (V4HI_type_node, short_integer_type_node,
15263                                     short_integer_type_node,
15264                                     short_integer_type_node,
15265                                     short_integer_type_node, NULL_TREE);
15266   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v4hi",
15267                ftype, IX86_BUILTIN_VEC_INIT_V4HI);
15268
15269   ftype = build_function_type_list (V8QI_type_node, char_type_node,
15270                                     char_type_node, char_type_node,
15271                                     char_type_node, char_type_node,
15272                                     char_type_node, char_type_node,
15273                                     char_type_node, NULL_TREE);
15274   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v8qi",
15275                ftype, IX86_BUILTIN_VEC_INIT_V8QI);
15276
15277   /* Access to the vec_extract patterns.  */
15278   ftype = build_function_type_list (double_type_node, V2DF_type_node,
15279                                     integer_type_node, NULL_TREE);
15280   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2df",
15281                ftype, IX86_BUILTIN_VEC_EXT_V2DF);
15282
15283   ftype = build_function_type_list (long_long_integer_type_node,
15284                                     V2DI_type_node, integer_type_node,
15285                                     NULL_TREE);
15286   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2di",
15287                ftype, IX86_BUILTIN_VEC_EXT_V2DI);
15288
15289   ftype = build_function_type_list (float_type_node, V4SF_type_node,
15290                                     integer_type_node, NULL_TREE);
15291   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4sf",
15292                ftype, IX86_BUILTIN_VEC_EXT_V4SF);
15293
15294   ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
15295                                     integer_type_node, NULL_TREE);
15296   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4si",
15297                ftype, IX86_BUILTIN_VEC_EXT_V4SI);
15298
15299   ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
15300                                     integer_type_node, NULL_TREE);
15301   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v8hi",
15302                ftype, IX86_BUILTIN_VEC_EXT_V8HI);
15303
15304   ftype = build_function_type_list (intHI_type_node, V4HI_type_node,
15305                                     integer_type_node, NULL_TREE);
15306   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_ext_v4hi",
15307                ftype, IX86_BUILTIN_VEC_EXT_V4HI);
15308
15309   ftype = build_function_type_list (intSI_type_node, V2SI_type_node,
15310                                     integer_type_node, NULL_TREE);
15311   def_builtin (MASK_MMX, "__builtin_ia32_vec_ext_v2si",
15312                ftype, IX86_BUILTIN_VEC_EXT_V2SI);
15313
15314   /* Access to the vec_set patterns.  */
15315   ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
15316                                     intHI_type_node,
15317                                     integer_type_node, NULL_TREE);
15318   def_builtin (MASK_SSE, "__builtin_ia32_vec_set_v8hi",
15319                ftype, IX86_BUILTIN_VEC_SET_V8HI);
15320   
15321   ftype = build_function_type_list (V4HI_type_node, V4HI_type_node,
15322                                     intHI_type_node,
15323                                     integer_type_node, NULL_TREE);
15324   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_set_v4hi",
15325                ftype, IX86_BUILTIN_VEC_SET_V4HI);
15326 }
15327 #undef def_builtin
15328
15329 /* Set up all the SSE ABI builtins that we may use to override
15330    the normal builtins.  */
15331 static void
15332 ix86_init_sse_abi_builtins (void)
15333 {
15334   tree dbl, flt, dbl2, flt2;
15335
15336   /* Bail out in case the template definitions are not available.  */
15337   if (! built_in_decls [BUILT_IN_SIN]
15338       || ! built_in_decls [BUILT_IN_SINF]
15339       || ! built_in_decls [BUILT_IN_ATAN2]
15340       || ! built_in_decls [BUILT_IN_ATAN2F])
15341     return;
15342
15343   /* Build the function types as variants of the existing ones.  */
15344   dbl = build_variant_type_copy (TREE_TYPE (built_in_decls [BUILT_IN_SIN]));
15345   TYPE_ATTRIBUTES (dbl)
15346     = tree_cons (get_identifier ("sseregparm"),
15347                  NULL_TREE, TYPE_ATTRIBUTES (dbl));
15348   flt = build_variant_type_copy (TREE_TYPE (built_in_decls [BUILT_IN_SINF]));
15349   TYPE_ATTRIBUTES (flt)
15350     = tree_cons (get_identifier ("sseregparm"),
15351                  NULL_TREE, TYPE_ATTRIBUTES (flt));
15352   dbl2 = build_variant_type_copy (TREE_TYPE (built_in_decls [BUILT_IN_ATAN2]));
15353   TYPE_ATTRIBUTES (dbl2)
15354     = tree_cons (get_identifier ("sseregparm"),
15355                  NULL_TREE, TYPE_ATTRIBUTES (dbl2));
15356   flt2 = build_variant_type_copy (TREE_TYPE (built_in_decls [BUILT_IN_ATAN2F]));
15357   TYPE_ATTRIBUTES (flt2)
15358     = tree_cons (get_identifier ("sseregparm"),
15359                  NULL_TREE, TYPE_ATTRIBUTES (flt2));
15360
15361 #define def_builtin(capname, name, type) \
15362   ix86_builtin_function_variants [BUILT_IN_ ## capname]                 \
15363     = lang_hooks.builtin_function ("__builtin_sse2_" # name, type,      \
15364                                    IX86_BUILTIN_SSE2_ ## capname,       \
15365                                    BUILT_IN_NORMAL,                     \
15366                                    "__libm_sse2_" # name, NULL_TREE)
15367  
15368   def_builtin (ACOS, acos, dbl);
15369   def_builtin (ACOSF, acosf, flt);
15370   def_builtin (ASIN, asin, dbl);
15371   def_builtin (ASINF, asinf, flt);
15372   def_builtin (ATAN, atan, dbl);
15373   def_builtin (ATANF, atanf, flt);
15374   def_builtin (ATAN2, atan2, dbl2);
15375   def_builtin (ATAN2F, atan2f, flt2);
15376   def_builtin (COS, cos, dbl);
15377   def_builtin (COSF, cosf, flt);
15378   def_builtin (EXP, exp, dbl);
15379   def_builtin (EXPF, expf, flt);
15380   def_builtin (LOG10, log10, dbl);
15381   def_builtin (LOG10F, log10f, flt);
15382   def_builtin (LOG, log, dbl);
15383   def_builtin (LOGF, logf, flt);
15384   def_builtin (SIN, sin, dbl);
15385   def_builtin (SINF, sinf, flt);
15386   def_builtin (TAN, tan, dbl);
15387   def_builtin (TANF, tanf, flt);
15388
15389 #undef def_builtin
15390 }
15391
15392 /* Errors in the source file can cause expand_expr to return const0_rtx
15393    where we expect a vector.  To avoid crashing, use one of the vector
15394    clear instructions.  */
15395 static rtx
15396 safe_vector_operand (rtx x, enum machine_mode mode)
15397 {
15398   if (x == const0_rtx)
15399     x = CONST0_RTX (mode);
15400   return x;
15401 }
15402
15403 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
15404
15405 static rtx
15406 ix86_expand_binop_builtin (enum insn_code icode, tree arglist, rtx target)
15407 {
15408   rtx pat, xops[3];
15409   tree arg0 = TREE_VALUE (arglist);
15410   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15411   rtx op0 = expand_normal (arg0);
15412   rtx op1 = expand_normal (arg1);
15413   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15414   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15415   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
15416
15417   if (VECTOR_MODE_P (mode0))
15418     op0 = safe_vector_operand (op0, mode0);
15419   if (VECTOR_MODE_P (mode1))
15420     op1 = safe_vector_operand (op1, mode1);
15421
15422   if (optimize || !target
15423       || GET_MODE (target) != tmode
15424       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15425     target = gen_reg_rtx (tmode);
15426
15427   if (GET_MODE (op1) == SImode && mode1 == TImode)
15428     {
15429       rtx x = gen_reg_rtx (V4SImode);
15430       emit_insn (gen_sse2_loadd (x, op1));
15431       op1 = gen_lowpart (TImode, x);
15432     }
15433
15434   /* The insn must want input operands in the same modes as the
15435      result.  */
15436   gcc_assert ((GET_MODE (op0) == mode0 || GET_MODE (op0) == VOIDmode)
15437               && (GET_MODE (op1) == mode1 || GET_MODE (op1) == VOIDmode));
15438
15439   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
15440     op0 = copy_to_mode_reg (mode0, op0);
15441   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
15442     op1 = copy_to_mode_reg (mode1, op1);
15443
15444   /* ??? Using ix86_fixup_binary_operands is problematic when
15445      we've got mismatched modes.  Fake it.  */
15446
15447   xops[0] = target;
15448   xops[1] = op0;
15449   xops[2] = op1;
15450
15451   if (tmode == mode0 && tmode == mode1)
15452     {
15453       target = ix86_fixup_binary_operands (UNKNOWN, tmode, xops);
15454       op0 = xops[1];
15455       op1 = xops[2];
15456     }
15457   else if (optimize || !ix86_binary_operator_ok (UNKNOWN, tmode, xops))
15458     {
15459       op0 = force_reg (mode0, op0);
15460       op1 = force_reg (mode1, op1);
15461       target = gen_reg_rtx (tmode);
15462     }
15463
15464   pat = GEN_FCN (icode) (target, op0, op1);
15465   if (! pat)
15466     return 0;
15467   emit_insn (pat);
15468   return target;
15469 }
15470
15471 /* Subroutine of ix86_expand_builtin to take care of stores.  */
15472
15473 static rtx
15474 ix86_expand_store_builtin (enum insn_code icode, tree arglist)
15475 {
15476   rtx pat;
15477   tree arg0 = TREE_VALUE (arglist);
15478   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15479   rtx op0 = expand_normal (arg0);
15480   rtx op1 = expand_normal (arg1);
15481   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
15482   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
15483
15484   if (VECTOR_MODE_P (mode1))
15485     op1 = safe_vector_operand (op1, mode1);
15486
15487   op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
15488   op1 = copy_to_mode_reg (mode1, op1);
15489
15490   pat = GEN_FCN (icode) (op0, op1);
15491   if (pat)
15492     emit_insn (pat);
15493   return 0;
15494 }
15495
15496 /* Subroutine of ix86_expand_builtin to take care of unop insns.  */
15497
15498 static rtx
15499 ix86_expand_unop_builtin (enum insn_code icode, tree arglist,
15500                           rtx target, int do_load)
15501 {
15502   rtx pat;
15503   tree arg0 = TREE_VALUE (arglist);
15504   rtx op0 = expand_normal (arg0);
15505   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15506   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15507
15508   if (optimize || !target
15509       || GET_MODE (target) != tmode
15510       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15511     target = gen_reg_rtx (tmode);
15512   if (do_load)
15513     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
15514   else
15515     {
15516       if (VECTOR_MODE_P (mode0))
15517         op0 = safe_vector_operand (op0, mode0);
15518
15519       if ((optimize && !register_operand (op0, mode0))
15520           || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15521         op0 = copy_to_mode_reg (mode0, op0);
15522     }
15523
15524   pat = GEN_FCN (icode) (target, op0);
15525   if (! pat)
15526     return 0;
15527   emit_insn (pat);
15528   return target;
15529 }
15530
15531 /* Subroutine of ix86_expand_builtin to take care of three special unop insns:
15532    sqrtss, rsqrtss, rcpss.  */
15533
15534 static rtx
15535 ix86_expand_unop1_builtin (enum insn_code icode, tree arglist, rtx target)
15536 {
15537   rtx pat;
15538   tree arg0 = TREE_VALUE (arglist);
15539   rtx op1, op0 = expand_normal (arg0);
15540   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15541   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15542
15543   if (optimize || !target
15544       || GET_MODE (target) != tmode
15545       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15546     target = gen_reg_rtx (tmode);
15547
15548   if (VECTOR_MODE_P (mode0))
15549     op0 = safe_vector_operand (op0, mode0);
15550
15551   if ((optimize && !register_operand (op0, mode0))
15552       || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15553     op0 = copy_to_mode_reg (mode0, op0);
15554
15555   op1 = op0;
15556   if (! (*insn_data[icode].operand[2].predicate) (op1, mode0))
15557     op1 = copy_to_mode_reg (mode0, op1);
15558
15559   pat = GEN_FCN (icode) (target, op0, op1);
15560   if (! pat)
15561     return 0;
15562   emit_insn (pat);
15563   return target;
15564 }
15565
15566 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
15567
15568 static rtx
15569 ix86_expand_sse_compare (const struct builtin_description *d, tree arglist,
15570                          rtx target)
15571 {
15572   rtx pat;
15573   tree arg0 = TREE_VALUE (arglist);
15574   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15575   rtx op0 = expand_normal (arg0);
15576   rtx op1 = expand_normal (arg1);
15577   rtx op2;
15578   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
15579   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
15580   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
15581   enum rtx_code comparison = d->comparison;
15582
15583   if (VECTOR_MODE_P (mode0))
15584     op0 = safe_vector_operand (op0, mode0);
15585   if (VECTOR_MODE_P (mode1))
15586     op1 = safe_vector_operand (op1, mode1);
15587
15588   /* Swap operands if we have a comparison that isn't available in
15589      hardware.  */
15590   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
15591     {
15592       rtx tmp = gen_reg_rtx (mode1);
15593       emit_move_insn (tmp, op1);
15594       op1 = op0;
15595       op0 = tmp;
15596     }
15597
15598   if (optimize || !target
15599       || GET_MODE (target) != tmode
15600       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
15601     target = gen_reg_rtx (tmode);
15602
15603   if ((optimize && !register_operand (op0, mode0))
15604       || ! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
15605     op0 = copy_to_mode_reg (mode0, op0);
15606   if ((optimize && !register_operand (op1, mode1))
15607       || ! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
15608     op1 = copy_to_mode_reg (mode1, op1);
15609
15610   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
15611   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
15612   if (! pat)
15613     return 0;
15614   emit_insn (pat);
15615   return target;
15616 }
15617
15618 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
15619
15620 static rtx
15621 ix86_expand_sse_comi (const struct builtin_description *d, tree arglist,
15622                       rtx target)
15623 {
15624   rtx pat;
15625   tree arg0 = TREE_VALUE (arglist);
15626   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15627   rtx op0 = expand_normal (arg0);
15628   rtx op1 = expand_normal (arg1);
15629   rtx op2;
15630   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
15631   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
15632   enum rtx_code comparison = d->comparison;
15633
15634   if (VECTOR_MODE_P (mode0))
15635     op0 = safe_vector_operand (op0, mode0);
15636   if (VECTOR_MODE_P (mode1))
15637     op1 = safe_vector_operand (op1, mode1);
15638
15639   /* Swap operands if we have a comparison that isn't available in
15640      hardware.  */
15641   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
15642     {
15643       rtx tmp = op1;
15644       op1 = op0;
15645       op0 = tmp;
15646     }
15647
15648   target = gen_reg_rtx (SImode);
15649   emit_move_insn (target, const0_rtx);
15650   target = gen_rtx_SUBREG (QImode, target, 0);
15651
15652   if ((optimize && !register_operand (op0, mode0))
15653       || !(*insn_data[d->icode].operand[0].predicate) (op0, mode0))
15654     op0 = copy_to_mode_reg (mode0, op0);
15655   if ((optimize && !register_operand (op1, mode1))
15656       || !(*insn_data[d->icode].operand[1].predicate) (op1, mode1))
15657     op1 = copy_to_mode_reg (mode1, op1);
15658
15659   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
15660   pat = GEN_FCN (d->icode) (op0, op1);
15661   if (! pat)
15662     return 0;
15663   emit_insn (pat);
15664   emit_insn (gen_rtx_SET (VOIDmode,
15665                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
15666                           gen_rtx_fmt_ee (comparison, QImode,
15667                                           SET_DEST (pat),
15668                                           const0_rtx)));
15669
15670   return SUBREG_REG (target);
15671 }
15672
15673 /* Return the integer constant in ARG.  Constrain it to be in the range
15674    of the subparts of VEC_TYPE; issue an error if not.  */
15675
15676 static int
15677 get_element_number (tree vec_type, tree arg)
15678 {
15679   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
15680
15681   if (!host_integerp (arg, 1)
15682       || (elt = tree_low_cst (arg, 1), elt > max))
15683     {
15684       error ("selector must be an integer constant in the range 0..%wi", max);
15685       return 0;
15686     }
15687
15688   return elt;
15689 }
15690
15691 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
15692    ix86_expand_vector_init.  We DO have language-level syntax for this, in
15693    the form of  (type){ init-list }.  Except that since we can't place emms
15694    instructions from inside the compiler, we can't allow the use of MMX
15695    registers unless the user explicitly asks for it.  So we do *not* define
15696    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
15697    we have builtins invoked by mmintrin.h that gives us license to emit 
15698    these sorts of instructions.  */
15699
15700 static rtx
15701 ix86_expand_vec_init_builtin (tree type, tree arglist, rtx target)
15702 {
15703   enum machine_mode tmode = TYPE_MODE (type);
15704   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
15705   int i, n_elt = GET_MODE_NUNITS (tmode);
15706   rtvec v = rtvec_alloc (n_elt);
15707
15708   gcc_assert (VECTOR_MODE_P (tmode));
15709
15710   for (i = 0; i < n_elt; ++i, arglist = TREE_CHAIN (arglist))
15711     {
15712       rtx x = expand_normal (TREE_VALUE (arglist));
15713       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
15714     }
15715
15716   gcc_assert (arglist == NULL);
15717
15718   if (!target || !register_operand (target, tmode))
15719     target = gen_reg_rtx (tmode);
15720
15721   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
15722   return target;
15723 }
15724
15725 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
15726    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
15727    had a language-level syntax for referencing vector elements.  */
15728
15729 static rtx
15730 ix86_expand_vec_ext_builtin (tree arglist, rtx target)
15731 {
15732   enum machine_mode tmode, mode0;
15733   tree arg0, arg1;
15734   int elt;
15735   rtx op0;
15736
15737   arg0 = TREE_VALUE (arglist);
15738   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15739
15740   op0 = expand_normal (arg0);
15741   elt = get_element_number (TREE_TYPE (arg0), arg1);
15742
15743   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
15744   mode0 = TYPE_MODE (TREE_TYPE (arg0));
15745   gcc_assert (VECTOR_MODE_P (mode0));
15746
15747   op0 = force_reg (mode0, op0);
15748
15749   if (optimize || !target || !register_operand (target, tmode))
15750     target = gen_reg_rtx (tmode);
15751
15752   ix86_expand_vector_extract (true, target, op0, elt);
15753
15754   return target;
15755 }
15756
15757 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
15758    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
15759    a language-level syntax for referencing vector elements.  */
15760
15761 static rtx
15762 ix86_expand_vec_set_builtin (tree arglist)
15763 {
15764   enum machine_mode tmode, mode1;
15765   tree arg0, arg1, arg2;
15766   int elt;
15767   rtx op0, op1;
15768
15769   arg0 = TREE_VALUE (arglist);
15770   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15771   arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
15772
15773   tmode = TYPE_MODE (TREE_TYPE (arg0));
15774   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
15775   gcc_assert (VECTOR_MODE_P (tmode));
15776
15777   op0 = expand_expr (arg0, NULL_RTX, tmode, 0);
15778   op1 = expand_expr (arg1, NULL_RTX, mode1, 0);
15779   elt = get_element_number (TREE_TYPE (arg0), arg2);
15780
15781   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
15782     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
15783
15784   op0 = force_reg (tmode, op0);
15785   op1 = force_reg (mode1, op1);
15786
15787   ix86_expand_vector_set (true, op0, op1, elt);
15788
15789   return op0;
15790 }
15791
15792 /* Expand an expression EXP that calls a built-in function,
15793    with result going to TARGET if that's convenient
15794    (and in mode MODE if that's convenient).
15795    SUBTARGET may be used as the target for computing one of EXP's operands.
15796    IGNORE is nonzero if the value is to be ignored.  */
15797
15798 static rtx
15799 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
15800                      enum machine_mode mode ATTRIBUTE_UNUSED,
15801                      int ignore ATTRIBUTE_UNUSED)
15802 {
15803   const struct builtin_description *d;
15804   size_t i;
15805   enum insn_code icode;
15806   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
15807   tree arglist = TREE_OPERAND (exp, 1);
15808   tree arg0, arg1, arg2;
15809   rtx op0, op1, op2, pat;
15810   enum machine_mode tmode, mode0, mode1, mode2;
15811   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
15812
15813   switch (fcode)
15814     {
15815     case IX86_BUILTIN_EMMS:
15816       emit_insn (gen_mmx_emms ());
15817       return 0;
15818
15819     case IX86_BUILTIN_SFENCE:
15820       emit_insn (gen_sse_sfence ());
15821       return 0;
15822
15823     case IX86_BUILTIN_MASKMOVQ:
15824     case IX86_BUILTIN_MASKMOVDQU:
15825       icode = (fcode == IX86_BUILTIN_MASKMOVQ
15826                ? CODE_FOR_mmx_maskmovq
15827                : CODE_FOR_sse2_maskmovdqu);
15828       /* Note the arg order is different from the operand order.  */
15829       arg1 = TREE_VALUE (arglist);
15830       arg2 = TREE_VALUE (TREE_CHAIN (arglist));
15831       arg0 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
15832       op0 = expand_normal (arg0);
15833       op1 = expand_normal (arg1);
15834       op2 = expand_normal (arg2);
15835       mode0 = insn_data[icode].operand[0].mode;
15836       mode1 = insn_data[icode].operand[1].mode;
15837       mode2 = insn_data[icode].operand[2].mode;
15838
15839       op0 = force_reg (Pmode, op0);
15840       op0 = gen_rtx_MEM (mode1, op0);
15841
15842       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
15843         op0 = copy_to_mode_reg (mode0, op0);
15844       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
15845         op1 = copy_to_mode_reg (mode1, op1);
15846       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
15847         op2 = copy_to_mode_reg (mode2, op2);
15848       pat = GEN_FCN (icode) (op0, op1, op2);
15849       if (! pat)
15850         return 0;
15851       emit_insn (pat);
15852       return 0;
15853
15854     case IX86_BUILTIN_SQRTSS:
15855       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmsqrtv4sf2, arglist, target);
15856     case IX86_BUILTIN_RSQRTSS:
15857       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrsqrtv4sf2, arglist, target);
15858     case IX86_BUILTIN_RCPSS:
15859       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrcpv4sf2, arglist, target);
15860
15861     case IX86_BUILTIN_LOADUPS:
15862       return ix86_expand_unop_builtin (CODE_FOR_sse_movups, arglist, target, 1);
15863
15864     case IX86_BUILTIN_STOREUPS:
15865       return ix86_expand_store_builtin (CODE_FOR_sse_movups, arglist);
15866
15867     case IX86_BUILTIN_LOADHPS:
15868     case IX86_BUILTIN_LOADLPS:
15869     case IX86_BUILTIN_LOADHPD:
15870     case IX86_BUILTIN_LOADLPD:
15871       icode = (fcode == IX86_BUILTIN_LOADHPS ? CODE_FOR_sse_loadhps
15872                : fcode == IX86_BUILTIN_LOADLPS ? CODE_FOR_sse_loadlps
15873                : fcode == IX86_BUILTIN_LOADHPD ? CODE_FOR_sse2_loadhpd
15874                : CODE_FOR_sse2_loadlpd);
15875       arg0 = TREE_VALUE (arglist);
15876       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15877       op0 = expand_normal (arg0);
15878       op1 = expand_normal (arg1);
15879       tmode = insn_data[icode].operand[0].mode;
15880       mode0 = insn_data[icode].operand[1].mode;
15881       mode1 = insn_data[icode].operand[2].mode;
15882
15883       op0 = force_reg (mode0, op0);
15884       op1 = gen_rtx_MEM (mode1, copy_to_mode_reg (Pmode, op1));
15885       if (optimize || target == 0
15886           || GET_MODE (target) != tmode
15887           || !register_operand (target, tmode))
15888         target = gen_reg_rtx (tmode);
15889       pat = GEN_FCN (icode) (target, op0, op1);
15890       if (! pat)
15891         return 0;
15892       emit_insn (pat);
15893       return target;
15894
15895     case IX86_BUILTIN_STOREHPS:
15896     case IX86_BUILTIN_STORELPS:
15897       icode = (fcode == IX86_BUILTIN_STOREHPS ? CODE_FOR_sse_storehps
15898                : CODE_FOR_sse_storelps);
15899       arg0 = TREE_VALUE (arglist);
15900       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15901       op0 = expand_normal (arg0);
15902       op1 = expand_normal (arg1);
15903       mode0 = insn_data[icode].operand[0].mode;
15904       mode1 = insn_data[icode].operand[1].mode;
15905
15906       op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
15907       op1 = force_reg (mode1, op1);
15908
15909       pat = GEN_FCN (icode) (op0, op1);
15910       if (! pat)
15911         return 0;
15912       emit_insn (pat);
15913       return const0_rtx;
15914
15915     case IX86_BUILTIN_MOVNTPS:
15916       return ix86_expand_store_builtin (CODE_FOR_sse_movntv4sf, arglist);
15917     case IX86_BUILTIN_MOVNTQ:
15918       return ix86_expand_store_builtin (CODE_FOR_sse_movntdi, arglist);
15919
15920     case IX86_BUILTIN_LDMXCSR:
15921       op0 = expand_normal (TREE_VALUE (arglist));
15922       target = assign_386_stack_local (SImode, SLOT_TEMP);
15923       emit_move_insn (target, op0);
15924       emit_insn (gen_sse_ldmxcsr (target));
15925       return 0;
15926
15927     case IX86_BUILTIN_STMXCSR:
15928       target = assign_386_stack_local (SImode, SLOT_TEMP);
15929       emit_insn (gen_sse_stmxcsr (target));
15930       return copy_to_mode_reg (SImode, target);
15931
15932     case IX86_BUILTIN_SHUFPS:
15933     case IX86_BUILTIN_SHUFPD:
15934       icode = (fcode == IX86_BUILTIN_SHUFPS
15935                ? CODE_FOR_sse_shufps
15936                : CODE_FOR_sse2_shufpd);
15937       arg0 = TREE_VALUE (arglist);
15938       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15939       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
15940       op0 = expand_normal (arg0);
15941       op1 = expand_normal (arg1);
15942       op2 = expand_normal (arg2);
15943       tmode = insn_data[icode].operand[0].mode;
15944       mode0 = insn_data[icode].operand[1].mode;
15945       mode1 = insn_data[icode].operand[2].mode;
15946       mode2 = insn_data[icode].operand[3].mode;
15947
15948       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15949         op0 = copy_to_mode_reg (mode0, op0);
15950       if ((optimize && !register_operand (op1, mode1))
15951           || !(*insn_data[icode].operand[2].predicate) (op1, mode1))
15952         op1 = copy_to_mode_reg (mode1, op1);
15953       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
15954         {
15955           /* @@@ better error message */
15956           error ("mask must be an immediate");
15957           return gen_reg_rtx (tmode);
15958         }
15959       if (optimize || target == 0
15960           || GET_MODE (target) != tmode
15961           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15962         target = gen_reg_rtx (tmode);
15963       pat = GEN_FCN (icode) (target, op0, op1, op2);
15964       if (! pat)
15965         return 0;
15966       emit_insn (pat);
15967       return target;
15968
15969     case IX86_BUILTIN_PSHUFW:
15970     case IX86_BUILTIN_PSHUFD:
15971     case IX86_BUILTIN_PSHUFHW:
15972     case IX86_BUILTIN_PSHUFLW:
15973       icode = (  fcode == IX86_BUILTIN_PSHUFHW ? CODE_FOR_sse2_pshufhw
15974                : fcode == IX86_BUILTIN_PSHUFLW ? CODE_FOR_sse2_pshuflw
15975                : fcode == IX86_BUILTIN_PSHUFD ? CODE_FOR_sse2_pshufd
15976                : CODE_FOR_mmx_pshufw);
15977       arg0 = TREE_VALUE (arglist);
15978       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15979       op0 = expand_normal (arg0);
15980       op1 = expand_normal (arg1);
15981       tmode = insn_data[icode].operand[0].mode;
15982       mode1 = insn_data[icode].operand[1].mode;
15983       mode2 = insn_data[icode].operand[2].mode;
15984
15985       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
15986         op0 = copy_to_mode_reg (mode1, op0);
15987       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
15988         {
15989           /* @@@ better error message */
15990           error ("mask must be an immediate");
15991           return const0_rtx;
15992         }
15993       if (target == 0
15994           || GET_MODE (target) != tmode
15995           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15996         target = gen_reg_rtx (tmode);
15997       pat = GEN_FCN (icode) (target, op0, op1);
15998       if (! pat)
15999         return 0;
16000       emit_insn (pat);
16001       return target;
16002
16003     case IX86_BUILTIN_PSLLDQI128:
16004     case IX86_BUILTIN_PSRLDQI128:
16005       icode = (  fcode == IX86_BUILTIN_PSLLDQI128 ? CODE_FOR_sse2_ashlti3
16006                : CODE_FOR_sse2_lshrti3);
16007       arg0 = TREE_VALUE (arglist);
16008       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16009       op0 = expand_normal (arg0);
16010       op1 = expand_normal (arg1);
16011       tmode = insn_data[icode].operand[0].mode;
16012       mode1 = insn_data[icode].operand[1].mode;
16013       mode2 = insn_data[icode].operand[2].mode;
16014
16015       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
16016         {
16017           op0 = copy_to_reg (op0);
16018           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
16019         }
16020       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
16021         {
16022           error ("shift must be an immediate");
16023           return const0_rtx;
16024         }
16025       target = gen_reg_rtx (V2DImode);
16026       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, V2DImode, 0), op0, op1);
16027       if (! pat)
16028         return 0;
16029       emit_insn (pat);
16030       return target;
16031
16032     case IX86_BUILTIN_FEMMS:
16033       emit_insn (gen_mmx_femms ());
16034       return NULL_RTX;
16035
16036     case IX86_BUILTIN_PAVGUSB:
16037       return ix86_expand_binop_builtin (CODE_FOR_mmx_uavgv8qi3, arglist, target);
16038
16039     case IX86_BUILTIN_PF2ID:
16040       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2id, arglist, target, 0);
16041
16042     case IX86_BUILTIN_PFACC:
16043       return ix86_expand_binop_builtin (CODE_FOR_mmx_haddv2sf3, arglist, target);
16044
16045     case IX86_BUILTIN_PFADD:
16046      return ix86_expand_binop_builtin (CODE_FOR_mmx_addv2sf3, arglist, target);
16047
16048     case IX86_BUILTIN_PFCMPEQ:
16049       return ix86_expand_binop_builtin (CODE_FOR_mmx_eqv2sf3, arglist, target);
16050
16051     case IX86_BUILTIN_PFCMPGE:
16052       return ix86_expand_binop_builtin (CODE_FOR_mmx_gev2sf3, arglist, target);
16053
16054     case IX86_BUILTIN_PFCMPGT:
16055       return ix86_expand_binop_builtin (CODE_FOR_mmx_gtv2sf3, arglist, target);
16056
16057     case IX86_BUILTIN_PFMAX:
16058       return ix86_expand_binop_builtin (CODE_FOR_mmx_smaxv2sf3, arglist, target);
16059
16060     case IX86_BUILTIN_PFMIN:
16061       return ix86_expand_binop_builtin (CODE_FOR_mmx_sminv2sf3, arglist, target);
16062
16063     case IX86_BUILTIN_PFMUL:
16064       return ix86_expand_binop_builtin (CODE_FOR_mmx_mulv2sf3, arglist, target);
16065
16066     case IX86_BUILTIN_PFRCP:
16067       return ix86_expand_unop_builtin (CODE_FOR_mmx_rcpv2sf2, arglist, target, 0);
16068
16069     case IX86_BUILTIN_PFRCPIT1:
16070       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit1v2sf3, arglist, target);
16071
16072     case IX86_BUILTIN_PFRCPIT2:
16073       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit2v2sf3, arglist, target);
16074
16075     case IX86_BUILTIN_PFRSQIT1:
16076       return ix86_expand_binop_builtin (CODE_FOR_mmx_rsqit1v2sf3, arglist, target);
16077
16078     case IX86_BUILTIN_PFRSQRT:
16079       return ix86_expand_unop_builtin (CODE_FOR_mmx_rsqrtv2sf2, arglist, target, 0);
16080
16081     case IX86_BUILTIN_PFSUB:
16082       return ix86_expand_binop_builtin (CODE_FOR_mmx_subv2sf3, arglist, target);
16083
16084     case IX86_BUILTIN_PFSUBR:
16085       return ix86_expand_binop_builtin (CODE_FOR_mmx_subrv2sf3, arglist, target);
16086
16087     case IX86_BUILTIN_PI2FD:
16088       return ix86_expand_unop_builtin (CODE_FOR_mmx_floatv2si2, arglist, target, 0);
16089
16090     case IX86_BUILTIN_PMULHRW:
16091       return ix86_expand_binop_builtin (CODE_FOR_mmx_pmulhrwv4hi3, arglist, target);
16092
16093     case IX86_BUILTIN_PF2IW:
16094       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2iw, arglist, target, 0);
16095
16096     case IX86_BUILTIN_PFNACC:
16097       return ix86_expand_binop_builtin (CODE_FOR_mmx_hsubv2sf3, arglist, target);
16098
16099     case IX86_BUILTIN_PFPNACC:
16100       return ix86_expand_binop_builtin (CODE_FOR_mmx_addsubv2sf3, arglist, target);
16101
16102     case IX86_BUILTIN_PI2FW:
16103       return ix86_expand_unop_builtin (CODE_FOR_mmx_pi2fw, arglist, target, 0);
16104
16105     case IX86_BUILTIN_PSWAPDSI:
16106       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2si2, arglist, target, 0);
16107
16108     case IX86_BUILTIN_PSWAPDSF:
16109       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2sf2, arglist, target, 0);
16110
16111     case IX86_BUILTIN_SQRTSD:
16112       return ix86_expand_unop1_builtin (CODE_FOR_sse2_vmsqrtv2df2, arglist, target);
16113     case IX86_BUILTIN_LOADUPD:
16114       return ix86_expand_unop_builtin (CODE_FOR_sse2_movupd, arglist, target, 1);
16115     case IX86_BUILTIN_STOREUPD:
16116       return ix86_expand_store_builtin (CODE_FOR_sse2_movupd, arglist);
16117
16118     case IX86_BUILTIN_MFENCE:
16119         emit_insn (gen_sse2_mfence ());
16120         return 0;
16121     case IX86_BUILTIN_LFENCE:
16122         emit_insn (gen_sse2_lfence ());
16123         return 0;
16124
16125     case IX86_BUILTIN_CLFLUSH:
16126         arg0 = TREE_VALUE (arglist);
16127         op0 = expand_normal (arg0);
16128         icode = CODE_FOR_sse2_clflush;
16129         if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
16130             op0 = copy_to_mode_reg (Pmode, op0);
16131
16132         emit_insn (gen_sse2_clflush (op0));
16133         return 0;
16134
16135     case IX86_BUILTIN_MOVNTPD:
16136       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2df, arglist);
16137     case IX86_BUILTIN_MOVNTDQ:
16138       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2di, arglist);
16139     case IX86_BUILTIN_MOVNTI:
16140       return ix86_expand_store_builtin (CODE_FOR_sse2_movntsi, arglist);
16141
16142     case IX86_BUILTIN_LOADDQU:
16143       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqu, arglist, target, 1);
16144     case IX86_BUILTIN_STOREDQU:
16145       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqu, arglist);
16146
16147     case IX86_BUILTIN_MONITOR:
16148       arg0 = TREE_VALUE (arglist);
16149       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16150       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
16151       op0 = expand_normal (arg0);
16152       op1 = expand_normal (arg1);
16153       op2 = expand_normal (arg2);
16154       if (!REG_P (op0))
16155         op0 = copy_to_mode_reg (SImode, op0);
16156       if (!REG_P (op1))
16157         op1 = copy_to_mode_reg (SImode, op1);
16158       if (!REG_P (op2))
16159         op2 = copy_to_mode_reg (SImode, op2);
16160       emit_insn (gen_sse3_monitor (op0, op1, op2));
16161       return 0;
16162
16163     case IX86_BUILTIN_MWAIT:
16164       arg0 = TREE_VALUE (arglist);
16165       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16166       op0 = expand_normal (arg0);
16167       op1 = expand_normal (arg1);
16168       if (!REG_P (op0))
16169         op0 = copy_to_mode_reg (SImode, op0);
16170       if (!REG_P (op1))
16171         op1 = copy_to_mode_reg (SImode, op1);
16172       emit_insn (gen_sse3_mwait (op0, op1));
16173       return 0;
16174
16175     case IX86_BUILTIN_LDDQU:
16176       return ix86_expand_unop_builtin (CODE_FOR_sse3_lddqu, arglist,
16177                                        target, 1);
16178
16179     case IX86_BUILTIN_VEC_INIT_V2SI:
16180     case IX86_BUILTIN_VEC_INIT_V4HI:
16181     case IX86_BUILTIN_VEC_INIT_V8QI:
16182       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), arglist, target);
16183
16184     case IX86_BUILTIN_VEC_EXT_V2DF:
16185     case IX86_BUILTIN_VEC_EXT_V2DI:
16186     case IX86_BUILTIN_VEC_EXT_V4SF:
16187     case IX86_BUILTIN_VEC_EXT_V4SI:
16188     case IX86_BUILTIN_VEC_EXT_V8HI:
16189     case IX86_BUILTIN_VEC_EXT_V2SI:
16190     case IX86_BUILTIN_VEC_EXT_V4HI:
16191       return ix86_expand_vec_ext_builtin (arglist, target);
16192
16193     case IX86_BUILTIN_VEC_SET_V8HI:
16194     case IX86_BUILTIN_VEC_SET_V4HI:
16195       return ix86_expand_vec_set_builtin (arglist);
16196
16197     default:
16198       break;
16199     }
16200
16201   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
16202     if (d->code == fcode)
16203       {
16204         /* Compares are treated specially.  */
16205         if (d->icode == CODE_FOR_sse_maskcmpv4sf3
16206             || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3
16207             || d->icode == CODE_FOR_sse2_maskcmpv2df3
16208             || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
16209           return ix86_expand_sse_compare (d, arglist, target);
16210
16211         return ix86_expand_binop_builtin (d->icode, arglist, target);
16212       }
16213
16214   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
16215     if (d->code == fcode)
16216       return ix86_expand_unop_builtin (d->icode, arglist, target, 0);
16217
16218   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
16219     if (d->code == fcode)
16220       return ix86_expand_sse_comi (d, arglist, target);
16221
16222   gcc_unreachable ();
16223 }
16224
16225 /* Expand an expression EXP that calls a built-in library function,
16226    with result going to TARGET if that's convenient
16227    (and in mode MODE if that's convenient).
16228    SUBTARGET may be used as the target for computing one of EXP's operands.
16229    IGNORE is nonzero if the value is to be ignored.  */
16230
16231 static rtx
16232 ix86_expand_library_builtin (tree exp, rtx target,
16233                              rtx subtarget ATTRIBUTE_UNUSED,
16234                              enum machine_mode mode ATTRIBUTE_UNUSED,
16235                              int ignore)
16236 {
16237   enum built_in_function fncode;
16238   tree fndecl, newfn, call;
16239
16240   /* Try expanding builtin math functions to the SSE2 ABI variants.  */
16241   if (!TARGET_SSELIBM)
16242       return NULL_RTX;
16243
16244   fncode = builtin_mathfn_code (exp);
16245   if (!ix86_builtin_function_variants [(int)fncode])
16246     return NULL_RTX;
16247
16248   fndecl = get_callee_fndecl (exp);
16249   if (DECL_RTL_SET_P (fndecl))
16250     return NULL_RTX;
16251
16252   /* Build the redirected call and expand it.  */
16253   newfn = ix86_builtin_function_variants [(int)fncode];
16254   call = build_function_call_expr (newfn, TREE_OPERAND (exp, 1));
16255   return expand_call (call, target, ignore);
16256 }
16257
16258 /* Store OPERAND to the memory after reload is completed.  This means
16259    that we can't easily use assign_stack_local.  */
16260 rtx
16261 ix86_force_to_memory (enum machine_mode mode, rtx operand)
16262 {
16263   rtx result;
16264   
16265   gcc_assert (reload_completed);
16266   if (TARGET_RED_ZONE)
16267     {
16268       result = gen_rtx_MEM (mode,
16269                             gen_rtx_PLUS (Pmode,
16270                                           stack_pointer_rtx,
16271                                           GEN_INT (-RED_ZONE_SIZE)));
16272       emit_move_insn (result, operand);
16273     }
16274   else if (!TARGET_RED_ZONE && TARGET_64BIT)
16275     {
16276       switch (mode)
16277         {
16278         case HImode:
16279         case SImode:
16280           operand = gen_lowpart (DImode, operand);
16281           /* FALLTHRU */
16282         case DImode:
16283           emit_insn (
16284                       gen_rtx_SET (VOIDmode,
16285                                    gen_rtx_MEM (DImode,
16286                                                 gen_rtx_PRE_DEC (DImode,
16287                                                         stack_pointer_rtx)),
16288                                    operand));
16289           break;
16290         default:
16291           gcc_unreachable ();
16292         }
16293       result = gen_rtx_MEM (mode, stack_pointer_rtx);
16294     }
16295   else
16296     {
16297       switch (mode)
16298         {
16299         case DImode:
16300           {
16301             rtx operands[2];
16302             split_di (&operand, 1, operands, operands + 1);
16303             emit_insn (
16304                         gen_rtx_SET (VOIDmode,
16305                                      gen_rtx_MEM (SImode,
16306                                                   gen_rtx_PRE_DEC (Pmode,
16307                                                         stack_pointer_rtx)),
16308                                      operands[1]));
16309             emit_insn (
16310                         gen_rtx_SET (VOIDmode,
16311                                      gen_rtx_MEM (SImode,
16312                                                   gen_rtx_PRE_DEC (Pmode,
16313                                                         stack_pointer_rtx)),
16314                                      operands[0]));
16315           }
16316           break;
16317         case HImode:
16318           /* Store HImodes as SImodes.  */
16319           operand = gen_lowpart (SImode, operand);
16320           /* FALLTHRU */
16321         case SImode:
16322           emit_insn (
16323                       gen_rtx_SET (VOIDmode,
16324                                    gen_rtx_MEM (GET_MODE (operand),
16325                                                 gen_rtx_PRE_DEC (SImode,
16326                                                         stack_pointer_rtx)),
16327                                    operand));
16328           break;
16329         default:
16330           gcc_unreachable ();
16331         }
16332       result = gen_rtx_MEM (mode, stack_pointer_rtx);
16333     }
16334   return result;
16335 }
16336
16337 /* Free operand from the memory.  */
16338 void
16339 ix86_free_from_memory (enum machine_mode mode)
16340 {
16341   if (!TARGET_RED_ZONE)
16342     {
16343       int size;
16344
16345       if (mode == DImode || TARGET_64BIT)
16346         size = 8;
16347       else
16348         size = 4;
16349       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
16350          to pop or add instruction if registers are available.  */
16351       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
16352                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
16353                                             GEN_INT (size))));
16354     }
16355 }
16356
16357 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
16358    QImode must go into class Q_REGS.
16359    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
16360    movdf to do mem-to-mem moves through integer regs.  */
16361 enum reg_class
16362 ix86_preferred_reload_class (rtx x, enum reg_class class)
16363 {
16364   /* We're only allowed to return a subclass of CLASS.  Many of the 
16365      following checks fail for NO_REGS, so eliminate that early.  */
16366   if (class == NO_REGS)
16367     return NO_REGS;
16368
16369   /* All classes can load zeros.  */
16370   if (x == CONST0_RTX (GET_MODE (x)))
16371     return class;
16372
16373   /* Floating-point constants need more complex checks.  */
16374   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
16375     {
16376       /* General regs can load everything.  */
16377       if (reg_class_subset_p (class, GENERAL_REGS))
16378         return class;
16379
16380       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
16381          zero above.  We only want to wind up preferring 80387 registers if
16382          we plan on doing computation with them.  */
16383       if (TARGET_80387
16384           && (TARGET_MIX_SSE_I387 
16385               || !(TARGET_SSE_MATH && SSE_FLOAT_MODE_P (GET_MODE (x))))
16386           && standard_80387_constant_p (x))
16387         {
16388           /* Limit class to non-sse.  */
16389           if (class == FLOAT_SSE_REGS)
16390             return FLOAT_REGS;
16391           if (class == FP_TOP_SSE_REGS)
16392             return FP_TOP_REG;
16393           if (class == FP_SECOND_SSE_REGS)
16394             return FP_SECOND_REG;
16395           if (class == FLOAT_INT_REGS || class == FLOAT_REGS)
16396             return class;
16397         }
16398
16399       return NO_REGS;
16400     }
16401   if (MAYBE_MMX_CLASS_P (class) && CONSTANT_P (x))
16402     return NO_REGS;
16403   if (MAYBE_SSE_CLASS_P (class) && CONSTANT_P (x))
16404     return NO_REGS;
16405
16406   /* Generally when we see PLUS here, it's the function invariant
16407      (plus soft-fp const_int).  Which can only be computed into general
16408      regs.  */
16409   if (GET_CODE (x) == PLUS)
16410     return reg_class_subset_p (class, GENERAL_REGS) ? class : NO_REGS;
16411
16412   /* QImode constants are easy to load, but non-constant QImode data
16413      must go into Q_REGS.  */
16414   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
16415     {
16416       if (reg_class_subset_p (class, Q_REGS))
16417         return class;
16418       if (reg_class_subset_p (Q_REGS, class))
16419         return Q_REGS;
16420       return NO_REGS;
16421     }
16422
16423   return class;
16424 }
16425
16426 /* If we are copying between general and FP registers, we need a memory
16427    location. The same is true for SSE and MMX registers.
16428
16429    The macro can't work reliably when one of the CLASSES is class containing
16430    registers from multiple units (SSE, MMX, integer).  We avoid this by never
16431    combining those units in single alternative in the machine description.
16432    Ensure that this constraint holds to avoid unexpected surprises.
16433
16434    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
16435    enforce these sanity checks.  */
16436
16437 int
16438 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
16439                               enum machine_mode mode, int strict)
16440 {
16441   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
16442       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
16443       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
16444       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
16445       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
16446       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
16447     {
16448       gcc_assert (!strict);
16449       return true;
16450     }
16451
16452   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
16453     return true;
16454
16455   /* ??? This is a lie.  We do have moves between mmx/general, and for
16456      mmx/sse2.  But by saying we need secondary memory we discourage the
16457      register allocator from using the mmx registers unless needed.  */
16458   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
16459     return true;
16460
16461   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
16462     {
16463       /* SSE1 doesn't have any direct moves from other classes.  */
16464       if (!TARGET_SSE2)
16465         return true;
16466
16467       /* If the target says that inter-unit moves are more expensive 
16468          than moving through memory, then don't generate them.  */
16469       if (!TARGET_INTER_UNIT_MOVES && !optimize_size)
16470         return true;
16471
16472       /* Between SSE and general, we have moves no larger than word size.  */
16473       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
16474         return true;
16475
16476       /* ??? For the cost of one register reformat penalty, we could use
16477          the same instructions to move SFmode and DFmode data, but the 
16478          relevant move patterns don't support those alternatives.  */
16479       if (mode == SFmode || mode == DFmode)
16480         return true;
16481     }
16482
16483   return false;
16484 }
16485
16486 /* Return true if the registers in CLASS cannot represent the change from
16487    modes FROM to TO.  */
16488
16489 bool
16490 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
16491                                enum reg_class class)
16492 {
16493   if (from == to)
16494     return false;
16495
16496   /* x87 registers can't do subreg at all, as all values are reformatted
16497      to extended precision.  */
16498   if (MAYBE_FLOAT_CLASS_P (class))
16499     return true;
16500
16501   if (MAYBE_SSE_CLASS_P (class) || MAYBE_MMX_CLASS_P (class))
16502     {
16503       /* Vector registers do not support QI or HImode loads.  If we don't
16504          disallow a change to these modes, reload will assume it's ok to
16505          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
16506          the vec_dupv4hi pattern.  */
16507       if (GET_MODE_SIZE (from) < 4)
16508         return true;
16509
16510       /* Vector registers do not support subreg with nonzero offsets, which
16511          are otherwise valid for integer registers.  Since we can't see 
16512          whether we have a nonzero offset from here, prohibit all
16513          nonparadoxical subregs changing size.  */
16514       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
16515         return true;
16516     }
16517
16518   return false;
16519 }
16520
16521 /* Return the cost of moving data from a register in class CLASS1 to
16522    one in class CLASS2.
16523
16524    It is not required that the cost always equal 2 when FROM is the same as TO;
16525    on some machines it is expensive to move between registers if they are not
16526    general registers.  */
16527
16528 int
16529 ix86_register_move_cost (enum machine_mode mode, enum reg_class class1,
16530                          enum reg_class class2)
16531 {
16532   /* In case we require secondary memory, compute cost of the store followed
16533      by load.  In order to avoid bad register allocation choices, we need
16534      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
16535
16536   if (ix86_secondary_memory_needed (class1, class2, mode, 0))
16537     {
16538       int cost = 1;
16539
16540       cost += MAX (MEMORY_MOVE_COST (mode, class1, 0),
16541                    MEMORY_MOVE_COST (mode, class1, 1));
16542       cost += MAX (MEMORY_MOVE_COST (mode, class2, 0),
16543                    MEMORY_MOVE_COST (mode, class2, 1));
16544
16545       /* In case of copying from general_purpose_register we may emit multiple
16546          stores followed by single load causing memory size mismatch stall.
16547          Count this as arbitrarily high cost of 20.  */
16548       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
16549         cost += 20;
16550
16551       /* In the case of FP/MMX moves, the registers actually overlap, and we
16552          have to switch modes in order to treat them differently.  */
16553       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
16554           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
16555         cost += 20;
16556
16557       return cost;
16558     }
16559
16560   /* Moves between SSE/MMX and integer unit are expensive.  */
16561   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
16562       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
16563     return ix86_cost->mmxsse_to_integer;
16564   if (MAYBE_FLOAT_CLASS_P (class1))
16565     return ix86_cost->fp_move;
16566   if (MAYBE_SSE_CLASS_P (class1))
16567     return ix86_cost->sse_move;
16568   if (MAYBE_MMX_CLASS_P (class1))
16569     return ix86_cost->mmx_move;
16570   return 2;
16571 }
16572
16573 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
16574
16575 bool
16576 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
16577 {
16578   /* Flags and only flags can only hold CCmode values.  */
16579   if (CC_REGNO_P (regno))
16580     return GET_MODE_CLASS (mode) == MODE_CC;
16581   if (GET_MODE_CLASS (mode) == MODE_CC
16582       || GET_MODE_CLASS (mode) == MODE_RANDOM
16583       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
16584     return 0;
16585   if (FP_REGNO_P (regno))
16586     return VALID_FP_MODE_P (mode);
16587   if (SSE_REGNO_P (regno))
16588     {
16589       /* We implement the move patterns for all vector modes into and
16590          out of SSE registers, even when no operation instructions
16591          are available.  */
16592       return (VALID_SSE_REG_MODE (mode)
16593               || VALID_SSE2_REG_MODE (mode)
16594               || VALID_MMX_REG_MODE (mode)
16595               || VALID_MMX_REG_MODE_3DNOW (mode));
16596     }
16597   if (MMX_REGNO_P (regno))
16598     {
16599       /* We implement the move patterns for 3DNOW modes even in MMX mode,
16600          so if the register is available at all, then we can move data of
16601          the given mode into or out of it.  */
16602       return (VALID_MMX_REG_MODE (mode)
16603               || VALID_MMX_REG_MODE_3DNOW (mode));
16604     }
16605
16606   if (mode == QImode)
16607     {
16608       /* Take care for QImode values - they can be in non-QI regs,
16609          but then they do cause partial register stalls.  */
16610       if (regno < 4 || TARGET_64BIT)
16611         return 1;
16612       if (!TARGET_PARTIAL_REG_STALL)
16613         return 1;
16614       return reload_in_progress || reload_completed;
16615     }
16616   /* We handle both integer and floats in the general purpose registers.  */
16617   else if (VALID_INT_MODE_P (mode))
16618     return 1;
16619   else if (VALID_FP_MODE_P (mode))
16620     return 1;
16621   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
16622      on to use that value in smaller contexts, this can easily force a 
16623      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
16624      supporting DImode, allow it.  */
16625   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
16626     return 1;
16627
16628   return 0;
16629 }
16630
16631 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a 
16632    tieable integer mode.  */
16633
16634 static bool
16635 ix86_tieable_integer_mode_p (enum machine_mode mode)
16636 {
16637   switch (mode)
16638     {
16639     case HImode:
16640     case SImode:
16641       return true;
16642
16643     case QImode:
16644       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
16645
16646     case DImode:
16647       return TARGET_64BIT;
16648
16649     default:
16650       return false;
16651     }
16652 }
16653
16654 /* Return true if MODE1 is accessible in a register that can hold MODE2
16655    without copying.  That is, all register classes that can hold MODE2
16656    can also hold MODE1.  */
16657
16658 bool
16659 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
16660 {
16661   if (mode1 == mode2)
16662     return true;
16663
16664   if (ix86_tieable_integer_mode_p (mode1)
16665       && ix86_tieable_integer_mode_p (mode2))
16666     return true;
16667
16668   /* MODE2 being XFmode implies fp stack or general regs, which means we
16669      can tie any smaller floating point modes to it.  Note that we do not
16670      tie this with TFmode.  */
16671   if (mode2 == XFmode)
16672     return mode1 == SFmode || mode1 == DFmode;
16673
16674   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
16675      that we can tie it with SFmode.  */
16676   if (mode2 == DFmode)
16677     return mode1 == SFmode;
16678
16679   /* If MODE2 is only appropriate for an SSE register, then tie with 
16680      any other mode acceptable to SSE registers.  */
16681   if (GET_MODE_SIZE (mode2) >= 8
16682       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
16683     return ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1);
16684
16685   /* If MODE2 is appropriate for an MMX (or SSE) register, then tie
16686      with any other mode acceptable to MMX registers.  */
16687   if (GET_MODE_SIZE (mode2) == 8
16688       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
16689     return ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1);
16690
16691   return false;
16692 }
16693
16694 /* Return the cost of moving data of mode M between a
16695    register and memory.  A value of 2 is the default; this cost is
16696    relative to those in `REGISTER_MOVE_COST'.
16697
16698    If moving between registers and memory is more expensive than
16699    between two registers, you should define this macro to express the
16700    relative cost.
16701
16702    Model also increased moving costs of QImode registers in non
16703    Q_REGS classes.
16704  */
16705 int
16706 ix86_memory_move_cost (enum machine_mode mode, enum reg_class class, int in)
16707 {
16708   if (FLOAT_CLASS_P (class))
16709     {
16710       int index;
16711       switch (mode)
16712         {
16713           case SFmode:
16714             index = 0;
16715             break;
16716           case DFmode:
16717             index = 1;
16718             break;
16719           case XFmode:
16720             index = 2;
16721             break;
16722           default:
16723             return 100;
16724         }
16725       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
16726     }
16727   if (SSE_CLASS_P (class))
16728     {
16729       int index;
16730       switch (GET_MODE_SIZE (mode))
16731         {
16732           case 4:
16733             index = 0;
16734             break;
16735           case 8:
16736             index = 1;
16737             break;
16738           case 16:
16739             index = 2;
16740             break;
16741           default:
16742             return 100;
16743         }
16744       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
16745     }
16746   if (MMX_CLASS_P (class))
16747     {
16748       int index;
16749       switch (GET_MODE_SIZE (mode))
16750         {
16751           case 4:
16752             index = 0;
16753             break;
16754           case 8:
16755             index = 1;
16756             break;
16757           default:
16758             return 100;
16759         }
16760       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
16761     }
16762   switch (GET_MODE_SIZE (mode))
16763     {
16764       case 1:
16765         if (in)
16766           return (Q_CLASS_P (class) ? ix86_cost->int_load[0]
16767                   : ix86_cost->movzbl_load);
16768         else
16769           return (Q_CLASS_P (class) ? ix86_cost->int_store[0]
16770                   : ix86_cost->int_store[0] + 4);
16771         break;
16772       case 2:
16773         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
16774       default:
16775         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
16776         if (mode == TFmode)
16777           mode = XFmode;
16778         return ((in ? ix86_cost->int_load[2] : ix86_cost->int_store[2])
16779                 * (((int) GET_MODE_SIZE (mode)
16780                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
16781     }
16782 }
16783
16784 /* Compute a (partial) cost for rtx X.  Return true if the complete
16785    cost has been computed, and false if subexpressions should be
16786    scanned.  In either case, *TOTAL contains the cost result.  */
16787
16788 static bool
16789 ix86_rtx_costs (rtx x, int code, int outer_code, int *total)
16790 {
16791   enum machine_mode mode = GET_MODE (x);
16792
16793   switch (code)
16794     {
16795     case CONST_INT:
16796     case CONST:
16797     case LABEL_REF:
16798     case SYMBOL_REF:
16799       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
16800         *total = 3;
16801       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
16802         *total = 2;
16803       else if (flag_pic && SYMBOLIC_CONST (x)
16804                && (!TARGET_64BIT
16805                    || (!GET_CODE (x) != LABEL_REF
16806                        && (GET_CODE (x) != SYMBOL_REF
16807                            || !SYMBOL_REF_LOCAL_P (x)))))
16808         *total = 1;
16809       else
16810         *total = 0;
16811       return true;
16812
16813     case CONST_DOUBLE:
16814       if (mode == VOIDmode)
16815         *total = 0;
16816       else
16817         switch (standard_80387_constant_p (x))
16818           {
16819           case 1: /* 0.0 */
16820             *total = 1;
16821             break;
16822           default: /* Other constants */
16823             *total = 2;
16824             break;
16825           case 0:
16826           case -1:
16827             /* Start with (MEM (SYMBOL_REF)), since that's where
16828                it'll probably end up.  Add a penalty for size.  */
16829             *total = (COSTS_N_INSNS (1)
16830                       + (flag_pic != 0 && !TARGET_64BIT)
16831                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
16832             break;
16833           }
16834       return true;
16835
16836     case ZERO_EXTEND:
16837       /* The zero extensions is often completely free on x86_64, so make
16838          it as cheap as possible.  */
16839       if (TARGET_64BIT && mode == DImode
16840           && GET_MODE (XEXP (x, 0)) == SImode)
16841         *total = 1;
16842       else if (TARGET_ZERO_EXTEND_WITH_AND)
16843         *total = ix86_cost->add;
16844       else
16845         *total = ix86_cost->movzx;
16846       return false;
16847
16848     case SIGN_EXTEND:
16849       *total = ix86_cost->movsx;
16850       return false;
16851
16852     case ASHIFT:
16853       if (GET_CODE (XEXP (x, 1)) == CONST_INT
16854           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
16855         {
16856           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
16857           if (value == 1)
16858             {
16859               *total = ix86_cost->add;
16860               return false;
16861             }
16862           if ((value == 2 || value == 3)
16863               && ix86_cost->lea <= ix86_cost->shift_const)
16864             {
16865               *total = ix86_cost->lea;
16866               return false;
16867             }
16868         }
16869       /* FALLTHRU */
16870
16871     case ROTATE:
16872     case ASHIFTRT:
16873     case LSHIFTRT:
16874     case ROTATERT:
16875       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
16876         {
16877           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
16878             {
16879               if (INTVAL (XEXP (x, 1)) > 32)
16880                 *total = ix86_cost->shift_const + COSTS_N_INSNS (2);
16881               else
16882                 *total = ix86_cost->shift_const * 2;
16883             }
16884           else
16885             {
16886               if (GET_CODE (XEXP (x, 1)) == AND)
16887                 *total = ix86_cost->shift_var * 2;
16888               else
16889                 *total = ix86_cost->shift_var * 6 + COSTS_N_INSNS (2);
16890             }
16891         }
16892       else
16893         {
16894           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
16895             *total = ix86_cost->shift_const;
16896           else
16897             *total = ix86_cost->shift_var;
16898         }
16899       return false;
16900
16901     case MULT:
16902       if (FLOAT_MODE_P (mode))
16903         {
16904           *total = ix86_cost->fmul;
16905           return false;
16906         }
16907       else
16908         {
16909           rtx op0 = XEXP (x, 0);
16910           rtx op1 = XEXP (x, 1);
16911           int nbits;
16912           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
16913             {
16914               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
16915               for (nbits = 0; value != 0; value &= value - 1)
16916                 nbits++;
16917             }
16918           else
16919             /* This is arbitrary.  */
16920             nbits = 7;
16921
16922           /* Compute costs correctly for widening multiplication.  */
16923           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op1) == ZERO_EXTEND)
16924               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
16925                  == GET_MODE_SIZE (mode))
16926             {
16927               int is_mulwiden = 0;
16928               enum machine_mode inner_mode = GET_MODE (op0);
16929
16930               if (GET_CODE (op0) == GET_CODE (op1))
16931                 is_mulwiden = 1, op1 = XEXP (op1, 0);
16932               else if (GET_CODE (op1) == CONST_INT)
16933                 {
16934                   if (GET_CODE (op0) == SIGN_EXTEND)
16935                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
16936                                   == INTVAL (op1);
16937                   else
16938                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
16939                 }
16940
16941               if (is_mulwiden)
16942                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
16943             }
16944
16945           *total = (ix86_cost->mult_init[MODE_INDEX (mode)]
16946                     + nbits * ix86_cost->mult_bit
16947                     + rtx_cost (op0, outer_code) + rtx_cost (op1, outer_code));
16948
16949           return true;
16950         }
16951
16952     case DIV:
16953     case UDIV:
16954     case MOD:
16955     case UMOD:
16956       if (FLOAT_MODE_P (mode))
16957         *total = ix86_cost->fdiv;
16958       else
16959         *total = ix86_cost->divide[MODE_INDEX (mode)];
16960       return false;
16961
16962     case PLUS:
16963       if (FLOAT_MODE_P (mode))
16964         *total = ix86_cost->fadd;
16965       else if (GET_MODE_CLASS (mode) == MODE_INT
16966                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
16967         {
16968           if (GET_CODE (XEXP (x, 0)) == PLUS
16969               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
16970               && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
16971               && CONSTANT_P (XEXP (x, 1)))
16972             {
16973               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
16974               if (val == 2 || val == 4 || val == 8)
16975                 {
16976                   *total = ix86_cost->lea;
16977                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
16978                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
16979                                       outer_code);
16980                   *total += rtx_cost (XEXP (x, 1), outer_code);
16981                   return true;
16982                 }
16983             }
16984           else if (GET_CODE (XEXP (x, 0)) == MULT
16985                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
16986             {
16987               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
16988               if (val == 2 || val == 4 || val == 8)
16989                 {
16990                   *total = ix86_cost->lea;
16991                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
16992                   *total += rtx_cost (XEXP (x, 1), outer_code);
16993                   return true;
16994                 }
16995             }
16996           else if (GET_CODE (XEXP (x, 0)) == PLUS)
16997             {
16998               *total = ix86_cost->lea;
16999               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
17000               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
17001               *total += rtx_cost (XEXP (x, 1), outer_code);
17002               return true;
17003             }
17004         }
17005       /* FALLTHRU */
17006
17007     case MINUS:
17008       if (FLOAT_MODE_P (mode))
17009         {
17010           *total = ix86_cost->fadd;
17011           return false;
17012         }
17013       /* FALLTHRU */
17014
17015     case AND:
17016     case IOR:
17017     case XOR:
17018       if (!TARGET_64BIT && mode == DImode)
17019         {
17020           *total = (ix86_cost->add * 2
17021                     + (rtx_cost (XEXP (x, 0), outer_code)
17022                        << (GET_MODE (XEXP (x, 0)) != DImode))
17023                     + (rtx_cost (XEXP (x, 1), outer_code)
17024                        << (GET_MODE (XEXP (x, 1)) != DImode)));
17025           return true;
17026         }
17027       /* FALLTHRU */
17028
17029     case NEG:
17030       if (FLOAT_MODE_P (mode))
17031         {
17032           *total = ix86_cost->fchs;
17033           return false;
17034         }
17035       /* FALLTHRU */
17036
17037     case NOT:
17038       if (!TARGET_64BIT && mode == DImode)
17039         *total = ix86_cost->add * 2;
17040       else
17041         *total = ix86_cost->add;
17042       return false;
17043
17044     case COMPARE:
17045       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
17046           && XEXP (XEXP (x, 0), 1) == const1_rtx
17047           && GET_CODE (XEXP (XEXP (x, 0), 2)) == CONST_INT
17048           && XEXP (x, 1) == const0_rtx)
17049         {
17050           /* This kind of construct is implemented using test[bwl].
17051              Treat it as if we had an AND.  */
17052           *total = (ix86_cost->add
17053                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code)
17054                     + rtx_cost (const1_rtx, outer_code));
17055           return true;
17056         }
17057       return false;
17058
17059     case FLOAT_EXTEND:
17060       if (!TARGET_SSE_MATH
17061           || mode == XFmode
17062           || (mode == DFmode && !TARGET_SSE2))
17063         *total = 0;
17064       return false;
17065
17066     case ABS:
17067       if (FLOAT_MODE_P (mode))
17068         *total = ix86_cost->fabs;
17069       return false;
17070
17071     case SQRT:
17072       if (FLOAT_MODE_P (mode))
17073         *total = ix86_cost->fsqrt;
17074       return false;
17075
17076     case UNSPEC:
17077       if (XINT (x, 1) == UNSPEC_TP)
17078         *total = 0;
17079       return false;
17080
17081     default:
17082       return false;
17083     }
17084 }
17085
17086 #if TARGET_MACHO
17087
17088 static int current_machopic_label_num;
17089
17090 /* Given a symbol name and its associated stub, write out the
17091    definition of the stub.  */
17092
17093 void
17094 machopic_output_stub (FILE *file, const char *symb, const char *stub)
17095 {
17096   unsigned int length;
17097   char *binder_name, *symbol_name, lazy_ptr_name[32];
17098   int label = ++current_machopic_label_num;
17099
17100   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
17101   symb = (*targetm.strip_name_encoding) (symb);
17102
17103   length = strlen (stub);
17104   binder_name = alloca (length + 32);
17105   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
17106
17107   length = strlen (symb);
17108   symbol_name = alloca (length + 32);
17109   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
17110
17111   sprintf (lazy_ptr_name, "L%d$lz", label);
17112
17113   if (MACHOPIC_PURE)
17114     switch_to_section (darwin_sections[machopic_picsymbol_stub_section]);
17115   else
17116     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
17117
17118   fprintf (file, "%s:\n", stub);
17119   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
17120
17121   if (MACHOPIC_PURE)
17122     {
17123       fprintf (file, "\tcall LPC$%d\nLPC$%d:\tpopl %%eax\n", label, label);
17124       fprintf (file, "\tmovl %s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
17125       fprintf (file, "\tjmp *%%edx\n");
17126     }
17127   else
17128     fprintf (file, "\tjmp *%s\n", lazy_ptr_name);
17129
17130   fprintf (file, "%s:\n", binder_name);
17131
17132   if (MACHOPIC_PURE)
17133     {
17134       fprintf (file, "\tlea %s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
17135       fprintf (file, "\tpushl %%eax\n");
17136     }
17137   else
17138     fprintf (file, "\t pushl $%s\n", lazy_ptr_name);
17139
17140   fprintf (file, "\tjmp dyld_stub_binding_helper\n");
17141
17142   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
17143   fprintf (file, "%s:\n", lazy_ptr_name);
17144   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
17145   fprintf (file, "\t.long %s\n", binder_name);
17146 }
17147
17148 void
17149 darwin_x86_file_end (void)
17150 {
17151   darwin_file_end ();
17152   ix86_file_end ();
17153 }
17154 #endif /* TARGET_MACHO */
17155
17156 /* Order the registers for register allocator.  */
17157
17158 void
17159 x86_order_regs_for_local_alloc (void)
17160 {
17161    int pos = 0;
17162    int i;
17163
17164    /* First allocate the local general purpose registers.  */
17165    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17166      if (GENERAL_REGNO_P (i) && call_used_regs[i])
17167         reg_alloc_order [pos++] = i;
17168
17169    /* Global general purpose registers.  */
17170    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17171      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
17172         reg_alloc_order [pos++] = i;
17173
17174    /* x87 registers come first in case we are doing FP math
17175       using them.  */
17176    if (!TARGET_SSE_MATH)
17177      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
17178        reg_alloc_order [pos++] = i;
17179
17180    /* SSE registers.  */
17181    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
17182      reg_alloc_order [pos++] = i;
17183    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
17184      reg_alloc_order [pos++] = i;
17185
17186    /* x87 registers.  */
17187    if (TARGET_SSE_MATH)
17188      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
17189        reg_alloc_order [pos++] = i;
17190
17191    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
17192      reg_alloc_order [pos++] = i;
17193
17194    /* Initialize the rest of array as we do not allocate some registers
17195       at all.  */
17196    while (pos < FIRST_PSEUDO_REGISTER)
17197      reg_alloc_order [pos++] = 0;
17198 }
17199
17200 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
17201    struct attribute_spec.handler.  */
17202 static tree
17203 ix86_handle_struct_attribute (tree *node, tree name,
17204                               tree args ATTRIBUTE_UNUSED,
17205                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
17206 {
17207   tree *type = NULL;
17208   if (DECL_P (*node))
17209     {
17210       if (TREE_CODE (*node) == TYPE_DECL)
17211         type = &TREE_TYPE (*node);
17212     }
17213   else
17214     type = node;
17215
17216   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
17217                  || TREE_CODE (*type) == UNION_TYPE)))
17218     {
17219       warning (OPT_Wattributes, "%qs attribute ignored",
17220                IDENTIFIER_POINTER (name));
17221       *no_add_attrs = true;
17222     }
17223
17224   else if ((is_attribute_p ("ms_struct", name)
17225             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
17226            || ((is_attribute_p ("gcc_struct", name)
17227                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
17228     {
17229       warning (OPT_Wattributes, "%qs incompatible attribute ignored",
17230                IDENTIFIER_POINTER (name));
17231       *no_add_attrs = true;
17232     }
17233
17234   return NULL_TREE;
17235 }
17236
17237 static bool
17238 ix86_ms_bitfield_layout_p (tree record_type)
17239 {
17240   return (TARGET_MS_BITFIELD_LAYOUT &&
17241           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
17242     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
17243 }
17244
17245 /* Returns an expression indicating where the this parameter is
17246    located on entry to the FUNCTION.  */
17247
17248 static rtx
17249 x86_this_parameter (tree function)
17250 {
17251   tree type = TREE_TYPE (function);
17252
17253   if (TARGET_64BIT)
17254     {
17255       int n = aggregate_value_p (TREE_TYPE (type), type) != 0;
17256       return gen_rtx_REG (DImode, x86_64_int_parameter_registers[n]);
17257     }
17258
17259   if (ix86_function_regparm (type, function) > 0)
17260     {
17261       tree parm;
17262
17263       parm = TYPE_ARG_TYPES (type);
17264       /* Figure out whether or not the function has a variable number of
17265          arguments.  */
17266       for (; parm; parm = TREE_CHAIN (parm))
17267         if (TREE_VALUE (parm) == void_type_node)
17268           break;
17269       /* If not, the this parameter is in the first argument.  */
17270       if (parm)
17271         {
17272           int regno = 0;
17273           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
17274             regno = 2;
17275           return gen_rtx_REG (SImode, regno);
17276         }
17277     }
17278
17279   if (aggregate_value_p (TREE_TYPE (type), type))
17280     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 8));
17281   else
17282     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 4));
17283 }
17284
17285 /* Determine whether x86_output_mi_thunk can succeed.  */
17286
17287 static bool
17288 x86_can_output_mi_thunk (tree thunk ATTRIBUTE_UNUSED,
17289                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
17290                          HOST_WIDE_INT vcall_offset, tree function)
17291 {
17292   /* 64-bit can handle anything.  */
17293   if (TARGET_64BIT)
17294     return true;
17295
17296   /* For 32-bit, everything's fine if we have one free register.  */
17297   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
17298     return true;
17299
17300   /* Need a free register for vcall_offset.  */
17301   if (vcall_offset)
17302     return false;
17303
17304   /* Need a free register for GOT references.  */
17305   if (flag_pic && !(*targetm.binds_local_p) (function))
17306     return false;
17307
17308   /* Otherwise ok.  */
17309   return true;
17310 }
17311
17312 /* Output the assembler code for a thunk function.  THUNK_DECL is the
17313    declaration for the thunk function itself, FUNCTION is the decl for
17314    the target function.  DELTA is an immediate constant offset to be
17315    added to THIS.  If VCALL_OFFSET is nonzero, the word at
17316    *(*this + vcall_offset) should be added to THIS.  */
17317
17318 static void
17319 x86_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
17320                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
17321                      HOST_WIDE_INT vcall_offset, tree function)
17322 {
17323   rtx xops[3];
17324   rtx this = x86_this_parameter (function);
17325   rtx this_reg, tmp;
17326
17327   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
17328      pull it in now and let DELTA benefit.  */
17329   if (REG_P (this))
17330     this_reg = this;
17331   else if (vcall_offset)
17332     {
17333       /* Put the this parameter into %eax.  */
17334       xops[0] = this;
17335       xops[1] = this_reg = gen_rtx_REG (Pmode, 0);
17336       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
17337     }
17338   else
17339     this_reg = NULL_RTX;
17340
17341   /* Adjust the this parameter by a fixed constant.  */
17342   if (delta)
17343     {
17344       xops[0] = GEN_INT (delta);
17345       xops[1] = this_reg ? this_reg : this;
17346       if (TARGET_64BIT)
17347         {
17348           if (!x86_64_general_operand (xops[0], DImode))
17349             {
17350               tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
17351               xops[1] = tmp;
17352               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
17353               xops[0] = tmp;
17354               xops[1] = this;
17355             }
17356           output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
17357         }
17358       else
17359         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
17360     }
17361
17362   /* Adjust the this parameter by a value stored in the vtable.  */
17363   if (vcall_offset)
17364     {
17365       if (TARGET_64BIT)
17366         tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
17367       else
17368         {
17369           int tmp_regno = 2 /* ECX */;
17370           if (lookup_attribute ("fastcall",
17371               TYPE_ATTRIBUTES (TREE_TYPE (function))))
17372             tmp_regno = 0 /* EAX */;
17373           tmp = gen_rtx_REG (SImode, tmp_regno);
17374         }
17375
17376       xops[0] = gen_rtx_MEM (Pmode, this_reg);
17377       xops[1] = tmp;
17378       if (TARGET_64BIT)
17379         output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
17380       else
17381         output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
17382
17383       /* Adjust the this parameter.  */
17384       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
17385       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
17386         {
17387           rtx tmp2 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
17388           xops[0] = GEN_INT (vcall_offset);
17389           xops[1] = tmp2;
17390           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
17391           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
17392         }
17393       xops[1] = this_reg;
17394       if (TARGET_64BIT)
17395         output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
17396       else
17397         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
17398     }
17399
17400   /* If necessary, drop THIS back to its stack slot.  */
17401   if (this_reg && this_reg != this)
17402     {
17403       xops[0] = this_reg;
17404       xops[1] = this;
17405       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
17406     }
17407
17408   xops[0] = XEXP (DECL_RTL (function), 0);
17409   if (TARGET_64BIT)
17410     {
17411       if (!flag_pic || (*targetm.binds_local_p) (function))
17412         output_asm_insn ("jmp\t%P0", xops);
17413       else
17414         {
17415           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
17416           tmp = gen_rtx_CONST (Pmode, tmp);
17417           tmp = gen_rtx_MEM (QImode, tmp);
17418           xops[0] = tmp;
17419           output_asm_insn ("jmp\t%A0", xops);
17420         }
17421     }
17422   else
17423     {
17424       if (!flag_pic || (*targetm.binds_local_p) (function))
17425         output_asm_insn ("jmp\t%P0", xops);
17426       else
17427 #if TARGET_MACHO
17428         if (TARGET_MACHO)
17429           {
17430             rtx sym_ref = XEXP (DECL_RTL (function), 0);
17431             tmp = (gen_rtx_SYMBOL_REF
17432                    (Pmode,
17433                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
17434             tmp = gen_rtx_MEM (QImode, tmp);
17435             xops[0] = tmp;
17436             output_asm_insn ("jmp\t%0", xops);
17437           }
17438         else
17439 #endif /* TARGET_MACHO */
17440         {
17441           tmp = gen_rtx_REG (SImode, 2 /* ECX */);
17442           output_set_got (tmp, NULL_RTX);
17443
17444           xops[1] = tmp;
17445           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
17446           output_asm_insn ("jmp\t{*}%1", xops);
17447         }
17448     }
17449 }
17450
17451 static void
17452 x86_file_start (void)
17453 {
17454   default_file_start ();
17455   if (X86_FILE_START_VERSION_DIRECTIVE)
17456     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
17457   if (X86_FILE_START_FLTUSED)
17458     fputs ("\t.global\t__fltused\n", asm_out_file);
17459   if (ix86_asm_dialect == ASM_INTEL)
17460     fputs ("\t.intel_syntax\n", asm_out_file);
17461 }
17462
17463 int
17464 x86_field_alignment (tree field, int computed)
17465 {
17466   enum machine_mode mode;
17467   tree type = TREE_TYPE (field);
17468
17469   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
17470     return computed;
17471   mode = TYPE_MODE (TREE_CODE (type) == ARRAY_TYPE
17472                     ? get_inner_array_type (type) : type);
17473   if (mode == DFmode || mode == DCmode
17474       || GET_MODE_CLASS (mode) == MODE_INT
17475       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
17476     return MIN (32, computed);
17477   return computed;
17478 }
17479
17480 /* Output assembler code to FILE to increment profiler label # LABELNO
17481    for profiling a function entry.  */
17482 void
17483 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
17484 {
17485   if (TARGET_64BIT)
17486     if (flag_pic)
17487       {
17488 #ifndef NO_PROFILE_COUNTERS
17489         fprintf (file, "\tleaq\t%sP%d@(%%rip),%%r11\n", LPREFIX, labelno);
17490 #endif
17491         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", MCOUNT_NAME);
17492       }
17493     else
17494       {
17495 #ifndef NO_PROFILE_COUNTERS
17496         fprintf (file, "\tmovq\t$%sP%d,%%r11\n", LPREFIX, labelno);
17497 #endif
17498         fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
17499       }
17500   else if (flag_pic)
17501     {
17502 #ifndef NO_PROFILE_COUNTERS
17503       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%%s\n",
17504                LPREFIX, labelno, PROFILE_COUNT_REGISTER);
17505 #endif
17506       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", MCOUNT_NAME);
17507     }
17508   else
17509     {
17510 #ifndef NO_PROFILE_COUNTERS
17511       fprintf (file, "\tmovl\t$%sP%d,%%%s\n", LPREFIX, labelno,
17512                PROFILE_COUNT_REGISTER);
17513 #endif
17514       fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
17515     }
17516 }
17517
17518 /* We don't have exact information about the insn sizes, but we may assume
17519    quite safely that we are informed about all 1 byte insns and memory
17520    address sizes.  This is enough to eliminate unnecessary padding in
17521    99% of cases.  */
17522
17523 static int
17524 min_insn_size (rtx insn)
17525 {
17526   int l = 0;
17527
17528   if (!INSN_P (insn) || !active_insn_p (insn))
17529     return 0;
17530
17531   /* Discard alignments we've emit and jump instructions.  */
17532   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
17533       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
17534     return 0;
17535   if (GET_CODE (insn) == JUMP_INSN
17536       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
17537           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
17538     return 0;
17539
17540   /* Important case - calls are always 5 bytes.
17541      It is common to have many calls in the row.  */
17542   if (GET_CODE (insn) == CALL_INSN
17543       && symbolic_reference_mentioned_p (PATTERN (insn))
17544       && !SIBLING_CALL_P (insn))
17545     return 5;
17546   if (get_attr_length (insn) <= 1)
17547     return 1;
17548
17549   /* For normal instructions we may rely on the sizes of addresses
17550      and the presence of symbol to require 4 bytes of encoding.
17551      This is not the case for jumps where references are PC relative.  */
17552   if (GET_CODE (insn) != JUMP_INSN)
17553     {
17554       l = get_attr_length_address (insn);
17555       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
17556         l = 4;
17557     }
17558   if (l)
17559     return 1+l;
17560   else
17561     return 2;
17562 }
17563
17564 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
17565    window.  */
17566
17567 static void
17568 ix86_avoid_jump_misspredicts (void)
17569 {
17570   rtx insn, start = get_insns ();
17571   int nbytes = 0, njumps = 0;
17572   int isjump = 0;
17573
17574   /* Look for all minimal intervals of instructions containing 4 jumps.
17575      The intervals are bounded by START and INSN.  NBYTES is the total
17576      size of instructions in the interval including INSN and not including
17577      START.  When the NBYTES is smaller than 16 bytes, it is possible
17578      that the end of START and INSN ends up in the same 16byte page.
17579
17580      The smallest offset in the page INSN can start is the case where START
17581      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
17582      We add p2align to 16byte window with maxskip 17 - NBYTES + sizeof (INSN).
17583      */
17584   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
17585     {
17586
17587       nbytes += min_insn_size (insn);
17588       if (dump_file)
17589         fprintf(dump_file, "Insn %i estimated to %i bytes\n",
17590                 INSN_UID (insn), min_insn_size (insn));
17591       if ((GET_CODE (insn) == JUMP_INSN
17592            && GET_CODE (PATTERN (insn)) != ADDR_VEC
17593            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
17594           || GET_CODE (insn) == CALL_INSN)
17595         njumps++;
17596       else
17597         continue;
17598
17599       while (njumps > 3)
17600         {
17601           start = NEXT_INSN (start);
17602           if ((GET_CODE (start) == JUMP_INSN
17603                && GET_CODE (PATTERN (start)) != ADDR_VEC
17604                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
17605               || GET_CODE (start) == CALL_INSN)
17606             njumps--, isjump = 1;
17607           else
17608             isjump = 0;
17609           nbytes -= min_insn_size (start);
17610         }
17611       gcc_assert (njumps >= 0);
17612       if (dump_file)
17613         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
17614                 INSN_UID (start), INSN_UID (insn), nbytes);
17615
17616       if (njumps == 3 && isjump && nbytes < 16)
17617         {
17618           int padsize = 15 - nbytes + min_insn_size (insn);
17619
17620           if (dump_file)
17621             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
17622                      INSN_UID (insn), padsize);
17623           emit_insn_before (gen_align (GEN_INT (padsize)), insn);
17624         }
17625     }
17626 }
17627
17628 /* AMD Athlon works faster
17629    when RET is not destination of conditional jump or directly preceded
17630    by other jump instruction.  We avoid the penalty by inserting NOP just
17631    before the RET instructions in such cases.  */
17632 static void
17633 ix86_pad_returns (void)
17634 {
17635   edge e;
17636   edge_iterator ei;
17637
17638   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
17639     {
17640       basic_block bb = e->src;
17641       rtx ret = BB_END (bb);
17642       rtx prev;
17643       bool replace = false;
17644
17645       if (GET_CODE (ret) != JUMP_INSN || GET_CODE (PATTERN (ret)) != RETURN
17646           || !maybe_hot_bb_p (bb))
17647         continue;
17648       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
17649         if (active_insn_p (prev) || GET_CODE (prev) == CODE_LABEL)
17650           break;
17651       if (prev && GET_CODE (prev) == CODE_LABEL)
17652         {
17653           edge e;
17654           edge_iterator ei;
17655
17656           FOR_EACH_EDGE (e, ei, bb->preds)
17657             if (EDGE_FREQUENCY (e) && e->src->index >= 0
17658                 && !(e->flags & EDGE_FALLTHRU))
17659               replace = true;
17660         }
17661       if (!replace)
17662         {
17663           prev = prev_active_insn (ret);
17664           if (prev
17665               && ((GET_CODE (prev) == JUMP_INSN && any_condjump_p (prev))
17666                   || GET_CODE (prev) == CALL_INSN))
17667             replace = true;
17668           /* Empty functions get branch mispredict even when the jump destination
17669              is not visible to us.  */
17670           if (!prev && cfun->function_frequency > FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
17671             replace = true;
17672         }
17673       if (replace)
17674         {
17675           emit_insn_before (gen_return_internal_long (), ret);
17676           delete_insn (ret);
17677         }
17678     }
17679 }
17680
17681 /* Implement machine specific optimizations.  We implement padding of returns
17682    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
17683 static void
17684 ix86_reorg (void)
17685 {
17686   if (TARGET_PAD_RETURNS && optimize && !optimize_size)
17687     ix86_pad_returns ();
17688   if (TARGET_FOUR_JUMP_LIMIT && optimize && !optimize_size)
17689     ix86_avoid_jump_misspredicts ();
17690 }
17691
17692 /* Return nonzero when QImode register that must be represented via REX prefix
17693    is used.  */
17694 bool
17695 x86_extended_QIreg_mentioned_p (rtx insn)
17696 {
17697   int i;
17698   extract_insn_cached (insn);
17699   for (i = 0; i < recog_data.n_operands; i++)
17700     if (REG_P (recog_data.operand[i])
17701         && REGNO (recog_data.operand[i]) >= 4)
17702        return true;
17703   return false;
17704 }
17705
17706 /* Return nonzero when P points to register encoded via REX prefix.
17707    Called via for_each_rtx.  */
17708 static int
17709 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
17710 {
17711    unsigned int regno;
17712    if (!REG_P (*p))
17713      return 0;
17714    regno = REGNO (*p);
17715    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
17716 }
17717
17718 /* Return true when INSN mentions register that must be encoded using REX
17719    prefix.  */
17720 bool
17721 x86_extended_reg_mentioned_p (rtx insn)
17722 {
17723   return for_each_rtx (&PATTERN (insn), extended_reg_mentioned_1, NULL);
17724 }
17725
17726 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
17727    optabs would emit if we didn't have TFmode patterns.  */
17728
17729 void
17730 x86_emit_floatuns (rtx operands[2])
17731 {
17732   rtx neglab, donelab, i0, i1, f0, in, out;
17733   enum machine_mode mode, inmode;
17734
17735   inmode = GET_MODE (operands[1]);
17736   gcc_assert (inmode == SImode || inmode == DImode);
17737
17738   out = operands[0];
17739   in = force_reg (inmode, operands[1]);
17740   mode = GET_MODE (out);
17741   neglab = gen_label_rtx ();
17742   donelab = gen_label_rtx ();
17743   i1 = gen_reg_rtx (Pmode);
17744   f0 = gen_reg_rtx (mode);
17745
17746   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, Pmode, 0, neglab);
17747
17748   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_FLOAT (mode, in)));
17749   emit_jump_insn (gen_jump (donelab));
17750   emit_barrier ();
17751
17752   emit_label (neglab);
17753
17754   i0 = expand_simple_binop (Pmode, LSHIFTRT, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
17755   i1 = expand_simple_binop (Pmode, AND, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
17756   i0 = expand_simple_binop (Pmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
17757   expand_float (f0, i0, 0);
17758   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
17759
17760   emit_label (donelab);
17761 }
17762 \f
17763 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
17764    with all elements equal to VAR.  Return true if successful.  */
17765
17766 static bool
17767 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
17768                                    rtx target, rtx val)
17769 {
17770   enum machine_mode smode, wsmode, wvmode;
17771   rtx x;
17772
17773   switch (mode)
17774     {
17775     case V2SImode:
17776     case V2SFmode:
17777       if (!mmx_ok && !TARGET_SSE)
17778         return false;
17779       /* FALLTHRU */
17780
17781     case V2DFmode:
17782     case V2DImode:
17783     case V4SFmode:
17784     case V4SImode:
17785       val = force_reg (GET_MODE_INNER (mode), val);
17786       x = gen_rtx_VEC_DUPLICATE (mode, val);
17787       emit_insn (gen_rtx_SET (VOIDmode, target, x));
17788       return true;
17789
17790     case V4HImode:
17791       if (!mmx_ok)
17792         return false;
17793       if (TARGET_SSE || TARGET_3DNOW_A)
17794         {
17795           val = gen_lowpart (SImode, val);
17796           x = gen_rtx_TRUNCATE (HImode, val);
17797           x = gen_rtx_VEC_DUPLICATE (mode, x);
17798           emit_insn (gen_rtx_SET (VOIDmode, target, x));
17799           return true;
17800         }
17801       else
17802         {
17803           smode = HImode;
17804           wsmode = SImode;
17805           wvmode = V2SImode;
17806           goto widen;
17807         }
17808
17809     case V8QImode:
17810       if (!mmx_ok)
17811         return false;
17812       smode = QImode;
17813       wsmode = HImode;
17814       wvmode = V4HImode;
17815       goto widen;
17816     case V8HImode:
17817       smode = HImode;
17818       wsmode = SImode;
17819       wvmode = V4SImode;
17820       goto widen;
17821     case V16QImode:
17822       smode = QImode;
17823       wsmode = HImode;
17824       wvmode = V8HImode;
17825       goto widen;
17826     widen:
17827       /* Replicate the value once into the next wider mode and recurse.  */
17828       val = convert_modes (wsmode, smode, val, true);
17829       x = expand_simple_binop (wsmode, ASHIFT, val,
17830                                GEN_INT (GET_MODE_BITSIZE (smode)),
17831                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
17832       val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
17833
17834       x = gen_reg_rtx (wvmode);
17835       if (!ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val))
17836         gcc_unreachable ();
17837       emit_move_insn (target, gen_lowpart (mode, x));
17838       return true;
17839
17840     default:
17841       return false;
17842     }
17843 }
17844
17845 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
17846    whose low element is VAR, and other elements are zero.  Return true
17847    if successful.  */
17848
17849 static bool
17850 ix86_expand_vector_init_low_nonzero (bool mmx_ok, enum machine_mode mode,
17851                                      rtx target, rtx var)
17852 {
17853   enum machine_mode vsimode;
17854   rtx x;
17855
17856   switch (mode)
17857     {
17858     case V2SFmode:
17859     case V2SImode:
17860       if (!mmx_ok && !TARGET_SSE)
17861         return false;
17862       /* FALLTHRU */
17863
17864     case V2DFmode:
17865     case V2DImode:
17866       var = force_reg (GET_MODE_INNER (mode), var);
17867       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
17868       emit_insn (gen_rtx_SET (VOIDmode, target, x));
17869       return true;
17870
17871     case V4SFmode:
17872     case V4SImode:
17873       var = force_reg (GET_MODE_INNER (mode), var);
17874       x = gen_rtx_VEC_DUPLICATE (mode, var);
17875       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
17876       emit_insn (gen_rtx_SET (VOIDmode, target, x));
17877       return true;
17878
17879     case V8HImode:
17880     case V16QImode:
17881       vsimode = V4SImode;
17882       goto widen;
17883     case V4HImode:
17884     case V8QImode:
17885       if (!mmx_ok)
17886         return false;
17887       vsimode = V2SImode;
17888       goto widen;
17889     widen:
17890       /* Zero extend the variable element to SImode and recurse.  */
17891       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
17892
17893       x = gen_reg_rtx (vsimode);
17894       if (!ix86_expand_vector_init_low_nonzero (mmx_ok, vsimode, x, var))
17895         gcc_unreachable ();
17896
17897       emit_move_insn (target, gen_lowpart (mode, x));
17898       return true;
17899
17900     default:
17901       return false;
17902     }
17903 }
17904
17905 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
17906    consisting of the values in VALS.  It is known that all elements
17907    except ONE_VAR are constants.  Return true if successful.  */
17908
17909 static bool
17910 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
17911                                  rtx target, rtx vals, int one_var)
17912 {
17913   rtx var = XVECEXP (vals, 0, one_var);
17914   enum machine_mode wmode;
17915   rtx const_vec, x;
17916
17917   const_vec = copy_rtx (vals);
17918   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
17919   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
17920
17921   switch (mode)
17922     {
17923     case V2DFmode:
17924     case V2DImode:
17925     case V2SFmode:
17926     case V2SImode:
17927       /* For the two element vectors, it's just as easy to use
17928          the general case.  */
17929       return false;
17930
17931     case V4SFmode:
17932     case V4SImode:
17933     case V8HImode:
17934     case V4HImode:
17935       break;
17936
17937     case V16QImode:
17938       wmode = V8HImode;
17939       goto widen;
17940     case V8QImode:
17941       wmode = V4HImode;
17942       goto widen;
17943     widen:
17944       /* There's no way to set one QImode entry easily.  Combine
17945          the variable value with its adjacent constant value, and
17946          promote to an HImode set.  */
17947       x = XVECEXP (vals, 0, one_var ^ 1);
17948       if (one_var & 1)
17949         {
17950           var = convert_modes (HImode, QImode, var, true);
17951           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
17952                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
17953           x = GEN_INT (INTVAL (x) & 0xff);
17954         }
17955       else
17956         {
17957           var = convert_modes (HImode, QImode, var, true);
17958           x = gen_int_mode (INTVAL (x) << 8, HImode);
17959         }
17960       if (x != const0_rtx)
17961         var = expand_simple_binop (HImode, IOR, var, x, var,
17962                                    1, OPTAB_LIB_WIDEN);
17963
17964       x = gen_reg_rtx (wmode);
17965       emit_move_insn (x, gen_lowpart (wmode, const_vec));
17966       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
17967
17968       emit_move_insn (target, gen_lowpart (mode, x));
17969       return true;
17970
17971     default:
17972       return false;
17973     }
17974
17975   emit_move_insn (target, const_vec);
17976   ix86_expand_vector_set (mmx_ok, target, var, one_var);
17977   return true;
17978 }
17979
17980 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
17981    all values variable, and none identical.  */
17982
17983 static void
17984 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
17985                                  rtx target, rtx vals)
17986 {
17987   enum machine_mode half_mode = GET_MODE_INNER (mode);
17988   rtx op0 = NULL, op1 = NULL;
17989   bool use_vec_concat = false;
17990
17991   switch (mode)
17992     {
17993     case V2SFmode:
17994     case V2SImode:
17995       if (!mmx_ok && !TARGET_SSE)
17996         break;
17997       /* FALLTHRU */
17998
17999     case V2DFmode:
18000     case V2DImode:
18001       /* For the two element vectors, we always implement VEC_CONCAT.  */
18002       op0 = XVECEXP (vals, 0, 0);
18003       op1 = XVECEXP (vals, 0, 1);
18004       use_vec_concat = true;
18005       break;
18006
18007     case V4SFmode:
18008       half_mode = V2SFmode;
18009       goto half;
18010     case V4SImode:
18011       half_mode = V2SImode;
18012       goto half;
18013     half:
18014       {
18015         rtvec v;
18016
18017         /* For V4SF and V4SI, we implement a concat of two V2 vectors.
18018            Recurse to load the two halves.  */
18019
18020         op0 = gen_reg_rtx (half_mode);
18021         v = gen_rtvec (2, XVECEXP (vals, 0, 0), XVECEXP (vals, 0, 1));
18022         ix86_expand_vector_init (false, op0, gen_rtx_PARALLEL (half_mode, v));
18023
18024         op1 = gen_reg_rtx (half_mode);
18025         v = gen_rtvec (2, XVECEXP (vals, 0, 2), XVECEXP (vals, 0, 3));
18026         ix86_expand_vector_init (false, op1, gen_rtx_PARALLEL (half_mode, v));
18027
18028         use_vec_concat = true;
18029       }
18030       break;
18031
18032     case V8HImode:
18033     case V16QImode:
18034     case V4HImode:
18035     case V8QImode:
18036       break;
18037
18038     default:
18039       gcc_unreachable ();
18040     }
18041
18042   if (use_vec_concat)
18043     {
18044       if (!register_operand (op0, half_mode))
18045         op0 = force_reg (half_mode, op0);
18046       if (!register_operand (op1, half_mode))
18047         op1 = force_reg (half_mode, op1);
18048
18049       emit_insn (gen_rtx_SET (VOIDmode, target, 
18050                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
18051     }
18052   else
18053     {
18054       int i, j, n_elts, n_words, n_elt_per_word;
18055       enum machine_mode inner_mode;
18056       rtx words[4], shift;
18057
18058       inner_mode = GET_MODE_INNER (mode);
18059       n_elts = GET_MODE_NUNITS (mode);
18060       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
18061       n_elt_per_word = n_elts / n_words;
18062       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
18063
18064       for (i = 0; i < n_words; ++i)
18065         {
18066           rtx word = NULL_RTX;
18067
18068           for (j = 0; j < n_elt_per_word; ++j)
18069             {
18070               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
18071               elt = convert_modes (word_mode, inner_mode, elt, true);
18072
18073               if (j == 0)
18074                 word = elt;
18075               else
18076                 {
18077                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
18078                                               word, 1, OPTAB_LIB_WIDEN);
18079                   word = expand_simple_binop (word_mode, IOR, word, elt,
18080                                               word, 1, OPTAB_LIB_WIDEN);
18081                 }
18082             }
18083
18084           words[i] = word;
18085         }
18086
18087       if (n_words == 1)
18088         emit_move_insn (target, gen_lowpart (mode, words[0]));
18089       else if (n_words == 2)
18090         {
18091           rtx tmp = gen_reg_rtx (mode);
18092           emit_insn (gen_rtx_CLOBBER (VOIDmode, tmp));
18093           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
18094           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
18095           emit_move_insn (target, tmp);
18096         }
18097       else if (n_words == 4)
18098         {
18099           rtx tmp = gen_reg_rtx (V4SImode);
18100           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
18101           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
18102           emit_move_insn (target, gen_lowpart (mode, tmp));
18103         }
18104       else
18105         gcc_unreachable ();
18106     }
18107 }
18108
18109 /* Initialize vector TARGET via VALS.  Suppress the use of MMX 
18110    instructions unless MMX_OK is true.  */
18111
18112 void
18113 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
18114 {
18115   enum machine_mode mode = GET_MODE (target);
18116   enum machine_mode inner_mode = GET_MODE_INNER (mode);
18117   int n_elts = GET_MODE_NUNITS (mode);
18118   int n_var = 0, one_var = -1;
18119   bool all_same = true, all_const_zero = true;
18120   int i;
18121   rtx x;
18122
18123   for (i = 0; i < n_elts; ++i)
18124     {
18125       x = XVECEXP (vals, 0, i);
18126       if (!CONSTANT_P (x))
18127         n_var++, one_var = i;
18128       else if (x != CONST0_RTX (inner_mode))
18129         all_const_zero = false;
18130       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
18131         all_same = false;
18132     }
18133
18134   /* Constants are best loaded from the constant pool.  */
18135   if (n_var == 0)
18136     {
18137       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
18138       return;
18139     }
18140
18141   /* If all values are identical, broadcast the value.  */
18142   if (all_same
18143       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
18144                                             XVECEXP (vals, 0, 0)))
18145     return;
18146
18147   /* Values where only one field is non-constant are best loaded from
18148      the pool and overwritten via move later.  */
18149   if (n_var == 1)
18150     {
18151       if (all_const_zero && one_var == 0
18152           && ix86_expand_vector_init_low_nonzero (mmx_ok, mode, target,
18153                                                   XVECEXP (vals, 0, 0)))
18154         return;
18155
18156       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
18157         return;
18158     }
18159
18160   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
18161 }
18162
18163 void
18164 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
18165 {
18166   enum machine_mode mode = GET_MODE (target);
18167   enum machine_mode inner_mode = GET_MODE_INNER (mode);
18168   bool use_vec_merge = false;
18169   rtx tmp;
18170
18171   switch (mode)
18172     {
18173     case V2SFmode:
18174     case V2SImode:
18175       if (mmx_ok)
18176         {
18177           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
18178           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
18179           if (elt == 0)
18180             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
18181           else
18182             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
18183           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
18184           return;
18185         }
18186       break;
18187
18188     case V2DFmode:
18189     case V2DImode:
18190       {
18191         rtx op0, op1;
18192
18193         /* For the two element vectors, we implement a VEC_CONCAT with
18194            the extraction of the other element.  */
18195
18196         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
18197         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
18198
18199         if (elt == 0)
18200           op0 = val, op1 = tmp;
18201         else
18202           op0 = tmp, op1 = val;
18203
18204         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
18205         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
18206       }
18207       return;
18208
18209     case V4SFmode:
18210       switch (elt)
18211         {
18212         case 0:
18213           use_vec_merge = true;
18214           break;
18215
18216         case 1:
18217           /* tmp = target = A B C D */
18218           tmp = copy_to_reg (target);
18219           /* target = A A B B */
18220           emit_insn (gen_sse_unpcklps (target, target, target));
18221           /* target = X A B B */
18222           ix86_expand_vector_set (false, target, val, 0);
18223           /* target = A X C D  */
18224           emit_insn (gen_sse_shufps_1 (target, target, tmp,
18225                                        GEN_INT (1), GEN_INT (0),
18226                                        GEN_INT (2+4), GEN_INT (3+4)));
18227           return;
18228
18229         case 2:
18230           /* tmp = target = A B C D */
18231           tmp = copy_to_reg (target);
18232           /* tmp = X B C D */
18233           ix86_expand_vector_set (false, tmp, val, 0);
18234           /* target = A B X D */
18235           emit_insn (gen_sse_shufps_1 (target, target, tmp,
18236                                        GEN_INT (0), GEN_INT (1),
18237                                        GEN_INT (0+4), GEN_INT (3+4)));
18238           return;
18239
18240         case 3:
18241           /* tmp = target = A B C D */
18242           tmp = copy_to_reg (target);
18243           /* tmp = X B C D */
18244           ix86_expand_vector_set (false, tmp, val, 0);
18245           /* target = A B X D */
18246           emit_insn (gen_sse_shufps_1 (target, target, tmp,
18247                                        GEN_INT (0), GEN_INT (1),
18248                                        GEN_INT (2+4), GEN_INT (0+4)));
18249           return;
18250
18251         default:
18252           gcc_unreachable ();
18253         }
18254       break;
18255
18256     case V4SImode:
18257       /* Element 0 handled by vec_merge below.  */
18258       if (elt == 0)
18259         {
18260           use_vec_merge = true;
18261           break;
18262         }
18263
18264       if (TARGET_SSE2)
18265         {
18266           /* With SSE2, use integer shuffles to swap element 0 and ELT,
18267              store into element 0, then shuffle them back.  */
18268
18269           rtx order[4];
18270
18271           order[0] = GEN_INT (elt);
18272           order[1] = const1_rtx;
18273           order[2] = const2_rtx;
18274           order[3] = GEN_INT (3);
18275           order[elt] = const0_rtx;
18276
18277           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
18278                                         order[1], order[2], order[3]));
18279
18280           ix86_expand_vector_set (false, target, val, 0);
18281
18282           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
18283                                         order[1], order[2], order[3]));
18284         }
18285       else
18286         {
18287           /* For SSE1, we have to reuse the V4SF code.  */
18288           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
18289                                   gen_lowpart (SFmode, val), elt);
18290         }
18291       return;
18292
18293     case V8HImode:
18294       use_vec_merge = TARGET_SSE2;
18295       break;
18296     case V4HImode:
18297       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
18298       break;
18299
18300     case V16QImode:
18301     case V8QImode:
18302     default:
18303       break;
18304     }
18305
18306   if (use_vec_merge)
18307     {
18308       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
18309       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
18310       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
18311     }
18312   else
18313     {
18314       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
18315
18316       emit_move_insn (mem, target);
18317
18318       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
18319       emit_move_insn (tmp, val);
18320
18321       emit_move_insn (target, mem);
18322     }
18323 }
18324
18325 void
18326 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
18327 {
18328   enum machine_mode mode = GET_MODE (vec);
18329   enum machine_mode inner_mode = GET_MODE_INNER (mode);
18330   bool use_vec_extr = false;
18331   rtx tmp;
18332
18333   switch (mode)
18334     {
18335     case V2SImode:
18336     case V2SFmode:
18337       if (!mmx_ok)
18338         break;
18339       /* FALLTHRU */
18340
18341     case V2DFmode:
18342     case V2DImode:
18343       use_vec_extr = true;
18344       break;
18345
18346     case V4SFmode:
18347       switch (elt)
18348         {
18349         case 0:
18350           tmp = vec;
18351           break;
18352
18353         case 1:
18354         case 3:
18355           tmp = gen_reg_rtx (mode);
18356           emit_insn (gen_sse_shufps_1 (tmp, vec, vec,
18357                                        GEN_INT (elt), GEN_INT (elt),
18358                                        GEN_INT (elt+4), GEN_INT (elt+4)));
18359           break;
18360
18361         case 2:
18362           tmp = gen_reg_rtx (mode);
18363           emit_insn (gen_sse_unpckhps (tmp, vec, vec));
18364           break;
18365
18366         default:
18367           gcc_unreachable ();
18368         }
18369       vec = tmp;
18370       use_vec_extr = true;
18371       elt = 0;
18372       break;
18373
18374     case V4SImode:
18375       if (TARGET_SSE2)
18376         {
18377           switch (elt)
18378             {
18379             case 0:
18380               tmp = vec;
18381               break;
18382
18383             case 1:
18384             case 3:
18385               tmp = gen_reg_rtx (mode);
18386               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
18387                                             GEN_INT (elt), GEN_INT (elt),
18388                                             GEN_INT (elt), GEN_INT (elt)));
18389               break;
18390
18391             case 2:
18392               tmp = gen_reg_rtx (mode);
18393               emit_insn (gen_sse2_punpckhdq (tmp, vec, vec));
18394               break;
18395
18396             default:
18397               gcc_unreachable ();
18398             }
18399           vec = tmp;
18400           use_vec_extr = true;
18401           elt = 0;
18402         }
18403       else
18404         {
18405           /* For SSE1, we have to reuse the V4SF code.  */
18406           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
18407                                       gen_lowpart (V4SFmode, vec), elt);
18408           return;
18409         }
18410       break;
18411
18412     case V8HImode:
18413       use_vec_extr = TARGET_SSE2;
18414       break;
18415     case V4HImode:
18416       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
18417       break;
18418
18419     case V16QImode:
18420     case V8QImode:
18421       /* ??? Could extract the appropriate HImode element and shift.  */
18422     default:
18423       break;
18424     }
18425
18426   if (use_vec_extr)
18427     {
18428       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
18429       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
18430
18431       /* Let the rtl optimizers know about the zero extension performed.  */
18432       if (inner_mode == HImode)
18433         {
18434           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
18435           target = gen_lowpart (SImode, target);
18436         }
18437
18438       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
18439     }
18440   else
18441     {
18442       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
18443
18444       emit_move_insn (mem, vec);
18445
18446       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
18447       emit_move_insn (target, tmp);
18448     }
18449 }
18450
18451 /* Expand a vector reduction on V4SFmode for SSE1.  FN is the binary
18452    pattern to reduce; DEST is the destination; IN is the input vector.  */
18453
18454 void
18455 ix86_expand_reduc_v4sf (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
18456 {
18457   rtx tmp1, tmp2, tmp3;
18458
18459   tmp1 = gen_reg_rtx (V4SFmode);
18460   tmp2 = gen_reg_rtx (V4SFmode);
18461   tmp3 = gen_reg_rtx (V4SFmode);
18462
18463   emit_insn (gen_sse_movhlps (tmp1, in, in));
18464   emit_insn (fn (tmp2, tmp1, in));
18465
18466   emit_insn (gen_sse_shufps_1 (tmp3, tmp2, tmp2,
18467                                GEN_INT (1), GEN_INT (1),
18468                                GEN_INT (1+4), GEN_INT (1+4)));
18469   emit_insn (fn (dest, tmp2, tmp3));
18470 }
18471 \f
18472 /* Target hook for scalar_mode_supported_p.  */
18473 static bool
18474 ix86_scalar_mode_supported_p (enum machine_mode mode)
18475 {
18476   if (DECIMAL_FLOAT_MODE_P (mode))
18477     return true;
18478   else
18479     return default_scalar_mode_supported_p (mode);
18480 }
18481
18482 /* Implements target hook vector_mode_supported_p.  */
18483 static bool
18484 ix86_vector_mode_supported_p (enum machine_mode mode)
18485 {
18486   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
18487     return true;
18488   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
18489     return true;
18490   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
18491     return true;
18492   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
18493     return true;
18494   return false;
18495 }
18496
18497 /* Worker function for TARGET_MD_ASM_CLOBBERS.
18498
18499    We do this in the new i386 backend to maintain source compatibility
18500    with the old cc0-based compiler.  */
18501
18502 static tree
18503 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
18504                       tree inputs ATTRIBUTE_UNUSED,
18505                       tree clobbers)
18506 {
18507   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
18508                         clobbers);
18509   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
18510                         clobbers);
18511   clobbers = tree_cons (NULL_TREE, build_string (7, "dirflag"),
18512                         clobbers);
18513   return clobbers;
18514 }
18515
18516 /* Return true if this goes in small data/bss.  */
18517
18518 static bool
18519 ix86_in_large_data_p (tree exp)
18520 {
18521   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
18522     return false;
18523
18524   /* Functions are never large data.  */
18525   if (TREE_CODE (exp) == FUNCTION_DECL)
18526     return false;
18527
18528   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
18529     {
18530       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
18531       if (strcmp (section, ".ldata") == 0
18532           || strcmp (section, ".lbss") == 0)
18533         return true;
18534       return false;
18535     }
18536   else
18537     {
18538       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
18539
18540       /* If this is an incomplete type with size 0, then we can't put it
18541          in data because it might be too big when completed.  */
18542       if (!size || size > ix86_section_threshold)
18543         return true;
18544     }
18545
18546   return false;
18547 }
18548 static void
18549 ix86_encode_section_info (tree decl, rtx rtl, int first)
18550 {
18551   default_encode_section_info (decl, rtl, first);
18552
18553   if (TREE_CODE (decl) == VAR_DECL
18554       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
18555       && ix86_in_large_data_p (decl))
18556     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
18557 }
18558
18559 /* Worker function for REVERSE_CONDITION.  */
18560
18561 enum rtx_code
18562 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
18563 {
18564   return (mode != CCFPmode && mode != CCFPUmode
18565           ? reverse_condition (code)
18566           : reverse_condition_maybe_unordered (code));
18567 }
18568
18569 /* Output code to perform an x87 FP register move, from OPERANDS[1]
18570    to OPERANDS[0].  */
18571
18572 const char *
18573 output_387_reg_move (rtx insn, rtx *operands)
18574 {
18575   if (REG_P (operands[1])
18576       && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
18577     {
18578       if (REGNO (operands[0]) == FIRST_STACK_REG
18579           && TARGET_USE_FFREEP)
18580         return "ffreep\t%y0";
18581       return "fstp\t%y0";
18582     }
18583   if (STACK_TOP_P (operands[0]))
18584     return "fld%z1\t%y1";
18585   return "fst\t%y0";
18586 }
18587
18588 /* Output code to perform a conditional jump to LABEL, if C2 flag in
18589    FP status register is set.  */
18590
18591 void
18592 ix86_emit_fp_unordered_jump (rtx label)
18593 {
18594   rtx reg = gen_reg_rtx (HImode);
18595   rtx temp;
18596
18597   emit_insn (gen_x86_fnstsw_1 (reg));
18598
18599   if (TARGET_USE_SAHF)
18600     {
18601       emit_insn (gen_x86_sahf_1 (reg));
18602
18603       temp = gen_rtx_REG (CCmode, FLAGS_REG);
18604       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
18605     }
18606   else
18607     {
18608       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
18609
18610       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
18611       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
18612     }
18613
18614   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
18615                               gen_rtx_LABEL_REF (VOIDmode, label),
18616                               pc_rtx);
18617   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
18618   emit_jump_insn (temp);
18619 }
18620
18621 /* Output code to perform a log1p XFmode calculation.  */
18622
18623 void ix86_emit_i387_log1p (rtx op0, rtx op1)
18624 {
18625   rtx label1 = gen_label_rtx ();
18626   rtx label2 = gen_label_rtx ();
18627
18628   rtx tmp = gen_reg_rtx (XFmode);
18629   rtx tmp2 = gen_reg_rtx (XFmode);
18630
18631   emit_insn (gen_absxf2 (tmp, op1));
18632   emit_insn (gen_cmpxf (tmp,
18633     CONST_DOUBLE_FROM_REAL_VALUE (
18634        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
18635        XFmode)));
18636   emit_jump_insn (gen_bge (label1));
18637
18638   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
18639   emit_insn (gen_fyl2xp1_xf3 (op0, tmp2, op1));
18640   emit_jump (label2);
18641
18642   emit_label (label1);
18643   emit_move_insn (tmp, CONST1_RTX (XFmode));
18644   emit_insn (gen_addxf3 (tmp, op1, tmp));
18645   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
18646   emit_insn (gen_fyl2x_xf3 (op0, tmp2, tmp));
18647
18648   emit_label (label2);
18649 }
18650
18651 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
18652
18653 static void
18654 i386_solaris_elf_named_section (const char *name, unsigned int flags,
18655                                 tree decl)
18656 {
18657   /* With Binutils 2.15, the "@unwind" marker must be specified on
18658      every occurrence of the ".eh_frame" section, not just the first
18659      one.  */
18660   if (TARGET_64BIT
18661       && strcmp (name, ".eh_frame") == 0)
18662     {
18663       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
18664                flags & SECTION_WRITE ? "aw" : "a");
18665       return;
18666     }
18667   default_elf_asm_named_section (name, flags, decl);
18668 }
18669
18670 /* Return the mangling of TYPE if it is an extended fundamental type.  */
18671
18672 static const char *
18673 ix86_mangle_fundamental_type (tree type)
18674 {
18675   switch (TYPE_MODE (type))
18676     {
18677     case TFmode:
18678       /* __float128 is "g".  */
18679       return "g";
18680     case XFmode:
18681       /* "long double" or __float80 is "e".  */
18682       return "e";
18683     default:
18684       return NULL;
18685     }
18686 }
18687
18688 /* For 32-bit code we can save PIC register setup by using
18689    __stack_chk_fail_local hidden function instead of calling
18690    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
18691    register, so it is better to call __stack_chk_fail directly.  */
18692
18693 static tree
18694 ix86_stack_protect_fail (void)
18695 {
18696   return TARGET_64BIT
18697          ? default_external_stack_protect_fail ()
18698          : default_hidden_stack_protect_fail ();
18699 }
18700
18701 /* Select a format to encode pointers in exception handling data.  CODE
18702    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
18703    true if the symbol may be affected by dynamic relocations.
18704
18705    ??? All x86 object file formats are capable of representing this.
18706    After all, the relocation needed is the same as for the call insn.
18707    Whether or not a particular assembler allows us to enter such, I
18708    guess we'll have to see.  */
18709 int
18710 asm_preferred_eh_data_format (int code, int global)
18711 {
18712   if (flag_pic)
18713     {
18714 int type = DW_EH_PE_sdata8;
18715       if (!TARGET_64BIT
18716           || ix86_cmodel == CM_SMALL_PIC
18717           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
18718         type = DW_EH_PE_sdata4;
18719       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
18720     }
18721   if (ix86_cmodel == CM_SMALL
18722       || (ix86_cmodel == CM_MEDIUM && code))
18723     return DW_EH_PE_udata4;
18724   return DW_EH_PE_absptr;
18725 }
18726
18727 #include "gt-i386.h"