OSDN Git Service

* i386.c (ix86_emit_restore_regs_using_mov): Deal with large offsets.
[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 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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-attr.h"
36 #include "flags.h"
37 #include "except.h"
38 #include "function.h"
39 #include "recog.h"
40 #include "expr.h"
41 #include "optabs.h"
42 #include "toplev.h"
43 #include "basic-block.h"
44 #include "ggc.h"
45 #include "target.h"
46 #include "target-def.h"
47 #include "langhooks.h"
48 #include "cgraph.h"
49
50 #ifndef CHECK_STACK_LIMIT
51 #define CHECK_STACK_LIMIT (-1)
52 #endif
53
54 /* Return index of given mode in mult and division cost tables.  */
55 #define MODE_INDEX(mode)                                        \
56   ((mode) == QImode ? 0                                         \
57    : (mode) == HImode ? 1                                       \
58    : (mode) == SImode ? 2                                       \
59    : (mode) == DImode ? 3                                       \
60    : 4)
61
62 /* Processor costs (relative to an add) */
63 static const
64 struct processor_costs size_cost = {    /* costs for tunning for size */
65   2,                                    /* cost of an add instruction */
66   3,                                    /* cost of a lea instruction */
67   2,                                    /* variable shift costs */
68   3,                                    /* constant shift costs */
69   {3, 3, 3, 3, 5},                      /* cost of starting a multiply */
70   0,                                    /* cost of multiply per each bit set */
71   {3, 3, 3, 3, 5},                      /* cost of a divide/mod */
72   3,                                    /* cost of movsx */
73   3,                                    /* cost of movzx */
74   0,                                    /* "large" insn */
75   2,                                    /* MOVE_RATIO */
76   2,                                    /* cost for loading QImode using movzbl */
77   {2, 2, 2},                            /* cost of loading integer registers
78                                            in QImode, HImode and SImode.
79                                            Relative to reg-reg move (2).  */
80   {2, 2, 2},                            /* cost of storing integer registers */
81   2,                                    /* cost of reg,reg fld/fst */
82   {2, 2, 2},                            /* cost of loading fp registers
83                                            in SFmode, DFmode and XFmode */
84   {2, 2, 2},                            /* cost of loading integer registers */
85   3,                                    /* cost of moving MMX register */
86   {3, 3},                               /* cost of loading MMX registers
87                                            in SImode and DImode */
88   {3, 3},                               /* cost of storing MMX registers
89                                            in SImode and DImode */
90   3,                                    /* cost of moving SSE register */
91   {3, 3, 3},                            /* cost of loading SSE registers
92                                            in SImode, DImode and TImode */
93   {3, 3, 3},                            /* cost of storing SSE registers
94                                            in SImode, DImode and TImode */
95   3,                                    /* MMX or SSE register to integer */
96   0,                                    /* size of prefetch block */
97   0,                                    /* number of parallel prefetches */
98   1,                                    /* Branch cost */
99   2,                                    /* cost of FADD and FSUB insns.  */
100   2,                                    /* cost of FMUL instruction.  */
101   2,                                    /* cost of FDIV instruction.  */
102   2,                                    /* cost of FABS instruction.  */
103   2,                                    /* cost of FCHS instruction.  */
104   2,                                    /* cost of FSQRT instruction.  */
105 };
106
107 /* Processor costs (relative to an add) */
108 static const
109 struct processor_costs i386_cost = {    /* 386 specific costs */
110   1,                                    /* cost of an add instruction */
111   1,                                    /* cost of a lea instruction */
112   3,                                    /* variable shift costs */
113   2,                                    /* constant shift costs */
114   {6, 6, 6, 6, 6},                      /* cost of starting a multiply */
115   1,                                    /* cost of multiply per each bit set */
116   {23, 23, 23, 23, 23},                 /* cost of a divide/mod */
117   3,                                    /* cost of movsx */
118   2,                                    /* cost of movzx */
119   15,                                   /* "large" insn */
120   3,                                    /* MOVE_RATIO */
121   4,                                    /* cost for loading QImode using movzbl */
122   {2, 4, 2},                            /* cost of loading integer registers
123                                            in QImode, HImode and SImode.
124                                            Relative to reg-reg move (2).  */
125   {2, 4, 2},                            /* cost of storing integer registers */
126   2,                                    /* cost of reg,reg fld/fst */
127   {8, 8, 8},                            /* cost of loading fp registers
128                                            in SFmode, DFmode and XFmode */
129   {8, 8, 8},                            /* cost of loading integer registers */
130   2,                                    /* cost of moving MMX register */
131   {4, 8},                               /* cost of loading MMX registers
132                                            in SImode and DImode */
133   {4, 8},                               /* cost of storing MMX registers
134                                            in SImode and DImode */
135   2,                                    /* cost of moving SSE register */
136   {4, 8, 16},                           /* cost of loading SSE registers
137                                            in SImode, DImode and TImode */
138   {4, 8, 16},                           /* cost of storing SSE registers
139                                            in SImode, DImode and TImode */
140   3,                                    /* MMX or SSE register to integer */
141   0,                                    /* size of prefetch block */
142   0,                                    /* number of parallel prefetches */
143   1,                                    /* Branch cost */
144   23,                                   /* cost of FADD and FSUB insns.  */
145   27,                                   /* cost of FMUL instruction.  */
146   88,                                   /* cost of FDIV instruction.  */
147   22,                                   /* cost of FABS instruction.  */
148   24,                                   /* cost of FCHS instruction.  */
149   122,                                  /* cost of FSQRT instruction.  */
150 };
151
152 static const
153 struct processor_costs i486_cost = {    /* 486 specific costs */
154   1,                                    /* cost of an add instruction */
155   1,                                    /* cost of a lea instruction */
156   3,                                    /* variable shift costs */
157   2,                                    /* constant shift costs */
158   {12, 12, 12, 12, 12},                 /* cost of starting a multiply */
159   1,                                    /* cost of multiply per each bit set */
160   {40, 40, 40, 40, 40},                 /* cost of a divide/mod */
161   3,                                    /* cost of movsx */
162   2,                                    /* cost of movzx */
163   15,                                   /* "large" insn */
164   3,                                    /* MOVE_RATIO */
165   4,                                    /* cost for loading QImode using movzbl */
166   {2, 4, 2},                            /* cost of loading integer registers
167                                            in QImode, HImode and SImode.
168                                            Relative to reg-reg move (2).  */
169   {2, 4, 2},                            /* cost of storing integer registers */
170   2,                                    /* cost of reg,reg fld/fst */
171   {8, 8, 8},                            /* cost of loading fp registers
172                                            in SFmode, DFmode and XFmode */
173   {8, 8, 8},                            /* cost of loading integer registers */
174   2,                                    /* cost of moving MMX register */
175   {4, 8},                               /* cost of loading MMX registers
176                                            in SImode and DImode */
177   {4, 8},                               /* cost of storing MMX registers
178                                            in SImode and DImode */
179   2,                                    /* cost of moving SSE register */
180   {4, 8, 16},                           /* cost of loading SSE registers
181                                            in SImode, DImode and TImode */
182   {4, 8, 16},                           /* cost of storing SSE registers
183                                            in SImode, DImode and TImode */
184   3,                                    /* MMX or SSE register to integer */
185   0,                                    /* size of prefetch block */
186   0,                                    /* number of parallel prefetches */
187   1,                                    /* Branch cost */
188   8,                                    /* cost of FADD and FSUB insns.  */
189   16,                                   /* cost of FMUL instruction.  */
190   73,                                   /* cost of FDIV instruction.  */
191   3,                                    /* cost of FABS instruction.  */
192   3,                                    /* cost of FCHS instruction.  */
193   83,                                   /* cost of FSQRT instruction.  */
194 };
195
196 static const
197 struct processor_costs pentium_cost = {
198   1,                                    /* cost of an add instruction */
199   1,                                    /* cost of a lea instruction */
200   4,                                    /* variable shift costs */
201   1,                                    /* constant shift costs */
202   {11, 11, 11, 11, 11},                 /* cost of starting a multiply */
203   0,                                    /* cost of multiply per each bit set */
204   {25, 25, 25, 25, 25},                 /* cost of a divide/mod */
205   3,                                    /* cost of movsx */
206   2,                                    /* cost of movzx */
207   8,                                    /* "large" insn */
208   6,                                    /* MOVE_RATIO */
209   6,                                    /* cost for loading QImode using movzbl */
210   {2, 4, 2},                            /* cost of loading integer registers
211                                            in QImode, HImode and SImode.
212                                            Relative to reg-reg move (2).  */
213   {2, 4, 2},                            /* cost of storing integer registers */
214   2,                                    /* cost of reg,reg fld/fst */
215   {2, 2, 6},                            /* cost of loading fp registers
216                                            in SFmode, DFmode and XFmode */
217   {4, 4, 6},                            /* cost of loading integer registers */
218   8,                                    /* cost of moving MMX register */
219   {8, 8},                               /* cost of loading MMX registers
220                                            in SImode and DImode */
221   {8, 8},                               /* cost of storing MMX registers
222                                            in SImode and DImode */
223   2,                                    /* cost of moving SSE register */
224   {4, 8, 16},                           /* cost of loading SSE registers
225                                            in SImode, DImode and TImode */
226   {4, 8, 16},                           /* cost of storing SSE registers
227                                            in SImode, DImode and TImode */
228   3,                                    /* MMX or SSE register to integer */
229   0,                                    /* size of prefetch block */
230   0,                                    /* number of parallel prefetches */
231   2,                                    /* Branch cost */
232   3,                                    /* cost of FADD and FSUB insns.  */
233   3,                                    /* cost of FMUL instruction.  */
234   39,                                   /* cost of FDIV instruction.  */
235   1,                                    /* cost of FABS instruction.  */
236   1,                                    /* cost of FCHS instruction.  */
237   70,                                   /* cost of FSQRT instruction.  */
238 };
239
240 static const
241 struct processor_costs pentiumpro_cost = {
242   1,                                    /* cost of an add instruction */
243   1,                                    /* cost of a lea instruction */
244   1,                                    /* variable shift costs */
245   1,                                    /* constant shift costs */
246   {4, 4, 4, 4, 4},                      /* cost of starting a multiply */
247   0,                                    /* cost of multiply per each bit set */
248   {17, 17, 17, 17, 17},                 /* cost of a divide/mod */
249   1,                                    /* cost of movsx */
250   1,                                    /* cost of movzx */
251   8,                                    /* "large" insn */
252   6,                                    /* MOVE_RATIO */
253   2,                                    /* cost for loading QImode using movzbl */
254   {4, 4, 4},                            /* cost of loading integer registers
255                                            in QImode, HImode and SImode.
256                                            Relative to reg-reg move (2).  */
257   {2, 2, 2},                            /* cost of storing integer registers */
258   2,                                    /* cost of reg,reg fld/fst */
259   {2, 2, 6},                            /* cost of loading fp registers
260                                            in SFmode, DFmode and XFmode */
261   {4, 4, 6},                            /* cost of loading integer registers */
262   2,                                    /* cost of moving MMX register */
263   {2, 2},                               /* cost of loading MMX registers
264                                            in SImode and DImode */
265   {2, 2},                               /* cost of storing MMX registers
266                                            in SImode and DImode */
267   2,                                    /* cost of moving SSE register */
268   {2, 2, 8},                            /* cost of loading SSE registers
269                                            in SImode, DImode and TImode */
270   {2, 2, 8},                            /* cost of storing SSE registers
271                                            in SImode, DImode and TImode */
272   3,                                    /* MMX or SSE register to integer */
273   32,                                   /* size of prefetch block */
274   6,                                    /* number of parallel prefetches */
275   2,                                    /* Branch cost */
276   3,                                    /* cost of FADD and FSUB insns.  */
277   5,                                    /* cost of FMUL instruction.  */
278   56,                                   /* cost of FDIV instruction.  */
279   2,                                    /* cost of FABS instruction.  */
280   2,                                    /* cost of FCHS instruction.  */
281   56,                                   /* cost of FSQRT instruction.  */
282 };
283
284 static const
285 struct processor_costs k6_cost = {
286   1,                                    /* cost of an add instruction */
287   2,                                    /* cost of a lea instruction */
288   1,                                    /* variable shift costs */
289   1,                                    /* constant shift costs */
290   {3, 3, 3, 3, 3},                      /* cost of starting a multiply */
291   0,                                    /* cost of multiply per each bit set */
292   {18, 18, 18, 18, 18},                 /* cost of a divide/mod */
293   2,                                    /* cost of movsx */
294   2,                                    /* cost of movzx */
295   8,                                    /* "large" insn */
296   4,                                    /* MOVE_RATIO */
297   3,                                    /* cost for loading QImode using movzbl */
298   {4, 5, 4},                            /* cost of loading integer registers
299                                            in QImode, HImode and SImode.
300                                            Relative to reg-reg move (2).  */
301   {2, 3, 2},                            /* cost of storing integer registers */
302   4,                                    /* cost of reg,reg fld/fst */
303   {6, 6, 6},                            /* cost of loading fp registers
304                                            in SFmode, DFmode and XFmode */
305   {4, 4, 4},                            /* cost of loading integer registers */
306   2,                                    /* cost of moving MMX register */
307   {2, 2},                               /* cost of loading MMX registers
308                                            in SImode and DImode */
309   {2, 2},                               /* cost of storing MMX registers
310                                            in SImode and DImode */
311   2,                                    /* cost of moving SSE register */
312   {2, 2, 8},                            /* cost of loading SSE registers
313                                            in SImode, DImode and TImode */
314   {2, 2, 8},                            /* cost of storing SSE registers
315                                            in SImode, DImode and TImode */
316   6,                                    /* MMX or SSE register to integer */
317   32,                                   /* size of prefetch block */
318   1,                                    /* number of parallel prefetches */
319   1,                                    /* Branch cost */
320   2,                                    /* cost of FADD and FSUB insns.  */
321   2,                                    /* cost of FMUL instruction.  */
322   56,                                   /* cost of FDIV instruction.  */
323   2,                                    /* cost of FABS instruction.  */
324   2,                                    /* cost of FCHS instruction.  */
325   56,                                   /* cost of FSQRT instruction.  */
326 };
327
328 static const
329 struct processor_costs athlon_cost = {
330   1,                                    /* cost of an add instruction */
331   2,                                    /* cost of a lea instruction */
332   1,                                    /* variable shift costs */
333   1,                                    /* constant shift costs */
334   {5, 5, 5, 5, 5},                      /* cost of starting a multiply */
335   0,                                    /* cost of multiply per each bit set */
336   {18, 26, 42, 74, 74},                 /* cost of a divide/mod */
337   1,                                    /* cost of movsx */
338   1,                                    /* cost of movzx */
339   8,                                    /* "large" insn */
340   9,                                    /* MOVE_RATIO */
341   4,                                    /* cost for loading QImode using movzbl */
342   {3, 4, 3},                            /* cost of loading integer registers
343                                            in QImode, HImode and SImode.
344                                            Relative to reg-reg move (2).  */
345   {3, 4, 3},                            /* cost of storing integer registers */
346   4,                                    /* cost of reg,reg fld/fst */
347   {4, 4, 12},                           /* cost of loading fp registers
348                                            in SFmode, DFmode and XFmode */
349   {6, 6, 8},                            /* cost of loading integer registers */
350   2,                                    /* cost of moving MMX register */
351   {4, 4},                               /* cost of loading MMX registers
352                                            in SImode and DImode */
353   {4, 4},                               /* cost of storing MMX registers
354                                            in SImode and DImode */
355   2,                                    /* cost of moving SSE register */
356   {4, 4, 6},                            /* cost of loading SSE registers
357                                            in SImode, DImode and TImode */
358   {4, 4, 5},                            /* cost of storing SSE registers
359                                            in SImode, DImode and TImode */
360   5,                                    /* MMX or SSE register to integer */
361   64,                                   /* size of prefetch block */
362   6,                                    /* number of parallel prefetches */
363   2,                                    /* Branch cost */
364   4,                                    /* cost of FADD and FSUB insns.  */
365   4,                                    /* cost of FMUL instruction.  */
366   24,                                   /* cost of FDIV instruction.  */
367   2,                                    /* cost of FABS instruction.  */
368   2,                                    /* cost of FCHS instruction.  */
369   35,                                   /* cost of FSQRT instruction.  */
370 };
371
372 static const
373 struct processor_costs k8_cost = {
374   1,                                    /* cost of an add instruction */
375   2,                                    /* cost of a lea instruction */
376   1,                                    /* variable shift costs */
377   1,                                    /* constant shift costs */
378   {3, 4, 3, 4, 5},                      /* cost of starting a multiply */
379   0,                                    /* cost of multiply per each bit set */
380   {18, 26, 42, 74, 74},                 /* cost of a divide/mod */
381   1,                                    /* cost of movsx */
382   1,                                    /* cost of movzx */
383   8,                                    /* "large" insn */
384   9,                                    /* MOVE_RATIO */
385   4,                                    /* cost for loading QImode using movzbl */
386   {3, 4, 3},                            /* cost of loading integer registers
387                                            in QImode, HImode and SImode.
388                                            Relative to reg-reg move (2).  */
389   {3, 4, 3},                            /* cost of storing integer registers */
390   4,                                    /* cost of reg,reg fld/fst */
391   {4, 4, 12},                           /* cost of loading fp registers
392                                            in SFmode, DFmode and XFmode */
393   {6, 6, 8},                            /* cost of loading integer registers */
394   2,                                    /* cost of moving MMX register */
395   {3, 3},                               /* cost of loading MMX registers
396                                            in SImode and DImode */
397   {4, 4},                               /* cost of storing MMX registers
398                                            in SImode and DImode */
399   2,                                    /* cost of moving SSE register */
400   {4, 3, 6},                            /* cost of loading SSE registers
401                                            in SImode, DImode and TImode */
402   {4, 4, 5},                            /* cost of storing SSE registers
403                                            in SImode, DImode and TImode */
404   5,                                    /* MMX or SSE register to integer */
405   64,                                   /* size of prefetch block */
406   6,                                    /* number of parallel prefetches */
407   2,                                    /* Branch cost */
408   4,                                    /* cost of FADD and FSUB insns.  */
409   4,                                    /* cost of FMUL instruction.  */
410   19,                                   /* cost of FDIV instruction.  */
411   2,                                    /* cost of FABS instruction.  */
412   2,                                    /* cost of FCHS instruction.  */
413   35,                                   /* cost of FSQRT instruction.  */
414 };
415
416 static const
417 struct processor_costs pentium4_cost = {
418   1,                                    /* cost of an add instruction */
419   1,                                    /* cost of a lea instruction */
420   4,                                    /* variable shift costs */
421   4,                                    /* constant shift costs */
422   {15, 15, 15, 15, 15},                 /* cost of starting a multiply */
423   0,                                    /* cost of multiply per each bit set */
424   {56, 56, 56, 56, 56},                 /* cost of a divide/mod */
425   1,                                    /* cost of movsx */
426   1,                                    /* cost of movzx */
427   16,                                   /* "large" insn */
428   6,                                    /* MOVE_RATIO */
429   2,                                    /* cost for loading QImode using movzbl */
430   {4, 5, 4},                            /* cost of loading integer registers
431                                            in QImode, HImode and SImode.
432                                            Relative to reg-reg move (2).  */
433   {2, 3, 2},                            /* cost of storing integer registers */
434   2,                                    /* cost of reg,reg fld/fst */
435   {2, 2, 6},                            /* cost of loading fp registers
436                                            in SFmode, DFmode and XFmode */
437   {4, 4, 6},                            /* cost of loading integer registers */
438   2,                                    /* cost of moving MMX register */
439   {2, 2},                               /* cost of loading MMX registers
440                                            in SImode and DImode */
441   {2, 2},                               /* cost of storing MMX registers
442                                            in SImode and DImode */
443   12,                                   /* cost of moving SSE register */
444   {12, 12, 12},                         /* cost of loading SSE registers
445                                            in SImode, DImode and TImode */
446   {2, 2, 8},                            /* cost of storing SSE registers
447                                            in SImode, DImode and TImode */
448   10,                                   /* MMX or SSE register to integer */
449   64,                                   /* size of prefetch block */
450   6,                                    /* number of parallel prefetches */
451   2,                                    /* Branch cost */
452   5,                                    /* cost of FADD and FSUB insns.  */
453   7,                                    /* cost of FMUL instruction.  */
454   43,                                   /* cost of FDIV instruction.  */
455   2,                                    /* cost of FABS instruction.  */
456   2,                                    /* cost of FCHS instruction.  */
457   43,                                   /* cost of FSQRT instruction.  */
458 };
459
460 const struct processor_costs *ix86_cost = &pentium_cost;
461
462 /* Processor feature/optimization bitmasks.  */
463 #define m_386 (1<<PROCESSOR_I386)
464 #define m_486 (1<<PROCESSOR_I486)
465 #define m_PENT (1<<PROCESSOR_PENTIUM)
466 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
467 #define m_K6  (1<<PROCESSOR_K6)
468 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
469 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
470 #define m_K8  (1<<PROCESSOR_K8)
471 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
472
473 const int x86_use_leave = m_386 | m_K6 | m_ATHLON_K8;
474 const int x86_push_memory = m_386 | m_K6 | m_ATHLON_K8 | m_PENT4;
475 const int x86_zero_extend_with_and = m_486 | m_PENT;
476 const int x86_movx = m_ATHLON_K8 | m_PPRO | m_PENT4 /* m_386 | m_K6 */;
477 const int x86_double_with_add = ~m_386;
478 const int x86_use_bit_test = m_386;
479 const int x86_unroll_strlen = m_486 | m_PENT | m_PPRO | m_ATHLON_K8 | m_K6;
480 const int x86_cmove = m_PPRO | m_ATHLON_K8 | m_PENT4;
481 const int x86_3dnow_a = m_ATHLON_K8;
482 const int x86_deep_branch = m_PPRO | m_K6 | m_ATHLON_K8 | m_PENT4;
483 const int x86_branch_hints = m_PENT4;
484 const int x86_use_sahf = m_PPRO | m_K6 | m_PENT4;
485 const int x86_partial_reg_stall = m_PPRO;
486 const int x86_use_loop = m_K6;
487 const int x86_use_fiop = ~(m_PPRO | m_ATHLON_K8 | m_PENT);
488 const int x86_use_mov0 = m_K6;
489 const int x86_use_cltd = ~(m_PENT | m_K6);
490 const int x86_read_modify_write = ~m_PENT;
491 const int x86_read_modify = ~(m_PENT | m_PPRO);
492 const int x86_split_long_moves = m_PPRO;
493 const int x86_promote_QImode = m_K6 | m_PENT | m_386 | m_486 | m_ATHLON_K8;
494 const int x86_fast_prefix = ~(m_PENT | m_486 | m_386);
495 const int x86_single_stringop = m_386 | m_PENT4;
496 const int x86_qimode_math = ~(0);
497 const int x86_promote_qi_regs = 0;
498 const int x86_himode_math = ~(m_PPRO);
499 const int x86_promote_hi_regs = m_PPRO;
500 const int x86_sub_esp_4 = m_ATHLON_K8 | m_PPRO | m_PENT4;
501 const int x86_sub_esp_8 = m_ATHLON_K8 | m_PPRO | m_386 | m_486 | m_PENT4;
502 const int x86_add_esp_4 = m_ATHLON_K8 | m_K6 | m_PENT4;
503 const int x86_add_esp_8 = m_ATHLON_K8 | m_PPRO | m_K6 | m_386 | m_486 | m_PENT4;
504 const int x86_integer_DFmode_moves = ~(m_ATHLON_K8 | m_PENT4 | m_PPRO);
505 const int x86_partial_reg_dependency = m_ATHLON_K8 | m_PENT4;
506 const int x86_memory_mismatch_stall = m_ATHLON_K8 | m_PENT4;
507 const int x86_accumulate_outgoing_args = m_ATHLON_K8 | m_PENT4 | m_PPRO;
508 const int x86_prologue_using_move = m_ATHLON_K8 | m_PENT4 | m_PPRO;
509 const int x86_epilogue_using_move = m_ATHLON_K8 | m_PENT4 | m_PPRO;
510 const int x86_decompose_lea = m_PENT4;
511 const int x86_shift1 = ~m_486;
512 const int x86_arch_always_fancy_math_387 = m_PENT | m_PPRO | m_ATHLON_K8 | m_PENT4;
513 const int x86_sse_partial_reg_dependency = m_PENT4 | m_PPRO;
514 /* Set for machines where the type and dependencies are resolved on SSE register
515    parts instead of whole registers, so we may maintain just lower part of
516    scalar values in proper format leaving the upper part undefined.  */
517 const int x86_sse_partial_regs = m_ATHLON_K8;
518 /* Athlon optimizes partial-register FPS special case, thus avoiding the
519    need for extra instructions beforehand  */
520 const int x86_sse_partial_regs_for_cvtsd2ss = 0;
521 const int x86_sse_typeless_stores = m_ATHLON_K8;
522 const int x86_sse_load0_by_pxor = m_PPRO | m_PENT4;
523 const int x86_use_ffreep = m_ATHLON_K8;
524 const int x86_rep_movl_optimal = m_386 | m_PENT | m_PPRO | m_K6;
525 const int x86_inter_unit_moves = ~(m_ATHLON_K8);
526 const int x86_ext_80387_constants = m_K6 | m_ATHLON | m_PENT4 | m_PPRO;
527
528 /* In case the average insn count for single function invocation is
529    lower than this constant, emit fast (but longer) prologue and
530    epilogue code.  */
531 #define FAST_PROLOGUE_INSN_COUNT 20
532
533 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
534 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
535 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
536 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
537
538 /* Array of the smallest class containing reg number REGNO, indexed by
539    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
540
541 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
542 {
543   /* ax, dx, cx, bx */
544   AREG, DREG, CREG, BREG,
545   /* si, di, bp, sp */
546   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
547   /* FP registers */
548   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
549   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
550   /* arg pointer */
551   NON_Q_REGS,
552   /* flags, fpsr, dirflag, frame */
553   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
554   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
555   SSE_REGS, SSE_REGS,
556   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
557   MMX_REGS, MMX_REGS,
558   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
559   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
560   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
561   SSE_REGS, SSE_REGS,
562 };
563
564 /* The "default" register map used in 32bit mode.  */
565
566 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
567 {
568   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
569   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
570   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, dir, frame */
571   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
572   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
573   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
574   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
575 };
576
577 static int const x86_64_int_parameter_registers[6] =
578 {
579   5 /*RDI*/, 4 /*RSI*/, 1 /*RDX*/, 2 /*RCX*/,
580   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
581 };
582
583 static int const x86_64_int_return_registers[4] =
584 {
585   0 /*RAX*/, 1 /*RDI*/, 5 /*RDI*/, 4 /*RSI*/
586 };
587
588 /* The "default" register map used in 64bit mode.  */
589 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
590 {
591   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
592   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
593   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, dir, frame */
594   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
595   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
596   8,9,10,11,12,13,14,15,                /* extended integer registers */
597   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
598 };
599
600 /* Define the register numbers to be used in Dwarf debugging information.
601    The SVR4 reference port C compiler uses the following register numbers
602    in its Dwarf output code:
603         0 for %eax (gcc regno = 0)
604         1 for %ecx (gcc regno = 2)
605         2 for %edx (gcc regno = 1)
606         3 for %ebx (gcc regno = 3)
607         4 for %esp (gcc regno = 7)
608         5 for %ebp (gcc regno = 6)
609         6 for %esi (gcc regno = 4)
610         7 for %edi (gcc regno = 5)
611    The following three DWARF register numbers are never generated by
612    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
613    believes these numbers have these meanings.
614         8  for %eip    (no gcc equivalent)
615         9  for %eflags (gcc regno = 17)
616         10 for %trapno (no gcc equivalent)
617    It is not at all clear how we should number the FP stack registers
618    for the x86 architecture.  If the version of SDB on x86/svr4 were
619    a bit less brain dead with respect to floating-point then we would
620    have a precedent to follow with respect to DWARF register numbers
621    for x86 FP registers, but the SDB on x86/svr4 is so completely
622    broken with respect to FP registers that it is hardly worth thinking
623    of it as something to strive for compatibility with.
624    The version of x86/svr4 SDB I have at the moment does (partially)
625    seem to believe that DWARF register number 11 is associated with
626    the x86 register %st(0), but that's about all.  Higher DWARF
627    register numbers don't seem to be associated with anything in
628    particular, and even for DWARF regno 11, SDB only seems to under-
629    stand that it should say that a variable lives in %st(0) (when
630    asked via an `=' command) if we said it was in DWARF regno 11,
631    but SDB still prints garbage when asked for the value of the
632    variable in question (via a `/' command).
633    (Also note that the labels SDB prints for various FP stack regs
634    when doing an `x' command are all wrong.)
635    Note that these problems generally don't affect the native SVR4
636    C compiler because it doesn't allow the use of -O with -g and
637    because when it is *not* optimizing, it allocates a memory
638    location for each floating-point variable, and the memory
639    location is what gets described in the DWARF AT_location
640    attribute for the variable in question.
641    Regardless of the severe mental illness of the x86/svr4 SDB, we
642    do something sensible here and we use the following DWARF
643    register numbers.  Note that these are all stack-top-relative
644    numbers.
645         11 for %st(0) (gcc regno = 8)
646         12 for %st(1) (gcc regno = 9)
647         13 for %st(2) (gcc regno = 10)
648         14 for %st(3) (gcc regno = 11)
649         15 for %st(4) (gcc regno = 12)
650         16 for %st(5) (gcc regno = 13)
651         17 for %st(6) (gcc regno = 14)
652         18 for %st(7) (gcc regno = 15)
653 */
654 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
655 {
656   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
657   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
658   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, dir, frame */
659   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
660   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
661   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
662   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
663 };
664
665 /* Test and compare insns in i386.md store the information needed to
666    generate branch and scc insns here.  */
667
668 rtx ix86_compare_op0 = NULL_RTX;
669 rtx ix86_compare_op1 = NULL_RTX;
670
671 #define MAX_386_STACK_LOCALS 3
672 /* Size of the register save area.  */
673 #define X86_64_VARARGS_SIZE (REGPARM_MAX * UNITS_PER_WORD + SSE_REGPARM_MAX * 16)
674
675 /* Define the structure for the machine field in struct function.  */
676
677 struct stack_local_entry GTY(())
678 {
679   unsigned short mode;
680   unsigned short n;
681   rtx rtl;
682   struct stack_local_entry *next;
683 };
684
685 /* Structure describing stack frame layout.
686    Stack grows downward:
687
688    [arguments]
689                                               <- ARG_POINTER
690    saved pc
691
692    saved frame pointer if frame_pointer_needed
693                                               <- HARD_FRAME_POINTER
694    [saved regs]
695
696    [padding1]          \
697                         )
698    [va_arg registers]  (
699                         > to_allocate         <- FRAME_POINTER
700    [frame]             (
701                         )
702    [padding2]          /
703   */
704 struct ix86_frame
705 {
706   int nregs;
707   int padding1;
708   int va_arg_size;
709   HOST_WIDE_INT frame;
710   int padding2;
711   int outgoing_arguments_size;
712   int red_zone_size;
713
714   HOST_WIDE_INT to_allocate;
715   /* The offsets relative to ARG_POINTER.  */
716   HOST_WIDE_INT frame_pointer_offset;
717   HOST_WIDE_INT hard_frame_pointer_offset;
718   HOST_WIDE_INT stack_pointer_offset;
719
720   /* When save_regs_using_mov is set, emit prologue using
721      move instead of push instructions.  */
722   bool save_regs_using_mov;
723 };
724
725 /* Used to enable/disable debugging features.  */
726 const char *ix86_debug_arg_string, *ix86_debug_addr_string;
727 /* Code model option as passed by user.  */
728 const char *ix86_cmodel_string;
729 /* Parsed value.  */
730 enum cmodel ix86_cmodel;
731 /* Asm dialect.  */
732 const char *ix86_asm_string;
733 enum asm_dialect ix86_asm_dialect = ASM_ATT;
734 /* TLS dialext.  */
735 const char *ix86_tls_dialect_string;
736 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
737
738 /* Which unit we are generating floating point math for.  */
739 enum fpmath_unit ix86_fpmath;
740
741 /* Which cpu are we scheduling for.  */
742 enum processor_type ix86_tune;
743 /* Which instruction set architecture to use.  */
744 enum processor_type ix86_arch;
745
746 /* Strings to hold which cpu and instruction set architecture  to use.  */
747 const char *ix86_tune_string;           /* for -mtune=<xxx> */
748 const char *ix86_arch_string;           /* for -march=<xxx> */
749 const char *ix86_fpmath_string;         /* for -mfpmath=<xxx> */
750
751 /* # of registers to use to pass arguments.  */
752 const char *ix86_regparm_string;
753
754 /* true if sse prefetch instruction is not NOOP.  */
755 int x86_prefetch_sse;
756
757 /* ix86_regparm_string as a number */
758 int ix86_regparm;
759
760 /* Alignment to use for loops and jumps:  */
761
762 /* Power of two alignment for loops.  */
763 const char *ix86_align_loops_string;
764
765 /* Power of two alignment for non-loop jumps.  */
766 const char *ix86_align_jumps_string;
767
768 /* Power of two alignment for stack boundary in bytes.  */
769 const char *ix86_preferred_stack_boundary_string;
770
771 /* Preferred alignment for stack boundary in bits.  */
772 int ix86_preferred_stack_boundary;
773
774 /* Values 1-5: see jump.c */
775 int ix86_branch_cost;
776 const char *ix86_branch_cost_string;
777
778 /* Power of two alignment for functions.  */
779 const char *ix86_align_funcs_string;
780
781 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
782 static char internal_label_prefix[16];
783 static int internal_label_prefix_len;
784 \f
785 static int local_symbolic_operand (rtx, enum machine_mode);
786 static int tls_symbolic_operand_1 (rtx, enum tls_model);
787 static void output_pic_addr_const (FILE *, rtx, int);
788 static void put_condition_code (enum rtx_code, enum machine_mode,
789                                 int, int, FILE *);
790 static const char *get_some_local_dynamic_name (void);
791 static int get_some_local_dynamic_name_1 (rtx *, void *);
792 static rtx maybe_get_pool_constant (rtx);
793 static rtx ix86_expand_int_compare (enum rtx_code, rtx, rtx);
794 static enum rtx_code ix86_prepare_fp_compare_args (enum rtx_code, rtx *,
795                                                    rtx *);
796 static rtx get_thread_pointer (int);
797 static rtx legitimize_tls_address (rtx, enum tls_model, int);
798 static void get_pc_thunk_name (char [32], unsigned int);
799 static rtx gen_push (rtx);
800 static int memory_address_length (rtx addr);
801 static int ix86_flags_dependant (rtx, rtx, enum attr_type);
802 static int ix86_agi_dependant (rtx, rtx, enum attr_type);
803 static enum attr_ppro_uops ix86_safe_ppro_uops (rtx);
804 static void ix86_dump_ppro_packet (FILE *);
805 static void ix86_reorder_insn (rtx *, rtx *);
806 static struct machine_function * ix86_init_machine_status (void);
807 static int ix86_split_to_parts (rtx, rtx *, enum machine_mode);
808 static int ix86_nsaved_regs (void);
809 static void ix86_emit_save_regs (void);
810 static void ix86_emit_save_regs_using_mov (rtx, HOST_WIDE_INT);
811 static void ix86_emit_restore_regs_using_mov (rtx, HOST_WIDE_INT, int);
812 static void ix86_output_function_epilogue (FILE *, HOST_WIDE_INT);
813 static void ix86_set_move_mem_attrs_1 (rtx, rtx, rtx, rtx, rtx);
814 static void ix86_sched_reorder_ppro (rtx *, rtx *);
815 static HOST_WIDE_INT ix86_GOT_alias_set (void);
816 static void ix86_adjust_counter (rtx, HOST_WIDE_INT);
817 static rtx ix86_expand_aligntest (rtx, int);
818 static void ix86_expand_strlensi_unroll_1 (rtx, rtx);
819 static int ix86_issue_rate (void);
820 static int ix86_adjust_cost (rtx, rtx, rtx, int);
821 static void ix86_sched_init (FILE *, int, int);
822 static int ix86_sched_reorder (FILE *, int, rtx *, int *, int);
823 static int ix86_variable_issue (FILE *, int, rtx, int);
824 static int ia32_use_dfa_pipeline_interface (void);
825 static int ia32_multipass_dfa_lookahead (void);
826 static void ix86_init_mmx_sse_builtins (void);
827 static rtx x86_this_parameter (tree);
828 static void x86_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
829                                  HOST_WIDE_INT, tree);
830 static bool x86_can_output_mi_thunk (tree, HOST_WIDE_INT, HOST_WIDE_INT, tree);
831 static void x86_file_start (void);
832 static void ix86_reorg (void);
833 static bool ix86_expand_carry_flag_compare (enum rtx_code, rtx, rtx, rtx*);
834 static tree ix86_build_builtin_va_list (void);
835
836 struct ix86_address
837 {
838   rtx base, index, disp;
839   HOST_WIDE_INT scale;
840   enum ix86_address_seg { SEG_DEFAULT, SEG_FS, SEG_GS } seg;
841 };
842
843 static int ix86_decompose_address (rtx, struct ix86_address *);
844 static int ix86_address_cost (rtx);
845 static bool ix86_cannot_force_const_mem (rtx);
846 static rtx ix86_delegitimize_address (rtx);
847
848 struct builtin_description;
849 static rtx ix86_expand_sse_comi (const struct builtin_description *,
850                                  tree, rtx);
851 static rtx ix86_expand_sse_compare (const struct builtin_description *,
852                                     tree, rtx);
853 static rtx ix86_expand_unop1_builtin (enum insn_code, tree, rtx);
854 static rtx ix86_expand_unop_builtin (enum insn_code, tree, rtx, int);
855 static rtx ix86_expand_binop_builtin (enum insn_code, tree, rtx);
856 static rtx ix86_expand_store_builtin (enum insn_code, tree);
857 static rtx safe_vector_operand (rtx, enum machine_mode);
858 static enum rtx_code ix86_fp_compare_code_to_integer (enum rtx_code);
859 static void ix86_fp_comparison_codes (enum rtx_code code, enum rtx_code *,
860                                       enum rtx_code *, enum rtx_code *);
861 static rtx ix86_expand_fp_compare (enum rtx_code, rtx, rtx, rtx, rtx *, rtx *);
862 static int ix86_fp_comparison_arithmetics_cost (enum rtx_code code);
863 static int ix86_fp_comparison_fcomi_cost (enum rtx_code code);
864 static int ix86_fp_comparison_sahf_cost (enum rtx_code code);
865 static int ix86_fp_comparison_cost (enum rtx_code code);
866 static unsigned int ix86_select_alt_pic_regnum (void);
867 static int ix86_save_reg (unsigned int, int);
868 static void ix86_compute_frame_layout (struct ix86_frame *);
869 static int ix86_comp_type_attributes (tree, tree);
870 static int ix86_function_regparm (tree, tree);
871 const struct attribute_spec ix86_attribute_table[];
872 static bool ix86_function_ok_for_sibcall (tree, tree);
873 static tree ix86_handle_cdecl_attribute (tree *, tree, tree, int, bool *);
874 static tree ix86_handle_regparm_attribute (tree *, tree, tree, int, bool *);
875 static int ix86_value_regno (enum machine_mode);
876 static bool contains_128bit_aligned_vector_p (tree);
877 static bool ix86_ms_bitfield_layout_p (tree);
878 static tree ix86_handle_struct_attribute (tree *, tree, tree, int, bool *);
879 static int extended_reg_mentioned_1 (rtx *, void *);
880 static bool ix86_rtx_costs (rtx, int, int, int *);
881 static int min_insn_size (rtx);
882 static void k8_avoid_jump_misspredicts (void);
883
884 #if defined (DO_GLOBAL_CTORS_BODY) && defined (HAS_INIT_SECTION)
885 static void ix86_svr3_asm_out_constructor (rtx, int);
886 #endif
887
888 /* Register class used for passing given 64bit part of the argument.
889    These represent classes as documented by the PS ABI, with the exception
890    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
891    use SF or DFmode move instead of DImode to avoid reformatting penalties.
892
893    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
894    whenever possible (upper half does contain padding).
895  */
896 enum x86_64_reg_class
897   {
898     X86_64_NO_CLASS,
899     X86_64_INTEGER_CLASS,
900     X86_64_INTEGERSI_CLASS,
901     X86_64_SSE_CLASS,
902     X86_64_SSESF_CLASS,
903     X86_64_SSEDF_CLASS,
904     X86_64_SSEUP_CLASS,
905     X86_64_X87_CLASS,
906     X86_64_X87UP_CLASS,
907     X86_64_MEMORY_CLASS
908   };
909 static const char * const x86_64_reg_class_name[] =
910    {"no", "integer", "integerSI", "sse", "sseSF", "sseDF", "sseup", "x87", "x87up", "no"};
911
912 #define MAX_CLASSES 4
913 static int classify_argument (enum machine_mode, tree,
914                               enum x86_64_reg_class [MAX_CLASSES], int);
915 static int examine_argument (enum machine_mode, tree, int, int *, int *);
916 static rtx construct_container (enum machine_mode, tree, int, int, int,
917                                 const int *, int);
918 static enum x86_64_reg_class merge_classes (enum x86_64_reg_class,
919                                             enum x86_64_reg_class);
920
921 /* Table of constants used by fldpi, fldln2, etc....  */
922 static REAL_VALUE_TYPE ext_80387_constants_table [5];
923 static bool ext_80387_constants_init = 0;
924 static void init_ext_80387_constants (void);
925 \f
926 /* Initialize the GCC target structure.  */
927 #undef TARGET_ATTRIBUTE_TABLE
928 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
929 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
930 #  undef TARGET_MERGE_DECL_ATTRIBUTES
931 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
932 #endif
933
934 #undef TARGET_COMP_TYPE_ATTRIBUTES
935 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
936
937 #undef TARGET_INIT_BUILTINS
938 #define TARGET_INIT_BUILTINS ix86_init_builtins
939
940 #undef TARGET_EXPAND_BUILTIN
941 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
942
943 #undef TARGET_ASM_FUNCTION_EPILOGUE
944 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
945
946 #undef TARGET_ASM_OPEN_PAREN
947 #define TARGET_ASM_OPEN_PAREN ""
948 #undef TARGET_ASM_CLOSE_PAREN
949 #define TARGET_ASM_CLOSE_PAREN ""
950
951 #undef TARGET_ASM_ALIGNED_HI_OP
952 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
953 #undef TARGET_ASM_ALIGNED_SI_OP
954 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
955 #ifdef ASM_QUAD
956 #undef TARGET_ASM_ALIGNED_DI_OP
957 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
958 #endif
959
960 #undef TARGET_ASM_UNALIGNED_HI_OP
961 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
962 #undef TARGET_ASM_UNALIGNED_SI_OP
963 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
964 #undef TARGET_ASM_UNALIGNED_DI_OP
965 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
966
967 #undef TARGET_SCHED_ADJUST_COST
968 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
969 #undef TARGET_SCHED_ISSUE_RATE
970 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
971 #undef TARGET_SCHED_VARIABLE_ISSUE
972 #define TARGET_SCHED_VARIABLE_ISSUE ix86_variable_issue
973 #undef TARGET_SCHED_INIT
974 #define TARGET_SCHED_INIT ix86_sched_init
975 #undef TARGET_SCHED_REORDER
976 #define TARGET_SCHED_REORDER ix86_sched_reorder
977 #undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE
978 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE \
979   ia32_use_dfa_pipeline_interface
980 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
981 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
982   ia32_multipass_dfa_lookahead
983
984 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
985 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
986
987 #ifdef HAVE_AS_TLS
988 #undef TARGET_HAVE_TLS
989 #define TARGET_HAVE_TLS true
990 #endif
991 #undef TARGET_CANNOT_FORCE_CONST_MEM
992 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
993
994 #undef TARGET_DELEGITIMIZE_ADDRESS
995 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
996
997 #undef TARGET_MS_BITFIELD_LAYOUT_P
998 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
999
1000 #undef TARGET_ASM_OUTPUT_MI_THUNK
1001 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
1002 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1003 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
1004
1005 #undef TARGET_ASM_FILE_START
1006 #define TARGET_ASM_FILE_START x86_file_start
1007
1008 #undef TARGET_RTX_COSTS
1009 #define TARGET_RTX_COSTS ix86_rtx_costs
1010 #undef TARGET_ADDRESS_COST
1011 #define TARGET_ADDRESS_COST ix86_address_cost
1012
1013 #undef TARGET_MACHINE_DEPENDENT_REORG
1014 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
1015
1016 #undef TARGET_BUILD_BUILTIN_VA_LIST
1017 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
1018
1019 struct gcc_target targetm = TARGET_INITIALIZER;
1020 \f
1021 /* The svr4 ABI for the i386 says that records and unions are returned
1022    in memory.  */
1023 #ifndef DEFAULT_PCC_STRUCT_RETURN
1024 #define DEFAULT_PCC_STRUCT_RETURN 1
1025 #endif
1026
1027 /* Sometimes certain combinations of command options do not make
1028    sense on a particular target machine.  You can define a macro
1029    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
1030    defined, is executed once just after all the command options have
1031    been parsed.
1032
1033    Don't use this macro to turn on various extra optimizations for
1034    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
1035
1036 void
1037 override_options (void)
1038 {
1039   int i;
1040   /* Comes from final.c -- no real reason to change it.  */
1041 #define MAX_CODE_ALIGN 16
1042
1043   static struct ptt
1044     {
1045       const struct processor_costs *cost;       /* Processor costs */
1046       const int target_enable;                  /* Target flags to enable.  */
1047       const int target_disable;                 /* Target flags to disable.  */
1048       const int align_loop;                     /* Default alignments.  */
1049       const int align_loop_max_skip;
1050       const int align_jump;
1051       const int align_jump_max_skip;
1052       const int align_func;
1053     }
1054   const processor_target_table[PROCESSOR_max] =
1055     {
1056       {&i386_cost, 0, 0, 4, 3, 4, 3, 4},
1057       {&i486_cost, 0, 0, 16, 15, 16, 15, 16},
1058       {&pentium_cost, 0, 0, 16, 7, 16, 7, 16},
1059       {&pentiumpro_cost, 0, 0, 16, 15, 16, 7, 16},
1060       {&k6_cost, 0, 0, 32, 7, 32, 7, 32},
1061       {&athlon_cost, 0, 0, 16, 7, 16, 7, 16},
1062       {&pentium4_cost, 0, 0, 0, 0, 0, 0, 0},
1063       {&k8_cost, 0, 0, 16, 7, 16, 7, 16}
1064     };
1065
1066   static const char * const cpu_names[] = TARGET_CPU_DEFAULT_NAMES;
1067   static struct pta
1068     {
1069       const char *const name;           /* processor name or nickname.  */
1070       const enum processor_type processor;
1071       const enum pta_flags
1072         {
1073           PTA_SSE = 1,
1074           PTA_SSE2 = 2,
1075           PTA_MMX = 4,
1076           PTA_PREFETCH_SSE = 8,
1077           PTA_3DNOW = 16,
1078           PTA_3DNOW_A = 64,
1079           PTA_64BIT = 128
1080         } flags;
1081     }
1082   const processor_alias_table[] =
1083     {
1084       {"i386", PROCESSOR_I386, 0},
1085       {"i486", PROCESSOR_I486, 0},
1086       {"i586", PROCESSOR_PENTIUM, 0},
1087       {"pentium", PROCESSOR_PENTIUM, 0},
1088       {"pentium-mmx", PROCESSOR_PENTIUM, PTA_MMX},
1089       {"winchip-c6", PROCESSOR_I486, PTA_MMX},
1090       {"winchip2", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1091       {"c3", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1092       {"c3-2", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_PREFETCH_SSE | PTA_SSE},
1093       {"i686", PROCESSOR_PENTIUMPRO, 0},
1094       {"pentiumpro", PROCESSOR_PENTIUMPRO, 0},
1095       {"pentium2", PROCESSOR_PENTIUMPRO, PTA_MMX},
1096       {"pentium3", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1097       {"pentium4", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2 |
1098                                        PTA_MMX | PTA_PREFETCH_SSE},
1099       {"k6", PROCESSOR_K6, PTA_MMX},
1100       {"k6-2", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1101       {"k6-3", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1102       {"athlon", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1103                                    | PTA_3DNOW_A},
1104       {"athlon-tbird", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE
1105                                          | PTA_3DNOW | PTA_3DNOW_A},
1106       {"athlon-4", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1107                                     | PTA_3DNOW_A | PTA_SSE},
1108       {"athlon-xp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1109                                       | PTA_3DNOW_A | PTA_SSE},
1110       {"athlon-mp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1111                                       | PTA_3DNOW_A | PTA_SSE},
1112       {"k8", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1113                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1114       {"opteron", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1115                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1116       {"athlon64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1117                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1118       {"athlon-fx", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1119                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1120     };
1121
1122   int const pta_size = ARRAY_SIZE (processor_alias_table);
1123
1124   /* Set the default values for switches whose default depends on TARGET_64BIT
1125      in case they weren't overwritten by command line options.  */
1126   if (TARGET_64BIT)
1127     {
1128       if (flag_omit_frame_pointer == 2)
1129         flag_omit_frame_pointer = 1;
1130       if (flag_asynchronous_unwind_tables == 2)
1131         flag_asynchronous_unwind_tables = 1;
1132       if (flag_pcc_struct_return == 2)
1133         flag_pcc_struct_return = 0;
1134     }
1135   else
1136     {
1137       if (flag_omit_frame_pointer == 2)
1138         flag_omit_frame_pointer = 0;
1139       if (flag_asynchronous_unwind_tables == 2)
1140         flag_asynchronous_unwind_tables = 0;
1141       if (flag_pcc_struct_return == 2)
1142         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
1143     }
1144
1145 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1146   SUBTARGET_OVERRIDE_OPTIONS;
1147 #endif
1148
1149   if (!ix86_tune_string && ix86_arch_string)
1150     ix86_tune_string = ix86_arch_string;
1151   if (!ix86_tune_string)
1152     ix86_tune_string = cpu_names [TARGET_CPU_DEFAULT];
1153   if (!ix86_arch_string)
1154     ix86_arch_string = TARGET_64BIT ? "k8" : "i386";
1155
1156   if (ix86_cmodel_string != 0)
1157     {
1158       if (!strcmp (ix86_cmodel_string, "small"))
1159         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1160       else if (flag_pic)
1161         sorry ("code model %s not supported in PIC mode", ix86_cmodel_string);
1162       else if (!strcmp (ix86_cmodel_string, "32"))
1163         ix86_cmodel = CM_32;
1164       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
1165         ix86_cmodel = CM_KERNEL;
1166       else if (!strcmp (ix86_cmodel_string, "medium") && !flag_pic)
1167         ix86_cmodel = CM_MEDIUM;
1168       else if (!strcmp (ix86_cmodel_string, "large") && !flag_pic)
1169         ix86_cmodel = CM_LARGE;
1170       else
1171         error ("bad value (%s) for -mcmodel= switch", ix86_cmodel_string);
1172     }
1173   else
1174     {
1175       ix86_cmodel = CM_32;
1176       if (TARGET_64BIT)
1177         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1178     }
1179   if (ix86_asm_string != 0)
1180     {
1181       if (!strcmp (ix86_asm_string, "intel"))
1182         ix86_asm_dialect = ASM_INTEL;
1183       else if (!strcmp (ix86_asm_string, "att"))
1184         ix86_asm_dialect = ASM_ATT;
1185       else
1186         error ("bad value (%s) for -masm= switch", ix86_asm_string);
1187     }
1188   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
1189     error ("code model `%s' not supported in the %s bit mode",
1190            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
1191   if (ix86_cmodel == CM_LARGE)
1192     sorry ("code model `large' not supported yet");
1193   if ((TARGET_64BIT != 0) != ((target_flags & MASK_64BIT) != 0))
1194     sorry ("%i-bit mode not compiled in",
1195            (target_flags & MASK_64BIT) ? 64 : 32);
1196
1197   for (i = 0; i < pta_size; i++)
1198     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
1199       {
1200         ix86_arch = processor_alias_table[i].processor;
1201         /* Default cpu tuning to the architecture.  */
1202         ix86_tune = ix86_arch;
1203         if (processor_alias_table[i].flags & PTA_MMX
1204             && !(target_flags_explicit & MASK_MMX))
1205           target_flags |= MASK_MMX;
1206         if (processor_alias_table[i].flags & PTA_3DNOW
1207             && !(target_flags_explicit & MASK_3DNOW))
1208           target_flags |= MASK_3DNOW;
1209         if (processor_alias_table[i].flags & PTA_3DNOW_A
1210             && !(target_flags_explicit & MASK_3DNOW_A))
1211           target_flags |= MASK_3DNOW_A;
1212         if (processor_alias_table[i].flags & PTA_SSE
1213             && !(target_flags_explicit & MASK_SSE))
1214           target_flags |= MASK_SSE;
1215         if (processor_alias_table[i].flags & PTA_SSE2
1216             && !(target_flags_explicit & MASK_SSE2))
1217           target_flags |= MASK_SSE2;
1218         if (processor_alias_table[i].flags & PTA_PREFETCH_SSE)
1219           x86_prefetch_sse = true;
1220         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1221           error ("CPU you selected does not support x86-64 instruction set");
1222         break;
1223       }
1224
1225   if (i == pta_size)
1226     error ("bad value (%s) for -march= switch", ix86_arch_string);
1227
1228   for (i = 0; i < pta_size; i++)
1229     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
1230       {
1231         ix86_tune = processor_alias_table[i].processor;
1232         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1233           error ("CPU you selected does not support x86-64 instruction set");
1234         break;
1235       }
1236   if (processor_alias_table[i].flags & PTA_PREFETCH_SSE)
1237     x86_prefetch_sse = true;
1238   if (i == pta_size)
1239     error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1240
1241   if (optimize_size)
1242     ix86_cost = &size_cost;
1243   else
1244     ix86_cost = processor_target_table[ix86_tune].cost;
1245   target_flags |= processor_target_table[ix86_tune].target_enable;
1246   target_flags &= ~processor_target_table[ix86_tune].target_disable;
1247
1248   /* Arrange to set up i386_stack_locals for all functions.  */
1249   init_machine_status = ix86_init_machine_status;
1250
1251   /* Validate -mregparm= value.  */
1252   if (ix86_regparm_string)
1253     {
1254       i = atoi (ix86_regparm_string);
1255       if (i < 0 || i > REGPARM_MAX)
1256         error ("-mregparm=%d is not between 0 and %d", i, REGPARM_MAX);
1257       else
1258         ix86_regparm = i;
1259     }
1260   else
1261    if (TARGET_64BIT)
1262      ix86_regparm = REGPARM_MAX;
1263
1264   /* If the user has provided any of the -malign-* options,
1265      warn and use that value only if -falign-* is not set.
1266      Remove this code in GCC 3.2 or later.  */
1267   if (ix86_align_loops_string)
1268     {
1269       warning ("-malign-loops is obsolete, use -falign-loops");
1270       if (align_loops == 0)
1271         {
1272           i = atoi (ix86_align_loops_string);
1273           if (i < 0 || i > MAX_CODE_ALIGN)
1274             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1275           else
1276             align_loops = 1 << i;
1277         }
1278     }
1279
1280   if (ix86_align_jumps_string)
1281     {
1282       warning ("-malign-jumps is obsolete, use -falign-jumps");
1283       if (align_jumps == 0)
1284         {
1285           i = atoi (ix86_align_jumps_string);
1286           if (i < 0 || i > MAX_CODE_ALIGN)
1287             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1288           else
1289             align_jumps = 1 << i;
1290         }
1291     }
1292
1293   if (ix86_align_funcs_string)
1294     {
1295       warning ("-malign-functions is obsolete, use -falign-functions");
1296       if (align_functions == 0)
1297         {
1298           i = atoi (ix86_align_funcs_string);
1299           if (i < 0 || i > MAX_CODE_ALIGN)
1300             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1301           else
1302             align_functions = 1 << i;
1303         }
1304     }
1305
1306   /* Default align_* from the processor table.  */
1307   if (align_loops == 0)
1308     {
1309       align_loops = processor_target_table[ix86_tune].align_loop;
1310       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
1311     }
1312   if (align_jumps == 0)
1313     {
1314       align_jumps = processor_target_table[ix86_tune].align_jump;
1315       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
1316     }
1317   if (align_functions == 0)
1318     {
1319       align_functions = processor_target_table[ix86_tune].align_func;
1320     }
1321
1322   /* Validate -mpreferred-stack-boundary= value, or provide default.
1323      The default of 128 bits is for Pentium III's SSE __m128, but we
1324      don't want additional code to keep the stack aligned when
1325      optimizing for code size.  */
1326   ix86_preferred_stack_boundary = (optimize_size
1327                                    ? TARGET_64BIT ? 128 : 32
1328                                    : 128);
1329   if (ix86_preferred_stack_boundary_string)
1330     {
1331       i = atoi (ix86_preferred_stack_boundary_string);
1332       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
1333         error ("-mpreferred-stack-boundary=%d is not between %d and 12", i,
1334                TARGET_64BIT ? 4 : 2);
1335       else
1336         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
1337     }
1338
1339   /* Validate -mbranch-cost= value, or provide default.  */
1340   ix86_branch_cost = processor_target_table[ix86_tune].cost->branch_cost;
1341   if (ix86_branch_cost_string)
1342     {
1343       i = atoi (ix86_branch_cost_string);
1344       if (i < 0 || i > 5)
1345         error ("-mbranch-cost=%d is not between 0 and 5", i);
1346       else
1347         ix86_branch_cost = i;
1348     }
1349
1350   if (ix86_tls_dialect_string)
1351     {
1352       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
1353         ix86_tls_dialect = TLS_DIALECT_GNU;
1354       else if (strcmp (ix86_tls_dialect_string, "sun") == 0)
1355         ix86_tls_dialect = TLS_DIALECT_SUN;
1356       else
1357         error ("bad value (%s) for -mtls-dialect= switch",
1358                ix86_tls_dialect_string);
1359     }
1360
1361   /* Keep nonleaf frame pointers.  */
1362   if (TARGET_OMIT_LEAF_FRAME_POINTER)
1363     flag_omit_frame_pointer = 1;
1364
1365   /* If we're doing fast math, we don't care about comparison order
1366      wrt NaNs.  This lets us use a shorter comparison sequence.  */
1367   if (flag_unsafe_math_optimizations)
1368     target_flags &= ~MASK_IEEE_FP;
1369
1370   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
1371      since the insns won't need emulation.  */
1372   if (x86_arch_always_fancy_math_387 & (1 << ix86_arch))
1373     target_flags &= ~MASK_NO_FANCY_MATH_387;
1374
1375   /* Turn on SSE2 builtins for -mpni.  */
1376   if (TARGET_PNI)
1377     target_flags |= MASK_SSE2;
1378
1379   /* Turn on SSE builtins for -msse2.  */
1380   if (TARGET_SSE2)
1381     target_flags |= MASK_SSE;
1382
1383   if (TARGET_64BIT)
1384     {
1385       if (TARGET_ALIGN_DOUBLE)
1386         error ("-malign-double makes no sense in the 64bit mode");
1387       if (TARGET_RTD)
1388         error ("-mrtd calling convention not supported in the 64bit mode");
1389       /* Enable by default the SSE and MMX builtins.  */
1390       target_flags |= (MASK_SSE2 | MASK_SSE | MASK_MMX | MASK_128BIT_LONG_DOUBLE);
1391       ix86_fpmath = FPMATH_SSE;
1392      }
1393   else
1394     {
1395       ix86_fpmath = FPMATH_387;
1396       /* i386 ABI does not specify red zone.  It still makes sense to use it
1397          when programmer takes care to stack from being destroyed.  */
1398       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
1399         target_flags |= MASK_NO_RED_ZONE;
1400     }
1401
1402   if (ix86_fpmath_string != 0)
1403     {
1404       if (! strcmp (ix86_fpmath_string, "387"))
1405         ix86_fpmath = FPMATH_387;
1406       else if (! strcmp (ix86_fpmath_string, "sse"))
1407         {
1408           if (!TARGET_SSE)
1409             {
1410               warning ("SSE instruction set disabled, using 387 arithmetics");
1411               ix86_fpmath = FPMATH_387;
1412             }
1413           else
1414             ix86_fpmath = FPMATH_SSE;
1415         }
1416       else if (! strcmp (ix86_fpmath_string, "387,sse")
1417                || ! strcmp (ix86_fpmath_string, "sse,387"))
1418         {
1419           if (!TARGET_SSE)
1420             {
1421               warning ("SSE instruction set disabled, using 387 arithmetics");
1422               ix86_fpmath = FPMATH_387;
1423             }
1424           else if (!TARGET_80387)
1425             {
1426               warning ("387 instruction set disabled, using SSE arithmetics");
1427               ix86_fpmath = FPMATH_SSE;
1428             }
1429           else
1430             ix86_fpmath = FPMATH_SSE | FPMATH_387;
1431         }
1432       else
1433         error ("bad value (%s) for -mfpmath= switch", ix86_fpmath_string);
1434     }
1435
1436   /* It makes no sense to ask for just SSE builtins, so MMX is also turned
1437      on by -msse.  */
1438   if (TARGET_SSE)
1439     {
1440       target_flags |= MASK_MMX;
1441       x86_prefetch_sse = true;
1442     }
1443
1444   /* If it has 3DNow! it also has MMX so MMX is also turned on by -m3dnow */
1445   if (TARGET_3DNOW)
1446     {
1447       target_flags |= MASK_MMX;
1448       /* If we are targeting the Athlon architecture, enable the 3Dnow/MMX
1449          extensions it adds.  */
1450       if (x86_3dnow_a & (1 << ix86_arch))
1451         target_flags |= MASK_3DNOW_A;
1452     }
1453   if ((x86_accumulate_outgoing_args & TUNEMASK)
1454       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
1455       && !optimize_size)
1456     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
1457
1458   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
1459   {
1460     char *p;
1461     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
1462     p = strchr (internal_label_prefix, 'X');
1463     internal_label_prefix_len = p - internal_label_prefix;
1464     *p = '\0';
1465   }
1466 }
1467 \f
1468 void
1469 optimization_options (int level, int size ATTRIBUTE_UNUSED)
1470 {
1471   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
1472      make the problem with not enough registers even worse.  */
1473 #ifdef INSN_SCHEDULING
1474   if (level > 1)
1475     flag_schedule_insns = 0;
1476 #endif
1477
1478   /* The default values of these switches depend on the TARGET_64BIT
1479      that is not known at this moment.  Mark these values with 2 and
1480      let user the to override these.  In case there is no command line option
1481      specifying them, we will set the defaults in override_options.  */
1482   if (optimize >= 1)
1483     flag_omit_frame_pointer = 2;
1484   flag_pcc_struct_return = 2;
1485   flag_asynchronous_unwind_tables = 2;
1486 }
1487 \f
1488 /* Table of valid machine attributes.  */
1489 const struct attribute_spec ix86_attribute_table[] =
1490 {
1491   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
1492   /* Stdcall attribute says callee is responsible for popping arguments
1493      if they are not variable.  */
1494   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cdecl_attribute },
1495   /* Fastcall attribute says callee is responsible for popping arguments
1496      if they are not variable.  */
1497   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cdecl_attribute },
1498   /* Cdecl attribute says the callee is a normal C declaration */
1499   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cdecl_attribute },
1500   /* Regparm attribute specifies how many integer arguments are to be
1501      passed in registers.  */
1502   { "regparm",   1, 1, false, true,  true,  ix86_handle_regparm_attribute },
1503 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
1504   { "dllimport", 0, 0, false, false, false, ix86_handle_dll_attribute },
1505   { "dllexport", 0, 0, false, false, false, ix86_handle_dll_attribute },
1506   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
1507 #endif
1508   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
1509   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
1510   { NULL,        0, 0, false, false, false, NULL }
1511 };
1512
1513 /* Decide whether we can make a sibling call to a function.  DECL is the
1514    declaration of the function being targeted by the call and EXP is the
1515    CALL_EXPR representing the call.  */
1516
1517 static bool
1518 ix86_function_ok_for_sibcall (tree decl, tree exp)
1519 {
1520   /* If we are generating position-independent code, we cannot sibcall
1521      optimize any indirect call, or a direct call to a global function,
1522      as the PLT requires %ebx be live.  */
1523   if (!TARGET_64BIT && flag_pic && (!decl || TREE_PUBLIC (decl)))
1524     return false;
1525
1526   /* If we are returning floats on the 80387 register stack, we cannot
1527      make a sibcall from a function that doesn't return a float to a
1528      function that does or, conversely, from a function that does return
1529      a float to a function that doesn't; the necessary stack adjustment
1530      would not be executed.  */
1531   if (STACK_REG_P (ix86_function_value (TREE_TYPE (exp)))
1532       != STACK_REG_P (ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)))))
1533     return false;
1534
1535   /* If this call is indirect, we'll need to be able to use a call-clobbered
1536      register for the address of the target function.  Make sure that all
1537      such registers are not used for passing parameters.  */
1538   if (!decl && !TARGET_64BIT)
1539     {
1540       tree type;
1541
1542       /* We're looking at the CALL_EXPR, we need the type of the function.  */
1543       type = TREE_OPERAND (exp, 0);             /* pointer expression */
1544       type = TREE_TYPE (type);                  /* pointer type */
1545       type = TREE_TYPE (type);                  /* function type */
1546
1547       if (ix86_function_regparm (type, NULL) >= 3)
1548         {
1549           /* ??? Need to count the actual number of registers to be used,
1550              not the possible number of registers.  Fix later.  */
1551           return false;
1552         }
1553     }
1554
1555   /* Otherwise okay.  That also includes certain types of indirect calls.  */
1556   return true;
1557 }
1558
1559 /* Handle a "cdecl", "stdcall", or "fastcall" attribute;
1560    arguments as in struct attribute_spec.handler.  */
1561 static tree
1562 ix86_handle_cdecl_attribute (tree *node, tree name,
1563                              tree args ATTRIBUTE_UNUSED,
1564                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1565 {
1566   if (TREE_CODE (*node) != FUNCTION_TYPE
1567       && TREE_CODE (*node) != METHOD_TYPE
1568       && TREE_CODE (*node) != FIELD_DECL
1569       && TREE_CODE (*node) != TYPE_DECL)
1570     {
1571       warning ("`%s' attribute only applies to functions",
1572                IDENTIFIER_POINTER (name));
1573       *no_add_attrs = true;
1574     }
1575   else
1576     {
1577       if (is_attribute_p ("fastcall", name))
1578         {
1579           if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
1580             {
1581               error ("fastcall and stdcall attributes are not compatible");
1582             }
1583            else if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
1584             {
1585               error ("fastcall and regparm attributes are not compatible");
1586             }
1587         }
1588       else if (is_attribute_p ("stdcall", name))
1589         {
1590           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
1591             {
1592               error ("fastcall and stdcall attributes are not compatible");
1593             }
1594         }
1595     }
1596
1597   if (TARGET_64BIT)
1598     {
1599       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1600       *no_add_attrs = true;
1601     }
1602
1603   return NULL_TREE;
1604 }
1605
1606 /* Handle a "regparm" attribute;
1607    arguments as in struct attribute_spec.handler.  */
1608 static tree
1609 ix86_handle_regparm_attribute (tree *node, tree name, tree args,
1610                                int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1611 {
1612   if (TREE_CODE (*node) != FUNCTION_TYPE
1613       && TREE_CODE (*node) != METHOD_TYPE
1614       && TREE_CODE (*node) != FIELD_DECL
1615       && TREE_CODE (*node) != TYPE_DECL)
1616     {
1617       warning ("`%s' attribute only applies to functions",
1618                IDENTIFIER_POINTER (name));
1619       *no_add_attrs = true;
1620     }
1621   else
1622     {
1623       tree cst;
1624
1625       cst = TREE_VALUE (args);
1626       if (TREE_CODE (cst) != INTEGER_CST)
1627         {
1628           warning ("`%s' attribute requires an integer constant argument",
1629                    IDENTIFIER_POINTER (name));
1630           *no_add_attrs = true;
1631         }
1632       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
1633         {
1634           warning ("argument to `%s' attribute larger than %d",
1635                    IDENTIFIER_POINTER (name), REGPARM_MAX);
1636           *no_add_attrs = true;
1637         }
1638
1639       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
1640         {
1641           error ("fastcall and regparm attributes are not compatible");
1642         }
1643     }
1644
1645   return NULL_TREE;
1646 }
1647
1648 /* Return 0 if the attributes for two types are incompatible, 1 if they
1649    are compatible, and 2 if they are nearly compatible (which causes a
1650    warning to be generated).  */
1651
1652 static int
1653 ix86_comp_type_attributes (tree type1, tree type2)
1654 {
1655   /* Check for mismatch of non-default calling convention.  */
1656   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
1657
1658   if (TREE_CODE (type1) != FUNCTION_TYPE)
1659     return 1;
1660
1661   /*  Check for mismatched fastcall types */
1662   if (!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
1663       != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
1664     return 0;
1665
1666   /* Check for mismatched return types (cdecl vs stdcall).  */
1667   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
1668       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
1669     return 0;
1670   return 1;
1671 }
1672 \f
1673 /* Return the regparm value for a fuctio with the indicated TYPE and DECL.
1674    DECL may be NULL when calling function indirectly
1675    or considering a libcall.  */
1676
1677 static int
1678 ix86_function_regparm (tree type, tree decl)
1679 {
1680   tree attr;
1681   int regparm = ix86_regparm;
1682   bool user_convention = false;
1683
1684   if (!TARGET_64BIT)
1685     {
1686       attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
1687       if (attr)
1688         {
1689           regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
1690           user_convention = true;
1691         }
1692
1693       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
1694         {
1695           regparm = 2;
1696           user_convention = true;
1697         }
1698
1699       /* Use register calling convention for local functions when possible.  */
1700       if (!TARGET_64BIT && !user_convention && decl
1701           && flag_unit_at_a_time && !profile_flag)
1702         {
1703           struct cgraph_local_info *i = cgraph_local_info (decl);
1704           if (i && i->local)
1705             {
1706               /* We can't use regparm(3) for nested functions as these use
1707                  static chain pointer in third argument.  */
1708               if (DECL_CONTEXT (decl) && !DECL_NO_STATIC_CHAIN (decl))
1709                 regparm = 2;
1710               else
1711                 regparm = 3;
1712             }
1713         }
1714     }
1715   return regparm;
1716 }
1717
1718 /* Return true if EAX is live at the start of the function.  Used by 
1719    ix86_expand_prologue to determine if we need special help before
1720    calling allocate_stack_worker.  */
1721
1722 static bool
1723 ix86_eax_live_at_start_p (void)
1724 {
1725   /* Cheat.  Don't bother working forward from ix86_function_regparm
1726      to the function type to whether an actual argument is located in
1727      eax.  Instead just look at cfg info, which is still close enough
1728      to correct at this point.  This gives false positives for broken
1729      functions that might use uninitialized data that happens to be
1730      allocated in eax, but who cares?  */
1731   return REGNO_REG_SET_P (ENTRY_BLOCK_PTR->global_live_at_end, 0);
1732 }
1733
1734 /* Value is the number of bytes of arguments automatically
1735    popped when returning from a subroutine call.
1736    FUNDECL is the declaration node of the function (as a tree),
1737    FUNTYPE is the data type of the function (as a tree),
1738    or for a library call it is an identifier node for the subroutine name.
1739    SIZE is the number of bytes of arguments passed on the stack.
1740
1741    On the 80386, the RTD insn may be used to pop them if the number
1742      of args is fixed, but if the number is variable then the caller
1743      must pop them all.  RTD can't be used for library calls now
1744      because the library is compiled with the Unix compiler.
1745    Use of RTD is a selectable option, since it is incompatible with
1746    standard Unix calling sequences.  If the option is not selected,
1747    the caller must always pop the args.
1748
1749    The attribute stdcall is equivalent to RTD on a per module basis.  */
1750
1751 int
1752 ix86_return_pops_args (tree fundecl, tree funtype, int size)
1753 {
1754   int rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
1755
1756   /* Cdecl functions override -mrtd, and never pop the stack.  */
1757   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype))) {
1758
1759     /* Stdcall and fastcall functions will pop the stack if not
1760        variable args.  */
1761     if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
1762         || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype)))
1763       rtd = 1;
1764
1765     if (rtd
1766         && (TYPE_ARG_TYPES (funtype) == NULL_TREE
1767             || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (funtype)))
1768                 == void_type_node)))
1769       return size;
1770   }
1771
1772   /* Lose any fake structure return argument if it is passed on the stack.  */
1773   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
1774       && !TARGET_64BIT)
1775     {
1776       int nregs = ix86_function_regparm (funtype, fundecl);
1777
1778       if (!nregs)
1779         return GET_MODE_SIZE (Pmode);
1780     }
1781
1782   return 0;
1783 }
1784 \f
1785 /* Argument support functions.  */
1786
1787 /* Return true when register may be used to pass function parameters.  */
1788 bool
1789 ix86_function_arg_regno_p (int regno)
1790 {
1791   int i;
1792   if (!TARGET_64BIT)
1793     return (regno < REGPARM_MAX
1794             || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
1795   if (SSE_REGNO_P (regno) && TARGET_SSE)
1796     return true;
1797   /* RAX is used as hidden argument to va_arg functions.  */
1798   if (!regno)
1799     return true;
1800   for (i = 0; i < REGPARM_MAX; i++)
1801     if (regno == x86_64_int_parameter_registers[i])
1802       return true;
1803   return false;
1804 }
1805
1806 /* Initialize a variable CUM of type CUMULATIVE_ARGS
1807    for a call to a function whose data type is FNTYPE.
1808    For a library call, FNTYPE is 0.  */
1809
1810 void
1811 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
1812                       tree fntype,      /* tree ptr for function decl */
1813                       rtx libname,      /* SYMBOL_REF of library name or 0 */
1814                       tree fndecl)
1815 {
1816   static CUMULATIVE_ARGS zero_cum;
1817   tree param, next_param;
1818
1819   if (TARGET_DEBUG_ARG)
1820     {
1821       fprintf (stderr, "\ninit_cumulative_args (");
1822       if (fntype)
1823         fprintf (stderr, "fntype code = %s, ret code = %s",
1824                  tree_code_name[(int) TREE_CODE (fntype)],
1825                  tree_code_name[(int) TREE_CODE (TREE_TYPE (fntype))]);
1826       else
1827         fprintf (stderr, "no fntype");
1828
1829       if (libname)
1830         fprintf (stderr, ", libname = %s", XSTR (libname, 0));
1831     }
1832
1833   *cum = zero_cum;
1834
1835   /* Set up the number of registers to use for passing arguments.  */
1836   if (fntype)
1837     cum->nregs = ix86_function_regparm (fntype, fndecl);
1838   else
1839     cum->nregs = ix86_regparm;
1840   cum->sse_nregs = SSE_REGPARM_MAX;
1841   cum->mmx_nregs = MMX_REGPARM_MAX;
1842   cum->warn_sse = true;
1843   cum->warn_mmx = true;
1844   cum->maybe_vaarg = false;
1845
1846   /* Use ecx and edx registers if function has fastcall attribute */
1847   if (fntype && !TARGET_64BIT)
1848     {
1849       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
1850         {
1851           cum->nregs = 2;
1852           cum->fastcall = 1;
1853         }
1854     }
1855
1856
1857   /* Determine if this function has variable arguments.  This is
1858      indicated by the last argument being 'void_type_mode' if there
1859      are no variable arguments.  If there are variable arguments, then
1860      we won't pass anything in registers */
1861
1862   if (cum->nregs || !TARGET_MMX || !TARGET_SSE)
1863     {
1864       for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
1865            param != 0; param = next_param)
1866         {
1867           next_param = TREE_CHAIN (param);
1868           if (next_param == 0 && TREE_VALUE (param) != void_type_node)
1869             {
1870               if (!TARGET_64BIT)
1871                 {
1872                   cum->nregs = 0;
1873                   cum->sse_nregs = 0;
1874                   cum->mmx_nregs = 0;
1875                   cum->warn_sse = 0;
1876                   cum->warn_mmx = 0;
1877                   cum->fastcall = 0;
1878                 }
1879               cum->maybe_vaarg = true;
1880             }
1881         }
1882     }
1883   if ((!fntype && !libname)
1884       || (fntype && !TYPE_ARG_TYPES (fntype)))
1885     cum->maybe_vaarg = 1;
1886
1887   if (TARGET_DEBUG_ARG)
1888     fprintf (stderr, ", nregs=%d )\n", cum->nregs);
1889
1890   return;
1891 }
1892
1893 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
1894    of this code is to classify each 8bytes of incoming argument by the register
1895    class and assign registers accordingly.  */
1896
1897 /* Return the union class of CLASS1 and CLASS2.
1898    See the x86-64 PS ABI for details.  */
1899
1900 static enum x86_64_reg_class
1901 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
1902 {
1903   /* Rule #1: If both classes are equal, this is the resulting class.  */
1904   if (class1 == class2)
1905     return class1;
1906
1907   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
1908      the other class.  */
1909   if (class1 == X86_64_NO_CLASS)
1910     return class2;
1911   if (class2 == X86_64_NO_CLASS)
1912     return class1;
1913
1914   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
1915   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
1916     return X86_64_MEMORY_CLASS;
1917
1918   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
1919   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
1920       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
1921     return X86_64_INTEGERSI_CLASS;
1922   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
1923       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
1924     return X86_64_INTEGER_CLASS;
1925
1926   /* Rule #5: If one of the classes is X87 or X87UP class, MEMORY is used.  */
1927   if (class1 == X86_64_X87_CLASS || class1 == X86_64_X87UP_CLASS
1928       || class2 == X86_64_X87_CLASS || class2 == X86_64_X87UP_CLASS)
1929     return X86_64_MEMORY_CLASS;
1930
1931   /* Rule #6: Otherwise class SSE is used.  */
1932   return X86_64_SSE_CLASS;
1933 }
1934
1935 /* Classify the argument of type TYPE and mode MODE.
1936    CLASSES will be filled by the register class used to pass each word
1937    of the operand.  The number of words is returned.  In case the parameter
1938    should be passed in memory, 0 is returned. As a special case for zero
1939    sized containers, classes[0] will be NO_CLASS and 1 is returned.
1940
1941    BIT_OFFSET is used internally for handling records and specifies offset
1942    of the offset in bits modulo 256 to avoid overflow cases.
1943
1944    See the x86-64 PS ABI for details.
1945 */
1946
1947 static int
1948 classify_argument (enum machine_mode mode, tree type,
1949                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
1950 {
1951   HOST_WIDE_INT bytes =
1952     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
1953   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1954
1955   /* Variable sized entities are always passed/returned in memory.  */
1956   if (bytes < 0)
1957     return 0;
1958
1959   if (mode != VOIDmode
1960       && MUST_PASS_IN_STACK (mode, type))
1961     return 0;
1962
1963   if (type && AGGREGATE_TYPE_P (type))
1964     {
1965       int i;
1966       tree field;
1967       enum x86_64_reg_class subclasses[MAX_CLASSES];
1968
1969       /* On x86-64 we pass structures larger than 16 bytes on the stack.  */
1970       if (bytes > 16)
1971         return 0;
1972
1973       for (i = 0; i < words; i++)
1974         classes[i] = X86_64_NO_CLASS;
1975
1976       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
1977          signalize memory class, so handle it as special case.  */
1978       if (!words)
1979         {
1980           classes[0] = X86_64_NO_CLASS;
1981           return 1;
1982         }
1983
1984       /* Classify each field of record and merge classes.  */
1985       if (TREE_CODE (type) == RECORD_TYPE)
1986         {
1987           /* For classes first merge in the field of the subclasses.  */
1988           if (TYPE_BINFO (type) != NULL && TYPE_BINFO_BASETYPES (type) != NULL)
1989             {
1990               tree bases = TYPE_BINFO_BASETYPES (type);
1991               int n_bases = TREE_VEC_LENGTH (bases);
1992               int i;
1993
1994               for (i = 0; i < n_bases; ++i)
1995                 {
1996                    tree binfo = TREE_VEC_ELT (bases, i);
1997                    int num;
1998                    int offset = tree_low_cst (BINFO_OFFSET (binfo), 0) * 8;
1999                    tree type = BINFO_TYPE (binfo);
2000
2001                    num = classify_argument (TYPE_MODE (type),
2002                                             type, subclasses,
2003                                             (offset + bit_offset) % 256);
2004                    if (!num)
2005                      return 0;
2006                    for (i = 0; i < num; i++)
2007                      {
2008                        int pos = (offset + (bit_offset % 64)) / 8 / 8;
2009                        classes[i + pos] =
2010                          merge_classes (subclasses[i], classes[i + pos]);
2011                      }
2012                 }
2013             }
2014           /* And now merge the fields of structure.  */
2015           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2016             {
2017               if (TREE_CODE (field) == FIELD_DECL)
2018                 {
2019                   int num;
2020
2021                   /* Bitfields are always classified as integer.  Handle them
2022                      early, since later code would consider them to be
2023                      misaligned integers.  */
2024                   if (DECL_BIT_FIELD (field))
2025                     {
2026                       for (i = int_bit_position (field) / 8 / 8;
2027                            i < (int_bit_position (field)
2028                                 + tree_low_cst (DECL_SIZE (field), 0)
2029                                 + 63) / 8 / 8; i++)
2030                         classes[i] =
2031                           merge_classes (X86_64_INTEGER_CLASS,
2032                                          classes[i]);
2033                     }
2034                   else
2035                     {
2036                       num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
2037                                                TREE_TYPE (field), subclasses,
2038                                                (int_bit_position (field)
2039                                                 + bit_offset) % 256);
2040                       if (!num)
2041                         return 0;
2042                       for (i = 0; i < num; i++)
2043                         {
2044                           int pos =
2045                             (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
2046                           classes[i + pos] =
2047                             merge_classes (subclasses[i], classes[i + pos]);
2048                         }
2049                     }
2050                 }
2051             }
2052         }
2053       /* Arrays are handled as small records.  */
2054       else if (TREE_CODE (type) == ARRAY_TYPE)
2055         {
2056           int num;
2057           num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
2058                                    TREE_TYPE (type), subclasses, bit_offset);
2059           if (!num)
2060             return 0;
2061
2062           /* The partial classes are now full classes.  */
2063           if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
2064             subclasses[0] = X86_64_SSE_CLASS;
2065           if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
2066             subclasses[0] = X86_64_INTEGER_CLASS;
2067
2068           for (i = 0; i < words; i++)
2069             classes[i] = subclasses[i % num];
2070         }
2071       /* Unions are similar to RECORD_TYPE but offset is always 0.  */
2072       else if (TREE_CODE (type) == UNION_TYPE
2073                || TREE_CODE (type) == QUAL_UNION_TYPE)
2074         {
2075           /* For classes first merge in the field of the subclasses.  */
2076           if (TYPE_BINFO (type) != NULL && TYPE_BINFO_BASETYPES (type) != NULL)
2077             {
2078               tree bases = TYPE_BINFO_BASETYPES (type);
2079               int n_bases = TREE_VEC_LENGTH (bases);
2080               int i;
2081
2082               for (i = 0; i < n_bases; ++i)
2083                 {
2084                    tree binfo = TREE_VEC_ELT (bases, i);
2085                    int num;
2086                    int offset = tree_low_cst (BINFO_OFFSET (binfo), 0) * 8;
2087                    tree type = BINFO_TYPE (binfo);
2088
2089                    num = classify_argument (TYPE_MODE (type),
2090                                             type, subclasses,
2091                                             (offset + (bit_offset % 64)) % 256);
2092                    if (!num)
2093                      return 0;
2094                    for (i = 0; i < num; i++)
2095                      {
2096                        int pos = (offset + (bit_offset % 64)) / 8 / 8;
2097                        classes[i + pos] =
2098                          merge_classes (subclasses[i], classes[i + pos]);
2099                      }
2100                 }
2101             }
2102           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2103             {
2104               if (TREE_CODE (field) == FIELD_DECL)
2105                 {
2106                   int num;
2107                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
2108                                            TREE_TYPE (field), subclasses,
2109                                            bit_offset);
2110                   if (!num)
2111                     return 0;
2112                   for (i = 0; i < num; i++)
2113                     classes[i] = merge_classes (subclasses[i], classes[i]);
2114                 }
2115             }
2116         }
2117       else if (TREE_CODE (type) == SET_TYPE)
2118         {
2119           if (bytes <= 4)
2120             {
2121               classes[0] = X86_64_INTEGERSI_CLASS;
2122               return 1;
2123             }
2124           else if (bytes <= 8)
2125             {
2126               classes[0] = X86_64_INTEGER_CLASS;
2127               return 1;
2128             }
2129           else if (bytes <= 12)
2130             {
2131               classes[0] = X86_64_INTEGER_CLASS;
2132               classes[1] = X86_64_INTEGERSI_CLASS;
2133               return 2;
2134             }
2135           else
2136             {
2137               classes[0] = X86_64_INTEGER_CLASS;
2138               classes[1] = X86_64_INTEGER_CLASS;
2139               return 2;
2140             }
2141         }
2142       else
2143         abort ();
2144
2145       /* Final merger cleanup.  */
2146       for (i = 0; i < words; i++)
2147         {
2148           /* If one class is MEMORY, everything should be passed in
2149              memory.  */
2150           if (classes[i] == X86_64_MEMORY_CLASS)
2151             return 0;
2152
2153           /* The X86_64_SSEUP_CLASS should be always preceded by
2154              X86_64_SSE_CLASS.  */
2155           if (classes[i] == X86_64_SSEUP_CLASS
2156               && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
2157             classes[i] = X86_64_SSE_CLASS;
2158
2159           /*  X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS.  */
2160           if (classes[i] == X86_64_X87UP_CLASS
2161               && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
2162             classes[i] = X86_64_SSE_CLASS;
2163         }
2164       return words;
2165     }
2166
2167   /* Compute alignment needed.  We align all types to natural boundaries with
2168      exception of XFmode that is aligned to 64bits.  */
2169   if (mode != VOIDmode && mode != BLKmode)
2170     {
2171       int mode_alignment = GET_MODE_BITSIZE (mode);
2172
2173       if (mode == XFmode)
2174         mode_alignment = 128;
2175       else if (mode == XCmode)
2176         mode_alignment = 256;
2177       /* Misaligned fields are always returned in memory.  */
2178       if (bit_offset % mode_alignment)
2179         return 0;
2180     }
2181
2182   /* Classification of atomic types.  */
2183   switch (mode)
2184     {
2185     case DImode:
2186     case SImode:
2187     case HImode:
2188     case QImode:
2189     case CSImode:
2190     case CHImode:
2191     case CQImode:
2192       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
2193         classes[0] = X86_64_INTEGERSI_CLASS;
2194       else
2195         classes[0] = X86_64_INTEGER_CLASS;
2196       return 1;
2197     case CDImode:
2198     case TImode:
2199       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
2200       return 2;
2201     case CTImode:
2202       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
2203       classes[2] = classes[3] = X86_64_INTEGER_CLASS;
2204       return 4;
2205     case SFmode:
2206       if (!(bit_offset % 64))
2207         classes[0] = X86_64_SSESF_CLASS;
2208       else
2209         classes[0] = X86_64_SSE_CLASS;
2210       return 1;
2211     case DFmode:
2212       classes[0] = X86_64_SSEDF_CLASS;
2213       return 1;
2214     case XFmode:
2215       classes[0] = X86_64_X87_CLASS;
2216       classes[1] = X86_64_X87UP_CLASS;
2217       return 2;
2218     case TFmode:
2219     case TCmode:
2220       return 0;
2221     case XCmode:
2222       classes[0] = X86_64_X87_CLASS;
2223       classes[1] = X86_64_X87UP_CLASS;
2224       classes[2] = X86_64_X87_CLASS;
2225       classes[3] = X86_64_X87UP_CLASS;
2226       return 4;
2227     case DCmode:
2228       classes[0] = X86_64_SSEDF_CLASS;
2229       classes[1] = X86_64_SSEDF_CLASS;
2230       return 2;
2231     case SCmode:
2232       classes[0] = X86_64_SSE_CLASS;
2233       return 1;
2234     case V4SFmode:
2235     case V4SImode:
2236     case V16QImode:
2237     case V8HImode:
2238     case V2DFmode:
2239     case V2DImode:
2240       classes[0] = X86_64_SSE_CLASS;
2241       classes[1] = X86_64_SSEUP_CLASS;
2242       return 2;
2243     case V2SFmode:
2244     case V2SImode:
2245     case V4HImode:
2246     case V8QImode:
2247       return 0;
2248     case BLKmode:
2249     case VOIDmode:
2250       return 0;
2251     default:
2252       abort ();
2253     }
2254 }
2255
2256 /* Examine the argument and return set number of register required in each
2257    class.  Return 0 iff parameter should be passed in memory.  */
2258 static int
2259 examine_argument (enum machine_mode mode, tree type, int in_return,
2260                   int *int_nregs, int *sse_nregs)
2261 {
2262   enum x86_64_reg_class class[MAX_CLASSES];
2263   int n = classify_argument (mode, type, class, 0);
2264
2265   *int_nregs = 0;
2266   *sse_nregs = 0;
2267   if (!n)
2268     return 0;
2269   for (n--; n >= 0; n--)
2270     switch (class[n])
2271       {
2272       case X86_64_INTEGER_CLASS:
2273       case X86_64_INTEGERSI_CLASS:
2274         (*int_nregs)++;
2275         break;
2276       case X86_64_SSE_CLASS:
2277       case X86_64_SSESF_CLASS:
2278       case X86_64_SSEDF_CLASS:
2279         (*sse_nregs)++;
2280         break;
2281       case X86_64_NO_CLASS:
2282       case X86_64_SSEUP_CLASS:
2283         break;
2284       case X86_64_X87_CLASS:
2285       case X86_64_X87UP_CLASS:
2286         if (!in_return)
2287           return 0;
2288         break;
2289       case X86_64_MEMORY_CLASS:
2290         abort ();
2291       }
2292   return 1;
2293 }
2294 /* Construct container for the argument used by GCC interface.  See
2295    FUNCTION_ARG for the detailed description.  */
2296 static rtx
2297 construct_container (enum machine_mode mode, tree type, int in_return,
2298                      int nintregs, int nsseregs, const int * intreg,
2299                      int sse_regno)
2300 {
2301   enum machine_mode tmpmode;
2302   int bytes =
2303     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2304   enum x86_64_reg_class class[MAX_CLASSES];
2305   int n;
2306   int i;
2307   int nexps = 0;
2308   int needed_sseregs, needed_intregs;
2309   rtx exp[MAX_CLASSES];
2310   rtx ret;
2311
2312   n = classify_argument (mode, type, class, 0);
2313   if (TARGET_DEBUG_ARG)
2314     {
2315       if (!n)
2316         fprintf (stderr, "Memory class\n");
2317       else
2318         {
2319           fprintf (stderr, "Classes:");
2320           for (i = 0; i < n; i++)
2321             {
2322               fprintf (stderr, " %s", x86_64_reg_class_name[class[i]]);
2323             }
2324            fprintf (stderr, "\n");
2325         }
2326     }
2327   if (!n)
2328     return NULL;
2329   if (!examine_argument (mode, type, in_return, &needed_intregs, &needed_sseregs))
2330     return NULL;
2331   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
2332     return NULL;
2333
2334   /* First construct simple cases.  Avoid SCmode, since we want to use
2335      single register to pass this type.  */
2336   if (n == 1 && mode != SCmode)
2337     switch (class[0])
2338       {
2339       case X86_64_INTEGER_CLASS:
2340       case X86_64_INTEGERSI_CLASS:
2341         return gen_rtx_REG (mode, intreg[0]);
2342       case X86_64_SSE_CLASS:
2343       case X86_64_SSESF_CLASS:
2344       case X86_64_SSEDF_CLASS:
2345         return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
2346       case X86_64_X87_CLASS:
2347         return gen_rtx_REG (mode, FIRST_STACK_REG);
2348       case X86_64_NO_CLASS:
2349         /* Zero sized array, struct or class.  */
2350         return NULL;
2351       default:
2352         abort ();
2353       }
2354   if (n == 2 && class[0] == X86_64_SSE_CLASS && class[1] == X86_64_SSEUP_CLASS)
2355     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
2356   if (n == 2
2357       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS)
2358     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
2359   if (n == 2 && class[0] == X86_64_INTEGER_CLASS
2360       && class[1] == X86_64_INTEGER_CLASS
2361       && (mode == CDImode || mode == TImode || mode == TFmode)
2362       && intreg[0] + 1 == intreg[1])
2363     return gen_rtx_REG (mode, intreg[0]);
2364   if (n == 4
2365       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS
2366       && class[2] == X86_64_X87_CLASS && class[3] == X86_64_X87UP_CLASS)
2367     return gen_rtx_REG (XCmode, FIRST_STACK_REG);
2368
2369   /* Otherwise figure out the entries of the PARALLEL.  */
2370   for (i = 0; i < n; i++)
2371     {
2372       switch (class[i])
2373         {
2374           case X86_64_NO_CLASS:
2375             break;
2376           case X86_64_INTEGER_CLASS:
2377           case X86_64_INTEGERSI_CLASS:
2378             /* Merge TImodes on aligned occasions here too.  */
2379             if (i * 8 + 8 > bytes)
2380               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
2381             else if (class[i] == X86_64_INTEGERSI_CLASS)
2382               tmpmode = SImode;
2383             else
2384               tmpmode = DImode;
2385             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
2386             if (tmpmode == BLKmode)
2387               tmpmode = DImode;
2388             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2389                                                gen_rtx_REG (tmpmode, *intreg),
2390                                                GEN_INT (i*8));
2391             intreg++;
2392             break;
2393           case X86_64_SSESF_CLASS:
2394             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2395                                                gen_rtx_REG (SFmode,
2396                                                             SSE_REGNO (sse_regno)),
2397                                                GEN_INT (i*8));
2398             sse_regno++;
2399             break;
2400           case X86_64_SSEDF_CLASS:
2401             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2402                                                gen_rtx_REG (DFmode,
2403                                                             SSE_REGNO (sse_regno)),
2404                                                GEN_INT (i*8));
2405             sse_regno++;
2406             break;
2407           case X86_64_SSE_CLASS:
2408             if (i < n - 1 && class[i + 1] == X86_64_SSEUP_CLASS)
2409               tmpmode = TImode;
2410             else
2411               tmpmode = DImode;
2412             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2413                                                gen_rtx_REG (tmpmode,
2414                                                             SSE_REGNO (sse_regno)),
2415                                                GEN_INT (i*8));
2416             if (tmpmode == TImode)
2417               i++;
2418             sse_regno++;
2419             break;
2420           default:
2421             abort ();
2422         }
2423     }
2424   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
2425   for (i = 0; i < nexps; i++)
2426     XVECEXP (ret, 0, i) = exp [i];
2427   return ret;
2428 }
2429
2430 /* Update the data in CUM to advance over an argument
2431    of mode MODE and data type TYPE.
2432    (TYPE is null for libcalls where that information may not be available.)  */
2433
2434 void
2435 function_arg_advance (CUMULATIVE_ARGS *cum,     /* current arg information */
2436                       enum machine_mode mode,   /* current arg mode */
2437                       tree type,        /* type of the argument or 0 if lib support */
2438                       int named)        /* whether or not the argument was named */
2439 {
2440   int bytes =
2441     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2442   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2443
2444   if (TARGET_DEBUG_ARG)
2445     fprintf (stderr,
2446              "function_adv (sz=%d, wds=%2d, nregs=%d, ssenregs=%d, mode=%s, named=%d)\n\n",
2447              words, cum->words, cum->nregs, cum->sse_nregs, GET_MODE_NAME (mode), named);
2448   if (TARGET_64BIT)
2449     {
2450       int int_nregs, sse_nregs;
2451       if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
2452         cum->words += words;
2453       else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
2454         {
2455           cum->nregs -= int_nregs;
2456           cum->sse_nregs -= sse_nregs;
2457           cum->regno += int_nregs;
2458           cum->sse_regno += sse_nregs;
2459         }
2460       else
2461         cum->words += words;
2462     }
2463   else
2464     {
2465       if (TARGET_SSE && SSE_REG_MODE_P (mode)
2466           && (!type || !AGGREGATE_TYPE_P (type)))
2467         {
2468           cum->sse_words += words;
2469           cum->sse_nregs -= 1;
2470           cum->sse_regno += 1;
2471           if (cum->sse_nregs <= 0)
2472             {
2473               cum->sse_nregs = 0;
2474               cum->sse_regno = 0;
2475             }
2476         }
2477       else if (TARGET_MMX && MMX_REG_MODE_P (mode)
2478                && (!type || !AGGREGATE_TYPE_P (type)))
2479         {
2480           cum->mmx_words += words;
2481           cum->mmx_nregs -= 1;
2482           cum->mmx_regno += 1;
2483           if (cum->mmx_nregs <= 0)
2484             {
2485               cum->mmx_nregs = 0;
2486               cum->mmx_regno = 0;
2487             }
2488         }
2489       else
2490         {
2491           cum->words += words;
2492           cum->nregs -= words;
2493           cum->regno += words;
2494
2495           if (cum->nregs <= 0)
2496             {
2497               cum->nregs = 0;
2498               cum->regno = 0;
2499             }
2500         }
2501     }
2502   return;
2503 }
2504
2505 /* Define where to put the arguments to a function.
2506    Value is zero to push the argument on the stack,
2507    or a hard register in which to store the argument.
2508
2509    MODE is the argument's machine mode.
2510    TYPE is the data type of the argument (as a tree).
2511     This is null for libcalls where that information may
2512     not be available.
2513    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2514     the preceding args and about the function being called.
2515    NAMED is nonzero if this argument is a named parameter
2516     (otherwise it is an extra parameter matching an ellipsis).  */
2517
2518 rtx
2519 function_arg (CUMULATIVE_ARGS *cum,     /* current arg information */
2520               enum machine_mode mode,   /* current arg mode */
2521               tree type,        /* type of the argument or 0 if lib support */
2522               int named)        /* != 0 for normal args, == 0 for ...  args */
2523 {
2524   rtx ret   = NULL_RTX;
2525   int bytes =
2526     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2527   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2528   static bool warnedsse, warnedmmx;
2529
2530   /* Handle a hidden AL argument containing number of registers for varargs
2531      x86-64 functions.  For i386 ABI just return constm1_rtx to avoid
2532      any AL settings.  */
2533   if (mode == VOIDmode)
2534     {
2535       if (TARGET_64BIT)
2536         return GEN_INT (cum->maybe_vaarg
2537                         ? (cum->sse_nregs < 0
2538                            ? SSE_REGPARM_MAX
2539                            : cum->sse_regno)
2540                         : -1);
2541       else
2542         return constm1_rtx;
2543     }
2544   if (TARGET_64BIT)
2545     ret = construct_container (mode, type, 0, cum->nregs, cum->sse_nregs,
2546                                &x86_64_int_parameter_registers [cum->regno],
2547                                cum->sse_regno);
2548   else
2549     switch (mode)
2550       {
2551         /* For now, pass fp/complex values on the stack.  */
2552       default:
2553         break;
2554
2555       case BLKmode:
2556         if (bytes < 0)
2557           break;
2558         /* FALLTHRU */
2559       case DImode:
2560       case SImode:
2561       case HImode:
2562       case QImode:
2563         if (words <= cum->nregs)
2564           {
2565             int regno = cum->regno;
2566
2567             /* Fastcall allocates the first two DWORD (SImode) or
2568                smaller arguments to ECX and EDX.  */
2569             if (cum->fastcall)
2570               {
2571                 if (mode == BLKmode || mode == DImode)
2572                   break;
2573
2574                 /* ECX not EAX is the first allocated register.  */
2575                 if (regno == 0)
2576                   regno = 2;
2577               }
2578             ret = gen_rtx_REG (mode, regno);
2579           }
2580         break;
2581       case TImode:
2582       case V16QImode:
2583       case V8HImode:
2584       case V4SImode:
2585       case V2DImode:
2586       case V4SFmode:
2587       case V2DFmode:
2588         if (!type || !AGGREGATE_TYPE_P (type))
2589           {
2590             if (!TARGET_SSE && !warnedmmx && cum->warn_sse)
2591               {
2592                 warnedsse = true;
2593                 warning ("SSE vector argument without SSE enabled "
2594                          "changes the ABI");
2595               }
2596             if (cum->sse_nregs)
2597               ret = gen_rtx_REG (mode, cum->sse_regno + FIRST_SSE_REG);
2598           }
2599         break;
2600       case V8QImode:
2601       case V4HImode:
2602       case V2SImode:
2603       case V2SFmode:
2604         if (!type || !AGGREGATE_TYPE_P (type))
2605           {
2606             if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
2607               {
2608                 warnedmmx = true;
2609                 warning ("MMX vector argument without MMX enabled "
2610                          "changes the ABI");
2611               }
2612             if (cum->mmx_nregs)
2613               ret = gen_rtx_REG (mode, cum->mmx_regno + FIRST_MMX_REG);
2614           }
2615         break;
2616       }
2617
2618   if (TARGET_DEBUG_ARG)
2619     {
2620       fprintf (stderr,
2621                "function_arg (size=%d, wds=%2d, nregs=%d, mode=%4s, named=%d, ",
2622                words, cum->words, cum->nregs, GET_MODE_NAME (mode), named);
2623
2624       if (ret)
2625         print_simple_rtl (stderr, ret);
2626       else
2627         fprintf (stderr, ", stack");
2628
2629       fprintf (stderr, " )\n");
2630     }
2631
2632   return ret;
2633 }
2634
2635 /* A C expression that indicates when an argument must be passed by
2636    reference.  If nonzero for an argument, a copy of that argument is
2637    made in memory and a pointer to the argument is passed instead of
2638    the argument itself.  The pointer is passed in whatever way is
2639    appropriate for passing a pointer to that type.  */
2640
2641 int
2642 function_arg_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2643                                 enum machine_mode mode ATTRIBUTE_UNUSED,
2644                                 tree type, int named ATTRIBUTE_UNUSED)
2645 {
2646   if (!TARGET_64BIT)
2647     return 0;
2648
2649   if (type && int_size_in_bytes (type) == -1)
2650     {
2651       if (TARGET_DEBUG_ARG)
2652         fprintf (stderr, "function_arg_pass_by_reference\n");
2653       return 1;
2654     }
2655
2656   return 0;
2657 }
2658
2659 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
2660    ABI  */
2661 static bool
2662 contains_128bit_aligned_vector_p (tree type)
2663 {
2664   enum machine_mode mode = TYPE_MODE (type);
2665   if (SSE_REG_MODE_P (mode)
2666       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
2667     return true;
2668   if (TYPE_ALIGN (type) < 128)
2669     return false;
2670
2671   if (AGGREGATE_TYPE_P (type))
2672     {
2673       /* Walk the aggregates recursively.  */
2674       if (TREE_CODE (type) == RECORD_TYPE
2675           || TREE_CODE (type) == UNION_TYPE
2676           || TREE_CODE (type) == QUAL_UNION_TYPE)
2677         {
2678           tree field;
2679
2680           if (TYPE_BINFO (type) != NULL
2681               && TYPE_BINFO_BASETYPES (type) != NULL)
2682             {
2683               tree bases = TYPE_BINFO_BASETYPES (type);
2684               int n_bases = TREE_VEC_LENGTH (bases);
2685               int i;
2686
2687               for (i = 0; i < n_bases; ++i)
2688                 {
2689                   tree binfo = TREE_VEC_ELT (bases, i);
2690                   tree type = BINFO_TYPE (binfo);
2691
2692                   if (contains_128bit_aligned_vector_p (type))
2693                     return true;
2694                 }
2695             }
2696           /* And now merge the fields of structure.  */
2697           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2698             {
2699               if (TREE_CODE (field) == FIELD_DECL
2700                   && contains_128bit_aligned_vector_p (TREE_TYPE (field)))
2701                 return true;
2702             }
2703         }
2704       /* Just for use if some languages passes arrays by value.  */
2705       else if (TREE_CODE (type) == ARRAY_TYPE)
2706         {
2707           if (contains_128bit_aligned_vector_p (TREE_TYPE (type)))
2708             return true;
2709         }
2710       else
2711         abort ();
2712     }
2713   return false;
2714 }
2715
2716 /* Gives the alignment boundary, in bits, of an argument with the
2717    specified mode and type.  */
2718
2719 int
2720 ix86_function_arg_boundary (enum machine_mode mode, tree type)
2721 {
2722   int align;
2723   if (type)
2724     align = TYPE_ALIGN (type);
2725   else
2726     align = GET_MODE_ALIGNMENT (mode);
2727   if (align < PARM_BOUNDARY)
2728     align = PARM_BOUNDARY;
2729   if (!TARGET_64BIT)
2730     {
2731       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
2732          make an exception for SSE modes since these require 128bit
2733          alignment.
2734
2735          The handling here differs from field_alignment.  ICC aligns MMX
2736          arguments to 4 byte boundaries, while structure fields are aligned
2737          to 8 byte boundaries.  */
2738       if (!type)
2739         {
2740           if (!SSE_REG_MODE_P (mode))
2741             align = PARM_BOUNDARY;
2742         }
2743       else
2744         {
2745           if (!contains_128bit_aligned_vector_p (type))
2746             align = PARM_BOUNDARY;
2747         }
2748     }
2749   if (align > 128)
2750     align = 128;
2751   return align;
2752 }
2753
2754 /* Return true if N is a possible register number of function value.  */
2755 bool
2756 ix86_function_value_regno_p (int regno)
2757 {
2758   if (!TARGET_64BIT)
2759     {
2760       return ((regno) == 0
2761               || ((regno) == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387)
2762               || ((regno) == FIRST_SSE_REG && TARGET_SSE));
2763     }
2764   return ((regno) == 0 || (regno) == FIRST_FLOAT_REG
2765           || ((regno) == FIRST_SSE_REG && TARGET_SSE)
2766           || ((regno) == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387));
2767 }
2768
2769 /* Define how to find the value returned by a function.
2770    VALTYPE is the data type of the value (as a tree).
2771    If the precise function being called is known, FUNC is its FUNCTION_DECL;
2772    otherwise, FUNC is 0.  */
2773 rtx
2774 ix86_function_value (tree valtype)
2775 {
2776   if (TARGET_64BIT)
2777     {
2778       rtx ret = construct_container (TYPE_MODE (valtype), valtype, 1,
2779                                      REGPARM_MAX, SSE_REGPARM_MAX,
2780                                      x86_64_int_return_registers, 0);
2781       /* For zero sized structures, construct_container return NULL, but we need
2782          to keep rest of compiler happy by returning meaningful value.  */
2783       if (!ret)
2784         ret = gen_rtx_REG (TYPE_MODE (valtype), 0);
2785       return ret;
2786     }
2787   else
2788     return gen_rtx_REG (TYPE_MODE (valtype),
2789                         ix86_value_regno (TYPE_MODE (valtype)));
2790 }
2791
2792 /* Return false iff type is returned in memory.  */
2793 int
2794 ix86_return_in_memory (tree type)
2795 {
2796   int needed_intregs, needed_sseregs, size;
2797   enum machine_mode mode = TYPE_MODE (type);
2798
2799   if (TARGET_64BIT)
2800     return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
2801
2802   if (mode == BLKmode)
2803     return 1;
2804
2805   size = int_size_in_bytes (type);
2806
2807   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
2808     return 0;
2809
2810   if (VECTOR_MODE_P (mode) || mode == TImode)
2811     {
2812       /* User-created vectors small enough to fit in EAX.  */
2813       if (size < 8)
2814         return 0;
2815
2816       /* MMX/3dNow values are returned on the stack, since we've
2817          got to EMMS/FEMMS before returning.  */
2818       if (size == 8)
2819         return 1;
2820
2821       /* SSE values are returned in XMM0.  */
2822       /* ??? Except when it doesn't exist?  We have a choice of
2823          either (1) being abi incompatible with a -march switch,
2824          or (2) generating an error here.  Given no good solution,
2825          I think the safest thing is one warning.  The user won't
2826          be able to use -Werror, but....  */
2827       if (size == 16)
2828         {
2829           static bool warned;
2830
2831           if (TARGET_SSE)
2832             return 0;
2833
2834           if (!warned)
2835             {
2836               warned = true;
2837               warning ("SSE vector return without SSE enabled "
2838                        "changes the ABI");
2839             }
2840           return 1;
2841         }
2842     }
2843
2844   if (mode == XFmode)
2845     return 0;
2846
2847   if (size > 12)
2848     return 1;
2849   return 0;
2850 }
2851
2852 /* Define how to find the value returned by a library function
2853    assuming the value has mode MODE.  */
2854 rtx
2855 ix86_libcall_value (enum machine_mode mode)
2856 {
2857   if (TARGET_64BIT)
2858     {
2859       switch (mode)
2860         {
2861         case SFmode:
2862         case SCmode:
2863         case DFmode:
2864         case DCmode:
2865           return gen_rtx_REG (mode, FIRST_SSE_REG);
2866         case XFmode:
2867         case XCmode:
2868           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
2869         case TFmode:
2870         case TCmode:
2871           return NULL;
2872         default:
2873           return gen_rtx_REG (mode, 0);
2874         }
2875     }
2876   else
2877     return gen_rtx_REG (mode, ix86_value_regno (mode));
2878 }
2879
2880 /* Given a mode, return the register to use for a return value.  */
2881
2882 static int
2883 ix86_value_regno (enum machine_mode mode)
2884 {
2885   /* Floating point return values in %st(0).  */
2886   if (GET_MODE_CLASS (mode) == MODE_FLOAT && TARGET_FLOAT_RETURNS_IN_80387)
2887     return FIRST_FLOAT_REG;
2888   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
2889      we prevent this case when sse is not available.  */
2890   if (mode == TImode || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
2891     return FIRST_SSE_REG;
2892   /* Everything else in %eax.  */
2893   return 0;
2894 }
2895 \f
2896 /* Create the va_list data type.  */
2897
2898 static tree
2899 ix86_build_builtin_va_list (void)
2900 {
2901   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
2902
2903   /* For i386 we use plain pointer to argument area.  */
2904   if (!TARGET_64BIT)
2905     return build_pointer_type (char_type_node);
2906
2907   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
2908   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
2909
2910   f_gpr = build_decl (FIELD_DECL, get_identifier ("gp_offset"),
2911                       unsigned_type_node);
2912   f_fpr = build_decl (FIELD_DECL, get_identifier ("fp_offset"),
2913                       unsigned_type_node);
2914   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
2915                       ptr_type_node);
2916   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
2917                       ptr_type_node);
2918
2919   DECL_FIELD_CONTEXT (f_gpr) = record;
2920   DECL_FIELD_CONTEXT (f_fpr) = record;
2921   DECL_FIELD_CONTEXT (f_ovf) = record;
2922   DECL_FIELD_CONTEXT (f_sav) = record;
2923
2924   TREE_CHAIN (record) = type_decl;
2925   TYPE_NAME (record) = type_decl;
2926   TYPE_FIELDS (record) = f_gpr;
2927   TREE_CHAIN (f_gpr) = f_fpr;
2928   TREE_CHAIN (f_fpr) = f_ovf;
2929   TREE_CHAIN (f_ovf) = f_sav;
2930
2931   layout_type (record);
2932
2933   /* The correct type is an array type of one element.  */
2934   return build_array_type (record, build_index_type (size_zero_node));
2935 }
2936
2937 /* Perform any needed actions needed for a function that is receiving a
2938    variable number of arguments.
2939
2940    CUM is as above.
2941
2942    MODE and TYPE are the mode and type of the current parameter.
2943
2944    PRETEND_SIZE is a variable that should be set to the amount of stack
2945    that must be pushed by the prolog to pretend that our caller pushed
2946    it.
2947
2948    Normally, this macro will push all remaining incoming registers on the
2949    stack and set PRETEND_SIZE to the length of the registers pushed.  */
2950
2951 void
2952 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
2953                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
2954                              int no_rtl)
2955 {
2956   CUMULATIVE_ARGS next_cum;
2957   rtx save_area = NULL_RTX, mem;
2958   rtx label;
2959   rtx label_ref;
2960   rtx tmp_reg;
2961   rtx nsse_reg;
2962   int set;
2963   tree fntype;
2964   int stdarg_p;
2965   int i;
2966
2967   if (!TARGET_64BIT)
2968     return;
2969
2970   /* Indicate to allocate space on the stack for varargs save area.  */
2971   ix86_save_varrargs_registers = 1;
2972
2973   cfun->stack_alignment_needed = 128;
2974
2975   fntype = TREE_TYPE (current_function_decl);
2976   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
2977               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
2978                   != void_type_node));
2979
2980   /* For varargs, we do not want to skip the dummy va_dcl argument.
2981      For stdargs, we do want to skip the last named argument.  */
2982   next_cum = *cum;
2983   if (stdarg_p)
2984     function_arg_advance (&next_cum, mode, type, 1);
2985
2986   if (!no_rtl)
2987     save_area = frame_pointer_rtx;
2988
2989   set = get_varargs_alias_set ();
2990
2991   for (i = next_cum.regno; i < ix86_regparm; i++)
2992     {
2993       mem = gen_rtx_MEM (Pmode,
2994                          plus_constant (save_area, i * UNITS_PER_WORD));
2995       set_mem_alias_set (mem, set);
2996       emit_move_insn (mem, gen_rtx_REG (Pmode,
2997                                         x86_64_int_parameter_registers[i]));
2998     }
2999
3000   if (next_cum.sse_nregs)
3001     {
3002       /* Now emit code to save SSE registers.  The AX parameter contains number
3003          of SSE parameter registers used to call this function.  We use
3004          sse_prologue_save insn template that produces computed jump across
3005          SSE saves.  We need some preparation work to get this working.  */
3006
3007       label = gen_label_rtx ();
3008       label_ref = gen_rtx_LABEL_REF (Pmode, label);
3009
3010       /* Compute address to jump to :
3011          label - 5*eax + nnamed_sse_arguments*5  */
3012       tmp_reg = gen_reg_rtx (Pmode);
3013       nsse_reg = gen_reg_rtx (Pmode);
3014       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, 0)));
3015       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
3016                               gen_rtx_MULT (Pmode, nsse_reg,
3017                                             GEN_INT (4))));
3018       if (next_cum.sse_regno)
3019         emit_move_insn
3020           (nsse_reg,
3021            gen_rtx_CONST (DImode,
3022                           gen_rtx_PLUS (DImode,
3023                                         label_ref,
3024                                         GEN_INT (next_cum.sse_regno * 4))));
3025       else
3026         emit_move_insn (nsse_reg, label_ref);
3027       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
3028
3029       /* Compute address of memory block we save into.  We always use pointer
3030          pointing 127 bytes after first byte to store - this is needed to keep
3031          instruction size limited by 4 bytes.  */
3032       tmp_reg = gen_reg_rtx (Pmode);
3033       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
3034                               plus_constant (save_area,
3035                                              8 * REGPARM_MAX + 127)));
3036       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
3037       set_mem_alias_set (mem, set);
3038       set_mem_align (mem, BITS_PER_WORD);
3039
3040       /* And finally do the dirty job!  */
3041       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
3042                                         GEN_INT (next_cum.sse_regno), label));
3043     }
3044
3045 }
3046
3047 /* Implement va_start.  */
3048
3049 void
3050 ix86_va_start (tree valist, rtx nextarg)
3051 {
3052   HOST_WIDE_INT words, n_gpr, n_fpr;
3053   tree f_gpr, f_fpr, f_ovf, f_sav;
3054   tree gpr, fpr, ovf, sav, t;
3055
3056   /* Only 64bit target needs something special.  */
3057   if (!TARGET_64BIT)
3058     {
3059       std_expand_builtin_va_start (valist, nextarg);
3060       return;
3061     }
3062
3063   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3064   f_fpr = TREE_CHAIN (f_gpr);
3065   f_ovf = TREE_CHAIN (f_fpr);
3066   f_sav = TREE_CHAIN (f_ovf);
3067
3068   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
3069   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
3070   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
3071   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
3072   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
3073
3074   /* Count number of gp and fp argument registers used.  */
3075   words = current_function_args_info.words;
3076   n_gpr = current_function_args_info.regno;
3077   n_fpr = current_function_args_info.sse_regno;
3078
3079   if (TARGET_DEBUG_ARG)
3080     fprintf (stderr, "va_start: words = %d, n_gpr = %d, n_fpr = %d\n",
3081              (int) words, (int) n_gpr, (int) n_fpr);
3082
3083   t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
3084              build_int_2 (n_gpr * 8, 0));
3085   TREE_SIDE_EFFECTS (t) = 1;
3086   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3087
3088   t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
3089              build_int_2 (n_fpr * 16 + 8*REGPARM_MAX, 0));
3090   TREE_SIDE_EFFECTS (t) = 1;
3091   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3092
3093   /* Find the overflow area.  */
3094   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
3095   if (words != 0)
3096     t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
3097                build_int_2 (words * UNITS_PER_WORD, 0));
3098   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3099   TREE_SIDE_EFFECTS (t) = 1;
3100   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3101
3102   /* Find the register save area.
3103      Prologue of the function save it right above stack frame.  */
3104   t = make_tree (TREE_TYPE (sav), frame_pointer_rtx);
3105   t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
3106   TREE_SIDE_EFFECTS (t) = 1;
3107   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3108 }
3109
3110 /* Implement va_arg.  */
3111 rtx
3112 ix86_va_arg (tree valist, tree type)
3113 {
3114   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
3115   tree f_gpr, f_fpr, f_ovf, f_sav;
3116   tree gpr, fpr, ovf, sav, t;
3117   int size, rsize;
3118   rtx lab_false, lab_over = NULL_RTX;
3119   rtx addr_rtx, r;
3120   rtx container;
3121   int indirect_p = 0;
3122
3123   /* Only 64bit target needs something special.  */
3124   if (!TARGET_64BIT)
3125     {
3126       return std_expand_builtin_va_arg (valist, type);
3127     }
3128
3129   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3130   f_fpr = TREE_CHAIN (f_gpr);
3131   f_ovf = TREE_CHAIN (f_fpr);
3132   f_sav = TREE_CHAIN (f_ovf);
3133
3134   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
3135   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
3136   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
3137   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
3138   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
3139
3140   size = int_size_in_bytes (type);
3141   if (size == -1)
3142     {
3143       /* Passed by reference.  */
3144       indirect_p = 1;
3145       type = build_pointer_type (type);
3146       size = int_size_in_bytes (type);
3147     }
3148   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3149
3150   container = construct_container (TYPE_MODE (type), type, 0,
3151                                    REGPARM_MAX, SSE_REGPARM_MAX, intreg, 0);
3152   /*
3153    * Pull the value out of the saved registers ...
3154    */
3155
3156   addr_rtx = gen_reg_rtx (Pmode);
3157
3158   if (container)
3159     {
3160       rtx int_addr_rtx, sse_addr_rtx;
3161       int needed_intregs, needed_sseregs;
3162       int need_temp;
3163
3164       lab_over = gen_label_rtx ();
3165       lab_false = gen_label_rtx ();
3166
3167       examine_argument (TYPE_MODE (type), type, 0,
3168                         &needed_intregs, &needed_sseregs);
3169
3170
3171       need_temp = ((needed_intregs && TYPE_ALIGN (type) > 64)
3172                    || TYPE_ALIGN (type) > 128);
3173
3174       /* In case we are passing structure, verify that it is consecutive block
3175          on the register save area.  If not we need to do moves.  */
3176       if (!need_temp && !REG_P (container))
3177         {
3178           /* Verify that all registers are strictly consecutive  */
3179           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
3180             {
3181               int i;
3182
3183               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
3184                 {
3185                   rtx slot = XVECEXP (container, 0, i);
3186                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
3187                       || INTVAL (XEXP (slot, 1)) != i * 16)
3188                     need_temp = 1;
3189                 }
3190             }
3191           else
3192             {
3193               int i;
3194
3195               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
3196                 {
3197                   rtx slot = XVECEXP (container, 0, i);
3198                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
3199                       || INTVAL (XEXP (slot, 1)) != i * 8)
3200                     need_temp = 1;
3201                 }
3202             }
3203         }
3204       if (!need_temp)
3205         {
3206           int_addr_rtx = addr_rtx;
3207           sse_addr_rtx = addr_rtx;
3208         }
3209       else
3210         {
3211           int_addr_rtx = gen_reg_rtx (Pmode);
3212           sse_addr_rtx = gen_reg_rtx (Pmode);
3213         }
3214       /* First ensure that we fit completely in registers.  */
3215       if (needed_intregs)
3216         {
3217           emit_cmp_and_jump_insns (expand_expr
3218                                    (gpr, NULL_RTX, SImode, EXPAND_NORMAL),
3219                                    GEN_INT ((REGPARM_MAX - needed_intregs +
3220                                              1) * 8), GE, const1_rtx, SImode,
3221                                    1, lab_false);
3222         }
3223       if (needed_sseregs)
3224         {
3225           emit_cmp_and_jump_insns (expand_expr
3226                                    (fpr, NULL_RTX, SImode, EXPAND_NORMAL),
3227                                    GEN_INT ((SSE_REGPARM_MAX -
3228                                              needed_sseregs + 1) * 16 +
3229                                             REGPARM_MAX * 8), GE, const1_rtx,
3230                                    SImode, 1, lab_false);
3231         }
3232
3233       /* Compute index to start of area used for integer regs.  */
3234       if (needed_intregs)
3235         {
3236           t = build (PLUS_EXPR, ptr_type_node, sav, gpr);
3237           r = expand_expr (t, int_addr_rtx, Pmode, EXPAND_NORMAL);
3238           if (r != int_addr_rtx)
3239             emit_move_insn (int_addr_rtx, r);
3240         }
3241       if (needed_sseregs)
3242         {
3243           t = build (PLUS_EXPR, ptr_type_node, sav, fpr);
3244           r = expand_expr (t, sse_addr_rtx, Pmode, EXPAND_NORMAL);
3245           if (r != sse_addr_rtx)
3246             emit_move_insn (sse_addr_rtx, r);
3247         }
3248       if (need_temp)
3249         {
3250           int i;
3251           rtx mem;
3252           rtx x;
3253
3254           /* Never use the memory itself, as it has the alias set.  */
3255           x = XEXP (assign_temp (type, 0, 1, 0), 0);
3256           mem = gen_rtx_MEM (BLKmode, x);
3257           force_operand (x, addr_rtx);
3258           set_mem_alias_set (mem, get_varargs_alias_set ());
3259           set_mem_align (mem, BITS_PER_UNIT);
3260
3261           for (i = 0; i < XVECLEN (container, 0); i++)
3262             {
3263               rtx slot = XVECEXP (container, 0, i);
3264               rtx reg = XEXP (slot, 0);
3265               enum machine_mode mode = GET_MODE (reg);
3266               rtx src_addr;
3267               rtx src_mem;
3268               int src_offset;
3269               rtx dest_mem;
3270
3271               if (SSE_REGNO_P (REGNO (reg)))
3272                 {
3273                   src_addr = sse_addr_rtx;
3274                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
3275                 }
3276               else
3277                 {
3278                   src_addr = int_addr_rtx;
3279                   src_offset = REGNO (reg) * 8;
3280                 }
3281               src_mem = gen_rtx_MEM (mode, src_addr);
3282               set_mem_alias_set (src_mem, get_varargs_alias_set ());
3283               src_mem = adjust_address (src_mem, mode, src_offset);
3284               dest_mem = adjust_address (mem, mode, INTVAL (XEXP (slot, 1)));
3285               emit_move_insn (dest_mem, src_mem);
3286             }
3287         }
3288
3289       if (needed_intregs)
3290         {
3291           t =
3292             build (PLUS_EXPR, TREE_TYPE (gpr), gpr,
3293                    build_int_2 (needed_intregs * 8, 0));
3294           t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
3295           TREE_SIDE_EFFECTS (t) = 1;
3296           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3297         }
3298       if (needed_sseregs)
3299         {
3300           t =
3301             build (PLUS_EXPR, TREE_TYPE (fpr), fpr,
3302                    build_int_2 (needed_sseregs * 16, 0));
3303           t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr, t);
3304           TREE_SIDE_EFFECTS (t) = 1;
3305           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3306         }
3307
3308       emit_jump_insn (gen_jump (lab_over));
3309       emit_barrier ();
3310       emit_label (lab_false);
3311     }
3312
3313   /* ... otherwise out of the overflow area.  */
3314
3315   /* Care for on-stack alignment if needed.  */
3316   if (FUNCTION_ARG_BOUNDARY (VOIDmode, type) <= 64)
3317     t = ovf;
3318   else
3319     {
3320       HOST_WIDE_INT align = FUNCTION_ARG_BOUNDARY (VOIDmode, type) / 8;
3321       t = build (PLUS_EXPR, TREE_TYPE (ovf), ovf, build_int_2 (align - 1, 0));
3322       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align, -1));
3323     }
3324   t = save_expr (t);
3325
3326   r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
3327   if (r != addr_rtx)
3328     emit_move_insn (addr_rtx, r);
3329
3330   t =
3331     build (PLUS_EXPR, TREE_TYPE (t), t,
3332            build_int_2 (rsize * UNITS_PER_WORD, 0));
3333   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3334   TREE_SIDE_EFFECTS (t) = 1;
3335   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3336
3337   if (container)
3338     emit_label (lab_over);
3339
3340   if (indirect_p)
3341     {
3342       r = gen_rtx_MEM (Pmode, addr_rtx);
3343       set_mem_alias_set (r, get_varargs_alias_set ());
3344       emit_move_insn (addr_rtx, r);
3345     }
3346
3347   return addr_rtx;
3348 }
3349 \f
3350 /* Return nonzero if OP is either a i387 or SSE fp register.  */
3351 int
3352 any_fp_register_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3353 {
3354   return ANY_FP_REG_P (op);
3355 }
3356
3357 /* Return nonzero if OP is an i387 fp register.  */
3358 int
3359 fp_register_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3360 {
3361   return FP_REG_P (op);
3362 }
3363
3364 /* Return nonzero if OP is a non-fp register_operand.  */
3365 int
3366 register_and_not_any_fp_reg_operand (rtx op, enum machine_mode mode)
3367 {
3368   return register_operand (op, mode) && !ANY_FP_REG_P (op);
3369 }
3370
3371 /* Return nonzero if OP is a register operand other than an
3372    i387 fp register.  */
3373 int
3374 register_and_not_fp_reg_operand (rtx op, enum machine_mode mode)
3375 {
3376   return register_operand (op, mode) && !FP_REG_P (op);
3377 }
3378
3379 /* Return nonzero if OP is general operand representable on x86_64.  */
3380
3381 int
3382 x86_64_general_operand (rtx op, enum machine_mode mode)
3383 {
3384   if (!TARGET_64BIT)
3385     return general_operand (op, mode);
3386   if (nonimmediate_operand (op, mode))
3387     return 1;
3388   return x86_64_sign_extended_value (op);
3389 }
3390
3391 /* Return nonzero if OP is general operand representable on x86_64
3392    as either sign extended or zero extended constant.  */
3393
3394 int
3395 x86_64_szext_general_operand (rtx op, enum machine_mode mode)
3396 {
3397   if (!TARGET_64BIT)
3398     return general_operand (op, mode);
3399   if (nonimmediate_operand (op, mode))
3400     return 1;
3401   return x86_64_sign_extended_value (op) || x86_64_zero_extended_value (op);
3402 }
3403
3404 /* Return nonzero if OP is nonmemory operand representable on x86_64.  */
3405
3406 int
3407 x86_64_nonmemory_operand (rtx op, enum machine_mode mode)
3408 {
3409   if (!TARGET_64BIT)
3410     return nonmemory_operand (op, mode);
3411   if (register_operand (op, mode))
3412     return 1;
3413   return x86_64_sign_extended_value (op);
3414 }
3415
3416 /* Return nonzero if OP is nonmemory operand acceptable by movabs patterns.  */
3417
3418 int
3419 x86_64_movabs_operand (rtx op, enum machine_mode mode)
3420 {
3421   if (!TARGET_64BIT || !flag_pic)
3422     return nonmemory_operand (op, mode);
3423   if (register_operand (op, mode) || x86_64_sign_extended_value (op))
3424     return 1;
3425   if (CONSTANT_P (op) && !symbolic_reference_mentioned_p (op))
3426     return 1;
3427   return 0;
3428 }
3429
3430 /* Return nonzero if OPNUM's MEM should be matched
3431    in movabs* patterns.  */
3432
3433 int
3434 ix86_check_movabs (rtx insn, int opnum)
3435 {
3436   rtx set, mem;
3437
3438   set = PATTERN (insn);
3439   if (GET_CODE (set) == PARALLEL)
3440     set = XVECEXP (set, 0, 0);
3441   if (GET_CODE (set) != SET)
3442     abort ();
3443   mem = XEXP (set, opnum);
3444   while (GET_CODE (mem) == SUBREG)
3445     mem = SUBREG_REG (mem);
3446   if (GET_CODE (mem) != MEM)
3447     abort ();
3448   return (volatile_ok || !MEM_VOLATILE_P (mem));
3449 }
3450
3451 /* Return nonzero if OP is nonmemory operand representable on x86_64.  */
3452
3453 int
3454 x86_64_szext_nonmemory_operand (rtx op, enum machine_mode mode)
3455 {
3456   if (!TARGET_64BIT)
3457     return nonmemory_operand (op, mode);
3458   if (register_operand (op, mode))
3459     return 1;
3460   return x86_64_sign_extended_value (op) || x86_64_zero_extended_value (op);
3461 }
3462
3463 /* Return nonzero if OP is immediate operand representable on x86_64.  */
3464
3465 int
3466 x86_64_immediate_operand (rtx op, enum machine_mode mode)
3467 {
3468   if (!TARGET_64BIT)
3469     return immediate_operand (op, mode);
3470   return x86_64_sign_extended_value (op);
3471 }
3472
3473 /* Return nonzero if OP is immediate operand representable on x86_64.  */
3474
3475 int
3476 x86_64_zext_immediate_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3477 {
3478   return x86_64_zero_extended_value (op);
3479 }
3480
3481 /* Return nonzero if OP is CONST_INT >= 1 and <= 31 (a valid operand
3482    for shift & compare patterns, as shifting by 0 does not change flags),
3483    else return zero.  */
3484
3485 int
3486 const_int_1_31_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3487 {
3488   return (GET_CODE (op) == CONST_INT && INTVAL (op) >= 1 && INTVAL (op) <= 31);
3489 }
3490
3491 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
3492    reference and a constant.  */
3493
3494 int
3495 symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3496 {
3497   switch (GET_CODE (op))
3498     {
3499     case SYMBOL_REF:
3500     case LABEL_REF:
3501       return 1;
3502
3503     case CONST:
3504       op = XEXP (op, 0);
3505       if (GET_CODE (op) == SYMBOL_REF
3506           || GET_CODE (op) == LABEL_REF
3507           || (GET_CODE (op) == UNSPEC
3508               && (XINT (op, 1) == UNSPEC_GOT
3509                   || XINT (op, 1) == UNSPEC_GOTOFF
3510                   || XINT (op, 1) == UNSPEC_GOTPCREL)))
3511         return 1;
3512       if (GET_CODE (op) != PLUS
3513           || GET_CODE (XEXP (op, 1)) != CONST_INT)
3514         return 0;
3515
3516       op = XEXP (op, 0);
3517       if (GET_CODE (op) == SYMBOL_REF
3518           || GET_CODE (op) == LABEL_REF)
3519         return 1;
3520       /* Only @GOTOFF gets offsets.  */
3521       if (GET_CODE (op) != UNSPEC
3522           || XINT (op, 1) != UNSPEC_GOTOFF)
3523         return 0;
3524
3525       op = XVECEXP (op, 0, 0);
3526       if (GET_CODE (op) == SYMBOL_REF
3527           || GET_CODE (op) == LABEL_REF)
3528         return 1;
3529       return 0;
3530
3531     default:
3532       return 0;
3533     }
3534 }
3535
3536 /* Return true if the operand contains a @GOT or @GOTOFF reference.  */
3537
3538 int
3539 pic_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3540 {
3541   if (GET_CODE (op) != CONST)
3542     return 0;
3543   op = XEXP (op, 0);
3544   if (TARGET_64BIT)
3545     {
3546       if (GET_CODE (op) == UNSPEC
3547           && XINT (op, 1) == UNSPEC_GOTPCREL)
3548         return 1;
3549       if (GET_CODE (op) == PLUS
3550           && GET_CODE (XEXP (op, 0)) == UNSPEC
3551           && XINT (XEXP (op, 0), 1) == UNSPEC_GOTPCREL)
3552         return 1;
3553     }
3554   else
3555     {
3556       if (GET_CODE (op) == UNSPEC)
3557         return 1;
3558       if (GET_CODE (op) != PLUS
3559           || GET_CODE (XEXP (op, 1)) != CONST_INT)
3560         return 0;
3561       op = XEXP (op, 0);
3562       if (GET_CODE (op) == UNSPEC)
3563         return 1;
3564     }
3565   return 0;
3566 }
3567
3568 /* Return true if OP is a symbolic operand that resolves locally.  */
3569
3570 static int
3571 local_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3572 {
3573   if (GET_CODE (op) == CONST
3574       && GET_CODE (XEXP (op, 0)) == PLUS
3575       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT)
3576     op = XEXP (XEXP (op, 0), 0);
3577
3578   if (GET_CODE (op) == LABEL_REF)
3579     return 1;
3580
3581   if (GET_CODE (op) != SYMBOL_REF)
3582     return 0;
3583
3584   if (SYMBOL_REF_LOCAL_P (op))
3585     return 1;
3586
3587   /* There is, however, a not insubstantial body of code in the rest of
3588      the compiler that assumes it can just stick the results of
3589      ASM_GENERATE_INTERNAL_LABEL in a symbol_ref and have done.  */
3590   /* ??? This is a hack.  Should update the body of the compiler to
3591      always create a DECL an invoke targetm.encode_section_info.  */
3592   if (strncmp (XSTR (op, 0), internal_label_prefix,
3593                internal_label_prefix_len) == 0)
3594     return 1;
3595
3596   return 0;
3597 }
3598
3599 /* Test for various thread-local symbols.  */
3600
3601 int
3602 tls_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3603 {
3604   if (GET_CODE (op) != SYMBOL_REF)
3605     return 0;
3606   return SYMBOL_REF_TLS_MODEL (op);
3607 }
3608
3609 static inline int
3610 tls_symbolic_operand_1 (rtx op, enum tls_model kind)
3611 {
3612   if (GET_CODE (op) != SYMBOL_REF)
3613     return 0;
3614   return SYMBOL_REF_TLS_MODEL (op) == kind;
3615 }
3616
3617 int
3618 global_dynamic_symbolic_operand (rtx op,
3619                                  enum machine_mode mode ATTRIBUTE_UNUSED)
3620 {
3621   return tls_symbolic_operand_1 (op, TLS_MODEL_GLOBAL_DYNAMIC);
3622 }
3623
3624 int
3625 local_dynamic_symbolic_operand (rtx op,
3626                                 enum machine_mode mode ATTRIBUTE_UNUSED)
3627 {
3628   return tls_symbolic_operand_1 (op, TLS_MODEL_LOCAL_DYNAMIC);
3629 }
3630
3631 int
3632 initial_exec_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3633 {
3634   return tls_symbolic_operand_1 (op, TLS_MODEL_INITIAL_EXEC);
3635 }
3636
3637 int
3638 local_exec_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3639 {
3640   return tls_symbolic_operand_1 (op, TLS_MODEL_LOCAL_EXEC);
3641 }
3642
3643 /* Test for a valid operand for a call instruction.  Don't allow the
3644    arg pointer register or virtual regs since they may decay into
3645    reg + const, which the patterns can't handle.  */
3646
3647 int
3648 call_insn_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3649 {
3650   /* Disallow indirect through a virtual register.  This leads to
3651      compiler aborts when trying to eliminate them.  */
3652   if (GET_CODE (op) == REG
3653       && (op == arg_pointer_rtx
3654           || op == frame_pointer_rtx
3655           || (REGNO (op) >= FIRST_PSEUDO_REGISTER
3656               && REGNO (op) <= LAST_VIRTUAL_REGISTER)))
3657     return 0;
3658
3659   /* Disallow `call 1234'.  Due to varying assembler lameness this
3660      gets either rejected or translated to `call .+1234'.  */
3661   if (GET_CODE (op) == CONST_INT)
3662     return 0;
3663
3664   /* Explicitly allow SYMBOL_REF even if pic.  */
3665   if (GET_CODE (op) == SYMBOL_REF)
3666     return 1;
3667
3668   /* Otherwise we can allow any general_operand in the address.  */
3669   return general_operand (op, Pmode);
3670 }
3671
3672 /* Test for a valid operand for a call instruction.  Don't allow the
3673    arg pointer register or virtual regs since they may decay into
3674    reg + const, which the patterns can't handle.  */
3675
3676 int
3677 sibcall_insn_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3678 {
3679   /* Disallow indirect through a virtual register.  This leads to
3680      compiler aborts when trying to eliminate them.  */
3681   if (GET_CODE (op) == REG
3682       && (op == arg_pointer_rtx
3683           || op == frame_pointer_rtx
3684           || (REGNO (op) >= FIRST_PSEUDO_REGISTER
3685               && REGNO (op) <= LAST_VIRTUAL_REGISTER)))
3686     return 0;
3687
3688   /* Explicitly allow SYMBOL_REF even if pic.  */
3689   if (GET_CODE (op) == SYMBOL_REF)
3690     return 1;
3691
3692   /* Otherwise we can only allow register operands.  */
3693   return register_operand (op, Pmode);
3694 }
3695
3696 int
3697 constant_call_address_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3698 {
3699   if (GET_CODE (op) == CONST
3700       && GET_CODE (XEXP (op, 0)) == PLUS
3701       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT)
3702     op = XEXP (XEXP (op, 0), 0);
3703   return GET_CODE (op) == SYMBOL_REF;
3704 }
3705
3706 /* Match exactly zero and one.  */
3707
3708 int
3709 const0_operand (rtx op, enum machine_mode mode)
3710 {
3711   return op == CONST0_RTX (mode);
3712 }
3713
3714 int
3715 const1_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3716 {
3717   return op == const1_rtx;
3718 }
3719
3720 /* Match 2, 4, or 8.  Used for leal multiplicands.  */
3721
3722 int
3723 const248_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3724 {
3725   return (GET_CODE (op) == CONST_INT
3726           && (INTVAL (op) == 2 || INTVAL (op) == 4 || INTVAL (op) == 8));
3727 }
3728
3729 int
3730 const_0_to_3_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3731 {
3732   return (GET_CODE (op) == CONST_INT && INTVAL (op) >= 0 && INTVAL (op) < 4);
3733 }
3734
3735 int
3736 const_0_to_7_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3737 {
3738   return (GET_CODE (op) == CONST_INT && INTVAL (op) >= 0 && INTVAL (op) < 8);
3739 }
3740
3741 int
3742 const_0_to_15_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3743 {
3744   return (GET_CODE (op) == CONST_INT && INTVAL (op) >= 0 && INTVAL (op) < 16);
3745 }
3746
3747 int
3748 const_0_to_255_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3749 {
3750   return (GET_CODE (op) == CONST_INT && INTVAL (op) >= 0 && INTVAL (op) < 256);
3751 }
3752
3753
3754 /* True if this is a constant appropriate for an increment or decrement.  */
3755
3756 int
3757 incdec_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3758 {
3759   /* On Pentium4, the inc and dec operations causes extra dependency on flag
3760      registers, since carry flag is not set.  */
3761   if (TARGET_PENTIUM4 && !optimize_size)
3762     return 0;
3763   return op == const1_rtx || op == constm1_rtx;
3764 }
3765
3766 /* Return nonzero if OP is acceptable as operand of DImode shift
3767    expander.  */
3768
3769 int
3770 shiftdi_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3771 {
3772   if (TARGET_64BIT)
3773     return nonimmediate_operand (op, mode);
3774   else
3775     return register_operand (op, mode);
3776 }
3777
3778 /* Return false if this is the stack pointer, or any other fake
3779    register eliminable to the stack pointer.  Otherwise, this is
3780    a register operand.
3781
3782    This is used to prevent esp from being used as an index reg.
3783    Which would only happen in pathological cases.  */
3784
3785 int
3786 reg_no_sp_operand (rtx op, enum machine_mode mode)
3787 {
3788   rtx t = op;
3789   if (GET_CODE (t) == SUBREG)
3790     t = SUBREG_REG (t);
3791   if (t == stack_pointer_rtx || t == arg_pointer_rtx || t == frame_pointer_rtx)
3792     return 0;
3793
3794   return register_operand (op, mode);
3795 }
3796
3797 int
3798 mmx_reg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3799 {
3800   return MMX_REG_P (op);
3801 }
3802
3803 /* Return false if this is any eliminable register.  Otherwise
3804    general_operand.  */
3805
3806 int
3807 general_no_elim_operand (rtx op, enum machine_mode mode)
3808 {
3809   rtx t = op;
3810   if (GET_CODE (t) == SUBREG)
3811     t = SUBREG_REG (t);
3812   if (t == arg_pointer_rtx || t == frame_pointer_rtx
3813       || t == virtual_incoming_args_rtx || t == virtual_stack_vars_rtx
3814       || t == virtual_stack_dynamic_rtx)
3815     return 0;
3816   if (REG_P (t)
3817       && REGNO (t) >= FIRST_VIRTUAL_REGISTER
3818       && REGNO (t) <= LAST_VIRTUAL_REGISTER)
3819     return 0;
3820
3821   return general_operand (op, mode);
3822 }
3823
3824 /* Return false if this is any eliminable register.  Otherwise
3825    register_operand or const_int.  */
3826
3827 int
3828 nonmemory_no_elim_operand (rtx op, enum machine_mode mode)
3829 {
3830   rtx t = op;
3831   if (GET_CODE (t) == SUBREG)
3832     t = SUBREG_REG (t);
3833   if (t == arg_pointer_rtx || t == frame_pointer_rtx
3834       || t == virtual_incoming_args_rtx || t == virtual_stack_vars_rtx
3835       || t == virtual_stack_dynamic_rtx)
3836     return 0;
3837
3838   return GET_CODE (op) == CONST_INT || register_operand (op, mode);
3839 }
3840
3841 /* Return false if this is any eliminable register or stack register,
3842    otherwise work like register_operand.  */
3843
3844 int
3845 index_register_operand (rtx op, enum machine_mode mode)
3846 {
3847   rtx t = op;
3848   if (GET_CODE (t) == SUBREG)
3849     t = SUBREG_REG (t);
3850   if (!REG_P (t))
3851     return 0;
3852   if (t == arg_pointer_rtx
3853       || t == frame_pointer_rtx
3854       || t == virtual_incoming_args_rtx
3855       || t == virtual_stack_vars_rtx
3856       || t == virtual_stack_dynamic_rtx
3857       || REGNO (t) == STACK_POINTER_REGNUM)
3858     return 0;
3859
3860   return general_operand (op, mode);
3861 }
3862
3863 /* Return true if op is a Q_REGS class register.  */
3864
3865 int
3866 q_regs_operand (rtx op, enum machine_mode mode)
3867 {
3868   if (mode != VOIDmode && GET_MODE (op) != mode)
3869     return 0;
3870   if (GET_CODE (op) == SUBREG)
3871     op = SUBREG_REG (op);
3872   return ANY_QI_REG_P (op);
3873 }
3874
3875 /* Return true if op is an flags register.  */
3876
3877 int
3878 flags_reg_operand (rtx op, enum machine_mode mode)
3879 {
3880   if (mode != VOIDmode && GET_MODE (op) != mode)
3881     return 0;
3882   return REG_P (op) && REGNO (op) == FLAGS_REG && GET_MODE (op) != VOIDmode;
3883 }
3884
3885 /* Return true if op is a NON_Q_REGS class register.  */
3886
3887 int
3888 non_q_regs_operand (rtx op, enum machine_mode mode)
3889 {
3890   if (mode != VOIDmode && GET_MODE (op) != mode)
3891     return 0;
3892   if (GET_CODE (op) == SUBREG)
3893     op = SUBREG_REG (op);
3894   return NON_QI_REG_P (op);
3895 }
3896
3897 int
3898 zero_extended_scalar_load_operand (rtx op,
3899                                    enum machine_mode mode ATTRIBUTE_UNUSED)
3900 {
3901   unsigned n_elts;
3902   if (GET_CODE (op) != MEM)
3903     return 0;
3904   op = maybe_get_pool_constant (op);
3905   if (!op)
3906     return 0;
3907   if (GET_CODE (op) != CONST_VECTOR)
3908     return 0;
3909   n_elts =
3910     (GET_MODE_SIZE (GET_MODE (op)) /
3911      GET_MODE_SIZE (GET_MODE_INNER (GET_MODE (op))));
3912   for (n_elts--; n_elts > 0; n_elts--)
3913     {
3914       rtx elt = CONST_VECTOR_ELT (op, n_elts);
3915       if (elt != CONST0_RTX (GET_MODE_INNER (GET_MODE (op))))
3916         return 0;
3917     }
3918   return 1;
3919 }
3920
3921 /*  Return 1 when OP is operand acceptable for standard SSE move.  */
3922 int
3923 vector_move_operand (rtx op, enum machine_mode mode)
3924 {
3925   if (nonimmediate_operand (op, mode))
3926     return 1;
3927   if (GET_MODE (op) != mode && mode != VOIDmode)
3928     return 0;
3929   return (op == CONST0_RTX (GET_MODE (op)));
3930 }
3931
3932 /* Return true if op if a valid address, and does not contain
3933    a segment override.  */
3934
3935 int
3936 no_seg_address_operand (rtx op, enum machine_mode mode)
3937 {
3938   struct ix86_address parts;
3939
3940   if (! address_operand (op, mode))
3941     return 0;
3942
3943   if (! ix86_decompose_address (op, &parts))
3944     abort ();
3945
3946   return parts.seg == SEG_DEFAULT;
3947 }
3948
3949 /* Return 1 if OP is a comparison that can be used in the CMPSS/CMPPS
3950    insns.  */
3951 int
3952 sse_comparison_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3953 {
3954   enum rtx_code code = GET_CODE (op);
3955   switch (code)
3956     {
3957     /* Operations supported directly.  */
3958     case EQ:
3959     case LT:
3960     case LE:
3961     case UNORDERED:
3962     case NE:
3963     case UNGE:
3964     case UNGT:
3965     case ORDERED:
3966       return 1;
3967     /* These are equivalent to ones above in non-IEEE comparisons.  */
3968     case UNEQ:
3969     case UNLT:
3970     case UNLE:
3971     case LTGT:
3972     case GE:
3973     case GT:
3974       return !TARGET_IEEE_FP;
3975     default:
3976       return 0;
3977     }
3978 }
3979 /* Return 1 if OP is a valid comparison operator in valid mode.  */
3980 int
3981 ix86_comparison_operator (rtx op, enum machine_mode mode)
3982 {
3983   enum machine_mode inmode;
3984   enum rtx_code code = GET_CODE (op);
3985   if (mode != VOIDmode && GET_MODE (op) != mode)
3986     return 0;
3987   if (GET_RTX_CLASS (code) != '<')
3988     return 0;
3989   inmode = GET_MODE (XEXP (op, 0));
3990
3991   if (inmode == CCFPmode || inmode == CCFPUmode)
3992     {
3993       enum rtx_code second_code, bypass_code;
3994       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
3995       return (bypass_code == NIL && second_code == NIL);
3996     }
3997   switch (code)
3998     {
3999     case EQ: case NE:
4000       return 1;
4001     case LT: case GE:
4002       if (inmode == CCmode || inmode == CCGCmode
4003           || inmode == CCGOCmode || inmode == CCNOmode)
4004         return 1;
4005       return 0;
4006     case LTU: case GTU: case LEU: case ORDERED: case UNORDERED: case GEU:
4007       if (inmode == CCmode)
4008         return 1;
4009       return 0;
4010     case GT: case LE:
4011       if (inmode == CCmode || inmode == CCGCmode || inmode == CCNOmode)
4012         return 1;
4013       return 0;
4014     default:
4015       return 0;
4016     }
4017 }
4018
4019 /* Return 1 if OP is a valid comparison operator testing carry flag
4020    to be set.  */
4021 int
4022 ix86_carry_flag_operator (rtx op, enum machine_mode mode)
4023 {
4024   enum machine_mode inmode;
4025   enum rtx_code code = GET_CODE (op);
4026
4027   if (mode != VOIDmode && GET_MODE (op) != mode)
4028     return 0;
4029   if (GET_RTX_CLASS (code) != '<')
4030     return 0;
4031   inmode = GET_MODE (XEXP (op, 0));
4032   if (GET_CODE (XEXP (op, 0)) != REG
4033       || REGNO (XEXP (op, 0)) != 17
4034       || XEXP (op, 1) != const0_rtx)
4035     return 0;
4036
4037   if (inmode == CCFPmode || inmode == CCFPUmode)
4038     {
4039       enum rtx_code second_code, bypass_code;
4040
4041       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
4042       if (bypass_code != NIL || second_code != NIL)
4043         return 0;
4044       code = ix86_fp_compare_code_to_integer (code);
4045     }
4046   else if (inmode != CCmode)
4047     return 0;
4048   return code == LTU;
4049 }
4050
4051 /* Return 1 if OP is a comparison operator that can be issued by fcmov.  */
4052
4053 int
4054 fcmov_comparison_operator (rtx op, enum machine_mode mode)
4055 {
4056   enum machine_mode inmode;
4057   enum rtx_code code = GET_CODE (op);
4058
4059   if (mode != VOIDmode && GET_MODE (op) != mode)
4060     return 0;
4061   if (GET_RTX_CLASS (code) != '<')
4062     return 0;
4063   inmode = GET_MODE (XEXP (op, 0));
4064   if (inmode == CCFPmode || inmode == CCFPUmode)
4065     {
4066       enum rtx_code second_code, bypass_code;
4067
4068       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
4069       if (bypass_code != NIL || second_code != NIL)
4070         return 0;
4071       code = ix86_fp_compare_code_to_integer (code);
4072     }
4073   /* i387 supports just limited amount of conditional codes.  */
4074   switch (code)
4075     {
4076     case LTU: case GTU: case LEU: case GEU:
4077       if (inmode == CCmode || inmode == CCFPmode || inmode == CCFPUmode)
4078         return 1;
4079       return 0;
4080     case ORDERED: case UNORDERED:
4081     case EQ: case NE:
4082       return 1;
4083     default:
4084       return 0;
4085     }
4086 }
4087
4088 /* Return 1 if OP is a binary operator that can be promoted to wider mode.  */
4089
4090 int
4091 promotable_binary_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4092 {
4093   switch (GET_CODE (op))
4094     {
4095     case MULT:
4096       /* Modern CPUs have same latency for HImode and SImode multiply,
4097          but 386 and 486 do HImode multiply faster.  */
4098       return ix86_tune > PROCESSOR_I486;
4099     case PLUS:
4100     case AND:
4101     case IOR:
4102     case XOR:
4103     case ASHIFT:
4104       return 1;
4105     default:
4106       return 0;
4107     }
4108 }
4109
4110 /* Nearly general operand, but accept any const_double, since we wish
4111    to be able to drop them into memory rather than have them get pulled
4112    into registers.  */
4113
4114 int
4115 cmp_fp_expander_operand (rtx op, enum machine_mode mode)
4116 {
4117   if (mode != VOIDmode && mode != GET_MODE (op))
4118     return 0;
4119   if (GET_CODE (op) == CONST_DOUBLE)
4120     return 1;
4121   return general_operand (op, mode);
4122 }
4123
4124 /* Match an SI or HImode register for a zero_extract.  */
4125
4126 int
4127 ext_register_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4128 {
4129   int regno;
4130   if ((!TARGET_64BIT || GET_MODE (op) != DImode)
4131       && GET_MODE (op) != SImode && GET_MODE (op) != HImode)
4132     return 0;
4133
4134   if (!register_operand (op, VOIDmode))
4135     return 0;
4136
4137   /* Be careful to accept only registers having upper parts.  */
4138   regno = REG_P (op) ? REGNO (op) : REGNO (SUBREG_REG (op));
4139   return (regno > LAST_VIRTUAL_REGISTER || regno < 4);
4140 }
4141
4142 /* Return 1 if this is a valid binary floating-point operation.
4143    OP is the expression matched, and MODE is its mode.  */
4144
4145 int
4146 binary_fp_operator (rtx op, enum machine_mode mode)
4147 {
4148   if (mode != VOIDmode && mode != GET_MODE (op))
4149     return 0;
4150
4151   switch (GET_CODE (op))
4152     {
4153     case PLUS:
4154     case MINUS:
4155     case MULT:
4156     case DIV:
4157       return GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT;
4158
4159     default:
4160       return 0;
4161     }
4162 }
4163
4164 int
4165 mult_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4166 {
4167   return GET_CODE (op) == MULT;
4168 }
4169
4170 int
4171 div_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4172 {
4173   return GET_CODE (op) == DIV;
4174 }
4175
4176 int
4177 arith_or_logical_operator (rtx op, enum machine_mode mode)
4178 {
4179   return ((mode == VOIDmode || GET_MODE (op) == mode)
4180           && (GET_RTX_CLASS (GET_CODE (op)) == 'c'
4181               || GET_RTX_CLASS (GET_CODE (op)) == '2'));
4182 }
4183
4184 /* Returns 1 if OP is memory operand with a displacement.  */
4185
4186 int
4187 memory_displacement_operand (rtx op, enum machine_mode mode)
4188 {
4189   struct ix86_address parts;
4190
4191   if (! memory_operand (op, mode))
4192     return 0;
4193
4194   if (! ix86_decompose_address (XEXP (op, 0), &parts))
4195     abort ();
4196
4197   return parts.disp != NULL_RTX;
4198 }
4199
4200 /* To avoid problems when jump re-emits comparisons like testqi_ext_ccno_0,
4201    re-recognize the operand to avoid a copy_to_mode_reg that will fail.
4202
4203    ??? It seems likely that this will only work because cmpsi is an
4204    expander, and no actual insns use this.  */
4205
4206 int
4207 cmpsi_operand (rtx op, enum machine_mode mode)
4208 {
4209   if (nonimmediate_operand (op, mode))
4210     return 1;
4211
4212   if (GET_CODE (op) == AND
4213       && GET_MODE (op) == SImode
4214       && GET_CODE (XEXP (op, 0)) == ZERO_EXTRACT
4215       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
4216       && GET_CODE (XEXP (XEXP (op, 0), 2)) == CONST_INT
4217       && INTVAL (XEXP (XEXP (op, 0), 1)) == 8
4218       && INTVAL (XEXP (XEXP (op, 0), 2)) == 8
4219       && GET_CODE (XEXP (op, 1)) == CONST_INT)
4220     return 1;
4221
4222   return 0;
4223 }
4224
4225 /* Returns 1 if OP is memory operand that can not be represented by the
4226    modRM array.  */
4227
4228 int
4229 long_memory_operand (rtx op, enum machine_mode mode)
4230 {
4231   if (! memory_operand (op, mode))
4232     return 0;
4233
4234   return memory_address_length (op) != 0;
4235 }
4236
4237 /* Return nonzero if the rtx is known aligned.  */
4238
4239 int
4240 aligned_operand (rtx op, enum machine_mode mode)
4241 {
4242   struct ix86_address parts;
4243
4244   if (!general_operand (op, mode))
4245     return 0;
4246
4247   /* Registers and immediate operands are always "aligned".  */
4248   if (GET_CODE (op) != MEM)
4249     return 1;
4250
4251   /* Don't even try to do any aligned optimizations with volatiles.  */
4252   if (MEM_VOLATILE_P (op))
4253     return 0;
4254
4255   op = XEXP (op, 0);
4256
4257   /* Pushes and pops are only valid on the stack pointer.  */
4258   if (GET_CODE (op) == PRE_DEC
4259       || GET_CODE (op) == POST_INC)
4260     return 1;
4261
4262   /* Decode the address.  */
4263   if (! ix86_decompose_address (op, &parts))
4264     abort ();
4265
4266   /* Look for some component that isn't known to be aligned.  */
4267   if (parts.index)
4268     {
4269       if (parts.scale < 4
4270           && REGNO_POINTER_ALIGN (REGNO (parts.index)) < 32)
4271         return 0;
4272     }
4273   if (parts.base)
4274     {
4275       if (REGNO_POINTER_ALIGN (REGNO (parts.base)) < 32)
4276         return 0;
4277     }
4278   if (parts.disp)
4279     {
4280       if (GET_CODE (parts.disp) != CONST_INT
4281           || (INTVAL (parts.disp) & 3) != 0)
4282         return 0;
4283     }
4284
4285   /* Didn't find one -- this must be an aligned address.  */
4286   return 1;
4287 }
4288 \f
4289 /* Initialize the table of extra 80387 mathematical constants.  */
4290
4291 static void
4292 init_ext_80387_constants (void)
4293 {
4294   static const char * cst[5] =
4295   {
4296     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
4297     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
4298     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
4299     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
4300     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
4301   };
4302   int i;
4303
4304   for (i = 0; i < 5; i++)
4305     {
4306       real_from_string (&ext_80387_constants_table[i], cst[i]);
4307       /* Ensure each constant is rounded to XFmode precision.  */
4308       real_convert (&ext_80387_constants_table[i],
4309                     XFmode, &ext_80387_constants_table[i]);
4310     }
4311
4312   ext_80387_constants_init = 1;
4313 }
4314
4315 /* Return true if the constant is something that can be loaded with
4316    a special instruction.  */
4317
4318 int
4319 standard_80387_constant_p (rtx x)
4320 {
4321   if (GET_CODE (x) != CONST_DOUBLE || !FLOAT_MODE_P (GET_MODE (x)))
4322     return -1;
4323
4324   if (x == CONST0_RTX (GET_MODE (x)))
4325     return 1;
4326   if (x == CONST1_RTX (GET_MODE (x)))
4327     return 2;
4328
4329   /* For XFmode constants, try to find a special 80387 instruction on
4330      those CPUs that benefit from them.  */
4331   if (GET_MODE (x) == XFmode
4332       && x86_ext_80387_constants & TUNEMASK)
4333     {
4334       REAL_VALUE_TYPE r;
4335       int i;
4336
4337       if (! ext_80387_constants_init)
4338         init_ext_80387_constants ();
4339
4340       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4341       for (i = 0; i < 5; i++)
4342         if (real_identical (&r, &ext_80387_constants_table[i]))
4343           return i + 3;
4344     }
4345
4346   return 0;
4347 }
4348
4349 /* Return the opcode of the special instruction to be used to load
4350    the constant X.  */
4351
4352 const char *
4353 standard_80387_constant_opcode (rtx x)
4354 {
4355   switch (standard_80387_constant_p (x))
4356     {
4357     case 1:
4358       return "fldz";
4359     case 2:
4360       return "fld1";
4361     case 3:
4362       return "fldlg2";
4363     case 4:
4364       return "fldln2";
4365     case 5:
4366       return "fldl2e";
4367     case 6:
4368       return "fldl2t";
4369     case 7:
4370       return "fldpi";
4371     }
4372   abort ();
4373 }
4374
4375 /* Return the CONST_DOUBLE representing the 80387 constant that is
4376    loaded by the specified special instruction.  The argument IDX
4377    matches the return value from standard_80387_constant_p.  */
4378
4379 rtx
4380 standard_80387_constant_rtx (int idx)
4381 {
4382   int i;
4383
4384   if (! ext_80387_constants_init)
4385     init_ext_80387_constants ();
4386
4387   switch (idx)
4388     {
4389     case 3:
4390     case 4:
4391     case 5:
4392     case 6:
4393     case 7:
4394       i = idx - 3;
4395       break;
4396
4397     default:
4398       abort ();
4399     }
4400
4401   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
4402                                        XFmode);
4403 }
4404
4405 /* Return 1 if X is FP constant we can load to SSE register w/o using memory.
4406  */
4407 int
4408 standard_sse_constant_p (rtx x)
4409 {
4410   if (x == const0_rtx)
4411     return 1;
4412   return (x == CONST0_RTX (GET_MODE (x)));
4413 }
4414
4415 /* Returns 1 if OP contains a symbol reference */
4416
4417 int
4418 symbolic_reference_mentioned_p (rtx op)
4419 {
4420   const char *fmt;
4421   int i;
4422
4423   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
4424     return 1;
4425
4426   fmt = GET_RTX_FORMAT (GET_CODE (op));
4427   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
4428     {
4429       if (fmt[i] == 'E')
4430         {
4431           int j;
4432
4433           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
4434             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
4435               return 1;
4436         }
4437
4438       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
4439         return 1;
4440     }
4441
4442   return 0;
4443 }
4444
4445 /* Return 1 if it is appropriate to emit `ret' instructions in the
4446    body of a function.  Do this only if the epilogue is simple, needing a
4447    couple of insns.  Prior to reloading, we can't tell how many registers
4448    must be saved, so return 0 then.  Return 0 if there is no frame
4449    marker to de-allocate.
4450
4451    If NON_SAVING_SETJMP is defined and true, then it is not possible
4452    for the epilogue to be simple, so return 0.  This is a special case
4453    since NON_SAVING_SETJMP will not cause regs_ever_live to change
4454    until final, but jump_optimize may need to know sooner if a
4455    `return' is OK.  */
4456
4457 int
4458 ix86_can_use_return_insn_p (void)
4459 {
4460   struct ix86_frame frame;
4461
4462 #ifdef NON_SAVING_SETJMP
4463   if (NON_SAVING_SETJMP && current_function_calls_setjmp)
4464     return 0;
4465 #endif
4466
4467   if (! reload_completed || frame_pointer_needed)
4468     return 0;
4469
4470   /* Don't allow more than 32 pop, since that's all we can do
4471      with one instruction.  */
4472   if (current_function_pops_args
4473       && current_function_args_size >= 32768)
4474     return 0;
4475
4476   ix86_compute_frame_layout (&frame);
4477   return frame.to_allocate == 0 && frame.nregs == 0;
4478 }
4479 \f
4480 /* Return 1 if VALUE can be stored in the sign extended immediate field.  */
4481 int
4482 x86_64_sign_extended_value (rtx value)
4483 {
4484   switch (GET_CODE (value))
4485     {
4486       /* CONST_DOUBLES never match, since HOST_BITS_PER_WIDE_INT is known
4487          to be at least 32 and this all acceptable constants are
4488          represented as CONST_INT.  */
4489       case CONST_INT:
4490         if (HOST_BITS_PER_WIDE_INT == 32)
4491           return 1;
4492         else
4493           {
4494             HOST_WIDE_INT val = trunc_int_for_mode (INTVAL (value), DImode);
4495             return trunc_int_for_mode (val, SImode) == val;
4496           }
4497         break;
4498
4499       /* For certain code models, the symbolic references are known to fit.
4500          in CM_SMALL_PIC model we know it fits if it is local to the shared
4501          library.  Don't count TLS SYMBOL_REFs here, since they should fit
4502          only if inside of UNSPEC handled below.  */
4503       case SYMBOL_REF:
4504         /* TLS symbols are not constant.  */
4505         if (tls_symbolic_operand (value, Pmode))
4506           return false;
4507         return (ix86_cmodel == CM_SMALL || ix86_cmodel == CM_KERNEL);
4508
4509       /* For certain code models, the code is near as well.  */
4510       case LABEL_REF:
4511         return (ix86_cmodel == CM_SMALL || ix86_cmodel == CM_MEDIUM
4512                 || ix86_cmodel == CM_KERNEL);
4513
4514       /* We also may accept the offsetted memory references in certain special
4515          cases.  */
4516       case CONST:
4517         if (GET_CODE (XEXP (value, 0)) == UNSPEC)
4518           switch (XINT (XEXP (value, 0), 1))
4519             {
4520             case UNSPEC_GOTPCREL:
4521             case UNSPEC_DTPOFF:
4522             case UNSPEC_GOTNTPOFF:
4523             case UNSPEC_NTPOFF:
4524               return 1;
4525             default:
4526               break;
4527             }
4528         if (GET_CODE (XEXP (value, 0)) == PLUS)
4529           {
4530             rtx op1 = XEXP (XEXP (value, 0), 0);
4531             rtx op2 = XEXP (XEXP (value, 0), 1);
4532             HOST_WIDE_INT offset;
4533
4534             if (ix86_cmodel == CM_LARGE)
4535               return 0;
4536             if (GET_CODE (op2) != CONST_INT)
4537               return 0;
4538             offset = trunc_int_for_mode (INTVAL (op2), DImode);
4539             switch (GET_CODE (op1))
4540               {
4541                 case SYMBOL_REF:
4542                   /* For CM_SMALL assume that latest object is 16MB before
4543                      end of 31bits boundary.  We may also accept pretty
4544                      large negative constants knowing that all objects are
4545                      in the positive half of address space.  */
4546                   if (ix86_cmodel == CM_SMALL
4547                       && offset < 16*1024*1024
4548                       && trunc_int_for_mode (offset, SImode) == offset)
4549                     return 1;
4550                   /* For CM_KERNEL we know that all object resist in the
4551                      negative half of 32bits address space.  We may not
4552                      accept negative offsets, since they may be just off
4553                      and we may accept pretty large positive ones.  */
4554                   if (ix86_cmodel == CM_KERNEL
4555                       && offset > 0
4556                       && trunc_int_for_mode (offset, SImode) == offset)
4557                     return 1;
4558                   break;
4559                 case LABEL_REF:
4560                   /* These conditions are similar to SYMBOL_REF ones, just the
4561                      constraints for code models differ.  */
4562                   if ((ix86_cmodel == CM_SMALL || ix86_cmodel == CM_MEDIUM)
4563                       && offset < 16*1024*1024
4564                       && trunc_int_for_mode (offset, SImode) == offset)
4565                     return 1;
4566                   if (ix86_cmodel == CM_KERNEL
4567                       && offset > 0
4568                       && trunc_int_for_mode (offset, SImode) == offset)
4569                     return 1;
4570                   break;
4571                 case UNSPEC:
4572                   switch (XINT (op1, 1))
4573                     {
4574                     case UNSPEC_DTPOFF:
4575                     case UNSPEC_NTPOFF:
4576                       if (offset > 0
4577                           && trunc_int_for_mode (offset, SImode) == offset)
4578                         return 1;
4579                     }
4580                   break;
4581                 default:
4582                   return 0;
4583               }
4584           }
4585         return 0;
4586       default:
4587         return 0;
4588     }
4589 }
4590
4591 /* Return 1 if VALUE can be stored in the zero extended immediate field.  */
4592 int
4593 x86_64_zero_extended_value (rtx value)
4594 {
4595   switch (GET_CODE (value))
4596     {
4597       case CONST_DOUBLE:
4598         if (HOST_BITS_PER_WIDE_INT == 32)
4599           return  (GET_MODE (value) == VOIDmode
4600                    && !CONST_DOUBLE_HIGH (value));
4601         else
4602           return 0;
4603       case CONST_INT:
4604         if (HOST_BITS_PER_WIDE_INT == 32)
4605           return INTVAL (value) >= 0;
4606         else
4607           return !(INTVAL (value) & ~(HOST_WIDE_INT) 0xffffffff);
4608         break;
4609
4610       /* For certain code models, the symbolic references are known to fit.  */
4611       case SYMBOL_REF:
4612         /* TLS symbols are not constant.  */
4613         if (tls_symbolic_operand (value, Pmode))
4614           return false;
4615         return ix86_cmodel == CM_SMALL;
4616
4617       /* For certain code models, the code is near as well.  */
4618       case LABEL_REF:
4619         return ix86_cmodel == CM_SMALL || ix86_cmodel == CM_MEDIUM;
4620
4621       /* We also may accept the offsetted memory references in certain special
4622          cases.  */
4623       case CONST:
4624         if (GET_CODE (XEXP (value, 0)) == PLUS)
4625           {
4626             rtx op1 = XEXP (XEXP (value, 0), 0);
4627             rtx op2 = XEXP (XEXP (value, 0), 1);
4628
4629             if (ix86_cmodel == CM_LARGE)
4630               return 0;
4631             switch (GET_CODE (op1))
4632               {
4633                 case SYMBOL_REF:
4634                     return 0;
4635                   /* For small code model we may accept pretty large positive
4636                      offsets, since one bit is available for free.  Negative
4637                      offsets are limited by the size of NULL pointer area
4638                      specified by the ABI.  */
4639                   if (ix86_cmodel == CM_SMALL
4640                       && GET_CODE (op2) == CONST_INT
4641                       && trunc_int_for_mode (INTVAL (op2), DImode) > -0x10000
4642                       && (trunc_int_for_mode (INTVAL (op2), SImode)
4643                           == INTVAL (op2)))
4644                     return 1;
4645                   /* ??? For the kernel, we may accept adjustment of
4646                      -0x10000000, since we know that it will just convert
4647                      negative address space to positive, but perhaps this
4648                      is not worthwhile.  */
4649                   break;
4650                 case LABEL_REF:
4651                   /* These conditions are similar to SYMBOL_REF ones, just the
4652                      constraints for code models differ.  */
4653                   if ((ix86_cmodel == CM_SMALL || ix86_cmodel == CM_MEDIUM)
4654                       && GET_CODE (op2) == CONST_INT
4655                       && trunc_int_for_mode (INTVAL (op2), DImode) > -0x10000
4656                       && (trunc_int_for_mode (INTVAL (op2), SImode)
4657                           == INTVAL (op2)))
4658                     return 1;
4659                   break;
4660                 default:
4661                   return 0;
4662               }
4663           }
4664         return 0;
4665       default:
4666         return 0;
4667     }
4668 }
4669
4670 /* Value should be nonzero if functions must have frame pointers.
4671    Zero means the frame pointer need not be set up (and parms may
4672    be accessed via the stack pointer) in functions that seem suitable.  */
4673
4674 int
4675 ix86_frame_pointer_required (void)
4676 {
4677   /* If we accessed previous frames, then the generated code expects
4678      to be able to access the saved ebp value in our frame.  */
4679   if (cfun->machine->accesses_prev_frame)
4680     return 1;
4681
4682   /* Several x86 os'es need a frame pointer for other reasons,
4683      usually pertaining to setjmp.  */
4684   if (SUBTARGET_FRAME_POINTER_REQUIRED)
4685     return 1;
4686
4687   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
4688      the frame pointer by default.  Turn it back on now if we've not
4689      got a leaf function.  */
4690   if (TARGET_OMIT_LEAF_FRAME_POINTER
4691       && (!current_function_is_leaf))
4692     return 1;
4693
4694   if (current_function_profile)
4695     return 1;
4696
4697   return 0;
4698 }
4699
4700 /* Record that the current function accesses previous call frames.  */
4701
4702 void
4703 ix86_setup_frame_addresses (void)
4704 {
4705   cfun->machine->accesses_prev_frame = 1;
4706 }
4707 \f
4708 #if defined(HAVE_GAS_HIDDEN) && defined(SUPPORTS_ONE_ONLY)
4709 # define USE_HIDDEN_LINKONCE 1
4710 #else
4711 # define USE_HIDDEN_LINKONCE 0
4712 #endif
4713
4714 static int pic_labels_used;
4715
4716 /* Fills in the label name that should be used for a pc thunk for
4717    the given register.  */
4718
4719 static void
4720 get_pc_thunk_name (char name[32], unsigned int regno)
4721 {
4722   if (USE_HIDDEN_LINKONCE)
4723     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
4724   else
4725     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
4726 }
4727
4728
4729 /* This function generates code for -fpic that loads %ebx with
4730    the return address of the caller and then returns.  */
4731
4732 void
4733 ix86_file_end (void)
4734 {
4735   rtx xops[2];
4736   int regno;
4737
4738   for (regno = 0; regno < 8; ++regno)
4739     {
4740       char name[32];
4741
4742       if (! ((pic_labels_used >> regno) & 1))
4743         continue;
4744
4745       get_pc_thunk_name (name, regno);
4746
4747       if (USE_HIDDEN_LINKONCE)
4748         {
4749           tree decl;
4750
4751           decl = build_decl (FUNCTION_DECL, get_identifier (name),
4752                              error_mark_node);
4753           TREE_PUBLIC (decl) = 1;
4754           TREE_STATIC (decl) = 1;
4755           DECL_ONE_ONLY (decl) = 1;
4756
4757           (*targetm.asm_out.unique_section) (decl, 0);
4758           named_section (decl, NULL, 0);
4759
4760           (*targetm.asm_out.globalize_label) (asm_out_file, name);
4761           fputs ("\t.hidden\t", asm_out_file);
4762           assemble_name (asm_out_file, name);
4763           fputc ('\n', asm_out_file);
4764           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
4765         }
4766       else
4767         {
4768           text_section ();
4769           ASM_OUTPUT_LABEL (asm_out_file, name);
4770         }
4771
4772       xops[0] = gen_rtx_REG (SImode, regno);
4773       xops[1] = gen_rtx_MEM (SImode, stack_pointer_rtx);
4774       output_asm_insn ("mov{l}\t{%1, %0|%0, %1}", xops);
4775       output_asm_insn ("ret", xops);
4776     }
4777
4778   if (NEED_INDICATE_EXEC_STACK)
4779     file_end_indicate_exec_stack ();
4780 }
4781
4782 /* Emit code for the SET_GOT patterns.  */
4783
4784 const char *
4785 output_set_got (rtx dest)
4786 {
4787   rtx xops[3];
4788
4789   xops[0] = dest;
4790   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
4791
4792   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
4793     {
4794       xops[2] = gen_rtx_LABEL_REF (Pmode, gen_label_rtx ());
4795
4796       if (!flag_pic)
4797         output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
4798       else
4799         output_asm_insn ("call\t%a2", xops);
4800
4801 #if TARGET_MACHO
4802       /* Output the "canonical" label name ("Lxx$pb") here too.  This
4803          is what will be referred to by the Mach-O PIC subsystem.  */
4804       ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
4805 #endif
4806       (*targetm.asm_out.internal_label) (asm_out_file, "L",
4807                                  CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
4808
4809       if (flag_pic)
4810         output_asm_insn ("pop{l}\t%0", xops);
4811     }
4812   else
4813     {
4814       char name[32];
4815       get_pc_thunk_name (name, REGNO (dest));
4816       pic_labels_used |= 1 << REGNO (dest);
4817
4818       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
4819       xops[2] = gen_rtx_MEM (QImode, xops[2]);
4820       output_asm_insn ("call\t%X2", xops);
4821     }
4822
4823   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
4824     output_asm_insn ("add{l}\t{%1, %0|%0, %1}", xops);
4825   else if (!TARGET_MACHO)
4826     output_asm_insn ("add{l}\t{%1+[.-%a2], %0|%0, %a1+(.-%a2)}", xops);
4827
4828   return "";
4829 }
4830
4831 /* Generate an "push" pattern for input ARG.  */
4832
4833 static rtx
4834 gen_push (rtx arg)
4835 {
4836   return gen_rtx_SET (VOIDmode,
4837                       gen_rtx_MEM (Pmode,
4838                                    gen_rtx_PRE_DEC (Pmode,
4839                                                     stack_pointer_rtx)),
4840                       arg);
4841 }
4842
4843 /* Return >= 0 if there is an unused call-clobbered register available
4844    for the entire function.  */
4845
4846 static unsigned int
4847 ix86_select_alt_pic_regnum (void)
4848 {
4849   if (current_function_is_leaf && !current_function_profile)
4850     {
4851       int i;
4852       for (i = 2; i >= 0; --i)
4853         if (!regs_ever_live[i])
4854           return i;
4855     }
4856
4857   return INVALID_REGNUM;
4858 }
4859
4860 /* Return 1 if we need to save REGNO.  */
4861 static int
4862 ix86_save_reg (unsigned int regno, int maybe_eh_return)
4863 {
4864   if (pic_offset_table_rtx
4865       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
4866       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
4867           || current_function_profile
4868           || current_function_calls_eh_return
4869           || current_function_uses_const_pool))
4870     {
4871       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
4872         return 0;
4873       return 1;
4874     }
4875
4876   if (current_function_calls_eh_return && maybe_eh_return)
4877     {
4878       unsigned i;
4879       for (i = 0; ; i++)
4880         {
4881           unsigned test = EH_RETURN_DATA_REGNO (i);
4882           if (test == INVALID_REGNUM)
4883             break;
4884           if (test == regno)
4885             return 1;
4886         }
4887     }
4888
4889   return (regs_ever_live[regno]
4890           && !call_used_regs[regno]
4891           && !fixed_regs[regno]
4892           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
4893 }
4894
4895 /* Return number of registers to be saved on the stack.  */
4896
4897 static int
4898 ix86_nsaved_regs (void)
4899 {
4900   int nregs = 0;
4901   int regno;
4902
4903   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
4904     if (ix86_save_reg (regno, true))
4905       nregs++;
4906   return nregs;
4907 }
4908
4909 /* Return the offset between two registers, one to be eliminated, and the other
4910    its replacement, at the start of a routine.  */
4911
4912 HOST_WIDE_INT
4913 ix86_initial_elimination_offset (int from, int to)
4914 {
4915   struct ix86_frame frame;
4916   ix86_compute_frame_layout (&frame);
4917
4918   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
4919     return frame.hard_frame_pointer_offset;
4920   else if (from == FRAME_POINTER_REGNUM
4921            && to == HARD_FRAME_POINTER_REGNUM)
4922     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
4923   else
4924     {
4925       if (to != STACK_POINTER_REGNUM)
4926         abort ();
4927       else if (from == ARG_POINTER_REGNUM)
4928         return frame.stack_pointer_offset;
4929       else if (from != FRAME_POINTER_REGNUM)
4930         abort ();
4931       else
4932         return frame.stack_pointer_offset - frame.frame_pointer_offset;
4933     }
4934 }
4935
4936 /* Fill structure ix86_frame about frame of currently computed function.  */
4937
4938 static void
4939 ix86_compute_frame_layout (struct ix86_frame *frame)
4940 {
4941   HOST_WIDE_INT total_size;
4942   int stack_alignment_needed = cfun->stack_alignment_needed / BITS_PER_UNIT;
4943   HOST_WIDE_INT offset;
4944   int preferred_alignment = cfun->preferred_stack_boundary / BITS_PER_UNIT;
4945   HOST_WIDE_INT size = get_frame_size ();
4946
4947   frame->nregs = ix86_nsaved_regs ();
4948   total_size = size;
4949
4950   /* During reload iteration the amount of registers saved can change.
4951      Recompute the value as needed.  Do not recompute when amount of registers
4952      didn't change as reload does mutiple calls to the function and does not
4953      expect the decision to change within single iteration.  */
4954   if (!optimize_size
4955       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
4956     {
4957       int count = frame->nregs;
4958
4959       cfun->machine->use_fast_prologue_epilogue_nregs = count;
4960       /* The fast prologue uses move instead of push to save registers.  This
4961          is significantly longer, but also executes faster as modern hardware
4962          can execute the moves in parallel, but can't do that for push/pop.
4963
4964          Be careful about choosing what prologue to emit:  When function takes
4965          many instructions to execute we may use slow version as well as in
4966          case function is known to be outside hot spot (this is known with
4967          feedback only).  Weight the size of function by number of registers
4968          to save as it is cheap to use one or two push instructions but very
4969          slow to use many of them.  */
4970       if (count)
4971         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
4972       if (cfun->function_frequency < FUNCTION_FREQUENCY_NORMAL
4973           || (flag_branch_probabilities
4974               && cfun->function_frequency < FUNCTION_FREQUENCY_HOT))
4975         cfun->machine->use_fast_prologue_epilogue = false;
4976       else
4977         cfun->machine->use_fast_prologue_epilogue
4978            = !expensive_function_p (count);
4979     }
4980   if (TARGET_PROLOGUE_USING_MOVE
4981       && cfun->machine->use_fast_prologue_epilogue)
4982     frame->save_regs_using_mov = true;
4983   else
4984     frame->save_regs_using_mov = false;
4985
4986
4987   /* Skip return address and saved base pointer.  */
4988   offset = frame_pointer_needed ? UNITS_PER_WORD * 2 : UNITS_PER_WORD;
4989
4990   frame->hard_frame_pointer_offset = offset;
4991
4992   /* Do some sanity checking of stack_alignment_needed and
4993      preferred_alignment, since i386 port is the only using those features
4994      that may break easily.  */
4995
4996   if (size && !stack_alignment_needed)
4997     abort ();
4998   if (preferred_alignment < STACK_BOUNDARY / BITS_PER_UNIT)
4999     abort ();
5000   if (preferred_alignment > PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
5001     abort ();
5002   if (stack_alignment_needed > PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
5003     abort ();
5004
5005   if (stack_alignment_needed < STACK_BOUNDARY / BITS_PER_UNIT)
5006     stack_alignment_needed = STACK_BOUNDARY / BITS_PER_UNIT;
5007
5008   /* Register save area */
5009   offset += frame->nregs * UNITS_PER_WORD;
5010
5011   /* Va-arg area */
5012   if (ix86_save_varrargs_registers)
5013     {
5014       offset += X86_64_VARARGS_SIZE;
5015       frame->va_arg_size = X86_64_VARARGS_SIZE;
5016     }
5017   else
5018     frame->va_arg_size = 0;
5019
5020   /* Align start of frame for local function.  */
5021   frame->padding1 = ((offset + stack_alignment_needed - 1)
5022                      & -stack_alignment_needed) - offset;
5023
5024   offset += frame->padding1;
5025
5026   /* Frame pointer points here.  */
5027   frame->frame_pointer_offset = offset;
5028
5029   offset += size;
5030
5031   /* Add outgoing arguments area.  Can be skipped if we eliminated
5032      all the function calls as dead code.
5033      Skipping is however impossible when function calls alloca.  Alloca
5034      expander assumes that last current_function_outgoing_args_size
5035      of stack frame are unused.  */
5036   if (ACCUMULATE_OUTGOING_ARGS
5037       && (!current_function_is_leaf || current_function_calls_alloca))
5038     {
5039       offset += current_function_outgoing_args_size;
5040       frame->outgoing_arguments_size = current_function_outgoing_args_size;
5041     }
5042   else
5043     frame->outgoing_arguments_size = 0;
5044
5045   /* Align stack boundary.  Only needed if we're calling another function
5046      or using alloca.  */
5047   if (!current_function_is_leaf || current_function_calls_alloca)
5048     frame->padding2 = ((offset + preferred_alignment - 1)
5049                        & -preferred_alignment) - offset;
5050   else
5051     frame->padding2 = 0;
5052
5053   offset += frame->padding2;
5054
5055   /* We've reached end of stack frame.  */
5056   frame->stack_pointer_offset = offset;
5057
5058   /* Size prologue needs to allocate.  */
5059   frame->to_allocate =
5060     (size + frame->padding1 + frame->padding2
5061      + frame->outgoing_arguments_size + frame->va_arg_size);
5062
5063   if ((!frame->to_allocate && frame->nregs <= 1)
5064       || (TARGET_64BIT && frame->to_allocate >= (HOST_WIDE_INT) 0x80000000))
5065     frame->save_regs_using_mov = false;
5066
5067   if (TARGET_RED_ZONE && current_function_sp_is_unchanging
5068       && current_function_is_leaf)
5069     {
5070       frame->red_zone_size = frame->to_allocate;
5071       if (frame->save_regs_using_mov)
5072         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
5073       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
5074         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
5075     }
5076   else
5077     frame->red_zone_size = 0;
5078   frame->to_allocate -= frame->red_zone_size;
5079   frame->stack_pointer_offset -= frame->red_zone_size;
5080 #if 0
5081   fprintf (stderr, "nregs: %i\n", frame->nregs);
5082   fprintf (stderr, "size: %i\n", size);
5083   fprintf (stderr, "alignment1: %i\n", stack_alignment_needed);
5084   fprintf (stderr, "padding1: %i\n", frame->padding1);
5085   fprintf (stderr, "va_arg: %i\n", frame->va_arg_size);
5086   fprintf (stderr, "padding2: %i\n", frame->padding2);
5087   fprintf (stderr, "to_allocate: %i\n", frame->to_allocate);
5088   fprintf (stderr, "red_zone_size: %i\n", frame->red_zone_size);
5089   fprintf (stderr, "frame_pointer_offset: %i\n", frame->frame_pointer_offset);
5090   fprintf (stderr, "hard_frame_pointer_offset: %i\n",
5091            frame->hard_frame_pointer_offset);
5092   fprintf (stderr, "stack_pointer_offset: %i\n", frame->stack_pointer_offset);
5093 #endif
5094 }
5095
5096 /* Emit code to save registers in the prologue.  */
5097
5098 static void
5099 ix86_emit_save_regs (void)
5100 {
5101   int regno;
5102   rtx insn;
5103
5104   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
5105     if (ix86_save_reg (regno, true))
5106       {
5107         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
5108         RTX_FRAME_RELATED_P (insn) = 1;
5109       }
5110 }
5111
5112 /* Emit code to save registers using MOV insns.  First register
5113    is restored from POINTER + OFFSET.  */
5114 static void
5115 ix86_emit_save_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
5116 {
5117   int regno;
5118   rtx insn;
5119
5120   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5121     if (ix86_save_reg (regno, true))
5122       {
5123         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
5124                                                Pmode, offset),
5125                                gen_rtx_REG (Pmode, regno));
5126         RTX_FRAME_RELATED_P (insn) = 1;
5127         offset += UNITS_PER_WORD;
5128       }
5129 }
5130
5131 /* Expand prologue or epilogue stack adjustment.
5132    The pattern exist to put a dependency on all ebp-based memory accesses.
5133    STYLE should be negative if instructions should be marked as frame related,
5134    zero if %r11 register is live and cannot be freely used and positive
5135    otherwise.  */
5136
5137 static void
5138 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset, int style)
5139 {
5140   rtx insn;
5141
5142   if (! TARGET_64BIT)
5143     insn = emit_insn (gen_pro_epilogue_adjust_stack_1 (dest, src, offset));
5144   else if (x86_64_immediate_operand (offset, DImode))
5145     insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64 (dest, src, offset));
5146   else
5147     {
5148       rtx r11;
5149       /* r11 is used by indirect sibcall return as well, set before the
5150          epilogue and used after the epilogue.  ATM indirect sibcall
5151          shouldn't be used together with huge frame sizes in one
5152          function because of the frame_size check in sibcall.c.  */
5153       if (style == 0)
5154         abort ();
5155       r11 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
5156       insn = emit_insn (gen_rtx_SET (DImode, r11, offset));
5157       if (style < 0)
5158         RTX_FRAME_RELATED_P (insn) = 1;
5159       insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64_2 (dest, src, r11,
5160                                                                offset));
5161     }
5162   if (style < 0)
5163     RTX_FRAME_RELATED_P (insn) = 1;
5164 }
5165
5166 /* Expand the prologue into a bunch of separate insns.  */
5167
5168 void
5169 ix86_expand_prologue (void)
5170 {
5171   rtx insn;
5172   bool pic_reg_used;
5173   struct ix86_frame frame;
5174   HOST_WIDE_INT allocate;
5175
5176   ix86_compute_frame_layout (&frame);
5177
5178   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
5179      slower on all targets.  Also sdb doesn't like it.  */
5180
5181   if (frame_pointer_needed)
5182     {
5183       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
5184       RTX_FRAME_RELATED_P (insn) = 1;
5185
5186       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
5187       RTX_FRAME_RELATED_P (insn) = 1;
5188     }
5189
5190   allocate = frame.to_allocate;
5191
5192   if (!frame.save_regs_using_mov)
5193     ix86_emit_save_regs ();
5194   else
5195     allocate += frame.nregs * UNITS_PER_WORD;
5196
5197   /* When using red zone we may start register saving before allocating
5198      the stack frame saving one cycle of the prologue.  */
5199   if (TARGET_RED_ZONE && frame.save_regs_using_mov)
5200     ix86_emit_save_regs_using_mov (frame_pointer_needed ? hard_frame_pointer_rtx
5201                                    : stack_pointer_rtx,
5202                                    -frame.nregs * UNITS_PER_WORD);
5203
5204   if (allocate == 0)
5205     ;
5206   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
5207     pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5208                                GEN_INT (-allocate), -1);
5209   else
5210     {
5211       /* Only valid for Win32.  */
5212       rtx eax = gen_rtx_REG (SImode, 0);
5213       bool eax_live = ix86_eax_live_at_start_p ();
5214
5215       if (TARGET_64BIT)
5216         abort ();
5217
5218       if (eax_live)
5219         {
5220           emit_insn (gen_push (eax));
5221           allocate -= 4;
5222         }
5223
5224       insn = emit_move_insn (eax, GEN_INT (allocate));
5225       RTX_FRAME_RELATED_P (insn) = 1;
5226
5227       insn = emit_insn (gen_allocate_stack_worker (eax));
5228       RTX_FRAME_RELATED_P (insn) = 1;
5229
5230       if (eax_live)
5231         {
5232           rtx t = plus_constant (stack_pointer_rtx, allocate);
5233           emit_move_insn (eax, gen_rtx_MEM (SImode, t));
5234         }
5235     }
5236
5237   if (frame.save_regs_using_mov && !TARGET_RED_ZONE)
5238     {
5239       if (!frame_pointer_needed || !frame.to_allocate)
5240         ix86_emit_save_regs_using_mov (stack_pointer_rtx, frame.to_allocate);
5241       else
5242         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
5243                                        -frame.nregs * UNITS_PER_WORD);
5244     }
5245
5246   pic_reg_used = false;
5247   if (pic_offset_table_rtx
5248       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
5249           || current_function_profile))
5250     {
5251       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
5252
5253       if (alt_pic_reg_used != INVALID_REGNUM)
5254         REGNO (pic_offset_table_rtx) = alt_pic_reg_used;
5255
5256       pic_reg_used = true;
5257     }
5258
5259   if (pic_reg_used)
5260     {
5261       insn = emit_insn (gen_set_got (pic_offset_table_rtx));
5262
5263       /* Even with accurate pre-reload life analysis, we can wind up
5264          deleting all references to the pic register after reload.
5265          Consider if cross-jumping unifies two sides of a branch
5266          controlled by a comparison vs the only read from a global.
5267          In which case, allow the set_got to be deleted, though we're
5268          too late to do anything about the ebx save in the prologue.  */
5269       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5270     }
5271
5272   /* Prevent function calls from be scheduled before the call to mcount.
5273      In the pic_reg_used case, make sure that the got load isn't deleted.  */
5274   if (current_function_profile)
5275     emit_insn (gen_blockage (pic_reg_used ? pic_offset_table_rtx : const0_rtx));
5276 }
5277
5278 /* Emit code to restore saved registers using MOV insns.  First register
5279    is restored from POINTER + OFFSET.  */
5280 static void
5281 ix86_emit_restore_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
5282                                   int maybe_eh_return)
5283 {
5284   int regno;
5285   rtx base_address = gen_rtx_MEM (Pmode, pointer);
5286
5287   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5288     if (ix86_save_reg (regno, maybe_eh_return))
5289       {
5290         /* Ensure that adjust_address won't be forced to produce pointer
5291            out of range allowed by x86-64 instruction set.  */
5292         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
5293           {
5294             rtx r11;
5295
5296             r11 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
5297             emit_move_insn (r11, GEN_INT (offset));
5298             emit_insn (gen_adddi3 (r11, r11, pointer));
5299             base_address = gen_rtx_MEM (Pmode, r11);
5300             offset = 0;
5301           }
5302         emit_move_insn (gen_rtx_REG (Pmode, regno),
5303                         adjust_address (base_address, Pmode, offset));
5304         offset += UNITS_PER_WORD;
5305       }
5306 }
5307
5308 /* Restore function stack, frame, and registers.  */
5309
5310 void
5311 ix86_expand_epilogue (int style)
5312 {
5313   int regno;
5314   int sp_valid = !frame_pointer_needed || current_function_sp_is_unchanging;
5315   struct ix86_frame frame;
5316   HOST_WIDE_INT offset;
5317
5318   ix86_compute_frame_layout (&frame);
5319
5320   /* Calculate start of saved registers relative to ebp.  Special care
5321      must be taken for the normal return case of a function using
5322      eh_return: the eax and edx registers are marked as saved, but not
5323      restored along this path.  */
5324   offset = frame.nregs;
5325   if (current_function_calls_eh_return && style != 2)
5326     offset -= 2;
5327   offset *= -UNITS_PER_WORD;
5328
5329   /* If we're only restoring one register and sp is not valid then
5330      using a move instruction to restore the register since it's
5331      less work than reloading sp and popping the register.
5332
5333      The default code result in stack adjustment using add/lea instruction,
5334      while this code results in LEAVE instruction (or discrete equivalent),
5335      so it is profitable in some other cases as well.  Especially when there
5336      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
5337      and there is exactly one register to pop. This heuristic may need some
5338      tuning in future.  */
5339   if ((!sp_valid && frame.nregs <= 1)
5340       || (TARGET_EPILOGUE_USING_MOVE
5341           && cfun->machine->use_fast_prologue_epilogue
5342           && (frame.nregs > 1 || frame.to_allocate))
5343       || (frame_pointer_needed && !frame.nregs && frame.to_allocate)
5344       || (frame_pointer_needed && TARGET_USE_LEAVE
5345           && cfun->machine->use_fast_prologue_epilogue
5346           && frame.nregs == 1)
5347       || current_function_calls_eh_return)
5348     {
5349       /* Restore registers.  We can use ebp or esp to address the memory
5350          locations.  If both are available, default to ebp, since offsets
5351          are known to be small.  Only exception is esp pointing directly to the
5352          end of block of saved registers, where we may simplify addressing
5353          mode.  */
5354
5355       if (!frame_pointer_needed || (sp_valid && !frame.to_allocate))
5356         ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
5357                                           frame.to_allocate, style == 2);
5358       else
5359         ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
5360                                           offset, style == 2);
5361
5362       /* eh_return epilogues need %ecx added to the stack pointer.  */
5363       if (style == 2)
5364         {
5365           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
5366
5367           if (frame_pointer_needed)
5368             {
5369               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
5370               tmp = plus_constant (tmp, UNITS_PER_WORD);
5371               emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
5372
5373               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
5374               emit_move_insn (hard_frame_pointer_rtx, tmp);
5375
5376               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
5377                                          const0_rtx, style);
5378             }
5379           else
5380             {
5381               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
5382               tmp = plus_constant (tmp, (frame.to_allocate
5383                                          + frame.nregs * UNITS_PER_WORD));
5384               emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
5385             }
5386         }
5387       else if (!frame_pointer_needed)
5388         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5389                                    GEN_INT (frame.to_allocate
5390                                             + frame.nregs * UNITS_PER_WORD),
5391                                    style);
5392       /* If not an i386, mov & pop is faster than "leave".  */
5393       else if (TARGET_USE_LEAVE || optimize_size
5394                || !cfun->machine->use_fast_prologue_epilogue)
5395         emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5396       else
5397         {
5398           pro_epilogue_adjust_stack (stack_pointer_rtx,
5399                                      hard_frame_pointer_rtx,
5400                                      const0_rtx, style);
5401           if (TARGET_64BIT)
5402             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5403           else
5404             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5405         }
5406     }
5407   else
5408     {
5409       /* First step is to deallocate the stack frame so that we can
5410          pop the registers.  */
5411       if (!sp_valid)
5412         {
5413           if (!frame_pointer_needed)
5414             abort ();
5415           pro_epilogue_adjust_stack (stack_pointer_rtx,
5416                                      hard_frame_pointer_rtx,
5417                                      GEN_INT (offset), style);
5418         }
5419       else if (frame.to_allocate)
5420         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5421                                    GEN_INT (frame.to_allocate), style);
5422
5423       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5424         if (ix86_save_reg (regno, false))
5425           {
5426             if (TARGET_64BIT)
5427               emit_insn (gen_popdi1 (gen_rtx_REG (Pmode, regno)));
5428             else
5429               emit_insn (gen_popsi1 (gen_rtx_REG (Pmode, regno)));
5430           }
5431       if (frame_pointer_needed)
5432         {
5433           /* Leave results in shorter dependency chains on CPUs that are
5434              able to grok it fast.  */
5435           if (TARGET_USE_LEAVE)
5436             emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5437           else if (TARGET_64BIT)
5438             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5439           else
5440             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5441         }
5442     }
5443
5444   /* Sibcall epilogues don't want a return instruction.  */
5445   if (style == 0)
5446     return;
5447
5448   if (current_function_pops_args && current_function_args_size)
5449     {
5450       rtx popc = GEN_INT (current_function_pops_args);
5451
5452       /* i386 can only pop 64K bytes.  If asked to pop more, pop
5453          return address, do explicit add, and jump indirectly to the
5454          caller.  */
5455
5456       if (current_function_pops_args >= 65536)
5457         {
5458           rtx ecx = gen_rtx_REG (SImode, 2);
5459
5460           /* There is no "pascal" calling convention in 64bit ABI.  */
5461           if (TARGET_64BIT)
5462             abort ();
5463
5464           emit_insn (gen_popsi1 (ecx));
5465           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, popc));
5466           emit_jump_insn (gen_return_indirect_internal (ecx));
5467         }
5468       else
5469         emit_jump_insn (gen_return_pop_internal (popc));
5470     }
5471   else
5472     emit_jump_insn (gen_return_internal ());
5473 }
5474
5475 /* Reset from the function's potential modifications.  */
5476
5477 static void
5478 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
5479                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
5480 {
5481   if (pic_offset_table_rtx)
5482     REGNO (pic_offset_table_rtx) = REAL_PIC_OFFSET_TABLE_REGNUM;
5483 }
5484 \f
5485 /* Extract the parts of an RTL expression that is a valid memory address
5486    for an instruction.  Return 0 if the structure of the address is
5487    grossly off.  Return -1 if the address contains ASHIFT, so it is not
5488    strictly valid, but still used for computing length of lea instruction.  */
5489
5490 static int
5491 ix86_decompose_address (rtx addr, struct ix86_address *out)
5492 {
5493   rtx base = NULL_RTX;
5494   rtx index = NULL_RTX;
5495   rtx disp = NULL_RTX;
5496   HOST_WIDE_INT scale = 1;
5497   rtx scale_rtx = NULL_RTX;
5498   int retval = 1;
5499   enum ix86_address_seg seg = SEG_DEFAULT;
5500
5501   if (GET_CODE (addr) == REG || GET_CODE (addr) == SUBREG)
5502     base = addr;
5503   else if (GET_CODE (addr) == PLUS)
5504     {
5505       rtx addends[4], op;
5506       int n = 0, i;
5507
5508       op = addr;
5509       do
5510         {
5511           if (n >= 4)
5512             return 0;
5513           addends[n++] = XEXP (op, 1);
5514           op = XEXP (op, 0);
5515         }
5516       while (GET_CODE (op) == PLUS);
5517       if (n >= 4)
5518         return 0;
5519       addends[n] = op;
5520
5521       for (i = n; i >= 0; --i)
5522         {
5523           op = addends[i];
5524           switch (GET_CODE (op))
5525             {
5526             case MULT:
5527               if (index)
5528                 return 0;
5529               index = XEXP (op, 0);
5530               scale_rtx = XEXP (op, 1);
5531               break;
5532
5533             case UNSPEC:
5534               if (XINT (op, 1) == UNSPEC_TP
5535                   && TARGET_TLS_DIRECT_SEG_REFS
5536                   && seg == SEG_DEFAULT)
5537                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
5538               else
5539                 return 0;
5540               break;
5541
5542             case REG:
5543             case SUBREG:
5544               if (!base)
5545                 base = op;
5546               else if (!index)
5547                 index = op;
5548               else
5549                 return 0;
5550               break;
5551
5552             case CONST:
5553             case CONST_INT:
5554             case SYMBOL_REF:
5555             case LABEL_REF:
5556               if (disp)
5557                 return 0;
5558               disp = op;
5559               break;
5560
5561             default:
5562               return 0;
5563             }
5564         }
5565     }
5566   else if (GET_CODE (addr) == MULT)
5567     {
5568       index = XEXP (addr, 0);           /* index*scale */
5569       scale_rtx = XEXP (addr, 1);
5570     }
5571   else if (GET_CODE (addr) == ASHIFT)
5572     {
5573       rtx tmp;
5574
5575       /* We're called for lea too, which implements ashift on occasion.  */
5576       index = XEXP (addr, 0);
5577       tmp = XEXP (addr, 1);
5578       if (GET_CODE (tmp) != CONST_INT)
5579         return 0;
5580       scale = INTVAL (tmp);
5581       if ((unsigned HOST_WIDE_INT) scale > 3)
5582         return 0;
5583       scale = 1 << scale;
5584       retval = -1;
5585     }
5586   else
5587     disp = addr;                        /* displacement */
5588
5589   /* Extract the integral value of scale.  */
5590   if (scale_rtx)
5591     {
5592       if (GET_CODE (scale_rtx) != CONST_INT)
5593         return 0;
5594       scale = INTVAL (scale_rtx);
5595     }
5596
5597   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
5598   if (base && index && scale == 1
5599       && (index == arg_pointer_rtx
5600           || index == frame_pointer_rtx
5601           || (REG_P (index) && REGNO (index) == STACK_POINTER_REGNUM)))
5602     {
5603       rtx tmp = base;
5604       base = index;
5605       index = tmp;
5606     }
5607
5608   /* Special case: %ebp cannot be encoded as a base without a displacement.  */
5609   if ((base == hard_frame_pointer_rtx
5610        || base == frame_pointer_rtx
5611        || base == arg_pointer_rtx) && !disp)
5612     disp = const0_rtx;
5613
5614   /* Special case: on K6, [%esi] makes the instruction vector decoded.
5615      Avoid this by transforming to [%esi+0].  */
5616   if (ix86_tune == PROCESSOR_K6 && !optimize_size
5617       && base && !index && !disp
5618       && REG_P (base)
5619       && REGNO_REG_CLASS (REGNO (base)) == SIREG)
5620     disp = const0_rtx;
5621
5622   /* Special case: encode reg+reg instead of reg*2.  */
5623   if (!base && index && scale && scale == 2)
5624     base = index, scale = 1;
5625
5626   /* Special case: scaling cannot be encoded without base or displacement.  */
5627   if (!base && !disp && index && scale != 1)
5628     disp = const0_rtx;
5629
5630   out->base = base;
5631   out->index = index;
5632   out->disp = disp;
5633   out->scale = scale;
5634   out->seg = seg;
5635
5636   return retval;
5637 }
5638 \f
5639 /* Return cost of the memory address x.
5640    For i386, it is better to use a complex address than let gcc copy
5641    the address into a reg and make a new pseudo.  But not if the address
5642    requires to two regs - that would mean more pseudos with longer
5643    lifetimes.  */
5644 static int
5645 ix86_address_cost (rtx x)
5646 {
5647   struct ix86_address parts;
5648   int cost = 1;
5649
5650   if (!ix86_decompose_address (x, &parts))
5651     abort ();
5652
5653   /* More complex memory references are better.  */
5654   if (parts.disp && parts.disp != const0_rtx)
5655     cost--;
5656   if (parts.seg != SEG_DEFAULT)
5657     cost--;
5658
5659   /* Attempt to minimize number of registers in the address.  */
5660   if ((parts.base
5661        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
5662       || (parts.index
5663           && (!REG_P (parts.index)
5664               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
5665     cost++;
5666
5667   if (parts.base
5668       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
5669       && parts.index
5670       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
5671       && parts.base != parts.index)
5672     cost++;
5673
5674   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
5675      since it's predecode logic can't detect the length of instructions
5676      and it degenerates to vector decoded.  Increase cost of such
5677      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
5678      to split such addresses or even refuse such addresses at all.
5679
5680      Following addressing modes are affected:
5681       [base+scale*index]
5682       [scale*index+disp]
5683       [base+index]
5684
5685      The first and last case  may be avoidable by explicitly coding the zero in
5686      memory address, but I don't have AMD-K6 machine handy to check this
5687      theory.  */
5688
5689   if (TARGET_K6
5690       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
5691           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
5692           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
5693     cost += 10;
5694
5695   return cost;
5696 }
5697 \f
5698 /* If X is a machine specific address (i.e. a symbol or label being
5699    referenced as a displacement from the GOT implemented using an
5700    UNSPEC), then return the base term.  Otherwise return X.  */
5701
5702 rtx
5703 ix86_find_base_term (rtx x)
5704 {
5705   rtx term;
5706
5707   if (TARGET_64BIT)
5708     {
5709       if (GET_CODE (x) != CONST)
5710         return x;
5711       term = XEXP (x, 0);
5712       if (GET_CODE (term) == PLUS
5713           && (GET_CODE (XEXP (term, 1)) == CONST_INT
5714               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
5715         term = XEXP (term, 0);
5716       if (GET_CODE (term) != UNSPEC
5717           || XINT (term, 1) != UNSPEC_GOTPCREL)
5718         return x;
5719
5720       term = XVECEXP (term, 0, 0);
5721
5722       if (GET_CODE (term) != SYMBOL_REF
5723           && GET_CODE (term) != LABEL_REF)
5724         return x;
5725
5726       return term;
5727     }
5728
5729   term = ix86_delegitimize_address (x);
5730
5731   if (GET_CODE (term) != SYMBOL_REF
5732       && GET_CODE (term) != LABEL_REF)
5733     return x;
5734
5735   return term;
5736 }
5737 \f
5738 /* Determine if a given RTX is a valid constant.  We already know this
5739    satisfies CONSTANT_P.  */
5740
5741 bool
5742 legitimate_constant_p (rtx x)
5743 {
5744   rtx inner;
5745
5746   switch (GET_CODE (x))
5747     {
5748     case SYMBOL_REF:
5749       /* TLS symbols are not constant.  */
5750       if (tls_symbolic_operand (x, Pmode))
5751         return false;
5752       break;
5753
5754     case CONST:
5755       inner = XEXP (x, 0);
5756
5757       /* Offsets of TLS symbols are never valid.
5758          Discourage CSE from creating them.  */
5759       if (GET_CODE (inner) == PLUS
5760           && tls_symbolic_operand (XEXP (inner, 0), Pmode))
5761         return false;
5762
5763       if (GET_CODE (inner) == PLUS)
5764         {
5765           if (GET_CODE (XEXP (inner, 1)) != CONST_INT)
5766             return false;
5767           inner = XEXP (inner, 0);
5768         }
5769
5770       /* Only some unspecs are valid as "constants".  */
5771       if (GET_CODE (inner) == UNSPEC)
5772         switch (XINT (inner, 1))
5773           {
5774           case UNSPEC_TPOFF:
5775           case UNSPEC_NTPOFF:
5776             return local_exec_symbolic_operand (XVECEXP (inner, 0, 0), Pmode);
5777           case UNSPEC_DTPOFF:
5778             return local_dynamic_symbolic_operand (XVECEXP (inner, 0, 0), Pmode);
5779           default:
5780             return false;
5781           }
5782       break;
5783
5784     default:
5785       break;
5786     }
5787
5788   /* Otherwise we handle everything else in the move patterns.  */
5789   return true;
5790 }
5791
5792 /* Determine if it's legal to put X into the constant pool.  This
5793    is not possible for the address of thread-local symbols, which
5794    is checked above.  */
5795
5796 static bool
5797 ix86_cannot_force_const_mem (rtx x)
5798 {
5799   return !legitimate_constant_p (x);
5800 }
5801
5802 /* Determine if a given RTX is a valid constant address.  */
5803
5804 bool
5805 constant_address_p (rtx x)
5806 {
5807   return CONSTANT_P (x) && legitimate_address_p (Pmode, x, 1);
5808 }
5809
5810 /* Nonzero if the constant value X is a legitimate general operand
5811    when generating PIC code.  It is given that flag_pic is on and
5812    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
5813
5814 bool
5815 legitimate_pic_operand_p (rtx x)
5816 {
5817   rtx inner;
5818
5819   switch (GET_CODE (x))
5820     {
5821     case CONST:
5822       inner = XEXP (x, 0);
5823
5824       /* Only some unspecs are valid as "constants".  */
5825       if (GET_CODE (inner) == UNSPEC)
5826         switch (XINT (inner, 1))
5827           {
5828           case UNSPEC_TPOFF:
5829             return local_exec_symbolic_operand (XVECEXP (inner, 0, 0), Pmode);
5830           default:
5831             return false;
5832           }
5833       /* FALLTHRU */
5834
5835     case SYMBOL_REF:
5836     case LABEL_REF:
5837       return legitimate_pic_address_disp_p (x);
5838
5839     default:
5840       return true;
5841     }
5842 }
5843
5844 /* Determine if a given CONST RTX is a valid memory displacement
5845    in PIC mode.  */
5846
5847 int
5848 legitimate_pic_address_disp_p (rtx disp)
5849 {
5850   bool saw_plus;
5851
5852   /* In 64bit mode we can allow direct addresses of symbols and labels
5853      when they are not dynamic symbols.  */
5854   if (TARGET_64BIT)
5855     {
5856       /* TLS references should always be enclosed in UNSPEC.  */
5857       if (tls_symbolic_operand (disp, GET_MODE (disp)))
5858         return 0;
5859       if (GET_CODE (disp) == SYMBOL_REF
5860           && ix86_cmodel == CM_SMALL_PIC
5861           && SYMBOL_REF_LOCAL_P (disp))
5862         return 1;
5863       if (GET_CODE (disp) == LABEL_REF)
5864         return 1;
5865       if (GET_CODE (disp) == CONST
5866           && GET_CODE (XEXP (disp, 0)) == PLUS)
5867         {
5868           rtx op0 = XEXP (XEXP (disp, 0), 0);
5869           rtx op1 = XEXP (XEXP (disp, 0), 1);
5870
5871           /* TLS references should always be enclosed in UNSPEC.  */
5872           if (tls_symbolic_operand (op0, GET_MODE (op0)))
5873             return 0;
5874           if (((GET_CODE (op0) == SYMBOL_REF
5875                 && ix86_cmodel == CM_SMALL_PIC
5876                 && SYMBOL_REF_LOCAL_P (op0))
5877                || GET_CODE (op0) == LABEL_REF)
5878               && GET_CODE (op1) == CONST_INT
5879               && INTVAL (op1) < 16*1024*1024
5880               && INTVAL (op1) >= -16*1024*1024)
5881             return 1;
5882         }
5883     }
5884   if (GET_CODE (disp) != CONST)
5885     return 0;
5886   disp = XEXP (disp, 0);
5887
5888   if (TARGET_64BIT)
5889     {
5890       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
5891          of GOT tables.  We should not need these anyway.  */
5892       if (GET_CODE (disp) != UNSPEC
5893           || XINT (disp, 1) != UNSPEC_GOTPCREL)
5894         return 0;
5895
5896       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
5897           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
5898         return 0;
5899       return 1;
5900     }
5901
5902   saw_plus = false;
5903   if (GET_CODE (disp) == PLUS)
5904     {
5905       if (GET_CODE (XEXP (disp, 1)) != CONST_INT)
5906         return 0;
5907       disp = XEXP (disp, 0);
5908       saw_plus = true;
5909     }
5910
5911   /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O.  */
5912   if (TARGET_MACHO && GET_CODE (disp) == MINUS)
5913     {
5914       if (GET_CODE (XEXP (disp, 0)) == LABEL_REF
5915           || GET_CODE (XEXP (disp, 0)) == SYMBOL_REF)
5916         if (GET_CODE (XEXP (disp, 1)) == SYMBOL_REF)
5917           {
5918             const char *sym_name = XSTR (XEXP (disp, 1), 0);
5919             if (! strcmp (sym_name, "<pic base>"))
5920               return 1;
5921           }
5922     }
5923
5924   if (GET_CODE (disp) != UNSPEC)
5925     return 0;
5926
5927   switch (XINT (disp, 1))
5928     {
5929     case UNSPEC_GOT:
5930       if (saw_plus)
5931         return false;
5932       return GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF;
5933     case UNSPEC_GOTOFF:
5934       if (GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
5935           || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
5936         return local_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5937       return false;
5938     case UNSPEC_GOTTPOFF:
5939     case UNSPEC_GOTNTPOFF:
5940     case UNSPEC_INDNTPOFF:
5941       if (saw_plus)
5942         return false;
5943       return initial_exec_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5944     case UNSPEC_NTPOFF:
5945       return local_exec_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5946     case UNSPEC_DTPOFF:
5947       return local_dynamic_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5948     }
5949
5950   return 0;
5951 }
5952
5953 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression that is a valid
5954    memory address for an instruction.  The MODE argument is the machine mode
5955    for the MEM expression that wants to use this address.
5956
5957    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
5958    convert common non-canonical forms to canonical form so that they will
5959    be recognized.  */
5960
5961 int
5962 legitimate_address_p (enum machine_mode mode, rtx addr, int strict)
5963 {
5964   struct ix86_address parts;
5965   rtx base, index, disp;
5966   HOST_WIDE_INT scale;
5967   const char *reason = NULL;
5968   rtx reason_rtx = NULL_RTX;
5969
5970   if (TARGET_DEBUG_ADDR)
5971     {
5972       fprintf (stderr,
5973                "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
5974                GET_MODE_NAME (mode), strict);
5975       debug_rtx (addr);
5976     }
5977
5978   if (ix86_decompose_address (addr, &parts) <= 0)
5979     {
5980       reason = "decomposition failed";
5981       goto report_error;
5982     }
5983
5984   base = parts.base;
5985   index = parts.index;
5986   disp = parts.disp;
5987   scale = parts.scale;
5988
5989   /* Validate base register.
5990
5991      Don't allow SUBREG's here, it can lead to spill failures when the base
5992      is one word out of a two word structure, which is represented internally
5993      as a DImode int.  */
5994
5995   if (base)
5996     {
5997       reason_rtx = base;
5998
5999       if (GET_CODE (base) != REG)
6000         {
6001           reason = "base is not a register";
6002           goto report_error;
6003         }
6004
6005       if (GET_MODE (base) != Pmode)
6006         {
6007           reason = "base is not in Pmode";
6008           goto report_error;
6009         }
6010
6011       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (base))
6012           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (base)))
6013         {
6014           reason = "base is not valid";
6015           goto report_error;
6016         }
6017     }
6018
6019   /* Validate index register.
6020
6021      Don't allow SUBREG's here, it can lead to spill failures when the index
6022      is one word out of a two word structure, which is represented internally
6023      as a DImode int.  */
6024
6025   if (index)
6026     {
6027       reason_rtx = index;
6028
6029       if (GET_CODE (index) != REG)
6030         {
6031           reason = "index is not a register";
6032           goto report_error;
6033         }
6034
6035       if (GET_MODE (index) != Pmode)
6036         {
6037           reason = "index is not in Pmode";
6038           goto report_error;
6039         }
6040
6041       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (index))
6042           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (index)))
6043         {
6044           reason = "index is not valid";
6045           goto report_error;
6046         }
6047     }
6048
6049   /* Validate scale factor.  */
6050   if (scale != 1)
6051     {
6052       reason_rtx = GEN_INT (scale);
6053       if (!index)
6054         {
6055           reason = "scale without index";
6056           goto report_error;
6057         }
6058
6059       if (scale != 2 && scale != 4 && scale != 8)
6060         {
6061           reason = "scale is not a valid multiplier";
6062           goto report_error;
6063         }
6064     }
6065
6066   /* Validate displacement.  */
6067   if (disp)
6068     {
6069       reason_rtx = disp;
6070
6071       if (GET_CODE (disp) == CONST
6072           && GET_CODE (XEXP (disp, 0)) == UNSPEC)
6073         switch (XINT (XEXP (disp, 0), 1))
6074           {
6075           case UNSPEC_GOT:
6076           case UNSPEC_GOTOFF:
6077           case UNSPEC_GOTPCREL:
6078             if (!flag_pic)
6079               abort ();
6080             goto is_legitimate_pic;
6081
6082           case UNSPEC_GOTTPOFF:
6083           case UNSPEC_GOTNTPOFF:
6084           case UNSPEC_INDNTPOFF:
6085           case UNSPEC_NTPOFF:
6086           case UNSPEC_DTPOFF:
6087             break;
6088
6089           default:
6090             reason = "invalid address unspec";
6091             goto report_error;
6092           }
6093
6094       else if (flag_pic && (SYMBOLIC_CONST (disp)
6095 #if TARGET_MACHO
6096                             && !machopic_operand_p (disp)
6097 #endif
6098                             ))
6099         {
6100         is_legitimate_pic:
6101           if (TARGET_64BIT && (index || base))
6102             {
6103               /* foo@dtpoff(%rX) is ok.  */
6104               if (GET_CODE (disp) != CONST
6105                   || GET_CODE (XEXP (disp, 0)) != PLUS
6106                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
6107                   || GET_CODE (XEXP (XEXP (disp, 0), 1)) != CONST_INT
6108                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
6109                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
6110                 {
6111                   reason = "non-constant pic memory reference";
6112                   goto report_error;
6113                 }
6114             }
6115           else if (! legitimate_pic_address_disp_p (disp))
6116             {
6117               reason = "displacement is an invalid pic construct";
6118               goto report_error;
6119             }
6120
6121           /* This code used to verify that a symbolic pic displacement
6122              includes the pic_offset_table_rtx register.
6123
6124              While this is good idea, unfortunately these constructs may
6125              be created by "adds using lea" optimization for incorrect
6126              code like:
6127
6128              int a;
6129              int foo(int i)
6130                {
6131                  return *(&a+i);
6132                }
6133
6134              This code is nonsensical, but results in addressing
6135              GOT table with pic_offset_table_rtx base.  We can't
6136              just refuse it easily, since it gets matched by
6137              "addsi3" pattern, that later gets split to lea in the
6138              case output register differs from input.  While this
6139              can be handled by separate addsi pattern for this case
6140              that never results in lea, this seems to be easier and
6141              correct fix for crash to disable this test.  */
6142         }
6143       else if (GET_CODE (disp) != LABEL_REF
6144                && GET_CODE (disp) != CONST_INT
6145                && (GET_CODE (disp) != CONST
6146                    || !legitimate_constant_p (disp))
6147                && (GET_CODE (disp) != SYMBOL_REF
6148                    || !legitimate_constant_p (disp)))
6149         {
6150           reason = "displacement is not constant";
6151           goto report_error;
6152         }
6153       else if (TARGET_64BIT && !x86_64_sign_extended_value (disp))
6154         {
6155           reason = "displacement is out of range";
6156           goto report_error;
6157         }
6158     }
6159
6160   /* Everything looks valid.  */
6161   if (TARGET_DEBUG_ADDR)
6162     fprintf (stderr, "Success.\n");
6163   return TRUE;
6164
6165  report_error:
6166   if (TARGET_DEBUG_ADDR)
6167     {
6168       fprintf (stderr, "Error: %s\n", reason);
6169       debug_rtx (reason_rtx);
6170     }
6171   return FALSE;
6172 }
6173 \f
6174 /* Return an unique alias set for the GOT.  */
6175
6176 static HOST_WIDE_INT
6177 ix86_GOT_alias_set (void)
6178 {
6179   static HOST_WIDE_INT set = -1;
6180   if (set == -1)
6181     set = new_alias_set ();
6182   return set;
6183 }
6184
6185 /* Return a legitimate reference for ORIG (an address) using the
6186    register REG.  If REG is 0, a new pseudo is generated.
6187
6188    There are two types of references that must be handled:
6189
6190    1. Global data references must load the address from the GOT, via
6191       the PIC reg.  An insn is emitted to do this load, and the reg is
6192       returned.
6193
6194    2. Static data references, constant pool addresses, and code labels
6195       compute the address as an offset from the GOT, whose base is in
6196       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
6197       differentiate them from global data objects.  The returned
6198       address is the PIC reg + an unspec constant.
6199
6200    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
6201    reg also appears in the address.  */
6202
6203 rtx
6204 legitimize_pic_address (rtx orig, rtx reg)
6205 {
6206   rtx addr = orig;
6207   rtx new = orig;
6208   rtx base;
6209
6210 #if TARGET_MACHO
6211   if (reg == 0)
6212     reg = gen_reg_rtx (Pmode);
6213   /* Use the generic Mach-O PIC machinery.  */
6214   return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
6215 #endif
6216
6217   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
6218     new = addr;
6219   else if (!TARGET_64BIT && local_symbolic_operand (addr, Pmode))
6220     {
6221       /* This symbol may be referenced via a displacement from the PIC
6222          base address (@GOTOFF).  */
6223
6224       if (reload_in_progress)
6225         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6226       if (GET_CODE (addr) == CONST)
6227         addr = XEXP (addr, 0);
6228       if (GET_CODE (addr) == PLUS)
6229           {
6230             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
6231             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
6232           }
6233         else
6234           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
6235       new = gen_rtx_CONST (Pmode, new);
6236       new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6237
6238       if (reg != 0)
6239         {
6240           emit_move_insn (reg, new);
6241           new = reg;
6242         }
6243     }
6244   else if (GET_CODE (addr) == SYMBOL_REF)
6245     {
6246       if (TARGET_64BIT)
6247         {
6248           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
6249           new = gen_rtx_CONST (Pmode, new);
6250           new = gen_rtx_MEM (Pmode, new);
6251           RTX_UNCHANGING_P (new) = 1;
6252           set_mem_alias_set (new, ix86_GOT_alias_set ());
6253
6254           if (reg == 0)
6255             reg = gen_reg_rtx (Pmode);
6256           /* Use directly gen_movsi, otherwise the address is loaded
6257              into register for CSE.  We don't want to CSE this addresses,
6258              instead we CSE addresses from the GOT table, so skip this.  */
6259           emit_insn (gen_movsi (reg, new));
6260           new = reg;
6261         }
6262       else
6263         {
6264           /* This symbol must be referenced via a load from the
6265              Global Offset Table (@GOT).  */
6266
6267           if (reload_in_progress)
6268             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6269           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
6270           new = gen_rtx_CONST (Pmode, new);
6271           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6272           new = gen_rtx_MEM (Pmode, new);
6273           RTX_UNCHANGING_P (new) = 1;
6274           set_mem_alias_set (new, ix86_GOT_alias_set ());
6275
6276           if (reg == 0)
6277             reg = gen_reg_rtx (Pmode);
6278           emit_move_insn (reg, new);
6279           new = reg;
6280         }
6281     }
6282   else
6283     {
6284       if (GET_CODE (addr) == CONST)
6285         {
6286           addr = XEXP (addr, 0);
6287
6288           /* We must match stuff we generate before.  Assume the only
6289              unspecs that can get here are ours.  Not that we could do
6290              anything with them anyway....  */
6291           if (GET_CODE (addr) == UNSPEC
6292               || (GET_CODE (addr) == PLUS
6293                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
6294             return orig;
6295           if (GET_CODE (addr) != PLUS)
6296             abort ();
6297         }
6298       if (GET_CODE (addr) == PLUS)
6299         {
6300           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
6301
6302           /* Check first to see if this is a constant offset from a @GOTOFF
6303              symbol reference.  */
6304           if (local_symbolic_operand (op0, Pmode)
6305               && GET_CODE (op1) == CONST_INT)
6306             {
6307               if (!TARGET_64BIT)
6308                 {
6309                   if (reload_in_progress)
6310                     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6311                   new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
6312                                         UNSPEC_GOTOFF);
6313                   new = gen_rtx_PLUS (Pmode, new, op1);
6314                   new = gen_rtx_CONST (Pmode, new);
6315                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6316
6317                   if (reg != 0)
6318                     {
6319                       emit_move_insn (reg, new);
6320                       new = reg;
6321                     }
6322                 }
6323               else
6324                 {
6325                   if (INTVAL (op1) < -16*1024*1024
6326                       || INTVAL (op1) >= 16*1024*1024)
6327                     new = gen_rtx_PLUS (Pmode, op0, force_reg (Pmode, op1));
6328                 }
6329             }
6330           else
6331             {
6332               base = legitimize_pic_address (XEXP (addr, 0), reg);
6333               new  = legitimize_pic_address (XEXP (addr, 1),
6334                                              base == reg ? NULL_RTX : reg);
6335
6336               if (GET_CODE (new) == CONST_INT)
6337                 new = plus_constant (base, INTVAL (new));
6338               else
6339                 {
6340                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
6341                     {
6342                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
6343                       new = XEXP (new, 1);
6344                     }
6345                   new = gen_rtx_PLUS (Pmode, base, new);
6346                 }
6347             }
6348         }
6349     }
6350   return new;
6351 }
6352 \f
6353 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
6354
6355 static rtx
6356 get_thread_pointer (int to_reg)
6357 {
6358   rtx tp, reg, insn;
6359
6360   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
6361   if (!to_reg)
6362     return tp;
6363
6364   reg = gen_reg_rtx (Pmode);
6365   insn = gen_rtx_SET (VOIDmode, reg, tp);
6366   insn = emit_insn (insn);
6367
6368   return reg;
6369 }
6370
6371 /* A subroutine of legitimize_address and ix86_expand_move.  FOR_MOV is
6372    false if we expect this to be used for a memory address and true if
6373    we expect to load the address into a register.  */
6374
6375 static rtx
6376 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
6377 {
6378   rtx dest, base, off, pic;
6379   int type;
6380
6381   switch (model)
6382     {
6383     case TLS_MODEL_GLOBAL_DYNAMIC:
6384       dest = gen_reg_rtx (Pmode);
6385       if (TARGET_64BIT)
6386         {
6387           rtx rax = gen_rtx_REG (Pmode, 0), insns;
6388
6389           start_sequence ();
6390           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
6391           insns = get_insns ();
6392           end_sequence ();
6393
6394           emit_libcall_block (insns, dest, rax, x);
6395         }
6396       else
6397         emit_insn (gen_tls_global_dynamic_32 (dest, x));
6398       break;
6399
6400     case TLS_MODEL_LOCAL_DYNAMIC:
6401       base = gen_reg_rtx (Pmode);
6402       if (TARGET_64BIT)
6403         {
6404           rtx rax = gen_rtx_REG (Pmode, 0), insns, note;
6405
6406           start_sequence ();
6407           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
6408           insns = get_insns ();
6409           end_sequence ();
6410
6411           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
6412           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
6413           emit_libcall_block (insns, base, rax, note);
6414         }
6415       else
6416         emit_insn (gen_tls_local_dynamic_base_32 (base));
6417
6418       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
6419       off = gen_rtx_CONST (Pmode, off);
6420
6421       return gen_rtx_PLUS (Pmode, base, off);
6422
6423     case TLS_MODEL_INITIAL_EXEC:
6424       if (TARGET_64BIT)
6425         {
6426           pic = NULL;
6427           type = UNSPEC_GOTNTPOFF;
6428         }
6429       else if (flag_pic)
6430         {
6431           if (reload_in_progress)
6432             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6433           pic = pic_offset_table_rtx;
6434           type = TARGET_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
6435         }
6436       else if (!TARGET_GNU_TLS)
6437         {
6438           pic = gen_reg_rtx (Pmode);
6439           emit_insn (gen_set_got (pic));
6440           type = UNSPEC_GOTTPOFF;
6441         }
6442       else
6443         {
6444           pic = NULL;
6445           type = UNSPEC_INDNTPOFF;
6446         }
6447
6448       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
6449       off = gen_rtx_CONST (Pmode, off);
6450       if (pic)
6451         off = gen_rtx_PLUS (Pmode, pic, off);
6452       off = gen_rtx_MEM (Pmode, off);
6453       RTX_UNCHANGING_P (off) = 1;
6454       set_mem_alias_set (off, ix86_GOT_alias_set ());
6455
6456       if (TARGET_64BIT || TARGET_GNU_TLS)
6457         {
6458           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
6459           off = force_reg (Pmode, off);
6460           return gen_rtx_PLUS (Pmode, base, off);
6461         }
6462       else
6463         {
6464           base = get_thread_pointer (true);
6465           dest = gen_reg_rtx (Pmode);
6466           emit_insn (gen_subsi3 (dest, base, off));
6467         }
6468       break;
6469
6470     case TLS_MODEL_LOCAL_EXEC:
6471       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
6472                             (TARGET_64BIT || TARGET_GNU_TLS)
6473                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
6474       off = gen_rtx_CONST (Pmode, off);
6475
6476       if (TARGET_64BIT || TARGET_GNU_TLS)
6477         {
6478           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
6479           return gen_rtx_PLUS (Pmode, base, off);
6480         }
6481       else
6482         {
6483           base = get_thread_pointer (true);
6484           dest = gen_reg_rtx (Pmode);
6485           emit_insn (gen_subsi3 (dest, base, off));
6486         }
6487       break;
6488
6489     default:
6490       abort ();
6491     }
6492
6493   return dest;
6494 }
6495
6496 /* Try machine-dependent ways of modifying an illegitimate address
6497    to be legitimate.  If we find one, return the new, valid address.
6498    This macro is used in only one place: `memory_address' in explow.c.
6499
6500    OLDX is the address as it was before break_out_memory_refs was called.
6501    In some cases it is useful to look at this to decide what needs to be done.
6502
6503    MODE and WIN are passed so that this macro can use
6504    GO_IF_LEGITIMATE_ADDRESS.
6505
6506    It is always safe for this macro to do nothing.  It exists to recognize
6507    opportunities to optimize the output.
6508
6509    For the 80386, we handle X+REG by loading X into a register R and
6510    using R+REG.  R will go in a general reg and indexing will be used.
6511    However, if REG is a broken-out memory address or multiplication,
6512    nothing needs to be done because REG can certainly go in a general reg.
6513
6514    When -fpic is used, special handling is needed for symbolic references.
6515    See comments by legitimize_pic_address in i386.c for details.  */
6516
6517 rtx
6518 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED, enum machine_mode mode)
6519 {
6520   int changed = 0;
6521   unsigned log;
6522
6523   if (TARGET_DEBUG_ADDR)
6524     {
6525       fprintf (stderr, "\n==========\nLEGITIMIZE_ADDRESS, mode = %s\n",
6526                GET_MODE_NAME (mode));
6527       debug_rtx (x);
6528     }
6529
6530   log = tls_symbolic_operand (x, mode);
6531   if (log)
6532     return legitimize_tls_address (x, log, false);
6533
6534   if (flag_pic && SYMBOLIC_CONST (x))
6535     return legitimize_pic_address (x, 0);
6536
6537   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
6538   if (GET_CODE (x) == ASHIFT
6539       && GET_CODE (XEXP (x, 1)) == CONST_INT
6540       && (log = (unsigned) exact_log2 (INTVAL (XEXP (x, 1)))) < 4)
6541     {
6542       changed = 1;
6543       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
6544                         GEN_INT (1 << log));
6545     }
6546
6547   if (GET_CODE (x) == PLUS)
6548     {
6549       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
6550
6551       if (GET_CODE (XEXP (x, 0)) == ASHIFT
6552           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6553           && (log = (unsigned) exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) < 4)
6554         {
6555           changed = 1;
6556           XEXP (x, 0) = gen_rtx_MULT (Pmode,
6557                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
6558                                       GEN_INT (1 << log));
6559         }
6560
6561       if (GET_CODE (XEXP (x, 1)) == ASHIFT
6562           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
6563           && (log = (unsigned) exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1)))) < 4)
6564         {
6565           changed = 1;
6566           XEXP (x, 1) = gen_rtx_MULT (Pmode,
6567                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
6568                                       GEN_INT (1 << log));
6569         }
6570
6571       /* Put multiply first if it isn't already.  */
6572       if (GET_CODE (XEXP (x, 1)) == MULT)
6573         {
6574           rtx tmp = XEXP (x, 0);
6575           XEXP (x, 0) = XEXP (x, 1);
6576           XEXP (x, 1) = tmp;
6577           changed = 1;
6578         }
6579
6580       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
6581          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
6582          created by virtual register instantiation, register elimination, and
6583          similar optimizations.  */
6584       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
6585         {
6586           changed = 1;
6587           x = gen_rtx_PLUS (Pmode,
6588                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
6589                                           XEXP (XEXP (x, 1), 0)),
6590                             XEXP (XEXP (x, 1), 1));
6591         }
6592
6593       /* Canonicalize
6594          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
6595          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
6596       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
6597                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
6598                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
6599                && CONSTANT_P (XEXP (x, 1)))
6600         {
6601           rtx constant;
6602           rtx other = NULL_RTX;
6603
6604           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6605             {
6606               constant = XEXP (x, 1);
6607               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
6608             }
6609           else if (GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 1)) == CONST_INT)
6610             {
6611               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
6612               other = XEXP (x, 1);
6613             }
6614           else
6615             constant = 0;
6616
6617           if (constant)
6618             {
6619               changed = 1;
6620               x = gen_rtx_PLUS (Pmode,
6621                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
6622                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
6623                                 plus_constant (other, INTVAL (constant)));
6624             }
6625         }
6626
6627       if (changed && legitimate_address_p (mode, x, FALSE))
6628         return x;
6629
6630       if (GET_CODE (XEXP (x, 0)) == MULT)
6631         {
6632           changed = 1;
6633           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
6634         }
6635
6636       if (GET_CODE (XEXP (x, 1)) == MULT)
6637         {
6638           changed = 1;
6639           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
6640         }
6641
6642       if (changed
6643           && GET_CODE (XEXP (x, 1)) == REG
6644           && GET_CODE (XEXP (x, 0)) == REG)
6645         return x;
6646
6647       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
6648         {
6649           changed = 1;
6650           x = legitimize_pic_address (x, 0);
6651         }
6652
6653       if (changed && legitimate_address_p (mode, x, FALSE))
6654         return x;
6655
6656       if (GET_CODE (XEXP (x, 0)) == REG)
6657         {
6658           rtx temp = gen_reg_rtx (Pmode);
6659           rtx val  = force_operand (XEXP (x, 1), temp);
6660           if (val != temp)
6661             emit_move_insn (temp, val);
6662
6663           XEXP (x, 1) = temp;
6664           return x;
6665         }
6666
6667       else if (GET_CODE (XEXP (x, 1)) == REG)
6668         {
6669           rtx temp = gen_reg_rtx (Pmode);
6670           rtx val  = force_operand (XEXP (x, 0), temp);
6671           if (val != temp)
6672             emit_move_insn (temp, val);
6673
6674           XEXP (x, 0) = temp;
6675           return x;
6676         }
6677     }
6678
6679   return x;
6680 }
6681 \f
6682 /* Print an integer constant expression in assembler syntax.  Addition
6683    and subtraction are the only arithmetic that may appear in these
6684    expressions.  FILE is the stdio stream to write to, X is the rtx, and
6685    CODE is the operand print code from the output string.  */
6686
6687 static void
6688 output_pic_addr_const (FILE *file, rtx x, int code)
6689 {
6690   char buf[256];
6691
6692   switch (GET_CODE (x))
6693     {
6694     case PC:
6695       if (flag_pic)
6696         putc ('.', file);
6697       else
6698         abort ();
6699       break;
6700
6701     case SYMBOL_REF:
6702       assemble_name (file, XSTR (x, 0));
6703       if (!TARGET_MACHO && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
6704         fputs ("@PLT", file);
6705       break;
6706
6707     case LABEL_REF:
6708       x = XEXP (x, 0);
6709       /* FALLTHRU */
6710     case CODE_LABEL:
6711       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
6712       assemble_name (asm_out_file, buf);
6713       break;
6714
6715     case CONST_INT:
6716       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
6717       break;
6718
6719     case CONST:
6720       /* This used to output parentheses around the expression,
6721          but that does not work on the 386 (either ATT or BSD assembler).  */
6722       output_pic_addr_const (file, XEXP (x, 0), code);
6723       break;
6724
6725     case CONST_DOUBLE:
6726       if (GET_MODE (x) == VOIDmode)
6727         {
6728           /* We can use %d if the number is <32 bits and positive.  */
6729           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
6730             fprintf (file, "0x%lx%08lx",
6731                      (unsigned long) CONST_DOUBLE_HIGH (x),
6732                      (unsigned long) CONST_DOUBLE_LOW (x));
6733           else
6734             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
6735         }
6736       else
6737         /* We can't handle floating point constants;
6738            PRINT_OPERAND must handle them.  */
6739         output_operand_lossage ("floating constant misused");
6740       break;
6741
6742     case PLUS:
6743       /* Some assemblers need integer constants to appear first.  */
6744       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
6745         {
6746           output_pic_addr_const (file, XEXP (x, 0), code);
6747           putc ('+', file);
6748           output_pic_addr_const (file, XEXP (x, 1), code);
6749         }
6750       else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6751         {
6752           output_pic_addr_const (file, XEXP (x, 1), code);
6753           putc ('+', file);
6754           output_pic_addr_const (file, XEXP (x, 0), code);
6755         }
6756       else
6757         abort ();
6758       break;
6759
6760     case MINUS:
6761       if (!TARGET_MACHO)
6762         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
6763       output_pic_addr_const (file, XEXP (x, 0), code);
6764       putc ('-', file);
6765       output_pic_addr_const (file, XEXP (x, 1), code);
6766       if (!TARGET_MACHO)
6767         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
6768       break;
6769
6770      case UNSPEC:
6771        if (XVECLEN (x, 0) != 1)
6772          abort ();
6773        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
6774        switch (XINT (x, 1))
6775         {
6776         case UNSPEC_GOT:
6777           fputs ("@GOT", file);
6778           break;
6779         case UNSPEC_GOTOFF:
6780           fputs ("@GOTOFF", file);
6781           break;
6782         case UNSPEC_GOTPCREL:
6783           fputs ("@GOTPCREL(%rip)", file);
6784           break;
6785         case UNSPEC_GOTTPOFF:
6786           /* FIXME: This might be @TPOFF in Sun ld too.  */
6787           fputs ("@GOTTPOFF", file);
6788           break;
6789         case UNSPEC_TPOFF:
6790           fputs ("@TPOFF", file);
6791           break;
6792         case UNSPEC_NTPOFF:
6793           if (TARGET_64BIT)
6794             fputs ("@TPOFF", file);
6795           else
6796             fputs ("@NTPOFF", file);
6797           break;
6798         case UNSPEC_DTPOFF:
6799           fputs ("@DTPOFF", file);
6800           break;
6801         case UNSPEC_GOTNTPOFF:
6802           if (TARGET_64BIT)
6803             fputs ("@GOTTPOFF(%rip)", file);
6804           else
6805             fputs ("@GOTNTPOFF", file);
6806           break;
6807         case UNSPEC_INDNTPOFF:
6808           fputs ("@INDNTPOFF", file);
6809           break;
6810         default:
6811           output_operand_lossage ("invalid UNSPEC as operand");
6812           break;
6813         }
6814        break;
6815
6816     default:
6817       output_operand_lossage ("invalid expression as operand");
6818     }
6819 }
6820
6821 /* This is called from dwarfout.c via ASM_OUTPUT_DWARF_ADDR_CONST.
6822    We need to handle our special PIC relocations.  */
6823
6824 void
6825 i386_dwarf_output_addr_const (FILE *file, rtx x)
6826 {
6827 #ifdef ASM_QUAD
6828   fprintf (file, "%s", TARGET_64BIT ? ASM_QUAD : ASM_LONG);
6829 #else
6830   if (TARGET_64BIT)
6831     abort ();
6832   fprintf (file, "%s", ASM_LONG);
6833 #endif
6834   if (flag_pic)
6835     output_pic_addr_const (file, x, '\0');
6836   else
6837     output_addr_const (file, x);
6838   fputc ('\n', file);
6839 }
6840
6841 /* This is called from dwarf2out.c via ASM_OUTPUT_DWARF_DTPREL.
6842    We need to emit DTP-relative relocations.  */
6843
6844 void
6845 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
6846 {
6847   fputs (ASM_LONG, file);
6848   output_addr_const (file, x);
6849   fputs ("@DTPOFF", file);
6850   switch (size)
6851     {
6852     case 4:
6853       break;
6854     case 8:
6855       fputs (", 0", file);
6856       break;
6857     default:
6858       abort ();
6859    }
6860 }
6861
6862 /* In the name of slightly smaller debug output, and to cater to
6863    general assembler losage, recognize PIC+GOTOFF and turn it back
6864    into a direct symbol reference.  */
6865
6866 static rtx
6867 ix86_delegitimize_address (rtx orig_x)
6868 {
6869   rtx x = orig_x, y;
6870
6871   if (GET_CODE (x) == MEM)
6872     x = XEXP (x, 0);
6873
6874   if (TARGET_64BIT)
6875     {
6876       if (GET_CODE (x) != CONST
6877           || GET_CODE (XEXP (x, 0)) != UNSPEC
6878           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
6879           || GET_CODE (orig_x) != MEM)
6880         return orig_x;
6881       return XVECEXP (XEXP (x, 0), 0, 0);
6882     }
6883
6884   if (GET_CODE (x) != PLUS
6885       || GET_CODE (XEXP (x, 1)) != CONST)
6886     return orig_x;
6887
6888   if (GET_CODE (XEXP (x, 0)) == REG
6889       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
6890     /* %ebx + GOT/GOTOFF */
6891     y = NULL;
6892   else if (GET_CODE (XEXP (x, 0)) == PLUS)
6893     {
6894       /* %ebx + %reg * scale + GOT/GOTOFF */
6895       y = XEXP (x, 0);
6896       if (GET_CODE (XEXP (y, 0)) == REG
6897           && REGNO (XEXP (y, 0)) == PIC_OFFSET_TABLE_REGNUM)
6898         y = XEXP (y, 1);
6899       else if (GET_CODE (XEXP (y, 1)) == REG
6900                && REGNO (XEXP (y, 1)) == PIC_OFFSET_TABLE_REGNUM)
6901         y = XEXP (y, 0);
6902       else
6903         return orig_x;
6904       if (GET_CODE (y) != REG
6905           && GET_CODE (y) != MULT
6906           && GET_CODE (y) != ASHIFT)
6907         return orig_x;
6908     }
6909   else
6910     return orig_x;
6911
6912   x = XEXP (XEXP (x, 1), 0);
6913   if (GET_CODE (x) == UNSPEC
6914       && ((XINT (x, 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
6915           || (XINT (x, 1) == UNSPEC_GOTOFF && GET_CODE (orig_x) != MEM)))
6916     {
6917       if (y)
6918         return gen_rtx_PLUS (Pmode, y, XVECEXP (x, 0, 0));
6919       return XVECEXP (x, 0, 0);
6920     }
6921
6922   if (GET_CODE (x) == PLUS
6923       && GET_CODE (XEXP (x, 0)) == UNSPEC
6924       && GET_CODE (XEXP (x, 1)) == CONST_INT
6925       && ((XINT (XEXP (x, 0), 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
6926           || (XINT (XEXP (x, 0), 1) == UNSPEC_GOTOFF
6927               && GET_CODE (orig_x) != MEM)))
6928     {
6929       x = gen_rtx_PLUS (VOIDmode, XVECEXP (XEXP (x, 0), 0, 0), XEXP (x, 1));
6930       if (y)
6931         return gen_rtx_PLUS (Pmode, y, x);
6932       return x;
6933     }
6934
6935   return orig_x;
6936 }
6937 \f
6938 static void
6939 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
6940                     int fp, FILE *file)
6941 {
6942   const char *suffix;
6943
6944   if (mode == CCFPmode || mode == CCFPUmode)
6945     {
6946       enum rtx_code second_code, bypass_code;
6947       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
6948       if (bypass_code != NIL || second_code != NIL)
6949         abort ();
6950       code = ix86_fp_compare_code_to_integer (code);
6951       mode = CCmode;
6952     }
6953   if (reverse)
6954     code = reverse_condition (code);
6955
6956   switch (code)
6957     {
6958     case EQ:
6959       suffix = "e";
6960       break;
6961     case NE:
6962       suffix = "ne";
6963       break;
6964     case GT:
6965       if (mode != CCmode && mode != CCNOmode && mode != CCGCmode)
6966         abort ();
6967       suffix = "g";
6968       break;
6969     case GTU:
6970       /* ??? Use "nbe" instead of "a" for fcmov losage on some assemblers.
6971          Those same assemblers have the same but opposite losage on cmov.  */
6972       if (mode != CCmode)
6973         abort ();
6974       suffix = fp ? "nbe" : "a";
6975       break;
6976     case LT:
6977       if (mode == CCNOmode || mode == CCGOCmode)
6978         suffix = "s";
6979       else if (mode == CCmode || mode == CCGCmode)
6980         suffix = "l";
6981       else
6982         abort ();
6983       break;
6984     case LTU:
6985       if (mode != CCmode)
6986         abort ();
6987       suffix = "b";
6988       break;
6989     case GE:
6990       if (mode == CCNOmode || mode == CCGOCmode)
6991         suffix = "ns";
6992       else if (mode == CCmode || mode == CCGCmode)
6993         suffix = "ge";
6994       else
6995         abort ();
6996       break;
6997     case GEU:
6998       /* ??? As above.  */
6999       if (mode != CCmode)
7000         abort ();
7001       suffix = fp ? "nb" : "ae";
7002       break;
7003     case LE:
7004       if (mode != CCmode && mode != CCGCmode && mode != CCNOmode)
7005         abort ();
7006       suffix = "le";
7007       break;
7008     case LEU:
7009       if (mode != CCmode)
7010         abort ();
7011       suffix = "be";
7012       break;
7013     case UNORDERED:
7014       suffix = fp ? "u" : "p";
7015       break;
7016     case ORDERED:
7017       suffix = fp ? "nu" : "np";
7018       break;
7019     default:
7020       abort ();
7021     }
7022   fputs (suffix, file);
7023 }
7024
7025 /* Print the name of register X to FILE based on its machine mode and number.
7026    If CODE is 'w', pretend the mode is HImode.
7027    If CODE is 'b', pretend the mode is QImode.
7028    If CODE is 'k', pretend the mode is SImode.
7029    If CODE is 'q', pretend the mode is DImode.
7030    If CODE is 'h', pretend the reg is the `high' byte register.
7031    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.  */
7032
7033 void
7034 print_reg (rtx x, int code, FILE *file)
7035 {
7036   if (REGNO (x) == ARG_POINTER_REGNUM
7037       || REGNO (x) == FRAME_POINTER_REGNUM
7038       || REGNO (x) == FLAGS_REG
7039       || REGNO (x) == FPSR_REG)
7040     abort ();
7041
7042   if (ASSEMBLER_DIALECT == ASM_ATT || USER_LABEL_PREFIX[0] == 0)
7043     putc ('%', file);
7044
7045   if (code == 'w' || MMX_REG_P (x))
7046     code = 2;
7047   else if (code == 'b')
7048     code = 1;
7049   else if (code == 'k')
7050     code = 4;
7051   else if (code == 'q')
7052     code = 8;
7053   else if (code == 'y')
7054     code = 3;
7055   else if (code == 'h')
7056     code = 0;
7057   else
7058     code = GET_MODE_SIZE (GET_MODE (x));
7059
7060   /* Irritatingly, AMD extended registers use different naming convention
7061      from the normal registers.  */
7062   if (REX_INT_REG_P (x))
7063     {
7064       if (!TARGET_64BIT)
7065         abort ();
7066       switch (code)
7067         {
7068           case 0:
7069             error ("extended registers have no high halves");
7070             break;
7071           case 1:
7072             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
7073             break;
7074           case 2:
7075             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
7076             break;
7077           case 4:
7078             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
7079             break;
7080           case 8:
7081             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
7082             break;
7083           default:
7084             error ("unsupported operand size for extended register");
7085             break;
7086         }
7087       return;
7088     }
7089   switch (code)
7090     {
7091     case 3:
7092       if (STACK_TOP_P (x))
7093         {
7094           fputs ("st(0)", file);
7095           break;
7096         }
7097       /* FALLTHRU */
7098     case 8:
7099     case 4:
7100     case 12:
7101       if (! ANY_FP_REG_P (x))
7102         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
7103       /* FALLTHRU */
7104     case 16:
7105     case 2:
7106     normal:
7107       fputs (hi_reg_name[REGNO (x)], file);
7108       break;
7109     case 1:
7110       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
7111         goto normal;
7112       fputs (qi_reg_name[REGNO (x)], file);
7113       break;
7114     case 0:
7115       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
7116         goto normal;
7117       fputs (qi_high_reg_name[REGNO (x)], file);
7118       break;
7119     default:
7120       abort ();
7121     }
7122 }
7123
7124 /* Locate some local-dynamic symbol still in use by this function
7125    so that we can print its name in some tls_local_dynamic_base
7126    pattern.  */
7127
7128 static const char *
7129 get_some_local_dynamic_name (void)
7130 {
7131   rtx insn;
7132
7133   if (cfun->machine->some_ld_name)
7134     return cfun->machine->some_ld_name;
7135
7136   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
7137     if (INSN_P (insn)
7138         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
7139       return cfun->machine->some_ld_name;
7140
7141   abort ();
7142 }
7143
7144 static int
7145 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
7146 {
7147   rtx x = *px;
7148
7149   if (GET_CODE (x) == SYMBOL_REF
7150       && local_dynamic_symbolic_operand (x, Pmode))
7151     {
7152       cfun->machine->some_ld_name = XSTR (x, 0);
7153       return 1;
7154     }
7155
7156   return 0;
7157 }
7158
7159 /* Meaning of CODE:
7160    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
7161    C -- print opcode suffix for set/cmov insn.
7162    c -- like C, but print reversed condition
7163    F,f -- likewise, but for floating-point.
7164    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
7165         otherwise nothing
7166    R -- print the prefix for register names.
7167    z -- print the opcode suffix for the size of the current operand.
7168    * -- print a star (in certain assembler syntax)
7169    A -- print an absolute memory reference.
7170    w -- print the operand as if it's a "word" (HImode) even if it isn't.
7171    s -- print a shift double count, followed by the assemblers argument
7172         delimiter.
7173    b -- print the QImode name of the register for the indicated operand.
7174         %b0 would print %al if operands[0] is reg 0.
7175    w --  likewise, print the HImode name of the register.
7176    k --  likewise, print the SImode name of the register.
7177    q --  likewise, print the DImode name of the register.
7178    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
7179    y -- print "st(0)" instead of "st" as a register.
7180    D -- print condition for SSE cmp instruction.
7181    P -- if PIC, print an @PLT suffix.
7182    X -- don't print any sort of PIC '@' suffix for a symbol.
7183    & -- print some in-use local-dynamic symbol name.
7184  */
7185
7186 void
7187 print_operand (FILE *file, rtx x, int code)
7188 {
7189   if (code)
7190     {
7191       switch (code)
7192         {
7193         case '*':
7194           if (ASSEMBLER_DIALECT == ASM_ATT)
7195             putc ('*', file);
7196           return;
7197
7198         case '&':
7199           assemble_name (file, get_some_local_dynamic_name ());
7200           return;
7201
7202         case 'A':
7203           if (ASSEMBLER_DIALECT == ASM_ATT)
7204             putc ('*', file);
7205           else if (ASSEMBLER_DIALECT == ASM_INTEL)
7206             {
7207               /* Intel syntax. For absolute addresses, registers should not
7208                  be surrounded by braces.  */
7209               if (GET_CODE (x) != REG)
7210                 {
7211                   putc ('[', file);
7212                   PRINT_OPERAND (file, x, 0);
7213                   putc (']', file);
7214                   return;
7215                 }
7216             }
7217           else
7218             abort ();
7219
7220           PRINT_OPERAND (file, x, 0);
7221           return;
7222
7223
7224         case 'L':
7225           if (ASSEMBLER_DIALECT == ASM_ATT)
7226             putc ('l', file);
7227           return;
7228
7229         case 'W':
7230           if (ASSEMBLER_DIALECT == ASM_ATT)
7231             putc ('w', file);
7232           return;
7233
7234         case 'B':
7235           if (ASSEMBLER_DIALECT == ASM_ATT)
7236             putc ('b', file);
7237           return;
7238
7239         case 'Q':
7240           if (ASSEMBLER_DIALECT == ASM_ATT)
7241             putc ('l', file);
7242           return;
7243
7244         case 'S':
7245           if (ASSEMBLER_DIALECT == ASM_ATT)
7246             putc ('s', file);
7247           return;
7248
7249         case 'T':
7250           if (ASSEMBLER_DIALECT == ASM_ATT)
7251             putc ('t', file);
7252           return;
7253
7254         case 'z':
7255           /* 387 opcodes don't get size suffixes if the operands are
7256              registers.  */
7257           if (STACK_REG_P (x))
7258             return;
7259
7260           /* Likewise if using Intel opcodes.  */
7261           if (ASSEMBLER_DIALECT == ASM_INTEL)
7262             return;
7263
7264           /* This is the size of op from size of operand.  */
7265           switch (GET_MODE_SIZE (GET_MODE (x)))
7266             {
7267             case 2:
7268 #ifdef HAVE_GAS_FILDS_FISTS
7269               putc ('s', file);
7270 #endif
7271               return;
7272
7273             case 4:
7274               if (GET_MODE (x) == SFmode)
7275                 {
7276                   putc ('s', file);
7277                   return;
7278                 }
7279               else
7280                 putc ('l', file);
7281               return;
7282
7283             case 12:
7284             case 16:
7285               putc ('t', file);
7286               return;
7287
7288             case 8:
7289               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
7290                 {
7291 #ifdef GAS_MNEMONICS
7292                   putc ('q', file);
7293 #else
7294                   putc ('l', file);
7295                   putc ('l', file);
7296 #endif
7297                 }
7298               else
7299                 putc ('l', file);
7300               return;
7301
7302             default:
7303               abort ();
7304             }
7305
7306         case 'b':
7307         case 'w':
7308         case 'k':
7309         case 'q':
7310         case 'h':
7311         case 'y':
7312         case 'X':
7313         case 'P':
7314           break;
7315
7316         case 's':
7317           if (GET_CODE (x) == CONST_INT || ! SHIFT_DOUBLE_OMITS_COUNT)
7318             {
7319               PRINT_OPERAND (file, x, 0);
7320               putc (',', file);
7321             }
7322           return;
7323
7324         case 'D':
7325           /* Little bit of braindamage here.  The SSE compare instructions
7326              does use completely different names for the comparisons that the
7327              fp conditional moves.  */
7328           switch (GET_CODE (x))
7329             {
7330             case EQ:
7331             case UNEQ:
7332               fputs ("eq", file);
7333               break;
7334             case LT:
7335             case UNLT:
7336               fputs ("lt", file);
7337               break;
7338             case LE:
7339             case UNLE:
7340               fputs ("le", file);
7341               break;
7342             case UNORDERED:
7343               fputs ("unord", file);
7344               break;
7345             case NE:
7346             case LTGT:
7347               fputs ("neq", file);
7348               break;
7349             case UNGE:
7350             case GE:
7351               fputs ("nlt", file);
7352               break;
7353             case UNGT:
7354             case GT:
7355               fputs ("nle", file);
7356               break;
7357             case ORDERED:
7358               fputs ("ord", file);
7359               break;
7360             default:
7361               abort ();
7362               break;
7363             }
7364           return;
7365         case 'O':
7366 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7367           if (ASSEMBLER_DIALECT == ASM_ATT)
7368             {
7369               switch (GET_MODE (x))
7370                 {
7371                 case HImode: putc ('w', file); break;
7372                 case SImode:
7373                 case SFmode: putc ('l', file); break;
7374                 case DImode:
7375                 case DFmode: putc ('q', file); break;
7376                 default: abort ();
7377                 }
7378               putc ('.', file);
7379             }
7380 #endif
7381           return;
7382         case 'C':
7383           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
7384           return;
7385         case 'F':
7386 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7387           if (ASSEMBLER_DIALECT == ASM_ATT)
7388             putc ('.', file);
7389 #endif
7390           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
7391           return;
7392
7393           /* Like above, but reverse condition */
7394         case 'c':
7395           /* Check to see if argument to %c is really a constant
7396              and not a condition code which needs to be reversed.  */
7397           if (GET_RTX_CLASS (GET_CODE (x)) != '<')
7398           {
7399             output_operand_lossage ("operand is neither a constant nor a condition code, invalid operand code 'c'");
7400              return;
7401           }
7402           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
7403           return;
7404         case 'f':
7405 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7406           if (ASSEMBLER_DIALECT == ASM_ATT)
7407             putc ('.', file);
7408 #endif
7409           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
7410           return;
7411         case '+':
7412           {
7413             rtx x;
7414
7415             if (!optimize || optimize_size || !TARGET_BRANCH_PREDICTION_HINTS)
7416               return;
7417
7418             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
7419             if (x)
7420               {
7421                 int pred_val = INTVAL (XEXP (x, 0));
7422
7423                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
7424                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
7425                   {
7426                     int taken = pred_val > REG_BR_PROB_BASE / 2;
7427                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
7428
7429                     /* Emit hints only in the case default branch prediction
7430                        heuristics would fail.  */
7431                     if (taken != cputaken)
7432                       {
7433                         /* We use 3e (DS) prefix for taken branches and
7434                            2e (CS) prefix for not taken branches.  */
7435                         if (taken)
7436                           fputs ("ds ; ", file);
7437                         else
7438                           fputs ("cs ; ", file);
7439                       }
7440                   }
7441               }
7442             return;
7443           }
7444         default:
7445             output_operand_lossage ("invalid operand code `%c'", code);
7446         }
7447     }
7448
7449   if (GET_CODE (x) == REG)
7450     print_reg (x, code, file);
7451
7452   else if (GET_CODE (x) == MEM)
7453     {
7454       /* No `byte ptr' prefix for call instructions.  */
7455       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P')
7456         {
7457           const char * size;
7458           switch (GET_MODE_SIZE (GET_MODE (x)))
7459             {
7460             case 1: size = "BYTE"; break;
7461             case 2: size = "WORD"; break;
7462             case 4: size = "DWORD"; break;
7463             case 8: size = "QWORD"; break;
7464             case 12: size = "XWORD"; break;
7465             case 16: size = "XMMWORD"; break;
7466             default:
7467               abort ();
7468             }
7469
7470           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
7471           if (code == 'b')
7472             size = "BYTE";
7473           else if (code == 'w')
7474             size = "WORD";
7475           else if (code == 'k')
7476             size = "DWORD";
7477
7478           fputs (size, file);
7479           fputs (" PTR ", file);
7480         }
7481
7482       x = XEXP (x, 0);
7483       /* Avoid (%rip) for call operands.  */
7484       if (CONSTANT_ADDRESS_P (x) && code == 'P'
7485                && GET_CODE (x) != CONST_INT)
7486         output_addr_const (file, x);
7487       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
7488         output_operand_lossage ("invalid constraints for operand");
7489       else
7490         output_address (x);
7491     }
7492
7493   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
7494     {
7495       REAL_VALUE_TYPE r;
7496       long l;
7497
7498       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7499       REAL_VALUE_TO_TARGET_SINGLE (r, l);
7500
7501       if (ASSEMBLER_DIALECT == ASM_ATT)
7502         putc ('$', file);
7503       fprintf (file, "0x%08lx", l);
7504     }
7505
7506   /* These float cases don't actually occur as immediate operands.  */
7507   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
7508     {
7509       char dstr[30];
7510
7511       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
7512       fprintf (file, "%s", dstr);
7513     }
7514
7515   else if (GET_CODE (x) == CONST_DOUBLE
7516            && GET_MODE (x) == XFmode)
7517     {
7518       char dstr[30];
7519
7520       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
7521       fprintf (file, "%s", dstr);
7522     }
7523
7524   else
7525     {
7526       if (code != 'P')
7527         {
7528           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
7529             {
7530               if (ASSEMBLER_DIALECT == ASM_ATT)
7531                 putc ('$', file);
7532             }
7533           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
7534                    || GET_CODE (x) == LABEL_REF)
7535             {
7536               if (ASSEMBLER_DIALECT == ASM_ATT)
7537                 putc ('$', file);
7538               else
7539                 fputs ("OFFSET FLAT:", file);
7540             }
7541         }
7542       if (GET_CODE (x) == CONST_INT)
7543         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
7544       else if (flag_pic)
7545         output_pic_addr_const (file, x, code);
7546       else
7547         output_addr_const (file, x);
7548     }
7549 }
7550 \f
7551 /* Print a memory operand whose address is ADDR.  */
7552
7553 void
7554 print_operand_address (FILE *file, rtx addr)
7555 {
7556   struct ix86_address parts;
7557   rtx base, index, disp;
7558   int scale;
7559
7560   if (! ix86_decompose_address (addr, &parts))
7561     abort ();
7562
7563   base = parts.base;
7564   index = parts.index;
7565   disp = parts.disp;
7566   scale = parts.scale;
7567
7568   switch (parts.seg)
7569     {
7570     case SEG_DEFAULT:
7571       break;
7572     case SEG_FS:
7573     case SEG_GS:
7574       if (USER_LABEL_PREFIX[0] == 0)
7575         putc ('%', file);
7576       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
7577       break;
7578     default:
7579       abort ();
7580     }
7581
7582   if (!base && !index)
7583     {
7584       /* Displacement only requires special attention.  */
7585
7586       if (GET_CODE (disp) == CONST_INT)
7587         {
7588           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
7589             {
7590               if (USER_LABEL_PREFIX[0] == 0)
7591                 putc ('%', file);
7592               fputs ("ds:", file);
7593             }
7594           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
7595         }
7596       else if (flag_pic)
7597         output_pic_addr_const (file, disp, 0);
7598       else
7599         output_addr_const (file, disp);
7600
7601       /* Use one byte shorter RIP relative addressing for 64bit mode.  */
7602       if (TARGET_64BIT
7603           && ((GET_CODE (disp) == SYMBOL_REF
7604                && ! tls_symbolic_operand (disp, GET_MODE (disp)))
7605               || GET_CODE (disp) == LABEL_REF
7606               || (GET_CODE (disp) == CONST
7607                   && GET_CODE (XEXP (disp, 0)) == PLUS
7608                   && (GET_CODE (XEXP (XEXP (disp, 0), 0)) == SYMBOL_REF
7609                       || GET_CODE (XEXP (XEXP (disp, 0), 0)) == LABEL_REF)
7610                   && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)))
7611         fputs ("(%rip)", file);
7612     }
7613   else
7614     {
7615       if (ASSEMBLER_DIALECT == ASM_ATT)
7616         {
7617           if (disp)
7618             {
7619               if (flag_pic)
7620                 output_pic_addr_const (file, disp, 0);
7621               else if (GET_CODE (disp) == LABEL_REF)
7622                 output_asm_label (disp);
7623               else
7624                 output_addr_const (file, disp);
7625             }
7626
7627           putc ('(', file);
7628           if (base)
7629             print_reg (base, 0, file);
7630           if (index)
7631             {
7632               putc (',', file);
7633               print_reg (index, 0, file);
7634               if (scale != 1)
7635                 fprintf (file, ",%d", scale);
7636             }
7637           putc (')', file);
7638         }
7639       else
7640         {
7641           rtx offset = NULL_RTX;
7642
7643           if (disp)
7644             {
7645               /* Pull out the offset of a symbol; print any symbol itself.  */
7646               if (GET_CODE (disp) == CONST
7647                   && GET_CODE (XEXP (disp, 0)) == PLUS
7648                   && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
7649                 {
7650                   offset = XEXP (XEXP (disp, 0), 1);
7651                   disp = gen_rtx_CONST (VOIDmode,
7652                                         XEXP (XEXP (disp, 0), 0));
7653                 }
7654
7655               if (flag_pic)
7656                 output_pic_addr_const (file, disp, 0);
7657               else if (GET_CODE (disp) == LABEL_REF)
7658                 output_asm_label (disp);
7659               else if (GET_CODE (disp) == CONST_INT)
7660                 offset = disp;
7661               else
7662                 output_addr_const (file, disp);
7663             }
7664
7665           putc ('[', file);
7666           if (base)
7667             {
7668               print_reg (base, 0, file);
7669               if (offset)
7670                 {
7671                   if (INTVAL (offset) >= 0)
7672                     putc ('+', file);
7673                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
7674                 }
7675             }
7676           else if (offset)
7677             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
7678           else
7679             putc ('0', file);
7680
7681           if (index)
7682             {
7683               putc ('+', file);
7684               print_reg (index, 0, file);
7685               if (scale != 1)
7686                 fprintf (file, "*%d", scale);
7687             }
7688           putc (']', file);
7689         }
7690     }
7691 }
7692
7693 bool
7694 output_addr_const_extra (FILE *file, rtx x)
7695 {
7696   rtx op;
7697
7698   if (GET_CODE (x) != UNSPEC)
7699     return false;
7700
7701   op = XVECEXP (x, 0, 0);
7702   switch (XINT (x, 1))
7703     {
7704     case UNSPEC_GOTTPOFF:
7705       output_addr_const (file, op);
7706       /* FIXME: This might be @TPOFF in Sun ld.  */
7707       fputs ("@GOTTPOFF", file);
7708       break;
7709     case UNSPEC_TPOFF:
7710       output_addr_const (file, op);
7711       fputs ("@TPOFF", file);
7712       break;
7713     case UNSPEC_NTPOFF:
7714       output_addr_const (file, op);
7715       if (TARGET_64BIT)
7716         fputs ("@TPOFF", file);
7717       else
7718         fputs ("@NTPOFF", file);
7719       break;
7720     case UNSPEC_DTPOFF:
7721       output_addr_const (file, op);
7722       fputs ("@DTPOFF", file);
7723       break;
7724     case UNSPEC_GOTNTPOFF:
7725       output_addr_const (file, op);
7726       if (TARGET_64BIT)
7727         fputs ("@GOTTPOFF(%rip)", file);
7728       else
7729         fputs ("@GOTNTPOFF", file);
7730       break;
7731     case UNSPEC_INDNTPOFF:
7732       output_addr_const (file, op);
7733       fputs ("@INDNTPOFF", file);
7734       break;
7735
7736     default:
7737       return false;
7738     }
7739
7740   return true;
7741 }
7742 \f
7743 /* Split one or more DImode RTL references into pairs of SImode
7744    references.  The RTL can be REG, offsettable MEM, integer constant, or
7745    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
7746    split and "num" is its length.  lo_half and hi_half are output arrays
7747    that parallel "operands".  */
7748
7749 void
7750 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
7751 {
7752   while (num--)
7753     {
7754       rtx op = operands[num];
7755
7756       /* simplify_subreg refuse to split volatile memory addresses,
7757          but we still have to handle it.  */
7758       if (GET_CODE (op) == MEM)
7759         {
7760           lo_half[num] = adjust_address (op, SImode, 0);
7761           hi_half[num] = adjust_address (op, SImode, 4);
7762         }
7763       else
7764         {
7765           lo_half[num] = simplify_gen_subreg (SImode, op,
7766                                               GET_MODE (op) == VOIDmode
7767                                               ? DImode : GET_MODE (op), 0);
7768           hi_half[num] = simplify_gen_subreg (SImode, op,
7769                                               GET_MODE (op) == VOIDmode
7770                                               ? DImode : GET_MODE (op), 4);
7771         }
7772     }
7773 }
7774 /* Split one or more TImode RTL references into pairs of SImode
7775    references.  The RTL can be REG, offsettable MEM, integer constant, or
7776    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
7777    split and "num" is its length.  lo_half and hi_half are output arrays
7778    that parallel "operands".  */
7779
7780 void
7781 split_ti (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
7782 {
7783   while (num--)
7784     {
7785       rtx op = operands[num];
7786
7787       /* simplify_subreg refuse to split volatile memory addresses, but we
7788          still have to handle it.  */
7789       if (GET_CODE (op) == MEM)
7790         {
7791           lo_half[num] = adjust_address (op, DImode, 0);
7792           hi_half[num] = adjust_address (op, DImode, 8);
7793         }
7794       else
7795         {
7796           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
7797           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
7798         }
7799     }
7800 }
7801 \f
7802 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
7803    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
7804    is the expression of the binary operation.  The output may either be
7805    emitted here, or returned to the caller, like all output_* functions.
7806
7807    There is no guarantee that the operands are the same mode, as they
7808    might be within FLOAT or FLOAT_EXTEND expressions.  */
7809
7810 #ifndef SYSV386_COMPAT
7811 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
7812    wants to fix the assemblers because that causes incompatibility
7813    with gcc.  No-one wants to fix gcc because that causes
7814    incompatibility with assemblers...  You can use the option of
7815    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
7816 #define SYSV386_COMPAT 1
7817 #endif
7818
7819 const char *
7820 output_387_binary_op (rtx insn, rtx *operands)
7821 {
7822   static char buf[30];
7823   const char *p;
7824   const char *ssep;
7825   int is_sse = SSE_REG_P (operands[0]) | SSE_REG_P (operands[1]) | SSE_REG_P (operands[2]);
7826
7827 #ifdef ENABLE_CHECKING
7828   /* Even if we do not want to check the inputs, this documents input
7829      constraints.  Which helps in understanding the following code.  */
7830   if (STACK_REG_P (operands[0])
7831       && ((REG_P (operands[1])
7832            && REGNO (operands[0]) == REGNO (operands[1])
7833            && (STACK_REG_P (operands[2]) || GET_CODE (operands[2]) == MEM))
7834           || (REG_P (operands[2])
7835               && REGNO (operands[0]) == REGNO (operands[2])
7836               && (STACK_REG_P (operands[1]) || GET_CODE (operands[1]) == MEM)))
7837       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
7838     ; /* ok */
7839   else if (!is_sse)
7840     abort ();
7841 #endif
7842
7843   switch (GET_CODE (operands[3]))
7844     {
7845     case PLUS:
7846       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7847           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7848         p = "fiadd";
7849       else
7850         p = "fadd";
7851       ssep = "add";
7852       break;
7853
7854     case MINUS:
7855       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7856           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7857         p = "fisub";
7858       else
7859         p = "fsub";
7860       ssep = "sub";
7861       break;
7862
7863     case MULT:
7864       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7865           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7866         p = "fimul";
7867       else
7868         p = "fmul";
7869       ssep = "mul";
7870       break;
7871
7872     case DIV:
7873       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7874           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7875         p = "fidiv";
7876       else
7877         p = "fdiv";
7878       ssep = "div";
7879       break;
7880
7881     default:
7882       abort ();
7883     }
7884
7885   if (is_sse)
7886    {
7887       strcpy (buf, ssep);
7888       if (GET_MODE (operands[0]) == SFmode)
7889         strcat (buf, "ss\t{%2, %0|%0, %2}");
7890       else
7891         strcat (buf, "sd\t{%2, %0|%0, %2}");
7892       return buf;
7893    }
7894   strcpy (buf, p);
7895
7896   switch (GET_CODE (operands[3]))
7897     {
7898     case MULT:
7899     case PLUS:
7900       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
7901         {
7902           rtx temp = operands[2];
7903           operands[2] = operands[1];
7904           operands[1] = temp;
7905         }
7906
7907       /* know operands[0] == operands[1].  */
7908
7909       if (GET_CODE (operands[2]) == MEM)
7910         {
7911           p = "%z2\t%2";
7912           break;
7913         }
7914
7915       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
7916         {
7917           if (STACK_TOP_P (operands[0]))
7918             /* How is it that we are storing to a dead operand[2]?
7919                Well, presumably operands[1] is dead too.  We can't
7920                store the result to st(0) as st(0) gets popped on this
7921                instruction.  Instead store to operands[2] (which I
7922                think has to be st(1)).  st(1) will be popped later.
7923                gcc <= 2.8.1 didn't have this check and generated
7924                assembly code that the Unixware assembler rejected.  */
7925             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
7926           else
7927             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
7928           break;
7929         }
7930
7931       if (STACK_TOP_P (operands[0]))
7932         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
7933       else
7934         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
7935       break;
7936
7937     case MINUS:
7938     case DIV:
7939       if (GET_CODE (operands[1]) == MEM)
7940         {
7941           p = "r%z1\t%1";
7942           break;
7943         }
7944
7945       if (GET_CODE (operands[2]) == MEM)
7946         {
7947           p = "%z2\t%2";
7948           break;
7949         }
7950
7951       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
7952         {
7953 #if SYSV386_COMPAT
7954           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
7955              derived assemblers, confusingly reverse the direction of
7956              the operation for fsub{r} and fdiv{r} when the
7957              destination register is not st(0).  The Intel assembler
7958              doesn't have this brain damage.  Read !SYSV386_COMPAT to
7959              figure out what the hardware really does.  */
7960           if (STACK_TOP_P (operands[0]))
7961             p = "{p\t%0, %2|rp\t%2, %0}";
7962           else
7963             p = "{rp\t%2, %0|p\t%0, %2}";
7964 #else
7965           if (STACK_TOP_P (operands[0]))
7966             /* As above for fmul/fadd, we can't store to st(0).  */
7967             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
7968           else
7969             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
7970 #endif
7971           break;
7972         }
7973
7974       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
7975         {
7976 #if SYSV386_COMPAT
7977           if (STACK_TOP_P (operands[0]))
7978             p = "{rp\t%0, %1|p\t%1, %0}";
7979           else
7980             p = "{p\t%1, %0|rp\t%0, %1}";
7981 #else
7982           if (STACK_TOP_P (operands[0]))
7983             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
7984           else
7985             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
7986 #endif
7987           break;
7988         }
7989
7990       if (STACK_TOP_P (operands[0]))
7991         {
7992           if (STACK_TOP_P (operands[1]))
7993             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
7994           else
7995             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
7996           break;
7997         }
7998       else if (STACK_TOP_P (operands[1]))
7999         {
8000 #if SYSV386_COMPAT
8001           p = "{\t%1, %0|r\t%0, %1}";
8002 #else
8003           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
8004 #endif
8005         }
8006       else
8007         {
8008 #if SYSV386_COMPAT
8009           p = "{r\t%2, %0|\t%0, %2}";
8010 #else
8011           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
8012 #endif
8013         }
8014       break;
8015
8016     default:
8017       abort ();
8018     }
8019
8020   strcat (buf, p);
8021   return buf;
8022 }
8023
8024 /* Output code to initialize control word copies used by
8025    trunc?f?i patterns.  NORMAL is set to current control word, while ROUND_DOWN
8026    is set to control word rounding downwards.  */
8027 void
8028 emit_i387_cw_initialization (rtx normal, rtx round_down)
8029 {
8030   rtx reg = gen_reg_rtx (HImode);
8031
8032   emit_insn (gen_x86_fnstcw_1 (normal));
8033   emit_move_insn (reg, normal);
8034   if (!TARGET_PARTIAL_REG_STALL && !optimize_size
8035       && !TARGET_64BIT)
8036     emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
8037   else
8038     emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0xc00)));
8039   emit_move_insn (round_down, reg);
8040 }
8041
8042 /* Output code for INSN to convert a float to a signed int.  OPERANDS
8043    are the insn operands.  The output may be [HSD]Imode and the input
8044    operand may be [SDX]Fmode.  */
8045
8046 const char *
8047 output_fix_trunc (rtx insn, rtx *operands)
8048 {
8049   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
8050   int dimode_p = GET_MODE (operands[0]) == DImode;
8051
8052   /* Jump through a hoop or two for DImode, since the hardware has no
8053      non-popping instruction.  We used to do this a different way, but
8054      that was somewhat fragile and broke with post-reload splitters.  */
8055   if (dimode_p && !stack_top_dies)
8056     output_asm_insn ("fld\t%y1", operands);
8057
8058   if (!STACK_TOP_P (operands[1]))
8059     abort ();
8060
8061   if (GET_CODE (operands[0]) != MEM)
8062     abort ();
8063
8064   output_asm_insn ("fldcw\t%3", operands);
8065   if (stack_top_dies || dimode_p)
8066     output_asm_insn ("fistp%z0\t%0", operands);
8067   else
8068     output_asm_insn ("fist%z0\t%0", operands);
8069   output_asm_insn ("fldcw\t%2", operands);
8070
8071   return "";
8072 }
8073
8074 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
8075    should be used and 2 when fnstsw should be used.  UNORDERED_P is true
8076    when fucom should be used.  */
8077
8078 const char *
8079 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
8080 {
8081   int stack_top_dies;
8082   rtx cmp_op0 = operands[0];
8083   rtx cmp_op1 = operands[1];
8084   int is_sse = SSE_REG_P (operands[0]) | SSE_REG_P (operands[1]);
8085
8086   if (eflags_p == 2)
8087     {
8088       cmp_op0 = cmp_op1;
8089       cmp_op1 = operands[2];
8090     }
8091   if (is_sse)
8092     {
8093       if (GET_MODE (operands[0]) == SFmode)
8094         if (unordered_p)
8095           return "ucomiss\t{%1, %0|%0, %1}";
8096         else
8097           return "comiss\t{%1, %0|%0, %1}";
8098       else
8099         if (unordered_p)
8100           return "ucomisd\t{%1, %0|%0, %1}";
8101         else
8102           return "comisd\t{%1, %0|%0, %1}";
8103     }
8104
8105   if (! STACK_TOP_P (cmp_op0))
8106     abort ();
8107
8108   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
8109
8110   if (STACK_REG_P (cmp_op1)
8111       && stack_top_dies
8112       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
8113       && REGNO (cmp_op1) != FIRST_STACK_REG)
8114     {
8115       /* If both the top of the 387 stack dies, and the other operand
8116          is also a stack register that dies, then this must be a
8117          `fcompp' float compare */
8118
8119       if (eflags_p == 1)
8120         {
8121           /* There is no double popping fcomi variant.  Fortunately,
8122              eflags is immune from the fstp's cc clobbering.  */
8123           if (unordered_p)
8124             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
8125           else
8126             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
8127           return "fstp\t%y0";
8128         }
8129       else
8130         {
8131           if (eflags_p == 2)
8132             {
8133               if (unordered_p)
8134                 return "fucompp\n\tfnstsw\t%0";
8135               else
8136                 return "fcompp\n\tfnstsw\t%0";
8137             }
8138           else
8139             {
8140               if (unordered_p)
8141                 return "fucompp";
8142               else
8143                 return "fcompp";
8144             }
8145         }
8146     }
8147   else
8148     {
8149       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
8150
8151       static const char * const alt[24] =
8152       {
8153         "fcom%z1\t%y1",
8154         "fcomp%z1\t%y1",
8155         "fucom%z1\t%y1",
8156         "fucomp%z1\t%y1",
8157
8158         "ficom%z1\t%y1",
8159         "ficomp%z1\t%y1",
8160         NULL,
8161         NULL,
8162
8163         "fcomi\t{%y1, %0|%0, %y1}",
8164         "fcomip\t{%y1, %0|%0, %y1}",
8165         "fucomi\t{%y1, %0|%0, %y1}",
8166         "fucomip\t{%y1, %0|%0, %y1}",
8167
8168         NULL,
8169         NULL,
8170         NULL,
8171         NULL,
8172
8173         "fcom%z2\t%y2\n\tfnstsw\t%0",
8174         "fcomp%z2\t%y2\n\tfnstsw\t%0",
8175         "fucom%z2\t%y2\n\tfnstsw\t%0",
8176         "fucomp%z2\t%y2\n\tfnstsw\t%0",
8177
8178         "ficom%z2\t%y2\n\tfnstsw\t%0",
8179         "ficomp%z2\t%y2\n\tfnstsw\t%0",
8180         NULL,
8181         NULL
8182       };
8183
8184       int mask;
8185       const char *ret;
8186
8187       mask  = eflags_p << 3;
8188       mask |= (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT) << 2;
8189       mask |= unordered_p << 1;
8190       mask |= stack_top_dies;
8191
8192       if (mask >= 24)
8193         abort ();
8194       ret = alt[mask];
8195       if (ret == NULL)
8196         abort ();
8197
8198       return ret;
8199     }
8200 }
8201
8202 void
8203 ix86_output_addr_vec_elt (FILE *file, int value)
8204 {
8205   const char *directive = ASM_LONG;
8206
8207   if (TARGET_64BIT)
8208     {
8209 #ifdef ASM_QUAD
8210       directive = ASM_QUAD;
8211 #else
8212       abort ();
8213 #endif
8214     }
8215
8216   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
8217 }
8218
8219 void
8220 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
8221 {
8222   if (TARGET_64BIT)
8223     fprintf (file, "%s%s%d-%s%d\n",
8224              ASM_LONG, LPREFIX, value, LPREFIX, rel);
8225   else if (HAVE_AS_GOTOFF_IN_DATA)
8226     fprintf (file, "%s%s%d@GOTOFF\n", ASM_LONG, LPREFIX, value);
8227 #if TARGET_MACHO
8228   else if (TARGET_MACHO)
8229     {
8230       fprintf (file, "%s%s%d-", ASM_LONG, LPREFIX, value);
8231       machopic_output_function_base_name (file);
8232       fprintf(file, "\n");
8233     }
8234 #endif
8235   else
8236     asm_fprintf (file, "%s%U%s+[.-%s%d]\n",
8237                  ASM_LONG, GOT_SYMBOL_NAME, LPREFIX, value);
8238 }
8239 \f
8240 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
8241    for the target.  */
8242
8243 void
8244 ix86_expand_clear (rtx dest)
8245 {
8246   rtx tmp;
8247
8248   /* We play register width games, which are only valid after reload.  */
8249   if (!reload_completed)
8250     abort ();
8251
8252   /* Avoid HImode and its attendant prefix byte.  */
8253   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
8254     dest = gen_rtx_REG (SImode, REGNO (dest));
8255
8256   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
8257
8258   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
8259   if (reload_completed && (!TARGET_USE_MOV0 || optimize_size))
8260     {
8261       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, 17));
8262       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
8263     }
8264
8265   emit_insn (tmp);
8266 }
8267
8268 /* X is an unchanging MEM.  If it is a constant pool reference, return
8269    the constant pool rtx, else NULL.  */
8270
8271 static rtx
8272 maybe_get_pool_constant (rtx x)
8273 {
8274   x = ix86_delegitimize_address (XEXP (x, 0));
8275
8276   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
8277     return get_pool_constant (x);
8278
8279   return NULL_RTX;
8280 }
8281
8282 void
8283 ix86_expand_move (enum machine_mode mode, rtx operands[])
8284 {
8285   int strict = (reload_in_progress || reload_completed);
8286   rtx op0, op1;
8287   enum tls_model model;
8288
8289   op0 = operands[0];
8290   op1 = operands[1];
8291
8292   model = tls_symbolic_operand (op1, Pmode);
8293   if (model)
8294     {
8295       op1 = legitimize_tls_address (op1, model, true);
8296       op1 = force_operand (op1, op0);
8297       if (op1 == op0)
8298         return;
8299     }
8300
8301   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
8302     {
8303 #if TARGET_MACHO
8304       if (MACHOPIC_PURE)
8305         {
8306           rtx temp = ((reload_in_progress
8307                        || ((op0 && GET_CODE (op0) == REG)
8308                            && mode == Pmode))
8309                       ? op0 : gen_reg_rtx (Pmode));
8310           op1 = machopic_indirect_data_reference (op1, temp);
8311           op1 = machopic_legitimize_pic_address (op1, mode,
8312                                                  temp == op1 ? 0 : temp);
8313         }
8314       else if (MACHOPIC_INDIRECT)
8315         op1 = machopic_indirect_data_reference (op1, 0);
8316       if (op0 == op1)
8317         return;
8318 #else
8319       if (GET_CODE (op0) == MEM)
8320         op1 = force_reg (Pmode, op1);
8321       else
8322         {
8323           rtx temp = op0;
8324           if (GET_CODE (temp) != REG)
8325             temp = gen_reg_rtx (Pmode);
8326           temp = legitimize_pic_address (op1, temp);
8327           if (temp == op0)
8328             return;
8329           op1 = temp;
8330         }
8331 #endif /* TARGET_MACHO */
8332     }
8333   else
8334     {
8335       if (GET_CODE (op0) == MEM
8336           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
8337               || !push_operand (op0, mode))
8338           && GET_CODE (op1) == MEM)
8339         op1 = force_reg (mode, op1);
8340
8341       if (push_operand (op0, mode)
8342           && ! general_no_elim_operand (op1, mode))
8343         op1 = copy_to_mode_reg (mode, op1);
8344
8345       /* Force large constants in 64bit compilation into register
8346          to get them CSEed.  */
8347       if (TARGET_64BIT && mode == DImode
8348           && immediate_operand (op1, mode)
8349           && !x86_64_zero_extended_value (op1)
8350           && !register_operand (op0, mode)
8351           && optimize && !reload_completed && !reload_in_progress)
8352         op1 = copy_to_mode_reg (mode, op1);
8353
8354       if (FLOAT_MODE_P (mode))
8355         {
8356           /* If we are loading a floating point constant to a register,
8357              force the value to memory now, since we'll get better code
8358              out the back end.  */
8359
8360           if (strict)
8361             ;
8362           else if (GET_CODE (op1) == CONST_DOUBLE)
8363             {
8364               op1 = validize_mem (force_const_mem (mode, op1));
8365               if (!register_operand (op0, mode))
8366                 {
8367                   rtx temp = gen_reg_rtx (mode);
8368                   emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
8369                   emit_move_insn (op0, temp);
8370                   return;
8371                 }
8372             }
8373         }
8374     }
8375
8376   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
8377 }
8378
8379 void
8380 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
8381 {
8382   /* Force constants other than zero into memory.  We do not know how
8383      the instructions used to build constants modify the upper 64 bits
8384      of the register, once we have that information we may be able
8385      to handle some of them more efficiently.  */
8386   if ((reload_in_progress | reload_completed) == 0
8387       && register_operand (operands[0], mode)
8388       && CONSTANT_P (operands[1]) && operands[1] != CONST0_RTX (mode))
8389     operands[1] = validize_mem (force_const_mem (mode, operands[1]));
8390
8391   /* Make operand1 a register if it isn't already.  */
8392   if (!no_new_pseudos
8393       && !register_operand (operands[0], mode)
8394       && !register_operand (operands[1], mode))
8395     {
8396       rtx temp = force_reg (GET_MODE (operands[1]), operands[1]);
8397       emit_move_insn (operands[0], temp);
8398       return;
8399     }
8400
8401   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8402 }
8403
8404 /* Attempt to expand a binary operator.  Make the expansion closer to the
8405    actual machine, then just general_operand, which will allow 3 separate
8406    memory references (one output, two input) in a single insn.  */
8407
8408 void
8409 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
8410                              rtx operands[])
8411 {
8412   int matching_memory;
8413   rtx src1, src2, dst, op, clob;
8414
8415   dst = operands[0];
8416   src1 = operands[1];
8417   src2 = operands[2];
8418
8419   /* Recognize <var1> = <value> <op> <var1> for commutative operators */
8420   if (GET_RTX_CLASS (code) == 'c'
8421       && (rtx_equal_p (dst, src2)
8422           || immediate_operand (src1, mode)))
8423     {
8424       rtx temp = src1;
8425       src1 = src2;
8426       src2 = temp;
8427     }
8428
8429   /* If the destination is memory, and we do not have matching source
8430      operands, do things in registers.  */
8431   matching_memory = 0;
8432   if (GET_CODE (dst) == MEM)
8433     {
8434       if (rtx_equal_p (dst, src1))
8435         matching_memory = 1;
8436       else if (GET_RTX_CLASS (code) == 'c'
8437                && rtx_equal_p (dst, src2))
8438         matching_memory = 2;
8439       else
8440         dst = gen_reg_rtx (mode);
8441     }
8442
8443   /* Both source operands cannot be in memory.  */
8444   if (GET_CODE (src1) == MEM && GET_CODE (src2) == MEM)
8445     {
8446       if (matching_memory != 2)
8447         src2 = force_reg (mode, src2);
8448       else
8449         src1 = force_reg (mode, src1);
8450     }
8451
8452   /* If the operation is not commutable, source 1 cannot be a constant
8453      or non-matching memory.  */
8454   if ((CONSTANT_P (src1)
8455        || (!matching_memory && GET_CODE (src1) == MEM))
8456       && GET_RTX_CLASS (code) != 'c')
8457     src1 = force_reg (mode, src1);
8458
8459   /* If optimizing, copy to regs to improve CSE */
8460   if (optimize && ! no_new_pseudos)
8461     {
8462       if (GET_CODE (dst) == MEM)
8463         dst = gen_reg_rtx (mode);
8464       if (GET_CODE (src1) == MEM)
8465         src1 = force_reg (mode, src1);
8466       if (GET_CODE (src2) == MEM)
8467         src2 = force_reg (mode, src2);
8468     }
8469
8470   /* Emit the instruction.  */
8471
8472   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
8473   if (reload_in_progress)
8474     {
8475       /* Reload doesn't know about the flags register, and doesn't know that
8476          it doesn't want to clobber it.  We can only do this with PLUS.  */
8477       if (code != PLUS)
8478         abort ();
8479       emit_insn (op);
8480     }
8481   else
8482     {
8483       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
8484       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
8485     }
8486
8487   /* Fix up the destination if needed.  */
8488   if (dst != operands[0])
8489     emit_move_insn (operands[0], dst);
8490 }
8491
8492 /* Return TRUE or FALSE depending on whether the binary operator meets the
8493    appropriate constraints.  */
8494
8495 int
8496 ix86_binary_operator_ok (enum rtx_code code,
8497                          enum machine_mode mode ATTRIBUTE_UNUSED,
8498                          rtx operands[3])
8499 {
8500   /* Both source operands cannot be in memory.  */
8501   if (GET_CODE (operands[1]) == MEM && GET_CODE (operands[2]) == MEM)
8502     return 0;
8503   /* If the operation is not commutable, source 1 cannot be a constant.  */
8504   if (CONSTANT_P (operands[1]) && GET_RTX_CLASS (code) != 'c')
8505     return 0;
8506   /* If the destination is memory, we must have a matching source operand.  */
8507   if (GET_CODE (operands[0]) == MEM
8508       && ! (rtx_equal_p (operands[0], operands[1])
8509             || (GET_RTX_CLASS (code) == 'c'
8510                 && rtx_equal_p (operands[0], operands[2]))))
8511     return 0;
8512   /* If the operation is not commutable and the source 1 is memory, we must
8513      have a matching destination.  */
8514   if (GET_CODE (operands[1]) == MEM
8515       && GET_RTX_CLASS (code) != 'c'
8516       && ! rtx_equal_p (operands[0], operands[1]))
8517     return 0;
8518   return 1;
8519 }
8520
8521 /* Attempt to expand a unary operator.  Make the expansion closer to the
8522    actual machine, then just general_operand, which will allow 2 separate
8523    memory references (one output, one input) in a single insn.  */
8524
8525 void
8526 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
8527                             rtx operands[])
8528 {
8529   int matching_memory;
8530   rtx src, dst, op, clob;
8531
8532   dst = operands[0];
8533   src = operands[1];
8534
8535   /* If the destination is memory, and we do not have matching source
8536      operands, do things in registers.  */
8537   matching_memory = 0;
8538   if (GET_CODE (dst) == MEM)
8539     {
8540       if (rtx_equal_p (dst, src))
8541         matching_memory = 1;
8542       else
8543         dst = gen_reg_rtx (mode);
8544     }
8545
8546   /* When source operand is memory, destination must match.  */
8547   if (!matching_memory && GET_CODE (src) == MEM)
8548     src = force_reg (mode, src);
8549
8550   /* If optimizing, copy to regs to improve CSE */
8551   if (optimize && ! no_new_pseudos)
8552     {
8553       if (GET_CODE (dst) == MEM)
8554         dst = gen_reg_rtx (mode);
8555       if (GET_CODE (src) == MEM)
8556         src = force_reg (mode, src);
8557     }
8558
8559   /* Emit the instruction.  */
8560
8561   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
8562   if (reload_in_progress || code == NOT)
8563     {
8564       /* Reload doesn't know about the flags register, and doesn't know that
8565          it doesn't want to clobber it.  */
8566       if (code != NOT)
8567         abort ();
8568       emit_insn (op);
8569     }
8570   else
8571     {
8572       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
8573       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
8574     }
8575
8576   /* Fix up the destination if needed.  */
8577   if (dst != operands[0])
8578     emit_move_insn (operands[0], dst);
8579 }
8580
8581 /* Return TRUE or FALSE depending on whether the unary operator meets the
8582    appropriate constraints.  */
8583
8584 int
8585 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
8586                         enum machine_mode mode ATTRIBUTE_UNUSED,
8587                         rtx operands[2] ATTRIBUTE_UNUSED)
8588 {
8589   /* If one of operands is memory, source and destination must match.  */
8590   if ((GET_CODE (operands[0]) == MEM
8591        || GET_CODE (operands[1]) == MEM)
8592       && ! rtx_equal_p (operands[0], operands[1]))
8593     return FALSE;
8594   return TRUE;
8595 }
8596
8597 /* Return TRUE or FALSE depending on whether the first SET in INSN
8598    has source and destination with matching CC modes, and that the
8599    CC mode is at least as constrained as REQ_MODE.  */
8600
8601 int
8602 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
8603 {
8604   rtx set;
8605   enum machine_mode set_mode;
8606
8607   set = PATTERN (insn);
8608   if (GET_CODE (set) == PARALLEL)
8609     set = XVECEXP (set, 0, 0);
8610   if (GET_CODE (set) != SET)
8611     abort ();
8612   if (GET_CODE (SET_SRC (set)) != COMPARE)
8613     abort ();
8614
8615   set_mode = GET_MODE (SET_DEST (set));
8616   switch (set_mode)
8617     {
8618     case CCNOmode:
8619       if (req_mode != CCNOmode
8620           && (req_mode != CCmode
8621               || XEXP (SET_SRC (set), 1) != const0_rtx))
8622         return 0;
8623       break;
8624     case CCmode:
8625       if (req_mode == CCGCmode)
8626         return 0;
8627       /* FALLTHRU */
8628     case CCGCmode:
8629       if (req_mode == CCGOCmode || req_mode == CCNOmode)
8630         return 0;
8631       /* FALLTHRU */
8632     case CCGOCmode:
8633       if (req_mode == CCZmode)
8634         return 0;
8635       /* FALLTHRU */
8636     case CCZmode:
8637       break;
8638
8639     default:
8640       abort ();
8641     }
8642
8643   return (GET_MODE (SET_SRC (set)) == set_mode);
8644 }
8645
8646 /* Generate insn patterns to do an integer compare of OPERANDS.  */
8647
8648 static rtx
8649 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
8650 {
8651   enum machine_mode cmpmode;
8652   rtx tmp, flags;
8653
8654   cmpmode = SELECT_CC_MODE (code, op0, op1);
8655   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
8656
8657   /* This is very simple, but making the interface the same as in the
8658      FP case makes the rest of the code easier.  */
8659   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
8660   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
8661
8662   /* Return the test that should be put into the flags user, i.e.
8663      the bcc, scc, or cmov instruction.  */
8664   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
8665 }
8666
8667 /* Figure out whether to use ordered or unordered fp comparisons.
8668    Return the appropriate mode to use.  */
8669
8670 enum machine_mode
8671 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
8672 {
8673   /* ??? In order to make all comparisons reversible, we do all comparisons
8674      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
8675      all forms trapping and nontrapping comparisons, we can make inequality
8676      comparisons trapping again, since it results in better code when using
8677      FCOM based compares.  */
8678   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
8679 }
8680
8681 enum machine_mode
8682 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
8683 {
8684   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
8685     return ix86_fp_compare_mode (code);
8686   switch (code)
8687     {
8688       /* Only zero flag is needed.  */
8689     case EQ:                    /* ZF=0 */
8690     case NE:                    /* ZF!=0 */
8691       return CCZmode;
8692       /* Codes needing carry flag.  */
8693     case GEU:                   /* CF=0 */
8694     case GTU:                   /* CF=0 & ZF=0 */
8695     case LTU:                   /* CF=1 */
8696     case LEU:                   /* CF=1 | ZF=1 */
8697       return CCmode;
8698       /* Codes possibly doable only with sign flag when
8699          comparing against zero.  */
8700     case GE:                    /* SF=OF   or   SF=0 */
8701     case LT:                    /* SF<>OF  or   SF=1 */
8702       if (op1 == const0_rtx)
8703         return CCGOCmode;
8704       else
8705         /* For other cases Carry flag is not required.  */
8706         return CCGCmode;
8707       /* Codes doable only with sign flag when comparing
8708          against zero, but we miss jump instruction for it
8709          so we need to use relational tests against overflow
8710          that thus needs to be zero.  */
8711     case GT:                    /* ZF=0 & SF=OF */
8712     case LE:                    /* ZF=1 | SF<>OF */
8713       if (op1 == const0_rtx)
8714         return CCNOmode;
8715       else
8716         return CCGCmode;
8717       /* strcmp pattern do (use flags) and combine may ask us for proper
8718          mode.  */
8719     case USE:
8720       return CCmode;
8721     default:
8722       abort ();
8723     }
8724 }
8725
8726 /* Return true if we should use an FCOMI instruction for this fp comparison.  */
8727
8728 int
8729 ix86_use_fcomi_compare (enum rtx_code code ATTRIBUTE_UNUSED)
8730 {
8731   enum rtx_code swapped_code = swap_condition (code);
8732   return ((ix86_fp_comparison_cost (code) == ix86_fp_comparison_fcomi_cost (code))
8733           || (ix86_fp_comparison_cost (swapped_code)
8734               == ix86_fp_comparison_fcomi_cost (swapped_code)));
8735 }
8736
8737 /* Swap, force into registers, or otherwise massage the two operands
8738    to a fp comparison.  The operands are updated in place; the new
8739    comparison code is returned.  */
8740
8741 static enum rtx_code
8742 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
8743 {
8744   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
8745   rtx op0 = *pop0, op1 = *pop1;
8746   enum machine_mode op_mode = GET_MODE (op0);
8747   int is_sse = SSE_REG_P (op0) | SSE_REG_P (op1);
8748
8749   /* All of the unordered compare instructions only work on registers.
8750      The same is true of the XFmode compare instructions.  The same is
8751      true of the fcomi compare instructions.  */
8752
8753   if (!is_sse
8754       && (fpcmp_mode == CCFPUmode
8755           || op_mode == XFmode
8756           || ix86_use_fcomi_compare (code)))
8757     {
8758       op0 = force_reg (op_mode, op0);
8759       op1 = force_reg (op_mode, op1);
8760     }
8761   else
8762     {
8763       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
8764          things around if they appear profitable, otherwise force op0
8765          into a register.  */
8766
8767       if (standard_80387_constant_p (op0) == 0
8768           || (GET_CODE (op0) == MEM
8769               && ! (standard_80387_constant_p (op1) == 0
8770                     || GET_CODE (op1) == MEM)))
8771         {
8772           rtx tmp;
8773           tmp = op0, op0 = op1, op1 = tmp;
8774           code = swap_condition (code);
8775         }
8776
8777       if (GET_CODE (op0) != REG)
8778         op0 = force_reg (op_mode, op0);
8779
8780       if (CONSTANT_P (op1))
8781         {
8782           if (standard_80387_constant_p (op1))
8783             op1 = force_reg (op_mode, op1);
8784           else
8785             op1 = validize_mem (force_const_mem (op_mode, op1));
8786         }
8787     }
8788
8789   /* Try to rearrange the comparison to make it cheaper.  */
8790   if (ix86_fp_comparison_cost (code)
8791       > ix86_fp_comparison_cost (swap_condition (code))
8792       && (GET_CODE (op1) == REG || !no_new_pseudos))
8793     {
8794       rtx tmp;
8795       tmp = op0, op0 = op1, op1 = tmp;
8796       code = swap_condition (code);
8797       if (GET_CODE (op0) != REG)
8798         op0 = force_reg (op_mode, op0);
8799     }
8800
8801   *pop0 = op0;
8802   *pop1 = op1;
8803   return code;
8804 }
8805
8806 /* Convert comparison codes we use to represent FP comparison to integer
8807    code that will result in proper branch.  Return UNKNOWN if no such code
8808    is available.  */
8809 static enum rtx_code
8810 ix86_fp_compare_code_to_integer (enum rtx_code code)
8811 {
8812   switch (code)
8813     {
8814     case GT:
8815       return GTU;
8816     case GE:
8817       return GEU;
8818     case ORDERED:
8819     case UNORDERED:
8820       return code;
8821       break;
8822     case UNEQ:
8823       return EQ;
8824       break;
8825     case UNLT:
8826       return LTU;
8827       break;
8828     case UNLE:
8829       return LEU;
8830       break;
8831     case LTGT:
8832       return NE;
8833       break;
8834     default:
8835       return UNKNOWN;
8836     }
8837 }
8838
8839 /* Split comparison code CODE into comparisons we can do using branch
8840    instructions.  BYPASS_CODE is comparison code for branch that will
8841    branch around FIRST_CODE and SECOND_CODE.  If some of branches
8842    is not required, set value to NIL.
8843    We never require more than two branches.  */
8844 static void
8845 ix86_fp_comparison_codes (enum rtx_code code, enum rtx_code *bypass_code,
8846                           enum rtx_code *first_code,
8847                           enum rtx_code *second_code)
8848 {
8849   *first_code = code;
8850   *bypass_code = NIL;
8851   *second_code = NIL;
8852
8853   /* The fcomi comparison sets flags as follows:
8854
8855      cmp    ZF PF CF
8856      >      0  0  0
8857      <      0  0  1
8858      =      1  0  0
8859      un     1  1  1 */
8860
8861   switch (code)
8862     {
8863     case GT:                    /* GTU - CF=0 & ZF=0 */
8864     case GE:                    /* GEU - CF=0 */
8865     case ORDERED:               /* PF=0 */
8866     case UNORDERED:             /* PF=1 */
8867     case UNEQ:                  /* EQ - ZF=1 */
8868     case UNLT:                  /* LTU - CF=1 */
8869     case UNLE:                  /* LEU - CF=1 | ZF=1 */
8870     case LTGT:                  /* EQ - ZF=0 */
8871       break;
8872     case LT:                    /* LTU - CF=1 - fails on unordered */
8873       *first_code = UNLT;
8874       *bypass_code = UNORDERED;
8875       break;
8876     case LE:                    /* LEU - CF=1 | ZF=1 - fails on unordered */
8877       *first_code = UNLE;
8878       *bypass_code = UNORDERED;
8879       break;
8880     case EQ:                    /* EQ - ZF=1 - fails on unordered */
8881       *first_code = UNEQ;
8882       *bypass_code = UNORDERED;
8883       break;
8884     case NE:                    /* NE - ZF=0 - fails on unordered */
8885       *first_code = LTGT;
8886       *second_code = UNORDERED;
8887       break;
8888     case UNGE:                  /* GEU - CF=0 - fails on unordered */
8889       *first_code = GE;
8890       *second_code = UNORDERED;
8891       break;
8892     case UNGT:                  /* GTU - CF=0 & ZF=0 - fails on unordered */
8893       *first_code = GT;
8894       *second_code = UNORDERED;
8895       break;
8896     default:
8897       abort ();
8898     }
8899   if (!TARGET_IEEE_FP)
8900     {
8901       *second_code = NIL;
8902       *bypass_code = NIL;
8903     }
8904 }
8905
8906 /* Return cost of comparison done fcom + arithmetics operations on AX.
8907    All following functions do use number of instructions as a cost metrics.
8908    In future this should be tweaked to compute bytes for optimize_size and
8909    take into account performance of various instructions on various CPUs.  */
8910 static int
8911 ix86_fp_comparison_arithmetics_cost (enum rtx_code code)
8912 {
8913   if (!TARGET_IEEE_FP)
8914     return 4;
8915   /* The cost of code output by ix86_expand_fp_compare.  */
8916   switch (code)
8917     {
8918     case UNLE:
8919     case UNLT:
8920     case LTGT:
8921     case GT:
8922     case GE:
8923     case UNORDERED:
8924     case ORDERED:
8925     case UNEQ:
8926       return 4;
8927       break;
8928     case LT:
8929     case NE:
8930     case EQ:
8931     case UNGE:
8932       return 5;
8933       break;
8934     case LE:
8935     case UNGT:
8936       return 6;
8937       break;
8938     default:
8939       abort ();
8940     }
8941 }
8942
8943 /* Return cost of comparison done using fcomi operation.
8944    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
8945 static int
8946 ix86_fp_comparison_fcomi_cost (enum rtx_code code)
8947 {
8948   enum rtx_code bypass_code, first_code, second_code;
8949   /* Return arbitrarily high cost when instruction is not supported - this
8950      prevents gcc from using it.  */
8951   if (!TARGET_CMOVE)
8952     return 1024;
8953   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
8954   return (bypass_code != NIL || second_code != NIL) + 2;
8955 }
8956
8957 /* Return cost of comparison done using sahf operation.
8958    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
8959 static int
8960 ix86_fp_comparison_sahf_cost (enum rtx_code code)
8961 {
8962   enum rtx_code bypass_code, first_code, second_code;
8963   /* Return arbitrarily high cost when instruction is not preferred - this
8964      avoids gcc from using it.  */
8965   if (!TARGET_USE_SAHF && !optimize_size)
8966     return 1024;
8967   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
8968   return (bypass_code != NIL || second_code != NIL) + 3;
8969 }
8970
8971 /* Compute cost of the comparison done using any method.
8972    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
8973 static int
8974 ix86_fp_comparison_cost (enum rtx_code code)
8975 {
8976   int fcomi_cost, sahf_cost, arithmetics_cost = 1024;
8977   int min;
8978
8979   fcomi_cost = ix86_fp_comparison_fcomi_cost (code);
8980   sahf_cost = ix86_fp_comparison_sahf_cost (code);
8981
8982   min = arithmetics_cost = ix86_fp_comparison_arithmetics_cost (code);
8983   if (min > sahf_cost)
8984     min = sahf_cost;
8985   if (min > fcomi_cost)
8986     min = fcomi_cost;
8987   return min;
8988 }
8989
8990 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
8991
8992 static rtx
8993 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch,
8994                         rtx *second_test, rtx *bypass_test)
8995 {
8996   enum machine_mode fpcmp_mode, intcmp_mode;
8997   rtx tmp, tmp2;
8998   int cost = ix86_fp_comparison_cost (code);
8999   enum rtx_code bypass_code, first_code, second_code;
9000
9001   fpcmp_mode = ix86_fp_compare_mode (code);
9002   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
9003
9004   if (second_test)
9005     *second_test = NULL_RTX;
9006   if (bypass_test)
9007     *bypass_test = NULL_RTX;
9008
9009   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
9010
9011   /* Do fcomi/sahf based test when profitable.  */
9012   if ((bypass_code == NIL || bypass_test)
9013       && (second_code == NIL || second_test)
9014       && ix86_fp_comparison_arithmetics_cost (code) > cost)
9015     {
9016       if (TARGET_CMOVE)
9017         {
9018           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
9019           tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
9020                              tmp);
9021           emit_insn (tmp);
9022         }
9023       else
9024         {
9025           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
9026           tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
9027           if (!scratch)
9028             scratch = gen_reg_rtx (HImode);
9029           emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
9030           emit_insn (gen_x86_sahf_1 (scratch));
9031         }
9032
9033       /* The FP codes work out to act like unsigned.  */
9034       intcmp_mode = fpcmp_mode;
9035       code = first_code;
9036       if (bypass_code != NIL)
9037         *bypass_test = gen_rtx_fmt_ee (bypass_code, VOIDmode,
9038                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
9039                                        const0_rtx);
9040       if (second_code != NIL)
9041         *second_test = gen_rtx_fmt_ee (second_code, VOIDmode,
9042                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
9043                                        const0_rtx);
9044     }
9045   else
9046     {
9047       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
9048       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
9049       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
9050       if (!scratch)
9051         scratch = gen_reg_rtx (HImode);
9052       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
9053
9054       /* In the unordered case, we have to check C2 for NaN's, which
9055          doesn't happen to work out to anything nice combination-wise.
9056          So do some bit twiddling on the value we've got in AH to come
9057          up with an appropriate set of condition codes.  */
9058
9059       intcmp_mode = CCNOmode;
9060       switch (code)
9061         {
9062         case GT:
9063         case UNGT:
9064           if (code == GT || !TARGET_IEEE_FP)
9065             {
9066               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
9067               code = EQ;
9068             }
9069           else
9070             {
9071               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9072               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
9073               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
9074               intcmp_mode = CCmode;
9075               code = GEU;
9076             }
9077           break;
9078         case LT:
9079         case UNLT:
9080           if (code == LT && TARGET_IEEE_FP)
9081             {
9082               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9083               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x01)));
9084               intcmp_mode = CCmode;
9085               code = EQ;
9086             }
9087           else
9088             {
9089               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x01)));
9090               code = NE;
9091             }
9092           break;
9093         case GE:
9094         case UNGE:
9095           if (code == GE || !TARGET_IEEE_FP)
9096             {
9097               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
9098               code = EQ;
9099             }
9100           else
9101             {
9102               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9103               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
9104                                              GEN_INT (0x01)));
9105               code = NE;
9106             }
9107           break;
9108         case LE:
9109         case UNLE:
9110           if (code == LE && TARGET_IEEE_FP)
9111             {
9112               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9113               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
9114               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
9115               intcmp_mode = CCmode;
9116               code = LTU;
9117             }
9118           else
9119             {
9120               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
9121               code = NE;
9122             }
9123           break;
9124         case EQ:
9125         case UNEQ:
9126           if (code == EQ && TARGET_IEEE_FP)
9127             {
9128               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9129               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
9130               intcmp_mode = CCmode;
9131               code = EQ;
9132             }
9133           else
9134             {
9135               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
9136               code = NE;
9137               break;
9138             }
9139           break;
9140         case NE:
9141         case LTGT:
9142           if (code == NE && TARGET_IEEE_FP)
9143             {
9144               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9145               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
9146                                              GEN_INT (0x40)));
9147               code = NE;
9148             }
9149           else
9150             {
9151               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
9152               code = EQ;
9153             }
9154           break;
9155
9156         case UNORDERED:
9157           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
9158           code = NE;
9159           break;
9160         case ORDERED:
9161           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
9162           code = EQ;
9163           break;
9164
9165         default:
9166           abort ();
9167         }
9168     }
9169
9170   /* Return the test that should be put into the flags user, i.e.
9171      the bcc, scc, or cmov instruction.  */
9172   return gen_rtx_fmt_ee (code, VOIDmode,
9173                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
9174                          const0_rtx);
9175 }
9176
9177 rtx
9178 ix86_expand_compare (enum rtx_code code, rtx *second_test, rtx *bypass_test)
9179 {
9180   rtx op0, op1, ret;
9181   op0 = ix86_compare_op0;
9182   op1 = ix86_compare_op1;
9183
9184   if (second_test)
9185     *second_test = NULL_RTX;
9186   if (bypass_test)
9187     *bypass_test = NULL_RTX;
9188
9189   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
9190     ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
9191                                   second_test, bypass_test);
9192   else
9193     ret = ix86_expand_int_compare (code, op0, op1);
9194
9195   return ret;
9196 }
9197
9198 /* Return true if the CODE will result in nontrivial jump sequence.  */
9199 bool
9200 ix86_fp_jump_nontrivial_p (enum rtx_code code)
9201 {
9202   enum rtx_code bypass_code, first_code, second_code;
9203   if (!TARGET_CMOVE)
9204     return true;
9205   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
9206   return bypass_code != NIL || second_code != NIL;
9207 }
9208
9209 void
9210 ix86_expand_branch (enum rtx_code code, rtx label)
9211 {
9212   rtx tmp;
9213
9214   switch (GET_MODE (ix86_compare_op0))
9215     {
9216     case QImode:
9217     case HImode:
9218     case SImode:
9219       simple:
9220       tmp = ix86_expand_compare (code, NULL, NULL);
9221       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
9222                                   gen_rtx_LABEL_REF (VOIDmode, label),
9223                                   pc_rtx);
9224       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
9225       return;
9226
9227     case SFmode:
9228     case DFmode:
9229     case XFmode:
9230       {
9231         rtvec vec;
9232         int use_fcomi;
9233         enum rtx_code bypass_code, first_code, second_code;
9234
9235         code = ix86_prepare_fp_compare_args (code, &ix86_compare_op0,
9236                                              &ix86_compare_op1);
9237
9238         ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
9239
9240         /* Check whether we will use the natural sequence with one jump.  If
9241            so, we can expand jump early.  Otherwise delay expansion by
9242            creating compound insn to not confuse optimizers.  */
9243         if (bypass_code == NIL && second_code == NIL
9244             && TARGET_CMOVE)
9245           {
9246             ix86_split_fp_branch (code, ix86_compare_op0, ix86_compare_op1,
9247                                   gen_rtx_LABEL_REF (VOIDmode, label),
9248                                   pc_rtx, NULL_RTX);
9249           }
9250         else
9251           {
9252             tmp = gen_rtx_fmt_ee (code, VOIDmode,
9253                                   ix86_compare_op0, ix86_compare_op1);
9254             tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
9255                                         gen_rtx_LABEL_REF (VOIDmode, label),
9256                                         pc_rtx);
9257             tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);
9258
9259             use_fcomi = ix86_use_fcomi_compare (code);
9260             vec = rtvec_alloc (3 + !use_fcomi);
9261             RTVEC_ELT (vec, 0) = tmp;
9262             RTVEC_ELT (vec, 1)
9263               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 18));
9264             RTVEC_ELT (vec, 2)
9265               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 17));
9266             if (! use_fcomi)
9267               RTVEC_ELT (vec, 3)
9268                 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));
9269
9270             emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
9271           }
9272         return;
9273       }
9274
9275     case DImode:
9276       if (TARGET_64BIT)
9277         goto simple;
9278       /* Expand DImode branch into multiple compare+branch.  */
9279       {
9280         rtx lo[2], hi[2], label2;
9281         enum rtx_code code1, code2, code3;
9282
9283         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
9284           {
9285             tmp = ix86_compare_op0;
9286             ix86_compare_op0 = ix86_compare_op1;
9287             ix86_compare_op1 = tmp;
9288             code = swap_condition (code);
9289           }
9290         split_di (&ix86_compare_op0, 1, lo+0, hi+0);
9291         split_di (&ix86_compare_op1, 1, lo+1, hi+1);
9292
9293         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
9294            avoid two branches.  This costs one extra insn, so disable when
9295            optimizing for size.  */
9296
9297         if ((code == EQ || code == NE)
9298             && (!optimize_size
9299                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
9300           {
9301             rtx xor0, xor1;
9302
9303             xor1 = hi[0];
9304             if (hi[1] != const0_rtx)
9305               xor1 = expand_binop (SImode, xor_optab, xor1, hi[1],
9306                                    NULL_RTX, 0, OPTAB_WIDEN);
9307
9308             xor0 = lo[0];
9309             if (lo[1] != const0_rtx)
9310               xor0 = expand_binop (SImode, xor_optab, xor0, lo[1],
9311                                    NULL_RTX, 0, OPTAB_WIDEN);
9312
9313             tmp = expand_binop (SImode, ior_optab, xor1, xor0,
9314                                 NULL_RTX, 0, OPTAB_WIDEN);
9315
9316             ix86_compare_op0 = tmp;
9317             ix86_compare_op1 = const0_rtx;
9318             ix86_expand_branch (code, label);
9319             return;
9320           }
9321
9322         /* Otherwise, if we are doing less-than or greater-or-equal-than,
9323            op1 is a constant and the low word is zero, then we can just
9324            examine the high word.  */
9325
9326         if (GET_CODE (hi[1]) == CONST_INT && lo[1] == const0_rtx)
9327           switch (code)
9328             {
9329             case LT: case LTU: case GE: case GEU:
9330               ix86_compare_op0 = hi[0];
9331               ix86_compare_op1 = hi[1];
9332               ix86_expand_branch (code, label);
9333               return;
9334             default:
9335               break;
9336             }
9337
9338         /* Otherwise, we need two or three jumps.  */
9339
9340         label2 = gen_label_rtx ();
9341
9342         code1 = code;
9343         code2 = swap_condition (code);
9344         code3 = unsigned_condition (code);
9345
9346         switch (code)
9347           {
9348           case LT: case GT: case LTU: case GTU:
9349             break;
9350
9351           case LE:   code1 = LT;  code2 = GT;  break;
9352           case GE:   code1 = GT;  code2 = LT;  break;
9353           case LEU:  code1 = LTU; code2 = GTU; break;
9354           case GEU:  code1 = GTU; code2 = LTU; break;
9355
9356           case EQ:   code1 = NIL; code2 = NE;  break;
9357           case NE:   code2 = NIL; break;
9358
9359           default:
9360             abort ();
9361           }
9362
9363         /*
9364          * a < b =>
9365          *    if (hi(a) < hi(b)) goto true;
9366          *    if (hi(a) > hi(b)) goto false;
9367          *    if (lo(a) < lo(b)) goto true;
9368          *  false:
9369          */
9370
9371         ix86_compare_op0 = hi[0];
9372         ix86_compare_op1 = hi[1];
9373
9374         if (code1 != NIL)
9375           ix86_expand_branch (code1, label);
9376         if (code2 != NIL)
9377           ix86_expand_branch (code2, label2);
9378
9379         ix86_compare_op0 = lo[0];
9380         ix86_compare_op1 = lo[1];
9381         ix86_expand_branch (code3, label);
9382
9383         if (code2 != NIL)
9384           emit_label (label2);
9385         return;
9386       }
9387
9388     default:
9389       abort ();
9390     }
9391 }
9392
9393 /* Split branch based on floating point condition.  */
9394 void
9395 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
9396                       rtx target1, rtx target2, rtx tmp)
9397 {
9398   rtx second, bypass;
9399   rtx label = NULL_RTX;
9400   rtx condition;
9401   int bypass_probability = -1, second_probability = -1, probability = -1;
9402   rtx i;
9403
9404   if (target2 != pc_rtx)
9405     {
9406       rtx tmp = target2;
9407       code = reverse_condition_maybe_unordered (code);
9408       target2 = target1;
9409       target1 = tmp;
9410     }
9411
9412   condition = ix86_expand_fp_compare (code, op1, op2,
9413                                       tmp, &second, &bypass);
9414
9415   if (split_branch_probability >= 0)
9416     {
9417       /* Distribute the probabilities across the jumps.
9418          Assume the BYPASS and SECOND to be always test
9419          for UNORDERED.  */
9420       probability = split_branch_probability;
9421
9422       /* Value of 1 is low enough to make no need for probability
9423          to be updated.  Later we may run some experiments and see
9424          if unordered values are more frequent in practice.  */
9425       if (bypass)
9426         bypass_probability = 1;
9427       if (second)
9428         second_probability = 1;
9429     }
9430   if (bypass != NULL_RTX)
9431     {
9432       label = gen_label_rtx ();
9433       i = emit_jump_insn (gen_rtx_SET
9434                           (VOIDmode, pc_rtx,
9435                            gen_rtx_IF_THEN_ELSE (VOIDmode,
9436                                                  bypass,
9437                                                  gen_rtx_LABEL_REF (VOIDmode,
9438                                                                     label),
9439                                                  pc_rtx)));
9440       if (bypass_probability >= 0)
9441         REG_NOTES (i)
9442           = gen_rtx_EXPR_LIST (REG_BR_PROB,
9443                                GEN_INT (bypass_probability),
9444                                REG_NOTES (i));
9445     }
9446   i = emit_jump_insn (gen_rtx_SET
9447                       (VOIDmode, pc_rtx,
9448                        gen_rtx_IF_THEN_ELSE (VOIDmode,
9449                                              condition, target1, target2)));
9450   if (probability >= 0)
9451     REG_NOTES (i)
9452       = gen_rtx_EXPR_LIST (REG_BR_PROB,
9453                            GEN_INT (probability),
9454                            REG_NOTES (i));
9455   if (second != NULL_RTX)
9456     {
9457       i = emit_jump_insn (gen_rtx_SET
9458                           (VOIDmode, pc_rtx,
9459                            gen_rtx_IF_THEN_ELSE (VOIDmode, second, target1,
9460                                                  target2)));
9461       if (second_probability >= 0)
9462         REG_NOTES (i)
9463           = gen_rtx_EXPR_LIST (REG_BR_PROB,
9464                                GEN_INT (second_probability),
9465                                REG_NOTES (i));
9466     }
9467   if (label != NULL_RTX)
9468     emit_label (label);
9469 }
9470
9471 int
9472 ix86_expand_setcc (enum rtx_code code, rtx dest)
9473 {
9474   rtx ret, tmp, tmpreg, equiv;
9475   rtx second_test, bypass_test;
9476
9477   if (GET_MODE (ix86_compare_op0) == DImode
9478       && !TARGET_64BIT)
9479     return 0; /* FAIL */
9480
9481   if (GET_MODE (dest) != QImode)
9482     abort ();
9483
9484   ret = ix86_expand_compare (code, &second_test, &bypass_test);
9485   PUT_MODE (ret, QImode);
9486
9487   tmp = dest;
9488   tmpreg = dest;
9489
9490   emit_insn (gen_rtx_SET (VOIDmode, tmp, ret));
9491   if (bypass_test || second_test)
9492     {
9493       rtx test = second_test;
9494       int bypass = 0;
9495       rtx tmp2 = gen_reg_rtx (QImode);
9496       if (bypass_test)
9497         {
9498           if (second_test)
9499             abort ();
9500           test = bypass_test;
9501           bypass = 1;
9502           PUT_CODE (test, reverse_condition_maybe_unordered (GET_CODE (test)));
9503         }
9504       PUT_MODE (test, QImode);
9505       emit_insn (gen_rtx_SET (VOIDmode, tmp2, test));
9506
9507       if (bypass)
9508         emit_insn (gen_andqi3 (tmp, tmpreg, tmp2));
9509       else
9510         emit_insn (gen_iorqi3 (tmp, tmpreg, tmp2));
9511     }
9512
9513   /* Attach a REG_EQUAL note describing the comparison result.  */
9514   equiv = simplify_gen_relational (code, QImode,
9515                                    GET_MODE (ix86_compare_op0),
9516                                    ix86_compare_op0, ix86_compare_op1);
9517   set_unique_reg_note (get_last_insn (), REG_EQUAL, equiv);
9518
9519   return 1; /* DONE */
9520 }
9521
9522 /* Expand comparison setting or clearing carry flag.  Return true when
9523    successful and set pop for the operation.  */
9524 static bool
9525 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
9526 {
9527   enum machine_mode mode =
9528     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
9529
9530   /* Do not handle DImode compares that go trought special path.  Also we can't
9531      deal with FP compares yet.  This is possible to add.  */
9532   if ((mode == DImode && !TARGET_64BIT))
9533     return false;
9534   if (FLOAT_MODE_P (mode))
9535     {
9536       rtx second_test = NULL, bypass_test = NULL;
9537       rtx compare_op, compare_seq;
9538
9539       /* Shortcut:  following common codes never translate into carry flag compares.  */
9540       if (code == EQ || code == NE || code == UNEQ || code == LTGT
9541           || code == ORDERED || code == UNORDERED)
9542         return false;
9543
9544       /* These comparisons require zero flag; swap operands so they won't.  */
9545       if ((code == GT || code == UNLE || code == LE || code == UNGT)
9546           && !TARGET_IEEE_FP)
9547         {
9548           rtx tmp = op0;
9549           op0 = op1;
9550           op1 = tmp;
9551           code = swap_condition (code);
9552         }
9553
9554       /* Try to expand the comparison and verify that we end up with carry flag
9555          based comparison.  This is fails to be true only when we decide to expand
9556          comparison using arithmetic that is not too common scenario.  */
9557       start_sequence ();
9558       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
9559                                            &second_test, &bypass_test);
9560       compare_seq = get_insns ();
9561       end_sequence ();
9562
9563       if (second_test || bypass_test)
9564         return false;
9565       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
9566           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
9567         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
9568       else
9569         code = GET_CODE (compare_op);
9570       if (code != LTU && code != GEU)
9571         return false;
9572       emit_insn (compare_seq);
9573       *pop = compare_op;
9574       return true;
9575     }
9576   if (!INTEGRAL_MODE_P (mode))
9577     return false;
9578   switch (code)
9579     {
9580     case LTU:
9581     case GEU:
9582       break;
9583
9584     /* Convert a==0 into (unsigned)a<1.  */
9585     case EQ:
9586     case NE:
9587       if (op1 != const0_rtx)
9588         return false;
9589       op1 = const1_rtx;
9590       code = (code == EQ ? LTU : GEU);
9591       break;
9592
9593     /* Convert a>b into b<a or a>=b-1.  */
9594     case GTU:
9595     case LEU:
9596       if (GET_CODE (op1) == CONST_INT)
9597         {
9598           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
9599           /* Bail out on overflow.  We still can swap operands but that
9600              would force loading of the constant into register.  */
9601           if (op1 == const0_rtx
9602               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
9603             return false;
9604           code = (code == GTU ? GEU : LTU);
9605         }
9606       else
9607         {
9608           rtx tmp = op1;
9609           op1 = op0;
9610           op0 = tmp;
9611           code = (code == GTU ? LTU : GEU);
9612         }
9613       break;
9614
9615     /* Convert a>=0 into (unsigned)a<0x80000000.  */
9616     case LT:
9617     case GE:
9618       if (mode == DImode || op1 != const0_rtx)
9619         return false;
9620       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
9621       code = (code == LT ? GEU : LTU);
9622       break;
9623     case LE:
9624     case GT:
9625       if (mode == DImode || op1 != constm1_rtx)
9626         return false;
9627       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
9628       code = (code == LE ? GEU : LTU);
9629       break;
9630
9631     default:
9632       return false;
9633     }
9634   /* Swapping operands may cause constant to appear as first operand.  */
9635   if (!nonimmediate_operand (op0, VOIDmode))
9636     {
9637       if (no_new_pseudos)
9638         return false;
9639       op0 = force_reg (mode, op0);
9640     }
9641   ix86_compare_op0 = op0;
9642   ix86_compare_op1 = op1;
9643   *pop = ix86_expand_compare (code, NULL, NULL);
9644   if (GET_CODE (*pop) != LTU && GET_CODE (*pop) != GEU)
9645     abort ();
9646   return true;
9647 }
9648
9649 int
9650 ix86_expand_int_movcc (rtx operands[])
9651 {
9652   enum rtx_code code = GET_CODE (operands[1]), compare_code;
9653   rtx compare_seq, compare_op;
9654   rtx second_test, bypass_test;
9655   enum machine_mode mode = GET_MODE (operands[0]);
9656   bool sign_bit_compare_p = false;;
9657
9658   start_sequence ();
9659   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
9660   compare_seq = get_insns ();
9661   end_sequence ();
9662
9663   compare_code = GET_CODE (compare_op);
9664
9665   if ((ix86_compare_op1 == const0_rtx && (code == GE || code == LT))
9666       || (ix86_compare_op1 == constm1_rtx && (code == GT || code == LE)))
9667     sign_bit_compare_p = true;
9668
9669   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
9670      HImode insns, we'd be swallowed in word prefix ops.  */
9671
9672   if ((mode != HImode || TARGET_FAST_PREFIX)
9673       && (mode != DImode || TARGET_64BIT)
9674       && GET_CODE (operands[2]) == CONST_INT
9675       && GET_CODE (operands[3]) == CONST_INT)
9676     {
9677       rtx out = operands[0];
9678       HOST_WIDE_INT ct = INTVAL (operands[2]);
9679       HOST_WIDE_INT cf = INTVAL (operands[3]);
9680       HOST_WIDE_INT diff;
9681
9682       diff = ct - cf;
9683       /*  Sign bit compares are better done using shifts than we do by using
9684           sbb.  */
9685       if (sign_bit_compare_p
9686           || ix86_expand_carry_flag_compare (code, ix86_compare_op0,
9687                                              ix86_compare_op1, &compare_op))
9688         {
9689           /* Detect overlap between destination and compare sources.  */
9690           rtx tmp = out;
9691
9692           if (!sign_bit_compare_p)
9693             {
9694               bool fpcmp = false;
9695
9696               compare_code = GET_CODE (compare_op);
9697
9698               if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
9699                   || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
9700                 {
9701                   fpcmp = true;
9702                   compare_code = ix86_fp_compare_code_to_integer (compare_code);
9703                 }
9704
9705               /* To simplify rest of code, restrict to the GEU case.  */
9706               if (compare_code == LTU)
9707                 {
9708                   HOST_WIDE_INT tmp = ct;
9709                   ct = cf;
9710                   cf = tmp;
9711                   compare_code = reverse_condition (compare_code);
9712                   code = reverse_condition (code);
9713                 }
9714               else
9715                 {
9716                   if (fpcmp)
9717                     PUT_CODE (compare_op,
9718                               reverse_condition_maybe_unordered
9719                                 (GET_CODE (compare_op)));
9720                   else
9721                     PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
9722                 }
9723               diff = ct - cf;
9724
9725               if (reg_overlap_mentioned_p (out, ix86_compare_op0)
9726                   || reg_overlap_mentioned_p (out, ix86_compare_op1))
9727                 tmp = gen_reg_rtx (mode);
9728
9729               if (mode == DImode)
9730                 emit_insn (gen_x86_movdicc_0_m1_rex64 (tmp, compare_op));
9731               else
9732                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp), compare_op));
9733             }
9734           else
9735             {
9736               if (code == GT || code == GE)
9737                 code = reverse_condition (code);
9738               else
9739                 {
9740                   HOST_WIDE_INT tmp = ct;
9741                   ct = cf;
9742                   cf = tmp;
9743                   diff = ct - cf;
9744                 }
9745               tmp = emit_store_flag (tmp, code, ix86_compare_op0,
9746                                      ix86_compare_op1, VOIDmode, 0, -1);
9747             }
9748
9749           if (diff == 1)
9750             {
9751               /*
9752                * cmpl op0,op1
9753                * sbbl dest,dest
9754                * [addl dest, ct]
9755                *
9756                * Size 5 - 8.
9757                */
9758               if (ct)
9759                 tmp = expand_simple_binop (mode, PLUS,
9760                                            tmp, GEN_INT (ct),
9761                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
9762             }
9763           else if (cf == -1)
9764             {
9765               /*
9766                * cmpl op0,op1
9767                * sbbl dest,dest
9768                * orl $ct, dest
9769                *
9770                * Size 8.
9771                */
9772               tmp = expand_simple_binop (mode, IOR,
9773                                          tmp, GEN_INT (ct),
9774                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
9775             }
9776           else if (diff == -1 && ct)
9777             {
9778               /*
9779                * cmpl op0,op1
9780                * sbbl dest,dest
9781                * notl dest
9782                * [addl dest, cf]
9783                *
9784                * Size 8 - 11.
9785                */
9786               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
9787               if (cf)
9788                 tmp = expand_simple_binop (mode, PLUS,
9789                                            copy_rtx (tmp), GEN_INT (cf),
9790                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
9791             }
9792           else
9793             {
9794               /*
9795                * cmpl op0,op1
9796                * sbbl dest,dest
9797                * [notl dest]
9798                * andl cf - ct, dest
9799                * [addl dest, ct]
9800                *
9801                * Size 8 - 11.
9802                */
9803
9804               if (cf == 0)
9805                 {
9806                   cf = ct;
9807                   ct = 0;
9808                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
9809                 }
9810
9811               tmp = expand_simple_binop (mode, AND,
9812                                          copy_rtx (tmp),
9813                                          gen_int_mode (cf - ct, mode),
9814                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
9815               if (ct)
9816                 tmp = expand_simple_binop (mode, PLUS,
9817                                            copy_rtx (tmp), GEN_INT (ct),
9818                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
9819             }
9820
9821           if (!rtx_equal_p (tmp, out))
9822             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
9823
9824           return 1; /* DONE */
9825         }
9826
9827       if (diff < 0)
9828         {
9829           HOST_WIDE_INT tmp;
9830           tmp = ct, ct = cf, cf = tmp;
9831           diff = -diff;
9832           if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
9833             {
9834               /* We may be reversing unordered compare to normal compare, that
9835                  is not valid in general (we may convert non-trapping condition
9836                  to trapping one), however on i386 we currently emit all
9837                  comparisons unordered.  */
9838               compare_code = reverse_condition_maybe_unordered (compare_code);
9839               code = reverse_condition_maybe_unordered (code);
9840             }
9841           else
9842             {
9843               compare_code = reverse_condition (compare_code);
9844               code = reverse_condition (code);
9845             }
9846         }
9847
9848       compare_code = NIL;
9849       if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_INT
9850           && GET_CODE (ix86_compare_op1) == CONST_INT)
9851         {
9852           if (ix86_compare_op1 == const0_rtx
9853               && (code == LT || code == GE))
9854             compare_code = code;
9855           else if (ix86_compare_op1 == constm1_rtx)
9856             {
9857               if (code == LE)
9858                 compare_code = LT;
9859               else if (code == GT)
9860                 compare_code = GE;
9861             }
9862         }
9863
9864       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
9865       if (compare_code != NIL
9866           && GET_MODE (ix86_compare_op0) == GET_MODE (out)
9867           && (cf == -1 || ct == -1))
9868         {
9869           /* If lea code below could be used, only optimize
9870              if it results in a 2 insn sequence.  */
9871
9872           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
9873                  || diff == 3 || diff == 5 || diff == 9)
9874               || (compare_code == LT && ct == -1)
9875               || (compare_code == GE && cf == -1))
9876             {
9877               /*
9878                * notl op1       (if necessary)
9879                * sarl $31, op1
9880                * orl cf, op1
9881                */
9882               if (ct != -1)
9883                 {
9884                   cf = ct;
9885                   ct = -1;
9886                   code = reverse_condition (code);
9887                 }
9888
9889               out = emit_store_flag (out, code, ix86_compare_op0,
9890                                      ix86_compare_op1, VOIDmode, 0, -1);
9891
9892               out = expand_simple_binop (mode, IOR,
9893                                          out, GEN_INT (cf),
9894                                          out, 1, OPTAB_DIRECT);
9895               if (out != operands[0])
9896                 emit_move_insn (operands[0], out);
9897
9898               return 1; /* DONE */
9899             }
9900         }
9901
9902
9903       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
9904            || diff == 3 || diff == 5 || diff == 9)
9905           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
9906           && (mode != DImode || x86_64_sign_extended_value (GEN_INT (cf))))
9907         {
9908           /*
9909            * xorl dest,dest
9910            * cmpl op1,op2
9911            * setcc dest
9912            * lea cf(dest*(ct-cf)),dest
9913            *
9914            * Size 14.
9915            *
9916            * This also catches the degenerate setcc-only case.
9917            */
9918
9919           rtx tmp;
9920           int nops;
9921
9922           out = emit_store_flag (out, code, ix86_compare_op0,
9923                                  ix86_compare_op1, VOIDmode, 0, 1);
9924
9925           nops = 0;
9926           /* On x86_64 the lea instruction operates on Pmode, so we need
9927              to get arithmetics done in proper mode to match.  */
9928           if (diff == 1)
9929             tmp = copy_rtx (out);
9930           else
9931             {
9932               rtx out1;
9933               out1 = copy_rtx (out);
9934               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
9935               nops++;
9936               if (diff & 1)
9937                 {
9938                   tmp = gen_rtx_PLUS (mode, tmp, out1);
9939                   nops++;
9940                 }
9941             }
9942           if (cf != 0)
9943             {
9944               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
9945               nops++;
9946             }
9947           if (!rtx_equal_p (tmp, out))
9948             {
9949               if (nops == 1)
9950                 out = force_operand (tmp, copy_rtx (out));
9951               else
9952                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
9953             }
9954           if (!rtx_equal_p (out, operands[0]))
9955             emit_move_insn (operands[0], copy_rtx (out));
9956
9957           return 1; /* DONE */
9958         }
9959
9960       /*
9961        * General case:                  Jumpful:
9962        *   xorl dest,dest               cmpl op1, op2
9963        *   cmpl op1, op2                movl ct, dest
9964        *   setcc dest                   jcc 1f
9965        *   decl dest                    movl cf, dest
9966        *   andl (cf-ct),dest            1:
9967        *   addl ct,dest
9968        *
9969        * Size 20.                       Size 14.
9970        *
9971        * This is reasonably steep, but branch mispredict costs are
9972        * high on modern cpus, so consider failing only if optimizing
9973        * for space.
9974        */
9975
9976       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
9977           && BRANCH_COST >= 2)
9978         {
9979           if (cf == 0)
9980             {
9981               cf = ct;
9982               ct = 0;
9983               if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
9984                 /* We may be reversing unordered compare to normal compare,
9985                    that is not valid in general (we may convert non-trapping
9986                    condition to trapping one), however on i386 we currently
9987                    emit all comparisons unordered.  */
9988                 code = reverse_condition_maybe_unordered (code);
9989               else
9990                 {
9991                   code = reverse_condition (code);
9992                   if (compare_code != NIL)
9993                     compare_code = reverse_condition (compare_code);
9994                 }
9995             }
9996
9997           if (compare_code != NIL)
9998             {
9999               /* notl op1       (if needed)
10000                  sarl $31, op1
10001                  andl (cf-ct), op1
10002                  addl ct, op1
10003
10004                  For x < 0 (resp. x <= -1) there will be no notl,
10005                  so if possible swap the constants to get rid of the
10006                  complement.
10007                  True/false will be -1/0 while code below (store flag
10008                  followed by decrement) is 0/-1, so the constants need
10009                  to be exchanged once more.  */
10010
10011               if (compare_code == GE || !cf)
10012                 {
10013                   code = reverse_condition (code);
10014                   compare_code = LT;
10015                 }
10016               else
10017                 {
10018                   HOST_WIDE_INT tmp = cf;
10019                   cf = ct;
10020                   ct = tmp;
10021                 }
10022
10023               out = emit_store_flag (out, code, ix86_compare_op0,
10024                                      ix86_compare_op1, VOIDmode, 0, -1);
10025             }
10026           else
10027             {
10028               out = emit_store_flag (out, code, ix86_compare_op0,
10029                                      ix86_compare_op1, VOIDmode, 0, 1);
10030
10031               out = expand_simple_binop (mode, PLUS, copy_rtx (out), constm1_rtx,
10032                                          copy_rtx (out), 1, OPTAB_DIRECT);
10033             }
10034
10035           out = expand_simple_binop (mode, AND, copy_rtx (out),
10036                                      gen_int_mode (cf - ct, mode),
10037                                      copy_rtx (out), 1, OPTAB_DIRECT);
10038           if (ct)
10039             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
10040                                        copy_rtx (out), 1, OPTAB_DIRECT);
10041           if (!rtx_equal_p (out, operands[0]))
10042             emit_move_insn (operands[0], copy_rtx (out));
10043
10044           return 1; /* DONE */
10045         }
10046     }
10047
10048   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
10049     {
10050       /* Try a few things more with specific constants and a variable.  */
10051
10052       optab op;
10053       rtx var, orig_out, out, tmp;
10054
10055       if (BRANCH_COST <= 2)
10056         return 0; /* FAIL */
10057
10058       /* If one of the two operands is an interesting constant, load a
10059          constant with the above and mask it in with a logical operation.  */
10060
10061       if (GET_CODE (operands[2]) == CONST_INT)
10062         {
10063           var = operands[3];
10064           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
10065             operands[3] = constm1_rtx, op = and_optab;
10066           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
10067             operands[3] = const0_rtx, op = ior_optab;
10068           else
10069             return 0; /* FAIL */
10070         }
10071       else if (GET_CODE (operands[3]) == CONST_INT)
10072         {
10073           var = operands[2];
10074           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
10075             operands[2] = constm1_rtx, op = and_optab;
10076           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
10077             operands[2] = const0_rtx, op = ior_optab;
10078           else
10079             return 0; /* FAIL */
10080         }
10081       else
10082         return 0; /* FAIL */
10083
10084       orig_out = operands[0];
10085       tmp = gen_reg_rtx (mode);
10086       operands[0] = tmp;
10087
10088       /* Recurse to get the constant loaded.  */
10089       if (ix86_expand_int_movcc (operands) == 0)
10090         return 0; /* FAIL */
10091
10092       /* Mask in the interesting variable.  */
10093       out = expand_binop (mode, op, var, tmp, orig_out, 0,
10094                           OPTAB_WIDEN);
10095       if (!rtx_equal_p (out, orig_out))
10096         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
10097
10098       return 1; /* DONE */
10099     }
10100
10101   /*
10102    * For comparison with above,
10103    *
10104    * movl cf,dest
10105    * movl ct,tmp
10106    * cmpl op1,op2
10107    * cmovcc tmp,dest
10108    *
10109    * Size 15.
10110    */
10111
10112   if (! nonimmediate_operand (operands[2], mode))
10113     operands[2] = force_reg (mode, operands[2]);
10114   if (! nonimmediate_operand (operands[3], mode))
10115     operands[3] = force_reg (mode, operands[3]);
10116
10117   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
10118     {
10119       rtx tmp = gen_reg_rtx (mode);
10120       emit_move_insn (tmp, operands[3]);
10121       operands[3] = tmp;
10122     }
10123   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
10124     {
10125       rtx tmp = gen_reg_rtx (mode);
10126       emit_move_insn (tmp, operands[2]);
10127       operands[2] = tmp;
10128     }
10129
10130   if (! register_operand (operands[2], VOIDmode)
10131       && (mode == QImode
10132           || ! register_operand (operands[3], VOIDmode)))
10133     operands[2] = force_reg (mode, operands[2]);
10134
10135   if (mode == QImode
10136       && ! register_operand (operands[3], VOIDmode))
10137     operands[3] = force_reg (mode, operands[3]);
10138
10139   emit_insn (compare_seq);
10140   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10141                           gen_rtx_IF_THEN_ELSE (mode,
10142                                                 compare_op, operands[2],
10143                                                 operands[3])));
10144   if (bypass_test)
10145     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
10146                             gen_rtx_IF_THEN_ELSE (mode,
10147                                   bypass_test,
10148                                   copy_rtx (operands[3]),
10149                                   copy_rtx (operands[0]))));
10150   if (second_test)
10151     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
10152                             gen_rtx_IF_THEN_ELSE (mode,
10153                                   second_test,
10154                                   copy_rtx (operands[2]),
10155                                   copy_rtx (operands[0]))));
10156
10157   return 1; /* DONE */
10158 }
10159
10160 int
10161 ix86_expand_fp_movcc (rtx operands[])
10162 {
10163   enum rtx_code code;
10164   rtx tmp;
10165   rtx compare_op, second_test, bypass_test;
10166
10167   /* For SF/DFmode conditional moves based on comparisons
10168      in same mode, we may want to use SSE min/max instructions.  */
10169   if (((TARGET_SSE_MATH && GET_MODE (operands[0]) == SFmode)
10170        || (TARGET_SSE2 && TARGET_SSE_MATH && GET_MODE (operands[0]) == DFmode))
10171       && GET_MODE (ix86_compare_op0) == GET_MODE (operands[0])
10172       /* The SSE comparisons does not support the LTGT/UNEQ pair.  */
10173       && (!TARGET_IEEE_FP
10174           || (GET_CODE (operands[1]) != LTGT && GET_CODE (operands[1]) != UNEQ))
10175       /* We may be called from the post-reload splitter.  */
10176       && (!REG_P (operands[0])
10177           || SSE_REG_P (operands[0])
10178           || REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER))
10179     {
10180       rtx op0 = ix86_compare_op0, op1 = ix86_compare_op1;
10181       code = GET_CODE (operands[1]);
10182
10183       /* See if we have (cross) match between comparison operands and
10184          conditional move operands.  */
10185       if (rtx_equal_p (operands[2], op1))
10186         {
10187           rtx tmp = op0;
10188           op0 = op1;
10189           op1 = tmp;
10190           code = reverse_condition_maybe_unordered (code);
10191         }
10192       if (rtx_equal_p (operands[2], op0) && rtx_equal_p (operands[3], op1))
10193         {
10194           /* Check for min operation.  */
10195           if (code == LT || code == UNLE)
10196             {
10197                if (code == UNLE)
10198                 {
10199                   rtx tmp = op0;
10200                   op0 = op1;
10201                   op1 = tmp;
10202                 }
10203                operands[0] = force_reg (GET_MODE (operands[0]), operands[0]);
10204                if (memory_operand (op0, VOIDmode))
10205                  op0 = force_reg (GET_MODE (operands[0]), op0);
10206                if (GET_MODE (operands[0]) == SFmode)
10207                  emit_insn (gen_minsf3 (operands[0], op0, op1));
10208                else
10209                  emit_insn (gen_mindf3 (operands[0], op0, op1));
10210                return 1;
10211             }
10212           /* Check for max operation.  */
10213           if (code == GT || code == UNGE)
10214             {
10215                if (code == UNGE)
10216                 {
10217                   rtx tmp = op0;
10218                   op0 = op1;
10219                   op1 = tmp;
10220                 }
10221                operands[0] = force_reg (GET_MODE (operands[0]), operands[0]);
10222                if (memory_operand (op0, VOIDmode))
10223                  op0 = force_reg (GET_MODE (operands[0]), op0);
10224                if (GET_MODE (operands[0]) == SFmode)
10225                  emit_insn (gen_maxsf3 (operands[0], op0, op1));
10226                else
10227                  emit_insn (gen_maxdf3 (operands[0], op0, op1));
10228                return 1;
10229             }
10230         }
10231       /* Manage condition to be sse_comparison_operator.  In case we are
10232          in non-ieee mode, try to canonicalize the destination operand
10233          to be first in the comparison - this helps reload to avoid extra
10234          moves.  */
10235       if (!sse_comparison_operator (operands[1], VOIDmode)
10236           || (rtx_equal_p (operands[0], ix86_compare_op1) && !TARGET_IEEE_FP))
10237         {
10238           rtx tmp = ix86_compare_op0;
10239           ix86_compare_op0 = ix86_compare_op1;
10240           ix86_compare_op1 = tmp;
10241           operands[1] = gen_rtx_fmt_ee (swap_condition (GET_CODE (operands[1])),
10242                                         VOIDmode, ix86_compare_op0,
10243                                         ix86_compare_op1);
10244         }
10245       /* Similarly try to manage result to be first operand of conditional
10246          move. We also don't support the NE comparison on SSE, so try to
10247          avoid it.  */
10248       if ((rtx_equal_p (operands[0], operands[3])
10249            && (!TARGET_IEEE_FP || GET_CODE (operands[1]) != EQ))
10250           || (GET_CODE (operands[1]) == NE && TARGET_IEEE_FP))
10251         {
10252           rtx tmp = operands[2];
10253           operands[2] = operands[3];
10254           operands[3] = tmp;
10255           operands[1] = gen_rtx_fmt_ee (reverse_condition_maybe_unordered
10256                                           (GET_CODE (operands[1])),
10257                                         VOIDmode, ix86_compare_op0,
10258                                         ix86_compare_op1);
10259         }
10260       if (GET_MODE (operands[0]) == SFmode)
10261         emit_insn (gen_sse_movsfcc (operands[0], operands[1],
10262                                     operands[2], operands[3],
10263                                     ix86_compare_op0, ix86_compare_op1));
10264       else
10265         emit_insn (gen_sse_movdfcc (operands[0], operands[1],
10266                                     operands[2], operands[3],
10267                                     ix86_compare_op0, ix86_compare_op1));
10268       return 1;
10269     }
10270
10271   /* The floating point conditional move instructions don't directly
10272      support conditions resulting from a signed integer comparison.  */
10273
10274   code = GET_CODE (operands[1]);
10275   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
10276
10277   /* The floating point conditional move instructions don't directly
10278      support signed integer comparisons.  */
10279
10280   if (!fcmov_comparison_operator (compare_op, VOIDmode))
10281     {
10282       if (second_test != NULL || bypass_test != NULL)
10283         abort ();
10284       tmp = gen_reg_rtx (QImode);
10285       ix86_expand_setcc (code, tmp);
10286       code = NE;
10287       ix86_compare_op0 = tmp;
10288       ix86_compare_op1 = const0_rtx;
10289       compare_op = ix86_expand_compare (code,  &second_test, &bypass_test);
10290     }
10291   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
10292     {
10293       tmp = gen_reg_rtx (GET_MODE (operands[0]));
10294       emit_move_insn (tmp, operands[3]);
10295       operands[3] = tmp;
10296     }
10297   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
10298     {
10299       tmp = gen_reg_rtx (GET_MODE (operands[0]));
10300       emit_move_insn (tmp, operands[2]);
10301       operands[2] = tmp;
10302     }
10303
10304   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10305                           gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
10306                                 compare_op,
10307                                 operands[2],
10308                                 operands[3])));
10309   if (bypass_test)
10310     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10311                             gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
10312                                   bypass_test,
10313                                   operands[3],
10314                                   operands[0])));
10315   if (second_test)
10316     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10317                             gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
10318                                   second_test,
10319                                   operands[2],
10320                                   operands[0])));
10321
10322   return 1;
10323 }
10324
10325 /* Expand conditional increment or decrement using adb/sbb instructions.
10326    The default case using setcc followed by the conditional move can be
10327    done by generic code.  */
10328 int
10329 ix86_expand_int_addcc (rtx operands[])
10330 {
10331   enum rtx_code code = GET_CODE (operands[1]);
10332   rtx compare_op;
10333   rtx val = const0_rtx;
10334   bool fpcmp = false;
10335   enum machine_mode mode = GET_MODE (operands[0]);
10336
10337   if (operands[3] != const1_rtx
10338       && operands[3] != constm1_rtx)
10339     return 0;
10340   if (!ix86_expand_carry_flag_compare (code, ix86_compare_op0,
10341                                        ix86_compare_op1, &compare_op))
10342      return 0;
10343   code = GET_CODE (compare_op);
10344
10345   if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
10346       || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
10347     {
10348       fpcmp = true;
10349       code = ix86_fp_compare_code_to_integer (code);
10350     }
10351
10352   if (code != LTU)
10353     {
10354       val = constm1_rtx;
10355       if (fpcmp)
10356         PUT_CODE (compare_op,
10357                   reverse_condition_maybe_unordered
10358                     (GET_CODE (compare_op)));
10359       else
10360         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
10361     }
10362   PUT_MODE (compare_op, mode);
10363
10364   /* Construct either adc or sbb insn.  */
10365   if ((code == LTU) == (operands[3] == constm1_rtx))
10366     {
10367       switch (GET_MODE (operands[0]))
10368         {
10369           case QImode:
10370             emit_insn (gen_subqi3_carry (operands[0], operands[2], val, compare_op));
10371             break;
10372           case HImode:
10373             emit_insn (gen_subhi3_carry (operands[0], operands[2], val, compare_op));
10374             break;
10375           case SImode:
10376             emit_insn (gen_subsi3_carry (operands[0], operands[2], val, compare_op));
10377             break;
10378           case DImode:
10379             emit_insn (gen_subdi3_carry_rex64 (operands[0], operands[2], val, compare_op));
10380             break;
10381           default:
10382             abort ();
10383         }
10384     }
10385   else
10386     {
10387       switch (GET_MODE (operands[0]))
10388         {
10389           case QImode:
10390             emit_insn (gen_addqi3_carry (operands[0], operands[2], val, compare_op));
10391             break;
10392           case HImode:
10393             emit_insn (gen_addhi3_carry (operands[0], operands[2], val, compare_op));
10394             break;
10395           case SImode:
10396             emit_insn (gen_addsi3_carry (operands[0], operands[2], val, compare_op));
10397             break;
10398           case DImode:
10399             emit_insn (gen_adddi3_carry_rex64 (operands[0], operands[2], val, compare_op));
10400             break;
10401           default:
10402             abort ();
10403         }
10404     }
10405   return 1; /* DONE */
10406 }
10407
10408
10409 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
10410    works for floating pointer parameters and nonoffsetable memories.
10411    For pushes, it returns just stack offsets; the values will be saved
10412    in the right order.  Maximally three parts are generated.  */
10413
10414 static int
10415 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
10416 {
10417   int size;
10418
10419   if (!TARGET_64BIT)
10420     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
10421   else
10422     size = (GET_MODE_SIZE (mode) + 4) / 8;
10423
10424   if (GET_CODE (operand) == REG && MMX_REGNO_P (REGNO (operand)))
10425     abort ();
10426   if (size < 2 || size > 3)
10427     abort ();
10428
10429   /* Optimize constant pool reference to immediates.  This is used by fp
10430      moves, that force all constants to memory to allow combining.  */
10431   if (GET_CODE (operand) == MEM && RTX_UNCHANGING_P (operand))
10432     {
10433       rtx tmp = maybe_get_pool_constant (operand);
10434       if (tmp)
10435         operand = tmp;
10436     }
10437
10438   if (GET_CODE (operand) == MEM && !offsettable_memref_p (operand))
10439     {
10440       /* The only non-offsetable memories we handle are pushes.  */
10441       if (! push_operand (operand, VOIDmode))
10442         abort ();
10443
10444       operand = copy_rtx (operand);
10445       PUT_MODE (operand, Pmode);
10446       parts[0] = parts[1] = parts[2] = operand;
10447     }
10448   else if (!TARGET_64BIT)
10449     {
10450       if (mode == DImode)
10451         split_di (&operand, 1, &parts[0], &parts[1]);
10452       else
10453         {
10454           if (REG_P (operand))
10455             {
10456               if (!reload_completed)
10457                 abort ();
10458               parts[0] = gen_rtx_REG (SImode, REGNO (operand) + 0);
10459               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
10460               if (size == 3)
10461                 parts[2] = gen_rtx_REG (SImode, REGNO (operand) + 2);
10462             }
10463           else if (offsettable_memref_p (operand))
10464             {
10465               operand = adjust_address (operand, SImode, 0);
10466               parts[0] = operand;
10467               parts[1] = adjust_address (operand, SImode, 4);
10468               if (size == 3)
10469                 parts[2] = adjust_address (operand, SImode, 8);
10470             }
10471           else if (GET_CODE (operand) == CONST_DOUBLE)
10472             {
10473               REAL_VALUE_TYPE r;
10474               long l[4];
10475
10476               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
10477               switch (mode)
10478                 {
10479                 case XFmode:
10480                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
10481                   parts[2] = gen_int_mode (l[2], SImode);
10482                   break;
10483                 case DFmode:
10484                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
10485                   break;
10486                 default:
10487                   abort ();
10488                 }
10489               parts[1] = gen_int_mode (l[1], SImode);
10490               parts[0] = gen_int_mode (l[0], SImode);
10491             }
10492           else
10493             abort ();
10494         }
10495     }
10496   else
10497     {
10498       if (mode == TImode)
10499         split_ti (&operand, 1, &parts[0], &parts[1]);
10500       if (mode == XFmode || mode == TFmode)
10501         {
10502           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
10503           if (REG_P (operand))
10504             {
10505               if (!reload_completed)
10506                 abort ();
10507               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
10508               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
10509             }
10510           else if (offsettable_memref_p (operand))
10511             {
10512               operand = adjust_address (operand, DImode, 0);
10513               parts[0] = operand;
10514               parts[1] = adjust_address (operand, upper_mode, 8);
10515             }
10516           else if (GET_CODE (operand) == CONST_DOUBLE)
10517             {
10518               REAL_VALUE_TYPE r;
10519               long l[3];
10520
10521               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
10522               REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
10523               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
10524               if (HOST_BITS_PER_WIDE_INT >= 64)
10525                 parts[0]
10526                   = gen_int_mode
10527                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
10528                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
10529                        DImode);
10530               else
10531                 parts[0] = immed_double_const (l[0], l[1], DImode);
10532               if (upper_mode == SImode)
10533                 parts[1] = gen_int_mode (l[2], SImode);
10534               else if (HOST_BITS_PER_WIDE_INT >= 64)
10535                 parts[1]
10536                   = gen_int_mode
10537                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
10538                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
10539                        DImode);
10540               else
10541                 parts[1] = immed_double_const (l[2], l[3], DImode);
10542             }
10543           else
10544             abort ();
10545         }
10546     }
10547
10548   return size;
10549 }
10550
10551 /* Emit insns to perform a move or push of DI, DF, and XF values.
10552    Return false when normal moves are needed; true when all required
10553    insns have been emitted.  Operands 2-4 contain the input values
10554    int the correct order; operands 5-7 contain the output values.  */
10555
10556 void
10557 ix86_split_long_move (rtx operands[])
10558 {
10559   rtx part[2][3];
10560   int nparts;
10561   int push = 0;
10562   int collisions = 0;
10563   enum machine_mode mode = GET_MODE (operands[0]);
10564
10565   /* The DFmode expanders may ask us to move double.
10566      For 64bit target this is single move.  By hiding the fact
10567      here we simplify i386.md splitters.  */
10568   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
10569     {
10570       /* Optimize constant pool reference to immediates.  This is used by
10571          fp moves, that force all constants to memory to allow combining.  */
10572
10573       if (GET_CODE (operands[1]) == MEM
10574           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
10575           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
10576         operands[1] = get_pool_constant (XEXP (operands[1], 0));
10577       if (push_operand (operands[0], VOIDmode))
10578         {
10579           operands[0] = copy_rtx (operands[0]);
10580           PUT_MODE (operands[0], Pmode);
10581         }
10582       else
10583         operands[0] = gen_lowpart (DImode, operands[0]);
10584       operands[1] = gen_lowpart (DImode, operands[1]);
10585       emit_move_insn (operands[0], operands[1]);
10586       return;
10587     }
10588
10589   /* The only non-offsettable memory we handle is push.  */
10590   if (push_operand (operands[0], VOIDmode))
10591     push = 1;
10592   else if (GET_CODE (operands[0]) == MEM
10593            && ! offsettable_memref_p (operands[0]))
10594     abort ();
10595
10596   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
10597   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
10598
10599   /* When emitting push, take care for source operands on the stack.  */
10600   if (push && GET_CODE (operands[1]) == MEM
10601       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
10602     {
10603       if (nparts == 3)
10604         part[1][1] = change_address (part[1][1], GET_MODE (part[1][1]),
10605                                      XEXP (part[1][2], 0));
10606       part[1][0] = change_address (part[1][0], GET_MODE (part[1][0]),
10607                                    XEXP (part[1][1], 0));
10608     }
10609
10610   /* We need to do copy in the right order in case an address register
10611      of the source overlaps the destination.  */
10612   if (REG_P (part[0][0]) && GET_CODE (part[1][0]) == MEM)
10613     {
10614       if (reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0)))
10615         collisions++;
10616       if (reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
10617         collisions++;
10618       if (nparts == 3
10619           && reg_overlap_mentioned_p (part[0][2], XEXP (part[1][0], 0)))
10620         collisions++;
10621
10622       /* Collision in the middle part can be handled by reordering.  */
10623       if (collisions == 1 && nparts == 3
10624           && reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
10625         {
10626           rtx tmp;
10627           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
10628           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
10629         }
10630
10631       /* If there are more collisions, we can't handle it by reordering.
10632          Do an lea to the last part and use only one colliding move.  */
10633       else if (collisions > 1)
10634         {
10635           rtx base;
10636
10637           collisions = 1;
10638
10639           base = part[0][nparts - 1];
10640
10641           /* Handle the case when the last part isn't valid for lea.
10642              Happens in 64-bit mode storing the 12-byte XFmode.  */
10643           if (GET_MODE (base) != Pmode)
10644             base = gen_rtx_REG (Pmode, REGNO (base));
10645
10646           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
10647           part[1][0] = replace_equiv_address (part[1][0], base);
10648           part[1][1] = replace_equiv_address (part[1][1],
10649                                       plus_constant (base, UNITS_PER_WORD));
10650           if (nparts == 3)
10651             part[1][2] = replace_equiv_address (part[1][2],
10652                                       plus_constant (base, 8));
10653         }
10654     }
10655
10656   if (push)
10657     {
10658       if (!TARGET_64BIT)
10659         {
10660           if (nparts == 3)
10661             {
10662               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
10663                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, GEN_INT (-4)));
10664               emit_move_insn (part[0][2], part[1][2]);
10665             }
10666         }
10667       else
10668         {
10669           /* In 64bit mode we don't have 32bit push available.  In case this is
10670              register, it is OK - we will just use larger counterpart.  We also
10671              retype memory - these comes from attempt to avoid REX prefix on
10672              moving of second half of TFmode value.  */
10673           if (GET_MODE (part[1][1]) == SImode)
10674             {
10675               if (GET_CODE (part[1][1]) == MEM)
10676                 part[1][1] = adjust_address (part[1][1], DImode, 0);
10677               else if (REG_P (part[1][1]))
10678                 part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
10679               else
10680                 abort ();
10681               if (GET_MODE (part[1][0]) == SImode)
10682                 part[1][0] = part[1][1];
10683             }
10684         }
10685       emit_move_insn (part[0][1], part[1][1]);
10686       emit_move_insn (part[0][0], part[1][0]);
10687       return;
10688     }
10689
10690   /* Choose correct order to not overwrite the source before it is copied.  */
10691   if ((REG_P (part[0][0])
10692        && REG_P (part[1][1])
10693        && (REGNO (part[0][0]) == REGNO (part[1][1])
10694            || (nparts == 3
10695                && REGNO (part[0][0]) == REGNO (part[1][2]))))
10696       || (collisions > 0
10697           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
10698     {
10699       if (nparts == 3)
10700         {
10701           operands[2] = part[0][2];
10702           operands[3] = part[0][1];
10703           operands[4] = part[0][0];
10704           operands[5] = part[1][2];
10705           operands[6] = part[1][1];
10706           operands[7] = part[1][0];
10707         }
10708       else
10709         {
10710           operands[2] = part[0][1];
10711           operands[3] = part[0][0];
10712           operands[5] = part[1][1];
10713           operands[6] = part[1][0];
10714         }
10715     }
10716   else
10717     {
10718       if (nparts == 3)
10719         {
10720           operands[2] = part[0][0];
10721           operands[3] = part[0][1];
10722           operands[4] = part[0][2];
10723           operands[5] = part[1][0];
10724           operands[6] = part[1][1];
10725           operands[7] = part[1][2];
10726         }
10727       else
10728         {
10729           operands[2] = part[0][0];
10730           operands[3] = part[0][1];
10731           operands[5] = part[1][0];
10732           operands[6] = part[1][1];
10733         }
10734     }
10735   emit_move_insn (operands[2], operands[5]);
10736   emit_move_insn (operands[3], operands[6]);
10737   if (nparts == 3)
10738     emit_move_insn (operands[4], operands[7]);
10739
10740   return;
10741 }
10742
10743 void
10744 ix86_split_ashldi (rtx *operands, rtx scratch)
10745 {
10746   rtx low[2], high[2];
10747   int count;
10748
10749   if (GET_CODE (operands[2]) == CONST_INT)
10750     {
10751       split_di (operands, 2, low, high);
10752       count = INTVAL (operands[2]) & 63;
10753
10754       if (count >= 32)
10755         {
10756           emit_move_insn (high[0], low[1]);
10757           emit_move_insn (low[0], const0_rtx);
10758
10759           if (count > 32)
10760             emit_insn (gen_ashlsi3 (high[0], high[0], GEN_INT (count - 32)));
10761         }
10762       else
10763         {
10764           if (!rtx_equal_p (operands[0], operands[1]))
10765             emit_move_insn (operands[0], operands[1]);
10766           emit_insn (gen_x86_shld_1 (high[0], low[0], GEN_INT (count)));
10767           emit_insn (gen_ashlsi3 (low[0], low[0], GEN_INT (count)));
10768         }
10769     }
10770   else
10771     {
10772       if (!rtx_equal_p (operands[0], operands[1]))
10773         emit_move_insn (operands[0], operands[1]);
10774
10775       split_di (operands, 1, low, high);
10776
10777       emit_insn (gen_x86_shld_1 (high[0], low[0], operands[2]));
10778       emit_insn (gen_ashlsi3 (low[0], low[0], operands[2]));
10779
10780       if (TARGET_CMOVE && (! no_new_pseudos || scratch))
10781         {
10782           if (! no_new_pseudos)
10783             scratch = force_reg (SImode, const0_rtx);
10784           else
10785             emit_move_insn (scratch, const0_rtx);
10786
10787           emit_insn (gen_x86_shift_adj_1 (high[0], low[0], operands[2],
10788                                           scratch));
10789         }
10790       else
10791         emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
10792     }
10793 }
10794
10795 void
10796 ix86_split_ashrdi (rtx *operands, rtx scratch)
10797 {
10798   rtx low[2], high[2];
10799   int count;
10800
10801   if (GET_CODE (operands[2]) == CONST_INT)
10802     {
10803       split_di (operands, 2, low, high);
10804       count = INTVAL (operands[2]) & 63;
10805
10806       if (count >= 32)
10807         {
10808           emit_move_insn (low[0], high[1]);
10809
10810           if (! reload_completed)
10811             emit_insn (gen_ashrsi3 (high[0], low[0], GEN_INT (31)));
10812           else
10813             {
10814               emit_move_insn (high[0], low[0]);
10815               emit_insn (gen_ashrsi3 (high[0], high[0], GEN_INT (31)));
10816             }
10817
10818           if (count > 32)
10819             emit_insn (gen_ashrsi3 (low[0], low[0], GEN_INT (count - 32)));
10820         }
10821       else
10822         {
10823           if (!rtx_equal_p (operands[0], operands[1]))
10824             emit_move_insn (operands[0], operands[1]);
10825           emit_insn (gen_x86_shrd_1 (low[0], high[0], GEN_INT (count)));
10826           emit_insn (gen_ashrsi3 (high[0], high[0], GEN_INT (count)));
10827         }
10828     }
10829   else
10830     {
10831       if (!rtx_equal_p (operands[0], operands[1]))
10832         emit_move_insn (operands[0], operands[1]);
10833
10834       split_di (operands, 1, low, high);
10835
10836       emit_insn (gen_x86_shrd_1 (low[0], high[0], operands[2]));
10837       emit_insn (gen_ashrsi3 (high[0], high[0], operands[2]));
10838
10839       if (TARGET_CMOVE && (! no_new_pseudos || scratch))
10840         {
10841           if (! no_new_pseudos)
10842             scratch = gen_reg_rtx (SImode);
10843           emit_move_insn (scratch, high[0]);
10844           emit_insn (gen_ashrsi3 (scratch, scratch, GEN_INT (31)));
10845           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
10846                                           scratch));
10847         }
10848       else
10849         emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
10850     }
10851 }
10852
10853 void
10854 ix86_split_lshrdi (rtx *operands, rtx scratch)
10855 {
10856   rtx low[2], high[2];
10857   int count;
10858
10859   if (GET_CODE (operands[2]) == CONST_INT)
10860     {
10861       split_di (operands, 2, low, high);
10862       count = INTVAL (operands[2]) & 63;
10863
10864       if (count >= 32)
10865         {
10866           emit_move_insn (low[0], high[1]);
10867           emit_move_insn (high[0], const0_rtx);
10868
10869           if (count > 32)
10870             emit_insn (gen_lshrsi3 (low[0], low[0], GEN_INT (count - 32)));
10871         }
10872       else
10873         {
10874           if (!rtx_equal_p (operands[0], operands[1]))
10875             emit_move_insn (operands[0], operands[1]);
10876           emit_insn (gen_x86_shrd_1 (low[0], high[0], GEN_INT (count)));
10877           emit_insn (gen_lshrsi3 (high[0], high[0], GEN_INT (count)));
10878         }
10879     }
10880   else
10881     {
10882       if (!rtx_equal_p (operands[0], operands[1]))
10883         emit_move_insn (operands[0], operands[1]);
10884
10885       split_di (operands, 1, low, high);
10886
10887       emit_insn (gen_x86_shrd_1 (low[0], high[0], operands[2]));
10888       emit_insn (gen_lshrsi3 (high[0], high[0], operands[2]));
10889
10890       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
10891       if (TARGET_CMOVE && (! no_new_pseudos || scratch))
10892         {
10893           if (! no_new_pseudos)
10894             scratch = force_reg (SImode, const0_rtx);
10895           else
10896             emit_move_insn (scratch, const0_rtx);
10897
10898           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
10899                                           scratch));
10900         }
10901       else
10902         emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
10903     }
10904 }
10905
10906 /* Helper function for the string operations below.  Dest VARIABLE whether
10907    it is aligned to VALUE bytes.  If true, jump to the label.  */
10908 static rtx
10909 ix86_expand_aligntest (rtx variable, int value)
10910 {
10911   rtx label = gen_label_rtx ();
10912   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
10913   if (GET_MODE (variable) == DImode)
10914     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
10915   else
10916     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
10917   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
10918                            1, label);
10919   return label;
10920 }
10921
10922 /* Adjust COUNTER by the VALUE.  */
10923 static void
10924 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
10925 {
10926   if (GET_MODE (countreg) == DImode)
10927     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
10928   else
10929     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
10930 }
10931
10932 /* Zero extend possibly SImode EXP to Pmode register.  */
10933 rtx
10934 ix86_zero_extend_to_Pmode (rtx exp)
10935 {
10936   rtx r;
10937   if (GET_MODE (exp) == VOIDmode)
10938     return force_reg (Pmode, exp);
10939   if (GET_MODE (exp) == Pmode)
10940     return copy_to_mode_reg (Pmode, exp);
10941   r = gen_reg_rtx (Pmode);
10942   emit_insn (gen_zero_extendsidi2 (r, exp));
10943   return r;
10944 }
10945
10946 /* Expand string move (memcpy) operation.  Use i386 string operations when
10947    profitable.  expand_clrstr contains similar code.  */
10948 int
10949 ix86_expand_movstr (rtx dst, rtx src, rtx count_exp, rtx align_exp)
10950 {
10951   rtx srcreg, destreg, countreg;
10952   enum machine_mode counter_mode;
10953   HOST_WIDE_INT align = 0;
10954   unsigned HOST_WIDE_INT count = 0;
10955   rtx insns;
10956
10957   if (GET_CODE (align_exp) == CONST_INT)
10958     align = INTVAL (align_exp);
10959
10960   /* Can't use any of this if the user has appropriated esi or edi.  */
10961   if (global_regs[4] || global_regs[5])
10962     return 0;
10963
10964   /* This simple hack avoids all inlining code and simplifies code below.  */
10965   if (!TARGET_ALIGN_STRINGOPS)
10966     align = 64;
10967
10968   if (GET_CODE (count_exp) == CONST_INT)
10969     {
10970       count = INTVAL (count_exp);
10971       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
10972         return 0;
10973     }
10974
10975   /* Figure out proper mode for counter.  For 32bits it is always SImode,
10976      for 64bits use SImode when possible, otherwise DImode.
10977      Set count to number of bytes copied when known at compile time.  */
10978   if (!TARGET_64BIT || GET_MODE (count_exp) == SImode
10979       || x86_64_zero_extended_value (count_exp))
10980     counter_mode = SImode;
10981   else
10982     counter_mode = DImode;
10983
10984   start_sequence ();
10985
10986   if (counter_mode != SImode && counter_mode != DImode)
10987     abort ();
10988
10989   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
10990   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
10991
10992   emit_insn (gen_cld ());
10993
10994   /* When optimizing for size emit simple rep ; movsb instruction for
10995      counts not divisible by 4.  */
10996
10997   if ((!optimize || optimize_size) && (count == 0 || (count & 0x03)))
10998     {
10999       countreg = ix86_zero_extend_to_Pmode (count_exp);
11000       if (TARGET_64BIT)
11001         emit_insn (gen_rep_movqi_rex64 (destreg, srcreg, countreg,
11002                                         destreg, srcreg, countreg));
11003       else
11004         emit_insn (gen_rep_movqi (destreg, srcreg, countreg,
11005                                   destreg, srcreg, countreg));
11006     }
11007
11008   /* For constant aligned (or small unaligned) copies use rep movsl
11009      followed by code copying the rest.  For PentiumPro ensure 8 byte
11010      alignment to allow rep movsl acceleration.  */
11011
11012   else if (count != 0
11013            && (align >= 8
11014                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
11015                || optimize_size || count < (unsigned int) 64))
11016     {
11017       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
11018       if (count & ~(size - 1))
11019         {
11020           countreg = copy_to_mode_reg (counter_mode,
11021                                        GEN_INT ((count >> (size == 4 ? 2 : 3))
11022                                                 & (TARGET_64BIT ? -1 : 0x3fffffff)));
11023           countreg = ix86_zero_extend_to_Pmode (countreg);
11024           if (size == 4)
11025             {
11026               if (TARGET_64BIT)
11027                 emit_insn (gen_rep_movsi_rex64 (destreg, srcreg, countreg,
11028                                                 destreg, srcreg, countreg));
11029               else
11030                 emit_insn (gen_rep_movsi (destreg, srcreg, countreg,
11031                                           destreg, srcreg, countreg));
11032             }
11033           else
11034             emit_insn (gen_rep_movdi_rex64 (destreg, srcreg, countreg,
11035                                             destreg, srcreg, countreg));
11036         }
11037       if (size == 8 && (count & 0x04))
11038         emit_insn (gen_strmovsi (destreg, srcreg));
11039       if (count & 0x02)
11040         emit_insn (gen_strmovhi (destreg, srcreg));
11041       if (count & 0x01)
11042         emit_insn (gen_strmovqi (destreg, srcreg));
11043     }
11044   /* The generic code based on the glibc implementation:
11045      - align destination to 4 bytes (8 byte alignment is used for PentiumPro
11046      allowing accelerated copying there)
11047      - copy the data using rep movsl
11048      - copy the rest.  */
11049   else
11050     {
11051       rtx countreg2;
11052       rtx label = NULL;
11053       int desired_alignment = (TARGET_PENTIUMPRO
11054                                && (count == 0 || count >= (unsigned int) 260)
11055                                ? 8 : UNITS_PER_WORD);
11056
11057       /* In case we don't know anything about the alignment, default to
11058          library version, since it is usually equally fast and result in
11059          shorter code.
11060
11061          Also emit call when we know that the count is large and call overhead
11062          will not be important.  */
11063       if (!TARGET_INLINE_ALL_STRINGOPS
11064           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
11065         {
11066           end_sequence ();
11067           return 0;
11068         }
11069
11070       if (TARGET_SINGLE_STRINGOP)
11071         emit_insn (gen_cld ());
11072
11073       countreg2 = gen_reg_rtx (Pmode);
11074       countreg = copy_to_mode_reg (counter_mode, count_exp);
11075
11076       /* We don't use loops to align destination and to copy parts smaller
11077          than 4 bytes, because gcc is able to optimize such code better (in
11078          the case the destination or the count really is aligned, gcc is often
11079          able to predict the branches) and also it is friendlier to the
11080          hardware branch prediction.
11081
11082          Using loops is beneficial for generic case, because we can
11083          handle small counts using the loops.  Many CPUs (such as Athlon)
11084          have large REP prefix setup costs.
11085
11086          This is quite costly.  Maybe we can revisit this decision later or
11087          add some customizability to this code.  */
11088
11089       if (count == 0 && align < desired_alignment)
11090         {
11091           label = gen_label_rtx ();
11092           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
11093                                    LEU, 0, counter_mode, 1, label);
11094         }
11095       if (align <= 1)
11096         {
11097           rtx label = ix86_expand_aligntest (destreg, 1);
11098           emit_insn (gen_strmovqi (destreg, srcreg));
11099           ix86_adjust_counter (countreg, 1);
11100           emit_label (label);
11101           LABEL_NUSES (label) = 1;
11102         }
11103       if (align <= 2)
11104         {
11105           rtx label = ix86_expand_aligntest (destreg, 2);
11106           emit_insn (gen_strmovhi (destreg, srcreg));
11107           ix86_adjust_counter (countreg, 2);
11108           emit_label (label);
11109           LABEL_NUSES (label) = 1;
11110         }
11111       if (align <= 4 && desired_alignment > 4)
11112         {
11113           rtx label = ix86_expand_aligntest (destreg, 4);
11114           emit_insn (gen_strmovsi (destreg, srcreg));
11115           ix86_adjust_counter (countreg, 4);
11116           emit_label (label);
11117           LABEL_NUSES (label) = 1;
11118         }
11119
11120       if (label && desired_alignment > 4 && !TARGET_64BIT)
11121         {
11122           emit_label (label);
11123           LABEL_NUSES (label) = 1;
11124           label = NULL_RTX;
11125         }
11126       if (!TARGET_SINGLE_STRINGOP)
11127         emit_insn (gen_cld ());
11128       if (TARGET_64BIT)
11129         {
11130           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
11131                                   GEN_INT (3)));
11132           emit_insn (gen_rep_movdi_rex64 (destreg, srcreg, countreg2,
11133                                           destreg, srcreg, countreg2));
11134         }
11135       else
11136         {
11137           emit_insn (gen_lshrsi3 (countreg2, countreg, GEN_INT (2)));
11138           emit_insn (gen_rep_movsi (destreg, srcreg, countreg2,
11139                                     destreg, srcreg, countreg2));
11140         }
11141
11142       if (label)
11143         {
11144           emit_label (label);
11145           LABEL_NUSES (label) = 1;
11146         }
11147       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
11148         emit_insn (gen_strmovsi (destreg, srcreg));
11149       if ((align <= 4 || count == 0) && TARGET_64BIT)
11150         {
11151           rtx label = ix86_expand_aligntest (countreg, 4);
11152           emit_insn (gen_strmovsi (destreg, srcreg));
11153           emit_label (label);
11154           LABEL_NUSES (label) = 1;
11155         }
11156       if (align > 2 && count != 0 && (count & 2))
11157         emit_insn (gen_strmovhi (destreg, srcreg));
11158       if (align <= 2 || count == 0)
11159         {
11160           rtx label = ix86_expand_aligntest (countreg, 2);
11161           emit_insn (gen_strmovhi (destreg, srcreg));
11162           emit_label (label);
11163           LABEL_NUSES (label) = 1;
11164         }
11165       if (align > 1 && count != 0 && (count & 1))
11166         emit_insn (gen_strmovqi (destreg, srcreg));
11167       if (align <= 1 || count == 0)
11168         {
11169           rtx label = ix86_expand_aligntest (countreg, 1);
11170           emit_insn (gen_strmovqi (destreg, srcreg));
11171           emit_label (label);
11172           LABEL_NUSES (label) = 1;
11173         }
11174     }
11175
11176   insns = get_insns ();
11177   end_sequence ();
11178
11179   ix86_set_move_mem_attrs (insns, dst, src, destreg, srcreg);
11180   emit_insn (insns);
11181   return 1;
11182 }
11183
11184 /* Expand string clear operation (bzero).  Use i386 string operations when
11185    profitable.  expand_movstr contains similar code.  */
11186 int
11187 ix86_expand_clrstr (rtx src, rtx count_exp, rtx align_exp)
11188 {
11189   rtx destreg, zeroreg, countreg;
11190   enum machine_mode counter_mode;
11191   HOST_WIDE_INT align = 0;
11192   unsigned HOST_WIDE_INT count = 0;
11193
11194   if (GET_CODE (align_exp) == CONST_INT)
11195     align = INTVAL (align_exp);
11196
11197   /* Can't use any of this if the user has appropriated esi.  */
11198   if (global_regs[4])
11199     return 0;
11200
11201   /* This simple hack avoids all inlining code and simplifies code below.  */
11202   if (!TARGET_ALIGN_STRINGOPS)
11203     align = 32;
11204
11205   if (GET_CODE (count_exp) == CONST_INT)
11206     {
11207       count = INTVAL (count_exp);
11208       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
11209         return 0;
11210     }
11211   /* Figure out proper mode for counter.  For 32bits it is always SImode,
11212      for 64bits use SImode when possible, otherwise DImode.
11213      Set count to number of bytes copied when known at compile time.  */
11214   if (!TARGET_64BIT || GET_MODE (count_exp) == SImode
11215       || x86_64_zero_extended_value (count_exp))
11216     counter_mode = SImode;
11217   else
11218     counter_mode = DImode;
11219
11220   destreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
11221
11222   emit_insn (gen_cld ());
11223
11224   /* When optimizing for size emit simple rep ; movsb instruction for
11225      counts not divisible by 4.  */
11226
11227   if ((!optimize || optimize_size) && (count == 0 || (count & 0x03)))
11228     {
11229       countreg = ix86_zero_extend_to_Pmode (count_exp);
11230       zeroreg = copy_to_mode_reg (QImode, const0_rtx);
11231       if (TARGET_64BIT)
11232         emit_insn (gen_rep_stosqi_rex64 (destreg, countreg, zeroreg,
11233                                          destreg, countreg));
11234       else
11235         emit_insn (gen_rep_stosqi (destreg, countreg, zeroreg,
11236                                    destreg, countreg));
11237     }
11238   else if (count != 0
11239            && (align >= 8
11240                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
11241                || optimize_size || count < (unsigned int) 64))
11242     {
11243       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
11244       zeroreg = copy_to_mode_reg (size == 4 ? SImode : DImode, const0_rtx);
11245       if (count & ~(size - 1))
11246         {
11247           countreg = copy_to_mode_reg (counter_mode,
11248                                        GEN_INT ((count >> (size == 4 ? 2 : 3))
11249                                                 & (TARGET_64BIT ? -1 : 0x3fffffff)));
11250           countreg = ix86_zero_extend_to_Pmode (countreg);
11251           if (size == 4)
11252             {
11253               if (TARGET_64BIT)
11254                 emit_insn (gen_rep_stossi_rex64 (destreg, countreg, zeroreg,
11255                                                  destreg, countreg));
11256               else
11257                 emit_insn (gen_rep_stossi (destreg, countreg, zeroreg,
11258                                            destreg, countreg));
11259             }
11260           else
11261             emit_insn (gen_rep_stosdi_rex64 (destreg, countreg, zeroreg,
11262                                              destreg, countreg));
11263         }
11264       if (size == 8 && (count & 0x04))
11265         emit_insn (gen_strsetsi (destreg,
11266                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
11267       if (count & 0x02)
11268         emit_insn (gen_strsethi (destreg,
11269                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
11270       if (count & 0x01)
11271         emit_insn (gen_strsetqi (destreg,
11272                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
11273     }
11274   else
11275     {
11276       rtx countreg2;
11277       rtx label = NULL;
11278       /* Compute desired alignment of the string operation.  */
11279       int desired_alignment = (TARGET_PENTIUMPRO
11280                                && (count == 0 || count >= (unsigned int) 260)
11281                                ? 8 : UNITS_PER_WORD);
11282
11283       /* In case we don't know anything about the alignment, default to
11284          library version, since it is usually equally fast and result in
11285          shorter code.
11286
11287          Also emit call when we know that the count is large and call overhead
11288          will not be important.  */
11289       if (!TARGET_INLINE_ALL_STRINGOPS
11290           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
11291         return 0;
11292
11293       if (TARGET_SINGLE_STRINGOP)
11294         emit_insn (gen_cld ());
11295
11296       countreg2 = gen_reg_rtx (Pmode);
11297       countreg = copy_to_mode_reg (counter_mode, count_exp);
11298       zeroreg = copy_to_mode_reg (Pmode, const0_rtx);
11299
11300       if (count == 0 && align < desired_alignment)
11301         {
11302           label = gen_label_rtx ();
11303           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
11304                                    LEU, 0, counter_mode, 1, label);
11305         }
11306       if (align <= 1)
11307         {
11308           rtx label = ix86_expand_aligntest (destreg, 1);
11309           emit_insn (gen_strsetqi (destreg,
11310                                    gen_rtx_SUBREG (QImode, zeroreg, 0)));
11311           ix86_adjust_counter (countreg, 1);
11312           emit_label (label);
11313           LABEL_NUSES (label) = 1;
11314         }
11315       if (align <= 2)
11316         {
11317           rtx label = ix86_expand_aligntest (destreg, 2);
11318           emit_insn (gen_strsethi (destreg,
11319                                    gen_rtx_SUBREG (HImode, zeroreg, 0)));
11320           ix86_adjust_counter (countreg, 2);
11321           emit_label (label);
11322           LABEL_NUSES (label) = 1;
11323         }
11324       if (align <= 4 && desired_alignment > 4)
11325         {
11326           rtx label = ix86_expand_aligntest (destreg, 4);
11327           emit_insn (gen_strsetsi (destreg, (TARGET_64BIT
11328                                              ? gen_rtx_SUBREG (SImode, zeroreg, 0)
11329                                              : zeroreg)));
11330           ix86_adjust_counter (countreg, 4);
11331           emit_label (label);
11332           LABEL_NUSES (label) = 1;
11333         }
11334
11335       if (label && desired_alignment > 4 && !TARGET_64BIT)
11336         {
11337           emit_label (label);
11338           LABEL_NUSES (label) = 1;
11339           label = NULL_RTX;
11340         }
11341
11342       if (!TARGET_SINGLE_STRINGOP)
11343         emit_insn (gen_cld ());
11344       if (TARGET_64BIT)
11345         {
11346           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
11347                                   GEN_INT (3)));
11348           emit_insn (gen_rep_stosdi_rex64 (destreg, countreg2, zeroreg,
11349                                            destreg, countreg2));
11350         }
11351       else
11352         {
11353           emit_insn (gen_lshrsi3 (countreg2, countreg, GEN_INT (2)));
11354           emit_insn (gen_rep_stossi (destreg, countreg2, zeroreg,
11355                                      destreg, countreg2));
11356         }
11357       if (label)
11358         {
11359           emit_label (label);
11360           LABEL_NUSES (label) = 1;
11361         }
11362
11363       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
11364         emit_insn (gen_strsetsi (destreg,
11365                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
11366       if (TARGET_64BIT && (align <= 4 || count == 0))
11367         {
11368           rtx label = ix86_expand_aligntest (countreg, 4);
11369           emit_insn (gen_strsetsi (destreg,
11370                                    gen_rtx_SUBREG (SImode, zeroreg, 0)));
11371           emit_label (label);
11372           LABEL_NUSES (label) = 1;
11373         }
11374       if (align > 2 && count != 0 && (count & 2))
11375         emit_insn (gen_strsethi (destreg,
11376                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
11377       if (align <= 2 || count == 0)
11378         {
11379           rtx label = ix86_expand_aligntest (countreg, 2);
11380           emit_insn (gen_strsethi (destreg,
11381                                    gen_rtx_SUBREG (HImode, zeroreg, 0)));
11382           emit_label (label);
11383           LABEL_NUSES (label) = 1;
11384         }
11385       if (align > 1 && count != 0 && (count & 1))
11386         emit_insn (gen_strsetqi (destreg,
11387                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
11388       if (align <= 1 || count == 0)
11389         {
11390           rtx label = ix86_expand_aligntest (countreg, 1);
11391           emit_insn (gen_strsetqi (destreg,
11392                                    gen_rtx_SUBREG (QImode, zeroreg, 0)));
11393           emit_label (label);
11394           LABEL_NUSES (label) = 1;
11395         }
11396     }
11397   return 1;
11398 }
11399 /* Expand strlen.  */
11400 int
11401 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
11402 {
11403   rtx addr, scratch1, scratch2, scratch3, scratch4;
11404
11405   /* The generic case of strlen expander is long.  Avoid it's
11406      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
11407
11408   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
11409       && !TARGET_INLINE_ALL_STRINGOPS
11410       && !optimize_size
11411       && (GET_CODE (align) != CONST_INT || INTVAL (align) < 4))
11412     return 0;
11413
11414   addr = force_reg (Pmode, XEXP (src, 0));
11415   scratch1 = gen_reg_rtx (Pmode);
11416
11417   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
11418       && !optimize_size)
11419     {
11420       /* Well it seems that some optimizer does not combine a call like
11421          foo(strlen(bar), strlen(bar));
11422          when the move and the subtraction is done here.  It does calculate
11423          the length just once when these instructions are done inside of
11424          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
11425          often used and I use one fewer register for the lifetime of
11426          output_strlen_unroll() this is better.  */
11427
11428       emit_move_insn (out, addr);
11429
11430       ix86_expand_strlensi_unroll_1 (out, align);
11431
11432       /* strlensi_unroll_1 returns the address of the zero at the end of
11433          the string, like memchr(), so compute the length by subtracting
11434          the start address.  */
11435       if (TARGET_64BIT)
11436         emit_insn (gen_subdi3 (out, out, addr));
11437       else
11438         emit_insn (gen_subsi3 (out, out, addr));
11439     }
11440   else
11441     {
11442       scratch2 = gen_reg_rtx (Pmode);
11443       scratch3 = gen_reg_rtx (Pmode);
11444       scratch4 = force_reg (Pmode, constm1_rtx);
11445
11446       emit_move_insn (scratch3, addr);
11447       eoschar = force_reg (QImode, eoschar);
11448
11449       emit_insn (gen_cld ());
11450       if (TARGET_64BIT)
11451         {
11452           emit_insn (gen_strlenqi_rex_1 (scratch1, scratch3, eoschar,
11453                                          align, scratch4, scratch3));
11454           emit_insn (gen_one_cmpldi2 (scratch2, scratch1));
11455           emit_insn (gen_adddi3 (out, scratch2, constm1_rtx));
11456         }
11457       else
11458         {
11459           emit_insn (gen_strlenqi_1 (scratch1, scratch3, eoschar,
11460                                      align, scratch4, scratch3));
11461           emit_insn (gen_one_cmplsi2 (scratch2, scratch1));
11462           emit_insn (gen_addsi3 (out, scratch2, constm1_rtx));
11463         }
11464     }
11465   return 1;
11466 }
11467
11468 /* Expand the appropriate insns for doing strlen if not just doing
11469    repnz; scasb
11470
11471    out = result, initialized with the start address
11472    align_rtx = alignment of the address.
11473    scratch = scratch register, initialized with the startaddress when
11474         not aligned, otherwise undefined
11475
11476    This is just the body. It needs the initializations mentioned above and
11477    some address computing at the end.  These things are done in i386.md.  */
11478
11479 static void
11480 ix86_expand_strlensi_unroll_1 (rtx out, rtx align_rtx)
11481 {
11482   int align;
11483   rtx tmp;
11484   rtx align_2_label = NULL_RTX;
11485   rtx align_3_label = NULL_RTX;
11486   rtx align_4_label = gen_label_rtx ();
11487   rtx end_0_label = gen_label_rtx ();
11488   rtx mem;
11489   rtx tmpreg = gen_reg_rtx (SImode);
11490   rtx scratch = gen_reg_rtx (SImode);
11491   rtx cmp;
11492
11493   align = 0;
11494   if (GET_CODE (align_rtx) == CONST_INT)
11495     align = INTVAL (align_rtx);
11496
11497   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
11498
11499   /* Is there a known alignment and is it less than 4?  */
11500   if (align < 4)
11501     {
11502       rtx scratch1 = gen_reg_rtx (Pmode);
11503       emit_move_insn (scratch1, out);
11504       /* Is there a known alignment and is it not 2? */
11505       if (align != 2)
11506         {
11507           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
11508           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
11509
11510           /* Leave just the 3 lower bits.  */
11511           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
11512                                     NULL_RTX, 0, OPTAB_WIDEN);
11513
11514           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
11515                                    Pmode, 1, align_4_label);
11516           emit_cmp_and_jump_insns (align_rtx, GEN_INT (2), EQ, NULL,
11517                                    Pmode, 1, align_2_label);
11518           emit_cmp_and_jump_insns (align_rtx, GEN_INT (2), GTU, NULL,
11519                                    Pmode, 1, align_3_label);
11520         }
11521       else
11522         {
11523           /* Since the alignment is 2, we have to check 2 or 0 bytes;
11524              check if is aligned to 4 - byte.  */
11525
11526           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (2),
11527                                     NULL_RTX, 0, OPTAB_WIDEN);
11528
11529           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
11530                                    Pmode, 1, align_4_label);
11531         }
11532
11533       mem = gen_rtx_MEM (QImode, out);
11534
11535       /* Now compare the bytes.  */
11536
11537       /* Compare the first n unaligned byte on a byte per byte basis.  */
11538       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
11539                                QImode, 1, end_0_label);
11540
11541       /* Increment the address.  */
11542       if (TARGET_64BIT)
11543         emit_insn (gen_adddi3 (out, out, const1_rtx));
11544       else
11545         emit_insn (gen_addsi3 (out, out, const1_rtx));
11546
11547       /* Not needed with an alignment of 2 */
11548       if (align != 2)
11549         {
11550           emit_label (align_2_label);
11551
11552           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
11553                                    end_0_label);
11554
11555           if (TARGET_64BIT)
11556             emit_insn (gen_adddi3 (out, out, const1_rtx));
11557           else
11558             emit_insn (gen_addsi3 (out, out, const1_rtx));
11559
11560           emit_label (align_3_label);
11561         }
11562
11563       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
11564                                end_0_label);
11565
11566       if (TARGET_64BIT)
11567         emit_insn (gen_adddi3 (out, out, const1_rtx));
11568       else
11569         emit_insn (gen_addsi3 (out, out, const1_rtx));
11570     }
11571
11572   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
11573      align this loop.  It gives only huge programs, but does not help to
11574      speed up.  */
11575   emit_label (align_4_label);
11576
11577   mem = gen_rtx_MEM (SImode, out);
11578   emit_move_insn (scratch, mem);
11579   if (TARGET_64BIT)
11580     emit_insn (gen_adddi3 (out, out, GEN_INT (4)));
11581   else
11582     emit_insn (gen_addsi3 (out, out, GEN_INT (4)));
11583
11584   /* This formula yields a nonzero result iff one of the bytes is zero.
11585      This saves three branches inside loop and many cycles.  */
11586
11587   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
11588   emit_insn (gen_one_cmplsi2 (scratch, scratch));
11589   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
11590   emit_insn (gen_andsi3 (tmpreg, tmpreg,
11591                          gen_int_mode (0x80808080, SImode)));
11592   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
11593                            align_4_label);
11594
11595   if (TARGET_CMOVE)
11596     {
11597        rtx reg = gen_reg_rtx (SImode);
11598        rtx reg2 = gen_reg_rtx (Pmode);
11599        emit_move_insn (reg, tmpreg);
11600        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
11601
11602        /* If zero is not in the first two bytes, move two bytes forward.  */
11603        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
11604        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
11605        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
11606        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
11607                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
11608                                                      reg,
11609                                                      tmpreg)));
11610        /* Emit lea manually to avoid clobbering of flags.  */
11611        emit_insn (gen_rtx_SET (SImode, reg2,
11612                                gen_rtx_PLUS (Pmode, out, GEN_INT (2))));
11613
11614        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
11615        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
11616        emit_insn (gen_rtx_SET (VOIDmode, out,
11617                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
11618                                                      reg2,
11619                                                      out)));
11620
11621     }
11622   else
11623     {
11624        rtx end_2_label = gen_label_rtx ();
11625        /* Is zero in the first two bytes? */
11626
11627        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
11628        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
11629        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
11630        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
11631                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
11632                             pc_rtx);
11633        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
11634        JUMP_LABEL (tmp) = end_2_label;
11635
11636        /* Not in the first two.  Move two bytes forward.  */
11637        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
11638        if (TARGET_64BIT)
11639          emit_insn (gen_adddi3 (out, out, GEN_INT (2)));
11640        else
11641          emit_insn (gen_addsi3 (out, out, GEN_INT (2)));
11642
11643        emit_label (end_2_label);
11644
11645     }
11646
11647   /* Avoid branch in fixing the byte.  */
11648   tmpreg = gen_lowpart (QImode, tmpreg);
11649   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
11650   cmp = gen_rtx_LTU (Pmode, gen_rtx_REG (CCmode, 17), const0_rtx);
11651   if (TARGET_64BIT)
11652     emit_insn (gen_subdi3_carry_rex64 (out, out, GEN_INT (3), cmp));
11653   else
11654     emit_insn (gen_subsi3_carry (out, out, GEN_INT (3), cmp));
11655
11656   emit_label (end_0_label);
11657 }
11658
11659 void
11660 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
11661                   rtx callarg2 ATTRIBUTE_UNUSED,
11662                   rtx pop, int sibcall)
11663 {
11664   rtx use = NULL, call;
11665
11666   if (pop == const0_rtx)
11667     pop = NULL;
11668   if (TARGET_64BIT && pop)
11669     abort ();
11670
11671 #if TARGET_MACHO
11672   if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
11673     fnaddr = machopic_indirect_call_target (fnaddr);
11674 #else
11675   /* Static functions and indirect calls don't need the pic register.  */
11676   if (! TARGET_64BIT && flag_pic
11677       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
11678       && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
11679     use_reg (&use, pic_offset_table_rtx);
11680
11681   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
11682     {
11683       rtx al = gen_rtx_REG (QImode, 0);
11684       emit_move_insn (al, callarg2);
11685       use_reg (&use, al);
11686     }
11687 #endif /* TARGET_MACHO */
11688
11689   if (! call_insn_operand (XEXP (fnaddr, 0), Pmode))
11690     {
11691       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
11692       fnaddr = gen_rtx_MEM (QImode, fnaddr);
11693     }
11694   if (sibcall && TARGET_64BIT
11695       && !constant_call_address_operand (XEXP (fnaddr, 0), Pmode))
11696     {
11697       rtx addr;
11698       addr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
11699       fnaddr = gen_rtx_REG (Pmode, FIRST_REX_INT_REG + 3 /* R11 */);
11700       emit_move_insn (fnaddr, addr);
11701       fnaddr = gen_rtx_MEM (QImode, fnaddr);
11702     }
11703
11704   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
11705   if (retval)
11706     call = gen_rtx_SET (VOIDmode, retval, call);
11707   if (pop)
11708     {
11709       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
11710       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
11711       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
11712     }
11713
11714   call = emit_call_insn (call);
11715   if (use)
11716     CALL_INSN_FUNCTION_USAGE (call) = use;
11717 }
11718
11719 \f
11720 /* Clear stack slot assignments remembered from previous functions.
11721    This is called from INIT_EXPANDERS once before RTL is emitted for each
11722    function.  */
11723
11724 static struct machine_function *
11725 ix86_init_machine_status (void)
11726 {
11727   struct machine_function *f;
11728
11729   f = ggc_alloc_cleared (sizeof (struct machine_function));
11730   f->use_fast_prologue_epilogue_nregs = -1;
11731
11732   return f;
11733 }
11734
11735 /* Return a MEM corresponding to a stack slot with mode MODE.
11736    Allocate a new slot if necessary.
11737
11738    The RTL for a function can have several slots available: N is
11739    which slot to use.  */
11740
11741 rtx
11742 assign_386_stack_local (enum machine_mode mode, int n)
11743 {
11744   struct stack_local_entry *s;
11745
11746   if (n < 0 || n >= MAX_386_STACK_LOCALS)
11747     abort ();
11748
11749   for (s = ix86_stack_locals; s; s = s->next)
11750     if (s->mode == mode && s->n == n)
11751       return s->rtl;
11752
11753   s = (struct stack_local_entry *)
11754     ggc_alloc (sizeof (struct stack_local_entry));
11755   s->n = n;
11756   s->mode = mode;
11757   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
11758
11759   s->next = ix86_stack_locals;
11760   ix86_stack_locals = s;
11761   return s->rtl;
11762 }
11763
11764 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
11765
11766 static GTY(()) rtx ix86_tls_symbol;
11767 rtx
11768 ix86_tls_get_addr (void)
11769 {
11770
11771   if (!ix86_tls_symbol)
11772     {
11773       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
11774                                             (TARGET_GNU_TLS && !TARGET_64BIT)
11775                                             ? "___tls_get_addr"
11776                                             : "__tls_get_addr");
11777     }
11778
11779   return ix86_tls_symbol;
11780 }
11781 \f
11782 /* Calculate the length of the memory address in the instruction
11783    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
11784
11785 static int
11786 memory_address_length (rtx addr)
11787 {
11788   struct ix86_address parts;
11789   rtx base, index, disp;
11790   int len;
11791
11792   if (GET_CODE (addr) == PRE_DEC
11793       || GET_CODE (addr) == POST_INC
11794       || GET_CODE (addr) == PRE_MODIFY
11795       || GET_CODE (addr) == POST_MODIFY)
11796     return 0;
11797
11798   if (! ix86_decompose_address (addr, &parts))
11799     abort ();
11800
11801   base = parts.base;
11802   index = parts.index;
11803   disp = parts.disp;
11804   len = 0;
11805
11806   /* Rule of thumb:
11807        - esp as the base always wants an index,
11808        - ebp as the base always wants a displacement.  */
11809
11810   /* Register Indirect.  */
11811   if (base && !index && !disp)
11812     {
11813       /* esp (for its index) and ebp (for its displacement) need
11814          the two-byte modrm form.  */
11815       if (addr == stack_pointer_rtx
11816           || addr == arg_pointer_rtx
11817           || addr == frame_pointer_rtx
11818           || addr == hard_frame_pointer_rtx)
11819         len = 1;
11820     }
11821
11822   /* Direct Addressing.  */
11823   else if (disp && !base && !index)
11824     len = 4;
11825
11826   else
11827     {
11828       /* Find the length of the displacement constant.  */
11829       if (disp)
11830         {
11831           if (GET_CODE (disp) == CONST_INT
11832               && CONST_OK_FOR_LETTER_P (INTVAL (disp), 'K')
11833               && base)
11834             len = 1;
11835           else
11836             len = 4;
11837         }
11838       /* ebp always wants a displacement.  */
11839       else if (base == hard_frame_pointer_rtx)
11840         len = 1;
11841
11842       /* An index requires the two-byte modrm form....  */
11843       if (index
11844           /* ...like esp, which always wants an index.  */
11845           || base == stack_pointer_rtx
11846           || base == arg_pointer_rtx
11847           || base == frame_pointer_rtx)
11848         len += 1;
11849     }
11850
11851   return len;
11852 }
11853
11854 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
11855    is set, expect that insn have 8bit immediate alternative.  */
11856 int
11857 ix86_attr_length_immediate_default (rtx insn, int shortform)
11858 {
11859   int len = 0;
11860   int i;
11861   extract_insn_cached (insn);
11862   for (i = recog_data.n_operands - 1; i >= 0; --i)
11863     if (CONSTANT_P (recog_data.operand[i]))
11864       {
11865         if (len)
11866           abort ();
11867         if (shortform
11868             && GET_CODE (recog_data.operand[i]) == CONST_INT
11869             && CONST_OK_FOR_LETTER_P (INTVAL (recog_data.operand[i]), 'K'))
11870           len = 1;
11871         else
11872           {
11873             switch (get_attr_mode (insn))
11874               {
11875                 case MODE_QI:
11876                   len+=1;
11877                   break;
11878                 case MODE_HI:
11879                   len+=2;
11880                   break;
11881                 case MODE_SI:
11882                   len+=4;
11883                   break;
11884                 /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
11885                 case MODE_DI:
11886                   len+=4;
11887                   break;
11888                 default:
11889                   fatal_insn ("unknown insn mode", insn);
11890               }
11891           }
11892       }
11893   return len;
11894 }
11895 /* Compute default value for "length_address" attribute.  */
11896 int
11897 ix86_attr_length_address_default (rtx insn)
11898 {
11899   int i;
11900
11901   if (get_attr_type (insn) == TYPE_LEA)
11902     {
11903       rtx set = PATTERN (insn);
11904       if (GET_CODE (set) == SET)
11905         ;
11906       else if (GET_CODE (set) == PARALLEL
11907                && GET_CODE (XVECEXP (set, 0, 0)) == SET)
11908         set = XVECEXP (set, 0, 0);
11909       else
11910         {
11911 #ifdef ENABLE_CHECKING
11912           abort ();
11913 #endif
11914           return 0;
11915         }
11916
11917       return memory_address_length (SET_SRC (set));
11918     }
11919
11920   extract_insn_cached (insn);
11921   for (i = recog_data.n_operands - 1; i >= 0; --i)
11922     if (GET_CODE (recog_data.operand[i]) == MEM)
11923       {
11924         return memory_address_length (XEXP (recog_data.operand[i], 0));
11925         break;
11926       }
11927   return 0;
11928 }
11929 \f
11930 /* Return the maximum number of instructions a cpu can issue.  */
11931
11932 static int
11933 ix86_issue_rate (void)
11934 {
11935   switch (ix86_tune)
11936     {
11937     case PROCESSOR_PENTIUM:
11938     case PROCESSOR_K6:
11939       return 2;
11940
11941     case PROCESSOR_PENTIUMPRO:
11942     case PROCESSOR_PENTIUM4:
11943     case PROCESSOR_ATHLON:
11944     case PROCESSOR_K8:
11945       return 3;
11946
11947     default:
11948       return 1;
11949     }
11950 }
11951
11952 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
11953    by DEP_INSN and nothing set by DEP_INSN.  */
11954
11955 static int
11956 ix86_flags_dependant (rtx insn, rtx dep_insn, enum attr_type insn_type)
11957 {
11958   rtx set, set2;
11959
11960   /* Simplify the test for uninteresting insns.  */
11961   if (insn_type != TYPE_SETCC
11962       && insn_type != TYPE_ICMOV
11963       && insn_type != TYPE_FCMOV
11964       && insn_type != TYPE_IBR)
11965     return 0;
11966
11967   if ((set = single_set (dep_insn)) != 0)
11968     {
11969       set = SET_DEST (set);
11970       set2 = NULL_RTX;
11971     }
11972   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
11973            && XVECLEN (PATTERN (dep_insn), 0) == 2
11974            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
11975            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
11976     {
11977       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
11978       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
11979     }
11980   else
11981     return 0;
11982
11983   if (GET_CODE (set) != REG || REGNO (set) != FLAGS_REG)
11984     return 0;
11985
11986   /* This test is true if the dependent insn reads the flags but
11987      not any other potentially set register.  */
11988   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
11989     return 0;
11990
11991   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
11992     return 0;
11993
11994   return 1;
11995 }
11996
11997 /* A subroutine of ix86_adjust_cost -- return true iff INSN has a memory
11998    address with operands set by DEP_INSN.  */
11999
12000 static int
12001 ix86_agi_dependant (rtx insn, rtx dep_insn, enum attr_type insn_type)
12002 {
12003   rtx addr;
12004
12005   if (insn_type == TYPE_LEA
12006       && TARGET_PENTIUM)
12007     {
12008       addr = PATTERN (insn);
12009       if (GET_CODE (addr) == SET)
12010         ;
12011       else if (GET_CODE (addr) == PARALLEL
12012                && GET_CODE (XVECEXP (addr, 0, 0)) == SET)
12013         addr = XVECEXP (addr, 0, 0);
12014       else
12015         abort ();
12016       addr = SET_SRC (addr);
12017     }
12018   else
12019     {
12020       int i;
12021       extract_insn_cached (insn);
12022       for (i = recog_data.n_operands - 1; i >= 0; --i)
12023         if (GET_CODE (recog_data.operand[i]) == MEM)
12024           {
12025             addr = XEXP (recog_data.operand[i], 0);
12026             goto found;
12027           }
12028       return 0;
12029     found:;
12030     }
12031
12032   return modified_in_p (addr, dep_insn);
12033 }
12034
12035 static int
12036 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
12037 {
12038   enum attr_type insn_type, dep_insn_type;
12039   enum attr_memory memory, dep_memory;
12040   rtx set, set2;
12041   int dep_insn_code_number;
12042
12043   /* Anti and output dependencies have zero cost on all CPUs.  */
12044   if (REG_NOTE_KIND (link) != 0)
12045     return 0;
12046
12047   dep_insn_code_number = recog_memoized (dep_insn);
12048
12049   /* If we can't recognize the insns, we can't really do anything.  */
12050   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
12051     return cost;
12052
12053   insn_type = get_attr_type (insn);
12054   dep_insn_type = get_attr_type (dep_insn);
12055
12056   switch (ix86_tune)
12057     {
12058     case PROCESSOR_PENTIUM:
12059       /* Address Generation Interlock adds a cycle of latency.  */
12060       if (ix86_agi_dependant (insn, dep_insn, insn_type))
12061         cost += 1;
12062
12063       /* ??? Compares pair with jump/setcc.  */
12064       if (ix86_flags_dependant (insn, dep_insn, insn_type))
12065         cost = 0;
12066
12067       /* Floating point stores require value to be ready one cycle earlier.  */
12068       if (insn_type == TYPE_FMOV
12069           && get_attr_memory (insn) == MEMORY_STORE
12070           && !ix86_agi_dependant (insn, dep_insn, insn_type))
12071         cost += 1;
12072       break;
12073
12074     case PROCESSOR_PENTIUMPRO:
12075       memory = get_attr_memory (insn);
12076       dep_memory = get_attr_memory (dep_insn);
12077
12078       /* Since we can't represent delayed latencies of load+operation,
12079          increase the cost here for non-imov insns.  */
12080       if (dep_insn_type != TYPE_IMOV
12081           && dep_insn_type != TYPE_FMOV
12082           && (dep_memory == MEMORY_LOAD || dep_memory == MEMORY_BOTH))
12083         cost += 1;
12084
12085       /* INT->FP conversion is expensive.  */
12086       if (get_attr_fp_int_src (dep_insn))
12087         cost += 5;
12088
12089       /* There is one cycle extra latency between an FP op and a store.  */
12090       if (insn_type == TYPE_FMOV
12091           && (set = single_set (dep_insn)) != NULL_RTX
12092           && (set2 = single_set (insn)) != NULL_RTX
12093           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
12094           && GET_CODE (SET_DEST (set2)) == MEM)
12095         cost += 1;
12096
12097       /* Show ability of reorder buffer to hide latency of load by executing
12098          in parallel with previous instruction in case
12099          previous instruction is not needed to compute the address.  */
12100       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
12101           && !ix86_agi_dependant (insn, dep_insn, insn_type))
12102         {
12103           /* Claim moves to take one cycle, as core can issue one load
12104              at time and the next load can start cycle later.  */
12105           if (dep_insn_type == TYPE_IMOV
12106               || dep_insn_type == TYPE_FMOV)
12107             cost = 1;
12108           else if (cost > 1)
12109             cost--;
12110         }
12111       break;
12112
12113     case PROCESSOR_K6:
12114       memory = get_attr_memory (insn);
12115       dep_memory = get_attr_memory (dep_insn);
12116       /* The esp dependency is resolved before the instruction is really
12117          finished.  */
12118       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
12119           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
12120         return 1;
12121
12122       /* Since we can't represent delayed latencies of load+operation,
12123          increase the cost here for non-imov insns.  */
12124       if (dep_memory == MEMORY_LOAD || dep_memory == MEMORY_BOTH)
12125         cost += (dep_insn_type != TYPE_IMOV) ? 2 : 1;
12126
12127       /* INT->FP conversion is expensive.  */
12128       if (get_attr_fp_int_src (dep_insn))
12129         cost += 5;
12130
12131       /* Show ability of reorder buffer to hide latency of load by executing
12132          in parallel with previous instruction in case
12133          previous instruction is not needed to compute the address.  */
12134       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
12135           && !ix86_agi_dependant (insn, dep_insn, insn_type))
12136         {
12137           /* Claim moves to take one cycle, as core can issue one load
12138              at time and the next load can start cycle later.  */
12139           if (dep_insn_type == TYPE_IMOV
12140               || dep_insn_type == TYPE_FMOV)
12141             cost = 1;
12142           else if (cost > 2)
12143             cost -= 2;
12144           else
12145             cost = 1;
12146         }
12147       break;
12148
12149     case PROCESSOR_ATHLON:
12150     case PROCESSOR_K8:
12151       memory = get_attr_memory (insn);
12152       dep_memory = get_attr_memory (dep_insn);
12153
12154       /* Show ability of reorder buffer to hide latency of load by executing
12155          in parallel with previous instruction in case
12156          previous instruction is not needed to compute the address.  */
12157       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
12158           && !ix86_agi_dependant (insn, dep_insn, insn_type))
12159         {
12160           enum attr_unit unit = get_attr_unit (insn);
12161           int loadcost = 3;
12162
12163           /* Because of the difference between the length of integer and
12164              floating unit pipeline preparation stages, the memory operands
12165              for floating point are cheaper.
12166
12167              ??? For Athlon it the difference is most probably 2.  */
12168           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
12169             loadcost = 3;
12170           else
12171             loadcost = TARGET_ATHLON ? 2 : 0;
12172
12173           if (cost >= loadcost)
12174             cost -= loadcost;
12175           else
12176             cost = 0;
12177         }
12178
12179     default:
12180       break;
12181     }
12182
12183   return cost;
12184 }
12185
12186 static union
12187 {
12188   struct ppro_sched_data
12189   {
12190     rtx decode[3];
12191     int issued_this_cycle;
12192   } ppro;
12193 } ix86_sched_data;
12194
12195 static enum attr_ppro_uops
12196 ix86_safe_ppro_uops (rtx insn)
12197 {
12198   if (recog_memoized (insn) >= 0)
12199     return get_attr_ppro_uops (insn);
12200   else
12201     return PPRO_UOPS_MANY;
12202 }
12203
12204 static void
12205 ix86_dump_ppro_packet (FILE *dump)
12206 {
12207   if (ix86_sched_data.ppro.decode[0])
12208     {
12209       fprintf (dump, "PPRO packet: %d",
12210                INSN_UID (ix86_sched_data.ppro.decode[0]));
12211       if (ix86_sched_data.ppro.decode[1])
12212         fprintf (dump, " %d", INSN_UID (ix86_sched_data.ppro.decode[1]));
12213       if (ix86_sched_data.ppro.decode[2])
12214         fprintf (dump, " %d", INSN_UID (ix86_sched_data.ppro.decode[2]));
12215       fputc ('\n', dump);
12216     }
12217 }
12218
12219 /* We're beginning a new block.  Initialize data structures as necessary.  */
12220
12221 static void
12222 ix86_sched_init (FILE *dump ATTRIBUTE_UNUSED,
12223                  int sched_verbose ATTRIBUTE_UNUSED,
12224                  int veclen ATTRIBUTE_UNUSED)
12225 {
12226   memset (&ix86_sched_data, 0, sizeof (ix86_sched_data));
12227 }
12228
12229 /* Shift INSN to SLOT, and shift everything else down.  */
12230
12231 static void
12232 ix86_reorder_insn (rtx *insnp, rtx *slot)
12233 {
12234   if (insnp != slot)
12235     {
12236       rtx insn = *insnp;
12237       do
12238         insnp[0] = insnp[1];
12239       while (++insnp != slot);
12240       *insnp = insn;
12241     }
12242 }
12243
12244 static void
12245 ix86_sched_reorder_ppro (rtx *ready, rtx *e_ready)
12246 {
12247   rtx decode[3];
12248   enum attr_ppro_uops cur_uops;
12249   int issued_this_cycle;
12250   rtx *insnp;
12251   int i;
12252
12253   /* At this point .ppro.decode contains the state of the three
12254      decoders from last "cycle".  That is, those insns that were
12255      actually independent.  But here we're scheduling for the
12256      decoder, and we may find things that are decodable in the
12257      same cycle.  */
12258
12259   memcpy (decode, ix86_sched_data.ppro.decode, sizeof (decode));
12260   issued_this_cycle = 0;
12261
12262   insnp = e_ready;
12263   cur_uops = ix86_safe_ppro_uops (*insnp);
12264
12265   /* If the decoders are empty, and we've a complex insn at the
12266      head of the priority queue, let it issue without complaint.  */
12267   if (decode[0] == NULL)
12268     {
12269       if (cur_uops == PPRO_UOPS_MANY)
12270         {
12271           decode[0] = *insnp;
12272           goto ppro_done;
12273         }
12274
12275       /* Otherwise, search for a 2-4 uop unsn to issue.  */
12276       while (cur_uops != PPRO_UOPS_FEW)
12277         {
12278           if (insnp == ready)
12279             break;
12280           cur_uops = ix86_safe_ppro_uops (*--insnp);
12281         }
12282
12283       /* If so, move it to the head of the line.  */
12284       if (cur_uops == PPRO_UOPS_FEW)
12285         ix86_reorder_insn (insnp, e_ready);
12286
12287       /* Issue the head of the queue.  */
12288       issued_this_cycle = 1;
12289       decode[0] = *e_ready--;
12290     }
12291
12292   /* Look for simple insns to fill in the other two slots.  */
12293   for (i = 1; i < 3; ++i)
12294     if (decode[i] == NULL)
12295       {
12296         if (ready > e_ready)
12297           goto ppro_done;
12298
12299         insnp = e_ready;
12300         cur_uops = ix86_safe_ppro_uops (*insnp);
12301         while (cur_uops != PPRO_UOPS_ONE)
12302           {
12303             if (insnp == ready)
12304               break;
12305             cur_uops = ix86_safe_ppro_uops (*--insnp);
12306           }
12307
12308         /* Found one.  Move it to the head of the queue and issue it.  */
12309         if (cur_uops == PPRO_UOPS_ONE)
12310           {
12311             ix86_reorder_insn (insnp, e_ready);
12312             decode[i] = *e_ready--;
12313             issued_this_cycle++;
12314             continue;
12315           }
12316
12317         /* ??? Didn't find one.  Ideally, here we would do a lazy split
12318            of 2-uop insns, issue one and queue the other.  */
12319       }
12320
12321  ppro_done:
12322   if (issued_this_cycle == 0)
12323     issued_this_cycle = 1;
12324   ix86_sched_data.ppro.issued_this_cycle = issued_this_cycle;
12325 }
12326
12327 /* We are about to being issuing insns for this clock cycle.
12328    Override the default sort algorithm to better slot instructions.  */
12329 static int
12330 ix86_sched_reorder (FILE *dump ATTRIBUTE_UNUSED,
12331                     int sched_verbose ATTRIBUTE_UNUSED, rtx *ready,
12332                     int *n_readyp, int clock_var ATTRIBUTE_UNUSED)
12333 {
12334   int n_ready = *n_readyp;
12335   rtx *e_ready = ready + n_ready - 1;
12336
12337   /* Make sure to go ahead and initialize key items in
12338      ix86_sched_data if we are not going to bother trying to
12339      reorder the ready queue.  */
12340   if (n_ready < 2)
12341     {
12342       ix86_sched_data.ppro.issued_this_cycle = 1;
12343       goto out;
12344     }
12345
12346   switch (ix86_tune)
12347     {
12348     default:
12349       break;
12350
12351     case PROCESSOR_PENTIUMPRO:
12352       ix86_sched_reorder_ppro (ready, e_ready);
12353       break;
12354     }
12355
12356 out:
12357   return ix86_issue_rate ();
12358 }
12359
12360 /* We are about to issue INSN.  Return the number of insns left on the
12361    ready queue that can be issued this cycle.  */
12362
12363 static int
12364 ix86_variable_issue (FILE *dump, int sched_verbose, rtx insn,
12365                      int can_issue_more)
12366 {
12367   int i;
12368   switch (ix86_tune)
12369     {
12370     default:
12371       return can_issue_more - 1;
12372
12373     case PROCESSOR_PENTIUMPRO:
12374       {
12375         enum attr_ppro_uops uops = ix86_safe_ppro_uops (insn);
12376
12377         if (uops == PPRO_UOPS_MANY)
12378           {
12379             if (sched_verbose)
12380               ix86_dump_ppro_packet (dump);
12381             ix86_sched_data.ppro.decode[0] = insn;
12382             ix86_sched_data.ppro.decode[1] = NULL;
12383             ix86_sched_data.ppro.decode[2] = NULL;
12384             if (sched_verbose)
12385               ix86_dump_ppro_packet (dump);
12386             ix86_sched_data.ppro.decode[0] = NULL;
12387           }
12388         else if (uops == PPRO_UOPS_FEW)
12389           {
12390             if (sched_verbose)
12391               ix86_dump_ppro_packet (dump);
12392             ix86_sched_data.ppro.decode[0] = insn;
12393             ix86_sched_data.ppro.decode[1] = NULL;
12394             ix86_sched_data.ppro.decode[2] = NULL;
12395           }
12396         else
12397           {
12398             for (i = 0; i < 3; ++i)
12399               if (ix86_sched_data.ppro.decode[i] == NULL)
12400                 {
12401                   ix86_sched_data.ppro.decode[i] = insn;
12402                   break;
12403                 }
12404             if (i == 3)
12405               abort ();
12406             if (i == 2)
12407               {
12408                 if (sched_verbose)
12409                   ix86_dump_ppro_packet (dump);
12410                 ix86_sched_data.ppro.decode[0] = NULL;
12411                 ix86_sched_data.ppro.decode[1] = NULL;
12412                 ix86_sched_data.ppro.decode[2] = NULL;
12413               }
12414           }
12415       }
12416       return --ix86_sched_data.ppro.issued_this_cycle;
12417     }
12418 }
12419
12420 static int
12421 ia32_use_dfa_pipeline_interface (void)
12422 {
12423   if (TARGET_PENTIUM || TARGET_ATHLON_K8)
12424     return 1;
12425   return 0;
12426 }
12427
12428 /* How many alternative schedules to try.  This should be as wide as the
12429    scheduling freedom in the DFA, but no wider.  Making this value too
12430    large results extra work for the scheduler.  */
12431
12432 static int
12433 ia32_multipass_dfa_lookahead (void)
12434 {
12435   if (ix86_tune == PROCESSOR_PENTIUM)
12436     return 2;
12437   else
12438    return 0;
12439 }
12440
12441 \f
12442 /* Walk through INSNS and look for MEM references whose address is DSTREG or
12443    SRCREG and set the memory attribute to those of DSTREF and SRCREF, as
12444    appropriate.  */
12445
12446 void
12447 ix86_set_move_mem_attrs (rtx insns, rtx dstref, rtx srcref, rtx dstreg,
12448                          rtx srcreg)
12449 {
12450   rtx insn;
12451
12452   for (insn = insns; insn != 0 ; insn = NEXT_INSN (insn))
12453     if (INSN_P (insn))
12454       ix86_set_move_mem_attrs_1 (PATTERN (insn), dstref, srcref,
12455                                  dstreg, srcreg);
12456 }
12457
12458 /* Subroutine of above to actually do the updating by recursively walking
12459    the rtx.  */
12460
12461 static void
12462 ix86_set_move_mem_attrs_1 (rtx x, rtx dstref, rtx srcref, rtx dstreg,
12463                            rtx srcreg)
12464 {
12465   enum rtx_code code = GET_CODE (x);
12466   const char *format_ptr = GET_RTX_FORMAT (code);
12467   int i, j;
12468
12469   if (code == MEM && XEXP (x, 0) == dstreg)
12470     MEM_COPY_ATTRIBUTES (x, dstref);
12471   else if (code == MEM && XEXP (x, 0) == srcreg)
12472     MEM_COPY_ATTRIBUTES (x, srcref);
12473
12474   for (i = 0; i < GET_RTX_LENGTH (code); i++, format_ptr++)
12475     {
12476       if (*format_ptr == 'e')
12477         ix86_set_move_mem_attrs_1 (XEXP (x, i), dstref, srcref,
12478                                    dstreg, srcreg);
12479       else if (*format_ptr == 'E')
12480         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12481           ix86_set_move_mem_attrs_1 (XVECEXP (x, i, j), dstref, srcref,
12482                                      dstreg, srcreg);
12483     }
12484 }
12485 \f
12486 /* Compute the alignment given to a constant that is being placed in memory.
12487    EXP is the constant and ALIGN is the alignment that the object would
12488    ordinarily have.
12489    The value of this function is used instead of that alignment to align
12490    the object.  */
12491
12492 int
12493 ix86_constant_alignment (tree exp, int align)
12494 {
12495   if (TREE_CODE (exp) == REAL_CST)
12496     {
12497       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
12498         return 64;
12499       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
12500         return 128;
12501     }
12502   else if (TREE_CODE (exp) == STRING_CST && TREE_STRING_LENGTH (exp) >= 31
12503            && align < 256)
12504     return 256;
12505
12506   return align;
12507 }
12508
12509 /* Compute the alignment for a static variable.
12510    TYPE is the data type, and ALIGN is the alignment that
12511    the object would ordinarily have.  The value of this function is used
12512    instead of that alignment to align the object.  */
12513
12514 int
12515 ix86_data_alignment (tree type, int align)
12516 {
12517   if (AGGREGATE_TYPE_P (type)
12518        && TYPE_SIZE (type)
12519        && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
12520        && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 256
12521            || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 256)
12522     return 256;
12523
12524   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
12525      to 16byte boundary.  */
12526   if (TARGET_64BIT)
12527     {
12528       if (AGGREGATE_TYPE_P (type)
12529            && TYPE_SIZE (type)
12530            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
12531            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
12532                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
12533         return 128;
12534     }
12535
12536   if (TREE_CODE (type) == ARRAY_TYPE)
12537     {
12538       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
12539         return 64;
12540       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
12541         return 128;
12542     }
12543   else if (TREE_CODE (type) == COMPLEX_TYPE)
12544     {
12545
12546       if (TYPE_MODE (type) == DCmode && align < 64)
12547         return 64;
12548       if (TYPE_MODE (type) == XCmode && align < 128)
12549         return 128;
12550     }
12551   else if ((TREE_CODE (type) == RECORD_TYPE
12552             || TREE_CODE (type) == UNION_TYPE
12553             || TREE_CODE (type) == QUAL_UNION_TYPE)
12554            && TYPE_FIELDS (type))
12555     {
12556       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
12557         return 64;
12558       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
12559         return 128;
12560     }
12561   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
12562            || TREE_CODE (type) == INTEGER_TYPE)
12563     {
12564       if (TYPE_MODE (type) == DFmode && align < 64)
12565         return 64;
12566       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
12567         return 128;
12568     }
12569
12570   return align;
12571 }
12572
12573 /* Compute the alignment for a local variable.
12574    TYPE is the data type, and ALIGN is the alignment that
12575    the object would ordinarily have.  The value of this macro is used
12576    instead of that alignment to align the object.  */
12577
12578 int
12579 ix86_local_alignment (tree type, int align)
12580 {
12581   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
12582      to 16byte boundary.  */
12583   if (TARGET_64BIT)
12584     {
12585       if (AGGREGATE_TYPE_P (type)
12586            && TYPE_SIZE (type)
12587            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
12588            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
12589                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
12590         return 128;
12591     }
12592   if (TREE_CODE (type) == ARRAY_TYPE)
12593     {
12594       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
12595         return 64;
12596       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
12597         return 128;
12598     }
12599   else if (TREE_CODE (type) == COMPLEX_TYPE)
12600     {
12601       if (TYPE_MODE (type) == DCmode && align < 64)
12602         return 64;
12603       if (TYPE_MODE (type) == XCmode && align < 128)
12604         return 128;
12605     }
12606   else if ((TREE_CODE (type) == RECORD_TYPE
12607             || TREE_CODE (type) == UNION_TYPE
12608             || TREE_CODE (type) == QUAL_UNION_TYPE)
12609            && TYPE_FIELDS (type))
12610     {
12611       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
12612         return 64;
12613       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
12614         return 128;
12615     }
12616   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
12617            || TREE_CODE (type) == INTEGER_TYPE)
12618     {
12619
12620       if (TYPE_MODE (type) == DFmode && align < 64)
12621         return 64;
12622       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
12623         return 128;
12624     }
12625   return align;
12626 }
12627 \f
12628 /* Emit RTL insns to initialize the variable parts of a trampoline.
12629    FNADDR is an RTX for the address of the function's pure code.
12630    CXT is an RTX for the static chain value for the function.  */
12631 void
12632 x86_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
12633 {
12634   if (!TARGET_64BIT)
12635     {
12636       /* Compute offset from the end of the jmp to the target function.  */
12637       rtx disp = expand_binop (SImode, sub_optab, fnaddr,
12638                                plus_constant (tramp, 10),
12639                                NULL_RTX, 1, OPTAB_DIRECT);
12640       emit_move_insn (gen_rtx_MEM (QImode, tramp),
12641                       gen_int_mode (0xb9, QImode));
12642       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 1)), cxt);
12643       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 5)),
12644                       gen_int_mode (0xe9, QImode));
12645       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 6)), disp);
12646     }
12647   else
12648     {
12649       int offset = 0;
12650       /* Try to load address using shorter movl instead of movabs.
12651          We may want to support movq for kernel mode, but kernel does not use
12652          trampolines at the moment.  */
12653       if (x86_64_zero_extended_value (fnaddr))
12654         {
12655           fnaddr = copy_to_mode_reg (DImode, fnaddr);
12656           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
12657                           gen_int_mode (0xbb41, HImode));
12658           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, offset + 2)),
12659                           gen_lowpart (SImode, fnaddr));
12660           offset += 6;
12661         }
12662       else
12663         {
12664           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
12665                           gen_int_mode (0xbb49, HImode));
12666           emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
12667                           fnaddr);
12668           offset += 10;
12669         }
12670       /* Load static chain using movabs to r10.  */
12671       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
12672                       gen_int_mode (0xba49, HImode));
12673       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
12674                       cxt);
12675       offset += 10;
12676       /* Jump to the r11 */
12677       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
12678                       gen_int_mode (0xff49, HImode));
12679       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, offset+2)),
12680                       gen_int_mode (0xe3, QImode));
12681       offset += 3;
12682       if (offset > TRAMPOLINE_SIZE)
12683         abort ();
12684     }
12685
12686 #ifdef TRANSFER_FROM_TRAMPOLINE
12687   emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "__enable_execute_stack"),
12688                      LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
12689 #endif
12690 }
12691 \f
12692 #define def_builtin(MASK, NAME, TYPE, CODE)                     \
12693 do {                                                            \
12694   if ((MASK) & target_flags                                     \
12695       && (!((MASK) & MASK_64BIT) || TARGET_64BIT))              \
12696     builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,      \
12697                       NULL, NULL_TREE);                         \
12698 } while (0)
12699
12700 struct builtin_description
12701 {
12702   const unsigned int mask;
12703   const enum insn_code icode;
12704   const char *const name;
12705   const enum ix86_builtins code;
12706   const enum rtx_code comparison;
12707   const unsigned int flag;
12708 };
12709
12710 static const struct builtin_description bdesc_comi[] =
12711 {
12712   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
12713   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
12714   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
12715   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
12716   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
12717   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
12718   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
12719   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
12720   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
12721   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
12722   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
12723   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
12724   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
12725   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
12726   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
12727   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
12728   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
12729   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
12730   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
12731   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
12732   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
12733   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
12734   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
12735   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
12736 };
12737
12738 static const struct builtin_description bdesc_2arg[] =
12739 {
12740   /* SSE */
12741   { MASK_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, 0, 0 },
12742   { MASK_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, 0, 0 },
12743   { MASK_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, 0, 0 },
12744   { MASK_SSE, CODE_FOR_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, 0, 0 },
12745   { MASK_SSE, CODE_FOR_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, 0, 0 },
12746   { MASK_SSE, CODE_FOR_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, 0, 0 },
12747   { MASK_SSE, CODE_FOR_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, 0, 0 },
12748   { MASK_SSE, CODE_FOR_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, 0, 0 },
12749
12750   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, 0 },
12751   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, 0 },
12752   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, 0 },
12753   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT, 1 },
12754   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE, 1 },
12755   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, 0 },
12756   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, EQ, 0 },
12757   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, LT, 0 },
12758   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, LE, 0 },
12759   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, LT, 1 },
12760   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, LE, 1 },
12761   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, UNORDERED, 0 },
12762   { MASK_SSE, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, 0 },
12763   { MASK_SSE, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, 0 },
12764   { MASK_SSE, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, 0 },
12765   { MASK_SSE, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, 0 },
12766   { MASK_SSE, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, EQ, 0 },
12767   { MASK_SSE, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, LT, 0 },
12768   { MASK_SSE, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, LE, 0 },
12769   { MASK_SSE, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, UNORDERED, 0 },
12770
12771   { MASK_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, 0, 0 },
12772   { MASK_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, 0, 0 },
12773   { MASK_SSE, CODE_FOR_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, 0, 0 },
12774   { MASK_SSE, CODE_FOR_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, 0, 0 },
12775
12776   { MASK_SSE, CODE_FOR_sse_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, 0, 0 },
12777   { MASK_SSE, CODE_FOR_sse_nandv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, 0, 0 },
12778   { MASK_SSE, CODE_FOR_sse_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, 0, 0 },
12779   { MASK_SSE, CODE_FOR_sse_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, 0, 0 },
12780
12781   { MASK_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, 0, 0 },
12782   { MASK_SSE, CODE_FOR_sse_movhlps,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, 0, 0 },
12783   { MASK_SSE, CODE_FOR_sse_movlhps,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, 0, 0 },
12784   { MASK_SSE, CODE_FOR_sse_unpckhps, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, 0, 0 },
12785   { MASK_SSE, CODE_FOR_sse_unpcklps, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, 0, 0 },
12786
12787   /* MMX */
12788   { MASK_MMX, CODE_FOR_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, 0, 0 },
12789   { MASK_MMX, CODE_FOR_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, 0, 0 },
12790   { MASK_MMX, CODE_FOR_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, 0, 0 },
12791   { MASK_MMX, CODE_FOR_mmx_adddi3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, 0, 0 },
12792   { MASK_MMX, CODE_FOR_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, 0, 0 },
12793   { MASK_MMX, CODE_FOR_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, 0, 0 },
12794   { MASK_MMX, CODE_FOR_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, 0, 0 },
12795   { MASK_MMX, CODE_FOR_mmx_subdi3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, 0, 0 },
12796
12797   { MASK_MMX, CODE_FOR_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, 0, 0 },
12798   { MASK_MMX, CODE_FOR_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, 0, 0 },
12799   { MASK_MMX, CODE_FOR_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, 0, 0 },
12800   { MASK_MMX, CODE_FOR_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, 0, 0 },
12801   { MASK_MMX, CODE_FOR_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, 0, 0 },
12802   { MASK_MMX, CODE_FOR_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, 0, 0 },
12803   { MASK_MMX, CODE_FOR_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, 0, 0 },
12804   { MASK_MMX, CODE_FOR_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, 0, 0 },
12805
12806   { MASK_MMX, CODE_FOR_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, 0, 0 },
12807   { MASK_MMX, CODE_FOR_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, 0, 0 },
12808   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, 0, 0 },
12809
12810   { MASK_MMX, CODE_FOR_mmx_anddi3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, 0, 0 },
12811   { MASK_MMX, CODE_FOR_mmx_nanddi3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, 0, 0 },
12812   { MASK_MMX, CODE_FOR_mmx_iordi3, "__builtin_ia32_por", IX86_BUILTIN_POR, 0, 0 },
12813   { MASK_MMX, CODE_FOR_mmx_xordi3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, 0, 0 },
12814
12815   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, 0, 0 },
12816   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, 0, 0 },
12817
12818   { MASK_MMX, CODE_FOR_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, 0, 0 },
12819   { MASK_MMX, CODE_FOR_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, 0, 0 },
12820   { MASK_MMX, CODE_FOR_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, 0, 0 },
12821   { MASK_MMX, CODE_FOR_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, 0, 0 },
12822   { MASK_MMX, CODE_FOR_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, 0, 0 },
12823   { MASK_MMX, CODE_FOR_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, 0, 0 },
12824
12825   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, 0, 0 },
12826   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, 0, 0 },
12827   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, 0, 0 },
12828   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, 0, 0 },
12829
12830   { MASK_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, 0, 0 },
12831   { MASK_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, 0, 0 },
12832   { MASK_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, 0, 0 },
12833   { MASK_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, 0, 0 },
12834   { MASK_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, 0, 0 },
12835   { MASK_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, 0, 0 },
12836
12837   /* Special.  */
12838   { MASK_MMX, CODE_FOR_mmx_packsswb, 0, IX86_BUILTIN_PACKSSWB, 0, 0 },
12839   { MASK_MMX, CODE_FOR_mmx_packssdw, 0, IX86_BUILTIN_PACKSSDW, 0, 0 },
12840   { MASK_MMX, CODE_FOR_mmx_packuswb, 0, IX86_BUILTIN_PACKUSWB, 0, 0 },
12841
12842   { MASK_SSE, CODE_FOR_cvtpi2ps, 0, IX86_BUILTIN_CVTPI2PS, 0, 0 },
12843   { MASK_SSE, CODE_FOR_cvtsi2ss, 0, IX86_BUILTIN_CVTSI2SS, 0, 0 },
12844   { MASK_SSE | MASK_64BIT, CODE_FOR_cvtsi2ssq, 0, IX86_BUILTIN_CVTSI642SS, 0, 0 },
12845
12846   { MASK_MMX, CODE_FOR_ashlv4hi3, 0, IX86_BUILTIN_PSLLW, 0, 0 },
12847   { MASK_MMX, CODE_FOR_ashlv4hi3, 0, IX86_BUILTIN_PSLLWI, 0, 0 },
12848   { MASK_MMX, CODE_FOR_ashlv2si3, 0, IX86_BUILTIN_PSLLD, 0, 0 },
12849   { MASK_MMX, CODE_FOR_ashlv2si3, 0, IX86_BUILTIN_PSLLDI, 0, 0 },
12850   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQ, 0, 0 },
12851   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQI, 0, 0 },
12852
12853   { MASK_MMX, CODE_FOR_lshrv4hi3, 0, IX86_BUILTIN_PSRLW, 0, 0 },
12854   { MASK_MMX, CODE_FOR_lshrv4hi3, 0, IX86_BUILTIN_PSRLWI, 0, 0 },
12855   { MASK_MMX, CODE_FOR_lshrv2si3, 0, IX86_BUILTIN_PSRLD, 0, 0 },
12856   { MASK_MMX, CODE_FOR_lshrv2si3, 0, IX86_BUILTIN_PSRLDI, 0, 0 },
12857   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQ, 0, 0 },
12858   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQI, 0, 0 },
12859
12860   { MASK_MMX, CODE_FOR_ashrv4hi3, 0, IX86_BUILTIN_PSRAW, 0, 0 },
12861   { MASK_MMX, CODE_FOR_ashrv4hi3, 0, IX86_BUILTIN_PSRAWI, 0, 0 },
12862   { MASK_MMX, CODE_FOR_ashrv2si3, 0, IX86_BUILTIN_PSRAD, 0, 0 },
12863   { MASK_MMX, CODE_FOR_ashrv2si3, 0, IX86_BUILTIN_PSRADI, 0, 0 },
12864
12865   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_psadbw, 0, IX86_BUILTIN_PSADBW, 0, 0 },
12866   { MASK_MMX, CODE_FOR_mmx_pmaddwd, 0, IX86_BUILTIN_PMADDWD, 0, 0 },
12867
12868   /* SSE2 */
12869   { MASK_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, 0, 0 },
12870   { MASK_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, 0, 0 },
12871   { MASK_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, 0, 0 },
12872   { MASK_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, 0, 0 },
12873   { MASK_SSE2, CODE_FOR_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, 0, 0 },
12874   { MASK_SSE2, CODE_FOR_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, 0, 0 },
12875   { MASK_SSE2, CODE_FOR_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, 0, 0 },
12876   { MASK_SSE2, CODE_FOR_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, 0, 0 },
12877
12878   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, 0 },
12879   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, 0 },
12880   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, 0 },
12881   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT, 1 },
12882   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE, 1 },
12883   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, 0 },
12884   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, EQ, 0 },
12885   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, LT, 0 },
12886   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, LE, 0 },
12887   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, LT, 1 },
12888   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, LE, 1 },
12889   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, UNORDERED, 0 },
12890   { MASK_SSE2, CODE_FOR_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, 0 },
12891   { MASK_SSE2, CODE_FOR_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, 0 },
12892   { MASK_SSE2, CODE_FOR_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, 0 },
12893   { MASK_SSE2, CODE_FOR_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, 0 },
12894   { MASK_SSE2, CODE_FOR_vmmaskncmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, EQ, 0 },
12895   { MASK_SSE2, CODE_FOR_vmmaskncmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, LT, 0 },
12896   { MASK_SSE2, CODE_FOR_vmmaskncmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, LE, 0 },
12897   { MASK_SSE2, CODE_FOR_vmmaskncmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, UNORDERED, 0 },
12898
12899   { MASK_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, 0, 0 },
12900   { MASK_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, 0, 0 },
12901   { MASK_SSE2, CODE_FOR_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, 0, 0 },
12902   { MASK_SSE2, CODE_FOR_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, 0, 0 },
12903
12904   { MASK_SSE2, CODE_FOR_sse2_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, 0, 0 },
12905   { MASK_SSE2, CODE_FOR_sse2_nandv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, 0, 0 },
12906   { MASK_SSE2, CODE_FOR_sse2_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, 0, 0 },
12907   { MASK_SSE2, CODE_FOR_sse2_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, 0, 0 },
12908
12909   { MASK_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, 0, 0 },
12910   { MASK_SSE2, CODE_FOR_sse2_unpckhpd, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, 0, 0 },
12911   { MASK_SSE2, CODE_FOR_sse2_unpcklpd, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, 0, 0 },
12912
12913   /* SSE2 MMX */
12914   { MASK_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, 0, 0 },
12915   { MASK_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, 0, 0 },
12916   { MASK_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, 0, 0 },
12917   { MASK_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, 0, 0 },
12918   { MASK_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, 0, 0 },
12919   { MASK_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, 0, 0 },
12920   { MASK_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, 0, 0 },
12921   { MASK_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, 0, 0 },
12922
12923   { MASK_MMX, CODE_FOR_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, 0, 0 },
12924   { MASK_MMX, CODE_FOR_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, 0, 0 },
12925   { MASK_MMX, CODE_FOR_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, 0, 0 },
12926   { MASK_MMX, CODE_FOR_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, 0, 0 },
12927   { MASK_MMX, CODE_FOR_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, 0, 0 },
12928   { MASK_MMX, CODE_FOR_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, 0, 0 },
12929   { MASK_MMX, CODE_FOR_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, 0, 0 },
12930   { MASK_MMX, CODE_FOR_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, 0, 0 },
12931
12932   { MASK_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, 0, 0 },
12933   { MASK_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, 0, 0 },
12934   { MASK_SSE2, CODE_FOR_sse2_umulsidi3, "__builtin_ia32_pmuludq", IX86_BUILTIN_PMULUDQ, 0, 0 },
12935   { MASK_SSE2, CODE_FOR_sse2_umulv2siv2di3, "__builtin_ia32_pmuludq128", IX86_BUILTIN_PMULUDQ128, 0, 0 },
12936
12937   { MASK_SSE2, CODE_FOR_sse2_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, 0, 0 },
12938   { MASK_SSE2, CODE_FOR_sse2_nandv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, 0, 0 },
12939   { MASK_SSE2, CODE_FOR_sse2_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, 0, 0 },
12940   { MASK_SSE2, CODE_FOR_sse2_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, 0, 0 },
12941
12942   { MASK_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, 0, 0 },
12943   { MASK_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, 0, 0 },
12944
12945   { MASK_SSE2, CODE_FOR_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, 0, 0 },
12946   { MASK_SSE2, CODE_FOR_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, 0, 0 },
12947   { MASK_SSE2, CODE_FOR_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, 0, 0 },
12948   { MASK_SSE2, CODE_FOR_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, 0, 0 },
12949   { MASK_SSE2, CODE_FOR_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, 0, 0 },
12950   { MASK_SSE2, CODE_FOR_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, 0, 0 },
12951
12952   { MASK_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, 0, 0 },
12953   { MASK_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, 0, 0 },
12954   { MASK_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, 0, 0 },
12955   { MASK_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, 0, 0 },
12956
12957   { MASK_SSE2, CODE_FOR_sse2_punpckhbw, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, 0, 0 },
12958   { MASK_SSE2, CODE_FOR_sse2_punpckhwd, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, 0, 0 },
12959   { MASK_SSE2, CODE_FOR_sse2_punpckhdq, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, 0, 0 },
12960   { MASK_SSE2, CODE_FOR_sse2_punpckhqdq, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, 0, 0 },
12961   { MASK_SSE2, CODE_FOR_sse2_punpcklbw, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, 0, 0 },
12962   { MASK_SSE2, CODE_FOR_sse2_punpcklwd, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, 0, 0 },
12963   { MASK_SSE2, CODE_FOR_sse2_punpckldq, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, 0, 0 },
12964   { MASK_SSE2, CODE_FOR_sse2_punpcklqdq, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, 0, 0 },
12965
12966   { MASK_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, 0, 0 },
12967   { MASK_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, 0, 0 },
12968   { MASK_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, 0, 0 },
12969
12970   { MASK_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, 0, 0 },
12971   { MASK_SSE2, CODE_FOR_sse2_psadbw, 0, IX86_BUILTIN_PSADBW128, 0, 0 },
12972
12973   { MASK_SSE2, CODE_FOR_ashlv8hi3_ti, 0, IX86_BUILTIN_PSLLW128, 0, 0 },
12974   { MASK_SSE2, CODE_FOR_ashlv8hi3, 0, IX86_BUILTIN_PSLLWI128, 0, 0 },
12975   { MASK_SSE2, CODE_FOR_ashlv4si3_ti, 0, IX86_BUILTIN_PSLLD128, 0, 0 },
12976   { MASK_SSE2, CODE_FOR_ashlv4si3, 0, IX86_BUILTIN_PSLLDI128, 0, 0 },
12977   { MASK_SSE2, CODE_FOR_ashlv2di3_ti, 0, IX86_BUILTIN_PSLLQ128, 0, 0 },
12978   { MASK_SSE2, CODE_FOR_ashlv2di3, 0, IX86_BUILTIN_PSLLQI128, 0, 0 },
12979
12980   { MASK_SSE2, CODE_FOR_lshrv8hi3_ti, 0, IX86_BUILTIN_PSRLW128, 0, 0 },
12981   { MASK_SSE2, CODE_FOR_lshrv8hi3, 0, IX86_BUILTIN_PSRLWI128, 0, 0 },
12982   { MASK_SSE2, CODE_FOR_lshrv4si3_ti, 0, IX86_BUILTIN_PSRLD128, 0, 0 },
12983   { MASK_SSE2, CODE_FOR_lshrv4si3, 0, IX86_BUILTIN_PSRLDI128, 0, 0 },
12984   { MASK_SSE2, CODE_FOR_lshrv2di3_ti, 0, IX86_BUILTIN_PSRLQ128, 0, 0 },
12985   { MASK_SSE2, CODE_FOR_lshrv2di3, 0, IX86_BUILTIN_PSRLQI128, 0, 0 },
12986
12987   { MASK_SSE2, CODE_FOR_ashrv8hi3_ti, 0, IX86_BUILTIN_PSRAW128, 0, 0 },
12988   { MASK_SSE2, CODE_FOR_ashrv8hi3, 0, IX86_BUILTIN_PSRAWI128, 0, 0 },
12989   { MASK_SSE2, CODE_FOR_ashrv4si3_ti, 0, IX86_BUILTIN_PSRAD128, 0, 0 },
12990   { MASK_SSE2, CODE_FOR_ashrv4si3, 0, IX86_BUILTIN_PSRADI128, 0, 0 },
12991
12992   { MASK_SSE2, CODE_FOR_sse2_pmaddwd, 0, IX86_BUILTIN_PMADDWD128, 0, 0 },
12993
12994   { MASK_SSE2, CODE_FOR_cvtsi2sd, 0, IX86_BUILTIN_CVTSI2SD, 0, 0 },
12995   { MASK_SSE2 | MASK_64BIT, CODE_FOR_cvtsi2sdq, 0, IX86_BUILTIN_CVTSI642SD, 0, 0 },
12996   { MASK_SSE2, CODE_FOR_cvtsd2ss, 0, IX86_BUILTIN_CVTSD2SS, 0, 0 },
12997   { MASK_SSE2, CODE_FOR_cvtss2sd, 0, IX86_BUILTIN_CVTSS2SD, 0, 0 },
12998
12999   /* PNI MMX */
13000   { MASK_PNI, CODE_FOR_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, 0, 0 },
13001   { MASK_PNI, CODE_FOR_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, 0, 0 },
13002   { MASK_PNI, CODE_FOR_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, 0, 0 },
13003   { MASK_PNI, CODE_FOR_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, 0, 0 },
13004   { MASK_PNI, CODE_FOR_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, 0, 0 },
13005   { MASK_PNI, CODE_FOR_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, 0, 0 }
13006 };
13007
13008 static const struct builtin_description bdesc_1arg[] =
13009 {
13010   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB, 0, 0 },
13011   { MASK_SSE, CODE_FOR_sse_movmskps, 0, IX86_BUILTIN_MOVMSKPS, 0, 0 },
13012
13013   { MASK_SSE, CODE_FOR_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS, 0, 0 },
13014   { MASK_SSE, CODE_FOR_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS, 0, 0 },
13015   { MASK_SSE, CODE_FOR_rcpv4sf2, 0, IX86_BUILTIN_RCPPS, 0, 0 },
13016
13017   { MASK_SSE, CODE_FOR_cvtps2pi, 0, IX86_BUILTIN_CVTPS2PI, 0, 0 },
13018   { MASK_SSE, CODE_FOR_cvtss2si, 0, IX86_BUILTIN_CVTSS2SI, 0, 0 },
13019   { MASK_SSE | MASK_64BIT, CODE_FOR_cvtss2siq, 0, IX86_BUILTIN_CVTSS2SI64, 0, 0 },
13020   { MASK_SSE, CODE_FOR_cvttps2pi, 0, IX86_BUILTIN_CVTTPS2PI, 0, 0 },
13021   { MASK_SSE, CODE_FOR_cvttss2si, 0, IX86_BUILTIN_CVTTSS2SI, 0, 0 },
13022   { MASK_SSE | MASK_64BIT, CODE_FOR_cvttss2siq, 0, IX86_BUILTIN_CVTTSS2SI64, 0, 0 },
13023
13024   { MASK_SSE2, CODE_FOR_sse2_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB128, 0, 0 },
13025   { MASK_SSE2, CODE_FOR_sse2_movmskpd, 0, IX86_BUILTIN_MOVMSKPD, 0, 0 },
13026   { MASK_SSE2, CODE_FOR_sse2_movq2dq, 0, IX86_BUILTIN_MOVQ2DQ, 0, 0 },
13027   { MASK_SSE2, CODE_FOR_sse2_movdq2q, 0, IX86_BUILTIN_MOVDQ2Q, 0, 0 },
13028
13029   { MASK_SSE2, CODE_FOR_sqrtv2df2, 0, IX86_BUILTIN_SQRTPD, 0, 0 },
13030
13031   { MASK_SSE2, CODE_FOR_cvtdq2pd, 0, IX86_BUILTIN_CVTDQ2PD, 0, 0 },
13032   { MASK_SSE2, CODE_FOR_cvtdq2ps, 0, IX86_BUILTIN_CVTDQ2PS, 0, 0 },
13033
13034   { MASK_SSE2, CODE_FOR_cvtpd2dq, 0, IX86_BUILTIN_CVTPD2DQ, 0, 0 },
13035   { MASK_SSE2, CODE_FOR_cvtpd2pi, 0, IX86_BUILTIN_CVTPD2PI, 0, 0 },
13036   { MASK_SSE2, CODE_FOR_cvtpd2ps, 0, IX86_BUILTIN_CVTPD2PS, 0, 0 },
13037   { MASK_SSE2, CODE_FOR_cvttpd2dq, 0, IX86_BUILTIN_CVTTPD2DQ, 0, 0 },
13038   { MASK_SSE2, CODE_FOR_cvttpd2pi, 0, IX86_BUILTIN_CVTTPD2PI, 0, 0 },
13039
13040   { MASK_SSE2, CODE_FOR_cvtpi2pd, 0, IX86_BUILTIN_CVTPI2PD, 0, 0 },
13041
13042   { MASK_SSE2, CODE_FOR_cvtsd2si, 0, IX86_BUILTIN_CVTSD2SI, 0, 0 },
13043   { MASK_SSE2, CODE_FOR_cvttsd2si, 0, IX86_BUILTIN_CVTTSD2SI, 0, 0 },
13044   { MASK_SSE2 | MASK_64BIT, CODE_FOR_cvtsd2siq, 0, IX86_BUILTIN_CVTSD2SI64, 0, 0 },
13045   { MASK_SSE2 | MASK_64BIT, CODE_FOR_cvttsd2siq, 0, IX86_BUILTIN_CVTTSD2SI64, 0, 0 },
13046
13047   { MASK_SSE2, CODE_FOR_cvtps2dq, 0, IX86_BUILTIN_CVTPS2DQ, 0, 0 },
13048   { MASK_SSE2, CODE_FOR_cvtps2pd, 0, IX86_BUILTIN_CVTPS2PD, 0, 0 },
13049   { MASK_SSE2, CODE_FOR_cvttps2dq, 0, IX86_BUILTIN_CVTTPS2DQ, 0, 0 },
13050
13051   { MASK_SSE2, CODE_FOR_sse2_movq, 0, IX86_BUILTIN_MOVQ, 0, 0 },
13052
13053   /* PNI */
13054   { MASK_PNI, CODE_FOR_movshdup, 0, IX86_BUILTIN_MOVSHDUP, 0, 0 },
13055   { MASK_PNI, CODE_FOR_movsldup, 0, IX86_BUILTIN_MOVSLDUP, 0, 0 },
13056   { MASK_PNI, CODE_FOR_movddup,  0, IX86_BUILTIN_MOVDDUP, 0, 0 }
13057 };
13058
13059 void
13060 ix86_init_builtins (void)
13061 {
13062   if (TARGET_MMX)
13063     ix86_init_mmx_sse_builtins ();
13064 }
13065
13066 /* Set up all the MMX/SSE builtins.  This is not called if TARGET_MMX
13067    is zero.  Otherwise, if TARGET_SSE is not set, only expand the MMX
13068    builtins.  */
13069 static void
13070 ix86_init_mmx_sse_builtins (void)
13071 {
13072   const struct builtin_description * d;
13073   size_t i;
13074
13075   tree pchar_type_node = build_pointer_type (char_type_node);
13076   tree pcchar_type_node = build_pointer_type (
13077                              build_type_variant (char_type_node, 1, 0));
13078   tree pfloat_type_node = build_pointer_type (float_type_node);
13079   tree pcfloat_type_node = build_pointer_type (
13080                              build_type_variant (float_type_node, 1, 0));
13081   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
13082   tree pv2di_type_node = build_pointer_type (V2DI_type_node);
13083   tree pdi_type_node = build_pointer_type (long_long_unsigned_type_node);
13084
13085   /* Comparisons.  */
13086   tree int_ftype_v4sf_v4sf
13087     = build_function_type_list (integer_type_node,
13088                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
13089   tree v4si_ftype_v4sf_v4sf
13090     = build_function_type_list (V4SI_type_node,
13091                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
13092   /* MMX/SSE/integer conversions.  */
13093   tree int_ftype_v4sf
13094     = build_function_type_list (integer_type_node,
13095                                 V4SF_type_node, NULL_TREE);
13096   tree int64_ftype_v4sf
13097     = build_function_type_list (long_long_integer_type_node,
13098                                 V4SF_type_node, NULL_TREE);
13099   tree int_ftype_v8qi
13100     = build_function_type_list (integer_type_node, V8QI_type_node, NULL_TREE);
13101   tree v4sf_ftype_v4sf_int
13102     = build_function_type_list (V4SF_type_node,
13103                                 V4SF_type_node, integer_type_node, NULL_TREE);
13104   tree v4sf_ftype_v4sf_int64
13105     = build_function_type_list (V4SF_type_node,
13106                                 V4SF_type_node, long_long_integer_type_node,
13107                                 NULL_TREE);
13108   tree v4sf_ftype_v4sf_v2si
13109     = build_function_type_list (V4SF_type_node,
13110                                 V4SF_type_node, V2SI_type_node, NULL_TREE);
13111   tree int_ftype_v4hi_int
13112     = build_function_type_list (integer_type_node,
13113                                 V4HI_type_node, integer_type_node, NULL_TREE);
13114   tree v4hi_ftype_v4hi_int_int
13115     = build_function_type_list (V4HI_type_node, V4HI_type_node,
13116                                 integer_type_node, integer_type_node,
13117                                 NULL_TREE);
13118   /* Miscellaneous.  */
13119   tree v8qi_ftype_v4hi_v4hi
13120     = build_function_type_list (V8QI_type_node,
13121                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
13122   tree v4hi_ftype_v2si_v2si
13123     = build_function_type_list (V4HI_type_node,
13124                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
13125   tree v4sf_ftype_v4sf_v4sf_int
13126     = build_function_type_list (V4SF_type_node,
13127                                 V4SF_type_node, V4SF_type_node,
13128                                 integer_type_node, NULL_TREE);
13129   tree v2si_ftype_v4hi_v4hi
13130     = build_function_type_list (V2SI_type_node,
13131                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
13132   tree v4hi_ftype_v4hi_int
13133     = build_function_type_list (V4HI_type_node,
13134                                 V4HI_type_node, integer_type_node, NULL_TREE);
13135   tree v4hi_ftype_v4hi_di
13136     = build_function_type_list (V4HI_type_node,
13137                                 V4HI_type_node, long_long_unsigned_type_node,
13138                                 NULL_TREE);
13139   tree v2si_ftype_v2si_di
13140     = build_function_type_list (V2SI_type_node,
13141                                 V2SI_type_node, long_long_unsigned_type_node,
13142                                 NULL_TREE);
13143   tree void_ftype_void
13144     = build_function_type (void_type_node, void_list_node);
13145   tree void_ftype_unsigned
13146     = build_function_type_list (void_type_node, unsigned_type_node, NULL_TREE);
13147   tree void_ftype_unsigned_unsigned
13148     = build_function_type_list (void_type_node, unsigned_type_node,
13149                                 unsigned_type_node, NULL_TREE);
13150   tree void_ftype_pcvoid_unsigned_unsigned
13151     = build_function_type_list (void_type_node, const_ptr_type_node,
13152                                 unsigned_type_node, unsigned_type_node,
13153                                 NULL_TREE);
13154   tree unsigned_ftype_void
13155     = build_function_type (unsigned_type_node, void_list_node);
13156   tree di_ftype_void
13157     = build_function_type (long_long_unsigned_type_node, void_list_node);
13158   tree v4sf_ftype_void
13159     = build_function_type (V4SF_type_node, void_list_node);
13160   tree v2si_ftype_v4sf
13161     = build_function_type_list (V2SI_type_node, V4SF_type_node, NULL_TREE);
13162   /* Loads/stores.  */
13163   tree void_ftype_v8qi_v8qi_pchar
13164     = build_function_type_list (void_type_node,
13165                                 V8QI_type_node, V8QI_type_node,
13166                                 pchar_type_node, NULL_TREE);
13167   tree v4sf_ftype_pcfloat
13168     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
13169   /* @@@ the type is bogus */
13170   tree v4sf_ftype_v4sf_pv2si
13171     = build_function_type_list (V4SF_type_node,
13172                                 V4SF_type_node, pv2si_type_node, NULL_TREE);
13173   tree void_ftype_pv2si_v4sf
13174     = build_function_type_list (void_type_node,
13175                                 pv2si_type_node, V4SF_type_node, NULL_TREE);
13176   tree void_ftype_pfloat_v4sf
13177     = build_function_type_list (void_type_node,
13178                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
13179   tree void_ftype_pdi_di
13180     = build_function_type_list (void_type_node,
13181                                 pdi_type_node, long_long_unsigned_type_node,
13182                                 NULL_TREE);
13183   tree void_ftype_pv2di_v2di
13184     = build_function_type_list (void_type_node,
13185                                 pv2di_type_node, V2DI_type_node, NULL_TREE);
13186   /* Normal vector unops.  */
13187   tree v4sf_ftype_v4sf
13188     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
13189
13190   /* Normal vector binops.  */
13191   tree v4sf_ftype_v4sf_v4sf
13192     = build_function_type_list (V4SF_type_node,
13193                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
13194   tree v8qi_ftype_v8qi_v8qi
13195     = build_function_type_list (V8QI_type_node,
13196                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
13197   tree v4hi_ftype_v4hi_v4hi
13198     = build_function_type_list (V4HI_type_node,
13199                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
13200   tree v2si_ftype_v2si_v2si
13201     = build_function_type_list (V2SI_type_node,
13202                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
13203   tree di_ftype_di_di
13204     = build_function_type_list (long_long_unsigned_type_node,
13205                                 long_long_unsigned_type_node,
13206                                 long_long_unsigned_type_node, NULL_TREE);
13207
13208   tree v2si_ftype_v2sf
13209     = build_function_type_list (V2SI_type_node, V2SF_type_node, NULL_TREE);
13210   tree v2sf_ftype_v2si
13211     = build_function_type_list (V2SF_type_node, V2SI_type_node, NULL_TREE);
13212   tree v2si_ftype_v2si
13213     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
13214   tree v2sf_ftype_v2sf
13215     = build_function_type_list (V2SF_type_node, V2SF_type_node, NULL_TREE);
13216   tree v2sf_ftype_v2sf_v2sf
13217     = build_function_type_list (V2SF_type_node,
13218                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
13219   tree v2si_ftype_v2sf_v2sf
13220     = build_function_type_list (V2SI_type_node,
13221                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
13222   tree pint_type_node    = build_pointer_type (integer_type_node);
13223   tree pcint_type_node = build_pointer_type (
13224                              build_type_variant (integer_type_node, 1, 0));
13225   tree pdouble_type_node = build_pointer_type (double_type_node);
13226   tree pcdouble_type_node = build_pointer_type (
13227                                 build_type_variant (double_type_node, 1, 0));
13228   tree int_ftype_v2df_v2df
13229     = build_function_type_list (integer_type_node,
13230                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
13231
13232   tree ti_ftype_void
13233     = build_function_type (intTI_type_node, void_list_node);
13234   tree v2di_ftype_void
13235     = build_function_type (V2DI_type_node, void_list_node);
13236   tree ti_ftype_ti_ti
13237     = build_function_type_list (intTI_type_node,
13238                                 intTI_type_node, intTI_type_node, NULL_TREE);
13239   tree void_ftype_pcvoid
13240     = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE);
13241   tree v2di_ftype_di
13242     = build_function_type_list (V2DI_type_node,
13243                                 long_long_unsigned_type_node, NULL_TREE);
13244   tree di_ftype_v2di
13245     = build_function_type_list (long_long_unsigned_type_node,
13246                                 V2DI_type_node, NULL_TREE);
13247   tree v4sf_ftype_v4si
13248     = build_function_type_list (V4SF_type_node, V4SI_type_node, NULL_TREE);
13249   tree v4si_ftype_v4sf
13250     = build_function_type_list (V4SI_type_node, V4SF_type_node, NULL_TREE);
13251   tree v2df_ftype_v4si
13252     = build_function_type_list (V2DF_type_node, V4SI_type_node, NULL_TREE);
13253   tree v4si_ftype_v2df
13254     = build_function_type_list (V4SI_type_node, V2DF_type_node, NULL_TREE);
13255   tree v2si_ftype_v2df
13256     = build_function_type_list (V2SI_type_node, V2DF_type_node, NULL_TREE);
13257   tree v4sf_ftype_v2df
13258     = build_function_type_list (V4SF_type_node, V2DF_type_node, NULL_TREE);
13259   tree v2df_ftype_v2si
13260     = build_function_type_list (V2DF_type_node, V2SI_type_node, NULL_TREE);
13261   tree v2df_ftype_v4sf
13262     = build_function_type_list (V2DF_type_node, V4SF_type_node, NULL_TREE);
13263   tree int_ftype_v2df
13264     = build_function_type_list (integer_type_node, V2DF_type_node, NULL_TREE);
13265   tree int64_ftype_v2df
13266     = build_function_type_list (long_long_integer_type_node,
13267                                 V2DF_type_node, NULL_TREE);
13268   tree v2df_ftype_v2df_int
13269     = build_function_type_list (V2DF_type_node,
13270                                 V2DF_type_node, integer_type_node, NULL_TREE);
13271   tree v2df_ftype_v2df_int64
13272     = build_function_type_list (V2DF_type_node,
13273                                 V2DF_type_node, long_long_integer_type_node,
13274                                 NULL_TREE);
13275   tree v4sf_ftype_v4sf_v2df
13276     = build_function_type_list (V4SF_type_node,
13277                                 V4SF_type_node, V2DF_type_node, NULL_TREE);
13278   tree v2df_ftype_v2df_v4sf
13279     = build_function_type_list (V2DF_type_node,
13280                                 V2DF_type_node, V4SF_type_node, NULL_TREE);
13281   tree v2df_ftype_v2df_v2df_int
13282     = build_function_type_list (V2DF_type_node,
13283                                 V2DF_type_node, V2DF_type_node,
13284                                 integer_type_node,
13285                                 NULL_TREE);
13286   tree v2df_ftype_v2df_pv2si
13287     = build_function_type_list (V2DF_type_node,
13288                                 V2DF_type_node, pv2si_type_node, NULL_TREE);
13289   tree void_ftype_pv2si_v2df
13290     = build_function_type_list (void_type_node,
13291                                 pv2si_type_node, V2DF_type_node, NULL_TREE);
13292   tree void_ftype_pdouble_v2df
13293     = build_function_type_list (void_type_node,
13294                                 pdouble_type_node, V2DF_type_node, NULL_TREE);
13295   tree void_ftype_pint_int
13296     = build_function_type_list (void_type_node,
13297                                 pint_type_node, integer_type_node, NULL_TREE);
13298   tree void_ftype_v16qi_v16qi_pchar
13299     = build_function_type_list (void_type_node,
13300                                 V16QI_type_node, V16QI_type_node,
13301                                 pchar_type_node, NULL_TREE);
13302   tree v2df_ftype_pcdouble
13303     = build_function_type_list (V2DF_type_node, pcdouble_type_node, NULL_TREE);
13304   tree v2df_ftype_v2df_v2df
13305     = build_function_type_list (V2DF_type_node,
13306                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
13307   tree v16qi_ftype_v16qi_v16qi
13308     = build_function_type_list (V16QI_type_node,
13309                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
13310   tree v8hi_ftype_v8hi_v8hi
13311     = build_function_type_list (V8HI_type_node,
13312                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
13313   tree v4si_ftype_v4si_v4si
13314     = build_function_type_list (V4SI_type_node,
13315                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
13316   tree v2di_ftype_v2di_v2di
13317     = build_function_type_list (V2DI_type_node,
13318                                 V2DI_type_node, V2DI_type_node, NULL_TREE);
13319   tree v2di_ftype_v2df_v2df
13320     = build_function_type_list (V2DI_type_node,
13321                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
13322   tree v2df_ftype_v2df
13323     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
13324   tree v2df_ftype_double
13325     = build_function_type_list (V2DF_type_node, double_type_node, NULL_TREE);
13326   tree v2df_ftype_double_double
13327     = build_function_type_list (V2DF_type_node,
13328                                 double_type_node, double_type_node, NULL_TREE);
13329   tree int_ftype_v8hi_int
13330     = build_function_type_list (integer_type_node,
13331                                 V8HI_type_node, integer_type_node, NULL_TREE);
13332   tree v8hi_ftype_v8hi_int_int
13333     = build_function_type_list (V8HI_type_node,
13334                                 V8HI_type_node, integer_type_node,
13335                                 integer_type_node, NULL_TREE);
13336   tree v2di_ftype_v2di_int
13337     = build_function_type_list (V2DI_type_node,
13338                                 V2DI_type_node, integer_type_node, NULL_TREE);
13339   tree v4si_ftype_v4si_int
13340     = build_function_type_list (V4SI_type_node,
13341                                 V4SI_type_node, integer_type_node, NULL_TREE);
13342   tree v8hi_ftype_v8hi_int
13343     = build_function_type_list (V8HI_type_node,
13344                                 V8HI_type_node, integer_type_node, NULL_TREE);
13345   tree v8hi_ftype_v8hi_v2di
13346     = build_function_type_list (V8HI_type_node,
13347                                 V8HI_type_node, V2DI_type_node, NULL_TREE);
13348   tree v4si_ftype_v4si_v2di
13349     = build_function_type_list (V4SI_type_node,
13350                                 V4SI_type_node, V2DI_type_node, NULL_TREE);
13351   tree v4si_ftype_v8hi_v8hi
13352     = build_function_type_list (V4SI_type_node,
13353                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
13354   tree di_ftype_v8qi_v8qi
13355     = build_function_type_list (long_long_unsigned_type_node,
13356                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
13357   tree v2di_ftype_v16qi_v16qi
13358     = build_function_type_list (V2DI_type_node,
13359                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
13360   tree int_ftype_v16qi
13361     = build_function_type_list (integer_type_node, V16QI_type_node, NULL_TREE);
13362   tree v16qi_ftype_pcchar
13363     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
13364   tree void_ftype_pchar_v16qi
13365     = build_function_type_list (void_type_node,
13366                                 pchar_type_node, V16QI_type_node, NULL_TREE);
13367   tree v4si_ftype_pcint
13368     = build_function_type_list (V4SI_type_node, pcint_type_node, NULL_TREE);
13369   tree void_ftype_pcint_v4si
13370     = build_function_type_list (void_type_node,
13371                                 pcint_type_node, V4SI_type_node, NULL_TREE);
13372   tree v2di_ftype_v2di
13373     = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
13374
13375   tree float80_type;
13376   tree float128_type;
13377
13378   /* The __float80 type.  */
13379   if (TYPE_MODE (long_double_type_node) == XFmode)
13380     (*lang_hooks.types.register_builtin_type) (long_double_type_node,
13381                                                "__float80");
13382   else
13383     {
13384       /* The __float80 type.  */
13385       float80_type = make_node (REAL_TYPE);
13386       TYPE_PRECISION (float80_type) = 96;
13387       layout_type (float80_type);
13388       (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
13389     }
13390
13391   float128_type = make_node (REAL_TYPE);
13392   TYPE_PRECISION (float128_type) = 128;
13393   layout_type (float128_type);
13394   (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
13395
13396   /* Add all builtins that are more or less simple operations on two
13397      operands.  */
13398   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
13399     {
13400       /* Use one of the operands; the target can have a different mode for
13401          mask-generating compares.  */
13402       enum machine_mode mode;
13403       tree type;
13404
13405       if (d->name == 0)
13406         continue;
13407       mode = insn_data[d->icode].operand[1].mode;
13408
13409       switch (mode)
13410         {
13411         case V16QImode:
13412           type = v16qi_ftype_v16qi_v16qi;
13413           break;
13414         case V8HImode:
13415           type = v8hi_ftype_v8hi_v8hi;
13416           break;
13417         case V4SImode:
13418           type = v4si_ftype_v4si_v4si;
13419           break;
13420         case V2DImode:
13421           type = v2di_ftype_v2di_v2di;
13422           break;
13423         case V2DFmode:
13424           type = v2df_ftype_v2df_v2df;
13425           break;
13426         case TImode:
13427           type = ti_ftype_ti_ti;
13428           break;
13429         case V4SFmode:
13430           type = v4sf_ftype_v4sf_v4sf;
13431           break;
13432         case V8QImode:
13433           type = v8qi_ftype_v8qi_v8qi;
13434           break;
13435         case V4HImode:
13436           type = v4hi_ftype_v4hi_v4hi;
13437           break;
13438         case V2SImode:
13439           type = v2si_ftype_v2si_v2si;
13440           break;
13441         case DImode:
13442           type = di_ftype_di_di;
13443           break;
13444
13445         default:
13446           abort ();
13447         }
13448
13449       /* Override for comparisons.  */
13450       if (d->icode == CODE_FOR_maskcmpv4sf3
13451           || d->icode == CODE_FOR_maskncmpv4sf3
13452           || d->icode == CODE_FOR_vmmaskcmpv4sf3
13453           || d->icode == CODE_FOR_vmmaskncmpv4sf3)
13454         type = v4si_ftype_v4sf_v4sf;
13455
13456       if (d->icode == CODE_FOR_maskcmpv2df3
13457           || d->icode == CODE_FOR_maskncmpv2df3
13458           || d->icode == CODE_FOR_vmmaskcmpv2df3
13459           || d->icode == CODE_FOR_vmmaskncmpv2df3)
13460         type = v2di_ftype_v2df_v2df;
13461
13462       def_builtin (d->mask, d->name, type, d->code);
13463     }
13464
13465   /* Add the remaining MMX insns with somewhat more complicated types.  */
13466   def_builtin (MASK_MMX, "__builtin_ia32_mmx_zero", di_ftype_void, IX86_BUILTIN_MMX_ZERO);
13467   def_builtin (MASK_MMX, "__builtin_ia32_emms", void_ftype_void, IX86_BUILTIN_EMMS);
13468   def_builtin (MASK_MMX, "__builtin_ia32_psllw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSLLW);
13469   def_builtin (MASK_MMX, "__builtin_ia32_pslld", v2si_ftype_v2si_di, IX86_BUILTIN_PSLLD);
13470   def_builtin (MASK_MMX, "__builtin_ia32_psllq", di_ftype_di_di, IX86_BUILTIN_PSLLQ);
13471
13472   def_builtin (MASK_MMX, "__builtin_ia32_psrlw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRLW);
13473   def_builtin (MASK_MMX, "__builtin_ia32_psrld", v2si_ftype_v2si_di, IX86_BUILTIN_PSRLD);
13474   def_builtin (MASK_MMX, "__builtin_ia32_psrlq", di_ftype_di_di, IX86_BUILTIN_PSRLQ);
13475
13476   def_builtin (MASK_MMX, "__builtin_ia32_psraw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRAW);
13477   def_builtin (MASK_MMX, "__builtin_ia32_psrad", v2si_ftype_v2si_di, IX86_BUILTIN_PSRAD);
13478
13479   def_builtin (MASK_MMX, "__builtin_ia32_pshufw", v4hi_ftype_v4hi_int, IX86_BUILTIN_PSHUFW);
13480   def_builtin (MASK_MMX, "__builtin_ia32_pmaddwd", v2si_ftype_v4hi_v4hi, IX86_BUILTIN_PMADDWD);
13481
13482   /* comi/ucomi insns.  */
13483   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
13484     if (d->mask == MASK_SSE2)
13485       def_builtin (d->mask, d->name, int_ftype_v2df_v2df, d->code);
13486     else
13487       def_builtin (d->mask, d->name, int_ftype_v4sf_v4sf, d->code);
13488
13489   def_builtin (MASK_MMX, "__builtin_ia32_packsswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKSSWB);
13490   def_builtin (MASK_MMX, "__builtin_ia32_packssdw", v4hi_ftype_v2si_v2si, IX86_BUILTIN_PACKSSDW);
13491   def_builtin (MASK_MMX, "__builtin_ia32_packuswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKUSWB);
13492
13493   def_builtin (MASK_SSE, "__builtin_ia32_ldmxcsr", void_ftype_unsigned, IX86_BUILTIN_LDMXCSR);
13494   def_builtin (MASK_SSE, "__builtin_ia32_stmxcsr", unsigned_ftype_void, IX86_BUILTIN_STMXCSR);
13495   def_builtin (MASK_SSE, "__builtin_ia32_cvtpi2ps", v4sf_ftype_v4sf_v2si, IX86_BUILTIN_CVTPI2PS);
13496   def_builtin (MASK_SSE, "__builtin_ia32_cvtps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTPS2PI);
13497   def_builtin (MASK_SSE, "__builtin_ia32_cvtsi2ss", v4sf_ftype_v4sf_int, IX86_BUILTIN_CVTSI2SS);
13498   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtsi642ss", v4sf_ftype_v4sf_int64, IX86_BUILTIN_CVTSI642SS);
13499   def_builtin (MASK_SSE, "__builtin_ia32_cvtss2si", int_ftype_v4sf, IX86_BUILTIN_CVTSS2SI);
13500   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTSS2SI64);
13501   def_builtin (MASK_SSE, "__builtin_ia32_cvttps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTTPS2PI);
13502   def_builtin (MASK_SSE, "__builtin_ia32_cvttss2si", int_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI);
13503   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvttss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI64);
13504
13505   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pextrw", int_ftype_v4hi_int, IX86_BUILTIN_PEXTRW);
13506   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pinsrw", v4hi_ftype_v4hi_int_int, IX86_BUILTIN_PINSRW);
13507
13508   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_maskmovq", void_ftype_v8qi_v8qi_pchar, IX86_BUILTIN_MASKMOVQ);
13509
13510   def_builtin (MASK_SSE, "__builtin_ia32_loadaps", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADAPS);
13511   def_builtin (MASK_SSE, "__builtin_ia32_loadups", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADUPS);
13512   def_builtin (MASK_SSE, "__builtin_ia32_loadss", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADSS);
13513   def_builtin (MASK_SSE, "__builtin_ia32_storeaps", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREAPS);
13514   def_builtin (MASK_SSE, "__builtin_ia32_storeups", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREUPS);
13515   def_builtin (MASK_SSE, "__builtin_ia32_storess", void_ftype_pfloat_v4sf, IX86_BUILTIN_STORESS);
13516
13517   def_builtin (MASK_SSE, "__builtin_ia32_loadhps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADHPS);
13518   def_builtin (MASK_SSE, "__builtin_ia32_loadlps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADLPS);
13519   def_builtin (MASK_SSE, "__builtin_ia32_storehps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STOREHPS);
13520   def_builtin (MASK_SSE, "__builtin_ia32_storelps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STORELPS);
13521
13522   def_builtin (MASK_SSE, "__builtin_ia32_movmskps", int_ftype_v4sf, IX86_BUILTIN_MOVMSKPS);
13523   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pmovmskb", int_ftype_v8qi, IX86_BUILTIN_PMOVMSKB);
13524   def_builtin (MASK_SSE, "__builtin_ia32_movntps", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTPS);
13525   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_movntq", void_ftype_pdi_di, IX86_BUILTIN_MOVNTQ);
13526
13527   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_sfence", void_ftype_void, IX86_BUILTIN_SFENCE);
13528
13529   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_psadbw", di_ftype_v8qi_v8qi, IX86_BUILTIN_PSADBW);
13530
13531   def_builtin (MASK_SSE, "__builtin_ia32_rcpps", v4sf_ftype_v4sf, IX86_BUILTIN_RCPPS);
13532   def_builtin (MASK_SSE, "__builtin_ia32_rcpss", v4sf_ftype_v4sf, IX86_BUILTIN_RCPSS);
13533   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS);
13534   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTSS);
13535   def_builtin (MASK_SSE, "__builtin_ia32_sqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS);
13536   def_builtin (MASK_SSE, "__builtin_ia32_sqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTSS);
13537
13538   def_builtin (MASK_SSE, "__builtin_ia32_shufps", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_SHUFPS);
13539
13540   /* Original 3DNow!  */
13541   def_builtin (MASK_3DNOW, "__builtin_ia32_femms", void_ftype_void, IX86_BUILTIN_FEMMS);
13542   def_builtin (MASK_3DNOW, "__builtin_ia32_pavgusb", v8qi_ftype_v8qi_v8qi, IX86_BUILTIN_PAVGUSB);
13543   def_builtin (MASK_3DNOW, "__builtin_ia32_pf2id", v2si_ftype_v2sf, IX86_BUILTIN_PF2ID);
13544   def_builtin (MASK_3DNOW, "__builtin_ia32_pfacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFACC);
13545   def_builtin (MASK_3DNOW, "__builtin_ia32_pfadd", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFADD);
13546   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpeq", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPEQ);
13547   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpge", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGE);
13548   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpgt", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGT);
13549   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmax", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMAX);
13550   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmin", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMIN);
13551   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmul", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMUL);
13552   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcp", v2sf_ftype_v2sf, IX86_BUILTIN_PFRCP);
13553   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT1);
13554   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit2", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT2);
13555   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqrt", v2sf_ftype_v2sf, IX86_BUILTIN_PFRSQRT);
13556   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRSQIT1);
13557   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsub", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUB);
13558   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsubr", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUBR);
13559   def_builtin (MASK_3DNOW, "__builtin_ia32_pi2fd", v2sf_ftype_v2si, IX86_BUILTIN_PI2FD);
13560   def_builtin (MASK_3DNOW, "__builtin_ia32_pmulhrw", v4hi_ftype_v4hi_v4hi, IX86_BUILTIN_PMULHRW);
13561
13562   /* 3DNow! extension as used in the Athlon CPU.  */
13563   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pf2iw", v2si_ftype_v2sf, IX86_BUILTIN_PF2IW);
13564   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFNACC);
13565   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfpnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFPNACC);
13566   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pi2fw", v2sf_ftype_v2si, IX86_BUILTIN_PI2FW);
13567   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsf", v2sf_ftype_v2sf, IX86_BUILTIN_PSWAPDSF);
13568   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsi", v2si_ftype_v2si, IX86_BUILTIN_PSWAPDSI);
13569
13570   def_builtin (MASK_SSE, "__builtin_ia32_setzerops", v4sf_ftype_void, IX86_BUILTIN_SSE_ZERO);
13571
13572   /* SSE2 */
13573   def_builtin (MASK_SSE2, "__builtin_ia32_pextrw128", int_ftype_v8hi_int, IX86_BUILTIN_PEXTRW128);
13574   def_builtin (MASK_SSE2, "__builtin_ia32_pinsrw128", v8hi_ftype_v8hi_int_int, IX86_BUILTIN_PINSRW128);
13575
13576   def_builtin (MASK_SSE2, "__builtin_ia32_maskmovdqu", void_ftype_v16qi_v16qi_pchar, IX86_BUILTIN_MASKMOVDQU);
13577   def_builtin (MASK_SSE2, "__builtin_ia32_movq2dq", v2di_ftype_di, IX86_BUILTIN_MOVQ2DQ);
13578   def_builtin (MASK_SSE2, "__builtin_ia32_movdq2q", di_ftype_v2di, IX86_BUILTIN_MOVDQ2Q);
13579
13580   def_builtin (MASK_SSE2, "__builtin_ia32_loadapd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADAPD);
13581   def_builtin (MASK_SSE2, "__builtin_ia32_loadupd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADUPD);
13582   def_builtin (MASK_SSE2, "__builtin_ia32_loadsd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADSD);
13583   def_builtin (MASK_SSE2, "__builtin_ia32_storeapd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREAPD);
13584   def_builtin (MASK_SSE2, "__builtin_ia32_storeupd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREUPD);
13585   def_builtin (MASK_SSE2, "__builtin_ia32_storesd", void_ftype_pdouble_v2df, IX86_BUILTIN_STORESD);
13586
13587   def_builtin (MASK_SSE2, "__builtin_ia32_loadhpd", v2df_ftype_v2df_pv2si, IX86_BUILTIN_LOADHPD);
13588   def_builtin (MASK_SSE2, "__builtin_ia32_loadlpd", v2df_ftype_v2df_pv2si, IX86_BUILTIN_LOADLPD);
13589   def_builtin (MASK_SSE2, "__builtin_ia32_storehpd", void_ftype_pv2si_v2df, IX86_BUILTIN_STOREHPD);
13590   def_builtin (MASK_SSE2, "__builtin_ia32_storelpd", void_ftype_pv2si_v2df, IX86_BUILTIN_STORELPD);
13591
13592   def_builtin (MASK_SSE2, "__builtin_ia32_movmskpd", int_ftype_v2df, IX86_BUILTIN_MOVMSKPD);
13593   def_builtin (MASK_SSE2, "__builtin_ia32_pmovmskb128", int_ftype_v16qi, IX86_BUILTIN_PMOVMSKB128);
13594   def_builtin (MASK_SSE2, "__builtin_ia32_movnti", void_ftype_pint_int, IX86_BUILTIN_MOVNTI);
13595   def_builtin (MASK_SSE2, "__builtin_ia32_movntpd", void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTPD);
13596   def_builtin (MASK_SSE2, "__builtin_ia32_movntdq", void_ftype_pv2di_v2di, IX86_BUILTIN_MOVNTDQ);
13597
13598   def_builtin (MASK_SSE2, "__builtin_ia32_pshufd", v4si_ftype_v4si_int, IX86_BUILTIN_PSHUFD);
13599   def_builtin (MASK_SSE2, "__builtin_ia32_pshuflw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFLW);
13600   def_builtin (MASK_SSE2, "__builtin_ia32_pshufhw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFHW);
13601   def_builtin (MASK_SSE2, "__builtin_ia32_psadbw128", v2di_ftype_v16qi_v16qi, IX86_BUILTIN_PSADBW128);
13602
13603   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtpd", v2df_ftype_v2df, IX86_BUILTIN_SQRTPD);
13604   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtsd", v2df_ftype_v2df, IX86_BUILTIN_SQRTSD);
13605
13606   def_builtin (MASK_SSE2, "__builtin_ia32_shufpd", v2df_ftype_v2df_v2df_int, IX86_BUILTIN_SHUFPD);
13607
13608   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2pd", v2df_ftype_v4si, IX86_BUILTIN_CVTDQ2PD);
13609   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2ps", v4sf_ftype_v4si, IX86_BUILTIN_CVTDQ2PS);
13610
13611   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTPD2DQ);
13612   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTPD2PI);
13613   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2ps", v4sf_ftype_v2df, IX86_BUILTIN_CVTPD2PS);
13614   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTTPD2DQ);
13615   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTTPD2PI);
13616
13617   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpi2pd", v2df_ftype_v2si, IX86_BUILTIN_CVTPI2PD);
13618
13619   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2si", int_ftype_v2df, IX86_BUILTIN_CVTSD2SI);
13620   def_builtin (MASK_SSE2, "__builtin_ia32_cvttsd2si", int_ftype_v2df, IX86_BUILTIN_CVTTSD2SI);
13621   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTSD2SI64);
13622   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvttsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTTSD2SI64);
13623
13624   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTPS2DQ);
13625   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2pd", v2df_ftype_v4sf, IX86_BUILTIN_CVTPS2PD);
13626   def_builtin (MASK_SSE2, "__builtin_ia32_cvttps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTTPS2DQ);
13627
13628   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsi2sd", v2df_ftype_v2df_int, IX86_BUILTIN_CVTSI2SD);
13629   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsi642sd", v2df_ftype_v2df_int64, IX86_BUILTIN_CVTSI642SD);
13630   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2ss", v4sf_ftype_v4sf_v2df, IX86_BUILTIN_CVTSD2SS);
13631   def_builtin (MASK_SSE2, "__builtin_ia32_cvtss2sd", v2df_ftype_v2df_v4sf, IX86_BUILTIN_CVTSS2SD);
13632
13633   def_builtin (MASK_SSE2, "__builtin_ia32_setpd1", v2df_ftype_double, IX86_BUILTIN_SETPD1);
13634   def_builtin (MASK_SSE2, "__builtin_ia32_setpd", v2df_ftype_double_double, IX86_BUILTIN_SETPD);
13635   def_builtin (MASK_SSE2, "__builtin_ia32_setzeropd", ti_ftype_void, IX86_BUILTIN_CLRPD);
13636   def_builtin (MASK_SSE2, "__builtin_ia32_loadpd1", v2df_ftype_pcdouble, IX86_BUILTIN_LOADPD1);
13637   def_builtin (MASK_SSE2, "__builtin_ia32_loadrpd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADRPD);
13638   def_builtin (MASK_SSE2, "__builtin_ia32_storepd1", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREPD1);
13639   def_builtin (MASK_SSE2, "__builtin_ia32_storerpd", void_ftype_pdouble_v2df, IX86_BUILTIN_STORERPD);
13640
13641   def_builtin (MASK_SSE2, "__builtin_ia32_clflush", void_ftype_pcvoid, IX86_BUILTIN_CLFLUSH);
13642   def_builtin (MASK_SSE2, "__builtin_ia32_lfence", void_ftype_void, IX86_BUILTIN_LFENCE);
13643   def_builtin (MASK_SSE2, "__builtin_ia32_mfence", void_ftype_void, IX86_BUILTIN_MFENCE);
13644
13645   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqa", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQA);
13646   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqu", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQU);
13647   def_builtin (MASK_SSE2, "__builtin_ia32_loadd", v4si_ftype_pcint, IX86_BUILTIN_LOADD);
13648   def_builtin (MASK_SSE2, "__builtin_ia32_storedqa", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQA);
13649   def_builtin (MASK_SSE2, "__builtin_ia32_storedqu", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQU);
13650   def_builtin (MASK_SSE2, "__builtin_ia32_stored", void_ftype_pcint_v4si, IX86_BUILTIN_STORED);
13651   def_builtin (MASK_SSE2, "__builtin_ia32_movq", v2di_ftype_v2di, IX86_BUILTIN_MOVQ);
13652
13653   def_builtin (MASK_SSE, "__builtin_ia32_setzero128", v2di_ftype_void, IX86_BUILTIN_CLRTI);
13654
13655   def_builtin (MASK_SSE2, "__builtin_ia32_psllw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSLLW128);
13656   def_builtin (MASK_SSE2, "__builtin_ia32_pslld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSLLD128);
13657   def_builtin (MASK_SSE2, "__builtin_ia32_psllq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSLLQ128);
13658
13659   def_builtin (MASK_SSE2, "__builtin_ia32_psrlw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRLW128);
13660   def_builtin (MASK_SSE2, "__builtin_ia32_psrld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRLD128);
13661   def_builtin (MASK_SSE2, "__builtin_ia32_psrlq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSRLQ128);
13662
13663   def_builtin (MASK_SSE2, "__builtin_ia32_psraw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRAW128);
13664   def_builtin (MASK_SSE2, "__builtin_ia32_psrad128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRAD128);
13665
13666   def_builtin (MASK_SSE2, "__builtin_ia32_pslldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLDQI128);
13667   def_builtin (MASK_SSE2, "__builtin_ia32_psllwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSLLWI128);
13668   def_builtin (MASK_SSE2, "__builtin_ia32_pslldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSLLDI128);
13669   def_builtin (MASK_SSE2, "__builtin_ia32_psllqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLQI128);
13670
13671   def_builtin (MASK_SSE2, "__builtin_ia32_psrldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLDQI128);
13672   def_builtin (MASK_SSE2, "__builtin_ia32_psrlwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRLWI128);
13673   def_builtin (MASK_SSE2, "__builtin_ia32_psrldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRLDI128);
13674   def_builtin (MASK_SSE2, "__builtin_ia32_psrlqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLQI128);
13675
13676   def_builtin (MASK_SSE2, "__builtin_ia32_psrawi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRAWI128);
13677   def_builtin (MASK_SSE2, "__builtin_ia32_psradi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRADI128);
13678
13679   def_builtin (MASK_SSE2, "__builtin_ia32_pmaddwd128", v4si_ftype_v8hi_v8hi, IX86_BUILTIN_PMADDWD128);
13680
13681   /* Prescott New Instructions.  */
13682   def_builtin (MASK_PNI, "__builtin_ia32_monitor",
13683                void_ftype_pcvoid_unsigned_unsigned,
13684                IX86_BUILTIN_MONITOR);
13685   def_builtin (MASK_PNI, "__builtin_ia32_mwait",
13686                void_ftype_unsigned_unsigned,
13687                IX86_BUILTIN_MWAIT);
13688   def_builtin (MASK_PNI, "__builtin_ia32_movshdup",
13689                v4sf_ftype_v4sf,
13690                IX86_BUILTIN_MOVSHDUP);
13691   def_builtin (MASK_PNI, "__builtin_ia32_movsldup",
13692                v4sf_ftype_v4sf,
13693                IX86_BUILTIN_MOVSLDUP);
13694   def_builtin (MASK_PNI, "__builtin_ia32_lddqu",
13695                v16qi_ftype_pcchar, IX86_BUILTIN_LDDQU);
13696   def_builtin (MASK_PNI, "__builtin_ia32_loadddup",
13697                v2df_ftype_pcdouble, IX86_BUILTIN_LOADDDUP);
13698   def_builtin (MASK_PNI, "__builtin_ia32_movddup",
13699                v2df_ftype_v2df, IX86_BUILTIN_MOVDDUP);
13700 }
13701
13702 /* Errors in the source file can cause expand_expr to return const0_rtx
13703    where we expect a vector.  To avoid crashing, use one of the vector
13704    clear instructions.  */
13705 static rtx
13706 safe_vector_operand (rtx x, enum machine_mode mode)
13707 {
13708   if (x != const0_rtx)
13709     return x;
13710   x = gen_reg_rtx (mode);
13711
13712   if (VALID_MMX_REG_MODE (mode) || VALID_MMX_REG_MODE_3DNOW (mode))
13713     emit_insn (gen_mmx_clrdi (mode == DImode ? x
13714                               : gen_rtx_SUBREG (DImode, x, 0)));
13715   else
13716     emit_insn (gen_sse_clrv4sf (mode == V4SFmode ? x
13717                                 : gen_rtx_SUBREG (V4SFmode, x, 0),
13718                                 CONST0_RTX (V4SFmode)));
13719   return x;
13720 }
13721
13722 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
13723
13724 static rtx
13725 ix86_expand_binop_builtin (enum insn_code icode, tree arglist, rtx target)
13726 {
13727   rtx pat;
13728   tree arg0 = TREE_VALUE (arglist);
13729   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13730   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13731   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13732   enum machine_mode tmode = insn_data[icode].operand[0].mode;
13733   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13734   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13735
13736   if (VECTOR_MODE_P (mode0))
13737     op0 = safe_vector_operand (op0, mode0);
13738   if (VECTOR_MODE_P (mode1))
13739     op1 = safe_vector_operand (op1, mode1);
13740
13741   if (! target
13742       || GET_MODE (target) != tmode
13743       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13744     target = gen_reg_rtx (tmode);
13745
13746   if (GET_MODE (op1) == SImode && mode1 == TImode)
13747     {
13748       rtx x = gen_reg_rtx (V4SImode);
13749       emit_insn (gen_sse2_loadd (x, op1));
13750       op1 = gen_lowpart (TImode, x);
13751     }
13752
13753   /* In case the insn wants input operands in modes different from
13754      the result, abort.  */
13755   if ((GET_MODE (op0) != mode0 && GET_MODE (op0) != VOIDmode)
13756       || (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode))
13757     abort ();
13758
13759   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13760     op0 = copy_to_mode_reg (mode0, op0);
13761   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13762     op1 = copy_to_mode_reg (mode1, op1);
13763
13764   /* In the commutative cases, both op0 and op1 are nonimmediate_operand,
13765      yet one of the two must not be a memory.  This is normally enforced
13766      by expanders, but we didn't bother to create one here.  */
13767   if (GET_CODE (op0) == MEM && GET_CODE (op1) == MEM)
13768     op0 = copy_to_mode_reg (mode0, op0);
13769
13770   pat = GEN_FCN (icode) (target, op0, op1);
13771   if (! pat)
13772     return 0;
13773   emit_insn (pat);
13774   return target;
13775 }
13776
13777 /* Subroutine of ix86_expand_builtin to take care of stores.  */
13778
13779 static rtx
13780 ix86_expand_store_builtin (enum insn_code icode, tree arglist)
13781 {
13782   rtx pat;
13783   tree arg0 = TREE_VALUE (arglist);
13784   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13785   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13786   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13787   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
13788   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
13789
13790   if (VECTOR_MODE_P (mode1))
13791     op1 = safe_vector_operand (op1, mode1);
13792
13793   op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
13794   op1 = copy_to_mode_reg (mode1, op1);
13795
13796   pat = GEN_FCN (icode) (op0, op1);
13797   if (pat)
13798     emit_insn (pat);
13799   return 0;
13800 }
13801
13802 /* Subroutine of ix86_expand_builtin to take care of unop insns.  */
13803
13804 static rtx
13805 ix86_expand_unop_builtin (enum insn_code icode, tree arglist,
13806                           rtx target, int do_load)
13807 {
13808   rtx pat;
13809   tree arg0 = TREE_VALUE (arglist);
13810   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13811   enum machine_mode tmode = insn_data[icode].operand[0].mode;
13812   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13813
13814   if (! target
13815       || GET_MODE (target) != tmode
13816       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13817     target = gen_reg_rtx (tmode);
13818   if (do_load)
13819     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
13820   else
13821     {
13822       if (VECTOR_MODE_P (mode0))
13823         op0 = safe_vector_operand (op0, mode0);
13824
13825       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13826         op0 = copy_to_mode_reg (mode0, op0);
13827     }
13828
13829   pat = GEN_FCN (icode) (target, op0);
13830   if (! pat)
13831     return 0;
13832   emit_insn (pat);
13833   return target;
13834 }
13835
13836 /* Subroutine of ix86_expand_builtin to take care of three special unop insns:
13837    sqrtss, rsqrtss, rcpss.  */
13838
13839 static rtx
13840 ix86_expand_unop1_builtin (enum insn_code icode, tree arglist, rtx target)
13841 {
13842   rtx pat;
13843   tree arg0 = TREE_VALUE (arglist);
13844   rtx op1, op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13845   enum machine_mode tmode = insn_data[icode].operand[0].mode;
13846   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13847
13848   if (! target
13849       || GET_MODE (target) != tmode
13850       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13851     target = gen_reg_rtx (tmode);
13852
13853   if (VECTOR_MODE_P (mode0))
13854     op0 = safe_vector_operand (op0, mode0);
13855
13856   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13857     op0 = copy_to_mode_reg (mode0, op0);
13858
13859   op1 = op0;
13860   if (! (*insn_data[icode].operand[2].predicate) (op1, mode0))
13861     op1 = copy_to_mode_reg (mode0, op1);
13862
13863   pat = GEN_FCN (icode) (target, op0, op1);
13864   if (! pat)
13865     return 0;
13866   emit_insn (pat);
13867   return target;
13868 }
13869
13870 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
13871
13872 static rtx
13873 ix86_expand_sse_compare (const struct builtin_description *d, tree arglist,
13874                          rtx target)
13875 {
13876   rtx pat;
13877   tree arg0 = TREE_VALUE (arglist);
13878   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13879   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13880   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13881   rtx op2;
13882   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
13883   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
13884   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
13885   enum rtx_code comparison = d->comparison;
13886
13887   if (VECTOR_MODE_P (mode0))
13888     op0 = safe_vector_operand (op0, mode0);
13889   if (VECTOR_MODE_P (mode1))
13890     op1 = safe_vector_operand (op1, mode1);
13891
13892   /* Swap operands if we have a comparison that isn't available in
13893      hardware.  */
13894   if (d->flag)
13895     {
13896       rtx tmp = gen_reg_rtx (mode1);
13897       emit_move_insn (tmp, op1);
13898       op1 = op0;
13899       op0 = tmp;
13900     }
13901
13902   if (! target
13903       || GET_MODE (target) != tmode
13904       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
13905     target = gen_reg_rtx (tmode);
13906
13907   if (! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
13908     op0 = copy_to_mode_reg (mode0, op0);
13909   if (! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
13910     op1 = copy_to_mode_reg (mode1, op1);
13911
13912   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
13913   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
13914   if (! pat)
13915     return 0;
13916   emit_insn (pat);
13917   return target;
13918 }
13919
13920 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
13921
13922 static rtx
13923 ix86_expand_sse_comi (const struct builtin_description *d, tree arglist,
13924                       rtx target)
13925 {
13926   rtx pat;
13927   tree arg0 = TREE_VALUE (arglist);
13928   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13929   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13930   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13931   rtx op2;
13932   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
13933   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
13934   enum rtx_code comparison = d->comparison;
13935
13936   if (VECTOR_MODE_P (mode0))
13937     op0 = safe_vector_operand (op0, mode0);
13938   if (VECTOR_MODE_P (mode1))
13939     op1 = safe_vector_operand (op1, mode1);
13940
13941   /* Swap operands if we have a comparison that isn't available in
13942      hardware.  */
13943   if (d->flag)
13944     {
13945       rtx tmp = op1;
13946       op1 = op0;
13947       op0 = tmp;
13948     }
13949
13950   target = gen_reg_rtx (SImode);
13951   emit_move_insn (target, const0_rtx);
13952   target = gen_rtx_SUBREG (QImode, target, 0);
13953
13954   if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
13955     op0 = copy_to_mode_reg (mode0, op0);
13956   if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
13957     op1 = copy_to_mode_reg (mode1, op1);
13958
13959   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
13960   pat = GEN_FCN (d->icode) (op0, op1);
13961   if (! pat)
13962     return 0;
13963   emit_insn (pat);
13964   emit_insn (gen_rtx_SET (VOIDmode,
13965                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
13966                           gen_rtx_fmt_ee (comparison, QImode,
13967                                           SET_DEST (pat),
13968                                           const0_rtx)));
13969
13970   return SUBREG_REG (target);
13971 }
13972
13973 /* Expand an expression EXP that calls a built-in function,
13974    with result going to TARGET if that's convenient
13975    (and in mode MODE if that's convenient).
13976    SUBTARGET may be used as the target for computing one of EXP's operands.
13977    IGNORE is nonzero if the value is to be ignored.  */
13978
13979 rtx
13980 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
13981                      enum machine_mode mode ATTRIBUTE_UNUSED,
13982                      int ignore ATTRIBUTE_UNUSED)
13983 {
13984   const struct builtin_description *d;
13985   size_t i;
13986   enum insn_code icode;
13987   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
13988   tree arglist = TREE_OPERAND (exp, 1);
13989   tree arg0, arg1, arg2;
13990   rtx op0, op1, op2, pat;
13991   enum machine_mode tmode, mode0, mode1, mode2;
13992   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
13993
13994   switch (fcode)
13995     {
13996     case IX86_BUILTIN_EMMS:
13997       emit_insn (gen_emms ());
13998       return 0;
13999
14000     case IX86_BUILTIN_SFENCE:
14001       emit_insn (gen_sfence ());
14002       return 0;
14003
14004     case IX86_BUILTIN_PEXTRW:
14005     case IX86_BUILTIN_PEXTRW128:
14006       icode = (fcode == IX86_BUILTIN_PEXTRW
14007                ? CODE_FOR_mmx_pextrw
14008                : CODE_FOR_sse2_pextrw);
14009       arg0 = TREE_VALUE (arglist);
14010       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14011       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14012       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14013       tmode = insn_data[icode].operand[0].mode;
14014       mode0 = insn_data[icode].operand[1].mode;
14015       mode1 = insn_data[icode].operand[2].mode;
14016
14017       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14018         op0 = copy_to_mode_reg (mode0, op0);
14019       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
14020         {
14021           error ("selector must be an integer constant in the range 0..%i",
14022                   fcode == IX86_BUILTIN_PEXTRW ? 3:7);
14023           return gen_reg_rtx (tmode);
14024         }
14025       if (target == 0
14026           || GET_MODE (target) != tmode
14027           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14028         target = gen_reg_rtx (tmode);
14029       pat = GEN_FCN (icode) (target, op0, op1);
14030       if (! pat)
14031         return 0;
14032       emit_insn (pat);
14033       return target;
14034
14035     case IX86_BUILTIN_PINSRW:
14036     case IX86_BUILTIN_PINSRW128:
14037       icode = (fcode == IX86_BUILTIN_PINSRW
14038                ? CODE_FOR_mmx_pinsrw
14039                : CODE_FOR_sse2_pinsrw);
14040       arg0 = TREE_VALUE (arglist);
14041       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14042       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
14043       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14044       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14045       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
14046       tmode = insn_data[icode].operand[0].mode;
14047       mode0 = insn_data[icode].operand[1].mode;
14048       mode1 = insn_data[icode].operand[2].mode;
14049       mode2 = insn_data[icode].operand[3].mode;
14050
14051       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14052         op0 = copy_to_mode_reg (mode0, op0);
14053       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
14054         op1 = copy_to_mode_reg (mode1, op1);
14055       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
14056         {
14057           error ("selector must be an integer constant in the range 0..%i",
14058                   fcode == IX86_BUILTIN_PINSRW ? 15:255);
14059           return const0_rtx;
14060         }
14061       if (target == 0
14062           || GET_MODE (target) != tmode
14063           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14064         target = gen_reg_rtx (tmode);
14065       pat = GEN_FCN (icode) (target, op0, op1, op2);
14066       if (! pat)
14067         return 0;
14068       emit_insn (pat);
14069       return target;
14070
14071     case IX86_BUILTIN_MASKMOVQ:
14072     case IX86_BUILTIN_MASKMOVDQU:
14073       icode = (fcode == IX86_BUILTIN_MASKMOVQ
14074                ? (TARGET_64BIT ? CODE_FOR_mmx_maskmovq_rex : CODE_FOR_mmx_maskmovq)
14075                : (TARGET_64BIT ? CODE_FOR_sse2_maskmovdqu_rex64
14076                   : CODE_FOR_sse2_maskmovdqu));
14077       /* Note the arg order is different from the operand order.  */
14078       arg1 = TREE_VALUE (arglist);
14079       arg2 = TREE_VALUE (TREE_CHAIN (arglist));
14080       arg0 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
14081       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14082       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14083       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
14084       mode0 = insn_data[icode].operand[0].mode;
14085       mode1 = insn_data[icode].operand[1].mode;
14086       mode2 = insn_data[icode].operand[2].mode;
14087
14088       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
14089         op0 = copy_to_mode_reg (mode0, op0);
14090       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
14091         op1 = copy_to_mode_reg (mode1, op1);
14092       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
14093         op2 = copy_to_mode_reg (mode2, op2);
14094       pat = GEN_FCN (icode) (op0, op1, op2);
14095       if (! pat)
14096         return 0;
14097       emit_insn (pat);
14098       return 0;
14099
14100     case IX86_BUILTIN_SQRTSS:
14101       return ix86_expand_unop1_builtin (CODE_FOR_vmsqrtv4sf2, arglist, target);
14102     case IX86_BUILTIN_RSQRTSS:
14103       return ix86_expand_unop1_builtin (CODE_FOR_vmrsqrtv4sf2, arglist, target);
14104     case IX86_BUILTIN_RCPSS:
14105       return ix86_expand_unop1_builtin (CODE_FOR_vmrcpv4sf2, arglist, target);
14106
14107     case IX86_BUILTIN_LOADAPS:
14108       return ix86_expand_unop_builtin (CODE_FOR_sse_movaps, arglist, target, 1);
14109
14110     case IX86_BUILTIN_LOADUPS:
14111       return ix86_expand_unop_builtin (CODE_FOR_sse_movups, arglist, target, 1);
14112
14113     case IX86_BUILTIN_STOREAPS:
14114       return ix86_expand_store_builtin (CODE_FOR_sse_movaps, arglist);
14115
14116     case IX86_BUILTIN_STOREUPS:
14117       return ix86_expand_store_builtin (CODE_FOR_sse_movups, arglist);
14118
14119     case IX86_BUILTIN_LOADSS:
14120       return ix86_expand_unop_builtin (CODE_FOR_sse_loadss, arglist, target, 1);
14121
14122     case IX86_BUILTIN_STORESS:
14123       return ix86_expand_store_builtin (CODE_FOR_sse_storess, arglist);
14124
14125     case IX86_BUILTIN_LOADHPS:
14126     case IX86_BUILTIN_LOADLPS:
14127     case IX86_BUILTIN_LOADHPD:
14128     case IX86_BUILTIN_LOADLPD:
14129       icode = (fcode == IX86_BUILTIN_LOADHPS ? CODE_FOR_sse_movhps
14130                : fcode == IX86_BUILTIN_LOADLPS ? CODE_FOR_sse_movlps
14131                : fcode == IX86_BUILTIN_LOADHPD ? CODE_FOR_sse2_movhpd
14132                : CODE_FOR_sse2_movsd);
14133       arg0 = TREE_VALUE (arglist);
14134       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14135       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14136       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14137       tmode = insn_data[icode].operand[0].mode;
14138       mode0 = insn_data[icode].operand[1].mode;
14139       mode1 = insn_data[icode].operand[2].mode;
14140
14141       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14142         op0 = copy_to_mode_reg (mode0, op0);
14143       op1 = gen_rtx_MEM (mode1, copy_to_mode_reg (Pmode, op1));
14144       if (target == 0
14145           || GET_MODE (target) != tmode
14146           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14147         target = gen_reg_rtx (tmode);
14148       pat = GEN_FCN (icode) (target, op0, op1);
14149       if (! pat)
14150         return 0;
14151       emit_insn (pat);
14152       return target;
14153
14154     case IX86_BUILTIN_STOREHPS:
14155     case IX86_BUILTIN_STORELPS:
14156     case IX86_BUILTIN_STOREHPD:
14157     case IX86_BUILTIN_STORELPD:
14158       icode = (fcode == IX86_BUILTIN_STOREHPS ? CODE_FOR_sse_movhps
14159                : fcode == IX86_BUILTIN_STORELPS ? CODE_FOR_sse_movlps
14160                : fcode == IX86_BUILTIN_STOREHPD ? CODE_FOR_sse2_movhpd
14161                : CODE_FOR_sse2_movsd);
14162       arg0 = TREE_VALUE (arglist);
14163       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14164       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14165       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14166       mode0 = insn_data[icode].operand[1].mode;
14167       mode1 = insn_data[icode].operand[2].mode;
14168
14169       op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
14170       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
14171         op1 = copy_to_mode_reg (mode1, op1);
14172
14173       pat = GEN_FCN (icode) (op0, op0, op1);
14174       if (! pat)
14175         return 0;
14176       emit_insn (pat);
14177       return 0;
14178
14179     case IX86_BUILTIN_MOVNTPS:
14180       return ix86_expand_store_builtin (CODE_FOR_sse_movntv4sf, arglist);
14181     case IX86_BUILTIN_MOVNTQ:
14182       return ix86_expand_store_builtin (CODE_FOR_sse_movntdi, arglist);
14183
14184     case IX86_BUILTIN_LDMXCSR:
14185       op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
14186       target = assign_386_stack_local (SImode, 0);
14187       emit_move_insn (target, op0);
14188       emit_insn (gen_ldmxcsr (target));
14189       return 0;
14190
14191     case IX86_BUILTIN_STMXCSR:
14192       target = assign_386_stack_local (SImode, 0);
14193       emit_insn (gen_stmxcsr (target));
14194       return copy_to_mode_reg (SImode, target);
14195
14196     case IX86_BUILTIN_SHUFPS:
14197     case IX86_BUILTIN_SHUFPD:
14198       icode = (fcode == IX86_BUILTIN_SHUFPS
14199                ? CODE_FOR_sse_shufps
14200                : CODE_FOR_sse2_shufpd);
14201       arg0 = TREE_VALUE (arglist);
14202       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14203       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
14204       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14205       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14206       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
14207       tmode = insn_data[icode].operand[0].mode;
14208       mode0 = insn_data[icode].operand[1].mode;
14209       mode1 = insn_data[icode].operand[2].mode;
14210       mode2 = insn_data[icode].operand[3].mode;
14211
14212       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14213         op0 = copy_to_mode_reg (mode0, op0);
14214       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
14215         op1 = copy_to_mode_reg (mode1, op1);
14216       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
14217         {
14218           /* @@@ better error message */
14219           error ("mask must be an immediate");
14220           return gen_reg_rtx (tmode);
14221         }
14222       if (target == 0
14223           || GET_MODE (target) != tmode
14224           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14225         target = gen_reg_rtx (tmode);
14226       pat = GEN_FCN (icode) (target, op0, op1, op2);
14227       if (! pat)
14228         return 0;
14229       emit_insn (pat);
14230       return target;
14231
14232     case IX86_BUILTIN_PSHUFW:
14233     case IX86_BUILTIN_PSHUFD:
14234     case IX86_BUILTIN_PSHUFHW:
14235     case IX86_BUILTIN_PSHUFLW:
14236       icode = (  fcode == IX86_BUILTIN_PSHUFHW ? CODE_FOR_sse2_pshufhw
14237                : fcode == IX86_BUILTIN_PSHUFLW ? CODE_FOR_sse2_pshuflw
14238                : fcode == IX86_BUILTIN_PSHUFD ? CODE_FOR_sse2_pshufd
14239                : CODE_FOR_mmx_pshufw);
14240       arg0 = TREE_VALUE (arglist);
14241       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14242       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14243       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14244       tmode = insn_data[icode].operand[0].mode;
14245       mode1 = insn_data[icode].operand[1].mode;
14246       mode2 = insn_data[icode].operand[2].mode;
14247
14248       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
14249         op0 = copy_to_mode_reg (mode1, op0);
14250       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
14251         {
14252           /* @@@ better error message */
14253           error ("mask must be an immediate");
14254           return const0_rtx;
14255         }
14256       if (target == 0
14257           || GET_MODE (target) != tmode
14258           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14259         target = gen_reg_rtx (tmode);
14260       pat = GEN_FCN (icode) (target, op0, op1);
14261       if (! pat)
14262         return 0;
14263       emit_insn (pat);
14264       return target;
14265
14266     case IX86_BUILTIN_PSLLDQI128:
14267     case IX86_BUILTIN_PSRLDQI128:
14268       icode = (  fcode == IX86_BUILTIN_PSLLDQI128 ? CODE_FOR_sse2_ashlti3
14269                : CODE_FOR_sse2_lshrti3);
14270       arg0 = TREE_VALUE (arglist);
14271       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14272       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14273       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14274       tmode = insn_data[icode].operand[0].mode;
14275       mode1 = insn_data[icode].operand[1].mode;
14276       mode2 = insn_data[icode].operand[2].mode;
14277
14278       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
14279         {
14280           op0 = copy_to_reg (op0);
14281           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
14282         }
14283       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
14284         {
14285           error ("shift must be an immediate");
14286           return const0_rtx;
14287         }
14288       target = gen_reg_rtx (V2DImode);
14289       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, V2DImode, 0), op0, op1);
14290       if (! pat)
14291         return 0;
14292       emit_insn (pat);
14293       return target;
14294
14295     case IX86_BUILTIN_FEMMS:
14296       emit_insn (gen_femms ());
14297       return NULL_RTX;
14298
14299     case IX86_BUILTIN_PAVGUSB:
14300       return ix86_expand_binop_builtin (CODE_FOR_pavgusb, arglist, target);
14301
14302     case IX86_BUILTIN_PF2ID:
14303       return ix86_expand_unop_builtin (CODE_FOR_pf2id, arglist, target, 0);
14304
14305     case IX86_BUILTIN_PFACC:
14306       return ix86_expand_binop_builtin (CODE_FOR_pfacc, arglist, target);
14307
14308     case IX86_BUILTIN_PFADD:
14309      return ix86_expand_binop_builtin (CODE_FOR_addv2sf3, arglist, target);
14310
14311     case IX86_BUILTIN_PFCMPEQ:
14312       return ix86_expand_binop_builtin (CODE_FOR_eqv2sf3, arglist, target);
14313
14314     case IX86_BUILTIN_PFCMPGE:
14315       return ix86_expand_binop_builtin (CODE_FOR_gev2sf3, arglist, target);
14316
14317     case IX86_BUILTIN_PFCMPGT:
14318       return ix86_expand_binop_builtin (CODE_FOR_gtv2sf3, arglist, target);
14319
14320     case IX86_BUILTIN_PFMAX:
14321       return ix86_expand_binop_builtin (CODE_FOR_pfmaxv2sf3, arglist, target);
14322
14323     case IX86_BUILTIN_PFMIN:
14324       return ix86_expand_binop_builtin (CODE_FOR_pfminv2sf3, arglist, target);
14325
14326     case IX86_BUILTIN_PFMUL:
14327       return ix86_expand_binop_builtin (CODE_FOR_mulv2sf3, arglist, target);
14328
14329     case IX86_BUILTIN_PFRCP:
14330       return ix86_expand_unop_builtin (CODE_FOR_pfrcpv2sf2, arglist, target, 0);
14331
14332     case IX86_BUILTIN_PFRCPIT1:
14333       return ix86_expand_binop_builtin (CODE_FOR_pfrcpit1v2sf3, arglist, target);
14334
14335     case IX86_BUILTIN_PFRCPIT2:
14336       return ix86_expand_binop_builtin (CODE_FOR_pfrcpit2v2sf3, arglist, target);
14337
14338     case IX86_BUILTIN_PFRSQIT1:
14339       return ix86_expand_binop_builtin (CODE_FOR_pfrsqit1v2sf3, arglist, target);
14340
14341     case IX86_BUILTIN_PFRSQRT:
14342       return ix86_expand_unop_builtin (CODE_FOR_pfrsqrtv2sf2, arglist, target, 0);
14343
14344     case IX86_BUILTIN_PFSUB:
14345       return ix86_expand_binop_builtin (CODE_FOR_subv2sf3, arglist, target);
14346
14347     case IX86_BUILTIN_PFSUBR:
14348       return ix86_expand_binop_builtin (CODE_FOR_subrv2sf3, arglist, target);
14349
14350     case IX86_BUILTIN_PI2FD:
14351       return ix86_expand_unop_builtin (CODE_FOR_floatv2si2, arglist, target, 0);
14352
14353     case IX86_BUILTIN_PMULHRW:
14354       return ix86_expand_binop_builtin (CODE_FOR_pmulhrwv4hi3, arglist, target);
14355
14356     case IX86_BUILTIN_PF2IW:
14357       return ix86_expand_unop_builtin (CODE_FOR_pf2iw, arglist, target, 0);
14358
14359     case IX86_BUILTIN_PFNACC:
14360       return ix86_expand_binop_builtin (CODE_FOR_pfnacc, arglist, target);
14361
14362     case IX86_BUILTIN_PFPNACC:
14363       return ix86_expand_binop_builtin (CODE_FOR_pfpnacc, arglist, target);
14364
14365     case IX86_BUILTIN_PI2FW:
14366       return ix86_expand_unop_builtin (CODE_FOR_pi2fw, arglist, target, 0);
14367
14368     case IX86_BUILTIN_PSWAPDSI:
14369       return ix86_expand_unop_builtin (CODE_FOR_pswapdv2si2, arglist, target, 0);
14370
14371     case IX86_BUILTIN_PSWAPDSF:
14372       return ix86_expand_unop_builtin (CODE_FOR_pswapdv2sf2, arglist, target, 0);
14373
14374     case IX86_BUILTIN_SSE_ZERO:
14375       target = gen_reg_rtx (V4SFmode);
14376       emit_insn (gen_sse_clrv4sf (target, CONST0_RTX (V4SFmode)));
14377       return target;
14378
14379     case IX86_BUILTIN_MMX_ZERO:
14380       target = gen_reg_rtx (DImode);
14381       emit_insn (gen_mmx_clrdi (target));
14382       return target;
14383
14384     case IX86_BUILTIN_CLRTI:
14385       target = gen_reg_rtx (V2DImode);
14386       emit_insn (gen_sse2_clrti (simplify_gen_subreg (TImode, target, V2DImode, 0)));
14387       return target;
14388
14389
14390     case IX86_BUILTIN_SQRTSD:
14391       return ix86_expand_unop1_builtin (CODE_FOR_vmsqrtv2df2, arglist, target);
14392     case IX86_BUILTIN_LOADAPD:
14393       return ix86_expand_unop_builtin (CODE_FOR_sse2_movapd, arglist, target, 1);
14394     case IX86_BUILTIN_LOADUPD:
14395       return ix86_expand_unop_builtin (CODE_FOR_sse2_movupd, arglist, target, 1);
14396
14397     case IX86_BUILTIN_STOREAPD:
14398       return ix86_expand_store_builtin (CODE_FOR_sse2_movapd, arglist);
14399     case IX86_BUILTIN_STOREUPD:
14400       return ix86_expand_store_builtin (CODE_FOR_sse2_movupd, arglist);
14401
14402     case IX86_BUILTIN_LOADSD:
14403       return ix86_expand_unop_builtin (CODE_FOR_sse2_loadsd, arglist, target, 1);
14404
14405     case IX86_BUILTIN_STORESD:
14406       return ix86_expand_store_builtin (CODE_FOR_sse2_storesd, arglist);
14407
14408     case IX86_BUILTIN_SETPD1:
14409       target = assign_386_stack_local (DFmode, 0);
14410       arg0 = TREE_VALUE (arglist);
14411       emit_move_insn (adjust_address (target, DFmode, 0),
14412                       expand_expr (arg0, NULL_RTX, VOIDmode, 0));
14413       op0 = gen_reg_rtx (V2DFmode);
14414       emit_insn (gen_sse2_loadsd (op0, adjust_address (target, V2DFmode, 0)));
14415       emit_insn (gen_sse2_shufpd (op0, op0, op0, GEN_INT (0)));
14416       return op0;
14417
14418     case IX86_BUILTIN_SETPD:
14419       target = assign_386_stack_local (V2DFmode, 0);
14420       arg0 = TREE_VALUE (arglist);
14421       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14422       emit_move_insn (adjust_address (target, DFmode, 0),
14423                       expand_expr (arg0, NULL_RTX, VOIDmode, 0));
14424       emit_move_insn (adjust_address (target, DFmode, 8),
14425                       expand_expr (arg1, NULL_RTX, VOIDmode, 0));
14426       op0 = gen_reg_rtx (V2DFmode);
14427       emit_insn (gen_sse2_movapd (op0, target));
14428       return op0;
14429
14430     case IX86_BUILTIN_LOADRPD:
14431       target = ix86_expand_unop_builtin (CODE_FOR_sse2_movapd, arglist,
14432                                          gen_reg_rtx (V2DFmode), 1);
14433       emit_insn (gen_sse2_shufpd (target, target, target, GEN_INT (1)));
14434       return target;
14435
14436     case IX86_BUILTIN_LOADPD1:
14437       target = ix86_expand_unop_builtin (CODE_FOR_sse2_loadsd, arglist,
14438                                          gen_reg_rtx (V2DFmode), 1);
14439       emit_insn (gen_sse2_shufpd (target, target, target, const0_rtx));
14440       return target;
14441
14442     case IX86_BUILTIN_STOREPD1:
14443       return ix86_expand_store_builtin (CODE_FOR_sse2_movapd, arglist);
14444     case IX86_BUILTIN_STORERPD:
14445       return ix86_expand_store_builtin (CODE_FOR_sse2_movapd, arglist);
14446
14447     case IX86_BUILTIN_CLRPD:
14448       target = gen_reg_rtx (V2DFmode);
14449       emit_insn (gen_sse_clrv2df (target));
14450       return target;
14451
14452     case IX86_BUILTIN_MFENCE:
14453         emit_insn (gen_sse2_mfence ());
14454         return 0;
14455     case IX86_BUILTIN_LFENCE:
14456         emit_insn (gen_sse2_lfence ());
14457         return 0;
14458
14459     case IX86_BUILTIN_CLFLUSH:
14460         arg0 = TREE_VALUE (arglist);
14461         op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14462         icode = CODE_FOR_sse2_clflush;
14463         if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
14464             op0 = copy_to_mode_reg (Pmode, op0);
14465
14466         emit_insn (gen_sse2_clflush (op0));
14467         return 0;
14468
14469     case IX86_BUILTIN_MOVNTPD:
14470       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2df, arglist);
14471     case IX86_BUILTIN_MOVNTDQ:
14472       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2di, arglist);
14473     case IX86_BUILTIN_MOVNTI:
14474       return ix86_expand_store_builtin (CODE_FOR_sse2_movntsi, arglist);
14475
14476     case IX86_BUILTIN_LOADDQA:
14477       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqa, arglist, target, 1);
14478     case IX86_BUILTIN_LOADDQU:
14479       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqu, arglist, target, 1);
14480     case IX86_BUILTIN_LOADD:
14481       return ix86_expand_unop_builtin (CODE_FOR_sse2_loadd, arglist, target, 1);
14482
14483     case IX86_BUILTIN_STOREDQA:
14484       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqa, arglist);
14485     case IX86_BUILTIN_STOREDQU:
14486       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqu, arglist);
14487     case IX86_BUILTIN_STORED:
14488       return ix86_expand_store_builtin (CODE_FOR_sse2_stored, arglist);
14489
14490     case IX86_BUILTIN_MONITOR:
14491       arg0 = TREE_VALUE (arglist);
14492       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14493       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
14494       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14495       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14496       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
14497       if (!REG_P (op0))
14498         op0 = copy_to_mode_reg (SImode, op0);
14499       if (!REG_P (op1))
14500         op1 = copy_to_mode_reg (SImode, op1);
14501       if (!REG_P (op2))
14502         op2 = copy_to_mode_reg (SImode, op2);
14503       emit_insn (gen_monitor (op0, op1, op2));
14504       return 0;
14505
14506     case IX86_BUILTIN_MWAIT:
14507       arg0 = TREE_VALUE (arglist);
14508       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14509       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14510       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14511       if (!REG_P (op0))
14512         op0 = copy_to_mode_reg (SImode, op0);
14513       if (!REG_P (op1))
14514         op1 = copy_to_mode_reg (SImode, op1);
14515       emit_insn (gen_mwait (op0, op1));
14516       return 0;
14517
14518     case IX86_BUILTIN_LOADDDUP:
14519       return ix86_expand_unop_builtin (CODE_FOR_loadddup, arglist, target, 1);
14520
14521     case IX86_BUILTIN_LDDQU:
14522       return ix86_expand_unop_builtin (CODE_FOR_lddqu, arglist, target,
14523                                        1);
14524
14525     default:
14526       break;
14527     }
14528
14529   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
14530     if (d->code == fcode)
14531       {
14532         /* Compares are treated specially.  */
14533         if (d->icode == CODE_FOR_maskcmpv4sf3
14534             || d->icode == CODE_FOR_vmmaskcmpv4sf3
14535             || d->icode == CODE_FOR_maskncmpv4sf3
14536             || d->icode == CODE_FOR_vmmaskncmpv4sf3
14537             || d->icode == CODE_FOR_maskcmpv2df3
14538             || d->icode == CODE_FOR_vmmaskcmpv2df3
14539             || d->icode == CODE_FOR_maskncmpv2df3
14540             || d->icode == CODE_FOR_vmmaskncmpv2df3)
14541           return ix86_expand_sse_compare (d, arglist, target);
14542
14543         return ix86_expand_binop_builtin (d->icode, arglist, target);
14544       }
14545
14546   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
14547     if (d->code == fcode)
14548       return ix86_expand_unop_builtin (d->icode, arglist, target, 0);
14549
14550   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
14551     if (d->code == fcode)
14552       return ix86_expand_sse_comi (d, arglist, target);
14553
14554   /* @@@ Should really do something sensible here.  */
14555   return 0;
14556 }
14557
14558 /* Store OPERAND to the memory after reload is completed.  This means
14559    that we can't easily use assign_stack_local.  */
14560 rtx
14561 ix86_force_to_memory (enum machine_mode mode, rtx operand)
14562 {
14563   rtx result;
14564   if (!reload_completed)
14565     abort ();
14566   if (TARGET_RED_ZONE)
14567     {
14568       result = gen_rtx_MEM (mode,
14569                             gen_rtx_PLUS (Pmode,
14570                                           stack_pointer_rtx,
14571                                           GEN_INT (-RED_ZONE_SIZE)));
14572       emit_move_insn (result, operand);
14573     }
14574   else if (!TARGET_RED_ZONE && TARGET_64BIT)
14575     {
14576       switch (mode)
14577         {
14578         case HImode:
14579         case SImode:
14580           operand = gen_lowpart (DImode, operand);
14581           /* FALLTHRU */
14582         case DImode:
14583           emit_insn (
14584                       gen_rtx_SET (VOIDmode,
14585                                    gen_rtx_MEM (DImode,
14586                                                 gen_rtx_PRE_DEC (DImode,
14587                                                         stack_pointer_rtx)),
14588                                    operand));
14589           break;
14590         default:
14591           abort ();
14592         }
14593       result = gen_rtx_MEM (mode, stack_pointer_rtx);
14594     }
14595   else
14596     {
14597       switch (mode)
14598         {
14599         case DImode:
14600           {
14601             rtx operands[2];
14602             split_di (&operand, 1, operands, operands + 1);
14603             emit_insn (
14604                         gen_rtx_SET (VOIDmode,
14605                                      gen_rtx_MEM (SImode,
14606                                                   gen_rtx_PRE_DEC (Pmode,
14607                                                         stack_pointer_rtx)),
14608                                      operands[1]));
14609             emit_insn (
14610                         gen_rtx_SET (VOIDmode,
14611                                      gen_rtx_MEM (SImode,
14612                                                   gen_rtx_PRE_DEC (Pmode,
14613                                                         stack_pointer_rtx)),
14614                                      operands[0]));
14615           }
14616           break;
14617         case HImode:
14618           /* It is better to store HImodes as SImodes.  */
14619           if (!TARGET_PARTIAL_REG_STALL)
14620             operand = gen_lowpart (SImode, operand);
14621           /* FALLTHRU */
14622         case SImode:
14623           emit_insn (
14624                       gen_rtx_SET (VOIDmode,
14625                                    gen_rtx_MEM (GET_MODE (operand),
14626                                                 gen_rtx_PRE_DEC (SImode,
14627                                                         stack_pointer_rtx)),
14628                                    operand));
14629           break;
14630         default:
14631           abort ();
14632         }
14633       result = gen_rtx_MEM (mode, stack_pointer_rtx);
14634     }
14635   return result;
14636 }
14637
14638 /* Free operand from the memory.  */
14639 void
14640 ix86_free_from_memory (enum machine_mode mode)
14641 {
14642   if (!TARGET_RED_ZONE)
14643     {
14644       int size;
14645
14646       if (mode == DImode || TARGET_64BIT)
14647         size = 8;
14648       else if (mode == HImode && TARGET_PARTIAL_REG_STALL)
14649         size = 2;
14650       else
14651         size = 4;
14652       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
14653          to pop or add instruction if registers are available.  */
14654       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
14655                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
14656                                             GEN_INT (size))));
14657     }
14658 }
14659
14660 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
14661    QImode must go into class Q_REGS.
14662    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
14663    movdf to do mem-to-mem moves through integer regs.  */
14664 enum reg_class
14665 ix86_preferred_reload_class (rtx x, enum reg_class class)
14666 {
14667   if (GET_CODE (x) == CONST_VECTOR && x != CONST0_RTX (GET_MODE (x)))
14668     return NO_REGS;
14669   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
14670     {
14671       /* SSE can't load any constant directly yet.  */
14672       if (SSE_CLASS_P (class))
14673         return NO_REGS;
14674       /* Floats can load 0 and 1.  */
14675       if (MAYBE_FLOAT_CLASS_P (class) && standard_80387_constant_p (x))
14676         {
14677           /* Limit class to non-SSE.  Use GENERAL_REGS if possible.  */
14678           if (MAYBE_SSE_CLASS_P (class))
14679             return (reg_class_subset_p (class, GENERAL_REGS)
14680                     ? GENERAL_REGS : FLOAT_REGS);
14681           else
14682             return class;
14683         }
14684       /* General regs can load everything.  */
14685       if (reg_class_subset_p (class, GENERAL_REGS))
14686         return GENERAL_REGS;
14687       /* In case we haven't resolved FLOAT or SSE yet, give up.  */
14688       if (MAYBE_FLOAT_CLASS_P (class) || MAYBE_SSE_CLASS_P (class))
14689         return NO_REGS;
14690     }
14691   if (MAYBE_MMX_CLASS_P (class) && CONSTANT_P (x))
14692     return NO_REGS;
14693   if (GET_MODE (x) == QImode && ! reg_class_subset_p (class, Q_REGS))
14694     return Q_REGS;
14695   return class;
14696 }
14697
14698 /* If we are copying between general and FP registers, we need a memory
14699    location. The same is true for SSE and MMX registers.
14700
14701    The macro can't work reliably when one of the CLASSES is class containing
14702    registers from multiple units (SSE, MMX, integer).  We avoid this by never
14703    combining those units in single alternative in the machine description.
14704    Ensure that this constraint holds to avoid unexpected surprises.
14705
14706    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
14707    enforce these sanity checks.  */
14708 int
14709 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
14710                               enum machine_mode mode, int strict)
14711 {
14712   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
14713       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
14714       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
14715       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
14716       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
14717       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
14718     {
14719       if (strict)
14720         abort ();
14721       else
14722         return 1;
14723     }
14724   return (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2)
14725           || ((SSE_CLASS_P (class1) != SSE_CLASS_P (class2)
14726                || MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
14727               && ((mode != SImode && (mode != DImode || !TARGET_64BIT))
14728                   || (!TARGET_INTER_UNIT_MOVES && !optimize_size))));
14729 }
14730 /* Return the cost of moving data from a register in class CLASS1 to
14731    one in class CLASS2.
14732
14733    It is not required that the cost always equal 2 when FROM is the same as TO;
14734    on some machines it is expensive to move between registers if they are not
14735    general registers.  */
14736 int
14737 ix86_register_move_cost (enum machine_mode mode, enum reg_class class1,
14738                          enum reg_class class2)
14739 {
14740   /* In case we require secondary memory, compute cost of the store followed
14741      by load.  In order to avoid bad register allocation choices, we need
14742      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
14743
14744   if (ix86_secondary_memory_needed (class1, class2, mode, 0))
14745     {
14746       int cost = 1;
14747
14748       cost += MAX (MEMORY_MOVE_COST (mode, class1, 0),
14749                    MEMORY_MOVE_COST (mode, class1, 1));
14750       cost += MAX (MEMORY_MOVE_COST (mode, class2, 0),
14751                    MEMORY_MOVE_COST (mode, class2, 1));
14752
14753       /* In case of copying from general_purpose_register we may emit multiple
14754          stores followed by single load causing memory size mismatch stall.
14755          Count this as arbitrarily high cost of 20.  */
14756       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
14757         cost += 20;
14758
14759       /* In the case of FP/MMX moves, the registers actually overlap, and we
14760          have to switch modes in order to treat them differently.  */
14761       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
14762           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
14763         cost += 20;
14764
14765       return cost;
14766     }
14767
14768   /* Moves between SSE/MMX and integer unit are expensive.  */
14769   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
14770       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
14771     return ix86_cost->mmxsse_to_integer;
14772   if (MAYBE_FLOAT_CLASS_P (class1))
14773     return ix86_cost->fp_move;
14774   if (MAYBE_SSE_CLASS_P (class1))
14775     return ix86_cost->sse_move;
14776   if (MAYBE_MMX_CLASS_P (class1))
14777     return ix86_cost->mmx_move;
14778   return 2;
14779 }
14780
14781 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
14782 int
14783 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
14784 {
14785   /* Flags and only flags can only hold CCmode values.  */
14786   if (CC_REGNO_P (regno))
14787     return GET_MODE_CLASS (mode) == MODE_CC;
14788   if (GET_MODE_CLASS (mode) == MODE_CC
14789       || GET_MODE_CLASS (mode) == MODE_RANDOM
14790       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
14791     return 0;
14792   if (FP_REGNO_P (regno))
14793     return VALID_FP_MODE_P (mode);
14794   if (SSE_REGNO_P (regno))
14795     return (TARGET_SSE ? VALID_SSE_REG_MODE (mode) : 0);
14796   if (MMX_REGNO_P (regno))
14797     return (TARGET_MMX
14798             ? VALID_MMX_REG_MODE (mode) || VALID_MMX_REG_MODE_3DNOW (mode) : 0);
14799   /* We handle both integer and floats in the general purpose registers.
14800      In future we should be able to handle vector modes as well.  */
14801   if (!VALID_INT_MODE_P (mode) && !VALID_FP_MODE_P (mode))
14802     return 0;
14803   /* Take care for QImode values - they can be in non-QI regs, but then
14804      they do cause partial register stalls.  */
14805   if (regno < 4 || mode != QImode || TARGET_64BIT)
14806     return 1;
14807   return reload_in_progress || reload_completed || !TARGET_PARTIAL_REG_STALL;
14808 }
14809
14810 /* Return the cost of moving data of mode M between a
14811    register and memory.  A value of 2 is the default; this cost is
14812    relative to those in `REGISTER_MOVE_COST'.
14813
14814    If moving between registers and memory is more expensive than
14815    between two registers, you should define this macro to express the
14816    relative cost.
14817
14818    Model also increased moving costs of QImode registers in non
14819    Q_REGS classes.
14820  */
14821 int
14822 ix86_memory_move_cost (enum machine_mode mode, enum reg_class class, int in)
14823 {
14824   if (FLOAT_CLASS_P (class))
14825     {
14826       int index;
14827       switch (mode)
14828         {
14829           case SFmode:
14830             index = 0;
14831             break;
14832           case DFmode:
14833             index = 1;
14834             break;
14835           case XFmode:
14836             index = 2;
14837             break;
14838           default:
14839             return 100;
14840         }
14841       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
14842     }
14843   if (SSE_CLASS_P (class))
14844     {
14845       int index;
14846       switch (GET_MODE_SIZE (mode))
14847         {
14848           case 4:
14849             index = 0;
14850             break;
14851           case 8:
14852             index = 1;
14853             break;
14854           case 16:
14855             index = 2;
14856             break;
14857           default:
14858             return 100;
14859         }
14860       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
14861     }
14862   if (MMX_CLASS_P (class))
14863     {
14864       int index;
14865       switch (GET_MODE_SIZE (mode))
14866         {
14867           case 4:
14868             index = 0;
14869             break;
14870           case 8:
14871             index = 1;
14872             break;
14873           default:
14874             return 100;
14875         }
14876       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
14877     }
14878   switch (GET_MODE_SIZE (mode))
14879     {
14880       case 1:
14881         if (in)
14882           return (Q_CLASS_P (class) ? ix86_cost->int_load[0]
14883                   : ix86_cost->movzbl_load);
14884         else
14885           return (Q_CLASS_P (class) ? ix86_cost->int_store[0]
14886                   : ix86_cost->int_store[0] + 4);
14887         break;
14888       case 2:
14889         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
14890       default:
14891         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
14892         if (mode == TFmode)
14893           mode = XFmode;
14894         return ((in ? ix86_cost->int_load[2] : ix86_cost->int_store[2])
14895                 * (((int) GET_MODE_SIZE (mode)
14896                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
14897     }
14898 }
14899
14900 /* Compute a (partial) cost for rtx X.  Return true if the complete
14901    cost has been computed, and false if subexpressions should be
14902    scanned.  In either case, *TOTAL contains the cost result.  */
14903
14904 static bool
14905 ix86_rtx_costs (rtx x, int code, int outer_code, int *total)
14906 {
14907   enum machine_mode mode = GET_MODE (x);
14908
14909   switch (code)
14910     {
14911     case CONST_INT:
14912     case CONST:
14913     case LABEL_REF:
14914     case SYMBOL_REF:
14915       if (TARGET_64BIT && !x86_64_sign_extended_value (x))
14916         *total = 3;
14917       else if (TARGET_64BIT && !x86_64_zero_extended_value (x))
14918         *total = 2;
14919       else if (flag_pic && SYMBOLIC_CONST (x)
14920                && (!TARGET_64BIT
14921                    || (!GET_CODE (x) != LABEL_REF
14922                        && (GET_CODE (x) != SYMBOL_REF
14923                            || !SYMBOL_REF_LOCAL_P (x)))))
14924         *total = 1;
14925       else
14926         *total = 0;
14927       return true;
14928
14929     case CONST_DOUBLE:
14930       if (mode == VOIDmode)
14931         *total = 0;
14932       else
14933         switch (standard_80387_constant_p (x))
14934           {
14935           case 1: /* 0.0 */
14936             *total = 1;
14937             break;
14938           default: /* Other constants */
14939             *total = 2;
14940             break;
14941           case 0:
14942           case -1:
14943             /* Start with (MEM (SYMBOL_REF)), since that's where
14944                it'll probably end up.  Add a penalty for size.  */
14945             *total = (COSTS_N_INSNS (1)
14946                       + (flag_pic != 0 && !TARGET_64BIT)
14947                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
14948             break;
14949           }
14950       return true;
14951
14952     case ZERO_EXTEND:
14953       /* The zero extensions is often completely free on x86_64, so make
14954          it as cheap as possible.  */
14955       if (TARGET_64BIT && mode == DImode
14956           && GET_MODE (XEXP (x, 0)) == SImode)
14957         *total = 1;
14958       else if (TARGET_ZERO_EXTEND_WITH_AND)
14959         *total = COSTS_N_INSNS (ix86_cost->add);
14960       else
14961         *total = COSTS_N_INSNS (ix86_cost->movzx);
14962       return false;
14963
14964     case SIGN_EXTEND:
14965       *total = COSTS_N_INSNS (ix86_cost->movsx);
14966       return false;
14967
14968     case ASHIFT:
14969       if (GET_CODE (XEXP (x, 1)) == CONST_INT
14970           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
14971         {
14972           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
14973           if (value == 1)
14974             {
14975               *total = COSTS_N_INSNS (ix86_cost->add);
14976               return false;
14977             }
14978           if ((value == 2 || value == 3)
14979               && !TARGET_DECOMPOSE_LEA
14980               && ix86_cost->lea <= ix86_cost->shift_const)
14981             {
14982               *total = COSTS_N_INSNS (ix86_cost->lea);
14983               return false;
14984             }
14985         }
14986       /* FALLTHRU */
14987
14988     case ROTATE:
14989     case ASHIFTRT:
14990     case LSHIFTRT:
14991     case ROTATERT:
14992       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
14993         {
14994           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
14995             {
14996               if (INTVAL (XEXP (x, 1)) > 32)
14997                 *total = COSTS_N_INSNS(ix86_cost->shift_const + 2);
14998               else
14999                 *total = COSTS_N_INSNS(ix86_cost->shift_const * 2);
15000             }
15001           else
15002             {
15003               if (GET_CODE (XEXP (x, 1)) == AND)
15004                 *total = COSTS_N_INSNS(ix86_cost->shift_var * 2);
15005               else
15006                 *total = COSTS_N_INSNS(ix86_cost->shift_var * 6 + 2);
15007             }
15008         }
15009       else
15010         {
15011           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
15012             *total = COSTS_N_INSNS (ix86_cost->shift_const);
15013           else
15014             *total = COSTS_N_INSNS (ix86_cost->shift_var);
15015         }
15016       return false;
15017
15018     case MULT:
15019       if (FLOAT_MODE_P (mode))
15020         *total = COSTS_N_INSNS (ix86_cost->fmul);
15021       else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
15022         {
15023           unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
15024           int nbits;
15025
15026           for (nbits = 0; value != 0; value >>= 1)
15027             nbits++;
15028
15029           *total = COSTS_N_INSNS (ix86_cost->mult_init[MODE_INDEX (mode)]
15030                                   + nbits * ix86_cost->mult_bit);
15031         }
15032       else
15033         {
15034           /* This is arbitrary */
15035           *total = COSTS_N_INSNS (ix86_cost->mult_init[MODE_INDEX (mode)]
15036                                   + 7 * ix86_cost->mult_bit);
15037         }
15038       return false;
15039
15040     case DIV:
15041     case UDIV:
15042     case MOD:
15043     case UMOD:
15044       if (FLOAT_MODE_P (mode))
15045         *total = COSTS_N_INSNS (ix86_cost->fdiv);
15046       else
15047         *total = COSTS_N_INSNS (ix86_cost->divide[MODE_INDEX (mode)]);
15048       return false;
15049
15050     case PLUS:
15051       if (FLOAT_MODE_P (mode))
15052         *total = COSTS_N_INSNS (ix86_cost->fadd);
15053       else if (!TARGET_DECOMPOSE_LEA
15054                && GET_MODE_CLASS (mode) == MODE_INT
15055                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
15056         {
15057           if (GET_CODE (XEXP (x, 0)) == PLUS
15058               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
15059               && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
15060               && CONSTANT_P (XEXP (x, 1)))
15061             {
15062               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
15063               if (val == 2 || val == 4 || val == 8)
15064                 {
15065                   *total = COSTS_N_INSNS (ix86_cost->lea);
15066                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
15067                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
15068                                       outer_code);
15069                   *total += rtx_cost (XEXP (x, 1), outer_code);
15070                   return true;
15071                 }
15072             }
15073           else if (GET_CODE (XEXP (x, 0)) == MULT
15074                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
15075             {
15076               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
15077               if (val == 2 || val == 4 || val == 8)
15078                 {
15079                   *total = COSTS_N_INSNS (ix86_cost->lea);
15080                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
15081                   *total += rtx_cost (XEXP (x, 1), outer_code);
15082                   return true;
15083                 }
15084             }
15085           else if (GET_CODE (XEXP (x, 0)) == PLUS)
15086             {
15087               *total = COSTS_N_INSNS (ix86_cost->lea);
15088               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
15089               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
15090               *total += rtx_cost (XEXP (x, 1), outer_code);
15091               return true;
15092             }
15093         }
15094       /* FALLTHRU */
15095
15096     case MINUS:
15097       if (FLOAT_MODE_P (mode))
15098         {
15099           *total = COSTS_N_INSNS (ix86_cost->fadd);
15100           return false;
15101         }
15102       /* FALLTHRU */
15103
15104     case AND:
15105     case IOR:
15106     case XOR:
15107       if (!TARGET_64BIT && mode == DImode)
15108         {
15109           *total = (COSTS_N_INSNS (ix86_cost->add) * 2
15110                     + (rtx_cost (XEXP (x, 0), outer_code)
15111                        << (GET_MODE (XEXP (x, 0)) != DImode))
15112                     + (rtx_cost (XEXP (x, 1), outer_code)
15113                        << (GET_MODE (XEXP (x, 1)) != DImode)));
15114           return true;
15115         }
15116       /* FALLTHRU */
15117
15118     case NEG:
15119       if (FLOAT_MODE_P (mode))
15120         {
15121           *total = COSTS_N_INSNS (ix86_cost->fchs);
15122           return false;
15123         }
15124       /* FALLTHRU */
15125
15126     case NOT:
15127       if (!TARGET_64BIT && mode == DImode)
15128         *total = COSTS_N_INSNS (ix86_cost->add * 2);
15129       else
15130         *total = COSTS_N_INSNS (ix86_cost->add);
15131       return false;
15132
15133     case FLOAT_EXTEND:
15134       if (!TARGET_SSE_MATH || !VALID_SSE_REG_MODE (mode))
15135         *total = 0;
15136       return false;
15137
15138     case ABS:
15139       if (FLOAT_MODE_P (mode))
15140         *total = COSTS_N_INSNS (ix86_cost->fabs);
15141       return false;
15142
15143     case SQRT:
15144       if (FLOAT_MODE_P (mode))
15145         *total = COSTS_N_INSNS (ix86_cost->fsqrt);
15146       return false;
15147
15148     case UNSPEC:
15149       if (XINT (x, 1) == UNSPEC_TP)
15150         *total = 0;
15151       return false;
15152
15153     default:
15154       return false;
15155     }
15156 }
15157
15158 #if defined (DO_GLOBAL_CTORS_BODY) && defined (HAS_INIT_SECTION)
15159 static void
15160 ix86_svr3_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
15161 {
15162   init_section ();
15163   fputs ("\tpushl $", asm_out_file);
15164   assemble_name (asm_out_file, XSTR (symbol, 0));
15165   fputc ('\n', asm_out_file);
15166 }
15167 #endif
15168
15169 #if TARGET_MACHO
15170
15171 static int current_machopic_label_num;
15172
15173 /* Given a symbol name and its associated stub, write out the
15174    definition of the stub.  */
15175
15176 void
15177 machopic_output_stub (FILE *file, const char *symb, const char *stub)
15178 {
15179   unsigned int length;
15180   char *binder_name, *symbol_name, lazy_ptr_name[32];
15181   int label = ++current_machopic_label_num;
15182
15183   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
15184   symb = (*targetm.strip_name_encoding) (symb);
15185
15186   length = strlen (stub);
15187   binder_name = alloca (length + 32);
15188   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
15189
15190   length = strlen (symb);
15191   symbol_name = alloca (length + 32);
15192   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
15193
15194   sprintf (lazy_ptr_name, "L%d$lz", label);
15195
15196   if (MACHOPIC_PURE)
15197     machopic_picsymbol_stub_section ();
15198   else
15199     machopic_symbol_stub_section ();
15200
15201   fprintf (file, "%s:\n", stub);
15202   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
15203
15204   if (MACHOPIC_PURE)
15205     {
15206       fprintf (file, "\tcall LPC$%d\nLPC$%d:\tpopl %%eax\n", label, label);
15207       fprintf (file, "\tmovl %s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
15208       fprintf (file, "\tjmp %%edx\n");
15209     }
15210   else
15211     fprintf (file, "\tjmp *%s\n", lazy_ptr_name);
15212
15213   fprintf (file, "%s:\n", binder_name);
15214
15215   if (MACHOPIC_PURE)
15216     {
15217       fprintf (file, "\tlea %s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
15218       fprintf (file, "\tpushl %%eax\n");
15219     }
15220   else
15221     fprintf (file, "\t pushl $%s\n", lazy_ptr_name);
15222
15223   fprintf (file, "\tjmp dyld_stub_binding_helper\n");
15224
15225   machopic_lazy_symbol_ptr_section ();
15226   fprintf (file, "%s:\n", lazy_ptr_name);
15227   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
15228   fprintf (file, "\t.long %s\n", binder_name);
15229 }
15230 #endif /* TARGET_MACHO */
15231
15232 /* Order the registers for register allocator.  */
15233
15234 void
15235 x86_order_regs_for_local_alloc (void)
15236 {
15237    int pos = 0;
15238    int i;
15239
15240    /* First allocate the local general purpose registers.  */
15241    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
15242      if (GENERAL_REGNO_P (i) && call_used_regs[i])
15243         reg_alloc_order [pos++] = i;
15244
15245    /* Global general purpose registers.  */
15246    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
15247      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
15248         reg_alloc_order [pos++] = i;
15249
15250    /* x87 registers come first in case we are doing FP math
15251       using them.  */
15252    if (!TARGET_SSE_MATH)
15253      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
15254        reg_alloc_order [pos++] = i;
15255
15256    /* SSE registers.  */
15257    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
15258      reg_alloc_order [pos++] = i;
15259    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
15260      reg_alloc_order [pos++] = i;
15261
15262    /* x87 registers.  */
15263    if (TARGET_SSE_MATH)
15264      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
15265        reg_alloc_order [pos++] = i;
15266
15267    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
15268      reg_alloc_order [pos++] = i;
15269
15270    /* Initialize the rest of array as we do not allocate some registers
15271       at all.  */
15272    while (pos < FIRST_PSEUDO_REGISTER)
15273      reg_alloc_order [pos++] = 0;
15274 }
15275
15276 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
15277 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
15278 #endif
15279
15280 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
15281    struct attribute_spec.handler.  */
15282 static tree
15283 ix86_handle_struct_attribute (tree *node, tree name,
15284                               tree args ATTRIBUTE_UNUSED,
15285                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
15286 {
15287   tree *type = NULL;
15288   if (DECL_P (*node))
15289     {
15290       if (TREE_CODE (*node) == TYPE_DECL)
15291         type = &TREE_TYPE (*node);
15292     }
15293   else
15294     type = node;
15295
15296   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
15297                  || TREE_CODE (*type) == UNION_TYPE)))
15298     {
15299       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
15300       *no_add_attrs = true;
15301     }
15302
15303   else if ((is_attribute_p ("ms_struct", name)
15304             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
15305            || ((is_attribute_p ("gcc_struct", name)
15306                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
15307     {
15308       warning ("`%s' incompatible attribute ignored",
15309                IDENTIFIER_POINTER (name));
15310       *no_add_attrs = true;
15311     }
15312
15313   return NULL_TREE;
15314 }
15315
15316 static bool
15317 ix86_ms_bitfield_layout_p (tree record_type)
15318 {
15319   return (TARGET_USE_MS_BITFIELD_LAYOUT &&
15320           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
15321     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
15322 }
15323
15324 /* Returns an expression indicating where the this parameter is
15325    located on entry to the FUNCTION.  */
15326
15327 static rtx
15328 x86_this_parameter (tree function)
15329 {
15330   tree type = TREE_TYPE (function);
15331
15332   if (TARGET_64BIT)
15333     {
15334       int n = aggregate_value_p (TREE_TYPE (type), type) != 0;
15335       return gen_rtx_REG (DImode, x86_64_int_parameter_registers[n]);
15336     }
15337
15338   if (ix86_function_regparm (type, function) > 0)
15339     {
15340       tree parm;
15341
15342       parm = TYPE_ARG_TYPES (type);
15343       /* Figure out whether or not the function has a variable number of
15344          arguments.  */
15345       for (; parm; parm = TREE_CHAIN (parm))
15346         if (TREE_VALUE (parm) == void_type_node)
15347           break;
15348       /* If not, the this parameter is in the first argument.  */
15349       if (parm)
15350         {
15351           int regno = 0;
15352           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
15353             regno = 2;
15354           return gen_rtx_REG (SImode, regno);
15355         }
15356     }
15357
15358   if (aggregate_value_p (TREE_TYPE (type), type))
15359     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 8));
15360   else
15361     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 4));
15362 }
15363
15364 /* Determine whether x86_output_mi_thunk can succeed.  */
15365
15366 static bool
15367 x86_can_output_mi_thunk (tree thunk ATTRIBUTE_UNUSED,
15368                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
15369                          HOST_WIDE_INT vcall_offset, tree function)
15370 {
15371   /* 64-bit can handle anything.  */
15372   if (TARGET_64BIT)
15373     return true;
15374
15375   /* For 32-bit, everything's fine if we have one free register.  */
15376   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
15377     return true;
15378
15379   /* Need a free register for vcall_offset.  */
15380   if (vcall_offset)
15381     return false;
15382
15383   /* Need a free register for GOT references.  */
15384   if (flag_pic && !(*targetm.binds_local_p) (function))
15385     return false;
15386
15387   /* Otherwise ok.  */
15388   return true;
15389 }
15390
15391 /* Output the assembler code for a thunk function.  THUNK_DECL is the
15392    declaration for the thunk function itself, FUNCTION is the decl for
15393    the target function.  DELTA is an immediate constant offset to be
15394    added to THIS.  If VCALL_OFFSET is nonzero, the word at
15395    *(*this + vcall_offset) should be added to THIS.  */
15396
15397 static void
15398 x86_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
15399                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
15400                      HOST_WIDE_INT vcall_offset, tree function)
15401 {
15402   rtx xops[3];
15403   rtx this = x86_this_parameter (function);
15404   rtx this_reg, tmp;
15405
15406   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
15407      pull it in now and let DELTA benefit.  */
15408   if (REG_P (this))
15409     this_reg = this;
15410   else if (vcall_offset)
15411     {
15412       /* Put the this parameter into %eax.  */
15413       xops[0] = this;
15414       xops[1] = this_reg = gen_rtx_REG (Pmode, 0);
15415       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
15416     }
15417   else
15418     this_reg = NULL_RTX;
15419
15420   /* Adjust the this parameter by a fixed constant.  */
15421   if (delta)
15422     {
15423       xops[0] = GEN_INT (delta);
15424       xops[1] = this_reg ? this_reg : this;
15425       if (TARGET_64BIT)
15426         {
15427           if (!x86_64_general_operand (xops[0], DImode))
15428             {
15429               tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
15430               xops[1] = tmp;
15431               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
15432               xops[0] = tmp;
15433               xops[1] = this;
15434             }
15435           output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
15436         }
15437       else
15438         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
15439     }
15440
15441   /* Adjust the this parameter by a value stored in the vtable.  */
15442   if (vcall_offset)
15443     {
15444       if (TARGET_64BIT)
15445         tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
15446       else
15447         {
15448           int tmp_regno = 2 /* ECX */;
15449           if (lookup_attribute ("fastcall",
15450               TYPE_ATTRIBUTES (TREE_TYPE (function))))
15451             tmp_regno = 0 /* EAX */;
15452           tmp = gen_rtx_REG (SImode, tmp_regno);
15453         }
15454
15455       xops[0] = gen_rtx_MEM (Pmode, this_reg);
15456       xops[1] = tmp;
15457       if (TARGET_64BIT)
15458         output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
15459       else
15460         output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
15461
15462       /* Adjust the this parameter.  */
15463       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
15464       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
15465         {
15466           rtx tmp2 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
15467           xops[0] = GEN_INT (vcall_offset);
15468           xops[1] = tmp2;
15469           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
15470           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
15471         }
15472       xops[1] = this_reg;
15473       if (TARGET_64BIT)
15474         output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
15475       else
15476         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
15477     }
15478
15479   /* If necessary, drop THIS back to its stack slot.  */
15480   if (this_reg && this_reg != this)
15481     {
15482       xops[0] = this_reg;
15483       xops[1] = this;
15484       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
15485     }
15486
15487   xops[0] = XEXP (DECL_RTL (function), 0);
15488   if (TARGET_64BIT)
15489     {
15490       if (!flag_pic || (*targetm.binds_local_p) (function))
15491         output_asm_insn ("jmp\t%P0", xops);
15492       else
15493         {
15494           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
15495           tmp = gen_rtx_CONST (Pmode, tmp);
15496           tmp = gen_rtx_MEM (QImode, tmp);
15497           xops[0] = tmp;
15498           output_asm_insn ("jmp\t%A0", xops);
15499         }
15500     }
15501   else
15502     {
15503       if (!flag_pic || (*targetm.binds_local_p) (function))
15504         output_asm_insn ("jmp\t%P0", xops);
15505       else
15506 #if TARGET_MACHO
15507         if (TARGET_MACHO)
15508           {
15509             const char *ip = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
15510             tmp = gen_rtx_SYMBOL_REF (Pmode, machopic_stub_name (ip));
15511             tmp = gen_rtx_MEM (QImode, tmp);
15512             xops[0] = tmp;
15513             output_asm_insn ("jmp\t%0", xops);
15514           }
15515         else
15516 #endif /* TARGET_MACHO */
15517         {
15518           tmp = gen_rtx_REG (SImode, 2 /* ECX */);
15519           output_set_got (tmp);
15520
15521           xops[1] = tmp;
15522           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
15523           output_asm_insn ("jmp\t{*}%1", xops);
15524         }
15525     }
15526 }
15527
15528 static void
15529 x86_file_start (void)
15530 {
15531   default_file_start ();
15532   if (X86_FILE_START_VERSION_DIRECTIVE)
15533     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
15534   if (X86_FILE_START_FLTUSED)
15535     fputs ("\t.global\t__fltused\n", asm_out_file);
15536   if (ix86_asm_dialect == ASM_INTEL)
15537     fputs ("\t.intel_syntax\n", asm_out_file);
15538 }
15539
15540 int
15541 x86_field_alignment (tree field, int computed)
15542 {
15543   enum machine_mode mode;
15544   tree type = TREE_TYPE (field);
15545
15546   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
15547     return computed;
15548   mode = TYPE_MODE (TREE_CODE (type) == ARRAY_TYPE
15549                     ? get_inner_array_type (type) : type);
15550   if (mode == DFmode || mode == DCmode
15551       || GET_MODE_CLASS (mode) == MODE_INT
15552       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
15553     return MIN (32, computed);
15554   return computed;
15555 }
15556
15557 /* Output assembler code to FILE to increment profiler label # LABELNO
15558    for profiling a function entry.  */
15559 void
15560 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
15561 {
15562   if (TARGET_64BIT)
15563     if (flag_pic)
15564       {
15565 #ifndef NO_PROFILE_COUNTERS
15566         fprintf (file, "\tleaq\t%sP%d@(%%rip),%%r11\n", LPREFIX, labelno);
15567 #endif
15568         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", MCOUNT_NAME);
15569       }
15570     else
15571       {
15572 #ifndef NO_PROFILE_COUNTERS
15573         fprintf (file, "\tmovq\t$%sP%d,%%r11\n", LPREFIX, labelno);
15574 #endif
15575         fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
15576       }
15577   else if (flag_pic)
15578     {
15579 #ifndef NO_PROFILE_COUNTERS
15580       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%%s\n",
15581                LPREFIX, labelno, PROFILE_COUNT_REGISTER);
15582 #endif
15583       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", MCOUNT_NAME);
15584     }
15585   else
15586     {
15587 #ifndef NO_PROFILE_COUNTERS
15588       fprintf (file, "\tmovl\t$%sP%d,%%%s\n", LPREFIX, labelno,
15589                PROFILE_COUNT_REGISTER);
15590 #endif
15591       fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
15592     }
15593 }
15594
15595 /* We don't have exact information about the insn sizes, but we may assume
15596    quite safely that we are informed about all 1 byte insns and memory
15597    address sizes.  This is enough to eliminate unnecessary padding in
15598    99% of cases.  */
15599
15600 static int
15601 min_insn_size (rtx insn)
15602 {
15603   int l = 0;
15604
15605   if (!INSN_P (insn) || !active_insn_p (insn))
15606     return 0;
15607
15608   /* Discard alignments we've emit and jump instructions.  */
15609   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
15610       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
15611     return 0;
15612   if (GET_CODE (insn) == JUMP_INSN
15613       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
15614           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
15615     return 0;
15616
15617   /* Important case - calls are always 5 bytes.
15618      It is common to have many calls in the row.  */
15619   if (GET_CODE (insn) == CALL_INSN
15620       && symbolic_reference_mentioned_p (PATTERN (insn))
15621       && !SIBLING_CALL_P (insn))
15622     return 5;
15623   if (get_attr_length (insn) <= 1)
15624     return 1;
15625
15626   /* For normal instructions we may rely on the sizes of addresses
15627      and the presence of symbol to require 4 bytes of encoding.
15628      This is not the case for jumps where references are PC relative.  */
15629   if (GET_CODE (insn) != JUMP_INSN)
15630     {
15631       l = get_attr_length_address (insn);
15632       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
15633         l = 4;
15634     }
15635   if (l)
15636     return 1+l;
15637   else
15638     return 2;
15639 }
15640
15641 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
15642    window.  */
15643
15644 static void
15645 k8_avoid_jump_misspredicts (void)
15646 {
15647   rtx insn, start = get_insns ();
15648   int nbytes = 0, njumps = 0;
15649   int isjump = 0;
15650
15651   /* Look for all minimal intervals of instructions containing 4 jumps.
15652      The intervals are bounded by START and INSN.  NBYTES is the total
15653      size of instructions in the interval including INSN and not including
15654      START.  When the NBYTES is smaller than 16 bytes, it is possible
15655      that the end of START and INSN ends up in the same 16byte page.
15656
15657      The smallest offset in the page INSN can start is the case where START
15658      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
15659      We add p2align to 16byte window with maxskip 17 - NBYTES + sizeof (INSN).
15660      */
15661   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
15662     {
15663
15664       nbytes += min_insn_size (insn);
15665       if (rtl_dump_file)
15666         fprintf(rtl_dump_file, "Insn %i estimated to %i bytes\n",
15667                 INSN_UID (insn), min_insn_size (insn));
15668       if ((GET_CODE (insn) == JUMP_INSN
15669            && GET_CODE (PATTERN (insn)) != ADDR_VEC
15670            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
15671           || GET_CODE (insn) == CALL_INSN)
15672         njumps++;
15673       else
15674         continue;
15675
15676       while (njumps > 3)
15677         {
15678           start = NEXT_INSN (start);
15679           if ((GET_CODE (start) == JUMP_INSN
15680                && GET_CODE (PATTERN (start)) != ADDR_VEC
15681                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
15682               || GET_CODE (start) == CALL_INSN)
15683             njumps--, isjump = 1;
15684           else
15685             isjump = 0;
15686           nbytes -= min_insn_size (start);
15687         }
15688       if (njumps < 0)
15689         abort ();
15690       if (rtl_dump_file)
15691         fprintf(rtl_dump_file, "Interval %i to %i has %i bytes\n",
15692                 INSN_UID (start), INSN_UID (insn), nbytes);
15693
15694       if (njumps == 3 && isjump && nbytes < 16)
15695         {
15696           int padsize = 15 - nbytes + min_insn_size (insn);
15697
15698           if (rtl_dump_file)
15699             fprintf (rtl_dump_file, "Padding insn %i by %i bytes!\n", INSN_UID (insn), padsize);
15700           emit_insn_before (gen_align (GEN_INT (padsize)), insn);
15701         }
15702     }
15703 }
15704
15705 /* Implement machine specific optimizations.
15706    At the moment we implement single transformation: AMD Athlon works faster
15707    when RET is not destination of conditional jump or directly preceded
15708    by other jump instruction.  We avoid the penalty by inserting NOP just
15709    before the RET instructions in such cases.  */
15710 static void
15711 ix86_reorg (void)
15712 {
15713   edge e;
15714
15715   if (!TARGET_ATHLON_K8 || !optimize || optimize_size)
15716     return;
15717   for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
15718   {
15719     basic_block bb = e->src;
15720     rtx ret = BB_END (bb);
15721     rtx prev;
15722     bool replace = false;
15723
15724     if (GET_CODE (ret) != JUMP_INSN || GET_CODE (PATTERN (ret)) != RETURN
15725         || !maybe_hot_bb_p (bb))
15726       continue;
15727     for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
15728       if (active_insn_p (prev) || GET_CODE (prev) == CODE_LABEL)
15729         break;
15730     if (prev && GET_CODE (prev) == CODE_LABEL)
15731       {
15732         edge e;
15733         for (e = bb->pred; e; e = e->pred_next)
15734           if (EDGE_FREQUENCY (e) && e->src->index >= 0
15735               && !(e->flags & EDGE_FALLTHRU))
15736             replace = true;
15737       }
15738     if (!replace)
15739       {
15740         prev = prev_active_insn (ret);
15741         if (prev
15742             && ((GET_CODE (prev) == JUMP_INSN && any_condjump_p (prev))
15743                 || GET_CODE (prev) == CALL_INSN))
15744           replace = true;
15745         /* Empty functions get branch mispredict even when the jump destination
15746            is not visible to us.  */
15747         if (!prev && cfun->function_frequency > FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
15748           replace = true;
15749       }
15750     if (replace)
15751       {
15752         emit_insn_before (gen_return_internal_long (), ret);
15753         delete_insn (ret);
15754       }
15755   }
15756   k8_avoid_jump_misspredicts ();
15757 }
15758
15759 /* Return nonzero when QImode register that must be represented via REX prefix
15760    is used.  */
15761 bool
15762 x86_extended_QIreg_mentioned_p (rtx insn)
15763 {
15764   int i;
15765   extract_insn_cached (insn);
15766   for (i = 0; i < recog_data.n_operands; i++)
15767     if (REG_P (recog_data.operand[i])
15768         && REGNO (recog_data.operand[i]) >= 4)
15769        return true;
15770   return false;
15771 }
15772
15773 /* Return nonzero when P points to register encoded via REX prefix.
15774    Called via for_each_rtx.  */
15775 static int
15776 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
15777 {
15778    unsigned int regno;
15779    if (!REG_P (*p))
15780      return 0;
15781    regno = REGNO (*p);
15782    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
15783 }
15784
15785 /* Return true when INSN mentions register that must be encoded using REX
15786    prefix.  */
15787 bool
15788 x86_extended_reg_mentioned_p (rtx insn)
15789 {
15790   return for_each_rtx (&PATTERN (insn), extended_reg_mentioned_1, NULL);
15791 }
15792
15793 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
15794    optabs would emit if we didn't have TFmode patterns.  */
15795
15796 void
15797 x86_emit_floatuns (rtx operands[2])
15798 {
15799   rtx neglab, donelab, i0, i1, f0, in, out;
15800   enum machine_mode mode, inmode;
15801
15802   inmode = GET_MODE (operands[1]);
15803   if (inmode != SImode
15804       && inmode != DImode)
15805     abort ();
15806
15807   out = operands[0];
15808   in = force_reg (inmode, operands[1]);
15809   mode = GET_MODE (out);
15810   neglab = gen_label_rtx ();
15811   donelab = gen_label_rtx ();
15812   i1 = gen_reg_rtx (Pmode);
15813   f0 = gen_reg_rtx (mode);
15814
15815   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, Pmode, 0, neglab);
15816
15817   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_FLOAT (mode, in)));
15818   emit_jump_insn (gen_jump (donelab));
15819   emit_barrier ();
15820
15821   emit_label (neglab);
15822
15823   i0 = expand_simple_binop (Pmode, LSHIFTRT, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
15824   i1 = expand_simple_binop (Pmode, AND, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
15825   i0 = expand_simple_binop (Pmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
15826   expand_float (f0, i0, 0);
15827   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
15828
15829   emit_label (donelab);
15830 }
15831
15832 /* Return if we do not know how to pass TYPE solely in registers.  */
15833 bool
15834 ix86_must_pass_in_stack (enum machine_mode mode, tree type)
15835 {
15836    if (default_must_pass_in_stack (mode, type))
15837      return true;
15838    return (!TARGET_64BIT && type && mode == TImode);
15839 }
15840
15841 /* Initialize vector TARGET via VALS.  */
15842 void
15843 ix86_expand_vector_init (rtx target, rtx vals)
15844 {
15845   enum machine_mode mode = GET_MODE (target);
15846   int elt_size = GET_MODE_SIZE (GET_MODE_INNER (mode));
15847   int n_elts = (GET_MODE_SIZE (mode) / elt_size);
15848   int i;
15849   
15850   for (i = n_elts - 1; i >= 0; i--)
15851     if (GET_CODE (XVECEXP (vals, 0, i)) != CONST_INT
15852         && GET_CODE (XVECEXP (vals, 0, i)) != CONST_DOUBLE)
15853       break;
15854
15855   /* Few special cases first...  
15856      ... constants are best loaded from constant pool.  */
15857   if (i < 0)
15858     {
15859       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
15860       return;
15861     }
15862
15863   /* ... values where only first field is non-constant are best loaded
15864      from the pool and overwriten via move later.  */
15865   if (!i)
15866     {
15867       rtx op = simplify_gen_subreg (mode, XVECEXP (vals, 0, 0),
15868                                     GET_MODE_INNER (mode), 0);
15869
15870       op = force_reg (mode, op);
15871       XVECEXP (vals, 0, 0) = CONST0_RTX (GET_MODE_INNER (mode));
15872       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
15873       switch (GET_MODE (target))
15874         {
15875           case V2DFmode:
15876             emit_insn (gen_sse2_movsd (target, target, op));
15877             break;
15878           case V4SFmode:
15879             emit_insn (gen_sse_movss (target, target, op));
15880             break;
15881           default:
15882             break;
15883         }
15884       return;
15885     }
15886
15887   /* And the busy sequence doing rotations.  */
15888   switch (GET_MODE (target))
15889     {
15890       case V2DFmode:
15891         {
15892           rtx vecop0 =
15893             simplify_gen_subreg (V2DFmode, XVECEXP (vals, 0, 0), DFmode, 0);
15894           rtx vecop1 =
15895             simplify_gen_subreg (V2DFmode, XVECEXP (vals, 0, 1), DFmode, 0);
15896
15897           vecop0 = force_reg (V2DFmode, vecop0);
15898           vecop1 = force_reg (V2DFmode, vecop1);
15899           emit_insn (gen_sse2_unpcklpd (target, vecop0, vecop1));
15900         }
15901         break;
15902       case V4SFmode:
15903         {
15904           rtx vecop0 =
15905             simplify_gen_subreg (V4SFmode, XVECEXP (vals, 0, 0), SFmode, 0);
15906           rtx vecop1 =
15907             simplify_gen_subreg (V4SFmode, XVECEXP (vals, 0, 1), SFmode, 0);
15908           rtx vecop2 =
15909             simplify_gen_subreg (V4SFmode, XVECEXP (vals, 0, 2), SFmode, 0);
15910           rtx vecop3 =
15911             simplify_gen_subreg (V4SFmode, XVECEXP (vals, 0, 3), SFmode, 0);
15912           rtx tmp1 = gen_reg_rtx (V4SFmode);
15913           rtx tmp2 = gen_reg_rtx (V4SFmode);
15914
15915           vecop0 = force_reg (V4SFmode, vecop0);
15916           vecop1 = force_reg (V4SFmode, vecop1);
15917           vecop2 = force_reg (V4SFmode, vecop2);
15918           vecop3 = force_reg (V4SFmode, vecop3);
15919           emit_insn (gen_sse_unpcklps (tmp1, vecop1, vecop3));
15920           emit_insn (gen_sse_unpcklps (tmp2, vecop0, vecop2));
15921           emit_insn (gen_sse_unpcklps (target, tmp2, tmp1));
15922         }
15923         break;
15924       default:
15925         abort ();
15926     }
15927 }
15928
15929 #include "gt-i386.h"