OSDN Git Service

* Makefile.in (cse.o): Depend on TARGET_H.
[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 GNU CC.
6
7 GNU CC 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 GNU CC 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 GNU CC; 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
49 #ifndef CHECK_STACK_LIMIT
50 #define CHECK_STACK_LIMIT (-1)
51 #endif
52
53 /* Return index of given mode in mult and division cost tables.  */
54 #define MODE_INDEX(mode)                                        \
55   ((mode) == QImode ? 0                                         \
56    : (mode) == HImode ? 1                                       \
57    : (mode) == SImode ? 2                                       \
58    : (mode) == DImode ? 3                                       \
59    : 4)
60
61 /* Processor costs (relative to an add) */
62 static const
63 struct processor_costs size_cost = {    /* costs for tunning for size */
64   2,                                    /* cost of an add instruction */
65   3,                                    /* cost of a lea instruction */
66   2,                                    /* variable shift costs */
67   3,                                    /* constant shift costs */
68   {3, 3, 3, 3, 5},                      /* cost of starting a multiply */
69   0,                                    /* cost of multiply per each bit set */
70   {3, 3, 3, 3, 5},                      /* cost of a divide/mod */
71   3,                                    /* cost of movsx */
72   3,                                    /* cost of movzx */
73   0,                                    /* "large" insn */
74   2,                                    /* MOVE_RATIO */
75   2,                                    /* cost for loading QImode using movzbl */
76   {2, 2, 2},                            /* cost of loading integer registers
77                                            in QImode, HImode and SImode.
78                                            Relative to reg-reg move (2).  */
79   {2, 2, 2},                            /* cost of storing integer registers */
80   2,                                    /* cost of reg,reg fld/fst */
81   {2, 2, 2},                            /* cost of loading fp registers
82                                            in SFmode, DFmode and XFmode */
83   {2, 2, 2},                            /* cost of loading integer registers */
84   3,                                    /* cost of moving MMX register */
85   {3, 3},                               /* cost of loading MMX registers
86                                            in SImode and DImode */
87   {3, 3},                               /* cost of storing MMX registers
88                                            in SImode and DImode */
89   3,                                    /* cost of moving SSE register */
90   {3, 3, 3},                            /* cost of loading SSE registers
91                                            in SImode, DImode and TImode */
92   {3, 3, 3},                            /* cost of storing SSE registers
93                                            in SImode, DImode and TImode */
94   3,                                    /* MMX or SSE register to integer */
95   0,                                    /* size of prefetch block */
96   0,                                    /* number of parallel prefetches */
97   1,                                    /* Branch cost */
98   2,                                    /* cost of FADD and FSUB insns.  */
99   2,                                    /* cost of FMUL instruction.  */
100   2,                                    /* cost of FDIV instruction.  */
101   2,                                    /* cost of FABS instruction.  */
102   2,                                    /* cost of FCHS instruction.  */
103   2,                                    /* cost of FSQRT instruction.  */
104 };
105
106 /* Processor costs (relative to an add) */
107 static const
108 struct processor_costs i386_cost = {    /* 386 specific costs */
109   1,                                    /* cost of an add instruction */
110   1,                                    /* cost of a lea instruction */
111   3,                                    /* variable shift costs */
112   2,                                    /* constant shift costs */
113   {6, 6, 6, 6, 6},                      /* cost of starting a multiply */
114   1,                                    /* cost of multiply per each bit set */
115   {23, 23, 23, 23, 23},                 /* cost of a divide/mod */
116   3,                                    /* cost of movsx */
117   2,                                    /* cost of movzx */
118   15,                                   /* "large" insn */
119   3,                                    /* MOVE_RATIO */
120   4,                                    /* cost for loading QImode using movzbl */
121   {2, 4, 2},                            /* cost of loading integer registers
122                                            in QImode, HImode and SImode.
123                                            Relative to reg-reg move (2).  */
124   {2, 4, 2},                            /* cost of storing integer registers */
125   2,                                    /* cost of reg,reg fld/fst */
126   {8, 8, 8},                            /* cost of loading fp registers
127                                            in SFmode, DFmode and XFmode */
128   {8, 8, 8},                            /* cost of loading integer registers */
129   2,                                    /* cost of moving MMX register */
130   {4, 8},                               /* cost of loading MMX registers
131                                            in SImode and DImode */
132   {4, 8},                               /* cost of storing MMX registers
133                                            in SImode and DImode */
134   2,                                    /* cost of moving SSE register */
135   {4, 8, 16},                           /* cost of loading SSE registers
136                                            in SImode, DImode and TImode */
137   {4, 8, 16},                           /* cost of storing SSE registers
138                                            in SImode, DImode and TImode */
139   3,                                    /* MMX or SSE register to integer */
140   0,                                    /* size of prefetch block */
141   0,                                    /* number of parallel prefetches */
142   1,                                    /* Branch cost */
143   23,                                   /* cost of FADD and FSUB insns.  */
144   27,                                   /* cost of FMUL instruction.  */
145   88,                                   /* cost of FDIV instruction.  */
146   22,                                   /* cost of FABS instruction.  */
147   24,                                   /* cost of FCHS instruction.  */
148   122,                                  /* cost of FSQRT instruction.  */
149 };
150
151 static const
152 struct processor_costs i486_cost = {    /* 486 specific costs */
153   1,                                    /* cost of an add instruction */
154   1,                                    /* cost of a lea instruction */
155   3,                                    /* variable shift costs */
156   2,                                    /* constant shift costs */
157   {12, 12, 12, 12, 12},                 /* cost of starting a multiply */
158   1,                                    /* cost of multiply per each bit set */
159   {40, 40, 40, 40, 40},                 /* cost of a divide/mod */
160   3,                                    /* cost of movsx */
161   2,                                    /* cost of movzx */
162   15,                                   /* "large" insn */
163   3,                                    /* MOVE_RATIO */
164   4,                                    /* cost for loading QImode using movzbl */
165   {2, 4, 2},                            /* cost of loading integer registers
166                                            in QImode, HImode and SImode.
167                                            Relative to reg-reg move (2).  */
168   {2, 4, 2},                            /* cost of storing integer registers */
169   2,                                    /* cost of reg,reg fld/fst */
170   {8, 8, 8},                            /* cost of loading fp registers
171                                            in SFmode, DFmode and XFmode */
172   {8, 8, 8},                            /* cost of loading integer registers */
173   2,                                    /* cost of moving MMX register */
174   {4, 8},                               /* cost of loading MMX registers
175                                            in SImode and DImode */
176   {4, 8},                               /* cost of storing MMX registers
177                                            in SImode and DImode */
178   2,                                    /* cost of moving SSE register */
179   {4, 8, 16},                           /* cost of loading SSE registers
180                                            in SImode, DImode and TImode */
181   {4, 8, 16},                           /* cost of storing SSE registers
182                                            in SImode, DImode and TImode */
183   3,                                    /* MMX or SSE register to integer */
184   0,                                    /* size of prefetch block */
185   0,                                    /* number of parallel prefetches */
186   1,                                    /* Branch cost */
187   8,                                    /* cost of FADD and FSUB insns.  */
188   16,                                   /* cost of FMUL instruction.  */
189   73,                                   /* cost of FDIV instruction.  */
190   3,                                    /* cost of FABS instruction.  */
191   3,                                    /* cost of FCHS instruction.  */
192   83,                                   /* cost of FSQRT instruction.  */
193 };
194
195 static const
196 struct processor_costs pentium_cost = {
197   1,                                    /* cost of an add instruction */
198   1,                                    /* cost of a lea instruction */
199   4,                                    /* variable shift costs */
200   1,                                    /* constant shift costs */
201   {11, 11, 11, 11, 11},                 /* cost of starting a multiply */
202   0,                                    /* cost of multiply per each bit set */
203   {25, 25, 25, 25, 25},                 /* cost of a divide/mod */
204   3,                                    /* cost of movsx */
205   2,                                    /* cost of movzx */
206   8,                                    /* "large" insn */
207   6,                                    /* MOVE_RATIO */
208   6,                                    /* cost for loading QImode using movzbl */
209   {2, 4, 2},                            /* cost of loading integer registers
210                                            in QImode, HImode and SImode.
211                                            Relative to reg-reg move (2).  */
212   {2, 4, 2},                            /* cost of storing integer registers */
213   2,                                    /* cost of reg,reg fld/fst */
214   {2, 2, 6},                            /* cost of loading fp registers
215                                            in SFmode, DFmode and XFmode */
216   {4, 4, 6},                            /* cost of loading integer registers */
217   8,                                    /* cost of moving MMX register */
218   {8, 8},                               /* cost of loading MMX registers
219                                            in SImode and DImode */
220   {8, 8},                               /* cost of storing MMX registers
221                                            in SImode and DImode */
222   2,                                    /* cost of moving SSE register */
223   {4, 8, 16},                           /* cost of loading SSE registers
224                                            in SImode, DImode and TImode */
225   {4, 8, 16},                           /* cost of storing SSE registers
226                                            in SImode, DImode and TImode */
227   3,                                    /* MMX or SSE register to integer */
228   0,                                    /* size of prefetch block */
229   0,                                    /* number of parallel prefetches */
230   2,                                    /* Branch cost */
231   3,                                    /* cost of FADD and FSUB insns.  */
232   3,                                    /* cost of FMUL instruction.  */
233   39,                                   /* cost of FDIV instruction.  */
234   1,                                    /* cost of FABS instruction.  */
235   1,                                    /* cost of FCHS instruction.  */
236   70,                                   /* cost of FSQRT instruction.  */
237 };
238
239 static const
240 struct processor_costs pentiumpro_cost = {
241   1,                                    /* cost of an add instruction */
242   1,                                    /* cost of a lea instruction */
243   1,                                    /* variable shift costs */
244   1,                                    /* constant shift costs */
245   {4, 4, 4, 4, 4},                      /* cost of starting a multiply */
246   0,                                    /* cost of multiply per each bit set */
247   {17, 17, 17, 17, 17},                 /* cost of a divide/mod */
248   1,                                    /* cost of movsx */
249   1,                                    /* cost of movzx */
250   8,                                    /* "large" insn */
251   6,                                    /* MOVE_RATIO */
252   2,                                    /* cost for loading QImode using movzbl */
253   {4, 4, 4},                            /* cost of loading integer registers
254                                            in QImode, HImode and SImode.
255                                            Relative to reg-reg move (2).  */
256   {2, 2, 2},                            /* cost of storing integer registers */
257   2,                                    /* cost of reg,reg fld/fst */
258   {2, 2, 6},                            /* cost of loading fp registers
259                                            in SFmode, DFmode and XFmode */
260   {4, 4, 6},                            /* cost of loading integer registers */
261   2,                                    /* cost of moving MMX register */
262   {2, 2},                               /* cost of loading MMX registers
263                                            in SImode and DImode */
264   {2, 2},                               /* cost of storing MMX registers
265                                            in SImode and DImode */
266   2,                                    /* cost of moving SSE register */
267   {2, 2, 8},                            /* cost of loading SSE registers
268                                            in SImode, DImode and TImode */
269   {2, 2, 8},                            /* cost of storing SSE registers
270                                            in SImode, DImode and TImode */
271   3,                                    /* MMX or SSE register to integer */
272   32,                                   /* size of prefetch block */
273   6,                                    /* number of parallel prefetches */
274   2,                                    /* Branch cost */
275   3,                                    /* cost of FADD and FSUB insns.  */
276   5,                                    /* cost of FMUL instruction.  */
277   56,                                   /* cost of FDIV instruction.  */
278   2,                                    /* cost of FABS instruction.  */
279   2,                                    /* cost of FCHS instruction.  */
280   56,                                   /* cost of FSQRT instruction.  */
281 };
282
283 static const
284 struct processor_costs k6_cost = {
285   1,                                    /* cost of an add instruction */
286   2,                                    /* cost of a lea instruction */
287   1,                                    /* variable shift costs */
288   1,                                    /* constant shift costs */
289   {3, 3, 3, 3, 3},                      /* cost of starting a multiply */
290   0,                                    /* cost of multiply per each bit set */
291   {18, 18, 18, 18, 18},                 /* cost of a divide/mod */
292   2,                                    /* cost of movsx */
293   2,                                    /* cost of movzx */
294   8,                                    /* "large" insn */
295   4,                                    /* MOVE_RATIO */
296   3,                                    /* cost for loading QImode using movzbl */
297   {4, 5, 4},                            /* cost of loading integer registers
298                                            in QImode, HImode and SImode.
299                                            Relative to reg-reg move (2).  */
300   {2, 3, 2},                            /* cost of storing integer registers */
301   4,                                    /* cost of reg,reg fld/fst */
302   {6, 6, 6},                            /* cost of loading fp registers
303                                            in SFmode, DFmode and XFmode */
304   {4, 4, 4},                            /* cost of loading integer registers */
305   2,                                    /* cost of moving MMX register */
306   {2, 2},                               /* cost of loading MMX registers
307                                            in SImode and DImode */
308   {2, 2},                               /* cost of storing MMX registers
309                                            in SImode and DImode */
310   2,                                    /* cost of moving SSE register */
311   {2, 2, 8},                            /* cost of loading SSE registers
312                                            in SImode, DImode and TImode */
313   {2, 2, 8},                            /* cost of storing SSE registers
314                                            in SImode, DImode and TImode */
315   6,                                    /* MMX or SSE register to integer */
316   32,                                   /* size of prefetch block */
317   1,                                    /* number of parallel prefetches */
318   1,                                    /* Branch cost */
319   2,                                    /* cost of FADD and FSUB insns.  */
320   2,                                    /* cost of FMUL instruction.  */
321   56,                                   /* cost of FDIV instruction.  */
322   2,                                    /* cost of FABS instruction.  */
323   2,                                    /* cost of FCHS instruction.  */
324   56,                                   /* cost of FSQRT instruction.  */
325 };
326
327 static const
328 struct processor_costs athlon_cost = {
329   1,                                    /* cost of an add instruction */
330   2,                                    /* cost of a lea instruction */
331   1,                                    /* variable shift costs */
332   1,                                    /* constant shift costs */
333   {5, 5, 5, 5, 5},                      /* cost of starting a multiply */
334   0,                                    /* cost of multiply per each bit set */
335   {18, 26, 42, 74, 74},                 /* cost of a divide/mod */
336   1,                                    /* cost of movsx */
337   1,                                    /* cost of movzx */
338   8,                                    /* "large" insn */
339   9,                                    /* MOVE_RATIO */
340   4,                                    /* cost for loading QImode using movzbl */
341   {3, 4, 3},                            /* cost of loading integer registers
342                                            in QImode, HImode and SImode.
343                                            Relative to reg-reg move (2).  */
344   {3, 4, 3},                            /* cost of storing integer registers */
345   4,                                    /* cost of reg,reg fld/fst */
346   {4, 4, 12},                           /* cost of loading fp registers
347                                            in SFmode, DFmode and XFmode */
348   {6, 6, 8},                            /* cost of loading integer registers */
349   2,                                    /* cost of moving MMX register */
350   {4, 4},                               /* cost of loading MMX registers
351                                            in SImode and DImode */
352   {4, 4},                               /* cost of storing MMX registers
353                                            in SImode and DImode */
354   2,                                    /* cost of moving SSE register */
355   {4, 4, 6},                            /* cost of loading SSE registers
356                                            in SImode, DImode and TImode */
357   {4, 4, 5},                            /* cost of storing SSE registers
358                                            in SImode, DImode and TImode */
359   5,                                    /* MMX or SSE register to integer */
360   64,                                   /* size of prefetch block */
361   6,                                    /* number of parallel prefetches */
362   2,                                    /* Branch cost */
363   4,                                    /* cost of FADD and FSUB insns.  */
364   4,                                    /* cost of FMUL instruction.  */
365   24,                                   /* cost of FDIV instruction.  */
366   2,                                    /* cost of FABS instruction.  */
367   2,                                    /* cost of FCHS instruction.  */
368   35,                                   /* cost of FSQRT instruction.  */
369 };
370
371 static const
372 struct processor_costs k8_cost = {
373   1,                                    /* cost of an add instruction */
374   2,                                    /* cost of a lea instruction */
375   1,                                    /* variable shift costs */
376   1,                                    /* constant shift costs */
377   {3, 4, 3, 4, 5},                      /* cost of starting a multiply */
378   0,                                    /* cost of multiply per each bit set */
379   {18, 26, 42, 74, 74},                 /* cost of a divide/mod */
380   1,                                    /* cost of movsx */
381   1,                                    /* cost of movzx */
382   8,                                    /* "large" insn */
383   9,                                    /* MOVE_RATIO */
384   4,                                    /* cost for loading QImode using movzbl */
385   {3, 4, 3},                            /* cost of loading integer registers
386                                            in QImode, HImode and SImode.
387                                            Relative to reg-reg move (2).  */
388   {3, 4, 3},                            /* cost of storing integer registers */
389   4,                                    /* cost of reg,reg fld/fst */
390   {4, 4, 12},                           /* cost of loading fp registers
391                                            in SFmode, DFmode and XFmode */
392   {6, 6, 8},                            /* cost of loading integer registers */
393   2,                                    /* cost of moving MMX register */
394   {3, 3},                               /* cost of loading MMX registers
395                                            in SImode and DImode */
396   {4, 4},                               /* cost of storing MMX registers
397                                            in SImode and DImode */
398   2,                                    /* cost of moving SSE register */
399   {4, 3, 6},                            /* cost of loading SSE registers
400                                            in SImode, DImode and TImode */
401   {4, 4, 5},                            /* cost of storing SSE registers
402                                            in SImode, DImode and TImode */
403   5,                                    /* MMX or SSE register to integer */
404   64,                                   /* size of prefetch block */
405   6,                                    /* number of parallel prefetches */
406   2,                                    /* Branch cost */
407   4,                                    /* cost of FADD and FSUB insns.  */
408   4,                                    /* cost of FMUL instruction.  */
409   19,                                   /* cost of FDIV instruction.  */
410   2,                                    /* cost of FABS instruction.  */
411   2,                                    /* cost of FCHS instruction.  */
412   35,                                   /* cost of FSQRT instruction.  */
413 };
414
415 static const
416 struct processor_costs pentium4_cost = {
417   1,                                    /* cost of an add instruction */
418   1,                                    /* cost of a lea instruction */
419   4,                                    /* variable shift costs */
420   4,                                    /* constant shift costs */
421   {15, 15, 15, 15, 15},                 /* cost of starting a multiply */
422   0,                                    /* cost of multiply per each bit set */
423   {56, 56, 56, 56, 56},                 /* cost of a divide/mod */
424   1,                                    /* cost of movsx */
425   1,                                    /* cost of movzx */
426   16,                                   /* "large" insn */
427   6,                                    /* MOVE_RATIO */
428   2,                                    /* cost for loading QImode using movzbl */
429   {4, 5, 4},                            /* cost of loading integer registers
430                                            in QImode, HImode and SImode.
431                                            Relative to reg-reg move (2).  */
432   {2, 3, 2},                            /* cost of storing integer registers */
433   2,                                    /* cost of reg,reg fld/fst */
434   {2, 2, 6},                            /* cost of loading fp registers
435                                            in SFmode, DFmode and XFmode */
436   {4, 4, 6},                            /* cost of loading integer registers */
437   2,                                    /* cost of moving MMX register */
438   {2, 2},                               /* cost of loading MMX registers
439                                            in SImode and DImode */
440   {2, 2},                               /* cost of storing MMX registers
441                                            in SImode and DImode */
442   12,                                   /* cost of moving SSE register */
443   {12, 12, 12},                         /* cost of loading SSE registers
444                                            in SImode, DImode and TImode */
445   {2, 2, 8},                            /* cost of storing SSE registers
446                                            in SImode, DImode and TImode */
447   10,                                   /* MMX or SSE register to integer */
448   64,                                   /* size of prefetch block */
449   6,                                    /* number of parallel prefetches */
450   2,                                    /* Branch cost */
451   5,                                    /* cost of FADD and FSUB insns.  */
452   7,                                    /* cost of FMUL instruction.  */
453   43,                                   /* cost of FDIV instruction.  */
454   2,                                    /* cost of FABS instruction.  */
455   2,                                    /* cost of FCHS instruction.  */
456   43,                                   /* cost of FSQRT instruction.  */
457 };
458
459 const struct processor_costs *ix86_cost = &pentium_cost;
460
461 /* Processor feature/optimization bitmasks.  */
462 #define m_386 (1<<PROCESSOR_I386)
463 #define m_486 (1<<PROCESSOR_I486)
464 #define m_PENT (1<<PROCESSOR_PENTIUM)
465 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
466 #define m_K6  (1<<PROCESSOR_K6)
467 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
468 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
469 #define m_K8  (1<<PROCESSOR_K8)
470 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
471
472 const int x86_use_leave = m_386 | m_K6 | m_ATHLON_K8;
473 const int x86_push_memory = m_386 | m_K6 | m_ATHLON_K8 | m_PENT4;
474 const int x86_zero_extend_with_and = m_486 | m_PENT;
475 const int x86_movx = m_ATHLON_K8 | m_PPRO | m_PENT4 /* m_386 | m_K6 */;
476 const int x86_double_with_add = ~m_386;
477 const int x86_use_bit_test = m_386;
478 const int x86_unroll_strlen = m_486 | m_PENT | m_PPRO | m_ATHLON_K8 | m_K6;
479 const int x86_cmove = m_PPRO | m_ATHLON_K8 | m_PENT4;
480 const int x86_3dnow_a = m_ATHLON_K8;
481 const int x86_deep_branch = m_PPRO | m_K6 | m_ATHLON_K8 | m_PENT4;
482 const int x86_branch_hints = m_PENT4;
483 const int x86_use_sahf = m_PPRO | m_K6 | m_PENT4;
484 const int x86_partial_reg_stall = m_PPRO;
485 const int x86_use_loop = m_K6;
486 const int x86_use_fiop = ~(m_PPRO | m_ATHLON_K8 | m_PENT);
487 const int x86_use_mov0 = m_K6;
488 const int x86_use_cltd = ~(m_PENT | m_K6);
489 const int x86_read_modify_write = ~m_PENT;
490 const int x86_read_modify = ~(m_PENT | m_PPRO);
491 const int x86_split_long_moves = m_PPRO;
492 const int x86_promote_QImode = m_K6 | m_PENT | m_386 | m_486 | m_ATHLON_K8;
493 const int x86_fast_prefix = ~(m_PENT | m_486 | m_386);
494 const int x86_single_stringop = m_386 | m_PENT4;
495 const int x86_qimode_math = ~(0);
496 const int x86_promote_qi_regs = 0;
497 const int x86_himode_math = ~(m_PPRO);
498 const int x86_promote_hi_regs = m_PPRO;
499 const int x86_sub_esp_4 = m_ATHLON_K8 | m_PPRO | m_PENT4;
500 const int x86_sub_esp_8 = m_ATHLON_K8 | m_PPRO | m_386 | m_486 | m_PENT4;
501 const int x86_add_esp_4 = m_ATHLON_K8 | m_K6 | m_PENT4;
502 const int x86_add_esp_8 = m_ATHLON_K8 | m_PPRO | m_K6 | m_386 | m_486 | m_PENT4;
503 const int x86_integer_DFmode_moves = ~(m_ATHLON_K8 | m_PENT4 | m_PPRO);
504 const int x86_partial_reg_dependency = m_ATHLON_K8 | m_PENT4;
505 const int x86_memory_mismatch_stall = m_ATHLON_K8 | m_PENT4;
506 const int x86_accumulate_outgoing_args = m_ATHLON_K8 | m_PENT4 | m_PPRO;
507 const int x86_prologue_using_move = m_ATHLON_K8 | m_PENT4 | m_PPRO;
508 const int x86_epilogue_using_move = m_ATHLON_K8 | m_PENT4 | m_PPRO;
509 const int x86_decompose_lea = m_PENT4;
510 const int x86_shift1 = ~m_486;
511 const int x86_arch_always_fancy_math_387 = m_PENT | m_PPRO | m_ATHLON_K8 | m_PENT4;
512 const int x86_sse_partial_reg_dependency = m_PENT4 | m_PPRO;
513 /* Set for machines where the type and dependencies are resolved on SSE register
514    parts instead of whole registers, so we may maintain just lower part of
515    scalar values in proper format leaving the upper part undefined.  */
516 const int x86_sse_partial_regs = m_ATHLON_K8;
517 /* Athlon optimizes partial-register FPS special case, thus avoiding the
518    need for extra instructions beforehand  */
519 const int x86_sse_partial_regs_for_cvtsd2ss = 0;
520 const int x86_sse_typeless_stores = m_ATHLON_K8;
521 const int x86_sse_load0_by_pxor = m_PPRO | m_PENT4;
522 const int x86_use_ffreep = m_ATHLON_K8;
523 const int x86_rep_movl_optimal = m_386 | m_PENT | m_PPRO | m_K6;
524
525 /* In case the average insn count for single function invocation is
526    lower than this constant, emit fast (but longer) prologue and
527    epilogue code.  */
528 #define FAST_PROLOGUE_INSN_COUNT 20
529
530 /* Set by prologue expander and used by epilogue expander to determine
531    the style used.  */
532 static int use_fast_prologue_epilogue;
533
534 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
535 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
536 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
537 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
538
539 /* Array of the smallest class containing reg number REGNO, indexed by
540    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
541
542 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
543 {
544   /* ax, dx, cx, bx */
545   AREG, DREG, CREG, BREG,
546   /* si, di, bp, sp */
547   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
548   /* FP registers */
549   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
550   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
551   /* arg pointer */
552   NON_Q_REGS,
553   /* flags, fpsr, dirflag, frame */
554   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
555   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
556   SSE_REGS, SSE_REGS,
557   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
558   MMX_REGS, MMX_REGS,
559   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
560   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
561   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
562   SSE_REGS, SSE_REGS,
563 };
564
565 /* The "default" register map used in 32bit mode.  */
566
567 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
568 {
569   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
570   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
571   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, dir, frame */
572   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
573   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
574   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
575   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
576 };
577
578 static int const x86_64_int_parameter_registers[6] =
579 {
580   5 /*RDI*/, 4 /*RSI*/, 1 /*RDX*/, 2 /*RCX*/,
581   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
582 };
583
584 static int const x86_64_int_return_registers[4] =
585 {
586   0 /*RAX*/, 1 /*RDI*/, 5 /*RDI*/, 4 /*RSI*/
587 };
588
589 /* The "default" register map used in 64bit mode.  */
590 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
591 {
592   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
593   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
594   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, dir, frame */
595   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
596   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
597   8,9,10,11,12,13,14,15,                /* extended integer registers */
598   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
599 };
600
601 /* Define the register numbers to be used in Dwarf debugging information.
602    The SVR4 reference port C compiler uses the following register numbers
603    in its Dwarf output code:
604         0 for %eax (gcc regno = 0)
605         1 for %ecx (gcc regno = 2)
606         2 for %edx (gcc regno = 1)
607         3 for %ebx (gcc regno = 3)
608         4 for %esp (gcc regno = 7)
609         5 for %ebp (gcc regno = 6)
610         6 for %esi (gcc regno = 4)
611         7 for %edi (gcc regno = 5)
612    The following three DWARF register numbers are never generated by
613    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
614    believes these numbers have these meanings.
615         8  for %eip    (no gcc equivalent)
616         9  for %eflags (gcc regno = 17)
617         10 for %trapno (no gcc equivalent)
618    It is not at all clear how we should number the FP stack registers
619    for the x86 architecture.  If the version of SDB on x86/svr4 were
620    a bit less brain dead with respect to floating-point then we would
621    have a precedent to follow with respect to DWARF register numbers
622    for x86 FP registers, but the SDB on x86/svr4 is so completely
623    broken with respect to FP registers that it is hardly worth thinking
624    of it as something to strive for compatibility with.
625    The version of x86/svr4 SDB I have at the moment does (partially)
626    seem to believe that DWARF register number 11 is associated with
627    the x86 register %st(0), but that's about all.  Higher DWARF
628    register numbers don't seem to be associated with anything in
629    particular, and even for DWARF regno 11, SDB only seems to under-
630    stand that it should say that a variable lives in %st(0) (when
631    asked via an `=' command) if we said it was in DWARF regno 11,
632    but SDB still prints garbage when asked for the value of the
633    variable in question (via a `/' command).
634    (Also note that the labels SDB prints for various FP stack regs
635    when doing an `x' command are all wrong.)
636    Note that these problems generally don't affect the native SVR4
637    C compiler because it doesn't allow the use of -O with -g and
638    because when it is *not* optimizing, it allocates a memory
639    location for each floating-point variable, and the memory
640    location is what gets described in the DWARF AT_location
641    attribute for the variable in question.
642    Regardless of the severe mental illness of the x86/svr4 SDB, we
643    do something sensible here and we use the following DWARF
644    register numbers.  Note that these are all stack-top-relative
645    numbers.
646         11 for %st(0) (gcc regno = 8)
647         12 for %st(1) (gcc regno = 9)
648         13 for %st(2) (gcc regno = 10)
649         14 for %st(3) (gcc regno = 11)
650         15 for %st(4) (gcc regno = 12)
651         16 for %st(5) (gcc regno = 13)
652         17 for %st(6) (gcc regno = 14)
653         18 for %st(7) (gcc regno = 15)
654 */
655 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
656 {
657   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
658   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
659   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, dir, frame */
660   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
661   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
662   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
663   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
664 };
665
666 /* Test and compare insns in i386.md store the information needed to
667    generate branch and scc insns here.  */
668
669 rtx ix86_compare_op0 = NULL_RTX;
670 rtx ix86_compare_op1 = NULL_RTX;
671
672 /* The encoding characters for the four TLS models present in ELF.  */
673
674 static char const tls_model_chars[] = " GLil";
675
676 #define MAX_386_STACK_LOCALS 3
677 /* Size of the register save area.  */
678 #define X86_64_VARARGS_SIZE (REGPARM_MAX * UNITS_PER_WORD + SSE_REGPARM_MAX * 16)
679
680 /* Define the structure for the machine field in struct function.  */
681 struct machine_function GTY(())
682 {
683   rtx stack_locals[(int) MAX_MACHINE_MODE][MAX_386_STACK_LOCALS];
684   const char *some_ld_name;
685   int save_varrargs_registers;
686   int accesses_prev_frame;
687 };
688
689 #define ix86_stack_locals (cfun->machine->stack_locals)
690 #define ix86_save_varrargs_registers (cfun->machine->save_varrargs_registers)
691
692 /* Structure describing stack frame layout.
693    Stack grows downward:
694
695    [arguments]
696                                               <- ARG_POINTER
697    saved pc
698
699    saved frame pointer if frame_pointer_needed
700                                               <- HARD_FRAME_POINTER
701    [saved regs]
702
703    [padding1]          \
704                         )
705    [va_arg registers]  (
706                         > to_allocate         <- FRAME_POINTER
707    [frame]             (
708                         )
709    [padding2]          /
710   */
711 struct ix86_frame
712 {
713   int nregs;
714   int padding1;
715   int va_arg_size;
716   HOST_WIDE_INT frame;
717   int padding2;
718   int outgoing_arguments_size;
719   int red_zone_size;
720
721   HOST_WIDE_INT to_allocate;
722   /* The offsets relative to ARG_POINTER.  */
723   HOST_WIDE_INT frame_pointer_offset;
724   HOST_WIDE_INT hard_frame_pointer_offset;
725   HOST_WIDE_INT stack_pointer_offset;
726 };
727
728 /* Used to enable/disable debugging features.  */
729 const char *ix86_debug_arg_string, *ix86_debug_addr_string;
730 /* Code model option as passed by user.  */
731 const char *ix86_cmodel_string;
732 /* Parsed value.  */
733 enum cmodel ix86_cmodel;
734 /* Asm dialect.  */
735 const char *ix86_asm_string;
736 enum asm_dialect ix86_asm_dialect = ASM_ATT;
737 /* TLS dialext.  */
738 const char *ix86_tls_dialect_string;
739 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
740
741 /* Which unit we are generating floating point math for.  */
742 enum fpmath_unit ix86_fpmath;
743
744 /* Which cpu are we scheduling for.  */
745 enum processor_type ix86_cpu;
746 /* Which instruction set architecture to use.  */
747 enum processor_type ix86_arch;
748
749 /* Strings to hold which cpu and instruction set architecture  to use.  */
750 const char *ix86_cpu_string;            /* for -mcpu=<xxx> */
751 const char *ix86_arch_string;           /* for -march=<xxx> */
752 const char *ix86_fpmath_string;         /* for -mfpmath=<xxx> */
753
754 /* # of registers to use to pass arguments.  */
755 const char *ix86_regparm_string;
756
757 /* true if sse prefetch instruction is not NOOP.  */
758 int x86_prefetch_sse;
759
760 /* ix86_regparm_string as a number */
761 int ix86_regparm;
762
763 /* Alignment to use for loops and jumps:  */
764
765 /* Power of two alignment for loops.  */
766 const char *ix86_align_loops_string;
767
768 /* Power of two alignment for non-loop jumps.  */
769 const char *ix86_align_jumps_string;
770
771 /* Power of two alignment for stack boundary in bytes.  */
772 const char *ix86_preferred_stack_boundary_string;
773
774 /* Preferred alignment for stack boundary in bits.  */
775 int ix86_preferred_stack_boundary;
776
777 /* Values 1-5: see jump.c */
778 int ix86_branch_cost;
779 const char *ix86_branch_cost_string;
780
781 /* Power of two alignment for functions.  */
782 const char *ix86_align_funcs_string;
783
784 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
785 static char internal_label_prefix[16];
786 static int internal_label_prefix_len;
787 \f
788 static int local_symbolic_operand PARAMS ((rtx, enum machine_mode));
789 static int tls_symbolic_operand_1 PARAMS ((rtx, enum tls_model));
790 static void output_pic_addr_const PARAMS ((FILE *, rtx, int));
791 static void put_condition_code PARAMS ((enum rtx_code, enum machine_mode,
792                                        int, int, FILE *));
793 static const char *get_some_local_dynamic_name PARAMS ((void));
794 static int get_some_local_dynamic_name_1 PARAMS ((rtx *, void *));
795 static rtx maybe_get_pool_constant PARAMS ((rtx));
796 static rtx ix86_expand_int_compare PARAMS ((enum rtx_code, rtx, rtx));
797 static enum rtx_code ix86_prepare_fp_compare_args PARAMS ((enum rtx_code,
798                                                            rtx *, rtx *));
799 static rtx get_thread_pointer PARAMS ((void));
800 static void get_pc_thunk_name PARAMS ((char [32], unsigned int));
801 static rtx gen_push PARAMS ((rtx));
802 static int memory_address_length PARAMS ((rtx addr));
803 static int ix86_flags_dependant PARAMS ((rtx, rtx, enum attr_type));
804 static int ix86_agi_dependant PARAMS ((rtx, rtx, enum attr_type));
805 static enum attr_ppro_uops ix86_safe_ppro_uops PARAMS ((rtx));
806 static void ix86_dump_ppro_packet PARAMS ((FILE *));
807 static void ix86_reorder_insn PARAMS ((rtx *, rtx *));
808 static struct machine_function * ix86_init_machine_status PARAMS ((void));
809 static int ix86_split_to_parts PARAMS ((rtx, rtx *, enum machine_mode));
810 static int ix86_nsaved_regs PARAMS ((void));
811 static void ix86_emit_save_regs PARAMS ((void));
812 static void ix86_emit_save_regs_using_mov PARAMS ((rtx, HOST_WIDE_INT));
813 static void ix86_emit_restore_regs_using_mov PARAMS ((rtx, int, int));
814 static void ix86_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
815 static void ix86_set_move_mem_attrs_1 PARAMS ((rtx, rtx, rtx, rtx, rtx));
816 static void ix86_sched_reorder_ppro PARAMS ((rtx *, rtx *));
817 static HOST_WIDE_INT ix86_GOT_alias_set PARAMS ((void));
818 static void ix86_adjust_counter PARAMS ((rtx, HOST_WIDE_INT));
819 static rtx ix86_expand_aligntest PARAMS ((rtx, int));
820 static void ix86_expand_strlensi_unroll_1 PARAMS ((rtx, rtx));
821 static int ix86_issue_rate PARAMS ((void));
822 static int ix86_adjust_cost PARAMS ((rtx, rtx, rtx, int));
823 static void ix86_sched_init PARAMS ((FILE *, int, int));
824 static int ix86_sched_reorder PARAMS ((FILE *, int, rtx *, int *, int));
825 static int ix86_variable_issue PARAMS ((FILE *, int, rtx, int));
826 static int ia32_use_dfa_pipeline_interface PARAMS ((void));
827 static int ia32_multipass_dfa_lookahead PARAMS ((void));
828 static void ix86_init_mmx_sse_builtins PARAMS ((void));
829 static rtx x86_this_parameter PARAMS ((tree));
830 static void x86_output_mi_thunk PARAMS ((FILE *, tree, HOST_WIDE_INT,
831                                          HOST_WIDE_INT, tree));
832 static bool x86_can_output_mi_thunk PARAMS ((tree, HOST_WIDE_INT,
833                                              HOST_WIDE_INT, tree));
834 bool ix86_expand_carry_flag_compare PARAMS ((enum rtx_code, rtx, rtx, rtx*));
835
836 struct ix86_address
837 {
838   rtx base, index, disp;
839   HOST_WIDE_INT scale;
840 };
841
842 static int ix86_decompose_address PARAMS ((rtx, struct ix86_address *));
843 static bool ix86_cannot_force_const_mem PARAMS ((rtx));
844
845 static void ix86_encode_section_info PARAMS ((tree, int)) ATTRIBUTE_UNUSED;
846 static const char *ix86_strip_name_encoding PARAMS ((const char *))
847      ATTRIBUTE_UNUSED;
848
849 struct builtin_description;
850 static rtx ix86_expand_sse_comi PARAMS ((const struct builtin_description *,
851                                          tree, rtx));
852 static rtx ix86_expand_sse_compare PARAMS ((const struct builtin_description *,
853                                             tree, rtx));
854 static rtx ix86_expand_unop1_builtin PARAMS ((enum insn_code, tree, rtx));
855 static rtx ix86_expand_unop_builtin PARAMS ((enum insn_code, tree, rtx, int));
856 static rtx ix86_expand_binop_builtin PARAMS ((enum insn_code, tree, rtx));
857 static rtx ix86_expand_store_builtin PARAMS ((enum insn_code, tree));
858 static rtx safe_vector_operand PARAMS ((rtx, enum machine_mode));
859 static enum rtx_code ix86_fp_compare_code_to_integer PARAMS ((enum rtx_code));
860 static void ix86_fp_comparison_codes PARAMS ((enum rtx_code code,
861                                               enum rtx_code *,
862                                               enum rtx_code *,
863                                               enum rtx_code *));
864 static rtx ix86_expand_fp_compare PARAMS ((enum rtx_code, rtx, rtx, rtx,
865                                           rtx *, rtx *));
866 static int ix86_fp_comparison_arithmetics_cost PARAMS ((enum rtx_code code));
867 static int ix86_fp_comparison_fcomi_cost PARAMS ((enum rtx_code code));
868 static int ix86_fp_comparison_sahf_cost PARAMS ((enum rtx_code code));
869 static int ix86_fp_comparison_cost PARAMS ((enum rtx_code code));
870 static unsigned int ix86_select_alt_pic_regnum PARAMS ((void));
871 static int ix86_save_reg PARAMS ((unsigned int, int));
872 static void ix86_compute_frame_layout PARAMS ((struct ix86_frame *));
873 static int ix86_comp_type_attributes PARAMS ((tree, tree));
874 static int ix86_fntype_regparm PARAMS ((tree));
875 const struct attribute_spec ix86_attribute_table[];
876 static bool ix86_function_ok_for_sibcall PARAMS ((tree, tree));
877 static tree ix86_handle_cdecl_attribute PARAMS ((tree *, tree, tree, int, bool *));
878 static tree ix86_handle_regparm_attribute PARAMS ((tree *, tree, tree, int, bool *));
879 static int ix86_value_regno PARAMS ((enum machine_mode));
880 static bool ix86_ms_bitfield_layout_p PARAMS ((tree));
881 static tree ix86_handle_struct_attribute PARAMS ((tree *, tree, tree, int, bool *));
882 static int extended_reg_mentioned_1 PARAMS ((rtx *, void *));
883 static bool ix86_rtx_costs PARAMS ((rtx, int, int, int *));
884
885 #if defined (DO_GLOBAL_CTORS_BODY) && defined (HAS_INIT_SECTION)
886 static void ix86_svr3_asm_out_constructor PARAMS ((rtx, int));
887 #endif
888
889 /* Register class used for passing given 64bit part of the argument.
890    These represent classes as documented by the PS ABI, with the exception
891    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
892    use SF or DFmode move instead of DImode to avoid reformatting penalties.
893
894    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
895    whenever possible (upper half does contain padding).
896  */
897 enum x86_64_reg_class
898   {
899     X86_64_NO_CLASS,
900     X86_64_INTEGER_CLASS,
901     X86_64_INTEGERSI_CLASS,
902     X86_64_SSE_CLASS,
903     X86_64_SSESF_CLASS,
904     X86_64_SSEDF_CLASS,
905     X86_64_SSEUP_CLASS,
906     X86_64_X87_CLASS,
907     X86_64_X87UP_CLASS,
908     X86_64_MEMORY_CLASS
909   };
910 static const char * const x86_64_reg_class_name[] =
911    {"no", "integer", "integerSI", "sse", "sseSF", "sseDF", "sseup", "x87", "x87up", "no"};
912
913 #define MAX_CLASSES 4
914 static int classify_argument PARAMS ((enum machine_mode, tree,
915                                       enum x86_64_reg_class [MAX_CLASSES],
916                                       int));
917 static int examine_argument PARAMS ((enum machine_mode, tree, int, int *,
918                                      int *));
919 static rtx construct_container PARAMS ((enum machine_mode, tree, int, int, int,
920                                         const int *, int));
921 static enum x86_64_reg_class merge_classes PARAMS ((enum x86_64_reg_class,
922                                                     enum x86_64_reg_class));
923 \f
924 /* Initialize the GCC target structure.  */
925 #undef TARGET_ATTRIBUTE_TABLE
926 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
927 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
928 #  undef TARGET_MERGE_DECL_ATTRIBUTES
929 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
930 #endif
931
932 #undef TARGET_COMP_TYPE_ATTRIBUTES
933 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
934
935 #undef TARGET_INIT_BUILTINS
936 #define TARGET_INIT_BUILTINS ix86_init_builtins
937
938 #undef TARGET_EXPAND_BUILTIN
939 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
940
941 #undef TARGET_ASM_FUNCTION_EPILOGUE
942 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
943
944 #undef TARGET_ASM_OPEN_PAREN
945 #define TARGET_ASM_OPEN_PAREN ""
946 #undef TARGET_ASM_CLOSE_PAREN
947 #define TARGET_ASM_CLOSE_PAREN ""
948
949 #undef TARGET_ASM_ALIGNED_HI_OP
950 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
951 #undef TARGET_ASM_ALIGNED_SI_OP
952 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
953 #ifdef ASM_QUAD
954 #undef TARGET_ASM_ALIGNED_DI_OP
955 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
956 #endif
957
958 #undef TARGET_ASM_UNALIGNED_HI_OP
959 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
960 #undef TARGET_ASM_UNALIGNED_SI_OP
961 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
962 #undef TARGET_ASM_UNALIGNED_DI_OP
963 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
964
965 #undef TARGET_SCHED_ADJUST_COST
966 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
967 #undef TARGET_SCHED_ISSUE_RATE
968 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
969 #undef TARGET_SCHED_VARIABLE_ISSUE
970 #define TARGET_SCHED_VARIABLE_ISSUE ix86_variable_issue
971 #undef TARGET_SCHED_INIT
972 #define TARGET_SCHED_INIT ix86_sched_init
973 #undef TARGET_SCHED_REORDER
974 #define TARGET_SCHED_REORDER ix86_sched_reorder
975 #undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE
976 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE \
977   ia32_use_dfa_pipeline_interface
978 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
979 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
980   ia32_multipass_dfa_lookahead
981
982 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
983 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
984
985 #ifdef HAVE_AS_TLS
986 #undef TARGET_HAVE_TLS
987 #define TARGET_HAVE_TLS true
988 #endif
989 #undef TARGET_CANNOT_FORCE_CONST_MEM
990 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
991
992 #undef TARGET_MS_BITFIELD_LAYOUT_P
993 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
994
995 #undef TARGET_ASM_OUTPUT_MI_THUNK
996 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
997 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
998 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
999
1000 #undef TARGET_RTX_COSTS
1001 #define TARGET_RTX_COSTS ix86_rtx_costs
1002
1003 struct gcc_target targetm = TARGET_INITIALIZER;
1004 \f
1005 /* Sometimes certain combinations of command options do not make
1006    sense on a particular target machine.  You can define a macro
1007    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
1008    defined, is executed once just after all the command options have
1009    been parsed.
1010
1011    Don't use this macro to turn on various extra optimizations for
1012    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
1013
1014 void
1015 override_options ()
1016 {
1017   int i;
1018   /* Comes from final.c -- no real reason to change it.  */
1019 #define MAX_CODE_ALIGN 16
1020
1021   static struct ptt
1022     {
1023       const struct processor_costs *cost;       /* Processor costs */
1024       const int target_enable;                  /* Target flags to enable.  */
1025       const int target_disable;                 /* Target flags to disable.  */
1026       const int align_loop;                     /* Default alignments.  */
1027       const int align_loop_max_skip;
1028       const int align_jump;
1029       const int align_jump_max_skip;
1030       const int align_func;
1031     }
1032   const processor_target_table[PROCESSOR_max] =
1033     {
1034       {&i386_cost, 0, 0, 4, 3, 4, 3, 4},
1035       {&i486_cost, 0, 0, 16, 15, 16, 15, 16},
1036       {&pentium_cost, 0, 0, 16, 7, 16, 7, 16},
1037       {&pentiumpro_cost, 0, 0, 16, 15, 16, 7, 16},
1038       {&k6_cost, 0, 0, 32, 7, 32, 7, 32},
1039       {&athlon_cost, 0, 0, 16, 7, 16, 7, 16},
1040       {&pentium4_cost, 0, 0, 0, 0, 0, 0, 0},
1041       {&k8_cost, 0, 0, 16, 7, 16, 7, 16}
1042     };
1043
1044   static const char * const cpu_names[] = TARGET_CPU_DEFAULT_NAMES;
1045   static struct pta
1046     {
1047       const char *const name;           /* processor name or nickname.  */
1048       const enum processor_type processor;
1049       const enum pta_flags
1050         {
1051           PTA_SSE = 1,
1052           PTA_SSE2 = 2,
1053           PTA_MMX = 4,
1054           PTA_PREFETCH_SSE = 8,
1055           PTA_3DNOW = 16,
1056           PTA_3DNOW_A = 64,
1057           PTA_64BIT = 128
1058         } flags;
1059     }
1060   const processor_alias_table[] =
1061     {
1062       {"i386", PROCESSOR_I386, 0},
1063       {"i486", PROCESSOR_I486, 0},
1064       {"i586", PROCESSOR_PENTIUM, 0},
1065       {"pentium", PROCESSOR_PENTIUM, 0},
1066       {"pentium-mmx", PROCESSOR_PENTIUM, PTA_MMX},
1067       {"winchip-c6", PROCESSOR_I486, PTA_MMX},
1068       {"winchip2", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1069       {"c3", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1070       {"i686", PROCESSOR_PENTIUMPRO, 0},
1071       {"pentiumpro", PROCESSOR_PENTIUMPRO, 0},
1072       {"pentium2", PROCESSOR_PENTIUMPRO, PTA_MMX},
1073       {"pentium3", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1074       {"pentium4", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2 |
1075                                        PTA_MMX | PTA_PREFETCH_SSE},
1076       {"k6", PROCESSOR_K6, PTA_MMX},
1077       {"k6-2", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1078       {"k6-3", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1079       {"athlon", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1080                                    | PTA_3DNOW_A},
1081       {"athlon-tbird", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE
1082                                          | PTA_3DNOW | PTA_3DNOW_A},
1083       {"athlon-4", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1084                                     | PTA_3DNOW_A | PTA_SSE},
1085       {"athlon-xp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1086                                       | PTA_3DNOW_A | PTA_SSE},
1087       {"athlon-mp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1088                                       | PTA_3DNOW_A | PTA_SSE},
1089       {"k8", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1090                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1091     };
1092
1093   int const pta_size = ARRAY_SIZE (processor_alias_table);
1094
1095   /* By default our XFmode is the 80-bit extended format.  If we have
1096      use TFmode instead, it's also the 80-bit format, but with padding.  */
1097   real_format_for_mode[XFmode - QFmode] = &ieee_extended_intel_96_format;
1098   real_format_for_mode[TFmode - QFmode] = &ieee_extended_intel_128_format;
1099
1100   /* Set the default values for switches whose default depends on TARGET_64BIT
1101      in case they weren't overwritten by command line options.  */
1102   if (TARGET_64BIT)
1103     {
1104       if (flag_omit_frame_pointer == 2)
1105         flag_omit_frame_pointer = 1;
1106       if (flag_asynchronous_unwind_tables == 2)
1107         flag_asynchronous_unwind_tables = 1;
1108       if (flag_pcc_struct_return == 2)
1109         flag_pcc_struct_return = 0;
1110     }
1111   else
1112     {
1113       if (flag_omit_frame_pointer == 2)
1114         flag_omit_frame_pointer = 0;
1115       if (flag_asynchronous_unwind_tables == 2)
1116         flag_asynchronous_unwind_tables = 0;
1117       if (flag_pcc_struct_return == 2)
1118         flag_pcc_struct_return = 1;
1119     }
1120
1121 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1122   SUBTARGET_OVERRIDE_OPTIONS;
1123 #endif
1124
1125   if (!ix86_cpu_string && ix86_arch_string)
1126     ix86_cpu_string = ix86_arch_string;
1127   if (!ix86_cpu_string)
1128     ix86_cpu_string = cpu_names [TARGET_CPU_DEFAULT];
1129   if (!ix86_arch_string)
1130     ix86_arch_string = TARGET_64BIT ? "k8" : "i386";
1131
1132   if (ix86_cmodel_string != 0)
1133     {
1134       if (!strcmp (ix86_cmodel_string, "small"))
1135         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1136       else if (flag_pic)
1137         sorry ("code model %s not supported in PIC mode", ix86_cmodel_string);
1138       else if (!strcmp (ix86_cmodel_string, "32"))
1139         ix86_cmodel = CM_32;
1140       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
1141         ix86_cmodel = CM_KERNEL;
1142       else if (!strcmp (ix86_cmodel_string, "medium") && !flag_pic)
1143         ix86_cmodel = CM_MEDIUM;
1144       else if (!strcmp (ix86_cmodel_string, "large") && !flag_pic)
1145         ix86_cmodel = CM_LARGE;
1146       else
1147         error ("bad value (%s) for -mcmodel= switch", ix86_cmodel_string);
1148     }
1149   else
1150     {
1151       ix86_cmodel = CM_32;
1152       if (TARGET_64BIT)
1153         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1154     }
1155   if (ix86_asm_string != 0)
1156     {
1157       if (!strcmp (ix86_asm_string, "intel"))
1158         ix86_asm_dialect = ASM_INTEL;
1159       else if (!strcmp (ix86_asm_string, "att"))
1160         ix86_asm_dialect = ASM_ATT;
1161       else
1162         error ("bad value (%s) for -masm= switch", ix86_asm_string);
1163     }
1164   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
1165     error ("code model `%s' not supported in the %s bit mode",
1166            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
1167   if (ix86_cmodel == CM_LARGE)
1168     sorry ("code model `large' not supported yet");
1169   if ((TARGET_64BIT != 0) != ((target_flags & MASK_64BIT) != 0))
1170     sorry ("%i-bit mode not compiled in",
1171            (target_flags & MASK_64BIT) ? 64 : 32);
1172
1173   for (i = 0; i < pta_size; i++)
1174     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
1175       {
1176         ix86_arch = processor_alias_table[i].processor;
1177         /* Default cpu tuning to the architecture.  */
1178         ix86_cpu = ix86_arch;
1179         if (processor_alias_table[i].flags & PTA_MMX
1180             && !(target_flags_explicit & MASK_MMX))
1181           target_flags |= MASK_MMX;
1182         if (processor_alias_table[i].flags & PTA_3DNOW
1183             && !(target_flags_explicit & MASK_3DNOW))
1184           target_flags |= MASK_3DNOW;
1185         if (processor_alias_table[i].flags & PTA_3DNOW_A
1186             && !(target_flags_explicit & MASK_3DNOW_A))
1187           target_flags |= MASK_3DNOW_A;
1188         if (processor_alias_table[i].flags & PTA_SSE
1189             && !(target_flags_explicit & MASK_SSE))
1190           target_flags |= MASK_SSE;
1191         if (processor_alias_table[i].flags & PTA_SSE2
1192             && !(target_flags_explicit & MASK_SSE2))
1193           target_flags |= MASK_SSE2;
1194         if (processor_alias_table[i].flags & PTA_PREFETCH_SSE)
1195           x86_prefetch_sse = true;
1196         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1197           error ("CPU you selected does not support x86-64 instruction set");
1198         break;
1199       }
1200
1201   if (i == pta_size)
1202     error ("bad value (%s) for -march= switch", ix86_arch_string);
1203
1204   for (i = 0; i < pta_size; i++)
1205     if (! strcmp (ix86_cpu_string, processor_alias_table[i].name))
1206       {
1207         ix86_cpu = processor_alias_table[i].processor;
1208         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1209           error ("CPU you selected does not support x86-64 instruction set");
1210         break;
1211       }
1212   if (processor_alias_table[i].flags & PTA_PREFETCH_SSE)
1213     x86_prefetch_sse = true;
1214   if (i == pta_size)
1215     error ("bad value (%s) for -mcpu= switch", ix86_cpu_string);
1216
1217   if (optimize_size)
1218     ix86_cost = &size_cost;
1219   else
1220     ix86_cost = processor_target_table[ix86_cpu].cost;
1221   target_flags |= processor_target_table[ix86_cpu].target_enable;
1222   target_flags &= ~processor_target_table[ix86_cpu].target_disable;
1223
1224   /* Arrange to set up i386_stack_locals for all functions.  */
1225   init_machine_status = ix86_init_machine_status;
1226
1227   /* Validate -mregparm= value.  */
1228   if (ix86_regparm_string)
1229     {
1230       i = atoi (ix86_regparm_string);
1231       if (i < 0 || i > REGPARM_MAX)
1232         error ("-mregparm=%d is not between 0 and %d", i, REGPARM_MAX);
1233       else
1234         ix86_regparm = i;
1235     }
1236   else
1237    if (TARGET_64BIT)
1238      ix86_regparm = REGPARM_MAX;
1239
1240   /* If the user has provided any of the -malign-* options,
1241      warn and use that value only if -falign-* is not set.
1242      Remove this code in GCC 3.2 or later.  */
1243   if (ix86_align_loops_string)
1244     {
1245       warning ("-malign-loops is obsolete, use -falign-loops");
1246       if (align_loops == 0)
1247         {
1248           i = atoi (ix86_align_loops_string);
1249           if (i < 0 || i > MAX_CODE_ALIGN)
1250             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1251           else
1252             align_loops = 1 << i;
1253         }
1254     }
1255
1256   if (ix86_align_jumps_string)
1257     {
1258       warning ("-malign-jumps is obsolete, use -falign-jumps");
1259       if (align_jumps == 0)
1260         {
1261           i = atoi (ix86_align_jumps_string);
1262           if (i < 0 || i > MAX_CODE_ALIGN)
1263             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1264           else
1265             align_jumps = 1 << i;
1266         }
1267     }
1268
1269   if (ix86_align_funcs_string)
1270     {
1271       warning ("-malign-functions is obsolete, use -falign-functions");
1272       if (align_functions == 0)
1273         {
1274           i = atoi (ix86_align_funcs_string);
1275           if (i < 0 || i > MAX_CODE_ALIGN)
1276             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1277           else
1278             align_functions = 1 << i;
1279         }
1280     }
1281
1282   /* Default align_* from the processor table.  */
1283   if (align_loops == 0)
1284     {
1285       align_loops = processor_target_table[ix86_cpu].align_loop;
1286       align_loops_max_skip = processor_target_table[ix86_cpu].align_loop_max_skip;
1287     }
1288   if (align_jumps == 0)
1289     {
1290       align_jumps = processor_target_table[ix86_cpu].align_jump;
1291       align_jumps_max_skip = processor_target_table[ix86_cpu].align_jump_max_skip;
1292     }
1293   if (align_functions == 0)
1294     {
1295       align_functions = processor_target_table[ix86_cpu].align_func;
1296     }
1297
1298   /* Validate -mpreferred-stack-boundary= value, or provide default.
1299      The default of 128 bits is for Pentium III's SSE __m128, but we
1300      don't want additional code to keep the stack aligned when
1301      optimizing for code size.  */
1302   ix86_preferred_stack_boundary = (optimize_size
1303                                    ? TARGET_64BIT ? 128 : 32
1304                                    : 128);
1305   if (ix86_preferred_stack_boundary_string)
1306     {
1307       i = atoi (ix86_preferred_stack_boundary_string);
1308       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
1309         error ("-mpreferred-stack-boundary=%d is not between %d and 12", i,
1310                TARGET_64BIT ? 4 : 2);
1311       else
1312         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
1313     }
1314
1315   /* Validate -mbranch-cost= value, or provide default.  */
1316   ix86_branch_cost = processor_target_table[ix86_cpu].cost->branch_cost;
1317   if (ix86_branch_cost_string)
1318     {
1319       i = atoi (ix86_branch_cost_string);
1320       if (i < 0 || i > 5)
1321         error ("-mbranch-cost=%d is not between 0 and 5", i);
1322       else
1323         ix86_branch_cost = i;
1324     }
1325
1326   if (ix86_tls_dialect_string)
1327     {
1328       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
1329         ix86_tls_dialect = TLS_DIALECT_GNU;
1330       else if (strcmp (ix86_tls_dialect_string, "sun") == 0)
1331         ix86_tls_dialect = TLS_DIALECT_SUN;
1332       else
1333         error ("bad value (%s) for -mtls-dialect= switch",
1334                ix86_tls_dialect_string);
1335     }
1336
1337   /* Keep nonleaf frame pointers.  */
1338   if (TARGET_OMIT_LEAF_FRAME_POINTER)
1339     flag_omit_frame_pointer = 1;
1340
1341   /* If we're doing fast math, we don't care about comparison order
1342      wrt NaNs.  This lets us use a shorter comparison sequence.  */
1343   if (flag_unsafe_math_optimizations)
1344     target_flags &= ~MASK_IEEE_FP;
1345
1346   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
1347      since the insns won't need emulation.  */
1348   if (x86_arch_always_fancy_math_387 & (1 << ix86_arch))
1349     target_flags &= ~MASK_NO_FANCY_MATH_387;
1350
1351   if (TARGET_64BIT)
1352     {
1353       if (TARGET_ALIGN_DOUBLE)
1354         error ("-malign-double makes no sense in the 64bit mode");
1355       if (TARGET_RTD)
1356         error ("-mrtd calling convention not supported in the 64bit mode");
1357       /* Enable by default the SSE and MMX builtins.  */
1358       target_flags |= (MASK_SSE2 | MASK_SSE | MASK_MMX | MASK_128BIT_LONG_DOUBLE);
1359       ix86_fpmath = FPMATH_SSE;
1360      }
1361   else
1362     ix86_fpmath = FPMATH_387;
1363
1364   if (ix86_fpmath_string != 0)
1365     {
1366       if (! strcmp (ix86_fpmath_string, "387"))
1367         ix86_fpmath = FPMATH_387;
1368       else if (! strcmp (ix86_fpmath_string, "sse"))
1369         {
1370           if (!TARGET_SSE)
1371             {
1372               warning ("SSE instruction set disabled, using 387 arithmetics");
1373               ix86_fpmath = FPMATH_387;
1374             }
1375           else
1376             ix86_fpmath = FPMATH_SSE;
1377         }
1378       else if (! strcmp (ix86_fpmath_string, "387,sse")
1379                || ! strcmp (ix86_fpmath_string, "sse,387"))
1380         {
1381           if (!TARGET_SSE)
1382             {
1383               warning ("SSE instruction set disabled, using 387 arithmetics");
1384               ix86_fpmath = FPMATH_387;
1385             }
1386           else if (!TARGET_80387)
1387             {
1388               warning ("387 instruction set disabled, using SSE arithmetics");
1389               ix86_fpmath = FPMATH_SSE;
1390             }
1391           else
1392             ix86_fpmath = FPMATH_SSE | FPMATH_387;
1393         }
1394       else
1395         error ("bad value (%s) for -mfpmath= switch", ix86_fpmath_string);
1396     }
1397
1398   /* It makes no sense to ask for just SSE builtins, so MMX is also turned
1399      on by -msse.  */
1400   if (TARGET_SSE)
1401     {
1402       target_flags |= MASK_MMX;
1403       x86_prefetch_sse = true;
1404     }
1405
1406   /* If it has 3DNow! it also has MMX so MMX is also turned on by -m3dnow */
1407   if (TARGET_3DNOW)
1408     {
1409       target_flags |= MASK_MMX;
1410       /* If we are targeting the Athlon architecture, enable the 3Dnow/MMX
1411          extensions it adds.  */
1412       if (x86_3dnow_a & (1 << ix86_arch))
1413         target_flags |= MASK_3DNOW_A;
1414     }
1415   if ((x86_accumulate_outgoing_args & CPUMASK)
1416       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
1417       && !optimize_size)
1418     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
1419
1420   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
1421   {
1422     char *p;
1423     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
1424     p = strchr (internal_label_prefix, 'X');
1425     internal_label_prefix_len = p - internal_label_prefix;
1426     *p = '\0';
1427   }
1428 }
1429 \f
1430 void
1431 optimization_options (level, size)
1432      int level;
1433      int size ATTRIBUTE_UNUSED;
1434 {
1435   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
1436      make the problem with not enough registers even worse.  */
1437 #ifdef INSN_SCHEDULING
1438   if (level > 1)
1439     flag_schedule_insns = 0;
1440 #endif
1441
1442   /* The default values of these switches depend on the TARGET_64BIT
1443      that is not known at this moment.  Mark these values with 2 and
1444      let user the to override these.  In case there is no command line option
1445      specifying them, we will set the defaults in override_options.  */
1446   if (optimize >= 1)
1447     flag_omit_frame_pointer = 2;
1448   flag_pcc_struct_return = 2;
1449   flag_asynchronous_unwind_tables = 2;
1450 }
1451 \f
1452 /* Table of valid machine attributes.  */
1453 const struct attribute_spec ix86_attribute_table[] =
1454 {
1455   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
1456   /* Stdcall attribute says callee is responsible for popping arguments
1457      if they are not variable.  */
1458   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cdecl_attribute },
1459   /* Fastcall attribute says callee is responsible for popping arguments
1460      if they are not variable.  */
1461   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cdecl_attribute },
1462   /* Cdecl attribute says the callee is a normal C declaration */
1463   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cdecl_attribute },
1464   /* Regparm attribute specifies how many integer arguments are to be
1465      passed in registers.  */
1466   { "regparm",   1, 1, false, true,  true,  ix86_handle_regparm_attribute },
1467 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
1468   { "dllimport", 0, 0, false, false, false, ix86_handle_dll_attribute },
1469   { "dllexport", 0, 0, false, false, false, ix86_handle_dll_attribute },
1470   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
1471 #endif
1472   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
1473   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
1474   { NULL,        0, 0, false, false, false, NULL }
1475 };
1476
1477 /* If PIC, we cannot make sibling calls to global functions
1478    because the PLT requires %ebx live.
1479    If we are returning floats on the register stack, we cannot make
1480    sibling calls to functions that return floats.  (The stack adjust
1481    instruction will wind up after the sibcall jump, and not be executed.)  */
1482
1483 static bool
1484 ix86_function_ok_for_sibcall (decl, exp)
1485      tree decl;
1486      tree exp;
1487 {
1488   /* If we are generating position-independent code, we cannot sibcall
1489      optimize any indirect call, or a direct call to a global function,
1490      as the PLT requires %ebx be live.  */
1491   if (!TARGET_64BIT && flag_pic && (!decl || TREE_PUBLIC (decl)))
1492     return false;
1493
1494   /* If we are returning floats on the 80387 register stack, we cannot
1495      make a sibcall from a function that doesn't return a float to a
1496      function that does; the necessary stack adjustment will not be
1497      executed.  */
1498   if (STACK_REG_P (ix86_function_value (TREE_TYPE (exp)))
1499       && ! STACK_REG_P (ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)))))
1500     return false;
1501
1502   /* If this call is indirect, we'll need to be able to use a call-clobbered
1503      register for the address of the target function.  Make sure that all 
1504      such registers are not used for passing parameters.  */
1505   if (!decl && !TARGET_64BIT)
1506     {
1507       int regparm = ix86_regparm;
1508       tree attr, type;
1509
1510       /* We're looking at the CALL_EXPR, we need the type of the function.  */
1511       type = TREE_OPERAND (exp, 0);             /* pointer expression */
1512       type = TREE_TYPE (type);                  /* pointer type */
1513       type = TREE_TYPE (type);                  /* function type */
1514
1515       attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
1516       if (attr)
1517         regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
1518
1519       if (regparm >= 3)
1520         {
1521           /* ??? Need to count the actual number of registers to be used,
1522              not the possible number of registers.  Fix later.  */
1523           return false;
1524         }
1525     }
1526
1527   /* Otherwise okay.  That also includes certain types of indirect calls.  */
1528   return true;
1529 }
1530
1531 /* Handle a "cdecl", "stdcall", or "fastcall" attribute;
1532    arguments as in struct attribute_spec.handler.  */
1533 static tree
1534 ix86_handle_cdecl_attribute (node, name, args, flags, no_add_attrs)
1535      tree *node;
1536      tree name;
1537      tree args ATTRIBUTE_UNUSED;
1538      int flags ATTRIBUTE_UNUSED;
1539      bool *no_add_attrs;
1540 {
1541   if (TREE_CODE (*node) != FUNCTION_TYPE
1542       && TREE_CODE (*node) != METHOD_TYPE
1543       && TREE_CODE (*node) != FIELD_DECL
1544       && TREE_CODE (*node) != TYPE_DECL)
1545     {
1546       warning ("`%s' attribute only applies to functions",
1547                IDENTIFIER_POINTER (name));
1548       *no_add_attrs = true;
1549     }
1550   else
1551     {
1552       if (is_attribute_p ("fastcall", name))
1553         {
1554           if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
1555             {
1556               error ("fastcall and stdcall attributes are not compatible");
1557             }
1558            else if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
1559             {
1560               error ("fastcall and regparm attributes are not compatible");
1561             }
1562         }
1563       else if (is_attribute_p ("stdcall", name))
1564         {
1565           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
1566             {
1567               error ("fastcall and stdcall attributes are not compatible");
1568             }
1569         }
1570     }
1571
1572   if (TARGET_64BIT)
1573     {
1574       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1575       *no_add_attrs = true;
1576     }
1577
1578   return NULL_TREE;
1579 }
1580
1581 /* Handle a "regparm" attribute;
1582    arguments as in struct attribute_spec.handler.  */
1583 static tree
1584 ix86_handle_regparm_attribute (node, name, args, flags, no_add_attrs)
1585      tree *node;
1586      tree name;
1587      tree args;
1588      int flags ATTRIBUTE_UNUSED;
1589      bool *no_add_attrs;
1590 {
1591   if (TREE_CODE (*node) != FUNCTION_TYPE
1592       && TREE_CODE (*node) != METHOD_TYPE
1593       && TREE_CODE (*node) != FIELD_DECL
1594       && TREE_CODE (*node) != TYPE_DECL)
1595     {
1596       warning ("`%s' attribute only applies to functions",
1597                IDENTIFIER_POINTER (name));
1598       *no_add_attrs = true;
1599     }
1600   else
1601     {
1602       tree cst;
1603
1604       cst = TREE_VALUE (args);
1605       if (TREE_CODE (cst) != INTEGER_CST)
1606         {
1607           warning ("`%s' attribute requires an integer constant argument",
1608                    IDENTIFIER_POINTER (name));
1609           *no_add_attrs = true;
1610         }
1611       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
1612         {
1613           warning ("argument to `%s' attribute larger than %d",
1614                    IDENTIFIER_POINTER (name), REGPARM_MAX);
1615           *no_add_attrs = true;
1616         }
1617
1618       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
1619     {
1620       error ("fastcall and regparm attributes are not compatible");
1621     }
1622     }
1623
1624   return NULL_TREE;
1625 }
1626
1627 /* Return 0 if the attributes for two types are incompatible, 1 if they
1628    are compatible, and 2 if they are nearly compatible (which causes a
1629    warning to be generated).  */
1630
1631 static int
1632 ix86_comp_type_attributes (type1, type2)
1633      tree type1;
1634      tree type2;
1635 {
1636   /* Check for mismatch of non-default calling convention.  */
1637   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
1638
1639   if (TREE_CODE (type1) != FUNCTION_TYPE)
1640     return 1;
1641
1642   /*  Check for mismatched fastcall types */ 
1643   if (!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
1644       != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
1645     return 0; 
1646
1647   /* Check for mismatched return types (cdecl vs stdcall).  */
1648   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
1649       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
1650     return 0;
1651   return 1;
1652 }
1653 \f
1654 /* Return the regparm value for a fuctio with the indicated TYPE.  */
1655
1656 static int
1657 ix86_fntype_regparm (type)
1658      tree type;
1659 {
1660   tree attr;
1661
1662   attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
1663   if (attr)
1664     return TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
1665   else
1666     return ix86_regparm;
1667 }
1668
1669 /* Value is the number of bytes of arguments automatically
1670    popped when returning from a subroutine call.
1671    FUNDECL is the declaration node of the function (as a tree),
1672    FUNTYPE is the data type of the function (as a tree),
1673    or for a library call it is an identifier node for the subroutine name.
1674    SIZE is the number of bytes of arguments passed on the stack.
1675
1676    On the 80386, the RTD insn may be used to pop them if the number
1677      of args is fixed, but if the number is variable then the caller
1678      must pop them all.  RTD can't be used for library calls now
1679      because the library is compiled with the Unix compiler.
1680    Use of RTD is a selectable option, since it is incompatible with
1681    standard Unix calling sequences.  If the option is not selected,
1682    the caller must always pop the args.
1683
1684    The attribute stdcall is equivalent to RTD on a per module basis.  */
1685
1686 int
1687 ix86_return_pops_args (fundecl, funtype, size)
1688      tree fundecl;
1689      tree funtype;
1690      int size;
1691 {
1692   int rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
1693
1694     /* Cdecl functions override -mrtd, and never pop the stack.  */
1695   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype))) {
1696
1697     /* Stdcall and fastcall functions will pop the stack if not variable args. */
1698     if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
1699         || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype)))
1700       rtd = 1;
1701
1702     if (rtd
1703         && (TYPE_ARG_TYPES (funtype) == NULL_TREE
1704             || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (funtype)))
1705                 == void_type_node)))
1706       return size;
1707   }
1708
1709   /* Lose any fake structure return argument if it is passed on the stack.  */
1710   if (aggregate_value_p (TREE_TYPE (funtype))
1711       && !TARGET_64BIT)
1712     {
1713       int nregs = ix86_fntype_regparm (funtype);
1714
1715       if (!nregs)
1716         return GET_MODE_SIZE (Pmode);
1717     }
1718
1719   return 0;
1720 }
1721 \f
1722 /* Argument support functions.  */
1723
1724 /* Return true when register may be used to pass function parameters.  */
1725 bool
1726 ix86_function_arg_regno_p (regno)
1727      int regno;
1728 {
1729   int i;
1730   if (!TARGET_64BIT)
1731     return (regno < REGPARM_MAX
1732             || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
1733   if (SSE_REGNO_P (regno) && TARGET_SSE)
1734     return true;
1735   /* RAX is used as hidden argument to va_arg functions.  */
1736   if (!regno)
1737     return true;
1738   for (i = 0; i < REGPARM_MAX; i++)
1739     if (regno == x86_64_int_parameter_registers[i])
1740       return true;
1741   return false;
1742 }
1743
1744 /* Initialize a variable CUM of type CUMULATIVE_ARGS
1745    for a call to a function whose data type is FNTYPE.
1746    For a library call, FNTYPE is 0.  */
1747
1748 void
1749 init_cumulative_args (cum, fntype, libname)
1750      CUMULATIVE_ARGS *cum;      /* Argument info to initialize */
1751      tree fntype;               /* tree ptr for function decl */
1752      rtx libname;               /* SYMBOL_REF of library name or 0 */
1753 {
1754   static CUMULATIVE_ARGS zero_cum;
1755   tree param, next_param;
1756
1757   if (TARGET_DEBUG_ARG)
1758     {
1759       fprintf (stderr, "\ninit_cumulative_args (");
1760       if (fntype)
1761         fprintf (stderr, "fntype code = %s, ret code = %s",
1762                  tree_code_name[(int) TREE_CODE (fntype)],
1763                  tree_code_name[(int) TREE_CODE (TREE_TYPE (fntype))]);
1764       else
1765         fprintf (stderr, "no fntype");
1766
1767       if (libname)
1768         fprintf (stderr, ", libname = %s", XSTR (libname, 0));
1769     }
1770
1771   *cum = zero_cum;
1772
1773   /* Set up the number of registers to use for passing arguments.  */
1774   cum->nregs = ix86_regparm;
1775   cum->sse_nregs = SSE_REGPARM_MAX;
1776   if (fntype && !TARGET_64BIT)
1777     {
1778       tree attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (fntype));
1779
1780       if (attr)
1781         cum->nregs = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
1782     }
1783   cum->maybe_vaarg = false;
1784
1785   /* Use ecx and edx registers if function has fastcall attribute */
1786   if (fntype && !TARGET_64BIT)
1787     {
1788       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
1789         {
1790           cum->nregs = 2;
1791           cum->fastcall = 1;
1792         }
1793     }
1794
1795
1796   /* Determine if this function has variable arguments.  This is
1797      indicated by the last argument being 'void_type_mode' if there
1798      are no variable arguments.  If there are variable arguments, then
1799      we won't pass anything in registers */
1800
1801   if (cum->nregs)
1802     {
1803       for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
1804            param != 0; param = next_param)
1805         {
1806           next_param = TREE_CHAIN (param);
1807           if (next_param == 0 && TREE_VALUE (param) != void_type_node)
1808             {
1809               if (!TARGET_64BIT)
1810                 {
1811                   cum->nregs = 0;
1812                   cum->fastcall = 0;
1813                 }
1814               cum->maybe_vaarg = true;
1815             }
1816         }
1817     }
1818   if ((!fntype && !libname)
1819       || (fntype && !TYPE_ARG_TYPES (fntype)))
1820     cum->maybe_vaarg = 1;
1821
1822   if (TARGET_DEBUG_ARG)
1823     fprintf (stderr, ", nregs=%d )\n", cum->nregs);
1824
1825   return;
1826 }
1827
1828 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
1829    of this code is to classify each 8bytes of incoming argument by the register
1830    class and assign registers accordingly.  */
1831
1832 /* Return the union class of CLASS1 and CLASS2.
1833    See the x86-64 PS ABI for details.  */
1834
1835 static enum x86_64_reg_class
1836 merge_classes (class1, class2)
1837      enum x86_64_reg_class class1, class2;
1838 {
1839   /* Rule #1: If both classes are equal, this is the resulting class.  */
1840   if (class1 == class2)
1841     return class1;
1842
1843   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
1844      the other class.  */
1845   if (class1 == X86_64_NO_CLASS)
1846     return class2;
1847   if (class2 == X86_64_NO_CLASS)
1848     return class1;
1849
1850   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
1851   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
1852     return X86_64_MEMORY_CLASS;
1853
1854   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
1855   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
1856       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
1857     return X86_64_INTEGERSI_CLASS;
1858   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
1859       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
1860     return X86_64_INTEGER_CLASS;
1861
1862   /* Rule #5: If one of the classes is X87 or X87UP class, MEMORY is used.  */
1863   if (class1 == X86_64_X87_CLASS || class1 == X86_64_X87UP_CLASS
1864       || class2 == X86_64_X87_CLASS || class2 == X86_64_X87UP_CLASS)
1865     return X86_64_MEMORY_CLASS;
1866
1867   /* Rule #6: Otherwise class SSE is used.  */
1868   return X86_64_SSE_CLASS;
1869 }
1870
1871 /* Classify the argument of type TYPE and mode MODE.
1872    CLASSES will be filled by the register class used to pass each word
1873    of the operand.  The number of words is returned.  In case the parameter
1874    should be passed in memory, 0 is returned. As a special case for zero
1875    sized containers, classes[0] will be NO_CLASS and 1 is returned.
1876
1877    BIT_OFFSET is used internally for handling records and specifies offset
1878    of the offset in bits modulo 256 to avoid overflow cases.
1879
1880    See the x86-64 PS ABI for details.
1881 */
1882
1883 static int
1884 classify_argument (mode, type, classes, bit_offset)
1885      enum machine_mode mode;
1886      tree type;
1887      enum x86_64_reg_class classes[MAX_CLASSES];
1888      int bit_offset;
1889 {
1890   int bytes =
1891     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
1892   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1893
1894   /* Variable sized entities are always passed/returned in memory.  */
1895   if (bytes < 0)
1896     return 0;
1897
1898   if (type && AGGREGATE_TYPE_P (type))
1899     {
1900       int i;
1901       tree field;
1902       enum x86_64_reg_class subclasses[MAX_CLASSES];
1903
1904       /* On x86-64 we pass structures larger than 16 bytes on the stack.  */
1905       if (bytes > 16)
1906         return 0;
1907
1908       for (i = 0; i < words; i++)
1909         classes[i] = X86_64_NO_CLASS;
1910
1911       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
1912          signalize memory class, so handle it as special case.  */
1913       if (!words)
1914         {
1915           classes[0] = X86_64_NO_CLASS;
1916           return 1;
1917         }
1918
1919       /* Classify each field of record and merge classes.  */
1920       if (TREE_CODE (type) == RECORD_TYPE)
1921         {
1922           /* For classes first merge in the field of the subclasses.  */
1923           if (TYPE_BINFO (type) != NULL && TYPE_BINFO_BASETYPES (type) != NULL)
1924             {
1925               tree bases = TYPE_BINFO_BASETYPES (type);
1926               int n_bases = TREE_VEC_LENGTH (bases);
1927               int i;
1928
1929               for (i = 0; i < n_bases; ++i)
1930                 {
1931                    tree binfo = TREE_VEC_ELT (bases, i);
1932                    int num;
1933                    int offset = tree_low_cst (BINFO_OFFSET (binfo), 0) * 8;
1934                    tree type = BINFO_TYPE (binfo);
1935
1936                    num = classify_argument (TYPE_MODE (type),
1937                                             type, subclasses,
1938                                             (offset + bit_offset) % 256);
1939                    if (!num)
1940                      return 0;
1941                    for (i = 0; i < num; i++)
1942                      {
1943                        int pos = (offset + (bit_offset % 64)) / 8 / 8;
1944                        classes[i + pos] =
1945                          merge_classes (subclasses[i], classes[i + pos]);
1946                      }
1947                 }
1948             }
1949           /* And now merge the fields of structure.   */
1950           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1951             {
1952               if (TREE_CODE (field) == FIELD_DECL)
1953                 {
1954                   int num;
1955
1956                   /* Bitfields are always classified as integer.  Handle them
1957                      early, since later code would consider them to be
1958                      misaligned integers.  */
1959                   if (DECL_BIT_FIELD (field))
1960                     {
1961                       for (i = int_bit_position (field) / 8 / 8;
1962                            i < (int_bit_position (field)
1963                                 + tree_low_cst (DECL_SIZE (field), 0)
1964                                 + 63) / 8 / 8; i++)
1965                         classes[i] =
1966                           merge_classes (X86_64_INTEGER_CLASS,
1967                                          classes[i]);
1968                     }
1969                   else
1970                     {
1971                       num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
1972                                                TREE_TYPE (field), subclasses,
1973                                                (int_bit_position (field)
1974                                                 + bit_offset) % 256);
1975                       if (!num)
1976                         return 0;
1977                       for (i = 0; i < num; i++)
1978                         {
1979                           int pos =
1980                             (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
1981                           classes[i + pos] =
1982                             merge_classes (subclasses[i], classes[i + pos]);
1983                         }
1984                     }
1985                 }
1986             }
1987         }
1988       /* Arrays are handled as small records.  */
1989       else if (TREE_CODE (type) == ARRAY_TYPE)
1990         {
1991           int num;
1992           num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
1993                                    TREE_TYPE (type), subclasses, bit_offset);
1994           if (!num)
1995             return 0;
1996
1997           /* The partial classes are now full classes.  */
1998           if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
1999             subclasses[0] = X86_64_SSE_CLASS;
2000           if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
2001             subclasses[0] = X86_64_INTEGER_CLASS;
2002
2003           for (i = 0; i < words; i++)
2004             classes[i] = subclasses[i % num];
2005         }
2006       /* Unions are similar to RECORD_TYPE but offset is always 0.  */
2007       else if (TREE_CODE (type) == UNION_TYPE
2008                || TREE_CODE (type) == QUAL_UNION_TYPE)
2009         {
2010           /* For classes first merge in the field of the subclasses.  */
2011           if (TYPE_BINFO (type) != NULL && TYPE_BINFO_BASETYPES (type) != NULL)
2012             {
2013               tree bases = TYPE_BINFO_BASETYPES (type);
2014               int n_bases = TREE_VEC_LENGTH (bases);
2015               int i;
2016
2017               for (i = 0; i < n_bases; ++i)
2018                 {
2019                    tree binfo = TREE_VEC_ELT (bases, i);
2020                    int num;
2021                    int offset = tree_low_cst (BINFO_OFFSET (binfo), 0) * 8;
2022                    tree type = BINFO_TYPE (binfo);
2023
2024                    num = classify_argument (TYPE_MODE (type),
2025                                             type, subclasses,
2026                                             (offset + (bit_offset % 64)) % 256);
2027                    if (!num)
2028                      return 0;
2029                    for (i = 0; i < num; i++)
2030                      {
2031                        int pos = (offset + (bit_offset % 64)) / 8 / 8;
2032                        classes[i + pos] =
2033                          merge_classes (subclasses[i], classes[i + pos]);
2034                      }
2035                 }
2036             }
2037           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2038             {
2039               if (TREE_CODE (field) == FIELD_DECL)
2040                 {
2041                   int num;
2042                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
2043                                            TREE_TYPE (field), subclasses,
2044                                            bit_offset);
2045                   if (!num)
2046                     return 0;
2047                   for (i = 0; i < num; i++)
2048                     classes[i] = merge_classes (subclasses[i], classes[i]);
2049                 }
2050             }
2051         }
2052       else
2053         abort ();
2054
2055       /* Final merger cleanup.  */
2056       for (i = 0; i < words; i++)
2057         {
2058           /* If one class is MEMORY, everything should be passed in
2059              memory.  */
2060           if (classes[i] == X86_64_MEMORY_CLASS)
2061             return 0;
2062
2063           /* The X86_64_SSEUP_CLASS should be always preceded by
2064              X86_64_SSE_CLASS.  */
2065           if (classes[i] == X86_64_SSEUP_CLASS
2066               && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
2067             classes[i] = X86_64_SSE_CLASS;
2068
2069           /*  X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS.  */
2070           if (classes[i] == X86_64_X87UP_CLASS
2071               && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
2072             classes[i] = X86_64_SSE_CLASS;
2073         }
2074       return words;
2075     }
2076
2077   /* Compute alignment needed.  We align all types to natural boundaries with
2078      exception of XFmode that is aligned to 64bits.  */
2079   if (mode != VOIDmode && mode != BLKmode)
2080     {
2081       int mode_alignment = GET_MODE_BITSIZE (mode);
2082
2083       if (mode == XFmode)
2084         mode_alignment = 128;
2085       else if (mode == XCmode)
2086         mode_alignment = 256;
2087       /* Misaligned fields are always returned in memory.  */
2088       if (bit_offset % mode_alignment)
2089         return 0;
2090     }
2091
2092   /* Classification of atomic types.  */
2093   switch (mode)
2094     {
2095     case DImode:
2096     case SImode:
2097     case HImode:
2098     case QImode:
2099     case CSImode:
2100     case CHImode:
2101     case CQImode:
2102       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
2103         classes[0] = X86_64_INTEGERSI_CLASS;
2104       else
2105         classes[0] = X86_64_INTEGER_CLASS;
2106       return 1;
2107     case CDImode:
2108     case TImode:
2109       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
2110       return 2;
2111     case CTImode:
2112       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
2113       classes[2] = classes[3] = X86_64_INTEGER_CLASS;
2114       return 4;
2115     case SFmode:
2116       if (!(bit_offset % 64))
2117         classes[0] = X86_64_SSESF_CLASS;
2118       else
2119         classes[0] = X86_64_SSE_CLASS;
2120       return 1;
2121     case DFmode:
2122       classes[0] = X86_64_SSEDF_CLASS;
2123       return 1;
2124     case TFmode:
2125       classes[0] = X86_64_X87_CLASS;
2126       classes[1] = X86_64_X87UP_CLASS;
2127       return 2;
2128     case TCmode:
2129       classes[0] = X86_64_X87_CLASS;
2130       classes[1] = X86_64_X87UP_CLASS;
2131       classes[2] = X86_64_X87_CLASS;
2132       classes[3] = X86_64_X87UP_CLASS;
2133       return 4;
2134     case DCmode:
2135       classes[0] = X86_64_SSEDF_CLASS;
2136       classes[1] = X86_64_SSEDF_CLASS;
2137       return 2;
2138     case SCmode:
2139       classes[0] = X86_64_SSE_CLASS;
2140       return 1;
2141     case V4SFmode:
2142     case V4SImode:
2143     case V16QImode:
2144     case V8HImode:
2145     case V2DFmode:
2146     case V2DImode:
2147       classes[0] = X86_64_SSE_CLASS;
2148       classes[1] = X86_64_SSEUP_CLASS;
2149       return 2;
2150     case V2SFmode:
2151     case V2SImode:
2152     case V4HImode:
2153     case V8QImode:
2154       return 0;
2155     case BLKmode:
2156     case VOIDmode:
2157       return 0;
2158     default:
2159       abort ();
2160     }
2161 }
2162
2163 /* Examine the argument and return set number of register required in each
2164    class.  Return 0 iff parameter should be passed in memory.  */
2165 static int
2166 examine_argument (mode, type, in_return, int_nregs, sse_nregs)
2167      enum machine_mode mode;
2168      tree type;
2169      int *int_nregs, *sse_nregs;
2170      int in_return;
2171 {
2172   enum x86_64_reg_class class[MAX_CLASSES];
2173   int n = classify_argument (mode, type, class, 0);
2174
2175   *int_nregs = 0;
2176   *sse_nregs = 0;
2177   if (!n)
2178     return 0;
2179   for (n--; n >= 0; n--)
2180     switch (class[n])
2181       {
2182       case X86_64_INTEGER_CLASS:
2183       case X86_64_INTEGERSI_CLASS:
2184         (*int_nregs)++;
2185         break;
2186       case X86_64_SSE_CLASS:
2187       case X86_64_SSESF_CLASS:
2188       case X86_64_SSEDF_CLASS:
2189         (*sse_nregs)++;
2190         break;
2191       case X86_64_NO_CLASS:
2192       case X86_64_SSEUP_CLASS:
2193         break;
2194       case X86_64_X87_CLASS:
2195       case X86_64_X87UP_CLASS:
2196         if (!in_return)
2197           return 0;
2198         break;
2199       case X86_64_MEMORY_CLASS:
2200         abort ();
2201       }
2202   return 1;
2203 }
2204 /* Construct container for the argument used by GCC interface.  See
2205    FUNCTION_ARG for the detailed description.  */
2206 static rtx
2207 construct_container (mode, type, in_return, nintregs, nsseregs, intreg, sse_regno)
2208      enum machine_mode mode;
2209      tree type;
2210      int in_return;
2211      int nintregs, nsseregs;
2212      const int * intreg;
2213      int sse_regno;
2214 {
2215   enum machine_mode tmpmode;
2216   int bytes =
2217     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2218   enum x86_64_reg_class class[MAX_CLASSES];
2219   int n;
2220   int i;
2221   int nexps = 0;
2222   int needed_sseregs, needed_intregs;
2223   rtx exp[MAX_CLASSES];
2224   rtx ret;
2225
2226   n = classify_argument (mode, type, class, 0);
2227   if (TARGET_DEBUG_ARG)
2228     {
2229       if (!n)
2230         fprintf (stderr, "Memory class\n");
2231       else
2232         {
2233           fprintf (stderr, "Classes:");
2234           for (i = 0; i < n; i++)
2235             {
2236               fprintf (stderr, " %s", x86_64_reg_class_name[class[i]]);
2237             }
2238            fprintf (stderr, "\n");
2239         }
2240     }
2241   if (!n)
2242     return NULL;
2243   if (!examine_argument (mode, type, in_return, &needed_intregs, &needed_sseregs))
2244     return NULL;
2245   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
2246     return NULL;
2247
2248   /* First construct simple cases.  Avoid SCmode, since we want to use
2249      single register to pass this type.  */
2250   if (n == 1 && mode != SCmode)
2251     switch (class[0])
2252       {
2253       case X86_64_INTEGER_CLASS:
2254       case X86_64_INTEGERSI_CLASS:
2255         return gen_rtx_REG (mode, intreg[0]);
2256       case X86_64_SSE_CLASS:
2257       case X86_64_SSESF_CLASS:
2258       case X86_64_SSEDF_CLASS:
2259         return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
2260       case X86_64_X87_CLASS:
2261         return gen_rtx_REG (mode, FIRST_STACK_REG);
2262       case X86_64_NO_CLASS:
2263         /* Zero sized array, struct or class.  */
2264         return NULL;
2265       default:
2266         abort ();
2267       }
2268   if (n == 2 && class[0] == X86_64_SSE_CLASS && class[1] == X86_64_SSEUP_CLASS)
2269     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
2270   if (n == 2
2271       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS)
2272     return gen_rtx_REG (TFmode, FIRST_STACK_REG);
2273   if (n == 2 && class[0] == X86_64_INTEGER_CLASS
2274       && class[1] == X86_64_INTEGER_CLASS
2275       && (mode == CDImode || mode == TImode)
2276       && intreg[0] + 1 == intreg[1])
2277     return gen_rtx_REG (mode, intreg[0]);
2278   if (n == 4
2279       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS
2280       && class[2] == X86_64_X87_CLASS && class[3] == X86_64_X87UP_CLASS)
2281     return gen_rtx_REG (TCmode, FIRST_STACK_REG);
2282
2283   /* Otherwise figure out the entries of the PARALLEL.  */
2284   for (i = 0; i < n; i++)
2285     {
2286       switch (class[i])
2287         {
2288           case X86_64_NO_CLASS:
2289             break;
2290           case X86_64_INTEGER_CLASS:
2291           case X86_64_INTEGERSI_CLASS:
2292             /* Merge TImodes on aligned occasions here too.  */
2293             if (i * 8 + 8 > bytes)
2294               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
2295             else if (class[i] == X86_64_INTEGERSI_CLASS)
2296               tmpmode = SImode;
2297             else
2298               tmpmode = DImode;
2299             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
2300             if (tmpmode == BLKmode)
2301               tmpmode = DImode;
2302             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2303                                                gen_rtx_REG (tmpmode, *intreg),
2304                                                GEN_INT (i*8));
2305             intreg++;
2306             break;
2307           case X86_64_SSESF_CLASS:
2308             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2309                                                gen_rtx_REG (SFmode,
2310                                                             SSE_REGNO (sse_regno)),
2311                                                GEN_INT (i*8));
2312             sse_regno++;
2313             break;
2314           case X86_64_SSEDF_CLASS:
2315             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2316                                                gen_rtx_REG (DFmode,
2317                                                             SSE_REGNO (sse_regno)),
2318                                                GEN_INT (i*8));
2319             sse_regno++;
2320             break;
2321           case X86_64_SSE_CLASS:
2322             if (i < n - 1 && class[i + 1] == X86_64_SSEUP_CLASS)
2323               tmpmode = TImode;
2324             else
2325               tmpmode = DImode;
2326             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2327                                                gen_rtx_REG (tmpmode,
2328                                                             SSE_REGNO (sse_regno)),
2329                                                GEN_INT (i*8));
2330             if (tmpmode == TImode)
2331               i++;
2332             sse_regno++;
2333             break;
2334           default:
2335             abort ();
2336         }
2337     }
2338   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
2339   for (i = 0; i < nexps; i++)
2340     XVECEXP (ret, 0, i) = exp [i];
2341   return ret;
2342 }
2343
2344 /* Update the data in CUM to advance over an argument
2345    of mode MODE and data type TYPE.
2346    (TYPE is null for libcalls where that information may not be available.)  */
2347
2348 void
2349 function_arg_advance (cum, mode, type, named)
2350      CUMULATIVE_ARGS *cum;      /* current arg information */
2351      enum machine_mode mode;    /* current arg mode */
2352      tree type;                 /* type of the argument or 0 if lib support */
2353      int named;                 /* whether or not the argument was named */
2354 {
2355   int bytes =
2356     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2357   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2358
2359   if (TARGET_DEBUG_ARG)
2360     fprintf (stderr,
2361              "function_adv (sz=%d, wds=%2d, nregs=%d, mode=%s, named=%d)\n\n",
2362              words, cum->words, cum->nregs, GET_MODE_NAME (mode), named);
2363   if (TARGET_64BIT)
2364     {
2365       int int_nregs, sse_nregs;
2366       if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
2367         cum->words += words;
2368       else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
2369         {
2370           cum->nregs -= int_nregs;
2371           cum->sse_nregs -= sse_nregs;
2372           cum->regno += int_nregs;
2373           cum->sse_regno += sse_nregs;
2374         }
2375       else
2376         cum->words += words;
2377     }
2378   else
2379     {
2380       if (TARGET_SSE && mode == TImode)
2381         {
2382           cum->sse_words += words;
2383           cum->sse_nregs -= 1;
2384           cum->sse_regno += 1;
2385           if (cum->sse_nregs <= 0)
2386             {
2387               cum->sse_nregs = 0;
2388               cum->sse_regno = 0;
2389             }
2390         }
2391       else
2392         {
2393           cum->words += words;
2394           cum->nregs -= words;
2395           cum->regno += words;
2396
2397           if (cum->nregs <= 0)
2398             {
2399               cum->nregs = 0;
2400               cum->regno = 0;
2401             }
2402         }
2403     }
2404   return;
2405 }
2406
2407 /* Define where to put the arguments to a function.
2408    Value is zero to push the argument on the stack,
2409    or a hard register in which to store the argument.
2410
2411    MODE is the argument's machine mode.
2412    TYPE is the data type of the argument (as a tree).
2413     This is null for libcalls where that information may
2414     not be available.
2415    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2416     the preceding args and about the function being called.
2417    NAMED is nonzero if this argument is a named parameter
2418     (otherwise it is an extra parameter matching an ellipsis).  */
2419
2420 rtx
2421 function_arg (cum, mode, type, named)
2422      CUMULATIVE_ARGS *cum;      /* current arg information */
2423      enum machine_mode mode;    /* current arg mode */
2424      tree type;                 /* type of the argument or 0 if lib support */
2425      int named;                 /* != 0 for normal args, == 0 for ... args */
2426 {
2427   rtx ret   = NULL_RTX;
2428   int bytes =
2429     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2430   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2431
2432   /* Handle a hidden AL argument containing number of registers for varargs
2433      x86-64 functions.  For i386 ABI just return constm1_rtx to avoid
2434      any AL settings.  */
2435   if (mode == VOIDmode)
2436     {
2437       if (TARGET_64BIT)
2438         return GEN_INT (cum->maybe_vaarg
2439                         ? (cum->sse_nregs < 0
2440                            ? SSE_REGPARM_MAX
2441                            : cum->sse_regno)
2442                         : -1);
2443       else
2444         return constm1_rtx;
2445     }
2446   if (TARGET_64BIT)
2447     ret = construct_container (mode, type, 0, cum->nregs, cum->sse_nregs,
2448                                &x86_64_int_parameter_registers [cum->regno],
2449                                cum->sse_regno);
2450   else
2451     switch (mode)
2452       {
2453         /* For now, pass fp/complex values on the stack.  */
2454       default:
2455         break;
2456
2457       case BLKmode:
2458       case DImode:
2459       case SImode:
2460       case HImode:
2461       case QImode:
2462         if (words <= cum->nregs)
2463           {
2464             int regno = cum->regno;
2465
2466             /* Fastcall allocates the first two DWORD (SImode) or
2467                smaller arguments to ECX and EDX.  */
2468             if (cum->fastcall)
2469               {
2470                 if (mode == BLKmode || mode == DImode)
2471                   break;
2472  
2473                 /* ECX not EAX is the first allocated register.  */
2474                 if (regno == 0)
2475                       regno = 2;
2476               }
2477             ret = gen_rtx_REG (mode, regno);
2478           }
2479         break;
2480       case TImode:
2481         if (cum->sse_nregs)
2482           ret = gen_rtx_REG (mode, cum->sse_regno);
2483         break;
2484       }
2485
2486   if (TARGET_DEBUG_ARG)
2487     {
2488       fprintf (stderr,
2489                "function_arg (size=%d, wds=%2d, nregs=%d, mode=%4s, named=%d, ",
2490                words, cum->words, cum->nregs, GET_MODE_NAME (mode), named);
2491
2492       if (ret)
2493         print_simple_rtl (stderr, ret);
2494       else
2495         fprintf (stderr, ", stack");
2496
2497       fprintf (stderr, " )\n");
2498     }
2499
2500   return ret;
2501 }
2502
2503 /* A C expression that indicates when an argument must be passed by
2504    reference.  If nonzero for an argument, a copy of that argument is
2505    made in memory and a pointer to the argument is passed instead of
2506    the argument itself.  The pointer is passed in whatever way is
2507    appropriate for passing a pointer to that type.  */
2508
2509 int
2510 function_arg_pass_by_reference (cum, mode, type, named)
2511      CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED;
2512      enum machine_mode mode ATTRIBUTE_UNUSED;
2513      tree type;
2514      int named ATTRIBUTE_UNUSED;
2515 {
2516   if (!TARGET_64BIT)
2517     return 0;
2518
2519   if (type && int_size_in_bytes (type) == -1)
2520     {
2521       if (TARGET_DEBUG_ARG)
2522         fprintf (stderr, "function_arg_pass_by_reference\n");
2523       return 1;
2524     }
2525
2526   return 0;
2527 }
2528
2529 /* Gives the alignment boundary, in bits, of an argument with the specified mode
2530    and type.   */
2531
2532 int
2533 ix86_function_arg_boundary (mode, type)
2534      enum machine_mode mode;
2535      tree type;
2536 {
2537   int align;
2538   if (!TARGET_64BIT)
2539     return PARM_BOUNDARY;
2540   if (type)
2541     align = TYPE_ALIGN (type);
2542   else
2543     align = GET_MODE_ALIGNMENT (mode);
2544   if (align < PARM_BOUNDARY)
2545     align = PARM_BOUNDARY;
2546   if (align > 128)
2547     align = 128;
2548   return align;
2549 }
2550
2551 /* Return true if N is a possible register number of function value.  */
2552 bool
2553 ix86_function_value_regno_p (regno)
2554      int regno;
2555 {
2556   if (!TARGET_64BIT)
2557     {
2558       return ((regno) == 0
2559               || ((regno) == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387)
2560               || ((regno) == FIRST_SSE_REG && TARGET_SSE));
2561     }
2562   return ((regno) == 0 || (regno) == FIRST_FLOAT_REG
2563           || ((regno) == FIRST_SSE_REG && TARGET_SSE)
2564           || ((regno) == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387));
2565 }
2566
2567 /* Define how to find the value returned by a function.
2568    VALTYPE is the data type of the value (as a tree).
2569    If the precise function being called is known, FUNC is its FUNCTION_DECL;
2570    otherwise, FUNC is 0.  */
2571 rtx
2572 ix86_function_value (valtype)
2573      tree valtype;
2574 {
2575   if (TARGET_64BIT)
2576     {
2577       rtx ret = construct_container (TYPE_MODE (valtype), valtype, 1,
2578                                      REGPARM_MAX, SSE_REGPARM_MAX,
2579                                      x86_64_int_return_registers, 0);
2580       /* For zero sized structures, construct_container return NULL, but we need
2581          to keep rest of compiler happy by returning meaningful value.  */
2582       if (!ret)
2583         ret = gen_rtx_REG (TYPE_MODE (valtype), 0);
2584       return ret;
2585     }
2586   else
2587     return gen_rtx_REG (TYPE_MODE (valtype),
2588                         ix86_value_regno (TYPE_MODE (valtype)));
2589 }
2590
2591 /* Return false iff type is returned in memory.  */
2592 int
2593 ix86_return_in_memory (type)
2594      tree type;
2595 {
2596   int needed_intregs, needed_sseregs;
2597   if (TARGET_64BIT)
2598     {
2599       return !examine_argument (TYPE_MODE (type), type, 1,
2600                                 &needed_intregs, &needed_sseregs);
2601     }
2602   else
2603     {
2604       if (TYPE_MODE (type) == BLKmode
2605           || (VECTOR_MODE_P (TYPE_MODE (type))
2606               && int_size_in_bytes (type) == 8)
2607           || (int_size_in_bytes (type) > 12 && TYPE_MODE (type) != TImode
2608               && TYPE_MODE (type) != TFmode
2609               && !VECTOR_MODE_P (TYPE_MODE (type))))
2610         return 1;
2611       return 0;
2612     }
2613 }
2614
2615 /* Define how to find the value returned by a library function
2616    assuming the value has mode MODE.  */
2617 rtx
2618 ix86_libcall_value (mode)
2619    enum machine_mode mode;
2620 {
2621   if (TARGET_64BIT)
2622     {
2623       switch (mode)
2624         {
2625           case SFmode:
2626           case SCmode:
2627           case DFmode:
2628           case DCmode:
2629             return gen_rtx_REG (mode, FIRST_SSE_REG);
2630           case TFmode:
2631           case TCmode:
2632             return gen_rtx_REG (mode, FIRST_FLOAT_REG);
2633           default:
2634             return gen_rtx_REG (mode, 0);
2635         }
2636     }
2637   else
2638    return gen_rtx_REG (mode, ix86_value_regno (mode));
2639 }
2640
2641 /* Given a mode, return the register to use for a return value.  */
2642
2643 static int
2644 ix86_value_regno (mode)
2645      enum machine_mode mode;
2646 {
2647   if (GET_MODE_CLASS (mode) == MODE_FLOAT && TARGET_FLOAT_RETURNS_IN_80387)
2648     return FIRST_FLOAT_REG;
2649   if (mode == TImode || VECTOR_MODE_P (mode))
2650     return FIRST_SSE_REG;
2651   return 0;
2652 }
2653 \f
2654 /* Create the va_list data type.  */
2655
2656 tree
2657 ix86_build_va_list ()
2658 {
2659   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
2660
2661   /* For i386 we use plain pointer to argument area.  */
2662   if (!TARGET_64BIT)
2663     return build_pointer_type (char_type_node);
2664
2665   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
2666   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
2667
2668   f_gpr = build_decl (FIELD_DECL, get_identifier ("gp_offset"),
2669                       unsigned_type_node);
2670   f_fpr = build_decl (FIELD_DECL, get_identifier ("fp_offset"),
2671                       unsigned_type_node);
2672   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
2673                       ptr_type_node);
2674   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
2675                       ptr_type_node);
2676
2677   DECL_FIELD_CONTEXT (f_gpr) = record;
2678   DECL_FIELD_CONTEXT (f_fpr) = record;
2679   DECL_FIELD_CONTEXT (f_ovf) = record;
2680   DECL_FIELD_CONTEXT (f_sav) = record;
2681
2682   TREE_CHAIN (record) = type_decl;
2683   TYPE_NAME (record) = type_decl;
2684   TYPE_FIELDS (record) = f_gpr;
2685   TREE_CHAIN (f_gpr) = f_fpr;
2686   TREE_CHAIN (f_fpr) = f_ovf;
2687   TREE_CHAIN (f_ovf) = f_sav;
2688
2689   layout_type (record);
2690
2691   /* The correct type is an array type of one element.  */
2692   return build_array_type (record, build_index_type (size_zero_node));
2693 }
2694
2695 /* Perform any needed actions needed for a function that is receiving a
2696    variable number of arguments.
2697
2698    CUM is as above.
2699
2700    MODE and TYPE are the mode and type of the current parameter.
2701
2702    PRETEND_SIZE is a variable that should be set to the amount of stack
2703    that must be pushed by the prolog to pretend that our caller pushed
2704    it.
2705
2706    Normally, this macro will push all remaining incoming registers on the
2707    stack and set PRETEND_SIZE to the length of the registers pushed.  */
2708
2709 void
2710 ix86_setup_incoming_varargs (cum, mode, type, pretend_size, no_rtl)
2711      CUMULATIVE_ARGS *cum;
2712      enum machine_mode mode;
2713      tree type;
2714      int *pretend_size ATTRIBUTE_UNUSED;
2715      int no_rtl;
2716
2717 {
2718   CUMULATIVE_ARGS next_cum;
2719   rtx save_area = NULL_RTX, mem;
2720   rtx label;
2721   rtx label_ref;
2722   rtx tmp_reg;
2723   rtx nsse_reg;
2724   int set;
2725   tree fntype;
2726   int stdarg_p;
2727   int i;
2728
2729   if (!TARGET_64BIT)
2730     return;
2731
2732   /* Indicate to allocate space on the stack for varargs save area.  */
2733   ix86_save_varrargs_registers = 1;
2734
2735   fntype = TREE_TYPE (current_function_decl);
2736   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
2737               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
2738                   != void_type_node));
2739
2740   /* For varargs, we do not want to skip the dummy va_dcl argument.
2741      For stdargs, we do want to skip the last named argument.  */
2742   next_cum = *cum;
2743   if (stdarg_p)
2744     function_arg_advance (&next_cum, mode, type, 1);
2745
2746   if (!no_rtl)
2747     save_area = frame_pointer_rtx;
2748
2749   set = get_varargs_alias_set ();
2750
2751   for (i = next_cum.regno; i < ix86_regparm; i++)
2752     {
2753       mem = gen_rtx_MEM (Pmode,
2754                          plus_constant (save_area, i * UNITS_PER_WORD));
2755       set_mem_alias_set (mem, set);
2756       emit_move_insn (mem, gen_rtx_REG (Pmode,
2757                                         x86_64_int_parameter_registers[i]));
2758     }
2759
2760   if (next_cum.sse_nregs)
2761     {
2762       /* Now emit code to save SSE registers.  The AX parameter contains number
2763          of SSE parameter registers used to call this function.  We use
2764          sse_prologue_save insn template that produces computed jump across
2765          SSE saves.  We need some preparation work to get this working.  */
2766
2767       label = gen_label_rtx ();
2768       label_ref = gen_rtx_LABEL_REF (Pmode, label);
2769
2770       /* Compute address to jump to :
2771          label - 5*eax + nnamed_sse_arguments*5  */
2772       tmp_reg = gen_reg_rtx (Pmode);
2773       nsse_reg = gen_reg_rtx (Pmode);
2774       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, 0)));
2775       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
2776                               gen_rtx_MULT (Pmode, nsse_reg,
2777                                             GEN_INT (4))));
2778       if (next_cum.sse_regno)
2779         emit_move_insn
2780           (nsse_reg,
2781            gen_rtx_CONST (DImode,
2782                           gen_rtx_PLUS (DImode,
2783                                         label_ref,
2784                                         GEN_INT (next_cum.sse_regno * 4))));
2785       else
2786         emit_move_insn (nsse_reg, label_ref);
2787       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
2788
2789       /* Compute address of memory block we save into.  We always use pointer
2790          pointing 127 bytes after first byte to store - this is needed to keep
2791          instruction size limited by 4 bytes.  */
2792       tmp_reg = gen_reg_rtx (Pmode);
2793       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
2794                               plus_constant (save_area,
2795                                              8 * REGPARM_MAX + 127)));
2796       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
2797       set_mem_alias_set (mem, set);
2798       set_mem_align (mem, BITS_PER_WORD);
2799
2800       /* And finally do the dirty job!  */
2801       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
2802                                         GEN_INT (next_cum.sse_regno), label));
2803     }
2804
2805 }
2806
2807 /* Implement va_start.  */
2808
2809 void
2810 ix86_va_start (valist, nextarg)
2811      tree valist;
2812      rtx nextarg;
2813 {
2814   HOST_WIDE_INT words, n_gpr, n_fpr;
2815   tree f_gpr, f_fpr, f_ovf, f_sav;
2816   tree gpr, fpr, ovf, sav, t;
2817
2818   /* Only 64bit target needs something special.  */
2819   if (!TARGET_64BIT)
2820     {
2821       std_expand_builtin_va_start (valist, nextarg);
2822       return;
2823     }
2824
2825   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
2826   f_fpr = TREE_CHAIN (f_gpr);
2827   f_ovf = TREE_CHAIN (f_fpr);
2828   f_sav = TREE_CHAIN (f_ovf);
2829
2830   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
2831   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
2832   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
2833   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
2834   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
2835
2836   /* Count number of gp and fp argument registers used.  */
2837   words = current_function_args_info.words;
2838   n_gpr = current_function_args_info.regno;
2839   n_fpr = current_function_args_info.sse_regno;
2840
2841   if (TARGET_DEBUG_ARG)
2842     fprintf (stderr, "va_start: words = %d, n_gpr = %d, n_fpr = %d\n",
2843              (int) words, (int) n_gpr, (int) n_fpr);
2844
2845   t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
2846              build_int_2 (n_gpr * 8, 0));
2847   TREE_SIDE_EFFECTS (t) = 1;
2848   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2849
2850   t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
2851              build_int_2 (n_fpr * 16 + 8*REGPARM_MAX, 0));
2852   TREE_SIDE_EFFECTS (t) = 1;
2853   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2854
2855   /* Find the overflow area.  */
2856   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
2857   if (words != 0)
2858     t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
2859                build_int_2 (words * UNITS_PER_WORD, 0));
2860   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
2861   TREE_SIDE_EFFECTS (t) = 1;
2862   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2863
2864   /* Find the register save area.
2865      Prologue of the function save it right above stack frame.  */
2866   t = make_tree (TREE_TYPE (sav), frame_pointer_rtx);
2867   t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
2868   TREE_SIDE_EFFECTS (t) = 1;
2869   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2870 }
2871
2872 /* Implement va_arg.  */
2873 rtx
2874 ix86_va_arg (valist, type)
2875      tree valist, type;
2876 {
2877   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
2878   tree f_gpr, f_fpr, f_ovf, f_sav;
2879   tree gpr, fpr, ovf, sav, t;
2880   int size, rsize;
2881   rtx lab_false, lab_over = NULL_RTX;
2882   rtx addr_rtx, r;
2883   rtx container;
2884   int indirect_p = 0;
2885
2886   /* Only 64bit target needs something special.  */
2887   if (!TARGET_64BIT)
2888     {
2889       return std_expand_builtin_va_arg (valist, type);
2890     }
2891
2892   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
2893   f_fpr = TREE_CHAIN (f_gpr);
2894   f_ovf = TREE_CHAIN (f_fpr);
2895   f_sav = TREE_CHAIN (f_ovf);
2896
2897   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
2898   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
2899   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
2900   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
2901   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
2902
2903   size = int_size_in_bytes (type);
2904   if (size == -1)
2905     {
2906       /* Passed by reference.  */
2907       indirect_p = 1;
2908       type = build_pointer_type (type);
2909       size = int_size_in_bytes (type);
2910     }
2911   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2912
2913   container = construct_container (TYPE_MODE (type), type, 0,
2914                                    REGPARM_MAX, SSE_REGPARM_MAX, intreg, 0);
2915   /*
2916    * Pull the value out of the saved registers ...
2917    */
2918
2919   addr_rtx = gen_reg_rtx (Pmode);
2920
2921   if (container)
2922     {
2923       rtx int_addr_rtx, sse_addr_rtx;
2924       int needed_intregs, needed_sseregs;
2925       int need_temp;
2926
2927       lab_over = gen_label_rtx ();
2928       lab_false = gen_label_rtx ();
2929
2930       examine_argument (TYPE_MODE (type), type, 0,
2931                         &needed_intregs, &needed_sseregs);
2932
2933
2934       need_temp = ((needed_intregs && TYPE_ALIGN (type) > 64)
2935                    || TYPE_ALIGN (type) > 128);
2936
2937       /* In case we are passing structure, verify that it is consecutive block
2938          on the register save area.  If not we need to do moves.  */
2939       if (!need_temp && !REG_P (container))
2940         {
2941           /* Verify that all registers are strictly consecutive  */
2942           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
2943             {
2944               int i;
2945
2946               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
2947                 {
2948                   rtx slot = XVECEXP (container, 0, i);
2949                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
2950                       || INTVAL (XEXP (slot, 1)) != i * 16)
2951                     need_temp = 1;
2952                 }
2953             }
2954           else
2955             {
2956               int i;
2957
2958               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
2959                 {
2960                   rtx slot = XVECEXP (container, 0, i);
2961                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
2962                       || INTVAL (XEXP (slot, 1)) != i * 8)
2963                     need_temp = 1;
2964                 }
2965             }
2966         }
2967       if (!need_temp)
2968         {
2969           int_addr_rtx = addr_rtx;
2970           sse_addr_rtx = addr_rtx;
2971         }
2972       else
2973         {
2974           int_addr_rtx = gen_reg_rtx (Pmode);
2975           sse_addr_rtx = gen_reg_rtx (Pmode);
2976         }
2977       /* First ensure that we fit completely in registers.  */
2978       if (needed_intregs)
2979         {
2980           emit_cmp_and_jump_insns (expand_expr
2981                                    (gpr, NULL_RTX, SImode, EXPAND_NORMAL),
2982                                    GEN_INT ((REGPARM_MAX - needed_intregs +
2983                                              1) * 8), GE, const1_rtx, SImode,
2984                                    1, lab_false);
2985         }
2986       if (needed_sseregs)
2987         {
2988           emit_cmp_and_jump_insns (expand_expr
2989                                    (fpr, NULL_RTX, SImode, EXPAND_NORMAL),
2990                                    GEN_INT ((SSE_REGPARM_MAX -
2991                                              needed_sseregs + 1) * 16 +
2992                                             REGPARM_MAX * 8), GE, const1_rtx,
2993                                    SImode, 1, lab_false);
2994         }
2995
2996       /* Compute index to start of area used for integer regs.  */
2997       if (needed_intregs)
2998         {
2999           t = build (PLUS_EXPR, ptr_type_node, sav, gpr);
3000           r = expand_expr (t, int_addr_rtx, Pmode, EXPAND_NORMAL);
3001           if (r != int_addr_rtx)
3002             emit_move_insn (int_addr_rtx, r);
3003         }
3004       if (needed_sseregs)
3005         {
3006           t = build (PLUS_EXPR, ptr_type_node, sav, fpr);
3007           r = expand_expr (t, sse_addr_rtx, Pmode, EXPAND_NORMAL);
3008           if (r != sse_addr_rtx)
3009             emit_move_insn (sse_addr_rtx, r);
3010         }
3011       if (need_temp)
3012         {
3013           int i;
3014           rtx mem;
3015
3016           /* Never use the memory itself, as it has the alias set.  */
3017           addr_rtx = XEXP (assign_temp (type, 0, 1, 0), 0);
3018           mem = gen_rtx_MEM (BLKmode, addr_rtx);
3019           set_mem_alias_set (mem, get_varargs_alias_set ());
3020           set_mem_align (mem, BITS_PER_UNIT);
3021
3022           for (i = 0; i < XVECLEN (container, 0); i++)
3023             {
3024               rtx slot = XVECEXP (container, 0, i);
3025               rtx reg = XEXP (slot, 0);
3026               enum machine_mode mode = GET_MODE (reg);
3027               rtx src_addr;
3028               rtx src_mem;
3029               int src_offset;
3030               rtx dest_mem;
3031
3032               if (SSE_REGNO_P (REGNO (reg)))
3033                 {
3034                   src_addr = sse_addr_rtx;
3035                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
3036                 }
3037               else
3038                 {
3039                   src_addr = int_addr_rtx;
3040                   src_offset = REGNO (reg) * 8;
3041                 }
3042               src_mem = gen_rtx_MEM (mode, src_addr);
3043               set_mem_alias_set (src_mem, get_varargs_alias_set ());
3044               src_mem = adjust_address (src_mem, mode, src_offset);
3045               dest_mem = adjust_address (mem, mode, INTVAL (XEXP (slot, 1)));
3046               emit_move_insn (dest_mem, src_mem);
3047             }
3048         }
3049
3050       if (needed_intregs)
3051         {
3052           t =
3053             build (PLUS_EXPR, TREE_TYPE (gpr), gpr,
3054                    build_int_2 (needed_intregs * 8, 0));
3055           t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
3056           TREE_SIDE_EFFECTS (t) = 1;
3057           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3058         }
3059       if (needed_sseregs)
3060         {
3061           t =
3062             build (PLUS_EXPR, TREE_TYPE (fpr), fpr,
3063                    build_int_2 (needed_sseregs * 16, 0));
3064           t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr, t);
3065           TREE_SIDE_EFFECTS (t) = 1;
3066           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3067         }
3068
3069       emit_jump_insn (gen_jump (lab_over));
3070       emit_barrier ();
3071       emit_label (lab_false);
3072     }
3073
3074   /* ... otherwise out of the overflow area.  */
3075
3076   /* Care for on-stack alignment if needed.  */
3077   if (FUNCTION_ARG_BOUNDARY (VOIDmode, type) <= 64)
3078     t = ovf;
3079   else
3080     {
3081       HOST_WIDE_INT align = FUNCTION_ARG_BOUNDARY (VOIDmode, type) / 8;
3082       t = build (PLUS_EXPR, TREE_TYPE (ovf), ovf, build_int_2 (align - 1, 0));
3083       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align, -1));
3084     }
3085   t = save_expr (t);
3086
3087   r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
3088   if (r != addr_rtx)
3089     emit_move_insn (addr_rtx, r);
3090
3091   t =
3092     build (PLUS_EXPR, TREE_TYPE (t), t,
3093            build_int_2 (rsize * UNITS_PER_WORD, 0));
3094   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3095   TREE_SIDE_EFFECTS (t) = 1;
3096   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3097
3098   if (container)
3099     emit_label (lab_over);
3100
3101   if (indirect_p)
3102     {
3103       r = gen_rtx_MEM (Pmode, addr_rtx);
3104       set_mem_alias_set (r, get_varargs_alias_set ());
3105       emit_move_insn (addr_rtx, r);
3106     }
3107
3108   return addr_rtx;
3109 }
3110 \f
3111 /* Return nonzero if OP is either a i387 or SSE fp register.  */
3112 int
3113 any_fp_register_operand (op, mode)
3114      rtx op;
3115      enum machine_mode mode ATTRIBUTE_UNUSED;
3116 {
3117   return ANY_FP_REG_P (op);
3118 }
3119
3120 /* Return nonzero if OP is an i387 fp register.  */
3121 int
3122 fp_register_operand (op, mode)
3123      rtx op;
3124      enum machine_mode mode ATTRIBUTE_UNUSED;
3125 {
3126   return FP_REG_P (op);
3127 }
3128
3129 /* Return nonzero if OP is a non-fp register_operand.  */
3130 int
3131 register_and_not_any_fp_reg_operand (op, mode)
3132      rtx op;
3133      enum machine_mode mode;
3134 {
3135   return register_operand (op, mode) && !ANY_FP_REG_P (op);
3136 }
3137
3138 /* Return nonzero if OP is a register operand other than an
3139    i387 fp register.  */
3140 int
3141 register_and_not_fp_reg_operand (op, mode)
3142      rtx op;
3143      enum machine_mode mode;
3144 {
3145   return register_operand (op, mode) && !FP_REG_P (op);
3146 }
3147
3148 /* Return nonzero if OP is general operand representable on x86_64.  */
3149
3150 int
3151 x86_64_general_operand (op, mode)
3152      rtx op;
3153      enum machine_mode mode;
3154 {
3155   if (!TARGET_64BIT)
3156     return general_operand (op, mode);
3157   if (nonimmediate_operand (op, mode))
3158     return 1;
3159   return x86_64_sign_extended_value (op);
3160 }
3161
3162 /* Return nonzero if OP is general operand representable on x86_64
3163    as either sign extended or zero extended constant.  */
3164
3165 int
3166 x86_64_szext_general_operand (op, mode)
3167      rtx op;
3168      enum machine_mode mode;
3169 {
3170   if (!TARGET_64BIT)
3171     return general_operand (op, mode);
3172   if (nonimmediate_operand (op, mode))
3173     return 1;
3174   return x86_64_sign_extended_value (op) || x86_64_zero_extended_value (op);
3175 }
3176
3177 /* Return nonzero if OP is nonmemory operand representable on x86_64.  */
3178
3179 int
3180 x86_64_nonmemory_operand (op, mode)
3181      rtx op;
3182      enum machine_mode mode;
3183 {
3184   if (!TARGET_64BIT)
3185     return nonmemory_operand (op, mode);
3186   if (register_operand (op, mode))
3187     return 1;
3188   return x86_64_sign_extended_value (op);
3189 }
3190
3191 /* Return nonzero if OP is nonmemory operand acceptable by movabs patterns.  */
3192
3193 int
3194 x86_64_movabs_operand (op, mode)
3195      rtx op;
3196      enum machine_mode mode;
3197 {
3198   if (!TARGET_64BIT || !flag_pic)
3199     return nonmemory_operand (op, mode);
3200   if (register_operand (op, mode) || x86_64_sign_extended_value (op))
3201     return 1;
3202   if (CONSTANT_P (op) && !symbolic_reference_mentioned_p (op))
3203     return 1;
3204   return 0;
3205 }
3206
3207 /* Return nonzero if OP is nonmemory operand representable on x86_64.  */
3208
3209 int
3210 x86_64_szext_nonmemory_operand (op, mode)
3211      rtx op;
3212      enum machine_mode mode;
3213 {
3214   if (!TARGET_64BIT)
3215     return nonmemory_operand (op, mode);
3216   if (register_operand (op, mode))
3217     return 1;
3218   return x86_64_sign_extended_value (op) || x86_64_zero_extended_value (op);
3219 }
3220
3221 /* Return nonzero if OP is immediate operand representable on x86_64.  */
3222
3223 int
3224 x86_64_immediate_operand (op, mode)
3225      rtx op;
3226      enum machine_mode mode;
3227 {
3228   if (!TARGET_64BIT)
3229     return immediate_operand (op, mode);
3230   return x86_64_sign_extended_value (op);
3231 }
3232
3233 /* Return nonzero if OP is immediate operand representable on x86_64.  */
3234
3235 int
3236 x86_64_zext_immediate_operand (op, mode)
3237      rtx op;
3238      enum machine_mode mode ATTRIBUTE_UNUSED;
3239 {
3240   return x86_64_zero_extended_value (op);
3241 }
3242
3243 /* Return nonzero if OP is (const_int 1), else return zero.  */
3244
3245 int
3246 const_int_1_operand (op, mode)
3247      rtx op;
3248      enum machine_mode mode ATTRIBUTE_UNUSED;
3249 {
3250   return (GET_CODE (op) == CONST_INT && INTVAL (op) == 1);
3251 }
3252
3253 /* Return nonzero if OP is CONST_INT >= 1 and <= 31 (a valid operand
3254    for shift & compare patterns, as shifting by 0 does not change flags),
3255    else return zero.  */
3256
3257 int
3258 const_int_1_31_operand (op, mode)
3259      rtx op;
3260      enum machine_mode mode ATTRIBUTE_UNUSED;
3261 {
3262   return (GET_CODE (op) == CONST_INT && INTVAL (op) >= 1 && INTVAL (op) <= 31);
3263 }
3264
3265 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
3266    reference and a constant.  */
3267
3268 int
3269 symbolic_operand (op, mode)
3270      register rtx op;
3271      enum machine_mode mode ATTRIBUTE_UNUSED;
3272 {
3273   switch (GET_CODE (op))
3274     {
3275     case SYMBOL_REF:
3276     case LABEL_REF:
3277       return 1;
3278
3279     case CONST:
3280       op = XEXP (op, 0);
3281       if (GET_CODE (op) == SYMBOL_REF
3282           || GET_CODE (op) == LABEL_REF
3283           || (GET_CODE (op) == UNSPEC
3284               && (XINT (op, 1) == UNSPEC_GOT
3285                   || XINT (op, 1) == UNSPEC_GOTOFF
3286                   || XINT (op, 1) == UNSPEC_GOTPCREL)))
3287         return 1;
3288       if (GET_CODE (op) != PLUS
3289           || GET_CODE (XEXP (op, 1)) != CONST_INT)
3290         return 0;
3291
3292       op = XEXP (op, 0);
3293       if (GET_CODE (op) == SYMBOL_REF
3294           || GET_CODE (op) == LABEL_REF)
3295         return 1;
3296       /* Only @GOTOFF gets offsets.  */
3297       if (GET_CODE (op) != UNSPEC
3298           || XINT (op, 1) != UNSPEC_GOTOFF)
3299         return 0;
3300
3301       op = XVECEXP (op, 0, 0);
3302       if (GET_CODE (op) == SYMBOL_REF
3303           || GET_CODE (op) == LABEL_REF)
3304         return 1;
3305       return 0;
3306
3307     default:
3308       return 0;
3309     }
3310 }
3311
3312 /* Return true if the operand contains a @GOT or @GOTOFF reference.  */
3313
3314 int
3315 pic_symbolic_operand (op, mode)
3316      register rtx op;
3317      enum machine_mode mode ATTRIBUTE_UNUSED;
3318 {
3319   if (GET_CODE (op) != CONST)
3320     return 0;
3321   op = XEXP (op, 0);
3322   if (TARGET_64BIT)
3323     {
3324       if (GET_CODE (XEXP (op, 0)) == UNSPEC)
3325         return 1;
3326     }
3327   else
3328     {
3329       if (GET_CODE (op) == UNSPEC)
3330         return 1;
3331       if (GET_CODE (op) != PLUS
3332           || GET_CODE (XEXP (op, 1)) != CONST_INT)
3333         return 0;
3334       op = XEXP (op, 0);
3335       if (GET_CODE (op) == UNSPEC)
3336         return 1;
3337     }
3338   return 0;
3339 }
3340
3341 /* Return true if OP is a symbolic operand that resolves locally.  */
3342
3343 static int
3344 local_symbolic_operand (op, mode)
3345      rtx op;
3346      enum machine_mode mode ATTRIBUTE_UNUSED;
3347 {
3348   if (GET_CODE (op) == CONST
3349       && GET_CODE (XEXP (op, 0)) == PLUS
3350       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT)
3351     op = XEXP (XEXP (op, 0), 0);
3352
3353   if (GET_CODE (op) == LABEL_REF)
3354     return 1;
3355
3356   if (GET_CODE (op) != SYMBOL_REF)
3357     return 0;
3358
3359   /* These we've been told are local by varasm and encode_section_info
3360      respectively.  */
3361   if (CONSTANT_POOL_ADDRESS_P (op) || SYMBOL_REF_FLAG (op))
3362     return 1;
3363
3364   /* There is, however, a not insubstantial body of code in the rest of
3365      the compiler that assumes it can just stick the results of
3366      ASM_GENERATE_INTERNAL_LABEL in a symbol_ref and have done.  */
3367   /* ??? This is a hack.  Should update the body of the compiler to
3368      always create a DECL an invoke targetm.encode_section_info.  */
3369   if (strncmp (XSTR (op, 0), internal_label_prefix,
3370                internal_label_prefix_len) == 0)
3371     return 1;
3372
3373   return 0;
3374 }
3375
3376 /* Test for various thread-local symbols.  See ix86_encode_section_info. */
3377
3378 int
3379 tls_symbolic_operand (op, mode)
3380      register rtx op;
3381      enum machine_mode mode ATTRIBUTE_UNUSED;
3382 {
3383   const char *symbol_str;
3384
3385   if (GET_CODE (op) != SYMBOL_REF)
3386     return 0;
3387   symbol_str = XSTR (op, 0);
3388
3389   if (symbol_str[0] != '%')
3390     return 0;
3391   return strchr (tls_model_chars, symbol_str[1]) - tls_model_chars;
3392 }
3393
3394 static int
3395 tls_symbolic_operand_1 (op, kind)
3396      rtx op;
3397      enum tls_model kind;
3398 {
3399   const char *symbol_str;
3400
3401   if (GET_CODE (op) != SYMBOL_REF)
3402     return 0;
3403   symbol_str = XSTR (op, 0);
3404
3405   return symbol_str[0] == '%' && symbol_str[1] == tls_model_chars[kind];
3406 }
3407
3408 int
3409 global_dynamic_symbolic_operand (op, mode)
3410      register rtx op;
3411      enum machine_mode mode ATTRIBUTE_UNUSED;
3412 {
3413   return tls_symbolic_operand_1 (op, TLS_MODEL_GLOBAL_DYNAMIC);
3414 }
3415
3416 int
3417 local_dynamic_symbolic_operand (op, mode)
3418      register rtx op;
3419      enum machine_mode mode ATTRIBUTE_UNUSED;
3420 {
3421   return tls_symbolic_operand_1 (op, TLS_MODEL_LOCAL_DYNAMIC);
3422 }
3423
3424 int
3425 initial_exec_symbolic_operand (op, mode)
3426      register rtx op;
3427      enum machine_mode mode ATTRIBUTE_UNUSED;
3428 {
3429   return tls_symbolic_operand_1 (op, TLS_MODEL_INITIAL_EXEC);
3430 }
3431
3432 int
3433 local_exec_symbolic_operand (op, mode)
3434      register rtx op;
3435      enum machine_mode mode ATTRIBUTE_UNUSED;
3436 {
3437   return tls_symbolic_operand_1 (op, TLS_MODEL_LOCAL_EXEC);
3438 }
3439
3440 /* Test for a valid operand for a call instruction.  Don't allow the
3441    arg pointer register or virtual regs since they may decay into
3442    reg + const, which the patterns can't handle.  */
3443
3444 int
3445 call_insn_operand (op, mode)
3446      rtx op;
3447      enum machine_mode mode ATTRIBUTE_UNUSED;
3448 {
3449   /* Disallow indirect through a virtual register.  This leads to
3450      compiler aborts when trying to eliminate them.  */
3451   if (GET_CODE (op) == REG
3452       && (op == arg_pointer_rtx
3453           || op == frame_pointer_rtx
3454           || (REGNO (op) >= FIRST_PSEUDO_REGISTER
3455               && REGNO (op) <= LAST_VIRTUAL_REGISTER)))
3456     return 0;
3457
3458   /* Disallow `call 1234'.  Due to varying assembler lameness this
3459      gets either rejected or translated to `call .+1234'.  */
3460   if (GET_CODE (op) == CONST_INT)
3461     return 0;
3462
3463   /* Explicitly allow SYMBOL_REF even if pic.  */
3464   if (GET_CODE (op) == SYMBOL_REF)
3465     return 1;
3466
3467   /* Otherwise we can allow any general_operand in the address.  */
3468   return general_operand (op, Pmode);
3469 }
3470
3471 /* Test for a valid operand for a call instruction.  Don't allow the
3472    arg pointer register or virtual regs since they may decay into
3473    reg + const, which the patterns can't handle.  */
3474
3475 int
3476 sibcall_insn_operand (op, mode)
3477      rtx op;
3478      enum machine_mode mode ATTRIBUTE_UNUSED;
3479 {
3480   /* Disallow indirect through a virtual register.  This leads to
3481      compiler aborts when trying to eliminate them.  */
3482   if (GET_CODE (op) == REG
3483       && (op == arg_pointer_rtx
3484           || op == frame_pointer_rtx
3485           || (REGNO (op) >= FIRST_PSEUDO_REGISTER
3486               && REGNO (op) <= LAST_VIRTUAL_REGISTER)))
3487     return 0;
3488
3489   /* Explicitly allow SYMBOL_REF even if pic.  */
3490   if (GET_CODE (op) == SYMBOL_REF)
3491     return 1;
3492
3493   /* Otherwise we can only allow register operands.  */
3494   return register_operand (op, Pmode);
3495 }
3496
3497 int
3498 constant_call_address_operand (op, mode)
3499      rtx op;
3500      enum machine_mode mode ATTRIBUTE_UNUSED;
3501 {
3502   if (GET_CODE (op) == CONST
3503       && GET_CODE (XEXP (op, 0)) == PLUS
3504       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT)
3505     op = XEXP (XEXP (op, 0), 0);
3506   return GET_CODE (op) == SYMBOL_REF;
3507 }
3508
3509 /* Match exactly zero and one.  */
3510
3511 int
3512 const0_operand (op, mode)
3513      register rtx op;
3514      enum machine_mode mode;
3515 {
3516   return op == CONST0_RTX (mode);
3517 }
3518
3519 int
3520 const1_operand (op, mode)
3521      register rtx op;
3522      enum machine_mode mode ATTRIBUTE_UNUSED;
3523 {
3524   return op == const1_rtx;
3525 }
3526
3527 /* Match 2, 4, or 8.  Used for leal multiplicands.  */
3528
3529 int
3530 const248_operand (op, mode)
3531      register rtx op;
3532      enum machine_mode mode ATTRIBUTE_UNUSED;
3533 {
3534   return (GET_CODE (op) == CONST_INT
3535           && (INTVAL (op) == 2 || INTVAL (op) == 4 || INTVAL (op) == 8));
3536 }
3537
3538 /* True if this is a constant appropriate for an increment or decrement.  */
3539
3540 int
3541 incdec_operand (op, mode)
3542      register rtx op;
3543      enum machine_mode mode ATTRIBUTE_UNUSED;
3544 {
3545   /* On Pentium4, the inc and dec operations causes extra dependency on flag
3546      registers, since carry flag is not set.  */
3547   if (TARGET_PENTIUM4 && !optimize_size)
3548     return 0;
3549   return op == const1_rtx || op == constm1_rtx;
3550 }
3551
3552 /* Return nonzero if OP is acceptable as operand of DImode shift
3553    expander.  */
3554
3555 int
3556 shiftdi_operand (op, mode)
3557      rtx op;
3558      enum machine_mode mode ATTRIBUTE_UNUSED;
3559 {
3560   if (TARGET_64BIT)
3561     return nonimmediate_operand (op, mode);
3562   else
3563     return register_operand (op, mode);
3564 }
3565
3566 /* Return false if this is the stack pointer, or any other fake
3567    register eliminable to the stack pointer.  Otherwise, this is
3568    a register operand.
3569
3570    This is used to prevent esp from being used as an index reg.
3571    Which would only happen in pathological cases.  */
3572
3573 int
3574 reg_no_sp_operand (op, mode)
3575      register rtx op;
3576      enum machine_mode mode;
3577 {
3578   rtx t = op;
3579   if (GET_CODE (t) == SUBREG)
3580     t = SUBREG_REG (t);
3581   if (t == stack_pointer_rtx || t == arg_pointer_rtx || t == frame_pointer_rtx)
3582     return 0;
3583
3584   return register_operand (op, mode);
3585 }
3586
3587 int
3588 mmx_reg_operand (op, mode)
3589      register rtx op;
3590      enum machine_mode mode ATTRIBUTE_UNUSED;
3591 {
3592   return MMX_REG_P (op);
3593 }
3594
3595 /* Return false if this is any eliminable register.  Otherwise
3596    general_operand.  */
3597
3598 int
3599 general_no_elim_operand (op, mode)
3600      register rtx op;
3601      enum machine_mode mode;
3602 {
3603   rtx t = op;
3604   if (GET_CODE (t) == SUBREG)
3605     t = SUBREG_REG (t);
3606   if (t == arg_pointer_rtx || t == frame_pointer_rtx
3607       || t == virtual_incoming_args_rtx || t == virtual_stack_vars_rtx
3608       || t == virtual_stack_dynamic_rtx)
3609     return 0;
3610   if (REG_P (t)
3611       && REGNO (t) >= FIRST_VIRTUAL_REGISTER
3612       && REGNO (t) <= LAST_VIRTUAL_REGISTER)
3613     return 0;
3614
3615   return general_operand (op, mode);
3616 }
3617
3618 /* Return false if this is any eliminable register.  Otherwise
3619    register_operand or const_int.  */
3620
3621 int
3622 nonmemory_no_elim_operand (op, mode)
3623      register rtx op;
3624      enum machine_mode mode;
3625 {
3626   rtx t = op;
3627   if (GET_CODE (t) == SUBREG)
3628     t = SUBREG_REG (t);
3629   if (t == arg_pointer_rtx || t == frame_pointer_rtx
3630       || t == virtual_incoming_args_rtx || t == virtual_stack_vars_rtx
3631       || t == virtual_stack_dynamic_rtx)
3632     return 0;
3633
3634   return GET_CODE (op) == CONST_INT || register_operand (op, mode);
3635 }
3636
3637 /* Return false if this is any eliminable register or stack register,
3638    otherwise work like register_operand.  */
3639
3640 int
3641 index_register_operand (op, mode)
3642      register rtx op;
3643      enum machine_mode mode;
3644 {
3645   rtx t = op;
3646   if (GET_CODE (t) == SUBREG)
3647     t = SUBREG_REG (t);
3648   if (!REG_P (t))
3649     return 0;
3650   if (t == arg_pointer_rtx
3651       || t == frame_pointer_rtx
3652       || t == virtual_incoming_args_rtx
3653       || t == virtual_stack_vars_rtx
3654       || t == virtual_stack_dynamic_rtx
3655       || REGNO (t) == STACK_POINTER_REGNUM)
3656     return 0;
3657
3658   return general_operand (op, mode);
3659 }
3660
3661 /* Return true if op is a Q_REGS class register.  */
3662
3663 int
3664 q_regs_operand (op, mode)
3665      register rtx op;
3666      enum machine_mode mode;
3667 {
3668   if (mode != VOIDmode && GET_MODE (op) != mode)
3669     return 0;
3670   if (GET_CODE (op) == SUBREG)
3671     op = SUBREG_REG (op);
3672   return ANY_QI_REG_P (op);
3673 }
3674
3675 /* Return true if op is an flags register.  */
3676
3677 int
3678 flags_reg_operand (op, mode)
3679      register rtx op;
3680      enum machine_mode mode;
3681 {
3682   if (mode != VOIDmode && GET_MODE (op) != mode)
3683     return 0;
3684   return REG_P (op) && REGNO (op) == FLAGS_REG && GET_MODE (op) != VOIDmode;
3685 }
3686
3687 /* Return true if op is a NON_Q_REGS class register.  */
3688
3689 int
3690 non_q_regs_operand (op, mode)
3691      register rtx op;
3692      enum machine_mode mode;
3693 {
3694   if (mode != VOIDmode && GET_MODE (op) != mode)
3695     return 0;
3696   if (GET_CODE (op) == SUBREG)
3697     op = SUBREG_REG (op);
3698   return NON_QI_REG_P (op);
3699 }
3700
3701 int
3702 zero_extended_scalar_load_operand (op, mode)
3703      rtx op;
3704      enum machine_mode mode ATTRIBUTE_UNUSED;
3705 {
3706   unsigned n_elts;
3707   if (GET_CODE (op) != MEM)
3708     return 0;
3709   op = maybe_get_pool_constant (op);
3710   if (!op)
3711     return 0;
3712   if (GET_CODE (op) != CONST_VECTOR)
3713     return 0;
3714   n_elts =
3715     (GET_MODE_SIZE (GET_MODE (op)) /
3716      GET_MODE_SIZE (GET_MODE_INNER (GET_MODE (op))));
3717   for (n_elts--; n_elts > 0; n_elts--)
3718     {
3719       rtx elt = CONST_VECTOR_ELT (op, n_elts);
3720       if (elt != CONST0_RTX (GET_MODE_INNER (GET_MODE (op))))
3721         return 0;
3722     }
3723   return 1;
3724 }
3725
3726 /* Return 1 if OP is a comparison that can be used in the CMPSS/CMPPS
3727    insns.  */
3728 int
3729 sse_comparison_operator (op, mode)
3730      rtx op;
3731      enum machine_mode mode ATTRIBUTE_UNUSED;
3732 {
3733   enum rtx_code code = GET_CODE (op);
3734   switch (code)
3735     {
3736     /* Operations supported directly.  */
3737     case EQ:
3738     case LT:
3739     case LE:
3740     case UNORDERED:
3741     case NE:
3742     case UNGE:
3743     case UNGT:
3744     case ORDERED:
3745       return 1;
3746     /* These are equivalent to ones above in non-IEEE comparisons.  */
3747     case UNEQ:
3748     case UNLT:
3749     case UNLE:
3750     case LTGT:
3751     case GE:
3752     case GT:
3753       return !TARGET_IEEE_FP;
3754     default:
3755       return 0;
3756     }
3757 }
3758 /* Return 1 if OP is a valid comparison operator in valid mode.  */
3759 int
3760 ix86_comparison_operator (op, mode)
3761      register rtx op;
3762      enum machine_mode mode;
3763 {
3764   enum machine_mode inmode;
3765   enum rtx_code code = GET_CODE (op);
3766   if (mode != VOIDmode && GET_MODE (op) != mode)
3767     return 0;
3768   if (GET_RTX_CLASS (code) != '<')
3769     return 0;
3770   inmode = GET_MODE (XEXP (op, 0));
3771
3772   if (inmode == CCFPmode || inmode == CCFPUmode)
3773     {
3774       enum rtx_code second_code, bypass_code;
3775       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
3776       return (bypass_code == NIL && second_code == NIL);
3777     }
3778   switch (code)
3779     {
3780     case EQ: case NE:
3781       return 1;
3782     case LT: case GE:
3783       if (inmode == CCmode || inmode == CCGCmode
3784           || inmode == CCGOCmode || inmode == CCNOmode)
3785         return 1;
3786       return 0;
3787     case LTU: case GTU: case LEU: case ORDERED: case UNORDERED: case GEU:
3788       if (inmode == CCmode)
3789         return 1;
3790       return 0;
3791     case GT: case LE:
3792       if (inmode == CCmode || inmode == CCGCmode || inmode == CCNOmode)
3793         return 1;
3794       return 0;
3795     default:
3796       return 0;
3797     }
3798 }
3799
3800 /* Return 1 if OP is a comparison operator that can be issued by fcmov.  */
3801
3802 int
3803 fcmov_comparison_operator (op, mode)
3804     register rtx op;
3805     enum machine_mode mode;
3806 {
3807   enum machine_mode inmode;
3808   enum rtx_code code = GET_CODE (op);
3809   if (mode != VOIDmode && GET_MODE (op) != mode)
3810     return 0;
3811   if (GET_RTX_CLASS (code) != '<')
3812     return 0;
3813   inmode = GET_MODE (XEXP (op, 0));
3814   if (inmode == CCFPmode || inmode == CCFPUmode)
3815     {
3816       enum rtx_code second_code, bypass_code;
3817       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
3818       if (bypass_code != NIL || second_code != NIL)
3819         return 0;
3820       code = ix86_fp_compare_code_to_integer (code);
3821     }
3822   /* i387 supports just limited amount of conditional codes.  */
3823   switch (code)
3824     {
3825     case LTU: case GTU: case LEU: case GEU:
3826       if (inmode == CCmode || inmode == CCFPmode || inmode == CCFPUmode)
3827         return 1;
3828       return 0;
3829     case ORDERED: case UNORDERED:
3830     case EQ: case NE:
3831       return 1;
3832     default:
3833       return 0;
3834     }
3835 }
3836
3837 /* Return 1 if OP is a binary operator that can be promoted to wider mode.  */
3838
3839 int
3840 promotable_binary_operator (op, mode)
3841      register rtx op;
3842      enum machine_mode mode ATTRIBUTE_UNUSED;
3843 {
3844   switch (GET_CODE (op))
3845     {
3846     case MULT:
3847       /* Modern CPUs have same latency for HImode and SImode multiply,
3848          but 386 and 486 do HImode multiply faster.  */
3849       return ix86_cpu > PROCESSOR_I486;
3850     case PLUS:
3851     case AND:
3852     case IOR:
3853     case XOR:
3854     case ASHIFT:
3855       return 1;
3856     default:
3857       return 0;
3858     }
3859 }
3860
3861 /* Nearly general operand, but accept any const_double, since we wish
3862    to be able to drop them into memory rather than have them get pulled
3863    into registers.  */
3864
3865 int
3866 cmp_fp_expander_operand (op, mode)
3867      register rtx op;
3868      enum machine_mode mode;
3869 {
3870   if (mode != VOIDmode && mode != GET_MODE (op))
3871     return 0;
3872   if (GET_CODE (op) == CONST_DOUBLE)
3873     return 1;
3874   return general_operand (op, mode);
3875 }
3876
3877 /* Match an SI or HImode register for a zero_extract.  */
3878
3879 int
3880 ext_register_operand (op, mode)
3881      register rtx op;
3882      enum machine_mode mode ATTRIBUTE_UNUSED;
3883 {
3884   int regno;
3885   if ((!TARGET_64BIT || GET_MODE (op) != DImode)
3886       && GET_MODE (op) != SImode && GET_MODE (op) != HImode)
3887     return 0;
3888
3889   if (!register_operand (op, VOIDmode))
3890     return 0;
3891
3892   /* Be careful to accept only registers having upper parts.  */
3893   regno = REG_P (op) ? REGNO (op) : REGNO (SUBREG_REG (op));
3894   return (regno > LAST_VIRTUAL_REGISTER || regno < 4);
3895 }
3896
3897 /* Return 1 if this is a valid binary floating-point operation.
3898    OP is the expression matched, and MODE is its mode.  */
3899
3900 int
3901 binary_fp_operator (op, mode)
3902     register rtx op;
3903     enum machine_mode mode;
3904 {
3905   if (mode != VOIDmode && mode != GET_MODE (op))
3906     return 0;
3907
3908   switch (GET_CODE (op))
3909     {
3910     case PLUS:
3911     case MINUS:
3912     case MULT:
3913     case DIV:
3914       return GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT;
3915
3916     default:
3917       return 0;
3918     }
3919 }
3920
3921 int
3922 mult_operator (op, mode)
3923     register rtx op;
3924     enum machine_mode mode ATTRIBUTE_UNUSED;
3925 {
3926   return GET_CODE (op) == MULT;
3927 }
3928
3929 int
3930 div_operator (op, mode)
3931     register rtx op;
3932     enum machine_mode mode ATTRIBUTE_UNUSED;
3933 {
3934   return GET_CODE (op) == DIV;
3935 }
3936
3937 int
3938 arith_or_logical_operator (op, mode)
3939       rtx op;
3940       enum machine_mode mode;
3941 {
3942   return ((mode == VOIDmode || GET_MODE (op) == mode)
3943           && (GET_RTX_CLASS (GET_CODE (op)) == 'c'
3944               || GET_RTX_CLASS (GET_CODE (op)) == '2'));
3945 }
3946
3947 /* Returns 1 if OP is memory operand with a displacement.  */
3948
3949 int
3950 memory_displacement_operand (op, mode)
3951      register rtx op;
3952      enum machine_mode mode;
3953 {
3954   struct ix86_address parts;
3955
3956   if (! memory_operand (op, mode))
3957     return 0;
3958
3959   if (! ix86_decompose_address (XEXP (op, 0), &parts))
3960     abort ();
3961
3962   return parts.disp != NULL_RTX;
3963 }
3964
3965 /* To avoid problems when jump re-emits comparisons like testqi_ext_ccno_0,
3966    re-recognize the operand to avoid a copy_to_mode_reg that will fail.
3967
3968    ??? It seems likely that this will only work because cmpsi is an
3969    expander, and no actual insns use this.  */
3970
3971 int
3972 cmpsi_operand (op, mode)
3973       rtx op;
3974       enum machine_mode mode;
3975 {
3976   if (nonimmediate_operand (op, mode))
3977     return 1;
3978
3979   if (GET_CODE (op) == AND
3980       && GET_MODE (op) == SImode
3981       && GET_CODE (XEXP (op, 0)) == ZERO_EXTRACT
3982       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
3983       && GET_CODE (XEXP (XEXP (op, 0), 2)) == CONST_INT
3984       && INTVAL (XEXP (XEXP (op, 0), 1)) == 8
3985       && INTVAL (XEXP (XEXP (op, 0), 2)) == 8
3986       && GET_CODE (XEXP (op, 1)) == CONST_INT)
3987     return 1;
3988
3989   return 0;
3990 }
3991
3992 /* Returns 1 if OP is memory operand that can not be represented by the
3993    modRM array.  */
3994
3995 int
3996 long_memory_operand (op, mode)
3997      register rtx op;
3998      enum machine_mode mode;
3999 {
4000   if (! memory_operand (op, mode))
4001     return 0;
4002
4003   return memory_address_length (op) != 0;
4004 }
4005
4006 /* Return nonzero if the rtx is known aligned.  */
4007
4008 int
4009 aligned_operand (op, mode)
4010      rtx op;
4011      enum machine_mode mode;
4012 {
4013   struct ix86_address parts;
4014
4015   if (!general_operand (op, mode))
4016     return 0;
4017
4018   /* Registers and immediate operands are always "aligned".  */
4019   if (GET_CODE (op) != MEM)
4020     return 1;
4021
4022   /* Don't even try to do any aligned optimizations with volatiles.  */
4023   if (MEM_VOLATILE_P (op))
4024     return 0;
4025
4026   op = XEXP (op, 0);
4027
4028   /* Pushes and pops are only valid on the stack pointer.  */
4029   if (GET_CODE (op) == PRE_DEC
4030       || GET_CODE (op) == POST_INC)
4031     return 1;
4032
4033   /* Decode the address.  */
4034   if (! ix86_decompose_address (op, &parts))
4035     abort ();
4036
4037   if (parts.base && GET_CODE (parts.base) == SUBREG)
4038     parts.base = SUBREG_REG (parts.base);
4039   if (parts.index && GET_CODE (parts.index) == SUBREG)
4040     parts.index = SUBREG_REG (parts.index);
4041
4042   /* Look for some component that isn't known to be aligned.  */
4043   if (parts.index)
4044     {
4045       if (parts.scale < 4
4046           && REGNO_POINTER_ALIGN (REGNO (parts.index)) < 32)
4047         return 0;
4048     }
4049   if (parts.base)
4050     {
4051       if (REGNO_POINTER_ALIGN (REGNO (parts.base)) < 32)
4052         return 0;
4053     }
4054   if (parts.disp)
4055     {
4056       if (GET_CODE (parts.disp) != CONST_INT
4057           || (INTVAL (parts.disp) & 3) != 0)
4058         return 0;
4059     }
4060
4061   /* Didn't find one -- this must be an aligned address.  */
4062   return 1;
4063 }
4064 \f
4065 /* Return true if the constant is something that can be loaded with
4066    a special instruction.  Only handle 0.0 and 1.0; others are less
4067    worthwhile.  */
4068
4069 int
4070 standard_80387_constant_p (x)
4071      rtx x;
4072 {
4073   if (GET_CODE (x) != CONST_DOUBLE || !FLOAT_MODE_P (GET_MODE (x)))
4074     return -1;
4075   /* Note that on the 80387, other constants, such as pi, that we should support
4076      too.  On some machines, these are much slower to load as standard constant,
4077      than to load from doubles in memory.  */
4078   if (x == CONST0_RTX (GET_MODE (x)))
4079     return 1;
4080   if (x == CONST1_RTX (GET_MODE (x)))
4081     return 2;
4082   return 0;
4083 }
4084
4085 /* Return 1 if X is FP constant we can load to SSE register w/o using memory.
4086  */
4087 int
4088 standard_sse_constant_p (x)
4089      rtx x;
4090 {
4091   if (x == const0_rtx)
4092     return 1;
4093   return (x == CONST0_RTX (GET_MODE (x)));
4094 }
4095
4096 /* Returns 1 if OP contains a symbol reference */
4097
4098 int
4099 symbolic_reference_mentioned_p (op)
4100      rtx op;
4101 {
4102   register const char *fmt;
4103   register int i;
4104
4105   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
4106     return 1;
4107
4108   fmt = GET_RTX_FORMAT (GET_CODE (op));
4109   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
4110     {
4111       if (fmt[i] == 'E')
4112         {
4113           register int j;
4114
4115           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
4116             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
4117               return 1;
4118         }
4119
4120       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
4121         return 1;
4122     }
4123
4124   return 0;
4125 }
4126
4127 /* Return 1 if it is appropriate to emit `ret' instructions in the
4128    body of a function.  Do this only if the epilogue is simple, needing a
4129    couple of insns.  Prior to reloading, we can't tell how many registers
4130    must be saved, so return 0 then.  Return 0 if there is no frame
4131    marker to de-allocate.
4132
4133    If NON_SAVING_SETJMP is defined and true, then it is not possible
4134    for the epilogue to be simple, so return 0.  This is a special case
4135    since NON_SAVING_SETJMP will not cause regs_ever_live to change
4136    until final, but jump_optimize may need to know sooner if a
4137    `return' is OK.  */
4138
4139 int
4140 ix86_can_use_return_insn_p ()
4141 {
4142   struct ix86_frame frame;
4143
4144 #ifdef NON_SAVING_SETJMP
4145   if (NON_SAVING_SETJMP && current_function_calls_setjmp)
4146     return 0;
4147 #endif
4148
4149   if (! reload_completed || frame_pointer_needed)
4150     return 0;
4151
4152   /* Don't allow more than 32 pop, since that's all we can do
4153      with one instruction.  */
4154   if (current_function_pops_args
4155       && current_function_args_size >= 32768)
4156     return 0;
4157
4158   ix86_compute_frame_layout (&frame);
4159   return frame.to_allocate == 0 && frame.nregs == 0;
4160 }
4161 \f
4162 /* Return 1 if VALUE can be stored in the sign extended immediate field.  */
4163 int
4164 x86_64_sign_extended_value (value)
4165      rtx value;
4166 {
4167   switch (GET_CODE (value))
4168     {
4169       /* CONST_DOUBLES never match, since HOST_BITS_PER_WIDE_INT is known
4170          to be at least 32 and this all acceptable constants are
4171          represented as CONST_INT.  */
4172       case CONST_INT:
4173         if (HOST_BITS_PER_WIDE_INT == 32)
4174           return 1;
4175         else
4176           {
4177             HOST_WIDE_INT val = trunc_int_for_mode (INTVAL (value), DImode);
4178             return trunc_int_for_mode (val, SImode) == val;
4179           }
4180         break;
4181
4182       /* For certain code models, the symbolic references are known to fit.
4183          in CM_SMALL_PIC model we know it fits if it is local to the shared
4184          library.  Don't count TLS SYMBOL_REFs here, since they should fit
4185          only if inside of UNSPEC handled below.  */
4186       case SYMBOL_REF:
4187         return (ix86_cmodel == CM_SMALL || ix86_cmodel == CM_KERNEL);
4188
4189       /* For certain code models, the code is near as well.  */
4190       case LABEL_REF:
4191         return (ix86_cmodel == CM_SMALL || ix86_cmodel == CM_MEDIUM
4192                 || ix86_cmodel == CM_KERNEL);
4193
4194       /* We also may accept the offsetted memory references in certain special
4195          cases.  */
4196       case CONST:
4197         if (GET_CODE (XEXP (value, 0)) == UNSPEC)
4198           switch (XINT (XEXP (value, 0), 1))
4199             {
4200             case UNSPEC_GOTPCREL:
4201             case UNSPEC_DTPOFF:
4202             case UNSPEC_GOTNTPOFF:
4203             case UNSPEC_NTPOFF:
4204               return 1;
4205             default:
4206               break;
4207             }
4208         if (GET_CODE (XEXP (value, 0)) == PLUS)
4209           {
4210             rtx op1 = XEXP (XEXP (value, 0), 0);
4211             rtx op2 = XEXP (XEXP (value, 0), 1);
4212             HOST_WIDE_INT offset;
4213
4214             if (ix86_cmodel == CM_LARGE)
4215               return 0;
4216             if (GET_CODE (op2) != CONST_INT)
4217               return 0;
4218             offset = trunc_int_for_mode (INTVAL (op2), DImode);
4219             switch (GET_CODE (op1))
4220               {
4221                 case SYMBOL_REF:
4222                   /* For CM_SMALL assume that latest object is 16MB before
4223                      end of 31bits boundary.  We may also accept pretty
4224                      large negative constants knowing that all objects are
4225                      in the positive half of address space.  */
4226                   if (ix86_cmodel == CM_SMALL
4227                       && offset < 16*1024*1024
4228                       && trunc_int_for_mode (offset, SImode) == offset)
4229                     return 1;
4230                   /* For CM_KERNEL we know that all object resist in the
4231                      negative half of 32bits address space.  We may not
4232                      accept negative offsets, since they may be just off
4233                      and we may accept pretty large positive ones.  */
4234                   if (ix86_cmodel == CM_KERNEL
4235                       && offset > 0
4236                       && trunc_int_for_mode (offset, SImode) == offset)
4237                     return 1;
4238                   break;
4239                 case LABEL_REF:
4240                   /* These conditions are similar to SYMBOL_REF ones, just the
4241                      constraints for code models differ.  */
4242                   if ((ix86_cmodel == CM_SMALL || ix86_cmodel == CM_MEDIUM)
4243                       && offset < 16*1024*1024
4244                       && trunc_int_for_mode (offset, SImode) == offset)
4245                     return 1;
4246                   if (ix86_cmodel == CM_KERNEL
4247                       && offset > 0
4248                       && trunc_int_for_mode (offset, SImode) == offset)
4249                     return 1;
4250                   break;
4251                 case UNSPEC:
4252                   switch (XINT (op1, 1))
4253                     {
4254                     case UNSPEC_DTPOFF:
4255                     case UNSPEC_NTPOFF:
4256                       if (offset > 0
4257                           && trunc_int_for_mode (offset, SImode) == offset)
4258                         return 1;
4259                     }
4260                   break;
4261                 default:
4262                   return 0;
4263               }
4264           }
4265         return 0;
4266       default:
4267         return 0;
4268     }
4269 }
4270
4271 /* Return 1 if VALUE can be stored in the zero extended immediate field.  */
4272 int
4273 x86_64_zero_extended_value (value)
4274      rtx value;
4275 {
4276   switch (GET_CODE (value))
4277     {
4278       case CONST_DOUBLE:
4279         if (HOST_BITS_PER_WIDE_INT == 32)
4280           return  (GET_MODE (value) == VOIDmode
4281                    && !CONST_DOUBLE_HIGH (value));
4282         else
4283           return 0;
4284       case CONST_INT:
4285         if (HOST_BITS_PER_WIDE_INT == 32)
4286           return INTVAL (value) >= 0;
4287         else
4288           return !(INTVAL (value) & ~(HOST_WIDE_INT) 0xffffffff);
4289         break;
4290
4291       /* For certain code models, the symbolic references are known to fit.  */
4292       case SYMBOL_REF:
4293         return ix86_cmodel == CM_SMALL;
4294
4295       /* For certain code models, the code is near as well.  */
4296       case LABEL_REF:
4297         return ix86_cmodel == CM_SMALL || ix86_cmodel == CM_MEDIUM;
4298
4299       /* We also may accept the offsetted memory references in certain special
4300          cases.  */
4301       case CONST:
4302         if (GET_CODE (XEXP (value, 0)) == PLUS)
4303           {
4304             rtx op1 = XEXP (XEXP (value, 0), 0);
4305             rtx op2 = XEXP (XEXP (value, 0), 1);
4306
4307             if (ix86_cmodel == CM_LARGE)
4308               return 0;
4309             switch (GET_CODE (op1))
4310               {
4311                 case SYMBOL_REF:
4312                     return 0;
4313                   /* For small code model we may accept pretty large positive
4314                      offsets, since one bit is available for free.  Negative
4315                      offsets are limited by the size of NULL pointer area
4316                      specified by the ABI.  */
4317                   if (ix86_cmodel == CM_SMALL
4318                       && GET_CODE (op2) == CONST_INT
4319                       && trunc_int_for_mode (INTVAL (op2), DImode) > -0x10000
4320                       && (trunc_int_for_mode (INTVAL (op2), SImode)
4321                           == INTVAL (op2)))
4322                     return 1;
4323                   /* ??? For the kernel, we may accept adjustment of
4324                      -0x10000000, since we know that it will just convert
4325                      negative address space to positive, but perhaps this
4326                      is not worthwhile.  */
4327                   break;
4328                 case LABEL_REF:
4329                   /* These conditions are similar to SYMBOL_REF ones, just the
4330                      constraints for code models differ.  */
4331                   if ((ix86_cmodel == CM_SMALL || ix86_cmodel == CM_MEDIUM)
4332                       && GET_CODE (op2) == CONST_INT
4333                       && trunc_int_for_mode (INTVAL (op2), DImode) > -0x10000
4334                       && (trunc_int_for_mode (INTVAL (op2), SImode)
4335                           == INTVAL (op2)))
4336                     return 1;
4337                   break;
4338                 default:
4339                   return 0;
4340               }
4341           }
4342         return 0;
4343       default:
4344         return 0;
4345     }
4346 }
4347
4348 /* Value should be nonzero if functions must have frame pointers.
4349    Zero means the frame pointer need not be set up (and parms may
4350    be accessed via the stack pointer) in functions that seem suitable.  */
4351
4352 int
4353 ix86_frame_pointer_required ()
4354 {
4355   /* If we accessed previous frames, then the generated code expects
4356      to be able to access the saved ebp value in our frame.  */
4357   if (cfun->machine->accesses_prev_frame)
4358     return 1;
4359
4360   /* Several x86 os'es need a frame pointer for other reasons,
4361      usually pertaining to setjmp.  */
4362   if (SUBTARGET_FRAME_POINTER_REQUIRED)
4363     return 1;
4364
4365   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
4366      the frame pointer by default.  Turn it back on now if we've not
4367      got a leaf function.  */
4368   if (TARGET_OMIT_LEAF_FRAME_POINTER
4369       && (!current_function_is_leaf))
4370     return 1;
4371
4372   if (current_function_profile)
4373     return 1;
4374
4375   return 0;
4376 }
4377
4378 /* Record that the current function accesses previous call frames.  */
4379
4380 void
4381 ix86_setup_frame_addresses ()
4382 {
4383   cfun->machine->accesses_prev_frame = 1;
4384 }
4385 \f
4386 #if defined(HAVE_GAS_HIDDEN) && defined(SUPPORTS_ONE_ONLY)
4387 # define USE_HIDDEN_LINKONCE 1
4388 #else
4389 # define USE_HIDDEN_LINKONCE 0
4390 #endif
4391
4392 static int pic_labels_used;
4393
4394 /* Fills in the label name that should be used for a pc thunk for
4395    the given register.  */
4396
4397 static void
4398 get_pc_thunk_name (name, regno)
4399      char name[32];
4400      unsigned int regno;
4401 {
4402   if (USE_HIDDEN_LINKONCE)
4403     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
4404   else
4405     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
4406 }
4407
4408
4409 /* This function generates code for -fpic that loads %ebx with
4410    the return address of the caller and then returns.  */
4411
4412 void
4413 ix86_asm_file_end (file)
4414      FILE *file;
4415 {
4416   rtx xops[2];
4417   int regno;
4418
4419   for (regno = 0; regno < 8; ++regno)
4420     {
4421       char name[32];
4422
4423       if (! ((pic_labels_used >> regno) & 1))
4424         continue;
4425
4426       get_pc_thunk_name (name, regno);
4427
4428       if (USE_HIDDEN_LINKONCE)
4429         {
4430           tree decl;
4431
4432           decl = build_decl (FUNCTION_DECL, get_identifier (name),
4433                              error_mark_node);
4434           TREE_PUBLIC (decl) = 1;
4435           TREE_STATIC (decl) = 1;
4436           DECL_ONE_ONLY (decl) = 1;
4437
4438           (*targetm.asm_out.unique_section) (decl, 0);
4439           named_section (decl, NULL, 0);
4440
4441           (*targetm.asm_out.globalize_label) (file, name);
4442           fputs ("\t.hidden\t", file);
4443           assemble_name (file, name);
4444           fputc ('\n', file);
4445           ASM_DECLARE_FUNCTION_NAME (file, name, decl);
4446         }
4447       else
4448         {
4449           text_section ();
4450           ASM_OUTPUT_LABEL (file, name);
4451         }
4452
4453       xops[0] = gen_rtx_REG (SImode, regno);
4454       xops[1] = gen_rtx_MEM (SImode, stack_pointer_rtx);
4455       output_asm_insn ("mov{l}\t{%1, %0|%0, %1}", xops);
4456       output_asm_insn ("ret", xops);
4457     }
4458 }
4459
4460 /* Emit code for the SET_GOT patterns.  */
4461
4462 const char *
4463 output_set_got (dest)
4464      rtx dest;
4465 {
4466   rtx xops[3];
4467
4468   xops[0] = dest;
4469   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
4470
4471   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
4472     {
4473       xops[2] = gen_rtx_LABEL_REF (Pmode, gen_label_rtx ());
4474
4475       if (!flag_pic)
4476         output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
4477       else
4478         output_asm_insn ("call\t%a2", xops);
4479
4480 #if TARGET_MACHO
4481       /* Output the "canonical" label name ("Lxx$pb") here too.  This
4482          is what will be referred to by the Mach-O PIC subsystem.  */
4483       ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
4484 #endif
4485       (*targetm.asm_out.internal_label) (asm_out_file, "L",
4486                                  CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
4487
4488       if (flag_pic)
4489         output_asm_insn ("pop{l}\t%0", xops);
4490     }
4491   else
4492     {
4493       char name[32];
4494       get_pc_thunk_name (name, REGNO (dest));
4495       pic_labels_used |= 1 << REGNO (dest);
4496
4497       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
4498       xops[2] = gen_rtx_MEM (QImode, xops[2]);
4499       output_asm_insn ("call\t%X2", xops);
4500     }
4501
4502   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
4503     output_asm_insn ("add{l}\t{%1, %0|%0, %1}", xops);
4504   else if (!TARGET_MACHO)
4505     output_asm_insn ("add{l}\t{%1+[.-%a2], %0|%0, %a1+(.-%a2)}", xops);
4506
4507   return "";
4508 }
4509
4510 /* Generate an "push" pattern for input ARG.  */
4511
4512 static rtx
4513 gen_push (arg)
4514      rtx arg;
4515 {
4516   return gen_rtx_SET (VOIDmode,
4517                       gen_rtx_MEM (Pmode,
4518                                    gen_rtx_PRE_DEC (Pmode,
4519                                                     stack_pointer_rtx)),
4520                       arg);
4521 }
4522
4523 /* Return >= 0 if there is an unused call-clobbered register available
4524    for the entire function.  */
4525
4526 static unsigned int
4527 ix86_select_alt_pic_regnum ()
4528 {
4529   if (current_function_is_leaf && !current_function_profile)
4530     {
4531       int i;
4532       for (i = 2; i >= 0; --i)
4533         if (!regs_ever_live[i])
4534           return i;
4535     }
4536
4537   return INVALID_REGNUM;
4538 }
4539
4540 /* Return 1 if we need to save REGNO.  */
4541 static int
4542 ix86_save_reg (regno, maybe_eh_return)
4543      unsigned int regno;
4544      int maybe_eh_return;
4545 {
4546   if (pic_offset_table_rtx
4547       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
4548       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
4549           || current_function_profile
4550           || current_function_calls_eh_return))
4551     {
4552       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
4553         return 0;
4554       return 1;
4555     }
4556
4557   if (current_function_calls_eh_return && maybe_eh_return)
4558     {
4559       unsigned i;
4560       for (i = 0; ; i++)
4561         {
4562           unsigned test = EH_RETURN_DATA_REGNO (i);
4563           if (test == INVALID_REGNUM)
4564             break;
4565           if (test == regno)
4566             return 1;
4567         }
4568     }
4569
4570   return (regs_ever_live[regno]
4571           && !call_used_regs[regno]
4572           && !fixed_regs[regno]
4573           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
4574 }
4575
4576 /* Return number of registers to be saved on the stack.  */
4577
4578 static int
4579 ix86_nsaved_regs ()
4580 {
4581   int nregs = 0;
4582   int regno;
4583
4584   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
4585     if (ix86_save_reg (regno, true))
4586       nregs++;
4587   return nregs;
4588 }
4589
4590 /* Return the offset between two registers, one to be eliminated, and the other
4591    its replacement, at the start of a routine.  */
4592
4593 HOST_WIDE_INT
4594 ix86_initial_elimination_offset (from, to)
4595      int from;
4596      int to;
4597 {
4598   struct ix86_frame frame;
4599   ix86_compute_frame_layout (&frame);
4600
4601   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
4602     return frame.hard_frame_pointer_offset;
4603   else if (from == FRAME_POINTER_REGNUM
4604            && to == HARD_FRAME_POINTER_REGNUM)
4605     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
4606   else
4607     {
4608       if (to != STACK_POINTER_REGNUM)
4609         abort ();
4610       else if (from == ARG_POINTER_REGNUM)
4611         return frame.stack_pointer_offset;
4612       else if (from != FRAME_POINTER_REGNUM)
4613         abort ();
4614       else
4615         return frame.stack_pointer_offset - frame.frame_pointer_offset;
4616     }
4617 }
4618
4619 /* Fill structure ix86_frame about frame of currently computed function.  */
4620
4621 static void
4622 ix86_compute_frame_layout (frame)
4623      struct ix86_frame *frame;
4624 {
4625   HOST_WIDE_INT total_size;
4626   int stack_alignment_needed = cfun->stack_alignment_needed / BITS_PER_UNIT;
4627   int offset;
4628   int preferred_alignment = cfun->preferred_stack_boundary / BITS_PER_UNIT;
4629   HOST_WIDE_INT size = get_frame_size ();
4630
4631   frame->nregs = ix86_nsaved_regs ();
4632   total_size = size;
4633
4634   /* Skip return address and saved base pointer.  */
4635   offset = frame_pointer_needed ? UNITS_PER_WORD * 2 : UNITS_PER_WORD;
4636
4637   frame->hard_frame_pointer_offset = offset;
4638
4639   /* Do some sanity checking of stack_alignment_needed and
4640      preferred_alignment, since i386 port is the only using those features
4641      that may break easily.  */
4642
4643   if (size && !stack_alignment_needed)
4644     abort ();
4645   if (preferred_alignment < STACK_BOUNDARY / BITS_PER_UNIT)
4646     abort ();
4647   if (preferred_alignment > PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
4648     abort ();
4649   if (stack_alignment_needed > PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
4650     abort ();
4651
4652   if (stack_alignment_needed < STACK_BOUNDARY / BITS_PER_UNIT)
4653     stack_alignment_needed = STACK_BOUNDARY / BITS_PER_UNIT;
4654
4655   /* Register save area */
4656   offset += frame->nregs * UNITS_PER_WORD;
4657
4658   /* Va-arg area */
4659   if (ix86_save_varrargs_registers)
4660     {
4661       offset += X86_64_VARARGS_SIZE;
4662       frame->va_arg_size = X86_64_VARARGS_SIZE;
4663     }
4664   else
4665     frame->va_arg_size = 0;
4666
4667   /* Align start of frame for local function.  */
4668   frame->padding1 = ((offset + stack_alignment_needed - 1)
4669                      & -stack_alignment_needed) - offset;
4670
4671   offset += frame->padding1;
4672
4673   /* Frame pointer points here.  */
4674   frame->frame_pointer_offset = offset;
4675
4676   offset += size;
4677
4678   /* Add outgoing arguments area.  Can be skipped if we eliminated
4679      all the function calls as dead code.  */
4680   if (ACCUMULATE_OUTGOING_ARGS && !current_function_is_leaf)
4681     {
4682       offset += current_function_outgoing_args_size;
4683       frame->outgoing_arguments_size = current_function_outgoing_args_size;
4684     }
4685   else
4686     frame->outgoing_arguments_size = 0;
4687
4688   /* Align stack boundary.  Only needed if we're calling another function
4689      or using alloca.  */
4690   if (!current_function_is_leaf || current_function_calls_alloca)
4691     frame->padding2 = ((offset + preferred_alignment - 1)
4692                        & -preferred_alignment) - offset;
4693   else
4694     frame->padding2 = 0;
4695
4696   offset += frame->padding2;
4697
4698   /* We've reached end of stack frame.  */
4699   frame->stack_pointer_offset = offset;
4700
4701   /* Size prologue needs to allocate.  */
4702   frame->to_allocate =
4703     (size + frame->padding1 + frame->padding2
4704      + frame->outgoing_arguments_size + frame->va_arg_size);
4705
4706   if (TARGET_64BIT && TARGET_RED_ZONE && current_function_sp_is_unchanging
4707       && current_function_is_leaf)
4708     {
4709       frame->red_zone_size = frame->to_allocate;
4710       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
4711         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
4712     }
4713   else
4714     frame->red_zone_size = 0;
4715   frame->to_allocate -= frame->red_zone_size;
4716   frame->stack_pointer_offset -= frame->red_zone_size;
4717 #if 0
4718   fprintf (stderr, "nregs: %i\n", frame->nregs);
4719   fprintf (stderr, "size: %i\n", size);
4720   fprintf (stderr, "alignment1: %i\n", stack_alignment_needed);
4721   fprintf (stderr, "padding1: %i\n", frame->padding1);
4722   fprintf (stderr, "va_arg: %i\n", frame->va_arg_size);
4723   fprintf (stderr, "padding2: %i\n", frame->padding2);
4724   fprintf (stderr, "to_allocate: %i\n", frame->to_allocate);
4725   fprintf (stderr, "red_zone_size: %i\n", frame->red_zone_size);
4726   fprintf (stderr, "frame_pointer_offset: %i\n", frame->frame_pointer_offset);
4727   fprintf (stderr, "hard_frame_pointer_offset: %i\n",
4728            frame->hard_frame_pointer_offset);
4729   fprintf (stderr, "stack_pointer_offset: %i\n", frame->stack_pointer_offset);
4730 #endif
4731 }
4732
4733 /* Emit code to save registers in the prologue.  */
4734
4735 static void
4736 ix86_emit_save_regs ()
4737 {
4738   register int regno;
4739   rtx insn;
4740
4741   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
4742     if (ix86_save_reg (regno, true))
4743       {
4744         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
4745         RTX_FRAME_RELATED_P (insn) = 1;
4746       }
4747 }
4748
4749 /* Emit code to save registers using MOV insns.  First register
4750    is restored from POINTER + OFFSET.  */
4751 static void
4752 ix86_emit_save_regs_using_mov (pointer, offset)
4753      rtx pointer;
4754      HOST_WIDE_INT offset;
4755 {
4756   int regno;
4757   rtx insn;
4758
4759   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4760     if (ix86_save_reg (regno, true))
4761       {
4762         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
4763                                                Pmode, offset),
4764                                gen_rtx_REG (Pmode, regno));
4765         RTX_FRAME_RELATED_P (insn) = 1;
4766         offset += UNITS_PER_WORD;
4767       }
4768 }
4769
4770 /* Expand the prologue into a bunch of separate insns.  */
4771
4772 void
4773 ix86_expand_prologue ()
4774 {
4775   rtx insn;
4776   bool pic_reg_used;
4777   struct ix86_frame frame;
4778   int use_mov = 0;
4779   HOST_WIDE_INT allocate;
4780
4781   ix86_compute_frame_layout (&frame);
4782   if (!optimize_size)
4783     {
4784       int count = frame.nregs;
4785
4786       /* The fast prologue uses move instead of push to save registers.  This
4787          is significantly longer, but also executes faster as modern hardware
4788          can execute the moves in parallel, but can't do that for push/pop.
4789          
4790          Be careful about choosing what prologue to emit:  When function takes
4791          many instructions to execute we may use slow version as well as in
4792          case function is known to be outside hot spot (this is known with
4793          feedback only).  Weight the size of function by number of registers
4794          to save as it is cheap to use one or two push instructions but very
4795          slow to use many of them.  */
4796       if (count)
4797         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
4798       if (cfun->function_frequency < FUNCTION_FREQUENCY_NORMAL
4799           || (flag_branch_probabilities
4800               && cfun->function_frequency < FUNCTION_FREQUENCY_HOT))
4801         use_fast_prologue_epilogue = 0;
4802       else
4803         use_fast_prologue_epilogue = !expensive_function_p (count);
4804       if (TARGET_PROLOGUE_USING_MOVE)
4805         use_mov = use_fast_prologue_epilogue;
4806     }
4807
4808   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
4809      slower on all targets.  Also sdb doesn't like it.  */
4810
4811   if (frame_pointer_needed)
4812     {
4813       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
4814       RTX_FRAME_RELATED_P (insn) = 1;
4815
4816       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
4817       RTX_FRAME_RELATED_P (insn) = 1;
4818     }
4819
4820   allocate = frame.to_allocate;
4821   /* In case we are dealing only with single register and empty frame,
4822      push is equivalent of the mov+add sequence.  */
4823   if (allocate == 0 && frame.nregs <= 1)
4824     use_mov = 0;
4825
4826   if (!use_mov)
4827     ix86_emit_save_regs ();
4828   else
4829     allocate += frame.nregs * UNITS_PER_WORD;
4830
4831   if (allocate == 0)
4832     ;
4833   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
4834     {
4835       insn = emit_insn (gen_pro_epilogue_adjust_stack
4836                         (stack_pointer_rtx, stack_pointer_rtx,
4837                          GEN_INT (-allocate)));
4838       RTX_FRAME_RELATED_P (insn) = 1;
4839     }
4840   else
4841     {
4842       /* ??? Is this only valid for Win32?  */
4843
4844       rtx arg0, sym;
4845
4846       if (TARGET_64BIT)
4847         abort ();
4848
4849       arg0 = gen_rtx_REG (SImode, 0);
4850       emit_move_insn (arg0, GEN_INT (allocate));
4851
4852       sym = gen_rtx_MEM (FUNCTION_MODE,
4853                          gen_rtx_SYMBOL_REF (Pmode, "_alloca"));
4854       insn = emit_call_insn (gen_call (sym, const0_rtx, constm1_rtx));
4855
4856       CALL_INSN_FUNCTION_USAGE (insn)
4857         = gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_USE (VOIDmode, arg0),
4858                              CALL_INSN_FUNCTION_USAGE (insn));
4859
4860       /* Don't allow scheduling pass to move insns across __alloca
4861          call.  */
4862       emit_insn (gen_blockage (const0_rtx));
4863     }
4864   if (use_mov)
4865     {
4866       if (!frame_pointer_needed || !frame.to_allocate)
4867         ix86_emit_save_regs_using_mov (stack_pointer_rtx, frame.to_allocate);
4868       else
4869         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
4870                                        -frame.nregs * UNITS_PER_WORD);
4871     }
4872
4873 #ifdef SUBTARGET_PROLOGUE
4874   SUBTARGET_PROLOGUE;
4875 #endif
4876
4877   pic_reg_used = false;
4878   if (pic_offset_table_rtx
4879       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
4880           || current_function_profile))
4881     {
4882       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
4883
4884       if (alt_pic_reg_used != INVALID_REGNUM)
4885         REGNO (pic_offset_table_rtx) = alt_pic_reg_used;
4886
4887       pic_reg_used = true;
4888     }
4889
4890   if (pic_reg_used)
4891     {
4892       insn = emit_insn (gen_set_got (pic_offset_table_rtx));
4893
4894       /* Even with accurate pre-reload life analysis, we can wind up
4895          deleting all references to the pic register after reload.
4896          Consider if cross-jumping unifies two sides of a branch
4897          controlled by a comparison vs the only read from a global.
4898          In which case, allow the set_got to be deleted, though we're
4899          too late to do anything about the ebx save in the prologue.  */
4900       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
4901     }
4902
4903   /* Prevent function calls from be scheduled before the call to mcount.
4904      In the pic_reg_used case, make sure that the got load isn't deleted.  */
4905   if (current_function_profile)
4906     emit_insn (gen_blockage (pic_reg_used ? pic_offset_table_rtx : const0_rtx));
4907 }
4908
4909 /* Emit code to restore saved registers using MOV insns.  First register
4910    is restored from POINTER + OFFSET.  */
4911 static void
4912 ix86_emit_restore_regs_using_mov (pointer, offset, maybe_eh_return)
4913      rtx pointer;
4914      int offset;
4915      int maybe_eh_return;
4916 {
4917   int regno;
4918
4919   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4920     if (ix86_save_reg (regno, maybe_eh_return))
4921       {
4922         emit_move_insn (gen_rtx_REG (Pmode, regno),
4923                         adjust_address (gen_rtx_MEM (Pmode, pointer),
4924                                         Pmode, offset));
4925         offset += UNITS_PER_WORD;
4926       }
4927 }
4928
4929 /* Restore function stack, frame, and registers.  */
4930
4931 void
4932 ix86_expand_epilogue (style)
4933      int style;
4934 {
4935   int regno;
4936   int sp_valid = !frame_pointer_needed || current_function_sp_is_unchanging;
4937   struct ix86_frame frame;
4938   HOST_WIDE_INT offset;
4939
4940   ix86_compute_frame_layout (&frame);
4941
4942   /* Calculate start of saved registers relative to ebp.  Special care
4943      must be taken for the normal return case of a function using
4944      eh_return: the eax and edx registers are marked as saved, but not
4945      restored along this path.  */
4946   offset = frame.nregs;
4947   if (current_function_calls_eh_return && style != 2)
4948     offset -= 2;
4949   offset *= -UNITS_PER_WORD;
4950
4951   /* If we're only restoring one register and sp is not valid then
4952      using a move instruction to restore the register since it's
4953      less work than reloading sp and popping the register.
4954
4955      The default code result in stack adjustment using add/lea instruction,
4956      while this code results in LEAVE instruction (or discrete equivalent),
4957      so it is profitable in some other cases as well.  Especially when there
4958      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
4959      and there is exactly one register to pop. This heuristic may need some
4960      tuning in future.  */
4961   if ((!sp_valid && frame.nregs <= 1)
4962       || (TARGET_EPILOGUE_USING_MOVE
4963           && use_fast_prologue_epilogue
4964           && (frame.nregs > 1 || frame.to_allocate))
4965       || (frame_pointer_needed && !frame.nregs && frame.to_allocate)
4966       || (frame_pointer_needed && TARGET_USE_LEAVE
4967           && use_fast_prologue_epilogue && frame.nregs == 1)
4968       || current_function_calls_eh_return)
4969     {
4970       /* Restore registers.  We can use ebp or esp to address the memory
4971          locations.  If both are available, default to ebp, since offsets
4972          are known to be small.  Only exception is esp pointing directly to the
4973          end of block of saved registers, where we may simplify addressing
4974          mode.  */
4975
4976       if (!frame_pointer_needed || (sp_valid && !frame.to_allocate))
4977         ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
4978                                           frame.to_allocate, style == 2);
4979       else
4980         ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
4981                                           offset, style == 2);
4982
4983       /* eh_return epilogues need %ecx added to the stack pointer.  */
4984       if (style == 2)
4985         {
4986           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
4987
4988           if (frame_pointer_needed)
4989             {
4990               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
4991               tmp = plus_constant (tmp, UNITS_PER_WORD);
4992               emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
4993
4994               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
4995               emit_move_insn (hard_frame_pointer_rtx, tmp);
4996
4997               emit_insn (gen_pro_epilogue_adjust_stack
4998                          (stack_pointer_rtx, sa, const0_rtx));
4999             }
5000           else
5001             {
5002               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
5003               tmp = plus_constant (tmp, (frame.to_allocate
5004                                          + frame.nregs * UNITS_PER_WORD));
5005               emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
5006             }
5007         }
5008       else if (!frame_pointer_needed)
5009         emit_insn (gen_pro_epilogue_adjust_stack
5010                    (stack_pointer_rtx, stack_pointer_rtx,
5011                     GEN_INT (frame.to_allocate
5012                              + frame.nregs * UNITS_PER_WORD)));
5013       /* If not an i386, mov & pop is faster than "leave".  */
5014       else if (TARGET_USE_LEAVE || optimize_size || !use_fast_prologue_epilogue)
5015         emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5016       else
5017         {
5018           emit_insn (gen_pro_epilogue_adjust_stack (stack_pointer_rtx,
5019                                                     hard_frame_pointer_rtx,
5020                                                     const0_rtx));
5021           if (TARGET_64BIT)
5022             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5023           else
5024             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5025         }
5026     }
5027   else
5028     {
5029       /* First step is to deallocate the stack frame so that we can
5030          pop the registers.  */
5031       if (!sp_valid)
5032         {
5033           if (!frame_pointer_needed)
5034             abort ();
5035           emit_insn (gen_pro_epilogue_adjust_stack (stack_pointer_rtx,
5036                                                     hard_frame_pointer_rtx,
5037                                                     GEN_INT (offset)));
5038         }
5039       else if (frame.to_allocate)
5040         emit_insn (gen_pro_epilogue_adjust_stack
5041                    (stack_pointer_rtx, stack_pointer_rtx,
5042                     GEN_INT (frame.to_allocate)));
5043
5044       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5045         if (ix86_save_reg (regno, false))
5046           {
5047             if (TARGET_64BIT)
5048               emit_insn (gen_popdi1 (gen_rtx_REG (Pmode, regno)));
5049             else
5050               emit_insn (gen_popsi1 (gen_rtx_REG (Pmode, regno)));
5051           }
5052       if (frame_pointer_needed)
5053         {
5054           /* Leave results in shorter dependency chains on CPUs that are
5055              able to grok it fast.  */
5056           if (TARGET_USE_LEAVE)
5057             emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5058           else if (TARGET_64BIT)
5059             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5060           else
5061             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5062         }
5063     }
5064
5065   /* Sibcall epilogues don't want a return instruction.  */
5066   if (style == 0)
5067     return;
5068
5069   if (current_function_pops_args && current_function_args_size)
5070     {
5071       rtx popc = GEN_INT (current_function_pops_args);
5072
5073       /* i386 can only pop 64K bytes.  If asked to pop more, pop
5074          return address, do explicit add, and jump indirectly to the
5075          caller.  */
5076
5077       if (current_function_pops_args >= 65536)
5078         {
5079           rtx ecx = gen_rtx_REG (SImode, 2);
5080
5081           /* There are is no "pascal" calling convention in 64bit ABI.  */
5082           if (TARGET_64BIT)
5083             abort ();
5084
5085           emit_insn (gen_popsi1 (ecx));
5086           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, popc));
5087           emit_jump_insn (gen_return_indirect_internal (ecx));
5088         }
5089       else
5090         emit_jump_insn (gen_return_pop_internal (popc));
5091     }
5092   else
5093     emit_jump_insn (gen_return_internal ());
5094 }
5095
5096 /* Reset from the function's potential modifications.  */
5097
5098 static void
5099 ix86_output_function_epilogue (file, size)
5100      FILE *file ATTRIBUTE_UNUSED;
5101      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
5102 {
5103   if (pic_offset_table_rtx)
5104     REGNO (pic_offset_table_rtx) = REAL_PIC_OFFSET_TABLE_REGNUM;
5105 }
5106 \f
5107 /* Extract the parts of an RTL expression that is a valid memory address
5108    for an instruction.  Return 0 if the structure of the address is
5109    grossly off.  Return -1 if the address contains ASHIFT, so it is not
5110    strictly valid, but still used for computing length of lea instruction.
5111    */
5112
5113 static int
5114 ix86_decompose_address (addr, out)
5115      register rtx addr;
5116      struct ix86_address *out;
5117 {
5118   rtx base = NULL_RTX;
5119   rtx index = NULL_RTX;
5120   rtx disp = NULL_RTX;
5121   HOST_WIDE_INT scale = 1;
5122   rtx scale_rtx = NULL_RTX;
5123   int retval = 1;
5124
5125   if (REG_P (addr) || GET_CODE (addr) == SUBREG)
5126     base = addr;
5127   else if (GET_CODE (addr) == PLUS)
5128     {
5129       rtx op0 = XEXP (addr, 0);
5130       rtx op1 = XEXP (addr, 1);
5131       enum rtx_code code0 = GET_CODE (op0);
5132       enum rtx_code code1 = GET_CODE (op1);
5133
5134       if (code0 == REG || code0 == SUBREG)
5135         {
5136           if (code1 == REG || code1 == SUBREG)
5137             index = op0, base = op1;    /* index + base */
5138           else
5139             base = op0, disp = op1;     /* base + displacement */
5140         }
5141       else if (code0 == MULT)
5142         {
5143           index = XEXP (op0, 0);
5144           scale_rtx = XEXP (op0, 1);
5145           if (code1 == REG || code1 == SUBREG)
5146             base = op1;                 /* index*scale + base */
5147           else
5148             disp = op1;                 /* index*scale + disp */
5149         }
5150       else if (code0 == PLUS && GET_CODE (XEXP (op0, 0)) == MULT)
5151         {
5152           index = XEXP (XEXP (op0, 0), 0);      /* index*scale + base + disp */
5153           scale_rtx = XEXP (XEXP (op0, 0), 1);
5154           base = XEXP (op0, 1);
5155           disp = op1;
5156         }
5157       else if (code0 == PLUS)
5158         {
5159           index = XEXP (op0, 0);        /* index + base + disp */
5160           base = XEXP (op0, 1);
5161           disp = op1;
5162         }
5163       else
5164         return 0;
5165     }
5166   else if (GET_CODE (addr) == MULT)
5167     {
5168       index = XEXP (addr, 0);           /* index*scale */
5169       scale_rtx = XEXP (addr, 1);
5170     }
5171   else if (GET_CODE (addr) == ASHIFT)
5172     {
5173       rtx tmp;
5174
5175       /* We're called for lea too, which implements ashift on occasion.  */
5176       index = XEXP (addr, 0);
5177       tmp = XEXP (addr, 1);
5178       if (GET_CODE (tmp) != CONST_INT)
5179         return 0;
5180       scale = INTVAL (tmp);
5181       if ((unsigned HOST_WIDE_INT) scale > 3)
5182         return 0;
5183       scale = 1 << scale;
5184       retval = -1;
5185     }
5186   else
5187     disp = addr;                        /* displacement */
5188
5189   /* Extract the integral value of scale.  */
5190   if (scale_rtx)
5191     {
5192       if (GET_CODE (scale_rtx) != CONST_INT)
5193         return 0;
5194       scale = INTVAL (scale_rtx);
5195     }
5196
5197   /* Allow arg pointer and stack pointer as index if there is not scaling */
5198   if (base && index && scale == 1
5199       && (index == arg_pointer_rtx || index == frame_pointer_rtx
5200           || index == stack_pointer_rtx))
5201     {
5202       rtx tmp = base;
5203       base = index;
5204       index = tmp;
5205     }
5206
5207   /* Special case: %ebp cannot be encoded as a base without a displacement.  */
5208   if ((base == hard_frame_pointer_rtx
5209        || base == frame_pointer_rtx
5210        || base == arg_pointer_rtx) && !disp)
5211     disp = const0_rtx;
5212
5213   /* Special case: on K6, [%esi] makes the instruction vector decoded.
5214      Avoid this by transforming to [%esi+0].  */
5215   if (ix86_cpu == PROCESSOR_K6 && !optimize_size
5216       && base && !index && !disp
5217       && REG_P (base)
5218       && REGNO_REG_CLASS (REGNO (base)) == SIREG)
5219     disp = const0_rtx;
5220
5221   /* Special case: encode reg+reg instead of reg*2.  */
5222   if (!base && index && scale && scale == 2)
5223     base = index, scale = 1;
5224
5225   /* Special case: scaling cannot be encoded without base or displacement.  */
5226   if (!base && !disp && index && scale != 1)
5227     disp = const0_rtx;
5228
5229   out->base = base;
5230   out->index = index;
5231   out->disp = disp;
5232   out->scale = scale;
5233
5234   return retval;
5235 }
5236 \f
5237 /* Return cost of the memory address x.
5238    For i386, it is better to use a complex address than let gcc copy
5239    the address into a reg and make a new pseudo.  But not if the address
5240    requires to two regs - that would mean more pseudos with longer
5241    lifetimes.  */
5242 int
5243 ix86_address_cost (x)
5244      rtx x;
5245 {
5246   struct ix86_address parts;
5247   int cost = 1;
5248
5249   if (!ix86_decompose_address (x, &parts))
5250     abort ();
5251
5252   if (parts.base && GET_CODE (parts.base) == SUBREG)
5253     parts.base = SUBREG_REG (parts.base);
5254   if (parts.index && GET_CODE (parts.index) == SUBREG)
5255     parts.index = SUBREG_REG (parts.index);
5256
5257   /* More complex memory references are better.  */
5258   if (parts.disp && parts.disp != const0_rtx)
5259     cost--;
5260
5261   /* Attempt to minimize number of registers in the address.  */
5262   if ((parts.base
5263        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
5264       || (parts.index
5265           && (!REG_P (parts.index)
5266               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
5267     cost++;
5268
5269   if (parts.base
5270       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
5271       && parts.index
5272       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
5273       && parts.base != parts.index)
5274     cost++;
5275
5276   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
5277      since it's predecode logic can't detect the length of instructions
5278      and it degenerates to vector decoded.  Increase cost of such
5279      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
5280      to split such addresses or even refuse such addresses at all.
5281
5282      Following addressing modes are affected:
5283       [base+scale*index]
5284       [scale*index+disp]
5285       [base+index]
5286
5287      The first and last case  may be avoidable by explicitly coding the zero in
5288      memory address, but I don't have AMD-K6 machine handy to check this
5289      theory.  */
5290
5291   if (TARGET_K6
5292       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
5293           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
5294           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
5295     cost += 10;
5296
5297   return cost;
5298 }
5299 \f
5300 /* If X is a machine specific address (i.e. a symbol or label being
5301    referenced as a displacement from the GOT implemented using an
5302    UNSPEC), then return the base term.  Otherwise return X.  */
5303
5304 rtx
5305 ix86_find_base_term (x)
5306      rtx x;
5307 {
5308   rtx term;
5309
5310   if (TARGET_64BIT)
5311     {
5312       if (GET_CODE (x) != CONST)
5313         return x;
5314       term = XEXP (x, 0);
5315       if (GET_CODE (term) == PLUS
5316           && (GET_CODE (XEXP (term, 1)) == CONST_INT
5317               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
5318         term = XEXP (term, 0);
5319       if (GET_CODE (term) != UNSPEC
5320           || XINT (term, 1) != UNSPEC_GOTPCREL)
5321         return x;
5322
5323       term = XVECEXP (term, 0, 0);
5324
5325       if (GET_CODE (term) != SYMBOL_REF
5326           && GET_CODE (term) != LABEL_REF)
5327         return x;
5328
5329       return term;
5330     }
5331
5332   if (GET_CODE (x) != PLUS
5333       || XEXP (x, 0) != pic_offset_table_rtx
5334       || GET_CODE (XEXP (x, 1)) != CONST)
5335     return x;
5336
5337   term = XEXP (XEXP (x, 1), 0);
5338
5339   if (GET_CODE (term) == PLUS && GET_CODE (XEXP (term, 1)) == CONST_INT)
5340     term = XEXP (term, 0);
5341
5342   if (GET_CODE (term) != UNSPEC
5343       || XINT (term, 1) != UNSPEC_GOTOFF)
5344     return x;
5345
5346   term = XVECEXP (term, 0, 0);
5347
5348   if (GET_CODE (term) != SYMBOL_REF
5349       && GET_CODE (term) != LABEL_REF)
5350     return x;
5351
5352   return term;
5353 }
5354 \f
5355 /* Determine if a given RTX is a valid constant.  We already know this
5356    satisfies CONSTANT_P.  */
5357
5358 bool
5359 legitimate_constant_p (x)
5360      rtx x;
5361 {
5362   rtx inner;
5363
5364   switch (GET_CODE (x))
5365     {
5366     case SYMBOL_REF:
5367       /* TLS symbols are not constant.  */
5368       if (tls_symbolic_operand (x, Pmode))
5369         return false;
5370       break;
5371
5372     case CONST:
5373       inner = XEXP (x, 0);
5374
5375       /* Offsets of TLS symbols are never valid.
5376          Discourage CSE from creating them.  */
5377       if (GET_CODE (inner) == PLUS
5378           && tls_symbolic_operand (XEXP (inner, 0), Pmode))
5379         return false;
5380
5381       /* Only some unspecs are valid as "constants".  */
5382       if (GET_CODE (inner) == UNSPEC)
5383         switch (XINT (inner, 1))
5384           {
5385           case UNSPEC_TPOFF:
5386             return local_exec_symbolic_operand (XVECEXP (inner, 0, 0), Pmode);
5387           default:
5388             return false;
5389           }
5390       break;
5391
5392     default:
5393       break;
5394     }
5395
5396   /* Otherwise we handle everything else in the move patterns.  */
5397   return true;
5398 }
5399
5400 /* Determine if it's legal to put X into the constant pool.  This
5401    is not possible for the address of thread-local symbols, which
5402    is checked above.  */
5403
5404 static bool
5405 ix86_cannot_force_const_mem (x)
5406      rtx x;
5407 {
5408   return !legitimate_constant_p (x);
5409 }
5410
5411 /* Determine if a given RTX is a valid constant address.  */
5412
5413 bool
5414 constant_address_p (x)
5415      rtx x;
5416 {
5417   switch (GET_CODE (x))
5418     {
5419     case LABEL_REF:
5420     case CONST_INT:
5421       return true;
5422
5423     case CONST_DOUBLE:
5424       return TARGET_64BIT;
5425
5426     case CONST:
5427       /* For Mach-O, really believe the CONST.  */
5428       if (TARGET_MACHO)
5429         return true;
5430       /* Otherwise fall through.  */
5431     case SYMBOL_REF:
5432       return !flag_pic && legitimate_constant_p (x);
5433
5434     default:
5435       return false;
5436     }
5437 }
5438
5439 /* Nonzero if the constant value X is a legitimate general operand
5440    when generating PIC code.  It is given that flag_pic is on and
5441    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
5442
5443 bool
5444 legitimate_pic_operand_p (x)
5445      rtx x;
5446 {
5447   rtx inner;
5448
5449   switch (GET_CODE (x))
5450     {
5451     case CONST:
5452       inner = XEXP (x, 0);
5453
5454       /* Only some unspecs are valid as "constants".  */
5455       if (GET_CODE (inner) == UNSPEC)
5456         switch (XINT (inner, 1))
5457           {
5458           case UNSPEC_TPOFF:
5459             return local_exec_symbolic_operand (XVECEXP (inner, 0, 0), Pmode);
5460           default:
5461             return false;
5462           }
5463       /* FALLTHRU */
5464
5465     case SYMBOL_REF:
5466     case LABEL_REF:
5467       return legitimate_pic_address_disp_p (x);
5468
5469     default:
5470       return true;
5471     }
5472 }
5473
5474 /* Determine if a given CONST RTX is a valid memory displacement
5475    in PIC mode.  */
5476
5477 int
5478 legitimate_pic_address_disp_p (disp)
5479      register rtx disp;
5480 {
5481   bool saw_plus;
5482
5483   /* In 64bit mode we can allow direct addresses of symbols and labels
5484      when they are not dynamic symbols.  */
5485   if (TARGET_64BIT)
5486     {
5487       /* TLS references should always be enclosed in UNSPEC.  */
5488       if (tls_symbolic_operand (disp, GET_MODE (disp)))
5489         return 0;
5490       if (GET_CODE (disp) == SYMBOL_REF
5491           && ix86_cmodel == CM_SMALL_PIC
5492           && (CONSTANT_POOL_ADDRESS_P (disp)
5493               || SYMBOL_REF_FLAG (disp)))
5494         return 1;
5495       if (GET_CODE (disp) == LABEL_REF)
5496         return 1;
5497       if (GET_CODE (disp) == CONST
5498           && GET_CODE (XEXP (disp, 0)) == PLUS
5499           && ((GET_CODE (XEXP (XEXP (disp, 0), 0)) == SYMBOL_REF
5500                && ix86_cmodel == CM_SMALL_PIC
5501                && (CONSTANT_POOL_ADDRESS_P (XEXP (XEXP (disp, 0), 0))
5502                    || SYMBOL_REF_FLAG (XEXP (XEXP (disp, 0), 0))))
5503               || GET_CODE (XEXP (XEXP (disp, 0), 0)) == LABEL_REF)
5504           && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT
5505           && INTVAL (XEXP (XEXP (disp, 0), 1)) < 16*1024*1024
5506           && INTVAL (XEXP (XEXP (disp, 0), 1)) >= -16*1024*1024)
5507         return 1;
5508     }
5509   if (GET_CODE (disp) != CONST)
5510     return 0;
5511   disp = XEXP (disp, 0);
5512
5513   if (TARGET_64BIT)
5514     {
5515       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
5516          of GOT tables.  We should not need these anyway.  */
5517       if (GET_CODE (disp) != UNSPEC
5518           || XINT (disp, 1) != UNSPEC_GOTPCREL)
5519         return 0;
5520
5521       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
5522           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
5523         return 0;
5524       return 1;
5525     }
5526
5527   saw_plus = false;
5528   if (GET_CODE (disp) == PLUS)
5529     {
5530       if (GET_CODE (XEXP (disp, 1)) != CONST_INT)
5531         return 0;
5532       disp = XEXP (disp, 0);
5533       saw_plus = true;
5534     }
5535
5536   /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O.  */
5537   if (TARGET_MACHO && GET_CODE (disp) == MINUS)
5538     {
5539       if (GET_CODE (XEXP (disp, 0)) == LABEL_REF
5540           || GET_CODE (XEXP (disp, 0)) == SYMBOL_REF)
5541         if (GET_CODE (XEXP (disp, 1)) == SYMBOL_REF)
5542           {
5543             const char *sym_name = XSTR (XEXP (disp, 1), 0);
5544             if (strstr (sym_name, "$pb") != 0)
5545               return 1;
5546           }
5547     }
5548
5549   if (GET_CODE (disp) != UNSPEC)
5550     return 0;
5551
5552   switch (XINT (disp, 1))
5553     {
5554     case UNSPEC_GOT:
5555       if (saw_plus)
5556         return false;
5557       return GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF;
5558     case UNSPEC_GOTOFF:
5559       return local_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5560     case UNSPEC_GOTTPOFF:
5561     case UNSPEC_GOTNTPOFF:
5562     case UNSPEC_INDNTPOFF:
5563       if (saw_plus)
5564         return false;
5565       return initial_exec_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5566     case UNSPEC_NTPOFF:
5567       return local_exec_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5568     case UNSPEC_DTPOFF:
5569       return local_dynamic_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5570     }
5571
5572   return 0;
5573 }
5574
5575 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression that is a valid
5576    memory address for an instruction.  The MODE argument is the machine mode
5577    for the MEM expression that wants to use this address.
5578
5579    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
5580    convert common non-canonical forms to canonical form so that they will
5581    be recognized.  */
5582
5583 int
5584 legitimate_address_p (mode, addr, strict)
5585      enum machine_mode mode;
5586      register rtx addr;
5587      int strict;
5588 {
5589   struct ix86_address parts;
5590   rtx base, index, disp;
5591   HOST_WIDE_INT scale;
5592   const char *reason = NULL;
5593   rtx reason_rtx = NULL_RTX;
5594
5595   if (TARGET_DEBUG_ADDR)
5596     {
5597       fprintf (stderr,
5598                "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
5599                GET_MODE_NAME (mode), strict);
5600       debug_rtx (addr);
5601     }
5602
5603   if (GET_CODE (addr) == UNSPEC && XINT (addr, 1) == UNSPEC_TP)
5604     {
5605       if (TARGET_DEBUG_ADDR)
5606         fprintf (stderr, "Success.\n");
5607       return TRUE;
5608     }
5609
5610   if (ix86_decompose_address (addr, &parts) <= 0)
5611     {
5612       reason = "decomposition failed";
5613       goto report_error;
5614     }
5615
5616   base = parts.base;
5617   index = parts.index;
5618   disp = parts.disp;
5619   scale = parts.scale;
5620
5621   /* Validate base register.
5622
5623      Don't allow SUBREG's here, it can lead to spill failures when the base
5624      is one word out of a two word structure, which is represented internally
5625      as a DImode int.  */
5626
5627   if (base)
5628     {
5629       rtx reg;
5630       reason_rtx = base;
5631
5632       if (GET_CODE (base) == SUBREG)
5633         reg = SUBREG_REG (base);
5634       else
5635         reg = base;
5636
5637       if (GET_CODE (reg) != REG)
5638         {
5639           reason = "base is not a register";
5640           goto report_error;
5641         }
5642
5643       if (GET_MODE (base) != Pmode)
5644         {
5645           reason = "base is not in Pmode";
5646           goto report_error;
5647         }
5648
5649       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
5650           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
5651         {
5652           reason = "base is not valid";
5653           goto report_error;
5654         }
5655     }
5656
5657   /* Validate index register.
5658
5659      Don't allow SUBREG's here, it can lead to spill failures when the index
5660      is one word out of a two word structure, which is represented internally
5661      as a DImode int.  */
5662
5663   if (index)
5664     {
5665       rtx reg;
5666       reason_rtx = index;
5667
5668       if (GET_CODE (index) == SUBREG)
5669         reg = SUBREG_REG (index);
5670       else
5671         reg = index;
5672
5673       if (GET_CODE (reg) != REG)
5674         {
5675           reason = "index is not a register";
5676           goto report_error;
5677         }
5678
5679       if (GET_MODE (index) != Pmode)
5680         {
5681           reason = "index is not in Pmode";
5682           goto report_error;
5683         }
5684
5685       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
5686           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
5687         {
5688           reason = "index is not valid";
5689           goto report_error;
5690         }
5691     }
5692
5693   /* Validate scale factor.  */
5694   if (scale != 1)
5695     {
5696       reason_rtx = GEN_INT (scale);
5697       if (!index)
5698         {
5699           reason = "scale without index";
5700           goto report_error;
5701         }
5702
5703       if (scale != 2 && scale != 4 && scale != 8)
5704         {
5705           reason = "scale is not a valid multiplier";
5706           goto report_error;
5707         }
5708     }
5709
5710   /* Validate displacement.  */
5711   if (disp)
5712     {
5713       reason_rtx = disp;
5714
5715       if (GET_CODE (disp) == CONST
5716           && GET_CODE (XEXP (disp, 0)) == UNSPEC)
5717         switch (XINT (XEXP (disp, 0), 1))
5718           {
5719           case UNSPEC_GOT:
5720           case UNSPEC_GOTOFF:
5721           case UNSPEC_GOTPCREL:
5722             if (!flag_pic)
5723               abort ();
5724             goto is_legitimate_pic;
5725
5726           case UNSPEC_GOTTPOFF:
5727           case UNSPEC_GOTNTPOFF:
5728           case UNSPEC_INDNTPOFF:
5729           case UNSPEC_NTPOFF:
5730           case UNSPEC_DTPOFF:
5731             break;
5732
5733           default:
5734             reason = "invalid address unspec";
5735             goto report_error;
5736           }
5737
5738       else if (flag_pic && (SYMBOLIC_CONST (disp)
5739 #if TARGET_MACHO
5740                             && !machopic_operand_p (disp)
5741 #endif
5742                             ))
5743         {
5744         is_legitimate_pic:
5745           if (TARGET_64BIT && (index || base))
5746             {
5747               /* foo@dtpoff(%rX) is ok.  */
5748               if (GET_CODE (disp) != CONST
5749                   || GET_CODE (XEXP (disp, 0)) != PLUS
5750                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
5751                   || GET_CODE (XEXP (XEXP (disp, 0), 1)) != CONST_INT
5752                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
5753                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
5754                 {
5755                   reason = "non-constant pic memory reference";
5756                   goto report_error;
5757                 }
5758             }
5759           else if (! legitimate_pic_address_disp_p (disp))
5760             {
5761               reason = "displacement is an invalid pic construct";
5762               goto report_error;
5763             }
5764
5765           /* This code used to verify that a symbolic pic displacement
5766              includes the pic_offset_table_rtx register.
5767
5768              While this is good idea, unfortunately these constructs may
5769              be created by "adds using lea" optimization for incorrect
5770              code like:
5771
5772              int a;
5773              int foo(int i)
5774                {
5775                  return *(&a+i);
5776                }
5777
5778              This code is nonsensical, but results in addressing
5779              GOT table with pic_offset_table_rtx base.  We can't
5780              just refuse it easily, since it gets matched by
5781              "addsi3" pattern, that later gets split to lea in the
5782              case output register differs from input.  While this
5783              can be handled by separate addsi pattern for this case
5784              that never results in lea, this seems to be easier and
5785              correct fix for crash to disable this test.  */
5786         }
5787       else if (!CONSTANT_ADDRESS_P (disp))
5788         {
5789           reason = "displacement is not constant";
5790           goto report_error;
5791         }
5792       else if (TARGET_64BIT && !x86_64_sign_extended_value (disp))
5793         {
5794           reason = "displacement is out of range";
5795           goto report_error;
5796         }
5797       else if (!TARGET_64BIT && GET_CODE (disp) == CONST_DOUBLE)
5798         {
5799           reason = "displacement is a const_double";
5800           goto report_error;
5801         }
5802     }
5803
5804   /* Everything looks valid.  */
5805   if (TARGET_DEBUG_ADDR)
5806     fprintf (stderr, "Success.\n");
5807   return TRUE;
5808
5809  report_error:
5810   if (TARGET_DEBUG_ADDR)
5811     {
5812       fprintf (stderr, "Error: %s\n", reason);
5813       debug_rtx (reason_rtx);
5814     }
5815   return FALSE;
5816 }
5817 \f
5818 /* Return an unique alias set for the GOT.  */
5819
5820 static HOST_WIDE_INT
5821 ix86_GOT_alias_set ()
5822 {
5823   static HOST_WIDE_INT set = -1;
5824   if (set == -1)
5825     set = new_alias_set ();
5826   return set;
5827 }
5828
5829 /* Return a legitimate reference for ORIG (an address) using the
5830    register REG.  If REG is 0, a new pseudo is generated.
5831
5832    There are two types of references that must be handled:
5833
5834    1. Global data references must load the address from the GOT, via
5835       the PIC reg.  An insn is emitted to do this load, and the reg is
5836       returned.
5837
5838    2. Static data references, constant pool addresses, and code labels
5839       compute the address as an offset from the GOT, whose base is in
5840       the PIC reg.  Static data objects have SYMBOL_REF_FLAG set to
5841       differentiate them from global data objects.  The returned
5842       address is the PIC reg + an unspec constant.
5843
5844    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
5845    reg also appears in the address.  */
5846
5847 rtx
5848 legitimize_pic_address (orig, reg)
5849      rtx orig;
5850      rtx reg;
5851 {
5852   rtx addr = orig;
5853   rtx new = orig;
5854   rtx base;
5855
5856 #if TARGET_MACHO
5857   if (reg == 0)
5858     reg = gen_reg_rtx (Pmode);
5859   /* Use the generic Mach-O PIC machinery.  */
5860   return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
5861 #endif
5862
5863   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
5864     new = addr;
5865   else if (!TARGET_64BIT && local_symbolic_operand (addr, Pmode))
5866     {
5867       /* This symbol may be referenced via a displacement from the PIC
5868          base address (@GOTOFF).  */
5869
5870       if (reload_in_progress)
5871         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
5872       new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
5873       new = gen_rtx_CONST (Pmode, new);
5874       new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
5875
5876       if (reg != 0)
5877         {
5878           emit_move_insn (reg, new);
5879           new = reg;
5880         }
5881     }
5882   else if (GET_CODE (addr) == SYMBOL_REF)
5883     {
5884       if (TARGET_64BIT)
5885         {
5886           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
5887           new = gen_rtx_CONST (Pmode, new);
5888           new = gen_rtx_MEM (Pmode, new);
5889           RTX_UNCHANGING_P (new) = 1;
5890           set_mem_alias_set (new, ix86_GOT_alias_set ());
5891
5892           if (reg == 0)
5893             reg = gen_reg_rtx (Pmode);
5894           /* Use directly gen_movsi, otherwise the address is loaded
5895              into register for CSE.  We don't want to CSE this addresses,
5896              instead we CSE addresses from the GOT table, so skip this.  */
5897           emit_insn (gen_movsi (reg, new));
5898           new = reg;
5899         }
5900       else
5901         {
5902           /* This symbol must be referenced via a load from the
5903              Global Offset Table (@GOT).  */
5904
5905           if (reload_in_progress)
5906             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
5907           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
5908           new = gen_rtx_CONST (Pmode, new);
5909           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
5910           new = gen_rtx_MEM (Pmode, new);
5911           RTX_UNCHANGING_P (new) = 1;
5912           set_mem_alias_set (new, ix86_GOT_alias_set ());
5913
5914           if (reg == 0)
5915             reg = gen_reg_rtx (Pmode);
5916           emit_move_insn (reg, new);
5917           new = reg;
5918         }
5919     }
5920   else
5921     {
5922       if (GET_CODE (addr) == CONST)
5923         {
5924           addr = XEXP (addr, 0);
5925
5926           /* We must match stuff we generate before.  Assume the only
5927              unspecs that can get here are ours.  Not that we could do
5928              anything with them anyway...  */
5929           if (GET_CODE (addr) == UNSPEC
5930               || (GET_CODE (addr) == PLUS
5931                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
5932             return orig;
5933           if (GET_CODE (addr) != PLUS)
5934             abort ();
5935         }
5936       if (GET_CODE (addr) == PLUS)
5937         {
5938           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
5939
5940           /* Check first to see if this is a constant offset from a @GOTOFF
5941              symbol reference.  */
5942           if (local_symbolic_operand (op0, Pmode)
5943               && GET_CODE (op1) == CONST_INT)
5944             {
5945               if (!TARGET_64BIT)
5946                 {
5947                   if (reload_in_progress)
5948                     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
5949                   new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
5950                                         UNSPEC_GOTOFF);
5951                   new = gen_rtx_PLUS (Pmode, new, op1);
5952                   new = gen_rtx_CONST (Pmode, new);
5953                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
5954
5955                   if (reg != 0)
5956                     {
5957                       emit_move_insn (reg, new);
5958                       new = reg;
5959                     }
5960                 }
5961               else
5962                 {
5963                   if (INTVAL (op1) < -16*1024*1024
5964                       || INTVAL (op1) >= 16*1024*1024)
5965                     new = gen_rtx_PLUS (Pmode, op0, force_reg (Pmode, op1));
5966                 }
5967             }
5968           else
5969             {
5970               base = legitimize_pic_address (XEXP (addr, 0), reg);
5971               new  = legitimize_pic_address (XEXP (addr, 1),
5972                                              base == reg ? NULL_RTX : reg);
5973
5974               if (GET_CODE (new) == CONST_INT)
5975                 new = plus_constant (base, INTVAL (new));
5976               else
5977                 {
5978                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
5979                     {
5980                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
5981                       new = XEXP (new, 1);
5982                     }
5983                   new = gen_rtx_PLUS (Pmode, base, new);
5984                 }
5985             }
5986         }
5987     }
5988   return new;
5989 }
5990
5991 static void
5992 ix86_encode_section_info (decl, first)
5993      tree decl;
5994      int first ATTRIBUTE_UNUSED;
5995 {
5996   bool local_p = (*targetm.binds_local_p) (decl);
5997   rtx rtl, symbol;
5998
5999   rtl = DECL_P (decl) ? DECL_RTL (decl) : TREE_CST_RTL (decl);
6000   if (GET_CODE (rtl) != MEM)
6001     return;
6002   symbol = XEXP (rtl, 0);
6003   if (GET_CODE (symbol) != SYMBOL_REF)
6004     return;
6005
6006   /* For basic x86, if using PIC, mark a SYMBOL_REF for a non-global
6007      symbol so that we may access it directly in the GOT.  */
6008
6009   if (flag_pic)
6010     SYMBOL_REF_FLAG (symbol) = local_p;
6011
6012   /* For ELF, encode thread-local data with %[GLil] for "global dynamic",
6013      "local dynamic", "initial exec" or "local exec" TLS models
6014      respectively.  */
6015
6016   if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
6017     {
6018       const char *symbol_str;
6019       char *newstr;
6020       size_t len;
6021       enum tls_model kind = decl_tls_model (decl);
6022
6023       if (TARGET_64BIT && ! flag_pic)
6024         {
6025           /* x86-64 doesn't allow non-pic code for shared libraries,
6026              so don't generate GD/LD TLS models for non-pic code.  */
6027           switch (kind)
6028             {
6029             case TLS_MODEL_GLOBAL_DYNAMIC:
6030               kind = TLS_MODEL_INITIAL_EXEC; break;
6031             case TLS_MODEL_LOCAL_DYNAMIC:
6032               kind = TLS_MODEL_LOCAL_EXEC; break;
6033             default:
6034               break;
6035             }
6036         }
6037
6038       symbol_str = XSTR (symbol, 0);
6039
6040       if (symbol_str[0] == '%')
6041         {
6042           if (symbol_str[1] == tls_model_chars[kind])
6043             return;
6044           symbol_str += 2;
6045         }
6046       len = strlen (symbol_str) + 1;
6047       newstr = alloca (len + 2);
6048
6049       newstr[0] = '%';
6050       newstr[1] = tls_model_chars[kind];
6051       memcpy (newstr + 2, symbol_str, len);
6052
6053       XSTR (symbol, 0) = ggc_alloc_string (newstr, len + 2 - 1);
6054     }
6055 }
6056
6057 /* Undo the above when printing symbol names.  */
6058
6059 static const char *
6060 ix86_strip_name_encoding (str)
6061      const char *str;
6062 {
6063   if (str[0] == '%')
6064     str += 2;
6065   if (str [0] == '*')
6066     str += 1;
6067   return str;
6068 }
6069 \f
6070 /* Load the thread pointer into a register.  */
6071
6072 static rtx
6073 get_thread_pointer ()
6074 {
6075   rtx tp;
6076
6077   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
6078   tp = gen_rtx_MEM (Pmode, tp);
6079   RTX_UNCHANGING_P (tp) = 1;
6080   set_mem_alias_set (tp, ix86_GOT_alias_set ());
6081   tp = force_reg (Pmode, tp);
6082
6083   return tp;
6084 }
6085
6086 /* Try machine-dependent ways of modifying an illegitimate address
6087    to be legitimate.  If we find one, return the new, valid address.
6088    This macro is used in only one place: `memory_address' in explow.c.
6089
6090    OLDX is the address as it was before break_out_memory_refs was called.
6091    In some cases it is useful to look at this to decide what needs to be done.
6092
6093    MODE and WIN are passed so that this macro can use
6094    GO_IF_LEGITIMATE_ADDRESS.
6095
6096    It is always safe for this macro to do nothing.  It exists to recognize
6097    opportunities to optimize the output.
6098
6099    For the 80386, we handle X+REG by loading X into a register R and
6100    using R+REG.  R will go in a general reg and indexing will be used.
6101    However, if REG is a broken-out memory address or multiplication,
6102    nothing needs to be done because REG can certainly go in a general reg.
6103
6104    When -fpic is used, special handling is needed for symbolic references.
6105    See comments by legitimize_pic_address in i386.c for details.  */
6106
6107 rtx
6108 legitimize_address (x, oldx, mode)
6109      register rtx x;
6110      register rtx oldx ATTRIBUTE_UNUSED;
6111      enum machine_mode mode;
6112 {
6113   int changed = 0;
6114   unsigned log;
6115
6116   if (TARGET_DEBUG_ADDR)
6117     {
6118       fprintf (stderr, "\n==========\nLEGITIMIZE_ADDRESS, mode = %s\n",
6119                GET_MODE_NAME (mode));
6120       debug_rtx (x);
6121     }
6122
6123   log = tls_symbolic_operand (x, mode);
6124   if (log)
6125     {
6126       rtx dest, base, off, pic;
6127       int type;
6128
6129       switch (log)
6130         {
6131         case TLS_MODEL_GLOBAL_DYNAMIC:
6132           dest = gen_reg_rtx (Pmode);
6133           if (TARGET_64BIT)
6134             {
6135               rtx rax = gen_rtx_REG (Pmode, 0), insns;
6136
6137               start_sequence ();
6138               emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
6139               insns = get_insns ();
6140               end_sequence ();
6141
6142               emit_libcall_block (insns, dest, rax, x);
6143             }
6144           else
6145             emit_insn (gen_tls_global_dynamic_32 (dest, x));
6146           break;
6147
6148         case TLS_MODEL_LOCAL_DYNAMIC:
6149           base = gen_reg_rtx (Pmode);
6150           if (TARGET_64BIT)
6151             {
6152               rtx rax = gen_rtx_REG (Pmode, 0), insns, note;
6153
6154               start_sequence ();
6155               emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
6156               insns = get_insns ();
6157               end_sequence ();
6158
6159               note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
6160               note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
6161               emit_libcall_block (insns, base, rax, note);
6162             }
6163           else
6164             emit_insn (gen_tls_local_dynamic_base_32 (base));
6165
6166           off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
6167           off = gen_rtx_CONST (Pmode, off);
6168
6169           return gen_rtx_PLUS (Pmode, base, off);
6170
6171         case TLS_MODEL_INITIAL_EXEC:
6172           if (TARGET_64BIT)
6173             {
6174               pic = NULL;
6175               type = UNSPEC_GOTNTPOFF;
6176             }
6177           else if (flag_pic)
6178             {
6179               if (reload_in_progress)
6180                 regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6181               pic = pic_offset_table_rtx;
6182               type = TARGET_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
6183             }
6184           else if (!TARGET_GNU_TLS)
6185             {
6186               pic = gen_reg_rtx (Pmode);
6187               emit_insn (gen_set_got (pic));
6188               type = UNSPEC_GOTTPOFF;
6189             }
6190           else
6191             {
6192               pic = NULL;
6193               type = UNSPEC_INDNTPOFF;
6194             }
6195
6196           base = get_thread_pointer ();
6197
6198           off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
6199           off = gen_rtx_CONST (Pmode, off);
6200           if (pic)
6201             off = gen_rtx_PLUS (Pmode, pic, off);
6202           off = gen_rtx_MEM (Pmode, off);
6203           RTX_UNCHANGING_P (off) = 1;
6204           set_mem_alias_set (off, ix86_GOT_alias_set ());
6205           dest = gen_reg_rtx (Pmode);
6206
6207           if (TARGET_64BIT || TARGET_GNU_TLS)
6208             {
6209               emit_move_insn (dest, off);
6210               return gen_rtx_PLUS (Pmode, base, dest);
6211             }
6212           else
6213             emit_insn (gen_subsi3 (dest, base, off));
6214           break;
6215
6216         case TLS_MODEL_LOCAL_EXEC:
6217           base = get_thread_pointer ();
6218
6219           off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
6220                                 (TARGET_64BIT || TARGET_GNU_TLS)
6221                                 ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
6222           off = gen_rtx_CONST (Pmode, off);
6223
6224           if (TARGET_64BIT || TARGET_GNU_TLS)
6225             return gen_rtx_PLUS (Pmode, base, off);
6226           else
6227             {
6228               dest = gen_reg_rtx (Pmode);
6229               emit_insn (gen_subsi3 (dest, base, off));
6230             }
6231           break;
6232
6233         default:
6234           abort ();
6235         }
6236
6237       return dest;
6238     }
6239
6240   if (flag_pic && SYMBOLIC_CONST (x))
6241     return legitimize_pic_address (x, 0);
6242
6243   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
6244   if (GET_CODE (x) == ASHIFT
6245       && GET_CODE (XEXP (x, 1)) == CONST_INT
6246       && (log = (unsigned) exact_log2 (INTVAL (XEXP (x, 1)))) < 4)
6247     {
6248       changed = 1;
6249       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
6250                         GEN_INT (1 << log));
6251     }
6252
6253   if (GET_CODE (x) == PLUS)
6254     {
6255       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
6256
6257       if (GET_CODE (XEXP (x, 0)) == ASHIFT
6258           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6259           && (log = (unsigned) exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) < 4)
6260         {
6261           changed = 1;
6262           XEXP (x, 0) = gen_rtx_MULT (Pmode,
6263                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
6264                                       GEN_INT (1 << log));
6265         }
6266
6267       if (GET_CODE (XEXP (x, 1)) == ASHIFT
6268           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
6269           && (log = (unsigned) exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1)))) < 4)
6270         {
6271           changed = 1;
6272           XEXP (x, 1) = gen_rtx_MULT (Pmode,
6273                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
6274                                       GEN_INT (1 << log));
6275         }
6276
6277       /* Put multiply first if it isn't already.  */
6278       if (GET_CODE (XEXP (x, 1)) == MULT)
6279         {
6280           rtx tmp = XEXP (x, 0);
6281           XEXP (x, 0) = XEXP (x, 1);
6282           XEXP (x, 1) = tmp;
6283           changed = 1;
6284         }
6285
6286       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
6287          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
6288          created by virtual register instantiation, register elimination, and
6289          similar optimizations.  */
6290       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
6291         {
6292           changed = 1;
6293           x = gen_rtx_PLUS (Pmode,
6294                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
6295                                           XEXP (XEXP (x, 1), 0)),
6296                             XEXP (XEXP (x, 1), 1));
6297         }
6298
6299       /* Canonicalize
6300          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
6301          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
6302       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
6303                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
6304                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
6305                && CONSTANT_P (XEXP (x, 1)))
6306         {
6307           rtx constant;
6308           rtx other = NULL_RTX;
6309
6310           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6311             {
6312               constant = XEXP (x, 1);
6313               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
6314             }
6315           else if (GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 1)) == CONST_INT)
6316             {
6317               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
6318               other = XEXP (x, 1);
6319             }
6320           else
6321             constant = 0;
6322
6323           if (constant)
6324             {
6325               changed = 1;
6326               x = gen_rtx_PLUS (Pmode,
6327                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
6328                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
6329                                 plus_constant (other, INTVAL (constant)));
6330             }
6331         }
6332
6333       if (changed && legitimate_address_p (mode, x, FALSE))
6334         return x;
6335
6336       if (GET_CODE (XEXP (x, 0)) == MULT)
6337         {
6338           changed = 1;
6339           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
6340         }
6341
6342       if (GET_CODE (XEXP (x, 1)) == MULT)
6343         {
6344           changed = 1;
6345           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
6346         }
6347
6348       if (changed
6349           && GET_CODE (XEXP (x, 1)) == REG
6350           && GET_CODE (XEXP (x, 0)) == REG)
6351         return x;
6352
6353       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
6354         {
6355           changed = 1;
6356           x = legitimize_pic_address (x, 0);
6357         }
6358
6359       if (changed && legitimate_address_p (mode, x, FALSE))
6360         return x;
6361
6362       if (GET_CODE (XEXP (x, 0)) == REG)
6363         {
6364           register rtx temp = gen_reg_rtx (Pmode);
6365           register rtx val  = force_operand (XEXP (x, 1), temp);
6366           if (val != temp)
6367             emit_move_insn (temp, val);
6368
6369           XEXP (x, 1) = temp;
6370           return x;
6371         }
6372
6373       else if (GET_CODE (XEXP (x, 1)) == REG)
6374         {
6375           register rtx temp = gen_reg_rtx (Pmode);
6376           register rtx val  = force_operand (XEXP (x, 0), temp);
6377           if (val != temp)
6378             emit_move_insn (temp, val);
6379
6380           XEXP (x, 0) = temp;
6381           return x;
6382         }
6383     }
6384
6385   return x;
6386 }
6387 \f
6388 /* Print an integer constant expression in assembler syntax.  Addition
6389    and subtraction are the only arithmetic that may appear in these
6390    expressions.  FILE is the stdio stream to write to, X is the rtx, and
6391    CODE is the operand print code from the output string.  */
6392
6393 static void
6394 output_pic_addr_const (file, x, code)
6395      FILE *file;
6396      rtx x;
6397      int code;
6398 {
6399   char buf[256];
6400
6401   switch (GET_CODE (x))
6402     {
6403     case PC:
6404       if (flag_pic)
6405         putc ('.', file);
6406       else
6407         abort ();
6408       break;
6409
6410     case SYMBOL_REF:
6411       assemble_name (file, XSTR (x, 0));
6412       if (!TARGET_MACHO && code == 'P' && ! SYMBOL_REF_FLAG (x))
6413         fputs ("@PLT", file);
6414       break;
6415
6416     case LABEL_REF:
6417       x = XEXP (x, 0);
6418       /* FALLTHRU */
6419     case CODE_LABEL:
6420       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
6421       assemble_name (asm_out_file, buf);
6422       break;
6423
6424     case CONST_INT:
6425       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
6426       break;
6427
6428     case CONST:
6429       /* This used to output parentheses around the expression,
6430          but that does not work on the 386 (either ATT or BSD assembler).  */
6431       output_pic_addr_const (file, XEXP (x, 0), code);
6432       break;
6433
6434     case CONST_DOUBLE:
6435       if (GET_MODE (x) == VOIDmode)
6436         {
6437           /* We can use %d if the number is <32 bits and positive.  */
6438           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
6439             fprintf (file, "0x%lx%08lx",
6440                      (unsigned long) CONST_DOUBLE_HIGH (x),
6441                      (unsigned long) CONST_DOUBLE_LOW (x));
6442           else
6443             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
6444         }
6445       else
6446         /* We can't handle floating point constants;
6447            PRINT_OPERAND must handle them.  */
6448         output_operand_lossage ("floating constant misused");
6449       break;
6450
6451     case PLUS:
6452       /* Some assemblers need integer constants to appear first.  */
6453       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
6454         {
6455           output_pic_addr_const (file, XEXP (x, 0), code);
6456           putc ('+', file);
6457           output_pic_addr_const (file, XEXP (x, 1), code);
6458         }
6459       else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6460         {
6461           output_pic_addr_const (file, XEXP (x, 1), code);
6462           putc ('+', file);
6463           output_pic_addr_const (file, XEXP (x, 0), code);
6464         }
6465       else
6466         abort ();
6467       break;
6468
6469     case MINUS:
6470       if (!TARGET_MACHO)
6471         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
6472       output_pic_addr_const (file, XEXP (x, 0), code);
6473       putc ('-', file);
6474       output_pic_addr_const (file, XEXP (x, 1), code);
6475       if (!TARGET_MACHO)
6476         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
6477       break;
6478
6479      case UNSPEC:
6480        if (XVECLEN (x, 0) != 1)
6481          abort ();
6482        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
6483        switch (XINT (x, 1))
6484         {
6485         case UNSPEC_GOT:
6486           fputs ("@GOT", file);
6487           break;
6488         case UNSPEC_GOTOFF:
6489           fputs ("@GOTOFF", file);
6490           break;
6491         case UNSPEC_GOTPCREL:
6492           fputs ("@GOTPCREL(%rip)", file);
6493           break;
6494         case UNSPEC_GOTTPOFF:
6495           /* FIXME: This might be @TPOFF in Sun ld too.  */
6496           fputs ("@GOTTPOFF", file);
6497           break;
6498         case UNSPEC_TPOFF:
6499           fputs ("@TPOFF", file);
6500           break;
6501         case UNSPEC_NTPOFF:
6502           if (TARGET_64BIT)
6503             fputs ("@TPOFF", file);
6504           else
6505             fputs ("@NTPOFF", file);
6506           break;
6507         case UNSPEC_DTPOFF:
6508           fputs ("@DTPOFF", file);
6509           break;
6510         case UNSPEC_GOTNTPOFF:
6511           if (TARGET_64BIT)
6512             fputs ("@GOTTPOFF(%rip)", file);
6513           else
6514             fputs ("@GOTNTPOFF", file);
6515           break;
6516         case UNSPEC_INDNTPOFF:
6517           fputs ("@INDNTPOFF", file);
6518           break;
6519         default:
6520           output_operand_lossage ("invalid UNSPEC as operand");
6521           break;
6522         }
6523        break;
6524
6525     default:
6526       output_operand_lossage ("invalid expression as operand");
6527     }
6528 }
6529
6530 /* This is called from dwarfout.c via ASM_OUTPUT_DWARF_ADDR_CONST.
6531    We need to handle our special PIC relocations.  */
6532
6533 void
6534 i386_dwarf_output_addr_const (file, x)
6535      FILE *file;
6536      rtx x;
6537 {
6538 #ifdef ASM_QUAD
6539   fprintf (file, "%s", TARGET_64BIT ? ASM_QUAD : ASM_LONG);
6540 #else
6541   if (TARGET_64BIT)
6542     abort ();
6543   fprintf (file, "%s", ASM_LONG);
6544 #endif
6545   if (flag_pic)
6546     output_pic_addr_const (file, x, '\0');
6547   else
6548     output_addr_const (file, x);
6549   fputc ('\n', file);
6550 }
6551
6552 /* This is called from dwarf2out.c via ASM_OUTPUT_DWARF_DTPREL.
6553    We need to emit DTP-relative relocations.  */
6554
6555 void
6556 i386_output_dwarf_dtprel (file, size, x)
6557      FILE *file;
6558      int size;
6559      rtx x;
6560 {
6561   fputs (ASM_LONG, file);
6562   output_addr_const (file, x);
6563   fputs ("@DTPOFF", file);
6564   switch (size)
6565     {
6566     case 4:
6567       break;
6568     case 8:
6569       fputs (", 0", file);
6570       break;
6571     default:
6572       abort ();
6573    }
6574 }
6575
6576 /* In the name of slightly smaller debug output, and to cater to
6577    general assembler losage, recognize PIC+GOTOFF and turn it back
6578    into a direct symbol reference.  */
6579
6580 rtx
6581 i386_simplify_dwarf_addr (orig_x)
6582      rtx orig_x;
6583 {
6584   rtx x = orig_x, y;
6585
6586   if (GET_CODE (x) == MEM)
6587     x = XEXP (x, 0);
6588
6589   if (TARGET_64BIT)
6590     {
6591       if (GET_CODE (x) != CONST
6592           || GET_CODE (XEXP (x, 0)) != UNSPEC
6593           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
6594           || GET_CODE (orig_x) != MEM)
6595         return orig_x;
6596       return XVECEXP (XEXP (x, 0), 0, 0);
6597     }
6598
6599   if (GET_CODE (x) != PLUS
6600       || GET_CODE (XEXP (x, 1)) != CONST)
6601     return orig_x;
6602
6603   if (GET_CODE (XEXP (x, 0)) == REG
6604       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
6605     /* %ebx + GOT/GOTOFF */
6606     y = NULL;
6607   else if (GET_CODE (XEXP (x, 0)) == PLUS)
6608     {
6609       /* %ebx + %reg * scale + GOT/GOTOFF */
6610       y = XEXP (x, 0);
6611       if (GET_CODE (XEXP (y, 0)) == REG
6612           && REGNO (XEXP (y, 0)) == PIC_OFFSET_TABLE_REGNUM)
6613         y = XEXP (y, 1);
6614       else if (GET_CODE (XEXP (y, 1)) == REG
6615                && REGNO (XEXP (y, 1)) == PIC_OFFSET_TABLE_REGNUM)
6616         y = XEXP (y, 0);
6617       else
6618         return orig_x;
6619       if (GET_CODE (y) != REG
6620           && GET_CODE (y) != MULT
6621           && GET_CODE (y) != ASHIFT)
6622         return orig_x;
6623     }
6624   else
6625     return orig_x;
6626
6627   x = XEXP (XEXP (x, 1), 0);
6628   if (GET_CODE (x) == UNSPEC
6629       && ((XINT (x, 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
6630           || (XINT (x, 1) == UNSPEC_GOTOFF && GET_CODE (orig_x) != MEM)))
6631     {
6632       if (y)
6633         return gen_rtx_PLUS (Pmode, y, XVECEXP (x, 0, 0));
6634       return XVECEXP (x, 0, 0);
6635     }
6636
6637   if (GET_CODE (x) == PLUS
6638       && GET_CODE (XEXP (x, 0)) == UNSPEC
6639       && GET_CODE (XEXP (x, 1)) == CONST_INT
6640       && ((XINT (XEXP (x, 0), 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
6641           || (XINT (XEXP (x, 0), 1) == UNSPEC_GOTOFF
6642               && GET_CODE (orig_x) != MEM)))
6643     {
6644       x = gen_rtx_PLUS (VOIDmode, XVECEXP (XEXP (x, 0), 0, 0), XEXP (x, 1));
6645       if (y)
6646         return gen_rtx_PLUS (Pmode, y, x);
6647       return x;
6648     }
6649
6650   return orig_x;
6651 }
6652 \f
6653 static void
6654 put_condition_code (code, mode, reverse, fp, file)
6655      enum rtx_code code;
6656      enum machine_mode mode;
6657      int reverse, fp;
6658      FILE *file;
6659 {
6660   const char *suffix;
6661
6662   if (mode == CCFPmode || mode == CCFPUmode)
6663     {
6664       enum rtx_code second_code, bypass_code;
6665       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
6666       if (bypass_code != NIL || second_code != NIL)
6667         abort ();
6668       code = ix86_fp_compare_code_to_integer (code);
6669       mode = CCmode;
6670     }
6671   if (reverse)
6672     code = reverse_condition (code);
6673
6674   switch (code)
6675     {
6676     case EQ:
6677       suffix = "e";
6678       break;
6679     case NE:
6680       suffix = "ne";
6681       break;
6682     case GT:
6683       if (mode != CCmode && mode != CCNOmode && mode != CCGCmode)
6684         abort ();
6685       suffix = "g";
6686       break;
6687     case GTU:
6688       /* ??? Use "nbe" instead of "a" for fcmov losage on some assemblers.
6689          Those same assemblers have the same but opposite losage on cmov.  */
6690       if (mode != CCmode)
6691         abort ();
6692       suffix = fp ? "nbe" : "a";
6693       break;
6694     case LT:
6695       if (mode == CCNOmode || mode == CCGOCmode)
6696         suffix = "s";
6697       else if (mode == CCmode || mode == CCGCmode)
6698         suffix = "l";
6699       else
6700         abort ();
6701       break;
6702     case LTU:
6703       if (mode != CCmode)
6704         abort ();
6705       suffix = "b";
6706       break;
6707     case GE:
6708       if (mode == CCNOmode || mode == CCGOCmode)
6709         suffix = "ns";
6710       else if (mode == CCmode || mode == CCGCmode)
6711         suffix = "ge";
6712       else
6713         abort ();
6714       break;
6715     case GEU:
6716       /* ??? As above.  */
6717       if (mode != CCmode)
6718         abort ();
6719       suffix = fp ? "nb" : "ae";
6720       break;
6721     case LE:
6722       if (mode != CCmode && mode != CCGCmode && mode != CCNOmode)
6723         abort ();
6724       suffix = "le";
6725       break;
6726     case LEU:
6727       if (mode != CCmode)
6728         abort ();
6729       suffix = "be";
6730       break;
6731     case UNORDERED:
6732       suffix = fp ? "u" : "p";
6733       break;
6734     case ORDERED:
6735       suffix = fp ? "nu" : "np";
6736       break;
6737     default:
6738       abort ();
6739     }
6740   fputs (suffix, file);
6741 }
6742
6743 void
6744 print_reg (x, code, file)
6745      rtx x;
6746      int code;
6747      FILE *file;
6748 {
6749   if (REGNO (x) == ARG_POINTER_REGNUM
6750       || REGNO (x) == FRAME_POINTER_REGNUM
6751       || REGNO (x) == FLAGS_REG
6752       || REGNO (x) == FPSR_REG)
6753     abort ();
6754
6755   if (ASSEMBLER_DIALECT == ASM_ATT || USER_LABEL_PREFIX[0] == 0)
6756     putc ('%', file);
6757
6758   if (code == 'w' || MMX_REG_P (x))
6759     code = 2;
6760   else if (code == 'b')
6761     code = 1;
6762   else if (code == 'k')
6763     code = 4;
6764   else if (code == 'q')
6765     code = 8;
6766   else if (code == 'y')
6767     code = 3;
6768   else if (code == 'h')
6769     code = 0;
6770   else
6771     code = GET_MODE_SIZE (GET_MODE (x));
6772
6773   /* Irritatingly, AMD extended registers use different naming convention
6774      from the normal registers.  */
6775   if (REX_INT_REG_P (x))
6776     {
6777       if (!TARGET_64BIT)
6778         abort ();
6779       switch (code)
6780         {
6781           case 0:
6782             error ("extended registers have no high halves");
6783             break;
6784           case 1:
6785             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
6786             break;
6787           case 2:
6788             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
6789             break;
6790           case 4:
6791             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
6792             break;
6793           case 8:
6794             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
6795             break;
6796           default:
6797             error ("unsupported operand size for extended register");
6798             break;
6799         }
6800       return;
6801     }
6802   switch (code)
6803     {
6804     case 3:
6805       if (STACK_TOP_P (x))
6806         {
6807           fputs ("st(0)", file);
6808           break;
6809         }
6810       /* FALLTHRU */
6811     case 8:
6812     case 4:
6813     case 12:
6814       if (! ANY_FP_REG_P (x))
6815         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
6816       /* FALLTHRU */
6817     case 16:
6818     case 2:
6819       fputs (hi_reg_name[REGNO (x)], file);
6820       break;
6821     case 1:
6822       fputs (qi_reg_name[REGNO (x)], file);
6823       break;
6824     case 0:
6825       fputs (qi_high_reg_name[REGNO (x)], file);
6826       break;
6827     default:
6828       abort ();
6829     }
6830 }
6831
6832 /* Locate some local-dynamic symbol still in use by this function
6833    so that we can print its name in some tls_local_dynamic_base
6834    pattern.  */
6835
6836 static const char *
6837 get_some_local_dynamic_name ()
6838 {
6839   rtx insn;
6840
6841   if (cfun->machine->some_ld_name)
6842     return cfun->machine->some_ld_name;
6843
6844   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
6845     if (INSN_P (insn)
6846         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
6847       return cfun->machine->some_ld_name;
6848
6849   abort ();
6850 }
6851
6852 static int
6853 get_some_local_dynamic_name_1 (px, data)
6854      rtx *px;
6855      void *data ATTRIBUTE_UNUSED;
6856 {
6857   rtx x = *px;
6858
6859   if (GET_CODE (x) == SYMBOL_REF
6860       && local_dynamic_symbolic_operand (x, Pmode))
6861     {
6862       cfun->machine->some_ld_name = XSTR (x, 0);
6863       return 1;
6864     }
6865
6866   return 0;
6867 }
6868
6869 /* Meaning of CODE:
6870    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
6871    C -- print opcode suffix for set/cmov insn.
6872    c -- like C, but print reversed condition
6873    F,f -- likewise, but for floating-point.
6874    O -- if CMOV_SUN_AS_SYNTAX, expand to "w.", "l." or "q.", otherwise
6875         nothing
6876    R -- print the prefix for register names.
6877    z -- print the opcode suffix for the size of the current operand.
6878    * -- print a star (in certain assembler syntax)
6879    A -- print an absolute memory reference.
6880    w -- print the operand as if it's a "word" (HImode) even if it isn't.
6881    s -- print a shift double count, followed by the assemblers argument
6882         delimiter.
6883    b -- print the QImode name of the register for the indicated operand.
6884         %b0 would print %al if operands[0] is reg 0.
6885    w --  likewise, print the HImode name of the register.
6886    k --  likewise, print the SImode name of the register.
6887    q --  likewise, print the DImode name of the register.
6888    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
6889    y -- print "st(0)" instead of "st" as a register.
6890    D -- print condition for SSE cmp instruction.
6891    P -- if PIC, print an @PLT suffix.
6892    X -- don't print any sort of PIC '@' suffix for a symbol.
6893    & -- print some in-use local-dynamic symbol name.
6894  */
6895
6896 void
6897 print_operand (file, x, code)
6898      FILE *file;
6899      rtx x;
6900      int code;
6901 {
6902   if (code)
6903     {
6904       switch (code)
6905         {
6906         case '*':
6907           if (ASSEMBLER_DIALECT == ASM_ATT)
6908             putc ('*', file);
6909           return;
6910
6911         case '&':
6912           assemble_name (file, get_some_local_dynamic_name ());
6913           return;
6914
6915         case 'A':
6916           if (ASSEMBLER_DIALECT == ASM_ATT)
6917             putc ('*', file);
6918           else if (ASSEMBLER_DIALECT == ASM_INTEL)
6919             {
6920               /* Intel syntax. For absolute addresses, registers should not
6921                  be surrounded by braces.  */
6922               if (GET_CODE (x) != REG)
6923                 {
6924                   putc ('[', file);
6925                   PRINT_OPERAND (file, x, 0);
6926                   putc (']', file);
6927                   return;
6928                 }
6929             }
6930           else
6931             abort ();
6932
6933           PRINT_OPERAND (file, x, 0);
6934           return;
6935
6936
6937         case 'L':
6938           if (ASSEMBLER_DIALECT == ASM_ATT)
6939             putc ('l', file);
6940           return;
6941
6942         case 'W':
6943           if (ASSEMBLER_DIALECT == ASM_ATT)
6944             putc ('w', file);
6945           return;
6946
6947         case 'B':
6948           if (ASSEMBLER_DIALECT == ASM_ATT)
6949             putc ('b', file);
6950           return;
6951
6952         case 'Q':
6953           if (ASSEMBLER_DIALECT == ASM_ATT)
6954             putc ('l', file);
6955           return;
6956
6957         case 'S':
6958           if (ASSEMBLER_DIALECT == ASM_ATT)
6959             putc ('s', file);
6960           return;
6961
6962         case 'T':
6963           if (ASSEMBLER_DIALECT == ASM_ATT)
6964             putc ('t', file);
6965           return;
6966
6967         case 'z':
6968           /* 387 opcodes don't get size suffixes if the operands are
6969              registers.  */
6970           if (STACK_REG_P (x))
6971             return;
6972
6973           /* Likewise if using Intel opcodes.  */
6974           if (ASSEMBLER_DIALECT == ASM_INTEL)
6975             return;
6976
6977           /* This is the size of op from size of operand.  */
6978           switch (GET_MODE_SIZE (GET_MODE (x)))
6979             {
6980             case 2:
6981 #ifdef HAVE_GAS_FILDS_FISTS
6982               putc ('s', file);
6983 #endif
6984               return;
6985
6986             case 4:
6987               if (GET_MODE (x) == SFmode)
6988                 {
6989                   putc ('s', file);
6990                   return;
6991                 }
6992               else
6993                 putc ('l', file);
6994               return;
6995
6996             case 12:
6997             case 16:
6998               putc ('t', file);
6999               return;
7000
7001             case 8:
7002               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
7003                 {
7004 #ifdef GAS_MNEMONICS
7005                   putc ('q', file);
7006 #else
7007                   putc ('l', file);
7008                   putc ('l', file);
7009 #endif
7010                 }
7011               else
7012                 putc ('l', file);
7013               return;
7014
7015             default:
7016               abort ();
7017             }
7018
7019         case 'b':
7020         case 'w':
7021         case 'k':
7022         case 'q':
7023         case 'h':
7024         case 'y':
7025         case 'X':
7026         case 'P':
7027           break;
7028
7029         case 's':
7030           if (GET_CODE (x) == CONST_INT || ! SHIFT_DOUBLE_OMITS_COUNT)
7031             {
7032               PRINT_OPERAND (file, x, 0);
7033               putc (',', file);
7034             }
7035           return;
7036
7037         case 'D':
7038           /* Little bit of braindamage here.  The SSE compare instructions
7039              does use completely different names for the comparisons that the
7040              fp conditional moves.  */
7041           switch (GET_CODE (x))
7042             {
7043             case EQ:
7044             case UNEQ:
7045               fputs ("eq", file);
7046               break;
7047             case LT:
7048             case UNLT:
7049               fputs ("lt", file);
7050               break;
7051             case LE:
7052             case UNLE:
7053               fputs ("le", file);
7054               break;
7055             case UNORDERED:
7056               fputs ("unord", file);
7057               break;
7058             case NE:
7059             case LTGT:
7060               fputs ("neq", file);
7061               break;
7062             case UNGE:
7063             case GE:
7064               fputs ("nlt", file);
7065               break;
7066             case UNGT:
7067             case GT:
7068               fputs ("nle", file);
7069               break;
7070             case ORDERED:
7071               fputs ("ord", file);
7072               break;
7073             default:
7074               abort ();
7075               break;
7076             }
7077           return;
7078         case 'O':
7079 #ifdef CMOV_SUN_AS_SYNTAX
7080           if (ASSEMBLER_DIALECT == ASM_ATT)
7081             {
7082               switch (GET_MODE (x))
7083                 {
7084                 case HImode: putc ('w', file); break;
7085                 case SImode:
7086                 case SFmode: putc ('l', file); break;
7087                 case DImode:
7088                 case DFmode: putc ('q', file); break;
7089                 default: abort ();
7090                 }
7091               putc ('.', file);
7092             }
7093 #endif
7094           return;
7095         case 'C':
7096           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
7097           return;
7098         case 'F':
7099 #ifdef CMOV_SUN_AS_SYNTAX
7100           if (ASSEMBLER_DIALECT == ASM_ATT)
7101             putc ('.', file);
7102 #endif
7103           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
7104           return;
7105
7106           /* Like above, but reverse condition */
7107         case 'c':
7108           /* Check to see if argument to %c is really a constant
7109              and not a condition code which needs to be reversed.  */
7110           if (GET_RTX_CLASS (GET_CODE (x)) != '<')
7111           {
7112             output_operand_lossage ("operand is neither a constant nor a condition code, invalid operand code 'c'");
7113              return;
7114           }
7115           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
7116           return;
7117         case 'f':
7118 #ifdef CMOV_SUN_AS_SYNTAX
7119           if (ASSEMBLER_DIALECT == ASM_ATT)
7120             putc ('.', file);
7121 #endif
7122           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
7123           return;
7124         case '+':
7125           {
7126             rtx x;
7127
7128             if (!optimize || optimize_size || !TARGET_BRANCH_PREDICTION_HINTS)
7129               return;
7130
7131             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
7132             if (x)
7133               {
7134                 int pred_val = INTVAL (XEXP (x, 0));
7135
7136                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
7137                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
7138                   {
7139                     int taken = pred_val > REG_BR_PROB_BASE / 2;
7140                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
7141
7142                     /* Emit hints only in the case default branch prediction
7143                        heuristics would fail.  */
7144                     if (taken != cputaken)
7145                       {
7146                         /* We use 3e (DS) prefix for taken branches and
7147                            2e (CS) prefix for not taken branches.  */
7148                         if (taken)
7149                           fputs ("ds ; ", file);
7150                         else
7151                           fputs ("cs ; ", file);
7152                       }
7153                   }
7154               }
7155             return;
7156           }
7157         default:
7158             output_operand_lossage ("invalid operand code `%c'", code);
7159         }
7160     }
7161
7162   if (GET_CODE (x) == REG)
7163     {
7164       PRINT_REG (x, code, file);
7165     }
7166
7167   else if (GET_CODE (x) == MEM)
7168     {
7169       /* No `byte ptr' prefix for call instructions.  */
7170       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P')
7171         {
7172           const char * size;
7173           switch (GET_MODE_SIZE (GET_MODE (x)))
7174             {
7175             case 1: size = "BYTE"; break;
7176             case 2: size = "WORD"; break;
7177             case 4: size = "DWORD"; break;
7178             case 8: size = "QWORD"; break;
7179             case 12: size = "XWORD"; break;
7180             case 16: size = "XMMWORD"; break;
7181             default:
7182               abort ();
7183             }
7184
7185           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
7186           if (code == 'b')
7187             size = "BYTE";
7188           else if (code == 'w')
7189             size = "WORD";
7190           else if (code == 'k')
7191             size = "DWORD";
7192
7193           fputs (size, file);
7194           fputs (" PTR ", file);
7195         }
7196
7197       x = XEXP (x, 0);
7198       if (flag_pic && CONSTANT_ADDRESS_P (x))
7199         output_pic_addr_const (file, x, code);
7200       /* Avoid (%rip) for call operands.  */
7201       else if (CONSTANT_ADDRESS_P (x) && code == 'P'
7202                && GET_CODE (x) != CONST_INT)
7203         output_addr_const (file, x);
7204       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
7205         output_operand_lossage ("invalid constraints for operand");
7206       else
7207         output_address (x);
7208     }
7209
7210   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
7211     {
7212       REAL_VALUE_TYPE r;
7213       long l;
7214
7215       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7216       REAL_VALUE_TO_TARGET_SINGLE (r, l);
7217
7218       if (ASSEMBLER_DIALECT == ASM_ATT)
7219         putc ('$', file);
7220       fprintf (file, "0x%lx", l);
7221     }
7222
7223  /* These float cases don't actually occur as immediate operands.  */
7224  else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
7225     {
7226       char dstr[30];
7227
7228       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
7229       fprintf (file, "%s", dstr);
7230     }
7231
7232   else if (GET_CODE (x) == CONST_DOUBLE
7233            && (GET_MODE (x) == XFmode || GET_MODE (x) == TFmode))
7234     {
7235       char dstr[30];
7236
7237       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
7238       fprintf (file, "%s", dstr);
7239     }
7240
7241   else
7242     {
7243       if (code != 'P')
7244         {
7245           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
7246             {
7247               if (ASSEMBLER_DIALECT == ASM_ATT)
7248                 putc ('$', file);
7249             }
7250           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
7251                    || GET_CODE (x) == LABEL_REF)
7252             {
7253               if (ASSEMBLER_DIALECT == ASM_ATT)
7254                 putc ('$', file);
7255               else
7256                 fputs ("OFFSET FLAT:", file);
7257             }
7258         }
7259       if (GET_CODE (x) == CONST_INT)
7260         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
7261       else if (flag_pic)
7262         output_pic_addr_const (file, x, code);
7263       else
7264         output_addr_const (file, x);
7265     }
7266 }
7267 \f
7268 /* Print a memory operand whose address is ADDR.  */
7269
7270 void
7271 print_operand_address (file, addr)
7272      FILE *file;
7273      register rtx addr;
7274 {
7275   struct ix86_address parts;
7276   rtx base, index, disp;
7277   int scale;
7278
7279   if (GET_CODE (addr) == UNSPEC && XINT (addr, 1) == UNSPEC_TP)
7280     {
7281       if (ASSEMBLER_DIALECT == ASM_INTEL)
7282         fputs ("DWORD PTR ", file);
7283       if (ASSEMBLER_DIALECT == ASM_ATT || USER_LABEL_PREFIX[0] == 0)
7284         putc ('%', file);
7285       if (TARGET_64BIT)
7286         fputs ("fs:0", file);
7287       else
7288         fputs ("gs:0", file);
7289       return;
7290     }
7291
7292   if (! ix86_decompose_address (addr, &parts))
7293     abort ();
7294
7295   base = parts.base;
7296   index = parts.index;
7297   disp = parts.disp;
7298   scale = parts.scale;
7299
7300   if (!base && !index)
7301     {
7302       /* Displacement only requires special attention.  */
7303
7304       if (GET_CODE (disp) == CONST_INT)
7305         {
7306           if (ASSEMBLER_DIALECT == ASM_INTEL)
7307             {
7308               if (USER_LABEL_PREFIX[0] == 0)
7309                 putc ('%', file);
7310               fputs ("ds:", file);
7311             }
7312           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (addr));
7313         }
7314       else if (flag_pic)
7315         output_pic_addr_const (file, addr, 0);
7316       else
7317         output_addr_const (file, addr);
7318
7319       /* Use one byte shorter RIP relative addressing for 64bit mode.  */
7320       if (TARGET_64BIT
7321           && ((GET_CODE (addr) == SYMBOL_REF
7322                && ! tls_symbolic_operand (addr, GET_MODE (addr)))
7323               || GET_CODE (addr) == LABEL_REF
7324               || (GET_CODE (addr) == CONST
7325                   && GET_CODE (XEXP (addr, 0)) == PLUS
7326                   && (GET_CODE (XEXP (XEXP (addr, 0), 0)) == SYMBOL_REF
7327                       || GET_CODE (XEXP (XEXP (addr, 0), 0)) == LABEL_REF)
7328                   && GET_CODE (XEXP (XEXP (addr, 0), 1)) == CONST_INT)))
7329         fputs ("(%rip)", file);
7330     }
7331   else
7332     {
7333       if (ASSEMBLER_DIALECT == ASM_ATT)
7334         {
7335           if (disp)
7336             {
7337               if (flag_pic)
7338                 output_pic_addr_const (file, disp, 0);
7339               else if (GET_CODE (disp) == LABEL_REF)
7340                 output_asm_label (disp);
7341               else
7342                 output_addr_const (file, disp);
7343             }
7344
7345           putc ('(', file);
7346           if (base)
7347             PRINT_REG (base, 0, file);
7348           if (index)
7349             {
7350               putc (',', file);
7351               PRINT_REG (index, 0, file);
7352               if (scale != 1)
7353                 fprintf (file, ",%d", scale);
7354             }
7355           putc (')', file);
7356         }
7357       else
7358         {
7359           rtx offset = NULL_RTX;
7360
7361           if (disp)
7362             {
7363               /* Pull out the offset of a symbol; print any symbol itself.  */
7364               if (GET_CODE (disp) == CONST
7365                   && GET_CODE (XEXP (disp, 0)) == PLUS
7366                   && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
7367                 {
7368                   offset = XEXP (XEXP (disp, 0), 1);
7369                   disp = gen_rtx_CONST (VOIDmode,
7370                                         XEXP (XEXP (disp, 0), 0));
7371                 }
7372
7373               if (flag_pic)
7374                 output_pic_addr_const (file, disp, 0);
7375               else if (GET_CODE (disp) == LABEL_REF)
7376                 output_asm_label (disp);
7377               else if (GET_CODE (disp) == CONST_INT)
7378                 offset = disp;
7379               else
7380                 output_addr_const (file, disp);
7381             }
7382
7383           putc ('[', file);
7384           if (base)
7385             {
7386               PRINT_REG (base, 0, file);
7387               if (offset)
7388                 {
7389                   if (INTVAL (offset) >= 0)
7390                     putc ('+', file);
7391                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
7392                 }
7393             }
7394           else if (offset)
7395             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
7396           else
7397             putc ('0', file);
7398
7399           if (index)
7400             {
7401               putc ('+', file);
7402               PRINT_REG (index, 0, file);
7403               if (scale != 1)
7404                 fprintf (file, "*%d", scale);
7405             }
7406           putc (']', file);
7407         }
7408     }
7409 }
7410
7411 bool
7412 output_addr_const_extra (file, x)
7413      FILE *file;
7414      rtx x;
7415 {
7416   rtx op;
7417
7418   if (GET_CODE (x) != UNSPEC)
7419     return false;
7420
7421   op = XVECEXP (x, 0, 0);
7422   switch (XINT (x, 1))
7423     {
7424     case UNSPEC_GOTTPOFF:
7425       output_addr_const (file, op);
7426       /* FIXME: This might be @TPOFF in Sun ld.  */
7427       fputs ("@GOTTPOFF", file);
7428       break;
7429     case UNSPEC_TPOFF:
7430       output_addr_const (file, op);
7431       fputs ("@TPOFF", file);
7432       break;
7433     case UNSPEC_NTPOFF:
7434       output_addr_const (file, op);
7435       if (TARGET_64BIT)
7436         fputs ("@TPOFF", file);
7437       else
7438         fputs ("@NTPOFF", file);
7439       break;
7440     case UNSPEC_DTPOFF:
7441       output_addr_const (file, op);
7442       fputs ("@DTPOFF", file);
7443       break;
7444     case UNSPEC_GOTNTPOFF:
7445       output_addr_const (file, op);
7446       if (TARGET_64BIT)
7447         fputs ("@GOTTPOFF(%rip)", file);
7448       else
7449         fputs ("@GOTNTPOFF", file);
7450       break;
7451     case UNSPEC_INDNTPOFF:
7452       output_addr_const (file, op);
7453       fputs ("@INDNTPOFF", file);
7454       break;
7455
7456     default:
7457       return false;
7458     }
7459
7460   return true;
7461 }
7462 \f
7463 /* Split one or more DImode RTL references into pairs of SImode
7464    references.  The RTL can be REG, offsettable MEM, integer constant, or
7465    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
7466    split and "num" is its length.  lo_half and hi_half are output arrays
7467    that parallel "operands".  */
7468
7469 void
7470 split_di (operands, num, lo_half, hi_half)
7471      rtx operands[];
7472      int num;
7473      rtx lo_half[], hi_half[];
7474 {
7475   while (num--)
7476     {
7477       rtx op = operands[num];
7478
7479       /* simplify_subreg refuse to split volatile memory addresses,
7480          but we still have to handle it.  */
7481       if (GET_CODE (op) == MEM)
7482         {
7483           lo_half[num] = adjust_address (op, SImode, 0);
7484           hi_half[num] = adjust_address (op, SImode, 4);
7485         }
7486       else
7487         {
7488           lo_half[num] = simplify_gen_subreg (SImode, op,
7489                                               GET_MODE (op) == VOIDmode
7490                                               ? DImode : GET_MODE (op), 0);
7491           hi_half[num] = simplify_gen_subreg (SImode, op,
7492                                               GET_MODE (op) == VOIDmode
7493                                               ? DImode : GET_MODE (op), 4);
7494         }
7495     }
7496 }
7497 /* Split one or more TImode RTL references into pairs of SImode
7498    references.  The RTL can be REG, offsettable MEM, integer constant, or
7499    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
7500    split and "num" is its length.  lo_half and hi_half are output arrays
7501    that parallel "operands".  */
7502
7503 void
7504 split_ti (operands, num, lo_half, hi_half)
7505      rtx operands[];
7506      int num;
7507      rtx lo_half[], hi_half[];
7508 {
7509   while (num--)
7510     {
7511       rtx op = operands[num];
7512
7513       /* simplify_subreg refuse to split volatile memory addresses, but we
7514          still have to handle it.  */
7515       if (GET_CODE (op) == MEM)
7516         {
7517           lo_half[num] = adjust_address (op, DImode, 0);
7518           hi_half[num] = adjust_address (op, DImode, 8);
7519         }
7520       else
7521         {
7522           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
7523           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
7524         }
7525     }
7526 }
7527 \f
7528 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
7529    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
7530    is the expression of the binary operation.  The output may either be
7531    emitted here, or returned to the caller, like all output_* functions.
7532
7533    There is no guarantee that the operands are the same mode, as they
7534    might be within FLOAT or FLOAT_EXTEND expressions.  */
7535
7536 #ifndef SYSV386_COMPAT
7537 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
7538    wants to fix the assemblers because that causes incompatibility
7539    with gcc.  No-one wants to fix gcc because that causes
7540    incompatibility with assemblers...  You can use the option of
7541    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
7542 #define SYSV386_COMPAT 1
7543 #endif
7544
7545 const char *
7546 output_387_binary_op (insn, operands)
7547      rtx insn;
7548      rtx *operands;
7549 {
7550   static char buf[30];
7551   const char *p;
7552   const char *ssep;
7553   int is_sse = SSE_REG_P (operands[0]) | SSE_REG_P (operands[1]) | SSE_REG_P (operands[2]);
7554
7555 #ifdef ENABLE_CHECKING
7556   /* Even if we do not want to check the inputs, this documents input
7557      constraints.  Which helps in understanding the following code.  */
7558   if (STACK_REG_P (operands[0])
7559       && ((REG_P (operands[1])
7560            && REGNO (operands[0]) == REGNO (operands[1])
7561            && (STACK_REG_P (operands[2]) || GET_CODE (operands[2]) == MEM))
7562           || (REG_P (operands[2])
7563               && REGNO (operands[0]) == REGNO (operands[2])
7564               && (STACK_REG_P (operands[1]) || GET_CODE (operands[1]) == MEM)))
7565       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
7566     ; /* ok */
7567   else if (!is_sse)
7568     abort ();
7569 #endif
7570
7571   switch (GET_CODE (operands[3]))
7572     {
7573     case PLUS:
7574       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7575           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7576         p = "fiadd";
7577       else
7578         p = "fadd";
7579       ssep = "add";
7580       break;
7581
7582     case MINUS:
7583       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7584           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7585         p = "fisub";
7586       else
7587         p = "fsub";
7588       ssep = "sub";
7589       break;
7590
7591     case MULT:
7592       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7593           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7594         p = "fimul";
7595       else
7596         p = "fmul";
7597       ssep = "mul";
7598       break;
7599
7600     case DIV:
7601       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7602           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7603         p = "fidiv";
7604       else
7605         p = "fdiv";
7606       ssep = "div";
7607       break;
7608
7609     default:
7610       abort ();
7611     }
7612
7613   if (is_sse)
7614    {
7615       strcpy (buf, ssep);
7616       if (GET_MODE (operands[0]) == SFmode)
7617         strcat (buf, "ss\t{%2, %0|%0, %2}");
7618       else
7619         strcat (buf, "sd\t{%2, %0|%0, %2}");
7620       return buf;
7621    }
7622   strcpy (buf, p);
7623
7624   switch (GET_CODE (operands[3]))
7625     {
7626     case MULT:
7627     case PLUS:
7628       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
7629         {
7630           rtx temp = operands[2];
7631           operands[2] = operands[1];
7632           operands[1] = temp;
7633         }
7634
7635       /* know operands[0] == operands[1].  */
7636
7637       if (GET_CODE (operands[2]) == MEM)
7638         {
7639           p = "%z2\t%2";
7640           break;
7641         }
7642
7643       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
7644         {
7645           if (STACK_TOP_P (operands[0]))
7646             /* How is it that we are storing to a dead operand[2]?
7647                Well, presumably operands[1] is dead too.  We can't
7648                store the result to st(0) as st(0) gets popped on this
7649                instruction.  Instead store to operands[2] (which I
7650                think has to be st(1)).  st(1) will be popped later.
7651                gcc <= 2.8.1 didn't have this check and generated
7652                assembly code that the Unixware assembler rejected.  */
7653             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
7654           else
7655             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
7656           break;
7657         }
7658
7659       if (STACK_TOP_P (operands[0]))
7660         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
7661       else
7662         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
7663       break;
7664
7665     case MINUS:
7666     case DIV:
7667       if (GET_CODE (operands[1]) == MEM)
7668         {
7669           p = "r%z1\t%1";
7670           break;
7671         }
7672
7673       if (GET_CODE (operands[2]) == MEM)
7674         {
7675           p = "%z2\t%2";
7676           break;
7677         }
7678
7679       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
7680         {
7681 #if SYSV386_COMPAT
7682           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
7683              derived assemblers, confusingly reverse the direction of
7684              the operation for fsub{r} and fdiv{r} when the
7685              destination register is not st(0).  The Intel assembler
7686              doesn't have this brain damage.  Read !SYSV386_COMPAT to
7687              figure out what the hardware really does.  */
7688           if (STACK_TOP_P (operands[0]))
7689             p = "{p\t%0, %2|rp\t%2, %0}";
7690           else
7691             p = "{rp\t%2, %0|p\t%0, %2}";
7692 #else
7693           if (STACK_TOP_P (operands[0]))
7694             /* As above for fmul/fadd, we can't store to st(0).  */
7695             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
7696           else
7697             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
7698 #endif
7699           break;
7700         }
7701
7702       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
7703         {
7704 #if SYSV386_COMPAT
7705           if (STACK_TOP_P (operands[0]))
7706             p = "{rp\t%0, %1|p\t%1, %0}";
7707           else
7708             p = "{p\t%1, %0|rp\t%0, %1}";
7709 #else
7710           if (STACK_TOP_P (operands[0]))
7711             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
7712           else
7713             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
7714 #endif
7715           break;
7716         }
7717
7718       if (STACK_TOP_P (operands[0]))
7719         {
7720           if (STACK_TOP_P (operands[1]))
7721             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
7722           else
7723             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
7724           break;
7725         }
7726       else if (STACK_TOP_P (operands[1]))
7727         {
7728 #if SYSV386_COMPAT
7729           p = "{\t%1, %0|r\t%0, %1}";
7730 #else
7731           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
7732 #endif
7733         }
7734       else
7735         {
7736 #if SYSV386_COMPAT
7737           p = "{r\t%2, %0|\t%0, %2}";
7738 #else
7739           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
7740 #endif
7741         }
7742       break;
7743
7744     default:
7745       abort ();
7746     }
7747
7748   strcat (buf, p);
7749   return buf;
7750 }
7751
7752 /* Output code to initialize control word copies used by
7753    trunc?f?i patterns.  NORMAL is set to current control word, while ROUND_DOWN
7754    is set to control word rounding downwards.  */
7755 void
7756 emit_i387_cw_initialization (normal, round_down)
7757      rtx normal, round_down;
7758 {
7759   rtx reg = gen_reg_rtx (HImode);
7760
7761   emit_insn (gen_x86_fnstcw_1 (normal));
7762   emit_move_insn (reg, normal);
7763   if (!TARGET_PARTIAL_REG_STALL && !optimize_size
7764       && !TARGET_64BIT)
7765     emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
7766   else
7767     emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0xc00)));
7768   emit_move_insn (round_down, reg);
7769 }
7770
7771 /* Output code for INSN to convert a float to a signed int.  OPERANDS
7772    are the insn operands.  The output may be [HSD]Imode and the input
7773    operand may be [SDX]Fmode.  */
7774
7775 const char *
7776 output_fix_trunc (insn, operands)
7777      rtx insn;
7778      rtx *operands;
7779 {
7780   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
7781   int dimode_p = GET_MODE (operands[0]) == DImode;
7782
7783   /* Jump through a hoop or two for DImode, since the hardware has no
7784      non-popping instruction.  We used to do this a different way, but
7785      that was somewhat fragile and broke with post-reload splitters.  */
7786   if (dimode_p && !stack_top_dies)
7787     output_asm_insn ("fld\t%y1", operands);
7788
7789   if (!STACK_TOP_P (operands[1]))
7790     abort ();
7791
7792   if (GET_CODE (operands[0]) != MEM)
7793     abort ();
7794
7795   output_asm_insn ("fldcw\t%3", operands);
7796   if (stack_top_dies || dimode_p)
7797     output_asm_insn ("fistp%z0\t%0", operands);
7798   else
7799     output_asm_insn ("fist%z0\t%0", operands);
7800   output_asm_insn ("fldcw\t%2", operands);
7801
7802   return "";
7803 }
7804
7805 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
7806    should be used and 2 when fnstsw should be used.  UNORDERED_P is true
7807    when fucom should be used.  */
7808
7809 const char *
7810 output_fp_compare (insn, operands, eflags_p, unordered_p)
7811      rtx insn;
7812      rtx *operands;
7813      int eflags_p, unordered_p;
7814 {
7815   int stack_top_dies;
7816   rtx cmp_op0 = operands[0];
7817   rtx cmp_op1 = operands[1];
7818   int is_sse = SSE_REG_P (operands[0]) | SSE_REG_P (operands[1]);
7819
7820   if (eflags_p == 2)
7821     {
7822       cmp_op0 = cmp_op1;
7823       cmp_op1 = operands[2];
7824     }
7825   if (is_sse)
7826     {
7827       if (GET_MODE (operands[0]) == SFmode)
7828         if (unordered_p)
7829           return "ucomiss\t{%1, %0|%0, %1}";
7830         else
7831           return "comiss\t{%1, %0|%0, %1}";
7832       else
7833         if (unordered_p)
7834           return "ucomisd\t{%1, %0|%0, %1}";
7835         else
7836           return "comisd\t{%1, %0|%0, %1}";
7837     }
7838
7839   if (! STACK_TOP_P (cmp_op0))
7840     abort ();
7841
7842   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
7843
7844   if (STACK_REG_P (cmp_op1)
7845       && stack_top_dies
7846       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
7847       && REGNO (cmp_op1) != FIRST_STACK_REG)
7848     {
7849       /* If both the top of the 387 stack dies, and the other operand
7850          is also a stack register that dies, then this must be a
7851          `fcompp' float compare */
7852
7853       if (eflags_p == 1)
7854         {
7855           /* There is no double popping fcomi variant.  Fortunately,
7856              eflags is immune from the fstp's cc clobbering.  */
7857           if (unordered_p)
7858             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
7859           else
7860             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
7861           return "fstp\t%y0";
7862         }
7863       else
7864         {
7865           if (eflags_p == 2)
7866             {
7867               if (unordered_p)
7868                 return "fucompp\n\tfnstsw\t%0";
7869               else
7870                 return "fcompp\n\tfnstsw\t%0";
7871             }
7872           else
7873             {
7874               if (unordered_p)
7875                 return "fucompp";
7876               else
7877                 return "fcompp";
7878             }
7879         }
7880     }
7881   else
7882     {
7883       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
7884
7885       static const char * const alt[24] =
7886       {
7887         "fcom%z1\t%y1",
7888         "fcomp%z1\t%y1",
7889         "fucom%z1\t%y1",
7890         "fucomp%z1\t%y1",
7891
7892         "ficom%z1\t%y1",
7893         "ficomp%z1\t%y1",
7894         NULL,
7895         NULL,
7896
7897         "fcomi\t{%y1, %0|%0, %y1}",
7898         "fcomip\t{%y1, %0|%0, %y1}",
7899         "fucomi\t{%y1, %0|%0, %y1}",
7900         "fucomip\t{%y1, %0|%0, %y1}",
7901
7902         NULL,
7903         NULL,
7904         NULL,
7905         NULL,
7906
7907         "fcom%z2\t%y2\n\tfnstsw\t%0",
7908         "fcomp%z2\t%y2\n\tfnstsw\t%0",
7909         "fucom%z2\t%y2\n\tfnstsw\t%0",
7910         "fucomp%z2\t%y2\n\tfnstsw\t%0",
7911
7912         "ficom%z2\t%y2\n\tfnstsw\t%0",
7913         "ficomp%z2\t%y2\n\tfnstsw\t%0",
7914         NULL,
7915         NULL
7916       };
7917
7918       int mask;
7919       const char *ret;
7920
7921       mask  = eflags_p << 3;
7922       mask |= (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT) << 2;
7923       mask |= unordered_p << 1;
7924       mask |= stack_top_dies;
7925
7926       if (mask >= 24)
7927         abort ();
7928       ret = alt[mask];
7929       if (ret == NULL)
7930         abort ();
7931
7932       return ret;
7933     }
7934 }
7935
7936 void
7937 ix86_output_addr_vec_elt (file, value)
7938      FILE *file;
7939      int value;
7940 {
7941   const char *directive = ASM_LONG;
7942
7943   if (TARGET_64BIT)
7944     {
7945 #ifdef ASM_QUAD
7946       directive = ASM_QUAD;
7947 #else
7948       abort ();
7949 #endif
7950     }
7951
7952   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
7953 }
7954
7955 void
7956 ix86_output_addr_diff_elt (file, value, rel)
7957      FILE *file;
7958      int value, rel;
7959 {
7960   if (TARGET_64BIT)
7961     fprintf (file, "%s%s%d-%s%d\n",
7962              ASM_LONG, LPREFIX, value, LPREFIX, rel);
7963   else if (HAVE_AS_GOTOFF_IN_DATA)
7964     fprintf (file, "%s%s%d@GOTOFF\n", ASM_LONG, LPREFIX, value);
7965 #if TARGET_MACHO
7966   else if (TARGET_MACHO)
7967     fprintf (file, "%s%s%d-%s\n", ASM_LONG, LPREFIX, value,
7968              machopic_function_base_name () + 1);
7969 #endif
7970   else
7971     asm_fprintf (file, "%s%U%s+[.-%s%d]\n",
7972                  ASM_LONG, GOT_SYMBOL_NAME, LPREFIX, value);
7973 }
7974 \f
7975 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
7976    for the target.  */
7977
7978 void
7979 ix86_expand_clear (dest)
7980      rtx dest;
7981 {
7982   rtx tmp;
7983
7984   /* We play register width games, which are only valid after reload.  */
7985   if (!reload_completed)
7986     abort ();
7987
7988   /* Avoid HImode and its attendant prefix byte.  */
7989   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
7990     dest = gen_rtx_REG (SImode, REGNO (dest));
7991
7992   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
7993
7994   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
7995   if (reload_completed && (!TARGET_USE_MOV0 || optimize_size))
7996     {
7997       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, 17));
7998       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
7999     }
8000
8001   emit_insn (tmp);
8002 }
8003
8004 /* X is an unchanging MEM.  If it is a constant pool reference, return
8005    the constant pool rtx, else NULL.  */
8006
8007 static rtx
8008 maybe_get_pool_constant (x)
8009      rtx x;
8010 {
8011   x = XEXP (x, 0);
8012
8013   if (flag_pic && ! TARGET_64BIT)
8014     {
8015       if (GET_CODE (x) != PLUS)
8016         return NULL_RTX;
8017       if (XEXP (x, 0) != pic_offset_table_rtx)
8018         return NULL_RTX;
8019       x = XEXP (x, 1);
8020       if (GET_CODE (x) != CONST)
8021         return NULL_RTX;
8022       x = XEXP (x, 0);
8023       if (GET_CODE (x) != UNSPEC)
8024         return NULL_RTX;
8025       if (XINT (x, 1) != UNSPEC_GOTOFF)
8026         return NULL_RTX;
8027       x = XVECEXP (x, 0, 0);
8028     }
8029
8030   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
8031     return get_pool_constant (x);
8032
8033   return NULL_RTX;
8034 }
8035
8036 void
8037 ix86_expand_move (mode, operands)
8038      enum machine_mode mode;
8039      rtx operands[];
8040 {
8041   int strict = (reload_in_progress || reload_completed);
8042   rtx insn, op0, op1, tmp;
8043
8044   op0 = operands[0];
8045   op1 = operands[1];
8046
8047   if (tls_symbolic_operand (op1, Pmode))
8048     {
8049       op1 = legitimize_address (op1, op1, VOIDmode);
8050       if (GET_CODE (op0) == MEM)
8051         {
8052           tmp = gen_reg_rtx (mode);
8053           emit_insn (gen_rtx_SET (VOIDmode, tmp, op1));
8054           op1 = tmp;
8055         }
8056     }
8057   else if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
8058     {
8059 #if TARGET_MACHO
8060       if (MACHOPIC_PURE)
8061         {
8062           rtx temp = ((reload_in_progress
8063                        || ((op0 && GET_CODE (op0) == REG)
8064                            && mode == Pmode))
8065                       ? op0 : gen_reg_rtx (Pmode));
8066           op1 = machopic_indirect_data_reference (op1, temp);
8067           op1 = machopic_legitimize_pic_address (op1, mode,
8068                                                  temp == op1 ? 0 : temp);
8069         }
8070       else
8071         {
8072           if (MACHOPIC_INDIRECT)
8073             op1 = machopic_indirect_data_reference (op1, 0);
8074         }
8075       if (op0 != op1)
8076         {
8077           insn = gen_rtx_SET (VOIDmode, op0, op1);
8078           emit_insn (insn);
8079         }
8080       return;
8081 #endif /* TARGET_MACHO */
8082       if (GET_CODE (op0) == MEM)
8083         op1 = force_reg (Pmode, op1);
8084       else
8085         {
8086           rtx temp = op0;
8087           if (GET_CODE (temp) != REG)
8088             temp = gen_reg_rtx (Pmode);
8089           temp = legitimize_pic_address (op1, temp);
8090           if (temp == op0)
8091             return;
8092           op1 = temp;
8093         }
8094     }
8095   else
8096     {
8097       if (GET_CODE (op0) == MEM
8098           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
8099               || !push_operand (op0, mode))
8100           && GET_CODE (op1) == MEM)
8101         op1 = force_reg (mode, op1);
8102
8103       if (push_operand (op0, mode)
8104           && ! general_no_elim_operand (op1, mode))
8105         op1 = copy_to_mode_reg (mode, op1);
8106
8107       /* Force large constants in 64bit compilation into register
8108          to get them CSEed.  */
8109       if (TARGET_64BIT && mode == DImode
8110           && immediate_operand (op1, mode)
8111           && !x86_64_zero_extended_value (op1)
8112           && !register_operand (op0, mode)
8113           && optimize && !reload_completed && !reload_in_progress)
8114         op1 = copy_to_mode_reg (mode, op1);
8115
8116       if (FLOAT_MODE_P (mode))
8117         {
8118           /* If we are loading a floating point constant to a register,
8119              force the value to memory now, since we'll get better code
8120              out the back end.  */
8121
8122           if (strict)
8123             ;
8124           else if (GET_CODE (op1) == CONST_DOUBLE
8125                    && register_operand (op0, mode))
8126             op1 = validize_mem (force_const_mem (mode, op1));
8127         }
8128     }
8129
8130   insn = gen_rtx_SET (VOIDmode, op0, op1);
8131
8132   emit_insn (insn);
8133 }
8134
8135 void
8136 ix86_expand_vector_move (mode, operands)
8137      enum machine_mode mode;
8138      rtx operands[];
8139 {
8140   /* Force constants other than zero into memory.  We do not know how
8141      the instructions used to build constants modify the upper 64 bits
8142      of the register, once we have that information we may be able
8143      to handle some of them more efficiently.  */
8144   if ((reload_in_progress | reload_completed) == 0
8145       && register_operand (operands[0], mode)
8146       && CONSTANT_P (operands[1]))
8147     operands[1] = validize_mem (force_const_mem (mode, operands[1]));
8148
8149   /* Make operand1 a register if it isn't already.  */
8150   if (!no_new_pseudos
8151       && !register_operand (operands[0], mode)
8152       && !register_operand (operands[1], mode))
8153     {
8154       rtx temp = force_reg (GET_MODE (operands[1]), operands[1]);
8155       emit_move_insn (operands[0], temp);
8156       return;
8157     }
8158
8159   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8160 }
8161
8162 /* Attempt to expand a binary operator.  Make the expansion closer to the
8163    actual machine, then just general_operand, which will allow 3 separate
8164    memory references (one output, two input) in a single insn.  */
8165
8166 void
8167 ix86_expand_binary_operator (code, mode, operands)
8168      enum rtx_code code;
8169      enum machine_mode mode;
8170      rtx operands[];
8171 {
8172   int matching_memory;
8173   rtx src1, src2, dst, op, clob;
8174
8175   dst = operands[0];
8176   src1 = operands[1];
8177   src2 = operands[2];
8178
8179   /* Recognize <var1> = <value> <op> <var1> for commutative operators */
8180   if (GET_RTX_CLASS (code) == 'c'
8181       && (rtx_equal_p (dst, src2)
8182           || immediate_operand (src1, mode)))
8183     {
8184       rtx temp = src1;
8185       src1 = src2;
8186       src2 = temp;
8187     }
8188
8189   /* If the destination is memory, and we do not have matching source
8190      operands, do things in registers.  */
8191   matching_memory = 0;
8192   if (GET_CODE (dst) == MEM)
8193     {
8194       if (rtx_equal_p (dst, src1))
8195         matching_memory = 1;
8196       else if (GET_RTX_CLASS (code) == 'c'
8197                && rtx_equal_p (dst, src2))
8198         matching_memory = 2;
8199       else
8200         dst = gen_reg_rtx (mode);
8201     }
8202
8203   /* Both source operands cannot be in memory.  */
8204   if (GET_CODE (src1) == MEM && GET_CODE (src2) == MEM)
8205     {
8206       if (matching_memory != 2)
8207         src2 = force_reg (mode, src2);
8208       else
8209         src1 = force_reg (mode, src1);
8210     }
8211
8212   /* If the operation is not commutable, source 1 cannot be a constant
8213      or non-matching memory.  */
8214   if ((CONSTANT_P (src1)
8215        || (!matching_memory && GET_CODE (src1) == MEM))
8216       && GET_RTX_CLASS (code) != 'c')
8217     src1 = force_reg (mode, src1);
8218
8219   /* If optimizing, copy to regs to improve CSE */
8220   if (optimize && ! no_new_pseudos)
8221     {
8222       if (GET_CODE (dst) == MEM)
8223         dst = gen_reg_rtx (mode);
8224       if (GET_CODE (src1) == MEM)
8225         src1 = force_reg (mode, src1);
8226       if (GET_CODE (src2) == MEM)
8227         src2 = force_reg (mode, src2);
8228     }
8229
8230   /* Emit the instruction.  */
8231
8232   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
8233   if (reload_in_progress)
8234     {
8235       /* Reload doesn't know about the flags register, and doesn't know that
8236          it doesn't want to clobber it.  We can only do this with PLUS.  */
8237       if (code != PLUS)
8238         abort ();
8239       emit_insn (op);
8240     }
8241   else
8242     {
8243       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
8244       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
8245     }
8246
8247   /* Fix up the destination if needed.  */
8248   if (dst != operands[0])
8249     emit_move_insn (operands[0], dst);
8250 }
8251
8252 /* Return TRUE or FALSE depending on whether the binary operator meets the
8253    appropriate constraints.  */
8254
8255 int
8256 ix86_binary_operator_ok (code, mode, operands)
8257      enum rtx_code code;
8258      enum machine_mode mode ATTRIBUTE_UNUSED;
8259      rtx operands[3];
8260 {
8261   /* Both source operands cannot be in memory.  */
8262   if (GET_CODE (operands[1]) == MEM && GET_CODE (operands[2]) == MEM)
8263     return 0;
8264   /* If the operation is not commutable, source 1 cannot be a constant.  */
8265   if (CONSTANT_P (operands[1]) && GET_RTX_CLASS (code) != 'c')
8266     return 0;
8267   /* If the destination is memory, we must have a matching source operand.  */
8268   if (GET_CODE (operands[0]) == MEM
8269       && ! (rtx_equal_p (operands[0], operands[1])
8270             || (GET_RTX_CLASS (code) == 'c'
8271                 && rtx_equal_p (operands[0], operands[2]))))
8272     return 0;
8273   /* If the operation is not commutable and the source 1 is memory, we must
8274      have a matching destination.  */
8275   if (GET_CODE (operands[1]) == MEM
8276       && GET_RTX_CLASS (code) != 'c'
8277       && ! rtx_equal_p (operands[0], operands[1]))
8278     return 0;
8279   return 1;
8280 }
8281
8282 /* Attempt to expand a unary operator.  Make the expansion closer to the
8283    actual machine, then just general_operand, which will allow 2 separate
8284    memory references (one output, one input) in a single insn.  */
8285
8286 void
8287 ix86_expand_unary_operator (code, mode, operands)
8288      enum rtx_code code;
8289      enum machine_mode mode;
8290      rtx operands[];
8291 {
8292   int matching_memory;
8293   rtx src, dst, op, clob;
8294
8295   dst = operands[0];
8296   src = operands[1];
8297
8298   /* If the destination is memory, and we do not have matching source
8299      operands, do things in registers.  */
8300   matching_memory = 0;
8301   if (GET_CODE (dst) == MEM)
8302     {
8303       if (rtx_equal_p (dst, src))
8304         matching_memory = 1;
8305       else
8306         dst = gen_reg_rtx (mode);
8307     }
8308
8309   /* When source operand is memory, destination must match.  */
8310   if (!matching_memory && GET_CODE (src) == MEM)
8311     src = force_reg (mode, src);
8312
8313   /* If optimizing, copy to regs to improve CSE */
8314   if (optimize && ! no_new_pseudos)
8315     {
8316       if (GET_CODE (dst) == MEM)
8317         dst = gen_reg_rtx (mode);
8318       if (GET_CODE (src) == MEM)
8319         src = force_reg (mode, src);
8320     }
8321
8322   /* Emit the instruction.  */
8323
8324   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
8325   if (reload_in_progress || code == NOT)
8326     {
8327       /* Reload doesn't know about the flags register, and doesn't know that
8328          it doesn't want to clobber it.  */
8329       if (code != NOT)
8330         abort ();
8331       emit_insn (op);
8332     }
8333   else
8334     {
8335       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
8336       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
8337     }
8338
8339   /* Fix up the destination if needed.  */
8340   if (dst != operands[0])
8341     emit_move_insn (operands[0], dst);
8342 }
8343
8344 /* Return TRUE or FALSE depending on whether the unary operator meets the
8345    appropriate constraints.  */
8346
8347 int
8348 ix86_unary_operator_ok (code, mode, operands)
8349      enum rtx_code code ATTRIBUTE_UNUSED;
8350      enum machine_mode mode ATTRIBUTE_UNUSED;
8351      rtx operands[2] ATTRIBUTE_UNUSED;
8352 {
8353   /* If one of operands is memory, source and destination must match.  */
8354   if ((GET_CODE (operands[0]) == MEM
8355        || GET_CODE (operands[1]) == MEM)
8356       && ! rtx_equal_p (operands[0], operands[1]))
8357     return FALSE;
8358   return TRUE;
8359 }
8360
8361 /* Return TRUE or FALSE depending on whether the first SET in INSN
8362    has source and destination with matching CC modes, and that the
8363    CC mode is at least as constrained as REQ_MODE.  */
8364
8365 int
8366 ix86_match_ccmode (insn, req_mode)
8367      rtx insn;
8368      enum machine_mode req_mode;
8369 {
8370   rtx set;
8371   enum machine_mode set_mode;
8372
8373   set = PATTERN (insn);
8374   if (GET_CODE (set) == PARALLEL)
8375     set = XVECEXP (set, 0, 0);
8376   if (GET_CODE (set) != SET)
8377     abort ();
8378   if (GET_CODE (SET_SRC (set)) != COMPARE)
8379     abort ();
8380
8381   set_mode = GET_MODE (SET_DEST (set));
8382   switch (set_mode)
8383     {
8384     case CCNOmode:
8385       if (req_mode != CCNOmode
8386           && (req_mode != CCmode
8387               || XEXP (SET_SRC (set), 1) != const0_rtx))
8388         return 0;
8389       break;
8390     case CCmode:
8391       if (req_mode == CCGCmode)
8392         return 0;
8393       /* FALLTHRU */
8394     case CCGCmode:
8395       if (req_mode == CCGOCmode || req_mode == CCNOmode)
8396         return 0;
8397       /* FALLTHRU */
8398     case CCGOCmode:
8399       if (req_mode == CCZmode)
8400         return 0;
8401       /* FALLTHRU */
8402     case CCZmode:
8403       break;
8404
8405     default:
8406       abort ();
8407     }
8408
8409   return (GET_MODE (SET_SRC (set)) == set_mode);
8410 }
8411
8412 /* Generate insn patterns to do an integer compare of OPERANDS.  */
8413
8414 static rtx
8415 ix86_expand_int_compare (code, op0, op1)
8416      enum rtx_code code;
8417      rtx op0, op1;
8418 {
8419   enum machine_mode cmpmode;
8420   rtx tmp, flags;
8421
8422   cmpmode = SELECT_CC_MODE (code, op0, op1);
8423   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
8424
8425   /* This is very simple, but making the interface the same as in the
8426      FP case makes the rest of the code easier.  */
8427   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
8428   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
8429
8430   /* Return the test that should be put into the flags user, i.e.
8431      the bcc, scc, or cmov instruction.  */
8432   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
8433 }
8434
8435 /* Figure out whether to use ordered or unordered fp comparisons.
8436    Return the appropriate mode to use.  */
8437
8438 enum machine_mode
8439 ix86_fp_compare_mode (code)
8440      enum rtx_code code ATTRIBUTE_UNUSED;
8441 {
8442   /* ??? In order to make all comparisons reversible, we do all comparisons
8443      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
8444      all forms trapping and nontrapping comparisons, we can make inequality
8445      comparisons trapping again, since it results in better code when using
8446      FCOM based compares.  */
8447   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
8448 }
8449
8450 enum machine_mode
8451 ix86_cc_mode (code, op0, op1)
8452      enum rtx_code code;
8453      rtx op0, op1;
8454 {
8455   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
8456     return ix86_fp_compare_mode (code);
8457   switch (code)
8458     {
8459       /* Only zero flag is needed.  */
8460     case EQ:                    /* ZF=0 */
8461     case NE:                    /* ZF!=0 */
8462       return CCZmode;
8463       /* Codes needing carry flag.  */
8464     case GEU:                   /* CF=0 */
8465     case GTU:                   /* CF=0 & ZF=0 */
8466     case LTU:                   /* CF=1 */
8467     case LEU:                   /* CF=1 | ZF=1 */
8468       return CCmode;
8469       /* Codes possibly doable only with sign flag when
8470          comparing against zero.  */
8471     case GE:                    /* SF=OF   or   SF=0 */
8472     case LT:                    /* SF<>OF  or   SF=1 */
8473       if (op1 == const0_rtx)
8474         return CCGOCmode;
8475       else
8476         /* For other cases Carry flag is not required.  */
8477         return CCGCmode;
8478       /* Codes doable only with sign flag when comparing
8479          against zero, but we miss jump instruction for it
8480          so we need to use relational tests against overflow
8481          that thus needs to be zero.  */
8482     case GT:                    /* ZF=0 & SF=OF */
8483     case LE:                    /* ZF=1 | SF<>OF */
8484       if (op1 == const0_rtx)
8485         return CCNOmode;
8486       else
8487         return CCGCmode;
8488       /* strcmp pattern do (use flags) and combine may ask us for proper
8489          mode.  */
8490     case USE:
8491       return CCmode;
8492     default:
8493       abort ();
8494     }
8495 }
8496
8497 /* Return true if we should use an FCOMI instruction for this fp comparison.  */
8498
8499 int
8500 ix86_use_fcomi_compare (code)
8501      enum rtx_code code ATTRIBUTE_UNUSED;
8502 {
8503   enum rtx_code swapped_code = swap_condition (code);
8504   return ((ix86_fp_comparison_cost (code) == ix86_fp_comparison_fcomi_cost (code))
8505           || (ix86_fp_comparison_cost (swapped_code)
8506               == ix86_fp_comparison_fcomi_cost (swapped_code)));
8507 }
8508
8509 /* Swap, force into registers, or otherwise massage the two operands
8510    to a fp comparison.  The operands are updated in place; the new
8511    comparison code is returned.  */
8512
8513 static enum rtx_code
8514 ix86_prepare_fp_compare_args (code, pop0, pop1)
8515      enum rtx_code code;
8516      rtx *pop0, *pop1;
8517 {
8518   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
8519   rtx op0 = *pop0, op1 = *pop1;
8520   enum machine_mode op_mode = GET_MODE (op0);
8521   int is_sse = SSE_REG_P (op0) | SSE_REG_P (op1);
8522
8523   /* All of the unordered compare instructions only work on registers.
8524      The same is true of the XFmode compare instructions.  The same is
8525      true of the fcomi compare instructions.  */
8526
8527   if (!is_sse
8528       && (fpcmp_mode == CCFPUmode
8529           || op_mode == XFmode
8530           || op_mode == TFmode
8531           || ix86_use_fcomi_compare (code)))
8532     {
8533       op0 = force_reg (op_mode, op0);
8534       op1 = force_reg (op_mode, op1);
8535     }
8536   else
8537     {
8538       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
8539          things around if they appear profitable, otherwise force op0
8540          into a register.  */
8541
8542       if (standard_80387_constant_p (op0) == 0
8543           || (GET_CODE (op0) == MEM
8544               && ! (standard_80387_constant_p (op1) == 0
8545                     || GET_CODE (op1) == MEM)))
8546         {
8547           rtx tmp;
8548           tmp = op0, op0 = op1, op1 = tmp;
8549           code = swap_condition (code);
8550         }
8551
8552       if (GET_CODE (op0) != REG)
8553         op0 = force_reg (op_mode, op0);
8554
8555       if (CONSTANT_P (op1))
8556         {
8557           if (standard_80387_constant_p (op1))
8558             op1 = force_reg (op_mode, op1);
8559           else
8560             op1 = validize_mem (force_const_mem (op_mode, op1));
8561         }
8562     }
8563
8564   /* Try to rearrange the comparison to make it cheaper.  */
8565   if (ix86_fp_comparison_cost (code)
8566       > ix86_fp_comparison_cost (swap_condition (code))
8567       && (GET_CODE (op1) == REG || !no_new_pseudos))
8568     {
8569       rtx tmp;
8570       tmp = op0, op0 = op1, op1 = tmp;
8571       code = swap_condition (code);
8572       if (GET_CODE (op0) != REG)
8573         op0 = force_reg (op_mode, op0);
8574     }
8575
8576   *pop0 = op0;
8577   *pop1 = op1;
8578   return code;
8579 }
8580
8581 /* Convert comparison codes we use to represent FP comparison to integer
8582    code that will result in proper branch.  Return UNKNOWN if no such code
8583    is available.  */
8584 static enum rtx_code
8585 ix86_fp_compare_code_to_integer (code)
8586      enum rtx_code code;
8587 {
8588   switch (code)
8589     {
8590     case GT:
8591       return GTU;
8592     case GE:
8593       return GEU;
8594     case ORDERED:
8595     case UNORDERED:
8596       return code;
8597       break;
8598     case UNEQ:
8599       return EQ;
8600       break;
8601     case UNLT:
8602       return LTU;
8603       break;
8604     case UNLE:
8605       return LEU;
8606       break;
8607     case LTGT:
8608       return NE;
8609       break;
8610     default:
8611       return UNKNOWN;
8612     }
8613 }
8614
8615 /* Split comparison code CODE into comparisons we can do using branch
8616    instructions.  BYPASS_CODE is comparison code for branch that will
8617    branch around FIRST_CODE and SECOND_CODE.  If some of branches
8618    is not required, set value to NIL.
8619    We never require more than two branches.  */
8620 static void
8621 ix86_fp_comparison_codes (code, bypass_code, first_code, second_code)
8622      enum rtx_code code, *bypass_code, *first_code, *second_code;
8623 {
8624   *first_code = code;
8625   *bypass_code = NIL;
8626   *second_code = NIL;
8627
8628   /* The fcomi comparison sets flags as follows:
8629
8630      cmp    ZF PF CF
8631      >      0  0  0
8632      <      0  0  1
8633      =      1  0  0
8634      un     1  1  1 */
8635
8636   switch (code)
8637     {
8638     case GT:                    /* GTU - CF=0 & ZF=0 */
8639     case GE:                    /* GEU - CF=0 */
8640     case ORDERED:               /* PF=0 */
8641     case UNORDERED:             /* PF=1 */
8642     case UNEQ:                  /* EQ - ZF=1 */
8643     case UNLT:                  /* LTU - CF=1 */
8644     case UNLE:                  /* LEU - CF=1 | ZF=1 */
8645     case LTGT:                  /* EQ - ZF=0 */
8646       break;
8647     case LT:                    /* LTU - CF=1 - fails on unordered */
8648       *first_code = UNLT;
8649       *bypass_code = UNORDERED;
8650       break;
8651     case LE:                    /* LEU - CF=1 | ZF=1 - fails on unordered */
8652       *first_code = UNLE;
8653       *bypass_code = UNORDERED;
8654       break;
8655     case EQ:                    /* EQ - ZF=1 - fails on unordered */
8656       *first_code = UNEQ;
8657       *bypass_code = UNORDERED;
8658       break;
8659     case NE:                    /* NE - ZF=0 - fails on unordered */
8660       *first_code = LTGT;
8661       *second_code = UNORDERED;
8662       break;
8663     case UNGE:                  /* GEU - CF=0 - fails on unordered */
8664       *first_code = GE;
8665       *second_code = UNORDERED;
8666       break;
8667     case UNGT:                  /* GTU - CF=0 & ZF=0 - fails on unordered */
8668       *first_code = GT;
8669       *second_code = UNORDERED;
8670       break;
8671     default:
8672       abort ();
8673     }
8674   if (!TARGET_IEEE_FP)
8675     {
8676       *second_code = NIL;
8677       *bypass_code = NIL;
8678     }
8679 }
8680
8681 /* Return cost of comparison done fcom + arithmetics operations on AX.
8682    All following functions do use number of instructions as a cost metrics.
8683    In future this should be tweaked to compute bytes for optimize_size and
8684    take into account performance of various instructions on various CPUs.  */
8685 static int
8686 ix86_fp_comparison_arithmetics_cost (code)
8687      enum rtx_code code;
8688 {
8689   if (!TARGET_IEEE_FP)
8690     return 4;
8691   /* The cost of code output by ix86_expand_fp_compare.  */
8692   switch (code)
8693     {
8694     case UNLE:
8695     case UNLT:
8696     case LTGT:
8697     case GT:
8698     case GE:
8699     case UNORDERED:
8700     case ORDERED:
8701     case UNEQ:
8702       return 4;
8703       break;
8704     case LT:
8705     case NE:
8706     case EQ:
8707     case UNGE:
8708       return 5;
8709       break;
8710     case LE:
8711     case UNGT:
8712       return 6;
8713       break;
8714     default:
8715       abort ();
8716     }
8717 }
8718
8719 /* Return cost of comparison done using fcomi operation.
8720    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
8721 static int
8722 ix86_fp_comparison_fcomi_cost (code)
8723      enum rtx_code code;
8724 {
8725   enum rtx_code bypass_code, first_code, second_code;
8726   /* Return arbitrarily high cost when instruction is not supported - this
8727      prevents gcc from using it.  */
8728   if (!TARGET_CMOVE)
8729     return 1024;
8730   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
8731   return (bypass_code != NIL || second_code != NIL) + 2;
8732 }
8733
8734 /* Return cost of comparison done using sahf operation.
8735    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
8736 static int
8737 ix86_fp_comparison_sahf_cost (code)
8738      enum rtx_code code;
8739 {
8740   enum rtx_code bypass_code, first_code, second_code;
8741   /* Return arbitrarily high cost when instruction is not preferred - this
8742      avoids gcc from using it.  */
8743   if (!TARGET_USE_SAHF && !optimize_size)
8744     return 1024;
8745   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
8746   return (bypass_code != NIL || second_code != NIL) + 3;
8747 }
8748
8749 /* Compute cost of the comparison done using any method.
8750    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
8751 static int
8752 ix86_fp_comparison_cost (code)
8753      enum rtx_code code;
8754 {
8755   int fcomi_cost, sahf_cost, arithmetics_cost = 1024;
8756   int min;
8757
8758   fcomi_cost = ix86_fp_comparison_fcomi_cost (code);
8759   sahf_cost = ix86_fp_comparison_sahf_cost (code);
8760
8761   min = arithmetics_cost = ix86_fp_comparison_arithmetics_cost (code);
8762   if (min > sahf_cost)
8763     min = sahf_cost;
8764   if (min > fcomi_cost)
8765     min = fcomi_cost;
8766   return min;
8767 }
8768
8769 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
8770
8771 static rtx
8772 ix86_expand_fp_compare (code, op0, op1, scratch, second_test, bypass_test)
8773      enum rtx_code code;
8774      rtx op0, op1, scratch;
8775      rtx *second_test;
8776      rtx *bypass_test;
8777 {
8778   enum machine_mode fpcmp_mode, intcmp_mode;
8779   rtx tmp, tmp2;
8780   int cost = ix86_fp_comparison_cost (code);
8781   enum rtx_code bypass_code, first_code, second_code;
8782
8783   fpcmp_mode = ix86_fp_compare_mode (code);
8784   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
8785
8786   if (second_test)
8787     *second_test = NULL_RTX;
8788   if (bypass_test)
8789     *bypass_test = NULL_RTX;
8790
8791   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
8792
8793   /* Do fcomi/sahf based test when profitable.  */
8794   if ((bypass_code == NIL || bypass_test)
8795       && (second_code == NIL || second_test)
8796       && ix86_fp_comparison_arithmetics_cost (code) > cost)
8797     {
8798       if (TARGET_CMOVE)
8799         {
8800           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
8801           tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
8802                              tmp);
8803           emit_insn (tmp);
8804         }
8805       else
8806         {
8807           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
8808           tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
8809           if (!scratch)
8810             scratch = gen_reg_rtx (HImode);
8811           emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
8812           emit_insn (gen_x86_sahf_1 (scratch));
8813         }
8814
8815       /* The FP codes work out to act like unsigned.  */
8816       intcmp_mode = fpcmp_mode;
8817       code = first_code;
8818       if (bypass_code != NIL)
8819         *bypass_test = gen_rtx_fmt_ee (bypass_code, VOIDmode,
8820                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
8821                                        const0_rtx);
8822       if (second_code != NIL)
8823         *second_test = gen_rtx_fmt_ee (second_code, VOIDmode,
8824                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
8825                                        const0_rtx);
8826     }
8827   else
8828     {
8829       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
8830       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
8831       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
8832       if (!scratch)
8833         scratch = gen_reg_rtx (HImode);
8834       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
8835
8836       /* In the unordered case, we have to check C2 for NaN's, which
8837          doesn't happen to work out to anything nice combination-wise.
8838          So do some bit twiddling on the value we've got in AH to come
8839          up with an appropriate set of condition codes.  */
8840
8841       intcmp_mode = CCNOmode;
8842       switch (code)
8843         {
8844         case GT:
8845         case UNGT:
8846           if (code == GT || !TARGET_IEEE_FP)
8847             {
8848               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
8849               code = EQ;
8850             }
8851           else
8852             {
8853               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
8854               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
8855               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
8856               intcmp_mode = CCmode;
8857               code = GEU;
8858             }
8859           break;
8860         case LT:
8861         case UNLT:
8862           if (code == LT && TARGET_IEEE_FP)
8863             {
8864               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
8865               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x01)));
8866               intcmp_mode = CCmode;
8867               code = EQ;
8868             }
8869           else
8870             {
8871               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x01)));
8872               code = NE;
8873             }
8874           break;
8875         case GE:
8876         case UNGE:
8877           if (code == GE || !TARGET_IEEE_FP)
8878             {
8879               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
8880               code = EQ;
8881             }
8882           else
8883             {
8884               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
8885               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
8886                                              GEN_INT (0x01)));
8887               code = NE;
8888             }
8889           break;
8890         case LE:
8891         case UNLE:
8892           if (code == LE && TARGET_IEEE_FP)
8893             {
8894               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
8895               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
8896               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
8897               intcmp_mode = CCmode;
8898               code = LTU;
8899             }
8900           else
8901             {
8902               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
8903               code = NE;
8904             }
8905           break;
8906         case EQ:
8907         case UNEQ:
8908           if (code == EQ && TARGET_IEEE_FP)
8909             {
8910               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
8911               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
8912               intcmp_mode = CCmode;
8913               code = EQ;
8914             }
8915           else
8916             {
8917               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
8918               code = NE;
8919               break;
8920             }
8921           break;
8922         case NE:
8923         case LTGT:
8924           if (code == NE && TARGET_IEEE_FP)
8925             {
8926               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
8927               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
8928                                              GEN_INT (0x40)));
8929               code = NE;
8930             }
8931           else
8932             {
8933               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
8934               code = EQ;
8935             }
8936           break;
8937
8938         case UNORDERED:
8939           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
8940           code = NE;
8941           break;
8942         case ORDERED:
8943           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
8944           code = EQ;
8945           break;
8946
8947         default:
8948           abort ();
8949         }
8950     }
8951
8952   /* Return the test that should be put into the flags user, i.e.
8953      the bcc, scc, or cmov instruction.  */
8954   return gen_rtx_fmt_ee (code, VOIDmode,
8955                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
8956                          const0_rtx);
8957 }
8958
8959 rtx
8960 ix86_expand_compare (code, second_test, bypass_test)
8961      enum rtx_code code;
8962      rtx *second_test, *bypass_test;
8963 {
8964   rtx op0, op1, ret;
8965   op0 = ix86_compare_op0;
8966   op1 = ix86_compare_op1;
8967
8968   if (second_test)
8969     *second_test = NULL_RTX;
8970   if (bypass_test)
8971     *bypass_test = NULL_RTX;
8972
8973   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
8974     ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
8975                                   second_test, bypass_test);
8976   else
8977     ret = ix86_expand_int_compare (code, op0, op1);
8978
8979   return ret;
8980 }
8981
8982 /* Return true if the CODE will result in nontrivial jump sequence.  */
8983 bool
8984 ix86_fp_jump_nontrivial_p (code)
8985     enum rtx_code code;
8986 {
8987   enum rtx_code bypass_code, first_code, second_code;
8988   if (!TARGET_CMOVE)
8989     return true;
8990   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
8991   return bypass_code != NIL || second_code != NIL;
8992 }
8993
8994 void
8995 ix86_expand_branch (code, label)
8996      enum rtx_code code;
8997      rtx label;
8998 {
8999   rtx tmp;
9000
9001   switch (GET_MODE (ix86_compare_op0))
9002     {
9003     case QImode:
9004     case HImode:
9005     case SImode:
9006       simple:
9007       tmp = ix86_expand_compare (code, NULL, NULL);
9008       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
9009                                   gen_rtx_LABEL_REF (VOIDmode, label),
9010                                   pc_rtx);
9011       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
9012       return;
9013
9014     case SFmode:
9015     case DFmode:
9016     case XFmode:
9017     case TFmode:
9018       {
9019         rtvec vec;
9020         int use_fcomi;
9021         enum rtx_code bypass_code, first_code, second_code;
9022
9023         code = ix86_prepare_fp_compare_args (code, &ix86_compare_op0,
9024                                              &ix86_compare_op1);
9025
9026         ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
9027
9028         /* Check whether we will use the natural sequence with one jump.  If
9029            so, we can expand jump early.  Otherwise delay expansion by
9030            creating compound insn to not confuse optimizers.  */
9031         if (bypass_code == NIL && second_code == NIL
9032             && TARGET_CMOVE)
9033           {
9034             ix86_split_fp_branch (code, ix86_compare_op0, ix86_compare_op1,
9035                                   gen_rtx_LABEL_REF (VOIDmode, label),
9036                                   pc_rtx, NULL_RTX);
9037           }
9038         else
9039           {
9040             tmp = gen_rtx_fmt_ee (code, VOIDmode,
9041                                   ix86_compare_op0, ix86_compare_op1);
9042             tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
9043                                         gen_rtx_LABEL_REF (VOIDmode, label),
9044                                         pc_rtx);
9045             tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);
9046
9047             use_fcomi = ix86_use_fcomi_compare (code);
9048             vec = rtvec_alloc (3 + !use_fcomi);
9049             RTVEC_ELT (vec, 0) = tmp;
9050             RTVEC_ELT (vec, 1)
9051               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 18));
9052             RTVEC_ELT (vec, 2)
9053               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 17));
9054             if (! use_fcomi)
9055               RTVEC_ELT (vec, 3)
9056                 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));
9057
9058             emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
9059           }
9060         return;
9061       }
9062
9063     case DImode:
9064       if (TARGET_64BIT)
9065         goto simple;
9066       /* Expand DImode branch into multiple compare+branch.  */
9067       {
9068         rtx lo[2], hi[2], label2;
9069         enum rtx_code code1, code2, code3;
9070
9071         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
9072           {
9073             tmp = ix86_compare_op0;
9074             ix86_compare_op0 = ix86_compare_op1;
9075             ix86_compare_op1 = tmp;
9076             code = swap_condition (code);
9077           }
9078         split_di (&ix86_compare_op0, 1, lo+0, hi+0);
9079         split_di (&ix86_compare_op1, 1, lo+1, hi+1);
9080
9081         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
9082            avoid two branches.  This costs one extra insn, so disable when
9083            optimizing for size.  */
9084
9085         if ((code == EQ || code == NE)
9086             && (!optimize_size
9087                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
9088           {
9089             rtx xor0, xor1;
9090
9091             xor1 = hi[0];
9092             if (hi[1] != const0_rtx)
9093               xor1 = expand_binop (SImode, xor_optab, xor1, hi[1],
9094                                    NULL_RTX, 0, OPTAB_WIDEN);
9095
9096             xor0 = lo[0];
9097             if (lo[1] != const0_rtx)
9098               xor0 = expand_binop (SImode, xor_optab, xor0, lo[1],
9099                                    NULL_RTX, 0, OPTAB_WIDEN);
9100
9101             tmp = expand_binop (SImode, ior_optab, xor1, xor0,
9102                                 NULL_RTX, 0, OPTAB_WIDEN);
9103
9104             ix86_compare_op0 = tmp;
9105             ix86_compare_op1 = const0_rtx;
9106             ix86_expand_branch (code, label);
9107             return;
9108           }
9109
9110         /* Otherwise, if we are doing less-than or greater-or-equal-than,
9111            op1 is a constant and the low word is zero, then we can just
9112            examine the high word.  */
9113
9114         if (GET_CODE (hi[1]) == CONST_INT && lo[1] == const0_rtx)
9115           switch (code)
9116             {
9117             case LT: case LTU: case GE: case GEU:
9118               ix86_compare_op0 = hi[0];
9119               ix86_compare_op1 = hi[1];
9120               ix86_expand_branch (code, label);
9121               return;
9122             default:
9123               break;
9124             }
9125
9126         /* Otherwise, we need two or three jumps.  */
9127
9128         label2 = gen_label_rtx ();
9129
9130         code1 = code;
9131         code2 = swap_condition (code);
9132         code3 = unsigned_condition (code);
9133
9134         switch (code)
9135           {
9136           case LT: case GT: case LTU: case GTU:
9137             break;
9138
9139           case LE:   code1 = LT;  code2 = GT;  break;
9140           case GE:   code1 = GT;  code2 = LT;  break;
9141           case LEU:  code1 = LTU; code2 = GTU; break;
9142           case GEU:  code1 = GTU; code2 = LTU; break;
9143
9144           case EQ:   code1 = NIL; code2 = NE;  break;
9145           case NE:   code2 = NIL; break;
9146
9147           default:
9148             abort ();
9149           }
9150
9151         /*
9152          * a < b =>
9153          *    if (hi(a) < hi(b)) goto true;
9154          *    if (hi(a) > hi(b)) goto false;
9155          *    if (lo(a) < lo(b)) goto true;
9156          *  false:
9157          */
9158
9159         ix86_compare_op0 = hi[0];
9160         ix86_compare_op1 = hi[1];
9161
9162         if (code1 != NIL)
9163           ix86_expand_branch (code1, label);
9164         if (code2 != NIL)
9165           ix86_expand_branch (code2, label2);
9166
9167         ix86_compare_op0 = lo[0];
9168         ix86_compare_op1 = lo[1];
9169         ix86_expand_branch (code3, label);
9170
9171         if (code2 != NIL)
9172           emit_label (label2);
9173         return;
9174       }
9175
9176     default:
9177       abort ();
9178     }
9179 }
9180
9181 /* Split branch based on floating point condition.  */
9182 void
9183 ix86_split_fp_branch (code, op1, op2, target1, target2, tmp)
9184      enum rtx_code code;
9185      rtx op1, op2, target1, target2, tmp;
9186 {
9187   rtx second, bypass;
9188   rtx label = NULL_RTX;
9189   rtx condition;
9190   int bypass_probability = -1, second_probability = -1, probability = -1;
9191   rtx i;
9192
9193   if (target2 != pc_rtx)
9194     {
9195       rtx tmp = target2;
9196       code = reverse_condition_maybe_unordered (code);
9197       target2 = target1;
9198       target1 = tmp;
9199     }
9200
9201   condition = ix86_expand_fp_compare (code, op1, op2,
9202                                       tmp, &second, &bypass);
9203
9204   if (split_branch_probability >= 0)
9205     {
9206       /* Distribute the probabilities across the jumps.
9207          Assume the BYPASS and SECOND to be always test
9208          for UNORDERED.  */
9209       probability = split_branch_probability;
9210
9211       /* Value of 1 is low enough to make no need for probability
9212          to be updated.  Later we may run some experiments and see
9213          if unordered values are more frequent in practice.  */
9214       if (bypass)
9215         bypass_probability = 1;
9216       if (second)
9217         second_probability = 1;
9218     }
9219   if (bypass != NULL_RTX)
9220     {
9221       label = gen_label_rtx ();
9222       i = emit_jump_insn (gen_rtx_SET
9223                           (VOIDmode, pc_rtx,
9224                            gen_rtx_IF_THEN_ELSE (VOIDmode,
9225                                                  bypass,
9226                                                  gen_rtx_LABEL_REF (VOIDmode,
9227                                                                     label),
9228                                                  pc_rtx)));
9229       if (bypass_probability >= 0)
9230         REG_NOTES (i)
9231           = gen_rtx_EXPR_LIST (REG_BR_PROB,
9232                                GEN_INT (bypass_probability),
9233                                REG_NOTES (i));
9234     }
9235   i = emit_jump_insn (gen_rtx_SET
9236                       (VOIDmode, pc_rtx,
9237                        gen_rtx_IF_THEN_ELSE (VOIDmode,
9238                                              condition, target1, target2)));
9239   if (probability >= 0)
9240     REG_NOTES (i)
9241       = gen_rtx_EXPR_LIST (REG_BR_PROB,
9242                            GEN_INT (probability),
9243                            REG_NOTES (i));
9244   if (second != NULL_RTX)
9245     {
9246       i = emit_jump_insn (gen_rtx_SET
9247                           (VOIDmode, pc_rtx,
9248                            gen_rtx_IF_THEN_ELSE (VOIDmode, second, target1,
9249                                                  target2)));
9250       if (second_probability >= 0)
9251         REG_NOTES (i)
9252           = gen_rtx_EXPR_LIST (REG_BR_PROB,
9253                                GEN_INT (second_probability),
9254                                REG_NOTES (i));
9255     }
9256   if (label != NULL_RTX)
9257     emit_label (label);
9258 }
9259
9260 int
9261 ix86_expand_setcc (code, dest)
9262      enum rtx_code code;
9263      rtx dest;
9264 {
9265   rtx ret, tmp, tmpreg;
9266   rtx second_test, bypass_test;
9267
9268   if (GET_MODE (ix86_compare_op0) == DImode
9269       && !TARGET_64BIT)
9270     return 0; /* FAIL */
9271
9272   if (GET_MODE (dest) != QImode)
9273     abort ();
9274
9275   ret = ix86_expand_compare (code, &second_test, &bypass_test);
9276   PUT_MODE (ret, QImode);
9277
9278   tmp = dest;
9279   tmpreg = dest;
9280
9281   emit_insn (gen_rtx_SET (VOIDmode, tmp, ret));
9282   if (bypass_test || second_test)
9283     {
9284       rtx test = second_test;
9285       int bypass = 0;
9286       rtx tmp2 = gen_reg_rtx (QImode);
9287       if (bypass_test)
9288         {
9289           if (second_test)
9290             abort ();
9291           test = bypass_test;
9292           bypass = 1;
9293           PUT_CODE (test, reverse_condition_maybe_unordered (GET_CODE (test)));
9294         }
9295       PUT_MODE (test, QImode);
9296       emit_insn (gen_rtx_SET (VOIDmode, tmp2, test));
9297
9298       if (bypass)
9299         emit_insn (gen_andqi3 (tmp, tmpreg, tmp2));
9300       else
9301         emit_insn (gen_iorqi3 (tmp, tmpreg, tmp2));
9302     }
9303
9304   return 1; /* DONE */
9305 }
9306
9307 /* Expand comparison setting or clearing carry flag.  Return true when successful
9308    and set pop for the operation.  */
9309 bool
9310 ix86_expand_carry_flag_compare (code, op0, op1, pop)
9311      rtx op0, op1, *pop;
9312      enum rtx_code code;
9313 {
9314   enum machine_mode mode =
9315     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
9316
9317   /* Do not handle DImode compares that go trought special path.  Also we can't
9318      deal with FP compares yet.  This is possible to add.   */
9319   if ((mode == DImode && !TARGET_64BIT) || !INTEGRAL_MODE_P (mode))
9320     return false;
9321   switch (code)
9322     {
9323     case LTU:
9324     case GEU:
9325       break;
9326
9327     /* Convert a==0 into (unsigned)a<1.  */
9328     case EQ:
9329     case NE:
9330       if (op1 != const0_rtx)
9331         return false;
9332       op1 = const1_rtx;
9333       code = (code == EQ ? LTU : GEU);
9334       break;
9335
9336     /* Convert a>b into b<a or a>=b-1.  */
9337     case GTU:
9338     case LEU:
9339       if (GET_CODE (op1) == CONST_INT)
9340         {
9341           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
9342           /* Bail out on overflow.  We still can swap operands but that
9343              would force loading of the constant into register. */
9344           if (op1 == const0_rtx
9345               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
9346             return false;
9347           code = (code == GTU ? GEU : LTU);
9348         }
9349       else
9350         {
9351           rtx tmp = op1;
9352           op1 = op0;
9353           op0 = tmp;
9354           code = (code == GTU ? LTU : GEU);
9355         }
9356       break;
9357
9358     /* Convert a>0 into (unsigned)a<0x7fffffff.  */
9359     case LT:
9360     case GE:
9361       if (mode == DImode || op1 != const0_rtx)
9362         return false;
9363       op1 = gen_int_mode (~(1 << (GET_MODE_BITSIZE (mode) - 1)), mode);
9364       code = (code == LT ? GEU : LTU);
9365       break;
9366     case LE:
9367     case GT:
9368       if (mode == DImode || op1 != constm1_rtx)
9369         return false;
9370       op1 = gen_int_mode (~(1 << (GET_MODE_BITSIZE (mode) - 1)), mode);
9371       code = (code == LE ? GEU : LTU);
9372       break;
9373
9374     default:
9375       return false;
9376     }
9377   ix86_compare_op0 = op0;
9378   ix86_compare_op1 = op1;
9379   *pop = ix86_expand_compare (code, NULL, NULL);
9380   if (GET_CODE (*pop) != LTU && GET_CODE (*pop) != GEU)
9381     abort ();
9382   return true;
9383 }
9384
9385 int
9386 ix86_expand_int_movcc (operands)
9387      rtx operands[];
9388 {
9389   enum rtx_code code = GET_CODE (operands[1]), compare_code;
9390   rtx compare_seq, compare_op;
9391   rtx second_test, bypass_test;
9392   enum machine_mode mode = GET_MODE (operands[0]);
9393   bool sign_bit_compare_p = false;;
9394
9395   start_sequence ();
9396   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
9397   compare_seq = get_insns ();
9398   end_sequence ();
9399
9400   compare_code = GET_CODE (compare_op);
9401
9402   if ((ix86_compare_op1 == const0_rtx && (code == GE || code == LT))
9403       || (ix86_compare_op1 == constm1_rtx && (code == GT || code == LE)))
9404     sign_bit_compare_p = true;
9405
9406   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
9407      HImode insns, we'd be swallowed in word prefix ops.  */
9408
9409   if ((mode != HImode || TARGET_FAST_PREFIX)
9410       && (mode != DImode || TARGET_64BIT)
9411       && GET_CODE (operands[2]) == CONST_INT
9412       && GET_CODE (operands[3]) == CONST_INT)
9413     {
9414       rtx out = operands[0];
9415       HOST_WIDE_INT ct = INTVAL (operands[2]);
9416       HOST_WIDE_INT cf = INTVAL (operands[3]);
9417       HOST_WIDE_INT diff;
9418
9419       diff = ct - cf;
9420       /*  Sign bit compares are better done using shifts than we do by using
9421           sbb.  */
9422       if (sign_bit_compare_p
9423           || ix86_expand_carry_flag_compare (code, ix86_compare_op0,
9424                                              ix86_compare_op1, &compare_op))
9425         {
9426           /* Detect overlap between destination and compare sources.  */
9427           rtx tmp = out;
9428
9429           if (!sign_bit_compare_p)
9430             {
9431               compare_code = GET_CODE (compare_op);
9432
9433               /* To simplify rest of code, restrict to the GEU case.  */
9434               if (compare_code == LTU)
9435                 {
9436                   HOST_WIDE_INT tmp = ct;
9437                   ct = cf;
9438                   cf = tmp;
9439                   compare_code = reverse_condition (compare_code);
9440                   code = reverse_condition (code);
9441                 }
9442               diff = ct - cf;
9443
9444               if (reg_overlap_mentioned_p (out, ix86_compare_op0)
9445                   || reg_overlap_mentioned_p (out, ix86_compare_op1))
9446                 tmp = gen_reg_rtx (mode);
9447
9448               if (mode == DImode)
9449                 emit_insn (gen_x86_movdicc_0_m1_rex64 (tmp));
9450               else
9451                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp)));
9452             }
9453           else
9454             {
9455               if (code == GT || code == GE)
9456                 code = reverse_condition (code);
9457               else
9458                 {
9459                   HOST_WIDE_INT tmp = ct;
9460                   ct = cf;
9461                   cf = tmp;
9462                 }
9463               tmp = emit_store_flag (tmp, code, ix86_compare_op0,
9464                                      ix86_compare_op1, VOIDmode, 0, -1);
9465             }
9466
9467           if (diff == 1)
9468             {
9469               /*
9470                * cmpl op0,op1
9471                * sbbl dest,dest
9472                * [addl dest, ct]
9473                *
9474                * Size 5 - 8.
9475                */
9476               if (ct)
9477                 tmp = expand_simple_binop (mode, PLUS,
9478                                            tmp, GEN_INT (ct),
9479                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
9480             }
9481           else if (cf == -1)
9482             {
9483               /*
9484                * cmpl op0,op1
9485                * sbbl dest,dest
9486                * orl $ct, dest
9487                *
9488                * Size 8.
9489                */
9490               tmp = expand_simple_binop (mode, IOR,
9491                                          tmp, GEN_INT (ct),
9492                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
9493             }
9494           else if (diff == -1 && ct)
9495             {
9496               /*
9497                * cmpl op0,op1
9498                * sbbl dest,dest
9499                * notl dest
9500                * [addl dest, cf]
9501                *
9502                * Size 8 - 11.
9503                */
9504               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
9505               if (cf)
9506                 tmp = expand_simple_binop (mode, PLUS,
9507                                            copy_rtx (tmp), GEN_INT (cf),
9508                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
9509             }
9510           else
9511             {
9512               /*
9513                * cmpl op0,op1
9514                * sbbl dest,dest
9515                * [notl dest]
9516                * andl cf - ct, dest
9517                * [addl dest, ct]
9518                *
9519                * Size 8 - 11.
9520                */
9521
9522               if (cf == 0)
9523                 {
9524                   cf = ct;
9525                   ct = 0;
9526                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
9527                 }
9528
9529               tmp = expand_simple_binop (mode, AND,
9530                                          copy_rtx (tmp),
9531                                          gen_int_mode (cf - ct, mode),
9532                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
9533               if (ct)
9534                 tmp = expand_simple_binop (mode, PLUS,
9535                                            copy_rtx (tmp), GEN_INT (ct),
9536                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
9537             }
9538
9539           if (!rtx_equal_p (tmp, out))
9540             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
9541
9542           return 1; /* DONE */
9543         }
9544
9545       if (diff < 0)
9546         {
9547           HOST_WIDE_INT tmp;
9548           tmp = ct, ct = cf, cf = tmp;
9549           diff = -diff;
9550           if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
9551             {
9552               /* We may be reversing unordered compare to normal compare, that
9553                  is not valid in general (we may convert non-trapping condition
9554                  to trapping one), however on i386 we currently emit all
9555                  comparisons unordered.  */
9556               compare_code = reverse_condition_maybe_unordered (compare_code);
9557               code = reverse_condition_maybe_unordered (code);
9558             }
9559           else
9560             {
9561               compare_code = reverse_condition (compare_code);
9562               code = reverse_condition (code);
9563             }
9564         }
9565
9566       compare_code = NIL;
9567       if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_INT
9568           && GET_CODE (ix86_compare_op1) == CONST_INT)
9569         {
9570           if (ix86_compare_op1 == const0_rtx
9571               && (code == LT || code == GE))
9572             compare_code = code;
9573           else if (ix86_compare_op1 == constm1_rtx)
9574             {
9575               if (code == LE)
9576                 compare_code = LT;
9577               else if (code == GT)
9578                 compare_code = GE;
9579             }
9580         }
9581
9582       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
9583       if (compare_code != NIL
9584           && GET_MODE (ix86_compare_op0) == GET_MODE (out)
9585           && (cf == -1 || ct == -1))
9586         {
9587           /* If lea code below could be used, only optimize
9588              if it results in a 2 insn sequence.  */
9589
9590           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
9591                  || diff == 3 || diff == 5 || diff == 9)
9592               || (compare_code == LT && ct == -1)
9593               || (compare_code == GE && cf == -1))
9594             {
9595               /*
9596                * notl op1       (if necessary)
9597                * sarl $31, op1
9598                * orl cf, op1
9599                */
9600               if (ct != -1)
9601                 {
9602                   cf = ct;
9603                   ct = -1;
9604                   code = reverse_condition (code);
9605                 }
9606
9607               out = emit_store_flag (out, code, ix86_compare_op0,
9608                                      ix86_compare_op1, VOIDmode, 0, -1);
9609
9610               out = expand_simple_binop (mode, IOR,
9611                                          out, GEN_INT (cf),
9612                                          out, 1, OPTAB_DIRECT);
9613               if (out != operands[0])
9614                 emit_move_insn (operands[0], out);
9615
9616               return 1; /* DONE */
9617             }
9618         }
9619
9620
9621       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
9622            || diff == 3 || diff == 5 || diff == 9)
9623           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
9624           && (mode != DImode || x86_64_sign_extended_value (GEN_INT (cf))))
9625         {
9626           /*
9627            * xorl dest,dest
9628            * cmpl op1,op2
9629            * setcc dest
9630            * lea cf(dest*(ct-cf)),dest
9631            *
9632            * Size 14.
9633            *
9634            * This also catches the degenerate setcc-only case.
9635            */
9636
9637           rtx tmp;
9638           int nops;
9639
9640           out = emit_store_flag (out, code, ix86_compare_op0,
9641                                  ix86_compare_op1, VOIDmode, 0, 1);
9642
9643           nops = 0;
9644           /* On x86_64 the lea instruction operates on Pmode, so we need
9645              to get arithmetics done in proper mode to match.  */
9646           if (diff == 1)
9647             tmp = copy_rtx (out);
9648           else
9649             {
9650               rtx out1;
9651               out1 = copy_rtx (out);
9652               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
9653               nops++;
9654               if (diff & 1)
9655                 {
9656                   tmp = gen_rtx_PLUS (mode, tmp, out1);
9657                   nops++;
9658                 }
9659             }
9660           if (cf != 0)
9661             {
9662               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
9663               nops++;
9664             }
9665           if (!rtx_equal_p (tmp, out))
9666             {
9667               if (nops == 1)
9668                 out = force_operand (tmp, copy_rtx (out));
9669               else
9670                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
9671             }
9672           if (!rtx_equal_p (out, operands[0]))
9673             emit_move_insn (operands[0], copy_rtx (out));
9674
9675           return 1; /* DONE */
9676         }
9677
9678       /*
9679        * General case:                  Jumpful:
9680        *   xorl dest,dest               cmpl op1, op2
9681        *   cmpl op1, op2                movl ct, dest
9682        *   setcc dest                   jcc 1f
9683        *   decl dest                    movl cf, dest
9684        *   andl (cf-ct),dest            1:
9685        *   addl ct,dest
9686        *
9687        * Size 20.                       Size 14.
9688        *
9689        * This is reasonably steep, but branch mispredict costs are
9690        * high on modern cpus, so consider failing only if optimizing
9691        * for space.
9692        */
9693
9694       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
9695           && BRANCH_COST >= 2)
9696         {
9697           if (cf == 0)
9698             {
9699               cf = ct;
9700               ct = 0;
9701               if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
9702                 /* We may be reversing unordered compare to normal compare,
9703                    that is not valid in general (we may convert non-trapping
9704                    condition to trapping one), however on i386 we currently
9705                    emit all comparisons unordered.  */
9706                 code = reverse_condition_maybe_unordered (code);
9707               else
9708                 {
9709                   code = reverse_condition (code);
9710                   if (compare_code != NIL)
9711                     compare_code = reverse_condition (compare_code);
9712                 }
9713             }
9714
9715           if (compare_code != NIL)
9716             {
9717               /* notl op1       (if needed)
9718                  sarl $31, op1
9719                  andl (cf-ct), op1
9720                  addl ct, op1
9721
9722                  For x < 0 (resp. x <= -1) there will be no notl,
9723                  so if possible swap the constants to get rid of the
9724                  complement.
9725                  True/false will be -1/0 while code below (store flag
9726                  followed by decrement) is 0/-1, so the constants need
9727                  to be exchanged once more.  */
9728
9729               if (compare_code == GE || !cf)
9730                 {
9731                   code = reverse_condition (code);
9732                   compare_code = LT;
9733                 }
9734               else
9735                 {
9736                   HOST_WIDE_INT tmp = cf;
9737                   cf = ct;
9738                   ct = tmp;
9739                 }
9740
9741               out = emit_store_flag (out, code, ix86_compare_op0,
9742                                      ix86_compare_op1, VOIDmode, 0, -1);
9743             }
9744           else
9745             {
9746               out = emit_store_flag (out, code, ix86_compare_op0,
9747                                      ix86_compare_op1, VOIDmode, 0, 1);
9748
9749               out = expand_simple_binop (mode, PLUS, copy_rtx (out), constm1_rtx,
9750                                          copy_rtx (out), 1, OPTAB_DIRECT);
9751             }
9752
9753           out = expand_simple_binop (mode, AND, copy_rtx (out),
9754                                      gen_int_mode (cf - ct, mode),
9755                                      copy_rtx (out), 1, OPTAB_DIRECT);
9756           if (ct)
9757             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
9758                                        copy_rtx (out), 1, OPTAB_DIRECT);
9759           if (!rtx_equal_p (out, operands[0]))
9760             emit_move_insn (operands[0], copy_rtx (out));
9761
9762           return 1; /* DONE */
9763         }
9764     }
9765
9766   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
9767     {
9768       /* Try a few things more with specific constants and a variable.  */
9769
9770       optab op;
9771       rtx var, orig_out, out, tmp;
9772
9773       if (BRANCH_COST <= 2)
9774         return 0; /* FAIL */
9775
9776       /* If one of the two operands is an interesting constant, load a
9777          constant with the above and mask it in with a logical operation.  */
9778
9779       if (GET_CODE (operands[2]) == CONST_INT)
9780         {
9781           var = operands[3];
9782           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
9783             operands[3] = constm1_rtx, op = and_optab;
9784           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
9785             operands[3] = const0_rtx, op = ior_optab;
9786           else
9787             return 0; /* FAIL */
9788         }
9789       else if (GET_CODE (operands[3]) == CONST_INT)
9790         {
9791           var = operands[2];
9792           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
9793             operands[2] = constm1_rtx, op = and_optab;
9794           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
9795             operands[2] = const0_rtx, op = ior_optab;
9796           else
9797             return 0; /* FAIL */
9798         }
9799       else
9800         return 0; /* FAIL */
9801
9802       orig_out = operands[0];
9803       tmp = gen_reg_rtx (mode);
9804       operands[0] = tmp;
9805
9806       /* Recurse to get the constant loaded.  */
9807       if (ix86_expand_int_movcc (operands) == 0)
9808         return 0; /* FAIL */
9809
9810       /* Mask in the interesting variable.  */
9811       out = expand_binop (mode, op, var, tmp, orig_out, 0,
9812                           OPTAB_WIDEN);
9813       if (!rtx_equal_p (out, orig_out))
9814         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
9815
9816       return 1; /* DONE */
9817     }
9818
9819   /*
9820    * For comparison with above,
9821    *
9822    * movl cf,dest
9823    * movl ct,tmp
9824    * cmpl op1,op2
9825    * cmovcc tmp,dest
9826    *
9827    * Size 15.
9828    */
9829
9830   if (! nonimmediate_operand (operands[2], mode))
9831     operands[2] = force_reg (mode, operands[2]);
9832   if (! nonimmediate_operand (operands[3], mode))
9833     operands[3] = force_reg (mode, operands[3]);
9834
9835   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
9836     {
9837       rtx tmp = gen_reg_rtx (mode);
9838       emit_move_insn (tmp, operands[3]);
9839       operands[3] = tmp;
9840     }
9841   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
9842     {
9843       rtx tmp = gen_reg_rtx (mode);
9844       emit_move_insn (tmp, operands[2]);
9845       operands[2] = tmp;
9846     }
9847
9848   if (! register_operand (operands[2], VOIDmode)
9849       && (mode == QImode 
9850           || ! register_operand (operands[3], VOIDmode)))
9851     operands[2] = force_reg (mode, operands[2]);
9852
9853   if (mode == QImode
9854       && ! register_operand (operands[3], VOIDmode))
9855     operands[3] = force_reg (mode, operands[3]);
9856
9857   emit_insn (compare_seq);
9858   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
9859                           gen_rtx_IF_THEN_ELSE (mode,
9860                                                 compare_op, operands[2],
9861                                                 operands[3])));
9862   if (bypass_test)
9863     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
9864                             gen_rtx_IF_THEN_ELSE (mode,
9865                                   bypass_test,
9866                                   copy_rtx (operands[3]),
9867                                   copy_rtx (operands[0]))));
9868   if (second_test)
9869     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
9870                             gen_rtx_IF_THEN_ELSE (mode,
9871                                   second_test,
9872                                   copy_rtx (operands[2]),
9873                                   copy_rtx (operands[0]))));
9874
9875   return 1; /* DONE */
9876 }
9877
9878 int
9879 ix86_expand_fp_movcc (operands)
9880      rtx operands[];
9881 {
9882   enum rtx_code code;
9883   rtx tmp;
9884   rtx compare_op, second_test, bypass_test;
9885
9886   /* For SF/DFmode conditional moves based on comparisons
9887      in same mode, we may want to use SSE min/max instructions.  */
9888   if (((TARGET_SSE_MATH && GET_MODE (operands[0]) == SFmode)
9889        || (TARGET_SSE2 && TARGET_SSE_MATH && GET_MODE (operands[0]) == DFmode))
9890       && GET_MODE (ix86_compare_op0) == GET_MODE (operands[0])
9891       /* The SSE comparisons does not support the LTGT/UNEQ pair.  */
9892       && (!TARGET_IEEE_FP
9893           || (GET_CODE (operands[1]) != LTGT && GET_CODE (operands[1]) != UNEQ))
9894       /* We may be called from the post-reload splitter.  */
9895       && (!REG_P (operands[0])
9896           || SSE_REG_P (operands[0])
9897           || REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER))
9898     {
9899       rtx op0 = ix86_compare_op0, op1 = ix86_compare_op1;
9900       code = GET_CODE (operands[1]);
9901
9902       /* See if we have (cross) match between comparison operands and
9903          conditional move operands.  */
9904       if (rtx_equal_p (operands[2], op1))
9905         {
9906           rtx tmp = op0;
9907           op0 = op1;
9908           op1 = tmp;
9909           code = reverse_condition_maybe_unordered (code);
9910         }
9911       if (rtx_equal_p (operands[2], op0) && rtx_equal_p (operands[3], op1))
9912         {
9913           /* Check for min operation.  */
9914           if (code == LT || code == UNLE)
9915             {
9916                if (code == UNLE)
9917                 {
9918                   rtx tmp = op0;
9919                   op0 = op1;
9920                   op1 = tmp;
9921                 }
9922                operands[0] = force_reg (GET_MODE (operands[0]), operands[0]);
9923                if (memory_operand (op0, VOIDmode))
9924                  op0 = force_reg (GET_MODE (operands[0]), op0);
9925                if (GET_MODE (operands[0]) == SFmode)
9926                  emit_insn (gen_minsf3 (operands[0], op0, op1));
9927                else
9928                  emit_insn (gen_mindf3 (operands[0], op0, op1));
9929                return 1;
9930             }
9931           /* Check for max operation.  */
9932           if (code == GT || code == UNGE)
9933             {
9934                if (code == UNGE)
9935                 {
9936                   rtx tmp = op0;
9937                   op0 = op1;
9938                   op1 = tmp;
9939                 }
9940                operands[0] = force_reg (GET_MODE (operands[0]), operands[0]);
9941                if (memory_operand (op0, VOIDmode))
9942                  op0 = force_reg (GET_MODE (operands[0]), op0);
9943                if (GET_MODE (operands[0]) == SFmode)
9944                  emit_insn (gen_maxsf3 (operands[0], op0, op1));
9945                else
9946                  emit_insn (gen_maxdf3 (operands[0], op0, op1));
9947                return 1;
9948             }
9949         }
9950       /* Manage condition to be sse_comparison_operator.  In case we are
9951          in non-ieee mode, try to canonicalize the destination operand
9952          to be first in the comparison - this helps reload to avoid extra
9953          moves.  */
9954       if (!sse_comparison_operator (operands[1], VOIDmode)
9955           || (rtx_equal_p (operands[0], ix86_compare_op1) && !TARGET_IEEE_FP))
9956         {
9957           rtx tmp = ix86_compare_op0;
9958           ix86_compare_op0 = ix86_compare_op1;
9959           ix86_compare_op1 = tmp;
9960           operands[1] = gen_rtx_fmt_ee (swap_condition (GET_CODE (operands[1])),
9961                                         VOIDmode, ix86_compare_op0,
9962                                         ix86_compare_op1);
9963         }
9964       /* Similarly try to manage result to be first operand of conditional
9965          move. We also don't support the NE comparison on SSE, so try to
9966          avoid it.  */
9967       if ((rtx_equal_p (operands[0], operands[3])
9968            && (!TARGET_IEEE_FP || GET_CODE (operands[1]) != EQ))
9969           || (GET_CODE (operands[1]) == NE && TARGET_IEEE_FP))
9970         {
9971           rtx tmp = operands[2];
9972           operands[2] = operands[3];
9973           operands[3] = tmp;
9974           operands[1] = gen_rtx_fmt_ee (reverse_condition_maybe_unordered
9975                                           (GET_CODE (operands[1])),
9976                                         VOIDmode, ix86_compare_op0,
9977                                         ix86_compare_op1);
9978         }
9979       if (GET_MODE (operands[0]) == SFmode)
9980         emit_insn (gen_sse_movsfcc (operands[0], operands[1],
9981                                     operands[2], operands[3],
9982                                     ix86_compare_op0, ix86_compare_op1));
9983       else
9984         emit_insn (gen_sse_movdfcc (operands[0], operands[1],
9985                                     operands[2], operands[3],
9986                                     ix86_compare_op0, ix86_compare_op1));
9987       return 1;
9988     }
9989
9990   /* The floating point conditional move instructions don't directly
9991      support conditions resulting from a signed integer comparison.  */
9992
9993   code = GET_CODE (operands[1]);
9994   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
9995
9996   /* The floating point conditional move instructions don't directly
9997      support signed integer comparisons.  */
9998
9999   if (!fcmov_comparison_operator (compare_op, VOIDmode))
10000     {
10001       if (second_test != NULL || bypass_test != NULL)
10002         abort ();
10003       tmp = gen_reg_rtx (QImode);
10004       ix86_expand_setcc (code, tmp);
10005       code = NE;
10006       ix86_compare_op0 = tmp;
10007       ix86_compare_op1 = const0_rtx;
10008       compare_op = ix86_expand_compare (code,  &second_test, &bypass_test);
10009     }
10010   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
10011     {
10012       tmp = gen_reg_rtx (GET_MODE (operands[0]));
10013       emit_move_insn (tmp, operands[3]);
10014       operands[3] = tmp;
10015     }
10016   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
10017     {
10018       tmp = gen_reg_rtx (GET_MODE (operands[0]));
10019       emit_move_insn (tmp, operands[2]);
10020       operands[2] = tmp;
10021     }
10022
10023   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10024                           gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
10025                                 compare_op,
10026                                 operands[2],
10027                                 operands[3])));
10028   if (bypass_test)
10029     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10030                             gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
10031                                   bypass_test,
10032                                   operands[3],
10033                                   operands[0])));
10034   if (second_test)
10035     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10036                             gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
10037                                   second_test,
10038                                   operands[2],
10039                                   operands[0])));
10040
10041   return 1;
10042 }
10043
10044 /* Expand conditional increment or decrement using adb/sbb instructions.
10045    The default case using setcc followed by the conditional move can be
10046    done by generic code.  */
10047 int
10048 ix86_expand_int_addcc (operands)
10049      rtx operands[];
10050 {
10051   enum rtx_code code = GET_CODE (operands[1]);
10052   rtx compare_op;
10053   rtx val = const0_rtx;
10054
10055   if (operands[3] != const1_rtx
10056       && operands[3] != constm1_rtx)
10057     return 0;
10058   if (!ix86_expand_carry_flag_compare (code, ix86_compare_op0,
10059                                        ix86_compare_op1, &compare_op))
10060      return 0;
10061   if (GET_CODE (compare_op) != LTU)
10062     val = constm1_rtx;
10063   if ((GET_CODE (compare_op) == LTU) == (operands[3] == constm1_rtx))
10064     {
10065       switch (GET_MODE (operands[0]))
10066         {
10067           case QImode:
10068             emit_insn (gen_subqi3_carry (operands[0], operands[2], val));
10069             break;
10070           case HImode:
10071             emit_insn (gen_subhi3_carry (operands[0], operands[2], val));
10072             break;
10073           case SImode:
10074             emit_insn (gen_subsi3_carry (operands[0], operands[2], val));
10075             break;
10076           case DImode:
10077             emit_insn (gen_subdi3_carry_rex64 (operands[0], operands[2], val));
10078             break;
10079           default:
10080             abort ();
10081         }
10082     }
10083   else
10084     {
10085       switch (GET_MODE (operands[0]))
10086         {
10087           case QImode:
10088             emit_insn (gen_addqi3_carry (operands[0], operands[2], val));
10089             break;
10090           case HImode:
10091             emit_insn (gen_addhi3_carry (operands[0], operands[2], val));
10092             break;
10093           case SImode:
10094             emit_insn (gen_addsi3_carry (operands[0], operands[2], val));
10095             break;
10096           case DImode:
10097             emit_insn (gen_adddi3_carry_rex64 (operands[0], operands[2], val));
10098             break;
10099           default:
10100             abort ();
10101         }
10102     }
10103   return 1; /* DONE */
10104 }
10105
10106
10107 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
10108    works for floating pointer parameters and nonoffsetable memories.
10109    For pushes, it returns just stack offsets; the values will be saved
10110    in the right order.  Maximally three parts are generated.  */
10111
10112 static int
10113 ix86_split_to_parts (operand, parts, mode)
10114      rtx operand;
10115      rtx *parts;
10116      enum machine_mode mode;
10117 {
10118   int size;
10119
10120   if (!TARGET_64BIT)
10121     size = mode == TFmode ? 3 : (GET_MODE_SIZE (mode) / 4);
10122   else
10123     size = (GET_MODE_SIZE (mode) + 4) / 8;
10124
10125   if (GET_CODE (operand) == REG && MMX_REGNO_P (REGNO (operand)))
10126     abort ();
10127   if (size < 2 || size > 3)
10128     abort ();
10129
10130   /* Optimize constant pool reference to immediates.  This is used by fp
10131      moves, that force all constants to memory to allow combining.  */
10132   if (GET_CODE (operand) == MEM && RTX_UNCHANGING_P (operand))
10133     {
10134       rtx tmp = maybe_get_pool_constant (operand);
10135       if (tmp)
10136         operand = tmp;
10137     }
10138
10139   if (GET_CODE (operand) == MEM && !offsettable_memref_p (operand))
10140     {
10141       /* The only non-offsetable memories we handle are pushes.  */
10142       if (! push_operand (operand, VOIDmode))
10143         abort ();
10144
10145       operand = copy_rtx (operand);
10146       PUT_MODE (operand, Pmode);
10147       parts[0] = parts[1] = parts[2] = operand;
10148     }
10149   else if (!TARGET_64BIT)
10150     {
10151       if (mode == DImode)
10152         split_di (&operand, 1, &parts[0], &parts[1]);
10153       else
10154         {
10155           if (REG_P (operand))
10156             {
10157               if (!reload_completed)
10158                 abort ();
10159               parts[0] = gen_rtx_REG (SImode, REGNO (operand) + 0);
10160               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
10161               if (size == 3)
10162                 parts[2] = gen_rtx_REG (SImode, REGNO (operand) + 2);
10163             }
10164           else if (offsettable_memref_p (operand))
10165             {
10166               operand = adjust_address (operand, SImode, 0);
10167               parts[0] = operand;
10168               parts[1] = adjust_address (operand, SImode, 4);
10169               if (size == 3)
10170                 parts[2] = adjust_address (operand, SImode, 8);
10171             }
10172           else if (GET_CODE (operand) == CONST_DOUBLE)
10173             {
10174               REAL_VALUE_TYPE r;
10175               long l[4];
10176
10177               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
10178               switch (mode)
10179                 {
10180                 case XFmode:
10181                 case TFmode:
10182                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
10183                   parts[2] = gen_int_mode (l[2], SImode);
10184                   break;
10185                 case DFmode:
10186                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
10187                   break;
10188                 default:
10189                   abort ();
10190                 }
10191               parts[1] = gen_int_mode (l[1], SImode);
10192               parts[0] = gen_int_mode (l[0], SImode);
10193             }
10194           else
10195             abort ();
10196         }
10197     }
10198   else
10199     {
10200       if (mode == TImode)
10201         split_ti (&operand, 1, &parts[0], &parts[1]);
10202       if (mode == XFmode || mode == TFmode)
10203         {
10204           if (REG_P (operand))
10205             {
10206               if (!reload_completed)
10207                 abort ();
10208               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
10209               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
10210             }
10211           else if (offsettable_memref_p (operand))
10212             {
10213               operand = adjust_address (operand, DImode, 0);
10214               parts[0] = operand;
10215               parts[1] = adjust_address (operand, SImode, 8);
10216             }
10217           else if (GET_CODE (operand) == CONST_DOUBLE)
10218             {
10219               REAL_VALUE_TYPE r;
10220               long l[3];
10221
10222               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
10223               REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
10224               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
10225               if (HOST_BITS_PER_WIDE_INT >= 64)
10226                 parts[0]
10227                   = gen_int_mode
10228                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
10229                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
10230                        DImode);
10231               else
10232                 parts[0] = immed_double_const (l[0], l[1], DImode);
10233               parts[1] = gen_int_mode (l[2], SImode);
10234             }
10235           else
10236             abort ();
10237         }
10238     }
10239
10240   return size;
10241 }
10242
10243 /* Emit insns to perform a move or push of DI, DF, and XF values.
10244    Return false when normal moves are needed; true when all required
10245    insns have been emitted.  Operands 2-4 contain the input values
10246    int the correct order; operands 5-7 contain the output values.  */
10247
10248 void
10249 ix86_split_long_move (operands)
10250      rtx operands[];
10251 {
10252   rtx part[2][3];
10253   int nparts;
10254   int push = 0;
10255   int collisions = 0;
10256   enum machine_mode mode = GET_MODE (operands[0]);
10257
10258   /* The DFmode expanders may ask us to move double.
10259      For 64bit target this is single move.  By hiding the fact
10260      here we simplify i386.md splitters.  */
10261   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
10262     {
10263       /* Optimize constant pool reference to immediates.  This is used by
10264          fp moves, that force all constants to memory to allow combining.  */
10265
10266       if (GET_CODE (operands[1]) == MEM
10267           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
10268           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
10269         operands[1] = get_pool_constant (XEXP (operands[1], 0));
10270       if (push_operand (operands[0], VOIDmode))
10271         {
10272           operands[0] = copy_rtx (operands[0]);
10273           PUT_MODE (operands[0], Pmode);
10274         }
10275       else
10276         operands[0] = gen_lowpart (DImode, operands[0]);
10277       operands[1] = gen_lowpart (DImode, operands[1]);
10278       emit_move_insn (operands[0], operands[1]);
10279       return;
10280     }
10281
10282   /* The only non-offsettable memory we handle is push.  */
10283   if (push_operand (operands[0], VOIDmode))
10284     push = 1;
10285   else if (GET_CODE (operands[0]) == MEM
10286            && ! offsettable_memref_p (operands[0]))
10287     abort ();
10288
10289   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
10290   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
10291
10292   /* When emitting push, take care for source operands on the stack.  */
10293   if (push && GET_CODE (operands[1]) == MEM
10294       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
10295     {
10296       if (nparts == 3)
10297         part[1][1] = change_address (part[1][1], GET_MODE (part[1][1]),
10298                                      XEXP (part[1][2], 0));
10299       part[1][0] = change_address (part[1][0], GET_MODE (part[1][0]),
10300                                    XEXP (part[1][1], 0));
10301     }
10302
10303   /* We need to do copy in the right order in case an address register
10304      of the source overlaps the destination.  */
10305   if (REG_P (part[0][0]) && GET_CODE (part[1][0]) == MEM)
10306     {
10307       if (reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0)))
10308         collisions++;
10309       if (reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
10310         collisions++;
10311       if (nparts == 3
10312           && reg_overlap_mentioned_p (part[0][2], XEXP (part[1][0], 0)))
10313         collisions++;
10314
10315       /* Collision in the middle part can be handled by reordering.  */
10316       if (collisions == 1 && nparts == 3
10317           && reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
10318         {
10319           rtx tmp;
10320           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
10321           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
10322         }
10323
10324       /* If there are more collisions, we can't handle it by reordering.
10325          Do an lea to the last part and use only one colliding move.  */
10326       else if (collisions > 1)
10327         {
10328           collisions = 1;
10329           emit_insn (gen_rtx_SET (VOIDmode, part[0][nparts - 1],
10330                                   XEXP (part[1][0], 0)));
10331           part[1][0] = change_address (part[1][0],
10332                                        TARGET_64BIT ? DImode : SImode,
10333                                        part[0][nparts - 1]);
10334           part[1][1] = adjust_address (part[1][0], VOIDmode, UNITS_PER_WORD);
10335           if (nparts == 3)
10336             part[1][2] = adjust_address (part[1][0], VOIDmode, 8);
10337         }
10338     }
10339
10340   if (push)
10341     {
10342       if (!TARGET_64BIT)
10343         {
10344           if (nparts == 3)
10345             {
10346               /* We use only first 12 bytes of TFmode value, but for pushing we
10347                  are required to adjust stack as if we were pushing real 16byte
10348                  value.  */
10349               if (mode == TFmode && !TARGET_64BIT)
10350                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10351                                        GEN_INT (-4)));
10352               emit_move_insn (part[0][2], part[1][2]);
10353             }
10354         }
10355       else
10356         {
10357           /* In 64bit mode we don't have 32bit push available.  In case this is
10358              register, it is OK - we will just use larger counterpart.  We also
10359              retype memory - these comes from attempt to avoid REX prefix on
10360              moving of second half of TFmode value.  */
10361           if (GET_MODE (part[1][1]) == SImode)
10362             {
10363               if (GET_CODE (part[1][1]) == MEM)
10364                 part[1][1] = adjust_address (part[1][1], DImode, 0);
10365               else if (REG_P (part[1][1]))
10366                 part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
10367               else
10368                 abort ();
10369               if (GET_MODE (part[1][0]) == SImode)
10370                 part[1][0] = part[1][1];
10371             }
10372         }
10373       emit_move_insn (part[0][1], part[1][1]);
10374       emit_move_insn (part[0][0], part[1][0]);
10375       return;
10376     }
10377
10378   /* Choose correct order to not overwrite the source before it is copied.  */
10379   if ((REG_P (part[0][0])
10380        && REG_P (part[1][1])
10381        && (REGNO (part[0][0]) == REGNO (part[1][1])
10382            || (nparts == 3
10383                && REGNO (part[0][0]) == REGNO (part[1][2]))))
10384       || (collisions > 0
10385           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
10386     {
10387       if (nparts == 3)
10388         {
10389           operands[2] = part[0][2];
10390           operands[3] = part[0][1];
10391           operands[4] = part[0][0];
10392           operands[5] = part[1][2];
10393           operands[6] = part[1][1];
10394           operands[7] = part[1][0];
10395         }
10396       else
10397         {
10398           operands[2] = part[0][1];
10399           operands[3] = part[0][0];
10400           operands[5] = part[1][1];
10401           operands[6] = part[1][0];
10402         }
10403     }
10404   else
10405     {
10406       if (nparts == 3)
10407         {
10408           operands[2] = part[0][0];
10409           operands[3] = part[0][1];
10410           operands[4] = part[0][2];
10411           operands[5] = part[1][0];
10412           operands[6] = part[1][1];
10413           operands[7] = part[1][2];
10414         }
10415       else
10416         {
10417           operands[2] = part[0][0];
10418           operands[3] = part[0][1];
10419           operands[5] = part[1][0];
10420           operands[6] = part[1][1];
10421         }
10422     }
10423   emit_move_insn (operands[2], operands[5]);
10424   emit_move_insn (operands[3], operands[6]);
10425   if (nparts == 3)
10426     emit_move_insn (operands[4], operands[7]);
10427
10428   return;
10429 }
10430
10431 void
10432 ix86_split_ashldi (operands, scratch)
10433      rtx *operands, scratch;
10434 {
10435   rtx low[2], high[2];
10436   int count;
10437
10438   if (GET_CODE (operands[2]) == CONST_INT)
10439     {
10440       split_di (operands, 2, low, high);
10441       count = INTVAL (operands[2]) & 63;
10442
10443       if (count >= 32)
10444         {
10445           emit_move_insn (high[0], low[1]);
10446           emit_move_insn (low[0], const0_rtx);
10447
10448           if (count > 32)
10449             emit_insn (gen_ashlsi3 (high[0], high[0], GEN_INT (count - 32)));
10450         }
10451       else
10452         {
10453           if (!rtx_equal_p (operands[0], operands[1]))
10454             emit_move_insn (operands[0], operands[1]);
10455           emit_insn (gen_x86_shld_1 (high[0], low[0], GEN_INT (count)));
10456           emit_insn (gen_ashlsi3 (low[0], low[0], GEN_INT (count)));
10457         }
10458     }
10459   else
10460     {
10461       if (!rtx_equal_p (operands[0], operands[1]))
10462         emit_move_insn (operands[0], operands[1]);
10463
10464       split_di (operands, 1, low, high);
10465
10466       emit_insn (gen_x86_shld_1 (high[0], low[0], operands[2]));
10467       emit_insn (gen_ashlsi3 (low[0], low[0], operands[2]));
10468
10469       if (TARGET_CMOVE && (! no_new_pseudos || scratch))
10470         {
10471           if (! no_new_pseudos)
10472             scratch = force_reg (SImode, const0_rtx);
10473           else
10474             emit_move_insn (scratch, const0_rtx);
10475
10476           emit_insn (gen_x86_shift_adj_1 (high[0], low[0], operands[2],
10477                                           scratch));
10478         }
10479       else
10480         emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
10481     }
10482 }
10483
10484 void
10485 ix86_split_ashrdi (operands, scratch)
10486      rtx *operands, scratch;
10487 {
10488   rtx low[2], high[2];
10489   int count;
10490
10491   if (GET_CODE (operands[2]) == CONST_INT)
10492     {
10493       split_di (operands, 2, low, high);
10494       count = INTVAL (operands[2]) & 63;
10495
10496       if (count >= 32)
10497         {
10498           emit_move_insn (low[0], high[1]);
10499
10500           if (! reload_completed)
10501             emit_insn (gen_ashrsi3 (high[0], low[0], GEN_INT (31)));
10502           else
10503             {
10504               emit_move_insn (high[0], low[0]);
10505               emit_insn (gen_ashrsi3 (high[0], high[0], GEN_INT (31)));
10506             }
10507
10508           if (count > 32)
10509             emit_insn (gen_ashrsi3 (low[0], low[0], GEN_INT (count - 32)));
10510         }
10511       else
10512         {
10513           if (!rtx_equal_p (operands[0], operands[1]))
10514             emit_move_insn (operands[0], operands[1]);
10515           emit_insn (gen_x86_shrd_1 (low[0], high[0], GEN_INT (count)));
10516           emit_insn (gen_ashrsi3 (high[0], high[0], GEN_INT (count)));
10517         }
10518     }
10519   else
10520     {
10521       if (!rtx_equal_p (operands[0], operands[1]))
10522         emit_move_insn (operands[0], operands[1]);
10523
10524       split_di (operands, 1, low, high);
10525
10526       emit_insn (gen_x86_shrd_1 (low[0], high[0], operands[2]));
10527       emit_insn (gen_ashrsi3 (high[0], high[0], operands[2]));
10528
10529       if (TARGET_CMOVE && (! no_new_pseudos || scratch))
10530         {
10531           if (! no_new_pseudos)
10532             scratch = gen_reg_rtx (SImode);
10533           emit_move_insn (scratch, high[0]);
10534           emit_insn (gen_ashrsi3 (scratch, scratch, GEN_INT (31)));
10535           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
10536                                           scratch));
10537         }
10538       else
10539         emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
10540     }
10541 }
10542
10543 void
10544 ix86_split_lshrdi (operands, scratch)
10545      rtx *operands, scratch;
10546 {
10547   rtx low[2], high[2];
10548   int count;
10549
10550   if (GET_CODE (operands[2]) == CONST_INT)
10551     {
10552       split_di (operands, 2, low, high);
10553       count = INTVAL (operands[2]) & 63;
10554
10555       if (count >= 32)
10556         {
10557           emit_move_insn (low[0], high[1]);
10558           emit_move_insn (high[0], const0_rtx);
10559
10560           if (count > 32)
10561             emit_insn (gen_lshrsi3 (low[0], low[0], GEN_INT (count - 32)));
10562         }
10563       else
10564         {
10565           if (!rtx_equal_p (operands[0], operands[1]))
10566             emit_move_insn (operands[0], operands[1]);
10567           emit_insn (gen_x86_shrd_1 (low[0], high[0], GEN_INT (count)));
10568           emit_insn (gen_lshrsi3 (high[0], high[0], GEN_INT (count)));
10569         }
10570     }
10571   else
10572     {
10573       if (!rtx_equal_p (operands[0], operands[1]))
10574         emit_move_insn (operands[0], operands[1]);
10575
10576       split_di (operands, 1, low, high);
10577
10578       emit_insn (gen_x86_shrd_1 (low[0], high[0], operands[2]));
10579       emit_insn (gen_lshrsi3 (high[0], high[0], operands[2]));
10580
10581       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
10582       if (TARGET_CMOVE && (! no_new_pseudos || scratch))
10583         {
10584           if (! no_new_pseudos)
10585             scratch = force_reg (SImode, const0_rtx);
10586           else
10587             emit_move_insn (scratch, const0_rtx);
10588
10589           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
10590                                           scratch));
10591         }
10592       else
10593         emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
10594     }
10595 }
10596
10597 /* Helper function for the string operations below.  Dest VARIABLE whether
10598    it is aligned to VALUE bytes.  If true, jump to the label.  */
10599 static rtx
10600 ix86_expand_aligntest (variable, value)
10601      rtx variable;
10602      int value;
10603 {
10604   rtx label = gen_label_rtx ();
10605   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
10606   if (GET_MODE (variable) == DImode)
10607     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
10608   else
10609     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
10610   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
10611                            1, label);
10612   return label;
10613 }
10614
10615 /* Adjust COUNTER by the VALUE.  */
10616 static void
10617 ix86_adjust_counter (countreg, value)
10618      rtx countreg;
10619      HOST_WIDE_INT value;
10620 {
10621   if (GET_MODE (countreg) == DImode)
10622     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
10623   else
10624     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
10625 }
10626
10627 /* Zero extend possibly SImode EXP to Pmode register.  */
10628 rtx
10629 ix86_zero_extend_to_Pmode (exp)
10630    rtx exp;
10631 {
10632   rtx r;
10633   if (GET_MODE (exp) == VOIDmode)
10634     return force_reg (Pmode, exp);
10635   if (GET_MODE (exp) == Pmode)
10636     return copy_to_mode_reg (Pmode, exp);
10637   r = gen_reg_rtx (Pmode);
10638   emit_insn (gen_zero_extendsidi2 (r, exp));
10639   return r;
10640 }
10641
10642 /* Expand string move (memcpy) operation.  Use i386 string operations when
10643    profitable.  expand_clrstr contains similar code.  */
10644 int
10645 ix86_expand_movstr (dst, src, count_exp, align_exp)
10646      rtx dst, src, count_exp, align_exp;
10647 {
10648   rtx srcreg, destreg, countreg;
10649   enum machine_mode counter_mode;
10650   HOST_WIDE_INT align = 0;
10651   unsigned HOST_WIDE_INT count = 0;
10652   rtx insns;
10653
10654
10655   if (GET_CODE (align_exp) == CONST_INT)
10656     align = INTVAL (align_exp);
10657
10658   /* This simple hack avoids all inlining code and simplifies code below.  */
10659   if (!TARGET_ALIGN_STRINGOPS)
10660     align = 64;
10661
10662   if (GET_CODE (count_exp) == CONST_INT)
10663     {
10664       count = INTVAL (count_exp);
10665       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
10666         return 0;
10667     }
10668
10669   /* Figure out proper mode for counter.  For 32bits it is always SImode,
10670      for 64bits use SImode when possible, otherwise DImode.
10671      Set count to number of bytes copied when known at compile time.  */
10672   if (!TARGET_64BIT || GET_MODE (count_exp) == SImode
10673       || x86_64_zero_extended_value (count_exp))
10674     counter_mode = SImode;
10675   else
10676     counter_mode = DImode;
10677
10678   start_sequence ();
10679
10680   if (counter_mode != SImode && counter_mode != DImode)
10681     abort ();
10682
10683   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
10684   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
10685
10686   emit_insn (gen_cld ());
10687
10688   /* When optimizing for size emit simple rep ; movsb instruction for
10689      counts not divisible by 4.  */
10690
10691   if ((!optimize || optimize_size) && (count == 0 || (count & 0x03)))
10692     {
10693       countreg = ix86_zero_extend_to_Pmode (count_exp);
10694       if (TARGET_64BIT)
10695         emit_insn (gen_rep_movqi_rex64 (destreg, srcreg, countreg,
10696                                         destreg, srcreg, countreg));
10697       else
10698         emit_insn (gen_rep_movqi (destreg, srcreg, countreg,
10699                                   destreg, srcreg, countreg));
10700     }
10701
10702   /* For constant aligned (or small unaligned) copies use rep movsl
10703      followed by code copying the rest.  For PentiumPro ensure 8 byte
10704      alignment to allow rep movsl acceleration.  */
10705
10706   else if (count != 0
10707            && (align >= 8
10708                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
10709                || optimize_size || count < (unsigned int) 64))
10710     {
10711       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
10712       if (count & ~(size - 1))
10713         {
10714           countreg = copy_to_mode_reg (counter_mode,
10715                                        GEN_INT ((count >> (size == 4 ? 2 : 3))
10716                                                 & (TARGET_64BIT ? -1 : 0x3fffffff)));
10717           countreg = ix86_zero_extend_to_Pmode (countreg);
10718           if (size == 4)
10719             {
10720               if (TARGET_64BIT)
10721                 emit_insn (gen_rep_movsi_rex64 (destreg, srcreg, countreg,
10722                                                 destreg, srcreg, countreg));
10723               else
10724                 emit_insn (gen_rep_movsi (destreg, srcreg, countreg,
10725                                           destreg, srcreg, countreg));
10726             }
10727           else
10728             emit_insn (gen_rep_movdi_rex64 (destreg, srcreg, countreg,
10729                                             destreg, srcreg, countreg));
10730         }
10731       if (size == 8 && (count & 0x04))
10732         emit_insn (gen_strmovsi (destreg, srcreg));
10733       if (count & 0x02)
10734         emit_insn (gen_strmovhi (destreg, srcreg));
10735       if (count & 0x01)
10736         emit_insn (gen_strmovqi (destreg, srcreg));
10737     }
10738   /* The generic code based on the glibc implementation:
10739      - align destination to 4 bytes (8 byte alignment is used for PentiumPro
10740      allowing accelerated copying there)
10741      - copy the data using rep movsl
10742      - copy the rest.  */
10743   else
10744     {
10745       rtx countreg2;
10746       rtx label = NULL;
10747       int desired_alignment = (TARGET_PENTIUMPRO
10748                                && (count == 0 || count >= (unsigned int) 260)
10749                                ? 8 : UNITS_PER_WORD);
10750
10751       /* In case we don't know anything about the alignment, default to
10752          library version, since it is usually equally fast and result in
10753          shorter code. 
10754
10755          Also emit call when we know that the count is large and call overhead
10756          will not be important.  */
10757       if (!TARGET_INLINE_ALL_STRINGOPS
10758           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
10759         {
10760           end_sequence ();
10761           return 0;
10762         }
10763
10764       if (TARGET_SINGLE_STRINGOP)
10765         emit_insn (gen_cld ());
10766
10767       countreg2 = gen_reg_rtx (Pmode);
10768       countreg = copy_to_mode_reg (counter_mode, count_exp);
10769
10770       /* We don't use loops to align destination and to copy parts smaller
10771          than 4 bytes, because gcc is able to optimize such code better (in
10772          the case the destination or the count really is aligned, gcc is often
10773          able to predict the branches) and also it is friendlier to the
10774          hardware branch prediction.
10775
10776          Using loops is beneficial for generic case, because we can
10777          handle small counts using the loops.  Many CPUs (such as Athlon)
10778          have large REP prefix setup costs.
10779
10780          This is quite costly.  Maybe we can revisit this decision later or
10781          add some customizability to this code.  */
10782
10783       if (count == 0 && align < desired_alignment)
10784         {
10785           label = gen_label_rtx ();
10786           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
10787                                    LEU, 0, counter_mode, 1, label);
10788         }
10789       if (align <= 1)
10790         {
10791           rtx label = ix86_expand_aligntest (destreg, 1);
10792           emit_insn (gen_strmovqi (destreg, srcreg));
10793           ix86_adjust_counter (countreg, 1);
10794           emit_label (label);
10795           LABEL_NUSES (label) = 1;
10796         }
10797       if (align <= 2)
10798         {
10799           rtx label = ix86_expand_aligntest (destreg, 2);
10800           emit_insn (gen_strmovhi (destreg, srcreg));
10801           ix86_adjust_counter (countreg, 2);
10802           emit_label (label);
10803           LABEL_NUSES (label) = 1;
10804         }
10805       if (align <= 4 && desired_alignment > 4)
10806         {
10807           rtx label = ix86_expand_aligntest (destreg, 4);
10808           emit_insn (gen_strmovsi (destreg, srcreg));
10809           ix86_adjust_counter (countreg, 4);
10810           emit_label (label);
10811           LABEL_NUSES (label) = 1;
10812         }
10813
10814       if (label && desired_alignment > 4 && !TARGET_64BIT)
10815         {
10816           emit_label (label);
10817           LABEL_NUSES (label) = 1;
10818           label = NULL_RTX;
10819         }
10820       if (!TARGET_SINGLE_STRINGOP)
10821         emit_insn (gen_cld ());
10822       if (TARGET_64BIT)
10823         {
10824           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
10825                                   GEN_INT (3)));
10826           emit_insn (gen_rep_movdi_rex64 (destreg, srcreg, countreg2,
10827                                           destreg, srcreg, countreg2));
10828         }
10829       else
10830         {
10831           emit_insn (gen_lshrsi3 (countreg2, countreg, GEN_INT (2)));
10832           emit_insn (gen_rep_movsi (destreg, srcreg, countreg2,
10833                                     destreg, srcreg, countreg2));
10834         }
10835
10836       if (label)
10837         {
10838           emit_label (label);
10839           LABEL_NUSES (label) = 1;
10840         }
10841       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
10842         emit_insn (gen_strmovsi (destreg, srcreg));
10843       if ((align <= 4 || count == 0) && TARGET_64BIT)
10844         {
10845           rtx label = ix86_expand_aligntest (countreg, 4);
10846           emit_insn (gen_strmovsi (destreg, srcreg));
10847           emit_label (label);
10848           LABEL_NUSES (label) = 1;
10849         }
10850       if (align > 2 && count != 0 && (count & 2))
10851         emit_insn (gen_strmovhi (destreg, srcreg));
10852       if (align <= 2 || count == 0)
10853         {
10854           rtx label = ix86_expand_aligntest (countreg, 2);
10855           emit_insn (gen_strmovhi (destreg, srcreg));
10856           emit_label (label);
10857           LABEL_NUSES (label) = 1;
10858         }
10859       if (align > 1 && count != 0 && (count & 1))
10860         emit_insn (gen_strmovqi (destreg, srcreg));
10861       if (align <= 1 || count == 0)
10862         {
10863           rtx label = ix86_expand_aligntest (countreg, 1);
10864           emit_insn (gen_strmovqi (destreg, srcreg));
10865           emit_label (label);
10866           LABEL_NUSES (label) = 1;
10867         }
10868     }
10869
10870   insns = get_insns ();
10871   end_sequence ();
10872
10873   ix86_set_move_mem_attrs (insns, dst, src, destreg, srcreg);
10874   emit_insn (insns);
10875   return 1;
10876 }
10877
10878 /* Expand string clear operation (bzero).  Use i386 string operations when
10879    profitable.  expand_movstr contains similar code.  */
10880 int
10881 ix86_expand_clrstr (src, count_exp, align_exp)
10882      rtx src, count_exp, align_exp;
10883 {
10884   rtx destreg, zeroreg, countreg;
10885   enum machine_mode counter_mode;
10886   HOST_WIDE_INT align = 0;
10887   unsigned HOST_WIDE_INT count = 0;
10888
10889   if (GET_CODE (align_exp) == CONST_INT)
10890     align = INTVAL (align_exp);
10891
10892   /* This simple hack avoids all inlining code and simplifies code below.  */
10893   if (!TARGET_ALIGN_STRINGOPS)
10894     align = 32;
10895
10896   if (GET_CODE (count_exp) == CONST_INT)
10897     {
10898       count = INTVAL (count_exp);
10899       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
10900         return 0;
10901     }
10902   /* Figure out proper mode for counter.  For 32bits it is always SImode,
10903      for 64bits use SImode when possible, otherwise DImode.
10904      Set count to number of bytes copied when known at compile time.  */
10905   if (!TARGET_64BIT || GET_MODE (count_exp) == SImode
10906       || x86_64_zero_extended_value (count_exp))
10907     counter_mode = SImode;
10908   else
10909     counter_mode = DImode;
10910
10911   destreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
10912
10913   emit_insn (gen_cld ());
10914
10915   /* When optimizing for size emit simple rep ; movsb instruction for
10916      counts not divisible by 4.  */
10917
10918   if ((!optimize || optimize_size) && (count == 0 || (count & 0x03)))
10919     {
10920       countreg = ix86_zero_extend_to_Pmode (count_exp);
10921       zeroreg = copy_to_mode_reg (QImode, const0_rtx);
10922       if (TARGET_64BIT)
10923         emit_insn (gen_rep_stosqi_rex64 (destreg, countreg, zeroreg,
10924                                          destreg, countreg));
10925       else
10926         emit_insn (gen_rep_stosqi (destreg, countreg, zeroreg,
10927                                    destreg, countreg));
10928     }
10929   else if (count != 0
10930            && (align >= 8
10931                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
10932                || optimize_size || count < (unsigned int) 64))
10933     {
10934       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
10935       zeroreg = copy_to_mode_reg (size == 4 ? SImode : DImode, const0_rtx);
10936       if (count & ~(size - 1))
10937         {
10938           countreg = copy_to_mode_reg (counter_mode,
10939                                        GEN_INT ((count >> (size == 4 ? 2 : 3))
10940                                                 & (TARGET_64BIT ? -1 : 0x3fffffff)));
10941           countreg = ix86_zero_extend_to_Pmode (countreg);
10942           if (size == 4)
10943             {
10944               if (TARGET_64BIT)
10945                 emit_insn (gen_rep_stossi_rex64 (destreg, countreg, zeroreg,
10946                                                  destreg, countreg));
10947               else
10948                 emit_insn (gen_rep_stossi (destreg, countreg, zeroreg,
10949                                            destreg, countreg));
10950             }
10951           else
10952             emit_insn (gen_rep_stosdi_rex64 (destreg, countreg, zeroreg,
10953                                              destreg, countreg));
10954         }
10955       if (size == 8 && (count & 0x04))
10956         emit_insn (gen_strsetsi (destreg,
10957                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
10958       if (count & 0x02)
10959         emit_insn (gen_strsethi (destreg,
10960                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
10961       if (count & 0x01)
10962         emit_insn (gen_strsetqi (destreg,
10963                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
10964     }
10965   else
10966     {
10967       rtx countreg2;
10968       rtx label = NULL;
10969       /* Compute desired alignment of the string operation.  */
10970       int desired_alignment = (TARGET_PENTIUMPRO
10971                                && (count == 0 || count >= (unsigned int) 260)
10972                                ? 8 : UNITS_PER_WORD);
10973
10974       /* In case we don't know anything about the alignment, default to
10975          library version, since it is usually equally fast and result in
10976          shorter code.
10977
10978          Also emit call when we know that the count is large and call overhead
10979          will not be important.  */
10980       if (!TARGET_INLINE_ALL_STRINGOPS
10981           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
10982         return 0;
10983
10984       if (TARGET_SINGLE_STRINGOP)
10985         emit_insn (gen_cld ());
10986
10987       countreg2 = gen_reg_rtx (Pmode);
10988       countreg = copy_to_mode_reg (counter_mode, count_exp);
10989       zeroreg = copy_to_mode_reg (Pmode, const0_rtx);
10990
10991       if (count == 0 && align < desired_alignment)
10992         {
10993           label = gen_label_rtx ();
10994           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
10995                                    LEU, 0, counter_mode, 1, label);
10996         }
10997       if (align <= 1)
10998         {
10999           rtx label = ix86_expand_aligntest (destreg, 1);
11000           emit_insn (gen_strsetqi (destreg,
11001                                    gen_rtx_SUBREG (QImode, zeroreg, 0)));
11002           ix86_adjust_counter (countreg, 1);
11003           emit_label (label);
11004           LABEL_NUSES (label) = 1;
11005         }
11006       if (align <= 2)
11007         {
11008           rtx label = ix86_expand_aligntest (destreg, 2);
11009           emit_insn (gen_strsethi (destreg,
11010                                    gen_rtx_SUBREG (HImode, zeroreg, 0)));
11011           ix86_adjust_counter (countreg, 2);
11012           emit_label (label);
11013           LABEL_NUSES (label) = 1;
11014         }
11015       if (align <= 4 && desired_alignment > 4)
11016         {
11017           rtx label = ix86_expand_aligntest (destreg, 4);
11018           emit_insn (gen_strsetsi (destreg, (TARGET_64BIT
11019                                              ? gen_rtx_SUBREG (SImode, zeroreg, 0)
11020                                              : zeroreg)));
11021           ix86_adjust_counter (countreg, 4);
11022           emit_label (label);
11023           LABEL_NUSES (label) = 1;
11024         }
11025
11026       if (label && desired_alignment > 4 && !TARGET_64BIT)
11027         {
11028           emit_label (label);
11029           LABEL_NUSES (label) = 1;
11030           label = NULL_RTX;
11031         }
11032
11033       if (!TARGET_SINGLE_STRINGOP)
11034         emit_insn (gen_cld ());
11035       if (TARGET_64BIT)
11036         {
11037           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
11038                                   GEN_INT (3)));
11039           emit_insn (gen_rep_stosdi_rex64 (destreg, countreg2, zeroreg,
11040                                            destreg, countreg2));
11041         }
11042       else
11043         {
11044           emit_insn (gen_lshrsi3 (countreg2, countreg, GEN_INT (2)));
11045           emit_insn (gen_rep_stossi (destreg, countreg2, zeroreg,
11046                                      destreg, countreg2));
11047         }
11048       if (label)
11049         {
11050           emit_label (label);
11051           LABEL_NUSES (label) = 1;
11052         }
11053
11054       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
11055         emit_insn (gen_strsetsi (destreg,
11056                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
11057       if (TARGET_64BIT && (align <= 4 || count == 0))
11058         {
11059           rtx label = ix86_expand_aligntest (countreg, 4);
11060           emit_insn (gen_strsetsi (destreg,
11061                                    gen_rtx_SUBREG (SImode, zeroreg, 0)));
11062           emit_label (label);
11063           LABEL_NUSES (label) = 1;
11064         }
11065       if (align > 2 && count != 0 && (count & 2))
11066         emit_insn (gen_strsethi (destreg,
11067                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
11068       if (align <= 2 || count == 0)
11069         {
11070           rtx label = ix86_expand_aligntest (countreg, 2);
11071           emit_insn (gen_strsethi (destreg,
11072                                    gen_rtx_SUBREG (HImode, zeroreg, 0)));
11073           emit_label (label);
11074           LABEL_NUSES (label) = 1;
11075         }
11076       if (align > 1 && count != 0 && (count & 1))
11077         emit_insn (gen_strsetqi (destreg,
11078                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
11079       if (align <= 1 || count == 0)
11080         {
11081           rtx label = ix86_expand_aligntest (countreg, 1);
11082           emit_insn (gen_strsetqi (destreg,
11083                                    gen_rtx_SUBREG (QImode, zeroreg, 0)));
11084           emit_label (label);
11085           LABEL_NUSES (label) = 1;
11086         }
11087     }
11088   return 1;
11089 }
11090 /* Expand strlen.  */
11091 int
11092 ix86_expand_strlen (out, src, eoschar, align)
11093      rtx out, src, eoschar, align;
11094 {
11095   rtx addr, scratch1, scratch2, scratch3, scratch4;
11096
11097   /* The generic case of strlen expander is long.  Avoid it's
11098      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
11099
11100   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
11101       && !TARGET_INLINE_ALL_STRINGOPS
11102       && !optimize_size
11103       && (GET_CODE (align) != CONST_INT || INTVAL (align) < 4))
11104     return 0;
11105
11106   addr = force_reg (Pmode, XEXP (src, 0));
11107   scratch1 = gen_reg_rtx (Pmode);
11108
11109   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
11110       && !optimize_size)
11111     {
11112       /* Well it seems that some optimizer does not combine a call like
11113          foo(strlen(bar), strlen(bar));
11114          when the move and the subtraction is done here.  It does calculate
11115          the length just once when these instructions are done inside of
11116          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
11117          often used and I use one fewer register for the lifetime of
11118          output_strlen_unroll() this is better.  */
11119
11120       emit_move_insn (out, addr);
11121
11122       ix86_expand_strlensi_unroll_1 (out, align);
11123
11124       /* strlensi_unroll_1 returns the address of the zero at the end of
11125          the string, like memchr(), so compute the length by subtracting
11126          the start address.  */
11127       if (TARGET_64BIT)
11128         emit_insn (gen_subdi3 (out, out, addr));
11129       else
11130         emit_insn (gen_subsi3 (out, out, addr));
11131     }
11132   else
11133     {
11134       scratch2 = gen_reg_rtx (Pmode);
11135       scratch3 = gen_reg_rtx (Pmode);
11136       scratch4 = force_reg (Pmode, constm1_rtx);
11137
11138       emit_move_insn (scratch3, addr);
11139       eoschar = force_reg (QImode, eoschar);
11140
11141       emit_insn (gen_cld ());
11142       if (TARGET_64BIT)
11143         {
11144           emit_insn (gen_strlenqi_rex_1 (scratch1, scratch3, eoschar,
11145                                          align, scratch4, scratch3));
11146           emit_insn (gen_one_cmpldi2 (scratch2, scratch1));
11147           emit_insn (gen_adddi3 (out, scratch2, constm1_rtx));
11148         }
11149       else
11150         {
11151           emit_insn (gen_strlenqi_1 (scratch1, scratch3, eoschar,
11152                                      align, scratch4, scratch3));
11153           emit_insn (gen_one_cmplsi2 (scratch2, scratch1));
11154           emit_insn (gen_addsi3 (out, scratch2, constm1_rtx));
11155         }
11156     }
11157   return 1;
11158 }
11159
11160 /* Expand the appropriate insns for doing strlen if not just doing
11161    repnz; scasb
11162
11163    out = result, initialized with the start address
11164    align_rtx = alignment of the address.
11165    scratch = scratch register, initialized with the startaddress when
11166         not aligned, otherwise undefined
11167
11168    This is just the body. It needs the initialisations mentioned above and
11169    some address computing at the end.  These things are done in i386.md.  */
11170
11171 static void
11172 ix86_expand_strlensi_unroll_1 (out, align_rtx)
11173      rtx out, align_rtx;
11174 {
11175   int align;
11176   rtx tmp;
11177   rtx align_2_label = NULL_RTX;
11178   rtx align_3_label = NULL_RTX;
11179   rtx align_4_label = gen_label_rtx ();
11180   rtx end_0_label = gen_label_rtx ();
11181   rtx mem;
11182   rtx tmpreg = gen_reg_rtx (SImode);
11183   rtx scratch = gen_reg_rtx (SImode);
11184
11185   align = 0;
11186   if (GET_CODE (align_rtx) == CONST_INT)
11187     align = INTVAL (align_rtx);
11188
11189   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
11190
11191   /* Is there a known alignment and is it less than 4?  */
11192   if (align < 4)
11193     {
11194       rtx scratch1 = gen_reg_rtx (Pmode);
11195       emit_move_insn (scratch1, out);
11196       /* Is there a known alignment and is it not 2? */
11197       if (align != 2)
11198         {
11199           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
11200           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
11201
11202           /* Leave just the 3 lower bits.  */
11203           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
11204                                     NULL_RTX, 0, OPTAB_WIDEN);
11205
11206           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
11207                                    Pmode, 1, align_4_label);
11208           emit_cmp_and_jump_insns (align_rtx, GEN_INT (2), EQ, NULL,
11209                                    Pmode, 1, align_2_label);
11210           emit_cmp_and_jump_insns (align_rtx, GEN_INT (2), GTU, NULL,
11211                                    Pmode, 1, align_3_label);
11212         }
11213       else
11214         {
11215           /* Since the alignment is 2, we have to check 2 or 0 bytes;
11216              check if is aligned to 4 - byte.  */
11217
11218           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (2),
11219                                     NULL_RTX, 0, OPTAB_WIDEN);
11220
11221           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
11222                                    Pmode, 1, align_4_label);
11223         }
11224
11225       mem = gen_rtx_MEM (QImode, out);
11226
11227       /* Now compare the bytes.  */
11228
11229       /* Compare the first n unaligned byte on a byte per byte basis.  */
11230       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
11231                                QImode, 1, end_0_label);
11232
11233       /* Increment the address.  */
11234       if (TARGET_64BIT)
11235         emit_insn (gen_adddi3 (out, out, const1_rtx));
11236       else
11237         emit_insn (gen_addsi3 (out, out, const1_rtx));
11238
11239       /* Not needed with an alignment of 2 */
11240       if (align != 2)
11241         {
11242           emit_label (align_2_label);
11243
11244           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
11245                                    end_0_label);
11246
11247           if (TARGET_64BIT)
11248             emit_insn (gen_adddi3 (out, out, const1_rtx));
11249           else
11250             emit_insn (gen_addsi3 (out, out, const1_rtx));
11251
11252           emit_label (align_3_label);
11253         }
11254
11255       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
11256                                end_0_label);
11257
11258       if (TARGET_64BIT)
11259         emit_insn (gen_adddi3 (out, out, const1_rtx));
11260       else
11261         emit_insn (gen_addsi3 (out, out, const1_rtx));
11262     }
11263
11264   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
11265      align this loop.  It gives only huge programs, but does not help to
11266      speed up.  */
11267   emit_label (align_4_label);
11268
11269   mem = gen_rtx_MEM (SImode, out);
11270   emit_move_insn (scratch, mem);
11271   if (TARGET_64BIT)
11272     emit_insn (gen_adddi3 (out, out, GEN_INT (4)));
11273   else
11274     emit_insn (gen_addsi3 (out, out, GEN_INT (4)));
11275
11276   /* This formula yields a nonzero result iff one of the bytes is zero.
11277      This saves three branches inside loop and many cycles.  */
11278
11279   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
11280   emit_insn (gen_one_cmplsi2 (scratch, scratch));
11281   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
11282   emit_insn (gen_andsi3 (tmpreg, tmpreg,
11283                          gen_int_mode (0x80808080, SImode)));
11284   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
11285                            align_4_label);
11286
11287   if (TARGET_CMOVE)
11288     {
11289        rtx reg = gen_reg_rtx (SImode);
11290        rtx reg2 = gen_reg_rtx (Pmode);
11291        emit_move_insn (reg, tmpreg);
11292        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
11293
11294        /* If zero is not in the first two bytes, move two bytes forward.  */
11295        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
11296        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
11297        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
11298        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
11299                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
11300                                                      reg,
11301                                                      tmpreg)));
11302        /* Emit lea manually to avoid clobbering of flags.  */
11303        emit_insn (gen_rtx_SET (SImode, reg2,
11304                                gen_rtx_PLUS (Pmode, out, GEN_INT (2))));
11305
11306        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
11307        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
11308        emit_insn (gen_rtx_SET (VOIDmode, out,
11309                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
11310                                                      reg2,
11311                                                      out)));
11312
11313     }
11314   else
11315     {
11316        rtx end_2_label = gen_label_rtx ();
11317        /* Is zero in the first two bytes? */
11318
11319        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
11320        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
11321        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
11322        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
11323                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
11324                             pc_rtx);
11325        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
11326        JUMP_LABEL (tmp) = end_2_label;
11327
11328        /* Not in the first two.  Move two bytes forward.  */
11329        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
11330        if (TARGET_64BIT)
11331          emit_insn (gen_adddi3 (out, out, GEN_INT (2)));
11332        else
11333          emit_insn (gen_addsi3 (out, out, GEN_INT (2)));
11334
11335        emit_label (end_2_label);
11336
11337     }
11338
11339   /* Avoid branch in fixing the byte.  */
11340   tmpreg = gen_lowpart (QImode, tmpreg);
11341   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
11342   if (TARGET_64BIT)
11343     emit_insn (gen_subdi3_carry_rex64 (out, out, GEN_INT (3)));
11344   else
11345     emit_insn (gen_subsi3_carry (out, out, GEN_INT (3)));
11346
11347   emit_label (end_0_label);
11348 }
11349
11350 void
11351 ix86_expand_call (retval, fnaddr, callarg1, callarg2, pop, sibcall)
11352      rtx retval, fnaddr, callarg1, callarg2, pop;
11353      int sibcall;
11354 {
11355   rtx use = NULL, call;
11356
11357   if (pop == const0_rtx)
11358     pop = NULL;
11359   if (TARGET_64BIT && pop)
11360     abort ();
11361
11362 #if TARGET_MACHO
11363   if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
11364     fnaddr = machopic_indirect_call_target (fnaddr);
11365 #else
11366   /* Static functions and indirect calls don't need the pic register.  */
11367   if (! TARGET_64BIT && flag_pic
11368       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
11369       && ! SYMBOL_REF_FLAG (XEXP (fnaddr, 0)))
11370     use_reg (&use, pic_offset_table_rtx);
11371
11372   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
11373     {
11374       rtx al = gen_rtx_REG (QImode, 0);
11375       emit_move_insn (al, callarg2);
11376       use_reg (&use, al);
11377     }
11378 #endif /* TARGET_MACHO */
11379
11380   if (! call_insn_operand (XEXP (fnaddr, 0), Pmode))
11381     {
11382       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
11383       fnaddr = gen_rtx_MEM (QImode, fnaddr);
11384     }
11385   if (sibcall && TARGET_64BIT
11386       && !constant_call_address_operand (XEXP (fnaddr, 0), Pmode))
11387     {
11388       rtx addr;
11389       addr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
11390       fnaddr = gen_rtx_REG (Pmode, 40);
11391       emit_move_insn (fnaddr, addr);
11392       fnaddr = gen_rtx_MEM (QImode, fnaddr);
11393     }
11394
11395   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
11396   if (retval)
11397     call = gen_rtx_SET (VOIDmode, retval, call);
11398   if (pop)
11399     {
11400       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
11401       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
11402       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
11403     }
11404
11405   call = emit_call_insn (call);
11406   if (use)
11407     CALL_INSN_FUNCTION_USAGE (call) = use;
11408 }
11409
11410 \f
11411 /* Clear stack slot assignments remembered from previous functions.
11412    This is called from INIT_EXPANDERS once before RTL is emitted for each
11413    function.  */
11414
11415 static struct machine_function *
11416 ix86_init_machine_status ()
11417 {
11418   return ggc_alloc_cleared (sizeof (struct machine_function));
11419 }
11420
11421 /* Return a MEM corresponding to a stack slot with mode MODE.
11422    Allocate a new slot if necessary.
11423
11424    The RTL for a function can have several slots available: N is
11425    which slot to use.  */
11426
11427 rtx
11428 assign_386_stack_local (mode, n)
11429      enum machine_mode mode;
11430      int n;
11431 {
11432   if (n < 0 || n >= MAX_386_STACK_LOCALS)
11433     abort ();
11434
11435   if (ix86_stack_locals[(int) mode][n] == NULL_RTX)
11436     ix86_stack_locals[(int) mode][n]
11437       = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
11438
11439   return ix86_stack_locals[(int) mode][n];
11440 }
11441
11442 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
11443
11444 static GTY(()) rtx ix86_tls_symbol;
11445 rtx
11446 ix86_tls_get_addr ()
11447 {
11448
11449   if (!ix86_tls_symbol)
11450     {
11451       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
11452                                             (TARGET_GNU_TLS && !TARGET_64BIT)
11453                                             ? "___tls_get_addr"
11454                                             : "__tls_get_addr");
11455     }
11456
11457   return ix86_tls_symbol;
11458 }
11459 \f
11460 /* Calculate the length of the memory address in the instruction
11461    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
11462
11463 static int
11464 memory_address_length (addr)
11465      rtx addr;
11466 {
11467   struct ix86_address parts;
11468   rtx base, index, disp;
11469   int len;
11470
11471   if (GET_CODE (addr) == PRE_DEC
11472       || GET_CODE (addr) == POST_INC
11473       || GET_CODE (addr) == PRE_MODIFY
11474       || GET_CODE (addr) == POST_MODIFY)
11475     return 0;
11476
11477   if (! ix86_decompose_address (addr, &parts))
11478     abort ();
11479
11480   base = parts.base;
11481   index = parts.index;
11482   disp = parts.disp;
11483   len = 0;
11484
11485   /* Register Indirect.  */
11486   if (base && !index && !disp)
11487     {
11488       /* Special cases: ebp and esp need the two-byte modrm form.  */
11489       if (addr == stack_pointer_rtx
11490           || addr == arg_pointer_rtx
11491           || addr == frame_pointer_rtx
11492           || addr == hard_frame_pointer_rtx)
11493         len = 1;
11494     }
11495
11496   /* Direct Addressing.  */
11497   else if (disp && !base && !index)
11498     len = 4;
11499
11500   else
11501     {
11502       /* Find the length of the displacement constant.  */
11503       if (disp)
11504         {
11505           if (GET_CODE (disp) == CONST_INT
11506               && CONST_OK_FOR_LETTER_P (INTVAL (disp), 'K'))
11507             len = 1;
11508           else
11509             len = 4;
11510         }
11511
11512       /* An index requires the two-byte modrm form.  */
11513       if (index)
11514         len += 1;
11515     }
11516
11517   return len;
11518 }
11519
11520 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
11521    is set, expect that insn have 8bit immediate alternative.  */
11522 int
11523 ix86_attr_length_immediate_default (insn, shortform)
11524      rtx insn;
11525      int shortform;
11526 {
11527   int len = 0;
11528   int i;
11529   extract_insn_cached (insn);
11530   for (i = recog_data.n_operands - 1; i >= 0; --i)
11531     if (CONSTANT_P (recog_data.operand[i]))
11532       {
11533         if (len)
11534           abort ();
11535         if (shortform
11536             && GET_CODE (recog_data.operand[i]) == CONST_INT
11537             && CONST_OK_FOR_LETTER_P (INTVAL (recog_data.operand[i]), 'K'))
11538           len = 1;
11539         else
11540           {
11541             switch (get_attr_mode (insn))
11542               {
11543                 case MODE_QI:
11544                   len+=1;
11545                   break;
11546                 case MODE_HI:
11547                   len+=2;
11548                   break;
11549                 case MODE_SI:
11550                   len+=4;
11551                   break;
11552                 /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
11553                 case MODE_DI:
11554                   len+=4;
11555                   break;
11556                 default:
11557                   fatal_insn ("unknown insn mode", insn);
11558               }
11559           }
11560       }
11561   return len;
11562 }
11563 /* Compute default value for "length_address" attribute.  */
11564 int
11565 ix86_attr_length_address_default (insn)
11566      rtx insn;
11567 {
11568   int i;
11569   extract_insn_cached (insn);
11570   for (i = recog_data.n_operands - 1; i >= 0; --i)
11571     if (GET_CODE (recog_data.operand[i]) == MEM)
11572       {
11573         return memory_address_length (XEXP (recog_data.operand[i], 0));
11574         break;
11575       }
11576   return 0;
11577 }
11578 \f
11579 /* Return the maximum number of instructions a cpu can issue.  */
11580
11581 static int
11582 ix86_issue_rate ()
11583 {
11584   switch (ix86_cpu)
11585     {
11586     case PROCESSOR_PENTIUM:
11587     case PROCESSOR_K6:
11588       return 2;
11589
11590     case PROCESSOR_PENTIUMPRO:
11591     case PROCESSOR_PENTIUM4:
11592     case PROCESSOR_ATHLON:
11593     case PROCESSOR_K8:
11594       return 3;
11595
11596     default:
11597       return 1;
11598     }
11599 }
11600
11601 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
11602    by DEP_INSN and nothing set by DEP_INSN.  */
11603
11604 static int
11605 ix86_flags_dependant (insn, dep_insn, insn_type)
11606      rtx insn, dep_insn;
11607      enum attr_type insn_type;
11608 {
11609   rtx set, set2;
11610
11611   /* Simplify the test for uninteresting insns.  */
11612   if (insn_type != TYPE_SETCC
11613       && insn_type != TYPE_ICMOV
11614       && insn_type != TYPE_FCMOV
11615       && insn_type != TYPE_IBR)
11616     return 0;
11617
11618   if ((set = single_set (dep_insn)) != 0)
11619     {
11620       set = SET_DEST (set);
11621       set2 = NULL_RTX;
11622     }
11623   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
11624            && XVECLEN (PATTERN (dep_insn), 0) == 2
11625            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
11626            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
11627     {
11628       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
11629       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
11630     }
11631   else
11632     return 0;
11633
11634   if (GET_CODE (set) != REG || REGNO (set) != FLAGS_REG)
11635     return 0;
11636
11637   /* This test is true if the dependent insn reads the flags but
11638      not any other potentially set register.  */
11639   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
11640     return 0;
11641
11642   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
11643     return 0;
11644
11645   return 1;
11646 }
11647
11648 /* A subroutine of ix86_adjust_cost -- return true iff INSN has a memory
11649    address with operands set by DEP_INSN.  */
11650
11651 static int
11652 ix86_agi_dependant (insn, dep_insn, insn_type)
11653      rtx insn, dep_insn;
11654      enum attr_type insn_type;
11655 {
11656   rtx addr;
11657
11658   if (insn_type == TYPE_LEA
11659       && TARGET_PENTIUM)
11660     {
11661       addr = PATTERN (insn);
11662       if (GET_CODE (addr) == SET)
11663         ;
11664       else if (GET_CODE (addr) == PARALLEL
11665                && GET_CODE (XVECEXP (addr, 0, 0)) == SET)
11666         addr = XVECEXP (addr, 0, 0);
11667       else
11668         abort ();
11669       addr = SET_SRC (addr);
11670     }
11671   else
11672     {
11673       int i;
11674       extract_insn_cached (insn);
11675       for (i = recog_data.n_operands - 1; i >= 0; --i)
11676         if (GET_CODE (recog_data.operand[i]) == MEM)
11677           {
11678             addr = XEXP (recog_data.operand[i], 0);
11679             goto found;
11680           }
11681       return 0;
11682     found:;
11683     }
11684
11685   return modified_in_p (addr, dep_insn);
11686 }
11687
11688 static int
11689 ix86_adjust_cost (insn, link, dep_insn, cost)
11690      rtx insn, link, dep_insn;
11691      int cost;
11692 {
11693   enum attr_type insn_type, dep_insn_type;
11694   enum attr_memory memory, dep_memory;
11695   rtx set, set2;
11696   int dep_insn_code_number;
11697
11698   /* Anti and output dependencies have zero cost on all CPUs.  */
11699   if (REG_NOTE_KIND (link) != 0)
11700     return 0;
11701
11702   dep_insn_code_number = recog_memoized (dep_insn);
11703
11704   /* If we can't recognize the insns, we can't really do anything.  */
11705   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
11706     return cost;
11707
11708   insn_type = get_attr_type (insn);
11709   dep_insn_type = get_attr_type (dep_insn);
11710
11711   switch (ix86_cpu)
11712     {
11713     case PROCESSOR_PENTIUM:
11714       /* Address Generation Interlock adds a cycle of latency.  */
11715       if (ix86_agi_dependant (insn, dep_insn, insn_type))
11716         cost += 1;
11717
11718       /* ??? Compares pair with jump/setcc.  */
11719       if (ix86_flags_dependant (insn, dep_insn, insn_type))
11720         cost = 0;
11721
11722       /* Floating point stores require value to be ready one cycle earlier.  */
11723       if (insn_type == TYPE_FMOV
11724           && get_attr_memory (insn) == MEMORY_STORE
11725           && !ix86_agi_dependant (insn, dep_insn, insn_type))
11726         cost += 1;
11727       break;
11728
11729     case PROCESSOR_PENTIUMPRO:
11730       memory = get_attr_memory (insn);
11731       dep_memory = get_attr_memory (dep_insn);
11732
11733       /* Since we can't represent delayed latencies of load+operation,
11734          increase the cost here for non-imov insns.  */
11735       if (dep_insn_type != TYPE_IMOV
11736           && dep_insn_type != TYPE_FMOV
11737           && (dep_memory == MEMORY_LOAD || dep_memory == MEMORY_BOTH))
11738         cost += 1;
11739
11740       /* INT->FP conversion is expensive.  */
11741       if (get_attr_fp_int_src (dep_insn))
11742         cost += 5;
11743
11744       /* There is one cycle extra latency between an FP op and a store.  */
11745       if (insn_type == TYPE_FMOV
11746           && (set = single_set (dep_insn)) != NULL_RTX
11747           && (set2 = single_set (insn)) != NULL_RTX
11748           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
11749           && GET_CODE (SET_DEST (set2)) == MEM)
11750         cost += 1;
11751
11752       /* Show ability of reorder buffer to hide latency of load by executing
11753          in parallel with previous instruction in case
11754          previous instruction is not needed to compute the address.  */
11755       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
11756           && !ix86_agi_dependant (insn, dep_insn, insn_type))
11757         {
11758           /* Claim moves to take one cycle, as core can issue one load
11759              at time and the next load can start cycle later.  */
11760           if (dep_insn_type == TYPE_IMOV
11761               || dep_insn_type == TYPE_FMOV)
11762             cost = 1;
11763           else if (cost > 1)
11764             cost--;
11765         }
11766       break;
11767
11768     case PROCESSOR_K6:
11769       memory = get_attr_memory (insn);
11770       dep_memory = get_attr_memory (dep_insn);
11771       /* The esp dependency is resolved before the instruction is really
11772          finished.  */
11773       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
11774           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
11775         return 1;
11776
11777       /* Since we can't represent delayed latencies of load+operation,
11778          increase the cost here for non-imov insns.  */
11779       if (dep_memory == MEMORY_LOAD || dep_memory == MEMORY_BOTH)
11780         cost += (dep_insn_type != TYPE_IMOV) ? 2 : 1;
11781
11782       /* INT->FP conversion is expensive.  */
11783       if (get_attr_fp_int_src (dep_insn))
11784         cost += 5;
11785
11786       /* Show ability of reorder buffer to hide latency of load by executing
11787          in parallel with previous instruction in case
11788          previous instruction is not needed to compute the address.  */
11789       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
11790           && !ix86_agi_dependant (insn, dep_insn, insn_type))
11791         {
11792           /* Claim moves to take one cycle, as core can issue one load
11793              at time and the next load can start cycle later.  */
11794           if (dep_insn_type == TYPE_IMOV
11795               || dep_insn_type == TYPE_FMOV)
11796             cost = 1;
11797           else if (cost > 2)
11798             cost -= 2;
11799           else
11800             cost = 1;
11801         }
11802       break;
11803
11804     case PROCESSOR_ATHLON:
11805     case PROCESSOR_K8:
11806       memory = get_attr_memory (insn);
11807       dep_memory = get_attr_memory (dep_insn);
11808
11809       /* Show ability of reorder buffer to hide latency of load by executing
11810          in parallel with previous instruction in case
11811          previous instruction is not needed to compute the address.  */
11812       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
11813           && !ix86_agi_dependant (insn, dep_insn, insn_type))
11814         {
11815           /* Claim moves to take one cycle, as core can issue one load
11816              at time and the next load can start cycle later.  */
11817           if (dep_insn_type == TYPE_IMOV
11818               || dep_insn_type == TYPE_FMOV)
11819             cost = 0;
11820           else if (cost >= 3)
11821             cost -= 3;
11822           else
11823             cost = 0;
11824         }
11825
11826     default:
11827       break;
11828     }
11829
11830   return cost;
11831 }
11832
11833 static union
11834 {
11835   struct ppro_sched_data
11836   {
11837     rtx decode[3];
11838     int issued_this_cycle;
11839   } ppro;
11840 } ix86_sched_data;
11841
11842 static enum attr_ppro_uops
11843 ix86_safe_ppro_uops (insn)
11844      rtx insn;
11845 {
11846   if (recog_memoized (insn) >= 0)
11847     return get_attr_ppro_uops (insn);
11848   else
11849     return PPRO_UOPS_MANY;
11850 }
11851
11852 static void
11853 ix86_dump_ppro_packet (dump)
11854      FILE *dump;
11855 {
11856   if (ix86_sched_data.ppro.decode[0])
11857     {
11858       fprintf (dump, "PPRO packet: %d",
11859                INSN_UID (ix86_sched_data.ppro.decode[0]));
11860       if (ix86_sched_data.ppro.decode[1])
11861         fprintf (dump, " %d", INSN_UID (ix86_sched_data.ppro.decode[1]));
11862       if (ix86_sched_data.ppro.decode[2])
11863         fprintf (dump, " %d", INSN_UID (ix86_sched_data.ppro.decode[2]));
11864       fputc ('\n', dump);
11865     }
11866 }
11867
11868 /* We're beginning a new block.  Initialize data structures as necessary.  */
11869
11870 static void
11871 ix86_sched_init (dump, sched_verbose, veclen)
11872      FILE *dump ATTRIBUTE_UNUSED;
11873      int sched_verbose ATTRIBUTE_UNUSED;
11874      int veclen ATTRIBUTE_UNUSED;
11875 {
11876   memset (&ix86_sched_data, 0, sizeof (ix86_sched_data));
11877 }
11878
11879 /* Shift INSN to SLOT, and shift everything else down.  */
11880
11881 static void
11882 ix86_reorder_insn (insnp, slot)
11883      rtx *insnp, *slot;
11884 {
11885   if (insnp != slot)
11886     {
11887       rtx insn = *insnp;
11888       do
11889         insnp[0] = insnp[1];
11890       while (++insnp != slot);
11891       *insnp = insn;
11892     }
11893 }
11894
11895 static void
11896 ix86_sched_reorder_ppro (ready, e_ready)
11897      rtx *ready;
11898      rtx *e_ready;
11899 {
11900   rtx decode[3];
11901   enum attr_ppro_uops cur_uops;
11902   int issued_this_cycle;
11903   rtx *insnp;
11904   int i;
11905
11906   /* At this point .ppro.decode contains the state of the three
11907      decoders from last "cycle".  That is, those insns that were
11908      actually independent.  But here we're scheduling for the
11909      decoder, and we may find things that are decodable in the
11910      same cycle.  */
11911
11912   memcpy (decode, ix86_sched_data.ppro.decode, sizeof (decode));
11913   issued_this_cycle = 0;
11914
11915   insnp = e_ready;
11916   cur_uops = ix86_safe_ppro_uops (*insnp);
11917
11918   /* If the decoders are empty, and we've a complex insn at the
11919      head of the priority queue, let it issue without complaint.  */
11920   if (decode[0] == NULL)
11921     {
11922       if (cur_uops == PPRO_UOPS_MANY)
11923         {
11924           decode[0] = *insnp;
11925           goto ppro_done;
11926         }
11927
11928       /* Otherwise, search for a 2-4 uop unsn to issue.  */
11929       while (cur_uops != PPRO_UOPS_FEW)
11930         {
11931           if (insnp == ready)
11932             break;
11933           cur_uops = ix86_safe_ppro_uops (*--insnp);
11934         }
11935
11936       /* If so, move it to the head of the line.  */
11937       if (cur_uops == PPRO_UOPS_FEW)
11938         ix86_reorder_insn (insnp, e_ready);
11939
11940       /* Issue the head of the queue.  */
11941       issued_this_cycle = 1;
11942       decode[0] = *e_ready--;
11943     }
11944
11945   /* Look for simple insns to fill in the other two slots.  */
11946   for (i = 1; i < 3; ++i)
11947     if (decode[i] == NULL)
11948       {
11949         if (ready > e_ready)
11950           goto ppro_done;
11951
11952         insnp = e_ready;
11953         cur_uops = ix86_safe_ppro_uops (*insnp);
11954         while (cur_uops != PPRO_UOPS_ONE)
11955           {
11956             if (insnp == ready)
11957               break;
11958             cur_uops = ix86_safe_ppro_uops (*--insnp);
11959           }
11960
11961         /* Found one.  Move it to the head of the queue and issue it.  */
11962         if (cur_uops == PPRO_UOPS_ONE)
11963           {
11964             ix86_reorder_insn (insnp, e_ready);
11965             decode[i] = *e_ready--;
11966             issued_this_cycle++;
11967             continue;
11968           }
11969
11970         /* ??? Didn't find one.  Ideally, here we would do a lazy split
11971            of 2-uop insns, issue one and queue the other.  */
11972       }
11973
11974  ppro_done:
11975   if (issued_this_cycle == 0)
11976     issued_this_cycle = 1;
11977   ix86_sched_data.ppro.issued_this_cycle = issued_this_cycle;
11978 }
11979
11980 /* We are about to being issuing insns for this clock cycle.
11981    Override the default sort algorithm to better slot instructions.  */
11982 static int
11983 ix86_sched_reorder (dump, sched_verbose, ready, n_readyp, clock_var)
11984      FILE *dump ATTRIBUTE_UNUSED;
11985      int sched_verbose ATTRIBUTE_UNUSED;
11986      rtx *ready;
11987      int *n_readyp;
11988      int clock_var ATTRIBUTE_UNUSED;
11989 {
11990   int n_ready = *n_readyp;
11991   rtx *e_ready = ready + n_ready - 1;
11992
11993   /* Make sure to go ahead and initialize key items in
11994      ix86_sched_data if we are not going to bother trying to
11995      reorder the ready queue.  */
11996   if (n_ready < 2)
11997     {
11998       ix86_sched_data.ppro.issued_this_cycle = 1;
11999       goto out;
12000     }
12001
12002   switch (ix86_cpu)
12003     {
12004     default:
12005       break;
12006
12007     case PROCESSOR_PENTIUMPRO:
12008       ix86_sched_reorder_ppro (ready, e_ready);
12009       break;
12010     }
12011
12012 out:
12013   return ix86_issue_rate ();
12014 }
12015
12016 /* We are about to issue INSN.  Return the number of insns left on the
12017    ready queue that can be issued this cycle.  */
12018
12019 static int
12020 ix86_variable_issue (dump, sched_verbose, insn, can_issue_more)
12021      FILE *dump;
12022      int sched_verbose;
12023      rtx insn;
12024      int can_issue_more;
12025 {
12026   int i;
12027   switch (ix86_cpu)
12028     {
12029     default:
12030       return can_issue_more - 1;
12031
12032     case PROCESSOR_PENTIUMPRO:
12033       {
12034         enum attr_ppro_uops uops = ix86_safe_ppro_uops (insn);
12035
12036         if (uops == PPRO_UOPS_MANY)
12037           {
12038             if (sched_verbose)
12039               ix86_dump_ppro_packet (dump);
12040             ix86_sched_data.ppro.decode[0] = insn;
12041             ix86_sched_data.ppro.decode[1] = NULL;
12042             ix86_sched_data.ppro.decode[2] = NULL;
12043             if (sched_verbose)
12044               ix86_dump_ppro_packet (dump);
12045             ix86_sched_data.ppro.decode[0] = NULL;
12046           }
12047         else if (uops == PPRO_UOPS_FEW)
12048           {
12049             if (sched_verbose)
12050               ix86_dump_ppro_packet (dump);
12051             ix86_sched_data.ppro.decode[0] = insn;
12052             ix86_sched_data.ppro.decode[1] = NULL;
12053             ix86_sched_data.ppro.decode[2] = NULL;
12054           }
12055         else
12056           {
12057             for (i = 0; i < 3; ++i)
12058               if (ix86_sched_data.ppro.decode[i] == NULL)
12059                 {
12060                   ix86_sched_data.ppro.decode[i] = insn;
12061                   break;
12062                 }
12063             if (i == 3)
12064               abort ();
12065             if (i == 2)
12066               {
12067                 if (sched_verbose)
12068                   ix86_dump_ppro_packet (dump);
12069                 ix86_sched_data.ppro.decode[0] = NULL;
12070                 ix86_sched_data.ppro.decode[1] = NULL;
12071                 ix86_sched_data.ppro.decode[2] = NULL;
12072               }
12073           }
12074       }
12075       return --ix86_sched_data.ppro.issued_this_cycle;
12076     }
12077 }
12078
12079 static int
12080 ia32_use_dfa_pipeline_interface ()
12081 {
12082   if (TARGET_PENTIUM || TARGET_ATHLON_K8)
12083     return 1;
12084   return 0;
12085 }
12086
12087 /* How many alternative schedules to try.  This should be as wide as the
12088    scheduling freedom in the DFA, but no wider.  Making this value too
12089    large results extra work for the scheduler.  */
12090
12091 static int
12092 ia32_multipass_dfa_lookahead ()
12093 {
12094   if (ix86_cpu == PROCESSOR_PENTIUM)
12095     return 2;
12096   else
12097    return 0;
12098 }
12099
12100 \f
12101 /* Walk through INSNS and look for MEM references whose address is DSTREG or
12102    SRCREG and set the memory attribute to those of DSTREF and SRCREF, as
12103    appropriate.  */
12104
12105 void
12106 ix86_set_move_mem_attrs (insns, dstref, srcref, dstreg, srcreg)
12107      rtx insns;
12108      rtx dstref, srcref, dstreg, srcreg;
12109 {
12110   rtx insn;
12111
12112   for (insn = insns; insn != 0 ; insn = NEXT_INSN (insn))
12113     if (INSN_P (insn))
12114       ix86_set_move_mem_attrs_1 (PATTERN (insn), dstref, srcref,
12115                                  dstreg, srcreg);
12116 }
12117
12118 /* Subroutine of above to actually do the updating by recursively walking
12119    the rtx.  */
12120
12121 static void
12122 ix86_set_move_mem_attrs_1 (x, dstref, srcref, dstreg, srcreg)
12123      rtx x;
12124      rtx dstref, srcref, dstreg, srcreg;
12125 {
12126   enum rtx_code code = GET_CODE (x);
12127   const char *format_ptr = GET_RTX_FORMAT (code);
12128   int i, j;
12129
12130   if (code == MEM && XEXP (x, 0) == dstreg)
12131     MEM_COPY_ATTRIBUTES (x, dstref);
12132   else if (code == MEM && XEXP (x, 0) == srcreg)
12133     MEM_COPY_ATTRIBUTES (x, srcref);
12134
12135   for (i = 0; i < GET_RTX_LENGTH (code); i++, format_ptr++)
12136     {
12137       if (*format_ptr == 'e')
12138         ix86_set_move_mem_attrs_1 (XEXP (x, i), dstref, srcref,
12139                                    dstreg, srcreg);
12140       else if (*format_ptr == 'E')
12141         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12142           ix86_set_move_mem_attrs_1 (XVECEXP (x, i, j), dstref, srcref,
12143                                      dstreg, srcreg);
12144     }
12145 }
12146 \f
12147 /* Compute the alignment given to a constant that is being placed in memory.
12148    EXP is the constant and ALIGN is the alignment that the object would
12149    ordinarily have.
12150    The value of this function is used instead of that alignment to align
12151    the object.  */
12152
12153 int
12154 ix86_constant_alignment (exp, align)
12155      tree exp;
12156      int align;
12157 {
12158   if (TREE_CODE (exp) == REAL_CST)
12159     {
12160       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
12161         return 64;
12162       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
12163         return 128;
12164     }
12165   else if (TREE_CODE (exp) == STRING_CST && TREE_STRING_LENGTH (exp) >= 31
12166            && align < 256)
12167     return 256;
12168
12169   return align;
12170 }
12171
12172 /* Compute the alignment for a static variable.
12173    TYPE is the data type, and ALIGN is the alignment that
12174    the object would ordinarily have.  The value of this function is used
12175    instead of that alignment to align the object.  */
12176
12177 int
12178 ix86_data_alignment (type, align)
12179      tree type;
12180      int align;
12181 {
12182   if (AGGREGATE_TYPE_P (type)
12183        && TYPE_SIZE (type)
12184        && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
12185        && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 256
12186            || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 256)
12187     return 256;
12188
12189   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
12190      to 16byte boundary.  */
12191   if (TARGET_64BIT)
12192     {
12193       if (AGGREGATE_TYPE_P (type)
12194            && TYPE_SIZE (type)
12195            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
12196            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
12197                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
12198         return 128;
12199     }
12200
12201   if (TREE_CODE (type) == ARRAY_TYPE)
12202     {
12203       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
12204         return 64;
12205       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
12206         return 128;
12207     }
12208   else if (TREE_CODE (type) == COMPLEX_TYPE)
12209     {
12210
12211       if (TYPE_MODE (type) == DCmode && align < 64)
12212         return 64;
12213       if (TYPE_MODE (type) == XCmode && align < 128)
12214         return 128;
12215     }
12216   else if ((TREE_CODE (type) == RECORD_TYPE
12217             || TREE_CODE (type) == UNION_TYPE
12218             || TREE_CODE (type) == QUAL_UNION_TYPE)
12219            && TYPE_FIELDS (type))
12220     {
12221       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
12222         return 64;
12223       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
12224         return 128;
12225     }
12226   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
12227            || TREE_CODE (type) == INTEGER_TYPE)
12228     {
12229       if (TYPE_MODE (type) == DFmode && align < 64)
12230         return 64;
12231       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
12232         return 128;
12233     }
12234
12235   return align;
12236 }
12237
12238 /* Compute the alignment for a local variable.
12239    TYPE is the data type, and ALIGN is the alignment that
12240    the object would ordinarily have.  The value of this macro is used
12241    instead of that alignment to align the object.  */
12242
12243 int
12244 ix86_local_alignment (type, align)
12245      tree type;
12246      int align;
12247 {
12248   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
12249      to 16byte boundary.  */
12250   if (TARGET_64BIT)
12251     {
12252       if (AGGREGATE_TYPE_P (type)
12253            && TYPE_SIZE (type)
12254            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
12255            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
12256                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
12257         return 128;
12258     }
12259   if (TREE_CODE (type) == ARRAY_TYPE)
12260     {
12261       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
12262         return 64;
12263       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
12264         return 128;
12265     }
12266   else if (TREE_CODE (type) == COMPLEX_TYPE)
12267     {
12268       if (TYPE_MODE (type) == DCmode && align < 64)
12269         return 64;
12270       if (TYPE_MODE (type) == XCmode && align < 128)
12271         return 128;
12272     }
12273   else if ((TREE_CODE (type) == RECORD_TYPE
12274             || TREE_CODE (type) == UNION_TYPE
12275             || TREE_CODE (type) == QUAL_UNION_TYPE)
12276            && TYPE_FIELDS (type))
12277     {
12278       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
12279         return 64;
12280       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
12281         return 128;
12282     }
12283   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
12284            || TREE_CODE (type) == INTEGER_TYPE)
12285     {
12286
12287       if (TYPE_MODE (type) == DFmode && align < 64)
12288         return 64;
12289       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
12290         return 128;
12291     }
12292   return align;
12293 }
12294 \f
12295 /* Emit RTL insns to initialize the variable parts of a trampoline.
12296    FNADDR is an RTX for the address of the function's pure code.
12297    CXT is an RTX for the static chain value for the function.  */
12298 void
12299 x86_initialize_trampoline (tramp, fnaddr, cxt)
12300      rtx tramp, fnaddr, cxt;
12301 {
12302   if (!TARGET_64BIT)
12303     {
12304       /* Compute offset from the end of the jmp to the target function.  */
12305       rtx disp = expand_binop (SImode, sub_optab, fnaddr,
12306                                plus_constant (tramp, 10),
12307                                NULL_RTX, 1, OPTAB_DIRECT);
12308       emit_move_insn (gen_rtx_MEM (QImode, tramp),
12309                       gen_int_mode (0xb9, QImode));
12310       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 1)), cxt);
12311       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 5)),
12312                       gen_int_mode (0xe9, QImode));
12313       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 6)), disp);
12314     }
12315   else
12316     {
12317       int offset = 0;
12318       /* Try to load address using shorter movl instead of movabs.
12319          We may want to support movq for kernel mode, but kernel does not use
12320          trampolines at the moment.  */
12321       if (x86_64_zero_extended_value (fnaddr))
12322         {
12323           fnaddr = copy_to_mode_reg (DImode, fnaddr);
12324           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
12325                           gen_int_mode (0xbb41, HImode));
12326           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, offset + 2)),
12327                           gen_lowpart (SImode, fnaddr));
12328           offset += 6;
12329         }
12330       else
12331         {
12332           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
12333                           gen_int_mode (0xbb49, HImode));
12334           emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
12335                           fnaddr);
12336           offset += 10;
12337         }
12338       /* Load static chain using movabs to r10.  */
12339       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
12340                       gen_int_mode (0xba49, HImode));
12341       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
12342                       cxt);
12343       offset += 10;
12344       /* Jump to the r11 */
12345       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
12346                       gen_int_mode (0xff49, HImode));
12347       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, offset+2)),
12348                       gen_int_mode (0xe3, QImode));
12349       offset += 3;
12350       if (offset > TRAMPOLINE_SIZE)
12351         abort ();
12352     }
12353
12354 #ifdef TRANSFER_FROM_TRAMPOLINE
12355   emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "__enable_execute_stack"),
12356                      LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
12357 #endif
12358 }
12359 \f
12360 #define def_builtin(MASK, NAME, TYPE, CODE)                     \
12361 do {                                                            \
12362   if ((MASK) & target_flags)                                    \
12363     builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,      \
12364                       NULL, NULL_TREE);                         \
12365 } while (0)
12366
12367 struct builtin_description
12368 {
12369   const unsigned int mask;
12370   const enum insn_code icode;
12371   const char *const name;
12372   const enum ix86_builtins code;
12373   const enum rtx_code comparison;
12374   const unsigned int flag;
12375 };
12376
12377 /* Used for builtins that are enabled both by -msse and -msse2.  */
12378 #define MASK_SSE1 (MASK_SSE | MASK_SSE2)
12379
12380 static const struct builtin_description bdesc_comi[] =
12381 {
12382   { MASK_SSE1, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
12383   { MASK_SSE1, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
12384   { MASK_SSE1, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
12385   { MASK_SSE1, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
12386   { MASK_SSE1, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
12387   { MASK_SSE1, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
12388   { MASK_SSE1, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
12389   { MASK_SSE1, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
12390   { MASK_SSE1, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
12391   { MASK_SSE1, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
12392   { MASK_SSE1, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
12393   { MASK_SSE1, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
12394   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
12395   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
12396   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
12397   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
12398   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
12399   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
12400   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
12401   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
12402   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
12403   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
12404   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
12405   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
12406 };
12407
12408 static const struct builtin_description bdesc_2arg[] =
12409 {
12410   /* SSE */
12411   { MASK_SSE1, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, 0, 0 },
12412   { MASK_SSE1, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, 0, 0 },
12413   { MASK_SSE1, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, 0, 0 },
12414   { MASK_SSE1, CODE_FOR_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, 0, 0 },
12415   { MASK_SSE1, CODE_FOR_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, 0, 0 },
12416   { MASK_SSE1, CODE_FOR_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, 0, 0 },
12417   { MASK_SSE1, CODE_FOR_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, 0, 0 },
12418   { MASK_SSE1, CODE_FOR_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, 0, 0 },
12419
12420   { MASK_SSE1, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, 0 },
12421   { MASK_SSE1, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, 0 },
12422   { MASK_SSE1, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, 0 },
12423   { MASK_SSE1, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT, 1 },
12424   { MASK_SSE1, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE, 1 },
12425   { MASK_SSE1, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, 0 },
12426   { MASK_SSE1, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, EQ, 0 },
12427   { MASK_SSE1, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, LT, 0 },
12428   { MASK_SSE1, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, LE, 0 },
12429   { MASK_SSE1, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, LT, 1 },
12430   { MASK_SSE1, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, LE, 1 },
12431   { MASK_SSE1, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, UNORDERED, 0 },
12432   { MASK_SSE1, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, 0 },
12433   { MASK_SSE1, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, 0 },
12434   { MASK_SSE1, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, 0 },
12435   { MASK_SSE1, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, 0 },
12436   { MASK_SSE1, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, EQ, 0 },
12437   { MASK_SSE1, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, LT, 0 },
12438   { MASK_SSE1, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, LE, 0 },
12439   { MASK_SSE1, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, UNORDERED, 0 },
12440
12441   { MASK_SSE1, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, 0, 0 },
12442   { MASK_SSE1, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, 0, 0 },
12443   { MASK_SSE1, CODE_FOR_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, 0, 0 },
12444   { MASK_SSE1, CODE_FOR_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, 0, 0 },
12445
12446   { MASK_SSE1, CODE_FOR_sse_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, 0, 0 },
12447   { MASK_SSE1, CODE_FOR_sse_nandv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, 0, 0 },
12448   { MASK_SSE1, CODE_FOR_sse_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, 0, 0 },
12449   { MASK_SSE1, CODE_FOR_sse_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, 0, 0 },
12450
12451   { MASK_SSE1, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, 0, 0 },
12452   { MASK_SSE1, CODE_FOR_sse_movhlps,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, 0, 0 },
12453   { MASK_SSE1, CODE_FOR_sse_movlhps,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, 0, 0 },
12454   { MASK_SSE1, CODE_FOR_sse_unpckhps, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, 0, 0 },
12455   { MASK_SSE1, CODE_FOR_sse_unpcklps, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, 0, 0 },
12456
12457   /* MMX */
12458   { MASK_MMX, CODE_FOR_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, 0, 0 },
12459   { MASK_MMX, CODE_FOR_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, 0, 0 },
12460   { MASK_MMX, CODE_FOR_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, 0, 0 },
12461   { MASK_MMX, CODE_FOR_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, 0, 0 },
12462   { MASK_MMX, CODE_FOR_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, 0, 0 },
12463   { MASK_MMX, CODE_FOR_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, 0, 0 },
12464
12465   { MASK_MMX, CODE_FOR_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, 0, 0 },
12466   { MASK_MMX, CODE_FOR_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, 0, 0 },
12467   { MASK_MMX, CODE_FOR_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, 0, 0 },
12468   { MASK_MMX, CODE_FOR_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, 0, 0 },
12469   { MASK_MMX, CODE_FOR_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, 0, 0 },
12470   { MASK_MMX, CODE_FOR_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, 0, 0 },
12471   { MASK_MMX, CODE_FOR_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, 0, 0 },
12472   { MASK_MMX, CODE_FOR_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, 0, 0 },
12473
12474   { MASK_MMX, CODE_FOR_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, 0, 0 },
12475   { MASK_MMX, CODE_FOR_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, 0, 0 },
12476   { MASK_SSE1 | MASK_3DNOW_A, CODE_FOR_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, 0, 0 },
12477
12478   { MASK_MMX, CODE_FOR_mmx_anddi3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, 0, 0 },
12479   { MASK_MMX, CODE_FOR_mmx_nanddi3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, 0, 0 },
12480   { MASK_MMX, CODE_FOR_mmx_iordi3, "__builtin_ia32_por", IX86_BUILTIN_POR, 0, 0 },
12481   { MASK_MMX, CODE_FOR_mmx_xordi3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, 0, 0 },
12482
12483   { MASK_SSE1 | MASK_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, 0, 0 },
12484   { MASK_SSE1 | MASK_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, 0, 0 },
12485
12486   { MASK_MMX, CODE_FOR_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, 0, 0 },
12487   { MASK_MMX, CODE_FOR_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, 0, 0 },
12488   { MASK_MMX, CODE_FOR_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, 0, 0 },
12489   { MASK_MMX, CODE_FOR_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, 0, 0 },
12490   { MASK_MMX, CODE_FOR_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, 0, 0 },
12491   { MASK_MMX, CODE_FOR_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, 0, 0 },
12492
12493   { MASK_SSE1 | MASK_3DNOW_A, CODE_FOR_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, 0, 0 },
12494   { MASK_SSE1 | MASK_3DNOW_A, CODE_FOR_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, 0, 0 },
12495   { MASK_SSE1 | MASK_3DNOW_A, CODE_FOR_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, 0, 0 },
12496   { MASK_SSE1 | MASK_3DNOW_A, CODE_FOR_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, 0, 0 },
12497
12498   { MASK_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, 0, 0 },
12499   { MASK_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, 0, 0 },
12500   { MASK_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, 0, 0 },
12501   { MASK_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, 0, 0 },
12502   { MASK_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, 0, 0 },
12503   { MASK_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, 0, 0 },
12504
12505   /* Special.  */
12506   { MASK_MMX, CODE_FOR_mmx_packsswb, 0, IX86_BUILTIN_PACKSSWB, 0, 0 },
12507   { MASK_MMX, CODE_FOR_mmx_packssdw, 0, IX86_BUILTIN_PACKSSDW, 0, 0 },
12508   { MASK_MMX, CODE_FOR_mmx_packuswb, 0, IX86_BUILTIN_PACKUSWB, 0, 0 },
12509
12510   { MASK_SSE1, CODE_FOR_cvtpi2ps, 0, IX86_BUILTIN_CVTPI2PS, 0, 0 },
12511   { MASK_SSE1, CODE_FOR_cvtsi2ss, 0, IX86_BUILTIN_CVTSI2SS, 0, 0 },
12512
12513   { MASK_MMX, CODE_FOR_ashlv4hi3, 0, IX86_BUILTIN_PSLLW, 0, 0 },
12514   { MASK_MMX, CODE_FOR_ashlv4hi3, 0, IX86_BUILTIN_PSLLWI, 0, 0 },
12515   { MASK_MMX, CODE_FOR_ashlv2si3, 0, IX86_BUILTIN_PSLLD, 0, 0 },
12516   { MASK_MMX, CODE_FOR_ashlv2si3, 0, IX86_BUILTIN_PSLLDI, 0, 0 },
12517   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQ, 0, 0 },
12518   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQI, 0, 0 },
12519
12520   { MASK_MMX, CODE_FOR_lshrv4hi3, 0, IX86_BUILTIN_PSRLW, 0, 0 },
12521   { MASK_MMX, CODE_FOR_lshrv4hi3, 0, IX86_BUILTIN_PSRLWI, 0, 0 },
12522   { MASK_MMX, CODE_FOR_lshrv2si3, 0, IX86_BUILTIN_PSRLD, 0, 0 },
12523   { MASK_MMX, CODE_FOR_lshrv2si3, 0, IX86_BUILTIN_PSRLDI, 0, 0 },
12524   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQ, 0, 0 },
12525   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQI, 0, 0 },
12526
12527   { MASK_MMX, CODE_FOR_ashrv4hi3, 0, IX86_BUILTIN_PSRAW, 0, 0 },
12528   { MASK_MMX, CODE_FOR_ashrv4hi3, 0, IX86_BUILTIN_PSRAWI, 0, 0 },
12529   { MASK_MMX, CODE_FOR_ashrv2si3, 0, IX86_BUILTIN_PSRAD, 0, 0 },
12530   { MASK_MMX, CODE_FOR_ashrv2si3, 0, IX86_BUILTIN_PSRADI, 0, 0 },
12531
12532   { MASK_SSE1 | MASK_3DNOW_A, CODE_FOR_mmx_psadbw, 0, IX86_BUILTIN_PSADBW, 0, 0 },
12533   { MASK_MMX, CODE_FOR_mmx_pmaddwd, 0, IX86_BUILTIN_PMADDWD, 0, 0 },
12534
12535   /* SSE2 */
12536   { MASK_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, 0, 0 },
12537   { MASK_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, 0, 0 },
12538   { MASK_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, 0, 0 },
12539   { MASK_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, 0, 0 },
12540   { MASK_SSE2, CODE_FOR_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, 0, 0 },
12541   { MASK_SSE2, CODE_FOR_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, 0, 0 },
12542   { MASK_SSE2, CODE_FOR_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, 0, 0 },
12543   { MASK_SSE2, CODE_FOR_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, 0, 0 },
12544
12545   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, 0 },
12546   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, 0 },
12547   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, 0 },
12548   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT, 1 },
12549   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE, 1 },
12550   { MASK_SSE2, CODE_FOR_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, 0 },
12551   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, EQ, 0 },
12552   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, LT, 0 },
12553   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, LE, 0 },
12554   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, LT, 1 },
12555   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, LE, 1 },
12556   { MASK_SSE2, CODE_FOR_maskncmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, UNORDERED, 0 },
12557   { MASK_SSE2, CODE_FOR_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, 0 },
12558   { MASK_SSE2, CODE_FOR_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, 0 },
12559   { MASK_SSE2, CODE_FOR_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, 0 },
12560   { MASK_SSE2, CODE_FOR_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, 0 },
12561   { MASK_SSE2, CODE_FOR_vmmaskncmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, EQ, 0 },
12562   { MASK_SSE2, CODE_FOR_vmmaskncmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, LT, 0 },
12563   { MASK_SSE2, CODE_FOR_vmmaskncmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, LE, 0 },
12564   { MASK_SSE2, CODE_FOR_vmmaskncmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, UNORDERED, 0 },
12565
12566   { MASK_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, 0, 0 },
12567   { MASK_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, 0, 0 },
12568   { MASK_SSE2, CODE_FOR_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, 0, 0 },
12569   { MASK_SSE2, CODE_FOR_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, 0, 0 },
12570
12571   { MASK_SSE2, CODE_FOR_sse2_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, 0, 0 },
12572   { MASK_SSE2, CODE_FOR_sse2_nandv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, 0, 0 },
12573   { MASK_SSE2, CODE_FOR_sse2_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, 0, 0 },
12574   { MASK_SSE2, CODE_FOR_sse2_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, 0, 0 },
12575
12576   { MASK_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, 0, 0 },
12577   { MASK_SSE2, CODE_FOR_sse2_unpckhpd, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, 0, 0 },
12578   { MASK_SSE2, CODE_FOR_sse2_unpcklpd, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, 0, 0 },
12579
12580   /* SSE2 MMX */
12581   { MASK_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, 0, 0 },
12582   { MASK_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, 0, 0 },
12583   { MASK_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, 0, 0 },
12584   { MASK_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, 0, 0 },
12585   { MASK_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, 0, 0 },
12586   { MASK_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, 0, 0 },
12587   { MASK_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, 0, 0 },
12588   { MASK_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, 0, 0 },
12589
12590   { MASK_MMX, CODE_FOR_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, 0, 0 },
12591   { MASK_MMX, CODE_FOR_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, 0, 0 },
12592   { MASK_MMX, CODE_FOR_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, 0, 0 },
12593   { MASK_MMX, CODE_FOR_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, 0, 0 },
12594   { MASK_MMX, CODE_FOR_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, 0, 0 },
12595   { MASK_MMX, CODE_FOR_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, 0, 0 },
12596   { MASK_MMX, CODE_FOR_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, 0, 0 },
12597   { MASK_MMX, CODE_FOR_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, 0, 0 },
12598
12599   { MASK_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, 0, 0 },
12600   { MASK_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, 0, 0 },
12601   { MASK_SSE2, CODE_FOR_sse2_umulsidi3, "__builtin_ia32_pmuludq", IX86_BUILTIN_PMULUDQ, 0, 0 },
12602   { MASK_SSE2, CODE_FOR_sse2_umulv2siv2di3, "__builtin_ia32_pmuludq128", IX86_BUILTIN_PMULUDQ128, 0, 0 },
12603
12604   { MASK_SSE2, CODE_FOR_sse2_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, 0, 0 },
12605   { MASK_SSE2, CODE_FOR_sse2_nandv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, 0, 0 },
12606   { MASK_SSE2, CODE_FOR_sse2_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, 0, 0 },
12607   { MASK_SSE2, CODE_FOR_sse2_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, 0, 0 },
12608
12609   { MASK_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, 0, 0 },
12610   { MASK_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, 0, 0 },
12611
12612   { MASK_SSE2, CODE_FOR_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, 0, 0 },
12613   { MASK_SSE2, CODE_FOR_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, 0, 0 },
12614   { MASK_SSE2, CODE_FOR_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, 0, 0 },
12615   { MASK_SSE2, CODE_FOR_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, 0, 0 },
12616   { MASK_SSE2, CODE_FOR_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, 0, 0 },
12617   { MASK_SSE2, CODE_FOR_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, 0, 0 },
12618
12619   { MASK_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, 0, 0 },
12620   { MASK_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, 0, 0 },
12621   { MASK_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, 0, 0 },
12622   { MASK_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, 0, 0 },
12623
12624   { MASK_SSE2, CODE_FOR_sse2_punpckhbw, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, 0, 0 },
12625   { MASK_SSE2, CODE_FOR_sse2_punpckhwd, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, 0, 0 },
12626   { MASK_SSE2, CODE_FOR_sse2_punpckhdq, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, 0, 0 },
12627   { MASK_SSE2, CODE_FOR_sse2_punpckhqdq, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, 0, 0 },
12628   { MASK_SSE2, CODE_FOR_sse2_punpcklbw, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, 0, 0 },
12629   { MASK_SSE2, CODE_FOR_sse2_punpcklwd, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, 0, 0 },
12630   { MASK_SSE2, CODE_FOR_sse2_punpckldq, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, 0, 0 },
12631   { MASK_SSE2, CODE_FOR_sse2_punpcklqdq, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, 0, 0 },
12632
12633   { MASK_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, 0, 0 },
12634   { MASK_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, 0, 0 },
12635   { MASK_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, 0, 0 },
12636
12637   { MASK_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, 0, 0 },
12638   { MASK_SSE2, CODE_FOR_sse2_psadbw, 0, IX86_BUILTIN_PSADBW128, 0, 0 },
12639
12640   { MASK_SSE2, CODE_FOR_ashlv8hi3_ti, 0, IX86_BUILTIN_PSLLW128, 0, 0 },
12641   { MASK_SSE2, CODE_FOR_ashlv8hi3, 0, IX86_BUILTIN_PSLLWI128, 0, 0 },
12642   { MASK_SSE2, CODE_FOR_ashlv4si3_ti, 0, IX86_BUILTIN_PSLLD128, 0, 0 },
12643   { MASK_SSE2, CODE_FOR_ashlv4si3, 0, IX86_BUILTIN_PSLLDI128, 0, 0 },
12644   { MASK_SSE2, CODE_FOR_ashlv2di3_ti, 0, IX86_BUILTIN_PSLLQ128, 0, 0 },
12645   { MASK_SSE2, CODE_FOR_ashlv2di3, 0, IX86_BUILTIN_PSLLQI128, 0, 0 },
12646
12647   { MASK_SSE2, CODE_FOR_lshrv8hi3_ti, 0, IX86_BUILTIN_PSRLW128, 0, 0 },
12648   { MASK_SSE2, CODE_FOR_lshrv8hi3, 0, IX86_BUILTIN_PSRLWI128, 0, 0 },
12649   { MASK_SSE2, CODE_FOR_lshrv4si3_ti, 0, IX86_BUILTIN_PSRLD128, 0, 0 },
12650   { MASK_SSE2, CODE_FOR_lshrv4si3, 0, IX86_BUILTIN_PSRLDI128, 0, 0 },
12651   { MASK_SSE2, CODE_FOR_lshrv2di3_ti, 0, IX86_BUILTIN_PSRLQ128, 0, 0 },
12652   { MASK_SSE2, CODE_FOR_lshrv2di3, 0, IX86_BUILTIN_PSRLQI128, 0, 0 },
12653
12654   { MASK_SSE2, CODE_FOR_ashrv8hi3_ti, 0, IX86_BUILTIN_PSRAW128, 0, 0 },
12655   { MASK_SSE2, CODE_FOR_ashrv8hi3, 0, IX86_BUILTIN_PSRAWI128, 0, 0 },
12656   { MASK_SSE2, CODE_FOR_ashrv4si3_ti, 0, IX86_BUILTIN_PSRAD128, 0, 0 },
12657   { MASK_SSE2, CODE_FOR_ashrv4si3, 0, IX86_BUILTIN_PSRADI128, 0, 0 },
12658
12659   { MASK_SSE2, CODE_FOR_sse2_pmaddwd, 0, IX86_BUILTIN_PMADDWD128, 0, 0 },
12660
12661   { MASK_SSE2, CODE_FOR_cvtsi2sd, 0, IX86_BUILTIN_CVTSI2SD, 0, 0 },
12662   { MASK_SSE2, CODE_FOR_cvtsd2ss, 0, IX86_BUILTIN_CVTSD2SS, 0, 0 },
12663   { MASK_SSE2, CODE_FOR_cvtss2sd, 0, IX86_BUILTIN_CVTSS2SD, 0, 0 }
12664 };
12665
12666 static const struct builtin_description bdesc_1arg[] =
12667 {
12668   { MASK_SSE1 | MASK_3DNOW_A, CODE_FOR_mmx_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB, 0, 0 },
12669   { MASK_SSE1, CODE_FOR_sse_movmskps, 0, IX86_BUILTIN_MOVMSKPS, 0, 0 },
12670
12671   { MASK_SSE1, CODE_FOR_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS, 0, 0 },
12672   { MASK_SSE1, CODE_FOR_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS, 0, 0 },
12673   { MASK_SSE1, CODE_FOR_rcpv4sf2, 0, IX86_BUILTIN_RCPPS, 0, 0 },
12674
12675   { MASK_SSE1, CODE_FOR_cvtps2pi, 0, IX86_BUILTIN_CVTPS2PI, 0, 0 },
12676   { MASK_SSE1, CODE_FOR_cvtss2si, 0, IX86_BUILTIN_CVTSS2SI, 0, 0 },
12677   { MASK_SSE1, CODE_FOR_cvttps2pi, 0, IX86_BUILTIN_CVTTPS2PI, 0, 0 },
12678   { MASK_SSE1, CODE_FOR_cvttss2si, 0, IX86_BUILTIN_CVTTSS2SI, 0, 0 },
12679
12680   { MASK_SSE2, CODE_FOR_sse2_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB128, 0, 0 },
12681   { MASK_SSE2, CODE_FOR_sse2_movmskpd, 0, IX86_BUILTIN_MOVMSKPD, 0, 0 },
12682   { MASK_SSE2, CODE_FOR_sse2_movq2dq, 0, IX86_BUILTIN_MOVQ2DQ, 0, 0 },
12683   { MASK_SSE2, CODE_FOR_sse2_movdq2q, 0, IX86_BUILTIN_MOVDQ2Q, 0, 0 },
12684
12685   { MASK_SSE2, CODE_FOR_sqrtv2df2, 0, IX86_BUILTIN_SQRTPD, 0, 0 },
12686
12687   { MASK_SSE2, CODE_FOR_cvtdq2pd, 0, IX86_BUILTIN_CVTDQ2PD, 0, 0 },
12688   { MASK_SSE2, CODE_FOR_cvtdq2ps, 0, IX86_BUILTIN_CVTDQ2PS, 0, 0 },
12689
12690   { MASK_SSE2, CODE_FOR_cvtpd2dq, 0, IX86_BUILTIN_CVTPD2DQ, 0, 0 },
12691   { MASK_SSE2, CODE_FOR_cvtpd2pi, 0, IX86_BUILTIN_CVTPD2PI, 0, 0 },
12692   { MASK_SSE2, CODE_FOR_cvtpd2ps, 0, IX86_BUILTIN_CVTPD2PS, 0, 0 },
12693   { MASK_SSE2, CODE_FOR_cvttpd2dq, 0, IX86_BUILTIN_CVTTPD2DQ, 0, 0 },
12694   { MASK_SSE2, CODE_FOR_cvttpd2pi, 0, IX86_BUILTIN_CVTTPD2PI, 0, 0 },
12695
12696   { MASK_SSE2, CODE_FOR_cvtpi2pd, 0, IX86_BUILTIN_CVTPI2PD, 0, 0 },
12697
12698   { MASK_SSE2, CODE_FOR_cvtsd2si, 0, IX86_BUILTIN_CVTSD2SI, 0, 0 },
12699   { MASK_SSE2, CODE_FOR_cvttsd2si, 0, IX86_BUILTIN_CVTTSD2SI, 0, 0 },
12700
12701   { MASK_SSE2, CODE_FOR_cvtps2dq, 0, IX86_BUILTIN_CVTPS2DQ, 0, 0 },
12702   { MASK_SSE2, CODE_FOR_cvtps2pd, 0, IX86_BUILTIN_CVTPS2PD, 0, 0 },
12703   { MASK_SSE2, CODE_FOR_cvttps2dq, 0, IX86_BUILTIN_CVTTPS2DQ, 0, 0 },
12704
12705   { MASK_SSE2, CODE_FOR_sse2_movq, 0, IX86_BUILTIN_MOVQ, 0, 0 }
12706 };
12707
12708 void
12709 ix86_init_builtins ()
12710 {
12711   if (TARGET_MMX)
12712     ix86_init_mmx_sse_builtins ();
12713 }
12714
12715 /* Set up all the MMX/SSE builtins.  This is not called if TARGET_MMX
12716    is zero.  Otherwise, if TARGET_SSE is not set, only expand the MMX
12717    builtins.  */
12718 static void
12719 ix86_init_mmx_sse_builtins ()
12720 {
12721   const struct builtin_description * d;
12722   size_t i;
12723
12724   tree pchar_type_node = build_pointer_type (char_type_node);
12725   tree pcchar_type_node = build_pointer_type (
12726                              build_type_variant (char_type_node, 1, 0));
12727   tree pfloat_type_node = build_pointer_type (float_type_node);
12728   tree pcfloat_type_node = build_pointer_type (
12729                              build_type_variant (float_type_node, 1, 0));
12730   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
12731   tree pv2di_type_node = build_pointer_type (V2DI_type_node);
12732   tree pdi_type_node = build_pointer_type (long_long_unsigned_type_node);
12733
12734   /* Comparisons.  */
12735   tree int_ftype_v4sf_v4sf
12736     = build_function_type_list (integer_type_node,
12737                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
12738   tree v4si_ftype_v4sf_v4sf
12739     = build_function_type_list (V4SI_type_node,
12740                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
12741   /* MMX/SSE/integer conversions.  */
12742   tree int_ftype_v4sf
12743     = build_function_type_list (integer_type_node,
12744                                 V4SF_type_node, NULL_TREE);
12745   tree int_ftype_v8qi
12746     = build_function_type_list (integer_type_node, V8QI_type_node, NULL_TREE);
12747   tree v4sf_ftype_v4sf_int
12748     = build_function_type_list (V4SF_type_node,
12749                                 V4SF_type_node, integer_type_node, NULL_TREE);
12750   tree v4sf_ftype_v4sf_v2si
12751     = build_function_type_list (V4SF_type_node,
12752                                 V4SF_type_node, V2SI_type_node, NULL_TREE);
12753   tree int_ftype_v4hi_int
12754     = build_function_type_list (integer_type_node,
12755                                 V4HI_type_node, integer_type_node, NULL_TREE);
12756   tree v4hi_ftype_v4hi_int_int
12757     = build_function_type_list (V4HI_type_node, V4HI_type_node,
12758                                 integer_type_node, integer_type_node,
12759                                 NULL_TREE);
12760   /* Miscellaneous.  */
12761   tree v8qi_ftype_v4hi_v4hi
12762     = build_function_type_list (V8QI_type_node,
12763                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
12764   tree v4hi_ftype_v2si_v2si
12765     = build_function_type_list (V4HI_type_node,
12766                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
12767   tree v4sf_ftype_v4sf_v4sf_int
12768     = build_function_type_list (V4SF_type_node,
12769                                 V4SF_type_node, V4SF_type_node,
12770                                 integer_type_node, NULL_TREE);
12771   tree v2si_ftype_v4hi_v4hi
12772     = build_function_type_list (V2SI_type_node,
12773                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
12774   tree v4hi_ftype_v4hi_int
12775     = build_function_type_list (V4HI_type_node,
12776                                 V4HI_type_node, integer_type_node, NULL_TREE);
12777   tree v4hi_ftype_v4hi_di
12778     = build_function_type_list (V4HI_type_node,
12779                                 V4HI_type_node, long_long_unsigned_type_node,
12780                                 NULL_TREE);
12781   tree v2si_ftype_v2si_di
12782     = build_function_type_list (V2SI_type_node,
12783                                 V2SI_type_node, long_long_unsigned_type_node,
12784                                 NULL_TREE);
12785   tree void_ftype_void
12786     = build_function_type (void_type_node, void_list_node);
12787   tree void_ftype_unsigned
12788     = build_function_type_list (void_type_node, unsigned_type_node, NULL_TREE);
12789   tree unsigned_ftype_void
12790     = build_function_type (unsigned_type_node, void_list_node);
12791   tree di_ftype_void
12792     = build_function_type (long_long_unsigned_type_node, void_list_node);
12793   tree v4sf_ftype_void
12794     = build_function_type (V4SF_type_node, void_list_node);
12795   tree v2si_ftype_v4sf
12796     = build_function_type_list (V2SI_type_node, V4SF_type_node, NULL_TREE);
12797   /* Loads/stores.  */
12798   tree void_ftype_v8qi_v8qi_pchar
12799     = build_function_type_list (void_type_node,
12800                                 V8QI_type_node, V8QI_type_node,
12801                                 pchar_type_node, NULL_TREE);
12802   tree v4sf_ftype_pcfloat
12803     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
12804   /* @@@ the type is bogus */
12805   tree v4sf_ftype_v4sf_pv2si
12806     = build_function_type_list (V4SF_type_node,
12807                                 V4SF_type_node, pv2si_type_node, NULL_TREE);
12808   tree void_ftype_pv2si_v4sf
12809     = build_function_type_list (void_type_node,
12810                                 pv2si_type_node, V4SF_type_node, NULL_TREE);
12811   tree void_ftype_pfloat_v4sf
12812     = build_function_type_list (void_type_node,
12813                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
12814   tree void_ftype_pdi_di
12815     = build_function_type_list (void_type_node,
12816                                 pdi_type_node, long_long_unsigned_type_node,
12817                                 NULL_TREE);
12818   tree void_ftype_pv2di_v2di
12819     = build_function_type_list (void_type_node,
12820                                 pv2di_type_node, V2DI_type_node, NULL_TREE);
12821   /* Normal vector unops.  */
12822   tree v4sf_ftype_v4sf
12823     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
12824
12825   /* Normal vector binops.  */
12826   tree v4sf_ftype_v4sf_v4sf
12827     = build_function_type_list (V4SF_type_node,
12828                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
12829   tree v8qi_ftype_v8qi_v8qi
12830     = build_function_type_list (V8QI_type_node,
12831                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
12832   tree v4hi_ftype_v4hi_v4hi
12833     = build_function_type_list (V4HI_type_node,
12834                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
12835   tree v2si_ftype_v2si_v2si
12836     = build_function_type_list (V2SI_type_node,
12837                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
12838   tree di_ftype_di_di
12839     = build_function_type_list (long_long_unsigned_type_node,
12840                                 long_long_unsigned_type_node,
12841                                 long_long_unsigned_type_node, NULL_TREE);
12842
12843   tree v2si_ftype_v2sf
12844     = build_function_type_list (V2SI_type_node, V2SF_type_node, NULL_TREE);
12845   tree v2sf_ftype_v2si
12846     = build_function_type_list (V2SF_type_node, V2SI_type_node, NULL_TREE);
12847   tree v2si_ftype_v2si
12848     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
12849   tree v2sf_ftype_v2sf
12850     = build_function_type_list (V2SF_type_node, V2SF_type_node, NULL_TREE);
12851   tree v2sf_ftype_v2sf_v2sf
12852     = build_function_type_list (V2SF_type_node,
12853                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
12854   tree v2si_ftype_v2sf_v2sf
12855     = build_function_type_list (V2SI_type_node,
12856                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
12857   tree pint_type_node    = build_pointer_type (integer_type_node);
12858   tree pcint_type_node = build_pointer_type (
12859                              build_type_variant (integer_type_node, 1, 0));
12860   tree pdouble_type_node = build_pointer_type (double_type_node);
12861   tree pcdouble_type_node = build_pointer_type (
12862                                 build_type_variant (double_type_node, 1, 0));
12863   tree int_ftype_v2df_v2df
12864     = build_function_type_list (integer_type_node,
12865                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
12866
12867   tree ti_ftype_void
12868     = build_function_type (intTI_type_node, void_list_node);
12869   tree v2di_ftype_void
12870     = build_function_type (V2DI_type_node, void_list_node);
12871   tree ti_ftype_ti_ti
12872     = build_function_type_list (intTI_type_node,
12873                                 intTI_type_node, intTI_type_node, NULL_TREE);
12874   tree void_ftype_pcvoid
12875     = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE);
12876   tree v2di_ftype_di
12877     = build_function_type_list (V2DI_type_node,
12878                                 long_long_unsigned_type_node, NULL_TREE);
12879   tree di_ftype_v2di
12880     = build_function_type_list (long_long_unsigned_type_node,
12881                                 V2DI_type_node, NULL_TREE);
12882   tree v4sf_ftype_v4si
12883     = build_function_type_list (V4SF_type_node, V4SI_type_node, NULL_TREE);
12884   tree v4si_ftype_v4sf
12885     = build_function_type_list (V4SI_type_node, V4SF_type_node, NULL_TREE);
12886   tree v2df_ftype_v4si
12887     = build_function_type_list (V2DF_type_node, V4SI_type_node, NULL_TREE);
12888   tree v4si_ftype_v2df
12889     = build_function_type_list (V4SI_type_node, V2DF_type_node, NULL_TREE);
12890   tree v2si_ftype_v2df
12891     = build_function_type_list (V2SI_type_node, V2DF_type_node, NULL_TREE);
12892   tree v4sf_ftype_v2df
12893     = build_function_type_list (V4SF_type_node, V2DF_type_node, NULL_TREE);
12894   tree v2df_ftype_v2si
12895     = build_function_type_list (V2DF_type_node, V2SI_type_node, NULL_TREE);
12896   tree v2df_ftype_v4sf
12897     = build_function_type_list (V2DF_type_node, V4SF_type_node, NULL_TREE);
12898   tree int_ftype_v2df
12899     = build_function_type_list (integer_type_node, V2DF_type_node, NULL_TREE);
12900   tree v2df_ftype_v2df_int
12901     = build_function_type_list (V2DF_type_node,
12902                                 V2DF_type_node, integer_type_node, NULL_TREE);
12903   tree v4sf_ftype_v4sf_v2df
12904     = build_function_type_list (V4SF_type_node,
12905                                 V4SF_type_node, V2DF_type_node, NULL_TREE);
12906   tree v2df_ftype_v2df_v4sf
12907     = build_function_type_list (V2DF_type_node,
12908                                 V2DF_type_node, V4SF_type_node, NULL_TREE);
12909   tree v2df_ftype_v2df_v2df_int
12910     = build_function_type_list (V2DF_type_node,
12911                                 V2DF_type_node, V2DF_type_node,
12912                                 integer_type_node,
12913                                 NULL_TREE);
12914   tree v2df_ftype_v2df_pv2si
12915     = build_function_type_list (V2DF_type_node,
12916                                 V2DF_type_node, pv2si_type_node, NULL_TREE);
12917   tree void_ftype_pv2si_v2df
12918     = build_function_type_list (void_type_node,
12919                                 pv2si_type_node, V2DF_type_node, NULL_TREE);
12920   tree void_ftype_pdouble_v2df
12921     = build_function_type_list (void_type_node,
12922                                 pdouble_type_node, V2DF_type_node, NULL_TREE);
12923   tree void_ftype_pint_int
12924     = build_function_type_list (void_type_node,
12925                                 pint_type_node, integer_type_node, NULL_TREE);
12926   tree void_ftype_v16qi_v16qi_pchar
12927     = build_function_type_list (void_type_node,
12928                                 V16QI_type_node, V16QI_type_node,
12929                                 pchar_type_node, NULL_TREE);
12930   tree v2df_ftype_pcdouble
12931     = build_function_type_list (V2DF_type_node, pcdouble_type_node, NULL_TREE);
12932   tree v2df_ftype_v2df_v2df
12933     = build_function_type_list (V2DF_type_node,
12934                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
12935   tree v16qi_ftype_v16qi_v16qi
12936     = build_function_type_list (V16QI_type_node,
12937                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
12938   tree v8hi_ftype_v8hi_v8hi
12939     = build_function_type_list (V8HI_type_node,
12940                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
12941   tree v4si_ftype_v4si_v4si
12942     = build_function_type_list (V4SI_type_node,
12943                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
12944   tree v2di_ftype_v2di_v2di
12945     = build_function_type_list (V2DI_type_node,
12946                                 V2DI_type_node, V2DI_type_node, NULL_TREE);
12947   tree v2di_ftype_v2df_v2df
12948     = build_function_type_list (V2DI_type_node,
12949                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
12950   tree v2df_ftype_v2df
12951     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
12952   tree v2df_ftype_double
12953     = build_function_type_list (V2DF_type_node, double_type_node, NULL_TREE);
12954   tree v2df_ftype_double_double
12955     = build_function_type_list (V2DF_type_node,
12956                                 double_type_node, double_type_node, NULL_TREE);
12957   tree int_ftype_v8hi_int
12958     = build_function_type_list (integer_type_node,
12959                                 V8HI_type_node, integer_type_node, NULL_TREE);
12960   tree v8hi_ftype_v8hi_int_int
12961     = build_function_type_list (V8HI_type_node,
12962                                 V8HI_type_node, integer_type_node,
12963                                 integer_type_node, NULL_TREE);
12964   tree v2di_ftype_v2di_int
12965     = build_function_type_list (V2DI_type_node,
12966                                 V2DI_type_node, integer_type_node, NULL_TREE);
12967   tree v4si_ftype_v4si_int
12968     = build_function_type_list (V4SI_type_node,
12969                                 V4SI_type_node, integer_type_node, NULL_TREE);
12970   tree v8hi_ftype_v8hi_int
12971     = build_function_type_list (V8HI_type_node,
12972                                 V8HI_type_node, integer_type_node, NULL_TREE);
12973   tree v8hi_ftype_v8hi_v2di
12974     = build_function_type_list (V8HI_type_node,
12975                                 V8HI_type_node, V2DI_type_node, NULL_TREE);
12976   tree v4si_ftype_v4si_v2di
12977     = build_function_type_list (V4SI_type_node,
12978                                 V4SI_type_node, V2DI_type_node, NULL_TREE);
12979   tree v4si_ftype_v8hi_v8hi
12980     = build_function_type_list (V4SI_type_node,
12981                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
12982   tree di_ftype_v8qi_v8qi
12983     = build_function_type_list (long_long_unsigned_type_node,
12984                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
12985   tree v2di_ftype_v16qi_v16qi
12986     = build_function_type_list (V2DI_type_node,
12987                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
12988   tree int_ftype_v16qi
12989     = build_function_type_list (integer_type_node, V16QI_type_node, NULL_TREE);
12990   tree v16qi_ftype_pcchar
12991     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
12992   tree void_ftype_pchar_v16qi
12993     = build_function_type_list (void_type_node,
12994                                 pchar_type_node, V16QI_type_node, NULL_TREE);
12995   tree v4si_ftype_pcint
12996     = build_function_type_list (V4SI_type_node, pcint_type_node, NULL_TREE);
12997   tree void_ftype_pcint_v4si
12998     = build_function_type_list (void_type_node,
12999                                 pcint_type_node, V4SI_type_node, NULL_TREE);
13000   tree v2di_ftype_v2di
13001     = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
13002
13003   /* Add all builtins that are more or less simple operations on two
13004      operands.  */
13005   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
13006     {
13007       /* Use one of the operands; the target can have a different mode for
13008          mask-generating compares.  */
13009       enum machine_mode mode;
13010       tree type;
13011
13012       if (d->name == 0)
13013         continue;
13014       mode = insn_data[d->icode].operand[1].mode;
13015
13016       switch (mode)
13017         {
13018         case V16QImode:
13019           type = v16qi_ftype_v16qi_v16qi;
13020           break;
13021         case V8HImode:
13022           type = v8hi_ftype_v8hi_v8hi;
13023           break;
13024         case V4SImode:
13025           type = v4si_ftype_v4si_v4si;
13026           break;
13027         case V2DImode:
13028           type = v2di_ftype_v2di_v2di;
13029           break;
13030         case V2DFmode:
13031           type = v2df_ftype_v2df_v2df;
13032           break;
13033         case TImode:
13034           type = ti_ftype_ti_ti;
13035           break;
13036         case V4SFmode:
13037           type = v4sf_ftype_v4sf_v4sf;
13038           break;
13039         case V8QImode:
13040           type = v8qi_ftype_v8qi_v8qi;
13041           break;
13042         case V4HImode:
13043           type = v4hi_ftype_v4hi_v4hi;
13044           break;
13045         case V2SImode:
13046           type = v2si_ftype_v2si_v2si;
13047           break;
13048         case DImode:
13049           type = di_ftype_di_di;
13050           break;
13051
13052         default:
13053           abort ();
13054         }
13055
13056       /* Override for comparisons.  */
13057       if (d->icode == CODE_FOR_maskcmpv4sf3
13058           || d->icode == CODE_FOR_maskncmpv4sf3
13059           || d->icode == CODE_FOR_vmmaskcmpv4sf3
13060           || d->icode == CODE_FOR_vmmaskncmpv4sf3)
13061         type = v4si_ftype_v4sf_v4sf;
13062
13063       if (d->icode == CODE_FOR_maskcmpv2df3
13064           || d->icode == CODE_FOR_maskncmpv2df3
13065           || d->icode == CODE_FOR_vmmaskcmpv2df3
13066           || d->icode == CODE_FOR_vmmaskncmpv2df3)
13067         type = v2di_ftype_v2df_v2df;
13068
13069       def_builtin (d->mask, d->name, type, d->code);
13070     }
13071
13072   /* Add the remaining MMX insns with somewhat more complicated types.  */
13073   def_builtin (MASK_MMX, "__builtin_ia32_mmx_zero", di_ftype_void, IX86_BUILTIN_MMX_ZERO);
13074   def_builtin (MASK_MMX, "__builtin_ia32_emms", void_ftype_void, IX86_BUILTIN_EMMS);
13075   def_builtin (MASK_MMX, "__builtin_ia32_psllw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSLLW);
13076   def_builtin (MASK_MMX, "__builtin_ia32_pslld", v2si_ftype_v2si_di, IX86_BUILTIN_PSLLD);
13077   def_builtin (MASK_MMX, "__builtin_ia32_psllq", di_ftype_di_di, IX86_BUILTIN_PSLLQ);
13078
13079   def_builtin (MASK_MMX, "__builtin_ia32_psrlw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRLW);
13080   def_builtin (MASK_MMX, "__builtin_ia32_psrld", v2si_ftype_v2si_di, IX86_BUILTIN_PSRLD);
13081   def_builtin (MASK_MMX, "__builtin_ia32_psrlq", di_ftype_di_di, IX86_BUILTIN_PSRLQ);
13082
13083   def_builtin (MASK_MMX, "__builtin_ia32_psraw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRAW);
13084   def_builtin (MASK_MMX, "__builtin_ia32_psrad", v2si_ftype_v2si_di, IX86_BUILTIN_PSRAD);
13085
13086   def_builtin (MASK_MMX, "__builtin_ia32_pshufw", v4hi_ftype_v4hi_int, IX86_BUILTIN_PSHUFW);
13087   def_builtin (MASK_MMX, "__builtin_ia32_pmaddwd", v2si_ftype_v4hi_v4hi, IX86_BUILTIN_PMADDWD);
13088
13089   /* comi/ucomi insns.  */
13090   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
13091     if (d->mask == MASK_SSE2)
13092       def_builtin (d->mask, d->name, int_ftype_v2df_v2df, d->code);
13093     else
13094       def_builtin (d->mask, d->name, int_ftype_v4sf_v4sf, d->code);
13095
13096   def_builtin (MASK_MMX, "__builtin_ia32_packsswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKSSWB);
13097   def_builtin (MASK_MMX, "__builtin_ia32_packssdw", v4hi_ftype_v2si_v2si, IX86_BUILTIN_PACKSSDW);
13098   def_builtin (MASK_MMX, "__builtin_ia32_packuswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKUSWB);
13099
13100   def_builtin (MASK_SSE1, "__builtin_ia32_ldmxcsr", void_ftype_unsigned, IX86_BUILTIN_LDMXCSR);
13101   def_builtin (MASK_SSE1, "__builtin_ia32_stmxcsr", unsigned_ftype_void, IX86_BUILTIN_STMXCSR);
13102   def_builtin (MASK_SSE1, "__builtin_ia32_cvtpi2ps", v4sf_ftype_v4sf_v2si, IX86_BUILTIN_CVTPI2PS);
13103   def_builtin (MASK_SSE1, "__builtin_ia32_cvtps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTPS2PI);
13104   def_builtin (MASK_SSE1, "__builtin_ia32_cvtsi2ss", v4sf_ftype_v4sf_int, IX86_BUILTIN_CVTSI2SS);
13105   def_builtin (MASK_SSE1, "__builtin_ia32_cvtss2si", int_ftype_v4sf, IX86_BUILTIN_CVTSS2SI);
13106   def_builtin (MASK_SSE1, "__builtin_ia32_cvttps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTTPS2PI);
13107   def_builtin (MASK_SSE1, "__builtin_ia32_cvttss2si", int_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI);
13108
13109   def_builtin (MASK_SSE1 | MASK_3DNOW_A, "__builtin_ia32_pextrw", int_ftype_v4hi_int, IX86_BUILTIN_PEXTRW);
13110   def_builtin (MASK_SSE1 | MASK_3DNOW_A, "__builtin_ia32_pinsrw", v4hi_ftype_v4hi_int_int, IX86_BUILTIN_PINSRW);
13111
13112   def_builtin (MASK_SSE1 | MASK_3DNOW_A, "__builtin_ia32_maskmovq", void_ftype_v8qi_v8qi_pchar, IX86_BUILTIN_MASKMOVQ);
13113
13114   def_builtin (MASK_SSE1, "__builtin_ia32_loadaps", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADAPS);
13115   def_builtin (MASK_SSE1, "__builtin_ia32_loadups", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADUPS);
13116   def_builtin (MASK_SSE1, "__builtin_ia32_loadss", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADSS);
13117   def_builtin (MASK_SSE1, "__builtin_ia32_storeaps", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREAPS);
13118   def_builtin (MASK_SSE1, "__builtin_ia32_storeups", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREUPS);
13119   def_builtin (MASK_SSE1, "__builtin_ia32_storess", void_ftype_pfloat_v4sf, IX86_BUILTIN_STORESS);
13120
13121   def_builtin (MASK_SSE1, "__builtin_ia32_loadhps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADHPS);
13122   def_builtin (MASK_SSE1, "__builtin_ia32_loadlps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADLPS);
13123   def_builtin (MASK_SSE1, "__builtin_ia32_storehps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STOREHPS);
13124   def_builtin (MASK_SSE1, "__builtin_ia32_storelps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STORELPS);
13125
13126   def_builtin (MASK_SSE1, "__builtin_ia32_movmskps", int_ftype_v4sf, IX86_BUILTIN_MOVMSKPS);
13127   def_builtin (MASK_SSE1 | MASK_3DNOW_A, "__builtin_ia32_pmovmskb", int_ftype_v8qi, IX86_BUILTIN_PMOVMSKB);
13128   def_builtin (MASK_SSE1, "__builtin_ia32_movntps", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTPS);
13129   def_builtin (MASK_SSE1 | MASK_3DNOW_A, "__builtin_ia32_movntq", void_ftype_pdi_di, IX86_BUILTIN_MOVNTQ);
13130
13131   def_builtin (MASK_SSE1 | MASK_3DNOW_A, "__builtin_ia32_sfence", void_ftype_void, IX86_BUILTIN_SFENCE);
13132
13133   def_builtin (MASK_SSE1 | MASK_3DNOW_A, "__builtin_ia32_psadbw", di_ftype_v8qi_v8qi, IX86_BUILTIN_PSADBW);
13134
13135   def_builtin (MASK_SSE1, "__builtin_ia32_rcpps", v4sf_ftype_v4sf, IX86_BUILTIN_RCPPS);
13136   def_builtin (MASK_SSE1, "__builtin_ia32_rcpss", v4sf_ftype_v4sf, IX86_BUILTIN_RCPSS);
13137   def_builtin (MASK_SSE1, "__builtin_ia32_rsqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS);
13138   def_builtin (MASK_SSE1, "__builtin_ia32_rsqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTSS);
13139   def_builtin (MASK_SSE1, "__builtin_ia32_sqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS);
13140   def_builtin (MASK_SSE1, "__builtin_ia32_sqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTSS);
13141
13142   def_builtin (MASK_SSE1, "__builtin_ia32_shufps", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_SHUFPS);
13143
13144   /* Original 3DNow!  */
13145   def_builtin (MASK_3DNOW, "__builtin_ia32_femms", void_ftype_void, IX86_BUILTIN_FEMMS);
13146   def_builtin (MASK_3DNOW, "__builtin_ia32_pavgusb", v8qi_ftype_v8qi_v8qi, IX86_BUILTIN_PAVGUSB);
13147   def_builtin (MASK_3DNOW, "__builtin_ia32_pf2id", v2si_ftype_v2sf, IX86_BUILTIN_PF2ID);
13148   def_builtin (MASK_3DNOW, "__builtin_ia32_pfacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFACC);
13149   def_builtin (MASK_3DNOW, "__builtin_ia32_pfadd", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFADD);
13150   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpeq", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPEQ);
13151   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpge", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGE);
13152   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpgt", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGT);
13153   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmax", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMAX);
13154   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmin", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMIN);
13155   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmul", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMUL);
13156   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcp", v2sf_ftype_v2sf, IX86_BUILTIN_PFRCP);
13157   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT1);
13158   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit2", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT2);
13159   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqrt", v2sf_ftype_v2sf, IX86_BUILTIN_PFRSQRT);
13160   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRSQIT1);
13161   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsub", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUB);
13162   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsubr", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUBR);
13163   def_builtin (MASK_3DNOW, "__builtin_ia32_pi2fd", v2sf_ftype_v2si, IX86_BUILTIN_PI2FD);
13164   def_builtin (MASK_3DNOW, "__builtin_ia32_pmulhrw", v4hi_ftype_v4hi_v4hi, IX86_BUILTIN_PMULHRW);
13165
13166   /* 3DNow! extension as used in the Athlon CPU.  */
13167   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pf2iw", v2si_ftype_v2sf, IX86_BUILTIN_PF2IW);
13168   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFNACC);
13169   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfpnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFPNACC);
13170   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pi2fw", v2sf_ftype_v2si, IX86_BUILTIN_PI2FW);
13171   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsf", v2sf_ftype_v2sf, IX86_BUILTIN_PSWAPDSF);
13172   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsi", v2si_ftype_v2si, IX86_BUILTIN_PSWAPDSI);
13173
13174   def_builtin (MASK_SSE1, "__builtin_ia32_setzerops", v4sf_ftype_void, IX86_BUILTIN_SSE_ZERO);
13175
13176   /* SSE2 */
13177   def_builtin (MASK_SSE2, "__builtin_ia32_pextrw128", int_ftype_v8hi_int, IX86_BUILTIN_PEXTRW128);
13178   def_builtin (MASK_SSE2, "__builtin_ia32_pinsrw128", v8hi_ftype_v8hi_int_int, IX86_BUILTIN_PINSRW128);
13179
13180   def_builtin (MASK_SSE2, "__builtin_ia32_maskmovdqu", void_ftype_v16qi_v16qi_pchar, IX86_BUILTIN_MASKMOVDQU);
13181   def_builtin (MASK_SSE2, "__builtin_ia32_movq2dq", v2di_ftype_di, IX86_BUILTIN_MOVQ2DQ);
13182   def_builtin (MASK_SSE2, "__builtin_ia32_movdq2q", di_ftype_v2di, IX86_BUILTIN_MOVDQ2Q);
13183
13184   def_builtin (MASK_SSE2, "__builtin_ia32_loadapd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADAPD);
13185   def_builtin (MASK_SSE2, "__builtin_ia32_loadupd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADUPD);
13186   def_builtin (MASK_SSE2, "__builtin_ia32_loadsd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADSD);
13187   def_builtin (MASK_SSE2, "__builtin_ia32_storeapd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREAPD);
13188   def_builtin (MASK_SSE2, "__builtin_ia32_storeupd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREUPD);
13189   def_builtin (MASK_SSE2, "__builtin_ia32_storesd", void_ftype_pdouble_v2df, IX86_BUILTIN_STORESD);
13190
13191   def_builtin (MASK_SSE2, "__builtin_ia32_loadhpd", v2df_ftype_v2df_pv2si, IX86_BUILTIN_LOADHPD);
13192   def_builtin (MASK_SSE2, "__builtin_ia32_loadlpd", v2df_ftype_v2df_pv2si, IX86_BUILTIN_LOADLPD);
13193   def_builtin (MASK_SSE2, "__builtin_ia32_storehpd", void_ftype_pv2si_v2df, IX86_BUILTIN_STOREHPD);
13194   def_builtin (MASK_SSE2, "__builtin_ia32_storelpd", void_ftype_pv2si_v2df, IX86_BUILTIN_STORELPD);
13195
13196   def_builtin (MASK_SSE2, "__builtin_ia32_movmskpd", int_ftype_v2df, IX86_BUILTIN_MOVMSKPD);
13197   def_builtin (MASK_SSE2, "__builtin_ia32_pmovmskb128", int_ftype_v16qi, IX86_BUILTIN_PMOVMSKB128);
13198   def_builtin (MASK_SSE2, "__builtin_ia32_movnti", void_ftype_pint_int, IX86_BUILTIN_MOVNTI);
13199   def_builtin (MASK_SSE2, "__builtin_ia32_movntpd", void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTPD);
13200   def_builtin (MASK_SSE2, "__builtin_ia32_movntdq", void_ftype_pv2di_v2di, IX86_BUILTIN_MOVNTDQ);
13201
13202   def_builtin (MASK_SSE2, "__builtin_ia32_pshufd", v4si_ftype_v4si_int, IX86_BUILTIN_PSHUFD);
13203   def_builtin (MASK_SSE2, "__builtin_ia32_pshuflw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFLW);
13204   def_builtin (MASK_SSE2, "__builtin_ia32_pshufhw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFHW);
13205   def_builtin (MASK_SSE2, "__builtin_ia32_psadbw128", v2di_ftype_v16qi_v16qi, IX86_BUILTIN_PSADBW128);
13206
13207   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtpd", v2df_ftype_v2df, IX86_BUILTIN_SQRTPD);
13208   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtsd", v2df_ftype_v2df, IX86_BUILTIN_SQRTSD);
13209
13210   def_builtin (MASK_SSE2, "__builtin_ia32_shufpd", v2df_ftype_v2df_v2df_int, IX86_BUILTIN_SHUFPD);
13211
13212   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2pd", v2df_ftype_v4si, IX86_BUILTIN_CVTDQ2PD);
13213   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2ps", v4sf_ftype_v4si, IX86_BUILTIN_CVTDQ2PS);
13214
13215   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTPD2DQ);
13216   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTPD2PI);
13217   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2ps", v4sf_ftype_v2df, IX86_BUILTIN_CVTPD2PS);
13218   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTTPD2DQ);
13219   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTTPD2PI);
13220
13221   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpi2pd", v2df_ftype_v2si, IX86_BUILTIN_CVTPI2PD);
13222
13223   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2si", int_ftype_v2df, IX86_BUILTIN_CVTSD2SI);
13224   def_builtin (MASK_SSE2, "__builtin_ia32_cvttsd2si", int_ftype_v2df, IX86_BUILTIN_CVTTSD2SI);
13225
13226   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTPS2DQ);
13227   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2pd", v2df_ftype_v4sf, IX86_BUILTIN_CVTPS2PD);
13228   def_builtin (MASK_SSE2, "__builtin_ia32_cvttps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTTPS2DQ);
13229
13230   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsi2sd", v2df_ftype_v2df_int, IX86_BUILTIN_CVTSI2SD);
13231   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2ss", v4sf_ftype_v4sf_v2df, IX86_BUILTIN_CVTSD2SS);
13232   def_builtin (MASK_SSE2, "__builtin_ia32_cvtss2sd", v2df_ftype_v2df_v4sf, IX86_BUILTIN_CVTSS2SD);
13233
13234   def_builtin (MASK_SSE2, "__builtin_ia32_setpd1", v2df_ftype_double, IX86_BUILTIN_SETPD1);
13235   def_builtin (MASK_SSE2, "__builtin_ia32_setpd", v2df_ftype_double_double, IX86_BUILTIN_SETPD);
13236   def_builtin (MASK_SSE2, "__builtin_ia32_setzeropd", ti_ftype_void, IX86_BUILTIN_CLRPD);
13237   def_builtin (MASK_SSE2, "__builtin_ia32_loadpd1", v2df_ftype_pcdouble, IX86_BUILTIN_LOADPD1);
13238   def_builtin (MASK_SSE2, "__builtin_ia32_loadrpd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADRPD);
13239   def_builtin (MASK_SSE2, "__builtin_ia32_storepd1", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREPD1);
13240   def_builtin (MASK_SSE2, "__builtin_ia32_storerpd", void_ftype_pdouble_v2df, IX86_BUILTIN_STORERPD);
13241
13242   def_builtin (MASK_SSE2, "__builtin_ia32_clflush", void_ftype_pcvoid, IX86_BUILTIN_CLFLUSH);
13243   def_builtin (MASK_SSE2, "__builtin_ia32_lfence", void_ftype_void, IX86_BUILTIN_LFENCE);
13244   def_builtin (MASK_SSE2, "__builtin_ia32_mfence", void_ftype_void, IX86_BUILTIN_MFENCE);
13245
13246   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqa", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQA);
13247   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqu", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQU);
13248   def_builtin (MASK_SSE2, "__builtin_ia32_loadd", v4si_ftype_pcint, IX86_BUILTIN_LOADD);
13249   def_builtin (MASK_SSE2, "__builtin_ia32_storedqa", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQA);
13250   def_builtin (MASK_SSE2, "__builtin_ia32_storedqu", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQU);
13251   def_builtin (MASK_SSE2, "__builtin_ia32_stored", void_ftype_pcint_v4si, IX86_BUILTIN_STORED);
13252   def_builtin (MASK_SSE2, "__builtin_ia32_movq", v2di_ftype_v2di, IX86_BUILTIN_MOVQ);
13253
13254   def_builtin (MASK_SSE1, "__builtin_ia32_setzero128", v2di_ftype_void, IX86_BUILTIN_CLRTI);
13255
13256   def_builtin (MASK_SSE2, "__builtin_ia32_psllw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSLLW128);
13257   def_builtin (MASK_SSE2, "__builtin_ia32_pslld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSLLD128);
13258   def_builtin (MASK_SSE2, "__builtin_ia32_psllq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSLLQ128);
13259
13260   def_builtin (MASK_SSE2, "__builtin_ia32_psrlw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRLW128);
13261   def_builtin (MASK_SSE2, "__builtin_ia32_psrld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRLD128);
13262   def_builtin (MASK_SSE2, "__builtin_ia32_psrlq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSRLQ128);
13263
13264   def_builtin (MASK_SSE2, "__builtin_ia32_psraw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRAW128);
13265   def_builtin (MASK_SSE2, "__builtin_ia32_psrad128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRAD128);
13266
13267   def_builtin (MASK_SSE2, "__builtin_ia32_pslldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLDQI128);
13268   def_builtin (MASK_SSE2, "__builtin_ia32_psllwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSLLWI128);
13269   def_builtin (MASK_SSE2, "__builtin_ia32_pslldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSLLDI128);
13270   def_builtin (MASK_SSE2, "__builtin_ia32_psllqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLQI128);
13271
13272   def_builtin (MASK_SSE2, "__builtin_ia32_psrldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLDQI128);
13273   def_builtin (MASK_SSE2, "__builtin_ia32_psrlwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRLWI128);
13274   def_builtin (MASK_SSE2, "__builtin_ia32_psrldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRLDI128);
13275   def_builtin (MASK_SSE2, "__builtin_ia32_psrlqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLQI128);
13276
13277   def_builtin (MASK_SSE2, "__builtin_ia32_psrawi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRAWI128);
13278   def_builtin (MASK_SSE2, "__builtin_ia32_psradi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRADI128);
13279
13280   def_builtin (MASK_SSE2, "__builtin_ia32_pmaddwd128", v4si_ftype_v8hi_v8hi, IX86_BUILTIN_PMADDWD128);
13281 }
13282
13283 /* Errors in the source file can cause expand_expr to return const0_rtx
13284    where we expect a vector.  To avoid crashing, use one of the vector
13285    clear instructions.  */
13286 static rtx
13287 safe_vector_operand (x, mode)
13288      rtx x;
13289      enum machine_mode mode;
13290 {
13291   if (x != const0_rtx)
13292     return x;
13293   x = gen_reg_rtx (mode);
13294
13295   if (VALID_MMX_REG_MODE (mode) || VALID_MMX_REG_MODE_3DNOW (mode))
13296     emit_insn (gen_mmx_clrdi (mode == DImode ? x
13297                               : gen_rtx_SUBREG (DImode, x, 0)));
13298   else
13299     emit_insn (gen_sse_clrv4sf (mode == V4SFmode ? x
13300                                 : gen_rtx_SUBREG (V4SFmode, x, 0),
13301                                 CONST0_RTX (V4SFmode)));
13302   return x;
13303 }
13304
13305 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
13306
13307 static rtx
13308 ix86_expand_binop_builtin (icode, arglist, target)
13309      enum insn_code icode;
13310      tree arglist;
13311      rtx target;
13312 {
13313   rtx pat;
13314   tree arg0 = TREE_VALUE (arglist);
13315   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13316   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13317   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13318   enum machine_mode tmode = insn_data[icode].operand[0].mode;
13319   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13320   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13321
13322   if (VECTOR_MODE_P (mode0))
13323     op0 = safe_vector_operand (op0, mode0);
13324   if (VECTOR_MODE_P (mode1))
13325     op1 = safe_vector_operand (op1, mode1);
13326
13327   if (! target
13328       || GET_MODE (target) != tmode
13329       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13330     target = gen_reg_rtx (tmode);
13331
13332   /* In case the insn wants input operands in modes different from
13333      the result, abort.  */
13334   if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
13335     abort ();
13336
13337   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13338     op0 = copy_to_mode_reg (mode0, op0);
13339   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13340     op1 = copy_to_mode_reg (mode1, op1);
13341
13342   /* In the commutative cases, both op0 and op1 are nonimmediate_operand,
13343      yet one of the two must not be a memory.  This is normally enforced
13344      by expanders, but we didn't bother to create one here.  */
13345   if (GET_CODE (op0) == MEM && GET_CODE (op1) == MEM)
13346     op0 = copy_to_mode_reg (mode0, op0);
13347
13348   pat = GEN_FCN (icode) (target, op0, op1);
13349   if (! pat)
13350     return 0;
13351   emit_insn (pat);
13352   return target;
13353 }
13354
13355 /* Subroutine of ix86_expand_builtin to take care of stores.  */
13356
13357 static rtx
13358 ix86_expand_store_builtin (icode, arglist)
13359      enum insn_code icode;
13360      tree arglist;
13361 {
13362   rtx pat;
13363   tree arg0 = TREE_VALUE (arglist);
13364   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13365   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13366   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13367   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
13368   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
13369
13370   if (VECTOR_MODE_P (mode1))
13371     op1 = safe_vector_operand (op1, mode1);
13372
13373   op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
13374
13375   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
13376     op1 = copy_to_mode_reg (mode1, op1);
13377
13378   pat = GEN_FCN (icode) (op0, op1);
13379   if (pat)
13380     emit_insn (pat);
13381   return 0;
13382 }
13383
13384 /* Subroutine of ix86_expand_builtin to take care of unop insns.  */
13385
13386 static rtx
13387 ix86_expand_unop_builtin (icode, arglist, target, do_load)
13388      enum insn_code icode;
13389      tree arglist;
13390      rtx target;
13391      int do_load;
13392 {
13393   rtx pat;
13394   tree arg0 = TREE_VALUE (arglist);
13395   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13396   enum machine_mode tmode = insn_data[icode].operand[0].mode;
13397   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13398
13399   if (! target
13400       || GET_MODE (target) != tmode
13401       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13402     target = gen_reg_rtx (tmode);
13403   if (do_load)
13404     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
13405   else
13406     {
13407       if (VECTOR_MODE_P (mode0))
13408         op0 = safe_vector_operand (op0, mode0);
13409
13410       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13411         op0 = copy_to_mode_reg (mode0, op0);
13412     }
13413
13414   pat = GEN_FCN (icode) (target, op0);
13415   if (! pat)
13416     return 0;
13417   emit_insn (pat);
13418   return target;
13419 }
13420
13421 /* Subroutine of ix86_expand_builtin to take care of three special unop insns:
13422    sqrtss, rsqrtss, rcpss.  */
13423
13424 static rtx
13425 ix86_expand_unop1_builtin (icode, arglist, target)
13426      enum insn_code icode;
13427      tree arglist;
13428      rtx target;
13429 {
13430   rtx pat;
13431   tree arg0 = TREE_VALUE (arglist);
13432   rtx op1, op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13433   enum machine_mode tmode = insn_data[icode].operand[0].mode;
13434   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13435
13436   if (! target
13437       || GET_MODE (target) != tmode
13438       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13439     target = gen_reg_rtx (tmode);
13440
13441   if (VECTOR_MODE_P (mode0))
13442     op0 = safe_vector_operand (op0, mode0);
13443
13444   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13445     op0 = copy_to_mode_reg (mode0, op0);
13446
13447   op1 = op0;
13448   if (! (*insn_data[icode].operand[2].predicate) (op1, mode0))
13449     op1 = copy_to_mode_reg (mode0, op1);
13450
13451   pat = GEN_FCN (icode) (target, op0, op1);
13452   if (! pat)
13453     return 0;
13454   emit_insn (pat);
13455   return target;
13456 }
13457
13458 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
13459
13460 static rtx
13461 ix86_expand_sse_compare (d, arglist, target)
13462      const struct builtin_description *d;
13463      tree arglist;
13464      rtx target;
13465 {
13466   rtx pat;
13467   tree arg0 = TREE_VALUE (arglist);
13468   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13469   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13470   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13471   rtx op2;
13472   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
13473   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
13474   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
13475   enum rtx_code comparison = d->comparison;
13476
13477   if (VECTOR_MODE_P (mode0))
13478     op0 = safe_vector_operand (op0, mode0);
13479   if (VECTOR_MODE_P (mode1))
13480     op1 = safe_vector_operand (op1, mode1);
13481
13482   /* Swap operands if we have a comparison that isn't available in
13483      hardware.  */
13484   if (d->flag)
13485     {
13486       rtx tmp = gen_reg_rtx (mode1);
13487       emit_move_insn (tmp, op1);
13488       op1 = op0;
13489       op0 = tmp;
13490     }
13491
13492   if (! target
13493       || GET_MODE (target) != tmode
13494       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
13495     target = gen_reg_rtx (tmode);
13496
13497   if (! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
13498     op0 = copy_to_mode_reg (mode0, op0);
13499   if (! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
13500     op1 = copy_to_mode_reg (mode1, op1);
13501
13502   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
13503   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
13504   if (! pat)
13505     return 0;
13506   emit_insn (pat);
13507   return target;
13508 }
13509
13510 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
13511
13512 static rtx
13513 ix86_expand_sse_comi (d, arglist, target)
13514      const struct builtin_description *d;
13515      tree arglist;
13516      rtx target;
13517 {
13518   rtx pat;
13519   tree arg0 = TREE_VALUE (arglist);
13520   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13521   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13522   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13523   rtx op2;
13524   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
13525   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
13526   enum rtx_code comparison = d->comparison;
13527
13528   if (VECTOR_MODE_P (mode0))
13529     op0 = safe_vector_operand (op0, mode0);
13530   if (VECTOR_MODE_P (mode1))
13531     op1 = safe_vector_operand (op1, mode1);
13532
13533   /* Swap operands if we have a comparison that isn't available in
13534      hardware.  */
13535   if (d->flag)
13536     {
13537       rtx tmp = op1;
13538       op1 = op0;
13539       op0 = tmp;
13540     }
13541
13542   target = gen_reg_rtx (SImode);
13543   emit_move_insn (target, const0_rtx);
13544   target = gen_rtx_SUBREG (QImode, target, 0);
13545
13546   if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
13547     op0 = copy_to_mode_reg (mode0, op0);
13548   if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
13549     op1 = copy_to_mode_reg (mode1, op1);
13550
13551   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
13552   pat = GEN_FCN (d->icode) (op0, op1);
13553   if (! pat)
13554     return 0;
13555   emit_insn (pat);
13556   emit_insn (gen_rtx_SET (VOIDmode,
13557                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
13558                           gen_rtx_fmt_ee (comparison, QImode,
13559                                           SET_DEST (pat),
13560                                           const0_rtx)));
13561
13562   return SUBREG_REG (target);
13563 }
13564
13565 /* Expand an expression EXP that calls a built-in function,
13566    with result going to TARGET if that's convenient
13567    (and in mode MODE if that's convenient).
13568    SUBTARGET may be used as the target for computing one of EXP's operands.
13569    IGNORE is nonzero if the value is to be ignored.  */
13570
13571 rtx
13572 ix86_expand_builtin (exp, target, subtarget, mode, ignore)
13573      tree exp;
13574      rtx target;
13575      rtx subtarget ATTRIBUTE_UNUSED;
13576      enum machine_mode mode ATTRIBUTE_UNUSED;
13577      int ignore ATTRIBUTE_UNUSED;
13578 {
13579   const struct builtin_description *d;
13580   size_t i;
13581   enum insn_code icode;
13582   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
13583   tree arglist = TREE_OPERAND (exp, 1);
13584   tree arg0, arg1, arg2;
13585   rtx op0, op1, op2, pat;
13586   enum machine_mode tmode, mode0, mode1, mode2;
13587   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
13588
13589   switch (fcode)
13590     {
13591     case IX86_BUILTIN_EMMS:
13592       emit_insn (gen_emms ());
13593       return 0;
13594
13595     case IX86_BUILTIN_SFENCE:
13596       emit_insn (gen_sfence ());
13597       return 0;
13598
13599     case IX86_BUILTIN_PEXTRW:
13600     case IX86_BUILTIN_PEXTRW128:
13601       icode = (fcode == IX86_BUILTIN_PEXTRW
13602                ? CODE_FOR_mmx_pextrw
13603                : CODE_FOR_sse2_pextrw);
13604       arg0 = TREE_VALUE (arglist);
13605       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13606       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13607       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13608       tmode = insn_data[icode].operand[0].mode;
13609       mode0 = insn_data[icode].operand[1].mode;
13610       mode1 = insn_data[icode].operand[2].mode;
13611
13612       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13613         op0 = copy_to_mode_reg (mode0, op0);
13614       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13615         {
13616           /* @@@ better error message */
13617           error ("selector must be an immediate");
13618           return gen_reg_rtx (tmode);
13619         }
13620       if (target == 0
13621           || GET_MODE (target) != tmode
13622           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13623         target = gen_reg_rtx (tmode);
13624       pat = GEN_FCN (icode) (target, op0, op1);
13625       if (! pat)
13626         return 0;
13627       emit_insn (pat);
13628       return target;
13629
13630     case IX86_BUILTIN_PINSRW:
13631     case IX86_BUILTIN_PINSRW128:
13632       icode = (fcode == IX86_BUILTIN_PINSRW
13633                ? CODE_FOR_mmx_pinsrw
13634                : CODE_FOR_sse2_pinsrw);
13635       arg0 = TREE_VALUE (arglist);
13636       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13637       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
13638       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13639       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13640       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
13641       tmode = insn_data[icode].operand[0].mode;
13642       mode0 = insn_data[icode].operand[1].mode;
13643       mode1 = insn_data[icode].operand[2].mode;
13644       mode2 = insn_data[icode].operand[3].mode;
13645
13646       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13647         op0 = copy_to_mode_reg (mode0, op0);
13648       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13649         op1 = copy_to_mode_reg (mode1, op1);
13650       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
13651         {
13652           /* @@@ better error message */
13653           error ("selector must be an immediate");
13654           return const0_rtx;
13655         }
13656       if (target == 0
13657           || GET_MODE (target) != tmode
13658           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13659         target = gen_reg_rtx (tmode);
13660       pat = GEN_FCN (icode) (target, op0, op1, op2);
13661       if (! pat)
13662         return 0;
13663       emit_insn (pat);
13664       return target;
13665
13666     case IX86_BUILTIN_MASKMOVQ:
13667     case IX86_BUILTIN_MASKMOVDQU:
13668       icode = (fcode == IX86_BUILTIN_MASKMOVQ
13669                ? (TARGET_64BIT ? CODE_FOR_mmx_maskmovq_rex : CODE_FOR_mmx_maskmovq)
13670                : (TARGET_64BIT ? CODE_FOR_sse2_maskmovdqu_rex64
13671                   : CODE_FOR_sse2_maskmovdqu));
13672       /* Note the arg order is different from the operand order.  */
13673       arg1 = TREE_VALUE (arglist);
13674       arg2 = TREE_VALUE (TREE_CHAIN (arglist));
13675       arg0 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
13676       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13677       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13678       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
13679       mode0 = insn_data[icode].operand[0].mode;
13680       mode1 = insn_data[icode].operand[1].mode;
13681       mode2 = insn_data[icode].operand[2].mode;
13682
13683       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
13684         op0 = copy_to_mode_reg (mode0, op0);
13685       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
13686         op1 = copy_to_mode_reg (mode1, op1);
13687       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
13688         op2 = copy_to_mode_reg (mode2, op2);
13689       pat = GEN_FCN (icode) (op0, op1, op2);
13690       if (! pat)
13691         return 0;
13692       emit_insn (pat);
13693       return 0;
13694
13695     case IX86_BUILTIN_SQRTSS:
13696       return ix86_expand_unop1_builtin (CODE_FOR_vmsqrtv4sf2, arglist, target);
13697     case IX86_BUILTIN_RSQRTSS:
13698       return ix86_expand_unop1_builtin (CODE_FOR_vmrsqrtv4sf2, arglist, target);
13699     case IX86_BUILTIN_RCPSS:
13700       return ix86_expand_unop1_builtin (CODE_FOR_vmrcpv4sf2, arglist, target);
13701
13702     case IX86_BUILTIN_LOADAPS:
13703       return ix86_expand_unop_builtin (CODE_FOR_sse_movaps, arglist, target, 1);
13704
13705     case IX86_BUILTIN_LOADUPS:
13706       return ix86_expand_unop_builtin (CODE_FOR_sse_movups, arglist, target, 1);
13707
13708     case IX86_BUILTIN_STOREAPS:
13709       return ix86_expand_store_builtin (CODE_FOR_sse_movaps, arglist);
13710
13711     case IX86_BUILTIN_STOREUPS:
13712       return ix86_expand_store_builtin (CODE_FOR_sse_movups, arglist);
13713
13714     case IX86_BUILTIN_LOADSS:
13715       return ix86_expand_unop_builtin (CODE_FOR_sse_loadss, arglist, target, 1);
13716
13717     case IX86_BUILTIN_STORESS:
13718       return ix86_expand_store_builtin (CODE_FOR_sse_storess, arglist);
13719
13720     case IX86_BUILTIN_LOADHPS:
13721     case IX86_BUILTIN_LOADLPS:
13722     case IX86_BUILTIN_LOADHPD:
13723     case IX86_BUILTIN_LOADLPD:
13724       icode = (fcode == IX86_BUILTIN_LOADHPS ? CODE_FOR_sse_movhps
13725                : fcode == IX86_BUILTIN_LOADLPS ? CODE_FOR_sse_movlps
13726                : fcode == IX86_BUILTIN_LOADHPD ? CODE_FOR_sse2_movhpd
13727                : CODE_FOR_sse2_movlpd);
13728       arg0 = TREE_VALUE (arglist);
13729       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13730       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13731       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13732       tmode = insn_data[icode].operand[0].mode;
13733       mode0 = insn_data[icode].operand[1].mode;
13734       mode1 = insn_data[icode].operand[2].mode;
13735
13736       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13737         op0 = copy_to_mode_reg (mode0, op0);
13738       op1 = gen_rtx_MEM (mode1, copy_to_mode_reg (Pmode, op1));
13739       if (target == 0
13740           || GET_MODE (target) != tmode
13741           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13742         target = gen_reg_rtx (tmode);
13743       pat = GEN_FCN (icode) (target, op0, op1);
13744       if (! pat)
13745         return 0;
13746       emit_insn (pat);
13747       return target;
13748
13749     case IX86_BUILTIN_STOREHPS:
13750     case IX86_BUILTIN_STORELPS:
13751     case IX86_BUILTIN_STOREHPD:
13752     case IX86_BUILTIN_STORELPD:
13753       icode = (fcode == IX86_BUILTIN_STOREHPS ? CODE_FOR_sse_movhps
13754                : fcode == IX86_BUILTIN_STORELPS ? CODE_FOR_sse_movlps
13755                : fcode == IX86_BUILTIN_STOREHPD ? CODE_FOR_sse2_movhpd
13756                : CODE_FOR_sse2_movlpd);
13757       arg0 = TREE_VALUE (arglist);
13758       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13759       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13760       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13761       mode0 = insn_data[icode].operand[1].mode;
13762       mode1 = insn_data[icode].operand[2].mode;
13763
13764       op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
13765       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13766         op1 = copy_to_mode_reg (mode1, op1);
13767
13768       pat = GEN_FCN (icode) (op0, op0, op1);
13769       if (! pat)
13770         return 0;
13771       emit_insn (pat);
13772       return 0;
13773
13774     case IX86_BUILTIN_MOVNTPS:
13775       return ix86_expand_store_builtin (CODE_FOR_sse_movntv4sf, arglist);
13776     case IX86_BUILTIN_MOVNTQ:
13777       return ix86_expand_store_builtin (CODE_FOR_sse_movntdi, arglist);
13778
13779     case IX86_BUILTIN_LDMXCSR:
13780       op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
13781       target = assign_386_stack_local (SImode, 0);
13782       emit_move_insn (target, op0);
13783       emit_insn (gen_ldmxcsr (target));
13784       return 0;
13785
13786     case IX86_BUILTIN_STMXCSR:
13787       target = assign_386_stack_local (SImode, 0);
13788       emit_insn (gen_stmxcsr (target));
13789       return copy_to_mode_reg (SImode, target);
13790
13791     case IX86_BUILTIN_SHUFPS:
13792     case IX86_BUILTIN_SHUFPD:
13793       icode = (fcode == IX86_BUILTIN_SHUFPS
13794                ? CODE_FOR_sse_shufps
13795                : CODE_FOR_sse2_shufpd);
13796       arg0 = TREE_VALUE (arglist);
13797       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13798       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
13799       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13800       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13801       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
13802       tmode = insn_data[icode].operand[0].mode;
13803       mode0 = insn_data[icode].operand[1].mode;
13804       mode1 = insn_data[icode].operand[2].mode;
13805       mode2 = insn_data[icode].operand[3].mode;
13806
13807       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13808         op0 = copy_to_mode_reg (mode0, op0);
13809       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13810         op1 = copy_to_mode_reg (mode1, op1);
13811       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
13812         {
13813           /* @@@ better error message */
13814           error ("mask must be an immediate");
13815           return gen_reg_rtx (tmode);
13816         }
13817       if (target == 0
13818           || GET_MODE (target) != tmode
13819           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13820         target = gen_reg_rtx (tmode);
13821       pat = GEN_FCN (icode) (target, op0, op1, op2);
13822       if (! pat)
13823         return 0;
13824       emit_insn (pat);
13825       return target;
13826
13827     case IX86_BUILTIN_PSHUFW:
13828     case IX86_BUILTIN_PSHUFD:
13829     case IX86_BUILTIN_PSHUFHW:
13830     case IX86_BUILTIN_PSHUFLW:
13831       icode = (  fcode == IX86_BUILTIN_PSHUFHW ? CODE_FOR_sse2_pshufhw
13832                : fcode == IX86_BUILTIN_PSHUFLW ? CODE_FOR_sse2_pshuflw
13833                : fcode == IX86_BUILTIN_PSHUFD ? CODE_FOR_sse2_pshufd
13834                : CODE_FOR_mmx_pshufw);
13835       arg0 = TREE_VALUE (arglist);
13836       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13837       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13838       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13839       tmode = insn_data[icode].operand[0].mode;
13840       mode1 = insn_data[icode].operand[1].mode;
13841       mode2 = insn_data[icode].operand[2].mode;
13842
13843       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
13844         op0 = copy_to_mode_reg (mode1, op0);
13845       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
13846         {
13847           /* @@@ better error message */
13848           error ("mask must be an immediate");
13849           return const0_rtx;
13850         }
13851       if (target == 0
13852           || GET_MODE (target) != tmode
13853           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13854         target = gen_reg_rtx (tmode);
13855       pat = GEN_FCN (icode) (target, op0, op1);
13856       if (! pat)
13857         return 0;
13858       emit_insn (pat);
13859       return target;
13860
13861     case IX86_BUILTIN_PSLLDQI128:
13862     case IX86_BUILTIN_PSRLDQI128:
13863       icode = (  fcode == IX86_BUILTIN_PSLLDQI128 ? CODE_FOR_sse2_ashlti3
13864                : CODE_FOR_sse2_lshrti3);
13865       arg0 = TREE_VALUE (arglist);
13866       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
13867       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
13868       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
13869       tmode = insn_data[icode].operand[0].mode;
13870       mode1 = insn_data[icode].operand[1].mode;
13871       mode2 = insn_data[icode].operand[2].mode;
13872
13873       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
13874         {
13875           op0 = copy_to_reg (op0);
13876           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
13877         }
13878       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
13879         {
13880           error ("shift must be an immediate");
13881           return const0_rtx;
13882         }
13883       target = gen_reg_rtx (V2DImode);
13884       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, V2DImode, 0), op0, op1);
13885       if (! pat)
13886         return 0;
13887       emit_insn (pat);
13888       return target;
13889
13890     case IX86_BUILTIN_FEMMS:
13891       emit_insn (gen_femms ());
13892       return NULL_RTX;
13893
13894     case IX86_BUILTIN_PAVGUSB:
13895       return ix86_expand_binop_builtin (CODE_FOR_pavgusb, arglist, target);
13896
13897     case IX86_BUILTIN_PF2ID:
13898       return ix86_expand_unop_builtin (CODE_FOR_pf2id, arglist, target, 0);
13899
13900     case IX86_BUILTIN_PFACC:
13901       return ix86_expand_binop_builtin (CODE_FOR_pfacc, arglist, target);
13902
13903     case IX86_BUILTIN_PFADD:
13904      return ix86_expand_binop_builtin (CODE_FOR_addv2sf3, arglist, target);
13905
13906     case IX86_BUILTIN_PFCMPEQ:
13907       return ix86_expand_binop_builtin (CODE_FOR_eqv2sf3, arglist, target);
13908
13909     case IX86_BUILTIN_PFCMPGE:
13910       return ix86_expand_binop_builtin (CODE_FOR_gev2sf3, arglist, target);
13911
13912     case IX86_BUILTIN_PFCMPGT:
13913       return ix86_expand_binop_builtin (CODE_FOR_gtv2sf3, arglist, target);
13914
13915     case IX86_BUILTIN_PFMAX:
13916       return ix86_expand_binop_builtin (CODE_FOR_pfmaxv2sf3, arglist, target);
13917
13918     case IX86_BUILTIN_PFMIN:
13919       return ix86_expand_binop_builtin (CODE_FOR_pfminv2sf3, arglist, target);
13920
13921     case IX86_BUILTIN_PFMUL:
13922       return ix86_expand_binop_builtin (CODE_FOR_mulv2sf3, arglist, target);
13923
13924     case IX86_BUILTIN_PFRCP:
13925       return ix86_expand_unop_builtin (CODE_FOR_pfrcpv2sf2, arglist, target, 0);
13926
13927     case IX86_BUILTIN_PFRCPIT1:
13928       return ix86_expand_binop_builtin (CODE_FOR_pfrcpit1v2sf3, arglist, target);
13929
13930     case IX86_BUILTIN_PFRCPIT2:
13931       return ix86_expand_binop_builtin (CODE_FOR_pfrcpit2v2sf3, arglist, target);
13932
13933     case IX86_BUILTIN_PFRSQIT1:
13934       return ix86_expand_binop_builtin (CODE_FOR_pfrsqit1v2sf3, arglist, target);
13935
13936     case IX86_BUILTIN_PFRSQRT:
13937       return ix86_expand_unop_builtin (CODE_FOR_pfrsqrtv2sf2, arglist, target, 0);
13938
13939     case IX86_BUILTIN_PFSUB:
13940       return ix86_expand_binop_builtin (CODE_FOR_subv2sf3, arglist, target);
13941
13942     case IX86_BUILTIN_PFSUBR:
13943       return ix86_expand_binop_builtin (CODE_FOR_subrv2sf3, arglist, target);
13944
13945     case IX86_BUILTIN_PI2FD:
13946       return ix86_expand_unop_builtin (CODE_FOR_floatv2si2, arglist, target, 0);
13947
13948     case IX86_BUILTIN_PMULHRW:
13949       return ix86_expand_binop_builtin (CODE_FOR_pmulhrwv4hi3, arglist, target);
13950
13951     case IX86_BUILTIN_PF2IW:
13952       return ix86_expand_unop_builtin (CODE_FOR_pf2iw, arglist, target, 0);
13953
13954     case IX86_BUILTIN_PFNACC:
13955       return ix86_expand_binop_builtin (CODE_FOR_pfnacc, arglist, target);
13956
13957     case IX86_BUILTIN_PFPNACC:
13958       return ix86_expand_binop_builtin (CODE_FOR_pfpnacc, arglist, target);
13959
13960     case IX86_BUILTIN_PI2FW:
13961       return ix86_expand_unop_builtin (CODE_FOR_pi2fw, arglist, target, 0);
13962
13963     case IX86_BUILTIN_PSWAPDSI:
13964       return ix86_expand_unop_builtin (CODE_FOR_pswapdv2si2, arglist, target, 0);
13965
13966     case IX86_BUILTIN_PSWAPDSF:
13967       return ix86_expand_unop_builtin (CODE_FOR_pswapdv2sf2, arglist, target, 0);
13968
13969     case IX86_BUILTIN_SSE_ZERO:
13970       target = gen_reg_rtx (V4SFmode);
13971       emit_insn (gen_sse_clrv4sf (target, CONST0_RTX (V4SFmode)));
13972       return target;
13973
13974     case IX86_BUILTIN_MMX_ZERO:
13975       target = gen_reg_rtx (DImode);
13976       emit_insn (gen_mmx_clrdi (target));
13977       return target;
13978
13979     case IX86_BUILTIN_CLRTI:
13980       target = gen_reg_rtx (V2DImode);
13981       emit_insn (gen_sse2_clrti (simplify_gen_subreg (TImode, target, V2DImode, 0)));
13982       return target;
13983
13984
13985     case IX86_BUILTIN_SQRTSD:
13986       return ix86_expand_unop1_builtin (CODE_FOR_vmsqrtv2df2, arglist, target);
13987     case IX86_BUILTIN_LOADAPD:
13988       return ix86_expand_unop_builtin (CODE_FOR_sse2_movapd, arglist, target, 1);
13989     case IX86_BUILTIN_LOADUPD:
13990       return ix86_expand_unop_builtin (CODE_FOR_sse2_movupd, arglist, target, 1);
13991
13992     case IX86_BUILTIN_STOREAPD:
13993       return ix86_expand_store_builtin (CODE_FOR_sse2_movapd, arglist);
13994     case IX86_BUILTIN_STOREUPD:
13995       return ix86_expand_store_builtin (CODE_FOR_sse2_movupd, arglist);
13996
13997     case IX86_BUILTIN_LOADSD:
13998       return ix86_expand_unop_builtin (CODE_FOR_sse2_loadsd, arglist, target, 1);
13999
14000     case IX86_BUILTIN_STORESD:
14001       return ix86_expand_store_builtin (CODE_FOR_sse2_storesd, arglist);
14002
14003     case IX86_BUILTIN_SETPD1:
14004       target = assign_386_stack_local (DFmode, 0);
14005       arg0 = TREE_VALUE (arglist);
14006       emit_move_insn (adjust_address (target, DFmode, 0),
14007                       expand_expr (arg0, NULL_RTX, VOIDmode, 0));
14008       op0 = gen_reg_rtx (V2DFmode);
14009       emit_insn (gen_sse2_loadsd (op0, adjust_address (target, V2DFmode, 0)));
14010       emit_insn (gen_sse2_shufpd (op0, op0, op0, GEN_INT (0)));
14011       return op0;
14012
14013     case IX86_BUILTIN_SETPD:
14014       target = assign_386_stack_local (V2DFmode, 0);
14015       arg0 = TREE_VALUE (arglist);
14016       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14017       emit_move_insn (adjust_address (target, DFmode, 0),
14018                       expand_expr (arg0, NULL_RTX, VOIDmode, 0));
14019       emit_move_insn (adjust_address (target, DFmode, 8),
14020                       expand_expr (arg1, NULL_RTX, VOIDmode, 0));
14021       op0 = gen_reg_rtx (V2DFmode);
14022       emit_insn (gen_sse2_movapd (op0, target));
14023       return op0;
14024
14025     case IX86_BUILTIN_LOADRPD:
14026       target = ix86_expand_unop_builtin (CODE_FOR_sse2_movapd, arglist,
14027                                          gen_reg_rtx (V2DFmode), 1);
14028       emit_insn (gen_sse2_shufpd (target, target, target, GEN_INT (1)));
14029       return target;
14030
14031     case IX86_BUILTIN_LOADPD1:
14032       target = ix86_expand_unop_builtin (CODE_FOR_sse2_loadsd, arglist,
14033                                          gen_reg_rtx (V2DFmode), 1);
14034       emit_insn (gen_sse2_shufpd (target, target, target, const0_rtx));
14035       return target;
14036
14037     case IX86_BUILTIN_STOREPD1:
14038       return ix86_expand_store_builtin (CODE_FOR_sse2_movapd, arglist);
14039     case IX86_BUILTIN_STORERPD:
14040       return ix86_expand_store_builtin (CODE_FOR_sse2_movapd, arglist);
14041
14042     case IX86_BUILTIN_CLRPD:
14043       target = gen_reg_rtx (V2DFmode);
14044       emit_insn (gen_sse_clrv2df (target));
14045       return target;
14046
14047     case IX86_BUILTIN_MFENCE:
14048         emit_insn (gen_sse2_mfence ());
14049         return 0;
14050     case IX86_BUILTIN_LFENCE:
14051         emit_insn (gen_sse2_lfence ());
14052         return 0;
14053
14054     case IX86_BUILTIN_CLFLUSH:
14055         arg0 = TREE_VALUE (arglist);
14056         op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14057         icode = CODE_FOR_sse2_clflush;
14058         if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
14059             op0 = copy_to_mode_reg (Pmode, op0);
14060
14061         emit_insn (gen_sse2_clflush (op0));
14062         return 0;
14063
14064     case IX86_BUILTIN_MOVNTPD:
14065       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2df, arglist);
14066     case IX86_BUILTIN_MOVNTDQ:
14067       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2di, arglist);
14068     case IX86_BUILTIN_MOVNTI:
14069       return ix86_expand_store_builtin (CODE_FOR_sse2_movntsi, arglist);
14070
14071     case IX86_BUILTIN_LOADDQA:
14072       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqa, arglist, target, 1);
14073     case IX86_BUILTIN_LOADDQU:
14074       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqu, arglist, target, 1);
14075     case IX86_BUILTIN_LOADD:
14076       return ix86_expand_unop_builtin (CODE_FOR_sse2_loadd, arglist, target, 1);
14077
14078     case IX86_BUILTIN_STOREDQA:
14079       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqa, arglist);
14080     case IX86_BUILTIN_STOREDQU:
14081       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqu, arglist);
14082     case IX86_BUILTIN_STORED:
14083       return ix86_expand_store_builtin (CODE_FOR_sse2_stored, arglist);
14084
14085     default:
14086       break;
14087     }
14088
14089   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
14090     if (d->code == fcode)
14091       {
14092         /* Compares are treated specially.  */
14093         if (d->icode == CODE_FOR_maskcmpv4sf3
14094             || d->icode == CODE_FOR_vmmaskcmpv4sf3
14095             || d->icode == CODE_FOR_maskncmpv4sf3
14096             || d->icode == CODE_FOR_vmmaskncmpv4sf3
14097             || d->icode == CODE_FOR_maskcmpv2df3
14098             || d->icode == CODE_FOR_vmmaskcmpv2df3
14099             || d->icode == CODE_FOR_maskncmpv2df3
14100             || d->icode == CODE_FOR_vmmaskncmpv2df3)
14101           return ix86_expand_sse_compare (d, arglist, target);
14102
14103         return ix86_expand_binop_builtin (d->icode, arglist, target);
14104       }
14105
14106   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
14107     if (d->code == fcode)
14108       return ix86_expand_unop_builtin (d->icode, arglist, target, 0);
14109
14110   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
14111     if (d->code == fcode)
14112       return ix86_expand_sse_comi (d, arglist, target);
14113
14114   /* @@@ Should really do something sensible here.  */
14115   return 0;
14116 }
14117
14118 /* Store OPERAND to the memory after reload is completed.  This means
14119    that we can't easily use assign_stack_local.  */
14120 rtx
14121 ix86_force_to_memory (mode, operand)
14122      enum machine_mode mode;
14123      rtx operand;
14124 {
14125   rtx result;
14126   if (!reload_completed)
14127     abort ();
14128   if (TARGET_64BIT && TARGET_RED_ZONE)
14129     {
14130       result = gen_rtx_MEM (mode,
14131                             gen_rtx_PLUS (Pmode,
14132                                           stack_pointer_rtx,
14133                                           GEN_INT (-RED_ZONE_SIZE)));
14134       emit_move_insn (result, operand);
14135     }
14136   else if (TARGET_64BIT && !TARGET_RED_ZONE)
14137     {
14138       switch (mode)
14139         {
14140         case HImode:
14141         case SImode:
14142           operand = gen_lowpart (DImode, operand);
14143           /* FALLTHRU */
14144         case DImode:
14145           emit_insn (
14146                       gen_rtx_SET (VOIDmode,
14147                                    gen_rtx_MEM (DImode,
14148                                                 gen_rtx_PRE_DEC (DImode,
14149                                                         stack_pointer_rtx)),
14150                                    operand));
14151           break;
14152         default:
14153           abort ();
14154         }
14155       result = gen_rtx_MEM (mode, stack_pointer_rtx);
14156     }
14157   else
14158     {
14159       switch (mode)
14160         {
14161         case DImode:
14162           {
14163             rtx operands[2];
14164             split_di (&operand, 1, operands, operands + 1);
14165             emit_insn (
14166                         gen_rtx_SET (VOIDmode,
14167                                      gen_rtx_MEM (SImode,
14168                                                   gen_rtx_PRE_DEC (Pmode,
14169                                                         stack_pointer_rtx)),
14170                                      operands[1]));
14171             emit_insn (
14172                         gen_rtx_SET (VOIDmode,
14173                                      gen_rtx_MEM (SImode,
14174                                                   gen_rtx_PRE_DEC (Pmode,
14175                                                         stack_pointer_rtx)),
14176                                      operands[0]));
14177           }
14178           break;
14179         case HImode:
14180           /* It is better to store HImodes as SImodes.  */
14181           if (!TARGET_PARTIAL_REG_STALL)
14182             operand = gen_lowpart (SImode, operand);
14183           /* FALLTHRU */
14184         case SImode:
14185           emit_insn (
14186                       gen_rtx_SET (VOIDmode,
14187                                    gen_rtx_MEM (GET_MODE (operand),
14188                                                 gen_rtx_PRE_DEC (SImode,
14189                                                         stack_pointer_rtx)),
14190                                    operand));
14191           break;
14192         default:
14193           abort ();
14194         }
14195       result = gen_rtx_MEM (mode, stack_pointer_rtx);
14196     }
14197   return result;
14198 }
14199
14200 /* Free operand from the memory.  */
14201 void
14202 ix86_free_from_memory (mode)
14203      enum machine_mode mode;
14204 {
14205   if (!TARGET_64BIT || !TARGET_RED_ZONE)
14206     {
14207       int size;
14208
14209       if (mode == DImode || TARGET_64BIT)
14210         size = 8;
14211       else if (mode == HImode && TARGET_PARTIAL_REG_STALL)
14212         size = 2;
14213       else
14214         size = 4;
14215       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
14216          to pop or add instruction if registers are available.  */
14217       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
14218                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
14219                                             GEN_INT (size))));
14220     }
14221 }
14222
14223 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
14224    QImode must go into class Q_REGS.
14225    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
14226    movdf to do mem-to-mem moves through integer regs.  */
14227 enum reg_class
14228 ix86_preferred_reload_class (x, class)
14229      rtx x;
14230      enum reg_class class;
14231 {
14232   if (GET_CODE (x) == CONST_VECTOR && x != CONST0_RTX (GET_MODE (x)))
14233     return NO_REGS;
14234   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
14235     {
14236       /* SSE can't load any constant directly yet.  */
14237       if (SSE_CLASS_P (class))
14238         return NO_REGS;
14239       /* Floats can load 0 and 1.  */
14240       if (MAYBE_FLOAT_CLASS_P (class) && standard_80387_constant_p (x))
14241         {
14242           /* Limit class to non-SSE.  Use GENERAL_REGS if possible.  */
14243           if (MAYBE_SSE_CLASS_P (class))
14244             return (reg_class_subset_p (class, GENERAL_REGS)
14245                     ? GENERAL_REGS : FLOAT_REGS);
14246           else
14247             return class;
14248         }
14249       /* General regs can load everything.  */
14250       if (reg_class_subset_p (class, GENERAL_REGS))
14251         return GENERAL_REGS;
14252       /* In case we haven't resolved FLOAT or SSE yet, give up.  */
14253       if (MAYBE_FLOAT_CLASS_P (class) || MAYBE_SSE_CLASS_P (class))
14254         return NO_REGS;
14255     }
14256   if (MAYBE_MMX_CLASS_P (class) && CONSTANT_P (x))
14257     return NO_REGS;
14258   if (GET_MODE (x) == QImode && ! reg_class_subset_p (class, Q_REGS))
14259     return Q_REGS;
14260   return class;
14261 }
14262
14263 /* If we are copying between general and FP registers, we need a memory
14264    location. The same is true for SSE and MMX registers.
14265
14266    The macro can't work reliably when one of the CLASSES is class containing
14267    registers from multiple units (SSE, MMX, integer).  We avoid this by never
14268    combining those units in single alternative in the machine description.
14269    Ensure that this constraint holds to avoid unexpected surprises.
14270
14271    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
14272    enforce these sanity checks.  */
14273 int
14274 ix86_secondary_memory_needed (class1, class2, mode, strict)
14275      enum reg_class class1, class2;
14276      enum machine_mode mode;
14277      int strict;
14278 {
14279   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
14280       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
14281       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
14282       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
14283       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
14284       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
14285     {
14286       if (strict)
14287         abort ();
14288       else
14289         return 1;
14290     }
14291   return (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2)
14292           || (SSE_CLASS_P (class1) != SSE_CLASS_P (class2)
14293               && (mode) != SImode)
14294           || (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
14295               && (mode) != SImode));
14296 }
14297 /* Return the cost of moving data from a register in class CLASS1 to
14298    one in class CLASS2.
14299
14300    It is not required that the cost always equal 2 when FROM is the same as TO;
14301    on some machines it is expensive to move between registers if they are not
14302    general registers.  */
14303 int
14304 ix86_register_move_cost (mode, class1, class2)
14305      enum machine_mode mode;
14306      enum reg_class class1, class2;
14307 {
14308   /* In case we require secondary memory, compute cost of the store followed
14309      by load.  In order to avoid bad register allocation choices, we need 
14310      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
14311
14312   if (ix86_secondary_memory_needed (class1, class2, mode, 0))
14313     {
14314       int cost = 1;
14315
14316       cost += MAX (MEMORY_MOVE_COST (mode, class1, 0),
14317                    MEMORY_MOVE_COST (mode, class1, 1));
14318       cost += MAX (MEMORY_MOVE_COST (mode, class2, 0),
14319                    MEMORY_MOVE_COST (mode, class2, 1));
14320       
14321       /* In case of copying from general_purpose_register we may emit multiple
14322          stores followed by single load causing memory size mismatch stall.
14323          Count this as arbitrarily high cost of 20.  */
14324       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
14325         cost += 20;
14326
14327       /* In the case of FP/MMX moves, the registers actually overlap, and we
14328          have to switch modes in order to treat them differently.  */
14329       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
14330           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
14331         cost += 20;
14332
14333       return cost;
14334     }
14335
14336   /* Moves between SSE/MMX and integer unit are expensive.  */
14337   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
14338       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
14339     return ix86_cost->mmxsse_to_integer;
14340   if (MAYBE_FLOAT_CLASS_P (class1))
14341     return ix86_cost->fp_move;
14342   if (MAYBE_SSE_CLASS_P (class1))
14343     return ix86_cost->sse_move;
14344   if (MAYBE_MMX_CLASS_P (class1))
14345     return ix86_cost->mmx_move;
14346   return 2;
14347 }
14348
14349 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
14350 int
14351 ix86_hard_regno_mode_ok (regno, mode)
14352      int regno;
14353      enum machine_mode mode;
14354 {
14355   /* Flags and only flags can only hold CCmode values.  */
14356   if (CC_REGNO_P (regno))
14357     return GET_MODE_CLASS (mode) == MODE_CC;
14358   if (GET_MODE_CLASS (mode) == MODE_CC
14359       || GET_MODE_CLASS (mode) == MODE_RANDOM
14360       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
14361     return 0;
14362   if (FP_REGNO_P (regno))
14363     return VALID_FP_MODE_P (mode);
14364   if (SSE_REGNO_P (regno))
14365     return VALID_SSE_REG_MODE (mode);
14366   if (MMX_REGNO_P (regno))
14367     return VALID_MMX_REG_MODE (mode) || VALID_MMX_REG_MODE_3DNOW (mode);
14368   /* We handle both integer and floats in the general purpose registers.
14369      In future we should be able to handle vector modes as well.  */
14370   if (!VALID_INT_MODE_P (mode) && !VALID_FP_MODE_P (mode))
14371     return 0;
14372   /* Take care for QImode values - they can be in non-QI regs, but then
14373      they do cause partial register stalls.  */
14374   if (regno < 4 || mode != QImode || TARGET_64BIT)
14375     return 1;
14376   return reload_in_progress || reload_completed || !TARGET_PARTIAL_REG_STALL;
14377 }
14378
14379 /* Return the cost of moving data of mode M between a
14380    register and memory.  A value of 2 is the default; this cost is
14381    relative to those in `REGISTER_MOVE_COST'.
14382
14383    If moving between registers and memory is more expensive than
14384    between two registers, you should define this macro to express the
14385    relative cost.
14386
14387    Model also increased moving costs of QImode registers in non
14388    Q_REGS classes.
14389  */
14390 int
14391 ix86_memory_move_cost (mode, class, in)
14392      enum machine_mode mode;
14393      enum reg_class class;
14394      int in;
14395 {
14396   if (FLOAT_CLASS_P (class))
14397     {
14398       int index;
14399       switch (mode)
14400         {
14401           case SFmode:
14402             index = 0;
14403             break;
14404           case DFmode:
14405             index = 1;
14406             break;
14407           case XFmode:
14408           case TFmode:
14409             index = 2;
14410             break;
14411           default:
14412             return 100;
14413         }
14414       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
14415     }
14416   if (SSE_CLASS_P (class))
14417     {
14418       int index;
14419       switch (GET_MODE_SIZE (mode))
14420         {
14421           case 4:
14422             index = 0;
14423             break;
14424           case 8:
14425             index = 1;
14426             break;
14427           case 16:
14428             index = 2;
14429             break;
14430           default:
14431             return 100;
14432         }
14433       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
14434     }
14435   if (MMX_CLASS_P (class))
14436     {
14437       int index;
14438       switch (GET_MODE_SIZE (mode))
14439         {
14440           case 4:
14441             index = 0;
14442             break;
14443           case 8:
14444             index = 1;
14445             break;
14446           default:
14447             return 100;
14448         }
14449       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
14450     }
14451   switch (GET_MODE_SIZE (mode))
14452     {
14453       case 1:
14454         if (in)
14455           return (Q_CLASS_P (class) ? ix86_cost->int_load[0]
14456                   : ix86_cost->movzbl_load);
14457         else
14458           return (Q_CLASS_P (class) ? ix86_cost->int_store[0]
14459                   : ix86_cost->int_store[0] + 4);
14460         break;
14461       case 2:
14462         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
14463       default:
14464         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
14465         if (mode == TFmode)
14466           mode = XFmode;
14467         return ((in ? ix86_cost->int_load[2] : ix86_cost->int_store[2])
14468                 * ((int) GET_MODE_SIZE (mode)
14469                    + UNITS_PER_WORD -1 ) / UNITS_PER_WORD);
14470     }
14471 }
14472
14473 /* Compute a (partial) cost for rtx X.  Return true if the complete
14474    cost has been computed, and false if subexpressions should be
14475    scanned.  In either case, *TOTAL contains the cost result.  */
14476
14477 static bool
14478 ix86_rtx_costs (x, code, outer_code, total)
14479      rtx x;
14480      int code, outer_code;
14481      int *total;
14482 {
14483   enum machine_mode mode = GET_MODE (x);
14484
14485   switch (code)
14486     {
14487     case CONST_INT:
14488     case CONST:
14489     case LABEL_REF:
14490     case SYMBOL_REF:
14491       if (TARGET_64BIT && !x86_64_sign_extended_value (x))
14492         *total = 3;
14493       else if (TARGET_64BIT && !x86_64_zero_extended_value (x))
14494         *total = 2;
14495       else if (flag_pic && SYMBOLIC_CONST (x))
14496         *total = 1;
14497       else
14498         *total = 0;
14499       return true;
14500
14501     case CONST_DOUBLE:
14502       if (mode == VOIDmode)
14503         *total = 0;
14504       else
14505         switch (standard_80387_constant_p (x))
14506           {
14507           case 1: /* 0.0 */
14508             *total = 1;
14509             break;
14510           case 2: /* 1.0 */
14511             *total = 2;
14512             break;
14513           default:
14514             /* Start with (MEM (SYMBOL_REF)), since that's where
14515                it'll probably end up.  Add a penalty for size.  */
14516             *total = (COSTS_N_INSNS (1)
14517                       + (flag_pic != 0)
14518                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
14519             break;
14520           }
14521       return true;
14522
14523     case ZERO_EXTEND:
14524       /* The zero extensions is often completely free on x86_64, so make
14525          it as cheap as possible.  */
14526       if (TARGET_64BIT && mode == DImode
14527           && GET_MODE (XEXP (x, 0)) == SImode)
14528         *total = 1;
14529       else if (TARGET_ZERO_EXTEND_WITH_AND)
14530         *total = COSTS_N_INSNS (ix86_cost->add);
14531       else
14532         *total = COSTS_N_INSNS (ix86_cost->movzx);
14533       return false;
14534
14535     case SIGN_EXTEND:
14536       *total = COSTS_N_INSNS (ix86_cost->movsx);
14537       return false;
14538
14539     case ASHIFT:
14540       if (GET_CODE (XEXP (x, 1)) == CONST_INT
14541           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
14542         {
14543           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
14544           if (value == 1)
14545             {
14546               *total = COSTS_N_INSNS (ix86_cost->add);
14547               return false;
14548             }
14549           if ((value == 2 || value == 3)
14550               && !TARGET_DECOMPOSE_LEA
14551               && ix86_cost->lea <= ix86_cost->shift_const)
14552             {
14553               *total = COSTS_N_INSNS (ix86_cost->lea);
14554               return false;
14555             }
14556         }
14557       /* FALLTHRU */
14558
14559     case ROTATE:
14560     case ASHIFTRT:
14561     case LSHIFTRT:
14562     case ROTATERT:
14563       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
14564         {
14565           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
14566             {
14567               if (INTVAL (XEXP (x, 1)) > 32)
14568                 *total = COSTS_N_INSNS(ix86_cost->shift_const + 2);
14569               else
14570                 *total = COSTS_N_INSNS(ix86_cost->shift_const * 2);
14571             }
14572           else
14573             {
14574               if (GET_CODE (XEXP (x, 1)) == AND)
14575                 *total = COSTS_N_INSNS(ix86_cost->shift_var * 2);
14576               else
14577                 *total = COSTS_N_INSNS(ix86_cost->shift_var * 6 + 2);
14578             }
14579         }
14580       else
14581         {
14582           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
14583             *total = COSTS_N_INSNS (ix86_cost->shift_const);
14584           else
14585             *total = COSTS_N_INSNS (ix86_cost->shift_var);
14586         }
14587       return false;
14588
14589     case MULT:
14590       if (FLOAT_MODE_P (mode))
14591         *total = COSTS_N_INSNS (ix86_cost->fmul);
14592       else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
14593         {
14594           unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
14595           int nbits;
14596
14597           for (nbits = 0; value != 0; value >>= 1)
14598             nbits++;
14599
14600           *total = COSTS_N_INSNS (ix86_cost->mult_init[MODE_INDEX (mode)]
14601                                   + nbits * ix86_cost->mult_bit);
14602         }
14603       else
14604         {
14605           /* This is arbitrary */
14606           *total = COSTS_N_INSNS (ix86_cost->mult_init[MODE_INDEX (mode)]
14607                                   + 7 * ix86_cost->mult_bit);
14608         }
14609       return false;
14610
14611     case DIV:
14612     case UDIV:
14613     case MOD:
14614     case UMOD:
14615       if (FLOAT_MODE_P (mode))
14616         *total = COSTS_N_INSNS (ix86_cost->fdiv);
14617       else
14618         *total = COSTS_N_INSNS (ix86_cost->divide[MODE_INDEX (mode)]);
14619       return false;
14620
14621     case PLUS:
14622       if (FLOAT_MODE_P (mode))
14623         *total = COSTS_N_INSNS (ix86_cost->fadd);
14624       else if (!TARGET_DECOMPOSE_LEA
14625                && GET_MODE_CLASS (mode) == MODE_INT
14626                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
14627         {
14628           if (GET_CODE (XEXP (x, 0)) == PLUS
14629               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
14630               && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
14631               && CONSTANT_P (XEXP (x, 1)))
14632             {
14633               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
14634               if (val == 2 || val == 4 || val == 8)
14635                 {
14636                   *total = COSTS_N_INSNS (ix86_cost->lea);
14637                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
14638                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
14639                                       outer_code);
14640                   *total += rtx_cost (XEXP (x, 1), outer_code);
14641                   return true;
14642                 }
14643             }
14644           else if (GET_CODE (XEXP (x, 0)) == MULT
14645                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
14646             {
14647               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
14648               if (val == 2 || val == 4 || val == 8)
14649                 {
14650                   *total = COSTS_N_INSNS (ix86_cost->lea);
14651                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
14652                   *total += rtx_cost (XEXP (x, 1), outer_code);
14653                   return true;
14654                 }
14655             }
14656           else if (GET_CODE (XEXP (x, 0)) == PLUS)
14657             {
14658               *total = COSTS_N_INSNS (ix86_cost->lea);
14659               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
14660               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
14661               *total += rtx_cost (XEXP (x, 1), outer_code);
14662               return true;
14663             }
14664         }
14665       /* FALLTHRU */
14666
14667     case MINUS:
14668       if (FLOAT_MODE_P (mode))
14669         {
14670           *total = COSTS_N_INSNS (ix86_cost->fadd);
14671           return false;
14672         }
14673       /* FALLTHRU */
14674
14675     case AND:
14676     case IOR:
14677     case XOR:
14678       if (!TARGET_64BIT && mode == DImode)
14679         {
14680           *total = (COSTS_N_INSNS (ix86_cost->add) * 2
14681                     + (rtx_cost (XEXP (x, 0), outer_code)
14682                        << (GET_MODE (XEXP (x, 0)) != DImode))
14683                     + (rtx_cost (XEXP (x, 1), outer_code)
14684                        << (GET_MODE (XEXP (x, 1)) != DImode)));
14685           return true;
14686         }
14687       /* FALLTHRU */
14688
14689     case NEG:
14690       if (FLOAT_MODE_P (mode))
14691         {
14692           *total = COSTS_N_INSNS (ix86_cost->fchs);
14693           return false;
14694         }
14695       /* FALLTHRU */
14696
14697     case NOT:
14698       if (!TARGET_64BIT && mode == DImode)
14699         *total = COSTS_N_INSNS (ix86_cost->add * 2);
14700       else
14701         *total = COSTS_N_INSNS (ix86_cost->add);
14702       return false;
14703
14704     case FLOAT_EXTEND:
14705       if (!TARGET_SSE_MATH || !VALID_SSE_REG_MODE (mode))
14706         *total = 0;
14707       return false;
14708
14709     case ABS:
14710       if (FLOAT_MODE_P (mode))
14711         *total = COSTS_N_INSNS (ix86_cost->fabs);
14712       return false;
14713
14714     case SQRT:
14715       if (FLOAT_MODE_P (mode))
14716         *total = COSTS_N_INSNS (ix86_cost->fsqrt);
14717       return false;
14718
14719     default:
14720       return false;
14721     }
14722 }
14723
14724 #if defined (DO_GLOBAL_CTORS_BODY) && defined (HAS_INIT_SECTION)
14725 static void
14726 ix86_svr3_asm_out_constructor (symbol, priority)
14727      rtx symbol;
14728      int priority ATTRIBUTE_UNUSED;
14729 {
14730   init_section ();
14731   fputs ("\tpushl $", asm_out_file);
14732   assemble_name (asm_out_file, XSTR (symbol, 0));
14733   fputc ('\n', asm_out_file);
14734 }
14735 #endif
14736
14737 #if TARGET_MACHO
14738
14739 static int current_machopic_label_num;
14740
14741 /* Given a symbol name and its associated stub, write out the
14742    definition of the stub.  */
14743
14744 void
14745 machopic_output_stub (file, symb, stub)
14746      FILE *file;
14747      const char *symb, *stub;
14748 {
14749   unsigned int length;
14750   char *binder_name, *symbol_name, lazy_ptr_name[32];
14751   int label = ++current_machopic_label_num;
14752
14753   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
14754   symb = (*targetm.strip_name_encoding) (symb);
14755
14756   length = strlen (stub);
14757   binder_name = alloca (length + 32);
14758   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
14759
14760   length = strlen (symb);
14761   symbol_name = alloca (length + 32);
14762   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
14763
14764   sprintf (lazy_ptr_name, "L%d$lz", label);
14765
14766   if (MACHOPIC_PURE)
14767     machopic_picsymbol_stub_section ();
14768   else
14769     machopic_symbol_stub_section ();
14770
14771   fprintf (file, "%s:\n", stub);
14772   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
14773
14774   if (MACHOPIC_PURE)
14775     {
14776       fprintf (file, "\tcall LPC$%d\nLPC$%d:\tpopl %%eax\n", label, label);
14777       fprintf (file, "\tmovl %s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
14778       fprintf (file, "\tjmp %%edx\n");
14779     }
14780   else
14781     fprintf (file, "\tjmp *%s\n", lazy_ptr_name);
14782   
14783   fprintf (file, "%s:\n", binder_name);
14784   
14785   if (MACHOPIC_PURE)
14786     {
14787       fprintf (file, "\tlea %s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
14788       fprintf (file, "\tpushl %%eax\n");
14789     }
14790   else
14791     fprintf (file, "\t pushl $%s\n", lazy_ptr_name);
14792
14793   fprintf (file, "\tjmp dyld_stub_binding_helper\n");
14794
14795   machopic_lazy_symbol_ptr_section ();
14796   fprintf (file, "%s:\n", lazy_ptr_name);
14797   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
14798   fprintf (file, "\t.long %s\n", binder_name);
14799 }
14800 #endif /* TARGET_MACHO */
14801
14802 /* Order the registers for register allocator.  */
14803
14804 void
14805 x86_order_regs_for_local_alloc ()
14806 {
14807    int pos = 0;
14808    int i;
14809
14810    /* First allocate the local general purpose registers.  */
14811    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
14812      if (GENERAL_REGNO_P (i) && call_used_regs[i])
14813         reg_alloc_order [pos++] = i;
14814
14815    /* Global general purpose registers.  */
14816    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
14817      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
14818         reg_alloc_order [pos++] = i;
14819
14820    /* x87 registers come first in case we are doing FP math
14821       using them.  */
14822    if (!TARGET_SSE_MATH)
14823      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
14824        reg_alloc_order [pos++] = i;
14825
14826    /* SSE registers.  */
14827    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
14828      reg_alloc_order [pos++] = i;
14829    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
14830      reg_alloc_order [pos++] = i;
14831
14832    /* x87 registers.  */
14833    if (TARGET_SSE_MATH)
14834      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
14835        reg_alloc_order [pos++] = i;
14836
14837    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
14838      reg_alloc_order [pos++] = i;
14839
14840    /* Initialize the rest of array as we do not allocate some registers
14841       at all.  */
14842    while (pos < FIRST_PSEUDO_REGISTER)
14843      reg_alloc_order [pos++] = 0;
14844 }
14845
14846 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
14847 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
14848 #endif
14849
14850 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
14851    struct attribute_spec.handler.  */
14852 static tree
14853 ix86_handle_struct_attribute (node, name, args, flags, no_add_attrs)
14854      tree *node;
14855      tree name;
14856      tree args ATTRIBUTE_UNUSED;
14857      int flags ATTRIBUTE_UNUSED;
14858      bool *no_add_attrs;
14859 {
14860   tree *type = NULL;
14861   if (DECL_P (*node))
14862     {
14863       if (TREE_CODE (*node) == TYPE_DECL)
14864         type = &TREE_TYPE (*node);
14865     }
14866   else
14867     type = node;
14868
14869   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
14870                  || TREE_CODE (*type) == UNION_TYPE)))
14871     {
14872       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
14873       *no_add_attrs = true;
14874     }
14875
14876   else if ((is_attribute_p ("ms_struct", name)
14877             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
14878            || ((is_attribute_p ("gcc_struct", name)
14879                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
14880     {
14881       warning ("`%s' incompatible attribute ignored",
14882                IDENTIFIER_POINTER (name));
14883       *no_add_attrs = true;
14884     }
14885
14886   return NULL_TREE;
14887 }
14888
14889 static bool
14890 ix86_ms_bitfield_layout_p (record_type)
14891      tree record_type;
14892 {
14893   return (TARGET_USE_MS_BITFIELD_LAYOUT &&
14894       !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
14895     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
14896 }
14897
14898 /* Returns an expression indicating where the this parameter is
14899    located on entry to the FUNCTION.  */
14900
14901 static rtx
14902 x86_this_parameter (function)
14903      tree function;
14904 {
14905   tree type = TREE_TYPE (function);
14906
14907   if (TARGET_64BIT)
14908     {
14909       int n = aggregate_value_p (TREE_TYPE (type)) != 0;
14910       return gen_rtx_REG (DImode, x86_64_int_parameter_registers[n]);
14911     }
14912
14913   if (ix86_fntype_regparm (type) > 0)
14914     {
14915       tree parm;
14916
14917       parm = TYPE_ARG_TYPES (type);
14918       /* Figure out whether or not the function has a variable number of
14919          arguments.  */
14920       for (; parm; parm = TREE_CHAIN (parm))
14921         if (TREE_VALUE (parm) == void_type_node)
14922           break;
14923       /* If not, the this parameter is in %eax.  */
14924       if (parm)
14925         return gen_rtx_REG (SImode, 0);
14926     }
14927
14928   if (aggregate_value_p (TREE_TYPE (type)))
14929     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 8));
14930   else
14931     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 4));
14932 }
14933
14934 /* Determine whether x86_output_mi_thunk can succeed.  */
14935
14936 static bool
14937 x86_can_output_mi_thunk (thunk, delta, vcall_offset, function)
14938      tree thunk ATTRIBUTE_UNUSED;
14939      HOST_WIDE_INT delta ATTRIBUTE_UNUSED;
14940      HOST_WIDE_INT vcall_offset;
14941      tree function;
14942 {
14943   /* 64-bit can handle anything.  */
14944   if (TARGET_64BIT)
14945     return true;
14946
14947   /* For 32-bit, everything's fine if we have one free register.  */
14948   if (ix86_fntype_regparm (TREE_TYPE (function)) < 3)
14949     return true;
14950
14951   /* Need a free register for vcall_offset.  */
14952   if (vcall_offset)
14953     return false;
14954
14955   /* Need a free register for GOT references.  */
14956   if (flag_pic && !(*targetm.binds_local_p) (function))
14957     return false;
14958
14959   /* Otherwise ok.  */
14960   return true;
14961 }
14962
14963 /* Output the assembler code for a thunk function.  THUNK_DECL is the
14964    declaration for the thunk function itself, FUNCTION is the decl for
14965    the target function.  DELTA is an immediate constant offset to be
14966    added to THIS.  If VCALL_OFFSET is nonzero, the word at
14967    *(*this + vcall_offset) should be added to THIS.  */
14968
14969 static void
14970 x86_output_mi_thunk (file, thunk, delta, vcall_offset, function)
14971      FILE *file ATTRIBUTE_UNUSED;
14972      tree thunk ATTRIBUTE_UNUSED;
14973      HOST_WIDE_INT delta;
14974      HOST_WIDE_INT vcall_offset;
14975      tree function;
14976 {
14977   rtx xops[3];
14978   rtx this = x86_this_parameter (function);
14979   rtx this_reg, tmp;
14980
14981   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
14982      pull it in now and let DELTA benefit.  */
14983   if (REG_P (this))
14984     this_reg = this;
14985   else if (vcall_offset)
14986     {
14987       /* Put the this parameter into %eax.  */
14988       xops[0] = this;
14989       xops[1] = this_reg = gen_rtx_REG (Pmode, 0);
14990       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
14991     }
14992   else
14993     this_reg = NULL_RTX;
14994
14995   /* Adjust the this parameter by a fixed constant.  */
14996   if (delta)
14997     {
14998       xops[0] = GEN_INT (delta);
14999       xops[1] = this_reg ? this_reg : this;
15000       if (TARGET_64BIT)
15001         {
15002           if (!x86_64_general_operand (xops[0], DImode))
15003             {
15004               tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
15005               xops[1] = tmp;
15006               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
15007               xops[0] = tmp;
15008               xops[1] = this;
15009             }
15010           output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
15011         }
15012       else
15013         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
15014     }
15015
15016   /* Adjust the this parameter by a value stored in the vtable.  */
15017   if (vcall_offset)
15018     {
15019       if (TARGET_64BIT)
15020         tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
15021       else
15022         tmp = gen_rtx_REG (SImode, 2 /* ECX */);
15023
15024       xops[0] = gen_rtx_MEM (Pmode, this_reg);
15025       xops[1] = tmp;
15026       if (TARGET_64BIT)
15027         output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
15028       else
15029         output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
15030
15031       /* Adjust the this parameter.  */
15032       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
15033       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
15034         {
15035           rtx tmp2 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
15036           xops[0] = GEN_INT (vcall_offset);
15037           xops[1] = tmp2;
15038           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
15039           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
15040         }
15041       xops[1] = this_reg;
15042       if (TARGET_64BIT)
15043         output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
15044       else
15045         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
15046     }
15047
15048   /* If necessary, drop THIS back to its stack slot.  */
15049   if (this_reg && this_reg != this)
15050     {
15051       xops[0] = this_reg;
15052       xops[1] = this;
15053       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
15054     }
15055
15056   xops[0] = DECL_RTL (function);
15057   if (TARGET_64BIT)
15058     {
15059       if (!flag_pic || (*targetm.binds_local_p) (function))
15060         output_asm_insn ("jmp\t%P0", xops);
15061       else
15062         {
15063           tmp = XEXP (xops[0], 0);
15064           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, tmp), UNSPEC_GOTPCREL);
15065           tmp = gen_rtx_CONST (Pmode, tmp);
15066           tmp = gen_rtx_MEM (QImode, tmp);
15067           xops[0] = tmp;
15068           output_asm_insn ("jmp\t%A0", xops);
15069         }
15070     }
15071   else
15072     {
15073       if (!flag_pic || (*targetm.binds_local_p) (function))
15074         output_asm_insn ("jmp\t%P0", xops);
15075       else
15076 #if TARGET_MACHO
15077         if (TARGET_MACHO)
15078           {
15079             char *ip = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
15080             tmp = gen_rtx_SYMBOL_REF (Pmode, machopic_stub_name (ip));
15081             tmp = gen_rtx_MEM (QImode, tmp);
15082             xops[0] = tmp;
15083             output_asm_insn ("jmp\t%0", xops);
15084           }
15085         else
15086 #endif /* TARGET_MACHO */
15087         {
15088           tmp = gen_rtx_REG (SImode, 2 /* ECX */);
15089           output_set_got (tmp);
15090
15091           xops[1] = tmp;
15092           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
15093           output_asm_insn ("jmp\t{*}%1", xops);
15094         }
15095     }
15096 }
15097
15098 int
15099 x86_field_alignment (field, computed)
15100      tree field;
15101      int computed;
15102 {
15103   enum machine_mode mode;
15104   tree type = TREE_TYPE (field);
15105
15106   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
15107     return computed;
15108   mode = TYPE_MODE (TREE_CODE (type) == ARRAY_TYPE
15109                     ? get_inner_array_type (type) : type);
15110   if (mode == DFmode || mode == DCmode
15111       || GET_MODE_CLASS (mode) == MODE_INT
15112       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
15113     return MIN (32, computed);
15114   return computed;
15115 }
15116
15117 /* Output assembler code to FILE to increment profiler label # LABELNO
15118    for profiling a function entry.  */
15119 void
15120 x86_function_profiler (file, labelno)
15121      FILE *file;
15122      int labelno ATTRIBUTE_UNUSED;
15123 {
15124   if (TARGET_64BIT)
15125     if (flag_pic)
15126       {
15127 #ifndef NO_PROFILE_COUNTERS
15128         fprintf (file, "\tleaq\t%sP%d@(%%rip),%%r11\n", LPREFIX, labelno);
15129 #endif
15130         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", MCOUNT_NAME);
15131       }
15132     else
15133       {
15134 #ifndef NO_PROFILE_COUNTERS
15135         fprintf (file, "\tmovq\t$%sP%d,%%r11\n", LPREFIX, labelno);
15136 #endif
15137         fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
15138       }
15139   else if (flag_pic)
15140     {
15141 #ifndef NO_PROFILE_COUNTERS
15142       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%%s\n",
15143                LPREFIX, labelno, PROFILE_COUNT_REGISTER);
15144 #endif
15145       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", MCOUNT_NAME);
15146     }
15147   else
15148     {
15149 #ifndef NO_PROFILE_COUNTERS
15150       fprintf (file, "\tmovl\t$%sP%d,%%$%s\n", LPREFIX, labelno,
15151                PROFILE_COUNT_REGISTER);
15152 #endif
15153       fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
15154     }
15155 }
15156
15157 /* Implement machine specific optimizations.  
15158    At the moment we implement single transformation: AMD Athlon works faster
15159    when RET is not destination of conditional jump or directly preceded
15160    by other jump instruction.  We avoid the penalty by inserting NOP just
15161    before the RET instructions in such cases.  */
15162 void
15163 x86_machine_dependent_reorg (first)
15164      rtx first ATTRIBUTE_UNUSED;
15165 {
15166   edge e;
15167
15168   if (!TARGET_ATHLON_K8 || !optimize || optimize_size)
15169     return;
15170   for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
15171   {
15172     basic_block bb = e->src;
15173     rtx ret = bb->end;
15174     rtx prev;
15175     bool insert = false;
15176
15177     if (!returnjump_p (ret) || !maybe_hot_bb_p (bb))
15178       continue;
15179     for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
15180       if (active_insn_p (prev) || GET_CODE (prev) == CODE_LABEL)
15181         break;
15182     if (prev && GET_CODE (prev) == CODE_LABEL)
15183       {
15184         edge e;
15185         for (e = bb->pred; e; e = e->pred_next)
15186           if (EDGE_FREQUENCY (e) && e->src->index >= 0
15187               && !(e->flags & EDGE_FALLTHRU))
15188             insert = 1;
15189       }
15190     if (!insert)
15191       {
15192         prev = prev_active_insn (ret);
15193         if (prev && GET_CODE (prev) == JUMP_INSN
15194             && any_condjump_p (prev))
15195           insert = 1;
15196         /* Empty functions get branch misspredict even when the jump destination
15197            is not visible to us.  */
15198         if (!prev && cfun->function_frequency > FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
15199           insert = 1;
15200       }
15201     if (insert)
15202       emit_insn_before (gen_nop (), ret);
15203   }
15204 }
15205
15206 /* Return nonzero when QImode register that must be represented via REX prefix
15207    is used.  */
15208 bool
15209 x86_extended_QIreg_mentioned_p (insn)
15210      rtx insn;
15211 {
15212   int i;
15213   extract_insn_cached (insn);
15214   for (i = 0; i < recog_data.n_operands; i++)
15215     if (REG_P (recog_data.operand[i])
15216         && REGNO (recog_data.operand[i]) >= 4)
15217        return true;
15218   return false;
15219 }
15220
15221 /* Return nonzero when P points to register encoded via REX prefix.
15222    Called via for_each_rtx.  */
15223 static int
15224 extended_reg_mentioned_1 (p, data)
15225         rtx *p;
15226         void *data ATTRIBUTE_UNUSED;
15227 {
15228    unsigned int regno;
15229    if (!REG_P (*p))
15230      return 0;
15231    regno = REGNO (*p);
15232    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
15233 }
15234
15235 /* Return true when INSN mentions register that must be encoded using REX
15236    prefix.  */
15237 bool
15238 x86_extended_reg_mentioned_p (insn)
15239      rtx insn;
15240 {
15241   return for_each_rtx (&PATTERN (insn), extended_reg_mentioned_1, NULL);
15242 }
15243
15244 #include "gt-i386.h"